drm/i915: Move non-phys cursors into the GTT
[linux/fpc-iii.git] / drivers / net / usb / usbnet.c
bloba95c73de5824c67f7bd3ca471a4e8697c16ffd34
1 /*
2 * USB Network driver infrastructure
3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
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.
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.
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
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.
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.
33 // #define DEBUG // error path messages, extra info
34 // #define VERBOSE // more; success messages
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 #include <linux/slab.h>
48 #define DRIVER_VERSION "22-Aug-2005"
51 /*-------------------------------------------------------------------------*/
54 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
55 * Several dozen bytes of IPv4 data can fit in two such transactions.
56 * One maximum size Ethernet packet takes twenty four of them.
57 * For high speed, each frame comfortably fits almost 36 max size
58 * Ethernet packets (so queues should be bigger).
60 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
61 * let the USB host controller be busy for 5msec or more before an irq
62 * is required, under load. Jumbograms change the equation.
64 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
65 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
66 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
67 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
68 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
70 // reawaken network queue this soon after stopping; else watchdog barks
71 #define TX_TIMEOUT_JIFFIES (5*HZ)
73 // throttle rx/tx briefly after some faults, so khubd might disconnect()
74 // us (it polls at HZ/4 usually) before we report too many false errors.
75 #define THROTTLE_JIFFIES (HZ/8)
77 // between wakeups
78 #define UNLINK_TIMEOUT_MS 3
80 /*-------------------------------------------------------------------------*/
82 // randomly generated ethernet address
83 static u8 node_id [ETH_ALEN];
85 static const char driver_name [] = "usbnet";
87 /* use ethtool to change the level for any given device */
88 static int msg_level = -1;
89 module_param (msg_level, int, 0);
90 MODULE_PARM_DESC (msg_level, "Override default message level");
92 /*-------------------------------------------------------------------------*/
94 /* handles CDC Ethernet and many other network "bulk data" interfaces */
95 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
97 int tmp;
98 struct usb_host_interface *alt = NULL;
99 struct usb_host_endpoint *in = NULL, *out = NULL;
100 struct usb_host_endpoint *status = NULL;
102 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
103 unsigned ep;
105 in = out = status = NULL;
106 alt = intf->altsetting + tmp;
108 /* take the first altsetting with in-bulk + out-bulk;
109 * remember any status endpoint, just in case;
110 * ignore other endpoints and altsetttings.
112 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
113 struct usb_host_endpoint *e;
114 int intr = 0;
116 e = alt->endpoint + ep;
117 switch (e->desc.bmAttributes) {
118 case USB_ENDPOINT_XFER_INT:
119 if (!usb_endpoint_dir_in(&e->desc))
120 continue;
121 intr = 1;
122 /* FALLTHROUGH */
123 case USB_ENDPOINT_XFER_BULK:
124 break;
125 default:
126 continue;
128 if (usb_endpoint_dir_in(&e->desc)) {
129 if (!intr && !in)
130 in = e;
131 else if (intr && !status)
132 status = e;
133 } else {
134 if (!out)
135 out = e;
138 if (in && out)
139 break;
141 if (!alt || !in || !out)
142 return -EINVAL;
144 if (alt->desc.bAlternateSetting != 0 ||
145 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
146 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
147 alt->desc.bAlternateSetting);
148 if (tmp < 0)
149 return tmp;
152 dev->in = usb_rcvbulkpipe (dev->udev,
153 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
154 dev->out = usb_sndbulkpipe (dev->udev,
155 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
156 dev->status = status;
157 return 0;
159 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
161 static u8 nibble(unsigned char c)
163 if (likely(isdigit(c)))
164 return c - '0';
165 c = toupper(c);
166 if (likely(isxdigit(c)))
167 return 10 + c - 'A';
168 return 0;
171 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
173 int tmp, i;
174 unsigned char buf [13];
176 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
177 if (tmp != 12) {
178 dev_dbg(&dev->udev->dev,
179 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
180 if (tmp >= 0)
181 tmp = -EINVAL;
182 return tmp;
184 for (i = tmp = 0; i < 6; i++, tmp += 2)
185 dev->net->dev_addr [i] =
186 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
187 return 0;
189 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
191 static void intr_complete (struct urb *urb);
193 static int init_status (struct usbnet *dev, struct usb_interface *intf)
195 char *buf = NULL;
196 unsigned pipe = 0;
197 unsigned maxp;
198 unsigned period;
200 if (!dev->driver_info->status)
201 return 0;
203 pipe = usb_rcvintpipe (dev->udev,
204 dev->status->desc.bEndpointAddress
205 & USB_ENDPOINT_NUMBER_MASK);
206 maxp = usb_maxpacket (dev->udev, pipe, 0);
208 /* avoid 1 msec chatter: min 8 msec poll rate */
209 period = max ((int) dev->status->desc.bInterval,
210 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
212 buf = kmalloc (maxp, GFP_KERNEL);
213 if (buf) {
214 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
215 if (!dev->interrupt) {
216 kfree (buf);
217 return -ENOMEM;
218 } else {
219 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
220 buf, maxp, intr_complete, dev, period);
221 dev_dbg(&intf->dev,
222 "status ep%din, %d bytes period %d\n",
223 usb_pipeendpoint(pipe), maxp, period);
226 return 0;
229 /* Passes this packet up the stack, updating its accounting.
230 * Some link protocols batch packets, so their rx_fixup paths
231 * can return clones as well as just modify the original skb.
233 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
235 int status;
237 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
238 skb_queue_tail(&dev->rxq_pause, skb);
239 return;
242 skb->protocol = eth_type_trans (skb, dev->net);
243 dev->net->stats.rx_packets++;
244 dev->net->stats.rx_bytes += skb->len;
246 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
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)
251 netif_dbg(dev, rx_err, dev->net,
252 "netif_rx status %d\n", status);
254 EXPORT_SYMBOL_GPL(usbnet_skb_return);
257 /*-------------------------------------------------------------------------
259 * Network Device Driver (peer link to "Host Device", from USB host)
261 *-------------------------------------------------------------------------*/
263 int usbnet_change_mtu (struct net_device *net, int new_mtu)
265 struct usbnet *dev = netdev_priv(net);
266 int ll_mtu = new_mtu + net->hard_header_len;
267 int old_hard_mtu = dev->hard_mtu;
268 int old_rx_urb_size = dev->rx_urb_size;
270 if (new_mtu <= 0)
271 return -EINVAL;
272 // no second zero-length packet read wanted after mtu-sized packets
273 if ((ll_mtu % dev->maxpacket) == 0)
274 return -EDOM;
275 net->mtu = new_mtu;
277 dev->hard_mtu = net->mtu + net->hard_header_len;
278 if (dev->rx_urb_size == old_hard_mtu) {
279 dev->rx_urb_size = dev->hard_mtu;
280 if (dev->rx_urb_size > old_rx_urb_size)
281 usbnet_unlink_rx_urbs(dev);
284 return 0;
286 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
288 /*-------------------------------------------------------------------------*/
290 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
291 * completion callbacks. 2.5 should have fixed those bugs...
294 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
296 unsigned long flags;
298 spin_lock_irqsave(&list->lock, flags);
299 __skb_unlink(skb, list);
300 spin_unlock(&list->lock);
301 spin_lock(&dev->done.lock);
302 __skb_queue_tail(&dev->done, skb);
303 if (dev->done.qlen == 1)
304 tasklet_schedule(&dev->bh);
305 spin_unlock_irqrestore(&dev->done.lock, flags);
308 /* some work can't be done in tasklets, so we use keventd
310 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
311 * but tasklet_schedule() doesn't. hope the failure is rare.
313 void usbnet_defer_kevent (struct usbnet *dev, int work)
315 set_bit (work, &dev->flags);
316 if (!schedule_work (&dev->kevent))
317 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
318 else
319 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
321 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
323 /*-------------------------------------------------------------------------*/
325 static void rx_complete (struct urb *urb);
327 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
329 struct sk_buff *skb;
330 struct skb_data *entry;
331 int retval = 0;
332 unsigned long lockflags;
333 size_t size = dev->rx_urb_size;
335 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
336 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
337 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
338 usb_free_urb (urb);
339 return;
341 skb_reserve (skb, NET_IP_ALIGN);
343 entry = (struct skb_data *) skb->cb;
344 entry->urb = urb;
345 entry->dev = dev;
346 entry->state = rx_start;
347 entry->length = 0;
349 usb_fill_bulk_urb (urb, dev->udev, dev->in,
350 skb->data, size, rx_complete, skb);
352 spin_lock_irqsave (&dev->rxq.lock, lockflags);
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 netif_dbg(dev, ifdown, dev->net, "device gone\n");
367 netif_device_detach (dev->net);
368 break;
369 default:
370 netif_dbg(dev, rx_err, dev->net,
371 "rx submit, %d\n", retval);
372 tasklet_schedule (&dev->bh);
373 break;
374 case 0:
375 __skb_queue_tail (&dev->rxq, skb);
377 } else {
378 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
379 retval = -ENOLINK;
381 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
382 if (retval) {
383 dev_kfree_skb_any (skb);
384 usb_free_urb (urb);
389 /*-------------------------------------------------------------------------*/
391 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
393 if (dev->driver_info->rx_fixup &&
394 !dev->driver_info->rx_fixup (dev, skb))
395 goto error;
396 // else network stack removes extra byte if we forced a short packet
398 if (skb->len)
399 usbnet_skb_return (dev, skb);
400 else {
401 netif_dbg(dev, rx_err, dev->net, "drop\n");
402 error:
403 dev->net->stats.rx_errors++;
404 skb_queue_tail (&dev->done, skb);
408 /*-------------------------------------------------------------------------*/
410 static void rx_complete (struct urb *urb)
412 struct sk_buff *skb = (struct sk_buff *) urb->context;
413 struct skb_data *entry = (struct skb_data *) skb->cb;
414 struct usbnet *dev = entry->dev;
415 int urb_status = urb->status;
417 skb_put (skb, urb->actual_length);
418 entry->state = rx_done;
419 entry->urb = NULL;
421 switch (urb_status) {
422 /* success */
423 case 0:
424 if (skb->len < dev->net->hard_header_len) {
425 entry->state = rx_cleanup;
426 dev->net->stats.rx_errors++;
427 dev->net->stats.rx_length_errors++;
428 netif_dbg(dev, rx_err, dev->net,
429 "rx length %d\n", skb->len);
431 break;
433 /* stalls need manual reset. this is rare ... except that
434 * when going through USB 2.0 TTs, unplug appears this way.
435 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
436 * storm, recovering as needed.
438 case -EPIPE:
439 dev->net->stats.rx_errors++;
440 usbnet_defer_kevent (dev, EVENT_RX_HALT);
441 // FALLTHROUGH
443 /* software-driven interface shutdown */
444 case -ECONNRESET: /* async unlink */
445 case -ESHUTDOWN: /* hardware gone */
446 netif_dbg(dev, ifdown, dev->net,
447 "rx shutdown, code %d\n", urb_status);
448 goto block;
450 /* we get controller i/o faults during khubd disconnect() delays.
451 * throttle down resubmits, to avoid log floods; just temporarily,
452 * so we still recover when the fault isn't a khubd delay.
454 case -EPROTO:
455 case -ETIME:
456 case -EILSEQ:
457 dev->net->stats.rx_errors++;
458 if (!timer_pending (&dev->delay)) {
459 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
460 netif_dbg(dev, link, dev->net,
461 "rx throttle %d\n", urb_status);
463 block:
464 entry->state = rx_cleanup;
465 entry->urb = urb;
466 urb = NULL;
467 break;
469 /* data overrun ... flush fifo? */
470 case -EOVERFLOW:
471 dev->net->stats.rx_over_errors++;
472 // FALLTHROUGH
474 default:
475 entry->state = rx_cleanup;
476 dev->net->stats.rx_errors++;
477 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
478 break;
481 defer_bh(dev, skb, &dev->rxq);
483 if (urb) {
484 if (netif_running (dev->net) &&
485 !test_bit (EVENT_RX_HALT, &dev->flags)) {
486 rx_submit (dev, urb, GFP_ATOMIC);
487 return;
489 usb_free_urb (urb);
491 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
494 static void intr_complete (struct urb *urb)
496 struct usbnet *dev = urb->context;
497 int status = urb->status;
499 switch (status) {
500 /* success */
501 case 0:
502 dev->driver_info->status(dev, urb);
503 break;
505 /* software-driven interface shutdown */
506 case -ENOENT: /* urb killed */
507 case -ESHUTDOWN: /* hardware gone */
508 netif_dbg(dev, ifdown, dev->net,
509 "intr shutdown, code %d\n", status);
510 return;
512 /* NOTE: not throttling like RX/TX, since this endpoint
513 * already polls infrequently
515 default:
516 netdev_dbg(dev->net, "intr status %d\n", status);
517 break;
520 if (!netif_running (dev->net))
521 return;
523 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
524 status = usb_submit_urb (urb, GFP_ATOMIC);
525 if (status != 0)
526 netif_err(dev, timer, dev->net,
527 "intr resubmit --> %d\n", status);
530 /*-------------------------------------------------------------------------*/
531 void usbnet_pause_rx(struct usbnet *dev)
533 set_bit(EVENT_RX_PAUSED, &dev->flags);
535 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
537 EXPORT_SYMBOL_GPL(usbnet_pause_rx);
539 void usbnet_resume_rx(struct usbnet *dev)
541 struct sk_buff *skb;
542 int num = 0;
544 clear_bit(EVENT_RX_PAUSED, &dev->flags);
546 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
547 usbnet_skb_return(dev, skb);
548 num++;
551 tasklet_schedule(&dev->bh);
553 netif_dbg(dev, rx_status, dev->net,
554 "paused rx queue disabled, %d skbs requeued\n", num);
556 EXPORT_SYMBOL_GPL(usbnet_resume_rx);
558 void usbnet_purge_paused_rxq(struct usbnet *dev)
560 skb_queue_purge(&dev->rxq_pause);
562 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
564 /*-------------------------------------------------------------------------*/
566 // unlink pending rx/tx; completion handlers do all other cleanup
568 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
570 unsigned long flags;
571 struct sk_buff *skb, *skbnext;
572 int count = 0;
574 spin_lock_irqsave (&q->lock, flags);
575 skb_queue_walk_safe(q, skb, skbnext) {
576 struct skb_data *entry;
577 struct urb *urb;
578 int retval;
580 entry = (struct skb_data *) skb->cb;
581 urb = entry->urb;
583 // during some PM-driven resume scenarios,
584 // these (async) unlinks complete immediately
585 retval = usb_unlink_urb (urb);
586 if (retval != -EINPROGRESS && retval != 0)
587 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
588 else
589 count++;
591 spin_unlock_irqrestore (&q->lock, flags);
592 return count;
595 // Flush all pending rx urbs
596 // minidrivers may need to do this when the MTU changes
598 void usbnet_unlink_rx_urbs(struct usbnet *dev)
600 if (netif_running(dev->net)) {
601 (void) unlink_urbs (dev, &dev->rxq);
602 tasklet_schedule(&dev->bh);
605 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
607 /*-------------------------------------------------------------------------*/
609 // precondition: never called in_interrupt
610 static void usbnet_terminate_urbs(struct usbnet *dev)
612 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
613 DECLARE_WAITQUEUE(wait, current);
614 int temp;
616 /* ensure there are no more active urbs */
617 add_wait_queue(&unlink_wakeup, &wait);
618 set_current_state(TASK_UNINTERRUPTIBLE);
619 dev->wait = &unlink_wakeup;
620 temp = unlink_urbs(dev, &dev->txq) +
621 unlink_urbs(dev, &dev->rxq);
623 /* maybe wait for deletions to finish. */
624 while (!skb_queue_empty(&dev->rxq)
625 && !skb_queue_empty(&dev->txq)
626 && !skb_queue_empty(&dev->done)) {
627 schedule_timeout(UNLINK_TIMEOUT_MS);
628 set_current_state(TASK_UNINTERRUPTIBLE);
629 netif_dbg(dev, ifdown, dev->net,
630 "waited for %d urb completions\n", temp);
632 set_current_state(TASK_RUNNING);
633 dev->wait = NULL;
634 remove_wait_queue(&unlink_wakeup, &wait);
637 int usbnet_stop (struct net_device *net)
639 struct usbnet *dev = netdev_priv(net);
640 struct driver_info *info = dev->driver_info;
641 int retval;
643 netif_stop_queue (net);
645 netif_info(dev, ifdown, dev->net,
646 "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
647 net->stats.rx_packets, net->stats.tx_packets,
648 net->stats.rx_errors, net->stats.tx_errors);
650 /* allow minidriver to stop correctly (wireless devices to turn off
651 * radio etc) */
652 if (info->stop) {
653 retval = info->stop(dev);
654 if (retval < 0)
655 netif_info(dev, ifdown, dev->net,
656 "stop fail (%d) usbnet usb-%s-%s, %s\n",
657 retval,
658 dev->udev->bus->bus_name, dev->udev->devpath,
659 info->description);
662 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
663 usbnet_terminate_urbs(dev);
665 usb_kill_urb(dev->interrupt);
667 usbnet_purge_paused_rxq(dev);
669 /* deferred work (task, timer, softirq) must also stop.
670 * can't flush_scheduled_work() until we drop rtnl (later),
671 * else workers could deadlock; so make workers a NOP.
673 dev->flags = 0;
674 del_timer_sync (&dev->delay);
675 tasklet_kill (&dev->bh);
676 if (info->manage_power)
677 info->manage_power(dev, 0);
678 else
679 usb_autopm_put_interface(dev->intf);
681 return 0;
683 EXPORT_SYMBOL_GPL(usbnet_stop);
685 /*-------------------------------------------------------------------------*/
687 // posts reads, and enables write queuing
689 // precondition: never called in_interrupt
691 int usbnet_open (struct net_device *net)
693 struct usbnet *dev = netdev_priv(net);
694 int retval;
695 struct driver_info *info = dev->driver_info;
697 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
698 netif_info(dev, ifup, dev->net,
699 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
700 retval,
701 dev->udev->bus->bus_name,
702 dev->udev->devpath,
703 info->description);
704 goto done_nopm;
707 // put into "known safe" state
708 if (info->reset && (retval = info->reset (dev)) < 0) {
709 netif_info(dev, ifup, dev->net,
710 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
711 retval,
712 dev->udev->bus->bus_name,
713 dev->udev->devpath,
714 info->description);
715 goto done;
718 // insist peer be connected
719 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
720 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
721 goto done;
724 /* start any status interrupt transfer */
725 if (dev->interrupt) {
726 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
727 if (retval < 0) {
728 netif_err(dev, ifup, dev->net,
729 "intr submit %d\n", retval);
730 goto done;
734 netif_start_queue (net);
735 netif_info(dev, ifup, dev->net,
736 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
737 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
738 dev->net->mtu,
739 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
740 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
741 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
742 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
743 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
744 "simple");
746 // delay posting reads until we're fully open
747 tasklet_schedule (&dev->bh);
748 if (info->manage_power) {
749 retval = info->manage_power(dev, 1);
750 if (retval < 0)
751 goto done;
752 usb_autopm_put_interface(dev->intf);
754 return retval;
756 done:
757 usb_autopm_put_interface(dev->intf);
758 done_nopm:
759 return retval;
761 EXPORT_SYMBOL_GPL(usbnet_open);
763 /*-------------------------------------------------------------------------*/
765 /* ethtool methods; minidrivers may need to add some more, but
766 * they'll probably want to use this base set.
769 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
771 struct usbnet *dev = netdev_priv(net);
773 if (!dev->mii.mdio_read)
774 return -EOPNOTSUPP;
776 return mii_ethtool_gset(&dev->mii, cmd);
778 EXPORT_SYMBOL_GPL(usbnet_get_settings);
780 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
782 struct usbnet *dev = netdev_priv(net);
783 int retval;
785 if (!dev->mii.mdio_write)
786 return -EOPNOTSUPP;
788 retval = mii_ethtool_sset(&dev->mii, cmd);
790 /* link speed/duplex might have changed */
791 if (dev->driver_info->link_reset)
792 dev->driver_info->link_reset(dev);
794 return retval;
797 EXPORT_SYMBOL_GPL(usbnet_set_settings);
799 u32 usbnet_get_link (struct net_device *net)
801 struct usbnet *dev = netdev_priv(net);
803 /* If a check_connect is defined, return its result */
804 if (dev->driver_info->check_connect)
805 return dev->driver_info->check_connect (dev) == 0;
807 /* if the device has mii operations, use those */
808 if (dev->mii.mdio_read)
809 return mii_link_ok(&dev->mii);
811 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
812 return ethtool_op_get_link(net);
814 EXPORT_SYMBOL_GPL(usbnet_get_link);
816 int usbnet_nway_reset(struct net_device *net)
818 struct usbnet *dev = netdev_priv(net);
820 if (!dev->mii.mdio_write)
821 return -EOPNOTSUPP;
823 return mii_nway_restart(&dev->mii);
825 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
827 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
829 struct usbnet *dev = netdev_priv(net);
831 strncpy (info->driver, dev->driver_name, sizeof info->driver);
832 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
833 strncpy (info->fw_version, dev->driver_info->description,
834 sizeof info->fw_version);
835 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
837 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
839 u32 usbnet_get_msglevel (struct net_device *net)
841 struct usbnet *dev = netdev_priv(net);
843 return dev->msg_enable;
845 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
847 void usbnet_set_msglevel (struct net_device *net, u32 level)
849 struct usbnet *dev = netdev_priv(net);
851 dev->msg_enable = level;
853 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
855 /* drivers may override default ethtool_ops in their bind() routine */
856 static const struct ethtool_ops usbnet_ethtool_ops = {
857 .get_settings = usbnet_get_settings,
858 .set_settings = usbnet_set_settings,
859 .get_link = usbnet_get_link,
860 .nway_reset = usbnet_nway_reset,
861 .get_drvinfo = usbnet_get_drvinfo,
862 .get_msglevel = usbnet_get_msglevel,
863 .set_msglevel = usbnet_set_msglevel,
866 /*-------------------------------------------------------------------------*/
868 /* work that cannot be done in interrupt context uses keventd.
870 * NOTE: with 2.5 we could do more of this using completion callbacks,
871 * especially now that control transfers can be queued.
873 static void
874 kevent (struct work_struct *work)
876 struct usbnet *dev =
877 container_of(work, struct usbnet, kevent);
878 int status;
880 /* usb_clear_halt() needs a thread context */
881 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
882 unlink_urbs (dev, &dev->txq);
883 status = usb_autopm_get_interface(dev->intf);
884 if (status < 0)
885 goto fail_pipe;
886 status = usb_clear_halt (dev->udev, dev->out);
887 usb_autopm_put_interface(dev->intf);
888 if (status < 0 &&
889 status != -EPIPE &&
890 status != -ESHUTDOWN) {
891 if (netif_msg_tx_err (dev))
892 fail_pipe:
893 netdev_err(dev->net, "can't clear tx halt, status %d\n",
894 status);
895 } else {
896 clear_bit (EVENT_TX_HALT, &dev->flags);
897 if (status != -ESHUTDOWN)
898 netif_wake_queue (dev->net);
901 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
902 unlink_urbs (dev, &dev->rxq);
903 status = usb_autopm_get_interface(dev->intf);
904 if (status < 0)
905 goto fail_halt;
906 status = usb_clear_halt (dev->udev, dev->in);
907 usb_autopm_put_interface(dev->intf);
908 if (status < 0 &&
909 status != -EPIPE &&
910 status != -ESHUTDOWN) {
911 if (netif_msg_rx_err (dev))
912 fail_halt:
913 netdev_err(dev->net, "can't clear rx halt, status %d\n",
914 status);
915 } else {
916 clear_bit (EVENT_RX_HALT, &dev->flags);
917 tasklet_schedule (&dev->bh);
921 /* tasklet could resubmit itself forever if memory is tight */
922 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
923 struct urb *urb = NULL;
925 if (netif_running (dev->net))
926 urb = usb_alloc_urb (0, GFP_KERNEL);
927 else
928 clear_bit (EVENT_RX_MEMORY, &dev->flags);
929 if (urb != NULL) {
930 clear_bit (EVENT_RX_MEMORY, &dev->flags);
931 status = usb_autopm_get_interface(dev->intf);
932 if (status < 0)
933 goto fail_lowmem;
934 rx_submit (dev, urb, GFP_KERNEL);
935 usb_autopm_put_interface(dev->intf);
936 fail_lowmem:
937 tasklet_schedule (&dev->bh);
941 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
942 struct driver_info *info = dev->driver_info;
943 int retval = 0;
945 clear_bit (EVENT_LINK_RESET, &dev->flags);
946 status = usb_autopm_get_interface(dev->intf);
947 if (status < 0)
948 goto skip_reset;
949 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
950 usb_autopm_put_interface(dev->intf);
951 skip_reset:
952 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
953 retval,
954 dev->udev->bus->bus_name,
955 dev->udev->devpath,
956 info->description);
957 } else {
958 usb_autopm_put_interface(dev->intf);
962 if (dev->flags)
963 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
966 /*-------------------------------------------------------------------------*/
968 static void tx_complete (struct urb *urb)
970 struct sk_buff *skb = (struct sk_buff *) urb->context;
971 struct skb_data *entry = (struct skb_data *) skb->cb;
972 struct usbnet *dev = entry->dev;
974 if (urb->status == 0) {
975 dev->net->stats.tx_packets++;
976 dev->net->stats.tx_bytes += entry->length;
977 } else {
978 dev->net->stats.tx_errors++;
980 switch (urb->status) {
981 case -EPIPE:
982 usbnet_defer_kevent (dev, EVENT_TX_HALT);
983 break;
985 /* software-driven interface shutdown */
986 case -ECONNRESET: // async unlink
987 case -ESHUTDOWN: // hardware gone
988 break;
990 // like rx, tx gets controller i/o faults during khubd delays
991 // and so it uses the same throttling mechanism.
992 case -EPROTO:
993 case -ETIME:
994 case -EILSEQ:
995 usb_mark_last_busy(dev->udev);
996 if (!timer_pending (&dev->delay)) {
997 mod_timer (&dev->delay,
998 jiffies + THROTTLE_JIFFIES);
999 netif_dbg(dev, link, dev->net,
1000 "tx throttle %d\n", urb->status);
1002 netif_stop_queue (dev->net);
1003 break;
1004 default:
1005 netif_dbg(dev, tx_err, dev->net,
1006 "tx err %d\n", entry->urb->status);
1007 break;
1011 usb_autopm_put_interface_async(dev->intf);
1012 urb->dev = NULL;
1013 entry->state = tx_done;
1014 defer_bh(dev, skb, &dev->txq);
1017 /*-------------------------------------------------------------------------*/
1019 void usbnet_tx_timeout (struct net_device *net)
1021 struct usbnet *dev = netdev_priv(net);
1023 unlink_urbs (dev, &dev->txq);
1024 tasklet_schedule (&dev->bh);
1026 // FIXME: device recovery -- reset?
1028 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1030 /*-------------------------------------------------------------------------*/
1032 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1033 struct net_device *net)
1035 struct usbnet *dev = netdev_priv(net);
1036 int length;
1037 struct urb *urb = NULL;
1038 struct skb_data *entry;
1039 struct driver_info *info = dev->driver_info;
1040 unsigned long flags;
1041 int retval;
1043 // some devices want funky USB-level framing, for
1044 // win32 driver (usually) and/or hardware quirks
1045 if (info->tx_fixup) {
1046 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1047 if (!skb) {
1048 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1049 goto drop;
1052 length = skb->len;
1054 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1055 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1056 goto drop;
1059 entry = (struct skb_data *) skb->cb;
1060 entry->urb = urb;
1061 entry->dev = dev;
1062 entry->state = tx_start;
1063 entry->length = length;
1065 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1066 skb->data, skb->len, tx_complete, skb);
1068 /* don't assume the hardware handles USB_ZERO_PACKET
1069 * NOTE: strictly conforming cdc-ether devices should expect
1070 * the ZLP here, but ignore the one-byte packet.
1072 if (length % dev->maxpacket == 0) {
1073 if (!(info->flags & FLAG_SEND_ZLP)) {
1074 urb->transfer_buffer_length++;
1075 if (skb_tailroom(skb)) {
1076 skb->data[skb->len] = 0;
1077 __skb_put(skb, 1);
1079 } else
1080 urb->transfer_flags |= URB_ZERO_PACKET;
1083 spin_lock_irqsave(&dev->txq.lock, flags);
1084 retval = usb_autopm_get_interface_async(dev->intf);
1085 if (retval < 0) {
1086 spin_unlock_irqrestore(&dev->txq.lock, flags);
1087 goto drop;
1090 #ifdef CONFIG_PM
1091 /* if this triggers the device is still a sleep */
1092 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1093 /* transmission will be done in resume */
1094 usb_anchor_urb(urb, &dev->deferred);
1095 /* no use to process more packets */
1096 netif_stop_queue(net);
1097 spin_unlock_irqrestore(&dev->txq.lock, flags);
1098 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
1099 goto deferred;
1101 #endif
1103 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1104 case -EPIPE:
1105 netif_stop_queue (net);
1106 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1107 usb_autopm_put_interface_async(dev->intf);
1108 break;
1109 default:
1110 usb_autopm_put_interface_async(dev->intf);
1111 netif_dbg(dev, tx_err, dev->net,
1112 "tx: submit urb err %d\n", retval);
1113 break;
1114 case 0:
1115 net->trans_start = jiffies;
1116 __skb_queue_tail (&dev->txq, skb);
1117 if (dev->txq.qlen >= TX_QLEN (dev))
1118 netif_stop_queue (net);
1120 spin_unlock_irqrestore (&dev->txq.lock, flags);
1122 if (retval) {
1123 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1124 drop:
1125 dev->net->stats.tx_dropped++;
1126 if (skb)
1127 dev_kfree_skb_any (skb);
1128 usb_free_urb (urb);
1129 } else
1130 netif_dbg(dev, tx_queued, dev->net,
1131 "> tx, len %d, type 0x%x\n", length, skb->protocol);
1132 #ifdef CONFIG_PM
1133 deferred:
1134 #endif
1135 return NETDEV_TX_OK;
1137 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1139 /*-------------------------------------------------------------------------*/
1141 // tasklet (work deferred from completions, in_irq) or timer
1143 static void usbnet_bh (unsigned long param)
1145 struct usbnet *dev = (struct usbnet *) param;
1146 struct sk_buff *skb;
1147 struct skb_data *entry;
1149 while ((skb = skb_dequeue (&dev->done))) {
1150 entry = (struct skb_data *) skb->cb;
1151 switch (entry->state) {
1152 case rx_done:
1153 entry->state = rx_cleanup;
1154 rx_process (dev, skb);
1155 continue;
1156 case tx_done:
1157 case rx_cleanup:
1158 usb_free_urb (entry->urb);
1159 dev_kfree_skb (skb);
1160 continue;
1161 default:
1162 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1166 // waiting for all pending urbs to complete?
1167 if (dev->wait) {
1168 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1169 wake_up (dev->wait);
1172 // or are we maybe short a few urbs?
1173 } else if (netif_running (dev->net) &&
1174 netif_device_present (dev->net) &&
1175 !timer_pending (&dev->delay) &&
1176 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1177 int temp = dev->rxq.qlen;
1178 int qlen = RX_QLEN (dev);
1180 if (temp < qlen) {
1181 struct urb *urb;
1182 int i;
1184 // don't refill the queue all at once
1185 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1186 urb = usb_alloc_urb (0, GFP_ATOMIC);
1187 if (urb != NULL)
1188 rx_submit (dev, urb, GFP_ATOMIC);
1190 if (temp != dev->rxq.qlen)
1191 netif_dbg(dev, link, dev->net,
1192 "rxqlen %d --> %d\n",
1193 temp, dev->rxq.qlen);
1194 if (dev->rxq.qlen < qlen)
1195 tasklet_schedule (&dev->bh);
1197 if (dev->txq.qlen < TX_QLEN (dev))
1198 netif_wake_queue (dev->net);
1203 /*-------------------------------------------------------------------------
1205 * USB Device Driver support
1207 *-------------------------------------------------------------------------*/
1209 // precondition: never called in_interrupt
1211 void usbnet_disconnect (struct usb_interface *intf)
1213 struct usbnet *dev;
1214 struct usb_device *xdev;
1215 struct net_device *net;
1217 dev = usb_get_intfdata(intf);
1218 usb_set_intfdata(intf, NULL);
1219 if (!dev)
1220 return;
1222 xdev = interface_to_usbdev (intf);
1224 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1225 intf->dev.driver->name,
1226 xdev->bus->bus_name, xdev->devpath,
1227 dev->driver_info->description);
1229 net = dev->net;
1230 unregister_netdev (net);
1232 /* we don't hold rtnl here ... */
1233 flush_scheduled_work ();
1235 if (dev->driver_info->unbind)
1236 dev->driver_info->unbind (dev, intf);
1238 free_netdev(net);
1239 usb_put_dev (xdev);
1241 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1243 static const struct net_device_ops usbnet_netdev_ops = {
1244 .ndo_open = usbnet_open,
1245 .ndo_stop = usbnet_stop,
1246 .ndo_start_xmit = usbnet_start_xmit,
1247 .ndo_tx_timeout = usbnet_tx_timeout,
1248 .ndo_change_mtu = usbnet_change_mtu,
1249 .ndo_set_mac_address = eth_mac_addr,
1250 .ndo_validate_addr = eth_validate_addr,
1253 /*-------------------------------------------------------------------------*/
1255 // precondition: never called in_interrupt
1257 static struct device_type wlan_type = {
1258 .name = "wlan",
1261 static struct device_type wwan_type = {
1262 .name = "wwan",
1266 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1268 struct usbnet *dev;
1269 struct net_device *net;
1270 struct usb_host_interface *interface;
1271 struct driver_info *info;
1272 struct usb_device *xdev;
1273 int status;
1274 const char *name;
1276 name = udev->dev.driver->name;
1277 info = (struct driver_info *) prod->driver_info;
1278 if (!info) {
1279 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1280 return -ENODEV;
1282 xdev = interface_to_usbdev (udev);
1283 interface = udev->cur_altsetting;
1285 usb_get_dev (xdev);
1287 status = -ENOMEM;
1289 // set up our own records
1290 net = alloc_etherdev(sizeof(*dev));
1291 if (!net) {
1292 dbg ("can't kmalloc dev");
1293 goto out;
1296 dev = netdev_priv(net);
1297 dev->udev = xdev;
1298 dev->intf = udev;
1299 dev->driver_info = info;
1300 dev->driver_name = name;
1301 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1302 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1303 skb_queue_head_init (&dev->rxq);
1304 skb_queue_head_init (&dev->txq);
1305 skb_queue_head_init (&dev->done);
1306 skb_queue_head_init(&dev->rxq_pause);
1307 dev->bh.func = usbnet_bh;
1308 dev->bh.data = (unsigned long) dev;
1309 INIT_WORK (&dev->kevent, kevent);
1310 init_usb_anchor(&dev->deferred);
1311 dev->delay.function = usbnet_bh;
1312 dev->delay.data = (unsigned long) dev;
1313 init_timer (&dev->delay);
1314 mutex_init (&dev->phy_mutex);
1316 dev->net = net;
1317 strcpy (net->name, "usb%d");
1318 memcpy (net->dev_addr, node_id, sizeof node_id);
1320 /* rx and tx sides can use different message sizes;
1321 * bind() should set rx_urb_size in that case.
1323 dev->hard_mtu = net->mtu + net->hard_header_len;
1324 #if 0
1325 // dma_supported() is deeply broken on almost all architectures
1326 // possible with some EHCI controllers
1327 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
1328 net->features |= NETIF_F_HIGHDMA;
1329 #endif
1331 net->netdev_ops = &usbnet_netdev_ops;
1332 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1333 net->ethtool_ops = &usbnet_ethtool_ops;
1335 // allow device-specific bind/init procedures
1336 // NOTE net->name still not usable ...
1337 if (info->bind) {
1338 status = info->bind (dev, udev);
1339 if (status < 0)
1340 goto out1;
1342 // heuristic: "usb%d" for links we know are two-host,
1343 // else "eth%d" when there's reasonable doubt. userspace
1344 // can rename the link if it knows better.
1345 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1346 (net->dev_addr [0] & 0x02) == 0)
1347 strcpy (net->name, "eth%d");
1348 /* WLAN devices should always be named "wlan%d" */
1349 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1350 strcpy(net->name, "wlan%d");
1351 /* WWAN devices should always be named "wwan%d" */
1352 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1353 strcpy(net->name, "wwan%d");
1355 /* maybe the remote can't receive an Ethernet MTU */
1356 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1357 net->mtu = dev->hard_mtu - net->hard_header_len;
1358 } else if (!info->in || !info->out)
1359 status = usbnet_get_endpoints (dev, udev);
1360 else {
1361 dev->in = usb_rcvbulkpipe (xdev, info->in);
1362 dev->out = usb_sndbulkpipe (xdev, info->out);
1363 if (!(info->flags & FLAG_NO_SETINT))
1364 status = usb_set_interface (xdev,
1365 interface->desc.bInterfaceNumber,
1366 interface->desc.bAlternateSetting);
1367 else
1368 status = 0;
1371 if (status >= 0 && dev->status)
1372 status = init_status (dev, udev);
1373 if (status < 0)
1374 goto out3;
1376 if (!dev->rx_urb_size)
1377 dev->rx_urb_size = dev->hard_mtu;
1378 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1380 SET_NETDEV_DEV(net, &udev->dev);
1382 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1383 SET_NETDEV_DEVTYPE(net, &wlan_type);
1384 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1385 SET_NETDEV_DEVTYPE(net, &wwan_type);
1387 status = register_netdev (net);
1388 if (status)
1389 goto out3;
1390 netif_info(dev, probe, dev->net,
1391 "register '%s' at usb-%s-%s, %s, %pM\n",
1392 udev->dev.driver->name,
1393 xdev->bus->bus_name, xdev->devpath,
1394 dev->driver_info->description,
1395 net->dev_addr);
1397 // ok, it's ready to go.
1398 usb_set_intfdata (udev, dev);
1400 netif_device_attach (net);
1402 if (dev->driver_info->flags & FLAG_LINK_INTR)
1403 netif_carrier_off(net);
1405 return 0;
1407 out3:
1408 if (info->unbind)
1409 info->unbind (dev, udev);
1410 out1:
1411 free_netdev(net);
1412 out:
1413 usb_put_dev(xdev);
1414 return status;
1416 EXPORT_SYMBOL_GPL(usbnet_probe);
1418 /*-------------------------------------------------------------------------*/
1421 * suspend the whole driver as soon as the first interface is suspended
1422 * resume only when the last interface is resumed
1425 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1427 struct usbnet *dev = usb_get_intfdata(intf);
1429 if (!dev->suspend_count++) {
1430 spin_lock_irq(&dev->txq.lock);
1431 /* don't autosuspend while transmitting */
1432 if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) {
1433 spin_unlock_irq(&dev->txq.lock);
1434 return -EBUSY;
1435 } else {
1436 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1437 spin_unlock_irq(&dev->txq.lock);
1440 * accelerate emptying of the rx and queues, to avoid
1441 * having everything error out.
1443 netif_device_detach (dev->net);
1444 usbnet_terminate_urbs(dev);
1445 usb_kill_urb(dev->interrupt);
1448 * reattach so runtime management can use and
1449 * wake the device
1451 netif_device_attach (dev->net);
1453 return 0;
1455 EXPORT_SYMBOL_GPL(usbnet_suspend);
1457 int usbnet_resume (struct usb_interface *intf)
1459 struct usbnet *dev = usb_get_intfdata(intf);
1460 struct sk_buff *skb;
1461 struct urb *res;
1462 int retval;
1464 if (!--dev->suspend_count) {
1465 spin_lock_irq(&dev->txq.lock);
1466 while ((res = usb_get_from_anchor(&dev->deferred))) {
1468 printk(KERN_INFO"%s has delayed data\n", __func__);
1469 skb = (struct sk_buff *)res->context;
1470 retval = usb_submit_urb(res, GFP_ATOMIC);
1471 if (retval < 0) {
1472 dev_kfree_skb_any(skb);
1473 usb_free_urb(res);
1474 usb_autopm_put_interface_async(dev->intf);
1475 } else {
1476 dev->net->trans_start = jiffies;
1477 __skb_queue_tail(&dev->txq, skb);
1481 smp_mb();
1482 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1483 spin_unlock_irq(&dev->txq.lock);
1484 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1485 netif_start_queue(dev->net);
1486 tasklet_schedule (&dev->bh);
1488 return 0;
1490 EXPORT_SYMBOL_GPL(usbnet_resume);
1493 /*-------------------------------------------------------------------------*/
1495 static int __init usbnet_init(void)
1497 /* compiler should optimize this out */
1498 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1499 < sizeof (struct skb_data));
1501 random_ether_addr(node_id);
1502 return 0;
1504 module_init(usbnet_init);
1506 static void __exit usbnet_exit(void)
1509 module_exit(usbnet_exit);
1511 MODULE_AUTHOR("David Brownell");
1512 MODULE_DESCRIPTION("USB network driver framework");
1513 MODULE_LICENSE("GPL");