Pull bugzilla-5764 into release branch
[linux-2.6/verdex.git] / drivers / usb / net / usbnet.c
blob362d6907c9bba3d68cd54f6898581deca3c7eada
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/config.h>
37 #include <linux/module.h>
38 #include <linux/sched.h>
39 #include <linux/init.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/workqueue.h>
44 #include <linux/mii.h>
45 #include <linux/usb.h>
47 #include "usbnet.h"
49 #define DRIVER_VERSION "22-Aug-2005"
52 /*-------------------------------------------------------------------------*/
55 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
56 * Several dozen bytes of IPv4 data can fit in two such transactions.
57 * One maximum size Ethernet packet takes twenty four of them.
58 * For high speed, each frame comfortably fits almost 36 max size
59 * Ethernet packets (so queues should be bigger).
61 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
62 * let the USB host controller be busy for 5msec or more before an irq
63 * is required, under load. Jumbograms change the equation.
65 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
66 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
68 // reawaken network queue this soon after stopping; else watchdog barks
69 #define TX_TIMEOUT_JIFFIES (5*HZ)
71 // throttle rx/tx briefly after some faults, so khubd might disconnect()
72 // us (it polls at HZ/4 usually) before we report too many false errors.
73 #define THROTTLE_JIFFIES (HZ/8)
75 // between wakeups
76 #define UNLINK_TIMEOUT_MS 3
78 /*-------------------------------------------------------------------------*/
80 // randomly generated ethernet address
81 static u8 node_id [ETH_ALEN];
83 static const char driver_name [] = "usbnet";
85 /* use ethtool to change the level for any given device */
86 static int msg_level = -1;
87 module_param (msg_level, int, 0);
88 MODULE_PARM_DESC (msg_level, "Override default message level");
90 /*-------------------------------------------------------------------------*/
92 /* handles CDC Ethernet and many other network "bulk data" interfaces */
93 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
95 int tmp;
96 struct usb_host_interface *alt = NULL;
97 struct usb_host_endpoint *in = NULL, *out = NULL;
98 struct usb_host_endpoint *status = NULL;
100 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
101 unsigned ep;
103 in = out = status = NULL;
104 alt = intf->altsetting + tmp;
106 /* take the first altsetting with in-bulk + out-bulk;
107 * remember any status endpoint, just in case;
108 * ignore other endpoints and altsetttings.
110 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
111 struct usb_host_endpoint *e;
112 int intr = 0;
114 e = alt->endpoint + ep;
115 switch (e->desc.bmAttributes) {
116 case USB_ENDPOINT_XFER_INT:
117 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
118 continue;
119 intr = 1;
120 /* FALLTHROUGH */
121 case USB_ENDPOINT_XFER_BULK:
122 break;
123 default:
124 continue;
126 if (e->desc.bEndpointAddress & USB_DIR_IN) {
127 if (!intr && !in)
128 in = e;
129 else if (intr && !status)
130 status = e;
131 } else {
132 if (!out)
133 out = e;
136 if (in && out)
137 break;
139 if (!alt || !in || !out)
140 return -EINVAL;
142 if (alt->desc.bAlternateSetting != 0
143 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
144 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
145 alt->desc.bAlternateSetting);
146 if (tmp < 0)
147 return tmp;
150 dev->in = usb_rcvbulkpipe (dev->udev,
151 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
152 dev->out = usb_sndbulkpipe (dev->udev,
153 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
154 dev->status = status;
155 return 0;
157 EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
159 static void intr_complete (struct urb *urb, struct pt_regs *regs);
161 static int init_status (struct usbnet *dev, struct usb_interface *intf)
163 char *buf = NULL;
164 unsigned pipe = 0;
165 unsigned maxp;
166 unsigned period;
168 if (!dev->driver_info->status)
169 return 0;
171 pipe = usb_rcvintpipe (dev->udev,
172 dev->status->desc.bEndpointAddress
173 & USB_ENDPOINT_NUMBER_MASK);
174 maxp = usb_maxpacket (dev->udev, pipe, 0);
176 /* avoid 1 msec chatter: min 8 msec poll rate */
177 period = max ((int) dev->status->desc.bInterval,
178 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
180 buf = kmalloc (maxp, SLAB_KERNEL);
181 if (buf) {
182 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
183 if (!dev->interrupt) {
184 kfree (buf);
185 return -ENOMEM;
186 } else {
187 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
188 buf, maxp, intr_complete, dev, period);
189 dev_dbg(&intf->dev,
190 "status ep%din, %d bytes period %d\n",
191 usb_pipeendpoint(pipe), maxp, period);
194 return 0;
197 /* Passes this packet up the stack, updating its accounting.
198 * Some link protocols batch packets, so their rx_fixup paths
199 * can return clones as well as just modify the original skb.
201 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
203 int status;
205 skb->dev = dev->net;
206 skb->protocol = eth_type_trans (skb, dev->net);
207 dev->stats.rx_packets++;
208 dev->stats.rx_bytes += skb->len;
210 if (netif_msg_rx_status (dev))
211 devdbg (dev, "< rx, len %zu, type 0x%x",
212 skb->len + sizeof (struct ethhdr), skb->protocol);
213 memset (skb->cb, 0, sizeof (struct skb_data));
214 status = netif_rx (skb);
215 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
216 devdbg (dev, "netif_rx status %d", status);
218 EXPORT_SYMBOL_GPL(usbnet_skb_return);
221 /*-------------------------------------------------------------------------
223 * Network Device Driver (peer link to "Host Device", from USB host)
225 *-------------------------------------------------------------------------*/
227 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
229 struct usbnet *dev = netdev_priv(net);
230 int ll_mtu = new_mtu + net->hard_header_len;
232 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
233 return -EINVAL;
234 // no second zero-length packet read wanted after mtu-sized packets
235 if ((ll_mtu % dev->maxpacket) == 0)
236 return -EDOM;
237 net->mtu = new_mtu;
238 return 0;
241 /*-------------------------------------------------------------------------*/
243 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
245 struct usbnet *dev = netdev_priv(net);
246 return &dev->stats;
249 /*-------------------------------------------------------------------------*/
251 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
252 * completion callbacks. 2.5 should have fixed those bugs...
255 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
257 unsigned long flags;
259 spin_lock_irqsave(&list->lock, flags);
260 __skb_unlink(skb, list);
261 spin_unlock(&list->lock);
262 spin_lock(&dev->done.lock);
263 __skb_queue_tail(&dev->done, skb);
264 if (dev->done.qlen == 1)
265 tasklet_schedule(&dev->bh);
266 spin_unlock_irqrestore(&dev->done.lock, flags);
269 /* some work can't be done in tasklets, so we use keventd
271 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
272 * but tasklet_schedule() doesn't. hope the failure is rare.
274 void usbnet_defer_kevent (struct usbnet *dev, int work)
276 set_bit (work, &dev->flags);
277 if (!schedule_work (&dev->kevent))
278 deverr (dev, "kevent %d may have been dropped", work);
279 else
280 devdbg (dev, "kevent %d scheduled", work);
282 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
284 /*-------------------------------------------------------------------------*/
286 static void rx_complete (struct urb *urb, struct pt_regs *regs);
288 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
290 struct sk_buff *skb;
291 struct skb_data *entry;
292 int retval = 0;
293 unsigned long lockflags;
294 size_t size = dev->rx_urb_size;
296 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
297 if (netif_msg_rx_err (dev))
298 devdbg (dev, "no rx skb");
299 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
300 usb_free_urb (urb);
301 return;
303 skb_reserve (skb, NET_IP_ALIGN);
305 entry = (struct skb_data *) skb->cb;
306 entry->urb = urb;
307 entry->dev = dev;
308 entry->state = rx_start;
309 entry->length = 0;
311 usb_fill_bulk_urb (urb, dev->udev, dev->in,
312 skb->data, size, rx_complete, skb);
314 spin_lock_irqsave (&dev->rxq.lock, lockflags);
316 if (netif_running (dev->net)
317 && netif_device_present (dev->net)
318 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
319 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
320 case -EPIPE:
321 usbnet_defer_kevent (dev, EVENT_RX_HALT);
322 break;
323 case -ENOMEM:
324 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
325 break;
326 case -ENODEV:
327 if (netif_msg_ifdown (dev))
328 devdbg (dev, "device gone");
329 netif_device_detach (dev->net);
330 break;
331 default:
332 if (netif_msg_rx_err (dev))
333 devdbg (dev, "rx submit, %d", retval);
334 tasklet_schedule (&dev->bh);
335 break;
336 case 0:
337 __skb_queue_tail (&dev->rxq, skb);
339 } else {
340 if (netif_msg_ifdown (dev))
341 devdbg (dev, "rx: stopped");
342 retval = -ENOLINK;
344 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
345 if (retval) {
346 dev_kfree_skb_any (skb);
347 usb_free_urb (urb);
352 /*-------------------------------------------------------------------------*/
354 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
356 if (dev->driver_info->rx_fixup
357 && !dev->driver_info->rx_fixup (dev, skb))
358 goto error;
359 // else network stack removes extra byte if we forced a short packet
361 if (skb->len)
362 usbnet_skb_return (dev, skb);
363 else {
364 if (netif_msg_rx_err (dev))
365 devdbg (dev, "drop");
366 error:
367 dev->stats.rx_errors++;
368 skb_queue_tail (&dev->done, skb);
372 /*-------------------------------------------------------------------------*/
374 static void rx_complete (struct urb *urb, struct pt_regs *regs)
376 struct sk_buff *skb = (struct sk_buff *) urb->context;
377 struct skb_data *entry = (struct skb_data *) skb->cb;
378 struct usbnet *dev = entry->dev;
379 int urb_status = urb->status;
381 skb_put (skb, urb->actual_length);
382 entry->state = rx_done;
383 entry->urb = NULL;
385 switch (urb_status) {
386 // success
387 case 0:
388 if (skb->len < dev->net->hard_header_len) {
389 entry->state = rx_cleanup;
390 dev->stats.rx_errors++;
391 dev->stats.rx_length_errors++;
392 if (netif_msg_rx_err (dev))
393 devdbg (dev, "rx length %d", skb->len);
395 break;
397 // stalls need manual reset. this is rare ... except that
398 // when going through USB 2.0 TTs, unplug appears this way.
399 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
400 // storm, recovering as needed.
401 case -EPIPE:
402 dev->stats.rx_errors++;
403 usbnet_defer_kevent (dev, EVENT_RX_HALT);
404 // FALLTHROUGH
406 // software-driven interface shutdown
407 case -ECONNRESET: // async unlink
408 case -ESHUTDOWN: // hardware gone
409 if (netif_msg_ifdown (dev))
410 devdbg (dev, "rx shutdown, code %d", urb_status);
411 goto block;
413 // we get controller i/o faults during khubd disconnect() delays.
414 // throttle down resubmits, to avoid log floods; just temporarily,
415 // so we still recover when the fault isn't a khubd delay.
416 case -EPROTO: // ehci
417 case -ETIMEDOUT: // ohci
418 case -EILSEQ: // uhci
419 dev->stats.rx_errors++;
420 if (!timer_pending (&dev->delay)) {
421 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
422 if (netif_msg_link (dev))
423 devdbg (dev, "rx throttle %d", urb_status);
425 block:
426 entry->state = rx_cleanup;
427 entry->urb = urb;
428 urb = NULL;
429 break;
431 // data overrun ... flush fifo?
432 case -EOVERFLOW:
433 dev->stats.rx_over_errors++;
434 // FALLTHROUGH
436 default:
437 entry->state = rx_cleanup;
438 dev->stats.rx_errors++;
439 if (netif_msg_rx_err (dev))
440 devdbg (dev, "rx status %d", urb_status);
441 break;
444 defer_bh(dev, skb, &dev->rxq);
446 if (urb) {
447 if (netif_running (dev->net)
448 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
449 rx_submit (dev, urb, GFP_ATOMIC);
450 return;
452 usb_free_urb (urb);
454 if (netif_msg_rx_err (dev))
455 devdbg (dev, "no read resubmitted");
458 static void intr_complete (struct urb *urb, struct pt_regs *regs)
460 struct usbnet *dev = urb->context;
461 int status = urb->status;
463 switch (status) {
464 /* success */
465 case 0:
466 dev->driver_info->status(dev, urb);
467 break;
469 /* software-driven interface shutdown */
470 case -ENOENT: // urb killed
471 case -ESHUTDOWN: // hardware gone
472 if (netif_msg_ifdown (dev))
473 devdbg (dev, "intr shutdown, code %d", status);
474 return;
476 /* NOTE: not throttling like RX/TX, since this endpoint
477 * already polls infrequently
479 default:
480 devdbg (dev, "intr status %d", status);
481 break;
484 if (!netif_running (dev->net))
485 return;
487 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
488 status = usb_submit_urb (urb, GFP_ATOMIC);
489 if (status != 0 && netif_msg_timer (dev))
490 deverr(dev, "intr resubmit --> %d", status);
493 /*-------------------------------------------------------------------------*/
495 // unlink pending rx/tx; completion handlers do all other cleanup
497 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
499 unsigned long flags;
500 struct sk_buff *skb, *skbnext;
501 int count = 0;
503 spin_lock_irqsave (&q->lock, flags);
504 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
505 struct skb_data *entry;
506 struct urb *urb;
507 int retval;
509 entry = (struct skb_data *) skb->cb;
510 urb = entry->urb;
511 skbnext = skb->next;
513 // during some PM-driven resume scenarios,
514 // these (async) unlinks complete immediately
515 retval = usb_unlink_urb (urb);
516 if (retval != -EINPROGRESS && retval != 0)
517 devdbg (dev, "unlink urb err, %d", retval);
518 else
519 count++;
521 spin_unlock_irqrestore (&q->lock, flags);
522 return count;
526 /*-------------------------------------------------------------------------*/
528 // precondition: never called in_interrupt
530 static int usbnet_stop (struct net_device *net)
532 struct usbnet *dev = netdev_priv(net);
533 int temp;
534 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
535 DECLARE_WAITQUEUE (wait, current);
537 netif_stop_queue (net);
539 if (netif_msg_ifdown (dev))
540 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
541 dev->stats.rx_packets, dev->stats.tx_packets,
542 dev->stats.rx_errors, dev->stats.tx_errors
545 // ensure there are no more active urbs
546 add_wait_queue (&unlink_wakeup, &wait);
547 dev->wait = &unlink_wakeup;
548 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
550 // maybe wait for deletions to finish.
551 while (!skb_queue_empty(&dev->rxq) &&
552 !skb_queue_empty(&dev->txq) &&
553 !skb_queue_empty(&dev->done)) {
554 msleep(UNLINK_TIMEOUT_MS);
555 if (netif_msg_ifdown (dev))
556 devdbg (dev, "waited for %d urb completions", temp);
558 dev->wait = NULL;
559 remove_wait_queue (&unlink_wakeup, &wait);
561 usb_kill_urb(dev->interrupt);
563 /* deferred work (task, timer, softirq) must also stop.
564 * can't flush_scheduled_work() until we drop rtnl (later),
565 * else workers could deadlock; so make workers a NOP.
567 dev->flags = 0;
568 del_timer_sync (&dev->delay);
569 tasklet_kill (&dev->bh);
571 return 0;
574 /*-------------------------------------------------------------------------*/
576 // posts reads, and enables write queuing
578 // precondition: never called in_interrupt
580 static int usbnet_open (struct net_device *net)
582 struct usbnet *dev = netdev_priv(net);
583 int retval = 0;
584 struct driver_info *info = dev->driver_info;
586 // put into "known safe" state
587 if (info->reset && (retval = info->reset (dev)) < 0) {
588 if (netif_msg_ifup (dev))
589 devinfo (dev,
590 "open reset fail (%d) usbnet usb-%s-%s, %s",
591 retval,
592 dev->udev->bus->bus_name, dev->udev->devpath,
593 info->description);
594 goto done;
597 // insist peer be connected
598 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
599 if (netif_msg_ifup (dev))
600 devdbg (dev, "can't open; %d", retval);
601 goto done;
604 /* start any status interrupt transfer */
605 if (dev->interrupt) {
606 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
607 if (retval < 0) {
608 if (netif_msg_ifup (dev))
609 deverr (dev, "intr submit %d", retval);
610 goto done;
614 netif_start_queue (net);
615 if (netif_msg_ifup (dev)) {
616 char *framing;
618 if (dev->driver_info->flags & FLAG_FRAMING_NC)
619 framing = "NetChip";
620 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
621 framing = "GeneSys";
622 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
623 framing = "Zaurus";
624 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
625 framing = "RNDIS";
626 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
627 framing = "ASIX";
628 else
629 framing = "simple";
631 devinfo (dev, "open: enable queueing "
632 "(rx %d, tx %d) mtu %d %s framing",
633 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
634 framing);
637 // delay posting reads until we're fully open
638 tasklet_schedule (&dev->bh);
639 done:
640 return retval;
643 /*-------------------------------------------------------------------------*/
645 /* ethtool methods; minidrivers may need to add some more, but
646 * they'll probably want to use this base set.
649 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
651 struct usbnet *dev = netdev_priv(net);
653 /* REVISIT don't always return "usbnet" */
654 strncpy (info->driver, driver_name, sizeof info->driver);
655 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
656 strncpy (info->fw_version, dev->driver_info->description,
657 sizeof info->fw_version);
658 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
660 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
662 static u32 usbnet_get_link (struct net_device *net)
664 struct usbnet *dev = netdev_priv(net);
666 /* If a check_connect is defined, return its result */
667 if (dev->driver_info->check_connect)
668 return dev->driver_info->check_connect (dev) == 0;
670 /* Otherwise, say we're up (to avoid breaking scripts) */
671 return 1;
674 u32 usbnet_get_msglevel (struct net_device *net)
676 struct usbnet *dev = netdev_priv(net);
678 return dev->msg_enable;
680 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
682 void usbnet_set_msglevel (struct net_device *net, u32 level)
684 struct usbnet *dev = netdev_priv(net);
686 dev->msg_enable = level;
688 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
690 /* drivers may override default ethtool_ops in their bind() routine */
691 static struct ethtool_ops usbnet_ethtool_ops = {
692 .get_drvinfo = usbnet_get_drvinfo,
693 .get_link = usbnet_get_link,
694 .get_msglevel = usbnet_get_msglevel,
695 .set_msglevel = usbnet_set_msglevel,
698 /*-------------------------------------------------------------------------*/
700 /* work that cannot be done in interrupt context uses keventd.
702 * NOTE: with 2.5 we could do more of this using completion callbacks,
703 * especially now that control transfers can be queued.
705 static void
706 kevent (void *data)
708 struct usbnet *dev = data;
709 int status;
711 /* usb_clear_halt() needs a thread context */
712 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
713 unlink_urbs (dev, &dev->txq);
714 status = usb_clear_halt (dev->udev, dev->out);
715 if (status < 0
716 && status != -EPIPE
717 && status != -ESHUTDOWN) {
718 if (netif_msg_tx_err (dev))
719 deverr (dev, "can't clear tx halt, status %d",
720 status);
721 } else {
722 clear_bit (EVENT_TX_HALT, &dev->flags);
723 if (status != -ESHUTDOWN)
724 netif_wake_queue (dev->net);
727 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
728 unlink_urbs (dev, &dev->rxq);
729 status = usb_clear_halt (dev->udev, dev->in);
730 if (status < 0
731 && status != -EPIPE
732 && status != -ESHUTDOWN) {
733 if (netif_msg_rx_err (dev))
734 deverr (dev, "can't clear rx halt, status %d",
735 status);
736 } else {
737 clear_bit (EVENT_RX_HALT, &dev->flags);
738 tasklet_schedule (&dev->bh);
742 /* tasklet could resubmit itself forever if memory is tight */
743 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
744 struct urb *urb = NULL;
746 if (netif_running (dev->net))
747 urb = usb_alloc_urb (0, GFP_KERNEL);
748 else
749 clear_bit (EVENT_RX_MEMORY, &dev->flags);
750 if (urb != NULL) {
751 clear_bit (EVENT_RX_MEMORY, &dev->flags);
752 rx_submit (dev, urb, GFP_KERNEL);
753 tasklet_schedule (&dev->bh);
757 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
758 struct driver_info *info = dev->driver_info;
759 int retval = 0;
761 clear_bit (EVENT_LINK_RESET, &dev->flags);
762 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
763 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
764 retval,
765 dev->udev->bus->bus_name, dev->udev->devpath,
766 info->description);
770 if (dev->flags)
771 devdbg (dev, "kevent done, flags = 0x%lx",
772 dev->flags);
775 /*-------------------------------------------------------------------------*/
777 static void tx_complete (struct urb *urb, struct pt_regs *regs)
779 struct sk_buff *skb = (struct sk_buff *) urb->context;
780 struct skb_data *entry = (struct skb_data *) skb->cb;
781 struct usbnet *dev = entry->dev;
783 if (urb->status == 0) {
784 dev->stats.tx_packets++;
785 dev->stats.tx_bytes += entry->length;
786 } else {
787 dev->stats.tx_errors++;
789 switch (urb->status) {
790 case -EPIPE:
791 usbnet_defer_kevent (dev, EVENT_TX_HALT);
792 break;
794 /* software-driven interface shutdown */
795 case -ECONNRESET: // async unlink
796 case -ESHUTDOWN: // hardware gone
797 break;
799 // like rx, tx gets controller i/o faults during khubd delays
800 // and so it uses the same throttling mechanism.
801 case -EPROTO: // ehci
802 case -ETIMEDOUT: // ohci
803 case -EILSEQ: // uhci
804 if (!timer_pending (&dev->delay)) {
805 mod_timer (&dev->delay,
806 jiffies + THROTTLE_JIFFIES);
807 if (netif_msg_link (dev))
808 devdbg (dev, "tx throttle %d",
809 urb->status);
811 netif_stop_queue (dev->net);
812 break;
813 default:
814 if (netif_msg_tx_err (dev))
815 devdbg (dev, "tx err %d", entry->urb->status);
816 break;
820 urb->dev = NULL;
821 entry->state = tx_done;
822 defer_bh(dev, skb, &dev->txq);
825 /*-------------------------------------------------------------------------*/
827 static void usbnet_tx_timeout (struct net_device *net)
829 struct usbnet *dev = netdev_priv(net);
831 unlink_urbs (dev, &dev->txq);
832 tasklet_schedule (&dev->bh);
834 // FIXME: device recovery -- reset?
837 /*-------------------------------------------------------------------------*/
839 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
841 struct usbnet *dev = netdev_priv(net);
842 int length;
843 int retval = NET_XMIT_SUCCESS;
844 struct urb *urb = NULL;
845 struct skb_data *entry;
846 struct driver_info *info = dev->driver_info;
847 unsigned long flags;
849 // some devices want funky USB-level framing, for
850 // win32 driver (usually) and/or hardware quirks
851 if (info->tx_fixup) {
852 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
853 if (!skb) {
854 if (netif_msg_tx_err (dev))
855 devdbg (dev, "can't tx_fixup skb");
856 goto drop;
859 length = skb->len;
861 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
862 if (netif_msg_tx_err (dev))
863 devdbg (dev, "no urb");
864 goto drop;
867 entry = (struct skb_data *) skb->cb;
868 entry->urb = urb;
869 entry->dev = dev;
870 entry->state = tx_start;
871 entry->length = length;
873 usb_fill_bulk_urb (urb, dev->udev, dev->out,
874 skb->data, skb->len, tx_complete, skb);
876 /* don't assume the hardware handles USB_ZERO_PACKET
877 * NOTE: strictly conforming cdc-ether devices should expect
878 * the ZLP here, but ignore the one-byte packet.
880 * FIXME zero that byte, if it doesn't require a new skb.
882 if ((length % dev->maxpacket) == 0)
883 urb->transfer_buffer_length++;
885 spin_lock_irqsave (&dev->txq.lock, flags);
887 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
888 case -EPIPE:
889 netif_stop_queue (net);
890 usbnet_defer_kevent (dev, EVENT_TX_HALT);
891 break;
892 default:
893 if (netif_msg_tx_err (dev))
894 devdbg (dev, "tx: submit urb err %d", retval);
895 break;
896 case 0:
897 net->trans_start = jiffies;
898 __skb_queue_tail (&dev->txq, skb);
899 if (dev->txq.qlen >= TX_QLEN (dev))
900 netif_stop_queue (net);
902 spin_unlock_irqrestore (&dev->txq.lock, flags);
904 if (retval) {
905 if (netif_msg_tx_err (dev))
906 devdbg (dev, "drop, code %d", retval);
907 drop:
908 retval = NET_XMIT_SUCCESS;
909 dev->stats.tx_dropped++;
910 if (skb)
911 dev_kfree_skb_any (skb);
912 usb_free_urb (urb);
913 } else if (netif_msg_tx_queued (dev)) {
914 devdbg (dev, "> tx, len %d, type 0x%x",
915 length, skb->protocol);
917 return retval;
921 /*-------------------------------------------------------------------------*/
923 // tasklet (work deferred from completions, in_irq) or timer
925 static void usbnet_bh (unsigned long param)
927 struct usbnet *dev = (struct usbnet *) param;
928 struct sk_buff *skb;
929 struct skb_data *entry;
931 while ((skb = skb_dequeue (&dev->done))) {
932 entry = (struct skb_data *) skb->cb;
933 switch (entry->state) {
934 case rx_done:
935 entry->state = rx_cleanup;
936 rx_process (dev, skb);
937 continue;
938 case tx_done:
939 case rx_cleanup:
940 usb_free_urb (entry->urb);
941 dev_kfree_skb (skb);
942 continue;
943 default:
944 devdbg (dev, "bogus skb state %d", entry->state);
948 // waiting for all pending urbs to complete?
949 if (dev->wait) {
950 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
951 wake_up (dev->wait);
954 // or are we maybe short a few urbs?
955 } else if (netif_running (dev->net)
956 && netif_device_present (dev->net)
957 && !timer_pending (&dev->delay)
958 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
959 int temp = dev->rxq.qlen;
960 int qlen = RX_QLEN (dev);
962 if (temp < qlen) {
963 struct urb *urb;
964 int i;
966 // don't refill the queue all at once
967 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
968 urb = usb_alloc_urb (0, GFP_ATOMIC);
969 if (urb != NULL)
970 rx_submit (dev, urb, GFP_ATOMIC);
972 if (temp != dev->rxq.qlen && netif_msg_link (dev))
973 devdbg (dev, "rxqlen %d --> %d",
974 temp, dev->rxq.qlen);
975 if (dev->rxq.qlen < qlen)
976 tasklet_schedule (&dev->bh);
978 if (dev->txq.qlen < TX_QLEN (dev))
979 netif_wake_queue (dev->net);
985 /*-------------------------------------------------------------------------
987 * USB Device Driver support
989 *-------------------------------------------------------------------------*/
991 // precondition: never called in_interrupt
993 void usbnet_disconnect (struct usb_interface *intf)
995 struct usbnet *dev;
996 struct usb_device *xdev;
997 struct net_device *net;
999 dev = usb_get_intfdata(intf);
1000 usb_set_intfdata(intf, NULL);
1001 if (!dev)
1002 return;
1004 xdev = interface_to_usbdev (intf);
1006 if (netif_msg_probe (dev))
1007 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1008 intf->dev.driver->name,
1009 xdev->bus->bus_name, xdev->devpath,
1010 dev->driver_info->description);
1012 net = dev->net;
1013 unregister_netdev (net);
1015 /* we don't hold rtnl here ... */
1016 flush_scheduled_work ();
1018 if (dev->driver_info->unbind)
1019 dev->driver_info->unbind (dev, intf);
1021 free_netdev(net);
1022 usb_put_dev (xdev);
1024 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1027 /*-------------------------------------------------------------------------*/
1029 // precondition: never called in_interrupt
1032 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1034 struct usbnet *dev;
1035 struct net_device *net;
1036 struct usb_host_interface *interface;
1037 struct driver_info *info;
1038 struct usb_device *xdev;
1039 int status;
1041 info = (struct driver_info *) prod->driver_info;
1042 if (!info) {
1043 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1044 return -ENODEV;
1046 xdev = interface_to_usbdev (udev);
1047 interface = udev->cur_altsetting;
1049 usb_get_dev (xdev);
1051 status = -ENOMEM;
1053 // set up our own records
1054 net = alloc_etherdev(sizeof(*dev));
1055 if (!net) {
1056 dbg ("can't kmalloc dev");
1057 goto out;
1060 dev = netdev_priv(net);
1061 dev->udev = xdev;
1062 dev->driver_info = info;
1063 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1064 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1065 skb_queue_head_init (&dev->rxq);
1066 skb_queue_head_init (&dev->txq);
1067 skb_queue_head_init (&dev->done);
1068 dev->bh.func = usbnet_bh;
1069 dev->bh.data = (unsigned long) dev;
1070 INIT_WORK (&dev->kevent, kevent, dev);
1071 dev->delay.function = usbnet_bh;
1072 dev->delay.data = (unsigned long) dev;
1073 init_timer (&dev->delay);
1075 SET_MODULE_OWNER (net);
1076 dev->net = net;
1077 strcpy (net->name, "usb%d");
1078 memcpy (net->dev_addr, node_id, sizeof node_id);
1080 /* rx and tx sides can use different message sizes;
1081 * bind() should set rx_urb_size in that case.
1083 dev->hard_mtu = net->mtu + net->hard_header_len;
1084 #if 0
1085 // dma_supported() is deeply broken on almost all architectures
1086 // possible with some EHCI controllers
1087 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1088 net->features |= NETIF_F_HIGHDMA;
1089 #endif
1091 net->change_mtu = usbnet_change_mtu;
1092 net->get_stats = usbnet_get_stats;
1093 net->hard_start_xmit = usbnet_start_xmit;
1094 net->open = usbnet_open;
1095 net->stop = usbnet_stop;
1096 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1097 net->tx_timeout = usbnet_tx_timeout;
1098 net->ethtool_ops = &usbnet_ethtool_ops;
1100 // allow device-specific bind/init procedures
1101 // NOTE net->name still not usable ...
1102 if (info->bind) {
1103 status = info->bind (dev, udev);
1104 // heuristic: "usb%d" for links we know are two-host,
1105 // else "eth%d" when there's reasonable doubt. userspace
1106 // can rename the link if it knows better.
1107 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1108 && (net->dev_addr [0] & 0x02) == 0)
1109 strcpy (net->name, "eth%d");
1111 /* maybe the remote can't receive an Ethernet MTU */
1112 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1113 net->mtu = dev->hard_mtu - net->hard_header_len;
1114 } else if (!info->in || !info->out)
1115 status = usbnet_get_endpoints (dev, udev);
1116 else {
1117 dev->in = usb_rcvbulkpipe (xdev, info->in);
1118 dev->out = usb_sndbulkpipe (xdev, info->out);
1119 if (!(info->flags & FLAG_NO_SETINT))
1120 status = usb_set_interface (xdev,
1121 interface->desc.bInterfaceNumber,
1122 interface->desc.bAlternateSetting);
1123 else
1124 status = 0;
1127 if (status == 0 && dev->status)
1128 status = init_status (dev, udev);
1129 if (status < 0)
1130 goto out1;
1132 if (!dev->rx_urb_size)
1133 dev->rx_urb_size = dev->hard_mtu;
1134 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1136 SET_NETDEV_DEV(net, &udev->dev);
1137 status = register_netdev (net);
1138 if (status)
1139 goto out3;
1140 if (netif_msg_probe (dev))
1141 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1142 "%02x:%02x:%02x:%02x:%02x:%02x",
1143 udev->dev.driver->name,
1144 xdev->bus->bus_name, xdev->devpath,
1145 dev->driver_info->description,
1146 net->dev_addr [0], net->dev_addr [1],
1147 net->dev_addr [2], net->dev_addr [3],
1148 net->dev_addr [4], net->dev_addr [5]);
1150 // ok, it's ready to go.
1151 usb_set_intfdata (udev, dev);
1153 // start as if the link is up
1154 netif_device_attach (net);
1156 return 0;
1158 out3:
1159 if (info->unbind)
1160 info->unbind (dev, udev);
1161 out1:
1162 free_netdev(net);
1163 out:
1164 usb_put_dev(xdev);
1165 return status;
1167 EXPORT_SYMBOL_GPL(usbnet_probe);
1169 /*-------------------------------------------------------------------------*/
1171 /* FIXME these suspend/resume methods assume non-CDC style
1172 * devices, with only one interface.
1175 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1177 struct usbnet *dev = usb_get_intfdata(intf);
1179 /* accelerate emptying of the rx and queues, to avoid
1180 * having everything error out.
1182 netif_device_detach (dev->net);
1183 (void) unlink_urbs (dev, &dev->rxq);
1184 (void) unlink_urbs (dev, &dev->txq);
1185 return 0;
1187 EXPORT_SYMBOL_GPL(usbnet_suspend);
1189 int usbnet_resume (struct usb_interface *intf)
1191 struct usbnet *dev = usb_get_intfdata(intf);
1193 netif_device_attach (dev->net);
1194 tasklet_schedule (&dev->bh);
1195 return 0;
1197 EXPORT_SYMBOL_GPL(usbnet_resume);
1200 /*-------------------------------------------------------------------------*/
1202 static int __init usbnet_init(void)
1204 /* compiler should optimize this out */
1205 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1206 < sizeof (struct skb_data));
1208 random_ether_addr(node_id);
1209 return 0;
1211 module_init(usbnet_init);
1213 static void __exit usbnet_exit(void)
1216 module_exit(usbnet_exit);
1218 MODULE_AUTHOR("David Brownell");
1219 MODULE_DESCRIPTION("USB network driver framework");
1220 MODULE_LICENSE("GPL");