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, see <http://www.gnu.org/licenses/>.
21 * This is a generic "USB networking" framework that works with several
22 * kinds of full and high speed networking devices: host-to-host cables,
23 * smart usb peripherals, and actual Ethernet adapters.
25 * These devices usually differ in terms of control protocols (if they
26 * even have one!) and sometimes they define new framing to wrap or batch
27 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
28 * so interface (un)binding, endpoint I/O queues, fault handling, and other
29 * issues can usefully be addressed by this framework.
32 // #define DEBUG // error path messages, extra info
33 // #define VERBOSE // more; success messages
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/netdevice.h>
38 #include <linux/etherdevice.h>
39 #include <linux/ctype.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>
45 #include <linux/usb/cdc.h>
46 #include <linux/slab.h>
47 #include <linux/kernel.h>
48 #include <linux/pm_runtime.h>
50 #define DRIVER_VERSION "22-Aug-2005"
53 /*-------------------------------------------------------------------------*/
56 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
57 * Several dozen bytes of IPv4 data can fit in two such transactions.
58 * One maximum size Ethernet packet takes twenty four of them.
59 * For high speed, each frame comfortably fits almost 36 max size
60 * Ethernet packets (so queues should be bigger).
62 * The goal is to let the USB host controller be busy for 5msec or
63 * more before an irq is required, under load. Jumbograms change
66 #define MAX_QUEUE_MEMORY (60 * 1518)
67 #define RX_QLEN(dev) ((dev)->rx_qlen)
68 #define TX_QLEN(dev) ((dev)->tx_qlen)
70 // reawaken network queue this soon after stopping; else watchdog barks
71 #define TX_TIMEOUT_JIFFIES (5*HZ)
73 /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
74 * us (it polls at HZ/4 usually) before we report too many false errors.
76 #define THROTTLE_JIFFIES (HZ/8)
79 #define UNLINK_TIMEOUT_MS 3
81 /*-------------------------------------------------------------------------*/
83 // randomly generated ethernet address
84 static u8 node_id [ETH_ALEN];
86 static const char driver_name [] = "usbnet";
88 /* use ethtool to change the level for any given device */
89 static int msg_level = -1;
90 module_param (msg_level, int, 0);
91 MODULE_PARM_DESC (msg_level, "Override default message level");
93 /*-------------------------------------------------------------------------*/
95 /* handles CDC Ethernet and many other network "bulk data" interfaces */
96 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
99 struct usb_host_interface *alt = NULL;
100 struct usb_host_endpoint *in = NULL, *out = NULL;
101 struct usb_host_endpoint *status = NULL;
103 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
106 in = out = status = NULL;
107 alt = intf->altsetting + tmp;
109 /* take the first altsetting with in-bulk + out-bulk;
110 * remember any status endpoint, just in case;
111 * ignore other endpoints and altsettings.
113 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
114 struct usb_host_endpoint *e;
117 e = alt->endpoint + ep;
118 switch (e->desc.bmAttributes) {
119 case USB_ENDPOINT_XFER_INT:
120 if (!usb_endpoint_dir_in(&e->desc))
124 case USB_ENDPOINT_XFER_BULK:
129 if (usb_endpoint_dir_in(&e->desc)) {
132 else if (intr && !status)
142 if (!alt || !in || !out)
145 if (alt->desc.bAlternateSetting != 0 ||
146 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
147 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
148 alt->desc.bAlternateSetting);
153 dev->in = usb_rcvbulkpipe (dev->udev,
154 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155 dev->out = usb_sndbulkpipe (dev->udev,
156 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
157 dev->status = status;
160 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
162 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
165 unsigned char buf [13];
167 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
169 tmp = hex2bin(dev->net->dev_addr, buf, 6);
171 dev_dbg(&dev->udev->dev,
172 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
179 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
181 static void intr_complete (struct urb *urb)
183 struct usbnet *dev = urb->context;
184 int status = urb->status;
189 dev->driver_info->status(dev, urb);
192 /* software-driven interface shutdown */
193 case -ENOENT: /* urb killed */
194 case -ESHUTDOWN: /* hardware gone */
195 netif_dbg(dev, ifdown, dev->net,
196 "intr shutdown, code %d\n", status);
199 /* NOTE: not throttling like RX/TX, since this endpoint
200 * already polls infrequently
203 netdev_dbg(dev->net, "intr status %d\n", status);
207 status = usb_submit_urb (urb, GFP_ATOMIC);
209 netif_err(dev, timer, dev->net,
210 "intr resubmit --> %d\n", status);
213 static int init_status (struct usbnet *dev, struct usb_interface *intf)
220 if (!dev->driver_info->status)
223 pipe = usb_rcvintpipe (dev->udev,
224 dev->status->desc.bEndpointAddress
225 & USB_ENDPOINT_NUMBER_MASK);
226 maxp = usb_maxpacket (dev->udev, pipe, 0);
228 /* avoid 1 msec chatter: min 8 msec poll rate */
229 period = max ((int) dev->status->desc.bInterval,
230 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
232 buf = kmalloc (maxp, GFP_KERNEL);
234 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
235 if (!dev->interrupt) {
239 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
240 buf, maxp, intr_complete, dev, period);
241 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
243 "status ep%din, %d bytes period %d\n",
244 usb_pipeendpoint(pipe), maxp, period);
250 /* Submit the interrupt URB if not previously submitted, increasing refcount */
251 int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
255 WARN_ON_ONCE(dev->interrupt == NULL);
256 if (dev->interrupt) {
257 mutex_lock(&dev->interrupt_mutex);
259 if (++dev->interrupt_count == 1)
260 ret = usb_submit_urb(dev->interrupt, mem_flags);
262 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
263 dev->interrupt_count);
264 mutex_unlock(&dev->interrupt_mutex);
268 EXPORT_SYMBOL_GPL(usbnet_status_start);
270 /* For resume; submit interrupt URB if previously submitted */
271 static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
275 mutex_lock(&dev->interrupt_mutex);
276 if (dev->interrupt_count) {
277 ret = usb_submit_urb(dev->interrupt, mem_flags);
278 dev_dbg(&dev->udev->dev,
279 "submitted interrupt URB for resume\n");
281 mutex_unlock(&dev->interrupt_mutex);
285 /* Kill the interrupt URB if all submitters want it killed */
286 void usbnet_status_stop(struct usbnet *dev)
288 if (dev->interrupt) {
289 mutex_lock(&dev->interrupt_mutex);
290 WARN_ON(dev->interrupt_count == 0);
292 if (dev->interrupt_count && --dev->interrupt_count == 0)
293 usb_kill_urb(dev->interrupt);
295 dev_dbg(&dev->udev->dev,
296 "decremented interrupt URB count to %d\n",
297 dev->interrupt_count);
298 mutex_unlock(&dev->interrupt_mutex);
301 EXPORT_SYMBOL_GPL(usbnet_status_stop);
303 /* For suspend; always kill interrupt URB */
304 static void __usbnet_status_stop_force(struct usbnet *dev)
306 if (dev->interrupt) {
307 mutex_lock(&dev->interrupt_mutex);
308 usb_kill_urb(dev->interrupt);
309 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
310 mutex_unlock(&dev->interrupt_mutex);
314 /* Passes this packet up the stack, updating its accounting.
315 * Some link protocols batch packets, so their rx_fixup paths
316 * can return clones as well as just modify the original skb.
318 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
322 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
323 skb_queue_tail(&dev->rxq_pause, skb);
327 skb->protocol = eth_type_trans (skb, dev->net);
328 dev->net->stats.rx_packets++;
329 dev->net->stats.rx_bytes += skb->len;
331 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
332 skb->len + sizeof (struct ethhdr), skb->protocol);
333 memset (skb->cb, 0, sizeof (struct skb_data));
335 if (skb_defer_rx_timestamp(skb))
338 status = netif_rx (skb);
339 if (status != NET_RX_SUCCESS)
340 netif_dbg(dev, rx_err, dev->net,
341 "netif_rx status %d\n", status);
343 EXPORT_SYMBOL_GPL(usbnet_skb_return);
345 /* must be called if hard_mtu or rx_urb_size changed */
346 void usbnet_update_max_qlen(struct usbnet *dev)
348 enum usb_device_speed speed = dev->udev->speed;
352 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
353 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
355 case USB_SPEED_SUPER:
357 * Not take default 5ms qlen for super speed HC to
358 * save memory, and iperf tests show 2.5ms qlen can
361 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
362 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
365 dev->rx_qlen = dev->tx_qlen = 4;
368 EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
371 /*-------------------------------------------------------------------------
373 * Network Device Driver (peer link to "Host Device", from USB host)
375 *-------------------------------------------------------------------------*/
377 int usbnet_change_mtu (struct net_device *net, int new_mtu)
379 struct usbnet *dev = netdev_priv(net);
380 int ll_mtu = new_mtu + net->hard_header_len;
381 int old_hard_mtu = dev->hard_mtu;
382 int old_rx_urb_size = dev->rx_urb_size;
386 // no second zero-length packet read wanted after mtu-sized packets
387 if ((ll_mtu % dev->maxpacket) == 0)
391 dev->hard_mtu = net->mtu + net->hard_header_len;
392 if (dev->rx_urb_size == old_hard_mtu) {
393 dev->rx_urb_size = dev->hard_mtu;
394 if (dev->rx_urb_size > old_rx_urb_size)
395 usbnet_unlink_rx_urbs(dev);
398 /* max qlen depend on hard_mtu and rx_urb_size */
399 usbnet_update_max_qlen(dev);
403 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
405 /* The caller must hold list->lock */
406 static void __usbnet_queue_skb(struct sk_buff_head *list,
407 struct sk_buff *newsk, enum skb_state state)
409 struct skb_data *entry = (struct skb_data *) newsk->cb;
411 __skb_queue_tail(list, newsk);
412 entry->state = state;
415 /*-------------------------------------------------------------------------*/
417 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
418 * completion callbacks. 2.5 should have fixed those bugs...
421 static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
422 struct sk_buff_head *list, enum skb_state state)
425 enum skb_state old_state;
426 struct skb_data *entry = (struct skb_data *) skb->cb;
428 spin_lock_irqsave(&list->lock, flags);
429 old_state = entry->state;
430 entry->state = state;
431 __skb_unlink(skb, list);
433 /* defer_bh() is never called with list == &dev->done.
434 * spin_lock_nested() tells lockdep that it is OK to take
435 * dev->done.lock here with list->lock held.
437 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
439 __skb_queue_tail(&dev->done, skb);
440 if (dev->done.qlen == 1)
441 tasklet_schedule(&dev->bh);
442 spin_unlock(&dev->done.lock);
443 spin_unlock_irqrestore(&list->lock, flags);
447 /* some work can't be done in tasklets, so we use keventd
449 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
450 * but tasklet_schedule() doesn't. hope the failure is rare.
452 void usbnet_defer_kevent (struct usbnet *dev, int work)
454 set_bit (work, &dev->flags);
455 if (!schedule_work (&dev->kevent)) {
457 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
459 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
462 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
464 /*-------------------------------------------------------------------------*/
466 static void rx_complete (struct urb *urb);
468 static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
471 struct skb_data *entry;
473 unsigned long lockflags;
474 size_t size = dev->rx_urb_size;
476 /* prevent rx skb allocation when error ratio is high */
477 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
482 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
484 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
485 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
490 entry = (struct skb_data *) skb->cb;
495 usb_fill_bulk_urb (urb, dev->udev, dev->in,
496 skb->data, size, rx_complete, skb);
498 spin_lock_irqsave (&dev->rxq.lock, lockflags);
500 if (netif_running (dev->net) &&
501 netif_device_present (dev->net) &&
502 !test_bit (EVENT_RX_HALT, &dev->flags) &&
503 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
504 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
506 usbnet_defer_kevent (dev, EVENT_RX_HALT);
509 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
512 netif_dbg(dev, ifdown, dev->net, "device gone\n");
513 netif_device_detach (dev->net);
519 netif_dbg(dev, rx_err, dev->net,
520 "rx submit, %d\n", retval);
521 tasklet_schedule (&dev->bh);
524 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
527 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
530 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
532 dev_kfree_skb_any (skb);
539 /*-------------------------------------------------------------------------*/
541 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
543 if (dev->driver_info->rx_fixup &&
544 !dev->driver_info->rx_fixup (dev, skb)) {
545 /* With RX_ASSEMBLE, rx_fixup() must update counters */
546 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
547 dev->net->stats.rx_errors++;
550 // else network stack removes extra byte if we forced a short packet
552 /* all data was already cloned from skb inside the driver */
553 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
556 if (skb->len < ETH_HLEN) {
557 dev->net->stats.rx_errors++;
558 dev->net->stats.rx_length_errors++;
559 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
561 usbnet_skb_return(dev, skb);
566 skb_queue_tail(&dev->done, skb);
569 /*-------------------------------------------------------------------------*/
571 static void rx_complete (struct urb *urb)
573 struct sk_buff *skb = (struct sk_buff *) urb->context;
574 struct skb_data *entry = (struct skb_data *) skb->cb;
575 struct usbnet *dev = entry->dev;
576 int urb_status = urb->status;
577 enum skb_state state;
579 skb_put (skb, urb->actual_length);
583 switch (urb_status) {
588 /* stalls need manual reset. this is rare ... except that
589 * when going through USB 2.0 TTs, unplug appears this way.
590 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
591 * storm, recovering as needed.
594 dev->net->stats.rx_errors++;
595 usbnet_defer_kevent (dev, EVENT_RX_HALT);
598 /* software-driven interface shutdown */
599 case -ECONNRESET: /* async unlink */
600 case -ESHUTDOWN: /* hardware gone */
601 netif_dbg(dev, ifdown, dev->net,
602 "rx shutdown, code %d\n", urb_status);
605 /* we get controller i/o faults during hub_wq disconnect() delays.
606 * throttle down resubmits, to avoid log floods; just temporarily,
607 * so we still recover when the fault isn't a hub_wq delay.
612 dev->net->stats.rx_errors++;
613 if (!timer_pending (&dev->delay)) {
614 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
615 netif_dbg(dev, link, dev->net,
616 "rx throttle %d\n", urb_status);
624 /* data overrun ... flush fifo? */
626 dev->net->stats.rx_over_errors++;
631 dev->net->stats.rx_errors++;
632 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
636 /* stop rx if packet error rate is high */
637 if (++dev->pkt_cnt > 30) {
641 if (state == rx_cleanup)
643 if (dev->pkt_err > 20)
644 set_bit(EVENT_RX_KILL, &dev->flags);
647 state = defer_bh(dev, skb, &dev->rxq, state);
650 if (netif_running (dev->net) &&
651 !test_bit (EVENT_RX_HALT, &dev->flags) &&
652 state != unlink_start) {
653 rx_submit (dev, urb, GFP_ATOMIC);
654 usb_mark_last_busy(dev->udev);
659 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
662 /*-------------------------------------------------------------------------*/
663 void usbnet_pause_rx(struct usbnet *dev)
665 set_bit(EVENT_RX_PAUSED, &dev->flags);
667 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
669 EXPORT_SYMBOL_GPL(usbnet_pause_rx);
671 void usbnet_resume_rx(struct usbnet *dev)
676 clear_bit(EVENT_RX_PAUSED, &dev->flags);
678 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
679 usbnet_skb_return(dev, skb);
683 tasklet_schedule(&dev->bh);
685 netif_dbg(dev, rx_status, dev->net,
686 "paused rx queue disabled, %d skbs requeued\n", num);
688 EXPORT_SYMBOL_GPL(usbnet_resume_rx);
690 void usbnet_purge_paused_rxq(struct usbnet *dev)
692 skb_queue_purge(&dev->rxq_pause);
694 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
696 /*-------------------------------------------------------------------------*/
698 // unlink pending rx/tx; completion handlers do all other cleanup
700 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
706 spin_lock_irqsave (&q->lock, flags);
707 while (!skb_queue_empty(q)) {
708 struct skb_data *entry;
712 skb_queue_walk(q, skb) {
713 entry = (struct skb_data *) skb->cb;
714 if (entry->state != unlink_start)
719 entry->state = unlink_start;
723 * Get reference count of the URB to avoid it to be
724 * freed during usb_unlink_urb, which may trigger
725 * use-after-free problem inside usb_unlink_urb since
726 * usb_unlink_urb is always racing with .complete
727 * handler(include defer_bh).
730 spin_unlock_irqrestore(&q->lock, flags);
731 // during some PM-driven resume scenarios,
732 // these (async) unlinks complete immediately
733 retval = usb_unlink_urb (urb);
734 if (retval != -EINPROGRESS && retval != 0)
735 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
739 spin_lock_irqsave(&q->lock, flags);
741 spin_unlock_irqrestore (&q->lock, flags);
745 // Flush all pending rx urbs
746 // minidrivers may need to do this when the MTU changes
748 void usbnet_unlink_rx_urbs(struct usbnet *dev)
750 if (netif_running(dev->net)) {
751 (void) unlink_urbs (dev, &dev->rxq);
752 tasklet_schedule(&dev->bh);
755 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
757 /*-------------------------------------------------------------------------*/
759 static void wait_skb_queue_empty(struct sk_buff_head *q)
763 spin_lock_irqsave(&q->lock, flags);
764 while (!skb_queue_empty(q)) {
765 spin_unlock_irqrestore(&q->lock, flags);
766 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 spin_lock_irqsave(&q->lock, flags);
770 spin_unlock_irqrestore(&q->lock, flags);
773 // precondition: never called in_interrupt
774 static void usbnet_terminate_urbs(struct usbnet *dev)
776 DECLARE_WAITQUEUE(wait, current);
779 /* ensure there are no more active urbs */
780 add_wait_queue(&dev->wait, &wait);
781 set_current_state(TASK_UNINTERRUPTIBLE);
782 temp = unlink_urbs(dev, &dev->txq) +
783 unlink_urbs(dev, &dev->rxq);
785 /* maybe wait for deletions to finish. */
786 wait_skb_queue_empty(&dev->rxq);
787 wait_skb_queue_empty(&dev->txq);
788 wait_skb_queue_empty(&dev->done);
789 netif_dbg(dev, ifdown, dev->net,
790 "waited for %d urb completions\n", temp);
791 set_current_state(TASK_RUNNING);
792 remove_wait_queue(&dev->wait, &wait);
795 int usbnet_stop (struct net_device *net)
797 struct usbnet *dev = netdev_priv(net);
798 struct driver_info *info = dev->driver_info;
801 clear_bit(EVENT_DEV_OPEN, &dev->flags);
802 netif_stop_queue (net);
804 netif_info(dev, ifdown, dev->net,
805 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
806 net->stats.rx_packets, net->stats.tx_packets,
807 net->stats.rx_errors, net->stats.tx_errors);
809 /* to not race resume */
810 pm = usb_autopm_get_interface(dev->intf);
811 /* allow minidriver to stop correctly (wireless devices to turn off
814 retval = info->stop(dev);
816 netif_info(dev, ifdown, dev->net,
817 "stop fail (%d) usbnet usb-%s-%s, %s\n",
819 dev->udev->bus->bus_name, dev->udev->devpath,
823 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
824 usbnet_terminate_urbs(dev);
826 usbnet_status_stop(dev);
828 usbnet_purge_paused_rxq(dev);
830 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
832 /* deferred work (task, timer, softirq) must also stop.
833 * can't flush_scheduled_work() until we drop rtnl (later),
834 * else workers could deadlock; so make workers a NOP.
837 del_timer_sync (&dev->delay);
838 tasklet_kill (&dev->bh);
840 usb_autopm_put_interface(dev->intf);
842 if (info->manage_power && mpn)
843 info->manage_power(dev, 0);
845 usb_autopm_put_interface(dev->intf);
849 EXPORT_SYMBOL_GPL(usbnet_stop);
851 /*-------------------------------------------------------------------------*/
853 // posts reads, and enables write queuing
855 // precondition: never called in_interrupt
857 int usbnet_open (struct net_device *net)
859 struct usbnet *dev = netdev_priv(net);
861 struct driver_info *info = dev->driver_info;
863 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
864 netif_info(dev, ifup, dev->net,
865 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
867 dev->udev->bus->bus_name,
873 // put into "known safe" state
874 if (info->reset && (retval = info->reset (dev)) < 0) {
875 netif_info(dev, ifup, dev->net,
876 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
878 dev->udev->bus->bus_name,
884 /* hard_mtu or rx_urb_size may change in reset() */
885 usbnet_update_max_qlen(dev);
887 // insist peer be connected
888 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
889 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
893 /* start any status interrupt transfer */
894 if (dev->interrupt) {
895 retval = usbnet_status_start(dev, GFP_KERNEL);
897 netif_err(dev, ifup, dev->net,
898 "intr submit %d\n", retval);
903 set_bit(EVENT_DEV_OPEN, &dev->flags);
904 netif_start_queue (net);
905 netif_info(dev, ifup, dev->net,
906 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
907 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
909 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
910 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
911 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
912 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
913 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
916 /* reset rx error state */
919 clear_bit(EVENT_RX_KILL, &dev->flags);
921 // delay posting reads until we're fully open
922 tasklet_schedule (&dev->bh);
923 if (info->manage_power) {
924 retval = info->manage_power(dev, 1);
927 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
929 usb_autopm_put_interface(dev->intf);
934 usb_autopm_put_interface(dev->intf);
938 EXPORT_SYMBOL_GPL(usbnet_open);
940 /*-------------------------------------------------------------------------*/
942 /* ethtool methods; minidrivers may need to add some more, but
943 * they'll probably want to use this base set.
946 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
948 struct usbnet *dev = netdev_priv(net);
950 if (!dev->mii.mdio_read)
953 return mii_ethtool_gset(&dev->mii, cmd);
955 EXPORT_SYMBOL_GPL(usbnet_get_settings);
957 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
959 struct usbnet *dev = netdev_priv(net);
962 if (!dev->mii.mdio_write)
965 retval = mii_ethtool_sset(&dev->mii, cmd);
967 /* link speed/duplex might have changed */
968 if (dev->driver_info->link_reset)
969 dev->driver_info->link_reset(dev);
971 /* hard_mtu or rx_urb_size may change in link_reset() */
972 usbnet_update_max_qlen(dev);
977 EXPORT_SYMBOL_GPL(usbnet_set_settings);
979 u32 usbnet_get_link (struct net_device *net)
981 struct usbnet *dev = netdev_priv(net);
983 /* If a check_connect is defined, return its result */
984 if (dev->driver_info->check_connect)
985 return dev->driver_info->check_connect (dev) == 0;
987 /* if the device has mii operations, use those */
988 if (dev->mii.mdio_read)
989 return mii_link_ok(&dev->mii);
991 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
992 return ethtool_op_get_link(net);
994 EXPORT_SYMBOL_GPL(usbnet_get_link);
996 int usbnet_nway_reset(struct net_device *net)
998 struct usbnet *dev = netdev_priv(net);
1000 if (!dev->mii.mdio_write)
1003 return mii_nway_restart(&dev->mii);
1005 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1007 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1009 struct usbnet *dev = netdev_priv(net);
1011 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1012 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
1013 strlcpy (info->fw_version, dev->driver_info->description,
1014 sizeof info->fw_version);
1015 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1017 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1019 u32 usbnet_get_msglevel (struct net_device *net)
1021 struct usbnet *dev = netdev_priv(net);
1023 return dev->msg_enable;
1025 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1027 void usbnet_set_msglevel (struct net_device *net, u32 level)
1029 struct usbnet *dev = netdev_priv(net);
1031 dev->msg_enable = level;
1033 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1035 /* drivers may override default ethtool_ops in their bind() routine */
1036 static const struct ethtool_ops usbnet_ethtool_ops = {
1037 .get_settings = usbnet_get_settings,
1038 .set_settings = usbnet_set_settings,
1039 .get_link = usbnet_get_link,
1040 .nway_reset = usbnet_nway_reset,
1041 .get_drvinfo = usbnet_get_drvinfo,
1042 .get_msglevel = usbnet_get_msglevel,
1043 .set_msglevel = usbnet_set_msglevel,
1044 .get_ts_info = ethtool_op_get_ts_info,
1047 /*-------------------------------------------------------------------------*/
1049 static void __handle_link_change(struct usbnet *dev)
1051 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1054 if (!netif_carrier_ok(dev->net)) {
1055 /* kill URBs for reading packets to save bus bandwidth */
1056 unlink_urbs(dev, &dev->rxq);
1059 * tx_timeout will unlink URBs for sending packets and
1060 * tx queue is stopped by netcore after link becomes off
1063 /* submitting URBs for reading packets */
1064 tasklet_schedule(&dev->bh);
1067 /* hard_mtu or rx_urb_size may change during link change */
1068 usbnet_update_max_qlen(dev);
1070 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1073 static void usbnet_set_rx_mode(struct net_device *net)
1075 struct usbnet *dev = netdev_priv(net);
1077 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
1080 static void __handle_set_rx_mode(struct usbnet *dev)
1082 if (dev->driver_info->set_rx_mode)
1083 (dev->driver_info->set_rx_mode)(dev);
1085 clear_bit(EVENT_SET_RX_MODE, &dev->flags);
1088 /* work that cannot be done in interrupt context uses keventd.
1090 * NOTE: with 2.5 we could do more of this using completion callbacks,
1091 * especially now that control transfers can be queued.
1094 usbnet_deferred_kevent (struct work_struct *work)
1096 struct usbnet *dev =
1097 container_of(work, struct usbnet, kevent);
1100 /* usb_clear_halt() needs a thread context */
1101 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1102 unlink_urbs (dev, &dev->txq);
1103 status = usb_autopm_get_interface(dev->intf);
1106 status = usb_clear_halt (dev->udev, dev->out);
1107 usb_autopm_put_interface(dev->intf);
1110 status != -ESHUTDOWN) {
1111 if (netif_msg_tx_err (dev))
1113 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1116 clear_bit (EVENT_TX_HALT, &dev->flags);
1117 if (status != -ESHUTDOWN)
1118 netif_wake_queue (dev->net);
1121 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1122 unlink_urbs (dev, &dev->rxq);
1123 status = usb_autopm_get_interface(dev->intf);
1126 status = usb_clear_halt (dev->udev, dev->in);
1127 usb_autopm_put_interface(dev->intf);
1130 status != -ESHUTDOWN) {
1131 if (netif_msg_rx_err (dev))
1133 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1136 clear_bit (EVENT_RX_HALT, &dev->flags);
1137 tasklet_schedule (&dev->bh);
1141 /* tasklet could resubmit itself forever if memory is tight */
1142 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1143 struct urb *urb = NULL;
1146 if (netif_running (dev->net))
1147 urb = usb_alloc_urb (0, GFP_KERNEL);
1149 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1151 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1152 status = usb_autopm_get_interface(dev->intf);
1157 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1159 usb_autopm_put_interface(dev->intf);
1162 tasklet_schedule (&dev->bh);
1166 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1167 struct driver_info *info = dev->driver_info;
1170 clear_bit (EVENT_LINK_RESET, &dev->flags);
1171 status = usb_autopm_get_interface(dev->intf);
1174 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1175 usb_autopm_put_interface(dev->intf);
1177 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1179 dev->udev->bus->bus_name,
1183 usb_autopm_put_interface(dev->intf);
1186 /* handle link change from link resetting */
1187 __handle_link_change(dev);
1190 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1191 __handle_link_change(dev);
1193 if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
1194 __handle_set_rx_mode(dev);
1198 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1201 /*-------------------------------------------------------------------------*/
1203 static void tx_complete (struct urb *urb)
1205 struct sk_buff *skb = (struct sk_buff *) urb->context;
1206 struct skb_data *entry = (struct skb_data *) skb->cb;
1207 struct usbnet *dev = entry->dev;
1209 if (urb->status == 0) {
1210 dev->net->stats.tx_packets += entry->packets;
1211 dev->net->stats.tx_bytes += entry->length;
1213 dev->net->stats.tx_errors++;
1215 switch (urb->status) {
1217 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1220 /* software-driven interface shutdown */
1221 case -ECONNRESET: // async unlink
1222 case -ESHUTDOWN: // hardware gone
1225 /* like rx, tx gets controller i/o faults during hub_wq
1226 * delays and so it uses the same throttling mechanism.
1231 usb_mark_last_busy(dev->udev);
1232 if (!timer_pending (&dev->delay)) {
1233 mod_timer (&dev->delay,
1234 jiffies + THROTTLE_JIFFIES);
1235 netif_dbg(dev, link, dev->net,
1236 "tx throttle %d\n", urb->status);
1238 netif_stop_queue (dev->net);
1241 netif_dbg(dev, tx_err, dev->net,
1242 "tx err %d\n", entry->urb->status);
1247 usb_autopm_put_interface_async(dev->intf);
1248 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1251 /*-------------------------------------------------------------------------*/
1253 void usbnet_tx_timeout (struct net_device *net)
1255 struct usbnet *dev = netdev_priv(net);
1257 unlink_urbs (dev, &dev->txq);
1258 tasklet_schedule (&dev->bh);
1259 /* this needs to be handled individually because the generic layer
1260 * doesn't know what is sufficient and could not restore private
1261 * information if a remedy of an unconditional reset were used.
1263 if (dev->driver_info->recover)
1264 (dev->driver_info->recover)(dev);
1266 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1268 /*-------------------------------------------------------------------------*/
1270 static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1272 unsigned num_sgs, total_len = 0;
1275 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1279 /* reserve one for zero packet */
1280 urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
1285 urb->num_sgs = num_sgs;
1286 sg_init_table(urb->sg, urb->num_sgs + 1);
1288 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1289 total_len += skb_headlen(skb);
1291 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1292 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1294 total_len += skb_frag_size(f);
1295 sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1298 urb->transfer_buffer_length = total_len;
1303 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1304 struct net_device *net)
1306 struct usbnet *dev = netdev_priv(net);
1307 unsigned int length;
1308 struct urb *urb = NULL;
1309 struct skb_data *entry;
1310 struct driver_info *info = dev->driver_info;
1311 unsigned long flags;
1315 skb_tx_timestamp(skb);
1317 // some devices want funky USB-level framing, for
1318 // win32 driver (usually) and/or hardware quirks
1319 if (info->tx_fixup) {
1320 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1322 /* packet collected; minidriver waiting for more */
1323 if (info->flags & FLAG_MULTI_PACKET)
1325 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1330 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1331 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1335 entry = (struct skb_data *) skb->cb;
1339 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1340 skb->data, skb->len, tx_complete, skb);
1341 if (dev->can_dma_sg) {
1342 if (build_dma_sg(skb, urb) < 0)
1345 length = urb->transfer_buffer_length;
1347 /* don't assume the hardware handles USB_ZERO_PACKET
1348 * NOTE: strictly conforming cdc-ether devices should expect
1349 * the ZLP here, but ignore the one-byte packet.
1350 * NOTE2: CDC NCM specification is different from CDC ECM when
1351 * handling ZLP/short packets, so cdc_ncm driver will make short
1352 * packet itself if needed.
1354 if (length % dev->maxpacket == 0) {
1355 if (!(info->flags & FLAG_SEND_ZLP)) {
1356 if (!(info->flags & FLAG_MULTI_PACKET)) {
1358 if (skb_tailroom(skb) && !urb->num_sgs) {
1359 skb->data[skb->len] = 0;
1361 } else if (urb->num_sgs)
1362 sg_set_buf(&urb->sg[urb->num_sgs++],
1363 dev->padding_pkt, 1);
1366 urb->transfer_flags |= URB_ZERO_PACKET;
1368 urb->transfer_buffer_length = length;
1370 if (info->flags & FLAG_MULTI_PACKET) {
1371 /* Driver has set number of packets and a length delta.
1372 * Calculate the complete length and ensure that it's
1375 entry->length += length;
1376 if (WARN_ON_ONCE(entry->length <= 0))
1377 entry->length = length;
1379 usbnet_set_skb_tx_stats(skb, 1, length);
1382 spin_lock_irqsave(&dev->txq.lock, flags);
1383 retval = usb_autopm_get_interface_async(dev->intf);
1385 spin_unlock_irqrestore(&dev->txq.lock, flags);
1390 /* if this triggers the device is still a sleep */
1391 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1392 /* transmission will be done in resume */
1393 usb_anchor_urb(urb, &dev->deferred);
1394 /* no use to process more packets */
1395 netif_stop_queue(net);
1397 spin_unlock_irqrestore(&dev->txq.lock, flags);
1398 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1403 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1405 netif_stop_queue (net);
1406 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1407 usb_autopm_put_interface_async(dev->intf);
1410 usb_autopm_put_interface_async(dev->intf);
1411 netif_dbg(dev, tx_err, dev->net,
1412 "tx: submit urb err %d\n", retval);
1415 net->trans_start = jiffies;
1416 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1417 if (dev->txq.qlen >= TX_QLEN (dev))
1418 netif_stop_queue (net);
1420 spin_unlock_irqrestore (&dev->txq.lock, flags);
1423 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1425 dev->net->stats.tx_dropped++;
1428 dev_kfree_skb_any (skb);
1434 netif_dbg(dev, tx_queued, dev->net,
1435 "> tx, len %u, type 0x%x\n", length, skb->protocol);
1439 return NETDEV_TX_OK;
1441 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1443 static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
1449 /* don't refill the queue all at once */
1450 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1451 urb = usb_alloc_urb(0, flags);
1453 ret = rx_submit(dev, urb, flags);
1465 /*-------------------------------------------------------------------------*/
1467 // tasklet (work deferred from completions, in_irq) or timer
1469 static void usbnet_bh (unsigned long param)
1471 struct usbnet *dev = (struct usbnet *) param;
1472 struct sk_buff *skb;
1473 struct skb_data *entry;
1475 while ((skb = skb_dequeue (&dev->done))) {
1476 entry = (struct skb_data *) skb->cb;
1477 switch (entry->state) {
1479 entry->state = rx_cleanup;
1480 rx_process (dev, skb);
1483 kfree(entry->urb->sg);
1485 usb_free_urb (entry->urb);
1486 dev_kfree_skb (skb);
1489 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1493 /* restart RX again after disabling due to high error rate */
1494 clear_bit(EVENT_RX_KILL, &dev->flags);
1496 /* waiting for all pending urbs to complete?
1497 * only then can we forgo submitting anew
1499 if (waitqueue_active(&dev->wait)) {
1500 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1501 wake_up_all(&dev->wait);
1503 // or are we maybe short a few urbs?
1504 } else if (netif_running (dev->net) &&
1505 netif_device_present (dev->net) &&
1506 netif_carrier_ok(dev->net) &&
1507 !timer_pending (&dev->delay) &&
1508 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1509 int temp = dev->rxq.qlen;
1511 if (temp < RX_QLEN(dev)) {
1512 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1514 if (temp != dev->rxq.qlen)
1515 netif_dbg(dev, link, dev->net,
1516 "rxqlen %d --> %d\n",
1517 temp, dev->rxq.qlen);
1518 if (dev->rxq.qlen < RX_QLEN(dev))
1519 tasklet_schedule (&dev->bh);
1521 if (dev->txq.qlen < TX_QLEN (dev))
1522 netif_wake_queue (dev->net);
1527 /*-------------------------------------------------------------------------
1529 * USB Device Driver support
1531 *-------------------------------------------------------------------------*/
1533 // precondition: never called in_interrupt
1535 void usbnet_disconnect (struct usb_interface *intf)
1538 struct usb_device *xdev;
1539 struct net_device *net;
1541 dev = usb_get_intfdata(intf);
1542 usb_set_intfdata(intf, NULL);
1546 xdev = interface_to_usbdev (intf);
1548 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1549 intf->dev.driver->name,
1550 xdev->bus->bus_name, xdev->devpath,
1551 dev->driver_info->description);
1554 unregister_netdev (net);
1556 cancel_work_sync(&dev->kevent);
1558 usb_scuttle_anchored_urbs(&dev->deferred);
1560 if (dev->driver_info->unbind)
1561 dev->driver_info->unbind (dev, intf);
1563 usb_kill_urb(dev->interrupt);
1564 usb_free_urb(dev->interrupt);
1565 kfree(dev->padding_pkt);
1569 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1571 static const struct net_device_ops usbnet_netdev_ops = {
1572 .ndo_open = usbnet_open,
1573 .ndo_stop = usbnet_stop,
1574 .ndo_start_xmit = usbnet_start_xmit,
1575 .ndo_tx_timeout = usbnet_tx_timeout,
1576 .ndo_set_rx_mode = usbnet_set_rx_mode,
1577 .ndo_change_mtu = usbnet_change_mtu,
1578 .ndo_set_mac_address = eth_mac_addr,
1579 .ndo_validate_addr = eth_validate_addr,
1582 /*-------------------------------------------------------------------------*/
1584 // precondition: never called in_interrupt
1586 static struct device_type wlan_type = {
1590 static struct device_type wwan_type = {
1595 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1598 struct net_device *net;
1599 struct usb_host_interface *interface;
1600 struct driver_info *info;
1601 struct usb_device *xdev;
1604 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1606 /* usbnet already took usb runtime pm, so have to enable the feature
1607 * for usb interface, otherwise usb_autopm_get_interface may return
1608 * failure if RUNTIME_PM is enabled.
1610 if (!driver->supports_autosuspend) {
1611 driver->supports_autosuspend = 1;
1612 pm_runtime_enable(&udev->dev);
1615 name = udev->dev.driver->name;
1616 info = (struct driver_info *) prod->driver_info;
1618 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1621 xdev = interface_to_usbdev (udev);
1622 interface = udev->cur_altsetting;
1626 // set up our own records
1627 net = alloc_etherdev(sizeof(*dev));
1631 /* netdev_printk() needs this so do it as early as possible */
1632 SET_NETDEV_DEV(net, &udev->dev);
1634 dev = netdev_priv(net);
1637 dev->driver_info = info;
1638 dev->driver_name = name;
1639 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1640 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1641 init_waitqueue_head(&dev->wait);
1642 skb_queue_head_init (&dev->rxq);
1643 skb_queue_head_init (&dev->txq);
1644 skb_queue_head_init (&dev->done);
1645 skb_queue_head_init(&dev->rxq_pause);
1646 dev->bh.func = usbnet_bh;
1647 dev->bh.data = (unsigned long) dev;
1648 INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
1649 init_usb_anchor(&dev->deferred);
1650 dev->delay.function = usbnet_bh;
1651 dev->delay.data = (unsigned long) dev;
1652 init_timer (&dev->delay);
1653 mutex_init (&dev->phy_mutex);
1654 mutex_init(&dev->interrupt_mutex);
1655 dev->interrupt_count = 0;
1658 strcpy (net->name, "usb%d");
1659 memcpy (net->dev_addr, node_id, sizeof node_id);
1661 /* rx and tx sides can use different message sizes;
1662 * bind() should set rx_urb_size in that case.
1664 dev->hard_mtu = net->mtu + net->hard_header_len;
1666 net->netdev_ops = &usbnet_netdev_ops;
1667 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1668 net->ethtool_ops = &usbnet_ethtool_ops;
1670 // allow device-specific bind/init procedures
1671 // NOTE net->name still not usable ...
1673 status = info->bind (dev, udev);
1677 // heuristic: "usb%d" for links we know are two-host,
1678 // else "eth%d" when there's reasonable doubt. userspace
1679 // can rename the link if it knows better.
1680 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1681 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1682 (net->dev_addr [0] & 0x02) == 0))
1683 strcpy (net->name, "eth%d");
1684 /* WLAN devices should always be named "wlan%d" */
1685 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1686 strcpy(net->name, "wlan%d");
1687 /* WWAN devices should always be named "wwan%d" */
1688 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1689 strcpy(net->name, "wwan%d");
1691 /* devices that cannot do ARP */
1692 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1693 net->flags |= IFF_NOARP;
1695 /* maybe the remote can't receive an Ethernet MTU */
1696 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1697 net->mtu = dev->hard_mtu - net->hard_header_len;
1698 } else if (!info->in || !info->out)
1699 status = usbnet_get_endpoints (dev, udev);
1701 dev->in = usb_rcvbulkpipe (xdev, info->in);
1702 dev->out = usb_sndbulkpipe (xdev, info->out);
1703 if (!(info->flags & FLAG_NO_SETINT))
1704 status = usb_set_interface (xdev,
1705 interface->desc.bInterfaceNumber,
1706 interface->desc.bAlternateSetting);
1711 if (status >= 0 && dev->status)
1712 status = init_status (dev, udev);
1716 if (!dev->rx_urb_size)
1717 dev->rx_urb_size = dev->hard_mtu;
1718 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1720 /* let userspace know we have a random address */
1721 if (ether_addr_equal(net->dev_addr, node_id))
1722 net->addr_assign_type = NET_ADDR_RANDOM;
1724 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1725 SET_NETDEV_DEVTYPE(net, &wlan_type);
1726 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1727 SET_NETDEV_DEVTYPE(net, &wwan_type);
1729 /* initialize max rx_qlen and tx_qlen */
1730 usbnet_update_max_qlen(dev);
1732 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1733 !(info->flags & FLAG_MULTI_PACKET)) {
1734 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
1735 if (!dev->padding_pkt) {
1741 status = register_netdev (net);
1744 netif_info(dev, probe, dev->net,
1745 "register '%s' at usb-%s-%s, %s, %pM\n",
1746 udev->dev.driver->name,
1747 xdev->bus->bus_name, xdev->devpath,
1748 dev->driver_info->description,
1751 // ok, it's ready to go.
1752 usb_set_intfdata (udev, dev);
1754 netif_device_attach (net);
1756 if (dev->driver_info->flags & FLAG_LINK_INTR)
1757 usbnet_link_change(dev, 0, 0);
1762 kfree(dev->padding_pkt);
1764 usb_free_urb(dev->interrupt);
1767 info->unbind (dev, udev);
1769 /* subdrivers must undo all they did in bind() if they
1770 * fail it, but we may fail later and a deferred kevent
1771 * may trigger an error resubmitting itself and, worse,
1772 * schedule a timer. So we kill it all just in case.
1774 cancel_work_sync(&dev->kevent);
1775 del_timer_sync(&dev->delay);
1780 EXPORT_SYMBOL_GPL(usbnet_probe);
1782 /*-------------------------------------------------------------------------*/
1785 * suspend the whole driver as soon as the first interface is suspended
1786 * resume only when the last interface is resumed
1789 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1791 struct usbnet *dev = usb_get_intfdata(intf);
1793 if (!dev->suspend_count++) {
1794 spin_lock_irq(&dev->txq.lock);
1795 /* don't autosuspend while transmitting */
1796 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
1797 dev->suspend_count--;
1798 spin_unlock_irq(&dev->txq.lock);
1801 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1802 spin_unlock_irq(&dev->txq.lock);
1805 * accelerate emptying of the rx and queues, to avoid
1806 * having everything error out.
1808 netif_device_detach (dev->net);
1809 usbnet_terminate_urbs(dev);
1810 __usbnet_status_stop_force(dev);
1813 * reattach so runtime management can use and
1816 netif_device_attach (dev->net);
1820 EXPORT_SYMBOL_GPL(usbnet_suspend);
1822 int usbnet_resume (struct usb_interface *intf)
1824 struct usbnet *dev = usb_get_intfdata(intf);
1825 struct sk_buff *skb;
1829 if (!--dev->suspend_count) {
1830 /* resume interrupt URB if it was previously submitted */
1831 __usbnet_status_start_force(dev, GFP_NOIO);
1833 spin_lock_irq(&dev->txq.lock);
1834 while ((res = usb_get_from_anchor(&dev->deferred))) {
1836 skb = (struct sk_buff *)res->context;
1837 retval = usb_submit_urb(res, GFP_ATOMIC);
1839 dev_kfree_skb_any(skb);
1842 usb_autopm_put_interface_async(dev->intf);
1844 dev->net->trans_start = jiffies;
1845 __skb_queue_tail(&dev->txq, skb);
1850 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1851 spin_unlock_irq(&dev->txq.lock);
1853 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1854 /* handle remote wakeup ASAP
1855 * we cannot race against stop
1857 if (netif_device_present(dev->net) &&
1858 !timer_pending(&dev->delay) &&
1859 !test_bit(EVENT_RX_HALT, &dev->flags))
1860 rx_alloc_submit(dev, GFP_NOIO);
1862 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1863 netif_tx_wake_all_queues(dev->net);
1864 tasklet_schedule (&dev->bh);
1868 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1869 usb_autopm_get_interface_no_resume(intf);
1873 EXPORT_SYMBOL_GPL(usbnet_resume);
1876 * Either a subdriver implements manage_power, then it is assumed to always
1877 * be ready to be suspended or it reports the readiness to be suspended
1880 void usbnet_device_suggests_idle(struct usbnet *dev)
1882 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1883 dev->intf->needs_remote_wakeup = 1;
1884 usb_autopm_put_interface_async(dev->intf);
1887 EXPORT_SYMBOL(usbnet_device_suggests_idle);
1890 * For devices that can do without special commands
1892 int usbnet_manage_power(struct usbnet *dev, int on)
1894 dev->intf->needs_remote_wakeup = on;
1897 EXPORT_SYMBOL(usbnet_manage_power);
1899 void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1901 /* update link after link is reseted */
1902 if (link && !need_reset)
1903 netif_carrier_on(dev->net);
1905 netif_carrier_off(dev->net);
1907 if (need_reset && link)
1908 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
1910 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1912 EXPORT_SYMBOL(usbnet_link_change);
1914 /*-------------------------------------------------------------------------*/
1915 static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1916 u16 value, u16 index, void *data, u16 size)
1921 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1922 " value=0x%04x index=0x%04x size=%d\n",
1923 cmd, reqtype, value, index, size);
1926 buf = kmalloc(size, GFP_KERNEL);
1931 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1932 cmd, reqtype, value, index, buf, size,
1933 USB_CTRL_GET_TIMEOUT);
1934 if (err > 0 && err <= size)
1935 memcpy(data, buf, err);
1941 static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1942 u16 value, u16 index, const void *data,
1948 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1949 " value=0x%04x index=0x%04x size=%d\n",
1950 cmd, reqtype, value, index, size);
1953 buf = kmemdup(data, size, GFP_KERNEL);
1958 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1959 cmd, reqtype, value, index, buf, size,
1960 USB_CTRL_SET_TIMEOUT);
1967 int cdc_parse_cdc_header(struct usb_cdc_parsed_header *hdr,
1968 struct usb_interface *intf,
1972 /* duplicates are ignored */
1973 struct usb_cdc_union_desc *union_header = NULL;
1975 /* duplicates are not tolerated */
1976 struct usb_cdc_header_desc *header = NULL;
1977 struct usb_cdc_ether_desc *ether = NULL;
1978 struct usb_cdc_mdlm_detail_desc *detail = NULL;
1979 struct usb_cdc_mdlm_desc *desc = NULL;
1981 unsigned int elength;
1984 memset(hdr, 0x00, sizeof(struct usb_cdc_parsed_header));
1985 hdr->phonet_magic_present = false;
1986 while (buflen > 0) {
1987 elength = buffer[0];
1989 dev_err(&intf->dev, "skipping garbage byte\n");
1993 if (buffer[1] != USB_DT_CS_INTERFACE) {
1994 dev_err(&intf->dev, "skipping garbage\n");
1998 switch (buffer[2]) {
1999 case USB_CDC_UNION_TYPE: /* we've found it */
2000 if (elength < sizeof(struct usb_cdc_union_desc))
2003 dev_err(&intf->dev, "More than one union descriptor, skipping ...\n");
2006 union_header = (struct usb_cdc_union_desc *)buffer;
2008 case USB_CDC_COUNTRY_TYPE:
2009 if (elength < sizeof(struct usb_cdc_country_functional_desc))
2011 hdr->usb_cdc_country_functional_desc =
2012 (struct usb_cdc_country_functional_desc *)buffer;
2014 case USB_CDC_HEADER_TYPE:
2015 if (elength != sizeof(struct usb_cdc_header_desc))
2019 header = (struct usb_cdc_header_desc *)buffer;
2021 case USB_CDC_ACM_TYPE:
2022 if (elength < sizeof(struct usb_cdc_acm_descriptor))
2024 hdr->usb_cdc_acm_descriptor =
2025 (struct usb_cdc_acm_descriptor *)buffer;
2027 case USB_CDC_ETHERNET_TYPE:
2028 if (elength != sizeof(struct usb_cdc_ether_desc))
2032 ether = (struct usb_cdc_ether_desc *)buffer;
2034 case USB_CDC_CALL_MANAGEMENT_TYPE:
2035 if (elength < sizeof(struct usb_cdc_call_mgmt_descriptor))
2037 hdr->usb_cdc_call_mgmt_descriptor =
2038 (struct usb_cdc_call_mgmt_descriptor *)buffer;
2040 case USB_CDC_DMM_TYPE:
2041 if (elength < sizeof(struct usb_cdc_dmm_desc))
2043 hdr->usb_cdc_dmm_desc =
2044 (struct usb_cdc_dmm_desc *)buffer;
2046 case USB_CDC_MDLM_TYPE:
2047 if (elength < sizeof(struct usb_cdc_mdlm_desc *))
2051 desc = (struct usb_cdc_mdlm_desc *)buffer;
2053 case USB_CDC_MDLM_DETAIL_TYPE:
2054 if (elength < sizeof(struct usb_cdc_mdlm_detail_desc *))
2058 detail = (struct usb_cdc_mdlm_detail_desc *)buffer;
2060 case USB_CDC_NCM_TYPE:
2061 if (elength < sizeof(struct usb_cdc_ncm_desc))
2063 hdr->usb_cdc_ncm_desc = (struct usb_cdc_ncm_desc *)buffer;
2065 case USB_CDC_MBIM_TYPE:
2066 if (elength < sizeof(struct usb_cdc_mbim_desc))
2069 hdr->usb_cdc_mbim_desc = (struct usb_cdc_mbim_desc *)buffer;
2071 case USB_CDC_MBIM_EXTENDED_TYPE:
2072 if (elength < sizeof(struct usb_cdc_mbim_extended_desc))
2074 hdr->usb_cdc_mbim_extended_desc =
2075 (struct usb_cdc_mbim_extended_desc *)buffer;
2077 case CDC_PHONET_MAGIC_NUMBER:
2078 hdr->phonet_magic_present = true;
2082 * there are LOTS more CDC descriptors that
2083 * could legitimately be found here.
2085 dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %ud\n",
2086 buffer[2], elength);
2094 hdr->usb_cdc_union_desc = union_header;
2095 hdr->usb_cdc_header_desc = header;
2096 hdr->usb_cdc_mdlm_detail_desc = detail;
2097 hdr->usb_cdc_mdlm_desc = desc;
2098 hdr->usb_cdc_ether_desc = ether;
2102 EXPORT_SYMBOL(cdc_parse_cdc_header);
2105 * The function can't be called inside suspend/resume callback,
2106 * otherwise deadlock will be caused.
2108 int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2109 u16 value, u16 index, void *data, u16 size)
2113 if (usb_autopm_get_interface(dev->intf) < 0)
2115 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2117 usb_autopm_put_interface(dev->intf);
2120 EXPORT_SYMBOL_GPL(usbnet_read_cmd);
2123 * The function can't be called inside suspend/resume callback,
2124 * otherwise deadlock will be caused.
2126 int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2127 u16 value, u16 index, const void *data, u16 size)
2131 if (usb_autopm_get_interface(dev->intf) < 0)
2133 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2135 usb_autopm_put_interface(dev->intf);
2138 EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2141 * The function can be called inside suspend/resume callback safely
2142 * and should only be called by suspend/resume callback generally.
2144 int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2145 u16 value, u16 index, void *data, u16 size)
2147 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2150 EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
2153 * The function can be called inside suspend/resume callback safely
2154 * and should only be called by suspend/resume callback generally.
2156 int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2157 u16 value, u16 index, const void *data,
2160 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2163 EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
2165 static void usbnet_async_cmd_cb(struct urb *urb)
2167 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2168 int status = urb->status;
2171 dev_dbg(&urb->dev->dev, "%s failed with %d",
2179 * The caller must make sure that device can't be put into suspend
2180 * state until the control URB completes.
2182 int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2183 u16 value, u16 index, const void *data, u16 size)
2185 struct usb_ctrlrequest *req = NULL;
2190 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2191 " value=0x%04x index=0x%04x size=%d\n",
2192 cmd, reqtype, value, index, size);
2194 urb = usb_alloc_urb(0, GFP_ATOMIC);
2196 netdev_err(dev->net, "Error allocating URB in"
2197 " %s!\n", __func__);
2202 buf = kmemdup(data, size, GFP_ATOMIC);
2204 netdev_err(dev->net, "Error allocating buffer"
2205 " in %s!\n", __func__);
2210 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
2214 req->bRequestType = reqtype;
2215 req->bRequest = cmd;
2216 req->wValue = cpu_to_le16(value);
2217 req->wIndex = cpu_to_le16(index);
2218 req->wLength = cpu_to_le16(size);
2220 usb_fill_control_urb(urb, dev->udev,
2221 usb_sndctrlpipe(dev->udev, 0),
2222 (void *)req, buf, size,
2223 usbnet_async_cmd_cb, req);
2224 urb->transfer_flags |= URB_FREE_BUFFER;
2226 err = usb_submit_urb(urb, GFP_ATOMIC);
2228 netdev_err(dev->net, "Error submitting the control"
2229 " message: status=%d\n", err);
2243 EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2244 /*-------------------------------------------------------------------------*/
2246 static int __init usbnet_init(void)
2248 /* Compiler should optimize this out. */
2250 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
2252 eth_random_addr(node_id);
2255 module_init(usbnet_init);
2257 static void __exit usbnet_exit(void)
2260 module_exit(usbnet_exit);
2262 MODULE_AUTHOR("David Brownell");
2263 MODULE_DESCRIPTION("USB network driver framework");
2264 MODULE_LICENSE("GPL");