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/ctype.h>
41 #include <linux/ethtool.h>
42 #include <linux/workqueue.h>
43 #include <linux/mii.h>
44 #include <linux/usb.h>
45 #include <linux/usb/usbnet.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 u8 nibble(unsigned char c)
162 if (likely(isdigit(c)))
165 if (likely(isxdigit(c)))
170 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
173 unsigned char buf [13];
175 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
177 dev_dbg(&dev->udev->dev,
178 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
183 for (i = tmp = 0; i < 6; i++, tmp += 2)
184 dev->net->dev_addr [i] =
185 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
188 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
190 static void intr_complete (struct urb *urb);
192 static int init_status (struct usbnet *dev, struct usb_interface *intf)
199 if (!dev->driver_info->status)
202 pipe = usb_rcvintpipe (dev->udev,
203 dev->status->desc.bEndpointAddress
204 & USB_ENDPOINT_NUMBER_MASK);
205 maxp = usb_maxpacket (dev->udev, pipe, 0);
207 /* avoid 1 msec chatter: min 8 msec poll rate */
208 period = max ((int) dev->status->desc.bInterval,
209 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
211 buf = kmalloc (maxp, GFP_KERNEL);
213 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
214 if (!dev->interrupt) {
218 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
219 buf, maxp, intr_complete, dev, period);
221 "status ep%din, %d bytes period %d\n",
222 usb_pipeendpoint(pipe), maxp, period);
228 /* Passes this packet up the stack, updating its accounting.
229 * Some link protocols batch packets, so their rx_fixup paths
230 * can return clones as well as just modify the original skb.
232 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
236 skb->protocol = eth_type_trans (skb, dev->net);
237 dev->stats.rx_packets++;
238 dev->stats.rx_bytes += skb->len;
240 if (netif_msg_rx_status (dev))
241 devdbg (dev, "< rx, len %zu, type 0x%x",
242 skb->len + sizeof (struct ethhdr), skb->protocol);
243 memset (skb->cb, 0, sizeof (struct skb_data));
244 status = netif_rx (skb);
245 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
246 devdbg (dev, "netif_rx status %d", status);
248 EXPORT_SYMBOL_GPL(usbnet_skb_return);
251 /*-------------------------------------------------------------------------
253 * Network Device Driver (peer link to "Host Device", from USB host)
255 *-------------------------------------------------------------------------*/
257 int usbnet_change_mtu (struct net_device *net, int new_mtu)
259 struct usbnet *dev = netdev_priv(net);
260 int ll_mtu = new_mtu + net->hard_header_len;
261 int old_hard_mtu = dev->hard_mtu;
262 int old_rx_urb_size = dev->rx_urb_size;
266 // no second zero-length packet read wanted after mtu-sized packets
267 if ((ll_mtu % dev->maxpacket) == 0)
271 dev->hard_mtu = net->mtu + net->hard_header_len;
272 if (dev->rx_urb_size == old_hard_mtu) {
273 dev->rx_urb_size = dev->hard_mtu;
274 if (dev->rx_urb_size > old_rx_urb_size)
275 usbnet_unlink_rx_urbs(dev);
280 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
282 /*-------------------------------------------------------------------------*/
284 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
285 * completion callbacks. 2.5 should have fixed those bugs...
288 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
292 spin_lock_irqsave(&list->lock, flags);
293 __skb_unlink(skb, list);
294 spin_unlock(&list->lock);
295 spin_lock(&dev->done.lock);
296 __skb_queue_tail(&dev->done, skb);
297 if (dev->done.qlen == 1)
298 tasklet_schedule(&dev->bh);
299 spin_unlock_irqrestore(&dev->done.lock, flags);
302 /* some work can't be done in tasklets, so we use keventd
304 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
305 * but tasklet_schedule() doesn't. hope the failure is rare.
307 void usbnet_defer_kevent (struct usbnet *dev, int work)
309 set_bit (work, &dev->flags);
310 if (!schedule_work (&dev->kevent))
311 deverr (dev, "kevent %d may have been dropped", work);
313 devdbg (dev, "kevent %d scheduled", work);
315 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
317 /*-------------------------------------------------------------------------*/
319 static void rx_complete (struct urb *urb);
321 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
324 struct skb_data *entry;
326 unsigned long lockflags;
327 size_t size = dev->rx_urb_size;
329 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
330 if (netif_msg_rx_err (dev))
331 devdbg (dev, "no rx skb");
332 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
336 skb_reserve (skb, NET_IP_ALIGN);
338 entry = (struct skb_data *) skb->cb;
341 entry->state = rx_start;
344 usb_fill_bulk_urb (urb, dev->udev, dev->in,
345 skb->data, size, rx_complete, skb);
347 spin_lock_irqsave (&dev->rxq.lock, lockflags);
349 if (netif_running (dev->net)
350 && netif_device_present (dev->net)
351 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
352 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
354 usbnet_defer_kevent (dev, EVENT_RX_HALT);
357 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
360 if (netif_msg_ifdown (dev))
361 devdbg (dev, "device gone");
362 netif_device_detach (dev->net);
365 if (netif_msg_rx_err (dev))
366 devdbg (dev, "rx submit, %d", retval);
367 tasklet_schedule (&dev->bh);
370 __skb_queue_tail (&dev->rxq, skb);
373 if (netif_msg_ifdown (dev))
374 devdbg (dev, "rx: stopped");
377 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
379 dev_kfree_skb_any (skb);
385 /*-------------------------------------------------------------------------*/
387 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
389 if (dev->driver_info->rx_fixup
390 && !dev->driver_info->rx_fixup (dev, skb))
392 // else network stack removes extra byte if we forced a short packet
395 usbnet_skb_return (dev, skb);
397 if (netif_msg_rx_err (dev))
398 devdbg (dev, "drop");
400 dev->stats.rx_errors++;
401 skb_queue_tail (&dev->done, skb);
405 /*-------------------------------------------------------------------------*/
407 static void rx_complete (struct urb *urb)
409 struct sk_buff *skb = (struct sk_buff *) urb->context;
410 struct skb_data *entry = (struct skb_data *) skb->cb;
411 struct usbnet *dev = entry->dev;
412 int urb_status = urb->status;
414 skb_put (skb, urb->actual_length);
415 entry->state = rx_done;
418 switch (urb_status) {
421 if (skb->len < dev->net->hard_header_len) {
422 entry->state = rx_cleanup;
423 dev->stats.rx_errors++;
424 dev->stats.rx_length_errors++;
425 if (netif_msg_rx_err (dev))
426 devdbg (dev, "rx length %d", skb->len);
430 /* stalls need manual reset. this is rare ... except that
431 * when going through USB 2.0 TTs, unplug appears this way.
432 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
433 * storm, recovering as needed.
436 dev->stats.rx_errors++;
437 usbnet_defer_kevent (dev, EVENT_RX_HALT);
440 /* software-driven interface shutdown */
441 case -ECONNRESET: /* async unlink */
442 case -ESHUTDOWN: /* hardware gone */
443 if (netif_msg_ifdown (dev))
444 devdbg (dev, "rx shutdown, code %d", urb_status);
447 /* we get controller i/o faults during khubd disconnect() delays.
448 * throttle down resubmits, to avoid log floods; just temporarily,
449 * so we still recover when the fault isn't a khubd delay.
454 dev->stats.rx_errors++;
455 if (!timer_pending (&dev->delay)) {
456 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
457 if (netif_msg_link (dev))
458 devdbg (dev, "rx throttle %d", urb_status);
461 entry->state = rx_cleanup;
466 /* data overrun ... flush fifo? */
468 dev->stats.rx_over_errors++;
472 entry->state = rx_cleanup;
473 dev->stats.rx_errors++;
474 if (netif_msg_rx_err (dev))
475 devdbg (dev, "rx status %d", urb_status);
479 defer_bh(dev, skb, &dev->rxq);
482 if (netif_running (dev->net)
483 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
484 rx_submit (dev, urb, GFP_ATOMIC);
489 if (netif_msg_rx_err (dev))
490 devdbg (dev, "no read resubmitted");
493 static void intr_complete (struct urb *urb)
495 struct usbnet *dev = urb->context;
496 int status = urb->status;
501 dev->driver_info->status(dev, urb);
504 /* software-driven interface shutdown */
505 case -ENOENT: /* urb killed */
506 case -ESHUTDOWN: /* hardware gone */
507 if (netif_msg_ifdown (dev))
508 devdbg (dev, "intr shutdown, code %d", status);
511 /* NOTE: not throttling like RX/TX, since this endpoint
512 * already polls infrequently
515 devdbg (dev, "intr status %d", status);
519 if (!netif_running (dev->net))
522 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
523 status = usb_submit_urb (urb, GFP_ATOMIC);
524 if (status != 0 && netif_msg_timer (dev))
525 deverr(dev, "intr resubmit --> %d", status);
528 /*-------------------------------------------------------------------------*/
530 // unlink pending rx/tx; completion handlers do all other cleanup
532 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
535 struct sk_buff *skb, *skbnext;
538 spin_lock_irqsave (&q->lock, flags);
539 skb_queue_walk_safe(q, skb, skbnext) {
540 struct skb_data *entry;
544 entry = (struct skb_data *) skb->cb;
547 // during some PM-driven resume scenarios,
548 // these (async) unlinks complete immediately
549 retval = usb_unlink_urb (urb);
550 if (retval != -EINPROGRESS && retval != 0)
551 devdbg (dev, "unlink urb err, %d", retval);
555 spin_unlock_irqrestore (&q->lock, flags);
559 // Flush all pending rx urbs
560 // minidrivers may need to do this when the MTU changes
562 void usbnet_unlink_rx_urbs(struct usbnet *dev)
564 if (netif_running(dev->net)) {
565 (void) unlink_urbs (dev, &dev->rxq);
566 tasklet_schedule(&dev->bh);
569 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
571 /*-------------------------------------------------------------------------*/
573 // precondition: never called in_interrupt
575 int usbnet_stop (struct net_device *net)
577 struct usbnet *dev = netdev_priv(net);
579 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
580 DECLARE_WAITQUEUE (wait, current);
582 netif_stop_queue (net);
584 if (netif_msg_ifdown (dev))
585 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
586 dev->stats.rx_packets, dev->stats.tx_packets,
587 dev->stats.rx_errors, dev->stats.tx_errors
590 // ensure there are no more active urbs
591 add_wait_queue (&unlink_wakeup, &wait);
592 dev->wait = &unlink_wakeup;
593 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
595 // maybe wait for deletions to finish.
596 while (!skb_queue_empty(&dev->rxq)
597 && !skb_queue_empty(&dev->txq)
598 && !skb_queue_empty(&dev->done)) {
599 msleep(UNLINK_TIMEOUT_MS);
600 if (netif_msg_ifdown (dev))
601 devdbg (dev, "waited for %d urb completions", temp);
604 remove_wait_queue (&unlink_wakeup, &wait);
606 usb_kill_urb(dev->interrupt);
608 /* deferred work (task, timer, softirq) must also stop.
609 * can't flush_scheduled_work() until we drop rtnl (later),
610 * else workers could deadlock; so make workers a NOP.
613 del_timer_sync (&dev->delay);
614 tasklet_kill (&dev->bh);
615 usb_autopm_put_interface(dev->intf);
619 EXPORT_SYMBOL_GPL(usbnet_stop);
621 /*-------------------------------------------------------------------------*/
623 // posts reads, and enables write queuing
625 // precondition: never called in_interrupt
627 int usbnet_open (struct net_device *net)
629 struct usbnet *dev = netdev_priv(net);
631 struct driver_info *info = dev->driver_info;
633 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
634 if (netif_msg_ifup (dev))
636 "resumption fail (%d) usbnet usb-%s-%s, %s",
638 dev->udev->bus->bus_name, dev->udev->devpath,
643 // put into "known safe" state
644 if (info->reset && (retval = info->reset (dev)) < 0) {
645 if (netif_msg_ifup (dev))
647 "open reset fail (%d) usbnet usb-%s-%s, %s",
649 dev->udev->bus->bus_name, dev->udev->devpath,
654 // insist peer be connected
655 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
656 if (netif_msg_ifup (dev))
657 devdbg (dev, "can't open; %d", retval);
661 /* start any status interrupt transfer */
662 if (dev->interrupt) {
663 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
665 if (netif_msg_ifup (dev))
666 deverr (dev, "intr submit %d", retval);
671 netif_start_queue (net);
672 if (netif_msg_ifup (dev)) {
675 if (dev->driver_info->flags & FLAG_FRAMING_NC)
677 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
679 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
681 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
683 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
688 devinfo (dev, "open: enable queueing "
689 "(rx %d, tx %d) mtu %d %s framing",
690 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
694 // delay posting reads until we're fully open
695 tasklet_schedule (&dev->bh);
698 usb_autopm_put_interface(dev->intf);
702 EXPORT_SYMBOL_GPL(usbnet_open);
704 /*-------------------------------------------------------------------------*/
706 /* ethtool methods; minidrivers may need to add some more, but
707 * they'll probably want to use this base set.
710 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
712 struct usbnet *dev = netdev_priv(net);
714 if (!dev->mii.mdio_read)
717 return mii_ethtool_gset(&dev->mii, cmd);
719 EXPORT_SYMBOL_GPL(usbnet_get_settings);
721 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
723 struct usbnet *dev = netdev_priv(net);
726 if (!dev->mii.mdio_write)
729 retval = mii_ethtool_sset(&dev->mii, cmd);
731 /* link speed/duplex might have changed */
732 if (dev->driver_info->link_reset)
733 dev->driver_info->link_reset(dev);
738 EXPORT_SYMBOL_GPL(usbnet_set_settings);
740 u32 usbnet_get_link (struct net_device *net)
742 struct usbnet *dev = netdev_priv(net);
744 /* If a check_connect is defined, return its result */
745 if (dev->driver_info->check_connect)
746 return dev->driver_info->check_connect (dev) == 0;
748 /* if the device has mii operations, use those */
749 if (dev->mii.mdio_read)
750 return mii_link_ok(&dev->mii);
752 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
753 return ethtool_op_get_link(net);
755 EXPORT_SYMBOL_GPL(usbnet_get_link);
757 int usbnet_nway_reset(struct net_device *net)
759 struct usbnet *dev = netdev_priv(net);
761 if (!dev->mii.mdio_write)
764 return mii_nway_restart(&dev->mii);
766 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
768 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
770 struct usbnet *dev = netdev_priv(net);
772 strncpy (info->driver, dev->driver_name, sizeof info->driver);
773 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
774 strncpy (info->fw_version, dev->driver_info->description,
775 sizeof info->fw_version);
776 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
778 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
780 u32 usbnet_get_msglevel (struct net_device *net)
782 struct usbnet *dev = netdev_priv(net);
784 return dev->msg_enable;
786 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
788 void usbnet_set_msglevel (struct net_device *net, u32 level)
790 struct usbnet *dev = netdev_priv(net);
792 dev->msg_enable = level;
794 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
796 /* drivers may override default ethtool_ops in their bind() routine */
797 static struct ethtool_ops usbnet_ethtool_ops = {
798 .get_settings = usbnet_get_settings,
799 .set_settings = usbnet_set_settings,
800 .get_link = usbnet_get_link,
801 .nway_reset = usbnet_nway_reset,
802 .get_drvinfo = usbnet_get_drvinfo,
803 .get_msglevel = usbnet_get_msglevel,
804 .set_msglevel = usbnet_set_msglevel,
807 /*-------------------------------------------------------------------------*/
809 /* work that cannot be done in interrupt context uses keventd.
811 * NOTE: with 2.5 we could do more of this using completion callbacks,
812 * especially now that control transfers can be queued.
815 kevent (struct work_struct *work)
818 container_of(work, struct usbnet, kevent);
821 /* usb_clear_halt() needs a thread context */
822 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
823 unlink_urbs (dev, &dev->txq);
824 status = usb_clear_halt (dev->udev, dev->out);
827 && status != -ESHUTDOWN) {
828 if (netif_msg_tx_err (dev))
829 deverr (dev, "can't clear tx halt, status %d",
832 clear_bit (EVENT_TX_HALT, &dev->flags);
833 if (status != -ESHUTDOWN)
834 netif_wake_queue (dev->net);
837 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
838 unlink_urbs (dev, &dev->rxq);
839 status = usb_clear_halt (dev->udev, dev->in);
842 && status != -ESHUTDOWN) {
843 if (netif_msg_rx_err (dev))
844 deverr (dev, "can't clear rx halt, status %d",
847 clear_bit (EVENT_RX_HALT, &dev->flags);
848 tasklet_schedule (&dev->bh);
852 /* tasklet could resubmit itself forever if memory is tight */
853 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
854 struct urb *urb = NULL;
856 if (netif_running (dev->net))
857 urb = usb_alloc_urb (0, GFP_KERNEL);
859 clear_bit (EVENT_RX_MEMORY, &dev->flags);
861 clear_bit (EVENT_RX_MEMORY, &dev->flags);
862 rx_submit (dev, urb, GFP_KERNEL);
863 tasklet_schedule (&dev->bh);
867 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
868 struct driver_info *info = dev->driver_info;
871 clear_bit (EVENT_LINK_RESET, &dev->flags);
872 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
873 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
875 dev->udev->bus->bus_name, dev->udev->devpath,
881 devdbg (dev, "kevent done, flags = 0x%lx",
885 /*-------------------------------------------------------------------------*/
887 static void tx_complete (struct urb *urb)
889 struct sk_buff *skb = (struct sk_buff *) urb->context;
890 struct skb_data *entry = (struct skb_data *) skb->cb;
891 struct usbnet *dev = entry->dev;
893 if (urb->status == 0) {
894 dev->stats.tx_packets++;
895 dev->stats.tx_bytes += entry->length;
897 dev->stats.tx_errors++;
899 switch (urb->status) {
901 usbnet_defer_kevent (dev, EVENT_TX_HALT);
904 /* software-driven interface shutdown */
905 case -ECONNRESET: // async unlink
906 case -ESHUTDOWN: // hardware gone
909 // like rx, tx gets controller i/o faults during khubd delays
910 // and so it uses the same throttling mechanism.
914 if (!timer_pending (&dev->delay)) {
915 mod_timer (&dev->delay,
916 jiffies + THROTTLE_JIFFIES);
917 if (netif_msg_link (dev))
918 devdbg (dev, "tx throttle %d",
921 netif_stop_queue (dev->net);
924 if (netif_msg_tx_err (dev))
925 devdbg (dev, "tx err %d", entry->urb->status);
931 entry->state = tx_done;
932 defer_bh(dev, skb, &dev->txq);
935 /*-------------------------------------------------------------------------*/
937 void usbnet_tx_timeout (struct net_device *net)
939 struct usbnet *dev = netdev_priv(net);
941 unlink_urbs (dev, &dev->txq);
942 tasklet_schedule (&dev->bh);
944 // FIXME: device recovery -- reset?
946 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
948 /*-------------------------------------------------------------------------*/
950 int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
952 struct usbnet *dev = netdev_priv(net);
954 int retval = NET_XMIT_SUCCESS;
955 struct urb *urb = NULL;
956 struct skb_data *entry;
957 struct driver_info *info = dev->driver_info;
960 // some devices want funky USB-level framing, for
961 // win32 driver (usually) and/or hardware quirks
962 if (info->tx_fixup) {
963 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
965 if (netif_msg_tx_err (dev))
966 devdbg (dev, "can't tx_fixup skb");
972 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
973 if (netif_msg_tx_err (dev))
974 devdbg (dev, "no urb");
978 entry = (struct skb_data *) skb->cb;
981 entry->state = tx_start;
982 entry->length = length;
984 usb_fill_bulk_urb (urb, dev->udev, dev->out,
985 skb->data, skb->len, tx_complete, skb);
987 /* don't assume the hardware handles USB_ZERO_PACKET
988 * NOTE: strictly conforming cdc-ether devices should expect
989 * the ZLP here, but ignore the one-byte packet.
991 if ((length % dev->maxpacket) == 0) {
992 urb->transfer_buffer_length++;
993 if (skb_tailroom(skb)) {
994 skb->data[skb->len] = 0;
999 spin_lock_irqsave (&dev->txq.lock, flags);
1001 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1003 netif_stop_queue (net);
1004 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1007 if (netif_msg_tx_err (dev))
1008 devdbg (dev, "tx: submit urb err %d", retval);
1011 net->trans_start = jiffies;
1012 __skb_queue_tail (&dev->txq, skb);
1013 if (dev->txq.qlen >= TX_QLEN (dev))
1014 netif_stop_queue (net);
1016 spin_unlock_irqrestore (&dev->txq.lock, flags);
1019 if (netif_msg_tx_err (dev))
1020 devdbg (dev, "drop, code %d", retval);
1022 retval = NET_XMIT_SUCCESS;
1023 dev->stats.tx_dropped++;
1025 dev_kfree_skb_any (skb);
1027 } else if (netif_msg_tx_queued (dev)) {
1028 devdbg (dev, "> tx, len %d, type 0x%x",
1029 length, skb->protocol);
1033 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1035 /*-------------------------------------------------------------------------*/
1037 // tasklet (work deferred from completions, in_irq) or timer
1039 static void usbnet_bh (unsigned long param)
1041 struct usbnet *dev = (struct usbnet *) param;
1042 struct sk_buff *skb;
1043 struct skb_data *entry;
1045 while ((skb = skb_dequeue (&dev->done))) {
1046 entry = (struct skb_data *) skb->cb;
1047 switch (entry->state) {
1049 entry->state = rx_cleanup;
1050 rx_process (dev, skb);
1054 usb_free_urb (entry->urb);
1055 dev_kfree_skb (skb);
1058 devdbg (dev, "bogus skb state %d", entry->state);
1062 // waiting for all pending urbs to complete?
1064 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1065 wake_up (dev->wait);
1068 // or are we maybe short a few urbs?
1069 } else if (netif_running (dev->net)
1070 && netif_device_present (dev->net)
1071 && !timer_pending (&dev->delay)
1072 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1073 int temp = dev->rxq.qlen;
1074 int qlen = RX_QLEN (dev);
1080 // don't refill the queue all at once
1081 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1082 urb = usb_alloc_urb (0, GFP_ATOMIC);
1084 rx_submit (dev, urb, GFP_ATOMIC);
1086 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1087 devdbg (dev, "rxqlen %d --> %d",
1088 temp, dev->rxq.qlen);
1089 if (dev->rxq.qlen < qlen)
1090 tasklet_schedule (&dev->bh);
1092 if (dev->txq.qlen < TX_QLEN (dev))
1093 netif_wake_queue (dev->net);
1099 /*-------------------------------------------------------------------------
1101 * USB Device Driver support
1103 *-------------------------------------------------------------------------*/
1105 // precondition: never called in_interrupt
1107 void usbnet_disconnect (struct usb_interface *intf)
1110 struct usb_device *xdev;
1111 struct net_device *net;
1113 dev = usb_get_intfdata(intf);
1114 usb_set_intfdata(intf, NULL);
1118 xdev = interface_to_usbdev (intf);
1120 if (netif_msg_probe (dev))
1121 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1122 intf->dev.driver->name,
1123 xdev->bus->bus_name, xdev->devpath,
1124 dev->driver_info->description);
1127 unregister_netdev (net);
1129 /* we don't hold rtnl here ... */
1130 flush_scheduled_work ();
1132 if (dev->driver_info->unbind)
1133 dev->driver_info->unbind (dev, intf);
1138 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1140 static const struct net_device_ops usbnet_netdev_ops = {
1141 .ndo_open = usbnet_open,
1142 .ndo_stop = usbnet_stop,
1143 .ndo_start_xmit = usbnet_start_xmit,
1144 .ndo_tx_timeout = usbnet_tx_timeout,
1145 .ndo_change_mtu = usbnet_change_mtu,
1146 .ndo_set_mac_address = eth_mac_addr,
1147 .ndo_validate_addr = eth_validate_addr,
1150 /*-------------------------------------------------------------------------*/
1152 // precondition: never called in_interrupt
1155 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1158 struct net_device *net;
1159 struct usb_host_interface *interface;
1160 struct driver_info *info;
1161 struct usb_device *xdev;
1165 name = udev->dev.driver->name;
1166 info = (struct driver_info *) prod->driver_info;
1168 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1171 xdev = interface_to_usbdev (udev);
1172 interface = udev->cur_altsetting;
1178 // set up our own records
1179 net = alloc_etherdev(sizeof(*dev));
1181 dbg ("can't kmalloc dev");
1185 dev = netdev_priv(net);
1188 dev->driver_info = info;
1189 dev->driver_name = name;
1190 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1191 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1192 skb_queue_head_init (&dev->rxq);
1193 skb_queue_head_init (&dev->txq);
1194 skb_queue_head_init (&dev->done);
1195 dev->bh.func = usbnet_bh;
1196 dev->bh.data = (unsigned long) dev;
1197 INIT_WORK (&dev->kevent, kevent);
1198 dev->delay.function = usbnet_bh;
1199 dev->delay.data = (unsigned long) dev;
1200 init_timer (&dev->delay);
1201 mutex_init (&dev->phy_mutex);
1204 strcpy (net->name, "usb%d");
1205 memcpy (net->dev_addr, node_id, sizeof node_id);
1207 /* rx and tx sides can use different message sizes;
1208 * bind() should set rx_urb_size in that case.
1210 dev->hard_mtu = net->mtu + net->hard_header_len;
1212 // dma_supported() is deeply broken on almost all architectures
1213 // possible with some EHCI controllers
1214 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
1215 net->features |= NETIF_F_HIGHDMA;
1218 net->netdev_ops = &usbnet_netdev_ops;
1219 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1220 net->ethtool_ops = &usbnet_ethtool_ops;
1222 // allow device-specific bind/init procedures
1223 // NOTE net->name still not usable ...
1225 status = info->bind (dev, udev);
1229 // heuristic: "usb%d" for links we know are two-host,
1230 // else "eth%d" when there's reasonable doubt. userspace
1231 // can rename the link if it knows better.
1232 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1233 && (net->dev_addr [0] & 0x02) == 0)
1234 strcpy (net->name, "eth%d");
1235 /* WLAN devices should always be named "wlan%d" */
1236 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1237 strcpy(net->name, "wlan%d");
1239 /* maybe the remote can't receive an Ethernet MTU */
1240 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1241 net->mtu = dev->hard_mtu - net->hard_header_len;
1242 } else if (!info->in || !info->out)
1243 status = usbnet_get_endpoints (dev, udev);
1245 dev->in = usb_rcvbulkpipe (xdev, info->in);
1246 dev->out = usb_sndbulkpipe (xdev, info->out);
1247 if (!(info->flags & FLAG_NO_SETINT))
1248 status = usb_set_interface (xdev,
1249 interface->desc.bInterfaceNumber,
1250 interface->desc.bAlternateSetting);
1255 if (status >= 0 && dev->status)
1256 status = init_status (dev, udev);
1260 if (!dev->rx_urb_size)
1261 dev->rx_urb_size = dev->hard_mtu;
1262 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1264 SET_NETDEV_DEV(net, &udev->dev);
1265 status = register_netdev (net);
1268 if (netif_msg_probe (dev))
1269 devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
1270 udev->dev.driver->name,
1271 xdev->bus->bus_name, xdev->devpath,
1272 dev->driver_info->description,
1275 // ok, it's ready to go.
1276 usb_set_intfdata (udev, dev);
1278 // start as if the link is up
1279 netif_device_attach (net);
1285 info->unbind (dev, udev);
1292 EXPORT_SYMBOL_GPL(usbnet_probe);
1294 /*-------------------------------------------------------------------------*/
1297 * suspend the whole driver as soon as the first interface is suspended
1298 * resume only when the last interface is resumed
1301 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1303 struct usbnet *dev = usb_get_intfdata(intf);
1305 if (!dev->suspend_count++) {
1307 * accelerate emptying of the rx and queues, to avoid
1308 * having everything error out.
1310 netif_device_detach (dev->net);
1311 (void) unlink_urbs (dev, &dev->rxq);
1312 (void) unlink_urbs (dev, &dev->txq);
1314 * reattach so runtime management can use and
1317 netif_device_attach (dev->net);
1321 EXPORT_SYMBOL_GPL(usbnet_suspend);
1323 int usbnet_resume (struct usb_interface *intf)
1325 struct usbnet *dev = usb_get_intfdata(intf);
1327 if (!--dev->suspend_count)
1328 tasklet_schedule (&dev->bh);
1332 EXPORT_SYMBOL_GPL(usbnet_resume);
1335 /*-------------------------------------------------------------------------*/
1337 static int __init usbnet_init(void)
1339 /* compiler should optimize this out */
1340 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1341 < sizeof (struct skb_data));
1343 random_ether_addr(node_id);
1346 module_init(usbnet_init);
1348 static void __exit usbnet_exit(void)
1351 module_exit(usbnet_exit);
1353 MODULE_AUTHOR("David Brownell");
1354 MODULE_DESCRIPTION("USB network driver framework");
1355 MODULE_LICENSE("GPL");