blob: 46f084cae01f0bf7aefbd72ff1ae90e8fccf02cf [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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 */
7
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.
12 *
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.
18 */
19
20// #define DEBUG // error path messages, extra info
21// #define VERBOSE // more; success messages
22
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>
36
37#define DRIVER_VERSION "22-Aug-2005"
38
39
40/*-------------------------------------------------------------------------*/
41
42/*
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).
48 *
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.
52 */
53#define MAX_QUEUE_MEMORY (60 * 1518)
54#define RX_QLEN(dev) ((dev)->rx_qlen)
55#define TX_QLEN(dev) ((dev)->tx_qlen)
56
57// reawaken network queue this soon after stopping; else watchdog barks
58#define TX_TIMEOUT_JIFFIES (5*HZ)
59
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.
62 */
63#define THROTTLE_JIFFIES (HZ/8)
64
65// between wakeups
66#define UNLINK_TIMEOUT_MS 3
67
68/*-------------------------------------------------------------------------*/
69
70/* use ethtool to change the level for any given device */
71static int msg_level = -1;
72module_param (msg_level, int, 0);
73MODULE_PARM_DESC (msg_level, "Override default message level");
74
75/*-------------------------------------------------------------------------*/
76
77/* handles CDC Ethernet and many other network "bulk data" interfaces */
78int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
79{
80 int tmp;
81 struct usb_host_interface *alt = NULL;
82 struct usb_host_endpoint *in = NULL, *out = NULL;
83 struct usb_host_endpoint *status = NULL;
84
85 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
86 unsigned ep;
87
88 in = out = status = NULL;
89 alt = intf->altsetting + tmp;
90
91 /* take the first altsetting with in-bulk + out-bulk;
92 * remember any status endpoint, just in case;
93 * ignore other endpoints and altsettings.
94 */
95 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
96 struct usb_host_endpoint *e;
97 int intr = 0;
98
99 e = alt->endpoint + ep;
100
101 /* ignore endpoints which cannot transfer data */
102 if (!usb_endpoint_maxp(&e->desc))
103 continue;
104
105 switch (e->desc.bmAttributes) {
106 case USB_ENDPOINT_XFER_INT:
107 if (!usb_endpoint_dir_in(&e->desc))
108 continue;
109 intr = 1;
110 /* FALLTHROUGH */
111 case USB_ENDPOINT_XFER_BULK:
112 break;
113 default:
114 continue;
115 }
116 if (usb_endpoint_dir_in(&e->desc)) {
117 if (!intr && !in)
118 in = e;
119 else if (intr && !status)
120 status = e;
121 } else {
122 if (!out)
123 out = e;
124 }
125 }
126 if (in && out)
127 break;
128 }
129 if (!alt || !in || !out)
130 return -EINVAL;
131
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);
136 if (tmp < 0)
137 return tmp;
138 }
139
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;
145 return 0;
146}
147EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
148
149int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
150{
151 u8 addr[ETH_ALEN];
152 int tmp = -1, ret;
153 unsigned char buf [13];
154
155 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
156 if (ret == 12)
157 tmp = hex2bin(addr, buf, 6);
158 if (tmp < 0) {
159 dev_dbg(&dev->udev->dev,
160 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
161 if (ret >= 0)
162 ret = -EINVAL;
163 return ret;
164 }
165 eth_hw_addr_set(dev->net, addr);
166 return 0;
167}
168EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
169
170static void intr_complete (struct urb *urb)
171{
172 struct usbnet *dev = urb->context;
173 int status = urb->status;
174
175 switch (status) {
176 /* success */
177 case 0:
178 dev->driver_info->status(dev, urb);
179 break;
180
181 /* software-driven interface shutdown */
182 case -ENOENT: /* urb killed */
183 case -ESHUTDOWN: /* hardware gone */
184 netif_dbg(dev, ifdown, dev->net,
185 "intr shutdown, code %d\n", status);
186 return;
187
188 /* NOTE: not throttling like RX/TX, since this endpoint
189 * already polls infrequently
190 */
191 default:
192 netdev_dbg(dev->net, "intr status %d\n", status);
193 break;
194 }
195
196 status = usb_submit_urb (urb, GFP_ATOMIC);
197 if (status != 0)
198 netif_err(dev, timer, dev->net,
199 "intr resubmit --> %d\n", status);
200}
201
202static int init_status (struct usbnet *dev, struct usb_interface *intf)
203{
204 char *buf = NULL;
205 unsigned pipe = 0;
206 unsigned maxp;
207 unsigned period;
208
209 if (!dev->driver_info->status)
210 return 0;
211
212 pipe = usb_rcvintpipe (dev->udev,
213 dev->status->desc.bEndpointAddress
214 & USB_ENDPOINT_NUMBER_MASK);
215 maxp = usb_maxpacket (dev->udev, pipe, 0);
216
217 /* avoid 1 msec chatter: min 8 msec poll rate */
218 period = max ((int) dev->status->desc.bInterval,
219 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
220
221 buf = kmalloc (maxp, GFP_KERNEL);
222 if (buf) {
223 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
224 if (!dev->interrupt) {
225 kfree (buf);
226 return -ENOMEM;
227 } else {
228 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
229 buf, maxp, intr_complete, dev, period);
230 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
231 dev_dbg(&intf->dev,
232 "status ep%din, %d bytes period %d\n",
233 usb_pipeendpoint(pipe), maxp, period);
234 }
235 }
236 return 0;
237}
238
239/* Submit the interrupt URB if not previously submitted, increasing refcount */
240int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
241{
242 int ret = 0;
243
244 WARN_ON_ONCE(dev->interrupt == NULL);
245 if (dev->interrupt) {
246 mutex_lock(&dev->interrupt_mutex);
247
248 if (++dev->interrupt_count == 1)
249 ret = usb_submit_urb(dev->interrupt, mem_flags);
250
251 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
252 dev->interrupt_count);
253 mutex_unlock(&dev->interrupt_mutex);
254 }
255 return ret;
256}
257EXPORT_SYMBOL_GPL(usbnet_status_start);
258
259/* For resume; submit interrupt URB if previously submitted */
260static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
261{
262 int ret = 0;
263
264 mutex_lock(&dev->interrupt_mutex);
265 if (dev->interrupt_count) {
266 ret = usb_submit_urb(dev->interrupt, mem_flags);
267 dev_dbg(&dev->udev->dev,
268 "submitted interrupt URB for resume\n");
269 }
270 mutex_unlock(&dev->interrupt_mutex);
271 return ret;
272}
273
274/* Kill the interrupt URB if all submitters want it killed */
275void usbnet_status_stop(struct usbnet *dev)
276{
277 if (dev->interrupt) {
278 mutex_lock(&dev->interrupt_mutex);
279 WARN_ON(dev->interrupt_count == 0);
280
281 if (dev->interrupt_count && --dev->interrupt_count == 0)
282 usb_kill_urb(dev->interrupt);
283
284 dev_dbg(&dev->udev->dev,
285 "decremented interrupt URB count to %d\n",
286 dev->interrupt_count);
287 mutex_unlock(&dev->interrupt_mutex);
288 }
289}
290EXPORT_SYMBOL_GPL(usbnet_status_stop);
291
292/* For suspend; always kill interrupt URB */
293static void __usbnet_status_stop_force(struct usbnet *dev)
294{
295 if (dev->interrupt) {
296 mutex_lock(&dev->interrupt_mutex);
297 usb_kill_urb(dev->interrupt);
298 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
299 mutex_unlock(&dev->interrupt_mutex);
300 }
301}
302
303/* Passes this packet up the stack, updating its accounting.
304 * Some link protocols batch packets, so their rx_fixup paths
305 * can return clones as well as just modify the original skb.
306 */
307void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
308{
309 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
310 unsigned long flags;
311 int status;
312
313 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
314 skb_queue_tail(&dev->rxq_pause, skb);
315 return;
316 }
317
318 /* only update if unset to allow minidriver rx_fixup override */
319 if (skb->protocol == 0)
320 skb->protocol = eth_type_trans (skb, dev->net);
321
322 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
323 stats64->rx_packets++;
324 stats64->rx_bytes += skb->len;
325 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
326
327 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
328 skb->len + sizeof (struct ethhdr), skb->protocol);
329 memset (skb->cb, 0, sizeof (struct skb_data));
330
331 if (skb_defer_rx_timestamp(skb))
332 return;
333
334 status = netif_rx (skb);
335 if (status != NET_RX_SUCCESS)
336 netif_dbg(dev, rx_err, dev->net,
337 "netif_rx status %d\n", status);
338}
339EXPORT_SYMBOL_GPL(usbnet_skb_return);
340
341/* must be called if hard_mtu or rx_urb_size changed */
342void usbnet_update_max_qlen(struct usbnet *dev)
343{
344 enum usb_device_speed speed = dev->udev->speed;
345
346 if (!dev->rx_urb_size || !dev->hard_mtu)
347 goto insanity;
348 switch (speed) {
349 case USB_SPEED_HIGH:
350 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
351 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
352 break;
353 case USB_SPEED_SUPER:
354 case USB_SPEED_SUPER_PLUS:
355 /*
356 * Not take default 5ms qlen for super speed HC to
357 * save memory, and iperf tests show 2.5ms qlen can
358 * work well
359 */
360 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
361 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
362 break;
363 default:
364insanity:
365 dev->rx_qlen = dev->tx_qlen = 4;
366 }
367}
368EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
369
370
371/*-------------------------------------------------------------------------
372 *
373 * Network Device Driver (peer link to "Host Device", from USB host)
374 *
375 *-------------------------------------------------------------------------*/
376
377int usbnet_change_mtu (struct net_device *net, int new_mtu)
378{
379 struct usbnet *dev = netdev_priv(net);
380 int ll_mtu = new_mtu + net->hard_header_len;
381 int old_hard_mtu = dev->hard_mtu;
382 int old_rx_urb_size = dev->rx_urb_size;
383
384 // no second zero-length packet read wanted after mtu-sized packets
385 if ((ll_mtu % dev->maxpacket) == 0)
386 return -EDOM;
387 net->mtu = new_mtu;
388
389 dev->hard_mtu = net->mtu + net->hard_header_len;
390 if (dev->rx_urb_size == old_hard_mtu) {
391 dev->rx_urb_size = dev->hard_mtu;
392 if (dev->rx_urb_size > old_rx_urb_size) {
393 usbnet_pause_rx(dev);
394 usbnet_unlink_rx_urbs(dev);
395 usbnet_resume_rx(dev);
396 }
397 }
398
399 /* max qlen depend on hard_mtu and rx_urb_size */
400 usbnet_update_max_qlen(dev);
401
402 return 0;
403}
404EXPORT_SYMBOL_GPL(usbnet_change_mtu);
405
406/* The caller must hold list->lock */
407static void __usbnet_queue_skb(struct sk_buff_head *list,
408 struct sk_buff *newsk, enum skb_state state)
409{
410 struct skb_data *entry = (struct skb_data *) newsk->cb;
411
412 __skb_queue_tail(list, newsk);
413 entry->state = state;
414}
415
416/*-------------------------------------------------------------------------*/
417
418/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
419 * completion callbacks. 2.5 should have fixed those bugs...
420 */
421
422static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
423 struct sk_buff_head *list, enum skb_state state)
424{
425 unsigned long flags;
426 enum skb_state old_state;
427 struct skb_data *entry = (struct skb_data *) skb->cb;
428
429 spin_lock_irqsave(&list->lock, flags);
430 old_state = entry->state;
431 entry->state = state;
432 __skb_unlink(skb, list);
433
434 /* defer_bh() is never called with list == &dev->done.
435 * spin_lock_nested() tells lockdep that it is OK to take
436 * dev->done.lock here with list->lock held.
437 */
438 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
439
440 __skb_queue_tail(&dev->done, skb);
441 if (dev->done.qlen == 1)
442 tasklet_schedule(&dev->bh);
443 spin_unlock(&dev->done.lock);
444 spin_unlock_irqrestore(&list->lock, flags);
445 return old_state;
446}
447
448/* some work can't be done in tasklets, so we use keventd
449 *
450 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
451 * but tasklet_schedule() doesn't. hope the failure is rare.
452 */
453void usbnet_defer_kevent (struct usbnet *dev, int work)
454{
455 set_bit (work, &dev->flags);
456 if (!schedule_work (&dev->kevent))
457 netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
458 else
459 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
460}
461EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
462
463/*-------------------------------------------------------------------------*/
464
465static void rx_complete (struct urb *urb);
466
467static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
468{
469 struct sk_buff *skb;
470 struct skb_data *entry;
471 int retval = 0;
472 unsigned long lockflags;
473 size_t size = dev->rx_urb_size;
474
475 /* prevent rx skb allocation when error ratio is high */
476 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
477 usb_free_urb(urb);
478 return -ENOLINK;
479 }
480
481 if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
482 skb = __netdev_alloc_skb(dev->net, size, flags);
483 else
484 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
485 if (!skb) {
486 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
487 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
488 usb_free_urb (urb);
489 return -ENOMEM;
490 }
491
492 entry = (struct skb_data *) skb->cb;
493 entry->urb = urb;
494 entry->dev = dev;
495 entry->length = 0;
496
497 usb_fill_bulk_urb (urb, dev->udev, dev->in,
498 skb->data, size, rx_complete, skb);
499
500 spin_lock_irqsave (&dev->rxq.lock, lockflags);
501
502 if (netif_running (dev->net) &&
503 netif_device_present (dev->net) &&
504 test_bit(EVENT_DEV_OPEN, &dev->flags) &&
505 !test_bit (EVENT_RX_HALT, &dev->flags) &&
506 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
507 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
508 case -EPIPE:
509 usbnet_defer_kevent (dev, EVENT_RX_HALT);
510 break;
511 case -ENOMEM:
512 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
513 break;
514 case -ENODEV:
515 netif_dbg(dev, ifdown, dev->net, "device gone\n");
516 netif_device_detach (dev->net);
517 break;
518 case -EHOSTUNREACH:
519 retval = -ENOLINK;
520 break;
521 default:
522 netif_dbg(dev, rx_err, dev->net,
523 "rx submit, %d\n", retval);
524 tasklet_schedule (&dev->bh);
525 break;
526 case 0:
527 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
528 }
529 } else {
530 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
531 retval = -ENOLINK;
532 }
533 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
534 if (retval) {
535 dev_kfree_skb_any (skb);
536 usb_free_urb (urb);
537 }
538 return retval;
539}
540
541
542/*-------------------------------------------------------------------------*/
543
544static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
545{
546 if (dev->driver_info->rx_fixup &&
547 !dev->driver_info->rx_fixup (dev, skb)) {
548 /* With RX_ASSEMBLE, rx_fixup() must update counters */
549 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
550 dev->net->stats.rx_errors++;
551 goto done;
552 }
553 // else network stack removes extra byte if we forced a short packet
554
555 /* all data was already cloned from skb inside the driver */
556 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
557 goto done;
558
559 if (skb->len < ETH_HLEN) {
560 dev->net->stats.rx_errors++;
561 dev->net->stats.rx_length_errors++;
562 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
563 } else {
564 usbnet_skb_return(dev, skb);
565 return;
566 }
567
568done:
569 skb_queue_tail(&dev->done, skb);
570}
571
572/*-------------------------------------------------------------------------*/
573
574static void rx_complete (struct urb *urb)
575{
576 struct sk_buff *skb = (struct sk_buff *) urb->context;
577 struct skb_data *entry = (struct skb_data *) skb->cb;
578 struct usbnet *dev = entry->dev;
579 int urb_status = urb->status;
580 enum skb_state state;
581
582 skb_put (skb, urb->actual_length);
583 state = rx_done;
584 entry->urb = NULL;
585
586 switch (urb_status) {
587 /* success */
588 case 0:
589 break;
590
591 /* stalls need manual reset. this is rare ... except that
592 * when going through USB 2.0 TTs, unplug appears this way.
593 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
594 * storm, recovering as needed.
595 */
596 case -EPIPE:
597 dev->net->stats.rx_errors++;
598 usbnet_defer_kevent (dev, EVENT_RX_HALT);
599 // FALLTHROUGH
600
601 /* software-driven interface shutdown */
602 case -ECONNRESET: /* async unlink */
603 case -ESHUTDOWN: /* hardware gone */
604 netif_dbg(dev, ifdown, dev->net,
605 "rx shutdown, code %d\n", urb_status);
606 goto block;
607
608 /* we get controller i/o faults during hub_wq disconnect() delays.
609 * throttle down resubmits, to avoid log floods; just temporarily,
610 * so we still recover when the fault isn't a hub_wq delay.
611 */
612 case -EPROTO:
613 case -ETIME:
614 case -EILSEQ:
615 dev->net->stats.rx_errors++;
616 if (!timer_pending (&dev->delay)) {
617 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
618 netif_dbg(dev, link, dev->net,
619 "rx throttle %d\n", urb_status);
620 }
621block:
622 state = rx_cleanup;
623 entry->urb = urb;
624 urb = NULL;
625 break;
626
627 /* data overrun ... flush fifo? */
628 case -EOVERFLOW:
629 dev->net->stats.rx_over_errors++;
630 // FALLTHROUGH
631
632 default:
633 state = rx_cleanup;
634 dev->net->stats.rx_errors++;
635 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
636 break;
637 }
638
639 /* stop rx if packet error rate is high */
640 if (++dev->pkt_cnt > 30) {
641 dev->pkt_cnt = 0;
642 dev->pkt_err = 0;
643 } else {
644 if (state == rx_cleanup)
645 dev->pkt_err++;
646 if (dev->pkt_err > 20)
647 set_bit(EVENT_RX_KILL, &dev->flags);
648 }
649
650 state = defer_bh(dev, skb, &dev->rxq, state);
651
652 if (urb) {
653 if (netif_running (dev->net) &&
654 !test_bit (EVENT_RX_HALT, &dev->flags) &&
655 state != unlink_start) {
656 rx_submit (dev, urb, GFP_ATOMIC);
657 usb_mark_last_busy(dev->udev);
658 return;
659 }
660 usb_free_urb (urb);
661 }
662 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
663}
664
665/*-------------------------------------------------------------------------*/
666void usbnet_pause_rx(struct usbnet *dev)
667{
668 set_bit(EVENT_RX_PAUSED, &dev->flags);
669
670 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
671}
672EXPORT_SYMBOL_GPL(usbnet_pause_rx);
673
674void usbnet_resume_rx(struct usbnet *dev)
675{
676 struct sk_buff *skb;
677 int num = 0;
678
679 clear_bit(EVENT_RX_PAUSED, &dev->flags);
680
681 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
682 usbnet_skb_return(dev, skb);
683 num++;
684 }
685
686 tasklet_schedule(&dev->bh);
687
688 netif_dbg(dev, rx_status, dev->net,
689 "paused rx queue disabled, %d skbs requeued\n", num);
690}
691EXPORT_SYMBOL_GPL(usbnet_resume_rx);
692
693void usbnet_purge_paused_rxq(struct usbnet *dev)
694{
695 skb_queue_purge(&dev->rxq_pause);
696}
697EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
698
699/*-------------------------------------------------------------------------*/
700
701// unlink pending rx/tx; completion handlers do all other cleanup
702
703static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
704{
705 unsigned long flags;
706 struct sk_buff *skb;
707 int count = 0;
708
709 spin_lock_irqsave (&q->lock, flags);
710 while (!skb_queue_empty(q)) {
711 struct skb_data *entry;
712 struct urb *urb;
713 int retval;
714
715 skb_queue_walk(q, skb) {
716 entry = (struct skb_data *) skb->cb;
717 if (entry->state != unlink_start)
718 goto found;
719 }
720 break;
721found:
722 entry->state = unlink_start;
723 urb = entry->urb;
724
725 /*
726 * Get reference count of the URB to avoid it to be
727 * freed during usb_unlink_urb, which may trigger
728 * use-after-free problem inside usb_unlink_urb since
729 * usb_unlink_urb is always racing with .complete
730 * handler(include defer_bh).
731 */
732 usb_get_urb(urb);
733 spin_unlock_irqrestore(&q->lock, flags);
734 // during some PM-driven resume scenarios,
735 // these (async) unlinks complete immediately
736 retval = usb_unlink_urb (urb);
737 if (retval != -EINPROGRESS && retval != 0)
738 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
739 else
740 count++;
741 usb_put_urb(urb);
742 spin_lock_irqsave(&q->lock, flags);
743 }
744 spin_unlock_irqrestore (&q->lock, flags);
745 return count;
746}
747
748// Flush all pending rx urbs
749// minidrivers may need to do this when the MTU changes
750
751void usbnet_unlink_rx_urbs(struct usbnet *dev)
752{
753 if (netif_running(dev->net)) {
754 (void) unlink_urbs (dev, &dev->rxq);
755 tasklet_schedule(&dev->bh);
756 }
757}
758EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
759
760/*-------------------------------------------------------------------------*/
761
762static void wait_skb_queue_empty(struct sk_buff_head *q)
763{
764 unsigned long flags;
765
766 spin_lock_irqsave(&q->lock, flags);
767 while (!skb_queue_empty(q)) {
768 spin_unlock_irqrestore(&q->lock, flags);
769 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
770 set_current_state(TASK_UNINTERRUPTIBLE);
771 spin_lock_irqsave(&q->lock, flags);
772 }
773 spin_unlock_irqrestore(&q->lock, flags);
774}
775
776// precondition: never called in_interrupt
777static void usbnet_terminate_urbs(struct usbnet *dev)
778{
779 DECLARE_WAITQUEUE(wait, current);
780 int temp;
781
782 /* ensure there are no more active urbs */
783 add_wait_queue(&dev->wait, &wait);
784 set_current_state(TASK_UNINTERRUPTIBLE);
785 temp = unlink_urbs(dev, &dev->txq) +
786 unlink_urbs(dev, &dev->rxq);
787
788 /* maybe wait for deletions to finish. */
789 wait_skb_queue_empty(&dev->rxq);
790 wait_skb_queue_empty(&dev->txq);
791 wait_skb_queue_empty(&dev->done);
792 netif_dbg(dev, ifdown, dev->net,
793 "waited for %d urb completions\n", temp);
794 set_current_state(TASK_RUNNING);
795 remove_wait_queue(&dev->wait, &wait);
796}
797
798int usbnet_stop (struct net_device *net)
799{
800 struct usbnet *dev = netdev_priv(net);
801 const struct driver_info *info = dev->driver_info;
802 int retval, pm, mpn;
803
804 clear_bit(EVENT_DEV_OPEN, &dev->flags);
805 netif_stop_queue (net);
806
807 netif_info(dev, ifdown, dev->net,
808 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
809 net->stats.rx_packets, net->stats.tx_packets,
810 net->stats.rx_errors, net->stats.tx_errors);
811
812 /* to not race resume */
813 pm = usb_autopm_get_interface(dev->intf);
814 /* allow minidriver to stop correctly (wireless devices to turn off
815 * radio etc) */
816 if (info->stop) {
817 retval = info->stop(dev);
818 if (retval < 0)
819 netif_info(dev, ifdown, dev->net,
820 "stop fail (%d) usbnet usb-%s-%s, %s\n",
821 retval,
822 dev->udev->bus->bus_name, dev->udev->devpath,
823 info->description);
824 }
825
826 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
827 usbnet_terminate_urbs(dev);
828
829 usbnet_status_stop(dev);
830
831 usbnet_purge_paused_rxq(dev);
832
833 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
834
835 /* deferred work (timer, softirq, task) must also stop */
836 dev->flags = 0;
837 del_timer_sync (&dev->delay);
838 tasklet_kill (&dev->bh);
839 cancel_work_sync(&dev->kevent);
840 if (!pm)
841 usb_autopm_put_interface(dev->intf);
842
843 if (info->manage_power && mpn)
844 info->manage_power(dev, 0);
845 else
846 usb_autopm_put_interface(dev->intf);
847
848 return 0;
849}
850EXPORT_SYMBOL_GPL(usbnet_stop);
851
852/*-------------------------------------------------------------------------*/
853
854// posts reads, and enables write queuing
855
856// precondition: never called in_interrupt
857
858int usbnet_open (struct net_device *net)
859{
860 struct usbnet *dev = netdev_priv(net);
861 int retval;
862 const struct driver_info *info = dev->driver_info;
863
864 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
865 netif_info(dev, ifup, dev->net,
866 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
867 retval,
868 dev->udev->bus->bus_name,
869 dev->udev->devpath,
870 info->description);
871 goto done_nopm;
872 }
873
874 // put into "known safe" state
875 if (info->reset && (retval = info->reset (dev)) < 0) {
876 netif_info(dev, ifup, dev->net,
877 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
878 retval,
879 dev->udev->bus->bus_name,
880 dev->udev->devpath,
881 info->description);
882 goto done;
883 }
884
885 /* hard_mtu or rx_urb_size may change in reset() */
886 usbnet_update_max_qlen(dev);
887
888 // insist peer be connected
889 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
890 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
891 goto done;
892 }
893
894 /* start any status interrupt transfer */
895 if (dev->interrupt) {
896 retval = usbnet_status_start(dev, GFP_KERNEL);
897 if (retval < 0) {
898 netif_err(dev, ifup, dev->net,
899 "intr submit %d\n", retval);
900 goto done;
901 }
902 }
903
904 set_bit(EVENT_DEV_OPEN, &dev->flags);
905 netif_start_queue (net);
906 netif_info(dev, ifup, dev->net,
907 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
908 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
909 dev->net->mtu,
910 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
911 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
912 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
913 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
914 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
915 "simple");
916
917 /* reset rx error state */
918 dev->pkt_cnt = 0;
919 dev->pkt_err = 0;
920 clear_bit(EVENT_RX_KILL, &dev->flags);
921
922 // delay posting reads until we're fully open
923 tasklet_schedule (&dev->bh);
924 if (info->manage_power) {
925 retval = info->manage_power(dev, 1);
926 if (retval < 0) {
927 retval = 0;
928 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
929 } else {
930 usb_autopm_put_interface(dev->intf);
931 }
932 }
933 return retval;
934done:
935 usb_autopm_put_interface(dev->intf);
936done_nopm:
937 return retval;
938}
939EXPORT_SYMBOL_GPL(usbnet_open);
940
941/*-------------------------------------------------------------------------*/
942
943/* ethtool methods; minidrivers may need to add some more, but
944 * they'll probably want to use this base set.
945 */
946
947int usbnet_get_link_ksettings(struct net_device *net,
948 struct ethtool_link_ksettings *cmd)
949{
950 struct usbnet *dev = netdev_priv(net);
951
952 if (!dev->mii.mdio_read)
953 return -EOPNOTSUPP;
954
955 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
956
957 return 0;
958}
959EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
960
961int usbnet_set_link_ksettings(struct net_device *net,
962 const struct ethtool_link_ksettings *cmd)
963{
964 struct usbnet *dev = netdev_priv(net);
965 int retval;
966
967 if (!dev->mii.mdio_write)
968 return -EOPNOTSUPP;
969
970 retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
971
972 /* link speed/duplex might have changed */
973 if (dev->driver_info->link_reset)
974 dev->driver_info->link_reset(dev);
975
976 /* hard_mtu or rx_urb_size may change in link_reset() */
977 usbnet_update_max_qlen(dev);
978
979 return retval;
980}
981EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
982
983void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
984{
985 struct usbnet *dev = netdev_priv(net);
986 unsigned int start;
987 int cpu;
988
989 netdev_stats_to_stats64(stats, &net->stats);
990
991 for_each_possible_cpu(cpu) {
992 struct pcpu_sw_netstats *stats64;
993 u64 rx_packets, rx_bytes;
994 u64 tx_packets, tx_bytes;
995
996 stats64 = per_cpu_ptr(dev->stats64, cpu);
997
998 do {
999 start = u64_stats_fetch_begin_irq(&stats64->syncp);
1000 rx_packets = stats64->rx_packets;
1001 rx_bytes = stats64->rx_bytes;
1002 tx_packets = stats64->tx_packets;
1003 tx_bytes = stats64->tx_bytes;
1004 } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
1005
1006 stats->rx_packets += rx_packets;
1007 stats->rx_bytes += rx_bytes;
1008 stats->tx_packets += tx_packets;
1009 stats->tx_bytes += tx_bytes;
1010 }
1011}
1012EXPORT_SYMBOL_GPL(usbnet_get_stats64);
1013
1014u32 usbnet_get_link (struct net_device *net)
1015{
1016 struct usbnet *dev = netdev_priv(net);
1017
1018 /* If a check_connect is defined, return its result */
1019 if (dev->driver_info->check_connect)
1020 return dev->driver_info->check_connect (dev) == 0;
1021
1022 /* if the device has mii operations, use those */
1023 if (dev->mii.mdio_read)
1024 return mii_link_ok(&dev->mii);
1025
1026 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
1027 return ethtool_op_get_link(net);
1028}
1029EXPORT_SYMBOL_GPL(usbnet_get_link);
1030
1031int usbnet_nway_reset(struct net_device *net)
1032{
1033 struct usbnet *dev = netdev_priv(net);
1034
1035 if (!dev->mii.mdio_write)
1036 return -EOPNOTSUPP;
1037
1038 return mii_nway_restart(&dev->mii);
1039}
1040EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1041
1042void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1043{
1044 struct usbnet *dev = netdev_priv(net);
1045
1046 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1047 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
1048 strlcpy (info->fw_version, dev->driver_info->description,
1049 sizeof info->fw_version);
1050 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1051}
1052EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1053
1054u32 usbnet_get_msglevel (struct net_device *net)
1055{
1056 struct usbnet *dev = netdev_priv(net);
1057
1058 return dev->msg_enable;
1059}
1060EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1061
1062void usbnet_set_msglevel (struct net_device *net, u32 level)
1063{
1064 struct usbnet *dev = netdev_priv(net);
1065
1066 dev->msg_enable = level;
1067}
1068EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1069
1070/* drivers may override default ethtool_ops in their bind() routine */
1071static const struct ethtool_ops usbnet_ethtool_ops = {
1072 .get_link = usbnet_get_link,
1073 .nway_reset = usbnet_nway_reset,
1074 .get_drvinfo = usbnet_get_drvinfo,
1075 .get_msglevel = usbnet_get_msglevel,
1076 .set_msglevel = usbnet_set_msglevel,
1077 .get_ts_info = ethtool_op_get_ts_info,
1078 .get_link_ksettings = usbnet_get_link_ksettings,
1079 .set_link_ksettings = usbnet_set_link_ksettings,
1080};
1081
1082/*-------------------------------------------------------------------------*/
1083
1084static void __handle_link_change(struct usbnet *dev)
1085{
1086 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1087 return;
1088
1089 if (!netif_carrier_ok(dev->net)) {
1090 /* kill URBs for reading packets to save bus bandwidth */
1091 unlink_urbs(dev, &dev->rxq);
1092
1093 /*
1094 * tx_timeout will unlink URBs for sending packets and
1095 * tx queue is stopped by netcore after link becomes off
1096 */
1097 } else {
1098 /* submitting URBs for reading packets */
1099 tasklet_schedule(&dev->bh);
1100 }
1101
1102 /* hard_mtu or rx_urb_size may change during link change */
1103 usbnet_update_max_qlen(dev);
1104
1105 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1106}
1107
1108static void usbnet_set_rx_mode(struct net_device *net)
1109{
1110 struct usbnet *dev = netdev_priv(net);
1111
1112 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
1113}
1114
1115static void __handle_set_rx_mode(struct usbnet *dev)
1116{
1117 if (dev->driver_info->set_rx_mode)
1118 (dev->driver_info->set_rx_mode)(dev);
1119
1120 clear_bit(EVENT_SET_RX_MODE, &dev->flags);
1121}
1122
1123/* work that cannot be done in interrupt context uses keventd.
1124 *
1125 * NOTE: with 2.5 we could do more of this using completion callbacks,
1126 * especially now that control transfers can be queued.
1127 */
1128static void
1129usbnet_deferred_kevent (struct work_struct *work)
1130{
1131 struct usbnet *dev =
1132 container_of(work, struct usbnet, kevent);
1133 int status;
1134
1135 /* usb_clear_halt() needs a thread context */
1136 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1137 unlink_urbs (dev, &dev->txq);
1138 status = usb_autopm_get_interface(dev->intf);
1139 if (status < 0)
1140 goto fail_pipe;
1141 status = usb_clear_halt (dev->udev, dev->out);
1142 usb_autopm_put_interface(dev->intf);
1143 if (status < 0 &&
1144 status != -EPIPE &&
1145 status != -ESHUTDOWN) {
1146 if (netif_msg_tx_err (dev))
1147fail_pipe:
1148 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1149 status);
1150 } else {
1151 clear_bit (EVENT_TX_HALT, &dev->flags);
1152 if (status != -ESHUTDOWN)
1153 netif_wake_queue (dev->net);
1154 }
1155 }
1156 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1157 unlink_urbs (dev, &dev->rxq);
1158 status = usb_autopm_get_interface(dev->intf);
1159 if (status < 0)
1160 goto fail_halt;
1161 status = usb_clear_halt (dev->udev, dev->in);
1162 usb_autopm_put_interface(dev->intf);
1163 if (status < 0 &&
1164 status != -EPIPE &&
1165 status != -ESHUTDOWN) {
1166 if (netif_msg_rx_err (dev))
1167fail_halt:
1168 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1169 status);
1170 } else {
1171 clear_bit (EVENT_RX_HALT, &dev->flags);
1172 tasklet_schedule (&dev->bh);
1173 }
1174 }
1175
1176 /* tasklet could resubmit itself forever if memory is tight */
1177 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1178 struct urb *urb = NULL;
1179 int resched = 1;
1180
1181 if (netif_running (dev->net))
1182 urb = usb_alloc_urb (0, GFP_KERNEL);
1183 else
1184 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1185 if (urb != NULL) {
1186 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1187 status = usb_autopm_get_interface(dev->intf);
1188 if (status < 0) {
1189 usb_free_urb(urb);
1190 goto fail_lowmem;
1191 }
1192 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1193 resched = 0;
1194 usb_autopm_put_interface(dev->intf);
1195fail_lowmem:
1196 if (resched)
1197 tasklet_schedule (&dev->bh);
1198 }
1199 }
1200
1201 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1202 const struct driver_info *info = dev->driver_info;
1203 int retval = 0;
1204
1205 clear_bit (EVENT_LINK_RESET, &dev->flags);
1206 status = usb_autopm_get_interface(dev->intf);
1207 if (status < 0)
1208 goto skip_reset;
1209 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
1210 usb_autopm_put_interface(dev->intf);
1211skip_reset:
1212 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1213 retval,
1214 dev->udev->bus->bus_name,
1215 dev->udev->devpath,
1216 info->description);
1217 } else {
1218 usb_autopm_put_interface(dev->intf);
1219 }
1220
1221 /* handle link change from link resetting */
1222 __handle_link_change(dev);
1223 }
1224
1225 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1226 __handle_link_change(dev);
1227
1228 if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
1229 __handle_set_rx_mode(dev);
1230
1231
1232 if (dev->flags)
1233 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1234}
1235
1236/*-------------------------------------------------------------------------*/
1237
1238static void tx_complete (struct urb *urb)
1239{
1240 struct sk_buff *skb = (struct sk_buff *) urb->context;
1241 struct skb_data *entry = (struct skb_data *) skb->cb;
1242 struct usbnet *dev = entry->dev;
1243
1244 if (urb->status == 0) {
1245 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
1246 unsigned long flags;
1247
1248 flags = u64_stats_update_begin_irqsave(&stats64->syncp);
1249 stats64->tx_packets += entry->packets;
1250 stats64->tx_bytes += entry->length;
1251 u64_stats_update_end_irqrestore(&stats64->syncp, flags);
1252 } else {
1253 dev->net->stats.tx_errors++;
1254
1255 switch (urb->status) {
1256 case -EPIPE:
1257 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1258 break;
1259
1260 /* software-driven interface shutdown */
1261 case -ECONNRESET: // async unlink
1262 case -ESHUTDOWN: // hardware gone
1263 break;
1264
1265 /* like rx, tx gets controller i/o faults during hub_wq
1266 * delays and so it uses the same throttling mechanism.
1267 */
1268 case -EPROTO:
1269 case -ETIME:
1270 case -EILSEQ:
1271 usb_mark_last_busy(dev->udev);
1272 if (!timer_pending (&dev->delay)) {
1273 mod_timer (&dev->delay,
1274 jiffies + THROTTLE_JIFFIES);
1275 netif_dbg(dev, link, dev->net,
1276 "tx throttle %d\n", urb->status);
1277 }
1278 netif_stop_queue (dev->net);
1279 break;
1280 default:
1281 netif_dbg(dev, tx_err, dev->net,
1282 "tx err %d\n", entry->urb->status);
1283 break;
1284 }
1285 }
1286
1287 usb_autopm_put_interface_async(dev->intf);
1288 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1289}
1290
1291/*-------------------------------------------------------------------------*/
1292
1293void usbnet_tx_timeout (struct net_device *net)
1294{
1295 struct usbnet *dev = netdev_priv(net);
1296
1297 unlink_urbs (dev, &dev->txq);
1298 tasklet_schedule (&dev->bh);
1299 /* this needs to be handled individually because the generic layer
1300 * doesn't know what is sufficient and could not restore private
1301 * information if a remedy of an unconditional reset were used.
1302 */
1303 if (dev->driver_info->recover)
1304 (dev->driver_info->recover)(dev);
1305}
1306EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1307
1308/*-------------------------------------------------------------------------*/
1309
1310static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1311{
1312 unsigned num_sgs, total_len = 0;
1313 int i, s = 0;
1314
1315 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1316 if (num_sgs == 1)
1317 return 0;
1318
1319 /* reserve one for zero packet */
1320 urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
1321 GFP_ATOMIC);
1322 if (!urb->sg)
1323 return -ENOMEM;
1324
1325 urb->num_sgs = num_sgs;
1326 sg_init_table(urb->sg, urb->num_sgs + 1);
1327
1328 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1329 total_len += skb_headlen(skb);
1330
1331 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1332 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1333
1334 total_len += skb_frag_size(f);
1335 sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f),
1336 skb_frag_off(f));
1337 }
1338 urb->transfer_buffer_length = total_len;
1339
1340 return 1;
1341}
1342
1343netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1344 struct net_device *net)
1345{
1346 struct usbnet *dev = netdev_priv(net);
1347 unsigned int length;
1348 struct urb *urb = NULL;
1349 struct skb_data *entry;
1350 const struct driver_info *info = dev->driver_info;
1351 unsigned long flags;
1352 int retval;
1353
1354 if (skb)
1355 skb_tx_timestamp(skb);
1356
1357 // some devices want funky USB-level framing, for
1358 // win32 driver (usually) and/or hardware quirks
1359 if (info->tx_fixup) {
1360 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1361 if (!skb) {
1362 /* packet collected; minidriver waiting for more */
1363 if (info->flags & FLAG_MULTI_PACKET)
1364 goto not_drop;
1365 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1366 goto drop;
1367 }
1368 }
1369
1370 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1371 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1372 goto drop;
1373 }
1374
1375 entry = (struct skb_data *) skb->cb;
1376 entry->urb = urb;
1377 entry->dev = dev;
1378
1379 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1380 skb->data, skb->len, tx_complete, skb);
1381 if (dev->can_dma_sg) {
1382 if (build_dma_sg(skb, urb) < 0)
1383 goto drop;
1384 }
1385 length = urb->transfer_buffer_length;
1386
1387 /* don't assume the hardware handles USB_ZERO_PACKET
1388 * NOTE: strictly conforming cdc-ether devices should expect
1389 * the ZLP here, but ignore the one-byte packet.
1390 * NOTE2: CDC NCM specification is different from CDC ECM when
1391 * handling ZLP/short packets, so cdc_ncm driver will make short
1392 * packet itself if needed.
1393 */
1394 if (length % dev->maxpacket == 0) {
1395 if (!(info->flags & FLAG_SEND_ZLP)) {
1396 if (!(info->flags & FLAG_MULTI_PACKET)) {
1397 length++;
1398 if (skb_tailroom(skb) && !urb->num_sgs) {
1399 skb->data[skb->len] = 0;
1400 __skb_put(skb, 1);
1401 } else if (urb->num_sgs)
1402 sg_set_buf(&urb->sg[urb->num_sgs++],
1403 dev->padding_pkt, 1);
1404 }
1405 } else
1406 urb->transfer_flags |= URB_ZERO_PACKET;
1407 }
1408 urb->transfer_buffer_length = length;
1409
1410 if (info->flags & FLAG_MULTI_PACKET) {
1411 /* Driver has set number of packets and a length delta.
1412 * Calculate the complete length and ensure that it's
1413 * positive.
1414 */
1415 entry->length += length;
1416 if (WARN_ON_ONCE(entry->length <= 0))
1417 entry->length = length;
1418 } else {
1419 usbnet_set_skb_tx_stats(skb, 1, length);
1420 }
1421
1422 spin_lock_irqsave(&dev->txq.lock, flags);
1423 retval = usb_autopm_get_interface_async(dev->intf);
1424 if (retval < 0) {
1425 spin_unlock_irqrestore(&dev->txq.lock, flags);
1426 goto drop;
1427 }
1428 if (netif_queue_stopped(net)) {
1429 usb_autopm_put_interface_async(dev->intf);
1430 spin_unlock_irqrestore(&dev->txq.lock, flags);
1431 goto drop;
1432 }
1433
1434#ifdef CONFIG_PM
1435 /* if this triggers the device is still a sleep */
1436 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1437 /* transmission will be done in resume */
1438 usb_anchor_urb(urb, &dev->deferred);
1439 /* no use to process more packets */
1440 netif_stop_queue(net);
1441 usb_put_urb(urb);
1442 spin_unlock_irqrestore(&dev->txq.lock, flags);
1443 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1444 goto deferred;
1445 }
1446#endif
1447
1448 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1449 case -EPIPE:
1450 netif_stop_queue (net);
1451 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1452 usb_autopm_put_interface_async(dev->intf);
1453 break;
1454 default:
1455 usb_autopm_put_interface_async(dev->intf);
1456 netif_dbg(dev, tx_err, dev->net,
1457 "tx: submit urb err %d\n", retval);
1458 break;
1459 case 0:
1460 netif_trans_update(net);
1461 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1462 if (dev->txq.qlen >= TX_QLEN (dev))
1463 netif_stop_queue (net);
1464 }
1465 spin_unlock_irqrestore (&dev->txq.lock, flags);
1466
1467 if (retval) {
1468 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1469drop:
1470 dev->net->stats.tx_dropped++;
1471not_drop:
1472 if (skb)
1473 dev_kfree_skb_any (skb);
1474 if (urb) {
1475 kfree(urb->sg);
1476 usb_free_urb(urb);
1477 }
1478 } else
1479 netif_dbg(dev, tx_queued, dev->net,
1480 "> tx, len %u, type 0x%x\n", length, skb->protocol);
1481#ifdef CONFIG_PM
1482deferred:
1483#endif
1484 return NETDEV_TX_OK;
1485}
1486EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1487
1488static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
1489{
1490 struct urb *urb;
1491 int i;
1492 int ret = 0;
1493
1494 /* don't refill the queue all at once */
1495 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1496 urb = usb_alloc_urb(0, flags);
1497 if (urb != NULL) {
1498 ret = rx_submit(dev, urb, flags);
1499 if (ret)
1500 goto err;
1501 } else {
1502 ret = -ENOMEM;
1503 goto err;
1504 }
1505 }
1506err:
1507 return ret;
1508}
1509
1510/*-------------------------------------------------------------------------*/
1511
1512// tasklet (work deferred from completions, in_irq) or timer
1513
1514static void usbnet_bh (struct timer_list *t)
1515{
1516 struct usbnet *dev = from_timer(dev, t, delay);
1517 struct sk_buff *skb;
1518 struct skb_data *entry;
1519
1520 while ((skb = skb_dequeue (&dev->done))) {
1521 entry = (struct skb_data *) skb->cb;
1522 switch (entry->state) {
1523 case rx_done:
1524 entry->state = rx_cleanup;
1525 rx_process (dev, skb);
1526 continue;
1527 case tx_done:
1528 kfree(entry->urb->sg);
1529 /* fall through */
1530 case rx_cleanup:
1531 usb_free_urb (entry->urb);
1532 dev_kfree_skb (skb);
1533 continue;
1534 default:
1535 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1536 }
1537 }
1538
1539 /* restart RX again after disabling due to high error rate */
1540 clear_bit(EVENT_RX_KILL, &dev->flags);
1541
1542 /* waiting for all pending urbs to complete?
1543 * only then can we forgo submitting anew
1544 */
1545 if (waitqueue_active(&dev->wait)) {
1546 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1547 wake_up_all(&dev->wait);
1548
1549 // or are we maybe short a few urbs?
1550 } else if (netif_running (dev->net) &&
1551 netif_device_present (dev->net) &&
1552 netif_carrier_ok(dev->net) &&
1553 !timer_pending(&dev->delay) &&
1554 !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
1555 !test_bit(EVENT_RX_HALT, &dev->flags)) {
1556 int temp = dev->rxq.qlen;
1557
1558 if (temp < RX_QLEN(dev)) {
1559 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1560 return;
1561 if (temp != dev->rxq.qlen)
1562 netif_dbg(dev, link, dev->net,
1563 "rxqlen %d --> %d\n",
1564 temp, dev->rxq.qlen);
1565 if (dev->rxq.qlen < RX_QLEN(dev))
1566 tasklet_schedule (&dev->bh);
1567 }
1568 if (dev->txq.qlen < TX_QLEN (dev))
1569 netif_wake_queue (dev->net);
1570 }
1571}
1572
1573static void usbnet_bh_tasklet(unsigned long data)
1574{
1575 struct timer_list *t = (struct timer_list *)data;
1576
1577 usbnet_bh(t);
1578}
1579
1580
1581/*-------------------------------------------------------------------------
1582 *
1583 * USB Device Driver support
1584 *
1585 *-------------------------------------------------------------------------*/
1586
1587// precondition: never called in_interrupt
1588
1589void usbnet_disconnect (struct usb_interface *intf)
1590{
1591 struct usbnet *dev;
1592 struct usb_device *xdev;
1593 struct net_device *net;
1594 struct urb *urb;
1595
1596 dev = usb_get_intfdata(intf);
1597 usb_set_intfdata(intf, NULL);
1598 if (!dev)
1599 return;
1600
1601 xdev = interface_to_usbdev (intf);
1602
1603 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1604 intf->dev.driver->name,
1605 xdev->bus->bus_name, xdev->devpath,
1606 dev->driver_info->description);
1607
1608 net = dev->net;
1609 unregister_netdev (net);
1610
1611 while ((urb = usb_get_from_anchor(&dev->deferred))) {
1612 dev_kfree_skb(urb->context);
1613 kfree(urb->sg);
1614 usb_free_urb(urb);
1615 }
1616
1617 if (dev->driver_info->unbind)
1618 dev->driver_info->unbind (dev, intf);
1619
1620 usb_kill_urb(dev->interrupt);
1621 usb_free_urb(dev->interrupt);
1622 kfree(dev->padding_pkt);
1623
1624 free_percpu(dev->stats64);
1625 free_netdev(net);
1626}
1627EXPORT_SYMBOL_GPL(usbnet_disconnect);
1628
1629static const struct net_device_ops usbnet_netdev_ops = {
1630 .ndo_open = usbnet_open,
1631 .ndo_stop = usbnet_stop,
1632 .ndo_start_xmit = usbnet_start_xmit,
1633 .ndo_tx_timeout = usbnet_tx_timeout,
1634 .ndo_set_rx_mode = usbnet_set_rx_mode,
1635 .ndo_change_mtu = usbnet_change_mtu,
1636 .ndo_get_stats64 = usbnet_get_stats64,
1637 .ndo_set_mac_address = eth_mac_addr,
1638 .ndo_validate_addr = eth_validate_addr,
1639};
1640
1641/*-------------------------------------------------------------------------*/
1642
1643// precondition: never called in_interrupt
1644
1645static struct device_type wlan_type = {
1646 .name = "wlan",
1647};
1648
1649static struct device_type wwan_type = {
1650 .name = "wwan",
1651};
1652
1653int
1654usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1655{
1656 struct usbnet *dev;
1657 struct net_device *net;
1658 struct usb_host_interface *interface;
1659 const struct driver_info *info;
1660 struct usb_device *xdev;
1661 int status;
1662 const char *name;
1663 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1664
1665 /* usbnet already took usb runtime pm, so have to enable the feature
1666 * for usb interface, otherwise usb_autopm_get_interface may return
1667 * failure if RUNTIME_PM is enabled.
1668 */
1669 if (!driver->supports_autosuspend) {
1670 driver->supports_autosuspend = 1;
1671 pm_runtime_enable(&udev->dev);
1672 }
1673
1674 name = udev->dev.driver->name;
1675 info = (const struct driver_info *) prod->driver_info;
1676 if (!info) {
1677 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1678 return -ENODEV;
1679 }
1680 xdev = interface_to_usbdev (udev);
1681 interface = udev->cur_altsetting;
1682
1683 status = -ENOMEM;
1684
1685 // set up our own records
1686 net = alloc_etherdev(sizeof(*dev));
1687 if (!net)
1688 goto out;
1689
1690 /* netdev_printk() needs this so do it as early as possible */
1691 SET_NETDEV_DEV(net, &udev->dev);
1692
1693 dev = netdev_priv(net);
1694 dev->udev = xdev;
1695 dev->intf = udev;
1696 dev->driver_info = info;
1697 dev->driver_name = name;
1698
1699 dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1700 if (!dev->stats64)
1701 goto out0;
1702
1703 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1704 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1705 init_waitqueue_head(&dev->wait);
1706 skb_queue_head_init (&dev->rxq);
1707 skb_queue_head_init (&dev->txq);
1708 skb_queue_head_init (&dev->done);
1709 skb_queue_head_init(&dev->rxq_pause);
1710 dev->bh.func = usbnet_bh_tasklet;
1711 dev->bh.data = (unsigned long)&dev->delay;
1712 INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
1713 init_usb_anchor(&dev->deferred);
1714 timer_setup(&dev->delay, usbnet_bh, 0);
1715 mutex_init (&dev->phy_mutex);
1716 mutex_init(&dev->interrupt_mutex);
1717 dev->interrupt_count = 0;
1718
1719 dev->net = net;
1720 strscpy(net->name, "usb%d", sizeof(net->name));
1721
1722 /* rx and tx sides can use different message sizes;
1723 * bind() should set rx_urb_size in that case.
1724 */
1725 dev->hard_mtu = net->mtu + net->hard_header_len;
1726 net->min_mtu = 0;
1727 net->max_mtu = ETH_MAX_MTU;
1728
1729 net->netdev_ops = &usbnet_netdev_ops;
1730 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1731 net->ethtool_ops = &usbnet_ethtool_ops;
1732
1733 // allow device-specific bind/init procedures
1734 // NOTE net->name still not usable ...
1735 if (info->bind) {
1736 status = info->bind (dev, udev);
1737 if (status < 0)
1738 goto out1;
1739
1740 // heuristic: "usb%d" for links we know are two-host,
1741 // else "eth%d" when there's reasonable doubt. userspace
1742 // can rename the link if it knows better.
1743 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1744 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1745 /* somebody touched it*/
1746 !is_zero_ether_addr(net->dev_addr)))
1747 strscpy(net->name, "eth%d", sizeof(net->name));
1748 /* WLAN devices should always be named "wlan%d" */
1749 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1750 strscpy(net->name, "wlan%d", sizeof(net->name));
1751 /* WWAN devices should always be named "wwan%d" */
1752 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1753 strscpy(net->name, "wwan%d", sizeof(net->name));
1754
1755 /* devices that cannot do ARP */
1756 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1757 net->flags |= IFF_NOARP;
1758
1759 /* maybe the remote can't receive an Ethernet MTU */
1760 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1761 net->mtu = dev->hard_mtu - net->hard_header_len;
1762 } else if (!info->in || !info->out)
1763 status = usbnet_get_endpoints (dev, udev);
1764 else {
1765 u8 ep_addrs[3] = {
1766 info->in + USB_DIR_IN, info->out + USB_DIR_OUT, 0
1767 };
1768
1769 dev->in = usb_rcvbulkpipe (xdev, info->in);
1770 dev->out = usb_sndbulkpipe (xdev, info->out);
1771 if (!(info->flags & FLAG_NO_SETINT))
1772 status = usb_set_interface (xdev,
1773 interface->desc.bInterfaceNumber,
1774 interface->desc.bAlternateSetting);
1775 else
1776 status = 0;
1777
1778 if (status == 0 && !usb_check_bulk_endpoints(udev, ep_addrs))
1779 status = -EINVAL;
1780 }
1781 if (status >= 0 && dev->status)
1782 status = init_status (dev, udev);
1783 if (status < 0)
1784 goto out3;
1785
1786 if (!dev->rx_urb_size)
1787 dev->rx_urb_size = dev->hard_mtu;
1788 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1789 if (dev->maxpacket == 0) {
1790 /* that is a broken device */
1791 status = -ENODEV;
1792 goto out4;
1793 }
1794
1795 /* this flags the device for user space */
1796 if (!is_valid_ether_addr(net->dev_addr))
1797 eth_hw_addr_random(net);
1798
1799 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1800 SET_NETDEV_DEVTYPE(net, &wlan_type);
1801 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1802 SET_NETDEV_DEVTYPE(net, &wwan_type);
1803
1804 /* initialize max rx_qlen and tx_qlen */
1805 usbnet_update_max_qlen(dev);
1806
1807 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1808 !(info->flags & FLAG_MULTI_PACKET)) {
1809 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
1810 if (!dev->padding_pkt) {
1811 status = -ENOMEM;
1812 goto out4;
1813 }
1814 }
1815
1816 status = register_netdev (net);
1817 if (status)
1818 goto out5;
1819 netif_info(dev, probe, dev->net,
1820 "register '%s' at usb-%s-%s, %s, %pM\n",
1821 udev->dev.driver->name,
1822 xdev->bus->bus_name, xdev->devpath,
1823 dev->driver_info->description,
1824 net->dev_addr);
1825
1826 // ok, it's ready to go.
1827 usb_set_intfdata (udev, dev);
1828
1829 netif_device_attach (net);
1830
1831 if (dev->driver_info->flags & FLAG_LINK_INTR)
1832 usbnet_link_change(dev, 0, 0);
1833
1834 return 0;
1835
1836out5:
1837 kfree(dev->padding_pkt);
1838out4:
1839 usb_free_urb(dev->interrupt);
1840out3:
1841 if (info->unbind)
1842 info->unbind (dev, udev);
1843out1:
1844 /* subdrivers must undo all they did in bind() if they
1845 * fail it, but we may fail later and a deferred kevent
1846 * may trigger an error resubmitting itself and, worse,
1847 * schedule a timer. So we kill it all just in case.
1848 */
1849 cancel_work_sync(&dev->kevent);
1850 del_timer_sync(&dev->delay);
1851 free_percpu(dev->stats64);
1852out0:
1853 free_netdev(net);
1854out:
1855 return status;
1856}
1857EXPORT_SYMBOL_GPL(usbnet_probe);
1858
1859/*-------------------------------------------------------------------------*/
1860
1861/*
1862 * suspend the whole driver as soon as the first interface is suspended
1863 * resume only when the last interface is resumed
1864 */
1865
1866int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1867{
1868 struct usbnet *dev = usb_get_intfdata(intf);
1869
1870 if (!dev->suspend_count++) {
1871 spin_lock_irq(&dev->txq.lock);
1872 /* don't autosuspend while transmitting */
1873 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
1874 dev->suspend_count--;
1875 spin_unlock_irq(&dev->txq.lock);
1876 return -EBUSY;
1877 } else {
1878 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1879 spin_unlock_irq(&dev->txq.lock);
1880 }
1881 /*
1882 * accelerate emptying of the rx and queues, to avoid
1883 * having everything error out.
1884 */
1885 netif_device_detach (dev->net);
1886 usbnet_terminate_urbs(dev);
1887 __usbnet_status_stop_force(dev);
1888
1889 /*
1890 * reattach so runtime management can use and
1891 * wake the device
1892 */
1893 netif_device_attach (dev->net);
1894 }
1895 return 0;
1896}
1897EXPORT_SYMBOL_GPL(usbnet_suspend);
1898
1899int usbnet_resume (struct usb_interface *intf)
1900{
1901 struct usbnet *dev = usb_get_intfdata(intf);
1902 struct sk_buff *skb;
1903 struct urb *res;
1904 int retval;
1905
1906 if (!--dev->suspend_count) {
1907 /* resume interrupt URB if it was previously submitted */
1908 __usbnet_status_start_force(dev, GFP_NOIO);
1909
1910 spin_lock_irq(&dev->txq.lock);
1911 while ((res = usb_get_from_anchor(&dev->deferred))) {
1912
1913 skb = (struct sk_buff *)res->context;
1914 retval = usb_submit_urb(res, GFP_ATOMIC);
1915 if (retval < 0) {
1916 dev_kfree_skb_any(skb);
1917 kfree(res->sg);
1918 usb_free_urb(res);
1919 usb_autopm_put_interface_async(dev->intf);
1920 } else {
1921 netif_trans_update(dev->net);
1922 __skb_queue_tail(&dev->txq, skb);
1923 }
1924 }
1925
1926 smp_mb();
1927 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1928 spin_unlock_irq(&dev->txq.lock);
1929
1930 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1931 /* handle remote wakeup ASAP
1932 * we cannot race against stop
1933 */
1934 if (netif_device_present(dev->net) &&
1935 !timer_pending(&dev->delay) &&
1936 !test_bit(EVENT_RX_HALT, &dev->flags))
1937 rx_alloc_submit(dev, GFP_NOIO);
1938
1939 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1940 netif_tx_wake_all_queues(dev->net);
1941 tasklet_schedule (&dev->bh);
1942 }
1943 }
1944
1945 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1946 usb_autopm_get_interface_no_resume(intf);
1947
1948 return 0;
1949}
1950EXPORT_SYMBOL_GPL(usbnet_resume);
1951
1952/*
1953 * Either a subdriver implements manage_power, then it is assumed to always
1954 * be ready to be suspended or it reports the readiness to be suspended
1955 * explicitly
1956 */
1957void usbnet_device_suggests_idle(struct usbnet *dev)
1958{
1959 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1960 dev->intf->needs_remote_wakeup = 1;
1961 usb_autopm_put_interface_async(dev->intf);
1962 }
1963}
1964EXPORT_SYMBOL(usbnet_device_suggests_idle);
1965
1966/*
1967 * For devices that can do without special commands
1968 */
1969int usbnet_manage_power(struct usbnet *dev, int on)
1970{
1971 dev->intf->needs_remote_wakeup = on;
1972 return 0;
1973}
1974EXPORT_SYMBOL(usbnet_manage_power);
1975
1976void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1977{
1978 /* update link after link is reseted */
1979 if (link && !need_reset)
1980 netif_carrier_on(dev->net);
1981 else
1982 netif_carrier_off(dev->net);
1983
1984 if (need_reset && link)
1985 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
1986 else
1987 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1988}
1989EXPORT_SYMBOL(usbnet_link_change);
1990
1991/*-------------------------------------------------------------------------*/
1992static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1993 u16 value, u16 index, void *data, u16 size)
1994{
1995 void *buf = NULL;
1996 int err = -ENOMEM;
1997
1998 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1999 " value=0x%04x index=0x%04x size=%d\n",
2000 cmd, reqtype, value, index, size);
2001
2002 if (size) {
2003 buf = kmalloc(size, GFP_NOIO);
2004 if (!buf)
2005 goto out;
2006 }
2007
2008 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2009 cmd, reqtype, value, index, buf, size,
2010 USB_CTRL_GET_TIMEOUT);
2011 if (err > 0 && err <= size) {
2012 if (data)
2013 memcpy(data, buf, err);
2014 else
2015 netdev_dbg(dev->net,
2016 "Huh? Data requested but thrown away.\n");
2017 }
2018 kfree(buf);
2019out:
2020 return err;
2021}
2022
2023static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2024 u16 value, u16 index, const void *data,
2025 u16 size)
2026{
2027 void *buf = NULL;
2028 int err = -ENOMEM;
2029
2030 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2031 " value=0x%04x index=0x%04x size=%d\n",
2032 cmd, reqtype, value, index, size);
2033
2034 if (data) {
2035 buf = kmemdup(data, size, GFP_NOIO);
2036 if (!buf)
2037 goto out;
2038 } else {
2039 if (size) {
2040 WARN_ON_ONCE(1);
2041 err = -EINVAL;
2042 goto out;
2043 }
2044 }
2045
2046 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2047 cmd, reqtype, value, index, buf, size,
2048 USB_CTRL_SET_TIMEOUT);
2049 kfree(buf);
2050
2051out:
2052 return err;
2053}
2054
2055/*
2056 * The function can't be called inside suspend/resume callback,
2057 * otherwise deadlock will be caused.
2058 */
2059int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2060 u16 value, u16 index, void *data, u16 size)
2061{
2062 int ret;
2063
2064 if (usb_autopm_get_interface(dev->intf) < 0)
2065 return -ENODEV;
2066 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2067 data, size);
2068 usb_autopm_put_interface(dev->intf);
2069 return ret;
2070}
2071EXPORT_SYMBOL_GPL(usbnet_read_cmd);
2072
2073/*
2074 * The function can't be called inside suspend/resume callback,
2075 * otherwise deadlock will be caused.
2076 */
2077int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2078 u16 value, u16 index, const void *data, u16 size)
2079{
2080 int ret;
2081
2082 if (usb_autopm_get_interface(dev->intf) < 0)
2083 return -ENODEV;
2084 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2085 data, size);
2086 usb_autopm_put_interface(dev->intf);
2087 return ret;
2088}
2089EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2090
2091/*
2092 * The function can be called inside suspend/resume callback safely
2093 * and should only be called by suspend/resume callback generally.
2094 */
2095int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2096 u16 value, u16 index, void *data, u16 size)
2097{
2098 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2099 data, size);
2100}
2101EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
2102
2103/*
2104 * The function can be called inside suspend/resume callback safely
2105 * and should only be called by suspend/resume callback generally.
2106 */
2107int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2108 u16 value, u16 index, const void *data,
2109 u16 size)
2110{
2111 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2112 data, size);
2113}
2114EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
2115
2116static void usbnet_async_cmd_cb(struct urb *urb)
2117{
2118 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2119 int status = urb->status;
2120
2121 if (status < 0)
2122 dev_dbg(&urb->dev->dev, "%s failed with %d",
2123 __func__, status);
2124
2125 kfree(req);
2126 usb_free_urb(urb);
2127}
2128
2129/*
2130 * The caller must make sure that device can't be put into suspend
2131 * state until the control URB completes.
2132 */
2133int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2134 u16 value, u16 index, const void *data, u16 size)
2135{
2136 struct usb_ctrlrequest *req;
2137 struct urb *urb;
2138 int err = -ENOMEM;
2139 void *buf = NULL;
2140
2141 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2142 " value=0x%04x index=0x%04x size=%d\n",
2143 cmd, reqtype, value, index, size);
2144
2145 urb = usb_alloc_urb(0, GFP_ATOMIC);
2146 if (!urb)
2147 goto fail;
2148
2149 if (data) {
2150 buf = kmemdup(data, size, GFP_ATOMIC);
2151 if (!buf) {
2152 netdev_err(dev->net, "Error allocating buffer"
2153 " in %s!\n", __func__);
2154 goto fail_free_urb;
2155 }
2156 }
2157
2158 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
2159 if (!req)
2160 goto fail_free_buf;
2161
2162 req->bRequestType = reqtype;
2163 req->bRequest = cmd;
2164 req->wValue = cpu_to_le16(value);
2165 req->wIndex = cpu_to_le16(index);
2166 req->wLength = cpu_to_le16(size);
2167
2168 usb_fill_control_urb(urb, dev->udev,
2169 usb_sndctrlpipe(dev->udev, 0),
2170 (void *)req, buf, size,
2171 usbnet_async_cmd_cb, req);
2172 urb->transfer_flags |= URB_FREE_BUFFER;
2173
2174 err = usb_submit_urb(urb, GFP_ATOMIC);
2175 if (err < 0) {
2176 netdev_err(dev->net, "Error submitting the control"
2177 " message: status=%d\n", err);
2178 goto fail_free_all;
2179 }
2180 return 0;
2181
2182fail_free_all:
2183 kfree(req);
2184fail_free_buf:
2185 kfree(buf);
2186 /*
2187 * avoid a double free
2188 * needed because the flag can be set only
2189 * after filling the URB
2190 */
2191 urb->transfer_flags = 0;
2192fail_free_urb:
2193 usb_free_urb(urb);
2194fail:
2195 return err;
2196
2197}
2198EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2199/*-------------------------------------------------------------------------*/
2200
2201static int __init usbnet_init(void)
2202{
2203 /* Compiler should optimize this out. */
2204 BUILD_BUG_ON(
2205 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
2206
2207 return 0;
2208}
2209module_init(usbnet_init);
2210
2211static void __exit usbnet_exit(void)
2212{
2213}
2214module_exit(usbnet_exit);
2215
2216MODULE_AUTHOR("David Brownell");
2217MODULE_DESCRIPTION("USB network driver framework");
2218MODULE_LICENSE("GPL");