2 * USB Network driver infrastructure
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: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
26 * These devices usually differ in terms of control protocols (if they
27 * even have one!) and sometimes they define new framing to wrap or batch
28 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
29 * so interface (un)binding, endpoint I/O queues, fault handling, and other
30 * issues can usefully be addressed by this framework.
33 // #define DEBUG // error path messages, extra info
34 // #define VERBOSE // more; success messages
36 #include <linux/module.h>
37 #include <linux/sched.h>
38 #include <linux/init.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/workqueue.h>
43 #include <linux/mii.h>
44 #include <linux/usb.h>
48 #define DRIVER_VERSION "22-Aug-2005"
51 /*-------------------------------------------------------------------------*/
54 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
55 * Several dozen bytes of IPv4 data can fit in two such transactions.
56 * One maximum size Ethernet packet takes twenty four of them.
57 * For high speed, each frame comfortably fits almost 36 max size
58 * Ethernet packets (so queues should be bigger).
60 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
61 * let the USB host controller be busy for 5msec or more before an irq
62 * is required, under load. Jumbograms change the equation.
64 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
65 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
67 // reawaken network queue this soon after stopping; else watchdog barks
68 #define TX_TIMEOUT_JIFFIES (5*HZ)
70 // throttle rx/tx briefly after some faults, so khubd might disconnect()
71 // us (it polls at HZ/4 usually) before we report too many false errors.
72 #define THROTTLE_JIFFIES (HZ/8)
75 #define UNLINK_TIMEOUT_MS 3
77 /*-------------------------------------------------------------------------*/
79 // randomly generated ethernet address
80 static u8 node_id [ETH_ALEN];
82 static const char driver_name [] = "usbnet";
84 /* use ethtool to change the level for any given device */
85 static int msg_level = -1;
86 module_param (msg_level, int, 0);
87 MODULE_PARM_DESC (msg_level, "Override default message level");
89 /*-------------------------------------------------------------------------*/
91 /* handles CDC Ethernet and many other network "bulk data" interfaces */
92 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
95 struct usb_host_interface *alt = NULL;
96 struct usb_host_endpoint *in = NULL, *out = NULL;
97 struct usb_host_endpoint *status = NULL;
99 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
102 in = out = status = NULL;
103 alt = intf->altsetting + tmp;
105 /* take the first altsetting with in-bulk + out-bulk;
106 * remember any status endpoint, just in case;
107 * ignore other endpoints and altsetttings.
109 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
110 struct usb_host_endpoint *e;
113 e = alt->endpoint + ep;
114 switch (e->desc.bmAttributes) {
115 case USB_ENDPOINT_XFER_INT:
116 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
120 case USB_ENDPOINT_XFER_BULK:
125 if (e->desc.bEndpointAddress & USB_DIR_IN) {
128 else if (intr && !status)
138 if (!alt || !in || !out)
141 if (alt->desc.bAlternateSetting != 0
142 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
143 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
144 alt->desc.bAlternateSetting);
149 dev->in = usb_rcvbulkpipe (dev->udev,
150 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
151 dev->out = usb_sndbulkpipe (dev->udev,
152 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
153 dev->status = status;
156 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
158 static void intr_complete (struct urb *urb, struct pt_regs *regs);
160 static int init_status (struct usbnet *dev, struct usb_interface *intf)
167 if (!dev->driver_info->status)
170 pipe = usb_rcvintpipe (dev->udev,
171 dev->status->desc.bEndpointAddress
172 & USB_ENDPOINT_NUMBER_MASK);
173 maxp = usb_maxpacket (dev->udev, pipe, 0);
175 /* avoid 1 msec chatter: min 8 msec poll rate */
176 period = max ((int) dev->status->desc.bInterval,
177 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
179 buf = kmalloc (maxp, SLAB_KERNEL);
181 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
182 if (!dev->interrupt) {
186 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
187 buf, maxp, intr_complete, dev, period);
189 "status ep%din, %d bytes period %d\n",
190 usb_pipeendpoint(pipe), maxp, period);
196 /* Passes this packet up the stack, updating its accounting.
197 * Some link protocols batch packets, so their rx_fixup paths
198 * can return clones as well as just modify the original skb.
200 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
205 skb->protocol = eth_type_trans (skb, dev->net);
206 dev->stats.rx_packets++;
207 dev->stats.rx_bytes += skb->len;
209 if (netif_msg_rx_status (dev))
210 devdbg (dev, "< rx, len %zu, type 0x%x",
211 skb->len + sizeof (struct ethhdr), skb->protocol);
212 memset (skb->cb, 0, sizeof (struct skb_data));
213 status = netif_rx (skb);
214 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
215 devdbg (dev, "netif_rx status %d", status);
217 EXPORT_SYMBOL_GPL(usbnet_skb_return);
220 /*-------------------------------------------------------------------------
222 * Network Device Driver (peer link to "Host Device", from USB host)
224 *-------------------------------------------------------------------------*/
226 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
228 struct usbnet *dev = netdev_priv(net);
229 int ll_mtu = new_mtu + net->hard_header_len;
231 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
233 // no second zero-length packet read wanted after mtu-sized packets
234 if ((ll_mtu % dev->maxpacket) == 0)
240 /*-------------------------------------------------------------------------*/
242 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
244 struct usbnet *dev = netdev_priv(net);
248 /*-------------------------------------------------------------------------*/
250 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
251 * completion callbacks. 2.5 should have fixed those bugs...
254 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
258 spin_lock_irqsave(&list->lock, flags);
259 __skb_unlink(skb, list);
260 spin_unlock(&list->lock);
261 spin_lock(&dev->done.lock);
262 __skb_queue_tail(&dev->done, skb);
263 if (dev->done.qlen == 1)
264 tasklet_schedule(&dev->bh);
265 spin_unlock_irqrestore(&dev->done.lock, flags);
268 /* some work can't be done in tasklets, so we use keventd
270 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
271 * but tasklet_schedule() doesn't. hope the failure is rare.
273 void usbnet_defer_kevent (struct usbnet *dev, int work)
275 set_bit (work, &dev->flags);
276 if (!schedule_work (&dev->kevent))
277 deverr (dev, "kevent %d may have been dropped", work);
279 devdbg (dev, "kevent %d scheduled", work);
281 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
283 /*-------------------------------------------------------------------------*/
285 static void rx_complete (struct urb *urb, struct pt_regs *regs);
287 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
290 struct skb_data *entry;
292 unsigned long lockflags;
293 size_t size = dev->rx_urb_size;
295 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
296 if (netif_msg_rx_err (dev))
297 devdbg (dev, "no rx skb");
298 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
302 skb_reserve (skb, NET_IP_ALIGN);
304 entry = (struct skb_data *) skb->cb;
307 entry->state = rx_start;
310 usb_fill_bulk_urb (urb, dev->udev, dev->in,
311 skb->data, size, rx_complete, skb);
313 spin_lock_irqsave (&dev->rxq.lock, lockflags);
315 if (netif_running (dev->net)
316 && netif_device_present (dev->net)
317 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
318 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
320 usbnet_defer_kevent (dev, EVENT_RX_HALT);
323 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
326 if (netif_msg_ifdown (dev))
327 devdbg (dev, "device gone");
328 netif_device_detach (dev->net);
331 if (netif_msg_rx_err (dev))
332 devdbg (dev, "rx submit, %d", retval);
333 tasklet_schedule (&dev->bh);
336 __skb_queue_tail (&dev->rxq, skb);
339 if (netif_msg_ifdown (dev))
340 devdbg (dev, "rx: stopped");
343 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
345 dev_kfree_skb_any (skb);
351 /*-------------------------------------------------------------------------*/
353 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
355 if (dev->driver_info->rx_fixup
356 && !dev->driver_info->rx_fixup (dev, skb))
358 // else network stack removes extra byte if we forced a short packet
361 usbnet_skb_return (dev, skb);
363 if (netif_msg_rx_err (dev))
364 devdbg (dev, "drop");
366 dev->stats.rx_errors++;
367 skb_queue_tail (&dev->done, skb);
371 /*-------------------------------------------------------------------------*/
373 static void rx_complete (struct urb *urb, struct pt_regs *regs)
375 struct sk_buff *skb = (struct sk_buff *) urb->context;
376 struct skb_data *entry = (struct skb_data *) skb->cb;
377 struct usbnet *dev = entry->dev;
378 int urb_status = urb->status;
380 skb_put (skb, urb->actual_length);
381 entry->state = rx_done;
384 switch (urb_status) {
387 if (skb->len < dev->net->hard_header_len) {
388 entry->state = rx_cleanup;
389 dev->stats.rx_errors++;
390 dev->stats.rx_length_errors++;
391 if (netif_msg_rx_err (dev))
392 devdbg (dev, "rx length %d", skb->len);
396 // stalls need manual reset. this is rare ... except that
397 // when going through USB 2.0 TTs, unplug appears this way.
398 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
399 // storm, recovering as needed.
401 dev->stats.rx_errors++;
402 usbnet_defer_kevent (dev, EVENT_RX_HALT);
405 // software-driven interface shutdown
406 case -ECONNRESET: // async unlink
407 case -ESHUTDOWN: // hardware gone
408 if (netif_msg_ifdown (dev))
409 devdbg (dev, "rx shutdown, code %d", urb_status);
412 // we get controller i/o faults during khubd disconnect() delays.
413 // throttle down resubmits, to avoid log floods; just temporarily,
414 // so we still recover when the fault isn't a khubd delay.
415 case -EPROTO: // ehci
416 case -ETIMEDOUT: // ohci
417 case -EILSEQ: // uhci
418 dev->stats.rx_errors++;
419 if (!timer_pending (&dev->delay)) {
420 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
421 if (netif_msg_link (dev))
422 devdbg (dev, "rx throttle %d", urb_status);
425 entry->state = rx_cleanup;
430 // data overrun ... flush fifo?
432 dev->stats.rx_over_errors++;
436 entry->state = rx_cleanup;
437 dev->stats.rx_errors++;
438 if (netif_msg_rx_err (dev))
439 devdbg (dev, "rx status %d", urb_status);
443 defer_bh(dev, skb, &dev->rxq);
446 if (netif_running (dev->net)
447 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
448 rx_submit (dev, urb, GFP_ATOMIC);
453 if (netif_msg_rx_err (dev))
454 devdbg (dev, "no read resubmitted");
457 static void intr_complete (struct urb *urb, struct pt_regs *regs)
459 struct usbnet *dev = urb->context;
460 int status = urb->status;
465 dev->driver_info->status(dev, urb);
468 /* software-driven interface shutdown */
469 case -ENOENT: // urb killed
470 case -ESHUTDOWN: // hardware gone
471 if (netif_msg_ifdown (dev))
472 devdbg (dev, "intr shutdown, code %d", status);
475 /* NOTE: not throttling like RX/TX, since this endpoint
476 * already polls infrequently
479 devdbg (dev, "intr status %d", status);
483 if (!netif_running (dev->net))
486 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
487 status = usb_submit_urb (urb, GFP_ATOMIC);
488 if (status != 0 && netif_msg_timer (dev))
489 deverr(dev, "intr resubmit --> %d", status);
492 /*-------------------------------------------------------------------------*/
494 // unlink pending rx/tx; completion handlers do all other cleanup
496 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
499 struct sk_buff *skb, *skbnext;
502 spin_lock_irqsave (&q->lock, flags);
503 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
504 struct skb_data *entry;
508 entry = (struct skb_data *) skb->cb;
512 // during some PM-driven resume scenarios,
513 // these (async) unlinks complete immediately
514 retval = usb_unlink_urb (urb);
515 if (retval != -EINPROGRESS && retval != 0)
516 devdbg (dev, "unlink urb err, %d", retval);
520 spin_unlock_irqrestore (&q->lock, flags);
524 // Flush all pending rx urbs
525 // minidrivers may need to do this when the MTU changes
527 void usbnet_unlink_rx_urbs(struct usbnet *dev)
529 if (netif_running(dev->net)) {
530 (void) unlink_urbs (dev, &dev->rxq);
531 tasklet_schedule(&dev->bh);
534 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
537 /*-------------------------------------------------------------------------*/
539 // precondition: never called in_interrupt
541 static int usbnet_stop (struct net_device *net)
543 struct usbnet *dev = netdev_priv(net);
545 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
546 DECLARE_WAITQUEUE (wait, current);
548 netif_stop_queue (net);
550 if (netif_msg_ifdown (dev))
551 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
552 dev->stats.rx_packets, dev->stats.tx_packets,
553 dev->stats.rx_errors, dev->stats.tx_errors
556 // ensure there are no more active urbs
557 add_wait_queue (&unlink_wakeup, &wait);
558 dev->wait = &unlink_wakeup;
559 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
561 // maybe wait for deletions to finish.
562 while (!skb_queue_empty(&dev->rxq) &&
563 !skb_queue_empty(&dev->txq) &&
564 !skb_queue_empty(&dev->done)) {
565 msleep(UNLINK_TIMEOUT_MS);
566 if (netif_msg_ifdown (dev))
567 devdbg (dev, "waited for %d urb completions", temp);
570 remove_wait_queue (&unlink_wakeup, &wait);
572 usb_kill_urb(dev->interrupt);
574 /* deferred work (task, timer, softirq) must also stop.
575 * can't flush_scheduled_work() until we drop rtnl (later),
576 * else workers could deadlock; so make workers a NOP.
579 del_timer_sync (&dev->delay);
580 tasklet_kill (&dev->bh);
585 /*-------------------------------------------------------------------------*/
587 // posts reads, and enables write queuing
589 // precondition: never called in_interrupt
591 static int usbnet_open (struct net_device *net)
593 struct usbnet *dev = netdev_priv(net);
595 struct driver_info *info = dev->driver_info;
597 // put into "known safe" state
598 if (info->reset && (retval = info->reset (dev)) < 0) {
599 if (netif_msg_ifup (dev))
601 "open reset fail (%d) usbnet usb-%s-%s, %s",
603 dev->udev->bus->bus_name, dev->udev->devpath,
608 // insist peer be connected
609 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
610 if (netif_msg_ifup (dev))
611 devdbg (dev, "can't open; %d", retval);
615 /* start any status interrupt transfer */
616 if (dev->interrupt) {
617 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
619 if (netif_msg_ifup (dev))
620 deverr (dev, "intr submit %d", retval);
625 netif_start_queue (net);
626 if (netif_msg_ifup (dev)) {
629 if (dev->driver_info->flags & FLAG_FRAMING_NC)
631 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
633 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
635 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
637 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
642 devinfo (dev, "open: enable queueing "
643 "(rx %d, tx %d) mtu %d %s framing",
644 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
648 // delay posting reads until we're fully open
649 tasklet_schedule (&dev->bh);
654 /*-------------------------------------------------------------------------*/
656 /* ethtool methods; minidrivers may need to add some more, but
657 * they'll probably want to use this base set.
660 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
662 struct usbnet *dev = netdev_priv(net);
664 /* REVISIT don't always return "usbnet" */
665 strncpy (info->driver, driver_name, sizeof info->driver);
666 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
667 strncpy (info->fw_version, dev->driver_info->description,
668 sizeof info->fw_version);
669 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
671 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
673 static u32 usbnet_get_link (struct net_device *net)
675 struct usbnet *dev = netdev_priv(net);
677 /* If a check_connect is defined, return its result */
678 if (dev->driver_info->check_connect)
679 return dev->driver_info->check_connect (dev) == 0;
681 /* Otherwise, say we're up (to avoid breaking scripts) */
685 u32 usbnet_get_msglevel (struct net_device *net)
687 struct usbnet *dev = netdev_priv(net);
689 return dev->msg_enable;
691 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
693 void usbnet_set_msglevel (struct net_device *net, u32 level)
695 struct usbnet *dev = netdev_priv(net);
697 dev->msg_enable = level;
699 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
701 /* drivers may override default ethtool_ops in their bind() routine */
702 static struct ethtool_ops usbnet_ethtool_ops = {
703 .get_drvinfo = usbnet_get_drvinfo,
704 .get_link = usbnet_get_link,
705 .get_msglevel = usbnet_get_msglevel,
706 .set_msglevel = usbnet_set_msglevel,
709 /*-------------------------------------------------------------------------*/
711 /* work that cannot be done in interrupt context uses keventd.
713 * NOTE: with 2.5 we could do more of this using completion callbacks,
714 * especially now that control transfers can be queued.
719 struct usbnet *dev = data;
722 /* usb_clear_halt() needs a thread context */
723 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
724 unlink_urbs (dev, &dev->txq);
725 status = usb_clear_halt (dev->udev, dev->out);
728 && status != -ESHUTDOWN) {
729 if (netif_msg_tx_err (dev))
730 deverr (dev, "can't clear tx halt, status %d",
733 clear_bit (EVENT_TX_HALT, &dev->flags);
734 if (status != -ESHUTDOWN)
735 netif_wake_queue (dev->net);
738 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
739 unlink_urbs (dev, &dev->rxq);
740 status = usb_clear_halt (dev->udev, dev->in);
743 && status != -ESHUTDOWN) {
744 if (netif_msg_rx_err (dev))
745 deverr (dev, "can't clear rx halt, status %d",
748 clear_bit (EVENT_RX_HALT, &dev->flags);
749 tasklet_schedule (&dev->bh);
753 /* tasklet could resubmit itself forever if memory is tight */
754 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
755 struct urb *urb = NULL;
757 if (netif_running (dev->net))
758 urb = usb_alloc_urb (0, GFP_KERNEL);
760 clear_bit (EVENT_RX_MEMORY, &dev->flags);
762 clear_bit (EVENT_RX_MEMORY, &dev->flags);
763 rx_submit (dev, urb, GFP_KERNEL);
764 tasklet_schedule (&dev->bh);
768 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
769 struct driver_info *info = dev->driver_info;
772 clear_bit (EVENT_LINK_RESET, &dev->flags);
773 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
774 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
776 dev->udev->bus->bus_name, dev->udev->devpath,
782 devdbg (dev, "kevent done, flags = 0x%lx",
786 /*-------------------------------------------------------------------------*/
788 static void tx_complete (struct urb *urb, struct pt_regs *regs)
790 struct sk_buff *skb = (struct sk_buff *) urb->context;
791 struct skb_data *entry = (struct skb_data *) skb->cb;
792 struct usbnet *dev = entry->dev;
794 if (urb->status == 0) {
795 dev->stats.tx_packets++;
796 dev->stats.tx_bytes += entry->length;
798 dev->stats.tx_errors++;
800 switch (urb->status) {
802 usbnet_defer_kevent (dev, EVENT_TX_HALT);
805 /* software-driven interface shutdown */
806 case -ECONNRESET: // async unlink
807 case -ESHUTDOWN: // hardware gone
810 // like rx, tx gets controller i/o faults during khubd delays
811 // and so it uses the same throttling mechanism.
812 case -EPROTO: // ehci
813 case -ETIMEDOUT: // ohci
814 case -EILSEQ: // uhci
815 if (!timer_pending (&dev->delay)) {
816 mod_timer (&dev->delay,
817 jiffies + THROTTLE_JIFFIES);
818 if (netif_msg_link (dev))
819 devdbg (dev, "tx throttle %d",
822 netif_stop_queue (dev->net);
825 if (netif_msg_tx_err (dev))
826 devdbg (dev, "tx err %d", entry->urb->status);
832 entry->state = tx_done;
833 defer_bh(dev, skb, &dev->txq);
836 /*-------------------------------------------------------------------------*/
838 static void usbnet_tx_timeout (struct net_device *net)
840 struct usbnet *dev = netdev_priv(net);
842 unlink_urbs (dev, &dev->txq);
843 tasklet_schedule (&dev->bh);
845 // FIXME: device recovery -- reset?
848 /*-------------------------------------------------------------------------*/
850 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
852 struct usbnet *dev = netdev_priv(net);
854 int retval = NET_XMIT_SUCCESS;
855 struct urb *urb = NULL;
856 struct skb_data *entry;
857 struct driver_info *info = dev->driver_info;
860 // some devices want funky USB-level framing, for
861 // win32 driver (usually) and/or hardware quirks
862 if (info->tx_fixup) {
863 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
865 if (netif_msg_tx_err (dev))
866 devdbg (dev, "can't tx_fixup skb");
872 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
873 if (netif_msg_tx_err (dev))
874 devdbg (dev, "no urb");
878 entry = (struct skb_data *) skb->cb;
881 entry->state = tx_start;
882 entry->length = length;
884 usb_fill_bulk_urb (urb, dev->udev, dev->out,
885 skb->data, skb->len, tx_complete, skb);
887 /* don't assume the hardware handles USB_ZERO_PACKET
888 * NOTE: strictly conforming cdc-ether devices should expect
889 * the ZLP here, but ignore the one-byte packet.
891 * FIXME zero that byte, if it doesn't require a new skb.
893 if ((length % dev->maxpacket) == 0)
894 urb->transfer_buffer_length++;
896 spin_lock_irqsave (&dev->txq.lock, flags);
898 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
900 netif_stop_queue (net);
901 usbnet_defer_kevent (dev, EVENT_TX_HALT);
904 if (netif_msg_tx_err (dev))
905 devdbg (dev, "tx: submit urb err %d", retval);
908 net->trans_start = jiffies;
909 __skb_queue_tail (&dev->txq, skb);
910 if (dev->txq.qlen >= TX_QLEN (dev))
911 netif_stop_queue (net);
913 spin_unlock_irqrestore (&dev->txq.lock, flags);
916 if (netif_msg_tx_err (dev))
917 devdbg (dev, "drop, code %d", retval);
919 retval = NET_XMIT_SUCCESS;
920 dev->stats.tx_dropped++;
922 dev_kfree_skb_any (skb);
924 } else if (netif_msg_tx_queued (dev)) {
925 devdbg (dev, "> tx, len %d, type 0x%x",
926 length, skb->protocol);
932 /*-------------------------------------------------------------------------*/
934 // tasklet (work deferred from completions, in_irq) or timer
936 static void usbnet_bh (unsigned long param)
938 struct usbnet *dev = (struct usbnet *) param;
940 struct skb_data *entry;
942 while ((skb = skb_dequeue (&dev->done))) {
943 entry = (struct skb_data *) skb->cb;
944 switch (entry->state) {
946 entry->state = rx_cleanup;
947 rx_process (dev, skb);
951 usb_free_urb (entry->urb);
955 devdbg (dev, "bogus skb state %d", entry->state);
959 // waiting for all pending urbs to complete?
961 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
965 // or are we maybe short a few urbs?
966 } else if (netif_running (dev->net)
967 && netif_device_present (dev->net)
968 && !timer_pending (&dev->delay)
969 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
970 int temp = dev->rxq.qlen;
971 int qlen = RX_QLEN (dev);
977 // don't refill the queue all at once
978 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
979 urb = usb_alloc_urb (0, GFP_ATOMIC);
981 rx_submit (dev, urb, GFP_ATOMIC);
983 if (temp != dev->rxq.qlen && netif_msg_link (dev))
984 devdbg (dev, "rxqlen %d --> %d",
985 temp, dev->rxq.qlen);
986 if (dev->rxq.qlen < qlen)
987 tasklet_schedule (&dev->bh);
989 if (dev->txq.qlen < TX_QLEN (dev))
990 netif_wake_queue (dev->net);
996 /*-------------------------------------------------------------------------
998 * USB Device Driver support
1000 *-------------------------------------------------------------------------*/
1002 // precondition: never called in_interrupt
1004 void usbnet_disconnect (struct usb_interface *intf)
1007 struct usb_device *xdev;
1008 struct net_device *net;
1010 dev = usb_get_intfdata(intf);
1011 usb_set_intfdata(intf, NULL);
1015 xdev = interface_to_usbdev (intf);
1017 if (netif_msg_probe (dev))
1018 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1019 intf->dev.driver->name,
1020 xdev->bus->bus_name, xdev->devpath,
1021 dev->driver_info->description);
1024 unregister_netdev (net);
1026 /* we don't hold rtnl here ... */
1027 flush_scheduled_work ();
1029 if (dev->driver_info->unbind)
1030 dev->driver_info->unbind (dev, intf);
1035 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1038 /*-------------------------------------------------------------------------*/
1040 // precondition: never called in_interrupt
1043 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1046 struct net_device *net;
1047 struct usb_host_interface *interface;
1048 struct driver_info *info;
1049 struct usb_device *xdev;
1052 info = (struct driver_info *) prod->driver_info;
1054 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1057 xdev = interface_to_usbdev (udev);
1058 interface = udev->cur_altsetting;
1064 // set up our own records
1065 net = alloc_etherdev(sizeof(*dev));
1067 dbg ("can't kmalloc dev");
1071 dev = netdev_priv(net);
1073 dev->driver_info = info;
1074 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1075 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1076 skb_queue_head_init (&dev->rxq);
1077 skb_queue_head_init (&dev->txq);
1078 skb_queue_head_init (&dev->done);
1079 dev->bh.func = usbnet_bh;
1080 dev->bh.data = (unsigned long) dev;
1081 INIT_WORK (&dev->kevent, kevent, dev);
1082 dev->delay.function = usbnet_bh;
1083 dev->delay.data = (unsigned long) dev;
1084 init_timer (&dev->delay);
1086 SET_MODULE_OWNER (net);
1088 strcpy (net->name, "usb%d");
1089 memcpy (net->dev_addr, node_id, sizeof node_id);
1091 /* rx and tx sides can use different message sizes;
1092 * bind() should set rx_urb_size in that case.
1094 dev->hard_mtu = net->mtu + net->hard_header_len;
1096 // dma_supported() is deeply broken on almost all architectures
1097 // possible with some EHCI controllers
1098 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1099 net->features |= NETIF_F_HIGHDMA;
1102 net->change_mtu = usbnet_change_mtu;
1103 net->get_stats = usbnet_get_stats;
1104 net->hard_start_xmit = usbnet_start_xmit;
1105 net->open = usbnet_open;
1106 net->stop = usbnet_stop;
1107 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1108 net->tx_timeout = usbnet_tx_timeout;
1109 net->ethtool_ops = &usbnet_ethtool_ops;
1111 // allow device-specific bind/init procedures
1112 // NOTE net->name still not usable ...
1114 status = info->bind (dev, udev);
1115 // heuristic: "usb%d" for links we know are two-host,
1116 // else "eth%d" when there's reasonable doubt. userspace
1117 // can rename the link if it knows better.
1118 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1119 && (net->dev_addr [0] & 0x02) == 0)
1120 strcpy (net->name, "eth%d");
1122 /* maybe the remote can't receive an Ethernet MTU */
1123 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1124 net->mtu = dev->hard_mtu - net->hard_header_len;
1125 } else if (!info->in || !info->out)
1126 status = usbnet_get_endpoints (dev, udev);
1128 dev->in = usb_rcvbulkpipe (xdev, info->in);
1129 dev->out = usb_sndbulkpipe (xdev, info->out);
1130 if (!(info->flags & FLAG_NO_SETINT))
1131 status = usb_set_interface (xdev,
1132 interface->desc.bInterfaceNumber,
1133 interface->desc.bAlternateSetting);
1138 if (status == 0 && dev->status)
1139 status = init_status (dev, udev);
1143 if (!dev->rx_urb_size)
1144 dev->rx_urb_size = dev->hard_mtu;
1145 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1147 SET_NETDEV_DEV(net, &udev->dev);
1148 status = register_netdev (net);
1151 if (netif_msg_probe (dev))
1152 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1153 "%02x:%02x:%02x:%02x:%02x:%02x",
1154 udev->dev.driver->name,
1155 xdev->bus->bus_name, xdev->devpath,
1156 dev->driver_info->description,
1157 net->dev_addr [0], net->dev_addr [1],
1158 net->dev_addr [2], net->dev_addr [3],
1159 net->dev_addr [4], net->dev_addr [5]);
1161 // ok, it's ready to go.
1162 usb_set_intfdata (udev, dev);
1164 // start as if the link is up
1165 netif_device_attach (net);
1171 info->unbind (dev, udev);
1178 EXPORT_SYMBOL_GPL(usbnet_probe);
1180 /*-------------------------------------------------------------------------*/
1182 /* FIXME these suspend/resume methods assume non-CDC style
1183 * devices, with only one interface.
1186 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1188 struct usbnet *dev = usb_get_intfdata(intf);
1190 /* accelerate emptying of the rx and queues, to avoid
1191 * having everything error out.
1193 netif_device_detach (dev->net);
1194 (void) unlink_urbs (dev, &dev->rxq);
1195 (void) unlink_urbs (dev, &dev->txq);
1198 EXPORT_SYMBOL_GPL(usbnet_suspend);
1200 int usbnet_resume (struct usb_interface *intf)
1202 struct usbnet *dev = usb_get_intfdata(intf);
1204 netif_device_attach (dev->net);
1205 tasklet_schedule (&dev->bh);
1208 EXPORT_SYMBOL_GPL(usbnet_resume);
1211 /*-------------------------------------------------------------------------*/
1213 static int __init usbnet_init(void)
1215 /* compiler should optimize this out */
1216 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1217 < sizeof (struct skb_data));
1219 random_ether_addr(node_id);
1222 module_init(usbnet_init);
1224 static void __exit usbnet_exit(void)
1227 module_exit(usbnet_exit);
1229 MODULE_AUTHOR("David Brownell");
1230 MODULE_DESCRIPTION("USB network driver framework");
1231 MODULE_LICENSE("GPL");