blob: f605204de3e5eef414634526e5fac57d55bcb277 [file] [log] [blame]
David Brownell2e55cc72005-08-31 09:53:10 -07001/*
2 * ASIX AX8817X based USB 2.0 Ethernet Devices
David Hollis933a27d2006-07-29 10:12:50 -04003 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
David Brownell2e55cc72005-08-31 09:53:10 -07004 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
David Hollis933a27d2006-07-29 10:12:50 -04005 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
David Brownell2e55cc72005-08-31 09:53:10 -07006 * Copyright (c) 2002-2003 TiVo Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23// #define DEBUG // error path messages, extra info
24// #define VERBOSE // more; success messages
25
David Brownell2e55cc72005-08-31 09:53:10 -070026#include <linux/module.h>
27#include <linux/kmod.h>
David Brownell2e55cc72005-08-31 09:53:10 -070028#include <linux/init.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/ethtool.h>
32#include <linux/workqueue.h>
33#include <linux/mii.h>
34#include <linux/usb.h>
35#include <linux/crc32.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020036#include <linux/usb/usbnet.h>
David Brownell2e55cc72005-08-31 09:53:10 -070037
David Hollis933a27d2006-07-29 10:12:50 -040038#define DRIVER_VERSION "14-Jun-2006"
39static const char driver_name [] = "asix";
40
David Brownell2e55cc72005-08-31 09:53:10 -070041/* ASIX AX8817X based USB 2.0 Ethernet Devices */
42
43#define AX_CMD_SET_SW_MII 0x06
44#define AX_CMD_READ_MII_REG 0x07
45#define AX_CMD_WRITE_MII_REG 0x08
46#define AX_CMD_SET_HW_MII 0x0a
47#define AX_CMD_READ_EEPROM 0x0b
48#define AX_CMD_WRITE_EEPROM 0x0c
49#define AX_CMD_WRITE_ENABLE 0x0d
50#define AX_CMD_WRITE_DISABLE 0x0e
David Hollis933a27d2006-07-29 10:12:50 -040051#define AX_CMD_READ_RX_CTL 0x0f
David Brownell2e55cc72005-08-31 09:53:10 -070052#define AX_CMD_WRITE_RX_CTL 0x10
53#define AX_CMD_READ_IPG012 0x11
54#define AX_CMD_WRITE_IPG0 0x12
55#define AX_CMD_WRITE_IPG1 0x13
David Hollis933a27d2006-07-29 10:12:50 -040056#define AX_CMD_READ_NODE_ID 0x13
David Brownell2e55cc72005-08-31 09:53:10 -070057#define AX_CMD_WRITE_IPG2 0x14
58#define AX_CMD_WRITE_MULTI_FILTER 0x16
David Hollis933a27d2006-07-29 10:12:50 -040059#define AX88172_CMD_READ_NODE_ID 0x17
David Brownell2e55cc72005-08-31 09:53:10 -070060#define AX_CMD_READ_PHY_ID 0x19
61#define AX_CMD_READ_MEDIUM_STATUS 0x1a
62#define AX_CMD_WRITE_MEDIUM_MODE 0x1b
63#define AX_CMD_READ_MONITOR_MODE 0x1c
64#define AX_CMD_WRITE_MONITOR_MODE 0x1d
David Hollis933a27d2006-07-29 10:12:50 -040065#define AX_CMD_READ_GPIOS 0x1e
David Brownell2e55cc72005-08-31 09:53:10 -070066#define AX_CMD_WRITE_GPIOS 0x1f
67#define AX_CMD_SW_RESET 0x20
68#define AX_CMD_SW_PHY_STATUS 0x21
69#define AX_CMD_SW_PHY_SELECT 0x22
David Brownell2e55cc72005-08-31 09:53:10 -070070
71#define AX_MONITOR_MODE 0x01
72#define AX_MONITOR_LINK 0x02
73#define AX_MONITOR_MAGIC 0x04
74#define AX_MONITOR_HSFS 0x10
75
76/* AX88172 Medium Status Register values */
David Hollis933a27d2006-07-29 10:12:50 -040077#define AX88172_MEDIUM_FD 0x02
78#define AX88172_MEDIUM_TX 0x04
79#define AX88172_MEDIUM_FC 0x10
80#define AX88172_MEDIUM_DEFAULT \
81 ( AX88172_MEDIUM_FD | AX88172_MEDIUM_TX | AX88172_MEDIUM_FC )
David Brownell2e55cc72005-08-31 09:53:10 -070082
83#define AX_MCAST_FILTER_SIZE 8
84#define AX_MAX_MCAST 64
85
David Brownell2e55cc72005-08-31 09:53:10 -070086#define AX_SWRESET_CLEAR 0x00
87#define AX_SWRESET_RR 0x01
88#define AX_SWRESET_RT 0x02
89#define AX_SWRESET_PRTE 0x04
90#define AX_SWRESET_PRL 0x08
91#define AX_SWRESET_BZ 0x10
92#define AX_SWRESET_IPRL 0x20
93#define AX_SWRESET_IPPD 0x40
94
95#define AX88772_IPG0_DEFAULT 0x15
96#define AX88772_IPG1_DEFAULT 0x0c
97#define AX88772_IPG2_DEFAULT 0x12
98
David Hollis933a27d2006-07-29 10:12:50 -040099/* AX88772 & AX88178 Medium Mode Register */
100#define AX_MEDIUM_PF 0x0080
101#define AX_MEDIUM_JFE 0x0040
102#define AX_MEDIUM_TFC 0x0020
103#define AX_MEDIUM_RFC 0x0010
104#define AX_MEDIUM_ENCK 0x0008
105#define AX_MEDIUM_AC 0x0004
106#define AX_MEDIUM_FD 0x0002
107#define AX_MEDIUM_GM 0x0001
108#define AX_MEDIUM_SM 0x1000
109#define AX_MEDIUM_SBP 0x0800
110#define AX_MEDIUM_PS 0x0200
111#define AX_MEDIUM_RE 0x0100
David Brownell2e55cc72005-08-31 09:53:10 -0700112
David Hollis933a27d2006-07-29 10:12:50 -0400113#define AX88178_MEDIUM_DEFAULT \
114 (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
115 AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
116 AX_MEDIUM_RE )
117
118#define AX88772_MEDIUM_DEFAULT \
119 (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
120 AX_MEDIUM_TFC | AX_MEDIUM_PS | \
121 AX_MEDIUM_AC | AX_MEDIUM_RE )
122
123/* AX88772 & AX88178 RX_CTL values */
124#define AX_RX_CTL_SO 0x0080
125#define AX_RX_CTL_AP 0x0020
126#define AX_RX_CTL_AM 0x0010
127#define AX_RX_CTL_AB 0x0008
128#define AX_RX_CTL_SEP 0x0004
129#define AX_RX_CTL_AMALL 0x0002
130#define AX_RX_CTL_PRO 0x0001
131#define AX_RX_CTL_MFB_2048 0x0000
132#define AX_RX_CTL_MFB_4096 0x0100
133#define AX_RX_CTL_MFB_8192 0x0200
134#define AX_RX_CTL_MFB_16384 0x0300
135
136#define AX_DEFAULT_RX_CTL \
137 (AX_RX_CTL_SO | AX_RX_CTL_AB )
138
139/* GPIO 0 .. 2 toggles */
140#define AX_GPIO_GPO0EN 0x01 /* GPIO0 Output enable */
141#define AX_GPIO_GPO_0 0x02 /* GPIO0 Output value */
142#define AX_GPIO_GPO1EN 0x04 /* GPIO1 Output enable */
143#define AX_GPIO_GPO_1 0x08 /* GPIO1 Output value */
144#define AX_GPIO_GPO2EN 0x10 /* GPIO2 Output enable */
145#define AX_GPIO_GPO_2 0x20 /* GPIO2 Output value */
146#define AX_GPIO_RESERVED 0x40 /* Reserved */
147#define AX_GPIO_RSE 0x80 /* Reload serial EEPROM */
148
149#define AX_EEPROM_MAGIC 0xdeadbeef
150#define AX88172_EEPROM_LEN 0x40
151#define AX88772_EEPROM_LEN 0xff
152
153#define PHY_MODE_MARVELL 0x0000
154#define MII_MARVELL_LED_CTRL 0x0018
155#define MII_MARVELL_STATUS 0x001b
156#define MII_MARVELL_CTRL 0x0014
157
158#define MARVELL_LED_MANUAL 0x0019
159
160#define MARVELL_STATUS_HWCFG 0x0004
161
162#define MARVELL_CTRL_TXDELAY 0x0002
163#define MARVELL_CTRL_RXDELAY 0x0080
David Brownell2e55cc72005-08-31 09:53:10 -0700164
165/* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
David Hollis48b1be62006-03-28 20:15:42 -0500166struct asix_data {
David Brownell2e55cc72005-08-31 09:53:10 -0700167 u8 multi_filter[AX_MCAST_FILTER_SIZE];
David Hollis933a27d2006-07-29 10:12:50 -0400168 u8 phymode;
169 u8 ledmode;
170 u8 eeprom_len;
David Brownell2e55cc72005-08-31 09:53:10 -0700171};
172
173struct ax88172_int_data {
Al Viro51bf2972007-12-22 17:42:36 +0000174 __le16 res1;
David Brownell2e55cc72005-08-31 09:53:10 -0700175 u8 link;
Al Viro51bf2972007-12-22 17:42:36 +0000176 __le16 res2;
David Brownell2e55cc72005-08-31 09:53:10 -0700177 u8 status;
Al Viro51bf2972007-12-22 17:42:36 +0000178 __le16 res3;
David Brownell2e55cc72005-08-31 09:53:10 -0700179} __attribute__ ((packed));
180
David Hollis48b1be62006-03-28 20:15:42 -0500181static int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700182 u16 size, void *data)
183{
Al Viro51bf2972007-12-22 17:42:36 +0000184 void *buf;
185 int err = -ENOMEM;
186
Joe Perches60b86752010-02-17 10:30:23 +0000187 netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
188 cmd, value, index, size);
Al Viro51bf2972007-12-22 17:42:36 +0000189
190 buf = kmalloc(size, GFP_KERNEL);
191 if (!buf)
192 goto out;
193
194 err = usb_control_msg(
David Brownell2e55cc72005-08-31 09:53:10 -0700195 dev->udev,
196 usb_rcvctrlpipe(dev->udev, 0),
197 cmd,
198 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
199 value,
200 index,
Al Viro51bf2972007-12-22 17:42:36 +0000201 buf,
David Brownell2e55cc72005-08-31 09:53:10 -0700202 size,
203 USB_CTRL_GET_TIMEOUT);
Russ Dill94d43362008-01-09 21:32:07 -0700204 if (err == size)
Al Viro51bf2972007-12-22 17:42:36 +0000205 memcpy(data, buf, size);
Russ Dill94d43362008-01-09 21:32:07 -0700206 else if (err >= 0)
207 err = -EINVAL;
Al Viro51bf2972007-12-22 17:42:36 +0000208 kfree(buf);
209
210out:
211 return err;
David Brownell2e55cc72005-08-31 09:53:10 -0700212}
213
David Hollis48b1be62006-03-28 20:15:42 -0500214static int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
David Brownell2e55cc72005-08-31 09:53:10 -0700215 u16 size, void *data)
216{
Al Viro51bf2972007-12-22 17:42:36 +0000217 void *buf = NULL;
218 int err = -ENOMEM;
219
Joe Perches60b86752010-02-17 10:30:23 +0000220 netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
221 cmd, value, index, size);
Al Viro51bf2972007-12-22 17:42:36 +0000222
223 if (data) {
224 buf = kmalloc(size, GFP_KERNEL);
225 if (!buf)
226 goto out;
227 memcpy(buf, data, size);
228 }
229
230 err = usb_control_msg(
David Brownell2e55cc72005-08-31 09:53:10 -0700231 dev->udev,
232 usb_sndctrlpipe(dev->udev, 0),
233 cmd,
234 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
235 value,
236 index,
Al Viro51bf2972007-12-22 17:42:36 +0000237 buf,
David Brownell2e55cc72005-08-31 09:53:10 -0700238 size,
239 USB_CTRL_SET_TIMEOUT);
Al Viro51bf2972007-12-22 17:42:36 +0000240 kfree(buf);
241
242out:
243 return err;
David Brownell2e55cc72005-08-31 09:53:10 -0700244}
245
David Howells7d12e782006-10-05 14:55:46 +0100246static void asix_async_cmd_callback(struct urb *urb)
David Brownell2e55cc72005-08-31 09:53:10 -0700247{
248 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800249 int status = urb->status;
David Brownell2e55cc72005-08-31 09:53:10 -0700250
Oliver Neukumc94cb312008-12-18 23:00:59 -0800251 if (status < 0)
David Hollis48b1be62006-03-28 20:15:42 -0500252 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800253 status);
David Brownell2e55cc72005-08-31 09:53:10 -0700254
255 kfree(req);
256 usb_free_urb(urb);
257}
258
David Hollis933a27d2006-07-29 10:12:50 -0400259static void
260asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
261 u16 size, void *data)
David Hollis48b1be62006-03-28 20:15:42 -0500262{
David Hollis933a27d2006-07-29 10:12:50 -0400263 struct usb_ctrlrequest *req;
264 int status;
265 struct urb *urb;
David Hollis48b1be62006-03-28 20:15:42 -0500266
Joe Perches60b86752010-02-17 10:30:23 +0000267 netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
268 cmd, value, index, size);
David Hollis933a27d2006-07-29 10:12:50 -0400269 if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
Joe Perches60b86752010-02-17 10:30:23 +0000270 netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
David Hollis933a27d2006-07-29 10:12:50 -0400271 return;
David Hollis48b1be62006-03-28 20:15:42 -0500272 }
David Hollis933a27d2006-07-29 10:12:50 -0400273
274 if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
Joe Perches60b86752010-02-17 10:30:23 +0000275 netdev_err(dev->net, "Failed to allocate memory for control request\n");
David Hollis933a27d2006-07-29 10:12:50 -0400276 usb_free_urb(urb);
277 return;
278 }
279
280 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
281 req->bRequest = cmd;
Oliver Neukum9aa742e2006-11-23 12:45:31 +0100282 req->wValue = cpu_to_le16(value);
283 req->wIndex = cpu_to_le16(index);
284 req->wLength = cpu_to_le16(size);
David Hollis933a27d2006-07-29 10:12:50 -0400285
286 usb_fill_control_urb(urb, dev->udev,
287 usb_sndctrlpipe(dev->udev, 0),
288 (void *)req, data, size,
289 asix_async_cmd_callback, req);
290
291 if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000292 netdev_err(dev->net, "Error submitting the control message: status=%d\n",
293 status);
David Hollis933a27d2006-07-29 10:12:50 -0400294 kfree(req);
295 usb_free_urb(urb);
296 }
David Hollis48b1be62006-03-28 20:15:42 -0500297}
298
David Hollis933a27d2006-07-29 10:12:50 -0400299static int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
David Hollis48b1be62006-03-28 20:15:42 -0500300{
David Hollis933a27d2006-07-29 10:12:50 -0400301 u8 *head;
302 u32 header;
303 char *packet;
304 struct sk_buff *ax_skb;
305 u16 size;
David Hollis48b1be62006-03-28 20:15:42 -0500306
David Hollis933a27d2006-07-29 10:12:50 -0400307 head = (u8 *) skb->data;
308 memcpy(&header, head, sizeof(header));
309 le32_to_cpus(&header);
310 packet = head + sizeof(header);
David Hollis48b1be62006-03-28 20:15:42 -0500311
David Hollis933a27d2006-07-29 10:12:50 -0400312 skb_pull(skb, 4);
313
314 while (skb->len > 0) {
315 if ((short)(header & 0x0000ffff) !=
316 ~((short)((header & 0xffff0000) >> 16))) {
Joe Perches60b86752010-02-17 10:30:23 +0000317 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
David Hollis933a27d2006-07-29 10:12:50 -0400318 }
319 /* get the packet length */
320 size = (u16) (header & 0x0000ffff);
321
322 if ((skb->len) - ((size + 1) & 0xfffe) == 0)
323 return 2;
324 if (size > ETH_FRAME_LEN) {
Joe Perches60b86752010-02-17 10:30:23 +0000325 netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
326 size);
David Hollis933a27d2006-07-29 10:12:50 -0400327 return 0;
328 }
329 ax_skb = skb_clone(skb, GFP_ATOMIC);
330 if (ax_skb) {
331 ax_skb->len = size;
332 ax_skb->data = packet;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700333 skb_set_tail_pointer(ax_skb, size);
David Hollis933a27d2006-07-29 10:12:50 -0400334 usbnet_skb_return(dev, ax_skb);
335 } else {
336 return 0;
337 }
338
339 skb_pull(skb, (size + 1) & 0xfffe);
340
341 if (skb->len == 0)
342 break;
343
344 head = (u8 *) skb->data;
345 memcpy(&header, head, sizeof(header));
346 le32_to_cpus(&header);
347 packet = head + sizeof(header);
348 skb_pull(skb, 4);
349 }
350
351 if (skb->len < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000352 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
353 skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400354 return 0;
355 }
356 return 1;
David Hollis48b1be62006-03-28 20:15:42 -0500357}
358
David Hollis933a27d2006-07-29 10:12:50 -0400359static struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
360 gfp_t flags)
David Hollis48b1be62006-03-28 20:15:42 -0500361{
David Hollis933a27d2006-07-29 10:12:50 -0400362 int padlen;
363 int headroom = skb_headroom(skb);
364 int tailroom = skb_tailroom(skb);
365 u32 packet_len;
366 u32 padbytes = 0xffff0000;
David Hollis48b1be62006-03-28 20:15:42 -0500367
David Hollis933a27d2006-07-29 10:12:50 -0400368 padlen = ((skb->len + 4) % 512) ? 0 : 4;
David Hollis48b1be62006-03-28 20:15:42 -0500369
Joe Perches8e95a202009-12-03 07:58:21 +0000370 if ((!skb_cloned(skb)) &&
371 ((headroom + tailroom) >= (4 + padlen))) {
David Hollis933a27d2006-07-29 10:12:50 -0400372 if ((headroom < 4) || (tailroom < padlen)) {
373 skb->data = memmove(skb->head + 4, skb->data, skb->len);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700374 skb_set_tail_pointer(skb, skb->len);
David Hollis933a27d2006-07-29 10:12:50 -0400375 }
376 } else {
377 struct sk_buff *skb2;
378 skb2 = skb_copy_expand(skb, 4, padlen, flags);
379 dev_kfree_skb_any(skb);
380 skb = skb2;
381 if (!skb)
382 return NULL;
383 }
384
385 skb_push(skb, 4);
386 packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
David Hollis57e4f042007-02-05 12:03:03 -0500387 cpu_to_le32s(&packet_len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300388 skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
David Hollis933a27d2006-07-29 10:12:50 -0400389
390 if ((skb->len % 512) == 0) {
David Hollis57e4f042007-02-05 12:03:03 -0500391 cpu_to_le32s(&padbytes);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700392 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
David Hollis933a27d2006-07-29 10:12:50 -0400393 skb_put(skb, sizeof(padbytes));
394 }
395 return skb;
David Hollis48b1be62006-03-28 20:15:42 -0500396}
397
398static void asix_status(struct usbnet *dev, struct urb *urb)
David Brownell2e55cc72005-08-31 09:53:10 -0700399{
400 struct ax88172_int_data *event;
401 int link;
402
403 if (urb->actual_length < 8)
404 return;
405
406 event = urb->transfer_buffer;
407 link = event->link & 0x01;
408 if (netif_carrier_ok(dev->net) != link) {
409 if (link) {
410 netif_carrier_on(dev->net);
411 usbnet_defer_kevent (dev, EVENT_LINK_RESET );
412 } else
413 netif_carrier_off(dev->net);
Joe Perches60b86752010-02-17 10:30:23 +0000414 netdev_dbg(dev->net, "Link Status is: %d\n", link);
David Brownell2e55cc72005-08-31 09:53:10 -0700415 }
416}
417
David Hollis933a27d2006-07-29 10:12:50 -0400418static inline int asix_set_sw_mii(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700419{
David Hollis933a27d2006-07-29 10:12:50 -0400420 int ret;
421 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
422 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000423 netdev_err(dev->net, "Failed to enable software MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400424 return ret;
David Brownell2e55cc72005-08-31 09:53:10 -0700425}
426
David Hollis933a27d2006-07-29 10:12:50 -0400427static inline int asix_set_hw_mii(struct usbnet *dev)
428{
429 int ret;
430 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
431 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000432 netdev_err(dev->net, "Failed to enable hardware MII access\n");
David Hollis933a27d2006-07-29 10:12:50 -0400433 return ret;
434}
435
436static inline int asix_get_phy_addr(struct usbnet *dev)
437{
Al Viro51bf2972007-12-22 17:42:36 +0000438 u8 buf[2];
439 int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
David Hollis933a27d2006-07-29 10:12:50 -0400440
Joe Perches60b86752010-02-17 10:30:23 +0000441 netdev_dbg(dev->net, "asix_get_phy_addr()\n");
David Hollis933a27d2006-07-29 10:12:50 -0400442
Al Viro51bf2972007-12-22 17:42:36 +0000443 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000444 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000445 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400446 }
Joe Perches60b86752010-02-17 10:30:23 +0000447 netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
448 *((__le16 *)buf));
Al Viro51bf2972007-12-22 17:42:36 +0000449 ret = buf[1];
450
451out:
David Hollis933a27d2006-07-29 10:12:50 -0400452 return ret;
453}
454
455static int asix_sw_reset(struct usbnet *dev, u8 flags)
456{
457 int ret;
458
459 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
460 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000461 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
David Hollis933a27d2006-07-29 10:12:50 -0400462
463 return ret;
464}
465
466static u16 asix_read_rx_ctl(struct usbnet *dev)
467{
Al Viro51bf2972007-12-22 17:42:36 +0000468 __le16 v;
469 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400470
Al Viro51bf2972007-12-22 17:42:36 +0000471 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000472 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000473 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400474 }
Al Viro51bf2972007-12-22 17:42:36 +0000475 ret = le16_to_cpu(v);
476out:
David Hollis933a27d2006-07-29 10:12:50 -0400477 return ret;
478}
479
480static int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
481{
482 int ret;
483
Joe Perches60b86752010-02-17 10:30:23 +0000484 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400485 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
486 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000487 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
488 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400489
490 return ret;
491}
492
493static u16 asix_read_medium_status(struct usbnet *dev)
494{
Al Viro51bf2972007-12-22 17:42:36 +0000495 __le16 v;
496 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
David Hollis933a27d2006-07-29 10:12:50 -0400497
Al Viro51bf2972007-12-22 17:42:36 +0000498 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000499 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
500 ret);
Al Viro51bf2972007-12-22 17:42:36 +0000501 goto out;
David Hollis933a27d2006-07-29 10:12:50 -0400502 }
Al Viro51bf2972007-12-22 17:42:36 +0000503 ret = le16_to_cpu(v);
504out:
David Hollis933a27d2006-07-29 10:12:50 -0400505 return ret;
506}
507
508static int asix_write_medium_mode(struct usbnet *dev, u16 mode)
509{
510 int ret;
511
Joe Perches60b86752010-02-17 10:30:23 +0000512 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
David Hollis933a27d2006-07-29 10:12:50 -0400513 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
514 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000515 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
516 mode, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400517
518 return ret;
519}
520
521static int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
522{
523 int ret;
524
Joe Perches60b86752010-02-17 10:30:23 +0000525 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
David Hollis933a27d2006-07-29 10:12:50 -0400526 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
527 if (ret < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000528 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
529 value, ret);
David Hollis933a27d2006-07-29 10:12:50 -0400530
531 if (sleep)
532 msleep(sleep);
533
534 return ret;
535}
536
537/*
538 * AX88772 & AX88178 have a 16-bit RX_CTL value
539 */
David Hollis48b1be62006-03-28 20:15:42 -0500540static void asix_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700541{
542 struct usbnet *dev = netdev_priv(net);
David Hollis48b1be62006-03-28 20:15:42 -0500543 struct asix_data *data = (struct asix_data *)&dev->data;
David Hollis933a27d2006-07-29 10:12:50 -0400544 u16 rx_ctl = AX_DEFAULT_RX_CTL;
David Brownell2e55cc72005-08-31 09:53:10 -0700545
546 if (net->flags & IFF_PROMISC) {
David Hollis933a27d2006-07-29 10:12:50 -0400547 rx_ctl |= AX_RX_CTL_PRO;
Joe Perches8e95a202009-12-03 07:58:21 +0000548 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000549 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400550 rx_ctl |= AX_RX_CTL_AMALL;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000551 } else if (netdev_mc_empty(net)) {
David Brownell2e55cc72005-08-31 09:53:10 -0700552 /* just broadcast and directed */
553 } else {
554 /* We use the 20 byte dev->data
555 * for our 8 byte filter buffer
556 * to avoid allocating memory that
557 * is tricky to free later */
558 struct dev_mc_list *mc_list = net->mc_list;
559 u32 crc_bits;
560 int i;
561
562 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
563
564 /* Build the multicast hash filter. */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000565 for (i = 0; i < netdev_mc_count(net); i++) {
David Brownell2e55cc72005-08-31 09:53:10 -0700566 crc_bits =
567 ether_crc(ETH_ALEN,
568 mc_list->dmi_addr) >> 26;
569 data->multi_filter[crc_bits >> 3] |=
570 1 << (crc_bits & 7);
571 mc_list = mc_list->next;
572 }
573
David Hollis48b1be62006-03-28 20:15:42 -0500574 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
David Brownell2e55cc72005-08-31 09:53:10 -0700575 AX_MCAST_FILTER_SIZE, data->multi_filter);
576
David Hollis933a27d2006-07-29 10:12:50 -0400577 rx_ctl |= AX_RX_CTL_AM;
David Brownell2e55cc72005-08-31 09:53:10 -0700578 }
579
David Hollis48b1be62006-03-28 20:15:42 -0500580 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700581}
582
David Hollis48b1be62006-03-28 20:15:42 -0500583static int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
David Brownell2e55cc72005-08-31 09:53:10 -0700584{
585 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000586 __le16 res;
David Brownell2e55cc72005-08-31 09:53:10 -0700587
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200588 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500589 asix_set_sw_mii(dev);
590 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
Al Viro51bf2972007-12-22 17:42:36 +0000591 (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500592 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200593 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700594
Joe Perches60b86752010-02-17 10:30:23 +0000595 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
596 phy_id, loc, le16_to_cpu(res));
David Brownell2e55cc72005-08-31 09:53:10 -0700597
Al Viro51bf2972007-12-22 17:42:36 +0000598 return le16_to_cpu(res);
David Brownell2e55cc72005-08-31 09:53:10 -0700599}
600
601static void
David Hollis48b1be62006-03-28 20:15:42 -0500602asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
David Brownell2e55cc72005-08-31 09:53:10 -0700603{
604 struct usbnet *dev = netdev_priv(netdev);
Al Viro51bf2972007-12-22 17:42:36 +0000605 __le16 res = cpu_to_le16(val);
David Brownell2e55cc72005-08-31 09:53:10 -0700606
Joe Perches60b86752010-02-17 10:30:23 +0000607 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
608 phy_id, loc, val);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200609 mutex_lock(&dev->phy_mutex);
David Hollis48b1be62006-03-28 20:15:42 -0500610 asix_set_sw_mii(dev);
Al Viro51bf2972007-12-22 17:42:36 +0000611 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
David Hollis48b1be62006-03-28 20:15:42 -0500612 asix_set_hw_mii(dev);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200613 mutex_unlock(&dev->phy_mutex);
David Brownell2e55cc72005-08-31 09:53:10 -0700614}
615
David Hollis933a27d2006-07-29 10:12:50 -0400616/* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
617static u32 asix_get_phyid(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -0700618{
David Hollis933a27d2006-07-29 10:12:50 -0400619 int phy_reg;
620 u32 phy_id;
David Brownell2e55cc72005-08-31 09:53:10 -0700621
David Hollis933a27d2006-07-29 10:12:50 -0400622 phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
623 if (phy_reg < 0)
624 return 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700625
David Hollis933a27d2006-07-29 10:12:50 -0400626 phy_id = (phy_reg & 0xffff) << 16;
David Brownell2e55cc72005-08-31 09:53:10 -0700627
David Hollis933a27d2006-07-29 10:12:50 -0400628 phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
629 if (phy_reg < 0)
630 return 0;
631
632 phy_id |= (phy_reg & 0xffff);
633
634 return phy_id;
David Brownell2e55cc72005-08-31 09:53:10 -0700635}
636
637static void
David Hollis48b1be62006-03-28 20:15:42 -0500638asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700639{
640 struct usbnet *dev = netdev_priv(net);
641 u8 opt;
642
David Hollis48b1be62006-03-28 20:15:42 -0500643 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700644 wolinfo->supported = 0;
645 wolinfo->wolopts = 0;
646 return;
647 }
648 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
649 wolinfo->wolopts = 0;
650 if (opt & AX_MONITOR_MODE) {
651 if (opt & AX_MONITOR_LINK)
652 wolinfo->wolopts |= WAKE_PHY;
653 if (opt & AX_MONITOR_MAGIC)
654 wolinfo->wolopts |= WAKE_MAGIC;
655 }
656}
657
658static int
David Hollis48b1be62006-03-28 20:15:42 -0500659asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
David Brownell2e55cc72005-08-31 09:53:10 -0700660{
661 struct usbnet *dev = netdev_priv(net);
662 u8 opt = 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700663
664 if (wolinfo->wolopts & WAKE_PHY)
665 opt |= AX_MONITOR_LINK;
666 if (wolinfo->wolopts & WAKE_MAGIC)
667 opt |= AX_MONITOR_MAGIC;
668 if (opt != 0)
669 opt |= AX_MONITOR_MODE;
670
David Hollis48b1be62006-03-28 20:15:42 -0500671 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
Al Viro51bf2972007-12-22 17:42:36 +0000672 opt, 0, 0, NULL) < 0)
David Brownell2e55cc72005-08-31 09:53:10 -0700673 return -EINVAL;
674
675 return 0;
676}
677
David Hollis48b1be62006-03-28 20:15:42 -0500678static int asix_get_eeprom_len(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700679{
David Hollis933a27d2006-07-29 10:12:50 -0400680 struct usbnet *dev = netdev_priv(net);
681 struct asix_data *data = (struct asix_data *)&dev->data;
682
683 return data->eeprom_len;
David Brownell2e55cc72005-08-31 09:53:10 -0700684}
685
David Hollis48b1be62006-03-28 20:15:42 -0500686static int asix_get_eeprom(struct net_device *net,
David Brownell2e55cc72005-08-31 09:53:10 -0700687 struct ethtool_eeprom *eeprom, u8 *data)
688{
689 struct usbnet *dev = netdev_priv(net);
Al Viro51bf2972007-12-22 17:42:36 +0000690 __le16 *ebuf = (__le16 *)data;
David Brownell2e55cc72005-08-31 09:53:10 -0700691 int i;
692
693 /* Crude hack to ensure that we don't overwrite memory
694 * if an odd length is supplied
695 */
696 if (eeprom->len % 2)
697 return -EINVAL;
698
699 eeprom->magic = AX_EEPROM_MAGIC;
700
701 /* ax8817x returns 2 bytes from eeprom on read */
702 for (i=0; i < eeprom->len / 2; i++) {
David Hollis48b1be62006-03-28 20:15:42 -0500703 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
David Brownell2e55cc72005-08-31 09:53:10 -0700704 eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
705 return -EINVAL;
706 }
707 return 0;
708}
709
David Hollis48b1be62006-03-28 20:15:42 -0500710static void asix_get_drvinfo (struct net_device *net,
David Brownell2e55cc72005-08-31 09:53:10 -0700711 struct ethtool_drvinfo *info)
712{
David Hollis933a27d2006-07-29 10:12:50 -0400713 struct usbnet *dev = netdev_priv(net);
714 struct asix_data *data = (struct asix_data *)&dev->data;
715
David Brownell2e55cc72005-08-31 09:53:10 -0700716 /* Inherit standard device info */
717 usbnet_get_drvinfo(net, info);
David Hollis933a27d2006-07-29 10:12:50 -0400718 strncpy (info->driver, driver_name, sizeof info->driver);
719 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
720 info->eedump_len = data->eeprom_len;
David Brownell2e55cc72005-08-31 09:53:10 -0700721}
722
David Hollis933a27d2006-07-29 10:12:50 -0400723static u32 asix_get_link(struct net_device *net)
724{
725 struct usbnet *dev = netdev_priv(net);
726
727 return mii_link_ok(&dev->mii);
728}
729
730static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
731{
732 struct usbnet *dev = netdev_priv(net);
733
734 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
David Brownell2e55cc72005-08-31 09:53:10 -0700735}
736
737/* We need to override some ethtool_ops so we require our
738 own structure so we don't interfere with other usbnet
739 devices that may be connected at the same time. */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700740static const struct ethtool_ops ax88172_ethtool_ops = {
David Hollis48b1be62006-03-28 20:15:42 -0500741 .get_drvinfo = asix_get_drvinfo,
David Hollis933a27d2006-07-29 10:12:50 -0400742 .get_link = asix_get_link,
David Brownell2e55cc72005-08-31 09:53:10 -0700743 .get_msglevel = usbnet_get_msglevel,
744 .set_msglevel = usbnet_set_msglevel,
David Hollis48b1be62006-03-28 20:15:42 -0500745 .get_wol = asix_get_wol,
746 .set_wol = asix_set_wol,
747 .get_eeprom_len = asix_get_eeprom_len,
748 .get_eeprom = asix_get_eeprom,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200749 .get_settings = usbnet_get_settings,
750 .set_settings = usbnet_set_settings,
751 .nway_reset = usbnet_nway_reset,
David Brownell2e55cc72005-08-31 09:53:10 -0700752};
753
David Hollis933a27d2006-07-29 10:12:50 -0400754static void ax88172_set_multicast(struct net_device *net)
David Brownell2e55cc72005-08-31 09:53:10 -0700755{
756 struct usbnet *dev = netdev_priv(net);
David Hollis933a27d2006-07-29 10:12:50 -0400757 struct asix_data *data = (struct asix_data *)&dev->data;
758 u8 rx_ctl = 0x8c;
David Brownell2e55cc72005-08-31 09:53:10 -0700759
David Hollis933a27d2006-07-29 10:12:50 -0400760 if (net->flags & IFF_PROMISC) {
761 rx_ctl |= 0x01;
Joe Perches8e95a202009-12-03 07:58:21 +0000762 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000763 netdev_mc_count(net) > AX_MAX_MCAST) {
David Hollis933a27d2006-07-29 10:12:50 -0400764 rx_ctl |= 0x02;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000765 } else if (netdev_mc_empty(net)) {
David Hollis933a27d2006-07-29 10:12:50 -0400766 /* just broadcast and directed */
767 } else {
768 /* We use the 20 byte dev->data
769 * for our 8 byte filter buffer
770 * to avoid allocating memory that
771 * is tricky to free later */
772 struct dev_mc_list *mc_list = net->mc_list;
773 u32 crc_bits;
774 int i;
775
776 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
777
778 /* Build the multicast hash filter. */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000779 for (i = 0; i < netdev_mc_count(net); i++) {
David Hollis933a27d2006-07-29 10:12:50 -0400780 crc_bits =
781 ether_crc(ETH_ALEN,
782 mc_list->dmi_addr) >> 26;
783 data->multi_filter[crc_bits >> 3] |=
784 1 << (crc_bits & 7);
785 mc_list = mc_list->next;
786 }
787
788 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
789 AX_MCAST_FILTER_SIZE, data->multi_filter);
790
791 rx_ctl |= 0x10;
792 }
793
794 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
795}
796
797static int ax88172_link_reset(struct usbnet *dev)
798{
799 u8 mode;
800 struct ethtool_cmd ecmd;
801
802 mii_check_media(&dev->mii, 1, 1);
803 mii_ethtool_gset(&dev->mii, &ecmd);
804 mode = AX88172_MEDIUM_DEFAULT;
805
806 if (ecmd.duplex != DUPLEX_FULL)
807 mode |= ~AX88172_MEDIUM_FD;
808
Joe Perches60b86752010-02-17 10:30:23 +0000809 netdev_dbg(dev->net, "ax88172_link_reset() speed: %d duplex: %d setting mode to 0x%04x\n",
810 ecmd.speed, ecmd.duplex, mode);
David Hollis933a27d2006-07-29 10:12:50 -0400811
812 asix_write_medium_mode(dev, mode);
813
814 return 0;
David Brownell2e55cc72005-08-31 09:53:10 -0700815}
816
Stephen Hemminger17033382009-03-20 19:35:55 +0000817static const struct net_device_ops ax88172_netdev_ops = {
818 .ndo_open = usbnet_open,
819 .ndo_stop = usbnet_stop,
820 .ndo_start_xmit = usbnet_start_xmit,
821 .ndo_tx_timeout = usbnet_tx_timeout,
822 .ndo_change_mtu = usbnet_change_mtu,
823 .ndo_set_mac_address = eth_mac_addr,
824 .ndo_validate_addr = eth_validate_addr,
825 .ndo_do_ioctl = asix_ioctl,
826 .ndo_set_multicast_list = ax88172_set_multicast,
827};
828
David Hollis48b1be62006-03-28 20:15:42 -0500829static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
David Brownell2e55cc72005-08-31 09:53:10 -0700830{
831 int ret = 0;
Al Viro51bf2972007-12-22 17:42:36 +0000832 u8 buf[ETH_ALEN];
David Brownell2e55cc72005-08-31 09:53:10 -0700833 int i;
834 unsigned long gpio_bits = dev->driver_info->data;
David Hollis933a27d2006-07-29 10:12:50 -0400835 struct asix_data *data = (struct asix_data *)&dev->data;
836
837 data->eeprom_len = AX88172_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700838
839 usbnet_get_endpoints(dev,intf);
840
David Brownell2e55cc72005-08-31 09:53:10 -0700841 /* Toggle the GPIOs in a manufacturer/model specific way */
842 for (i = 2; i >= 0; i--) {
David Hollis48b1be62006-03-28 20:15:42 -0500843 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
David Brownell2e55cc72005-08-31 09:53:10 -0700844 (gpio_bits >> (i * 8)) & 0xff, 0, 0,
Al Viro51bf2972007-12-22 17:42:36 +0000845 NULL)) < 0)
846 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700847 msleep(5);
848 }
849
David Hollis933a27d2006-07-29 10:12:50 -0400850 if ((ret = asix_write_rx_ctl(dev, 0x80)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000851 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700852
853 /* Get the MAC address */
David Hollis933a27d2006-07-29 10:12:50 -0400854 if ((ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID,
Al Viro51bf2972007-12-22 17:42:36 +0000855 0, 0, ETH_ALEN, buf)) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700856 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000857 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700858 }
859 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
860
David Brownell2e55cc72005-08-31 09:53:10 -0700861 /* Initialize MII structure */
862 dev->mii.dev = dev->net;
David Hollis48b1be62006-03-28 20:15:42 -0500863 dev->mii.mdio_read = asix_mdio_read;
864 dev->mii.mdio_write = asix_mdio_write;
David Brownell2e55cc72005-08-31 09:53:10 -0700865 dev->mii.phy_id_mask = 0x3f;
866 dev->mii.reg_num_mask = 0x1f;
David Hollis933a27d2006-07-29 10:12:50 -0400867 dev->mii.phy_id = asix_get_phy_addr(dev);
David Brownell2e55cc72005-08-31 09:53:10 -0700868
Stephen Hemminger17033382009-03-20 19:35:55 +0000869 dev->net->netdev_ops = &ax88172_netdev_ops;
David Hollis48b1be62006-03-28 20:15:42 -0500870 dev->net->ethtool_ops = &ax88172_ethtool_ops;
David Brownell2e55cc72005-08-31 09:53:10 -0700871
David Hollis933a27d2006-07-29 10:12:50 -0400872 asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
873 asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
David Brownell2e55cc72005-08-31 09:53:10 -0700874 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
875 mii_nway_restart(&dev->mii);
876
877 return 0;
Al Viro51bf2972007-12-22 17:42:36 +0000878
879out:
David Brownell2e55cc72005-08-31 09:53:10 -0700880 return ret;
881}
882
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700883static const struct ethtool_ops ax88772_ethtool_ops = {
David Hollis48b1be62006-03-28 20:15:42 -0500884 .get_drvinfo = asix_get_drvinfo,
David Hollis933a27d2006-07-29 10:12:50 -0400885 .get_link = asix_get_link,
David Brownell2e55cc72005-08-31 09:53:10 -0700886 .get_msglevel = usbnet_get_msglevel,
887 .set_msglevel = usbnet_set_msglevel,
David Hollis48b1be62006-03-28 20:15:42 -0500888 .get_wol = asix_get_wol,
889 .set_wol = asix_set_wol,
890 .get_eeprom_len = asix_get_eeprom_len,
891 .get_eeprom = asix_get_eeprom,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200892 .get_settings = usbnet_get_settings,
893 .set_settings = usbnet_set_settings,
894 .nway_reset = usbnet_nway_reset,
David Brownell2e55cc72005-08-31 09:53:10 -0700895};
896
David Hollis933a27d2006-07-29 10:12:50 -0400897static int ax88772_link_reset(struct usbnet *dev)
898{
899 u16 mode;
900 struct ethtool_cmd ecmd;
901
902 mii_check_media(&dev->mii, 1, 1);
903 mii_ethtool_gset(&dev->mii, &ecmd);
904 mode = AX88772_MEDIUM_DEFAULT;
905
906 if (ecmd.speed != SPEED_100)
907 mode &= ~AX_MEDIUM_PS;
908
909 if (ecmd.duplex != DUPLEX_FULL)
910 mode &= ~AX_MEDIUM_FD;
911
Joe Perches60b86752010-02-17 10:30:23 +0000912 netdev_dbg(dev->net, "ax88772_link_reset() speed: %d duplex: %d setting mode to 0x%04x\n",
913 ecmd.speed, ecmd.duplex, mode);
David Hollis933a27d2006-07-29 10:12:50 -0400914
915 asix_write_medium_mode(dev, mode);
916
917 return 0;
918}
919
Stephen Hemminger17033382009-03-20 19:35:55 +0000920static const struct net_device_ops ax88772_netdev_ops = {
921 .ndo_open = usbnet_open,
922 .ndo_stop = usbnet_stop,
923 .ndo_start_xmit = usbnet_start_xmit,
924 .ndo_tx_timeout = usbnet_tx_timeout,
925 .ndo_change_mtu = usbnet_change_mtu,
926 .ndo_set_mac_address = eth_mac_addr,
927 .ndo_validate_addr = eth_validate_addr,
928 .ndo_do_ioctl = asix_ioctl,
929 .ndo_set_multicast_list = asix_set_multicast,
930};
931
David Brownell2e55cc72005-08-31 09:53:10 -0700932static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
933{
Andres Salomond0ffff82007-01-11 18:39:16 -0500934 int ret, embd_phy;
David Hollis933a27d2006-07-29 10:12:50 -0400935 u16 rx_ctl;
936 struct asix_data *data = (struct asix_data *)&dev->data;
Al Viro51bf2972007-12-22 17:42:36 +0000937 u8 buf[ETH_ALEN];
David Hollis933a27d2006-07-29 10:12:50 -0400938 u32 phyid;
939
940 data->eeprom_len = AX88772_EEPROM_LEN;
David Brownell2e55cc72005-08-31 09:53:10 -0700941
942 usbnet_get_endpoints(dev,intf);
943
David Hollis933a27d2006-07-29 10:12:50 -0400944 if ((ret = asix_write_gpio(dev,
945 AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000946 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700947
Andres Salomond0ffff82007-01-11 18:39:16 -0500948 /* 0x10 is the phy id of the embedded 10/100 ethernet phy */
949 embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
David Hollis48b1be62006-03-28 20:15:42 -0500950 if ((ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
Al Viro51bf2972007-12-22 17:42:36 +0000951 embd_phy, 0, 0, NULL)) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -0700952 dbg("Select PHY #1 failed: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000953 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700954 }
955
Andres Salomond0ffff82007-01-11 18:39:16 -0500956 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000957 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700958
959 msleep(150);
David Hollis48b1be62006-03-28 20:15:42 -0500960 if ((ret = asix_sw_reset(dev, AX_SWRESET_CLEAR)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000961 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700962
963 msleep(150);
Andres Salomond0ffff82007-01-11 18:39:16 -0500964 if (embd_phy) {
965 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000966 goto out;
Andres Salomond0ffff82007-01-11 18:39:16 -0500967 }
968 else {
969 if ((ret = asix_sw_reset(dev, AX_SWRESET_PRTE)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000970 goto out;
Andres Salomond0ffff82007-01-11 18:39:16 -0500971 }
David Brownell2e55cc72005-08-31 09:53:10 -0700972
973 msleep(150);
David Hollis933a27d2006-07-29 10:12:50 -0400974 rx_ctl = asix_read_rx_ctl(dev);
975 dbg("RX_CTL is 0x%04x after software reset", rx_ctl);
976 if ((ret = asix_write_rx_ctl(dev, 0x0000)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +0000977 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700978
David Hollis933a27d2006-07-29 10:12:50 -0400979 rx_ctl = asix_read_rx_ctl(dev);
980 dbg("RX_CTL is 0x%04x setting to 0x0000", rx_ctl);
981
David Brownell2e55cc72005-08-31 09:53:10 -0700982 /* Get the MAC address */
David Hollis933a27d2006-07-29 10:12:50 -0400983 if ((ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
David Brownell2e55cc72005-08-31 09:53:10 -0700984 0, 0, ETH_ALEN, buf)) < 0) {
985 dbg("Failed to read MAC address: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +0000986 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -0700987 }
988 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
989
David Brownell2e55cc72005-08-31 09:53:10 -0700990 /* Initialize MII structure */
991 dev->mii.dev = dev->net;
David Hollis48b1be62006-03-28 20:15:42 -0500992 dev->mii.mdio_read = asix_mdio_read;
993 dev->mii.mdio_write = asix_mdio_write;
David Hollis933a27d2006-07-29 10:12:50 -0400994 dev->mii.phy_id_mask = 0x1f;
995 dev->mii.reg_num_mask = 0x1f;
David Hollis933a27d2006-07-29 10:12:50 -0400996 dev->mii.phy_id = asix_get_phy_addr(dev);
997
998 phyid = asix_get_phyid(dev);
999 dbg("PHYID=0x%08x", phyid);
David Brownell2e55cc72005-08-31 09:53:10 -07001000
David Hollis48b1be62006-03-28 20:15:42 -05001001 if ((ret = asix_sw_reset(dev, AX_SWRESET_PRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001002 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001003
David Brownell2e55cc72005-08-31 09:53:10 -07001004 msleep(150);
1005
David Hollis48b1be62006-03-28 20:15:42 -05001006 if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001007 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001008
David Hollis48b1be62006-03-28 20:15:42 -05001009 msleep(150);
1010
Stephen Hemminger17033382009-03-20 19:35:55 +00001011 dev->net->netdev_ops = &ax88772_netdev_ops;
David Brownell2e55cc72005-08-31 09:53:10 -07001012 dev->net->ethtool_ops = &ax88772_ethtool_ops;
1013
David Hollis933a27d2006-07-29 10:12:50 -04001014 asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1015 asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
David Brownell2e55cc72005-08-31 09:53:10 -07001016 ADVERTISE_ALL | ADVERTISE_CSMA);
1017 mii_nway_restart(&dev->mii);
1018
David Hollis933a27d2006-07-29 10:12:50 -04001019 if ((ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001020 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001021
David Hollis48b1be62006-03-28 20:15:42 -05001022 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
David Brownell2e55cc72005-08-31 09:53:10 -07001023 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
Al Viro51bf2972007-12-22 17:42:36 +00001024 AX88772_IPG2_DEFAULT, 0, NULL)) < 0) {
David Brownell2e55cc72005-08-31 09:53:10 -07001025 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +00001026 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001027 }
David Brownell2e55cc72005-08-31 09:53:10 -07001028
1029 /* Set RX_CTL to default values with 2k buffer, and enable cactus */
David Hollis933a27d2006-07-29 10:12:50 -04001030 if ((ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001031 goto out;
David Brownell2e55cc72005-08-31 09:53:10 -07001032
David Hollis933a27d2006-07-29 10:12:50 -04001033 rx_ctl = asix_read_rx_ctl(dev);
1034 dbg("RX_CTL is 0x%04x after all initializations", rx_ctl);
1035
1036 rx_ctl = asix_read_medium_status(dev);
1037 dbg("Medium Status is 0x%04x after all initializations", rx_ctl);
1038
David Brownell2e55cc72005-08-31 09:53:10 -07001039 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
1040 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
1041 /* hard_mtu is still the default - the device does not support
1042 jumbo eth frames */
1043 dev->rx_urb_size = 2048;
1044 }
David Brownell2e55cc72005-08-31 09:53:10 -07001045 return 0;
1046
Al Viro51bf2972007-12-22 17:42:36 +00001047out:
David Brownell2e55cc72005-08-31 09:53:10 -07001048 return ret;
1049}
1050
David Hollis933a27d2006-07-29 10:12:50 -04001051static struct ethtool_ops ax88178_ethtool_ops = {
1052 .get_drvinfo = asix_get_drvinfo,
1053 .get_link = asix_get_link,
David Hollis933a27d2006-07-29 10:12:50 -04001054 .get_msglevel = usbnet_get_msglevel,
1055 .set_msglevel = usbnet_set_msglevel,
1056 .get_wol = asix_get_wol,
1057 .set_wol = asix_set_wol,
1058 .get_eeprom_len = asix_get_eeprom_len,
1059 .get_eeprom = asix_get_eeprom,
Arnd Bergmannc41286f2006-10-09 00:08:01 +02001060 .get_settings = usbnet_get_settings,
1061 .set_settings = usbnet_set_settings,
1062 .nway_reset = usbnet_nway_reset,
David Hollis933a27d2006-07-29 10:12:50 -04001063};
1064
1065static int marvell_phy_init(struct usbnet *dev)
David Brownell2e55cc72005-08-31 09:53:10 -07001066{
David Hollis933a27d2006-07-29 10:12:50 -04001067 struct asix_data *data = (struct asix_data *)&dev->data;
1068 u16 reg;
David Brownell2e55cc72005-08-31 09:53:10 -07001069
Joe Perches60b86752010-02-17 10:30:23 +00001070 netdev_dbg(dev->net, "marvell_phy_init()\n");
David Brownell2e55cc72005-08-31 09:53:10 -07001071
David Hollis933a27d2006-07-29 10:12:50 -04001072 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
Joe Perches60b86752010-02-17 10:30:23 +00001073 netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
David Brownell2e55cc72005-08-31 09:53:10 -07001074
David Hollis933a27d2006-07-29 10:12:50 -04001075 asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
1076 MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
David Brownell2e55cc72005-08-31 09:53:10 -07001077
David Hollis933a27d2006-07-29 10:12:50 -04001078 if (data->ledmode) {
1079 reg = asix_mdio_read(dev->net, dev->mii.phy_id,
1080 MII_MARVELL_LED_CTRL);
Joe Perches60b86752010-02-17 10:30:23 +00001081 netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
David Brownell2e55cc72005-08-31 09:53:10 -07001082
David Hollis933a27d2006-07-29 10:12:50 -04001083 reg &= 0xf8ff;
1084 reg |= (1 + 0x0100);
1085 asix_mdio_write(dev->net, dev->mii.phy_id,
1086 MII_MARVELL_LED_CTRL, reg);
David Brownell2e55cc72005-08-31 09:53:10 -07001087
David Hollis933a27d2006-07-29 10:12:50 -04001088 reg = asix_mdio_read(dev->net, dev->mii.phy_id,
1089 MII_MARVELL_LED_CTRL);
Joe Perches60b86752010-02-17 10:30:23 +00001090 netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
David Hollis933a27d2006-07-29 10:12:50 -04001091 reg &= 0xfc0f;
David Brownell2e55cc72005-08-31 09:53:10 -07001092 }
1093
David Brownell2e55cc72005-08-31 09:53:10 -07001094 return 0;
1095}
1096
David Hollis933a27d2006-07-29 10:12:50 -04001097static int marvell_led_status(struct usbnet *dev, u16 speed)
1098{
1099 u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
1100
Joe Perches60b86752010-02-17 10:30:23 +00001101 netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
David Hollis933a27d2006-07-29 10:12:50 -04001102
1103 /* Clear out the center LED bits - 0x03F0 */
1104 reg &= 0xfc0f;
1105
1106 switch (speed) {
1107 case SPEED_1000:
1108 reg |= 0x03e0;
1109 break;
1110 case SPEED_100:
1111 reg |= 0x03b0;
1112 break;
1113 default:
1114 reg |= 0x02f0;
1115 }
1116
Joe Perches60b86752010-02-17 10:30:23 +00001117 netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
David Hollis933a27d2006-07-29 10:12:50 -04001118 asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
1119
1120 return 0;
1121}
1122
1123static int ax88178_link_reset(struct usbnet *dev)
1124{
1125 u16 mode;
1126 struct ethtool_cmd ecmd;
1127 struct asix_data *data = (struct asix_data *)&dev->data;
1128
Joe Perches60b86752010-02-17 10:30:23 +00001129 netdev_dbg(dev->net, "ax88178_link_reset()\n");
David Hollis933a27d2006-07-29 10:12:50 -04001130
1131 mii_check_media(&dev->mii, 1, 1);
1132 mii_ethtool_gset(&dev->mii, &ecmd);
1133 mode = AX88178_MEDIUM_DEFAULT;
1134
1135 if (ecmd.speed == SPEED_1000)
Pantelis Koukousoulasa7f75c02008-11-20 01:48:46 -08001136 mode |= AX_MEDIUM_GM;
David Hollis933a27d2006-07-29 10:12:50 -04001137 else if (ecmd.speed == SPEED_100)
1138 mode |= AX_MEDIUM_PS;
1139 else
1140 mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
1141
Pantelis Koukousoulasa7f75c02008-11-20 01:48:46 -08001142 mode |= AX_MEDIUM_ENCK;
1143
David Hollis933a27d2006-07-29 10:12:50 -04001144 if (ecmd.duplex == DUPLEX_FULL)
1145 mode |= AX_MEDIUM_FD;
1146 else
1147 mode &= ~AX_MEDIUM_FD;
1148
Joe Perches60b86752010-02-17 10:30:23 +00001149 netdev_dbg(dev->net, "ax88178_link_reset() speed: %d duplex: %d setting mode to 0x%04x\n",
1150 ecmd.speed, ecmd.duplex, mode);
David Hollis933a27d2006-07-29 10:12:50 -04001151
1152 asix_write_medium_mode(dev, mode);
1153
1154 if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
1155 marvell_led_status(dev, ecmd.speed);
1156
1157 return 0;
1158}
1159
1160static void ax88178_set_mfb(struct usbnet *dev)
1161{
1162 u16 mfb = AX_RX_CTL_MFB_16384;
1163 u16 rxctl;
1164 u16 medium;
1165 int old_rx_urb_size = dev->rx_urb_size;
1166
1167 if (dev->hard_mtu < 2048) {
1168 dev->rx_urb_size = 2048;
1169 mfb = AX_RX_CTL_MFB_2048;
1170 } else if (dev->hard_mtu < 4096) {
1171 dev->rx_urb_size = 4096;
1172 mfb = AX_RX_CTL_MFB_4096;
1173 } else if (dev->hard_mtu < 8192) {
1174 dev->rx_urb_size = 8192;
1175 mfb = AX_RX_CTL_MFB_8192;
1176 } else if (dev->hard_mtu < 16384) {
1177 dev->rx_urb_size = 16384;
1178 mfb = AX_RX_CTL_MFB_16384;
1179 }
1180
1181 rxctl = asix_read_rx_ctl(dev);
1182 asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
1183
1184 medium = asix_read_medium_status(dev);
1185 if (dev->net->mtu > 1500)
1186 medium |= AX_MEDIUM_JFE;
1187 else
1188 medium &= ~AX_MEDIUM_JFE;
1189 asix_write_medium_mode(dev, medium);
1190
1191 if (dev->rx_urb_size > old_rx_urb_size)
1192 usbnet_unlink_rx_urbs(dev);
1193}
1194
1195static int ax88178_change_mtu(struct net_device *net, int new_mtu)
1196{
1197 struct usbnet *dev = netdev_priv(net);
1198 int ll_mtu = new_mtu + net->hard_header_len + 4;
1199
Joe Perches60b86752010-02-17 10:30:23 +00001200 netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
David Hollis933a27d2006-07-29 10:12:50 -04001201
1202 if (new_mtu <= 0 || ll_mtu > 16384)
1203 return -EINVAL;
1204
1205 if ((ll_mtu % dev->maxpacket) == 0)
1206 return -EDOM;
1207
1208 net->mtu = new_mtu;
1209 dev->hard_mtu = net->mtu + net->hard_header_len;
1210 ax88178_set_mfb(dev);
1211
1212 return 0;
1213}
1214
Stephen Hemminger17033382009-03-20 19:35:55 +00001215static const struct net_device_ops ax88178_netdev_ops = {
1216 .ndo_open = usbnet_open,
1217 .ndo_stop = usbnet_stop,
1218 .ndo_start_xmit = usbnet_start_xmit,
1219 .ndo_tx_timeout = usbnet_tx_timeout,
1220 .ndo_set_mac_address = eth_mac_addr,
1221 .ndo_validate_addr = eth_validate_addr,
1222 .ndo_set_multicast_list = asix_set_multicast,
1223 .ndo_do_ioctl = asix_ioctl,
1224 .ndo_change_mtu = ax88178_change_mtu,
1225};
1226
David Hollis933a27d2006-07-29 10:12:50 -04001227static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
1228{
1229 struct asix_data *data = (struct asix_data *)&dev->data;
1230 int ret;
Al Viro51bf2972007-12-22 17:42:36 +00001231 u8 buf[ETH_ALEN];
1232 __le16 eeprom;
1233 u8 status;
David Hollis933a27d2006-07-29 10:12:50 -04001234 int gpio0 = 0;
1235 u32 phyid;
1236
1237 usbnet_get_endpoints(dev,intf);
1238
Al Viro51bf2972007-12-22 17:42:36 +00001239 asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
1240 dbg("GPIO Status: 0x%04x", status);
David Hollis933a27d2006-07-29 10:12:50 -04001241
1242 asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
1243 asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
1244 asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
1245
1246 dbg("EEPROM index 0x17 is 0x%04x", eeprom);
1247
Al Viro51bf2972007-12-22 17:42:36 +00001248 if (eeprom == cpu_to_le16(0xffff)) {
David Hollis933a27d2006-07-29 10:12:50 -04001249 data->phymode = PHY_MODE_MARVELL;
1250 data->ledmode = 0;
1251 gpio0 = 1;
1252 } else {
Al Viro51bf2972007-12-22 17:42:36 +00001253 data->phymode = le16_to_cpu(eeprom) & 7;
1254 data->ledmode = le16_to_cpu(eeprom) >> 8;
1255 gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
David Hollis933a27d2006-07-29 10:12:50 -04001256 }
1257 dbg("GPIO0: %d, PhyMode: %d", gpio0, data->phymode);
1258
1259 asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
Al Viro51bf2972007-12-22 17:42:36 +00001260 if ((le16_to_cpu(eeprom) >> 8) != 1) {
David Hollis933a27d2006-07-29 10:12:50 -04001261 asix_write_gpio(dev, 0x003c, 30);
1262 asix_write_gpio(dev, 0x001c, 300);
1263 asix_write_gpio(dev, 0x003c, 30);
1264 } else {
1265 dbg("gpio phymode == 1 path");
1266 asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
1267 asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
1268 }
1269
1270 asix_sw_reset(dev, 0);
1271 msleep(150);
1272
1273 asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
1274 msleep(150);
1275
1276 asix_write_rx_ctl(dev, 0);
1277
1278 /* Get the MAC address */
David Hollis933a27d2006-07-29 10:12:50 -04001279 if ((ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
1280 0, 0, ETH_ALEN, buf)) < 0) {
1281 dbg("Failed to read MAC address: %d", ret);
Al Viro51bf2972007-12-22 17:42:36 +00001282 goto out;
David Hollis933a27d2006-07-29 10:12:50 -04001283 }
1284 memcpy(dev->net->dev_addr, buf, ETH_ALEN);
1285
1286 /* Initialize MII structure */
1287 dev->mii.dev = dev->net;
1288 dev->mii.mdio_read = asix_mdio_read;
1289 dev->mii.mdio_write = asix_mdio_write;
1290 dev->mii.phy_id_mask = 0x1f;
1291 dev->mii.reg_num_mask = 0xff;
1292 dev->mii.supports_gmii = 1;
David Hollis933a27d2006-07-29 10:12:50 -04001293 dev->mii.phy_id = asix_get_phy_addr(dev);
Stephen Hemminger17033382009-03-20 19:35:55 +00001294
1295 dev->net->netdev_ops = &ax88178_netdev_ops;
David Hollis933a27d2006-07-29 10:12:50 -04001296 dev->net->ethtool_ops = &ax88178_ethtool_ops;
David Hollis933a27d2006-07-29 10:12:50 -04001297
1298 phyid = asix_get_phyid(dev);
1299 dbg("PHYID=0x%08x", phyid);
1300
1301 if (data->phymode == PHY_MODE_MARVELL) {
1302 marvell_phy_init(dev);
1303 msleep(60);
1304 }
1305
1306 asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
1307 BMCR_RESET | BMCR_ANENABLE);
1308 asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1309 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
1310 asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
1311 ADVERTISE_1000FULL);
1312
1313 mii_nway_restart(&dev->mii);
1314
1315 if ((ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001316 goto out;
David Hollis933a27d2006-07-29 10:12:50 -04001317
1318 if ((ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL)) < 0)
Al Viro51bf2972007-12-22 17:42:36 +00001319 goto out;
David Hollis933a27d2006-07-29 10:12:50 -04001320
1321 /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
1322 if (dev->driver_info->flags & FLAG_FRAMING_AX) {
1323 /* hard_mtu is still the default - the device does not support
1324 jumbo eth frames */
1325 dev->rx_urb_size = 2048;
1326 }
David Hollis933a27d2006-07-29 10:12:50 -04001327 return 0;
1328
Al Viro51bf2972007-12-22 17:42:36 +00001329out:
David Hollis933a27d2006-07-29 10:12:50 -04001330 return ret;
1331}
1332
David Brownell2e55cc72005-08-31 09:53:10 -07001333static const struct driver_info ax8817x_info = {
1334 .description = "ASIX AX8817x USB 2.0 Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001335 .bind = ax88172_bind,
1336 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001337 .link_reset = ax88172_link_reset,
1338 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001339 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001340 .data = 0x00130103,
1341};
1342
1343static const struct driver_info dlink_dub_e100_info = {
1344 .description = "DLink DUB-E100 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001345 .bind = ax88172_bind,
1346 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001347 .link_reset = ax88172_link_reset,
1348 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001349 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001350 .data = 0x009f9d9f,
1351};
1352
1353static const struct driver_info netgear_fa120_info = {
1354 .description = "Netgear FA-120 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001355 .bind = ax88172_bind,
1356 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001357 .link_reset = ax88172_link_reset,
1358 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001359 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001360 .data = 0x00130103,
1361};
1362
1363static const struct driver_info hawking_uf200_info = {
1364 .description = "Hawking UF200 USB Ethernet",
David Hollis48b1be62006-03-28 20:15:42 -05001365 .bind = ax88172_bind,
1366 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001367 .link_reset = ax88172_link_reset,
1368 .reset = ax88172_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001369 .flags = FLAG_ETHER | FLAG_LINK_INTR,
David Brownell2e55cc72005-08-31 09:53:10 -07001370 .data = 0x001f1d1f,
1371};
1372
1373static const struct driver_info ax88772_info = {
1374 .description = "ASIX AX88772 USB 2.0 Ethernet",
1375 .bind = ax88772_bind,
David Hollis48b1be62006-03-28 20:15:42 -05001376 .status = asix_status,
David Brownell2e55cc72005-08-31 09:53:10 -07001377 .link_reset = ax88772_link_reset,
1378 .reset = ax88772_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001379 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
David Hollis933a27d2006-07-29 10:12:50 -04001380 .rx_fixup = asix_rx_fixup,
1381 .tx_fixup = asix_tx_fixup,
1382};
1383
1384static const struct driver_info ax88178_info = {
1385 .description = "ASIX AX88178 USB 2.0 Ethernet",
1386 .bind = ax88178_bind,
1387 .status = asix_status,
1388 .link_reset = ax88178_link_reset,
1389 .reset = ax88178_link_reset,
Ben Hutchings37e82732009-11-04 15:29:52 +00001390 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
David Hollis933a27d2006-07-29 10:12:50 -04001391 .rx_fixup = asix_rx_fixup,
1392 .tx_fixup = asix_tx_fixup,
David Brownell2e55cc72005-08-31 09:53:10 -07001393};
1394
1395static const struct usb_device_id products [] = {
1396{
1397 // Linksys USB200M
1398 USB_DEVICE (0x077b, 0x2226),
1399 .driver_info = (unsigned long) &ax8817x_info,
1400}, {
1401 // Netgear FA120
1402 USB_DEVICE (0x0846, 0x1040),
1403 .driver_info = (unsigned long) &netgear_fa120_info,
1404}, {
1405 // DLink DUB-E100
1406 USB_DEVICE (0x2001, 0x1a00),
1407 .driver_info = (unsigned long) &dlink_dub_e100_info,
1408}, {
1409 // Intellinet, ST Lab USB Ethernet
1410 USB_DEVICE (0x0b95, 0x1720),
1411 .driver_info = (unsigned long) &ax8817x_info,
1412}, {
1413 // Hawking UF200, TrendNet TU2-ET100
1414 USB_DEVICE (0x07b8, 0x420a),
1415 .driver_info = (unsigned long) &hawking_uf200_info,
1416}, {
David Hollis39c4b382007-02-20 08:02:24 -05001417 // Billionton Systems, USB2AR
1418 USB_DEVICE (0x08dd, 0x90ff),
1419 .driver_info = (unsigned long) &ax8817x_info,
David Brownell2e55cc72005-08-31 09:53:10 -07001420}, {
1421 // ATEN UC210T
1422 USB_DEVICE (0x0557, 0x2009),
1423 .driver_info = (unsigned long) &ax8817x_info,
1424}, {
1425 // Buffalo LUA-U2-KTX
1426 USB_DEVICE (0x0411, 0x003d),
1427 .driver_info = (unsigned long) &ax8817x_info,
1428}, {
Mattia Dongiliac7b77f2008-05-06 20:42:35 -07001429 // Buffalo LUA-U2-GT 10/100/1000
1430 USB_DEVICE (0x0411, 0x006e),
1431 .driver_info = (unsigned long) &ax88178_info,
1432}, {
David Brownell2e55cc72005-08-31 09:53:10 -07001433 // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
1434 USB_DEVICE (0x6189, 0x182d),
1435 .driver_info = (unsigned long) &ax8817x_info,
1436}, {
1437 // corega FEther USB2-TX
1438 USB_DEVICE (0x07aa, 0x0017),
1439 .driver_info = (unsigned long) &ax8817x_info,
1440}, {
1441 // Surecom EP-1427X-2
1442 USB_DEVICE (0x1189, 0x0893),
1443 .driver_info = (unsigned long) &ax8817x_info,
1444}, {
1445 // goodway corp usb gwusb2e
1446 USB_DEVICE (0x1631, 0x6200),
1447 .driver_info = (unsigned long) &ax8817x_info,
1448}, {
David Hollis39c4b382007-02-20 08:02:24 -05001449 // JVC MP-PRX1 Port Replicator
1450 USB_DEVICE (0x04f1, 0x3008),
1451 .driver_info = (unsigned long) &ax8817x_info,
1452}, {
David Brownell2e55cc72005-08-31 09:53:10 -07001453 // ASIX AX88772 10/100
David Hollis39c4b382007-02-20 08:02:24 -05001454 USB_DEVICE (0x0b95, 0x7720),
1455 .driver_info = (unsigned long) &ax88772_info,
David Hollis5e0f76c2005-12-19 13:58:38 -05001456}, {
Eduard Warkentin73274132006-05-18 01:13:17 -07001457 // ASIX AX88178 10/100/1000
1458 USB_DEVICE (0x0b95, 0x1780),
David Hollis933a27d2006-07-29 10:12:50 -04001459 .driver_info = (unsigned long) &ax88178_info,
Eduard Warkentin73274132006-05-18 01:13:17 -07001460}, {
David Hollis5e0f76c2005-12-19 13:58:38 -05001461 // Linksys USB200M Rev 2
1462 USB_DEVICE (0x13b1, 0x0018),
1463 .driver_info = (unsigned long) &ax88772_info,
David Hollis5732ce82006-01-05 14:39:49 -05001464}, {
1465 // 0Q0 cable ethernet
1466 USB_DEVICE (0x1557, 0x7720),
1467 .driver_info = (unsigned long) &ax88772_info,
David Hollis933a27d2006-07-29 10:12:50 -04001468}, {
1469 // DLink DUB-E100 H/W Ver B1
1470 USB_DEVICE (0x07d1, 0x3c05),
1471 .driver_info = (unsigned long) &ax88772_info,
1472}, {
David Hollisb923e7f2006-09-21 08:09:29 -04001473 // DLink DUB-E100 H/W Ver B1 Alternate
1474 USB_DEVICE (0x2001, 0x3c05),
1475 .driver_info = (unsigned long) &ax88772_info,
1476}, {
David Hollis933a27d2006-07-29 10:12:50 -04001477 // Linksys USB1000
1478 USB_DEVICE (0x1737, 0x0039),
1479 .driver_info = (unsigned long) &ax88178_info,
YOSHIFUJI Hideaki / 吉藤英明b29cf312007-01-26 22:57:38 +09001480}, {
1481 // IO-DATA ETG-US2
1482 USB_DEVICE (0x04bb, 0x0930),
1483 .driver_info = (unsigned long) &ax88178_info,
David Hollis2ed22bc2007-05-23 07:33:17 -04001484}, {
1485 // Belkin F5D5055
1486 USB_DEVICE(0x050d, 0x5055),
1487 .driver_info = (unsigned long) &ax88178_info,
Aurelien Nephtali3d60efb52008-05-14 17:04:13 -07001488}, {
1489 // Apple USB Ethernet Adapter
1490 USB_DEVICE(0x05ac, 0x1402),
1491 .driver_info = (unsigned long) &ax88772_info,
Jason Cooperccf95402008-11-11 13:02:53 -05001492}, {
1493 // Cables-to-Go USB Ethernet Adapter
1494 USB_DEVICE(0x0b95, 0x772a),
1495 .driver_info = (unsigned long) &ax88772_info,
Greg Kroah-Hartmanfef7cc02009-02-24 23:52:24 -08001496}, {
1497 // ABOCOM for pci
1498 USB_DEVICE(0x14ea, 0xab11),
1499 .driver_info = (unsigned long) &ax88178_info,
1500}, {
1501 // ASIX 88772a
1502 USB_DEVICE(0x0db0, 0xa877),
1503 .driver_info = (unsigned long) &ax88772_info,
David Brownell2e55cc72005-08-31 09:53:10 -07001504},
1505 { }, // END
1506};
1507MODULE_DEVICE_TABLE(usb, products);
1508
1509static struct usb_driver asix_driver = {
David Brownell2e55cc72005-08-31 09:53:10 -07001510 .name = "asix",
1511 .id_table = products,
1512 .probe = usbnet_probe,
1513 .suspend = usbnet_suspend,
1514 .resume = usbnet_resume,
1515 .disconnect = usbnet_disconnect,
Oliver Neukuma11a6542007-08-03 13:52:19 +02001516 .supports_autosuspend = 1,
David Brownell2e55cc72005-08-31 09:53:10 -07001517};
1518
1519static int __init asix_init(void)
1520{
1521 return usb_register(&asix_driver);
1522}
1523module_init(asix_init);
1524
1525static void __exit asix_exit(void)
1526{
1527 usb_deregister(&asix_driver);
1528}
1529module_exit(asix_exit);
1530
1531MODULE_AUTHOR("David Hollis");
1532MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
1533MODULE_LICENSE("GPL");
1534