2 * USB Network driver infrastructure
3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This is a generic "USB networking" framework that works with several
23 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
26 * These devices usually differ in terms of control protocols (if they
27 * even have one!) and sometimes they define new framing to wrap or batch
28 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
29 * so interface (un)binding, endpoint I/O queues, fault handling, and other
30 * issues can usefully be addressed by this framework.
33 // #define DEBUG // error path messages, extra info
34 // #define VERBOSE // more; success messages
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/ctype.h>
41 #include <linux/ethtool.h>
42 #include <linux/workqueue.h>
43 #include <linux/mii.h>
44 #include <linux/usb.h>
45 #include <linux/usb/usbnet.h>
47 #define DRIVER_VERSION "22-Aug-2005"
50 /*-------------------------------------------------------------------------*/
53 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
54 * Several dozen bytes of IPv4 data can fit in two such transactions.
55 * One maximum size Ethernet packet takes twenty four of them.
56 * For high speed, each frame comfortably fits almost 36 max size
57 * Ethernet packets (so queues should be bigger).
59 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
60 * let the USB host controller be busy for 5msec or more before an irq
61 * is required, under load. Jumbograms change the equation.
63 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
64 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
65 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
66 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
67 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
69 // reawaken network queue this soon after stopping; else watchdog barks
70 #define TX_TIMEOUT_JIFFIES (5*HZ)
72 // throttle rx/tx briefly after some faults, so khubd might disconnect()
73 // us (it polls at HZ/4 usually) before we report too many false errors.
74 #define THROTTLE_JIFFIES (HZ/8)
77 #define UNLINK_TIMEOUT_MS 3
79 /*-------------------------------------------------------------------------*/
81 // randomly generated ethernet address
82 static u8 node_id
[ETH_ALEN
];
84 static const char driver_name
[] = "usbnet";
86 /* use ethtool to change the level for any given device */
87 static int msg_level
= -1;
88 module_param (msg_level
, int, 0);
89 MODULE_PARM_DESC (msg_level
, "Override default message level");
91 /*-------------------------------------------------------------------------*/
93 /* handles CDC Ethernet and many other network "bulk data" interfaces */
94 int usbnet_get_endpoints(struct usbnet
*dev
, struct usb_interface
*intf
)
97 struct usb_host_interface
*alt
= NULL
;
98 struct usb_host_endpoint
*in
= NULL
, *out
= NULL
;
99 struct usb_host_endpoint
*status
= NULL
;
101 for (tmp
= 0; tmp
< intf
->num_altsetting
; tmp
++) {
104 in
= out
= status
= NULL
;
105 alt
= intf
->altsetting
+ tmp
;
107 /* take the first altsetting with in-bulk + out-bulk;
108 * remember any status endpoint, just in case;
109 * ignore other endpoints and altsetttings.
111 for (ep
= 0; ep
< alt
->desc
.bNumEndpoints
; ep
++) {
112 struct usb_host_endpoint
*e
;
115 e
= alt
->endpoint
+ ep
;
116 switch (e
->desc
.bmAttributes
) {
117 case USB_ENDPOINT_XFER_INT
:
118 if (!usb_endpoint_dir_in(&e
->desc
))
122 case USB_ENDPOINT_XFER_BULK
:
127 if (usb_endpoint_dir_in(&e
->desc
)) {
130 else if (intr
&& !status
)
140 if (!alt
|| !in
|| !out
)
143 if (alt
->desc
.bAlternateSetting
!= 0
144 || !(dev
->driver_info
->flags
& FLAG_NO_SETINT
)) {
145 tmp
= usb_set_interface (dev
->udev
, alt
->desc
.bInterfaceNumber
,
146 alt
->desc
.bAlternateSetting
);
151 dev
->in
= usb_rcvbulkpipe (dev
->udev
,
152 in
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
153 dev
->out
= usb_sndbulkpipe (dev
->udev
,
154 out
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
155 dev
->status
= status
;
158 EXPORT_SYMBOL_GPL(usbnet_get_endpoints
);
160 static u8
nibble(unsigned char c
)
162 if (likely(isdigit(c
)))
165 if (likely(isxdigit(c
)))
170 int usbnet_get_ethernet_addr(struct usbnet
*dev
, int iMACAddress
)
173 unsigned char buf
[13];
175 tmp
= usb_string(dev
->udev
, iMACAddress
, buf
, sizeof buf
);
177 dev_dbg(&dev
->udev
->dev
,
178 "bad MAC string %d fetch, %d\n", iMACAddress
, tmp
);
183 for (i
= tmp
= 0; i
< 6; i
++, tmp
+= 2)
184 dev
->net
->dev_addr
[i
] =
185 (nibble(buf
[tmp
]) << 4) + nibble(buf
[tmp
+ 1]);
188 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr
);
190 static void intr_complete (struct urb
*urb
);
192 static int init_status (struct usbnet
*dev
, struct usb_interface
*intf
)
199 if (!dev
->driver_info
->status
)
202 pipe
= usb_rcvintpipe (dev
->udev
,
203 dev
->status
->desc
.bEndpointAddress
204 & USB_ENDPOINT_NUMBER_MASK
);
205 maxp
= usb_maxpacket (dev
->udev
, pipe
, 0);
207 /* avoid 1 msec chatter: min 8 msec poll rate */
208 period
= max ((int) dev
->status
->desc
.bInterval
,
209 (dev
->udev
->speed
== USB_SPEED_HIGH
) ? 7 : 3);
211 buf
= kmalloc (maxp
, GFP_KERNEL
);
213 dev
->interrupt
= usb_alloc_urb (0, GFP_KERNEL
);
214 if (!dev
->interrupt
) {
218 usb_fill_int_urb(dev
->interrupt
, dev
->udev
, pipe
,
219 buf
, maxp
, intr_complete
, dev
, period
);
221 "status ep%din, %d bytes period %d\n",
222 usb_pipeendpoint(pipe
), maxp
, period
);
228 /* Passes this packet up the stack, updating its accounting.
229 * Some link protocols batch packets, so their rx_fixup paths
230 * can return clones as well as just modify the original skb.
232 void usbnet_skb_return (struct usbnet
*dev
, struct sk_buff
*skb
)
236 if (test_bit(EVENT_RX_PAUSED
, &dev
->flags
)) {
237 skb_queue_tail(&dev
->rxq_pause
, skb
);
241 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
242 dev
->net
->stats
.rx_packets
++;
243 dev
->net
->stats
.rx_bytes
+= skb
->len
;
245 if (netif_msg_rx_status (dev
))
246 devdbg (dev
, "< rx, len %zu, type 0x%x",
247 skb
->len
+ sizeof (struct ethhdr
), skb
->protocol
);
248 memset (skb
->cb
, 0, sizeof (struct skb_data
));
249 status
= netif_rx (skb
);
250 if (status
!= NET_RX_SUCCESS
&& netif_msg_rx_err (dev
))
251 devdbg (dev
, "netif_rx status %d", status
);
253 EXPORT_SYMBOL_GPL(usbnet_skb_return
);
256 /*-------------------------------------------------------------------------
258 * Network Device Driver (peer link to "Host Device", from USB host)
260 *-------------------------------------------------------------------------*/
262 int usbnet_change_mtu (struct net_device
*net
, int new_mtu
)
264 struct usbnet
*dev
= netdev_priv(net
);
265 int ll_mtu
= new_mtu
+ net
->hard_header_len
;
266 int old_hard_mtu
= dev
->hard_mtu
;
267 int old_rx_urb_size
= dev
->rx_urb_size
;
271 // no second zero-length packet read wanted after mtu-sized packets
272 if ((ll_mtu
% dev
->maxpacket
) == 0)
276 dev
->hard_mtu
= net
->mtu
+ net
->hard_header_len
;
277 if (dev
->rx_urb_size
== old_hard_mtu
) {
278 dev
->rx_urb_size
= dev
->hard_mtu
;
279 if (dev
->rx_urb_size
> old_rx_urb_size
)
280 usbnet_unlink_rx_urbs(dev
);
285 EXPORT_SYMBOL_GPL(usbnet_change_mtu
);
287 /*-------------------------------------------------------------------------*/
289 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
290 * completion callbacks. 2.5 should have fixed those bugs...
293 static void defer_bh(struct usbnet
*dev
, struct sk_buff
*skb
, struct sk_buff_head
*list
)
297 spin_lock_irqsave(&list
->lock
, flags
);
298 __skb_unlink(skb
, list
);
299 spin_unlock(&list
->lock
);
300 spin_lock(&dev
->done
.lock
);
301 __skb_queue_tail(&dev
->done
, skb
);
302 if (dev
->done
.qlen
== 1)
303 tasklet_schedule(&dev
->bh
);
304 spin_unlock_irqrestore(&dev
->done
.lock
, flags
);
307 /* some work can't be done in tasklets, so we use keventd
309 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
310 * but tasklet_schedule() doesn't. hope the failure is rare.
312 void usbnet_defer_kevent (struct usbnet
*dev
, int work
)
314 set_bit (work
, &dev
->flags
);
315 if (!schedule_work (&dev
->kevent
))
316 deverr (dev
, "kevent %d may have been dropped", work
);
318 devdbg (dev
, "kevent %d scheduled", work
);
320 EXPORT_SYMBOL_GPL(usbnet_defer_kevent
);
322 /*-------------------------------------------------------------------------*/
324 static void rx_complete (struct urb
*urb
);
326 static void rx_submit (struct usbnet
*dev
, struct urb
*urb
, gfp_t flags
)
329 struct skb_data
*entry
;
331 unsigned long lockflags
;
332 size_t size
= dev
->rx_urb_size
;
334 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, flags
)) == NULL
) {
335 if (netif_msg_rx_err (dev
))
336 devdbg (dev
, "no rx skb");
337 usbnet_defer_kevent (dev
, EVENT_RX_MEMORY
);
341 skb_reserve (skb
, NET_IP_ALIGN
);
343 entry
= (struct skb_data
*) skb
->cb
;
346 entry
->state
= rx_start
;
349 usb_fill_bulk_urb (urb
, dev
->udev
, dev
->in
,
350 skb
->data
, size
, rx_complete
, skb
);
352 spin_lock_irqsave (&dev
->rxq
.lock
, lockflags
);
354 if (netif_running (dev
->net
)
355 && netif_device_present (dev
->net
)
356 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
357 switch (retval
= usb_submit_urb (urb
, GFP_ATOMIC
)) {
359 usbnet_defer_kevent (dev
, EVENT_RX_HALT
);
362 usbnet_defer_kevent (dev
, EVENT_RX_MEMORY
);
365 if (netif_msg_ifdown (dev
))
366 devdbg (dev
, "device gone");
367 netif_device_detach (dev
->net
);
370 if (netif_msg_rx_err (dev
))
371 devdbg (dev
, "rx submit, %d", retval
);
372 tasklet_schedule (&dev
->bh
);
375 __skb_queue_tail (&dev
->rxq
, skb
);
378 if (netif_msg_ifdown (dev
))
379 devdbg (dev
, "rx: stopped");
382 spin_unlock_irqrestore (&dev
->rxq
.lock
, lockflags
);
384 dev_kfree_skb_any (skb
);
390 /*-------------------------------------------------------------------------*/
392 static inline void rx_process (struct usbnet
*dev
, struct sk_buff
*skb
)
394 if (dev
->driver_info
->rx_fixup
395 && !dev
->driver_info
->rx_fixup (dev
, skb
))
397 // else network stack removes extra byte if we forced a short packet
400 usbnet_skb_return (dev
, skb
);
402 if (netif_msg_rx_err (dev
))
403 devdbg (dev
, "drop");
405 dev
->net
->stats
.rx_errors
++;
406 skb_queue_tail (&dev
->done
, skb
);
410 /*-------------------------------------------------------------------------*/
412 static void rx_complete (struct urb
*urb
)
414 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
415 struct skb_data
*entry
= (struct skb_data
*) skb
->cb
;
416 struct usbnet
*dev
= entry
->dev
;
417 int urb_status
= urb
->status
;
419 skb_put (skb
, urb
->actual_length
);
420 entry
->state
= rx_done
;
423 switch (urb_status
) {
426 if (skb
->len
< dev
->net
->hard_header_len
) {
427 entry
->state
= rx_cleanup
;
428 dev
->net
->stats
.rx_errors
++;
429 dev
->net
->stats
.rx_length_errors
++;
430 if (netif_msg_rx_err (dev
))
431 devdbg (dev
, "rx length %d", skb
->len
);
435 /* stalls need manual reset. this is rare ... except that
436 * when going through USB 2.0 TTs, unplug appears this way.
437 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
438 * storm, recovering as needed.
441 dev
->net
->stats
.rx_errors
++;
442 usbnet_defer_kevent (dev
, EVENT_RX_HALT
);
445 /* software-driven interface shutdown */
446 case -ECONNRESET
: /* async unlink */
447 case -ESHUTDOWN
: /* hardware gone */
448 if (netif_msg_ifdown (dev
))
449 devdbg (dev
, "rx shutdown, code %d", urb_status
);
452 /* we get controller i/o faults during khubd disconnect() delays.
453 * throttle down resubmits, to avoid log floods; just temporarily,
454 * so we still recover when the fault isn't a khubd delay.
459 dev
->net
->stats
.rx_errors
++;
460 if (!timer_pending (&dev
->delay
)) {
461 mod_timer (&dev
->delay
, jiffies
+ THROTTLE_JIFFIES
);
462 if (netif_msg_link (dev
))
463 devdbg (dev
, "rx throttle %d", urb_status
);
466 entry
->state
= rx_cleanup
;
471 /* data overrun ... flush fifo? */
473 dev
->net
->stats
.rx_over_errors
++;
477 entry
->state
= rx_cleanup
;
478 dev
->net
->stats
.rx_errors
++;
479 if (netif_msg_rx_err (dev
))
480 devdbg (dev
, "rx status %d", urb_status
);
484 defer_bh(dev
, skb
, &dev
->rxq
);
487 if (netif_running (dev
->net
)
488 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
489 rx_submit (dev
, urb
, GFP_ATOMIC
);
494 if (netif_msg_rx_err (dev
))
495 devdbg (dev
, "no read resubmitted");
498 static void intr_complete (struct urb
*urb
)
500 struct usbnet
*dev
= urb
->context
;
501 int status
= urb
->status
;
506 dev
->driver_info
->status(dev
, urb
);
509 /* software-driven interface shutdown */
510 case -ENOENT
: /* urb killed */
511 case -ESHUTDOWN
: /* hardware gone */
512 if (netif_msg_ifdown (dev
))
513 devdbg (dev
, "intr shutdown, code %d", status
);
516 /* NOTE: not throttling like RX/TX, since this endpoint
517 * already polls infrequently
520 devdbg (dev
, "intr status %d", status
);
524 if (!netif_running (dev
->net
))
527 memset(urb
->transfer_buffer
, 0, urb
->transfer_buffer_length
);
528 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
529 if (status
!= 0 && netif_msg_timer (dev
))
530 deverr(dev
, "intr resubmit --> %d", status
);
533 /*-------------------------------------------------------------------------*/
534 void usbnet_pause_rx(struct usbnet
*dev
)
536 set_bit(EVENT_RX_PAUSED
, &dev
->flags
);
538 if (netif_msg_rx_status(dev
))
539 devdbg(dev
, "paused rx queue enabled");
541 EXPORT_SYMBOL_GPL(usbnet_pause_rx
);
543 void usbnet_resume_rx(struct usbnet
*dev
)
548 clear_bit(EVENT_RX_PAUSED
, &dev
->flags
);
550 while ((skb
= skb_dequeue(&dev
->rxq_pause
)) != NULL
) {
551 usbnet_skb_return(dev
, skb
);
555 tasklet_schedule(&dev
->bh
);
557 if (netif_msg_rx_status(dev
))
558 devdbg(dev
, "paused rx queue disabled, %d skbs requeued", num
);
560 EXPORT_SYMBOL_GPL(usbnet_resume_rx
);
562 void usbnet_purge_paused_rxq(struct usbnet
*dev
)
564 skb_queue_purge(&dev
->rxq_pause
);
566 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq
);
568 /*-------------------------------------------------------------------------*/
570 // unlink pending rx/tx; completion handlers do all other cleanup
572 static int unlink_urbs (struct usbnet
*dev
, struct sk_buff_head
*q
)
575 struct sk_buff
*skb
, *skbnext
;
578 spin_lock_irqsave (&q
->lock
, flags
);
579 skb_queue_walk_safe(q
, skb
, skbnext
) {
580 struct skb_data
*entry
;
584 entry
= (struct skb_data
*) skb
->cb
;
587 // during some PM-driven resume scenarios,
588 // these (async) unlinks complete immediately
589 retval
= usb_unlink_urb (urb
);
590 if (retval
!= -EINPROGRESS
&& retval
!= 0)
591 devdbg (dev
, "unlink urb err, %d", retval
);
595 spin_unlock_irqrestore (&q
->lock
, flags
);
599 // Flush all pending rx urbs
600 // minidrivers may need to do this when the MTU changes
602 void usbnet_unlink_rx_urbs(struct usbnet
*dev
)
604 if (netif_running(dev
->net
)) {
605 (void) unlink_urbs (dev
, &dev
->rxq
);
606 tasklet_schedule(&dev
->bh
);
609 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs
);
611 /*-------------------------------------------------------------------------*/
613 // precondition: never called in_interrupt
615 int usbnet_stop (struct net_device
*net
)
617 struct usbnet
*dev
= netdev_priv(net
);
618 struct driver_info
*info
= dev
->driver_info
;
621 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup
);
622 DECLARE_WAITQUEUE (wait
, current
);
624 netif_stop_queue (net
);
626 if (netif_msg_ifdown (dev
))
627 devinfo (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
628 net
->stats
.rx_packets
, net
->stats
.tx_packets
,
629 net
->stats
.rx_errors
, net
->stats
.tx_errors
632 /* allow minidriver to stop correctly (wireless devices to turn off
635 retval
= info
->stop(dev
);
636 if (retval
< 0 && netif_msg_ifdown(dev
))
638 "stop fail (%d) usbnet usb-%s-%s, %s",
640 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
644 if (!(info
->flags
& FLAG_AVOID_UNLINK_URBS
)) {
645 /* ensure there are no more active urbs */
646 add_wait_queue(&unlink_wakeup
, &wait
);
647 dev
->wait
= &unlink_wakeup
;
648 temp
= unlink_urbs(dev
, &dev
->txq
) +
649 unlink_urbs(dev
, &dev
->rxq
);
651 /* maybe wait for deletions to finish. */
652 while (!skb_queue_empty(&dev
->rxq
)
653 && !skb_queue_empty(&dev
->txq
)
654 && !skb_queue_empty(&dev
->done
)) {
655 msleep(UNLINK_TIMEOUT_MS
);
656 if (netif_msg_ifdown(dev
))
657 devdbg(dev
, "waited for %d urb completions",
661 remove_wait_queue(&unlink_wakeup
, &wait
);
664 usb_kill_urb(dev
->interrupt
);
666 usbnet_purge_paused_rxq(dev
);
668 /* deferred work (task, timer, softirq) must also stop.
669 * can't flush_scheduled_work() until we drop rtnl (later),
670 * else workers could deadlock; so make workers a NOP.
673 del_timer_sync (&dev
->delay
);
674 tasklet_kill (&dev
->bh
);
675 usb_autopm_put_interface(dev
->intf
);
679 EXPORT_SYMBOL_GPL(usbnet_stop
);
681 /*-------------------------------------------------------------------------*/
683 // posts reads, and enables write queuing
685 // precondition: never called in_interrupt
687 int usbnet_open (struct net_device
*net
)
689 struct usbnet
*dev
= netdev_priv(net
);
691 struct driver_info
*info
= dev
->driver_info
;
693 if ((retval
= usb_autopm_get_interface(dev
->intf
)) < 0) {
694 if (netif_msg_ifup (dev
))
696 "resumption fail (%d) usbnet usb-%s-%s, %s",
698 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
703 // put into "known safe" state
704 if (info
->reset
&& (retval
= info
->reset (dev
)) < 0) {
705 if (netif_msg_ifup (dev
))
707 "open reset fail (%d) usbnet usb-%s-%s, %s",
709 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
714 // insist peer be connected
715 if (info
->check_connect
&& (retval
= info
->check_connect (dev
)) < 0) {
716 if (netif_msg_ifup (dev
))
717 devdbg (dev
, "can't open; %d", retval
);
721 /* start any status interrupt transfer */
722 if (dev
->interrupt
) {
723 retval
= usb_submit_urb (dev
->interrupt
, GFP_KERNEL
);
725 if (netif_msg_ifup (dev
))
726 deverr (dev
, "intr submit %d", retval
);
731 netif_start_queue (net
);
732 if (netif_msg_ifup (dev
)) {
735 if (dev
->driver_info
->flags
& FLAG_FRAMING_NC
)
737 else if (dev
->driver_info
->flags
& FLAG_FRAMING_GL
)
739 else if (dev
->driver_info
->flags
& FLAG_FRAMING_Z
)
741 else if (dev
->driver_info
->flags
& FLAG_FRAMING_RN
)
743 else if (dev
->driver_info
->flags
& FLAG_FRAMING_AX
)
748 devinfo (dev
, "open: enable queueing "
749 "(rx %d, tx %d) mtu %d %s framing",
750 (int)RX_QLEN (dev
), (int)TX_QLEN (dev
), dev
->net
->mtu
,
754 // delay posting reads until we're fully open
755 tasklet_schedule (&dev
->bh
);
758 usb_autopm_put_interface(dev
->intf
);
762 EXPORT_SYMBOL_GPL(usbnet_open
);
764 /*-------------------------------------------------------------------------*/
766 /* ethtool methods; minidrivers may need to add some more, but
767 * they'll probably want to use this base set.
770 int usbnet_get_settings (struct net_device
*net
, struct ethtool_cmd
*cmd
)
772 struct usbnet
*dev
= netdev_priv(net
);
774 if (!dev
->mii
.mdio_read
)
777 return mii_ethtool_gset(&dev
->mii
, cmd
);
779 EXPORT_SYMBOL_GPL(usbnet_get_settings
);
781 int usbnet_set_settings (struct net_device
*net
, struct ethtool_cmd
*cmd
)
783 struct usbnet
*dev
= netdev_priv(net
);
786 if (!dev
->mii
.mdio_write
)
789 retval
= mii_ethtool_sset(&dev
->mii
, cmd
);
791 /* link speed/duplex might have changed */
792 if (dev
->driver_info
->link_reset
)
793 dev
->driver_info
->link_reset(dev
);
798 EXPORT_SYMBOL_GPL(usbnet_set_settings
);
800 u32
usbnet_get_link (struct net_device
*net
)
802 struct usbnet
*dev
= netdev_priv(net
);
804 /* If a check_connect is defined, return its result */
805 if (dev
->driver_info
->check_connect
)
806 return dev
->driver_info
->check_connect (dev
) == 0;
808 /* if the device has mii operations, use those */
809 if (dev
->mii
.mdio_read
)
810 return mii_link_ok(&dev
->mii
);
812 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
813 return ethtool_op_get_link(net
);
815 EXPORT_SYMBOL_GPL(usbnet_get_link
);
817 int usbnet_nway_reset(struct net_device
*net
)
819 struct usbnet
*dev
= netdev_priv(net
);
821 if (!dev
->mii
.mdio_write
)
824 return mii_nway_restart(&dev
->mii
);
826 EXPORT_SYMBOL_GPL(usbnet_nway_reset
);
828 void usbnet_get_drvinfo (struct net_device
*net
, struct ethtool_drvinfo
*info
)
830 struct usbnet
*dev
= netdev_priv(net
);
832 strncpy (info
->driver
, dev
->driver_name
, sizeof info
->driver
);
833 strncpy (info
->version
, DRIVER_VERSION
, sizeof info
->version
);
834 strncpy (info
->fw_version
, dev
->driver_info
->description
,
835 sizeof info
->fw_version
);
836 usb_make_path (dev
->udev
, info
->bus_info
, sizeof info
->bus_info
);
838 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo
);
840 u32
usbnet_get_msglevel (struct net_device
*net
)
842 struct usbnet
*dev
= netdev_priv(net
);
844 return dev
->msg_enable
;
846 EXPORT_SYMBOL_GPL(usbnet_get_msglevel
);
848 void usbnet_set_msglevel (struct net_device
*net
, u32 level
)
850 struct usbnet
*dev
= netdev_priv(net
);
852 dev
->msg_enable
= level
;
854 EXPORT_SYMBOL_GPL(usbnet_set_msglevel
);
856 /* drivers may override default ethtool_ops in their bind() routine */
857 static const struct ethtool_ops usbnet_ethtool_ops
= {
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 .get_drvinfo
= usbnet_get_drvinfo
,
863 .get_msglevel
= usbnet_get_msglevel
,
864 .set_msglevel
= usbnet_set_msglevel
,
867 /*-------------------------------------------------------------------------*/
869 /* work that cannot be done in interrupt context uses keventd.
871 * NOTE: with 2.5 we could do more of this using completion callbacks,
872 * especially now that control transfers can be queued.
875 kevent (struct work_struct
*work
)
878 container_of(work
, struct usbnet
, kevent
);
881 /* usb_clear_halt() needs a thread context */
882 if (test_bit (EVENT_TX_HALT
, &dev
->flags
)) {
883 unlink_urbs (dev
, &dev
->txq
);
884 status
= usb_clear_halt (dev
->udev
, dev
->out
);
887 && status
!= -ESHUTDOWN
) {
888 if (netif_msg_tx_err (dev
))
889 deverr (dev
, "can't clear tx halt, status %d",
892 clear_bit (EVENT_TX_HALT
, &dev
->flags
);
893 if (status
!= -ESHUTDOWN
)
894 netif_wake_queue (dev
->net
);
897 if (test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
898 unlink_urbs (dev
, &dev
->rxq
);
899 status
= usb_clear_halt (dev
->udev
, dev
->in
);
902 && status
!= -ESHUTDOWN
) {
903 if (netif_msg_rx_err (dev
))
904 deverr (dev
, "can't clear rx halt, status %d",
907 clear_bit (EVENT_RX_HALT
, &dev
->flags
);
908 tasklet_schedule (&dev
->bh
);
912 /* tasklet could resubmit itself forever if memory is tight */
913 if (test_bit (EVENT_RX_MEMORY
, &dev
->flags
)) {
914 struct urb
*urb
= NULL
;
916 if (netif_running (dev
->net
))
917 urb
= usb_alloc_urb (0, GFP_KERNEL
);
919 clear_bit (EVENT_RX_MEMORY
, &dev
->flags
);
921 clear_bit (EVENT_RX_MEMORY
, &dev
->flags
);
922 rx_submit (dev
, urb
, GFP_KERNEL
);
923 tasklet_schedule (&dev
->bh
);
927 if (test_bit (EVENT_LINK_RESET
, &dev
->flags
)) {
928 struct driver_info
*info
= dev
->driver_info
;
931 clear_bit (EVENT_LINK_RESET
, &dev
->flags
);
932 if(info
->link_reset
&& (retval
= info
->link_reset(dev
)) < 0) {
933 devinfo(dev
, "link reset failed (%d) usbnet usb-%s-%s, %s",
935 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
941 devdbg (dev
, "kevent done, flags = 0x%lx",
945 /*-------------------------------------------------------------------------*/
947 static void tx_complete (struct urb
*urb
)
949 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
950 struct skb_data
*entry
= (struct skb_data
*) skb
->cb
;
951 struct usbnet
*dev
= entry
->dev
;
953 if (urb
->status
== 0) {
954 dev
->net
->stats
.tx_packets
++;
955 dev
->net
->stats
.tx_bytes
+= entry
->length
;
957 dev
->net
->stats
.tx_errors
++;
959 switch (urb
->status
) {
961 usbnet_defer_kevent (dev
, EVENT_TX_HALT
);
964 /* software-driven interface shutdown */
965 case -ECONNRESET
: // async unlink
966 case -ESHUTDOWN
: // hardware gone
969 // like rx, tx gets controller i/o faults during khubd delays
970 // and so it uses the same throttling mechanism.
974 if (!timer_pending (&dev
->delay
)) {
975 mod_timer (&dev
->delay
,
976 jiffies
+ THROTTLE_JIFFIES
);
977 if (netif_msg_link (dev
))
978 devdbg (dev
, "tx throttle %d",
981 netif_stop_queue (dev
->net
);
984 if (netif_msg_tx_err (dev
))
985 devdbg (dev
, "tx err %d", entry
->urb
->status
);
991 entry
->state
= tx_done
;
992 defer_bh(dev
, skb
, &dev
->txq
);
995 /*-------------------------------------------------------------------------*/
997 void usbnet_tx_timeout (struct net_device
*net
)
999 struct usbnet
*dev
= netdev_priv(net
);
1001 unlink_urbs (dev
, &dev
->txq
);
1002 tasklet_schedule (&dev
->bh
);
1004 // FIXME: device recovery -- reset?
1006 EXPORT_SYMBOL_GPL(usbnet_tx_timeout
);
1008 /*-------------------------------------------------------------------------*/
1010 netdev_tx_t
usbnet_start_xmit (struct sk_buff
*skb
,
1011 struct net_device
*net
)
1013 struct usbnet
*dev
= netdev_priv(net
);
1015 struct urb
*urb
= NULL
;
1016 struct skb_data
*entry
;
1017 struct driver_info
*info
= dev
->driver_info
;
1018 unsigned long flags
;
1021 // some devices want funky USB-level framing, for
1022 // win32 driver (usually) and/or hardware quirks
1023 if (info
->tx_fixup
) {
1024 skb
= info
->tx_fixup (dev
, skb
, GFP_ATOMIC
);
1026 if (netif_msg_tx_err (dev
))
1027 devdbg (dev
, "can't tx_fixup skb");
1033 if (!(urb
= usb_alloc_urb (0, GFP_ATOMIC
))) {
1034 if (netif_msg_tx_err (dev
))
1035 devdbg (dev
, "no urb");
1039 entry
= (struct skb_data
*) skb
->cb
;
1042 entry
->state
= tx_start
;
1043 entry
->length
= length
;
1045 usb_fill_bulk_urb (urb
, dev
->udev
, dev
->out
,
1046 skb
->data
, skb
->len
, tx_complete
, skb
);
1048 /* don't assume the hardware handles USB_ZERO_PACKET
1049 * NOTE: strictly conforming cdc-ether devices should expect
1050 * the ZLP here, but ignore the one-byte packet.
1052 if (!(info
->flags
& FLAG_SEND_ZLP
) && (length
% dev
->maxpacket
) == 0) {
1053 urb
->transfer_buffer_length
++;
1054 if (skb_tailroom(skb
)) {
1055 skb
->data
[skb
->len
] = 0;
1060 spin_lock_irqsave (&dev
->txq
.lock
, flags
);
1062 switch ((retval
= usb_submit_urb (urb
, GFP_ATOMIC
))) {
1064 netif_stop_queue (net
);
1065 usbnet_defer_kevent (dev
, EVENT_TX_HALT
);
1068 if (netif_msg_tx_err (dev
))
1069 devdbg (dev
, "tx: submit urb err %d", retval
);
1072 net
->trans_start
= jiffies
;
1073 __skb_queue_tail (&dev
->txq
, skb
);
1074 if (dev
->txq
.qlen
>= TX_QLEN (dev
))
1075 netif_stop_queue (net
);
1077 spin_unlock_irqrestore (&dev
->txq
.lock
, flags
);
1080 if (netif_msg_tx_err (dev
))
1081 devdbg (dev
, "drop, code %d", retval
);
1083 dev
->net
->stats
.tx_dropped
++;
1085 dev_kfree_skb_any (skb
);
1087 } else if (netif_msg_tx_queued (dev
)) {
1088 devdbg (dev
, "> tx, len %d, type 0x%x",
1089 length
, skb
->protocol
);
1091 return NETDEV_TX_OK
;
1093 EXPORT_SYMBOL_GPL(usbnet_start_xmit
);
1095 /*-------------------------------------------------------------------------*/
1097 // tasklet (work deferred from completions, in_irq) or timer
1099 static void usbnet_bh (unsigned long param
)
1101 struct usbnet
*dev
= (struct usbnet
*) param
;
1102 struct sk_buff
*skb
;
1103 struct skb_data
*entry
;
1105 while ((skb
= skb_dequeue (&dev
->done
))) {
1106 entry
= (struct skb_data
*) skb
->cb
;
1107 switch (entry
->state
) {
1109 entry
->state
= rx_cleanup
;
1110 rx_process (dev
, skb
);
1114 usb_free_urb (entry
->urb
);
1115 dev_kfree_skb (skb
);
1118 devdbg (dev
, "bogus skb state %d", entry
->state
);
1122 // waiting for all pending urbs to complete?
1124 if ((dev
->txq
.qlen
+ dev
->rxq
.qlen
+ dev
->done
.qlen
) == 0) {
1125 wake_up (dev
->wait
);
1128 // or are we maybe short a few urbs?
1129 } else if (netif_running (dev
->net
)
1130 && netif_device_present (dev
->net
)
1131 && !timer_pending (&dev
->delay
)
1132 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
1133 int temp
= dev
->rxq
.qlen
;
1134 int qlen
= RX_QLEN (dev
);
1140 // don't refill the queue all at once
1141 for (i
= 0; i
< 10 && dev
->rxq
.qlen
< qlen
; i
++) {
1142 urb
= usb_alloc_urb (0, GFP_ATOMIC
);
1144 rx_submit (dev
, urb
, GFP_ATOMIC
);
1146 if (temp
!= dev
->rxq
.qlen
&& netif_msg_link (dev
))
1147 devdbg (dev
, "rxqlen %d --> %d",
1148 temp
, dev
->rxq
.qlen
);
1149 if (dev
->rxq
.qlen
< qlen
)
1150 tasklet_schedule (&dev
->bh
);
1152 if (dev
->txq
.qlen
< TX_QLEN (dev
))
1153 netif_wake_queue (dev
->net
);
1158 /*-------------------------------------------------------------------------
1160 * USB Device Driver support
1162 *-------------------------------------------------------------------------*/
1164 // precondition: never called in_interrupt
1166 void usbnet_disconnect (struct usb_interface
*intf
)
1169 struct usb_device
*xdev
;
1170 struct net_device
*net
;
1172 dev
= usb_get_intfdata(intf
);
1173 usb_set_intfdata(intf
, NULL
);
1177 xdev
= interface_to_usbdev (intf
);
1179 if (netif_msg_probe (dev
))
1180 devinfo (dev
, "unregister '%s' usb-%s-%s, %s",
1181 intf
->dev
.driver
->name
,
1182 xdev
->bus
->bus_name
, xdev
->devpath
,
1183 dev
->driver_info
->description
);
1186 unregister_netdev (net
);
1188 /* we don't hold rtnl here ... */
1189 flush_scheduled_work ();
1191 if (dev
->driver_info
->unbind
)
1192 dev
->driver_info
->unbind (dev
, intf
);
1197 EXPORT_SYMBOL_GPL(usbnet_disconnect
);
1199 static const struct net_device_ops usbnet_netdev_ops
= {
1200 .ndo_open
= usbnet_open
,
1201 .ndo_stop
= usbnet_stop
,
1202 .ndo_start_xmit
= usbnet_start_xmit
,
1203 .ndo_tx_timeout
= usbnet_tx_timeout
,
1204 .ndo_change_mtu
= usbnet_change_mtu
,
1205 .ndo_set_mac_address
= eth_mac_addr
,
1206 .ndo_validate_addr
= eth_validate_addr
,
1209 /*-------------------------------------------------------------------------*/
1211 // precondition: never called in_interrupt
1213 static struct device_type wlan_type
= {
1217 static struct device_type wwan_type
= {
1222 usbnet_probe (struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1225 struct net_device
*net
;
1226 struct usb_host_interface
*interface
;
1227 struct driver_info
*info
;
1228 struct usb_device
*xdev
;
1232 name
= udev
->dev
.driver
->name
;
1233 info
= (struct driver_info
*) prod
->driver_info
;
1235 dev_dbg (&udev
->dev
, "blacklisted by %s\n", name
);
1238 xdev
= interface_to_usbdev (udev
);
1239 interface
= udev
->cur_altsetting
;
1245 // set up our own records
1246 net
= alloc_etherdev(sizeof(*dev
));
1248 dbg ("can't kmalloc dev");
1252 dev
= netdev_priv(net
);
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 skb_queue_head_init(&dev
->rxq_pause
);
1263 dev
->bh
.func
= usbnet_bh
;
1264 dev
->bh
.data
= (unsigned long) dev
;
1265 INIT_WORK (&dev
->kevent
, kevent
);
1266 dev
->delay
.function
= usbnet_bh
;
1267 dev
->delay
.data
= (unsigned long) dev
;
1268 init_timer (&dev
->delay
);
1269 mutex_init (&dev
->phy_mutex
);
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
;
1280 // dma_supported() is deeply broken on almost all architectures
1281 // possible with some EHCI controllers
1282 if (dma_supported (&udev
->dev
, DMA_BIT_MASK(64)))
1283 net
->features
|= NETIF_F_HIGHDMA
;
1286 net
->netdev_ops
= &usbnet_netdev_ops
;
1287 net
->watchdog_timeo
= TX_TIMEOUT_JIFFIES
;
1288 net
->ethtool_ops
= &usbnet_ethtool_ops
;
1290 // allow device-specific bind/init procedures
1291 // NOTE net->name still not usable ...
1293 status
= info
->bind (dev
, udev
);
1297 // heuristic: "usb%d" for links we know are two-host,
1298 // else "eth%d" when there's reasonable doubt. userspace
1299 // can rename the link if it knows better.
1300 if ((dev
->driver_info
->flags
& FLAG_ETHER
) != 0
1301 && (net
->dev_addr
[0] & 0x02) == 0)
1302 strcpy (net
->name
, "eth%d");
1303 /* WLAN devices should always be named "wlan%d" */
1304 if ((dev
->driver_info
->flags
& FLAG_WLAN
) != 0)
1305 strcpy(net
->name
, "wlan%d");
1306 /* WWAN devices should always be named "wwan%d" */
1307 if ((dev
->driver_info
->flags
& FLAG_WWAN
) != 0)
1308 strcpy(net
->name
, "wwan%d");
1310 /* maybe the remote can't receive an Ethernet MTU */
1311 if (net
->mtu
> (dev
->hard_mtu
- net
->hard_header_len
))
1312 net
->mtu
= dev
->hard_mtu
- net
->hard_header_len
;
1313 } else if (!info
->in
|| !info
->out
)
1314 status
= usbnet_get_endpoints (dev
, udev
);
1316 dev
->in
= usb_rcvbulkpipe (xdev
, info
->in
);
1317 dev
->out
= usb_sndbulkpipe (xdev
, info
->out
);
1318 if (!(info
->flags
& FLAG_NO_SETINT
))
1319 status
= usb_set_interface (xdev
,
1320 interface
->desc
.bInterfaceNumber
,
1321 interface
->desc
.bAlternateSetting
);
1326 if (status
>= 0 && dev
->status
)
1327 status
= init_status (dev
, udev
);
1331 if (!dev
->rx_urb_size
)
1332 dev
->rx_urb_size
= dev
->hard_mtu
;
1333 dev
->maxpacket
= usb_maxpacket (dev
->udev
, dev
->out
, 1);
1335 SET_NETDEV_DEV(net
, &udev
->dev
);
1337 if ((dev
->driver_info
->flags
& FLAG_WLAN
) != 0)
1338 SET_NETDEV_DEVTYPE(net
, &wlan_type
);
1339 if ((dev
->driver_info
->flags
& FLAG_WWAN
) != 0)
1340 SET_NETDEV_DEVTYPE(net
, &wwan_type
);
1342 status
= register_netdev (net
);
1345 if (netif_msg_probe (dev
))
1346 devinfo (dev
, "register '%s' at usb-%s-%s, %s, %pM",
1347 udev
->dev
.driver
->name
,
1348 xdev
->bus
->bus_name
, xdev
->devpath
,
1349 dev
->driver_info
->description
,
1352 // ok, it's ready to go.
1353 usb_set_intfdata (udev
, dev
);
1355 netif_device_attach (net
);
1357 if (dev
->driver_info
->flags
& FLAG_LINK_INTR
)
1358 netif_carrier_off(net
);
1364 info
->unbind (dev
, udev
);
1371 EXPORT_SYMBOL_GPL(usbnet_probe
);
1373 /*-------------------------------------------------------------------------*/
1376 * suspend the whole driver as soon as the first interface is suspended
1377 * resume only when the last interface is resumed
1380 int usbnet_suspend (struct usb_interface
*intf
, pm_message_t message
)
1382 struct usbnet
*dev
= usb_get_intfdata(intf
);
1384 if (!dev
->suspend_count
++) {
1386 * accelerate emptying of the rx and queues, to avoid
1387 * having everything error out.
1389 netif_device_detach (dev
->net
);
1390 (void) unlink_urbs (dev
, &dev
->rxq
);
1391 (void) unlink_urbs (dev
, &dev
->txq
);
1393 * reattach so runtime management can use and
1396 netif_device_attach (dev
->net
);
1400 EXPORT_SYMBOL_GPL(usbnet_suspend
);
1402 int usbnet_resume (struct usb_interface
*intf
)
1404 struct usbnet
*dev
= usb_get_intfdata(intf
);
1406 if (!--dev
->suspend_count
)
1407 tasklet_schedule (&dev
->bh
);
1411 EXPORT_SYMBOL_GPL(usbnet_resume
);
1414 /*-------------------------------------------------------------------------*/
1416 static int __init
usbnet_init(void)
1418 /* compiler should optimize this out */
1419 BUILD_BUG_ON (sizeof (((struct sk_buff
*)0)->cb
)
1420 < sizeof (struct skb_data
));
1422 random_ether_addr(node_id
);
1425 module_init(usbnet_init
);
1427 static void __exit
usbnet_exit(void)
1430 module_exit(usbnet_exit
);
1432 MODULE_AUTHOR("David Brownell");
1433 MODULE_DESCRIPTION("USB network driver framework");
1434 MODULE_LICENSE("GPL");