[PATCH] USB: usbnet (4/9) module for net1080 cables
[linux-2.6] / drivers / usb / net / usbnet.c
1 /*
2  * USB Networking Links
3  * Copyright (C) 2000-2005 by David Brownell
4  * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /*
23  * This is a generic "USB networking" framework that works with several
24  * kinds of full and high speed networking devices:
25  *
26  *   + USB host-to-host "network cables", used for IP-over-USB links.
27  *     These are often used for Laplink style connectivity products.
28  *      - AnchorChip 2720
29  *      - Belkin, eTEK (interops with Win32 drivers)
30  *      - GeneSys GL620USB-A
31  *      - NetChip 1080 (interoperates with NetChip Win32 drivers)
32  *      - Prolific PL-2301/2302 (replaces "plusb" driver)
33  *      - KC Technology KC2190
34  *
35  *   + Smart USB devices can support such links directly, using Internet
36  *     standard protocols instead of proprietary host-to-device links.
37  *      - Linux PDAs like iPaq, Yopy, and Zaurus
38  *      - The BLOB boot loader (for diskless booting)
39  *      - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
40  *      - Devices using EPSON's sample USB firmware
41  *      - CDC-Ethernet class devices, such as many cable modems
42  *
43  *   + Adapters to networks such as Ethernet.
44  *      - AX8817X based USB 2.0 products
45  *
46  * Links to these devices can be bridged using Linux Ethernet bridging.
47  * With minor exceptions, these all use similar USB framing for network
48  * traffic, but need different protocols for control traffic.
49  *
50  * USB devices can implement their side of this protocol at the cost
51  * of two bulk endpoints; it's not restricted to "cable" applications.
52  * See the SA1110, Zaurus, or EPSON device/client support in this driver;
53  * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
54  * "g_ether" (in the Linux "gadget" framework) implement that behavior
55  * within devices.
56  *
57  *
58  * CHANGELOG:
59  *
60  * 13-sep-2000  experimental, new
61  * 10-oct-2000  usb_device_id table created. 
62  * 28-oct-2000  misc fixes; mostly, discard more TTL-mangled rx packets.
63  * 01-nov-2000  usb_device_id table and probing api update by
64  *              Adam J. Richter <adam@yggdrasil.com>.
65  * 18-dec-2000  (db) tx watchdog, "net1080" renaming to "usbnet", device_info
66  *              and prolific support, isolate net1080-specific bits, cleanup.
67  *              fix unlink_urbs oops in D3 PM resume code path.
68  *
69  * 02-feb-2001  (db) fix tx skb sharing, packet length, match_flags, ...
70  * 08-feb-2001  stubbed in "linuxdev", maybe the SA-1100 folk can use it;
71  *              AnchorChips 2720 support (from spec) for testing;
72  *              fix bit-ordering problem with ethernet multicast addr
73  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
74  *              updates. Oleg Drokin (green@iXcelerator.com)
75  * 25-mar-2001  More SA-1100 updates, including workaround for ip problem
76  *              expecting cleared skb->cb and framing change to match latest
77  *              handhelds.org version (Oleg).  Enable device IDs from the
78  *              Win32 Belkin driver; other cleanups (db).
79  * 16-jul-2001  Bugfixes for uhci oops-on-unplug, Belkin support, various
80  *              cleanups for problems not yet seen in the field. (db)
81  * 17-oct-2001  Handle "Advance USBNET" product, like Belkin/eTEK devices,
82  *              from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
83  *              rx unlinks somehow weren't async; minor cleanup.
84  * 03-nov-2001  Merged GeneSys driver; original code from Jiun-Jie Huang
85  *              <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
86  *              <utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
87  *              tie mostly to (sub)driver info.  Workaround some PL-2302
88  *              chips that seem to reject SET_INTERFACE requests.
89  *
90  * 06-apr-2002  Added ethtool support, based on a patch from Brad Hards.
91  *              Level of diagnostics is more configurable; they use device
92  *              location (usb_device->devpath) instead of address (2.5).
93  *              For tx_fixup, memflags can't be NOIO.
94  * 07-may-2002  Generalize/cleanup keventd support, handling rx stalls (mostly
95  *              for USB 2.0 TTs) and memory shortages (potential) too. (db)
96  *              Use "locally assigned" IEEE802 address space. (Brad Hards)
97  * 18-oct-2002  Support for Zaurus (Pavel Machek), related cleanup (db).
98  * 14-dec-2002  Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
99  *              cleanups and stubbed PXA-250 support (db), fix for framing
100  *              issues on Z, net1080, and gl620a (Toby Milne)
101  *
102  * 31-mar-2003  Use endpoint descriptors:  high speed support, simpler sa1100
103  *              vs pxa25x, and CDC Ethernet.  Throttle down log floods on
104  *              disconnect; other cleanups. (db)  Flush net1080 fifos
105  *              after several sequential framing errors. (Johannes Erdfelt)
106  * 22-aug-2003  AX8817X support (Dave Hollis).
107  * 14-jun-2004  Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
108  *              (Neil Bortnak)
109  * 03-nov-2004  Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
110  *
111  * 01-feb-2005  AX88772 support (Phil Chang & Dave Hollis)
112  *-------------------------------------------------------------------------*/
113
114 // #define      DEBUG                   // error path messages, extra info
115 // #define      VERBOSE                 // more; success messages
116
117 #include <linux/config.h>
118 #ifdef  CONFIG_USB_DEBUG
119 #   define DEBUG
120 #endif
121 #include <linux/module.h>
122 #include <linux/sched.h>
123 #include <linux/init.h>
124 #include <linux/netdevice.h>
125 #include <linux/etherdevice.h>
126 #include <linux/ethtool.h>
127 #include <linux/workqueue.h>
128 #include <linux/mii.h>
129 #include <linux/usb.h>
130
131 #include "usbnet.h"
132
133 #define DRIVER_VERSION          "22-Aug-2005"
134
135
136 /*-------------------------------------------------------------------------*/
137
138 /*
139  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
140  * Several dozen bytes of IPv4 data can fit in two such transactions.
141  * One maximum size Ethernet packet takes twenty four of them.
142  * For high speed, each frame comfortably fits almost 36 max size
143  * Ethernet packets (so queues should be bigger).
144  *
145  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
146  * let the USB host controller be busy for 5msec or more before an irq
147  * is required, under load.  Jumbograms change the equation.
148  */
149 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
150 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
151
152 // reawaken network queue this soon after stopping; else watchdog barks
153 #define TX_TIMEOUT_JIFFIES      (5*HZ)
154
155 // throttle rx/tx briefly after some faults, so khubd might disconnect()
156 // us (it polls at HZ/4 usually) before we report too many false errors.
157 #define THROTTLE_JIFFIES        (HZ/8)
158
159 // for vendor-specific control operations
160 #define CONTROL_TIMEOUT_MS      USB_CTRL_GET_TIMEOUT
161
162 // between wakeups
163 #define UNLINK_TIMEOUT_MS       3
164
165 /*-------------------------------------------------------------------------*/
166
167 // randomly generated ethernet address
168 static u8       node_id [ETH_ALEN];
169
170 static const char driver_name [] = "usbnet";
171
172 /* use ethtool to change the level for any given device */
173 static int msg_level = -1;
174 module_param (msg_level, int, 0);
175 MODULE_PARM_DESC (msg_level, "Override default message level");
176
177 /*-------------------------------------------------------------------------*/
178
179 static u32 usbnet_get_link (struct net_device *);
180
181 /* mostly for PDA style devices, which are always connected if present */
182 static int always_connected (struct usbnet *dev)
183 {
184         return 0;
185 }
186
187 /* handles CDC Ethernet and many other network "bulk data" interfaces */
188 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
189 {
190         int                             tmp;
191         struct usb_host_interface       *alt = NULL;
192         struct usb_host_endpoint        *in = NULL, *out = NULL;
193         struct usb_host_endpoint        *status = NULL;
194
195         for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
196                 unsigned        ep;
197
198                 in = out = status = NULL;
199                 alt = intf->altsetting + tmp;
200
201                 /* take the first altsetting with in-bulk + out-bulk;
202                  * remember any status endpoint, just in case;
203                  * ignore other endpoints and altsetttings.
204                  */
205                 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
206                         struct usb_host_endpoint        *e;
207                         int                             intr = 0;
208
209                         e = alt->endpoint + ep;
210                         switch (e->desc.bmAttributes) {
211                         case USB_ENDPOINT_XFER_INT:
212                                 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
213                                         continue;
214                                 intr = 1;
215                                 /* FALLTHROUGH */
216                         case USB_ENDPOINT_XFER_BULK:
217                                 break;
218                         default:
219                                 continue;
220                         }
221                         if (e->desc.bEndpointAddress & USB_DIR_IN) {
222                                 if (!intr && !in)
223                                         in = e;
224                                 else if (intr && !status)
225                                         status = e;
226                         } else {
227                                 if (!out)
228                                         out = e;
229                         }
230                 }
231                 if (in && out)
232                         break;
233         }
234         if (!alt || !in || !out)
235                 return -EINVAL;
236
237         if (alt->desc.bAlternateSetting != 0
238                         || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
239                 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
240                                 alt->desc.bAlternateSetting);
241                 if (tmp < 0)
242                         return tmp;
243         }
244         
245         dev->in = usb_rcvbulkpipe (dev->udev,
246                         in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
247         dev->out = usb_sndbulkpipe (dev->udev,
248                         out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
249         dev->status = status;
250         return 0;
251 }
252 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
253
254 static void intr_complete (struct urb *urb, struct pt_regs *regs);
255
256 static int init_status (struct usbnet *dev, struct usb_interface *intf)
257 {
258         char            *buf = NULL;
259         unsigned        pipe = 0;
260         unsigned        maxp;
261         unsigned        period;
262
263         if (!dev->driver_info->status)
264                 return 0;
265
266         pipe = usb_rcvintpipe (dev->udev,
267                         dev->status->desc.bEndpointAddress
268                                 & USB_ENDPOINT_NUMBER_MASK);
269         maxp = usb_maxpacket (dev->udev, pipe, 0);
270
271         /* avoid 1 msec chatter:  min 8 msec poll rate */
272         period = max ((int) dev->status->desc.bInterval,
273                 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
274
275         buf = kmalloc (maxp, SLAB_KERNEL);
276         if (buf) {
277                 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
278                 if (!dev->interrupt) {
279                         kfree (buf);
280                         return -ENOMEM;
281                 } else {
282                         usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
283                                 buf, maxp, intr_complete, dev, period);
284                         dev_dbg(&intf->dev,
285                                 "status ep%din, %d bytes period %d\n",
286                                 usb_pipeendpoint(pipe), maxp, period);
287                 }
288         }
289         return  0;
290 }
291
292 /* Passes this packet up the stack, updating its accounting.
293  * Some link protocols batch packets, so their rx_fixup paths
294  * can return clones as well as just modify the original skb.
295  */
296 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
297 {
298         int     status;
299
300         skb->dev = dev->net;
301         skb->protocol = eth_type_trans (skb, dev->net);
302         dev->stats.rx_packets++;
303         dev->stats.rx_bytes += skb->len;
304
305         if (netif_msg_rx_status (dev))
306                 devdbg (dev, "< rx, len %zu, type 0x%x",
307                         skb->len + sizeof (struct ethhdr), skb->protocol);
308         memset (skb->cb, 0, sizeof (struct skb_data));
309         status = netif_rx (skb);
310         if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
311                 devdbg (dev, "netif_rx status %d", status);
312 }
313 EXPORT_SYMBOL_GPL(usbnet_skb_return);
314
315 \f
316 /*-------------------------------------------------------------------------
317  *
318  * Communications Device Class declarations.
319  * Used by CDC Ethernet, and some CDC variants
320  *
321  *-------------------------------------------------------------------------*/
322
323 #ifdef  CONFIG_USB_CDCETHER
324 #define NEED_GENERIC_CDC
325 #endif
326
327 #ifdef  CONFIG_USB_ZAURUS
328 /* Ethernet variant uses funky framing, broken ethernet addressing */
329 #define NEED_GENERIC_CDC
330 #endif
331
332 #ifdef  CONFIG_USB_RNDIS
333 /* ACM variant uses even funkier framing, complex control RPC scheme */
334 #define NEED_GENERIC_CDC
335 #endif
336
337
338 #ifdef  NEED_GENERIC_CDC
339
340 #include <linux/usb_cdc.h>
341
342 struct cdc_state {
343         struct usb_cdc_header_desc      *header;
344         struct usb_cdc_union_desc       *u;
345         struct usb_cdc_ether_desc       *ether;
346         struct usb_interface            *control;
347         struct usb_interface            *data;
348 };
349
350 static struct usb_driver usbnet_driver;
351
352 /*
353  * probes control interface, claims data interface, collects the bulk
354  * endpoints, activates data interface (if needed), maybe sets MTU.
355  * all pure cdc, except for certain firmware workarounds.
356  */
357 static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
358 {
359         u8                              *buf = intf->cur_altsetting->extra;
360         int                             len = intf->cur_altsetting->extralen;
361         struct usb_interface_descriptor *d;
362         struct cdc_state                *info = (void *) &dev->data;
363         int                             status;
364         int                             rndis;
365
366         if (sizeof dev->data < sizeof *info)
367                 return -EDOM;
368
369         /* expect strict spec conformance for the descriptors, but
370          * cope with firmware which stores them in the wrong place
371          */
372         if (len == 0 && dev->udev->actconfig->extralen) {
373                 /* Motorola SB4100 (and others: Brad Hards says it's
374                  * from a Broadcom design) put CDC descriptors here
375                  */
376                 buf = dev->udev->actconfig->extra;
377                 len = dev->udev->actconfig->extralen;
378                 if (len)
379                         dev_dbg (&intf->dev,
380                                 "CDC descriptors on config\n");
381         }
382
383         /* this assumes that if there's a non-RNDIS vendor variant
384          * of cdc-acm, it'll fail RNDIS requests cleanly.
385          */
386         rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
387
388         memset (info, 0, sizeof *info);
389         info->control = intf;
390         while (len > 3) {
391                 if (buf [1] != USB_DT_CS_INTERFACE)
392                         goto next_desc;
393
394                 /* use bDescriptorSubType to identify the CDC descriptors.
395                  * We expect devices with CDC header and union descriptors.
396                  * For CDC Ethernet we need the ethernet descriptor.
397                  * For RNDIS, ignore two (pointless) CDC modem descriptors
398                  * in favor of a complicated OID-based RPC scheme doing what
399                  * CDC Ethernet achieves with a simple descriptor.
400                  */
401                 switch (buf [2]) {
402                 case USB_CDC_HEADER_TYPE:
403                         if (info->header) {
404                                 dev_dbg (&intf->dev, "extra CDC header\n");
405                                 goto bad_desc;
406                         }
407                         info->header = (void *) buf;
408                         if (info->header->bLength != sizeof *info->header) {
409                                 dev_dbg (&intf->dev, "CDC header len %u\n",
410                                         info->header->bLength);
411                                 goto bad_desc;
412                         }
413                         break;
414                 case USB_CDC_UNION_TYPE:
415                         if (info->u) {
416                                 dev_dbg (&intf->dev, "extra CDC union\n");
417                                 goto bad_desc;
418                         }
419                         info->u = (void *) buf;
420                         if (info->u->bLength != sizeof *info->u) {
421                                 dev_dbg (&intf->dev, "CDC union len %u\n",
422                                         info->u->bLength);
423                                 goto bad_desc;
424                         }
425
426                         /* we need a master/control interface (what we're
427                          * probed with) and a slave/data interface; union
428                          * descriptors sort this all out.
429                          */
430                         info->control = usb_ifnum_to_if(dev->udev,
431                                                 info->u->bMasterInterface0);
432                         info->data = usb_ifnum_to_if(dev->udev,
433                                                 info->u->bSlaveInterface0);
434                         if (!info->control || !info->data) {
435                                 dev_dbg (&intf->dev,
436                                         "master #%u/%p slave #%u/%p\n",
437                                         info->u->bMasterInterface0,
438                                         info->control,
439                                         info->u->bSlaveInterface0,
440                                         info->data);
441                                 goto bad_desc;
442                         }
443                         if (info->control != intf) {
444                                 dev_dbg (&intf->dev, "bogus CDC Union\n");
445                                 /* Ambit USB Cable Modem (and maybe others)
446                                  * interchanges master and slave interface.
447                                  */
448                                 if (info->data == intf) {
449                                         info->data = info->control;
450                                         info->control = intf;
451                                 } else
452                                         goto bad_desc;
453                         }
454
455                         /* a data interface altsetting does the real i/o */
456                         d = &info->data->cur_altsetting->desc;
457                         if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
458                                 dev_dbg (&intf->dev, "slave class %u\n",
459                                         d->bInterfaceClass);
460                                 goto bad_desc;
461                         }
462                         break;
463                 case USB_CDC_ETHERNET_TYPE:
464                         if (info->ether) {
465                                 dev_dbg (&intf->dev, "extra CDC ether\n");
466                                 goto bad_desc;
467                         }
468                         info->ether = (void *) buf;
469                         if (info->ether->bLength != sizeof *info->ether) {
470                                 dev_dbg (&intf->dev, "CDC ether len %u\n",
471                                         info->ether->bLength);
472                                 goto bad_desc;
473                         }
474                         dev->hard_mtu = le16_to_cpu(
475                                                 info->ether->wMaxSegmentSize);
476                         /* because of Zaurus, we may be ignoring the host
477                          * side link address we were given.
478                          */
479                         break;
480                 }
481 next_desc:
482                 len -= buf [0]; /* bLength */
483                 buf += buf [0];
484         }
485
486         if (!info->header || !info->u || (!rndis && !info->ether)) {
487                 dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
488                         info->header ? "" : "header ",
489                         info->u ? "" : "union ",
490                         info->ether ? "" : "ether ");
491                 goto bad_desc;
492         }
493
494         /* claim data interface and set it up ... with side effects.
495          * network traffic can't flow until an altsetting is enabled.
496          */
497         status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
498         if (status < 0)
499                 return status;
500         status = usbnet_get_endpoints (dev, info->data);
501         if (status < 0) {
502                 /* ensure immediate exit from usbnet_disconnect */
503                 usb_set_intfdata(info->data, NULL);
504                 usb_driver_release_interface (&usbnet_driver, info->data);
505                 return status;
506         }
507
508         /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
509         dev->status = NULL;
510         if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
511                 struct usb_endpoint_descriptor  *desc;
512
513                 dev->status = &info->control->cur_altsetting->endpoint [0];
514                 desc = &dev->status->desc;
515                 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
516                                 || !(desc->bEndpointAddress & USB_DIR_IN)
517                                 || (le16_to_cpu(desc->wMaxPacketSize)
518                                         < sizeof (struct usb_cdc_notification))
519                                 || !desc->bInterval) {
520                         dev_dbg (&intf->dev, "bad notification endpoint\n");
521                         dev->status = NULL;
522                 }
523         }
524         if (rndis && !dev->status) {
525                 dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
526                 usb_set_intfdata(info->data, NULL);
527                 usb_driver_release_interface (&usbnet_driver, info->data);
528                 return -ENODEV;
529         }
530         return 0;
531
532 bad_desc:
533         dev_info (&dev->udev->dev, "bad CDC descriptors\n");
534         return -ENODEV;
535 }
536
537 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
538 {
539         struct cdc_state                *info = (void *) &dev->data;
540
541         /* disconnect master --> disconnect slave */
542         if (intf == info->control && info->data) {
543                 /* ensure immediate exit from usbnet_disconnect */
544                 usb_set_intfdata(info->data, NULL);
545                 usb_driver_release_interface (&usbnet_driver, info->data);
546                 info->data = NULL;
547         }
548
549         /* and vice versa (just in case) */
550         else if (intf == info->data && info->control) {
551                 /* ensure immediate exit from usbnet_disconnect */
552                 usb_set_intfdata(info->control, NULL);
553                 usb_driver_release_interface (&usbnet_driver, info->control);
554                 info->control = NULL;
555         }
556 }
557
558 #endif  /* NEED_GENERIC_CDC */
559
560 \f
561 #ifdef  CONFIG_USB_CDCETHER
562 #define HAVE_HARDWARE
563
564 /*-------------------------------------------------------------------------
565  *
566  * Communications Device Class, Ethernet Control model
567  *
568  * Takes two interfaces.  The DATA interface is inactive till an altsetting
569  * is selected.  Configuration data includes class descriptors.
570  *
571  * This should interop with whatever the 2.4 "CDCEther.c" driver
572  * (by Brad Hards) talked with.
573  *
574  *-------------------------------------------------------------------------*/
575
576 #include <linux/ctype.h>
577
578
579 static void dumpspeed (struct usbnet *dev, __le32 *speeds)
580 {
581         if (netif_msg_timer (dev))
582                 devinfo (dev, "link speeds: %u kbps up, %u kbps down",
583                         __le32_to_cpu(speeds[0]) / 1000,
584                 __le32_to_cpu(speeds[1]) / 1000);
585 }
586
587 static void cdc_status (struct usbnet *dev, struct urb *urb)
588 {
589         struct usb_cdc_notification     *event;
590
591         if (urb->actual_length < sizeof *event)
592                 return;
593         
594         /* SPEED_CHANGE can get split into two 8-byte packets */
595         if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
596                 dumpspeed (dev, (__le32 *) urb->transfer_buffer);
597                 return;
598         }
599
600         event = urb->transfer_buffer;
601         switch (event->bNotificationType) {
602         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
603                 if (netif_msg_timer (dev))
604                         devdbg (dev, "CDC: carrier %s",
605                                         event->wValue ? "on" : "off");
606                 if (event->wValue)
607                         netif_carrier_on(dev->net);
608                 else
609                         netif_carrier_off(dev->net);
610                 break;
611         case USB_CDC_NOTIFY_SPEED_CHANGE:       /* tx/rx rates */
612                 if (netif_msg_timer (dev))
613                         devdbg (dev, "CDC: speed change (len %d)",
614                                         urb->actual_length);
615                 if (urb->actual_length != (sizeof *event + 8))
616                         set_bit (EVENT_STS_SPLIT, &dev->flags);
617                 else
618                         dumpspeed (dev, (__le32 *) &event[1]);
619                 break;
620         // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:      /* RNDIS; or unsolicited */
621         default:
622                 deverr (dev, "CDC: unexpected notification %02x!",
623                                  event->bNotificationType);
624                 break;
625         }
626 }
627
628 static u8 nibble (unsigned char c)
629 {
630         if (likely (isdigit (c)))
631                 return c - '0';
632         c = toupper (c);
633         if (likely (isxdigit (c)))
634                 return 10 + c - 'A';
635         return 0;
636 }
637
638 static inline int
639 get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
640 {
641         int             tmp, i;
642         unsigned char   buf [13];
643
644         tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
645         if (tmp != 12) {
646                 dev_dbg (&dev->udev->dev,
647                         "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
648                 if (tmp >= 0)
649                         tmp = -EINVAL;
650                 return tmp;
651         }
652         for (i = tmp = 0; i < 6; i++, tmp += 2)
653                 dev->net->dev_addr [i] =
654                          (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
655         return 0;
656 }
657
658 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
659 {
660         int                             status;
661         struct cdc_state                *info = (void *) &dev->data;
662
663         status = generic_cdc_bind (dev, intf);
664         if (status < 0)
665                 return status;
666
667         status = get_ethernet_addr (dev, info->ether);
668         if (status < 0) {
669                 usb_set_intfdata(info->data, NULL);
670                 usb_driver_release_interface (&usbnet_driver, info->data);
671                 return status;
672         }
673
674         /* FIXME cdc-ether has some multicast code too, though it complains
675          * in routine cases.  info->ether describes the multicast support.
676          */
677         return 0;
678 }
679
680 static const struct driver_info cdc_info = {
681         .description =  "CDC Ethernet Device",
682         .flags =        FLAG_ETHER,
683         // .check_connect = cdc_check_connect,
684         .bind =         cdc_bind,
685         .unbind =       cdc_unbind,
686         .status =       cdc_status,
687 };
688
689 #endif  /* CONFIG_USB_CDCETHER */
690
691
692 \f
693 #ifdef CONFIG_USB_GENESYS
694 #define HAVE_HARDWARE
695
696 /*-------------------------------------------------------------------------
697  *
698  * GeneSys GL620USB-A (www.genesyslogic.com.tw)
699  *
700  * ... should partially interop with the Win32 driver for this hardware
701  * The GeneSys docs imply there's some NDIS issue motivating this framing.
702  *
703  * Some info from GeneSys:
704  *  - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
705  *    (Some cables, like the BAFO-100c, use the half duplex version.)
706  *  - For the full duplex model, the low bit of the version code says
707  *    which side is which ("left/right").
708  *  - For the half duplex type, a control/interrupt handshake settles
709  *    the transfer direction.  (That's disabled here, partially coded.)
710  *    A control URB would block until other side writes an interrupt.
711  *
712  * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
713  * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
714  *
715  *-------------------------------------------------------------------------*/
716
717 // control msg write command
718 #define GENELINK_CONNECT_WRITE                  0xF0
719 // interrupt pipe index
720 #define GENELINK_INTERRUPT_PIPE                 0x03
721 // interrupt read buffer size
722 #define INTERRUPT_BUFSIZE                       0x08
723 // interrupt pipe interval value
724 #define GENELINK_INTERRUPT_INTERVAL             0x10
725 // max transmit packet number per transmit
726 #define GL_MAX_TRANSMIT_PACKETS                 32
727 // max packet length
728 #define GL_MAX_PACKET_LEN                       1514
729 // max receive buffer size 
730 #define GL_RCV_BUF_SIZE         \
731         (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
732
733 struct gl_packet {
734         u32             packet_length;
735         char            packet_data [1];
736 };
737
738 struct gl_header {
739         u32                     packet_count;
740         struct gl_packet        packets;
741 };
742
743 #ifdef  GENELINK_ACK
744
745 // FIXME:  this code is incomplete, not debugged; it doesn't
746 // handle interrupts correctly.  interrupts should be generic
747 // code like all other device I/O, anyway.
748
749 struct gl_priv { 
750         struct urb      *irq_urb;
751         char            irq_buf [INTERRUPT_BUFSIZE];
752 };
753
754 static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
755 {
756         int retval;
757
758         retval = usb_control_msg (dev->udev,
759                       usb_sndctrlpipe (dev->udev, 0),
760                       request,
761                       USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
762                       value, 
763                       0,                        // index
764                       0,                        // data buffer
765                       0,                        // size
766                       CONTROL_TIMEOUT_MS);
767         return retval;
768 }
769
770 static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
771 {
772         int status = urb->status;
773         
774         switch (status) {
775         case 0:
776                 /* success */
777                 break;
778         case -ECONNRESET:
779         case -ENOENT:
780         case -ESHUTDOWN:
781                 /* this urb is terminated, clean up */
782                 dbg("%s - urb shutting down with status: %d",
783                                 __FUNCTION__, status);
784                 return;
785         default:
786                 dbg("%s - nonzero urb status received: %d",
787                                 __FUNCTION__, urb->status);
788         }
789
790         status = usb_submit_urb (urb, GFP_ATOMIC);
791         if (status)
792                 err ("%s - usb_submit_urb failed with result %d",
793                      __FUNCTION__, status);
794 }
795
796 static int gl_interrupt_read (struct usbnet *dev)
797 {
798         struct gl_priv  *priv = dev->priv_data;
799         int             retval;
800
801         // issue usb interrupt read
802         if (priv && priv->irq_urb) {
803                 // submit urb
804                 if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
805                         dbg ("gl_interrupt_read: submit fail - %X...", retval);
806                 else
807                         dbg ("gl_interrupt_read: submit success...");
808         }
809
810         return 0;
811 }
812
813 // check whether another side is connected
814 static int genelink_check_connect (struct usbnet *dev)
815 {
816         int                     retval;
817
818         dbg ("genelink_check_connect...");
819
820         // detect whether another side is connected
821         if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
822                 dbg ("%s: genelink_check_connect write fail - %X",
823                         dev->net->name, retval);
824                 return retval;
825         }
826
827         // usb interrupt read to ack another side 
828         if ((retval = gl_interrupt_read (dev)) != 0) {
829                 dbg ("%s: genelink_check_connect read fail - %X",
830                         dev->net->name, retval);
831                 return retval;
832         }
833
834         dbg ("%s: genelink_check_connect read success", dev->net->name);
835         return 0;
836 }
837
838 // allocate and initialize the private data for genelink
839 static int genelink_init (struct usbnet *dev)
840 {
841         struct gl_priv *priv;
842
843         // allocate the private data structure
844         if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
845                 dbg ("%s: cannot allocate private data per device",
846                         dev->net->name);
847                 return -ENOMEM;
848         }
849
850         // allocate irq urb
851         if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
852                 dbg ("%s: cannot allocate private irq urb per device",
853                         dev->net->name);
854                 kfree (priv);
855                 return -ENOMEM;
856         }
857
858         // fill irq urb
859         usb_fill_int_urb (priv->irq_urb, dev->udev,
860                 usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
861                 priv->irq_buf, INTERRUPT_BUFSIZE,
862                 gl_interrupt_complete, 0,
863                 GENELINK_INTERRUPT_INTERVAL);
864
865         // set private data pointer
866         dev->priv_data = priv;
867
868         return 0;
869 }
870
871 // release the private data
872 static int genelink_free (struct usbnet *dev)
873 {
874         struct gl_priv  *priv = dev->priv_data;
875
876         if (!priv) 
877                 return 0;
878
879 // FIXME:  can't cancel here; it's synchronous, and
880 // should have happened earlier in any case (interrupt
881 // handling needs to be generic)
882
883         // cancel irq urb first
884         usb_kill_urb (priv->irq_urb);
885
886         // free irq urb
887         usb_free_urb (priv->irq_urb);
888
889         // free the private data structure
890         kfree (priv);
891
892         return 0;
893 }
894
895 #endif
896
897 static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
898 {
899         struct gl_header        *header;
900         struct gl_packet        *packet;
901         struct sk_buff          *gl_skb;
902         u32                     size;
903
904         header = (struct gl_header *) skb->data;
905
906         // get the packet count of the received skb
907         le32_to_cpus (&header->packet_count);
908         if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
909                         || (header->packet_count < 0)) {
910                 dbg ("genelink: invalid received packet count %d",
911                         header->packet_count);
912                 return 0;
913         }
914
915         // set the current packet pointer to the first packet
916         packet = &header->packets;
917
918         // decrement the length for the packet count size 4 bytes
919         skb_pull (skb, 4);
920
921         while (header->packet_count > 1) {
922                 // get the packet length
923                 size = packet->packet_length;
924
925                 // this may be a broken packet
926                 if (size > GL_MAX_PACKET_LEN) {
927                         dbg ("genelink: invalid rx length %d", size);
928                         return 0;
929                 }
930
931                 // allocate the skb for the individual packet
932                 gl_skb = alloc_skb (size, GFP_ATOMIC);
933                 if (gl_skb) {
934
935                         // copy the packet data to the new skb
936                         memcpy(skb_put(gl_skb, size), packet->packet_data, size);
937                         usbnet_skb_return (dev, gl_skb);
938                 }
939
940                 // advance to the next packet
941                 packet = (struct gl_packet *)
942                         &packet->packet_data [size];
943                 header->packet_count--;
944
945                 // shift the data pointer to the next gl_packet
946                 skb_pull (skb, size + 4);
947         }
948
949         // skip the packet length field 4 bytes
950         skb_pull (skb, 4);
951
952         if (skb->len > GL_MAX_PACKET_LEN) {
953                 dbg ("genelink: invalid rx length %d", skb->len);
954                 return 0;
955         }
956         return 1;
957 }
958
959 static struct sk_buff *
960 genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
961 {
962         int     padlen;
963         int     length = skb->len;
964         int     headroom = skb_headroom (skb);
965         int     tailroom = skb_tailroom (skb);
966         u32     *packet_count;
967         u32     *packet_len;
968
969         // FIXME:  magic numbers, bleech
970         padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
971
972         if ((!skb_cloned (skb))
973                         && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
974                 if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
975                         skb->data = memmove (skb->head + (4 + 4*1),
976                                              skb->data, skb->len);
977                         skb->tail = skb->data + skb->len;
978                 }
979         } else {
980                 struct sk_buff  *skb2;
981                 skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
982                 dev_kfree_skb_any (skb);
983                 skb = skb2;
984                 if (!skb)
985                         return NULL;
986         }
987
988         // attach the packet count to the header
989         packet_count = (u32 *) skb_push (skb, (4 + 4*1));
990         packet_len = packet_count + 1;
991
992         // FIXME little endian?
993         *packet_count = 1;
994         *packet_len = length;
995
996         // add padding byte
997         if ((skb->len % dev->maxpacket) == 0)
998                 skb_put (skb, 1);
999
1000         return skb;
1001 }
1002
1003 static int genelink_bind (struct usbnet *dev, struct usb_interface *intf)
1004 {
1005         dev->hard_mtu = GL_RCV_BUF_SIZE;
1006         dev->net->hard_header_len += 4;
1007         return 0;
1008 }
1009
1010 static const struct driver_info genelink_info = {
1011         .description =  "Genesys GeneLink",
1012         .flags =        FLAG_FRAMING_GL | FLAG_NO_SETINT,
1013         .bind =         genelink_bind,
1014         .rx_fixup =     genelink_rx_fixup,
1015         .tx_fixup =     genelink_tx_fixup,
1016
1017         .in = 1, .out = 2,
1018
1019 #ifdef  GENELINK_ACK
1020         .check_connect =genelink_check_connect,
1021 #endif
1022 };
1023
1024 #endif /* CONFIG_USB_GENESYS */
1025
1026
1027 \f
1028 #ifdef CONFIG_USB_PL2301
1029 #define HAVE_HARDWARE
1030
1031 /*-------------------------------------------------------------------------
1032  *
1033  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
1034  *
1035  * The protocol and handshaking used here should be bug-compatible
1036  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
1037  *
1038  *-------------------------------------------------------------------------*/
1039
1040 /*
1041  * Bits 0-4 can be used for software handshaking; they're set from
1042  * one end, cleared from the other, "read" with the interrupt byte.
1043  */
1044 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
1045 /* reserved bit -- rx ready (6) ? */
1046 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
1047 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
1048 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
1049 #define PL_TX_C         (1<<2)          /* transmission complete */
1050 #define PL_TX_REQ       (1<<1)          /* transmission received */
1051 #define PL_PEER_E       (1<<0)          /* peer exists */
1052
1053 static inline int
1054 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
1055 {
1056         return usb_control_msg (dev->udev,
1057                 usb_rcvctrlpipe (dev->udev, 0),
1058                 req,
1059                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1060                 val, index,
1061                 NULL, 0,
1062                 CONTROL_TIMEOUT_MS);
1063 }
1064
1065 static inline int
1066 pl_clear_QuickLink_features (struct usbnet *dev, int val)
1067 {
1068         return pl_vendor_req (dev, 1, (u8) val, 0);
1069 }
1070
1071 static inline int
1072 pl_set_QuickLink_features (struct usbnet *dev, int val)
1073 {
1074         return pl_vendor_req (dev, 3, (u8) val, 0);
1075 }
1076
1077 /*-------------------------------------------------------------------------*/
1078
1079 static int pl_reset (struct usbnet *dev)
1080 {
1081         /* some units seem to need this reset, others reject it utterly.
1082          * FIXME be more like "naplink" or windows drivers.
1083          */
1084         (void) pl_set_QuickLink_features (dev,
1085                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
1086         return 0;
1087 }
1088
1089 static const struct driver_info prolific_info = {
1090         .description =  "Prolific PL-2301/PL-2302",
1091         .flags =        FLAG_NO_SETINT,
1092                 /* some PL-2302 versions seem to fail usb_set_interface() */
1093         .reset =        pl_reset,
1094 };
1095
1096 #endif /* CONFIG_USB_PL2301 */
1097
1098 \f
1099 #ifdef CONFIG_USB_ZAURUS
1100 #define HAVE_HARDWARE
1101
1102 #include <linux/crc32.h>
1103
1104 /*-------------------------------------------------------------------------
1105  *
1106  * Zaurus is also a SA-1110 based PDA, but one using a different driver
1107  * (and framing) for its USB slave/gadget controller than the case above.
1108  *
1109  * For the current version of that driver, the main way that framing is
1110  * nonstandard (also from perspective of the CDC ethernet model!) is a
1111  * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
1112  * haven't been fully worked around.  Also, all Zaurii use the same
1113  * default Ethernet address.
1114  *
1115  * PXA based models use the same framing, and also can't implement
1116  * set_interface properly.
1117  *
1118  * All known Zaurii lie about their standards conformance.  Most lie by
1119  * saying they support CDC Ethernet.  Some lie and say they support CDC
1120  * MDLM (as if for access to cell phone modems).  Someone, please beat 
1121  * on Sharp (and other such vendors) for a while with a cluestick.
1122  *
1123  *-------------------------------------------------------------------------*/
1124
1125 static struct sk_buff *
1126 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
1127 {
1128         int                     padlen;
1129         struct sk_buff          *skb2;
1130
1131         padlen = 2;
1132         if (!skb_cloned (skb)) {
1133                 int     tailroom = skb_tailroom (skb);
1134                 if ((padlen + 4) <= tailroom)
1135                         goto done;
1136         }
1137         skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
1138         dev_kfree_skb_any (skb);
1139         skb = skb2;
1140         if (skb) {
1141                 u32             fcs;
1142 done:
1143                 fcs = crc32_le (~0, skb->data, skb->len);
1144                 fcs = ~fcs;
1145
1146                 *skb_put (skb, 1) = fcs       & 0xff;
1147                 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
1148                 *skb_put (skb, 1) = (fcs>>16) & 0xff;
1149                 *skb_put (skb, 1) = (fcs>>24) & 0xff;
1150         }
1151         return skb;
1152 }
1153
1154 static int zaurus_bind (struct usbnet *dev, struct usb_interface *intf)
1155 {
1156         /* Belcarra's funky framing has other options; mostly
1157          * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
1158          */
1159         dev->net->hard_header_len += 6;
1160         return generic_cdc_bind(dev, intf);
1161 }
1162
1163 static const struct driver_info zaurus_sl5x00_info = {
1164         .description =  "Sharp Zaurus SL-5x00",
1165         .flags =        FLAG_FRAMING_Z,
1166         .check_connect = always_connected,
1167         .bind =         zaurus_bind,
1168         .unbind =       cdc_unbind,
1169         .tx_fixup =     zaurus_tx_fixup,
1170 };
1171 #define ZAURUS_STRONGARM_INFO   ((unsigned long)&zaurus_sl5x00_info)
1172
1173 static const struct driver_info zaurus_pxa_info = {
1174         .description =  "Sharp Zaurus, PXA-2xx based",
1175         .flags =        FLAG_FRAMING_Z,
1176         .check_connect = always_connected,
1177         .bind =         zaurus_bind,
1178         .unbind =       cdc_unbind,
1179         .tx_fixup =     zaurus_tx_fixup,
1180 };
1181 #define ZAURUS_PXA_INFO         ((unsigned long)&zaurus_pxa_info)
1182
1183 static const struct driver_info olympus_mxl_info = {
1184         .description =  "Olympus R1000",
1185         .flags =        FLAG_FRAMING_Z,
1186         .check_connect = always_connected,
1187         .bind =         zaurus_bind,
1188         .unbind =       cdc_unbind,
1189         .tx_fixup =     zaurus_tx_fixup,
1190 };
1191 #define OLYMPUS_MXL_INFO        ((unsigned long)&olympus_mxl_info)
1192
1193
1194 /* Some more recent products using Lineo/Belcarra code will wrongly claim
1195  * CDC MDLM conformance.  They aren't conformant:  data endpoints live
1196  * in the control interface, there's no data interface, and it's not used
1197  * to talk to a cell phone radio.  But at least we can detect these two
1198  * pseudo-classes, rather than growing this product list with entries for
1199  * each new nonconformant product (sigh).
1200  */
1201 static const u8 safe_guid[16] = {
1202         0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
1203         0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
1204 };
1205 static const u8 blan_guid[16] = {
1206         0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
1207         0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
1208 };
1209
1210 static int blan_mdlm_bind (struct usbnet *dev, struct usb_interface *intf)
1211 {
1212         u8                              *buf = intf->cur_altsetting->extra;
1213         int                             len = intf->cur_altsetting->extralen;
1214         struct usb_cdc_mdlm_desc        *desc = NULL;
1215         struct usb_cdc_mdlm_detail_desc *detail = NULL;
1216
1217         while (len > 3) {
1218                 if (buf [1] != USB_DT_CS_INTERFACE)
1219                         goto next_desc;
1220
1221                 /* use bDescriptorSubType, and just verify that we get a
1222                  * "BLAN" (or "SAFE") descriptor.
1223                  */
1224                 switch (buf [2]) {
1225                 case USB_CDC_MDLM_TYPE:
1226                         if (desc) {
1227                                 dev_dbg (&intf->dev, "extra MDLM\n");
1228                                 goto bad_desc;
1229                         }
1230                         desc = (void *) buf;
1231                         if (desc->bLength != sizeof *desc) {
1232                                 dev_dbg (&intf->dev, "MDLM len %u\n",
1233                                         desc->bLength);
1234                                 goto bad_desc;
1235                         }
1236                         /* expect bcdVersion 1.0, ignore */
1237                         if (memcmp(&desc->bGUID, blan_guid, 16)
1238                                     && memcmp(&desc->bGUID, safe_guid, 16) ) {
1239                                 /* hey, this one might _really_ be MDLM! */
1240                                 dev_dbg (&intf->dev, "MDLM guid\n");
1241                                 goto bad_desc;
1242                         }
1243                         break;
1244                 case USB_CDC_MDLM_DETAIL_TYPE:
1245                         if (detail) {
1246                                 dev_dbg (&intf->dev, "extra MDLM detail\n");
1247                                 goto bad_desc;
1248                         }
1249                         detail = (void *) buf;
1250                         switch (detail->bGuidDescriptorType) {
1251                         case 0:                 /* "SAFE" */
1252                                 if (detail->bLength != (sizeof *detail + 2))
1253                                         goto bad_detail;
1254                                 break;
1255                         case 1:                 /* "BLAN" */
1256                                 if (detail->bLength != (sizeof *detail + 3))
1257                                         goto bad_detail;
1258                                 break;
1259                         default:
1260                                 goto bad_detail;
1261                         }
1262
1263                         /* assuming we either noticed BLAN already, or will
1264                          * find it soon, there are some data bytes here:
1265                          *  - bmNetworkCapabilities (unused)
1266                          *  - bmDataCapabilities (bits, see below)
1267                          *  - bPad (ignored, for PADAFTER -- BLAN-only)
1268                          * bits are:
1269                          *  - 0x01 -- Zaurus framing (add CRC)
1270                          *  - 0x02 -- PADBEFORE (CRC includes some padding)
1271                          *  - 0x04 -- PADAFTER (some padding after CRC)
1272                          *  - 0x08 -- "fermat" packet mangling (for hw bugs)
1273                          * the PADBEFORE appears not to matter; we interop
1274                          * with devices that use it and those that don't.
1275                          */
1276                         if ((detail->bDetailData[1] & ~02) != 0x01) {
1277                                 /* bmDataCapabilites == 0 would be fine too,
1278                                  * but framing is minidriver-coupled for now.
1279                                  */
1280 bad_detail:
1281                                 dev_dbg (&intf->dev,
1282                                                 "bad MDLM detail, %d %d %d\n",
1283                                                 detail->bLength,
1284                                                 detail->bDetailData[0],
1285                                                 detail->bDetailData[2]);
1286                                 goto bad_desc;
1287                         }
1288                         break;
1289                 }
1290 next_desc:
1291                 len -= buf [0]; /* bLength */
1292                 buf += buf [0];
1293         }
1294
1295         if (!desc || !detail) {
1296                 dev_dbg (&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
1297                         desc ? "" : "func ",
1298                         detail ? "" : "detail ");
1299                 goto bad_desc;
1300         }
1301
1302         /* There's probably a CDC Ethernet descriptor there, but we can't
1303          * rely on the Ethernet address it provides since not all vendors
1304          * bother to make it unique.  Likewise there's no point in tracking
1305          * of the CDC event notifications.
1306          */
1307         return usbnet_get_endpoints (dev, intf);
1308
1309 bad_desc:
1310         dev_info (&dev->udev->dev, "unsupported MDLM descriptors\n");
1311         return -ENODEV;
1312 }
1313
1314 static const struct driver_info bogus_mdlm_info = {
1315         .description =  "pseudo-MDLM (BLAN) device",
1316         .flags =        FLAG_FRAMING_Z,
1317         .check_connect = always_connected,
1318         .tx_fixup =     zaurus_tx_fixup,
1319         .bind =         blan_mdlm_bind,
1320 };
1321
1322 #else
1323
1324 /* blacklist all those devices */
1325 #define ZAURUS_STRONGARM_INFO   0
1326 #define ZAURUS_PXA_INFO         0
1327 #define OLYMPUS_MXL_INFO        0
1328
1329 #endif
1330
1331 \f
1332 /*-------------------------------------------------------------------------
1333  *
1334  * Network Device Driver (peer link to "Host Device", from USB host)
1335  *
1336  *-------------------------------------------------------------------------*/
1337
1338 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1339 {
1340         struct usbnet   *dev = netdev_priv(net);
1341         int             ll_mtu = new_mtu + net->hard_header_len;
1342
1343         if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
1344                 return -EINVAL;
1345         // no second zero-length packet read wanted after mtu-sized packets
1346         if ((ll_mtu % dev->maxpacket) == 0)
1347                 return -EDOM;
1348         net->mtu = new_mtu;
1349         return 0;
1350 }
1351
1352 /*-------------------------------------------------------------------------*/
1353
1354 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1355 {
1356         struct usbnet   *dev = netdev_priv(net);
1357         return &dev->stats;
1358 }
1359
1360 /*-------------------------------------------------------------------------*/
1361
1362 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
1363  * completion callbacks.  2.5 should have fixed those bugs...
1364  */
1365
1366 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
1367 {
1368         unsigned long           flags;
1369
1370         spin_lock_irqsave(&list->lock, flags);
1371         __skb_unlink(skb, list);
1372         spin_unlock(&list->lock);
1373         spin_lock(&dev->done.lock);
1374         __skb_queue_tail(&dev->done, skb);
1375         if (dev->done.qlen == 1)
1376                 tasklet_schedule(&dev->bh);
1377         spin_unlock_irqrestore(&dev->done.lock, flags);
1378 }
1379
1380 /* some work can't be done in tasklets, so we use keventd
1381  *
1382  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
1383  * but tasklet_schedule() doesn't.  hope the failure is rare.
1384  */
1385 void usbnet_defer_kevent (struct usbnet *dev, int work)
1386 {
1387         set_bit (work, &dev->flags);
1388         if (!schedule_work (&dev->kevent))
1389                 deverr (dev, "kevent %d may have been dropped", work);
1390         else
1391                 devdbg (dev, "kevent %d scheduled", work);
1392 }
1393 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
1394
1395 /*-------------------------------------------------------------------------*/
1396
1397 static void rx_complete (struct urb *urb, struct pt_regs *regs);
1398
1399 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
1400 {
1401         struct sk_buff          *skb;
1402         struct skb_data         *entry;
1403         int                     retval = 0;
1404         unsigned long           lockflags;
1405         size_t                  size = dev->rx_urb_size;
1406
1407         if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
1408                 if (netif_msg_rx_err (dev))
1409                         devdbg (dev, "no rx skb");
1410                 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1411                 usb_free_urb (urb);
1412                 return;
1413         }
1414         skb_reserve (skb, NET_IP_ALIGN);
1415
1416         entry = (struct skb_data *) skb->cb;
1417         entry->urb = urb;
1418         entry->dev = dev;
1419         entry->state = rx_start;
1420         entry->length = 0;
1421
1422         usb_fill_bulk_urb (urb, dev->udev, dev->in,
1423                 skb->data, size, rx_complete, skb);
1424
1425         spin_lock_irqsave (&dev->rxq.lock, lockflags);
1426
1427         if (netif_running (dev->net)
1428                         && netif_device_present (dev->net)
1429                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1430                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
1431                 case -EPIPE:
1432                         usbnet_defer_kevent (dev, EVENT_RX_HALT);
1433                         break;
1434                 case -ENOMEM:
1435                         usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1436                         break;
1437                 case -ENODEV:
1438                         if (netif_msg_ifdown (dev))
1439                                 devdbg (dev, "device gone");
1440                         netif_device_detach (dev->net);
1441                         break;
1442                 default:
1443                         if (netif_msg_rx_err (dev))
1444                                 devdbg (dev, "rx submit, %d", retval);
1445                         tasklet_schedule (&dev->bh);
1446                         break;
1447                 case 0:
1448                         __skb_queue_tail (&dev->rxq, skb);
1449                 }
1450         } else {
1451                 if (netif_msg_ifdown (dev))
1452                         devdbg (dev, "rx: stopped");
1453                 retval = -ENOLINK;
1454         }
1455         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1456         if (retval) {
1457                 dev_kfree_skb_any (skb);
1458                 usb_free_urb (urb);
1459         }
1460 }
1461
1462
1463 /*-------------------------------------------------------------------------*/
1464
1465 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1466 {
1467         if (dev->driver_info->rx_fixup
1468                         && !dev->driver_info->rx_fixup (dev, skb))
1469                 goto error;
1470         // else network stack removes extra byte if we forced a short packet
1471
1472         if (skb->len)
1473                 usbnet_skb_return (dev, skb);
1474         else {
1475                 if (netif_msg_rx_err (dev))
1476                         devdbg (dev, "drop");
1477 error:
1478                 dev->stats.rx_errors++;
1479                 skb_queue_tail (&dev->done, skb);
1480         }
1481 }
1482
1483 /*-------------------------------------------------------------------------*/
1484
1485 static void rx_complete (struct urb *urb, struct pt_regs *regs)
1486 {
1487         struct sk_buff          *skb = (struct sk_buff *) urb->context;
1488         struct skb_data         *entry = (struct skb_data *) skb->cb;
1489         struct usbnet           *dev = entry->dev;
1490         int                     urb_status = urb->status;
1491
1492         skb_put (skb, urb->actual_length);
1493         entry->state = rx_done;
1494         entry->urb = NULL;
1495
1496         switch (urb_status) {
1497             // success
1498             case 0:
1499                 if (skb->len < dev->net->hard_header_len) {
1500                         entry->state = rx_cleanup;
1501                         dev->stats.rx_errors++;
1502                         dev->stats.rx_length_errors++;
1503                         if (netif_msg_rx_err (dev))
1504                                 devdbg (dev, "rx length %d", skb->len);
1505                 }
1506                 break;
1507
1508             // stalls need manual reset. this is rare ... except that
1509             // when going through USB 2.0 TTs, unplug appears this way.
1510             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1511             // storm, recovering as needed.
1512             case -EPIPE:
1513                 dev->stats.rx_errors++;
1514                 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1515                 // FALLTHROUGH
1516
1517             // software-driven interface shutdown
1518             case -ECONNRESET:           // async unlink
1519             case -ESHUTDOWN:            // hardware gone
1520                 if (netif_msg_ifdown (dev))
1521                         devdbg (dev, "rx shutdown, code %d", urb_status);
1522                 goto block;
1523
1524             // we get controller i/o faults during khubd disconnect() delays.
1525             // throttle down resubmits, to avoid log floods; just temporarily,
1526             // so we still recover when the fault isn't a khubd delay.
1527             case -EPROTO:               // ehci
1528             case -ETIMEDOUT:            // ohci
1529             case -EILSEQ:               // uhci
1530                 dev->stats.rx_errors++;
1531                 if (!timer_pending (&dev->delay)) {
1532                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
1533                         if (netif_msg_link (dev))
1534                                 devdbg (dev, "rx throttle %d", urb_status);
1535                 }
1536 block:
1537                 entry->state = rx_cleanup;
1538                 entry->urb = urb;
1539                 urb = NULL;
1540                 break;
1541
1542             // data overrun ... flush fifo?
1543             case -EOVERFLOW:
1544                 dev->stats.rx_over_errors++;
1545                 // FALLTHROUGH
1546             
1547             default:
1548                 entry->state = rx_cleanup;
1549                 dev->stats.rx_errors++;
1550                 if (netif_msg_rx_err (dev))
1551                         devdbg (dev, "rx status %d", urb_status);
1552                 break;
1553         }
1554
1555         defer_bh(dev, skb, &dev->rxq);
1556
1557         if (urb) {
1558                 if (netif_running (dev->net)
1559                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1560                         rx_submit (dev, urb, GFP_ATOMIC);
1561                         return;
1562                 }
1563                 usb_free_urb (urb);
1564         }
1565         if (netif_msg_rx_err (dev))
1566                 devdbg (dev, "no read resubmitted");
1567 }
1568
1569 static void intr_complete (struct urb *urb, struct pt_regs *regs)
1570 {
1571         struct usbnet   *dev = urb->context;
1572         int             status = urb->status;
1573
1574         switch (status) {
1575             /* success */
1576             case 0:
1577                 dev->driver_info->status(dev, urb);
1578                 break;
1579
1580             /* software-driven interface shutdown */
1581             case -ENOENT:               // urb killed
1582             case -ESHUTDOWN:            // hardware gone
1583                 if (netif_msg_ifdown (dev))
1584                         devdbg (dev, "intr shutdown, code %d", status);
1585                 return;
1586
1587             /* NOTE:  not throttling like RX/TX, since this endpoint
1588              * already polls infrequently
1589              */
1590             default:
1591                 devdbg (dev, "intr status %d", status);
1592                 break;
1593         }
1594
1595         if (!netif_running (dev->net))
1596                 return;
1597
1598         memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
1599         status = usb_submit_urb (urb, GFP_ATOMIC);
1600         if (status != 0 && netif_msg_timer (dev))
1601                 deverr(dev, "intr resubmit --> %d", status);
1602 }
1603
1604 /*-------------------------------------------------------------------------*/
1605
1606 // unlink pending rx/tx; completion handlers do all other cleanup
1607
1608 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1609 {
1610         unsigned long           flags;
1611         struct sk_buff          *skb, *skbnext;
1612         int                     count = 0;
1613
1614         spin_lock_irqsave (&q->lock, flags);
1615         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1616                 struct skb_data         *entry;
1617                 struct urb              *urb;
1618                 int                     retval;
1619
1620                 entry = (struct skb_data *) skb->cb;
1621                 urb = entry->urb;
1622                 skbnext = skb->next;
1623
1624                 // during some PM-driven resume scenarios,
1625                 // these (async) unlinks complete immediately
1626                 retval = usb_unlink_urb (urb);
1627                 if (retval != -EINPROGRESS && retval != 0)
1628                         devdbg (dev, "unlink urb err, %d", retval);
1629                 else
1630                         count++;
1631         }
1632         spin_unlock_irqrestore (&q->lock, flags);
1633         return count;
1634 }
1635
1636
1637 /*-------------------------------------------------------------------------*/
1638
1639 // precondition: never called in_interrupt
1640
1641 static int usbnet_stop (struct net_device *net)
1642 {
1643         struct usbnet           *dev = netdev_priv(net);
1644         int                     temp;
1645         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
1646         DECLARE_WAITQUEUE (wait, current);
1647
1648         netif_stop_queue (net);
1649
1650         if (netif_msg_ifdown (dev))
1651                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
1652                         dev->stats.rx_packets, dev->stats.tx_packets, 
1653                         dev->stats.rx_errors, dev->stats.tx_errors
1654                         );
1655
1656         // ensure there are no more active urbs
1657         add_wait_queue (&unlink_wakeup, &wait);
1658         dev->wait = &unlink_wakeup;
1659         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
1660
1661         // maybe wait for deletions to finish.
1662         while (!skb_queue_empty(&dev->rxq) &&
1663                !skb_queue_empty(&dev->txq) &&
1664                !skb_queue_empty(&dev->done)) {
1665                 msleep(UNLINK_TIMEOUT_MS);
1666                 if (netif_msg_ifdown (dev))
1667                         devdbg (dev, "waited for %d urb completions", temp);
1668         }
1669         dev->wait = NULL;
1670         remove_wait_queue (&unlink_wakeup, &wait); 
1671
1672         usb_kill_urb(dev->interrupt);
1673
1674         /* deferred work (task, timer, softirq) must also stop.
1675          * can't flush_scheduled_work() until we drop rtnl (later),
1676          * else workers could deadlock; so make workers a NOP.
1677          */
1678         dev->flags = 0;
1679         del_timer_sync (&dev->delay);
1680         tasklet_kill (&dev->bh);
1681
1682         return 0;
1683 }
1684
1685 /*-------------------------------------------------------------------------*/
1686
1687 // posts reads, and enables write queuing
1688
1689 // precondition: never called in_interrupt
1690
1691 static int usbnet_open (struct net_device *net)
1692 {
1693         struct usbnet           *dev = netdev_priv(net);
1694         int                     retval = 0;
1695         struct driver_info      *info = dev->driver_info;
1696
1697         // put into "known safe" state
1698         if (info->reset && (retval = info->reset (dev)) < 0) {
1699                 if (netif_msg_ifup (dev))
1700                         devinfo (dev,
1701                                 "open reset fail (%d) usbnet usb-%s-%s, %s",
1702                                 retval,
1703                                 dev->udev->bus->bus_name, dev->udev->devpath,
1704                         info->description);
1705                 goto done;
1706         }
1707
1708         // insist peer be connected
1709         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
1710                 if (netif_msg_ifup (dev))
1711                         devdbg (dev, "can't open; %d", retval);
1712                 goto done;
1713         }
1714
1715         /* start any status interrupt transfer */
1716         if (dev->interrupt) {
1717                 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
1718                 if (retval < 0) {
1719                         if (netif_msg_ifup (dev))
1720                                 deverr (dev, "intr submit %d", retval);
1721                         goto done;
1722                 }
1723         }
1724
1725         netif_start_queue (net);
1726         if (netif_msg_ifup (dev)) {
1727                 char    *framing;
1728
1729                 if (dev->driver_info->flags & FLAG_FRAMING_NC)
1730                         framing = "NetChip";
1731                 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
1732                         framing = "GeneSys";
1733                 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
1734                         framing = "Zaurus";
1735                 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
1736                         framing = "RNDIS";
1737                 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
1738                         framing = "ASIX";
1739                 else
1740                         framing = "simple";
1741
1742                 devinfo (dev, "open: enable queueing "
1743                                 "(rx %d, tx %d) mtu %d %s framing",
1744                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
1745                         framing);
1746         }
1747
1748         // delay posting reads until we're fully open
1749         tasklet_schedule (&dev->bh);
1750 done:
1751         return retval;
1752 }
1753
1754 /*-------------------------------------------------------------------------*/
1755
1756 /* ethtool methods; minidrivers may need to add some more, but
1757  * they'll probably want to use this base set.
1758  */
1759
1760 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1761 {
1762         struct usbnet *dev = netdev_priv(net);
1763
1764         /* REVISIT don't always return "usbnet" */
1765         strncpy (info->driver, driver_name, sizeof info->driver);
1766         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
1767         strncpy (info->fw_version, dev->driver_info->description,
1768                 sizeof info->fw_version);
1769         usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1770 }
1771 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1772
1773 static u32 usbnet_get_link (struct net_device *net)
1774 {
1775         struct usbnet *dev = netdev_priv(net);
1776
1777         /* If a check_connect is defined, return its result */
1778         if (dev->driver_info->check_connect)
1779                 return dev->driver_info->check_connect (dev) == 0;
1780
1781         /* Otherwise, say we're up (to avoid breaking scripts) */
1782         return 1;
1783 }
1784
1785 u32 usbnet_get_msglevel (struct net_device *net)
1786 {
1787         struct usbnet *dev = netdev_priv(net);
1788
1789         return dev->msg_enable;
1790 }
1791 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1792
1793 void usbnet_set_msglevel (struct net_device *net, u32 level)
1794 {
1795         struct usbnet *dev = netdev_priv(net);
1796
1797         dev->msg_enable = level;
1798 }
1799 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1800
1801 /* drivers may override default ethtool_ops in their bind() routine */
1802 static struct ethtool_ops usbnet_ethtool_ops = {
1803         .get_drvinfo            = usbnet_get_drvinfo,
1804         .get_link               = usbnet_get_link,
1805         .get_msglevel           = usbnet_get_msglevel,
1806         .set_msglevel           = usbnet_set_msglevel,
1807 };
1808
1809 /*-------------------------------------------------------------------------*/
1810
1811 /* work that cannot be done in interrupt context uses keventd.
1812  *
1813  * NOTE:  with 2.5 we could do more of this using completion callbacks,
1814  * especially now that control transfers can be queued.
1815  */
1816 static void
1817 kevent (void *data)
1818 {
1819         struct usbnet           *dev = data;
1820         int                     status;
1821
1822         /* usb_clear_halt() needs a thread context */
1823         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1824                 unlink_urbs (dev, &dev->txq);
1825                 status = usb_clear_halt (dev->udev, dev->out);
1826                 if (status < 0
1827                                 && status != -EPIPE
1828                                 && status != -ESHUTDOWN) {
1829                         if (netif_msg_tx_err (dev))
1830                                 deverr (dev, "can't clear tx halt, status %d",
1831                                         status);
1832                 } else {
1833                         clear_bit (EVENT_TX_HALT, &dev->flags);
1834                         if (status != -ESHUTDOWN)
1835                                 netif_wake_queue (dev->net);
1836                 }
1837         }
1838         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1839                 unlink_urbs (dev, &dev->rxq);
1840                 status = usb_clear_halt (dev->udev, dev->in);
1841                 if (status < 0
1842                                 && status != -EPIPE
1843                                 && status != -ESHUTDOWN) {
1844                         if (netif_msg_rx_err (dev))
1845                                 deverr (dev, "can't clear rx halt, status %d",
1846                                         status);
1847                 } else {
1848                         clear_bit (EVENT_RX_HALT, &dev->flags);
1849                         tasklet_schedule (&dev->bh);
1850                 }
1851         }
1852
1853         /* tasklet could resubmit itself forever if memory is tight */
1854         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1855                 struct urb      *urb = NULL;
1856
1857                 if (netif_running (dev->net))
1858                         urb = usb_alloc_urb (0, GFP_KERNEL);
1859                 else
1860                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
1861                 if (urb != NULL) {
1862                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
1863                         rx_submit (dev, urb, GFP_KERNEL);
1864                         tasklet_schedule (&dev->bh);
1865                 }
1866         }
1867
1868         if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1869                 struct driver_info      *info = dev->driver_info;
1870                 int                     retval = 0;
1871
1872                 clear_bit (EVENT_LINK_RESET, &dev->flags);
1873                 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1874                         devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
1875                                 retval,
1876                                 dev->udev->bus->bus_name, dev->udev->devpath,
1877                                 info->description);
1878                 }
1879         }
1880
1881         if (dev->flags)
1882                 devdbg (dev, "kevent done, flags = 0x%lx",
1883                         dev->flags);
1884 }
1885
1886 /*-------------------------------------------------------------------------*/
1887
1888 static void tx_complete (struct urb *urb, struct pt_regs *regs)
1889 {
1890         struct sk_buff          *skb = (struct sk_buff *) urb->context;
1891         struct skb_data         *entry = (struct skb_data *) skb->cb;
1892         struct usbnet           *dev = entry->dev;
1893
1894         if (urb->status == 0) {
1895                 dev->stats.tx_packets++;
1896                 dev->stats.tx_bytes += entry->length;
1897         } else {
1898                 dev->stats.tx_errors++;
1899
1900                 switch (urb->status) {
1901                 case -EPIPE:
1902                         usbnet_defer_kevent (dev, EVENT_TX_HALT);
1903                         break;
1904
1905                 /* software-driven interface shutdown */
1906                 case -ECONNRESET:               // async unlink
1907                 case -ESHUTDOWN:                // hardware gone
1908                         break;
1909
1910                 // like rx, tx gets controller i/o faults during khubd delays
1911                 // and so it uses the same throttling mechanism.
1912                 case -EPROTO:           // ehci
1913                 case -ETIMEDOUT:        // ohci
1914                 case -EILSEQ:           // uhci
1915                         if (!timer_pending (&dev->delay)) {
1916                                 mod_timer (&dev->delay,
1917                                         jiffies + THROTTLE_JIFFIES);
1918                                 if (netif_msg_link (dev))
1919                                         devdbg (dev, "tx throttle %d",
1920                                                         urb->status);
1921                         }
1922                         netif_stop_queue (dev->net);
1923                         break;
1924                 default:
1925                         if (netif_msg_tx_err (dev))
1926                                 devdbg (dev, "tx err %d", entry->urb->status);
1927                         break;
1928                 }
1929         }
1930
1931         urb->dev = NULL;
1932         entry->state = tx_done;
1933         defer_bh(dev, skb, &dev->txq);
1934 }
1935
1936 /*-------------------------------------------------------------------------*/
1937
1938 static void usbnet_tx_timeout (struct net_device *net)
1939 {
1940         struct usbnet           *dev = netdev_priv(net);
1941
1942         unlink_urbs (dev, &dev->txq);
1943         tasklet_schedule (&dev->bh);
1944
1945         // FIXME: device recovery -- reset?
1946 }
1947
1948 /*-------------------------------------------------------------------------*/
1949
1950 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
1951 {
1952         struct usbnet           *dev = netdev_priv(net);
1953         int                     length;
1954         int                     retval = NET_XMIT_SUCCESS;
1955         struct urb              *urb = NULL;
1956         struct skb_data         *entry;
1957         struct driver_info      *info = dev->driver_info;
1958         unsigned long           flags;
1959
1960         // some devices want funky USB-level framing, for
1961         // win32 driver (usually) and/or hardware quirks
1962         if (info->tx_fixup) {
1963                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1964                 if (!skb) {
1965                         if (netif_msg_tx_err (dev))
1966                                 devdbg (dev, "can't tx_fixup skb");
1967                         goto drop;
1968                 }
1969         }
1970         length = skb->len;
1971
1972         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1973                 if (netif_msg_tx_err (dev))
1974                         devdbg (dev, "no urb");
1975                 goto drop;
1976         }
1977
1978         entry = (struct skb_data *) skb->cb;
1979         entry->urb = urb;
1980         entry->dev = dev;
1981         entry->state = tx_start;
1982         entry->length = length;
1983
1984         usb_fill_bulk_urb (urb, dev->udev, dev->out,
1985                         skb->data, skb->len, tx_complete, skb);
1986
1987         /* don't assume the hardware handles USB_ZERO_PACKET
1988          * NOTE:  strictly conforming cdc-ether devices should expect
1989          * the ZLP here, but ignore the one-byte packet.
1990          *
1991          * FIXME zero that byte, if it doesn't require a new skb.
1992          */
1993         if ((length % dev->maxpacket) == 0)
1994                 urb->transfer_buffer_length++;
1995
1996         spin_lock_irqsave (&dev->txq.lock, flags);
1997
1998         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1999         case -EPIPE:
2000                 netif_stop_queue (net);
2001                 usbnet_defer_kevent (dev, EVENT_TX_HALT);
2002                 break;
2003         default:
2004                 if (netif_msg_tx_err (dev))
2005                         devdbg (dev, "tx: submit urb err %d", retval);
2006                 break;
2007         case 0:
2008                 net->trans_start = jiffies;
2009                 __skb_queue_tail (&dev->txq, skb);
2010                 if (dev->txq.qlen >= TX_QLEN (dev))
2011                         netif_stop_queue (net);
2012         }
2013         spin_unlock_irqrestore (&dev->txq.lock, flags);
2014
2015         if (retval) {
2016                 if (netif_msg_tx_err (dev))
2017                         devdbg (dev, "drop, code %d", retval);
2018 drop:
2019                 retval = NET_XMIT_SUCCESS;
2020                 dev->stats.tx_dropped++;
2021                 if (skb)
2022                         dev_kfree_skb_any (skb);
2023                 usb_free_urb (urb);
2024         } else if (netif_msg_tx_queued (dev)) {
2025                 devdbg (dev, "> tx, len %d, type 0x%x",
2026                         length, skb->protocol);
2027         }
2028         return retval;
2029 }
2030
2031
2032 /*-------------------------------------------------------------------------*/
2033
2034 // tasklet (work deferred from completions, in_irq) or timer
2035
2036 static void usbnet_bh (unsigned long param)
2037 {
2038         struct usbnet           *dev = (struct usbnet *) param;
2039         struct sk_buff          *skb;
2040         struct skb_data         *entry;
2041
2042         while ((skb = skb_dequeue (&dev->done))) {
2043                 entry = (struct skb_data *) skb->cb;
2044                 switch (entry->state) {
2045                     case rx_done:
2046                         entry->state = rx_cleanup;
2047                         rx_process (dev, skb);
2048                         continue;
2049                     case tx_done:
2050                     case rx_cleanup:
2051                         usb_free_urb (entry->urb);
2052                         dev_kfree_skb (skb);
2053                         continue;
2054                     default:
2055                         devdbg (dev, "bogus skb state %d", entry->state);
2056                 }
2057         }
2058
2059         // waiting for all pending urbs to complete?
2060         if (dev->wait) {
2061                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
2062                         wake_up (dev->wait);
2063                 }
2064
2065         // or are we maybe short a few urbs?
2066         } else if (netif_running (dev->net)
2067                         && netif_device_present (dev->net)
2068                         && !timer_pending (&dev->delay)
2069                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
2070                 int     temp = dev->rxq.qlen;
2071                 int     qlen = RX_QLEN (dev);
2072
2073                 if (temp < qlen) {
2074                         struct urb      *urb;
2075                         int             i;
2076
2077                         // don't refill the queue all at once
2078                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
2079                                 urb = usb_alloc_urb (0, GFP_ATOMIC);
2080                                 if (urb != NULL)
2081                                         rx_submit (dev, urb, GFP_ATOMIC);
2082                         }
2083                         if (temp != dev->rxq.qlen && netif_msg_link (dev))
2084                                 devdbg (dev, "rxqlen %d --> %d",
2085                                                 temp, dev->rxq.qlen);
2086                         if (dev->rxq.qlen < qlen)
2087                                 tasklet_schedule (&dev->bh);
2088                 }
2089                 if (dev->txq.qlen < TX_QLEN (dev))
2090                         netif_wake_queue (dev->net);
2091         }
2092 }
2093
2094
2095 \f
2096 /*-------------------------------------------------------------------------
2097  *
2098  * USB Device Driver support
2099  *
2100  *-------------------------------------------------------------------------*/
2101  
2102 // precondition: never called in_interrupt
2103
2104 void usbnet_disconnect (struct usb_interface *intf)
2105 {
2106         struct usbnet           *dev;
2107         struct usb_device       *xdev;
2108         struct net_device       *net;
2109
2110         dev = usb_get_intfdata(intf);
2111         usb_set_intfdata(intf, NULL);
2112         if (!dev)
2113                 return;
2114
2115         xdev = interface_to_usbdev (intf);
2116
2117         if (netif_msg_probe (dev))
2118                 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
2119                         intf->dev.driver->name,
2120                         xdev->bus->bus_name, xdev->devpath,
2121                         dev->driver_info->description);
2122         
2123         net = dev->net;
2124         unregister_netdev (net);
2125
2126         /* we don't hold rtnl here ... */
2127         flush_scheduled_work ();
2128
2129         if (dev->driver_info->unbind)
2130                 dev->driver_info->unbind (dev, intf);
2131
2132         free_netdev(net);
2133         usb_put_dev (xdev);
2134 }
2135 EXPORT_SYMBOL_GPL(usbnet_disconnect);
2136
2137
2138 /*-------------------------------------------------------------------------*/
2139
2140 // precondition: never called in_interrupt
2141
2142 int
2143 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
2144 {
2145         struct usbnet                   *dev;
2146         struct net_device               *net;
2147         struct usb_host_interface       *interface;
2148         struct driver_info              *info;
2149         struct usb_device               *xdev;
2150         int                             status;
2151
2152         info = (struct driver_info *) prod->driver_info;
2153         if (!info) {
2154                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
2155                 return -ENODEV;
2156         }
2157         xdev = interface_to_usbdev (udev);
2158         interface = udev->cur_altsetting;
2159
2160         usb_get_dev (xdev);
2161
2162         status = -ENOMEM;
2163
2164         // set up our own records
2165         net = alloc_etherdev(sizeof(*dev));
2166         if (!net) {
2167                 dbg ("can't kmalloc dev");
2168                 goto out;
2169         }
2170
2171         dev = netdev_priv(net);
2172         dev->udev = xdev;
2173         dev->driver_info = info;
2174         dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
2175                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
2176         skb_queue_head_init (&dev->rxq);
2177         skb_queue_head_init (&dev->txq);
2178         skb_queue_head_init (&dev->done);
2179         dev->bh.func = usbnet_bh;
2180         dev->bh.data = (unsigned long) dev;
2181         INIT_WORK (&dev->kevent, kevent, dev);
2182         dev->delay.function = usbnet_bh;
2183         dev->delay.data = (unsigned long) dev;
2184         init_timer (&dev->delay);
2185
2186         SET_MODULE_OWNER (net);
2187         dev->net = net;
2188         strcpy (net->name, "usb%d");
2189         memcpy (net->dev_addr, node_id, sizeof node_id);
2190
2191         /* rx and tx sides can use different message sizes;
2192          * bind() should set rx_urb_size in that case.
2193          */
2194         dev->hard_mtu = net->mtu + net->hard_header_len;
2195 #if 0
2196 // dma_supported() is deeply broken on almost all architectures
2197         // possible with some EHCI controllers
2198         if (dma_supported (&udev->dev, DMA_64BIT_MASK))
2199                 net->features |= NETIF_F_HIGHDMA;
2200 #endif
2201
2202         net->change_mtu = usbnet_change_mtu;
2203         net->get_stats = usbnet_get_stats;
2204         net->hard_start_xmit = usbnet_start_xmit;
2205         net->open = usbnet_open;
2206         net->stop = usbnet_stop;
2207         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
2208         net->tx_timeout = usbnet_tx_timeout;
2209         net->ethtool_ops = &usbnet_ethtool_ops;
2210
2211         // allow device-specific bind/init procedures
2212         // NOTE net->name still not usable ...
2213         if (info->bind) {
2214                 status = info->bind (dev, udev);
2215                 // heuristic:  "usb%d" for links we know are two-host,
2216                 // else "eth%d" when there's reasonable doubt.  userspace
2217                 // can rename the link if it knows better.
2218                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
2219                                 && (net->dev_addr [0] & 0x02) == 0)
2220                         strcpy (net->name, "eth%d");
2221
2222                 /* maybe the remote can't receive an Ethernet MTU */
2223                 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
2224                         net->mtu = dev->hard_mtu - net->hard_header_len;
2225         } else if (!info->in || !info->out)
2226                 status = usbnet_get_endpoints (dev, udev);
2227         else {
2228                 dev->in = usb_rcvbulkpipe (xdev, info->in);
2229                 dev->out = usb_sndbulkpipe (xdev, info->out);
2230                 if (!(info->flags & FLAG_NO_SETINT))
2231                         status = usb_set_interface (xdev,
2232                                 interface->desc.bInterfaceNumber,
2233                                 interface->desc.bAlternateSetting);
2234                 else
2235                         status = 0;
2236
2237         }
2238         if (status == 0 && dev->status)
2239                 status = init_status (dev, udev);
2240         if (status < 0)
2241                 goto out1;
2242
2243         if (!dev->rx_urb_size)
2244                 dev->rx_urb_size = dev->hard_mtu;
2245         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
2246         
2247         SET_NETDEV_DEV(net, &udev->dev);
2248         status = register_netdev (net);
2249         if (status)
2250                 goto out3;
2251         if (netif_msg_probe (dev))
2252                 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
2253                                 "%02x:%02x:%02x:%02x:%02x:%02x",
2254                         udev->dev.driver->name,
2255                         xdev->bus->bus_name, xdev->devpath,
2256                         dev->driver_info->description,
2257                         net->dev_addr [0], net->dev_addr [1],
2258                         net->dev_addr [2], net->dev_addr [3],
2259                         net->dev_addr [4], net->dev_addr [5]);
2260
2261         // ok, it's ready to go.
2262         usb_set_intfdata (udev, dev);
2263
2264         // start as if the link is up
2265         netif_device_attach (net);
2266
2267         return 0;
2268
2269 out3:
2270         if (info->unbind)
2271                 info->unbind (dev, udev);
2272 out1:
2273         free_netdev(net);
2274 out:
2275         usb_put_dev(xdev);
2276         return status;
2277 }
2278 EXPORT_SYMBOL_GPL(usbnet_probe);
2279
2280 /*-------------------------------------------------------------------------*/
2281
2282 /* FIXME these suspend/resume methods assume non-CDC style
2283  * devices, with only one interface.
2284  */
2285
2286 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
2287 {
2288         struct usbnet           *dev = usb_get_intfdata(intf);
2289         
2290         /* accelerate emptying of the rx and queues, to avoid
2291          * having everything error out.
2292          */
2293         netif_device_detach (dev->net);
2294         (void) unlink_urbs (dev, &dev->rxq);
2295         (void) unlink_urbs (dev, &dev->txq);
2296         intf->dev.power.power_state = PMSG_SUSPEND;
2297         return 0;
2298 }
2299 EXPORT_SYMBOL_GPL(usbnet_suspend);
2300
2301 int usbnet_resume (struct usb_interface *intf)
2302 {
2303         struct usbnet           *dev = usb_get_intfdata(intf);
2304
2305         intf->dev.power.power_state = PMSG_ON;
2306         netif_device_attach (dev->net);
2307         tasklet_schedule (&dev->bh);
2308         return 0;
2309 }
2310 EXPORT_SYMBOL_GPL(usbnet_resume);
2311
2312
2313 /*-------------------------------------------------------------------------*/
2314
2315 #ifndef HAVE_HARDWARE
2316 #error You need to configure some hardware for this driver
2317 #endif
2318
2319 /*
2320  * chip vendor names won't normally be on the cables, and
2321  * may not be on the device.
2322  */
2323
2324 static const struct usb_device_id       products [] = {
2325
2326 #ifdef  CONFIG_USB_GENESYS
2327 {
2328         USB_DEVICE (0x05e3, 0x0502),    // GL620USB-A
2329         .driver_info =  (unsigned long) &genelink_info,
2330 },
2331         /* NOT: USB_DEVICE (0x05e3, 0x0501),    // GL620USB
2332          * that's half duplex, not currently supported
2333          */
2334 #endif
2335
2336 #ifdef CONFIG_USB_PL2301
2337 {
2338         USB_DEVICE (0x067b, 0x0000),    // PL-2301
2339         .driver_info =  (unsigned long) &prolific_info,
2340 }, {
2341         USB_DEVICE (0x067b, 0x0001),    // PL-2302
2342         .driver_info =  (unsigned long) &prolific_info,
2343 },
2344 #endif
2345
2346 #ifdef  CONFIG_USB_RNDIS
2347 {
2348         /* RNDIS is MSFT's un-official variant of CDC ACM */
2349         USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2350         .driver_info = (unsigned long) &rndis_info,
2351 },
2352 #endif
2353
2354 #if     defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER)
2355 /*
2356  * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2357  * Same idea as above, but different framing.
2358  *
2359  * PXA-2xx based models are also lying-about-cdc.
2360  * Some models don't even tell the same lies ...
2361  *
2362  * NOTE:  OpenZaurus versions with 2.6 kernels won't use these entries,
2363  * unlike the older ones with 2.4 "embedix" kernels.
2364  *
2365  * NOTE:  These entries do double-duty, serving as blacklist entries
2366  * whenever Zaurus support isn't enabled, but CDC Ethernet is.
2367  */
2368 #define ZAURUS_MASTER_INTERFACE \
2369         .bInterfaceClass        = USB_CLASS_COMM, \
2370         .bInterfaceSubClass     = USB_CDC_SUBCLASS_ETHERNET, \
2371         .bInterfaceProtocol     = USB_CDC_PROTO_NONE
2372 {
2373         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2374                           | USB_DEVICE_ID_MATCH_DEVICE, 
2375         .idVendor               = 0x04DD,
2376         .idProduct              = 0x8004,
2377         ZAURUS_MASTER_INTERFACE,
2378         .driver_info = ZAURUS_STRONGARM_INFO,
2379 }, {
2380         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2381                           | USB_DEVICE_ID_MATCH_DEVICE, 
2382         .idVendor               = 0x04DD,
2383         .idProduct              = 0x8005,       /* A-300 */
2384         ZAURUS_MASTER_INTERFACE,
2385         .driver_info = ZAURUS_PXA_INFO,
2386 }, {
2387         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2388                           | USB_DEVICE_ID_MATCH_DEVICE, 
2389         .idVendor               = 0x04DD,
2390         .idProduct              = 0x8006,       /* B-500/SL-5600 */
2391         ZAURUS_MASTER_INTERFACE,
2392         .driver_info = ZAURUS_PXA_INFO,
2393 }, {
2394         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2395                   | USB_DEVICE_ID_MATCH_DEVICE,
2396         .idVendor               = 0x04DD,
2397         .idProduct              = 0x8007,       /* C-700 */
2398         ZAURUS_MASTER_INTERFACE,
2399         .driver_info = ZAURUS_PXA_INFO,
2400 }, {
2401         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2402                  | USB_DEVICE_ID_MATCH_DEVICE,
2403         .idVendor               = 0x04DD,
2404         .idProduct              = 0x9031,       /* C-750 C-760 */
2405         ZAURUS_MASTER_INTERFACE,
2406         .driver_info = ZAURUS_PXA_INFO,
2407 }, {
2408         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2409                  | USB_DEVICE_ID_MATCH_DEVICE,
2410         .idVendor               = 0x04DD,
2411         .idProduct              = 0x9032,       /* SL-6000 */
2412         ZAURUS_MASTER_INTERFACE,
2413         .driver_info = ZAURUS_PXA_INFO,
2414 }, {
2415         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2416                  | USB_DEVICE_ID_MATCH_DEVICE,
2417         .idVendor               = 0x04DD,
2418         /* reported with some C860 units */
2419         .idProduct              = 0x9050,       /* C-860 */
2420         ZAURUS_MASTER_INTERFACE,
2421         .driver_info = ZAURUS_PXA_INFO,
2422 },
2423
2424 #ifdef  CONFIG_USB_ZAURUS
2425         /* At least some (reports vary) PXA units have very different lies
2426          * about their standards support:  they claim to be cell phones with
2427          * direct access to their radios.  (They don't conform to CDC MDLM.)
2428          */
2429 {
2430         USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
2431                         USB_CDC_PROTO_NONE),
2432         .driver_info = (unsigned long) &bogus_mdlm_info,
2433 },
2434 #endif
2435
2436 /* Olympus has some models with a Zaurus-compatible option.
2437  * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
2438  */
2439 {
2440         .match_flags    =   USB_DEVICE_ID_MATCH_INT_INFO
2441                  | USB_DEVICE_ID_MATCH_DEVICE,
2442         .idVendor               = 0x07B4,
2443         .idProduct              = 0x0F02,       /* R-1000 */
2444         ZAURUS_MASTER_INTERFACE,
2445         .driver_info = OLYMPUS_MXL_INFO,
2446 },
2447 #endif
2448
2449 #ifdef  CONFIG_USB_CDCETHER
2450 {
2451         /* CDC Ether uses two interfaces, not necessarily consecutive.
2452          * We match the main interface, ignoring the optional device
2453          * class so we could handle devices that aren't exclusively
2454          * CDC ether.
2455          *
2456          * NOTE:  this match must come AFTER entries working around
2457          * bugs/quirks in a given product (like Zaurus, above).
2458          */
2459         USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
2460                         USB_CDC_PROTO_NONE),
2461         .driver_info = (unsigned long) &cdc_info,
2462 },
2463 #endif
2464
2465         { },            // END
2466 };
2467 MODULE_DEVICE_TABLE (usb, products);
2468
2469 static struct usb_driver usbnet_driver = {
2470         .owner =        THIS_MODULE,
2471         .name =         driver_name,
2472         .id_table =     products,
2473         .probe =        usbnet_probe,
2474         .disconnect =   usbnet_disconnect,
2475         .suspend =      usbnet_suspend,
2476         .resume =       usbnet_resume,
2477 };
2478
2479 /*-------------------------------------------------------------------------*/
2480
2481 static int __init usbnet_init(void)
2482 {
2483         // compiler should optimize these out
2484         BUG_ON (sizeof (((struct sk_buff *)0)->cb)
2485                         < sizeof (struct skb_data));
2486 #ifdef  CONFIG_USB_CDCETHER
2487         BUG_ON ((sizeof (((struct usbnet *)0)->data)
2488                         < sizeof (struct cdc_state)));
2489 #endif
2490
2491         random_ether_addr(node_id);
2492
2493         return usb_register(&usbnet_driver);
2494 }
2495 module_init(usbnet_init);
2496
2497 static void __exit usbnet_exit(void)
2498 {
2499         usb_deregister(&usbnet_driver);
2500 }
2501 module_exit(usbnet_exit);
2502
2503 MODULE_AUTHOR("David Brownell");
2504 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
2505 MODULE_LICENSE("GPL");