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>
44 #include <linux/usb/usbnet.h>
46 #define DRIVER_VERSION "22-Aug-2005"
49 /*-------------------------------------------------------------------------*/
52 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
53 * Several dozen bytes of IPv4 data can fit in two such transactions.
54 * One maximum size Ethernet packet takes twenty four of them.
55 * For high speed, each frame comfortably fits almost 36 max size
56 * Ethernet packets (so queues should be bigger).
58 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
59 * let the USB host controller be busy for 5msec or more before an irq
60 * is required, under load. Jumbograms change the equation.
62 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
63 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
64 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
65 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
66 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
68 // reawaken network queue this soon after stopping; else watchdog barks
69 #define TX_TIMEOUT_JIFFIES (5*HZ)
71 // throttle rx/tx briefly after some faults, so khubd might disconnect()
72 // us (it polls at HZ/4 usually) before we report too many false errors.
73 #define THROTTLE_JIFFIES (HZ/8)
76 #define UNLINK_TIMEOUT_MS 3
78 /*-------------------------------------------------------------------------*/
80 // randomly generated ethernet address
81 static u8 node_id [ETH_ALEN];
83 static const char driver_name [] = "usbnet";
85 /* use ethtool to change the level for any given device */
86 static int msg_level = -1;
87 module_param (msg_level, int, 0);
88 MODULE_PARM_DESC (msg_level, "Override default message level");
90 /*-------------------------------------------------------------------------*/
92 /* handles CDC Ethernet and many other network "bulk data" interfaces */
93 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
96 struct usb_host_interface *alt = NULL;
97 struct usb_host_endpoint *in = NULL, *out = NULL;
98 struct usb_host_endpoint *status = NULL;
100 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
103 in = out = status = NULL;
104 alt = intf->altsetting + tmp;
106 /* take the first altsetting with in-bulk + out-bulk;
107 * remember any status endpoint, just in case;
108 * ignore other endpoints and altsetttings.
110 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
111 struct usb_host_endpoint *e;
114 e = alt->endpoint + ep;
115 switch (e->desc.bmAttributes) {
116 case USB_ENDPOINT_XFER_INT:
117 if (!usb_endpoint_dir_in(&e->desc))
121 case USB_ENDPOINT_XFER_BULK:
126 if (usb_endpoint_dir_in(&e->desc)) {
129 else if (intr && !status)
139 if (!alt || !in || !out)
142 if (alt->desc.bAlternateSetting != 0
143 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
144 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
145 alt->desc.bAlternateSetting);
150 dev->in = usb_rcvbulkpipe (dev->udev,
151 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
152 dev->out = usb_sndbulkpipe (dev->udev,
153 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
154 dev->status = status;
157 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
159 static void intr_complete (struct urb *urb);
161 static int init_status (struct usbnet *dev, struct usb_interface *intf)
168 if (!dev->driver_info->status)
171 pipe = usb_rcvintpipe (dev->udev,
172 dev->status->desc.bEndpointAddress
173 & USB_ENDPOINT_NUMBER_MASK);
174 maxp = usb_maxpacket (dev->udev, pipe, 0);
176 /* avoid 1 msec chatter: min 8 msec poll rate */
177 period = max ((int) dev->status->desc.bInterval,
178 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
180 buf = kmalloc (maxp, GFP_KERNEL);
182 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
183 if (!dev->interrupt) {
187 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
188 buf, maxp, intr_complete, dev, period);
190 "status ep%din, %d bytes period %d\n",
191 usb_pipeendpoint(pipe), maxp, period);
197 /* Passes this packet up the stack, updating its accounting.
198 * Some link protocols batch packets, so their rx_fixup paths
199 * can return clones as well as just modify the original skb.
201 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 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;
230 int old_hard_mtu = dev->hard_mtu;
231 int old_rx_urb_size = dev->rx_urb_size;
235 // no second zero-length packet read wanted after mtu-sized packets
236 if ((ll_mtu % dev->maxpacket) == 0)
240 dev->hard_mtu = net->mtu + net->hard_header_len;
241 if (dev->rx_urb_size == old_hard_mtu) {
242 dev->rx_urb_size = dev->hard_mtu;
243 if (dev->rx_urb_size > old_rx_urb_size)
244 usbnet_unlink_rx_urbs(dev);
249 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
251 /*-------------------------------------------------------------------------*/
253 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
254 * completion callbacks. 2.5 should have fixed those bugs...
257 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
261 spin_lock_irqsave(&list->lock, flags);
262 __skb_unlink(skb, list);
263 spin_unlock(&list->lock);
264 spin_lock(&dev->done.lock);
265 __skb_queue_tail(&dev->done, skb);
266 if (dev->done.qlen == 1)
267 tasklet_schedule(&dev->bh);
268 spin_unlock_irqrestore(&dev->done.lock, flags);
271 /* some work can't be done in tasklets, so we use keventd
273 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
274 * but tasklet_schedule() doesn't. hope the failure is rare.
276 void usbnet_defer_kevent (struct usbnet *dev, int work)
278 set_bit (work, &dev->flags);
279 if (!schedule_work (&dev->kevent))
280 deverr (dev, "kevent %d may have been dropped", work);
282 devdbg (dev, "kevent %d scheduled", work);
284 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
286 /*-------------------------------------------------------------------------*/
288 static void rx_complete (struct urb *urb);
290 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
293 struct skb_data *entry;
295 unsigned long lockflags;
296 size_t size = dev->rx_urb_size;
298 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
299 if (netif_msg_rx_err (dev))
300 devdbg (dev, "no rx skb");
301 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
305 skb_reserve (skb, NET_IP_ALIGN);
307 entry = (struct skb_data *) skb->cb;
310 entry->state = rx_start;
313 usb_fill_bulk_urb (urb, dev->udev, dev->in,
314 skb->data, size, rx_complete, skb);
316 spin_lock_irqsave (&dev->rxq.lock, lockflags);
318 if (netif_running (dev->net)
319 && netif_device_present (dev->net)
320 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
321 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
323 usbnet_defer_kevent (dev, EVENT_RX_HALT);
326 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
329 if (netif_msg_ifdown (dev))
330 devdbg (dev, "device gone");
331 netif_device_detach (dev->net);
334 if (netif_msg_rx_err (dev))
335 devdbg (dev, "rx submit, %d", retval);
336 tasklet_schedule (&dev->bh);
339 __skb_queue_tail (&dev->rxq, skb);
342 if (netif_msg_ifdown (dev))
343 devdbg (dev, "rx: stopped");
346 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
348 dev_kfree_skb_any (skb);
354 /*-------------------------------------------------------------------------*/
356 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
358 if (dev->driver_info->rx_fixup
359 && !dev->driver_info->rx_fixup (dev, skb))
361 // else network stack removes extra byte if we forced a short packet
364 usbnet_skb_return (dev, skb);
366 if (netif_msg_rx_err (dev))
367 devdbg (dev, "drop");
369 dev->stats.rx_errors++;
370 skb_queue_tail (&dev->done, skb);
374 /*-------------------------------------------------------------------------*/
376 static void rx_complete (struct urb *urb)
378 struct sk_buff *skb = (struct sk_buff *) urb->context;
379 struct skb_data *entry = (struct skb_data *) skb->cb;
380 struct usbnet *dev = entry->dev;
381 int urb_status = urb->status;
383 skb_put (skb, urb->actual_length);
384 entry->state = rx_done;
387 switch (urb_status) {
390 if (skb->len < dev->net->hard_header_len) {
391 entry->state = rx_cleanup;
392 dev->stats.rx_errors++;
393 dev->stats.rx_length_errors++;
394 if (netif_msg_rx_err (dev))
395 devdbg (dev, "rx length %d", skb->len);
399 /* stalls need manual reset. this is rare ... except that
400 * when going through USB 2.0 TTs, unplug appears this way.
401 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
402 * storm, recovering as needed.
405 dev->stats.rx_errors++;
406 usbnet_defer_kevent (dev, EVENT_RX_HALT);
409 /* software-driven interface shutdown */
410 case -ECONNRESET: /* async unlink */
411 case -ESHUTDOWN: /* hardware gone */
412 if (netif_msg_ifdown (dev))
413 devdbg (dev, "rx shutdown, code %d", urb_status);
416 /* we get controller i/o faults during khubd disconnect() delays.
417 * throttle down resubmits, to avoid log floods; just temporarily,
418 * so we still recover when the fault isn't a khubd delay.
423 dev->stats.rx_errors++;
424 if (!timer_pending (&dev->delay)) {
425 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
426 if (netif_msg_link (dev))
427 devdbg (dev, "rx throttle %d", urb_status);
430 entry->state = rx_cleanup;
435 /* data overrun ... flush fifo? */
437 dev->stats.rx_over_errors++;
441 entry->state = rx_cleanup;
442 dev->stats.rx_errors++;
443 if (netif_msg_rx_err (dev))
444 devdbg (dev, "rx status %d", urb_status);
448 defer_bh(dev, skb, &dev->rxq);
451 if (netif_running (dev->net)
452 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
453 rx_submit (dev, urb, GFP_ATOMIC);
458 if (netif_msg_rx_err (dev))
459 devdbg (dev, "no read resubmitted");
462 static void intr_complete (struct urb *urb)
464 struct usbnet *dev = urb->context;
465 int status = urb->status;
470 dev->driver_info->status(dev, urb);
473 /* software-driven interface shutdown */
474 case -ENOENT: /* urb killed */
475 case -ESHUTDOWN: /* hardware gone */
476 if (netif_msg_ifdown (dev))
477 devdbg (dev, "intr shutdown, code %d", status);
480 /* NOTE: not throttling like RX/TX, since this endpoint
481 * already polls infrequently
484 devdbg (dev, "intr status %d", status);
488 if (!netif_running (dev->net))
491 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
492 status = usb_submit_urb (urb, GFP_ATOMIC);
493 if (status != 0 && netif_msg_timer (dev))
494 deverr(dev, "intr resubmit --> %d", status);
497 /*-------------------------------------------------------------------------*/
499 // unlink pending rx/tx; completion handlers do all other cleanup
501 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
504 struct sk_buff *skb, *skbnext;
507 spin_lock_irqsave (&q->lock, flags);
508 skb_queue_walk_safe(q, skb, skbnext) {
509 struct skb_data *entry;
513 entry = (struct skb_data *) skb->cb;
516 // during some PM-driven resume scenarios,
517 // these (async) unlinks complete immediately
518 retval = usb_unlink_urb (urb);
519 if (retval != -EINPROGRESS && retval != 0)
520 devdbg (dev, "unlink urb err, %d", retval);
524 spin_unlock_irqrestore (&q->lock, flags);
528 // Flush all pending rx urbs
529 // minidrivers may need to do this when the MTU changes
531 void usbnet_unlink_rx_urbs(struct usbnet *dev)
533 if (netif_running(dev->net)) {
534 (void) unlink_urbs (dev, &dev->rxq);
535 tasklet_schedule(&dev->bh);
538 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
540 /*-------------------------------------------------------------------------*/
542 // precondition: never called in_interrupt
544 int usbnet_stop (struct net_device *net)
546 struct usbnet *dev = netdev_priv(net);
548 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
549 DECLARE_WAITQUEUE (wait, current);
551 netif_stop_queue (net);
553 if (netif_msg_ifdown (dev))
554 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
555 dev->stats.rx_packets, dev->stats.tx_packets,
556 dev->stats.rx_errors, dev->stats.tx_errors
559 // ensure there are no more active urbs
560 add_wait_queue (&unlink_wakeup, &wait);
561 dev->wait = &unlink_wakeup;
562 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
564 // maybe wait for deletions to finish.
565 while (!skb_queue_empty(&dev->rxq)
566 && !skb_queue_empty(&dev->txq)
567 && !skb_queue_empty(&dev->done)) {
568 msleep(UNLINK_TIMEOUT_MS);
569 if (netif_msg_ifdown (dev))
570 devdbg (dev, "waited for %d urb completions", temp);
573 remove_wait_queue (&unlink_wakeup, &wait);
575 usb_kill_urb(dev->interrupt);
577 /* deferred work (task, timer, softirq) must also stop.
578 * can't flush_scheduled_work() until we drop rtnl (later),
579 * else workers could deadlock; so make workers a NOP.
582 del_timer_sync (&dev->delay);
583 tasklet_kill (&dev->bh);
584 usb_autopm_put_interface(dev->intf);
588 EXPORT_SYMBOL_GPL(usbnet_stop);
590 /*-------------------------------------------------------------------------*/
592 // posts reads, and enables write queuing
594 // precondition: never called in_interrupt
596 int usbnet_open (struct net_device *net)
598 struct usbnet *dev = netdev_priv(net);
600 struct driver_info *info = dev->driver_info;
602 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
603 if (netif_msg_ifup (dev))
605 "resumption fail (%d) usbnet usb-%s-%s, %s",
607 dev->udev->bus->bus_name, dev->udev->devpath,
612 // put into "known safe" state
613 if (info->reset && (retval = info->reset (dev)) < 0) {
614 if (netif_msg_ifup (dev))
616 "open reset fail (%d) usbnet usb-%s-%s, %s",
618 dev->udev->bus->bus_name, dev->udev->devpath,
623 // insist peer be connected
624 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
625 if (netif_msg_ifup (dev))
626 devdbg (dev, "can't open; %d", retval);
630 /* start any status interrupt transfer */
631 if (dev->interrupt) {
632 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
634 if (netif_msg_ifup (dev))
635 deverr (dev, "intr submit %d", retval);
640 netif_start_queue (net);
641 if (netif_msg_ifup (dev)) {
644 if (dev->driver_info->flags & FLAG_FRAMING_NC)
646 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
648 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
650 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
652 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
657 devinfo (dev, "open: enable queueing "
658 "(rx %d, tx %d) mtu %d %s framing",
659 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
663 // delay posting reads until we're fully open
664 tasklet_schedule (&dev->bh);
667 usb_autopm_put_interface(dev->intf);
671 EXPORT_SYMBOL_GPL(usbnet_open);
673 /*-------------------------------------------------------------------------*/
675 /* ethtool methods; minidrivers may need to add some more, but
676 * they'll probably want to use this base set.
679 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
681 struct usbnet *dev = netdev_priv(net);
683 if (!dev->mii.mdio_read)
686 return mii_ethtool_gset(&dev->mii, cmd);
688 EXPORT_SYMBOL_GPL(usbnet_get_settings);
690 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
692 struct usbnet *dev = netdev_priv(net);
695 if (!dev->mii.mdio_write)
698 retval = mii_ethtool_sset(&dev->mii, cmd);
700 /* link speed/duplex might have changed */
701 if (dev->driver_info->link_reset)
702 dev->driver_info->link_reset(dev);
707 EXPORT_SYMBOL_GPL(usbnet_set_settings);
709 u32 usbnet_get_link (struct net_device *net)
711 struct usbnet *dev = netdev_priv(net);
713 /* If a check_connect is defined, return its result */
714 if (dev->driver_info->check_connect)
715 return dev->driver_info->check_connect (dev) == 0;
717 /* if the device has mii operations, use those */
718 if (dev->mii.mdio_read)
719 return mii_link_ok(&dev->mii);
721 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
722 return ethtool_op_get_link(net);
724 EXPORT_SYMBOL_GPL(usbnet_get_link);
726 int usbnet_nway_reset(struct net_device *net)
728 struct usbnet *dev = netdev_priv(net);
730 if (!dev->mii.mdio_write)
733 return mii_nway_restart(&dev->mii);
735 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
737 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
739 struct usbnet *dev = netdev_priv(net);
741 strncpy (info->driver, dev->driver_name, sizeof info->driver);
742 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
743 strncpy (info->fw_version, dev->driver_info->description,
744 sizeof info->fw_version);
745 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
747 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
749 u32 usbnet_get_msglevel (struct net_device *net)
751 struct usbnet *dev = netdev_priv(net);
753 return dev->msg_enable;
755 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
757 void usbnet_set_msglevel (struct net_device *net, u32 level)
759 struct usbnet *dev = netdev_priv(net);
761 dev->msg_enable = level;
763 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
765 /* drivers may override default ethtool_ops in their bind() routine */
766 static struct ethtool_ops usbnet_ethtool_ops = {
767 .get_settings = usbnet_get_settings,
768 .set_settings = usbnet_set_settings,
769 .get_link = usbnet_get_link,
770 .nway_reset = usbnet_nway_reset,
771 .get_drvinfo = usbnet_get_drvinfo,
772 .get_msglevel = usbnet_get_msglevel,
773 .set_msglevel = usbnet_set_msglevel,
776 /*-------------------------------------------------------------------------*/
778 /* work that cannot be done in interrupt context uses keventd.
780 * NOTE: with 2.5 we could do more of this using completion callbacks,
781 * especially now that control transfers can be queued.
784 kevent (struct work_struct *work)
787 container_of(work, struct usbnet, kevent);
790 /* usb_clear_halt() needs a thread context */
791 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
792 unlink_urbs (dev, &dev->txq);
793 status = usb_clear_halt (dev->udev, dev->out);
796 && status != -ESHUTDOWN) {
797 if (netif_msg_tx_err (dev))
798 deverr (dev, "can't clear tx halt, status %d",
801 clear_bit (EVENT_TX_HALT, &dev->flags);
802 if (status != -ESHUTDOWN)
803 netif_wake_queue (dev->net);
806 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
807 unlink_urbs (dev, &dev->rxq);
808 status = usb_clear_halt (dev->udev, dev->in);
811 && status != -ESHUTDOWN) {
812 if (netif_msg_rx_err (dev))
813 deverr (dev, "can't clear rx halt, status %d",
816 clear_bit (EVENT_RX_HALT, &dev->flags);
817 tasklet_schedule (&dev->bh);
821 /* tasklet could resubmit itself forever if memory is tight */
822 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
823 struct urb *urb = NULL;
825 if (netif_running (dev->net))
826 urb = usb_alloc_urb (0, GFP_KERNEL);
828 clear_bit (EVENT_RX_MEMORY, &dev->flags);
830 clear_bit (EVENT_RX_MEMORY, &dev->flags);
831 rx_submit (dev, urb, GFP_KERNEL);
832 tasklet_schedule (&dev->bh);
836 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
837 struct driver_info *info = dev->driver_info;
840 clear_bit (EVENT_LINK_RESET, &dev->flags);
841 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
842 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
844 dev->udev->bus->bus_name, dev->udev->devpath,
850 devdbg (dev, "kevent done, flags = 0x%lx",
854 /*-------------------------------------------------------------------------*/
856 static void tx_complete (struct urb *urb)
858 struct sk_buff *skb = (struct sk_buff *) urb->context;
859 struct skb_data *entry = (struct skb_data *) skb->cb;
860 struct usbnet *dev = entry->dev;
862 if (urb->status == 0) {
863 dev->stats.tx_packets++;
864 dev->stats.tx_bytes += entry->length;
866 dev->stats.tx_errors++;
868 switch (urb->status) {
870 usbnet_defer_kevent (dev, EVENT_TX_HALT);
873 /* software-driven interface shutdown */
874 case -ECONNRESET: // async unlink
875 case -ESHUTDOWN: // hardware gone
878 // like rx, tx gets controller i/o faults during khubd delays
879 // and so it uses the same throttling mechanism.
883 if (!timer_pending (&dev->delay)) {
884 mod_timer (&dev->delay,
885 jiffies + THROTTLE_JIFFIES);
886 if (netif_msg_link (dev))
887 devdbg (dev, "tx throttle %d",
890 netif_stop_queue (dev->net);
893 if (netif_msg_tx_err (dev))
894 devdbg (dev, "tx err %d", entry->urb->status);
900 entry->state = tx_done;
901 defer_bh(dev, skb, &dev->txq);
904 /*-------------------------------------------------------------------------*/
906 void usbnet_tx_timeout (struct net_device *net)
908 struct usbnet *dev = netdev_priv(net);
910 unlink_urbs (dev, &dev->txq);
911 tasklet_schedule (&dev->bh);
913 // FIXME: device recovery -- reset?
915 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
917 /*-------------------------------------------------------------------------*/
919 int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
921 struct usbnet *dev = netdev_priv(net);
923 int retval = NET_XMIT_SUCCESS;
924 struct urb *urb = NULL;
925 struct skb_data *entry;
926 struct driver_info *info = dev->driver_info;
929 // some devices want funky USB-level framing, for
930 // win32 driver (usually) and/or hardware quirks
931 if (info->tx_fixup) {
932 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
934 if (netif_msg_tx_err (dev))
935 devdbg (dev, "can't tx_fixup skb");
941 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
942 if (netif_msg_tx_err (dev))
943 devdbg (dev, "no urb");
947 entry = (struct skb_data *) skb->cb;
950 entry->state = tx_start;
951 entry->length = length;
953 usb_fill_bulk_urb (urb, dev->udev, dev->out,
954 skb->data, skb->len, tx_complete, skb);
956 /* don't assume the hardware handles USB_ZERO_PACKET
957 * NOTE: strictly conforming cdc-ether devices should expect
958 * the ZLP here, but ignore the one-byte packet.
960 if ((length % dev->maxpacket) == 0) {
961 urb->transfer_buffer_length++;
962 if (skb_tailroom(skb)) {
963 skb->data[skb->len] = 0;
968 spin_lock_irqsave (&dev->txq.lock, flags);
970 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
972 netif_stop_queue (net);
973 usbnet_defer_kevent (dev, EVENT_TX_HALT);
976 if (netif_msg_tx_err (dev))
977 devdbg (dev, "tx: submit urb err %d", retval);
980 net->trans_start = jiffies;
981 __skb_queue_tail (&dev->txq, skb);
982 if (dev->txq.qlen >= TX_QLEN (dev))
983 netif_stop_queue (net);
985 spin_unlock_irqrestore (&dev->txq.lock, flags);
988 if (netif_msg_tx_err (dev))
989 devdbg (dev, "drop, code %d", retval);
991 retval = NET_XMIT_SUCCESS;
992 dev->stats.tx_dropped++;
994 dev_kfree_skb_any (skb);
996 } else if (netif_msg_tx_queued (dev)) {
997 devdbg (dev, "> tx, len %d, type 0x%x",
998 length, skb->protocol);
1002 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1004 /*-------------------------------------------------------------------------*/
1006 // tasklet (work deferred from completions, in_irq) or timer
1008 static void usbnet_bh (unsigned long param)
1010 struct usbnet *dev = (struct usbnet *) param;
1011 struct sk_buff *skb;
1012 struct skb_data *entry;
1014 while ((skb = skb_dequeue (&dev->done))) {
1015 entry = (struct skb_data *) skb->cb;
1016 switch (entry->state) {
1018 entry->state = rx_cleanup;
1019 rx_process (dev, skb);
1023 usb_free_urb (entry->urb);
1024 dev_kfree_skb (skb);
1027 devdbg (dev, "bogus skb state %d", entry->state);
1031 // waiting for all pending urbs to complete?
1033 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1034 wake_up (dev->wait);
1037 // or are we maybe short a few urbs?
1038 } else if (netif_running (dev->net)
1039 && netif_device_present (dev->net)
1040 && !timer_pending (&dev->delay)
1041 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1042 int temp = dev->rxq.qlen;
1043 int qlen = RX_QLEN (dev);
1049 // don't refill the queue all at once
1050 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1051 urb = usb_alloc_urb (0, GFP_ATOMIC);
1053 rx_submit (dev, urb, GFP_ATOMIC);
1055 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1056 devdbg (dev, "rxqlen %d --> %d",
1057 temp, dev->rxq.qlen);
1058 if (dev->rxq.qlen < qlen)
1059 tasklet_schedule (&dev->bh);
1061 if (dev->txq.qlen < TX_QLEN (dev))
1062 netif_wake_queue (dev->net);
1068 /*-------------------------------------------------------------------------
1070 * USB Device Driver support
1072 *-------------------------------------------------------------------------*/
1074 // precondition: never called in_interrupt
1076 void usbnet_disconnect (struct usb_interface *intf)
1079 struct usb_device *xdev;
1080 struct net_device *net;
1082 dev = usb_get_intfdata(intf);
1083 usb_set_intfdata(intf, NULL);
1087 xdev = interface_to_usbdev (intf);
1089 if (netif_msg_probe (dev))
1090 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1091 intf->dev.driver->name,
1092 xdev->bus->bus_name, xdev->devpath,
1093 dev->driver_info->description);
1096 unregister_netdev (net);
1098 /* we don't hold rtnl here ... */
1099 flush_scheduled_work ();
1101 if (dev->driver_info->unbind)
1102 dev->driver_info->unbind (dev, intf);
1107 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1109 static const struct net_device_ops usbnet_netdev_ops = {
1110 .ndo_open = usbnet_open,
1111 .ndo_stop = usbnet_stop,
1112 .ndo_start_xmit = usbnet_start_xmit,
1113 .ndo_tx_timeout = usbnet_tx_timeout,
1114 .ndo_change_mtu = usbnet_change_mtu,
1115 .ndo_set_mac_address = eth_mac_addr,
1116 .ndo_validate_addr = eth_validate_addr,
1119 /*-------------------------------------------------------------------------*/
1121 // precondition: never called in_interrupt
1124 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1127 struct net_device *net;
1128 struct usb_host_interface *interface;
1129 struct driver_info *info;
1130 struct usb_device *xdev;
1134 name = udev->dev.driver->name;
1135 info = (struct driver_info *) prod->driver_info;
1137 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1140 xdev = interface_to_usbdev (udev);
1141 interface = udev->cur_altsetting;
1147 // set up our own records
1148 net = alloc_etherdev(sizeof(*dev));
1150 dbg ("can't kmalloc dev");
1154 dev = netdev_priv(net);
1157 dev->driver_info = info;
1158 dev->driver_name = name;
1159 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1160 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1161 skb_queue_head_init (&dev->rxq);
1162 skb_queue_head_init (&dev->txq);
1163 skb_queue_head_init (&dev->done);
1164 dev->bh.func = usbnet_bh;
1165 dev->bh.data = (unsigned long) dev;
1166 INIT_WORK (&dev->kevent, kevent);
1167 dev->delay.function = usbnet_bh;
1168 dev->delay.data = (unsigned long) dev;
1169 init_timer (&dev->delay);
1170 mutex_init (&dev->phy_mutex);
1173 strcpy (net->name, "usb%d");
1174 memcpy (net->dev_addr, node_id, sizeof node_id);
1176 /* rx and tx sides can use different message sizes;
1177 * bind() should set rx_urb_size in that case.
1179 dev->hard_mtu = net->mtu + net->hard_header_len;
1181 // dma_supported() is deeply broken on almost all architectures
1182 // possible with some EHCI controllers
1183 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1184 net->features |= NETIF_F_HIGHDMA;
1187 net->netdev_ops = &usbnet_netdev_ops;
1188 #ifdef CONFIG_COMPAT_NET_DEV_OPS
1189 net->hard_start_xmit = usbnet_start_xmit;
1190 net->open = usbnet_open;
1191 net->stop = usbnet_stop;
1192 net->tx_timeout = usbnet_tx_timeout;
1194 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1195 net->ethtool_ops = &usbnet_ethtool_ops;
1197 // allow device-specific bind/init procedures
1198 // NOTE net->name still not usable ...
1200 status = info->bind (dev, udev);
1204 // heuristic: "usb%d" for links we know are two-host,
1205 // else "eth%d" when there's reasonable doubt. userspace
1206 // can rename the link if it knows better.
1207 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1208 && (net->dev_addr [0] & 0x02) == 0)
1209 strcpy (net->name, "eth%d");
1210 /* WLAN devices should always be named "wlan%d" */
1211 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1212 strcpy(net->name, "wlan%d");
1214 /* maybe the remote can't receive an Ethernet MTU */
1215 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1216 net->mtu = dev->hard_mtu - net->hard_header_len;
1217 } else if (!info->in || !info->out)
1218 status = usbnet_get_endpoints (dev, udev);
1220 dev->in = usb_rcvbulkpipe (xdev, info->in);
1221 dev->out = usb_sndbulkpipe (xdev, info->out);
1222 if (!(info->flags & FLAG_NO_SETINT))
1223 status = usb_set_interface (xdev,
1224 interface->desc.bInterfaceNumber,
1225 interface->desc.bAlternateSetting);
1230 if (status >= 0 && dev->status)
1231 status = init_status (dev, udev);
1235 if (!dev->rx_urb_size)
1236 dev->rx_urb_size = dev->hard_mtu;
1237 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1239 SET_NETDEV_DEV(net, &udev->dev);
1240 status = register_netdev (net);
1243 if (netif_msg_probe (dev))
1244 devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
1245 udev->dev.driver->name,
1246 xdev->bus->bus_name, xdev->devpath,
1247 dev->driver_info->description,
1250 // ok, it's ready to go.
1251 usb_set_intfdata (udev, dev);
1253 // start as if the link is up
1254 netif_device_attach (net);
1260 info->unbind (dev, udev);
1267 EXPORT_SYMBOL_GPL(usbnet_probe);
1269 /*-------------------------------------------------------------------------*/
1272 * suspend the whole driver as soon as the first interface is suspended
1273 * resume only when the last interface is resumed
1276 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1278 struct usbnet *dev = usb_get_intfdata(intf);
1280 if (!dev->suspend_count++) {
1282 * accelerate emptying of the rx and queues, to avoid
1283 * having everything error out.
1285 netif_device_detach (dev->net);
1286 (void) unlink_urbs (dev, &dev->rxq);
1287 (void) unlink_urbs (dev, &dev->txq);
1289 * reattach so runtime management can use and
1292 netif_device_attach (dev->net);
1296 EXPORT_SYMBOL_GPL(usbnet_suspend);
1298 int usbnet_resume (struct usb_interface *intf)
1300 struct usbnet *dev = usb_get_intfdata(intf);
1302 if (!--dev->suspend_count)
1303 tasklet_schedule (&dev->bh);
1307 EXPORT_SYMBOL_GPL(usbnet_resume);
1310 /*-------------------------------------------------------------------------*/
1312 static int __init usbnet_init(void)
1314 /* compiler should optimize this out */
1315 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1316 < sizeof (struct skb_data));
1318 random_ether_addr(node_id);
1321 module_init(usbnet_init);
1323 static void __exit usbnet_exit(void)
1326 module_exit(usbnet_exit);
1328 MODULE_AUTHOR("David Brownell");
1329 MODULE_DESCRIPTION("USB network driver framework");
1330 MODULE_LICENSE("GPL");