1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB Network driver infrastructure
4 * Copyright (C) 2000-2005 by David Brownell
5 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
9 * This is a generic "USB networking" framework that works with several
10 * kinds of full and high speed networking devices: host-to-host cables,
11 * smart usb peripherals, and actual Ethernet adapters.
13 * These devices usually differ in terms of control protocols (if they
14 * even have one!) and sometimes they define new framing to wrap or batch
15 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
16 * so interface (un)binding, endpoint I/O queues, fault handling, and other
17 * issues can usefully be addressed by this framework.
20 // #define DEBUG // error path messages, extra info
21 // #define VERBOSE // more; success messages
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ctype.h>
28 #include <linux/ethtool.h>
29 #include <linux/workqueue.h>
30 #include <linux/mii.h>
31 #include <linux/usb.h>
32 #include <linux/usb/usbnet.h>
33 #include <linux/slab.h>
34 #include <linux/kernel.h>
35 #include <linux/pm_runtime.h>
37 /*-------------------------------------------------------------------------*/
40 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
41 * Several dozen bytes of IPv4 data can fit in two such transactions.
42 * One maximum size Ethernet packet takes twenty four of them.
43 * For high speed, each frame comfortably fits almost 36 max size
44 * Ethernet packets (so queues should be bigger).
46 * The goal is to let the USB host controller be busy for 5msec or
47 * more before an irq is required, under load. Jumbograms change
50 #define MAX_QUEUE_MEMORY (60 * 1518)
51 #define RX_QLEN(dev) ((dev)->rx_qlen)
52 #define TX_QLEN(dev) ((dev)->tx_qlen)
54 // reawaken network queue this soon after stopping; else watchdog barks
55 #define TX_TIMEOUT_JIFFIES (5*HZ)
57 /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
58 * us (it polls at HZ/4 usually) before we report too many false errors.
60 #define THROTTLE_JIFFIES (HZ/8)
63 #define UNLINK_TIMEOUT_MS 3
65 /*-------------------------------------------------------------------------*/
67 // randomly generated ethernet address
68 static u8 node_id [ETH_ALEN];
70 /* use ethtool to change the level for any given device */
71 static int msg_level = -1;
72 module_param (msg_level, int, 0);
73 MODULE_PARM_DESC (msg_level, "Override default message level");
75 /*-------------------------------------------------------------------------*/
77 /* handles CDC Ethernet and many other network "bulk data" interfaces */
78 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
81 struct usb_host_interface *alt = NULL;
82 struct usb_host_endpoint *in = NULL, *out = NULL;
83 struct usb_host_endpoint *status = NULL;
85 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
88 in = out = status = NULL;
89 alt = intf->altsetting + tmp;
91 /* take the first altsetting with in-bulk + out-bulk;
92 * remember any status endpoint, just in case;
93 * ignore other endpoints and altsettings.
95 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
96 struct usb_host_endpoint *e;
99 e = alt->endpoint + ep;
101 /* ignore endpoints which cannot transfer data */
102 if (!usb_endpoint_maxp(&e->desc))
105 switch (e->desc.bmAttributes) {
106 case USB_ENDPOINT_XFER_INT:
107 if (!usb_endpoint_dir_in(&e->desc))
111 case USB_ENDPOINT_XFER_BULK:
116 if (usb_endpoint_dir_in(&e->desc)) {
119 else if (intr && !status)
129 if (!alt || !in || !out)
132 if (alt->desc.bAlternateSetting != 0 ||
133 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
134 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
135 alt->desc.bAlternateSetting);
140 dev->in = usb_rcvbulkpipe (dev->udev,
141 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
142 dev->out = usb_sndbulkpipe (dev->udev,
143 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
144 dev->status = status;
147 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
149 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
152 unsigned char buf [13];
154 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
156 tmp = hex2bin(dev->net->dev_addr, buf, 6);
158 dev_dbg(&dev->udev->dev,
159 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
166 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
168 static void intr_complete (struct urb *urb)
170 struct usbnet *dev = urb->context;
171 int status = urb->status;
176 dev->driver_info->status(dev, urb);
179 /* software-driven interface shutdown */
180 case -ENOENT: /* urb killed */
181 case -ESHUTDOWN: /* hardware gone */
182 netif_dbg(dev, ifdown, dev->net,
183 "intr shutdown, code %d\n", status);
186 /* NOTE: not throttling like RX/TX, since this endpoint
187 * already polls infrequently
190 netdev_dbg(dev->net, "intr status %d\n", status);
194 status = usb_submit_urb (urb, GFP_ATOMIC);
196 netif_err(dev, timer, dev->net,
197 "intr resubmit --> %d\n", status);
200 static int init_status (struct usbnet *dev, struct usb_interface *intf)
207 if (!dev->driver_info->status)
210 pipe = usb_rcvintpipe (dev->udev,
211 dev->status->desc.bEndpointAddress
212 & USB_ENDPOINT_NUMBER_MASK);
213 maxp = usb_maxpacket (dev->udev, pipe, 0);
215 /* avoid 1 msec chatter: min 8 msec poll rate */
216 period = max ((int) dev->status->desc.bInterval,
217 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
219 buf = kmalloc (maxp, GFP_KERNEL);
221 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
222 if (!dev->interrupt) {
226 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
227 buf, maxp, intr_complete, dev, period);
228 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
230 "status ep%din, %d bytes period %d\n",
231 usb_pipeendpoint(pipe), maxp, period);
237 /* Submit the interrupt URB if not previously submitted, increasing refcount */
238 int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
242 WARN_ON_ONCE(dev->interrupt == NULL);
243 if (dev->interrupt) {
244 mutex_lock(&dev->interrupt_mutex);
246 if (++dev->interrupt_count == 1)
247 ret = usb_submit_urb(dev->interrupt, mem_flags);
249 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
250 dev->interrupt_count);
251 mutex_unlock(&dev->interrupt_mutex);
255 EXPORT_SYMBOL_GPL(usbnet_status_start);
257 /* For resume; submit interrupt URB if previously submitted */
258 static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
262 mutex_lock(&dev->interrupt_mutex);
263 if (dev->interrupt_count) {
264 ret = usb_submit_urb(dev->interrupt, mem_flags);
265 dev_dbg(&dev->udev->dev,
266 "submitted interrupt URB for resume\n");
268 mutex_unlock(&dev->interrupt_mutex);
272 /* Kill the interrupt URB if all submitters want it killed */
273 void usbnet_status_stop(struct usbnet *dev)
275 if (dev->interrupt) {
276 mutex_lock(&dev->interrupt_mutex);
277 WARN_ON(dev->interrupt_count == 0);
279 if (dev->interrupt_count && --dev->interrupt_count == 0)
280 usb_kill_urb(dev->interrupt);
282 dev_dbg(&dev->udev->dev,
283 "decremented interrupt URB count to %d\n",
284 dev->interrupt_count);
285 mutex_unlock(&dev->interrupt_mutex);
288 EXPORT_SYMBOL_GPL(usbnet_status_stop);
290 /* For suspend; always kill interrupt URB */
291 static void __usbnet_status_stop_force(struct usbnet *dev)
293 if (dev->interrupt) {
294 mutex_lock(&dev->interrupt_mutex);
295 usb_kill_urb(dev->interrupt);
296 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
297 mutex_unlock(&dev->interrupt_mutex);
301 /* Passes this packet up the stack, updating its accounting.
302 * Some link protocols batch packets, so their rx_fixup paths
303 * can return clones as well as just modify the original skb.
305 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
307 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
311 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
312 skb_queue_tail(&dev->rxq_pause, skb);
316 /* only update if unset to allow minidriver rx_fixup override */
317 if (skb->protocol == 0)
318 skb->protocol = eth_type_trans (skb, dev->net);
320 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
321 stats64->rx_packets++;
322 stats64->rx_bytes += skb->len;
323 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
325 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
326 skb->len + sizeof (struct ethhdr), skb->protocol);
327 memset (skb->cb, 0, sizeof (struct skb_data));
329 if (skb_defer_rx_timestamp(skb))
332 status = netif_rx (skb);
333 if (status != NET_RX_SUCCESS)
334 netif_dbg(dev, rx_err, dev->net,
335 "netif_rx status %d\n", status);
337 EXPORT_SYMBOL_GPL(usbnet_skb_return);
339 /* must be called if hard_mtu or rx_urb_size changed */
340 void usbnet_update_max_qlen(struct usbnet *dev)
342 enum usb_device_speed speed = dev->udev->speed;
344 if (!dev->rx_urb_size || !dev->hard_mtu)
348 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
349 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
351 case USB_SPEED_SUPER:
352 case USB_SPEED_SUPER_PLUS:
354 * Not take default 5ms qlen for super speed HC to
355 * save memory, and iperf tests show 2.5ms qlen can
358 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
359 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
363 dev->rx_qlen = dev->tx_qlen = 4;
366 EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
369 /*-------------------------------------------------------------------------
371 * Network Device Driver (peer link to "Host Device", from USB host)
373 *-------------------------------------------------------------------------*/
375 int usbnet_change_mtu (struct net_device *net, int new_mtu)
377 struct usbnet *dev = netdev_priv(net);
378 int ll_mtu = new_mtu + net->hard_header_len;
379 int old_hard_mtu = dev->hard_mtu;
380 int old_rx_urb_size = dev->rx_urb_size;
382 // no second zero-length packet read wanted after mtu-sized packets
383 if ((ll_mtu % dev->maxpacket) == 0)
387 dev->hard_mtu = net->mtu + net->hard_header_len;
388 if (dev->rx_urb_size == old_hard_mtu) {
389 dev->rx_urb_size = dev->hard_mtu;
390 if (dev->rx_urb_size > old_rx_urb_size) {
391 usbnet_pause_rx(dev);
392 usbnet_unlink_rx_urbs(dev);
393 usbnet_resume_rx(dev);
397 /* max qlen depend on hard_mtu and rx_urb_size */
398 usbnet_update_max_qlen(dev);
402 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
404 /* The caller must hold list->lock */
405 static void __usbnet_queue_skb(struct sk_buff_head *list,
406 struct sk_buff *newsk, enum skb_state state)
408 struct skb_data *entry = (struct skb_data *) newsk->cb;
410 __skb_queue_tail(list, newsk);
411 entry->state = state;
414 /*-------------------------------------------------------------------------*/
416 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
417 * completion callbacks. 2.5 should have fixed those bugs...
420 static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
421 struct sk_buff_head *list, enum skb_state state)
424 enum skb_state old_state;
425 struct skb_data *entry = (struct skb_data *) skb->cb;
427 spin_lock_irqsave(&list->lock, flags);
428 old_state = entry->state;
429 entry->state = state;
430 __skb_unlink(skb, list);
432 /* defer_bh() is never called with list == &dev->done.
433 * spin_lock_nested() tells lockdep that it is OK to take
434 * dev->done.lock here with list->lock held.
436 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
438 __skb_queue_tail(&dev->done, skb);
439 if (dev->done.qlen == 1)
440 tasklet_schedule(&dev->bh);
441 spin_unlock(&dev->done.lock);
442 spin_unlock_irqrestore(&list->lock, flags);
446 /* some work can't be done in tasklets, so we use keventd
448 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
449 * but tasklet_schedule() doesn't. hope the failure is rare.
451 void usbnet_defer_kevent (struct usbnet *dev, int work)
453 set_bit (work, &dev->flags);
454 if (!schedule_work (&dev->kevent))
455 netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
457 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
459 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
461 /*-------------------------------------------------------------------------*/
463 static void rx_complete (struct urb *urb);
465 static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
468 struct skb_data *entry;
470 unsigned long lockflags;
471 size_t size = dev->rx_urb_size;
473 /* prevent rx skb allocation when error ratio is high */
474 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
479 if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
480 skb = __netdev_alloc_skb(dev->net, size, 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_DEV_OPEN, &dev->flags) &&
503 !test_bit (EVENT_RX_HALT, &dev->flags) &&
504 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
505 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
507 usbnet_defer_kevent (dev, EVENT_RX_HALT);
510 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
513 netif_dbg(dev, ifdown, dev->net, "device gone\n");
514 netif_device_detach (dev->net);
520 netif_dbg(dev, rx_err, dev->net,
521 "rx submit, %d\n", retval);
522 tasklet_schedule (&dev->bh);
525 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
528 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
531 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
533 dev_kfree_skb_any (skb);
540 /*-------------------------------------------------------------------------*/
542 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
544 if (dev->driver_info->rx_fixup &&
545 !dev->driver_info->rx_fixup (dev, skb)) {
546 /* With RX_ASSEMBLE, rx_fixup() must update counters */
547 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
548 dev->net->stats.rx_errors++;
551 // else network stack removes extra byte if we forced a short packet
553 /* all data was already cloned from skb inside the driver */
554 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
557 if (skb->len < ETH_HLEN) {
558 dev->net->stats.rx_errors++;
559 dev->net->stats.rx_length_errors++;
560 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
562 usbnet_skb_return(dev, skb);
567 skb_queue_tail(&dev->done, skb);
570 /*-------------------------------------------------------------------------*/
572 static void rx_complete (struct urb *urb)
574 struct sk_buff *skb = (struct sk_buff *) urb->context;
575 struct skb_data *entry = (struct skb_data *) skb->cb;
576 struct usbnet *dev = entry->dev;
577 int urb_status = urb->status;
578 enum skb_state state;
580 skb_put (skb, urb->actual_length);
584 switch (urb_status) {
589 /* stalls need manual reset. this is rare ... except that
590 * when going through USB 2.0 TTs, unplug appears this way.
591 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
592 * storm, recovering as needed.
595 dev->net->stats.rx_errors++;
596 usbnet_defer_kevent (dev, EVENT_RX_HALT);
599 /* software-driven interface shutdown */
600 case -ECONNRESET: /* async unlink */
601 case -ESHUTDOWN: /* hardware gone */
602 netif_dbg(dev, ifdown, dev->net,
603 "rx shutdown, code %d\n", urb_status);
606 /* we get controller i/o faults during hub_wq disconnect() delays.
607 * throttle down resubmits, to avoid log floods; just temporarily,
608 * so we still recover when the fault isn't a hub_wq delay.
613 dev->net->stats.rx_errors++;
614 if (!timer_pending (&dev->delay)) {
615 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
616 netif_dbg(dev, link, dev->net,
617 "rx throttle %d\n", urb_status);
625 /* data overrun ... flush fifo? */
627 dev->net->stats.rx_over_errors++;
632 dev->net->stats.rx_errors++;
633 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
637 /* stop rx if packet error rate is high */
638 if (++dev->pkt_cnt > 30) {
642 if (state == rx_cleanup)
644 if (dev->pkt_err > 20)
645 set_bit(EVENT_RX_KILL, &dev->flags);
648 state = defer_bh(dev, skb, &dev->rxq, state);
651 if (netif_running (dev->net) &&
652 !test_bit (EVENT_RX_HALT, &dev->flags) &&
653 state != unlink_start) {
654 rx_submit (dev, urb, GFP_ATOMIC);
655 usb_mark_last_busy(dev->udev);
660 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
663 /*-------------------------------------------------------------------------*/
664 void usbnet_pause_rx(struct usbnet *dev)
666 set_bit(EVENT_RX_PAUSED, &dev->flags);
668 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
670 EXPORT_SYMBOL_GPL(usbnet_pause_rx);
672 void usbnet_resume_rx(struct usbnet *dev)
677 clear_bit(EVENT_RX_PAUSED, &dev->flags);
679 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
680 usbnet_skb_return(dev, skb);
684 tasklet_schedule(&dev->bh);
686 netif_dbg(dev, rx_status, dev->net,
687 "paused rx queue disabled, %d skbs requeued\n", num);
689 EXPORT_SYMBOL_GPL(usbnet_resume_rx);
691 void usbnet_purge_paused_rxq(struct usbnet *dev)
693 skb_queue_purge(&dev->rxq_pause);
695 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
697 /*-------------------------------------------------------------------------*/
699 // unlink pending rx/tx; completion handlers do all other cleanup
701 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
707 spin_lock_irqsave (&q->lock, flags);
708 while (!skb_queue_empty(q)) {
709 struct skb_data *entry;
713 skb_queue_walk(q, skb) {
714 entry = (struct skb_data *) skb->cb;
715 if (entry->state != unlink_start)
720 entry->state = unlink_start;
724 * Get reference count of the URB to avoid it to be
725 * freed during usb_unlink_urb, which may trigger
726 * use-after-free problem inside usb_unlink_urb since
727 * usb_unlink_urb is always racing with .complete
728 * handler(include defer_bh).
731 spin_unlock_irqrestore(&q->lock, flags);
732 // during some PM-driven resume scenarios,
733 // these (async) unlinks complete immediately
734 retval = usb_unlink_urb (urb);
735 if (retval != -EINPROGRESS && retval != 0)
736 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
740 spin_lock_irqsave(&q->lock, flags);
742 spin_unlock_irqrestore (&q->lock, flags);
746 // Flush all pending rx urbs
747 // minidrivers may need to do this when the MTU changes
749 void usbnet_unlink_rx_urbs(struct usbnet *dev)
751 if (netif_running(dev->net)) {
752 (void) unlink_urbs (dev, &dev->rxq);
753 tasklet_schedule(&dev->bh);
756 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
758 /*-------------------------------------------------------------------------*/
760 static void wait_skb_queue_empty(struct sk_buff_head *q)
764 spin_lock_irqsave(&q->lock, flags);
765 while (!skb_queue_empty(q)) {
766 spin_unlock_irqrestore(&q->lock, flags);
767 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
768 set_current_state(TASK_UNINTERRUPTIBLE);
769 spin_lock_irqsave(&q->lock, flags);
771 spin_unlock_irqrestore(&q->lock, flags);
774 // precondition: never called in_interrupt
775 static void usbnet_terminate_urbs(struct usbnet *dev)
777 DECLARE_WAITQUEUE(wait, current);
780 /* ensure there are no more active urbs */
781 add_wait_queue(&dev->wait, &wait);
782 set_current_state(TASK_UNINTERRUPTIBLE);
783 temp = unlink_urbs(dev, &dev->txq) +
784 unlink_urbs(dev, &dev->rxq);
786 /* maybe wait for deletions to finish. */
787 wait_skb_queue_empty(&dev->rxq);
788 wait_skb_queue_empty(&dev->txq);
789 wait_skb_queue_empty(&dev->done);
790 netif_dbg(dev, ifdown, dev->net,
791 "waited for %d urb completions\n", temp);
792 set_current_state(TASK_RUNNING);
793 remove_wait_queue(&dev->wait, &wait);
796 int usbnet_stop (struct net_device *net)
798 struct usbnet *dev = netdev_priv(net);
799 const struct driver_info *info = dev->driver_info;
802 clear_bit(EVENT_DEV_OPEN, &dev->flags);
803 netif_stop_queue (net);
805 netif_info(dev, ifdown, dev->net,
806 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
807 net->stats.rx_packets, net->stats.tx_packets,
808 net->stats.rx_errors, net->stats.tx_errors);
810 /* to not race resume */
811 pm = usb_autopm_get_interface(dev->intf);
812 /* allow minidriver to stop correctly (wireless devices to turn off
815 retval = info->stop(dev);
817 netif_info(dev, ifdown, dev->net,
818 "stop fail (%d) usbnet usb-%s-%s, %s\n",
820 dev->udev->bus->bus_name, dev->udev->devpath,
824 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
825 usbnet_terminate_urbs(dev);
827 usbnet_status_stop(dev);
829 usbnet_purge_paused_rxq(dev);
831 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
833 /* deferred work (timer, softirq, task) must also stop */
835 del_timer_sync (&dev->delay);
836 tasklet_kill (&dev->bh);
837 cancel_work_sync(&dev->kevent);
839 usb_autopm_put_interface(dev->intf);
841 if (info->manage_power && mpn)
842 info->manage_power(dev, 0);
844 usb_autopm_put_interface(dev->intf);
848 EXPORT_SYMBOL_GPL(usbnet_stop);
850 /*-------------------------------------------------------------------------*/
852 // posts reads, and enables write queuing
854 // precondition: never called in_interrupt
856 int usbnet_open (struct net_device *net)
858 struct usbnet *dev = netdev_priv(net);
860 const struct driver_info *info = dev->driver_info;
862 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
863 netif_info(dev, ifup, dev->net,
864 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
866 dev->udev->bus->bus_name,
872 // put into "known safe" state
873 if (info->reset && (retval = info->reset (dev)) < 0) {
874 netif_info(dev, ifup, dev->net,
875 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
877 dev->udev->bus->bus_name,
883 /* hard_mtu or rx_urb_size may change in reset() */
884 usbnet_update_max_qlen(dev);
886 // insist peer be connected
887 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
888 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
892 /* start any status interrupt transfer */
893 if (dev->interrupt) {
894 retval = usbnet_status_start(dev, GFP_KERNEL);
896 netif_err(dev, ifup, dev->net,
897 "intr submit %d\n", retval);
902 set_bit(EVENT_DEV_OPEN, &dev->flags);
903 netif_start_queue (net);
904 netif_info(dev, ifup, dev->net,
905 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
906 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
908 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
909 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
910 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
911 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
912 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
915 /* reset rx error state */
918 clear_bit(EVENT_RX_KILL, &dev->flags);
920 // delay posting reads until we're fully open
921 tasklet_schedule (&dev->bh);
922 if (info->manage_power) {
923 retval = info->manage_power(dev, 1);
926 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
928 usb_autopm_put_interface(dev->intf);
933 usb_autopm_put_interface(dev->intf);
937 EXPORT_SYMBOL_GPL(usbnet_open);
939 /*-------------------------------------------------------------------------*/
941 /* ethtool methods; minidrivers may need to add some more, but
942 * they'll probably want to use this base set.
945 int usbnet_get_link_ksettings(struct net_device *net,
946 struct ethtool_link_ksettings *cmd)
948 struct usbnet *dev = netdev_priv(net);
950 if (!dev->mii.mdio_read)
953 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
957 EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
959 int usbnet_set_link_ksettings(struct net_device *net,
960 const struct ethtool_link_ksettings *cmd)
962 struct usbnet *dev = netdev_priv(net);
965 if (!dev->mii.mdio_write)
968 retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
970 /* link speed/duplex might have changed */
971 if (dev->driver_info->link_reset)
972 dev->driver_info->link_reset(dev);
974 /* hard_mtu or rx_urb_size may change in link_reset() */
975 usbnet_update_max_qlen(dev);
979 EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
981 void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
983 struct usbnet *dev = netdev_priv(net);
985 netdev_stats_to_stats64(stats, &net->stats);
986 dev_fetch_sw_netstats(stats, dev->stats64);
988 EXPORT_SYMBOL_GPL(usbnet_get_stats64);
990 u32 usbnet_get_link (struct net_device *net)
992 struct usbnet *dev = netdev_priv(net);
994 /* If a check_connect is defined, return its result */
995 if (dev->driver_info->check_connect)
996 return dev->driver_info->check_connect (dev) == 0;
998 /* if the device has mii operations, use those */
999 if (dev->mii.mdio_read)
1000 return mii_link_ok(&dev->mii);
1002 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
1003 return ethtool_op_get_link(net);
1005 EXPORT_SYMBOL_GPL(usbnet_get_link);
1007 int usbnet_nway_reset(struct net_device *net)
1009 struct usbnet *dev = netdev_priv(net);
1011 if (!dev->mii.mdio_write)
1014 return mii_nway_restart(&dev->mii);
1016 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1018 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1020 struct usbnet *dev = netdev_priv(net);
1022 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1023 strlcpy (info->fw_version, dev->driver_info->description,
1024 sizeof info->fw_version);
1025 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1027 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1029 u32 usbnet_get_msglevel (struct net_device *net)
1031 struct usbnet *dev = netdev_priv(net);
1033 return dev->msg_enable;
1035 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1037 void usbnet_set_msglevel (struct net_device *net, u32 level)
1039 struct usbnet *dev = netdev_priv(net);
1041 dev->msg_enable = level;
1043 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1045 /* drivers may override default ethtool_ops in their bind() routine */
1046 static const struct ethtool_ops usbnet_ethtool_ops = {
1047 .get_link = usbnet_get_link,
1048 .nway_reset = usbnet_nway_reset,
1049 .get_drvinfo = usbnet_get_drvinfo,
1050 .get_msglevel = usbnet_get_msglevel,
1051 .set_msglevel = usbnet_set_msglevel,
1052 .get_ts_info = ethtool_op_get_ts_info,
1053 .get_link_ksettings = usbnet_get_link_ksettings,
1054 .set_link_ksettings = usbnet_set_link_ksettings,
1057 /*-------------------------------------------------------------------------*/
1059 static void __handle_link_change(struct usbnet *dev)
1061 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1064 if (!netif_carrier_ok(dev->net)) {
1065 /* kill URBs for reading packets to save bus bandwidth */
1066 unlink_urbs(dev, &dev->rxq);
1069 * tx_timeout will unlink URBs for sending packets and
1070 * tx queue is stopped by netcore after link becomes off
1073 /* submitting URBs for reading packets */
1074 tasklet_schedule(&dev->bh);
1077 /* hard_mtu or rx_urb_size may change during link change */
1078 usbnet_update_max_qlen(dev);
1080 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1083 void usbnet_set_rx_mode(struct net_device *net)
1085 struct usbnet *dev = netdev_priv(net);
1087 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
1089 EXPORT_SYMBOL_GPL(usbnet_set_rx_mode);
1091 static void __handle_set_rx_mode(struct usbnet *dev)
1093 if (dev->driver_info->set_rx_mode)
1094 (dev->driver_info->set_rx_mode)(dev);
1096 clear_bit(EVENT_SET_RX_MODE, &dev->flags);
1099 /* work that cannot be done in interrupt context uses keventd.
1101 * NOTE: with 2.5 we could do more of this using completion callbacks,
1102 * especially now that control transfers can be queued.
1105 usbnet_deferred_kevent (struct work_struct *work)
1107 struct usbnet *dev =
1108 container_of(work, struct usbnet, kevent);
1111 /* usb_clear_halt() needs a thread context */
1112 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1113 unlink_urbs (dev, &dev->txq);
1114 status = usb_autopm_get_interface(dev->intf);
1117 status = usb_clear_halt (dev->udev, dev->out);
1118 usb_autopm_put_interface(dev->intf);
1121 status != -ESHUTDOWN) {
1122 if (netif_msg_tx_err (dev))
1124 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1127 clear_bit (EVENT_TX_HALT, &dev->flags);
1128 if (status != -ESHUTDOWN)
1129 netif_wake_queue (dev->net);
1132 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1133 unlink_urbs (dev, &dev->rxq);
1134 status = usb_autopm_get_interface(dev->intf);
1137 status = usb_clear_halt (dev->udev, dev->in);
1138 usb_autopm_put_interface(dev->intf);
1141 status != -ESHUTDOWN) {
1142 if (netif_msg_rx_err (dev))
1144 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1147 clear_bit (EVENT_RX_HALT, &dev->flags);
1148 tasklet_schedule (&dev->bh);
1152 /* tasklet could resubmit itself forever if memory is tight */
1153 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1154 struct urb *urb = NULL;
1157 if (netif_running (dev->net))
1158 urb = usb_alloc_urb (0, GFP_KERNEL);
1160 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1162 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1163 status = usb_autopm_get_interface(dev->intf);
1168 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1170 usb_autopm_put_interface(dev->intf);
1173 tasklet_schedule (&dev->bh);
1177 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1178 const struct driver_info *info = dev->driver_info;
1181 clear_bit (EVENT_LINK_RESET, &dev->flags);
1182 status = usb_autopm_get_interface(dev->intf);
1185 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1186 usb_autopm_put_interface(dev->intf);
1188 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1190 dev->udev->bus->bus_name,
1194 usb_autopm_put_interface(dev->intf);
1197 /* handle link change from link resetting */
1198 __handle_link_change(dev);
1201 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1202 __handle_link_change(dev);
1204 if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
1205 __handle_set_rx_mode(dev);
1209 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1212 /*-------------------------------------------------------------------------*/
1214 static void tx_complete (struct urb *urb)
1216 struct sk_buff *skb = (struct sk_buff *) urb->context;
1217 struct skb_data *entry = (struct skb_data *) skb->cb;
1218 struct usbnet *dev = entry->dev;
1220 if (urb->status == 0) {
1221 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
1222 unsigned long flags;
1224 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
1225 stats64->tx_packets += entry->packets;
1226 stats64->tx_bytes += entry->length;
1227 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
1229 dev->net->stats.tx_errors++;
1231 switch (urb->status) {
1233 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1236 /* software-driven interface shutdown */
1237 case -ECONNRESET: // async unlink
1238 case -ESHUTDOWN: // hardware gone
1241 /* like rx, tx gets controller i/o faults during hub_wq
1242 * delays and so it uses the same throttling mechanism.
1247 usb_mark_last_busy(dev->udev);
1248 if (!timer_pending (&dev->delay)) {
1249 mod_timer (&dev->delay,
1250 jiffies + THROTTLE_JIFFIES);
1251 netif_dbg(dev, link, dev->net,
1252 "tx throttle %d\n", urb->status);
1254 netif_stop_queue (dev->net);
1257 netif_dbg(dev, tx_err, dev->net,
1258 "tx err %d\n", entry->urb->status);
1263 usb_autopm_put_interface_async(dev->intf);
1264 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1267 /*-------------------------------------------------------------------------*/
1269 void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue)
1271 struct usbnet *dev = netdev_priv(net);
1273 unlink_urbs (dev, &dev->txq);
1274 tasklet_schedule (&dev->bh);
1275 /* this needs to be handled individually because the generic layer
1276 * doesn't know what is sufficient and could not restore private
1277 * information if a remedy of an unconditional reset were used.
1279 if (dev->driver_info->recover)
1280 (dev->driver_info->recover)(dev);
1282 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1284 /*-------------------------------------------------------------------------*/
1286 static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1288 unsigned num_sgs, total_len = 0;
1291 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1295 /* reserve one for zero packet */
1296 urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
1301 urb->num_sgs = num_sgs;
1302 sg_init_table(urb->sg, urb->num_sgs + 1);
1304 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1305 total_len += skb_headlen(skb);
1307 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1308 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1310 total_len += skb_frag_size(f);
1311 sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f),
1314 urb->transfer_buffer_length = total_len;
1319 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1320 struct net_device *net)
1322 struct usbnet *dev = netdev_priv(net);
1323 unsigned int length;
1324 struct urb *urb = NULL;
1325 struct skb_data *entry;
1326 const struct driver_info *info = dev->driver_info;
1327 unsigned long flags;
1331 skb_tx_timestamp(skb);
1333 // some devices want funky USB-level framing, for
1334 // win32 driver (usually) and/or hardware quirks
1335 if (info->tx_fixup) {
1336 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1338 /* packet collected; minidriver waiting for more */
1339 if (info->flags & FLAG_MULTI_PACKET)
1341 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1346 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1347 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1351 entry = (struct skb_data *) skb->cb;
1355 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1356 skb->data, skb->len, tx_complete, skb);
1357 if (dev->can_dma_sg) {
1358 if (build_dma_sg(skb, urb) < 0)
1361 length = urb->transfer_buffer_length;
1363 /* don't assume the hardware handles USB_ZERO_PACKET
1364 * NOTE: strictly conforming cdc-ether devices should expect
1365 * the ZLP here, but ignore the one-byte packet.
1366 * NOTE2: CDC NCM specification is different from CDC ECM when
1367 * handling ZLP/short packets, so cdc_ncm driver will make short
1368 * packet itself if needed.
1370 if (length % dev->maxpacket == 0) {
1371 if (!(info->flags & FLAG_SEND_ZLP)) {
1372 if (!(info->flags & FLAG_MULTI_PACKET)) {
1374 if (skb_tailroom(skb) && !urb->num_sgs) {
1375 skb->data[skb->len] = 0;
1377 } else if (urb->num_sgs)
1378 sg_set_buf(&urb->sg[urb->num_sgs++],
1379 dev->padding_pkt, 1);
1382 urb->transfer_flags |= URB_ZERO_PACKET;
1384 urb->transfer_buffer_length = length;
1386 if (info->flags & FLAG_MULTI_PACKET) {
1387 /* Driver has set number of packets and a length delta.
1388 * Calculate the complete length and ensure that it's
1391 entry->length += length;
1392 if (WARN_ON_ONCE(entry->length <= 0))
1393 entry->length = length;
1395 usbnet_set_skb_tx_stats(skb, 1, length);
1398 spin_lock_irqsave(&dev->txq.lock, flags);
1399 retval = usb_autopm_get_interface_async(dev->intf);
1401 spin_unlock_irqrestore(&dev->txq.lock, flags);
1404 if (netif_queue_stopped(net)) {
1405 usb_autopm_put_interface_async(dev->intf);
1406 spin_unlock_irqrestore(&dev->txq.lock, flags);
1411 /* if this triggers the device is still a sleep */
1412 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1413 /* transmission will be done in resume */
1414 usb_anchor_urb(urb, &dev->deferred);
1415 /* no use to process more packets */
1416 netif_stop_queue(net);
1418 spin_unlock_irqrestore(&dev->txq.lock, flags);
1419 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1424 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1426 netif_stop_queue (net);
1427 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1428 usb_autopm_put_interface_async(dev->intf);
1431 usb_autopm_put_interface_async(dev->intf);
1432 netif_dbg(dev, tx_err, dev->net,
1433 "tx: submit urb err %d\n", retval);
1436 netif_trans_update(net);
1437 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1438 if (dev->txq.qlen >= TX_QLEN (dev))
1439 netif_stop_queue (net);
1441 spin_unlock_irqrestore (&dev->txq.lock, flags);
1444 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1446 dev->net->stats.tx_dropped++;
1449 dev_kfree_skb_any (skb);
1455 netif_dbg(dev, tx_queued, dev->net,
1456 "> tx, len %u, type 0x%x\n", length, skb->protocol);
1460 return NETDEV_TX_OK;
1462 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1464 static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
1470 /* don't refill the queue all at once */
1471 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1472 urb = usb_alloc_urb(0, flags);
1474 ret = rx_submit(dev, urb, flags);
1486 /*-------------------------------------------------------------------------*/
1488 // tasklet (work deferred from completions, in_irq) or timer
1490 static void usbnet_bh (struct timer_list *t)
1492 struct usbnet *dev = from_timer(dev, t, delay);
1493 struct sk_buff *skb;
1494 struct skb_data *entry;
1496 while ((skb = skb_dequeue (&dev->done))) {
1497 entry = (struct skb_data *) skb->cb;
1498 switch (entry->state) {
1500 entry->state = rx_cleanup;
1501 rx_process (dev, skb);
1504 kfree(entry->urb->sg);
1507 usb_free_urb (entry->urb);
1508 dev_kfree_skb (skb);
1511 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1515 /* restart RX again after disabling due to high error rate */
1516 clear_bit(EVENT_RX_KILL, &dev->flags);
1518 /* waiting for all pending urbs to complete?
1519 * only then can we forgo submitting anew
1521 if (waitqueue_active(&dev->wait)) {
1522 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1523 wake_up_all(&dev->wait);
1525 // or are we maybe short a few urbs?
1526 } else if (netif_running (dev->net) &&
1527 netif_device_present (dev->net) &&
1528 netif_carrier_ok(dev->net) &&
1529 !timer_pending(&dev->delay) &&
1530 !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
1531 !test_bit(EVENT_RX_HALT, &dev->flags)) {
1532 int temp = dev->rxq.qlen;
1534 if (temp < RX_QLEN(dev)) {
1535 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1537 if (temp != dev->rxq.qlen)
1538 netif_dbg(dev, link, dev->net,
1539 "rxqlen %d --> %d\n",
1540 temp, dev->rxq.qlen);
1541 if (dev->rxq.qlen < RX_QLEN(dev))
1542 tasklet_schedule (&dev->bh);
1544 if (dev->txq.qlen < TX_QLEN (dev))
1545 netif_wake_queue (dev->net);
1549 static void usbnet_bh_tasklet(unsigned long data)
1551 struct timer_list *t = (struct timer_list *)data;
1557 /*-------------------------------------------------------------------------
1559 * USB Device Driver support
1561 *-------------------------------------------------------------------------*/
1563 // precondition: never called in_interrupt
1565 void usbnet_disconnect (struct usb_interface *intf)
1568 struct usb_device *xdev;
1569 struct net_device *net;
1572 dev = usb_get_intfdata(intf);
1573 usb_set_intfdata(intf, NULL);
1577 xdev = interface_to_usbdev (intf);
1579 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1580 intf->dev.driver->name,
1581 xdev->bus->bus_name, xdev->devpath,
1582 dev->driver_info->description);
1585 unregister_netdev (net);
1587 while ((urb = usb_get_from_anchor(&dev->deferred))) {
1588 dev_kfree_skb(urb->context);
1593 if (dev->driver_info->unbind)
1594 dev->driver_info->unbind (dev, intf);
1596 usb_kill_urb(dev->interrupt);
1597 usb_free_urb(dev->interrupt);
1598 kfree(dev->padding_pkt);
1600 free_percpu(dev->stats64);
1603 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1605 static const struct net_device_ops usbnet_netdev_ops = {
1606 .ndo_open = usbnet_open,
1607 .ndo_stop = usbnet_stop,
1608 .ndo_start_xmit = usbnet_start_xmit,
1609 .ndo_tx_timeout = usbnet_tx_timeout,
1610 .ndo_set_rx_mode = usbnet_set_rx_mode,
1611 .ndo_change_mtu = usbnet_change_mtu,
1612 .ndo_get_stats64 = usbnet_get_stats64,
1613 .ndo_set_mac_address = eth_mac_addr,
1614 .ndo_validate_addr = eth_validate_addr,
1617 /*-------------------------------------------------------------------------*/
1619 // precondition: never called in_interrupt
1621 static struct device_type wlan_type = {
1625 static struct device_type wwan_type = {
1630 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1633 struct net_device *net;
1634 struct usb_host_interface *interface;
1635 const struct driver_info *info;
1636 struct usb_device *xdev;
1639 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1641 /* usbnet already took usb runtime pm, so have to enable the feature
1642 * for usb interface, otherwise usb_autopm_get_interface may return
1643 * failure if RUNTIME_PM is enabled.
1645 if (!driver->supports_autosuspend) {
1646 driver->supports_autosuspend = 1;
1647 pm_runtime_enable(&udev->dev);
1650 name = udev->dev.driver->name;
1651 info = (const struct driver_info *) prod->driver_info;
1653 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1656 xdev = interface_to_usbdev (udev);
1657 interface = udev->cur_altsetting;
1661 // set up our own records
1662 net = alloc_etherdev(sizeof(*dev));
1666 /* netdev_printk() needs this so do it as early as possible */
1667 SET_NETDEV_DEV(net, &udev->dev);
1669 dev = netdev_priv(net);
1672 dev->driver_info = info;
1673 dev->driver_name = name;
1675 dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1679 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1680 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1681 init_waitqueue_head(&dev->wait);
1682 skb_queue_head_init (&dev->rxq);
1683 skb_queue_head_init (&dev->txq);
1684 skb_queue_head_init (&dev->done);
1685 skb_queue_head_init(&dev->rxq_pause);
1686 dev->bh.func = usbnet_bh_tasklet;
1687 dev->bh.data = (unsigned long)&dev->delay;
1688 INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
1689 init_usb_anchor(&dev->deferred);
1690 timer_setup(&dev->delay, usbnet_bh, 0);
1691 mutex_init (&dev->phy_mutex);
1692 mutex_init(&dev->interrupt_mutex);
1693 dev->interrupt_count = 0;
1696 strcpy (net->name, "usb%d");
1697 memcpy (net->dev_addr, node_id, sizeof node_id);
1699 /* rx and tx sides can use different message sizes;
1700 * bind() should set rx_urb_size in that case.
1702 dev->hard_mtu = net->mtu + net->hard_header_len;
1704 net->max_mtu = ETH_MAX_MTU;
1706 net->netdev_ops = &usbnet_netdev_ops;
1707 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1708 net->ethtool_ops = &usbnet_ethtool_ops;
1710 // allow device-specific bind/init procedures
1711 // NOTE net->name still not usable ...
1713 status = info->bind (dev, udev);
1717 // heuristic: "usb%d" for links we know are two-host,
1718 // else "eth%d" when there's reasonable doubt. userspace
1719 // can rename the link if it knows better.
1720 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1721 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1722 (net->dev_addr [0] & 0x02) == 0))
1723 strcpy (net->name, "eth%d");
1724 /* WLAN devices should always be named "wlan%d" */
1725 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1726 strcpy(net->name, "wlan%d");
1727 /* WWAN devices should always be named "wwan%d" */
1728 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1729 strcpy(net->name, "wwan%d");
1731 /* devices that cannot do ARP */
1732 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1733 net->flags |= IFF_NOARP;
1735 /* maybe the remote can't receive an Ethernet MTU */
1736 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1737 net->mtu = dev->hard_mtu - net->hard_header_len;
1738 } else if (!info->in || !info->out)
1739 status = usbnet_get_endpoints (dev, udev);
1742 info->in + USB_DIR_IN, info->out + USB_DIR_OUT, 0
1745 dev->in = usb_rcvbulkpipe (xdev, info->in);
1746 dev->out = usb_sndbulkpipe (xdev, info->out);
1747 if (!(info->flags & FLAG_NO_SETINT))
1748 status = usb_set_interface (xdev,
1749 interface->desc.bInterfaceNumber,
1750 interface->desc.bAlternateSetting);
1754 if (status == 0 && !usb_check_bulk_endpoints(udev, ep_addrs))
1757 if (status >= 0 && dev->status)
1758 status = init_status (dev, udev);
1762 if (!dev->rx_urb_size)
1763 dev->rx_urb_size = dev->hard_mtu;
1764 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1765 if (dev->maxpacket == 0) {
1766 /* that is a broken device */
1771 /* let userspace know we have a random address */
1772 if (ether_addr_equal(net->dev_addr, node_id))
1773 net->addr_assign_type = NET_ADDR_RANDOM;
1775 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1776 SET_NETDEV_DEVTYPE(net, &wlan_type);
1777 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1778 SET_NETDEV_DEVTYPE(net, &wwan_type);
1780 /* initialize max rx_qlen and tx_qlen */
1781 usbnet_update_max_qlen(dev);
1783 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1784 !(info->flags & FLAG_MULTI_PACKET)) {
1785 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
1786 if (!dev->padding_pkt) {
1792 status = register_netdev (net);
1795 netif_info(dev, probe, dev->net,
1796 "register '%s' at usb-%s-%s, %s, %pM\n",
1797 udev->dev.driver->name,
1798 xdev->bus->bus_name, xdev->devpath,
1799 dev->driver_info->description,
1802 // ok, it's ready to go.
1803 usb_set_intfdata (udev, dev);
1805 netif_device_attach (net);
1807 if (dev->driver_info->flags & FLAG_LINK_INTR)
1808 usbnet_link_change(dev, 0, 0);
1813 kfree(dev->padding_pkt);
1815 usb_free_urb(dev->interrupt);
1818 info->unbind (dev, udev);
1820 /* subdrivers must undo all they did in bind() if they
1821 * fail it, but we may fail later and a deferred kevent
1822 * may trigger an error resubmitting itself and, worse,
1823 * schedule a timer. So we kill it all just in case.
1825 cancel_work_sync(&dev->kevent);
1826 del_timer_sync(&dev->delay);
1827 free_percpu(dev->stats64);
1833 EXPORT_SYMBOL_GPL(usbnet_probe);
1835 /*-------------------------------------------------------------------------*/
1838 * suspend the whole driver as soon as the first interface is suspended
1839 * resume only when the last interface is resumed
1842 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1844 struct usbnet *dev = usb_get_intfdata(intf);
1846 if (!dev->suspend_count++) {
1847 spin_lock_irq(&dev->txq.lock);
1848 /* don't autosuspend while transmitting */
1849 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
1850 dev->suspend_count--;
1851 spin_unlock_irq(&dev->txq.lock);
1854 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1855 spin_unlock_irq(&dev->txq.lock);
1858 * accelerate emptying of the rx and queues, to avoid
1859 * having everything error out.
1861 netif_device_detach (dev->net);
1862 usbnet_terminate_urbs(dev);
1863 __usbnet_status_stop_force(dev);
1866 * reattach so runtime management can use and
1869 netif_device_attach (dev->net);
1873 EXPORT_SYMBOL_GPL(usbnet_suspend);
1875 int usbnet_resume (struct usb_interface *intf)
1877 struct usbnet *dev = usb_get_intfdata(intf);
1878 struct sk_buff *skb;
1882 if (!--dev->suspend_count) {
1883 /* resume interrupt URB if it was previously submitted */
1884 __usbnet_status_start_force(dev, GFP_NOIO);
1886 spin_lock_irq(&dev->txq.lock);
1887 while ((res = usb_get_from_anchor(&dev->deferred))) {
1889 skb = (struct sk_buff *)res->context;
1890 retval = usb_submit_urb(res, GFP_ATOMIC);
1892 dev_kfree_skb_any(skb);
1895 usb_autopm_put_interface_async(dev->intf);
1897 netif_trans_update(dev->net);
1898 __skb_queue_tail(&dev->txq, skb);
1903 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1904 spin_unlock_irq(&dev->txq.lock);
1906 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1907 /* handle remote wakeup ASAP
1908 * we cannot race against stop
1910 if (netif_device_present(dev->net) &&
1911 !timer_pending(&dev->delay) &&
1912 !test_bit(EVENT_RX_HALT, &dev->flags))
1913 rx_alloc_submit(dev, GFP_NOIO);
1915 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1916 netif_tx_wake_all_queues(dev->net);
1917 tasklet_schedule (&dev->bh);
1921 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1922 usb_autopm_get_interface_no_resume(intf);
1926 EXPORT_SYMBOL_GPL(usbnet_resume);
1929 * Either a subdriver implements manage_power, then it is assumed to always
1930 * be ready to be suspended or it reports the readiness to be suspended
1933 void usbnet_device_suggests_idle(struct usbnet *dev)
1935 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1936 dev->intf->needs_remote_wakeup = 1;
1937 usb_autopm_put_interface_async(dev->intf);
1940 EXPORT_SYMBOL(usbnet_device_suggests_idle);
1943 * For devices that can do without special commands
1945 int usbnet_manage_power(struct usbnet *dev, int on)
1947 dev->intf->needs_remote_wakeup = on;
1950 EXPORT_SYMBOL(usbnet_manage_power);
1952 void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1954 /* update link after link is reseted */
1955 if (link && !need_reset)
1956 netif_carrier_on(dev->net);
1958 netif_carrier_off(dev->net);
1960 if (need_reset && link)
1961 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
1963 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1965 EXPORT_SYMBOL(usbnet_link_change);
1967 /*-------------------------------------------------------------------------*/
1968 static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1969 u16 value, u16 index, void *data, u16 size)
1974 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1975 " value=0x%04x index=0x%04x size=%d\n",
1976 cmd, reqtype, value, index, size);
1979 buf = kmalloc(size, GFP_NOIO);
1984 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1985 cmd, reqtype, value, index, buf, size,
1986 USB_CTRL_GET_TIMEOUT);
1987 if (err > 0 && err <= size) {
1989 memcpy(data, buf, err);
1991 netdev_dbg(dev->net,
1992 "Huh? Data requested but thrown away.\n");
1999 static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2000 u16 value, u16 index, const void *data,
2006 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2007 " value=0x%04x index=0x%04x size=%d\n",
2008 cmd, reqtype, value, index, size);
2011 buf = kmemdup(data, size, GFP_NOIO);
2022 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2023 cmd, reqtype, value, index, buf, size,
2024 USB_CTRL_SET_TIMEOUT);
2032 * The function can't be called inside suspend/resume callback,
2033 * otherwise deadlock will be caused.
2035 int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2036 u16 value, u16 index, void *data, u16 size)
2040 if (usb_autopm_get_interface(dev->intf) < 0)
2042 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2044 usb_autopm_put_interface(dev->intf);
2047 EXPORT_SYMBOL_GPL(usbnet_read_cmd);
2050 * The function can't be called inside suspend/resume callback,
2051 * otherwise deadlock will be caused.
2053 int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2054 u16 value, u16 index, const void *data, u16 size)
2058 if (usb_autopm_get_interface(dev->intf) < 0)
2060 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2062 usb_autopm_put_interface(dev->intf);
2065 EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2068 * The function can be called inside suspend/resume callback safely
2069 * and should only be called by suspend/resume callback generally.
2071 int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2072 u16 value, u16 index, void *data, u16 size)
2074 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2077 EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
2080 * The function can be called inside suspend/resume callback safely
2081 * and should only be called by suspend/resume callback generally.
2083 int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2084 u16 value, u16 index, const void *data,
2087 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2090 EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
2092 static void usbnet_async_cmd_cb(struct urb *urb)
2094 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2095 int status = urb->status;
2098 dev_dbg(&urb->dev->dev, "%s failed with %d",
2106 * The caller must make sure that device can't be put into suspend
2107 * state until the control URB completes.
2109 int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2110 u16 value, u16 index, const void *data, u16 size)
2112 struct usb_ctrlrequest *req;
2117 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2118 " value=0x%04x index=0x%04x size=%d\n",
2119 cmd, reqtype, value, index, size);
2121 urb = usb_alloc_urb(0, GFP_ATOMIC);
2126 buf = kmemdup(data, size, GFP_ATOMIC);
2128 netdev_err(dev->net, "Error allocating buffer"
2129 " in %s!\n", __func__);
2134 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
2138 req->bRequestType = reqtype;
2139 req->bRequest = cmd;
2140 req->wValue = cpu_to_le16(value);
2141 req->wIndex = cpu_to_le16(index);
2142 req->wLength = cpu_to_le16(size);
2144 usb_fill_control_urb(urb, dev->udev,
2145 usb_sndctrlpipe(dev->udev, 0),
2146 (void *)req, buf, size,
2147 usbnet_async_cmd_cb, req);
2148 urb->transfer_flags |= URB_FREE_BUFFER;
2150 err = usb_submit_urb(urb, GFP_ATOMIC);
2152 netdev_err(dev->net, "Error submitting the control"
2153 " message: status=%d\n", err);
2163 * avoid a double free
2164 * needed because the flag can be set only
2165 * after filling the URB
2167 urb->transfer_flags = 0;
2174 EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2175 /*-------------------------------------------------------------------------*/
2177 static int __init usbnet_init(void)
2179 /* Compiler should optimize this out. */
2181 sizeof_field(struct sk_buff, cb) < sizeof(struct skb_data));
2183 eth_random_addr(node_id);
2186 module_init(usbnet_init);
2188 static void __exit usbnet_exit(void)
2191 module_exit(usbnet_exit);
2193 MODULE_AUTHOR("David Brownell");
2194 MODULE_DESCRIPTION("USB network driver framework");
2195 MODULE_LICENSE("GPL");