[PATCH] USB: usbnet and zaurus zl-5600
[pandora-kernel.git] / drivers / usb / net / usbnet.c
1 /*
2  * USB Networking Links
3  * Copyright (C) 2000-2003 by David Brownell <dbrownell@users.sourceforge.net>
4  * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
6  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
7  * Copyright (c) 2002-2003 TiVo Inc.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /*
25  * This is a generic "USB networking" framework that works with several
26  * kinds of full and high speed networking devices:
27  *
28  *   + USB host-to-host "network cables", used for IP-over-USB links.
29  *     These are often used for Laplink style connectivity products.
30  *      - AnchorChip 2720
31  *      - Belkin, eTEK (interops with Win32 drivers)
32  *      - GeneSys GL620USB-A
33  *      - NetChip 1080 (interoperates with NetChip Win32 drivers)
34  *      - Prolific PL-2301/2302 (replaces "plusb" driver)
35  *      - KC Technology KC2190
36  *
37  *   + Smart USB devices can support such links directly, using Internet
38  *     standard protocols instead of proprietary host-to-device links.
39  *      - Linux PDAs like iPaq, Yopy, and Zaurus
40  *      - The BLOB boot loader (for diskless booting)
41  *      - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
42  *      - Devices using EPSON's sample USB firmware
43  *      - CDC-Ethernet class devices, such as many cable modems
44  *
45  *   + Adapters to networks such as Ethernet.
46  *      - AX8817X based USB 2.0 products
47  *
48  * Links to these devices can be bridged using Linux Ethernet bridging.
49  * With minor exceptions, these all use similar USB framing for network
50  * traffic, but need different protocols for control traffic.
51  *
52  * USB devices can implement their side of this protocol at the cost
53  * of two bulk endpoints; it's not restricted to "cable" applications.
54  * See the SA1110, Zaurus, or EPSON device/client support in this driver;
55  * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
56  * "g_ether" (in the Linux "gadget" framework) implement that behavior
57  * within devices.
58  *
59  *
60  * CHANGELOG:
61  *
62  * 13-sep-2000  experimental, new
63  * 10-oct-2000  usb_device_id table created. 
64  * 28-oct-2000  misc fixes; mostly, discard more TTL-mangled rx packets.
65  * 01-nov-2000  usb_device_id table and probing api update by
66  *              Adam J. Richter <adam@yggdrasil.com>.
67  * 18-dec-2000  (db) tx watchdog, "net1080" renaming to "usbnet", device_info
68  *              and prolific support, isolate net1080-specific bits, cleanup.
69  *              fix unlink_urbs oops in D3 PM resume code path.
70  *
71  * 02-feb-2001  (db) fix tx skb sharing, packet length, match_flags, ...
72  * 08-feb-2001  stubbed in "linuxdev", maybe the SA-1100 folk can use it;
73  *              AnchorChips 2720 support (from spec) for testing;
74  *              fix bit-ordering problem with ethernet multicast addr
75  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
76  *              updates. Oleg Drokin (green@iXcelerator.com)
77  * 25-mar-2001  More SA-1100 updates, including workaround for ip problem
78  *              expecting cleared skb->cb and framing change to match latest
79  *              handhelds.org version (Oleg).  Enable device IDs from the
80  *              Win32 Belkin driver; other cleanups (db).
81  * 16-jul-2001  Bugfixes for uhci oops-on-unplug, Belkin support, various
82  *              cleanups for problems not yet seen in the field. (db)
83  * 17-oct-2001  Handle "Advance USBNET" product, like Belkin/eTEK devices,
84  *              from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
85  *              rx unlinks somehow weren't async; minor cleanup.
86  * 03-nov-2001  Merged GeneSys driver; original code from Jiun-Jie Huang
87  *              <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
88  *              <utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
89  *              tie mostly to (sub)driver info.  Workaround some PL-2302
90  *              chips that seem to reject SET_INTERFACE requests.
91  *
92  * 06-apr-2002  Added ethtool support, based on a patch from Brad Hards.
93  *              Level of diagnostics is more configurable; they use device
94  *              location (usb_device->devpath) instead of address (2.5).
95  *              For tx_fixup, memflags can't be NOIO.
96  * 07-may-2002  Generalize/cleanup keventd support, handling rx stalls (mostly
97  *              for USB 2.0 TTs) and memory shortages (potential) too. (db)
98  *              Use "locally assigned" IEEE802 address space. (Brad Hards)
99  * 18-oct-2002  Support for Zaurus (Pavel Machek), related cleanup (db).
100  * 14-dec-2002  Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
101  *              cleanups and stubbed PXA-250 support (db), fix for framing
102  *              issues on Z, net1080, and gl620a (Toby Milne)
103  *
104  * 31-mar-2003  Use endpoint descriptors:  high speed support, simpler sa1100
105  *              vs pxa25x, and CDC Ethernet.  Throttle down log floods on
106  *              disconnect; other cleanups. (db)  Flush net1080 fifos
107  *              after several sequential framing errors. (Johannes Erdfelt)
108  * 22-aug-2003  AX8817X support (Dave Hollis).
109  * 14-jun-2004  Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
110  *              (Neil Bortnak)
111  * 03-nov-2004  Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
112  *
113  * 01-feb-2005  AX88772 support (Phil Chang & Dave Hollis)
114  *-------------------------------------------------------------------------*/
115
116 // #define      DEBUG                   // error path messages, extra info
117 // #define      VERBOSE                 // more; success messages
118
119 #include <linux/config.h>
120 #ifdef  CONFIG_USB_DEBUG
121 #   define DEBUG
122 #endif
123 #include <linux/module.h>
124 #include <linux/kmod.h>
125 #include <linux/sched.h>
126 #include <linux/init.h>
127 #include <linux/netdevice.h>
128 #include <linux/etherdevice.h>
129 #include <linux/random.h>
130 #include <linux/ethtool.h>
131 #include <linux/workqueue.h>
132 #include <linux/mii.h>
133 #include <asm/uaccess.h>
134 #include <asm/unaligned.h>
135 #include <linux/usb.h>
136 #include <asm/io.h>
137 #include <asm/scatterlist.h>
138 #include <linux/mm.h>
139 #include <linux/dma-mapping.h>
140
141 #define DRIVER_VERSION          "03-Nov-2004"
142
143
144 /*-------------------------------------------------------------------------*/
145
146 /*
147  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
148  * Several dozen bytes of IPv4 data can fit in two such transactions.
149  * One maximum size Ethernet packet takes twenty four of them.
150  * For high speed, each frame comfortably fits almost 36 max size
151  * Ethernet packets (so queues should be bigger).
152  */
153 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
154 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
155
156 // packets are always ethernet inside
157 // ... except they can be bigger (limit of 64K with NetChip framing)
158 #define MIN_PACKET      sizeof(struct ethhdr)
159 #define MAX_PACKET      32768
160
161 // reawaken network queue this soon after stopping; else watchdog barks
162 #define TX_TIMEOUT_JIFFIES      (5*HZ)
163
164 // throttle rx/tx briefly after some faults, so khubd might disconnect()
165 // us (it polls at HZ/4 usually) before we report too many false errors.
166 #define THROTTLE_JIFFIES        (HZ/8)
167
168 // for vendor-specific control operations
169 #define CONTROL_TIMEOUT_MS      500
170
171 // between wakeups
172 #define UNLINK_TIMEOUT_MS       3
173
174 /*-------------------------------------------------------------------------*/
175
176 // randomly generated ethernet address
177 static u8       node_id [ETH_ALEN];
178
179 // state we keep for each device we handle
180 struct usbnet {
181         // housekeeping
182         struct usb_device       *udev;
183         struct driver_info      *driver_info;
184         wait_queue_head_t       *wait;
185
186         // i/o info: pipes etc
187         unsigned                in, out;
188         struct usb_host_endpoint *status;
189         unsigned                maxpacket;
190         struct timer_list       delay;
191
192         // protocol/interface state
193         struct net_device       *net;
194         struct net_device_stats stats;
195         int                     msg_enable;
196         unsigned long           data [5];
197
198         struct mii_if_info      mii;
199
200         // various kinds of pending driver work
201         struct sk_buff_head     rxq;
202         struct sk_buff_head     txq;
203         struct sk_buff_head     done;
204         struct urb              *interrupt;
205         struct tasklet_struct   bh;
206
207         struct work_struct      kevent;
208         unsigned long           flags;
209 #               define EVENT_TX_HALT    0
210 #               define EVENT_RX_HALT    1
211 #               define EVENT_RX_MEMORY  2
212 #               define EVENT_STS_SPLIT  3
213 };
214
215 // device-specific info used by the driver
216 struct driver_info {
217         char            *description;
218
219         int             flags;
220 /* framing is CDC Ethernet, not writing ZLPs (hw issues), or optionally: */
221 #define FLAG_FRAMING_NC 0x0001          /* guard against device dropouts */ 
222 #define FLAG_FRAMING_GL 0x0002          /* genelink batches packets */
223 #define FLAG_FRAMING_Z  0x0004          /* zaurus adds a trailer */
224 #define FLAG_FRAMING_RN 0x0008          /* RNDIS batches, plus huge header */
225
226 #define FLAG_NO_SETINT  0x0010          /* device can't set_interface() */
227 #define FLAG_ETHER      0x0020          /* maybe use "eth%d" names */
228
229 #define FLAG_FRAMING_AX 0x0040          /* AX88772/178 packets */
230
231         /* init device ... can sleep, or cause probe() failure */
232         int     (*bind)(struct usbnet *, struct usb_interface *);
233
234         /* cleanup device ... can sleep, but can't fail */
235         void    (*unbind)(struct usbnet *, struct usb_interface *);
236
237         /* reset device ... can sleep */
238         int     (*reset)(struct usbnet *);
239
240         /* see if peer is connected ... can sleep */
241         int     (*check_connect)(struct usbnet *);
242
243         /* for status polling */
244         void    (*status)(struct usbnet *, struct urb *);
245
246         /* fixup rx packet (strip framing) */
247         int     (*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
248
249         /* fixup tx packet (add framing) */
250         struct sk_buff  *(*tx_fixup)(struct usbnet *dev,
251                                 struct sk_buff *skb, int flags);
252
253         // FIXME -- also an interrupt mechanism
254         // useful for at least PL2301/2302 and GL620USB-A
255         // and CDC use them to report 'is it connected' changes
256
257         /* for new devices, use the descriptor-reading code instead */
258         int             in;             /* rx endpoint */
259         int             out;            /* tx endpoint */
260
261         unsigned long   data;           /* Misc driver specific data */
262 };
263
264 // we record the state for each of our queued skbs
265 enum skb_state {
266         illegal = 0,
267         tx_start, tx_done,
268         rx_start, rx_done, rx_cleanup
269 };
270
271 struct skb_data {       // skb->cb is one of these
272         struct urb              *urb;
273         struct usbnet           *dev;
274         enum skb_state          state;
275         size_t                  length;
276 };
277
278 static const char driver_name [] = "usbnet";
279
280 /* use ethtool to change the level for any given device */
281 static int msg_level = -1;
282 module_param (msg_level, int, 0);
283 MODULE_PARM_DESC (msg_level, "Override default message level");
284
285
286 #ifdef DEBUG
287 #define devdbg(usbnet, fmt, arg...) \
288         printk(KERN_DEBUG "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
289 #else
290 #define devdbg(usbnet, fmt, arg...) do {} while(0)
291 #endif
292
293 #define deverr(usbnet, fmt, arg...) \
294         printk(KERN_ERR "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
295 #define devwarn(usbnet, fmt, arg...) \
296         printk(KERN_WARNING "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
297
298 #define devinfo(usbnet, fmt, arg...) \
299         printk(KERN_INFO "%s: " fmt "\n" , (usbnet)->net->name , ## arg); \
300
301 /*-------------------------------------------------------------------------*/
302
303 static void usbnet_get_drvinfo (struct net_device *, struct ethtool_drvinfo *);
304 static u32 usbnet_get_link (struct net_device *);
305 static u32 usbnet_get_msglevel (struct net_device *);
306 static void usbnet_set_msglevel (struct net_device *, u32);
307
308 /* mostly for PDA style devices, which are always connected if present */
309 static int always_connected (struct usbnet *dev)
310 {
311         return 0;
312 }
313
314 /* handles CDC Ethernet and many other network "bulk data" interfaces */
315 static int
316 get_endpoints (struct usbnet *dev, struct usb_interface *intf)
317 {
318         int                             tmp;
319         struct usb_host_interface       *alt = NULL;
320         struct usb_host_endpoint        *in = NULL, *out = NULL;
321         struct usb_host_endpoint        *status = NULL;
322
323         for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
324                 unsigned        ep;
325
326                 in = out = status = NULL;
327                 alt = intf->altsetting + tmp;
328
329                 /* take the first altsetting with in-bulk + out-bulk;
330                  * remember any status endpoint, just in case;
331                  * ignore other endpoints and altsetttings.
332                  */
333                 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
334                         struct usb_host_endpoint        *e;
335                         int                             intr = 0;
336
337                         e = alt->endpoint + ep;
338                         switch (e->desc.bmAttributes) {
339                         case USB_ENDPOINT_XFER_INT:
340                                 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
341                                         continue;
342                                 intr = 1;
343                                 /* FALLTHROUGH */
344                         case USB_ENDPOINT_XFER_BULK:
345                                 break;
346                         default:
347                                 continue;
348                         }
349                         if (e->desc.bEndpointAddress & USB_DIR_IN) {
350                                 if (!intr && !in)
351                                         in = e;
352                                 else if (intr && !status)
353                                         status = e;
354                         } else {
355                                 if (!out)
356                                         out = e;
357                         }
358                 }
359                 if (in && out)
360                         break;
361         }
362         if (!alt || !in || !out)
363                 return -EINVAL;
364
365         if (alt->desc.bAlternateSetting != 0
366                         || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
367                 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
368                                 alt->desc.bAlternateSetting);
369                 if (tmp < 0)
370                         return tmp;
371         }
372         
373         dev->in = usb_rcvbulkpipe (dev->udev,
374                         in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
375         dev->out = usb_sndbulkpipe (dev->udev,
376                         out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
377         dev->status = status;
378         return 0;
379 }
380
381 static void intr_complete (struct urb *urb, struct pt_regs *regs);
382
383 static int init_status (struct usbnet *dev, struct usb_interface *intf)
384 {
385         char            *buf = NULL;
386         unsigned        pipe = 0;
387         unsigned        maxp;
388         unsigned        period;
389
390         if (!dev->driver_info->status)
391                 return 0;
392
393         pipe = usb_rcvintpipe (dev->udev,
394                         dev->status->desc.bEndpointAddress
395                                 & USB_ENDPOINT_NUMBER_MASK);
396         maxp = usb_maxpacket (dev->udev, pipe, 0);
397
398         /* avoid 1 msec chatter:  min 8 msec poll rate */
399         period = max ((int) dev->status->desc.bInterval,
400                 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
401
402         buf = kmalloc (maxp, SLAB_KERNEL);
403         if (buf) {
404                 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
405                 if (!dev->interrupt) {
406                         kfree (buf);
407                         return -ENOMEM;
408                 } else {
409                         usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
410                                 buf, maxp, intr_complete, dev, period);
411                         dev_dbg(&intf->dev,
412                                 "status ep%din, %d bytes period %d\n",
413                                 usb_pipeendpoint(pipe), maxp, period);
414                 }
415         }
416         return  0;
417 }
418
419 static void skb_return (struct usbnet *dev, struct sk_buff *skb)
420 {
421         int     status;
422
423         skb->dev = dev->net;
424         skb->protocol = eth_type_trans (skb, dev->net);
425         dev->stats.rx_packets++;
426         dev->stats.rx_bytes += skb->len;
427
428         if (netif_msg_rx_status (dev))
429                 devdbg (dev, "< rx, len %zd, type 0x%x",
430                         skb->len + sizeof (struct ethhdr), skb->protocol);
431         memset (skb->cb, 0, sizeof (struct skb_data));
432         status = netif_rx (skb);
433         if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
434                 devdbg (dev, "netif_rx status %d", status);
435 }
436
437 \f
438 #ifdef  CONFIG_USB_ALI_M5632
439 #define HAVE_HARDWARE
440
441 /*-------------------------------------------------------------------------
442  *
443  * ALi M5632 driver ... does high speed
444  *
445  *-------------------------------------------------------------------------*/
446
447 static const struct driver_info ali_m5632_info = {
448         .description =  "ALi M5632",
449 };
450
451
452 #endif
453
454 \f
455 #ifdef  CONFIG_USB_AN2720
456 #define HAVE_HARDWARE
457
458 /*-------------------------------------------------------------------------
459  *
460  * AnchorChips 2720 driver ... http://www.cypress.com
461  *
462  * This doesn't seem to have a way to detect whether the peer is
463  * connected, or need any reset handshaking.  It's got pretty big
464  * internal buffers (handles most of a frame's worth of data).
465  * Chip data sheets don't describe any vendor control messages.
466  *
467  *-------------------------------------------------------------------------*/
468
469 static const struct driver_info an2720_info = {
470         .description =  "AnchorChips/Cypress 2720",
471         // no reset available!
472         // no check_connect available!
473
474         .in = 2, .out = 2,              // direction distinguishes these
475 };
476
477 #endif  /* CONFIG_USB_AN2720 */
478
479 \f
480 #ifdef CONFIG_USB_AX8817X
481 /* ASIX AX8817X based USB 2.0 Ethernet Devices */
482
483 #define HAVE_HARDWARE
484 #define NEED_MII
485
486 #include <linux/crc32.h>
487
488 #define AX_CMD_SET_SW_MII               0x06
489 #define AX_CMD_READ_MII_REG             0x07
490 #define AX_CMD_WRITE_MII_REG            0x08
491 #define AX_CMD_SET_HW_MII               0x0a
492 #define AX_CMD_READ_EEPROM              0x0b
493 #define AX_CMD_WRITE_EEPROM             0x0c
494 #define AX_CMD_WRITE_ENABLE             0x0d
495 #define AX_CMD_WRITE_DISABLE            0x0e
496 #define AX_CMD_WRITE_RX_CTL             0x10
497 #define AX_CMD_READ_IPG012              0x11
498 #define AX_CMD_WRITE_IPG0               0x12
499 #define AX_CMD_WRITE_IPG1               0x13
500 #define AX_CMD_WRITE_IPG2               0x14
501 #define AX_CMD_WRITE_MULTI_FILTER       0x16
502 #define AX_CMD_READ_NODE_ID             0x17
503 #define AX_CMD_READ_PHY_ID              0x19
504 #define AX_CMD_WRITE_MEDIUM_MODE        0x1b
505 #define AX_CMD_READ_MONITOR_MODE        0x1c
506 #define AX_CMD_WRITE_MONITOR_MODE       0x1d
507 #define AX_CMD_WRITE_GPIOS              0x1f
508 #define AX_CMD_SW_RESET                 0x20
509 #define AX_CMD_SW_PHY_STATUS            0x21
510 #define AX_CMD_SW_PHY_SELECT            0x22
511 #define AX88772_CMD_READ_NODE_ID        0x13
512
513 #define AX_MONITOR_MODE                 0x01
514 #define AX_MONITOR_LINK                 0x02
515 #define AX_MONITOR_MAGIC                0x04
516 #define AX_MONITOR_HSFS                 0x10
517
518 #define AX_MCAST_FILTER_SIZE            8
519 #define AX_MAX_MCAST                    64
520
521 #define AX_INTERRUPT_BUFSIZE            8
522
523 #define AX_EEPROM_LEN                   0x40
524
525 #define AX_SWRESET_CLEAR                0x00
526 #define AX_SWRESET_RR                   0x01
527 #define AX_SWRESET_RT                   0x02
528 #define AX_SWRESET_PRTE                 0x04
529 #define AX_SWRESET_PRL                  0x08
530 #define AX_SWRESET_BZ                   0x10
531 #define AX_SWRESET_IPRL                 0x20
532 #define AX_SWRESET_IPPD                 0x40
533
534 #define AX88772_IPG0_DEFAULT            0x15
535 #define AX88772_IPG1_DEFAULT            0x0c
536 #define AX88772_IPG2_DEFAULT            0x12
537
538 #define AX_EEPROM_MAGIC                 0xdeadbeef
539
540 /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
541 struct ax8817x_data {
542         u8 multi_filter[AX_MCAST_FILTER_SIZE];
543         struct urb *int_urb;
544         u8 *int_buf;
545 };
546
547 static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
548                             u16 size, void *data)
549 {
550         return usb_control_msg(
551                 dev->udev,
552                 usb_rcvctrlpipe(dev->udev, 0),
553                 cmd,
554                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
555                 value,
556                 index,
557                 data,
558                 size,
559                 CONTROL_TIMEOUT_MS);
560 }
561
562 static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
563                              u16 size, void *data)
564 {
565         return usb_control_msg(
566                 dev->udev,
567                 usb_sndctrlpipe(dev->udev, 0),
568                 cmd,
569                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
570                 value,
571                 index,
572                 data,
573                 size,
574                 CONTROL_TIMEOUT_MS);
575 }
576
577 static void ax8817x_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
578 {
579         struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
580
581         if (urb->status < 0)
582                 printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d",
583                         urb->status);
584
585         kfree(req);
586         usb_free_urb(urb);
587 }
588
589 static void ax8817x_interrupt_complete(struct urb *urb, struct pt_regs *regs)
590 {
591         struct usbnet *dev = (struct usbnet *)urb->context;
592         struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
593         int link;
594
595         if (urb->status < 0) {
596                 devdbg(dev,"ax8817x_interrupt_complete() failed with %d",
597                         urb->status);
598         } else {
599                 link = data->int_buf[2] & 0x01;
600                 if (netif_carrier_ok(dev->net) != link) {
601                         if (link)
602                                 netif_carrier_on(dev->net);
603                         else
604                                 netif_carrier_off(dev->net);
605                         devdbg(dev, "ax8817x - Link Status is: %d", link);
606                 }
607                 usb_submit_urb(data->int_urb, GFP_ATOMIC);
608         }
609 }
610
611 static void ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
612                                     u16 size, void *data)
613 {
614         struct usb_ctrlrequest *req;
615         int status;
616         struct urb *urb;
617
618         if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
619                 devdbg(dev, "Error allocating URB in write_cmd_async!");
620                 return;
621         }
622
623         if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
624                 deverr(dev, "Failed to allocate memory for control request");
625                 usb_free_urb(urb);
626                 return;
627         }
628
629         req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
630         req->bRequest = cmd;
631         req->wValue = cpu_to_le16(value);
632         req->wIndex = cpu_to_le16(index); 
633         req->wLength = cpu_to_le16(size);
634
635         usb_fill_control_urb(urb, dev->udev,
636                              usb_sndctrlpipe(dev->udev, 0),
637                              (void *)req, data, size,
638                              ax8817x_async_cmd_callback, req);
639
640         if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
641                 deverr(dev, "Error submitting the control message: status=%d", status);
642                 kfree(req);
643                 usb_free_urb(urb);
644         }
645 }
646
647 static void ax8817x_set_multicast(struct net_device *net)
648 {
649         struct usbnet *dev = netdev_priv(net);
650         struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
651         u8 rx_ctl = 0x8c;
652
653         if (net->flags & IFF_PROMISC) {
654                 rx_ctl |= 0x01;
655         } else if (net->flags & IFF_ALLMULTI
656                    || net->mc_count > AX_MAX_MCAST) {
657                 rx_ctl |= 0x02;
658         } else if (net->mc_count == 0) {
659                 /* just broadcast and directed */
660         } else {
661                 /* We use the 20 byte dev->data
662                  * for our 8 byte filter buffer
663                  * to avoid allocating memory that
664                  * is tricky to free later */
665                 struct dev_mc_list *mc_list = net->mc_list;
666                 u32 crc_bits;
667                 int i;
668
669                 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
670
671                 /* Build the multicast hash filter. */
672                 for (i = 0; i < net->mc_count; i++) {
673                         crc_bits =
674                             ether_crc(ETH_ALEN,
675                                       mc_list->dmi_addr) >> 26;
676                         data->multi_filter[crc_bits >> 3] |=
677                             1 << (crc_bits & 7);
678                         mc_list = mc_list->next;
679                 }
680
681                 ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
682                                    AX_MCAST_FILTER_SIZE, data->multi_filter);
683
684                 rx_ctl |= 0x10;
685         }
686
687         ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
688 }
689
690 static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc)
691 {
692         struct usbnet *dev = netdev_priv(netdev);
693         u16 res;
694         u8 buf[1];
695
696         ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
697         ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
698         ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
699
700         return res & 0xffff;
701 }
702
703 static void ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
704 {
705         struct usbnet *dev = netdev_priv(netdev);
706         u16 res = val;
707         u8 buf[1];
708
709         ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
710         ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
711         ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
712 }
713
714 static void ax8817x_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
715 {
716         struct usbnet *dev = netdev_priv(net);
717         u8 opt;
718
719         if (ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
720                 wolinfo->supported = 0;
721                 wolinfo->wolopts = 0;
722                 return;
723         }
724         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
725         wolinfo->wolopts = 0;
726         if (opt & AX_MONITOR_MODE) {
727                 if (opt & AX_MONITOR_LINK)
728                         wolinfo->wolopts |= WAKE_PHY;
729                 if (opt & AX_MONITOR_MAGIC)
730                         wolinfo->wolopts |= WAKE_MAGIC;
731         }
732 }
733
734 static int ax8817x_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
735 {
736         struct usbnet *dev = netdev_priv(net);
737         u8 opt = 0;
738         u8 buf[1];
739
740         if (wolinfo->wolopts & WAKE_PHY)
741                 opt |= AX_MONITOR_LINK;
742         if (wolinfo->wolopts & WAKE_MAGIC)
743                 opt |= AX_MONITOR_MAGIC;
744         if (opt != 0)
745                 opt |= AX_MONITOR_MODE;
746
747         if (ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
748                               opt, 0, 0, &buf) < 0)
749                 return -EINVAL;
750
751         return 0;
752 }
753
754 static int ax8817x_get_eeprom_len(struct net_device *net)
755 {
756         return AX_EEPROM_LEN;
757 }
758
759 static int ax8817x_get_eeprom(struct net_device *net,
760                               struct ethtool_eeprom *eeprom, u8 *data)
761 {
762         struct usbnet *dev = netdev_priv(net);
763         u16 *ebuf = (u16 *)data;
764         int i;
765
766         /* Crude hack to ensure that we don't overwrite memory
767          * if an odd length is supplied
768          */
769         if (eeprom->len % 2)
770                 return -EINVAL;
771
772         eeprom->magic = AX_EEPROM_MAGIC;
773
774         /* ax8817x returns 2 bytes from eeprom on read */
775         for (i=0; i < eeprom->len / 2; i++) {
776                 if (ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM, 
777                         eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
778                         return -EINVAL;
779         }
780         return 0;
781 }
782
783 static void ax8817x_get_drvinfo (struct net_device *net,
784                                  struct ethtool_drvinfo *info)
785 {
786         /* Inherit standard device info */
787         usbnet_get_drvinfo(net, info);
788         info->eedump_len = 0x3e;
789 }
790
791 static int ax8817x_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
792 {
793         struct usbnet *dev = netdev_priv(net);
794
795         return mii_ethtool_gset(&dev->mii,cmd);
796 }
797
798 static int ax8817x_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
799 {
800         struct usbnet *dev = netdev_priv(net);
801
802         return mii_ethtool_sset(&dev->mii,cmd);
803 }
804
805 /* We need to override some ethtool_ops so we require our
806    own structure so we don't interfere with other usbnet
807    devices that may be connected at the same time. */
808 static struct ethtool_ops ax8817x_ethtool_ops = {
809         .get_drvinfo            = ax8817x_get_drvinfo,
810         .get_link               = ethtool_op_get_link,
811         .get_msglevel           = usbnet_get_msglevel,
812         .set_msglevel           = usbnet_set_msglevel,
813         .get_wol                = ax8817x_get_wol,
814         .set_wol                = ax8817x_set_wol,
815         .get_eeprom_len         = ax8817x_get_eeprom_len,
816         .get_eeprom             = ax8817x_get_eeprom,
817         .get_settings           = ax8817x_get_settings,
818         .set_settings           = ax8817x_set_settings,
819 };
820
821 static int ax8817x_bind(struct usbnet *dev, struct usb_interface *intf)
822 {
823         int ret = 0;
824         void *buf;
825         int i;
826         unsigned long gpio_bits = dev->driver_info->data;
827         struct ax8817x_data *data = (struct ax8817x_data *)dev->data;
828
829         get_endpoints(dev,intf);
830
831         if ((data->int_urb = usb_alloc_urb (0, GFP_KERNEL)) == NULL) {
832                 dbg ("%s: cannot allocate interrupt URB",
833                         dev->net->name);
834                 ret = -ENOMEM;
835                 goto out1;
836         }
837         
838         if ((data->int_buf = kmalloc(AX_INTERRUPT_BUFSIZE, GFP_KERNEL)) == NULL) {
839                 dbg ("%s: cannot allocate memory for interrupt buffer",
840                         dev->net->name);
841                 ret = -ENOMEM;
842                 goto out1;
843         }
844         memset(data->int_buf, 0, AX_INTERRUPT_BUFSIZE);
845
846         usb_fill_int_urb (data->int_urb, dev->udev,
847                 usb_rcvintpipe (dev->udev, 1),
848                 data->int_buf, AX_INTERRUPT_BUFSIZE,
849                 ax8817x_interrupt_complete, dev,
850                 dev->udev->speed == USB_SPEED_HIGH ? 8 : 100);
851
852         buf = kmalloc(ETH_ALEN, GFP_KERNEL);
853         if(!buf) {
854                 ret = -ENOMEM;
855                 goto out2;
856         }
857
858         /* Toggle the GPIOs in a manufacturer/model specific way */
859         for (i = 2; i >= 0; i--) {
860                 if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
861                                         (gpio_bits >> (i * 8)) & 0xff, 0, 0,
862                                         buf)) < 0)
863                         goto out3;
864                 msleep(5);
865         }
866
867         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf)) < 0) {
868                 dbg("send AX_CMD_WRITE_RX_CTL failed: %d", ret);
869                 goto out3;
870         }
871
872         /* Get the MAC address */
873         memset(buf, 0, ETH_ALEN);
874         if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, 6, buf)) < 0) {
875                 dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
876                 goto out3;
877         }
878         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
879
880         /* Get the PHY id */
881         if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
882                 dbg("error on read AX_CMD_READ_PHY_ID: %02x", ret);
883                 goto out3;
884         } else if (ret < 2) {
885                 /* this should always return 2 bytes */
886                 dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x", ret);
887                 ret = -EIO;
888                 goto out3;
889         }
890
891         /* Initialize MII structure */
892         dev->mii.dev = dev->net;
893         dev->mii.mdio_read = ax8817x_mdio_read;
894         dev->mii.mdio_write = ax8817x_mdio_write;
895         dev->mii.phy_id_mask = 0x3f;
896         dev->mii.reg_num_mask = 0x1f;
897         dev->mii.phy_id = *((u8 *)buf + 1);
898
899         dev->net->set_multicast_list = ax8817x_set_multicast;
900         dev->net->ethtool_ops = &ax8817x_ethtool_ops;
901
902         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
903                         cpu_to_le16(BMCR_RESET));
904         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
905                         cpu_to_le16(ADVERTISE_ALL | ADVERTISE_CSMA | 0x0400));
906         mii_nway_restart(&dev->mii);
907
908         if((ret = usb_submit_urb(data->int_urb, GFP_KERNEL)) < 0) {
909                 dbg("Failed to submit interrupt URB: %02x", ret);
910                 goto out2;
911         }
912
913         return 0;
914 out3:
915         kfree(buf);
916 out2:
917         kfree(data->int_buf);
918 out1:
919         usb_free_urb(data->int_urb);
920         return ret;
921 }
922
923 static void ax8817x_unbind(struct usbnet *dev, struct usb_interface *intf)
924 {
925         struct ax8817x_data *data = (struct ax8817x_data *)dev->data;
926
927         usb_kill_urb(data->int_urb);
928         usb_free_urb(data->int_urb);
929         kfree(data->int_buf);
930 }
931
932 static struct ethtool_ops ax88772_ethtool_ops = {
933         .get_drvinfo            = ax8817x_get_drvinfo,
934         .get_link               = ethtool_op_get_link,
935         .get_msglevel           = usbnet_get_msglevel,
936         .set_msglevel           = usbnet_set_msglevel,
937         .get_wol                = ax8817x_get_wol,
938         .set_wol                = ax8817x_set_wol,
939         .get_eeprom_len         = ax8817x_get_eeprom_len,
940         .get_eeprom             = ax8817x_get_eeprom,
941         .get_settings           = ax8817x_get_settings,
942         .set_settings           = ax8817x_set_settings,
943 };
944
945 static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
946 {
947         int ret;
948         void *buf;
949         struct ax8817x_data *data = (struct ax8817x_data *)dev->data;
950
951         get_endpoints(dev,intf);
952
953         if ((data->int_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
954                 dbg ("Cannot allocate interrupt URB");
955                 ret = -ENOMEM;
956                 goto out1;
957         }
958         
959         if ((data->int_buf = kmalloc(AX_INTERRUPT_BUFSIZE, GFP_KERNEL)) == NULL) {
960                 dbg ("Cannot allocate memory for interrupt buffer");
961                 ret = -ENOMEM;
962                 goto out1;
963         }
964         memset(data->int_buf, 0, AX_INTERRUPT_BUFSIZE);
965
966         usb_fill_int_urb (data->int_urb, dev->udev,
967                 usb_rcvintpipe (dev->udev, 1),
968                 data->int_buf, AX_INTERRUPT_BUFSIZE,
969                 ax8817x_interrupt_complete, dev,
970                 dev->udev->speed == USB_SPEED_HIGH ? 8 : 100);
971
972         buf = kmalloc(6, GFP_KERNEL);
973         if(!buf) {
974                 dbg ("Cannot allocate memory for buffer");
975                 ret = -ENOMEM;
976                 goto out2;
977         }
978
979         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
980                                      0x00B0, 0, 0, buf)) < 0)
981                 goto out3;
982
983         msleep(5);
984         if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0x0001, 0, 0, buf)) < 0) {
985                 dbg("Select PHY #1 failed: %d", ret);
986                 goto out3;
987         }
988
989         if ((ret =
990              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPPD, 0, 0, buf)) < 0) {
991                 dbg("Failed to power down internal PHY: %d", ret);
992                 goto out3;
993         }
994
995         msleep(150);
996         if ((ret =
997              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_CLEAR, 0, 0, buf)) < 0) {
998                 dbg("Failed to perform software reset: %d", ret);
999                 goto out3;
1000         }
1001
1002         msleep(150);
1003         if ((ret =
1004              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) {
1005                 dbg("Failed to set Internal/External PHY reset control: %d", ret);
1006                 goto out3;
1007         }
1008
1009         msleep(150);
1010         if ((ret =
1011              ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0000, 0, 0,
1012                                buf)) < 0) {
1013                 dbg("Failed to reset RX_CTL: %d", ret);
1014                 goto out3;
1015         }
1016
1017         /* Get the MAC address */
1018         memset(buf, 0, ETH_ALEN);
1019         if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)) < 0) {
1020                 dbg("Failed to read MAC address: %d", ret);
1021                 goto out3;
1022         }
1023         memcpy(dev->net->dev_addr, buf, ETH_ALEN);
1024
1025         if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, buf)) < 0) {
1026                 dbg("Enabling software MII failed: %d", ret);
1027                 goto out3;
1028         }
1029
1030         if (((ret =
1031               ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, 0x0010, 2, 2, buf)) < 0)
1032             || (*((u16 *)buf) != 0x003b)) {
1033                 dbg("Read PHY register 2 must be 0x3b00: %d", ret);
1034                 goto out3;
1035         }
1036
1037         /* Initialize MII structure */
1038         dev->mii.dev = dev->net;
1039         dev->mii.mdio_read = ax8817x_mdio_read;
1040         dev->mii.mdio_write = ax8817x_mdio_write;
1041         dev->mii.phy_id_mask = 0xff;
1042         dev->mii.reg_num_mask = 0xff;
1043
1044         /* Get the PHY id */
1045         if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
1046                 dbg("Error reading PHY ID: %02x", ret);
1047                 goto out3;
1048         } else if (ret < 2) {
1049                 /* this should always return 2 bytes */
1050                 dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x",
1051                     ret);
1052                 ret = -EIO;
1053                 goto out3;
1054         }
1055         dev->mii.phy_id = *((u8 *)buf + 1);
1056
1057         if ((ret =
1058              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_PRL, 0, 0, buf)) < 0) {
1059                 dbg("Set external PHY reset pin level: %d", ret);
1060                 goto out3;
1061         }
1062         msleep(150);
1063         if ((ret =
1064              ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) {
1065                 dbg("Set Internal/External PHY reset control: %d", ret);
1066                 goto out3;
1067         }
1068         msleep(150);
1069
1070
1071         dev->net->set_multicast_list = ax8817x_set_multicast;
1072         dev->net->ethtool_ops = &ax88772_ethtool_ops;
1073
1074         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR,
1075                         cpu_to_le16(BMCR_RESET));
1076         ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1077                         cpu_to_le16(ADVERTISE_ALL | ADVERTISE_CSMA));
1078         mii_nway_restart(&dev->mii);
1079
1080         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 0x0336, 0, 0, buf)) < 0) {
1081                 dbg("Write medium mode register: %d", ret);
1082                 goto out3;
1083         }
1084
1085         if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0, AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,AX88772_IPG2_DEFAULT, 0, buf)) < 0) {
1086                 dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
1087                 goto out3;
1088         }
1089         if ((ret =
1090              ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf)) < 0) {
1091                 dbg("Failed to set hardware MII: %02x", ret);
1092                 goto out3;
1093         }
1094
1095         /* Set RX_CTL to default values with 2k buffer, and enable cactus */
1096         if ((ret =
1097              ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0088, 0, 0,
1098                                buf)) < 0) {
1099                 dbg("Reset RX_CTL failed: %d", ret);
1100                 goto out3;
1101         }
1102
1103         if((ret = usb_submit_urb(data->int_urb, GFP_KERNEL)) < 0) {
1104                 dbg("Failed to submit interrupt URB: %02x", ret);
1105                 goto out3;
1106         }
1107
1108         kfree(buf);
1109
1110         return 0;
1111
1112 out3:
1113         kfree(buf);
1114 out2:
1115         kfree(data->int_buf);
1116 out1:
1117         usb_free_urb(data->int_urb);
1118
1119         return ret;
1120 }
1121
1122 static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1123 {
1124         u32 *header;
1125         char *packet;
1126         struct sk_buff *ax_skb;
1127         u16 size;
1128
1129         header = (u32 *) skb->data;
1130         le32_to_cpus(header);
1131         packet = (char *)(header + 1);
1132
1133         skb_pull(skb, 4);
1134
1135         while (skb->len > 0) {
1136                 if ((short)(*header & 0x0000ffff) !=
1137                     ~((short)((*header & 0xffff0000) >> 16))) {
1138                         devdbg(dev,"header length data is error");
1139                 }
1140                 /* get the packet length */
1141                 size = (u16) (*header & 0x0000ffff);
1142
1143                 if ((skb->len) - ((size + 1) & 0xfffe) == 0)
1144                         return 2;
1145                 if (size > ETH_FRAME_LEN) {
1146                         devdbg(dev,"invalid rx length %d", size);
1147                         return 0;
1148                 }
1149                 ax_skb = skb_clone(skb, GFP_ATOMIC);
1150                 if (ax_skb) {
1151                         ax_skb->len = size;
1152                         ax_skb->data = packet;
1153                         ax_skb->tail = packet + size;
1154                         skb_return(dev, ax_skb);
1155                 } else {
1156                         return 0;
1157                 }
1158
1159                 skb_pull(skb, (size + 1) & 0xfffe);
1160
1161                 if (skb->len == 0)
1162                         break;
1163
1164                 header = (u32 *) skb->data;
1165                 le32_to_cpus(header);
1166                 packet = (char *)(header + 1);
1167                 skb_pull(skb, 4);
1168         }
1169
1170         if (skb->len < 0) {
1171                 devdbg(dev,"invalid rx length %d", skb->len);
1172                 return 0;
1173         }
1174         return 1;
1175 }
1176
1177 static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
1178                                         int flags)
1179 {
1180         int padlen;
1181         int headroom = skb_headroom(skb);
1182         int tailroom = skb_tailroom(skb);
1183         u32 *packet_len;
1184         u32 *padbytes_ptr;
1185
1186         padlen = ((skb->len + 4) % 512) ? 0 : 4;
1187
1188         if ((!skb_cloned(skb))
1189             && ((headroom + tailroom) >= (4 + padlen))) {
1190                 if ((headroom < 4) || (tailroom < padlen)) {
1191                         skb->data = memmove(skb->head + 4, skb->data, skb->len);
1192                         skb->tail = skb->data + skb->len;
1193                 }
1194         } else {
1195                 struct sk_buff *skb2;
1196                 skb2 = skb_copy_expand(skb, 4, padlen, flags);
1197                 dev_kfree_skb_any(skb);
1198                 skb = skb2;
1199                 if (!skb)
1200                         return NULL;
1201         }
1202
1203         packet_len = (u32 *) skb_push(skb, 4);
1204
1205         packet_len = (u32 *) skb->data;
1206         *packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
1207
1208         if ((skb->len % 512) == 0) {
1209                 padbytes_ptr = (u32 *) skb->tail;
1210                 *padbytes_ptr = 0xffff0000;
1211                 skb_put(skb, padlen);
1212         }
1213         return skb;
1214 }
1215
1216 static const struct driver_info ax8817x_info = {
1217         .description = "ASIX AX8817x USB 2.0 Ethernet",
1218         .bind = ax8817x_bind,
1219         .unbind = ax8817x_unbind,
1220         .flags =  FLAG_ETHER,
1221         .data = 0x00130103,
1222 };
1223
1224 static const struct driver_info dlink_dub_e100_info = {
1225         .description = "DLink DUB-E100 USB Ethernet",
1226         .bind = ax8817x_bind,
1227         .unbind = ax8817x_unbind,
1228         .flags =  FLAG_ETHER,
1229         .data = 0x009f9d9f,
1230 };
1231
1232 static const struct driver_info netgear_fa120_info = {
1233         .description = "Netgear FA-120 USB Ethernet",
1234         .bind = ax8817x_bind,
1235         .unbind = ax8817x_unbind,
1236         .flags =  FLAG_ETHER,
1237         .data = 0x00130103,
1238 };
1239
1240 static const struct driver_info hawking_uf200_info = {
1241         .description = "Hawking UF200 USB Ethernet",
1242         .bind = ax8817x_bind,
1243         .unbind = ax8817x_unbind,
1244         .flags =  FLAG_ETHER,
1245         .data = 0x001f1d1f,
1246 };
1247
1248 static const struct driver_info ax88772_info = {
1249         .description = "ASIX AX88772 USB 2.0 Ethernet",
1250         .bind = ax88772_bind,
1251         .unbind = ax8817x_unbind,
1252         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1253         .rx_fixup = ax88772_rx_fixup,
1254         .tx_fixup = ax88772_tx_fixup,
1255         .data = 0x00130103,
1256 };
1257
1258 #endif /* CONFIG_USB_AX8817X */
1259
1260
1261 \f
1262 #ifdef  CONFIG_USB_BELKIN
1263 #define HAVE_HARDWARE
1264
1265 /*-------------------------------------------------------------------------
1266  *
1267  * Belkin F5U104 ... two NetChip 2280 devices + Atmel microcontroller
1268  *
1269  * ... also two eTEK designs, including one sold as "Advance USBNET"
1270  *
1271  *-------------------------------------------------------------------------*/
1272
1273 static const struct driver_info belkin_info = {
1274         .description =  "Belkin, eTEK, or compatible",
1275 };
1276
1277 #endif  /* CONFIG_USB_BELKIN */
1278
1279
1280 \f
1281 /*-------------------------------------------------------------------------
1282  *
1283  * Communications Device Class declarations.
1284  * Used by CDC Ethernet, and some CDC variants
1285  *
1286  *-------------------------------------------------------------------------*/
1287
1288 #ifdef  CONFIG_USB_CDCETHER
1289 #define NEED_GENERIC_CDC
1290 #endif
1291
1292 #ifdef  CONFIG_USB_ZAURUS
1293 /* Ethernet variant uses funky framing, broken ethernet addressing */
1294 #define NEED_GENERIC_CDC
1295 #endif
1296
1297 #ifdef  CONFIG_USB_RNDIS
1298 /* ACM variant uses even funkier framing, complex control RPC scheme */
1299 #define NEED_GENERIC_CDC
1300 #endif
1301
1302
1303 #ifdef  NEED_GENERIC_CDC
1304
1305 #include <linux/usb_cdc.h>
1306
1307 struct cdc_state {
1308         struct usb_cdc_header_desc      *header;
1309         struct usb_cdc_union_desc       *u;
1310         struct usb_cdc_ether_desc       *ether;
1311         struct usb_interface            *control;
1312         struct usb_interface            *data;
1313 };
1314
1315 static struct usb_driver usbnet_driver;
1316
1317 /*
1318  * probes control interface, claims data interface, collects the bulk
1319  * endpoints, activates data interface (if needed), maybe sets MTU.
1320  * all pure cdc, except for certain firmware workarounds.
1321  */
1322 static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
1323 {
1324         u8                              *buf = intf->cur_altsetting->extra;
1325         int                             len = intf->cur_altsetting->extralen;
1326         struct usb_interface_descriptor *d;
1327         struct cdc_state                *info = (void *) &dev->data;
1328         int                             status;
1329         int                             rndis;
1330
1331         if (sizeof dev->data < sizeof *info)
1332                 return -EDOM;
1333
1334         /* expect strict spec conformance for the descriptors, but
1335          * cope with firmware which stores them in the wrong place
1336          */
1337         if (len == 0 && dev->udev->actconfig->extralen) {
1338                 /* Motorola SB4100 (and others: Brad Hards says it's
1339                  * from a Broadcom design) put CDC descriptors here
1340                  */
1341                 buf = dev->udev->actconfig->extra;
1342                 len = dev->udev->actconfig->extralen;
1343                 if (len)
1344                         dev_dbg (&intf->dev,
1345                                 "CDC descriptors on config\n");
1346         }
1347
1348         /* this assumes that if there's a non-RNDIS vendor variant
1349          * of cdc-acm, it'll fail RNDIS requests cleanly.
1350          */
1351         rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
1352
1353         memset (info, 0, sizeof *info);
1354         info->control = intf;
1355         while (len > 3) {
1356                 if (buf [1] != USB_DT_CS_INTERFACE)
1357                         goto next_desc;
1358
1359                 /* use bDescriptorSubType to identify the CDC descriptors.
1360                  * We expect devices with CDC header and union descriptors.
1361                  * For CDC Ethernet we need the ethernet descriptor.
1362                  * For RNDIS, ignore two (pointless) CDC modem descriptors
1363                  * in favor of a complicated OID-based RPC scheme doing what
1364                  * CDC Ethernet achieves with a simple descriptor.
1365                  */
1366                 switch (buf [2]) {
1367                 case USB_CDC_HEADER_TYPE:
1368                         if (info->header) {
1369                                 dev_dbg (&intf->dev, "extra CDC header\n");
1370                                 goto bad_desc;
1371                         }
1372                         info->header = (void *) buf;
1373                         if (info->header->bLength != sizeof *info->header) {
1374                                 dev_dbg (&intf->dev, "CDC header len %u\n",
1375                                         info->header->bLength);
1376                                 goto bad_desc;
1377                         }
1378                         break;
1379                 case USB_CDC_UNION_TYPE:
1380                         if (info->u) {
1381                                 dev_dbg (&intf->dev, "extra CDC union\n");
1382                                 goto bad_desc;
1383                         }
1384                         info->u = (void *) buf;
1385                         if (info->u->bLength != sizeof *info->u) {
1386                                 dev_dbg (&intf->dev, "CDC union len %u\n",
1387                                         info->u->bLength);
1388                                 goto bad_desc;
1389                         }
1390
1391                         /* we need a master/control interface (what we're
1392                          * probed with) and a slave/data interface; union
1393                          * descriptors sort this all out.
1394                          */
1395                         info->control = usb_ifnum_to_if(dev->udev,
1396                                                 info->u->bMasterInterface0);
1397                         info->data = usb_ifnum_to_if(dev->udev,
1398                                                 info->u->bSlaveInterface0);
1399                         if (!info->control || !info->data) {
1400                                 dev_dbg (&intf->dev,
1401                                         "master #%u/%p slave #%u/%p\n",
1402                                         info->u->bMasterInterface0,
1403                                         info->control,
1404                                         info->u->bSlaveInterface0,
1405                                         info->data);
1406                                 goto bad_desc;
1407                         }
1408                         if (info->control != intf) {
1409                                 dev_dbg (&intf->dev, "bogus CDC Union\n");
1410                                 /* Ambit USB Cable Modem (and maybe others)
1411                                  * interchanges master and slave interface.
1412                                  */
1413                                 if (info->data == intf) {
1414                                         info->data = info->control;
1415                                         info->control = intf;
1416                                 } else
1417                                         goto bad_desc;
1418                         }
1419
1420                         /* a data interface altsetting does the real i/o */
1421                         d = &info->data->cur_altsetting->desc;
1422                         if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
1423                                 dev_dbg (&intf->dev, "slave class %u\n",
1424                                         d->bInterfaceClass);
1425                                 goto bad_desc;
1426                         }
1427                         break;
1428                 case USB_CDC_ETHERNET_TYPE:
1429                         if (info->ether) {
1430                                 dev_dbg (&intf->dev, "extra CDC ether\n");
1431                                 goto bad_desc;
1432                         }
1433                         info->ether = (void *) buf;
1434                         if (info->ether->bLength != sizeof *info->ether) {
1435                                 dev_dbg (&intf->dev, "CDC ether len %u\n",
1436                                         info->u->bLength);
1437                                 goto bad_desc;
1438                         }
1439                         dev->net->mtu = le16_to_cpup (
1440                                                 &info->ether->wMaxSegmentSize)
1441                                         - ETH_HLEN;
1442                         /* because of Zaurus, we may be ignoring the host
1443                          * side link address we were given.
1444                          */
1445                         break;
1446                 }
1447 next_desc:
1448                 len -= buf [0]; /* bLength */
1449                 buf += buf [0];
1450         }
1451
1452         if (!info->header || !info->u || (!rndis && !info->ether)) {
1453                 dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
1454                         info->header ? "" : "header ",
1455                         info->u ? "" : "union ",
1456                         info->ether ? "" : "ether ");
1457                 goto bad_desc;
1458         }
1459
1460         /* claim data interface and set it up ... with side effects.
1461          * network traffic can't flow until an altsetting is enabled.
1462          */
1463         status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
1464         if (status < 0)
1465                 return status;
1466         status = get_endpoints (dev, info->data);
1467         if (status < 0) {
1468                 /* ensure immediate exit from usbnet_disconnect */
1469                 usb_set_intfdata(info->data, NULL);
1470                 usb_driver_release_interface (&usbnet_driver, info->data);
1471                 return status;
1472         }
1473
1474         /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
1475         dev->status = NULL;
1476         if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
1477                 struct usb_endpoint_descriptor  *desc;
1478
1479                 dev->status = &info->control->cur_altsetting->endpoint [0];
1480                 desc = &dev->status->desc;
1481                 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
1482                                 || !(desc->bEndpointAddress & USB_DIR_IN)
1483                                 || (le16_to_cpu(desc->wMaxPacketSize)
1484                                         < sizeof (struct usb_cdc_notification))
1485                                 || !desc->bInterval) {
1486                         dev_dbg (&intf->dev, "bad notification endpoint\n");
1487                         dev->status = NULL;
1488                 }
1489         }
1490         if (rndis && !dev->status) {
1491                 dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
1492                 usb_set_intfdata(info->data, NULL);
1493                 usb_driver_release_interface (&usbnet_driver, info->data);
1494                 return -ENODEV;
1495         }
1496         return 0;
1497
1498 bad_desc:
1499         dev_info (&dev->udev->dev, "bad CDC descriptors\n");
1500         return -ENODEV;
1501 }
1502
1503 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
1504 {
1505         struct cdc_state                *info = (void *) &dev->data;
1506
1507         /* disconnect master --> disconnect slave */
1508         if (intf == info->control && info->data) {
1509                 /* ensure immediate exit from usbnet_disconnect */
1510                 usb_set_intfdata(info->data, NULL);
1511                 usb_driver_release_interface (&usbnet_driver, info->data);
1512                 info->data = NULL;
1513         }
1514
1515         /* and vice versa (just in case) */
1516         else if (intf == info->data && info->control) {
1517                 /* ensure immediate exit from usbnet_disconnect */
1518                 usb_set_intfdata(info->control, NULL);
1519                 usb_driver_release_interface (&usbnet_driver, info->control);
1520                 info->control = NULL;
1521         }
1522 }
1523
1524
1525 static void dumpspeed (struct usbnet *dev, __le32 *speeds)
1526 {
1527         if (netif_msg_timer (dev))
1528                 devinfo (dev, "link speeds: %u kbps up, %u kbps down",
1529                         __le32_to_cpu(speeds[0]) / 1000,
1530                 __le32_to_cpu(speeds[1]) / 1000);
1531 }
1532
1533 static void cdc_status (struct usbnet *dev, struct urb *urb)
1534 {
1535         struct usb_cdc_notification     *event;
1536
1537         if (urb->actual_length < sizeof *event)
1538                 return;
1539         
1540         /* SPEED_CHANGE can get split into two 8-byte packets */
1541         if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
1542                 dumpspeed (dev, (__le32 *) urb->transfer_buffer);
1543                 return;
1544         }
1545
1546         event = urb->transfer_buffer;
1547         switch (event->bNotificationType) {
1548         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
1549                 if (netif_msg_timer (dev))
1550                         devdbg (dev, "CDC: carrier %s",
1551                                         event->wValue ? "on" : "off");
1552                 if (event->wValue)
1553                         netif_carrier_on(dev->net);
1554                 else
1555                         netif_carrier_off(dev->net);
1556                 break;
1557         case USB_CDC_NOTIFY_SPEED_CHANGE:       /* tx/rx rates */
1558                 if (netif_msg_timer (dev))
1559                         devdbg (dev, "CDC: speed change (len %d)",
1560                                         urb->actual_length);
1561                 if (urb->actual_length != (sizeof *event + 8))
1562                         set_bit (EVENT_STS_SPLIT, &dev->flags);
1563                 else
1564                         dumpspeed (dev, (__le32 *) &event[1]);
1565                 break;
1566         // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:      /* RNDIS; or unsolicited */
1567         default:
1568                 deverr (dev, "CDC: unexpected notification %02x!",
1569                                  event->bNotificationType);
1570                 break;
1571         }
1572 }
1573
1574 #endif  /* NEED_GENERIC_CDC */
1575
1576 \f
1577 #ifdef  CONFIG_USB_CDCETHER
1578 #define HAVE_HARDWARE
1579
1580 /*-------------------------------------------------------------------------
1581  *
1582  * Communications Device Class, Ethernet Control model
1583  * 
1584  * Takes two interfaces.  The DATA interface is inactive till an altsetting
1585  * is selected.  Configuration data includes class descriptors.
1586  *
1587  * This should interop with whatever the 2.4 "CDCEther.c" driver
1588  * (by Brad Hards) talked with.
1589  *
1590  *-------------------------------------------------------------------------*/
1591
1592 #include <linux/ctype.h>
1593
1594 static u8 nibble (unsigned char c)
1595 {
1596         if (likely (isdigit (c)))
1597                 return c - '0';
1598         c = toupper (c);
1599         if (likely (isxdigit (c)))
1600                 return 10 + c - 'A';
1601         return 0;
1602 }
1603
1604 static inline int
1605 get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
1606 {
1607         int             tmp, i;
1608         unsigned char   buf [13];
1609
1610         tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
1611         if (tmp != 12) {
1612                 dev_dbg (&dev->udev->dev,
1613                         "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
1614                 if (tmp >= 0)
1615                         tmp = -EINVAL;
1616                 return tmp;
1617         }
1618         for (i = tmp = 0; i < 6; i++, tmp += 2)
1619                 dev->net->dev_addr [i] =
1620                          (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
1621         return 0;
1622 }
1623
1624 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
1625 {
1626         int                             status;
1627         struct cdc_state                *info = (void *) &dev->data;
1628
1629         status = generic_cdc_bind (dev, intf);
1630         if (status < 0)
1631                 return status;
1632
1633         status = get_ethernet_addr (dev, info->ether);
1634         if (status < 0) {
1635                 usb_set_intfdata(info->data, NULL);
1636                 usb_driver_release_interface (&usbnet_driver, info->data);
1637                 return status;
1638         }
1639
1640         /* FIXME cdc-ether has some multicast code too, though it complains
1641          * in routine cases.  info->ether describes the multicast support.
1642          */
1643         return 0;
1644 }
1645
1646 static const struct driver_info cdc_info = {
1647         .description =  "CDC Ethernet Device",
1648         .flags =        FLAG_ETHER,
1649         // .check_connect = cdc_check_connect,
1650         .bind =         cdc_bind,
1651         .unbind =       cdc_unbind,
1652         .status =       cdc_status,
1653 };
1654
1655 #endif  /* CONFIG_USB_CDCETHER */
1656
1657
1658 \f
1659 #ifdef  CONFIG_USB_EPSON2888
1660 #define HAVE_HARDWARE
1661
1662 /*-------------------------------------------------------------------------
1663  *
1664  * EPSON USB clients
1665  *
1666  * This is the same idea as Linux PDAs (below) except the firmware in the
1667  * device might not be Tux-powered.  Epson provides reference firmware that
1668  * implements this interface.  Product developers can reuse or modify that
1669  * code, such as by using their own product and vendor codes.
1670  *
1671  * Support was from Juro Bystricky <bystricky.juro@erd.epson.com>
1672  *
1673  *-------------------------------------------------------------------------*/
1674
1675 static const struct driver_info epson2888_info = {
1676         .description =  "Epson USB Device",
1677         .check_connect = always_connected,
1678
1679         .in = 4, .out = 3,
1680 };
1681
1682 #endif  /* CONFIG_USB_EPSON2888 */
1683
1684 \f
1685 #ifdef CONFIG_USB_GENESYS
1686 #define HAVE_HARDWARE
1687
1688 /*-------------------------------------------------------------------------
1689  *
1690  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
1691  *
1692  * ... should partially interop with the Win32 driver for this hardware
1693  * The GeneSys docs imply there's some NDIS issue motivating this framing.
1694  *
1695  * Some info from GeneSys:
1696  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
1697  *    (Some cables, like the BAFO-100c, use the half duplex version.)
1698  *  - For the full duplex model, the low bit of the version code says
1699  *    which side is which ("left/right").
1700  *  - For the half duplex type, a control/interrupt handshake settles
1701  *    the transfer direction.  (That's disabled here, partially coded.)
1702  *    A control URB would block until other side writes an interrupt.
1703  *
1704  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
1705  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
1706  *
1707  *-------------------------------------------------------------------------*/
1708
1709 // control msg write command
1710 #define GENELINK_CONNECT_WRITE                  0xF0
1711 // interrupt pipe index
1712 #define GENELINK_INTERRUPT_PIPE                 0x03
1713 // interrupt read buffer size
1714 #define INTERRUPT_BUFSIZE                       0x08
1715 // interrupt pipe interval value
1716 #define GENELINK_INTERRUPT_INTERVAL             0x10
1717 // max transmit packet number per transmit
1718 #define GL_MAX_TRANSMIT_PACKETS                 32
1719 // max packet length
1720 #define GL_MAX_PACKET_LEN                       1514
1721 // max receive buffer size 
1722 #define GL_RCV_BUF_SIZE         \
1723         (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
1724
1725 struct gl_packet {
1726         u32             packet_length;
1727         char            packet_data [1];
1728 };
1729
1730 struct gl_header {
1731         u32                     packet_count;
1732         struct gl_packet        packets;
1733 };
1734
1735 #ifdef  GENELINK_ACK
1736
1737 // FIXME:  this code is incomplete, not debugged; it doesn't
1738 // handle interrupts correctly.  interrupts should be generic
1739 // code like all other device I/O, anyway.
1740
1741 struct gl_priv { 
1742         struct urb      *irq_urb;
1743         char            irq_buf [INTERRUPT_BUFSIZE];
1744 };
1745
1746 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
1747 {
1748         int retval;
1749
1750         retval = usb_control_msg (dev->udev,
1751                       usb_sndctrlpipe (dev->udev, 0),
1752                       request,
1753                       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1754                       value, 
1755                       0,                        // index
1756                       0,                        // data buffer
1757                       0,                        // size
1758                       CONTROL_TIMEOUT_MS);
1759         return retval;
1760 }
1761
1762 static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
1763 {
1764         int status = urb->status;
1765         
1766         switch (status) {
1767         case 0:
1768                 /* success */
1769                 break;
1770         case -ECONNRESET:
1771         case -ENOENT:
1772         case -ESHUTDOWN:
1773                 /* this urb is terminated, clean up */
1774                 dbg("%s - urb shutting down with status: %d",
1775                                 __FUNCTION__, status);
1776                 return;
1777         default:
1778                 dbg("%s - nonzero urb status received: %d",
1779                                 __FUNCTION__, urb->status);
1780         }
1781
1782         status = usb_submit_urb (urb, GFP_ATOMIC);
1783         if (status)
1784                 err ("%s - usb_submit_urb failed with result %d",
1785                      __FUNCTION__, status);
1786 }
1787
1788 static int gl_interrupt_read (struct usbnet *dev)
1789 {
1790         struct gl_priv  *priv = dev->priv_data;
1791         int             retval;
1792
1793         // issue usb interrupt read
1794         if (priv && priv->irq_urb) {
1795                 // submit urb
1796                 if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
1797                         dbg ("gl_interrupt_read: submit fail - %X...", retval);
1798                 else
1799                         dbg ("gl_interrupt_read: submit success...");
1800         }
1801
1802         return 0;
1803 }
1804
1805 // check whether another side is connected
1806 static int genelink_check_connect (struct usbnet *dev)
1807 {
1808         int                     retval;
1809
1810         dbg ("genelink_check_connect...");
1811
1812         // detect whether another side is connected
1813         if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
1814                 dbg ("%s: genelink_check_connect write fail - %X",
1815                         dev->net->name, retval);
1816                 return retval;
1817         }
1818
1819         // usb interrupt read to ack another side 
1820         if ((retval = gl_interrupt_read (dev)) != 0) {
1821                 dbg ("%s: genelink_check_connect read fail - %X",
1822                         dev->net->name, retval);
1823                 return retval;
1824         }
1825
1826         dbg ("%s: genelink_check_connect read success", dev->net->name);
1827         return 0;
1828 }
1829
1830 // allocate and initialize the private data for genelink
1831 static int genelink_init (struct usbnet *dev)
1832 {
1833         struct gl_priv *priv;
1834
1835         // allocate the private data structure
1836         if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
1837                 dbg ("%s: cannot allocate private data per device",
1838                         dev->net->name);
1839                 return -ENOMEM;
1840         }
1841
1842         // allocate irq urb
1843         if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
1844                 dbg ("%s: cannot allocate private irq urb per device",
1845                         dev->net->name);
1846                 kfree (priv);
1847                 return -ENOMEM;
1848         }
1849
1850         // fill irq urb
1851         usb_fill_int_urb (priv->irq_urb, dev->udev,
1852                 usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
1853                 priv->irq_buf, INTERRUPT_BUFSIZE,
1854                 gl_interrupt_complete, 0,
1855                 GENELINK_INTERRUPT_INTERVAL);
1856
1857         // set private data pointer
1858         dev->priv_data = priv;
1859
1860         return 0;
1861 }
1862
1863 // release the private data
1864 static int genelink_free (struct usbnet *dev)
1865 {
1866         struct gl_priv  *priv = dev->priv_data;
1867
1868         if (!priv) 
1869                 return 0;
1870
1871 // FIXME:  can't cancel here; it's synchronous, and
1872 // should have happened earlier in any case (interrupt
1873 // handling needs to be generic)
1874
1875         // cancel irq urb first
1876         usb_kill_urb (priv->irq_urb);
1877
1878         // free irq urb
1879         usb_free_urb (priv->irq_urb);
1880
1881         // free the private data structure
1882         kfree (priv);
1883
1884         return 0;
1885 }
1886
1887 #endif
1888
1889 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
1890 {
1891         struct gl_header        *header;
1892         struct gl_packet        *packet;
1893         struct sk_buff          *gl_skb;
1894         u32                     size;
1895
1896         header = (struct gl_header *) skb->data;
1897
1898         // get the packet count of the received skb
1899         le32_to_cpus (&header->packet_count);
1900         if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
1901                         || (header->packet_count < 0)) {
1902                 dbg ("genelink: invalid received packet count %d",
1903                         header->packet_count);
1904                 return 0;
1905         }
1906
1907         // set the current packet pointer to the first packet
1908         packet = &header->packets;
1909
1910         // decrement the length for the packet count size 4 bytes
1911         skb_pull (skb, 4);
1912
1913         while (header->packet_count > 1) {
1914                 // get the packet length
1915                 size = packet->packet_length;
1916
1917                 // this may be a broken packet
1918                 if (size > GL_MAX_PACKET_LEN) {
1919                         dbg ("genelink: invalid rx length %d", size);
1920                         return 0;
1921                 }
1922
1923                 // allocate the skb for the individual packet
1924                 gl_skb = alloc_skb (size, GFP_ATOMIC);
1925                 if (gl_skb) {
1926
1927                         // copy the packet data to the new skb
1928                         memcpy(skb_put(gl_skb, size), packet->packet_data, size);
1929                         skb_return (dev, skb);
1930                 }
1931
1932                 // advance to the next packet
1933                 packet = (struct gl_packet *)
1934                         &packet->packet_data [size];
1935                 header->packet_count--;
1936
1937                 // shift the data pointer to the next gl_packet
1938                 skb_pull (skb, size + 4);
1939         }
1940
1941         // skip the packet length field 4 bytes
1942         skb_pull (skb, 4);
1943
1944         if (skb->len > GL_MAX_PACKET_LEN) {
1945                 dbg ("genelink: invalid rx length %d", skb->len);
1946                 return 0;
1947         }
1948         return 1;
1949 }
1950
1951 static struct sk_buff *
1952 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1953 {
1954         int     padlen;
1955         int     length = skb->len;
1956         int     headroom = skb_headroom (skb);
1957         int     tailroom = skb_tailroom (skb);
1958         u32     *packet_count;
1959         u32     *packet_len;
1960
1961         // FIXME:  magic numbers, bleech
1962         padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
1963
1964         if ((!skb_cloned (skb))
1965                         && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
1966                 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
1967                         skb->data = memmove (skb->head + (4 + 4*1),
1968                                              skb->data, skb->len);
1969                         skb->tail = skb->data + skb->len;
1970                 }
1971         } else {
1972                 struct sk_buff  *skb2;
1973                 skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
1974                 dev_kfree_skb_any (skb);
1975                 skb = skb2;
1976                 if (!skb)
1977                         return NULL;
1978         }
1979
1980         // attach the packet count to the header
1981         packet_count = (u32 *) skb_push (skb, (4 + 4*1));
1982         packet_len = packet_count + 1;
1983
1984         // FIXME little endian?
1985         *packet_count = 1;
1986         *packet_len = length;
1987
1988         // add padding byte
1989         if ((skb->len % dev->maxpacket) == 0)
1990                 skb_put (skb, 1);
1991
1992         return skb;
1993 }
1994
1995 static const struct driver_info genelink_info = {
1996         .description =  "Genesys GeneLink",
1997         .flags =        FLAG_FRAMING_GL | FLAG_NO_SETINT,
1998         .rx_fixup =     genelink_rx_fixup,
1999         .tx_fixup =     genelink_tx_fixup,
2000
2001         .in = 1, .out = 2,
2002
2003 #ifdef  GENELINK_ACK
2004         .check_connect =genelink_check_connect,
2005 #endif
2006 };
2007
2008 #endif /* CONFIG_USB_GENESYS */
2009
2010
2011 \f
2012 #ifdef  CONFIG_USB_NET1080
2013 #define HAVE_HARDWARE
2014
2015 /*-------------------------------------------------------------------------
2016  *
2017  * Netchip 1080 driver ... http://www.netchip.com
2018  * Used in LapLink cables
2019  *
2020  *-------------------------------------------------------------------------*/
2021
2022 #define dev_packet_id   data[0]
2023 #define frame_errors    data[1]
2024
2025 /*
2026  * NetChip framing of ethernet packets, supporting additional error
2027  * checks for links that may drop bulk packets from inside messages.
2028  * Odd USB length == always short read for last usb packet.
2029  *      - nc_header
2030  *      - Ethernet header (14 bytes)
2031  *      - payload
2032  *      - (optional padding byte, if needed so length becomes odd)
2033  *      - nc_trailer
2034  *
2035  * This framing is to be avoided for non-NetChip devices.
2036  */
2037
2038 struct nc_header {              // packed:
2039         __le16  hdr_len;                // sizeof nc_header (LE, all)
2040         __le16  packet_len;             // payload size (including ethhdr)
2041         __le16  packet_id;              // detects dropped packets
2042 #define MIN_HEADER      6
2043
2044         // all else is optional, and must start with:
2045         // u16  vendorId;               // from usb-if
2046         // u16  productId;
2047 } __attribute__((__packed__));
2048
2049 #define PAD_BYTE        ((unsigned char)0xAC)
2050
2051 struct nc_trailer {
2052         __le16  packet_id;
2053 } __attribute__((__packed__));
2054
2055 // packets may use FLAG_FRAMING_NC and optional pad
2056 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
2057                                 + sizeof (struct ethhdr) \
2058                                 + (mtu) \
2059                                 + 1 \
2060                                 + sizeof (struct nc_trailer))
2061
2062 #define MIN_FRAMED      FRAMED_SIZE(0)
2063
2064
2065 /*
2066  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
2067  * before the hardware drops it.  If that's done, the driver will need to
2068  * frame network packets to guard against the dropped USB packets.  The win32
2069  * driver sets this for both sides of the link.
2070  */
2071 #define NC_READ_TTL_MS  ((u8)255)       // ms
2072
2073 /*
2074  * We ignore most registers and EEPROM contents.
2075  */
2076 #define REG_USBCTL      ((u8)0x04)
2077 #define REG_TTL         ((u8)0x10)
2078 #define REG_STATUS      ((u8)0x11)
2079
2080 /*
2081  * Vendor specific requests to read/write data
2082  */
2083 #define REQUEST_REGISTER        ((u8)0x10)
2084 #define REQUEST_EEPROM          ((u8)0x11)
2085
2086 static int
2087 nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
2088 {
2089         int status = usb_control_msg (dev->udev,
2090                 usb_rcvctrlpipe (dev->udev, 0),
2091                 req,
2092                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2093                 0, regnum,
2094                 retval_ptr, sizeof *retval_ptr,
2095                 CONTROL_TIMEOUT_MS);
2096         if (status > 0)
2097                 status = 0;
2098         if (!status)
2099                 le16_to_cpus (retval_ptr);
2100         return status;
2101 }
2102
2103 static inline int
2104 nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
2105 {
2106         return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
2107 }
2108
2109 // no retval ... can become async, usable in_interrupt()
2110 static void
2111 nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
2112 {
2113         usb_control_msg (dev->udev,
2114                 usb_sndctrlpipe (dev->udev, 0),
2115                 req,
2116                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2117                 value, regnum,
2118                 NULL, 0,                        // data is in setup packet
2119                 CONTROL_TIMEOUT_MS);
2120 }
2121
2122 static inline void
2123 nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
2124 {
2125         nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
2126 }
2127
2128
2129 #if 0
2130 static void nc_dump_registers (struct usbnet *dev)
2131 {
2132         u8      reg;
2133         u16     *vp = kmalloc (sizeof (u16));
2134
2135         if (!vp) {
2136                 dbg ("no memory?");
2137                 return;
2138         }
2139
2140         dbg ("%s registers:", dev->net->name);
2141         for (reg = 0; reg < 0x20; reg++) {
2142                 int retval;
2143
2144                 // reading some registers is trouble
2145                 if (reg >= 0x08 && reg <= 0xf)
2146                         continue;
2147                 if (reg >= 0x12 && reg <= 0x1e)
2148                         continue;
2149
2150                 retval = nc_register_read (dev, reg, vp);
2151                 if (retval < 0)
2152                         dbg ("%s reg [0x%x] ==> error %d",
2153                                 dev->net->name, reg, retval);
2154                 else
2155                         dbg ("%s reg [0x%x] = 0x%x",
2156                                 dev->net->name, reg, *vp);
2157         }
2158         kfree (vp);
2159 }
2160 #endif
2161
2162
2163 /*-------------------------------------------------------------------------*/
2164
2165 /*
2166  * Control register
2167  */
2168
2169 #define USBCTL_WRITABLE_MASK    0x1f0f
2170 // bits 15-13 reserved, r/o
2171 #define USBCTL_ENABLE_LANG      (1 << 12)
2172 #define USBCTL_ENABLE_MFGR      (1 << 11)
2173 #define USBCTL_ENABLE_PROD      (1 << 10)
2174 #define USBCTL_ENABLE_SERIAL    (1 << 9)
2175 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
2176 // bits 7-4 reserved, r/o
2177 #define USBCTL_FLUSH_OTHER      (1 << 3)
2178 #define USBCTL_FLUSH_THIS       (1 << 2)
2179 #define USBCTL_DISCONN_OTHER    (1 << 1)
2180 #define USBCTL_DISCONN_THIS     (1 << 0)
2181
2182 static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
2183 {
2184         if (!netif_msg_link (dev))
2185                 return;
2186         devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
2187                         " this%s%s;"
2188                         " other%s%s; r/o 0x%x",
2189                 dev->udev->bus->bus_name, dev->udev->devpath,
2190                 usbctl,
2191                 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
2192                 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
2193                 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
2194                 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
2195                 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
2196
2197                 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
2198                 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
2199                 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
2200                 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
2201                 usbctl & ~USBCTL_WRITABLE_MASK
2202                 );
2203 }
2204
2205 /*-------------------------------------------------------------------------*/
2206
2207 /*
2208  * Status register
2209  */
2210
2211 #define STATUS_PORT_A           (1 << 15)
2212
2213 #define STATUS_CONN_OTHER       (1 << 14)
2214 #define STATUS_SUSPEND_OTHER    (1 << 13)
2215 #define STATUS_MAILBOX_OTHER    (1 << 12)
2216 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
2217
2218 #define STATUS_CONN_THIS        (1 << 6)
2219 #define STATUS_SUSPEND_THIS     (1 << 5)
2220 #define STATUS_MAILBOX_THIS     (1 << 4)
2221 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) && 0x03)
2222
2223 #define STATUS_UNSPEC_MASK      0x0c8c
2224 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
2225
2226
2227 static inline void nc_dump_status (struct usbnet *dev, u16 status)
2228 {
2229         if (!netif_msg_link (dev))
2230                 return;
2231         devdbg (dev, "net1080 %s-%s status 0x%x:"
2232                         " this (%c) PKT=%d%s%s%s;"
2233                         " other PKT=%d%s%s%s; unspec 0x%x",
2234                 dev->udev->bus->bus_name, dev->udev->devpath,
2235                 status,
2236
2237                 // XXX the packet counts don't seem right
2238                 // (1 at reset, not 0); maybe UNSPEC too
2239
2240                 (status & STATUS_PORT_A) ? 'A' : 'B',
2241                 STATUS_PACKETS_THIS (status),
2242                 (status & STATUS_CONN_THIS) ? " CON" : "",
2243                 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
2244                 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
2245
2246                 STATUS_PACKETS_OTHER (status),
2247                 (status & STATUS_CONN_OTHER) ? " CON" : "",
2248                 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
2249                 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
2250
2251                 status & STATUS_UNSPEC_MASK
2252                 );
2253 }
2254
2255 /*-------------------------------------------------------------------------*/
2256
2257 /*
2258  * TTL register
2259  */
2260
2261 #define TTL_THIS(ttl)   (0x00ff & ttl)
2262 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
2263 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
2264
2265 static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
2266 {
2267         if (netif_msg_link (dev))
2268                 devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
2269                         dev->udev->bus->bus_name, dev->udev->devpath,
2270                         ttl, TTL_THIS (ttl), TTL_OTHER (ttl));
2271 }
2272
2273 /*-------------------------------------------------------------------------*/
2274
2275 static int net1080_reset (struct usbnet *dev)
2276 {
2277         u16             usbctl, status, ttl;
2278         u16             *vp = kmalloc (sizeof (u16), GFP_KERNEL);
2279         int             retval;
2280
2281         if (!vp)
2282                 return -ENOMEM;
2283
2284         // nc_dump_registers (dev);
2285
2286         if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
2287                 dbg ("can't read %s-%s status: %d",
2288                         dev->udev->bus->bus_name, dev->udev->devpath, retval);
2289                 goto done;
2290         }
2291         status = *vp;
2292         nc_dump_status (dev, status);
2293
2294         if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
2295                 dbg ("can't read USBCTL, %d", retval);
2296                 goto done;
2297         }
2298         usbctl = *vp;
2299         nc_dump_usbctl (dev, usbctl);
2300
2301         nc_register_write (dev, REG_USBCTL,
2302                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
2303
2304         if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
2305                 dbg ("can't read TTL, %d", retval);
2306                 goto done;
2307         }
2308         ttl = *vp;
2309         // nc_dump_ttl (dev, ttl);
2310
2311         nc_register_write (dev, REG_TTL,
2312                         MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
2313         dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
2314
2315         if (netif_msg_link (dev))
2316                 devinfo (dev, "port %c, peer %sconnected",
2317                         (status & STATUS_PORT_A) ? 'A' : 'B',
2318                         (status & STATUS_CONN_OTHER) ? "" : "dis"
2319                         );
2320         retval = 0;
2321
2322 done:
2323         kfree (vp);
2324         return retval;
2325 }
2326
2327 static int net1080_check_connect (struct usbnet *dev)
2328 {
2329         int                     retval;
2330         u16                     status;
2331         u16                     *vp = kmalloc (sizeof (u16), GFP_KERNEL);
2332
2333         if (!vp)
2334                 return -ENOMEM;
2335         retval = nc_register_read (dev, REG_STATUS, vp);
2336         status = *vp;
2337         kfree (vp);
2338         if (retval != 0) {
2339                 dbg ("%s net1080_check_conn read - %d", dev->net->name, retval);
2340                 return retval;
2341         }
2342         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
2343                 return -ENOLINK;
2344         return 0;
2345 }
2346
2347 static void nc_flush_complete (struct urb *urb, struct pt_regs *regs)
2348 {
2349         kfree (urb->context);
2350         usb_free_urb(urb);
2351 }
2352
2353 static void nc_ensure_sync (struct usbnet *dev)
2354 {
2355         dev->frame_errors++;
2356         if (dev->frame_errors > 5) {
2357                 struct urb              *urb;
2358                 struct usb_ctrlrequest  *req;
2359                 int                     status;
2360
2361                 /* Send a flush */
2362                 urb = usb_alloc_urb (0, SLAB_ATOMIC);
2363                 if (!urb)
2364                         return;
2365
2366                 req = kmalloc (sizeof *req, GFP_ATOMIC);
2367                 if (!req) {
2368                         usb_free_urb (urb);
2369                         return;
2370                 }
2371
2372                 req->bRequestType = USB_DIR_OUT
2373                         | USB_TYPE_VENDOR
2374                         | USB_RECIP_DEVICE;
2375                 req->bRequest = REQUEST_REGISTER;
2376                 req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS
2377                                 | USBCTL_FLUSH_OTHER);
2378                 req->wIndex = cpu_to_le16 (REG_USBCTL);
2379                 req->wLength = cpu_to_le16 (0);
2380
2381                 /* queue an async control request, we don't need
2382                  * to do anything when it finishes except clean up.
2383                  */
2384                 usb_fill_control_urb (urb, dev->udev,
2385                         usb_sndctrlpipe (dev->udev, 0),
2386                         (unsigned char *) req,
2387                         NULL, 0,
2388                         nc_flush_complete, req);
2389                 status = usb_submit_urb (urb, GFP_ATOMIC);
2390                 if (status) {
2391                         kfree (req);
2392                         usb_free_urb (urb);
2393                         return;
2394                 }
2395
2396                 if (netif_msg_rx_err (dev))
2397                         devdbg (dev, "flush net1080; too many framing errors");
2398                 dev->frame_errors = 0;
2399         }
2400 }
2401
2402 static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
2403 {
2404         struct nc_header        *header;
2405         struct nc_trailer       *trailer;
2406         u16                     hdr_len, packet_len;
2407
2408         if (!(skb->len & 0x01)
2409                         || MIN_FRAMED > skb->len
2410                         || skb->len > FRAMED_SIZE (dev->net->mtu)) {
2411                 dev->stats.rx_frame_errors++;
2412                 dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
2413                         (int)MIN_FRAMED, (int)FRAMED_SIZE (dev->net->mtu),
2414                         dev->net->mtu);
2415                 nc_ensure_sync (dev);
2416                 return 0;
2417         }
2418
2419         header = (struct nc_header *) skb->data;
2420         hdr_len = le16_to_cpup (&header->hdr_len);
2421         packet_len = le16_to_cpup (&header->packet_len);
2422         if (FRAMED_SIZE (packet_len) > MAX_PACKET) {
2423                 dev->stats.rx_frame_errors++;
2424                 dbg ("packet too big, %d", packet_len);
2425                 nc_ensure_sync (dev);
2426                 return 0;
2427         } else if (hdr_len < MIN_HEADER) {
2428                 dev->stats.rx_frame_errors++;
2429                 dbg ("header too short, %d", hdr_len);
2430                 nc_ensure_sync (dev);
2431                 return 0;
2432         } else if (hdr_len > MIN_HEADER) {
2433                 // out of band data for us?
2434                 dbg ("header OOB, %d bytes", hdr_len - MIN_HEADER);
2435                 nc_ensure_sync (dev);
2436                 // switch (vendor/product ids) { ... }
2437         }
2438         skb_pull (skb, hdr_len);
2439
2440         trailer = (struct nc_trailer *)
2441                 (skb->data + skb->len - sizeof *trailer);
2442         skb_trim (skb, skb->len - sizeof *trailer);
2443
2444         if ((packet_len & 0x01) == 0) {
2445                 if (skb->data [packet_len] != PAD_BYTE) {
2446                         dev->stats.rx_frame_errors++;
2447                         dbg ("bad pad");
2448                         return 0;
2449                 }
2450                 skb_trim (skb, skb->len - 1);
2451         }
2452         if (skb->len != packet_len) {
2453                 dev->stats.rx_frame_errors++;
2454                 dbg ("bad packet len %d (expected %d)",
2455                         skb->len, packet_len);
2456                 nc_ensure_sync (dev);
2457                 return 0;
2458         }
2459         if (header->packet_id != get_unaligned (&trailer->packet_id)) {
2460                 dev->stats.rx_fifo_errors++;
2461                 dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
2462                         le16_to_cpu (header->packet_id),
2463                         le16_to_cpu (trailer->packet_id));
2464                 return 0;
2465         }
2466 #if 0
2467         devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
2468                 header->packet_len, header->packet_id);
2469 #endif
2470         dev->frame_errors = 0;
2471         return 1;
2472 }
2473
2474 static struct sk_buff *
2475 net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
2476 {
2477         int                     padlen;
2478         struct sk_buff          *skb2;
2479
2480         padlen = ((skb->len + sizeof (struct nc_header)
2481                         + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
2482         if (!skb_cloned (skb)) {
2483                 int     headroom = skb_headroom (skb);
2484                 int     tailroom = skb_tailroom (skb);
2485
2486                 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
2487                             && sizeof (struct nc_header) <= headroom)
2488                         /* There's enough head and tail room */
2489                         return skb;
2490
2491                 if ((sizeof (struct nc_header) + padlen
2492                                         + sizeof (struct nc_trailer)) <
2493                                 (headroom + tailroom)) {
2494                         /* There's enough total room, so just readjust */
2495                         skb->data = memmove (skb->head
2496                                                 + sizeof (struct nc_header),
2497                                             skb->data, skb->len);
2498                         skb->tail = skb->data + skb->len;
2499                         return skb;
2500                 }
2501         }
2502
2503         /* Create a new skb to use with the correct size */
2504         skb2 = skb_copy_expand (skb,
2505                                 sizeof (struct nc_header),
2506                                 sizeof (struct nc_trailer) + padlen,
2507                                 flags);
2508         dev_kfree_skb_any (skb);
2509         return skb2;
2510 }
2511
2512 static const struct driver_info net1080_info = {
2513         .description =  "NetChip TurboCONNECT",
2514         .flags =        FLAG_FRAMING_NC,
2515         .reset =        net1080_reset,
2516         .check_connect =net1080_check_connect,
2517         .rx_fixup =     net1080_rx_fixup,
2518         .tx_fixup =     net1080_tx_fixup,
2519 };
2520
2521 #endif /* CONFIG_USB_NET1080 */
2522
2523
2524 \f
2525 #ifdef CONFIG_USB_PL2301
2526 #define HAVE_HARDWARE
2527
2528 /*-------------------------------------------------------------------------
2529  *
2530  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
2531  *
2532  * The protocol and handshaking used here should be bug-compatible
2533  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
2534  *
2535  *-------------------------------------------------------------------------*/
2536
2537 /*
2538  * Bits 0-4 can be used for software handshaking; they're set from
2539  * one end, cleared from the other, "read" with the interrupt byte.
2540  */
2541 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
2542 /* reserved bit -- rx ready (6) ? */
2543 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
2544 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
2545 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
2546 #define PL_TX_C         (1<<2)          /* transmission complete */
2547 #define PL_TX_REQ       (1<<1)          /* transmission received */
2548 #define PL_PEER_E       (1<<0)          /* peer exists */
2549
2550 static inline int
2551 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
2552 {
2553         return usb_control_msg (dev->udev,
2554                 usb_rcvctrlpipe (dev->udev, 0),
2555                 req,
2556                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2557                 val, index,
2558                 NULL, 0,
2559                 CONTROL_TIMEOUT_MS);
2560 }
2561
2562 static inline int
2563 pl_clear_QuickLink_features (struct usbnet *dev, int val)
2564 {
2565         return pl_vendor_req (dev, 1, (u8) val, 0);
2566 }
2567
2568 static inline int
2569 pl_set_QuickLink_features (struct usbnet *dev, int val)
2570 {
2571         return pl_vendor_req (dev, 3, (u8) val, 0);
2572 }
2573
2574 /*-------------------------------------------------------------------------*/
2575
2576 static int pl_reset (struct usbnet *dev)
2577 {
2578         /* some units seem to need this reset, others reject it utterly.
2579          * FIXME be more like "naplink" or windows drivers.
2580          */
2581         (void) pl_set_QuickLink_features (dev,
2582                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
2583         return 0;
2584 }
2585
2586 static const struct driver_info prolific_info = {
2587         .description =  "Prolific PL-2301/PL-2302",
2588         .flags =        FLAG_NO_SETINT,
2589                 /* some PL-2302 versions seem to fail usb_set_interface() */
2590         .reset =        pl_reset,
2591 };
2592
2593 #endif /* CONFIG_USB_PL2301 */
2594
2595 \f
2596 #ifdef CONFIG_USB_KC2190
2597 #define HAVE_HARDWARE
2598 static const struct driver_info kc2190_info = {
2599         .description =  "KC Technology KC-190",
2600 };
2601 #endif /* CONFIG_USB_KC2190 */
2602
2603 \f
2604 #ifdef  CONFIG_USB_ARMLINUX
2605 #define HAVE_HARDWARE
2606
2607 /*-------------------------------------------------------------------------
2608  *
2609  * Intel's SA-1100 chip integrates basic USB support, and is used
2610  * in PDAs like some iPaqs, the Yopy, some Zaurus models, and more.
2611  * When they run Linux, arch/arm/mach-sa1100/usb-eth.c may be used to
2612  * network using minimal USB framing data.
2613  *
2614  * This describes the driver currently in standard ARM Linux kernels.
2615  * The Zaurus uses a different driver (see later).
2616  *
2617  * PXA25x and PXA210 use XScale cores (ARM v5TE) with better USB support
2618  * and different USB endpoint numbering than the SA1100 devices.  The
2619  * mach-pxa/usb-eth.c driver re-uses the device ids from mach-sa1100
2620  * so we rely on the endpoint descriptors.
2621  *
2622  *-------------------------------------------------------------------------*/
2623
2624 static const struct driver_info linuxdev_info = {
2625         .description =  "Linux Device",
2626         .check_connect = always_connected,
2627 };
2628
2629 static const struct driver_info yopy_info = {
2630         .description =  "Yopy",
2631         .check_connect = always_connected,
2632 };
2633
2634 static const struct driver_info blob_info = {
2635         .description =  "Boot Loader OBject",
2636         .check_connect = always_connected,
2637 };
2638
2639 #endif  /* CONFIG_USB_ARMLINUX */
2640
2641 \f
2642 #ifdef CONFIG_USB_ZAURUS
2643 #define HAVE_HARDWARE
2644
2645 #include <linux/crc32.h>
2646
2647 /*-------------------------------------------------------------------------
2648  *
2649  * Zaurus is also a SA-1110 based PDA, but one using a different driver
2650  * (and framing) for its USB slave/gadget controller than the case above.
2651  *
2652  * For the current version of that driver, the main way that framing is
2653  * nonstandard (also from perspective of the CDC ethernet model!) is a
2654  * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
2655  * haven't been fully worked around.  Also, all Zaurii use the same
2656  * default Ethernet address.
2657  *
2658  * PXA based models use the same framing, and also can't implement
2659  * set_interface properly.
2660  *
2661  * All known Zaurii lie about their standards conformance.  Most lie by
2662  * saying they support CDC Ethernet.  Some lie and say they support CDC
2663  * MDLM (as if for access to cell phone modems).  Someone, please beat 
2664  * on Sharp for a while with a cluestick.
2665  *
2666  *-------------------------------------------------------------------------*/
2667
2668 static struct sk_buff *
2669 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
2670 {
2671         int                     padlen;
2672         struct sk_buff          *skb2;
2673
2674         padlen = 2;
2675         if (!skb_cloned (skb)) {
2676                 int     tailroom = skb_tailroom (skb);
2677                 if ((padlen + 4) <= tailroom)
2678                         goto done;
2679         }
2680         skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
2681         dev_kfree_skb_any (skb);
2682         skb = skb2;
2683         if (skb) {
2684                 u32             fcs;
2685 done:
2686                 fcs = crc32_le (~0, skb->data, skb->len);
2687                 fcs = ~fcs;
2688
2689                 *skb_put (skb, 1) = fcs       & 0xff;
2690                 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
2691                 *skb_put (skb, 1) = (fcs>>16) & 0xff;
2692                 *skb_put (skb, 1) = (fcs>>24) & 0xff;
2693         }
2694         return skb;
2695 }
2696
2697 static const struct driver_info zaurus_sl5x00_info = {
2698         .description =  "Sharp Zaurus SL-5x00",
2699         .flags =        FLAG_FRAMING_Z,
2700         .check_connect = always_connected,
2701         .bind =         generic_cdc_bind,
2702         .unbind =       cdc_unbind,
2703         .tx_fixup =     zaurus_tx_fixup,
2704 };
2705 #define ZAURUS_STRONGARM_INFO   ((unsigned long)&zaurus_sl5x00_info)
2706
2707 static const struct driver_info zaurus_pxa_info = {
2708         .description =  "Sharp Zaurus, PXA-2xx based",
2709         .flags =        FLAG_FRAMING_Z,
2710         .check_connect = always_connected,
2711         .bind =         generic_cdc_bind,
2712         .unbind =       cdc_unbind,
2713         .tx_fixup =     zaurus_tx_fixup,
2714 };
2715 #define ZAURUS_PXA_INFO         ((unsigned long)&zaurus_pxa_info)
2716
2717 static const struct driver_info zaurus_pxa_mdlm_info = {
2718         .description =  "Sharp Zaurus, PXA-255 based",
2719         .flags =        FLAG_FRAMING_Z,
2720         .check_connect = always_connected,
2721         .tx_fixup =     zaurus_tx_fixup,
2722 };
2723
2724 static const struct driver_info olympus_mxl_info = {
2725         .description =  "Olympus R1000",
2726         .flags =        FLAG_FRAMING_Z,
2727         .check_connect = always_connected,
2728         .bind =         generic_cdc_bind,
2729         .unbind =       cdc_unbind,
2730         .tx_fixup =     zaurus_tx_fixup,
2731 };
2732 #define OLYMPUS_MXL_INFO        ((unsigned long)&olympus_mxl_info)
2733
2734 #else
2735
2736 /* blacklist all those devices */
2737 #define ZAURUS_STRONGARM_INFO   0
2738 #define ZAURUS_PXA_INFO         0
2739 #define OLYMPUS_MXL_INFO        0
2740
2741 #endif
2742
2743 \f
2744 /*-------------------------------------------------------------------------
2745  *
2746  * Network Device Driver (peer link to "Host Device", from USB host)
2747  *
2748  *-------------------------------------------------------------------------*/
2749
2750 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
2751 {
2752         struct usbnet   *dev = netdev_priv(net);
2753
2754         if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
2755                 return -EINVAL;
2756 #ifdef  CONFIG_USB_NET1080
2757         if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) {
2758                 if (FRAMED_SIZE (new_mtu) > MAX_PACKET)
2759                         return -EINVAL;
2760         }
2761 #endif
2762 #ifdef  CONFIG_USB_GENESYS
2763         if (((dev->driver_info->flags) & FLAG_FRAMING_GL)
2764                         && new_mtu > GL_MAX_PACKET_LEN)
2765                 return -EINVAL;
2766 #endif
2767         // no second zero-length packet read wanted after mtu-sized packets
2768         if (((new_mtu + sizeof (struct ethhdr)) % dev->maxpacket) == 0)
2769                 return -EDOM;
2770         net->mtu = new_mtu;
2771         return 0;
2772 }
2773
2774 /*-------------------------------------------------------------------------*/
2775
2776 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
2777 {
2778         struct usbnet   *dev = netdev_priv(net);
2779         return &dev->stats;
2780 }
2781
2782 /*-------------------------------------------------------------------------*/
2783
2784 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
2785  * completion callbacks.  2.5 should have fixed those bugs...
2786  */
2787
2788 static void defer_bh (struct usbnet *dev, struct sk_buff *skb)
2789 {
2790         struct sk_buff_head     *list = skb->list;
2791         unsigned long           flags;
2792
2793         spin_lock_irqsave (&list->lock, flags);
2794         __skb_unlink (skb, list);
2795         spin_unlock (&list->lock);
2796         spin_lock (&dev->done.lock);
2797         __skb_queue_tail (&dev->done, skb);
2798         if (dev->done.qlen == 1)
2799                 tasklet_schedule (&dev->bh);
2800         spin_unlock_irqrestore (&dev->done.lock, flags);
2801 }
2802
2803 /* some work can't be done in tasklets, so we use keventd
2804  *
2805  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
2806  * but tasklet_schedule() doesn't.  hope the failure is rare.
2807  */
2808 static void defer_kevent (struct usbnet *dev, int work)
2809 {
2810         set_bit (work, &dev->flags);
2811         if (!schedule_work (&dev->kevent))
2812                 deverr (dev, "kevent %d may have been dropped", work);
2813         else
2814                 devdbg (dev, "kevent %d scheduled", work);
2815 }
2816
2817 /*-------------------------------------------------------------------------*/
2818
2819 static void rx_complete (struct urb *urb, struct pt_regs *regs);
2820
2821 static void rx_submit (struct usbnet *dev, struct urb *urb, int flags)
2822 {
2823         struct sk_buff          *skb;
2824         struct skb_data         *entry;
2825         int                     retval = 0;
2826         unsigned long           lockflags;
2827         size_t                  size;
2828
2829 #ifdef CONFIG_USB_NET1080
2830         if (dev->driver_info->flags & FLAG_FRAMING_NC)
2831                 size = FRAMED_SIZE (dev->net->mtu);
2832         else
2833 #endif
2834 #ifdef CONFIG_USB_GENESYS
2835         if (dev->driver_info->flags & FLAG_FRAMING_GL)
2836                 size = GL_RCV_BUF_SIZE;
2837         else
2838 #endif
2839 #ifdef CONFIG_USB_ZAURUS
2840         if (dev->driver_info->flags & FLAG_FRAMING_Z)
2841                 size = 6 + (sizeof (struct ethhdr) + dev->net->mtu);
2842         else
2843 #endif
2844 #ifdef CONFIG_USB_RNDIS
2845         if (dev->driver_info->flags & FLAG_FRAMING_RN)
2846                 size = RNDIS_MAX_TRANSFER;
2847         else
2848 #endif
2849 #ifdef CONFIG_USB_AX8817X
2850         if (dev->driver_info->flags & FLAG_FRAMING_AX)
2851                 size = 2048;
2852         else
2853 #endif
2854                 size = (sizeof (struct ethhdr) + dev->net->mtu);
2855
2856         if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
2857                 if (netif_msg_rx_err (dev))
2858                         devdbg (dev, "no rx skb");
2859                 defer_kevent (dev, EVENT_RX_MEMORY);
2860                 usb_free_urb (urb);
2861                 return;
2862         }
2863         skb_reserve (skb, NET_IP_ALIGN);
2864
2865         entry = (struct skb_data *) skb->cb;
2866         entry->urb = urb;
2867         entry->dev = dev;
2868         entry->state = rx_start;
2869         entry->length = 0;
2870
2871         usb_fill_bulk_urb (urb, dev->udev, dev->in,
2872                 skb->data, size, rx_complete, skb);
2873         urb->transfer_flags |= URB_ASYNC_UNLINK;
2874
2875         spin_lock_irqsave (&dev->rxq.lock, lockflags);
2876
2877         if (netif_running (dev->net)
2878                         && netif_device_present (dev->net)
2879                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
2880                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
2881                 case -EPIPE:
2882                         defer_kevent (dev, EVENT_RX_HALT);
2883                         break;
2884                 case -ENOMEM:
2885                         defer_kevent (dev, EVENT_RX_MEMORY);
2886                         break;
2887                 case -ENODEV:
2888                         if (netif_msg_ifdown (dev))
2889                                 devdbg (dev, "device gone");
2890                         netif_device_detach (dev->net);
2891                         break;
2892                 default:
2893                         if (netif_msg_rx_err (dev))
2894                                 devdbg (dev, "rx submit, %d", retval);
2895                         tasklet_schedule (&dev->bh);
2896                         break;
2897                 case 0:
2898                         __skb_queue_tail (&dev->rxq, skb);
2899                 }
2900         } else {
2901                 if (netif_msg_ifdown (dev))
2902                         devdbg (dev, "rx: stopped");
2903                 retval = -ENOLINK;
2904         }
2905         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
2906         if (retval) {
2907                 dev_kfree_skb_any (skb);
2908                 usb_free_urb (urb);
2909         }
2910 }
2911
2912
2913 /*-------------------------------------------------------------------------*/
2914
2915 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
2916 {
2917         if (dev->driver_info->rx_fixup
2918                         && !dev->driver_info->rx_fixup (dev, skb))
2919                 goto error;
2920         // else network stack removes extra byte if we forced a short packet
2921
2922         if (skb->len)
2923                 skb_return (dev, skb);
2924         else {
2925                 if (netif_msg_rx_err (dev))
2926                         devdbg (dev, "drop");
2927 error:
2928                 dev->stats.rx_errors++;
2929                 skb_queue_tail (&dev->done, skb);
2930         }
2931 }
2932
2933 /*-------------------------------------------------------------------------*/
2934
2935 static void rx_complete (struct urb *urb, struct pt_regs *regs)
2936 {
2937         struct sk_buff          *skb = (struct sk_buff *) urb->context;
2938         struct skb_data         *entry = (struct skb_data *) skb->cb;
2939         struct usbnet           *dev = entry->dev;
2940         int                     urb_status = urb->status;
2941
2942         skb_put (skb, urb->actual_length);
2943         entry->state = rx_done;
2944         entry->urb = NULL;
2945
2946         switch (urb_status) {
2947             // success
2948             case 0:
2949                 if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) {
2950                         entry->state = rx_cleanup;
2951                         dev->stats.rx_errors++;
2952                         dev->stats.rx_length_errors++;
2953                         if (netif_msg_rx_err (dev))
2954                                 devdbg (dev, "rx length %d", skb->len);
2955                 }
2956                 break;
2957
2958             // stalls need manual reset. this is rare ... except that
2959             // when going through USB 2.0 TTs, unplug appears this way.
2960             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
2961             // storm, recovering as needed.
2962             case -EPIPE:
2963                 dev->stats.rx_errors++;
2964                 defer_kevent (dev, EVENT_RX_HALT);
2965                 // FALLTHROUGH
2966
2967             // software-driven interface shutdown
2968             case -ECONNRESET:           // async unlink
2969             case -ESHUTDOWN:            // hardware gone
2970                 if (netif_msg_ifdown (dev))
2971                         devdbg (dev, "rx shutdown, code %d", urb_status);
2972                 goto block;
2973
2974             // we get controller i/o faults during khubd disconnect() delays.
2975             // throttle down resubmits, to avoid log floods; just temporarily,
2976             // so we still recover when the fault isn't a khubd delay.
2977             case -EPROTO:               // ehci
2978             case -ETIMEDOUT:            // ohci
2979             case -EILSEQ:               // uhci
2980                 dev->stats.rx_errors++;
2981                 if (!timer_pending (&dev->delay)) {
2982                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
2983                         if (netif_msg_link (dev))
2984                                 devdbg (dev, "rx throttle %d", urb_status);
2985                 }
2986 block:
2987                 entry->state = rx_cleanup;
2988                 entry->urb = urb;
2989                 urb = NULL;
2990                 break;
2991
2992             // data overrun ... flush fifo?
2993             case -EOVERFLOW:
2994                 dev->stats.rx_over_errors++;
2995                 // FALLTHROUGH
2996             
2997             default:
2998                 entry->state = rx_cleanup;
2999                 dev->stats.rx_errors++;
3000                 if (netif_msg_rx_err (dev))
3001                         devdbg (dev, "rx status %d", urb_status);
3002                 break;
3003         }
3004
3005         defer_bh (dev, skb);
3006
3007         if (urb) {
3008                 if (netif_running (dev->net)
3009                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
3010                         rx_submit (dev, urb, GFP_ATOMIC);
3011                         return;
3012                 }
3013                 usb_free_urb (urb);
3014         }
3015         if (netif_msg_rx_err (dev))
3016                 devdbg (dev, "no read resubmitted");
3017 }
3018
3019 static void intr_complete (struct urb *urb, struct pt_regs *regs)
3020 {
3021         struct usbnet   *dev = urb->context;
3022         int             status = urb->status;
3023
3024         switch (status) {
3025             /* success */
3026             case 0:
3027                 dev->driver_info->status(dev, urb);
3028                 break;
3029
3030             /* software-driven interface shutdown */
3031             case -ENOENT:               // urb killed
3032             case -ESHUTDOWN:            // hardware gone
3033                 if (netif_msg_ifdown (dev))
3034                         devdbg (dev, "intr shutdown, code %d", status);
3035                 return;
3036
3037             /* NOTE:  not throttling like RX/TX, since this endpoint
3038              * already polls infrequently
3039              */
3040             default:
3041                 devdbg (dev, "intr status %d", status);
3042                 break;
3043         }
3044
3045         if (!netif_running (dev->net))
3046                 return;
3047
3048         memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
3049         status = usb_submit_urb (urb, GFP_ATOMIC);
3050         if (status != 0 && netif_msg_timer (dev))
3051                 deverr(dev, "intr resubmit --> %d", status);
3052 }
3053
3054 /*-------------------------------------------------------------------------*/
3055
3056 // unlink pending rx/tx; completion handlers do all other cleanup
3057
3058 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
3059 {
3060         unsigned long           flags;
3061         struct sk_buff          *skb, *skbnext;
3062         int                     count = 0;
3063
3064         spin_lock_irqsave (&q->lock, flags);
3065         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
3066                 struct skb_data         *entry;
3067                 struct urb              *urb;
3068                 int                     retval;
3069
3070                 entry = (struct skb_data *) skb->cb;
3071                 urb = entry->urb;
3072                 skbnext = skb->next;
3073
3074                 // during some PM-driven resume scenarios,
3075                 // these (async) unlinks complete immediately
3076                 retval = usb_unlink_urb (urb);
3077                 if (retval != -EINPROGRESS && retval != 0)
3078                         devdbg (dev, "unlink urb err, %d", retval);
3079                 else
3080                         count++;
3081         }
3082         spin_unlock_irqrestore (&q->lock, flags);
3083         return count;
3084 }
3085
3086
3087 /*-------------------------------------------------------------------------*/
3088
3089 // precondition: never called in_interrupt
3090
3091 static int usbnet_stop (struct net_device *net)
3092 {
3093         struct usbnet           *dev = netdev_priv(net);
3094         int                     temp;
3095         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
3096         DECLARE_WAITQUEUE (wait, current);
3097
3098         netif_stop_queue (net);
3099
3100         if (netif_msg_ifdown (dev))
3101                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
3102                         dev->stats.rx_packets, dev->stats.tx_packets, 
3103                         dev->stats.rx_errors, dev->stats.tx_errors
3104                         );
3105
3106         // ensure there are no more active urbs
3107         add_wait_queue (&unlink_wakeup, &wait);
3108         dev->wait = &unlink_wakeup;
3109         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
3110
3111         // maybe wait for deletions to finish.
3112         while (skb_queue_len (&dev->rxq)
3113                         && skb_queue_len (&dev->txq)
3114                         && skb_queue_len (&dev->done)) {
3115                 msleep(UNLINK_TIMEOUT_MS);
3116                 if (netif_msg_ifdown (dev))
3117                         devdbg (dev, "waited for %d urb completions", temp);
3118         }
3119         dev->wait = NULL;
3120         remove_wait_queue (&unlink_wakeup, &wait); 
3121
3122         usb_kill_urb(dev->interrupt);
3123
3124         /* deferred work (task, timer, softirq) must also stop.
3125          * can't flush_scheduled_work() until we drop rtnl (later),
3126          * else workers could deadlock; so make workers a NOP.
3127          */
3128         dev->flags = 0;
3129         del_timer_sync (&dev->delay);
3130         tasklet_kill (&dev->bh);
3131
3132         return 0;
3133 }
3134
3135 /*-------------------------------------------------------------------------*/
3136
3137 // posts reads, and enables write queuing
3138
3139 // precondition: never called in_interrupt
3140
3141 static int usbnet_open (struct net_device *net)
3142 {
3143         struct usbnet           *dev = netdev_priv(net);
3144         int                     retval = 0;
3145         struct driver_info      *info = dev->driver_info;
3146
3147         // put into "known safe" state
3148         if (info->reset && (retval = info->reset (dev)) < 0) {
3149                 if (netif_msg_ifup (dev))
3150                         devinfo (dev,
3151                                 "open reset fail (%d) usbnet usb-%s-%s, %s",
3152                                 retval,
3153                                 dev->udev->bus->bus_name, dev->udev->devpath,
3154                         info->description);
3155                 goto done;
3156         }
3157
3158         // insist peer be connected
3159         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
3160                 if (netif_msg_ifup (dev))
3161                         devdbg (dev, "can't open; %d", retval);
3162                 goto done;
3163         }
3164
3165         /* start any status interrupt transfer */
3166         if (dev->interrupt) {
3167                 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
3168                 if (retval < 0) {
3169                         if (netif_msg_ifup (dev))
3170                                 deverr (dev, "intr submit %d", retval);
3171                         goto done;
3172                 }
3173         }
3174
3175         netif_start_queue (net);
3176         if (netif_msg_ifup (dev)) {
3177                 char    *framing;
3178
3179                 if (dev->driver_info->flags & FLAG_FRAMING_NC)
3180                         framing = "NetChip";
3181                 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
3182                         framing = "GeneSys";
3183                 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
3184                         framing = "Zaurus";
3185                 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
3186                         framing = "RNDIS";
3187                 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
3188                         framing = "ASIX";
3189                 else
3190                         framing = "simple";
3191
3192                 devinfo (dev, "open: enable queueing "
3193                                 "(rx %d, tx %d) mtu %d %s framing",
3194                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
3195                         framing);
3196         }
3197
3198         // delay posting reads until we're fully open
3199         tasklet_schedule (&dev->bh);
3200 done:
3201         return retval;
3202 }
3203
3204 /*-------------------------------------------------------------------------*/
3205
3206 static void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
3207 {
3208         struct usbnet *dev = netdev_priv(net);
3209
3210         strncpy (info->driver, driver_name, sizeof info->driver);
3211         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
3212         strncpy (info->fw_version, dev->driver_info->description,
3213                 sizeof info->fw_version);
3214         usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
3215 }
3216
3217 static u32 usbnet_get_link (struct net_device *net)
3218 {
3219         struct usbnet *dev = netdev_priv(net);
3220
3221         /* If a check_connect is defined, return it's results */
3222         if (dev->driver_info->check_connect)
3223                 return dev->driver_info->check_connect (dev) == 0;
3224
3225         /* Otherwise, we're up to avoid breaking scripts */
3226         return 1;
3227 }
3228
3229 static u32 usbnet_get_msglevel (struct net_device *net)
3230 {
3231         struct usbnet *dev = netdev_priv(net);
3232
3233         return dev->msg_enable;
3234 }
3235
3236 static void usbnet_set_msglevel (struct net_device *net, u32 level)
3237 {
3238         struct usbnet *dev = netdev_priv(net);
3239
3240         dev->msg_enable = level;
3241 }
3242
3243 static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
3244 {
3245 #ifdef NEED_MII
3246         {
3247         struct usbnet *dev = netdev_priv(net);
3248
3249         if (dev->mii.mdio_read != NULL && dev->mii.mdio_write != NULL)
3250                 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
3251         }
3252 #endif
3253         return -EOPNOTSUPP;
3254 }
3255
3256 /*-------------------------------------------------------------------------*/
3257
3258 /* work that cannot be done in interrupt context uses keventd.
3259  *
3260  * NOTE:  with 2.5 we could do more of this using completion callbacks,
3261  * especially now that control transfers can be queued.
3262  */
3263 static void
3264 kevent (void *data)
3265 {
3266         struct usbnet           *dev = data;
3267         int                     status;
3268
3269         /* usb_clear_halt() needs a thread context */
3270         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
3271                 unlink_urbs (dev, &dev->txq);
3272                 status = usb_clear_halt (dev->udev, dev->out);
3273                 if (status < 0 && status != -EPIPE) {
3274                         if (netif_msg_tx_err (dev))
3275                                 deverr (dev, "can't clear tx halt, status %d",
3276                                         status);
3277                 } else {
3278                         clear_bit (EVENT_TX_HALT, &dev->flags);
3279                         netif_wake_queue (dev->net);
3280                 }
3281         }
3282         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
3283                 unlink_urbs (dev, &dev->rxq);
3284                 status = usb_clear_halt (dev->udev, dev->in);
3285                 if (status < 0 && status != -EPIPE) {
3286                         if (netif_msg_rx_err (dev))
3287                                 deverr (dev, "can't clear rx halt, status %d",
3288                                         status);
3289                 } else {
3290                         clear_bit (EVENT_RX_HALT, &dev->flags);
3291                         tasklet_schedule (&dev->bh);
3292                 }
3293         }
3294
3295         /* tasklet could resubmit itself forever if memory is tight */
3296         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
3297                 struct urb      *urb = NULL;
3298
3299                 if (netif_running (dev->net))
3300                         urb = usb_alloc_urb (0, GFP_KERNEL);
3301                 else
3302                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
3303                 if (urb != NULL) {
3304                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
3305                         rx_submit (dev, urb, GFP_KERNEL);
3306                         tasklet_schedule (&dev->bh);
3307                 }
3308         }
3309
3310         if (dev->flags)
3311                 devdbg (dev, "kevent done, flags = 0x%lx",
3312                         dev->flags);
3313 }
3314
3315 /*-------------------------------------------------------------------------*/
3316
3317 static void tx_complete (struct urb *urb, struct pt_regs *regs)
3318 {
3319         struct sk_buff          *skb = (struct sk_buff *) urb->context;
3320         struct skb_data         *entry = (struct skb_data *) skb->cb;
3321         struct usbnet           *dev = entry->dev;
3322
3323         if (urb->status == 0) {
3324                 dev->stats.tx_packets++;
3325                 dev->stats.tx_bytes += entry->length;
3326         } else {
3327                 dev->stats.tx_errors++;
3328
3329                 switch (urb->status) {
3330                 case -EPIPE:
3331                         defer_kevent (dev, EVENT_TX_HALT);
3332                         break;
3333
3334                 /* software-driven interface shutdown */
3335                 case -ECONNRESET:               // async unlink
3336                 case -ESHUTDOWN:                // hardware gone
3337                         break;
3338
3339                 // like rx, tx gets controller i/o faults during khubd delays
3340                 // and so it uses the same throttling mechanism.
3341                 case -EPROTO:           // ehci
3342                 case -ETIMEDOUT:        // ohci
3343                 case -EILSEQ:           // uhci
3344                         if (!timer_pending (&dev->delay)) {
3345                                 mod_timer (&dev->delay,
3346                                         jiffies + THROTTLE_JIFFIES);
3347                                 if (netif_msg_link (dev))
3348                                         devdbg (dev, "tx throttle %d",
3349                                                         urb->status);
3350                         }
3351                         netif_stop_queue (dev->net);
3352                         break;
3353                 default:
3354                         if (netif_msg_tx_err (dev))
3355                                 devdbg (dev, "tx err %d", entry->urb->status);
3356                         break;
3357                 }
3358         }
3359
3360         urb->dev = NULL;
3361         entry->state = tx_done;
3362         defer_bh (dev, skb);
3363 }
3364
3365 /*-------------------------------------------------------------------------*/
3366
3367 static void usbnet_tx_timeout (struct net_device *net)
3368 {
3369         struct usbnet           *dev = netdev_priv(net);
3370
3371         unlink_urbs (dev, &dev->txq);
3372         tasklet_schedule (&dev->bh);
3373
3374         // FIXME: device recovery -- reset?
3375 }
3376
3377 /*-------------------------------------------------------------------------*/
3378
3379 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
3380 {
3381         struct usbnet           *dev = netdev_priv(net);
3382         int                     length;
3383         int                     retval = NET_XMIT_SUCCESS;
3384         struct urb              *urb = NULL;
3385         struct skb_data         *entry;
3386         struct driver_info      *info = dev->driver_info;
3387         unsigned long           flags;
3388 #ifdef  CONFIG_USB_NET1080
3389         struct nc_header        *header = NULL;
3390         struct nc_trailer       *trailer = NULL;
3391 #endif  /* CONFIG_USB_NET1080 */
3392
3393         // some devices want funky USB-level framing, for
3394         // win32 driver (usually) and/or hardware quirks
3395         if (info->tx_fixup) {
3396                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
3397                 if (!skb) {
3398                         if (netif_msg_tx_err (dev))
3399                                 devdbg (dev, "can't tx_fixup skb");
3400                         goto drop;
3401                 }
3402         }
3403         length = skb->len;
3404
3405         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
3406                 if (netif_msg_tx_err (dev))
3407                         devdbg (dev, "no urb");
3408                 goto drop;
3409         }
3410
3411         entry = (struct skb_data *) skb->cb;
3412         entry->urb = urb;
3413         entry->dev = dev;
3414         entry->state = tx_start;
3415         entry->length = length;
3416
3417         // FIXME: reorganize a bit, so that fixup() fills out NetChip
3418         // framing too. (Packet ID update needs the spinlock...)
3419         // [ BETTER:  we already own net->xmit_lock, that's enough ]
3420
3421 #ifdef  CONFIG_USB_NET1080
3422         if (info->flags & FLAG_FRAMING_NC) {
3423                 header = (struct nc_header *) skb_push (skb, sizeof *header);
3424                 header->hdr_len = cpu_to_le16 (sizeof (*header));
3425                 header->packet_len = cpu_to_le16 (length);
3426                 if (!((skb->len + sizeof *trailer) & 0x01))
3427                         *skb_put (skb, 1) = PAD_BYTE;
3428                 trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
3429         }
3430 #endif  /* CONFIG_USB_NET1080 */
3431
3432         usb_fill_bulk_urb (urb, dev->udev, dev->out,
3433                         skb->data, skb->len, tx_complete, skb);
3434         urb->transfer_flags |= URB_ASYNC_UNLINK;
3435
3436         /* don't assume the hardware handles USB_ZERO_PACKET
3437          * NOTE:  strictly conforming cdc-ether devices should expect
3438          * the ZLP here, but ignore the one-byte packet.
3439          *
3440          * FIXME zero that byte, if it doesn't require a new skb.
3441          */
3442         if ((length % dev->maxpacket) == 0)
3443                 urb->transfer_buffer_length++;
3444
3445         spin_lock_irqsave (&dev->txq.lock, flags);
3446
3447 #ifdef  CONFIG_USB_NET1080
3448         if (info->flags & FLAG_FRAMING_NC) {
3449                 header->packet_id = cpu_to_le16 ((u16)dev->dev_packet_id++);
3450                 put_unaligned (header->packet_id, &trailer->packet_id);
3451 #if 0
3452                 devdbg (dev, "frame >tx h %d p %d id %d",
3453                         header->hdr_len, header->packet_len,
3454                         header->packet_id);
3455 #endif
3456         }
3457 #endif  /* CONFIG_USB_NET1080 */
3458
3459         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
3460         case -EPIPE:
3461                 netif_stop_queue (net);
3462                 defer_kevent (dev, EVENT_TX_HALT);
3463                 break;
3464         default:
3465                 if (netif_msg_tx_err (dev))
3466                         devdbg (dev, "tx: submit urb err %d", retval);
3467                 break;
3468         case 0:
3469                 net->trans_start = jiffies;
3470                 __skb_queue_tail (&dev->txq, skb);
3471                 if (dev->txq.qlen >= TX_QLEN (dev))
3472                         netif_stop_queue (net);
3473         }
3474         spin_unlock_irqrestore (&dev->txq.lock, flags);
3475
3476         if (retval) {
3477                 if (netif_msg_tx_err (dev))
3478                         devdbg (dev, "drop, code %d", retval);
3479 drop:
3480                 retval = NET_XMIT_SUCCESS;
3481                 dev->stats.tx_dropped++;
3482                 if (skb)
3483                         dev_kfree_skb_any (skb);
3484                 usb_free_urb (urb);
3485         } else if (netif_msg_tx_queued (dev)) {
3486                 devdbg (dev, "> tx, len %d, type 0x%x",
3487                         length, skb->protocol);
3488         }
3489         return retval;
3490 }
3491
3492
3493 /*-------------------------------------------------------------------------*/
3494
3495 // tasklet (work deferred from completions, in_irq) or timer
3496
3497 static void usbnet_bh (unsigned long param)
3498 {
3499         struct usbnet           *dev = (struct usbnet *) param;
3500         struct sk_buff          *skb;
3501         struct skb_data         *entry;
3502
3503         while ((skb = skb_dequeue (&dev->done))) {
3504                 entry = (struct skb_data *) skb->cb;
3505                 switch (entry->state) {
3506                     case rx_done:
3507                         entry->state = rx_cleanup;
3508                         rx_process (dev, skb);
3509                         continue;
3510                     case tx_done:
3511                     case rx_cleanup:
3512                         usb_free_urb (entry->urb);
3513                         dev_kfree_skb (skb);
3514                         continue;
3515                     default:
3516                         devdbg (dev, "bogus skb state %d", entry->state);
3517                 }
3518         }
3519
3520         // waiting for all pending urbs to complete?
3521         if (dev->wait) {
3522                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
3523                         wake_up (dev->wait);
3524                 }
3525
3526         // or are we maybe short a few urbs?
3527         } else if (netif_running (dev->net)
3528                         && netif_device_present (dev->net)
3529                         && !timer_pending (&dev->delay)
3530                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
3531                 int     temp = dev->rxq.qlen;
3532                 int     qlen = RX_QLEN (dev);
3533
3534                 if (temp < qlen) {
3535                         struct urb      *urb;
3536                         int             i;
3537
3538                         // don't refill the queue all at once
3539                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
3540                                 urb = usb_alloc_urb (0, GFP_ATOMIC);
3541                                 if (urb != NULL)
3542                                         rx_submit (dev, urb, GFP_ATOMIC);
3543                         }
3544                         if (temp != dev->rxq.qlen && netif_msg_link (dev))
3545                                 devdbg (dev, "rxqlen %d --> %d",
3546                                                 temp, dev->rxq.qlen);
3547                         if (dev->rxq.qlen < qlen)
3548                                 tasklet_schedule (&dev->bh);
3549                 }
3550                 if (dev->txq.qlen < TX_QLEN (dev))
3551                         netif_wake_queue (dev->net);
3552         }
3553 }
3554
3555
3556 \f
3557 /*-------------------------------------------------------------------------
3558  *
3559  * USB Device Driver support
3560  *
3561  *-------------------------------------------------------------------------*/
3562  
3563 // precondition: never called in_interrupt
3564
3565 static void usbnet_disconnect (struct usb_interface *intf)
3566 {
3567         struct usbnet           *dev;
3568         struct usb_device       *xdev;
3569         struct net_device       *net;
3570
3571         dev = usb_get_intfdata(intf);
3572         usb_set_intfdata(intf, NULL);
3573         if (!dev)
3574                 return;
3575
3576         xdev = interface_to_usbdev (intf);
3577
3578         if (netif_msg_probe (dev))
3579                 devinfo (dev, "unregister usbnet usb-%s-%s, %s",
3580                         xdev->bus->bus_name, xdev->devpath,
3581                         dev->driver_info->description);
3582         
3583         net = dev->net;
3584         unregister_netdev (net);
3585
3586         /* we don't hold rtnl here ... */
3587         flush_scheduled_work ();
3588
3589         if (dev->driver_info->unbind)
3590                 dev->driver_info->unbind (dev, intf);
3591
3592         free_netdev(net);
3593         usb_put_dev (xdev);
3594 }
3595
3596
3597 /*-------------------------------------------------------------------------*/
3598
3599 static struct ethtool_ops usbnet_ethtool_ops;
3600
3601 // precondition: never called in_interrupt
3602
3603 static int
3604 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
3605 {
3606         struct usbnet                   *dev;
3607         struct net_device               *net;
3608         struct usb_host_interface       *interface;
3609         struct driver_info              *info;
3610         struct usb_device               *xdev;
3611         int                             status;
3612
3613         info = (struct driver_info *) prod->driver_info;
3614         if (!info) {
3615                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
3616                 return -ENODEV;
3617         }
3618         xdev = interface_to_usbdev (udev);
3619         interface = udev->cur_altsetting;
3620
3621         usb_get_dev (xdev);
3622
3623         status = -ENOMEM;
3624
3625         // set up our own records
3626         net = alloc_etherdev(sizeof(*dev));
3627         if (!net) {
3628                 dbg ("can't kmalloc dev");
3629                 goto out;
3630         }
3631
3632         dev = netdev_priv(net);
3633         dev->udev = xdev;
3634         dev->driver_info = info;
3635         dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
3636                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
3637         skb_queue_head_init (&dev->rxq);
3638         skb_queue_head_init (&dev->txq);
3639         skb_queue_head_init (&dev->done);
3640         dev->bh.func = usbnet_bh;
3641         dev->bh.data = (unsigned long) dev;
3642         INIT_WORK (&dev->kevent, kevent, dev);
3643         dev->delay.function = usbnet_bh;
3644         dev->delay.data = (unsigned long) dev;
3645         init_timer (&dev->delay);
3646
3647         SET_MODULE_OWNER (net);
3648         dev->net = net;
3649         strcpy (net->name, "usb%d");
3650         memcpy (net->dev_addr, node_id, sizeof node_id);
3651
3652 #if 0
3653 // dma_supported() is deeply broken on almost all architectures
3654         // possible with some EHCI controllers
3655         if (dma_supported (&udev->dev, DMA_64BIT_MASK))
3656                 net->features |= NETIF_F_HIGHDMA;
3657 #endif
3658
3659         net->change_mtu = usbnet_change_mtu;
3660         net->get_stats = usbnet_get_stats;
3661         net->hard_start_xmit = usbnet_start_xmit;
3662         net->open = usbnet_open;
3663         net->stop = usbnet_stop;
3664         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
3665         net->tx_timeout = usbnet_tx_timeout;
3666         net->do_ioctl = usbnet_ioctl;
3667         net->ethtool_ops = &usbnet_ethtool_ops;
3668
3669         // allow device-specific bind/init procedures
3670         // NOTE net->name still not usable ...
3671         if (info->bind) {
3672                 status = info->bind (dev, udev);
3673                 // heuristic:  "usb%d" for links we know are two-host,
3674                 // else "eth%d" when there's reasonable doubt.  userspace
3675                 // can rename the link if it knows better.
3676                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
3677                                 && (net->dev_addr [0] & 0x02) == 0)
3678                         strcpy (net->name, "eth%d");
3679         } else if (!info->in || info->out)
3680                 status = get_endpoints (dev, udev);
3681         else {
3682                 dev->in = usb_rcvbulkpipe (xdev, info->in);
3683                 dev->out = usb_sndbulkpipe (xdev, info->out);
3684                 if (!(info->flags & FLAG_NO_SETINT))
3685                         status = usb_set_interface (xdev,
3686                                 interface->desc.bInterfaceNumber,
3687                                 interface->desc.bAlternateSetting);
3688                 else
3689                         status = 0;
3690
3691         }
3692
3693         if (status == 0 && dev->status)
3694                 status = init_status (dev, udev);
3695         if (status < 0)
3696                 goto out1;
3697
3698         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
3699         
3700         SET_NETDEV_DEV(net, &udev->dev);
3701         status = register_netdev (net);
3702         if (status)
3703                 goto out3;
3704         if (netif_msg_probe (dev))
3705                 devinfo (dev, "register usbnet at usb-%s-%s, %s, "
3706                                 "%02x:%02x:%02x:%02x:%02x:%02x",
3707                         xdev->bus->bus_name, xdev->devpath,
3708                         dev->driver_info->description,
3709                         net->dev_addr [0], net->dev_addr [1],
3710                         net->dev_addr [2], net->dev_addr [3],
3711                         net->dev_addr [4], net->dev_addr [5]);
3712
3713         // ok, it's ready to go.
3714         usb_set_intfdata (udev, dev);
3715
3716         // start as if the link is up
3717         netif_device_attach (net);
3718
3719         return 0;
3720
3721 out3:
3722         if (info->unbind)
3723                 info->unbind (dev, udev);
3724 out1:
3725         free_netdev(net);
3726 out:
3727         usb_put_dev(xdev);
3728         return status;
3729 }
3730
3731 /*-------------------------------------------------------------------------*/
3732
3733 #ifdef  CONFIG_PM
3734
3735 static int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
3736 {
3737         struct usbnet           *dev = usb_get_intfdata(intf);
3738         
3739         /* accelerate emptying of the rx and queues, to avoid
3740          * having everything error out.
3741          */
3742         netif_device_detach (dev->net);
3743         (void) unlink_urbs (dev, &dev->rxq);
3744         (void) unlink_urbs (dev, &dev->txq);
3745         intf->dev.power.power_state = PMSG_SUSPEND;
3746         return 0;
3747 }
3748
3749 static int usbnet_resume (struct usb_interface *intf)
3750 {
3751         struct usbnet           *dev = usb_get_intfdata(intf);
3752
3753         intf->dev.power.power_state = PMSG_ON;
3754         netif_device_attach (dev->net);
3755         tasklet_schedule (&dev->bh);
3756         return 0;
3757 }
3758
3759 #else   /* !CONFIG_PM */
3760
3761 #define usbnet_suspend  NULL
3762 #define usbnet_resume   NULL
3763
3764 #endif  /* CONFIG_PM */
3765
3766 /*-------------------------------------------------------------------------*/
3767
3768 #ifndef HAVE_HARDWARE
3769 #error You need to configure some hardware for this driver
3770 #endif
3771
3772 /*
3773  * chip vendor names won't normally be on the cables, and
3774  * may not be on the device.
3775  */
3776
3777 static const struct usb_device_id       products [] = {
3778
3779 #ifdef  CONFIG_USB_ALI_M5632
3780 {
3781         USB_DEVICE (0x0402, 0x5632),    // ALi defaults
3782         .driver_info =  (unsigned long) &ali_m5632_info,
3783 },
3784 #endif
3785
3786 #ifdef  CONFIG_USB_AN2720
3787 {
3788         USB_DEVICE (0x0547, 0x2720),    // AnchorChips defaults
3789         .driver_info =  (unsigned long) &an2720_info,
3790 }, {
3791         USB_DEVICE (0x0547, 0x2727),    // Xircom PGUNET
3792         .driver_info =  (unsigned long) &an2720_info,
3793 },
3794 #endif
3795
3796 #ifdef  CONFIG_USB_BELKIN
3797 {
3798         USB_DEVICE (0x050d, 0x0004),    // Belkin
3799         .driver_info =  (unsigned long) &belkin_info,
3800 }, {
3801         USB_DEVICE (0x056c, 0x8100),    // eTEK
3802         .driver_info =  (unsigned long) &belkin_info,
3803 }, {
3804         USB_DEVICE (0x0525, 0x9901),    // Advance USBNET (eTEK)
3805         .driver_info =  (unsigned long) &belkin_info,
3806 },
3807 #endif
3808
3809 #ifdef CONFIG_USB_AX8817X
3810 {
3811         // Linksys USB200M
3812         USB_DEVICE (0x077b, 0x2226),
3813         .driver_info =  (unsigned long) &ax8817x_info,
3814 }, {
3815         // Netgear FA120
3816         USB_DEVICE (0x0846, 0x1040),
3817         .driver_info =  (unsigned long) &netgear_fa120_info,
3818 }, {
3819         // DLink DUB-E100
3820         USB_DEVICE (0x2001, 0x1a00),
3821         .driver_info =  (unsigned long) &dlink_dub_e100_info,
3822 }, {
3823         // Intellinet, ST Lab USB Ethernet
3824         USB_DEVICE (0x0b95, 0x1720),
3825         .driver_info =  (unsigned long) &ax8817x_info,
3826 }, {
3827         // Hawking UF200, TrendNet TU2-ET100
3828         USB_DEVICE (0x07b8, 0x420a),
3829         .driver_info =  (unsigned long) &hawking_uf200_info,
3830 }, {
3831         // Billionton Systems, USB2AR 
3832         USB_DEVICE (0x08dd, 0x90ff),
3833         .driver_info =  (unsigned long) &ax8817x_info,
3834 }, {
3835         // ATEN UC210T
3836         USB_DEVICE (0x0557, 0x2009),
3837         .driver_info =  (unsigned long) &ax8817x_info,
3838 }, {
3839         // Buffalo LUA-U2-KTX
3840         USB_DEVICE (0x0411, 0x003d),
3841         .driver_info =  (unsigned long) &ax8817x_info,
3842 }, {
3843         // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
3844         USB_DEVICE (0x6189, 0x182d),
3845         .driver_info =  (unsigned long) &ax8817x_info,
3846 }, {
3847         // corega FEther USB2-TX
3848         USB_DEVICE (0x07aa, 0x0017),
3849         .driver_info =  (unsigned long) &ax8817x_info,
3850 }, {
3851         // Surecom EP-1427X-2
3852         USB_DEVICE (0x1189, 0x0893),
3853         .driver_info = (unsigned long) &ax8817x_info,
3854 }, {
3855         // goodway corp usb gwusb2e
3856         USB_DEVICE (0x1631, 0x6200),
3857         .driver_info = (unsigned long) &ax8817x_info,
3858 }, {
3859         // ASIX AX88772 10/100
3860         USB_DEVICE (0x0b95, 0x7720),
3861         .driver_info = (unsigned long) &ax88772_info,
3862 },
3863 #endif
3864
3865 #ifdef  CONFIG_USB_EPSON2888
3866 {
3867         USB_DEVICE (0x0525, 0x2888),    // EPSON USB client
3868         .driver_info    = (unsigned long) &epson2888_info,
3869 },
3870 #endif
3871
3872 #ifdef  CONFIG_USB_GENESYS
3873 {
3874         USB_DEVICE (0x05e3, 0x0502),    // GL620USB-A
3875         .driver_info =  (unsigned long) &genelink_info,
3876 },
3877         /* NOT: USB_DEVICE (0x05e3, 0x0501),    // GL620USB
3878          * that's half duplex, not currently supported
3879          */
3880 #endif
3881
3882 #ifdef  CONFIG_USB_NET1080
3883 {
3884         USB_DEVICE (0x0525, 0x1080),    // NetChip ref design
3885         .driver_info =  (unsigned long) &net1080_info,
3886 }, {
3887         USB_DEVICE (0x06D0, 0x0622),    // Laplink Gold
3888         .driver_info =  (unsigned long) &net1080_info,
3889 },
3890 #endif
3891
3892 #ifdef CONFIG_USB_PL2301
3893 {
3894         USB_DEVICE (0x067b, 0x0000),    // PL-2301
3895         .driver_info =  (unsigned long) &prolific_info,
3896 }, {
3897         USB_DEVICE (0x067b, 0x0001),    // PL-2302
3898         .driver_info =  (unsigned long) &prolific_info,
3899 },
3900 #endif
3901
3902 #ifdef CONFIG_USB_KC2190
3903 {
3904         USB_DEVICE (0x050f, 0x0190),    // KC-190
3905         .driver_info =  (unsigned long) &kc2190_info,
3906 },
3907 #endif
3908
3909 #ifdef  CONFIG_USB_RNDIS
3910 {
3911         /* RNDIS is MSFT's un-official variant of CDC ACM */
3912         USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
3913         .driver_info = (unsigned long) &rndis_info,
3914 },
3915 #endif
3916
3917 #ifdef  CONFIG_USB_ARMLINUX
3918 /*
3919  * SA-1100 using standard ARM Linux kernels, or compatible.
3920  * Often used when talking to Linux PDAs (iPaq, Yopy, etc).
3921  * The sa-1100 "usb-eth" driver handles the basic framing.
3922  *
3923  * PXA25x or PXA210 ...  these use a "usb-eth" driver much like
3924  * the sa1100 one, but hardware uses different endpoint numbers.
3925  *
3926  * Or the Linux "Ethernet" gadget on hardware that can't talk
3927  * CDC Ethernet (e.g., no altsettings), in either of two modes:
3928  *  - acting just like the old "usb-eth" firmware, though
3929  *    the implementation is different 
3930  *  - supporting RNDIS as the first/default configuration for
3931  *    MS-Windows interop; Linux needs to use the other config
3932  */
3933 {
3934         // 1183 = 0x049F, both used as hex values?
3935         // Compaq "Itsy" vendor/product id
3936         USB_DEVICE (0x049F, 0x505A),    // usb-eth, or compatible
3937         .driver_info =  (unsigned long) &linuxdev_info,
3938 }, {
3939         USB_DEVICE (0x0E7E, 0x1001),    // G.Mate "Yopy"
3940         .driver_info =  (unsigned long) &yopy_info,
3941 }, {
3942         USB_DEVICE (0x8086, 0x07d3),    // "blob" bootloader
3943         .driver_info =  (unsigned long) &blob_info,
3944 }, {
3945         // Linux Ethernet/RNDIS gadget on pxa210/25x/26x
3946         // e.g. Gumstix, current OpenZaurus, ...
3947         USB_DEVICE_VER (0x0525, 0xa4a2, 0x0203, 0x0203),
3948         .driver_info =  (unsigned long) &linuxdev_info,
3949 }, 
3950 #endif
3951
3952 #if     defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER)
3953 /*
3954  * SA-1100 based Sharp Zaurus ("collie"), or compatible.
3955  * Same idea as above, but different framing.
3956  *
3957  * PXA-2xx based models are also lying-about-cdc.
3958  * Some models don't even tell the same lies ...
3959  *
3960  * NOTE:  OpenZaurus versions with 2.6 kernels won't use these entries,
3961  * unlike the older ones with 2.4 "embedix" kernels.
3962  *
3963  * NOTE:  These entries do double-duty, serving as blacklist entries
3964  * whenever Zaurus support isn't enabled, but CDC Ethernet is.
3965  */
3966 #define ZAURUS_MASTER_INTERFACE \
3967         .bInterfaceClass        = USB_CLASS_COMM, \
3968         .bInterfaceSubClass     = USB_CDC_SUBCLASS_ETHERNET, \
3969         .bInterfaceProtocol     = USB_CDC_PROTO_NONE
3970 {
3971         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3972                           | USB_DEVICE_ID_MATCH_DEVICE, 
3973         .idVendor               = 0x04DD,
3974         .idProduct              = 0x8004,
3975         ZAURUS_MASTER_INTERFACE,
3976         .driver_info = ZAURUS_STRONGARM_INFO,
3977 }, {
3978         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3979                           | USB_DEVICE_ID_MATCH_DEVICE, 
3980         .idVendor               = 0x04DD,
3981         .idProduct              = 0x8005,       /* A-300 */
3982         ZAURUS_MASTER_INTERFACE,
3983         .driver_info = ZAURUS_PXA_INFO,
3984 }, {
3985         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3986                           | USB_DEVICE_ID_MATCH_DEVICE, 
3987         .idVendor               = 0x04DD,
3988         .idProduct              = 0x8006,       /* B-500/SL-5600 */
3989         ZAURUS_MASTER_INTERFACE,
3990         .driver_info = ZAURUS_PXA_INFO,
3991 }, {
3992         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
3993                   | USB_DEVICE_ID_MATCH_DEVICE,
3994         .idVendor               = 0x04DD,
3995         .idProduct              = 0x8007,       /* C-700 */
3996         ZAURUS_MASTER_INTERFACE,
3997         .driver_info = ZAURUS_PXA_INFO,
3998 }, {
3999         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4000                  | USB_DEVICE_ID_MATCH_DEVICE,
4001         .idVendor               = 0x04DD,
4002         .idProduct              = 0x9031,       /* C-750 C-760 */
4003         ZAURUS_MASTER_INTERFACE,
4004         .driver_info = ZAURUS_PXA_INFO,
4005 }, {
4006         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4007                  | USB_DEVICE_ID_MATCH_DEVICE,
4008         .idVendor               = 0x04DD,
4009         .idProduct              = 0x9032,       /* SL-6000 */
4010         ZAURUS_MASTER_INTERFACE,
4011         .driver_info = ZAURUS_PXA_INFO,
4012 }, {
4013         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4014                  | USB_DEVICE_ID_MATCH_DEVICE,
4015         .idVendor               = 0x04DD,
4016         /* reported with some C860 units */
4017         .idProduct              = 0x9050,       /* C-860 */
4018         ZAURUS_MASTER_INTERFACE,
4019         .driver_info = ZAURUS_PXA_INFO,
4020 },
4021
4022 #ifdef  CONFIG_USB_ZAURUS
4023         /* at least some (reports vary) PXA units have very different
4024          * lies about their standards support:  they claim to be cell
4025          * phones giving direct radio access (which they aren't).
4026          */
4027 {
4028         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4029                  | USB_DEVICE_ID_MATCH_DEVICE,
4030         .idVendor               = 0x04DD,
4031         /* Sharp ROM v1.32 */
4032         .idProduct              = 0x8006,       /* SL-5600 */
4033         .bInterfaceClass        = USB_CLASS_COMM,
4034         .bInterfaceSubClass     = USB_CDC_SUBCLASS_MDLM,
4035         .bInterfaceProtocol     = USB_CDC_PROTO_NONE,
4036         .driver_info            = (unsigned long) &zaurus_pxa_mdlm_info,
4037 }, {
4038         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4039                  | USB_DEVICE_ID_MATCH_DEVICE,
4040         .idVendor               = 0x04DD,
4041         /* reported with some C860 units */
4042         .idProduct              = 0x9031,       /* C-860 */
4043         .bInterfaceClass        = USB_CLASS_COMM,
4044         .bInterfaceSubClass     = USB_CDC_SUBCLASS_MDLM,
4045         .bInterfaceProtocol     = USB_CDC_PROTO_NONE,
4046         .driver_info            = (unsigned long) &zaurus_pxa_mdlm_info,
4047 },
4048 #endif
4049
4050 /* Olympus has some models with a Zaurus-compatible option.
4051  * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
4052  */
4053 {
4054         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
4055                  | USB_DEVICE_ID_MATCH_DEVICE,
4056         .idVendor               = 0x07B4,
4057         .idProduct              = 0x0F02,       /* R-1000 */
4058         ZAURUS_MASTER_INTERFACE,
4059         .driver_info = OLYMPUS_MXL_INFO,
4060 },
4061 #endif
4062
4063 #ifdef  CONFIG_USB_CDCETHER
4064 {
4065         /* CDC Ether uses two interfaces, not necessarily consecutive.
4066          * We match the main interface, ignoring the optional device
4067          * class so we could handle devices that aren't exclusively
4068          * CDC ether.
4069          *
4070          * NOTE:  this match must come AFTER entries working around
4071          * bugs/quirks in a given product (like Zaurus, above).
4072          */
4073         USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
4074                         USB_CDC_PROTO_NONE),
4075         .driver_info = (unsigned long) &cdc_info,
4076 },
4077 #endif
4078
4079         { },            // END
4080 };
4081 MODULE_DEVICE_TABLE (usb, products);
4082
4083 static struct usb_driver usbnet_driver = {
4084         .owner =        THIS_MODULE,
4085         .name =         driver_name,
4086         .id_table =     products,
4087         .probe =        usbnet_probe,
4088         .disconnect =   usbnet_disconnect,
4089         .suspend =      usbnet_suspend,
4090         .resume =       usbnet_resume,
4091 };
4092
4093 /* Default ethtool_ops assigned.  Devices can override in their bind() routine */
4094 static struct ethtool_ops usbnet_ethtool_ops = {
4095         .get_drvinfo            = usbnet_get_drvinfo,
4096         .get_link               = usbnet_get_link,
4097         .get_msglevel           = usbnet_get_msglevel,
4098         .set_msglevel           = usbnet_set_msglevel,
4099 };
4100
4101 /*-------------------------------------------------------------------------*/
4102
4103 static int __init usbnet_init (void)
4104 {
4105         // compiler should optimize these out
4106         BUG_ON (sizeof (((struct sk_buff *)0)->cb)
4107                         < sizeof (struct skb_data));
4108 #ifdef  CONFIG_USB_CDCETHER
4109         BUG_ON ((sizeof (((struct usbnet *)0)->data)
4110                         < sizeof (struct cdc_state)));
4111 #endif
4112
4113         random_ether_addr(node_id);
4114
4115         return usb_register(&usbnet_driver);
4116 }
4117 module_init (usbnet_init);
4118
4119 static void __exit usbnet_exit (void)
4120 {
4121         usb_deregister (&usbnet_driver);
4122 }
4123 module_exit (usbnet_exit);
4124
4125 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
4126 MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (numerous vendors)");
4127 MODULE_LICENSE ("GPL");