xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision 4d8c79b7e9ff05030aad68421f7584b129933ba6)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25b2fc499SJeff Garzik /*
35b2fc499SJeff Garzik  * USB Network driver infrastructure
45b2fc499SJeff Garzik  * Copyright (C) 2000-2005 by David Brownell
55b2fc499SJeff Garzik  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
65b2fc499SJeff Garzik  */
75b2fc499SJeff Garzik 
85b2fc499SJeff Garzik /*
95b2fc499SJeff Garzik  * This is a generic "USB networking" framework that works with several
105b2fc499SJeff Garzik  * kinds of full and high speed networking devices:  host-to-host cables,
115b2fc499SJeff Garzik  * smart usb peripherals, and actual Ethernet adapters.
125b2fc499SJeff Garzik  *
135b2fc499SJeff Garzik  * These devices usually differ in terms of control protocols (if they
145b2fc499SJeff Garzik  * even have one!) and sometimes they define new framing to wrap or batch
155b2fc499SJeff Garzik  * Ethernet packets.  Otherwise, they talk to USB pretty much the same,
165b2fc499SJeff Garzik  * so interface (un)binding, endpoint I/O queues, fault handling, and other
175b2fc499SJeff Garzik  * issues can usefully be addressed by this framework.
185b2fc499SJeff Garzik  */
195b2fc499SJeff Garzik 
205b2fc499SJeff Garzik // #define	DEBUG			// error path messages, extra info
215b2fc499SJeff Garzik // #define	VERBOSE			// more; success messages
225b2fc499SJeff Garzik 
235b2fc499SJeff Garzik #include <linux/module.h>
245b2fc499SJeff Garzik #include <linux/init.h>
255b2fc499SJeff Garzik #include <linux/netdevice.h>
265b2fc499SJeff Garzik #include <linux/etherdevice.h>
2703ad032bSPeter Holik #include <linux/ctype.h>
285b2fc499SJeff Garzik #include <linux/ethtool.h>
295b2fc499SJeff Garzik #include <linux/workqueue.h>
305b2fc499SJeff Garzik #include <linux/mii.h>
315b2fc499SJeff Garzik #include <linux/usb.h>
323692e94fSJussi Kivilinna #include <linux/usb/usbnet.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
34fd1f170dSAndy Shevchenko #include <linux/kernel.h>
35b0786b43SMing Lei #include <linux/pm_runtime.h>
365b2fc499SJeff Garzik 
375b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
385b2fc499SJeff Garzik 
395b2fc499SJeff Garzik /*
405b2fc499SJeff Garzik  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
415b2fc499SJeff Garzik  * Several dozen bytes of IPv4 data can fit in two such transactions.
425b2fc499SJeff Garzik  * One maximum size Ethernet packet takes twenty four of them.
435b2fc499SJeff Garzik  * For high speed, each frame comfortably fits almost 36 max size
445b2fc499SJeff Garzik  * Ethernet packets (so queues should be bigger).
455b2fc499SJeff Garzik  *
46a88c32aeSMing Lei  * The goal is to let the USB host controller be busy for 5msec or
47a88c32aeSMing Lei  * more before an irq is required, under load.  Jumbograms change
48a88c32aeSMing Lei  * the equation.
495b2fc499SJeff Garzik  */
50a88c32aeSMing Lei #define	MAX_QUEUE_MEMORY	(60 * 1518)
51a88c32aeSMing Lei #define	RX_QLEN(dev)		((dev)->rx_qlen)
52a88c32aeSMing Lei #define	TX_QLEN(dev)		((dev)->tx_qlen)
535b2fc499SJeff Garzik 
545b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
555b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES	(5*HZ)
565b2fc499SJeff Garzik 
5737ebb549SPetr Mladek /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
5837ebb549SPetr Mladek  * us (it polls at HZ/4 usually) before we report too many false errors.
5937ebb549SPetr Mladek  */
605b2fc499SJeff Garzik #define THROTTLE_JIFFIES	(HZ/8)
615b2fc499SJeff Garzik 
625b2fc499SJeff Garzik // between wakeups
635b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS	3
645b2fc499SJeff Garzik 
655b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
665b2fc499SJeff Garzik 
675b2fc499SJeff Garzik // randomly generated ethernet address
685b2fc499SJeff Garzik static u8	node_id [ETH_ALEN];
695b2fc499SJeff Garzik 
705b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
715b2fc499SJeff Garzik static int msg_level = -1;
725b2fc499SJeff Garzik module_param (msg_level, int, 0);
735b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
745b2fc499SJeff Garzik 
755b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
765b2fc499SJeff Garzik 
775b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
785b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
795b2fc499SJeff Garzik {
805b2fc499SJeff Garzik 	int				tmp;
815b2fc499SJeff Garzik 	struct usb_host_interface	*alt = NULL;
825b2fc499SJeff Garzik 	struct usb_host_endpoint	*in = NULL, *out = NULL;
835b2fc499SJeff Garzik 	struct usb_host_endpoint	*status = NULL;
845b2fc499SJeff Garzik 
855b2fc499SJeff Garzik 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
865b2fc499SJeff Garzik 		unsigned	ep;
875b2fc499SJeff Garzik 
885b2fc499SJeff Garzik 		in = out = status = NULL;
895b2fc499SJeff Garzik 		alt = intf->altsetting + tmp;
905b2fc499SJeff Garzik 
915b2fc499SJeff Garzik 		/* take the first altsetting with in-bulk + out-bulk;
925b2fc499SJeff Garzik 		 * remember any status endpoint, just in case;
9370f23fd6SJustin P. Mattock 		 * ignore other endpoints and altsettings.
945b2fc499SJeff Garzik 		 */
955b2fc499SJeff Garzik 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
965b2fc499SJeff Garzik 			struct usb_host_endpoint	*e;
975b2fc499SJeff Garzik 			int				intr = 0;
985b2fc499SJeff Garzik 
995b2fc499SJeff Garzik 			e = alt->endpoint + ep;
1008d3d7c20SBjørn Mork 
1018d3d7c20SBjørn Mork 			/* ignore endpoints which cannot transfer data */
1028d3d7c20SBjørn Mork 			if (!usb_endpoint_maxp(&e->desc))
1038d3d7c20SBjørn Mork 				continue;
1048d3d7c20SBjørn Mork 
1055b2fc499SJeff Garzik 			switch (e->desc.bmAttributes) {
1065b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_INT:
1075b2fc499SJeff Garzik 				if (!usb_endpoint_dir_in(&e->desc))
1085b2fc499SJeff Garzik 					continue;
1095b2fc499SJeff Garzik 				intr = 1;
110df561f66SGustavo A. R. Silva 				fallthrough;
1115b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_BULK:
1125b2fc499SJeff Garzik 				break;
1135b2fc499SJeff Garzik 			default:
1145b2fc499SJeff Garzik 				continue;
1155b2fc499SJeff Garzik 			}
1165b2fc499SJeff Garzik 			if (usb_endpoint_dir_in(&e->desc)) {
1175b2fc499SJeff Garzik 				if (!intr && !in)
1185b2fc499SJeff Garzik 					in = e;
1195b2fc499SJeff Garzik 				else if (intr && !status)
1205b2fc499SJeff Garzik 					status = e;
1215b2fc499SJeff Garzik 			} else {
1225b2fc499SJeff Garzik 				if (!out)
1235b2fc499SJeff Garzik 					out = e;
1245b2fc499SJeff Garzik 			}
1255b2fc499SJeff Garzik 		}
1265b2fc499SJeff Garzik 		if (in && out)
1275b2fc499SJeff Garzik 			break;
1285b2fc499SJeff Garzik 	}
1295b2fc499SJeff Garzik 	if (!alt || !in || !out)
1305b2fc499SJeff Garzik 		return -EINVAL;
1315b2fc499SJeff Garzik 
1328e95a202SJoe Perches 	if (alt->desc.bAlternateSetting != 0 ||
1338e95a202SJoe Perches 	    !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1345b2fc499SJeff Garzik 		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
1355b2fc499SJeff Garzik 				alt->desc.bAlternateSetting);
1365b2fc499SJeff Garzik 		if (tmp < 0)
1375b2fc499SJeff Garzik 			return tmp;
1385b2fc499SJeff Garzik 	}
1395b2fc499SJeff Garzik 
1405b2fc499SJeff Garzik 	dev->in = usb_rcvbulkpipe (dev->udev,
1415b2fc499SJeff Garzik 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1425b2fc499SJeff Garzik 	dev->out = usb_sndbulkpipe (dev->udev,
1435b2fc499SJeff Garzik 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1445b2fc499SJeff Garzik 	dev->status = status;
1455b2fc499SJeff Garzik 	return 0;
1465b2fc499SJeff Garzik }
1475b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1485b2fc499SJeff Garzik 
14903ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
15003ad032bSPeter Holik {
15151487ae7SAndy Shevchenko 	int 		tmp = -1, ret;
15203ad032bSPeter Holik 	unsigned char	buf [13];
15303ad032bSPeter Holik 
15451487ae7SAndy Shevchenko 	ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
15551487ae7SAndy Shevchenko 	if (ret == 12)
15651487ae7SAndy Shevchenko 		tmp = hex2bin(dev->net->dev_addr, buf, 6);
15751487ae7SAndy Shevchenko 	if (tmp < 0) {
15803ad032bSPeter Holik 		dev_dbg(&dev->udev->dev,
15903ad032bSPeter Holik 			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
16051487ae7SAndy Shevchenko 		if (ret >= 0)
16151487ae7SAndy Shevchenko 			ret = -EINVAL;
16251487ae7SAndy Shevchenko 		return ret;
16303ad032bSPeter Holik 	}
16403ad032bSPeter Holik 	return 0;
16503ad032bSPeter Holik }
16603ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
16703ad032bSPeter Holik 
16824ead299Stom.leiming@gmail.com static void intr_complete (struct urb *urb)
16924ead299Stom.leiming@gmail.com {
17024ead299Stom.leiming@gmail.com 	struct usbnet	*dev = urb->context;
17124ead299Stom.leiming@gmail.com 	int		status = urb->status;
17224ead299Stom.leiming@gmail.com 
17324ead299Stom.leiming@gmail.com 	switch (status) {
17424ead299Stom.leiming@gmail.com 	/* success */
17524ead299Stom.leiming@gmail.com 	case 0:
17624ead299Stom.leiming@gmail.com 		dev->driver_info->status(dev, urb);
17724ead299Stom.leiming@gmail.com 		break;
17824ead299Stom.leiming@gmail.com 
17924ead299Stom.leiming@gmail.com 	/* software-driven interface shutdown */
18024ead299Stom.leiming@gmail.com 	case -ENOENT:		/* urb killed */
18124ead299Stom.leiming@gmail.com 	case -ESHUTDOWN:	/* hardware gone */
18224ead299Stom.leiming@gmail.com 		netif_dbg(dev, ifdown, dev->net,
18324ead299Stom.leiming@gmail.com 			  "intr shutdown, code %d\n", status);
18424ead299Stom.leiming@gmail.com 		return;
18524ead299Stom.leiming@gmail.com 
18624ead299Stom.leiming@gmail.com 	/* NOTE:  not throttling like RX/TX, since this endpoint
18724ead299Stom.leiming@gmail.com 	 * already polls infrequently
18824ead299Stom.leiming@gmail.com 	 */
18924ead299Stom.leiming@gmail.com 	default:
19024ead299Stom.leiming@gmail.com 		netdev_dbg(dev->net, "intr status %d\n", status);
19124ead299Stom.leiming@gmail.com 		break;
19224ead299Stom.leiming@gmail.com 	}
19324ead299Stom.leiming@gmail.com 
19424ead299Stom.leiming@gmail.com 	status = usb_submit_urb (urb, GFP_ATOMIC);
19524ead299Stom.leiming@gmail.com 	if (status != 0)
19624ead299Stom.leiming@gmail.com 		netif_err(dev, timer, dev->net,
19724ead299Stom.leiming@gmail.com 			  "intr resubmit --> %d\n", status);
19824ead299Stom.leiming@gmail.com }
1995b2fc499SJeff Garzik 
2005b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
2015b2fc499SJeff Garzik {
2025b2fc499SJeff Garzik 	char		*buf = NULL;
2035b2fc499SJeff Garzik 	unsigned	pipe = 0;
2045b2fc499SJeff Garzik 	unsigned	maxp;
2055b2fc499SJeff Garzik 	unsigned	period;
2065b2fc499SJeff Garzik 
2075b2fc499SJeff Garzik 	if (!dev->driver_info->status)
2085b2fc499SJeff Garzik 		return 0;
2095b2fc499SJeff Garzik 
2105b2fc499SJeff Garzik 	pipe = usb_rcvintpipe (dev->udev,
2115b2fc499SJeff Garzik 			dev->status->desc.bEndpointAddress
2125b2fc499SJeff Garzik 				& USB_ENDPOINT_NUMBER_MASK);
2135b2fc499SJeff Garzik 	maxp = usb_maxpacket (dev->udev, pipe, 0);
2145b2fc499SJeff Garzik 
2155b2fc499SJeff Garzik 	/* avoid 1 msec chatter:  min 8 msec poll rate */
2165b2fc499SJeff Garzik 	period = max ((int) dev->status->desc.bInterval,
2175b2fc499SJeff Garzik 		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
2185b2fc499SJeff Garzik 
2195b2fc499SJeff Garzik 	buf = kmalloc (maxp, GFP_KERNEL);
2205b2fc499SJeff Garzik 	if (buf) {
2215b2fc499SJeff Garzik 		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
2225b2fc499SJeff Garzik 		if (!dev->interrupt) {
2235b2fc499SJeff Garzik 			kfree (buf);
2245b2fc499SJeff Garzik 			return -ENOMEM;
2255b2fc499SJeff Garzik 		} else {
2265b2fc499SJeff Garzik 			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
2275b2fc499SJeff Garzik 				buf, maxp, intr_complete, dev, period);
228720f3d7cStom.leiming@gmail.com 			dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
2295b2fc499SJeff Garzik 			dev_dbg(&intf->dev,
2305b2fc499SJeff Garzik 				"status ep%din, %d bytes period %d\n",
2315b2fc499SJeff Garzik 				usb_pipeendpoint(pipe), maxp, period);
2325b2fc499SJeff Garzik 		}
2335b2fc499SJeff Garzik 	}
2345b2fc499SJeff Garzik 	return 0;
2355b2fc499SJeff Garzik }
2365b2fc499SJeff Garzik 
2376eecdc5fSDan Williams /* Submit the interrupt URB if not previously submitted, increasing refcount */
2386eecdc5fSDan Williams int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
2396eecdc5fSDan Williams {
2406eecdc5fSDan Williams 	int ret = 0;
2416eecdc5fSDan Williams 
2426eecdc5fSDan Williams 	WARN_ON_ONCE(dev->interrupt == NULL);
2436eecdc5fSDan Williams 	if (dev->interrupt) {
2446eecdc5fSDan Williams 		mutex_lock(&dev->interrupt_mutex);
2456eecdc5fSDan Williams 
2466eecdc5fSDan Williams 		if (++dev->interrupt_count == 1)
2476eecdc5fSDan Williams 			ret = usb_submit_urb(dev->interrupt, mem_flags);
2486eecdc5fSDan Williams 
2496eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
2506eecdc5fSDan Williams 			dev->interrupt_count);
2516eecdc5fSDan Williams 		mutex_unlock(&dev->interrupt_mutex);
2526eecdc5fSDan Williams 	}
2536eecdc5fSDan Williams 	return ret;
2546eecdc5fSDan Williams }
2556eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_start);
2566eecdc5fSDan Williams 
2576eecdc5fSDan Williams /* For resume; submit interrupt URB if previously submitted */
2586eecdc5fSDan Williams static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
2596eecdc5fSDan Williams {
2606eecdc5fSDan Williams 	int ret = 0;
2616eecdc5fSDan Williams 
2626eecdc5fSDan Williams 	mutex_lock(&dev->interrupt_mutex);
2636eecdc5fSDan Williams 	if (dev->interrupt_count) {
2646eecdc5fSDan Williams 		ret = usb_submit_urb(dev->interrupt, mem_flags);
2656eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev,
2666eecdc5fSDan Williams 			"submitted interrupt URB for resume\n");
2676eecdc5fSDan Williams 	}
2686eecdc5fSDan Williams 	mutex_unlock(&dev->interrupt_mutex);
2696eecdc5fSDan Williams 	return ret;
2706eecdc5fSDan Williams }
2716eecdc5fSDan Williams 
2726eecdc5fSDan Williams /* Kill the interrupt URB if all submitters want it killed */
2736eecdc5fSDan Williams void usbnet_status_stop(struct usbnet *dev)
2746eecdc5fSDan Williams {
2756eecdc5fSDan Williams 	if (dev->interrupt) {
2766eecdc5fSDan Williams 		mutex_lock(&dev->interrupt_mutex);
2776eecdc5fSDan Williams 		WARN_ON(dev->interrupt_count == 0);
2786eecdc5fSDan Williams 
2796eecdc5fSDan Williams 		if (dev->interrupt_count && --dev->interrupt_count == 0)
2806eecdc5fSDan Williams 			usb_kill_urb(dev->interrupt);
2816eecdc5fSDan Williams 
2826eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev,
2836eecdc5fSDan Williams 			"decremented interrupt URB count to %d\n",
2846eecdc5fSDan Williams 			dev->interrupt_count);
2856eecdc5fSDan Williams 		mutex_unlock(&dev->interrupt_mutex);
2866eecdc5fSDan Williams 	}
2876eecdc5fSDan Williams }
2886eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_stop);
2896eecdc5fSDan Williams 
2906eecdc5fSDan Williams /* For suspend; always kill interrupt URB */
2916eecdc5fSDan Williams static void __usbnet_status_stop_force(struct usbnet *dev)
2926eecdc5fSDan Williams {
2936eecdc5fSDan Williams 	if (dev->interrupt) {
2946eecdc5fSDan Williams 		mutex_lock(&dev->interrupt_mutex);
2956eecdc5fSDan Williams 		usb_kill_urb(dev->interrupt);
2966eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
2976eecdc5fSDan Williams 		mutex_unlock(&dev->interrupt_mutex);
2986eecdc5fSDan Williams 	}
2996eecdc5fSDan Williams }
3006eecdc5fSDan Williams 
3015b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
3025b2fc499SJeff Garzik  * Some link protocols batch packets, so their rx_fixup paths
3035b2fc499SJeff Garzik  * can return clones as well as just modify the original skb.
3045b2fc499SJeff Garzik  */
3055b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
3065b2fc499SJeff Garzik {
307af0c351cSHeiner Kallweit 	struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats);
3082695578bSEric Dumazet 	unsigned long flags;
3095b2fc499SJeff Garzik 	int	status;
3105b2fc499SJeff Garzik 
3117834ddbcSJussi Kivilinna 	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
3127834ddbcSJussi Kivilinna 		skb_queue_tail(&dev->rxq_pause, skb);
3137834ddbcSJussi Kivilinna 		return;
3147834ddbcSJussi Kivilinna 	}
3157834ddbcSJussi Kivilinna 
31681e0ce79SBjørn Mork 	/* only update if unset to allow minidriver rx_fixup override */
31781e0ce79SBjørn Mork 	if (skb->protocol == 0)
3185b2fc499SJeff Garzik 		skb->protocol = eth_type_trans (skb, dev->net);
31981e0ce79SBjørn Mork 
3202695578bSEric Dumazet 	flags = u64_stats_update_begin_irqsave(&stats64->syncp);
321c8b5d129SGreg Ungerer 	stats64->rx_packets++;
322c8b5d129SGreg Ungerer 	stats64->rx_bytes += skb->len;
3232695578bSEric Dumazet 	u64_stats_update_end_irqrestore(&stats64->syncp, flags);
3245b2fc499SJeff Garzik 
325a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
3265b2fc499SJeff Garzik 		  skb->len + sizeof (struct ethhdr), skb->protocol);
3275b2fc499SJeff Garzik 	memset (skb->cb, 0, sizeof (struct skb_data));
328f9b491ecSMichael Riesch 
329f9b491ecSMichael Riesch 	if (skb_defer_rx_timestamp(skb))
330f9b491ecSMichael Riesch 		return;
331f9b491ecSMichael Riesch 
3325b2fc499SJeff Garzik 	status = netif_rx (skb);
333a475f603SJoe Perches 	if (status != NET_RX_SUCCESS)
334a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net,
335a475f603SJoe Perches 			  "netif_rx status %d\n", status);
3365b2fc499SJeff Garzik }
3375b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
3385b2fc499SJeff Garzik 
339a88c32aeSMing Lei /* must be called if hard_mtu or rx_urb_size changed */
340a88c32aeSMing Lei void usbnet_update_max_qlen(struct usbnet *dev)
341a88c32aeSMing Lei {
342a88c32aeSMing Lei 	enum usb_device_speed speed = dev->udev->speed;
343a88c32aeSMing Lei 
344280ceaedSOliver Neukum 	if (!dev->rx_urb_size || !dev->hard_mtu)
345280ceaedSOliver Neukum 		goto insanity;
346a88c32aeSMing Lei 	switch (speed) {
347a88c32aeSMing Lei 	case USB_SPEED_HIGH:
348a88c32aeSMing Lei 		dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
349a88c32aeSMing Lei 		dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
350a88c32aeSMing Lei 		break;
351452c447aSMing Lei 	case USB_SPEED_SUPER:
352ea079842SOliver Neukum 	case USB_SPEED_SUPER_PLUS:
353452c447aSMing Lei 		/*
354452c447aSMing Lei 		 * Not take default 5ms qlen for super speed HC to
355452c447aSMing Lei 		 * save memory, and iperf tests show 2.5ms qlen can
356452c447aSMing Lei 		 * work well
357452c447aSMing Lei 		 */
358452c447aSMing Lei 		dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
359452c447aSMing Lei 		dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
360452c447aSMing Lei 		break;
361a88c32aeSMing Lei 	default:
362280ceaedSOliver Neukum insanity:
363a88c32aeSMing Lei 		dev->rx_qlen = dev->tx_qlen = 4;
364a88c32aeSMing Lei 	}
365a88c32aeSMing Lei }
366a88c32aeSMing Lei EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
367a88c32aeSMing Lei 
3685b2fc499SJeff Garzik 
3695b2fc499SJeff Garzik /*-------------------------------------------------------------------------
3705b2fc499SJeff Garzik  *
3715b2fc499SJeff Garzik  * Network Device Driver (peer link to "Host Device", from USB host)
3725b2fc499SJeff Garzik  *
3735b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
3745b2fc499SJeff Garzik 
375777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu)
3765b2fc499SJeff Garzik {
3775b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
3785b2fc499SJeff Garzik 	int		ll_mtu = new_mtu + net->hard_header_len;
3795b2fc499SJeff Garzik 	int		old_hard_mtu = dev->hard_mtu;
3805b2fc499SJeff Garzik 	int		old_rx_urb_size = dev->rx_urb_size;
3815b2fc499SJeff Garzik 
3825b2fc499SJeff Garzik 	// no second zero-length packet read wanted after mtu-sized packets
3835b2fc499SJeff Garzik 	if ((ll_mtu % dev->maxpacket) == 0)
3845b2fc499SJeff Garzik 		return -EDOM;
3855b2fc499SJeff Garzik 	net->mtu = new_mtu;
3865b2fc499SJeff Garzik 
3875b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
3885b2fc499SJeff Garzik 	if (dev->rx_urb_size == old_hard_mtu) {
3895b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
39043daa96bSSoohoon Lee 		if (dev->rx_urb_size > old_rx_urb_size) {
39143daa96bSSoohoon Lee 			usbnet_pause_rx(dev);
3925b2fc499SJeff Garzik 			usbnet_unlink_rx_urbs(dev);
39343daa96bSSoohoon Lee 			usbnet_resume_rx(dev);
39443daa96bSSoohoon Lee 		}
3955b2fc499SJeff Garzik 	}
3965b2fc499SJeff Garzik 
397a88c32aeSMing Lei 	/* max qlen depend on hard_mtu and rx_urb_size */
398a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
399a88c32aeSMing Lei 
4005b2fc499SJeff Garzik 	return 0;
4015b2fc499SJeff Garzik }
402777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu);
4035b2fc499SJeff Garzik 
4045b6e9bcdSMing Lei /* The caller must hold list->lock */
4055b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list,
4065b6e9bcdSMing Lei 			struct sk_buff *newsk, enum skb_state state)
4075b6e9bcdSMing Lei {
4085b6e9bcdSMing Lei 	struct skb_data *entry = (struct skb_data *) newsk->cb;
4095b6e9bcdSMing Lei 
4105b6e9bcdSMing Lei 	__skb_queue_tail(list, newsk);
4115b6e9bcdSMing Lei 	entry->state = state;
4125b6e9bcdSMing Lei }
4135b6e9bcdSMing Lei 
4145b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4155b2fc499SJeff Garzik 
4165b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
4175b2fc499SJeff Garzik  * completion callbacks.  2.5 should have fixed those bugs...
4185b2fc499SJeff Garzik  */
4195b2fc499SJeff Garzik 
4205b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
4215b6e9bcdSMing Lei 		struct sk_buff_head *list, enum skb_state state)
4225b2fc499SJeff Garzik {
4235b2fc499SJeff Garzik 	unsigned long		flags;
4245b6e9bcdSMing Lei 	enum skb_state 		old_state;
4255b6e9bcdSMing Lei 	struct skb_data *entry = (struct skb_data *) skb->cb;
4265b2fc499SJeff Garzik 
4275b2fc499SJeff Garzik 	spin_lock_irqsave(&list->lock, flags);
4285b6e9bcdSMing Lei 	old_state = entry->state;
4295b6e9bcdSMing Lei 	entry->state = state;
4305b2fc499SJeff Garzik 	__skb_unlink(skb, list);
431fcb0bb6aSEugene Shatokhin 
432fcb0bb6aSEugene Shatokhin 	/* defer_bh() is never called with list == &dev->done.
433fcb0bb6aSEugene Shatokhin 	 * spin_lock_nested() tells lockdep that it is OK to take
434fcb0bb6aSEugene Shatokhin 	 * dev->done.lock here with list->lock held.
435fcb0bb6aSEugene Shatokhin 	 */
436fcb0bb6aSEugene Shatokhin 	spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
437fcb0bb6aSEugene Shatokhin 
4385b2fc499SJeff Garzik 	__skb_queue_tail(&dev->done, skb);
4395b2fc499SJeff Garzik 	if (dev->done.qlen == 1)
4405b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
441fcb0bb6aSEugene Shatokhin 	spin_unlock(&dev->done.lock);
442fcb0bb6aSEugene Shatokhin 	spin_unlock_irqrestore(&list->lock, flags);
4435b6e9bcdSMing Lei 	return old_state;
4445b2fc499SJeff Garzik }
4455b2fc499SJeff Garzik 
4465b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
4475b2fc499SJeff Garzik  *
4485b2fc499SJeff Garzik  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
4495b2fc499SJeff Garzik  * but tasklet_schedule() doesn't.  hope the failure is rare.
4505b2fc499SJeff Garzik  */
4515b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
4525b2fc499SJeff Garzik {
4535b2fc499SJeff Garzik 	set_bit (work, &dev->flags);
454ab18a9c9SOliver Neukum 	if (!schedule_work (&dev->kevent))
455ab18a9c9SOliver Neukum 		netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
456ab18a9c9SOliver Neukum 	else
45760b86755SJoe Perches 		netdev_dbg(dev->net, "kevent %d scheduled\n", work);
4585b2fc499SJeff Garzik }
4595b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
4605b2fc499SJeff Garzik 
4615b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4625b2fc499SJeff Garzik 
4635b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
4645b2fc499SJeff Garzik 
465dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
4665b2fc499SJeff Garzik {
4675b2fc499SJeff Garzik 	struct sk_buff		*skb;
4685b2fc499SJeff Garzik 	struct skb_data		*entry;
4695b2fc499SJeff Garzik 	int			retval = 0;
4705b2fc499SJeff Garzik 	unsigned long		lockflags;
4715b2fc499SJeff Garzik 	size_t			size = dev->rx_urb_size;
4725b2fc499SJeff Garzik 
47370c37bf9SBjørn Mork 	/* prevent rx skb allocation when error ratio is high */
47470c37bf9SBjørn Mork 	if (test_bit(EVENT_RX_KILL, &dev->flags)) {
47570c37bf9SBjørn Mork 		usb_free_urb(urb);
47670c37bf9SBjørn Mork 		return -ENOLINK;
47770c37bf9SBjørn Mork 	}
47870c37bf9SBjørn Mork 
479a4abd7a8SBjørn Mork 	if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
480a4abd7a8SBjørn Mork 		skb = __netdev_alloc_skb(dev->net, size, flags);
481a4abd7a8SBjørn Mork 	else
4827bdd4027SEric Dumazet 		skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
4837bdd4027SEric Dumazet 	if (!skb) {
484a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
4855b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
4865b2fc499SJeff Garzik 		usb_free_urb (urb);
487dacb3975SDavid S. Miller 		return -ENOMEM;
4885b2fc499SJeff Garzik 	}
4895b2fc499SJeff Garzik 
4905b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
4915b2fc499SJeff Garzik 	entry->urb = urb;
4925b2fc499SJeff Garzik 	entry->dev = dev;
4935b2fc499SJeff Garzik 	entry->length = 0;
4945b2fc499SJeff Garzik 
4955b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
4965b2fc499SJeff Garzik 		skb->data, size, rx_complete, skb);
4975b2fc499SJeff Garzik 
4985b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
4995b2fc499SJeff Garzik 
5008e95a202SJoe Perches 	if (netif_running (dev->net) &&
5018e95a202SJoe Perches 	    netif_device_present (dev->net) &&
502ad70411aSKloetzke Jan 	    test_bit(EVENT_DEV_OPEN, &dev->flags) &&
50369ee472fSOliver Neukum 	    !test_bit (EVENT_RX_HALT, &dev->flags) &&
50469ee472fSOliver Neukum 	    !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
5055b2fc499SJeff Garzik 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
5065b2fc499SJeff Garzik 		case -EPIPE:
5075b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
5085b2fc499SJeff Garzik 			break;
5095b2fc499SJeff Garzik 		case -ENOMEM:
5105b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
5115b2fc499SJeff Garzik 			break;
5125b2fc499SJeff Garzik 		case -ENODEV:
513a475f603SJoe Perches 			netif_dbg(dev, ifdown, dev->net, "device gone\n");
5145b2fc499SJeff Garzik 			netif_device_detach (dev->net);
5155b2fc499SJeff Garzik 			break;
516dacb3975SDavid S. Miller 		case -EHOSTUNREACH:
517dacb3975SDavid S. Miller 			retval = -ENOLINK;
518dacb3975SDavid S. Miller 			break;
5195b2fc499SJeff Garzik 		default:
520a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
521a475f603SJoe Perches 				  "rx submit, %d\n", retval);
5225b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
5235b2fc499SJeff Garzik 			break;
5245b2fc499SJeff Garzik 		case 0:
5255b6e9bcdSMing Lei 			__usbnet_queue_skb(&dev->rxq, skb, rx_start);
5265b2fc499SJeff Garzik 		}
5275b2fc499SJeff Garzik 	} else {
528a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
5295b2fc499SJeff Garzik 		retval = -ENOLINK;
5305b2fc499SJeff Garzik 	}
5315b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
5325b2fc499SJeff Garzik 	if (retval) {
5335b2fc499SJeff Garzik 		dev_kfree_skb_any (skb);
5345b2fc499SJeff Garzik 		usb_free_urb (urb);
5355b2fc499SJeff Garzik 	}
536dacb3975SDavid S. Miller 	return retval;
5375b2fc499SJeff Garzik }
5385b2fc499SJeff Garzik 
5395b2fc499SJeff Garzik 
5405b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5415b2fc499SJeff Garzik 
5425b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
5435b2fc499SJeff Garzik {
5448e95a202SJoe Perches 	if (dev->driver_info->rx_fixup &&
5457a635ea9SAndrzej Zaborowski 	    !dev->driver_info->rx_fixup (dev, skb)) {
5467a635ea9SAndrzej Zaborowski 		/* With RX_ASSEMBLE, rx_fixup() must update counters */
5477a635ea9SAndrzej Zaborowski 		if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
5487a635ea9SAndrzej Zaborowski 			dev->net->stats.rx_errors++;
5497a635ea9SAndrzej Zaborowski 		goto done;
5507a635ea9SAndrzej Zaborowski 	}
5515b2fc499SJeff Garzik 	// else network stack removes extra byte if we forced a short packet
5525b2fc499SJeff Garzik 
553073285fdSAlexey Orishko 	/* all data was already cloned from skb inside the driver */
554073285fdSAlexey Orishko 	if (dev->driver_info->flags & FLAG_MULTI_PACKET)
555eb85569fSEmil Goode 		goto done;
556eb85569fSEmil Goode 
557eb85569fSEmil Goode 	if (skb->len < ETH_HLEN) {
558eb85569fSEmil Goode 		dev->net->stats.rx_errors++;
559eb85569fSEmil Goode 		dev->net->stats.rx_length_errors++;
560eb85569fSEmil Goode 		netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
561eb85569fSEmil Goode 	} else {
5625b2fc499SJeff Garzik 		usbnet_skb_return(dev, skb);
563073285fdSAlexey Orishko 		return;
564073285fdSAlexey Orishko 	}
565073285fdSAlexey Orishko 
5667a635ea9SAndrzej Zaborowski done:
5675b2fc499SJeff Garzik 	skb_queue_tail(&dev->done, skb);
5685b2fc499SJeff Garzik }
5695b2fc499SJeff Garzik 
5705b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5715b2fc499SJeff Garzik 
5725b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
5735b2fc499SJeff Garzik {
5745b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
5755b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
5765b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
5775b2fc499SJeff Garzik 	int			urb_status = urb->status;
5785b6e9bcdSMing Lei 	enum skb_state		state;
5795b2fc499SJeff Garzik 
5805b2fc499SJeff Garzik 	skb_put (skb, urb->actual_length);
5815b6e9bcdSMing Lei 	state = rx_done;
5825b2fc499SJeff Garzik 	entry->urb = NULL;
5835b2fc499SJeff Garzik 
5845b2fc499SJeff Garzik 	switch (urb_status) {
58518ab458fSDavid Brownell 	/* success */
5865b2fc499SJeff Garzik 	case 0:
5875b2fc499SJeff Garzik 		break;
5885b2fc499SJeff Garzik 
58918ab458fSDavid Brownell 	/* stalls need manual reset. this is rare ... except that
59018ab458fSDavid Brownell 	 * when going through USB 2.0 TTs, unplug appears this way.
5913ac49a1cSJean Delvare 	 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
59218ab458fSDavid Brownell 	 * storm, recovering as needed.
59318ab458fSDavid Brownell 	 */
5945b2fc499SJeff Garzik 	case -EPIPE:
5957963837fSHerbert Xu 		dev->net->stats.rx_errors++;
5965b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
5971a10d0bcSGustavo A. R. Silva 		fallthrough;
5985b2fc499SJeff Garzik 
59918ab458fSDavid Brownell 	/* software-driven interface shutdown */
60018ab458fSDavid Brownell 	case -ECONNRESET:		/* async unlink */
60118ab458fSDavid Brownell 	case -ESHUTDOWN:		/* hardware gone */
602a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net,
603a475f603SJoe Perches 			  "rx shutdown, code %d\n", urb_status);
6045b2fc499SJeff Garzik 		goto block;
6055b2fc499SJeff Garzik 
60637ebb549SPetr Mladek 	/* we get controller i/o faults during hub_wq disconnect() delays.
60718ab458fSDavid Brownell 	 * throttle down resubmits, to avoid log floods; just temporarily,
60837ebb549SPetr Mladek 	 * so we still recover when the fault isn't a hub_wq delay.
60918ab458fSDavid Brownell 	 */
6105b2fc499SJeff Garzik 	case -EPROTO:
6115b2fc499SJeff Garzik 	case -ETIME:
6125b2fc499SJeff Garzik 	case -EILSEQ:
6137963837fSHerbert Xu 		dev->net->stats.rx_errors++;
6145b2fc499SJeff Garzik 		if (!timer_pending (&dev->delay)) {
6155b2fc499SJeff Garzik 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
616a475f603SJoe Perches 			netif_dbg(dev, link, dev->net,
617a475f603SJoe Perches 				  "rx throttle %d\n", urb_status);
6185b2fc499SJeff Garzik 		}
6195b2fc499SJeff Garzik block:
6205b6e9bcdSMing Lei 		state = rx_cleanup;
6215b2fc499SJeff Garzik 		entry->urb = urb;
6225b2fc499SJeff Garzik 		urb = NULL;
6235b2fc499SJeff Garzik 		break;
6245b2fc499SJeff Garzik 
62518ab458fSDavid Brownell 	/* data overrun ... flush fifo? */
6265b2fc499SJeff Garzik 	case -EOVERFLOW:
6277963837fSHerbert Xu 		dev->net->stats.rx_over_errors++;
628df561f66SGustavo A. R. Silva 		fallthrough;
6295b2fc499SJeff Garzik 
6305b2fc499SJeff Garzik 	default:
6315b6e9bcdSMing Lei 		state = rx_cleanup;
6327963837fSHerbert Xu 		dev->net->stats.rx_errors++;
633a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
6345b2fc499SJeff Garzik 		break;
6355b2fc499SJeff Garzik 	}
6365b2fc499SJeff Garzik 
63770c37bf9SBjørn Mork 	/* stop rx if packet error rate is high */
63870c37bf9SBjørn Mork 	if (++dev->pkt_cnt > 30) {
63970c37bf9SBjørn Mork 		dev->pkt_cnt = 0;
64070c37bf9SBjørn Mork 		dev->pkt_err = 0;
64170c37bf9SBjørn Mork 	} else {
64270c37bf9SBjørn Mork 		if (state == rx_cleanup)
64370c37bf9SBjørn Mork 			dev->pkt_err++;
64470c37bf9SBjørn Mork 		if (dev->pkt_err > 20)
64570c37bf9SBjørn Mork 			set_bit(EVENT_RX_KILL, &dev->flags);
64670c37bf9SBjørn Mork 	}
64770c37bf9SBjørn Mork 
6485b6e9bcdSMing Lei 	state = defer_bh(dev, skb, &dev->rxq, state);
6495b2fc499SJeff Garzik 
6505b2fc499SJeff Garzik 	if (urb) {
6518e95a202SJoe Perches 		if (netif_running (dev->net) &&
6525b6e9bcdSMing Lei 		    !test_bit (EVENT_RX_HALT, &dev->flags) &&
6535b6e9bcdSMing Lei 		    state != unlink_start) {
6545b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_ATOMIC);
6558a783354SOliver Neukum 			usb_mark_last_busy(dev->udev);
6565b2fc499SJeff Garzik 			return;
6575b2fc499SJeff Garzik 		}
6585b2fc499SJeff Garzik 		usb_free_urb (urb);
6595b2fc499SJeff Garzik 	}
660a475f603SJoe Perches 	netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
6615b2fc499SJeff Garzik }
6625b2fc499SJeff Garzik 
6635b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
6647834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev)
6657834ddbcSJussi Kivilinna {
6667834ddbcSJussi Kivilinna 	set_bit(EVENT_RX_PAUSED, &dev->flags);
6677834ddbcSJussi Kivilinna 
668a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
6697834ddbcSJussi Kivilinna }
6707834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx);
6717834ddbcSJussi Kivilinna 
6727834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev)
6737834ddbcSJussi Kivilinna {
6747834ddbcSJussi Kivilinna 	struct sk_buff *skb;
6757834ddbcSJussi Kivilinna 	int num = 0;
6767834ddbcSJussi Kivilinna 
6777834ddbcSJussi Kivilinna 	clear_bit(EVENT_RX_PAUSED, &dev->flags);
6787834ddbcSJussi Kivilinna 
6797834ddbcSJussi Kivilinna 	while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
6807834ddbcSJussi Kivilinna 		usbnet_skb_return(dev, skb);
6817834ddbcSJussi Kivilinna 		num++;
6827834ddbcSJussi Kivilinna 	}
6837834ddbcSJussi Kivilinna 
6847834ddbcSJussi Kivilinna 	tasklet_schedule(&dev->bh);
6857834ddbcSJussi Kivilinna 
686a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net,
687a475f603SJoe Perches 		  "paused rx queue disabled, %d skbs requeued\n", num);
6887834ddbcSJussi Kivilinna }
6897834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx);
6907834ddbcSJussi Kivilinna 
6917834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev)
6927834ddbcSJussi Kivilinna {
6937834ddbcSJussi Kivilinna 	skb_queue_purge(&dev->rxq_pause);
6947834ddbcSJussi Kivilinna }
6957834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
6967834ddbcSJussi Kivilinna 
6977834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/
6985b2fc499SJeff Garzik 
6995b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
7005b2fc499SJeff Garzik 
7015b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
7025b2fc499SJeff Garzik {
7035b2fc499SJeff Garzik 	unsigned long		flags;
7045b6e9bcdSMing Lei 	struct sk_buff		*skb;
7055b2fc499SJeff Garzik 	int			count = 0;
7065b2fc499SJeff Garzik 
7075b2fc499SJeff Garzik 	spin_lock_irqsave (&q->lock, flags);
7085b6e9bcdSMing Lei 	while (!skb_queue_empty(q)) {
7095b2fc499SJeff Garzik 		struct skb_data		*entry;
7105b2fc499SJeff Garzik 		struct urb		*urb;
7115b2fc499SJeff Garzik 		int			retval;
7125b2fc499SJeff Garzik 
7135b6e9bcdSMing Lei 		skb_queue_walk(q, skb) {
7145b2fc499SJeff Garzik 			entry = (struct skb_data *) skb->cb;
7155b6e9bcdSMing Lei 			if (entry->state != unlink_start)
7165b6e9bcdSMing Lei 				goto found;
7175b6e9bcdSMing Lei 		}
7185b6e9bcdSMing Lei 		break;
7195b6e9bcdSMing Lei found:
7205b6e9bcdSMing Lei 		entry->state = unlink_start;
7215b2fc499SJeff Garzik 		urb = entry->urb;
7225b2fc499SJeff Garzik 
7230956a8c2Stom.leiming@gmail.com 		/*
7240956a8c2Stom.leiming@gmail.com 		 * Get reference count of the URB to avoid it to be
7250956a8c2Stom.leiming@gmail.com 		 * freed during usb_unlink_urb, which may trigger
7260956a8c2Stom.leiming@gmail.com 		 * use-after-free problem inside usb_unlink_urb since
7270956a8c2Stom.leiming@gmail.com 		 * usb_unlink_urb is always racing with .complete
7280956a8c2Stom.leiming@gmail.com 		 * handler(include defer_bh).
7290956a8c2Stom.leiming@gmail.com 		 */
7300956a8c2Stom.leiming@gmail.com 		usb_get_urb(urb);
7314231d47eSSebastian Siewior 		spin_unlock_irqrestore(&q->lock, flags);
7325b2fc499SJeff Garzik 		// during some PM-driven resume scenarios,
7335b2fc499SJeff Garzik 		// these (async) unlinks complete immediately
7345b2fc499SJeff Garzik 		retval = usb_unlink_urb (urb);
7355b2fc499SJeff Garzik 		if (retval != -EINPROGRESS && retval != 0)
73660b86755SJoe Perches 			netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
7375b2fc499SJeff Garzik 		else
7385b2fc499SJeff Garzik 			count++;
7390956a8c2Stom.leiming@gmail.com 		usb_put_urb(urb);
7404231d47eSSebastian Siewior 		spin_lock_irqsave(&q->lock, flags);
7415b2fc499SJeff Garzik 	}
7425b2fc499SJeff Garzik 	spin_unlock_irqrestore (&q->lock, flags);
7435b2fc499SJeff Garzik 	return count;
7445b2fc499SJeff Garzik }
7455b2fc499SJeff Garzik 
7465b2fc499SJeff Garzik // Flush all pending rx urbs
7475b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
7485b2fc499SJeff Garzik 
7495b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
7505b2fc499SJeff Garzik {
7515b2fc499SJeff Garzik 	if (netif_running(dev->net)) {
7525b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
7535b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
7545b2fc499SJeff Garzik 	}
7555b2fc499SJeff Garzik }
7565b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
7575b2fc499SJeff Garzik 
7585b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
7595b2fc499SJeff Garzik 
760fcb0bb6aSEugene Shatokhin static void wait_skb_queue_empty(struct sk_buff_head *q)
761fcb0bb6aSEugene Shatokhin {
762fcb0bb6aSEugene Shatokhin 	unsigned long flags;
763fcb0bb6aSEugene Shatokhin 
764fcb0bb6aSEugene Shatokhin 	spin_lock_irqsave(&q->lock, flags);
765fcb0bb6aSEugene Shatokhin 	while (!skb_queue_empty(q)) {
766fcb0bb6aSEugene Shatokhin 		spin_unlock_irqrestore(&q->lock, flags);
767fcb0bb6aSEugene Shatokhin 		schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
768fcb0bb6aSEugene Shatokhin 		set_current_state(TASK_UNINTERRUPTIBLE);
769fcb0bb6aSEugene Shatokhin 		spin_lock_irqsave(&q->lock, flags);
770fcb0bb6aSEugene Shatokhin 	}
771fcb0bb6aSEugene Shatokhin 	spin_unlock_irqrestore(&q->lock, flags);
772fcb0bb6aSEugene Shatokhin }
773fcb0bb6aSEugene Shatokhin 
7745b2fc499SJeff Garzik // precondition: never called in_interrupt
77569ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev)
77669ee472fSOliver Neukum {
77769ee472fSOliver Neukum 	DECLARE_WAITQUEUE(wait, current);
77869ee472fSOliver Neukum 	int temp;
77969ee472fSOliver Neukum 
78069ee472fSOliver Neukum 	/* ensure there are no more active urbs */
78114a0d635SOliver Neukum 	add_wait_queue(&dev->wait, &wait);
78269ee472fSOliver Neukum 	set_current_state(TASK_UNINTERRUPTIBLE);
78369ee472fSOliver Neukum 	temp = unlink_urbs(dev, &dev->txq) +
78469ee472fSOliver Neukum 		unlink_urbs(dev, &dev->rxq);
78569ee472fSOliver Neukum 
78669ee472fSOliver Neukum 	/* maybe wait for deletions to finish. */
787fcb0bb6aSEugene Shatokhin 	wait_skb_queue_empty(&dev->rxq);
788fcb0bb6aSEugene Shatokhin 	wait_skb_queue_empty(&dev->txq);
789fcb0bb6aSEugene Shatokhin 	wait_skb_queue_empty(&dev->done);
790a475f603SJoe Perches 	netif_dbg(dev, ifdown, dev->net,
791a475f603SJoe Perches 		  "waited for %d urb completions\n", temp);
79269ee472fSOliver Neukum 	set_current_state(TASK_RUNNING);
79314a0d635SOliver Neukum 	remove_wait_queue(&dev->wait, &wait);
79469ee472fSOliver Neukum }
7955b2fc499SJeff Garzik 
796777baa47SStephen Hemminger int usbnet_stop (struct net_device *net)
7975b2fc499SJeff Garzik {
7985b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
799f3edc2dbSBen Dooks 	const struct driver_info *info = dev->driver_info;
800f50791acSEugene Shatokhin 	int			retval, pm, mpn;
8015b2fc499SJeff Garzik 
80275bd0cbdSMing Lei 	clear_bit(EVENT_DEV_OPEN, &dev->flags);
8035b2fc499SJeff Garzik 	netif_stop_queue (net);
8045b2fc499SJeff Garzik 
805a475f603SJoe Perches 	netif_info(dev, ifdown, dev->net,
8068ccef431SBen Hutchings 		   "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
8077963837fSHerbert Xu 		   net->stats.rx_packets, net->stats.tx_packets,
80860b86755SJoe Perches 		   net->stats.rx_errors, net->stats.tx_errors);
8095b2fc499SJeff Garzik 
81014a0d635SOliver Neukum 	/* to not race resume */
81114a0d635SOliver Neukum 	pm = usb_autopm_get_interface(dev->intf);
812a33e9e7fSJussi Kivilinna 	/* allow minidriver to stop correctly (wireless devices to turn off
813a33e9e7fSJussi Kivilinna 	 * radio etc) */
814a33e9e7fSJussi Kivilinna 	if (info->stop) {
815a33e9e7fSJussi Kivilinna 		retval = info->stop(dev);
816a475f603SJoe Perches 		if (retval < 0)
817a475f603SJoe Perches 			netif_info(dev, ifdown, dev->net,
81860b86755SJoe Perches 				   "stop fail (%d) usbnet usb-%s-%s, %s\n",
819a33e9e7fSJussi Kivilinna 				   retval,
820a33e9e7fSJussi Kivilinna 				   dev->udev->bus->bus_name, dev->udev->devpath,
821a33e9e7fSJussi Kivilinna 				   info->description);
822a33e9e7fSJussi Kivilinna 	}
823a33e9e7fSJussi Kivilinna 
82469ee472fSOliver Neukum 	if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
82569ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
8265b2fc499SJeff Garzik 
8276eecdc5fSDan Williams 	usbnet_status_stop(dev);
8285b2fc499SJeff Garzik 
8297834ddbcSJussi Kivilinna 	usbnet_purge_paused_rxq(dev);
8307834ddbcSJussi Kivilinna 
831f50791acSEugene Shatokhin 	mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
832f50791acSEugene Shatokhin 
8335b2fc499SJeff Garzik 	/* deferred work (task, timer, softirq) must also stop.
8345b2fc499SJeff Garzik 	 * can't flush_scheduled_work() until we drop rtnl (later),
8355b2fc499SJeff Garzik 	 * else workers could deadlock; so make workers a NOP.
8365b2fc499SJeff Garzik 	 */
8375b2fc499SJeff Garzik 	dev->flags = 0;
8385b2fc499SJeff Garzik 	del_timer_sync (&dev->delay);
8395b2fc499SJeff Garzik 	tasklet_kill (&dev->bh);
84014a0d635SOliver Neukum 	if (!pm)
84114a0d635SOliver Neukum 		usb_autopm_put_interface(dev->intf);
84214a0d635SOliver Neukum 
843f50791acSEugene Shatokhin 	if (info->manage_power && mpn)
84469ee472fSOliver Neukum 		info->manage_power(dev, 0);
84569ee472fSOliver Neukum 	else
846a11a6544SOliver Neukum 		usb_autopm_put_interface(dev->intf);
8475b2fc499SJeff Garzik 
8485b2fc499SJeff Garzik 	return 0;
8495b2fc499SJeff Garzik }
850777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop);
8515b2fc499SJeff Garzik 
8525b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
8535b2fc499SJeff Garzik 
8545b2fc499SJeff Garzik // posts reads, and enables write queuing
8555b2fc499SJeff Garzik 
8565b2fc499SJeff Garzik // precondition: never called in_interrupt
8575b2fc499SJeff Garzik 
858777baa47SStephen Hemminger int usbnet_open (struct net_device *net)
8595b2fc499SJeff Garzik {
8605b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
861a11a6544SOliver Neukum 	int			retval;
862f3edc2dbSBen Dooks 	const struct driver_info *info = dev->driver_info;
8635b2fc499SJeff Garzik 
864a11a6544SOliver Neukum 	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
865a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
86660b86755SJoe Perches 			   "resumption fail (%d) usbnet usb-%s-%s, %s\n",
867a11a6544SOliver Neukum 			   retval,
86860b86755SJoe Perches 			   dev->udev->bus->bus_name,
86960b86755SJoe Perches 			   dev->udev->devpath,
870a11a6544SOliver Neukum 			   info->description);
871a11a6544SOliver Neukum 		goto done_nopm;
872a11a6544SOliver Neukum 	}
873a11a6544SOliver Neukum 
8745b2fc499SJeff Garzik 	// put into "known safe" state
8755b2fc499SJeff Garzik 	if (info->reset && (retval = info->reset (dev)) < 0) {
876a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
87760b86755SJoe Perches 			   "open reset fail (%d) usbnet usb-%s-%s, %s\n",
8785b2fc499SJeff Garzik 			   retval,
87960b86755SJoe Perches 			   dev->udev->bus->bus_name,
88060b86755SJoe Perches 			   dev->udev->devpath,
8815b2fc499SJeff Garzik 			   info->description);
8825b2fc499SJeff Garzik 		goto done;
8835b2fc499SJeff Garzik 	}
8845b2fc499SJeff Garzik 
885a88c32aeSMing Lei 	/* hard_mtu or rx_urb_size may change in reset() */
886a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
887a88c32aeSMing Lei 
8885b2fc499SJeff Garzik 	// insist peer be connected
8895b2fc499SJeff Garzik 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
890*4d8c79b7SGrant Grundler 		netif_err(dev, ifup, dev->net, "can't open; %d\n", retval);
8915b2fc499SJeff Garzik 		goto done;
8925b2fc499SJeff Garzik 	}
8935b2fc499SJeff Garzik 
8945b2fc499SJeff Garzik 	/* start any status interrupt transfer */
8955b2fc499SJeff Garzik 	if (dev->interrupt) {
8966eecdc5fSDan Williams 		retval = usbnet_status_start(dev, GFP_KERNEL);
8975b2fc499SJeff Garzik 		if (retval < 0) {
898a475f603SJoe Perches 			netif_err(dev, ifup, dev->net,
899a475f603SJoe Perches 				  "intr submit %d\n", retval);
9005b2fc499SJeff Garzik 			goto done;
9015b2fc499SJeff Garzik 		}
9025b2fc499SJeff Garzik 	}
9035b2fc499SJeff Garzik 
90468972efaSPaul Stewart 	set_bit(EVENT_DEV_OPEN, &dev->flags);
9055b2fc499SJeff Garzik 	netif_start_queue (net);
906a475f603SJoe Perches 	netif_info(dev, ifup, dev->net,
907a475f603SJoe Perches 		   "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
90860b86755SJoe Perches 		   (int)RX_QLEN(dev), (int)TX_QLEN(dev),
909a475f603SJoe Perches 		   dev->net->mtu,
910a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
911a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
912a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
913a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
914a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
915a475f603SJoe Perches 		   "simple");
9165b2fc499SJeff Garzik 
91770c37bf9SBjørn Mork 	/* reset rx error state */
91870c37bf9SBjørn Mork 	dev->pkt_cnt = 0;
91970c37bf9SBjørn Mork 	dev->pkt_err = 0;
92070c37bf9SBjørn Mork 	clear_bit(EVENT_RX_KILL, &dev->flags);
92170c37bf9SBjørn Mork 
9225b2fc499SJeff Garzik 	// delay posting reads until we're fully open
9235b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
92469ee472fSOliver Neukum 	if (info->manage_power) {
92569ee472fSOliver Neukum 		retval = info->manage_power(dev, 1);
926a1c088e0SOliver Neukum 		if (retval < 0) {
927a1c088e0SOliver Neukum 			retval = 0;
928a1c088e0SOliver Neukum 			set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
929a1c088e0SOliver Neukum 		} else {
93069ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
93169ee472fSOliver Neukum 		}
932a1c088e0SOliver Neukum 	}
933a11a6544SOliver Neukum 	return retval;
9345b2fc499SJeff Garzik done:
935a11a6544SOliver Neukum 	usb_autopm_put_interface(dev->intf);
936a11a6544SOliver Neukum done_nopm:
9375b2fc499SJeff Garzik 	return retval;
9385b2fc499SJeff Garzik }
939777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open);
9405b2fc499SJeff Garzik 
9415b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9425b2fc499SJeff Garzik 
9435b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
9445b2fc499SJeff Garzik  * they'll probably want to use this base set.
9455b2fc499SJeff Garzik  */
9465b2fc499SJeff Garzik 
9478bae3551SPhilippe Reynes int usbnet_get_link_ksettings(struct net_device *net,
9488bae3551SPhilippe Reynes 			      struct ethtool_link_ksettings *cmd)
9498bae3551SPhilippe Reynes {
9508bae3551SPhilippe Reynes 	struct usbnet *dev = netdev_priv(net);
9518bae3551SPhilippe Reynes 
9528bae3551SPhilippe Reynes 	if (!dev->mii.mdio_read)
9538bae3551SPhilippe Reynes 		return -EOPNOTSUPP;
9548bae3551SPhilippe Reynes 
95582c01a84Syuval.shaia@oracle.com 	mii_ethtool_get_link_ksettings(&dev->mii, cmd);
95682c01a84Syuval.shaia@oracle.com 
95782c01a84Syuval.shaia@oracle.com 	return 0;
9588bae3551SPhilippe Reynes }
9598bae3551SPhilippe Reynes EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
9608bae3551SPhilippe Reynes 
9618bae3551SPhilippe Reynes int usbnet_set_link_ksettings(struct net_device *net,
9628bae3551SPhilippe Reynes 			      const struct ethtool_link_ksettings *cmd)
9638bae3551SPhilippe Reynes {
9648bae3551SPhilippe Reynes 	struct usbnet *dev = netdev_priv(net);
9658bae3551SPhilippe Reynes 	int retval;
9668bae3551SPhilippe Reynes 
9678bae3551SPhilippe Reynes 	if (!dev->mii.mdio_write)
9688bae3551SPhilippe Reynes 		return -EOPNOTSUPP;
9698bae3551SPhilippe Reynes 
9708bae3551SPhilippe Reynes 	retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
9718bae3551SPhilippe Reynes 
9728bae3551SPhilippe Reynes 	/* link speed/duplex might have changed */
9738bae3551SPhilippe Reynes 	if (dev->driver_info->link_reset)
9748bae3551SPhilippe Reynes 		dev->driver_info->link_reset(dev);
9758bae3551SPhilippe Reynes 
9768bae3551SPhilippe Reynes 	/* hard_mtu or rx_urb_size may change in link_reset() */
9778bae3551SPhilippe Reynes 	usbnet_update_max_qlen(dev);
9788bae3551SPhilippe Reynes 
9798bae3551SPhilippe Reynes 	return retval;
9808bae3551SPhilippe Reynes }
9818bae3551SPhilippe Reynes EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
9828bae3551SPhilippe Reynes 
9835b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
9845b2fc499SJeff Garzik {
9855b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
9865b2fc499SJeff Garzik 
9875b2fc499SJeff Garzik 	/* If a check_connect is defined, return its result */
9885b2fc499SJeff Garzik 	if (dev->driver_info->check_connect)
9895b2fc499SJeff Garzik 		return dev->driver_info->check_connect (dev) == 0;
9905b2fc499SJeff Garzik 
9915b2fc499SJeff Garzik 	/* if the device has mii operations, use those */
9925b2fc499SJeff Garzik 	if (dev->mii.mdio_read)
9935b2fc499SJeff Garzik 		return mii_link_ok(&dev->mii);
9945b2fc499SJeff Garzik 
99505ffb3e2SBjørn Mork 	/* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
99605ffb3e2SBjørn Mork 	return ethtool_op_get_link(net);
9975b2fc499SJeff Garzik }
9985b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
9995b2fc499SJeff Garzik 
10005b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
10015b2fc499SJeff Garzik {
10025b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10035b2fc499SJeff Garzik 
10045b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
10055b2fc499SJeff Garzik 		return -EOPNOTSUPP;
10065b2fc499SJeff Garzik 
10075b2fc499SJeff Garzik 	return mii_nway_restart(&dev->mii);
10085b2fc499SJeff Garzik }
10095b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
10105b2fc499SJeff Garzik 
10115b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
10125b2fc499SJeff Garzik {
10135b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10145b2fc499SJeff Garzik 
101586a2f415SPhil Sutter 	strlcpy (info->driver, dev->driver_name, sizeof info->driver);
101686a2f415SPhil Sutter 	strlcpy (info->fw_version, dev->driver_info->description,
10175b2fc499SJeff Garzik 		sizeof info->fw_version);
10185b2fc499SJeff Garzik 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
10195b2fc499SJeff Garzik }
10205b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
10215b2fc499SJeff Garzik 
10225b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
10235b2fc499SJeff Garzik {
10245b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10255b2fc499SJeff Garzik 
10265b2fc499SJeff Garzik 	return dev->msg_enable;
10275b2fc499SJeff Garzik }
10285b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
10295b2fc499SJeff Garzik 
10305b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
10315b2fc499SJeff Garzik {
10325b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10335b2fc499SJeff Garzik 
10345b2fc499SJeff Garzik 	dev->msg_enable = level;
10355b2fc499SJeff Garzik }
10365b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
10375b2fc499SJeff Garzik 
10385b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
10390fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = {
10405b2fc499SJeff Garzik 	.get_link		= usbnet_get_link,
10415b2fc499SJeff Garzik 	.nway_reset		= usbnet_nway_reset,
10425b2fc499SJeff Garzik 	.get_drvinfo		= usbnet_get_drvinfo,
10435b2fc499SJeff Garzik 	.get_msglevel		= usbnet_get_msglevel,
10445b2fc499SJeff Garzik 	.set_msglevel		= usbnet_set_msglevel,
1045123edb18SRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
10468bae3551SPhilippe Reynes 	.get_link_ksettings	= usbnet_get_link_ksettings,
10478bae3551SPhilippe Reynes 	.set_link_ksettings	= usbnet_set_link_ksettings,
10485b2fc499SJeff Garzik };
10495b2fc499SJeff Garzik 
10505b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10515b2fc499SJeff Garzik 
10524b49f58fSMing Lei static void __handle_link_change(struct usbnet *dev)
10534b49f58fSMing Lei {
10544b49f58fSMing Lei 	if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
10554b49f58fSMing Lei 		return;
10564b49f58fSMing Lei 
10574b49f58fSMing Lei 	if (!netif_carrier_ok(dev->net)) {
10584b49f58fSMing Lei 		/* kill URBs for reading packets to save bus bandwidth */
10594b49f58fSMing Lei 		unlink_urbs(dev, &dev->rxq);
10604b49f58fSMing Lei 
10614b49f58fSMing Lei 		/*
10624b49f58fSMing Lei 		 * tx_timeout will unlink URBs for sending packets and
10634b49f58fSMing Lei 		 * tx queue is stopped by netcore after link becomes off
10644b49f58fSMing Lei 		 */
10654b49f58fSMing Lei 	} else {
10664b49f58fSMing Lei 		/* submitting URBs for reading packets */
10674b49f58fSMing Lei 		tasklet_schedule(&dev->bh);
10684b49f58fSMing Lei 	}
10694b49f58fSMing Lei 
1070a88c32aeSMing Lei 	/* hard_mtu or rx_urb_size may change during link change */
1071a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
1072a88c32aeSMing Lei 
10734b49f58fSMing Lei 	clear_bit(EVENT_LINK_CHANGE, &dev->flags);
10744b49f58fSMing Lei }
10754b49f58fSMing Lei 
10761ea2b748SBjørn Mork void usbnet_set_rx_mode(struct net_device *net)
10771efed2d0SOlivier Blin {
10781efed2d0SOlivier Blin 	struct usbnet		*dev = netdev_priv(net);
10791efed2d0SOlivier Blin 
10801efed2d0SOlivier Blin 	usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
10811efed2d0SOlivier Blin }
10821ea2b748SBjørn Mork EXPORT_SYMBOL_GPL(usbnet_set_rx_mode);
10831efed2d0SOlivier Blin 
10841efed2d0SOlivier Blin static void __handle_set_rx_mode(struct usbnet *dev)
10851efed2d0SOlivier Blin {
10861efed2d0SOlivier Blin 	if (dev->driver_info->set_rx_mode)
10871efed2d0SOlivier Blin 		(dev->driver_info->set_rx_mode)(dev);
10881efed2d0SOlivier Blin 
10891efed2d0SOlivier Blin 	clear_bit(EVENT_SET_RX_MODE, &dev->flags);
10901efed2d0SOlivier Blin }
10911efed2d0SOlivier Blin 
10925b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
10935b2fc499SJeff Garzik  *
10945b2fc499SJeff Garzik  * NOTE:  with 2.5 we could do more of this using completion callbacks,
10955b2fc499SJeff Garzik  * especially now that control transfers can be queued.
10965b2fc499SJeff Garzik  */
10975b2fc499SJeff Garzik static void
109811f17ef3SOliver Neukum usbnet_deferred_kevent (struct work_struct *work)
10995b2fc499SJeff Garzik {
11005b2fc499SJeff Garzik 	struct usbnet		*dev =
11015b2fc499SJeff Garzik 		container_of(work, struct usbnet, kevent);
11025b2fc499SJeff Garzik 	int			status;
11035b2fc499SJeff Garzik 
11045b2fc499SJeff Garzik 	/* usb_clear_halt() needs a thread context */
11055b2fc499SJeff Garzik 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
11065b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->txq);
110769ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
110869ee472fSOliver Neukum 		if (status < 0)
110969ee472fSOliver Neukum 			goto fail_pipe;
11105b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->out);
111169ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
11128e95a202SJoe Perches 		if (status < 0 &&
11138e95a202SJoe Perches 		    status != -EPIPE &&
11148e95a202SJoe Perches 		    status != -ESHUTDOWN) {
11155b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
111669ee472fSOliver Neukum fail_pipe:
111760b86755SJoe Perches 				netdev_err(dev->net, "can't clear tx halt, status %d\n",
11185b2fc499SJeff Garzik 					   status);
11195b2fc499SJeff Garzik 		} else {
11205b2fc499SJeff Garzik 			clear_bit (EVENT_TX_HALT, &dev->flags);
11215b2fc499SJeff Garzik 			if (status != -ESHUTDOWN)
11225b2fc499SJeff Garzik 				netif_wake_queue (dev->net);
11235b2fc499SJeff Garzik 		}
11245b2fc499SJeff Garzik 	}
11255b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
11265b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->rxq);
112769ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
112869ee472fSOliver Neukum 		if (status < 0)
112969ee472fSOliver Neukum 			goto fail_halt;
11305b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->in);
113169ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
11328e95a202SJoe Perches 		if (status < 0 &&
11338e95a202SJoe Perches 		    status != -EPIPE &&
11348e95a202SJoe Perches 		    status != -ESHUTDOWN) {
11355b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
113669ee472fSOliver Neukum fail_halt:
113760b86755SJoe Perches 				netdev_err(dev->net, "can't clear rx halt, status %d\n",
11385b2fc499SJeff Garzik 					   status);
11395b2fc499SJeff Garzik 		} else {
11405b2fc499SJeff Garzik 			clear_bit (EVENT_RX_HALT, &dev->flags);
11415b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
11425b2fc499SJeff Garzik 		}
11435b2fc499SJeff Garzik 	}
11445b2fc499SJeff Garzik 
11455b2fc499SJeff Garzik 	/* tasklet could resubmit itself forever if memory is tight */
11465b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
11475b2fc499SJeff Garzik 		struct urb	*urb = NULL;
1148dacb3975SDavid S. Miller 		int resched = 1;
11495b2fc499SJeff Garzik 
11505b2fc499SJeff Garzik 		if (netif_running (dev->net))
11515b2fc499SJeff Garzik 			urb = usb_alloc_urb (0, GFP_KERNEL);
11525b2fc499SJeff Garzik 		else
11535b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
11545b2fc499SJeff Garzik 		if (urb != NULL) {
11555b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
115669ee472fSOliver Neukum 			status = usb_autopm_get_interface(dev->intf);
1157ab60707fSJesper Juhl 			if (status < 0) {
1158ab60707fSJesper Juhl 				usb_free_urb(urb);
115969ee472fSOliver Neukum 				goto fail_lowmem;
1160ab60707fSJesper Juhl 			}
1161dacb3975SDavid S. Miller 			if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1162dacb3975SDavid S. Miller 				resched = 0;
116369ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
116469ee472fSOliver Neukum fail_lowmem:
1165dacb3975SDavid S. Miller 			if (resched)
11665b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
11675b2fc499SJeff Garzik 		}
11685b2fc499SJeff Garzik 	}
11695b2fc499SJeff Garzik 
11705b2fc499SJeff Garzik 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1171f3edc2dbSBen Dooks 		const struct driver_info *info = dev->driver_info;
11725b2fc499SJeff Garzik 		int			retval = 0;
11735b2fc499SJeff Garzik 
11745b2fc499SJeff Garzik 		clear_bit (EVENT_LINK_RESET, &dev->flags);
117569ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
117669ee472fSOliver Neukum 		if (status < 0)
117769ee472fSOliver Neukum 			goto skip_reset;
11785b2fc499SJeff Garzik 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
117969ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
118069ee472fSOliver Neukum skip_reset:
118160b86755SJoe Perches 			netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
11825b2fc499SJeff Garzik 				    retval,
118360b86755SJoe Perches 				    dev->udev->bus->bus_name,
118460b86755SJoe Perches 				    dev->udev->devpath,
11855b2fc499SJeff Garzik 				    info->description);
118669ee472fSOliver Neukum 		} else {
118769ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
11885b2fc499SJeff Garzik 		}
11894b49f58fSMing Lei 
11904b49f58fSMing Lei 		/* handle link change from link resetting */
11914b49f58fSMing Lei 		__handle_link_change(dev);
11925b2fc499SJeff Garzik 	}
11935b2fc499SJeff Garzik 
11944b49f58fSMing Lei 	if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
11954b49f58fSMing Lei 		__handle_link_change(dev);
11964b49f58fSMing Lei 
11971efed2d0SOlivier Blin 	if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
11981efed2d0SOlivier Blin 		__handle_set_rx_mode(dev);
11991efed2d0SOlivier Blin 
12001efed2d0SOlivier Blin 
12015b2fc499SJeff Garzik 	if (dev->flags)
120260b86755SJoe Perches 		netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
12035b2fc499SJeff Garzik }
12045b2fc499SJeff Garzik 
12055b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12065b2fc499SJeff Garzik 
12075b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
12085b2fc499SJeff Garzik {
12095b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
12105b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
12115b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
12125b2fc499SJeff Garzik 
12135b2fc499SJeff Garzik 	if (urb->status == 0) {
1214af0c351cSHeiner Kallweit 		struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats);
12152695578bSEric Dumazet 		unsigned long flags;
1216c8b5d129SGreg Ungerer 
12172695578bSEric Dumazet 		flags = u64_stats_update_begin_irqsave(&stats64->syncp);
1218c8b5d129SGreg Ungerer 		stats64->tx_packets += entry->packets;
1219c8b5d129SGreg Ungerer 		stats64->tx_bytes += entry->length;
12202695578bSEric Dumazet 		u64_stats_update_end_irqrestore(&stats64->syncp, flags);
12215b2fc499SJeff Garzik 	} else {
12227963837fSHerbert Xu 		dev->net->stats.tx_errors++;
12235b2fc499SJeff Garzik 
12245b2fc499SJeff Garzik 		switch (urb->status) {
12255b2fc499SJeff Garzik 		case -EPIPE:
12265b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
12275b2fc499SJeff Garzik 			break;
12285b2fc499SJeff Garzik 
12295b2fc499SJeff Garzik 		/* software-driven interface shutdown */
12305b2fc499SJeff Garzik 		case -ECONNRESET:		// async unlink
12315b2fc499SJeff Garzik 		case -ESHUTDOWN:		// hardware gone
12325b2fc499SJeff Garzik 			break;
12335b2fc499SJeff Garzik 
123437ebb549SPetr Mladek 		/* like rx, tx gets controller i/o faults during hub_wq
123537ebb549SPetr Mladek 		 * delays and so it uses the same throttling mechanism.
123637ebb549SPetr Mladek 		 */
12375b2fc499SJeff Garzik 		case -EPROTO:
12385b2fc499SJeff Garzik 		case -ETIME:
12395b2fc499SJeff Garzik 		case -EILSEQ:
124069ee472fSOliver Neukum 			usb_mark_last_busy(dev->udev);
12415b2fc499SJeff Garzik 			if (!timer_pending (&dev->delay)) {
12425b2fc499SJeff Garzik 				mod_timer (&dev->delay,
12435b2fc499SJeff Garzik 					jiffies + THROTTLE_JIFFIES);
1244a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1245a475f603SJoe Perches 					  "tx throttle %d\n", urb->status);
12465b2fc499SJeff Garzik 			}
12475b2fc499SJeff Garzik 			netif_stop_queue (dev->net);
12485b2fc499SJeff Garzik 			break;
12495b2fc499SJeff Garzik 		default:
1250a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net,
1251a475f603SJoe Perches 				  "tx err %d\n", entry->urb->status);
12525b2fc499SJeff Garzik 			break;
12535b2fc499SJeff Garzik 		}
12545b2fc499SJeff Garzik 	}
12555b2fc499SJeff Garzik 
125669ee472fSOliver Neukum 	usb_autopm_put_interface_async(dev->intf);
12575b6e9bcdSMing Lei 	(void) defer_bh(dev, skb, &dev->txq, tx_done);
12585b2fc499SJeff Garzik }
12595b2fc499SJeff Garzik 
12605b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12615b2fc499SJeff Garzik 
12620290bd29SMichael S. Tsirkin void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue)
12635b2fc499SJeff Garzik {
12645b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
12655b2fc499SJeff Garzik 
12665b2fc499SJeff Garzik 	unlink_urbs (dev, &dev->txq);
12675b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
1268dbcdd4d5SOliver Neukum 	/* this needs to be handled individually because the generic layer
1269dbcdd4d5SOliver Neukum 	 * doesn't know what is sufficient and could not restore private
1270dbcdd4d5SOliver Neukum 	 * information if a remedy of an unconditional reset were used.
1271dbcdd4d5SOliver Neukum 	 */
1272dbcdd4d5SOliver Neukum 	if (dev->driver_info->recover)
1273dbcdd4d5SOliver Neukum 		(dev->driver_info->recover)(dev);
12745b2fc499SJeff Garzik }
1275777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
12765b2fc499SJeff Garzik 
12775b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12785b2fc499SJeff Garzik 
1279638c5115SMing Lei static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1280638c5115SMing Lei {
1281638c5115SMing Lei 	unsigned num_sgs, total_len = 0;
1282638c5115SMing Lei 	int i, s = 0;
1283638c5115SMing Lei 
1284638c5115SMing Lei 	num_sgs = skb_shinfo(skb)->nr_frags + 1;
1285638c5115SMing Lei 	if (num_sgs == 1)
1286638c5115SMing Lei 		return 0;
1287638c5115SMing Lei 
128860e453a9SMing Lei 	/* reserve one for zero packet */
12896da2ec56SKees Cook 	urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
129060e453a9SMing Lei 				GFP_ATOMIC);
1291638c5115SMing Lei 	if (!urb->sg)
1292638c5115SMing Lei 		return -ENOMEM;
1293638c5115SMing Lei 
1294638c5115SMing Lei 	urb->num_sgs = num_sgs;
1295fdc3452cSBjørn Mork 	sg_init_table(urb->sg, urb->num_sgs + 1);
1296638c5115SMing Lei 
1297638c5115SMing Lei 	sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1298638c5115SMing Lei 	total_len += skb_headlen(skb);
1299638c5115SMing Lei 
1300638c5115SMing Lei 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1301d7840976SMatthew Wilcox (Oracle) 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1302638c5115SMing Lei 
1303638c5115SMing Lei 		total_len += skb_frag_size(f);
1304d7840976SMatthew Wilcox (Oracle) 		sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f),
1305b54c9d5bSJonathan Lemon 			    skb_frag_off(f));
1306638c5115SMing Lei 	}
1307638c5115SMing Lei 	urb->transfer_buffer_length = total_len;
1308638c5115SMing Lei 
1309638c5115SMing Lei 	return 1;
1310638c5115SMing Lei }
1311638c5115SMing Lei 
131225a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
131325a79c41SStephen Hemminger 				     struct net_device *net)
13145b2fc499SJeff Garzik {
13155b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
13163e4336a6SJason A. Donenfeld 	unsigned int			length;
13175b2fc499SJeff Garzik 	struct urb		*urb = NULL;
13185b2fc499SJeff Garzik 	struct skb_data		*entry;
1319f3edc2dbSBen Dooks 	const struct driver_info *info = dev->driver_info;
13205b2fc499SJeff Garzik 	unsigned long		flags;
132125a79c41SStephen Hemminger 	int retval;
13225b2fc499SJeff Garzik 
132323ba0799SKonstantin Khlebnikov 	if (skb)
1324f9b491ecSMichael Riesch 		skb_tx_timestamp(skb);
1325f9b491ecSMichael Riesch 
13265b2fc499SJeff Garzik 	// some devices want funky USB-level framing, for
13275b2fc499SJeff Garzik 	// win32 driver (usually) and/or hardware quirks
13285b2fc499SJeff Garzik 	if (info->tx_fixup) {
13295b2fc499SJeff Garzik 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
13305b2fc499SJeff Garzik 		if (!skb) {
1331bf414b36SBjørn Mork 			/* packet collected; minidriver waiting for more */
1332bf414b36SBjørn Mork 			if (info->flags & FLAG_MULTI_PACKET)
1333bf414b36SBjørn Mork 				goto not_drop;
1334a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
13355b2fc499SJeff Garzik 			goto drop;
13365b2fc499SJeff Garzik 		}
13375b2fc499SJeff Garzik 	}
13385b2fc499SJeff Garzik 
13395b2fc499SJeff Garzik 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1340a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "no urb\n");
13415b2fc499SJeff Garzik 		goto drop;
13425b2fc499SJeff Garzik 	}
13435b2fc499SJeff Garzik 
13445b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
13455b2fc499SJeff Garzik 	entry->urb = urb;
13465b2fc499SJeff Garzik 	entry->dev = dev;
13475b2fc499SJeff Garzik 
13485b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
13495b2fc499SJeff Garzik 			skb->data, skb->len, tx_complete, skb);
1350638c5115SMing Lei 	if (dev->can_dma_sg) {
1351638c5115SMing Lei 		if (build_dma_sg(skb, urb) < 0)
1352638c5115SMing Lei 			goto drop;
1353638c5115SMing Lei 	}
135460e453a9SMing Lei 	length = urb->transfer_buffer_length;
13555b2fc499SJeff Garzik 
13565b2fc499SJeff Garzik 	/* don't assume the hardware handles USB_ZERO_PACKET
13575b2fc499SJeff Garzik 	 * NOTE:  strictly conforming cdc-ether devices should expect
13585b2fc499SJeff Garzik 	 * the ZLP here, but ignore the one-byte packet.
1359073285fdSAlexey Orishko 	 * NOTE2: CDC NCM specification is different from CDC ECM when
1360073285fdSAlexey Orishko 	 * handling ZLP/short packets, so cdc_ncm driver will make short
1361073285fdSAlexey Orishko 	 * packet itself if needed.
13625b2fc499SJeff Garzik 	 */
1363b4d562e3SElina Pasheva 	if (length % dev->maxpacket == 0) {
1364b4d562e3SElina Pasheva 		if (!(info->flags & FLAG_SEND_ZLP)) {
1365073285fdSAlexey Orishko 			if (!(info->flags & FLAG_MULTI_PACKET)) {
136660e453a9SMing Lei 				length++;
136760e453a9SMing Lei 				if (skb_tailroom(skb) && !urb->num_sgs) {
13683e323f3eSPeter Korsgaard 					skb->data[skb->len] = 0;
13693e323f3eSPeter Korsgaard 					__skb_put(skb, 1);
137060e453a9SMing Lei 				} else if (urb->num_sgs)
137160e453a9SMing Lei 					sg_set_buf(&urb->sg[urb->num_sgs++],
137260e453a9SMing Lei 							dev->padding_pkt, 1);
1373073285fdSAlexey Orishko 			}
1374b4d562e3SElina Pasheva 		} else
1375b4d562e3SElina Pasheva 			urb->transfer_flags |= URB_ZERO_PACKET;
13763e323f3eSPeter Korsgaard 	}
13777a1e890eSBen Hutchings 	urb->transfer_buffer_length = length;
13787a1e890eSBen Hutchings 
13797a1e890eSBen Hutchings 	if (info->flags & FLAG_MULTI_PACKET) {
13807a1e890eSBen Hutchings 		/* Driver has set number of packets and a length delta.
13817a1e890eSBen Hutchings 		 * Calculate the complete length and ensure that it's
13827a1e890eSBen Hutchings 		 * positive.
13837a1e890eSBen Hutchings 		 */
13847a1e890eSBen Hutchings 		entry->length += length;
13857a1e890eSBen Hutchings 		if (WARN_ON_ONCE(entry->length <= 0))
13867a1e890eSBen Hutchings 			entry->length = length;
13877a1e890eSBen Hutchings 	} else {
13887a1e890eSBen Hutchings 		usbnet_set_skb_tx_stats(skb, 1, length);
13897a1e890eSBen Hutchings 	}
13905b2fc499SJeff Garzik 
13915b2fc499SJeff Garzik 	spin_lock_irqsave(&dev->txq.lock, flags);
139269ee472fSOliver Neukum 	retval = usb_autopm_get_interface_async(dev->intf);
139369ee472fSOliver Neukum 	if (retval < 0) {
139469ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
139569ee472fSOliver Neukum 		goto drop;
139669ee472fSOliver Neukum 	}
1397ad70411aSKloetzke Jan 	if (netif_queue_stopped(net)) {
1398ad70411aSKloetzke Jan 		usb_autopm_put_interface_async(dev->intf);
1399ad70411aSKloetzke Jan 		spin_unlock_irqrestore(&dev->txq.lock, flags);
1400ad70411aSKloetzke Jan 		goto drop;
1401ad70411aSKloetzke Jan 	}
140269ee472fSOliver Neukum 
140369ee472fSOliver Neukum #ifdef CONFIG_PM
140469ee472fSOliver Neukum 	/* if this triggers the device is still a sleep */
140569ee472fSOliver Neukum 	if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
140669ee472fSOliver Neukum 		/* transmission will be done in resume */
140769ee472fSOliver Neukum 		usb_anchor_urb(urb, &dev->deferred);
140869ee472fSOliver Neukum 		/* no use to process more packets */
140969ee472fSOliver Neukum 		netif_stop_queue(net);
141039707c2aSHemant Kumar 		usb_put_urb(urb);
141169ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
141260b86755SJoe Perches 		netdev_dbg(dev->net, "Delaying transmission for resumption\n");
141369ee472fSOliver Neukum 		goto deferred;
141469ee472fSOliver Neukum 	}
141569ee472fSOliver Neukum #endif
14165b2fc499SJeff Garzik 
14175b2fc499SJeff Garzik 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
14185b2fc499SJeff Garzik 	case -EPIPE:
14195b2fc499SJeff Garzik 		netif_stop_queue (net);
14205b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
142169ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
14225b2fc499SJeff Garzik 		break;
14235b2fc499SJeff Garzik 	default:
142469ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
1425a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net,
1426a475f603SJoe Perches 			  "tx: submit urb err %d\n", retval);
14275b2fc499SJeff Garzik 		break;
14285b2fc499SJeff Garzik 	case 0:
1429860e9538SFlorian Westphal 		netif_trans_update(net);
14305b6e9bcdSMing Lei 		__usbnet_queue_skb(&dev->txq, skb, tx_start);
14315b2fc499SJeff Garzik 		if (dev->txq.qlen >= TX_QLEN (dev))
14325b2fc499SJeff Garzik 			netif_stop_queue (net);
14335b2fc499SJeff Garzik 	}
14345b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->txq.lock, flags);
14355b2fc499SJeff Garzik 
14365b2fc499SJeff Garzik 	if (retval) {
1437a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
14385b2fc499SJeff Garzik drop:
14397963837fSHerbert Xu 		dev->net->stats.tx_dropped++;
1440073285fdSAlexey Orishko not_drop:
14415b2fc499SJeff Garzik 		if (skb)
14425b2fc499SJeff Garzik 			dev_kfree_skb_any (skb);
1443638c5115SMing Lei 		if (urb) {
1444638c5115SMing Lei 			kfree(urb->sg);
14455b2fc499SJeff Garzik 			usb_free_urb(urb);
1446638c5115SMing Lei 		}
1447a475f603SJoe Perches 	} else
1448a475f603SJoe Perches 		netif_dbg(dev, tx_queued, dev->net,
14493e4336a6SJason A. Donenfeld 			  "> tx, len %u, type 0x%x\n", length, skb->protocol);
145069ee472fSOliver Neukum #ifdef CONFIG_PM
145169ee472fSOliver Neukum deferred:
145269ee472fSOliver Neukum #endif
145325a79c41SStephen Hemminger 	return NETDEV_TX_OK;
14545b2fc499SJeff Garzik }
1455777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit);
14565b2fc499SJeff Garzik 
145785e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
145865841fd5SMing Lei {
145965841fd5SMing Lei 	struct urb	*urb;
146065841fd5SMing Lei 	int		i;
146185e87870SBjørn Mork 	int		ret = 0;
146265841fd5SMing Lei 
146365841fd5SMing Lei 	/* don't refill the queue all at once */
146465841fd5SMing Lei 	for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
146565841fd5SMing Lei 		urb = usb_alloc_urb(0, flags);
146665841fd5SMing Lei 		if (urb != NULL) {
146785e87870SBjørn Mork 			ret = rx_submit(dev, urb, flags);
146885e87870SBjørn Mork 			if (ret)
146985e87870SBjørn Mork 				goto err;
147085e87870SBjørn Mork 		} else {
147185e87870SBjørn Mork 			ret = -ENOMEM;
147285e87870SBjørn Mork 			goto err;
147365841fd5SMing Lei 		}
147465841fd5SMing Lei 	}
147585e87870SBjørn Mork err:
147685e87870SBjørn Mork 	return ret;
147765841fd5SMing Lei }
147865841fd5SMing Lei 
14795b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
14805b2fc499SJeff Garzik 
14815b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
14825b2fc499SJeff Garzik 
14832183c1a6SKees Cook static void usbnet_bh (struct timer_list *t)
14845b2fc499SJeff Garzik {
14852183c1a6SKees Cook 	struct usbnet		*dev = from_timer(dev, t, delay);
14865b2fc499SJeff Garzik 	struct sk_buff		*skb;
14875b2fc499SJeff Garzik 	struct skb_data		*entry;
14885b2fc499SJeff Garzik 
14895b2fc499SJeff Garzik 	while ((skb = skb_dequeue (&dev->done))) {
14905b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
14915b2fc499SJeff Garzik 		switch (entry->state) {
14925b2fc499SJeff Garzik 		case rx_done:
14935b2fc499SJeff Garzik 			entry->state = rx_cleanup;
14945b2fc499SJeff Garzik 			rx_process (dev, skb);
14955b2fc499SJeff Garzik 			continue;
14965b2fc499SJeff Garzik 		case tx_done:
1497638c5115SMing Lei 			kfree(entry->urb->sg);
1498df561f66SGustavo A. R. Silva 			fallthrough;
14995b2fc499SJeff Garzik 		case rx_cleanup:
15005b2fc499SJeff Garzik 			usb_free_urb (entry->urb);
15015b2fc499SJeff Garzik 			dev_kfree_skb (skb);
15025b2fc499SJeff Garzik 			continue;
15035b2fc499SJeff Garzik 		default:
150460b86755SJoe Perches 			netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
15055b2fc499SJeff Garzik 		}
15065b2fc499SJeff Garzik 	}
15075b2fc499SJeff Garzik 
150870c37bf9SBjørn Mork 	/* restart RX again after disabling due to high error rate */
150970c37bf9SBjørn Mork 	clear_bit(EVENT_RX_KILL, &dev->flags);
151070c37bf9SBjørn Mork 
151114a0d635SOliver Neukum 	/* waiting for all pending urbs to complete?
151214a0d635SOliver Neukum 	 * only then can we forgo submitting anew
151314a0d635SOliver Neukum 	 */
151414a0d635SOliver Neukum 	if (waitqueue_active(&dev->wait)) {
151514a0d635SOliver Neukum 		if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
151614a0d635SOliver Neukum 			wake_up_all(&dev->wait);
15175b2fc499SJeff Garzik 
15185b2fc499SJeff Garzik 	// or are we maybe short a few urbs?
15198e95a202SJoe Perches 	} else if (netif_running (dev->net) &&
15208e95a202SJoe Perches 		   netif_device_present (dev->net) &&
15214b49f58fSMing Lei 		   netif_carrier_ok(dev->net) &&
15228e95a202SJoe Perches 		   !timer_pending(&dev->delay) &&
152343daa96bSSoohoon Lee 		   !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
15248e95a202SJoe Perches 		   !test_bit(EVENT_RX_HALT, &dev->flags)) {
15255b2fc499SJeff Garzik 		int	temp = dev->rxq.qlen;
15265b2fc499SJeff Garzik 
152765841fd5SMing Lei 		if (temp < RX_QLEN(dev)) {
152885e87870SBjørn Mork 			if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
152985e87870SBjørn Mork 				return;
1530a475f603SJoe Perches 			if (temp != dev->rxq.qlen)
1531a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1532a475f603SJoe Perches 					  "rxqlen %d --> %d\n",
15335b2fc499SJeff Garzik 					  temp, dev->rxq.qlen);
153465841fd5SMing Lei 			if (dev->rxq.qlen < RX_QLEN(dev))
15355b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
15365b2fc499SJeff Garzik 		}
15375b2fc499SJeff Garzik 		if (dev->txq.qlen < TX_QLEN (dev))
15385b2fc499SJeff Garzik 			netif_wake_queue (dev->net);
15395b2fc499SJeff Garzik 	}
15405b2fc499SJeff Garzik }
15415b2fc499SJeff Garzik 
1542c955e329SEmil Renner Berthing static void usbnet_bh_tasklet(struct tasklet_struct *t)
15432eb1d3f4SPhong Tran {
1544c955e329SEmil Renner Berthing 	struct usbnet *dev = from_tasklet(dev, t, bh);
15452eb1d3f4SPhong Tran 
1546c955e329SEmil Renner Berthing 	usbnet_bh(&dev->delay);
15472eb1d3f4SPhong Tran }
15482eb1d3f4SPhong Tran 
15495b2fc499SJeff Garzik 
15505b2fc499SJeff Garzik /*-------------------------------------------------------------------------
15515b2fc499SJeff Garzik  *
15525b2fc499SJeff Garzik  * USB Device Driver support
15535b2fc499SJeff Garzik  *
15545b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
15555b2fc499SJeff Garzik 
15565b2fc499SJeff Garzik // precondition: never called in_interrupt
15575b2fc499SJeff Garzik 
15585b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
15595b2fc499SJeff Garzik {
15605b2fc499SJeff Garzik 	struct usbnet		*dev;
15615b2fc499SJeff Garzik 	struct usb_device	*xdev;
15625b2fc499SJeff Garzik 	struct net_device	*net;
15635b2fc499SJeff Garzik 
15645b2fc499SJeff Garzik 	dev = usb_get_intfdata(intf);
15655b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
15665b2fc499SJeff Garzik 	if (!dev)
15675b2fc499SJeff Garzik 		return;
15685b2fc499SJeff Garzik 
15695b2fc499SJeff Garzik 	xdev = interface_to_usbdev (intf);
15705b2fc499SJeff Garzik 
1571a475f603SJoe Perches 	netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
15725b2fc499SJeff Garzik 		   intf->dev.driver->name,
15735b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
15745b2fc499SJeff Garzik 		   dev->driver_info->description);
15755b2fc499SJeff Garzik 
15765b2fc499SJeff Garzik 	net = dev->net;
15775b2fc499SJeff Garzik 	unregister_netdev (net);
15785b2fc499SJeff Garzik 
157923f333a2STejun Heo 	cancel_work_sync(&dev->kevent);
15805b2fc499SJeff Garzik 
158139707c2aSHemant Kumar 	usb_scuttle_anchored_urbs(&dev->deferred);
158239707c2aSHemant Kumar 
15835b2fc499SJeff Garzik 	if (dev->driver_info->unbind)
15845b2fc499SJeff Garzik 		dev->driver_info->unbind (dev, intf);
15855b2fc499SJeff Garzik 
158668972efaSPaul Stewart 	usb_kill_urb(dev->interrupt);
158768972efaSPaul Stewart 	usb_free_urb(dev->interrupt);
158860e453a9SMing Lei 	kfree(dev->padding_pkt);
158968972efaSPaul Stewart 
1590af0c351cSHeiner Kallweit 	free_percpu(net->tstats);
15915b2fc499SJeff Garzik 	free_netdev(net);
15925b2fc499SJeff Garzik }
15935b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
15945b2fc499SJeff Garzik 
1595777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = {
1596777baa47SStephen Hemminger 	.ndo_open		= usbnet_open,
1597777baa47SStephen Hemminger 	.ndo_stop		= usbnet_stop,
1598777baa47SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
1599777baa47SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
16001efed2d0SOlivier Blin 	.ndo_set_rx_mode	= usbnet_set_rx_mode,
1601777baa47SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1602af0c351cSHeiner Kallweit 	.ndo_get_stats64	= dev_get_tstats64,
1603777baa47SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1604777baa47SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1605777baa47SStephen Hemminger };
16065b2fc499SJeff Garzik 
16075b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
16085b2fc499SJeff Garzik 
16095b2fc499SJeff Garzik // precondition: never called in_interrupt
16105b2fc499SJeff Garzik 
1611225794f8SMarcel Holtmann static struct device_type wlan_type = {
1612225794f8SMarcel Holtmann 	.name	= "wlan",
1613225794f8SMarcel Holtmann };
1614225794f8SMarcel Holtmann 
1615225794f8SMarcel Holtmann static struct device_type wwan_type = {
1616225794f8SMarcel Holtmann 	.name	= "wwan",
1617225794f8SMarcel Holtmann };
1618225794f8SMarcel Holtmann 
16195b2fc499SJeff Garzik int
16205b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
16215b2fc499SJeff Garzik {
16225b2fc499SJeff Garzik 	struct usbnet			*dev;
16235b2fc499SJeff Garzik 	struct net_device		*net;
16245b2fc499SJeff Garzik 	struct usb_host_interface	*interface;
1625f3edc2dbSBen Dooks 	const struct driver_info	*info;
16265b2fc499SJeff Garzik 	struct usb_device		*xdev;
16275b2fc499SJeff Garzik 	int				status;
16285b2fc499SJeff Garzik 	const char			*name;
1629b0786b43SMing Lei 	struct usb_driver 	*driver = to_usb_driver(udev->dev.driver);
1630b0786b43SMing Lei 
1631b0786b43SMing Lei 	/* usbnet already took usb runtime pm, so have to enable the feature
1632b0786b43SMing Lei 	 * for usb interface, otherwise usb_autopm_get_interface may return
163398f541c6SAlan Stern 	 * failure if RUNTIME_PM is enabled.
1634b0786b43SMing Lei 	 */
1635b0786b43SMing Lei 	if (!driver->supports_autosuspend) {
1636b0786b43SMing Lei 		driver->supports_autosuspend = 1;
1637b0786b43SMing Lei 		pm_runtime_enable(&udev->dev);
1638b0786b43SMing Lei 	}
16395b2fc499SJeff Garzik 
16405b2fc499SJeff Garzik 	name = udev->dev.driver->name;
1641f3edc2dbSBen Dooks 	info = (const struct driver_info *) prod->driver_info;
16425b2fc499SJeff Garzik 	if (!info) {
16435b2fc499SJeff Garzik 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
16445b2fc499SJeff Garzik 		return -ENODEV;
16455b2fc499SJeff Garzik 	}
16465b2fc499SJeff Garzik 	xdev = interface_to_usbdev (udev);
16475b2fc499SJeff Garzik 	interface = udev->cur_altsetting;
16485b2fc499SJeff Garzik 
16495b2fc499SJeff Garzik 	status = -ENOMEM;
16505b2fc499SJeff Garzik 
16515b2fc499SJeff Garzik 	// set up our own records
16525b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(*dev));
165341de8d4cSJoe Perches 	if (!net)
16545b2fc499SJeff Garzik 		goto out;
16555b2fc499SJeff Garzik 
16560dacca73SBen Hutchings 	/* netdev_printk() needs this so do it as early as possible */
16570dacca73SBen Hutchings 	SET_NETDEV_DEV(net, &udev->dev);
16580dacca73SBen Hutchings 
16595b2fc499SJeff Garzik 	dev = netdev_priv(net);
16605b2fc499SJeff Garzik 	dev->udev = xdev;
1661a11a6544SOliver Neukum 	dev->intf = udev;
16625b2fc499SJeff Garzik 	dev->driver_info = info;
16635b2fc499SJeff Garzik 	dev->driver_name = name;
1664c8b5d129SGreg Ungerer 
1665af0c351cSHeiner Kallweit 	net->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1666af0c351cSHeiner Kallweit 	if (!net->tstats)
1667c8b5d129SGreg Ungerer 		goto out0;
1668c8b5d129SGreg Ungerer 
16695b2fc499SJeff Garzik 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
16705b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
167114a0d635SOliver Neukum 	init_waitqueue_head(&dev->wait);
16725b2fc499SJeff Garzik 	skb_queue_head_init (&dev->rxq);
16735b2fc499SJeff Garzik 	skb_queue_head_init (&dev->txq);
16745b2fc499SJeff Garzik 	skb_queue_head_init (&dev->done);
16757834ddbcSJussi Kivilinna 	skb_queue_head_init(&dev->rxq_pause);
1676c955e329SEmil Renner Berthing 	tasklet_setup(&dev->bh, usbnet_bh_tasklet);
167711f17ef3SOliver Neukum 	INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
167869ee472fSOliver Neukum 	init_usb_anchor(&dev->deferred);
16792183c1a6SKees Cook 	timer_setup(&dev->delay, usbnet_bh, 0);
16805b2fc499SJeff Garzik 	mutex_init (&dev->phy_mutex);
16816eecdc5fSDan Williams 	mutex_init(&dev->interrupt_mutex);
16826eecdc5fSDan Williams 	dev->interrupt_count = 0;
16835b2fc499SJeff Garzik 
16845b2fc499SJeff Garzik 	dev->net = net;
16855b2fc499SJeff Garzik 	strcpy (net->name, "usb%d");
16865b2fc499SJeff Garzik 	memcpy (net->dev_addr, node_id, sizeof node_id);
16875b2fc499SJeff Garzik 
16885b2fc499SJeff Garzik 	/* rx and tx sides can use different message sizes;
16895b2fc499SJeff Garzik 	 * bind() should set rx_urb_size in that case.
16905b2fc499SJeff Garzik 	 */
16915b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
1692f77f0aeeSJarod Wilson 	net->min_mtu = 0;
1693f77f0aeeSJarod Wilson 	net->max_mtu = ETH_MAX_MTU;
16945b2fc499SJeff Garzik 
1695777baa47SStephen Hemminger 	net->netdev_ops = &usbnet_netdev_ops;
1696777baa47SStephen Hemminger 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
16975b2fc499SJeff Garzik 	net->ethtool_ops = &usbnet_ethtool_ops;
16985b2fc499SJeff Garzik 
16995b2fc499SJeff Garzik 	// allow device-specific bind/init procedures
17005b2fc499SJeff Garzik 	// NOTE net->name still not usable ...
17015b2fc499SJeff Garzik 	if (info->bind) {
17025b2fc499SJeff Garzik 		status = info->bind (dev, udev);
17035b2fc499SJeff Garzik 		if (status < 0)
17045b2fc499SJeff Garzik 			goto out1;
17055b2fc499SJeff Garzik 
17065b2fc499SJeff Garzik 		// heuristic:  "usb%d" for links we know are two-host,
17075b2fc499SJeff Garzik 		// else "eth%d" when there's reasonable doubt.  userspace
17085b2fc499SJeff Garzik 		// can rename the link if it knows better.
17098e95a202SJoe Perches 		if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1710c261344dSArnd Bergmann 		    ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1711c261344dSArnd Bergmann 		     (net->dev_addr [0] & 0x02) == 0))
17125b2fc499SJeff Garzik 			strcpy (net->name, "eth%d");
17136e3bbcc5SJussi Kivilinna 		/* WLAN devices should always be named "wlan%d" */
17146e3bbcc5SJussi Kivilinna 		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
17156e3bbcc5SJussi Kivilinna 			strcpy(net->name, "wlan%d");
1716e1e499eeSMarcel Holtmann 		/* WWAN devices should always be named "wwan%d" */
1717e1e499eeSMarcel Holtmann 		if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1718e1e499eeSMarcel Holtmann 			strcpy(net->name, "wwan%d");
17195b2fc499SJeff Garzik 
17206509141fSWei Shuai 		/* devices that cannot do ARP */
17216509141fSWei Shuai 		if ((dev->driver_info->flags & FLAG_NOARP) != 0)
17226509141fSWei Shuai 			net->flags |= IFF_NOARP;
17236509141fSWei Shuai 
17245b2fc499SJeff Garzik 		/* maybe the remote can't receive an Ethernet MTU */
17255b2fc499SJeff Garzik 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
17265b2fc499SJeff Garzik 			net->mtu = dev->hard_mtu - net->hard_header_len;
17275b2fc499SJeff Garzik 	} else if (!info->in || !info->out)
17285b2fc499SJeff Garzik 		status = usbnet_get_endpoints (dev, udev);
17295b2fc499SJeff Garzik 	else {
17305b2fc499SJeff Garzik 		dev->in = usb_rcvbulkpipe (xdev, info->in);
17315b2fc499SJeff Garzik 		dev->out = usb_sndbulkpipe (xdev, info->out);
17325b2fc499SJeff Garzik 		if (!(info->flags & FLAG_NO_SETINT))
17335b2fc499SJeff Garzik 			status = usb_set_interface (xdev,
17345b2fc499SJeff Garzik 				interface->desc.bInterfaceNumber,
17355b2fc499SJeff Garzik 				interface->desc.bAlternateSetting);
17365b2fc499SJeff Garzik 		else
17375b2fc499SJeff Garzik 			status = 0;
17385b2fc499SJeff Garzik 
17395b2fc499SJeff Garzik 	}
17409514bfe5SPeter Korsgaard 	if (status >= 0 && dev->status)
17415b2fc499SJeff Garzik 		status = init_status (dev, udev);
17425b2fc499SJeff Garzik 	if (status < 0)
17435b2fc499SJeff Garzik 		goto out3;
17445b2fc499SJeff Garzik 
17455b2fc499SJeff Garzik 	if (!dev->rx_urb_size)
17465b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
17475b2fc499SJeff Garzik 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
17485b2fc499SJeff Garzik 
1749eef23b53SBjørn Mork 	/* let userspace know we have a random address */
1750eef23b53SBjørn Mork 	if (ether_addr_equal(net->dev_addr, node_id))
1751eef23b53SBjørn Mork 		net->addr_assign_type = NET_ADDR_RANDOM;
1752eef23b53SBjørn Mork 
1753225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1754225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wlan_type);
1755225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1756225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wwan_type);
1757225794f8SMarcel Holtmann 
1758a88c32aeSMing Lei 	/* initialize max rx_qlen and tx_qlen */
1759a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
1760a88c32aeSMing Lei 
176160e453a9SMing Lei 	if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
176260e453a9SMing Lei 		!(info->flags & FLAG_MULTI_PACKET)) {
176360e453a9SMing Lei 		dev->padding_pkt = kzalloc(1, GFP_KERNEL);
176409b69024SWei Yongjun 		if (!dev->padding_pkt) {
176509b69024SWei Yongjun 			status = -ENOMEM;
176660e453a9SMing Lei 			goto out4;
176760e453a9SMing Lei 		}
176809b69024SWei Yongjun 	}
176960e453a9SMing Lei 
17705b2fc499SJeff Garzik 	status = register_netdev (net);
17715b2fc499SJeff Garzik 	if (status)
177260e453a9SMing Lei 		goto out5;
1773a475f603SJoe Perches 	netif_info(dev, probe, dev->net,
1774a475f603SJoe Perches 		   "register '%s' at usb-%s-%s, %s, %pM\n",
17755b2fc499SJeff Garzik 		   udev->dev.driver->name,
17765b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
17775b2fc499SJeff Garzik 		   dev->driver_info->description,
1778e174961cSJohannes Berg 		   net->dev_addr);
17795b2fc499SJeff Garzik 
17805b2fc499SJeff Garzik 	// ok, it's ready to go.
17815b2fc499SJeff Garzik 	usb_set_intfdata (udev, dev);
17825b2fc499SJeff Garzik 
17835b2fc499SJeff Garzik 	netif_device_attach (net);
17845b2fc499SJeff Garzik 
178537e8273cSBen Hutchings 	if (dev->driver_info->flags & FLAG_LINK_INTR)
17860162c554SMing Lei 		usbnet_link_change(dev, 0, 0);
178737e8273cSBen Hutchings 
17885b2fc499SJeff Garzik 	return 0;
17895b2fc499SJeff Garzik 
179060e453a9SMing Lei out5:
179160e453a9SMing Lei 	kfree(dev->padding_pkt);
1792a4723848Stom.leiming@gmail.com out4:
1793a4723848Stom.leiming@gmail.com 	usb_free_urb(dev->interrupt);
17945b2fc499SJeff Garzik out3:
17955b2fc499SJeff Garzik 	if (info->unbind)
17965b2fc499SJeff Garzik 		info->unbind (dev, udev);
17975b2fc499SJeff Garzik out1:
17981666984cSOliver Neukum 	/* subdrivers must undo all they did in bind() if they
17991666984cSOliver Neukum 	 * fail it, but we may fail later and a deferred kevent
18001666984cSOliver Neukum 	 * may trigger an error resubmitting itself and, worse,
18011666984cSOliver Neukum 	 * schedule a timer. So we kill it all just in case.
18021666984cSOliver Neukum 	 */
18031666984cSOliver Neukum 	cancel_work_sync(&dev->kevent);
18041666984cSOliver Neukum 	del_timer_sync(&dev->delay);
1805af0c351cSHeiner Kallweit 	free_percpu(net->tstats);
1806c8b5d129SGreg Ungerer out0:
18075b2fc499SJeff Garzik 	free_netdev(net);
18085b2fc499SJeff Garzik out:
18095b2fc499SJeff Garzik 	return status;
18105b2fc499SJeff Garzik }
18115b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
18125b2fc499SJeff Garzik 
18135b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
18145b2fc499SJeff Garzik 
181536433127SOliver Neukum /*
181636433127SOliver Neukum  * suspend the whole driver as soon as the first interface is suspended
181736433127SOliver Neukum  * resume only when the last interface is resumed
18185b2fc499SJeff Garzik  */
18195b2fc499SJeff Garzik 
18205b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
18215b2fc499SJeff Garzik {
18225b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
18235b2fc499SJeff Garzik 
182436433127SOliver Neukum 	if (!dev->suspend_count++) {
182569ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
182669ee472fSOliver Neukum 		/* don't autosuspend while transmitting */
18275b1b0b81SAlan Stern 		if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
18285eeb3132SMing Lei 			dev->suspend_count--;
182969ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
183069ee472fSOliver Neukum 			return -EBUSY;
183169ee472fSOliver Neukum 		} else {
183269ee472fSOliver Neukum 			set_bit(EVENT_DEV_ASLEEP, &dev->flags);
183369ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
183469ee472fSOliver Neukum 		}
1835a11a6544SOliver Neukum 		/*
1836a11a6544SOliver Neukum 		 * accelerate emptying of the rx and queues, to avoid
18375b2fc499SJeff Garzik 		 * having everything error out.
18385b2fc499SJeff Garzik 		 */
18395b2fc499SJeff Garzik 		netif_device_detach (dev->net);
184069ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
18416eecdc5fSDan Williams 		__usbnet_status_stop_force(dev);
184269ee472fSOliver Neukum 
1843a11a6544SOliver Neukum 		/*
1844a11a6544SOliver Neukum 		 * reattach so runtime management can use and
1845a11a6544SOliver Neukum 		 * wake the device
1846a11a6544SOliver Neukum 		 */
1847a11a6544SOliver Neukum 		netif_device_attach (dev->net);
184836433127SOliver Neukum 	}
18495b2fc499SJeff Garzik 	return 0;
18505b2fc499SJeff Garzik }
18515b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
18525b2fc499SJeff Garzik 
18535b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
18545b2fc499SJeff Garzik {
18555b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
185669ee472fSOliver Neukum 	struct sk_buff          *skb;
185769ee472fSOliver Neukum 	struct urb              *res;
185869ee472fSOliver Neukum 	int                     retval;
18595b2fc499SJeff Garzik 
186069ee472fSOliver Neukum 	if (!--dev->suspend_count) {
18616eecdc5fSDan Williams 		/* resume interrupt URB if it was previously submitted */
18626eecdc5fSDan Williams 		__usbnet_status_start_force(dev, GFP_NOIO);
186368972efaSPaul Stewart 
186469ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
186569ee472fSOliver Neukum 		while ((res = usb_get_from_anchor(&dev->deferred))) {
186669ee472fSOliver Neukum 
186769ee472fSOliver Neukum 			skb = (struct sk_buff *)res->context;
186869ee472fSOliver Neukum 			retval = usb_submit_urb(res, GFP_ATOMIC);
186969ee472fSOliver Neukum 			if (retval < 0) {
187069ee472fSOliver Neukum 				dev_kfree_skb_any(skb);
1871638c5115SMing Lei 				kfree(res->sg);
187269ee472fSOliver Neukum 				usb_free_urb(res);
187369ee472fSOliver Neukum 				usb_autopm_put_interface_async(dev->intf);
187469ee472fSOliver Neukum 			} else {
1875860e9538SFlorian Westphal 				netif_trans_update(dev->net);
187669ee472fSOliver Neukum 				__skb_queue_tail(&dev->txq, skb);
187769ee472fSOliver Neukum 			}
187869ee472fSOliver Neukum 		}
187969ee472fSOliver Neukum 
188069ee472fSOliver Neukum 		smp_mb();
188169ee472fSOliver Neukum 		clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
188269ee472fSOliver Neukum 		spin_unlock_irq(&dev->txq.lock);
188375bd0cbdSMing Lei 
188475bd0cbdSMing Lei 		if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
188514a0d635SOliver Neukum 			/* handle remote wakeup ASAP
188614a0d635SOliver Neukum 			 * we cannot race against stop
188714a0d635SOliver Neukum 			 */
188814a0d635SOliver Neukum 			if (netif_device_present(dev->net) &&
188965841fd5SMing Lei 				!timer_pending(&dev->delay) &&
189065841fd5SMing Lei 				!test_bit(EVENT_RX_HALT, &dev->flags))
1891ab6f148dSOliver Neukum 					rx_alloc_submit(dev, GFP_NOIO);
189265841fd5SMing Lei 
189369ee472fSOliver Neukum 			if (!(dev->txq.qlen >= TX_QLEN(dev)))
18941aa9bc5bSAlexey Orishko 				netif_tx_wake_all_queues(dev->net);
18955b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
189669ee472fSOliver Neukum 		}
189775bd0cbdSMing Lei 	}
18985d9d01a3SOliver Neukum 
18995d9d01a3SOliver Neukum 	if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
19005d9d01a3SOliver Neukum 		usb_autopm_get_interface_no_resume(intf);
19015d9d01a3SOliver Neukum 
19025b2fc499SJeff Garzik 	return 0;
19035b2fc499SJeff Garzik }
19045b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
19055b2fc499SJeff Garzik 
19065d9d01a3SOliver Neukum /*
19075d9d01a3SOliver Neukum  * Either a subdriver implements manage_power, then it is assumed to always
19085d9d01a3SOliver Neukum  * be ready to be suspended or it reports the readiness to be suspended
19095d9d01a3SOliver Neukum  * explicitly
19105d9d01a3SOliver Neukum  */
19115d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev)
19125d9d01a3SOliver Neukum {
19135d9d01a3SOliver Neukum 	if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
19145d9d01a3SOliver Neukum 		dev->intf->needs_remote_wakeup = 1;
19155d9d01a3SOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
19165d9d01a3SOliver Neukum 	}
19175d9d01a3SOliver Neukum }
19185d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle);
19195b2fc499SJeff Garzik 
19202dd7c8cfSOliver Neukum /*
19212dd7c8cfSOliver Neukum  * For devices that can do without special commands
19222dd7c8cfSOliver Neukum  */
19232dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on)
19242dd7c8cfSOliver Neukum {
19252dd7c8cfSOliver Neukum 	dev->intf->needs_remote_wakeup = on;
19262dd7c8cfSOliver Neukum 	return 0;
19272dd7c8cfSOliver Neukum }
19282dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power);
19292dd7c8cfSOliver Neukum 
1930ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1931ac64995dSMing Lei {
1932ac64995dSMing Lei 	/* update link after link is reseted */
1933ac64995dSMing Lei 	if (link && !need_reset)
1934ac64995dSMing Lei 		netif_carrier_on(dev->net);
1935ac64995dSMing Lei 	else
1936ac64995dSMing Lei 		netif_carrier_off(dev->net);
1937ac64995dSMing Lei 
1938ac64995dSMing Lei 	if (need_reset && link)
1939ac64995dSMing Lei 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
19404b49f58fSMing Lei 	else
19414b49f58fSMing Lei 		usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1942ac64995dSMing Lei }
1943ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change);
1944ac64995dSMing Lei 
19455b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
19460547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1947877bd862SMing Lei 			     u16 value, u16 index, void *data, u16 size)
1948877bd862SMing Lei {
1949877bd862SMing Lei 	void *buf = NULL;
1950877bd862SMing Lei 	int err = -ENOMEM;
1951877bd862SMing Lei 
1952877bd862SMing Lei 	netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1953877bd862SMing Lei 		   " value=0x%04x index=0x%04x size=%d\n",
1954877bd862SMing Lei 		   cmd, reqtype, value, index, size);
1955877bd862SMing Lei 
19566c22fce0SOliver Neukum 	if (size) {
1957877bd862SMing Lei 		buf = kmalloc(size, GFP_KERNEL);
1958877bd862SMing Lei 		if (!buf)
1959877bd862SMing Lei 			goto out;
1960877bd862SMing Lei 	}
1961877bd862SMing Lei 
1962877bd862SMing Lei 	err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1963877bd862SMing Lei 			      cmd, reqtype, value, index, buf, size,
1964877bd862SMing Lei 			      USB_CTRL_GET_TIMEOUT);
19656c22fce0SOliver Neukum 	if (err > 0 && err <= size) {
19666c22fce0SOliver Neukum 		if (data)
1967877bd862SMing Lei 			memcpy(data, buf, err);
19686c22fce0SOliver Neukum 		else
19696c22fce0SOliver Neukum 			netdev_dbg(dev->net,
19706c22fce0SOliver Neukum 				   "Huh? Data requested but thrown away.\n");
19716c22fce0SOliver Neukum 	}
1972877bd862SMing Lei 	kfree(buf);
1973877bd862SMing Lei out:
1974877bd862SMing Lei 	return err;
1975877bd862SMing Lei }
1976877bd862SMing Lei 
19770547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
19780547fad2SMing Lei 			      u16 value, u16 index, const void *data,
19790547fad2SMing Lei 			      u16 size)
1980877bd862SMing Lei {
1981877bd862SMing Lei 	void *buf = NULL;
1982877bd862SMing Lei 	int err = -ENOMEM;
1983877bd862SMing Lei 
1984877bd862SMing Lei 	netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1985877bd862SMing Lei 		   " value=0x%04x index=0x%04x size=%d\n",
1986877bd862SMing Lei 		   cmd, reqtype, value, index, size);
1987877bd862SMing Lei 
1988877bd862SMing Lei 	if (data) {
1989877bd862SMing Lei 		buf = kmemdup(data, size, GFP_KERNEL);
1990877bd862SMing Lei 		if (!buf)
1991877bd862SMing Lei 			goto out;
19926c22fce0SOliver Neukum 	} else {
19936c22fce0SOliver Neukum         if (size) {
19946c22fce0SOliver Neukum             WARN_ON_ONCE(1);
19956c22fce0SOliver Neukum             err = -EINVAL;
19966c22fce0SOliver Neukum             goto out;
19976c22fce0SOliver Neukum         }
1998877bd862SMing Lei     }
1999877bd862SMing Lei 
2000877bd862SMing Lei 	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2001877bd862SMing Lei 			      cmd, reqtype, value, index, buf, size,
2002877bd862SMing Lei 			      USB_CTRL_SET_TIMEOUT);
2003877bd862SMing Lei 	kfree(buf);
2004877bd862SMing Lei 
2005877bd862SMing Lei out:
2006877bd862SMing Lei 	return err;
2007877bd862SMing Lei }
20080547fad2SMing Lei 
20090547fad2SMing Lei /*
20100547fad2SMing Lei  * The function can't be called inside suspend/resume callback,
20110547fad2SMing Lei  * otherwise deadlock will be caused.
20120547fad2SMing Lei  */
20130547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20140547fad2SMing Lei 		    u16 value, u16 index, void *data, u16 size)
20150547fad2SMing Lei {
20166f0a0986SMing Lei 	int ret;
20176f0a0986SMing Lei 
20186f0a0986SMing Lei 	if (usb_autopm_get_interface(dev->intf) < 0)
20196f0a0986SMing Lei 		return -ENODEV;
20206f0a0986SMing Lei 	ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
20210547fad2SMing Lei 				data, size);
20226f0a0986SMing Lei 	usb_autopm_put_interface(dev->intf);
20236f0a0986SMing Lei 	return ret;
20240547fad2SMing Lei }
20250547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd);
20260547fad2SMing Lei 
20270547fad2SMing Lei /*
20280547fad2SMing Lei  * The function can't be called inside suspend/resume callback,
20290547fad2SMing Lei  * otherwise deadlock will be caused.
20300547fad2SMing Lei  */
20310547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20320547fad2SMing Lei 		     u16 value, u16 index, const void *data, u16 size)
20330547fad2SMing Lei {
20346f0a0986SMing Lei 	int ret;
20356f0a0986SMing Lei 
20366f0a0986SMing Lei 	if (usb_autopm_get_interface(dev->intf) < 0)
20376f0a0986SMing Lei 		return -ENODEV;
20386f0a0986SMing Lei 	ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
20390547fad2SMing Lei 				 data, size);
20406f0a0986SMing Lei 	usb_autopm_put_interface(dev->intf);
20416f0a0986SMing Lei 	return ret;
20420547fad2SMing Lei }
2043877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2044877bd862SMing Lei 
20450547fad2SMing Lei /*
20460547fad2SMing Lei  * The function can be called inside suspend/resume callback safely
20470547fad2SMing Lei  * and should only be called by suspend/resume callback generally.
20480547fad2SMing Lei  */
20490547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
20500547fad2SMing Lei 			  u16 value, u16 index, void *data, u16 size)
20510547fad2SMing Lei {
20520547fad2SMing Lei 	return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
20530547fad2SMing Lei 				 data, size);
20540547fad2SMing Lei }
20550547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
20560547fad2SMing Lei 
20570547fad2SMing Lei /*
20580547fad2SMing Lei  * The function can be called inside suspend/resume callback safely
20590547fad2SMing Lei  * and should only be called by suspend/resume callback generally.
20600547fad2SMing Lei  */
20610547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
20620547fad2SMing Lei 			  u16 value, u16 index, const void *data,
20630547fad2SMing Lei 			  u16 size)
20640547fad2SMing Lei {
20650547fad2SMing Lei 	return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
20660547fad2SMing Lei 				  data, size);
20670547fad2SMing Lei }
20680547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
20690547fad2SMing Lei 
2070877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb)
2071877bd862SMing Lei {
2072877bd862SMing Lei 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2073877bd862SMing Lei 	int status = urb->status;
2074877bd862SMing Lei 
2075877bd862SMing Lei 	if (status < 0)
2076877bd862SMing Lei 		dev_dbg(&urb->dev->dev, "%s failed with %d",
2077877bd862SMing Lei 			__func__, status);
2078877bd862SMing Lei 
2079877bd862SMing Lei 	kfree(req);
2080877bd862SMing Lei 	usb_free_urb(urb);
2081877bd862SMing Lei }
2082877bd862SMing Lei 
20830547fad2SMing Lei /*
20840547fad2SMing Lei  * The caller must make sure that device can't be put into suspend
20850547fad2SMing Lei  * state until the control URB completes.
20860547fad2SMing Lei  */
2087877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2088877bd862SMing Lei 			   u16 value, u16 index, const void *data, u16 size)
2089877bd862SMing Lei {
2090877bd862SMing Lei 	struct usb_ctrlrequest *req = NULL;
2091877bd862SMing Lei 	struct urb *urb;
2092877bd862SMing Lei 	int err = -ENOMEM;
2093877bd862SMing Lei 	void *buf = NULL;
2094877bd862SMing Lei 
2095877bd862SMing Lei 	netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2096877bd862SMing Lei 		   " value=0x%04x index=0x%04x size=%d\n",
2097877bd862SMing Lei 		   cmd, reqtype, value, index, size);
2098877bd862SMing Lei 
2099877bd862SMing Lei 	urb = usb_alloc_urb(0, GFP_ATOMIC);
2100ef6cd130SWolfram Sang 	if (!urb)
2101877bd862SMing Lei 		goto fail;
2102877bd862SMing Lei 
2103877bd862SMing Lei 	if (data) {
2104877bd862SMing Lei 		buf = kmemdup(data, size, GFP_ATOMIC);
2105877bd862SMing Lei 		if (!buf) {
2106877bd862SMing Lei 			netdev_err(dev->net, "Error allocating buffer"
2107877bd862SMing Lei 				   " in %s!\n", __func__);
2108877bd862SMing Lei 			goto fail_free;
2109877bd862SMing Lei 		}
2110877bd862SMing Lei 	}
2111877bd862SMing Lei 
2112877bd862SMing Lei 	req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
211338673c82SJoe Perches 	if (!req)
2114877bd862SMing Lei 		goto fail_free_buf;
2115877bd862SMing Lei 
2116877bd862SMing Lei 	req->bRequestType = reqtype;
2117877bd862SMing Lei 	req->bRequest = cmd;
2118877bd862SMing Lei 	req->wValue = cpu_to_le16(value);
2119877bd862SMing Lei 	req->wIndex = cpu_to_le16(index);
2120877bd862SMing Lei 	req->wLength = cpu_to_le16(size);
2121877bd862SMing Lei 
2122877bd862SMing Lei 	usb_fill_control_urb(urb, dev->udev,
2123877bd862SMing Lei 			     usb_sndctrlpipe(dev->udev, 0),
2124877bd862SMing Lei 			     (void *)req, buf, size,
2125877bd862SMing Lei 			     usbnet_async_cmd_cb, req);
2126877bd862SMing Lei 	urb->transfer_flags |= URB_FREE_BUFFER;
2127877bd862SMing Lei 
2128877bd862SMing Lei 	err = usb_submit_urb(urb, GFP_ATOMIC);
2129877bd862SMing Lei 	if (err < 0) {
2130877bd862SMing Lei 		netdev_err(dev->net, "Error submitting the control"
2131877bd862SMing Lei 			   " message: status=%d\n", err);
2132877bd862SMing Lei 		goto fail_free;
2133877bd862SMing Lei 	}
2134877bd862SMing Lei 	return 0;
2135877bd862SMing Lei 
2136877bd862SMing Lei fail_free_buf:
2137877bd862SMing Lei 	kfree(buf);
2138877bd862SMing Lei fail_free:
2139877bd862SMing Lei 	kfree(req);
2140877bd862SMing Lei 	usb_free_urb(urb);
2141877bd862SMing Lei fail:
2142877bd862SMing Lei 	return err;
2143877bd862SMing Lei 
2144877bd862SMing Lei }
2145877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2146877bd862SMing Lei /*-------------------------------------------------------------------------*/
21475b2fc499SJeff Garzik 
21485b2fc499SJeff Garzik static int __init usbnet_init(void)
21495b2fc499SJeff Garzik {
2150c582a950SThiago Farina 	/* Compiler should optimize this out. */
2151c582a950SThiago Farina 	BUILD_BUG_ON(
2152c593642cSPankaj Bharadiya 		sizeof_field(struct sk_buff, cb) < sizeof(struct skb_data));
21535b2fc499SJeff Garzik 
2154c7e12eadSJoe Perches 	eth_random_addr(node_id);
21555b2fc499SJeff Garzik 	return 0;
21565b2fc499SJeff Garzik }
21575b2fc499SJeff Garzik module_init(usbnet_init);
21585b2fc499SJeff Garzik 
21595b2fc499SJeff Garzik static void __exit usbnet_exit(void)
21605b2fc499SJeff Garzik {
21615b2fc499SJeff Garzik }
21625b2fc499SJeff Garzik module_exit(usbnet_exit);
21635b2fc499SJeff Garzik 
21645b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
21655b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
21665b2fc499SJeff Garzik MODULE_LICENSE("GPL");
2167