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