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/sched.h>
38 #include <linux/init.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/workqueue.h>
43 #include <linux/mii.h>
44 #include <linux/usb.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)
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
)
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
++) {
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
;
116 e
= alt
->endpoint
+ ep
;
117 switch (e
->desc
.bmAttributes
) {
118 case USB_ENDPOINT_XFER_INT
:
119 if (!(e
->desc
.bEndpointAddress
& USB_DIR_IN
))
123 case USB_ENDPOINT_XFER_BULK
:
128 if (e
->desc
.bEndpointAddress
& USB_DIR_IN
) {
131 else if (intr
&& !status
)
141 if (!alt
|| !in
|| !out
)
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
);
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
;
159 EXPORT_SYMBOL_GPL(usbnet_get_endpoints
);
161 static void intr_complete (struct urb
*urb
);
163 static int init_status (struct usbnet
*dev
, struct usb_interface
*intf
)
170 if (!dev
->driver_info
->status
)
173 pipe
= usb_rcvintpipe (dev
->udev
,
174 dev
->status
->desc
.bEndpointAddress
175 & USB_ENDPOINT_NUMBER_MASK
);
176 maxp
= usb_maxpacket (dev
->udev
, pipe
, 0);
178 /* avoid 1 msec chatter: min 8 msec poll rate */
179 period
= max ((int) dev
->status
->desc
.bInterval
,
180 (dev
->udev
->speed
== USB_SPEED_HIGH
) ? 7 : 3);
182 buf
= kmalloc (maxp
, SLAB_KERNEL
);
184 dev
->interrupt
= usb_alloc_urb (0, SLAB_KERNEL
);
185 if (!dev
->interrupt
) {
189 usb_fill_int_urb(dev
->interrupt
, dev
->udev
, pipe
,
190 buf
, maxp
, intr_complete
, dev
, period
);
192 "status ep%din, %d bytes period %d\n",
193 usb_pipeendpoint(pipe
), maxp
, period
);
199 /* Passes this packet up the stack, updating its accounting.
200 * Some link protocols batch packets, so their rx_fixup paths
201 * can return clones as well as just modify the original skb.
203 void usbnet_skb_return (struct usbnet
*dev
, struct sk_buff
*skb
)
208 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
209 dev
->stats
.rx_packets
++;
210 dev
->stats
.rx_bytes
+= skb
->len
;
212 if (netif_msg_rx_status (dev
))
213 devdbg (dev
, "< rx, len %zu, type 0x%x",
214 skb
->len
+ sizeof (struct ethhdr
), skb
->protocol
);
215 memset (skb
->cb
, 0, sizeof (struct skb_data
));
216 status
= netif_rx (skb
);
217 if (status
!= NET_RX_SUCCESS
&& netif_msg_rx_err (dev
))
218 devdbg (dev
, "netif_rx status %d", status
);
220 EXPORT_SYMBOL_GPL(usbnet_skb_return
);
223 /*-------------------------------------------------------------------------
225 * Network Device Driver (peer link to "Host Device", from USB host)
227 *-------------------------------------------------------------------------*/
229 static int usbnet_change_mtu (struct net_device
*net
, int new_mtu
)
231 struct usbnet
*dev
= netdev_priv(net
);
232 int ll_mtu
= new_mtu
+ net
->hard_header_len
;
233 int old_hard_mtu
= dev
->hard_mtu
;
234 int old_rx_urb_size
= dev
->rx_urb_size
;
238 // no second zero-length packet read wanted after mtu-sized packets
239 if ((ll_mtu
% dev
->maxpacket
) == 0)
243 dev
->hard_mtu
= net
->mtu
+ net
->hard_header_len
;
244 if (dev
->rx_urb_size
== old_hard_mtu
) {
245 dev
->rx_urb_size
= dev
->hard_mtu
;
246 if (dev
->rx_urb_size
> old_rx_urb_size
)
247 usbnet_unlink_rx_urbs(dev
);
253 /*-------------------------------------------------------------------------*/
255 static struct net_device_stats
*usbnet_get_stats (struct net_device
*net
)
257 struct usbnet
*dev
= netdev_priv(net
);
261 /*-------------------------------------------------------------------------*/
263 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
264 * completion callbacks. 2.5 should have fixed those bugs...
267 static void defer_bh(struct usbnet
*dev
, struct sk_buff
*skb
, struct sk_buff_head
*list
)
271 spin_lock_irqsave(&list
->lock
, flags
);
272 __skb_unlink(skb
, list
);
273 spin_unlock(&list
->lock
);
274 spin_lock(&dev
->done
.lock
);
275 __skb_queue_tail(&dev
->done
, skb
);
276 if (dev
->done
.qlen
== 1)
277 tasklet_schedule(&dev
->bh
);
278 spin_unlock_irqrestore(&dev
->done
.lock
, flags
);
281 /* some work can't be done in tasklets, so we use keventd
283 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
284 * but tasklet_schedule() doesn't. hope the failure is rare.
286 void usbnet_defer_kevent (struct usbnet
*dev
, int work
)
288 set_bit (work
, &dev
->flags
);
289 if (!schedule_work (&dev
->kevent
))
290 deverr (dev
, "kevent %d may have been dropped", work
);
292 devdbg (dev
, "kevent %d scheduled", work
);
294 EXPORT_SYMBOL_GPL(usbnet_defer_kevent
);
296 /*-------------------------------------------------------------------------*/
298 static void rx_complete (struct urb
*urb
);
300 static void rx_submit (struct usbnet
*dev
, struct urb
*urb
, gfp_t flags
)
303 struct skb_data
*entry
;
305 unsigned long lockflags
;
306 size_t size
= dev
->rx_urb_size
;
308 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, flags
)) == NULL
) {
309 if (netif_msg_rx_err (dev
))
310 devdbg (dev
, "no rx skb");
311 usbnet_defer_kevent (dev
, EVENT_RX_MEMORY
);
315 skb_reserve (skb
, NET_IP_ALIGN
);
317 entry
= (struct skb_data
*) skb
->cb
;
320 entry
->state
= rx_start
;
323 usb_fill_bulk_urb (urb
, dev
->udev
, dev
->in
,
324 skb
->data
, size
, rx_complete
, skb
);
326 spin_lock_irqsave (&dev
->rxq
.lock
, lockflags
);
328 if (netif_running (dev
->net
)
329 && netif_device_present (dev
->net
)
330 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
331 switch (retval
= usb_submit_urb (urb
, GFP_ATOMIC
)){
333 usbnet_defer_kevent (dev
, EVENT_RX_HALT
);
336 usbnet_defer_kevent (dev
, EVENT_RX_MEMORY
);
339 if (netif_msg_ifdown (dev
))
340 devdbg (dev
, "device gone");
341 netif_device_detach (dev
->net
);
344 if (netif_msg_rx_err (dev
))
345 devdbg (dev
, "rx submit, %d", retval
);
346 tasklet_schedule (&dev
->bh
);
349 __skb_queue_tail (&dev
->rxq
, skb
);
352 if (netif_msg_ifdown (dev
))
353 devdbg (dev
, "rx: stopped");
356 spin_unlock_irqrestore (&dev
->rxq
.lock
, lockflags
);
358 dev_kfree_skb_any (skb
);
364 /*-------------------------------------------------------------------------*/
366 static inline void rx_process (struct usbnet
*dev
, struct sk_buff
*skb
)
368 if (dev
->driver_info
->rx_fixup
369 && !dev
->driver_info
->rx_fixup (dev
, skb
))
371 // else network stack removes extra byte if we forced a short packet
374 usbnet_skb_return (dev
, skb
);
376 if (netif_msg_rx_err (dev
))
377 devdbg (dev
, "drop");
379 dev
->stats
.rx_errors
++;
380 skb_queue_tail (&dev
->done
, skb
);
384 /*-------------------------------------------------------------------------*/
386 static void rx_complete (struct urb
*urb
)
388 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
389 struct skb_data
*entry
= (struct skb_data
*) skb
->cb
;
390 struct usbnet
*dev
= entry
->dev
;
391 int urb_status
= urb
->status
;
393 skb_put (skb
, urb
->actual_length
);
394 entry
->state
= rx_done
;
397 switch (urb_status
) {
400 if (skb
->len
< dev
->net
->hard_header_len
) {
401 entry
->state
= rx_cleanup
;
402 dev
->stats
.rx_errors
++;
403 dev
->stats
.rx_length_errors
++;
404 if (netif_msg_rx_err (dev
))
405 devdbg (dev
, "rx length %d", skb
->len
);
409 // stalls need manual reset. this is rare ... except that
410 // when going through USB 2.0 TTs, unplug appears this way.
411 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
412 // storm, recovering as needed.
414 dev
->stats
.rx_errors
++;
415 usbnet_defer_kevent (dev
, EVENT_RX_HALT
);
418 // software-driven interface shutdown
419 case -ECONNRESET
: // async unlink
420 case -ESHUTDOWN
: // hardware gone
421 if (netif_msg_ifdown (dev
))
422 devdbg (dev
, "rx shutdown, code %d", urb_status
);
425 // we get controller i/o faults during khubd disconnect() delays.
426 // throttle down resubmits, to avoid log floods; just temporarily,
427 // so we still recover when the fault isn't a khubd delay.
431 dev
->stats
.rx_errors
++;
432 if (!timer_pending (&dev
->delay
)) {
433 mod_timer (&dev
->delay
, jiffies
+ THROTTLE_JIFFIES
);
434 if (netif_msg_link (dev
))
435 devdbg (dev
, "rx throttle %d", urb_status
);
438 entry
->state
= rx_cleanup
;
443 // data overrun ... flush fifo?
445 dev
->stats
.rx_over_errors
++;
449 entry
->state
= rx_cleanup
;
450 dev
->stats
.rx_errors
++;
451 if (netif_msg_rx_err (dev
))
452 devdbg (dev
, "rx status %d", urb_status
);
456 defer_bh(dev
, skb
, &dev
->rxq
);
459 if (netif_running (dev
->net
)
460 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
461 rx_submit (dev
, urb
, GFP_ATOMIC
);
466 if (netif_msg_rx_err (dev
))
467 devdbg (dev
, "no read resubmitted");
470 static void intr_complete (struct urb
*urb
)
472 struct usbnet
*dev
= urb
->context
;
473 int status
= urb
->status
;
478 dev
->driver_info
->status(dev
, urb
);
481 /* software-driven interface shutdown */
482 case -ENOENT
: // urb killed
483 case -ESHUTDOWN
: // hardware gone
484 if (netif_msg_ifdown (dev
))
485 devdbg (dev
, "intr shutdown, code %d", status
);
488 /* NOTE: not throttling like RX/TX, since this endpoint
489 * already polls infrequently
492 devdbg (dev
, "intr status %d", status
);
496 if (!netif_running (dev
->net
))
499 memset(urb
->transfer_buffer
, 0, urb
->transfer_buffer_length
);
500 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
501 if (status
!= 0 && netif_msg_timer (dev
))
502 deverr(dev
, "intr resubmit --> %d", status
);
505 /*-------------------------------------------------------------------------*/
507 // unlink pending rx/tx; completion handlers do all other cleanup
509 static int unlink_urbs (struct usbnet
*dev
, struct sk_buff_head
*q
)
512 struct sk_buff
*skb
, *skbnext
;
515 spin_lock_irqsave (&q
->lock
, flags
);
516 for (skb
= q
->next
; skb
!= (struct sk_buff
*) q
; skb
= skbnext
) {
517 struct skb_data
*entry
;
521 entry
= (struct skb_data
*) skb
->cb
;
525 // during some PM-driven resume scenarios,
526 // these (async) unlinks complete immediately
527 retval
= usb_unlink_urb (urb
);
528 if (retval
!= -EINPROGRESS
&& retval
!= 0)
529 devdbg (dev
, "unlink urb err, %d", retval
);
533 spin_unlock_irqrestore (&q
->lock
, flags
);
537 // Flush all pending rx urbs
538 // minidrivers may need to do this when the MTU changes
540 void usbnet_unlink_rx_urbs(struct usbnet
*dev
)
542 if (netif_running(dev
->net
)) {
543 (void) unlink_urbs (dev
, &dev
->rxq
);
544 tasklet_schedule(&dev
->bh
);
547 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs
);
549 /*-------------------------------------------------------------------------*/
551 // precondition: never called in_interrupt
553 static int usbnet_stop (struct net_device
*net
)
555 struct usbnet
*dev
= netdev_priv(net
);
557 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup
);
558 DECLARE_WAITQUEUE (wait
, current
);
560 netif_stop_queue (net
);
562 if (netif_msg_ifdown (dev
))
563 devinfo (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
564 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
565 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
568 // ensure there are no more active urbs
569 add_wait_queue (&unlink_wakeup
, &wait
);
570 dev
->wait
= &unlink_wakeup
;
571 temp
= unlink_urbs (dev
, &dev
->txq
) + unlink_urbs (dev
, &dev
->rxq
);
573 // maybe wait for deletions to finish.
574 while (!skb_queue_empty(&dev
->rxq
) &&
575 !skb_queue_empty(&dev
->txq
) &&
576 !skb_queue_empty(&dev
->done
)) {
577 msleep(UNLINK_TIMEOUT_MS
);
578 if (netif_msg_ifdown (dev
))
579 devdbg (dev
, "waited for %d urb completions", temp
);
582 remove_wait_queue (&unlink_wakeup
, &wait
);
584 usb_kill_urb(dev
->interrupt
);
586 /* deferred work (task, timer, softirq) must also stop.
587 * can't flush_scheduled_work() until we drop rtnl (later),
588 * else workers could deadlock; so make workers a NOP.
591 del_timer_sync (&dev
->delay
);
592 tasklet_kill (&dev
->bh
);
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
);
607 struct driver_info
*info
= dev
->driver_info
;
609 // put into "known safe" state
610 if (info
->reset
&& (retval
= info
->reset (dev
)) < 0) {
611 if (netif_msg_ifup (dev
))
613 "open reset fail (%d) usbnet usb-%s-%s, %s",
615 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
620 // insist peer be connected
621 if (info
->check_connect
&& (retval
= info
->check_connect (dev
)) < 0) {
622 if (netif_msg_ifup (dev
))
623 devdbg (dev
, "can't open; %d", retval
);
627 /* start any status interrupt transfer */
628 if (dev
->interrupt
) {
629 retval
= usb_submit_urb (dev
->interrupt
, GFP_KERNEL
);
631 if (netif_msg_ifup (dev
))
632 deverr (dev
, "intr submit %d", retval
);
637 netif_start_queue (net
);
638 if (netif_msg_ifup (dev
)) {
641 if (dev
->driver_info
->flags
& FLAG_FRAMING_NC
)
643 else if (dev
->driver_info
->flags
& FLAG_FRAMING_GL
)
645 else if (dev
->driver_info
->flags
& FLAG_FRAMING_Z
)
647 else if (dev
->driver_info
->flags
& FLAG_FRAMING_RN
)
649 else if (dev
->driver_info
->flags
& FLAG_FRAMING_AX
)
654 devinfo (dev
, "open: enable queueing "
655 "(rx %d, tx %d) mtu %d %s framing",
656 (int)RX_QLEN (dev
), (int)TX_QLEN (dev
), dev
->net
->mtu
,
660 // delay posting reads until we're fully open
661 tasklet_schedule (&dev
->bh
);
666 /*-------------------------------------------------------------------------*/
668 /* ethtool methods; minidrivers may need to add some more, but
669 * they'll probably want to use this base set.
672 void usbnet_get_drvinfo (struct net_device
*net
, struct ethtool_drvinfo
*info
)
674 struct usbnet
*dev
= netdev_priv(net
);
676 /* REVISIT don't always return "usbnet" */
677 strncpy (info
->driver
, driver_name
, sizeof info
->driver
);
678 strncpy (info
->version
, DRIVER_VERSION
, sizeof info
->version
);
679 strncpy (info
->fw_version
, dev
->driver_info
->description
,
680 sizeof info
->fw_version
);
681 usb_make_path (dev
->udev
, info
->bus_info
, sizeof info
->bus_info
);
683 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo
);
685 static u32
usbnet_get_link (struct net_device
*net
)
687 struct usbnet
*dev
= netdev_priv(net
);
689 /* If a check_connect is defined, return its result */
690 if (dev
->driver_info
->check_connect
)
691 return dev
->driver_info
->check_connect (dev
) == 0;
693 /* Otherwise, say we're up (to avoid breaking scripts) */
697 u32
usbnet_get_msglevel (struct net_device
*net
)
699 struct usbnet
*dev
= netdev_priv(net
);
701 return dev
->msg_enable
;
703 EXPORT_SYMBOL_GPL(usbnet_get_msglevel
);
705 void usbnet_set_msglevel (struct net_device
*net
, u32 level
)
707 struct usbnet
*dev
= netdev_priv(net
);
709 dev
->msg_enable
= level
;
711 EXPORT_SYMBOL_GPL(usbnet_set_msglevel
);
713 /* drivers may override default ethtool_ops in their bind() routine */
714 static struct ethtool_ops usbnet_ethtool_ops
= {
715 .get_drvinfo
= usbnet_get_drvinfo
,
716 .get_link
= usbnet_get_link
,
717 .get_msglevel
= usbnet_get_msglevel
,
718 .set_msglevel
= usbnet_set_msglevel
,
721 /*-------------------------------------------------------------------------*/
723 /* work that cannot be done in interrupt context uses keventd.
725 * NOTE: with 2.5 we could do more of this using completion callbacks,
726 * especially now that control transfers can be queued.
731 struct usbnet
*dev
= data
;
734 /* usb_clear_halt() needs a thread context */
735 if (test_bit (EVENT_TX_HALT
, &dev
->flags
)) {
736 unlink_urbs (dev
, &dev
->txq
);
737 status
= usb_clear_halt (dev
->udev
, dev
->out
);
740 && status
!= -ESHUTDOWN
) {
741 if (netif_msg_tx_err (dev
))
742 deverr (dev
, "can't clear tx halt, status %d",
745 clear_bit (EVENT_TX_HALT
, &dev
->flags
);
746 if (status
!= -ESHUTDOWN
)
747 netif_wake_queue (dev
->net
);
750 if (test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
751 unlink_urbs (dev
, &dev
->rxq
);
752 status
= usb_clear_halt (dev
->udev
, dev
->in
);
755 && status
!= -ESHUTDOWN
) {
756 if (netif_msg_rx_err (dev
))
757 deverr (dev
, "can't clear rx halt, status %d",
760 clear_bit (EVENT_RX_HALT
, &dev
->flags
);
761 tasklet_schedule (&dev
->bh
);
765 /* tasklet could resubmit itself forever if memory is tight */
766 if (test_bit (EVENT_RX_MEMORY
, &dev
->flags
)) {
767 struct urb
*urb
= NULL
;
769 if (netif_running (dev
->net
))
770 urb
= usb_alloc_urb (0, GFP_KERNEL
);
772 clear_bit (EVENT_RX_MEMORY
, &dev
->flags
);
774 clear_bit (EVENT_RX_MEMORY
, &dev
->flags
);
775 rx_submit (dev
, urb
, GFP_KERNEL
);
776 tasklet_schedule (&dev
->bh
);
780 if (test_bit (EVENT_LINK_RESET
, &dev
->flags
)) {
781 struct driver_info
*info
= dev
->driver_info
;
784 clear_bit (EVENT_LINK_RESET
, &dev
->flags
);
785 if(info
->link_reset
&& (retval
= info
->link_reset(dev
)) < 0) {
786 devinfo(dev
, "link reset failed (%d) usbnet usb-%s-%s, %s",
788 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
794 devdbg (dev
, "kevent done, flags = 0x%lx",
798 /*-------------------------------------------------------------------------*/
800 static void tx_complete (struct urb
*urb
)
802 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
803 struct skb_data
*entry
= (struct skb_data
*) skb
->cb
;
804 struct usbnet
*dev
= entry
->dev
;
806 if (urb
->status
== 0) {
807 dev
->stats
.tx_packets
++;
808 dev
->stats
.tx_bytes
+= entry
->length
;
810 dev
->stats
.tx_errors
++;
812 switch (urb
->status
) {
814 usbnet_defer_kevent (dev
, EVENT_TX_HALT
);
817 /* software-driven interface shutdown */
818 case -ECONNRESET
: // async unlink
819 case -ESHUTDOWN
: // hardware gone
822 // like rx, tx gets controller i/o faults during khubd delays
823 // and so it uses the same throttling mechanism.
827 if (!timer_pending (&dev
->delay
)) {
828 mod_timer (&dev
->delay
,
829 jiffies
+ THROTTLE_JIFFIES
);
830 if (netif_msg_link (dev
))
831 devdbg (dev
, "tx throttle %d",
834 netif_stop_queue (dev
->net
);
837 if (netif_msg_tx_err (dev
))
838 devdbg (dev
, "tx err %d", entry
->urb
->status
);
844 entry
->state
= tx_done
;
845 defer_bh(dev
, skb
, &dev
->txq
);
848 /*-------------------------------------------------------------------------*/
850 static void usbnet_tx_timeout (struct net_device
*net
)
852 struct usbnet
*dev
= netdev_priv(net
);
854 unlink_urbs (dev
, &dev
->txq
);
855 tasklet_schedule (&dev
->bh
);
857 // FIXME: device recovery -- reset?
860 /*-------------------------------------------------------------------------*/
862 static int usbnet_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
864 struct usbnet
*dev
= netdev_priv(net
);
866 int retval
= NET_XMIT_SUCCESS
;
867 struct urb
*urb
= NULL
;
868 struct skb_data
*entry
;
869 struct driver_info
*info
= dev
->driver_info
;
872 // some devices want funky USB-level framing, for
873 // win32 driver (usually) and/or hardware quirks
874 if (info
->tx_fixup
) {
875 skb
= info
->tx_fixup (dev
, skb
, GFP_ATOMIC
);
877 if (netif_msg_tx_err (dev
))
878 devdbg (dev
, "can't tx_fixup skb");
884 if (!(urb
= usb_alloc_urb (0, GFP_ATOMIC
))) {
885 if (netif_msg_tx_err (dev
))
886 devdbg (dev
, "no urb");
890 entry
= (struct skb_data
*) skb
->cb
;
893 entry
->state
= tx_start
;
894 entry
->length
= length
;
896 usb_fill_bulk_urb (urb
, dev
->udev
, dev
->out
,
897 skb
->data
, skb
->len
, tx_complete
, skb
);
899 /* don't assume the hardware handles USB_ZERO_PACKET
900 * NOTE: strictly conforming cdc-ether devices should expect
901 * the ZLP here, but ignore the one-byte packet.
903 * FIXME zero that byte, if it doesn't require a new skb.
905 if ((length
% dev
->maxpacket
) == 0)
906 urb
->transfer_buffer_length
++;
908 spin_lock_irqsave (&dev
->txq
.lock
, flags
);
910 switch ((retval
= usb_submit_urb (urb
, GFP_ATOMIC
))) {
912 netif_stop_queue (net
);
913 usbnet_defer_kevent (dev
, EVENT_TX_HALT
);
916 if (netif_msg_tx_err (dev
))
917 devdbg (dev
, "tx: submit urb err %d", retval
);
920 net
->trans_start
= jiffies
;
921 __skb_queue_tail (&dev
->txq
, skb
);
922 if (dev
->txq
.qlen
>= TX_QLEN (dev
))
923 netif_stop_queue (net
);
925 spin_unlock_irqrestore (&dev
->txq
.lock
, flags
);
928 if (netif_msg_tx_err (dev
))
929 devdbg (dev
, "drop, code %d", retval
);
931 retval
= NET_XMIT_SUCCESS
;
932 dev
->stats
.tx_dropped
++;
934 dev_kfree_skb_any (skb
);
936 } else if (netif_msg_tx_queued (dev
)) {
937 devdbg (dev
, "> tx, len %d, type 0x%x",
938 length
, skb
->protocol
);
944 /*-------------------------------------------------------------------------*/
946 // tasklet (work deferred from completions, in_irq) or timer
948 static void usbnet_bh (unsigned long param
)
950 struct usbnet
*dev
= (struct usbnet
*) param
;
952 struct skb_data
*entry
;
954 while ((skb
= skb_dequeue (&dev
->done
))) {
955 entry
= (struct skb_data
*) skb
->cb
;
956 switch (entry
->state
) {
958 entry
->state
= rx_cleanup
;
959 rx_process (dev
, skb
);
963 usb_free_urb (entry
->urb
);
967 devdbg (dev
, "bogus skb state %d", entry
->state
);
971 // waiting for all pending urbs to complete?
973 if ((dev
->txq
.qlen
+ dev
->rxq
.qlen
+ dev
->done
.qlen
) == 0) {
977 // or are we maybe short a few urbs?
978 } else if (netif_running (dev
->net
)
979 && netif_device_present (dev
->net
)
980 && !timer_pending (&dev
->delay
)
981 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
982 int temp
= dev
->rxq
.qlen
;
983 int qlen
= RX_QLEN (dev
);
989 // don't refill the queue all at once
990 for (i
= 0; i
< 10 && dev
->rxq
.qlen
< qlen
; i
++) {
991 urb
= usb_alloc_urb (0, GFP_ATOMIC
);
993 rx_submit (dev
, urb
, GFP_ATOMIC
);
995 if (temp
!= dev
->rxq
.qlen
&& netif_msg_link (dev
))
996 devdbg (dev
, "rxqlen %d --> %d",
997 temp
, dev
->rxq
.qlen
);
998 if (dev
->rxq
.qlen
< qlen
)
999 tasklet_schedule (&dev
->bh
);
1001 if (dev
->txq
.qlen
< TX_QLEN (dev
))
1002 netif_wake_queue (dev
->net
);
1008 /*-------------------------------------------------------------------------
1010 * USB Device Driver support
1012 *-------------------------------------------------------------------------*/
1014 // precondition: never called in_interrupt
1016 void usbnet_disconnect (struct usb_interface
*intf
)
1019 struct usb_device
*xdev
;
1020 struct net_device
*net
;
1022 dev
= usb_get_intfdata(intf
);
1023 usb_set_intfdata(intf
, NULL
);
1027 xdev
= interface_to_usbdev (intf
);
1029 if (netif_msg_probe (dev
))
1030 devinfo (dev
, "unregister '%s' usb-%s-%s, %s",
1031 intf
->dev
.driver
->name
,
1032 xdev
->bus
->bus_name
, xdev
->devpath
,
1033 dev
->driver_info
->description
);
1036 unregister_netdev (net
);
1038 /* we don't hold rtnl here ... */
1039 flush_scheduled_work ();
1041 if (dev
->driver_info
->unbind
)
1042 dev
->driver_info
->unbind (dev
, intf
);
1047 EXPORT_SYMBOL_GPL(usbnet_disconnect
);
1050 /*-------------------------------------------------------------------------*/
1052 // precondition: never called in_interrupt
1055 usbnet_probe (struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1058 struct net_device
*net
;
1059 struct usb_host_interface
*interface
;
1060 struct driver_info
*info
;
1061 struct usb_device
*xdev
;
1064 info
= (struct driver_info
*) prod
->driver_info
;
1066 dev_dbg (&udev
->dev
, "blacklisted by %s\n", driver_name
);
1069 xdev
= interface_to_usbdev (udev
);
1070 interface
= udev
->cur_altsetting
;
1076 // set up our own records
1077 net
= alloc_etherdev(sizeof(*dev
));
1079 dbg ("can't kmalloc dev");
1083 dev
= netdev_priv(net
);
1085 dev
->driver_info
= info
;
1086 dev
->msg_enable
= netif_msg_init (msg_level
, NETIF_MSG_DRV
1087 | NETIF_MSG_PROBE
| NETIF_MSG_LINK
);
1088 skb_queue_head_init (&dev
->rxq
);
1089 skb_queue_head_init (&dev
->txq
);
1090 skb_queue_head_init (&dev
->done
);
1091 dev
->bh
.func
= usbnet_bh
;
1092 dev
->bh
.data
= (unsigned long) dev
;
1093 INIT_WORK (&dev
->kevent
, kevent
, dev
);
1094 dev
->delay
.function
= usbnet_bh
;
1095 dev
->delay
.data
= (unsigned long) dev
;
1096 init_timer (&dev
->delay
);
1098 SET_MODULE_OWNER (net
);
1100 strcpy (net
->name
, "usb%d");
1101 memcpy (net
->dev_addr
, node_id
, sizeof node_id
);
1103 /* rx and tx sides can use different message sizes;
1104 * bind() should set rx_urb_size in that case.
1106 dev
->hard_mtu
= net
->mtu
+ net
->hard_header_len
;
1108 // dma_supported() is deeply broken on almost all architectures
1109 // possible with some EHCI controllers
1110 if (dma_supported (&udev
->dev
, DMA_64BIT_MASK
))
1111 net
->features
|= NETIF_F_HIGHDMA
;
1114 net
->change_mtu
= usbnet_change_mtu
;
1115 net
->get_stats
= usbnet_get_stats
;
1116 net
->hard_start_xmit
= usbnet_start_xmit
;
1117 net
->open
= usbnet_open
;
1118 net
->stop
= usbnet_stop
;
1119 net
->watchdog_timeo
= TX_TIMEOUT_JIFFIES
;
1120 net
->tx_timeout
= usbnet_tx_timeout
;
1121 net
->ethtool_ops
= &usbnet_ethtool_ops
;
1123 // allow device-specific bind/init procedures
1124 // NOTE net->name still not usable ...
1126 status
= info
->bind (dev
, udev
);
1127 // heuristic: "usb%d" for links we know are two-host,
1128 // else "eth%d" when there's reasonable doubt. userspace
1129 // can rename the link if it knows better.
1130 if ((dev
->driver_info
->flags
& FLAG_ETHER
) != 0
1131 && (net
->dev_addr
[0] & 0x02) == 0)
1132 strcpy (net
->name
, "eth%d");
1134 /* maybe the remote can't receive an Ethernet MTU */
1135 if (net
->mtu
> (dev
->hard_mtu
- net
->hard_header_len
))
1136 net
->mtu
= dev
->hard_mtu
- net
->hard_header_len
;
1137 } else if (!info
->in
|| !info
->out
)
1138 status
= usbnet_get_endpoints (dev
, udev
);
1140 dev
->in
= usb_rcvbulkpipe (xdev
, info
->in
);
1141 dev
->out
= usb_sndbulkpipe (xdev
, info
->out
);
1142 if (!(info
->flags
& FLAG_NO_SETINT
))
1143 status
= usb_set_interface (xdev
,
1144 interface
->desc
.bInterfaceNumber
,
1145 interface
->desc
.bAlternateSetting
);
1150 if (status
== 0 && dev
->status
)
1151 status
= init_status (dev
, udev
);
1155 if (!dev
->rx_urb_size
)
1156 dev
->rx_urb_size
= dev
->hard_mtu
;
1157 dev
->maxpacket
= usb_maxpacket (dev
->udev
, dev
->out
, 1);
1159 SET_NETDEV_DEV(net
, &udev
->dev
);
1160 status
= register_netdev (net
);
1163 if (netif_msg_probe (dev
))
1164 devinfo (dev
, "register '%s' at usb-%s-%s, %s, "
1165 "%02x:%02x:%02x:%02x:%02x:%02x",
1166 udev
->dev
.driver
->name
,
1167 xdev
->bus
->bus_name
, xdev
->devpath
,
1168 dev
->driver_info
->description
,
1169 net
->dev_addr
[0], net
->dev_addr
[1],
1170 net
->dev_addr
[2], net
->dev_addr
[3],
1171 net
->dev_addr
[4], net
->dev_addr
[5]);
1173 // ok, it's ready to go.
1174 usb_set_intfdata (udev
, dev
);
1176 // start as if the link is up
1177 netif_device_attach (net
);
1183 info
->unbind (dev
, udev
);
1190 EXPORT_SYMBOL_GPL(usbnet_probe
);
1192 /*-------------------------------------------------------------------------*/
1194 /* FIXME these suspend/resume methods assume non-CDC style
1195 * devices, with only one interface.
1198 int usbnet_suspend (struct usb_interface
*intf
, pm_message_t message
)
1200 struct usbnet
*dev
= usb_get_intfdata(intf
);
1202 /* accelerate emptying of the rx and queues, to avoid
1203 * having everything error out.
1205 netif_device_detach (dev
->net
);
1206 (void) unlink_urbs (dev
, &dev
->rxq
);
1207 (void) unlink_urbs (dev
, &dev
->txq
);
1210 EXPORT_SYMBOL_GPL(usbnet_suspend
);
1212 int usbnet_resume (struct usb_interface
*intf
)
1214 struct usbnet
*dev
= usb_get_intfdata(intf
);
1216 netif_device_attach (dev
->net
);
1217 tasklet_schedule (&dev
->bh
);
1220 EXPORT_SYMBOL_GPL(usbnet_resume
);
1223 /*-------------------------------------------------------------------------*/
1225 static int __init
usbnet_init(void)
1227 /* compiler should optimize this out */
1228 BUG_ON (sizeof (((struct sk_buff
*)0)->cb
)
1229 < sizeof (struct skb_data
));
1231 random_ether_addr(node_id
);
1234 module_init(usbnet_init
);
1236 static void __exit
usbnet_exit(void)
1239 module_exit(usbnet_exit
);
1241 MODULE_AUTHOR("David Brownell");
1242 MODULE_DESCRIPTION("USB network driver framework");
1243 MODULE_LICENSE("GPL");