3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
5 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * This is a generic "USB networking" framework that works with several
24 * kinds of full and high speed networking devices:
26 * + USB host-to-host "network cables", used for IP-over-USB links.
27 * These are often used for Laplink style connectivity products.
29 * - Belkin, eTEK (interops with Win32 drivers)
30 * - GeneSys GL620USB-A
31 * - NetChip 1080 (interoperates with NetChip Win32 drivers)
32 * - Prolific PL-2301/2302 (replaces "plusb" driver)
33 * - KC Technology KC2190
35 * + Smart USB devices can support such links directly, using Internet
36 * standard protocols instead of proprietary host-to-device links.
37 * - Linux PDAs like iPaq, Yopy, and Zaurus
38 * - The BLOB boot loader (for diskless booting)
39 * - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
40 * - Devices using EPSON's sample USB firmware
41 * - CDC-Ethernet class devices, such as many cable modems
43 * + Adapters to networks such as Ethernet.
44 * - AX8817X based USB 2.0 products
46 * Links to these devices can be bridged using Linux Ethernet bridging.
47 * With minor exceptions, these all use similar USB framing for network
48 * traffic, but need different protocols for control traffic.
50 * USB devices can implement their side of this protocol at the cost
51 * of two bulk endpoints; it's not restricted to "cable" applications.
52 * See the SA1110, Zaurus, or EPSON device/client support in this driver;
53 * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
54 * "g_ether" (in the Linux "gadget" framework) implement that behavior
60 * 13-sep-2000 experimental, new
61 * 10-oct-2000 usb_device_id table created.
62 * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
63 * 01-nov-2000 usb_device_id table and probing api update by
64 * Adam J. Richter <adam@yggdrasil.com>.
65 * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
66 * and prolific support, isolate net1080-specific bits, cleanup.
67 * fix unlink_urbs oops in D3 PM resume code path.
69 * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
70 * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
71 * AnchorChips 2720 support (from spec) for testing;
72 * fix bit-ordering problem with ethernet multicast addr
73 * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support
74 * updates. Oleg Drokin (green@iXcelerator.com)
75 * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
76 * expecting cleared skb->cb and framing change to match latest
77 * handhelds.org version (Oleg). Enable device IDs from the
78 * Win32 Belkin driver; other cleanups (db).
79 * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
80 * cleanups for problems not yet seen in the field. (db)
81 * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
82 * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
83 * rx unlinks somehow weren't async; minor cleanup.
84 * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
85 * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
86 * <utx@penguin.cz>. Made framing options (NetChip/GeneSys)
87 * tie mostly to (sub)driver info. Workaround some PL-2302
88 * chips that seem to reject SET_INTERFACE requests.
90 * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
91 * Level of diagnostics is more configurable; they use device
92 * location (usb_device->devpath) instead of address (2.5).
93 * For tx_fixup, memflags can't be NOIO.
94 * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
95 * for USB 2.0 TTs) and memory shortages (potential) too. (db)
96 * Use "locally assigned" IEEE802 address space. (Brad Hards)
97 * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db).
98 * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
99 * cleanups and stubbed PXA-250 support (db), fix for framing
100 * issues on Z, net1080, and gl620a (Toby Milne)
102 * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100
103 * vs pxa25x, and CDC Ethernet. Throttle down log floods on
104 * disconnect; other cleanups. (db) Flush net1080 fifos
105 * after several sequential framing errors. (Johannes Erdfelt)
106 * 22-aug-2003 AX8817X support (Dave Hollis).
107 * 14-jun-2004 Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
109 * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
111 * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis)
112 *-------------------------------------------------------------------------*/
114 // #define DEBUG // error path messages, extra info
115 // #define VERBOSE // more; success messages
117 #include <linux/config.h>
118 #ifdef CONFIG_USB_DEBUG
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>
133 #define DRIVER_VERSION "22-Aug-2005"
136 /*-------------------------------------------------------------------------*/
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).
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.
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)
152 // reawaken network queue this soon after stopping; else watchdog barks
153 #define TX_TIMEOUT_JIFFIES (5*HZ)
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)
160 #define UNLINK_TIMEOUT_MS 3
162 /*-------------------------------------------------------------------------*/
164 // randomly generated ethernet address
165 static u8 node_id [ETH_ALEN];
167 static const char driver_name [] = "usbnet";
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");
174 /*-------------------------------------------------------------------------*/
176 static u32 usbnet_get_link (struct net_device *);
178 /* mostly for PDA style devices, which are always connected if present */
179 static int always_connected (struct usbnet *dev)
184 /* handles CDC Ethernet and many other network "bulk data" interfaces */
185 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
188 struct usb_host_interface *alt = NULL;
189 struct usb_host_endpoint *in = NULL, *out = NULL;
190 struct usb_host_endpoint *status = NULL;
192 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
195 in = out = status = NULL;
196 alt = intf->altsetting + tmp;
198 /* take the first altsetting with in-bulk + out-bulk;
199 * remember any status endpoint, just in case;
200 * ignore other endpoints and altsetttings.
202 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
203 struct usb_host_endpoint *e;
206 e = alt->endpoint + ep;
207 switch (e->desc.bmAttributes) {
208 case USB_ENDPOINT_XFER_INT:
209 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
213 case USB_ENDPOINT_XFER_BULK:
218 if (e->desc.bEndpointAddress & USB_DIR_IN) {
221 else if (intr && !status)
231 if (!alt || !in || !out)
234 if (alt->desc.bAlternateSetting != 0
235 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
236 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
237 alt->desc.bAlternateSetting);
242 dev->in = usb_rcvbulkpipe (dev->udev,
243 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
244 dev->out = usb_sndbulkpipe (dev->udev,
245 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
246 dev->status = status;
249 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
251 static void intr_complete (struct urb *urb, struct pt_regs *regs);
253 static int init_status (struct usbnet *dev, struct usb_interface *intf)
260 if (!dev->driver_info->status)
263 pipe = usb_rcvintpipe (dev->udev,
264 dev->status->desc.bEndpointAddress
265 & USB_ENDPOINT_NUMBER_MASK);
266 maxp = usb_maxpacket (dev->udev, pipe, 0);
268 /* avoid 1 msec chatter: min 8 msec poll rate */
269 period = max ((int) dev->status->desc.bInterval,
270 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
272 buf = kmalloc (maxp, SLAB_KERNEL);
274 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
275 if (!dev->interrupt) {
279 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
280 buf, maxp, intr_complete, dev, period);
282 "status ep%din, %d bytes period %d\n",
283 usb_pipeendpoint(pipe), maxp, period);
289 /* Passes this packet up the stack, updating its accounting.
290 * Some link protocols batch packets, so their rx_fixup paths
291 * can return clones as well as just modify the original skb.
293 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
298 skb->protocol = eth_type_trans (skb, dev->net);
299 dev->stats.rx_packets++;
300 dev->stats.rx_bytes += skb->len;
302 if (netif_msg_rx_status (dev))
303 devdbg (dev, "< rx, len %zu, type 0x%x",
304 skb->len + sizeof (struct ethhdr), skb->protocol);
305 memset (skb->cb, 0, sizeof (struct skb_data));
306 status = netif_rx (skb);
307 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
308 devdbg (dev, "netif_rx status %d", status);
310 EXPORT_SYMBOL_GPL(usbnet_skb_return);
313 /*-------------------------------------------------------------------------
315 * Communications Device Class declarations.
316 * Used by CDC Ethernet, and some CDC variants
318 *-------------------------------------------------------------------------*/
320 #ifdef CONFIG_USB_CDCETHER
321 #define NEED_GENERIC_CDC
324 #ifdef CONFIG_USB_ZAURUS
325 /* Ethernet variant uses funky framing, broken ethernet addressing */
326 #define NEED_GENERIC_CDC
329 #ifdef CONFIG_USB_RNDIS
330 /* ACM variant uses even funkier framing, complex control RPC scheme */
331 #define NEED_GENERIC_CDC
335 #ifdef NEED_GENERIC_CDC
337 #include <linux/usb_cdc.h>
340 struct usb_cdc_header_desc *header;
341 struct usb_cdc_union_desc *u;
342 struct usb_cdc_ether_desc *ether;
343 struct usb_interface *control;
344 struct usb_interface *data;
347 static struct usb_driver usbnet_driver;
350 * probes control interface, claims data interface, collects the bulk
351 * endpoints, activates data interface (if needed), maybe sets MTU.
352 * all pure cdc, except for certain firmware workarounds.
354 static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
356 u8 *buf = intf->cur_altsetting->extra;
357 int len = intf->cur_altsetting->extralen;
358 struct usb_interface_descriptor *d;
359 struct cdc_state *info = (void *) &dev->data;
363 if (sizeof dev->data < sizeof *info)
366 /* expect strict spec conformance for the descriptors, but
367 * cope with firmware which stores them in the wrong place
369 if (len == 0 && dev->udev->actconfig->extralen) {
370 /* Motorola SB4100 (and others: Brad Hards says it's
371 * from a Broadcom design) put CDC descriptors here
373 buf = dev->udev->actconfig->extra;
374 len = dev->udev->actconfig->extralen;
377 "CDC descriptors on config\n");
380 /* this assumes that if there's a non-RNDIS vendor variant
381 * of cdc-acm, it'll fail RNDIS requests cleanly.
383 rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
385 memset (info, 0, sizeof *info);
386 info->control = intf;
388 if (buf [1] != USB_DT_CS_INTERFACE)
391 /* use bDescriptorSubType to identify the CDC descriptors.
392 * We expect devices with CDC header and union descriptors.
393 * For CDC Ethernet we need the ethernet descriptor.
394 * For RNDIS, ignore two (pointless) CDC modem descriptors
395 * in favor of a complicated OID-based RPC scheme doing what
396 * CDC Ethernet achieves with a simple descriptor.
399 case USB_CDC_HEADER_TYPE:
401 dev_dbg (&intf->dev, "extra CDC header\n");
404 info->header = (void *) buf;
405 if (info->header->bLength != sizeof *info->header) {
406 dev_dbg (&intf->dev, "CDC header len %u\n",
407 info->header->bLength);
411 case USB_CDC_UNION_TYPE:
413 dev_dbg (&intf->dev, "extra CDC union\n");
416 info->u = (void *) buf;
417 if (info->u->bLength != sizeof *info->u) {
418 dev_dbg (&intf->dev, "CDC union len %u\n",
423 /* we need a master/control interface (what we're
424 * probed with) and a slave/data interface; union
425 * descriptors sort this all out.
427 info->control = usb_ifnum_to_if(dev->udev,
428 info->u->bMasterInterface0);
429 info->data = usb_ifnum_to_if(dev->udev,
430 info->u->bSlaveInterface0);
431 if (!info->control || !info->data) {
433 "master #%u/%p slave #%u/%p\n",
434 info->u->bMasterInterface0,
436 info->u->bSlaveInterface0,
440 if (info->control != intf) {
441 dev_dbg (&intf->dev, "bogus CDC Union\n");
442 /* Ambit USB Cable Modem (and maybe others)
443 * interchanges master and slave interface.
445 if (info->data == intf) {
446 info->data = info->control;
447 info->control = intf;
452 /* a data interface altsetting does the real i/o */
453 d = &info->data->cur_altsetting->desc;
454 if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
455 dev_dbg (&intf->dev, "slave class %u\n",
460 case USB_CDC_ETHERNET_TYPE:
462 dev_dbg (&intf->dev, "extra CDC ether\n");
465 info->ether = (void *) buf;
466 if (info->ether->bLength != sizeof *info->ether) {
467 dev_dbg (&intf->dev, "CDC ether len %u\n",
468 info->ether->bLength);
471 dev->hard_mtu = le16_to_cpu(
472 info->ether->wMaxSegmentSize);
473 /* because of Zaurus, we may be ignoring the host
474 * side link address we were given.
479 len -= buf [0]; /* bLength */
483 if (!info->header || !info->u || (!rndis && !info->ether)) {
484 dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
485 info->header ? "" : "header ",
486 info->u ? "" : "union ",
487 info->ether ? "" : "ether ");
491 /* claim data interface and set it up ... with side effects.
492 * network traffic can't flow until an altsetting is enabled.
494 status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
497 status = usbnet_get_endpoints (dev, info->data);
499 /* ensure immediate exit from usbnet_disconnect */
500 usb_set_intfdata(info->data, NULL);
501 usb_driver_release_interface (&usbnet_driver, info->data);
505 /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
507 if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
508 struct usb_endpoint_descriptor *desc;
510 dev->status = &info->control->cur_altsetting->endpoint [0];
511 desc = &dev->status->desc;
512 if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
513 || !(desc->bEndpointAddress & USB_DIR_IN)
514 || (le16_to_cpu(desc->wMaxPacketSize)
515 < sizeof (struct usb_cdc_notification))
516 || !desc->bInterval) {
517 dev_dbg (&intf->dev, "bad notification endpoint\n");
521 if (rndis && !dev->status) {
522 dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
523 usb_set_intfdata(info->data, NULL);
524 usb_driver_release_interface (&usbnet_driver, info->data);
530 dev_info (&dev->udev->dev, "bad CDC descriptors\n");
534 static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
536 struct cdc_state *info = (void *) &dev->data;
538 /* disconnect master --> disconnect slave */
539 if (intf == info->control && info->data) {
540 /* ensure immediate exit from usbnet_disconnect */
541 usb_set_intfdata(info->data, NULL);
542 usb_driver_release_interface (&usbnet_driver, info->data);
546 /* and vice versa (just in case) */
547 else if (intf == info->data && info->control) {
548 /* ensure immediate exit from usbnet_disconnect */
549 usb_set_intfdata(info->control, NULL);
550 usb_driver_release_interface (&usbnet_driver, info->control);
551 info->control = NULL;
555 #endif /* NEED_GENERIC_CDC */
558 #ifdef CONFIG_USB_CDCETHER
559 #define HAVE_HARDWARE
561 /*-------------------------------------------------------------------------
563 * Communications Device Class, Ethernet Control model
565 * Takes two interfaces. The DATA interface is inactive till an altsetting
566 * is selected. Configuration data includes class descriptors.
568 * This should interop with whatever the 2.4 "CDCEther.c" driver
569 * (by Brad Hards) talked with.
571 *-------------------------------------------------------------------------*/
573 #include <linux/ctype.h>
576 static void dumpspeed (struct usbnet *dev, __le32 *speeds)
578 if (netif_msg_timer (dev))
579 devinfo (dev, "link speeds: %u kbps up, %u kbps down",
580 __le32_to_cpu(speeds[0]) / 1000,
581 __le32_to_cpu(speeds[1]) / 1000);
584 static void cdc_status (struct usbnet *dev, struct urb *urb)
586 struct usb_cdc_notification *event;
588 if (urb->actual_length < sizeof *event)
591 /* SPEED_CHANGE can get split into two 8-byte packets */
592 if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
593 dumpspeed (dev, (__le32 *) urb->transfer_buffer);
597 event = urb->transfer_buffer;
598 switch (event->bNotificationType) {
599 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
600 if (netif_msg_timer (dev))
601 devdbg (dev, "CDC: carrier %s",
602 event->wValue ? "on" : "off");
604 netif_carrier_on(dev->net);
606 netif_carrier_off(dev->net);
608 case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
609 if (netif_msg_timer (dev))
610 devdbg (dev, "CDC: speed change (len %d)",
612 if (urb->actual_length != (sizeof *event + 8))
613 set_bit (EVENT_STS_SPLIT, &dev->flags);
615 dumpspeed (dev, (__le32 *) &event[1]);
617 // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: /* RNDIS; or unsolicited */
619 deverr (dev, "CDC: unexpected notification %02x!",
620 event->bNotificationType);
625 static u8 nibble (unsigned char c)
627 if (likely (isdigit (c)))
630 if (likely (isxdigit (c)))
636 get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
639 unsigned char buf [13];
641 tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
643 dev_dbg (&dev->udev->dev,
644 "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
649 for (i = tmp = 0; i < 6; i++, tmp += 2)
650 dev->net->dev_addr [i] =
651 (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
655 static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
658 struct cdc_state *info = (void *) &dev->data;
660 status = generic_cdc_bind (dev, intf);
664 status = get_ethernet_addr (dev, info->ether);
666 usb_set_intfdata(info->data, NULL);
667 usb_driver_release_interface (&usbnet_driver, info->data);
671 /* FIXME cdc-ether has some multicast code too, though it complains
672 * in routine cases. info->ether describes the multicast support.
677 static const struct driver_info cdc_info = {
678 .description = "CDC Ethernet Device",
680 // .check_connect = cdc_check_connect,
682 .unbind = cdc_unbind,
683 .status = cdc_status,
686 #endif /* CONFIG_USB_CDCETHER */
690 #ifdef CONFIG_USB_PL2301
691 #define HAVE_HARDWARE
693 /*-------------------------------------------------------------------------
695 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
697 * The protocol and handshaking used here should be bug-compatible
698 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
700 *-------------------------------------------------------------------------*/
703 * Bits 0-4 can be used for software handshaking; they're set from
704 * one end, cleared from the other, "read" with the interrupt byte.
706 #define PL_S_EN (1<<7) /* (feature only) suspend enable */
707 /* reserved bit -- rx ready (6) ? */
708 #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
709 #define PL_RESET_OUT (1<<4) /* reset output pipe */
710 #define PL_RESET_IN (1<<3) /* reset input pipe */
711 #define PL_TX_C (1<<2) /* transmission complete */
712 #define PL_TX_REQ (1<<1) /* transmission received */
713 #define PL_PEER_E (1<<0) /* peer exists */
716 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
718 return usb_control_msg (dev->udev,
719 usb_rcvctrlpipe (dev->udev, 0),
721 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
724 USB_CTRL_GET_TIMEOUT);
728 pl_clear_QuickLink_features (struct usbnet *dev, int val)
730 return pl_vendor_req (dev, 1, (u8) val, 0);
734 pl_set_QuickLink_features (struct usbnet *dev, int val)
736 return pl_vendor_req (dev, 3, (u8) val, 0);
739 /*-------------------------------------------------------------------------*/
741 static int pl_reset (struct usbnet *dev)
743 /* some units seem to need this reset, others reject it utterly.
744 * FIXME be more like "naplink" or windows drivers.
746 (void) pl_set_QuickLink_features (dev,
747 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
751 static const struct driver_info prolific_info = {
752 .description = "Prolific PL-2301/PL-2302",
753 .flags = FLAG_NO_SETINT,
754 /* some PL-2302 versions seem to fail usb_set_interface() */
758 #endif /* CONFIG_USB_PL2301 */
761 #ifdef CONFIG_USB_ZAURUS
762 #define HAVE_HARDWARE
764 #include <linux/crc32.h>
766 /*-------------------------------------------------------------------------
768 * Zaurus is also a SA-1110 based PDA, but one using a different driver
769 * (and framing) for its USB slave/gadget controller than the case above.
771 * For the current version of that driver, the main way that framing is
772 * nonstandard (also from perspective of the CDC ethernet model!) is a
773 * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
774 * haven't been fully worked around. Also, all Zaurii use the same
775 * default Ethernet address.
777 * PXA based models use the same framing, and also can't implement
778 * set_interface properly.
780 * All known Zaurii lie about their standards conformance. Most lie by
781 * saying they support CDC Ethernet. Some lie and say they support CDC
782 * MDLM (as if for access to cell phone modems). Someone, please beat
783 * on Sharp (and other such vendors) for a while with a cluestick.
785 *-------------------------------------------------------------------------*/
787 static struct sk_buff *
788 zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
791 struct sk_buff *skb2;
794 if (!skb_cloned (skb)) {
795 int tailroom = skb_tailroom (skb);
796 if ((padlen + 4) <= tailroom)
799 skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
800 dev_kfree_skb_any (skb);
805 fcs = crc32_le (~0, skb->data, skb->len);
808 *skb_put (skb, 1) = fcs & 0xff;
809 *skb_put (skb, 1) = (fcs>> 8) & 0xff;
810 *skb_put (skb, 1) = (fcs>>16) & 0xff;
811 *skb_put (skb, 1) = (fcs>>24) & 0xff;
816 static int zaurus_bind (struct usbnet *dev, struct usb_interface *intf)
818 /* Belcarra's funky framing has other options; mostly
819 * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
821 dev->net->hard_header_len += 6;
822 return generic_cdc_bind(dev, intf);
825 static const struct driver_info zaurus_sl5x00_info = {
826 .description = "Sharp Zaurus SL-5x00",
827 .flags = FLAG_FRAMING_Z,
828 .check_connect = always_connected,
830 .unbind = cdc_unbind,
831 .tx_fixup = zaurus_tx_fixup,
833 #define ZAURUS_STRONGARM_INFO ((unsigned long)&zaurus_sl5x00_info)
835 static const struct driver_info zaurus_pxa_info = {
836 .description = "Sharp Zaurus, PXA-2xx based",
837 .flags = FLAG_FRAMING_Z,
838 .check_connect = always_connected,
840 .unbind = cdc_unbind,
841 .tx_fixup = zaurus_tx_fixup,
843 #define ZAURUS_PXA_INFO ((unsigned long)&zaurus_pxa_info)
845 static const struct driver_info olympus_mxl_info = {
846 .description = "Olympus R1000",
847 .flags = FLAG_FRAMING_Z,
848 .check_connect = always_connected,
850 .unbind = cdc_unbind,
851 .tx_fixup = zaurus_tx_fixup,
853 #define OLYMPUS_MXL_INFO ((unsigned long)&olympus_mxl_info)
856 /* Some more recent products using Lineo/Belcarra code will wrongly claim
857 * CDC MDLM conformance. They aren't conformant: data endpoints live
858 * in the control interface, there's no data interface, and it's not used
859 * to talk to a cell phone radio. But at least we can detect these two
860 * pseudo-classes, rather than growing this product list with entries for
861 * each new nonconformant product (sigh).
863 static const u8 safe_guid[16] = {
864 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
865 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
867 static const u8 blan_guid[16] = {
868 0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
869 0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
872 static int blan_mdlm_bind (struct usbnet *dev, struct usb_interface *intf)
874 u8 *buf = intf->cur_altsetting->extra;
875 int len = intf->cur_altsetting->extralen;
876 struct usb_cdc_mdlm_desc *desc = NULL;
877 struct usb_cdc_mdlm_detail_desc *detail = NULL;
880 if (buf [1] != USB_DT_CS_INTERFACE)
883 /* use bDescriptorSubType, and just verify that we get a
884 * "BLAN" (or "SAFE") descriptor.
887 case USB_CDC_MDLM_TYPE:
889 dev_dbg (&intf->dev, "extra MDLM\n");
893 if (desc->bLength != sizeof *desc) {
894 dev_dbg (&intf->dev, "MDLM len %u\n",
898 /* expect bcdVersion 1.0, ignore */
899 if (memcmp(&desc->bGUID, blan_guid, 16)
900 && memcmp(&desc->bGUID, safe_guid, 16) ) {
901 /* hey, this one might _really_ be MDLM! */
902 dev_dbg (&intf->dev, "MDLM guid\n");
906 case USB_CDC_MDLM_DETAIL_TYPE:
908 dev_dbg (&intf->dev, "extra MDLM detail\n");
911 detail = (void *) buf;
912 switch (detail->bGuidDescriptorType) {
914 if (detail->bLength != (sizeof *detail + 2))
918 if (detail->bLength != (sizeof *detail + 3))
925 /* assuming we either noticed BLAN already, or will
926 * find it soon, there are some data bytes here:
927 * - bmNetworkCapabilities (unused)
928 * - bmDataCapabilities (bits, see below)
929 * - bPad (ignored, for PADAFTER -- BLAN-only)
931 * - 0x01 -- Zaurus framing (add CRC)
932 * - 0x02 -- PADBEFORE (CRC includes some padding)
933 * - 0x04 -- PADAFTER (some padding after CRC)
934 * - 0x08 -- "fermat" packet mangling (for hw bugs)
935 * the PADBEFORE appears not to matter; we interop
936 * with devices that use it and those that don't.
938 if ((detail->bDetailData[1] & ~02) != 0x01) {
939 /* bmDataCapabilites == 0 would be fine too,
940 * but framing is minidriver-coupled for now.
944 "bad MDLM detail, %d %d %d\n",
946 detail->bDetailData[0],
947 detail->bDetailData[2]);
953 len -= buf [0]; /* bLength */
957 if (!desc || !detail) {
958 dev_dbg (&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
960 detail ? "" : "detail ");
964 /* There's probably a CDC Ethernet descriptor there, but we can't
965 * rely on the Ethernet address it provides since not all vendors
966 * bother to make it unique. Likewise there's no point in tracking
967 * of the CDC event notifications.
969 return usbnet_get_endpoints (dev, intf);
972 dev_info (&dev->udev->dev, "unsupported MDLM descriptors\n");
976 static const struct driver_info bogus_mdlm_info = {
977 .description = "pseudo-MDLM (BLAN) device",
978 .flags = FLAG_FRAMING_Z,
979 .check_connect = always_connected,
980 .tx_fixup = zaurus_tx_fixup,
981 .bind = blan_mdlm_bind,
986 /* blacklist all those devices */
987 #define ZAURUS_STRONGARM_INFO 0
988 #define ZAURUS_PXA_INFO 0
989 #define OLYMPUS_MXL_INFO 0
994 /*-------------------------------------------------------------------------
996 * Network Device Driver (peer link to "Host Device", from USB host)
998 *-------------------------------------------------------------------------*/
1000 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
1002 struct usbnet *dev = netdev_priv(net);
1003 int ll_mtu = new_mtu + net->hard_header_len;
1005 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
1007 // no second zero-length packet read wanted after mtu-sized packets
1008 if ((ll_mtu % dev->maxpacket) == 0)
1014 /*-------------------------------------------------------------------------*/
1016 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
1018 struct usbnet *dev = netdev_priv(net);
1022 /*-------------------------------------------------------------------------*/
1024 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
1025 * completion callbacks. 2.5 should have fixed those bugs...
1028 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
1030 unsigned long flags;
1032 spin_lock_irqsave(&list->lock, flags);
1033 __skb_unlink(skb, list);
1034 spin_unlock(&list->lock);
1035 spin_lock(&dev->done.lock);
1036 __skb_queue_tail(&dev->done, skb);
1037 if (dev->done.qlen == 1)
1038 tasklet_schedule(&dev->bh);
1039 spin_unlock_irqrestore(&dev->done.lock, flags);
1042 /* some work can't be done in tasklets, so we use keventd
1044 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
1045 * but tasklet_schedule() doesn't. hope the failure is rare.
1047 void usbnet_defer_kevent (struct usbnet *dev, int work)
1049 set_bit (work, &dev->flags);
1050 if (!schedule_work (&dev->kevent))
1051 deverr (dev, "kevent %d may have been dropped", work);
1053 devdbg (dev, "kevent %d scheduled", work);
1055 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
1057 /*-------------------------------------------------------------------------*/
1059 static void rx_complete (struct urb *urb, struct pt_regs *regs);
1061 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
1063 struct sk_buff *skb;
1064 struct skb_data *entry;
1066 unsigned long lockflags;
1067 size_t size = dev->rx_urb_size;
1069 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
1070 if (netif_msg_rx_err (dev))
1071 devdbg (dev, "no rx skb");
1072 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1076 skb_reserve (skb, NET_IP_ALIGN);
1078 entry = (struct skb_data *) skb->cb;
1081 entry->state = rx_start;
1084 usb_fill_bulk_urb (urb, dev->udev, dev->in,
1085 skb->data, size, rx_complete, skb);
1087 spin_lock_irqsave (&dev->rxq.lock, lockflags);
1089 if (netif_running (dev->net)
1090 && netif_device_present (dev->net)
1091 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1092 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
1094 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1097 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1100 if (netif_msg_ifdown (dev))
1101 devdbg (dev, "device gone");
1102 netif_device_detach (dev->net);
1105 if (netif_msg_rx_err (dev))
1106 devdbg (dev, "rx submit, %d", retval);
1107 tasklet_schedule (&dev->bh);
1110 __skb_queue_tail (&dev->rxq, skb);
1113 if (netif_msg_ifdown (dev))
1114 devdbg (dev, "rx: stopped");
1117 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
1119 dev_kfree_skb_any (skb);
1125 /*-------------------------------------------------------------------------*/
1127 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
1129 if (dev->driver_info->rx_fixup
1130 && !dev->driver_info->rx_fixup (dev, skb))
1132 // else network stack removes extra byte if we forced a short packet
1135 usbnet_skb_return (dev, skb);
1137 if (netif_msg_rx_err (dev))
1138 devdbg (dev, "drop");
1140 dev->stats.rx_errors++;
1141 skb_queue_tail (&dev->done, skb);
1145 /*-------------------------------------------------------------------------*/
1147 static void rx_complete (struct urb *urb, struct pt_regs *regs)
1149 struct sk_buff *skb = (struct sk_buff *) urb->context;
1150 struct skb_data *entry = (struct skb_data *) skb->cb;
1151 struct usbnet *dev = entry->dev;
1152 int urb_status = urb->status;
1154 skb_put (skb, urb->actual_length);
1155 entry->state = rx_done;
1158 switch (urb_status) {
1161 if (skb->len < dev->net->hard_header_len) {
1162 entry->state = rx_cleanup;
1163 dev->stats.rx_errors++;
1164 dev->stats.rx_length_errors++;
1165 if (netif_msg_rx_err (dev))
1166 devdbg (dev, "rx length %d", skb->len);
1170 // stalls need manual reset. this is rare ... except that
1171 // when going through USB 2.0 TTs, unplug appears this way.
1172 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
1173 // storm, recovering as needed.
1175 dev->stats.rx_errors++;
1176 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1179 // software-driven interface shutdown
1180 case -ECONNRESET: // async unlink
1181 case -ESHUTDOWN: // hardware gone
1182 if (netif_msg_ifdown (dev))
1183 devdbg (dev, "rx shutdown, code %d", urb_status);
1186 // we get controller i/o faults during khubd disconnect() delays.
1187 // throttle down resubmits, to avoid log floods; just temporarily,
1188 // so we still recover when the fault isn't a khubd delay.
1189 case -EPROTO: // ehci
1190 case -ETIMEDOUT: // ohci
1191 case -EILSEQ: // uhci
1192 dev->stats.rx_errors++;
1193 if (!timer_pending (&dev->delay)) {
1194 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
1195 if (netif_msg_link (dev))
1196 devdbg (dev, "rx throttle %d", urb_status);
1199 entry->state = rx_cleanup;
1204 // data overrun ... flush fifo?
1206 dev->stats.rx_over_errors++;
1210 entry->state = rx_cleanup;
1211 dev->stats.rx_errors++;
1212 if (netif_msg_rx_err (dev))
1213 devdbg (dev, "rx status %d", urb_status);
1217 defer_bh(dev, skb, &dev->rxq);
1220 if (netif_running (dev->net)
1221 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1222 rx_submit (dev, urb, GFP_ATOMIC);
1227 if (netif_msg_rx_err (dev))
1228 devdbg (dev, "no read resubmitted");
1231 static void intr_complete (struct urb *urb, struct pt_regs *regs)
1233 struct usbnet *dev = urb->context;
1234 int status = urb->status;
1239 dev->driver_info->status(dev, urb);
1242 /* software-driven interface shutdown */
1243 case -ENOENT: // urb killed
1244 case -ESHUTDOWN: // hardware gone
1245 if (netif_msg_ifdown (dev))
1246 devdbg (dev, "intr shutdown, code %d", status);
1249 /* NOTE: not throttling like RX/TX, since this endpoint
1250 * already polls infrequently
1253 devdbg (dev, "intr status %d", status);
1257 if (!netif_running (dev->net))
1260 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
1261 status = usb_submit_urb (urb, GFP_ATOMIC);
1262 if (status != 0 && netif_msg_timer (dev))
1263 deverr(dev, "intr resubmit --> %d", status);
1266 /*-------------------------------------------------------------------------*/
1268 // unlink pending rx/tx; completion handlers do all other cleanup
1270 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
1272 unsigned long flags;
1273 struct sk_buff *skb, *skbnext;
1276 spin_lock_irqsave (&q->lock, flags);
1277 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
1278 struct skb_data *entry;
1282 entry = (struct skb_data *) skb->cb;
1284 skbnext = skb->next;
1286 // during some PM-driven resume scenarios,
1287 // these (async) unlinks complete immediately
1288 retval = usb_unlink_urb (urb);
1289 if (retval != -EINPROGRESS && retval != 0)
1290 devdbg (dev, "unlink urb err, %d", retval);
1294 spin_unlock_irqrestore (&q->lock, flags);
1299 /*-------------------------------------------------------------------------*/
1301 // precondition: never called in_interrupt
1303 static int usbnet_stop (struct net_device *net)
1305 struct usbnet *dev = netdev_priv(net);
1307 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
1308 DECLARE_WAITQUEUE (wait, current);
1310 netif_stop_queue (net);
1312 if (netif_msg_ifdown (dev))
1313 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
1314 dev->stats.rx_packets, dev->stats.tx_packets,
1315 dev->stats.rx_errors, dev->stats.tx_errors
1318 // ensure there are no more active urbs
1319 add_wait_queue (&unlink_wakeup, &wait);
1320 dev->wait = &unlink_wakeup;
1321 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
1323 // maybe wait for deletions to finish.
1324 while (!skb_queue_empty(&dev->rxq) &&
1325 !skb_queue_empty(&dev->txq) &&
1326 !skb_queue_empty(&dev->done)) {
1327 msleep(UNLINK_TIMEOUT_MS);
1328 if (netif_msg_ifdown (dev))
1329 devdbg (dev, "waited for %d urb completions", temp);
1332 remove_wait_queue (&unlink_wakeup, &wait);
1334 usb_kill_urb(dev->interrupt);
1336 /* deferred work (task, timer, softirq) must also stop.
1337 * can't flush_scheduled_work() until we drop rtnl (later),
1338 * else workers could deadlock; so make workers a NOP.
1341 del_timer_sync (&dev->delay);
1342 tasklet_kill (&dev->bh);
1347 /*-------------------------------------------------------------------------*/
1349 // posts reads, and enables write queuing
1351 // precondition: never called in_interrupt
1353 static int usbnet_open (struct net_device *net)
1355 struct usbnet *dev = netdev_priv(net);
1357 struct driver_info *info = dev->driver_info;
1359 // put into "known safe" state
1360 if (info->reset && (retval = info->reset (dev)) < 0) {
1361 if (netif_msg_ifup (dev))
1363 "open reset fail (%d) usbnet usb-%s-%s, %s",
1365 dev->udev->bus->bus_name, dev->udev->devpath,
1370 // insist peer be connected
1371 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
1372 if (netif_msg_ifup (dev))
1373 devdbg (dev, "can't open; %d", retval);
1377 /* start any status interrupt transfer */
1378 if (dev->interrupt) {
1379 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
1381 if (netif_msg_ifup (dev))
1382 deverr (dev, "intr submit %d", retval);
1387 netif_start_queue (net);
1388 if (netif_msg_ifup (dev)) {
1391 if (dev->driver_info->flags & FLAG_FRAMING_NC)
1392 framing = "NetChip";
1393 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
1394 framing = "GeneSys";
1395 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
1397 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
1399 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
1404 devinfo (dev, "open: enable queueing "
1405 "(rx %d, tx %d) mtu %d %s framing",
1406 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
1410 // delay posting reads until we're fully open
1411 tasklet_schedule (&dev->bh);
1416 /*-------------------------------------------------------------------------*/
1418 /* ethtool methods; minidrivers may need to add some more, but
1419 * they'll probably want to use this base set.
1422 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1424 struct usbnet *dev = netdev_priv(net);
1426 /* REVISIT don't always return "usbnet" */
1427 strncpy (info->driver, driver_name, sizeof info->driver);
1428 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
1429 strncpy (info->fw_version, dev->driver_info->description,
1430 sizeof info->fw_version);
1431 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1433 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1435 static u32 usbnet_get_link (struct net_device *net)
1437 struct usbnet *dev = netdev_priv(net);
1439 /* If a check_connect is defined, return its result */
1440 if (dev->driver_info->check_connect)
1441 return dev->driver_info->check_connect (dev) == 0;
1443 /* Otherwise, say we're up (to avoid breaking scripts) */
1447 u32 usbnet_get_msglevel (struct net_device *net)
1449 struct usbnet *dev = netdev_priv(net);
1451 return dev->msg_enable;
1453 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1455 void usbnet_set_msglevel (struct net_device *net, u32 level)
1457 struct usbnet *dev = netdev_priv(net);
1459 dev->msg_enable = level;
1461 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1463 /* drivers may override default ethtool_ops in their bind() routine */
1464 static struct ethtool_ops usbnet_ethtool_ops = {
1465 .get_drvinfo = usbnet_get_drvinfo,
1466 .get_link = usbnet_get_link,
1467 .get_msglevel = usbnet_get_msglevel,
1468 .set_msglevel = usbnet_set_msglevel,
1471 /*-------------------------------------------------------------------------*/
1473 /* work that cannot be done in interrupt context uses keventd.
1475 * NOTE: with 2.5 we could do more of this using completion callbacks,
1476 * especially now that control transfers can be queued.
1481 struct usbnet *dev = data;
1484 /* usb_clear_halt() needs a thread context */
1485 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1486 unlink_urbs (dev, &dev->txq);
1487 status = usb_clear_halt (dev->udev, dev->out);
1490 && status != -ESHUTDOWN) {
1491 if (netif_msg_tx_err (dev))
1492 deverr (dev, "can't clear tx halt, status %d",
1495 clear_bit (EVENT_TX_HALT, &dev->flags);
1496 if (status != -ESHUTDOWN)
1497 netif_wake_queue (dev->net);
1500 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1501 unlink_urbs (dev, &dev->rxq);
1502 status = usb_clear_halt (dev->udev, dev->in);
1505 && status != -ESHUTDOWN) {
1506 if (netif_msg_rx_err (dev))
1507 deverr (dev, "can't clear rx halt, status %d",
1510 clear_bit (EVENT_RX_HALT, &dev->flags);
1511 tasklet_schedule (&dev->bh);
1515 /* tasklet could resubmit itself forever if memory is tight */
1516 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1517 struct urb *urb = NULL;
1519 if (netif_running (dev->net))
1520 urb = usb_alloc_urb (0, GFP_KERNEL);
1522 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1524 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1525 rx_submit (dev, urb, GFP_KERNEL);
1526 tasklet_schedule (&dev->bh);
1530 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1531 struct driver_info *info = dev->driver_info;
1534 clear_bit (EVENT_LINK_RESET, &dev->flags);
1535 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1536 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
1538 dev->udev->bus->bus_name, dev->udev->devpath,
1544 devdbg (dev, "kevent done, flags = 0x%lx",
1548 /*-------------------------------------------------------------------------*/
1550 static void tx_complete (struct urb *urb, struct pt_regs *regs)
1552 struct sk_buff *skb = (struct sk_buff *) urb->context;
1553 struct skb_data *entry = (struct skb_data *) skb->cb;
1554 struct usbnet *dev = entry->dev;
1556 if (urb->status == 0) {
1557 dev->stats.tx_packets++;
1558 dev->stats.tx_bytes += entry->length;
1560 dev->stats.tx_errors++;
1562 switch (urb->status) {
1564 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1567 /* software-driven interface shutdown */
1568 case -ECONNRESET: // async unlink
1569 case -ESHUTDOWN: // hardware gone
1572 // like rx, tx gets controller i/o faults during khubd delays
1573 // and so it uses the same throttling mechanism.
1574 case -EPROTO: // ehci
1575 case -ETIMEDOUT: // ohci
1576 case -EILSEQ: // uhci
1577 if (!timer_pending (&dev->delay)) {
1578 mod_timer (&dev->delay,
1579 jiffies + THROTTLE_JIFFIES);
1580 if (netif_msg_link (dev))
1581 devdbg (dev, "tx throttle %d",
1584 netif_stop_queue (dev->net);
1587 if (netif_msg_tx_err (dev))
1588 devdbg (dev, "tx err %d", entry->urb->status);
1594 entry->state = tx_done;
1595 defer_bh(dev, skb, &dev->txq);
1598 /*-------------------------------------------------------------------------*/
1600 static void usbnet_tx_timeout (struct net_device *net)
1602 struct usbnet *dev = netdev_priv(net);
1604 unlink_urbs (dev, &dev->txq);
1605 tasklet_schedule (&dev->bh);
1607 // FIXME: device recovery -- reset?
1610 /*-------------------------------------------------------------------------*/
1612 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
1614 struct usbnet *dev = netdev_priv(net);
1616 int retval = NET_XMIT_SUCCESS;
1617 struct urb *urb = NULL;
1618 struct skb_data *entry;
1619 struct driver_info *info = dev->driver_info;
1620 unsigned long flags;
1622 // some devices want funky USB-level framing, for
1623 // win32 driver (usually) and/or hardware quirks
1624 if (info->tx_fixup) {
1625 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1627 if (netif_msg_tx_err (dev))
1628 devdbg (dev, "can't tx_fixup skb");
1634 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1635 if (netif_msg_tx_err (dev))
1636 devdbg (dev, "no urb");
1640 entry = (struct skb_data *) skb->cb;
1643 entry->state = tx_start;
1644 entry->length = length;
1646 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1647 skb->data, skb->len, tx_complete, skb);
1649 /* don't assume the hardware handles USB_ZERO_PACKET
1650 * NOTE: strictly conforming cdc-ether devices should expect
1651 * the ZLP here, but ignore the one-byte packet.
1653 * FIXME zero that byte, if it doesn't require a new skb.
1655 if ((length % dev->maxpacket) == 0)
1656 urb->transfer_buffer_length++;
1658 spin_lock_irqsave (&dev->txq.lock, flags);
1660 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1662 netif_stop_queue (net);
1663 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1666 if (netif_msg_tx_err (dev))
1667 devdbg (dev, "tx: submit urb err %d", retval);
1670 net->trans_start = jiffies;
1671 __skb_queue_tail (&dev->txq, skb);
1672 if (dev->txq.qlen >= TX_QLEN (dev))
1673 netif_stop_queue (net);
1675 spin_unlock_irqrestore (&dev->txq.lock, flags);
1678 if (netif_msg_tx_err (dev))
1679 devdbg (dev, "drop, code %d", retval);
1681 retval = NET_XMIT_SUCCESS;
1682 dev->stats.tx_dropped++;
1684 dev_kfree_skb_any (skb);
1686 } else if (netif_msg_tx_queued (dev)) {
1687 devdbg (dev, "> tx, len %d, type 0x%x",
1688 length, skb->protocol);
1694 /*-------------------------------------------------------------------------*/
1696 // tasklet (work deferred from completions, in_irq) or timer
1698 static void usbnet_bh (unsigned long param)
1700 struct usbnet *dev = (struct usbnet *) param;
1701 struct sk_buff *skb;
1702 struct skb_data *entry;
1704 while ((skb = skb_dequeue (&dev->done))) {
1705 entry = (struct skb_data *) skb->cb;
1706 switch (entry->state) {
1708 entry->state = rx_cleanup;
1709 rx_process (dev, skb);
1713 usb_free_urb (entry->urb);
1714 dev_kfree_skb (skb);
1717 devdbg (dev, "bogus skb state %d", entry->state);
1721 // waiting for all pending urbs to complete?
1723 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1724 wake_up (dev->wait);
1727 // or are we maybe short a few urbs?
1728 } else if (netif_running (dev->net)
1729 && netif_device_present (dev->net)
1730 && !timer_pending (&dev->delay)
1731 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1732 int temp = dev->rxq.qlen;
1733 int qlen = RX_QLEN (dev);
1739 // don't refill the queue all at once
1740 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1741 urb = usb_alloc_urb (0, GFP_ATOMIC);
1743 rx_submit (dev, urb, GFP_ATOMIC);
1745 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1746 devdbg (dev, "rxqlen %d --> %d",
1747 temp, dev->rxq.qlen);
1748 if (dev->rxq.qlen < qlen)
1749 tasklet_schedule (&dev->bh);
1751 if (dev->txq.qlen < TX_QLEN (dev))
1752 netif_wake_queue (dev->net);
1758 /*-------------------------------------------------------------------------
1760 * USB Device Driver support
1762 *-------------------------------------------------------------------------*/
1764 // precondition: never called in_interrupt
1766 void usbnet_disconnect (struct usb_interface *intf)
1769 struct usb_device *xdev;
1770 struct net_device *net;
1772 dev = usb_get_intfdata(intf);
1773 usb_set_intfdata(intf, NULL);
1777 xdev = interface_to_usbdev (intf);
1779 if (netif_msg_probe (dev))
1780 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1781 intf->dev.driver->name,
1782 xdev->bus->bus_name, xdev->devpath,
1783 dev->driver_info->description);
1786 unregister_netdev (net);
1788 /* we don't hold rtnl here ... */
1789 flush_scheduled_work ();
1791 if (dev->driver_info->unbind)
1792 dev->driver_info->unbind (dev, intf);
1797 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1800 /*-------------------------------------------------------------------------*/
1802 // precondition: never called in_interrupt
1805 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1808 struct net_device *net;
1809 struct usb_host_interface *interface;
1810 struct driver_info *info;
1811 struct usb_device *xdev;
1814 info = (struct driver_info *) prod->driver_info;
1816 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1819 xdev = interface_to_usbdev (udev);
1820 interface = udev->cur_altsetting;
1826 // set up our own records
1827 net = alloc_etherdev(sizeof(*dev));
1829 dbg ("can't kmalloc dev");
1833 dev = netdev_priv(net);
1835 dev->driver_info = info;
1836 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1837 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1838 skb_queue_head_init (&dev->rxq);
1839 skb_queue_head_init (&dev->txq);
1840 skb_queue_head_init (&dev->done);
1841 dev->bh.func = usbnet_bh;
1842 dev->bh.data = (unsigned long) dev;
1843 INIT_WORK (&dev->kevent, kevent, dev);
1844 dev->delay.function = usbnet_bh;
1845 dev->delay.data = (unsigned long) dev;
1846 init_timer (&dev->delay);
1848 SET_MODULE_OWNER (net);
1850 strcpy (net->name, "usb%d");
1851 memcpy (net->dev_addr, node_id, sizeof node_id);
1853 /* rx and tx sides can use different message sizes;
1854 * bind() should set rx_urb_size in that case.
1856 dev->hard_mtu = net->mtu + net->hard_header_len;
1858 // dma_supported() is deeply broken on almost all architectures
1859 // possible with some EHCI controllers
1860 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1861 net->features |= NETIF_F_HIGHDMA;
1864 net->change_mtu = usbnet_change_mtu;
1865 net->get_stats = usbnet_get_stats;
1866 net->hard_start_xmit = usbnet_start_xmit;
1867 net->open = usbnet_open;
1868 net->stop = usbnet_stop;
1869 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1870 net->tx_timeout = usbnet_tx_timeout;
1871 net->ethtool_ops = &usbnet_ethtool_ops;
1873 // allow device-specific bind/init procedures
1874 // NOTE net->name still not usable ...
1876 status = info->bind (dev, udev);
1877 // heuristic: "usb%d" for links we know are two-host,
1878 // else "eth%d" when there's reasonable doubt. userspace
1879 // can rename the link if it knows better.
1880 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1881 && (net->dev_addr [0] & 0x02) == 0)
1882 strcpy (net->name, "eth%d");
1884 /* maybe the remote can't receive an Ethernet MTU */
1885 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1886 net->mtu = dev->hard_mtu - net->hard_header_len;
1887 } else if (!info->in || !info->out)
1888 status = usbnet_get_endpoints (dev, udev);
1890 dev->in = usb_rcvbulkpipe (xdev, info->in);
1891 dev->out = usb_sndbulkpipe (xdev, info->out);
1892 if (!(info->flags & FLAG_NO_SETINT))
1893 status = usb_set_interface (xdev,
1894 interface->desc.bInterfaceNumber,
1895 interface->desc.bAlternateSetting);
1900 if (status == 0 && dev->status)
1901 status = init_status (dev, udev);
1905 if (!dev->rx_urb_size)
1906 dev->rx_urb_size = dev->hard_mtu;
1907 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1909 SET_NETDEV_DEV(net, &udev->dev);
1910 status = register_netdev (net);
1913 if (netif_msg_probe (dev))
1914 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1915 "%02x:%02x:%02x:%02x:%02x:%02x",
1916 udev->dev.driver->name,
1917 xdev->bus->bus_name, xdev->devpath,
1918 dev->driver_info->description,
1919 net->dev_addr [0], net->dev_addr [1],
1920 net->dev_addr [2], net->dev_addr [3],
1921 net->dev_addr [4], net->dev_addr [5]);
1923 // ok, it's ready to go.
1924 usb_set_intfdata (udev, dev);
1926 // start as if the link is up
1927 netif_device_attach (net);
1933 info->unbind (dev, udev);
1940 EXPORT_SYMBOL_GPL(usbnet_probe);
1942 /*-------------------------------------------------------------------------*/
1944 /* FIXME these suspend/resume methods assume non-CDC style
1945 * devices, with only one interface.
1948 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1950 struct usbnet *dev = usb_get_intfdata(intf);
1952 /* accelerate emptying of the rx and queues, to avoid
1953 * having everything error out.
1955 netif_device_detach (dev->net);
1956 (void) unlink_urbs (dev, &dev->rxq);
1957 (void) unlink_urbs (dev, &dev->txq);
1958 intf->dev.power.power_state = PMSG_SUSPEND;
1961 EXPORT_SYMBOL_GPL(usbnet_suspend);
1963 int usbnet_resume (struct usb_interface *intf)
1965 struct usbnet *dev = usb_get_intfdata(intf);
1967 intf->dev.power.power_state = PMSG_ON;
1968 netif_device_attach (dev->net);
1969 tasklet_schedule (&dev->bh);
1972 EXPORT_SYMBOL_GPL(usbnet_resume);
1975 /*-------------------------------------------------------------------------*/
1977 #ifndef HAVE_HARDWARE
1978 #error You need to configure some hardware for this driver
1982 * chip vendor names won't normally be on the cables, and
1983 * may not be on the device.
1986 static const struct usb_device_id products [] = {
1988 #ifdef CONFIG_USB_PL2301
1990 USB_DEVICE (0x067b, 0x0000), // PL-2301
1991 .driver_info = (unsigned long) &prolific_info,
1993 USB_DEVICE (0x067b, 0x0001), // PL-2302
1994 .driver_info = (unsigned long) &prolific_info,
1998 #ifdef CONFIG_USB_RNDIS
2000 /* RNDIS is MSFT's un-official variant of CDC ACM */
2001 USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
2002 .driver_info = (unsigned long) &rndis_info,
2006 #if defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER)
2008 * SA-1100 based Sharp Zaurus ("collie"), or compatible.
2009 * Same idea as above, but different framing.
2011 * PXA-2xx based models are also lying-about-cdc.
2012 * Some models don't even tell the same lies ...
2014 * NOTE: OpenZaurus versions with 2.6 kernels won't use these entries,
2015 * unlike the older ones with 2.4 "embedix" kernels.
2017 * NOTE: These entries do double-duty, serving as blacklist entries
2018 * whenever Zaurus support isn't enabled, but CDC Ethernet is.
2020 #define ZAURUS_MASTER_INTERFACE \
2021 .bInterfaceClass = USB_CLASS_COMM, \
2022 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
2023 .bInterfaceProtocol = USB_CDC_PROTO_NONE
2025 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2026 | USB_DEVICE_ID_MATCH_DEVICE,
2028 .idProduct = 0x8004,
2029 ZAURUS_MASTER_INTERFACE,
2030 .driver_info = ZAURUS_STRONGARM_INFO,
2032 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2033 | USB_DEVICE_ID_MATCH_DEVICE,
2035 .idProduct = 0x8005, /* A-300 */
2036 ZAURUS_MASTER_INTERFACE,
2037 .driver_info = ZAURUS_PXA_INFO,
2039 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2040 | USB_DEVICE_ID_MATCH_DEVICE,
2042 .idProduct = 0x8006, /* B-500/SL-5600 */
2043 ZAURUS_MASTER_INTERFACE,
2044 .driver_info = ZAURUS_PXA_INFO,
2046 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2047 | USB_DEVICE_ID_MATCH_DEVICE,
2049 .idProduct = 0x8007, /* C-700 */
2050 ZAURUS_MASTER_INTERFACE,
2051 .driver_info = ZAURUS_PXA_INFO,
2053 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2054 | USB_DEVICE_ID_MATCH_DEVICE,
2056 .idProduct = 0x9031, /* C-750 C-760 */
2057 ZAURUS_MASTER_INTERFACE,
2058 .driver_info = ZAURUS_PXA_INFO,
2060 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2061 | USB_DEVICE_ID_MATCH_DEVICE,
2063 .idProduct = 0x9032, /* SL-6000 */
2064 ZAURUS_MASTER_INTERFACE,
2065 .driver_info = ZAURUS_PXA_INFO,
2067 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2068 | USB_DEVICE_ID_MATCH_DEVICE,
2070 /* reported with some C860 units */
2071 .idProduct = 0x9050, /* C-860 */
2072 ZAURUS_MASTER_INTERFACE,
2073 .driver_info = ZAURUS_PXA_INFO,
2076 #ifdef CONFIG_USB_ZAURUS
2077 /* At least some (reports vary) PXA units have very different lies
2078 * about their standards support: they claim to be cell phones with
2079 * direct access to their radios. (They don't conform to CDC MDLM.)
2082 USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
2083 USB_CDC_PROTO_NONE),
2084 .driver_info = (unsigned long) &bogus_mdlm_info,
2088 /* Olympus has some models with a Zaurus-compatible option.
2089 * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
2092 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
2093 | USB_DEVICE_ID_MATCH_DEVICE,
2095 .idProduct = 0x0F02, /* R-1000 */
2096 ZAURUS_MASTER_INTERFACE,
2097 .driver_info = OLYMPUS_MXL_INFO,
2101 #ifdef CONFIG_USB_CDCETHER
2103 /* CDC Ether uses two interfaces, not necessarily consecutive.
2104 * We match the main interface, ignoring the optional device
2105 * class so we could handle devices that aren't exclusively
2108 * NOTE: this match must come AFTER entries working around
2109 * bugs/quirks in a given product (like Zaurus, above).
2111 USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
2112 USB_CDC_PROTO_NONE),
2113 .driver_info = (unsigned long) &cdc_info,
2119 MODULE_DEVICE_TABLE (usb, products);
2121 static struct usb_driver usbnet_driver = {
2122 .owner = THIS_MODULE,
2123 .name = driver_name,
2124 .id_table = products,
2125 .probe = usbnet_probe,
2126 .disconnect = usbnet_disconnect,
2127 .suspend = usbnet_suspend,
2128 .resume = usbnet_resume,
2131 /*-------------------------------------------------------------------------*/
2133 static int __init usbnet_init(void)
2135 // compiler should optimize these out
2136 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
2137 < sizeof (struct skb_data));
2138 #ifdef CONFIG_USB_CDCETHER
2139 BUG_ON ((sizeof (((struct usbnet *)0)->data)
2140 < sizeof (struct cdc_state)));
2143 random_ether_addr(node_id);
2145 return usb_register(&usbnet_driver);
2147 module_init(usbnet_init);
2149 static void __exit usbnet_exit(void)
2151 usb_deregister(&usbnet_driver);
2153 module_exit(usbnet_exit);
2155 MODULE_AUTHOR("David Brownell");
2156 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
2157 MODULE_LICENSE("GPL");