3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
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.
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.
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
22 * This is a generic "USB networking" framework that works with several
23 * kinds of full and high speed networking devices:
25 * + USB host-to-host "network cables", used for IP-over-USB links.
26 * These are often used for Laplink style connectivity products.
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
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
42 * + Adapters to networks such as Ethernet.
43 * - AX8817X based USB 2.0 products
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.
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
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.
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.
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)
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
108 * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
110 * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis)
111 *-------------------------------------------------------------------------*/
113 // #define DEBUG // error path messages, extra info
114 // #define VERBOSE // more; success messages
116 #include <linux/config.h>
117 #ifdef CONFIG_USB_DEBUG
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>
132 #define DRIVER_VERSION "22-Aug-2005"
135 /*-------------------------------------------------------------------------*/
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).
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.
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)
151 // reawaken network queue this soon after stopping; else watchdog barks
152 #define TX_TIMEOUT_JIFFIES (5*HZ)
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)
159 #define UNLINK_TIMEOUT_MS 3
161 /*-------------------------------------------------------------------------*/
163 // randomly generated ethernet address
164 static u8 node_id [ETH_ALEN];
166 static const char driver_name [] = "usbnet";
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");
173 /*-------------------------------------------------------------------------*/
175 /* handles CDC Ethernet and many other network "bulk data" interfaces */
176 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
179 struct usb_host_interface *alt = NULL;
180 struct usb_host_endpoint *in = NULL, *out = NULL;
181 struct usb_host_endpoint *status = NULL;
183 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
186 in = out = status = NULL;
187 alt = intf->altsetting + tmp;
189 /* take the first altsetting with in-bulk + out-bulk;
190 * remember any status endpoint, just in case;
191 * ignore other endpoints and altsetttings.
193 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
194 struct usb_host_endpoint *e;
197 e = alt->endpoint + ep;
198 switch (e->desc.bmAttributes) {
199 case USB_ENDPOINT_XFER_INT:
200 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
204 case USB_ENDPOINT_XFER_BULK:
209 if (e->desc.bEndpointAddress & USB_DIR_IN) {
212 else if (intr && !status)
222 if (!alt || !in || !out)
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);
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;
240 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
242 static void intr_complete (struct urb *urb, struct pt_regs *regs);
244 static int init_status (struct usbnet *dev, struct usb_interface *intf)
251 if (!dev->driver_info->status)
254 pipe = usb_rcvintpipe (dev->udev,
255 dev->status->desc.bEndpointAddress
256 & USB_ENDPOINT_NUMBER_MASK);
257 maxp = usb_maxpacket (dev->udev, pipe, 0);
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);
263 buf = kmalloc (maxp, SLAB_KERNEL);
265 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
266 if (!dev->interrupt) {
270 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
271 buf, maxp, intr_complete, dev, period);
273 "status ep%din, %d bytes period %d\n",
274 usb_pipeendpoint(pipe), maxp, period);
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.
284 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
289 skb->protocol = eth_type_trans (skb, dev->net);
290 dev->stats.rx_packets++;
291 dev->stats.rx_bytes += skb->len;
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);
301 EXPORT_SYMBOL_GPL(usbnet_skb_return);
304 /*-------------------------------------------------------------------------
306 * Communications Device Class declarations.
307 * Used by CDC Ethernet, and some CDC variants
309 *-------------------------------------------------------------------------*/
311 #ifdef CONFIG_USB_CDCETHER
312 #define NEED_GENERIC_CDC
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
320 #ifdef CONFIG_USB_RNDIS
321 /* ACM variant uses even funkier framing, complex control RPC scheme */
322 #define NEED_GENERIC_CDC
326 #ifdef NEED_GENERIC_CDC
328 #include <linux/usb_cdc.h>
330 static struct usb_driver usbnet_driver;
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.
337 int usbnet_generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
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;
346 if (sizeof dev->data < sizeof *info)
349 /* expect strict spec conformance for the descriptors, but
350 * cope with firmware which stores them in the wrong place
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
356 buf = dev->udev->actconfig->extra;
357 len = dev->udev->actconfig->extralen;
360 "CDC descriptors on config\n");
363 /* this assumes that if there's a non-RNDIS vendor variant
364 * of cdc-acm, it'll fail RNDIS requests cleanly.
366 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
368 memset (info, 0, sizeof *info);
369 info->control = intf;
371 if (buf [1] != USB_DT_CS_INTERFACE)
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.
382 case USB_CDC_HEADER_TYPE:
384 dev_dbg (&intf->dev, "extra CDC header\n");
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);
394 case USB_CDC_UNION_TYPE:
396 dev_dbg (&intf->dev, "extra CDC union\n");
399 info->u = (void *) buf;
400 if (info->u->bLength != sizeof *info->u) {
401 dev_dbg (&intf->dev, "CDC union len %u\n",
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.
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) {
416 "master #%u/%p slave #%u/%p\n",
417 info->u->bMasterInterface0,
419 info->u->bSlaveInterface0,
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.
428 if (info->data == intf) {
429 info->data = info->control;
430 info->control = intf;
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",
443 case USB_CDC_ETHERNET_TYPE:
445 dev_dbg (&intf->dev, "extra CDC ether\n");
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);
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.
462 len -= buf [0]; /* bLength */
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 ");
474 /* claim data interface and set it up ... with side effects.
475 * network traffic can't flow until an altsetting is enabled.
477 status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
480 status = usbnet_get_endpoints (dev, info->data);
482 /* ensure immediate exit from usbnet_disconnect */
483 usb_set_intfdata(info->data, NULL);
484 usb_driver_release_interface (&usbnet_driver, info->data);
488 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
490 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
491 struct usb_endpoint_descriptor *desc;
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");
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);
513 dev_info (&dev->udev->dev, "bad CDC descriptors\n");
516 EXPORT_SYMBOL_GPL(usbnet_generic_cdc_bind);
518 void usbnet_cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
520 struct cdc_state *info = (void *) &dev->data;
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);
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;
538 EXPORT_SYMBOL_GPL(usbnet_cdc_unbind);
540 #endif /* NEED_GENERIC_CDC */
543 #ifdef CONFIG_USB_CDCETHER
544 #define HAVE_HARDWARE
546 /*-------------------------------------------------------------------------
548 * Communications Device Class, Ethernet Control model
550 * Takes two interfaces. The DATA interface is inactive till an altsetting
551 * is selected. Configuration data includes class descriptors.
553 * This should interop with whatever the 2.4 "CDCEther.c" driver
554 * (by Brad Hards) talked with.
556 *-------------------------------------------------------------------------*/
558 #include <linux/ctype.h>
561 static void dumpspeed (struct usbnet *dev, __le32 *speeds)
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);
569 static void cdc_status (struct usbnet *dev, struct urb *urb)
571 struct usb_cdc_notification *event;
573 if (urb->actual_length < sizeof *event)
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);
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");
589 netif_carrier_on(dev->net);
591 netif_carrier_off(dev->net);
593 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
594 if (netif_msg_timer (dev))
595 devdbg (dev, "CDC: speed change (len %d)",
597 if (urb->actual_length != (sizeof *event + 8))
598 set_bit (EVENT_STS_SPLIT, &dev->flags);
600 dumpspeed (dev, (__le32 *) &event[1]);
602 // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: /* RNDIS; or unsolicited */
604 deverr (dev, "CDC: unexpected notification %02x!",
605 event->bNotificationType);
610 static u8 nibble (unsigned char c)
612 if (likely (isdigit (c)))
615 if (likely (isxdigit (c)))
621 get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
624 unsigned char buf [13];
626 tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
628 dev_dbg (&dev->udev->dev,
629 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
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]);
640 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
643 struct cdc_state *info = (void *) &dev->data;
645 status = usbnet_generic_cdc_bind (dev, intf);
649 status = get_ethernet_addr (dev, info->ether);
651 usb_set_intfdata(info->data, NULL);
652 usb_driver_release_interface (&usbnet_driver, info->data);
656 /* FIXME cdc-ether has some multicast code too, though it complains
657 * in routine cases. info->ether describes the multicast support.
662 static const struct driver_info cdc_info = {
663 .description = "CDC Ethernet Device",
665 // .check_connect = cdc_check_connect,
667 .unbind = usbnet_cdc_unbind,
668 .status = cdc_status,
671 #endif /* CONFIG_USB_CDCETHER */
675 #ifdef CONFIG_USB_PL2301
676 #define HAVE_HARDWARE
678 /*-------------------------------------------------------------------------
680 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
682 * The protocol and handshaking used here should be bug-compatible
683 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
685 *-------------------------------------------------------------------------*/
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.
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 */
701 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
703 return usb_control_msg (dev->udev,
704 usb_rcvctrlpipe (dev->udev, 0),
706 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
709 USB_CTRL_GET_TIMEOUT);
713 pl_clear_QuickLink_features (struct usbnet *dev, int val)
715 return pl_vendor_req (dev, 1, (u8) val, 0);
719 pl_set_QuickLink_features (struct usbnet *dev, int val)
721 return pl_vendor_req (dev, 3, (u8) val, 0);
724 /*-------------------------------------------------------------------------*/
726 static int pl_reset (struct usbnet *dev)
728 /* some units seem to need this reset, others reject it utterly.
729 * FIXME be more like "naplink" or windows drivers.
731 (void) pl_set_QuickLink_features (dev,
732 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
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() */
743 #endif /* CONFIG_USB_PL2301 */
746 /*-------------------------------------------------------------------------
748 * Network Device Driver (peer link to "Host Device", from USB host)
750 *-------------------------------------------------------------------------*/
752 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
754 struct usbnet *dev = netdev_priv(net);
755 int ll_mtu = new_mtu + net->hard_header_len;
757 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
759 // no second zero-length packet read wanted after mtu-sized packets
760 if ((ll_mtu % dev->maxpacket) == 0)
766 /*-------------------------------------------------------------------------*/
768 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
770 struct usbnet *dev = netdev_priv(net);
774 /*-------------------------------------------------------------------------*/
776 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
777 * completion callbacks. 2.5 should have fixed those bugs...
780 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
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);
794 /* some work can't be done in tasklets, so we use keventd
796 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
797 * but tasklet_schedule() doesn't. hope the failure is rare.
799 void usbnet_defer_kevent (struct usbnet *dev, int work)
801 set_bit (work, &dev->flags);
802 if (!schedule_work (&dev->kevent))
803 deverr (dev, "kevent %d may have been dropped", work);
805 devdbg (dev, "kevent %d scheduled", work);
807 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
809 /*-------------------------------------------------------------------------*/
811 static void rx_complete (struct urb *urb, struct pt_regs *regs);
813 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
816 struct skb_data *entry;
818 unsigned long lockflags;
819 size_t size = dev->rx_urb_size;
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);
828 skb_reserve (skb, NET_IP_ALIGN);
830 entry = (struct skb_data *) skb->cb;
833 entry->state = rx_start;
836 usb_fill_bulk_urb (urb, dev->udev, dev->in,
837 skb->data, size, rx_complete, skb);
839 spin_lock_irqsave (&dev->rxq.lock, lockflags);
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)){
846 usbnet_defer_kevent (dev, EVENT_RX_HALT);
849 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
852 if (netif_msg_ifdown (dev))
853 devdbg (dev, "device gone");
854 netif_device_detach (dev->net);
857 if (netif_msg_rx_err (dev))
858 devdbg (dev, "rx submit, %d", retval);
859 tasklet_schedule (&dev->bh);
862 __skb_queue_tail (&dev->rxq, skb);
865 if (netif_msg_ifdown (dev))
866 devdbg (dev, "rx: stopped");
869 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
871 dev_kfree_skb_any (skb);
877 /*-------------------------------------------------------------------------*/
879 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
881 if (dev->driver_info->rx_fixup
882 && !dev->driver_info->rx_fixup (dev, skb))
884 // else network stack removes extra byte if we forced a short packet
887 usbnet_skb_return (dev, skb);
889 if (netif_msg_rx_err (dev))
890 devdbg (dev, "drop");
892 dev->stats.rx_errors++;
893 skb_queue_tail (&dev->done, skb);
897 /*-------------------------------------------------------------------------*/
899 static void rx_complete (struct urb *urb, struct pt_regs *regs)
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;
906 skb_put (skb, urb->actual_length);
907 entry->state = rx_done;
910 switch (urb_status) {
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);
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.
927 dev->stats.rx_errors++;
928 usbnet_defer_kevent (dev, EVENT_RX_HALT);
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);
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);
951 entry->state = rx_cleanup;
956 // data overrun ... flush fifo?
958 dev->stats.rx_over_errors++;
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);
969 defer_bh(dev, skb, &dev->rxq);
972 if (netif_running (dev->net)
973 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
974 rx_submit (dev, urb, GFP_ATOMIC);
979 if (netif_msg_rx_err (dev))
980 devdbg (dev, "no read resubmitted");
983 static void intr_complete (struct urb *urb, struct pt_regs *regs)
985 struct usbnet *dev = urb->context;
986 int status = urb->status;
991 dev->driver_info->status(dev, urb);
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);
1001 /* NOTE: not throttling like RX/TX, since this endpoint
1002 * already polls infrequently
1005 devdbg (dev, "intr status %d", status);
1009 if (!netif_running (dev->net))
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);
1018 /*-------------------------------------------------------------------------*/
1020 // unlink pending rx/tx; completion handlers do all other cleanup
1022 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1024 unsigned long flags;
1025 struct sk_buff *skb, *skbnext;
1028 spin_lock_irqsave (&q->lock, flags);
1029 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1030 struct skb_data *entry;
1034 entry = (struct skb_data *) skb->cb;
1036 skbnext = skb->next;
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);
1046 spin_unlock_irqrestore (&q->lock, flags);
1051 /*-------------------------------------------------------------------------*/
1053 // precondition: never called in_interrupt
1055 static int usbnet_stop (struct net_device *net)
1057 struct usbnet *dev = netdev_priv(net);
1059 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
1060 DECLARE_WAITQUEUE (wait, current);
1062 netif_stop_queue (net);
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
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);
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);
1084 remove_wait_queue (&unlink_wakeup, &wait);
1086 usb_kill_urb(dev->interrupt);
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.
1093 del_timer_sync (&dev->delay);
1094 tasklet_kill (&dev->bh);
1099 /*-------------------------------------------------------------------------*/
1101 // posts reads, and enables write queuing
1103 // precondition: never called in_interrupt
1105 static int usbnet_open (struct net_device *net)
1107 struct usbnet *dev = netdev_priv(net);
1109 struct driver_info *info = dev->driver_info;
1111 // put into "known safe" state
1112 if (info->reset && (retval = info->reset (dev)) < 0) {
1113 if (netif_msg_ifup (dev))
1115 "open reset fail (%d) usbnet usb-%s-%s, %s",
1117 dev->udev->bus->bus_name, dev->udev->devpath,
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);
1129 /* start any status interrupt transfer */
1130 if (dev->interrupt) {
1131 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
1133 if (netif_msg_ifup (dev))
1134 deverr (dev, "intr submit %d", retval);
1139 netif_start_queue (net);
1140 if (netif_msg_ifup (dev)) {
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)
1149 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
1151 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
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,
1162 // delay posting reads until we're fully open
1163 tasklet_schedule (&dev->bh);
1168 /*-------------------------------------------------------------------------*/
1170 /* ethtool methods; minidrivers may need to add some more, but
1171 * they'll probably want to use this base set.
1174 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1176 struct usbnet *dev = netdev_priv(net);
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);
1185 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1187 static u32 usbnet_get_link (struct net_device *net)
1189 struct usbnet *dev = netdev_priv(net);
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;
1195 /* Otherwise, say we're up (to avoid breaking scripts) */
1199 u32 usbnet_get_msglevel (struct net_device *net)
1201 struct usbnet *dev = netdev_priv(net);
1203 return dev->msg_enable;
1205 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1207 void usbnet_set_msglevel (struct net_device *net, u32 level)
1209 struct usbnet *dev = netdev_priv(net);
1211 dev->msg_enable = level;
1213 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
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,
1223 /*-------------------------------------------------------------------------*/
1225 /* work that cannot be done in interrupt context uses keventd.
1227 * NOTE: with 2.5 we could do more of this using completion callbacks,
1228 * especially now that control transfers can be queued.
1233 struct usbnet *dev = data;
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);
1242 && status != -ESHUTDOWN) {
1243 if (netif_msg_tx_err (dev))
1244 deverr (dev, "can't clear tx halt, status %d",
1247 clear_bit (EVENT_TX_HALT, &dev->flags);
1248 if (status != -ESHUTDOWN)
1249 netif_wake_queue (dev->net);
1252 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1253 unlink_urbs (dev, &dev->rxq);
1254 status = usb_clear_halt (dev->udev, dev->in);
1257 && status != -ESHUTDOWN) {
1258 if (netif_msg_rx_err (dev))
1259 deverr (dev, "can't clear rx halt, status %d",
1262 clear_bit (EVENT_RX_HALT, &dev->flags);
1263 tasklet_schedule (&dev->bh);
1267 /* tasklet could resubmit itself forever if memory is tight */
1268 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1269 struct urb *urb = NULL;
1271 if (netif_running (dev->net))
1272 urb = usb_alloc_urb (0, GFP_KERNEL);
1274 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1276 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1277 rx_submit (dev, urb, GFP_KERNEL);
1278 tasklet_schedule (&dev->bh);
1282 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1283 struct driver_info *info = dev->driver_info;
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",
1290 dev->udev->bus->bus_name, dev->udev->devpath,
1296 devdbg (dev, "kevent done, flags = 0x%lx",
1300 /*-------------------------------------------------------------------------*/
1302 static void tx_complete (struct urb *urb, struct pt_regs *regs)
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;
1308 if (urb->status == 0) {
1309 dev->stats.tx_packets++;
1310 dev->stats.tx_bytes += entry->length;
1312 dev->stats.tx_errors++;
1314 switch (urb->status) {
1316 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1319 /* software-driven interface shutdown */
1320 case -ECONNRESET: // async unlink
1321 case -ESHUTDOWN: // hardware gone
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",
1336 netif_stop_queue (dev->net);
1339 if (netif_msg_tx_err (dev))
1340 devdbg (dev, "tx err %d", entry->urb->status);
1346 entry->state = tx_done;
1347 defer_bh(dev, skb, &dev->txq);
1350 /*-------------------------------------------------------------------------*/
1352 static void usbnet_tx_timeout (struct net_device *net)
1354 struct usbnet *dev = netdev_priv(net);
1356 unlink_urbs (dev, &dev->txq);
1357 tasklet_schedule (&dev->bh);
1359 // FIXME: device recovery -- reset?
1362 /*-------------------------------------------------------------------------*/
1364 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
1366 struct usbnet *dev = netdev_priv(net);
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;
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);
1379 if (netif_msg_tx_err (dev))
1380 devdbg (dev, "can't tx_fixup skb");
1386 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1387 if (netif_msg_tx_err (dev))
1388 devdbg (dev, "no urb");
1392 entry = (struct skb_data *) skb->cb;
1395 entry->state = tx_start;
1396 entry->length = length;
1398 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1399 skb->data, skb->len, tx_complete, skb);
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.
1405 * FIXME zero that byte, if it doesn't require a new skb.
1407 if ((length % dev->maxpacket) == 0)
1408 urb->transfer_buffer_length++;
1410 spin_lock_irqsave (&dev->txq.lock, flags);
1412 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1414 netif_stop_queue (net);
1415 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1418 if (netif_msg_tx_err (dev))
1419 devdbg (dev, "tx: submit urb err %d", retval);
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);
1427 spin_unlock_irqrestore (&dev->txq.lock, flags);
1430 if (netif_msg_tx_err (dev))
1431 devdbg (dev, "drop, code %d", retval);
1433 retval = NET_XMIT_SUCCESS;
1434 dev->stats.tx_dropped++;
1436 dev_kfree_skb_any (skb);
1438 } else if (netif_msg_tx_queued (dev)) {
1439 devdbg (dev, "> tx, len %d, type 0x%x",
1440 length, skb->protocol);
1446 /*-------------------------------------------------------------------------*/
1448 // tasklet (work deferred from completions, in_irq) or timer
1450 static void usbnet_bh (unsigned long param)
1452 struct usbnet *dev = (struct usbnet *) param;
1453 struct sk_buff *skb;
1454 struct skb_data *entry;
1456 while ((skb = skb_dequeue (&dev->done))) {
1457 entry = (struct skb_data *) skb->cb;
1458 switch (entry->state) {
1460 entry->state = rx_cleanup;
1461 rx_process (dev, skb);
1465 usb_free_urb (entry->urb);
1466 dev_kfree_skb (skb);
1469 devdbg (dev, "bogus skb state %d", entry->state);
1473 // waiting for all pending urbs to complete?
1475 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1476 wake_up (dev->wait);
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);
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);
1495 rx_submit (dev, urb, GFP_ATOMIC);
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);
1503 if (dev->txq.qlen < TX_QLEN (dev))
1504 netif_wake_queue (dev->net);
1510 /*-------------------------------------------------------------------------
1512 * USB Device Driver support
1514 *-------------------------------------------------------------------------*/
1516 // precondition: never called in_interrupt
1518 void usbnet_disconnect (struct usb_interface *intf)
1521 struct usb_device *xdev;
1522 struct net_device *net;
1524 dev = usb_get_intfdata(intf);
1525 usb_set_intfdata(intf, NULL);
1529 xdev = interface_to_usbdev (intf);
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);
1538 unregister_netdev (net);
1540 /* we don't hold rtnl here ... */
1541 flush_scheduled_work ();
1543 if (dev->driver_info->unbind)
1544 dev->driver_info->unbind (dev, intf);
1549 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1552 /*-------------------------------------------------------------------------*/
1554 // precondition: never called in_interrupt
1557 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1560 struct net_device *net;
1561 struct usb_host_interface *interface;
1562 struct driver_info *info;
1563 struct usb_device *xdev;
1566 info = (struct driver_info *) prod->driver_info;
1568 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1571 xdev = interface_to_usbdev (udev);
1572 interface = udev->cur_altsetting;
1578 // set up our own records
1579 net = alloc_etherdev(sizeof(*dev));
1581 dbg ("can't kmalloc dev");
1585 dev = netdev_priv(net);
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);
1600 SET_MODULE_OWNER (net);
1602 strcpy (net->name, "usb%d");
1603 memcpy (net->dev_addr, node_id, sizeof node_id);
1605 /* rx and tx sides can use different message sizes;
1606 * bind() should set rx_urb_size in that case.
1608 dev->hard_mtu = net->mtu + net->hard_header_len;
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;
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;
1625 // allow device-specific bind/init procedures
1626 // NOTE net->name still not usable ...
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");
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);
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);
1652 if (status == 0 && dev->status)
1653 status = init_status (dev, udev);
1657 if (!dev->rx_urb_size)
1658 dev->rx_urb_size = dev->hard_mtu;
1659 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1661 SET_NETDEV_DEV(net, &udev->dev);
1662 status = register_netdev (net);
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]);
1675 // ok, it's ready to go.
1676 usb_set_intfdata (udev, dev);
1678 // start as if the link is up
1679 netif_device_attach (net);
1685 info->unbind (dev, udev);
1692 EXPORT_SYMBOL_GPL(usbnet_probe);
1694 /*-------------------------------------------------------------------------*/
1696 /* FIXME these suspend/resume methods assume non-CDC style
1697 * devices, with only one interface.
1700 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1702 struct usbnet *dev = usb_get_intfdata(intf);
1704 /* accelerate emptying of the rx and queues, to avoid
1705 * having everything error out.
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;
1713 EXPORT_SYMBOL_GPL(usbnet_suspend);
1715 int usbnet_resume (struct usb_interface *intf)
1717 struct usbnet *dev = usb_get_intfdata(intf);
1719 intf->dev.power.power_state = PMSG_ON;
1720 netif_device_attach (dev->net);
1721 tasklet_schedule (&dev->bh);
1724 EXPORT_SYMBOL_GPL(usbnet_resume);
1727 /*-------------------------------------------------------------------------*/
1729 #ifndef HAVE_HARDWARE
1730 #error You need to configure some hardware for this driver
1734 * chip vendor names won't normally be on the cables, and
1735 * may not be on the device.
1738 static const struct usb_device_id products [] = {
1740 #ifdef CONFIG_USB_PL2301
1742 USB_DEVICE (0x067b, 0x0000), // PL-2301
1743 .driver_info = (unsigned long) &prolific_info,
1745 USB_DEVICE (0x067b, 0x0001), // PL-2302
1746 .driver_info = (unsigned long) &prolific_info,
1750 #ifdef CONFIG_USB_RNDIS
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,
1758 #ifdef CONFIG_USB_CDCETHER
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
1765 * NOTE: this match must come AFTER entries working around
1766 * bugs/quirks in a given product (like Zaurus, above).
1768 USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
1769 USB_CDC_PROTO_NONE),
1770 .driver_info = (unsigned long) &cdc_info,
1776 MODULE_DEVICE_TABLE (usb, products);
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,
1788 /*-------------------------------------------------------------------------*/
1790 static int __init usbnet_init(void)
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)));
1800 random_ether_addr(node_id);
1802 return usb_register(&usbnet_driver);
1804 module_init(usbnet_init);
1806 static void __exit usbnet_exit(void)
1808 usb_deregister(&usbnet_driver);
1810 module_exit(usbnet_exit);
1812 MODULE_AUTHOR("David Brownell");
1813 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
1814 MODULE_LICENSE("GPL");