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 #ifdef CONFIG_USB_PL2301
305 #define HAVE_HARDWARE
307 /*-------------------------------------------------------------------------
309 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
311 * The protocol and handshaking used here should be bug-compatible
312 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
314 *-------------------------------------------------------------------------*/
317 * Bits 0-4 can be used for software handshaking; they're set from
318 * one end, cleared from the other, "read" with the interrupt byte.
320 #define PL_S_EN (1<<7) /* (feature only) suspend enable */
321 /* reserved bit -- rx ready (6) ? */
322 #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
323 #define PL_RESET_OUT (1<<4) /* reset output pipe */
324 #define PL_RESET_IN (1<<3) /* reset input pipe */
325 #define PL_TX_C (1<<2) /* transmission complete */
326 #define PL_TX_REQ (1<<1) /* transmission received */
327 #define PL_PEER_E (1<<0) /* peer exists */
330 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
332 return usb_control_msg (dev->udev,
333 usb_rcvctrlpipe (dev->udev, 0),
335 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
338 USB_CTRL_GET_TIMEOUT);
342 pl_clear_QuickLink_features (struct usbnet *dev, int val)
344 return pl_vendor_req (dev, 1, (u8) val, 0);
348 pl_set_QuickLink_features (struct usbnet *dev, int val)
350 return pl_vendor_req (dev, 3, (u8) val, 0);
353 /*-------------------------------------------------------------------------*/
355 static int pl_reset (struct usbnet *dev)
357 /* some units seem to need this reset, others reject it utterly.
358 * FIXME be more like "naplink" or windows drivers.
360 (void) pl_set_QuickLink_features (dev,
361 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
365 static const struct driver_info prolific_info = {
366 .description = "Prolific PL-2301/PL-2302",
367 .flags = FLAG_NO_SETINT,
368 /* some PL-2302 versions seem to fail usb_set_interface() */
372 #endif /* CONFIG_USB_PL2301 */
375 /*-------------------------------------------------------------------------
377 * Network Device Driver (peer link to "Host Device", from USB host)
379 *-------------------------------------------------------------------------*/
381 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
383 struct usbnet *dev = netdev_priv(net);
384 int ll_mtu = new_mtu + net->hard_header_len;
386 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
388 // no second zero-length packet read wanted after mtu-sized packets
389 if ((ll_mtu % dev->maxpacket) == 0)
395 /*-------------------------------------------------------------------------*/
397 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
399 struct usbnet *dev = netdev_priv(net);
403 /*-------------------------------------------------------------------------*/
405 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
406 * completion callbacks. 2.5 should have fixed those bugs...
409 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
413 spin_lock_irqsave(&list->lock, flags);
414 __skb_unlink(skb, list);
415 spin_unlock(&list->lock);
416 spin_lock(&dev->done.lock);
417 __skb_queue_tail(&dev->done, skb);
418 if (dev->done.qlen == 1)
419 tasklet_schedule(&dev->bh);
420 spin_unlock_irqrestore(&dev->done.lock, flags);
423 /* some work can't be done in tasklets, so we use keventd
425 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
426 * but tasklet_schedule() doesn't. hope the failure is rare.
428 void usbnet_defer_kevent (struct usbnet *dev, int work)
430 set_bit (work, &dev->flags);
431 if (!schedule_work (&dev->kevent))
432 deverr (dev, "kevent %d may have been dropped", work);
434 devdbg (dev, "kevent %d scheduled", work);
436 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
438 /*-------------------------------------------------------------------------*/
440 static void rx_complete (struct urb *urb, struct pt_regs *regs);
442 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
445 struct skb_data *entry;
447 unsigned long lockflags;
448 size_t size = dev->rx_urb_size;
450 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
451 if (netif_msg_rx_err (dev))
452 devdbg (dev, "no rx skb");
453 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
457 skb_reserve (skb, NET_IP_ALIGN);
459 entry = (struct skb_data *) skb->cb;
462 entry->state = rx_start;
465 usb_fill_bulk_urb (urb, dev->udev, dev->in,
466 skb->data, size, rx_complete, skb);
468 spin_lock_irqsave (&dev->rxq.lock, lockflags);
470 if (netif_running (dev->net)
471 && netif_device_present (dev->net)
472 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
473 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
475 usbnet_defer_kevent (dev, EVENT_RX_HALT);
478 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
481 if (netif_msg_ifdown (dev))
482 devdbg (dev, "device gone");
483 netif_device_detach (dev->net);
486 if (netif_msg_rx_err (dev))
487 devdbg (dev, "rx submit, %d", retval);
488 tasklet_schedule (&dev->bh);
491 __skb_queue_tail (&dev->rxq, skb);
494 if (netif_msg_ifdown (dev))
495 devdbg (dev, "rx: stopped");
498 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
500 dev_kfree_skb_any (skb);
506 /*-------------------------------------------------------------------------*/
508 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
510 if (dev->driver_info->rx_fixup
511 && !dev->driver_info->rx_fixup (dev, skb))
513 // else network stack removes extra byte if we forced a short packet
516 usbnet_skb_return (dev, skb);
518 if (netif_msg_rx_err (dev))
519 devdbg (dev, "drop");
521 dev->stats.rx_errors++;
522 skb_queue_tail (&dev->done, skb);
526 /*-------------------------------------------------------------------------*/
528 static void rx_complete (struct urb *urb, struct pt_regs *regs)
530 struct sk_buff *skb = (struct sk_buff *) urb->context;
531 struct skb_data *entry = (struct skb_data *) skb->cb;
532 struct usbnet *dev = entry->dev;
533 int urb_status = urb->status;
535 skb_put (skb, urb->actual_length);
536 entry->state = rx_done;
539 switch (urb_status) {
542 if (skb->len < dev->net->hard_header_len) {
543 entry->state = rx_cleanup;
544 dev->stats.rx_errors++;
545 dev->stats.rx_length_errors++;
546 if (netif_msg_rx_err (dev))
547 devdbg (dev, "rx length %d", skb->len);
551 // stalls need manual reset. this is rare ... except that
552 // when going through USB 2.0 TTs, unplug appears this way.
553 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
554 // storm, recovering as needed.
556 dev->stats.rx_errors++;
557 usbnet_defer_kevent (dev, EVENT_RX_HALT);
560 // software-driven interface shutdown
561 case -ECONNRESET: // async unlink
562 case -ESHUTDOWN: // hardware gone
563 if (netif_msg_ifdown (dev))
564 devdbg (dev, "rx shutdown, code %d", urb_status);
567 // we get controller i/o faults during khubd disconnect() delays.
568 // throttle down resubmits, to avoid log floods; just temporarily,
569 // so we still recover when the fault isn't a khubd delay.
570 case -EPROTO: // ehci
571 case -ETIMEDOUT: // ohci
572 case -EILSEQ: // uhci
573 dev->stats.rx_errors++;
574 if (!timer_pending (&dev->delay)) {
575 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
576 if (netif_msg_link (dev))
577 devdbg (dev, "rx throttle %d", urb_status);
580 entry->state = rx_cleanup;
585 // data overrun ... flush fifo?
587 dev->stats.rx_over_errors++;
591 entry->state = rx_cleanup;
592 dev->stats.rx_errors++;
593 if (netif_msg_rx_err (dev))
594 devdbg (dev, "rx status %d", urb_status);
598 defer_bh(dev, skb, &dev->rxq);
601 if (netif_running (dev->net)
602 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
603 rx_submit (dev, urb, GFP_ATOMIC);
608 if (netif_msg_rx_err (dev))
609 devdbg (dev, "no read resubmitted");
612 static void intr_complete (struct urb *urb, struct pt_regs *regs)
614 struct usbnet *dev = urb->context;
615 int status = urb->status;
620 dev->driver_info->status(dev, urb);
623 /* software-driven interface shutdown */
624 case -ENOENT: // urb killed
625 case -ESHUTDOWN: // hardware gone
626 if (netif_msg_ifdown (dev))
627 devdbg (dev, "intr shutdown, code %d", status);
630 /* NOTE: not throttling like RX/TX, since this endpoint
631 * already polls infrequently
634 devdbg (dev, "intr status %d", status);
638 if (!netif_running (dev->net))
641 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
642 status = usb_submit_urb (urb, GFP_ATOMIC);
643 if (status != 0 && netif_msg_timer (dev))
644 deverr(dev, "intr resubmit --> %d", status);
647 /*-------------------------------------------------------------------------*/
649 // unlink pending rx/tx; completion handlers do all other cleanup
651 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
654 struct sk_buff *skb, *skbnext;
657 spin_lock_irqsave (&q->lock, flags);
658 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
659 struct skb_data *entry;
663 entry = (struct skb_data *) skb->cb;
667 // during some PM-driven resume scenarios,
668 // these (async) unlinks complete immediately
669 retval = usb_unlink_urb (urb);
670 if (retval != -EINPROGRESS && retval != 0)
671 devdbg (dev, "unlink urb err, %d", retval);
675 spin_unlock_irqrestore (&q->lock, flags);
680 /*-------------------------------------------------------------------------*/
682 // precondition: never called in_interrupt
684 static int usbnet_stop (struct net_device *net)
686 struct usbnet *dev = netdev_priv(net);
688 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
689 DECLARE_WAITQUEUE (wait, current);
691 netif_stop_queue (net);
693 if (netif_msg_ifdown (dev))
694 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
695 dev->stats.rx_packets, dev->stats.tx_packets,
696 dev->stats.rx_errors, dev->stats.tx_errors
699 // ensure there are no more active urbs
700 add_wait_queue (&unlink_wakeup, &wait);
701 dev->wait = &unlink_wakeup;
702 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
704 // maybe wait for deletions to finish.
705 while (!skb_queue_empty(&dev->rxq) &&
706 !skb_queue_empty(&dev->txq) &&
707 !skb_queue_empty(&dev->done)) {
708 msleep(UNLINK_TIMEOUT_MS);
709 if (netif_msg_ifdown (dev))
710 devdbg (dev, "waited for %d urb completions", temp);
713 remove_wait_queue (&unlink_wakeup, &wait);
715 usb_kill_urb(dev->interrupt);
717 /* deferred work (task, timer, softirq) must also stop.
718 * can't flush_scheduled_work() until we drop rtnl (later),
719 * else workers could deadlock; so make workers a NOP.
722 del_timer_sync (&dev->delay);
723 tasklet_kill (&dev->bh);
728 /*-------------------------------------------------------------------------*/
730 // posts reads, and enables write queuing
732 // precondition: never called in_interrupt
734 static int usbnet_open (struct net_device *net)
736 struct usbnet *dev = netdev_priv(net);
738 struct driver_info *info = dev->driver_info;
740 // put into "known safe" state
741 if (info->reset && (retval = info->reset (dev)) < 0) {
742 if (netif_msg_ifup (dev))
744 "open reset fail (%d) usbnet usb-%s-%s, %s",
746 dev->udev->bus->bus_name, dev->udev->devpath,
751 // insist peer be connected
752 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
753 if (netif_msg_ifup (dev))
754 devdbg (dev, "can't open; %d", retval);
758 /* start any status interrupt transfer */
759 if (dev->interrupt) {
760 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
762 if (netif_msg_ifup (dev))
763 deverr (dev, "intr submit %d", retval);
768 netif_start_queue (net);
769 if (netif_msg_ifup (dev)) {
772 if (dev->driver_info->flags & FLAG_FRAMING_NC)
774 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
776 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
778 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
780 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
785 devinfo (dev, "open: enable queueing "
786 "(rx %d, tx %d) mtu %d %s framing",
787 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
791 // delay posting reads until we're fully open
792 tasklet_schedule (&dev->bh);
797 /*-------------------------------------------------------------------------*/
799 /* ethtool methods; minidrivers may need to add some more, but
800 * they'll probably want to use this base set.
803 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
805 struct usbnet *dev = netdev_priv(net);
807 /* REVISIT don't always return "usbnet" */
808 strncpy (info->driver, driver_name, sizeof info->driver);
809 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
810 strncpy (info->fw_version, dev->driver_info->description,
811 sizeof info->fw_version);
812 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
814 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
816 static u32 usbnet_get_link (struct net_device *net)
818 struct usbnet *dev = netdev_priv(net);
820 /* If a check_connect is defined, return its result */
821 if (dev->driver_info->check_connect)
822 return dev->driver_info->check_connect (dev) == 0;
824 /* Otherwise, say we're up (to avoid breaking scripts) */
828 u32 usbnet_get_msglevel (struct net_device *net)
830 struct usbnet *dev = netdev_priv(net);
832 return dev->msg_enable;
834 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
836 void usbnet_set_msglevel (struct net_device *net, u32 level)
838 struct usbnet *dev = netdev_priv(net);
840 dev->msg_enable = level;
842 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
844 /* drivers may override default ethtool_ops in their bind() routine */
845 static struct ethtool_ops usbnet_ethtool_ops = {
846 .get_drvinfo = usbnet_get_drvinfo,
847 .get_link = usbnet_get_link,
848 .get_msglevel = usbnet_get_msglevel,
849 .set_msglevel = usbnet_set_msglevel,
852 /*-------------------------------------------------------------------------*/
854 /* work that cannot be done in interrupt context uses keventd.
856 * NOTE: with 2.5 we could do more of this using completion callbacks,
857 * especially now that control transfers can be queued.
862 struct usbnet *dev = data;
865 /* usb_clear_halt() needs a thread context */
866 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
867 unlink_urbs (dev, &dev->txq);
868 status = usb_clear_halt (dev->udev, dev->out);
871 && status != -ESHUTDOWN) {
872 if (netif_msg_tx_err (dev))
873 deverr (dev, "can't clear tx halt, status %d",
876 clear_bit (EVENT_TX_HALT, &dev->flags);
877 if (status != -ESHUTDOWN)
878 netif_wake_queue (dev->net);
881 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
882 unlink_urbs (dev, &dev->rxq);
883 status = usb_clear_halt (dev->udev, dev->in);
886 && status != -ESHUTDOWN) {
887 if (netif_msg_rx_err (dev))
888 deverr (dev, "can't clear rx halt, status %d",
891 clear_bit (EVENT_RX_HALT, &dev->flags);
892 tasklet_schedule (&dev->bh);
896 /* tasklet could resubmit itself forever if memory is tight */
897 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
898 struct urb *urb = NULL;
900 if (netif_running (dev->net))
901 urb = usb_alloc_urb (0, GFP_KERNEL);
903 clear_bit (EVENT_RX_MEMORY, &dev->flags);
905 clear_bit (EVENT_RX_MEMORY, &dev->flags);
906 rx_submit (dev, urb, GFP_KERNEL);
907 tasklet_schedule (&dev->bh);
911 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
912 struct driver_info *info = dev->driver_info;
915 clear_bit (EVENT_LINK_RESET, &dev->flags);
916 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
917 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
919 dev->udev->bus->bus_name, dev->udev->devpath,
925 devdbg (dev, "kevent done, flags = 0x%lx",
929 /*-------------------------------------------------------------------------*/
931 static void tx_complete (struct urb *urb, struct pt_regs *regs)
933 struct sk_buff *skb = (struct sk_buff *) urb->context;
934 struct skb_data *entry = (struct skb_data *) skb->cb;
935 struct usbnet *dev = entry->dev;
937 if (urb->status == 0) {
938 dev->stats.tx_packets++;
939 dev->stats.tx_bytes += entry->length;
941 dev->stats.tx_errors++;
943 switch (urb->status) {
945 usbnet_defer_kevent (dev, EVENT_TX_HALT);
948 /* software-driven interface shutdown */
949 case -ECONNRESET: // async unlink
950 case -ESHUTDOWN: // hardware gone
953 // like rx, tx gets controller i/o faults during khubd delays
954 // and so it uses the same throttling mechanism.
955 case -EPROTO: // ehci
956 case -ETIMEDOUT: // ohci
957 case -EILSEQ: // uhci
958 if (!timer_pending (&dev->delay)) {
959 mod_timer (&dev->delay,
960 jiffies + THROTTLE_JIFFIES);
961 if (netif_msg_link (dev))
962 devdbg (dev, "tx throttle %d",
965 netif_stop_queue (dev->net);
968 if (netif_msg_tx_err (dev))
969 devdbg (dev, "tx err %d", entry->urb->status);
975 entry->state = tx_done;
976 defer_bh(dev, skb, &dev->txq);
979 /*-------------------------------------------------------------------------*/
981 static void usbnet_tx_timeout (struct net_device *net)
983 struct usbnet *dev = netdev_priv(net);
985 unlink_urbs (dev, &dev->txq);
986 tasklet_schedule (&dev->bh);
988 // FIXME: device recovery -- reset?
991 /*-------------------------------------------------------------------------*/
993 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
995 struct usbnet *dev = netdev_priv(net);
997 int retval = NET_XMIT_SUCCESS;
998 struct urb *urb = NULL;
999 struct skb_data *entry;
1000 struct driver_info *info = dev->driver_info;
1001 unsigned long flags;
1003 // some devices want funky USB-level framing, for
1004 // win32 driver (usually) and/or hardware quirks
1005 if (info->tx_fixup) {
1006 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1008 if (netif_msg_tx_err (dev))
1009 devdbg (dev, "can't tx_fixup skb");
1015 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1016 if (netif_msg_tx_err (dev))
1017 devdbg (dev, "no urb");
1021 entry = (struct skb_data *) skb->cb;
1024 entry->state = tx_start;
1025 entry->length = length;
1027 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1028 skb->data, skb->len, tx_complete, skb);
1030 /* don't assume the hardware handles USB_ZERO_PACKET
1031 * NOTE: strictly conforming cdc-ether devices should expect
1032 * the ZLP here, but ignore the one-byte packet.
1034 * FIXME zero that byte, if it doesn't require a new skb.
1036 if ((length % dev->maxpacket) == 0)
1037 urb->transfer_buffer_length++;
1039 spin_lock_irqsave (&dev->txq.lock, flags);
1041 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1043 netif_stop_queue (net);
1044 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1047 if (netif_msg_tx_err (dev))
1048 devdbg (dev, "tx: submit urb err %d", retval);
1051 net->trans_start = jiffies;
1052 __skb_queue_tail (&dev->txq, skb);
1053 if (dev->txq.qlen >= TX_QLEN (dev))
1054 netif_stop_queue (net);
1056 spin_unlock_irqrestore (&dev->txq.lock, flags);
1059 if (netif_msg_tx_err (dev))
1060 devdbg (dev, "drop, code %d", retval);
1062 retval = NET_XMIT_SUCCESS;
1063 dev->stats.tx_dropped++;
1065 dev_kfree_skb_any (skb);
1067 } else if (netif_msg_tx_queued (dev)) {
1068 devdbg (dev, "> tx, len %d, type 0x%x",
1069 length, skb->protocol);
1075 /*-------------------------------------------------------------------------*/
1077 // tasklet (work deferred from completions, in_irq) or timer
1079 static void usbnet_bh (unsigned long param)
1081 struct usbnet *dev = (struct usbnet *) param;
1082 struct sk_buff *skb;
1083 struct skb_data *entry;
1085 while ((skb = skb_dequeue (&dev->done))) {
1086 entry = (struct skb_data *) skb->cb;
1087 switch (entry->state) {
1089 entry->state = rx_cleanup;
1090 rx_process (dev, skb);
1094 usb_free_urb (entry->urb);
1095 dev_kfree_skb (skb);
1098 devdbg (dev, "bogus skb state %d", entry->state);
1102 // waiting for all pending urbs to complete?
1104 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1105 wake_up (dev->wait);
1108 // or are we maybe short a few urbs?
1109 } else if (netif_running (dev->net)
1110 && netif_device_present (dev->net)
1111 && !timer_pending (&dev->delay)
1112 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1113 int temp = dev->rxq.qlen;
1114 int qlen = RX_QLEN (dev);
1120 // don't refill the queue all at once
1121 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1122 urb = usb_alloc_urb (0, GFP_ATOMIC);
1124 rx_submit (dev, urb, GFP_ATOMIC);
1126 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1127 devdbg (dev, "rxqlen %d --> %d",
1128 temp, dev->rxq.qlen);
1129 if (dev->rxq.qlen < qlen)
1130 tasklet_schedule (&dev->bh);
1132 if (dev->txq.qlen < TX_QLEN (dev))
1133 netif_wake_queue (dev->net);
1139 /*-------------------------------------------------------------------------
1141 * USB Device Driver support
1143 *-------------------------------------------------------------------------*/
1145 // precondition: never called in_interrupt
1147 void usbnet_disconnect (struct usb_interface *intf)
1150 struct usb_device *xdev;
1151 struct net_device *net;
1153 dev = usb_get_intfdata(intf);
1154 usb_set_intfdata(intf, NULL);
1158 xdev = interface_to_usbdev (intf);
1160 if (netif_msg_probe (dev))
1161 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1162 intf->dev.driver->name,
1163 xdev->bus->bus_name, xdev->devpath,
1164 dev->driver_info->description);
1167 unregister_netdev (net);
1169 /* we don't hold rtnl here ... */
1170 flush_scheduled_work ();
1172 if (dev->driver_info->unbind)
1173 dev->driver_info->unbind (dev, intf);
1178 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1181 /*-------------------------------------------------------------------------*/
1183 // precondition: never called in_interrupt
1186 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1189 struct net_device *net;
1190 struct usb_host_interface *interface;
1191 struct driver_info *info;
1192 struct usb_device *xdev;
1195 info = (struct driver_info *) prod->driver_info;
1197 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1200 xdev = interface_to_usbdev (udev);
1201 interface = udev->cur_altsetting;
1207 // set up our own records
1208 net = alloc_etherdev(sizeof(*dev));
1210 dbg ("can't kmalloc dev");
1214 dev = netdev_priv(net);
1216 dev->driver_info = info;
1217 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1218 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1219 skb_queue_head_init (&dev->rxq);
1220 skb_queue_head_init (&dev->txq);
1221 skb_queue_head_init (&dev->done);
1222 dev->bh.func = usbnet_bh;
1223 dev->bh.data = (unsigned long) dev;
1224 INIT_WORK (&dev->kevent, kevent, dev);
1225 dev->delay.function = usbnet_bh;
1226 dev->delay.data = (unsigned long) dev;
1227 init_timer (&dev->delay);
1229 SET_MODULE_OWNER (net);
1231 strcpy (net->name, "usb%d");
1232 memcpy (net->dev_addr, node_id, sizeof node_id);
1234 /* rx and tx sides can use different message sizes;
1235 * bind() should set rx_urb_size in that case.
1237 dev->hard_mtu = net->mtu + net->hard_header_len;
1239 // dma_supported() is deeply broken on almost all architectures
1240 // possible with some EHCI controllers
1241 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1242 net->features |= NETIF_F_HIGHDMA;
1245 net->change_mtu = usbnet_change_mtu;
1246 net->get_stats = usbnet_get_stats;
1247 net->hard_start_xmit = usbnet_start_xmit;
1248 net->open = usbnet_open;
1249 net->stop = usbnet_stop;
1250 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1251 net->tx_timeout = usbnet_tx_timeout;
1252 net->ethtool_ops = &usbnet_ethtool_ops;
1254 // allow device-specific bind/init procedures
1255 // NOTE net->name still not usable ...
1257 status = info->bind (dev, udev);
1258 // heuristic: "usb%d" for links we know are two-host,
1259 // else "eth%d" when there's reasonable doubt. userspace
1260 // can rename the link if it knows better.
1261 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1262 && (net->dev_addr [0] & 0x02) == 0)
1263 strcpy (net->name, "eth%d");
1265 /* maybe the remote can't receive an Ethernet MTU */
1266 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1267 net->mtu = dev->hard_mtu - net->hard_header_len;
1268 } else if (!info->in || !info->out)
1269 status = usbnet_get_endpoints (dev, udev);
1271 dev->in = usb_rcvbulkpipe (xdev, info->in);
1272 dev->out = usb_sndbulkpipe (xdev, info->out);
1273 if (!(info->flags & FLAG_NO_SETINT))
1274 status = usb_set_interface (xdev,
1275 interface->desc.bInterfaceNumber,
1276 interface->desc.bAlternateSetting);
1281 if (status == 0 && dev->status)
1282 status = init_status (dev, udev);
1286 if (!dev->rx_urb_size)
1287 dev->rx_urb_size = dev->hard_mtu;
1288 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1290 SET_NETDEV_DEV(net, &udev->dev);
1291 status = register_netdev (net);
1294 if (netif_msg_probe (dev))
1295 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1296 "%02x:%02x:%02x:%02x:%02x:%02x",
1297 udev->dev.driver->name,
1298 xdev->bus->bus_name, xdev->devpath,
1299 dev->driver_info->description,
1300 net->dev_addr [0], net->dev_addr [1],
1301 net->dev_addr [2], net->dev_addr [3],
1302 net->dev_addr [4], net->dev_addr [5]);
1304 // ok, it's ready to go.
1305 usb_set_intfdata (udev, dev);
1307 // start as if the link is up
1308 netif_device_attach (net);
1314 info->unbind (dev, udev);
1321 EXPORT_SYMBOL_GPL(usbnet_probe);
1323 /*-------------------------------------------------------------------------*/
1325 /* FIXME these suspend/resume methods assume non-CDC style
1326 * devices, with only one interface.
1329 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1331 struct usbnet *dev = usb_get_intfdata(intf);
1333 /* accelerate emptying of the rx and queues, to avoid
1334 * having everything error out.
1336 netif_device_detach (dev->net);
1337 (void) unlink_urbs (dev, &dev->rxq);
1338 (void) unlink_urbs (dev, &dev->txq);
1339 intf->dev.power.power_state = PMSG_SUSPEND;
1342 EXPORT_SYMBOL_GPL(usbnet_suspend);
1344 int usbnet_resume (struct usb_interface *intf)
1346 struct usbnet *dev = usb_get_intfdata(intf);
1348 intf->dev.power.power_state = PMSG_ON;
1349 netif_device_attach (dev->net);
1350 tasklet_schedule (&dev->bh);
1353 EXPORT_SYMBOL_GPL(usbnet_resume);
1356 /*-------------------------------------------------------------------------*/
1358 #ifndef HAVE_HARDWARE
1359 #error You need to configure some hardware for this driver
1363 * chip vendor names won't normally be on the cables, and
1364 * may not be on the device.
1367 static const struct usb_device_id products [] = {
1369 #ifdef CONFIG_USB_PL2301
1371 USB_DEVICE (0x067b, 0x0000), // PL-2301
1372 .driver_info = (unsigned long) &prolific_info,
1374 USB_DEVICE (0x067b, 0x0001), // PL-2302
1375 .driver_info = (unsigned long) &prolific_info,
1379 #ifdef CONFIG_USB_RNDIS
1381 /* RNDIS is MSFT's un-official variant of CDC ACM */
1382 USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
1383 .driver_info = (unsigned long) &rndis_info,
1388 MODULE_DEVICE_TABLE (usb, products);
1390 static struct usb_driver usbnet_driver = {
1391 .owner = THIS_MODULE,
1392 .name = driver_name,
1393 .id_table = products,
1394 .probe = usbnet_probe,
1395 .disconnect = usbnet_disconnect,
1396 .suspend = usbnet_suspend,
1397 .resume = usbnet_resume,
1400 /*-------------------------------------------------------------------------*/
1402 static int __init usbnet_init(void)
1404 // compiler should optimize these out
1405 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1406 < sizeof (struct skb_data));
1408 random_ether_addr(node_id);
1410 return usb_register(&usbnet_driver);
1412 module_init(usbnet_init);
1414 static void __exit usbnet_exit(void)
1416 usb_deregister(&usbnet_driver);
1418 module_exit(usbnet_exit);
1420 MODULE_AUTHOR("David Brownell");
1421 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
1422 MODULE_LICENSE("GPL");