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