update/add new 3G modem modules
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / net / usb / usbnet.c
blob547ff410123c9815d728ca83018637157b5b41f8
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>
46 #include "usbnet.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->interrupt->transfer_flags |= URB_FREE_BUFFER;
222 dev_dbg(&intf->dev,
223 "status ep%din, %d bytes period %d\n",
224 usb_pipeendpoint(pipe), maxp, period);
227 return 0;
230 /* Passes this packet up the stack, updating its accounting.
231 * Some link protocols batch packets, so their rx_fixup paths
232 * can return clones as well as just modify the original skb.
234 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
236 int status;
238 skb->protocol = eth_type_trans (skb, dev->net);
239 dev->stats.rx_packets++;
240 dev->stats.rx_bytes += skb->len;
242 if (netif_msg_rx_status (dev))
243 devdbg (dev, "< rx, len %zu, type 0x%x",
244 skb->len + sizeof (struct ethhdr), skb->protocol);
245 memset (skb->cb, 0, sizeof (struct skb_data));
246 status = netif_rx (skb);
247 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
248 devdbg (dev, "netif_rx status %d", status);
250 EXPORT_SYMBOL_GPL(usbnet_skb_return);
253 /*-------------------------------------------------------------------------
255 * Network Device Driver (peer link to "Host Device", from USB host)
257 *-------------------------------------------------------------------------*/
259 int usbnet_change_mtu (struct net_device *net, int new_mtu)
261 struct usbnet *dev = netdev_priv(net);
262 int ll_mtu = new_mtu + net->hard_header_len;
263 int old_hard_mtu = dev->hard_mtu;
264 int old_rx_urb_size = dev->rx_urb_size;
266 if (new_mtu <= 0)
267 return -EINVAL;
268 // no second zero-length packet read wanted after mtu-sized packets
269 if ((ll_mtu % dev->maxpacket) == 0)
270 return -EDOM;
271 net->mtu = new_mtu;
273 dev->hard_mtu = net->mtu + net->hard_header_len;
274 if (dev->rx_urb_size == old_hard_mtu) {
275 dev->rx_urb_size = dev->hard_mtu;
276 if (dev->rx_urb_size > old_rx_urb_size)
277 usbnet_unlink_rx_urbs(dev);
280 return 0;
282 EXPORT_SYMBOL_GPL(usbnet_change_mtu);
284 /*-------------------------------------------------------------------------*/
286 static struct net_device_stats *usbnet_get_stats (struct net_device *net)
288 struct usbnet *dev = netdev_priv(net);
289 return &dev->stats;
292 /* The caller must hold list->lock */
293 static void __usbnet_queue_skb(struct sk_buff_head *list,
294 struct sk_buff *newsk, enum skb_state state)
296 struct skb_data *entry = (struct skb_data *) newsk->cb;
298 __skb_queue_tail(list, newsk);
299 entry->state = state;
302 /*-------------------------------------------------------------------------*/
304 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
305 * completion callbacks. 2.5 should have fixed those bugs...
308 static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
309 struct sk_buff_head *list, enum skb_state state)
311 unsigned long flags;
312 enum skb_state old_state;
313 struct skb_data *entry = (struct skb_data *) skb->cb;
315 spin_lock_irqsave(&list->lock, flags);
316 old_state = entry->state;
317 entry->state = state;
318 __skb_unlink(skb, list);
319 spin_unlock(&list->lock);
320 spin_lock(&dev->done.lock);
321 __skb_queue_tail(&dev->done, skb);
322 if (dev->done.qlen == 1)
323 tasklet_schedule(&dev->bh);
324 spin_unlock_irqrestore(&dev->done.lock, flags);
325 return old_state;
328 /* some work can't be done in tasklets, so we use keventd
330 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
331 * but tasklet_schedule() doesn't. hope the failure is rare.
333 void usbnet_defer_kevent (struct usbnet *dev, int work)
335 set_bit (work, &dev->flags);
336 if (!schedule_work (&dev->kevent)) {
337 if (net_ratelimit())
338 deverr (dev, "kevent %d may have been dropped", work);
339 } else {
340 devdbg (dev, "kevent %d scheduled", work);
343 EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
345 /*-------------------------------------------------------------------------*/
347 static void rx_complete (struct urb *urb);
349 static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
351 struct sk_buff *skb;
352 struct skb_data *entry;
353 int retval = 0;
354 unsigned long lockflags;
355 size_t size = dev->rx_urb_size;
357 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
358 if (netif_msg_rx_err (dev))
359 devdbg (dev, "no rx skb");
360 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
361 usb_free_urb (urb);
362 return -ENOMEM;
364 skb_reserve (skb, NET_IP_ALIGN);
366 entry = (struct skb_data *) skb->cb;
367 entry->urb = urb;
368 entry->dev = dev;
369 entry->length = 0;
371 usb_fill_bulk_urb (urb, dev->udev, dev->in,
372 skb->data, size, rx_complete, skb);
374 spin_lock_irqsave (&dev->rxq.lock, lockflags);
376 if (netif_running (dev->net)
377 && netif_device_present (dev->net)
378 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
379 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
380 case -EPIPE:
381 usbnet_defer_kevent (dev, EVENT_RX_HALT);
382 break;
383 case -ENOMEM:
384 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
385 break;
386 case -ENODEV:
387 if (netif_msg_ifdown (dev))
388 devdbg (dev, "device gone");
389 netif_device_detach (dev->net);
390 break;
391 case -EHOSTUNREACH:
392 retval = -ENOLINK;
393 break;
394 default:
395 if (netif_msg_rx_err (dev))
396 devdbg (dev, "rx submit, %d", retval);
397 tasklet_schedule (&dev->bh);
398 break;
399 case 0:
400 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
402 } else {
403 if (netif_msg_ifdown (dev))
404 devdbg (dev, "rx: stopped");
405 retval = -ENOLINK;
407 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
408 if (retval) {
409 dev_kfree_skb_any (skb);
410 usb_free_urb (urb);
412 return retval;
416 /*-------------------------------------------------------------------------*/
418 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
420 if (dev->driver_info->rx_fixup
421 && !dev->driver_info->rx_fixup (dev, skb))
422 goto error;
423 // else network stack removes extra byte if we forced a short packet
425 if (skb->len) {
426 /* all data was already cloned from skb inside the driver */
427 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
428 dev_kfree_skb_any(skb);
429 else
430 usbnet_skb_return(dev, skb);
431 return;
434 if (netif_msg_rx_err (dev))
435 devdbg (dev, "drop");
436 error:
437 dev->stats.rx_errors++;
438 skb_queue_tail(&dev->done, skb);
441 /*-------------------------------------------------------------------------*/
443 static void rx_complete (struct urb *urb)
445 struct sk_buff *skb = (struct sk_buff *) urb->context;
446 struct skb_data *entry = (struct skb_data *) skb->cb;
447 struct usbnet *dev = entry->dev;
448 int urb_status = urb->status;
449 enum skb_state state;
451 skb_put (skb, urb->actual_length);
452 state = rx_done;
453 entry->urb = NULL;
455 switch (urb_status) {
456 // success
457 case 0:
458 if (skb->len < dev->net->hard_header_len) {
459 state = rx_cleanup;
460 dev->stats.rx_errors++;
461 dev->stats.rx_length_errors++;
462 if (netif_msg_rx_err (dev))
463 devdbg (dev, "rx length %d", skb->len);
465 break;
467 // stalls need manual reset. this is rare ... except that
468 // when going through USB 2.0 TTs, unplug appears this way.
469 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
470 // storm, recovering as needed.
471 case -EPIPE:
472 dev->stats.rx_errors++;
473 usbnet_defer_kevent (dev, EVENT_RX_HALT);
474 // FALLTHROUGH
476 // software-driven interface shutdown
477 case -ECONNRESET: // async unlink
478 case -ESHUTDOWN: // hardware gone
479 if (netif_msg_ifdown (dev))
480 devdbg (dev, "rx shutdown, code %d", urb_status);
481 goto block;
483 // we get controller i/o faults during khubd disconnect() delays.
484 // throttle down resubmits, to avoid log floods; just temporarily,
485 // so we still recover when the fault isn't a khubd delay.
486 case -EPROTO:
487 case -ETIME:
488 case -EILSEQ:
489 dev->stats.rx_errors++;
490 if (!timer_pending (&dev->delay)) {
491 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
492 if (netif_msg_link (dev))
493 devdbg (dev, "rx throttle %d", urb_status);
495 block:
496 state = rx_cleanup;
497 entry->urb = urb;
498 urb = NULL;
499 break;
501 // data overrun ... flush fifo?
502 case -EOVERFLOW:
503 dev->stats.rx_over_errors++;
504 // FALLTHROUGH
506 default:
507 state = rx_cleanup;
508 dev->stats.rx_errors++;
509 if (netif_msg_rx_err (dev))
510 devdbg (dev, "rx status %d", urb_status);
511 break;
514 state = defer_bh(dev, skb, &dev->rxq, state);
516 if (urb) {
517 if (netif_running (dev->net)
518 && !test_bit (EVENT_RX_HALT, &dev->flags) &&
519 state != unlink_start) {
520 rx_submit (dev, urb, GFP_ATOMIC);
521 usb_mark_last_busy(dev->udev);
522 return;
524 usb_free_urb (urb);
526 if (netif_msg_rx_err (dev))
527 devdbg (dev, "no read resubmitted");
530 static void intr_complete (struct urb *urb)
532 struct usbnet *dev = urb->context;
533 int status = urb->status;
535 switch (status) {
536 /* success */
537 case 0:
538 dev->driver_info->status(dev, urb);
539 break;
541 /* software-driven interface shutdown */
542 case -ENOENT: // urb killed
543 case -ESHUTDOWN: // hardware gone
544 if (netif_msg_ifdown (dev))
545 devdbg (dev, "intr shutdown, code %d", status);
546 return;
548 /* NOTE: not throttling like RX/TX, since this endpoint
549 * already polls infrequently
551 default:
552 devdbg (dev, "intr status %d", status);
553 break;
556 if (!netif_running (dev->net))
557 return;
559 status = usb_submit_urb (urb, GFP_ATOMIC);
560 if (status != 0 && netif_msg_timer (dev))
561 deverr(dev, "intr resubmit --> %d", status);
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;
572 int count = 0;
574 spin_lock_irqsave (&q->lock, flags);
575 while (!skb_queue_empty(q)) {
576 struct skb_data *entry;
577 struct urb *urb;
578 int retval;
580 skb_queue_walk(q, skb) {
581 entry = (struct skb_data *) skb->cb;
582 if (entry->state != unlink_start)
583 goto found;
585 break;
586 found:
587 entry->state = unlink_start;
588 urb = entry->urb;
591 * Get reference count of the URB to avoid it to be
592 * freed during usb_unlink_urb, which may trigger
593 * use-after-free problem inside usb_unlink_urb since
594 * usb_unlink_urb is always racing with .complete
595 * handler(include defer_bh).
597 usb_get_urb(urb);
598 spin_unlock_irqrestore(&q->lock, flags);
599 // during some PM-driven resume scenarios,
600 // these (async) unlinks complete immediately
601 retval = usb_unlink_urb (urb);
602 if (retval != -EINPROGRESS && retval != 0)
603 devdbg (dev, "unlink urb err, %d", retval);
604 else
605 count++;
606 usb_put_urb(urb);
607 spin_lock_irqsave(&q->lock, flags);
609 spin_unlock_irqrestore (&q->lock, flags);
610 return count;
613 // Flush all pending rx urbs
614 // minidrivers may need to do this when the MTU changes
616 void usbnet_unlink_rx_urbs(struct usbnet *dev)
618 if (netif_running(dev->net)) {
619 (void) unlink_urbs (dev, &dev->rxq);
620 tasklet_schedule(&dev->bh);
623 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
625 /*-------------------------------------------------------------------------*/
627 // precondition: never called in_interrupt
629 int usbnet_stop (struct net_device *net)
631 struct usbnet *dev = netdev_priv(net);
632 int temp;
633 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
634 DECLARE_WAITQUEUE (wait, current);
636 netif_stop_queue (net);
638 if (netif_msg_ifdown (dev))
639 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
640 dev->stats.rx_packets, dev->stats.tx_packets,
641 dev->stats.rx_errors, dev->stats.tx_errors
644 // ensure there are no more active urbs
645 add_wait_queue (&unlink_wakeup, &wait);
646 dev->wait = &unlink_wakeup;
647 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
649 // maybe wait for deletions to finish.
650 while (!skb_queue_empty(&dev->rxq) &&
651 !skb_queue_empty(&dev->txq) &&
652 !skb_queue_empty(&dev->done)) {
653 msleep(UNLINK_TIMEOUT_MS);
654 if (netif_msg_ifdown (dev))
655 devdbg (dev, "waited for %d urb completions", temp);
657 dev->wait = NULL;
658 remove_wait_queue (&unlink_wakeup, &wait);
660 usb_kill_urb(dev->interrupt);
662 /* deferred work (task, timer, softirq) must also stop.
663 * can't flush_scheduled_work() until we drop rtnl (later),
664 * else workers could deadlock; so make workers a NOP.
666 dev->flags = 0;
667 del_timer_sync (&dev->delay);
668 tasklet_kill (&dev->bh);
669 usb_autopm_put_interface(dev->intf);
671 return 0;
673 EXPORT_SYMBOL_GPL(usbnet_stop);
675 /*-------------------------------------------------------------------------*/
677 // posts reads, and enables write queuing
679 // precondition: never called in_interrupt
681 int usbnet_open (struct net_device *net)
683 struct usbnet *dev = netdev_priv(net);
684 int retval;
685 struct driver_info *info = dev->driver_info;
687 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
688 if (netif_msg_ifup (dev))
689 devinfo (dev,
690 "resumption fail (%d) usbnet usb-%s-%s, %s",
691 retval,
692 dev->udev->bus->bus_name, dev->udev->devpath,
693 info->description);
694 goto done_nopm;
697 // put into "known safe" state
698 if (info->reset && (retval = info->reset (dev)) < 0) {
699 if (netif_msg_ifup (dev))
700 devinfo (dev,
701 "open reset fail (%d) usbnet usb-%s-%s, %s",
702 retval,
703 dev->udev->bus->bus_name, dev->udev->devpath,
704 info->description);
705 goto done;
708 // insist peer be connected
709 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
710 if (netif_msg_ifup (dev))
711 devdbg (dev, "can't open; %d", retval);
712 goto done;
715 /* start any status interrupt transfer */
716 if (dev->interrupt) {
717 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
718 if (retval < 0) {
719 if (netif_msg_ifup (dev))
720 deverr (dev, "intr submit %d", retval);
721 goto done;
725 netif_start_queue (net);
726 if (netif_msg_ifup (dev)) {
727 char *framing;
729 if (dev->driver_info->flags & FLAG_FRAMING_NC)
730 framing = "NetChip";
731 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
732 framing = "GeneSys";
733 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
734 framing = "Zaurus";
735 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
736 framing = "RNDIS";
737 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
738 framing = "ASIX";
739 else
740 framing = "simple";
742 devinfo (dev, "open: enable queueing "
743 "(rx %d, tx %d) mtu %d %s framing",
744 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
745 framing);
748 // delay posting reads until we're fully open
749 tasklet_schedule (&dev->bh);
750 return retval;
751 done:
752 usb_autopm_put_interface(dev->intf);
753 done_nopm:
754 return retval;
756 EXPORT_SYMBOL_GPL(usbnet_open);
758 /*-------------------------------------------------------------------------*/
760 /* ethtool methods; minidrivers may need to add some more, but
761 * they'll probably want to use this base set.
764 #if defined(CONFIG_MII) || defined(CONFIG_MII_MODULE)
765 #define HAVE_MII
767 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
769 struct usbnet *dev = netdev_priv(net);
771 if (!dev->mii.mdio_read)
772 return -EOPNOTSUPP;
774 return mii_ethtool_gset(&dev->mii, cmd);
776 EXPORT_SYMBOL_GPL(usbnet_get_settings);
778 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
780 struct usbnet *dev = netdev_priv(net);
781 int retval;
783 if (!dev->mii.mdio_write)
784 return -EOPNOTSUPP;
786 retval = mii_ethtool_sset(&dev->mii, cmd);
788 /* link speed/duplex might have changed */
789 if (dev->driver_info->link_reset)
790 dev->driver_info->link_reset(dev);
792 return retval;
795 EXPORT_SYMBOL_GPL(usbnet_set_settings);
797 u32 usbnet_get_link (struct net_device *net)
799 struct usbnet *dev = netdev_priv(net);
801 /* If a check_connect is defined, return its result */
802 if (dev->driver_info->check_connect)
803 return dev->driver_info->check_connect (dev) == 0;
805 /* if the device has mii operations, use those */
806 if (dev->mii.mdio_read)
807 return mii_link_ok(&dev->mii);
809 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
810 return ethtool_op_get_link(net);
812 EXPORT_SYMBOL_GPL(usbnet_get_link);
814 int usbnet_nway_reset(struct net_device *net)
816 struct usbnet *dev = netdev_priv(net);
818 if (!dev->mii.mdio_write)
819 return -EOPNOTSUPP;
821 return mii_nway_restart(&dev->mii);
823 EXPORT_SYMBOL_GPL(usbnet_nway_reset);
825 #endif /* HAVE_MII */
827 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
829 struct usbnet *dev = netdev_priv(net);
831 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
832 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
833 strlcpy (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 struct ethtool_ops usbnet_ethtool_ops = {
857 #ifdef HAVE_MII
858 .get_settings = usbnet_get_settings,
859 .set_settings = usbnet_set_settings,
860 .get_link = usbnet_get_link,
861 .nway_reset = usbnet_nway_reset,
862 #endif
863 .get_drvinfo = usbnet_get_drvinfo,
864 .get_msglevel = usbnet_get_msglevel,
865 .set_msglevel = usbnet_set_msglevel,
868 /*-------------------------------------------------------------------------*/
870 /* work that cannot be done in interrupt context uses keventd.
872 * NOTE: with 2.5 we could do more of this using completion callbacks,
873 * especially now that control transfers can be queued.
875 static void
876 kevent (struct work_struct *work)
878 struct usbnet *dev =
879 container_of(work, struct usbnet, kevent);
880 int status;
882 /* usb_clear_halt() needs a thread context */
883 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
884 unlink_urbs (dev, &dev->txq);
885 status = usb_clear_halt (dev->udev, dev->out);
886 if (status < 0
887 && status != -EPIPE
888 && status != -ESHUTDOWN) {
889 if (netif_msg_tx_err (dev))
890 deverr (dev, "can't clear tx halt, status %d",
891 status);
892 } else {
893 clear_bit (EVENT_TX_HALT, &dev->flags);
894 if (status != -ESHUTDOWN)
895 netif_wake_queue (dev->net);
898 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
899 unlink_urbs (dev, &dev->rxq);
900 status = usb_clear_halt (dev->udev, dev->in);
901 if (status < 0
902 && status != -EPIPE
903 && status != -ESHUTDOWN) {
904 if (netif_msg_rx_err (dev))
905 deverr (dev, "can't clear rx halt, status %d",
906 status);
907 } else {
908 clear_bit (EVENT_RX_HALT, &dev->flags);
909 tasklet_schedule (&dev->bh);
913 /* tasklet could resubmit itself forever if memory is tight */
914 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
915 struct urb *urb = NULL;
916 int resched = 1;
918 if (netif_running (dev->net))
919 urb = usb_alloc_urb (0, GFP_KERNEL);
920 else
921 clear_bit (EVENT_RX_MEMORY, &dev->flags);
922 if (urb != NULL) {
923 clear_bit (EVENT_RX_MEMORY, &dev->flags);
924 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
925 resched = 0;
926 if (resched)
927 tasklet_schedule (&dev->bh);
931 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
932 struct driver_info *info = dev->driver_info;
933 int retval = 0;
935 clear_bit (EVENT_LINK_RESET, &dev->flags);
936 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
937 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
938 retval,
939 dev->udev->bus->bus_name, dev->udev->devpath,
940 info->description);
944 if (dev->flags)
945 devdbg (dev, "kevent done, flags = 0x%lx",
946 dev->flags);
949 /*-------------------------------------------------------------------------*/
951 static void tx_complete (struct urb *urb)
953 struct sk_buff *skb = (struct sk_buff *) urb->context;
954 struct skb_data *entry = (struct skb_data *) skb->cb;
955 struct usbnet *dev = entry->dev;
957 if (urb->status == 0) {
958 if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
959 dev->stats.tx_packets++;
960 dev->stats.tx_bytes += entry->length;
961 } else {
962 dev->stats.tx_errors++;
964 switch (urb->status) {
965 case -EPIPE:
966 usbnet_defer_kevent (dev, EVENT_TX_HALT);
967 break;
969 /* software-driven interface shutdown */
970 case -ECONNRESET: // async unlink
971 case -ESHUTDOWN: // hardware gone
972 break;
974 // like rx, tx gets controller i/o faults during khubd delays
975 // and so it uses the same throttling mechanism.
976 case -EPROTO:
977 case -ETIME:
978 case -EILSEQ:
979 if (!timer_pending (&dev->delay)) {
980 mod_timer (&dev->delay,
981 jiffies + THROTTLE_JIFFIES);
982 if (netif_msg_link (dev))
983 devdbg (dev, "tx throttle %d",
984 urb->status);
986 netif_stop_queue (dev->net);
987 break;
988 default:
989 if (netif_msg_tx_err (dev))
990 devdbg (dev, "tx err %d", entry->urb->status);
991 break;
995 (void) defer_bh(dev, skb, &dev->txq, tx_done);
998 /*-------------------------------------------------------------------------*/
1000 void usbnet_tx_timeout (struct net_device *net)
1002 struct usbnet *dev = netdev_priv(net);
1004 unlink_urbs (dev, &dev->txq);
1005 tasklet_schedule (&dev->bh);
1007 // FIXME: device recovery -- reset?
1009 EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1011 /*-------------------------------------------------------------------------*/
1013 int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
1015 struct usbnet *dev = netdev_priv(net);
1016 int length;
1017 int retval = NET_XMIT_SUCCESS;
1018 struct urb *urb = NULL;
1019 struct skb_data *entry;
1020 struct driver_info *info = dev->driver_info;
1021 unsigned long flags;
1023 // some devices want funky USB-level framing, for
1024 // win32 driver (usually) and/or hardware quirks
1025 if (info->tx_fixup) {
1026 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1027 if (!skb) {
1028 /* packet collected; minidriver waiting for more */
1029 if (info->flags & FLAG_MULTI_PACKET)
1030 goto not_drop;
1031 devdbg(dev, "can't tx_fixup skb");
1032 goto drop;
1035 length = skb->len;
1037 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1038 if (netif_msg_tx_err (dev))
1039 devdbg (dev, "no urb");
1040 goto drop;
1043 entry = (struct skb_data *) skb->cb;
1044 entry->urb = urb;
1045 entry->dev = dev;
1046 entry->length = length;
1048 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1049 skb->data, skb->len, tx_complete, skb);
1051 /* don't assume the hardware handles USB_ZERO_PACKET
1052 * NOTE: strictly conforming cdc-ether devices should expect
1053 * the ZLP here, but ignore the one-byte packet.
1054 * NOTE2: CDC NCM specification is different from CDC ECM when
1055 * handling ZLP/short packets, so cdc_ncm driver will make short
1056 * packet itself if needed.
1058 if (length % dev->maxpacket == 0) {
1059 if (!(info->flags & FLAG_SEND_ZLP)) {
1060 if (!(info->flags & FLAG_MULTI_PACKET)) {
1061 urb->transfer_buffer_length++;
1062 if (skb_tailroom(skb)) {
1063 skb->data[skb->len] = 0;
1064 __skb_put(skb, 1);
1067 } else
1068 urb->transfer_flags |= URB_ZERO_PACKET;
1071 spin_lock_irqsave (&dev->txq.lock, flags);
1073 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1074 case -EPIPE:
1075 netif_stop_queue (net);
1076 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1077 break;
1078 default:
1079 if (netif_msg_tx_err (dev))
1080 devdbg (dev, "tx: submit urb err %d", retval);
1081 break;
1082 case 0:
1083 net->trans_start = jiffies;
1084 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1085 if (dev->txq.qlen >= TX_QLEN (dev))
1086 netif_stop_queue (net);
1088 spin_unlock_irqrestore (&dev->txq.lock, flags);
1090 if (retval) {
1091 if (netif_msg_tx_err (dev))
1092 devdbg (dev, "drop, code %d", retval);
1093 drop:
1094 retval = NET_XMIT_SUCCESS;
1095 dev->stats.tx_dropped++;
1096 not_drop:
1097 if (skb)
1098 dev_kfree_skb_any (skb);
1099 usb_free_urb (urb);
1100 } else if (netif_msg_tx_queued (dev)) {
1101 devdbg (dev, "> tx, len %d, type 0x%x",
1102 length, skb->protocol);
1104 return retval;
1106 EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1108 /*-------------------------------------------------------------------------*/
1110 // tasklet (work deferred from completions, in_irq) or timer
1112 static void usbnet_bh (unsigned long param)
1114 struct usbnet *dev = (struct usbnet *) param;
1115 struct sk_buff *skb;
1116 struct skb_data *entry;
1118 while ((skb = skb_dequeue (&dev->done))) {
1119 entry = (struct skb_data *) skb->cb;
1120 switch (entry->state) {
1121 case rx_done:
1122 entry->state = rx_cleanup;
1123 rx_process (dev, skb);
1124 continue;
1125 case tx_done:
1126 case rx_cleanup:
1127 usb_free_urb (entry->urb);
1128 dev_kfree_skb (skb);
1129 continue;
1130 default:
1131 devdbg (dev, "bogus skb state %d", entry->state);
1135 // waiting for all pending urbs to complete?
1136 if (dev->wait) {
1137 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1138 wake_up (dev->wait);
1141 // or are we maybe short a few urbs?
1142 } else if (netif_running (dev->net)
1143 && netif_device_present (dev->net)
1144 && !timer_pending (&dev->delay)
1145 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1146 int temp = dev->rxq.qlen;
1147 int qlen = RX_QLEN (dev);
1149 if (temp < qlen) {
1150 struct urb *urb;
1151 int i;
1153 // don't refill the queue all at once
1154 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1155 urb = usb_alloc_urb (0, GFP_ATOMIC);
1156 if (urb != NULL) {
1157 if (rx_submit (dev, urb, GFP_ATOMIC) ==
1158 -ENOLINK)
1159 return;
1162 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1163 devdbg (dev, "rxqlen %d --> %d",
1164 temp, dev->rxq.qlen);
1165 if (dev->rxq.qlen < qlen)
1166 tasklet_schedule (&dev->bh);
1168 if (dev->txq.qlen < TX_QLEN (dev))
1169 netif_wake_queue (dev->net);
1175 /*-------------------------------------------------------------------------
1177 * USB Device Driver support
1179 *-------------------------------------------------------------------------*/
1181 // precondition: never called in_interrupt
1183 void usbnet_disconnect (struct usb_interface *intf)
1185 struct usbnet *dev;
1186 struct usb_device *xdev;
1187 struct net_device *net;
1189 dev = usb_get_intfdata(intf);
1190 usb_set_intfdata(intf, NULL);
1191 if (!dev)
1192 return;
1194 xdev = interface_to_usbdev (intf);
1196 if (netif_msg_probe (dev))
1197 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1198 intf->dev.driver->name,
1199 xdev->bus->bus_name, xdev->devpath,
1200 dev->driver_info->description);
1202 net = dev->net;
1203 unregister_netdev (net);
1205 /* we don't hold rtnl here ... */
1206 flush_scheduled_work ();
1208 if (dev->driver_info->unbind)
1209 dev->driver_info->unbind (dev, intf);
1211 free_netdev(net);
1212 usb_put_dev (xdev);
1214 EXPORT_SYMBOL_GPL(usbnet_disconnect);
1217 /*-------------------------------------------------------------------------*/
1219 // precondition: never called in_interrupt
1222 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1224 struct usbnet *dev;
1225 struct net_device *net;
1226 struct usb_host_interface *interface;
1227 struct driver_info *info;
1228 struct usb_device *xdev;
1229 int status;
1230 const char *name;
1232 name = udev->dev.driver->name;
1233 info = (struct driver_info *) prod->driver_info;
1234 if (!info) {
1235 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1236 return -ENODEV;
1238 xdev = interface_to_usbdev (udev);
1239 interface = udev->cur_altsetting;
1241 usb_get_dev (xdev);
1243 status = -ENOMEM;
1245 // set up our own records
1246 net = alloc_etherdev(sizeof(*dev));
1247 if (!net) {
1248 dbg ("can't kmalloc dev");
1249 goto out;
1252 dev = netdev_priv(net);
1253 dev->udev = xdev;
1254 dev->intf = udev;
1255 dev->driver_info = info;
1256 dev->driver_name = name;
1257 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1258 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1259 skb_queue_head_init (&dev->rxq);
1260 skb_queue_head_init (&dev->txq);
1261 skb_queue_head_init (&dev->done);
1262 dev->bh.func = usbnet_bh;
1263 dev->bh.data = (unsigned long) dev;
1264 INIT_WORK (&dev->kevent, kevent);
1265 dev->delay.function = usbnet_bh;
1266 dev->delay.data = (unsigned long) dev;
1267 init_timer (&dev->delay);
1268 mutex_init (&dev->phy_mutex);
1270 SET_MODULE_OWNER (net);
1271 dev->net = net;
1272 strcpy (net->name, "usb%d");
1273 memcpy (net->dev_addr, node_id, sizeof node_id);
1275 /* rx and tx sides can use different message sizes;
1276 * bind() should set rx_urb_size in that case.
1278 dev->hard_mtu = net->mtu + net->hard_header_len;
1279 #if 0
1280 // dma_supported() is deeply broken on almost all architectures
1281 // possible with some EHCI controllers
1282 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1283 net->features |= NETIF_F_HIGHDMA;
1284 #endif
1286 net->change_mtu = usbnet_change_mtu;
1287 net->get_stats = usbnet_get_stats;
1288 net->hard_start_xmit = usbnet_start_xmit;
1289 net->open = usbnet_open;
1290 net->stop = usbnet_stop;
1291 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1292 net->tx_timeout = usbnet_tx_timeout;
1293 net->ethtool_ops = &usbnet_ethtool_ops;
1295 // allow device-specific bind/init procedures
1296 // NOTE net->name still not usable ...
1297 if (info->bind) {
1298 status = info->bind (dev, udev);
1299 if (status < 0)
1300 goto out1;
1302 // heuristic: "usb%d" for links we know are two-host,
1303 // else "eth%d" when there's reasonable doubt. userspace
1304 // can rename the link if it knows better.
1305 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1306 && (net->dev_addr [0] & 0x02) == 0)
1307 strcpy (net->name, "eth%d");
1309 /* devices that cannot do ARP */
1310 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1311 net->flags |= IFF_NOARP;
1313 /* maybe the remote can't receive an Ethernet MTU */
1314 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1315 net->mtu = dev->hard_mtu - net->hard_header_len;
1316 } else if (!info->in || !info->out)
1317 status = usbnet_get_endpoints (dev, udev);
1318 else {
1319 dev->in = usb_rcvbulkpipe (xdev, info->in);
1320 dev->out = usb_sndbulkpipe (xdev, info->out);
1321 if (!(info->flags & FLAG_NO_SETINT))
1322 status = usb_set_interface (xdev,
1323 interface->desc.bInterfaceNumber,
1324 interface->desc.bAlternateSetting);
1325 else
1326 status = 0;
1329 if (status == 0 && dev->status)
1330 status = init_status (dev, udev);
1331 if (status < 0)
1332 goto out3;
1334 if (!dev->rx_urb_size)
1335 dev->rx_urb_size = dev->hard_mtu;
1336 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1338 SET_NETDEV_DEV(net, &udev->dev);
1339 status = register_netdev (net);
1340 if (status)
1341 goto out3;
1342 if (netif_msg_probe (dev))
1343 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1344 "%02x:%02x:%02x:%02x:%02x:%02x",
1345 udev->dev.driver->name,
1346 xdev->bus->bus_name, xdev->devpath,
1347 dev->driver_info->description,
1348 net->dev_addr [0], net->dev_addr [1],
1349 net->dev_addr [2], net->dev_addr [3],
1350 net->dev_addr [4], net->dev_addr [5]);
1352 // ok, it's ready to go.
1353 usb_set_intfdata (udev, dev);
1355 // start as if the link is up
1356 netif_device_attach (net);
1358 return 0;
1360 out3:
1361 if (info->unbind)
1362 info->unbind (dev, udev);
1363 out1:
1364 free_netdev(net);
1365 out:
1366 usb_put_dev(xdev);
1367 return status;
1369 EXPORT_SYMBOL_GPL(usbnet_probe);
1371 /*-------------------------------------------------------------------------*/
1374 * suspend the whole driver as soon as the first interface is suspended
1375 * resume only when the last interface is resumed
1378 int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1380 struct usbnet *dev = usb_get_intfdata(intf);
1382 if (!dev->suspend_count++) {
1384 * accelerate emptying of the rx and queues, to avoid
1385 * having everything error out.
1387 netif_device_detach (dev->net);
1388 (void) unlink_urbs (dev, &dev->rxq);
1389 (void) unlink_urbs (dev, &dev->txq);
1391 * reattach so runtime management can use and
1392 * wake the device
1394 netif_device_attach (dev->net);
1396 return 0;
1398 EXPORT_SYMBOL_GPL(usbnet_suspend);
1400 int usbnet_resume (struct usb_interface *intf)
1402 struct usbnet *dev = usb_get_intfdata(intf);
1404 if (!--dev->suspend_count)
1405 tasklet_schedule (&dev->bh);
1407 return 0;
1409 EXPORT_SYMBOL_GPL(usbnet_resume);
1412 /*-------------------------------------------------------------------------*/
1414 static int __init usbnet_init(void)
1416 /* compiler should optimize this out */
1417 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1418 < sizeof (struct skb_data));
1420 random_ether_addr(node_id);
1421 return 0;
1423 module_init(usbnet_init);
1425 static void __exit usbnet_exit(void)
1428 module_exit(usbnet_exit);
1430 MODULE_AUTHOR("David Brownell");
1431 MODULE_DESCRIPTION("USB network driver framework");
1432 MODULE_LICENSE("GPL");