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/init.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/ethtool.h>
41 #include <linux/workqueue.h>
42 #include <linux/mii.h>
43 #include <linux/usb.h>
47 #define DRIVER_VERSION "22-Aug-2005"
50 /*-------------------------------------------------------------------------*/
53 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
54 * Several dozen bytes of IPv4 data can fit in two such transactions.
55 * One maximum size Ethernet packet takes twenty four of them.
56 * For high speed, each frame comfortably fits almost 36 max size
57 * Ethernet packets (so queues should be bigger).
59 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
60 * let the USB host controller be busy for 5msec or more before an irq
61 * is required, under load. Jumbograms change the equation.
63 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
64 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
65 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
66 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
67 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
69 // reawaken network queue this soon after stopping; else watchdog barks
70 #define TX_TIMEOUT_JIFFIES (5*HZ)
72 // throttle rx/tx briefly after some faults, so khubd might disconnect()
73 // us (it polls at HZ/4 usually) before we report too many false errors.
74 #define THROTTLE_JIFFIES (HZ/8)
77 #define UNLINK_TIMEOUT_MS 3
79 /*-------------------------------------------------------------------------*/
81 // randomly generated ethernet address
82 static u8 node_id [ETH_ALEN];
84 static const char driver_name [] = "usbnet";
86 /* use ethtool to change the level for any given device */
87 static int msg_level = -1;
88 module_param (msg_level, int, 0);
89 MODULE_PARM_DESC (msg_level, "Override default message level");
91 /*-------------------------------------------------------------------------*/
93 /* handles CDC Ethernet and many other network "bulk data" interfaces */
94 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
97 struct usb_host_interface *alt = NULL;
98 struct usb_host_endpoint *in = NULL, *out = NULL;
99 struct usb_host_endpoint *status = NULL;
101 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
104 in = out = status = NULL;
105 alt = intf->altsetting + tmp;
107 /* take the first altsetting with in-bulk + out-bulk;
108 * remember any status endpoint, just in case;
109 * ignore other endpoints and altsetttings.
111 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
112 struct usb_host_endpoint *e;
115 e = alt->endpoint + ep;
116 switch (e->desc.bmAttributes) {
117 case USB_ENDPOINT_XFER_INT:
118 if (!usb_endpoint_dir_in(&e->desc))
122 case USB_ENDPOINT_XFER_BULK:
127 if (usb_endpoint_dir_in(&e->desc)) {
130 else if (intr && !status)
140 if (!alt || !in || !out)
143 if (alt->desc.bAlternateSetting != 0
144 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
145 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
146 alt->desc.bAlternateSetting);
151 dev->in = usb_rcvbulkpipe (dev->udev,
152 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
153 dev->out = usb_sndbulkpipe (dev->udev,
154 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155 dev->status = status;
158 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
160 static void intr_complete (struct urb *urb);
162 static int init_status (struct usbnet *dev, struct usb_interface *intf)
169 if (!dev->driver_info->status)
172 pipe = usb_rcvintpipe (dev->udev,
173 dev->status->desc.bEndpointAddress
174 & USB_ENDPOINT_NUMBER_MASK);
175 maxp = usb_maxpacket (dev->udev, pipe, 0);
177 /* avoid 1 msec chatter: min 8 msec poll rate */
178 period = max ((int) dev->status->desc.bInterval,
179 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
181 buf = kmalloc (maxp, GFP_KERNEL);
183 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
184 if (!dev->interrupt) {
188 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
189 buf, maxp, intr_complete, dev, period);
191 "status ep%din, %d bytes period %d\n",
192 usb_pipeendpoint(pipe), maxp, period);
198 /* Passes this packet up the stack, updating its accounting.
199 * Some link protocols batch packets, so their rx_fixup paths
200 * can return clones as well as just modify the original skb.
202 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
206 skb->protocol = eth_type_trans (skb, dev->net);
207 dev->stats.rx_packets++;
208 dev->stats.rx_bytes += skb->len;
210 if (netif_msg_rx_status (dev))
211 devdbg (dev, "< rx, len %zu, type 0x%x",
212 skb->len + sizeof (struct ethhdr), skb->protocol);
213 memset (skb->cb, 0, sizeof (struct skb_data));
214 status = netif_rx (skb);
215 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
216 devdbg (dev, "netif_rx status %d", status);
218 EXPORT_SYMBOL_GPL(usbnet_skb_return);
221 /*-------------------------------------------------------------------------
223 * Network Device Driver (peer link to "Host Device", from USB host)
225 *-------------------------------------------------------------------------*/
227 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
229 struct usbnet *dev = netdev_priv(net);
230 int ll_mtu = new_mtu + net->hard_header_len;
231 int old_hard_mtu = dev->hard_mtu;
232 int old_rx_urb_size = dev->rx_urb_size;
236 // no second zero-length packet read wanted after mtu-sized packets
237 if ((ll_mtu % dev->maxpacket) == 0)
241 dev->hard_mtu = net->mtu + net->hard_header_len;
242 if (dev->rx_urb_size == old_hard_mtu) {
243 dev->rx_urb_size = dev->hard_mtu;
244 if (dev->rx_urb_size > old_rx_urb_size)
245 usbnet_unlink_rx_urbs(dev);
251 /*-------------------------------------------------------------------------*/
253 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
255 struct usbnet *dev = netdev_priv(net);
259 /*-------------------------------------------------------------------------*/
261 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
262 * completion callbacks. 2.5 should have fixed those bugs...
265 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
269 spin_lock_irqsave(&list->lock, flags);
270 __skb_unlink(skb, list);
271 spin_unlock(&list->lock);
272 spin_lock(&dev->done.lock);
273 __skb_queue_tail(&dev->done, skb);
274 if (dev->done.qlen == 1)
275 tasklet_schedule(&dev->bh);
276 spin_unlock_irqrestore(&dev->done.lock, flags);
279 /* some work can't be done in tasklets, so we use keventd
281 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
282 * but tasklet_schedule() doesn't. hope the failure is rare.
284 void usbnet_defer_kevent (struct usbnet *dev, int work)
286 set_bit (work, &dev->flags);
287 if (!schedule_work (&dev->kevent))
288 deverr (dev, "kevent %d may have been dropped", work);
290 devdbg (dev, "kevent %d scheduled", work);
292 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
294 /*-------------------------------------------------------------------------*/
296 static void rx_complete (struct urb *urb);
298 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
301 struct skb_data *entry;
303 unsigned long lockflags;
304 size_t size = dev->rx_urb_size;
306 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
307 if (netif_msg_rx_err (dev))
308 devdbg (dev, "no rx skb");
309 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
313 skb_reserve (skb, NET_IP_ALIGN);
315 entry = (struct skb_data *) skb->cb;
318 entry->state = rx_start;
321 usb_fill_bulk_urb (urb, dev->udev, dev->in,
322 skb->data, size, rx_complete, skb);
324 spin_lock_irqsave (&dev->rxq.lock, lockflags);
326 if (netif_running (dev->net)
327 && netif_device_present (dev->net)
328 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
329 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
331 usbnet_defer_kevent (dev, EVENT_RX_HALT);
334 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
337 if (netif_msg_ifdown (dev))
338 devdbg (dev, "device gone");
339 netif_device_detach (dev->net);
342 if (netif_msg_rx_err (dev))
343 devdbg (dev, "rx submit, %d", retval);
344 tasklet_schedule (&dev->bh);
347 __skb_queue_tail (&dev->rxq, skb);
350 if (netif_msg_ifdown (dev))
351 devdbg (dev, "rx: stopped");
354 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
356 dev_kfree_skb_any (skb);
362 /*-------------------------------------------------------------------------*/
364 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
366 if (dev->driver_info->rx_fixup
367 && !dev->driver_info->rx_fixup (dev, skb))
369 // else network stack removes extra byte if we forced a short packet
372 usbnet_skb_return (dev, skb);
374 if (netif_msg_rx_err (dev))
375 devdbg (dev, "drop");
377 dev->stats.rx_errors++;
378 skb_queue_tail (&dev->done, skb);
382 /*-------------------------------------------------------------------------*/
384 static void rx_complete (struct urb *urb)
386 struct sk_buff *skb = (struct sk_buff *) urb->context;
387 struct skb_data *entry = (struct skb_data *) skb->cb;
388 struct usbnet *dev = entry->dev;
389 int urb_status = urb->status;
391 skb_put (skb, urb->actual_length);
392 entry->state = rx_done;
395 switch (urb_status) {
398 if (skb->len < dev->net->hard_header_len) {
399 entry->state = rx_cleanup;
400 dev->stats.rx_errors++;
401 dev->stats.rx_length_errors++;
402 if (netif_msg_rx_err (dev))
403 devdbg (dev, "rx length %d", skb->len);
407 /* stalls need manual reset. this is rare ... except that
408 * when going through USB 2.0 TTs, unplug appears this way.
409 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
410 * storm, recovering as needed.
413 dev->stats.rx_errors++;
414 usbnet_defer_kevent (dev, EVENT_RX_HALT);
417 /* software-driven interface shutdown */
418 case -ECONNRESET: /* async unlink */
419 case -ESHUTDOWN: /* hardware gone */
420 if (netif_msg_ifdown (dev))
421 devdbg (dev, "rx shutdown, code %d", urb_status);
424 /* we get controller i/o faults during khubd disconnect() delays.
425 * throttle down resubmits, to avoid log floods; just temporarily,
426 * so we still recover when the fault isn't a khubd delay.
431 dev->stats.rx_errors++;
432 if (!timer_pending (&dev->delay)) {
433 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
434 if (netif_msg_link (dev))
435 devdbg (dev, "rx throttle %d", urb_status);
438 entry->state = rx_cleanup;
443 /* data overrun ... flush fifo? */
445 dev->stats.rx_over_errors++;
449 entry->state = rx_cleanup;
450 dev->stats.rx_errors++;
451 if (netif_msg_rx_err (dev))
452 devdbg (dev, "rx status %d", urb_status);
456 defer_bh(dev, skb, &dev->rxq);
459 if (netif_running (dev->net)
460 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
461 rx_submit (dev, urb, GFP_ATOMIC);
466 if (netif_msg_rx_err (dev))
467 devdbg (dev, "no read resubmitted");
470 static void intr_complete (struct urb *urb)
472 struct usbnet *dev = urb->context;
473 int status = urb->status;
478 dev->driver_info->status(dev, urb);
481 /* software-driven interface shutdown */
482 case -ENOENT: /* urb killed */
483 case -ESHUTDOWN: /* hardware gone */
484 if (netif_msg_ifdown (dev))
485 devdbg (dev, "intr shutdown, code %d", status);
488 /* NOTE: not throttling like RX/TX, since this endpoint
489 * already polls infrequently
492 devdbg (dev, "intr status %d", status);
496 if (!netif_running (dev->net))
499 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
500 status = usb_submit_urb (urb, GFP_ATOMIC);
501 if (status != 0 && netif_msg_timer (dev))
502 deverr(dev, "intr resubmit --> %d", status);
505 /*-------------------------------------------------------------------------*/
507 // unlink pending rx/tx; completion handlers do all other cleanup
509 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
512 struct sk_buff *skb, *skbnext;
515 spin_lock_irqsave (&q->lock, flags);
516 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
517 struct skb_data *entry;
521 entry = (struct skb_data *) skb->cb;
525 // during some PM-driven resume scenarios,
526 // these (async) unlinks complete immediately
527 retval = usb_unlink_urb (urb);
528 if (retval != -EINPROGRESS && retval != 0)
529 devdbg (dev, "unlink urb err, %d", retval);
533 spin_unlock_irqrestore (&q->lock, flags);
537 // Flush all pending rx urbs
538 // minidrivers may need to do this when the MTU changes
540 void usbnet_unlink_rx_urbs(struct usbnet *dev)
542 if (netif_running(dev->net)) {
543 (void) unlink_urbs (dev, &dev->rxq);
544 tasklet_schedule(&dev->bh);
547 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
549 /*-------------------------------------------------------------------------*/
551 // precondition: never called in_interrupt
553 static int usbnet_stop (struct net_device *net)
555 struct usbnet *dev = netdev_priv(net);
557 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
558 DECLARE_WAITQUEUE (wait, current);
560 netif_stop_queue (net);
562 if (netif_msg_ifdown (dev))
563 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
564 dev->stats.rx_packets, dev->stats.tx_packets,
565 dev->stats.rx_errors, dev->stats.tx_errors
568 // ensure there are no more active urbs
569 add_wait_queue (&unlink_wakeup, &wait);
570 dev->wait = &unlink_wakeup;
571 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
573 // maybe wait for deletions to finish.
574 while (!skb_queue_empty(&dev->rxq)
575 && !skb_queue_empty(&dev->txq)
576 && !skb_queue_empty(&dev->done)) {
577 msleep(UNLINK_TIMEOUT_MS);
578 if (netif_msg_ifdown (dev))
579 devdbg (dev, "waited for %d urb completions", temp);
582 remove_wait_queue (&unlink_wakeup, &wait);
584 usb_kill_urb(dev->interrupt);
586 /* deferred work (task, timer, softirq) must also stop.
587 * can't flush_scheduled_work() until we drop rtnl (later),
588 * else workers could deadlock; so make workers a NOP.
591 del_timer_sync (&dev->delay);
592 tasklet_kill (&dev->bh);
593 usb_autopm_put_interface(dev->intf);
598 /*-------------------------------------------------------------------------*/
600 // posts reads, and enables write queuing
602 // precondition: never called in_interrupt
604 static int usbnet_open (struct net_device *net)
606 struct usbnet *dev = netdev_priv(net);
608 struct driver_info *info = dev->driver_info;
610 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
611 if (netif_msg_ifup (dev))
613 "resumption fail (%d) usbnet usb-%s-%s, %s",
615 dev->udev->bus->bus_name, dev->udev->devpath,
620 // put into "known safe" state
621 if (info->reset && (retval = info->reset (dev)) < 0) {
622 if (netif_msg_ifup (dev))
624 "open reset fail (%d) usbnet usb-%s-%s, %s",
626 dev->udev->bus->bus_name, dev->udev->devpath,
631 // insist peer be connected
632 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
633 if (netif_msg_ifup (dev))
634 devdbg (dev, "can't open; %d", retval);
638 /* start any status interrupt transfer */
639 if (dev->interrupt) {
640 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
642 if (netif_msg_ifup (dev))
643 deverr (dev, "intr submit %d", retval);
648 netif_start_queue (net);
649 if (netif_msg_ifup (dev)) {
652 if (dev->driver_info->flags & FLAG_FRAMING_NC)
654 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
656 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
658 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
660 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
665 devinfo (dev, "open: enable queueing "
666 "(rx %d, tx %d) mtu %d %s framing",
667 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
671 // delay posting reads until we're fully open
672 tasklet_schedule (&dev->bh);
675 usb_autopm_put_interface(dev->intf);
680 /*-------------------------------------------------------------------------*/
682 /* ethtool methods; minidrivers may need to add some more, but
683 * they'll probably want to use this base set.
686 #if defined(CONFIG_MII) || defined(CONFIG_MII_MODULE)
689 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
691 struct usbnet *dev = netdev_priv(net);
693 if (!dev->mii.mdio_read)
696 return mii_ethtool_gset(&dev->mii, cmd);
698 EXPORT_SYMBOL_GPL(usbnet_get_settings);
700 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
702 struct usbnet *dev = netdev_priv(net);
705 if (!dev->mii.mdio_write)
708 retval = mii_ethtool_sset(&dev->mii, cmd);
710 /* link speed/duplex might have changed */
711 if (dev->driver_info->link_reset)
712 dev->driver_info->link_reset(dev);
717 EXPORT_SYMBOL_GPL(usbnet_set_settings);
719 u32 usbnet_get_link (struct net_device *net)
721 struct usbnet *dev = netdev_priv(net);
723 /* If a check_connect is defined, return its result */
724 if (dev->driver_info->check_connect)
725 return dev->driver_info->check_connect (dev) == 0;
727 /* if the device has mii operations, use those */
728 if (dev->mii.mdio_read)
729 return mii_link_ok(&dev->mii);
731 /* Otherwise, say we're up (to avoid breaking scripts) */
734 EXPORT_SYMBOL_GPL(usbnet_get_link);
736 int usbnet_nway_reset(struct net_device *net)
738 struct usbnet *dev = netdev_priv(net);
740 if (!dev->mii.mdio_write)
743 return mii_nway_restart(&dev->mii);
745 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
747 #endif /* HAVE_MII */
749 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
751 struct usbnet *dev = netdev_priv(net);
753 strncpy (info->driver, dev->driver_name, sizeof info->driver);
754 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
755 strncpy (info->fw_version, dev->driver_info->description,
756 sizeof info->fw_version);
757 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
759 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
761 u32 usbnet_get_msglevel (struct net_device *net)
763 struct usbnet *dev = netdev_priv(net);
765 return dev->msg_enable;
767 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
769 void usbnet_set_msglevel (struct net_device *net, u32 level)
771 struct usbnet *dev = netdev_priv(net);
773 dev->msg_enable = level;
775 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
777 /* drivers may override default ethtool_ops in their bind() routine */
778 static struct ethtool_ops usbnet_ethtool_ops = {
780 .get_settings = usbnet_get_settings,
781 .set_settings = usbnet_set_settings,
782 .get_link = usbnet_get_link,
783 .nway_reset = usbnet_nway_reset,
785 .get_drvinfo = usbnet_get_drvinfo,
786 .get_msglevel = usbnet_get_msglevel,
787 .set_msglevel = usbnet_set_msglevel,
790 /*-------------------------------------------------------------------------*/
792 /* work that cannot be done in interrupt context uses keventd.
794 * NOTE: with 2.5 we could do more of this using completion callbacks,
795 * especially now that control transfers can be queued.
798 kevent (struct work_struct *work)
801 container_of(work, struct usbnet, kevent);
804 /* usb_clear_halt() needs a thread context */
805 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
806 unlink_urbs (dev, &dev->txq);
807 status = usb_clear_halt (dev->udev, dev->out);
810 && status != -ESHUTDOWN) {
811 if (netif_msg_tx_err (dev))
812 deverr (dev, "can't clear tx halt, status %d",
815 clear_bit (EVENT_TX_HALT, &dev->flags);
816 if (status != -ESHUTDOWN)
817 netif_wake_queue (dev->net);
820 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
821 unlink_urbs (dev, &dev->rxq);
822 status = usb_clear_halt (dev->udev, dev->in);
825 && status != -ESHUTDOWN) {
826 if (netif_msg_rx_err (dev))
827 deverr (dev, "can't clear rx halt, status %d",
830 clear_bit (EVENT_RX_HALT, &dev->flags);
831 tasklet_schedule (&dev->bh);
835 /* tasklet could resubmit itself forever if memory is tight */
836 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
837 struct urb *urb = NULL;
839 if (netif_running (dev->net))
840 urb = usb_alloc_urb (0, GFP_KERNEL);
842 clear_bit (EVENT_RX_MEMORY, &dev->flags);
844 clear_bit (EVENT_RX_MEMORY, &dev->flags);
845 rx_submit (dev, urb, GFP_KERNEL);
846 tasklet_schedule (&dev->bh);
850 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
851 struct driver_info *info = dev->driver_info;
854 clear_bit (EVENT_LINK_RESET, &dev->flags);
855 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
856 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
858 dev->udev->bus->bus_name, dev->udev->devpath,
864 devdbg (dev, "kevent done, flags = 0x%lx",
868 /*-------------------------------------------------------------------------*/
870 static void tx_complete (struct urb *urb)
872 struct sk_buff *skb = (struct sk_buff *) urb->context;
873 struct skb_data *entry = (struct skb_data *) skb->cb;
874 struct usbnet *dev = entry->dev;
876 if (urb->status == 0) {
877 dev->stats.tx_packets++;
878 dev->stats.tx_bytes += entry->length;
880 dev->stats.tx_errors++;
882 switch (urb->status) {
884 usbnet_defer_kevent (dev, EVENT_TX_HALT);
887 /* software-driven interface shutdown */
888 case -ECONNRESET: // async unlink
889 case -ESHUTDOWN: // hardware gone
892 // like rx, tx gets controller i/o faults during khubd delays
893 // and so it uses the same throttling mechanism.
897 if (!timer_pending (&dev->delay)) {
898 mod_timer (&dev->delay,
899 jiffies + THROTTLE_JIFFIES);
900 if (netif_msg_link (dev))
901 devdbg (dev, "tx throttle %d",
904 netif_stop_queue (dev->net);
907 if (netif_msg_tx_err (dev))
908 devdbg (dev, "tx err %d", entry->urb->status);
914 entry->state = tx_done;
915 defer_bh(dev, skb, &dev->txq);
918 /*-------------------------------------------------------------------------*/
920 static void usbnet_tx_timeout (struct net_device *net)
922 struct usbnet *dev = netdev_priv(net);
924 unlink_urbs (dev, &dev->txq);
925 tasklet_schedule (&dev->bh);
927 // FIXME: device recovery -- reset?
930 /*-------------------------------------------------------------------------*/
932 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
934 struct usbnet *dev = netdev_priv(net);
936 int retval = NET_XMIT_SUCCESS;
937 struct urb *urb = NULL;
938 struct skb_data *entry;
939 struct driver_info *info = dev->driver_info;
942 // some devices want funky USB-level framing, for
943 // win32 driver (usually) and/or hardware quirks
944 if (info->tx_fixup) {
945 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
947 if (netif_msg_tx_err (dev))
948 devdbg (dev, "can't tx_fixup skb");
954 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
955 if (netif_msg_tx_err (dev))
956 devdbg (dev, "no urb");
960 entry = (struct skb_data *) skb->cb;
963 entry->state = tx_start;
964 entry->length = length;
966 usb_fill_bulk_urb (urb, dev->udev, dev->out,
967 skb->data, skb->len, tx_complete, skb);
969 /* don't assume the hardware handles USB_ZERO_PACKET
970 * NOTE: strictly conforming cdc-ether devices should expect
971 * the ZLP here, but ignore the one-byte packet.
973 if ((length % dev->maxpacket) == 0) {
974 urb->transfer_buffer_length++;
975 if (skb_tailroom(skb)) {
976 skb->data[skb->len] = 0;
981 spin_lock_irqsave (&dev->txq.lock, flags);
983 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
985 netif_stop_queue (net);
986 usbnet_defer_kevent (dev, EVENT_TX_HALT);
989 if (netif_msg_tx_err (dev))
990 devdbg (dev, "tx: submit urb err %d", retval);
993 net->trans_start = jiffies;
994 __skb_queue_tail (&dev->txq, skb);
995 if (dev->txq.qlen >= TX_QLEN (dev))
996 netif_stop_queue (net);
998 spin_unlock_irqrestore (&dev->txq.lock, flags);
1001 if (netif_msg_tx_err (dev))
1002 devdbg (dev, "drop, code %d", retval);
1004 retval = NET_XMIT_SUCCESS;
1005 dev->stats.tx_dropped++;
1007 dev_kfree_skb_any (skb);
1009 } else if (netif_msg_tx_queued (dev)) {
1010 devdbg (dev, "> tx, len %d, type 0x%x",
1011 length, skb->protocol);
1017 /*-------------------------------------------------------------------------*/
1019 // tasklet (work deferred from completions, in_irq) or timer
1021 static void usbnet_bh (unsigned long param)
1023 struct usbnet *dev = (struct usbnet *) param;
1024 struct sk_buff *skb;
1025 struct skb_data *entry;
1027 while ((skb = skb_dequeue (&dev->done))) {
1028 entry = (struct skb_data *) skb->cb;
1029 switch (entry->state) {
1031 entry->state = rx_cleanup;
1032 rx_process (dev, skb);
1036 usb_free_urb (entry->urb);
1037 dev_kfree_skb (skb);
1040 devdbg (dev, "bogus skb state %d", entry->state);
1044 // waiting for all pending urbs to complete?
1046 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1047 wake_up (dev->wait);
1050 // or are we maybe short a few urbs?
1051 } else if (netif_running (dev->net)
1052 && netif_device_present (dev->net)
1053 && !timer_pending (&dev->delay)
1054 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1055 int temp = dev->rxq.qlen;
1056 int qlen = RX_QLEN (dev);
1062 // don't refill the queue all at once
1063 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1064 urb = usb_alloc_urb (0, GFP_ATOMIC);
1066 rx_submit (dev, urb, GFP_ATOMIC);
1068 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1069 devdbg (dev, "rxqlen %d --> %d",
1070 temp, dev->rxq.qlen);
1071 if (dev->rxq.qlen < qlen)
1072 tasklet_schedule (&dev->bh);
1074 if (dev->txq.qlen < TX_QLEN (dev))
1075 netif_wake_queue (dev->net);
1081 /*-------------------------------------------------------------------------
1083 * USB Device Driver support
1085 *-------------------------------------------------------------------------*/
1087 // precondition: never called in_interrupt
1089 void usbnet_disconnect (struct usb_interface *intf)
1092 struct usb_device *xdev;
1093 struct net_device *net;
1095 dev = usb_get_intfdata(intf);
1096 usb_set_intfdata(intf, NULL);
1100 xdev = interface_to_usbdev (intf);
1102 if (netif_msg_probe (dev))
1103 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1104 intf->dev.driver->name,
1105 xdev->bus->bus_name, xdev->devpath,
1106 dev->driver_info->description);
1109 unregister_netdev (net);
1111 /* we don't hold rtnl here ... */
1112 flush_scheduled_work ();
1114 if (dev->driver_info->unbind)
1115 dev->driver_info->unbind (dev, intf);
1120 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1123 /*-------------------------------------------------------------------------*/
1125 // precondition: never called in_interrupt
1128 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1131 struct net_device *net;
1132 struct usb_host_interface *interface;
1133 struct driver_info *info;
1134 struct usb_device *xdev;
1137 DECLARE_MAC_BUF(mac);
1139 name = udev->dev.driver->name;
1140 info = (struct driver_info *) prod->driver_info;
1142 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1145 xdev = interface_to_usbdev (udev);
1146 interface = udev->cur_altsetting;
1152 // set up our own records
1153 net = alloc_etherdev(sizeof(*dev));
1155 dbg ("can't kmalloc dev");
1159 dev = netdev_priv(net);
1162 dev->driver_info = info;
1163 dev->driver_name = name;
1164 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1165 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1166 skb_queue_head_init (&dev->rxq);
1167 skb_queue_head_init (&dev->txq);
1168 skb_queue_head_init (&dev->done);
1169 dev->bh.func = usbnet_bh;
1170 dev->bh.data = (unsigned long) dev;
1171 INIT_WORK (&dev->kevent, kevent);
1172 dev->delay.function = usbnet_bh;
1173 dev->delay.data = (unsigned long) dev;
1174 init_timer (&dev->delay);
1175 mutex_init (&dev->phy_mutex);
1178 strcpy (net->name, "usb%d");
1179 memcpy (net->dev_addr, node_id, sizeof node_id);
1181 /* rx and tx sides can use different message sizes;
1182 * bind() should set rx_urb_size in that case.
1184 dev->hard_mtu = net->mtu + net->hard_header_len;
1186 // dma_supported() is deeply broken on almost all architectures
1187 // possible with some EHCI controllers
1188 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1189 net->features |= NETIF_F_HIGHDMA;
1192 net->change_mtu = usbnet_change_mtu;
1193 net->get_stats = usbnet_get_stats;
1194 net->hard_start_xmit = usbnet_start_xmit;
1195 net->open = usbnet_open;
1196 net->stop = usbnet_stop;
1197 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1198 net->tx_timeout = usbnet_tx_timeout;
1199 net->ethtool_ops = &usbnet_ethtool_ops;
1201 // allow device-specific bind/init procedures
1202 // NOTE net->name still not usable ...
1204 status = info->bind (dev, udev);
1208 // heuristic: "usb%d" for links we know are two-host,
1209 // else "eth%d" when there's reasonable doubt. userspace
1210 // can rename the link if it knows better.
1211 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1212 && (net->dev_addr [0] & 0x02) == 0)
1213 strcpy (net->name, "eth%d");
1215 /* maybe the remote can't receive an Ethernet MTU */
1216 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1217 net->mtu = dev->hard_mtu - net->hard_header_len;
1218 } else if (!info->in || !info->out)
1219 status = usbnet_get_endpoints (dev, udev);
1221 dev->in = usb_rcvbulkpipe (xdev, info->in);
1222 dev->out = usb_sndbulkpipe (xdev, info->out);
1223 if (!(info->flags & FLAG_NO_SETINT))
1224 status = usb_set_interface (xdev,
1225 interface->desc.bInterfaceNumber,
1226 interface->desc.bAlternateSetting);
1231 if (status >= 0 && dev->status)
1232 status = init_status (dev, udev);
1236 if (!dev->rx_urb_size)
1237 dev->rx_urb_size = dev->hard_mtu;
1238 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1240 SET_NETDEV_DEV(net, &udev->dev);
1241 status = register_netdev (net);
1244 if (netif_msg_probe (dev))
1245 devinfo (dev, "register '%s' at usb-%s-%s, %s, %s",
1246 udev->dev.driver->name,
1247 xdev->bus->bus_name, xdev->devpath,
1248 dev->driver_info->description,
1249 print_mac(mac, net->dev_addr));
1251 // ok, it's ready to go.
1252 usb_set_intfdata (udev, dev);
1254 // start as if the link is up
1255 netif_device_attach (net);
1261 info->unbind (dev, udev);
1268 EXPORT_SYMBOL_GPL(usbnet_probe);
1270 /*-------------------------------------------------------------------------*/
1273 * suspend the whole driver as soon as the first interface is suspended
1274 * resume only when the last interface is resumed
1277 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1279 struct usbnet *dev = usb_get_intfdata(intf);
1281 if (!dev->suspend_count++) {
1283 * accelerate emptying of the rx and queues, to avoid
1284 * having everything error out.
1286 netif_device_detach (dev->net);
1287 (void) unlink_urbs (dev, &dev->rxq);
1288 (void) unlink_urbs (dev, &dev->txq);
1290 * reattach so runtime management can use and
1293 netif_device_attach (dev->net);
1297 EXPORT_SYMBOL_GPL(usbnet_suspend);
1299 int usbnet_resume (struct usb_interface *intf)
1301 struct usbnet *dev = usb_get_intfdata(intf);
1303 if (!--dev->suspend_count)
1304 tasklet_schedule (&dev->bh);
1308 EXPORT_SYMBOL_GPL(usbnet_resume);
1311 /*-------------------------------------------------------------------------*/
1313 static int __init usbnet_init(void)
1315 /* compiler should optimize this out */
1316 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1317 < sizeof (struct skb_data));
1319 random_ether_addr(node_id);
1322 module_init(usbnet_init);
1324 static void __exit usbnet_exit(void)
1327 module_exit(usbnet_exit);
1329 MODULE_AUTHOR("David Brownell");
1330 MODULE_DESCRIPTION("USB network driver framework");
1331 MODULE_LICENSE("GPL");