lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / net / usb / usbnet.c
blob72514c46b4786ae26ba6d16a2c6d32b9a2cfb078
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * USB Network driver infrastructure
4 * Copyright (C) 2000-2005 by David Brownell
5 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
6 */
8 /*
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 #define DRIVER_VERSION "22-Aug-2005"
40 /*-------------------------------------------------------------------------*/
43 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
44 * Several dozen bytes of IPv4 data can fit in two such transactions.
45 * One maximum size Ethernet packet takes twenty four of them.
46 * For high speed, each frame comfortably fits almost 36 max size
47 * Ethernet packets (so queues should be bigger).
49 * The goal is to let the USB host controller be busy for 5msec or
50 * more before an irq is required, under load. Jumbograms change
51 * the equation.
53 #define MAX_QUEUE_MEMORY (60 * 1518)
54 #define RX_QLEN(dev) ((dev)->rx_qlen)
55 #define TX_QLEN(dev) ((dev)->tx_qlen)
57 // reawaken network queue this soon after stopping; else watchdog barks
58 #define TX_TIMEOUT_JIFFIES (5*HZ)
60 /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
61 * us (it polls at HZ/4 usually) before we report too many false errors.
63 #define THROTTLE_JIFFIES (HZ/8)
65 // between wakeups
66 #define UNLINK_TIMEOUT_MS 3
68 /*-------------------------------------------------------------------------*/
70 // randomly generated ethernet address
71 static u8 node_id [ETH_ALEN];
73 /* use ethtool to change the level for any given device */
74 static int msg_level = -1;
75 module_param (msg_level, int, 0);
76 MODULE_PARM_DESC (msg_level, "Override default message level");
78 /*-------------------------------------------------------------------------*/
80 /* handles CDC Ethernet and many other network "bulk data" interfaces */
81 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
83 int tmp;
84 struct usb_host_interface *alt = NULL;
85 struct usb_host_endpoint *in = NULL, *out = NULL;
86 struct usb_host_endpoint *status = NULL;
88 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
89 unsigned ep;
91 in = out = status = NULL;
92 alt = intf->altsetting + tmp;
94 /* take the first altsetting with in-bulk + out-bulk;
95 * remember any status endpoint, just in case;
96 * ignore other endpoints and altsettings.
98 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
99 struct usb_host_endpoint *e;
100 int intr = 0;
102 e = alt->endpoint + ep;
103 switch (e->desc.bmAttributes) {
104 case USB_ENDPOINT_XFER_INT:
105 if (!usb_endpoint_dir_in(&e->desc))
106 continue;
107 intr = 1;
108 /* FALLTHROUGH */
109 case USB_ENDPOINT_XFER_BULK:
110 break;
111 default:
112 continue;
114 if (usb_endpoint_dir_in(&e->desc)) {
115 if (!intr && !in)
116 in = e;
117 else if (intr && !status)
118 status = e;
119 } else {
120 if (!out)
121 out = e;
124 if (in && out)
125 break;
127 if (!alt || !in || !out)
128 return -EINVAL;
130 if (alt->desc.bAlternateSetting != 0 ||
131 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
132 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
133 alt->desc.bAlternateSetting);
134 if (tmp < 0)
135 return tmp;
138 dev->in = usb_rcvbulkpipe (dev->udev,
139 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
140 dev->out = usb_sndbulkpipe (dev->udev,
141 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
142 dev->status = status;
143 return 0;
145 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
147 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
149 int tmp = -1, ret;
150 unsigned char buf [13];
152 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
153 if (ret == 12)
154 tmp = hex2bin(dev->net->dev_addr, buf, 6);
155 if (tmp < 0) {
156 dev_dbg(&dev->udev->dev,
157 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
158 if (ret >= 0)
159 ret = -EINVAL;
160 return ret;
162 return 0;
164 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
166 static void intr_complete (struct urb *urb)
168 struct usbnet *dev = urb->context;
169 int status = urb->status;
171 switch (status) {
172 /* success */
173 case 0:
174 dev->driver_info->status(dev, urb);
175 break;
177 /* software-driven interface shutdown */
178 case -ENOENT: /* urb killed */
179 case -ESHUTDOWN: /* hardware gone */
180 netif_dbg(dev, ifdown, dev->net,
181 "intr shutdown, code %d\n", status);
182 return;
184 /* NOTE: not throttling like RX/TX, since this endpoint
185 * already polls infrequently
187 default:
188 netdev_dbg(dev->net, "intr status %d\n", status);
189 break;
192 status = usb_submit_urb (urb, GFP_ATOMIC);
193 if (status != 0)
194 netif_err(dev, timer, dev->net,
195 "intr resubmit --> %d\n", status);
198 static int init_status (struct usbnet *dev, struct usb_interface *intf)
200 char *buf = NULL;
201 unsigned pipe = 0;
202 unsigned maxp;
203 unsigned period;
205 if (!dev->driver_info->status)
206 return 0;
208 pipe = usb_rcvintpipe (dev->udev,
209 dev->status->desc.bEndpointAddress
210 & USB_ENDPOINT_NUMBER_MASK);
211 maxp = usb_maxpacket (dev->udev, pipe, 0);
213 /* avoid 1 msec chatter: min 8 msec poll rate */
214 period = max ((int) dev->status->desc.bInterval,
215 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
217 buf = kmalloc (maxp, GFP_KERNEL);
218 if (buf) {
219 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
220 if (!dev->interrupt) {
221 kfree (buf);
222 return -ENOMEM;
223 } else {
224 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
225 buf, maxp, intr_complete, dev, period);
226 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
227 dev_dbg(&intf->dev,
228 "status ep%din, %d bytes period %d\n",
229 usb_pipeendpoint(pipe), maxp, period);
232 return 0;
235 /* Submit the interrupt URB if not previously submitted, increasing refcount */
236 int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
238 int ret = 0;
240 WARN_ON_ONCE(dev->interrupt == NULL);
241 if (dev->interrupt) {
242 mutex_lock(&dev->interrupt_mutex);
244 if (++dev->interrupt_count == 1)
245 ret = usb_submit_urb(dev->interrupt, mem_flags);
247 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
248 dev->interrupt_count);
249 mutex_unlock(&dev->interrupt_mutex);
251 return ret;
253 EXPORT_SYMBOL_GPL(usbnet_status_start);
255 /* For resume; submit interrupt URB if previously submitted */
256 static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
258 int ret = 0;
260 mutex_lock(&dev->interrupt_mutex);
261 if (dev->interrupt_count) {
262 ret = usb_submit_urb(dev->interrupt, mem_flags);
263 dev_dbg(&dev->udev->dev,
264 "submitted interrupt URB for resume\n");
266 mutex_unlock(&dev->interrupt_mutex);
267 return ret;
270 /* Kill the interrupt URB if all submitters want it killed */
271 void usbnet_status_stop(struct usbnet *dev)
273 if (dev->interrupt) {
274 mutex_lock(&dev->interrupt_mutex);
275 WARN_ON(dev->interrupt_count == 0);
277 if (dev->interrupt_count && --dev->interrupt_count == 0)
278 usb_kill_urb(dev->interrupt);
280 dev_dbg(&dev->udev->dev,
281 "decremented interrupt URB count to %d\n",
282 dev->interrupt_count);
283 mutex_unlock(&dev->interrupt_mutex);
286 EXPORT_SYMBOL_GPL(usbnet_status_stop);
288 /* For suspend; always kill interrupt URB */
289 static void __usbnet_status_stop_force(struct usbnet *dev)
291 if (dev->interrupt) {
292 mutex_lock(&dev->interrupt_mutex);
293 usb_kill_urb(dev->interrupt);
294 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
295 mutex_unlock(&dev->interrupt_mutex);
299 /* Passes this packet up the stack, updating its accounting.
300 * Some link protocols batch packets, so their rx_fixup paths
301 * can return clones as well as just modify the original skb.
303 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
305 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
306 unsigned long flags;
307 int status;
309 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
310 skb_queue_tail(&dev->rxq_pause, skb);
311 return;
314 /* only update if unset to allow minidriver rx_fixup override */
315 if (skb->protocol == 0)
316 skb->protocol = eth_type_trans (skb, dev->net);
318 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
319 stats64->rx_packets++;
320 stats64->rx_bytes += skb->len;
321 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
323 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
324 skb->len + sizeof (struct ethhdr), skb->protocol);
325 memset (skb->cb, 0, sizeof (struct skb_data));
327 if (skb_defer_rx_timestamp(skb))
328 return;
330 status = netif_rx (skb);
331 if (status != NET_RX_SUCCESS)
332 netif_dbg(dev, rx_err, dev->net,
333 "netif_rx status %d\n", status);
335 EXPORT_SYMBOL_GPL(usbnet_skb_return);
337 /* must be called if hard_mtu or rx_urb_size changed */
338 void usbnet_update_max_qlen(struct usbnet *dev)
340 enum usb_device_speed speed = dev->udev->speed;
342 switch (speed) {
343 case USB_SPEED_HIGH:
344 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
345 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
346 break;
347 case USB_SPEED_SUPER:
348 case USB_SPEED_SUPER_PLUS:
350 * Not take default 5ms qlen for super speed HC to
351 * save memory, and iperf tests show 2.5ms qlen can
352 * work well
354 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
355 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
356 break;
357 default:
358 dev->rx_qlen = dev->tx_qlen = 4;
361 EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
364 /*-------------------------------------------------------------------------
366 * Network Device Driver (peer link to "Host Device", from USB host)
368 *-------------------------------------------------------------------------*/
370 int usbnet_change_mtu (struct net_device *net, int new_mtu)
372 struct usbnet *dev = netdev_priv(net);
373 int ll_mtu = new_mtu + net->hard_header_len;
374 int old_hard_mtu = dev->hard_mtu;
375 int old_rx_urb_size = dev->rx_urb_size;
377 // no second zero-length packet read wanted after mtu-sized packets
378 if ((ll_mtu % dev->maxpacket) == 0)
379 return -EDOM;
380 net->mtu = new_mtu;
382 dev->hard_mtu = net->mtu + net->hard_header_len;
383 if (dev->rx_urb_size == old_hard_mtu) {
384 dev->rx_urb_size = dev->hard_mtu;
385 if (dev->rx_urb_size > old_rx_urb_size) {
386 usbnet_pause_rx(dev);
387 usbnet_unlink_rx_urbs(dev);
388 usbnet_resume_rx(dev);
392 /* max qlen depend on hard_mtu and rx_urb_size */
393 usbnet_update_max_qlen(dev);
395 return 0;
397 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
399 /* The caller must hold list->lock */
400 static void __usbnet_queue_skb(struct sk_buff_head *list,
401 struct sk_buff *newsk, enum skb_state state)
403 struct skb_data *entry = (struct skb_data *) newsk->cb;
405 __skb_queue_tail(list, newsk);
406 entry->state = state;
409 /*-------------------------------------------------------------------------*/
411 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
412 * completion callbacks. 2.5 should have fixed those bugs...
415 static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
416 struct sk_buff_head *list, enum skb_state state)
418 unsigned long flags;
419 enum skb_state old_state;
420 struct skb_data *entry = (struct skb_data *) skb->cb;
422 spin_lock_irqsave(&list->lock, flags);
423 old_state = entry->state;
424 entry->state = state;
425 __skb_unlink(skb, list);
427 /* defer_bh() is never called with list == &dev->done.
428 * spin_lock_nested() tells lockdep that it is OK to take
429 * dev->done.lock here with list->lock held.
431 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
433 __skb_queue_tail(&dev->done, skb);
434 if (dev->done.qlen == 1)
435 tasklet_schedule(&dev->bh);
436 spin_unlock(&dev->done.lock);
437 spin_unlock_irqrestore(&list->lock, flags);
438 return old_state;
441 /* some work can't be done in tasklets, so we use keventd
443 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
444 * but tasklet_schedule() doesn't. hope the failure is rare.
446 void usbnet_defer_kevent (struct usbnet *dev, int work)
448 set_bit (work, &dev->flags);
449 if (!schedule_work (&dev->kevent))
450 netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
451 else
452 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
454 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
456 /*-------------------------------------------------------------------------*/
458 static void rx_complete (struct urb *urb);
460 static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
462 struct sk_buff *skb;
463 struct skb_data *entry;
464 int retval = 0;
465 unsigned long lockflags;
466 size_t size = dev->rx_urb_size;
468 /* prevent rx skb allocation when error ratio is high */
469 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
470 usb_free_urb(urb);
471 return -ENOLINK;
474 if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
475 skb = __netdev_alloc_skb(dev->net, size, flags);
476 else
477 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
478 if (!skb) {
479 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
480 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
481 usb_free_urb (urb);
482 return -ENOMEM;
485 entry = (struct skb_data *) skb->cb;
486 entry->urb = urb;
487 entry->dev = dev;
488 entry->length = 0;
490 usb_fill_bulk_urb (urb, dev->udev, dev->in,
491 skb->data, size, rx_complete, skb);
493 spin_lock_irqsave (&dev->rxq.lock, lockflags);
495 if (netif_running (dev->net) &&
496 netif_device_present (dev->net) &&
497 test_bit(EVENT_DEV_OPEN, &dev->flags) &&
498 !test_bit (EVENT_RX_HALT, &dev->flags) &&
499 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
500 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
501 case -EPIPE:
502 usbnet_defer_kevent (dev, EVENT_RX_HALT);
503 break;
504 case -ENOMEM:
505 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
506 break;
507 case -ENODEV:
508 netif_dbg(dev, ifdown, dev->net, "device gone\n");
509 netif_device_detach (dev->net);
510 break;
511 case -EHOSTUNREACH:
512 retval = -ENOLINK;
513 break;
514 default:
515 netif_dbg(dev, rx_err, dev->net,
516 "rx submit, %d\n", retval);
517 tasklet_schedule (&dev->bh);
518 break;
519 case 0:
520 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
522 } else {
523 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
524 retval = -ENOLINK;
526 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
527 if (retval) {
528 dev_kfree_skb_any (skb);
529 usb_free_urb (urb);
531 return retval;
535 /*-------------------------------------------------------------------------*/
537 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
539 if (dev->driver_info->rx_fixup &&
540 !dev->driver_info->rx_fixup (dev, skb)) {
541 /* With RX_ASSEMBLE, rx_fixup() must update counters */
542 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
543 dev->net->stats.rx_errors++;
544 goto done;
546 // else network stack removes extra byte if we forced a short packet
548 /* all data was already cloned from skb inside the driver */
549 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
550 goto done;
552 if (skb->len < ETH_HLEN) {
553 dev->net->stats.rx_errors++;
554 dev->net->stats.rx_length_errors++;
555 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
556 } else {
557 usbnet_skb_return(dev, skb);
558 return;
561 done:
562 skb_queue_tail(&dev->done, skb);
565 /*-------------------------------------------------------------------------*/
567 static void rx_complete (struct urb *urb)
569 struct sk_buff *skb = (struct sk_buff *) urb->context;
570 struct skb_data *entry = (struct skb_data *) skb->cb;
571 struct usbnet *dev = entry->dev;
572 int urb_status = urb->status;
573 enum skb_state state;
575 skb_put (skb, urb->actual_length);
576 state = rx_done;
577 entry->urb = NULL;
579 switch (urb_status) {
580 /* success */
581 case 0:
582 break;
584 /* stalls need manual reset. this is rare ... except that
585 * when going through USB 2.0 TTs, unplug appears this way.
586 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
587 * storm, recovering as needed.
589 case -EPIPE:
590 dev->net->stats.rx_errors++;
591 usbnet_defer_kevent (dev, EVENT_RX_HALT);
592 // FALLTHROUGH
594 /* software-driven interface shutdown */
595 case -ECONNRESET: /* async unlink */
596 case -ESHUTDOWN: /* hardware gone */
597 netif_dbg(dev, ifdown, dev->net,
598 "rx shutdown, code %d\n", urb_status);
599 goto block;
601 /* we get controller i/o faults during hub_wq disconnect() delays.
602 * throttle down resubmits, to avoid log floods; just temporarily,
603 * so we still recover when the fault isn't a hub_wq delay.
605 case -EPROTO:
606 case -ETIME:
607 case -EILSEQ:
608 dev->net->stats.rx_errors++;
609 if (!timer_pending (&dev->delay)) {
610 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
611 netif_dbg(dev, link, dev->net,
612 "rx throttle %d\n", urb_status);
614 block:
615 state = rx_cleanup;
616 entry->urb = urb;
617 urb = NULL;
618 break;
620 /* data overrun ... flush fifo? */
621 case -EOVERFLOW:
622 dev->net->stats.rx_over_errors++;
623 // FALLTHROUGH
625 default:
626 state = rx_cleanup;
627 dev->net->stats.rx_errors++;
628 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
629 break;
632 /* stop rx if packet error rate is high */
633 if (++dev->pkt_cnt > 30) {
634 dev->pkt_cnt = 0;
635 dev->pkt_err = 0;
636 } else {
637 if (state == rx_cleanup)
638 dev->pkt_err++;
639 if (dev->pkt_err > 20)
640 set_bit(EVENT_RX_KILL, &dev->flags);
643 state = defer_bh(dev, skb, &dev->rxq, state);
645 if (urb) {
646 if (netif_running (dev->net) &&
647 !test_bit (EVENT_RX_HALT, &dev->flags) &&
648 state != unlink_start) {
649 rx_submit (dev, urb, GFP_ATOMIC);
650 usb_mark_last_busy(dev->udev);
651 return;
653 usb_free_urb (urb);
655 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
658 /*-------------------------------------------------------------------------*/
659 void usbnet_pause_rx(struct usbnet *dev)
661 set_bit(EVENT_RX_PAUSED, &dev->flags);
663 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
665 EXPORT_SYMBOL_GPL(usbnet_pause_rx);
667 void usbnet_resume_rx(struct usbnet *dev)
669 struct sk_buff *skb;
670 int num = 0;
672 clear_bit(EVENT_RX_PAUSED, &dev->flags);
674 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
675 usbnet_skb_return(dev, skb);
676 num++;
679 tasklet_schedule(&dev->bh);
681 netif_dbg(dev, rx_status, dev->net,
682 "paused rx queue disabled, %d skbs requeued\n", num);
684 EXPORT_SYMBOL_GPL(usbnet_resume_rx);
686 void usbnet_purge_paused_rxq(struct usbnet *dev)
688 skb_queue_purge(&dev->rxq_pause);
690 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
692 /*-------------------------------------------------------------------------*/
694 // unlink pending rx/tx; completion handlers do all other cleanup
696 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
698 unsigned long flags;
699 struct sk_buff *skb;
700 int count = 0;
702 spin_lock_irqsave (&q->lock, flags);
703 while (!skb_queue_empty(q)) {
704 struct skb_data *entry;
705 struct urb *urb;
706 int retval;
708 skb_queue_walk(q, skb) {
709 entry = (struct skb_data *) skb->cb;
710 if (entry->state != unlink_start)
711 goto found;
713 break;
714 found:
715 entry->state = unlink_start;
716 urb = entry->urb;
719 * Get reference count of the URB to avoid it to be
720 * freed during usb_unlink_urb, which may trigger
721 * use-after-free problem inside usb_unlink_urb since
722 * usb_unlink_urb is always racing with .complete
723 * handler(include defer_bh).
725 usb_get_urb(urb);
726 spin_unlock_irqrestore(&q->lock, flags);
727 // during some PM-driven resume scenarios,
728 // these (async) unlinks complete immediately
729 retval = usb_unlink_urb (urb);
730 if (retval != -EINPROGRESS && retval != 0)
731 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
732 else
733 count++;
734 usb_put_urb(urb);
735 spin_lock_irqsave(&q->lock, flags);
737 spin_unlock_irqrestore (&q->lock, flags);
738 return count;
741 // Flush all pending rx urbs
742 // minidrivers may need to do this when the MTU changes
744 void usbnet_unlink_rx_urbs(struct usbnet *dev)
746 if (netif_running(dev->net)) {
747 (void) unlink_urbs (dev, &dev->rxq);
748 tasklet_schedule(&dev->bh);
751 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
753 /*-------------------------------------------------------------------------*/
755 static void wait_skb_queue_empty(struct sk_buff_head *q)
757 unsigned long flags;
759 spin_lock_irqsave(&q->lock, flags);
760 while (!skb_queue_empty(q)) {
761 spin_unlock_irqrestore(&q->lock, flags);
762 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
763 set_current_state(TASK_UNINTERRUPTIBLE);
764 spin_lock_irqsave(&q->lock, flags);
766 spin_unlock_irqrestore(&q->lock, flags);
769 // precondition: never called in_interrupt
770 static void usbnet_terminate_urbs(struct usbnet *dev)
772 DECLARE_WAITQUEUE(wait, current);
773 int temp;
775 /* ensure there are no more active urbs */
776 add_wait_queue(&dev->wait, &wait);
777 set_current_state(TASK_UNINTERRUPTIBLE);
778 temp = unlink_urbs(dev, &dev->txq) +
779 unlink_urbs(dev, &dev->rxq);
781 /* maybe wait for deletions to finish. */
782 wait_skb_queue_empty(&dev->rxq);
783 wait_skb_queue_empty(&dev->txq);
784 wait_skb_queue_empty(&dev->done);
785 netif_dbg(dev, ifdown, dev->net,
786 "waited for %d urb completions\n", temp);
787 set_current_state(TASK_RUNNING);
788 remove_wait_queue(&dev->wait, &wait);
791 int usbnet_stop (struct net_device *net)
793 struct usbnet *dev = netdev_priv(net);
794 const struct driver_info *info = dev->driver_info;
795 int retval, pm, mpn;
797 clear_bit(EVENT_DEV_OPEN, &dev->flags);
798 netif_stop_queue (net);
800 netif_info(dev, ifdown, dev->net,
801 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
802 net->stats.rx_packets, net->stats.tx_packets,
803 net->stats.rx_errors, net->stats.tx_errors);
805 /* to not race resume */
806 pm = usb_autopm_get_interface(dev->intf);
807 /* allow minidriver to stop correctly (wireless devices to turn off
808 * radio etc) */
809 if (info->stop) {
810 retval = info->stop(dev);
811 if (retval < 0)
812 netif_info(dev, ifdown, dev->net,
813 "stop fail (%d) usbnet usb-%s-%s, %s\n",
814 retval,
815 dev->udev->bus->bus_name, dev->udev->devpath,
816 info->description);
819 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
820 usbnet_terminate_urbs(dev);
822 usbnet_status_stop(dev);
824 usbnet_purge_paused_rxq(dev);
826 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
828 /* deferred work (task, timer, softirq) must also stop.
829 * can't flush_scheduled_work() until we drop rtnl (later),
830 * else workers could deadlock; so make workers a NOP.
832 dev->flags = 0;
833 del_timer_sync (&dev->delay);
834 tasklet_kill (&dev->bh);
835 if (!pm)
836 usb_autopm_put_interface(dev->intf);
838 if (info->manage_power && mpn)
839 info->manage_power(dev, 0);
840 else
841 usb_autopm_put_interface(dev->intf);
843 return 0;
845 EXPORT_SYMBOL_GPL(usbnet_stop);
847 /*-------------------------------------------------------------------------*/
849 // posts reads, and enables write queuing
851 // precondition: never called in_interrupt
853 int usbnet_open (struct net_device *net)
855 struct usbnet *dev = netdev_priv(net);
856 int retval;
857 const struct driver_info *info = dev->driver_info;
859 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
860 netif_info(dev, ifup, dev->net,
861 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
862 retval,
863 dev->udev->bus->bus_name,
864 dev->udev->devpath,
865 info->description);
866 goto done_nopm;
869 // put into "known safe" state
870 if (info->reset && (retval = info->reset (dev)) < 0) {
871 netif_info(dev, ifup, dev->net,
872 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
873 retval,
874 dev->udev->bus->bus_name,
875 dev->udev->devpath,
876 info->description);
877 goto done;
880 /* hard_mtu or rx_urb_size may change in reset() */
881 usbnet_update_max_qlen(dev);
883 // insist peer be connected
884 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
885 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
886 goto done;
889 /* start any status interrupt transfer */
890 if (dev->interrupt) {
891 retval = usbnet_status_start(dev, GFP_KERNEL);
892 if (retval < 0) {
893 netif_err(dev, ifup, dev->net,
894 "intr submit %d\n", retval);
895 goto done;
899 set_bit(EVENT_DEV_OPEN, &dev->flags);
900 netif_start_queue (net);
901 netif_info(dev, ifup, dev->net,
902 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
903 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
904 dev->net->mtu,
905 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
906 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
907 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
908 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
909 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
910 "simple");
912 /* reset rx error state */
913 dev->pkt_cnt = 0;
914 dev->pkt_err = 0;
915 clear_bit(EVENT_RX_KILL, &dev->flags);
917 // delay posting reads until we're fully open
918 tasklet_schedule (&dev->bh);
919 if (info->manage_power) {
920 retval = info->manage_power(dev, 1);
921 if (retval < 0) {
922 retval = 0;
923 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
924 } else {
925 usb_autopm_put_interface(dev->intf);
928 return retval;
929 done:
930 usb_autopm_put_interface(dev->intf);
931 done_nopm:
932 return retval;
934 EXPORT_SYMBOL_GPL(usbnet_open);
936 /*-------------------------------------------------------------------------*/
938 /* ethtool methods; minidrivers may need to add some more, but
939 * they'll probably want to use this base set.
942 int usbnet_get_link_ksettings(struct net_device *net,
943 struct ethtool_link_ksettings *cmd)
945 struct usbnet *dev = netdev_priv(net);
947 if (!dev->mii.mdio_read)
948 return -EOPNOTSUPP;
950 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
952 return 0;
954 EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
956 int usbnet_set_link_ksettings(struct net_device *net,
957 const struct ethtool_link_ksettings *cmd)
959 struct usbnet *dev = netdev_priv(net);
960 int retval;
962 if (!dev->mii.mdio_write)
963 return -EOPNOTSUPP;
965 retval = mii_ethtool_set_link_ksettings(&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);
974 return retval;
976 EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
978 void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
980 struct usbnet *dev = netdev_priv(net);
981 unsigned int start;
982 int cpu;
984 netdev_stats_to_stats64(stats, &net->stats);
986 for_each_possible_cpu(cpu) {
987 struct pcpu_sw_netstats *stats64;
988 u64 rx_packets, rx_bytes;
989 u64 tx_packets, tx_bytes;
991 stats64 = per_cpu_ptr(dev->stats64, cpu);
993 do {
994 start = u64_stats_fetch_begin_irq(&stats64->syncp);
995 rx_packets = stats64->rx_packets;
996 rx_bytes = stats64->rx_bytes;
997 tx_packets = stats64->tx_packets;
998 tx_bytes = stats64->tx_bytes;
999 } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
1001 stats->rx_packets += rx_packets;
1002 stats->rx_bytes += rx_bytes;
1003 stats->tx_packets += tx_packets;
1004 stats->tx_bytes += tx_bytes;
1007 EXPORT_SYMBOL_GPL(usbnet_get_stats64);
1009 u32 usbnet_get_link (struct net_device *net)
1011 struct usbnet *dev = netdev_priv(net);
1013 /* If a check_connect is defined, return its result */
1014 if (dev->driver_info->check_connect)
1015 return dev->driver_info->check_connect (dev) == 0;
1017 /* if the device has mii operations, use those */
1018 if (dev->mii.mdio_read)
1019 return mii_link_ok(&dev->mii);
1021 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
1022 return ethtool_op_get_link(net);
1024 EXPORT_SYMBOL_GPL(usbnet_get_link);
1026 int usbnet_nway_reset(struct net_device *net)
1028 struct usbnet *dev = netdev_priv(net);
1030 if (!dev->mii.mdio_write)
1031 return -EOPNOTSUPP;
1033 return mii_nway_restart(&dev->mii);
1035 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1037 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1039 struct usbnet *dev = netdev_priv(net);
1041 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1042 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
1043 strlcpy (info->fw_version, dev->driver_info->description,
1044 sizeof info->fw_version);
1045 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1047 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1049 u32 usbnet_get_msglevel (struct net_device *net)
1051 struct usbnet *dev = netdev_priv(net);
1053 return dev->msg_enable;
1055 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1057 void usbnet_set_msglevel (struct net_device *net, u32 level)
1059 struct usbnet *dev = netdev_priv(net);
1061 dev->msg_enable = level;
1063 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1065 /* drivers may override default ethtool_ops in their bind() routine */
1066 static const struct ethtool_ops usbnet_ethtool_ops = {
1067 .get_link = usbnet_get_link,
1068 .nway_reset = usbnet_nway_reset,
1069 .get_drvinfo = usbnet_get_drvinfo,
1070 .get_msglevel = usbnet_get_msglevel,
1071 .set_msglevel = usbnet_set_msglevel,
1072 .get_ts_info = ethtool_op_get_ts_info,
1073 .get_link_ksettings = usbnet_get_link_ksettings,
1074 .set_link_ksettings = usbnet_set_link_ksettings,
1077 /*-------------------------------------------------------------------------*/
1079 static void __handle_link_change(struct usbnet *dev)
1081 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1082 return;
1084 if (!netif_carrier_ok(dev->net)) {
1085 /* kill URBs for reading packets to save bus bandwidth */
1086 unlink_urbs(dev, &dev->rxq);
1089 * tx_timeout will unlink URBs for sending packets and
1090 * tx queue is stopped by netcore after link becomes off
1092 } else {
1093 /* submitting URBs for reading packets */
1094 tasklet_schedule(&dev->bh);
1097 /* hard_mtu or rx_urb_size may change during link change */
1098 usbnet_update_max_qlen(dev);
1100 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1103 static void usbnet_set_rx_mode(struct net_device *net)
1105 struct usbnet *dev = netdev_priv(net);
1107 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
1110 static void __handle_set_rx_mode(struct usbnet *dev)
1112 if (dev->driver_info->set_rx_mode)
1113 (dev->driver_info->set_rx_mode)(dev);
1115 clear_bit(EVENT_SET_RX_MODE, &dev->flags);
1118 /* work that cannot be done in interrupt context uses keventd.
1120 * NOTE: with 2.5 we could do more of this using completion callbacks,
1121 * especially now that control transfers can be queued.
1123 static void
1124 usbnet_deferred_kevent (struct work_struct *work)
1126 struct usbnet *dev =
1127 container_of(work, struct usbnet, kevent);
1128 int status;
1130 /* usb_clear_halt() needs a thread context */
1131 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1132 unlink_urbs (dev, &dev->txq);
1133 status = usb_autopm_get_interface(dev->intf);
1134 if (status < 0)
1135 goto fail_pipe;
1136 status = usb_clear_halt (dev->udev, dev->out);
1137 usb_autopm_put_interface(dev->intf);
1138 if (status < 0 &&
1139 status != -EPIPE &&
1140 status != -ESHUTDOWN) {
1141 if (netif_msg_tx_err (dev))
1142 fail_pipe:
1143 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1144 status);
1145 } else {
1146 clear_bit (EVENT_TX_HALT, &dev->flags);
1147 if (status != -ESHUTDOWN)
1148 netif_wake_queue (dev->net);
1151 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1152 unlink_urbs (dev, &dev->rxq);
1153 status = usb_autopm_get_interface(dev->intf);
1154 if (status < 0)
1155 goto fail_halt;
1156 status = usb_clear_halt (dev->udev, dev->in);
1157 usb_autopm_put_interface(dev->intf);
1158 if (status < 0 &&
1159 status != -EPIPE &&
1160 status != -ESHUTDOWN) {
1161 if (netif_msg_rx_err (dev))
1162 fail_halt:
1163 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1164 status);
1165 } else {
1166 clear_bit (EVENT_RX_HALT, &dev->flags);
1167 tasklet_schedule (&dev->bh);
1171 /* tasklet could resubmit itself forever if memory is tight */
1172 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1173 struct urb *urb = NULL;
1174 int resched = 1;
1176 if (netif_running (dev->net))
1177 urb = usb_alloc_urb (0, GFP_KERNEL);
1178 else
1179 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1180 if (urb != NULL) {
1181 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1182 status = usb_autopm_get_interface(dev->intf);
1183 if (status < 0) {
1184 usb_free_urb(urb);
1185 goto fail_lowmem;
1187 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1188 resched = 0;
1189 usb_autopm_put_interface(dev->intf);
1190 fail_lowmem:
1191 if (resched)
1192 tasklet_schedule (&dev->bh);
1196 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1197 const struct driver_info *info = dev->driver_info;
1198 int retval = 0;
1200 clear_bit (EVENT_LINK_RESET, &dev->flags);
1201 status = usb_autopm_get_interface(dev->intf);
1202 if (status < 0)
1203 goto skip_reset;
1204 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1205 usb_autopm_put_interface(dev->intf);
1206 skip_reset:
1207 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1208 retval,
1209 dev->udev->bus->bus_name,
1210 dev->udev->devpath,
1211 info->description);
1212 } else {
1213 usb_autopm_put_interface(dev->intf);
1216 /* handle link change from link resetting */
1217 __handle_link_change(dev);
1220 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1221 __handle_link_change(dev);
1223 if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
1224 __handle_set_rx_mode(dev);
1227 if (dev->flags)
1228 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1231 /*-------------------------------------------------------------------------*/
1233 static void tx_complete (struct urb *urb)
1235 struct sk_buff *skb = (struct sk_buff *) urb->context;
1236 struct skb_data *entry = (struct skb_data *) skb->cb;
1237 struct usbnet *dev = entry->dev;
1239 if (urb->status == 0) {
1240 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
1241 unsigned long flags;
1243 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
1244 stats64->tx_packets += entry->packets;
1245 stats64->tx_bytes += entry->length;
1246 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
1247 } else {
1248 dev->net->stats.tx_errors++;
1250 switch (urb->status) {
1251 case -EPIPE:
1252 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1253 break;
1255 /* software-driven interface shutdown */
1256 case -ECONNRESET: // async unlink
1257 case -ESHUTDOWN: // hardware gone
1258 break;
1260 /* like rx, tx gets controller i/o faults during hub_wq
1261 * delays and so it uses the same throttling mechanism.
1263 case -EPROTO:
1264 case -ETIME:
1265 case -EILSEQ:
1266 usb_mark_last_busy(dev->udev);
1267 if (!timer_pending (&dev->delay)) {
1268 mod_timer (&dev->delay,
1269 jiffies + THROTTLE_JIFFIES);
1270 netif_dbg(dev, link, dev->net,
1271 "tx throttle %d\n", urb->status);
1273 netif_stop_queue (dev->net);
1274 break;
1275 default:
1276 netif_dbg(dev, tx_err, dev->net,
1277 "tx err %d\n", entry->urb->status);
1278 break;
1282 usb_autopm_put_interface_async(dev->intf);
1283 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1286 /*-------------------------------------------------------------------------*/
1288 void usbnet_tx_timeout (struct net_device *net)
1290 struct usbnet *dev = netdev_priv(net);
1292 unlink_urbs (dev, &dev->txq);
1293 tasklet_schedule (&dev->bh);
1294 /* this needs to be handled individually because the generic layer
1295 * doesn't know what is sufficient and could not restore private
1296 * information if a remedy of an unconditional reset were used.
1298 if (dev->driver_info->recover)
1299 (dev->driver_info->recover)(dev);
1301 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1303 /*-------------------------------------------------------------------------*/
1305 static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1307 unsigned num_sgs, total_len = 0;
1308 int i, s = 0;
1310 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1311 if (num_sgs == 1)
1312 return 0;
1314 /* reserve one for zero packet */
1315 urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
1316 GFP_ATOMIC);
1317 if (!urb->sg)
1318 return -ENOMEM;
1320 urb->num_sgs = num_sgs;
1321 sg_init_table(urb->sg, urb->num_sgs + 1);
1323 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1324 total_len += skb_headlen(skb);
1326 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1327 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1329 total_len += skb_frag_size(f);
1330 sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1331 f->page_offset);
1333 urb->transfer_buffer_length = total_len;
1335 return 1;
1338 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1339 struct net_device *net)
1341 struct usbnet *dev = netdev_priv(net);
1342 unsigned int length;
1343 struct urb *urb = NULL;
1344 struct skb_data *entry;
1345 const struct driver_info *info = dev->driver_info;
1346 unsigned long flags;
1347 int retval;
1349 if (skb)
1350 skb_tx_timestamp(skb);
1352 // some devices want funky USB-level framing, for
1353 // win32 driver (usually) and/or hardware quirks
1354 if (info->tx_fixup) {
1355 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1356 if (!skb) {
1357 /* packet collected; minidriver waiting for more */
1358 if (info->flags & FLAG_MULTI_PACKET)
1359 goto not_drop;
1360 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1361 goto drop;
1365 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1366 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1367 goto drop;
1370 entry = (struct skb_data *) skb->cb;
1371 entry->urb = urb;
1372 entry->dev = dev;
1374 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1375 skb->data, skb->len, tx_complete, skb);
1376 if (dev->can_dma_sg) {
1377 if (build_dma_sg(skb, urb) < 0)
1378 goto drop;
1380 length = urb->transfer_buffer_length;
1382 /* don't assume the hardware handles USB_ZERO_PACKET
1383 * NOTE: strictly conforming cdc-ether devices should expect
1384 * the ZLP here, but ignore the one-byte packet.
1385 * NOTE2: CDC NCM specification is different from CDC ECM when
1386 * handling ZLP/short packets, so cdc_ncm driver will make short
1387 * packet itself if needed.
1389 if (length % dev->maxpacket == 0) {
1390 if (!(info->flags & FLAG_SEND_ZLP)) {
1391 if (!(info->flags & FLAG_MULTI_PACKET)) {
1392 length++;
1393 if (skb_tailroom(skb) && !urb->num_sgs) {
1394 skb->data[skb->len] = 0;
1395 __skb_put(skb, 1);
1396 } else if (urb->num_sgs)
1397 sg_set_buf(&urb->sg[urb->num_sgs++],
1398 dev->padding_pkt, 1);
1400 } else
1401 urb->transfer_flags |= URB_ZERO_PACKET;
1403 urb->transfer_buffer_length = length;
1405 if (info->flags & FLAG_MULTI_PACKET) {
1406 /* Driver has set number of packets and a length delta.
1407 * Calculate the complete length and ensure that it's
1408 * positive.
1410 entry->length += length;
1411 if (WARN_ON_ONCE(entry->length <= 0))
1412 entry->length = length;
1413 } else {
1414 usbnet_set_skb_tx_stats(skb, 1, length);
1417 spin_lock_irqsave(&dev->txq.lock, flags);
1418 retval = usb_autopm_get_interface_async(dev->intf);
1419 if (retval < 0) {
1420 spin_unlock_irqrestore(&dev->txq.lock, flags);
1421 goto drop;
1423 if (netif_queue_stopped(net)) {
1424 usb_autopm_put_interface_async(dev->intf);
1425 spin_unlock_irqrestore(&dev->txq.lock, flags);
1426 goto drop;
1429 #ifdef CONFIG_PM
1430 /* if this triggers the device is still a sleep */
1431 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1432 /* transmission will be done in resume */
1433 usb_anchor_urb(urb, &dev->deferred);
1434 /* no use to process more packets */
1435 netif_stop_queue(net);
1436 usb_put_urb(urb);
1437 spin_unlock_irqrestore(&dev->txq.lock, flags);
1438 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1439 goto deferred;
1441 #endif
1443 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1444 case -EPIPE:
1445 netif_stop_queue (net);
1446 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1447 usb_autopm_put_interface_async(dev->intf);
1448 break;
1449 default:
1450 usb_autopm_put_interface_async(dev->intf);
1451 netif_dbg(dev, tx_err, dev->net,
1452 "tx: submit urb err %d\n", retval);
1453 break;
1454 case 0:
1455 netif_trans_update(net);
1456 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1457 if (dev->txq.qlen >= TX_QLEN (dev))
1458 netif_stop_queue (net);
1460 spin_unlock_irqrestore (&dev->txq.lock, flags);
1462 if (retval) {
1463 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1464 drop:
1465 dev->net->stats.tx_dropped++;
1466 not_drop:
1467 if (skb)
1468 dev_kfree_skb_any (skb);
1469 if (urb) {
1470 kfree(urb->sg);
1471 usb_free_urb(urb);
1473 } else
1474 netif_dbg(dev, tx_queued, dev->net,
1475 "> tx, len %u, type 0x%x\n", length, skb->protocol);
1476 #ifdef CONFIG_PM
1477 deferred:
1478 #endif
1479 return NETDEV_TX_OK;
1481 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1483 static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
1485 struct urb *urb;
1486 int i;
1487 int ret = 0;
1489 /* don't refill the queue all at once */
1490 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1491 urb = usb_alloc_urb(0, flags);
1492 if (urb != NULL) {
1493 ret = rx_submit(dev, urb, flags);
1494 if (ret)
1495 goto err;
1496 } else {
1497 ret = -ENOMEM;
1498 goto err;
1501 err:
1502 return ret;
1505 /*-------------------------------------------------------------------------*/
1507 // tasklet (work deferred from completions, in_irq) or timer
1509 static void usbnet_bh (struct timer_list *t)
1511 struct usbnet *dev = from_timer(dev, t, delay);
1512 struct sk_buff *skb;
1513 struct skb_data *entry;
1515 while ((skb = skb_dequeue (&dev->done))) {
1516 entry = (struct skb_data *) skb->cb;
1517 switch (entry->state) {
1518 case rx_done:
1519 entry->state = rx_cleanup;
1520 rx_process (dev, skb);
1521 continue;
1522 case tx_done:
1523 kfree(entry->urb->sg);
1524 /* fall through */
1525 case rx_cleanup:
1526 usb_free_urb (entry->urb);
1527 dev_kfree_skb (skb);
1528 continue;
1529 default:
1530 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1534 /* restart RX again after disabling due to high error rate */
1535 clear_bit(EVENT_RX_KILL, &dev->flags);
1537 /* waiting for all pending urbs to complete?
1538 * only then can we forgo submitting anew
1540 if (waitqueue_active(&dev->wait)) {
1541 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1542 wake_up_all(&dev->wait);
1544 // or are we maybe short a few urbs?
1545 } else if (netif_running (dev->net) &&
1546 netif_device_present (dev->net) &&
1547 netif_carrier_ok(dev->net) &&
1548 !timer_pending(&dev->delay) &&
1549 !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
1550 !test_bit(EVENT_RX_HALT, &dev->flags)) {
1551 int temp = dev->rxq.qlen;
1553 if (temp < RX_QLEN(dev)) {
1554 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1555 return;
1556 if (temp != dev->rxq.qlen)
1557 netif_dbg(dev, link, dev->net,
1558 "rxqlen %d --> %d\n",
1559 temp, dev->rxq.qlen);
1560 if (dev->rxq.qlen < RX_QLEN(dev))
1561 tasklet_schedule (&dev->bh);
1563 if (dev->txq.qlen < TX_QLEN (dev))
1564 netif_wake_queue (dev->net);
1569 /*-------------------------------------------------------------------------
1571 * USB Device Driver support
1573 *-------------------------------------------------------------------------*/
1575 // precondition: never called in_interrupt
1577 void usbnet_disconnect (struct usb_interface *intf)
1579 struct usbnet *dev;
1580 struct usb_device *xdev;
1581 struct net_device *net;
1583 dev = usb_get_intfdata(intf);
1584 usb_set_intfdata(intf, NULL);
1585 if (!dev)
1586 return;
1588 xdev = interface_to_usbdev (intf);
1590 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1591 intf->dev.driver->name,
1592 xdev->bus->bus_name, xdev->devpath,
1593 dev->driver_info->description);
1595 net = dev->net;
1596 unregister_netdev (net);
1598 cancel_work_sync(&dev->kevent);
1600 usb_scuttle_anchored_urbs(&dev->deferred);
1602 if (dev->driver_info->unbind)
1603 dev->driver_info->unbind (dev, intf);
1605 usb_kill_urb(dev->interrupt);
1606 usb_free_urb(dev->interrupt);
1607 kfree(dev->padding_pkt);
1609 free_percpu(dev->stats64);
1610 free_netdev(net);
1612 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1614 static const struct net_device_ops usbnet_netdev_ops = {
1615 .ndo_open = usbnet_open,
1616 .ndo_stop = usbnet_stop,
1617 .ndo_start_xmit = usbnet_start_xmit,
1618 .ndo_tx_timeout = usbnet_tx_timeout,
1619 .ndo_set_rx_mode = usbnet_set_rx_mode,
1620 .ndo_change_mtu = usbnet_change_mtu,
1621 .ndo_get_stats64 = usbnet_get_stats64,
1622 .ndo_set_mac_address = eth_mac_addr,
1623 .ndo_validate_addr = eth_validate_addr,
1626 /*-------------------------------------------------------------------------*/
1628 // precondition: never called in_interrupt
1630 static struct device_type wlan_type = {
1631 .name = "wlan",
1634 static struct device_type wwan_type = {
1635 .name = "wwan",
1639 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1641 struct usbnet *dev;
1642 struct net_device *net;
1643 struct usb_host_interface *interface;
1644 const struct driver_info *info;
1645 struct usb_device *xdev;
1646 int status;
1647 const char *name;
1648 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1650 /* usbnet already took usb runtime pm, so have to enable the feature
1651 * for usb interface, otherwise usb_autopm_get_interface may return
1652 * failure if RUNTIME_PM is enabled.
1654 if (!driver->supports_autosuspend) {
1655 driver->supports_autosuspend = 1;
1656 pm_runtime_enable(&udev->dev);
1659 name = udev->dev.driver->name;
1660 info = (const struct driver_info *) prod->driver_info;
1661 if (!info) {
1662 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1663 return -ENODEV;
1665 xdev = interface_to_usbdev (udev);
1666 interface = udev->cur_altsetting;
1668 status = -ENOMEM;
1670 // set up our own records
1671 net = alloc_etherdev(sizeof(*dev));
1672 if (!net)
1673 goto out;
1675 /* netdev_printk() needs this so do it as early as possible */
1676 SET_NETDEV_DEV(net, &udev->dev);
1678 dev = netdev_priv(net);
1679 dev->udev = xdev;
1680 dev->intf = udev;
1681 dev->driver_info = info;
1682 dev->driver_name = name;
1684 dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1685 if (!dev->stats64)
1686 goto out0;
1688 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1689 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1690 init_waitqueue_head(&dev->wait);
1691 skb_queue_head_init (&dev->rxq);
1692 skb_queue_head_init (&dev->txq);
1693 skb_queue_head_init (&dev->done);
1694 skb_queue_head_init(&dev->rxq_pause);
1695 dev->bh.func = (void (*)(unsigned long))usbnet_bh;
1696 dev->bh.data = (unsigned long)&dev->delay;
1697 INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
1698 init_usb_anchor(&dev->deferred);
1699 timer_setup(&dev->delay, usbnet_bh, 0);
1700 mutex_init (&dev->phy_mutex);
1701 mutex_init(&dev->interrupt_mutex);
1702 dev->interrupt_count = 0;
1704 dev->net = net;
1705 strcpy (net->name, "usb%d");
1706 memcpy (net->dev_addr, node_id, sizeof node_id);
1708 /* rx and tx sides can use different message sizes;
1709 * bind() should set rx_urb_size in that case.
1711 dev->hard_mtu = net->mtu + net->hard_header_len;
1712 net->min_mtu = 0;
1713 net->max_mtu = ETH_MAX_MTU;
1715 net->netdev_ops = &usbnet_netdev_ops;
1716 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1717 net->ethtool_ops = &usbnet_ethtool_ops;
1719 // allow device-specific bind/init procedures
1720 // NOTE net->name still not usable ...
1721 if (info->bind) {
1722 status = info->bind (dev, udev);
1723 if (status < 0)
1724 goto out1;
1726 // heuristic: "usb%d" for links we know are two-host,
1727 // else "eth%d" when there's reasonable doubt. userspace
1728 // can rename the link if it knows better.
1729 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1730 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1731 (net->dev_addr [0] & 0x02) == 0))
1732 strcpy (net->name, "eth%d");
1733 /* WLAN devices should always be named "wlan%d" */
1734 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1735 strcpy(net->name, "wlan%d");
1736 /* WWAN devices should always be named "wwan%d" */
1737 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1738 strcpy(net->name, "wwan%d");
1740 /* devices that cannot do ARP */
1741 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1742 net->flags |= IFF_NOARP;
1744 /* maybe the remote can't receive an Ethernet MTU */
1745 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1746 net->mtu = dev->hard_mtu - net->hard_header_len;
1747 } else if (!info->in || !info->out)
1748 status = usbnet_get_endpoints (dev, udev);
1749 else {
1750 dev->in = usb_rcvbulkpipe (xdev, info->in);
1751 dev->out = usb_sndbulkpipe (xdev, info->out);
1752 if (!(info->flags & FLAG_NO_SETINT))
1753 status = usb_set_interface (xdev,
1754 interface->desc.bInterfaceNumber,
1755 interface->desc.bAlternateSetting);
1756 else
1757 status = 0;
1760 if (status >= 0 && dev->status)
1761 status = init_status (dev, udev);
1762 if (status < 0)
1763 goto out3;
1765 if (!dev->rx_urb_size)
1766 dev->rx_urb_size = dev->hard_mtu;
1767 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1769 /* let userspace know we have a random address */
1770 if (ether_addr_equal(net->dev_addr, node_id))
1771 net->addr_assign_type = NET_ADDR_RANDOM;
1773 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1774 SET_NETDEV_DEVTYPE(net, &wlan_type);
1775 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1776 SET_NETDEV_DEVTYPE(net, &wwan_type);
1778 /* initialize max rx_qlen and tx_qlen */
1779 usbnet_update_max_qlen(dev);
1781 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1782 !(info->flags & FLAG_MULTI_PACKET)) {
1783 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
1784 if (!dev->padding_pkt) {
1785 status = -ENOMEM;
1786 goto out4;
1790 status = register_netdev (net);
1791 if (status)
1792 goto out5;
1793 netif_info(dev, probe, dev->net,
1794 "register '%s' at usb-%s-%s, %s, %pM\n",
1795 udev->dev.driver->name,
1796 xdev->bus->bus_name, xdev->devpath,
1797 dev->driver_info->description,
1798 net->dev_addr);
1800 // ok, it's ready to go.
1801 usb_set_intfdata (udev, dev);
1803 netif_device_attach (net);
1805 if (dev->driver_info->flags & FLAG_LINK_INTR)
1806 usbnet_link_change(dev, 0, 0);
1808 return 0;
1810 out5:
1811 kfree(dev->padding_pkt);
1812 out4:
1813 usb_free_urb(dev->interrupt);
1814 out3:
1815 if (info->unbind)
1816 info->unbind (dev, udev);
1817 out1:
1818 /* subdrivers must undo all they did in bind() if they
1819 * fail it, but we may fail later and a deferred kevent
1820 * may trigger an error resubmitting itself and, worse,
1821 * schedule a timer. So we kill it all just in case.
1823 cancel_work_sync(&dev->kevent);
1824 del_timer_sync(&dev->delay);
1825 free_percpu(dev->stats64);
1826 out0:
1827 free_netdev(net);
1828 out:
1829 return status;
1831 EXPORT_SYMBOL_GPL(usbnet_probe);
1833 /*-------------------------------------------------------------------------*/
1836 * suspend the whole driver as soon as the first interface is suspended
1837 * resume only when the last interface is resumed
1840 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1842 struct usbnet *dev = usb_get_intfdata(intf);
1844 if (!dev->suspend_count++) {
1845 spin_lock_irq(&dev->txq.lock);
1846 /* don't autosuspend while transmitting */
1847 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
1848 dev->suspend_count--;
1849 spin_unlock_irq(&dev->txq.lock);
1850 return -EBUSY;
1851 } else {
1852 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1853 spin_unlock_irq(&dev->txq.lock);
1856 * accelerate emptying of the rx and queues, to avoid
1857 * having everything error out.
1859 netif_device_detach (dev->net);
1860 usbnet_terminate_urbs(dev);
1861 __usbnet_status_stop_force(dev);
1864 * reattach so runtime management can use and
1865 * wake the device
1867 netif_device_attach (dev->net);
1869 return 0;
1871 EXPORT_SYMBOL_GPL(usbnet_suspend);
1873 int usbnet_resume (struct usb_interface *intf)
1875 struct usbnet *dev = usb_get_intfdata(intf);
1876 struct sk_buff *skb;
1877 struct urb *res;
1878 int retval;
1880 if (!--dev->suspend_count) {
1881 /* resume interrupt URB if it was previously submitted */
1882 __usbnet_status_start_force(dev, GFP_NOIO);
1884 spin_lock_irq(&dev->txq.lock);
1885 while ((res = usb_get_from_anchor(&dev->deferred))) {
1887 skb = (struct sk_buff *)res->context;
1888 retval = usb_submit_urb(res, GFP_ATOMIC);
1889 if (retval < 0) {
1890 dev_kfree_skb_any(skb);
1891 kfree(res->sg);
1892 usb_free_urb(res);
1893 usb_autopm_put_interface_async(dev->intf);
1894 } else {
1895 netif_trans_update(dev->net);
1896 __skb_queue_tail(&dev->txq, skb);
1900 smp_mb();
1901 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1902 spin_unlock_irq(&dev->txq.lock);
1904 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1905 /* handle remote wakeup ASAP
1906 * we cannot race against stop
1908 if (netif_device_present(dev->net) &&
1909 !timer_pending(&dev->delay) &&
1910 !test_bit(EVENT_RX_HALT, &dev->flags))
1911 rx_alloc_submit(dev, GFP_NOIO);
1913 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1914 netif_tx_wake_all_queues(dev->net);
1915 tasklet_schedule (&dev->bh);
1919 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1920 usb_autopm_get_interface_no_resume(intf);
1922 return 0;
1924 EXPORT_SYMBOL_GPL(usbnet_resume);
1927 * Either a subdriver implements manage_power, then it is assumed to always
1928 * be ready to be suspended or it reports the readiness to be suspended
1929 * explicitly
1931 void usbnet_device_suggests_idle(struct usbnet *dev)
1933 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1934 dev->intf->needs_remote_wakeup = 1;
1935 usb_autopm_put_interface_async(dev->intf);
1938 EXPORT_SYMBOL(usbnet_device_suggests_idle);
1941 * For devices that can do without special commands
1943 int usbnet_manage_power(struct usbnet *dev, int on)
1945 dev->intf->needs_remote_wakeup = on;
1946 return 0;
1948 EXPORT_SYMBOL(usbnet_manage_power);
1950 void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1952 /* update link after link is reseted */
1953 if (link && !need_reset)
1954 netif_carrier_on(dev->net);
1955 else
1956 netif_carrier_off(dev->net);
1958 if (need_reset && link)
1959 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
1960 else
1961 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1963 EXPORT_SYMBOL(usbnet_link_change);
1965 /*-------------------------------------------------------------------------*/
1966 static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1967 u16 value, u16 index, void *data, u16 size)
1969 void *buf = NULL;
1970 int err = -ENOMEM;
1972 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1973 " value=0x%04x index=0x%04x size=%d\n",
1974 cmd, reqtype, value, index, size);
1976 if (size) {
1977 buf = kmalloc(size, GFP_KERNEL);
1978 if (!buf)
1979 goto out;
1982 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1983 cmd, reqtype, value, index, buf, size,
1984 USB_CTRL_GET_TIMEOUT);
1985 if (err > 0 && err <= size) {
1986 if (data)
1987 memcpy(data, buf, err);
1988 else
1989 netdev_dbg(dev->net,
1990 "Huh? Data requested but thrown away.\n");
1992 kfree(buf);
1993 out:
1994 return err;
1997 static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1998 u16 value, u16 index, const void *data,
1999 u16 size)
2001 void *buf = NULL;
2002 int err = -ENOMEM;
2004 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2005 " value=0x%04x index=0x%04x size=%d\n",
2006 cmd, reqtype, value, index, size);
2008 if (data) {
2009 buf = kmemdup(data, size, GFP_KERNEL);
2010 if (!buf)
2011 goto out;
2012 } else {
2013 if (size) {
2014 WARN_ON_ONCE(1);
2015 err = -EINVAL;
2016 goto out;
2020 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2021 cmd, reqtype, value, index, buf, size,
2022 USB_CTRL_SET_TIMEOUT);
2023 kfree(buf);
2025 out:
2026 return err;
2030 * The function can't be called inside suspend/resume callback,
2031 * otherwise deadlock will be caused.
2033 int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2034 u16 value, u16 index, void *data, u16 size)
2036 int ret;
2038 if (usb_autopm_get_interface(dev->intf) < 0)
2039 return -ENODEV;
2040 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2041 data, size);
2042 usb_autopm_put_interface(dev->intf);
2043 return ret;
2045 EXPORT_SYMBOL_GPL(usbnet_read_cmd);
2048 * The function can't be called inside suspend/resume callback,
2049 * otherwise deadlock will be caused.
2051 int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2052 u16 value, u16 index, const void *data, u16 size)
2054 int ret;
2056 if (usb_autopm_get_interface(dev->intf) < 0)
2057 return -ENODEV;
2058 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2059 data, size);
2060 usb_autopm_put_interface(dev->intf);
2061 return ret;
2063 EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2066 * The function can be called inside suspend/resume callback safely
2067 * and should only be called by suspend/resume callback generally.
2069 int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2070 u16 value, u16 index, void *data, u16 size)
2072 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2073 data, size);
2075 EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
2078 * The function can be called inside suspend/resume callback safely
2079 * and should only be called by suspend/resume callback generally.
2081 int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2082 u16 value, u16 index, const void *data,
2083 u16 size)
2085 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2086 data, size);
2088 EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
2090 static void usbnet_async_cmd_cb(struct urb *urb)
2092 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2093 int status = urb->status;
2095 if (status < 0)
2096 dev_dbg(&urb->dev->dev, "%s failed with %d",
2097 __func__, status);
2099 kfree(req);
2100 usb_free_urb(urb);
2104 * The caller must make sure that device can't be put into suspend
2105 * state until the control URB completes.
2107 int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2108 u16 value, u16 index, const void *data, u16 size)
2110 struct usb_ctrlrequest *req = NULL;
2111 struct urb *urb;
2112 int err = -ENOMEM;
2113 void *buf = NULL;
2115 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2116 " value=0x%04x index=0x%04x size=%d\n",
2117 cmd, reqtype, value, index, size);
2119 urb = usb_alloc_urb(0, GFP_ATOMIC);
2120 if (!urb)
2121 goto fail;
2123 if (data) {
2124 buf = kmemdup(data, size, GFP_ATOMIC);
2125 if (!buf) {
2126 netdev_err(dev->net, "Error allocating buffer"
2127 " in %s!\n", __func__);
2128 goto fail_free;
2132 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
2133 if (!req)
2134 goto fail_free_buf;
2136 req->bRequestType = reqtype;
2137 req->bRequest = cmd;
2138 req->wValue = cpu_to_le16(value);
2139 req->wIndex = cpu_to_le16(index);
2140 req->wLength = cpu_to_le16(size);
2142 usb_fill_control_urb(urb, dev->udev,
2143 usb_sndctrlpipe(dev->udev, 0),
2144 (void *)req, buf, size,
2145 usbnet_async_cmd_cb, req);
2146 urb->transfer_flags |= URB_FREE_BUFFER;
2148 err = usb_submit_urb(urb, GFP_ATOMIC);
2149 if (err < 0) {
2150 netdev_err(dev->net, "Error submitting the control"
2151 " message: status=%d\n", err);
2152 goto fail_free;
2154 return 0;
2156 fail_free_buf:
2157 kfree(buf);
2158 fail_free:
2159 kfree(req);
2160 usb_free_urb(urb);
2161 fail:
2162 return err;
2165 EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2166 /*-------------------------------------------------------------------------*/
2168 static int __init usbnet_init(void)
2170 /* Compiler should optimize this out. */
2171 BUILD_BUG_ON(
2172 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
2174 eth_random_addr(node_id);
2175 return 0;
2177 module_init(usbnet_init);
2179 static void __exit usbnet_exit(void)
2182 module_exit(usbnet_exit);
2184 MODULE_AUTHOR("David Brownell");
2185 MODULE_DESCRIPTION("USB network driver framework");
2186 MODULE_LICENSE("GPL");