]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/net/asix.c
drm: i810_dma.c: fix pointer arithmetic for 64-bit target
[linux-2.6-omap-h63xx.git] / drivers / usb / net / asix.c
1 /*
2  * ASIX AX8817X based USB 2.0 Ethernet Devices
3  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
4  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5  * Copyright (c) 2002-2003 TiVo Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 // #define      DEBUG                   // error path messages, extra info
23 // #define      VERBOSE                 // more; success messages
24
25 #include <linux/module.h>
26 #include <linux/kmod.h>
27 #include <linux/sched.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
37 #include "usbnet.h"
38
39 /* ASIX AX8817X based USB 2.0 Ethernet Devices */
40
41 #define AX_CMD_SET_SW_MII               0x06
42 #define AX_CMD_READ_MII_REG             0x07
43 #define AX_CMD_WRITE_MII_REG            0x08
44 #define AX_CMD_SET_HW_MII               0x0a
45 #define AX_CMD_READ_EEPROM              0x0b
46 #define AX_CMD_WRITE_EEPROM             0x0c
47 #define AX_CMD_WRITE_ENABLE             0x0d
48 #define AX_CMD_WRITE_DISABLE            0x0e
49 #define AX_CMD_WRITE_RX_CTL             0x10
50 #define AX_CMD_READ_IPG012              0x11
51 #define AX_CMD_WRITE_IPG0               0x12
52 #define AX_CMD_WRITE_IPG1               0x13
53 #define AX_CMD_WRITE_IPG2               0x14
54 #define AX_CMD_WRITE_MULTI_FILTER       0x16
55 #define AX_CMD_READ_NODE_ID             0x17
56 #define AX_CMD_READ_PHY_ID              0x19
57 #define AX_CMD_READ_MEDIUM_STATUS       0x1a
58 #define AX_CMD_WRITE_MEDIUM_MODE        0x1b
59 #define AX_CMD_READ_MONITOR_MODE        0x1c
60 #define AX_CMD_WRITE_MONITOR_MODE       0x1d
61 #define AX_CMD_WRITE_GPIOS              0x1f
62 #define AX_CMD_SW_RESET                 0x20
63 #define AX_CMD_SW_PHY_STATUS            0x21
64 #define AX_CMD_SW_PHY_SELECT            0x22
65 #define AX88772_CMD_READ_NODE_ID        0x13
66
67 #define AX_MONITOR_MODE                 0x01
68 #define AX_MONITOR_LINK                 0x02
69 #define AX_MONITOR_MAGIC                0x04
70 #define AX_MONITOR_HSFS                 0x10
71
72 /* AX88172 Medium Status Register values */
73 #define AX_MEDIUM_FULL_DUPLEX           0x02
74 #define AX_MEDIUM_TX_ABORT_ALLOW        0x04
75 #define AX_MEDIUM_FLOW_CONTROL_EN       0x10
76
77 #define AX_MCAST_FILTER_SIZE            8
78 #define AX_MAX_MCAST                    64
79
80 #define AX_EEPROM_LEN                   0x40
81
82 #define AX_SWRESET_CLEAR                0x00
83 #define AX_SWRESET_RR                   0x01
84 #define AX_SWRESET_RT                   0x02
85 #define AX_SWRESET_PRTE                 0x04
86 #define AX_SWRESET_PRL                  0x08
87 #define AX_SWRESET_BZ                   0x10
88 #define AX_SWRESET_IPRL                 0x20
89 #define AX_SWRESET_IPPD                 0x40
90
91 #define AX88772_IPG0_DEFAULT            0x15
92 #define AX88772_IPG1_DEFAULT            0x0c
93 #define AX88772_IPG2_DEFAULT            0x12
94
95 #define AX88772_MEDIUM_FULL_DUPLEX      0x0002
96 #define AX88772_MEDIUM_RESERVED         0x0004
97 #define AX88772_MEDIUM_RX_FC_ENABLE     0x0010
98 #define AX88772_MEDIUM_TX_FC_ENABLE     0x0020
99 #define AX88772_MEDIUM_PAUSE_FORMAT     0x0080
100 #define AX88772_MEDIUM_RX_ENABLE        0x0100
101 #define AX88772_MEDIUM_100MB            0x0200
102 #define AX88772_MEDIUM_DEFAULT  \
103         (AX88772_MEDIUM_FULL_DUPLEX | AX88772_MEDIUM_RX_FC_ENABLE | \
104          AX88772_MEDIUM_TX_FC_ENABLE | AX88772_MEDIUM_100MB | \
105          AX88772_MEDIUM_RESERVED | AX88772_MEDIUM_RX_ENABLE )
106
107 #define AX_EEPROM_MAGIC                 0xdeadbeef
108
109 /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
110 struct asix_data {
111         u8 multi_filter[AX_MCAST_FILTER_SIZE];
112 };
113
114 struct ax88172_int_data {
115         u16 res1;
116         u8 link;
117         u16 res2;
118         u8 status;
119         u16 res3;
120 } __attribute__ ((packed));
121
122 static int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
123                             u16 size, void *data)
124 {
125         return usb_control_msg(
126                 dev->udev,
127                 usb_rcvctrlpipe(dev->udev, 0),
128                 cmd,
129                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
130                 value,
131                 index,
132                 data,
133                 size,
134                 USB_CTRL_GET_TIMEOUT);
135 }
136
137 static int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
138                              u16 size, void *data)
139 {
140         return usb_control_msg(
141                 dev->udev,
142                 usb_sndctrlpipe(dev->udev, 0),
143                 cmd,
144                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
145                 value,
146                 index,
147                 data,
148                 size,
149                 USB_CTRL_SET_TIMEOUT);
150 }
151
152 static void asix_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
153 {
154         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
155
156         if (urb->status < 0)
157                 printk(KERN_DEBUG "asix_async_cmd_callback() failed with %d",
158                         urb->status);
159
160         kfree(req);
161         usb_free_urb(urb);
162 }
163
164 static inline int asix_set_sw_mii(struct usbnet *dev)
165 {
166         int ret;
167         ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);
168         if (ret < 0)
169                 devdbg(dev, "Failed to enable software MII access");
170         return ret;
171 }
172
173 static inline int asix_set_hw_mii(struct usbnet *dev)
174 {
175         int ret;
176         ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL);
177         if (ret < 0)
178                 devdbg(dev, "Failed to enable hardware MII access");
179         return ret;
180 }
181
182 static inline int asix_get_phyid(struct usbnet *dev)
183 {
184         int ret = 0;
185         void *buf;
186
187         buf = kmalloc(2, GFP_KERNEL);
188         if (!buf)
189                 goto out1;
190
191         if ((ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID,
192                                     0, 0, 2, buf)) < 2) {
193                 devdbg(dev, "Error reading PHYID register: %02x", ret);
194                 goto out2;
195         }
196         ret = *((u8 *)buf + 1);
197 out2:
198         kfree(buf);
199 out1:
200         return ret;
201 }
202
203 static int asix_sw_reset(struct usbnet *dev, u8 flags)
204 {
205         int ret;
206
207         ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL);
208         if (ret < 0)
209                 devdbg(dev,"Failed to send software reset: %02x", ret);
210
211         return ret;
212 }
213
214 static int asix_write_rx_ctl(struct usbnet *dev, u16 mode)
215 {
216         int ret;
217
218         ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL);
219         if (ret < 0)
220                 devdbg(dev, "Failed to write RX_CTL mode: %02x", ret);
221
222         return ret;
223 }
224
225 static void asix_status(struct usbnet *dev, struct urb *urb)
226 {
227         struct ax88172_int_data *event;
228         int link;
229
230         if (urb->actual_length < 8)
231                 return;
232
233         event = urb->transfer_buffer;
234         link = event->link & 0x01;
235         if (netif_carrier_ok(dev->net) != link) {
236                 if (link) {
237                         netif_carrier_on(dev->net);
238                         usbnet_defer_kevent (dev, EVENT_LINK_RESET );
239                 } else
240                         netif_carrier_off(dev->net);
241                 devdbg(dev, "Link Status is: %d", link);
242         }
243 }
244
245 static void
246 asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
247                                     u16 size, void *data)
248 {
249         struct usb_ctrlrequest *req;
250         int status;
251         struct urb *urb;
252
253         if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
254                 devdbg(dev, "Error allocating URB in write_cmd_async!");
255                 return;
256         }
257
258         if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
259                 deverr(dev, "Failed to allocate memory for control request");
260                 usb_free_urb(urb);
261                 return;
262         }
263
264         req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
265         req->bRequest = cmd;
266         req->wValue = cpu_to_le16(value);
267         req->wIndex = cpu_to_le16(index);
268         req->wLength = cpu_to_le16(size);
269
270         usb_fill_control_urb(urb, dev->udev,
271                              usb_sndctrlpipe(dev->udev, 0),
272                              (void *)req, data, size,
273                              asix_async_cmd_callback, req);
274
275         if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
276                 deverr(dev, "Error submitting the control message: status=%d",
277                                 status);
278                 kfree(req);
279                 usb_free_urb(urb);
280         }
281 }
282
283 static void asix_set_multicast(struct net_device *net)
284 {
285         struct usbnet *dev = netdev_priv(net);
286         struct asix_data *data = (struct asix_data *)&dev->data;
287         u8 rx_ctl = 0x8c;
288
289         if (net->flags & IFF_PROMISC) {
290                 rx_ctl |= 0x01;
291         } else if (net->flags & IFF_ALLMULTI
292                    || net->mc_count > AX_MAX_MCAST) {
293                 rx_ctl |= 0x02;
294         } else if (net->mc_count == 0) {
295                 /* just broadcast and directed */
296         } else {
297                 /* We use the 20 byte dev->data
298                  * for our 8 byte filter buffer
299                  * to avoid allocating memory that
300                  * is tricky to free later */
301                 struct dev_mc_list *mc_list = net->mc_list;
302                 u32 crc_bits;
303                 int i;
304
305                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
306
307                 /* Build the multicast hash filter. */
308                 for (i = 0; i < net->mc_count; i++) {
309                         crc_bits =
310                             ether_crc(ETH_ALEN,
311                                       mc_list->dmi_addr) >> 26;
312                         data->multi_filter[crc_bits >> 3] |=
313                             1 << (crc_bits & 7);
314                         mc_list = mc_list->next;
315                 }
316
317                 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
318                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
319
320                 rx_ctl |= 0x10;
321         }
322
323         asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
324 }
325
326 static int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
327 {
328         struct usbnet *dev = netdev_priv(netdev);
329         u16 res;
330
331         asix_set_sw_mii(dev);
332         asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
333                                 (__u16)loc, 2, (u16 *)&res);
334         asix_set_hw_mii(dev);
335
336         return res & 0xffff;
337 }
338
339 /* same as above, but converts resulting value to cpu byte order */
340 static int asix_mdio_read_le(struct net_device *netdev, int phy_id, int loc)
341 {
342         return le16_to_cpu(asix_mdio_read(netdev,phy_id, loc));
343 }
344
345 static void
346 asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
347 {
348         struct usbnet *dev = netdev_priv(netdev);
349         u16 res = val;
350
351         asix_set_sw_mii(dev);
352         asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
353                                 (__u16)loc, 2, (u16 *)&res);
354         asix_set_hw_mii(dev);
355 }
356
357 /* same as above, but converts new value to le16 byte order before writing */
358 static void
359 asix_mdio_write_le(struct net_device *netdev, int phy_id, int loc, int val)
360 {
361         asix_mdio_write( netdev, phy_id, loc, cpu_to_le16(val) );
362 }
363
364 static int ax88172_link_reset(struct usbnet *dev)
365 {
366         u16 lpa;
367         u16 adv;
368         u16 res;
369         u8 mode;
370
371         mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN;
372         lpa = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
373         adv = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
374         res = mii_nway_result(lpa|adv);
375         if (res & LPA_DUPLEX)
376                 mode |= AX_MEDIUM_FULL_DUPLEX;
377         asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
378
379         return 0;
380 }
381
382 static void
383 asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
384 {
385         struct usbnet *dev = netdev_priv(net);
386         u8 opt;
387
388         if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
389                 wolinfo->supported = 0;
390                 wolinfo->wolopts = 0;
391                 return;
392         }
393         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
394         wolinfo->wolopts = 0;
395         if (opt & AX_MONITOR_MODE) {
396                 if (opt & AX_MONITOR_LINK)
397                         wolinfo->wolopts |= WAKE_PHY;
398                 if (opt & AX_MONITOR_MAGIC)
399                         wolinfo->wolopts |= WAKE_MAGIC;
400         }
401 }
402
403 static int
404 asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
405 {
406         struct usbnet *dev = netdev_priv(net);
407         u8 opt = 0;
408         u8 buf[1];
409
410         if (wolinfo->wolopts & WAKE_PHY)
411                 opt |= AX_MONITOR_LINK;
412         if (wolinfo->wolopts & WAKE_MAGIC)
413                 opt |= AX_MONITOR_MAGIC;
414         if (opt != 0)
415                 opt |= AX_MONITOR_MODE;
416
417         if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
418                               opt, 0, 0, &buf) < 0)
419                 return -EINVAL;
420
421         return 0;
422 }
423
424 static int asix_get_eeprom_len(struct net_device *net)
425 {
426         return AX_EEPROM_LEN;
427 }
428
429 static int asix_get_eeprom(struct net_device *net,
430                               struct ethtool_eeprom *eeprom, u8 *data)
431 {
432         struct usbnet *dev = netdev_priv(net);
433         u16 *ebuf = (u16 *)data;
434         int i;
435
436         /* Crude hack to ensure that we don't overwrite memory
437          * if an odd length is supplied
438          */
439         if (eeprom->len % 2)
440                 return -EINVAL;
441
442         eeprom->magic = AX_EEPROM_MAGIC;
443
444         /* ax8817x returns 2 bytes from eeprom on read */
445         for (i=0; i < eeprom->len / 2; i++) {
446                 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM,
447                         eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
448                         return -EINVAL;
449         }
450         return 0;
451 }
452
453 static void asix_get_drvinfo (struct net_device *net,
454                                  struct ethtool_drvinfo *info)
455 {
456         /* Inherit standard device info */
457         usbnet_get_drvinfo(net, info);
458         info->eedump_len = 0x3e;
459 }
460
461 static int asix_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
462 {
463         struct usbnet *dev = netdev_priv(net);
464
465         return mii_ethtool_gset(&dev->mii,cmd);
466 }
467
468 static int asix_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
469 {
470         struct usbnet *dev = netdev_priv(net);
471
472         return mii_ethtool_sset(&dev->mii,cmd);
473 }
474
475 /* We need to override some ethtool_ops so we require our
476    own structure so we don't interfere with other usbnet
477    devices that may be connected at the same time. */
478 static struct ethtool_ops ax88172_ethtool_ops = {
479         .get_drvinfo            = asix_get_drvinfo,
480         .get_link               = ethtool_op_get_link,
481         .get_msglevel           = usbnet_get_msglevel,
482         .set_msglevel           = usbnet_set_msglevel,
483         .get_wol                = asix_get_wol,
484         .set_wol                = asix_set_wol,
485         .get_eeprom_len         = asix_get_eeprom_len,
486         .get_eeprom             = asix_get_eeprom,
487         .get_settings           = asix_get_settings,
488         .set_settings           = asix_set_settings,
489 };
490
491 static int asix_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
492 {
493         struct usbnet *dev = netdev_priv(net);
494
495         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
496 }
497
498 static int ax88172_bind(struct usbnet *dev, struct usb_interface *intf)
499 {
500         int ret = 0;
501         void *buf;
502         int i;
503         unsigned long gpio_bits = dev->driver_info->data;
504
505         usbnet_get_endpoints(dev,intf);
506
507         buf = kmalloc(ETH_ALEN, GFP_KERNEL);
508         if(!buf) {
509                 ret = -ENOMEM;
510                 goto out1;
511         }
512
513         /* Toggle the GPIOs in a manufacturer/model specific way */
514         for (i = 2; i >= 0; i--) {
515                 if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
516                                         (gpio_bits >> (i * 8)) & 0xff, 0, 0,
517                                         buf)) < 0)
518                         goto out2;
519                 msleep(5);
520         }
521
522         if ((ret = asix_write_rx_ctl(dev,0x80)) < 0)
523                 goto out2;
524
525         /* Get the MAC address */
526         memset(buf, 0, ETH_ALEN);
527         if ((ret = asix_read_cmd(dev, AX_CMD_READ_NODE_ID,
528                                 0, 0, 6, buf)) < 0) {
529                 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
530                 goto out2;
531         }
532         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
533
534         /* Initialize MII structure */
535         dev->mii.dev = dev->net;
536         dev->mii.mdio_read = asix_mdio_read;
537         dev->mii.mdio_write = asix_mdio_write;
538         dev->mii.phy_id_mask = 0x3f;
539         dev->mii.reg_num_mask = 0x1f;
540         dev->mii.phy_id = asix_get_phyid(dev);
541         dev->net->do_ioctl = asix_ioctl;
542
543         dev->net->set_multicast_list = asix_set_multicast;
544         dev->net->ethtool_ops = &ax88172_ethtool_ops;
545
546         asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
547         asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
548                 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
549         mii_nway_restart(&dev->mii);
550
551         return 0;
552 out2:
553         kfree(buf);
554 out1:
555         return ret;
556 }
557
558 static struct ethtool_ops ax88772_ethtool_ops = {
559         .get_drvinfo            = asix_get_drvinfo,
560         .get_link               = ethtool_op_get_link,
561         .get_msglevel           = usbnet_get_msglevel,
562         .set_msglevel           = usbnet_set_msglevel,
563         .get_wol                = asix_get_wol,
564         .set_wol                = asix_set_wol,
565         .get_eeprom_len         = asix_get_eeprom_len,
566         .get_eeprom             = asix_get_eeprom,
567         .get_settings           = asix_get_settings,
568         .set_settings           = asix_set_settings,
569 };
570
571 static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
572 {
573         int ret;
574         void *buf;
575
576         usbnet_get_endpoints(dev,intf);
577
578         buf = kmalloc(6, GFP_KERNEL);
579         if(!buf) {
580                 dbg ("Cannot allocate memory for buffer");
581                 ret = -ENOMEM;
582                 goto out1;
583         }
584
585         if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS,
586                                      0x00B0, 0, 0, buf)) < 0)
587                 goto out2;
588
589         msleep(5);
590         if ((ret = asix_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
591                                 0x0001, 0, 0, buf)) < 0) {
592                 dbg("Select PHY #1 failed: %d", ret);
593                 goto out2;
594         }
595
596         if ((ret = asix_sw_reset(dev, AX_SWRESET_IPPD)) < 0)
597                 goto out2;
598
599         msleep(150);
600         if ((ret = asix_sw_reset(dev, AX_SWRESET_CLEAR)) < 0)
601                 goto out2;
602
603         msleep(150);
604         if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL)) < 0)
605                 goto out2;
606
607         msleep(150);
608         if ((ret = asix_write_rx_ctl(dev, 0x00)) < 0)
609                 goto out2;
610
611         /* Get the MAC address */
612         memset(buf, 0, ETH_ALEN);
613         if ((ret = asix_read_cmd(dev, AX88772_CMD_READ_NODE_ID,
614                                 0, 0, ETH_ALEN, buf)) < 0) {
615                 dbg("Failed to read MAC address: %d", ret);
616                 goto out2;
617         }
618         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
619
620         if ((ret = asix_set_sw_mii(dev)) < 0)
621                 goto out2;
622
623         if (((ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG,
624                                 0x0010, 2, 2, buf)) < 0)
625                         || (*((u16 *)buf) != 0x003b)) {
626                 dbg("Read PHY register 2 must be 0x3b00: %d", ret);
627                 goto out2;
628         }
629
630         /* Initialize MII structure */
631         dev->mii.dev = dev->net;
632         dev->mii.mdio_read = asix_mdio_read;
633         dev->mii.mdio_write = asix_mdio_write;
634         dev->mii.phy_id_mask = 0xff;
635         dev->mii.reg_num_mask = 0xff;
636         dev->net->do_ioctl = asix_ioctl;
637         dev->mii.phy_id = asix_get_phyid(dev);
638
639         if ((ret = asix_sw_reset(dev, AX_SWRESET_PRL)) < 0)
640                 goto out2;
641
642         msleep(150);
643
644         if ((ret = asix_sw_reset(dev, AX_SWRESET_IPRL | AX_SWRESET_PRL)) < 0)
645                 goto out2;
646
647         msleep(150);
648
649         dev->net->set_multicast_list = asix_set_multicast;
650         dev->net->ethtool_ops = &ax88772_ethtool_ops;
651
652         asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
653         asix_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
654                         ADVERTISE_ALL | ADVERTISE_CSMA);
655         mii_nway_restart(&dev->mii);
656
657         if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
658                                 AX88772_MEDIUM_DEFAULT, 0, 0, buf)) < 0) {
659                 dbg("Write medium mode register: %d", ret);
660                 goto out2;
661         }
662
663         if ((ret = asix_write_cmd(dev, AX_CMD_WRITE_IPG0,
664                                 AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
665                                 AX88772_IPG2_DEFAULT, 0, buf)) < 0) {
666                 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
667                 goto out2;
668         }
669         if ((ret = asix_set_hw_mii(dev)) < 0)
670                 goto out2;
671
672         /* Set RX_CTL to default values with 2k buffer, and enable cactus */
673         if ((ret = asix_write_rx_ctl(dev, 0x0088)) < 0)
674                 goto out2;
675
676         kfree(buf);
677
678         /* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
679         if (dev->driver_info->flags & FLAG_FRAMING_AX) {
680                 /* hard_mtu  is still the default - the device does not support
681                    jumbo eth frames */
682                 dev->rx_urb_size = 2048;
683         }
684
685         return 0;
686
687 out2:
688         kfree(buf);
689 out1:
690         return ret;
691 }
692
693 static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
694 {
695         u8  *head;
696         u32  header;
697         char *packet;
698         struct sk_buff *ax_skb;
699         u16 size;
700
701         head = (u8 *) skb->data;
702         memcpy(&header, head, sizeof(header));
703         le32_to_cpus(&header);
704         packet = head + sizeof(header);
705
706         skb_pull(skb, 4);
707
708         while (skb->len > 0) {
709                 if ((short)(header & 0x0000ffff) !=
710                     ~((short)((header & 0xffff0000) >> 16))) {
711                         devdbg(dev,"header length data is error");
712                 }
713                 /* get the packet length */
714                 size = (u16) (header & 0x0000ffff);
715
716                 if ((skb->len) - ((size + 1) & 0xfffe) == 0)
717                         return 2;
718                 if (size > ETH_FRAME_LEN) {
719                         devdbg(dev,"invalid rx length %d", size);
720                         return 0;
721                 }
722                 ax_skb = skb_clone(skb, GFP_ATOMIC);
723                 if (ax_skb) {
724                         ax_skb->len = size;
725                         ax_skb->data = packet;
726                         ax_skb->tail = packet + size;
727                         usbnet_skb_return(dev, ax_skb);
728                 } else {
729                         return 0;
730                 }
731
732                 skb_pull(skb, (size + 1) & 0xfffe);
733
734                 if (skb->len == 0)
735                         break;
736
737                 head = (u8 *) skb->data;
738                 memcpy(&header, head, sizeof(header));
739                 le32_to_cpus(&header);
740                 packet = head + sizeof(header);
741                 skb_pull(skb, 4);
742         }
743
744         if (skb->len < 0) {
745                 devdbg(dev,"invalid rx length %d", skb->len);
746                 return 0;
747         }
748         return 1;
749 }
750
751 static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
752                                         gfp_t flags)
753 {
754         int padlen;
755         int headroom = skb_headroom(skb);
756         int tailroom = skb_tailroom(skb);
757         u32 packet_len;
758         u32 padbytes = 0xffff0000;
759
760         padlen = ((skb->len + 4) % 512) ? 0 : 4;
761
762         if ((!skb_cloned(skb))
763             && ((headroom + tailroom) >= (4 + padlen))) {
764                 if ((headroom < 4) || (tailroom < padlen)) {
765                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
766                         skb->tail = skb->data + skb->len;
767                 }
768         } else {
769                 struct sk_buff *skb2;
770                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
771                 dev_kfree_skb_any(skb);
772                 skb = skb2;
773                 if (!skb)
774                         return NULL;
775         }
776
777         skb_push(skb, 4);
778         packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
779         memcpy(skb->data, &packet_len, sizeof(packet_len));
780
781         if ((skb->len % 512) == 0) {
782                 memcpy( skb->tail, &padbytes, sizeof(padbytes));
783                 skb_put(skb, sizeof(padbytes));
784         }
785         return skb;
786 }
787
788 static int ax88772_link_reset(struct usbnet *dev)
789 {
790         u16 lpa;
791         u16 adv;
792         u16 res;
793         u16 mode;
794
795         mode = AX88772_MEDIUM_DEFAULT;
796         lpa = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
797         adv = asix_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
798         res = mii_nway_result(lpa|adv);
799
800         if ((res & LPA_DUPLEX) == 0)
801                 mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
802         if ((res & LPA_100) == 0)
803                 mode &= ~AX88772_MEDIUM_100MB;
804         asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
805
806         return 0;
807 }
808
809 static const struct driver_info ax8817x_info = {
810         .description = "ASIX AX8817x USB 2.0 Ethernet",
811         .bind = ax88172_bind,
812         .status = asix_status,
813         .link_reset = ax88172_link_reset,
814         .reset = ax88172_link_reset,
815         .flags =  FLAG_ETHER,
816         .data = 0x00130103,
817 };
818
819 static const struct driver_info dlink_dub_e100_info = {
820         .description = "DLink DUB-E100 USB Ethernet",
821         .bind = ax88172_bind,
822         .status = asix_status,
823         .link_reset = ax88172_link_reset,
824         .reset = ax88172_link_reset,
825         .flags =  FLAG_ETHER,
826         .data = 0x009f9d9f,
827 };
828
829 static const struct driver_info netgear_fa120_info = {
830         .description = "Netgear FA-120 USB Ethernet",
831         .bind = ax88172_bind,
832         .status = asix_status,
833         .link_reset = ax88172_link_reset,
834         .reset = ax88172_link_reset,
835         .flags =  FLAG_ETHER,
836         .data = 0x00130103,
837 };
838
839 static const struct driver_info hawking_uf200_info = {
840         .description = "Hawking UF200 USB Ethernet",
841         .bind = ax88172_bind,
842         .status = asix_status,
843         .link_reset = ax88172_link_reset,
844         .reset = ax88172_link_reset,
845         .flags =  FLAG_ETHER,
846         .data = 0x001f1d1f,
847 };
848
849 static const struct driver_info ax88772_info = {
850         .description = "ASIX AX88772 USB 2.0 Ethernet",
851         .bind = ax88772_bind,
852         .status = asix_status,
853         .link_reset = ax88772_link_reset,
854         .reset = ax88772_link_reset,
855         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
856         .rx_fixup = ax88772_rx_fixup,
857         .tx_fixup = ax88772_tx_fixup,
858 };
859
860 static const struct usb_device_id       products [] = {
861 {
862         // Linksys USB200M
863         USB_DEVICE (0x077b, 0x2226),
864         .driver_info =  (unsigned long) &ax8817x_info,
865 }, {
866         // Netgear FA120
867         USB_DEVICE (0x0846, 0x1040),
868         .driver_info =  (unsigned long) &netgear_fa120_info,
869 }, {
870         // DLink DUB-E100
871         USB_DEVICE (0x2001, 0x1a00),
872         .driver_info =  (unsigned long) &dlink_dub_e100_info,
873 }, {
874         // Intellinet, ST Lab USB Ethernet
875         USB_DEVICE (0x0b95, 0x1720),
876         .driver_info =  (unsigned long) &ax8817x_info,
877 }, {
878         // Hawking UF200, TrendNet TU2-ET100
879         USB_DEVICE (0x07b8, 0x420a),
880         .driver_info =  (unsigned long) &hawking_uf200_info,
881 }, {
882         // Billionton Systems, USB2AR
883         USB_DEVICE (0x08dd, 0x90ff),
884         .driver_info =  (unsigned long) &ax8817x_info,
885 }, {
886         // ATEN UC210T
887         USB_DEVICE (0x0557, 0x2009),
888         .driver_info =  (unsigned long) &ax8817x_info,
889 }, {
890         // Buffalo LUA-U2-KTX
891         USB_DEVICE (0x0411, 0x003d),
892         .driver_info =  (unsigned long) &ax8817x_info,
893 }, {
894         // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
895         USB_DEVICE (0x6189, 0x182d),
896         .driver_info =  (unsigned long) &ax8817x_info,
897 }, {
898         // corega FEther USB2-TX
899         USB_DEVICE (0x07aa, 0x0017),
900         .driver_info =  (unsigned long) &ax8817x_info,
901 }, {
902         // Surecom EP-1427X-2
903         USB_DEVICE (0x1189, 0x0893),
904         .driver_info = (unsigned long) &ax8817x_info,
905 }, {
906         // goodway corp usb gwusb2e
907         USB_DEVICE (0x1631, 0x6200),
908         .driver_info = (unsigned long) &ax8817x_info,
909 }, {
910         // ASIX AX88772 10/100
911         USB_DEVICE (0x0b95, 0x7720),
912         .driver_info = (unsigned long) &ax88772_info,
913 }, {
914         // ASIX AX88178 10/100/1000
915         USB_DEVICE (0x0b95, 0x1780),
916         .driver_info = (unsigned long) &ax88772_info,
917 }, {
918         // Linksys USB200M Rev 2
919         USB_DEVICE (0x13b1, 0x0018),
920         .driver_info = (unsigned long) &ax88772_info,
921 }, {
922         // 0Q0 cable ethernet
923         USB_DEVICE (0x1557, 0x7720),
924         .driver_info = (unsigned long) &ax88772_info,
925 },
926         { },            // END
927 };
928 MODULE_DEVICE_TABLE(usb, products);
929
930 static struct usb_driver asix_driver = {
931         .name =         "asix",
932         .id_table =     products,
933         .probe =        usbnet_probe,
934         .suspend =      usbnet_suspend,
935         .resume =       usbnet_resume,
936         .disconnect =   usbnet_disconnect,
937 };
938
939 static int __init asix_init(void)
940 {
941         return usb_register(&asix_driver);
942 }
943 module_init(asix_init);
944
945 static void __exit asix_exit(void)
946 {
947         usb_deregister(&asix_driver);
948 }
949 module_exit(asix_exit);
950
951 MODULE_AUTHOR("David Hollis");
952 MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
953 MODULE_LICENSE("GPL");
954