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>
46 #include <linux/slab.h>
47 #include <linux/kernel.h>
48 #include <linux/pm_runtime.h>
50 #define DRIVER_VERSION "22-Aug-2005"
52 static char version[] =
53 KERN_INFO "USBNET Framwork for ASIX USB Ethernet Adapter:3.2.101 Beta6"
54 " " __TIME__ " " __DATE__ "\n";
56 /*-------------------------------------------------------------------------*/
59 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
60 * Several dozen bytes of IPv4 data can fit in two such transactions.
61 * One maximum size Ethernet packet takes twenty four of them.
62 * For high speed, each frame comfortably fits almost 36 max size
63 * Ethernet packets (so queues should be bigger).
65 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
66 * let the USB host controller be busy for 5msec or more before an irq
67 * is required, under load. Jumbograms change the equation.
69 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
70 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
71 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
72 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
73 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
75 // reawaken network queue this soon after stopping; else watchdog barks
76 #define TX_TIMEOUT_JIFFIES (5*HZ)
78 // throttle rx/tx briefly after some faults, so khubd might disconnect()
79 // us (it polls at HZ/4 usually) before we report too many false errors.
80 #define THROTTLE_JIFFIES (HZ/8)
83 #define UNLINK_TIMEOUT_MS 3
85 /*-------------------------------------------------------------------------*/
87 // randomly generated ethernet address
88 static u8 node_id [ETH_ALEN];
90 static const char driver_name [] = "usbnet";
92 /* use ethtool to change the level for any given device */
93 static int msg_level = -1;
94 module_param (msg_level, int, 0);
95 MODULE_PARM_DESC (msg_level, "Override default message level");
97 /*-------------------------------------------------------------------------*/
99 /* handles CDC Ethernet and many other network "bulk data" interfaces */
100 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
103 struct usb_host_interface *alt = NULL;
104 struct usb_host_endpoint *in = NULL, *out = NULL;
105 struct usb_host_endpoint *status = NULL;
107 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
110 in = out = status = NULL;
111 alt = intf->altsetting + tmp;
113 /* take the first altsetting with in-bulk + out-bulk;
114 * remember any status endpoint, just in case;
115 * ignore other endpoints and altsettings.
117 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
118 struct usb_host_endpoint *e;
121 e = alt->endpoint + ep;
122 switch (e->desc.bmAttributes) {
123 case USB_ENDPOINT_XFER_INT:
124 if (!usb_endpoint_dir_in(&e->desc))
128 case USB_ENDPOINT_XFER_BULK:
133 if (usb_endpoint_dir_in(&e->desc)) {
136 else if (intr && !status)
146 if (!alt || !in || !out)
149 if (alt->desc.bAlternateSetting != 0 ||
150 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
151 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
152 alt->desc.bAlternateSetting);
157 dev->in = usb_rcvbulkpipe (dev->udev,
158 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
159 dev->out = usb_sndbulkpipe (dev->udev,
160 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
161 dev->status = status;
164 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
166 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
169 unsigned char buf [13];
171 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
173 dev_dbg(&dev->udev->dev,
174 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
179 for (i = tmp = 0; i < 6; i++, tmp += 2)
180 dev->net->dev_addr [i] =
181 (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]);
184 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
186 static void intr_complete (struct urb *urb);
188 static int init_status (struct usbnet *dev, struct usb_interface *intf)
195 if (!dev->driver_info->status)
198 pipe = usb_rcvintpipe (dev->udev,
199 dev->status->desc.bEndpointAddress
200 & USB_ENDPOINT_NUMBER_MASK);
201 maxp = usb_maxpacket (dev->udev, pipe, 0);
203 /* avoid 1 msec chatter: min 8 msec poll rate */
204 period = max ((int) dev->status->desc.bInterval,
205 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
207 buf = kmalloc (maxp, GFP_KERNEL);
209 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
210 if (!dev->interrupt) {
214 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
215 buf, maxp, intr_complete, dev, period);
217 "status ep%din, %d bytes period %d\n",
218 usb_pipeendpoint(pipe), maxp, period);
224 /* Passes this packet up the stack, updating its accounting.
225 * Some link protocols batch packets, so their rx_fixup paths
226 * can return clones as well as just modify the original skb.
228 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
232 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
233 skb_queue_tail(&dev->rxq_pause, skb);
237 skb->protocol = eth_type_trans (skb, dev->net);
238 dev->net->stats.rx_packets++;
239 dev->net->stats.rx_bytes += skb->len;
241 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
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)
246 netif_dbg(dev, rx_err, dev->net,
247 "netif_rx status %d\n", status);
249 EXPORT_SYMBOL_GPL(usbnet_skb_return);
252 /*-------------------------------------------------------------------------
254 * Network Device Driver (peer link to "Host Device", from USB host)
256 *-------------------------------------------------------------------------*/
258 int usbnet_change_mtu (struct net_device *net, int new_mtu)
260 struct usbnet *dev = netdev_priv(net);
261 int ll_mtu = new_mtu + net->hard_header_len;
262 int old_hard_mtu = dev->hard_mtu;
263 int old_rx_urb_size = dev->rx_urb_size;
267 // no second zero-length packet read wanted after mtu-sized packets
268 if ((ll_mtu % dev->maxpacket) == 0)
272 dev->hard_mtu = net->mtu + net->hard_header_len;
273 if (dev->rx_urb_size == old_hard_mtu) {
274 dev->rx_urb_size = dev->hard_mtu;
275 if (dev->rx_urb_size > old_rx_urb_size)
276 usbnet_unlink_rx_urbs(dev);
281 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
283 /* The caller must hold list->lock */
284 static void __usbnet_queue_skb(struct sk_buff_head *list,
285 struct sk_buff *newsk, enum skb_state state)
287 struct skb_data *entry = (struct skb_data *) newsk->cb;
289 __skb_queue_tail(list, newsk);
290 entry->state = state;
293 /*-------------------------------------------------------------------------*/
295 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
296 * completion callbacks. 2.5 should have fixed those bugs...
299 static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
300 struct sk_buff_head *list, enum skb_state state)
303 enum skb_state old_state;
304 struct skb_data *entry = (struct skb_data *) skb->cb;
306 spin_lock_irqsave(&list->lock, flags);
307 old_state = entry->state;
308 entry->state = state;
309 __skb_unlink(skb, list);
310 spin_unlock(&list->lock);
311 spin_lock(&dev->done.lock);
312 __skb_queue_tail(&dev->done, skb);
313 if (dev->done.qlen == 1)
314 tasklet_schedule(&dev->bh);
315 spin_unlock_irqrestore(&dev->done.lock, flags);
319 /* some work can't be done in tasklets, so we use keventd
321 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
322 * but tasklet_schedule() doesn't. hope the failure is rare.
324 void usbnet_defer_kevent (struct usbnet *dev, int work)
326 set_bit (work, &dev->flags);
327 if (!schedule_work (&dev->kevent))
328 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
330 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
332 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
334 /*-------------------------------------------------------------------------*/
336 static void rx_complete (struct urb *urb);
338 static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
341 struct skb_data *entry;
343 unsigned long lockflags;
344 size_t size = dev->rx_urb_size;
346 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
347 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
348 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
352 //skb_reserve (skb, NET_IP_ALIGN); //ylz++
354 entry = (struct skb_data *) skb->cb;
359 usb_fill_bulk_urb (urb, dev->udev, dev->in,
360 skb->data, size, rx_complete, skb);
362 spin_lock_irqsave (&dev->rxq.lock, lockflags);
364 if (netif_running (dev->net) &&
365 netif_device_present (dev->net) &&
366 !test_bit (EVENT_RX_HALT, &dev->flags) &&
367 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
368 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
370 usbnet_defer_kevent (dev, EVENT_RX_HALT);
373 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
376 netif_dbg(dev, ifdown, dev->net, "device gone\n");
377 netif_device_detach (dev->net);
383 netif_dbg(dev, rx_err, dev->net,
384 "rx submit, %d\n", retval);
385 tasklet_schedule (&dev->bh);
388 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
391 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
394 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
396 dev_kfree_skb_any (skb);
403 /*-------------------------------------------------------------------------*/
405 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
407 if (dev->driver_info->rx_fixup &&
408 !dev->driver_info->rx_fixup (dev, skb)) {
409 /* With RX_ASSEMBLE, rx_fixup() must update counters */
410 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
411 dev->net->stats.rx_errors++;
414 // else network stack removes extra byte if we forced a short packet
417 /* all data was already cloned from skb inside the driver */
418 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
419 dev_kfree_skb_any(skb);
421 usbnet_skb_return(dev, skb);
425 netif_dbg(dev, rx_err, dev->net, "drop\n");
426 dev->net->stats.rx_errors++;
428 skb_queue_tail(&dev->done, skb);
431 /*-------------------------------------------------------------------------*/
433 static void rx_complete (struct urb *urb)
435 struct sk_buff *skb = (struct sk_buff *) urb->context;
436 struct skb_data *entry = (struct skb_data *) skb->cb;
437 struct usbnet *dev = entry->dev;
438 int urb_status = urb->status;
439 enum skb_state state;
441 skb_put (skb, urb->actual_length);
445 switch (urb_status) {
448 if (skb->len < dev->net->hard_header_len) {
450 dev->net->stats.rx_errors++;
451 dev->net->stats.rx_length_errors++;
452 netif_dbg(dev, rx_err, dev->net,
453 "rx length %d\n", skb->len);
457 /* stalls need manual reset. this is rare ... except that
458 * when going through USB 2.0 TTs, unplug appears this way.
459 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
460 * storm, recovering as needed.
463 dev->net->stats.rx_errors++;
464 usbnet_defer_kevent (dev, EVENT_RX_HALT);
467 /* software-driven interface shutdown */
468 case -ECONNRESET: /* async unlink */
469 case -ESHUTDOWN: /* hardware gone */
470 netif_dbg(dev, ifdown, dev->net,
471 "rx shutdown, code %d\n", urb_status);
474 /* we get controller i/o faults during khubd disconnect() delays.
475 * throttle down resubmits, to avoid log floods; just temporarily,
476 * so we still recover when the fault isn't a khubd delay.
481 dev->net->stats.rx_errors++;
482 if (!timer_pending (&dev->delay)) {
483 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
484 netif_dbg(dev, link, dev->net,
485 "rx throttle %d\n", urb_status);
493 /* data overrun ... flush fifo? */
495 dev->net->stats.rx_over_errors++;
500 dev->net->stats.rx_errors++;
501 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
505 state = defer_bh(dev, skb, &dev->rxq, state);
508 if (netif_running (dev->net) &&
509 !test_bit (EVENT_RX_HALT, &dev->flags) &&
510 state != unlink_start) {
511 rx_submit (dev, urb, GFP_ATOMIC);
516 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
519 static void intr_complete (struct urb *urb)
521 struct usbnet *dev = urb->context;
522 int status = urb->status;
527 dev->driver_info->status(dev, urb);
530 /* software-driven interface shutdown */
531 case -ENOENT: /* urb killed */
532 case -ESHUTDOWN: /* hardware gone */
533 netif_dbg(dev, ifdown, dev->net,
534 "intr shutdown, code %d\n", status);
537 /* NOTE: not throttling like RX/TX, since this endpoint
538 * already polls infrequently
541 netdev_dbg(dev->net, "intr status %d\n", status);
545 if (!netif_running (dev->net))
548 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
549 status = usb_submit_urb (urb, GFP_ATOMIC);
551 netif_err(dev, timer, dev->net,
552 "intr resubmit --> %d\n", status);
555 /*-------------------------------------------------------------------------*/
556 void usbnet_pause_rx(struct usbnet *dev)
558 set_bit(EVENT_RX_PAUSED, &dev->flags);
560 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
562 EXPORT_SYMBOL_GPL(usbnet_pause_rx);
564 void usbnet_resume_rx(struct usbnet *dev)
569 clear_bit(EVENT_RX_PAUSED, &dev->flags);
571 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
572 usbnet_skb_return(dev, skb);
576 tasklet_schedule(&dev->bh);
578 netif_dbg(dev, rx_status, dev->net,
579 "paused rx queue disabled, %d skbs requeued\n", num);
581 EXPORT_SYMBOL_GPL(usbnet_resume_rx);
583 void usbnet_purge_paused_rxq(struct usbnet *dev)
585 skb_queue_purge(&dev->rxq_pause);
587 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
589 /*-------------------------------------------------------------------------*/
591 // unlink pending rx/tx; completion handlers do all other cleanup
593 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
599 spin_lock_irqsave (&q->lock, flags);
600 while (!skb_queue_empty(q)) {
601 struct skb_data *entry;
605 skb_queue_walk(q, skb) {
606 entry = (struct skb_data *) skb->cb;
607 if (entry->state != unlink_start)
612 entry->state = unlink_start;
616 * Get reference count of the URB to avoid it to be
617 * freed during usb_unlink_urb, which may trigger
618 * use-after-free problem inside usb_unlink_urb since
619 * usb_unlink_urb is always racing with .complete
620 * handler(include defer_bh).
623 spin_unlock_irqrestore(&q->lock, flags);
624 // during some PM-driven resume scenarios,
625 // these (async) unlinks complete immediately
626 retval = usb_unlink_urb (urb);
627 if (retval != -EINPROGRESS && retval != 0)
628 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
632 spin_lock_irqsave(&q->lock, flags);
634 spin_unlock_irqrestore (&q->lock, flags);
638 // Flush all pending rx urbs
639 // minidrivers may need to do this when the MTU changes
641 void usbnet_unlink_rx_urbs(struct usbnet *dev)
643 if (netif_running(dev->net)) {
644 (void) unlink_urbs (dev, &dev->rxq);
645 tasklet_schedule(&dev->bh);
648 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
650 /*-------------------------------------------------------------------------*/
652 // precondition: never called in_interrupt
653 static void usbnet_terminate_urbs(struct usbnet *dev)
655 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
656 DECLARE_WAITQUEUE(wait, current);
659 /* ensure there are no more active urbs */
660 add_wait_queue(&unlink_wakeup, &wait);
661 set_current_state(TASK_UNINTERRUPTIBLE);
662 dev->wait = &unlink_wakeup;
663 temp = unlink_urbs(dev, &dev->txq) +
664 unlink_urbs(dev, &dev->rxq);
666 /* maybe wait for deletions to finish. */
667 while (!skb_queue_empty(&dev->rxq)
668 && !skb_queue_empty(&dev->txq)
669 && !skb_queue_empty(&dev->done)) {
670 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
671 set_current_state(TASK_UNINTERRUPTIBLE);
672 netif_dbg(dev, ifdown, dev->net,
673 "waited for %d urb completions\n", temp);
675 set_current_state(TASK_RUNNING);
677 remove_wait_queue(&unlink_wakeup, &wait);
680 int usbnet_stop (struct net_device *net)
682 struct usbnet *dev = netdev_priv(net);
683 struct driver_info *info = dev->driver_info;
686 clear_bit(EVENT_DEV_OPEN, &dev->flags);
687 netif_stop_queue (net);
689 netif_info(dev, ifdown, dev->net,
690 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
691 net->stats.rx_packets, net->stats.tx_packets,
692 net->stats.rx_errors, net->stats.tx_errors);
694 /* allow minidriver to stop correctly (wireless devices to turn off
697 retval = info->stop(dev);
699 netif_info(dev, ifdown, dev->net,
700 "stop fail (%d) usbnet usb-%s-%s, %s\n",
702 dev->udev->bus->bus_name, dev->udev->devpath,
706 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
707 usbnet_terminate_urbs(dev);
709 usb_kill_urb(dev->interrupt);
711 usbnet_purge_paused_rxq(dev);
713 /* deferred work (task, timer, softirq) must also stop.
714 * can't flush_scheduled_work() until we drop rtnl (later),
715 * else workers could deadlock; so make workers a NOP.
718 del_timer_sync (&dev->delay);
719 tasklet_kill (&dev->bh);
720 if (info->manage_power)
721 info->manage_power(dev, 0);
723 usb_autopm_put_interface(dev->intf);
727 EXPORT_SYMBOL_GPL(usbnet_stop);
729 /*-------------------------------------------------------------------------*/
731 // posts reads, and enables write queuing
733 // precondition: never called in_interrupt
735 int usbnet_open (struct net_device *net)
737 struct usbnet *dev = netdev_priv(net);
739 struct driver_info *info = dev->driver_info;
741 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
742 netif_info(dev, ifup, dev->net,
743 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
745 dev->udev->bus->bus_name,
751 // put into "known safe" state
752 if (info->reset && (retval = info->reset (dev)) < 0) {
753 netif_info(dev, ifup, dev->net,
754 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
756 dev->udev->bus->bus_name,
762 // insist peer be connected
763 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
764 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
768 /* start any status interrupt transfer */
769 if (dev->interrupt) {
770 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
772 netif_err(dev, ifup, dev->net,
773 "intr submit %d\n", retval);
778 set_bit(EVENT_DEV_OPEN, &dev->flags);
779 netif_start_queue (net);
780 netif_info(dev, ifup, dev->net,
781 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
782 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
784 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
785 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
786 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
787 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
788 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
791 // delay posting reads until we're fully open
792 tasklet_schedule (&dev->bh);
793 if (info->manage_power) {
794 retval = info->manage_power(dev, 1);
797 usb_autopm_put_interface(dev->intf);
802 usb_autopm_put_interface(dev->intf);
806 EXPORT_SYMBOL_GPL(usbnet_open);
808 /*-------------------------------------------------------------------------*/
810 /* ethtool methods; minidrivers may need to add some more, but
811 * they'll probably want to use this base set.
814 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
816 struct usbnet *dev = netdev_priv(net);
818 if (!dev->mii.mdio_read)
821 return mii_ethtool_gset(&dev->mii, cmd);
823 EXPORT_SYMBOL_GPL(usbnet_get_settings);
825 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
827 struct usbnet *dev = netdev_priv(net);
830 if (!dev->mii.mdio_write)
833 retval = mii_ethtool_sset(&dev->mii, cmd);
835 /* link speed/duplex might have changed */
836 if (dev->driver_info->link_reset)
837 dev->driver_info->link_reset(dev);
842 EXPORT_SYMBOL_GPL(usbnet_set_settings);
844 u32 usbnet_get_link (struct net_device *net)
846 struct usbnet *dev = netdev_priv(net);
848 /* If a check_connect is defined, return its result */
849 if (dev->driver_info->check_connect)
850 return dev->driver_info->check_connect (dev) == 0;
852 /* if the device has mii operations, use those */
853 if (dev->mii.mdio_read)
854 return mii_link_ok(&dev->mii);
856 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
857 return ethtool_op_get_link(net);
859 EXPORT_SYMBOL_GPL(usbnet_get_link);
861 int usbnet_nway_reset(struct net_device *net)
863 struct usbnet *dev = netdev_priv(net);
865 if (!dev->mii.mdio_write)
868 return mii_nway_restart(&dev->mii);
870 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
872 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
874 struct usbnet *dev = netdev_priv(net);
876 strncpy (info->driver, dev->driver_name, sizeof info->driver);
877 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
878 strncpy (info->fw_version, dev->driver_info->description,
879 sizeof info->fw_version);
880 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
882 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
884 u32 usbnet_get_msglevel (struct net_device *net)
886 struct usbnet *dev = netdev_priv(net);
888 return dev->msg_enable;
890 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
892 void usbnet_set_msglevel (struct net_device *net, u32 level)
894 struct usbnet *dev = netdev_priv(net);
896 dev->msg_enable = level;
898 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
900 /* drivers may override default ethtool_ops in their bind() routine */
901 static const struct ethtool_ops usbnet_ethtool_ops = {
902 .get_settings = usbnet_get_settings,
903 .set_settings = usbnet_set_settings,
904 .get_link = usbnet_get_link,
905 .nway_reset = usbnet_nway_reset,
906 .get_drvinfo = usbnet_get_drvinfo,
907 .get_msglevel = usbnet_get_msglevel,
908 .set_msglevel = usbnet_set_msglevel,
911 /*-------------------------------------------------------------------------*/
913 /* work that cannot be done in interrupt context uses keventd.
915 * NOTE: with 2.5 we could do more of this using completion callbacks,
916 * especially now that control transfers can be queued.
919 kevent (struct work_struct *work)
922 container_of(work, struct usbnet, kevent);
925 /* usb_clear_halt() needs a thread context */
926 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
927 unlink_urbs (dev, &dev->txq);
928 status = usb_autopm_get_interface(dev->intf);
931 status = usb_clear_halt (dev->udev, dev->out);
932 usb_autopm_put_interface(dev->intf);
935 status != -ESHUTDOWN) {
936 if (netif_msg_tx_err (dev))
938 netdev_err(dev->net, "can't clear tx halt, status %d\n",
941 clear_bit (EVENT_TX_HALT, &dev->flags);
942 if (status != -ESHUTDOWN)
943 netif_wake_queue (dev->net);
946 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
947 unlink_urbs (dev, &dev->rxq);
948 status = usb_autopm_get_interface(dev->intf);
951 status = usb_clear_halt (dev->udev, dev->in);
952 usb_autopm_put_interface(dev->intf);
955 status != -ESHUTDOWN) {
956 if (netif_msg_rx_err (dev))
958 netdev_err(dev->net, "can't clear rx halt, status %d\n",
961 clear_bit (EVENT_RX_HALT, &dev->flags);
962 tasklet_schedule (&dev->bh);
966 /* tasklet could resubmit itself forever if memory is tight */
967 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
968 struct urb *urb = NULL;
971 if (netif_running (dev->net))
972 urb = usb_alloc_urb (0, GFP_KERNEL);
974 clear_bit (EVENT_RX_MEMORY, &dev->flags);
976 clear_bit (EVENT_RX_MEMORY, &dev->flags);
977 status = usb_autopm_get_interface(dev->intf);
982 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
984 usb_autopm_put_interface(dev->intf);
987 tasklet_schedule (&dev->bh);
991 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
992 struct driver_info *info = dev->driver_info;
995 clear_bit (EVENT_LINK_RESET, &dev->flags);
996 status = usb_autopm_get_interface(dev->intf);
999 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1000 usb_autopm_put_interface(dev->intf);
1002 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1004 dev->udev->bus->bus_name,
1008 usb_autopm_put_interface(dev->intf);
1013 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1016 /*-------------------------------------------------------------------------*/
1018 static void tx_complete (struct urb *urb)
1020 struct sk_buff *skb = (struct sk_buff *) urb->context;
1021 struct skb_data *entry = (struct skb_data *) skb->cb;
1022 struct usbnet *dev = entry->dev;
1024 if (urb->status == 0) {
1025 if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
1026 dev->net->stats.tx_packets++;
1027 dev->net->stats.tx_bytes += entry->length;
1029 dev->net->stats.tx_errors++;
1031 switch (urb->status) {
1033 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1036 /* software-driven interface shutdown */
1037 case -ECONNRESET: // async unlink
1038 case -ESHUTDOWN: // hardware gone
1041 // like rx, tx gets controller i/o faults during khubd delays
1042 // and so it uses the same throttling mechanism.
1046 usb_mark_last_busy(dev->udev);
1047 if (!timer_pending (&dev->delay)) {
1048 mod_timer (&dev->delay,
1049 jiffies + THROTTLE_JIFFIES);
1050 netif_dbg(dev, link, dev->net,
1051 "tx throttle %d\n", urb->status);
1053 netif_stop_queue (dev->net);
1056 netif_dbg(dev, tx_err, dev->net,
1057 "tx err %d\n", entry->urb->status);
1062 usb_autopm_put_interface_async(dev->intf);
1063 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1066 /*-------------------------------------------------------------------------*/
1068 void usbnet_tx_timeout (struct net_device *net)
1070 struct usbnet *dev = netdev_priv(net);
1072 unlink_urbs (dev, &dev->txq);
1073 tasklet_schedule (&dev->bh);
1075 // FIXME: device recovery -- reset?
1077 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1079 /*-------------------------------------------------------------------------*/
1081 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1082 struct net_device *net)
1084 struct usbnet *dev = netdev_priv(net);
1086 struct urb *urb = NULL;
1087 struct skb_data *entry;
1088 struct driver_info *info = dev->driver_info;
1089 unsigned long flags;
1092 // some devices want funky USB-level framing, for
1093 // win32 driver (usually) and/or hardware quirks
1094 if (info->tx_fixup) {
1095 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1097 if (netif_msg_tx_err(dev)) {
1098 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1101 /* cdc_ncm collected packet; waits for more */
1108 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1109 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1113 entry = (struct skb_data *) skb->cb;
1116 entry->length = length;
1118 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1119 skb->data, skb->len, tx_complete, skb);
1121 /* don't assume the hardware handles USB_ZERO_PACKET
1122 * NOTE: strictly conforming cdc-ether devices should expect
1123 * the ZLP here, but ignore the one-byte packet.
1124 * NOTE2: CDC NCM specification is different from CDC ECM when
1125 * handling ZLP/short packets, so cdc_ncm driver will make short
1126 * packet itself if needed.
1128 if (length % dev->maxpacket == 0) {
1129 if (!(info->flags & FLAG_SEND_ZLP)) {
1130 if (!(info->flags & FLAG_MULTI_PACKET)) {
1131 urb->transfer_buffer_length++;
1132 if (skb_tailroom(skb)) {
1133 skb->data[skb->len] = 0;
1138 urb->transfer_flags |= URB_ZERO_PACKET;
1141 spin_lock_irqsave(&dev->txq.lock, flags);
1142 retval = usb_autopm_get_interface_async(dev->intf);
1144 spin_unlock_irqrestore(&dev->txq.lock, flags);
1149 /* if this triggers the device is still a sleep */
1150 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1151 /* transmission will be done in resume */
1152 usb_anchor_urb(urb, &dev->deferred);
1153 /* no use to process more packets */
1154 netif_stop_queue(net);
1155 spin_unlock_irqrestore(&dev->txq.lock, flags);
1156 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1161 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1163 netif_stop_queue (net);
1164 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1165 usb_autopm_put_interface_async(dev->intf);
1168 usb_autopm_put_interface_async(dev->intf);
1169 netif_dbg(dev, tx_err, dev->net,
1170 "tx: submit urb err %d\n", retval);
1173 net->trans_start = jiffies;
1174 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1175 if (dev->txq.qlen >= TX_QLEN (dev))
1176 netif_stop_queue (net);
1178 spin_unlock_irqrestore (&dev->txq.lock, flags);
1181 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1183 dev->net->stats.tx_dropped++;
1186 dev_kfree_skb_any (skb);
1189 netif_dbg(dev, tx_queued, dev->net,
1190 "> tx, len %d, type 0x%x\n", length, skb->protocol);
1194 return NETDEV_TX_OK;
1196 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1198 /*-------------------------------------------------------------------------*/
1200 // tasklet (work deferred from completions, in_irq) or timer
1202 static void usbnet_bh (unsigned long param)
1204 struct usbnet *dev = (struct usbnet *) param;
1205 struct sk_buff *skb;
1206 struct skb_data *entry;
1208 while ((skb = skb_dequeue (&dev->done))) {
1209 entry = (struct skb_data *) skb->cb;
1210 switch (entry->state) {
1212 entry->state = rx_cleanup;
1213 rx_process (dev, skb);
1217 usb_free_urb (entry->urb);
1218 dev_kfree_skb (skb);
1221 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1225 // waiting for all pending urbs to complete?
1227 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1228 wake_up (dev->wait);
1231 // or are we maybe short a few urbs?
1232 } else if (netif_running (dev->net) &&
1233 netif_device_present (dev->net) &&
1234 !timer_pending (&dev->delay) &&
1235 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1236 int temp = dev->rxq.qlen;
1237 int qlen = RX_QLEN (dev);
1243 // don't refill the queue all at once
1244 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1245 urb = usb_alloc_urb (0, GFP_ATOMIC);
1247 if (rx_submit (dev, urb, GFP_ATOMIC) ==
1252 if (temp != dev->rxq.qlen)
1253 netif_dbg(dev, link, dev->net,
1254 "rxqlen %d --> %d\n",
1255 temp, dev->rxq.qlen);
1256 if (dev->rxq.qlen < qlen)
1257 tasklet_schedule (&dev->bh);
1259 if (dev->txq.qlen < TX_QLEN (dev))
1260 netif_wake_queue (dev->net);
1265 /*-------------------------------------------------------------------------
1267 * USB Device Driver support
1269 *-------------------------------------------------------------------------*/
1271 // precondition: never called in_interrupt
1273 void usbnet_disconnect (struct usb_interface *intf)
1276 struct usb_device *xdev;
1277 struct net_device *net;
1279 dev = usb_get_intfdata(intf);
1280 usb_set_intfdata(intf, NULL);
1284 xdev = interface_to_usbdev (intf);
1286 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1287 intf->dev.driver->name,
1288 xdev->bus->bus_name, xdev->devpath,
1289 dev->driver_info->description);
1292 unregister_netdev (net);
1294 cancel_work_sync(&dev->kevent);
1296 if (dev->driver_info->unbind)
1297 dev->driver_info->unbind (dev, intf);
1299 usb_kill_urb(dev->interrupt);
1300 usb_free_urb(dev->interrupt);
1305 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1307 static const struct net_device_ops usbnet_netdev_ops = {
1308 .ndo_open = usbnet_open,
1309 .ndo_stop = usbnet_stop,
1310 .ndo_start_xmit = usbnet_start_xmit,
1311 .ndo_tx_timeout = usbnet_tx_timeout,
1312 .ndo_change_mtu = usbnet_change_mtu,
1313 .ndo_set_mac_address = eth_mac_addr,
1314 .ndo_validate_addr = eth_validate_addr,
1317 /*-------------------------------------------------------------------------*/
1319 // precondition: never called in_interrupt
1321 static struct device_type wlan_type = {
1325 static struct device_type wwan_type = {
1330 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1333 struct net_device *net;
1334 struct usb_host_interface *interface;
1335 struct driver_info *info;
1336 struct usb_device *xdev;
1339 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1341 /* usbnet already took usb runtime pm, so have to enable the feature
1342 * for usb interface, otherwise usb_autopm_get_interface may return
1343 * failure if USB_SUSPEND(RUNTIME_PM) is enabled.
1345 if (!driver->supports_autosuspend) {
1346 driver->supports_autosuspend = 1;
1347 pm_runtime_enable(&udev->dev);
1350 name = udev->dev.driver->name;
1351 info = (struct driver_info *) prod->driver_info;
1353 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1356 xdev = interface_to_usbdev (udev);
1357 interface = udev->cur_altsetting;
1363 // set up our own records
1364 net = alloc_etherdev(sizeof(*dev));
1366 dbg ("can't kmalloc dev");
1370 /* netdev_printk() needs this so do it as early as possible */
1371 SET_NETDEV_DEV(net, &udev->dev);
1373 dev = netdev_priv(net);
1376 dev->driver_info = info;
1377 dev->driver_name = name;
1378 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1379 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1380 skb_queue_head_init (&dev->rxq);
1381 skb_queue_head_init (&dev->txq);
1382 skb_queue_head_init (&dev->done);
1383 skb_queue_head_init(&dev->rxq_pause);
1384 dev->bh.func = usbnet_bh;
1385 dev->bh.data = (unsigned long) dev;
1386 INIT_WORK (&dev->kevent, kevent);
1387 init_usb_anchor(&dev->deferred);
1388 dev->delay.function = usbnet_bh;
1389 dev->delay.data = (unsigned long) dev;
1390 init_timer (&dev->delay);
1391 mutex_init (&dev->phy_mutex);
1394 strcpy (net->name, "usb%d");
1395 memcpy (net->dev_addr, node_id, sizeof node_id);
1397 /* rx and tx sides can use different message sizes;
1398 * bind() should set rx_urb_size in that case.
1400 dev->hard_mtu = net->mtu + net->hard_header_len;
1402 // dma_supported() is deeply broken on almost all architectures
1403 // possible with some EHCI controllers
1404 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
1405 net->features |= NETIF_F_HIGHDMA;
1408 net->netdev_ops = &usbnet_netdev_ops;
1409 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1410 net->ethtool_ops = &usbnet_ethtool_ops;
1412 info->flags |= FLAG_AVOID_UNLINK_URBS;
1414 // allow device-specific bind/init procedures
1415 // NOTE net->name still not usable ...
1417 status = info->bind (dev, udev);
1421 // heuristic: "usb%d" for links we know are two-host,
1422 // else "eth%d" when there's reasonable doubt. userspace
1423 // can rename the link if it knows better.
1424 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1425 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1426 (net->dev_addr [0] & 0x02) == 0))
1427 strcpy (net->name, "eth%d");
1428 /* WLAN devices should always be named "wlan%d" */
1429 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1430 strcpy(net->name, "wlan%d");
1431 /* WWAN devices should always be named "wwan%d" */
1432 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1433 strcpy(net->name, "wwan%d");
1435 /* maybe the remote can't receive an Ethernet MTU */
1436 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1437 net->mtu = dev->hard_mtu - net->hard_header_len;
1438 } else if (!info->in || !info->out)
1439 status = usbnet_get_endpoints (dev, udev);
1441 dev->in = usb_rcvbulkpipe (xdev, info->in);
1442 dev->out = usb_sndbulkpipe (xdev, info->out);
1443 if (!(info->flags & FLAG_NO_SETINT))
1444 status = usb_set_interface (xdev,
1445 interface->desc.bInterfaceNumber,
1446 interface->desc.bAlternateSetting);
1451 if (status >= 0 && dev->status)
1452 status = init_status (dev, udev);
1456 if (!dev->rx_urb_size)
1457 dev->rx_urb_size = dev->hard_mtu;
1458 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1460 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1461 SET_NETDEV_DEVTYPE(net, &wlan_type);
1462 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1463 SET_NETDEV_DEVTYPE(net, &wwan_type);
1465 status = register_netdev (net);
1468 netif_info(dev, probe, dev->net,
1469 "register '%s' at usb-%s-%s, %s, %pM\n",
1470 udev->dev.driver->name,
1471 xdev->bus->bus_name, xdev->devpath,
1472 dev->driver_info->description,
1475 // ok, it's ready to go.
1476 usb_set_intfdata (udev, dev);
1478 netif_device_attach (net);
1480 if (dev->driver_info->flags & FLAG_LINK_INTR)
1481 netif_carrier_off(net);
1487 info->unbind (dev, udev);
1494 EXPORT_SYMBOL_GPL(usbnet_probe);
1496 /*-------------------------------------------------------------------------*/
1499 * suspend the whole driver as soon as the first interface is suspended
1500 * resume only when the last interface is resumed
1503 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1505 struct usbnet *dev = usb_get_intfdata(intf);
1507 if (!dev->suspend_count++) {
1508 spin_lock_irq(&dev->txq.lock);
1509 /* don't autosuspend while transmitting */
1510 if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) {
1511 spin_unlock_irq(&dev->txq.lock);
1514 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1515 spin_unlock_irq(&dev->txq.lock);
1518 * accelerate emptying of the rx and queues, to avoid
1519 * having everything error out.
1521 netif_device_detach (dev->net);
1522 usbnet_terminate_urbs(dev);
1523 usb_kill_urb(dev->interrupt);
1526 * reattach so runtime management can use and
1529 netif_device_attach (dev->net);
1533 EXPORT_SYMBOL_GPL(usbnet_suspend);
1535 int usbnet_resume (struct usb_interface *intf)
1537 struct usbnet *dev = usb_get_intfdata(intf);
1538 struct sk_buff *skb;
1542 if (!--dev->suspend_count) {
1543 /* resume interrupt URBs */
1544 if (dev->interrupt && test_bit(EVENT_DEV_OPEN, &dev->flags))
1545 usb_submit_urb(dev->interrupt, GFP_NOIO);
1547 spin_lock_irq(&dev->txq.lock);
1548 while ((res = usb_get_from_anchor(&dev->deferred))) {
1550 skb = (struct sk_buff *)res->context;
1551 retval = usb_submit_urb(res, GFP_ATOMIC);
1553 dev_kfree_skb_any(skb);
1555 usb_autopm_put_interface_async(dev->intf);
1557 dev->net->trans_start = jiffies;
1558 __skb_queue_tail(&dev->txq, skb);
1563 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1564 spin_unlock_irq(&dev->txq.lock);
1566 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1567 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1568 netif_start_queue(dev->net);
1569 tasklet_schedule (&dev->bh);
1574 EXPORT_SYMBOL_GPL(usbnet_resume);
1577 /*-------------------------------------------------------------------------*/
1579 static int __init usbnet_init(void)
1581 /* Compiler should optimize this out. */
1583 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
1585 random_ether_addr(node_id);
1588 module_init(usbnet_init);
1590 static void __exit usbnet_exit(void)
1593 module_exit(usbnet_exit);
1595 MODULE_AUTHOR("David Brownell");
1596 MODULE_DESCRIPTION("USB network driver framework");
1597 MODULE_LICENSE("GPL");