2 * USB Network driver infrastructure
3 * Copyright (C) 2000-2005 by David Brownell
4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This is a generic "USB networking" framework that works with several
23 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
26 * These devices usually differ in terms of control protocols (if they
27 * even have one!) and sometimes they define new framing to wrap or batch
28 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
29 * so interface (un)binding, endpoint I/O queues, fault handling, and other
30 * issues can usefully be addressed by this framework.
33 // #define DEBUG // error path messages, extra info
34 // #define VERBOSE // more; success messages
36 #include <linux/config.h>
37 #ifdef CONFIG_USB_DEBUG
40 #include <linux/module.h>
41 #include <linux/sched.h>
42 #include <linux/init.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/ethtool.h>
46 #include <linux/workqueue.h>
47 #include <linux/mii.h>
48 #include <linux/usb.h>
52 #define DRIVER_VERSION "22-Aug-2005"
55 /*-------------------------------------------------------------------------*/
58 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
59 * Several dozen bytes of IPv4 data can fit in two such transactions.
60 * One maximum size Ethernet packet takes twenty four of them.
61 * For high speed, each frame comfortably fits almost 36 max size
62 * Ethernet packets (so queues should be bigger).
64 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
65 * let the USB host controller be busy for 5msec or more before an irq
66 * is required, under load. Jumbograms change the equation.
68 #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
69 #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
71 // reawaken network queue this soon after stopping; else watchdog barks
72 #define TX_TIMEOUT_JIFFIES (5*HZ)
74 // throttle rx/tx briefly after some faults, so khubd might disconnect()
75 // us (it polls at HZ/4 usually) before we report too many false errors.
76 #define THROTTLE_JIFFIES (HZ/8)
79 #define UNLINK_TIMEOUT_MS 3
81 /*-------------------------------------------------------------------------*/
83 // randomly generated ethernet address
84 static u8 node_id
[ETH_ALEN
];
86 static const char driver_name
[] = "usbnet";
88 /* use ethtool to change the level for any given device */
89 static int msg_level
= -1;
90 module_param (msg_level
, int, 0);
91 MODULE_PARM_DESC (msg_level
, "Override default message level");
93 /*-------------------------------------------------------------------------*/
95 /* handles CDC Ethernet and many other network "bulk data" interfaces */
96 int usbnet_get_endpoints(struct usbnet
*dev
, struct usb_interface
*intf
)
99 struct usb_host_interface
*alt
= NULL
;
100 struct usb_host_endpoint
*in
= NULL
, *out
= NULL
;
101 struct usb_host_endpoint
*status
= NULL
;
103 for (tmp
= 0; tmp
< intf
->num_altsetting
; tmp
++) {
106 in
= out
= status
= NULL
;
107 alt
= intf
->altsetting
+ tmp
;
109 /* take the first altsetting with in-bulk + out-bulk;
110 * remember any status endpoint, just in case;
111 * ignore other endpoints and altsetttings.
113 for (ep
= 0; ep
< alt
->desc
.bNumEndpoints
; ep
++) {
114 struct usb_host_endpoint
*e
;
117 e
= alt
->endpoint
+ ep
;
118 switch (e
->desc
.bmAttributes
) {
119 case USB_ENDPOINT_XFER_INT
:
120 if (!(e
->desc
.bEndpointAddress
& USB_DIR_IN
))
124 case USB_ENDPOINT_XFER_BULK
:
129 if (e
->desc
.bEndpointAddress
& USB_DIR_IN
) {
132 else if (intr
&& !status
)
142 if (!alt
|| !in
|| !out
)
145 if (alt
->desc
.bAlternateSetting
!= 0
146 || !(dev
->driver_info
->flags
& FLAG_NO_SETINT
)) {
147 tmp
= usb_set_interface (dev
->udev
, alt
->desc
.bInterfaceNumber
,
148 alt
->desc
.bAlternateSetting
);
153 dev
->in
= usb_rcvbulkpipe (dev
->udev
,
154 in
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
155 dev
->out
= usb_sndbulkpipe (dev
->udev
,
156 out
->desc
.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
157 dev
->status
= status
;
160 EXPORT_SYMBOL_GPL(usbnet_get_endpoints
);
162 static void intr_complete (struct urb
*urb
, struct pt_regs
*regs
);
164 static int init_status (struct usbnet
*dev
, struct usb_interface
*intf
)
171 if (!dev
->driver_info
->status
)
174 pipe
= usb_rcvintpipe (dev
->udev
,
175 dev
->status
->desc
.bEndpointAddress
176 & USB_ENDPOINT_NUMBER_MASK
);
177 maxp
= usb_maxpacket (dev
->udev
, pipe
, 0);
179 /* avoid 1 msec chatter: min 8 msec poll rate */
180 period
= max ((int) dev
->status
->desc
.bInterval
,
181 (dev
->udev
->speed
== USB_SPEED_HIGH
) ? 7 : 3);
183 buf
= kmalloc (maxp
, SLAB_KERNEL
);
185 dev
->interrupt
= usb_alloc_urb (0, SLAB_KERNEL
);
186 if (!dev
->interrupt
) {
190 usb_fill_int_urb(dev
->interrupt
, dev
->udev
, pipe
,
191 buf
, maxp
, intr_complete
, dev
, period
);
193 "status ep%din, %d bytes period %d\n",
194 usb_pipeendpoint(pipe
), maxp
, period
);
200 /* Passes this packet up the stack, updating its accounting.
201 * Some link protocols batch packets, so their rx_fixup paths
202 * can return clones as well as just modify the original skb.
204 void usbnet_skb_return (struct usbnet
*dev
, struct sk_buff
*skb
)
209 skb
->protocol
= eth_type_trans (skb
, dev
->net
);
210 dev
->stats
.rx_packets
++;
211 dev
->stats
.rx_bytes
+= skb
->len
;
213 if (netif_msg_rx_status (dev
))
214 devdbg (dev
, "< rx, len %zu, type 0x%x",
215 skb
->len
+ sizeof (struct ethhdr
), skb
->protocol
);
216 memset (skb
->cb
, 0, sizeof (struct skb_data
));
217 status
= netif_rx (skb
);
218 if (status
!= NET_RX_SUCCESS
&& netif_msg_rx_err (dev
))
219 devdbg (dev
, "netif_rx status %d", status
);
221 EXPORT_SYMBOL_GPL(usbnet_skb_return
);
224 /*-------------------------------------------------------------------------
226 * Network Device Driver (peer link to "Host Device", from USB host)
228 *-------------------------------------------------------------------------*/
230 static int usbnet_change_mtu (struct net_device
*net
, int new_mtu
)
232 struct usbnet
*dev
= netdev_priv(net
);
233 int ll_mtu
= new_mtu
+ net
->hard_header_len
;
235 if (new_mtu
<= 0 || ll_mtu
> dev
->hard_mtu
)
237 // no second zero-length packet read wanted after mtu-sized packets
238 if ((ll_mtu
% dev
->maxpacket
) == 0)
244 /*-------------------------------------------------------------------------*/
246 static struct net_device_stats
*usbnet_get_stats (struct net_device
*net
)
248 struct usbnet
*dev
= netdev_priv(net
);
252 /*-------------------------------------------------------------------------*/
254 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
255 * completion callbacks. 2.5 should have fixed those bugs...
258 static void defer_bh(struct usbnet
*dev
, struct sk_buff
*skb
, struct sk_buff_head
*list
)
262 spin_lock_irqsave(&list
->lock
, flags
);
263 __skb_unlink(skb
, list
);
264 spin_unlock(&list
->lock
);
265 spin_lock(&dev
->done
.lock
);
266 __skb_queue_tail(&dev
->done
, skb
);
267 if (dev
->done
.qlen
== 1)
268 tasklet_schedule(&dev
->bh
);
269 spin_unlock_irqrestore(&dev
->done
.lock
, flags
);
272 /* some work can't be done in tasklets, so we use keventd
274 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
275 * but tasklet_schedule() doesn't. hope the failure is rare.
277 void usbnet_defer_kevent (struct usbnet
*dev
, int work
)
279 set_bit (work
, &dev
->flags
);
280 if (!schedule_work (&dev
->kevent
))
281 deverr (dev
, "kevent %d may have been dropped", work
);
283 devdbg (dev
, "kevent %d scheduled", work
);
285 EXPORT_SYMBOL_GPL(usbnet_defer_kevent
);
287 /*-------------------------------------------------------------------------*/
289 static void rx_complete (struct urb
*urb
, struct pt_regs
*regs
);
291 static void rx_submit (struct usbnet
*dev
, struct urb
*urb
, unsigned flags
)
294 struct skb_data
*entry
;
296 unsigned long lockflags
;
297 size_t size
= dev
->rx_urb_size
;
299 if ((skb
= alloc_skb (size
+ NET_IP_ALIGN
, flags
)) == NULL
) {
300 if (netif_msg_rx_err (dev
))
301 devdbg (dev
, "no rx skb");
302 usbnet_defer_kevent (dev
, EVENT_RX_MEMORY
);
306 skb_reserve (skb
, NET_IP_ALIGN
);
308 entry
= (struct skb_data
*) skb
->cb
;
311 entry
->state
= rx_start
;
314 usb_fill_bulk_urb (urb
, dev
->udev
, dev
->in
,
315 skb
->data
, size
, rx_complete
, skb
);
317 spin_lock_irqsave (&dev
->rxq
.lock
, lockflags
);
319 if (netif_running (dev
->net
)
320 && netif_device_present (dev
->net
)
321 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
322 switch (retval
= usb_submit_urb (urb
, GFP_ATOMIC
)){
324 usbnet_defer_kevent (dev
, EVENT_RX_HALT
);
327 usbnet_defer_kevent (dev
, EVENT_RX_MEMORY
);
330 if (netif_msg_ifdown (dev
))
331 devdbg (dev
, "device gone");
332 netif_device_detach (dev
->net
);
335 if (netif_msg_rx_err (dev
))
336 devdbg (dev
, "rx submit, %d", retval
);
337 tasklet_schedule (&dev
->bh
);
340 __skb_queue_tail (&dev
->rxq
, skb
);
343 if (netif_msg_ifdown (dev
))
344 devdbg (dev
, "rx: stopped");
347 spin_unlock_irqrestore (&dev
->rxq
.lock
, lockflags
);
349 dev_kfree_skb_any (skb
);
355 /*-------------------------------------------------------------------------*/
357 static inline void rx_process (struct usbnet
*dev
, struct sk_buff
*skb
)
359 if (dev
->driver_info
->rx_fixup
360 && !dev
->driver_info
->rx_fixup (dev
, skb
))
362 // else network stack removes extra byte if we forced a short packet
365 usbnet_skb_return (dev
, skb
);
367 if (netif_msg_rx_err (dev
))
368 devdbg (dev
, "drop");
370 dev
->stats
.rx_errors
++;
371 skb_queue_tail (&dev
->done
, skb
);
375 /*-------------------------------------------------------------------------*/
377 static void rx_complete (struct urb
*urb
, struct pt_regs
*regs
)
379 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
380 struct skb_data
*entry
= (struct skb_data
*) skb
->cb
;
381 struct usbnet
*dev
= entry
->dev
;
382 int urb_status
= urb
->status
;
384 skb_put (skb
, urb
->actual_length
);
385 entry
->state
= rx_done
;
388 switch (urb_status
) {
391 if (skb
->len
< dev
->net
->hard_header_len
) {
392 entry
->state
= rx_cleanup
;
393 dev
->stats
.rx_errors
++;
394 dev
->stats
.rx_length_errors
++;
395 if (netif_msg_rx_err (dev
))
396 devdbg (dev
, "rx length %d", skb
->len
);
400 // stalls need manual reset. this is rare ... except that
401 // when going through USB 2.0 TTs, unplug appears this way.
402 // we avoid the highspeed version of the ETIMEOUT/EILSEQ
403 // storm, recovering as needed.
405 dev
->stats
.rx_errors
++;
406 usbnet_defer_kevent (dev
, EVENT_RX_HALT
);
409 // software-driven interface shutdown
410 case -ECONNRESET
: // async unlink
411 case -ESHUTDOWN
: // hardware gone
412 if (netif_msg_ifdown (dev
))
413 devdbg (dev
, "rx shutdown, code %d", urb_status
);
416 // we get controller i/o faults during khubd disconnect() delays.
417 // throttle down resubmits, to avoid log floods; just temporarily,
418 // so we still recover when the fault isn't a khubd delay.
419 case -EPROTO
: // ehci
420 case -ETIMEDOUT
: // ohci
421 case -EILSEQ
: // uhci
422 dev
->stats
.rx_errors
++;
423 if (!timer_pending (&dev
->delay
)) {
424 mod_timer (&dev
->delay
, jiffies
+ THROTTLE_JIFFIES
);
425 if (netif_msg_link (dev
))
426 devdbg (dev
, "rx throttle %d", urb_status
);
429 entry
->state
= rx_cleanup
;
434 // data overrun ... flush fifo?
436 dev
->stats
.rx_over_errors
++;
440 entry
->state
= rx_cleanup
;
441 dev
->stats
.rx_errors
++;
442 if (netif_msg_rx_err (dev
))
443 devdbg (dev
, "rx status %d", urb_status
);
447 defer_bh(dev
, skb
, &dev
->rxq
);
450 if (netif_running (dev
->net
)
451 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
452 rx_submit (dev
, urb
, GFP_ATOMIC
);
457 if (netif_msg_rx_err (dev
))
458 devdbg (dev
, "no read resubmitted");
461 static void intr_complete (struct urb
*urb
, struct pt_regs
*regs
)
463 struct usbnet
*dev
= urb
->context
;
464 int status
= urb
->status
;
469 dev
->driver_info
->status(dev
, urb
);
472 /* software-driven interface shutdown */
473 case -ENOENT
: // urb killed
474 case -ESHUTDOWN
: // hardware gone
475 if (netif_msg_ifdown (dev
))
476 devdbg (dev
, "intr shutdown, code %d", status
);
479 /* NOTE: not throttling like RX/TX, since this endpoint
480 * already polls infrequently
483 devdbg (dev
, "intr status %d", status
);
487 if (!netif_running (dev
->net
))
490 memset(urb
->transfer_buffer
, 0, urb
->transfer_buffer_length
);
491 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
492 if (status
!= 0 && netif_msg_timer (dev
))
493 deverr(dev
, "intr resubmit --> %d", status
);
496 /*-------------------------------------------------------------------------*/
498 // unlink pending rx/tx; completion handlers do all other cleanup
500 static int unlink_urbs (struct usbnet
*dev
, struct sk_buff_head
*q
)
503 struct sk_buff
*skb
, *skbnext
;
506 spin_lock_irqsave (&q
->lock
, flags
);
507 for (skb
= q
->next
; skb
!= (struct sk_buff
*) q
; skb
= skbnext
) {
508 struct skb_data
*entry
;
512 entry
= (struct skb_data
*) skb
->cb
;
516 // during some PM-driven resume scenarios,
517 // these (async) unlinks complete immediately
518 retval
= usb_unlink_urb (urb
);
519 if (retval
!= -EINPROGRESS
&& retval
!= 0)
520 devdbg (dev
, "unlink urb err, %d", retval
);
524 spin_unlock_irqrestore (&q
->lock
, flags
);
529 /*-------------------------------------------------------------------------*/
531 // precondition: never called in_interrupt
533 static int usbnet_stop (struct net_device
*net
)
535 struct usbnet
*dev
= netdev_priv(net
);
537 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup
);
538 DECLARE_WAITQUEUE (wait
, current
);
540 netif_stop_queue (net
);
542 if (netif_msg_ifdown (dev
))
543 devinfo (dev
, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
544 dev
->stats
.rx_packets
, dev
->stats
.tx_packets
,
545 dev
->stats
.rx_errors
, dev
->stats
.tx_errors
548 // ensure there are no more active urbs
549 add_wait_queue (&unlink_wakeup
, &wait
);
550 dev
->wait
= &unlink_wakeup
;
551 temp
= unlink_urbs (dev
, &dev
->txq
) + unlink_urbs (dev
, &dev
->rxq
);
553 // maybe wait for deletions to finish.
554 while (!skb_queue_empty(&dev
->rxq
) &&
555 !skb_queue_empty(&dev
->txq
) &&
556 !skb_queue_empty(&dev
->done
)) {
557 msleep(UNLINK_TIMEOUT_MS
);
558 if (netif_msg_ifdown (dev
))
559 devdbg (dev
, "waited for %d urb completions", temp
);
562 remove_wait_queue (&unlink_wakeup
, &wait
);
564 usb_kill_urb(dev
->interrupt
);
566 /* deferred work (task, timer, softirq) must also stop.
567 * can't flush_scheduled_work() until we drop rtnl (later),
568 * else workers could deadlock; so make workers a NOP.
571 del_timer_sync (&dev
->delay
);
572 tasklet_kill (&dev
->bh
);
577 /*-------------------------------------------------------------------------*/
579 // posts reads, and enables write queuing
581 // precondition: never called in_interrupt
583 static int usbnet_open (struct net_device
*net
)
585 struct usbnet
*dev
= netdev_priv(net
);
587 struct driver_info
*info
= dev
->driver_info
;
589 // put into "known safe" state
590 if (info
->reset
&& (retval
= info
->reset (dev
)) < 0) {
591 if (netif_msg_ifup (dev
))
593 "open reset fail (%d) usbnet usb-%s-%s, %s",
595 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
600 // insist peer be connected
601 if (info
->check_connect
&& (retval
= info
->check_connect (dev
)) < 0) {
602 if (netif_msg_ifup (dev
))
603 devdbg (dev
, "can't open; %d", retval
);
607 /* start any status interrupt transfer */
608 if (dev
->interrupt
) {
609 retval
= usb_submit_urb (dev
->interrupt
, GFP_KERNEL
);
611 if (netif_msg_ifup (dev
))
612 deverr (dev
, "intr submit %d", retval
);
617 netif_start_queue (net
);
618 if (netif_msg_ifup (dev
)) {
621 if (dev
->driver_info
->flags
& FLAG_FRAMING_NC
)
623 else if (dev
->driver_info
->flags
& FLAG_FRAMING_GL
)
625 else if (dev
->driver_info
->flags
& FLAG_FRAMING_Z
)
627 else if (dev
->driver_info
->flags
& FLAG_FRAMING_RN
)
629 else if (dev
->driver_info
->flags
& FLAG_FRAMING_AX
)
634 devinfo (dev
, "open: enable queueing "
635 "(rx %d, tx %d) mtu %d %s framing",
636 RX_QLEN (dev
), TX_QLEN (dev
), dev
->net
->mtu
,
640 // delay posting reads until we're fully open
641 tasklet_schedule (&dev
->bh
);
646 /*-------------------------------------------------------------------------*/
648 /* ethtool methods; minidrivers may need to add some more, but
649 * they'll probably want to use this base set.
652 void usbnet_get_drvinfo (struct net_device
*net
, struct ethtool_drvinfo
*info
)
654 struct usbnet
*dev
= netdev_priv(net
);
656 /* REVISIT don't always return "usbnet" */
657 strncpy (info
->driver
, driver_name
, sizeof info
->driver
);
658 strncpy (info
->version
, DRIVER_VERSION
, sizeof info
->version
);
659 strncpy (info
->fw_version
, dev
->driver_info
->description
,
660 sizeof info
->fw_version
);
661 usb_make_path (dev
->udev
, info
->bus_info
, sizeof info
->bus_info
);
663 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo
);
665 static u32
usbnet_get_link (struct net_device
*net
)
667 struct usbnet
*dev
= netdev_priv(net
);
669 /* If a check_connect is defined, return its result */
670 if (dev
->driver_info
->check_connect
)
671 return dev
->driver_info
->check_connect (dev
) == 0;
673 /* Otherwise, say we're up (to avoid breaking scripts) */
677 u32
usbnet_get_msglevel (struct net_device
*net
)
679 struct usbnet
*dev
= netdev_priv(net
);
681 return dev
->msg_enable
;
683 EXPORT_SYMBOL_GPL(usbnet_get_msglevel
);
685 void usbnet_set_msglevel (struct net_device
*net
, u32 level
)
687 struct usbnet
*dev
= netdev_priv(net
);
689 dev
->msg_enable
= level
;
691 EXPORT_SYMBOL_GPL(usbnet_set_msglevel
);
693 /* drivers may override default ethtool_ops in their bind() routine */
694 static struct ethtool_ops usbnet_ethtool_ops
= {
695 .get_drvinfo
= usbnet_get_drvinfo
,
696 .get_link
= usbnet_get_link
,
697 .get_msglevel
= usbnet_get_msglevel
,
698 .set_msglevel
= usbnet_set_msglevel
,
701 /*-------------------------------------------------------------------------*/
703 /* work that cannot be done in interrupt context uses keventd.
705 * NOTE: with 2.5 we could do more of this using completion callbacks,
706 * especially now that control transfers can be queued.
711 struct usbnet
*dev
= data
;
714 /* usb_clear_halt() needs a thread context */
715 if (test_bit (EVENT_TX_HALT
, &dev
->flags
)) {
716 unlink_urbs (dev
, &dev
->txq
);
717 status
= usb_clear_halt (dev
->udev
, dev
->out
);
720 && status
!= -ESHUTDOWN
) {
721 if (netif_msg_tx_err (dev
))
722 deverr (dev
, "can't clear tx halt, status %d",
725 clear_bit (EVENT_TX_HALT
, &dev
->flags
);
726 if (status
!= -ESHUTDOWN
)
727 netif_wake_queue (dev
->net
);
730 if (test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
731 unlink_urbs (dev
, &dev
->rxq
);
732 status
= usb_clear_halt (dev
->udev
, dev
->in
);
735 && status
!= -ESHUTDOWN
) {
736 if (netif_msg_rx_err (dev
))
737 deverr (dev
, "can't clear rx halt, status %d",
740 clear_bit (EVENT_RX_HALT
, &dev
->flags
);
741 tasklet_schedule (&dev
->bh
);
745 /* tasklet could resubmit itself forever if memory is tight */
746 if (test_bit (EVENT_RX_MEMORY
, &dev
->flags
)) {
747 struct urb
*urb
= NULL
;
749 if (netif_running (dev
->net
))
750 urb
= usb_alloc_urb (0, GFP_KERNEL
);
752 clear_bit (EVENT_RX_MEMORY
, &dev
->flags
);
754 clear_bit (EVENT_RX_MEMORY
, &dev
->flags
);
755 rx_submit (dev
, urb
, GFP_KERNEL
);
756 tasklet_schedule (&dev
->bh
);
760 if (test_bit (EVENT_LINK_RESET
, &dev
->flags
)) {
761 struct driver_info
*info
= dev
->driver_info
;
764 clear_bit (EVENT_LINK_RESET
, &dev
->flags
);
765 if(info
->link_reset
&& (retval
= info
->link_reset(dev
)) < 0) {
766 devinfo(dev
, "link reset failed (%d) usbnet usb-%s-%s, %s",
768 dev
->udev
->bus
->bus_name
, dev
->udev
->devpath
,
774 devdbg (dev
, "kevent done, flags = 0x%lx",
778 /*-------------------------------------------------------------------------*/
780 static void tx_complete (struct urb
*urb
, struct pt_regs
*regs
)
782 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
783 struct skb_data
*entry
= (struct skb_data
*) skb
->cb
;
784 struct usbnet
*dev
= entry
->dev
;
786 if (urb
->status
== 0) {
787 dev
->stats
.tx_packets
++;
788 dev
->stats
.tx_bytes
+= entry
->length
;
790 dev
->stats
.tx_errors
++;
792 switch (urb
->status
) {
794 usbnet_defer_kevent (dev
, EVENT_TX_HALT
);
797 /* software-driven interface shutdown */
798 case -ECONNRESET
: // async unlink
799 case -ESHUTDOWN
: // hardware gone
802 // like rx, tx gets controller i/o faults during khubd delays
803 // and so it uses the same throttling mechanism.
804 case -EPROTO
: // ehci
805 case -ETIMEDOUT
: // ohci
806 case -EILSEQ
: // uhci
807 if (!timer_pending (&dev
->delay
)) {
808 mod_timer (&dev
->delay
,
809 jiffies
+ THROTTLE_JIFFIES
);
810 if (netif_msg_link (dev
))
811 devdbg (dev
, "tx throttle %d",
814 netif_stop_queue (dev
->net
);
817 if (netif_msg_tx_err (dev
))
818 devdbg (dev
, "tx err %d", entry
->urb
->status
);
824 entry
->state
= tx_done
;
825 defer_bh(dev
, skb
, &dev
->txq
);
828 /*-------------------------------------------------------------------------*/
830 static void usbnet_tx_timeout (struct net_device
*net
)
832 struct usbnet
*dev
= netdev_priv(net
);
834 unlink_urbs (dev
, &dev
->txq
);
835 tasklet_schedule (&dev
->bh
);
837 // FIXME: device recovery -- reset?
840 /*-------------------------------------------------------------------------*/
842 static int usbnet_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
844 struct usbnet
*dev
= netdev_priv(net
);
846 int retval
= NET_XMIT_SUCCESS
;
847 struct urb
*urb
= NULL
;
848 struct skb_data
*entry
;
849 struct driver_info
*info
= dev
->driver_info
;
852 // some devices want funky USB-level framing, for
853 // win32 driver (usually) and/or hardware quirks
854 if (info
->tx_fixup
) {
855 skb
= info
->tx_fixup (dev
, skb
, GFP_ATOMIC
);
857 if (netif_msg_tx_err (dev
))
858 devdbg (dev
, "can't tx_fixup skb");
864 if (!(urb
= usb_alloc_urb (0, GFP_ATOMIC
))) {
865 if (netif_msg_tx_err (dev
))
866 devdbg (dev
, "no urb");
870 entry
= (struct skb_data
*) skb
->cb
;
873 entry
->state
= tx_start
;
874 entry
->length
= length
;
876 usb_fill_bulk_urb (urb
, dev
->udev
, dev
->out
,
877 skb
->data
, skb
->len
, tx_complete
, skb
);
879 /* don't assume the hardware handles USB_ZERO_PACKET
880 * NOTE: strictly conforming cdc-ether devices should expect
881 * the ZLP here, but ignore the one-byte packet.
883 * FIXME zero that byte, if it doesn't require a new skb.
885 if ((length
% dev
->maxpacket
) == 0)
886 urb
->transfer_buffer_length
++;
888 spin_lock_irqsave (&dev
->txq
.lock
, flags
);
890 switch ((retval
= usb_submit_urb (urb
, GFP_ATOMIC
))) {
892 netif_stop_queue (net
);
893 usbnet_defer_kevent (dev
, EVENT_TX_HALT
);
896 if (netif_msg_tx_err (dev
))
897 devdbg (dev
, "tx: submit urb err %d", retval
);
900 net
->trans_start
= jiffies
;
901 __skb_queue_tail (&dev
->txq
, skb
);
902 if (dev
->txq
.qlen
>= TX_QLEN (dev
))
903 netif_stop_queue (net
);
905 spin_unlock_irqrestore (&dev
->txq
.lock
, flags
);
908 if (netif_msg_tx_err (dev
))
909 devdbg (dev
, "drop, code %d", retval
);
911 retval
= NET_XMIT_SUCCESS
;
912 dev
->stats
.tx_dropped
++;
914 dev_kfree_skb_any (skb
);
916 } else if (netif_msg_tx_queued (dev
)) {
917 devdbg (dev
, "> tx, len %d, type 0x%x",
918 length
, skb
->protocol
);
924 /*-------------------------------------------------------------------------*/
926 // tasklet (work deferred from completions, in_irq) or timer
928 static void usbnet_bh (unsigned long param
)
930 struct usbnet
*dev
= (struct usbnet
*) param
;
932 struct skb_data
*entry
;
934 while ((skb
= skb_dequeue (&dev
->done
))) {
935 entry
= (struct skb_data
*) skb
->cb
;
936 switch (entry
->state
) {
938 entry
->state
= rx_cleanup
;
939 rx_process (dev
, skb
);
943 usb_free_urb (entry
->urb
);
947 devdbg (dev
, "bogus skb state %d", entry
->state
);
951 // waiting for all pending urbs to complete?
953 if ((dev
->txq
.qlen
+ dev
->rxq
.qlen
+ dev
->done
.qlen
) == 0) {
957 // or are we maybe short a few urbs?
958 } else if (netif_running (dev
->net
)
959 && netif_device_present (dev
->net
)
960 && !timer_pending (&dev
->delay
)
961 && !test_bit (EVENT_RX_HALT
, &dev
->flags
)) {
962 int temp
= dev
->rxq
.qlen
;
963 int qlen
= RX_QLEN (dev
);
969 // don't refill the queue all at once
970 for (i
= 0; i
< 10 && dev
->rxq
.qlen
< qlen
; i
++) {
971 urb
= usb_alloc_urb (0, GFP_ATOMIC
);
973 rx_submit (dev
, urb
, GFP_ATOMIC
);
975 if (temp
!= dev
->rxq
.qlen
&& netif_msg_link (dev
))
976 devdbg (dev
, "rxqlen %d --> %d",
977 temp
, dev
->rxq
.qlen
);
978 if (dev
->rxq
.qlen
< qlen
)
979 tasklet_schedule (&dev
->bh
);
981 if (dev
->txq
.qlen
< TX_QLEN (dev
))
982 netif_wake_queue (dev
->net
);
988 /*-------------------------------------------------------------------------
990 * USB Device Driver support
992 *-------------------------------------------------------------------------*/
994 // precondition: never called in_interrupt
996 void usbnet_disconnect (struct usb_interface
*intf
)
999 struct usb_device
*xdev
;
1000 struct net_device
*net
;
1002 dev
= usb_get_intfdata(intf
);
1003 usb_set_intfdata(intf
, NULL
);
1007 xdev
= interface_to_usbdev (intf
);
1009 if (netif_msg_probe (dev
))
1010 devinfo (dev
, "unregister '%s' usb-%s-%s, %s",
1011 intf
->dev
.driver
->name
,
1012 xdev
->bus
->bus_name
, xdev
->devpath
,
1013 dev
->driver_info
->description
);
1016 unregister_netdev (net
);
1018 /* we don't hold rtnl here ... */
1019 flush_scheduled_work ();
1021 if (dev
->driver_info
->unbind
)
1022 dev
->driver_info
->unbind (dev
, intf
);
1027 EXPORT_SYMBOL_GPL(usbnet_disconnect
);
1030 /*-------------------------------------------------------------------------*/
1032 // precondition: never called in_interrupt
1035 usbnet_probe (struct usb_interface
*udev
, const struct usb_device_id
*prod
)
1038 struct net_device
*net
;
1039 struct usb_host_interface
*interface
;
1040 struct driver_info
*info
;
1041 struct usb_device
*xdev
;
1044 info
= (struct driver_info
*) prod
->driver_info
;
1046 dev_dbg (&udev
->dev
, "blacklisted by %s\n", driver_name
);
1049 xdev
= interface_to_usbdev (udev
);
1050 interface
= udev
->cur_altsetting
;
1056 // set up our own records
1057 net
= alloc_etherdev(sizeof(*dev
));
1059 dbg ("can't kmalloc dev");
1063 dev
= netdev_priv(net
);
1065 dev
->driver_info
= info
;
1066 dev
->msg_enable
= netif_msg_init (msg_level
, NETIF_MSG_DRV
1067 | NETIF_MSG_PROBE
| NETIF_MSG_LINK
);
1068 skb_queue_head_init (&dev
->rxq
);
1069 skb_queue_head_init (&dev
->txq
);
1070 skb_queue_head_init (&dev
->done
);
1071 dev
->bh
.func
= usbnet_bh
;
1072 dev
->bh
.data
= (unsigned long) dev
;
1073 INIT_WORK (&dev
->kevent
, kevent
, dev
);
1074 dev
->delay
.function
= usbnet_bh
;
1075 dev
->delay
.data
= (unsigned long) dev
;
1076 init_timer (&dev
->delay
);
1078 SET_MODULE_OWNER (net
);
1080 strcpy (net
->name
, "usb%d");
1081 memcpy (net
->dev_addr
, node_id
, sizeof node_id
);
1083 /* rx and tx sides can use different message sizes;
1084 * bind() should set rx_urb_size in that case.
1086 dev
->hard_mtu
= net
->mtu
+ net
->hard_header_len
;
1088 // dma_supported() is deeply broken on almost all architectures
1089 // possible with some EHCI controllers
1090 if (dma_supported (&udev
->dev
, DMA_64BIT_MASK
))
1091 net
->features
|= NETIF_F_HIGHDMA
;
1094 net
->change_mtu
= usbnet_change_mtu
;
1095 net
->get_stats
= usbnet_get_stats
;
1096 net
->hard_start_xmit
= usbnet_start_xmit
;
1097 net
->open
= usbnet_open
;
1098 net
->stop
= usbnet_stop
;
1099 net
->watchdog_timeo
= TX_TIMEOUT_JIFFIES
;
1100 net
->tx_timeout
= usbnet_tx_timeout
;
1101 net
->ethtool_ops
= &usbnet_ethtool_ops
;
1103 // allow device-specific bind/init procedures
1104 // NOTE net->name still not usable ...
1106 status
= info
->bind (dev
, udev
);
1107 // heuristic: "usb%d" for links we know are two-host,
1108 // else "eth%d" when there's reasonable doubt. userspace
1109 // can rename the link if it knows better.
1110 if ((dev
->driver_info
->flags
& FLAG_ETHER
) != 0
1111 && (net
->dev_addr
[0] & 0x02) == 0)
1112 strcpy (net
->name
, "eth%d");
1114 /* maybe the remote can't receive an Ethernet MTU */
1115 if (net
->mtu
> (dev
->hard_mtu
- net
->hard_header_len
))
1116 net
->mtu
= dev
->hard_mtu
- net
->hard_header_len
;
1117 } else if (!info
->in
|| !info
->out
)
1118 status
= usbnet_get_endpoints (dev
, udev
);
1120 dev
->in
= usb_rcvbulkpipe (xdev
, info
->in
);
1121 dev
->out
= usb_sndbulkpipe (xdev
, info
->out
);
1122 if (!(info
->flags
& FLAG_NO_SETINT
))
1123 status
= usb_set_interface (xdev
,
1124 interface
->desc
.bInterfaceNumber
,
1125 interface
->desc
.bAlternateSetting
);
1130 if (status
== 0 && dev
->status
)
1131 status
= init_status (dev
, udev
);
1135 if (!dev
->rx_urb_size
)
1136 dev
->rx_urb_size
= dev
->hard_mtu
;
1137 dev
->maxpacket
= usb_maxpacket (dev
->udev
, dev
->out
, 1);
1139 SET_NETDEV_DEV(net
, &udev
->dev
);
1140 status
= register_netdev (net
);
1143 if (netif_msg_probe (dev
))
1144 devinfo (dev
, "register '%s' at usb-%s-%s, %s, "
1145 "%02x:%02x:%02x:%02x:%02x:%02x",
1146 udev
->dev
.driver
->name
,
1147 xdev
->bus
->bus_name
, xdev
->devpath
,
1148 dev
->driver_info
->description
,
1149 net
->dev_addr
[0], net
->dev_addr
[1],
1150 net
->dev_addr
[2], net
->dev_addr
[3],
1151 net
->dev_addr
[4], net
->dev_addr
[5]);
1153 // ok, it's ready to go.
1154 usb_set_intfdata (udev
, dev
);
1156 // start as if the link is up
1157 netif_device_attach (net
);
1163 info
->unbind (dev
, udev
);
1170 EXPORT_SYMBOL_GPL(usbnet_probe
);
1172 /*-------------------------------------------------------------------------*/
1174 /* FIXME these suspend/resume methods assume non-CDC style
1175 * devices, with only one interface.
1178 int usbnet_suspend (struct usb_interface
*intf
, pm_message_t message
)
1180 struct usbnet
*dev
= usb_get_intfdata(intf
);
1182 /* accelerate emptying of the rx and queues, to avoid
1183 * having everything error out.
1185 netif_device_detach (dev
->net
);
1186 (void) unlink_urbs (dev
, &dev
->rxq
);
1187 (void) unlink_urbs (dev
, &dev
->txq
);
1188 intf
->dev
.power
.power_state
= PMSG_SUSPEND
;
1191 EXPORT_SYMBOL_GPL(usbnet_suspend
);
1193 int usbnet_resume (struct usb_interface
*intf
)
1195 struct usbnet
*dev
= usb_get_intfdata(intf
);
1197 intf
->dev
.power
.power_state
= PMSG_ON
;
1198 netif_device_attach (dev
->net
);
1199 tasklet_schedule (&dev
->bh
);
1202 EXPORT_SYMBOL_GPL(usbnet_resume
);
1205 /*-------------------------------------------------------------------------*/
1207 static int __init
usbnet_init(void)
1209 /* compiler should optimize this out */
1210 BUG_ON (sizeof (((struct sk_buff
*)0)->cb
)
1211 < sizeof (struct skb_data
));
1213 random_ether_addr(node_id
);
1216 module_init(usbnet_init
);
1218 static void __exit
usbnet_exit(void)
1221 module_exit(usbnet_exit
);
1223 MODULE_AUTHOR("David Brownell");
1224 MODULE_DESCRIPTION("USB network driver framework");
1225 MODULE_LICENSE("GPL");