commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / usb / net / usbnet.c
1 /*
2  * USB Host-to-Host Links
3  * Copyright (C) 2000-2002 by David Brownell <dbrownell@users.sourceforge.net>
4  * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * This is used for "USB networking", connecting USB hosts as peers.
23  *
24  * It can be used with USB "network cables", for IP-over-USB communications;
25  * Ethernet speeds without the Ethernet.  USB devices (including some PDAs)
26  * can support such links directly, replacing device-specific protocols
27  * with Internet standard ones.
28  *
29  * The links can be bridged using the Ethernet bridging (net/bridge)
30  * support as appropriate.  Devices currently supported include:
31  *
32  *      - AnchorChip 2720
33  *      - Belkin, eTEK (interops with Win32 drivers)
34  *      - EPSON USB clients
35  *      - GeneSys GL620USB-A
36  *      - NetChip 1080 (interoperates with NetChip Win32 drivers)
37  *      - Prolific PL-2301/2302 (replaces "plusb" driver)
38  *      - PXA-250 or SA-1100 Linux PDAs like iPaq, Yopy, and Zaurus
39  *
40  * USB devices can implement their side of this protocol at the cost
41  * of two bulk endpoints; it's not restricted to "cable" applications.
42  * See the SA1110, Zaurus, or EPSON device/client support in this driver;
43  * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) are
44  * used inside USB slave/target devices.
45  *
46  * 
47  * Status:
48  *
49  * - AN2720 ... not widely available, but reportedly works well
50  *
51  * - Belkin/eTEK ... no known issues
52  *
53  * - Both GeneSys and PL-230x use interrupt transfers for driver-to-driver
54  *   handshaking; it'd be worth implementing those as "carrier detect".
55  *   Prefer generic hooks, not minidriver-specific hacks.
56  *
57  * - For Netchip, should use keventd to poll via control requests to detect
58  *   hardware level "carrier detect". 
59  *
60  * - PL-230x ... the initialization protocol doesn't seem to match chip data
61  *   sheets, sometimes it's not needed and sometimes it hangs.  Prolific has
62  *   not responded to repeated support/information requests.
63  *
64  * - SA-1100 PDAs ... the standard ARM Linux SA-1100 support works nicely,
65  *   as found in www.handhelds.org and other kernels.  The Sharp/Lineo
66  *   kernels use different drivers, which also talk to this code.
67  *
68  * Interop with more Win32 drivers may be a good thing.
69  *
70  * Seems like reporting "peer connected" (carrier present) events may end
71  * up going through the netlink event system, not hotplug ... so new links
72  * would likely be handled with a link monitoring thread in some daemon.
73  *
74  * There are reports that bridging gives lower-than-usual throughput.
75  *
76  * Need smarter hotplug policy scripts ... ones that know how to arrange
77  * bridging with "brctl", and can handle static and dynamic ("pump") setups.
78  * Use those eventual "peer connected" events, and zeroconf.
79  *
80  *
81  * CHANGELOG:
82  *
83  * 13-sep-2000  experimental, new
84  * 10-oct-2000  usb_device_id table created. 
85  * 28-oct-2000  misc fixes; mostly, discard more TTL-mangled rx packets.
86  * 01-nov-2000  usb_device_id table and probing api update by
87  *              Adam J. Richter <adam@yggdrasil.com>.
88  * 18-dec-2000  (db) tx watchdog, "net1080" renaming to "usbnet", device_info
89  *              and prolific support, isolate net1080-specific bits, cleanup.
90  *              fix unlink_urbs oops in D3 PM resume code path.
91  *
92  * 02-feb-2001  (db) fix tx skb sharing, packet length, match_flags, ...
93  * 08-feb-2001  stubbed in "linuxdev", maybe the SA-1100 folk can use it;
94  *              AnchorChips 2720 support (from spec) for testing;
95  *              fix bit-ordering problem with ethernet multicast addr
96  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
97  *              updates. Oleg Drokin (green@iXcelerator.com)
98  * 25-mar-2001  More SA-1100 updates, including workaround for ip problem
99  *              expecting cleared skb->cb and framing change to match latest
100  *              handhelds.org version (Oleg).  Enable device IDs from the
101  *              Win32 Belkin driver; other cleanups (db).
102  * 16-jul-2001  Bugfixes for uhci oops-on-unplug, Belkin support, various
103  *              cleanups for problems not yet seen in the field. (db)
104  * 17-oct-2001  Handle "Advance USBNET" product, like Belkin/eTEK devices,
105  *              from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
106  *              rx unlinks somehow weren't async; minor cleanup.
107  * 03-nov-2001  Merged GeneSys driver; original code from Jiun-Jie Huang
108  *              <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
109  *              <utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
110  *              tie mostly to (sub)driver info.  Workaround some PL-2302
111  *              chips that seem to reject SET_INTERFACE requests.
112  *
113  * 06-apr-2002  Added ethtool support, based on a patch from Brad Hards.
114  *              Level of diagnostics is more configurable; they use device
115  *              location (usb_device->devpath) instead of address (2.5).
116  *              For tx_fixup, memflags can't be NOIO.
117  * 07-may-2002  Generalize/cleanup keventd support, handling rx stalls (mostly
118  *              for USB 2.0 TTs) and memory shortages (potential) too. (db)
119  *              Use "locally assigned" IEEE802 address space. (Brad Hards)
120  * 18-oct-2002  Support for Zaurus (Pavel Machek), related cleanup (db).
121  * 14-dec-2002  Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
122  *              cleanups and stubbed PXA-250 support (db), fix for framing
123  *              issues on Z, net1080, and gl620a (Toby Milne)
124  *
125  * 31-mar-2003  Use endpoint descriptors:  high speed support, simpler sa1100
126  *              vs pxa25x, and CDC Ethernet.  Throttle down log floods on
127  *              disconnect; other cleanups. (db)  Flush net1080 fifos
128  *              after several sequential framing errors. (Johannes Erdfelt)
129  *
130  *-------------------------------------------------------------------------*/
131
132 #include <linux/config.h>
133 #include <linux/module.h>
134 #include <linux/kmod.h>
135 #include <linux/sched.h>
136 #include <linux/init.h>
137 #include <linux/netdevice.h>
138 #include <linux/etherdevice.h>
139 #include <linux/random.h>
140 #include <linux/ethtool.h>
141 #include <linux/workqueue.h>
142 #include <asm/uaccess.h>
143 #include <asm/unaligned.h>
144
145 // #define      DEBUG                   // error path messages, extra info
146 // #define      VERBOSE                 // more; success messages
147 #define REALLY_QUEUE
148
149 #if !defined (DEBUG) && defined (CONFIG_USB_DEBUG)
150 #   define DEBUG
151 #endif
152 #include <linux/usb.h>
153
154 #include <asm/io.h>
155 #include <asm/scatterlist.h>
156 #include <linux/mm.h>
157 #include <linux/dma-mapping.h>
158
159
160 #define DRIVER_VERSION          "25-Apr-2003"
161
162 /*-------------------------------------------------------------------------*/
163
164 /*
165  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
166  * Several dozen bytes of IPv4 data can fit in two such transactions.
167  * One maximum size Ethernet packet takes twenty four of them.
168  * For high speed, each frame comfortably fits almost 36 max size
169  * Ethernet packets (so queues should be bigger).
170  */
171 #ifdef REALLY_QUEUE
172 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
173 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
174 #else
175 #define RX_QLEN(dev)            1
176 #define TX_QLEN(dev)            1
177 #endif
178
179 // packets are always ethernet inside
180 // ... except they can be bigger (limit of 64K with NetChip framing)
181 #define MIN_PACKET      sizeof(struct ethhdr)
182 #define MAX_PACKET      32768
183
184 // reawaken network queue this soon after stopping; else watchdog barks
185 #define TX_TIMEOUT_JIFFIES      (5*HZ)
186
187 // throttle rx/tx briefly after some faults, so khubd might disconnect()
188 // us (it polls at HZ/4 usually) before we report too many false errors.
189 #define THROTTLE_JIFFIES        (HZ/8)
190
191 // for vendor-specific control operations
192 #define CONTROL_TIMEOUT_MS      (500)                   /* msec */
193 #define CONTROL_TIMEOUT_JIFFIES ((CONTROL_TIMEOUT_MS * HZ)/1000)
194
195 // between wakeups
196 #define UNLINK_TIMEOUT_JIFFIES ((3  /*ms*/ * HZ)/1000)
197
198 /*-------------------------------------------------------------------------*/
199
200 // randomly generated ethernet address
201 static u8       node_id [ETH_ALEN];
202
203 // state we keep for each device we handle
204 struct usbnet {
205         // housekeeping
206         struct usb_device       *udev;
207         struct driver_info      *driver_info;
208         wait_queue_head_t       *wait;
209
210         // i/o info: pipes etc
211         unsigned                in, out;
212         unsigned                maxpacket;
213         struct timer_list       delay;
214
215         // protocol/interface state
216         struct net_device       *net;
217         struct net_device_stats stats;
218         int                     msg_level;
219         unsigned long           data [5];
220
221         // various kinds of pending driver work
222         struct sk_buff_head     rxq;
223         struct sk_buff_head     txq;
224         struct sk_buff_head     done;
225         struct tasklet_struct   bh;
226
227         struct work_struct      kevent;
228         unsigned long           flags;
229 #               define EVENT_TX_HALT    0
230 #               define EVENT_RX_HALT    1
231 #               define EVENT_RX_MEMORY  2
232 };
233
234 // device-specific info used by the driver
235 struct driver_info {
236         char            *description;
237
238         int             flags;
239 /* framing is CDC Ethernet, not writing ZLPs (hw issues), or optionally: */
240 #define FLAG_FRAMING_NC 0x0001          /* guard against device dropouts */ 
241 #define FLAG_FRAMING_GL 0x0002          /* genelink batches packets */
242 #define FLAG_FRAMING_Z  0x0004          /* zaurus adds a trailer */
243
244 #define FLAG_NO_SETINT  0x0010          /* device can't set_interface() */
245 #define FLAG_ETHER      0x0020          /* maybe use "eth%d" names */
246
247         /* init device ... can sleep, or cause probe() failure */
248         int     (*bind)(struct usbnet *, struct usb_interface *);
249
250         /* cleanup device ... can sleep, but can't fail */
251         void    (*unbind)(struct usbnet *, struct usb_interface *);
252
253         /* reset device ... can sleep */
254         int     (*reset)(struct usbnet *);
255
256         /* see if peer is connected ... can sleep */
257         int     (*check_connect)(struct usbnet *);
258
259         /* fixup rx packet (strip framing) */
260         int     (*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
261
262         /* fixup tx packet (add framing) */
263         struct sk_buff  *(*tx_fixup)(struct usbnet *dev,
264                                 struct sk_buff *skb, int flags);
265
266         // FIXME -- also an interrupt mechanism
267         // useful for at least PL2301/2302 and GL620USB-A
268         // and CDC use them to report 'is it connected' changes
269
270         /* for new devices, use the descriptor-reading code instead */
271         int             in;             /* rx endpoint */
272         int             out;            /* tx endpoint */
273 };
274
275 // we record the state for each of our queued skbs
276 enum skb_state {
277         illegal = 0,
278         tx_start, tx_done,
279         rx_start, rx_done, rx_cleanup
280 };
281
282 struct skb_data {       // skb->cb is one of these
283         struct urb              *urb;
284         struct usbnet           *dev;
285         enum skb_state          state;
286         size_t                  length;
287 };
288
289 static const char driver_name [] = "usbnet";
290
291 /* use ethtool to change the level for any given device */
292 static int msg_level = 1;
293 MODULE_PARM (msg_level, "i");
294 MODULE_PARM_DESC (msg_level, "Initial message level (default = 1)");
295
296
297 #define RUN_CONTEXT (in_irq () ? "in_irq" \
298                         : (in_interrupt () ? "in_interrupt" : "can sleep"))
299
300 #ifdef DEBUG
301 #define devdbg(usbnet, fmt, arg...) \
302         printk(KERN_DEBUG "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
303 #else
304 #define devdbg(usbnet, fmt, arg...) do {} while(0)
305 #endif
306
307 #define deverr(usbnet, fmt, arg...) \
308         printk(KERN_ERR "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
309 #define devwarn(usbnet, fmt, arg...) \
310         printk(KERN_WARNING "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
311
312 #define devinfo(usbnet, fmt, arg...) \
313         do { if ((usbnet)->msg_level >= 1) \
314         printk(KERN_INFO "%s: " fmt "\n" , (usbnet)->net->name , ## arg); \
315         } while (0)
316
317 /*-------------------------------------------------------------------------*/
318
319 /* mostly for PDA style devices, which are always connected if present */
320 static int always_connected (struct usbnet *dev)
321 {
322         return 0;
323 }
324
325 /* handles CDC Ethernet and many other network "bulk data" interfaces */
326 static int
327 get_endpoints (struct usbnet *dev, struct usb_interface *intf)
328 {
329         int                             tmp;
330         struct usb_host_interface       *alt;
331         struct usb_host_endpoint        *in, *out;
332
333         for (tmp = 0; tmp < intf->max_altsetting; tmp++) {
334                 unsigned        ep;
335
336                 in = out = 0;
337                 alt = intf->altsetting + tmp;
338
339                 /* take the first altsetting with in-bulk + out-bulk;
340                  * ignore other endpoints and altsetttings.
341                  */
342                 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
343                         struct usb_host_endpoint        *e;
344
345                         e = alt->endpoint + ep;
346                         if (e->desc.bmAttributes != USB_ENDPOINT_XFER_BULK)
347                                 continue;
348                         if (e->desc.bEndpointAddress & USB_DIR_IN) {
349                                 if (!in)
350                                         in = e;
351                         } else {
352                                 if (!out)
353                                         out = e;
354                         }
355                         if (in && out)
356                                 goto found;
357                 }
358         }
359         return -EINVAL;
360
361 found:
362         if (alt->desc.bAlternateSetting != 0
363                         || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
364                 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
365                                 alt->desc.bAlternateSetting);
366                 if (tmp < 0)
367                         return tmp;
368         }
369         
370         dev->in = usb_rcvbulkpipe (dev->udev,
371                         in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
372         dev->out = usb_sndbulkpipe (dev->udev,
373                         out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
374         return 0;
375 }
376
377 \f
378 #ifdef  CONFIG_USB_AN2720
379 #define HAVE_HARDWARE
380
381 /*-------------------------------------------------------------------------
382  *
383  * AnchorChips 2720 driver ... http://www.cypress.com
384  *
385  * This doesn't seem to have a way to detect whether the peer is
386  * connected, or need any reset handshaking.  It's got pretty big
387  * internal buffers (handles most of a frame's worth of data).
388  * Chip data sheets don't describe any vendor control messages.
389  *
390  *-------------------------------------------------------------------------*/
391
392 static const struct driver_info an2720_info = {
393         .description =  "AnchorChips/Cypress 2720",
394         // no reset available!
395         // no check_connect available!
396
397         .in = 2, .out = 2,              // direction distinguishes these
398 };
399
400 #endif  /* CONFIG_USB_AN2720 */
401
402
403 \f
404 #ifdef  CONFIG_USB_BELKIN
405 #define HAVE_HARDWARE
406
407 /*-------------------------------------------------------------------------
408  *
409  * Belkin F5U104 ... two NetChip 2280 devices + Atmel microcontroller
410  *
411  * ... also two eTEK designs, including one sold as "Advance USBNET"
412  *
413  *-------------------------------------------------------------------------*/
414
415 static const struct driver_info belkin_info = {
416         .description =  "Belkin, eTEK, or compatible",
417 };
418
419 #endif  /* CONFIG_USB_BELKIN */
420
421
422 \f
423 #if     defined (CONFIG_USB_CDCETHER) || defined (CONFIG_USB_ZAURUS)
424
425 /*-------------------------------------------------------------------------
426  *
427  * Communications Device Class, Ethernet Control model
428  * 
429  * Takes two interfaces.  The DATA interface is inactive till an altsetting
430  * is selected.  Configuration data includes class descriptors.
431  *
432  * Zaurus uses nonstandard framing, and doesn't uniquify its Ethernet
433  * addresses, but is otherwise CDC Ether.
434  *
435  * This should interop with whatever the 2.4 "CDCEther.c" driver
436  * (by Brad Hards) talked with.
437  *
438  *-------------------------------------------------------------------------*/
439
440 /* "Header Functional Descriptor" from CDC spec  5.2.3.1 */
441 struct header_desc {
442         u8      bLength;
443         u8      bDescriptorType;
444         u8      bDescriptorSubType;
445
446         u16     bcdCDC;
447 } __attribute__ ((packed));
448
449 /* "Union Functional Descriptor" from CDC spec 5.2.3.X */
450 struct union_desc {
451         u8      bLength;
452         u8      bDescriptorType;
453         u8      bDescriptorSubType;
454
455         u8      bMasterInterface0;
456         u8      bSlaveInterface0;
457         /* ... and there could be other slave interfaces */
458 } __attribute__ ((packed));
459
460 /* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
461 struct ether_desc {
462         u8      bLength;
463         u8      bDescriptorType;
464         u8      bDescriptorSubType;
465
466         u8      iMACAddress;
467         u32     bmEthernetStatistics;
468         u16     wMaxSegmentSize;
469         u16     wNumberMCFilters;
470         u8      bNumberPowerFilters;
471 } __attribute__ ((packed));
472
473 struct cdc_info {
474         struct header_desc      *header;
475         struct union_desc       *u;
476         struct ether_desc       *ether;
477         struct usb_interface    *control;
478         struct usb_interface    *data;
479 };
480
481 #include <linux/ctype.h>
482
483 static u8 nibble (unsigned char c)
484 {
485         if (likely (isdigit (c)))
486                 return c - '0';
487         c = toupper (c);
488         if (likely (isxdigit (c)))
489                 return 10 + c - 'A';
490         return 0;
491 }
492
493 static inline int get_ethernet_addr (struct usbnet *dev, struct ether_desc *e)
494 {
495         int             tmp, i;
496         unsigned char   buf [13];
497
498         tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
499         if (tmp < 0)
500                 return tmp;
501         else if (tmp != 12)
502                 return -EINVAL;
503         for (i = tmp = 0; i < 6; i++, tmp += 2)
504                 dev->net->dev_addr [i] =
505                          (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
506         return 0;
507 }
508
509 static struct usb_driver usbnet_driver;
510
511 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
512 {
513         u8                              *buf = intf->altsetting->extra;
514         int                             len = intf->altsetting->extralen;
515         struct usb_interface_descriptor *d;
516         struct cdc_info                 *info = (void *) &dev->data;
517         int                             status;
518
519         if (sizeof dev->data < sizeof *info)
520                 return -EDOM;
521
522         /* expect strict spec conformance for the descriptors, but
523          * cope with firmware which stores them in the wrong place
524          */
525         if (len == 0 && dev->udev->config->extralen) {
526                 /* Motorola SB4100 (and maybe others) put
527                  * CDC descriptors here
528                  */
529                 buf = dev->udev->config->extra;
530                 len = dev->udev->config->extralen;
531         }
532
533         memset (info, 0, sizeof *info);
534         info->control = intf;
535         while (len > 3) {
536                 /* ignore bDescriptorType != CS_INTERFACE */
537                 if (buf [1] != 0x24)
538                         goto next_desc;
539
540                 /* bDescriptorSubType identifies three "must have" descriptors;
541                  * save them for later.
542                  */
543                 switch (buf [2]) {
544                 case 0x00:              /* Header, mostly useless */
545                         if (info->header)
546                                 goto bad_desc;
547                         info->header = (void *) buf;
548                         if (info->header->bLength != sizeof *info->header)
549                                 goto bad_desc;
550                         break;
551                 case 0x06:              /* Union (groups interfaces) */
552                         if (info->u)
553                                 goto bad_desc;
554                         info->u = (void *) buf;
555                         if (info->u->bLength != sizeof *info->u)
556                                 goto bad_desc;
557                         d = &intf->altsetting->desc;
558                         if (info->u->bMasterInterface0 != d->bInterfaceNumber)
559                                 goto bad_desc;
560                         info->data = dev->udev->actconfig->interface[0];
561                         if (intf != (info->data + info->u->bMasterInterface0))
562                                 goto bad_desc;
563
564                         /* a data interface altsetting does the real i/o */
565                         info->data += info->u->bSlaveInterface0;
566                         d = &info->data->altsetting->desc;
567                         if (info->u->bSlaveInterface0 != d->bInterfaceNumber
568                                     || d->bInterfaceClass != USB_CLASS_CDC_DATA)
569                                 goto bad_desc;
570                         if (usb_interface_claimed (info->data))
571                                 return -EBUSY;
572                         break;
573                 case 0x0F:              /* Ethernet Networking */
574                         if (info->ether)
575                                 goto bad_desc;
576                         info->ether = (void *) buf;
577                         if (info->ether->bLength != sizeof *info->ether)
578                                 goto bad_desc;
579                         break;
580                 }
581 next_desc:
582                 len -= buf [0]; /* bLength */
583                 buf += buf [0];
584         }
585         if (!info->header || !info ->u || !info->ether)
586                 goto bad_desc;
587
588 #ifdef CONFIG_USB_ZAURUS
589         /* Zaurus ethernet addresses aren't unique ... */
590         if ((dev->driver_info->flags & FLAG_FRAMING_Z) != 0)
591                 /* ignore */ ;
592         else
593 #endif
594         {
595                 status = get_ethernet_addr (dev, info->ether);
596                 if (status < 0)
597                         return status;
598         }
599
600         /* claim data interface and set it up ... with side effects.
601          * network traffic can't flow until an altsetting is enabled.
602          */
603         usb_driver_claim_interface (&usbnet_driver, info->data, dev);
604         status = get_endpoints (dev, info->data);
605         if (status < 0) {
606                 usb_driver_release_interface (&usbnet_driver, info->data);
607                 return status;
608         }
609
610         /* FIXME cdc-ether has some multicast code too, though it complains
611          * in routine cases.  info->ether describes the multicast support.
612          */
613
614         dev->net->mtu = cpu_to_le16p (&info->ether->wMaxSegmentSize)
615                 - ETH_HLEN;
616         return 0;
617
618 bad_desc:
619         dev_info (&dev->udev->dev, "bad CDC descriptors\n");
620         return -ENODEV;
621 }
622
623 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
624 {
625         struct cdc_info                 *info = (void *) &dev->data;
626
627         /* disconnect master --> disconnect slave */
628         if (intf == info->control && info->data) {
629                 usb_driver_release_interface (&usbnet_driver, info->data);
630                 info->data = 0;
631         }
632
633         /* and vice versa (just in case) */
634         else if (intf == info->data && info->control) {
635                 usb_driver_release_interface (&usbnet_driver, info->control);
636                 info->control = 0;
637         }
638
639 }
640
641 #endif  /* CONFIG_USB_ZAURUS || CONFIG_USB_CDCETHER */
642
643
644 #ifdef  CONFIG_USB_CDCETHER
645 #define HAVE_HARDWARE
646
647 static const struct driver_info cdc_info = {
648         .description =  "CDC Ethernet Device",
649         .flags =        FLAG_ETHER,
650         // .check_connect = cdc_check_connect,
651         .bind =         cdc_bind,
652         .unbind =       cdc_unbind,
653 };
654
655 #endif  /* CONFIG_USB_CDCETHER */
656
657
658 \f
659 #ifdef  CONFIG_USB_EPSON2888
660 #define HAVE_HARDWARE
661
662 /*-------------------------------------------------------------------------
663  *
664  * EPSON USB clients
665  *
666  * This is the same idea as Linux PDAs (below) except the firmware in the
667  * device might not be Tux-powered.  Epson provides reference firmware that
668  * implements this interface.  Product developers can reuse or modify that
669  * code, such as by using their own product and vendor codes.
670  *
671  * Support was from Juro Bystricky <bystricky.juro@erd.epson.com>
672  *
673  *-------------------------------------------------------------------------*/
674
675 static const struct driver_info epson2888_info = {
676         .description =  "Epson USB Device",
677         .check_connect = always_connected,
678
679         .in = 4, .out = 3,
680 };
681
682 #endif  /* CONFIG_USB_EPSON2888 */
683
684 \f
685 #ifdef CONFIG_USB_GENESYS
686 #define HAVE_HARDWARE
687
688 /*-------------------------------------------------------------------------
689  *
690  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
691  *
692  * ... should partially interop with the Win32 driver for this hardware
693  * The GeneSys docs imply there's some NDIS issue motivating this framing.
694  *
695  * Some info from GeneSys:
696  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
697  *    (Some cables, like the BAFO-100c, use the half duplex version.)
698  *  - For the full duplex model, the low bit of the version code says
699  *    which side is which ("left/right").
700  *  - For the half duplex type, a control/interrupt handshake settles
701  *    the transfer direction.  (That's disabled here, partially coded.)
702  *    A control URB would block until other side writes an interrupt.
703  *
704  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
705  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
706  *
707  *-------------------------------------------------------------------------*/
708
709 // control msg write command
710 #define GENELINK_CONNECT_WRITE                  0xF0
711 // interrupt pipe index
712 #define GENELINK_INTERRUPT_PIPE                 0x03
713 // interrupt read buffer size
714 #define INTERRUPT_BUFSIZE                       0x08
715 // interrupt pipe interval value
716 #define GENELINK_INTERRUPT_INTERVAL             0x10
717 // max transmit packet number per transmit
718 #define GL_MAX_TRANSMIT_PACKETS                 32
719 // max packet length
720 #define GL_MAX_PACKET_LEN                       1514
721 // max receive buffer size 
722 #define GL_RCV_BUF_SIZE         \
723         (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
724
725 struct gl_packet {
726         u32             packet_length;
727         char            packet_data [1];
728 };
729
730 struct gl_header {
731         u32                     packet_count;
732         struct gl_packet        packets;
733 };
734
735 #ifdef  GENLINK_ACK
736
737 // FIXME:  this code is incomplete, not debugged; it doesn't
738 // handle interrupts correctly.  interrupts should be generic
739 // code like all other device I/O, anyway.
740
741 struct gl_priv { 
742         struct urb      *irq_urb;
743         char            irq_buf [INTERRUPT_BUFSIZE];
744 };
745
746 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
747 {
748         int retval;
749
750         retval = usb_control_msg (dev->udev,
751                       usb_sndctrlpipe (dev->udev, 0),
752                       request,
753                       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
754                       value, 
755                       0,                        // index
756                       0,                        // data buffer
757                       0,                        // size
758                       CONTROL_TIMEOUT_JIFFIES);
759         return retval;
760 }
761
762 static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
763 {
764         int status = urb->status;
765         
766         switch (status) {
767         case 0:
768                 /* success */
769                 break;
770         case -ECONNRESET:
771         case -ENOENT:
772         case -ESHUTDOWN:
773                 /* this urb is terminated, clean up */
774                 dbg("%s - urb shutting down with status: %d",
775                                 __FUNCTION__, status);
776                 return;
777         default:
778                 dbg("%s - nonzero urb status received: %d",
779                                 __FUNCTION__, urb->status);
780         }
781
782         status = usb_submit_urb (urb, GFP_ATOMIC);
783         if (status)
784                 err ("%s - usb_submit_urb failed with result %d",
785                      __FUNCTION__, status);
786 }
787
788 static int gl_interrupt_read (struct usbnet *dev)
789 {
790         struct gl_priv  *priv = dev->priv_data;
791         int             retval;
792
793         // issue usb interrupt read
794         if (priv && priv->irq_urb) {
795                 // submit urb
796                 if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
797                         dbg ("gl_interrupt_read: submit fail - %X...", retval);
798                 else
799                         dbg ("gl_interrupt_read: submit success...");
800         }
801
802         return 0;
803 }
804
805 // check whether another side is connected
806 static int genelink_check_connect (struct usbnet *dev)
807 {
808         int                     retval;
809
810         dbg ("genelink_check_connect...");
811
812         // detect whether another side is connected
813         if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
814                 dbg ("%s: genelink_check_connect write fail - %X",
815                         dev->net->name, retval);
816                 return retval;
817         }
818
819         // usb interrupt read to ack another side 
820         if ((retval = gl_interrupt_read (dev)) != 0) {
821                 dbg ("%s: genelink_check_connect read fail - %X",
822                         dev->net->name, retval);
823                 return retval;
824         }
825
826         dbg ("%s: genelink_check_connect read success", dev->net->name);
827         return 0;
828 }
829
830 // allocate and initialize the private data for genelink
831 static int genelink_init (struct usbnet *dev)
832 {
833         struct gl_priv *priv;
834
835         // allocate the private data structure
836         if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
837                 dbg ("%s: cannot allocate private data per device",
838                         dev->net->name);
839                 return -ENOMEM;
840         }
841
842         // allocate irq urb
843         if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
844                 dbg ("%s: cannot allocate private irq urb per device",
845                         dev->net->name);
846                 kfree (priv);
847                 return -ENOMEM;
848         }
849
850         // fill irq urb
851         usb_fill_int_urb (priv->irq_urb, dev->udev,
852                 usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
853                 priv->irq_buf, INTERRUPT_BUFSIZE,
854                 gl_interrupt_complete, 0,
855                 GENELINK_INTERRUPT_INTERVAL);
856
857         // set private data pointer
858         dev->priv_data = priv;
859
860         return 0;
861 }
862
863 // release the private data
864 static int genelink_free (struct usbnet *dev)
865 {
866         struct gl_priv  *priv = dev->priv_data;
867
868         if (!priv) 
869                 return 0;
870
871 // FIXME:  can't cancel here; it's synchronous, and
872 // should have happened earlier in any case (interrupt
873 // handling needs to be generic)
874
875         // cancel irq urb first
876         usb_unlink_urb (priv->irq_urb);
877
878         // free irq urb
879         usb_free_urb (priv->irq_urb);
880
881         // free the private data structure
882         kfree (priv);
883
884         return 0;
885 }
886
887 #endif
888
889 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
890 {
891         struct gl_header        *header;
892         struct gl_packet        *packet;
893         struct sk_buff          *gl_skb;
894         int                     status;
895         u32                     size;
896
897         header = (struct gl_header *) skb->data;
898
899         // get the packet count of the received skb
900         le32_to_cpus (&header->packet_count);
901         if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
902                         || (header->packet_count < 0)) {
903                 dbg ("genelink: invalid received packet count %d",
904                         header->packet_count);
905                 return 0;
906         }
907
908         // set the current packet pointer to the first packet
909         packet = &header->packets;
910
911         // decrement the length for the packet count size 4 bytes
912         skb_pull (skb, 4);
913
914         while (header->packet_count > 1) {
915                 // get the packet length
916                 size = packet->packet_length;
917
918                 // this may be a broken packet
919                 if (size > GL_MAX_PACKET_LEN) {
920                         dbg ("genelink: invalid rx length %d", size);
921                         return 0;
922                 }
923
924                 // allocate the skb for the individual packet
925                 gl_skb = alloc_skb (size, GFP_ATOMIC);
926                 if (gl_skb) {
927
928                         // copy the packet data to the new skb
929                         memcpy(skb_put(gl_skb, size), packet->packet_data, size);
930                         gl_skb->dev = dev->net;
931
932                         // determine the packet's protocol ID
933                         gl_skb->protocol = eth_type_trans (gl_skb, dev->net);
934
935                         // update the status
936                         dev->stats.rx_packets++;
937                         dev->stats.rx_bytes += size;
938
939                         // notify os of the received packet
940                         status = netif_rx (gl_skb);
941                 }
942
943                 // advance to the next packet
944                 packet = (struct gl_packet *)
945                         &packet->packet_data [size];
946                 header->packet_count--;
947
948                 // shift the data pointer to the next gl_packet
949                 skb_pull (skb, size + 4);
950         }
951
952         // skip the packet length field 4 bytes
953         skb_pull (skb, 4);
954
955         if (skb->len > GL_MAX_PACKET_LEN) {
956                 dbg ("genelink: invalid rx length %d", skb->len);
957                 return 0;
958         }
959         return 1;
960 }
961
962 static struct sk_buff *
963 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
964 {
965         int     padlen;
966         int     length = skb->len;
967         int     headroom = skb_headroom (skb);
968         int     tailroom = skb_tailroom (skb);
969         u32     *packet_count;
970         u32     *packet_len;
971
972         // FIXME:  magic numbers, bleech
973         padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
974
975         if ((!skb_cloned (skb))
976                         && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
977                 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
978                         skb->data = memmove (skb->head + (4 + 4*1),
979                                              skb->data, skb->len);
980                         skb->tail = skb->data + skb->len;
981                 }
982         } else {
983                 struct sk_buff  *skb2;
984                 skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
985                 dev_kfree_skb_any (skb);
986                 skb = skb2;
987         }
988
989         // attach the packet count to the header
990         packet_count = (u32 *) skb_push (skb, (4 + 4*1));
991         packet_len = packet_count + 1;
992
993         // FIXME little endian?
994         *packet_count = 1;
995         *packet_len = length;
996
997         // add padding byte
998         if ((skb->len % dev->maxpacket) == 0)
999                 skb_put (skb, 1);
1000
1001         return skb;
1002 }
1003
1004 static const struct driver_info genelink_info = {
1005         .description =  "Genesys GeneLink",
1006         .flags =        FLAG_FRAMING_GL | FLAG_NO_SETINT,
1007         .rx_fixup =     genelink_rx_fixup,
1008         .tx_fixup =     genelink_tx_fixup,
1009
1010         .in = 1, .out = 2,
1011
1012 #ifdef  GENELINK_ACK
1013         .check_connect =genelink_check_connect,
1014 #endif
1015 };
1016
1017 #endif /* CONFIG_USB_GENESYS */
1018
1019
1020 \f
1021 #ifdef  CONFIG_USB_NET1080
1022 #define HAVE_HARDWARE
1023
1024 /*-------------------------------------------------------------------------
1025  *
1026  * Netchip 1080 driver ... http://www.netchip.com
1027  * Used in LapLink cables
1028  *
1029  *-------------------------------------------------------------------------*/
1030
1031 #define dev_packet_id   data[0]
1032 #define frame_errors    data[1]
1033
1034 /*
1035  * NetChip framing of ethernet packets, supporting additional error
1036  * checks for links that may drop bulk packets from inside messages.
1037  * Odd USB length == always short read for last usb packet.
1038  *      - nc_header
1039  *      - Ethernet header (14 bytes)
1040  *      - payload
1041  *      - (optional padding byte, if needed so length becomes odd)
1042  *      - nc_trailer
1043  *
1044  * This framing is to be avoided for non-NetChip devices.
1045  */
1046
1047 struct nc_header {              // packed:
1048         u16     hdr_len;                // sizeof nc_header (LE, all)
1049         u16     packet_len;             // payload size (including ethhdr)
1050         u16     packet_id;              // detects dropped packets
1051 #define MIN_HEADER      6
1052
1053         // all else is optional, and must start with:
1054         // u16  vendorId;               // from usb-if
1055         // u16  productId;
1056 } __attribute__((__packed__));
1057
1058 #define PAD_BYTE        ((unsigned char)0xAC)
1059
1060 struct nc_trailer {
1061         u16     packet_id;
1062 } __attribute__((__packed__));
1063
1064 // packets may use FLAG_FRAMING_NC and optional pad
1065 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
1066                                 + sizeof (struct ethhdr) \
1067                                 + (mtu) \
1068                                 + 1 \
1069                                 + sizeof (struct nc_trailer))
1070
1071 #define MIN_FRAMED      FRAMED_SIZE(0)
1072
1073
1074 /*
1075  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
1076  * before the hardware drops it.  If that's done, the driver will need to
1077  * frame network packets to guard against the dropped USB packets.  The win32
1078  * driver sets this for both sides of the link.
1079  */
1080 #define NC_READ_TTL_MS  ((u8)255)       // ms
1081
1082 /*
1083  * We ignore most registers and EEPROM contents.
1084  */
1085 #define REG_USBCTL      ((u8)0x04)
1086 #define REG_TTL         ((u8)0x10)
1087 #define REG_STATUS      ((u8)0x11)
1088
1089 /*
1090  * Vendor specific requests to read/write data
1091  */
1092 #define REQUEST_REGISTER        ((u8)0x10)
1093 #define REQUEST_EEPROM          ((u8)0x11)
1094
1095 static int
1096 nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
1097 {
1098         int status = usb_control_msg (dev->udev,
1099                 usb_rcvctrlpipe (dev->udev, 0),
1100                 req,
1101                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1102                 0, regnum,
1103                 retval_ptr, sizeof *retval_ptr,
1104                 CONTROL_TIMEOUT_JIFFIES);
1105         if (status > 0)
1106                 status = 0;
1107         if (!status)
1108                 le16_to_cpus (retval_ptr);
1109         return status;
1110 }
1111
1112 static inline int
1113 nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
1114 {
1115         return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
1116 }
1117
1118 // no retval ... can become async, usable in_interrupt()
1119 static void
1120 nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
1121 {
1122         usb_control_msg (dev->udev,
1123                 usb_sndctrlpipe (dev->udev, 0),
1124                 req,
1125                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1126                 value, regnum,
1127                 0, 0,                   // data is in setup packet
1128                 CONTROL_TIMEOUT_JIFFIES);
1129 }
1130
1131 static inline void
1132 nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
1133 {
1134         nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
1135 }
1136
1137
1138 #if 0
1139 static void nc_dump_registers (struct usbnet *dev)
1140 {
1141         u8      reg;
1142         u16     *vp = kmalloc (sizeof (u16));
1143
1144         if (!vp) {
1145                 dbg ("no memory?");
1146                 return;
1147         }
1148
1149         dbg ("%s registers:", dev->net->name);
1150         for (reg = 0; reg < 0x20; reg++) {
1151                 int retval;
1152
1153                 // reading some registers is trouble
1154                 if (reg >= 0x08 && reg <= 0xf)
1155                         continue;
1156                 if (reg >= 0x12 && reg <= 0x1e)
1157                         continue;
1158
1159                 retval = nc_register_read (dev, reg, vp);
1160                 if (retval < 0)
1161                         dbg ("%s reg [0x%x] ==> error %d",
1162                                 dev->net->name, reg, retval);
1163                 else
1164                         dbg ("%s reg [0x%x] = 0x%x",
1165                                 dev->net->name, reg, *vp);
1166         }
1167         kfree (vp);
1168 }
1169 #endif
1170
1171
1172 /*-------------------------------------------------------------------------*/
1173
1174 /*
1175  * Control register
1176  */
1177
1178 #define USBCTL_WRITABLE_MASK    0x1f0f
1179 // bits 15-13 reserved, r/o
1180 #define USBCTL_ENABLE_LANG      (1 << 12)
1181 #define USBCTL_ENABLE_MFGR      (1 << 11)
1182 #define USBCTL_ENABLE_PROD      (1 << 10)
1183 #define USBCTL_ENABLE_SERIAL    (1 << 9)
1184 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
1185 // bits 7-4 reserved, r/o
1186 #define USBCTL_FLUSH_OTHER      (1 << 3)
1187 #define USBCTL_FLUSH_THIS       (1 << 2)
1188 #define USBCTL_DISCONN_OTHER    (1 << 1)
1189 #define USBCTL_DISCONN_THIS     (1 << 0)
1190
1191 static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
1192 {
1193 #ifdef DEBUG
1194         devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
1195                         " this%s%s;"
1196                         " other%s%s; r/o 0x%x",
1197                 dev->udev->bus->bus_name, dev->udev->devpath,
1198                 usbctl,
1199                 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
1200                 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
1201                 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
1202                 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
1203                 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
1204
1205                 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
1206                 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
1207                 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
1208                 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
1209                 usbctl & ~USBCTL_WRITABLE_MASK
1210                 );
1211 #endif
1212 }
1213
1214 /*-------------------------------------------------------------------------*/
1215
1216 /*
1217  * Status register
1218  */
1219
1220 #define STATUS_PORT_A           (1 << 15)
1221
1222 #define STATUS_CONN_OTHER       (1 << 14)
1223 #define STATUS_SUSPEND_OTHER    (1 << 13)
1224 #define STATUS_MAILBOX_OTHER    (1 << 12)
1225 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
1226
1227 #define STATUS_CONN_THIS        (1 << 6)
1228 #define STATUS_SUSPEND_THIS     (1 << 5)
1229 #define STATUS_MAILBOX_THIS     (1 << 4)
1230 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) && 0x03)
1231
1232 #define STATUS_UNSPEC_MASK      0x0c8c
1233 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
1234
1235
1236 static inline void nc_dump_status (struct usbnet *dev, u16 status)
1237 {
1238 #ifdef DEBUG
1239         devdbg (dev, "net1080 %s-%s status 0x%x:"
1240                         " this (%c) PKT=%d%s%s%s;"
1241                         " other PKT=%d%s%s%s; unspec 0x%x",
1242                 dev->udev->bus->bus_name, dev->udev->devpath,
1243                 status,
1244
1245                 // XXX the packet counts don't seem right
1246                 // (1 at reset, not 0); maybe UNSPEC too
1247
1248                 (status & STATUS_PORT_A) ? 'A' : 'B',
1249                 STATUS_PACKETS_THIS (status),
1250                 (status & STATUS_CONN_THIS) ? " CON" : "",
1251                 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
1252                 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
1253
1254                 STATUS_PACKETS_OTHER (status),
1255                 (status & STATUS_CONN_OTHER) ? " CON" : "",
1256                 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
1257                 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
1258
1259                 status & STATUS_UNSPEC_MASK
1260                 );
1261 #endif
1262 }
1263
1264 /*-------------------------------------------------------------------------*/
1265
1266 /*
1267  * TTL register
1268  */
1269
1270 #define TTL_THIS(ttl)   (0x00ff & ttl)
1271 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
1272 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
1273
1274 static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
1275 {
1276 #ifdef DEBUG
1277         devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
1278                 dev->udev->bus->bus_name, dev->udev->devpath,
1279                 ttl,
1280
1281                 TTL_THIS (ttl),
1282                 TTL_OTHER (ttl)
1283                 );
1284 #endif
1285 }
1286
1287 /*-------------------------------------------------------------------------*/
1288
1289 static int net1080_reset (struct usbnet *dev)
1290 {
1291         u16             usbctl, status, ttl;
1292         u16             *vp = kmalloc (sizeof (u16), GFP_KERNEL);
1293         int             retval;
1294
1295         if (!vp)
1296                 return -ENOMEM;
1297
1298         // nc_dump_registers (dev);
1299
1300         if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
1301                 dbg ("can't read %s-%s status: %d",
1302                         dev->udev->bus->bus_name, dev->udev->devpath, retval);
1303                 goto done;
1304         }
1305         status = *vp;
1306         // nc_dump_status (dev, status);
1307
1308         if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
1309                 dbg ("can't read USBCTL, %d", retval);
1310                 goto done;
1311         }
1312         usbctl = *vp;
1313         // nc_dump_usbctl (dev, usbctl);
1314
1315         nc_register_write (dev, REG_USBCTL,
1316                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
1317
1318         if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
1319                 dbg ("can't read TTL, %d", retval);
1320                 goto done;
1321         }
1322         ttl = *vp;
1323         // nc_dump_ttl (dev, ttl);
1324
1325         nc_register_write (dev, REG_TTL,
1326                         MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
1327         dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
1328
1329         if (dev->msg_level >= 2)
1330                 devinfo (dev, "port %c, peer %sconnected",
1331                         (status & STATUS_PORT_A) ? 'A' : 'B',
1332                         (status & STATUS_CONN_OTHER) ? "" : "dis"
1333                         );
1334         retval = 0;
1335
1336 done:
1337         kfree (vp);
1338         return retval;
1339 }
1340
1341 static int net1080_check_connect (struct usbnet *dev)
1342 {
1343         int                     retval;
1344         u16                     status;
1345         u16                     *vp = kmalloc (sizeof (u16), GFP_KERNEL);
1346
1347         if (!vp)
1348                 return -ENOMEM;
1349         retval = nc_register_read (dev, REG_STATUS, vp);
1350         status = *vp;
1351         kfree (vp);
1352         if (retval != 0) {
1353                 dbg ("%s net1080_check_conn read - %d", dev->net->name, retval);
1354                 return retval;
1355         }
1356         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
1357                 return -ENOLINK;
1358         return 0;
1359 }
1360
1361 static void nc_flush_complete (struct urb *urb, struct pt_regs *regs)
1362 {
1363         kfree (urb->context);
1364         usb_free_urb(urb);
1365 }
1366
1367 static void nc_ensure_sync (struct usbnet *dev)
1368 {
1369         dev->frame_errors++;
1370         if (dev->frame_errors > 5) {
1371                 struct urb              *urb;
1372                 struct usb_ctrlrequest  *req;
1373                 int                     status;
1374
1375                 /* Send a flush */
1376                 urb = usb_alloc_urb (0, SLAB_ATOMIC);
1377                 if (!urb)
1378                         return;
1379
1380                 req = kmalloc (sizeof *req, GFP_ATOMIC);
1381                 if (!req) {
1382                         usb_free_urb (urb);
1383                         return;
1384                 }
1385
1386                 req->bRequestType = USB_DIR_OUT
1387                         | USB_TYPE_VENDOR
1388                         | USB_RECIP_DEVICE;
1389                 req->bRequest = REQUEST_REGISTER;
1390                 req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS
1391                                 | USBCTL_FLUSH_OTHER);
1392                 req->wIndex = cpu_to_le16 (REG_USBCTL);
1393                 req->wLength = cpu_to_le16 (0);
1394
1395                 /* queue an async control request, we don't need
1396                  * to do anything when it finishes except clean up.
1397                  */
1398                 usb_fill_control_urb (urb, dev->udev,
1399                         usb_sndctrlpipe (dev->udev, 0),
1400                         (unsigned char *) req,
1401                         NULL, 0,
1402                         nc_flush_complete, req);
1403                 status = usb_submit_urb (urb, GFP_ATOMIC);
1404                 if (status) {
1405                         kfree (req);
1406                         usb_free_urb (urb);
1407                         return;
1408                 }
1409
1410                 devdbg (dev, "flush net1080; too many framing errors");
1411                 dev->frame_errors = 0;
1412         }
1413 }
1414
1415 static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
1416 {
1417         struct nc_header        *header;
1418         struct nc_trailer       *trailer;
1419
1420         if (!(skb->len & 0x01)
1421                         || MIN_FRAMED > skb->len
1422                         || skb->len > FRAMED_SIZE (dev->net->mtu)) {
1423                 dev->stats.rx_frame_errors++;
1424                 dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
1425                         (int)MIN_FRAMED, (int)FRAMED_SIZE (dev->net->mtu),
1426                         dev->net->mtu);
1427                 nc_ensure_sync (dev);
1428                 return 0;
1429         }
1430
1431         header = (struct nc_header *) skb->data;
1432         le16_to_cpus (&header->hdr_len);
1433         le16_to_cpus (&header->packet_len);
1434         if (FRAMED_SIZE (header->packet_len) > MAX_PACKET) {
1435                 dev->stats.rx_frame_errors++;
1436                 dbg ("packet too big, %d", header->packet_len);
1437                 nc_ensure_sync (dev);
1438                 return 0;
1439         } else if (header->hdr_len < MIN_HEADER) {
1440                 dev->stats.rx_frame_errors++;
1441                 dbg ("header too short, %d", header->hdr_len);
1442                 nc_ensure_sync (dev);
1443                 return 0;
1444         } else if (header->hdr_len > MIN_HEADER) {
1445                 // out of band data for us?
1446                 dbg ("header OOB, %d bytes",
1447                         header->hdr_len - MIN_HEADER);
1448                 nc_ensure_sync (dev);
1449                 // switch (vendor/product ids) { ... }
1450         }
1451         skb_pull (skb, header->hdr_len);
1452
1453         trailer = (struct nc_trailer *)
1454                 (skb->data + skb->len - sizeof *trailer);
1455         skb_trim (skb, skb->len - sizeof *trailer);
1456
1457         if ((header->packet_len & 0x01) == 0) {
1458                 if (skb->data [header->packet_len] != PAD_BYTE) {
1459                         dev->stats.rx_frame_errors++;
1460                         dbg ("bad pad");
1461                         return 0;
1462                 }
1463                 skb_trim (skb, skb->len - 1);
1464         }
1465         if (skb->len != header->packet_len) {
1466                 dev->stats.rx_frame_errors++;
1467                 dbg ("bad packet len %d (expected %d)",
1468                         skb->len, header->packet_len);
1469                 nc_ensure_sync (dev);
1470                 return 0;
1471         }
1472         if (header->packet_id != get_unaligned (&trailer->packet_id)) {
1473                 dev->stats.rx_fifo_errors++;
1474                 dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
1475                         header->packet_id, trailer->packet_id);
1476                 return 0;
1477         }
1478 #if 0
1479         devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
1480                 header->packet_len, header->packet_id);
1481 #endif
1482         dev->frame_errors = 0;
1483         return 1;
1484 }
1485
1486 static struct sk_buff *
1487 net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1488 {
1489         int                     padlen;
1490         struct sk_buff          *skb2;
1491
1492         padlen = ((skb->len + sizeof (struct nc_header)
1493                         + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
1494         if (!skb_cloned (skb)) {
1495                 int     headroom = skb_headroom (skb);
1496                 int     tailroom = skb_tailroom (skb);
1497
1498                 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
1499                             && sizeof (struct nc_header) <= headroom)
1500                         /* There's enough head and tail room */
1501                         return skb;
1502
1503                 if ((sizeof (struct nc_header) + padlen
1504                                         + sizeof (struct nc_trailer)) <
1505                                 (headroom + tailroom)) {
1506                         /* There's enough total room, so just readjust */
1507                         skb->data = memmove (skb->head
1508                                                 + sizeof (struct nc_header),
1509                                             skb->data, skb->len);
1510                         skb->tail = skb->data + skb->len;
1511                         return skb;
1512                 }
1513         }
1514
1515         /* Create a new skb to use with the correct size */
1516         skb2 = skb_copy_expand (skb,
1517                                 sizeof (struct nc_header),
1518                                 sizeof (struct nc_trailer) + padlen,
1519                                 flags);
1520         dev_kfree_skb_any (skb);
1521         return skb2;
1522 }
1523
1524 static const struct driver_info net1080_info = {
1525         .description =  "NetChip TurboCONNECT",
1526         .flags =        FLAG_FRAMING_NC,
1527         .reset =        net1080_reset,
1528         .check_connect =net1080_check_connect,
1529         .rx_fixup =     net1080_rx_fixup,
1530         .tx_fixup =     net1080_tx_fixup,
1531 };
1532
1533 #endif /* CONFIG_USB_NET1080 */
1534
1535
1536 \f
1537 #ifdef CONFIG_USB_PL2301
1538 #define HAVE_HARDWARE
1539
1540 /*-------------------------------------------------------------------------
1541  *
1542  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
1543  *
1544  * The protocol and handshaking used here should be bug-compatible
1545  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
1546  *
1547  *-------------------------------------------------------------------------*/
1548
1549 /*
1550  * Bits 0-4 can be used for software handshaking; they're set from
1551  * one end, cleared from the other, "read" with the interrupt byte.
1552  */
1553 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
1554 /* reserved bit -- rx ready (6) ? */
1555 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
1556 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
1557 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
1558 #define PL_TX_C         (1<<2)          /* transmission complete */
1559 #define PL_TX_REQ       (1<<1)          /* transmission received */
1560 #define PL_PEER_E       (1<<0)          /* peer exists */
1561
1562 static inline int
1563 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
1564 {
1565         return usb_control_msg (dev->udev,
1566                 usb_rcvctrlpipe (dev->udev, 0),
1567                 req,
1568                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1569                 val, index,
1570                 0, 0,
1571                 CONTROL_TIMEOUT_JIFFIES);
1572 }
1573
1574 static inline int
1575 pl_clear_QuickLink_features (struct usbnet *dev, int val)
1576 {
1577         return pl_vendor_req (dev, 1, (u8) val, 0);
1578 }
1579
1580 static inline int
1581 pl_set_QuickLink_features (struct usbnet *dev, int val)
1582 {
1583         return pl_vendor_req (dev, 3, (u8) val, 0);
1584 }
1585
1586 /*-------------------------------------------------------------------------*/
1587
1588 static int pl_reset (struct usbnet *dev)
1589 {
1590         return pl_set_QuickLink_features (dev,
1591                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
1592 }
1593
1594 static const struct driver_info prolific_info = {
1595         .description =  "Prolific PL-2301/PL-2302",
1596         .flags =        FLAG_NO_SETINT,
1597                 /* some PL-2302 versions seem to fail usb_set_interface() */
1598         .reset =        pl_reset,
1599 };
1600
1601 #endif /* CONFIG_USB_PL2301 */
1602
1603
1604 \f
1605 #ifdef  CONFIG_USB_ARMLINUX
1606 #define HAVE_HARDWARE
1607
1608 /*-------------------------------------------------------------------------
1609  *
1610  * Intel's SA-1100 chip integrates basic USB support, and is used
1611  * in PDAs like some iPaqs, the Yopy, some Zaurus models, and more.
1612  * When they run Linux, arch/arm/mach-sa1100/usb-eth.c may be used to
1613  * network using minimal USB framing data.
1614  *
1615  * This describes the driver currently in standard ARM Linux kernels.
1616  * The Zaurus uses a different driver (see later).
1617  *
1618  * PXA25x and PXA210 use XScale cores (ARM v5TE) with better USB support
1619  * and different USB endpoint numbering than the SA1100 devices.  The
1620  * mach-pxa/usb-eth.c driver re-uses the device ids from mach-sa1100
1621  * so we rely on the endpoint descriptors.
1622  *
1623  *-------------------------------------------------------------------------*/
1624
1625 static const struct driver_info linuxdev_info = {
1626         .description =  "Linux Device",
1627         .check_connect = always_connected,
1628 };
1629
1630 static const struct driver_info yopy_info = {
1631         .description =  "Yopy",
1632         .check_connect = always_connected,
1633 };
1634
1635 static const struct driver_info blob_info = {
1636         .description =  "Boot Loader OBject",
1637         .check_connect = always_connected,
1638 };
1639
1640 #endif  /* CONFIG_USB_ARMLINUX */
1641
1642 \f
1643 #ifdef CONFIG_USB_ZAURUS
1644 #define HAVE_HARDWARE
1645
1646 #include <linux/crc32.h>
1647
1648 /*-------------------------------------------------------------------------
1649  *
1650  * Zaurus is also a SA-1110 based PDA, but one using a different driver
1651  * (and framing) for its USB slave/gadget controller than the case above.
1652  *
1653  * For the current version of that driver, the main way that framing is
1654  * nonstandard (also from perspective of the CDC ethernet model!) is a
1655  * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
1656  * haven't been fully worked around.
1657  *
1658  * PXA based models use the same framing, and also can't implement
1659  * set_interface properly.
1660  *
1661  *-------------------------------------------------------------------------*/
1662
1663 static struct sk_buff *
1664 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1665 {
1666         int                     padlen;
1667         struct sk_buff          *skb2;
1668
1669         padlen = 2;
1670         if (!skb_cloned (skb)) {
1671                 int     tailroom = skb_tailroom (skb);
1672                 if ((padlen + 4) <= tailroom)
1673                         goto done;
1674         }
1675         skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
1676         dev_kfree_skb_any (skb);
1677         skb = skb2;
1678         if (skb) {
1679                 u32             fcs;
1680 done:
1681                 fcs = crc32_le (~0, skb->data, skb->len);
1682                 fcs = ~fcs;
1683
1684                 *skb_put (skb, 1) = fcs       & 0xff;
1685                 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
1686                 *skb_put (skb, 1) = (fcs>>16) & 0xff;
1687                 *skb_put (skb, 1) = (fcs>>24) & 0xff;
1688         }
1689         return skb;
1690 }
1691
1692 static const struct driver_info zaurus_sl5x00_info = {
1693         .description =  "Sharp Zaurus SL-5x00",
1694         .flags =        FLAG_FRAMING_Z,
1695         .check_connect = always_connected,
1696         .bind =         cdc_bind,
1697         .unbind =       cdc_unbind,
1698         .tx_fixup =     zaurus_tx_fixup,
1699 };
1700 static const struct driver_info zaurus_pxa_info = {
1701         .description =  "Sharp Zaurus, PXA-2xx based",
1702         .flags =        FLAG_FRAMING_Z,
1703         .check_connect = always_connected,
1704         .tx_fixup =     zaurus_tx_fixup,
1705
1706         .in = 1, .out = 2,
1707 };
1708
1709 #endif
1710
1711 \f
1712 /*-------------------------------------------------------------------------
1713  *
1714  * Network Device Driver (peer link to "Host Device", from USB host)
1715  *
1716  *-------------------------------------------------------------------------*/
1717
1718 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1719 {
1720         struct usbnet   *dev = (struct usbnet *) net->priv;
1721
1722         if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
1723                 return -EINVAL;
1724 #ifdef  CONFIG_USB_NET1080
1725         if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) {
1726                 if (FRAMED_SIZE (new_mtu) > MAX_PACKET)
1727                         return -EINVAL;
1728         }
1729 #endif
1730 #ifdef  CONFIG_USB_GENESYS
1731         if (((dev->driver_info->flags) & FLAG_FRAMING_GL)
1732                         && new_mtu > GL_MAX_PACKET_LEN)
1733                 return -EINVAL;
1734 #endif
1735         // no second zero-length packet read wanted after mtu-sized packets
1736         if (((new_mtu + sizeof (struct ethhdr)) % dev->maxpacket) == 0)
1737                 return -EDOM;
1738         net->mtu = new_mtu;
1739         return 0;
1740 }
1741
1742 /*-------------------------------------------------------------------------*/
1743
1744 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1745 {
1746         return &((struct usbnet *) net->priv)->stats;
1747 }
1748
1749 /*-------------------------------------------------------------------------*/
1750
1751 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
1752  * completion callbacks.  2.5 should have fixed those bugs...
1753  */
1754
1755 static void defer_bh (struct usbnet *dev, struct sk_buff *skb)
1756 {
1757         struct sk_buff_head     *list = skb->list;
1758         unsigned long           flags;
1759
1760         spin_lock_irqsave (&list->lock, flags);
1761         __skb_unlink (skb, list);
1762         spin_unlock (&list->lock);
1763         spin_lock (&dev->done.lock);
1764         __skb_queue_tail (&dev->done, skb);
1765         if (dev->done.qlen == 1)
1766                 tasklet_schedule (&dev->bh);
1767         spin_unlock_irqrestore (&dev->done.lock, flags);
1768 }
1769
1770 /* some work can't be done in tasklets, so we use keventd
1771  *
1772  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
1773  * but tasklet_schedule() doesn't.  hope the failure is rare.
1774  */
1775 static void defer_kevent (struct usbnet *dev, int work)
1776 {
1777         set_bit (work, &dev->flags);
1778         if (!schedule_work (&dev->kevent))
1779                 deverr (dev, "kevent %d may have been dropped", work);
1780         else
1781                 devdbg (dev, "kevent %d scheduled", work);
1782 }
1783
1784 /*-------------------------------------------------------------------------*/
1785
1786 static void rx_complete (struct urb *urb, struct pt_regs *regs);
1787
1788 static void rx_submit (struct usbnet *dev, struct urb *urb, int flags)
1789 {
1790         struct sk_buff          *skb;
1791         struct skb_data         *entry;
1792         int                     retval = 0;
1793         unsigned long           lockflags;
1794         size_t                  size;
1795
1796 #ifdef CONFIG_USB_NET1080
1797         if (dev->driver_info->flags & FLAG_FRAMING_NC)
1798                 size = FRAMED_SIZE (dev->net->mtu);
1799         else
1800 #endif
1801 #ifdef CONFIG_USB_GENESYS
1802         if (dev->driver_info->flags & FLAG_FRAMING_GL)
1803                 size = GL_RCV_BUF_SIZE;
1804         else
1805 #endif
1806 #ifdef CONFIG_USB_ZAURUS
1807         if (dev->driver_info->flags & FLAG_FRAMING_Z)
1808                 size = 6 + (sizeof (struct ethhdr) + dev->net->mtu);
1809         else
1810 #endif
1811                 size = (sizeof (struct ethhdr) + dev->net->mtu);
1812
1813         if ((skb = alloc_skb (size, flags)) == 0) {
1814                 devdbg (dev, "no rx skb");
1815                 defer_kevent (dev, EVENT_RX_MEMORY);
1816                 usb_free_urb (urb);
1817                 return;
1818         }
1819
1820         entry = (struct skb_data *) skb->cb;
1821         entry->urb = urb;
1822         entry->dev = dev;
1823         entry->state = rx_start;
1824         entry->length = 0;
1825
1826         usb_fill_bulk_urb (urb, dev->udev, dev->in,
1827                 skb->data, size, rx_complete, skb);
1828         urb->transfer_flags |= URB_ASYNC_UNLINK;
1829
1830         spin_lock_irqsave (&dev->rxq.lock, lockflags);
1831
1832         if (netif_running (dev->net)
1833                         && netif_device_present (dev->net)
1834                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1835                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
1836                 case -EPIPE:
1837                         defer_kevent (dev, EVENT_RX_HALT);
1838                         break;
1839                 case -ENOMEM:
1840                         defer_kevent (dev, EVENT_RX_MEMORY);
1841                         break;
1842                 case -ENODEV:
1843                         devdbg (dev, "device gone");
1844                         netif_device_detach (dev->net);
1845                         break;
1846                 default:
1847                         devdbg (dev, "rx submit, %d", retval);
1848                         tasklet_schedule (&dev->bh);
1849                         break;
1850                 case 0:
1851                         __skb_queue_tail (&dev->rxq, skb);
1852                 }
1853         } else {
1854                 devdbg (dev, "rx: stopped");
1855                 retval = -ENOLINK;
1856         }
1857         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1858         if (retval) {
1859                 dev_kfree_skb_any (skb);
1860                 usb_free_urb (urb);
1861         }
1862 }
1863
1864
1865 /*-------------------------------------------------------------------------*/
1866
1867 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1868 {
1869         if (dev->driver_info->rx_fixup
1870                         && !dev->driver_info->rx_fixup (dev, skb))
1871                 goto error;
1872         // else network stack removes extra byte if we forced a short packet
1873
1874         if (skb->len) {
1875                 int     status;
1876
1877                 skb->dev = dev->net;
1878                 skb->protocol = eth_type_trans (skb, dev->net);
1879                 dev->stats.rx_packets++;
1880                 dev->stats.rx_bytes += skb->len;
1881
1882 #ifdef  VERBOSE
1883                 devdbg (dev, "< rx, len %d, type 0x%x",
1884                         skb->len + sizeof (struct ethhdr), skb->protocol);
1885 #endif
1886                 memset (skb->cb, 0, sizeof (struct skb_data));
1887                 status = netif_rx (skb);
1888                 if (status != NET_RX_SUCCESS)
1889                         devdbg (dev, "netif_rx status %d", status);
1890         } else {
1891                 devdbg (dev, "drop");
1892 error:
1893                 dev->stats.rx_errors++;
1894                 skb_queue_tail (&dev->done, skb);
1895         }
1896 }
1897
1898 /*-------------------------------------------------------------------------*/
1899
1900 static void rx_complete (struct urb *urb, struct pt_regs *regs)
1901 {
1902         struct sk_buff          *skb = (struct sk_buff *) urb->context;
1903         struct skb_data         *entry = (struct skb_data *) skb->cb;
1904         struct usbnet           *dev = entry->dev;
1905         int                     urb_status = urb->status;
1906
1907         skb_put (skb, urb->actual_length);
1908         entry->state = rx_done;
1909         entry->urb = 0;
1910
1911         switch (urb_status) {
1912             // success
1913             case 0:
1914                 if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) {
1915                         entry->state = rx_cleanup;
1916                         dev->stats.rx_errors++;
1917                         dev->stats.rx_length_errors++;
1918                         devdbg (dev, "rx length %d", skb->len);
1919                 }
1920                 break;
1921
1922             // stalls need manual reset. this is rare ... except that
1923             // when going through USB 2.0 TTs, unplug appears this way.
1924             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1925             // storm, recovering as needed.
1926             case -EPIPE:
1927                 dev->stats.rx_errors++;
1928                 defer_kevent (dev, EVENT_RX_HALT);
1929                 // FALLTHROUGH
1930
1931             // software-driven interface shutdown
1932             case -ECONNRESET:           // async unlink
1933             case -ESHUTDOWN:            // hardware gone
1934 #ifdef  VERBOSE
1935                 devdbg (dev, "rx shutdown, code %d", urb_status);
1936 #endif
1937                 goto block;
1938
1939             // we get controller i/o faults during khubd disconnect() delays.
1940             // throttle down resubmits, to avoid log floods; just temporarily,
1941             // so we still recover when the fault isn't a khubd delay.
1942             case -EPROTO:               // ehci
1943             case -ETIMEDOUT:            // ohci
1944             case -EILSEQ:               // uhci
1945                 dev->stats.rx_errors++;
1946                 if (!timer_pending (&dev->delay)) {
1947                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
1948                         devdbg (dev, "rx throttle %d", urb_status);
1949                 }
1950 block:
1951                 entry->state = rx_cleanup;
1952                 entry->urb = urb;
1953                 urb = 0;
1954                 break;
1955
1956             // data overrun ... flush fifo?
1957             case -EOVERFLOW:
1958                 dev->stats.rx_over_errors++;
1959                 // FALLTHROUGH
1960             
1961             default:
1962                 entry->state = rx_cleanup;
1963                 dev->stats.rx_errors++;
1964                 devdbg (dev, "rx status %d", urb_status);
1965                 break;
1966         }
1967
1968         defer_bh (dev, skb);
1969
1970         if (urb) {
1971                 if (netif_running (dev->net)
1972                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1973                         rx_submit (dev, urb, GFP_ATOMIC);
1974                         return;
1975                 }
1976                 usb_free_urb (urb);
1977         }
1978 #ifdef  VERBOSE
1979         devdbg (dev, "no read resubmitted");
1980 #endif /* VERBOSE */
1981 }
1982
1983 /*-------------------------------------------------------------------------*/
1984
1985 // unlink pending rx/tx; completion handlers do all other cleanup
1986
1987 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1988 {
1989         unsigned long           flags;
1990         struct sk_buff          *skb, *skbnext;
1991         int                     count = 0;
1992
1993         spin_lock_irqsave (&q->lock, flags);
1994         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1995                 struct skb_data         *entry;
1996                 struct urb              *urb;
1997                 int                     retval;
1998
1999                 entry = (struct skb_data *) skb->cb;
2000                 urb = entry->urb;
2001                 skbnext = skb->next;
2002
2003                 // during some PM-driven resume scenarios,
2004                 // these (async) unlinks complete immediately
2005                 retval = usb_unlink_urb (urb);
2006                 if (retval != -EINPROGRESS && retval != 0)
2007                         devdbg (dev, "unlink urb err, %d", retval);
2008                 else
2009                         count++;
2010         }
2011         spin_unlock_irqrestore (&q->lock, flags);
2012         return count;
2013 }
2014
2015
2016 /*-------------------------------------------------------------------------*/
2017
2018 // precondition: never called in_interrupt
2019
2020 static int usbnet_stop (struct net_device *net)
2021 {
2022         struct usbnet           *dev = (struct usbnet *) net->priv;
2023         int                     temp;
2024         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
2025         DECLARE_WAITQUEUE (wait, current);
2026
2027         netif_stop_queue (net);
2028
2029         if (dev->msg_level >= 2)
2030                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
2031                         dev->stats.rx_packets, dev->stats.tx_packets, 
2032                         dev->stats.rx_errors, dev->stats.tx_errors
2033                         );
2034
2035         // ensure there are no more active urbs
2036         add_wait_queue (&unlink_wakeup, &wait);
2037         dev->wait = &unlink_wakeup;
2038         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
2039
2040         // maybe wait for deletions to finish.
2041         while (skb_queue_len (&dev->rxq)
2042                         && skb_queue_len (&dev->txq)
2043                         && skb_queue_len (&dev->done)) {
2044                 set_current_state (TASK_UNINTERRUPTIBLE);
2045                 schedule_timeout (UNLINK_TIMEOUT_JIFFIES);
2046                 devdbg (dev, "waited for %d urb completions", temp);
2047         }
2048         dev->wait = 0;
2049         remove_wait_queue (&unlink_wakeup, &wait); 
2050
2051         /* deferred work (task, timer, softirq) must also stop.
2052          * can't flush_scheduled_work() until we drop rtnl (later),
2053          * else workers could deadlock; so make workers a NOP.
2054          */
2055         dev->flags = 0;
2056         del_timer_sync (&dev->delay);
2057         tasklet_kill (&dev->bh);
2058
2059         return 0;
2060 }
2061
2062 /*-------------------------------------------------------------------------*/
2063
2064 // posts reads, and enables write queuing
2065
2066 // precondition: never called in_interrupt
2067
2068 static int usbnet_open (struct net_device *net)
2069 {
2070         struct usbnet           *dev = (struct usbnet *) net->priv;
2071         int                     retval = 0;
2072         struct driver_info      *info = dev->driver_info;
2073
2074         // put into "known safe" state
2075         if (info->reset && (retval = info->reset (dev)) < 0) {
2076                 devinfo (dev, "open reset fail (%d) usbnet usb-%s-%s, %s",
2077                         retval,
2078                         dev->udev->bus->bus_name, dev->udev->devpath,
2079                         info->description);
2080                 goto done;
2081         }
2082
2083         // insist peer be connected
2084         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
2085                 devdbg (dev, "can't open; %d", retval);
2086                 goto done;
2087         }
2088
2089         netif_start_queue (net);
2090         if (dev->msg_level >= 2)
2091                 devinfo (dev, "open: enable queueing "
2092                                 "(rx %d, tx %d) mtu %d %s framing",
2093                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
2094                         (info->flags & (FLAG_FRAMING_NC | FLAG_FRAMING_GL))
2095                             ? ((info->flags & FLAG_FRAMING_NC)
2096                                 ? "NetChip"
2097                                 : "GeneSys")
2098                             : "raw"
2099                         );
2100
2101         // delay posting reads until we're fully open
2102         tasklet_schedule (&dev->bh);
2103 done:
2104         return retval;
2105 }
2106
2107 /*-------------------------------------------------------------------------*/
2108
2109 static inline int
2110 usbnet_ethtool_ioctl (struct net_device *net, void __user *useraddr)
2111 {
2112         struct usbnet   *dev = (struct usbnet *) net->priv;
2113         u32             cmd;
2114
2115         if (get_user (cmd, (u32 *)useraddr))
2116                 return -EFAULT;
2117         switch (cmd) {
2118
2119         case ETHTOOL_GDRVINFO: {        /* get driver info */
2120                 struct ethtool_drvinfo          info;
2121
2122                 memset (&info, 0, sizeof info);
2123                 info.cmd = ETHTOOL_GDRVINFO;
2124                 strncpy (info.driver, driver_name, sizeof info.driver);
2125                 strncpy (info.version, DRIVER_VERSION, sizeof info.version);
2126                 strncpy (info.fw_version, dev->driver_info->description,
2127                         sizeof info.fw_version);
2128                 usb_make_path (dev->udev, info.bus_info, sizeof info.bus_info);
2129                 if (copy_to_user (useraddr, &info, sizeof (info)))
2130                         return -EFAULT;
2131                 return 0;
2132                 }
2133
2134         case ETHTOOL_GLINK:             /* get link status */
2135                 if (dev->driver_info->check_connect) {
2136                         struct ethtool_value    edata = { ETHTOOL_GLINK };
2137
2138                         edata.data = dev->driver_info->check_connect (dev) == 0;
2139                         if (copy_to_user (useraddr, &edata, sizeof (edata)))
2140                                 return -EFAULT;
2141                         return 0;
2142                 }
2143                 break;
2144
2145         case ETHTOOL_GMSGLVL: {         /* get message-level */
2146                 struct ethtool_value    edata = {ETHTOOL_GMSGLVL};
2147
2148                 edata.data = dev->msg_level;
2149                 if (copy_to_user (useraddr, &edata, sizeof (edata)))
2150                         return -EFAULT;
2151                 return 0;
2152                 }
2153
2154         case ETHTOOL_SMSGLVL: {         /* set message-level */
2155                 struct ethtool_value    edata;
2156
2157                 if (copy_from_user (&edata, useraddr, sizeof (edata)))
2158                         return -EFAULT;
2159                 dev->msg_level = edata.data;
2160                 return 0;
2161                 }
2162         
2163         /* could also map RINGPARAM to RX/TX QLEN */
2164
2165         }
2166         /* Note that the ethtool user space code requires EOPNOTSUPP */
2167         return -EOPNOTSUPP;
2168 }
2169
2170 static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
2171 {
2172         switch (cmd) {
2173         case SIOCETHTOOL:
2174                 return usbnet_ethtool_ioctl (net, (void __user *)rq->ifr_data);
2175         default:
2176                 return -EOPNOTSUPP;
2177         }
2178 }
2179
2180 /*-------------------------------------------------------------------------*/
2181
2182 /* work that cannot be done in interrupt context uses keventd.
2183  *
2184  * NOTE:  with 2.5 we could do more of this using completion callbacks,
2185  * especially now that control transfers can be queued.
2186  */
2187 static void
2188 kevent (void *data)
2189 {
2190         struct usbnet           *dev = data;
2191         int                     status;
2192
2193         /* usb_clear_halt() needs a thread context */
2194         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
2195                 unlink_urbs (dev, &dev->txq);
2196                 status = usb_clear_halt (dev->udev, dev->out);
2197                 if (status < 0)
2198                         deverr (dev, "can't clear tx halt, status %d",
2199                                 status);
2200                 else {
2201                         clear_bit (EVENT_TX_HALT, &dev->flags);
2202                         netif_wake_queue (dev->net);
2203                 }
2204         }
2205         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
2206                 unlink_urbs (dev, &dev->rxq);
2207                 status = usb_clear_halt (dev->udev, dev->in);
2208                 if (status < 0)
2209                         deverr (dev, "can't clear rx halt, status %d",
2210                                 status);
2211                 else {
2212                         clear_bit (EVENT_RX_HALT, &dev->flags);
2213                         tasklet_schedule (&dev->bh);
2214                 }
2215         }
2216
2217         /* tasklet could resubmit itself forever if memory is tight */
2218         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
2219                 struct urb      *urb = 0;
2220
2221                 if (netif_running (dev->net))
2222                         urb = usb_alloc_urb (0, GFP_KERNEL);
2223                 else
2224                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
2225                 if (urb != 0) {
2226                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
2227                         rx_submit (dev, urb, GFP_KERNEL);
2228                         tasklet_schedule (&dev->bh);
2229                 }
2230         }
2231
2232         if (dev->flags)
2233                 devdbg (dev, "kevent done, flags = 0x%lx",
2234                         dev->flags);
2235 }
2236
2237 /*-------------------------------------------------------------------------*/
2238
2239 static void tx_complete (struct urb *urb, struct pt_regs *regs)
2240 {
2241         struct sk_buff          *skb = (struct sk_buff *) urb->context;
2242         struct skb_data         *entry = (struct skb_data *) skb->cb;
2243         struct usbnet           *dev = entry->dev;
2244
2245         if (urb->status == 0) {
2246                 dev->stats.tx_packets++;
2247                 dev->stats.tx_bytes += entry->length;
2248         } else {
2249                 dev->stats.tx_errors++;
2250
2251                 switch (urb->status) {
2252                 case -EPIPE:
2253                         defer_kevent (dev, EVENT_TX_HALT);
2254                         break;
2255
2256                 // like rx, tx gets controller i/o faults during khubd delays
2257                 // and so it uses the same throttling mechanism.
2258                 case -EPROTO:           // ehci
2259                 case -ETIMEDOUT:        // ohci
2260                 case -EILSEQ:           // uhci
2261                         if (!timer_pending (&dev->delay)) {
2262                                 mod_timer (&dev->delay,
2263                                         jiffies + THROTTLE_JIFFIES);
2264                                 devdbg (dev, "tx throttle %d", urb->status);
2265                         }
2266                         netif_stop_queue (dev->net);
2267                         break;
2268                 default:
2269                         devdbg (dev, "tx err %d", entry->urb->status);
2270                         break;
2271                 }
2272         }
2273
2274         urb->dev = 0;
2275         entry->state = tx_done;
2276         defer_bh (dev, skb);
2277 }
2278
2279 /*-------------------------------------------------------------------------*/
2280
2281 static void usbnet_tx_timeout (struct net_device *net)
2282 {
2283         struct usbnet           *dev = (struct usbnet *) net->priv;
2284
2285         unlink_urbs (dev, &dev->txq);
2286         tasklet_schedule (&dev->bh);
2287
2288         // FIXME: device recovery -- reset?
2289 }
2290
2291 /*-------------------------------------------------------------------------*/
2292
2293 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
2294 {
2295         struct usbnet           *dev = (struct usbnet *) net->priv;
2296         int                     length;
2297         int                     retval = NET_XMIT_SUCCESS;
2298         struct urb              *urb = 0;
2299         struct skb_data         *entry;
2300         struct driver_info      *info = dev->driver_info;
2301         unsigned long           flags;
2302 #ifdef  CONFIG_USB_NET1080
2303         struct nc_header        *header = 0;
2304         struct nc_trailer       *trailer = 0;
2305 #endif  /* CONFIG_USB_NET1080 */
2306
2307         // some devices want funky USB-level framing, for
2308         // win32 driver (usually) and/or hardware quirks
2309         if (info->tx_fixup) {
2310                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
2311                 if (!skb) {
2312                         devdbg (dev, "can't tx_fixup skb");
2313                         goto drop;
2314                 }
2315         }
2316         length = skb->len;
2317
2318         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
2319                 devdbg (dev, "no urb");
2320                 goto drop;
2321         }
2322
2323         entry = (struct skb_data *) skb->cb;
2324         entry->urb = urb;
2325         entry->dev = dev;
2326         entry->state = tx_start;
2327         entry->length = length;
2328
2329         // FIXME: reorganize a bit, so that fixup() fills out NetChip
2330         // framing too. (Packet ID update needs the spinlock...)
2331         // [ BETTER:  we already own net->xmit_lock, that's enough ]
2332
2333 #ifdef  CONFIG_USB_NET1080
2334         if (info->flags & FLAG_FRAMING_NC) {
2335                 header = (struct nc_header *) skb_push (skb, sizeof *header);
2336                 header->hdr_len = cpu_to_le16 (sizeof (*header));
2337                 header->packet_len = cpu_to_le16 (length);
2338                 if (!((skb->len + sizeof *trailer) & 0x01))
2339                         *skb_put (skb, 1) = PAD_BYTE;
2340                 trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
2341         } else
2342 #endif  /* CONFIG_USB_NET1080 */
2343
2344         usb_fill_bulk_urb (urb, dev->udev, dev->out,
2345                         skb->data, skb->len, tx_complete, skb);
2346         urb->transfer_flags |= URB_ASYNC_UNLINK;
2347
2348         /* don't assume the hardware handles USB_ZERO_PACKET
2349          * NOTE:  strictly conforming cdc-ether devices should expect
2350          * the ZLP here, but ignore the one-byte packet.
2351          *
2352          * FIXME zero that byte, if it doesn't require a new skb.
2353          */
2354         if ((length % dev->maxpacket) == 0)
2355                 urb->transfer_buffer_length++;
2356
2357         spin_lock_irqsave (&dev->txq.lock, flags);
2358
2359 #ifdef  CONFIG_USB_NET1080
2360         if (info->flags & FLAG_FRAMING_NC) {
2361                 header->packet_id = cpu_to_le16 ((u16)dev->dev_packet_id++);
2362                 put_unaligned (header->packet_id, &trailer->packet_id);
2363 #if 0
2364                 devdbg (dev, "frame >tx h %d p %d id %d",
2365                         header->hdr_len, header->packet_len,
2366                         header->packet_id);
2367 #endif
2368         }
2369 #endif  /* CONFIG_USB_NET1080 */
2370
2371         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
2372         case -EPIPE:
2373                 netif_stop_queue (net);
2374                 defer_kevent (dev, EVENT_TX_HALT);
2375                 break;
2376         default:
2377                 devdbg (dev, "tx: submit urb err %d", retval);
2378                 break;
2379         case 0:
2380                 net->trans_start = jiffies;
2381                 __skb_queue_tail (&dev->txq, skb);
2382                 if (dev->txq.qlen >= TX_QLEN (dev))
2383                         netif_stop_queue (net);
2384         }
2385         spin_unlock_irqrestore (&dev->txq.lock, flags);
2386
2387         if (retval) {
2388                 devdbg (dev, "drop, code %d", retval);
2389 drop:
2390                 retval = NET_XMIT_SUCCESS;
2391                 dev->stats.tx_dropped++;
2392                 if (skb)
2393                         dev_kfree_skb_any (skb);
2394                 usb_free_urb (urb);
2395 #ifdef  VERBOSE
2396         } else {
2397                 devdbg (dev, "> tx, len %d, type 0x%x",
2398                         length, skb->protocol);
2399 #endif
2400         }
2401         return retval;
2402 }
2403
2404
2405 /*-------------------------------------------------------------------------*/
2406
2407 // tasklet (work deferred from completions, in_irq) or timer
2408
2409 static void usbnet_bh (unsigned long param)
2410 {
2411         struct usbnet           *dev = (struct usbnet *) param;
2412         struct sk_buff          *skb;
2413         struct skb_data         *entry;
2414
2415         while ((skb = skb_dequeue (&dev->done))) {
2416                 entry = (struct skb_data *) skb->cb;
2417                 switch (entry->state) {
2418                     case rx_done:
2419                         entry->state = rx_cleanup;
2420                         rx_process (dev, skb);
2421                         continue;
2422                     case tx_done:
2423                     case rx_cleanup:
2424                         usb_free_urb (entry->urb);
2425                         dev_kfree_skb (skb);
2426                         continue;
2427                     default:
2428                         devdbg (dev, "bogus skb state %d", entry->state);
2429                 }
2430         }
2431
2432         // waiting for all pending urbs to complete?
2433         if (dev->wait) {
2434                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
2435                         wake_up (dev->wait);
2436                 }
2437
2438         // or are we maybe short a few urbs?
2439         } else if (netif_running (dev->net)
2440                         && netif_device_present (dev->net)
2441                         && !timer_pending (&dev->delay)
2442                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
2443                 int     temp = dev->rxq.qlen;
2444                 int     qlen = RX_QLEN (dev);
2445
2446                 if (temp < qlen) {
2447                         struct urb      *urb;
2448                         int             i;
2449
2450                         // don't refill the queue all at once
2451                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
2452                                 if ((urb = usb_alloc_urb (0, GFP_ATOMIC)) != 0)
2453                                         rx_submit (dev, urb, GFP_ATOMIC);
2454                         }
2455                         if (temp != dev->rxq.qlen)
2456                                 devdbg (dev, "rxqlen %d --> %d",
2457                                                 temp, dev->rxq.qlen);
2458                         if (dev->rxq.qlen < qlen)
2459                                 tasklet_schedule (&dev->bh);
2460                 }
2461                 if (dev->txq.qlen < TX_QLEN (dev))
2462                         netif_wake_queue (dev->net);
2463         }
2464 }
2465
2466
2467 \f
2468 /*-------------------------------------------------------------------------
2469  *
2470  * USB Device Driver support
2471  *
2472  *-------------------------------------------------------------------------*/
2473  
2474 // precondition: never called in_interrupt
2475
2476 static void usbnet_disconnect (struct usb_interface *intf)
2477 {
2478         struct usbnet           *dev;
2479         struct usb_device       *xdev;
2480
2481         dev = usb_get_intfdata(intf);
2482         usb_set_intfdata(intf, NULL);
2483         if (!dev)
2484                 return;
2485
2486         xdev = interface_to_usbdev (intf);
2487
2488         devinfo (dev, "unregister usbnet usb-%s-%s, %s",
2489                 xdev->bus->bus_name, xdev->devpath,
2490                 dev->driver_info->description);
2491         
2492         unregister_netdev (dev->net);
2493
2494         /* we don't hold rtnl here ... */
2495         flush_scheduled_work ();
2496
2497         if (dev->driver_info->unbind)
2498                 dev->driver_info->unbind (dev, intf);
2499
2500         kfree(dev->net);
2501         kfree (dev);
2502         usb_put_dev (xdev);
2503 }
2504
2505
2506 /*-------------------------------------------------------------------------*/
2507
2508 // precondition: never called in_interrupt
2509
2510 int
2511 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
2512 {
2513         struct usbnet                   *dev;
2514         struct net_device               *net;
2515         struct usb_host_interface       *interface;
2516         struct driver_info              *info;
2517         struct usb_device               *xdev;
2518         int                             status;
2519
2520         info = (struct driver_info *) prod->driver_info;
2521         if (!info) {
2522                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
2523                 return -ENODEV;
2524         }
2525         xdev = interface_to_usbdev (udev);
2526         interface = &udev->altsetting [udev->act_altsetting];
2527
2528         usb_get_dev (xdev);
2529
2530         status = -ENOMEM;
2531
2532         // set up our own records
2533         if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
2534                 dbg ("can't kmalloc dev");
2535                 goto out;
2536         }
2537         memset (dev, 0, sizeof *dev);
2538
2539         dev->udev = xdev;
2540         dev->driver_info = info;
2541         dev->msg_level = msg_level;
2542         skb_queue_head_init (&dev->rxq);
2543         skb_queue_head_init (&dev->txq);
2544         skb_queue_head_init (&dev->done);
2545         dev->bh.func = usbnet_bh;
2546         dev->bh.data = (unsigned long) dev;
2547         INIT_WORK (&dev->kevent, kevent, dev);
2548         dev->delay.function = usbnet_bh;
2549         dev->delay.data = (unsigned long) dev;
2550         init_timer (&dev->delay);
2551
2552         // set up network interface records
2553         net = alloc_etherdev(0);
2554         if (!net)
2555                 goto out1;
2556
2557         SET_MODULE_OWNER (net);
2558         dev->net = net;
2559         net->priv = dev;
2560         strcpy (net->name, "usb%d");
2561         memcpy (net->dev_addr, node_id, sizeof node_id);
2562
2563         // possible with some EHCI controllers
2564         if (dma_supported (&udev->dev, 0xffffffffffffffffULL))
2565                 net->features |= NETIF_F_HIGHDMA;
2566
2567         net->change_mtu = usbnet_change_mtu;
2568         net->get_stats = usbnet_get_stats;
2569         net->hard_start_xmit = usbnet_start_xmit;
2570         net->open = usbnet_open;
2571         net->stop = usbnet_stop;
2572         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
2573         net->tx_timeout = usbnet_tx_timeout;
2574         net->do_ioctl = usbnet_ioctl;
2575
2576         // allow device-specific bind/init procedures
2577         // NOTE net->name still not usable ...
2578         if (info->bind) {
2579                 status = info->bind (dev, udev);
2580                 // heuristic:  "usb%d" for links we know are two-host,
2581                 // else "eth%d" when there's reasonable doubt.  userspace
2582                 // can rename the link if it knows better.
2583                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
2584                                 && (net->dev_addr [0] & 0x02) == 0)
2585                         strcpy (net->name, "eth%d");
2586         } else if (!info->in || info->out)
2587                 status = get_endpoints (dev, udev);
2588         else {
2589                 dev->in = usb_rcvbulkpipe (xdev, info->in);
2590                 dev->out = usb_sndbulkpipe (xdev, info->out);
2591                 if (!(info->flags & FLAG_NO_SETINT))
2592                         status = usb_set_interface (xdev,
2593                                 interface->desc.bInterfaceNumber,
2594                                 interface->desc.bAlternateSetting);
2595                 else
2596                         status = 0;
2597
2598         }
2599         if (status < 0)
2600                 goto out2;
2601
2602         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
2603         
2604         SET_NETDEV_DEV(dev->net, &udev->dev);
2605         status = register_netdev (dev->net);
2606         if (status)
2607                 goto out3;
2608         devinfo (dev, "register usbnet at usb-%s-%s, %s",
2609                 xdev->bus->bus_name, xdev->devpath,
2610                 dev->driver_info->description);
2611
2612         // ok, it's ready to go.
2613         usb_set_intfdata (udev, dev);
2614
2615         // start as if the link is up
2616         netif_device_attach (dev->net);
2617
2618         return 0;
2619
2620 out3:
2621         if (info->unbind)
2622                 info->unbind (dev, udev);
2623 out2:
2624         kfree(net);
2625 out1:
2626         kfree(dev);
2627 out:
2628         usb_put_dev(xdev);
2629         return status;
2630 }
2631
2632
2633 /*-------------------------------------------------------------------------*/
2634
2635 #ifndef HAVE_HARDWARE
2636 #error You need to configure some hardware for this driver
2637 #endif
2638
2639 /*
2640  * chip vendor names won't normally be on the cables, and
2641  * may not be on the device.
2642  */
2643
2644 static const struct usb_device_id       products [] = {
2645
2646 #ifdef  CONFIG_USB_AN2720
2647 {
2648         USB_DEVICE (0x0547, 0x2720),    // AnchorChips defaults
2649         .driver_info =  (unsigned long) &an2720_info,
2650 }, {
2651         USB_DEVICE (0x0547, 0x2727),    // Xircom PGUNET
2652         .driver_info =  (unsigned long) &an2720_info,
2653 },
2654 #endif
2655
2656 #ifdef  CONFIG_USB_BELKIN
2657 {
2658         USB_DEVICE (0x050d, 0x0004),    // Belkin
2659         .driver_info =  (unsigned long) &belkin_info,
2660 }, {
2661         USB_DEVICE (0x056c, 0x8100),    // eTEK
2662         .driver_info =  (unsigned long) &belkin_info,
2663 }, {
2664         USB_DEVICE (0x0525, 0x9901),    // Advance USBNET (eTEK)
2665         .driver_info =  (unsigned long) &belkin_info,
2666 },
2667 #endif
2668
2669 #ifdef  CONFIG_USB_EPSON2888
2670 {
2671         USB_DEVICE (0x0525, 0x2888),    // EPSON USB client
2672         .driver_info    = (unsigned long) &epson2888_info,
2673 },
2674 #endif
2675
2676 #ifdef  CONFIG_USB_GENESYS
2677 {
2678         USB_DEVICE (0x05e3, 0x0502),    // GL620USB-A
2679         .driver_info =  (unsigned long) &genelink_info,
2680 },
2681         /* NOT: USB_DEVICE (0x05e3, 0x0501),    // GL620USB
2682          * that's half duplex, not currently supported
2683          */
2684 #endif
2685
2686 #ifdef  CONFIG_USB_NET1080
2687 {
2688         USB_DEVICE (0x0525, 0x1080),    // NetChip ref design
2689         .driver_info =  (unsigned long) &net1080_info,
2690 }, {
2691         USB_DEVICE (0x06D0, 0x0622),    // Laplink Gold
2692         .driver_info =  (unsigned long) &net1080_info,
2693 },
2694 #endif
2695
2696 #ifdef CONFIG_USB_PL2301
2697 {
2698         USB_DEVICE (0x067b, 0x0000),    // PL-2301
2699         .driver_info =  (unsigned long) &prolific_info,
2700 }, {
2701         USB_DEVICE (0x067b, 0x0001),    // PL-2302
2702         .driver_info =  (unsigned long) &prolific_info,
2703 },
2704 #endif
2705
2706 #ifdef  CONFIG_USB_ARMLINUX
2707 /*
2708  * SA-1100 using standard ARM Linux kernels, or compatible.
2709  * Often used when talking to Linux PDAs (iPaq, Yopy, etc).
2710  * The sa-1100 "usb-eth" driver handles the basic framing.
2711  *
2712  * PXA25x or PXA210 ...  these use a "usb-eth" driver much like
2713  * the sa1100 one, but hardware uses different endpoint numbers.
2714  */
2715 {
2716         // 1183 = 0x049F, both used as hex values?
2717         // Compaq "Itsy" vendor/product id
2718         USB_DEVICE (0x049F, 0x505A),
2719         .driver_info =  (unsigned long) &linuxdev_info,
2720 }, {
2721         USB_DEVICE (0x0E7E, 0x1001),    // G.Mate "Yopy"
2722         .driver_info =  (unsigned long) &yopy_info,
2723 }, {
2724         USB_DEVICE (0x8086, 0x07d3),    // "blob" bootloader
2725         .driver_info =  (unsigned long) &blob_info,
2726 }, 
2727 #endif
2728
2729 #ifdef  CONFIG_USB_ZAURUS
2730 /*
2731  * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2732  * Same idea as above, but different framing.
2733  *
2734  * PXA-2xx based models are also lying-about-cdc.
2735  */
2736 {
2737         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2738                           | USB_DEVICE_ID_MATCH_DEVICE, 
2739         .idVendor               = 0x04DD,
2740         .idProduct              = 0x8004,
2741         /* match the master interface */
2742         .bInterfaceClass        = USB_CLASS_COMM,
2743         .bInterfaceSubClass     = 6 /* Ethernet model */,
2744         .bInterfaceProtocol     = 0,
2745         .driver_info =  (unsigned long) &zaurus_sl5x00_info,
2746 }, {
2747         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2748                           | USB_DEVICE_ID_MATCH_DEVICE, 
2749         .idVendor               = 0x04DD,
2750         .idProduct              = 0x8005,       /* A-300 */
2751         .bInterfaceClass        = 0x02,
2752         .bInterfaceSubClass     = 0x0a,
2753         .bInterfaceProtocol     = 0x00,
2754         .driver_info =  (unsigned long) &zaurus_pxa_info,
2755 }, {
2756         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2757                           | USB_DEVICE_ID_MATCH_DEVICE, 
2758         .idVendor               = 0x04DD,
2759         .idProduct              = 0x8006,       /* B-500/SL-5600 */
2760         .bInterfaceClass        = 0x02,
2761         .bInterfaceSubClass     = 0x0a,
2762         .bInterfaceProtocol     = 0x00,
2763         .driver_info =  (unsigned long) &zaurus_pxa_info,
2764 }, {
2765         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2766                   | USB_DEVICE_ID_MATCH_DEVICE,
2767         .idVendor               = 0x04DD,
2768         .idProduct              = 0x8007,       /* C-700 */
2769         .bInterfaceClass    = 0x02,
2770         .bInterfaceSubClass = 0x0a,
2771         .bInterfaceProtocol = 0x00,
2772         .driver_info =  (unsigned long) &zaurus_pxa_info,
2773 }, {
2774         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2775                  | USB_DEVICE_ID_MATCH_DEVICE,
2776         .idVendor               = 0x04DD,
2777         .idProduct              = 0x9031,       /* C-750 */
2778         .bInterfaceClass        = 0x02,
2779         .bInterfaceSubClass     = 0x0a,
2780         .bInterfaceProtocol     = 0x00,
2781         .driver_info =  (unsigned long) &zaurus_pxa_info,
2782 },
2783 #endif
2784
2785 #ifdef  CONFIG_USB_CDCETHER
2786
2787 #ifndef CONFIG_USB_ZAURUS
2788         /* if we couldn't whitelist Zaurus, we must blacklist it */
2789 {
2790         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2791                           | USB_DEVICE_ID_MATCH_DEVICE, 
2792         .idVendor               = 0x04DD,
2793         .idProduct              = 0x8004,
2794         /* match the master interface */
2795         .bInterfaceClass        = USB_CLASS_COMM,
2796         .bInterfaceSubClass     = 6 /* Ethernet model */,
2797         .bInterfaceProtocol     = 0,
2798         .driver_info            = 0, /* BLACKLIST */
2799 },
2800         // FIXME blacklist the other Zaurus models too, sigh
2801 #endif
2802
2803 {
2804         /* CDC Ether uses two interfaces, not necessarily consecutive.
2805          * We match the main interface, ignoring the optional device
2806          * class so we could handle devices that aren't exclusively
2807          * CDC ether.
2808          *
2809          * NOTE:  this match must come AFTER entries working around
2810          * bugs/quirks in a given product (like Zaurus, above).
2811          */
2812         USB_INTERFACE_INFO (USB_CLASS_COMM, 6 /* Ethernet model */, 0),
2813         .driver_info = (unsigned long) &cdc_info,
2814 },
2815 #endif
2816
2817         { },            // END
2818 };
2819 MODULE_DEVICE_TABLE (usb, products);
2820
2821 static struct usb_driver usbnet_driver = {
2822         .owner =        THIS_MODULE,
2823         .name =         driver_name,
2824         .id_table =     products,
2825         .probe =        usbnet_probe,
2826         .disconnect =   usbnet_disconnect,
2827 };
2828
2829 /*-------------------------------------------------------------------------*/
2830
2831 static int __init usbnet_init (void)
2832 {
2833         // compiler should optimize this out
2834         if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
2835                 BUG ();
2836
2837         get_random_bytes (node_id, sizeof node_id);
2838         node_id [0] &= 0xfe;    // clear multicast bit
2839         node_id [0] |= 0x02;    // set local assignment bit (IEEE802)
2840
2841         if (usb_register (&usbnet_driver) < 0)
2842                 return -1;
2843
2844         return 0;
2845 }
2846 module_init (usbnet_init);
2847
2848 static void __exit usbnet_exit (void)
2849 {
2850         usb_deregister (&usbnet_driver);
2851 }
2852 module_exit (usbnet_exit);
2853
2854 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
2855 MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (numerous vendors)");
2856 MODULE_LICENSE ("GPL");