xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision ab18a9c9ca27f8208c4960a1b25e8475b9f9dda5)
15b2fc499SJeff Garzik /*
25b2fc499SJeff Garzik  * USB Network driver infrastructure
35b2fc499SJeff Garzik  * Copyright (C) 2000-2005 by David Brownell
45b2fc499SJeff Garzik  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
55b2fc499SJeff Garzik  *
65b2fc499SJeff Garzik  * This program is free software; you can redistribute it and/or modify
75b2fc499SJeff Garzik  * it under the terms of the GNU General Public License as published by
85b2fc499SJeff Garzik  * the Free Software Foundation; either version 2 of the License, or
95b2fc499SJeff Garzik  * (at your option) any later version.
105b2fc499SJeff Garzik  *
115b2fc499SJeff Garzik  * This program is distributed in the hope that it will be useful,
125b2fc499SJeff Garzik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
135b2fc499SJeff Garzik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145b2fc499SJeff Garzik  * GNU General Public License for more details.
155b2fc499SJeff Garzik  *
165b2fc499SJeff Garzik  * You should have received a copy of the GNU General Public License
179cb00073SJeff Kirsher  * along with this program; if not, see <http://www.gnu.org/licenses/>.
185b2fc499SJeff Garzik  */
195b2fc499SJeff Garzik 
205b2fc499SJeff Garzik /*
215b2fc499SJeff Garzik  * This is a generic "USB networking" framework that works with several
225b2fc499SJeff Garzik  * kinds of full and high speed networking devices:  host-to-host cables,
235b2fc499SJeff Garzik  * smart usb peripherals, and actual Ethernet adapters.
245b2fc499SJeff Garzik  *
255b2fc499SJeff Garzik  * These devices usually differ in terms of control protocols (if they
265b2fc499SJeff Garzik  * even have one!) and sometimes they define new framing to wrap or batch
275b2fc499SJeff Garzik  * Ethernet packets.  Otherwise, they talk to USB pretty much the same,
285b2fc499SJeff Garzik  * so interface (un)binding, endpoint I/O queues, fault handling, and other
295b2fc499SJeff Garzik  * issues can usefully be addressed by this framework.
305b2fc499SJeff Garzik  */
315b2fc499SJeff Garzik 
325b2fc499SJeff Garzik // #define	DEBUG			// error path messages, extra info
335b2fc499SJeff Garzik // #define	VERBOSE			// more; success messages
345b2fc499SJeff Garzik 
355b2fc499SJeff Garzik #include <linux/module.h>
365b2fc499SJeff Garzik #include <linux/init.h>
375b2fc499SJeff Garzik #include <linux/netdevice.h>
385b2fc499SJeff Garzik #include <linux/etherdevice.h>
3903ad032bSPeter Holik #include <linux/ctype.h>
405b2fc499SJeff Garzik #include <linux/ethtool.h>
415b2fc499SJeff Garzik #include <linux/workqueue.h>
425b2fc499SJeff Garzik #include <linux/mii.h>
435b2fc499SJeff Garzik #include <linux/usb.h>
443692e94fSJussi Kivilinna #include <linux/usb/usbnet.h>
455a0e3ad6STejun Heo #include <linux/slab.h>
46fd1f170dSAndy Shevchenko #include <linux/kernel.h>
47b0786b43SMing Lei #include <linux/pm_runtime.h>
485b2fc499SJeff Garzik 
495b2fc499SJeff Garzik #define DRIVER_VERSION		"22-Aug-2005"
505b2fc499SJeff Garzik 
515b2fc499SJeff Garzik 
525b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
535b2fc499SJeff Garzik 
545b2fc499SJeff Garzik /*
555b2fc499SJeff Garzik  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
565b2fc499SJeff Garzik  * Several dozen bytes of IPv4 data can fit in two such transactions.
575b2fc499SJeff Garzik  * One maximum size Ethernet packet takes twenty four of them.
585b2fc499SJeff Garzik  * For high speed, each frame comfortably fits almost 36 max size
595b2fc499SJeff Garzik  * Ethernet packets (so queues should be bigger).
605b2fc499SJeff Garzik  *
61a88c32aeSMing Lei  * The goal is to let the USB host controller be busy for 5msec or
62a88c32aeSMing Lei  * more before an irq is required, under load.  Jumbograms change
63a88c32aeSMing Lei  * the equation.
645b2fc499SJeff Garzik  */
65a88c32aeSMing Lei #define	MAX_QUEUE_MEMORY	(60 * 1518)
66a88c32aeSMing Lei #define	RX_QLEN(dev)		((dev)->rx_qlen)
67a88c32aeSMing Lei #define	TX_QLEN(dev)		((dev)->tx_qlen)
685b2fc499SJeff Garzik 
695b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
705b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES	(5*HZ)
715b2fc499SJeff Garzik 
7237ebb549SPetr Mladek /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
7337ebb549SPetr Mladek  * us (it polls at HZ/4 usually) before we report too many false errors.
7437ebb549SPetr Mladek  */
755b2fc499SJeff Garzik #define THROTTLE_JIFFIES	(HZ/8)
765b2fc499SJeff Garzik 
775b2fc499SJeff Garzik // between wakeups
785b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS	3
795b2fc499SJeff Garzik 
805b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
815b2fc499SJeff Garzik 
825b2fc499SJeff Garzik // randomly generated ethernet address
835b2fc499SJeff Garzik static u8	node_id [ETH_ALEN];
845b2fc499SJeff Garzik 
855b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
865b2fc499SJeff Garzik static int msg_level = -1;
875b2fc499SJeff Garzik module_param (msg_level, int, 0);
885b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
895b2fc499SJeff Garzik 
905b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
915b2fc499SJeff Garzik 
925b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
935b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
945b2fc499SJeff Garzik {
955b2fc499SJeff Garzik 	int				tmp;
965b2fc499SJeff Garzik 	struct usb_host_interface	*alt = NULL;
975b2fc499SJeff Garzik 	struct usb_host_endpoint	*in = NULL, *out = NULL;
985b2fc499SJeff Garzik 	struct usb_host_endpoint	*status = NULL;
995b2fc499SJeff Garzik 
1005b2fc499SJeff Garzik 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
1015b2fc499SJeff Garzik 		unsigned	ep;
1025b2fc499SJeff Garzik 
1035b2fc499SJeff Garzik 		in = out = status = NULL;
1045b2fc499SJeff Garzik 		alt = intf->altsetting + tmp;
1055b2fc499SJeff Garzik 
1065b2fc499SJeff Garzik 		/* take the first altsetting with in-bulk + out-bulk;
1075b2fc499SJeff Garzik 		 * remember any status endpoint, just in case;
10870f23fd6SJustin P. Mattock 		 * ignore other endpoints and altsettings.
1095b2fc499SJeff Garzik 		 */
1105b2fc499SJeff Garzik 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
1115b2fc499SJeff Garzik 			struct usb_host_endpoint	*e;
1125b2fc499SJeff Garzik 			int				intr = 0;
1135b2fc499SJeff Garzik 
1145b2fc499SJeff Garzik 			e = alt->endpoint + ep;
1155b2fc499SJeff Garzik 			switch (e->desc.bmAttributes) {
1165b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_INT:
1175b2fc499SJeff Garzik 				if (!usb_endpoint_dir_in(&e->desc))
1185b2fc499SJeff Garzik 					continue;
1195b2fc499SJeff Garzik 				intr = 1;
1205b2fc499SJeff Garzik 				/* FALLTHROUGH */
1215b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_BULK:
1225b2fc499SJeff Garzik 				break;
1235b2fc499SJeff Garzik 			default:
1245b2fc499SJeff Garzik 				continue;
1255b2fc499SJeff Garzik 			}
1265b2fc499SJeff Garzik 			if (usb_endpoint_dir_in(&e->desc)) {
1275b2fc499SJeff Garzik 				if (!intr && !in)
1285b2fc499SJeff Garzik 					in = e;
1295b2fc499SJeff Garzik 				else if (intr && !status)
1305b2fc499SJeff Garzik 					status = e;
1315b2fc499SJeff Garzik 			} else {
1325b2fc499SJeff Garzik 				if (!out)
1335b2fc499SJeff Garzik 					out = e;
1345b2fc499SJeff Garzik 			}
1355b2fc499SJeff Garzik 		}
1365b2fc499SJeff Garzik 		if (in && out)
1375b2fc499SJeff Garzik 			break;
1385b2fc499SJeff Garzik 	}
1395b2fc499SJeff Garzik 	if (!alt || !in || !out)
1405b2fc499SJeff Garzik 		return -EINVAL;
1415b2fc499SJeff Garzik 
1428e95a202SJoe Perches 	if (alt->desc.bAlternateSetting != 0 ||
1438e95a202SJoe Perches 	    !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1445b2fc499SJeff Garzik 		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
1455b2fc499SJeff Garzik 				alt->desc.bAlternateSetting);
1465b2fc499SJeff Garzik 		if (tmp < 0)
1475b2fc499SJeff Garzik 			return tmp;
1485b2fc499SJeff Garzik 	}
1495b2fc499SJeff Garzik 
1505b2fc499SJeff Garzik 	dev->in = usb_rcvbulkpipe (dev->udev,
1515b2fc499SJeff Garzik 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1525b2fc499SJeff Garzik 	dev->out = usb_sndbulkpipe (dev->udev,
1535b2fc499SJeff Garzik 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1545b2fc499SJeff Garzik 	dev->status = status;
1555b2fc499SJeff Garzik 	return 0;
1565b2fc499SJeff Garzik }
1575b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1585b2fc499SJeff Garzik 
15903ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
16003ad032bSPeter Holik {
16151487ae7SAndy Shevchenko 	int 		tmp = -1, ret;
16203ad032bSPeter Holik 	unsigned char	buf [13];
16303ad032bSPeter Holik 
16451487ae7SAndy Shevchenko 	ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
16551487ae7SAndy Shevchenko 	if (ret == 12)
16651487ae7SAndy Shevchenko 		tmp = hex2bin(dev->net->dev_addr, buf, 6);
16751487ae7SAndy Shevchenko 	if (tmp < 0) {
16803ad032bSPeter Holik 		dev_dbg(&dev->udev->dev,
16903ad032bSPeter Holik 			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
17051487ae7SAndy Shevchenko 		if (ret >= 0)
17151487ae7SAndy Shevchenko 			ret = -EINVAL;
17251487ae7SAndy Shevchenko 		return ret;
17303ad032bSPeter Holik 	}
17403ad032bSPeter Holik 	return 0;
17503ad032bSPeter Holik }
17603ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
17703ad032bSPeter Holik 
17824ead299Stom.leiming@gmail.com static void intr_complete (struct urb *urb)
17924ead299Stom.leiming@gmail.com {
18024ead299Stom.leiming@gmail.com 	struct usbnet	*dev = urb->context;
18124ead299Stom.leiming@gmail.com 	int		status = urb->status;
18224ead299Stom.leiming@gmail.com 
18324ead299Stom.leiming@gmail.com 	switch (status) {
18424ead299Stom.leiming@gmail.com 	/* success */
18524ead299Stom.leiming@gmail.com 	case 0:
18624ead299Stom.leiming@gmail.com 		dev->driver_info->status(dev, urb);
18724ead299Stom.leiming@gmail.com 		break;
18824ead299Stom.leiming@gmail.com 
18924ead299Stom.leiming@gmail.com 	/* software-driven interface shutdown */
19024ead299Stom.leiming@gmail.com 	case -ENOENT:		/* urb killed */
19124ead299Stom.leiming@gmail.com 	case -ESHUTDOWN:	/* hardware gone */
19224ead299Stom.leiming@gmail.com 		netif_dbg(dev, ifdown, dev->net,
19324ead299Stom.leiming@gmail.com 			  "intr shutdown, code %d\n", status);
19424ead299Stom.leiming@gmail.com 		return;
19524ead299Stom.leiming@gmail.com 
19624ead299Stom.leiming@gmail.com 	/* NOTE:  not throttling like RX/TX, since this endpoint
19724ead299Stom.leiming@gmail.com 	 * already polls infrequently
19824ead299Stom.leiming@gmail.com 	 */
19924ead299Stom.leiming@gmail.com 	default:
20024ead299Stom.leiming@gmail.com 		netdev_dbg(dev->net, "intr status %d\n", status);
20124ead299Stom.leiming@gmail.com 		break;
20224ead299Stom.leiming@gmail.com 	}
20324ead299Stom.leiming@gmail.com 
20424ead299Stom.leiming@gmail.com 	status = usb_submit_urb (urb, GFP_ATOMIC);
20524ead299Stom.leiming@gmail.com 	if (status != 0)
20624ead299Stom.leiming@gmail.com 		netif_err(dev, timer, dev->net,
20724ead299Stom.leiming@gmail.com 			  "intr resubmit --> %d\n", status);
20824ead299Stom.leiming@gmail.com }
2095b2fc499SJeff Garzik 
2105b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
2115b2fc499SJeff Garzik {
2125b2fc499SJeff Garzik 	char		*buf = NULL;
2135b2fc499SJeff Garzik 	unsigned	pipe = 0;
2145b2fc499SJeff Garzik 	unsigned	maxp;
2155b2fc499SJeff Garzik 	unsigned	period;
2165b2fc499SJeff Garzik 
2175b2fc499SJeff Garzik 	if (!dev->driver_info->status)
2185b2fc499SJeff Garzik 		return 0;
2195b2fc499SJeff Garzik 
2205b2fc499SJeff Garzik 	pipe = usb_rcvintpipe (dev->udev,
2215b2fc499SJeff Garzik 			dev->status->desc.bEndpointAddress
2225b2fc499SJeff Garzik 				& USB_ENDPOINT_NUMBER_MASK);
2235b2fc499SJeff Garzik 	maxp = usb_maxpacket (dev->udev, pipe, 0);
2245b2fc499SJeff Garzik 
2255b2fc499SJeff Garzik 	/* avoid 1 msec chatter:  min 8 msec poll rate */
2265b2fc499SJeff Garzik 	period = max ((int) dev->status->desc.bInterval,
2275b2fc499SJeff Garzik 		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
2285b2fc499SJeff Garzik 
2295b2fc499SJeff Garzik 	buf = kmalloc (maxp, GFP_KERNEL);
2305b2fc499SJeff Garzik 	if (buf) {
2315b2fc499SJeff Garzik 		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
2325b2fc499SJeff Garzik 		if (!dev->interrupt) {
2335b2fc499SJeff Garzik 			kfree (buf);
2345b2fc499SJeff Garzik 			return -ENOMEM;
2355b2fc499SJeff Garzik 		} else {
2365b2fc499SJeff Garzik 			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
2375b2fc499SJeff Garzik 				buf, maxp, intr_complete, dev, period);
238720f3d7cStom.leiming@gmail.com 			dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
2395b2fc499SJeff Garzik 			dev_dbg(&intf->dev,
2405b2fc499SJeff Garzik 				"status ep%din, %d bytes period %d\n",
2415b2fc499SJeff Garzik 				usb_pipeendpoint(pipe), maxp, period);
2425b2fc499SJeff Garzik 		}
2435b2fc499SJeff Garzik 	}
2445b2fc499SJeff Garzik 	return 0;
2455b2fc499SJeff Garzik }
2465b2fc499SJeff Garzik 
2476eecdc5fSDan Williams /* Submit the interrupt URB if not previously submitted, increasing refcount */
2486eecdc5fSDan Williams int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
2496eecdc5fSDan Williams {
2506eecdc5fSDan Williams 	int ret = 0;
2516eecdc5fSDan Williams 
2526eecdc5fSDan Williams 	WARN_ON_ONCE(dev->interrupt == NULL);
2536eecdc5fSDan Williams 	if (dev->interrupt) {
2546eecdc5fSDan Williams 		mutex_lock(&dev->interrupt_mutex);
2556eecdc5fSDan Williams 
2566eecdc5fSDan Williams 		if (++dev->interrupt_count == 1)
2576eecdc5fSDan Williams 			ret = usb_submit_urb(dev->interrupt, mem_flags);
2586eecdc5fSDan Williams 
2596eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
2606eecdc5fSDan Williams 			dev->interrupt_count);
2616eecdc5fSDan Williams 		mutex_unlock(&dev->interrupt_mutex);
2626eecdc5fSDan Williams 	}
2636eecdc5fSDan Williams 	return ret;
2646eecdc5fSDan Williams }
2656eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_start);
2666eecdc5fSDan Williams 
2676eecdc5fSDan Williams /* For resume; submit interrupt URB if previously submitted */
2686eecdc5fSDan Williams static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
2696eecdc5fSDan Williams {
2706eecdc5fSDan Williams 	int ret = 0;
2716eecdc5fSDan Williams 
2726eecdc5fSDan Williams 	mutex_lock(&dev->interrupt_mutex);
2736eecdc5fSDan Williams 	if (dev->interrupt_count) {
2746eecdc5fSDan Williams 		ret = usb_submit_urb(dev->interrupt, mem_flags);
2756eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev,
2766eecdc5fSDan Williams 			"submitted interrupt URB for resume\n");
2776eecdc5fSDan Williams 	}
2786eecdc5fSDan Williams 	mutex_unlock(&dev->interrupt_mutex);
2796eecdc5fSDan Williams 	return ret;
2806eecdc5fSDan Williams }
2816eecdc5fSDan Williams 
2826eecdc5fSDan Williams /* Kill the interrupt URB if all submitters want it killed */
2836eecdc5fSDan Williams void usbnet_status_stop(struct usbnet *dev)
2846eecdc5fSDan Williams {
2856eecdc5fSDan Williams 	if (dev->interrupt) {
2866eecdc5fSDan Williams 		mutex_lock(&dev->interrupt_mutex);
2876eecdc5fSDan Williams 		WARN_ON(dev->interrupt_count == 0);
2886eecdc5fSDan Williams 
2896eecdc5fSDan Williams 		if (dev->interrupt_count && --dev->interrupt_count == 0)
2906eecdc5fSDan Williams 			usb_kill_urb(dev->interrupt);
2916eecdc5fSDan Williams 
2926eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev,
2936eecdc5fSDan Williams 			"decremented interrupt URB count to %d\n",
2946eecdc5fSDan Williams 			dev->interrupt_count);
2956eecdc5fSDan Williams 		mutex_unlock(&dev->interrupt_mutex);
2966eecdc5fSDan Williams 	}
2976eecdc5fSDan Williams }
2986eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_stop);
2996eecdc5fSDan Williams 
3006eecdc5fSDan Williams /* For suspend; always kill interrupt URB */
3016eecdc5fSDan Williams static void __usbnet_status_stop_force(struct usbnet *dev)
3026eecdc5fSDan Williams {
3036eecdc5fSDan Williams 	if (dev->interrupt) {
3046eecdc5fSDan Williams 		mutex_lock(&dev->interrupt_mutex);
3056eecdc5fSDan Williams 		usb_kill_urb(dev->interrupt);
3066eecdc5fSDan Williams 		dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
3076eecdc5fSDan Williams 		mutex_unlock(&dev->interrupt_mutex);
3086eecdc5fSDan Williams 	}
3096eecdc5fSDan Williams }
3106eecdc5fSDan Williams 
3115b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
3125b2fc499SJeff Garzik  * Some link protocols batch packets, so their rx_fixup paths
3135b2fc499SJeff Garzik  * can return clones as well as just modify the original skb.
3145b2fc499SJeff Garzik  */
3155b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
3165b2fc499SJeff Garzik {
317c8b5d129SGreg Ungerer 	struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
3185b2fc499SJeff Garzik 	int	status;
3195b2fc499SJeff Garzik 
3207834ddbcSJussi Kivilinna 	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
3217834ddbcSJussi Kivilinna 		skb_queue_tail(&dev->rxq_pause, skb);
3227834ddbcSJussi Kivilinna 		return;
3237834ddbcSJussi Kivilinna 	}
3247834ddbcSJussi Kivilinna 
32581e0ce79SBjørn Mork 	/* only update if unset to allow minidriver rx_fixup override */
32681e0ce79SBjørn Mork 	if (skb->protocol == 0)
3275b2fc499SJeff Garzik 		skb->protocol = eth_type_trans (skb, dev->net);
32881e0ce79SBjørn Mork 
329c8b5d129SGreg Ungerer 	u64_stats_update_begin(&stats64->syncp);
330c8b5d129SGreg Ungerer 	stats64->rx_packets++;
331c8b5d129SGreg Ungerer 	stats64->rx_bytes += skb->len;
332c8b5d129SGreg Ungerer 	u64_stats_update_end(&stats64->syncp);
3335b2fc499SJeff Garzik 
334a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
3355b2fc499SJeff Garzik 		  skb->len + sizeof (struct ethhdr), skb->protocol);
3365b2fc499SJeff Garzik 	memset (skb->cb, 0, sizeof (struct skb_data));
337f9b491ecSMichael Riesch 
338f9b491ecSMichael Riesch 	if (skb_defer_rx_timestamp(skb))
339f9b491ecSMichael Riesch 		return;
340f9b491ecSMichael Riesch 
3415b2fc499SJeff Garzik 	status = netif_rx (skb);
342a475f603SJoe Perches 	if (status != NET_RX_SUCCESS)
343a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net,
344a475f603SJoe Perches 			  "netif_rx status %d\n", status);
3455b2fc499SJeff Garzik }
3465b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
3475b2fc499SJeff Garzik 
348a88c32aeSMing Lei /* must be called if hard_mtu or rx_urb_size changed */
349a88c32aeSMing Lei void usbnet_update_max_qlen(struct usbnet *dev)
350a88c32aeSMing Lei {
351a88c32aeSMing Lei 	enum usb_device_speed speed = dev->udev->speed;
352a88c32aeSMing Lei 
353a88c32aeSMing Lei 	switch (speed) {
354a88c32aeSMing Lei 	case USB_SPEED_HIGH:
355a88c32aeSMing Lei 		dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
356a88c32aeSMing Lei 		dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
357a88c32aeSMing Lei 		break;
358452c447aSMing Lei 	case USB_SPEED_SUPER:
359ea079842SOliver Neukum 	case USB_SPEED_SUPER_PLUS:
360452c447aSMing Lei 		/*
361452c447aSMing Lei 		 * Not take default 5ms qlen for super speed HC to
362452c447aSMing Lei 		 * save memory, and iperf tests show 2.5ms qlen can
363452c447aSMing Lei 		 * work well
364452c447aSMing Lei 		 */
365452c447aSMing Lei 		dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
366452c447aSMing Lei 		dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
367452c447aSMing Lei 		break;
368a88c32aeSMing Lei 	default:
369a88c32aeSMing Lei 		dev->rx_qlen = dev->tx_qlen = 4;
370a88c32aeSMing Lei 	}
371a88c32aeSMing Lei }
372a88c32aeSMing Lei EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
373a88c32aeSMing Lei 
3745b2fc499SJeff Garzik 
3755b2fc499SJeff Garzik /*-------------------------------------------------------------------------
3765b2fc499SJeff Garzik  *
3775b2fc499SJeff Garzik  * Network Device Driver (peer link to "Host Device", from USB host)
3785b2fc499SJeff Garzik  *
3795b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
3805b2fc499SJeff Garzik 
381777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu)
3825b2fc499SJeff Garzik {
3835b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
3845b2fc499SJeff Garzik 	int		ll_mtu = new_mtu + net->hard_header_len;
3855b2fc499SJeff Garzik 	int		old_hard_mtu = dev->hard_mtu;
3865b2fc499SJeff Garzik 	int		old_rx_urb_size = dev->rx_urb_size;
3875b2fc499SJeff Garzik 
3885b2fc499SJeff Garzik 	// no second zero-length packet read wanted after mtu-sized packets
3895b2fc499SJeff Garzik 	if ((ll_mtu % dev->maxpacket) == 0)
3905b2fc499SJeff Garzik 		return -EDOM;
3915b2fc499SJeff Garzik 	net->mtu = new_mtu;
3925b2fc499SJeff Garzik 
3935b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
3945b2fc499SJeff Garzik 	if (dev->rx_urb_size == old_hard_mtu) {
3955b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
39643daa96bSSoohoon Lee 		if (dev->rx_urb_size > old_rx_urb_size) {
39743daa96bSSoohoon Lee 			usbnet_pause_rx(dev);
3985b2fc499SJeff Garzik 			usbnet_unlink_rx_urbs(dev);
39943daa96bSSoohoon Lee 			usbnet_resume_rx(dev);
40043daa96bSSoohoon Lee 		}
4015b2fc499SJeff Garzik 	}
4025b2fc499SJeff Garzik 
403a88c32aeSMing Lei 	/* max qlen depend on hard_mtu and rx_urb_size */
404a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
405a88c32aeSMing Lei 
4065b2fc499SJeff Garzik 	return 0;
4075b2fc499SJeff Garzik }
408777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu);
4095b2fc499SJeff Garzik 
4105b6e9bcdSMing Lei /* The caller must hold list->lock */
4115b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list,
4125b6e9bcdSMing Lei 			struct sk_buff *newsk, enum skb_state state)
4135b6e9bcdSMing Lei {
4145b6e9bcdSMing Lei 	struct skb_data *entry = (struct skb_data *) newsk->cb;
4155b6e9bcdSMing Lei 
4165b6e9bcdSMing Lei 	__skb_queue_tail(list, newsk);
4175b6e9bcdSMing Lei 	entry->state = state;
4185b6e9bcdSMing Lei }
4195b6e9bcdSMing Lei 
4205b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4215b2fc499SJeff Garzik 
4225b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
4235b2fc499SJeff Garzik  * completion callbacks.  2.5 should have fixed those bugs...
4245b2fc499SJeff Garzik  */
4255b2fc499SJeff Garzik 
4265b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
4275b6e9bcdSMing Lei 		struct sk_buff_head *list, enum skb_state state)
4285b2fc499SJeff Garzik {
4295b2fc499SJeff Garzik 	unsigned long		flags;
4305b6e9bcdSMing Lei 	enum skb_state 		old_state;
4315b6e9bcdSMing Lei 	struct skb_data *entry = (struct skb_data *) skb->cb;
4325b2fc499SJeff Garzik 
4335b2fc499SJeff Garzik 	spin_lock_irqsave(&list->lock, flags);
4345b6e9bcdSMing Lei 	old_state = entry->state;
4355b6e9bcdSMing Lei 	entry->state = state;
4365b2fc499SJeff Garzik 	__skb_unlink(skb, list);
437fcb0bb6aSEugene Shatokhin 
438fcb0bb6aSEugene Shatokhin 	/* defer_bh() is never called with list == &dev->done.
439fcb0bb6aSEugene Shatokhin 	 * spin_lock_nested() tells lockdep that it is OK to take
440fcb0bb6aSEugene Shatokhin 	 * dev->done.lock here with list->lock held.
441fcb0bb6aSEugene Shatokhin 	 */
442fcb0bb6aSEugene Shatokhin 	spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
443fcb0bb6aSEugene Shatokhin 
4445b2fc499SJeff Garzik 	__skb_queue_tail(&dev->done, skb);
4455b2fc499SJeff Garzik 	if (dev->done.qlen == 1)
4465b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
447fcb0bb6aSEugene Shatokhin 	spin_unlock(&dev->done.lock);
448fcb0bb6aSEugene Shatokhin 	spin_unlock_irqrestore(&list->lock, flags);
4495b6e9bcdSMing Lei 	return old_state;
4505b2fc499SJeff Garzik }
4515b2fc499SJeff Garzik 
4525b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
4535b2fc499SJeff Garzik  *
4545b2fc499SJeff Garzik  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
4555b2fc499SJeff Garzik  * but tasklet_schedule() doesn't.  hope the failure is rare.
4565b2fc499SJeff Garzik  */
4575b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
4585b2fc499SJeff Garzik {
4595b2fc499SJeff Garzik 	set_bit (work, &dev->flags);
460*ab18a9c9SOliver Neukum 	if (!schedule_work (&dev->kevent))
461*ab18a9c9SOliver Neukum 		netdev_dbg(dev->net, "kevent %d may have been dropped\n", work);
462*ab18a9c9SOliver Neukum 	else
46360b86755SJoe Perches 		netdev_dbg(dev->net, "kevent %d scheduled\n", work);
4645b2fc499SJeff Garzik }
4655b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
4665b2fc499SJeff Garzik 
4675b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4685b2fc499SJeff Garzik 
4695b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
4705b2fc499SJeff Garzik 
471dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
4725b2fc499SJeff Garzik {
4735b2fc499SJeff Garzik 	struct sk_buff		*skb;
4745b2fc499SJeff Garzik 	struct skb_data		*entry;
4755b2fc499SJeff Garzik 	int			retval = 0;
4765b2fc499SJeff Garzik 	unsigned long		lockflags;
4775b2fc499SJeff Garzik 	size_t			size = dev->rx_urb_size;
4785b2fc499SJeff Garzik 
47970c37bf9SBjørn Mork 	/* prevent rx skb allocation when error ratio is high */
48070c37bf9SBjørn Mork 	if (test_bit(EVENT_RX_KILL, &dev->flags)) {
48170c37bf9SBjørn Mork 		usb_free_urb(urb);
48270c37bf9SBjørn Mork 		return -ENOLINK;
48370c37bf9SBjørn Mork 	}
48470c37bf9SBjørn Mork 
485a4abd7a8SBjørn Mork 	if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
486a4abd7a8SBjørn Mork 		skb = __netdev_alloc_skb(dev->net, size, flags);
487a4abd7a8SBjørn Mork 	else
4887bdd4027SEric Dumazet 		skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
4897bdd4027SEric Dumazet 	if (!skb) {
490a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
4915b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
4925b2fc499SJeff Garzik 		usb_free_urb (urb);
493dacb3975SDavid S. Miller 		return -ENOMEM;
4945b2fc499SJeff Garzik 	}
4955b2fc499SJeff Garzik 
4965b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
4975b2fc499SJeff Garzik 	entry->urb = urb;
4985b2fc499SJeff Garzik 	entry->dev = dev;
4995b2fc499SJeff Garzik 	entry->length = 0;
5005b2fc499SJeff Garzik 
5015b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
5025b2fc499SJeff Garzik 		skb->data, size, rx_complete, skb);
5035b2fc499SJeff Garzik 
5045b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
5055b2fc499SJeff Garzik 
5068e95a202SJoe Perches 	if (netif_running (dev->net) &&
5078e95a202SJoe Perches 	    netif_device_present (dev->net) &&
50869ee472fSOliver Neukum 	    !test_bit (EVENT_RX_HALT, &dev->flags) &&
50969ee472fSOliver Neukum 	    !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
5105b2fc499SJeff Garzik 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
5115b2fc499SJeff Garzik 		case -EPIPE:
5125b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
5135b2fc499SJeff Garzik 			break;
5145b2fc499SJeff Garzik 		case -ENOMEM:
5155b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
5165b2fc499SJeff Garzik 			break;
5175b2fc499SJeff Garzik 		case -ENODEV:
518a475f603SJoe Perches 			netif_dbg(dev, ifdown, dev->net, "device gone\n");
5195b2fc499SJeff Garzik 			netif_device_detach (dev->net);
5205b2fc499SJeff Garzik 			break;
521dacb3975SDavid S. Miller 		case -EHOSTUNREACH:
522dacb3975SDavid S. Miller 			retval = -ENOLINK;
523dacb3975SDavid S. Miller 			break;
5245b2fc499SJeff Garzik 		default:
525a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
526a475f603SJoe Perches 				  "rx submit, %d\n", retval);
5275b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
5285b2fc499SJeff Garzik 			break;
5295b2fc499SJeff Garzik 		case 0:
5305b6e9bcdSMing Lei 			__usbnet_queue_skb(&dev->rxq, skb, rx_start);
5315b2fc499SJeff Garzik 		}
5325b2fc499SJeff Garzik 	} else {
533a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
5345b2fc499SJeff Garzik 		retval = -ENOLINK;
5355b2fc499SJeff Garzik 	}
5365b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
5375b2fc499SJeff Garzik 	if (retval) {
5385b2fc499SJeff Garzik 		dev_kfree_skb_any (skb);
5395b2fc499SJeff Garzik 		usb_free_urb (urb);
5405b2fc499SJeff Garzik 	}
541dacb3975SDavid S. Miller 	return retval;
5425b2fc499SJeff Garzik }
5435b2fc499SJeff Garzik 
5445b2fc499SJeff Garzik 
5455b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5465b2fc499SJeff Garzik 
5475b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
5485b2fc499SJeff Garzik {
5498e95a202SJoe Perches 	if (dev->driver_info->rx_fixup &&
5507a635ea9SAndrzej Zaborowski 	    !dev->driver_info->rx_fixup (dev, skb)) {
5517a635ea9SAndrzej Zaborowski 		/* With RX_ASSEMBLE, rx_fixup() must update counters */
5527a635ea9SAndrzej Zaborowski 		if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
5537a635ea9SAndrzej Zaborowski 			dev->net->stats.rx_errors++;
5547a635ea9SAndrzej Zaborowski 		goto done;
5557a635ea9SAndrzej Zaborowski 	}
5565b2fc499SJeff Garzik 	// else network stack removes extra byte if we forced a short packet
5575b2fc499SJeff Garzik 
558073285fdSAlexey Orishko 	/* all data was already cloned from skb inside the driver */
559073285fdSAlexey Orishko 	if (dev->driver_info->flags & FLAG_MULTI_PACKET)
560eb85569fSEmil Goode 		goto done;
561eb85569fSEmil Goode 
562eb85569fSEmil Goode 	if (skb->len < ETH_HLEN) {
563eb85569fSEmil Goode 		dev->net->stats.rx_errors++;
564eb85569fSEmil Goode 		dev->net->stats.rx_length_errors++;
565eb85569fSEmil Goode 		netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
566eb85569fSEmil Goode 	} else {
5675b2fc499SJeff Garzik 		usbnet_skb_return(dev, skb);
568073285fdSAlexey Orishko 		return;
569073285fdSAlexey Orishko 	}
570073285fdSAlexey Orishko 
5717a635ea9SAndrzej Zaborowski done:
5725b2fc499SJeff Garzik 	skb_queue_tail(&dev->done, skb);
5735b2fc499SJeff Garzik }
5745b2fc499SJeff Garzik 
5755b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5765b2fc499SJeff Garzik 
5775b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
5785b2fc499SJeff Garzik {
5795b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
5805b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
5815b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
5825b2fc499SJeff Garzik 	int			urb_status = urb->status;
5835b6e9bcdSMing Lei 	enum skb_state		state;
5845b2fc499SJeff Garzik 
5855b2fc499SJeff Garzik 	skb_put (skb, urb->actual_length);
5865b6e9bcdSMing Lei 	state = rx_done;
5875b2fc499SJeff Garzik 	entry->urb = NULL;
5885b2fc499SJeff Garzik 
5895b2fc499SJeff Garzik 	switch (urb_status) {
59018ab458fSDavid Brownell 	/* success */
5915b2fc499SJeff Garzik 	case 0:
5925b2fc499SJeff Garzik 		break;
5935b2fc499SJeff Garzik 
59418ab458fSDavid Brownell 	/* stalls need manual reset. this is rare ... except that
59518ab458fSDavid Brownell 	 * when going through USB 2.0 TTs, unplug appears this way.
5963ac49a1cSJean Delvare 	 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
59718ab458fSDavid Brownell 	 * storm, recovering as needed.
59818ab458fSDavid Brownell 	 */
5995b2fc499SJeff Garzik 	case -EPIPE:
6007963837fSHerbert Xu 		dev->net->stats.rx_errors++;
6015b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
6025b2fc499SJeff Garzik 		// FALLTHROUGH
6035b2fc499SJeff Garzik 
60418ab458fSDavid Brownell 	/* software-driven interface shutdown */
60518ab458fSDavid Brownell 	case -ECONNRESET:		/* async unlink */
60618ab458fSDavid Brownell 	case -ESHUTDOWN:		/* hardware gone */
607a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net,
608a475f603SJoe Perches 			  "rx shutdown, code %d\n", urb_status);
6095b2fc499SJeff Garzik 		goto block;
6105b2fc499SJeff Garzik 
61137ebb549SPetr Mladek 	/* we get controller i/o faults during hub_wq disconnect() delays.
61218ab458fSDavid Brownell 	 * throttle down resubmits, to avoid log floods; just temporarily,
61337ebb549SPetr Mladek 	 * so we still recover when the fault isn't a hub_wq delay.
61418ab458fSDavid Brownell 	 */
6155b2fc499SJeff Garzik 	case -EPROTO:
6165b2fc499SJeff Garzik 	case -ETIME:
6175b2fc499SJeff Garzik 	case -EILSEQ:
6187963837fSHerbert Xu 		dev->net->stats.rx_errors++;
6195b2fc499SJeff Garzik 		if (!timer_pending (&dev->delay)) {
6205b2fc499SJeff Garzik 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
621a475f603SJoe Perches 			netif_dbg(dev, link, dev->net,
622a475f603SJoe Perches 				  "rx throttle %d\n", urb_status);
6235b2fc499SJeff Garzik 		}
6245b2fc499SJeff Garzik block:
6255b6e9bcdSMing Lei 		state = rx_cleanup;
6265b2fc499SJeff Garzik 		entry->urb = urb;
6275b2fc499SJeff Garzik 		urb = NULL;
6285b2fc499SJeff Garzik 		break;
6295b2fc499SJeff Garzik 
63018ab458fSDavid Brownell 	/* data overrun ... flush fifo? */
6315b2fc499SJeff Garzik 	case -EOVERFLOW:
6327963837fSHerbert Xu 		dev->net->stats.rx_over_errors++;
6335b2fc499SJeff Garzik 		// FALLTHROUGH
6345b2fc499SJeff Garzik 
6355b2fc499SJeff Garzik 	default:
6365b6e9bcdSMing Lei 		state = rx_cleanup;
6377963837fSHerbert Xu 		dev->net->stats.rx_errors++;
638a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
6395b2fc499SJeff Garzik 		break;
6405b2fc499SJeff Garzik 	}
6415b2fc499SJeff Garzik 
64270c37bf9SBjørn Mork 	/* stop rx if packet error rate is high */
64370c37bf9SBjørn Mork 	if (++dev->pkt_cnt > 30) {
64470c37bf9SBjørn Mork 		dev->pkt_cnt = 0;
64570c37bf9SBjørn Mork 		dev->pkt_err = 0;
64670c37bf9SBjørn Mork 	} else {
64770c37bf9SBjørn Mork 		if (state == rx_cleanup)
64870c37bf9SBjørn Mork 			dev->pkt_err++;
64970c37bf9SBjørn Mork 		if (dev->pkt_err > 20)
65070c37bf9SBjørn Mork 			set_bit(EVENT_RX_KILL, &dev->flags);
65170c37bf9SBjørn Mork 	}
65270c37bf9SBjørn Mork 
6535b6e9bcdSMing Lei 	state = defer_bh(dev, skb, &dev->rxq, state);
6545b2fc499SJeff Garzik 
6555b2fc499SJeff Garzik 	if (urb) {
6568e95a202SJoe Perches 		if (netif_running (dev->net) &&
6575b6e9bcdSMing Lei 		    !test_bit (EVENT_RX_HALT, &dev->flags) &&
6585b6e9bcdSMing Lei 		    state != unlink_start) {
6595b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_ATOMIC);
6608a783354SOliver Neukum 			usb_mark_last_busy(dev->udev);
6615b2fc499SJeff Garzik 			return;
6625b2fc499SJeff Garzik 		}
6635b2fc499SJeff Garzik 		usb_free_urb (urb);
6645b2fc499SJeff Garzik 	}
665a475f603SJoe Perches 	netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
6665b2fc499SJeff Garzik }
6675b2fc499SJeff Garzik 
6685b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
6697834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev)
6707834ddbcSJussi Kivilinna {
6717834ddbcSJussi Kivilinna 	set_bit(EVENT_RX_PAUSED, &dev->flags);
6727834ddbcSJussi Kivilinna 
673a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
6747834ddbcSJussi Kivilinna }
6757834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx);
6767834ddbcSJussi Kivilinna 
6777834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev)
6787834ddbcSJussi Kivilinna {
6797834ddbcSJussi Kivilinna 	struct sk_buff *skb;
6807834ddbcSJussi Kivilinna 	int num = 0;
6817834ddbcSJussi Kivilinna 
6827834ddbcSJussi Kivilinna 	clear_bit(EVENT_RX_PAUSED, &dev->flags);
6837834ddbcSJussi Kivilinna 
6847834ddbcSJussi Kivilinna 	while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
6857834ddbcSJussi Kivilinna 		usbnet_skb_return(dev, skb);
6867834ddbcSJussi Kivilinna 		num++;
6877834ddbcSJussi Kivilinna 	}
6887834ddbcSJussi Kivilinna 
6897834ddbcSJussi Kivilinna 	tasklet_schedule(&dev->bh);
6907834ddbcSJussi Kivilinna 
691a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net,
692a475f603SJoe Perches 		  "paused rx queue disabled, %d skbs requeued\n", num);
6937834ddbcSJussi Kivilinna }
6947834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx);
6957834ddbcSJussi Kivilinna 
6967834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev)
6977834ddbcSJussi Kivilinna {
6987834ddbcSJussi Kivilinna 	skb_queue_purge(&dev->rxq_pause);
6997834ddbcSJussi Kivilinna }
7007834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
7017834ddbcSJussi Kivilinna 
7027834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/
7035b2fc499SJeff Garzik 
7045b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
7055b2fc499SJeff Garzik 
7065b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
7075b2fc499SJeff Garzik {
7085b2fc499SJeff Garzik 	unsigned long		flags;
7095b6e9bcdSMing Lei 	struct sk_buff		*skb;
7105b2fc499SJeff Garzik 	int			count = 0;
7115b2fc499SJeff Garzik 
7125b2fc499SJeff Garzik 	spin_lock_irqsave (&q->lock, flags);
7135b6e9bcdSMing Lei 	while (!skb_queue_empty(q)) {
7145b2fc499SJeff Garzik 		struct skb_data		*entry;
7155b2fc499SJeff Garzik 		struct urb		*urb;
7165b2fc499SJeff Garzik 		int			retval;
7175b2fc499SJeff Garzik 
7185b6e9bcdSMing Lei 		skb_queue_walk(q, skb) {
7195b2fc499SJeff Garzik 			entry = (struct skb_data *) skb->cb;
7205b6e9bcdSMing Lei 			if (entry->state != unlink_start)
7215b6e9bcdSMing Lei 				goto found;
7225b6e9bcdSMing Lei 		}
7235b6e9bcdSMing Lei 		break;
7245b6e9bcdSMing Lei found:
7255b6e9bcdSMing Lei 		entry->state = unlink_start;
7265b2fc499SJeff Garzik 		urb = entry->urb;
7275b2fc499SJeff Garzik 
7280956a8c2Stom.leiming@gmail.com 		/*
7290956a8c2Stom.leiming@gmail.com 		 * Get reference count of the URB to avoid it to be
7300956a8c2Stom.leiming@gmail.com 		 * freed during usb_unlink_urb, which may trigger
7310956a8c2Stom.leiming@gmail.com 		 * use-after-free problem inside usb_unlink_urb since
7320956a8c2Stom.leiming@gmail.com 		 * usb_unlink_urb is always racing with .complete
7330956a8c2Stom.leiming@gmail.com 		 * handler(include defer_bh).
7340956a8c2Stom.leiming@gmail.com 		 */
7350956a8c2Stom.leiming@gmail.com 		usb_get_urb(urb);
7364231d47eSSebastian Siewior 		spin_unlock_irqrestore(&q->lock, flags);
7375b2fc499SJeff Garzik 		// during some PM-driven resume scenarios,
7385b2fc499SJeff Garzik 		// these (async) unlinks complete immediately
7395b2fc499SJeff Garzik 		retval = usb_unlink_urb (urb);
7405b2fc499SJeff Garzik 		if (retval != -EINPROGRESS && retval != 0)
74160b86755SJoe Perches 			netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
7425b2fc499SJeff Garzik 		else
7435b2fc499SJeff Garzik 			count++;
7440956a8c2Stom.leiming@gmail.com 		usb_put_urb(urb);
7454231d47eSSebastian Siewior 		spin_lock_irqsave(&q->lock, flags);
7465b2fc499SJeff Garzik 	}
7475b2fc499SJeff Garzik 	spin_unlock_irqrestore (&q->lock, flags);
7485b2fc499SJeff Garzik 	return count;
7495b2fc499SJeff Garzik }
7505b2fc499SJeff Garzik 
7515b2fc499SJeff Garzik // Flush all pending rx urbs
7525b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
7535b2fc499SJeff Garzik 
7545b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
7555b2fc499SJeff Garzik {
7565b2fc499SJeff Garzik 	if (netif_running(dev->net)) {
7575b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
7585b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
7595b2fc499SJeff Garzik 	}
7605b2fc499SJeff Garzik }
7615b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
7625b2fc499SJeff Garzik 
7635b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
7645b2fc499SJeff Garzik 
765fcb0bb6aSEugene Shatokhin static void wait_skb_queue_empty(struct sk_buff_head *q)
766fcb0bb6aSEugene Shatokhin {
767fcb0bb6aSEugene Shatokhin 	unsigned long flags;
768fcb0bb6aSEugene Shatokhin 
769fcb0bb6aSEugene Shatokhin 	spin_lock_irqsave(&q->lock, flags);
770fcb0bb6aSEugene Shatokhin 	while (!skb_queue_empty(q)) {
771fcb0bb6aSEugene Shatokhin 		spin_unlock_irqrestore(&q->lock, flags);
772fcb0bb6aSEugene Shatokhin 		schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
773fcb0bb6aSEugene Shatokhin 		set_current_state(TASK_UNINTERRUPTIBLE);
774fcb0bb6aSEugene Shatokhin 		spin_lock_irqsave(&q->lock, flags);
775fcb0bb6aSEugene Shatokhin 	}
776fcb0bb6aSEugene Shatokhin 	spin_unlock_irqrestore(&q->lock, flags);
777fcb0bb6aSEugene Shatokhin }
778fcb0bb6aSEugene Shatokhin 
7795b2fc499SJeff Garzik // precondition: never called in_interrupt
78069ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev)
78169ee472fSOliver Neukum {
78269ee472fSOliver Neukum 	DECLARE_WAITQUEUE(wait, current);
78369ee472fSOliver Neukum 	int temp;
78469ee472fSOliver Neukum 
78569ee472fSOliver Neukum 	/* ensure there are no more active urbs */
78614a0d635SOliver Neukum 	add_wait_queue(&dev->wait, &wait);
78769ee472fSOliver Neukum 	set_current_state(TASK_UNINTERRUPTIBLE);
78869ee472fSOliver Neukum 	temp = unlink_urbs(dev, &dev->txq) +
78969ee472fSOliver Neukum 		unlink_urbs(dev, &dev->rxq);
79069ee472fSOliver Neukum 
79169ee472fSOliver Neukum 	/* maybe wait for deletions to finish. */
792fcb0bb6aSEugene Shatokhin 	wait_skb_queue_empty(&dev->rxq);
793fcb0bb6aSEugene Shatokhin 	wait_skb_queue_empty(&dev->txq);
794fcb0bb6aSEugene Shatokhin 	wait_skb_queue_empty(&dev->done);
795a475f603SJoe Perches 	netif_dbg(dev, ifdown, dev->net,
796a475f603SJoe Perches 		  "waited for %d urb completions\n", temp);
79769ee472fSOliver Neukum 	set_current_state(TASK_RUNNING);
79814a0d635SOliver Neukum 	remove_wait_queue(&dev->wait, &wait);
79969ee472fSOliver Neukum }
8005b2fc499SJeff Garzik 
801777baa47SStephen Hemminger int usbnet_stop (struct net_device *net)
8025b2fc499SJeff Garzik {
8035b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
804a33e9e7fSJussi Kivilinna 	struct driver_info	*info = dev->driver_info;
805f50791acSEugene Shatokhin 	int			retval, pm, mpn;
8065b2fc499SJeff Garzik 
80775bd0cbdSMing Lei 	clear_bit(EVENT_DEV_OPEN, &dev->flags);
8085b2fc499SJeff Garzik 	netif_stop_queue (net);
8095b2fc499SJeff Garzik 
810a475f603SJoe Perches 	netif_info(dev, ifdown, dev->net,
8118ccef431SBen Hutchings 		   "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
8127963837fSHerbert Xu 		   net->stats.rx_packets, net->stats.tx_packets,
81360b86755SJoe Perches 		   net->stats.rx_errors, net->stats.tx_errors);
8145b2fc499SJeff Garzik 
81514a0d635SOliver Neukum 	/* to not race resume */
81614a0d635SOliver Neukum 	pm = usb_autopm_get_interface(dev->intf);
817a33e9e7fSJussi Kivilinna 	/* allow minidriver to stop correctly (wireless devices to turn off
818a33e9e7fSJussi Kivilinna 	 * radio etc) */
819a33e9e7fSJussi Kivilinna 	if (info->stop) {
820a33e9e7fSJussi Kivilinna 		retval = info->stop(dev);
821a475f603SJoe Perches 		if (retval < 0)
822a475f603SJoe Perches 			netif_info(dev, ifdown, dev->net,
82360b86755SJoe Perches 				   "stop fail (%d) usbnet usb-%s-%s, %s\n",
824a33e9e7fSJussi Kivilinna 				   retval,
825a33e9e7fSJussi Kivilinna 				   dev->udev->bus->bus_name, dev->udev->devpath,
826a33e9e7fSJussi Kivilinna 				   info->description);
827a33e9e7fSJussi Kivilinna 	}
828a33e9e7fSJussi Kivilinna 
82969ee472fSOliver Neukum 	if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
83069ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
8315b2fc499SJeff Garzik 
8326eecdc5fSDan Williams 	usbnet_status_stop(dev);
8335b2fc499SJeff Garzik 
8347834ddbcSJussi Kivilinna 	usbnet_purge_paused_rxq(dev);
8357834ddbcSJussi Kivilinna 
836f50791acSEugene Shatokhin 	mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
837f50791acSEugene Shatokhin 
8385b2fc499SJeff Garzik 	/* deferred work (task, timer, softirq) must also stop.
8395b2fc499SJeff Garzik 	 * can't flush_scheduled_work() until we drop rtnl (later),
8405b2fc499SJeff Garzik 	 * else workers could deadlock; so make workers a NOP.
8415b2fc499SJeff Garzik 	 */
8425b2fc499SJeff Garzik 	dev->flags = 0;
8435b2fc499SJeff Garzik 	del_timer_sync (&dev->delay);
8445b2fc499SJeff Garzik 	tasklet_kill (&dev->bh);
84514a0d635SOliver Neukum 	if (!pm)
84614a0d635SOliver Neukum 		usb_autopm_put_interface(dev->intf);
84714a0d635SOliver Neukum 
848f50791acSEugene Shatokhin 	if (info->manage_power && mpn)
84969ee472fSOliver Neukum 		info->manage_power(dev, 0);
85069ee472fSOliver Neukum 	else
851a11a6544SOliver Neukum 		usb_autopm_put_interface(dev->intf);
8525b2fc499SJeff Garzik 
8535b2fc499SJeff Garzik 	return 0;
8545b2fc499SJeff Garzik }
855777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop);
8565b2fc499SJeff Garzik 
8575b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
8585b2fc499SJeff Garzik 
8595b2fc499SJeff Garzik // posts reads, and enables write queuing
8605b2fc499SJeff Garzik 
8615b2fc499SJeff Garzik // precondition: never called in_interrupt
8625b2fc499SJeff Garzik 
863777baa47SStephen Hemminger int usbnet_open (struct net_device *net)
8645b2fc499SJeff Garzik {
8655b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
866a11a6544SOliver Neukum 	int			retval;
8675b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
8685b2fc499SJeff Garzik 
869a11a6544SOliver Neukum 	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
870a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
87160b86755SJoe Perches 			   "resumption fail (%d) usbnet usb-%s-%s, %s\n",
872a11a6544SOliver Neukum 			   retval,
87360b86755SJoe Perches 			   dev->udev->bus->bus_name,
87460b86755SJoe Perches 			   dev->udev->devpath,
875a11a6544SOliver Neukum 			   info->description);
876a11a6544SOliver Neukum 		goto done_nopm;
877a11a6544SOliver Neukum 	}
878a11a6544SOliver Neukum 
8795b2fc499SJeff Garzik 	// put into "known safe" state
8805b2fc499SJeff Garzik 	if (info->reset && (retval = info->reset (dev)) < 0) {
881a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
88260b86755SJoe Perches 			   "open reset fail (%d) usbnet usb-%s-%s, %s\n",
8835b2fc499SJeff Garzik 			   retval,
88460b86755SJoe Perches 			   dev->udev->bus->bus_name,
88560b86755SJoe Perches 			   dev->udev->devpath,
8865b2fc499SJeff Garzik 			   info->description);
8875b2fc499SJeff Garzik 		goto done;
8885b2fc499SJeff Garzik 	}
8895b2fc499SJeff Garzik 
890a88c32aeSMing Lei 	/* hard_mtu or rx_urb_size may change in reset() */
891a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
892a88c32aeSMing Lei 
8935b2fc499SJeff Garzik 	// insist peer be connected
8945b2fc499SJeff Garzik 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
895a475f603SJoe Perches 		netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
8965b2fc499SJeff Garzik 		goto done;
8975b2fc499SJeff Garzik 	}
8985b2fc499SJeff Garzik 
8995b2fc499SJeff Garzik 	/* start any status interrupt transfer */
9005b2fc499SJeff Garzik 	if (dev->interrupt) {
9016eecdc5fSDan Williams 		retval = usbnet_status_start(dev, GFP_KERNEL);
9025b2fc499SJeff Garzik 		if (retval < 0) {
903a475f603SJoe Perches 			netif_err(dev, ifup, dev->net,
904a475f603SJoe Perches 				  "intr submit %d\n", retval);
9055b2fc499SJeff Garzik 			goto done;
9065b2fc499SJeff Garzik 		}
9075b2fc499SJeff Garzik 	}
9085b2fc499SJeff Garzik 
90968972efaSPaul Stewart 	set_bit(EVENT_DEV_OPEN, &dev->flags);
9105b2fc499SJeff Garzik 	netif_start_queue (net);
911a475f603SJoe Perches 	netif_info(dev, ifup, dev->net,
912a475f603SJoe Perches 		   "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
91360b86755SJoe Perches 		   (int)RX_QLEN(dev), (int)TX_QLEN(dev),
914a475f603SJoe Perches 		   dev->net->mtu,
915a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
916a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
917a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
918a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
919a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
920a475f603SJoe Perches 		   "simple");
9215b2fc499SJeff Garzik 
92270c37bf9SBjørn Mork 	/* reset rx error state */
92370c37bf9SBjørn Mork 	dev->pkt_cnt = 0;
92470c37bf9SBjørn Mork 	dev->pkt_err = 0;
92570c37bf9SBjørn Mork 	clear_bit(EVENT_RX_KILL, &dev->flags);
92670c37bf9SBjørn Mork 
9275b2fc499SJeff Garzik 	// delay posting reads until we're fully open
9285b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
92969ee472fSOliver Neukum 	if (info->manage_power) {
93069ee472fSOliver Neukum 		retval = info->manage_power(dev, 1);
931a1c088e0SOliver Neukum 		if (retval < 0) {
932a1c088e0SOliver Neukum 			retval = 0;
933a1c088e0SOliver Neukum 			set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
934a1c088e0SOliver Neukum 		} else {
93569ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
93669ee472fSOliver Neukum 		}
937a1c088e0SOliver Neukum 	}
938a11a6544SOliver Neukum 	return retval;
9395b2fc499SJeff Garzik done:
940a11a6544SOliver Neukum 	usb_autopm_put_interface(dev->intf);
941a11a6544SOliver Neukum done_nopm:
9425b2fc499SJeff Garzik 	return retval;
9435b2fc499SJeff Garzik }
944777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open);
9455b2fc499SJeff Garzik 
9465b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9475b2fc499SJeff Garzik 
9485b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
9495b2fc499SJeff Garzik  * they'll probably want to use this base set.
9505b2fc499SJeff Garzik  */
9515b2fc499SJeff Garzik 
9528bae3551SPhilippe Reynes int usbnet_get_link_ksettings(struct net_device *net,
9538bae3551SPhilippe Reynes 			      struct ethtool_link_ksettings *cmd)
9548bae3551SPhilippe Reynes {
9558bae3551SPhilippe Reynes 	struct usbnet *dev = netdev_priv(net);
9568bae3551SPhilippe Reynes 
9578bae3551SPhilippe Reynes 	if (!dev->mii.mdio_read)
9588bae3551SPhilippe Reynes 		return -EOPNOTSUPP;
9598bae3551SPhilippe Reynes 
96082c01a84Syuval.shaia@oracle.com 	mii_ethtool_get_link_ksettings(&dev->mii, cmd);
96182c01a84Syuval.shaia@oracle.com 
96282c01a84Syuval.shaia@oracle.com 	return 0;
9638bae3551SPhilippe Reynes }
9648bae3551SPhilippe Reynes EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings);
9658bae3551SPhilippe Reynes 
9668bae3551SPhilippe Reynes int usbnet_set_link_ksettings(struct net_device *net,
9678bae3551SPhilippe Reynes 			      const struct ethtool_link_ksettings *cmd)
9688bae3551SPhilippe Reynes {
9698bae3551SPhilippe Reynes 	struct usbnet *dev = netdev_priv(net);
9708bae3551SPhilippe Reynes 	int retval;
9718bae3551SPhilippe Reynes 
9728bae3551SPhilippe Reynes 	if (!dev->mii.mdio_write)
9738bae3551SPhilippe Reynes 		return -EOPNOTSUPP;
9748bae3551SPhilippe Reynes 
9758bae3551SPhilippe Reynes 	retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
9768bae3551SPhilippe Reynes 
9778bae3551SPhilippe Reynes 	/* link speed/duplex might have changed */
9788bae3551SPhilippe Reynes 	if (dev->driver_info->link_reset)
9798bae3551SPhilippe Reynes 		dev->driver_info->link_reset(dev);
9808bae3551SPhilippe Reynes 
9818bae3551SPhilippe Reynes 	/* hard_mtu or rx_urb_size may change in link_reset() */
9828bae3551SPhilippe Reynes 	usbnet_update_max_qlen(dev);
9838bae3551SPhilippe Reynes 
9848bae3551SPhilippe Reynes 	return retval;
9858bae3551SPhilippe Reynes }
9868bae3551SPhilippe Reynes EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings);
9878bae3551SPhilippe Reynes 
988c8b5d129SGreg Ungerer void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
989c8b5d129SGreg Ungerer {
990c8b5d129SGreg Ungerer 	struct usbnet *dev = netdev_priv(net);
991c8b5d129SGreg Ungerer 	unsigned int start;
992c8b5d129SGreg Ungerer 	int cpu;
993c8b5d129SGreg Ungerer 
994c8b5d129SGreg Ungerer 	netdev_stats_to_stats64(stats, &net->stats);
995c8b5d129SGreg Ungerer 
996c8b5d129SGreg Ungerer 	for_each_possible_cpu(cpu) {
997c8b5d129SGreg Ungerer 		struct pcpu_sw_netstats *stats64;
998c8b5d129SGreg Ungerer 		u64 rx_packets, rx_bytes;
999c8b5d129SGreg Ungerer 		u64 tx_packets, tx_bytes;
1000c8b5d129SGreg Ungerer 
1001c8b5d129SGreg Ungerer 		stats64 = per_cpu_ptr(dev->stats64, cpu);
1002c8b5d129SGreg Ungerer 
1003c8b5d129SGreg Ungerer 		do {
1004c8b5d129SGreg Ungerer 			start = u64_stats_fetch_begin_irq(&stats64->syncp);
1005c8b5d129SGreg Ungerer 			rx_packets = stats64->rx_packets;
1006c8b5d129SGreg Ungerer 			rx_bytes = stats64->rx_bytes;
1007c8b5d129SGreg Ungerer 			tx_packets = stats64->tx_packets;
1008c8b5d129SGreg Ungerer 			tx_bytes = stats64->tx_bytes;
1009c8b5d129SGreg Ungerer 		} while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
1010c8b5d129SGreg Ungerer 
1011c8b5d129SGreg Ungerer 		stats->rx_packets += rx_packets;
1012c8b5d129SGreg Ungerer 		stats->rx_bytes += rx_bytes;
1013c8b5d129SGreg Ungerer 		stats->tx_packets += tx_packets;
1014c8b5d129SGreg Ungerer 		stats->tx_bytes += tx_bytes;
1015c8b5d129SGreg Ungerer 	}
1016c8b5d129SGreg Ungerer }
1017c8b5d129SGreg Ungerer EXPORT_SYMBOL_GPL(usbnet_get_stats64);
1018c8b5d129SGreg Ungerer 
10195b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
10205b2fc499SJeff Garzik {
10215b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10225b2fc499SJeff Garzik 
10235b2fc499SJeff Garzik 	/* If a check_connect is defined, return its result */
10245b2fc499SJeff Garzik 	if (dev->driver_info->check_connect)
10255b2fc499SJeff Garzik 		return dev->driver_info->check_connect (dev) == 0;
10265b2fc499SJeff Garzik 
10275b2fc499SJeff Garzik 	/* if the device has mii operations, use those */
10285b2fc499SJeff Garzik 	if (dev->mii.mdio_read)
10295b2fc499SJeff Garzik 		return mii_link_ok(&dev->mii);
10305b2fc499SJeff Garzik 
103105ffb3e2SBjørn Mork 	/* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
103205ffb3e2SBjørn Mork 	return ethtool_op_get_link(net);
10335b2fc499SJeff Garzik }
10345b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
10355b2fc499SJeff Garzik 
10365b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
10375b2fc499SJeff Garzik {
10385b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10395b2fc499SJeff Garzik 
10405b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
10415b2fc499SJeff Garzik 		return -EOPNOTSUPP;
10425b2fc499SJeff Garzik 
10435b2fc499SJeff Garzik 	return mii_nway_restart(&dev->mii);
10445b2fc499SJeff Garzik }
10455b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
10465b2fc499SJeff Garzik 
10475b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
10485b2fc499SJeff Garzik {
10495b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10505b2fc499SJeff Garzik 
105186a2f415SPhil Sutter 	strlcpy (info->driver, dev->driver_name, sizeof info->driver);
105286a2f415SPhil Sutter 	strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
105386a2f415SPhil Sutter 	strlcpy (info->fw_version, dev->driver_info->description,
10545b2fc499SJeff Garzik 		sizeof info->fw_version);
10555b2fc499SJeff Garzik 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
10565b2fc499SJeff Garzik }
10575b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
10585b2fc499SJeff Garzik 
10595b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
10605b2fc499SJeff Garzik {
10615b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10625b2fc499SJeff Garzik 
10635b2fc499SJeff Garzik 	return dev->msg_enable;
10645b2fc499SJeff Garzik }
10655b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
10665b2fc499SJeff Garzik 
10675b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
10685b2fc499SJeff Garzik {
10695b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
10705b2fc499SJeff Garzik 
10715b2fc499SJeff Garzik 	dev->msg_enable = level;
10725b2fc499SJeff Garzik }
10735b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
10745b2fc499SJeff Garzik 
10755b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
10760fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = {
10775b2fc499SJeff Garzik 	.get_link		= usbnet_get_link,
10785b2fc499SJeff Garzik 	.nway_reset		= usbnet_nway_reset,
10795b2fc499SJeff Garzik 	.get_drvinfo		= usbnet_get_drvinfo,
10805b2fc499SJeff Garzik 	.get_msglevel		= usbnet_get_msglevel,
10815b2fc499SJeff Garzik 	.set_msglevel		= usbnet_set_msglevel,
1082123edb18SRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
10838bae3551SPhilippe Reynes 	.get_link_ksettings	= usbnet_get_link_ksettings,
10848bae3551SPhilippe Reynes 	.set_link_ksettings	= usbnet_set_link_ksettings,
10855b2fc499SJeff Garzik };
10865b2fc499SJeff Garzik 
10875b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10885b2fc499SJeff Garzik 
10894b49f58fSMing Lei static void __handle_link_change(struct usbnet *dev)
10904b49f58fSMing Lei {
10914b49f58fSMing Lei 	if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
10924b49f58fSMing Lei 		return;
10934b49f58fSMing Lei 
10944b49f58fSMing Lei 	if (!netif_carrier_ok(dev->net)) {
10954b49f58fSMing Lei 		/* kill URBs for reading packets to save bus bandwidth */
10964b49f58fSMing Lei 		unlink_urbs(dev, &dev->rxq);
10974b49f58fSMing Lei 
10984b49f58fSMing Lei 		/*
10994b49f58fSMing Lei 		 * tx_timeout will unlink URBs for sending packets and
11004b49f58fSMing Lei 		 * tx queue is stopped by netcore after link becomes off
11014b49f58fSMing Lei 		 */
11024b49f58fSMing Lei 	} else {
11034b49f58fSMing Lei 		/* submitting URBs for reading packets */
11044b49f58fSMing Lei 		tasklet_schedule(&dev->bh);
11054b49f58fSMing Lei 	}
11064b49f58fSMing Lei 
1107a88c32aeSMing Lei 	/* hard_mtu or rx_urb_size may change during link change */
1108a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
1109a88c32aeSMing Lei 
11104b49f58fSMing Lei 	clear_bit(EVENT_LINK_CHANGE, &dev->flags);
11114b49f58fSMing Lei }
11124b49f58fSMing Lei 
11131efed2d0SOlivier Blin static void usbnet_set_rx_mode(struct net_device *net)
11141efed2d0SOlivier Blin {
11151efed2d0SOlivier Blin 	struct usbnet		*dev = netdev_priv(net);
11161efed2d0SOlivier Blin 
11171efed2d0SOlivier Blin 	usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
11181efed2d0SOlivier Blin }
11191efed2d0SOlivier Blin 
11201efed2d0SOlivier Blin static void __handle_set_rx_mode(struct usbnet *dev)
11211efed2d0SOlivier Blin {
11221efed2d0SOlivier Blin 	if (dev->driver_info->set_rx_mode)
11231efed2d0SOlivier Blin 		(dev->driver_info->set_rx_mode)(dev);
11241efed2d0SOlivier Blin 
11251efed2d0SOlivier Blin 	clear_bit(EVENT_SET_RX_MODE, &dev->flags);
11261efed2d0SOlivier Blin }
11271efed2d0SOlivier Blin 
11285b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
11295b2fc499SJeff Garzik  *
11305b2fc499SJeff Garzik  * NOTE:  with 2.5 we could do more of this using completion callbacks,
11315b2fc499SJeff Garzik  * especially now that control transfers can be queued.
11325b2fc499SJeff Garzik  */
11335b2fc499SJeff Garzik static void
113411f17ef3SOliver Neukum usbnet_deferred_kevent (struct work_struct *work)
11355b2fc499SJeff Garzik {
11365b2fc499SJeff Garzik 	struct usbnet		*dev =
11375b2fc499SJeff Garzik 		container_of(work, struct usbnet, kevent);
11385b2fc499SJeff Garzik 	int			status;
11395b2fc499SJeff Garzik 
11405b2fc499SJeff Garzik 	/* usb_clear_halt() needs a thread context */
11415b2fc499SJeff Garzik 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
11425b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->txq);
114369ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
114469ee472fSOliver Neukum 		if (status < 0)
114569ee472fSOliver Neukum 			goto fail_pipe;
11465b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->out);
114769ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
11488e95a202SJoe Perches 		if (status < 0 &&
11498e95a202SJoe Perches 		    status != -EPIPE &&
11508e95a202SJoe Perches 		    status != -ESHUTDOWN) {
11515b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
115269ee472fSOliver Neukum fail_pipe:
115360b86755SJoe Perches 				netdev_err(dev->net, "can't clear tx halt, status %d\n",
11545b2fc499SJeff Garzik 					   status);
11555b2fc499SJeff Garzik 		} else {
11565b2fc499SJeff Garzik 			clear_bit (EVENT_TX_HALT, &dev->flags);
11575b2fc499SJeff Garzik 			if (status != -ESHUTDOWN)
11585b2fc499SJeff Garzik 				netif_wake_queue (dev->net);
11595b2fc499SJeff Garzik 		}
11605b2fc499SJeff Garzik 	}
11615b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
11625b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->rxq);
116369ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
116469ee472fSOliver Neukum 		if (status < 0)
116569ee472fSOliver Neukum 			goto fail_halt;
11665b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->in);
116769ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
11688e95a202SJoe Perches 		if (status < 0 &&
11698e95a202SJoe Perches 		    status != -EPIPE &&
11708e95a202SJoe Perches 		    status != -ESHUTDOWN) {
11715b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
117269ee472fSOliver Neukum fail_halt:
117360b86755SJoe Perches 				netdev_err(dev->net, "can't clear rx halt, status %d\n",
11745b2fc499SJeff Garzik 					   status);
11755b2fc499SJeff Garzik 		} else {
11765b2fc499SJeff Garzik 			clear_bit (EVENT_RX_HALT, &dev->flags);
11775b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
11785b2fc499SJeff Garzik 		}
11795b2fc499SJeff Garzik 	}
11805b2fc499SJeff Garzik 
11815b2fc499SJeff Garzik 	/* tasklet could resubmit itself forever if memory is tight */
11825b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
11835b2fc499SJeff Garzik 		struct urb	*urb = NULL;
1184dacb3975SDavid S. Miller 		int resched = 1;
11855b2fc499SJeff Garzik 
11865b2fc499SJeff Garzik 		if (netif_running (dev->net))
11875b2fc499SJeff Garzik 			urb = usb_alloc_urb (0, GFP_KERNEL);
11885b2fc499SJeff Garzik 		else
11895b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
11905b2fc499SJeff Garzik 		if (urb != NULL) {
11915b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
119269ee472fSOliver Neukum 			status = usb_autopm_get_interface(dev->intf);
1193ab60707fSJesper Juhl 			if (status < 0) {
1194ab60707fSJesper Juhl 				usb_free_urb(urb);
119569ee472fSOliver Neukum 				goto fail_lowmem;
1196ab60707fSJesper Juhl 			}
1197dacb3975SDavid S. Miller 			if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1198dacb3975SDavid S. Miller 				resched = 0;
119969ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
120069ee472fSOliver Neukum fail_lowmem:
1201dacb3975SDavid S. Miller 			if (resched)
12025b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
12035b2fc499SJeff Garzik 		}
12045b2fc499SJeff Garzik 	}
12055b2fc499SJeff Garzik 
12065b2fc499SJeff Garzik 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
12075b2fc499SJeff Garzik 		struct driver_info	*info = dev->driver_info;
12085b2fc499SJeff Garzik 		int			retval = 0;
12095b2fc499SJeff Garzik 
12105b2fc499SJeff Garzik 		clear_bit (EVENT_LINK_RESET, &dev->flags);
121169ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
121269ee472fSOliver Neukum 		if (status < 0)
121369ee472fSOliver Neukum 			goto skip_reset;
12145b2fc499SJeff Garzik 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
121569ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
121669ee472fSOliver Neukum skip_reset:
121760b86755SJoe Perches 			netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
12185b2fc499SJeff Garzik 				    retval,
121960b86755SJoe Perches 				    dev->udev->bus->bus_name,
122060b86755SJoe Perches 				    dev->udev->devpath,
12215b2fc499SJeff Garzik 				    info->description);
122269ee472fSOliver Neukum 		} else {
122369ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
12245b2fc499SJeff Garzik 		}
12254b49f58fSMing Lei 
12264b49f58fSMing Lei 		/* handle link change from link resetting */
12274b49f58fSMing Lei 		__handle_link_change(dev);
12285b2fc499SJeff Garzik 	}
12295b2fc499SJeff Garzik 
12304b49f58fSMing Lei 	if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
12314b49f58fSMing Lei 		__handle_link_change(dev);
12324b49f58fSMing Lei 
12331efed2d0SOlivier Blin 	if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
12341efed2d0SOlivier Blin 		__handle_set_rx_mode(dev);
12351efed2d0SOlivier Blin 
12361efed2d0SOlivier Blin 
12375b2fc499SJeff Garzik 	if (dev->flags)
123860b86755SJoe Perches 		netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
12395b2fc499SJeff Garzik }
12405b2fc499SJeff Garzik 
12415b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12425b2fc499SJeff Garzik 
12435b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
12445b2fc499SJeff Garzik {
12455b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
12465b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
12475b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
12485b2fc499SJeff Garzik 
12495b2fc499SJeff Garzik 	if (urb->status == 0) {
1250c8b5d129SGreg Ungerer 		struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->stats64);
1251c8b5d129SGreg Ungerer 
1252c8b5d129SGreg Ungerer 		u64_stats_update_begin(&stats64->syncp);
1253c8b5d129SGreg Ungerer 		stats64->tx_packets += entry->packets;
1254c8b5d129SGreg Ungerer 		stats64->tx_bytes += entry->length;
1255c8b5d129SGreg Ungerer 		u64_stats_update_end(&stats64->syncp);
12565b2fc499SJeff Garzik 	} else {
12577963837fSHerbert Xu 		dev->net->stats.tx_errors++;
12585b2fc499SJeff Garzik 
12595b2fc499SJeff Garzik 		switch (urb->status) {
12605b2fc499SJeff Garzik 		case -EPIPE:
12615b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
12625b2fc499SJeff Garzik 			break;
12635b2fc499SJeff Garzik 
12645b2fc499SJeff Garzik 		/* software-driven interface shutdown */
12655b2fc499SJeff Garzik 		case -ECONNRESET:		// async unlink
12665b2fc499SJeff Garzik 		case -ESHUTDOWN:		// hardware gone
12675b2fc499SJeff Garzik 			break;
12685b2fc499SJeff Garzik 
126937ebb549SPetr Mladek 		/* like rx, tx gets controller i/o faults during hub_wq
127037ebb549SPetr Mladek 		 * delays and so it uses the same throttling mechanism.
127137ebb549SPetr Mladek 		 */
12725b2fc499SJeff Garzik 		case -EPROTO:
12735b2fc499SJeff Garzik 		case -ETIME:
12745b2fc499SJeff Garzik 		case -EILSEQ:
127569ee472fSOliver Neukum 			usb_mark_last_busy(dev->udev);
12765b2fc499SJeff Garzik 			if (!timer_pending (&dev->delay)) {
12775b2fc499SJeff Garzik 				mod_timer (&dev->delay,
12785b2fc499SJeff Garzik 					jiffies + THROTTLE_JIFFIES);
1279a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1280a475f603SJoe Perches 					  "tx throttle %d\n", urb->status);
12815b2fc499SJeff Garzik 			}
12825b2fc499SJeff Garzik 			netif_stop_queue (dev->net);
12835b2fc499SJeff Garzik 			break;
12845b2fc499SJeff Garzik 		default:
1285a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net,
1286a475f603SJoe Perches 				  "tx err %d\n", entry->urb->status);
12875b2fc499SJeff Garzik 			break;
12885b2fc499SJeff Garzik 		}
12895b2fc499SJeff Garzik 	}
12905b2fc499SJeff Garzik 
129169ee472fSOliver Neukum 	usb_autopm_put_interface_async(dev->intf);
12925b6e9bcdSMing Lei 	(void) defer_bh(dev, skb, &dev->txq, tx_done);
12935b2fc499SJeff Garzik }
12945b2fc499SJeff Garzik 
12955b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12965b2fc499SJeff Garzik 
1297777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net)
12985b2fc499SJeff Garzik {
12995b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
13005b2fc499SJeff Garzik 
13015b2fc499SJeff Garzik 	unlink_urbs (dev, &dev->txq);
13025b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
1303dbcdd4d5SOliver Neukum 	/* this needs to be handled individually because the generic layer
1304dbcdd4d5SOliver Neukum 	 * doesn't know what is sufficient and could not restore private
1305dbcdd4d5SOliver Neukum 	 * information if a remedy of an unconditional reset were used.
1306dbcdd4d5SOliver Neukum 	 */
1307dbcdd4d5SOliver Neukum 	if (dev->driver_info->recover)
1308dbcdd4d5SOliver Neukum 		(dev->driver_info->recover)(dev);
13095b2fc499SJeff Garzik }
1310777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
13115b2fc499SJeff Garzik 
13125b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
13135b2fc499SJeff Garzik 
1314638c5115SMing Lei static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1315638c5115SMing Lei {
1316638c5115SMing Lei 	unsigned num_sgs, total_len = 0;
1317638c5115SMing Lei 	int i, s = 0;
1318638c5115SMing Lei 
1319638c5115SMing Lei 	num_sgs = skb_shinfo(skb)->nr_frags + 1;
1320638c5115SMing Lei 	if (num_sgs == 1)
1321638c5115SMing Lei 		return 0;
1322638c5115SMing Lei 
132360e453a9SMing Lei 	/* reserve one for zero packet */
132460e453a9SMing Lei 	urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
132560e453a9SMing Lei 			  GFP_ATOMIC);
1326638c5115SMing Lei 	if (!urb->sg)
1327638c5115SMing Lei 		return -ENOMEM;
1328638c5115SMing Lei 
1329638c5115SMing Lei 	urb->num_sgs = num_sgs;
1330fdc3452cSBjørn Mork 	sg_init_table(urb->sg, urb->num_sgs + 1);
1331638c5115SMing Lei 
1332638c5115SMing Lei 	sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1333638c5115SMing Lei 	total_len += skb_headlen(skb);
1334638c5115SMing Lei 
1335638c5115SMing Lei 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1336638c5115SMing Lei 		struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1337638c5115SMing Lei 
1338638c5115SMing Lei 		total_len += skb_frag_size(f);
1339638c5115SMing Lei 		sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1340638c5115SMing Lei 				f->page_offset);
1341638c5115SMing Lei 	}
1342638c5115SMing Lei 	urb->transfer_buffer_length = total_len;
1343638c5115SMing Lei 
1344638c5115SMing Lei 	return 1;
1345638c5115SMing Lei }
1346638c5115SMing Lei 
134725a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
134825a79c41SStephen Hemminger 				     struct net_device *net)
13495b2fc499SJeff Garzik {
13505b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
13513e4336a6SJason A. Donenfeld 	unsigned int			length;
13525b2fc499SJeff Garzik 	struct urb		*urb = NULL;
13535b2fc499SJeff Garzik 	struct skb_data		*entry;
13545b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
13555b2fc499SJeff Garzik 	unsigned long		flags;
135625a79c41SStephen Hemminger 	int retval;
13575b2fc499SJeff Garzik 
135823ba0799SKonstantin Khlebnikov 	if (skb)
1359f9b491ecSMichael Riesch 		skb_tx_timestamp(skb);
1360f9b491ecSMichael Riesch 
13615b2fc499SJeff Garzik 	// some devices want funky USB-level framing, for
13625b2fc499SJeff Garzik 	// win32 driver (usually) and/or hardware quirks
13635b2fc499SJeff Garzik 	if (info->tx_fixup) {
13645b2fc499SJeff Garzik 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
13655b2fc499SJeff Garzik 		if (!skb) {
1366bf414b36SBjørn Mork 			/* packet collected; minidriver waiting for more */
1367bf414b36SBjørn Mork 			if (info->flags & FLAG_MULTI_PACKET)
1368bf414b36SBjørn Mork 				goto not_drop;
1369a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
13705b2fc499SJeff Garzik 			goto drop;
13715b2fc499SJeff Garzik 		}
13725b2fc499SJeff Garzik 	}
13735b2fc499SJeff Garzik 
13745b2fc499SJeff Garzik 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1375a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "no urb\n");
13765b2fc499SJeff Garzik 		goto drop;
13775b2fc499SJeff Garzik 	}
13785b2fc499SJeff Garzik 
13795b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
13805b2fc499SJeff Garzik 	entry->urb = urb;
13815b2fc499SJeff Garzik 	entry->dev = dev;
13825b2fc499SJeff Garzik 
13835b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
13845b2fc499SJeff Garzik 			skb->data, skb->len, tx_complete, skb);
1385638c5115SMing Lei 	if (dev->can_dma_sg) {
1386638c5115SMing Lei 		if (build_dma_sg(skb, urb) < 0)
1387638c5115SMing Lei 			goto drop;
1388638c5115SMing Lei 	}
138960e453a9SMing Lei 	length = urb->transfer_buffer_length;
13905b2fc499SJeff Garzik 
13915b2fc499SJeff Garzik 	/* don't assume the hardware handles USB_ZERO_PACKET
13925b2fc499SJeff Garzik 	 * NOTE:  strictly conforming cdc-ether devices should expect
13935b2fc499SJeff Garzik 	 * the ZLP here, but ignore the one-byte packet.
1394073285fdSAlexey Orishko 	 * NOTE2: CDC NCM specification is different from CDC ECM when
1395073285fdSAlexey Orishko 	 * handling ZLP/short packets, so cdc_ncm driver will make short
1396073285fdSAlexey Orishko 	 * packet itself if needed.
13975b2fc499SJeff Garzik 	 */
1398b4d562e3SElina Pasheva 	if (length % dev->maxpacket == 0) {
1399b4d562e3SElina Pasheva 		if (!(info->flags & FLAG_SEND_ZLP)) {
1400073285fdSAlexey Orishko 			if (!(info->flags & FLAG_MULTI_PACKET)) {
140160e453a9SMing Lei 				length++;
140260e453a9SMing Lei 				if (skb_tailroom(skb) && !urb->num_sgs) {
14033e323f3eSPeter Korsgaard 					skb->data[skb->len] = 0;
14043e323f3eSPeter Korsgaard 					__skb_put(skb, 1);
140560e453a9SMing Lei 				} else if (urb->num_sgs)
140660e453a9SMing Lei 					sg_set_buf(&urb->sg[urb->num_sgs++],
140760e453a9SMing Lei 							dev->padding_pkt, 1);
1408073285fdSAlexey Orishko 			}
1409b4d562e3SElina Pasheva 		} else
1410b4d562e3SElina Pasheva 			urb->transfer_flags |= URB_ZERO_PACKET;
14113e323f3eSPeter Korsgaard 	}
14127a1e890eSBen Hutchings 	urb->transfer_buffer_length = length;
14137a1e890eSBen Hutchings 
14147a1e890eSBen Hutchings 	if (info->flags & FLAG_MULTI_PACKET) {
14157a1e890eSBen Hutchings 		/* Driver has set number of packets and a length delta.
14167a1e890eSBen Hutchings 		 * Calculate the complete length and ensure that it's
14177a1e890eSBen Hutchings 		 * positive.
14187a1e890eSBen Hutchings 		 */
14197a1e890eSBen Hutchings 		entry->length += length;
14207a1e890eSBen Hutchings 		if (WARN_ON_ONCE(entry->length <= 0))
14217a1e890eSBen Hutchings 			entry->length = length;
14227a1e890eSBen Hutchings 	} else {
14237a1e890eSBen Hutchings 		usbnet_set_skb_tx_stats(skb, 1, length);
14247a1e890eSBen Hutchings 	}
14255b2fc499SJeff Garzik 
14265b2fc499SJeff Garzik 	spin_lock_irqsave(&dev->txq.lock, flags);
142769ee472fSOliver Neukum 	retval = usb_autopm_get_interface_async(dev->intf);
142869ee472fSOliver Neukum 	if (retval < 0) {
142969ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
143069ee472fSOliver Neukum 		goto drop;
143169ee472fSOliver Neukum 	}
143269ee472fSOliver Neukum 
143369ee472fSOliver Neukum #ifdef CONFIG_PM
143469ee472fSOliver Neukum 	/* if this triggers the device is still a sleep */
143569ee472fSOliver Neukum 	if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
143669ee472fSOliver Neukum 		/* transmission will be done in resume */
143769ee472fSOliver Neukum 		usb_anchor_urb(urb, &dev->deferred);
143869ee472fSOliver Neukum 		/* no use to process more packets */
143969ee472fSOliver Neukum 		netif_stop_queue(net);
144039707c2aSHemant Kumar 		usb_put_urb(urb);
144169ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
144260b86755SJoe Perches 		netdev_dbg(dev->net, "Delaying transmission for resumption\n");
144369ee472fSOliver Neukum 		goto deferred;
144469ee472fSOliver Neukum 	}
144569ee472fSOliver Neukum #endif
14465b2fc499SJeff Garzik 
14475b2fc499SJeff Garzik 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
14485b2fc499SJeff Garzik 	case -EPIPE:
14495b2fc499SJeff Garzik 		netif_stop_queue (net);
14505b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
145169ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
14525b2fc499SJeff Garzik 		break;
14535b2fc499SJeff Garzik 	default:
145469ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
1455a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net,
1456a475f603SJoe Perches 			  "tx: submit urb err %d\n", retval);
14575b2fc499SJeff Garzik 		break;
14585b2fc499SJeff Garzik 	case 0:
1459860e9538SFlorian Westphal 		netif_trans_update(net);
14605b6e9bcdSMing Lei 		__usbnet_queue_skb(&dev->txq, skb, tx_start);
14615b2fc499SJeff Garzik 		if (dev->txq.qlen >= TX_QLEN (dev))
14625b2fc499SJeff Garzik 			netif_stop_queue (net);
14635b2fc499SJeff Garzik 	}
14645b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->txq.lock, flags);
14655b2fc499SJeff Garzik 
14665b2fc499SJeff Garzik 	if (retval) {
1467a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
14685b2fc499SJeff Garzik drop:
14697963837fSHerbert Xu 		dev->net->stats.tx_dropped++;
1470073285fdSAlexey Orishko not_drop:
14715b2fc499SJeff Garzik 		if (skb)
14725b2fc499SJeff Garzik 			dev_kfree_skb_any (skb);
1473638c5115SMing Lei 		if (urb) {
1474638c5115SMing Lei 			kfree(urb->sg);
14755b2fc499SJeff Garzik 			usb_free_urb(urb);
1476638c5115SMing Lei 		}
1477a475f603SJoe Perches 	} else
1478a475f603SJoe Perches 		netif_dbg(dev, tx_queued, dev->net,
14793e4336a6SJason A. Donenfeld 			  "> tx, len %u, type 0x%x\n", length, skb->protocol);
148069ee472fSOliver Neukum #ifdef CONFIG_PM
148169ee472fSOliver Neukum deferred:
148269ee472fSOliver Neukum #endif
148325a79c41SStephen Hemminger 	return NETDEV_TX_OK;
14845b2fc499SJeff Garzik }
1485777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit);
14865b2fc499SJeff Garzik 
148785e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
148865841fd5SMing Lei {
148965841fd5SMing Lei 	struct urb	*urb;
149065841fd5SMing Lei 	int		i;
149185e87870SBjørn Mork 	int		ret = 0;
149265841fd5SMing Lei 
149365841fd5SMing Lei 	/* don't refill the queue all at once */
149465841fd5SMing Lei 	for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
149565841fd5SMing Lei 		urb = usb_alloc_urb(0, flags);
149665841fd5SMing Lei 		if (urb != NULL) {
149785e87870SBjørn Mork 			ret = rx_submit(dev, urb, flags);
149885e87870SBjørn Mork 			if (ret)
149985e87870SBjørn Mork 				goto err;
150085e87870SBjørn Mork 		} else {
150185e87870SBjørn Mork 			ret = -ENOMEM;
150285e87870SBjørn Mork 			goto err;
150365841fd5SMing Lei 		}
150465841fd5SMing Lei 	}
150585e87870SBjørn Mork err:
150685e87870SBjørn Mork 	return ret;
150765841fd5SMing Lei }
150865841fd5SMing Lei 
15095b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
15105b2fc499SJeff Garzik 
15115b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
15125b2fc499SJeff Garzik 
15132183c1a6SKees Cook static void usbnet_bh (struct timer_list *t)
15145b2fc499SJeff Garzik {
15152183c1a6SKees Cook 	struct usbnet		*dev = from_timer(dev, t, delay);
15165b2fc499SJeff Garzik 	struct sk_buff		*skb;
15175b2fc499SJeff Garzik 	struct skb_data		*entry;
15185b2fc499SJeff Garzik 
15195b2fc499SJeff Garzik 	while ((skb = skb_dequeue (&dev->done))) {
15205b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
15215b2fc499SJeff Garzik 		switch (entry->state) {
15225b2fc499SJeff Garzik 		case rx_done:
15235b2fc499SJeff Garzik 			entry->state = rx_cleanup;
15245b2fc499SJeff Garzik 			rx_process (dev, skb);
15255b2fc499SJeff Garzik 			continue;
15265b2fc499SJeff Garzik 		case tx_done:
1527638c5115SMing Lei 			kfree(entry->urb->sg);
15285b2fc499SJeff Garzik 		case rx_cleanup:
15295b2fc499SJeff Garzik 			usb_free_urb (entry->urb);
15305b2fc499SJeff Garzik 			dev_kfree_skb (skb);
15315b2fc499SJeff Garzik 			continue;
15325b2fc499SJeff Garzik 		default:
153360b86755SJoe Perches 			netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
15345b2fc499SJeff Garzik 		}
15355b2fc499SJeff Garzik 	}
15365b2fc499SJeff Garzik 
153770c37bf9SBjørn Mork 	/* restart RX again after disabling due to high error rate */
153870c37bf9SBjørn Mork 	clear_bit(EVENT_RX_KILL, &dev->flags);
153970c37bf9SBjørn Mork 
154014a0d635SOliver Neukum 	/* waiting for all pending urbs to complete?
154114a0d635SOliver Neukum 	 * only then can we forgo submitting anew
154214a0d635SOliver Neukum 	 */
154314a0d635SOliver Neukum 	if (waitqueue_active(&dev->wait)) {
154414a0d635SOliver Neukum 		if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
154514a0d635SOliver Neukum 			wake_up_all(&dev->wait);
15465b2fc499SJeff Garzik 
15475b2fc499SJeff Garzik 	// or are we maybe short a few urbs?
15488e95a202SJoe Perches 	} else if (netif_running (dev->net) &&
15498e95a202SJoe Perches 		   netif_device_present (dev->net) &&
15504b49f58fSMing Lei 		   netif_carrier_ok(dev->net) &&
15518e95a202SJoe Perches 		   !timer_pending(&dev->delay) &&
155243daa96bSSoohoon Lee 		   !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
15538e95a202SJoe Perches 		   !test_bit(EVENT_RX_HALT, &dev->flags)) {
15545b2fc499SJeff Garzik 		int	temp = dev->rxq.qlen;
15555b2fc499SJeff Garzik 
155665841fd5SMing Lei 		if (temp < RX_QLEN(dev)) {
155785e87870SBjørn Mork 			if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
155885e87870SBjørn Mork 				return;
1559a475f603SJoe Perches 			if (temp != dev->rxq.qlen)
1560a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1561a475f603SJoe Perches 					  "rxqlen %d --> %d\n",
15625b2fc499SJeff Garzik 					  temp, dev->rxq.qlen);
156365841fd5SMing Lei 			if (dev->rxq.qlen < RX_QLEN(dev))
15645b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
15655b2fc499SJeff Garzik 		}
15665b2fc499SJeff Garzik 		if (dev->txq.qlen < TX_QLEN (dev))
15675b2fc499SJeff Garzik 			netif_wake_queue (dev->net);
15685b2fc499SJeff Garzik 	}
15695b2fc499SJeff Garzik }
15705b2fc499SJeff Garzik 
15715b2fc499SJeff Garzik 
15725b2fc499SJeff Garzik /*-------------------------------------------------------------------------
15735b2fc499SJeff Garzik  *
15745b2fc499SJeff Garzik  * USB Device Driver support
15755b2fc499SJeff Garzik  *
15765b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
15775b2fc499SJeff Garzik 
15785b2fc499SJeff Garzik // precondition: never called in_interrupt
15795b2fc499SJeff Garzik 
15805b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
15815b2fc499SJeff Garzik {
15825b2fc499SJeff Garzik 	struct usbnet		*dev;
15835b2fc499SJeff Garzik 	struct usb_device	*xdev;
15845b2fc499SJeff Garzik 	struct net_device	*net;
15855b2fc499SJeff Garzik 
15865b2fc499SJeff Garzik 	dev = usb_get_intfdata(intf);
15875b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
15885b2fc499SJeff Garzik 	if (!dev)
15895b2fc499SJeff Garzik 		return;
15905b2fc499SJeff Garzik 
15915b2fc499SJeff Garzik 	xdev = interface_to_usbdev (intf);
15925b2fc499SJeff Garzik 
1593a475f603SJoe Perches 	netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
15945b2fc499SJeff Garzik 		   intf->dev.driver->name,
15955b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
15965b2fc499SJeff Garzik 		   dev->driver_info->description);
15975b2fc499SJeff Garzik 
15985b2fc499SJeff Garzik 	net = dev->net;
15995b2fc499SJeff Garzik 	unregister_netdev (net);
16005b2fc499SJeff Garzik 
160123f333a2STejun Heo 	cancel_work_sync(&dev->kevent);
16025b2fc499SJeff Garzik 
160339707c2aSHemant Kumar 	usb_scuttle_anchored_urbs(&dev->deferred);
160439707c2aSHemant Kumar 
16055b2fc499SJeff Garzik 	if (dev->driver_info->unbind)
16065b2fc499SJeff Garzik 		dev->driver_info->unbind (dev, intf);
16075b2fc499SJeff Garzik 
160868972efaSPaul Stewart 	usb_kill_urb(dev->interrupt);
160968972efaSPaul Stewart 	usb_free_urb(dev->interrupt);
161060e453a9SMing Lei 	kfree(dev->padding_pkt);
161168972efaSPaul Stewart 
1612c8b5d129SGreg Ungerer 	free_percpu(dev->stats64);
16135b2fc499SJeff Garzik 	free_netdev(net);
16145b2fc499SJeff Garzik }
16155b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
16165b2fc499SJeff Garzik 
1617777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = {
1618777baa47SStephen Hemminger 	.ndo_open		= usbnet_open,
1619777baa47SStephen Hemminger 	.ndo_stop		= usbnet_stop,
1620777baa47SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
1621777baa47SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
16221efed2d0SOlivier Blin 	.ndo_set_rx_mode	= usbnet_set_rx_mode,
1623777baa47SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1624c8b5d129SGreg Ungerer 	.ndo_get_stats64	= usbnet_get_stats64,
1625777baa47SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1626777baa47SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1627777baa47SStephen Hemminger };
16285b2fc499SJeff Garzik 
16295b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
16305b2fc499SJeff Garzik 
16315b2fc499SJeff Garzik // precondition: never called in_interrupt
16325b2fc499SJeff Garzik 
1633225794f8SMarcel Holtmann static struct device_type wlan_type = {
1634225794f8SMarcel Holtmann 	.name	= "wlan",
1635225794f8SMarcel Holtmann };
1636225794f8SMarcel Holtmann 
1637225794f8SMarcel Holtmann static struct device_type wwan_type = {
1638225794f8SMarcel Holtmann 	.name	= "wwan",
1639225794f8SMarcel Holtmann };
1640225794f8SMarcel Holtmann 
16415b2fc499SJeff Garzik int
16425b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
16435b2fc499SJeff Garzik {
16445b2fc499SJeff Garzik 	struct usbnet			*dev;
16455b2fc499SJeff Garzik 	struct net_device		*net;
16465b2fc499SJeff Garzik 	struct usb_host_interface	*interface;
16475b2fc499SJeff Garzik 	struct driver_info		*info;
16485b2fc499SJeff Garzik 	struct usb_device		*xdev;
16495b2fc499SJeff Garzik 	int				status;
16505b2fc499SJeff Garzik 	const char			*name;
1651b0786b43SMing Lei 	struct usb_driver 	*driver = to_usb_driver(udev->dev.driver);
1652b0786b43SMing Lei 
1653b0786b43SMing Lei 	/* usbnet already took usb runtime pm, so have to enable the feature
1654b0786b43SMing Lei 	 * for usb interface, otherwise usb_autopm_get_interface may return
165598f541c6SAlan Stern 	 * failure if RUNTIME_PM is enabled.
1656b0786b43SMing Lei 	 */
1657b0786b43SMing Lei 	if (!driver->supports_autosuspend) {
1658b0786b43SMing Lei 		driver->supports_autosuspend = 1;
1659b0786b43SMing Lei 		pm_runtime_enable(&udev->dev);
1660b0786b43SMing Lei 	}
16615b2fc499SJeff Garzik 
16625b2fc499SJeff Garzik 	name = udev->dev.driver->name;
16635b2fc499SJeff Garzik 	info = (struct driver_info *) prod->driver_info;
16645b2fc499SJeff Garzik 	if (!info) {
16655b2fc499SJeff Garzik 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
16665b2fc499SJeff Garzik 		return -ENODEV;
16675b2fc499SJeff Garzik 	}
16685b2fc499SJeff Garzik 	xdev = interface_to_usbdev (udev);
16695b2fc499SJeff Garzik 	interface = udev->cur_altsetting;
16705b2fc499SJeff Garzik 
16715b2fc499SJeff Garzik 	status = -ENOMEM;
16725b2fc499SJeff Garzik 
16735b2fc499SJeff Garzik 	// set up our own records
16745b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(*dev));
167541de8d4cSJoe Perches 	if (!net)
16765b2fc499SJeff Garzik 		goto out;
16775b2fc499SJeff Garzik 
16780dacca73SBen Hutchings 	/* netdev_printk() needs this so do it as early as possible */
16790dacca73SBen Hutchings 	SET_NETDEV_DEV(net, &udev->dev);
16800dacca73SBen Hutchings 
16815b2fc499SJeff Garzik 	dev = netdev_priv(net);
16825b2fc499SJeff Garzik 	dev->udev = xdev;
1683a11a6544SOliver Neukum 	dev->intf = udev;
16845b2fc499SJeff Garzik 	dev->driver_info = info;
16855b2fc499SJeff Garzik 	dev->driver_name = name;
1686c8b5d129SGreg Ungerer 
1687c8b5d129SGreg Ungerer 	dev->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1688c8b5d129SGreg Ungerer 	if (!dev->stats64)
1689c8b5d129SGreg Ungerer 		goto out0;
1690c8b5d129SGreg Ungerer 
16915b2fc499SJeff Garzik 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
16925b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
169314a0d635SOliver Neukum 	init_waitqueue_head(&dev->wait);
16945b2fc499SJeff Garzik 	skb_queue_head_init (&dev->rxq);
16955b2fc499SJeff Garzik 	skb_queue_head_init (&dev->txq);
16965b2fc499SJeff Garzik 	skb_queue_head_init (&dev->done);
16977834ddbcSJussi Kivilinna 	skb_queue_head_init(&dev->rxq_pause);
16982183c1a6SKees Cook 	dev->bh.func = (void (*)(unsigned long))usbnet_bh;
16992183c1a6SKees Cook 	dev->bh.data = (unsigned long)&dev->delay;
170011f17ef3SOliver Neukum 	INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
170169ee472fSOliver Neukum 	init_usb_anchor(&dev->deferred);
17022183c1a6SKees Cook 	timer_setup(&dev->delay, usbnet_bh, 0);
17035b2fc499SJeff Garzik 	mutex_init (&dev->phy_mutex);
17046eecdc5fSDan Williams 	mutex_init(&dev->interrupt_mutex);
17056eecdc5fSDan Williams 	dev->interrupt_count = 0;
17065b2fc499SJeff Garzik 
17075b2fc499SJeff Garzik 	dev->net = net;
17085b2fc499SJeff Garzik 	strcpy (net->name, "usb%d");
17095b2fc499SJeff Garzik 	memcpy (net->dev_addr, node_id, sizeof node_id);
17105b2fc499SJeff Garzik 
17115b2fc499SJeff Garzik 	/* rx and tx sides can use different message sizes;
17125b2fc499SJeff Garzik 	 * bind() should set rx_urb_size in that case.
17135b2fc499SJeff Garzik 	 */
17145b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
1715f77f0aeeSJarod Wilson 	net->min_mtu = 0;
1716f77f0aeeSJarod Wilson 	net->max_mtu = ETH_MAX_MTU;
17175b2fc499SJeff Garzik 
1718777baa47SStephen Hemminger 	net->netdev_ops = &usbnet_netdev_ops;
1719777baa47SStephen Hemminger 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
17205b2fc499SJeff Garzik 	net->ethtool_ops = &usbnet_ethtool_ops;
17215b2fc499SJeff Garzik 
17225b2fc499SJeff Garzik 	// allow device-specific bind/init procedures
17235b2fc499SJeff Garzik 	// NOTE net->name still not usable ...
17245b2fc499SJeff Garzik 	if (info->bind) {
17255b2fc499SJeff Garzik 		status = info->bind (dev, udev);
17265b2fc499SJeff Garzik 		if (status < 0)
17275b2fc499SJeff Garzik 			goto out1;
17285b2fc499SJeff Garzik 
17295b2fc499SJeff Garzik 		// heuristic:  "usb%d" for links we know are two-host,
17305b2fc499SJeff Garzik 		// else "eth%d" when there's reasonable doubt.  userspace
17315b2fc499SJeff Garzik 		// can rename the link if it knows better.
17328e95a202SJoe Perches 		if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1733c261344dSArnd Bergmann 		    ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1734c261344dSArnd Bergmann 		     (net->dev_addr [0] & 0x02) == 0))
17355b2fc499SJeff Garzik 			strcpy (net->name, "eth%d");
17366e3bbcc5SJussi Kivilinna 		/* WLAN devices should always be named "wlan%d" */
17376e3bbcc5SJussi Kivilinna 		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
17386e3bbcc5SJussi Kivilinna 			strcpy(net->name, "wlan%d");
1739e1e499eeSMarcel Holtmann 		/* WWAN devices should always be named "wwan%d" */
1740e1e499eeSMarcel Holtmann 		if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1741e1e499eeSMarcel Holtmann 			strcpy(net->name, "wwan%d");
17425b2fc499SJeff Garzik 
17436509141fSWei Shuai 		/* devices that cannot do ARP */
17446509141fSWei Shuai 		if ((dev->driver_info->flags & FLAG_NOARP) != 0)
17456509141fSWei Shuai 			net->flags |= IFF_NOARP;
17466509141fSWei Shuai 
17475b2fc499SJeff Garzik 		/* maybe the remote can't receive an Ethernet MTU */
17485b2fc499SJeff Garzik 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
17495b2fc499SJeff Garzik 			net->mtu = dev->hard_mtu - net->hard_header_len;
17505b2fc499SJeff Garzik 	} else if (!info->in || !info->out)
17515b2fc499SJeff Garzik 		status = usbnet_get_endpoints (dev, udev);
17525b2fc499SJeff Garzik 	else {
17535b2fc499SJeff Garzik 		dev->in = usb_rcvbulkpipe (xdev, info->in);
17545b2fc499SJeff Garzik 		dev->out = usb_sndbulkpipe (xdev, info->out);
17555b2fc499SJeff Garzik 		if (!(info->flags & FLAG_NO_SETINT))
17565b2fc499SJeff Garzik 			status = usb_set_interface (xdev,
17575b2fc499SJeff Garzik 				interface->desc.bInterfaceNumber,
17585b2fc499SJeff Garzik 				interface->desc.bAlternateSetting);
17595b2fc499SJeff Garzik 		else
17605b2fc499SJeff Garzik 			status = 0;
17615b2fc499SJeff Garzik 
17625b2fc499SJeff Garzik 	}
17639514bfe5SPeter Korsgaard 	if (status >= 0 && dev->status)
17645b2fc499SJeff Garzik 		status = init_status (dev, udev);
17655b2fc499SJeff Garzik 	if (status < 0)
17665b2fc499SJeff Garzik 		goto out3;
17675b2fc499SJeff Garzik 
17685b2fc499SJeff Garzik 	if (!dev->rx_urb_size)
17695b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
17705b2fc499SJeff Garzik 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
17715b2fc499SJeff Garzik 
1772eef23b53SBjørn Mork 	/* let userspace know we have a random address */
1773eef23b53SBjørn Mork 	if (ether_addr_equal(net->dev_addr, node_id))
1774eef23b53SBjørn Mork 		net->addr_assign_type = NET_ADDR_RANDOM;
1775eef23b53SBjørn Mork 
1776225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1777225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wlan_type);
1778225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1779225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wwan_type);
1780225794f8SMarcel Holtmann 
1781a88c32aeSMing Lei 	/* initialize max rx_qlen and tx_qlen */
1782a88c32aeSMing Lei 	usbnet_update_max_qlen(dev);
1783a88c32aeSMing Lei 
178460e453a9SMing Lei 	if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
178560e453a9SMing Lei 		!(info->flags & FLAG_MULTI_PACKET)) {
178660e453a9SMing Lei 		dev->padding_pkt = kzalloc(1, GFP_KERNEL);
178709b69024SWei Yongjun 		if (!dev->padding_pkt) {
178809b69024SWei Yongjun 			status = -ENOMEM;
178960e453a9SMing Lei 			goto out4;
179060e453a9SMing Lei 		}
179109b69024SWei Yongjun 	}
179260e453a9SMing Lei 
17935b2fc499SJeff Garzik 	status = register_netdev (net);
17945b2fc499SJeff Garzik 	if (status)
179560e453a9SMing Lei 		goto out5;
1796a475f603SJoe Perches 	netif_info(dev, probe, dev->net,
1797a475f603SJoe Perches 		   "register '%s' at usb-%s-%s, %s, %pM\n",
17985b2fc499SJeff Garzik 		   udev->dev.driver->name,
17995b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
18005b2fc499SJeff Garzik 		   dev->driver_info->description,
1801e174961cSJohannes Berg 		   net->dev_addr);
18025b2fc499SJeff Garzik 
18035b2fc499SJeff Garzik 	// ok, it's ready to go.
18045b2fc499SJeff Garzik 	usb_set_intfdata (udev, dev);
18055b2fc499SJeff Garzik 
18065b2fc499SJeff Garzik 	netif_device_attach (net);
18075b2fc499SJeff Garzik 
180837e8273cSBen Hutchings 	if (dev->driver_info->flags & FLAG_LINK_INTR)
18090162c554SMing Lei 		usbnet_link_change(dev, 0, 0);
181037e8273cSBen Hutchings 
18115b2fc499SJeff Garzik 	return 0;
18125b2fc499SJeff Garzik 
181360e453a9SMing Lei out5:
181460e453a9SMing Lei 	kfree(dev->padding_pkt);
1815a4723848Stom.leiming@gmail.com out4:
1816a4723848Stom.leiming@gmail.com 	usb_free_urb(dev->interrupt);
18175b2fc499SJeff Garzik out3:
18185b2fc499SJeff Garzik 	if (info->unbind)
18195b2fc499SJeff Garzik 		info->unbind (dev, udev);
18205b2fc499SJeff Garzik out1:
18211666984cSOliver Neukum 	/* subdrivers must undo all they did in bind() if they
18221666984cSOliver Neukum 	 * fail it, but we may fail later and a deferred kevent
18231666984cSOliver Neukum 	 * may trigger an error resubmitting itself and, worse,
18241666984cSOliver Neukum 	 * schedule a timer. So we kill it all just in case.
18251666984cSOliver Neukum 	 */
18261666984cSOliver Neukum 	cancel_work_sync(&dev->kevent);
18271666984cSOliver Neukum 	del_timer_sync(&dev->delay);
1828c8b5d129SGreg Ungerer 	free_percpu(dev->stats64);
1829c8b5d129SGreg Ungerer out0:
18305b2fc499SJeff Garzik 	free_netdev(net);
18315b2fc499SJeff Garzik out:
18325b2fc499SJeff Garzik 	return status;
18335b2fc499SJeff Garzik }
18345b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
18355b2fc499SJeff Garzik 
18365b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
18375b2fc499SJeff Garzik 
183836433127SOliver Neukum /*
183936433127SOliver Neukum  * suspend the whole driver as soon as the first interface is suspended
184036433127SOliver Neukum  * resume only when the last interface is resumed
18415b2fc499SJeff Garzik  */
18425b2fc499SJeff Garzik 
18435b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
18445b2fc499SJeff Garzik {
18455b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
18465b2fc499SJeff Garzik 
184736433127SOliver Neukum 	if (!dev->suspend_count++) {
184869ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
184969ee472fSOliver Neukum 		/* don't autosuspend while transmitting */
18505b1b0b81SAlan Stern 		if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
18515eeb3132SMing Lei 			dev->suspend_count--;
185269ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
185369ee472fSOliver Neukum 			return -EBUSY;
185469ee472fSOliver Neukum 		} else {
185569ee472fSOliver Neukum 			set_bit(EVENT_DEV_ASLEEP, &dev->flags);
185669ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
185769ee472fSOliver Neukum 		}
1858a11a6544SOliver Neukum 		/*
1859a11a6544SOliver Neukum 		 * accelerate emptying of the rx and queues, to avoid
18605b2fc499SJeff Garzik 		 * having everything error out.
18615b2fc499SJeff Garzik 		 */
18625b2fc499SJeff Garzik 		netif_device_detach (dev->net);
186369ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
18646eecdc5fSDan Williams 		__usbnet_status_stop_force(dev);
186569ee472fSOliver Neukum 
1866a11a6544SOliver Neukum 		/*
1867a11a6544SOliver Neukum 		 * reattach so runtime management can use and
1868a11a6544SOliver Neukum 		 * wake the device
1869a11a6544SOliver Neukum 		 */
1870a11a6544SOliver Neukum 		netif_device_attach (dev->net);
187136433127SOliver Neukum 	}
18725b2fc499SJeff Garzik 	return 0;
18735b2fc499SJeff Garzik }
18745b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
18755b2fc499SJeff Garzik 
18765b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
18775b2fc499SJeff Garzik {
18785b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
187969ee472fSOliver Neukum 	struct sk_buff          *skb;
188069ee472fSOliver Neukum 	struct urb              *res;
188169ee472fSOliver Neukum 	int                     retval;
18825b2fc499SJeff Garzik 
188369ee472fSOliver Neukum 	if (!--dev->suspend_count) {
18846eecdc5fSDan Williams 		/* resume interrupt URB if it was previously submitted */
18856eecdc5fSDan Williams 		__usbnet_status_start_force(dev, GFP_NOIO);
188668972efaSPaul Stewart 
188769ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
188869ee472fSOliver Neukum 		while ((res = usb_get_from_anchor(&dev->deferred))) {
188969ee472fSOliver Neukum 
189069ee472fSOliver Neukum 			skb = (struct sk_buff *)res->context;
189169ee472fSOliver Neukum 			retval = usb_submit_urb(res, GFP_ATOMIC);
189269ee472fSOliver Neukum 			if (retval < 0) {
189369ee472fSOliver Neukum 				dev_kfree_skb_any(skb);
1894638c5115SMing Lei 				kfree(res->sg);
189569ee472fSOliver Neukum 				usb_free_urb(res);
189669ee472fSOliver Neukum 				usb_autopm_put_interface_async(dev->intf);
189769ee472fSOliver Neukum 			} else {
1898860e9538SFlorian Westphal 				netif_trans_update(dev->net);
189969ee472fSOliver Neukum 				__skb_queue_tail(&dev->txq, skb);
190069ee472fSOliver Neukum 			}
190169ee472fSOliver Neukum 		}
190269ee472fSOliver Neukum 
190369ee472fSOliver Neukum 		smp_mb();
190469ee472fSOliver Neukum 		clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
190569ee472fSOliver Neukum 		spin_unlock_irq(&dev->txq.lock);
190675bd0cbdSMing Lei 
190775bd0cbdSMing Lei 		if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
190814a0d635SOliver Neukum 			/* handle remote wakeup ASAP
190914a0d635SOliver Neukum 			 * we cannot race against stop
191014a0d635SOliver Neukum 			 */
191114a0d635SOliver Neukum 			if (netif_device_present(dev->net) &&
191265841fd5SMing Lei 				!timer_pending(&dev->delay) &&
191365841fd5SMing Lei 				!test_bit(EVENT_RX_HALT, &dev->flags))
1914ab6f148dSOliver Neukum 					rx_alloc_submit(dev, GFP_NOIO);
191565841fd5SMing Lei 
191669ee472fSOliver Neukum 			if (!(dev->txq.qlen >= TX_QLEN(dev)))
19171aa9bc5bSAlexey Orishko 				netif_tx_wake_all_queues(dev->net);
19185b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
191969ee472fSOliver Neukum 		}
192075bd0cbdSMing Lei 	}
19215d9d01a3SOliver Neukum 
19225d9d01a3SOliver Neukum 	if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
19235d9d01a3SOliver Neukum 		usb_autopm_get_interface_no_resume(intf);
19245d9d01a3SOliver Neukum 
19255b2fc499SJeff Garzik 	return 0;
19265b2fc499SJeff Garzik }
19275b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
19285b2fc499SJeff Garzik 
19295d9d01a3SOliver Neukum /*
19305d9d01a3SOliver Neukum  * Either a subdriver implements manage_power, then it is assumed to always
19315d9d01a3SOliver Neukum  * be ready to be suspended or it reports the readiness to be suspended
19325d9d01a3SOliver Neukum  * explicitly
19335d9d01a3SOliver Neukum  */
19345d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev)
19355d9d01a3SOliver Neukum {
19365d9d01a3SOliver Neukum 	if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
19375d9d01a3SOliver Neukum 		dev->intf->needs_remote_wakeup = 1;
19385d9d01a3SOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
19395d9d01a3SOliver Neukum 	}
19405d9d01a3SOliver Neukum }
19415d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle);
19425b2fc499SJeff Garzik 
19432dd7c8cfSOliver Neukum /*
19442dd7c8cfSOliver Neukum  * For devices that can do without special commands
19452dd7c8cfSOliver Neukum  */
19462dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on)
19472dd7c8cfSOliver Neukum {
19482dd7c8cfSOliver Neukum 	dev->intf->needs_remote_wakeup = on;
19492dd7c8cfSOliver Neukum 	return 0;
19502dd7c8cfSOliver Neukum }
19512dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power);
19522dd7c8cfSOliver Neukum 
1953ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1954ac64995dSMing Lei {
1955ac64995dSMing Lei 	/* update link after link is reseted */
1956ac64995dSMing Lei 	if (link && !need_reset)
1957ac64995dSMing Lei 		netif_carrier_on(dev->net);
1958ac64995dSMing Lei 	else
1959ac64995dSMing Lei 		netif_carrier_off(dev->net);
1960ac64995dSMing Lei 
1961ac64995dSMing Lei 	if (need_reset && link)
1962ac64995dSMing Lei 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
19634b49f58fSMing Lei 	else
19644b49f58fSMing Lei 		usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1965ac64995dSMing Lei }
1966ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change);
1967ac64995dSMing Lei 
19685b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
19690547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1970877bd862SMing Lei 			     u16 value, u16 index, void *data, u16 size)
1971877bd862SMing Lei {
1972877bd862SMing Lei 	void *buf = NULL;
1973877bd862SMing Lei 	int err = -ENOMEM;
1974877bd862SMing Lei 
1975877bd862SMing Lei 	netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1976877bd862SMing Lei 		   " value=0x%04x index=0x%04x size=%d\n",
1977877bd862SMing Lei 		   cmd, reqtype, value, index, size);
1978877bd862SMing Lei 
19796c22fce0SOliver Neukum 	if (size) {
1980877bd862SMing Lei 		buf = kmalloc(size, GFP_KERNEL);
1981877bd862SMing Lei 		if (!buf)
1982877bd862SMing Lei 			goto out;
1983877bd862SMing Lei 	}
1984877bd862SMing Lei 
1985877bd862SMing Lei 	err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1986877bd862SMing Lei 			      cmd, reqtype, value, index, buf, size,
1987877bd862SMing Lei 			      USB_CTRL_GET_TIMEOUT);
19886c22fce0SOliver Neukum 	if (err > 0 && err <= size) {
19896c22fce0SOliver Neukum         if (data)
1990877bd862SMing Lei             memcpy(data, buf, err);
19916c22fce0SOliver Neukum         else
19926c22fce0SOliver Neukum             netdev_dbg(dev->net,
19936c22fce0SOliver Neukum                 "Huh? Data requested but thrown away.\n");
19946c22fce0SOliver Neukum     }
1995877bd862SMing Lei 	kfree(buf);
1996877bd862SMing Lei out:
1997877bd862SMing Lei 	return err;
1998877bd862SMing Lei }
1999877bd862SMing Lei 
20000547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20010547fad2SMing Lei 			      u16 value, u16 index, const void *data,
20020547fad2SMing Lei 			      u16 size)
2003877bd862SMing Lei {
2004877bd862SMing Lei 	void *buf = NULL;
2005877bd862SMing Lei 	int err = -ENOMEM;
2006877bd862SMing Lei 
2007877bd862SMing Lei 	netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2008877bd862SMing Lei 		   " value=0x%04x index=0x%04x size=%d\n",
2009877bd862SMing Lei 		   cmd, reqtype, value, index, size);
2010877bd862SMing Lei 
2011877bd862SMing Lei 	if (data) {
2012877bd862SMing Lei 		buf = kmemdup(data, size, GFP_KERNEL);
2013877bd862SMing Lei 		if (!buf)
2014877bd862SMing Lei 			goto out;
20156c22fce0SOliver Neukum 	} else {
20166c22fce0SOliver Neukum         if (size) {
20176c22fce0SOliver Neukum             WARN_ON_ONCE(1);
20186c22fce0SOliver Neukum             err = -EINVAL;
20196c22fce0SOliver Neukum             goto out;
20206c22fce0SOliver Neukum         }
2021877bd862SMing Lei     }
2022877bd862SMing Lei 
2023877bd862SMing Lei 	err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2024877bd862SMing Lei 			      cmd, reqtype, value, index, buf, size,
2025877bd862SMing Lei 			      USB_CTRL_SET_TIMEOUT);
2026877bd862SMing Lei 	kfree(buf);
2027877bd862SMing Lei 
2028877bd862SMing Lei out:
2029877bd862SMing Lei 	return err;
2030877bd862SMing Lei }
20310547fad2SMing Lei 
20320547fad2SMing Lei /*
20330547fad2SMing Lei  * The function can't be called inside suspend/resume callback,
20340547fad2SMing Lei  * otherwise deadlock will be caused.
20350547fad2SMing Lei  */
20360547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20370547fad2SMing Lei 		    u16 value, u16 index, void *data, u16 size)
20380547fad2SMing Lei {
20396f0a0986SMing Lei 	int ret;
20406f0a0986SMing Lei 
20416f0a0986SMing Lei 	if (usb_autopm_get_interface(dev->intf) < 0)
20426f0a0986SMing Lei 		return -ENODEV;
20436f0a0986SMing Lei 	ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
20440547fad2SMing Lei 				data, size);
20456f0a0986SMing Lei 	usb_autopm_put_interface(dev->intf);
20466f0a0986SMing Lei 	return ret;
20470547fad2SMing Lei }
20480547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd);
20490547fad2SMing Lei 
20500547fad2SMing Lei /*
20510547fad2SMing Lei  * The function can't be called inside suspend/resume callback,
20520547fad2SMing Lei  * otherwise deadlock will be caused.
20530547fad2SMing Lei  */
20540547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20550547fad2SMing Lei 		     u16 value, u16 index, const void *data, u16 size)
20560547fad2SMing Lei {
20576f0a0986SMing Lei 	int ret;
20586f0a0986SMing Lei 
20596f0a0986SMing Lei 	if (usb_autopm_get_interface(dev->intf) < 0)
20606f0a0986SMing Lei 		return -ENODEV;
20616f0a0986SMing Lei 	ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
20620547fad2SMing Lei 				 data, size);
20636f0a0986SMing Lei 	usb_autopm_put_interface(dev->intf);
20646f0a0986SMing Lei 	return ret;
20650547fad2SMing Lei }
2066877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2067877bd862SMing Lei 
20680547fad2SMing Lei /*
20690547fad2SMing Lei  * The function can be called inside suspend/resume callback safely
20700547fad2SMing Lei  * and should only be called by suspend/resume callback generally.
20710547fad2SMing Lei  */
20720547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
20730547fad2SMing Lei 			  u16 value, u16 index, void *data, u16 size)
20740547fad2SMing Lei {
20750547fad2SMing Lei 	return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
20760547fad2SMing Lei 				 data, size);
20770547fad2SMing Lei }
20780547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
20790547fad2SMing Lei 
20800547fad2SMing Lei /*
20810547fad2SMing Lei  * The function can be called inside suspend/resume callback safely
20820547fad2SMing Lei  * and should only be called by suspend/resume callback generally.
20830547fad2SMing Lei  */
20840547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
20850547fad2SMing Lei 			  u16 value, u16 index, const void *data,
20860547fad2SMing Lei 			  u16 size)
20870547fad2SMing Lei {
20880547fad2SMing Lei 	return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
20890547fad2SMing Lei 				  data, size);
20900547fad2SMing Lei }
20910547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
20920547fad2SMing Lei 
2093877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb)
2094877bd862SMing Lei {
2095877bd862SMing Lei 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2096877bd862SMing Lei 	int status = urb->status;
2097877bd862SMing Lei 
2098877bd862SMing Lei 	if (status < 0)
2099877bd862SMing Lei 		dev_dbg(&urb->dev->dev, "%s failed with %d",
2100877bd862SMing Lei 			__func__, status);
2101877bd862SMing Lei 
2102877bd862SMing Lei 	kfree(req);
2103877bd862SMing Lei 	usb_free_urb(urb);
2104877bd862SMing Lei }
2105877bd862SMing Lei 
21060547fad2SMing Lei /*
21070547fad2SMing Lei  * The caller must make sure that device can't be put into suspend
21080547fad2SMing Lei  * state until the control URB completes.
21090547fad2SMing Lei  */
2110877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2111877bd862SMing Lei 			   u16 value, u16 index, const void *data, u16 size)
2112877bd862SMing Lei {
2113877bd862SMing Lei 	struct usb_ctrlrequest *req = NULL;
2114877bd862SMing Lei 	struct urb *urb;
2115877bd862SMing Lei 	int err = -ENOMEM;
2116877bd862SMing Lei 	void *buf = NULL;
2117877bd862SMing Lei 
2118877bd862SMing Lei 	netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2119877bd862SMing Lei 		   " value=0x%04x index=0x%04x size=%d\n",
2120877bd862SMing Lei 		   cmd, reqtype, value, index, size);
2121877bd862SMing Lei 
2122877bd862SMing Lei 	urb = usb_alloc_urb(0, GFP_ATOMIC);
2123ef6cd130SWolfram Sang 	if (!urb)
2124877bd862SMing Lei 		goto fail;
2125877bd862SMing Lei 
2126877bd862SMing Lei 	if (data) {
2127877bd862SMing Lei 		buf = kmemdup(data, size, GFP_ATOMIC);
2128877bd862SMing Lei 		if (!buf) {
2129877bd862SMing Lei 			netdev_err(dev->net, "Error allocating buffer"
2130877bd862SMing Lei 				   " in %s!\n", __func__);
2131877bd862SMing Lei 			goto fail_free;
2132877bd862SMing Lei 		}
2133877bd862SMing Lei 	}
2134877bd862SMing Lei 
2135877bd862SMing Lei 	req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
213638673c82SJoe Perches 	if (!req)
2137877bd862SMing Lei 		goto fail_free_buf;
2138877bd862SMing Lei 
2139877bd862SMing Lei 	req->bRequestType = reqtype;
2140877bd862SMing Lei 	req->bRequest = cmd;
2141877bd862SMing Lei 	req->wValue = cpu_to_le16(value);
2142877bd862SMing Lei 	req->wIndex = cpu_to_le16(index);
2143877bd862SMing Lei 	req->wLength = cpu_to_le16(size);
2144877bd862SMing Lei 
2145877bd862SMing Lei 	usb_fill_control_urb(urb, dev->udev,
2146877bd862SMing Lei 			     usb_sndctrlpipe(dev->udev, 0),
2147877bd862SMing Lei 			     (void *)req, buf, size,
2148877bd862SMing Lei 			     usbnet_async_cmd_cb, req);
2149877bd862SMing Lei 	urb->transfer_flags |= URB_FREE_BUFFER;
2150877bd862SMing Lei 
2151877bd862SMing Lei 	err = usb_submit_urb(urb, GFP_ATOMIC);
2152877bd862SMing Lei 	if (err < 0) {
2153877bd862SMing Lei 		netdev_err(dev->net, "Error submitting the control"
2154877bd862SMing Lei 			   " message: status=%d\n", err);
2155877bd862SMing Lei 		goto fail_free;
2156877bd862SMing Lei 	}
2157877bd862SMing Lei 	return 0;
2158877bd862SMing Lei 
2159877bd862SMing Lei fail_free_buf:
2160877bd862SMing Lei 	kfree(buf);
2161877bd862SMing Lei fail_free:
2162877bd862SMing Lei 	kfree(req);
2163877bd862SMing Lei 	usb_free_urb(urb);
2164877bd862SMing Lei fail:
2165877bd862SMing Lei 	return err;
2166877bd862SMing Lei 
2167877bd862SMing Lei }
2168877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2169877bd862SMing Lei /*-------------------------------------------------------------------------*/
21705b2fc499SJeff Garzik 
21715b2fc499SJeff Garzik static int __init usbnet_init(void)
21725b2fc499SJeff Garzik {
2173c582a950SThiago Farina 	/* Compiler should optimize this out. */
2174c582a950SThiago Farina 	BUILD_BUG_ON(
2175c582a950SThiago Farina 		FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
21765b2fc499SJeff Garzik 
2177c7e12eadSJoe Perches 	eth_random_addr(node_id);
21785b2fc499SJeff Garzik 	return 0;
21795b2fc499SJeff Garzik }
21805b2fc499SJeff Garzik module_init(usbnet_init);
21815b2fc499SJeff Garzik 
21825b2fc499SJeff Garzik static void __exit usbnet_exit(void)
21835b2fc499SJeff Garzik {
21845b2fc499SJeff Garzik }
21855b2fc499SJeff Garzik module_exit(usbnet_exit);
21865b2fc499SJeff Garzik 
21875b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
21885b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
21895b2fc499SJeff Garzik MODULE_LICENSE("GPL");
2190