xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision 5b2fc499917e5897a13add780e181b4cef197072)
1*5b2fc499SJeff Garzik /*
2*5b2fc499SJeff Garzik  * USB Network driver infrastructure
3*5b2fc499SJeff Garzik  * Copyright (C) 2000-2005 by David Brownell
4*5b2fc499SJeff Garzik  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
5*5b2fc499SJeff Garzik  *
6*5b2fc499SJeff Garzik  * This program is free software; you can redistribute it and/or modify
7*5b2fc499SJeff Garzik  * it under the terms of the GNU General Public License as published by
8*5b2fc499SJeff Garzik  * the Free Software Foundation; either version 2 of the License, or
9*5b2fc499SJeff Garzik  * (at your option) any later version.
10*5b2fc499SJeff Garzik  *
11*5b2fc499SJeff Garzik  * This program is distributed in the hope that it will be useful,
12*5b2fc499SJeff Garzik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*5b2fc499SJeff Garzik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*5b2fc499SJeff Garzik  * GNU General Public License for more details.
15*5b2fc499SJeff Garzik  *
16*5b2fc499SJeff Garzik  * You should have received a copy of the GNU General Public License
17*5b2fc499SJeff Garzik  * along with this program; if not, write to the Free Software
18*5b2fc499SJeff Garzik  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19*5b2fc499SJeff Garzik  */
20*5b2fc499SJeff Garzik 
21*5b2fc499SJeff Garzik /*
22*5b2fc499SJeff Garzik  * This is a generic "USB networking" framework that works with several
23*5b2fc499SJeff Garzik  * kinds of full and high speed networking devices:  host-to-host cables,
24*5b2fc499SJeff Garzik  * smart usb peripherals, and actual Ethernet adapters.
25*5b2fc499SJeff Garzik  *
26*5b2fc499SJeff Garzik  * These devices usually differ in terms of control protocols (if they
27*5b2fc499SJeff Garzik  * even have one!) and sometimes they define new framing to wrap or batch
28*5b2fc499SJeff Garzik  * Ethernet packets.  Otherwise, they talk to USB pretty much the same,
29*5b2fc499SJeff Garzik  * so interface (un)binding, endpoint I/O queues, fault handling, and other
30*5b2fc499SJeff Garzik  * issues can usefully be addressed by this framework.
31*5b2fc499SJeff Garzik  */
32*5b2fc499SJeff Garzik 
33*5b2fc499SJeff Garzik // #define	DEBUG			// error path messages, extra info
34*5b2fc499SJeff Garzik // #define	VERBOSE			// more; success messages
35*5b2fc499SJeff Garzik 
36*5b2fc499SJeff Garzik #include <linux/module.h>
37*5b2fc499SJeff Garzik #include <linux/init.h>
38*5b2fc499SJeff Garzik #include <linux/netdevice.h>
39*5b2fc499SJeff Garzik #include <linux/etherdevice.h>
40*5b2fc499SJeff Garzik #include <linux/ethtool.h>
41*5b2fc499SJeff Garzik #include <linux/workqueue.h>
42*5b2fc499SJeff Garzik #include <linux/mii.h>
43*5b2fc499SJeff Garzik #include <linux/usb.h>
44*5b2fc499SJeff Garzik 
45*5b2fc499SJeff Garzik #include "usbnet.h"
46*5b2fc499SJeff Garzik 
47*5b2fc499SJeff Garzik #define DRIVER_VERSION		"22-Aug-2005"
48*5b2fc499SJeff Garzik 
49*5b2fc499SJeff Garzik 
50*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
51*5b2fc499SJeff Garzik 
52*5b2fc499SJeff Garzik /*
53*5b2fc499SJeff Garzik  * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
54*5b2fc499SJeff Garzik  * Several dozen bytes of IPv4 data can fit in two such transactions.
55*5b2fc499SJeff Garzik  * One maximum size Ethernet packet takes twenty four of them.
56*5b2fc499SJeff Garzik  * For high speed, each frame comfortably fits almost 36 max size
57*5b2fc499SJeff Garzik  * Ethernet packets (so queues should be bigger).
58*5b2fc499SJeff Garzik  *
59*5b2fc499SJeff Garzik  * REVISIT qlens should be members of 'struct usbnet'; the goal is to
60*5b2fc499SJeff Garzik  * let the USB host controller be busy for 5msec or more before an irq
61*5b2fc499SJeff Garzik  * is required, under load.  Jumbograms change the equation.
62*5b2fc499SJeff Garzik  */
63*5b2fc499SJeff Garzik #define RX_MAX_QUEUE_MEMORY (60 * 1518)
64*5b2fc499SJeff Garzik #define	RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
65*5b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
66*5b2fc499SJeff Garzik #define	TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
67*5b2fc499SJeff Garzik 			(RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
68*5b2fc499SJeff Garzik 
69*5b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
70*5b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES	(5*HZ)
71*5b2fc499SJeff Garzik 
72*5b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect()
73*5b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors.
74*5b2fc499SJeff Garzik #define THROTTLE_JIFFIES	(HZ/8)
75*5b2fc499SJeff Garzik 
76*5b2fc499SJeff Garzik // between wakeups
77*5b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS	3
78*5b2fc499SJeff Garzik 
79*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
80*5b2fc499SJeff Garzik 
81*5b2fc499SJeff Garzik // randomly generated ethernet address
82*5b2fc499SJeff Garzik static u8	node_id [ETH_ALEN];
83*5b2fc499SJeff Garzik 
84*5b2fc499SJeff Garzik static const char driver_name [] = "usbnet";
85*5b2fc499SJeff Garzik 
86*5b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
87*5b2fc499SJeff Garzik static int msg_level = -1;
88*5b2fc499SJeff Garzik module_param (msg_level, int, 0);
89*5b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
90*5b2fc499SJeff Garzik 
91*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
92*5b2fc499SJeff Garzik 
93*5b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
94*5b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
95*5b2fc499SJeff Garzik {
96*5b2fc499SJeff Garzik 	int				tmp;
97*5b2fc499SJeff Garzik 	struct usb_host_interface	*alt = NULL;
98*5b2fc499SJeff Garzik 	struct usb_host_endpoint	*in = NULL, *out = NULL;
99*5b2fc499SJeff Garzik 	struct usb_host_endpoint	*status = NULL;
100*5b2fc499SJeff Garzik 
101*5b2fc499SJeff Garzik 	for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
102*5b2fc499SJeff Garzik 		unsigned	ep;
103*5b2fc499SJeff Garzik 
104*5b2fc499SJeff Garzik 		in = out = status = NULL;
105*5b2fc499SJeff Garzik 		alt = intf->altsetting + tmp;
106*5b2fc499SJeff Garzik 
107*5b2fc499SJeff Garzik 		/* take the first altsetting with in-bulk + out-bulk;
108*5b2fc499SJeff Garzik 		 * remember any status endpoint, just in case;
109*5b2fc499SJeff Garzik 		 * ignore other endpoints and altsetttings.
110*5b2fc499SJeff Garzik 		 */
111*5b2fc499SJeff Garzik 		for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
112*5b2fc499SJeff Garzik 			struct usb_host_endpoint	*e;
113*5b2fc499SJeff Garzik 			int				intr = 0;
114*5b2fc499SJeff Garzik 
115*5b2fc499SJeff Garzik 			e = alt->endpoint + ep;
116*5b2fc499SJeff Garzik 			switch (e->desc.bmAttributes) {
117*5b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_INT:
118*5b2fc499SJeff Garzik 				if (!usb_endpoint_dir_in(&e->desc))
119*5b2fc499SJeff Garzik 					continue;
120*5b2fc499SJeff Garzik 				intr = 1;
121*5b2fc499SJeff Garzik 				/* FALLTHROUGH */
122*5b2fc499SJeff Garzik 			case USB_ENDPOINT_XFER_BULK:
123*5b2fc499SJeff Garzik 				break;
124*5b2fc499SJeff Garzik 			default:
125*5b2fc499SJeff Garzik 				continue;
126*5b2fc499SJeff Garzik 			}
127*5b2fc499SJeff Garzik 			if (usb_endpoint_dir_in(&e->desc)) {
128*5b2fc499SJeff Garzik 				if (!intr && !in)
129*5b2fc499SJeff Garzik 					in = e;
130*5b2fc499SJeff Garzik 				else if (intr && !status)
131*5b2fc499SJeff Garzik 					status = e;
132*5b2fc499SJeff Garzik 			} else {
133*5b2fc499SJeff Garzik 				if (!out)
134*5b2fc499SJeff Garzik 					out = e;
135*5b2fc499SJeff Garzik 			}
136*5b2fc499SJeff Garzik 		}
137*5b2fc499SJeff Garzik 		if (in && out)
138*5b2fc499SJeff Garzik 			break;
139*5b2fc499SJeff Garzik 	}
140*5b2fc499SJeff Garzik 	if (!alt || !in || !out)
141*5b2fc499SJeff Garzik 		return -EINVAL;
142*5b2fc499SJeff Garzik 
143*5b2fc499SJeff Garzik 	if (alt->desc.bAlternateSetting != 0
144*5b2fc499SJeff Garzik 			|| !(dev->driver_info->flags & FLAG_NO_SETINT)) {
145*5b2fc499SJeff Garzik 		tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
146*5b2fc499SJeff Garzik 				alt->desc.bAlternateSetting);
147*5b2fc499SJeff Garzik 		if (tmp < 0)
148*5b2fc499SJeff Garzik 			return tmp;
149*5b2fc499SJeff Garzik 	}
150*5b2fc499SJeff Garzik 
151*5b2fc499SJeff Garzik 	dev->in = usb_rcvbulkpipe (dev->udev,
152*5b2fc499SJeff Garzik 			in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
153*5b2fc499SJeff Garzik 	dev->out = usb_sndbulkpipe (dev->udev,
154*5b2fc499SJeff Garzik 			out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155*5b2fc499SJeff Garzik 	dev->status = status;
156*5b2fc499SJeff Garzik 	return 0;
157*5b2fc499SJeff Garzik }
158*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
159*5b2fc499SJeff Garzik 
160*5b2fc499SJeff Garzik static void intr_complete (struct urb *urb);
161*5b2fc499SJeff Garzik 
162*5b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
163*5b2fc499SJeff Garzik {
164*5b2fc499SJeff Garzik 	char		*buf = NULL;
165*5b2fc499SJeff Garzik 	unsigned	pipe = 0;
166*5b2fc499SJeff Garzik 	unsigned	maxp;
167*5b2fc499SJeff Garzik 	unsigned	period;
168*5b2fc499SJeff Garzik 
169*5b2fc499SJeff Garzik 	if (!dev->driver_info->status)
170*5b2fc499SJeff Garzik 		return 0;
171*5b2fc499SJeff Garzik 
172*5b2fc499SJeff Garzik 	pipe = usb_rcvintpipe (dev->udev,
173*5b2fc499SJeff Garzik 			dev->status->desc.bEndpointAddress
174*5b2fc499SJeff Garzik 				& USB_ENDPOINT_NUMBER_MASK);
175*5b2fc499SJeff Garzik 	maxp = usb_maxpacket (dev->udev, pipe, 0);
176*5b2fc499SJeff Garzik 
177*5b2fc499SJeff Garzik 	/* avoid 1 msec chatter:  min 8 msec poll rate */
178*5b2fc499SJeff Garzik 	period = max ((int) dev->status->desc.bInterval,
179*5b2fc499SJeff Garzik 		(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
180*5b2fc499SJeff Garzik 
181*5b2fc499SJeff Garzik 	buf = kmalloc (maxp, GFP_KERNEL);
182*5b2fc499SJeff Garzik 	if (buf) {
183*5b2fc499SJeff Garzik 		dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
184*5b2fc499SJeff Garzik 		if (!dev->interrupt) {
185*5b2fc499SJeff Garzik 			kfree (buf);
186*5b2fc499SJeff Garzik 			return -ENOMEM;
187*5b2fc499SJeff Garzik 		} else {
188*5b2fc499SJeff Garzik 			usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
189*5b2fc499SJeff Garzik 				buf, maxp, intr_complete, dev, period);
190*5b2fc499SJeff Garzik 			dev_dbg(&intf->dev,
191*5b2fc499SJeff Garzik 				"status ep%din, %d bytes period %d\n",
192*5b2fc499SJeff Garzik 				usb_pipeendpoint(pipe), maxp, period);
193*5b2fc499SJeff Garzik 		}
194*5b2fc499SJeff Garzik 	}
195*5b2fc499SJeff Garzik 	return  0;
196*5b2fc499SJeff Garzik }
197*5b2fc499SJeff Garzik 
198*5b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
199*5b2fc499SJeff Garzik  * Some link protocols batch packets, so their rx_fixup paths
200*5b2fc499SJeff Garzik  * can return clones as well as just modify the original skb.
201*5b2fc499SJeff Garzik  */
202*5b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
203*5b2fc499SJeff Garzik {
204*5b2fc499SJeff Garzik 	int	status;
205*5b2fc499SJeff Garzik 
206*5b2fc499SJeff Garzik 	skb->protocol = eth_type_trans (skb, dev->net);
207*5b2fc499SJeff Garzik 	dev->stats.rx_packets++;
208*5b2fc499SJeff Garzik 	dev->stats.rx_bytes += skb->len;
209*5b2fc499SJeff Garzik 
210*5b2fc499SJeff Garzik 	if (netif_msg_rx_status (dev))
211*5b2fc499SJeff Garzik 		devdbg (dev, "< rx, len %zu, type 0x%x",
212*5b2fc499SJeff Garzik 			skb->len + sizeof (struct ethhdr), skb->protocol);
213*5b2fc499SJeff Garzik 	memset (skb->cb, 0, sizeof (struct skb_data));
214*5b2fc499SJeff Garzik 	status = netif_rx (skb);
215*5b2fc499SJeff Garzik 	if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
216*5b2fc499SJeff Garzik 		devdbg (dev, "netif_rx status %d", status);
217*5b2fc499SJeff Garzik }
218*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
219*5b2fc499SJeff Garzik 
220*5b2fc499SJeff Garzik 
221*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------
222*5b2fc499SJeff Garzik  *
223*5b2fc499SJeff Garzik  * Network Device Driver (peer link to "Host Device", from USB host)
224*5b2fc499SJeff Garzik  *
225*5b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
226*5b2fc499SJeff Garzik 
227*5b2fc499SJeff Garzik static int usbnet_change_mtu (struct net_device *net, int new_mtu)
228*5b2fc499SJeff Garzik {
229*5b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
230*5b2fc499SJeff Garzik 	int		ll_mtu = new_mtu + net->hard_header_len;
231*5b2fc499SJeff Garzik 	int		old_hard_mtu = dev->hard_mtu;
232*5b2fc499SJeff Garzik 	int		old_rx_urb_size = dev->rx_urb_size;
233*5b2fc499SJeff Garzik 
234*5b2fc499SJeff Garzik 	if (new_mtu <= 0)
235*5b2fc499SJeff Garzik 		return -EINVAL;
236*5b2fc499SJeff Garzik 	// no second zero-length packet read wanted after mtu-sized packets
237*5b2fc499SJeff Garzik 	if ((ll_mtu % dev->maxpacket) == 0)
238*5b2fc499SJeff Garzik 		return -EDOM;
239*5b2fc499SJeff Garzik 	net->mtu = new_mtu;
240*5b2fc499SJeff Garzik 
241*5b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
242*5b2fc499SJeff Garzik 	if (dev->rx_urb_size == old_hard_mtu) {
243*5b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
244*5b2fc499SJeff Garzik 		if (dev->rx_urb_size > old_rx_urb_size)
245*5b2fc499SJeff Garzik 			usbnet_unlink_rx_urbs(dev);
246*5b2fc499SJeff Garzik 	}
247*5b2fc499SJeff Garzik 
248*5b2fc499SJeff Garzik 	return 0;
249*5b2fc499SJeff Garzik }
250*5b2fc499SJeff Garzik 
251*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
252*5b2fc499SJeff Garzik 
253*5b2fc499SJeff Garzik static struct net_device_stats *usbnet_get_stats (struct net_device *net)
254*5b2fc499SJeff Garzik {
255*5b2fc499SJeff Garzik 	struct usbnet	*dev = netdev_priv(net);
256*5b2fc499SJeff Garzik 	return &dev->stats;
257*5b2fc499SJeff Garzik }
258*5b2fc499SJeff Garzik 
259*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
260*5b2fc499SJeff Garzik 
261*5b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
262*5b2fc499SJeff Garzik  * completion callbacks.  2.5 should have fixed those bugs...
263*5b2fc499SJeff Garzik  */
264*5b2fc499SJeff Garzik 
265*5b2fc499SJeff Garzik static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
266*5b2fc499SJeff Garzik {
267*5b2fc499SJeff Garzik 	unsigned long		flags;
268*5b2fc499SJeff Garzik 
269*5b2fc499SJeff Garzik 	spin_lock_irqsave(&list->lock, flags);
270*5b2fc499SJeff Garzik 	__skb_unlink(skb, list);
271*5b2fc499SJeff Garzik 	spin_unlock(&list->lock);
272*5b2fc499SJeff Garzik 	spin_lock(&dev->done.lock);
273*5b2fc499SJeff Garzik 	__skb_queue_tail(&dev->done, skb);
274*5b2fc499SJeff Garzik 	if (dev->done.qlen == 1)
275*5b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
276*5b2fc499SJeff Garzik 	spin_unlock_irqrestore(&dev->done.lock, flags);
277*5b2fc499SJeff Garzik }
278*5b2fc499SJeff Garzik 
279*5b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
280*5b2fc499SJeff Garzik  *
281*5b2fc499SJeff Garzik  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
282*5b2fc499SJeff Garzik  * but tasklet_schedule() doesn't.  hope the failure is rare.
283*5b2fc499SJeff Garzik  */
284*5b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
285*5b2fc499SJeff Garzik {
286*5b2fc499SJeff Garzik 	set_bit (work, &dev->flags);
287*5b2fc499SJeff Garzik 	if (!schedule_work (&dev->kevent))
288*5b2fc499SJeff Garzik 		deverr (dev, "kevent %d may have been dropped", work);
289*5b2fc499SJeff Garzik 	else
290*5b2fc499SJeff Garzik 		devdbg (dev, "kevent %d scheduled", work);
291*5b2fc499SJeff Garzik }
292*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
293*5b2fc499SJeff Garzik 
294*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
295*5b2fc499SJeff Garzik 
296*5b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
297*5b2fc499SJeff Garzik 
298*5b2fc499SJeff Garzik static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
299*5b2fc499SJeff Garzik {
300*5b2fc499SJeff Garzik 	struct sk_buff		*skb;
301*5b2fc499SJeff Garzik 	struct skb_data		*entry;
302*5b2fc499SJeff Garzik 	int			retval = 0;
303*5b2fc499SJeff Garzik 	unsigned long		lockflags;
304*5b2fc499SJeff Garzik 	size_t			size = dev->rx_urb_size;
305*5b2fc499SJeff Garzik 
306*5b2fc499SJeff Garzik 	if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
307*5b2fc499SJeff Garzik 		if (netif_msg_rx_err (dev))
308*5b2fc499SJeff Garzik 			devdbg (dev, "no rx skb");
309*5b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
310*5b2fc499SJeff Garzik 		usb_free_urb (urb);
311*5b2fc499SJeff Garzik 		return;
312*5b2fc499SJeff Garzik 	}
313*5b2fc499SJeff Garzik 	skb_reserve (skb, NET_IP_ALIGN);
314*5b2fc499SJeff Garzik 
315*5b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
316*5b2fc499SJeff Garzik 	entry->urb = urb;
317*5b2fc499SJeff Garzik 	entry->dev = dev;
318*5b2fc499SJeff Garzik 	entry->state = rx_start;
319*5b2fc499SJeff Garzik 	entry->length = 0;
320*5b2fc499SJeff Garzik 
321*5b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->in,
322*5b2fc499SJeff Garzik 		skb->data, size, rx_complete, skb);
323*5b2fc499SJeff Garzik 
324*5b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->rxq.lock, lockflags);
325*5b2fc499SJeff Garzik 
326*5b2fc499SJeff Garzik 	if (netif_running (dev->net)
327*5b2fc499SJeff Garzik 			&& netif_device_present (dev->net)
328*5b2fc499SJeff Garzik 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
329*5b2fc499SJeff Garzik 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
330*5b2fc499SJeff Garzik 		case -EPIPE:
331*5b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
332*5b2fc499SJeff Garzik 			break;
333*5b2fc499SJeff Garzik 		case -ENOMEM:
334*5b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
335*5b2fc499SJeff Garzik 			break;
336*5b2fc499SJeff Garzik 		case -ENODEV:
337*5b2fc499SJeff Garzik 			if (netif_msg_ifdown (dev))
338*5b2fc499SJeff Garzik 				devdbg (dev, "device gone");
339*5b2fc499SJeff Garzik 			netif_device_detach (dev->net);
340*5b2fc499SJeff Garzik 			break;
341*5b2fc499SJeff Garzik 		default:
342*5b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
343*5b2fc499SJeff Garzik 				devdbg (dev, "rx submit, %d", retval);
344*5b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
345*5b2fc499SJeff Garzik 			break;
346*5b2fc499SJeff Garzik 		case 0:
347*5b2fc499SJeff Garzik 			__skb_queue_tail (&dev->rxq, skb);
348*5b2fc499SJeff Garzik 		}
349*5b2fc499SJeff Garzik 	} else {
350*5b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
351*5b2fc499SJeff Garzik 			devdbg (dev, "rx: stopped");
352*5b2fc499SJeff Garzik 		retval = -ENOLINK;
353*5b2fc499SJeff Garzik 	}
354*5b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
355*5b2fc499SJeff Garzik 	if (retval) {
356*5b2fc499SJeff Garzik 		dev_kfree_skb_any (skb);
357*5b2fc499SJeff Garzik 		usb_free_urb (urb);
358*5b2fc499SJeff Garzik 	}
359*5b2fc499SJeff Garzik }
360*5b2fc499SJeff Garzik 
361*5b2fc499SJeff Garzik 
362*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
363*5b2fc499SJeff Garzik 
364*5b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
365*5b2fc499SJeff Garzik {
366*5b2fc499SJeff Garzik 	if (dev->driver_info->rx_fixup
367*5b2fc499SJeff Garzik 			&& !dev->driver_info->rx_fixup (dev, skb))
368*5b2fc499SJeff Garzik 		goto error;
369*5b2fc499SJeff Garzik 	// else network stack removes extra byte if we forced a short packet
370*5b2fc499SJeff Garzik 
371*5b2fc499SJeff Garzik 	if (skb->len)
372*5b2fc499SJeff Garzik 		usbnet_skb_return (dev, skb);
373*5b2fc499SJeff Garzik 	else {
374*5b2fc499SJeff Garzik 		if (netif_msg_rx_err (dev))
375*5b2fc499SJeff Garzik 			devdbg (dev, "drop");
376*5b2fc499SJeff Garzik error:
377*5b2fc499SJeff Garzik 		dev->stats.rx_errors++;
378*5b2fc499SJeff Garzik 		skb_queue_tail (&dev->done, skb);
379*5b2fc499SJeff Garzik 	}
380*5b2fc499SJeff Garzik }
381*5b2fc499SJeff Garzik 
382*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
383*5b2fc499SJeff Garzik 
384*5b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
385*5b2fc499SJeff Garzik {
386*5b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
387*5b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
388*5b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
389*5b2fc499SJeff Garzik 	int			urb_status = urb->status;
390*5b2fc499SJeff Garzik 
391*5b2fc499SJeff Garzik 	skb_put (skb, urb->actual_length);
392*5b2fc499SJeff Garzik 	entry->state = rx_done;
393*5b2fc499SJeff Garzik 	entry->urb = NULL;
394*5b2fc499SJeff Garzik 
395*5b2fc499SJeff Garzik 	switch (urb_status) {
396*5b2fc499SJeff Garzik 	    // success
397*5b2fc499SJeff Garzik 	    case 0:
398*5b2fc499SJeff Garzik 		if (skb->len < dev->net->hard_header_len) {
399*5b2fc499SJeff Garzik 			entry->state = rx_cleanup;
400*5b2fc499SJeff Garzik 			dev->stats.rx_errors++;
401*5b2fc499SJeff Garzik 			dev->stats.rx_length_errors++;
402*5b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
403*5b2fc499SJeff Garzik 				devdbg (dev, "rx length %d", skb->len);
404*5b2fc499SJeff Garzik 		}
405*5b2fc499SJeff Garzik 		break;
406*5b2fc499SJeff Garzik 
407*5b2fc499SJeff Garzik 	    // stalls need manual reset. this is rare ... except that
408*5b2fc499SJeff Garzik 	    // when going through USB 2.0 TTs, unplug appears this way.
409*5b2fc499SJeff Garzik 	    // we avoid the highspeed version of the ETIMEOUT/EILSEQ
410*5b2fc499SJeff Garzik 	    // storm, recovering as needed.
411*5b2fc499SJeff Garzik 	    case -EPIPE:
412*5b2fc499SJeff Garzik 		dev->stats.rx_errors++;
413*5b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
414*5b2fc499SJeff Garzik 		// FALLTHROUGH
415*5b2fc499SJeff Garzik 
416*5b2fc499SJeff Garzik 	    // software-driven interface shutdown
417*5b2fc499SJeff Garzik 	    case -ECONNRESET:		// async unlink
418*5b2fc499SJeff Garzik 	    case -ESHUTDOWN:		// hardware gone
419*5b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
420*5b2fc499SJeff Garzik 			devdbg (dev, "rx shutdown, code %d", urb_status);
421*5b2fc499SJeff Garzik 		goto block;
422*5b2fc499SJeff Garzik 
423*5b2fc499SJeff Garzik 	    // we get controller i/o faults during khubd disconnect() delays.
424*5b2fc499SJeff Garzik 	    // throttle down resubmits, to avoid log floods; just temporarily,
425*5b2fc499SJeff Garzik 	    // so we still recover when the fault isn't a khubd delay.
426*5b2fc499SJeff Garzik 	    case -EPROTO:
427*5b2fc499SJeff Garzik 	    case -ETIME:
428*5b2fc499SJeff Garzik 	    case -EILSEQ:
429*5b2fc499SJeff Garzik 		dev->stats.rx_errors++;
430*5b2fc499SJeff Garzik 		if (!timer_pending (&dev->delay)) {
431*5b2fc499SJeff Garzik 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
432*5b2fc499SJeff Garzik 			if (netif_msg_link (dev))
433*5b2fc499SJeff Garzik 				devdbg (dev, "rx throttle %d", urb_status);
434*5b2fc499SJeff Garzik 		}
435*5b2fc499SJeff Garzik block:
436*5b2fc499SJeff Garzik 		entry->state = rx_cleanup;
437*5b2fc499SJeff Garzik 		entry->urb = urb;
438*5b2fc499SJeff Garzik 		urb = NULL;
439*5b2fc499SJeff Garzik 		break;
440*5b2fc499SJeff Garzik 
441*5b2fc499SJeff Garzik 	    // data overrun ... flush fifo?
442*5b2fc499SJeff Garzik 	    case -EOVERFLOW:
443*5b2fc499SJeff Garzik 		dev->stats.rx_over_errors++;
444*5b2fc499SJeff Garzik 		// FALLTHROUGH
445*5b2fc499SJeff Garzik 
446*5b2fc499SJeff Garzik 	    default:
447*5b2fc499SJeff Garzik 		entry->state = rx_cleanup;
448*5b2fc499SJeff Garzik 		dev->stats.rx_errors++;
449*5b2fc499SJeff Garzik 		if (netif_msg_rx_err (dev))
450*5b2fc499SJeff Garzik 			devdbg (dev, "rx status %d", urb_status);
451*5b2fc499SJeff Garzik 		break;
452*5b2fc499SJeff Garzik 	}
453*5b2fc499SJeff Garzik 
454*5b2fc499SJeff Garzik 	defer_bh(dev, skb, &dev->rxq);
455*5b2fc499SJeff Garzik 
456*5b2fc499SJeff Garzik 	if (urb) {
457*5b2fc499SJeff Garzik 		if (netif_running (dev->net)
458*5b2fc499SJeff Garzik 				&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
459*5b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_ATOMIC);
460*5b2fc499SJeff Garzik 			return;
461*5b2fc499SJeff Garzik 		}
462*5b2fc499SJeff Garzik 		usb_free_urb (urb);
463*5b2fc499SJeff Garzik 	}
464*5b2fc499SJeff Garzik 	if (netif_msg_rx_err (dev))
465*5b2fc499SJeff Garzik 		devdbg (dev, "no read resubmitted");
466*5b2fc499SJeff Garzik }
467*5b2fc499SJeff Garzik 
468*5b2fc499SJeff Garzik static void intr_complete (struct urb *urb)
469*5b2fc499SJeff Garzik {
470*5b2fc499SJeff Garzik 	struct usbnet	*dev = urb->context;
471*5b2fc499SJeff Garzik 	int		status = urb->status;
472*5b2fc499SJeff Garzik 
473*5b2fc499SJeff Garzik 	switch (status) {
474*5b2fc499SJeff Garzik 	    /* success */
475*5b2fc499SJeff Garzik 	    case 0:
476*5b2fc499SJeff Garzik 		dev->driver_info->status(dev, urb);
477*5b2fc499SJeff Garzik 		break;
478*5b2fc499SJeff Garzik 
479*5b2fc499SJeff Garzik 	    /* software-driven interface shutdown */
480*5b2fc499SJeff Garzik 	    case -ENOENT:		// urb killed
481*5b2fc499SJeff Garzik 	    case -ESHUTDOWN:		// hardware gone
482*5b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
483*5b2fc499SJeff Garzik 			devdbg (dev, "intr shutdown, code %d", status);
484*5b2fc499SJeff Garzik 		return;
485*5b2fc499SJeff Garzik 
486*5b2fc499SJeff Garzik 	    /* NOTE:  not throttling like RX/TX, since this endpoint
487*5b2fc499SJeff Garzik 	     * already polls infrequently
488*5b2fc499SJeff Garzik 	     */
489*5b2fc499SJeff Garzik 	    default:
490*5b2fc499SJeff Garzik 		devdbg (dev, "intr status %d", status);
491*5b2fc499SJeff Garzik 		break;
492*5b2fc499SJeff Garzik 	}
493*5b2fc499SJeff Garzik 
494*5b2fc499SJeff Garzik 	if (!netif_running (dev->net))
495*5b2fc499SJeff Garzik 		return;
496*5b2fc499SJeff Garzik 
497*5b2fc499SJeff Garzik 	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
498*5b2fc499SJeff Garzik 	status = usb_submit_urb (urb, GFP_ATOMIC);
499*5b2fc499SJeff Garzik 	if (status != 0 && netif_msg_timer (dev))
500*5b2fc499SJeff Garzik 		deverr(dev, "intr resubmit --> %d", status);
501*5b2fc499SJeff Garzik }
502*5b2fc499SJeff Garzik 
503*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
504*5b2fc499SJeff Garzik 
505*5b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
506*5b2fc499SJeff Garzik 
507*5b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
508*5b2fc499SJeff Garzik {
509*5b2fc499SJeff Garzik 	unsigned long		flags;
510*5b2fc499SJeff Garzik 	struct sk_buff		*skb, *skbnext;
511*5b2fc499SJeff Garzik 	int			count = 0;
512*5b2fc499SJeff Garzik 
513*5b2fc499SJeff Garzik 	spin_lock_irqsave (&q->lock, flags);
514*5b2fc499SJeff Garzik 	for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
515*5b2fc499SJeff Garzik 		struct skb_data		*entry;
516*5b2fc499SJeff Garzik 		struct urb		*urb;
517*5b2fc499SJeff Garzik 		int			retval;
518*5b2fc499SJeff Garzik 
519*5b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
520*5b2fc499SJeff Garzik 		urb = entry->urb;
521*5b2fc499SJeff Garzik 		skbnext = skb->next;
522*5b2fc499SJeff Garzik 
523*5b2fc499SJeff Garzik 		// during some PM-driven resume scenarios,
524*5b2fc499SJeff Garzik 		// these (async) unlinks complete immediately
525*5b2fc499SJeff Garzik 		retval = usb_unlink_urb (urb);
526*5b2fc499SJeff Garzik 		if (retval != -EINPROGRESS && retval != 0)
527*5b2fc499SJeff Garzik 			devdbg (dev, "unlink urb err, %d", retval);
528*5b2fc499SJeff Garzik 		else
529*5b2fc499SJeff Garzik 			count++;
530*5b2fc499SJeff Garzik 	}
531*5b2fc499SJeff Garzik 	spin_unlock_irqrestore (&q->lock, flags);
532*5b2fc499SJeff Garzik 	return count;
533*5b2fc499SJeff Garzik }
534*5b2fc499SJeff Garzik 
535*5b2fc499SJeff Garzik // Flush all pending rx urbs
536*5b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
537*5b2fc499SJeff Garzik 
538*5b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
539*5b2fc499SJeff Garzik {
540*5b2fc499SJeff Garzik 	if (netif_running(dev->net)) {
541*5b2fc499SJeff Garzik 		(void) unlink_urbs (dev, &dev->rxq);
542*5b2fc499SJeff Garzik 		tasklet_schedule(&dev->bh);
543*5b2fc499SJeff Garzik 	}
544*5b2fc499SJeff Garzik }
545*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
546*5b2fc499SJeff Garzik 
547*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
548*5b2fc499SJeff Garzik 
549*5b2fc499SJeff Garzik // precondition: never called in_interrupt
550*5b2fc499SJeff Garzik 
551*5b2fc499SJeff Garzik static int usbnet_stop (struct net_device *net)
552*5b2fc499SJeff Garzik {
553*5b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
554*5b2fc499SJeff Garzik 	int			temp;
555*5b2fc499SJeff Garzik 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
556*5b2fc499SJeff Garzik 	DECLARE_WAITQUEUE (wait, current);
557*5b2fc499SJeff Garzik 
558*5b2fc499SJeff Garzik 	netif_stop_queue (net);
559*5b2fc499SJeff Garzik 
560*5b2fc499SJeff Garzik 	if (netif_msg_ifdown (dev))
561*5b2fc499SJeff Garzik 		devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
562*5b2fc499SJeff Garzik 			dev->stats.rx_packets, dev->stats.tx_packets,
563*5b2fc499SJeff Garzik 			dev->stats.rx_errors, dev->stats.tx_errors
564*5b2fc499SJeff Garzik 			);
565*5b2fc499SJeff Garzik 
566*5b2fc499SJeff Garzik 	// ensure there are no more active urbs
567*5b2fc499SJeff Garzik 	add_wait_queue (&unlink_wakeup, &wait);
568*5b2fc499SJeff Garzik 	dev->wait = &unlink_wakeup;
569*5b2fc499SJeff Garzik 	temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
570*5b2fc499SJeff Garzik 
571*5b2fc499SJeff Garzik 	// maybe wait for deletions to finish.
572*5b2fc499SJeff Garzik 	while (!skb_queue_empty(&dev->rxq) &&
573*5b2fc499SJeff Garzik 	       !skb_queue_empty(&dev->txq) &&
574*5b2fc499SJeff Garzik 	       !skb_queue_empty(&dev->done)) {
575*5b2fc499SJeff Garzik 		msleep(UNLINK_TIMEOUT_MS);
576*5b2fc499SJeff Garzik 		if (netif_msg_ifdown (dev))
577*5b2fc499SJeff Garzik 			devdbg (dev, "waited for %d urb completions", temp);
578*5b2fc499SJeff Garzik 	}
579*5b2fc499SJeff Garzik 	dev->wait = NULL;
580*5b2fc499SJeff Garzik 	remove_wait_queue (&unlink_wakeup, &wait);
581*5b2fc499SJeff Garzik 
582*5b2fc499SJeff Garzik 	usb_kill_urb(dev->interrupt);
583*5b2fc499SJeff Garzik 
584*5b2fc499SJeff Garzik 	/* deferred work (task, timer, softirq) must also stop.
585*5b2fc499SJeff Garzik 	 * can't flush_scheduled_work() until we drop rtnl (later),
586*5b2fc499SJeff Garzik 	 * else workers could deadlock; so make workers a NOP.
587*5b2fc499SJeff Garzik 	 */
588*5b2fc499SJeff Garzik 	dev->flags = 0;
589*5b2fc499SJeff Garzik 	del_timer_sync (&dev->delay);
590*5b2fc499SJeff Garzik 	tasklet_kill (&dev->bh);
591*5b2fc499SJeff Garzik 
592*5b2fc499SJeff Garzik 	return 0;
593*5b2fc499SJeff Garzik }
594*5b2fc499SJeff Garzik 
595*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
596*5b2fc499SJeff Garzik 
597*5b2fc499SJeff Garzik // posts reads, and enables write queuing
598*5b2fc499SJeff Garzik 
599*5b2fc499SJeff Garzik // precondition: never called in_interrupt
600*5b2fc499SJeff Garzik 
601*5b2fc499SJeff Garzik static int usbnet_open (struct net_device *net)
602*5b2fc499SJeff Garzik {
603*5b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
604*5b2fc499SJeff Garzik 	int			retval = 0;
605*5b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
606*5b2fc499SJeff Garzik 
607*5b2fc499SJeff Garzik 	// put into "known safe" state
608*5b2fc499SJeff Garzik 	if (info->reset && (retval = info->reset (dev)) < 0) {
609*5b2fc499SJeff Garzik 		if (netif_msg_ifup (dev))
610*5b2fc499SJeff Garzik 			devinfo (dev,
611*5b2fc499SJeff Garzik 				"open reset fail (%d) usbnet usb-%s-%s, %s",
612*5b2fc499SJeff Garzik 				retval,
613*5b2fc499SJeff Garzik 				dev->udev->bus->bus_name, dev->udev->devpath,
614*5b2fc499SJeff Garzik 			info->description);
615*5b2fc499SJeff Garzik 		goto done;
616*5b2fc499SJeff Garzik 	}
617*5b2fc499SJeff Garzik 
618*5b2fc499SJeff Garzik 	// insist peer be connected
619*5b2fc499SJeff Garzik 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
620*5b2fc499SJeff Garzik 		if (netif_msg_ifup (dev))
621*5b2fc499SJeff Garzik 			devdbg (dev, "can't open; %d", retval);
622*5b2fc499SJeff Garzik 		goto done;
623*5b2fc499SJeff Garzik 	}
624*5b2fc499SJeff Garzik 
625*5b2fc499SJeff Garzik 	/* start any status interrupt transfer */
626*5b2fc499SJeff Garzik 	if (dev->interrupt) {
627*5b2fc499SJeff Garzik 		retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
628*5b2fc499SJeff Garzik 		if (retval < 0) {
629*5b2fc499SJeff Garzik 			if (netif_msg_ifup (dev))
630*5b2fc499SJeff Garzik 				deverr (dev, "intr submit %d", retval);
631*5b2fc499SJeff Garzik 			goto done;
632*5b2fc499SJeff Garzik 		}
633*5b2fc499SJeff Garzik 	}
634*5b2fc499SJeff Garzik 
635*5b2fc499SJeff Garzik 	netif_start_queue (net);
636*5b2fc499SJeff Garzik 	if (netif_msg_ifup (dev)) {
637*5b2fc499SJeff Garzik 		char	*framing;
638*5b2fc499SJeff Garzik 
639*5b2fc499SJeff Garzik 		if (dev->driver_info->flags & FLAG_FRAMING_NC)
640*5b2fc499SJeff Garzik 			framing = "NetChip";
641*5b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_GL)
642*5b2fc499SJeff Garzik 			framing = "GeneSys";
643*5b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_Z)
644*5b2fc499SJeff Garzik 			framing = "Zaurus";
645*5b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_RN)
646*5b2fc499SJeff Garzik 			framing = "RNDIS";
647*5b2fc499SJeff Garzik 		else if (dev->driver_info->flags & FLAG_FRAMING_AX)
648*5b2fc499SJeff Garzik 			framing = "ASIX";
649*5b2fc499SJeff Garzik 		else
650*5b2fc499SJeff Garzik 			framing = "simple";
651*5b2fc499SJeff Garzik 
652*5b2fc499SJeff Garzik 		devinfo (dev, "open: enable queueing "
653*5b2fc499SJeff Garzik 				"(rx %d, tx %d) mtu %d %s framing",
654*5b2fc499SJeff Garzik 			(int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
655*5b2fc499SJeff Garzik 			framing);
656*5b2fc499SJeff Garzik 	}
657*5b2fc499SJeff Garzik 
658*5b2fc499SJeff Garzik 	// delay posting reads until we're fully open
659*5b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
660*5b2fc499SJeff Garzik done:
661*5b2fc499SJeff Garzik 	return retval;
662*5b2fc499SJeff Garzik }
663*5b2fc499SJeff Garzik 
664*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
665*5b2fc499SJeff Garzik 
666*5b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
667*5b2fc499SJeff Garzik  * they'll probably want to use this base set.
668*5b2fc499SJeff Garzik  */
669*5b2fc499SJeff Garzik 
670*5b2fc499SJeff Garzik #if defined(CONFIG_MII) || defined(CONFIG_MII_MODULE)
671*5b2fc499SJeff Garzik #define HAVE_MII
672*5b2fc499SJeff Garzik 
673*5b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
674*5b2fc499SJeff Garzik {
675*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
676*5b2fc499SJeff Garzik 
677*5b2fc499SJeff Garzik 	if (!dev->mii.mdio_read)
678*5b2fc499SJeff Garzik 		return -EOPNOTSUPP;
679*5b2fc499SJeff Garzik 
680*5b2fc499SJeff Garzik 	return mii_ethtool_gset(&dev->mii, cmd);
681*5b2fc499SJeff Garzik }
682*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings);
683*5b2fc499SJeff Garzik 
684*5b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
685*5b2fc499SJeff Garzik {
686*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
687*5b2fc499SJeff Garzik 	int retval;
688*5b2fc499SJeff Garzik 
689*5b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
690*5b2fc499SJeff Garzik 		return -EOPNOTSUPP;
691*5b2fc499SJeff Garzik 
692*5b2fc499SJeff Garzik 	retval = mii_ethtool_sset(&dev->mii, cmd);
693*5b2fc499SJeff Garzik 
694*5b2fc499SJeff Garzik 	/* link speed/duplex might have changed */
695*5b2fc499SJeff Garzik 	if (dev->driver_info->link_reset)
696*5b2fc499SJeff Garzik 		dev->driver_info->link_reset(dev);
697*5b2fc499SJeff Garzik 
698*5b2fc499SJeff Garzik 	return retval;
699*5b2fc499SJeff Garzik 
700*5b2fc499SJeff Garzik }
701*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings);
702*5b2fc499SJeff Garzik 
703*5b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
704*5b2fc499SJeff Garzik {
705*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
706*5b2fc499SJeff Garzik 
707*5b2fc499SJeff Garzik 	/* If a check_connect is defined, return its result */
708*5b2fc499SJeff Garzik 	if (dev->driver_info->check_connect)
709*5b2fc499SJeff Garzik 		return dev->driver_info->check_connect (dev) == 0;
710*5b2fc499SJeff Garzik 
711*5b2fc499SJeff Garzik 	/* if the device has mii operations, use those */
712*5b2fc499SJeff Garzik 	if (dev->mii.mdio_read)
713*5b2fc499SJeff Garzik 		return mii_link_ok(&dev->mii);
714*5b2fc499SJeff Garzik 
715*5b2fc499SJeff Garzik 	/* Otherwise, say we're up (to avoid breaking scripts) */
716*5b2fc499SJeff Garzik 	return 1;
717*5b2fc499SJeff Garzik }
718*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
719*5b2fc499SJeff Garzik 
720*5b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
721*5b2fc499SJeff Garzik {
722*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
723*5b2fc499SJeff Garzik 
724*5b2fc499SJeff Garzik 	if (!dev->mii.mdio_write)
725*5b2fc499SJeff Garzik 		return -EOPNOTSUPP;
726*5b2fc499SJeff Garzik 
727*5b2fc499SJeff Garzik 	return mii_nway_restart(&dev->mii);
728*5b2fc499SJeff Garzik }
729*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
730*5b2fc499SJeff Garzik 
731*5b2fc499SJeff Garzik #endif	/* HAVE_MII */
732*5b2fc499SJeff Garzik 
733*5b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
734*5b2fc499SJeff Garzik {
735*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
736*5b2fc499SJeff Garzik 
737*5b2fc499SJeff Garzik 	strncpy (info->driver, dev->driver_name, sizeof info->driver);
738*5b2fc499SJeff Garzik 	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
739*5b2fc499SJeff Garzik 	strncpy (info->fw_version, dev->driver_info->description,
740*5b2fc499SJeff Garzik 		sizeof info->fw_version);
741*5b2fc499SJeff Garzik 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
742*5b2fc499SJeff Garzik }
743*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
744*5b2fc499SJeff Garzik 
745*5b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
746*5b2fc499SJeff Garzik {
747*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
748*5b2fc499SJeff Garzik 
749*5b2fc499SJeff Garzik 	return dev->msg_enable;
750*5b2fc499SJeff Garzik }
751*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
752*5b2fc499SJeff Garzik 
753*5b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
754*5b2fc499SJeff Garzik {
755*5b2fc499SJeff Garzik 	struct usbnet *dev = netdev_priv(net);
756*5b2fc499SJeff Garzik 
757*5b2fc499SJeff Garzik 	dev->msg_enable = level;
758*5b2fc499SJeff Garzik }
759*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
760*5b2fc499SJeff Garzik 
761*5b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
762*5b2fc499SJeff Garzik static struct ethtool_ops usbnet_ethtool_ops = {
763*5b2fc499SJeff Garzik #ifdef	HAVE_MII
764*5b2fc499SJeff Garzik 	.get_settings		= usbnet_get_settings,
765*5b2fc499SJeff Garzik 	.set_settings		= usbnet_set_settings,
766*5b2fc499SJeff Garzik 	.get_link		= usbnet_get_link,
767*5b2fc499SJeff Garzik 	.nway_reset		= usbnet_nway_reset,
768*5b2fc499SJeff Garzik #endif
769*5b2fc499SJeff Garzik 	.get_drvinfo		= usbnet_get_drvinfo,
770*5b2fc499SJeff Garzik 	.get_msglevel		= usbnet_get_msglevel,
771*5b2fc499SJeff Garzik 	.set_msglevel		= usbnet_set_msglevel,
772*5b2fc499SJeff Garzik };
773*5b2fc499SJeff Garzik 
774*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
775*5b2fc499SJeff Garzik 
776*5b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
777*5b2fc499SJeff Garzik  *
778*5b2fc499SJeff Garzik  * NOTE:  with 2.5 we could do more of this using completion callbacks,
779*5b2fc499SJeff Garzik  * especially now that control transfers can be queued.
780*5b2fc499SJeff Garzik  */
781*5b2fc499SJeff Garzik static void
782*5b2fc499SJeff Garzik kevent (struct work_struct *work)
783*5b2fc499SJeff Garzik {
784*5b2fc499SJeff Garzik 	struct usbnet		*dev =
785*5b2fc499SJeff Garzik 		container_of(work, struct usbnet, kevent);
786*5b2fc499SJeff Garzik 	int			status;
787*5b2fc499SJeff Garzik 
788*5b2fc499SJeff Garzik 	/* usb_clear_halt() needs a thread context */
789*5b2fc499SJeff Garzik 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
790*5b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->txq);
791*5b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->out);
792*5b2fc499SJeff Garzik 		if (status < 0
793*5b2fc499SJeff Garzik 				&& status != -EPIPE
794*5b2fc499SJeff Garzik 				&& status != -ESHUTDOWN) {
795*5b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
796*5b2fc499SJeff Garzik 				deverr (dev, "can't clear tx halt, status %d",
797*5b2fc499SJeff Garzik 					status);
798*5b2fc499SJeff Garzik 		} else {
799*5b2fc499SJeff Garzik 			clear_bit (EVENT_TX_HALT, &dev->flags);
800*5b2fc499SJeff Garzik 			if (status != -ESHUTDOWN)
801*5b2fc499SJeff Garzik 				netif_wake_queue (dev->net);
802*5b2fc499SJeff Garzik 		}
803*5b2fc499SJeff Garzik 	}
804*5b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
805*5b2fc499SJeff Garzik 		unlink_urbs (dev, &dev->rxq);
806*5b2fc499SJeff Garzik 		status = usb_clear_halt (dev->udev, dev->in);
807*5b2fc499SJeff Garzik 		if (status < 0
808*5b2fc499SJeff Garzik 				&& status != -EPIPE
809*5b2fc499SJeff Garzik 				&& status != -ESHUTDOWN) {
810*5b2fc499SJeff Garzik 			if (netif_msg_rx_err (dev))
811*5b2fc499SJeff Garzik 				deverr (dev, "can't clear rx halt, status %d",
812*5b2fc499SJeff Garzik 					status);
813*5b2fc499SJeff Garzik 		} else {
814*5b2fc499SJeff Garzik 			clear_bit (EVENT_RX_HALT, &dev->flags);
815*5b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
816*5b2fc499SJeff Garzik 		}
817*5b2fc499SJeff Garzik 	}
818*5b2fc499SJeff Garzik 
819*5b2fc499SJeff Garzik 	/* tasklet could resubmit itself forever if memory is tight */
820*5b2fc499SJeff Garzik 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
821*5b2fc499SJeff Garzik 		struct urb	*urb = NULL;
822*5b2fc499SJeff Garzik 
823*5b2fc499SJeff Garzik 		if (netif_running (dev->net))
824*5b2fc499SJeff Garzik 			urb = usb_alloc_urb (0, GFP_KERNEL);
825*5b2fc499SJeff Garzik 		else
826*5b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
827*5b2fc499SJeff Garzik 		if (urb != NULL) {
828*5b2fc499SJeff Garzik 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
829*5b2fc499SJeff Garzik 			rx_submit (dev, urb, GFP_KERNEL);
830*5b2fc499SJeff Garzik 			tasklet_schedule (&dev->bh);
831*5b2fc499SJeff Garzik 		}
832*5b2fc499SJeff Garzik 	}
833*5b2fc499SJeff Garzik 
834*5b2fc499SJeff Garzik 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
835*5b2fc499SJeff Garzik 		struct driver_info	*info = dev->driver_info;
836*5b2fc499SJeff Garzik 		int			retval = 0;
837*5b2fc499SJeff Garzik 
838*5b2fc499SJeff Garzik 		clear_bit (EVENT_LINK_RESET, &dev->flags);
839*5b2fc499SJeff Garzik 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
840*5b2fc499SJeff Garzik 			devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
841*5b2fc499SJeff Garzik 				retval,
842*5b2fc499SJeff Garzik 				dev->udev->bus->bus_name, dev->udev->devpath,
843*5b2fc499SJeff Garzik 				info->description);
844*5b2fc499SJeff Garzik 		}
845*5b2fc499SJeff Garzik 	}
846*5b2fc499SJeff Garzik 
847*5b2fc499SJeff Garzik 	if (dev->flags)
848*5b2fc499SJeff Garzik 		devdbg (dev, "kevent done, flags = 0x%lx",
849*5b2fc499SJeff Garzik 			dev->flags);
850*5b2fc499SJeff Garzik }
851*5b2fc499SJeff Garzik 
852*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
853*5b2fc499SJeff Garzik 
854*5b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
855*5b2fc499SJeff Garzik {
856*5b2fc499SJeff Garzik 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
857*5b2fc499SJeff Garzik 	struct skb_data		*entry = (struct skb_data *) skb->cb;
858*5b2fc499SJeff Garzik 	struct usbnet		*dev = entry->dev;
859*5b2fc499SJeff Garzik 
860*5b2fc499SJeff Garzik 	if (urb->status == 0) {
861*5b2fc499SJeff Garzik 		dev->stats.tx_packets++;
862*5b2fc499SJeff Garzik 		dev->stats.tx_bytes += entry->length;
863*5b2fc499SJeff Garzik 	} else {
864*5b2fc499SJeff Garzik 		dev->stats.tx_errors++;
865*5b2fc499SJeff Garzik 
866*5b2fc499SJeff Garzik 		switch (urb->status) {
867*5b2fc499SJeff Garzik 		case -EPIPE:
868*5b2fc499SJeff Garzik 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
869*5b2fc499SJeff Garzik 			break;
870*5b2fc499SJeff Garzik 
871*5b2fc499SJeff Garzik 		/* software-driven interface shutdown */
872*5b2fc499SJeff Garzik 		case -ECONNRESET:		// async unlink
873*5b2fc499SJeff Garzik 		case -ESHUTDOWN:		// hardware gone
874*5b2fc499SJeff Garzik 			break;
875*5b2fc499SJeff Garzik 
876*5b2fc499SJeff Garzik 		// like rx, tx gets controller i/o faults during khubd delays
877*5b2fc499SJeff Garzik 		// and so it uses the same throttling mechanism.
878*5b2fc499SJeff Garzik 		case -EPROTO:
879*5b2fc499SJeff Garzik 		case -ETIME:
880*5b2fc499SJeff Garzik 		case -EILSEQ:
881*5b2fc499SJeff Garzik 			if (!timer_pending (&dev->delay)) {
882*5b2fc499SJeff Garzik 				mod_timer (&dev->delay,
883*5b2fc499SJeff Garzik 					jiffies + THROTTLE_JIFFIES);
884*5b2fc499SJeff Garzik 				if (netif_msg_link (dev))
885*5b2fc499SJeff Garzik 					devdbg (dev, "tx throttle %d",
886*5b2fc499SJeff Garzik 							urb->status);
887*5b2fc499SJeff Garzik 			}
888*5b2fc499SJeff Garzik 			netif_stop_queue (dev->net);
889*5b2fc499SJeff Garzik 			break;
890*5b2fc499SJeff Garzik 		default:
891*5b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
892*5b2fc499SJeff Garzik 				devdbg (dev, "tx err %d", entry->urb->status);
893*5b2fc499SJeff Garzik 			break;
894*5b2fc499SJeff Garzik 		}
895*5b2fc499SJeff Garzik 	}
896*5b2fc499SJeff Garzik 
897*5b2fc499SJeff Garzik 	urb->dev = NULL;
898*5b2fc499SJeff Garzik 	entry->state = tx_done;
899*5b2fc499SJeff Garzik 	defer_bh(dev, skb, &dev->txq);
900*5b2fc499SJeff Garzik }
901*5b2fc499SJeff Garzik 
902*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
903*5b2fc499SJeff Garzik 
904*5b2fc499SJeff Garzik static void usbnet_tx_timeout (struct net_device *net)
905*5b2fc499SJeff Garzik {
906*5b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
907*5b2fc499SJeff Garzik 
908*5b2fc499SJeff Garzik 	unlink_urbs (dev, &dev->txq);
909*5b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
910*5b2fc499SJeff Garzik 
911*5b2fc499SJeff Garzik 	// FIXME: device recovery -- reset?
912*5b2fc499SJeff Garzik }
913*5b2fc499SJeff Garzik 
914*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
915*5b2fc499SJeff Garzik 
916*5b2fc499SJeff Garzik static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
917*5b2fc499SJeff Garzik {
918*5b2fc499SJeff Garzik 	struct usbnet		*dev = netdev_priv(net);
919*5b2fc499SJeff Garzik 	int			length;
920*5b2fc499SJeff Garzik 	int			retval = NET_XMIT_SUCCESS;
921*5b2fc499SJeff Garzik 	struct urb		*urb = NULL;
922*5b2fc499SJeff Garzik 	struct skb_data		*entry;
923*5b2fc499SJeff Garzik 	struct driver_info	*info = dev->driver_info;
924*5b2fc499SJeff Garzik 	unsigned long		flags;
925*5b2fc499SJeff Garzik 
926*5b2fc499SJeff Garzik 	// some devices want funky USB-level framing, for
927*5b2fc499SJeff Garzik 	// win32 driver (usually) and/or hardware quirks
928*5b2fc499SJeff Garzik 	if (info->tx_fixup) {
929*5b2fc499SJeff Garzik 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
930*5b2fc499SJeff Garzik 		if (!skb) {
931*5b2fc499SJeff Garzik 			if (netif_msg_tx_err (dev))
932*5b2fc499SJeff Garzik 				devdbg (dev, "can't tx_fixup skb");
933*5b2fc499SJeff Garzik 			goto drop;
934*5b2fc499SJeff Garzik 		}
935*5b2fc499SJeff Garzik 	}
936*5b2fc499SJeff Garzik 	length = skb->len;
937*5b2fc499SJeff Garzik 
938*5b2fc499SJeff Garzik 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
939*5b2fc499SJeff Garzik 		if (netif_msg_tx_err (dev))
940*5b2fc499SJeff Garzik 			devdbg (dev, "no urb");
941*5b2fc499SJeff Garzik 		goto drop;
942*5b2fc499SJeff Garzik 	}
943*5b2fc499SJeff Garzik 
944*5b2fc499SJeff Garzik 	entry = (struct skb_data *) skb->cb;
945*5b2fc499SJeff Garzik 	entry->urb = urb;
946*5b2fc499SJeff Garzik 	entry->dev = dev;
947*5b2fc499SJeff Garzik 	entry->state = tx_start;
948*5b2fc499SJeff Garzik 	entry->length = length;
949*5b2fc499SJeff Garzik 
950*5b2fc499SJeff Garzik 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
951*5b2fc499SJeff Garzik 			skb->data, skb->len, tx_complete, skb);
952*5b2fc499SJeff Garzik 
953*5b2fc499SJeff Garzik 	/* don't assume the hardware handles USB_ZERO_PACKET
954*5b2fc499SJeff Garzik 	 * NOTE:  strictly conforming cdc-ether devices should expect
955*5b2fc499SJeff Garzik 	 * the ZLP here, but ignore the one-byte packet.
956*5b2fc499SJeff Garzik 	 *
957*5b2fc499SJeff Garzik 	 * FIXME zero that byte, if it doesn't require a new skb.
958*5b2fc499SJeff Garzik 	 */
959*5b2fc499SJeff Garzik 	if ((length % dev->maxpacket) == 0)
960*5b2fc499SJeff Garzik 		urb->transfer_buffer_length++;
961*5b2fc499SJeff Garzik 
962*5b2fc499SJeff Garzik 	spin_lock_irqsave (&dev->txq.lock, flags);
963*5b2fc499SJeff Garzik 
964*5b2fc499SJeff Garzik 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
965*5b2fc499SJeff Garzik 	case -EPIPE:
966*5b2fc499SJeff Garzik 		netif_stop_queue (net);
967*5b2fc499SJeff Garzik 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
968*5b2fc499SJeff Garzik 		break;
969*5b2fc499SJeff Garzik 	default:
970*5b2fc499SJeff Garzik 		if (netif_msg_tx_err (dev))
971*5b2fc499SJeff Garzik 			devdbg (dev, "tx: submit urb err %d", retval);
972*5b2fc499SJeff Garzik 		break;
973*5b2fc499SJeff Garzik 	case 0:
974*5b2fc499SJeff Garzik 		net->trans_start = jiffies;
975*5b2fc499SJeff Garzik 		__skb_queue_tail (&dev->txq, skb);
976*5b2fc499SJeff Garzik 		if (dev->txq.qlen >= TX_QLEN (dev))
977*5b2fc499SJeff Garzik 			netif_stop_queue (net);
978*5b2fc499SJeff Garzik 	}
979*5b2fc499SJeff Garzik 	spin_unlock_irqrestore (&dev->txq.lock, flags);
980*5b2fc499SJeff Garzik 
981*5b2fc499SJeff Garzik 	if (retval) {
982*5b2fc499SJeff Garzik 		if (netif_msg_tx_err (dev))
983*5b2fc499SJeff Garzik 			devdbg (dev, "drop, code %d", retval);
984*5b2fc499SJeff Garzik drop:
985*5b2fc499SJeff Garzik 		retval = NET_XMIT_SUCCESS;
986*5b2fc499SJeff Garzik 		dev->stats.tx_dropped++;
987*5b2fc499SJeff Garzik 		if (skb)
988*5b2fc499SJeff Garzik 			dev_kfree_skb_any (skb);
989*5b2fc499SJeff Garzik 		usb_free_urb (urb);
990*5b2fc499SJeff Garzik 	} else if (netif_msg_tx_queued (dev)) {
991*5b2fc499SJeff Garzik 		devdbg (dev, "> tx, len %d, type 0x%x",
992*5b2fc499SJeff Garzik 			length, skb->protocol);
993*5b2fc499SJeff Garzik 	}
994*5b2fc499SJeff Garzik 	return retval;
995*5b2fc499SJeff Garzik }
996*5b2fc499SJeff Garzik 
997*5b2fc499SJeff Garzik 
998*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
999*5b2fc499SJeff Garzik 
1000*5b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
1001*5b2fc499SJeff Garzik 
1002*5b2fc499SJeff Garzik static void usbnet_bh (unsigned long param)
1003*5b2fc499SJeff Garzik {
1004*5b2fc499SJeff Garzik 	struct usbnet		*dev = (struct usbnet *) param;
1005*5b2fc499SJeff Garzik 	struct sk_buff		*skb;
1006*5b2fc499SJeff Garzik 	struct skb_data		*entry;
1007*5b2fc499SJeff Garzik 
1008*5b2fc499SJeff Garzik 	while ((skb = skb_dequeue (&dev->done))) {
1009*5b2fc499SJeff Garzik 		entry = (struct skb_data *) skb->cb;
1010*5b2fc499SJeff Garzik 		switch (entry->state) {
1011*5b2fc499SJeff Garzik 		    case rx_done:
1012*5b2fc499SJeff Garzik 			entry->state = rx_cleanup;
1013*5b2fc499SJeff Garzik 			rx_process (dev, skb);
1014*5b2fc499SJeff Garzik 			continue;
1015*5b2fc499SJeff Garzik 		    case tx_done:
1016*5b2fc499SJeff Garzik 		    case rx_cleanup:
1017*5b2fc499SJeff Garzik 			usb_free_urb (entry->urb);
1018*5b2fc499SJeff Garzik 			dev_kfree_skb (skb);
1019*5b2fc499SJeff Garzik 			continue;
1020*5b2fc499SJeff Garzik 		    default:
1021*5b2fc499SJeff Garzik 			devdbg (dev, "bogus skb state %d", entry->state);
1022*5b2fc499SJeff Garzik 		}
1023*5b2fc499SJeff Garzik 	}
1024*5b2fc499SJeff Garzik 
1025*5b2fc499SJeff Garzik 	// waiting for all pending urbs to complete?
1026*5b2fc499SJeff Garzik 	if (dev->wait) {
1027*5b2fc499SJeff Garzik 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1028*5b2fc499SJeff Garzik 			wake_up (dev->wait);
1029*5b2fc499SJeff Garzik 		}
1030*5b2fc499SJeff Garzik 
1031*5b2fc499SJeff Garzik 	// or are we maybe short a few urbs?
1032*5b2fc499SJeff Garzik 	} else if (netif_running (dev->net)
1033*5b2fc499SJeff Garzik 			&& netif_device_present (dev->net)
1034*5b2fc499SJeff Garzik 			&& !timer_pending (&dev->delay)
1035*5b2fc499SJeff Garzik 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
1036*5b2fc499SJeff Garzik 		int	temp = dev->rxq.qlen;
1037*5b2fc499SJeff Garzik 		int	qlen = RX_QLEN (dev);
1038*5b2fc499SJeff Garzik 
1039*5b2fc499SJeff Garzik 		if (temp < qlen) {
1040*5b2fc499SJeff Garzik 			struct urb	*urb;
1041*5b2fc499SJeff Garzik 			int		i;
1042*5b2fc499SJeff Garzik 
1043*5b2fc499SJeff Garzik 			// don't refill the queue all at once
1044*5b2fc499SJeff Garzik 			for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1045*5b2fc499SJeff Garzik 				urb = usb_alloc_urb (0, GFP_ATOMIC);
1046*5b2fc499SJeff Garzik 				if (urb != NULL)
1047*5b2fc499SJeff Garzik 					rx_submit (dev, urb, GFP_ATOMIC);
1048*5b2fc499SJeff Garzik 			}
1049*5b2fc499SJeff Garzik 			if (temp != dev->rxq.qlen && netif_msg_link (dev))
1050*5b2fc499SJeff Garzik 				devdbg (dev, "rxqlen %d --> %d",
1051*5b2fc499SJeff Garzik 						temp, dev->rxq.qlen);
1052*5b2fc499SJeff Garzik 			if (dev->rxq.qlen < qlen)
1053*5b2fc499SJeff Garzik 				tasklet_schedule (&dev->bh);
1054*5b2fc499SJeff Garzik 		}
1055*5b2fc499SJeff Garzik 		if (dev->txq.qlen < TX_QLEN (dev))
1056*5b2fc499SJeff Garzik 			netif_wake_queue (dev->net);
1057*5b2fc499SJeff Garzik 	}
1058*5b2fc499SJeff Garzik }
1059*5b2fc499SJeff Garzik 
1060*5b2fc499SJeff Garzik 
1061*5b2fc499SJeff Garzik 
1062*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------
1063*5b2fc499SJeff Garzik  *
1064*5b2fc499SJeff Garzik  * USB Device Driver support
1065*5b2fc499SJeff Garzik  *
1066*5b2fc499SJeff Garzik  *-------------------------------------------------------------------------*/
1067*5b2fc499SJeff Garzik 
1068*5b2fc499SJeff Garzik // precondition: never called in_interrupt
1069*5b2fc499SJeff Garzik 
1070*5b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
1071*5b2fc499SJeff Garzik {
1072*5b2fc499SJeff Garzik 	struct usbnet		*dev;
1073*5b2fc499SJeff Garzik 	struct usb_device	*xdev;
1074*5b2fc499SJeff Garzik 	struct net_device	*net;
1075*5b2fc499SJeff Garzik 
1076*5b2fc499SJeff Garzik 	dev = usb_get_intfdata(intf);
1077*5b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
1078*5b2fc499SJeff Garzik 	if (!dev)
1079*5b2fc499SJeff Garzik 		return;
1080*5b2fc499SJeff Garzik 
1081*5b2fc499SJeff Garzik 	xdev = interface_to_usbdev (intf);
1082*5b2fc499SJeff Garzik 
1083*5b2fc499SJeff Garzik 	if (netif_msg_probe (dev))
1084*5b2fc499SJeff Garzik 		devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1085*5b2fc499SJeff Garzik 			intf->dev.driver->name,
1086*5b2fc499SJeff Garzik 			xdev->bus->bus_name, xdev->devpath,
1087*5b2fc499SJeff Garzik 			dev->driver_info->description);
1088*5b2fc499SJeff Garzik 
1089*5b2fc499SJeff Garzik 	net = dev->net;
1090*5b2fc499SJeff Garzik 	unregister_netdev (net);
1091*5b2fc499SJeff Garzik 
1092*5b2fc499SJeff Garzik 	/* we don't hold rtnl here ... */
1093*5b2fc499SJeff Garzik 	flush_scheduled_work ();
1094*5b2fc499SJeff Garzik 
1095*5b2fc499SJeff Garzik 	if (dev->driver_info->unbind)
1096*5b2fc499SJeff Garzik 		dev->driver_info->unbind (dev, intf);
1097*5b2fc499SJeff Garzik 
1098*5b2fc499SJeff Garzik 	free_netdev(net);
1099*5b2fc499SJeff Garzik 	usb_put_dev (xdev);
1100*5b2fc499SJeff Garzik }
1101*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
1102*5b2fc499SJeff Garzik 
1103*5b2fc499SJeff Garzik 
1104*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
1105*5b2fc499SJeff Garzik 
1106*5b2fc499SJeff Garzik // precondition: never called in_interrupt
1107*5b2fc499SJeff Garzik 
1108*5b2fc499SJeff Garzik int
1109*5b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1110*5b2fc499SJeff Garzik {
1111*5b2fc499SJeff Garzik 	struct usbnet			*dev;
1112*5b2fc499SJeff Garzik 	struct net_device		*net;
1113*5b2fc499SJeff Garzik 	struct usb_host_interface	*interface;
1114*5b2fc499SJeff Garzik 	struct driver_info		*info;
1115*5b2fc499SJeff Garzik 	struct usb_device		*xdev;
1116*5b2fc499SJeff Garzik 	int				status;
1117*5b2fc499SJeff Garzik 	const char			*name;
1118*5b2fc499SJeff Garzik 
1119*5b2fc499SJeff Garzik 	name = udev->dev.driver->name;
1120*5b2fc499SJeff Garzik 	info = (struct driver_info *) prod->driver_info;
1121*5b2fc499SJeff Garzik 	if (!info) {
1122*5b2fc499SJeff Garzik 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1123*5b2fc499SJeff Garzik 		return -ENODEV;
1124*5b2fc499SJeff Garzik 	}
1125*5b2fc499SJeff Garzik 	xdev = interface_to_usbdev (udev);
1126*5b2fc499SJeff Garzik 	interface = udev->cur_altsetting;
1127*5b2fc499SJeff Garzik 
1128*5b2fc499SJeff Garzik 	usb_get_dev (xdev);
1129*5b2fc499SJeff Garzik 
1130*5b2fc499SJeff Garzik 	status = -ENOMEM;
1131*5b2fc499SJeff Garzik 
1132*5b2fc499SJeff Garzik 	// set up our own records
1133*5b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(*dev));
1134*5b2fc499SJeff Garzik 	if (!net) {
1135*5b2fc499SJeff Garzik 		dbg ("can't kmalloc dev");
1136*5b2fc499SJeff Garzik 		goto out;
1137*5b2fc499SJeff Garzik 	}
1138*5b2fc499SJeff Garzik 
1139*5b2fc499SJeff Garzik 	dev = netdev_priv(net);
1140*5b2fc499SJeff Garzik 	dev->udev = xdev;
1141*5b2fc499SJeff Garzik 	dev->driver_info = info;
1142*5b2fc499SJeff Garzik 	dev->driver_name = name;
1143*5b2fc499SJeff Garzik 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1144*5b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
1145*5b2fc499SJeff Garzik 	skb_queue_head_init (&dev->rxq);
1146*5b2fc499SJeff Garzik 	skb_queue_head_init (&dev->txq);
1147*5b2fc499SJeff Garzik 	skb_queue_head_init (&dev->done);
1148*5b2fc499SJeff Garzik 	dev->bh.func = usbnet_bh;
1149*5b2fc499SJeff Garzik 	dev->bh.data = (unsigned long) dev;
1150*5b2fc499SJeff Garzik 	INIT_WORK (&dev->kevent, kevent);
1151*5b2fc499SJeff Garzik 	dev->delay.function = usbnet_bh;
1152*5b2fc499SJeff Garzik 	dev->delay.data = (unsigned long) dev;
1153*5b2fc499SJeff Garzik 	init_timer (&dev->delay);
1154*5b2fc499SJeff Garzik 	mutex_init (&dev->phy_mutex);
1155*5b2fc499SJeff Garzik 
1156*5b2fc499SJeff Garzik 	SET_MODULE_OWNER (net);
1157*5b2fc499SJeff Garzik 	dev->net = net;
1158*5b2fc499SJeff Garzik 	strcpy (net->name, "usb%d");
1159*5b2fc499SJeff Garzik 	memcpy (net->dev_addr, node_id, sizeof node_id);
1160*5b2fc499SJeff Garzik 
1161*5b2fc499SJeff Garzik 	/* rx and tx sides can use different message sizes;
1162*5b2fc499SJeff Garzik 	 * bind() should set rx_urb_size in that case.
1163*5b2fc499SJeff Garzik 	 */
1164*5b2fc499SJeff Garzik 	dev->hard_mtu = net->mtu + net->hard_header_len;
1165*5b2fc499SJeff Garzik #if 0
1166*5b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures
1167*5b2fc499SJeff Garzik 	// possible with some EHCI controllers
1168*5b2fc499SJeff Garzik 	if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1169*5b2fc499SJeff Garzik 		net->features |= NETIF_F_HIGHDMA;
1170*5b2fc499SJeff Garzik #endif
1171*5b2fc499SJeff Garzik 
1172*5b2fc499SJeff Garzik 	net->change_mtu = usbnet_change_mtu;
1173*5b2fc499SJeff Garzik 	net->get_stats = usbnet_get_stats;
1174*5b2fc499SJeff Garzik 	net->hard_start_xmit = usbnet_start_xmit;
1175*5b2fc499SJeff Garzik 	net->open = usbnet_open;
1176*5b2fc499SJeff Garzik 	net->stop = usbnet_stop;
1177*5b2fc499SJeff Garzik 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1178*5b2fc499SJeff Garzik 	net->tx_timeout = usbnet_tx_timeout;
1179*5b2fc499SJeff Garzik 	net->ethtool_ops = &usbnet_ethtool_ops;
1180*5b2fc499SJeff Garzik 
1181*5b2fc499SJeff Garzik 	// allow device-specific bind/init procedures
1182*5b2fc499SJeff Garzik 	// NOTE net->name still not usable ...
1183*5b2fc499SJeff Garzik 	if (info->bind) {
1184*5b2fc499SJeff Garzik 		status = info->bind (dev, udev);
1185*5b2fc499SJeff Garzik 		if (status < 0)
1186*5b2fc499SJeff Garzik 			goto out1;
1187*5b2fc499SJeff Garzik 
1188*5b2fc499SJeff Garzik 		// heuristic:  "usb%d" for links we know are two-host,
1189*5b2fc499SJeff Garzik 		// else "eth%d" when there's reasonable doubt.  userspace
1190*5b2fc499SJeff Garzik 		// can rename the link if it knows better.
1191*5b2fc499SJeff Garzik 		if ((dev->driver_info->flags & FLAG_ETHER) != 0
1192*5b2fc499SJeff Garzik 				&& (net->dev_addr [0] & 0x02) == 0)
1193*5b2fc499SJeff Garzik 			strcpy (net->name, "eth%d");
1194*5b2fc499SJeff Garzik 
1195*5b2fc499SJeff Garzik 		/* maybe the remote can't receive an Ethernet MTU */
1196*5b2fc499SJeff Garzik 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1197*5b2fc499SJeff Garzik 			net->mtu = dev->hard_mtu - net->hard_header_len;
1198*5b2fc499SJeff Garzik 	} else if (!info->in || !info->out)
1199*5b2fc499SJeff Garzik 		status = usbnet_get_endpoints (dev, udev);
1200*5b2fc499SJeff Garzik 	else {
1201*5b2fc499SJeff Garzik 		dev->in = usb_rcvbulkpipe (xdev, info->in);
1202*5b2fc499SJeff Garzik 		dev->out = usb_sndbulkpipe (xdev, info->out);
1203*5b2fc499SJeff Garzik 		if (!(info->flags & FLAG_NO_SETINT))
1204*5b2fc499SJeff Garzik 			status = usb_set_interface (xdev,
1205*5b2fc499SJeff Garzik 				interface->desc.bInterfaceNumber,
1206*5b2fc499SJeff Garzik 				interface->desc.bAlternateSetting);
1207*5b2fc499SJeff Garzik 		else
1208*5b2fc499SJeff Garzik 			status = 0;
1209*5b2fc499SJeff Garzik 
1210*5b2fc499SJeff Garzik 	}
1211*5b2fc499SJeff Garzik 	if (status == 0 && dev->status)
1212*5b2fc499SJeff Garzik 		status = init_status (dev, udev);
1213*5b2fc499SJeff Garzik 	if (status < 0)
1214*5b2fc499SJeff Garzik 		goto out3;
1215*5b2fc499SJeff Garzik 
1216*5b2fc499SJeff Garzik 	if (!dev->rx_urb_size)
1217*5b2fc499SJeff Garzik 		dev->rx_urb_size = dev->hard_mtu;
1218*5b2fc499SJeff Garzik 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1219*5b2fc499SJeff Garzik 
1220*5b2fc499SJeff Garzik 	SET_NETDEV_DEV(net, &udev->dev);
1221*5b2fc499SJeff Garzik 	status = register_netdev (net);
1222*5b2fc499SJeff Garzik 	if (status)
1223*5b2fc499SJeff Garzik 		goto out3;
1224*5b2fc499SJeff Garzik 	if (netif_msg_probe (dev))
1225*5b2fc499SJeff Garzik 		devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1226*5b2fc499SJeff Garzik 				"%02x:%02x:%02x:%02x:%02x:%02x",
1227*5b2fc499SJeff Garzik 			udev->dev.driver->name,
1228*5b2fc499SJeff Garzik 			xdev->bus->bus_name, xdev->devpath,
1229*5b2fc499SJeff Garzik 			dev->driver_info->description,
1230*5b2fc499SJeff Garzik 			net->dev_addr [0], net->dev_addr [1],
1231*5b2fc499SJeff Garzik 			net->dev_addr [2], net->dev_addr [3],
1232*5b2fc499SJeff Garzik 			net->dev_addr [4], net->dev_addr [5]);
1233*5b2fc499SJeff Garzik 
1234*5b2fc499SJeff Garzik 	// ok, it's ready to go.
1235*5b2fc499SJeff Garzik 	usb_set_intfdata (udev, dev);
1236*5b2fc499SJeff Garzik 
1237*5b2fc499SJeff Garzik 	// start as if the link is up
1238*5b2fc499SJeff Garzik 	netif_device_attach (net);
1239*5b2fc499SJeff Garzik 
1240*5b2fc499SJeff Garzik 	return 0;
1241*5b2fc499SJeff Garzik 
1242*5b2fc499SJeff Garzik out3:
1243*5b2fc499SJeff Garzik 	if (info->unbind)
1244*5b2fc499SJeff Garzik 		info->unbind (dev, udev);
1245*5b2fc499SJeff Garzik out1:
1246*5b2fc499SJeff Garzik 	free_netdev(net);
1247*5b2fc499SJeff Garzik out:
1248*5b2fc499SJeff Garzik 	usb_put_dev(xdev);
1249*5b2fc499SJeff Garzik 	return status;
1250*5b2fc499SJeff Garzik }
1251*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
1252*5b2fc499SJeff Garzik 
1253*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
1254*5b2fc499SJeff Garzik 
1255*5b2fc499SJeff Garzik /* FIXME these suspend/resume methods assume non-CDC style
1256*5b2fc499SJeff Garzik  * devices, with only one interface.
1257*5b2fc499SJeff Garzik  */
1258*5b2fc499SJeff Garzik 
1259*5b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1260*5b2fc499SJeff Garzik {
1261*5b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
1262*5b2fc499SJeff Garzik 
1263*5b2fc499SJeff Garzik 	/* accelerate emptying of the rx and queues, to avoid
1264*5b2fc499SJeff Garzik 	 * having everything error out.
1265*5b2fc499SJeff Garzik 	 */
1266*5b2fc499SJeff Garzik 	netif_device_detach (dev->net);
1267*5b2fc499SJeff Garzik 	(void) unlink_urbs (dev, &dev->rxq);
1268*5b2fc499SJeff Garzik 	(void) unlink_urbs (dev, &dev->txq);
1269*5b2fc499SJeff Garzik 	return 0;
1270*5b2fc499SJeff Garzik }
1271*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
1272*5b2fc499SJeff Garzik 
1273*5b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
1274*5b2fc499SJeff Garzik {
1275*5b2fc499SJeff Garzik 	struct usbnet		*dev = usb_get_intfdata(intf);
1276*5b2fc499SJeff Garzik 
1277*5b2fc499SJeff Garzik 	netif_device_attach (dev->net);
1278*5b2fc499SJeff Garzik 	tasklet_schedule (&dev->bh);
1279*5b2fc499SJeff Garzik 	return 0;
1280*5b2fc499SJeff Garzik }
1281*5b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
1282*5b2fc499SJeff Garzik 
1283*5b2fc499SJeff Garzik 
1284*5b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
1285*5b2fc499SJeff Garzik 
1286*5b2fc499SJeff Garzik static int __init usbnet_init(void)
1287*5b2fc499SJeff Garzik {
1288*5b2fc499SJeff Garzik 	/* compiler should optimize this out */
1289*5b2fc499SJeff Garzik 	BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1290*5b2fc499SJeff Garzik 			< sizeof (struct skb_data));
1291*5b2fc499SJeff Garzik 
1292*5b2fc499SJeff Garzik 	random_ether_addr(node_id);
1293*5b2fc499SJeff Garzik 	return 0;
1294*5b2fc499SJeff Garzik }
1295*5b2fc499SJeff Garzik module_init(usbnet_init);
1296*5b2fc499SJeff Garzik 
1297*5b2fc499SJeff Garzik static void __exit usbnet_exit(void)
1298*5b2fc499SJeff Garzik {
1299*5b2fc499SJeff Garzik }
1300*5b2fc499SJeff Garzik module_exit(usbnet_exit);
1301*5b2fc499SJeff Garzik 
1302*5b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
1303*5b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
1304*5b2fc499SJeff Garzik MODULE_LICENSE("GPL");
1305