4c6479aaa40c23bcc8845425720ff8d500a149bb
[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 */
523         memset (info, 0, sizeof *info);
524         info->control = intf;
525         while (len > 3) {
526                 /* ignore bDescriptorType != CS_INTERFACE */
527                 if (buf [1] != 0x24)
528                         goto next_desc;
529
530                 /* bDescriptorSubType identifies three "must have" descriptors;
531                  * save them for later.
532                  */
533                 switch (buf [2]) {
534                 case 0x00:              /* Header, mostly useless */
535                         if (info->header)
536                                 goto bad_desc;
537                         info->header = (void *) buf;
538                         if (info->header->bLength != sizeof *info->header)
539                                 goto bad_desc;
540                         break;
541                 case 0x06:              /* Union (groups interfaces) */
542                         if (info->u)
543                                 goto bad_desc;
544                         info->u = (void *) buf;
545                         if (info->u->bLength != sizeof *info->u)
546                                 goto bad_desc;
547                         d = &intf->altsetting->desc;
548                         if (info->u->bMasterInterface0 != d->bInterfaceNumber)
549                                 goto bad_desc;
550                         info->data = dev->udev->actconfig->interface;
551                         if (intf != (info->data + info->u->bMasterInterface0))
552                                 goto bad_desc;
553
554                         /* a data interface altsetting does the real i/o */
555                         info->data += info->u->bSlaveInterface0;
556                         d = &info->data->altsetting->desc;
557                         if (info->u->bSlaveInterface0 != d->bInterfaceNumber
558                                     || d->bInterfaceClass != USB_CLASS_CDC_DATA)
559                                 goto bad_desc;
560                         if (usb_interface_claimed (info->data))
561                                 return -EBUSY;
562                         break;
563                 case 0x0F:              /* Ethernet Networking */
564                         if (info->ether)
565                                 goto bad_desc;
566                         info->ether = (void *) buf;
567                         if (info->ether->bLength != sizeof *info->ether)
568                                 goto bad_desc;
569                         break;
570                 }
571 next_desc:
572                 len -= buf [0]; /* bLength */
573                 buf += buf [0];
574         }
575         if (!info->header || !info ->u || !info->ether)
576                 goto bad_desc;
577
578 #ifdef CONFIG_USB_ZAURUS
579         /* Zaurus ethernet addresses aren't unique ... */
580         if ((dev->driver_info->flags & FLAG_FRAMING_Z) != 0)
581                 /* ignore */ ;
582         else
583 #endif
584         {
585                 status = get_ethernet_addr (dev, info->ether);
586                 if (status < 0)
587                         return status;
588         }
589
590         /* claim data interface and set it up ... with side effects.
591          * network traffic can't flow until an altsetting is enabled.
592          */
593         usb_driver_claim_interface (&usbnet_driver, info->data, dev);
594         status = get_endpoints (dev, info->data);
595         if (status < 0) {
596                 usb_driver_release_interface (&usbnet_driver, info->data);
597                 return status;
598         }
599
600         /* FIXME cdc-ether has some multicast code too, though it complains
601          * in routine cases.  info->ether describes the multicast support.
602          */
603
604         dev->net->mtu = cpu_to_le16p (&info->ether->wMaxSegmentSize)
605                 - ETH_HLEN;
606         return 0;
607
608 bad_desc:
609         // devdbg (dev, "bad CDC descriptors");
610         return -ENODEV;
611 }
612
613 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
614 {
615         struct cdc_info                 *info = (void *) &dev->data;
616
617         /* disconnect master --> disconnect slave */
618         if (intf == info->control && info->data) {
619                 usb_driver_release_interface (&usbnet_driver, info->data);
620                 info->data = 0;
621         }
622
623         /* and vice versa (just in case) */
624         else if (intf == info->data && info->control) {
625                 usb_driver_release_interface (&usbnet_driver, info->control);
626                 info->control = 0;
627         }
628
629 }
630
631 #endif  /* CONFIG_USB_ZAURUS || CONFIG_USB_CDCETHER */
632
633
634 #ifdef  CONFIG_USB_CDCETHER
635 #define HAVE_HARDWARE
636
637 static const struct driver_info cdc_info = {
638         .description =  "CDC Ethernet Device",
639         .flags =        FLAG_ETHER,
640         // .check_connect = cdc_check_connect,
641         .bind =         cdc_bind,
642         .unbind =       cdc_unbind,
643 };
644
645 #endif  /* CONFIG_USB_CDCETHER */
646
647
648 \f
649 #ifdef  CONFIG_USB_EPSON2888
650 #define HAVE_HARDWARE
651
652 /*-------------------------------------------------------------------------
653  *
654  * EPSON USB clients
655  *
656  * This is the same idea as Linux PDAs (below) except the firmware in the
657  * device might not be Tux-powered.  Epson provides reference firmware that
658  * implements this interface.  Product developers can reuse or modify that
659  * code, such as by using their own product and vendor codes.
660  *
661  * Support was from Juro Bystricky <bystricky.juro@erd.epson.com>
662  *
663  *-------------------------------------------------------------------------*/
664
665 static const struct driver_info epson2888_info = {
666         .description =  "Epson USB Device",
667         .check_connect = always_connected,
668
669         .in = 4, .out = 3,
670 };
671
672 #endif  /* CONFIG_USB_EPSON2888 */
673
674 \f
675 #ifdef CONFIG_USB_GENESYS
676 #define HAVE_HARDWARE
677
678 /*-------------------------------------------------------------------------
679  *
680  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
681  *
682  * ... should partially interop with the Win32 driver for this hardware
683  * The GeneSys docs imply there's some NDIS issue motivating this framing.
684  *
685  * Some info from GeneSys:
686  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
687  *    (Some cables, like the BAFO-100c, use the half duplex version.)
688  *  - For the full duplex model, the low bit of the version code says
689  *    which side is which ("left/right").
690  *  - For the half duplex type, a control/interrupt handshake settles
691  *    the transfer direction.  (That's disabled here, partially coded.)
692  *    A control URB would block until other side writes an interrupt.
693  *
694  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
695  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
696  *
697  *-------------------------------------------------------------------------*/
698
699 // control msg write command
700 #define GENELINK_CONNECT_WRITE                  0xF0
701 // interrupt pipe index
702 #define GENELINK_INTERRUPT_PIPE                 0x03
703 // interrupt read buffer size
704 #define INTERRUPT_BUFSIZE                       0x08
705 // interrupt pipe interval value
706 #define GENELINK_INTERRUPT_INTERVAL             0x10
707 // max transmit packet number per transmit
708 #define GL_MAX_TRANSMIT_PACKETS                 32
709 // max packet length
710 #define GL_MAX_PACKET_LEN                       1514
711 // max receive buffer size 
712 #define GL_RCV_BUF_SIZE         \
713         (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
714
715 struct gl_packet {
716         u32             packet_length;
717         char            packet_data [1];
718 };
719
720 struct gl_header {
721         u32                     packet_count;
722         struct gl_packet        packets;
723 };
724
725 #ifdef  GENLINK_ACK
726
727 // FIXME:  this code is incomplete, not debugged; it doesn't
728 // handle interrupts correctly.  interrupts should be generic
729 // code like all other device I/O, anyway.
730
731 struct gl_priv { 
732         struct urb      *irq_urb;
733         char            irq_buf [INTERRUPT_BUFSIZE];
734 };
735
736 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
737 {
738         int retval;
739
740         retval = usb_control_msg (dev->udev,
741                       usb_sndctrlpipe (dev->udev, 0),
742                       request,
743                       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
744                       value, 
745                       0,                        // index
746                       0,                        // data buffer
747                       0,                        // size
748                       CONTROL_TIMEOUT_JIFFIES);
749         return retval;
750 }
751
752 static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
753 {
754         int status = urb->status;
755         
756         switch (status) {
757         case 0:
758                 /* success */
759                 break;
760         case -ECONNRESET:
761         case -ENOENT:
762         case -ESHUTDOWN:
763                 /* this urb is terminated, clean up */
764                 dbg("%s - urb shutting down with status: %d",
765                                 __FUNCTION__, status);
766                 return;
767         default:
768                 dbg("%s - nonzero urb status received: %d",
769                                 __FUNCTION__, urb->status);
770         }
771
772         status = usb_submit_urb (urb, GFP_ATOMIC);
773         if (status)
774                 err ("%s - usb_submit_urb failed with result %d",
775                      __FUNCTION__, status);
776 }
777
778 static int gl_interrupt_read (struct usbnet *dev)
779 {
780         struct gl_priv  *priv = dev->priv_data;
781         int             retval;
782
783         // issue usb interrupt read
784         if (priv && priv->irq_urb) {
785                 // submit urb
786                 if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
787                         dbg ("gl_interrupt_read: submit fail - %X...", retval);
788                 else
789                         dbg ("gl_interrupt_read: submit success...");
790         }
791
792         return 0;
793 }
794
795 // check whether another side is connected
796 static int genelink_check_connect (struct usbnet *dev)
797 {
798         int                     retval;
799
800         dbg ("genelink_check_connect...");
801
802         // detect whether another side is connected
803         if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
804                 dbg ("%s: genelink_check_connect write fail - %X",
805                         dev->net->name, retval);
806                 return retval;
807         }
808
809         // usb interrupt read to ack another side 
810         if ((retval = gl_interrupt_read (dev)) != 0) {
811                 dbg ("%s: genelink_check_connect read fail - %X",
812                         dev->net->name, retval);
813                 return retval;
814         }
815
816         dbg ("%s: genelink_check_connect read success", dev->net->name);
817         return 0;
818 }
819
820 // allocate and initialize the private data for genelink
821 static int genelink_init (struct usbnet *dev)
822 {
823         struct gl_priv *priv;
824
825         // allocate the private data structure
826         if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
827                 dbg ("%s: cannot allocate private data per device",
828                         dev->net->name);
829                 return -ENOMEM;
830         }
831
832         // allocate irq urb
833         if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
834                 dbg ("%s: cannot allocate private irq urb per device",
835                         dev->net->name);
836                 kfree (priv);
837                 return -ENOMEM;
838         }
839
840         // fill irq urb
841         usb_fill_int_urb (priv->irq_urb, dev->udev,
842                 usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
843                 priv->irq_buf, INTERRUPT_BUFSIZE,
844                 gl_interrupt_complete, 0,
845                 GENELINK_INTERRUPT_INTERVAL);
846
847         // set private data pointer
848         dev->priv_data = priv;
849
850         return 0;
851 }
852
853 // release the private data
854 static int genelink_free (struct usbnet *dev)
855 {
856         struct gl_priv  *priv = dev->priv_data;
857
858         if (!priv) 
859                 return 0;
860
861 // FIXME:  can't cancel here; it's synchronous, and
862 // should have happened earlier in any case (interrupt
863 // handling needs to be generic)
864
865         // cancel irq urb first
866         usb_unlink_urb (priv->irq_urb);
867
868         // free irq urb
869         usb_free_urb (priv->irq_urb);
870
871         // free the private data structure
872         kfree (priv);
873
874         return 0;
875 }
876
877 #endif
878
879 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
880 {
881         struct gl_header        *header;
882         struct gl_packet        *packet;
883         struct sk_buff          *gl_skb;
884         int                     status;
885         u32                     size;
886
887         header = (struct gl_header *) skb->data;
888
889         // get the packet count of the received skb
890         le32_to_cpus (&header->packet_count);
891         if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
892                         || (header->packet_count < 0)) {
893                 dbg ("genelink: illegal received packet count %d",
894                         header->packet_count);
895                 return 0;
896         }
897
898         // set the current packet pointer to the first packet
899         packet = &header->packets;
900
901         // decrement the length for the packet count size 4 bytes
902         skb_pull (skb, 4);
903
904         while (header->packet_count > 1) {
905                 // get the packet length
906                 size = packet->packet_length;
907
908                 // this may be a broken packet
909                 if (size > GL_MAX_PACKET_LEN) {
910                         dbg ("genelink: illegal rx length %d", size);
911                         return 0;
912                 }
913
914                 // allocate the skb for the individual packet
915                 gl_skb = alloc_skb (size, GFP_ATOMIC);
916                 if (gl_skb) {
917
918                         // copy the packet data to the new skb
919                         memcpy(skb_put(gl_skb, size), packet->packet_data, size);
920                         gl_skb->dev = dev->net;
921
922                         // determine the packet's protocol ID
923                         gl_skb->protocol = eth_type_trans (gl_skb, dev->net);
924
925                         // update the status
926                         dev->stats.rx_packets++;
927                         dev->stats.rx_bytes += size;
928
929                         // notify os of the received packet
930                         status = netif_rx (gl_skb);
931                 }
932
933                 // advance to the next packet
934                 packet = (struct gl_packet *)
935                         &packet->packet_data [size];
936                 header->packet_count--;
937
938                 // shift the data pointer to the next gl_packet
939                 skb_pull (skb, size + 4);
940         }
941
942         // skip the packet length field 4 bytes
943         skb_pull (skb, 4);
944
945         if (skb->len > GL_MAX_PACKET_LEN) {
946                 dbg ("genelink: illegal rx length %d", skb->len);
947                 return 0;
948         }
949         return 1;
950 }
951
952 static struct sk_buff *
953 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
954 {
955         int     padlen;
956         int     length = skb->len;
957         int     headroom = skb_headroom (skb);
958         int     tailroom = skb_tailroom (skb);
959         u32     *packet_count;
960         u32     *packet_len;
961
962         // FIXME:  magic numbers, bleech
963         padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
964
965         if ((!skb_cloned (skb))
966                         && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
967                 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
968                         skb->data = memmove (skb->head + (4 + 4*1),
969                                              skb->data, skb->len);
970                         skb->tail = skb->data + skb->len;
971                 }
972         } else {
973                 struct sk_buff  *skb2;
974                 skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
975                 dev_kfree_skb_any (skb);
976                 skb = skb2;
977         }
978
979         // attach the packet count to the header
980         packet_count = (u32 *) skb_push (skb, (4 + 4*1));
981         packet_len = packet_count + 1;
982
983         // FIXME little endian?
984         *packet_count = 1;
985         *packet_len = length;
986
987         // add padding byte
988         if ((skb->len % dev->maxpacket) == 0)
989                 skb_put (skb, 1);
990
991         return skb;
992 }
993
994 static const struct driver_info genelink_info = {
995         .description =  "Genesys GeneLink",
996         .flags =        FLAG_FRAMING_GL | FLAG_NO_SETINT,
997         .rx_fixup =     genelink_rx_fixup,
998         .tx_fixup =     genelink_tx_fixup,
999
1000         .in = 1, .out = 2,
1001
1002 #ifdef  GENELINK_ACK
1003         .check_connect =genelink_check_connect,
1004 #endif
1005 };
1006
1007 #endif /* CONFIG_USB_GENESYS */
1008
1009
1010 \f
1011 #ifdef  CONFIG_USB_NET1080
1012 #define HAVE_HARDWARE
1013
1014 /*-------------------------------------------------------------------------
1015  *
1016  * Netchip 1080 driver ... http://www.netchip.com
1017  * Used in LapLink cables
1018  *
1019  *-------------------------------------------------------------------------*/
1020
1021 #define dev_packet_id   data[0]
1022 #define frame_errors    data[1]
1023
1024 /*
1025  * NetChip framing of ethernet packets, supporting additional error
1026  * checks for links that may drop bulk packets from inside messages.
1027  * Odd USB length == always short read for last usb packet.
1028  *      - nc_header
1029  *      - Ethernet header (14 bytes)
1030  *      - payload
1031  *      - (optional padding byte, if needed so length becomes odd)
1032  *      - nc_trailer
1033  *
1034  * This framing is to be avoided for non-NetChip devices.
1035  */
1036
1037 struct nc_header {              // packed:
1038         u16     hdr_len;                // sizeof nc_header (LE, all)
1039         u16     packet_len;             // payload size (including ethhdr)
1040         u16     packet_id;              // detects dropped packets
1041 #define MIN_HEADER      6
1042
1043         // all else is optional, and must start with:
1044         // u16  vendorId;               // from usb-if
1045         // u16  productId;
1046 } __attribute__((__packed__));
1047
1048 #define PAD_BYTE        ((unsigned char)0xAC)
1049
1050 struct nc_trailer {
1051         u16     packet_id;
1052 } __attribute__((__packed__));
1053
1054 // packets may use FLAG_FRAMING_NC and optional pad
1055 #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
1056                                 + sizeof (struct ethhdr) \
1057                                 + (mtu) \
1058                                 + 1 \
1059                                 + sizeof (struct nc_trailer))
1060
1061 #define MIN_FRAMED      FRAMED_SIZE(0)
1062
1063
1064 /*
1065  * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
1066  * before the hardware drops it.  If that's done, the driver will need to
1067  * frame network packets to guard against the dropped USB packets.  The win32
1068  * driver sets this for both sides of the link.
1069  */
1070 #define NC_READ_TTL_MS  ((u8)255)       // ms
1071
1072 /*
1073  * We ignore most registers and EEPROM contents.
1074  */
1075 #define REG_USBCTL      ((u8)0x04)
1076 #define REG_TTL         ((u8)0x10)
1077 #define REG_STATUS      ((u8)0x11)
1078
1079 /*
1080  * Vendor specific requests to read/write data
1081  */
1082 #define REQUEST_REGISTER        ((u8)0x10)
1083 #define REQUEST_EEPROM          ((u8)0x11)
1084
1085 static int
1086 nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
1087 {
1088         int status = usb_control_msg (dev->udev,
1089                 usb_rcvctrlpipe (dev->udev, 0),
1090                 req,
1091                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1092                 0, regnum,
1093                 retval_ptr, sizeof *retval_ptr,
1094                 CONTROL_TIMEOUT_JIFFIES);
1095         if (status > 0)
1096                 status = 0;
1097         if (!status)
1098                 le16_to_cpus (retval_ptr);
1099         return status;
1100 }
1101
1102 static inline int
1103 nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
1104 {
1105         return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
1106 }
1107
1108 // no retval ... can become async, usable in_interrupt()
1109 static void
1110 nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
1111 {
1112         usb_control_msg (dev->udev,
1113                 usb_sndctrlpipe (dev->udev, 0),
1114                 req,
1115                 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1116                 value, regnum,
1117                 0, 0,                   // data is in setup packet
1118                 CONTROL_TIMEOUT_JIFFIES);
1119 }
1120
1121 static inline void
1122 nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
1123 {
1124         nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
1125 }
1126
1127
1128 #if 0
1129 static void nc_dump_registers (struct usbnet *dev)
1130 {
1131         u8      reg;
1132         u16     *vp = kmalloc (sizeof (u16));
1133
1134         if (!vp) {
1135                 dbg ("no memory?");
1136                 return;
1137         }
1138
1139         dbg ("%s registers:", dev->net->name);
1140         for (reg = 0; reg < 0x20; reg++) {
1141                 int retval;
1142
1143                 // reading some registers is trouble
1144                 if (reg >= 0x08 && reg <= 0xf)
1145                         continue;
1146                 if (reg >= 0x12 && reg <= 0x1e)
1147                         continue;
1148
1149                 retval = nc_register_read (dev, reg, vp);
1150                 if (retval < 0)
1151                         dbg ("%s reg [0x%x] ==> error %d",
1152                                 dev->net->name, reg, retval);
1153                 else
1154                         dbg ("%s reg [0x%x] = 0x%x",
1155                                 dev->net->name, reg, *vp);
1156         }
1157         kfree (vp);
1158 }
1159 #endif
1160
1161
1162 /*-------------------------------------------------------------------------*/
1163
1164 /*
1165  * Control register
1166  */
1167
1168 #define USBCTL_WRITABLE_MASK    0x1f0f
1169 // bits 15-13 reserved, r/o
1170 #define USBCTL_ENABLE_LANG      (1 << 12)
1171 #define USBCTL_ENABLE_MFGR      (1 << 11)
1172 #define USBCTL_ENABLE_PROD      (1 << 10)
1173 #define USBCTL_ENABLE_SERIAL    (1 << 9)
1174 #define USBCTL_ENABLE_DEFAULTS  (1 << 8)
1175 // bits 7-4 reserved, r/o
1176 #define USBCTL_FLUSH_OTHER      (1 << 3)
1177 #define USBCTL_FLUSH_THIS       (1 << 2)
1178 #define USBCTL_DISCONN_OTHER    (1 << 1)
1179 #define USBCTL_DISCONN_THIS     (1 << 0)
1180
1181 static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
1182 {
1183 #ifdef DEBUG
1184         devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
1185                         " this%s%s;"
1186                         " other%s%s; r/o 0x%x",
1187                 dev->udev->bus->bus_name, dev->udev->devpath,
1188                 usbctl,
1189                 (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
1190                 (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
1191                 (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
1192                 (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
1193                 (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
1194
1195                 (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
1196                 (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
1197                 (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
1198                 (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
1199                 usbctl & ~USBCTL_WRITABLE_MASK
1200                 );
1201 #endif
1202 }
1203
1204 /*-------------------------------------------------------------------------*/
1205
1206 /*
1207  * Status register
1208  */
1209
1210 #define STATUS_PORT_A           (1 << 15)
1211
1212 #define STATUS_CONN_OTHER       (1 << 14)
1213 #define STATUS_SUSPEND_OTHER    (1 << 13)
1214 #define STATUS_MAILBOX_OTHER    (1 << 12)
1215 #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
1216
1217 #define STATUS_CONN_THIS        (1 << 6)
1218 #define STATUS_SUSPEND_THIS     (1 << 5)
1219 #define STATUS_MAILBOX_THIS     (1 << 4)
1220 #define STATUS_PACKETS_THIS(n)  (((n) >> 0) && 0x03)
1221
1222 #define STATUS_UNSPEC_MASK      0x0c8c
1223 #define STATUS_NOISE_MASK       ((u16)~(0x0303|STATUS_UNSPEC_MASK))
1224
1225
1226 static inline void nc_dump_status (struct usbnet *dev, u16 status)
1227 {
1228 #ifdef DEBUG
1229         devdbg (dev, "net1080 %s-%s status 0x%x:"
1230                         " this (%c) PKT=%d%s%s%s;"
1231                         " other PKT=%d%s%s%s; unspec 0x%x",
1232                 dev->udev->bus->bus_name, dev->udev->devpath,
1233                 status,
1234
1235                 // XXX the packet counts don't seem right
1236                 // (1 at reset, not 0); maybe UNSPEC too
1237
1238                 (status & STATUS_PORT_A) ? 'A' : 'B',
1239                 STATUS_PACKETS_THIS (status),
1240                 (status & STATUS_CONN_THIS) ? " CON" : "",
1241                 (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
1242                 (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
1243
1244                 STATUS_PACKETS_OTHER (status),
1245                 (status & STATUS_CONN_OTHER) ? " CON" : "",
1246                 (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
1247                 (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
1248
1249                 status & STATUS_UNSPEC_MASK
1250                 );
1251 #endif
1252 }
1253
1254 /*-------------------------------------------------------------------------*/
1255
1256 /*
1257  * TTL register
1258  */
1259
1260 #define TTL_THIS(ttl)   (0x00ff & ttl)
1261 #define TTL_OTHER(ttl)  (0x00ff & (ttl >> 8))
1262 #define MK_TTL(this,other)      ((u16)(((other)<<8)|(0x00ff&(this))))
1263
1264 static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
1265 {
1266 #ifdef DEBUG
1267         devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
1268                 dev->udev->bus->bus_name, dev->udev->devpath,
1269                 ttl,
1270
1271                 TTL_THIS (ttl),
1272                 TTL_OTHER (ttl)
1273                 );
1274 #endif
1275 }
1276
1277 /*-------------------------------------------------------------------------*/
1278
1279 static int net1080_reset (struct usbnet *dev)
1280 {
1281         u16             usbctl, status, ttl;
1282         u16             *vp = kmalloc (sizeof (u16), GFP_KERNEL);
1283         int             retval;
1284
1285         if (!vp)
1286                 return -ENOMEM;
1287
1288         // nc_dump_registers (dev);
1289
1290         if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
1291                 dbg ("can't read %s-%s status: %d",
1292                         dev->udev->bus->bus_name, dev->udev->devpath, retval);
1293                 goto done;
1294         }
1295         status = *vp;
1296         // nc_dump_status (dev, status);
1297
1298         if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
1299                 dbg ("can't read USBCTL, %d", retval);
1300                 goto done;
1301         }
1302         usbctl = *vp;
1303         // nc_dump_usbctl (dev, usbctl);
1304
1305         nc_register_write (dev, REG_USBCTL,
1306                         USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
1307
1308         if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
1309                 dbg ("can't read TTL, %d", retval);
1310                 goto done;
1311         }
1312         ttl = *vp;
1313         // nc_dump_ttl (dev, ttl);
1314
1315         nc_register_write (dev, REG_TTL,
1316                         MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
1317         dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
1318
1319         if (dev->msg_level >= 2)
1320                 devinfo (dev, "port %c, peer %sconnected",
1321                         (status & STATUS_PORT_A) ? 'A' : 'B',
1322                         (status & STATUS_CONN_OTHER) ? "" : "dis"
1323                         );
1324         retval = 0;
1325
1326 done:
1327         kfree (vp);
1328         return retval;
1329 }
1330
1331 static int net1080_check_connect (struct usbnet *dev)
1332 {
1333         int                     retval;
1334         u16                     status;
1335         u16                     *vp = kmalloc (sizeof (u16), GFP_KERNEL);
1336
1337         if (!vp)
1338                 return -ENOMEM;
1339         retval = nc_register_read (dev, REG_STATUS, vp);
1340         status = *vp;
1341         kfree (vp);
1342         if (retval != 0) {
1343                 dbg ("%s net1080_check_conn read - %d", dev->net->name, retval);
1344                 return retval;
1345         }
1346         if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
1347                 return -ENOLINK;
1348         return 0;
1349 }
1350
1351 static void nc_flush_complete (struct urb *urb, struct pt_regs *regs)
1352 {
1353         kfree (urb->context);
1354         usb_free_urb(urb);
1355 }
1356
1357 static void nc_ensure_sync (struct usbnet *dev)
1358 {
1359         dev->frame_errors++;
1360         if (dev->frame_errors > 5) {
1361                 struct urb              *urb;
1362                 struct usb_ctrlrequest  *req;
1363                 int                     status;
1364
1365                 /* Send a flush */
1366                 urb = usb_alloc_urb (0, SLAB_ATOMIC);
1367                 if (!urb)
1368                         return;
1369
1370                 req = kmalloc (sizeof *req, GFP_ATOMIC);
1371                 if (!req) {
1372                         usb_free_urb (urb);
1373                         return;
1374                 }
1375
1376                 req->bRequestType = USB_DIR_OUT
1377                         | USB_TYPE_VENDOR
1378                         | USB_RECIP_DEVICE;
1379                 req->bRequest = REQUEST_REGISTER;
1380                 req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS
1381                                 | USBCTL_FLUSH_OTHER);
1382                 req->wIndex = cpu_to_le16 (REG_USBCTL);
1383                 req->wLength = cpu_to_le16 (0);
1384
1385                 /* queue an async control request, we don't need
1386                  * to do anything when it finishes except clean up.
1387                  */
1388                 usb_fill_control_urb (urb, dev->udev,
1389                         usb_sndctrlpipe (dev->udev, 0),
1390                         (unsigned char *) req,
1391                         NULL, 0,
1392                         nc_flush_complete, req);
1393                 status = usb_submit_urb (urb, GFP_ATOMIC);
1394                 if (status) {
1395                         kfree (req);
1396                         usb_free_urb (urb);
1397                         return;
1398                 }
1399
1400                 devdbg (dev, "flush net1080; too many framing errors");
1401                 dev->frame_errors = 0;
1402         }
1403 }
1404
1405 static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
1406 {
1407         struct nc_header        *header;
1408         struct nc_trailer       *trailer;
1409
1410         if (!(skb->len & 0x01)
1411                         || MIN_FRAMED > skb->len
1412                         || skb->len > FRAMED_SIZE (dev->net->mtu)) {
1413                 dev->stats.rx_frame_errors++;
1414                 dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
1415                         (int)MIN_FRAMED, (int)FRAMED_SIZE (dev->net->mtu),
1416                         dev->net->mtu);
1417                 nc_ensure_sync (dev);
1418                 return 0;
1419         }
1420
1421         header = (struct nc_header *) skb->data;
1422         le16_to_cpus (&header->hdr_len);
1423         le16_to_cpus (&header->packet_len);
1424         if (FRAMED_SIZE (header->packet_len) > MAX_PACKET) {
1425                 dev->stats.rx_frame_errors++;
1426                 dbg ("packet too big, %d", header->packet_len);
1427                 nc_ensure_sync (dev);
1428                 return 0;
1429         } else if (header->hdr_len < MIN_HEADER) {
1430                 dev->stats.rx_frame_errors++;
1431                 dbg ("header too short, %d", header->hdr_len);
1432                 nc_ensure_sync (dev);
1433                 return 0;
1434         } else if (header->hdr_len > MIN_HEADER) {
1435                 // out of band data for us?
1436                 dbg ("header OOB, %d bytes",
1437                         header->hdr_len - MIN_HEADER);
1438                 nc_ensure_sync (dev);
1439                 // switch (vendor/product ids) { ... }
1440         }
1441         skb_pull (skb, header->hdr_len);
1442
1443         trailer = (struct nc_trailer *)
1444                 (skb->data + skb->len - sizeof *trailer);
1445         skb_trim (skb, skb->len - sizeof *trailer);
1446
1447         if ((header->packet_len & 0x01) == 0) {
1448                 if (skb->data [header->packet_len] != PAD_BYTE) {
1449                         dev->stats.rx_frame_errors++;
1450                         dbg ("bad pad");
1451                         return 0;
1452                 }
1453                 skb_trim (skb, skb->len - 1);
1454         }
1455         if (skb->len != header->packet_len) {
1456                 dev->stats.rx_frame_errors++;
1457                 dbg ("bad packet len %d (expected %d)",
1458                         skb->len, header->packet_len);
1459                 nc_ensure_sync (dev);
1460                 return 0;
1461         }
1462         if (header->packet_id != get_unaligned (&trailer->packet_id)) {
1463                 dev->stats.rx_fifo_errors++;
1464                 dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
1465                         header->packet_id, trailer->packet_id);
1466                 return 0;
1467         }
1468 #if 0
1469         devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
1470                 header->packet_len, header->packet_id);
1471 #endif
1472         dev->frame_errors = 0;
1473         return 1;
1474 }
1475
1476 static struct sk_buff *
1477 net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1478 {
1479         int                     padlen;
1480         struct sk_buff          *skb2;
1481
1482         padlen = ((skb->len + sizeof (struct nc_header)
1483                         + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
1484         if (!skb_cloned (skb)) {
1485                 int     headroom = skb_headroom (skb);
1486                 int     tailroom = skb_tailroom (skb);
1487
1488                 if ((padlen + sizeof (struct nc_trailer)) <= tailroom
1489                             && sizeof (struct nc_header) <= headroom)
1490                         /* There's enough head and tail room */
1491                         return skb;
1492
1493                 if ((sizeof (struct nc_header) + padlen
1494                                         + sizeof (struct nc_trailer)) <
1495                                 (headroom + tailroom)) {
1496                         /* There's enough total room, so just readjust */
1497                         skb->data = memmove (skb->head
1498                                                 + sizeof (struct nc_header),
1499                                             skb->data, skb->len);
1500                         skb->tail = skb->data + skb->len;
1501                         return skb;
1502                 }
1503         }
1504
1505         /* Create a new skb to use with the correct size */
1506         skb2 = skb_copy_expand (skb,
1507                                 sizeof (struct nc_header),
1508                                 sizeof (struct nc_trailer) + padlen,
1509                                 flags);
1510         dev_kfree_skb_any (skb);
1511         return skb2;
1512 }
1513
1514 static const struct driver_info net1080_info = {
1515         .description =  "NetChip TurboCONNECT",
1516         .flags =        FLAG_FRAMING_NC,
1517         .reset =        net1080_reset,
1518         .check_connect =net1080_check_connect,
1519         .rx_fixup =     net1080_rx_fixup,
1520         .tx_fixup =     net1080_tx_fixup,
1521 };
1522
1523 #endif /* CONFIG_USB_NET1080 */
1524
1525
1526 \f
1527 #ifdef CONFIG_USB_PL2301
1528 #define HAVE_HARDWARE
1529
1530 /*-------------------------------------------------------------------------
1531  *
1532  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
1533  *
1534  * The protocol and handshaking used here should be bug-compatible
1535  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
1536  *
1537  *-------------------------------------------------------------------------*/
1538
1539 /*
1540  * Bits 0-4 can be used for software handshaking; they're set from
1541  * one end, cleared from the other, "read" with the interrupt byte.
1542  */
1543 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
1544 /* reserved bit -- rx ready (6) ? */
1545 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
1546 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
1547 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
1548 #define PL_TX_C         (1<<2)          /* transmission complete */
1549 #define PL_TX_REQ       (1<<1)          /* transmission received */
1550 #define PL_PEER_E       (1<<0)          /* peer exists */
1551
1552 static inline int
1553 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
1554 {
1555         return usb_control_msg (dev->udev,
1556                 usb_rcvctrlpipe (dev->udev, 0),
1557                 req,
1558                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1559                 val, index,
1560                 0, 0,
1561                 CONTROL_TIMEOUT_JIFFIES);
1562 }
1563
1564 static inline int
1565 pl_clear_QuickLink_features (struct usbnet *dev, int val)
1566 {
1567         return pl_vendor_req (dev, 1, (u8) val, 0);
1568 }
1569
1570 static inline int
1571 pl_set_QuickLink_features (struct usbnet *dev, int val)
1572 {
1573         return pl_vendor_req (dev, 3, (u8) val, 0);
1574 }
1575
1576 /*-------------------------------------------------------------------------*/
1577
1578 static int pl_reset (struct usbnet *dev)
1579 {
1580         return pl_set_QuickLink_features (dev,
1581                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
1582 }
1583
1584 static const struct driver_info prolific_info = {
1585         .description =  "Prolific PL-2301/PL-2302",
1586         .flags =        FLAG_NO_SETINT,
1587                 /* some PL-2302 versions seem to fail usb_set_interface() */
1588         .reset =        pl_reset,
1589 };
1590
1591 #endif /* CONFIG_USB_PL2301 */
1592
1593
1594 \f
1595 #ifdef  CONFIG_USB_ARMLINUX
1596 #define HAVE_HARDWARE
1597
1598 /*-------------------------------------------------------------------------
1599  *
1600  * Intel's SA-1100 chip integrates basic USB support, and is used
1601  * in PDAs like some iPaqs, the Yopy, some Zaurus models, and more.
1602  * When they run Linux, arch/arm/mach-sa1100/usb-eth.c may be used to
1603  * network using minimal USB framing data.
1604  *
1605  * This describes the driver currently in standard ARM Linux kernels.
1606  * The Zaurus uses a different driver (see later).
1607  *
1608  * PXA25x and PXA210 use XScale cores (ARM v5TE) with better USB support
1609  * and different USB endpoint numbering than the SA1100 devices.  The
1610  * mach-pxa/usb-eth.c driver re-uses the device ids from mach-sa1100
1611  * so we rely on the endpoint descriptors.
1612  *
1613  *-------------------------------------------------------------------------*/
1614
1615 static const struct driver_info linuxdev_info = {
1616         .description =  "Linux Device",
1617         .check_connect = always_connected,
1618 };
1619
1620 static const struct driver_info yopy_info = {
1621         .description =  "Yopy",
1622         .check_connect = always_connected,
1623 };
1624
1625 static const struct driver_info blob_info = {
1626         .description =  "Boot Loader OBject",
1627         .check_connect = always_connected,
1628 };
1629
1630 #endif  /* CONFIG_USB_ARMLINUX */
1631
1632 \f
1633 #ifdef CONFIG_USB_ZAURUS
1634 #define HAVE_HARDWARE
1635
1636 #include <linux/crc32.h>
1637
1638 /*-------------------------------------------------------------------------
1639  *
1640  * Zaurus is also a SA-1110 based PDA, but one using a different driver
1641  * (and framing) for its USB slave/gadget controller than the case above.
1642  *
1643  * For the current version of that driver, the main way that framing is
1644  * nonstandard (also from perspective of the CDC ethernet model!) is a
1645  * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
1646  * haven't been fully worked around.
1647  *
1648  *-------------------------------------------------------------------------*/
1649
1650 static struct sk_buff *
1651 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, int flags)
1652 {
1653         int                     padlen;
1654         struct sk_buff          *skb2;
1655
1656         padlen = 2;
1657         if (!skb_cloned (skb)) {
1658                 int     tailroom = skb_tailroom (skb);
1659                 if ((padlen + 4) <= tailroom)
1660                         goto done;
1661         }
1662         skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
1663         dev_kfree_skb_any (skb);
1664         skb = skb2;
1665         if (skb) {
1666                 u32             fcs;
1667 done:
1668                 fcs = crc32_le (~0, skb->data, skb->len);
1669                 fcs = ~fcs;
1670
1671                 *skb_put (skb, 1) = fcs       & 0xff;
1672                 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
1673                 *skb_put (skb, 1) = (fcs>>16) & 0xff;
1674                 *skb_put (skb, 1) = (fcs>>24) & 0xff;
1675         }
1676         return skb;
1677 }
1678
1679 static const struct driver_info zaurus_sl5x00_info = {
1680         .description =  "Sharp Zaurus SL-5x00",
1681         .flags =        FLAG_FRAMING_Z,
1682         .check_connect = always_connected,
1683         .bind =         cdc_bind,
1684         .unbind =       cdc_unbind,
1685         .tx_fixup =     zaurus_tx_fixup,
1686 };
1687 static const struct driver_info zaurus_sla300_info = {
1688         .description =  "Sharp Zaurus SL-A300",
1689         .flags =        FLAG_FRAMING_Z,
1690         .check_connect = always_connected,
1691         .tx_fixup =     zaurus_tx_fixup,
1692
1693         .in = 1, .out = 2,
1694 };
1695 static const struct driver_info zaurus_slb500_info = {
1696         /* Japanese B500 ~= US SL-5600 */
1697         .description =  "Sharp Zaurus SL-B500",
1698         .flags =        FLAG_FRAMING_Z,
1699         .check_connect = always_connected,
1700         .tx_fixup =     zaurus_tx_fixup,
1701
1702         .in = 1, .out = 2,
1703 };
1704 static const struct driver_info zaurus_slc700_info = {
1705     .description =  "Sharp Zaurus SL-C700",
1706     .flags =    FLAG_FRAMING_Z,
1707     .check_connect = always_connected,
1708     .tx_fixup =     zaurus_tx_fixup,
1709
1710     .in = 1, .out = 2,
1711 };
1712
1713
1714 // SL-5600 and C-700 are PXA based; should resemble A300
1715
1716 #endif
1717
1718 \f
1719 /*-------------------------------------------------------------------------
1720  *
1721  * Network Device Driver (peer link to "Host Device", from USB host)
1722  *
1723  *-------------------------------------------------------------------------*/
1724
1725 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1726 {
1727         struct usbnet   *dev = (struct usbnet *) net->priv;
1728
1729         if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
1730                 return -EINVAL;
1731 #ifdef  CONFIG_USB_NET1080
1732         if (((dev->driver_info->flags) & FLAG_FRAMING_NC)) {
1733                 if (FRAMED_SIZE (new_mtu) > MAX_PACKET)
1734                         return -EINVAL;
1735         }
1736 #endif
1737 #ifdef  CONFIG_USB_GENESYS
1738         if (((dev->driver_info->flags) & FLAG_FRAMING_GL)
1739                         && new_mtu > GL_MAX_PACKET_LEN)
1740                 return -EINVAL;
1741 #endif
1742         // no second zero-length packet read wanted after mtu-sized packets
1743         if (((new_mtu + sizeof (struct ethhdr)) % dev->maxpacket) == 0)
1744                 return -EDOM;
1745         net->mtu = new_mtu;
1746         return 0;
1747 }
1748
1749 /*-------------------------------------------------------------------------*/
1750
1751 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1752 {
1753         return &((struct usbnet *) net->priv)->stats;
1754 }
1755
1756 /*-------------------------------------------------------------------------*/
1757
1758 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
1759  * completion callbacks.  2.5 should have fixed those bugs...
1760  */
1761
1762 static void defer_bh (struct usbnet *dev, struct sk_buff *skb)
1763 {
1764         struct sk_buff_head     *list = skb->list;
1765         unsigned long           flags;
1766
1767         spin_lock_irqsave (&list->lock, flags);
1768         __skb_unlink (skb, list);
1769         spin_unlock (&list->lock);
1770         spin_lock (&dev->done.lock);
1771         __skb_queue_tail (&dev->done, skb);
1772         if (dev->done.qlen == 1)
1773                 tasklet_schedule (&dev->bh);
1774         spin_unlock_irqrestore (&dev->done.lock, flags);
1775 }
1776
1777 /* some work can't be done in tasklets, so we use keventd
1778  *
1779  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
1780  * but tasklet_schedule() doesn't.  hope the failure is rare.
1781  */
1782 static void defer_kevent (struct usbnet *dev, int work)
1783 {
1784         set_bit (work, &dev->flags);
1785         if (!schedule_work (&dev->kevent))
1786                 deverr (dev, "kevent %d may have been dropped", work);
1787         else
1788                 devdbg (dev, "kevent %d scheduled", work);
1789 }
1790
1791 /*-------------------------------------------------------------------------*/
1792
1793 static void rx_complete (struct urb *urb, struct pt_regs *regs);
1794
1795 static void rx_submit (struct usbnet *dev, struct urb *urb, int flags)
1796 {
1797         struct sk_buff          *skb;
1798         struct skb_data         *entry;
1799         int                     retval = 0;
1800         unsigned long           lockflags;
1801         size_t                  size;
1802
1803 #ifdef CONFIG_USB_NET1080
1804         if (dev->driver_info->flags & FLAG_FRAMING_NC)
1805                 size = FRAMED_SIZE (dev->net->mtu);
1806         else
1807 #endif
1808 #ifdef CONFIG_USB_GENESYS
1809         if (dev->driver_info->flags & FLAG_FRAMING_GL)
1810                 size = GL_RCV_BUF_SIZE;
1811         else
1812 #endif
1813 #ifdef CONFIG_USB_ZAURUS
1814         if (dev->driver_info->flags & FLAG_FRAMING_Z)
1815                 size = 6 + (sizeof (struct ethhdr) + dev->net->mtu);
1816         else
1817 #endif
1818                 size = (sizeof (struct ethhdr) + dev->net->mtu);
1819
1820         if ((skb = alloc_skb (size, flags)) == 0) {
1821                 devdbg (dev, "no rx skb");
1822                 defer_kevent (dev, EVENT_RX_MEMORY);
1823                 usb_free_urb (urb);
1824                 return;
1825         }
1826
1827         entry = (struct skb_data *) skb->cb;
1828         entry->urb = urb;
1829         entry->dev = dev;
1830         entry->state = rx_start;
1831         entry->length = 0;
1832
1833         usb_fill_bulk_urb (urb, dev->udev, dev->in,
1834                 skb->data, size, rx_complete, skb);
1835         urb->transfer_flags |= URB_ASYNC_UNLINK;
1836
1837         spin_lock_irqsave (&dev->rxq.lock, lockflags);
1838
1839         if (netif_running (dev->net)
1840                         && netif_device_present (dev->net)
1841                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1842                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
1843                 case -EPIPE:
1844                         defer_kevent (dev, EVENT_RX_HALT);
1845                         break;
1846                 case -ENOMEM:
1847                         defer_kevent (dev, EVENT_RX_MEMORY);
1848                         break;
1849                 case -ENODEV:
1850                         devdbg (dev, "device gone");
1851                         netif_device_detach (dev->net);
1852                         break;
1853                 default:
1854                         devdbg (dev, "rx submit, %d", retval);
1855                         tasklet_schedule (&dev->bh);
1856                         break;
1857                 case 0:
1858                         __skb_queue_tail (&dev->rxq, skb);
1859                 }
1860         } else {
1861                 devdbg (dev, "rx: stopped");
1862                 retval = -ENOLINK;
1863         }
1864         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1865         if (retval) {
1866                 dev_kfree_skb_any (skb);
1867                 usb_free_urb (urb);
1868         }
1869 }
1870
1871
1872 /*-------------------------------------------------------------------------*/
1873
1874 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1875 {
1876         if (dev->driver_info->rx_fixup
1877                         && !dev->driver_info->rx_fixup (dev, skb))
1878                 goto error;
1879         // else network stack removes extra byte if we forced a short packet
1880
1881         if (skb->len) {
1882                 int     status;
1883
1884                 skb->dev = dev->net;
1885                 skb->protocol = eth_type_trans (skb, dev->net);
1886                 dev->stats.rx_packets++;
1887                 dev->stats.rx_bytes += skb->len;
1888
1889 #ifdef  VERBOSE
1890                 devdbg (dev, "< rx, len %d, type 0x%x",
1891                         skb->len + sizeof (struct ethhdr), skb->protocol);
1892 #endif
1893                 memset (skb->cb, 0, sizeof (struct skb_data));
1894                 status = netif_rx (skb);
1895                 if (status != NET_RX_SUCCESS)
1896                         devdbg (dev, "netif_rx status %d", status);
1897         } else {
1898                 devdbg (dev, "drop");
1899 error:
1900                 dev->stats.rx_errors++;
1901                 skb_queue_tail (&dev->done, skb);
1902         }
1903 }
1904
1905 /*-------------------------------------------------------------------------*/
1906
1907 static void rx_complete (struct urb *urb, struct pt_regs *regs)
1908 {
1909         struct sk_buff          *skb = (struct sk_buff *) urb->context;
1910         struct skb_data         *entry = (struct skb_data *) skb->cb;
1911         struct usbnet           *dev = entry->dev;
1912         int                     urb_status = urb->status;
1913
1914         skb_put (skb, urb->actual_length);
1915         entry->state = rx_done;
1916         entry->urb = 0;
1917
1918         switch (urb_status) {
1919             // success
1920             case 0:
1921                 if (MIN_PACKET > skb->len || skb->len > MAX_PACKET) {
1922                         entry->state = rx_cleanup;
1923                         dev->stats.rx_errors++;
1924                         dev->stats.rx_length_errors++;
1925                         devdbg (dev, "rx length %d", skb->len);
1926                 }
1927                 break;
1928
1929             // stalls need manual reset. this is rare ... except that
1930             // when going through USB 2.0 TTs, unplug appears this way.
1931             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1932             // storm, recovering as needed.
1933             case -EPIPE:
1934                 dev->stats.rx_errors++;
1935                 defer_kevent (dev, EVENT_RX_HALT);
1936                 // FALLTHROUGH
1937
1938             // software-driven interface shutdown
1939             case -ECONNRESET:           // async unlink
1940             case -ESHUTDOWN:            // hardware gone
1941 #ifdef  VERBOSE
1942                 devdbg (dev, "rx shutdown, code %d", urb_status);
1943 #endif
1944                 goto block;
1945
1946             // we get controller i/o faults during khubd disconnect() delays.
1947             // throttle down resubmits, to avoid log floods; just temporarily,
1948             // so we still recover when the fault isn't a khubd delay.
1949             case -EPROTO:               // ehci
1950             case -ETIMEDOUT:            // ohci
1951             case -EILSEQ:               // uhci
1952                 dev->stats.rx_errors++;
1953                 if (!timer_pending (&dev->delay)) {
1954                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
1955                         devdbg (dev, "rx throttle %d", urb_status);
1956                 }
1957 block:
1958                 entry->state = rx_cleanup;
1959                 entry->urb = urb;
1960                 urb = 0;
1961                 break;
1962
1963             // data overrun ... flush fifo?
1964             case -EOVERFLOW:
1965                 dev->stats.rx_over_errors++;
1966                 // FALLTHROUGH
1967             
1968             default:
1969                 entry->state = rx_cleanup;
1970                 dev->stats.rx_errors++;
1971                 devdbg (dev, "rx status %d", urb_status);
1972                 break;
1973         }
1974
1975         defer_bh (dev, skb);
1976
1977         if (urb) {
1978                 if (netif_running (dev->net)
1979                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1980                         rx_submit (dev, urb, GFP_ATOMIC);
1981                         return;
1982                 }
1983                 usb_free_urb (urb);
1984         }
1985 #ifdef  VERBOSE
1986         devdbg (dev, "no read resubmitted");
1987 #endif /* VERBOSE */
1988 }
1989
1990 /*-------------------------------------------------------------------------*/
1991
1992 // unlink pending rx/tx; completion handlers do all other cleanup
1993
1994 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1995 {
1996         unsigned long           flags;
1997         struct sk_buff          *skb, *skbnext;
1998         int                     count = 0;
1999
2000         spin_lock_irqsave (&q->lock, flags);
2001         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
2002                 struct skb_data         *entry;
2003                 struct urb              *urb;
2004                 int                     retval;
2005
2006                 entry = (struct skb_data *) skb->cb;
2007                 urb = entry->urb;
2008                 skbnext = skb->next;
2009
2010                 // during some PM-driven resume scenarios,
2011                 // these (async) unlinks complete immediately
2012                 retval = usb_unlink_urb (urb);
2013                 if (retval != -EINPROGRESS && retval != 0)
2014                         devdbg (dev, "unlink urb err, %d", retval);
2015                 else
2016                         count++;
2017         }
2018         spin_unlock_irqrestore (&q->lock, flags);
2019         return count;
2020 }
2021
2022
2023 /*-------------------------------------------------------------------------*/
2024
2025 // precondition: never called in_interrupt
2026
2027 static int usbnet_stop (struct net_device *net)
2028 {
2029         struct usbnet           *dev = (struct usbnet *) net->priv;
2030         int                     temp;
2031         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
2032         DECLARE_WAITQUEUE (wait, current);
2033
2034         netif_stop_queue (net);
2035
2036         if (dev->msg_level >= 2)
2037                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
2038                         dev->stats.rx_packets, dev->stats.tx_packets, 
2039                         dev->stats.rx_errors, dev->stats.tx_errors
2040                         );
2041
2042         // ensure there are no more active urbs
2043         add_wait_queue (&unlink_wakeup, &wait);
2044         dev->wait = &unlink_wakeup;
2045         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
2046
2047         // maybe wait for deletions to finish.
2048         while (skb_queue_len (&dev->rxq)
2049                         && skb_queue_len (&dev->txq)
2050                         && skb_queue_len (&dev->done)) {
2051                 set_current_state (TASK_UNINTERRUPTIBLE);
2052                 schedule_timeout (UNLINK_TIMEOUT_JIFFIES);
2053                 devdbg (dev, "waited for %d urb completions", temp);
2054         }
2055         dev->wait = 0;
2056         remove_wait_queue (&unlink_wakeup, &wait); 
2057
2058         // deferred work (task, timer, softirq) must also stop
2059         flush_scheduled_work ();
2060         del_timer_sync (&dev->delay);
2061         tasklet_kill (&dev->bh);
2062
2063         return 0;
2064 }
2065
2066 /*-------------------------------------------------------------------------*/
2067
2068 // posts reads, and enables write queuing
2069
2070 // precondition: never called in_interrupt
2071
2072 static int usbnet_open (struct net_device *net)
2073 {
2074         struct usbnet           *dev = (struct usbnet *) net->priv;
2075         int                     retval = 0;
2076         struct driver_info      *info = dev->driver_info;
2077
2078         // put into "known safe" state
2079         if (info->reset && (retval = info->reset (dev)) < 0) {
2080                 devinfo (dev, "open reset fail (%d) usbnet usb-%s-%s, %s",
2081                         retval,
2082                         dev->udev->bus->bus_name, dev->udev->devpath,
2083                         info->description);
2084                 goto done;
2085         }
2086
2087         // insist peer be connected
2088         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
2089                 devdbg (dev, "can't open; %d", retval);
2090                 goto done;
2091         }
2092
2093         netif_start_queue (net);
2094         if (dev->msg_level >= 2)
2095                 devinfo (dev, "open: enable queueing "
2096                                 "(rx %d, tx %d) mtu %d %s framing",
2097                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
2098                         (info->flags & (FLAG_FRAMING_NC | FLAG_FRAMING_GL))
2099                             ? ((info->flags & FLAG_FRAMING_NC)
2100                                 ? "NetChip"
2101                                 : "GeneSys")
2102                             : "raw"
2103                         );
2104
2105         // delay posting reads until we're fully open
2106         tasklet_schedule (&dev->bh);
2107 done:
2108         return retval;
2109 }
2110
2111 /*-------------------------------------------------------------------------*/
2112
2113 static inline int
2114 usbnet_ethtool_ioctl (struct net_device *net, void __user *useraddr)
2115 {
2116         struct usbnet   *dev = (struct usbnet *) net->priv;
2117         u32             cmd;
2118
2119         if (get_user (cmd, (u32 *)useraddr))
2120                 return -EFAULT;
2121         switch (cmd) {
2122
2123         case ETHTOOL_GDRVINFO: {        /* get driver info */
2124                 struct ethtool_drvinfo          info;
2125
2126                 memset (&info, 0, sizeof info);
2127                 info.cmd = ETHTOOL_GDRVINFO;
2128                 strncpy (info.driver, driver_name, sizeof info.driver);
2129                 strncpy (info.version, DRIVER_VERSION, sizeof info.version);
2130                 strncpy (info.fw_version, dev->driver_info->description,
2131                         sizeof info.fw_version);
2132                 usb_make_path (dev->udev, info.bus_info, sizeof info.bus_info);
2133                 if (copy_to_user (useraddr, &info, sizeof (info)))
2134                         return -EFAULT;
2135                 return 0;
2136                 }
2137
2138         case ETHTOOL_GLINK:             /* get link status */
2139                 if (dev->driver_info->check_connect) {
2140                         struct ethtool_value    edata = { ETHTOOL_GLINK };
2141
2142                         edata.data = dev->driver_info->check_connect (dev) == 0;
2143                         if (copy_to_user (useraddr, &edata, sizeof (edata)))
2144                                 return -EFAULT;
2145                         return 0;
2146                 }
2147                 break;
2148
2149         case ETHTOOL_GMSGLVL: {         /* get message-level */
2150                 struct ethtool_value    edata = {ETHTOOL_GMSGLVL};
2151
2152                 edata.data = dev->msg_level;
2153                 if (copy_to_user (useraddr, &edata, sizeof (edata)))
2154                         return -EFAULT;
2155                 return 0;
2156                 }
2157
2158         case ETHTOOL_SMSGLVL: {         /* set message-level */
2159                 struct ethtool_value    edata;
2160
2161                 if (copy_from_user (&edata, useraddr, sizeof (edata)))
2162                         return -EFAULT;
2163                 dev->msg_level = edata.data;
2164                 return 0;
2165                 }
2166         
2167         /* could also map RINGPARAM to RX/TX QLEN */
2168
2169         }
2170         /* Note that the ethtool user space code requires EOPNOTSUPP */
2171         return -EOPNOTSUPP;
2172 }
2173
2174 static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
2175 {
2176         switch (cmd) {
2177         case SIOCETHTOOL:
2178                 return usbnet_ethtool_ioctl (net, (void __user *)rq->ifr_data);
2179         default:
2180                 return -EOPNOTSUPP;
2181         }
2182 }
2183
2184 /*-------------------------------------------------------------------------*/
2185
2186 /* work that cannot be done in interrupt context uses keventd.
2187  *
2188  * NOTE:  with 2.5 we could do more of this using completion callbacks,
2189  * especially now that control transfers can be queued.
2190  */
2191 static void
2192 kevent (void *data)
2193 {
2194         struct usbnet           *dev = data;
2195         int                     status;
2196
2197         /* usb_clear_halt() needs a thread context */
2198         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
2199                 unlink_urbs (dev, &dev->txq);
2200                 status = usb_clear_halt (dev->udev, dev->out);
2201                 if (status < 0)
2202                         deverr (dev, "can't clear tx halt, status %d",
2203                                 status);
2204                 else {
2205                         clear_bit (EVENT_TX_HALT, &dev->flags);
2206                         netif_wake_queue (dev->net);
2207                 }
2208         }
2209         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
2210                 unlink_urbs (dev, &dev->rxq);
2211                 status = usb_clear_halt (dev->udev, dev->in);
2212                 if (status < 0)
2213                         deverr (dev, "can't clear rx halt, status %d",
2214                                 status);
2215                 else {
2216                         clear_bit (EVENT_RX_HALT, &dev->flags);
2217                         tasklet_schedule (&dev->bh);
2218                 }
2219         }
2220
2221         /* tasklet could resubmit itself forever if memory is tight */
2222         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
2223                 struct urb      *urb = 0;
2224
2225                 if (netif_running (dev->net))
2226                         urb = usb_alloc_urb (0, GFP_KERNEL);
2227                 else
2228                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
2229                 if (urb != 0) {
2230                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
2231                         rx_submit (dev, urb, GFP_KERNEL);
2232                         tasklet_schedule (&dev->bh);
2233                 }
2234         }
2235
2236         if (dev->flags)
2237                 devdbg (dev, "kevent done, flags = 0x%lx",
2238                         dev->flags);
2239 }
2240
2241 /*-------------------------------------------------------------------------*/
2242
2243 static void tx_complete (struct urb *urb, struct pt_regs *regs)
2244 {
2245         struct sk_buff          *skb = (struct sk_buff *) urb->context;
2246         struct skb_data         *entry = (struct skb_data *) skb->cb;
2247         struct usbnet           *dev = entry->dev;
2248
2249         if (urb->status == 0) {
2250                 dev->stats.tx_packets++;
2251                 dev->stats.tx_bytes += entry->length;
2252         } else {
2253                 dev->stats.tx_errors++;
2254
2255                 switch (urb->status) {
2256                 case -EPIPE:
2257                         defer_kevent (dev, EVENT_TX_HALT);
2258                         break;
2259
2260                 // like rx, tx gets controller i/o faults during khubd delays
2261                 // and so it uses the same throttling mechanism.
2262                 case -EPROTO:           // ehci
2263                 case -ETIMEDOUT:        // ohci
2264                 case -EILSEQ:           // uhci
2265                         if (!timer_pending (&dev->delay)) {
2266                                 mod_timer (&dev->delay,
2267                                         jiffies + THROTTLE_JIFFIES);
2268                                 devdbg (dev, "tx throttle %d", urb->status);
2269                         }
2270                         netif_stop_queue (dev->net);
2271                         break;
2272                 default:
2273                         devdbg (dev, "tx err %d", entry->urb->status);
2274                         break;
2275                 }
2276         }
2277
2278         urb->dev = 0;
2279         entry->state = tx_done;
2280         defer_bh (dev, skb);
2281 }
2282
2283 /*-------------------------------------------------------------------------*/
2284
2285 static void usbnet_tx_timeout (struct net_device *net)
2286 {
2287         struct usbnet           *dev = (struct usbnet *) net->priv;
2288
2289         unlink_urbs (dev, &dev->txq);
2290         tasklet_schedule (&dev->bh);
2291
2292         // FIXME: device recovery -- reset?
2293 }
2294
2295 /*-------------------------------------------------------------------------*/
2296
2297 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
2298 {
2299         struct usbnet           *dev = (struct usbnet *) net->priv;
2300         int                     length;
2301         int                     retval = NET_XMIT_SUCCESS;
2302         struct urb              *urb = 0;
2303         struct skb_data         *entry;
2304         struct driver_info      *info = dev->driver_info;
2305         unsigned long           flags;
2306 #ifdef  CONFIG_USB_NET1080
2307         struct nc_header        *header = 0;
2308         struct nc_trailer       *trailer = 0;
2309 #endif  /* CONFIG_USB_NET1080 */
2310
2311         // some devices want funky USB-level framing, for
2312         // win32 driver (usually) and/or hardware quirks
2313         if (info->tx_fixup) {
2314                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
2315                 if (!skb) {
2316                         devdbg (dev, "can't tx_fixup skb");
2317                         goto drop;
2318                 }
2319         }
2320         length = skb->len;
2321
2322         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
2323                 devdbg (dev, "no urb");
2324                 goto drop;
2325         }
2326
2327         entry = (struct skb_data *) skb->cb;
2328         entry->urb = urb;
2329         entry->dev = dev;
2330         entry->state = tx_start;
2331         entry->length = length;
2332
2333         // FIXME: reorganize a bit, so that fixup() fills out NetChip
2334         // framing too. (Packet ID update needs the spinlock...)
2335         // [ BETTER:  we already own net->xmit_lock, that's enough ]
2336
2337 #ifdef  CONFIG_USB_NET1080
2338         if (info->flags & FLAG_FRAMING_NC) {
2339                 header = (struct nc_header *) skb_push (skb, sizeof *header);
2340                 header->hdr_len = cpu_to_le16 (sizeof (*header));
2341                 header->packet_len = cpu_to_le16 (length);
2342                 if (!((skb->len + sizeof *trailer) & 0x01))
2343                         *skb_put (skb, 1) = PAD_BYTE;
2344                 trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
2345         } else
2346 #endif  /* CONFIG_USB_NET1080 */
2347
2348         usb_fill_bulk_urb (urb, dev->udev, dev->out,
2349                         skb->data, skb->len, tx_complete, skb);
2350         urb->transfer_flags |= URB_ASYNC_UNLINK;
2351
2352         /* don't assume the hardware handles USB_ZERO_PACKET
2353          * NOTE:  strictly conforming cdc-ether devices should expect
2354          * the ZLP here, but ignore the one-byte packet.
2355          *
2356          * FIXME zero that byte, if it doesn't require a new skb.
2357          */
2358         if ((length % dev->maxpacket) == 0)
2359                 urb->transfer_buffer_length++;
2360
2361         spin_lock_irqsave (&dev->txq.lock, flags);
2362
2363 #ifdef  CONFIG_USB_NET1080
2364         if (info->flags & FLAG_FRAMING_NC) {
2365                 header->packet_id = cpu_to_le16 ((u16)dev->dev_packet_id++);
2366                 put_unaligned (header->packet_id, &trailer->packet_id);
2367 #if 0
2368                 devdbg (dev, "frame >tx h %d p %d id %d",
2369                         header->hdr_len, header->packet_len,
2370                         header->packet_id);
2371 #endif
2372         }
2373 #endif  /* CONFIG_USB_NET1080 */
2374
2375         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
2376         case -EPIPE:
2377                 netif_stop_queue (net);
2378                 defer_kevent (dev, EVENT_TX_HALT);
2379                 break;
2380         default:
2381                 devdbg (dev, "tx: submit urb err %d", retval);
2382                 break;
2383         case 0:
2384                 net->trans_start = jiffies;
2385                 __skb_queue_tail (&dev->txq, skb);
2386                 if (dev->txq.qlen >= TX_QLEN (dev))
2387                         netif_stop_queue (net);
2388         }
2389         spin_unlock_irqrestore (&dev->txq.lock, flags);
2390
2391         if (retval) {
2392                 devdbg (dev, "drop, code %d", retval);
2393 drop:
2394                 retval = NET_XMIT_SUCCESS;
2395                 dev->stats.tx_dropped++;
2396                 if (skb)
2397                         dev_kfree_skb_any (skb);
2398                 usb_free_urb (urb);
2399 #ifdef  VERBOSE
2400         } else {
2401                 devdbg (dev, "> tx, len %d, type 0x%x",
2402                         length, skb->protocol);
2403 #endif
2404         }
2405         return retval;
2406 }
2407
2408
2409 /*-------------------------------------------------------------------------*/
2410
2411 // tasklet (work deferred from completions, in_irq) or timer
2412
2413 static void usbnet_bh (unsigned long param)
2414 {
2415         struct usbnet           *dev = (struct usbnet *) param;
2416         struct sk_buff          *skb;
2417         struct skb_data         *entry;
2418
2419         while ((skb = skb_dequeue (&dev->done))) {
2420                 entry = (struct skb_data *) skb->cb;
2421                 switch (entry->state) {
2422                     case rx_done:
2423                         entry->state = rx_cleanup;
2424                         rx_process (dev, skb);
2425                         continue;
2426                     case tx_done:
2427                     case rx_cleanup:
2428                         usb_free_urb (entry->urb);
2429                         dev_kfree_skb (skb);
2430                         continue;
2431                     default:
2432                         devdbg (dev, "bogus skb state %d", entry->state);
2433                 }
2434         }
2435
2436         // waiting for all pending urbs to complete?
2437         if (dev->wait) {
2438                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
2439                         wake_up (dev->wait);
2440                 }
2441
2442         // or are we maybe short a few urbs?
2443         } else if (netif_running (dev->net)
2444                         && netif_device_present (dev->net)
2445                         && !timer_pending (&dev->delay)
2446                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
2447                 int     temp = dev->rxq.qlen;
2448                 int     qlen = RX_QLEN (dev);
2449
2450                 if (temp < qlen) {
2451                         struct urb      *urb;
2452                         int             i;
2453
2454                         // don't refill the queue all at once
2455                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
2456                                 if ((urb = usb_alloc_urb (0, GFP_ATOMIC)) != 0)
2457                                         rx_submit (dev, urb, GFP_ATOMIC);
2458                         }
2459                         if (temp != dev->rxq.qlen)
2460                                 devdbg (dev, "rxqlen %d --> %d",
2461                                                 temp, dev->rxq.qlen);
2462                         if (dev->rxq.qlen < qlen)
2463                                 tasklet_schedule (&dev->bh);
2464                 }
2465                 if (dev->txq.qlen < TX_QLEN (dev))
2466                         netif_wake_queue (dev->net);
2467         }
2468 }
2469
2470
2471 \f
2472 /*-------------------------------------------------------------------------
2473  *
2474  * USB Device Driver support
2475  *
2476  *-------------------------------------------------------------------------*/
2477  
2478 // precondition: never called in_interrupt
2479
2480 static void usbnet_disconnect (struct usb_interface *intf)
2481 {
2482         struct usbnet           *dev;
2483         struct usb_device       *xdev;
2484
2485         dev = usb_get_intfdata(intf);
2486         usb_set_intfdata(intf, NULL);
2487         if (!dev)
2488                 return;
2489
2490         xdev = interface_to_usbdev (intf);
2491
2492         devinfo (dev, "unregister usbnet usb-%s-%s, %s",
2493                 xdev->bus->bus_name, xdev->devpath,
2494                 dev->driver_info->description);
2495         
2496         unregister_netdev (dev->net);
2497
2498         if (dev->driver_info->unbind)
2499                 dev->driver_info->unbind (dev, intf);
2500
2501         kfree(dev->net);
2502         kfree (dev);
2503         usb_put_dev (xdev);
2504 }
2505
2506
2507 /*-------------------------------------------------------------------------*/
2508
2509 // precondition: never called in_interrupt
2510
2511 int
2512 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
2513 {
2514         struct usbnet                   *dev;
2515         struct net_device               *net;
2516         struct usb_host_interface       *interface;
2517         struct driver_info              *info;
2518         struct usb_device               *xdev;
2519         int                             status;
2520
2521         info = (struct driver_info *) prod->driver_info;
2522         if (!info) {
2523                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
2524                 return -ENODEV;
2525         }
2526         xdev = interface_to_usbdev (udev);
2527         interface = &udev->altsetting [udev->act_altsetting];
2528
2529         usb_get_dev (xdev);
2530
2531         status = -ENOMEM;
2532
2533         // set up our own records
2534         if (!(dev = kmalloc (sizeof *dev, GFP_KERNEL))) {
2535                 dbg ("can't kmalloc dev");
2536                 goto out;
2537         }
2538         memset (dev, 0, sizeof *dev);
2539
2540         dev->udev = xdev;
2541         dev->driver_info = info;
2542         dev->msg_level = msg_level;
2543         skb_queue_head_init (&dev->rxq);
2544         skb_queue_head_init (&dev->txq);
2545         skb_queue_head_init (&dev->done);
2546         dev->bh.func = usbnet_bh;
2547         dev->bh.data = (unsigned long) dev;
2548         INIT_WORK (&dev->kevent, kevent, dev);
2549         dev->delay.function = usbnet_bh;
2550         dev->delay.data = (unsigned long) dev;
2551         init_timer (&dev->delay);
2552
2553         // set up network interface records
2554         net = alloc_etherdev(0);
2555         if (!net)
2556                 goto out1;
2557
2558         SET_MODULE_OWNER (net);
2559         dev->net = net;
2560         net->priv = dev;
2561         strcpy (net->name, "usb%d");
2562         memcpy (net->dev_addr, node_id, sizeof node_id);
2563
2564         // possible with some EHCI controllers
2565         if (dma_supported (&udev->dev, 0xffffffffffffffffULL))
2566                 net->features |= NETIF_F_HIGHDMA;
2567
2568         net->change_mtu = usbnet_change_mtu;
2569         net->get_stats = usbnet_get_stats;
2570         net->hard_start_xmit = usbnet_start_xmit;
2571         net->open = usbnet_open;
2572         net->stop = usbnet_stop;
2573         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
2574         net->tx_timeout = usbnet_tx_timeout;
2575         net->do_ioctl = usbnet_ioctl;
2576
2577         // allow device-specific bind/init procedures
2578         // NOTE net->name still not usable ...
2579         if (info->bind) {
2580                 status = info->bind (dev, udev);
2581                 // heuristic:  "usb%d" for links we know are two-host,
2582                 // else "eth%d" when there's reasonable doubt.  userspace
2583                 // can rename the link if it knows better.
2584                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
2585                                 && (net->dev_addr [0] & 0x02) == 0)
2586                         strcpy (net->name, "eth%d");
2587         } else if (!info->in || info->out)
2588                 status = get_endpoints (dev, udev);
2589         else {
2590                 dev->in = usb_rcvbulkpipe (xdev, info->in);
2591                 dev->out = usb_sndbulkpipe (xdev, info->out);
2592                 if (!(info->flags & FLAG_NO_SETINT))
2593                         status = usb_set_interface (xdev,
2594                                 interface->desc.bInterfaceNumber,
2595                                 interface->desc.bAlternateSetting);
2596                 else
2597                         status = 0;
2598
2599         }
2600         if (status < 0)
2601                 goto out2;
2602
2603         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
2604         
2605         SET_NETDEV_DEV(dev->net, &udev->dev);
2606         status = register_netdev (dev->net);
2607         if (status)
2608                 goto out3;
2609         devinfo (dev, "register usbnet at usb-%s-%s, %s",
2610                 xdev->bus->bus_name, xdev->devpath,
2611                 dev->driver_info->description);
2612
2613         // ok, it's ready to go.
2614         usb_set_intfdata (udev, dev);
2615
2616         // start as if the link is up
2617         netif_device_attach (dev->net);
2618
2619         return 0;
2620
2621 out3:
2622         if (info->unbind)
2623                 info->unbind (dev, udev);
2624 out2:
2625         kfree(net);
2626 out1:
2627         kfree(dev);
2628 out:
2629         usb_put_dev(xdev);
2630         return status;
2631 }
2632
2633
2634 /*-------------------------------------------------------------------------*/
2635
2636 #ifndef HAVE_HARDWARE
2637 #error You need to configure some hardware for this driver
2638 #endif
2639
2640 /*
2641  * chip vendor names won't normally be on the cables, and
2642  * may not be on the device.
2643  */
2644
2645 static const struct usb_device_id       products [] = {
2646
2647 #ifdef  CONFIG_USB_AN2720
2648 {
2649         USB_DEVICE (0x0547, 0x2720),    // AnchorChips defaults
2650         .driver_info =  (unsigned long) &an2720_info,
2651 }, {
2652         USB_DEVICE (0x0547, 0x2727),    // Xircom PGUNET
2653         .driver_info =  (unsigned long) &an2720_info,
2654 },
2655 #endif
2656
2657 #ifdef  CONFIG_USB_BELKIN
2658 {
2659         USB_DEVICE (0x050d, 0x0004),    // Belkin
2660         .driver_info =  (unsigned long) &belkin_info,
2661 }, {
2662         USB_DEVICE (0x056c, 0x8100),    // eTEK
2663         .driver_info =  (unsigned long) &belkin_info,
2664 }, {
2665         USB_DEVICE (0x0525, 0x9901),    // Advance USBNET (eTEK)
2666         .driver_info =  (unsigned long) &belkin_info,
2667 },
2668 #endif
2669
2670 #ifdef  CONFIG_USB_EPSON2888
2671 {
2672         USB_DEVICE (0x0525, 0x2888),    // EPSON USB client
2673         .driver_info    = (unsigned long) &epson2888_info,
2674 },
2675 #endif
2676
2677 #ifdef  CONFIG_USB_GENESYS
2678 {
2679         USB_DEVICE (0x05e3, 0x0502),    // GL620USB-A
2680         .driver_info =  (unsigned long) &genelink_info,
2681 },
2682         /* NOT: USB_DEVICE (0x05e3, 0x0501),    // GL620USB
2683          * that's half duplex, not currently supported
2684          */
2685 #endif
2686
2687 #ifdef  CONFIG_USB_NET1080
2688 {
2689         USB_DEVICE (0x0525, 0x1080),    // NetChip ref design
2690         .driver_info =  (unsigned long) &net1080_info,
2691 }, {
2692         USB_DEVICE (0x06D0, 0x0622),    // Laplink Gold
2693         .driver_info =  (unsigned long) &net1080_info,
2694 },
2695 #endif
2696
2697 #ifdef CONFIG_USB_PL2301
2698 {
2699         USB_DEVICE (0x067b, 0x0000),    // PL-2301
2700         .driver_info =  (unsigned long) &prolific_info,
2701 }, {
2702         USB_DEVICE (0x067b, 0x0001),    // PL-2302
2703         .driver_info =  (unsigned long) &prolific_info,
2704 },
2705 #endif
2706
2707 #ifdef  CONFIG_USB_ARMLINUX
2708 /*
2709  * SA-1100 using standard ARM Linux kernels, or compatible.
2710  * Often used when talking to Linux PDAs (iPaq, Yopy, etc).
2711  * The sa-1100 "usb-eth" driver handles the basic framing.
2712  *
2713  * PXA25x or PXA210 ...  these use a "usb-eth" driver much like
2714  * the sa1100 one, but hardware uses different endpoint numbers.
2715  */
2716 {
2717         // 1183 = 0x049F, both used as hex values?
2718         // Compaq "Itsy" vendor/product id
2719         USB_DEVICE (0x049F, 0x505A),
2720         .driver_info =  (unsigned long) &linuxdev_info,
2721 }, {
2722         USB_DEVICE (0x0E7E, 0x1001),    // G.Mate "Yopy"
2723         .driver_info =  (unsigned long) &yopy_info,
2724 }, {
2725         USB_DEVICE (0x8086, 0x07d3),    // "blob" bootloader
2726         .driver_info =  (unsigned long) &blob_info,
2727 }, 
2728 #endif
2729
2730 #ifdef  CONFIG_USB_ZAURUS
2731 /*
2732  * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2733  * Same idea as above, but different framing.
2734  */
2735 {
2736         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2737                           | USB_DEVICE_ID_MATCH_DEVICE, 
2738         .idVendor               = 0x04DD,
2739         .idProduct              = 0x8004,
2740         /* match the master interface */
2741         .bInterfaceClass        = USB_CLASS_COMM,
2742         .bInterfaceSubClass     = 6 /* Ethernet model */,
2743         .bInterfaceProtocol     = 0,
2744         .driver_info =  (unsigned long) &zaurus_sl5x00_info,
2745 }, {
2746         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2747                           | USB_DEVICE_ID_MATCH_DEVICE, 
2748         .idVendor               = 0x04DD,
2749         .idProduct              = 0x8005,
2750         .bInterfaceClass        = 0x02,
2751         .bInterfaceSubClass     = 0x0a,
2752         .bInterfaceProtocol     = 0x00,
2753         .driver_info =  (unsigned long) &zaurus_sla300_info,
2754 }, {
2755         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2756                           | USB_DEVICE_ID_MATCH_DEVICE, 
2757         .idVendor               = 0x04DD,
2758         .idProduct              = 0x8006,
2759         .bInterfaceClass        = 0x02,
2760         .bInterfaceSubClass     = 0x0a,
2761         .bInterfaceProtocol     = 0x00,
2762         .driver_info =  (unsigned long) &zaurus_slb500_info,
2763 }, {
2764         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2765                   | USB_DEVICE_ID_MATCH_DEVICE,
2766         .idVendor       = 0x04DD,
2767         .idProduct      = 0x8007,
2768         .bInterfaceClass    = 0x02,
2769         .bInterfaceSubClass = 0x0a,
2770         .bInterfaceProtocol = 0x00,
2771         .driver_info =  (unsigned long) &zaurus_slc700_info,
2772 },
2773 #endif
2774
2775 #ifdef  CONFIG_USB_CDCETHER
2776
2777 #ifndef CONFIG_USB_ZAURUS
2778         /* if we couldn't whitelist Zaurus, we must blacklist it */
2779 {
2780         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2781                           | USB_DEVICE_ID_MATCH_DEVICE, 
2782         .idVendor               = 0x04DD,
2783         .idProduct              = 0x8004,
2784         /* match the master interface */
2785         .bInterfaceClass        = USB_CLASS_COMM,
2786         .bInterfaceSubClass     = 6 /* Ethernet model */,
2787         .bInterfaceProtocol     = 0,
2788         .driver_info            = 0, /* BLACKLIST */
2789 },
2790 #endif
2791
2792 {
2793         /* CDC Ether uses two interfaces, not necessarily consecutive.
2794          * We match the main interface, ignoring the optional device
2795          * class so we could handle devices that aren't exclusively
2796          * CDC ether.
2797          *
2798          * NOTE:  this match must come AFTER entries working around
2799          * bugs/quirks in a given product (like Zaurus, above).
2800          */
2801         USB_INTERFACE_INFO (USB_CLASS_COMM, 6 /* Ethernet model */, 0),
2802         .driver_info = (unsigned long) &cdc_info,
2803 },
2804 #endif
2805
2806         { },            // END
2807 };
2808 MODULE_DEVICE_TABLE (usb, products);
2809
2810 static struct usb_driver usbnet_driver = {
2811         .owner =        THIS_MODULE,
2812         .name =         driver_name,
2813         .id_table =     products,
2814         .probe =        usbnet_probe,
2815         .disconnect =   usbnet_disconnect,
2816 };
2817
2818 /*-------------------------------------------------------------------------*/
2819
2820 static int __init usbnet_init (void)
2821 {
2822         // compiler should optimize this out
2823         if (sizeof (((struct sk_buff *)0)->cb) < sizeof (struct skb_data))
2824                 BUG ();
2825
2826         get_random_bytes (node_id, sizeof node_id);
2827         node_id [0] &= 0xfe;    // clear multicast bit
2828         node_id [0] |= 0x02;    // set local assignment bit (IEEE802)
2829
2830         if (usb_register (&usbnet_driver) < 0)
2831                 return -1;
2832
2833         return 0;
2834 }
2835 module_init (usbnet_init);
2836
2837 static void __exit usbnet_exit (void)
2838 {
2839         usb_deregister (&usbnet_driver);
2840 }
2841 module_exit (usbnet_exit);
2842
2843 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
2844 MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (numerous vendors)");
2845 MODULE_LICENSE ("GPL");