xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision 6a35528a8346f6e6fd32ed7e51f04d1fa4ca2c01)
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>
405b2fc499SJeff Garzik #include <linux/ethtool.h>
415b2fc499SJeff Garzik #include <linux/workqueue.h>
425b2fc499SJeff Garzik #include <linux/mii.h>
435b2fc499SJeff Garzik #include <linux/usb.h>
443692e94fSJussi Kivilinna #include <linux/usb/usbnet.h>
455b2fc499SJeff Garzik 
465b2fc499SJeff Garzik #define DRIVER_VERSION		"22-Aug-2005"
475b2fc499SJeff Garzik 
485b2fc499SJeff Garzik 
495b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
505b2fc499SJeff Garzik 
515b2fc499SJeff Garzik /*
525b2fc499SJeff Garzik  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
535b2fc499SJeff Garzik  * Several dozen bytes of IPv4 data can fit in two such transactions.
545b2fc499SJeff Garzik  * One maximum size Ethernet packet takes twenty four of them.
555b2fc499SJeff Garzik  * For high speed, each frame comfortably fits almost 36 max size
565b2fc499SJeff Garzik  * Ethernet packets (so queues should be bigger).
575b2fc499SJeff Garzik  *
585b2fc499SJeff Garzik  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
595b2fc499SJeff Garzik  * let the USB host controller be busy for 5msec or more before an irq
605b2fc499SJeff Garzik  * is required, under load.  Jumbograms change the equation.
615b2fc499SJeff Garzik  */
625b2fc499SJeff Garzik #define RX_MAX_QUEUE_MEMORY (60 * 1518)
635b2fc499SJeff Garzik #define	RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
645b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
655b2fc499SJeff Garzik #define	TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
665b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
675b2fc499SJeff Garzik 
685b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
695b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES	(5*HZ)
705b2fc499SJeff Garzik 
715b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect()
725b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors.
735b2fc499SJeff Garzik #define THROTTLE_JIFFIES	(HZ/8)
745b2fc499SJeff Garzik 
755b2fc499SJeff Garzik // between wakeups
765b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS	3
775b2fc499SJeff Garzik 
785b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
795b2fc499SJeff Garzik 
805b2fc499SJeff Garzik // randomly generated ethernet address
815b2fc499SJeff Garzik static u8	node_id [ETH_ALEN];
825b2fc499SJeff Garzik 
835b2fc499SJeff Garzik static const char driver_name [] = "usbnet";
845b2fc499SJeff Garzik 
855b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
865b2fc499SJeff Garzik static int msg_level = -1;
875b2fc499SJeff Garzik module_param (msg_level, int, 0);
885b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
895b2fc499SJeff Garzik 
905b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
915b2fc499SJeff Garzik 
925b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
935b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
945b2fc499SJeff Garzik {
955b2fc499SJeff Garzik 	int				tmp;
965b2fc499SJeff Garzik 	struct usb_host_interface	*alt = NULL;
975b2fc499SJeff Garzik 	struct usb_host_endpoint	*in = NULL, *out = NULL;
985b2fc499SJeff Garzik 	struct usb_host_endpoint	*status = NULL;
995b2fc499SJeff Garzik 
1005b2fc499SJeff Garzik 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
1015b2fc499SJeff Garzik 		unsigned	ep;
1025b2fc499SJeff Garzik 
1035b2fc499SJeff Garzik 		in = out = status = NULL;
1045b2fc499SJeff Garzik 		alt = intf->altsetting + tmp;
1055b2fc499SJeff Garzik 
1065b2fc499SJeff Garzik 		/* take the first altsetting with in-bulk + out-bulk;
1075b2fc499SJeff Garzik 		 * remember any status endpoint, just in case;
1085b2fc499SJeff Garzik 		 * ignore other endpoints and altsetttings.
1095b2fc499SJeff Garzik 		 */
1105b2fc499SJeff Garzik 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
1115b2fc499SJeff Garzik 			struct usb_host_endpoint	*e;
1125b2fc499SJeff Garzik 			int				intr = 0;
1135b2fc499SJeff Garzik 
1145b2fc499SJeff Garzik 			e = alt->endpoint + ep;
1155b2fc499SJeff Garzik 			switch (e->desc.bmAttributes) {
1165b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_INT:
1175b2fc499SJeff Garzik 				if (!usb_endpoint_dir_in(&e->desc))
1185b2fc499SJeff Garzik 					continue;
1195b2fc499SJeff Garzik 				intr = 1;
1205b2fc499SJeff Garzik 				/* FALLTHROUGH */
1215b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_BULK:
1225b2fc499SJeff Garzik 				break;
1235b2fc499SJeff Garzik 			default:
1245b2fc499SJeff Garzik 				continue;
1255b2fc499SJeff Garzik 			}
1265b2fc499SJeff Garzik 			if (usb_endpoint_dir_in(&e->desc)) {
1275b2fc499SJeff Garzik 				if (!intr && !in)
1285b2fc499SJeff Garzik 					in = e;
1295b2fc499SJeff Garzik 				else if (intr && !status)
1305b2fc499SJeff Garzik 					status = e;
1315b2fc499SJeff Garzik 			} else {
1325b2fc499SJeff Garzik 				if (!out)
1335b2fc499SJeff Garzik 					out = e;
1345b2fc499SJeff Garzik 			}
1355b2fc499SJeff Garzik 		}
1365b2fc499SJeff Garzik 		if (in && out)
1375b2fc499SJeff Garzik 			break;
1385b2fc499SJeff Garzik 	}
1395b2fc499SJeff Garzik 	if (!alt || !in || !out)
1405b2fc499SJeff Garzik 		return -EINVAL;
1415b2fc499SJeff Garzik 
1425b2fc499SJeff Garzik 	if (alt->desc.bAlternateSetting != 0
1435b2fc499SJeff Garzik 			|| !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1445b2fc499SJeff Garzik 		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
1455b2fc499SJeff Garzik 				alt->desc.bAlternateSetting);
1465b2fc499SJeff Garzik 		if (tmp < 0)
1475b2fc499SJeff Garzik 			return tmp;
1485b2fc499SJeff Garzik 	}
1495b2fc499SJeff Garzik 
1505b2fc499SJeff Garzik 	dev->in = usb_rcvbulkpipe (dev->udev,
1515b2fc499SJeff Garzik 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1525b2fc499SJeff Garzik 	dev->out = usb_sndbulkpipe (dev->udev,
1535b2fc499SJeff Garzik 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1545b2fc499SJeff Garzik 	dev->status = status;
1555b2fc499SJeff Garzik 	return 0;
1565b2fc499SJeff Garzik }
1575b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1585b2fc499SJeff Garzik 
1595b2fc499SJeff Garzik static void intr_complete (struct urb *urb);
1605b2fc499SJeff Garzik 
1615b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
1625b2fc499SJeff Garzik {
1635b2fc499SJeff Garzik 	char		*buf = NULL;
1645b2fc499SJeff Garzik 	unsigned	pipe = 0;
1655b2fc499SJeff Garzik 	unsigned	maxp;
1665b2fc499SJeff Garzik 	unsigned	period;
1675b2fc499SJeff Garzik 
1685b2fc499SJeff Garzik 	if (!dev->driver_info->status)
1695b2fc499SJeff Garzik 		return 0;
1705b2fc499SJeff Garzik 
1715b2fc499SJeff Garzik 	pipe = usb_rcvintpipe (dev->udev,
1725b2fc499SJeff Garzik 			dev->status->desc.bEndpointAddress
1735b2fc499SJeff Garzik 				& USB_ENDPOINT_NUMBER_MASK);
1745b2fc499SJeff Garzik 	maxp = usb_maxpacket (dev->udev, pipe, 0);
1755b2fc499SJeff Garzik 
1765b2fc499SJeff Garzik 	/* avoid 1 msec chatter:  min 8 msec poll rate */
1775b2fc499SJeff Garzik 	period = max ((int) dev->status->desc.bInterval,
1785b2fc499SJeff Garzik 		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
1795b2fc499SJeff Garzik 
1805b2fc499SJeff Garzik 	buf = kmalloc (maxp, GFP_KERNEL);
1815b2fc499SJeff Garzik 	if (buf) {
1825b2fc499SJeff Garzik 		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
1835b2fc499SJeff Garzik 		if (!dev->interrupt) {
1845b2fc499SJeff Garzik 			kfree (buf);
1855b2fc499SJeff Garzik 			return -ENOMEM;
1865b2fc499SJeff Garzik 		} else {
1875b2fc499SJeff Garzik 			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
1885b2fc499SJeff Garzik 				buf, maxp, intr_complete, dev, period);
1895b2fc499SJeff Garzik 			dev_dbg(&intf->dev,
1905b2fc499SJeff Garzik 				"status ep%din, %d bytes period %d\n",
1915b2fc499SJeff Garzik 				usb_pipeendpoint(pipe), maxp, period);
1925b2fc499SJeff Garzik 		}
1935b2fc499SJeff Garzik 	}
1945b2fc499SJeff Garzik 	return 0;
1955b2fc499SJeff Garzik }
1965b2fc499SJeff Garzik 
1975b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
1985b2fc499SJeff Garzik  * Some link protocols batch packets, so their rx_fixup paths
1995b2fc499SJeff Garzik  * can return clones as well as just modify the original skb.
2005b2fc499SJeff Garzik  */
2015b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
2025b2fc499SJeff Garzik {
2035b2fc499SJeff Garzik 	int	status;
2045b2fc499SJeff Garzik 
2055b2fc499SJeff Garzik 	skb->protocol = eth_type_trans (skb, dev->net);
2065b2fc499SJeff Garzik 	dev->stats.rx_packets++;
2075b2fc499SJeff Garzik 	dev->stats.rx_bytes += skb->len;
2085b2fc499SJeff Garzik 
2095b2fc499SJeff Garzik 	if (netif_msg_rx_status (dev))
2105b2fc499SJeff Garzik 		devdbg (dev, "< rx, len %zu, type 0x%x",
2115b2fc499SJeff Garzik 			skb->len + sizeof (struct ethhdr), skb->protocol);
2125b2fc499SJeff Garzik 	memset (skb->cb, 0, sizeof (struct skb_data));
2135b2fc499SJeff Garzik 	status = netif_rx (skb);
2145b2fc499SJeff Garzik 	if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
2155b2fc499SJeff Garzik 		devdbg (dev, "netif_rx status %d", status);
2165b2fc499SJeff Garzik }
2175b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
2185b2fc499SJeff Garzik 
2195b2fc499SJeff Garzik 
2205b2fc499SJeff Garzik /*-------------------------------------------------------------------------
2215b2fc499SJeff Garzik  *
2225b2fc499SJeff Garzik  * Network Device Driver (peer link to "Host Device", from USB host)
2235b2fc499SJeff Garzik  *
2245b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
2255b2fc499SJeff Garzik 
226777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu)
2275b2fc499SJeff Garzik {
2285b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
2295b2fc499SJeff Garzik 	int		ll_mtu = new_mtu + net->hard_header_len;
2305b2fc499SJeff Garzik 	int		old_hard_mtu = dev->hard_mtu;
2315b2fc499SJeff Garzik 	int		old_rx_urb_size = dev->rx_urb_size;
2325b2fc499SJeff Garzik 
2335b2fc499SJeff Garzik 	if (new_mtu <= 0)
2345b2fc499SJeff Garzik 		return -EINVAL;
2355b2fc499SJeff Garzik 	// no second zero-length packet read wanted after mtu-sized packets
2365b2fc499SJeff Garzik 	if ((ll_mtu % dev->maxpacket) == 0)
2375b2fc499SJeff Garzik 		return -EDOM;
2385b2fc499SJeff Garzik 	net->mtu = new_mtu;
2395b2fc499SJeff Garzik 
2405b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
2415b2fc499SJeff Garzik 	if (dev->rx_urb_size == old_hard_mtu) {
2425b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
2435b2fc499SJeff Garzik 		if (dev->rx_urb_size > old_rx_urb_size)
2445b2fc499SJeff Garzik 			usbnet_unlink_rx_urbs(dev);
2455b2fc499SJeff Garzik 	}
2465b2fc499SJeff Garzik 
2475b2fc499SJeff Garzik 	return 0;
2485b2fc499SJeff Garzik }
249777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu);
2505b2fc499SJeff Garzik 
2515b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
2525b2fc499SJeff Garzik 
2535b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
2545b2fc499SJeff Garzik  * completion callbacks.  2.5 should have fixed those bugs...
2555b2fc499SJeff Garzik  */
2565b2fc499SJeff Garzik 
2575b2fc499SJeff Garzik static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
2585b2fc499SJeff Garzik {
2595b2fc499SJeff Garzik 	unsigned long		flags;
2605b2fc499SJeff Garzik 
2615b2fc499SJeff Garzik 	spin_lock_irqsave(&list->lock, flags);
2625b2fc499SJeff Garzik 	__skb_unlink(skb, list);
2635b2fc499SJeff Garzik 	spin_unlock(&list->lock);
2645b2fc499SJeff Garzik 	spin_lock(&dev->done.lock);
2655b2fc499SJeff Garzik 	__skb_queue_tail(&dev->done, skb);
2665b2fc499SJeff Garzik 	if (dev->done.qlen == 1)
2675b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
2685b2fc499SJeff Garzik 	spin_unlock_irqrestore(&dev->done.lock, flags);
2695b2fc499SJeff Garzik }
2705b2fc499SJeff Garzik 
2715b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
2725b2fc499SJeff Garzik  *
2735b2fc499SJeff Garzik  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
2745b2fc499SJeff Garzik  * but tasklet_schedule() doesn't.  hope the failure is rare.
2755b2fc499SJeff Garzik  */
2765b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
2775b2fc499SJeff Garzik {
2785b2fc499SJeff Garzik 	set_bit (work, &dev->flags);
2795b2fc499SJeff Garzik 	if (!schedule_work (&dev->kevent))
2805b2fc499SJeff Garzik 		deverr (dev, "kevent %d may have been dropped", work);
2815b2fc499SJeff Garzik 	else
2825b2fc499SJeff Garzik 		devdbg (dev, "kevent %d scheduled", work);
2835b2fc499SJeff Garzik }
2845b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
2855b2fc499SJeff Garzik 
2865b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
2875b2fc499SJeff Garzik 
2885b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
2895b2fc499SJeff Garzik 
2905b2fc499SJeff Garzik static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
2915b2fc499SJeff Garzik {
2925b2fc499SJeff Garzik 	struct sk_buff		*skb;
2935b2fc499SJeff Garzik 	struct skb_data		*entry;
2945b2fc499SJeff Garzik 	int			retval = 0;
2955b2fc499SJeff Garzik 	unsigned long		lockflags;
2965b2fc499SJeff Garzik 	size_t			size = dev->rx_urb_size;
2975b2fc499SJeff Garzik 
2985b2fc499SJeff Garzik 	if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
2995b2fc499SJeff Garzik 		if (netif_msg_rx_err (dev))
3005b2fc499SJeff Garzik 			devdbg (dev, "no rx skb");
3015b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
3025b2fc499SJeff Garzik 		usb_free_urb (urb);
3035b2fc499SJeff Garzik 		return;
3045b2fc499SJeff Garzik 	}
3055b2fc499SJeff Garzik 	skb_reserve (skb, NET_IP_ALIGN);
3065b2fc499SJeff Garzik 
3075b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
3085b2fc499SJeff Garzik 	entry->urb = urb;
3095b2fc499SJeff Garzik 	entry->dev = dev;
3105b2fc499SJeff Garzik 	entry->state = rx_start;
3115b2fc499SJeff Garzik 	entry->length = 0;
3125b2fc499SJeff Garzik 
3135b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
3145b2fc499SJeff Garzik 		skb->data, size, rx_complete, skb);
3155b2fc499SJeff Garzik 
3165b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
3175b2fc499SJeff Garzik 
3185b2fc499SJeff Garzik 	if (netif_running (dev->net)
3195b2fc499SJeff Garzik 			&& netif_device_present (dev->net)
3205b2fc499SJeff Garzik 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
3215b2fc499SJeff Garzik 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
3225b2fc499SJeff Garzik 		case -EPIPE:
3235b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
3245b2fc499SJeff Garzik 			break;
3255b2fc499SJeff Garzik 		case -ENOMEM:
3265b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
3275b2fc499SJeff Garzik 			break;
3285b2fc499SJeff Garzik 		case -ENODEV:
3295b2fc499SJeff Garzik 			if (netif_msg_ifdown (dev))
3305b2fc499SJeff Garzik 				devdbg (dev, "device gone");
3315b2fc499SJeff Garzik 			netif_device_detach (dev->net);
3325b2fc499SJeff Garzik 			break;
3335b2fc499SJeff Garzik 		default:
3345b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
3355b2fc499SJeff Garzik 				devdbg (dev, "rx submit, %d", retval);
3365b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
3375b2fc499SJeff Garzik 			break;
3385b2fc499SJeff Garzik 		case 0:
3395b2fc499SJeff Garzik 			__skb_queue_tail (&dev->rxq, skb);
3405b2fc499SJeff Garzik 		}
3415b2fc499SJeff Garzik 	} else {
3425b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
3435b2fc499SJeff Garzik 			devdbg (dev, "rx: stopped");
3445b2fc499SJeff Garzik 		retval = -ENOLINK;
3455b2fc499SJeff Garzik 	}
3465b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
3475b2fc499SJeff Garzik 	if (retval) {
3485b2fc499SJeff Garzik 		dev_kfree_skb_any (skb);
3495b2fc499SJeff Garzik 		usb_free_urb (urb);
3505b2fc499SJeff Garzik 	}
3515b2fc499SJeff Garzik }
3525b2fc499SJeff Garzik 
3535b2fc499SJeff Garzik 
3545b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
3555b2fc499SJeff Garzik 
3565b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
3575b2fc499SJeff Garzik {
3585b2fc499SJeff Garzik 	if (dev->driver_info->rx_fixup
3595b2fc499SJeff Garzik 			&& !dev->driver_info->rx_fixup (dev, skb))
3605b2fc499SJeff Garzik 		goto error;
3615b2fc499SJeff Garzik 	// else network stack removes extra byte if we forced a short packet
3625b2fc499SJeff Garzik 
3635b2fc499SJeff Garzik 	if (skb->len)
3645b2fc499SJeff Garzik 		usbnet_skb_return (dev, skb);
3655b2fc499SJeff Garzik 	else {
3665b2fc499SJeff Garzik 		if (netif_msg_rx_err (dev))
3675b2fc499SJeff Garzik 			devdbg (dev, "drop");
3685b2fc499SJeff Garzik error:
3695b2fc499SJeff Garzik 		dev->stats.rx_errors++;
3705b2fc499SJeff Garzik 		skb_queue_tail (&dev->done, skb);
3715b2fc499SJeff Garzik 	}
3725b2fc499SJeff Garzik }
3735b2fc499SJeff Garzik 
3745b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
3755b2fc499SJeff Garzik 
3765b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
3775b2fc499SJeff Garzik {
3785b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
3795b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
3805b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
3815b2fc499SJeff Garzik 	int			urb_status = urb->status;
3825b2fc499SJeff Garzik 
3835b2fc499SJeff Garzik 	skb_put (skb, urb->actual_length);
3845b2fc499SJeff Garzik 	entry->state = rx_done;
3855b2fc499SJeff Garzik 	entry->urb = NULL;
3865b2fc499SJeff Garzik 
3875b2fc499SJeff Garzik 	switch (urb_status) {
38818ab458fSDavid Brownell 	/* success */
3895b2fc499SJeff Garzik 	case 0:
3905b2fc499SJeff Garzik 		if (skb->len < dev->net->hard_header_len) {
3915b2fc499SJeff Garzik 			entry->state = rx_cleanup;
3925b2fc499SJeff Garzik 			dev->stats.rx_errors++;
3935b2fc499SJeff Garzik 			dev->stats.rx_length_errors++;
3945b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
3955b2fc499SJeff Garzik 				devdbg (dev, "rx length %d", skb->len);
3965b2fc499SJeff Garzik 		}
3975b2fc499SJeff Garzik 		break;
3985b2fc499SJeff Garzik 
39918ab458fSDavid Brownell 	/* stalls need manual reset. this is rare ... except that
40018ab458fSDavid Brownell 	 * when going through USB 2.0 TTs, unplug appears this way.
40118ab458fSDavid Brownell 	 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
40218ab458fSDavid Brownell 	 * storm, recovering as needed.
40318ab458fSDavid Brownell 	 */
4045b2fc499SJeff Garzik 	case -EPIPE:
4055b2fc499SJeff Garzik 		dev->stats.rx_errors++;
4065b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
4075b2fc499SJeff Garzik 		// FALLTHROUGH
4085b2fc499SJeff Garzik 
40918ab458fSDavid Brownell 	/* software-driven interface shutdown */
41018ab458fSDavid Brownell 	case -ECONNRESET:		/* async unlink */
41118ab458fSDavid Brownell 	case -ESHUTDOWN:		/* hardware gone */
4125b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
4135b2fc499SJeff Garzik 			devdbg (dev, "rx shutdown, code %d", urb_status);
4145b2fc499SJeff Garzik 		goto block;
4155b2fc499SJeff Garzik 
41618ab458fSDavid Brownell 	/* we get controller i/o faults during khubd disconnect() delays.
41718ab458fSDavid Brownell 	 * throttle down resubmits, to avoid log floods; just temporarily,
41818ab458fSDavid Brownell 	 * so we still recover when the fault isn't a khubd delay.
41918ab458fSDavid Brownell 	 */
4205b2fc499SJeff Garzik 	case -EPROTO:
4215b2fc499SJeff Garzik 	case -ETIME:
4225b2fc499SJeff Garzik 	case -EILSEQ:
4235b2fc499SJeff Garzik 		dev->stats.rx_errors++;
4245b2fc499SJeff Garzik 		if (!timer_pending (&dev->delay)) {
4255b2fc499SJeff Garzik 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
4265b2fc499SJeff Garzik 			if (netif_msg_link (dev))
4275b2fc499SJeff Garzik 				devdbg (dev, "rx throttle %d", urb_status);
4285b2fc499SJeff Garzik 		}
4295b2fc499SJeff Garzik block:
4305b2fc499SJeff Garzik 		entry->state = rx_cleanup;
4315b2fc499SJeff Garzik 		entry->urb = urb;
4325b2fc499SJeff Garzik 		urb = NULL;
4335b2fc499SJeff Garzik 		break;
4345b2fc499SJeff Garzik 
43518ab458fSDavid Brownell 	/* data overrun ... flush fifo? */
4365b2fc499SJeff Garzik 	case -EOVERFLOW:
4375b2fc499SJeff Garzik 		dev->stats.rx_over_errors++;
4385b2fc499SJeff Garzik 		// FALLTHROUGH
4395b2fc499SJeff Garzik 
4405b2fc499SJeff Garzik 	default:
4415b2fc499SJeff Garzik 		entry->state = rx_cleanup;
4425b2fc499SJeff Garzik 		dev->stats.rx_errors++;
4435b2fc499SJeff Garzik 		if (netif_msg_rx_err (dev))
4445b2fc499SJeff Garzik 			devdbg (dev, "rx status %d", urb_status);
4455b2fc499SJeff Garzik 		break;
4465b2fc499SJeff Garzik 	}
4475b2fc499SJeff Garzik 
4485b2fc499SJeff Garzik 	defer_bh(dev, skb, &dev->rxq);
4495b2fc499SJeff Garzik 
4505b2fc499SJeff Garzik 	if (urb) {
4515b2fc499SJeff Garzik 		if (netif_running (dev->net)
4525b2fc499SJeff Garzik 				&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
4535b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_ATOMIC);
4545b2fc499SJeff Garzik 			return;
4555b2fc499SJeff Garzik 		}
4565b2fc499SJeff Garzik 		usb_free_urb (urb);
4575b2fc499SJeff Garzik 	}
4585b2fc499SJeff Garzik 	if (netif_msg_rx_err (dev))
4595b2fc499SJeff Garzik 		devdbg (dev, "no read resubmitted");
4605b2fc499SJeff Garzik }
4615b2fc499SJeff Garzik 
4625b2fc499SJeff Garzik static void intr_complete (struct urb *urb)
4635b2fc499SJeff Garzik {
4645b2fc499SJeff Garzik 	struct usbnet	*dev = urb->context;
4655b2fc499SJeff Garzik 	int		status = urb->status;
4665b2fc499SJeff Garzik 
4675b2fc499SJeff Garzik 	switch (status) {
4685b2fc499SJeff Garzik 	/* success */
4695b2fc499SJeff Garzik 	case 0:
4705b2fc499SJeff Garzik 		dev->driver_info->status(dev, urb);
4715b2fc499SJeff Garzik 		break;
4725b2fc499SJeff Garzik 
4735b2fc499SJeff Garzik 	/* software-driven interface shutdown */
47418ab458fSDavid Brownell 	case -ENOENT:		/* urb killed */
47518ab458fSDavid Brownell 	case -ESHUTDOWN:	/* hardware gone */
4765b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
4775b2fc499SJeff Garzik 			devdbg (dev, "intr shutdown, code %d", status);
4785b2fc499SJeff Garzik 		return;
4795b2fc499SJeff Garzik 
4805b2fc499SJeff Garzik 	/* NOTE:  not throttling like RX/TX, since this endpoint
4815b2fc499SJeff Garzik 	 * already polls infrequently
4825b2fc499SJeff Garzik 	 */
4835b2fc499SJeff Garzik 	default:
4845b2fc499SJeff Garzik 		devdbg (dev, "intr status %d", status);
4855b2fc499SJeff Garzik 		break;
4865b2fc499SJeff Garzik 	}
4875b2fc499SJeff Garzik 
4885b2fc499SJeff Garzik 	if (!netif_running (dev->net))
4895b2fc499SJeff Garzik 		return;
4905b2fc499SJeff Garzik 
4915b2fc499SJeff Garzik 	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
4925b2fc499SJeff Garzik 	status = usb_submit_urb (urb, GFP_ATOMIC);
4935b2fc499SJeff Garzik 	if (status != 0 && netif_msg_timer (dev))
4945b2fc499SJeff Garzik 		deverr(dev, "intr resubmit --> %d", status);
4955b2fc499SJeff Garzik }
4965b2fc499SJeff Garzik 
4975b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4985b2fc499SJeff Garzik 
4995b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
5005b2fc499SJeff Garzik 
5015b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
5025b2fc499SJeff Garzik {
5035b2fc499SJeff Garzik 	unsigned long		flags;
5045b2fc499SJeff Garzik 	struct sk_buff		*skb, *skbnext;
5055b2fc499SJeff Garzik 	int			count = 0;
5065b2fc499SJeff Garzik 
5075b2fc499SJeff Garzik 	spin_lock_irqsave (&q->lock, flags);
50883bfba5fSDavid S. Miller 	skb_queue_walk_safe(q, skb, skbnext) {
5095b2fc499SJeff Garzik 		struct skb_data		*entry;
5105b2fc499SJeff Garzik 		struct urb		*urb;
5115b2fc499SJeff Garzik 		int			retval;
5125b2fc499SJeff Garzik 
5135b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
5145b2fc499SJeff Garzik 		urb = entry->urb;
5155b2fc499SJeff Garzik 
5165b2fc499SJeff Garzik 		// during some PM-driven resume scenarios,
5175b2fc499SJeff Garzik 		// these (async) unlinks complete immediately
5185b2fc499SJeff Garzik 		retval = usb_unlink_urb (urb);
5195b2fc499SJeff Garzik 		if (retval != -EINPROGRESS && retval != 0)
5205b2fc499SJeff Garzik 			devdbg (dev, "unlink urb err, %d", retval);
5215b2fc499SJeff Garzik 		else
5225b2fc499SJeff Garzik 			count++;
5235b2fc499SJeff Garzik 	}
5245b2fc499SJeff Garzik 	spin_unlock_irqrestore (&q->lock, flags);
5255b2fc499SJeff Garzik 	return count;
5265b2fc499SJeff Garzik }
5275b2fc499SJeff Garzik 
5285b2fc499SJeff Garzik // Flush all pending rx urbs
5295b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
5305b2fc499SJeff Garzik 
5315b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
5325b2fc499SJeff Garzik {
5335b2fc499SJeff Garzik 	if (netif_running(dev->net)) {
5345b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
5355b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
5365b2fc499SJeff Garzik 	}
5375b2fc499SJeff Garzik }
5385b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
5395b2fc499SJeff Garzik 
5405b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5415b2fc499SJeff Garzik 
5425b2fc499SJeff Garzik // precondition: never called in_interrupt
5435b2fc499SJeff Garzik 
544777baa47SStephen Hemminger int usbnet_stop (struct net_device *net)
5455b2fc499SJeff Garzik {
5465b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
5475b2fc499SJeff Garzik 	int			temp;
5485b2fc499SJeff Garzik 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
5495b2fc499SJeff Garzik 	DECLARE_WAITQUEUE (wait, current);
5505b2fc499SJeff Garzik 
5515b2fc499SJeff Garzik 	netif_stop_queue (net);
5525b2fc499SJeff Garzik 
5535b2fc499SJeff Garzik 	if (netif_msg_ifdown (dev))
5545b2fc499SJeff Garzik 		devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
5555b2fc499SJeff Garzik 			dev->stats.rx_packets, dev->stats.tx_packets,
5565b2fc499SJeff Garzik 			dev->stats.rx_errors, dev->stats.tx_errors
5575b2fc499SJeff Garzik 			);
5585b2fc499SJeff Garzik 
5595b2fc499SJeff Garzik 	// ensure there are no more active urbs
5605b2fc499SJeff Garzik 	add_wait_queue (&unlink_wakeup, &wait);
5615b2fc499SJeff Garzik 	dev->wait = &unlink_wakeup;
5625b2fc499SJeff Garzik 	temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
5635b2fc499SJeff Garzik 
5645b2fc499SJeff Garzik 	// maybe wait for deletions to finish.
56518ab458fSDavid Brownell 	while (!skb_queue_empty(&dev->rxq)
56618ab458fSDavid Brownell 			&& !skb_queue_empty(&dev->txq)
56718ab458fSDavid Brownell 			&& !skb_queue_empty(&dev->done)) {
5685b2fc499SJeff Garzik 		msleep(UNLINK_TIMEOUT_MS);
5695b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
5705b2fc499SJeff Garzik 			devdbg (dev, "waited for %d urb completions", temp);
5715b2fc499SJeff Garzik 	}
5725b2fc499SJeff Garzik 	dev->wait = NULL;
5735b2fc499SJeff Garzik 	remove_wait_queue (&unlink_wakeup, &wait);
5745b2fc499SJeff Garzik 
5755b2fc499SJeff Garzik 	usb_kill_urb(dev->interrupt);
5765b2fc499SJeff Garzik 
5775b2fc499SJeff Garzik 	/* deferred work (task, timer, softirq) must also stop.
5785b2fc499SJeff Garzik 	 * can't flush_scheduled_work() until we drop rtnl (later),
5795b2fc499SJeff Garzik 	 * else workers could deadlock; so make workers a NOP.
5805b2fc499SJeff Garzik 	 */
5815b2fc499SJeff Garzik 	dev->flags = 0;
5825b2fc499SJeff Garzik 	del_timer_sync (&dev->delay);
5835b2fc499SJeff Garzik 	tasklet_kill (&dev->bh);
584a11a6544SOliver Neukum 	usb_autopm_put_interface(dev->intf);
5855b2fc499SJeff Garzik 
5865b2fc499SJeff Garzik 	return 0;
5875b2fc499SJeff Garzik }
588777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop);
5895b2fc499SJeff Garzik 
5905b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5915b2fc499SJeff Garzik 
5925b2fc499SJeff Garzik // posts reads, and enables write queuing
5935b2fc499SJeff Garzik 
5945b2fc499SJeff Garzik // precondition: never called in_interrupt
5955b2fc499SJeff Garzik 
596777baa47SStephen Hemminger int usbnet_open (struct net_device *net)
5975b2fc499SJeff Garzik {
5985b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
599a11a6544SOliver Neukum 	int			retval;
6005b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
6015b2fc499SJeff Garzik 
602a11a6544SOliver Neukum 	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
603a11a6544SOliver Neukum 		if (netif_msg_ifup (dev))
604a11a6544SOliver Neukum 			devinfo (dev,
605a11a6544SOliver Neukum 				"resumption fail (%d) usbnet usb-%s-%s, %s",
606a11a6544SOliver Neukum 				retval,
607a11a6544SOliver Neukum 				dev->udev->bus->bus_name, dev->udev->devpath,
608a11a6544SOliver Neukum 			info->description);
609a11a6544SOliver Neukum 		goto done_nopm;
610a11a6544SOliver Neukum 	}
611a11a6544SOliver Neukum 
6125b2fc499SJeff Garzik 	// put into "known safe" state
6135b2fc499SJeff Garzik 	if (info->reset && (retval = info->reset (dev)) < 0) {
6145b2fc499SJeff Garzik 		if (netif_msg_ifup (dev))
6155b2fc499SJeff Garzik 			devinfo (dev,
6165b2fc499SJeff Garzik 				"open reset fail (%d) usbnet usb-%s-%s, %s",
6175b2fc499SJeff Garzik 				retval,
6185b2fc499SJeff Garzik 				dev->udev->bus->bus_name, dev->udev->devpath,
6195b2fc499SJeff Garzik 			info->description);
6205b2fc499SJeff Garzik 		goto done;
6215b2fc499SJeff Garzik 	}
6225b2fc499SJeff Garzik 
6235b2fc499SJeff Garzik 	// insist peer be connected
6245b2fc499SJeff Garzik 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
6255b2fc499SJeff Garzik 		if (netif_msg_ifup (dev))
6265b2fc499SJeff Garzik 			devdbg (dev, "can't open; %d", retval);
6275b2fc499SJeff Garzik 		goto done;
6285b2fc499SJeff Garzik 	}
6295b2fc499SJeff Garzik 
6305b2fc499SJeff Garzik 	/* start any status interrupt transfer */
6315b2fc499SJeff Garzik 	if (dev->interrupt) {
6325b2fc499SJeff Garzik 		retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
6335b2fc499SJeff Garzik 		if (retval < 0) {
6345b2fc499SJeff Garzik 			if (netif_msg_ifup (dev))
6355b2fc499SJeff Garzik 				deverr (dev, "intr submit %d", retval);
6365b2fc499SJeff Garzik 			goto done;
6375b2fc499SJeff Garzik 		}
6385b2fc499SJeff Garzik 	}
6395b2fc499SJeff Garzik 
6405b2fc499SJeff Garzik 	netif_start_queue (net);
6415b2fc499SJeff Garzik 	if (netif_msg_ifup (dev)) {
6425b2fc499SJeff Garzik 		char	*framing;
6435b2fc499SJeff Garzik 
6445b2fc499SJeff Garzik 		if (dev->driver_info->flags & FLAG_FRAMING_NC)
6455b2fc499SJeff Garzik 			framing = "NetChip";
6465b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_GL)
6475b2fc499SJeff Garzik 			framing = "GeneSys";
6485b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_Z)
6495b2fc499SJeff Garzik 			framing = "Zaurus";
6505b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_RN)
6515b2fc499SJeff Garzik 			framing = "RNDIS";
6525b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_AX)
6535b2fc499SJeff Garzik 			framing = "ASIX";
6545b2fc499SJeff Garzik 		else
6555b2fc499SJeff Garzik 			framing = "simple";
6565b2fc499SJeff Garzik 
6575b2fc499SJeff Garzik 		devinfo (dev, "open: enable queueing "
6585b2fc499SJeff Garzik 				"(rx %d, tx %d) mtu %d %s framing",
6595b2fc499SJeff Garzik 			(int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
6605b2fc499SJeff Garzik 			framing);
6615b2fc499SJeff Garzik 	}
6625b2fc499SJeff Garzik 
6635b2fc499SJeff Garzik 	// delay posting reads until we're fully open
6645b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
665a11a6544SOliver Neukum 	return retval;
6665b2fc499SJeff Garzik done:
667a11a6544SOliver Neukum 	usb_autopm_put_interface(dev->intf);
668a11a6544SOliver Neukum done_nopm:
6695b2fc499SJeff Garzik 	return retval;
6705b2fc499SJeff Garzik }
671777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open);
6725b2fc499SJeff Garzik 
6735b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
6745b2fc499SJeff Garzik 
6755b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
6765b2fc499SJeff Garzik  * they'll probably want to use this base set.
6775b2fc499SJeff Garzik  */
6785b2fc499SJeff Garzik 
6795b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
6805b2fc499SJeff Garzik {
6815b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
6825b2fc499SJeff Garzik 
6835b2fc499SJeff Garzik 	if (!dev->mii.mdio_read)
6845b2fc499SJeff Garzik 		return -EOPNOTSUPP;
6855b2fc499SJeff Garzik 
6865b2fc499SJeff Garzik 	return mii_ethtool_gset(&dev->mii, cmd);
6875b2fc499SJeff Garzik }
6885b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings);
6895b2fc499SJeff Garzik 
6905b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
6915b2fc499SJeff Garzik {
6925b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
6935b2fc499SJeff Garzik 	int retval;
6945b2fc499SJeff Garzik 
6955b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
6965b2fc499SJeff Garzik 		return -EOPNOTSUPP;
6975b2fc499SJeff Garzik 
6985b2fc499SJeff Garzik 	retval = mii_ethtool_sset(&dev->mii, cmd);
6995b2fc499SJeff Garzik 
7005b2fc499SJeff Garzik 	/* link speed/duplex might have changed */
7015b2fc499SJeff Garzik 	if (dev->driver_info->link_reset)
7025b2fc499SJeff Garzik 		dev->driver_info->link_reset(dev);
7035b2fc499SJeff Garzik 
7045b2fc499SJeff Garzik 	return retval;
7055b2fc499SJeff Garzik 
7065b2fc499SJeff Garzik }
7075b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings);
7085b2fc499SJeff Garzik 
7095b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
7105b2fc499SJeff Garzik {
7115b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7125b2fc499SJeff Garzik 
7135b2fc499SJeff Garzik 	/* If a check_connect is defined, return its result */
7145b2fc499SJeff Garzik 	if (dev->driver_info->check_connect)
7155b2fc499SJeff Garzik 		return dev->driver_info->check_connect (dev) == 0;
7165b2fc499SJeff Garzik 
7175b2fc499SJeff Garzik 	/* if the device has mii operations, use those */
7185b2fc499SJeff Garzik 	if (dev->mii.mdio_read)
7195b2fc499SJeff Garzik 		return mii_link_ok(&dev->mii);
7205b2fc499SJeff Garzik 
72105ffb3e2SBjørn Mork 	/* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
72205ffb3e2SBjørn Mork 	return ethtool_op_get_link(net);
7235b2fc499SJeff Garzik }
7245b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
7255b2fc499SJeff Garzik 
7265b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
7275b2fc499SJeff Garzik {
7285b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7295b2fc499SJeff Garzik 
7305b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
7315b2fc499SJeff Garzik 		return -EOPNOTSUPP;
7325b2fc499SJeff Garzik 
7335b2fc499SJeff Garzik 	return mii_nway_restart(&dev->mii);
7345b2fc499SJeff Garzik }
7355b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
7365b2fc499SJeff Garzik 
7375b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
7385b2fc499SJeff Garzik {
7395b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7405b2fc499SJeff Garzik 
7415b2fc499SJeff Garzik 	strncpy (info->driver, dev->driver_name, sizeof info->driver);
7425b2fc499SJeff Garzik 	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
7435b2fc499SJeff Garzik 	strncpy (info->fw_version, dev->driver_info->description,
7445b2fc499SJeff Garzik 		sizeof info->fw_version);
7455b2fc499SJeff Garzik 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
7465b2fc499SJeff Garzik }
7475b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
7485b2fc499SJeff Garzik 
7495b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
7505b2fc499SJeff Garzik {
7515b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7525b2fc499SJeff Garzik 
7535b2fc499SJeff Garzik 	return dev->msg_enable;
7545b2fc499SJeff Garzik }
7555b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
7565b2fc499SJeff Garzik 
7575b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
7585b2fc499SJeff Garzik {
7595b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
7605b2fc499SJeff Garzik 
7615b2fc499SJeff Garzik 	dev->msg_enable = level;
7625b2fc499SJeff Garzik }
7635b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
7645b2fc499SJeff Garzik 
7655b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
7665b2fc499SJeff Garzik static struct ethtool_ops usbnet_ethtool_ops = {
7675b2fc499SJeff Garzik 	.get_settings		= usbnet_get_settings,
7685b2fc499SJeff Garzik 	.set_settings		= usbnet_set_settings,
7695b2fc499SJeff Garzik 	.get_link		= usbnet_get_link,
7705b2fc499SJeff Garzik 	.nway_reset		= usbnet_nway_reset,
7715b2fc499SJeff Garzik 	.get_drvinfo		= usbnet_get_drvinfo,
7725b2fc499SJeff Garzik 	.get_msglevel		= usbnet_get_msglevel,
7735b2fc499SJeff Garzik 	.set_msglevel		= usbnet_set_msglevel,
7745b2fc499SJeff Garzik };
7755b2fc499SJeff Garzik 
7765b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
7775b2fc499SJeff Garzik 
7785b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
7795b2fc499SJeff Garzik  *
7805b2fc499SJeff Garzik  * NOTE:  with 2.5 we could do more of this using completion callbacks,
7815b2fc499SJeff Garzik  * especially now that control transfers can be queued.
7825b2fc499SJeff Garzik  */
7835b2fc499SJeff Garzik static void
7845b2fc499SJeff Garzik kevent (struct work_struct *work)
7855b2fc499SJeff Garzik {
7865b2fc499SJeff Garzik 	struct usbnet		*dev =
7875b2fc499SJeff Garzik 		container_of(work, struct usbnet, kevent);
7885b2fc499SJeff Garzik 	int			status;
7895b2fc499SJeff Garzik 
7905b2fc499SJeff Garzik 	/* usb_clear_halt() needs a thread context */
7915b2fc499SJeff Garzik 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
7925b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->txq);
7935b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->out);
7945b2fc499SJeff Garzik 		if (status < 0
7955b2fc499SJeff Garzik 				&& status != -EPIPE
7965b2fc499SJeff Garzik 				&& status != -ESHUTDOWN) {
7975b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
7985b2fc499SJeff Garzik 				deverr (dev, "can't clear tx halt, status %d",
7995b2fc499SJeff Garzik 					status);
8005b2fc499SJeff Garzik 		} else {
8015b2fc499SJeff Garzik 			clear_bit (EVENT_TX_HALT, &dev->flags);
8025b2fc499SJeff Garzik 			if (status != -ESHUTDOWN)
8035b2fc499SJeff Garzik 				netif_wake_queue (dev->net);
8045b2fc499SJeff Garzik 		}
8055b2fc499SJeff Garzik 	}
8065b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
8075b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->rxq);
8085b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->in);
8095b2fc499SJeff Garzik 		if (status < 0
8105b2fc499SJeff Garzik 				&& status != -EPIPE
8115b2fc499SJeff Garzik 				&& status != -ESHUTDOWN) {
8125b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
8135b2fc499SJeff Garzik 				deverr (dev, "can't clear rx halt, status %d",
8145b2fc499SJeff Garzik 					status);
8155b2fc499SJeff Garzik 		} else {
8165b2fc499SJeff Garzik 			clear_bit (EVENT_RX_HALT, &dev->flags);
8175b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
8185b2fc499SJeff Garzik 		}
8195b2fc499SJeff Garzik 	}
8205b2fc499SJeff Garzik 
8215b2fc499SJeff Garzik 	/* tasklet could resubmit itself forever if memory is tight */
8225b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
8235b2fc499SJeff Garzik 		struct urb	*urb = NULL;
8245b2fc499SJeff Garzik 
8255b2fc499SJeff Garzik 		if (netif_running (dev->net))
8265b2fc499SJeff Garzik 			urb = usb_alloc_urb (0, GFP_KERNEL);
8275b2fc499SJeff Garzik 		else
8285b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
8295b2fc499SJeff Garzik 		if (urb != NULL) {
8305b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
8315b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_KERNEL);
8325b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
8335b2fc499SJeff Garzik 		}
8345b2fc499SJeff Garzik 	}
8355b2fc499SJeff Garzik 
8365b2fc499SJeff Garzik 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
8375b2fc499SJeff Garzik 		struct driver_info	*info = dev->driver_info;
8385b2fc499SJeff Garzik 		int			retval = 0;
8395b2fc499SJeff Garzik 
8405b2fc499SJeff Garzik 		clear_bit (EVENT_LINK_RESET, &dev->flags);
8415b2fc499SJeff Garzik 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
8425b2fc499SJeff Garzik 			devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
8435b2fc499SJeff Garzik 				retval,
8445b2fc499SJeff Garzik 				dev->udev->bus->bus_name, dev->udev->devpath,
8455b2fc499SJeff Garzik 				info->description);
8465b2fc499SJeff Garzik 		}
8475b2fc499SJeff Garzik 	}
8485b2fc499SJeff Garzik 
8495b2fc499SJeff Garzik 	if (dev->flags)
8505b2fc499SJeff Garzik 		devdbg (dev, "kevent done, flags = 0x%lx",
8515b2fc499SJeff Garzik 			dev->flags);
8525b2fc499SJeff Garzik }
8535b2fc499SJeff Garzik 
8545b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
8555b2fc499SJeff Garzik 
8565b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
8575b2fc499SJeff Garzik {
8585b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
8595b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
8605b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
8615b2fc499SJeff Garzik 
8625b2fc499SJeff Garzik 	if (urb->status == 0) {
8635b2fc499SJeff Garzik 		dev->stats.tx_packets++;
8645b2fc499SJeff Garzik 		dev->stats.tx_bytes += entry->length;
8655b2fc499SJeff Garzik 	} else {
8665b2fc499SJeff Garzik 		dev->stats.tx_errors++;
8675b2fc499SJeff Garzik 
8685b2fc499SJeff Garzik 		switch (urb->status) {
8695b2fc499SJeff Garzik 		case -EPIPE:
8705b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
8715b2fc499SJeff Garzik 			break;
8725b2fc499SJeff Garzik 
8735b2fc499SJeff Garzik 		/* software-driven interface shutdown */
8745b2fc499SJeff Garzik 		case -ECONNRESET:		// async unlink
8755b2fc499SJeff Garzik 		case -ESHUTDOWN:		// hardware gone
8765b2fc499SJeff Garzik 			break;
8775b2fc499SJeff Garzik 
8785b2fc499SJeff Garzik 		// like rx, tx gets controller i/o faults during khubd delays
8795b2fc499SJeff Garzik 		// and so it uses the same throttling mechanism.
8805b2fc499SJeff Garzik 		case -EPROTO:
8815b2fc499SJeff Garzik 		case -ETIME:
8825b2fc499SJeff Garzik 		case -EILSEQ:
8835b2fc499SJeff Garzik 			if (!timer_pending (&dev->delay)) {
8845b2fc499SJeff Garzik 				mod_timer (&dev->delay,
8855b2fc499SJeff Garzik 					jiffies + THROTTLE_JIFFIES);
8865b2fc499SJeff Garzik 				if (netif_msg_link (dev))
8875b2fc499SJeff Garzik 					devdbg (dev, "tx throttle %d",
8885b2fc499SJeff Garzik 							urb->status);
8895b2fc499SJeff Garzik 			}
8905b2fc499SJeff Garzik 			netif_stop_queue (dev->net);
8915b2fc499SJeff Garzik 			break;
8925b2fc499SJeff Garzik 		default:
8935b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
8945b2fc499SJeff Garzik 				devdbg (dev, "tx err %d", entry->urb->status);
8955b2fc499SJeff Garzik 			break;
8965b2fc499SJeff Garzik 		}
8975b2fc499SJeff Garzik 	}
8985b2fc499SJeff Garzik 
8995b2fc499SJeff Garzik 	urb->dev = NULL;
9005b2fc499SJeff Garzik 	entry->state = tx_done;
9015b2fc499SJeff Garzik 	defer_bh(dev, skb, &dev->txq);
9025b2fc499SJeff Garzik }
9035b2fc499SJeff Garzik 
9045b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9055b2fc499SJeff Garzik 
906777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net)
9075b2fc499SJeff Garzik {
9085b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
9095b2fc499SJeff Garzik 
9105b2fc499SJeff Garzik 	unlink_urbs (dev, &dev->txq);
9115b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
9125b2fc499SJeff Garzik 
9135b2fc499SJeff Garzik 	// FIXME: device recovery -- reset?
9145b2fc499SJeff Garzik }
915777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
9165b2fc499SJeff Garzik 
9175b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9185b2fc499SJeff Garzik 
919777baa47SStephen Hemminger int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
9205b2fc499SJeff Garzik {
9215b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
9225b2fc499SJeff Garzik 	int			length;
9235b2fc499SJeff Garzik 	int			retval = NET_XMIT_SUCCESS;
9245b2fc499SJeff Garzik 	struct urb		*urb = NULL;
9255b2fc499SJeff Garzik 	struct skb_data		*entry;
9265b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
9275b2fc499SJeff Garzik 	unsigned long		flags;
9285b2fc499SJeff Garzik 
9295b2fc499SJeff Garzik 	// some devices want funky USB-level framing, for
9305b2fc499SJeff Garzik 	// win32 driver (usually) and/or hardware quirks
9315b2fc499SJeff Garzik 	if (info->tx_fixup) {
9325b2fc499SJeff Garzik 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
9335b2fc499SJeff Garzik 		if (!skb) {
9345b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
9355b2fc499SJeff Garzik 				devdbg (dev, "can't tx_fixup skb");
9365b2fc499SJeff Garzik 			goto drop;
9375b2fc499SJeff Garzik 		}
9385b2fc499SJeff Garzik 	}
9395b2fc499SJeff Garzik 	length = skb->len;
9405b2fc499SJeff Garzik 
9415b2fc499SJeff Garzik 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
9425b2fc499SJeff Garzik 		if (netif_msg_tx_err (dev))
9435b2fc499SJeff Garzik 			devdbg (dev, "no urb");
9445b2fc499SJeff Garzik 		goto drop;
9455b2fc499SJeff Garzik 	}
9465b2fc499SJeff Garzik 
9475b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
9485b2fc499SJeff Garzik 	entry->urb = urb;
9495b2fc499SJeff Garzik 	entry->dev = dev;
9505b2fc499SJeff Garzik 	entry->state = tx_start;
9515b2fc499SJeff Garzik 	entry->length = length;
9525b2fc499SJeff Garzik 
9535b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
9545b2fc499SJeff Garzik 			skb->data, skb->len, tx_complete, skb);
9555b2fc499SJeff Garzik 
9565b2fc499SJeff Garzik 	/* don't assume the hardware handles USB_ZERO_PACKET
9575b2fc499SJeff Garzik 	 * NOTE:  strictly conforming cdc-ether devices should expect
9585b2fc499SJeff Garzik 	 * the ZLP here, but ignore the one-byte packet.
9595b2fc499SJeff Garzik 	 */
9603e323f3eSPeter Korsgaard 	if ((length % dev->maxpacket) == 0) {
9615b2fc499SJeff Garzik 		urb->transfer_buffer_length++;
9623e323f3eSPeter Korsgaard 		if (skb_tailroom(skb)) {
9633e323f3eSPeter Korsgaard 			skb->data[skb->len] = 0;
9643e323f3eSPeter Korsgaard 			__skb_put(skb, 1);
9653e323f3eSPeter Korsgaard 		}
9663e323f3eSPeter Korsgaard 	}
9675b2fc499SJeff Garzik 
9685b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->txq.lock, flags);
9695b2fc499SJeff Garzik 
9705b2fc499SJeff Garzik 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
9715b2fc499SJeff Garzik 	case -EPIPE:
9725b2fc499SJeff Garzik 		netif_stop_queue (net);
9735b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
9745b2fc499SJeff Garzik 		break;
9755b2fc499SJeff Garzik 	default:
9765b2fc499SJeff Garzik 		if (netif_msg_tx_err (dev))
9775b2fc499SJeff Garzik 			devdbg (dev, "tx: submit urb err %d", retval);
9785b2fc499SJeff Garzik 		break;
9795b2fc499SJeff Garzik 	case 0:
9805b2fc499SJeff Garzik 		net->trans_start = jiffies;
9815b2fc499SJeff Garzik 		__skb_queue_tail (&dev->txq, skb);
9825b2fc499SJeff Garzik 		if (dev->txq.qlen >= TX_QLEN (dev))
9835b2fc499SJeff Garzik 			netif_stop_queue (net);
9845b2fc499SJeff Garzik 	}
9855b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->txq.lock, flags);
9865b2fc499SJeff Garzik 
9875b2fc499SJeff Garzik 	if (retval) {
9885b2fc499SJeff Garzik 		if (netif_msg_tx_err (dev))
9895b2fc499SJeff Garzik 			devdbg (dev, "drop, code %d", retval);
9905b2fc499SJeff Garzik drop:
9915b2fc499SJeff Garzik 		retval = NET_XMIT_SUCCESS;
9925b2fc499SJeff Garzik 		dev->stats.tx_dropped++;
9935b2fc499SJeff Garzik 		if (skb)
9945b2fc499SJeff Garzik 			dev_kfree_skb_any (skb);
9955b2fc499SJeff Garzik 		usb_free_urb (urb);
9965b2fc499SJeff Garzik 	} else if (netif_msg_tx_queued (dev)) {
9975b2fc499SJeff Garzik 		devdbg (dev, "> tx, len %d, type 0x%x",
9985b2fc499SJeff Garzik 			length, skb->protocol);
9995b2fc499SJeff Garzik 	}
10005b2fc499SJeff Garzik 	return retval;
10015b2fc499SJeff Garzik }
1002777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit);
10035b2fc499SJeff Garzik 
10045b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10055b2fc499SJeff Garzik 
10065b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
10075b2fc499SJeff Garzik 
10085b2fc499SJeff Garzik static void usbnet_bh (unsigned long param)
10095b2fc499SJeff Garzik {
10105b2fc499SJeff Garzik 	struct usbnet		*dev = (struct usbnet *) param;
10115b2fc499SJeff Garzik 	struct sk_buff		*skb;
10125b2fc499SJeff Garzik 	struct skb_data		*entry;
10135b2fc499SJeff Garzik 
10145b2fc499SJeff Garzik 	while ((skb = skb_dequeue (&dev->done))) {
10155b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
10165b2fc499SJeff Garzik 		switch (entry->state) {
10175b2fc499SJeff Garzik 		case rx_done:
10185b2fc499SJeff Garzik 			entry->state = rx_cleanup;
10195b2fc499SJeff Garzik 			rx_process (dev, skb);
10205b2fc499SJeff Garzik 			continue;
10215b2fc499SJeff Garzik 		case tx_done:
10225b2fc499SJeff Garzik 		case rx_cleanup:
10235b2fc499SJeff Garzik 			usb_free_urb (entry->urb);
10245b2fc499SJeff Garzik 			dev_kfree_skb (skb);
10255b2fc499SJeff Garzik 			continue;
10265b2fc499SJeff Garzik 		default:
10275b2fc499SJeff Garzik 			devdbg (dev, "bogus skb state %d", entry->state);
10285b2fc499SJeff Garzik 		}
10295b2fc499SJeff Garzik 	}
10305b2fc499SJeff Garzik 
10315b2fc499SJeff Garzik 	// waiting for all pending urbs to complete?
10325b2fc499SJeff Garzik 	if (dev->wait) {
10335b2fc499SJeff Garzik 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
10345b2fc499SJeff Garzik 			wake_up (dev->wait);
10355b2fc499SJeff Garzik 		}
10365b2fc499SJeff Garzik 
10375b2fc499SJeff Garzik 	// or are we maybe short a few urbs?
10385b2fc499SJeff Garzik 	} else if (netif_running (dev->net)
10395b2fc499SJeff Garzik 			&& netif_device_present (dev->net)
10405b2fc499SJeff Garzik 			&& !timer_pending (&dev->delay)
10415b2fc499SJeff Garzik 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
10425b2fc499SJeff Garzik 		int	temp = dev->rxq.qlen;
10435b2fc499SJeff Garzik 		int	qlen = RX_QLEN (dev);
10445b2fc499SJeff Garzik 
10455b2fc499SJeff Garzik 		if (temp < qlen) {
10465b2fc499SJeff Garzik 			struct urb	*urb;
10475b2fc499SJeff Garzik 			int		i;
10485b2fc499SJeff Garzik 
10495b2fc499SJeff Garzik 			// don't refill the queue all at once
10505b2fc499SJeff Garzik 			for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
10515b2fc499SJeff Garzik 				urb = usb_alloc_urb (0, GFP_ATOMIC);
10525b2fc499SJeff Garzik 				if (urb != NULL)
10535b2fc499SJeff Garzik 					rx_submit (dev, urb, GFP_ATOMIC);
10545b2fc499SJeff Garzik 			}
10555b2fc499SJeff Garzik 			if (temp != dev->rxq.qlen && netif_msg_link (dev))
10565b2fc499SJeff Garzik 				devdbg (dev, "rxqlen %d --> %d",
10575b2fc499SJeff Garzik 						temp, dev->rxq.qlen);
10585b2fc499SJeff Garzik 			if (dev->rxq.qlen < qlen)
10595b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
10605b2fc499SJeff Garzik 		}
10615b2fc499SJeff Garzik 		if (dev->txq.qlen < TX_QLEN (dev))
10625b2fc499SJeff Garzik 			netif_wake_queue (dev->net);
10635b2fc499SJeff Garzik 	}
10645b2fc499SJeff Garzik }
10655b2fc499SJeff Garzik 
10665b2fc499SJeff Garzik 
10675b2fc499SJeff Garzik 
10685b2fc499SJeff Garzik /*-------------------------------------------------------------------------
10695b2fc499SJeff Garzik  *
10705b2fc499SJeff Garzik  * USB Device Driver support
10715b2fc499SJeff Garzik  *
10725b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
10735b2fc499SJeff Garzik 
10745b2fc499SJeff Garzik // precondition: never called in_interrupt
10755b2fc499SJeff Garzik 
10765b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
10775b2fc499SJeff Garzik {
10785b2fc499SJeff Garzik 	struct usbnet		*dev;
10795b2fc499SJeff Garzik 	struct usb_device	*xdev;
10805b2fc499SJeff Garzik 	struct net_device	*net;
10815b2fc499SJeff Garzik 
10825b2fc499SJeff Garzik 	dev = usb_get_intfdata(intf);
10835b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
10845b2fc499SJeff Garzik 	if (!dev)
10855b2fc499SJeff Garzik 		return;
10865b2fc499SJeff Garzik 
10875b2fc499SJeff Garzik 	xdev = interface_to_usbdev (intf);
10885b2fc499SJeff Garzik 
10895b2fc499SJeff Garzik 	if (netif_msg_probe (dev))
10905b2fc499SJeff Garzik 		devinfo (dev, "unregister '%s' usb-%s-%s, %s",
10915b2fc499SJeff Garzik 			intf->dev.driver->name,
10925b2fc499SJeff Garzik 			xdev->bus->bus_name, xdev->devpath,
10935b2fc499SJeff Garzik 			dev->driver_info->description);
10945b2fc499SJeff Garzik 
10955b2fc499SJeff Garzik 	net = dev->net;
10965b2fc499SJeff Garzik 	unregister_netdev (net);
10975b2fc499SJeff Garzik 
10985b2fc499SJeff Garzik 	/* we don't hold rtnl here ... */
10995b2fc499SJeff Garzik 	flush_scheduled_work ();
11005b2fc499SJeff Garzik 
11015b2fc499SJeff Garzik 	if (dev->driver_info->unbind)
11025b2fc499SJeff Garzik 		dev->driver_info->unbind (dev, intf);
11035b2fc499SJeff Garzik 
11045b2fc499SJeff Garzik 	free_netdev(net);
11055b2fc499SJeff Garzik 	usb_put_dev (xdev);
11065b2fc499SJeff Garzik }
11075b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
11085b2fc499SJeff Garzik 
1109777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = {
1110777baa47SStephen Hemminger 	.ndo_open		= usbnet_open,
1111777baa47SStephen Hemminger 	.ndo_stop		= usbnet_stop,
1112777baa47SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
1113777baa47SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
1114777baa47SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1115777baa47SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
1116777baa47SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
1117777baa47SStephen Hemminger };
11185b2fc499SJeff Garzik 
11195b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
11205b2fc499SJeff Garzik 
11215b2fc499SJeff Garzik // precondition: never called in_interrupt
11225b2fc499SJeff Garzik 
11235b2fc499SJeff Garzik int
11245b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
11255b2fc499SJeff Garzik {
11265b2fc499SJeff Garzik 	struct usbnet			*dev;
11275b2fc499SJeff Garzik 	struct net_device		*net;
11285b2fc499SJeff Garzik 	struct usb_host_interface	*interface;
11295b2fc499SJeff Garzik 	struct driver_info		*info;
11305b2fc499SJeff Garzik 	struct usb_device		*xdev;
11315b2fc499SJeff Garzik 	int				status;
11325b2fc499SJeff Garzik 	const char			*name;
11335b2fc499SJeff Garzik 
11345b2fc499SJeff Garzik 	name = udev->dev.driver->name;
11355b2fc499SJeff Garzik 	info = (struct driver_info *) prod->driver_info;
11365b2fc499SJeff Garzik 	if (!info) {
11375b2fc499SJeff Garzik 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
11385b2fc499SJeff Garzik 		return -ENODEV;
11395b2fc499SJeff Garzik 	}
11405b2fc499SJeff Garzik 	xdev = interface_to_usbdev (udev);
11415b2fc499SJeff Garzik 	interface = udev->cur_altsetting;
11425b2fc499SJeff Garzik 
11435b2fc499SJeff Garzik 	usb_get_dev (xdev);
11445b2fc499SJeff Garzik 
11455b2fc499SJeff Garzik 	status = -ENOMEM;
11465b2fc499SJeff Garzik 
11475b2fc499SJeff Garzik 	// set up our own records
11485b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(*dev));
11495b2fc499SJeff Garzik 	if (!net) {
11505b2fc499SJeff Garzik 		dbg ("can't kmalloc dev");
11515b2fc499SJeff Garzik 		goto out;
11525b2fc499SJeff Garzik 	}
11535b2fc499SJeff Garzik 
11545b2fc499SJeff Garzik 	dev = netdev_priv(net);
11555b2fc499SJeff Garzik 	dev->udev = xdev;
1156a11a6544SOliver Neukum 	dev->intf = udev;
11575b2fc499SJeff Garzik 	dev->driver_info = info;
11585b2fc499SJeff Garzik 	dev->driver_name = name;
11595b2fc499SJeff Garzik 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
11605b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
11615b2fc499SJeff Garzik 	skb_queue_head_init (&dev->rxq);
11625b2fc499SJeff Garzik 	skb_queue_head_init (&dev->txq);
11635b2fc499SJeff Garzik 	skb_queue_head_init (&dev->done);
11645b2fc499SJeff Garzik 	dev->bh.func = usbnet_bh;
11655b2fc499SJeff Garzik 	dev->bh.data = (unsigned long) dev;
11665b2fc499SJeff Garzik 	INIT_WORK (&dev->kevent, kevent);
11675b2fc499SJeff Garzik 	dev->delay.function = usbnet_bh;
11685b2fc499SJeff Garzik 	dev->delay.data = (unsigned long) dev;
11695b2fc499SJeff Garzik 	init_timer (&dev->delay);
11705b2fc499SJeff Garzik 	mutex_init (&dev->phy_mutex);
11715b2fc499SJeff Garzik 
11725b2fc499SJeff Garzik 	dev->net = net;
11735b2fc499SJeff Garzik 	strcpy (net->name, "usb%d");
11745b2fc499SJeff Garzik 	memcpy (net->dev_addr, node_id, sizeof node_id);
11755b2fc499SJeff Garzik 
11765b2fc499SJeff Garzik 	/* rx and tx sides can use different message sizes;
11775b2fc499SJeff Garzik 	 * bind() should set rx_urb_size in that case.
11785b2fc499SJeff Garzik 	 */
11795b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
11805b2fc499SJeff Garzik #if 0
11815b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures
11825b2fc499SJeff Garzik 	// possible with some EHCI controllers
1183*6a35528aSYang Hongyang 	if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
11845b2fc499SJeff Garzik 		net->features |= NETIF_F_HIGHDMA;
11855b2fc499SJeff Garzik #endif
11865b2fc499SJeff Garzik 
1187777baa47SStephen Hemminger 	net->netdev_ops = &usbnet_netdev_ops;
1188777baa47SStephen Hemminger #ifdef CONFIG_COMPAT_NET_DEV_OPS
11895b2fc499SJeff Garzik 	net->hard_start_xmit = usbnet_start_xmit;
11905b2fc499SJeff Garzik 	net->open = usbnet_open;
11915b2fc499SJeff Garzik 	net->stop = usbnet_stop;
11925b2fc499SJeff Garzik 	net->tx_timeout = usbnet_tx_timeout;
1193777baa47SStephen Hemminger #endif
1194777baa47SStephen Hemminger 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
11955b2fc499SJeff Garzik 	net->ethtool_ops = &usbnet_ethtool_ops;
11965b2fc499SJeff Garzik 
11975b2fc499SJeff Garzik 	// allow device-specific bind/init procedures
11985b2fc499SJeff Garzik 	// NOTE net->name still not usable ...
11995b2fc499SJeff Garzik 	if (info->bind) {
12005b2fc499SJeff Garzik 		status = info->bind (dev, udev);
12015b2fc499SJeff Garzik 		if (status < 0)
12025b2fc499SJeff Garzik 			goto out1;
12035b2fc499SJeff Garzik 
12045b2fc499SJeff Garzik 		// heuristic:  "usb%d" for links we know are two-host,
12055b2fc499SJeff Garzik 		// else "eth%d" when there's reasonable doubt.  userspace
12065b2fc499SJeff Garzik 		// can rename the link if it knows better.
12075b2fc499SJeff Garzik 		if ((dev->driver_info->flags & FLAG_ETHER) != 0
12085b2fc499SJeff Garzik 				&& (net->dev_addr [0] & 0x02) == 0)
12095b2fc499SJeff Garzik 			strcpy (net->name, "eth%d");
12106e3bbcc5SJussi Kivilinna 		/* WLAN devices should always be named "wlan%d" */
12116e3bbcc5SJussi Kivilinna 		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
12126e3bbcc5SJussi Kivilinna 			strcpy(net->name, "wlan%d");
12135b2fc499SJeff Garzik 
12145b2fc499SJeff Garzik 		/* maybe the remote can't receive an Ethernet MTU */
12155b2fc499SJeff Garzik 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
12165b2fc499SJeff Garzik 			net->mtu = dev->hard_mtu - net->hard_header_len;
12175b2fc499SJeff Garzik 	} else if (!info->in || !info->out)
12185b2fc499SJeff Garzik 		status = usbnet_get_endpoints (dev, udev);
12195b2fc499SJeff Garzik 	else {
12205b2fc499SJeff Garzik 		dev->in = usb_rcvbulkpipe (xdev, info->in);
12215b2fc499SJeff Garzik 		dev->out = usb_sndbulkpipe (xdev, info->out);
12225b2fc499SJeff Garzik 		if (!(info->flags & FLAG_NO_SETINT))
12235b2fc499SJeff Garzik 			status = usb_set_interface (xdev,
12245b2fc499SJeff Garzik 				interface->desc.bInterfaceNumber,
12255b2fc499SJeff Garzik 				interface->desc.bAlternateSetting);
12265b2fc499SJeff Garzik 		else
12275b2fc499SJeff Garzik 			status = 0;
12285b2fc499SJeff Garzik 
12295b2fc499SJeff Garzik 	}
12309514bfe5SPeter Korsgaard 	if (status >= 0 && dev->status)
12315b2fc499SJeff Garzik 		status = init_status (dev, udev);
12325b2fc499SJeff Garzik 	if (status < 0)
12335b2fc499SJeff Garzik 		goto out3;
12345b2fc499SJeff Garzik 
12355b2fc499SJeff Garzik 	if (!dev->rx_urb_size)
12365b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
12375b2fc499SJeff Garzik 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
12385b2fc499SJeff Garzik 
12395b2fc499SJeff Garzik 	SET_NETDEV_DEV(net, &udev->dev);
12405b2fc499SJeff Garzik 	status = register_netdev (net);
12415b2fc499SJeff Garzik 	if (status)
12425b2fc499SJeff Garzik 		goto out3;
12435b2fc499SJeff Garzik 	if (netif_msg_probe (dev))
1244e174961cSJohannes Berg 		devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
12455b2fc499SJeff Garzik 			udev->dev.driver->name,
12465b2fc499SJeff Garzik 			xdev->bus->bus_name, xdev->devpath,
12475b2fc499SJeff Garzik 			dev->driver_info->description,
1248e174961cSJohannes Berg 			net->dev_addr);
12495b2fc499SJeff Garzik 
12505b2fc499SJeff Garzik 	// ok, it's ready to go.
12515b2fc499SJeff Garzik 	usb_set_intfdata (udev, dev);
12525b2fc499SJeff Garzik 
12535b2fc499SJeff Garzik 	// start as if the link is up
12545b2fc499SJeff Garzik 	netif_device_attach (net);
12555b2fc499SJeff Garzik 
12565b2fc499SJeff Garzik 	return 0;
12575b2fc499SJeff Garzik 
12585b2fc499SJeff Garzik out3:
12595b2fc499SJeff Garzik 	if (info->unbind)
12605b2fc499SJeff Garzik 		info->unbind (dev, udev);
12615b2fc499SJeff Garzik out1:
12625b2fc499SJeff Garzik 	free_netdev(net);
12635b2fc499SJeff Garzik out:
12645b2fc499SJeff Garzik 	usb_put_dev(xdev);
12655b2fc499SJeff Garzik 	return status;
12665b2fc499SJeff Garzik }
12675b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
12685b2fc499SJeff Garzik 
12695b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12705b2fc499SJeff Garzik 
127136433127SOliver Neukum /*
127236433127SOliver Neukum  * suspend the whole driver as soon as the first interface is suspended
127336433127SOliver Neukum  * resume only when the last interface is resumed
12745b2fc499SJeff Garzik  */
12755b2fc499SJeff Garzik 
12765b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
12775b2fc499SJeff Garzik {
12785b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
12795b2fc499SJeff Garzik 
128036433127SOliver Neukum 	if (!dev->suspend_count++) {
1281a11a6544SOliver Neukum 		/*
1282a11a6544SOliver Neukum 		 * accelerate emptying of the rx and queues, to avoid
12835b2fc499SJeff Garzik 		 * having everything error out.
12845b2fc499SJeff Garzik 		 */
12855b2fc499SJeff Garzik 		netif_device_detach (dev->net);
12865b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
12875b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->txq);
1288a11a6544SOliver Neukum 		/*
1289a11a6544SOliver Neukum 		 * reattach so runtime management can use and
1290a11a6544SOliver Neukum 		 * wake the device
1291a11a6544SOliver Neukum 		 */
1292a11a6544SOliver Neukum 		netif_device_attach (dev->net);
129336433127SOliver Neukum 	}
12945b2fc499SJeff Garzik 	return 0;
12955b2fc499SJeff Garzik }
12965b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
12975b2fc499SJeff Garzik 
12985b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
12995b2fc499SJeff Garzik {
13005b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
13015b2fc499SJeff Garzik 
1302a11a6544SOliver Neukum 	if (!--dev->suspend_count)
13035b2fc499SJeff Garzik 		tasklet_schedule (&dev->bh);
1304a11a6544SOliver Neukum 
13055b2fc499SJeff Garzik 	return 0;
13065b2fc499SJeff Garzik }
13075b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
13085b2fc499SJeff Garzik 
13095b2fc499SJeff Garzik 
13105b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
13115b2fc499SJeff Garzik 
13125b2fc499SJeff Garzik static int __init usbnet_init(void)
13135b2fc499SJeff Garzik {
13145b2fc499SJeff Garzik 	/* compiler should optimize this out */
13155b2fc499SJeff Garzik 	BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
13165b2fc499SJeff Garzik 			< sizeof (struct skb_data));
13175b2fc499SJeff Garzik 
13185b2fc499SJeff Garzik 	random_ether_addr(node_id);
13195b2fc499SJeff Garzik 	return 0;
13205b2fc499SJeff Garzik }
13215b2fc499SJeff Garzik module_init(usbnet_init);
13225b2fc499SJeff Garzik 
13235b2fc499SJeff Garzik static void __exit usbnet_exit(void)
13245b2fc499SJeff Garzik {
13255b2fc499SJeff Garzik }
13265b2fc499SJeff Garzik module_exit(usbnet_exit);
13275b2fc499SJeff Garzik 
13285b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
13295b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
13305b2fc499SJeff Garzik MODULE_LICENSE("GPL");
1331