xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision 5eeb3132eb4c5e9ca92e5247fe4575fe9f8c3efe)
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
175b2fc499SJeff Garzik  * along with this program; if not, write to the Free Software
185b2fc499SJeff Garzik  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
195b2fc499SJeff Garzik  */
205b2fc499SJeff Garzik 
215b2fc499SJeff Garzik /*
225b2fc499SJeff Garzik  * This is a generic "USB networking" framework that works with several
235b2fc499SJeff Garzik  * kinds of full and high speed networking devices:  host-to-host cables,
245b2fc499SJeff Garzik  * smart usb peripherals, and actual Ethernet adapters.
255b2fc499SJeff Garzik  *
265b2fc499SJeff Garzik  * These devices usually differ in terms of control protocols (if they
275b2fc499SJeff Garzik  * even have one!) and sometimes they define new framing to wrap or batch
285b2fc499SJeff Garzik  * Ethernet packets.  Otherwise, they talk to USB pretty much the same,
295b2fc499SJeff Garzik  * so interface (un)binding, endpoint I/O queues, fault handling, and other
305b2fc499SJeff Garzik  * issues can usefully be addressed by this framework.
315b2fc499SJeff Garzik  */
325b2fc499SJeff Garzik 
335b2fc499SJeff Garzik // #define	DEBUG			// error path messages, extra info
345b2fc499SJeff Garzik // #define	VERBOSE			// more; success messages
355b2fc499SJeff Garzik 
365b2fc499SJeff Garzik #include <linux/module.h>
375b2fc499SJeff Garzik #include <linux/init.h>
385b2fc499SJeff Garzik #include <linux/netdevice.h>
395b2fc499SJeff Garzik #include <linux/etherdevice.h>
4003ad032bSPeter Holik #include <linux/ctype.h>
415b2fc499SJeff Garzik #include <linux/ethtool.h>
425b2fc499SJeff Garzik #include <linux/workqueue.h>
435b2fc499SJeff Garzik #include <linux/mii.h>
445b2fc499SJeff Garzik #include <linux/usb.h>
453692e94fSJussi Kivilinna #include <linux/usb/usbnet.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47fd1f170dSAndy Shevchenko #include <linux/kernel.h>
48b0786b43SMing Lei #include <linux/pm_runtime.h>
495b2fc499SJeff Garzik 
505b2fc499SJeff Garzik #define DRIVER_VERSION		"22-Aug-2005"
515b2fc499SJeff Garzik 
525b2fc499SJeff Garzik 
535b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
545b2fc499SJeff Garzik 
555b2fc499SJeff Garzik /*
565b2fc499SJeff Garzik  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
575b2fc499SJeff Garzik  * Several dozen bytes of IPv4 data can fit in two such transactions.
585b2fc499SJeff Garzik  * One maximum size Ethernet packet takes twenty four of them.
595b2fc499SJeff Garzik  * For high speed, each frame comfortably fits almost 36 max size
605b2fc499SJeff Garzik  * Ethernet packets (so queues should be bigger).
615b2fc499SJeff Garzik  *
625b2fc499SJeff Garzik  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
635b2fc499SJeff Garzik  * let the USB host controller be busy for 5msec or more before an irq
645b2fc499SJeff Garzik  * is required, under load.  Jumbograms change the equation.
655b2fc499SJeff Garzik  */
665b2fc499SJeff Garzik #define RX_MAX_QUEUE_MEMORY (60 * 1518)
675b2fc499SJeff Garzik #define	RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
685b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
695b2fc499SJeff Garzik #define	TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
705b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
715b2fc499SJeff Garzik 
725b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
735b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES	(5*HZ)
745b2fc499SJeff Garzik 
755b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect()
765b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors.
775b2fc499SJeff Garzik #define THROTTLE_JIFFIES	(HZ/8)
785b2fc499SJeff Garzik 
795b2fc499SJeff Garzik // between wakeups
805b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS	3
815b2fc499SJeff Garzik 
825b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
835b2fc499SJeff Garzik 
845b2fc499SJeff Garzik // randomly generated ethernet address
855b2fc499SJeff Garzik static u8	node_id [ETH_ALEN];
865b2fc499SJeff Garzik 
875b2fc499SJeff Garzik static const char driver_name [] = "usbnet";
885b2fc499SJeff Garzik 
895b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
905b2fc499SJeff Garzik static int msg_level = -1;
915b2fc499SJeff Garzik module_param (msg_level, int, 0);
925b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
935b2fc499SJeff Garzik 
945b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
955b2fc499SJeff Garzik 
965b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
975b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
985b2fc499SJeff Garzik {
995b2fc499SJeff Garzik 	int				tmp;
1005b2fc499SJeff Garzik 	struct usb_host_interface	*alt = NULL;
1015b2fc499SJeff Garzik 	struct usb_host_endpoint	*in = NULL, *out = NULL;
1025b2fc499SJeff Garzik 	struct usb_host_endpoint	*status = NULL;
1035b2fc499SJeff Garzik 
1045b2fc499SJeff Garzik 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
1055b2fc499SJeff Garzik 		unsigned	ep;
1065b2fc499SJeff Garzik 
1075b2fc499SJeff Garzik 		in = out = status = NULL;
1085b2fc499SJeff Garzik 		alt = intf->altsetting + tmp;
1095b2fc499SJeff Garzik 
1105b2fc499SJeff Garzik 		/* take the first altsetting with in-bulk + out-bulk;
1115b2fc499SJeff Garzik 		 * remember any status endpoint, just in case;
11270f23fd6SJustin P. Mattock 		 * ignore other endpoints and altsettings.
1135b2fc499SJeff Garzik 		 */
1145b2fc499SJeff Garzik 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
1155b2fc499SJeff Garzik 			struct usb_host_endpoint	*e;
1165b2fc499SJeff Garzik 			int				intr = 0;
1175b2fc499SJeff Garzik 
1185b2fc499SJeff Garzik 			e = alt->endpoint + ep;
1195b2fc499SJeff Garzik 			switch (e->desc.bmAttributes) {
1205b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_INT:
1215b2fc499SJeff Garzik 				if (!usb_endpoint_dir_in(&e->desc))
1225b2fc499SJeff Garzik 					continue;
1235b2fc499SJeff Garzik 				intr = 1;
1245b2fc499SJeff Garzik 				/* FALLTHROUGH */
1255b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_BULK:
1265b2fc499SJeff Garzik 				break;
1275b2fc499SJeff Garzik 			default:
1285b2fc499SJeff Garzik 				continue;
1295b2fc499SJeff Garzik 			}
1305b2fc499SJeff Garzik 			if (usb_endpoint_dir_in(&e->desc)) {
1315b2fc499SJeff Garzik 				if (!intr && !in)
1325b2fc499SJeff Garzik 					in = e;
1335b2fc499SJeff Garzik 				else if (intr && !status)
1345b2fc499SJeff Garzik 					status = e;
1355b2fc499SJeff Garzik 			} else {
1365b2fc499SJeff Garzik 				if (!out)
1375b2fc499SJeff Garzik 					out = e;
1385b2fc499SJeff Garzik 			}
1395b2fc499SJeff Garzik 		}
1405b2fc499SJeff Garzik 		if (in && out)
1415b2fc499SJeff Garzik 			break;
1425b2fc499SJeff Garzik 	}
1435b2fc499SJeff Garzik 	if (!alt || !in || !out)
1445b2fc499SJeff Garzik 		return -EINVAL;
1455b2fc499SJeff Garzik 
1468e95a202SJoe Perches 	if (alt->desc.bAlternateSetting != 0 ||
1478e95a202SJoe Perches 	    !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1485b2fc499SJeff Garzik 		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
1495b2fc499SJeff Garzik 				alt->desc.bAlternateSetting);
1505b2fc499SJeff Garzik 		if (tmp < 0)
1515b2fc499SJeff Garzik 			return tmp;
1525b2fc499SJeff Garzik 	}
1535b2fc499SJeff Garzik 
1545b2fc499SJeff Garzik 	dev->in = usb_rcvbulkpipe (dev->udev,
1555b2fc499SJeff Garzik 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1565b2fc499SJeff Garzik 	dev->out = usb_sndbulkpipe (dev->udev,
1575b2fc499SJeff Garzik 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1585b2fc499SJeff Garzik 	dev->status = status;
1595b2fc499SJeff Garzik 	return 0;
1605b2fc499SJeff Garzik }
1615b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1625b2fc499SJeff Garzik 
16303ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
16403ad032bSPeter Holik {
16503ad032bSPeter Holik 	int 		tmp, i;
16603ad032bSPeter Holik 	unsigned char	buf [13];
16703ad032bSPeter Holik 
16803ad032bSPeter Holik 	tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
16903ad032bSPeter Holik 	if (tmp != 12) {
17003ad032bSPeter Holik 		dev_dbg(&dev->udev->dev,
17103ad032bSPeter Holik 			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
17203ad032bSPeter Holik 		if (tmp >= 0)
17303ad032bSPeter Holik 			tmp = -EINVAL;
17403ad032bSPeter Holik 		return tmp;
17503ad032bSPeter Holik 	}
17603ad032bSPeter Holik 	for (i = tmp = 0; i < 6; i++, tmp += 2)
17703ad032bSPeter Holik 		dev->net->dev_addr [i] =
178fd1f170dSAndy Shevchenko 			(hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]);
17903ad032bSPeter Holik 	return 0;
18003ad032bSPeter Holik }
18103ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
18203ad032bSPeter Holik 
1835b2fc499SJeff Garzik static void intr_complete (struct urb *urb);
1845b2fc499SJeff Garzik 
1855b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
1865b2fc499SJeff Garzik {
1875b2fc499SJeff Garzik 	char		*buf = NULL;
1885b2fc499SJeff Garzik 	unsigned	pipe = 0;
1895b2fc499SJeff Garzik 	unsigned	maxp;
1905b2fc499SJeff Garzik 	unsigned	period;
1915b2fc499SJeff Garzik 
1925b2fc499SJeff Garzik 	if (!dev->driver_info->status)
1935b2fc499SJeff Garzik 		return 0;
1945b2fc499SJeff Garzik 
1955b2fc499SJeff Garzik 	pipe = usb_rcvintpipe (dev->udev,
1965b2fc499SJeff Garzik 			dev->status->desc.bEndpointAddress
1975b2fc499SJeff Garzik 				& USB_ENDPOINT_NUMBER_MASK);
1985b2fc499SJeff Garzik 	maxp = usb_maxpacket (dev->udev, pipe, 0);
1995b2fc499SJeff Garzik 
2005b2fc499SJeff Garzik 	/* avoid 1 msec chatter:  min 8 msec poll rate */
2015b2fc499SJeff Garzik 	period = max ((int) dev->status->desc.bInterval,
2025b2fc499SJeff Garzik 		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
2035b2fc499SJeff Garzik 
2045b2fc499SJeff Garzik 	buf = kmalloc (maxp, GFP_KERNEL);
2055b2fc499SJeff Garzik 	if (buf) {
2065b2fc499SJeff Garzik 		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
2075b2fc499SJeff Garzik 		if (!dev->interrupt) {
2085b2fc499SJeff Garzik 			kfree (buf);
2095b2fc499SJeff Garzik 			return -ENOMEM;
2105b2fc499SJeff Garzik 		} else {
2115b2fc499SJeff Garzik 			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
2125b2fc499SJeff Garzik 				buf, maxp, intr_complete, dev, period);
213720f3d7cStom.leiming@gmail.com 			dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
2145b2fc499SJeff Garzik 			dev_dbg(&intf->dev,
2155b2fc499SJeff Garzik 				"status ep%din, %d bytes period %d\n",
2165b2fc499SJeff Garzik 				usb_pipeendpoint(pipe), maxp, period);
2175b2fc499SJeff Garzik 		}
2185b2fc499SJeff Garzik 	}
2195b2fc499SJeff Garzik 	return 0;
2205b2fc499SJeff Garzik }
2215b2fc499SJeff Garzik 
2225b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
2235b2fc499SJeff Garzik  * Some link protocols batch packets, so their rx_fixup paths
2245b2fc499SJeff Garzik  * can return clones as well as just modify the original skb.
2255b2fc499SJeff Garzik  */
2265b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
2275b2fc499SJeff Garzik {
2285b2fc499SJeff Garzik 	int	status;
2295b2fc499SJeff Garzik 
2307834ddbcSJussi Kivilinna 	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
2317834ddbcSJussi Kivilinna 		skb_queue_tail(&dev->rxq_pause, skb);
2327834ddbcSJussi Kivilinna 		return;
2337834ddbcSJussi Kivilinna 	}
2347834ddbcSJussi Kivilinna 
2355b2fc499SJeff Garzik 	skb->protocol = eth_type_trans (skb, dev->net);
2367963837fSHerbert Xu 	dev->net->stats.rx_packets++;
2377963837fSHerbert Xu 	dev->net->stats.rx_bytes += skb->len;
2385b2fc499SJeff Garzik 
239a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
2405b2fc499SJeff Garzik 		  skb->len + sizeof (struct ethhdr), skb->protocol);
2415b2fc499SJeff Garzik 	memset (skb->cb, 0, sizeof (struct skb_data));
242f9b491ecSMichael Riesch 
243f9b491ecSMichael Riesch 	if (skb_defer_rx_timestamp(skb))
244f9b491ecSMichael Riesch 		return;
245f9b491ecSMichael Riesch 
2465b2fc499SJeff Garzik 	status = netif_rx (skb);
247a475f603SJoe Perches 	if (status != NET_RX_SUCCESS)
248a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net,
249a475f603SJoe Perches 			  "netif_rx status %d\n", status);
2505b2fc499SJeff Garzik }
2515b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
2525b2fc499SJeff Garzik 
2535b2fc499SJeff Garzik 
2545b2fc499SJeff Garzik /*-------------------------------------------------------------------------
2555b2fc499SJeff Garzik  *
2565b2fc499SJeff Garzik  * Network Device Driver (peer link to "Host Device", from USB host)
2575b2fc499SJeff Garzik  *
2585b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
2595b2fc499SJeff Garzik 
260777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu)
2615b2fc499SJeff Garzik {
2625b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
2635b2fc499SJeff Garzik 	int		ll_mtu = new_mtu + net->hard_header_len;
2645b2fc499SJeff Garzik 	int		old_hard_mtu = dev->hard_mtu;
2655b2fc499SJeff Garzik 	int		old_rx_urb_size = dev->rx_urb_size;
2665b2fc499SJeff Garzik 
2675b2fc499SJeff Garzik 	if (new_mtu <= 0)
2685b2fc499SJeff Garzik 		return -EINVAL;
2695b2fc499SJeff Garzik 	// no second zero-length packet read wanted after mtu-sized packets
2705b2fc499SJeff Garzik 	if ((ll_mtu % dev->maxpacket) == 0)
2715b2fc499SJeff Garzik 		return -EDOM;
2725b2fc499SJeff Garzik 	net->mtu = new_mtu;
2735b2fc499SJeff Garzik 
2745b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
2755b2fc499SJeff Garzik 	if (dev->rx_urb_size == old_hard_mtu) {
2765b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
2775b2fc499SJeff Garzik 		if (dev->rx_urb_size > old_rx_urb_size)
2785b2fc499SJeff Garzik 			usbnet_unlink_rx_urbs(dev);
2795b2fc499SJeff Garzik 	}
2805b2fc499SJeff Garzik 
2815b2fc499SJeff Garzik 	return 0;
2825b2fc499SJeff Garzik }
283777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu);
2845b2fc499SJeff Garzik 
2855b6e9bcdSMing Lei /* The caller must hold list->lock */
2865b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list,
2875b6e9bcdSMing Lei 			struct sk_buff *newsk, enum skb_state state)
2885b6e9bcdSMing Lei {
2895b6e9bcdSMing Lei 	struct skb_data *entry = (struct skb_data *) newsk->cb;
2905b6e9bcdSMing Lei 
2915b6e9bcdSMing Lei 	__skb_queue_tail(list, newsk);
2925b6e9bcdSMing Lei 	entry->state = state;
2935b6e9bcdSMing Lei }
2945b6e9bcdSMing Lei 
2955b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
2965b2fc499SJeff Garzik 
2975b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
2985b2fc499SJeff Garzik  * completion callbacks.  2.5 should have fixed those bugs...
2995b2fc499SJeff Garzik  */
3005b2fc499SJeff Garzik 
3015b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
3025b6e9bcdSMing Lei 		struct sk_buff_head *list, enum skb_state state)
3035b2fc499SJeff Garzik {
3045b2fc499SJeff Garzik 	unsigned long		flags;
3055b6e9bcdSMing Lei 	enum skb_state 		old_state;
3065b6e9bcdSMing Lei 	struct skb_data *entry = (struct skb_data *) skb->cb;
3075b2fc499SJeff Garzik 
3085b2fc499SJeff Garzik 	spin_lock_irqsave(&list->lock, flags);
3095b6e9bcdSMing Lei 	old_state = entry->state;
3105b6e9bcdSMing Lei 	entry->state = state;
3115b2fc499SJeff Garzik 	__skb_unlink(skb, list);
3125b2fc499SJeff Garzik 	spin_unlock(&list->lock);
3135b2fc499SJeff Garzik 	spin_lock(&dev->done.lock);
3145b2fc499SJeff Garzik 	__skb_queue_tail(&dev->done, skb);
3155b2fc499SJeff Garzik 	if (dev->done.qlen == 1)
3165b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
3175b2fc499SJeff Garzik 	spin_unlock_irqrestore(&dev->done.lock, flags);
3185b6e9bcdSMing Lei 	return old_state;
3195b2fc499SJeff Garzik }
3205b2fc499SJeff Garzik 
3215b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
3225b2fc499SJeff Garzik  *
3235b2fc499SJeff Garzik  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
3245b2fc499SJeff Garzik  * but tasklet_schedule() doesn't.  hope the failure is rare.
3255b2fc499SJeff Garzik  */
3265b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
3275b2fc499SJeff Garzik {
3285b2fc499SJeff Garzik 	set_bit (work, &dev->flags);
3295b2fc499SJeff Garzik 	if (!schedule_work (&dev->kevent))
33060b86755SJoe Perches 		netdev_err(dev->net, "kevent %d may have been dropped\n", work);
3315b2fc499SJeff Garzik 	else
33260b86755SJoe Perches 		netdev_dbg(dev->net, "kevent %d scheduled\n", work);
3335b2fc499SJeff Garzik }
3345b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
3355b2fc499SJeff Garzik 
3365b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
3375b2fc499SJeff Garzik 
3385b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
3395b2fc499SJeff Garzik 
340dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
3415b2fc499SJeff Garzik {
3425b2fc499SJeff Garzik 	struct sk_buff		*skb;
3435b2fc499SJeff Garzik 	struct skb_data		*entry;
3445b2fc499SJeff Garzik 	int			retval = 0;
3455b2fc499SJeff Garzik 	unsigned long		lockflags;
3465b2fc499SJeff Garzik 	size_t			size = dev->rx_urb_size;
3475b2fc499SJeff Garzik 
3487bdd4027SEric Dumazet 	skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
3497bdd4027SEric Dumazet 	if (!skb) {
350a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
3515b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
3525b2fc499SJeff Garzik 		usb_free_urb (urb);
353dacb3975SDavid S. Miller 		return -ENOMEM;
3545b2fc499SJeff Garzik 	}
3555b2fc499SJeff Garzik 
3565b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
3575b2fc499SJeff Garzik 	entry->urb = urb;
3585b2fc499SJeff Garzik 	entry->dev = dev;
3595b2fc499SJeff Garzik 	entry->length = 0;
3605b2fc499SJeff Garzik 
3615b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
3625b2fc499SJeff Garzik 		skb->data, size, rx_complete, skb);
3635b2fc499SJeff Garzik 
3645b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
3655b2fc499SJeff Garzik 
3668e95a202SJoe Perches 	if (netif_running (dev->net) &&
3678e95a202SJoe Perches 	    netif_device_present (dev->net) &&
36869ee472fSOliver Neukum 	    !test_bit (EVENT_RX_HALT, &dev->flags) &&
36969ee472fSOliver Neukum 	    !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
3705b2fc499SJeff Garzik 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
3715b2fc499SJeff Garzik 		case -EPIPE:
3725b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
3735b2fc499SJeff Garzik 			break;
3745b2fc499SJeff Garzik 		case -ENOMEM:
3755b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
3765b2fc499SJeff Garzik 			break;
3775b2fc499SJeff Garzik 		case -ENODEV:
378a475f603SJoe Perches 			netif_dbg(dev, ifdown, dev->net, "device gone\n");
3795b2fc499SJeff Garzik 			netif_device_detach (dev->net);
3805b2fc499SJeff Garzik 			break;
381dacb3975SDavid S. Miller 		case -EHOSTUNREACH:
382dacb3975SDavid S. Miller 			retval = -ENOLINK;
383dacb3975SDavid S. Miller 			break;
3845b2fc499SJeff Garzik 		default:
385a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
386a475f603SJoe Perches 				  "rx submit, %d\n", retval);
3875b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
3885b2fc499SJeff Garzik 			break;
3895b2fc499SJeff Garzik 		case 0:
3905b6e9bcdSMing Lei 			__usbnet_queue_skb(&dev->rxq, skb, rx_start);
3915b2fc499SJeff Garzik 		}
3925b2fc499SJeff Garzik 	} else {
393a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
3945b2fc499SJeff Garzik 		retval = -ENOLINK;
3955b2fc499SJeff Garzik 	}
3965b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
3975b2fc499SJeff Garzik 	if (retval) {
3985b2fc499SJeff Garzik 		dev_kfree_skb_any (skb);
3995b2fc499SJeff Garzik 		usb_free_urb (urb);
4005b2fc499SJeff Garzik 	}
401dacb3975SDavid S. Miller 	return retval;
4025b2fc499SJeff Garzik }
4035b2fc499SJeff Garzik 
4045b2fc499SJeff Garzik 
4055b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4065b2fc499SJeff Garzik 
4075b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
4085b2fc499SJeff Garzik {
4098e95a202SJoe Perches 	if (dev->driver_info->rx_fixup &&
4107a635ea9SAndrzej Zaborowski 	    !dev->driver_info->rx_fixup (dev, skb)) {
4117a635ea9SAndrzej Zaborowski 		/* With RX_ASSEMBLE, rx_fixup() must update counters */
4127a635ea9SAndrzej Zaborowski 		if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
4137a635ea9SAndrzej Zaborowski 			dev->net->stats.rx_errors++;
4147a635ea9SAndrzej Zaborowski 		goto done;
4157a635ea9SAndrzej Zaborowski 	}
4165b2fc499SJeff Garzik 	// else network stack removes extra byte if we forced a short packet
4175b2fc499SJeff Garzik 
418073285fdSAlexey Orishko 	if (skb->len) {
419073285fdSAlexey Orishko 		/* all data was already cloned from skb inside the driver */
420073285fdSAlexey Orishko 		if (dev->driver_info->flags & FLAG_MULTI_PACKET)
421073285fdSAlexey Orishko 			dev_kfree_skb_any(skb);
422073285fdSAlexey Orishko 		else
4235b2fc499SJeff Garzik 			usbnet_skb_return(dev, skb);
424073285fdSAlexey Orishko 		return;
425073285fdSAlexey Orishko 	}
426073285fdSAlexey Orishko 
427a475f603SJoe Perches 	netif_dbg(dev, rx_err, dev->net, "drop\n");
4287963837fSHerbert Xu 	dev->net->stats.rx_errors++;
4297a635ea9SAndrzej Zaborowski done:
4305b2fc499SJeff Garzik 	skb_queue_tail(&dev->done, skb);
4315b2fc499SJeff Garzik }
4325b2fc499SJeff Garzik 
4335b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4345b2fc499SJeff Garzik 
4355b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
4365b2fc499SJeff Garzik {
4375b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
4385b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
4395b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
4405b2fc499SJeff Garzik 	int			urb_status = urb->status;
4415b6e9bcdSMing Lei 	enum skb_state		state;
4425b2fc499SJeff Garzik 
4435b2fc499SJeff Garzik 	skb_put (skb, urb->actual_length);
4445b6e9bcdSMing Lei 	state = rx_done;
4455b2fc499SJeff Garzik 	entry->urb = NULL;
4465b2fc499SJeff Garzik 
4475b2fc499SJeff Garzik 	switch (urb_status) {
44818ab458fSDavid Brownell 	/* success */
4495b2fc499SJeff Garzik 	case 0:
4505b2fc499SJeff Garzik 		if (skb->len < dev->net->hard_header_len) {
4515b6e9bcdSMing Lei 			state = rx_cleanup;
4527963837fSHerbert Xu 			dev->net->stats.rx_errors++;
4537963837fSHerbert Xu 			dev->net->stats.rx_length_errors++;
454a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
455a475f603SJoe Perches 				  "rx length %d\n", skb->len);
4565b2fc499SJeff Garzik 		}
4575b2fc499SJeff Garzik 		break;
4585b2fc499SJeff Garzik 
45918ab458fSDavid Brownell 	/* stalls need manual reset. this is rare ... except that
46018ab458fSDavid Brownell 	 * when going through USB 2.0 TTs, unplug appears this way.
4613ac49a1cSJean Delvare 	 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
46218ab458fSDavid Brownell 	 * storm, recovering as needed.
46318ab458fSDavid Brownell 	 */
4645b2fc499SJeff Garzik 	case -EPIPE:
4657963837fSHerbert Xu 		dev->net->stats.rx_errors++;
4665b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
4675b2fc499SJeff Garzik 		// FALLTHROUGH
4685b2fc499SJeff Garzik 
46918ab458fSDavid Brownell 	/* software-driven interface shutdown */
47018ab458fSDavid Brownell 	case -ECONNRESET:		/* async unlink */
47118ab458fSDavid Brownell 	case -ESHUTDOWN:		/* hardware gone */
472a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net,
473a475f603SJoe Perches 			  "rx shutdown, code %d\n", urb_status);
4745b2fc499SJeff Garzik 		goto block;
4755b2fc499SJeff Garzik 
47618ab458fSDavid Brownell 	/* we get controller i/o faults during khubd disconnect() delays.
47718ab458fSDavid Brownell 	 * throttle down resubmits, to avoid log floods; just temporarily,
47818ab458fSDavid Brownell 	 * so we still recover when the fault isn't a khubd delay.
47918ab458fSDavid Brownell 	 */
4805b2fc499SJeff Garzik 	case -EPROTO:
4815b2fc499SJeff Garzik 	case -ETIME:
4825b2fc499SJeff Garzik 	case -EILSEQ:
4837963837fSHerbert Xu 		dev->net->stats.rx_errors++;
4845b2fc499SJeff Garzik 		if (!timer_pending (&dev->delay)) {
4855b2fc499SJeff Garzik 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
486a475f603SJoe Perches 			netif_dbg(dev, link, dev->net,
487a475f603SJoe Perches 				  "rx throttle %d\n", urb_status);
4885b2fc499SJeff Garzik 		}
4895b2fc499SJeff Garzik block:
4905b6e9bcdSMing Lei 		state = rx_cleanup;
4915b2fc499SJeff Garzik 		entry->urb = urb;
4925b2fc499SJeff Garzik 		urb = NULL;
4935b2fc499SJeff Garzik 		break;
4945b2fc499SJeff Garzik 
49518ab458fSDavid Brownell 	/* data overrun ... flush fifo? */
4965b2fc499SJeff Garzik 	case -EOVERFLOW:
4977963837fSHerbert Xu 		dev->net->stats.rx_over_errors++;
4985b2fc499SJeff Garzik 		// FALLTHROUGH
4995b2fc499SJeff Garzik 
5005b2fc499SJeff Garzik 	default:
5015b6e9bcdSMing Lei 		state = rx_cleanup;
5027963837fSHerbert Xu 		dev->net->stats.rx_errors++;
503a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
5045b2fc499SJeff Garzik 		break;
5055b2fc499SJeff Garzik 	}
5065b2fc499SJeff Garzik 
5075b6e9bcdSMing Lei 	state = defer_bh(dev, skb, &dev->rxq, state);
5085b2fc499SJeff Garzik 
5095b2fc499SJeff Garzik 	if (urb) {
5108e95a202SJoe Perches 		if (netif_running (dev->net) &&
5115b6e9bcdSMing Lei 		    !test_bit (EVENT_RX_HALT, &dev->flags) &&
5125b6e9bcdSMing Lei 		    state != unlink_start) {
5135b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_ATOMIC);
5148a783354SOliver Neukum 			usb_mark_last_busy(dev->udev);
5155b2fc499SJeff Garzik 			return;
5165b2fc499SJeff Garzik 		}
5175b2fc499SJeff Garzik 		usb_free_urb (urb);
5185b2fc499SJeff Garzik 	}
519a475f603SJoe Perches 	netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
5205b2fc499SJeff Garzik }
5215b2fc499SJeff Garzik 
5225b2fc499SJeff Garzik static void intr_complete (struct urb *urb)
5235b2fc499SJeff Garzik {
5245b2fc499SJeff Garzik 	struct usbnet	*dev = urb->context;
5255b2fc499SJeff Garzik 	int		status = urb->status;
5265b2fc499SJeff Garzik 
5275b2fc499SJeff Garzik 	switch (status) {
5285b2fc499SJeff Garzik 	/* success */
5295b2fc499SJeff Garzik 	case 0:
5305b2fc499SJeff Garzik 		dev->driver_info->status(dev, urb);
5315b2fc499SJeff Garzik 		break;
5325b2fc499SJeff Garzik 
5335b2fc499SJeff Garzik 	/* software-driven interface shutdown */
53418ab458fSDavid Brownell 	case -ENOENT:		/* urb killed */
53518ab458fSDavid Brownell 	case -ESHUTDOWN:	/* hardware gone */
536a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net,
537a475f603SJoe Perches 			  "intr shutdown, code %d\n", status);
5385b2fc499SJeff Garzik 		return;
5395b2fc499SJeff Garzik 
5405b2fc499SJeff Garzik 	/* NOTE:  not throttling like RX/TX, since this endpoint
5415b2fc499SJeff Garzik 	 * already polls infrequently
5425b2fc499SJeff Garzik 	 */
5435b2fc499SJeff Garzik 	default:
54460b86755SJoe Perches 		netdev_dbg(dev->net, "intr status %d\n", status);
5455b2fc499SJeff Garzik 		break;
5465b2fc499SJeff Garzik 	}
5475b2fc499SJeff Garzik 
5485b2fc499SJeff Garzik 	if (!netif_running (dev->net))
5495b2fc499SJeff Garzik 		return;
5505b2fc499SJeff Garzik 
5515b2fc499SJeff Garzik 	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
5525b2fc499SJeff Garzik 	status = usb_submit_urb (urb, GFP_ATOMIC);
553a475f603SJoe Perches 	if (status != 0)
554a475f603SJoe Perches 		netif_err(dev, timer, dev->net,
555a475f603SJoe Perches 			  "intr resubmit --> %d\n", status);
5565b2fc499SJeff Garzik }
5575b2fc499SJeff Garzik 
5585b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5597834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev)
5607834ddbcSJussi Kivilinna {
5617834ddbcSJussi Kivilinna 	set_bit(EVENT_RX_PAUSED, &dev->flags);
5627834ddbcSJussi Kivilinna 
563a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
5647834ddbcSJussi Kivilinna }
5657834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx);
5667834ddbcSJussi Kivilinna 
5677834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev)
5687834ddbcSJussi Kivilinna {
5697834ddbcSJussi Kivilinna 	struct sk_buff *skb;
5707834ddbcSJussi Kivilinna 	int num = 0;
5717834ddbcSJussi Kivilinna 
5727834ddbcSJussi Kivilinna 	clear_bit(EVENT_RX_PAUSED, &dev->flags);
5737834ddbcSJussi Kivilinna 
5747834ddbcSJussi Kivilinna 	while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
5757834ddbcSJussi Kivilinna 		usbnet_skb_return(dev, skb);
5767834ddbcSJussi Kivilinna 		num++;
5777834ddbcSJussi Kivilinna 	}
5787834ddbcSJussi Kivilinna 
5797834ddbcSJussi Kivilinna 	tasklet_schedule(&dev->bh);
5807834ddbcSJussi Kivilinna 
581a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net,
582a475f603SJoe Perches 		  "paused rx queue disabled, %d skbs requeued\n", num);
5837834ddbcSJussi Kivilinna }
5847834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx);
5857834ddbcSJussi Kivilinna 
5867834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev)
5877834ddbcSJussi Kivilinna {
5887834ddbcSJussi Kivilinna 	skb_queue_purge(&dev->rxq_pause);
5897834ddbcSJussi Kivilinna }
5907834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
5917834ddbcSJussi Kivilinna 
5927834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/
5935b2fc499SJeff Garzik 
5945b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
5955b2fc499SJeff Garzik 
5965b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
5975b2fc499SJeff Garzik {
5985b2fc499SJeff Garzik 	unsigned long		flags;
5995b6e9bcdSMing Lei 	struct sk_buff		*skb;
6005b2fc499SJeff Garzik 	int			count = 0;
6015b2fc499SJeff Garzik 
6025b2fc499SJeff Garzik 	spin_lock_irqsave (&q->lock, flags);
6035b6e9bcdSMing Lei 	while (!skb_queue_empty(q)) {
6045b2fc499SJeff Garzik 		struct skb_data		*entry;
6055b2fc499SJeff Garzik 		struct urb		*urb;
6065b2fc499SJeff Garzik 		int			retval;
6075b2fc499SJeff Garzik 
6085b6e9bcdSMing Lei 		skb_queue_walk(q, skb) {
6095b2fc499SJeff Garzik 			entry = (struct skb_data *) skb->cb;
6105b6e9bcdSMing Lei 			if (entry->state != unlink_start)
6115b6e9bcdSMing Lei 				goto found;
6125b6e9bcdSMing Lei 		}
6135b6e9bcdSMing Lei 		break;
6145b6e9bcdSMing Lei found:
6155b6e9bcdSMing Lei 		entry->state = unlink_start;
6165b2fc499SJeff Garzik 		urb = entry->urb;
6175b2fc499SJeff Garzik 
6180956a8c2Stom.leiming@gmail.com 		/*
6190956a8c2Stom.leiming@gmail.com 		 * Get reference count of the URB to avoid it to be
6200956a8c2Stom.leiming@gmail.com 		 * freed during usb_unlink_urb, which may trigger
6210956a8c2Stom.leiming@gmail.com 		 * use-after-free problem inside usb_unlink_urb since
6220956a8c2Stom.leiming@gmail.com 		 * usb_unlink_urb is always racing with .complete
6230956a8c2Stom.leiming@gmail.com 		 * handler(include defer_bh).
6240956a8c2Stom.leiming@gmail.com 		 */
6250956a8c2Stom.leiming@gmail.com 		usb_get_urb(urb);
6264231d47eSSebastian Siewior 		spin_unlock_irqrestore(&q->lock, flags);
6275b2fc499SJeff Garzik 		// during some PM-driven resume scenarios,
6285b2fc499SJeff Garzik 		// these (async) unlinks complete immediately
6295b2fc499SJeff Garzik 		retval = usb_unlink_urb (urb);
6305b2fc499SJeff Garzik 		if (retval != -EINPROGRESS && retval != 0)
63160b86755SJoe Perches 			netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
6325b2fc499SJeff Garzik 		else
6335b2fc499SJeff Garzik 			count++;
6340956a8c2Stom.leiming@gmail.com 		usb_put_urb(urb);
6354231d47eSSebastian Siewior 		spin_lock_irqsave(&q->lock, flags);
6365b2fc499SJeff Garzik 	}
6375b2fc499SJeff Garzik 	spin_unlock_irqrestore (&q->lock, flags);
6385b2fc499SJeff Garzik 	return count;
6395b2fc499SJeff Garzik }
6405b2fc499SJeff Garzik 
6415b2fc499SJeff Garzik // Flush all pending rx urbs
6425b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
6435b2fc499SJeff Garzik 
6445b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
6455b2fc499SJeff Garzik {
6465b2fc499SJeff Garzik 	if (netif_running(dev->net)) {
6475b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
6485b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
6495b2fc499SJeff Garzik 	}
6505b2fc499SJeff Garzik }
6515b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
6525b2fc499SJeff Garzik 
6535b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
6545b2fc499SJeff Garzik 
6555b2fc499SJeff Garzik // precondition: never called in_interrupt
65669ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev)
65769ee472fSOliver Neukum {
65869ee472fSOliver Neukum 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
65969ee472fSOliver Neukum 	DECLARE_WAITQUEUE(wait, current);
66069ee472fSOliver Neukum 	int temp;
66169ee472fSOliver Neukum 
66269ee472fSOliver Neukum 	/* ensure there are no more active urbs */
66369ee472fSOliver Neukum 	add_wait_queue(&unlink_wakeup, &wait);
66469ee472fSOliver Neukum 	set_current_state(TASK_UNINTERRUPTIBLE);
66569ee472fSOliver Neukum 	dev->wait = &unlink_wakeup;
66669ee472fSOliver Neukum 	temp = unlink_urbs(dev, &dev->txq) +
66769ee472fSOliver Neukum 		unlink_urbs(dev, &dev->rxq);
66869ee472fSOliver Neukum 
66969ee472fSOliver Neukum 	/* maybe wait for deletions to finish. */
67069ee472fSOliver Neukum 	while (!skb_queue_empty(&dev->rxq)
67169ee472fSOliver Neukum 		&& !skb_queue_empty(&dev->txq)
67269ee472fSOliver Neukum 		&& !skb_queue_empty(&dev->done)) {
67366cc42a4SKulikov Vasiliy 			schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
67469ee472fSOliver Neukum 			set_current_state(TASK_UNINTERRUPTIBLE);
675a475f603SJoe Perches 			netif_dbg(dev, ifdown, dev->net,
676a475f603SJoe Perches 				  "waited for %d urb completions\n", temp);
67769ee472fSOliver Neukum 	}
67869ee472fSOliver Neukum 	set_current_state(TASK_RUNNING);
67969ee472fSOliver Neukum 	dev->wait = NULL;
68069ee472fSOliver Neukum 	remove_wait_queue(&unlink_wakeup, &wait);
68169ee472fSOliver Neukum }
6825b2fc499SJeff Garzik 
683777baa47SStephen Hemminger int usbnet_stop (struct net_device *net)
6845b2fc499SJeff Garzik {
6855b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
686a33e9e7fSJussi Kivilinna 	struct driver_info	*info = dev->driver_info;
687a33e9e7fSJussi Kivilinna 	int			retval;
6885b2fc499SJeff Garzik 
68975bd0cbdSMing Lei 	clear_bit(EVENT_DEV_OPEN, &dev->flags);
6905b2fc499SJeff Garzik 	netif_stop_queue (net);
6915b2fc499SJeff Garzik 
692a475f603SJoe Perches 	netif_info(dev, ifdown, dev->net,
6938ccef431SBen Hutchings 		   "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
6947963837fSHerbert Xu 		   net->stats.rx_packets, net->stats.tx_packets,
69560b86755SJoe Perches 		   net->stats.rx_errors, net->stats.tx_errors);
6965b2fc499SJeff Garzik 
697a33e9e7fSJussi Kivilinna 	/* allow minidriver to stop correctly (wireless devices to turn off
698a33e9e7fSJussi Kivilinna 	 * radio etc) */
699a33e9e7fSJussi Kivilinna 	if (info->stop) {
700a33e9e7fSJussi Kivilinna 		retval = info->stop(dev);
701a475f603SJoe Perches 		if (retval < 0)
702a475f603SJoe Perches 			netif_info(dev, ifdown, dev->net,
70360b86755SJoe Perches 				   "stop fail (%d) usbnet usb-%s-%s, %s\n",
704a33e9e7fSJussi Kivilinna 				   retval,
705a33e9e7fSJussi Kivilinna 				   dev->udev->bus->bus_name, dev->udev->devpath,
706a33e9e7fSJussi Kivilinna 				   info->description);
707a33e9e7fSJussi Kivilinna 	}
708a33e9e7fSJussi Kivilinna 
70969ee472fSOliver Neukum 	if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
71069ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
7115b2fc499SJeff Garzik 
7125b2fc499SJeff Garzik 	usb_kill_urb(dev->interrupt);
7135b2fc499SJeff Garzik 
7147834ddbcSJussi Kivilinna 	usbnet_purge_paused_rxq(dev);
7157834ddbcSJussi Kivilinna 
7165b2fc499SJeff Garzik 	/* deferred work (task, timer, softirq) must also stop.
7175b2fc499SJeff Garzik 	 * can't flush_scheduled_work() until we drop rtnl (later),
7185b2fc499SJeff Garzik 	 * else workers could deadlock; so make workers a NOP.
7195b2fc499SJeff Garzik 	 */
7205b2fc499SJeff Garzik 	dev->flags = 0;
7215b2fc499SJeff Garzik 	del_timer_sync (&dev->delay);
7225b2fc499SJeff Garzik 	tasklet_kill (&dev->bh);
72369ee472fSOliver Neukum 	if (info->manage_power)
72469ee472fSOliver Neukum 		info->manage_power(dev, 0);
72569ee472fSOliver Neukum 	else
726a11a6544SOliver Neukum 		usb_autopm_put_interface(dev->intf);
7275b2fc499SJeff Garzik 
7285b2fc499SJeff Garzik 	return 0;
7295b2fc499SJeff Garzik }
730777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop);
7315b2fc499SJeff Garzik 
7325b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
7335b2fc499SJeff Garzik 
7345b2fc499SJeff Garzik // posts reads, and enables write queuing
7355b2fc499SJeff Garzik 
7365b2fc499SJeff Garzik // precondition: never called in_interrupt
7375b2fc499SJeff Garzik 
738777baa47SStephen Hemminger int usbnet_open (struct net_device *net)
7395b2fc499SJeff Garzik {
7405b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
741a11a6544SOliver Neukum 	int			retval;
7425b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
7435b2fc499SJeff Garzik 
744a11a6544SOliver Neukum 	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
745a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
74660b86755SJoe Perches 			   "resumption fail (%d) usbnet usb-%s-%s, %s\n",
747a11a6544SOliver Neukum 			   retval,
74860b86755SJoe Perches 			   dev->udev->bus->bus_name,
74960b86755SJoe Perches 			   dev->udev->devpath,
750a11a6544SOliver Neukum 			   info->description);
751a11a6544SOliver Neukum 		goto done_nopm;
752a11a6544SOliver Neukum 	}
753a11a6544SOliver Neukum 
7545b2fc499SJeff Garzik 	// put into "known safe" state
7555b2fc499SJeff Garzik 	if (info->reset && (retval = info->reset (dev)) < 0) {
756a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
75760b86755SJoe Perches 			   "open reset fail (%d) usbnet usb-%s-%s, %s\n",
7585b2fc499SJeff Garzik 			   retval,
75960b86755SJoe Perches 			   dev->udev->bus->bus_name,
76060b86755SJoe Perches 			   dev->udev->devpath,
7615b2fc499SJeff Garzik 			   info->description);
7625b2fc499SJeff Garzik 		goto done;
7635b2fc499SJeff Garzik 	}
7645b2fc499SJeff Garzik 
7655b2fc499SJeff Garzik 	// insist peer be connected
7665b2fc499SJeff Garzik 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
767a475f603SJoe Perches 		netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
7685b2fc499SJeff Garzik 		goto done;
7695b2fc499SJeff Garzik 	}
7705b2fc499SJeff Garzik 
7715b2fc499SJeff Garzik 	/* start any status interrupt transfer */
7725b2fc499SJeff Garzik 	if (dev->interrupt) {
7735b2fc499SJeff Garzik 		retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
7745b2fc499SJeff Garzik 		if (retval < 0) {
775a475f603SJoe Perches 			netif_err(dev, ifup, dev->net,
776a475f603SJoe Perches 				  "intr submit %d\n", retval);
7775b2fc499SJeff Garzik 			goto done;
7785b2fc499SJeff Garzik 		}
7795b2fc499SJeff Garzik 	}
7805b2fc499SJeff Garzik 
78168972efaSPaul Stewart 	set_bit(EVENT_DEV_OPEN, &dev->flags);
7825b2fc499SJeff Garzik 	netif_start_queue (net);
783a475f603SJoe Perches 	netif_info(dev, ifup, dev->net,
784a475f603SJoe Perches 		   "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
78560b86755SJoe Perches 		   (int)RX_QLEN(dev), (int)TX_QLEN(dev),
786a475f603SJoe Perches 		   dev->net->mtu,
787a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
788a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
789a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
790a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
791a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
792a475f603SJoe Perches 		   "simple");
7935b2fc499SJeff Garzik 
7945b2fc499SJeff Garzik 	// delay posting reads until we're fully open
7955b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
79669ee472fSOliver Neukum 	if (info->manage_power) {
79769ee472fSOliver Neukum 		retval = info->manage_power(dev, 1);
79869ee472fSOliver Neukum 		if (retval < 0)
799fb13c47bSMing Lei 			goto done_manage_power_error;
80069ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
80169ee472fSOliver Neukum 	}
802a11a6544SOliver Neukum 	return retval;
803a475f603SJoe Perches 
804fb13c47bSMing Lei done_manage_power_error:
805fb13c47bSMing Lei 	clear_bit(EVENT_DEV_OPEN, &dev->flags);
8065b2fc499SJeff Garzik done:
807a11a6544SOliver Neukum 	usb_autopm_put_interface(dev->intf);
808a11a6544SOliver Neukum done_nopm:
8095b2fc499SJeff Garzik 	return retval;
8105b2fc499SJeff Garzik }
811777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open);
8125b2fc499SJeff Garzik 
8135b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
8145b2fc499SJeff Garzik 
8155b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
8165b2fc499SJeff Garzik  * they'll probably want to use this base set.
8175b2fc499SJeff Garzik  */
8185b2fc499SJeff Garzik 
8195b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
8205b2fc499SJeff Garzik {
8215b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8225b2fc499SJeff Garzik 
8235b2fc499SJeff Garzik 	if (!dev->mii.mdio_read)
8245b2fc499SJeff Garzik 		return -EOPNOTSUPP;
8255b2fc499SJeff Garzik 
8265b2fc499SJeff Garzik 	return mii_ethtool_gset(&dev->mii, cmd);
8275b2fc499SJeff Garzik }
8285b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings);
8295b2fc499SJeff Garzik 
8305b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
8315b2fc499SJeff Garzik {
8325b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8335b2fc499SJeff Garzik 	int retval;
8345b2fc499SJeff Garzik 
8355b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
8365b2fc499SJeff Garzik 		return -EOPNOTSUPP;
8375b2fc499SJeff Garzik 
8385b2fc499SJeff Garzik 	retval = mii_ethtool_sset(&dev->mii, cmd);
8395b2fc499SJeff Garzik 
8405b2fc499SJeff Garzik 	/* link speed/duplex might have changed */
8415b2fc499SJeff Garzik 	if (dev->driver_info->link_reset)
8425b2fc499SJeff Garzik 		dev->driver_info->link_reset(dev);
8435b2fc499SJeff Garzik 
8445b2fc499SJeff Garzik 	return retval;
8455b2fc499SJeff Garzik 
8465b2fc499SJeff Garzik }
8475b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings);
8485b2fc499SJeff Garzik 
8495b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
8505b2fc499SJeff Garzik {
8515b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8525b2fc499SJeff Garzik 
8535b2fc499SJeff Garzik 	/* If a check_connect is defined, return its result */
8545b2fc499SJeff Garzik 	if (dev->driver_info->check_connect)
8555b2fc499SJeff Garzik 		return dev->driver_info->check_connect (dev) == 0;
8565b2fc499SJeff Garzik 
8575b2fc499SJeff Garzik 	/* if the device has mii operations, use those */
8585b2fc499SJeff Garzik 	if (dev->mii.mdio_read)
8595b2fc499SJeff Garzik 		return mii_link_ok(&dev->mii);
8605b2fc499SJeff Garzik 
86105ffb3e2SBjørn Mork 	/* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
86205ffb3e2SBjørn Mork 	return ethtool_op_get_link(net);
8635b2fc499SJeff Garzik }
8645b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
8655b2fc499SJeff Garzik 
8665b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
8675b2fc499SJeff Garzik {
8685b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8695b2fc499SJeff Garzik 
8705b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
8715b2fc499SJeff Garzik 		return -EOPNOTSUPP;
8725b2fc499SJeff Garzik 
8735b2fc499SJeff Garzik 	return mii_nway_restart(&dev->mii);
8745b2fc499SJeff Garzik }
8755b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
8765b2fc499SJeff Garzik 
8775b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
8785b2fc499SJeff Garzik {
8795b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8805b2fc499SJeff Garzik 
88186a2f415SPhil Sutter 	strlcpy (info->driver, dev->driver_name, sizeof info->driver);
88286a2f415SPhil Sutter 	strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
88386a2f415SPhil Sutter 	strlcpy (info->fw_version, dev->driver_info->description,
8845b2fc499SJeff Garzik 		sizeof info->fw_version);
8855b2fc499SJeff Garzik 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
8865b2fc499SJeff Garzik }
8875b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
8885b2fc499SJeff Garzik 
8895b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
8905b2fc499SJeff Garzik {
8915b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8925b2fc499SJeff Garzik 
8935b2fc499SJeff Garzik 	return dev->msg_enable;
8945b2fc499SJeff Garzik }
8955b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
8965b2fc499SJeff Garzik 
8975b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
8985b2fc499SJeff Garzik {
8995b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
9005b2fc499SJeff Garzik 
9015b2fc499SJeff Garzik 	dev->msg_enable = level;
9025b2fc499SJeff Garzik }
9035b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
9045b2fc499SJeff Garzik 
9055b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
9060fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = {
9075b2fc499SJeff Garzik 	.get_settings		= usbnet_get_settings,
9085b2fc499SJeff Garzik 	.set_settings		= usbnet_set_settings,
9095b2fc499SJeff Garzik 	.get_link		= usbnet_get_link,
9105b2fc499SJeff Garzik 	.nway_reset		= usbnet_nway_reset,
9115b2fc499SJeff Garzik 	.get_drvinfo		= usbnet_get_drvinfo,
9125b2fc499SJeff Garzik 	.get_msglevel		= usbnet_get_msglevel,
9135b2fc499SJeff Garzik 	.set_msglevel		= usbnet_set_msglevel,
914123edb18SRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
9155b2fc499SJeff Garzik };
9165b2fc499SJeff Garzik 
9175b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9185b2fc499SJeff Garzik 
9195b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
9205b2fc499SJeff Garzik  *
9215b2fc499SJeff Garzik  * NOTE:  with 2.5 we could do more of this using completion callbacks,
9225b2fc499SJeff Garzik  * especially now that control transfers can be queued.
9235b2fc499SJeff Garzik  */
9245b2fc499SJeff Garzik static void
9255b2fc499SJeff Garzik kevent (struct work_struct *work)
9265b2fc499SJeff Garzik {
9275b2fc499SJeff Garzik 	struct usbnet		*dev =
9285b2fc499SJeff Garzik 		container_of(work, struct usbnet, kevent);
9295b2fc499SJeff Garzik 	int			status;
9305b2fc499SJeff Garzik 
9315b2fc499SJeff Garzik 	/* usb_clear_halt() needs a thread context */
9325b2fc499SJeff Garzik 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
9335b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->txq);
93469ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
93569ee472fSOliver Neukum 		if (status < 0)
93669ee472fSOliver Neukum 			goto fail_pipe;
9375b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->out);
93869ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
9398e95a202SJoe Perches 		if (status < 0 &&
9408e95a202SJoe Perches 		    status != -EPIPE &&
9418e95a202SJoe Perches 		    status != -ESHUTDOWN) {
9425b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
94369ee472fSOliver Neukum fail_pipe:
94460b86755SJoe Perches 				netdev_err(dev->net, "can't clear tx halt, status %d\n",
9455b2fc499SJeff Garzik 					   status);
9465b2fc499SJeff Garzik 		} else {
9475b2fc499SJeff Garzik 			clear_bit (EVENT_TX_HALT, &dev->flags);
9485b2fc499SJeff Garzik 			if (status != -ESHUTDOWN)
9495b2fc499SJeff Garzik 				netif_wake_queue (dev->net);
9505b2fc499SJeff Garzik 		}
9515b2fc499SJeff Garzik 	}
9525b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
9535b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->rxq);
95469ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
95569ee472fSOliver Neukum 		if (status < 0)
95669ee472fSOliver Neukum 			goto fail_halt;
9575b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->in);
95869ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
9598e95a202SJoe Perches 		if (status < 0 &&
9608e95a202SJoe Perches 		    status != -EPIPE &&
9618e95a202SJoe Perches 		    status != -ESHUTDOWN) {
9625b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
96369ee472fSOliver Neukum fail_halt:
96460b86755SJoe Perches 				netdev_err(dev->net, "can't clear rx halt, status %d\n",
9655b2fc499SJeff Garzik 					   status);
9665b2fc499SJeff Garzik 		} else {
9675b2fc499SJeff Garzik 			clear_bit (EVENT_RX_HALT, &dev->flags);
9685b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
9695b2fc499SJeff Garzik 		}
9705b2fc499SJeff Garzik 	}
9715b2fc499SJeff Garzik 
9725b2fc499SJeff Garzik 	/* tasklet could resubmit itself forever if memory is tight */
9735b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
9745b2fc499SJeff Garzik 		struct urb	*urb = NULL;
975dacb3975SDavid S. Miller 		int resched = 1;
9765b2fc499SJeff Garzik 
9775b2fc499SJeff Garzik 		if (netif_running (dev->net))
9785b2fc499SJeff Garzik 			urb = usb_alloc_urb (0, GFP_KERNEL);
9795b2fc499SJeff Garzik 		else
9805b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
9815b2fc499SJeff Garzik 		if (urb != NULL) {
9825b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
98369ee472fSOliver Neukum 			status = usb_autopm_get_interface(dev->intf);
984ab60707fSJesper Juhl 			if (status < 0) {
985ab60707fSJesper Juhl 				usb_free_urb(urb);
98669ee472fSOliver Neukum 				goto fail_lowmem;
987ab60707fSJesper Juhl 			}
988dacb3975SDavid S. Miller 			if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
989dacb3975SDavid S. Miller 				resched = 0;
99069ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
99169ee472fSOliver Neukum fail_lowmem:
992dacb3975SDavid S. Miller 			if (resched)
9935b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
9945b2fc499SJeff Garzik 		}
9955b2fc499SJeff Garzik 	}
9965b2fc499SJeff Garzik 
9975b2fc499SJeff Garzik 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
9985b2fc499SJeff Garzik 		struct driver_info	*info = dev->driver_info;
9995b2fc499SJeff Garzik 		int			retval = 0;
10005b2fc499SJeff Garzik 
10015b2fc499SJeff Garzik 		clear_bit (EVENT_LINK_RESET, &dev->flags);
100269ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
100369ee472fSOliver Neukum 		if (status < 0)
100469ee472fSOliver Neukum 			goto skip_reset;
10055b2fc499SJeff Garzik 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
100669ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
100769ee472fSOliver Neukum skip_reset:
100860b86755SJoe Perches 			netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
10095b2fc499SJeff Garzik 				    retval,
101060b86755SJoe Perches 				    dev->udev->bus->bus_name,
101160b86755SJoe Perches 				    dev->udev->devpath,
10125b2fc499SJeff Garzik 				    info->description);
101369ee472fSOliver Neukum 		} else {
101469ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
10155b2fc499SJeff Garzik 		}
10165b2fc499SJeff Garzik 	}
10175b2fc499SJeff Garzik 
10185b2fc499SJeff Garzik 	if (dev->flags)
101960b86755SJoe Perches 		netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
10205b2fc499SJeff Garzik }
10215b2fc499SJeff Garzik 
10225b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10235b2fc499SJeff Garzik 
10245b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
10255b2fc499SJeff Garzik {
10265b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
10275b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
10285b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
10295b2fc499SJeff Garzik 
10305b2fc499SJeff Garzik 	if (urb->status == 0) {
1031073285fdSAlexey Orishko 		if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
10327963837fSHerbert Xu 			dev->net->stats.tx_packets++;
10337963837fSHerbert Xu 		dev->net->stats.tx_bytes += entry->length;
10345b2fc499SJeff Garzik 	} else {
10357963837fSHerbert Xu 		dev->net->stats.tx_errors++;
10365b2fc499SJeff Garzik 
10375b2fc499SJeff Garzik 		switch (urb->status) {
10385b2fc499SJeff Garzik 		case -EPIPE:
10395b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
10405b2fc499SJeff Garzik 			break;
10415b2fc499SJeff Garzik 
10425b2fc499SJeff Garzik 		/* software-driven interface shutdown */
10435b2fc499SJeff Garzik 		case -ECONNRESET:		// async unlink
10445b2fc499SJeff Garzik 		case -ESHUTDOWN:		// hardware gone
10455b2fc499SJeff Garzik 			break;
10465b2fc499SJeff Garzik 
10475b2fc499SJeff Garzik 		// like rx, tx gets controller i/o faults during khubd delays
10485b2fc499SJeff Garzik 		// and so it uses the same throttling mechanism.
10495b2fc499SJeff Garzik 		case -EPROTO:
10505b2fc499SJeff Garzik 		case -ETIME:
10515b2fc499SJeff Garzik 		case -EILSEQ:
105269ee472fSOliver Neukum 			usb_mark_last_busy(dev->udev);
10535b2fc499SJeff Garzik 			if (!timer_pending (&dev->delay)) {
10545b2fc499SJeff Garzik 				mod_timer (&dev->delay,
10555b2fc499SJeff Garzik 					jiffies + THROTTLE_JIFFIES);
1056a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1057a475f603SJoe Perches 					  "tx throttle %d\n", urb->status);
10585b2fc499SJeff Garzik 			}
10595b2fc499SJeff Garzik 			netif_stop_queue (dev->net);
10605b2fc499SJeff Garzik 			break;
10615b2fc499SJeff Garzik 		default:
1062a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net,
1063a475f603SJoe Perches 				  "tx err %d\n", entry->urb->status);
10645b2fc499SJeff Garzik 			break;
10655b2fc499SJeff Garzik 		}
10665b2fc499SJeff Garzik 	}
10675b2fc499SJeff Garzik 
106869ee472fSOliver Neukum 	usb_autopm_put_interface_async(dev->intf);
10695b6e9bcdSMing Lei 	(void) defer_bh(dev, skb, &dev->txq, tx_done);
10705b2fc499SJeff Garzik }
10715b2fc499SJeff Garzik 
10725b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10735b2fc499SJeff Garzik 
1074777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net)
10755b2fc499SJeff Garzik {
10765b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
10775b2fc499SJeff Garzik 
10785b2fc499SJeff Garzik 	unlink_urbs (dev, &dev->txq);
10795b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
10805b2fc499SJeff Garzik 
10815b2fc499SJeff Garzik 	// FIXME: device recovery -- reset?
10825b2fc499SJeff Garzik }
1083777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
10845b2fc499SJeff Garzik 
10855b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10865b2fc499SJeff Garzik 
108725a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
108825a79c41SStephen Hemminger 				     struct net_device *net)
10895b2fc499SJeff Garzik {
10905b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
10915b2fc499SJeff Garzik 	int			length;
10925b2fc499SJeff Garzik 	struct urb		*urb = NULL;
10935b2fc499SJeff Garzik 	struct skb_data		*entry;
10945b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
10955b2fc499SJeff Garzik 	unsigned long		flags;
109625a79c41SStephen Hemminger 	int retval;
10975b2fc499SJeff Garzik 
109823ba0799SKonstantin Khlebnikov 	if (skb)
1099f9b491ecSMichael Riesch 		skb_tx_timestamp(skb);
1100f9b491ecSMichael Riesch 
11015b2fc499SJeff Garzik 	// some devices want funky USB-level framing, for
11025b2fc499SJeff Garzik 	// win32 driver (usually) and/or hardware quirks
11035b2fc499SJeff Garzik 	if (info->tx_fixup) {
11045b2fc499SJeff Garzik 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
11055b2fc499SJeff Garzik 		if (!skb) {
1106073285fdSAlexey Orishko 			if (netif_msg_tx_err(dev)) {
1107a475f603SJoe Perches 				netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
11085b2fc499SJeff Garzik 				goto drop;
1109073285fdSAlexey Orishko 			} else {
1110073285fdSAlexey Orishko 				/* cdc_ncm collected packet; waits for more */
1111073285fdSAlexey Orishko 				goto not_drop;
1112073285fdSAlexey Orishko 			}
11135b2fc499SJeff Garzik 		}
11145b2fc499SJeff Garzik 	}
11155b2fc499SJeff Garzik 	length = skb->len;
11165b2fc499SJeff Garzik 
11175b2fc499SJeff Garzik 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1118a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "no urb\n");
11195b2fc499SJeff Garzik 		goto drop;
11205b2fc499SJeff Garzik 	}
11215b2fc499SJeff Garzik 
11225b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
11235b2fc499SJeff Garzik 	entry->urb = urb;
11245b2fc499SJeff Garzik 	entry->dev = dev;
11255b2fc499SJeff Garzik 	entry->length = length;
11265b2fc499SJeff Garzik 
11275b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
11285b2fc499SJeff Garzik 			skb->data, skb->len, tx_complete, skb);
11295b2fc499SJeff Garzik 
11305b2fc499SJeff Garzik 	/* don't assume the hardware handles USB_ZERO_PACKET
11315b2fc499SJeff Garzik 	 * NOTE:  strictly conforming cdc-ether devices should expect
11325b2fc499SJeff Garzik 	 * the ZLP here, but ignore the one-byte packet.
1133073285fdSAlexey Orishko 	 * NOTE2: CDC NCM specification is different from CDC ECM when
1134073285fdSAlexey Orishko 	 * handling ZLP/short packets, so cdc_ncm driver will make short
1135073285fdSAlexey Orishko 	 * packet itself if needed.
11365b2fc499SJeff Garzik 	 */
1137b4d562e3SElina Pasheva 	if (length % dev->maxpacket == 0) {
1138b4d562e3SElina Pasheva 		if (!(info->flags & FLAG_SEND_ZLP)) {
1139073285fdSAlexey Orishko 			if (!(info->flags & FLAG_MULTI_PACKET)) {
11405b2fc499SJeff Garzik 				urb->transfer_buffer_length++;
11413e323f3eSPeter Korsgaard 				if (skb_tailroom(skb)) {
11423e323f3eSPeter Korsgaard 					skb->data[skb->len] = 0;
11433e323f3eSPeter Korsgaard 					__skb_put(skb, 1);
11443e323f3eSPeter Korsgaard 				}
1145073285fdSAlexey Orishko 			}
1146b4d562e3SElina Pasheva 		} else
1147b4d562e3SElina Pasheva 			urb->transfer_flags |= URB_ZERO_PACKET;
11483e323f3eSPeter Korsgaard 	}
11495b2fc499SJeff Garzik 
11505b2fc499SJeff Garzik 	spin_lock_irqsave(&dev->txq.lock, flags);
115169ee472fSOliver Neukum 	retval = usb_autopm_get_interface_async(dev->intf);
115269ee472fSOliver Neukum 	if (retval < 0) {
115369ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
115469ee472fSOliver Neukum 		goto drop;
115569ee472fSOliver Neukum 	}
115669ee472fSOliver Neukum 
115769ee472fSOliver Neukum #ifdef CONFIG_PM
115869ee472fSOliver Neukum 	/* if this triggers the device is still a sleep */
115969ee472fSOliver Neukum 	if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
116069ee472fSOliver Neukum 		/* transmission will be done in resume */
116169ee472fSOliver Neukum 		usb_anchor_urb(urb, &dev->deferred);
116269ee472fSOliver Neukum 		/* no use to process more packets */
116369ee472fSOliver Neukum 		netif_stop_queue(net);
116469ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
116560b86755SJoe Perches 		netdev_dbg(dev->net, "Delaying transmission for resumption\n");
116669ee472fSOliver Neukum 		goto deferred;
116769ee472fSOliver Neukum 	}
116869ee472fSOliver Neukum #endif
11695b2fc499SJeff Garzik 
11705b2fc499SJeff Garzik 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
11715b2fc499SJeff Garzik 	case -EPIPE:
11725b2fc499SJeff Garzik 		netif_stop_queue (net);
11735b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
117469ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
11755b2fc499SJeff Garzik 		break;
11765b2fc499SJeff Garzik 	default:
117769ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
1178a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net,
1179a475f603SJoe Perches 			  "tx: submit urb err %d\n", retval);
11805b2fc499SJeff Garzik 		break;
11815b2fc499SJeff Garzik 	case 0:
11825b2fc499SJeff Garzik 		net->trans_start = jiffies;
11835b6e9bcdSMing Lei 		__usbnet_queue_skb(&dev->txq, skb, tx_start);
11845b2fc499SJeff Garzik 		if (dev->txq.qlen >= TX_QLEN (dev))
11855b2fc499SJeff Garzik 			netif_stop_queue (net);
11865b2fc499SJeff Garzik 	}
11875b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->txq.lock, flags);
11885b2fc499SJeff Garzik 
11895b2fc499SJeff Garzik 	if (retval) {
1190a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
11915b2fc499SJeff Garzik drop:
11927963837fSHerbert Xu 		dev->net->stats.tx_dropped++;
1193073285fdSAlexey Orishko not_drop:
11945b2fc499SJeff Garzik 		if (skb)
11955b2fc499SJeff Garzik 			dev_kfree_skb_any (skb);
11965b2fc499SJeff Garzik 		usb_free_urb (urb);
1197a475f603SJoe Perches 	} else
1198a475f603SJoe Perches 		netif_dbg(dev, tx_queued, dev->net,
1199a475f603SJoe Perches 			  "> tx, len %d, type 0x%x\n", length, skb->protocol);
120069ee472fSOliver Neukum #ifdef CONFIG_PM
120169ee472fSOliver Neukum deferred:
120269ee472fSOliver Neukum #endif
120325a79c41SStephen Hemminger 	return NETDEV_TX_OK;
12045b2fc499SJeff Garzik }
1205777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit);
12065b2fc499SJeff Garzik 
12075b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12085b2fc499SJeff Garzik 
12095b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
12105b2fc499SJeff Garzik 
12115b2fc499SJeff Garzik static void usbnet_bh (unsigned long param)
12125b2fc499SJeff Garzik {
12135b2fc499SJeff Garzik 	struct usbnet		*dev = (struct usbnet *) param;
12145b2fc499SJeff Garzik 	struct sk_buff		*skb;
12155b2fc499SJeff Garzik 	struct skb_data		*entry;
12165b2fc499SJeff Garzik 
12175b2fc499SJeff Garzik 	while ((skb = skb_dequeue (&dev->done))) {
12185b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
12195b2fc499SJeff Garzik 		switch (entry->state) {
12205b2fc499SJeff Garzik 		case rx_done:
12215b2fc499SJeff Garzik 			entry->state = rx_cleanup;
12225b2fc499SJeff Garzik 			rx_process (dev, skb);
12235b2fc499SJeff Garzik 			continue;
12245b2fc499SJeff Garzik 		case tx_done:
12255b2fc499SJeff Garzik 		case rx_cleanup:
12265b2fc499SJeff Garzik 			usb_free_urb (entry->urb);
12275b2fc499SJeff Garzik 			dev_kfree_skb (skb);
12285b2fc499SJeff Garzik 			continue;
12295b2fc499SJeff Garzik 		default:
123060b86755SJoe Perches 			netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
12315b2fc499SJeff Garzik 		}
12325b2fc499SJeff Garzik 	}
12335b2fc499SJeff Garzik 
12345b2fc499SJeff Garzik 	// waiting for all pending urbs to complete?
12355b2fc499SJeff Garzik 	if (dev->wait) {
12365b2fc499SJeff Garzik 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
12375b2fc499SJeff Garzik 			wake_up (dev->wait);
12385b2fc499SJeff Garzik 		}
12395b2fc499SJeff Garzik 
12405b2fc499SJeff Garzik 	// or are we maybe short a few urbs?
12418e95a202SJoe Perches 	} else if (netif_running (dev->net) &&
12428e95a202SJoe Perches 		   netif_device_present (dev->net) &&
12438e95a202SJoe Perches 		   !timer_pending (&dev->delay) &&
12448e95a202SJoe Perches 		   !test_bit (EVENT_RX_HALT, &dev->flags)) {
12455b2fc499SJeff Garzik 		int	temp = dev->rxq.qlen;
12465b2fc499SJeff Garzik 		int	qlen = RX_QLEN (dev);
12475b2fc499SJeff Garzik 
12485b2fc499SJeff Garzik 		if (temp < qlen) {
12495b2fc499SJeff Garzik 			struct urb	*urb;
12505b2fc499SJeff Garzik 			int		i;
12515b2fc499SJeff Garzik 
12525b2fc499SJeff Garzik 			// don't refill the queue all at once
12535b2fc499SJeff Garzik 			for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
12545b2fc499SJeff Garzik 				urb = usb_alloc_urb (0, GFP_ATOMIC);
1255dacb3975SDavid S. Miller 				if (urb != NULL) {
1256dacb3975SDavid S. Miller 					if (rx_submit (dev, urb, GFP_ATOMIC) ==
1257dacb3975SDavid S. Miller 					    -ENOLINK)
1258dacb3975SDavid S. Miller 						return;
1259dacb3975SDavid S. Miller 				}
12605b2fc499SJeff Garzik 			}
1261a475f603SJoe Perches 			if (temp != dev->rxq.qlen)
1262a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1263a475f603SJoe Perches 					  "rxqlen %d --> %d\n",
12645b2fc499SJeff Garzik 					  temp, dev->rxq.qlen);
12655b2fc499SJeff Garzik 			if (dev->rxq.qlen < qlen)
12665b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
12675b2fc499SJeff Garzik 		}
12685b2fc499SJeff Garzik 		if (dev->txq.qlen < TX_QLEN (dev))
12695b2fc499SJeff Garzik 			netif_wake_queue (dev->net);
12705b2fc499SJeff Garzik 	}
12715b2fc499SJeff Garzik }
12725b2fc499SJeff Garzik 
12735b2fc499SJeff Garzik 
12745b2fc499SJeff Garzik /*-------------------------------------------------------------------------
12755b2fc499SJeff Garzik  *
12765b2fc499SJeff Garzik  * USB Device Driver support
12775b2fc499SJeff Garzik  *
12785b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
12795b2fc499SJeff Garzik 
12805b2fc499SJeff Garzik // precondition: never called in_interrupt
12815b2fc499SJeff Garzik 
12825b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
12835b2fc499SJeff Garzik {
12845b2fc499SJeff Garzik 	struct usbnet		*dev;
12855b2fc499SJeff Garzik 	struct usb_device	*xdev;
12865b2fc499SJeff Garzik 	struct net_device	*net;
12875b2fc499SJeff Garzik 
12885b2fc499SJeff Garzik 	dev = usb_get_intfdata(intf);
12895b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
12905b2fc499SJeff Garzik 	if (!dev)
12915b2fc499SJeff Garzik 		return;
12925b2fc499SJeff Garzik 
12935b2fc499SJeff Garzik 	xdev = interface_to_usbdev (intf);
12945b2fc499SJeff Garzik 
1295a475f603SJoe Perches 	netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
12965b2fc499SJeff Garzik 		   intf->dev.driver->name,
12975b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
12985b2fc499SJeff Garzik 		   dev->driver_info->description);
12995b2fc499SJeff Garzik 
13005b2fc499SJeff Garzik 	net = dev->net;
13015b2fc499SJeff Garzik 	unregister_netdev (net);
13025b2fc499SJeff Garzik 
130323f333a2STejun Heo 	cancel_work_sync(&dev->kevent);
13045b2fc499SJeff Garzik 
13055b2fc499SJeff Garzik 	if (dev->driver_info->unbind)
13065b2fc499SJeff Garzik 		dev->driver_info->unbind (dev, intf);
13075b2fc499SJeff Garzik 
130868972efaSPaul Stewart 	usb_kill_urb(dev->interrupt);
130968972efaSPaul Stewart 	usb_free_urb(dev->interrupt);
131068972efaSPaul Stewart 
13115b2fc499SJeff Garzik 	free_netdev(net);
13125b2fc499SJeff Garzik 	usb_put_dev (xdev);
13135b2fc499SJeff Garzik }
13145b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
13155b2fc499SJeff Garzik 
1316777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = {
1317777baa47SStephen Hemminger 	.ndo_open		= usbnet_open,
1318777baa47SStephen Hemminger 	.ndo_stop		= usbnet_stop,
1319777baa47SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
1320777baa47SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
1321777baa47SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1322777baa47SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1323777baa47SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1324777baa47SStephen Hemminger };
13255b2fc499SJeff Garzik 
13265b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
13275b2fc499SJeff Garzik 
13285b2fc499SJeff Garzik // precondition: never called in_interrupt
13295b2fc499SJeff Garzik 
1330225794f8SMarcel Holtmann static struct device_type wlan_type = {
1331225794f8SMarcel Holtmann 	.name	= "wlan",
1332225794f8SMarcel Holtmann };
1333225794f8SMarcel Holtmann 
1334225794f8SMarcel Holtmann static struct device_type wwan_type = {
1335225794f8SMarcel Holtmann 	.name	= "wwan",
1336225794f8SMarcel Holtmann };
1337225794f8SMarcel Holtmann 
13385b2fc499SJeff Garzik int
13395b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
13405b2fc499SJeff Garzik {
13415b2fc499SJeff Garzik 	struct usbnet			*dev;
13425b2fc499SJeff Garzik 	struct net_device		*net;
13435b2fc499SJeff Garzik 	struct usb_host_interface	*interface;
13445b2fc499SJeff Garzik 	struct driver_info		*info;
13455b2fc499SJeff Garzik 	struct usb_device		*xdev;
13465b2fc499SJeff Garzik 	int				status;
13475b2fc499SJeff Garzik 	const char			*name;
1348b0786b43SMing Lei 	struct usb_driver 	*driver = to_usb_driver(udev->dev.driver);
1349b0786b43SMing Lei 
1350b0786b43SMing Lei 	/* usbnet already took usb runtime pm, so have to enable the feature
1351b0786b43SMing Lei 	 * for usb interface, otherwise usb_autopm_get_interface may return
1352b0786b43SMing Lei 	 * failure if USB_SUSPEND(RUNTIME_PM) is enabled.
1353b0786b43SMing Lei 	 */
1354b0786b43SMing Lei 	if (!driver->supports_autosuspend) {
1355b0786b43SMing Lei 		driver->supports_autosuspend = 1;
1356b0786b43SMing Lei 		pm_runtime_enable(&udev->dev);
1357b0786b43SMing Lei 	}
13585b2fc499SJeff Garzik 
13595b2fc499SJeff Garzik 	name = udev->dev.driver->name;
13605b2fc499SJeff Garzik 	info = (struct driver_info *) prod->driver_info;
13615b2fc499SJeff Garzik 	if (!info) {
13625b2fc499SJeff Garzik 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
13635b2fc499SJeff Garzik 		return -ENODEV;
13645b2fc499SJeff Garzik 	}
13655b2fc499SJeff Garzik 	xdev = interface_to_usbdev (udev);
13665b2fc499SJeff Garzik 	interface = udev->cur_altsetting;
13675b2fc499SJeff Garzik 
13685b2fc499SJeff Garzik 	usb_get_dev (xdev);
13695b2fc499SJeff Garzik 
13705b2fc499SJeff Garzik 	status = -ENOMEM;
13715b2fc499SJeff Garzik 
13725b2fc499SJeff Garzik 	// set up our own records
13735b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(*dev));
137441de8d4cSJoe Perches 	if (!net)
13755b2fc499SJeff Garzik 		goto out;
13765b2fc499SJeff Garzik 
13770dacca73SBen Hutchings 	/* netdev_printk() needs this so do it as early as possible */
13780dacca73SBen Hutchings 	SET_NETDEV_DEV(net, &udev->dev);
13790dacca73SBen Hutchings 
13805b2fc499SJeff Garzik 	dev = netdev_priv(net);
13815b2fc499SJeff Garzik 	dev->udev = xdev;
1382a11a6544SOliver Neukum 	dev->intf = udev;
13835b2fc499SJeff Garzik 	dev->driver_info = info;
13845b2fc499SJeff Garzik 	dev->driver_name = name;
13855b2fc499SJeff Garzik 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
13865b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
13875b2fc499SJeff Garzik 	skb_queue_head_init (&dev->rxq);
13885b2fc499SJeff Garzik 	skb_queue_head_init (&dev->txq);
13895b2fc499SJeff Garzik 	skb_queue_head_init (&dev->done);
13907834ddbcSJussi Kivilinna 	skb_queue_head_init(&dev->rxq_pause);
13915b2fc499SJeff Garzik 	dev->bh.func = usbnet_bh;
13925b2fc499SJeff Garzik 	dev->bh.data = (unsigned long) dev;
13935b2fc499SJeff Garzik 	INIT_WORK (&dev->kevent, kevent);
139469ee472fSOliver Neukum 	init_usb_anchor(&dev->deferred);
13955b2fc499SJeff Garzik 	dev->delay.function = usbnet_bh;
13965b2fc499SJeff Garzik 	dev->delay.data = (unsigned long) dev;
13975b2fc499SJeff Garzik 	init_timer (&dev->delay);
13985b2fc499SJeff Garzik 	mutex_init (&dev->phy_mutex);
13995b2fc499SJeff Garzik 
14005b2fc499SJeff Garzik 	dev->net = net;
14015b2fc499SJeff Garzik 	strcpy (net->name, "usb%d");
14025b2fc499SJeff Garzik 	memcpy (net->dev_addr, node_id, sizeof node_id);
14035b2fc499SJeff Garzik 
14045b2fc499SJeff Garzik 	/* rx and tx sides can use different message sizes;
14055b2fc499SJeff Garzik 	 * bind() should set rx_urb_size in that case.
14065b2fc499SJeff Garzik 	 */
14075b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
14085b2fc499SJeff Garzik #if 0
14095b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures
14105b2fc499SJeff Garzik 	// possible with some EHCI controllers
14116a35528aSYang Hongyang 	if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
14125b2fc499SJeff Garzik 		net->features |= NETIF_F_HIGHDMA;
14135b2fc499SJeff Garzik #endif
14145b2fc499SJeff Garzik 
1415777baa47SStephen Hemminger 	net->netdev_ops = &usbnet_netdev_ops;
1416777baa47SStephen Hemminger 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
14175b2fc499SJeff Garzik 	net->ethtool_ops = &usbnet_ethtool_ops;
14185b2fc499SJeff Garzik 
14195b2fc499SJeff Garzik 	// allow device-specific bind/init procedures
14205b2fc499SJeff Garzik 	// NOTE net->name still not usable ...
14215b2fc499SJeff Garzik 	if (info->bind) {
14225b2fc499SJeff Garzik 		status = info->bind (dev, udev);
14235b2fc499SJeff Garzik 		if (status < 0)
14245b2fc499SJeff Garzik 			goto out1;
14255b2fc499SJeff Garzik 
14265b2fc499SJeff Garzik 		// heuristic:  "usb%d" for links we know are two-host,
14275b2fc499SJeff Garzik 		// else "eth%d" when there's reasonable doubt.  userspace
14285b2fc499SJeff Garzik 		// can rename the link if it knows better.
14298e95a202SJoe Perches 		if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1430c261344dSArnd Bergmann 		    ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1431c261344dSArnd Bergmann 		     (net->dev_addr [0] & 0x02) == 0))
14325b2fc499SJeff Garzik 			strcpy (net->name, "eth%d");
14336e3bbcc5SJussi Kivilinna 		/* WLAN devices should always be named "wlan%d" */
14346e3bbcc5SJussi Kivilinna 		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
14356e3bbcc5SJussi Kivilinna 			strcpy(net->name, "wlan%d");
1436e1e499eeSMarcel Holtmann 		/* WWAN devices should always be named "wwan%d" */
1437e1e499eeSMarcel Holtmann 		if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1438e1e499eeSMarcel Holtmann 			strcpy(net->name, "wwan%d");
14395b2fc499SJeff Garzik 
14405b2fc499SJeff Garzik 		/* maybe the remote can't receive an Ethernet MTU */
14415b2fc499SJeff Garzik 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
14425b2fc499SJeff Garzik 			net->mtu = dev->hard_mtu - net->hard_header_len;
14435b2fc499SJeff Garzik 	} else if (!info->in || !info->out)
14445b2fc499SJeff Garzik 		status = usbnet_get_endpoints (dev, udev);
14455b2fc499SJeff Garzik 	else {
14465b2fc499SJeff Garzik 		dev->in = usb_rcvbulkpipe (xdev, info->in);
14475b2fc499SJeff Garzik 		dev->out = usb_sndbulkpipe (xdev, info->out);
14485b2fc499SJeff Garzik 		if (!(info->flags & FLAG_NO_SETINT))
14495b2fc499SJeff Garzik 			status = usb_set_interface (xdev,
14505b2fc499SJeff Garzik 				interface->desc.bInterfaceNumber,
14515b2fc499SJeff Garzik 				interface->desc.bAlternateSetting);
14525b2fc499SJeff Garzik 		else
14535b2fc499SJeff Garzik 			status = 0;
14545b2fc499SJeff Garzik 
14555b2fc499SJeff Garzik 	}
14569514bfe5SPeter Korsgaard 	if (status >= 0 && dev->status)
14575b2fc499SJeff Garzik 		status = init_status (dev, udev);
14585b2fc499SJeff Garzik 	if (status < 0)
14595b2fc499SJeff Garzik 		goto out3;
14605b2fc499SJeff Garzik 
14615b2fc499SJeff Garzik 	if (!dev->rx_urb_size)
14625b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
14635b2fc499SJeff Garzik 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
14645b2fc499SJeff Garzik 
1465225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1466225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wlan_type);
1467225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1468225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wwan_type);
1469225794f8SMarcel Holtmann 
14705b2fc499SJeff Garzik 	status = register_netdev (net);
14715b2fc499SJeff Garzik 	if (status)
1472a4723848Stom.leiming@gmail.com 		goto out4;
1473a475f603SJoe Perches 	netif_info(dev, probe, dev->net,
1474a475f603SJoe Perches 		   "register '%s' at usb-%s-%s, %s, %pM\n",
14755b2fc499SJeff Garzik 		   udev->dev.driver->name,
14765b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
14775b2fc499SJeff Garzik 		   dev->driver_info->description,
1478e174961cSJohannes Berg 		   net->dev_addr);
14795b2fc499SJeff Garzik 
14805b2fc499SJeff Garzik 	// ok, it's ready to go.
14815b2fc499SJeff Garzik 	usb_set_intfdata (udev, dev);
14825b2fc499SJeff Garzik 
14835b2fc499SJeff Garzik 	netif_device_attach (net);
14845b2fc499SJeff Garzik 
148537e8273cSBen Hutchings 	if (dev->driver_info->flags & FLAG_LINK_INTR)
148637e8273cSBen Hutchings 		netif_carrier_off(net);
148737e8273cSBen Hutchings 
14885b2fc499SJeff Garzik 	return 0;
14895b2fc499SJeff Garzik 
1490a4723848Stom.leiming@gmail.com out4:
1491a4723848Stom.leiming@gmail.com 	usb_free_urb(dev->interrupt);
14925b2fc499SJeff Garzik out3:
14935b2fc499SJeff Garzik 	if (info->unbind)
14945b2fc499SJeff Garzik 		info->unbind (dev, udev);
14955b2fc499SJeff Garzik out1:
14965b2fc499SJeff Garzik 	free_netdev(net);
14975b2fc499SJeff Garzik out:
14985b2fc499SJeff Garzik 	usb_put_dev(xdev);
14995b2fc499SJeff Garzik 	return status;
15005b2fc499SJeff Garzik }
15015b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
15025b2fc499SJeff Garzik 
15035b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
15045b2fc499SJeff Garzik 
150536433127SOliver Neukum /*
150636433127SOliver Neukum  * suspend the whole driver as soon as the first interface is suspended
150736433127SOliver Neukum  * resume only when the last interface is resumed
15085b2fc499SJeff Garzik  */
15095b2fc499SJeff Garzik 
15105b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
15115b2fc499SJeff Garzik {
15125b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
15135b2fc499SJeff Garzik 
151436433127SOliver Neukum 	if (!dev->suspend_count++) {
151569ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
151669ee472fSOliver Neukum 		/* don't autosuspend while transmitting */
15175b1b0b81SAlan Stern 		if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
1518*5eeb3132SMing Lei 			dev->suspend_count--;
151969ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
152069ee472fSOliver Neukum 			return -EBUSY;
152169ee472fSOliver Neukum 		} else {
152269ee472fSOliver Neukum 			set_bit(EVENT_DEV_ASLEEP, &dev->flags);
152369ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
152469ee472fSOliver Neukum 		}
1525a11a6544SOliver Neukum 		/*
1526a11a6544SOliver Neukum 		 * accelerate emptying of the rx and queues, to avoid
15275b2fc499SJeff Garzik 		 * having everything error out.
15285b2fc499SJeff Garzik 		 */
15295b2fc499SJeff Garzik 		netif_device_detach (dev->net);
153069ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
153169ee472fSOliver Neukum 		usb_kill_urb(dev->interrupt);
153269ee472fSOliver Neukum 
1533a11a6544SOliver Neukum 		/*
1534a11a6544SOliver Neukum 		 * reattach so runtime management can use and
1535a11a6544SOliver Neukum 		 * wake the device
1536a11a6544SOliver Neukum 		 */
1537a11a6544SOliver Neukum 		netif_device_attach (dev->net);
153836433127SOliver Neukum 	}
15395b2fc499SJeff Garzik 	return 0;
15405b2fc499SJeff Garzik }
15415b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
15425b2fc499SJeff Garzik 
15435b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
15445b2fc499SJeff Garzik {
15455b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
154669ee472fSOliver Neukum 	struct sk_buff          *skb;
154769ee472fSOliver Neukum 	struct urb              *res;
154869ee472fSOliver Neukum 	int                     retval;
15495b2fc499SJeff Garzik 
155069ee472fSOliver Neukum 	if (!--dev->suspend_count) {
155168972efaSPaul Stewart 		/* resume interrupt URBs */
155268972efaSPaul Stewart 		if (dev->interrupt && test_bit(EVENT_DEV_OPEN, &dev->flags))
155368972efaSPaul Stewart 			usb_submit_urb(dev->interrupt, GFP_NOIO);
155468972efaSPaul Stewart 
155569ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
155669ee472fSOliver Neukum 		while ((res = usb_get_from_anchor(&dev->deferred))) {
155769ee472fSOliver Neukum 
155869ee472fSOliver Neukum 			skb = (struct sk_buff *)res->context;
155969ee472fSOliver Neukum 			retval = usb_submit_urb(res, GFP_ATOMIC);
156069ee472fSOliver Neukum 			if (retval < 0) {
156169ee472fSOliver Neukum 				dev_kfree_skb_any(skb);
156269ee472fSOliver Neukum 				usb_free_urb(res);
156369ee472fSOliver Neukum 				usb_autopm_put_interface_async(dev->intf);
156469ee472fSOliver Neukum 			} else {
156569ee472fSOliver Neukum 				dev->net->trans_start = jiffies;
156669ee472fSOliver Neukum 				__skb_queue_tail(&dev->txq, skb);
156769ee472fSOliver Neukum 			}
156869ee472fSOliver Neukum 		}
156969ee472fSOliver Neukum 
157069ee472fSOliver Neukum 		smp_mb();
157169ee472fSOliver Neukum 		clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
157269ee472fSOliver Neukum 		spin_unlock_irq(&dev->txq.lock);
157375bd0cbdSMing Lei 
157475bd0cbdSMing Lei 		if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
157569ee472fSOliver Neukum 			if (!(dev->txq.qlen >= TX_QLEN(dev)))
15761aa9bc5bSAlexey Orishko 				netif_tx_wake_all_queues(dev->net);
15775b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
157869ee472fSOliver Neukum 		}
157975bd0cbdSMing Lei 	}
15805b2fc499SJeff Garzik 	return 0;
15815b2fc499SJeff Garzik }
15825b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
15835b2fc499SJeff Garzik 
15845b2fc499SJeff Garzik 
15855b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
15865b2fc499SJeff Garzik 
15875b2fc499SJeff Garzik static int __init usbnet_init(void)
15885b2fc499SJeff Garzik {
1589c582a950SThiago Farina 	/* Compiler should optimize this out. */
1590c582a950SThiago Farina 	BUILD_BUG_ON(
1591c582a950SThiago Farina 		FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
15925b2fc499SJeff Garzik 
15935b2fc499SJeff Garzik 	random_ether_addr(node_id);
15945b2fc499SJeff Garzik 	return 0;
15955b2fc499SJeff Garzik }
15965b2fc499SJeff Garzik module_init(usbnet_init);
15975b2fc499SJeff Garzik 
15985b2fc499SJeff Garzik static void __exit usbnet_exit(void)
15995b2fc499SJeff Garzik {
16005b2fc499SJeff Garzik }
16015b2fc499SJeff Garzik module_exit(usbnet_exit);
16025b2fc499SJeff Garzik 
16035b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
16045b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
16055b2fc499SJeff Garzik MODULE_LICENSE("GPL");
1606