ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / net / usb / usbnet.c
blob8463efb9e0b119417b8199c78c53d6a0151ffe43
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/ethtool.h>
41 #include <linux/workqueue.h>
42 #include <linux/mii.h>
43 #include <linux/usb.h>
44 #include <linux/usb/usbnet.h>
46 #define DRIVER_VERSION "22-Aug-2005"
49 /*-------------------------------------------------------------------------*/
52 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
53 * Several dozen bytes of IPv4 data can fit in two such transactions.
54 * One maximum size Ethernet packet takes twenty four of them.
55 * For high speed, each frame comfortably fits almost 36 max size
56 * Ethernet packets (so queues should be bigger).
58 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
59 * let the USB host controller be busy for 5msec or more before an irq
60 * is required, under load. Jumbograms change the equation.
62 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
63 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
64 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
65 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
66 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 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 (!usb_endpoint_dir_in(&e->desc))
118 continue;
119 intr = 1;
120 /* FALLTHROUGH */
121 case USB_ENDPOINT_XFER_BULK:
122 break;
123 default:
124 continue;
126 if (usb_endpoint_dir_in(&e->desc)) {
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);
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, GFP_KERNEL);
181 if (buf) {
182 dev->interrupt = usb_alloc_urb (0, GFP_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->protocol = eth_type_trans (skb, dev->net);
206 dev->stats.rx_packets++;
207 dev->stats.rx_bytes += skb->len;
209 if (netif_msg_rx_status (dev))
210 devdbg (dev, "< rx, len %zu, type 0x%x",
211 skb->len + sizeof (struct ethhdr), skb->protocol);
212 memset (skb->cb, 0, sizeof (struct skb_data));
213 status = netif_rx (skb);
214 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
215 devdbg (dev, "netif_rx status %d", status);
217 EXPORT_SYMBOL_GPL(usbnet_skb_return);
220 /*-------------------------------------------------------------------------
222 * Network Device Driver (peer link to "Host Device", from USB host)
224 *-------------------------------------------------------------------------*/
226 static int usbnet_change_mtu (struct net_device *net, int new_mtu)
228 struct usbnet *dev = netdev_priv(net);
229 int ll_mtu = new_mtu + net->hard_header_len;
230 int old_hard_mtu = dev->hard_mtu;
231 int old_rx_urb_size = dev->rx_urb_size;
233 if (new_mtu <= 0)
234 return -EINVAL;
235 // no second zero-length packet read wanted after mtu-sized packets
236 if ((ll_mtu % dev->maxpacket) == 0)
237 return -EDOM;
238 net->mtu = new_mtu;
240 dev->hard_mtu = net->mtu + net->hard_header_len;
241 if (dev->rx_urb_size == old_hard_mtu) {
242 dev->rx_urb_size = dev->hard_mtu;
243 if (dev->rx_urb_size > old_rx_urb_size)
244 usbnet_unlink_rx_urbs(dev);
247 return 0;
250 /*-------------------------------------------------------------------------*/
252 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
254 struct usbnet *dev = netdev_priv(net);
255 return &dev->stats;
258 /*-------------------------------------------------------------------------*/
260 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
261 * completion callbacks. 2.5 should have fixed those bugs...
264 static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
266 unsigned long flags;
268 spin_lock_irqsave(&list->lock, flags);
269 __skb_unlink(skb, list);
270 spin_unlock(&list->lock);
271 spin_lock(&dev->done.lock);
272 __skb_queue_tail(&dev->done, skb);
273 if (dev->done.qlen == 1)
274 tasklet_schedule(&dev->bh);
275 spin_unlock_irqrestore(&dev->done.lock, flags);
278 /* some work can't be done in tasklets, so we use keventd
280 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
281 * but tasklet_schedule() doesn't. hope the failure is rare.
283 void usbnet_defer_kevent (struct usbnet *dev, int work)
285 set_bit (work, &dev->flags);
286 if (!schedule_work (&dev->kevent))
287 deverr (dev, "kevent %d may have been dropped", work);
288 else
289 devdbg (dev, "kevent %d scheduled", work);
291 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
293 /*-------------------------------------------------------------------------*/
295 static void rx_complete (struct urb *urb);
297 static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
299 struct sk_buff *skb;
300 struct skb_data *entry;
301 int retval = 0;
302 unsigned long lockflags;
303 size_t size = dev->rx_urb_size;
305 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
306 if (netif_msg_rx_err (dev))
307 devdbg (dev, "no rx skb");
308 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
309 usb_free_urb (urb);
310 return;
312 skb_reserve (skb, NET_IP_ALIGN);
314 entry = (struct skb_data *) skb->cb;
315 entry->urb = urb;
316 entry->dev = dev;
317 entry->state = rx_start;
318 entry->length = 0;
320 usb_fill_bulk_urb (urb, dev->udev, dev->in,
321 skb->data, size, rx_complete, skb);
323 spin_lock_irqsave (&dev->rxq.lock, lockflags);
325 if (netif_running (dev->net)
326 && netif_device_present (dev->net)
327 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
328 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
329 case -EPIPE:
330 usbnet_defer_kevent (dev, EVENT_RX_HALT);
331 break;
332 case -ENOMEM:
333 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
334 break;
335 case -ENODEV:
336 if (netif_msg_ifdown (dev))
337 devdbg (dev, "device gone");
338 netif_device_detach (dev->net);
339 break;
340 default:
341 if (netif_msg_rx_err (dev))
342 devdbg (dev, "rx submit, %d", retval);
343 tasklet_schedule (&dev->bh);
344 break;
345 case 0:
346 __skb_queue_tail (&dev->rxq, skb);
348 } else {
349 if (netif_msg_ifdown (dev))
350 devdbg (dev, "rx: stopped");
351 retval = -ENOLINK;
353 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
354 if (retval) {
355 dev_kfree_skb_any (skb);
356 usb_free_urb (urb);
361 /*-------------------------------------------------------------------------*/
363 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
365 if (dev->driver_info->rx_fixup
366 && !dev->driver_info->rx_fixup (dev, skb))
367 goto error;
368 // else network stack removes extra byte if we forced a short packet
370 if (skb->len)
371 usbnet_skb_return (dev, skb);
372 else {
373 if (netif_msg_rx_err (dev))
374 devdbg (dev, "drop");
375 error:
376 dev->stats.rx_errors++;
377 skb_queue_tail (&dev->done, skb);
381 /*-------------------------------------------------------------------------*/
383 static void rx_complete (struct urb *urb)
385 struct sk_buff *skb = (struct sk_buff *) urb->context;
386 struct skb_data *entry = (struct skb_data *) skb->cb;
387 struct usbnet *dev = entry->dev;
388 int urb_status = urb->status;
390 skb_put (skb, urb->actual_length);
391 entry->state = rx_done;
392 entry->urb = NULL;
394 switch (urb_status) {
395 /* success */
396 case 0:
397 if (skb->len < dev->net->hard_header_len) {
398 entry->state = rx_cleanup;
399 dev->stats.rx_errors++;
400 dev->stats.rx_length_errors++;
401 if (netif_msg_rx_err (dev))
402 devdbg (dev, "rx length %d", skb->len);
404 break;
406 /* stalls need manual reset. this is rare ... except that
407 * when going through USB 2.0 TTs, unplug appears this way.
408 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
409 * storm, recovering as needed.
411 case -EPIPE:
412 dev->stats.rx_errors++;
413 usbnet_defer_kevent (dev, EVENT_RX_HALT);
414 // FALLTHROUGH
416 /* software-driven interface shutdown */
417 case -ECONNRESET: /* async unlink */
418 case -ESHUTDOWN: /* hardware gone */
419 if (netif_msg_ifdown (dev))
420 devdbg (dev, "rx shutdown, code %d", urb_status);
421 goto block;
423 /* we get controller i/o faults during khubd disconnect() delays.
424 * throttle down resubmits, to avoid log floods; just temporarily,
425 * so we still recover when the fault isn't a khubd delay.
427 case -EPROTO:
428 case -ETIME:
429 case -EILSEQ:
430 dev->stats.rx_errors++;
431 if (!timer_pending (&dev->delay)) {
432 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
433 if (netif_msg_link (dev))
434 devdbg (dev, "rx throttle %d", urb_status);
436 block:
437 entry->state = rx_cleanup;
438 entry->urb = urb;
439 urb = NULL;
440 break;
442 /* data overrun ... flush fifo? */
443 case -EOVERFLOW:
444 dev->stats.rx_over_errors++;
445 // FALLTHROUGH
447 default:
448 entry->state = rx_cleanup;
449 dev->stats.rx_errors++;
450 if (netif_msg_rx_err (dev))
451 devdbg (dev, "rx status %d", urb_status);
452 break;
455 defer_bh(dev, skb, &dev->rxq);
457 if (urb) {
458 if (netif_running (dev->net)
459 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
460 rx_submit (dev, urb, GFP_ATOMIC);
461 return;
463 usb_free_urb (urb);
465 if (netif_msg_rx_err (dev))
466 devdbg (dev, "no read resubmitted");
469 static void intr_complete (struct urb *urb)
471 struct usbnet *dev = urb->context;
472 int status = urb->status;
474 switch (status) {
475 /* success */
476 case 0:
477 dev->driver_info->status(dev, urb);
478 break;
480 /* software-driven interface shutdown */
481 case -ENOENT: /* urb killed */
482 case -ESHUTDOWN: /* hardware gone */
483 if (netif_msg_ifdown (dev))
484 devdbg (dev, "intr shutdown, code %d", status);
485 return;
487 /* NOTE: not throttling like RX/TX, since this endpoint
488 * already polls infrequently
490 default:
491 devdbg (dev, "intr status %d", status);
492 break;
495 if (!netif_running (dev->net))
496 return;
498 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
499 status = usb_submit_urb (urb, GFP_ATOMIC);
500 if (status != 0 && netif_msg_timer (dev))
501 deverr(dev, "intr resubmit --> %d", status);
504 /*-------------------------------------------------------------------------*/
506 // unlink pending rx/tx; completion handlers do all other cleanup
508 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
510 unsigned long flags;
511 struct sk_buff *skb, *skbnext;
512 int count = 0;
514 spin_lock_irqsave (&q->lock, flags);
515 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
516 struct skb_data *entry;
517 struct urb *urb;
518 int retval;
520 entry = (struct skb_data *) skb->cb;
521 urb = entry->urb;
522 skbnext = skb->next;
524 // during some PM-driven resume scenarios,
525 // these (async) unlinks complete immediately
526 retval = usb_unlink_urb (urb);
527 if (retval != -EINPROGRESS && retval != 0)
528 devdbg (dev, "unlink urb err, %d", retval);
529 else
530 count++;
532 spin_unlock_irqrestore (&q->lock, flags);
533 return count;
536 // Flush all pending rx urbs
537 // minidrivers may need to do this when the MTU changes
539 void usbnet_unlink_rx_urbs(struct usbnet *dev)
541 if (netif_running(dev->net)) {
542 (void) unlink_urbs (dev, &dev->rxq);
543 tasklet_schedule(&dev->bh);
546 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
548 /*-------------------------------------------------------------------------*/
550 // precondition: never called in_interrupt
552 static int usbnet_stop (struct net_device *net)
554 struct usbnet *dev = netdev_priv(net);
555 int temp;
556 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
557 DECLARE_WAITQUEUE (wait, current);
559 netif_stop_queue (net);
561 if (netif_msg_ifdown (dev))
562 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
563 dev->stats.rx_packets, dev->stats.tx_packets,
564 dev->stats.rx_errors, dev->stats.tx_errors
567 // ensure there are no more active urbs
568 add_wait_queue (&unlink_wakeup, &wait);
569 dev->wait = &unlink_wakeup;
570 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
572 // maybe wait for deletions to finish.
573 while (!skb_queue_empty(&dev->rxq)
574 && !skb_queue_empty(&dev->txq)
575 && !skb_queue_empty(&dev->done)) {
576 msleep(UNLINK_TIMEOUT_MS);
577 if (netif_msg_ifdown (dev))
578 devdbg (dev, "waited for %d urb completions", temp);
580 dev->wait = NULL;
581 remove_wait_queue (&unlink_wakeup, &wait);
583 usb_kill_urb(dev->interrupt);
585 /* deferred work (task, timer, softirq) must also stop.
586 * can't flush_scheduled_work() until we drop rtnl (later),
587 * else workers could deadlock; so make workers a NOP.
589 dev->flags = 0;
590 del_timer_sync (&dev->delay);
591 tasklet_kill (&dev->bh);
592 usb_autopm_put_interface(dev->intf);
594 return 0;
597 /*-------------------------------------------------------------------------*/
599 // posts reads, and enables write queuing
601 // precondition: never called in_interrupt
603 static int usbnet_open (struct net_device *net)
605 struct usbnet *dev = netdev_priv(net);
606 int retval;
607 struct driver_info *info = dev->driver_info;
609 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
610 if (netif_msg_ifup (dev))
611 devinfo (dev,
612 "resumption fail (%d) usbnet usb-%s-%s, %s",
613 retval,
614 dev->udev->bus->bus_name, dev->udev->devpath,
615 info->description);
616 goto done_nopm;
619 // put into "known safe" state
620 if (info->reset && (retval = info->reset (dev)) < 0) {
621 if (netif_msg_ifup (dev))
622 devinfo (dev,
623 "open reset fail (%d) usbnet usb-%s-%s, %s",
624 retval,
625 dev->udev->bus->bus_name, dev->udev->devpath,
626 info->description);
627 goto done;
630 // insist peer be connected
631 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
632 if (netif_msg_ifup (dev))
633 devdbg (dev, "can't open; %d", retval);
634 goto done;
637 /* start any status interrupt transfer */
638 if (dev->interrupt) {
639 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
640 if (retval < 0) {
641 if (netif_msg_ifup (dev))
642 deverr (dev, "intr submit %d", retval);
643 goto done;
647 netif_start_queue (net);
648 if (netif_msg_ifup (dev)) {
649 char *framing;
651 if (dev->driver_info->flags & FLAG_FRAMING_NC)
652 framing = "NetChip";
653 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
654 framing = "GeneSys";
655 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
656 framing = "Zaurus";
657 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
658 framing = "RNDIS";
659 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
660 framing = "ASIX";
661 else
662 framing = "simple";
664 devinfo (dev, "open: enable queueing "
665 "(rx %d, tx %d) mtu %d %s framing",
666 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
667 framing);
670 // delay posting reads until we're fully open
671 tasklet_schedule (&dev->bh);
672 return retval;
673 done:
674 usb_autopm_put_interface(dev->intf);
675 done_nopm:
676 return retval;
679 /*-------------------------------------------------------------------------*/
681 /* ethtool methods; minidrivers may need to add some more, but
682 * they'll probably want to use this base set.
685 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
687 struct usbnet *dev = netdev_priv(net);
689 if (!dev->mii.mdio_read)
690 return -EOPNOTSUPP;
692 return mii_ethtool_gset(&dev->mii, cmd);
694 EXPORT_SYMBOL_GPL(usbnet_get_settings);
696 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
698 struct usbnet *dev = netdev_priv(net);
699 int retval;
701 if (!dev->mii.mdio_write)
702 return -EOPNOTSUPP;
704 retval = mii_ethtool_sset(&dev->mii, cmd);
706 /* link speed/duplex might have changed */
707 if (dev->driver_info->link_reset)
708 dev->driver_info->link_reset(dev);
710 return retval;
713 EXPORT_SYMBOL_GPL(usbnet_set_settings);
715 u32 usbnet_get_link (struct net_device *net)
717 struct usbnet *dev = netdev_priv(net);
719 /* If a check_connect is defined, return its result */
720 if (dev->driver_info->check_connect)
721 return dev->driver_info->check_connect (dev) == 0;
723 /* if the device has mii operations, use those */
724 if (dev->mii.mdio_read)
725 return mii_link_ok(&dev->mii);
727 /* Otherwise, say we're up (to avoid breaking scripts) */
728 return 1;
730 EXPORT_SYMBOL_GPL(usbnet_get_link);
732 int usbnet_nway_reset(struct net_device *net)
734 struct usbnet *dev = netdev_priv(net);
736 if (!dev->mii.mdio_write)
737 return -EOPNOTSUPP;
739 return mii_nway_restart(&dev->mii);
741 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
743 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
745 struct usbnet *dev = netdev_priv(net);
747 strncpy (info->driver, dev->driver_name, sizeof info->driver);
748 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
749 strncpy (info->fw_version, dev->driver_info->description,
750 sizeof info->fw_version);
751 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
753 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
755 u32 usbnet_get_msglevel (struct net_device *net)
757 struct usbnet *dev = netdev_priv(net);
759 return dev->msg_enable;
761 EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
763 void usbnet_set_msglevel (struct net_device *net, u32 level)
765 struct usbnet *dev = netdev_priv(net);
767 dev->msg_enable = level;
769 EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
771 /* drivers may override default ethtool_ops in their bind() routine */
772 static struct ethtool_ops usbnet_ethtool_ops = {
773 .get_settings = usbnet_get_settings,
774 .set_settings = usbnet_set_settings,
775 .get_link = usbnet_get_link,
776 .nway_reset = usbnet_nway_reset,
777 .get_drvinfo = usbnet_get_drvinfo,
778 .get_msglevel = usbnet_get_msglevel,
779 .set_msglevel = usbnet_set_msglevel,
782 /*-------------------------------------------------------------------------*/
784 /* work that cannot be done in interrupt context uses keventd.
786 * NOTE: with 2.5 we could do more of this using completion callbacks,
787 * especially now that control transfers can be queued.
789 static void
790 kevent (struct work_struct *work)
792 struct usbnet *dev =
793 container_of(work, struct usbnet, kevent);
794 int status;
796 /* usb_clear_halt() needs a thread context */
797 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
798 unlink_urbs (dev, &dev->txq);
799 status = usb_clear_halt (dev->udev, dev->out);
800 if (status < 0
801 && status != -EPIPE
802 && status != -ESHUTDOWN) {
803 if (netif_msg_tx_err (dev))
804 deverr (dev, "can't clear tx halt, status %d",
805 status);
806 } else {
807 clear_bit (EVENT_TX_HALT, &dev->flags);
808 if (status != -ESHUTDOWN)
809 netif_wake_queue (dev->net);
812 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
813 unlink_urbs (dev, &dev->rxq);
814 status = usb_clear_halt (dev->udev, dev->in);
815 if (status < 0
816 && status != -EPIPE
817 && status != -ESHUTDOWN) {
818 if (netif_msg_rx_err (dev))
819 deverr (dev, "can't clear rx halt, status %d",
820 status);
821 } else {
822 clear_bit (EVENT_RX_HALT, &dev->flags);
823 tasklet_schedule (&dev->bh);
827 /* tasklet could resubmit itself forever if memory is tight */
828 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
829 struct urb *urb = NULL;
831 if (netif_running (dev->net))
832 urb = usb_alloc_urb (0, GFP_KERNEL);
833 else
834 clear_bit (EVENT_RX_MEMORY, &dev->flags);
835 if (urb != NULL) {
836 clear_bit (EVENT_RX_MEMORY, &dev->flags);
837 rx_submit (dev, urb, GFP_KERNEL);
838 tasklet_schedule (&dev->bh);
842 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
843 struct driver_info *info = dev->driver_info;
844 int retval = 0;
846 clear_bit (EVENT_LINK_RESET, &dev->flags);
847 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
848 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
849 retval,
850 dev->udev->bus->bus_name, dev->udev->devpath,
851 info->description);
855 if (dev->flags)
856 devdbg (dev, "kevent done, flags = 0x%lx",
857 dev->flags);
860 /*-------------------------------------------------------------------------*/
862 static void tx_complete (struct urb *urb)
864 struct sk_buff *skb = (struct sk_buff *) urb->context;
865 struct skb_data *entry = (struct skb_data *) skb->cb;
866 struct usbnet *dev = entry->dev;
868 if (urb->status == 0) {
869 dev->stats.tx_packets++;
870 dev->stats.tx_bytes += entry->length;
871 } else {
872 dev->stats.tx_errors++;
874 switch (urb->status) {
875 case -EPIPE:
876 usbnet_defer_kevent (dev, EVENT_TX_HALT);
877 break;
879 /* software-driven interface shutdown */
880 case -ECONNRESET: // async unlink
881 case -ESHUTDOWN: // hardware gone
882 break;
884 // like rx, tx gets controller i/o faults during khubd delays
885 // and so it uses the same throttling mechanism.
886 case -EPROTO:
887 case -ETIME:
888 case -EILSEQ:
889 if (!timer_pending (&dev->delay)) {
890 mod_timer (&dev->delay,
891 jiffies + THROTTLE_JIFFIES);
892 if (netif_msg_link (dev))
893 devdbg (dev, "tx throttle %d",
894 urb->status);
896 netif_stop_queue (dev->net);
897 break;
898 default:
899 if (netif_msg_tx_err (dev))
900 devdbg (dev, "tx err %d", entry->urb->status);
901 break;
905 urb->dev = NULL;
906 entry->state = tx_done;
907 defer_bh(dev, skb, &dev->txq);
910 /*-------------------------------------------------------------------------*/
912 static void usbnet_tx_timeout (struct net_device *net)
914 struct usbnet *dev = netdev_priv(net);
916 unlink_urbs (dev, &dev->txq);
917 tasklet_schedule (&dev->bh);
919 // FIXME: device recovery -- reset?
922 /*-------------------------------------------------------------------------*/
924 static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
926 struct usbnet *dev = netdev_priv(net);
927 int length;
928 int retval = NET_XMIT_SUCCESS;
929 struct urb *urb = NULL;
930 struct skb_data *entry;
931 struct driver_info *info = dev->driver_info;
932 unsigned long flags;
934 // some devices want funky USB-level framing, for
935 // win32 driver (usually) and/or hardware quirks
936 if (info->tx_fixup) {
937 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
938 if (!skb) {
939 if (netif_msg_tx_err (dev))
940 devdbg (dev, "can't tx_fixup skb");
941 goto drop;
944 length = skb->len;
946 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
947 if (netif_msg_tx_err (dev))
948 devdbg (dev, "no urb");
949 goto drop;
952 entry = (struct skb_data *) skb->cb;
953 entry->urb = urb;
954 entry->dev = dev;
955 entry->state = tx_start;
956 entry->length = length;
958 usb_fill_bulk_urb (urb, dev->udev, dev->out,
959 skb->data, skb->len, tx_complete, skb);
961 /* don't assume the hardware handles USB_ZERO_PACKET
962 * NOTE: strictly conforming cdc-ether devices should expect
963 * the ZLP here, but ignore the one-byte packet.
965 if ((length % dev->maxpacket) == 0) {
966 urb->transfer_buffer_length++;
967 if (skb_tailroom(skb)) {
968 skb->data[skb->len] = 0;
969 __skb_put(skb, 1);
973 spin_lock_irqsave (&dev->txq.lock, flags);
975 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
976 case -EPIPE:
977 netif_stop_queue (net);
978 usbnet_defer_kevent (dev, EVENT_TX_HALT);
979 break;
980 default:
981 if (netif_msg_tx_err (dev))
982 devdbg (dev, "tx: submit urb err %d", retval);
983 break;
984 case 0:
985 net->trans_start = jiffies;
986 __skb_queue_tail (&dev->txq, skb);
987 if (dev->txq.qlen >= TX_QLEN (dev))
988 netif_stop_queue (net);
990 spin_unlock_irqrestore (&dev->txq.lock, flags);
992 if (retval) {
993 if (netif_msg_tx_err (dev))
994 devdbg (dev, "drop, code %d", retval);
995 drop:
996 retval = NET_XMIT_SUCCESS;
997 dev->stats.tx_dropped++;
998 if (skb)
999 dev_kfree_skb_any (skb);
1000 usb_free_urb (urb);
1001 } else if (netif_msg_tx_queued (dev)) {
1002 devdbg (dev, "> tx, len %d, type 0x%x",
1003 length, skb->protocol);
1005 return retval;
1009 /*-------------------------------------------------------------------------*/
1011 // tasklet (work deferred from completions, in_irq) or timer
1013 static void usbnet_bh (unsigned long param)
1015 struct usbnet *dev = (struct usbnet *) param;
1016 struct sk_buff *skb;
1017 struct skb_data *entry;
1019 while ((skb = skb_dequeue (&dev->done))) {
1020 entry = (struct skb_data *) skb->cb;
1021 switch (entry->state) {
1022 case rx_done:
1023 entry->state = rx_cleanup;
1024 rx_process (dev, skb);
1025 continue;
1026 case tx_done:
1027 case rx_cleanup:
1028 usb_free_urb (entry->urb);
1029 dev_kfree_skb (skb);
1030 continue;
1031 default:
1032 devdbg (dev, "bogus skb state %d", entry->state);
1036 // waiting for all pending urbs to complete?
1037 if (dev->wait) {
1038 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1039 wake_up (dev->wait);
1042 // or are we maybe short a few urbs?
1043 } else if (netif_running (dev->net)
1044 && netif_device_present (dev->net)
1045 && !timer_pending (&dev->delay)
1046 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1047 int temp = dev->rxq.qlen;
1048 int qlen = RX_QLEN (dev);
1050 if (temp < qlen) {
1051 struct urb *urb;
1052 int i;
1054 // don't refill the queue all at once
1055 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1056 urb = usb_alloc_urb (0, GFP_ATOMIC);
1057 if (urb != NULL)
1058 rx_submit (dev, urb, GFP_ATOMIC);
1060 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1061 devdbg (dev, "rxqlen %d --> %d",
1062 temp, dev->rxq.qlen);
1063 if (dev->rxq.qlen < qlen)
1064 tasklet_schedule (&dev->bh);
1066 if (dev->txq.qlen < TX_QLEN (dev))
1067 netif_wake_queue (dev->net);
1073 /*-------------------------------------------------------------------------
1075 * USB Device Driver support
1077 *-------------------------------------------------------------------------*/
1079 // precondition: never called in_interrupt
1081 void usbnet_disconnect (struct usb_interface *intf)
1083 struct usbnet *dev;
1084 struct usb_device *xdev;
1085 struct net_device *net;
1087 dev = usb_get_intfdata(intf);
1088 usb_set_intfdata(intf, NULL);
1089 if (!dev)
1090 return;
1092 xdev = interface_to_usbdev (intf);
1094 if (netif_msg_probe (dev))
1095 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1096 intf->dev.driver->name,
1097 xdev->bus->bus_name, xdev->devpath,
1098 dev->driver_info->description);
1100 net = dev->net;
1101 unregister_netdev (net);
1103 /* we don't hold rtnl here ... */
1104 flush_scheduled_work ();
1106 if (dev->driver_info->unbind)
1107 dev->driver_info->unbind (dev, intf);
1109 free_netdev(net);
1110 usb_put_dev (xdev);
1112 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1115 /*-------------------------------------------------------------------------*/
1117 // precondition: never called in_interrupt
1120 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1122 struct usbnet *dev;
1123 struct net_device *net;
1124 struct usb_host_interface *interface;
1125 struct driver_info *info;
1126 struct usb_device *xdev;
1127 int status;
1128 const char *name;
1129 DECLARE_MAC_BUF(mac);
1131 name = udev->dev.driver->name;
1132 info = (struct driver_info *) prod->driver_info;
1133 if (!info) {
1134 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1135 return -ENODEV;
1137 xdev = interface_to_usbdev (udev);
1138 interface = udev->cur_altsetting;
1140 usb_get_dev (xdev);
1142 status = -ENOMEM;
1144 // set up our own records
1145 net = alloc_etherdev(sizeof(*dev));
1146 if (!net) {
1147 dbg ("can't kmalloc dev");
1148 goto out;
1151 dev = netdev_priv(net);
1152 dev->udev = xdev;
1153 dev->intf = udev;
1154 dev->driver_info = info;
1155 dev->driver_name = name;
1156 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1157 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1158 skb_queue_head_init (&dev->rxq);
1159 skb_queue_head_init (&dev->txq);
1160 skb_queue_head_init (&dev->done);
1161 dev->bh.func = usbnet_bh;
1162 dev->bh.data = (unsigned long) dev;
1163 INIT_WORK (&dev->kevent, kevent);
1164 dev->delay.function = usbnet_bh;
1165 dev->delay.data = (unsigned long) dev;
1166 init_timer (&dev->delay);
1167 mutex_init (&dev->phy_mutex);
1169 dev->net = net;
1170 strcpy (net->name, "usb%d");
1171 memcpy (net->dev_addr, node_id, sizeof node_id);
1173 /* rx and tx sides can use different message sizes;
1174 * bind() should set rx_urb_size in that case.
1176 dev->hard_mtu = net->mtu + net->hard_header_len;
1177 #if 0
1178 // dma_supported() is deeply broken on almost all architectures
1179 // possible with some EHCI controllers
1180 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1181 net->features |= NETIF_F_HIGHDMA;
1182 #endif
1184 net->change_mtu = usbnet_change_mtu;
1185 net->get_stats = usbnet_get_stats;
1186 net->hard_start_xmit = usbnet_start_xmit;
1187 net->open = usbnet_open;
1188 net->stop = usbnet_stop;
1189 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1190 net->tx_timeout = usbnet_tx_timeout;
1191 net->ethtool_ops = &usbnet_ethtool_ops;
1193 // allow device-specific bind/init procedures
1194 // NOTE net->name still not usable ...
1195 if (info->bind) {
1196 status = info->bind (dev, udev);
1197 if (status < 0)
1198 goto out1;
1200 // heuristic: "usb%d" for links we know are two-host,
1201 // else "eth%d" when there's reasonable doubt. userspace
1202 // can rename the link if it knows better.
1203 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1204 && (net->dev_addr [0] & 0x02) == 0)
1205 strcpy (net->name, "eth%d");
1206 /* WLAN devices should always be named "wlan%d" */
1207 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1208 strcpy(net->name, "wlan%d");
1210 /* maybe the remote can't receive an Ethernet MTU */
1211 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1212 net->mtu = dev->hard_mtu - net->hard_header_len;
1213 } else if (!info->in || !info->out)
1214 status = usbnet_get_endpoints (dev, udev);
1215 else {
1216 dev->in = usb_rcvbulkpipe (xdev, info->in);
1217 dev->out = usb_sndbulkpipe (xdev, info->out);
1218 if (!(info->flags & FLAG_NO_SETINT))
1219 status = usb_set_interface (xdev,
1220 interface->desc.bInterfaceNumber,
1221 interface->desc.bAlternateSetting);
1222 else
1223 status = 0;
1226 if (status >= 0 && dev->status)
1227 status = init_status (dev, udev);
1228 if (status < 0)
1229 goto out3;
1231 if (!dev->rx_urb_size)
1232 dev->rx_urb_size = dev->hard_mtu;
1233 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1235 SET_NETDEV_DEV(net, &udev->dev);
1236 status = register_netdev (net);
1237 if (status)
1238 goto out3;
1239 if (netif_msg_probe (dev))
1240 devinfo (dev, "register '%s' at usb-%s-%s, %s, %s",
1241 udev->dev.driver->name,
1242 xdev->bus->bus_name, xdev->devpath,
1243 dev->driver_info->description,
1244 print_mac(mac, net->dev_addr));
1246 // ok, it's ready to go.
1247 usb_set_intfdata (udev, dev);
1249 // start as if the link is up
1250 netif_device_attach (net);
1252 return 0;
1254 out3:
1255 if (info->unbind)
1256 info->unbind (dev, udev);
1257 out1:
1258 free_netdev(net);
1259 out:
1260 usb_put_dev(xdev);
1261 return status;
1263 EXPORT_SYMBOL_GPL(usbnet_probe);
1265 /*-------------------------------------------------------------------------*/
1268 * suspend the whole driver as soon as the first interface is suspended
1269 * resume only when the last interface is resumed
1272 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1274 struct usbnet *dev = usb_get_intfdata(intf);
1276 if (!dev->suspend_count++) {
1278 * accelerate emptying of the rx and queues, to avoid
1279 * having everything error out.
1281 netif_device_detach (dev->net);
1282 (void) unlink_urbs (dev, &dev->rxq);
1283 (void) unlink_urbs (dev, &dev->txq);
1285 * reattach so runtime management can use and
1286 * wake the device
1288 netif_device_attach (dev->net);
1290 return 0;
1292 EXPORT_SYMBOL_GPL(usbnet_suspend);
1294 int usbnet_resume (struct usb_interface *intf)
1296 struct usbnet *dev = usb_get_intfdata(intf);
1298 if (!--dev->suspend_count)
1299 tasklet_schedule (&dev->bh);
1301 return 0;
1303 EXPORT_SYMBOL_GPL(usbnet_resume);
1306 /*-------------------------------------------------------------------------*/
1308 static int __init usbnet_init(void)
1310 /* compiler should optimize this out */
1311 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1312 < sizeof (struct skb_data));
1314 random_ether_addr(node_id);
1315 return 0;
1317 module_init(usbnet_init);
1319 static void __exit usbnet_exit(void)
1322 module_exit(usbnet_exit);
1324 MODULE_AUTHOR("David Brownell");
1325 MODULE_DESCRIPTION("USB network driver framework");
1326 MODULE_LICENSE("GPL");