xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision 0dacca73a3ddefa6cb8a7e0282f938e01faa1a64)
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>
475b2fc499SJeff Garzik 
485b2fc499SJeff Garzik #define DRIVER_VERSION		"22-Aug-2005"
495b2fc499SJeff Garzik 
505b2fc499SJeff Garzik 
515b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
525b2fc499SJeff Garzik 
535b2fc499SJeff Garzik /*
545b2fc499SJeff Garzik  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
555b2fc499SJeff Garzik  * Several dozen bytes of IPv4 data can fit in two such transactions.
565b2fc499SJeff Garzik  * One maximum size Ethernet packet takes twenty four of them.
575b2fc499SJeff Garzik  * For high speed, each frame comfortably fits almost 36 max size
585b2fc499SJeff Garzik  * Ethernet packets (so queues should be bigger).
595b2fc499SJeff Garzik  *
605b2fc499SJeff Garzik  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
615b2fc499SJeff Garzik  * let the USB host controller be busy for 5msec or more before an irq
625b2fc499SJeff Garzik  * is required, under load.  Jumbograms change the equation.
635b2fc499SJeff Garzik  */
645b2fc499SJeff Garzik #define RX_MAX_QUEUE_MEMORY (60 * 1518)
655b2fc499SJeff Garzik #define	RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
665b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
675b2fc499SJeff Garzik #define	TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
685b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
695b2fc499SJeff Garzik 
705b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
715b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES	(5*HZ)
725b2fc499SJeff Garzik 
735b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect()
745b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors.
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 static const char driver_name [] = "usbnet";
865b2fc499SJeff Garzik 
875b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
885b2fc499SJeff Garzik static int msg_level = -1;
895b2fc499SJeff Garzik module_param (msg_level, int, 0);
905b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
915b2fc499SJeff Garzik 
925b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
935b2fc499SJeff Garzik 
945b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
955b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
965b2fc499SJeff Garzik {
975b2fc499SJeff Garzik 	int				tmp;
985b2fc499SJeff Garzik 	struct usb_host_interface	*alt = NULL;
995b2fc499SJeff Garzik 	struct usb_host_endpoint	*in = NULL, *out = NULL;
1005b2fc499SJeff Garzik 	struct usb_host_endpoint	*status = NULL;
1015b2fc499SJeff Garzik 
1025b2fc499SJeff Garzik 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
1035b2fc499SJeff Garzik 		unsigned	ep;
1045b2fc499SJeff Garzik 
1055b2fc499SJeff Garzik 		in = out = status = NULL;
1065b2fc499SJeff Garzik 		alt = intf->altsetting + tmp;
1075b2fc499SJeff Garzik 
1085b2fc499SJeff Garzik 		/* take the first altsetting with in-bulk + out-bulk;
1095b2fc499SJeff Garzik 		 * remember any status endpoint, just in case;
1105b2fc499SJeff Garzik 		 * ignore other endpoints and altsetttings.
1115b2fc499SJeff Garzik 		 */
1125b2fc499SJeff Garzik 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
1135b2fc499SJeff Garzik 			struct usb_host_endpoint	*e;
1145b2fc499SJeff Garzik 			int				intr = 0;
1155b2fc499SJeff Garzik 
1165b2fc499SJeff Garzik 			e = alt->endpoint + ep;
1175b2fc499SJeff Garzik 			switch (e->desc.bmAttributes) {
1185b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_INT:
1195b2fc499SJeff Garzik 				if (!usb_endpoint_dir_in(&e->desc))
1205b2fc499SJeff Garzik 					continue;
1215b2fc499SJeff Garzik 				intr = 1;
1225b2fc499SJeff Garzik 				/* FALLTHROUGH */
1235b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_BULK:
1245b2fc499SJeff Garzik 				break;
1255b2fc499SJeff Garzik 			default:
1265b2fc499SJeff Garzik 				continue;
1275b2fc499SJeff Garzik 			}
1285b2fc499SJeff Garzik 			if (usb_endpoint_dir_in(&e->desc)) {
1295b2fc499SJeff Garzik 				if (!intr && !in)
1305b2fc499SJeff Garzik 					in = e;
1315b2fc499SJeff Garzik 				else if (intr && !status)
1325b2fc499SJeff Garzik 					status = e;
1335b2fc499SJeff Garzik 			} else {
1345b2fc499SJeff Garzik 				if (!out)
1355b2fc499SJeff Garzik 					out = e;
1365b2fc499SJeff Garzik 			}
1375b2fc499SJeff Garzik 		}
1385b2fc499SJeff Garzik 		if (in && out)
1395b2fc499SJeff Garzik 			break;
1405b2fc499SJeff Garzik 	}
1415b2fc499SJeff Garzik 	if (!alt || !in || !out)
1425b2fc499SJeff Garzik 		return -EINVAL;
1435b2fc499SJeff Garzik 
1448e95a202SJoe Perches 	if (alt->desc.bAlternateSetting != 0 ||
1458e95a202SJoe Perches 	    !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1465b2fc499SJeff Garzik 		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
1475b2fc499SJeff Garzik 				alt->desc.bAlternateSetting);
1485b2fc499SJeff Garzik 		if (tmp < 0)
1495b2fc499SJeff Garzik 			return tmp;
1505b2fc499SJeff Garzik 	}
1515b2fc499SJeff Garzik 
1525b2fc499SJeff Garzik 	dev->in = usb_rcvbulkpipe (dev->udev,
1535b2fc499SJeff Garzik 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1545b2fc499SJeff Garzik 	dev->out = usb_sndbulkpipe (dev->udev,
1555b2fc499SJeff Garzik 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1565b2fc499SJeff Garzik 	dev->status = status;
1575b2fc499SJeff Garzik 	return 0;
1585b2fc499SJeff Garzik }
1595b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1605b2fc499SJeff Garzik 
16103ad032bSPeter Holik static u8 nibble(unsigned char c)
16203ad032bSPeter Holik {
16303ad032bSPeter Holik 	if (likely(isdigit(c)))
16403ad032bSPeter Holik 		return c - '0';
16503ad032bSPeter Holik 	c = toupper(c);
16603ad032bSPeter Holik 	if (likely(isxdigit(c)))
16703ad032bSPeter Holik 		return 10 + c - 'A';
16803ad032bSPeter Holik 	return 0;
16903ad032bSPeter Holik }
17003ad032bSPeter Holik 
17103ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
17203ad032bSPeter Holik {
17303ad032bSPeter Holik 	int 		tmp, i;
17403ad032bSPeter Holik 	unsigned char	buf [13];
17503ad032bSPeter Holik 
17603ad032bSPeter Holik 	tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
17703ad032bSPeter Holik 	if (tmp != 12) {
17803ad032bSPeter Holik 		dev_dbg(&dev->udev->dev,
17903ad032bSPeter Holik 			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
18003ad032bSPeter Holik 		if (tmp >= 0)
18103ad032bSPeter Holik 			tmp = -EINVAL;
18203ad032bSPeter Holik 		return tmp;
18303ad032bSPeter Holik 	}
18403ad032bSPeter Holik 	for (i = tmp = 0; i < 6; i++, tmp += 2)
18503ad032bSPeter Holik 		dev->net->dev_addr [i] =
18603ad032bSPeter Holik 			(nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
18703ad032bSPeter Holik 	return 0;
18803ad032bSPeter Holik }
18903ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
19003ad032bSPeter Holik 
1915b2fc499SJeff Garzik static void intr_complete (struct urb *urb);
1925b2fc499SJeff Garzik 
1935b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
1945b2fc499SJeff Garzik {
1955b2fc499SJeff Garzik 	char		*buf = NULL;
1965b2fc499SJeff Garzik 	unsigned	pipe = 0;
1975b2fc499SJeff Garzik 	unsigned	maxp;
1985b2fc499SJeff Garzik 	unsigned	period;
1995b2fc499SJeff Garzik 
2005b2fc499SJeff Garzik 	if (!dev->driver_info->status)
2015b2fc499SJeff Garzik 		return 0;
2025b2fc499SJeff Garzik 
2035b2fc499SJeff Garzik 	pipe = usb_rcvintpipe (dev->udev,
2045b2fc499SJeff Garzik 			dev->status->desc.bEndpointAddress
2055b2fc499SJeff Garzik 				& USB_ENDPOINT_NUMBER_MASK);
2065b2fc499SJeff Garzik 	maxp = usb_maxpacket (dev->udev, pipe, 0);
2075b2fc499SJeff Garzik 
2085b2fc499SJeff Garzik 	/* avoid 1 msec chatter:  min 8 msec poll rate */
2095b2fc499SJeff Garzik 	period = max ((int) dev->status->desc.bInterval,
2105b2fc499SJeff Garzik 		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
2115b2fc499SJeff Garzik 
2125b2fc499SJeff Garzik 	buf = kmalloc (maxp, GFP_KERNEL);
2135b2fc499SJeff Garzik 	if (buf) {
2145b2fc499SJeff Garzik 		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
2155b2fc499SJeff Garzik 		if (!dev->interrupt) {
2165b2fc499SJeff Garzik 			kfree (buf);
2175b2fc499SJeff Garzik 			return -ENOMEM;
2185b2fc499SJeff Garzik 		} else {
2195b2fc499SJeff Garzik 			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
2205b2fc499SJeff Garzik 				buf, maxp, intr_complete, dev, period);
2215b2fc499SJeff Garzik 			dev_dbg(&intf->dev,
2225b2fc499SJeff Garzik 				"status ep%din, %d bytes period %d\n",
2235b2fc499SJeff Garzik 				usb_pipeendpoint(pipe), maxp, period);
2245b2fc499SJeff Garzik 		}
2255b2fc499SJeff Garzik 	}
2265b2fc499SJeff Garzik 	return 0;
2275b2fc499SJeff Garzik }
2285b2fc499SJeff Garzik 
2295b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
2305b2fc499SJeff Garzik  * Some link protocols batch packets, so their rx_fixup paths
2315b2fc499SJeff Garzik  * can return clones as well as just modify the original skb.
2325b2fc499SJeff Garzik  */
2335b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
2345b2fc499SJeff Garzik {
2355b2fc499SJeff Garzik 	int	status;
2365b2fc499SJeff Garzik 
2377834ddbcSJussi Kivilinna 	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
2387834ddbcSJussi Kivilinna 		skb_queue_tail(&dev->rxq_pause, skb);
2397834ddbcSJussi Kivilinna 		return;
2407834ddbcSJussi Kivilinna 	}
2417834ddbcSJussi Kivilinna 
2425b2fc499SJeff Garzik 	skb->protocol = eth_type_trans (skb, dev->net);
2437963837fSHerbert Xu 	dev->net->stats.rx_packets++;
2447963837fSHerbert Xu 	dev->net->stats.rx_bytes += skb->len;
2455b2fc499SJeff Garzik 
246a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
2475b2fc499SJeff Garzik 		  skb->len + sizeof (struct ethhdr), skb->protocol);
2485b2fc499SJeff Garzik 	memset (skb->cb, 0, sizeof (struct skb_data));
2495b2fc499SJeff Garzik 	status = netif_rx (skb);
250a475f603SJoe Perches 	if (status != NET_RX_SUCCESS)
251a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net,
252a475f603SJoe Perches 			  "netif_rx status %d\n", status);
2535b2fc499SJeff Garzik }
2545b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
2555b2fc499SJeff Garzik 
2565b2fc499SJeff Garzik 
2575b2fc499SJeff Garzik /*-------------------------------------------------------------------------
2585b2fc499SJeff Garzik  *
2595b2fc499SJeff Garzik  * Network Device Driver (peer link to "Host Device", from USB host)
2605b2fc499SJeff Garzik  *
2615b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
2625b2fc499SJeff Garzik 
263777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu)
2645b2fc499SJeff Garzik {
2655b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
2665b2fc499SJeff Garzik 	int		ll_mtu = new_mtu + net->hard_header_len;
2675b2fc499SJeff Garzik 	int		old_hard_mtu = dev->hard_mtu;
2685b2fc499SJeff Garzik 	int		old_rx_urb_size = dev->rx_urb_size;
2695b2fc499SJeff Garzik 
2705b2fc499SJeff Garzik 	if (new_mtu <= 0)
2715b2fc499SJeff Garzik 		return -EINVAL;
2725b2fc499SJeff Garzik 	// no second zero-length packet read wanted after mtu-sized packets
2735b2fc499SJeff Garzik 	if ((ll_mtu % dev->maxpacket) == 0)
2745b2fc499SJeff Garzik 		return -EDOM;
2755b2fc499SJeff Garzik 	net->mtu = new_mtu;
2765b2fc499SJeff Garzik 
2775b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
2785b2fc499SJeff Garzik 	if (dev->rx_urb_size == old_hard_mtu) {
2795b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
2805b2fc499SJeff Garzik 		if (dev->rx_urb_size > old_rx_urb_size)
2815b2fc499SJeff Garzik 			usbnet_unlink_rx_urbs(dev);
2825b2fc499SJeff Garzik 	}
2835b2fc499SJeff Garzik 
2845b2fc499SJeff Garzik 	return 0;
2855b2fc499SJeff Garzik }
286777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu);
2875b2fc499SJeff Garzik 
2885b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
2895b2fc499SJeff Garzik 
2905b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
2915b2fc499SJeff Garzik  * completion callbacks.  2.5 should have fixed those bugs...
2925b2fc499SJeff Garzik  */
2935b2fc499SJeff Garzik 
2945b2fc499SJeff Garzik static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
2955b2fc499SJeff Garzik {
2965b2fc499SJeff Garzik 	unsigned long		flags;
2975b2fc499SJeff Garzik 
2985b2fc499SJeff Garzik 	spin_lock_irqsave(&list->lock, flags);
2995b2fc499SJeff Garzik 	__skb_unlink(skb, list);
3005b2fc499SJeff Garzik 	spin_unlock(&list->lock);
3015b2fc499SJeff Garzik 	spin_lock(&dev->done.lock);
3025b2fc499SJeff Garzik 	__skb_queue_tail(&dev->done, skb);
3035b2fc499SJeff Garzik 	if (dev->done.qlen == 1)
3045b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
3055b2fc499SJeff Garzik 	spin_unlock_irqrestore(&dev->done.lock, flags);
3065b2fc499SJeff Garzik }
3075b2fc499SJeff Garzik 
3085b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
3095b2fc499SJeff Garzik  *
3105b2fc499SJeff Garzik  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
3115b2fc499SJeff Garzik  * but tasklet_schedule() doesn't.  hope the failure is rare.
3125b2fc499SJeff Garzik  */
3135b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
3145b2fc499SJeff Garzik {
3155b2fc499SJeff Garzik 	set_bit (work, &dev->flags);
3165b2fc499SJeff Garzik 	if (!schedule_work (&dev->kevent))
31760b86755SJoe Perches 		netdev_err(dev->net, "kevent %d may have been dropped\n", work);
3185b2fc499SJeff Garzik 	else
31960b86755SJoe Perches 		netdev_dbg(dev->net, "kevent %d scheduled\n", work);
3205b2fc499SJeff Garzik }
3215b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
3225b2fc499SJeff Garzik 
3235b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
3245b2fc499SJeff Garzik 
3255b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
3265b2fc499SJeff Garzik 
3275b2fc499SJeff Garzik static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
3285b2fc499SJeff Garzik {
3295b2fc499SJeff Garzik 	struct sk_buff		*skb;
3305b2fc499SJeff Garzik 	struct skb_data		*entry;
3315b2fc499SJeff Garzik 	int			retval = 0;
3325b2fc499SJeff Garzik 	unsigned long		lockflags;
3335b2fc499SJeff Garzik 	size_t			size = dev->rx_urb_size;
3345b2fc499SJeff Garzik 
3355b2fc499SJeff Garzik 	if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
336a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
3375b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
3385b2fc499SJeff Garzik 		usb_free_urb (urb);
3395b2fc499SJeff Garzik 		return;
3405b2fc499SJeff Garzik 	}
3415b2fc499SJeff Garzik 	skb_reserve (skb, NET_IP_ALIGN);
3425b2fc499SJeff Garzik 
3435b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
3445b2fc499SJeff Garzik 	entry->urb = urb;
3455b2fc499SJeff Garzik 	entry->dev = dev;
3465b2fc499SJeff Garzik 	entry->state = rx_start;
3475b2fc499SJeff Garzik 	entry->length = 0;
3485b2fc499SJeff Garzik 
3495b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
3505b2fc499SJeff Garzik 		skb->data, size, rx_complete, skb);
3515b2fc499SJeff Garzik 
3525b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
3535b2fc499SJeff Garzik 
3548e95a202SJoe Perches 	if (netif_running (dev->net) &&
3558e95a202SJoe Perches 	    netif_device_present (dev->net) &&
35669ee472fSOliver Neukum 	    !test_bit (EVENT_RX_HALT, &dev->flags) &&
35769ee472fSOliver Neukum 	    !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
3585b2fc499SJeff Garzik 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
3595b2fc499SJeff Garzik 		case -EPIPE:
3605b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
3615b2fc499SJeff Garzik 			break;
3625b2fc499SJeff Garzik 		case -ENOMEM:
3635b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
3645b2fc499SJeff Garzik 			break;
3655b2fc499SJeff Garzik 		case -ENODEV:
366a475f603SJoe Perches 			netif_dbg(dev, ifdown, dev->net, "device gone\n");
3675b2fc499SJeff Garzik 			netif_device_detach (dev->net);
3685b2fc499SJeff Garzik 			break;
3695b2fc499SJeff Garzik 		default:
370a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
371a475f603SJoe Perches 				  "rx submit, %d\n", retval);
3725b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
3735b2fc499SJeff Garzik 			break;
3745b2fc499SJeff Garzik 		case 0:
3755b2fc499SJeff Garzik 			__skb_queue_tail (&dev->rxq, skb);
3765b2fc499SJeff Garzik 		}
3775b2fc499SJeff Garzik 	} else {
378a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
3795b2fc499SJeff Garzik 		retval = -ENOLINK;
3805b2fc499SJeff Garzik 	}
3815b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
3825b2fc499SJeff Garzik 	if (retval) {
3835b2fc499SJeff Garzik 		dev_kfree_skb_any (skb);
3845b2fc499SJeff Garzik 		usb_free_urb (urb);
3855b2fc499SJeff Garzik 	}
3865b2fc499SJeff Garzik }
3875b2fc499SJeff Garzik 
3885b2fc499SJeff Garzik 
3895b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
3905b2fc499SJeff Garzik 
3915b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
3925b2fc499SJeff Garzik {
3938e95a202SJoe Perches 	if (dev->driver_info->rx_fixup &&
3948e95a202SJoe Perches 	    !dev->driver_info->rx_fixup (dev, skb))
3955b2fc499SJeff Garzik 		goto error;
3965b2fc499SJeff Garzik 	// else network stack removes extra byte if we forced a short packet
3975b2fc499SJeff Garzik 
3985b2fc499SJeff Garzik 	if (skb->len)
3995b2fc499SJeff Garzik 		usbnet_skb_return (dev, skb);
4005b2fc499SJeff Garzik 	else {
401a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "drop\n");
4025b2fc499SJeff Garzik error:
4037963837fSHerbert Xu 		dev->net->stats.rx_errors++;
4045b2fc499SJeff Garzik 		skb_queue_tail (&dev->done, skb);
4055b2fc499SJeff Garzik 	}
4065b2fc499SJeff Garzik }
4075b2fc499SJeff Garzik 
4085b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4095b2fc499SJeff Garzik 
4105b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
4115b2fc499SJeff Garzik {
4125b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
4135b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
4145b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
4155b2fc499SJeff Garzik 	int			urb_status = urb->status;
4165b2fc499SJeff Garzik 
4175b2fc499SJeff Garzik 	skb_put (skb, urb->actual_length);
4185b2fc499SJeff Garzik 	entry->state = rx_done;
4195b2fc499SJeff Garzik 	entry->urb = NULL;
4205b2fc499SJeff Garzik 
4215b2fc499SJeff Garzik 	switch (urb_status) {
42218ab458fSDavid Brownell 	/* success */
4235b2fc499SJeff Garzik 	case 0:
4245b2fc499SJeff Garzik 		if (skb->len < dev->net->hard_header_len) {
4255b2fc499SJeff Garzik 			entry->state = rx_cleanup;
4267963837fSHerbert Xu 			dev->net->stats.rx_errors++;
4277963837fSHerbert Xu 			dev->net->stats.rx_length_errors++;
428a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
429a475f603SJoe Perches 				  "rx length %d\n", skb->len);
4305b2fc499SJeff Garzik 		}
4315b2fc499SJeff Garzik 		break;
4325b2fc499SJeff Garzik 
43318ab458fSDavid Brownell 	/* stalls need manual reset. this is rare ... except that
43418ab458fSDavid Brownell 	 * when going through USB 2.0 TTs, unplug appears this way.
4353ac49a1cSJean Delvare 	 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
43618ab458fSDavid Brownell 	 * storm, recovering as needed.
43718ab458fSDavid Brownell 	 */
4385b2fc499SJeff Garzik 	case -EPIPE:
4397963837fSHerbert Xu 		dev->net->stats.rx_errors++;
4405b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
4415b2fc499SJeff Garzik 		// FALLTHROUGH
4425b2fc499SJeff Garzik 
44318ab458fSDavid Brownell 	/* software-driven interface shutdown */
44418ab458fSDavid Brownell 	case -ECONNRESET:		/* async unlink */
44518ab458fSDavid Brownell 	case -ESHUTDOWN:		/* hardware gone */
446a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net,
447a475f603SJoe Perches 			  "rx shutdown, code %d\n", urb_status);
4485b2fc499SJeff Garzik 		goto block;
4495b2fc499SJeff Garzik 
45018ab458fSDavid Brownell 	/* we get controller i/o faults during khubd disconnect() delays.
45118ab458fSDavid Brownell 	 * throttle down resubmits, to avoid log floods; just temporarily,
45218ab458fSDavid Brownell 	 * so we still recover when the fault isn't a khubd delay.
45318ab458fSDavid Brownell 	 */
4545b2fc499SJeff Garzik 	case -EPROTO:
4555b2fc499SJeff Garzik 	case -ETIME:
4565b2fc499SJeff Garzik 	case -EILSEQ:
4577963837fSHerbert Xu 		dev->net->stats.rx_errors++;
4585b2fc499SJeff Garzik 		if (!timer_pending (&dev->delay)) {
4595b2fc499SJeff Garzik 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
460a475f603SJoe Perches 			netif_dbg(dev, link, dev->net,
461a475f603SJoe Perches 				  "rx throttle %d\n", urb_status);
4625b2fc499SJeff Garzik 		}
4635b2fc499SJeff Garzik block:
4645b2fc499SJeff Garzik 		entry->state = rx_cleanup;
4655b2fc499SJeff Garzik 		entry->urb = urb;
4665b2fc499SJeff Garzik 		urb = NULL;
4675b2fc499SJeff Garzik 		break;
4685b2fc499SJeff Garzik 
46918ab458fSDavid Brownell 	/* data overrun ... flush fifo? */
4705b2fc499SJeff Garzik 	case -EOVERFLOW:
4717963837fSHerbert Xu 		dev->net->stats.rx_over_errors++;
4725b2fc499SJeff Garzik 		// FALLTHROUGH
4735b2fc499SJeff Garzik 
4745b2fc499SJeff Garzik 	default:
4755b2fc499SJeff Garzik 		entry->state = rx_cleanup;
4767963837fSHerbert Xu 		dev->net->stats.rx_errors++;
477a475f603SJoe Perches 		netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
4785b2fc499SJeff Garzik 		break;
4795b2fc499SJeff Garzik 	}
4805b2fc499SJeff Garzik 
4815b2fc499SJeff Garzik 	defer_bh(dev, skb, &dev->rxq);
4825b2fc499SJeff Garzik 
4835b2fc499SJeff Garzik 	if (urb) {
4848e95a202SJoe Perches 		if (netif_running (dev->net) &&
4858e95a202SJoe Perches 		    !test_bit (EVENT_RX_HALT, &dev->flags)) {
4865b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_ATOMIC);
4875b2fc499SJeff Garzik 			return;
4885b2fc499SJeff Garzik 		}
4895b2fc499SJeff Garzik 		usb_free_urb (urb);
4905b2fc499SJeff Garzik 	}
491a475f603SJoe Perches 	netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
4925b2fc499SJeff Garzik }
4935b2fc499SJeff Garzik 
4945b2fc499SJeff Garzik static void intr_complete (struct urb *urb)
4955b2fc499SJeff Garzik {
4965b2fc499SJeff Garzik 	struct usbnet	*dev = urb->context;
4975b2fc499SJeff Garzik 	int		status = urb->status;
4985b2fc499SJeff Garzik 
4995b2fc499SJeff Garzik 	switch (status) {
5005b2fc499SJeff Garzik 	/* success */
5015b2fc499SJeff Garzik 	case 0:
5025b2fc499SJeff Garzik 		dev->driver_info->status(dev, urb);
5035b2fc499SJeff Garzik 		break;
5045b2fc499SJeff Garzik 
5055b2fc499SJeff Garzik 	/* software-driven interface shutdown */
50618ab458fSDavid Brownell 	case -ENOENT:		/* urb killed */
50718ab458fSDavid Brownell 	case -ESHUTDOWN:	/* hardware gone */
508a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net,
509a475f603SJoe Perches 			  "intr shutdown, code %d\n", status);
5105b2fc499SJeff Garzik 		return;
5115b2fc499SJeff Garzik 
5125b2fc499SJeff Garzik 	/* NOTE:  not throttling like RX/TX, since this endpoint
5135b2fc499SJeff Garzik 	 * already polls infrequently
5145b2fc499SJeff Garzik 	 */
5155b2fc499SJeff Garzik 	default:
51660b86755SJoe Perches 		netdev_dbg(dev->net, "intr status %d\n", status);
5175b2fc499SJeff Garzik 		break;
5185b2fc499SJeff Garzik 	}
5195b2fc499SJeff Garzik 
5205b2fc499SJeff Garzik 	if (!netif_running (dev->net))
5215b2fc499SJeff Garzik 		return;
5225b2fc499SJeff Garzik 
5235b2fc499SJeff Garzik 	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
5245b2fc499SJeff Garzik 	status = usb_submit_urb (urb, GFP_ATOMIC);
525a475f603SJoe Perches 	if (status != 0)
526a475f603SJoe Perches 		netif_err(dev, timer, dev->net,
527a475f603SJoe Perches 			  "intr resubmit --> %d\n", status);
5285b2fc499SJeff Garzik }
5295b2fc499SJeff Garzik 
5305b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5317834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev)
5327834ddbcSJussi Kivilinna {
5337834ddbcSJussi Kivilinna 	set_bit(EVENT_RX_PAUSED, &dev->flags);
5347834ddbcSJussi Kivilinna 
535a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
5367834ddbcSJussi Kivilinna }
5377834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx);
5387834ddbcSJussi Kivilinna 
5397834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev)
5407834ddbcSJussi Kivilinna {
5417834ddbcSJussi Kivilinna 	struct sk_buff *skb;
5427834ddbcSJussi Kivilinna 	int num = 0;
5437834ddbcSJussi Kivilinna 
5447834ddbcSJussi Kivilinna 	clear_bit(EVENT_RX_PAUSED, &dev->flags);
5457834ddbcSJussi Kivilinna 
5467834ddbcSJussi Kivilinna 	while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
5477834ddbcSJussi Kivilinna 		usbnet_skb_return(dev, skb);
5487834ddbcSJussi Kivilinna 		num++;
5497834ddbcSJussi Kivilinna 	}
5507834ddbcSJussi Kivilinna 
5517834ddbcSJussi Kivilinna 	tasklet_schedule(&dev->bh);
5527834ddbcSJussi Kivilinna 
553a475f603SJoe Perches 	netif_dbg(dev, rx_status, dev->net,
554a475f603SJoe Perches 		  "paused rx queue disabled, %d skbs requeued\n", num);
5557834ddbcSJussi Kivilinna }
5567834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx);
5577834ddbcSJussi Kivilinna 
5587834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev)
5597834ddbcSJussi Kivilinna {
5607834ddbcSJussi Kivilinna 	skb_queue_purge(&dev->rxq_pause);
5617834ddbcSJussi Kivilinna }
5627834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
5637834ddbcSJussi Kivilinna 
5647834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/
5655b2fc499SJeff Garzik 
5665b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
5675b2fc499SJeff Garzik 
5685b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
5695b2fc499SJeff Garzik {
5705b2fc499SJeff Garzik 	unsigned long		flags;
5715b2fc499SJeff Garzik 	struct sk_buff		*skb, *skbnext;
5725b2fc499SJeff Garzik 	int			count = 0;
5735b2fc499SJeff Garzik 
5745b2fc499SJeff Garzik 	spin_lock_irqsave (&q->lock, flags);
57583bfba5fSDavid S. Miller 	skb_queue_walk_safe(q, skb, skbnext) {
5765b2fc499SJeff Garzik 		struct skb_data		*entry;
5775b2fc499SJeff Garzik 		struct urb		*urb;
5785b2fc499SJeff Garzik 		int			retval;
5795b2fc499SJeff Garzik 
5805b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
5815b2fc499SJeff Garzik 		urb = entry->urb;
5825b2fc499SJeff Garzik 
5835b2fc499SJeff Garzik 		// during some PM-driven resume scenarios,
5845b2fc499SJeff Garzik 		// these (async) unlinks complete immediately
5855b2fc499SJeff Garzik 		retval = usb_unlink_urb (urb);
5865b2fc499SJeff Garzik 		if (retval != -EINPROGRESS && retval != 0)
58760b86755SJoe Perches 			netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
5885b2fc499SJeff Garzik 		else
5895b2fc499SJeff Garzik 			count++;
5905b2fc499SJeff Garzik 	}
5915b2fc499SJeff Garzik 	spin_unlock_irqrestore (&q->lock, flags);
5925b2fc499SJeff Garzik 	return count;
5935b2fc499SJeff Garzik }
5945b2fc499SJeff Garzik 
5955b2fc499SJeff Garzik // Flush all pending rx urbs
5965b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
5975b2fc499SJeff Garzik 
5985b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
5995b2fc499SJeff Garzik {
6005b2fc499SJeff Garzik 	if (netif_running(dev->net)) {
6015b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
6025b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
6035b2fc499SJeff Garzik 	}
6045b2fc499SJeff Garzik }
6055b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
6065b2fc499SJeff Garzik 
6075b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
6085b2fc499SJeff Garzik 
6095b2fc499SJeff Garzik // precondition: never called in_interrupt
61069ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev)
61169ee472fSOliver Neukum {
61269ee472fSOliver Neukum 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
61369ee472fSOliver Neukum 	DECLARE_WAITQUEUE(wait, current);
61469ee472fSOliver Neukum 	int temp;
61569ee472fSOliver Neukum 
61669ee472fSOliver Neukum 	/* ensure there are no more active urbs */
61769ee472fSOliver Neukum 	add_wait_queue(&unlink_wakeup, &wait);
61869ee472fSOliver Neukum 	set_current_state(TASK_UNINTERRUPTIBLE);
61969ee472fSOliver Neukum 	dev->wait = &unlink_wakeup;
62069ee472fSOliver Neukum 	temp = unlink_urbs(dev, &dev->txq) +
62169ee472fSOliver Neukum 		unlink_urbs(dev, &dev->rxq);
62269ee472fSOliver Neukum 
62369ee472fSOliver Neukum 	/* maybe wait for deletions to finish. */
62469ee472fSOliver Neukum 	while (!skb_queue_empty(&dev->rxq)
62569ee472fSOliver Neukum 		&& !skb_queue_empty(&dev->txq)
62669ee472fSOliver Neukum 		&& !skb_queue_empty(&dev->done)) {
62769ee472fSOliver Neukum 			schedule_timeout(UNLINK_TIMEOUT_MS);
62869ee472fSOliver Neukum 			set_current_state(TASK_UNINTERRUPTIBLE);
629a475f603SJoe Perches 			netif_dbg(dev, ifdown, dev->net,
630a475f603SJoe Perches 				  "waited for %d urb completions\n", temp);
63169ee472fSOliver Neukum 	}
63269ee472fSOliver Neukum 	set_current_state(TASK_RUNNING);
63369ee472fSOliver Neukum 	dev->wait = NULL;
63469ee472fSOliver Neukum 	remove_wait_queue(&unlink_wakeup, &wait);
63569ee472fSOliver Neukum }
6365b2fc499SJeff Garzik 
637777baa47SStephen Hemminger int usbnet_stop (struct net_device *net)
6385b2fc499SJeff Garzik {
6395b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
640a33e9e7fSJussi Kivilinna 	struct driver_info	*info = dev->driver_info;
641a33e9e7fSJussi Kivilinna 	int			retval;
6425b2fc499SJeff Garzik 
6435b2fc499SJeff Garzik 	netif_stop_queue (net);
6445b2fc499SJeff Garzik 
645a475f603SJoe Perches 	netif_info(dev, ifdown, dev->net,
646a475f603SJoe Perches 		   "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
6477963837fSHerbert Xu 		   net->stats.rx_packets, net->stats.tx_packets,
64860b86755SJoe Perches 		   net->stats.rx_errors, net->stats.tx_errors);
6495b2fc499SJeff Garzik 
650a33e9e7fSJussi Kivilinna 	/* allow minidriver to stop correctly (wireless devices to turn off
651a33e9e7fSJussi Kivilinna 	 * radio etc) */
652a33e9e7fSJussi Kivilinna 	if (info->stop) {
653a33e9e7fSJussi Kivilinna 		retval = info->stop(dev);
654a475f603SJoe Perches 		if (retval < 0)
655a475f603SJoe Perches 			netif_info(dev, ifdown, dev->net,
65660b86755SJoe Perches 				   "stop fail (%d) usbnet usb-%s-%s, %s\n",
657a33e9e7fSJussi Kivilinna 				   retval,
658a33e9e7fSJussi Kivilinna 				   dev->udev->bus->bus_name, dev->udev->devpath,
659a33e9e7fSJussi Kivilinna 				   info->description);
660a33e9e7fSJussi Kivilinna 	}
661a33e9e7fSJussi Kivilinna 
66269ee472fSOliver Neukum 	if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
66369ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
6645b2fc499SJeff Garzik 
6655b2fc499SJeff Garzik 	usb_kill_urb(dev->interrupt);
6665b2fc499SJeff Garzik 
6677834ddbcSJussi Kivilinna 	usbnet_purge_paused_rxq(dev);
6687834ddbcSJussi Kivilinna 
6695b2fc499SJeff Garzik 	/* deferred work (task, timer, softirq) must also stop.
6705b2fc499SJeff Garzik 	 * can't flush_scheduled_work() until we drop rtnl (later),
6715b2fc499SJeff Garzik 	 * else workers could deadlock; so make workers a NOP.
6725b2fc499SJeff Garzik 	 */
6735b2fc499SJeff Garzik 	dev->flags = 0;
6745b2fc499SJeff Garzik 	del_timer_sync (&dev->delay);
6755b2fc499SJeff Garzik 	tasklet_kill (&dev->bh);
67669ee472fSOliver Neukum 	if (info->manage_power)
67769ee472fSOliver Neukum 		info->manage_power(dev, 0);
67869ee472fSOliver Neukum 	else
679a11a6544SOliver Neukum 		usb_autopm_put_interface(dev->intf);
6805b2fc499SJeff Garzik 
6815b2fc499SJeff Garzik 	return 0;
6825b2fc499SJeff Garzik }
683777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop);
6845b2fc499SJeff Garzik 
6855b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
6865b2fc499SJeff Garzik 
6875b2fc499SJeff Garzik // posts reads, and enables write queuing
6885b2fc499SJeff Garzik 
6895b2fc499SJeff Garzik // precondition: never called in_interrupt
6905b2fc499SJeff Garzik 
691777baa47SStephen Hemminger int usbnet_open (struct net_device *net)
6925b2fc499SJeff Garzik {
6935b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
694a11a6544SOliver Neukum 	int			retval;
6955b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
6965b2fc499SJeff Garzik 
697a11a6544SOliver Neukum 	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
698a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
69960b86755SJoe Perches 			   "resumption fail (%d) usbnet usb-%s-%s, %s\n",
700a11a6544SOliver Neukum 			   retval,
70160b86755SJoe Perches 			   dev->udev->bus->bus_name,
70260b86755SJoe Perches 			   dev->udev->devpath,
703a11a6544SOliver Neukum 			   info->description);
704a11a6544SOliver Neukum 		goto done_nopm;
705a11a6544SOliver Neukum 	}
706a11a6544SOliver Neukum 
7075b2fc499SJeff Garzik 	// put into "known safe" state
7085b2fc499SJeff Garzik 	if (info->reset && (retval = info->reset (dev)) < 0) {
709a475f603SJoe Perches 		netif_info(dev, ifup, dev->net,
71060b86755SJoe Perches 			   "open reset fail (%d) usbnet usb-%s-%s, %s\n",
7115b2fc499SJeff Garzik 			   retval,
71260b86755SJoe Perches 			   dev->udev->bus->bus_name,
71360b86755SJoe Perches 			   dev->udev->devpath,
7145b2fc499SJeff Garzik 			   info->description);
7155b2fc499SJeff Garzik 		goto done;
7165b2fc499SJeff Garzik 	}
7175b2fc499SJeff Garzik 
7185b2fc499SJeff Garzik 	// insist peer be connected
7195b2fc499SJeff Garzik 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
720a475f603SJoe Perches 		netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
7215b2fc499SJeff Garzik 		goto done;
7225b2fc499SJeff Garzik 	}
7235b2fc499SJeff Garzik 
7245b2fc499SJeff Garzik 	/* start any status interrupt transfer */
7255b2fc499SJeff Garzik 	if (dev->interrupt) {
7265b2fc499SJeff Garzik 		retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
7275b2fc499SJeff Garzik 		if (retval < 0) {
728a475f603SJoe Perches 			netif_err(dev, ifup, dev->net,
729a475f603SJoe Perches 				  "intr submit %d\n", retval);
7305b2fc499SJeff Garzik 			goto done;
7315b2fc499SJeff Garzik 		}
7325b2fc499SJeff Garzik 	}
7335b2fc499SJeff Garzik 
7345b2fc499SJeff Garzik 	netif_start_queue (net);
735a475f603SJoe Perches 	netif_info(dev, ifup, dev->net,
736a475f603SJoe Perches 		   "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
73760b86755SJoe Perches 		   (int)RX_QLEN(dev), (int)TX_QLEN(dev),
738a475f603SJoe Perches 		   dev->net->mtu,
739a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
740a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
741a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
742a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
743a475f603SJoe Perches 		   (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
744a475f603SJoe Perches 		   "simple");
7455b2fc499SJeff Garzik 
7465b2fc499SJeff Garzik 	// delay posting reads until we're fully open
7475b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
74869ee472fSOliver Neukum 	if (info->manage_power) {
74969ee472fSOliver Neukum 		retval = info->manage_power(dev, 1);
75069ee472fSOliver Neukum 		if (retval < 0)
75169ee472fSOliver Neukum 			goto done;
75269ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
75369ee472fSOliver Neukum 	}
754a11a6544SOliver Neukum 	return retval;
755a475f603SJoe Perches 
7565b2fc499SJeff Garzik done:
757a11a6544SOliver Neukum 	usb_autopm_put_interface(dev->intf);
758a11a6544SOliver Neukum done_nopm:
7595b2fc499SJeff Garzik 	return retval;
7605b2fc499SJeff Garzik }
761777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open);
7625b2fc499SJeff Garzik 
7635b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
7645b2fc499SJeff Garzik 
7655b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
7665b2fc499SJeff Garzik  * they'll probably want to use this base set.
7675b2fc499SJeff Garzik  */
7685b2fc499SJeff Garzik 
7695b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
7705b2fc499SJeff Garzik {
7715b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7725b2fc499SJeff Garzik 
7735b2fc499SJeff Garzik 	if (!dev->mii.mdio_read)
7745b2fc499SJeff Garzik 		return -EOPNOTSUPP;
7755b2fc499SJeff Garzik 
7765b2fc499SJeff Garzik 	return mii_ethtool_gset(&dev->mii, cmd);
7775b2fc499SJeff Garzik }
7785b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings);
7795b2fc499SJeff Garzik 
7805b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
7815b2fc499SJeff Garzik {
7825b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7835b2fc499SJeff Garzik 	int retval;
7845b2fc499SJeff Garzik 
7855b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
7865b2fc499SJeff Garzik 		return -EOPNOTSUPP;
7875b2fc499SJeff Garzik 
7885b2fc499SJeff Garzik 	retval = mii_ethtool_sset(&dev->mii, cmd);
7895b2fc499SJeff Garzik 
7905b2fc499SJeff Garzik 	/* link speed/duplex might have changed */
7915b2fc499SJeff Garzik 	if (dev->driver_info->link_reset)
7925b2fc499SJeff Garzik 		dev->driver_info->link_reset(dev);
7935b2fc499SJeff Garzik 
7945b2fc499SJeff Garzik 	return retval;
7955b2fc499SJeff Garzik 
7965b2fc499SJeff Garzik }
7975b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings);
7985b2fc499SJeff Garzik 
7995b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
8005b2fc499SJeff Garzik {
8015b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8025b2fc499SJeff Garzik 
8035b2fc499SJeff Garzik 	/* If a check_connect is defined, return its result */
8045b2fc499SJeff Garzik 	if (dev->driver_info->check_connect)
8055b2fc499SJeff Garzik 		return dev->driver_info->check_connect (dev) == 0;
8065b2fc499SJeff Garzik 
8075b2fc499SJeff Garzik 	/* if the device has mii operations, use those */
8085b2fc499SJeff Garzik 	if (dev->mii.mdio_read)
8095b2fc499SJeff Garzik 		return mii_link_ok(&dev->mii);
8105b2fc499SJeff Garzik 
81105ffb3e2SBjørn Mork 	/* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
81205ffb3e2SBjørn Mork 	return ethtool_op_get_link(net);
8135b2fc499SJeff Garzik }
8145b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
8155b2fc499SJeff Garzik 
8165b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
8175b2fc499SJeff Garzik {
8185b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8195b2fc499SJeff Garzik 
8205b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
8215b2fc499SJeff Garzik 		return -EOPNOTSUPP;
8225b2fc499SJeff Garzik 
8235b2fc499SJeff Garzik 	return mii_nway_restart(&dev->mii);
8245b2fc499SJeff Garzik }
8255b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
8265b2fc499SJeff Garzik 
8275b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
8285b2fc499SJeff Garzik {
8295b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8305b2fc499SJeff Garzik 
8315b2fc499SJeff Garzik 	strncpy (info->driver, dev->driver_name, sizeof info->driver);
8325b2fc499SJeff Garzik 	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
8335b2fc499SJeff Garzik 	strncpy (info->fw_version, dev->driver_info->description,
8345b2fc499SJeff Garzik 		sizeof info->fw_version);
8355b2fc499SJeff Garzik 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
8365b2fc499SJeff Garzik }
8375b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
8385b2fc499SJeff Garzik 
8395b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
8405b2fc499SJeff Garzik {
8415b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8425b2fc499SJeff Garzik 
8435b2fc499SJeff Garzik 	return dev->msg_enable;
8445b2fc499SJeff Garzik }
8455b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
8465b2fc499SJeff Garzik 
8475b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
8485b2fc499SJeff Garzik {
8495b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
8505b2fc499SJeff Garzik 
8515b2fc499SJeff Garzik 	dev->msg_enable = level;
8525b2fc499SJeff Garzik }
8535b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
8545b2fc499SJeff Garzik 
8555b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
8560fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = {
8575b2fc499SJeff Garzik 	.get_settings		= usbnet_get_settings,
8585b2fc499SJeff Garzik 	.set_settings		= usbnet_set_settings,
8595b2fc499SJeff Garzik 	.get_link		= usbnet_get_link,
8605b2fc499SJeff Garzik 	.nway_reset		= usbnet_nway_reset,
8615b2fc499SJeff Garzik 	.get_drvinfo		= usbnet_get_drvinfo,
8625b2fc499SJeff Garzik 	.get_msglevel		= usbnet_get_msglevel,
8635b2fc499SJeff Garzik 	.set_msglevel		= usbnet_set_msglevel,
8645b2fc499SJeff Garzik };
8655b2fc499SJeff Garzik 
8665b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
8675b2fc499SJeff Garzik 
8685b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
8695b2fc499SJeff Garzik  *
8705b2fc499SJeff Garzik  * NOTE:  with 2.5 we could do more of this using completion callbacks,
8715b2fc499SJeff Garzik  * especially now that control transfers can be queued.
8725b2fc499SJeff Garzik  */
8735b2fc499SJeff Garzik static void
8745b2fc499SJeff Garzik kevent (struct work_struct *work)
8755b2fc499SJeff Garzik {
8765b2fc499SJeff Garzik 	struct usbnet		*dev =
8775b2fc499SJeff Garzik 		container_of(work, struct usbnet, kevent);
8785b2fc499SJeff Garzik 	int			status;
8795b2fc499SJeff Garzik 
8805b2fc499SJeff Garzik 	/* usb_clear_halt() needs a thread context */
8815b2fc499SJeff Garzik 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
8825b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->txq);
88369ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
88469ee472fSOliver Neukum 		if (status < 0)
88569ee472fSOliver Neukum 			goto fail_pipe;
8865b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->out);
88769ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
8888e95a202SJoe Perches 		if (status < 0 &&
8898e95a202SJoe Perches 		    status != -EPIPE &&
8908e95a202SJoe Perches 		    status != -ESHUTDOWN) {
8915b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
89269ee472fSOliver Neukum fail_pipe:
89360b86755SJoe Perches 				netdev_err(dev->net, "can't clear tx halt, status %d\n",
8945b2fc499SJeff Garzik 					   status);
8955b2fc499SJeff Garzik 		} else {
8965b2fc499SJeff Garzik 			clear_bit (EVENT_TX_HALT, &dev->flags);
8975b2fc499SJeff Garzik 			if (status != -ESHUTDOWN)
8985b2fc499SJeff Garzik 				netif_wake_queue (dev->net);
8995b2fc499SJeff Garzik 		}
9005b2fc499SJeff Garzik 	}
9015b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
9025b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->rxq);
90369ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
90469ee472fSOliver Neukum 		if (status < 0)
90569ee472fSOliver Neukum 			goto fail_halt;
9065b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->in);
90769ee472fSOliver Neukum 		usb_autopm_put_interface(dev->intf);
9088e95a202SJoe Perches 		if (status < 0 &&
9098e95a202SJoe Perches 		    status != -EPIPE &&
9108e95a202SJoe Perches 		    status != -ESHUTDOWN) {
9115b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
91269ee472fSOliver Neukum fail_halt:
91360b86755SJoe Perches 				netdev_err(dev->net, "can't clear rx halt, status %d\n",
9145b2fc499SJeff Garzik 					   status);
9155b2fc499SJeff Garzik 		} else {
9165b2fc499SJeff Garzik 			clear_bit (EVENT_RX_HALT, &dev->flags);
9175b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
9185b2fc499SJeff Garzik 		}
9195b2fc499SJeff Garzik 	}
9205b2fc499SJeff Garzik 
9215b2fc499SJeff Garzik 	/* tasklet could resubmit itself forever if memory is tight */
9225b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
9235b2fc499SJeff Garzik 		struct urb	*urb = NULL;
9245b2fc499SJeff Garzik 
9255b2fc499SJeff Garzik 		if (netif_running (dev->net))
9265b2fc499SJeff Garzik 			urb = usb_alloc_urb (0, GFP_KERNEL);
9275b2fc499SJeff Garzik 		else
9285b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
9295b2fc499SJeff Garzik 		if (urb != NULL) {
9305b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
93169ee472fSOliver Neukum 			status = usb_autopm_get_interface(dev->intf);
93269ee472fSOliver Neukum 			if (status < 0)
93369ee472fSOliver Neukum 				goto fail_lowmem;
9345b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_KERNEL);
93569ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
93669ee472fSOliver Neukum fail_lowmem:
9375b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
9385b2fc499SJeff Garzik 		}
9395b2fc499SJeff Garzik 	}
9405b2fc499SJeff Garzik 
9415b2fc499SJeff Garzik 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
9425b2fc499SJeff Garzik 		struct driver_info	*info = dev->driver_info;
9435b2fc499SJeff Garzik 		int			retval = 0;
9445b2fc499SJeff Garzik 
9455b2fc499SJeff Garzik 		clear_bit (EVENT_LINK_RESET, &dev->flags);
94669ee472fSOliver Neukum 		status = usb_autopm_get_interface(dev->intf);
94769ee472fSOliver Neukum 		if (status < 0)
94869ee472fSOliver Neukum 			goto skip_reset;
9495b2fc499SJeff Garzik 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
95069ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
95169ee472fSOliver Neukum skip_reset:
95260b86755SJoe Perches 			netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
9535b2fc499SJeff Garzik 				    retval,
95460b86755SJoe Perches 				    dev->udev->bus->bus_name,
95560b86755SJoe Perches 				    dev->udev->devpath,
9565b2fc499SJeff Garzik 				    info->description);
95769ee472fSOliver Neukum 		} else {
95869ee472fSOliver Neukum 			usb_autopm_put_interface(dev->intf);
9595b2fc499SJeff Garzik 		}
9605b2fc499SJeff Garzik 	}
9615b2fc499SJeff Garzik 
9625b2fc499SJeff Garzik 	if (dev->flags)
96360b86755SJoe Perches 		netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
9645b2fc499SJeff Garzik }
9655b2fc499SJeff Garzik 
9665b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9675b2fc499SJeff Garzik 
9685b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
9695b2fc499SJeff Garzik {
9705b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
9715b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
9725b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
9735b2fc499SJeff Garzik 
9745b2fc499SJeff Garzik 	if (urb->status == 0) {
9757963837fSHerbert Xu 		dev->net->stats.tx_packets++;
9767963837fSHerbert Xu 		dev->net->stats.tx_bytes += entry->length;
9775b2fc499SJeff Garzik 	} else {
9787963837fSHerbert Xu 		dev->net->stats.tx_errors++;
9795b2fc499SJeff Garzik 
9805b2fc499SJeff Garzik 		switch (urb->status) {
9815b2fc499SJeff Garzik 		case -EPIPE:
9825b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
9835b2fc499SJeff Garzik 			break;
9845b2fc499SJeff Garzik 
9855b2fc499SJeff Garzik 		/* software-driven interface shutdown */
9865b2fc499SJeff Garzik 		case -ECONNRESET:		// async unlink
9875b2fc499SJeff Garzik 		case -ESHUTDOWN:		// hardware gone
9885b2fc499SJeff Garzik 			break;
9895b2fc499SJeff Garzik 
9905b2fc499SJeff Garzik 		// like rx, tx gets controller i/o faults during khubd delays
9915b2fc499SJeff Garzik 		// and so it uses the same throttling mechanism.
9925b2fc499SJeff Garzik 		case -EPROTO:
9935b2fc499SJeff Garzik 		case -ETIME:
9945b2fc499SJeff Garzik 		case -EILSEQ:
99569ee472fSOliver Neukum 			usb_mark_last_busy(dev->udev);
9965b2fc499SJeff Garzik 			if (!timer_pending (&dev->delay)) {
9975b2fc499SJeff Garzik 				mod_timer (&dev->delay,
9985b2fc499SJeff Garzik 					jiffies + THROTTLE_JIFFIES);
999a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1000a475f603SJoe Perches 					  "tx throttle %d\n", urb->status);
10015b2fc499SJeff Garzik 			}
10025b2fc499SJeff Garzik 			netif_stop_queue (dev->net);
10035b2fc499SJeff Garzik 			break;
10045b2fc499SJeff Garzik 		default:
1005a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net,
1006a475f603SJoe Perches 				  "tx err %d\n", entry->urb->status);
10075b2fc499SJeff Garzik 			break;
10085b2fc499SJeff Garzik 		}
10095b2fc499SJeff Garzik 	}
10105b2fc499SJeff Garzik 
101169ee472fSOliver Neukum 	usb_autopm_put_interface_async(dev->intf);
10125b2fc499SJeff Garzik 	urb->dev = NULL;
10135b2fc499SJeff Garzik 	entry->state = tx_done;
10145b2fc499SJeff Garzik 	defer_bh(dev, skb, &dev->txq);
10155b2fc499SJeff Garzik }
10165b2fc499SJeff Garzik 
10175b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10185b2fc499SJeff Garzik 
1019777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net)
10205b2fc499SJeff Garzik {
10215b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
10225b2fc499SJeff Garzik 
10235b2fc499SJeff Garzik 	unlink_urbs (dev, &dev->txq);
10245b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
10255b2fc499SJeff Garzik 
10265b2fc499SJeff Garzik 	// FIXME: device recovery -- reset?
10275b2fc499SJeff Garzik }
1028777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
10295b2fc499SJeff Garzik 
10305b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10315b2fc499SJeff Garzik 
103225a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
103325a79c41SStephen Hemminger 				     struct net_device *net)
10345b2fc499SJeff Garzik {
10355b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
10365b2fc499SJeff Garzik 	int			length;
10375b2fc499SJeff Garzik 	struct urb		*urb = NULL;
10385b2fc499SJeff Garzik 	struct skb_data		*entry;
10395b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
10405b2fc499SJeff Garzik 	unsigned long		flags;
104125a79c41SStephen Hemminger 	int retval;
10425b2fc499SJeff Garzik 
10435b2fc499SJeff Garzik 	// some devices want funky USB-level framing, for
10445b2fc499SJeff Garzik 	// win32 driver (usually) and/or hardware quirks
10455b2fc499SJeff Garzik 	if (info->tx_fixup) {
10465b2fc499SJeff Garzik 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
10475b2fc499SJeff Garzik 		if (!skb) {
1048a475f603SJoe Perches 			netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
10495b2fc499SJeff Garzik 			goto drop;
10505b2fc499SJeff Garzik 		}
10515b2fc499SJeff Garzik 	}
10525b2fc499SJeff Garzik 	length = skb->len;
10535b2fc499SJeff Garzik 
10545b2fc499SJeff Garzik 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1055a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "no urb\n");
10565b2fc499SJeff Garzik 		goto drop;
10575b2fc499SJeff Garzik 	}
10585b2fc499SJeff Garzik 
10595b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
10605b2fc499SJeff Garzik 	entry->urb = urb;
10615b2fc499SJeff Garzik 	entry->dev = dev;
10625b2fc499SJeff Garzik 	entry->state = tx_start;
10635b2fc499SJeff Garzik 	entry->length = length;
10645b2fc499SJeff Garzik 
10655b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
10665b2fc499SJeff Garzik 			skb->data, skb->len, tx_complete, skb);
10675b2fc499SJeff Garzik 
10685b2fc499SJeff Garzik 	/* don't assume the hardware handles USB_ZERO_PACKET
10695b2fc499SJeff Garzik 	 * NOTE:  strictly conforming cdc-ether devices should expect
10705b2fc499SJeff Garzik 	 * the ZLP here, but ignore the one-byte packet.
10715b2fc499SJeff Garzik 	 */
1072b4d562e3SElina Pasheva 	if (length % dev->maxpacket == 0) {
1073b4d562e3SElina Pasheva 		if (!(info->flags & FLAG_SEND_ZLP)) {
10745b2fc499SJeff Garzik 			urb->transfer_buffer_length++;
10753e323f3eSPeter Korsgaard 			if (skb_tailroom(skb)) {
10763e323f3eSPeter Korsgaard 				skb->data[skb->len] = 0;
10773e323f3eSPeter Korsgaard 				__skb_put(skb, 1);
10783e323f3eSPeter Korsgaard 			}
1079b4d562e3SElina Pasheva 		} else
1080b4d562e3SElina Pasheva 			urb->transfer_flags |= URB_ZERO_PACKET;
10813e323f3eSPeter Korsgaard 	}
10825b2fc499SJeff Garzik 
10835b2fc499SJeff Garzik 	spin_lock_irqsave(&dev->txq.lock, flags);
108469ee472fSOliver Neukum 	retval = usb_autopm_get_interface_async(dev->intf);
108569ee472fSOliver Neukum 	if (retval < 0) {
108669ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
108769ee472fSOliver Neukum 		goto drop;
108869ee472fSOliver Neukum 	}
108969ee472fSOliver Neukum 
109069ee472fSOliver Neukum #ifdef CONFIG_PM
109169ee472fSOliver Neukum 	/* if this triggers the device is still a sleep */
109269ee472fSOliver Neukum 	if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
109369ee472fSOliver Neukum 		/* transmission will be done in resume */
109469ee472fSOliver Neukum 		usb_anchor_urb(urb, &dev->deferred);
109569ee472fSOliver Neukum 		/* no use to process more packets */
109669ee472fSOliver Neukum 		netif_stop_queue(net);
109769ee472fSOliver Neukum 		spin_unlock_irqrestore(&dev->txq.lock, flags);
109860b86755SJoe Perches 		netdev_dbg(dev->net, "Delaying transmission for resumption\n");
109969ee472fSOliver Neukum 		goto deferred;
110069ee472fSOliver Neukum 	}
110169ee472fSOliver Neukum #endif
11025b2fc499SJeff Garzik 
11035b2fc499SJeff Garzik 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
11045b2fc499SJeff Garzik 	case -EPIPE:
11055b2fc499SJeff Garzik 		netif_stop_queue (net);
11065b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
110769ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
11085b2fc499SJeff Garzik 		break;
11095b2fc499SJeff Garzik 	default:
111069ee472fSOliver Neukum 		usb_autopm_put_interface_async(dev->intf);
1111a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net,
1112a475f603SJoe Perches 			  "tx: submit urb err %d\n", retval);
11135b2fc499SJeff Garzik 		break;
11145b2fc499SJeff Garzik 	case 0:
11155b2fc499SJeff Garzik 		net->trans_start = jiffies;
11165b2fc499SJeff Garzik 		__skb_queue_tail (&dev->txq, skb);
11175b2fc499SJeff Garzik 		if (dev->txq.qlen >= TX_QLEN (dev))
11185b2fc499SJeff Garzik 			netif_stop_queue (net);
11195b2fc499SJeff Garzik 	}
11205b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->txq.lock, flags);
11215b2fc499SJeff Garzik 
11225b2fc499SJeff Garzik 	if (retval) {
1123a475f603SJoe Perches 		netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
11245b2fc499SJeff Garzik drop:
11257963837fSHerbert Xu 		dev->net->stats.tx_dropped++;
11265b2fc499SJeff Garzik 		if (skb)
11275b2fc499SJeff Garzik 			dev_kfree_skb_any (skb);
11285b2fc499SJeff Garzik 		usb_free_urb (urb);
1129a475f603SJoe Perches 	} else
1130a475f603SJoe Perches 		netif_dbg(dev, tx_queued, dev->net,
1131a475f603SJoe Perches 			  "> tx, len %d, type 0x%x\n", length, skb->protocol);
113269ee472fSOliver Neukum #ifdef CONFIG_PM
113369ee472fSOliver Neukum deferred:
113469ee472fSOliver Neukum #endif
113525a79c41SStephen Hemminger 	return NETDEV_TX_OK;
11365b2fc499SJeff Garzik }
1137777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit);
11385b2fc499SJeff Garzik 
11395b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
11405b2fc499SJeff Garzik 
11415b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
11425b2fc499SJeff Garzik 
11435b2fc499SJeff Garzik static void usbnet_bh (unsigned long param)
11445b2fc499SJeff Garzik {
11455b2fc499SJeff Garzik 	struct usbnet		*dev = (struct usbnet *) param;
11465b2fc499SJeff Garzik 	struct sk_buff		*skb;
11475b2fc499SJeff Garzik 	struct skb_data		*entry;
11485b2fc499SJeff Garzik 
11495b2fc499SJeff Garzik 	while ((skb = skb_dequeue (&dev->done))) {
11505b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
11515b2fc499SJeff Garzik 		switch (entry->state) {
11525b2fc499SJeff Garzik 		case rx_done:
11535b2fc499SJeff Garzik 			entry->state = rx_cleanup;
11545b2fc499SJeff Garzik 			rx_process (dev, skb);
11555b2fc499SJeff Garzik 			continue;
11565b2fc499SJeff Garzik 		case tx_done:
11575b2fc499SJeff Garzik 		case rx_cleanup:
11585b2fc499SJeff Garzik 			usb_free_urb (entry->urb);
11595b2fc499SJeff Garzik 			dev_kfree_skb (skb);
11605b2fc499SJeff Garzik 			continue;
11615b2fc499SJeff Garzik 		default:
116260b86755SJoe Perches 			netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
11635b2fc499SJeff Garzik 		}
11645b2fc499SJeff Garzik 	}
11655b2fc499SJeff Garzik 
11665b2fc499SJeff Garzik 	// waiting for all pending urbs to complete?
11675b2fc499SJeff Garzik 	if (dev->wait) {
11685b2fc499SJeff Garzik 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
11695b2fc499SJeff Garzik 			wake_up (dev->wait);
11705b2fc499SJeff Garzik 		}
11715b2fc499SJeff Garzik 
11725b2fc499SJeff Garzik 	// or are we maybe short a few urbs?
11738e95a202SJoe Perches 	} else if (netif_running (dev->net) &&
11748e95a202SJoe Perches 		   netif_device_present (dev->net) &&
11758e95a202SJoe Perches 		   !timer_pending (&dev->delay) &&
11768e95a202SJoe Perches 		   !test_bit (EVENT_RX_HALT, &dev->flags)) {
11775b2fc499SJeff Garzik 		int	temp = dev->rxq.qlen;
11785b2fc499SJeff Garzik 		int	qlen = RX_QLEN (dev);
11795b2fc499SJeff Garzik 
11805b2fc499SJeff Garzik 		if (temp < qlen) {
11815b2fc499SJeff Garzik 			struct urb	*urb;
11825b2fc499SJeff Garzik 			int		i;
11835b2fc499SJeff Garzik 
11845b2fc499SJeff Garzik 			// don't refill the queue all at once
11855b2fc499SJeff Garzik 			for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
11865b2fc499SJeff Garzik 				urb = usb_alloc_urb (0, GFP_ATOMIC);
11875b2fc499SJeff Garzik 				if (urb != NULL)
11885b2fc499SJeff Garzik 					rx_submit (dev, urb, GFP_ATOMIC);
11895b2fc499SJeff Garzik 			}
1190a475f603SJoe Perches 			if (temp != dev->rxq.qlen)
1191a475f603SJoe Perches 				netif_dbg(dev, link, dev->net,
1192a475f603SJoe Perches 					  "rxqlen %d --> %d\n",
11935b2fc499SJeff Garzik 					  temp, dev->rxq.qlen);
11945b2fc499SJeff Garzik 			if (dev->rxq.qlen < qlen)
11955b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
11965b2fc499SJeff Garzik 		}
11975b2fc499SJeff Garzik 		if (dev->txq.qlen < TX_QLEN (dev))
11985b2fc499SJeff Garzik 			netif_wake_queue (dev->net);
11995b2fc499SJeff Garzik 	}
12005b2fc499SJeff Garzik }
12015b2fc499SJeff Garzik 
12025b2fc499SJeff Garzik 
12035b2fc499SJeff Garzik /*-------------------------------------------------------------------------
12045b2fc499SJeff Garzik  *
12055b2fc499SJeff Garzik  * USB Device Driver support
12065b2fc499SJeff Garzik  *
12075b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
12085b2fc499SJeff Garzik 
12095b2fc499SJeff Garzik // precondition: never called in_interrupt
12105b2fc499SJeff Garzik 
12115b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
12125b2fc499SJeff Garzik {
12135b2fc499SJeff Garzik 	struct usbnet		*dev;
12145b2fc499SJeff Garzik 	struct usb_device	*xdev;
12155b2fc499SJeff Garzik 	struct net_device	*net;
12165b2fc499SJeff Garzik 
12175b2fc499SJeff Garzik 	dev = usb_get_intfdata(intf);
12185b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
12195b2fc499SJeff Garzik 	if (!dev)
12205b2fc499SJeff Garzik 		return;
12215b2fc499SJeff Garzik 
12225b2fc499SJeff Garzik 	xdev = interface_to_usbdev (intf);
12235b2fc499SJeff Garzik 
1224a475f603SJoe Perches 	netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
12255b2fc499SJeff Garzik 		   intf->dev.driver->name,
12265b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
12275b2fc499SJeff Garzik 		   dev->driver_info->description);
12285b2fc499SJeff Garzik 
12295b2fc499SJeff Garzik 	net = dev->net;
12305b2fc499SJeff Garzik 	unregister_netdev (net);
12315b2fc499SJeff Garzik 
12325b2fc499SJeff Garzik 	/* we don't hold rtnl here ... */
12335b2fc499SJeff Garzik 	flush_scheduled_work ();
12345b2fc499SJeff Garzik 
12355b2fc499SJeff Garzik 	if (dev->driver_info->unbind)
12365b2fc499SJeff Garzik 		dev->driver_info->unbind (dev, intf);
12375b2fc499SJeff Garzik 
12385b2fc499SJeff Garzik 	free_netdev(net);
12395b2fc499SJeff Garzik 	usb_put_dev (xdev);
12405b2fc499SJeff Garzik }
12415b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
12425b2fc499SJeff Garzik 
1243777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = {
1244777baa47SStephen Hemminger 	.ndo_open		= usbnet_open,
1245777baa47SStephen Hemminger 	.ndo_stop		= usbnet_stop,
1246777baa47SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
1247777baa47SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
1248777baa47SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1249777baa47SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1250777baa47SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1251777baa47SStephen Hemminger };
12525b2fc499SJeff Garzik 
12535b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12545b2fc499SJeff Garzik 
12555b2fc499SJeff Garzik // precondition: never called in_interrupt
12565b2fc499SJeff Garzik 
1257225794f8SMarcel Holtmann static struct device_type wlan_type = {
1258225794f8SMarcel Holtmann 	.name	= "wlan",
1259225794f8SMarcel Holtmann };
1260225794f8SMarcel Holtmann 
1261225794f8SMarcel Holtmann static struct device_type wwan_type = {
1262225794f8SMarcel Holtmann 	.name	= "wwan",
1263225794f8SMarcel Holtmann };
1264225794f8SMarcel Holtmann 
12655b2fc499SJeff Garzik int
12665b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
12675b2fc499SJeff Garzik {
12685b2fc499SJeff Garzik 	struct usbnet			*dev;
12695b2fc499SJeff Garzik 	struct net_device		*net;
12705b2fc499SJeff Garzik 	struct usb_host_interface	*interface;
12715b2fc499SJeff Garzik 	struct driver_info		*info;
12725b2fc499SJeff Garzik 	struct usb_device		*xdev;
12735b2fc499SJeff Garzik 	int				status;
12745b2fc499SJeff Garzik 	const char			*name;
12755b2fc499SJeff Garzik 
12765b2fc499SJeff Garzik 	name = udev->dev.driver->name;
12775b2fc499SJeff Garzik 	info = (struct driver_info *) prod->driver_info;
12785b2fc499SJeff Garzik 	if (!info) {
12795b2fc499SJeff Garzik 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
12805b2fc499SJeff Garzik 		return -ENODEV;
12815b2fc499SJeff Garzik 	}
12825b2fc499SJeff Garzik 	xdev = interface_to_usbdev (udev);
12835b2fc499SJeff Garzik 	interface = udev->cur_altsetting;
12845b2fc499SJeff Garzik 
12855b2fc499SJeff Garzik 	usb_get_dev (xdev);
12865b2fc499SJeff Garzik 
12875b2fc499SJeff Garzik 	status = -ENOMEM;
12885b2fc499SJeff Garzik 
12895b2fc499SJeff Garzik 	// set up our own records
12905b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(*dev));
12915b2fc499SJeff Garzik 	if (!net) {
12925b2fc499SJeff Garzik 		dbg ("can't kmalloc dev");
12935b2fc499SJeff Garzik 		goto out;
12945b2fc499SJeff Garzik 	}
12955b2fc499SJeff Garzik 
1296*0dacca73SBen Hutchings 	/* netdev_printk() needs this so do it as early as possible */
1297*0dacca73SBen Hutchings 	SET_NETDEV_DEV(net, &udev->dev);
1298*0dacca73SBen Hutchings 
12995b2fc499SJeff Garzik 	dev = netdev_priv(net);
13005b2fc499SJeff Garzik 	dev->udev = xdev;
1301a11a6544SOliver Neukum 	dev->intf = udev;
13025b2fc499SJeff Garzik 	dev->driver_info = info;
13035b2fc499SJeff Garzik 	dev->driver_name = name;
13045b2fc499SJeff Garzik 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
13055b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
13065b2fc499SJeff Garzik 	skb_queue_head_init (&dev->rxq);
13075b2fc499SJeff Garzik 	skb_queue_head_init (&dev->txq);
13085b2fc499SJeff Garzik 	skb_queue_head_init (&dev->done);
13097834ddbcSJussi Kivilinna 	skb_queue_head_init(&dev->rxq_pause);
13105b2fc499SJeff Garzik 	dev->bh.func = usbnet_bh;
13115b2fc499SJeff Garzik 	dev->bh.data = (unsigned long) dev;
13125b2fc499SJeff Garzik 	INIT_WORK (&dev->kevent, kevent);
131369ee472fSOliver Neukum 	init_usb_anchor(&dev->deferred);
13145b2fc499SJeff Garzik 	dev->delay.function = usbnet_bh;
13155b2fc499SJeff Garzik 	dev->delay.data = (unsigned long) dev;
13165b2fc499SJeff Garzik 	init_timer (&dev->delay);
13175b2fc499SJeff Garzik 	mutex_init (&dev->phy_mutex);
13185b2fc499SJeff Garzik 
13195b2fc499SJeff Garzik 	dev->net = net;
13205b2fc499SJeff Garzik 	strcpy (net->name, "usb%d");
13215b2fc499SJeff Garzik 	memcpy (net->dev_addr, node_id, sizeof node_id);
13225b2fc499SJeff Garzik 
13235b2fc499SJeff Garzik 	/* rx and tx sides can use different message sizes;
13245b2fc499SJeff Garzik 	 * bind() should set rx_urb_size in that case.
13255b2fc499SJeff Garzik 	 */
13265b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
13275b2fc499SJeff Garzik #if 0
13285b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures
13295b2fc499SJeff Garzik 	// possible with some EHCI controllers
13306a35528aSYang Hongyang 	if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
13315b2fc499SJeff Garzik 		net->features |= NETIF_F_HIGHDMA;
13325b2fc499SJeff Garzik #endif
13335b2fc499SJeff Garzik 
1334777baa47SStephen Hemminger 	net->netdev_ops = &usbnet_netdev_ops;
1335777baa47SStephen Hemminger 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
13365b2fc499SJeff Garzik 	net->ethtool_ops = &usbnet_ethtool_ops;
13375b2fc499SJeff Garzik 
13385b2fc499SJeff Garzik 	// allow device-specific bind/init procedures
13395b2fc499SJeff Garzik 	// NOTE net->name still not usable ...
13405b2fc499SJeff Garzik 	if (info->bind) {
13415b2fc499SJeff Garzik 		status = info->bind (dev, udev);
13425b2fc499SJeff Garzik 		if (status < 0)
13435b2fc499SJeff Garzik 			goto out1;
13445b2fc499SJeff Garzik 
13455b2fc499SJeff Garzik 		// heuristic:  "usb%d" for links we know are two-host,
13465b2fc499SJeff Garzik 		// else "eth%d" when there's reasonable doubt.  userspace
13475b2fc499SJeff Garzik 		// can rename the link if it knows better.
13488e95a202SJoe Perches 		if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
13498e95a202SJoe Perches 		    (net->dev_addr [0] & 0x02) == 0)
13505b2fc499SJeff Garzik 			strcpy (net->name, "eth%d");
13516e3bbcc5SJussi Kivilinna 		/* WLAN devices should always be named "wlan%d" */
13526e3bbcc5SJussi Kivilinna 		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
13536e3bbcc5SJussi Kivilinna 			strcpy(net->name, "wlan%d");
1354e1e499eeSMarcel Holtmann 		/* WWAN devices should always be named "wwan%d" */
1355e1e499eeSMarcel Holtmann 		if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1356e1e499eeSMarcel Holtmann 			strcpy(net->name, "wwan%d");
13575b2fc499SJeff Garzik 
13585b2fc499SJeff Garzik 		/* maybe the remote can't receive an Ethernet MTU */
13595b2fc499SJeff Garzik 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
13605b2fc499SJeff Garzik 			net->mtu = dev->hard_mtu - net->hard_header_len;
13615b2fc499SJeff Garzik 	} else if (!info->in || !info->out)
13625b2fc499SJeff Garzik 		status = usbnet_get_endpoints (dev, udev);
13635b2fc499SJeff Garzik 	else {
13645b2fc499SJeff Garzik 		dev->in = usb_rcvbulkpipe (xdev, info->in);
13655b2fc499SJeff Garzik 		dev->out = usb_sndbulkpipe (xdev, info->out);
13665b2fc499SJeff Garzik 		if (!(info->flags & FLAG_NO_SETINT))
13675b2fc499SJeff Garzik 			status = usb_set_interface (xdev,
13685b2fc499SJeff Garzik 				interface->desc.bInterfaceNumber,
13695b2fc499SJeff Garzik 				interface->desc.bAlternateSetting);
13705b2fc499SJeff Garzik 		else
13715b2fc499SJeff Garzik 			status = 0;
13725b2fc499SJeff Garzik 
13735b2fc499SJeff Garzik 	}
13749514bfe5SPeter Korsgaard 	if (status >= 0 && dev->status)
13755b2fc499SJeff Garzik 		status = init_status (dev, udev);
13765b2fc499SJeff Garzik 	if (status < 0)
13775b2fc499SJeff Garzik 		goto out3;
13785b2fc499SJeff Garzik 
13795b2fc499SJeff Garzik 	if (!dev->rx_urb_size)
13805b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
13815b2fc499SJeff Garzik 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
13825b2fc499SJeff Garzik 
1383225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1384225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wlan_type);
1385225794f8SMarcel Holtmann 	if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1386225794f8SMarcel Holtmann 		SET_NETDEV_DEVTYPE(net, &wwan_type);
1387225794f8SMarcel Holtmann 
13885b2fc499SJeff Garzik 	status = register_netdev (net);
13895b2fc499SJeff Garzik 	if (status)
13905b2fc499SJeff Garzik 		goto out3;
1391a475f603SJoe Perches 	netif_info(dev, probe, dev->net,
1392a475f603SJoe Perches 		   "register '%s' at usb-%s-%s, %s, %pM\n",
13935b2fc499SJeff Garzik 		   udev->dev.driver->name,
13945b2fc499SJeff Garzik 		   xdev->bus->bus_name, xdev->devpath,
13955b2fc499SJeff Garzik 		   dev->driver_info->description,
1396e174961cSJohannes Berg 		   net->dev_addr);
13975b2fc499SJeff Garzik 
13985b2fc499SJeff Garzik 	// ok, it's ready to go.
13995b2fc499SJeff Garzik 	usb_set_intfdata (udev, dev);
14005b2fc499SJeff Garzik 
14015b2fc499SJeff Garzik 	netif_device_attach (net);
14025b2fc499SJeff Garzik 
140337e8273cSBen Hutchings 	if (dev->driver_info->flags & FLAG_LINK_INTR)
140437e8273cSBen Hutchings 		netif_carrier_off(net);
140537e8273cSBen Hutchings 
14065b2fc499SJeff Garzik 	return 0;
14075b2fc499SJeff Garzik 
14085b2fc499SJeff Garzik out3:
14095b2fc499SJeff Garzik 	if (info->unbind)
14105b2fc499SJeff Garzik 		info->unbind (dev, udev);
14115b2fc499SJeff Garzik out1:
14125b2fc499SJeff Garzik 	free_netdev(net);
14135b2fc499SJeff Garzik out:
14145b2fc499SJeff Garzik 	usb_put_dev(xdev);
14155b2fc499SJeff Garzik 	return status;
14165b2fc499SJeff Garzik }
14175b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
14185b2fc499SJeff Garzik 
14195b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
14205b2fc499SJeff Garzik 
142136433127SOliver Neukum /*
142236433127SOliver Neukum  * suspend the whole driver as soon as the first interface is suspended
142336433127SOliver Neukum  * resume only when the last interface is resumed
14245b2fc499SJeff Garzik  */
14255b2fc499SJeff Garzik 
14265b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
14275b2fc499SJeff Garzik {
14285b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
14295b2fc499SJeff Garzik 
143036433127SOliver Neukum 	if (!dev->suspend_count++) {
143169ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
143269ee472fSOliver Neukum 		/* don't autosuspend while transmitting */
143369ee472fSOliver Neukum 		if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) {
143469ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
143569ee472fSOliver Neukum 			return -EBUSY;
143669ee472fSOliver Neukum 		} else {
143769ee472fSOliver Neukum 			set_bit(EVENT_DEV_ASLEEP, &dev->flags);
143869ee472fSOliver Neukum 			spin_unlock_irq(&dev->txq.lock);
143969ee472fSOliver Neukum 		}
1440a11a6544SOliver Neukum 		/*
1441a11a6544SOliver Neukum 		 * accelerate emptying of the rx and queues, to avoid
14425b2fc499SJeff Garzik 		 * having everything error out.
14435b2fc499SJeff Garzik 		 */
14445b2fc499SJeff Garzik 		netif_device_detach (dev->net);
144569ee472fSOliver Neukum 		usbnet_terminate_urbs(dev);
144669ee472fSOliver Neukum 		usb_kill_urb(dev->interrupt);
144769ee472fSOliver Neukum 
1448a11a6544SOliver Neukum 		/*
1449a11a6544SOliver Neukum 		 * reattach so runtime management can use and
1450a11a6544SOliver Neukum 		 * wake the device
1451a11a6544SOliver Neukum 		 */
1452a11a6544SOliver Neukum 		netif_device_attach (dev->net);
145336433127SOliver Neukum 	}
14545b2fc499SJeff Garzik 	return 0;
14555b2fc499SJeff Garzik }
14565b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
14575b2fc499SJeff Garzik 
14585b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
14595b2fc499SJeff Garzik {
14605b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
146169ee472fSOliver Neukum 	struct sk_buff          *skb;
146269ee472fSOliver Neukum 	struct urb              *res;
146369ee472fSOliver Neukum 	int                     retval;
14645b2fc499SJeff Garzik 
146569ee472fSOliver Neukum 	if (!--dev->suspend_count) {
146669ee472fSOliver Neukum 		spin_lock_irq(&dev->txq.lock);
146769ee472fSOliver Neukum 		while ((res = usb_get_from_anchor(&dev->deferred))) {
146869ee472fSOliver Neukum 
146969ee472fSOliver Neukum 			printk(KERN_INFO"%s has delayed data\n", __func__);
147069ee472fSOliver Neukum 			skb = (struct sk_buff *)res->context;
147169ee472fSOliver Neukum 			retval = usb_submit_urb(res, GFP_ATOMIC);
147269ee472fSOliver Neukum 			if (retval < 0) {
147369ee472fSOliver Neukum 				dev_kfree_skb_any(skb);
147469ee472fSOliver Neukum 				usb_free_urb(res);
147569ee472fSOliver Neukum 				usb_autopm_put_interface_async(dev->intf);
147669ee472fSOliver Neukum 			} else {
147769ee472fSOliver Neukum 				dev->net->trans_start = jiffies;
147869ee472fSOliver Neukum 				__skb_queue_tail(&dev->txq, skb);
147969ee472fSOliver Neukum 			}
148069ee472fSOliver Neukum 		}
148169ee472fSOliver Neukum 
148269ee472fSOliver Neukum 		smp_mb();
148369ee472fSOliver Neukum 		clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
148469ee472fSOliver Neukum 		spin_unlock_irq(&dev->txq.lock);
148569ee472fSOliver Neukum 		if (!(dev->txq.qlen >= TX_QLEN(dev)))
148669ee472fSOliver Neukum 			netif_start_queue(dev->net);
14875b2fc499SJeff Garzik 		tasklet_schedule (&dev->bh);
148869ee472fSOliver Neukum 	}
14895b2fc499SJeff Garzik 	return 0;
14905b2fc499SJeff Garzik }
14915b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
14925b2fc499SJeff Garzik 
14935b2fc499SJeff Garzik 
14945b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
14955b2fc499SJeff Garzik 
14965b2fc499SJeff Garzik static int __init usbnet_init(void)
14975b2fc499SJeff Garzik {
14985b2fc499SJeff Garzik 	/* compiler should optimize this out */
14995b2fc499SJeff Garzik 	BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
15005b2fc499SJeff Garzik 			< sizeof (struct skb_data));
15015b2fc499SJeff Garzik 
15025b2fc499SJeff Garzik 	random_ether_addr(node_id);
15035b2fc499SJeff Garzik 	return 0;
15045b2fc499SJeff Garzik }
15055b2fc499SJeff Garzik module_init(usbnet_init);
15065b2fc499SJeff Garzik 
15075b2fc499SJeff Garzik static void __exit usbnet_exit(void)
15085b2fc499SJeff Garzik {
15095b2fc499SJeff Garzik }
15105b2fc499SJeff Garzik module_exit(usbnet_exit);
15115b2fc499SJeff Garzik 
15125b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
15135b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
15145b2fc499SJeff Garzik MODULE_LICENSE("GPL");
1515