[PATCH] USB: usbnet (6/9) module for Zaurii and compatibles
[linux-2.6] / drivers / usb / net / usbnet.c
1 /*
2  * USB Networking Links
3  * Copyright (C) 2000-2005 by David Brownell
4  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * This is a generic "USB networking" framework that works with several
23  * kinds of full and high speed networking devices:
24  *
25  *   + USB host-to-host "network cables", used for IP-over-USB links.
26  *     These are often used for Laplink style connectivity products.
27  *      - AnchorChip 2720
28  *      - Belkin, eTEK (interops with Win32 drivers)
29  *      - GeneSys GL620USB-A
30  *      - NetChip 1080 (interoperates with NetChip Win32 drivers)
31  *      - Prolific PL-2301/2302 (replaces "plusb" driver)
32  *      - KC Technology KC2190
33  *
34  *   + Smart USB devices can support such links directly, using Internet
35  *     standard protocols instead of proprietary host-to-device links.
36  *      - Linux PDAs like iPaq, Yopy, and Zaurus
37  *      - The BLOB boot loader (for diskless booting)
38  *      - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
39  *      - Devices using EPSON's sample USB firmware
40  *      - CDC-Ethernet class devices, such as many cable modems
41  *
42  *   + Adapters to networks such as Ethernet.
43  *      - AX8817X based USB 2.0 products
44  *
45  * Links to these devices can be bridged using Linux Ethernet bridging.
46  * With minor exceptions, these all use similar USB framing for network
47  * traffic, but need different protocols for control traffic.
48  *
49  * USB devices can implement their side of this protocol at the cost
50  * of two bulk endpoints; it's not restricted to "cable" applications.
51  * See the SA1110, Zaurus, or EPSON device/client support in this driver;
52  * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
53  * "g_ether" (in the Linux "gadget" framework) implement that behavior
54  * within devices.
55  *
56  *
57  * CHANGELOG:
58  *
59  * 13-sep-2000  experimental, new
60  * 10-oct-2000  usb_device_id table created. 
61  * 28-oct-2000  misc fixes; mostly, discard more TTL-mangled rx packets.
62  * 01-nov-2000  usb_device_id table and probing api update by
63  *              Adam J. Richter <adam@yggdrasil.com>.
64  * 18-dec-2000  (db) tx watchdog, "net1080" renaming to "usbnet", device_info
65  *              and prolific support, isolate net1080-specific bits, cleanup.
66  *              fix unlink_urbs oops in D3 PM resume code path.
67  *
68  * 02-feb-2001  (db) fix tx skb sharing, packet length, match_flags, ...
69  * 08-feb-2001  stubbed in "linuxdev", maybe the SA-1100 folk can use it;
70  *              AnchorChips 2720 support (from spec) for testing;
71  *              fix bit-ordering problem with ethernet multicast addr
72  * 19-feb-2001  Support for clearing halt conditions. SA1100 UDC support
73  *              updates. Oleg Drokin (green@iXcelerator.com)
74  * 25-mar-2001  More SA-1100 updates, including workaround for ip problem
75  *              expecting cleared skb->cb and framing change to match latest
76  *              handhelds.org version (Oleg).  Enable device IDs from the
77  *              Win32 Belkin driver; other cleanups (db).
78  * 16-jul-2001  Bugfixes for uhci oops-on-unplug, Belkin support, various
79  *              cleanups for problems not yet seen in the field. (db)
80  * 17-oct-2001  Handle "Advance USBNET" product, like Belkin/eTEK devices,
81  *              from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
82  *              rx unlinks somehow weren't async; minor cleanup.
83  * 03-nov-2001  Merged GeneSys driver; original code from Jiun-Jie Huang
84  *              <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
85  *              <utx@penguin.cz>.  Made framing options (NetChip/GeneSys)
86  *              tie mostly to (sub)driver info.  Workaround some PL-2302
87  *              chips that seem to reject SET_INTERFACE requests.
88  *
89  * 06-apr-2002  Added ethtool support, based on a patch from Brad Hards.
90  *              Level of diagnostics is more configurable; they use device
91  *              location (usb_device->devpath) instead of address (2.5).
92  *              For tx_fixup, memflags can't be NOIO.
93  * 07-may-2002  Generalize/cleanup keventd support, handling rx stalls (mostly
94  *              for USB 2.0 TTs) and memory shortages (potential) too. (db)
95  *              Use "locally assigned" IEEE802 address space. (Brad Hards)
96  * 18-oct-2002  Support for Zaurus (Pavel Machek), related cleanup (db).
97  * 14-dec-2002  Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
98  *              cleanups and stubbed PXA-250 support (db), fix for framing
99  *              issues on Z, net1080, and gl620a (Toby Milne)
100  *
101  * 31-mar-2003  Use endpoint descriptors:  high speed support, simpler sa1100
102  *              vs pxa25x, and CDC Ethernet.  Throttle down log floods on
103  *              disconnect; other cleanups. (db)  Flush net1080 fifos
104  *              after several sequential framing errors. (Johannes Erdfelt)
105  * 22-aug-2003  AX8817X support (Dave Hollis).
106  * 14-jun-2004  Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
107  *              (Neil Bortnak)
108  * 03-nov-2004  Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
109  *
110  * 01-feb-2005  AX88772 support (Phil Chang & Dave Hollis)
111  *-------------------------------------------------------------------------*/
112
113 // #define      DEBUG                   // error path messages, extra info
114 // #define      VERBOSE                 // more; success messages
115
116 #include <linux/config.h>
117 #ifdef  CONFIG_USB_DEBUG
118 #   define DEBUG
119 #endif
120 #include <linux/module.h>
121 #include <linux/sched.h>
122 #include <linux/init.h>
123 #include <linux/netdevice.h>
124 #include <linux/etherdevice.h>
125 #include <linux/ethtool.h>
126 #include <linux/workqueue.h>
127 #include <linux/mii.h>
128 #include <linux/usb.h>
129
130 #include "usbnet.h"
131
132 #define DRIVER_VERSION          "22-Aug-2005"
133
134
135 /*-------------------------------------------------------------------------*/
136
137 /*
138  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
139  * Several dozen bytes of IPv4 data can fit in two such transactions.
140  * One maximum size Ethernet packet takes twenty four of them.
141  * For high speed, each frame comfortably fits almost 36 max size
142  * Ethernet packets (so queues should be bigger).
143  *
144  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
145  * let the USB host controller be busy for 5msec or more before an irq
146  * is required, under load.  Jumbograms change the equation.
147  */
148 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
149 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
150
151 // reawaken network queue this soon after stopping; else watchdog barks
152 #define TX_TIMEOUT_JIFFIES      (5*HZ)
153
154 // throttle rx/tx briefly after some faults, so khubd might disconnect()
155 // us (it polls at HZ/4 usually) before we report too many false errors.
156 #define THROTTLE_JIFFIES        (HZ/8)
157
158 // between wakeups
159 #define UNLINK_TIMEOUT_MS       3
160
161 /*-------------------------------------------------------------------------*/
162
163 // randomly generated ethernet address
164 static u8       node_id [ETH_ALEN];
165
166 static const char driver_name [] = "usbnet";
167
168 /* use ethtool to change the level for any given device */
169 static int msg_level = -1;
170 module_param (msg_level, int, 0);
171 MODULE_PARM_DESC (msg_level, "Override default message level");
172
173 /*-------------------------------------------------------------------------*/
174
175 /* handles CDC Ethernet and many other network "bulk data" interfaces */
176 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
177 {
178         int                             tmp;
179         struct usb_host_interface       *alt = NULL;
180         struct usb_host_endpoint        *in = NULL, *out = NULL;
181         struct usb_host_endpoint        *status = NULL;
182
183         for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
184                 unsigned        ep;
185
186                 in = out = status = NULL;
187                 alt = intf->altsetting + tmp;
188
189                 /* take the first altsetting with in-bulk + out-bulk;
190                  * remember any status endpoint, just in case;
191                  * ignore other endpoints and altsetttings.
192                  */
193                 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
194                         struct usb_host_endpoint        *e;
195                         int                             intr = 0;
196
197                         e = alt->endpoint + ep;
198                         switch (e->desc.bmAttributes) {
199                         case USB_ENDPOINT_XFER_INT:
200                                 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
201                                         continue;
202                                 intr = 1;
203                                 /* FALLTHROUGH */
204                         case USB_ENDPOINT_XFER_BULK:
205                                 break;
206                         default:
207                                 continue;
208                         }
209                         if (e->desc.bEndpointAddress & USB_DIR_IN) {
210                                 if (!intr && !in)
211                                         in = e;
212                                 else if (intr && !status)
213                                         status = e;
214                         } else {
215                                 if (!out)
216                                         out = e;
217                         }
218                 }
219                 if (in && out)
220                         break;
221         }
222         if (!alt || !in || !out)
223                 return -EINVAL;
224
225         if (alt->desc.bAlternateSetting != 0
226                         || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
227                 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
228                                 alt->desc.bAlternateSetting);
229                 if (tmp < 0)
230                         return tmp;
231         }
232         
233         dev->in = usb_rcvbulkpipe (dev->udev,
234                         in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
235         dev->out = usb_sndbulkpipe (dev->udev,
236                         out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
237         dev->status = status;
238         return 0;
239 }
240 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
241
242 static void intr_complete (struct urb *urb, struct pt_regs *regs);
243
244 static int init_status (struct usbnet *dev, struct usb_interface *intf)
245 {
246         char            *buf = NULL;
247         unsigned        pipe = 0;
248         unsigned        maxp;
249         unsigned        period;
250
251         if (!dev->driver_info->status)
252                 return 0;
253
254         pipe = usb_rcvintpipe (dev->udev,
255                         dev->status->desc.bEndpointAddress
256                                 & USB_ENDPOINT_NUMBER_MASK);
257         maxp = usb_maxpacket (dev->udev, pipe, 0);
258
259         /* avoid 1 msec chatter:  min 8 msec poll rate */
260         period = max ((int) dev->status->desc.bInterval,
261                 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
262
263         buf = kmalloc (maxp, SLAB_KERNEL);
264         if (buf) {
265                 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
266                 if (!dev->interrupt) {
267                         kfree (buf);
268                         return -ENOMEM;
269                 } else {
270                         usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
271                                 buf, maxp, intr_complete, dev, period);
272                         dev_dbg(&intf->dev,
273                                 "status ep%din, %d bytes period %d\n",
274                                 usb_pipeendpoint(pipe), maxp, period);
275                 }
276         }
277         return  0;
278 }
279
280 /* Passes this packet up the stack, updating its accounting.
281  * Some link protocols batch packets, so their rx_fixup paths
282  * can return clones as well as just modify the original skb.
283  */
284 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
285 {
286         int     status;
287
288         skb->dev = dev->net;
289         skb->protocol = eth_type_trans (skb, dev->net);
290         dev->stats.rx_packets++;
291         dev->stats.rx_bytes += skb->len;
292
293         if (netif_msg_rx_status (dev))
294                 devdbg (dev, "< rx, len %zu, type 0x%x",
295                         skb->len + sizeof (struct ethhdr), skb->protocol);
296         memset (skb->cb, 0, sizeof (struct skb_data));
297         status = netif_rx (skb);
298         if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
299                 devdbg (dev, "netif_rx status %d", status);
300 }
301 EXPORT_SYMBOL_GPL(usbnet_skb_return);
302
303 \f
304 /*-------------------------------------------------------------------------
305  *
306  * Communications Device Class declarations.
307  * Used by CDC Ethernet, and some CDC variants
308  *
309  *-------------------------------------------------------------------------*/
310
311 #ifdef  CONFIG_USB_CDCETHER
312 #define NEED_GENERIC_CDC
313 #endif
314
315 #if defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_ZAURUS_MODULE)
316 /* Ethernet variant uses funky framing, broken ethernet addressing */
317 #define NEED_GENERIC_CDC
318 #endif
319
320 #ifdef  CONFIG_USB_RNDIS
321 /* ACM variant uses even funkier framing, complex control RPC scheme */
322 #define NEED_GENERIC_CDC
323 #endif
324
325
326 #ifdef  NEED_GENERIC_CDC
327
328 #include <linux/usb_cdc.h>
329
330 static struct usb_driver usbnet_driver;
331
332 /*
333  * probes control interface, claims data interface, collects the bulk
334  * endpoints, activates data interface (if needed), maybe sets MTU.
335  * all pure cdc, except for certain firmware workarounds.
336  */
337 int usbnet_generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
338 {
339         u8                              *buf = intf->cur_altsetting->extra;
340         int                             len = intf->cur_altsetting->extralen;
341         struct usb_interface_descriptor *d;
342         struct cdc_state                *info = (void *) &dev->data;
343         int                             status;
344         int                             rndis;
345
346         if (sizeof dev->data < sizeof *info)
347                 return -EDOM;
348
349         /* expect strict spec conformance for the descriptors, but
350          * cope with firmware which stores them in the wrong place
351          */
352         if (len == 0 && dev->udev->actconfig->extralen) {
353                 /* Motorola SB4100 (and others: Brad Hards says it's
354                  * from a Broadcom design) put CDC descriptors here
355                  */
356                 buf = dev->udev->actconfig->extra;
357                 len = dev->udev->actconfig->extralen;
358                 if (len)
359                         dev_dbg (&intf->dev,
360                                 "CDC descriptors on config\n");
361         }
362
363         /* this assumes that if there's a non-RNDIS vendor variant
364          * of cdc-acm, it'll fail RNDIS requests cleanly.
365          */
366         rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
367
368         memset (info, 0, sizeof *info);
369         info->control = intf;
370         while (len > 3) {
371                 if (buf [1] != USB_DT_CS_INTERFACE)
372                         goto next_desc;
373
374                 /* use bDescriptorSubType to identify the CDC descriptors.
375                  * We expect devices with CDC header and union descriptors.
376                  * For CDC Ethernet we need the ethernet descriptor.
377                  * For RNDIS, ignore two (pointless) CDC modem descriptors
378                  * in favor of a complicated OID-based RPC scheme doing what
379                  * CDC Ethernet achieves with a simple descriptor.
380                  */
381                 switch (buf [2]) {
382                 case USB_CDC_HEADER_TYPE:
383                         if (info->header) {
384                                 dev_dbg (&intf->dev, "extra CDC header\n");
385                                 goto bad_desc;
386                         }
387                         info->header = (void *) buf;
388                         if (info->header->bLength != sizeof *info->header) {
389                                 dev_dbg (&intf->dev, "CDC header len %u\n",
390                                         info->header->bLength);
391                                 goto bad_desc;
392                         }
393                         break;
394                 case USB_CDC_UNION_TYPE:
395                         if (info->u) {
396                                 dev_dbg (&intf->dev, "extra CDC union\n");
397                                 goto bad_desc;
398                         }
399                         info->u = (void *) buf;
400                         if (info->u->bLength != sizeof *info->u) {
401                                 dev_dbg (&intf->dev, "CDC union len %u\n",
402                                         info->u->bLength);
403                                 goto bad_desc;
404                         }
405
406                         /* we need a master/control interface (what we're
407                          * probed with) and a slave/data interface; union
408                          * descriptors sort this all out.
409                          */
410                         info->control = usb_ifnum_to_if(dev->udev,
411                                                 info->u->bMasterInterface0);
412                         info->data = usb_ifnum_to_if(dev->udev,
413                                                 info->u->bSlaveInterface0);
414                         if (!info->control || !info->data) {
415                                 dev_dbg (&intf->dev,
416                                         "master #%u/%p slave #%u/%p\n",
417                                         info->u->bMasterInterface0,
418                                         info->control,
419                                         info->u->bSlaveInterface0,
420                                         info->data);
421                                 goto bad_desc;
422                         }
423                         if (info->control != intf) {
424                                 dev_dbg (&intf->dev, "bogus CDC Union\n");
425                                 /* Ambit USB Cable Modem (and maybe others)
426                                  * interchanges master and slave interface.
427                                  */
428                                 if (info->data == intf) {
429                                         info->data = info->control;
430                                         info->control = intf;
431                                 } else
432                                         goto bad_desc;
433                         }
434
435                         /* a data interface altsetting does the real i/o */
436                         d = &info->data->cur_altsetting->desc;
437                         if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
438                                 dev_dbg (&intf->dev, "slave class %u\n",
439                                         d->bInterfaceClass);
440                                 goto bad_desc;
441                         }
442                         break;
443                 case USB_CDC_ETHERNET_TYPE:
444                         if (info->ether) {
445                                 dev_dbg (&intf->dev, "extra CDC ether\n");
446                                 goto bad_desc;
447                         }
448                         info->ether = (void *) buf;
449                         if (info->ether->bLength != sizeof *info->ether) {
450                                 dev_dbg (&intf->dev, "CDC ether len %u\n",
451                                         info->ether->bLength);
452                                 goto bad_desc;
453                         }
454                         dev->hard_mtu = le16_to_cpu(
455                                                 info->ether->wMaxSegmentSize);
456                         /* because of Zaurus, we may be ignoring the host
457                          * side link address we were given.
458                          */
459                         break;
460                 }
461 next_desc:
462                 len -= buf [0]; /* bLength */
463                 buf += buf [0];
464         }
465
466         if (!info->header || !info->u || (!rndis && !info->ether)) {
467                 dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
468                         info->header ? "" : "header ",
469                         info->u ? "" : "union ",
470                         info->ether ? "" : "ether ");
471                 goto bad_desc;
472         }
473
474         /* claim data interface and set it up ... with side effects.
475          * network traffic can't flow until an altsetting is enabled.
476          */
477         status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
478         if (status < 0)
479                 return status;
480         status = usbnet_get_endpoints (dev, info->data);
481         if (status < 0) {
482                 /* ensure immediate exit from usbnet_disconnect */
483                 usb_set_intfdata(info->data, NULL);
484                 usb_driver_release_interface (&usbnet_driver, info->data);
485                 return status;
486         }
487
488         /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
489         dev->status = NULL;
490         if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
491                 struct usb_endpoint_descriptor  *desc;
492
493                 dev->status = &info->control->cur_altsetting->endpoint [0];
494                 desc = &dev->status->desc;
495                 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
496                                 || !(desc->bEndpointAddress & USB_DIR_IN)
497                                 || (le16_to_cpu(desc->wMaxPacketSize)
498                                         < sizeof (struct usb_cdc_notification))
499                                 || !desc->bInterval) {
500                         dev_dbg (&intf->dev, "bad notification endpoint\n");
501                         dev->status = NULL;
502                 }
503         }
504         if (rndis && !dev->status) {
505                 dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
506                 usb_set_intfdata(info->data, NULL);
507                 usb_driver_release_interface (&usbnet_driver, info->data);
508                 return -ENODEV;
509         }
510         return 0;
511
512 bad_desc:
513         dev_info (&dev->udev->dev, "bad CDC descriptors\n");
514         return -ENODEV;
515 }
516 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
517
518 void usbnet_cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
519 {
520         struct cdc_state                *info = (void *) &dev->data;
521
522         /* disconnect master --> disconnect slave */
523         if (intf == info->control && info->data) {
524                 /* ensure immediate exit from usbnet_disconnect */
525                 usb_set_intfdata(info->data, NULL);
526                 usb_driver_release_interface (&usbnet_driver, info->data);
527                 info->data = NULL;
528         }
529
530         /* and vice versa (just in case) */
531         else if (intf == info->data && info->control) {
532                 /* ensure immediate exit from usbnet_disconnect */
533                 usb_set_intfdata(info->control, NULL);
534                 usb_driver_release_interface (&usbnet_driver, info->control);
535                 info->control = NULL;
536         }
537 }
538 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
539
540 #endif  /* NEED_GENERIC_CDC */
541
542 \f
543 #ifdef  CONFIG_USB_CDCETHER
544 #define HAVE_HARDWARE
545
546 /*-------------------------------------------------------------------------
547  *
548  * Communications Device Class, Ethernet Control model
549  *
550  * Takes two interfaces.  The DATA interface is inactive till an altsetting
551  * is selected.  Configuration data includes class descriptors.
552  *
553  * This should interop with whatever the 2.4 "CDCEther.c" driver
554  * (by Brad Hards) talked with.
555  *
556  *-------------------------------------------------------------------------*/
557
558 #include <linux/ctype.h>
559
560
561 static void dumpspeed (struct usbnet *dev, __le32 *speeds)
562 {
563         if (netif_msg_timer (dev))
564                 devinfo (dev, "link speeds: %u kbps up, %u kbps down",
565                         __le32_to_cpu(speeds[0]) / 1000,
566                 __le32_to_cpu(speeds[1]) / 1000);
567 }
568
569 static void cdc_status (struct usbnet *dev, struct urb *urb)
570 {
571         struct usb_cdc_notification     *event;
572
573         if (urb->actual_length < sizeof *event)
574                 return;
575         
576         /* SPEED_CHANGE can get split into two 8-byte packets */
577         if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
578                 dumpspeed (dev, (__le32 *) urb->transfer_buffer);
579                 return;
580         }
581
582         event = urb->transfer_buffer;
583         switch (event->bNotificationType) {
584         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
585                 if (netif_msg_timer (dev))
586                         devdbg (dev, "CDC: carrier %s",
587                                         event->wValue ? "on" : "off");
588                 if (event->wValue)
589                         netif_carrier_on(dev->net);
590                 else
591                         netif_carrier_off(dev->net);
592                 break;
593         case USB_CDC_NOTIFY_SPEED_CHANGE:       /* tx/rx rates */
594                 if (netif_msg_timer (dev))
595                         devdbg (dev, "CDC: speed change (len %d)",
596                                         urb->actual_length);
597                 if (urb->actual_length != (sizeof *event + 8))
598                         set_bit (EVENT_STS_SPLIT, &dev->flags);
599                 else
600                         dumpspeed (dev, (__le32 *) &event[1]);
601                 break;
602         // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:      /* RNDIS; or unsolicited */
603         default:
604                 deverr (dev, "CDC: unexpected notification %02x!",
605                                  event->bNotificationType);
606                 break;
607         }
608 }
609
610 static u8 nibble (unsigned char c)
611 {
612         if (likely (isdigit (c)))
613                 return c - '0';
614         c = toupper (c);
615         if (likely (isxdigit (c)))
616                 return 10 + c - 'A';
617         return 0;
618 }
619
620 static inline int
621 get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
622 {
623         int             tmp, i;
624         unsigned char   buf [13];
625
626         tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
627         if (tmp != 12) {
628                 dev_dbg (&dev->udev->dev,
629                         "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
630                 if (tmp >= 0)
631                         tmp = -EINVAL;
632                 return tmp;
633         }
634         for (i = tmp = 0; i < 6; i++, tmp += 2)
635                 dev->net->dev_addr [i] =
636                          (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
637         return 0;
638 }
639
640 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
641 {
642         int                             status;
643         struct cdc_state                *info = (void *) &dev->data;
644
645         status = usbnet_generic_cdc_bind (dev, intf);
646         if (status < 0)
647                 return status;
648
649         status = get_ethernet_addr (dev, info->ether);
650         if (status < 0) {
651                 usb_set_intfdata(info->data, NULL);
652                 usb_driver_release_interface (&usbnet_driver, info->data);
653                 return status;
654         }
655
656         /* FIXME cdc-ether has some multicast code too, though it complains
657          * in routine cases.  info->ether describes the multicast support.
658          */
659         return 0;
660 }
661
662 static const struct driver_info cdc_info = {
663         .description =  "CDC Ethernet Device",
664         .flags =        FLAG_ETHER,
665         // .check_connect = cdc_check_connect,
666         .bind =         cdc_bind,
667         .unbind =       usbnet_cdc_unbind,
668         .status =       cdc_status,
669 };
670
671 #endif  /* CONFIG_USB_CDCETHER */
672
673
674 \f
675 #ifdef CONFIG_USB_PL2301
676 #define HAVE_HARDWARE
677
678 /*-------------------------------------------------------------------------
679  *
680  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
681  *
682  * The protocol and handshaking used here should be bug-compatible
683  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
684  *
685  *-------------------------------------------------------------------------*/
686
687 /*
688  * Bits 0-4 can be used for software handshaking; they're set from
689  * one end, cleared from the other, "read" with the interrupt byte.
690  */
691 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
692 /* reserved bit -- rx ready (6) ? */
693 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
694 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
695 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
696 #define PL_TX_C         (1<<2)          /* transmission complete */
697 #define PL_TX_REQ       (1<<1)          /* transmission received */
698 #define PL_PEER_E       (1<<0)          /* peer exists */
699
700 static inline int
701 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
702 {
703         return usb_control_msg (dev->udev,
704                 usb_rcvctrlpipe (dev->udev, 0),
705                 req,
706                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
707                 val, index,
708                 NULL, 0,
709                 USB_CTRL_GET_TIMEOUT);
710 }
711
712 static inline int
713 pl_clear_QuickLink_features (struct usbnet *dev, int val)
714 {
715         return pl_vendor_req (dev, 1, (u8) val, 0);
716 }
717
718 static inline int
719 pl_set_QuickLink_features (struct usbnet *dev, int val)
720 {
721         return pl_vendor_req (dev, 3, (u8) val, 0);
722 }
723
724 /*-------------------------------------------------------------------------*/
725
726 static int pl_reset (struct usbnet *dev)
727 {
728         /* some units seem to need this reset, others reject it utterly.
729          * FIXME be more like "naplink" or windows drivers.
730          */
731         (void) pl_set_QuickLink_features (dev,
732                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
733         return 0;
734 }
735
736 static const struct driver_info prolific_info = {
737         .description =  "Prolific PL-2301/PL-2302",
738         .flags =        FLAG_NO_SETINT,
739                 /* some PL-2302 versions seem to fail usb_set_interface() */
740         .reset =        pl_reset,
741 };
742
743 #endif /* CONFIG_USB_PL2301 */
744
745 \f
746 /*-------------------------------------------------------------------------
747  *
748  * Network Device Driver (peer link to "Host Device", from USB host)
749  *
750  *-------------------------------------------------------------------------*/
751
752 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
753 {
754         struct usbnet   *dev = netdev_priv(net);
755         int             ll_mtu = new_mtu + net->hard_header_len;
756
757         if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
758                 return -EINVAL;
759         // no second zero-length packet read wanted after mtu-sized packets
760         if ((ll_mtu % dev->maxpacket) == 0)
761                 return -EDOM;
762         net->mtu = new_mtu;
763         return 0;
764 }
765
766 /*-------------------------------------------------------------------------*/
767
768 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
769 {
770         struct usbnet   *dev = netdev_priv(net);
771         return &dev->stats;
772 }
773
774 /*-------------------------------------------------------------------------*/
775
776 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
777  * completion callbacks.  2.5 should have fixed those bugs...
778  */
779
780 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
781 {
782         unsigned long           flags;
783
784         spin_lock_irqsave(&list->lock, flags);
785         __skb_unlink(skb, list);
786         spin_unlock(&list->lock);
787         spin_lock(&dev->done.lock);
788         __skb_queue_tail(&dev->done, skb);
789         if (dev->done.qlen == 1)
790                 tasklet_schedule(&dev->bh);
791         spin_unlock_irqrestore(&dev->done.lock, flags);
792 }
793
794 /* some work can't be done in tasklets, so we use keventd
795  *
796  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
797  * but tasklet_schedule() doesn't.  hope the failure is rare.
798  */
799 void usbnet_defer_kevent (struct usbnet *dev, int work)
800 {
801         set_bit (work, &dev->flags);
802         if (!schedule_work (&dev->kevent))
803                 deverr (dev, "kevent %d may have been dropped", work);
804         else
805                 devdbg (dev, "kevent %d scheduled", work);
806 }
807 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
808
809 /*-------------------------------------------------------------------------*/
810
811 static void rx_complete (struct urb *urb, struct pt_regs *regs);
812
813 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
814 {
815         struct sk_buff          *skb;
816         struct skb_data         *entry;
817         int                     retval = 0;
818         unsigned long           lockflags;
819         size_t                  size = dev->rx_urb_size;
820
821         if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
822                 if (netif_msg_rx_err (dev))
823                         devdbg (dev, "no rx skb");
824                 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
825                 usb_free_urb (urb);
826                 return;
827         }
828         skb_reserve (skb, NET_IP_ALIGN);
829
830         entry = (struct skb_data *) skb->cb;
831         entry->urb = urb;
832         entry->dev = dev;
833         entry->state = rx_start;
834         entry->length = 0;
835
836         usb_fill_bulk_urb (urb, dev->udev, dev->in,
837                 skb->data, size, rx_complete, skb);
838
839         spin_lock_irqsave (&dev->rxq.lock, lockflags);
840
841         if (netif_running (dev->net)
842                         && netif_device_present (dev->net)
843                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
844                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
845                 case -EPIPE:
846                         usbnet_defer_kevent (dev, EVENT_RX_HALT);
847                         break;
848                 case -ENOMEM:
849                         usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
850                         break;
851                 case -ENODEV:
852                         if (netif_msg_ifdown (dev))
853                                 devdbg (dev, "device gone");
854                         netif_device_detach (dev->net);
855                         break;
856                 default:
857                         if (netif_msg_rx_err (dev))
858                                 devdbg (dev, "rx submit, %d", retval);
859                         tasklet_schedule (&dev->bh);
860                         break;
861                 case 0:
862                         __skb_queue_tail (&dev->rxq, skb);
863                 }
864         } else {
865                 if (netif_msg_ifdown (dev))
866                         devdbg (dev, "rx: stopped");
867                 retval = -ENOLINK;
868         }
869         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
870         if (retval) {
871                 dev_kfree_skb_any (skb);
872                 usb_free_urb (urb);
873         }
874 }
875
876
877 /*-------------------------------------------------------------------------*/
878
879 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
880 {
881         if (dev->driver_info->rx_fixup
882                         && !dev->driver_info->rx_fixup (dev, skb))
883                 goto error;
884         // else network stack removes extra byte if we forced a short packet
885
886         if (skb->len)
887                 usbnet_skb_return (dev, skb);
888         else {
889                 if (netif_msg_rx_err (dev))
890                         devdbg (dev, "drop");
891 error:
892                 dev->stats.rx_errors++;
893                 skb_queue_tail (&dev->done, skb);
894         }
895 }
896
897 /*-------------------------------------------------------------------------*/
898
899 static void rx_complete (struct urb *urb, struct pt_regs *regs)
900 {
901         struct sk_buff          *skb = (struct sk_buff *) urb->context;
902         struct skb_data         *entry = (struct skb_data *) skb->cb;
903         struct usbnet           *dev = entry->dev;
904         int                     urb_status = urb->status;
905
906         skb_put (skb, urb->actual_length);
907         entry->state = rx_done;
908         entry->urb = NULL;
909
910         switch (urb_status) {
911             // success
912             case 0:
913                 if (skb->len < dev->net->hard_header_len) {
914                         entry->state = rx_cleanup;
915                         dev->stats.rx_errors++;
916                         dev->stats.rx_length_errors++;
917                         if (netif_msg_rx_err (dev))
918                                 devdbg (dev, "rx length %d", skb->len);
919                 }
920                 break;
921
922             // stalls need manual reset. this is rare ... except that
923             // when going through USB 2.0 TTs, unplug appears this way.
924             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
925             // storm, recovering as needed.
926             case -EPIPE:
927                 dev->stats.rx_errors++;
928                 usbnet_defer_kevent (dev, EVENT_RX_HALT);
929                 // FALLTHROUGH
930
931             // software-driven interface shutdown
932             case -ECONNRESET:           // async unlink
933             case -ESHUTDOWN:            // hardware gone
934                 if (netif_msg_ifdown (dev))
935                         devdbg (dev, "rx shutdown, code %d", urb_status);
936                 goto block;
937
938             // we get controller i/o faults during khubd disconnect() delays.
939             // throttle down resubmits, to avoid log floods; just temporarily,
940             // so we still recover when the fault isn't a khubd delay.
941             case -EPROTO:               // ehci
942             case -ETIMEDOUT:            // ohci
943             case -EILSEQ:               // uhci
944                 dev->stats.rx_errors++;
945                 if (!timer_pending (&dev->delay)) {
946                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
947                         if (netif_msg_link (dev))
948                                 devdbg (dev, "rx throttle %d", urb_status);
949                 }
950 block:
951                 entry->state = rx_cleanup;
952                 entry->urb = urb;
953                 urb = NULL;
954                 break;
955
956             // data overrun ... flush fifo?
957             case -EOVERFLOW:
958                 dev->stats.rx_over_errors++;
959                 // FALLTHROUGH
960             
961             default:
962                 entry->state = rx_cleanup;
963                 dev->stats.rx_errors++;
964                 if (netif_msg_rx_err (dev))
965                         devdbg (dev, "rx status %d", urb_status);
966                 break;
967         }
968
969         defer_bh(dev, skb, &dev->rxq);
970
971         if (urb) {
972                 if (netif_running (dev->net)
973                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
974                         rx_submit (dev, urb, GFP_ATOMIC);
975                         return;
976                 }
977                 usb_free_urb (urb);
978         }
979         if (netif_msg_rx_err (dev))
980                 devdbg (dev, "no read resubmitted");
981 }
982
983 static void intr_complete (struct urb *urb, struct pt_regs *regs)
984 {
985         struct usbnet   *dev = urb->context;
986         int             status = urb->status;
987
988         switch (status) {
989             /* success */
990             case 0:
991                 dev->driver_info->status(dev, urb);
992                 break;
993
994             /* software-driven interface shutdown */
995             case -ENOENT:               // urb killed
996             case -ESHUTDOWN:            // hardware gone
997                 if (netif_msg_ifdown (dev))
998                         devdbg (dev, "intr shutdown, code %d", status);
999                 return;
1000
1001             /* NOTE:  not throttling like RX/TX, since this endpoint
1002              * already polls infrequently
1003              */
1004             default:
1005                 devdbg (dev, "intr status %d", status);
1006                 break;
1007         }
1008
1009         if (!netif_running (dev->net))
1010                 return;
1011
1012         memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
1013         status = usb_submit_urb (urb, GFP_ATOMIC);
1014         if (status != 0 && netif_msg_timer (dev))
1015                 deverr(dev, "intr resubmit --> %d", status);
1016 }
1017
1018 /*-------------------------------------------------------------------------*/
1019
1020 // unlink pending rx/tx; completion handlers do all other cleanup
1021
1022 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1023 {
1024         unsigned long           flags;
1025         struct sk_buff          *skb, *skbnext;
1026         int                     count = 0;
1027
1028         spin_lock_irqsave (&q->lock, flags);
1029         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1030                 struct skb_data         *entry;
1031                 struct urb              *urb;
1032                 int                     retval;
1033
1034                 entry = (struct skb_data *) skb->cb;
1035                 urb = entry->urb;
1036                 skbnext = skb->next;
1037
1038                 // during some PM-driven resume scenarios,
1039                 // these (async) unlinks complete immediately
1040                 retval = usb_unlink_urb (urb);
1041                 if (retval != -EINPROGRESS && retval != 0)
1042                         devdbg (dev, "unlink urb err, %d", retval);
1043                 else
1044                         count++;
1045         }
1046         spin_unlock_irqrestore (&q->lock, flags);
1047         return count;
1048 }
1049
1050
1051 /*-------------------------------------------------------------------------*/
1052
1053 // precondition: never called in_interrupt
1054
1055 static int usbnet_stop (struct net_device *net)
1056 {
1057         struct usbnet           *dev = netdev_priv(net);
1058         int                     temp;
1059         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
1060         DECLARE_WAITQUEUE (wait, current);
1061
1062         netif_stop_queue (net);
1063
1064         if (netif_msg_ifdown (dev))
1065                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
1066                         dev->stats.rx_packets, dev->stats.tx_packets, 
1067                         dev->stats.rx_errors, dev->stats.tx_errors
1068                         );
1069
1070         // ensure there are no more active urbs
1071         add_wait_queue (&unlink_wakeup, &wait);
1072         dev->wait = &unlink_wakeup;
1073         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
1074
1075         // maybe wait for deletions to finish.
1076         while (!skb_queue_empty(&dev->rxq) &&
1077                !skb_queue_empty(&dev->txq) &&
1078                !skb_queue_empty(&dev->done)) {
1079                 msleep(UNLINK_TIMEOUT_MS);
1080                 if (netif_msg_ifdown (dev))
1081                         devdbg (dev, "waited for %d urb completions", temp);
1082         }
1083         dev->wait = NULL;
1084         remove_wait_queue (&unlink_wakeup, &wait); 
1085
1086         usb_kill_urb(dev->interrupt);
1087
1088         /* deferred work (task, timer, softirq) must also stop.
1089          * can't flush_scheduled_work() until we drop rtnl (later),
1090          * else workers could deadlock; so make workers a NOP.
1091          */
1092         dev->flags = 0;
1093         del_timer_sync (&dev->delay);
1094         tasklet_kill (&dev->bh);
1095
1096         return 0;
1097 }
1098
1099 /*-------------------------------------------------------------------------*/
1100
1101 // posts reads, and enables write queuing
1102
1103 // precondition: never called in_interrupt
1104
1105 static int usbnet_open (struct net_device *net)
1106 {
1107         struct usbnet           *dev = netdev_priv(net);
1108         int                     retval = 0;
1109         struct driver_info      *info = dev->driver_info;
1110
1111         // put into "known safe" state
1112         if (info->reset && (retval = info->reset (dev)) < 0) {
1113                 if (netif_msg_ifup (dev))
1114                         devinfo (dev,
1115                                 "open reset fail (%d) usbnet usb-%s-%s, %s",
1116                                 retval,
1117                                 dev->udev->bus->bus_name, dev->udev->devpath,
1118                         info->description);
1119                 goto done;
1120         }
1121
1122         // insist peer be connected
1123         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
1124                 if (netif_msg_ifup (dev))
1125                         devdbg (dev, "can't open; %d", retval);
1126                 goto done;
1127         }
1128
1129         /* start any status interrupt transfer */
1130         if (dev->interrupt) {
1131                 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
1132                 if (retval < 0) {
1133                         if (netif_msg_ifup (dev))
1134                                 deverr (dev, "intr submit %d", retval);
1135                         goto done;
1136                 }
1137         }
1138
1139         netif_start_queue (net);
1140         if (netif_msg_ifup (dev)) {
1141                 char    *framing;
1142
1143                 if (dev->driver_info->flags & FLAG_FRAMING_NC)
1144                         framing = "NetChip";
1145                 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
1146                         framing = "GeneSys";
1147                 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
1148                         framing = "Zaurus";
1149                 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
1150                         framing = "RNDIS";
1151                 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
1152                         framing = "ASIX";
1153                 else
1154                         framing = "simple";
1155
1156                 devinfo (dev, "open: enable queueing "
1157                                 "(rx %d, tx %d) mtu %d %s framing",
1158                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
1159                         framing);
1160         }
1161
1162         // delay posting reads until we're fully open
1163         tasklet_schedule (&dev->bh);
1164 done:
1165         return retval;
1166 }
1167
1168 /*-------------------------------------------------------------------------*/
1169
1170 /* ethtool methods; minidrivers may need to add some more, but
1171  * they'll probably want to use this base set.
1172  */
1173
1174 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1175 {
1176         struct usbnet *dev = netdev_priv(net);
1177
1178         /* REVISIT don't always return "usbnet" */
1179         strncpy (info->driver, driver_name, sizeof info->driver);
1180         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
1181         strncpy (info->fw_version, dev->driver_info->description,
1182                 sizeof info->fw_version);
1183         usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1184 }
1185 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1186
1187 static u32 usbnet_get_link (struct net_device *net)
1188 {
1189         struct usbnet *dev = netdev_priv(net);
1190
1191         /* If a check_connect is defined, return its result */
1192         if (dev->driver_info->check_connect)
1193                 return dev->driver_info->check_connect (dev) == 0;
1194
1195         /* Otherwise, say we're up (to avoid breaking scripts) */
1196         return 1;
1197 }
1198
1199 u32 usbnet_get_msglevel (struct net_device *net)
1200 {
1201         struct usbnet *dev = netdev_priv(net);
1202
1203         return dev->msg_enable;
1204 }
1205 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1206
1207 void usbnet_set_msglevel (struct net_device *net, u32 level)
1208 {
1209         struct usbnet *dev = netdev_priv(net);
1210
1211         dev->msg_enable = level;
1212 }
1213 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1214
1215 /* drivers may override default ethtool_ops in their bind() routine */
1216 static struct ethtool_ops usbnet_ethtool_ops = {
1217         .get_drvinfo            = usbnet_get_drvinfo,
1218         .get_link               = usbnet_get_link,
1219         .get_msglevel           = usbnet_get_msglevel,
1220         .set_msglevel           = usbnet_set_msglevel,
1221 };
1222
1223 /*-------------------------------------------------------------------------*/
1224
1225 /* work that cannot be done in interrupt context uses keventd.
1226  *
1227  * NOTE:  with 2.5 we could do more of this using completion callbacks,
1228  * especially now that control transfers can be queued.
1229  */
1230 static void
1231 kevent (void *data)
1232 {
1233         struct usbnet           *dev = data;
1234         int                     status;
1235
1236         /* usb_clear_halt() needs a thread context */
1237         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1238                 unlink_urbs (dev, &dev->txq);
1239                 status = usb_clear_halt (dev->udev, dev->out);
1240                 if (status < 0
1241                                 && status != -EPIPE
1242                                 && status != -ESHUTDOWN) {
1243                         if (netif_msg_tx_err (dev))
1244                                 deverr (dev, "can't clear tx halt, status %d",
1245                                         status);
1246                 } else {
1247                         clear_bit (EVENT_TX_HALT, &dev->flags);
1248                         if (status != -ESHUTDOWN)
1249                                 netif_wake_queue (dev->net);
1250                 }
1251         }
1252         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1253                 unlink_urbs (dev, &dev->rxq);
1254                 status = usb_clear_halt (dev->udev, dev->in);
1255                 if (status < 0
1256                                 && status != -EPIPE
1257                                 && status != -ESHUTDOWN) {
1258                         if (netif_msg_rx_err (dev))
1259                                 deverr (dev, "can't clear rx halt, status %d",
1260                                         status);
1261                 } else {
1262                         clear_bit (EVENT_RX_HALT, &dev->flags);
1263                         tasklet_schedule (&dev->bh);
1264                 }
1265         }
1266
1267         /* tasklet could resubmit itself forever if memory is tight */
1268         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1269                 struct urb      *urb = NULL;
1270
1271                 if (netif_running (dev->net))
1272                         urb = usb_alloc_urb (0, GFP_KERNEL);
1273                 else
1274                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
1275                 if (urb != NULL) {
1276                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
1277                         rx_submit (dev, urb, GFP_KERNEL);
1278                         tasklet_schedule (&dev->bh);
1279                 }
1280         }
1281
1282         if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1283                 struct driver_info      *info = dev->driver_info;
1284                 int                     retval = 0;
1285
1286                 clear_bit (EVENT_LINK_RESET, &dev->flags);
1287                 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1288                         devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
1289                                 retval,
1290                                 dev->udev->bus->bus_name, dev->udev->devpath,
1291                                 info->description);
1292                 }
1293         }
1294
1295         if (dev->flags)
1296                 devdbg (dev, "kevent done, flags = 0x%lx",
1297                         dev->flags);
1298 }
1299
1300 /*-------------------------------------------------------------------------*/
1301
1302 static void tx_complete (struct urb *urb, struct pt_regs *regs)
1303 {
1304         struct sk_buff          *skb = (struct sk_buff *) urb->context;
1305         struct skb_data         *entry = (struct skb_data *) skb->cb;
1306         struct usbnet           *dev = entry->dev;
1307
1308         if (urb->status == 0) {
1309                 dev->stats.tx_packets++;
1310                 dev->stats.tx_bytes += entry->length;
1311         } else {
1312                 dev->stats.tx_errors++;
1313
1314                 switch (urb->status) {
1315                 case -EPIPE:
1316                         usbnet_defer_kevent (dev, EVENT_TX_HALT);
1317                         break;
1318
1319                 /* software-driven interface shutdown */
1320                 case -ECONNRESET:               // async unlink
1321                 case -ESHUTDOWN:                // hardware gone
1322                         break;
1323
1324                 // like rx, tx gets controller i/o faults during khubd delays
1325                 // and so it uses the same throttling mechanism.
1326                 case -EPROTO:           // ehci
1327                 case -ETIMEDOUT:        // ohci
1328                 case -EILSEQ:           // uhci
1329                         if (!timer_pending (&dev->delay)) {
1330                                 mod_timer (&dev->delay,
1331                                         jiffies + THROTTLE_JIFFIES);
1332                                 if (netif_msg_link (dev))
1333                                         devdbg (dev, "tx throttle %d",
1334                                                         urb->status);
1335                         }
1336                         netif_stop_queue (dev->net);
1337                         break;
1338                 default:
1339                         if (netif_msg_tx_err (dev))
1340                                 devdbg (dev, "tx err %d", entry->urb->status);
1341                         break;
1342                 }
1343         }
1344
1345         urb->dev = NULL;
1346         entry->state = tx_done;
1347         defer_bh(dev, skb, &dev->txq);
1348 }
1349
1350 /*-------------------------------------------------------------------------*/
1351
1352 static void usbnet_tx_timeout (struct net_device *net)
1353 {
1354         struct usbnet           *dev = netdev_priv(net);
1355
1356         unlink_urbs (dev, &dev->txq);
1357         tasklet_schedule (&dev->bh);
1358
1359         // FIXME: device recovery -- reset?
1360 }
1361
1362 /*-------------------------------------------------------------------------*/
1363
1364 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
1365 {
1366         struct usbnet           *dev = netdev_priv(net);
1367         int                     length;
1368         int                     retval = NET_XMIT_SUCCESS;
1369         struct urb              *urb = NULL;
1370         struct skb_data         *entry;
1371         struct driver_info      *info = dev->driver_info;
1372         unsigned long           flags;
1373
1374         // some devices want funky USB-level framing, for
1375         // win32 driver (usually) and/or hardware quirks
1376         if (info->tx_fixup) {
1377                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1378                 if (!skb) {
1379                         if (netif_msg_tx_err (dev))
1380                                 devdbg (dev, "can't tx_fixup skb");
1381                         goto drop;
1382                 }
1383         }
1384         length = skb->len;
1385
1386         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1387                 if (netif_msg_tx_err (dev))
1388                         devdbg (dev, "no urb");
1389                 goto drop;
1390         }
1391
1392         entry = (struct skb_data *) skb->cb;
1393         entry->urb = urb;
1394         entry->dev = dev;
1395         entry->state = tx_start;
1396         entry->length = length;
1397
1398         usb_fill_bulk_urb (urb, dev->udev, dev->out,
1399                         skb->data, skb->len, tx_complete, skb);
1400
1401         /* don't assume the hardware handles USB_ZERO_PACKET
1402          * NOTE:  strictly conforming cdc-ether devices should expect
1403          * the ZLP here, but ignore the one-byte packet.
1404          *
1405          * FIXME zero that byte, if it doesn't require a new skb.
1406          */
1407         if ((length % dev->maxpacket) == 0)
1408                 urb->transfer_buffer_length++;
1409
1410         spin_lock_irqsave (&dev->txq.lock, flags);
1411
1412         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1413         case -EPIPE:
1414                 netif_stop_queue (net);
1415                 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1416                 break;
1417         default:
1418                 if (netif_msg_tx_err (dev))
1419                         devdbg (dev, "tx: submit urb err %d", retval);
1420                 break;
1421         case 0:
1422                 net->trans_start = jiffies;
1423                 __skb_queue_tail (&dev->txq, skb);
1424                 if (dev->txq.qlen >= TX_QLEN (dev))
1425                         netif_stop_queue (net);
1426         }
1427         spin_unlock_irqrestore (&dev->txq.lock, flags);
1428
1429         if (retval) {
1430                 if (netif_msg_tx_err (dev))
1431                         devdbg (dev, "drop, code %d", retval);
1432 drop:
1433                 retval = NET_XMIT_SUCCESS;
1434                 dev->stats.tx_dropped++;
1435                 if (skb)
1436                         dev_kfree_skb_any (skb);
1437                 usb_free_urb (urb);
1438         } else if (netif_msg_tx_queued (dev)) {
1439                 devdbg (dev, "> tx, len %d, type 0x%x",
1440                         length, skb->protocol);
1441         }
1442         return retval;
1443 }
1444
1445
1446 /*-------------------------------------------------------------------------*/
1447
1448 // tasklet (work deferred from completions, in_irq) or timer
1449
1450 static void usbnet_bh (unsigned long param)
1451 {
1452         struct usbnet           *dev = (struct usbnet *) param;
1453         struct sk_buff          *skb;
1454         struct skb_data         *entry;
1455
1456         while ((skb = skb_dequeue (&dev->done))) {
1457                 entry = (struct skb_data *) skb->cb;
1458                 switch (entry->state) {
1459                     case rx_done:
1460                         entry->state = rx_cleanup;
1461                         rx_process (dev, skb);
1462                         continue;
1463                     case tx_done:
1464                     case rx_cleanup:
1465                         usb_free_urb (entry->urb);
1466                         dev_kfree_skb (skb);
1467                         continue;
1468                     default:
1469                         devdbg (dev, "bogus skb state %d", entry->state);
1470                 }
1471         }
1472
1473         // waiting for all pending urbs to complete?
1474         if (dev->wait) {
1475                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1476                         wake_up (dev->wait);
1477                 }
1478
1479         // or are we maybe short a few urbs?
1480         } else if (netif_running (dev->net)
1481                         && netif_device_present (dev->net)
1482                         && !timer_pending (&dev->delay)
1483                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1484                 int     temp = dev->rxq.qlen;
1485                 int     qlen = RX_QLEN (dev);
1486
1487                 if (temp < qlen) {
1488                         struct urb      *urb;
1489                         int             i;
1490
1491                         // don't refill the queue all at once
1492                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1493                                 urb = usb_alloc_urb (0, GFP_ATOMIC);
1494                                 if (urb != NULL)
1495                                         rx_submit (dev, urb, GFP_ATOMIC);
1496                         }
1497                         if (temp != dev->rxq.qlen && netif_msg_link (dev))
1498                                 devdbg (dev, "rxqlen %d --> %d",
1499                                                 temp, dev->rxq.qlen);
1500                         if (dev->rxq.qlen < qlen)
1501                                 tasklet_schedule (&dev->bh);
1502                 }
1503                 if (dev->txq.qlen < TX_QLEN (dev))
1504                         netif_wake_queue (dev->net);
1505         }
1506 }
1507
1508
1509 \f
1510 /*-------------------------------------------------------------------------
1511  *
1512  * USB Device Driver support
1513  *
1514  *-------------------------------------------------------------------------*/
1515  
1516 // precondition: never called in_interrupt
1517
1518 void usbnet_disconnect (struct usb_interface *intf)
1519 {
1520         struct usbnet           *dev;
1521         struct usb_device       *xdev;
1522         struct net_device       *net;
1523
1524         dev = usb_get_intfdata(intf);
1525         usb_set_intfdata(intf, NULL);
1526         if (!dev)
1527                 return;
1528
1529         xdev = interface_to_usbdev (intf);
1530
1531         if (netif_msg_probe (dev))
1532                 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1533                         intf->dev.driver->name,
1534                         xdev->bus->bus_name, xdev->devpath,
1535                         dev->driver_info->description);
1536         
1537         net = dev->net;
1538         unregister_netdev (net);
1539
1540         /* we don't hold rtnl here ... */
1541         flush_scheduled_work ();
1542
1543         if (dev->driver_info->unbind)
1544                 dev->driver_info->unbind (dev, intf);
1545
1546         free_netdev(net);
1547         usb_put_dev (xdev);
1548 }
1549 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1550
1551
1552 /*-------------------------------------------------------------------------*/
1553
1554 // precondition: never called in_interrupt
1555
1556 int
1557 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1558 {
1559         struct usbnet                   *dev;
1560         struct net_device               *net;
1561         struct usb_host_interface       *interface;
1562         struct driver_info              *info;
1563         struct usb_device               *xdev;
1564         int                             status;
1565
1566         info = (struct driver_info *) prod->driver_info;
1567         if (!info) {
1568                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1569                 return -ENODEV;
1570         }
1571         xdev = interface_to_usbdev (udev);
1572         interface = udev->cur_altsetting;
1573
1574         usb_get_dev (xdev);
1575
1576         status = -ENOMEM;
1577
1578         // set up our own records
1579         net = alloc_etherdev(sizeof(*dev));
1580         if (!net) {
1581                 dbg ("can't kmalloc dev");
1582                 goto out;
1583         }
1584
1585         dev = netdev_priv(net);
1586         dev->udev = xdev;
1587         dev->driver_info = info;
1588         dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1589                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1590         skb_queue_head_init (&dev->rxq);
1591         skb_queue_head_init (&dev->txq);
1592         skb_queue_head_init (&dev->done);
1593         dev->bh.func = usbnet_bh;
1594         dev->bh.data = (unsigned long) dev;
1595         INIT_WORK (&dev->kevent, kevent, dev);
1596         dev->delay.function = usbnet_bh;
1597         dev->delay.data = (unsigned long) dev;
1598         init_timer (&dev->delay);
1599
1600         SET_MODULE_OWNER (net);
1601         dev->net = net;
1602         strcpy (net->name, "usb%d");
1603         memcpy (net->dev_addr, node_id, sizeof node_id);
1604
1605         /* rx and tx sides can use different message sizes;
1606          * bind() should set rx_urb_size in that case.
1607          */
1608         dev->hard_mtu = net->mtu + net->hard_header_len;
1609 #if 0
1610 // dma_supported() is deeply broken on almost all architectures
1611         // possible with some EHCI controllers
1612         if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1613                 net->features |= NETIF_F_HIGHDMA;
1614 #endif
1615
1616         net->change_mtu = usbnet_change_mtu;
1617         net->get_stats = usbnet_get_stats;
1618         net->hard_start_xmit = usbnet_start_xmit;
1619         net->open = usbnet_open;
1620         net->stop = usbnet_stop;
1621         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1622         net->tx_timeout = usbnet_tx_timeout;
1623         net->ethtool_ops = &usbnet_ethtool_ops;
1624
1625         // allow device-specific bind/init procedures
1626         // NOTE net->name still not usable ...
1627         if (info->bind) {
1628                 status = info->bind (dev, udev);
1629                 // heuristic:  "usb%d" for links we know are two-host,
1630                 // else "eth%d" when there's reasonable doubt.  userspace
1631                 // can rename the link if it knows better.
1632                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1633                                 && (net->dev_addr [0] & 0x02) == 0)
1634                         strcpy (net->name, "eth%d");
1635
1636                 /* maybe the remote can't receive an Ethernet MTU */
1637                 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1638                         net->mtu = dev->hard_mtu - net->hard_header_len;
1639         } else if (!info->in || !info->out)
1640                 status = usbnet_get_endpoints (dev, udev);
1641         else {
1642                 dev->in = usb_rcvbulkpipe (xdev, info->in);
1643                 dev->out = usb_sndbulkpipe (xdev, info->out);
1644                 if (!(info->flags & FLAG_NO_SETINT))
1645                         status = usb_set_interface (xdev,
1646                                 interface->desc.bInterfaceNumber,
1647                                 interface->desc.bAlternateSetting);
1648                 else
1649                         status = 0;
1650
1651         }
1652         if (status == 0 && dev->status)
1653                 status = init_status (dev, udev);
1654         if (status < 0)
1655                 goto out1;
1656
1657         if (!dev->rx_urb_size)
1658                 dev->rx_urb_size = dev->hard_mtu;
1659         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1660         
1661         SET_NETDEV_DEV(net, &udev->dev);
1662         status = register_netdev (net);
1663         if (status)
1664                 goto out3;
1665         if (netif_msg_probe (dev))
1666                 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1667                                 "%02x:%02x:%02x:%02x:%02x:%02x",
1668                         udev->dev.driver->name,
1669                         xdev->bus->bus_name, xdev->devpath,
1670                         dev->driver_info->description,
1671                         net->dev_addr [0], net->dev_addr [1],
1672                         net->dev_addr [2], net->dev_addr [3],
1673                         net->dev_addr [4], net->dev_addr [5]);
1674
1675         // ok, it's ready to go.
1676         usb_set_intfdata (udev, dev);
1677
1678         // start as if the link is up
1679         netif_device_attach (net);
1680
1681         return 0;
1682
1683 out3:
1684         if (info->unbind)
1685                 info->unbind (dev, udev);
1686 out1:
1687         free_netdev(net);
1688 out:
1689         usb_put_dev(xdev);
1690         return status;
1691 }
1692 EXPORT_SYMBOL_GPL(usbnet_probe);
1693
1694 /*-------------------------------------------------------------------------*/
1695
1696 /* FIXME these suspend/resume methods assume non-CDC style
1697  * devices, with only one interface.
1698  */
1699
1700 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1701 {
1702         struct usbnet           *dev = usb_get_intfdata(intf);
1703         
1704         /* accelerate emptying of the rx and queues, to avoid
1705          * having everything error out.
1706          */
1707         netif_device_detach (dev->net);
1708         (void) unlink_urbs (dev, &dev->rxq);
1709         (void) unlink_urbs (dev, &dev->txq);
1710         intf->dev.power.power_state = PMSG_SUSPEND;
1711         return 0;
1712 }
1713 EXPORT_SYMBOL_GPL(usbnet_suspend);
1714
1715 int usbnet_resume (struct usb_interface *intf)
1716 {
1717         struct usbnet           *dev = usb_get_intfdata(intf);
1718
1719         intf->dev.power.power_state = PMSG_ON;
1720         netif_device_attach (dev->net);
1721         tasklet_schedule (&dev->bh);
1722         return 0;
1723 }
1724 EXPORT_SYMBOL_GPL(usbnet_resume);
1725
1726
1727 /*-------------------------------------------------------------------------*/
1728
1729 #ifndef HAVE_HARDWARE
1730 #error You need to configure some hardware for this driver
1731 #endif
1732
1733 /*
1734  * chip vendor names won't normally be on the cables, and
1735  * may not be on the device.
1736  */
1737
1738 static const struct usb_device_id       products [] = {
1739
1740 #ifdef CONFIG_USB_PL2301
1741 {
1742         USB_DEVICE (0x067b, 0x0000),    // PL-2301
1743         .driver_info =  (unsigned long) &prolific_info,
1744 }, {
1745         USB_DEVICE (0x067b, 0x0001),    // PL-2302
1746         .driver_info =  (unsigned long) &prolific_info,
1747 },
1748 #endif
1749
1750 #ifdef  CONFIG_USB_RNDIS
1751 {
1752         /* RNDIS is MSFT's un-official variant of CDC ACM */
1753         USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
1754         .driver_info = (unsigned long) &rndis_info,
1755 },
1756 #endif
1757
1758 #ifdef  CONFIG_USB_CDCETHER
1759 {
1760         /* CDC Ether uses two interfaces, not necessarily consecutive.
1761          * We match the main interface, ignoring the optional device
1762          * class so we could handle devices that aren't exclusively
1763          * CDC ether.
1764          *
1765          * NOTE:  this match must come AFTER entries working around
1766          * bugs/quirks in a given product (like Zaurus, above).
1767          */
1768         USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
1769                         USB_CDC_PROTO_NONE),
1770         .driver_info = (unsigned long) &cdc_info,
1771 },
1772 #endif
1773
1774         { },            // END
1775 };
1776 MODULE_DEVICE_TABLE (usb, products);
1777
1778 static struct usb_driver usbnet_driver = {
1779         .owner =        THIS_MODULE,
1780         .name =         driver_name,
1781         .id_table =     products,
1782         .probe =        usbnet_probe,
1783         .disconnect =   usbnet_disconnect,
1784         .suspend =      usbnet_suspend,
1785         .resume =       usbnet_resume,
1786 };
1787
1788 /*-------------------------------------------------------------------------*/
1789
1790 static int __init usbnet_init(void)
1791 {
1792         // compiler should optimize these out
1793         BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1794                         < sizeof (struct skb_data));
1795 #ifdef  CONFIG_USB_CDCETHER
1796         BUG_ON ((sizeof (((struct usbnet *)0)->data)
1797                         < sizeof (struct cdc_state)));
1798 #endif
1799
1800         random_ether_addr(node_id);
1801
1802         return usb_register(&usbnet_driver);
1803 }
1804 module_init(usbnet_init);
1805
1806 static void __exit usbnet_exit(void)
1807 {
1808         usb_deregister(&usbnet_driver);
1809 }
1810 module_exit(usbnet_exit);
1811
1812 MODULE_AUTHOR("David Brownell");
1813 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
1814 MODULE_LICENSE("GPL");