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).
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 /* handles CDC Ethernet and many other network "bulk data" interfaces */
177 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
180 struct usb_host_interface *alt = NULL;
181 struct usb_host_endpoint *in = NULL, *out = NULL;
182 struct usb_host_endpoint *status = NULL;
184 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
187 in = out = status = NULL;
188 alt = intf->altsetting + tmp;
190 /* take the first altsetting with in-bulk + out-bulk;
191 * remember any status endpoint, just in case;
192 * ignore other endpoints and altsetttings.
194 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
195 struct usb_host_endpoint *e;
198 e = alt->endpoint + ep;
199 switch (e->desc.bmAttributes) {
200 case USB_ENDPOINT_XFER_INT:
201 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
205 case USB_ENDPOINT_XFER_BULK:
210 if (e->desc.bEndpointAddress & USB_DIR_IN) {
213 else if (intr && !status)
223 if (!alt || !in || !out)
226 if (alt->desc.bAlternateSetting != 0
227 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
228 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
229 alt->desc.bAlternateSetting);
234 dev->in = usb_rcvbulkpipe (dev->udev,
235 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
236 dev->out = usb_sndbulkpipe (dev->udev,
237 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
238 dev->status = status;
241 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
243 static void intr_complete (struct urb *urb, struct pt_regs *regs);
245 static int init_status (struct usbnet *dev, struct usb_interface *intf)
252 if (!dev->driver_info->status)
255 pipe = usb_rcvintpipe (dev->udev,
256 dev->status->desc.bEndpointAddress
257 & USB_ENDPOINT_NUMBER_MASK);
258 maxp = usb_maxpacket (dev->udev, pipe, 0);
260 /* avoid 1 msec chatter: min 8 msec poll rate */
261 period = max ((int) dev->status->desc.bInterval,
262 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
264 buf = kmalloc (maxp, SLAB_KERNEL);
266 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
267 if (!dev->interrupt) {
271 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
272 buf, maxp, intr_complete, dev, period);
274 "status ep%din, %d bytes period %d\n",
275 usb_pipeendpoint(pipe), maxp, period);
281 /* Passes this packet up the stack, updating its accounting.
282 * Some link protocols batch packets, so their rx_fixup paths
283 * can return clones as well as just modify the original skb.
285 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
290 skb->protocol = eth_type_trans (skb, dev->net);
291 dev->stats.rx_packets++;
292 dev->stats.rx_bytes += skb->len;
294 if (netif_msg_rx_status (dev))
295 devdbg (dev, "< rx, len %zu, type 0x%x",
296 skb->len + sizeof (struct ethhdr), skb->protocol);
297 memset (skb->cb, 0, sizeof (struct skb_data));
298 status = netif_rx (skb);
299 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
300 devdbg (dev, "netif_rx status %d", status);
302 EXPORT_SYMBOL_GPL(usbnet_skb_return);
305 #ifdef CONFIG_USB_PL2301
306 #define HAVE_HARDWARE
308 /*-------------------------------------------------------------------------
310 * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
312 * The protocol and handshaking used here should be bug-compatible
313 * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
315 *-------------------------------------------------------------------------*/
318 * Bits 0-4 can be used for software handshaking; they're set from
319 * one end, cleared from the other, "read" with the interrupt byte.
321 #define PL_S_EN (1<<7) /* (feature only) suspend enable */
322 /* reserved bit -- rx ready (6) ? */
323 #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
324 #define PL_RESET_OUT (1<<4) /* reset output pipe */
325 #define PL_RESET_IN (1<<3) /* reset input pipe */
326 #define PL_TX_C (1<<2) /* transmission complete */
327 #define PL_TX_REQ (1<<1) /* transmission received */
328 #define PL_PEER_E (1<<0) /* peer exists */
331 pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
333 return usb_control_msg (dev->udev,
334 usb_rcvctrlpipe (dev->udev, 0),
336 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
339 USB_CTRL_GET_TIMEOUT);
343 pl_clear_QuickLink_features (struct usbnet *dev, int val)
345 return pl_vendor_req (dev, 1, (u8) val, 0);
349 pl_set_QuickLink_features (struct usbnet *dev, int val)
351 return pl_vendor_req (dev, 3, (u8) val, 0);
354 /*-------------------------------------------------------------------------*/
356 static int pl_reset (struct usbnet *dev)
358 /* some units seem to need this reset, others reject it utterly.
359 * FIXME be more like "naplink" or windows drivers.
361 (void) pl_set_QuickLink_features (dev,
362 PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
366 static const struct driver_info prolific_info = {
367 .description = "Prolific PL-2301/PL-2302",
368 .flags = FLAG_NO_SETINT,
369 /* some PL-2302 versions seem to fail usb_set_interface() */
373 #endif /* CONFIG_USB_PL2301 */
376 /*-------------------------------------------------------------------------
378 * Network Device Driver (peer link to "Host Device", from USB host)
380 *-------------------------------------------------------------------------*/
382 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
384 struct usbnet *dev = netdev_priv(net);
385 int ll_mtu = new_mtu + net->hard_header_len;
387 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
389 // no second zero-length packet read wanted after mtu-sized packets
390 if ((ll_mtu % dev->maxpacket) == 0)
396 /*-------------------------------------------------------------------------*/
398 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
400 struct usbnet *dev = netdev_priv(net);
404 /*-------------------------------------------------------------------------*/
406 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
407 * completion callbacks. 2.5 should have fixed those bugs...
410 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
414 spin_lock_irqsave(&list->lock, flags);
415 __skb_unlink(skb, list);
416 spin_unlock(&list->lock);
417 spin_lock(&dev->done.lock);
418 __skb_queue_tail(&dev->done, skb);
419 if (dev->done.qlen == 1)
420 tasklet_schedule(&dev->bh);
421 spin_unlock_irqrestore(&dev->done.lock, flags);
424 /* some work can't be done in tasklets, so we use keventd
426 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
427 * but tasklet_schedule() doesn't. hope the failure is rare.
429 void usbnet_defer_kevent (struct usbnet *dev, int work)
431 set_bit (work, &dev->flags);
432 if (!schedule_work (&dev->kevent))
433 deverr (dev, "kevent %d may have been dropped", work);
435 devdbg (dev, "kevent %d scheduled", work);
437 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
439 /*-------------------------------------------------------------------------*/
441 static void rx_complete (struct urb *urb, struct pt_regs *regs);
443 static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
446 struct skb_data *entry;
448 unsigned long lockflags;
449 size_t size = dev->rx_urb_size;
451 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
452 if (netif_msg_rx_err (dev))
453 devdbg (dev, "no rx skb");
454 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
458 skb_reserve (skb, NET_IP_ALIGN);
460 entry = (struct skb_data *) skb->cb;
463 entry->state = rx_start;
466 usb_fill_bulk_urb (urb, dev->udev, dev->in,
467 skb->data, size, rx_complete, skb);
469 spin_lock_irqsave (&dev->rxq.lock, lockflags);
471 if (netif_running (dev->net)
472 && netif_device_present (dev->net)
473 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
474 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
476 usbnet_defer_kevent (dev, EVENT_RX_HALT);
479 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
482 if (netif_msg_ifdown (dev))
483 devdbg (dev, "device gone");
484 netif_device_detach (dev->net);
487 if (netif_msg_rx_err (dev))
488 devdbg (dev, "rx submit, %d", retval);
489 tasklet_schedule (&dev->bh);
492 __skb_queue_tail (&dev->rxq, skb);
495 if (netif_msg_ifdown (dev))
496 devdbg (dev, "rx: stopped");
499 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
501 dev_kfree_skb_any (skb);
507 /*-------------------------------------------------------------------------*/
509 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
511 if (dev->driver_info->rx_fixup
512 && !dev->driver_info->rx_fixup (dev, skb))
514 // else network stack removes extra byte if we forced a short packet
517 usbnet_skb_return (dev, skb);
519 if (netif_msg_rx_err (dev))
520 devdbg (dev, "drop");
522 dev->stats.rx_errors++;
523 skb_queue_tail (&dev->done, skb);
527 /*-------------------------------------------------------------------------*/
529 static void rx_complete (struct urb *urb, struct pt_regs *regs)
531 struct sk_buff *skb = (struct sk_buff *) urb->context;
532 struct skb_data *entry = (struct skb_data *) skb->cb;
533 struct usbnet *dev = entry->dev;
534 int urb_status = urb->status;
536 skb_put (skb, urb->actual_length);
537 entry->state = rx_done;
540 switch (urb_status) {
543 if (skb->len < dev->net->hard_header_len) {
544 entry->state = rx_cleanup;
545 dev->stats.rx_errors++;
546 dev->stats.rx_length_errors++;
547 if (netif_msg_rx_err (dev))
548 devdbg (dev, "rx length %d", skb->len);
552 // stalls need manual reset. this is rare ... except that
553 // when going through USB 2.0 TTs, unplug appears this way.
554 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
555 // storm, recovering as needed.
557 dev->stats.rx_errors++;
558 usbnet_defer_kevent (dev, EVENT_RX_HALT);
561 // software-driven interface shutdown
562 case -ECONNRESET: // async unlink
563 case -ESHUTDOWN: // hardware gone
564 if (netif_msg_ifdown (dev))
565 devdbg (dev, "rx shutdown, code %d", urb_status);
568 // we get controller i/o faults during khubd disconnect() delays.
569 // throttle down resubmits, to avoid log floods; just temporarily,
570 // so we still recover when the fault isn't a khubd delay.
571 case -EPROTO: // ehci
572 case -ETIMEDOUT: // ohci
573 case -EILSEQ: // uhci
574 dev->stats.rx_errors++;
575 if (!timer_pending (&dev->delay)) {
576 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
577 if (netif_msg_link (dev))
578 devdbg (dev, "rx throttle %d", urb_status);
581 entry->state = rx_cleanup;
586 // data overrun ... flush fifo?
588 dev->stats.rx_over_errors++;
592 entry->state = rx_cleanup;
593 dev->stats.rx_errors++;
594 if (netif_msg_rx_err (dev))
595 devdbg (dev, "rx status %d", urb_status);
599 defer_bh(dev, skb, &dev->rxq);
602 if (netif_running (dev->net)
603 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
604 rx_submit (dev, urb, GFP_ATOMIC);
609 if (netif_msg_rx_err (dev))
610 devdbg (dev, "no read resubmitted");
613 static void intr_complete (struct urb *urb, struct pt_regs *regs)
615 struct usbnet *dev = urb->context;
616 int status = urb->status;
621 dev->driver_info->status(dev, urb);
624 /* software-driven interface shutdown */
625 case -ENOENT: // urb killed
626 case -ESHUTDOWN: // hardware gone
627 if (netif_msg_ifdown (dev))
628 devdbg (dev, "intr shutdown, code %d", status);
631 /* NOTE: not throttling like RX/TX, since this endpoint
632 * already polls infrequently
635 devdbg (dev, "intr status %d", status);
639 if (!netif_running (dev->net))
642 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
643 status = usb_submit_urb (urb, GFP_ATOMIC);
644 if (status != 0 && netif_msg_timer (dev))
645 deverr(dev, "intr resubmit --> %d", status);
648 /*-------------------------------------------------------------------------*/
650 // unlink pending rx/tx; completion handlers do all other cleanup
652 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
655 struct sk_buff *skb, *skbnext;
658 spin_lock_irqsave (&q->lock, flags);
659 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
660 struct skb_data *entry;
664 entry = (struct skb_data *) skb->cb;
668 // during some PM-driven resume scenarios,
669 // these (async) unlinks complete immediately
670 retval = usb_unlink_urb (urb);
671 if (retval != -EINPROGRESS && retval != 0)
672 devdbg (dev, "unlink urb err, %d", retval);
676 spin_unlock_irqrestore (&q->lock, flags);
681 /*-------------------------------------------------------------------------*/
683 // precondition: never called in_interrupt
685 static int usbnet_stop (struct net_device *net)
687 struct usbnet *dev = netdev_priv(net);
689 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
690 DECLARE_WAITQUEUE (wait, current);
692 netif_stop_queue (net);
694 if (netif_msg_ifdown (dev))
695 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
696 dev->stats.rx_packets, dev->stats.tx_packets,
697 dev->stats.rx_errors, dev->stats.tx_errors
700 // ensure there are no more active urbs
701 add_wait_queue (&unlink_wakeup, &wait);
702 dev->wait = &unlink_wakeup;
703 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
705 // maybe wait for deletions to finish.
706 while (!skb_queue_empty(&dev->rxq) &&
707 !skb_queue_empty(&dev->txq) &&
708 !skb_queue_empty(&dev->done)) {
709 msleep(UNLINK_TIMEOUT_MS);
710 if (netif_msg_ifdown (dev))
711 devdbg (dev, "waited for %d urb completions", temp);
714 remove_wait_queue (&unlink_wakeup, &wait);
716 usb_kill_urb(dev->interrupt);
718 /* deferred work (task, timer, softirq) must also stop.
719 * can't flush_scheduled_work() until we drop rtnl (later),
720 * else workers could deadlock; so make workers a NOP.
723 del_timer_sync (&dev->delay);
724 tasklet_kill (&dev->bh);
729 /*-------------------------------------------------------------------------*/
731 // posts reads, and enables write queuing
733 // precondition: never called in_interrupt
735 static int usbnet_open (struct net_device *net)
737 struct usbnet *dev = netdev_priv(net);
739 struct driver_info *info = dev->driver_info;
741 // put into "known safe" state
742 if (info->reset && (retval = info->reset (dev)) < 0) {
743 if (netif_msg_ifup (dev))
745 "open reset fail (%d) usbnet usb-%s-%s, %s",
747 dev->udev->bus->bus_name, dev->udev->devpath,
752 // insist peer be connected
753 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
754 if (netif_msg_ifup (dev))
755 devdbg (dev, "can't open; %d", retval);
759 /* start any status interrupt transfer */
760 if (dev->interrupt) {
761 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
763 if (netif_msg_ifup (dev))
764 deverr (dev, "intr submit %d", retval);
769 netif_start_queue (net);
770 if (netif_msg_ifup (dev)) {
773 if (dev->driver_info->flags & FLAG_FRAMING_NC)
775 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
777 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
779 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
781 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
786 devinfo (dev, "open: enable queueing "
787 "(rx %d, tx %d) mtu %d %s framing",
788 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
792 // delay posting reads until we're fully open
793 tasklet_schedule (&dev->bh);
798 /*-------------------------------------------------------------------------*/
800 /* ethtool methods; minidrivers may need to add some more, but
801 * they'll probably want to use this base set.
804 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
806 struct usbnet *dev = netdev_priv(net);
808 /* REVISIT don't always return "usbnet" */
809 strncpy (info->driver, driver_name, sizeof info->driver);
810 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
811 strncpy (info->fw_version, dev->driver_info->description,
812 sizeof info->fw_version);
813 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
815 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
817 static u32 usbnet_get_link (struct net_device *net)
819 struct usbnet *dev = netdev_priv(net);
821 /* If a check_connect is defined, return its result */
822 if (dev->driver_info->check_connect)
823 return dev->driver_info->check_connect (dev) == 0;
825 /* Otherwise, say we're up (to avoid breaking scripts) */
829 u32 usbnet_get_msglevel (struct net_device *net)
831 struct usbnet *dev = netdev_priv(net);
833 return dev->msg_enable;
835 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
837 void usbnet_set_msglevel (struct net_device *net, u32 level)
839 struct usbnet *dev = netdev_priv(net);
841 dev->msg_enable = level;
843 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
845 /* drivers may override default ethtool_ops in their bind() routine */
846 static struct ethtool_ops usbnet_ethtool_ops = {
847 .get_drvinfo = usbnet_get_drvinfo,
848 .get_link = usbnet_get_link,
849 .get_msglevel = usbnet_get_msglevel,
850 .set_msglevel = usbnet_set_msglevel,
853 /*-------------------------------------------------------------------------*/
855 /* work that cannot be done in interrupt context uses keventd.
857 * NOTE: with 2.5 we could do more of this using completion callbacks,
858 * especially now that control transfers can be queued.
863 struct usbnet *dev = data;
866 /* usb_clear_halt() needs a thread context */
867 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
868 unlink_urbs (dev, &dev->txq);
869 status = usb_clear_halt (dev->udev, dev->out);
872 && status != -ESHUTDOWN) {
873 if (netif_msg_tx_err (dev))
874 deverr (dev, "can't clear tx halt, status %d",
877 clear_bit (EVENT_TX_HALT, &dev->flags);
878 if (status != -ESHUTDOWN)
879 netif_wake_queue (dev->net);
882 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
883 unlink_urbs (dev, &dev->rxq);
884 status = usb_clear_halt (dev->udev, dev->in);
887 && status != -ESHUTDOWN) {
888 if (netif_msg_rx_err (dev))
889 deverr (dev, "can't clear rx halt, status %d",
892 clear_bit (EVENT_RX_HALT, &dev->flags);
893 tasklet_schedule (&dev->bh);
897 /* tasklet could resubmit itself forever if memory is tight */
898 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
899 struct urb *urb = NULL;
901 if (netif_running (dev->net))
902 urb = usb_alloc_urb (0, GFP_KERNEL);
904 clear_bit (EVENT_RX_MEMORY, &dev->flags);
906 clear_bit (EVENT_RX_MEMORY, &dev->flags);
907 rx_submit (dev, urb, GFP_KERNEL);
908 tasklet_schedule (&dev->bh);
912 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
913 struct driver_info *info = dev->driver_info;
916 clear_bit (EVENT_LINK_RESET, &dev->flags);
917 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
918 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
920 dev->udev->bus->bus_name, dev->udev->devpath,
926 devdbg (dev, "kevent done, flags = 0x%lx",
930 /*-------------------------------------------------------------------------*/
932 static void tx_complete (struct urb *urb, struct pt_regs *regs)
934 struct sk_buff *skb = (struct sk_buff *) urb->context;
935 struct skb_data *entry = (struct skb_data *) skb->cb;
936 struct usbnet *dev = entry->dev;
938 if (urb->status == 0) {
939 dev->stats.tx_packets++;
940 dev->stats.tx_bytes += entry->length;
942 dev->stats.tx_errors++;
944 switch (urb->status) {
946 usbnet_defer_kevent (dev, EVENT_TX_HALT);
949 /* software-driven interface shutdown */
950 case -ECONNRESET: // async unlink
951 case -ESHUTDOWN: // hardware gone
954 // like rx, tx gets controller i/o faults during khubd delays
955 // and so it uses the same throttling mechanism.
956 case -EPROTO: // ehci
957 case -ETIMEDOUT: // ohci
958 case -EILSEQ: // uhci
959 if (!timer_pending (&dev->delay)) {
960 mod_timer (&dev->delay,
961 jiffies + THROTTLE_JIFFIES);
962 if (netif_msg_link (dev))
963 devdbg (dev, "tx throttle %d",
966 netif_stop_queue (dev->net);
969 if (netif_msg_tx_err (dev))
970 devdbg (dev, "tx err %d", entry->urb->status);
976 entry->state = tx_done;
977 defer_bh(dev, skb, &dev->txq);
980 /*-------------------------------------------------------------------------*/
982 static void usbnet_tx_timeout (struct net_device *net)
984 struct usbnet *dev = netdev_priv(net);
986 unlink_urbs (dev, &dev->txq);
987 tasklet_schedule (&dev->bh);
989 // FIXME: device recovery -- reset?
992 /*-------------------------------------------------------------------------*/
994 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
996 struct usbnet *dev = netdev_priv(net);
998 int retval = NET_XMIT_SUCCESS;
999 struct urb *urb = NULL;
1000 struct skb_data *entry;
1001 struct driver_info *info = dev->driver_info;
1002 unsigned long flags;
1004 // some devices want funky USB-level framing, for
1005 // win32 driver (usually) and/or hardware quirks
1006 if (info->tx_fixup) {
1007 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1009 if (netif_msg_tx_err (dev))
1010 devdbg (dev, "can't tx_fixup skb");
1016 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1017 if (netif_msg_tx_err (dev))
1018 devdbg (dev, "no urb");
1022 entry = (struct skb_data *) skb->cb;
1025 entry->state = tx_start;
1026 entry->length = length;
1028 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1029 skb->data, skb->len, tx_complete, skb);
1031 /* don't assume the hardware handles USB_ZERO_PACKET
1032 * NOTE: strictly conforming cdc-ether devices should expect
1033 * the ZLP here, but ignore the one-byte packet.
1035 * FIXME zero that byte, if it doesn't require a new skb.
1037 if ((length % dev->maxpacket) == 0)
1038 urb->transfer_buffer_length++;
1040 spin_lock_irqsave (&dev->txq.lock, flags);
1042 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1044 netif_stop_queue (net);
1045 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1048 if (netif_msg_tx_err (dev))
1049 devdbg (dev, "tx: submit urb err %d", retval);
1052 net->trans_start = jiffies;
1053 __skb_queue_tail (&dev->txq, skb);
1054 if (dev->txq.qlen >= TX_QLEN (dev))
1055 netif_stop_queue (net);
1057 spin_unlock_irqrestore (&dev->txq.lock, flags);
1060 if (netif_msg_tx_err (dev))
1061 devdbg (dev, "drop, code %d", retval);
1063 retval = NET_XMIT_SUCCESS;
1064 dev->stats.tx_dropped++;
1066 dev_kfree_skb_any (skb);
1068 } else if (netif_msg_tx_queued (dev)) {
1069 devdbg (dev, "> tx, len %d, type 0x%x",
1070 length, skb->protocol);
1076 /*-------------------------------------------------------------------------*/
1078 // tasklet (work deferred from completions, in_irq) or timer
1080 static void usbnet_bh (unsigned long param)
1082 struct usbnet *dev = (struct usbnet *) param;
1083 struct sk_buff *skb;
1084 struct skb_data *entry;
1086 while ((skb = skb_dequeue (&dev->done))) {
1087 entry = (struct skb_data *) skb->cb;
1088 switch (entry->state) {
1090 entry->state = rx_cleanup;
1091 rx_process (dev, skb);
1095 usb_free_urb (entry->urb);
1096 dev_kfree_skb (skb);
1099 devdbg (dev, "bogus skb state %d", entry->state);
1103 // waiting for all pending urbs to complete?
1105 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1106 wake_up (dev->wait);
1109 // or are we maybe short a few urbs?
1110 } else if (netif_running (dev->net)
1111 && netif_device_present (dev->net)
1112 && !timer_pending (&dev->delay)
1113 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1114 int temp = dev->rxq.qlen;
1115 int qlen = RX_QLEN (dev);
1121 // don't refill the queue all at once
1122 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1123 urb = usb_alloc_urb (0, GFP_ATOMIC);
1125 rx_submit (dev, urb, GFP_ATOMIC);
1127 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1128 devdbg (dev, "rxqlen %d --> %d",
1129 temp, dev->rxq.qlen);
1130 if (dev->rxq.qlen < qlen)
1131 tasklet_schedule (&dev->bh);
1133 if (dev->txq.qlen < TX_QLEN (dev))
1134 netif_wake_queue (dev->net);
1140 /*-------------------------------------------------------------------------
1142 * USB Device Driver support
1144 *-------------------------------------------------------------------------*/
1146 // precondition: never called in_interrupt
1148 void usbnet_disconnect (struct usb_interface *intf)
1151 struct usb_device *xdev;
1152 struct net_device *net;
1154 dev = usb_get_intfdata(intf);
1155 usb_set_intfdata(intf, NULL);
1159 xdev = interface_to_usbdev (intf);
1161 if (netif_msg_probe (dev))
1162 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1163 intf->dev.driver->name,
1164 xdev->bus->bus_name, xdev->devpath,
1165 dev->driver_info->description);
1168 unregister_netdev (net);
1170 /* we don't hold rtnl here ... */
1171 flush_scheduled_work ();
1173 if (dev->driver_info->unbind)
1174 dev->driver_info->unbind (dev, intf);
1179 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1182 /*-------------------------------------------------------------------------*/
1184 // precondition: never called in_interrupt
1187 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1190 struct net_device *net;
1191 struct usb_host_interface *interface;
1192 struct driver_info *info;
1193 struct usb_device *xdev;
1196 info = (struct driver_info *) prod->driver_info;
1198 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1201 xdev = interface_to_usbdev (udev);
1202 interface = udev->cur_altsetting;
1208 // set up our own records
1209 net = alloc_etherdev(sizeof(*dev));
1211 dbg ("can't kmalloc dev");
1215 dev = netdev_priv(net);
1217 dev->driver_info = info;
1218 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1219 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1220 skb_queue_head_init (&dev->rxq);
1221 skb_queue_head_init (&dev->txq);
1222 skb_queue_head_init (&dev->done);
1223 dev->bh.func = usbnet_bh;
1224 dev->bh.data = (unsigned long) dev;
1225 INIT_WORK (&dev->kevent, kevent, dev);
1226 dev->delay.function = usbnet_bh;
1227 dev->delay.data = (unsigned long) dev;
1228 init_timer (&dev->delay);
1230 SET_MODULE_OWNER (net);
1232 strcpy (net->name, "usb%d");
1233 memcpy (net->dev_addr, node_id, sizeof node_id);
1235 /* rx and tx sides can use different message sizes;
1236 * bind() should set rx_urb_size in that case.
1238 dev->hard_mtu = net->mtu + net->hard_header_len;
1240 // dma_supported() is deeply broken on almost all architectures
1241 // possible with some EHCI controllers
1242 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1243 net->features |= NETIF_F_HIGHDMA;
1246 net->change_mtu = usbnet_change_mtu;
1247 net->get_stats = usbnet_get_stats;
1248 net->hard_start_xmit = usbnet_start_xmit;
1249 net->open = usbnet_open;
1250 net->stop = usbnet_stop;
1251 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1252 net->tx_timeout = usbnet_tx_timeout;
1253 net->ethtool_ops = &usbnet_ethtool_ops;
1255 // allow device-specific bind/init procedures
1256 // NOTE net->name still not usable ...
1258 status = info->bind (dev, udev);
1259 // heuristic: "usb%d" for links we know are two-host,
1260 // else "eth%d" when there's reasonable doubt. userspace
1261 // can rename the link if it knows better.
1262 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1263 && (net->dev_addr [0] & 0x02) == 0)
1264 strcpy (net->name, "eth%d");
1266 /* maybe the remote can't receive an Ethernet MTU */
1267 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1268 net->mtu = dev->hard_mtu - net->hard_header_len;
1269 } else if (!info->in || !info->out)
1270 status = usbnet_get_endpoints (dev, udev);
1272 dev->in = usb_rcvbulkpipe (xdev, info->in);
1273 dev->out = usb_sndbulkpipe (xdev, info->out);
1274 if (!(info->flags & FLAG_NO_SETINT))
1275 status = usb_set_interface (xdev,
1276 interface->desc.bInterfaceNumber,
1277 interface->desc.bAlternateSetting);
1282 if (status == 0 && dev->status)
1283 status = init_status (dev, udev);
1287 if (!dev->rx_urb_size)
1288 dev->rx_urb_size = dev->hard_mtu;
1289 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1291 SET_NETDEV_DEV(net, &udev->dev);
1292 status = register_netdev (net);
1295 if (netif_msg_probe (dev))
1296 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1297 "%02x:%02x:%02x:%02x:%02x:%02x",
1298 udev->dev.driver->name,
1299 xdev->bus->bus_name, xdev->devpath,
1300 dev->driver_info->description,
1301 net->dev_addr [0], net->dev_addr [1],
1302 net->dev_addr [2], net->dev_addr [3],
1303 net->dev_addr [4], net->dev_addr [5]);
1305 // ok, it's ready to go.
1306 usb_set_intfdata (udev, dev);
1308 // start as if the link is up
1309 netif_device_attach (net);
1315 info->unbind (dev, udev);
1322 EXPORT_SYMBOL_GPL(usbnet_probe);
1324 /*-------------------------------------------------------------------------*/
1326 /* FIXME these suspend/resume methods assume non-CDC style
1327 * devices, with only one interface.
1330 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1332 struct usbnet *dev = usb_get_intfdata(intf);
1334 /* accelerate emptying of the rx and queues, to avoid
1335 * having everything error out.
1337 netif_device_detach (dev->net);
1338 (void) unlink_urbs (dev, &dev->rxq);
1339 (void) unlink_urbs (dev, &dev->txq);
1340 intf->dev.power.power_state = PMSG_SUSPEND;
1343 EXPORT_SYMBOL_GPL(usbnet_suspend);
1345 int usbnet_resume (struct usb_interface *intf)
1347 struct usbnet *dev = usb_get_intfdata(intf);
1349 intf->dev.power.power_state = PMSG_ON;
1350 netif_device_attach (dev->net);
1351 tasklet_schedule (&dev->bh);
1354 EXPORT_SYMBOL_GPL(usbnet_resume);
1357 /*-------------------------------------------------------------------------*/
1359 #ifndef HAVE_HARDWARE
1360 #error You need to configure some hardware for this driver
1364 * chip vendor names won't normally be on the cables, and
1365 * may not be on the device.
1368 static const struct usb_device_id products [] = {
1370 #ifdef CONFIG_USB_PL2301
1372 USB_DEVICE (0x067b, 0x0000), // PL-2301
1373 .driver_info = (unsigned long) &prolific_info,
1375 USB_DEVICE (0x067b, 0x0001), // PL-2302
1376 .driver_info = (unsigned long) &prolific_info,
1381 MODULE_DEVICE_TABLE (usb, products);
1383 static struct usb_driver usbnet_driver = {
1384 .owner = THIS_MODULE,
1385 .name = driver_name,
1386 .id_table = products,
1387 .probe = usbnet_probe,
1388 .disconnect = usbnet_disconnect,
1389 .suspend = usbnet_suspend,
1390 .resume = usbnet_resume,
1393 /*-------------------------------------------------------------------------*/
1395 static int __init usbnet_init(void)
1397 // compiler should optimize these out
1398 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1399 < sizeof (struct skb_data));
1401 random_ether_addr(node_id);
1403 return usb_register(&usbnet_driver);
1405 module_init(usbnet_init);
1407 static void __exit usbnet_exit(void)
1409 usb_deregister(&usbnet_driver);
1411 module_exit(usbnet_exit);
1413 MODULE_AUTHOR("David Brownell");
1414 MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
1415 MODULE_LICENSE("GPL");