Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / net / usb / asix.c
1 /*
2  * ASIX AX8817X based USB 2.0 Ethernet Devices
3  * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
4  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5  * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
6  * 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
26 #include <linux/module.h>
27 #include <linux/kmod.h>
28 #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>
36 #include <linux/usb/usbnet.h>
37 #include <linux/slab.h>
38 #include <linux/if_vlan.h>
39
40 #define DRIVER_VERSION "08-Nov-2011"
41 #define DRIVER_NAME "asix"
42
43 /* ASIX AX8817X based USB 2.0 Ethernet Devices */
44
45 #define AX_CMD_SET_SW_MII               0x06
46 #define AX_CMD_READ_MII_REG             0x07
47 #define AX_CMD_WRITE_MII_REG            0x08
48 #define AX_CMD_SET_HW_MII               0x0a
49 #define AX_CMD_READ_EEPROM              0x0b
50 #define AX_CMD_WRITE_EEPROM             0x0c
51 #define AX_CMD_WRITE_ENABLE             0x0d
52 #define AX_CMD_WRITE_DISABLE            0x0e
53 #define AX_CMD_READ_RX_CTL              0x0f
54 #define AX_CMD_WRITE_RX_CTL             0x10
55 #define AX_CMD_READ_IPG012              0x11
56 #define AX_CMD_WRITE_IPG0               0x12
57 #define AX_CMD_WRITE_IPG1               0x13
58 #define AX_CMD_READ_NODE_ID             0x13
59 #define AX_CMD_WRITE_NODE_ID            0x14
60 #define AX_CMD_WRITE_IPG2               0x14
61 #define AX_CMD_WRITE_MULTI_FILTER       0x16
62 #define AX88172_CMD_READ_NODE_ID        0x17
63 #define AX_CMD_READ_PHY_ID              0x19
64 #define AX_CMD_READ_MEDIUM_STATUS       0x1a
65 #define AX_CMD_WRITE_MEDIUM_MODE        0x1b
66 #define AX_CMD_READ_MONITOR_MODE        0x1c
67 #define AX_CMD_WRITE_MONITOR_MODE       0x1d
68 #define AX_CMD_READ_GPIOS               0x1e
69 #define AX_CMD_WRITE_GPIOS              0x1f
70 #define AX_CMD_SW_RESET                 0x20
71 #define AX_CMD_SW_PHY_STATUS            0x21
72 #define AX_CMD_SW_PHY_SELECT            0x22
73
74 #define AX_MONITOR_MODE                 0x01
75 #define AX_MONITOR_LINK                 0x02
76 #define AX_MONITOR_MAGIC                0x04
77 #define AX_MONITOR_HSFS                 0x10
78
79 /* AX88172 Medium Status Register values */
80 #define AX88172_MEDIUM_FD               0x02
81 #define AX88172_MEDIUM_TX               0x04
82 #define AX88172_MEDIUM_FC               0x10
83 #define AX88172_MEDIUM_DEFAULT \
84                 ( AX88172_MEDIUM_FD | AX88172_MEDIUM_TX | AX88172_MEDIUM_FC )
85
86 #define AX_MCAST_FILTER_SIZE            8
87 #define AX_MAX_MCAST                    64
88
89 #define AX_SWRESET_CLEAR                0x00
90 #define AX_SWRESET_RR                   0x01
91 #define AX_SWRESET_RT                   0x02
92 #define AX_SWRESET_PRTE                 0x04
93 #define AX_SWRESET_PRL                  0x08
94 #define AX_SWRESET_BZ                   0x10
95 #define AX_SWRESET_IPRL                 0x20
96 #define AX_SWRESET_IPPD                 0x40
97
98 #define AX88772_IPG0_DEFAULT            0x15
99 #define AX88772_IPG1_DEFAULT            0x0c
100 #define AX88772_IPG2_DEFAULT            0x12
101
102 /* AX88772 & AX88178 Medium Mode Register */
103 #define AX_MEDIUM_PF            0x0080
104 #define AX_MEDIUM_JFE           0x0040
105 #define AX_MEDIUM_TFC           0x0020
106 #define AX_MEDIUM_RFC           0x0010
107 #define AX_MEDIUM_ENCK          0x0008
108 #define AX_MEDIUM_AC            0x0004
109 #define AX_MEDIUM_FD            0x0002
110 #define AX_MEDIUM_GM            0x0001
111 #define AX_MEDIUM_SM            0x1000
112 #define AX_MEDIUM_SBP           0x0800
113 #define AX_MEDIUM_PS            0x0200
114 #define AX_MEDIUM_RE            0x0100
115
116 #define AX88178_MEDIUM_DEFAULT  \
117         (AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
118          AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
119          AX_MEDIUM_RE)
120
121 #define AX88772_MEDIUM_DEFAULT  \
122         (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
123          AX_MEDIUM_TFC | AX_MEDIUM_PS | \
124          AX_MEDIUM_AC | AX_MEDIUM_RE)
125
126 /* AX88772 & AX88178 RX_CTL values */
127 #define AX_RX_CTL_SO            0x0080
128 #define AX_RX_CTL_AP            0x0020
129 #define AX_RX_CTL_AM            0x0010
130 #define AX_RX_CTL_AB            0x0008
131 #define AX_RX_CTL_SEP           0x0004
132 #define AX_RX_CTL_AMALL         0x0002
133 #define AX_RX_CTL_PRO           0x0001
134 #define AX_RX_CTL_MFB_2048      0x0000
135 #define AX_RX_CTL_MFB_4096      0x0100
136 #define AX_RX_CTL_MFB_8192      0x0200
137 #define AX_RX_CTL_MFB_16384     0x0300
138
139 #define AX_DEFAULT_RX_CTL       (AX_RX_CTL_SO | AX_RX_CTL_AB)
140
141 /* GPIO 0 .. 2 toggles */
142 #define AX_GPIO_GPO0EN          0x01    /* GPIO0 Output enable */
143 #define AX_GPIO_GPO_0           0x02    /* GPIO0 Output value */
144 #define AX_GPIO_GPO1EN          0x04    /* GPIO1 Output enable */
145 #define AX_GPIO_GPO_1           0x08    /* GPIO1 Output value */
146 #define AX_GPIO_GPO2EN          0x10    /* GPIO2 Output enable */
147 #define AX_GPIO_GPO_2           0x20    /* GPIO2 Output value */
148 #define AX_GPIO_RESERVED        0x40    /* Reserved */
149 #define AX_GPIO_RSE             0x80    /* Reload serial EEPROM */
150
151 #define AX_EEPROM_MAGIC         0xdeadbeef
152 #define AX88172_EEPROM_LEN      0x40
153 #define AX88772_EEPROM_LEN      0xff
154
155 #define PHY_MODE_MARVELL        0x0000
156 #define MII_MARVELL_LED_CTRL    0x0018
157 #define MII_MARVELL_STATUS      0x001b
158 #define MII_MARVELL_CTRL        0x0014
159
160 #define MARVELL_LED_MANUAL      0x0019
161
162 #define MARVELL_STATUS_HWCFG    0x0004
163
164 #define MARVELL_CTRL_TXDELAY    0x0002
165 #define MARVELL_CTRL_RXDELAY    0x0080
166
167 #define PHY_MODE_RTL8211CL      0x000C
168
169 /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
170 struct asix_data {
171         u8 multi_filter[AX_MCAST_FILTER_SIZE];
172         u8 mac_addr[ETH_ALEN];
173         u8 phymode;
174         u8 ledmode;
175         u8 eeprom_len;
176 };
177
178 struct ax88172_int_data {
179         __le16 res1;
180         u8 link;
181         __le16 res2;
182         u8 status;
183         __le16 res3;
184 } __packed;
185
186 static int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
187                             u16 size, void *data)
188 {
189         void *buf;
190         int err = -ENOMEM;
191
192         netdev_dbg(dev->net, "asix_read_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
193                    cmd, value, index, size);
194
195         buf = kmalloc(size, GFP_KERNEL);
196         if (!buf)
197                 goto out;
198
199         err = usb_control_msg(
200                 dev->udev,
201                 usb_rcvctrlpipe(dev->udev, 0),
202                 cmd,
203                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
204                 value,
205                 index,
206                 buf,
207                 size,
208                 USB_CTRL_GET_TIMEOUT);
209         if (err == size)
210                 memcpy(data, buf, size);
211         else if (err >= 0)
212                 err = -EINVAL;
213         kfree(buf);
214
215 out:
216         return err;
217 }
218
219 static int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
220                              u16 size, void *data)
221 {
222         void *buf = NULL;
223         int err = -ENOMEM;
224
225         netdev_dbg(dev->net, "asix_write_cmd() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
226                    cmd, value, index, size);
227
228         if (data) {
229                 buf = kmemdup(data, size, GFP_KERNEL);
230                 if (!buf)
231                         goto out;
232         }
233
234         err = usb_control_msg(
235                 dev->udev,
236                 usb_sndctrlpipe(dev->udev, 0),
237                 cmd,
238                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
239                 value,
240                 index,
241                 buf,
242                 size,
243                 USB_CTRL_SET_TIMEOUT);
244         kfree(buf);
245
246 out:
247         return err;
248 }
249
250 static void asix_async_cmd_callback(struct urb *urb)
251 {
252         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
253         int status = urb->status;
254
255         if (status < 0)
256                 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
257                         status);
258
259         kfree(req);
260         usb_free_urb(urb);
261 }
262
263 static void
264 asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
265                                     u16 size, void *data)
266 {
267         struct usb_ctrlrequest *req;
268         int status;
269         struct urb *urb;
270
271         netdev_dbg(dev->net, "asix_write_cmd_async() cmd=0x%02x value=0x%04x index=0x%04x size=%d\n",
272                    cmd, value, index, size);
273
274         urb = usb_alloc_urb(0, GFP_ATOMIC);
275         if (!urb) {
276                 netdev_err(dev->net, "Error allocating URB in write_cmd_async!\n");
277                 return;
278         }
279
280         req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
281         if (!req) {
282                 netdev_err(dev->net, "Failed to allocate memory for control request\n");
283                 usb_free_urb(urb);
284                 return;
285         }
286
287         req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
288         req->bRequest = cmd;
289         req->wValue = cpu_to_le16(value);
290         req->wIndex = cpu_to_le16(index);
291         req->wLength = cpu_to_le16(size);
292
293         usb_fill_control_urb(urb, dev->udev,
294                              usb_sndctrlpipe(dev->udev, 0),
295                              (void *)req, data, size,
296                              asix_async_cmd_callback, req);
297
298         status = usb_submit_urb(urb, GFP_ATOMIC);
299         if (status < 0) {
300                 netdev_err(dev->net, "Error submitting the control message: status=%d\n",
301                            status);
302                 kfree(req);
303                 usb_free_urb(urb);
304         }
305 }
306
307 static int asix_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
308 {
309         u8  *head;
310         u32  header;
311         char *packet;
312         struct sk_buff *ax_skb;
313         u16 size;
314
315         head = (u8 *) skb->data;
316         memcpy(&header, head, sizeof(header));
317         le32_to_cpus(&header);
318         packet = head + sizeof(header);
319
320         skb_pull(skb, 4);
321
322         while (skb->len > 0) {
323                 if ((header & 0x07ff) != ((~header >> 16) & 0x07ff))
324                         netdev_err(dev->net, "asix_rx_fixup() Bad Header Length\n");
325
326                 /* get the packet length */
327                 size = (u16) (header & 0x000007ff);
328
329                 if ((skb->len) - ((size + 1) & 0xfffe) == 0) {
330                         u8 alignment = (unsigned long)skb->data & 0x3;
331                         if (alignment != 0x2) {
332                                 /*
333                                  * not 16bit aligned so use the room provided by
334                                  * the 32 bit header to align the data
335                                  *
336                                  * note we want 16bit alignment as MAC header is
337                                  * 14bytes thus ip header will be aligned on
338                                  * 32bit boundary so accessing ipheader elements
339                                  * using a cast to struct ip header wont cause
340                                  * an unaligned accesses.
341                                  */
342                                 u8 realignment = (alignment + 2) & 0x3;
343                                 memmove(skb->data - realignment,
344                                         skb->data,
345                                         size);
346                                 skb->data -= realignment;
347                                 skb_set_tail_pointer(skb, size);
348                         }
349                         return 2;
350                 }
351
352                 if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
353                         netdev_err(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
354                                    size);
355                         return 0;
356                 }
357                 ax_skb = skb_clone(skb, GFP_ATOMIC);
358                 if (ax_skb) {
359                         u8 alignment = (unsigned long)packet & 0x3;
360                         ax_skb->len = size;
361
362                         if (alignment != 0x2) {
363                                 /*
364                                  * not 16bit aligned use the room provided by
365                                  * the 32 bit header to align the data
366                                  */
367                                 u8 realignment = (alignment + 2) & 0x3;
368                                 memmove(packet - realignment, packet, size);
369                                 packet -= realignment;
370                         }
371                         ax_skb->data = packet;
372                         skb_set_tail_pointer(ax_skb, size);
373                         usbnet_skb_return(dev, ax_skb);
374                 } else {
375                         return 0;
376                 }
377
378                 skb_pull(skb, (size + 1) & 0xfffe);
379
380                 if (skb->len < sizeof(header))
381                         break;
382
383                 head = (u8 *) skb->data;
384                 memcpy(&header, head, sizeof(header));
385                 le32_to_cpus(&header);
386                 packet = head + sizeof(header);
387                 skb_pull(skb, 4);
388         }
389
390         if (skb->len < 0) {
391                 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d\n",
392                            skb->len);
393                 return 0;
394         }
395         return 1;
396 }
397
398 static struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
399                                         gfp_t flags)
400 {
401         int padlen;
402         int headroom = skb_headroom(skb);
403         int tailroom = skb_tailroom(skb);
404         u32 packet_len;
405         u32 padbytes = 0xffff0000;
406
407         padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
408
409         if ((!skb_cloned(skb)) &&
410             ((headroom + tailroom) >= (4 + padlen))) {
411                 if ((headroom < 4) || (tailroom < padlen)) {
412                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
413                         skb_set_tail_pointer(skb, skb->len);
414                 }
415         } else {
416                 struct sk_buff *skb2;
417                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
418                 dev_kfree_skb_any(skb);
419                 skb = skb2;
420                 if (!skb)
421                         return NULL;
422         }
423
424         skb_push(skb, 4);
425         packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
426         cpu_to_le32s(&packet_len);
427         skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
428
429         if (padlen) {
430                 cpu_to_le32s(&padbytes);
431                 memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
432                 skb_put(skb, sizeof(padbytes));
433         }
434         return skb;
435 }
436
437 static void asix_status(struct usbnet *dev, struct urb *urb)
438 {
439         struct ax88172_int_data *event;
440         int link;
441
442         if (urb->actual_length < 8)
443                 return;
444
445         event = urb->transfer_buffer;
446         link = event->link & 0x01;
447         if (netif_carrier_ok(dev->net) != link) {
448                 if (link) {
449                         netif_carrier_on(dev->net);
450                         usbnet_defer_kevent (dev, EVENT_LINK_RESET );
451                 } else
452                         netif_carrier_off(dev->net);
453                 netdev_dbg(dev->net, "Link Status is: %d\n", link);
454         }
455 }
456
457 static inline int asix_set_sw_mii(struct usbnet *dev)
458 {
459         int ret;
460         ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
461         if (ret < 0)
462                 netdev_err(dev->net, "Failed to enable software MII access\n");
463         return ret;
464 }
465
466 static inline int asix_set_hw_mii(struct usbnet *dev)
467 {
468         int ret;
469         ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
470         if (ret < 0)
471                 netdev_err(dev->net, "Failed to enable hardware MII access\n");
472         return ret;
473 }
474
475 static inline int asix_get_phy_addr(struct usbnet *dev)
476 {
477         u8 buf[2];
478         int ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf);
479
480         netdev_dbg(dev->net, "asix_get_phy_addr()\n");
481
482         if (ret < 0) {
483                 netdev_err(dev->net, "Error reading PHYID register: %02x\n", ret);
484                 goto out;
485         }
486         netdev_dbg(dev->net, "asix_get_phy_addr() returning 0x%04x\n",
487                    *((__le16 *)buf));
488         ret = buf[1];
489
490 out:
491         return ret;
492 }
493
494 static int asix_sw_reset(struct usbnet *dev, u8 flags)
495 {
496         int ret;
497
498         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
499         if (ret < 0)
500                 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
501
502         return ret;
503 }
504
505 static u16 asix_read_rx_ctl(struct usbnet *dev)
506 {
507         __le16 v;
508         int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v);
509
510         if (ret < 0) {
511                 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
512                 goto out;
513         }
514         ret = le16_to_cpu(v);
515 out:
516         return ret;
517 }
518
519 static int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
520 {
521         int ret;
522
523         netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
524         ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
525         if (ret < 0)
526                 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
527                            mode, ret);
528
529         return ret;
530 }
531
532 static u16 asix_read_medium_status(struct usbnet *dev)
533 {
534         __le16 v;
535         int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 0, 0, 2, &v);
536
537         if (ret < 0) {
538                 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
539                            ret);
540                 return ret;     /* TODO: callers not checking for error ret */
541         }
542
543         return le16_to_cpu(v);
544
545 }
546
547 static int asix_write_medium_mode(struct usbnet *dev, u16 mode)
548 {
549         int ret;
550
551         netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
552         ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
553         if (ret < 0)
554                 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
555                            mode, ret);
556
557         return ret;
558 }
559
560 static int asix_write_gpio(struct usbnet *dev, u16 value, int sleep)
561 {
562         int ret;
563
564         netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
565         ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL);
566         if (ret < 0)
567                 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
568                            value, ret);
569
570         if (sleep)
571                 msleep(sleep);
572
573         return ret;
574 }
575
576 /*
577  * AX88772 & AX88178 have a 16-bit RX_CTL value
578  */
579 static void asix_set_multicast(struct net_device *net)
580 {
581         struct usbnet *dev = netdev_priv(net);
582         struct asix_data *data = (struct asix_data *)&dev->data;
583         u16 rx_ctl = AX_DEFAULT_RX_CTL;
584
585         if (net->flags & IFF_PROMISC) {
586                 rx_ctl |= AX_RX_CTL_PRO;
587         } else if (net->flags & IFF_ALLMULTI ||
588                    netdev_mc_count(net) > AX_MAX_MCAST) {
589                 rx_ctl |= AX_RX_CTL_AMALL;
590         } else if (netdev_mc_empty(net)) {
591                 /* just broadcast and directed */
592         } else {
593                 /* We use the 20 byte dev->data
594                  * for our 8 byte filter buffer
595                  * to avoid allocating memory that
596                  * is tricky to free later */
597                 struct netdev_hw_addr *ha;
598                 u32 crc_bits;
599
600                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
601
602                 /* Build the multicast hash filter. */
603                 netdev_for_each_mc_addr(ha, net) {
604                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
605                         data->multi_filter[crc_bits >> 3] |=
606                             1 << (crc_bits & 7);
607                 }
608
609                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
610                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
611
612                 rx_ctl |= AX_RX_CTL_AM;
613         }
614
615         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
616 }
617
618 static int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
619 {
620         struct usbnet *dev = netdev_priv(netdev);
621         __le16 res;
622
623         mutex_lock(&dev->phy_mutex);
624         asix_set_sw_mii(dev);
625         asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
626                                 (__u16)loc, 2, &res);
627         asix_set_hw_mii(dev);
628         mutex_unlock(&dev->phy_mutex);
629
630         netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
631                    phy_id, loc, le16_to_cpu(res));
632
633         return le16_to_cpu(res);
634 }
635
636 static void
637 asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
638 {
639         struct usbnet *dev = netdev_priv(netdev);
640         __le16 res = cpu_to_le16(val);
641
642         netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
643                    phy_id, loc, val);
644         mutex_lock(&dev->phy_mutex);
645         asix_set_sw_mii(dev);
646         asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, &res);
647         asix_set_hw_mii(dev);
648         mutex_unlock(&dev->phy_mutex);
649 }
650
651 /* Get the PHY Identifier from the PHYSID1 & PHYSID2 MII registers */
652 static u32 asix_get_phyid(struct usbnet *dev)
653 {
654         int phy_reg;
655         u32 phy_id;
656         int i;
657
658         /* Poll for the rare case the FW or phy isn't ready yet.  */
659         for (i = 0; i < 100; i++) {
660                 phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID1);
661                 if (phy_reg != 0 && phy_reg != 0xFFFF)
662                         break;
663                 mdelay(1);
664         }
665
666         if (phy_reg <= 0 || phy_reg == 0xFFFF)
667                 return 0;
668
669         phy_id = (phy_reg & 0xffff) << 16;
670
671         phy_reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_PHYSID2);
672         if (phy_reg < 0)
673                 return 0;
674
675         phy_id |= (phy_reg & 0xffff);
676
677         return phy_id;
678 }
679
680 static void
681 asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
682 {
683         struct usbnet *dev = netdev_priv(net);
684         u8 opt;
685
686         if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
687                 wolinfo->supported = 0;
688                 wolinfo->wolopts = 0;
689                 return;
690         }
691         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
692         wolinfo->wolopts = 0;
693 }
694
695 static int
696 asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
697 {
698         struct usbnet *dev = netdev_priv(net);
699         u8 opt = 0;
700
701         if (wolinfo->wolopts & WAKE_PHY)
702                 opt |= AX_MONITOR_LINK;
703         if (wolinfo->wolopts & WAKE_MAGIC)
704                 opt |= AX_MONITOR_MAGIC;
705
706         if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
707                               opt, 0, 0, NULL) < 0)
708                 return -EINVAL;
709
710         return 0;
711 }
712
713 static int asix_get_eeprom_len(struct net_device *net)
714 {
715         struct usbnet *dev = netdev_priv(net);
716         struct asix_data *data = (struct asix_data *)&dev->data;
717
718         return data->eeprom_len;
719 }
720
721 static int asix_get_eeprom(struct net_device *net,
722                               struct ethtool_eeprom *eeprom, u8 *data)
723 {
724         struct usbnet *dev = netdev_priv(net);
725         __le16 *ebuf = (__le16 *)data;
726         int i;
727
728         /* Crude hack to ensure that we don't overwrite memory
729          * if an odd length is supplied
730          */
731         if (eeprom->len % 2)
732                 return -EINVAL;
733
734         eeprom->magic = AX_EEPROM_MAGIC;
735
736         /* ax8817x returns 2 bytes from eeprom on read */
737         for (i=0; i < eeprom->len / 2; i++) {
738                 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
739                         eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
740                         return -EINVAL;
741         }
742         return 0;
743 }
744
745 static void asix_get_drvinfo (struct net_device *net,
746                                  struct ethtool_drvinfo *info)
747 {
748         struct usbnet *dev = netdev_priv(net);
749         struct asix_data *data = (struct asix_data *)&dev->data;
750
751         /* Inherit standard device info */
752         usbnet_get_drvinfo(net, info);
753         strncpy (info->driver, DRIVER_NAME, sizeof info->driver);
754         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
755         info->eedump_len = data->eeprom_len;
756 }
757
758 static u32 asix_get_link(struct net_device *net)
759 {
760         struct usbnet *dev = netdev_priv(net);
761
762         return mii_link_ok(&dev->mii);
763 }
764
765 static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
766 {
767         struct usbnet *dev = netdev_priv(net);
768
769         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
770 }
771
772 static int asix_set_mac_address(struct net_device *net, void *p)
773 {
774         struct usbnet *dev = netdev_priv(net);
775         struct asix_data *data = (struct asix_data *)&dev->data;
776         struct sockaddr *addr = p;
777
778         if (netif_running(net))
779                 return -EBUSY;
780         if (!is_valid_ether_addr(addr->sa_data))
781                 return -EADDRNOTAVAIL;
782
783         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
784
785         /* We use the 20 byte dev->data
786          * for our 6 byte mac buffer
787          * to avoid allocating memory that
788          * is tricky to free later */
789         memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
790         asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
791                                                         data->mac_addr);
792
793         return 0;
794 }
795
796 /* We need to override some ethtool_ops so we require our
797    own structure so we don't interfere with other usbnet
798    devices that may be connected at the same time. */
799 static const struct ethtool_ops ax88172_ethtool_ops = {
800         .get_drvinfo            = asix_get_drvinfo,
801         .get_link               = asix_get_link,
802         .get_msglevel           = usbnet_get_msglevel,
803         .set_msglevel           = usbnet_set_msglevel,
804         .get_wol                = asix_get_wol,
805         .set_wol                = asix_set_wol,
806         .get_eeprom_len         = asix_get_eeprom_len,
807         .get_eeprom             = asix_get_eeprom,
808         .get_settings           = usbnet_get_settings,
809         .set_settings           = usbnet_set_settings,
810         .nway_reset             = usbnet_nway_reset,
811 };
812
813 static void ax88172_set_multicast(struct net_device *net)
814 {
815         struct usbnet *dev = netdev_priv(net);
816         struct asix_data *data = (struct asix_data *)&dev->data;
817         u8 rx_ctl = 0x8c;
818
819         if (net->flags & IFF_PROMISC) {
820                 rx_ctl |= 0x01;
821         } else if (net->flags & IFF_ALLMULTI ||
822                    netdev_mc_count(net) > AX_MAX_MCAST) {
823                 rx_ctl |= 0x02;
824         } else if (netdev_mc_empty(net)) {
825                 /* just broadcast and directed */
826         } else {
827                 /* We use the 20 byte dev->data
828                  * for our 8 byte filter buffer
829                  * to avoid allocating memory that
830                  * is tricky to free later */
831                 struct netdev_hw_addr *ha;
832                 u32 crc_bits;
833
834                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
835
836                 /* Build the multicast hash filter. */
837                 netdev_for_each_mc_addr(ha, net) {
838                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
839                         data->multi_filter[crc_bits >> 3] |=
840                             1 << (crc_bits & 7);
841                 }
842
843                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
844                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
845
846                 rx_ctl |= 0x10;
847         }
848
849         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
850 }
851
852 static int ax88172_link_reset(struct usbnet *dev)
853 {
854         u8 mode;
855         struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
856
857         mii_check_media(&dev->mii, 1, 1);
858         mii_ethtool_gset(&dev->mii, &ecmd);
859         mode = AX88172_MEDIUM_DEFAULT;
860
861         if (ecmd.duplex != DUPLEX_FULL)
862                 mode |= ~AX88172_MEDIUM_FD;
863
864         netdev_dbg(dev->net, "ax88172_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
865                    ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
866
867         asix_write_medium_mode(dev, mode);
868
869         return 0;
870 }
871
872 static const struct net_device_ops ax88172_netdev_ops = {
873         .ndo_open               = usbnet_open,
874         .ndo_stop               = usbnet_stop,
875         .ndo_start_xmit         = usbnet_start_xmit,
876         .ndo_tx_timeout         = usbnet_tx_timeout,
877         .ndo_change_mtu         = usbnet_change_mtu,
878         .ndo_set_mac_address    = eth_mac_addr,
879         .ndo_validate_addr      = eth_validate_addr,
880         .ndo_do_ioctl           = asix_ioctl,
881         .ndo_set_rx_mode        = ax88172_set_multicast,
882 };
883
884 static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
885 {
886         int ret = 0;
887         u8 buf[ETH_ALEN];
888         int i;
889         unsigned long gpio_bits = dev->driver_info->data;
890         struct asix_data *data = (struct asix_data *)&dev->data;
891
892         data->eeprom_len = AX88172_EEPROM_LEN;
893
894         usbnet_get_endpoints(dev,intf);
895
896         /* Toggle the GPIOs in a manufacturer/model specific way */
897         for (i = 2; i >= 0; i--) {
898                 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
899                                 (gpio_bits >> (i * 8)) & 0xff, 0, 0, NULL);
900                 if (ret < 0)
901                         goto out;
902                 msleep(5);
903         }
904
905         ret = asix_write_rx_ctl(dev, 0x80);
906         if (ret < 0)
907                 goto out;
908
909         /* Get the MAC address */
910         ret = asix_read_cmd(dev, AX88172_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
911         if (ret < 0) {
912                 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
913                 goto out;
914         }
915         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
916
917         /* Initialize MII structure */
918         dev->mii.dev = dev->net;
919         dev->mii.mdio_read = asix_mdio_read;
920         dev->mii.mdio_write = asix_mdio_write;
921         dev->mii.phy_id_mask = 0x3f;
922         dev->mii.reg_num_mask = 0x1f;
923         dev->mii.phy_id = asix_get_phy_addr(dev);
924
925         dev->net->netdev_ops = &ax88172_netdev_ops;
926         dev->net->ethtool_ops = &ax88172_ethtool_ops;
927
928         asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
929         asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
930                 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
931         mii_nway_restart(&dev->mii);
932
933         return 0;
934
935 out:
936         return ret;
937 }
938
939 static const struct ethtool_ops ax88772_ethtool_ops = {
940         .get_drvinfo            = asix_get_drvinfo,
941         .get_link               = asix_get_link,
942         .get_msglevel           = usbnet_get_msglevel,
943         .set_msglevel           = usbnet_set_msglevel,
944         .get_wol                = asix_get_wol,
945         .set_wol                = asix_set_wol,
946         .get_eeprom_len         = asix_get_eeprom_len,
947         .get_eeprom             = asix_get_eeprom,
948         .get_settings           = usbnet_get_settings,
949         .set_settings           = usbnet_set_settings,
950         .nway_reset             = usbnet_nway_reset,
951 };
952
953 static int ax88772_link_reset(struct usbnet *dev)
954 {
955         u16 mode;
956         struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
957
958         mii_check_media(&dev->mii, 1, 1);
959         mii_ethtool_gset(&dev->mii, &ecmd);
960         mode = AX88772_MEDIUM_DEFAULT;
961
962         if (ethtool_cmd_speed(&ecmd) != SPEED_100)
963                 mode &= ~AX_MEDIUM_PS;
964
965         if (ecmd.duplex != DUPLEX_FULL)
966                 mode &= ~AX_MEDIUM_FD;
967
968         netdev_dbg(dev->net, "ax88772_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
969                    ethtool_cmd_speed(&ecmd), ecmd.duplex, mode);
970
971         asix_write_medium_mode(dev, mode);
972
973         return 0;
974 }
975
976 static int ax88772_reset(struct usbnet *dev)
977 {
978         struct asix_data *data = (struct asix_data *)&dev->data;
979         int ret, embd_phy;
980         u16 rx_ctl;
981
982         ret = asix_write_gpio(dev,
983                         AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN, 5);
984         if (ret < 0)
985                 goto out;
986
987         embd_phy = ((asix_get_phy_addr(dev) & 0x1f) == 0x10 ? 1 : 0);
988
989         ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
990         if (ret < 0) {
991                 dbg("Select PHY #1 failed: %d", ret);
992                 goto out;
993         }
994
995         ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
996         if (ret < 0)
997                 goto out;
998
999         msleep(150);
1000
1001         ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
1002         if (ret < 0)
1003                 goto out;
1004
1005         msleep(150);
1006
1007         if (embd_phy) {
1008                 ret = asix_sw_reset(dev, AX_SWRESET_IPRL);
1009                 if (ret < 0)
1010                         goto out;
1011         } else {
1012                 ret = asix_sw_reset(dev, AX_SWRESET_PRTE);
1013                 if (ret < 0)
1014                         goto out;
1015         }
1016
1017         msleep(150);
1018         rx_ctl = asix_read_rx_ctl(dev);
1019         dbg("RX_CTL is 0x%04x after software reset", rx_ctl);
1020         ret = asix_write_rx_ctl(dev, 0x0000);
1021         if (ret < 0)
1022                 goto out;
1023
1024         rx_ctl = asix_read_rx_ctl(dev);
1025         dbg("RX_CTL is 0x%04x setting to 0x0000", rx_ctl);
1026
1027         ret = asix_sw_reset(dev, AX_SWRESET_PRL);
1028         if (ret < 0)
1029                 goto out;
1030
1031         msleep(150);
1032
1033         ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL);
1034         if (ret < 0)
1035                 goto out;
1036
1037         msleep(150);
1038
1039         asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1040         asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1041                         ADVERTISE_ALL | ADVERTISE_CSMA);
1042         mii_nway_restart(&dev->mii);
1043
1044         ret = asix_write_medium_mode(dev, AX88772_MEDIUM_DEFAULT);
1045         if (ret < 0)
1046                 goto out;
1047
1048         ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
1049                                 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
1050                                 AX88772_IPG2_DEFAULT, 0, NULL);
1051         if (ret < 0) {
1052                 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
1053                 goto out;
1054         }
1055
1056         /* Rewrite MAC address */
1057         memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
1058         ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
1059                                                         data->mac_addr);
1060         if (ret < 0)
1061                 goto out;
1062
1063         /* Set RX_CTL to default values with 2k buffer, and enable cactus */
1064         ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
1065         if (ret < 0)
1066                 goto out;
1067
1068         rx_ctl = asix_read_rx_ctl(dev);
1069         dbg("RX_CTL is 0x%04x after all initializations", rx_ctl);
1070
1071         rx_ctl = asix_read_medium_status(dev);
1072         dbg("Medium Status is 0x%04x after all initializations", rx_ctl);
1073
1074         return 0;
1075
1076 out:
1077         return ret;
1078
1079 }
1080
1081 static const struct net_device_ops ax88772_netdev_ops = {
1082         .ndo_open               = usbnet_open,
1083         .ndo_stop               = usbnet_stop,
1084         .ndo_start_xmit         = usbnet_start_xmit,
1085         .ndo_tx_timeout         = usbnet_tx_timeout,
1086         .ndo_change_mtu         = usbnet_change_mtu,
1087         .ndo_set_mac_address    = asix_set_mac_address,
1088         .ndo_validate_addr      = eth_validate_addr,
1089         .ndo_do_ioctl           = asix_ioctl,
1090         .ndo_set_rx_mode        = asix_set_multicast,
1091 };
1092
1093 static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
1094 {
1095         int ret, embd_phy;
1096         struct asix_data *data = (struct asix_data *)&dev->data;
1097         u8 buf[ETH_ALEN];
1098         u32 phyid;
1099
1100         data->eeprom_len = AX88772_EEPROM_LEN;
1101
1102         usbnet_get_endpoints(dev,intf);
1103
1104         /* Get the MAC address */
1105         ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
1106         if (ret < 0) {
1107                 dbg("Failed to read MAC address: %d", ret);
1108                 return ret;
1109         }
1110         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
1111
1112         /* Initialize MII structure */
1113         dev->mii.dev = dev->net;
1114         dev->mii.mdio_read = asix_mdio_read;
1115         dev->mii.mdio_write = asix_mdio_write;
1116         dev->mii.phy_id_mask = 0x1f;
1117         dev->mii.reg_num_mask = 0x1f;
1118         dev->mii.phy_id = asix_get_phy_addr(dev);
1119
1120         dev->net->netdev_ops = &ax88772_netdev_ops;
1121         dev->net->ethtool_ops = &ax88772_ethtool_ops;
1122
1123         embd_phy = ((dev->mii.phy_id & 0x1f) == 0x10 ? 1 : 0);
1124
1125         /* Reset the PHY to normal operation mode */
1126         ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, embd_phy, 0, 0, NULL);
1127         if (ret < 0) {
1128                 dbg("Select PHY #1 failed: %d", ret);
1129                 return ret;
1130         }
1131
1132         ret = asix_sw_reset(dev, AX_SWRESET_IPPD | AX_SWRESET_PRL);
1133         if (ret < 0)
1134                 return ret;
1135
1136         msleep(150);
1137
1138         ret = asix_sw_reset(dev, AX_SWRESET_CLEAR);
1139         if (ret < 0)
1140                 return ret;
1141
1142         msleep(150);
1143
1144         ret = asix_sw_reset(dev, embd_phy ? AX_SWRESET_IPRL : AX_SWRESET_PRTE);
1145
1146         /* Read PHYID register *AFTER* the PHY was reset properly */
1147         phyid = asix_get_phyid(dev);
1148         dbg("PHYID=0x%08x", phyid);
1149
1150         /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
1151         if (dev->driver_info->flags & FLAG_FRAMING_AX) {
1152                 /* hard_mtu  is still the default - the device does not support
1153                    jumbo eth frames */
1154                 dev->rx_urb_size = 2048;
1155         }
1156
1157         return 0;
1158 }
1159
1160 static struct ethtool_ops ax88178_ethtool_ops = {
1161         .get_drvinfo            = asix_get_drvinfo,
1162         .get_link               = asix_get_link,
1163         .get_msglevel           = usbnet_get_msglevel,
1164         .set_msglevel           = usbnet_set_msglevel,
1165         .get_wol                = asix_get_wol,
1166         .set_wol                = asix_set_wol,
1167         .get_eeprom_len         = asix_get_eeprom_len,
1168         .get_eeprom             = asix_get_eeprom,
1169         .get_settings           = usbnet_get_settings,
1170         .set_settings           = usbnet_set_settings,
1171         .nway_reset             = usbnet_nway_reset,
1172 };
1173
1174 static int marvell_phy_init(struct usbnet *dev)
1175 {
1176         struct asix_data *data = (struct asix_data *)&dev->data;
1177         u16 reg;
1178
1179         netdev_dbg(dev->net, "marvell_phy_init()\n");
1180
1181         reg = asix_mdio_read(dev->net, dev->mii.phy_id, MII_MARVELL_STATUS);
1182         netdev_dbg(dev->net, "MII_MARVELL_STATUS = 0x%04x\n", reg);
1183
1184         asix_mdio_write(dev->net, dev->mii.phy_id, MII_MARVELL_CTRL,
1185                         MARVELL_CTRL_RXDELAY | MARVELL_CTRL_TXDELAY);
1186
1187         if (data->ledmode) {
1188                 reg = asix_mdio_read(dev->net, dev->mii.phy_id,
1189                         MII_MARVELL_LED_CTRL);
1190                 netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (1) = 0x%04x\n", reg);
1191
1192                 reg &= 0xf8ff;
1193                 reg |= (1 + 0x0100);
1194                 asix_mdio_write(dev->net, dev->mii.phy_id,
1195                         MII_MARVELL_LED_CTRL, reg);
1196
1197                 reg = asix_mdio_read(dev->net, dev->mii.phy_id,
1198                         MII_MARVELL_LED_CTRL);
1199                 netdev_dbg(dev->net, "MII_MARVELL_LED_CTRL (2) = 0x%04x\n", reg);
1200                 reg &= 0xfc0f;
1201         }
1202
1203         return 0;
1204 }
1205
1206 static int rtl8211cl_phy_init(struct usbnet *dev)
1207 {
1208         struct asix_data *data = (struct asix_data *)&dev->data;
1209
1210         netdev_dbg(dev->net, "rtl8211cl_phy_init()\n");
1211
1212         asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0005);
1213         asix_mdio_write (dev->net, dev->mii.phy_id, 0x0c, 0);
1214         asix_mdio_write (dev->net, dev->mii.phy_id, 0x01,
1215                 asix_mdio_read (dev->net, dev->mii.phy_id, 0x01) | 0x0080);
1216         asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
1217
1218         if (data->ledmode == 12) {
1219                 asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0x0002);
1220                 asix_mdio_write (dev->net, dev->mii.phy_id, 0x1a, 0x00cb);
1221                 asix_mdio_write (dev->net, dev->mii.phy_id, 0x1f, 0);
1222         }
1223
1224         return 0;
1225 }
1226
1227 static int marvell_led_status(struct usbnet *dev, u16 speed)
1228 {
1229         u16 reg = asix_mdio_read(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL);
1230
1231         netdev_dbg(dev->net, "marvell_led_status() read 0x%04x\n", reg);
1232
1233         /* Clear out the center LED bits - 0x03F0 */
1234         reg &= 0xfc0f;
1235
1236         switch (speed) {
1237                 case SPEED_1000:
1238                         reg |= 0x03e0;
1239                         break;
1240                 case SPEED_100:
1241                         reg |= 0x03b0;
1242                         break;
1243                 default:
1244                         reg |= 0x02f0;
1245         }
1246
1247         netdev_dbg(dev->net, "marvell_led_status() writing 0x%04x\n", reg);
1248         asix_mdio_write(dev->net, dev->mii.phy_id, MARVELL_LED_MANUAL, reg);
1249
1250         return 0;
1251 }
1252
1253 static int ax88178_reset(struct usbnet *dev)
1254 {
1255         struct asix_data *data = (struct asix_data *)&dev->data;
1256         int ret;
1257         __le16 eeprom;
1258         u8 status;
1259         int gpio0 = 0;
1260         u32 phyid;
1261
1262         asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status);
1263         dbg("GPIO Status: 0x%04x", status);
1264
1265         asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL);
1266         asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom);
1267         asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL);
1268
1269         dbg("EEPROM index 0x17 is 0x%04x", eeprom);
1270
1271         if (eeprom == cpu_to_le16(0xffff)) {
1272                 data->phymode = PHY_MODE_MARVELL;
1273                 data->ledmode = 0;
1274                 gpio0 = 1;
1275         } else {
1276                 data->phymode = le16_to_cpu(eeprom) & 0x7F;
1277                 data->ledmode = le16_to_cpu(eeprom) >> 8;
1278                 gpio0 = (le16_to_cpu(eeprom) & 0x80) ? 0 : 1;
1279         }
1280         dbg("GPIO0: %d, PhyMode: %d", gpio0, data->phymode);
1281
1282         /* Power up external GigaPHY through AX88178 GPIO pin */
1283         asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_1 | AX_GPIO_GPO1EN, 40);
1284         if ((le16_to_cpu(eeprom) >> 8) != 1) {
1285                 asix_write_gpio(dev, 0x003c, 30);
1286                 asix_write_gpio(dev, 0x001c, 300);
1287                 asix_write_gpio(dev, 0x003c, 30);
1288         } else {
1289                 dbg("gpio phymode == 1 path");
1290                 asix_write_gpio(dev, AX_GPIO_GPO1EN, 30);
1291                 asix_write_gpio(dev, AX_GPIO_GPO1EN | AX_GPIO_GPO_1, 30);
1292         }
1293
1294         /* Read PHYID register *AFTER* powering up PHY */
1295         phyid = asix_get_phyid(dev);
1296         dbg("PHYID=0x%08x", phyid);
1297
1298         /* Set AX88178 to enable MII/GMII/RGMII interface for external PHY */
1299         asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL);
1300
1301         asix_sw_reset(dev, 0);
1302         msleep(150);
1303
1304         asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
1305         msleep(150);
1306
1307         asix_write_rx_ctl(dev, 0);
1308
1309         if (data->phymode == PHY_MODE_MARVELL) {
1310                 marvell_phy_init(dev);
1311                 msleep(60);
1312         } else if (data->phymode == PHY_MODE_RTL8211CL)
1313                 rtl8211cl_phy_init(dev);
1314
1315         asix_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
1316                         BMCR_RESET | BMCR_ANENABLE);
1317         asix_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1318                         ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
1319         asix_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
1320                         ADVERTISE_1000FULL);
1321
1322         mii_nway_restart(&dev->mii);
1323
1324         ret = asix_write_medium_mode(dev, AX88178_MEDIUM_DEFAULT);
1325         if (ret < 0)
1326                 return ret;
1327
1328         /* Rewrite MAC address */
1329         memcpy(data->mac_addr, dev->net->dev_addr, ETH_ALEN);
1330         ret = asix_write_cmd(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
1331                                                         data->mac_addr);
1332         if (ret < 0)
1333                 return ret;
1334
1335         ret = asix_write_rx_ctl(dev, AX_DEFAULT_RX_CTL);
1336         if (ret < 0)
1337                 return ret;
1338
1339         return 0;
1340 }
1341
1342 static int ax88178_link_reset(struct usbnet *dev)
1343 {
1344         u16 mode;
1345         struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
1346         struct asix_data *data = (struct asix_data *)&dev->data;
1347         u32 speed;
1348
1349         netdev_dbg(dev->net, "ax88178_link_reset()\n");
1350
1351         mii_check_media(&dev->mii, 1, 1);
1352         mii_ethtool_gset(&dev->mii, &ecmd);
1353         mode = AX88178_MEDIUM_DEFAULT;
1354         speed = ethtool_cmd_speed(&ecmd);
1355
1356         if (speed == SPEED_1000)
1357                 mode |= AX_MEDIUM_GM;
1358         else if (speed == SPEED_100)
1359                 mode |= AX_MEDIUM_PS;
1360         else
1361                 mode &= ~(AX_MEDIUM_PS | AX_MEDIUM_GM);
1362
1363         mode |= AX_MEDIUM_ENCK;
1364
1365         if (ecmd.duplex == DUPLEX_FULL)
1366                 mode |= AX_MEDIUM_FD;
1367         else
1368                 mode &= ~AX_MEDIUM_FD;
1369
1370         netdev_dbg(dev->net, "ax88178_link_reset() speed: %u duplex: %d setting mode to 0x%04x\n",
1371                    speed, ecmd.duplex, mode);
1372
1373         asix_write_medium_mode(dev, mode);
1374
1375         if (data->phymode == PHY_MODE_MARVELL && data->ledmode)
1376                 marvell_led_status(dev, speed);
1377
1378         return 0;
1379 }
1380
1381 static void ax88178_set_mfb(struct usbnet *dev)
1382 {
1383         u16 mfb = AX_RX_CTL_MFB_16384;
1384         u16 rxctl;
1385         u16 medium;
1386         int old_rx_urb_size = dev->rx_urb_size;
1387
1388         if (dev->hard_mtu < 2048) {
1389                 dev->rx_urb_size = 2048;
1390                 mfb = AX_RX_CTL_MFB_2048;
1391         } else if (dev->hard_mtu < 4096) {
1392                 dev->rx_urb_size = 4096;
1393                 mfb = AX_RX_CTL_MFB_4096;
1394         } else if (dev->hard_mtu < 8192) {
1395                 dev->rx_urb_size = 8192;
1396                 mfb = AX_RX_CTL_MFB_8192;
1397         } else if (dev->hard_mtu < 16384) {
1398                 dev->rx_urb_size = 16384;
1399                 mfb = AX_RX_CTL_MFB_16384;
1400         }
1401
1402         rxctl = asix_read_rx_ctl(dev);
1403         asix_write_rx_ctl(dev, (rxctl & ~AX_RX_CTL_MFB_16384) | mfb);
1404
1405         medium = asix_read_medium_status(dev);
1406         if (dev->net->mtu > 1500)
1407                 medium |= AX_MEDIUM_JFE;
1408         else
1409                 medium &= ~AX_MEDIUM_JFE;
1410         asix_write_medium_mode(dev, medium);
1411
1412         if (dev->rx_urb_size > old_rx_urb_size)
1413                 usbnet_unlink_rx_urbs(dev);
1414 }
1415
1416 static int ax88178_change_mtu(struct net_device *net, int new_mtu)
1417 {
1418         struct usbnet *dev = netdev_priv(net);
1419         int ll_mtu = new_mtu + net->hard_header_len + 4;
1420
1421         netdev_dbg(dev->net, "ax88178_change_mtu() new_mtu=%d\n", new_mtu);
1422
1423         if (new_mtu <= 0 || ll_mtu > 16384)
1424                 return -EINVAL;
1425
1426         if ((ll_mtu % dev->maxpacket) == 0)
1427                 return -EDOM;
1428
1429         net->mtu = new_mtu;
1430         dev->hard_mtu = net->mtu + net->hard_header_len;
1431         ax88178_set_mfb(dev);
1432
1433         return 0;
1434 }
1435
1436 static const struct net_device_ops ax88178_netdev_ops = {
1437         .ndo_open               = usbnet_open,
1438         .ndo_stop               = usbnet_stop,
1439         .ndo_start_xmit         = usbnet_start_xmit,
1440         .ndo_tx_timeout         = usbnet_tx_timeout,
1441         .ndo_set_mac_address    = asix_set_mac_address,
1442         .ndo_validate_addr      = eth_validate_addr,
1443         .ndo_set_rx_mode        = asix_set_multicast,
1444         .ndo_do_ioctl           = asix_ioctl,
1445         .ndo_change_mtu         = ax88178_change_mtu,
1446 };
1447
1448 static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
1449 {
1450         int ret;
1451         u8 buf[ETH_ALEN];
1452         struct asix_data *data = (struct asix_data *)&dev->data;
1453
1454         data->eeprom_len = AX88772_EEPROM_LEN;
1455
1456         usbnet_get_endpoints(dev,intf);
1457
1458         /* Get the MAC address */
1459         ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf);
1460         if (ret < 0) {
1461                 dbg("Failed to read MAC address: %d", ret);
1462                 return ret;
1463         }
1464         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
1465
1466         /* Initialize MII structure */
1467         dev->mii.dev = dev->net;
1468         dev->mii.mdio_read = asix_mdio_read;
1469         dev->mii.mdio_write = asix_mdio_write;
1470         dev->mii.phy_id_mask = 0x1f;
1471         dev->mii.reg_num_mask = 0xff;
1472         dev->mii.supports_gmii = 1;
1473         dev->mii.phy_id = asix_get_phy_addr(dev);
1474
1475         dev->net->netdev_ops = &ax88178_netdev_ops;
1476         dev->net->ethtool_ops = &ax88178_ethtool_ops;
1477
1478         /* Blink LEDS so users know driver saw dongle */
1479         asix_sw_reset(dev, 0);
1480         msleep(150);
1481
1482         asix_sw_reset(dev, AX_SWRESET_PRL | AX_SWRESET_IPPD);
1483         msleep(150);
1484
1485         /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
1486         if (dev->driver_info->flags & FLAG_FRAMING_AX) {
1487                 /* hard_mtu  is still the default - the device does not support
1488                    jumbo eth frames */
1489                 dev->rx_urb_size = 2048;
1490         }
1491
1492         return 0;
1493 }
1494
1495 static const struct driver_info ax8817x_info = {
1496         .description = "ASIX AX8817x USB 2.0 Ethernet",
1497         .bind = ax88172_bind,
1498         .status = asix_status,
1499         .link_reset = ax88172_link_reset,
1500         .reset = ax88172_link_reset,
1501         .flags =  FLAG_ETHER | FLAG_LINK_INTR,
1502         .data = 0x00130103,
1503 };
1504
1505 static const struct driver_info dlink_dub_e100_info = {
1506         .description = "DLink DUB-E100 USB Ethernet",
1507         .bind = ax88172_bind,
1508         .status = asix_status,
1509         .link_reset = ax88172_link_reset,
1510         .reset = ax88172_link_reset,
1511         .flags =  FLAG_ETHER | FLAG_LINK_INTR,
1512         .data = 0x009f9d9f,
1513 };
1514
1515 static const struct driver_info netgear_fa120_info = {
1516         .description = "Netgear FA-120 USB Ethernet",
1517         .bind = ax88172_bind,
1518         .status = asix_status,
1519         .link_reset = ax88172_link_reset,
1520         .reset = ax88172_link_reset,
1521         .flags =  FLAG_ETHER | FLAG_LINK_INTR,
1522         .data = 0x00130103,
1523 };
1524
1525 static const struct driver_info hawking_uf200_info = {
1526         .description = "Hawking UF200 USB Ethernet",
1527         .bind = ax88172_bind,
1528         .status = asix_status,
1529         .link_reset = ax88172_link_reset,
1530         .reset = ax88172_link_reset,
1531         .flags =  FLAG_ETHER | FLAG_LINK_INTR,
1532         .data = 0x001f1d1f,
1533 };
1534
1535 static const struct driver_info ax88772_info = {
1536         .description = "ASIX AX88772 USB 2.0 Ethernet",
1537         .bind = ax88772_bind,
1538         .status = asix_status,
1539         .link_reset = ax88772_link_reset,
1540         .reset = ax88772_reset,
1541         .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
1542         .rx_fixup = asix_rx_fixup,
1543         .tx_fixup = asix_tx_fixup,
1544 };
1545
1546 static const struct driver_info ax88178_info = {
1547         .description = "ASIX AX88178 USB 2.0 Ethernet",
1548         .bind = ax88178_bind,
1549         .status = asix_status,
1550         .link_reset = ax88178_link_reset,
1551         .reset = ax88178_reset,
1552         .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_LINK_INTR,
1553         .rx_fixup = asix_rx_fixup,
1554         .tx_fixup = asix_tx_fixup,
1555 };
1556
1557 static const struct usb_device_id       products [] = {
1558 {
1559         // Linksys USB200M
1560         USB_DEVICE (0x077b, 0x2226),
1561         .driver_info =  (unsigned long) &ax8817x_info,
1562 }, {
1563         // Netgear FA120
1564         USB_DEVICE (0x0846, 0x1040),
1565         .driver_info =  (unsigned long) &netgear_fa120_info,
1566 }, {
1567         // DLink DUB-E100
1568         USB_DEVICE (0x2001, 0x1a00),
1569         .driver_info =  (unsigned long) &dlink_dub_e100_info,
1570 }, {
1571         // Intellinet, ST Lab USB Ethernet
1572         USB_DEVICE (0x0b95, 0x1720),
1573         .driver_info =  (unsigned long) &ax8817x_info,
1574 }, {
1575         // Hawking UF200, TrendNet TU2-ET100
1576         USB_DEVICE (0x07b8, 0x420a),
1577         .driver_info =  (unsigned long) &hawking_uf200_info,
1578 }, {
1579         // Billionton Systems, USB2AR
1580         USB_DEVICE (0x08dd, 0x90ff),
1581         .driver_info =  (unsigned long) &ax8817x_info,
1582 }, {
1583         // ATEN UC210T
1584         USB_DEVICE (0x0557, 0x2009),
1585         .driver_info =  (unsigned long) &ax8817x_info,
1586 }, {
1587         // Buffalo LUA-U2-KTX
1588         USB_DEVICE (0x0411, 0x003d),
1589         .driver_info =  (unsigned long) &ax8817x_info,
1590 }, {
1591         // Buffalo LUA-U2-GT 10/100/1000
1592         USB_DEVICE (0x0411, 0x006e),
1593         .driver_info =  (unsigned long) &ax88178_info,
1594 }, {
1595         // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
1596         USB_DEVICE (0x6189, 0x182d),
1597         .driver_info =  (unsigned long) &ax8817x_info,
1598 }, {
1599         // Sitecom LN-031 "USB 2.0 10/100/1000 Ethernet adapter"
1600         USB_DEVICE (0x0df6, 0x0056),
1601         .driver_info =  (unsigned long) &ax88178_info,
1602 }, {
1603         // corega FEther USB2-TX
1604         USB_DEVICE (0x07aa, 0x0017),
1605         .driver_info =  (unsigned long) &ax8817x_info,
1606 }, {
1607         // Surecom EP-1427X-2
1608         USB_DEVICE (0x1189, 0x0893),
1609         .driver_info = (unsigned long) &ax8817x_info,
1610 }, {
1611         // goodway corp usb gwusb2e
1612         USB_DEVICE (0x1631, 0x6200),
1613         .driver_info = (unsigned long) &ax8817x_info,
1614 }, {
1615         // JVC MP-PRX1 Port Replicator
1616         USB_DEVICE (0x04f1, 0x3008),
1617         .driver_info = (unsigned long) &ax8817x_info,
1618 }, {
1619         // ASIX AX88772B 10/100
1620         USB_DEVICE (0x0b95, 0x772b),
1621         .driver_info = (unsigned long) &ax88772_info,
1622 }, {
1623         // ASIX AX88772 10/100
1624         USB_DEVICE (0x0b95, 0x7720),
1625         .driver_info = (unsigned long) &ax88772_info,
1626 }, {
1627         // ASIX AX88178 10/100/1000
1628         USB_DEVICE (0x0b95, 0x1780),
1629         .driver_info = (unsigned long) &ax88178_info,
1630 }, {
1631         // Logitec LAN-GTJ/U2A
1632         USB_DEVICE (0x0789, 0x0160),
1633         .driver_info = (unsigned long) &ax88178_info,
1634 }, {
1635         // Linksys USB200M Rev 2
1636         USB_DEVICE (0x13b1, 0x0018),
1637         .driver_info = (unsigned long) &ax88772_info,
1638 }, {
1639         // 0Q0 cable ethernet
1640         USB_DEVICE (0x1557, 0x7720),
1641         .driver_info = (unsigned long) &ax88772_info,
1642 }, {
1643         // DLink DUB-E100 H/W Ver B1
1644         USB_DEVICE (0x07d1, 0x3c05),
1645         .driver_info = (unsigned long) &ax88772_info,
1646 }, {
1647         // DLink DUB-E100 H/W Ver B1 Alternate
1648         USB_DEVICE (0x2001, 0x3c05),
1649         .driver_info = (unsigned long) &ax88772_info,
1650 }, {
1651        // DLink DUB-E100 H/W Ver C1
1652        USB_DEVICE (0x2001, 0x1a02),
1653        .driver_info = (unsigned long) &ax88772_info,
1654 }, {
1655         // Linksys USB1000
1656         USB_DEVICE (0x1737, 0x0039),
1657         .driver_info = (unsigned long) &ax88178_info,
1658 }, {
1659         // IO-DATA ETG-US2
1660         USB_DEVICE (0x04bb, 0x0930),
1661         .driver_info = (unsigned long) &ax88178_info,
1662 }, {
1663         // Belkin F5D5055
1664         USB_DEVICE(0x050d, 0x5055),
1665         .driver_info = (unsigned long) &ax88178_info,
1666 }, {
1667         // Apple USB Ethernet Adapter
1668         USB_DEVICE(0x05ac, 0x1402),
1669         .driver_info = (unsigned long) &ax88772_info,
1670 }, {
1671         // Cables-to-Go USB Ethernet Adapter
1672         USB_DEVICE(0x0b95, 0x772a),
1673         .driver_info = (unsigned long) &ax88772_info,
1674 }, {
1675         // ABOCOM for pci
1676         USB_DEVICE(0x14ea, 0xab11),
1677         .driver_info = (unsigned long) &ax88178_info,
1678 }, {
1679         // ASIX 88772a
1680         USB_DEVICE(0x0db0, 0xa877),
1681         .driver_info = (unsigned long) &ax88772_info,
1682 }, {
1683         // Asus USB Ethernet Adapter
1684         USB_DEVICE (0x0b95, 0x7e2b),
1685         .driver_info = (unsigned long) &ax88772_info,
1686 },
1687         { },            // END
1688 };
1689 MODULE_DEVICE_TABLE(usb, products);
1690
1691 static struct usb_driver asix_driver = {
1692         .name =         DRIVER_NAME,
1693         .id_table =     products,
1694         .probe =        usbnet_probe,
1695         .suspend =      usbnet_suspend,
1696         .resume =       usbnet_resume,
1697         .disconnect =   usbnet_disconnect,
1698         .supports_autosuspend = 1,
1699 };
1700
1701 static int __init asix_init(void)
1702 {
1703         return usb_register(&asix_driver);
1704 }
1705 module_init(asix_init);
1706
1707 static void __exit asix_exit(void)
1708 {
1709         usb_deregister(&asix_driver);
1710 }
1711 module_exit(asix_exit);
1712
1713 MODULE_AUTHOR("David Hollis");
1714 MODULE_VERSION(DRIVER_VERSION);
1715 MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
1716 MODULE_LICENSE("GPL");
1717