[PATCH] USB: usbnet (8/9) module for RNDIS devices
[powerpc.git] / 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  *
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 // between wakeups
160 #define UNLINK_TIMEOUT_MS       3
161
162 /*-------------------------------------------------------------------------*/
163
164 // randomly generated ethernet address
165 static u8       node_id [ETH_ALEN];
166
167 static const char driver_name [] = "usbnet";
168
169 /* use ethtool to change the level for any given device */
170 static int msg_level = -1;
171 module_param (msg_level, int, 0);
172 MODULE_PARM_DESC (msg_level, "Override default message level");
173
174 /*-------------------------------------------------------------------------*/
175
176 /* handles CDC Ethernet and many other network "bulk data" interfaces */
177 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
178 {
179         int                             tmp;
180         struct usb_host_interface       *alt = NULL;
181         struct usb_host_endpoint        *in = NULL, *out = NULL;
182         struct usb_host_endpoint        *status = NULL;
183
184         for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
185                 unsigned        ep;
186
187                 in = out = status = NULL;
188                 alt = intf->altsetting + tmp;
189
190                 /* take the first altsetting with in-bulk + out-bulk;
191                  * remember any status endpoint, just in case;
192                  * ignore other endpoints and altsetttings.
193                  */
194                 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
195                         struct usb_host_endpoint        *e;
196                         int                             intr = 0;
197
198                         e = alt->endpoint + ep;
199                         switch (e->desc.bmAttributes) {
200                         case USB_ENDPOINT_XFER_INT:
201                                 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
202                                         continue;
203                                 intr = 1;
204                                 /* FALLTHROUGH */
205                         case USB_ENDPOINT_XFER_BULK:
206                                 break;
207                         default:
208                                 continue;
209                         }
210                         if (e->desc.bEndpointAddress & USB_DIR_IN) {
211                                 if (!intr && !in)
212                                         in = e;
213                                 else if (intr && !status)
214                                         status = e;
215                         } else {
216                                 if (!out)
217                                         out = e;
218                         }
219                 }
220                 if (in && out)
221                         break;
222         }
223         if (!alt || !in || !out)
224                 return -EINVAL;
225
226         if (alt->desc.bAlternateSetting != 0
227                         || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
228                 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
229                                 alt->desc.bAlternateSetting);
230                 if (tmp < 0)
231                         return tmp;
232         }
233         
234         dev->in = usb_rcvbulkpipe (dev->udev,
235                         in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
236         dev->out = usb_sndbulkpipe (dev->udev,
237                         out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
238         dev->status = status;
239         return 0;
240 }
241 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
242
243 static void intr_complete (struct urb *urb, struct pt_regs *regs);
244
245 static int init_status (struct usbnet *dev, struct usb_interface *intf)
246 {
247         char            *buf = NULL;
248         unsigned        pipe = 0;
249         unsigned        maxp;
250         unsigned        period;
251
252         if (!dev->driver_info->status)
253                 return 0;
254
255         pipe = usb_rcvintpipe (dev->udev,
256                         dev->status->desc.bEndpointAddress
257                                 & USB_ENDPOINT_NUMBER_MASK);
258         maxp = usb_maxpacket (dev->udev, pipe, 0);
259
260         /* avoid 1 msec chatter:  min 8 msec poll rate */
261         period = max ((int) dev->status->desc.bInterval,
262                 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
263
264         buf = kmalloc (maxp, SLAB_KERNEL);
265         if (buf) {
266                 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
267                 if (!dev->interrupt) {
268                         kfree (buf);
269                         return -ENOMEM;
270                 } else {
271                         usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
272                                 buf, maxp, intr_complete, dev, period);
273                         dev_dbg(&intf->dev,
274                                 "status ep%din, %d bytes period %d\n",
275                                 usb_pipeendpoint(pipe), maxp, period);
276                 }
277         }
278         return  0;
279 }
280
281 /* Passes this packet up the stack, updating its accounting.
282  * Some link protocols batch packets, so their rx_fixup paths
283  * can return clones as well as just modify the original skb.
284  */
285 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
286 {
287         int     status;
288
289         skb->dev = dev->net;
290         skb->protocol = eth_type_trans (skb, dev->net);
291         dev->stats.rx_packets++;
292         dev->stats.rx_bytes += skb->len;
293
294         if (netif_msg_rx_status (dev))
295                 devdbg (dev, "< rx, len %zu, type 0x%x",
296                         skb->len + sizeof (struct ethhdr), skb->protocol);
297         memset (skb->cb, 0, sizeof (struct skb_data));
298         status = netif_rx (skb);
299         if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
300                 devdbg (dev, "netif_rx status %d", status);
301 }
302 EXPORT_SYMBOL_GPL(usbnet_skb_return);
303
304 \f
305 #ifdef CONFIG_USB_PL2301
306 #define HAVE_HARDWARE
307
308 /*-------------------------------------------------------------------------
309  *
310  * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
311  *
312  * The protocol and handshaking used here should be bug-compatible
313  * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
314  *
315  *-------------------------------------------------------------------------*/
316
317 /*
318  * Bits 0-4 can be used for software handshaking; they're set from
319  * one end, cleared from the other, "read" with the interrupt byte.
320  */
321 #define PL_S_EN         (1<<7)          /* (feature only) suspend enable */
322 /* reserved bit -- rx ready (6) ? */
323 #define PL_TX_READY     (1<<5)          /* (interrupt only) transmit ready */
324 #define PL_RESET_OUT    (1<<4)          /* reset output pipe */
325 #define PL_RESET_IN     (1<<3)          /* reset input pipe */
326 #define PL_TX_C         (1<<2)          /* transmission complete */
327 #define PL_TX_REQ       (1<<1)          /* transmission received */
328 #define PL_PEER_E       (1<<0)          /* peer exists */
329
330 static inline int
331 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
332 {
333         return usb_control_msg (dev->udev,
334                 usb_rcvctrlpipe (dev->udev, 0),
335                 req,
336                 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
337                 val, index,
338                 NULL, 0,
339                 USB_CTRL_GET_TIMEOUT);
340 }
341
342 static inline int
343 pl_clear_QuickLink_features (struct usbnet *dev, int val)
344 {
345         return pl_vendor_req (dev, 1, (u8) val, 0);
346 }
347
348 static inline int
349 pl_set_QuickLink_features (struct usbnet *dev, int val)
350 {
351         return pl_vendor_req (dev, 3, (u8) val, 0);
352 }
353
354 /*-------------------------------------------------------------------------*/
355
356 static int pl_reset (struct usbnet *dev)
357 {
358         /* some units seem to need this reset, others reject it utterly.
359          * FIXME be more like "naplink" or windows drivers.
360          */
361         (void) pl_set_QuickLink_features (dev,
362                 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
363         return 0;
364 }
365
366 static const struct driver_info prolific_info = {
367         .description =  "Prolific PL-2301/PL-2302",
368         .flags =        FLAG_NO_SETINT,
369                 /* some PL-2302 versions seem to fail usb_set_interface() */
370         .reset =        pl_reset,
371 };
372
373 #endif /* CONFIG_USB_PL2301 */
374
375 \f
376 /*-------------------------------------------------------------------------
377  *
378  * Network Device Driver (peer link to "Host Device", from USB host)
379  *
380  *-------------------------------------------------------------------------*/
381
382 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
383 {
384         struct usbnet   *dev = netdev_priv(net);
385         int             ll_mtu = new_mtu + net->hard_header_len;
386
387         if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
388                 return -EINVAL;
389         // no second zero-length packet read wanted after mtu-sized packets
390         if ((ll_mtu % dev->maxpacket) == 0)
391                 return -EDOM;
392         net->mtu = new_mtu;
393         return 0;
394 }
395
396 /*-------------------------------------------------------------------------*/
397
398 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
399 {
400         struct usbnet   *dev = netdev_priv(net);
401         return &dev->stats;
402 }
403
404 /*-------------------------------------------------------------------------*/
405
406 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
407  * completion callbacks.  2.5 should have fixed those bugs...
408  */
409
410 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
411 {
412         unsigned long           flags;
413
414         spin_lock_irqsave(&list->lock, flags);
415         __skb_unlink(skb, list);
416         spin_unlock(&list->lock);
417         spin_lock(&dev->done.lock);
418         __skb_queue_tail(&dev->done, skb);
419         if (dev->done.qlen == 1)
420                 tasklet_schedule(&dev->bh);
421         spin_unlock_irqrestore(&dev->done.lock, flags);
422 }
423
424 /* some work can't be done in tasklets, so we use keventd
425  *
426  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
427  * but tasklet_schedule() doesn't.  hope the failure is rare.
428  */
429 void usbnet_defer_kevent (struct usbnet *dev, int work)
430 {
431         set_bit (work, &dev->flags);
432         if (!schedule_work (&dev->kevent))
433                 deverr (dev, "kevent %d may have been dropped", work);
434         else
435                 devdbg (dev, "kevent %d scheduled", work);
436 }
437 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
438
439 /*-------------------------------------------------------------------------*/
440
441 static void rx_complete (struct urb *urb, struct pt_regs *regs);
442
443 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
444 {
445         struct sk_buff          *skb;
446         struct skb_data         *entry;
447         int                     retval = 0;
448         unsigned long           lockflags;
449         size_t                  size = dev->rx_urb_size;
450
451         if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
452                 if (netif_msg_rx_err (dev))
453                         devdbg (dev, "no rx skb");
454                 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
455                 usb_free_urb (urb);
456                 return;
457         }
458         skb_reserve (skb, NET_IP_ALIGN);
459
460         entry = (struct skb_data *) skb->cb;
461         entry->urb = urb;
462         entry->dev = dev;
463         entry->state = rx_start;
464         entry->length = 0;
465
466         usb_fill_bulk_urb (urb, dev->udev, dev->in,
467                 skb->data, size, rx_complete, skb);
468
469         spin_lock_irqsave (&dev->rxq.lock, lockflags);
470
471         if (netif_running (dev->net)
472                         && netif_device_present (dev->net)
473                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
474                 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 
475                 case -EPIPE:
476                         usbnet_defer_kevent (dev, EVENT_RX_HALT);
477                         break;
478                 case -ENOMEM:
479                         usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
480                         break;
481                 case -ENODEV:
482                         if (netif_msg_ifdown (dev))
483                                 devdbg (dev, "device gone");
484                         netif_device_detach (dev->net);
485                         break;
486                 default:
487                         if (netif_msg_rx_err (dev))
488                                 devdbg (dev, "rx submit, %d", retval);
489                         tasklet_schedule (&dev->bh);
490                         break;
491                 case 0:
492                         __skb_queue_tail (&dev->rxq, skb);
493                 }
494         } else {
495                 if (netif_msg_ifdown (dev))
496                         devdbg (dev, "rx: stopped");
497                 retval = -ENOLINK;
498         }
499         spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
500         if (retval) {
501                 dev_kfree_skb_any (skb);
502                 usb_free_urb (urb);
503         }
504 }
505
506
507 /*-------------------------------------------------------------------------*/
508
509 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
510 {
511         if (dev->driver_info->rx_fixup
512                         && !dev->driver_info->rx_fixup (dev, skb))
513                 goto error;
514         // else network stack removes extra byte if we forced a short packet
515
516         if (skb->len)
517                 usbnet_skb_return (dev, skb);
518         else {
519                 if (netif_msg_rx_err (dev))
520                         devdbg (dev, "drop");
521 error:
522                 dev->stats.rx_errors++;
523                 skb_queue_tail (&dev->done, skb);
524         }
525 }
526
527 /*-------------------------------------------------------------------------*/
528
529 static void rx_complete (struct urb *urb, struct pt_regs *regs)
530 {
531         struct sk_buff          *skb = (struct sk_buff *) urb->context;
532         struct skb_data         *entry = (struct skb_data *) skb->cb;
533         struct usbnet           *dev = entry->dev;
534         int                     urb_status = urb->status;
535
536         skb_put (skb, urb->actual_length);
537         entry->state = rx_done;
538         entry->urb = NULL;
539
540         switch (urb_status) {
541             // success
542             case 0:
543                 if (skb->len < dev->net->hard_header_len) {
544                         entry->state = rx_cleanup;
545                         dev->stats.rx_errors++;
546                         dev->stats.rx_length_errors++;
547                         if (netif_msg_rx_err (dev))
548                                 devdbg (dev, "rx length %d", skb->len);
549                 }
550                 break;
551
552             // stalls need manual reset. this is rare ... except that
553             // when going through USB 2.0 TTs, unplug appears this way.
554             // we avoid the highspeed version of the ETIMEOUT/EILSEQ
555             // storm, recovering as needed.
556             case -EPIPE:
557                 dev->stats.rx_errors++;
558                 usbnet_defer_kevent (dev, EVENT_RX_HALT);
559                 // FALLTHROUGH
560
561             // software-driven interface shutdown
562             case -ECONNRESET:           // async unlink
563             case -ESHUTDOWN:            // hardware gone
564                 if (netif_msg_ifdown (dev))
565                         devdbg (dev, "rx shutdown, code %d", urb_status);
566                 goto block;
567
568             // we get controller i/o faults during khubd disconnect() delays.
569             // throttle down resubmits, to avoid log floods; just temporarily,
570             // so we still recover when the fault isn't a khubd delay.
571             case -EPROTO:               // ehci
572             case -ETIMEDOUT:            // ohci
573             case -EILSEQ:               // uhci
574                 dev->stats.rx_errors++;
575                 if (!timer_pending (&dev->delay)) {
576                         mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
577                         if (netif_msg_link (dev))
578                                 devdbg (dev, "rx throttle %d", urb_status);
579                 }
580 block:
581                 entry->state = rx_cleanup;
582                 entry->urb = urb;
583                 urb = NULL;
584                 break;
585
586             // data overrun ... flush fifo?
587             case -EOVERFLOW:
588                 dev->stats.rx_over_errors++;
589                 // FALLTHROUGH
590             
591             default:
592                 entry->state = rx_cleanup;
593                 dev->stats.rx_errors++;
594                 if (netif_msg_rx_err (dev))
595                         devdbg (dev, "rx status %d", urb_status);
596                 break;
597         }
598
599         defer_bh(dev, skb, &dev->rxq);
600
601         if (urb) {
602                 if (netif_running (dev->net)
603                                 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
604                         rx_submit (dev, urb, GFP_ATOMIC);
605                         return;
606                 }
607                 usb_free_urb (urb);
608         }
609         if (netif_msg_rx_err (dev))
610                 devdbg (dev, "no read resubmitted");
611 }
612
613 static void intr_complete (struct urb *urb, struct pt_regs *regs)
614 {
615         struct usbnet   *dev = urb->context;
616         int             status = urb->status;
617
618         switch (status) {
619             /* success */
620             case 0:
621                 dev->driver_info->status(dev, urb);
622                 break;
623
624             /* software-driven interface shutdown */
625             case -ENOENT:               // urb killed
626             case -ESHUTDOWN:            // hardware gone
627                 if (netif_msg_ifdown (dev))
628                         devdbg (dev, "intr shutdown, code %d", status);
629                 return;
630
631             /* NOTE:  not throttling like RX/TX, since this endpoint
632              * already polls infrequently
633              */
634             default:
635                 devdbg (dev, "intr status %d", status);
636                 break;
637         }
638
639         if (!netif_running (dev->net))
640                 return;
641
642         memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
643         status = usb_submit_urb (urb, GFP_ATOMIC);
644         if (status != 0 && netif_msg_timer (dev))
645                 deverr(dev, "intr resubmit --> %d", status);
646 }
647
648 /*-------------------------------------------------------------------------*/
649
650 // unlink pending rx/tx; completion handlers do all other cleanup
651
652 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
653 {
654         unsigned long           flags;
655         struct sk_buff          *skb, *skbnext;
656         int                     count = 0;
657
658         spin_lock_irqsave (&q->lock, flags);
659         for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
660                 struct skb_data         *entry;
661                 struct urb              *urb;
662                 int                     retval;
663
664                 entry = (struct skb_data *) skb->cb;
665                 urb = entry->urb;
666                 skbnext = skb->next;
667
668                 // during some PM-driven resume scenarios,
669                 // these (async) unlinks complete immediately
670                 retval = usb_unlink_urb (urb);
671                 if (retval != -EINPROGRESS && retval != 0)
672                         devdbg (dev, "unlink urb err, %d", retval);
673                 else
674                         count++;
675         }
676         spin_unlock_irqrestore (&q->lock, flags);
677         return count;
678 }
679
680
681 /*-------------------------------------------------------------------------*/
682
683 // precondition: never called in_interrupt
684
685 static int usbnet_stop (struct net_device *net)
686 {
687         struct usbnet           *dev = netdev_priv(net);
688         int                     temp;
689         DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 
690         DECLARE_WAITQUEUE (wait, current);
691
692         netif_stop_queue (net);
693
694         if (netif_msg_ifdown (dev))
695                 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
696                         dev->stats.rx_packets, dev->stats.tx_packets, 
697                         dev->stats.rx_errors, dev->stats.tx_errors
698                         );
699
700         // ensure there are no more active urbs
701         add_wait_queue (&unlink_wakeup, &wait);
702         dev->wait = &unlink_wakeup;
703         temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
704
705         // maybe wait for deletions to finish.
706         while (!skb_queue_empty(&dev->rxq) &&
707                !skb_queue_empty(&dev->txq) &&
708                !skb_queue_empty(&dev->done)) {
709                 msleep(UNLINK_TIMEOUT_MS);
710                 if (netif_msg_ifdown (dev))
711                         devdbg (dev, "waited for %d urb completions", temp);
712         }
713         dev->wait = NULL;
714         remove_wait_queue (&unlink_wakeup, &wait); 
715
716         usb_kill_urb(dev->interrupt);
717
718         /* deferred work (task, timer, softirq) must also stop.
719          * can't flush_scheduled_work() until we drop rtnl (later),
720          * else workers could deadlock; so make workers a NOP.
721          */
722         dev->flags = 0;
723         del_timer_sync (&dev->delay);
724         tasklet_kill (&dev->bh);
725
726         return 0;
727 }
728
729 /*-------------------------------------------------------------------------*/
730
731 // posts reads, and enables write queuing
732
733 // precondition: never called in_interrupt
734
735 static int usbnet_open (struct net_device *net)
736 {
737         struct usbnet           *dev = netdev_priv(net);
738         int                     retval = 0;
739         struct driver_info      *info = dev->driver_info;
740
741         // put into "known safe" state
742         if (info->reset && (retval = info->reset (dev)) < 0) {
743                 if (netif_msg_ifup (dev))
744                         devinfo (dev,
745                                 "open reset fail (%d) usbnet usb-%s-%s, %s",
746                                 retval,
747                                 dev->udev->bus->bus_name, dev->udev->devpath,
748                         info->description);
749                 goto done;
750         }
751
752         // insist peer be connected
753         if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
754                 if (netif_msg_ifup (dev))
755                         devdbg (dev, "can't open; %d", retval);
756                 goto done;
757         }
758
759         /* start any status interrupt transfer */
760         if (dev->interrupt) {
761                 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
762                 if (retval < 0) {
763                         if (netif_msg_ifup (dev))
764                                 deverr (dev, "intr submit %d", retval);
765                         goto done;
766                 }
767         }
768
769         netif_start_queue (net);
770         if (netif_msg_ifup (dev)) {
771                 char    *framing;
772
773                 if (dev->driver_info->flags & FLAG_FRAMING_NC)
774                         framing = "NetChip";
775                 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
776                         framing = "GeneSys";
777                 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
778                         framing = "Zaurus";
779                 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
780                         framing = "RNDIS";
781                 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
782                         framing = "ASIX";
783                 else
784                         framing = "simple";
785
786                 devinfo (dev, "open: enable queueing "
787                                 "(rx %d, tx %d) mtu %d %s framing",
788                         RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
789                         framing);
790         }
791
792         // delay posting reads until we're fully open
793         tasklet_schedule (&dev->bh);
794 done:
795         return retval;
796 }
797
798 /*-------------------------------------------------------------------------*/
799
800 /* ethtool methods; minidrivers may need to add some more, but
801  * they'll probably want to use this base set.
802  */
803
804 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
805 {
806         struct usbnet *dev = netdev_priv(net);
807
808         /* REVISIT don't always return "usbnet" */
809         strncpy (info->driver, driver_name, sizeof info->driver);
810         strncpy (info->version, DRIVER_VERSION, sizeof info->version);
811         strncpy (info->fw_version, dev->driver_info->description,
812                 sizeof info->fw_version);
813         usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
814 }
815 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
816
817 static u32 usbnet_get_link (struct net_device *net)
818 {
819         struct usbnet *dev = netdev_priv(net);
820
821         /* If a check_connect is defined, return its result */
822         if (dev->driver_info->check_connect)
823                 return dev->driver_info->check_connect (dev) == 0;
824
825         /* Otherwise, say we're up (to avoid breaking scripts) */
826         return 1;
827 }
828
829 u32 usbnet_get_msglevel (struct net_device *net)
830 {
831         struct usbnet *dev = netdev_priv(net);
832
833         return dev->msg_enable;
834 }
835 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
836
837 void usbnet_set_msglevel (struct net_device *net, u32 level)
838 {
839         struct usbnet *dev = netdev_priv(net);
840
841         dev->msg_enable = level;
842 }
843 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
844
845 /* drivers may override default ethtool_ops in their bind() routine */
846 static struct ethtool_ops usbnet_ethtool_ops = {
847         .get_drvinfo            = usbnet_get_drvinfo,
848         .get_link               = usbnet_get_link,
849         .get_msglevel           = usbnet_get_msglevel,
850         .set_msglevel           = usbnet_set_msglevel,
851 };
852
853 /*-------------------------------------------------------------------------*/
854
855 /* work that cannot be done in interrupt context uses keventd.
856  *
857  * NOTE:  with 2.5 we could do more of this using completion callbacks,
858  * especially now that control transfers can be queued.
859  */
860 static void
861 kevent (void *data)
862 {
863         struct usbnet           *dev = data;
864         int                     status;
865
866         /* usb_clear_halt() needs a thread context */
867         if (test_bit (EVENT_TX_HALT, &dev->flags)) {
868                 unlink_urbs (dev, &dev->txq);
869                 status = usb_clear_halt (dev->udev, dev->out);
870                 if (status < 0
871                                 && status != -EPIPE
872                                 && status != -ESHUTDOWN) {
873                         if (netif_msg_tx_err (dev))
874                                 deverr (dev, "can't clear tx halt, status %d",
875                                         status);
876                 } else {
877                         clear_bit (EVENT_TX_HALT, &dev->flags);
878                         if (status != -ESHUTDOWN)
879                                 netif_wake_queue (dev->net);
880                 }
881         }
882         if (test_bit (EVENT_RX_HALT, &dev->flags)) {
883                 unlink_urbs (dev, &dev->rxq);
884                 status = usb_clear_halt (dev->udev, dev->in);
885                 if (status < 0
886                                 && status != -EPIPE
887                                 && status != -ESHUTDOWN) {
888                         if (netif_msg_rx_err (dev))
889                                 deverr (dev, "can't clear rx halt, status %d",
890                                         status);
891                 } else {
892                         clear_bit (EVENT_RX_HALT, &dev->flags);
893                         tasklet_schedule (&dev->bh);
894                 }
895         }
896
897         /* tasklet could resubmit itself forever if memory is tight */
898         if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
899                 struct urb      *urb = NULL;
900
901                 if (netif_running (dev->net))
902                         urb = usb_alloc_urb (0, GFP_KERNEL);
903                 else
904                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
905                 if (urb != NULL) {
906                         clear_bit (EVENT_RX_MEMORY, &dev->flags);
907                         rx_submit (dev, urb, GFP_KERNEL);
908                         tasklet_schedule (&dev->bh);
909                 }
910         }
911
912         if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
913                 struct driver_info      *info = dev->driver_info;
914                 int                     retval = 0;
915
916                 clear_bit (EVENT_LINK_RESET, &dev->flags);
917                 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
918                         devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
919                                 retval,
920                                 dev->udev->bus->bus_name, dev->udev->devpath,
921                                 info->description);
922                 }
923         }
924
925         if (dev->flags)
926                 devdbg (dev, "kevent done, flags = 0x%lx",
927                         dev->flags);
928 }
929
930 /*-------------------------------------------------------------------------*/
931
932 static void tx_complete (struct urb *urb, struct pt_regs *regs)
933 {
934         struct sk_buff          *skb = (struct sk_buff *) urb->context;
935         struct skb_data         *entry = (struct skb_data *) skb->cb;
936         struct usbnet           *dev = entry->dev;
937
938         if (urb->status == 0) {
939                 dev->stats.tx_packets++;
940                 dev->stats.tx_bytes += entry->length;
941         } else {
942                 dev->stats.tx_errors++;
943
944                 switch (urb->status) {
945                 case -EPIPE:
946                         usbnet_defer_kevent (dev, EVENT_TX_HALT);
947                         break;
948
949                 /* software-driven interface shutdown */
950                 case -ECONNRESET:               // async unlink
951                 case -ESHUTDOWN:                // hardware gone
952                         break;
953
954                 // like rx, tx gets controller i/o faults during khubd delays
955                 // and so it uses the same throttling mechanism.
956                 case -EPROTO:           // ehci
957                 case -ETIMEDOUT:        // ohci
958                 case -EILSEQ:           // uhci
959                         if (!timer_pending (&dev->delay)) {
960                                 mod_timer (&dev->delay,
961                                         jiffies + THROTTLE_JIFFIES);
962                                 if (netif_msg_link (dev))
963                                         devdbg (dev, "tx throttle %d",
964                                                         urb->status);
965                         }
966                         netif_stop_queue (dev->net);
967                         break;
968                 default:
969                         if (netif_msg_tx_err (dev))
970                                 devdbg (dev, "tx err %d", entry->urb->status);
971                         break;
972                 }
973         }
974
975         urb->dev = NULL;
976         entry->state = tx_done;
977         defer_bh(dev, skb, &dev->txq);
978 }
979
980 /*-------------------------------------------------------------------------*/
981
982 static void usbnet_tx_timeout (struct net_device *net)
983 {
984         struct usbnet           *dev = netdev_priv(net);
985
986         unlink_urbs (dev, &dev->txq);
987         tasklet_schedule (&dev->bh);
988
989         // FIXME: device recovery -- reset?
990 }
991
992 /*-------------------------------------------------------------------------*/
993
994 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
995 {
996         struct usbnet           *dev = netdev_priv(net);
997         int                     length;
998         int                     retval = NET_XMIT_SUCCESS;
999         struct urb              *urb = NULL;
1000         struct skb_data         *entry;
1001         struct driver_info      *info = dev->driver_info;
1002         unsigned long           flags;
1003
1004         // some devices want funky USB-level framing, for
1005         // win32 driver (usually) and/or hardware quirks
1006         if (info->tx_fixup) {
1007                 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1008                 if (!skb) {
1009                         if (netif_msg_tx_err (dev))
1010                                 devdbg (dev, "can't tx_fixup skb");
1011                         goto drop;
1012                 }
1013         }
1014         length = skb->len;
1015
1016         if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1017                 if (netif_msg_tx_err (dev))
1018                         devdbg (dev, "no urb");
1019                 goto drop;
1020         }
1021
1022         entry = (struct skb_data *) skb->cb;
1023         entry->urb = urb;
1024         entry->dev = dev;
1025         entry->state = tx_start;
1026         entry->length = length;
1027
1028         usb_fill_bulk_urb (urb, dev->udev, dev->out,
1029                         skb->data, skb->len, tx_complete, skb);
1030
1031         /* don't assume the hardware handles USB_ZERO_PACKET
1032          * NOTE:  strictly conforming cdc-ether devices should expect
1033          * the ZLP here, but ignore the one-byte packet.
1034          *
1035          * FIXME zero that byte, if it doesn't require a new skb.
1036          */
1037         if ((length % dev->maxpacket) == 0)
1038                 urb->transfer_buffer_length++;
1039
1040         spin_lock_irqsave (&dev->txq.lock, flags);
1041
1042         switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1043         case -EPIPE:
1044                 netif_stop_queue (net);
1045                 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1046                 break;
1047         default:
1048                 if (netif_msg_tx_err (dev))
1049                         devdbg (dev, "tx: submit urb err %d", retval);
1050                 break;
1051         case 0:
1052                 net->trans_start = jiffies;
1053                 __skb_queue_tail (&dev->txq, skb);
1054                 if (dev->txq.qlen >= TX_QLEN (dev))
1055                         netif_stop_queue (net);
1056         }
1057         spin_unlock_irqrestore (&dev->txq.lock, flags);
1058
1059         if (retval) {
1060                 if (netif_msg_tx_err (dev))
1061                         devdbg (dev, "drop, code %d", retval);
1062 drop:
1063                 retval = NET_XMIT_SUCCESS;
1064                 dev->stats.tx_dropped++;
1065                 if (skb)
1066                         dev_kfree_skb_any (skb);
1067                 usb_free_urb (urb);
1068         } else if (netif_msg_tx_queued (dev)) {
1069                 devdbg (dev, "> tx, len %d, type 0x%x",
1070                         length, skb->protocol);
1071         }
1072         return retval;
1073 }
1074
1075
1076 /*-------------------------------------------------------------------------*/
1077
1078 // tasklet (work deferred from completions, in_irq) or timer
1079
1080 static void usbnet_bh (unsigned long param)
1081 {
1082         struct usbnet           *dev = (struct usbnet *) param;
1083         struct sk_buff          *skb;
1084         struct skb_data         *entry;
1085
1086         while ((skb = skb_dequeue (&dev->done))) {
1087                 entry = (struct skb_data *) skb->cb;
1088                 switch (entry->state) {
1089                     case rx_done:
1090                         entry->state = rx_cleanup;
1091                         rx_process (dev, skb);
1092                         continue;
1093                     case tx_done:
1094                     case rx_cleanup:
1095                         usb_free_urb (entry->urb);
1096                         dev_kfree_skb (skb);
1097                         continue;
1098                     default:
1099                         devdbg (dev, "bogus skb state %d", entry->state);
1100                 }
1101         }
1102
1103         // waiting for all pending urbs to complete?
1104         if (dev->wait) {
1105                 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1106                         wake_up (dev->wait);
1107                 }
1108
1109         // or are we maybe short a few urbs?
1110         } else if (netif_running (dev->net)
1111                         && netif_device_present (dev->net)
1112                         && !timer_pending (&dev->delay)
1113                         && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1114                 int     temp = dev->rxq.qlen;
1115                 int     qlen = RX_QLEN (dev);
1116
1117                 if (temp < qlen) {
1118                         struct urb      *urb;
1119                         int             i;
1120
1121                         // don't refill the queue all at once
1122                         for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1123                                 urb = usb_alloc_urb (0, GFP_ATOMIC);
1124                                 if (urb != NULL)
1125                                         rx_submit (dev, urb, GFP_ATOMIC);
1126                         }
1127                         if (temp != dev->rxq.qlen && netif_msg_link (dev))
1128                                 devdbg (dev, "rxqlen %d --> %d",
1129                                                 temp, dev->rxq.qlen);
1130                         if (dev->rxq.qlen < qlen)
1131                                 tasklet_schedule (&dev->bh);
1132                 }
1133                 if (dev->txq.qlen < TX_QLEN (dev))
1134                         netif_wake_queue (dev->net);
1135         }
1136 }
1137
1138
1139 \f
1140 /*-------------------------------------------------------------------------
1141  *
1142  * USB Device Driver support
1143  *
1144  *-------------------------------------------------------------------------*/
1145  
1146 // precondition: never called in_interrupt
1147
1148 void usbnet_disconnect (struct usb_interface *intf)
1149 {
1150         struct usbnet           *dev;
1151         struct usb_device       *xdev;
1152         struct net_device       *net;
1153
1154         dev = usb_get_intfdata(intf);
1155         usb_set_intfdata(intf, NULL);
1156         if (!dev)
1157                 return;
1158
1159         xdev = interface_to_usbdev (intf);
1160
1161         if (netif_msg_probe (dev))
1162                 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1163                         intf->dev.driver->name,
1164                         xdev->bus->bus_name, xdev->devpath,
1165                         dev->driver_info->description);
1166         
1167         net = dev->net;
1168         unregister_netdev (net);
1169
1170         /* we don't hold rtnl here ... */
1171         flush_scheduled_work ();
1172
1173         if (dev->driver_info->unbind)
1174                 dev->driver_info->unbind (dev, intf);
1175
1176         free_netdev(net);
1177         usb_put_dev (xdev);
1178 }
1179 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1180
1181
1182 /*-------------------------------------------------------------------------*/
1183
1184 // precondition: never called in_interrupt
1185
1186 int
1187 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1188 {
1189         struct usbnet                   *dev;
1190         struct net_device               *net;
1191         struct usb_host_interface       *interface;
1192         struct driver_info              *info;
1193         struct usb_device               *xdev;
1194         int                             status;
1195
1196         info = (struct driver_info *) prod->driver_info;
1197         if (!info) {
1198                 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1199                 return -ENODEV;
1200         }
1201         xdev = interface_to_usbdev (udev);
1202         interface = udev->cur_altsetting;
1203
1204         usb_get_dev (xdev);
1205
1206         status = -ENOMEM;
1207
1208         // set up our own records
1209         net = alloc_etherdev(sizeof(*dev));
1210         if (!net) {
1211                 dbg ("can't kmalloc dev");
1212                 goto out;
1213         }
1214
1215         dev = netdev_priv(net);
1216         dev->udev = xdev;
1217         dev->driver_info = info;
1218         dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1219                                 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1220         skb_queue_head_init (&dev->rxq);
1221         skb_queue_head_init (&dev->txq);
1222         skb_queue_head_init (&dev->done);
1223         dev->bh.func = usbnet_bh;
1224         dev->bh.data = (unsigned long) dev;
1225         INIT_WORK (&dev->kevent, kevent, dev);
1226         dev->delay.function = usbnet_bh;
1227         dev->delay.data = (unsigned long) dev;
1228         init_timer (&dev->delay);
1229
1230         SET_MODULE_OWNER (net);
1231         dev->net = net;
1232         strcpy (net->name, "usb%d");
1233         memcpy (net->dev_addr, node_id, sizeof node_id);
1234
1235         /* rx and tx sides can use different message sizes;
1236          * bind() should set rx_urb_size in that case.
1237          */
1238         dev->hard_mtu = net->mtu + net->hard_header_len;
1239 #if 0
1240 // dma_supported() is deeply broken on almost all architectures
1241         // possible with some EHCI controllers
1242         if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1243                 net->features |= NETIF_F_HIGHDMA;
1244 #endif
1245
1246         net->change_mtu = usbnet_change_mtu;
1247         net->get_stats = usbnet_get_stats;
1248         net->hard_start_xmit = usbnet_start_xmit;
1249         net->open = usbnet_open;
1250         net->stop = usbnet_stop;
1251         net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1252         net->tx_timeout = usbnet_tx_timeout;
1253         net->ethtool_ops = &usbnet_ethtool_ops;
1254
1255         // allow device-specific bind/init procedures
1256         // NOTE net->name still not usable ...
1257         if (info->bind) {
1258                 status = info->bind (dev, udev);
1259                 // heuristic:  "usb%d" for links we know are two-host,
1260                 // else "eth%d" when there's reasonable doubt.  userspace
1261                 // can rename the link if it knows better.
1262                 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1263                                 && (net->dev_addr [0] & 0x02) == 0)
1264                         strcpy (net->name, "eth%d");
1265
1266                 /* maybe the remote can't receive an Ethernet MTU */
1267                 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1268                         net->mtu = dev->hard_mtu - net->hard_header_len;
1269         } else if (!info->in || !info->out)
1270                 status = usbnet_get_endpoints (dev, udev);
1271         else {
1272                 dev->in = usb_rcvbulkpipe (xdev, info->in);
1273                 dev->out = usb_sndbulkpipe (xdev, info->out);
1274                 if (!(info->flags & FLAG_NO_SETINT))
1275                         status = usb_set_interface (xdev,
1276                                 interface->desc.bInterfaceNumber,
1277                                 interface->desc.bAlternateSetting);
1278                 else
1279                         status = 0;
1280
1281         }
1282         if (status == 0 && dev->status)
1283                 status = init_status (dev, udev);
1284         if (status < 0)
1285                 goto out1;
1286
1287         if (!dev->rx_urb_size)
1288                 dev->rx_urb_size = dev->hard_mtu;
1289         dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1290         
1291         SET_NETDEV_DEV(net, &udev->dev);
1292         status = register_netdev (net);
1293         if (status)
1294                 goto out3;
1295         if (netif_msg_probe (dev))
1296                 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1297                                 "%02x:%02x:%02x:%02x:%02x:%02x",
1298                         udev->dev.driver->name,
1299                         xdev->bus->bus_name, xdev->devpath,
1300                         dev->driver_info->description,
1301                         net->dev_addr [0], net->dev_addr [1],
1302                         net->dev_addr [2], net->dev_addr [3],
1303                         net->dev_addr [4], net->dev_addr [5]);
1304
1305         // ok, it's ready to go.
1306         usb_set_intfdata (udev, dev);
1307
1308         // start as if the link is up
1309         netif_device_attach (net);
1310
1311         return 0;
1312
1313 out3:
1314         if (info->unbind)
1315                 info->unbind (dev, udev);
1316 out1:
1317         free_netdev(net);
1318 out:
1319         usb_put_dev(xdev);
1320         return status;
1321 }
1322 EXPORT_SYMBOL_GPL(usbnet_probe);
1323
1324 /*-------------------------------------------------------------------------*/
1325
1326 /* FIXME these suspend/resume methods assume non-CDC style
1327  * devices, with only one interface.
1328  */
1329
1330 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1331 {
1332         struct usbnet           *dev = usb_get_intfdata(intf);
1333         
1334         /* accelerate emptying of the rx and queues, to avoid
1335          * having everything error out.
1336          */
1337         netif_device_detach (dev->net);
1338         (void) unlink_urbs (dev, &dev->rxq);
1339         (void) unlink_urbs (dev, &dev->txq);
1340         intf->dev.power.power_state = PMSG_SUSPEND;
1341         return 0;
1342 }
1343 EXPORT_SYMBOL_GPL(usbnet_suspend);
1344
1345 int usbnet_resume (struct usb_interface *intf)
1346 {
1347         struct usbnet           *dev = usb_get_intfdata(intf);
1348
1349         intf->dev.power.power_state = PMSG_ON;
1350         netif_device_attach (dev->net);
1351         tasklet_schedule (&dev->bh);
1352         return 0;
1353 }
1354 EXPORT_SYMBOL_GPL(usbnet_resume);
1355
1356
1357 /*-------------------------------------------------------------------------*/
1358
1359 #ifndef HAVE_HARDWARE
1360 #error You need to configure some hardware for this driver
1361 #endif
1362
1363 /*
1364  * chip vendor names won't normally be on the cables, and
1365  * may not be on the device.
1366  */
1367
1368 static const struct usb_device_id       products [] = {
1369
1370 #ifdef CONFIG_USB_PL2301
1371 {
1372         USB_DEVICE (0x067b, 0x0000),    // PL-2301
1373         .driver_info =  (unsigned long) &prolific_info,
1374 }, {
1375         USB_DEVICE (0x067b, 0x0001),    // PL-2302
1376         .driver_info =  (unsigned long) &prolific_info,
1377 },
1378 #endif
1379         { },            // END
1380 };
1381 MODULE_DEVICE_TABLE (usb, products);
1382
1383 static struct usb_driver usbnet_driver = {
1384         .owner =        THIS_MODULE,
1385         .name =         driver_name,
1386         .id_table =     products,
1387         .probe =        usbnet_probe,
1388         .disconnect =   usbnet_disconnect,
1389         .suspend =      usbnet_suspend,
1390         .resume =       usbnet_resume,
1391 };
1392
1393 /*-------------------------------------------------------------------------*/
1394
1395 static int __init usbnet_init(void)
1396 {
1397         // compiler should optimize these out
1398         BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1399                         < sizeof (struct skb_data));
1400
1401         random_ether_addr(node_id);
1402
1403         return usb_register(&usbnet_driver);
1404 }
1405 module_init(usbnet_init);
1406
1407 static void __exit usbnet_exit(void)
1408 {
1409         usb_deregister(&usbnet_driver);
1410 }
1411 module_exit(usbnet_exit);
1412
1413 MODULE_AUTHOR("David Brownell");
1414 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
1415 MODULE_LICENSE("GPL");