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