xref: /openbmc/linux/drivers/net/usb/usbnet.c (revision fd589a8f)
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 		switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
358 		case -EPIPE:
359 			usbnet_defer_kevent (dev, EVENT_RX_HALT);
360 			break;
361 		case -ENOMEM:
362 			usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
363 			break;
364 		case -ENODEV:
365 			if (netif_msg_ifdown (dev))
366 				devdbg (dev, "device gone");
367 			netif_device_detach (dev->net);
368 			break;
369 		default:
370 			if (netif_msg_rx_err (dev))
371 				devdbg (dev, "rx submit, %d", retval);
372 			tasklet_schedule (&dev->bh);
373 			break;
374 		case 0:
375 			__skb_queue_tail (&dev->rxq, skb);
376 		}
377 	} else {
378 		if (netif_msg_ifdown (dev))
379 			devdbg (dev, "rx: stopped");
380 		retval = -ENOLINK;
381 	}
382 	spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
383 	if (retval) {
384 		dev_kfree_skb_any (skb);
385 		usb_free_urb (urb);
386 	}
387 }
388 
389 
390 /*-------------------------------------------------------------------------*/
391 
392 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
393 {
394 	if (dev->driver_info->rx_fixup
395 			&& !dev->driver_info->rx_fixup (dev, skb))
396 		goto error;
397 	// else network stack removes extra byte if we forced a short packet
398 
399 	if (skb->len)
400 		usbnet_skb_return (dev, skb);
401 	else {
402 		if (netif_msg_rx_err (dev))
403 			devdbg (dev, "drop");
404 error:
405 		dev->net->stats.rx_errors++;
406 		skb_queue_tail (&dev->done, skb);
407 	}
408 }
409 
410 /*-------------------------------------------------------------------------*/
411 
412 static void rx_complete (struct urb *urb)
413 {
414 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
415 	struct skb_data		*entry = (struct skb_data *) skb->cb;
416 	struct usbnet		*dev = entry->dev;
417 	int			urb_status = urb->status;
418 
419 	skb_put (skb, urb->actual_length);
420 	entry->state = rx_done;
421 	entry->urb = NULL;
422 
423 	switch (urb_status) {
424 	/* success */
425 	case 0:
426 		if (skb->len < dev->net->hard_header_len) {
427 			entry->state = rx_cleanup;
428 			dev->net->stats.rx_errors++;
429 			dev->net->stats.rx_length_errors++;
430 			if (netif_msg_rx_err (dev))
431 				devdbg (dev, "rx length %d", skb->len);
432 		}
433 		break;
434 
435 	/* stalls need manual reset. this is rare ... except that
436 	 * when going through USB 2.0 TTs, unplug appears this way.
437 	 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
438 	 * storm, recovering as needed.
439 	 */
440 	case -EPIPE:
441 		dev->net->stats.rx_errors++;
442 		usbnet_defer_kevent (dev, EVENT_RX_HALT);
443 		// FALLTHROUGH
444 
445 	/* software-driven interface shutdown */
446 	case -ECONNRESET:		/* async unlink */
447 	case -ESHUTDOWN:		/* hardware gone */
448 		if (netif_msg_ifdown (dev))
449 			devdbg (dev, "rx shutdown, code %d", urb_status);
450 		goto block;
451 
452 	/* we get controller i/o faults during khubd disconnect() delays.
453 	 * throttle down resubmits, to avoid log floods; just temporarily,
454 	 * so we still recover when the fault isn't a khubd delay.
455 	 */
456 	case -EPROTO:
457 	case -ETIME:
458 	case -EILSEQ:
459 		dev->net->stats.rx_errors++;
460 		if (!timer_pending (&dev->delay)) {
461 			mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
462 			if (netif_msg_link (dev))
463 				devdbg (dev, "rx throttle %d", urb_status);
464 		}
465 block:
466 		entry->state = rx_cleanup;
467 		entry->urb = urb;
468 		urb = NULL;
469 		break;
470 
471 	/* data overrun ... flush fifo? */
472 	case -EOVERFLOW:
473 		dev->net->stats.rx_over_errors++;
474 		// FALLTHROUGH
475 
476 	default:
477 		entry->state = rx_cleanup;
478 		dev->net->stats.rx_errors++;
479 		if (netif_msg_rx_err (dev))
480 			devdbg (dev, "rx status %d", urb_status);
481 		break;
482 	}
483 
484 	defer_bh(dev, skb, &dev->rxq);
485 
486 	if (urb) {
487 		if (netif_running (dev->net)
488 				&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
489 			rx_submit (dev, urb, GFP_ATOMIC);
490 			return;
491 		}
492 		usb_free_urb (urb);
493 	}
494 	if (netif_msg_rx_err (dev))
495 		devdbg (dev, "no read resubmitted");
496 }
497 
498 static void intr_complete (struct urb *urb)
499 {
500 	struct usbnet	*dev = urb->context;
501 	int		status = urb->status;
502 
503 	switch (status) {
504 	/* success */
505 	case 0:
506 		dev->driver_info->status(dev, urb);
507 		break;
508 
509 	/* software-driven interface shutdown */
510 	case -ENOENT:		/* urb killed */
511 	case -ESHUTDOWN:	/* hardware gone */
512 		if (netif_msg_ifdown (dev))
513 			devdbg (dev, "intr shutdown, code %d", status);
514 		return;
515 
516 	/* NOTE:  not throttling like RX/TX, since this endpoint
517 	 * already polls infrequently
518 	 */
519 	default:
520 		devdbg (dev, "intr status %d", status);
521 		break;
522 	}
523 
524 	if (!netif_running (dev->net))
525 		return;
526 
527 	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
528 	status = usb_submit_urb (urb, GFP_ATOMIC);
529 	if (status != 0 && netif_msg_timer (dev))
530 		deverr(dev, "intr resubmit --> %d", status);
531 }
532 
533 /*-------------------------------------------------------------------------*/
534 void usbnet_pause_rx(struct usbnet *dev)
535 {
536 	set_bit(EVENT_RX_PAUSED, &dev->flags);
537 
538 	if (netif_msg_rx_status(dev))
539 		devdbg(dev, "paused rx queue enabled");
540 }
541 EXPORT_SYMBOL_GPL(usbnet_pause_rx);
542 
543 void usbnet_resume_rx(struct usbnet *dev)
544 {
545 	struct sk_buff *skb;
546 	int num = 0;
547 
548 	clear_bit(EVENT_RX_PAUSED, &dev->flags);
549 
550 	while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
551 		usbnet_skb_return(dev, skb);
552 		num++;
553 	}
554 
555 	tasklet_schedule(&dev->bh);
556 
557 	if (netif_msg_rx_status(dev))
558 		devdbg(dev, "paused rx queue disabled, %d skbs requeued", num);
559 }
560 EXPORT_SYMBOL_GPL(usbnet_resume_rx);
561 
562 void usbnet_purge_paused_rxq(struct usbnet *dev)
563 {
564 	skb_queue_purge(&dev->rxq_pause);
565 }
566 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
567 
568 /*-------------------------------------------------------------------------*/
569 
570 // unlink pending rx/tx; completion handlers do all other cleanup
571 
572 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
573 {
574 	unsigned long		flags;
575 	struct sk_buff		*skb, *skbnext;
576 	int			count = 0;
577 
578 	spin_lock_irqsave (&q->lock, flags);
579 	skb_queue_walk_safe(q, skb, skbnext) {
580 		struct skb_data		*entry;
581 		struct urb		*urb;
582 		int			retval;
583 
584 		entry = (struct skb_data *) skb->cb;
585 		urb = entry->urb;
586 
587 		// during some PM-driven resume scenarios,
588 		// these (async) unlinks complete immediately
589 		retval = usb_unlink_urb (urb);
590 		if (retval != -EINPROGRESS && retval != 0)
591 			devdbg (dev, "unlink urb err, %d", retval);
592 		else
593 			count++;
594 	}
595 	spin_unlock_irqrestore (&q->lock, flags);
596 	return count;
597 }
598 
599 // Flush all pending rx urbs
600 // minidrivers may need to do this when the MTU changes
601 
602 void usbnet_unlink_rx_urbs(struct usbnet *dev)
603 {
604 	if (netif_running(dev->net)) {
605 		(void) unlink_urbs (dev, &dev->rxq);
606 		tasklet_schedule(&dev->bh);
607 	}
608 }
609 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
610 
611 /*-------------------------------------------------------------------------*/
612 
613 // precondition: never called in_interrupt
614 
615 int usbnet_stop (struct net_device *net)
616 {
617 	struct usbnet		*dev = netdev_priv(net);
618 	struct driver_info	*info = dev->driver_info;
619 	int			temp;
620 	int			retval;
621 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
622 	DECLARE_WAITQUEUE (wait, current);
623 
624 	netif_stop_queue (net);
625 
626 	if (netif_msg_ifdown (dev))
627 		devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
628 			net->stats.rx_packets, net->stats.tx_packets,
629 			net->stats.rx_errors, net->stats.tx_errors
630 			);
631 
632 	/* allow minidriver to stop correctly (wireless devices to turn off
633 	 * radio etc) */
634 	if (info->stop) {
635 		retval = info->stop(dev);
636 		if (retval < 0 && netif_msg_ifdown(dev))
637 			devinfo(dev,
638 				"stop fail (%d) usbnet usb-%s-%s, %s",
639 				retval,
640 				dev->udev->bus->bus_name, dev->udev->devpath,
641 				info->description);
642 	}
643 
644 	if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) {
645 		/* ensure there are no more active urbs */
646 		add_wait_queue(&unlink_wakeup, &wait);
647 		dev->wait = &unlink_wakeup;
648 		temp = unlink_urbs(dev, &dev->txq) +
649 			unlink_urbs(dev, &dev->rxq);
650 
651 		/* maybe wait for deletions to finish. */
652 		while (!skb_queue_empty(&dev->rxq)
653 				&& !skb_queue_empty(&dev->txq)
654 				&& !skb_queue_empty(&dev->done)) {
655 			msleep(UNLINK_TIMEOUT_MS);
656 			if (netif_msg_ifdown(dev))
657 				devdbg(dev, "waited for %d urb completions",
658 					temp);
659 		}
660 		dev->wait = NULL;
661 		remove_wait_queue(&unlink_wakeup, &wait);
662 	}
663 
664 	usb_kill_urb(dev->interrupt);
665 
666 	usbnet_purge_paused_rxq(dev);
667 
668 	/* deferred work (task, timer, softirq) must also stop.
669 	 * can't flush_scheduled_work() until we drop rtnl (later),
670 	 * else workers could deadlock; so make workers a NOP.
671 	 */
672 	dev->flags = 0;
673 	del_timer_sync (&dev->delay);
674 	tasklet_kill (&dev->bh);
675 	usb_autopm_put_interface(dev->intf);
676 
677 	return 0;
678 }
679 EXPORT_SYMBOL_GPL(usbnet_stop);
680 
681 /*-------------------------------------------------------------------------*/
682 
683 // posts reads, and enables write queuing
684 
685 // precondition: never called in_interrupt
686 
687 int usbnet_open (struct net_device *net)
688 {
689 	struct usbnet		*dev = netdev_priv(net);
690 	int			retval;
691 	struct driver_info	*info = dev->driver_info;
692 
693 	if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
694 		if (netif_msg_ifup (dev))
695 			devinfo (dev,
696 				"resumption fail (%d) usbnet usb-%s-%s, %s",
697 				retval,
698 				dev->udev->bus->bus_name, dev->udev->devpath,
699 			info->description);
700 		goto done_nopm;
701 	}
702 
703 	// put into "known safe" state
704 	if (info->reset && (retval = info->reset (dev)) < 0) {
705 		if (netif_msg_ifup (dev))
706 			devinfo (dev,
707 				"open reset fail (%d) usbnet usb-%s-%s, %s",
708 				retval,
709 				dev->udev->bus->bus_name, dev->udev->devpath,
710 			info->description);
711 		goto done;
712 	}
713 
714 	// insist peer be connected
715 	if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
716 		if (netif_msg_ifup (dev))
717 			devdbg (dev, "can't open; %d", retval);
718 		goto done;
719 	}
720 
721 	/* start any status interrupt transfer */
722 	if (dev->interrupt) {
723 		retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
724 		if (retval < 0) {
725 			if (netif_msg_ifup (dev))
726 				deverr (dev, "intr submit %d", retval);
727 			goto done;
728 		}
729 	}
730 
731 	netif_start_queue (net);
732 	if (netif_msg_ifup (dev)) {
733 		char	*framing;
734 
735 		if (dev->driver_info->flags & FLAG_FRAMING_NC)
736 			framing = "NetChip";
737 		else if (dev->driver_info->flags & FLAG_FRAMING_GL)
738 			framing = "GeneSys";
739 		else if (dev->driver_info->flags & FLAG_FRAMING_Z)
740 			framing = "Zaurus";
741 		else if (dev->driver_info->flags & FLAG_FRAMING_RN)
742 			framing = "RNDIS";
743 		else if (dev->driver_info->flags & FLAG_FRAMING_AX)
744 			framing = "ASIX";
745 		else
746 			framing = "simple";
747 
748 		devinfo (dev, "open: enable queueing "
749 				"(rx %d, tx %d) mtu %d %s framing",
750 			(int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
751 			framing);
752 	}
753 
754 	// delay posting reads until we're fully open
755 	tasklet_schedule (&dev->bh);
756 	return retval;
757 done:
758 	usb_autopm_put_interface(dev->intf);
759 done_nopm:
760 	return retval;
761 }
762 EXPORT_SYMBOL_GPL(usbnet_open);
763 
764 /*-------------------------------------------------------------------------*/
765 
766 /* ethtool methods; minidrivers may need to add some more, but
767  * they'll probably want to use this base set.
768  */
769 
770 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
771 {
772 	struct usbnet *dev = netdev_priv(net);
773 
774 	if (!dev->mii.mdio_read)
775 		return -EOPNOTSUPP;
776 
777 	return mii_ethtool_gset(&dev->mii, cmd);
778 }
779 EXPORT_SYMBOL_GPL(usbnet_get_settings);
780 
781 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
782 {
783 	struct usbnet *dev = netdev_priv(net);
784 	int retval;
785 
786 	if (!dev->mii.mdio_write)
787 		return -EOPNOTSUPP;
788 
789 	retval = mii_ethtool_sset(&dev->mii, cmd);
790 
791 	/* link speed/duplex might have changed */
792 	if (dev->driver_info->link_reset)
793 		dev->driver_info->link_reset(dev);
794 
795 	return retval;
796 
797 }
798 EXPORT_SYMBOL_GPL(usbnet_set_settings);
799 
800 u32 usbnet_get_link (struct net_device *net)
801 {
802 	struct usbnet *dev = netdev_priv(net);
803 
804 	/* If a check_connect is defined, return its result */
805 	if (dev->driver_info->check_connect)
806 		return dev->driver_info->check_connect (dev) == 0;
807 
808 	/* if the device has mii operations, use those */
809 	if (dev->mii.mdio_read)
810 		return mii_link_ok(&dev->mii);
811 
812 	/* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
813 	return ethtool_op_get_link(net);
814 }
815 EXPORT_SYMBOL_GPL(usbnet_get_link);
816 
817 int usbnet_nway_reset(struct net_device *net)
818 {
819 	struct usbnet *dev = netdev_priv(net);
820 
821 	if (!dev->mii.mdio_write)
822 		return -EOPNOTSUPP;
823 
824 	return mii_nway_restart(&dev->mii);
825 }
826 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
827 
828 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
829 {
830 	struct usbnet *dev = netdev_priv(net);
831 
832 	strncpy (info->driver, dev->driver_name, sizeof info->driver);
833 	strncpy (info->version, DRIVER_VERSION, sizeof info->version);
834 	strncpy (info->fw_version, dev->driver_info->description,
835 		sizeof info->fw_version);
836 	usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
837 }
838 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
839 
840 u32 usbnet_get_msglevel (struct net_device *net)
841 {
842 	struct usbnet *dev = netdev_priv(net);
843 
844 	return dev->msg_enable;
845 }
846 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
847 
848 void usbnet_set_msglevel (struct net_device *net, u32 level)
849 {
850 	struct usbnet *dev = netdev_priv(net);
851 
852 	dev->msg_enable = level;
853 }
854 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
855 
856 /* drivers may override default ethtool_ops in their bind() routine */
857 static const struct ethtool_ops usbnet_ethtool_ops = {
858 	.get_settings		= usbnet_get_settings,
859 	.set_settings		= usbnet_set_settings,
860 	.get_link		= usbnet_get_link,
861 	.nway_reset		= usbnet_nway_reset,
862 	.get_drvinfo		= usbnet_get_drvinfo,
863 	.get_msglevel		= usbnet_get_msglevel,
864 	.set_msglevel		= usbnet_set_msglevel,
865 };
866 
867 /*-------------------------------------------------------------------------*/
868 
869 /* work that cannot be done in interrupt context uses keventd.
870  *
871  * NOTE:  with 2.5 we could do more of this using completion callbacks,
872  * especially now that control transfers can be queued.
873  */
874 static void
875 kevent (struct work_struct *work)
876 {
877 	struct usbnet		*dev =
878 		container_of(work, struct usbnet, kevent);
879 	int			status;
880 
881 	/* usb_clear_halt() needs a thread context */
882 	if (test_bit (EVENT_TX_HALT, &dev->flags)) {
883 		unlink_urbs (dev, &dev->txq);
884 		status = usb_clear_halt (dev->udev, dev->out);
885 		if (status < 0
886 				&& status != -EPIPE
887 				&& status != -ESHUTDOWN) {
888 			if (netif_msg_tx_err (dev))
889 				deverr (dev, "can't clear tx halt, status %d",
890 					status);
891 		} else {
892 			clear_bit (EVENT_TX_HALT, &dev->flags);
893 			if (status != -ESHUTDOWN)
894 				netif_wake_queue (dev->net);
895 		}
896 	}
897 	if (test_bit (EVENT_RX_HALT, &dev->flags)) {
898 		unlink_urbs (dev, &dev->rxq);
899 		status = usb_clear_halt (dev->udev, dev->in);
900 		if (status < 0
901 				&& status != -EPIPE
902 				&& status != -ESHUTDOWN) {
903 			if (netif_msg_rx_err (dev))
904 				deverr (dev, "can't clear rx halt, status %d",
905 					status);
906 		} else {
907 			clear_bit (EVENT_RX_HALT, &dev->flags);
908 			tasklet_schedule (&dev->bh);
909 		}
910 	}
911 
912 	/* tasklet could resubmit itself forever if memory is tight */
913 	if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
914 		struct urb	*urb = NULL;
915 
916 		if (netif_running (dev->net))
917 			urb = usb_alloc_urb (0, GFP_KERNEL);
918 		else
919 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
920 		if (urb != NULL) {
921 			clear_bit (EVENT_RX_MEMORY, &dev->flags);
922 			rx_submit (dev, urb, GFP_KERNEL);
923 			tasklet_schedule (&dev->bh);
924 		}
925 	}
926 
927 	if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
928 		struct driver_info	*info = dev->driver_info;
929 		int			retval = 0;
930 
931 		clear_bit (EVENT_LINK_RESET, &dev->flags);
932 		if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
933 			devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
934 				retval,
935 				dev->udev->bus->bus_name, dev->udev->devpath,
936 				info->description);
937 		}
938 	}
939 
940 	if (dev->flags)
941 		devdbg (dev, "kevent done, flags = 0x%lx",
942 			dev->flags);
943 }
944 
945 /*-------------------------------------------------------------------------*/
946 
947 static void tx_complete (struct urb *urb)
948 {
949 	struct sk_buff		*skb = (struct sk_buff *) urb->context;
950 	struct skb_data		*entry = (struct skb_data *) skb->cb;
951 	struct usbnet		*dev = entry->dev;
952 
953 	if (urb->status == 0) {
954 		dev->net->stats.tx_packets++;
955 		dev->net->stats.tx_bytes += entry->length;
956 	} else {
957 		dev->net->stats.tx_errors++;
958 
959 		switch (urb->status) {
960 		case -EPIPE:
961 			usbnet_defer_kevent (dev, EVENT_TX_HALT);
962 			break;
963 
964 		/* software-driven interface shutdown */
965 		case -ECONNRESET:		// async unlink
966 		case -ESHUTDOWN:		// hardware gone
967 			break;
968 
969 		// like rx, tx gets controller i/o faults during khubd delays
970 		// and so it uses the same throttling mechanism.
971 		case -EPROTO:
972 		case -ETIME:
973 		case -EILSEQ:
974 			if (!timer_pending (&dev->delay)) {
975 				mod_timer (&dev->delay,
976 					jiffies + THROTTLE_JIFFIES);
977 				if (netif_msg_link (dev))
978 					devdbg (dev, "tx throttle %d",
979 							urb->status);
980 			}
981 			netif_stop_queue (dev->net);
982 			break;
983 		default:
984 			if (netif_msg_tx_err (dev))
985 				devdbg (dev, "tx err %d", entry->urb->status);
986 			break;
987 		}
988 	}
989 
990 	urb->dev = NULL;
991 	entry->state = tx_done;
992 	defer_bh(dev, skb, &dev->txq);
993 }
994 
995 /*-------------------------------------------------------------------------*/
996 
997 void usbnet_tx_timeout (struct net_device *net)
998 {
999 	struct usbnet		*dev = netdev_priv(net);
1000 
1001 	unlink_urbs (dev, &dev->txq);
1002 	tasklet_schedule (&dev->bh);
1003 
1004 	// FIXME: device recovery -- reset?
1005 }
1006 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1007 
1008 /*-------------------------------------------------------------------------*/
1009 
1010 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1011 				     struct net_device *net)
1012 {
1013 	struct usbnet		*dev = netdev_priv(net);
1014 	int			length;
1015 	struct urb		*urb = NULL;
1016 	struct skb_data		*entry;
1017 	struct driver_info	*info = dev->driver_info;
1018 	unsigned long		flags;
1019 	int retval;
1020 
1021 	// some devices want funky USB-level framing, for
1022 	// win32 driver (usually) and/or hardware quirks
1023 	if (info->tx_fixup) {
1024 		skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1025 		if (!skb) {
1026 			if (netif_msg_tx_err (dev))
1027 				devdbg (dev, "can't tx_fixup skb");
1028 			goto drop;
1029 		}
1030 	}
1031 	length = skb->len;
1032 
1033 	if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1034 		if (netif_msg_tx_err (dev))
1035 			devdbg (dev, "no urb");
1036 		goto drop;
1037 	}
1038 
1039 	entry = (struct skb_data *) skb->cb;
1040 	entry->urb = urb;
1041 	entry->dev = dev;
1042 	entry->state = tx_start;
1043 	entry->length = length;
1044 
1045 	usb_fill_bulk_urb (urb, dev->udev, dev->out,
1046 			skb->data, skb->len, tx_complete, skb);
1047 
1048 	/* don't assume the hardware handles USB_ZERO_PACKET
1049 	 * NOTE:  strictly conforming cdc-ether devices should expect
1050 	 * the ZLP here, but ignore the one-byte packet.
1051 	 */
1052 	if ((length % dev->maxpacket) == 0) {
1053 		urb->transfer_buffer_length++;
1054 		if (skb_tailroom(skb)) {
1055 			skb->data[skb->len] = 0;
1056 			__skb_put(skb, 1);
1057 		}
1058 	}
1059 
1060 	spin_lock_irqsave (&dev->txq.lock, flags);
1061 
1062 	switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1063 	case -EPIPE:
1064 		netif_stop_queue (net);
1065 		usbnet_defer_kevent (dev, EVENT_TX_HALT);
1066 		break;
1067 	default:
1068 		if (netif_msg_tx_err (dev))
1069 			devdbg (dev, "tx: submit urb err %d", retval);
1070 		break;
1071 	case 0:
1072 		net->trans_start = jiffies;
1073 		__skb_queue_tail (&dev->txq, skb);
1074 		if (dev->txq.qlen >= TX_QLEN (dev))
1075 			netif_stop_queue (net);
1076 	}
1077 	spin_unlock_irqrestore (&dev->txq.lock, flags);
1078 
1079 	if (retval) {
1080 		if (netif_msg_tx_err (dev))
1081 			devdbg (dev, "drop, code %d", retval);
1082 drop:
1083 		dev->net->stats.tx_dropped++;
1084 		if (skb)
1085 			dev_kfree_skb_any (skb);
1086 		usb_free_urb (urb);
1087 	} else if (netif_msg_tx_queued (dev)) {
1088 		devdbg (dev, "> tx, len %d, type 0x%x",
1089 			length, skb->protocol);
1090 	}
1091 	return NETDEV_TX_OK;
1092 }
1093 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1094 
1095 /*-------------------------------------------------------------------------*/
1096 
1097 // tasklet (work deferred from completions, in_irq) or timer
1098 
1099 static void usbnet_bh (unsigned long param)
1100 {
1101 	struct usbnet		*dev = (struct usbnet *) param;
1102 	struct sk_buff		*skb;
1103 	struct skb_data		*entry;
1104 
1105 	while ((skb = skb_dequeue (&dev->done))) {
1106 		entry = (struct skb_data *) skb->cb;
1107 		switch (entry->state) {
1108 		case rx_done:
1109 			entry->state = rx_cleanup;
1110 			rx_process (dev, skb);
1111 			continue;
1112 		case tx_done:
1113 		case rx_cleanup:
1114 			usb_free_urb (entry->urb);
1115 			dev_kfree_skb (skb);
1116 			continue;
1117 		default:
1118 			devdbg (dev, "bogus skb state %d", entry->state);
1119 		}
1120 	}
1121 
1122 	// waiting for all pending urbs to complete?
1123 	if (dev->wait) {
1124 		if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1125 			wake_up (dev->wait);
1126 		}
1127 
1128 	// or are we maybe short a few urbs?
1129 	} else if (netif_running (dev->net)
1130 			&& netif_device_present (dev->net)
1131 			&& !timer_pending (&dev->delay)
1132 			&& !test_bit (EVENT_RX_HALT, &dev->flags)) {
1133 		int	temp = dev->rxq.qlen;
1134 		int	qlen = RX_QLEN (dev);
1135 
1136 		if (temp < qlen) {
1137 			struct urb	*urb;
1138 			int		i;
1139 
1140 			// don't refill the queue all at once
1141 			for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1142 				urb = usb_alloc_urb (0, GFP_ATOMIC);
1143 				if (urb != NULL)
1144 					rx_submit (dev, urb, GFP_ATOMIC);
1145 			}
1146 			if (temp != dev->rxq.qlen && netif_msg_link (dev))
1147 				devdbg (dev, "rxqlen %d --> %d",
1148 						temp, dev->rxq.qlen);
1149 			if (dev->rxq.qlen < qlen)
1150 				tasklet_schedule (&dev->bh);
1151 		}
1152 		if (dev->txq.qlen < TX_QLEN (dev))
1153 			netif_wake_queue (dev->net);
1154 	}
1155 }
1156 
1157 
1158 /*-------------------------------------------------------------------------
1159  *
1160  * USB Device Driver support
1161  *
1162  *-------------------------------------------------------------------------*/
1163 
1164 // precondition: never called in_interrupt
1165 
1166 void usbnet_disconnect (struct usb_interface *intf)
1167 {
1168 	struct usbnet		*dev;
1169 	struct usb_device	*xdev;
1170 	struct net_device	*net;
1171 
1172 	dev = usb_get_intfdata(intf);
1173 	usb_set_intfdata(intf, NULL);
1174 	if (!dev)
1175 		return;
1176 
1177 	xdev = interface_to_usbdev (intf);
1178 
1179 	if (netif_msg_probe (dev))
1180 		devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1181 			intf->dev.driver->name,
1182 			xdev->bus->bus_name, xdev->devpath,
1183 			dev->driver_info->description);
1184 
1185 	net = dev->net;
1186 	unregister_netdev (net);
1187 
1188 	/* we don't hold rtnl here ... */
1189 	flush_scheduled_work ();
1190 
1191 	if (dev->driver_info->unbind)
1192 		dev->driver_info->unbind (dev, intf);
1193 
1194 	free_netdev(net);
1195 	usb_put_dev (xdev);
1196 }
1197 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1198 
1199 static const struct net_device_ops usbnet_netdev_ops = {
1200 	.ndo_open		= usbnet_open,
1201 	.ndo_stop		= usbnet_stop,
1202 	.ndo_start_xmit		= usbnet_start_xmit,
1203 	.ndo_tx_timeout		= usbnet_tx_timeout,
1204 	.ndo_change_mtu		= usbnet_change_mtu,
1205 	.ndo_set_mac_address 	= eth_mac_addr,
1206 	.ndo_validate_addr	= eth_validate_addr,
1207 };
1208 
1209 /*-------------------------------------------------------------------------*/
1210 
1211 // precondition: never called in_interrupt
1212 
1213 int
1214 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1215 {
1216 	struct usbnet			*dev;
1217 	struct net_device		*net;
1218 	struct usb_host_interface	*interface;
1219 	struct driver_info		*info;
1220 	struct usb_device		*xdev;
1221 	int				status;
1222 	const char			*name;
1223 
1224 	name = udev->dev.driver->name;
1225 	info = (struct driver_info *) prod->driver_info;
1226 	if (!info) {
1227 		dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1228 		return -ENODEV;
1229 	}
1230 	xdev = interface_to_usbdev (udev);
1231 	interface = udev->cur_altsetting;
1232 
1233 	usb_get_dev (xdev);
1234 
1235 	status = -ENOMEM;
1236 
1237 	// set up our own records
1238 	net = alloc_etherdev(sizeof(*dev));
1239 	if (!net) {
1240 		dbg ("can't kmalloc dev");
1241 		goto out;
1242 	}
1243 
1244 	dev = netdev_priv(net);
1245 	dev->udev = xdev;
1246 	dev->intf = udev;
1247 	dev->driver_info = info;
1248 	dev->driver_name = name;
1249 	dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1250 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
1251 	skb_queue_head_init (&dev->rxq);
1252 	skb_queue_head_init (&dev->txq);
1253 	skb_queue_head_init (&dev->done);
1254 	skb_queue_head_init(&dev->rxq_pause);
1255 	dev->bh.func = usbnet_bh;
1256 	dev->bh.data = (unsigned long) dev;
1257 	INIT_WORK (&dev->kevent, kevent);
1258 	dev->delay.function = usbnet_bh;
1259 	dev->delay.data = (unsigned long) dev;
1260 	init_timer (&dev->delay);
1261 	mutex_init (&dev->phy_mutex);
1262 
1263 	dev->net = net;
1264 	strcpy (net->name, "usb%d");
1265 	memcpy (net->dev_addr, node_id, sizeof node_id);
1266 
1267 	/* rx and tx sides can use different message sizes;
1268 	 * bind() should set rx_urb_size in that case.
1269 	 */
1270 	dev->hard_mtu = net->mtu + net->hard_header_len;
1271 #if 0
1272 // dma_supported() is deeply broken on almost all architectures
1273 	// possible with some EHCI controllers
1274 	if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
1275 		net->features |= NETIF_F_HIGHDMA;
1276 #endif
1277 
1278 	net->netdev_ops = &usbnet_netdev_ops;
1279 	net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1280 	net->ethtool_ops = &usbnet_ethtool_ops;
1281 
1282 	// allow device-specific bind/init procedures
1283 	// NOTE net->name still not usable ...
1284 	if (info->bind) {
1285 		status = info->bind (dev, udev);
1286 		if (status < 0)
1287 			goto out1;
1288 
1289 		// heuristic:  "usb%d" for links we know are two-host,
1290 		// else "eth%d" when there's reasonable doubt.  userspace
1291 		// can rename the link if it knows better.
1292 		if ((dev->driver_info->flags & FLAG_ETHER) != 0
1293 				&& (net->dev_addr [0] & 0x02) == 0)
1294 			strcpy (net->name, "eth%d");
1295 		/* WLAN devices should always be named "wlan%d" */
1296 		if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1297 			strcpy(net->name, "wlan%d");
1298 
1299 		/* maybe the remote can't receive an Ethernet MTU */
1300 		if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1301 			net->mtu = dev->hard_mtu - net->hard_header_len;
1302 	} else if (!info->in || !info->out)
1303 		status = usbnet_get_endpoints (dev, udev);
1304 	else {
1305 		dev->in = usb_rcvbulkpipe (xdev, info->in);
1306 		dev->out = usb_sndbulkpipe (xdev, info->out);
1307 		if (!(info->flags & FLAG_NO_SETINT))
1308 			status = usb_set_interface (xdev,
1309 				interface->desc.bInterfaceNumber,
1310 				interface->desc.bAlternateSetting);
1311 		else
1312 			status = 0;
1313 
1314 	}
1315 	if (status >= 0 && dev->status)
1316 		status = init_status (dev, udev);
1317 	if (status < 0)
1318 		goto out3;
1319 
1320 	if (!dev->rx_urb_size)
1321 		dev->rx_urb_size = dev->hard_mtu;
1322 	dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1323 
1324 	SET_NETDEV_DEV(net, &udev->dev);
1325 	status = register_netdev (net);
1326 	if (status)
1327 		goto out3;
1328 	if (netif_msg_probe (dev))
1329 		devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
1330 			udev->dev.driver->name,
1331 			xdev->bus->bus_name, xdev->devpath,
1332 			dev->driver_info->description,
1333 			net->dev_addr);
1334 
1335 	// ok, it's ready to go.
1336 	usb_set_intfdata (udev, dev);
1337 
1338 	// start as if the link is up
1339 	netif_device_attach (net);
1340 
1341 	return 0;
1342 
1343 out3:
1344 	if (info->unbind)
1345 		info->unbind (dev, udev);
1346 out1:
1347 	free_netdev(net);
1348 out:
1349 	usb_put_dev(xdev);
1350 	return status;
1351 }
1352 EXPORT_SYMBOL_GPL(usbnet_probe);
1353 
1354 /*-------------------------------------------------------------------------*/
1355 
1356 /*
1357  * suspend the whole driver as soon as the first interface is suspended
1358  * resume only when the last interface is resumed
1359  */
1360 
1361 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1362 {
1363 	struct usbnet		*dev = usb_get_intfdata(intf);
1364 
1365 	if (!dev->suspend_count++) {
1366 		/*
1367 		 * accelerate emptying of the rx and queues, to avoid
1368 		 * having everything error out.
1369 		 */
1370 		netif_device_detach (dev->net);
1371 		(void) unlink_urbs (dev, &dev->rxq);
1372 		(void) unlink_urbs (dev, &dev->txq);
1373 		/*
1374 		 * reattach so runtime management can use and
1375 		 * wake the device
1376 		 */
1377 		netif_device_attach (dev->net);
1378 	}
1379 	return 0;
1380 }
1381 EXPORT_SYMBOL_GPL(usbnet_suspend);
1382 
1383 int usbnet_resume (struct usb_interface *intf)
1384 {
1385 	struct usbnet		*dev = usb_get_intfdata(intf);
1386 
1387 	if (!--dev->suspend_count)
1388 		tasklet_schedule (&dev->bh);
1389 
1390 	return 0;
1391 }
1392 EXPORT_SYMBOL_GPL(usbnet_resume);
1393 
1394 
1395 /*-------------------------------------------------------------------------*/
1396 
1397 static int __init usbnet_init(void)
1398 {
1399 	/* compiler should optimize this out */
1400 	BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1401 			< sizeof (struct skb_data));
1402 
1403 	random_ether_addr(node_id);
1404 	return 0;
1405 }
1406 module_init(usbnet_init);
1407 
1408 static void __exit usbnet_exit(void)
1409 {
1410 }
1411 module_exit(usbnet_exit);
1412 
1413 MODULE_AUTHOR("David Brownell");
1414 MODULE_DESCRIPTION("USB network driver framework");
1415 MODULE_LICENSE("GPL");
1416