Merge ../bleed-2.6
[deliverable/linux.git] / drivers / usb / net / usbnet.c
CommitLineData
1da177e4 1/*
090ffa9d 2 * USB Network driver infrastructure
f29fc259 3 * Copyright (C) 2000-2005 by David Brownell
1da177e4 4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
1da177e4
LT
5 *
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.
10 *
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.
15 *
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
19 */
20
21/*
22 * This is a generic "USB networking" framework that works with several
090ffa9d
DB
23 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
1da177e4 25 *
090ffa9d
DB
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.
31 */
1da177e4
LT
32
33// #define DEBUG // error path messages, extra info
34// #define VERBOSE // more; success messages
35
36#include <linux/config.h>
37#ifdef CONFIG_USB_DEBUG
38# define DEBUG
39#endif
40#include <linux/module.h>
1da177e4
LT
41#include <linux/sched.h>
42#include <linux/init.h>
43#include <linux/netdevice.h>
44#include <linux/etherdevice.h>
1da177e4
LT
45#include <linux/ethtool.h>
46#include <linux/workqueue.h>
47#include <linux/mii.h>
1da177e4 48#include <linux/usb.h>
f29fc259
DB
49
50#include "usbnet.h"
51
52#define DRIVER_VERSION "22-Aug-2005"
1da177e4
LT
53
54
55/*-------------------------------------------------------------------------*/
56
57/*
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).
f29fc259
DB
63 *
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.
1da177e4
LT
67 */
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)
70
1da177e4
LT
71// reawaken network queue this soon after stopping; else watchdog barks
72#define TX_TIMEOUT_JIFFIES (5*HZ)
73
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)
77
1da177e4
LT
78// between wakeups
79#define UNLINK_TIMEOUT_MS 3
80
81/*-------------------------------------------------------------------------*/
82
83// randomly generated ethernet address
84static u8 node_id [ETH_ALEN];
85
1da177e4
LT
86static const char driver_name [] = "usbnet";
87
88/* use ethtool to change the level for any given device */
89static int msg_level = -1;
90module_param (msg_level, int, 0);
91MODULE_PARM_DESC (msg_level, "Override default message level");
92
1da177e4
LT
93/*-------------------------------------------------------------------------*/
94
1da177e4 95/* handles CDC Ethernet and many other network "bulk data" interfaces */
2e55cc72 96int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
1da177e4
LT
97{
98 int tmp;
99 struct usb_host_interface *alt = NULL;
100 struct usb_host_endpoint *in = NULL, *out = NULL;
101 struct usb_host_endpoint *status = NULL;
102
103 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
104 unsigned ep;
105
106 in = out = status = NULL;
107 alt = intf->altsetting + tmp;
108
109 /* take the first altsetting with in-bulk + out-bulk;
110 * remember any status endpoint, just in case;
111 * ignore other endpoints and altsetttings.
112 */
113 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
114 struct usb_host_endpoint *e;
115 int intr = 0;
116
117 e = alt->endpoint + ep;
118 switch (e->desc.bmAttributes) {
119 case USB_ENDPOINT_XFER_INT:
120 if (!(e->desc.bEndpointAddress & USB_DIR_IN))
121 continue;
122 intr = 1;
123 /* FALLTHROUGH */
124 case USB_ENDPOINT_XFER_BULK:
125 break;
126 default:
127 continue;
128 }
129 if (e->desc.bEndpointAddress & USB_DIR_IN) {
130 if (!intr && !in)
131 in = e;
132 else if (intr && !status)
133 status = e;
134 } else {
135 if (!out)
136 out = e;
137 }
138 }
139 if (in && out)
140 break;
141 }
142 if (!alt || !in || !out)
143 return -EINVAL;
144
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);
149 if (tmp < 0)
150 return tmp;
151 }
152
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;
158 return 0;
159}
2e55cc72 160EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1da177e4
LT
161
162static void intr_complete (struct urb *urb, struct pt_regs *regs);
163
164static int init_status (struct usbnet *dev, struct usb_interface *intf)
165{
166 char *buf = NULL;
167 unsigned pipe = 0;
168 unsigned maxp;
169 unsigned period;
170
171 if (!dev->driver_info->status)
172 return 0;
173
174 pipe = usb_rcvintpipe (dev->udev,
175 dev->status->desc.bEndpointAddress
176 & USB_ENDPOINT_NUMBER_MASK);
177 maxp = usb_maxpacket (dev->udev, pipe, 0);
178
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);
182
183 buf = kmalloc (maxp, SLAB_KERNEL);
184 if (buf) {
185 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
186 if (!dev->interrupt) {
187 kfree (buf);
188 return -ENOMEM;
189 } else {
190 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
191 buf, maxp, intr_complete, dev, period);
192 dev_dbg(&intf->dev,
193 "status ep%din, %d bytes period %d\n",
194 usb_pipeendpoint(pipe), maxp, period);
195 }
196 }
197 return 0;
198}
199
2e55cc72
DB
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.
203 */
204void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
1da177e4
LT
205{
206 int status;
207
208 skb->dev = dev->net;
209 skb->protocol = eth_type_trans (skb, dev->net);
210 dev->stats.rx_packets++;
211 dev->stats.rx_bytes += skb->len;
212
213 if (netif_msg_rx_status (dev))
5330e927 214 devdbg (dev, "< rx, len %zu, type 0x%x",
1da177e4
LT
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);
220}
2e55cc72 221EXPORT_SYMBOL_GPL(usbnet_skb_return);
1da177e4 222
1da177e4
LT
223\f
224/*-------------------------------------------------------------------------
225 *
226 * Network Device Driver (peer link to "Host Device", from USB host)
227 *
228 *-------------------------------------------------------------------------*/
229
230static int usbnet_change_mtu (struct net_device *net, int new_mtu)
231{
232 struct usbnet *dev = netdev_priv(net);
f29fc259 233 int ll_mtu = new_mtu + net->hard_header_len;
1da177e4 234
f29fc259 235 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
1da177e4 236 return -EINVAL;
1da177e4 237 // no second zero-length packet read wanted after mtu-sized packets
f29fc259 238 if ((ll_mtu % dev->maxpacket) == 0)
1da177e4
LT
239 return -EDOM;
240 net->mtu = new_mtu;
241 return 0;
242}
243
244/*-------------------------------------------------------------------------*/
245
246static struct net_device_stats *usbnet_get_stats (struct net_device *net)
247{
248 struct usbnet *dev = netdev_priv(net);
249 return &dev->stats;
250}
251
252/*-------------------------------------------------------------------------*/
253
254/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
255 * completion callbacks. 2.5 should have fixed those bugs...
256 */
257
8728b834 258static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
1da177e4 259{
1da177e4
LT
260 unsigned long flags;
261
8728b834
DM
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);
1da177e4 267 if (dev->done.qlen == 1)
8728b834
DM
268 tasklet_schedule(&dev->bh);
269 spin_unlock_irqrestore(&dev->done.lock, flags);
1da177e4
LT
270}
271
272/* some work can't be done in tasklets, so we use keventd
273 *
274 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
275 * but tasklet_schedule() doesn't. hope the failure is rare.
276 */
2e55cc72 277void usbnet_defer_kevent (struct usbnet *dev, int work)
1da177e4
LT
278{
279 set_bit (work, &dev->flags);
280 if (!schedule_work (&dev->kevent))
281 deverr (dev, "kevent %d may have been dropped", work);
282 else
283 devdbg (dev, "kevent %d scheduled", work);
284}
2e55cc72 285EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
1da177e4
LT
286
287/*-------------------------------------------------------------------------*/
288
289static void rx_complete (struct urb *urb, struct pt_regs *regs);
290
55016f10 291static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
1da177e4
LT
292{
293 struct sk_buff *skb;
294 struct skb_data *entry;
295 int retval = 0;
296 unsigned long lockflags;
2e55cc72 297 size_t size = dev->rx_urb_size;
1da177e4 298
1da177e4
LT
299 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
300 if (netif_msg_rx_err (dev))
301 devdbg (dev, "no rx skb");
2e55cc72 302 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4
LT
303 usb_free_urb (urb);
304 return;
305 }
306 skb_reserve (skb, NET_IP_ALIGN);
307
308 entry = (struct skb_data *) skb->cb;
309 entry->urb = urb;
310 entry->dev = dev;
311 entry->state = rx_start;
312 entry->length = 0;
313
314 usb_fill_bulk_urb (urb, dev->udev, dev->in,
315 skb->data, size, rx_complete, skb);
1da177e4
LT
316
317 spin_lock_irqsave (&dev->rxq.lock, lockflags);
318
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)){
323 case -EPIPE:
2e55cc72 324 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
325 break;
326 case -ENOMEM:
2e55cc72 327 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4
LT
328 break;
329 case -ENODEV:
330 if (netif_msg_ifdown (dev))
331 devdbg (dev, "device gone");
332 netif_device_detach (dev->net);
333 break;
334 default:
335 if (netif_msg_rx_err (dev))
336 devdbg (dev, "rx submit, %d", retval);
337 tasklet_schedule (&dev->bh);
338 break;
339 case 0:
340 __skb_queue_tail (&dev->rxq, skb);
341 }
342 } else {
343 if (netif_msg_ifdown (dev))
344 devdbg (dev, "rx: stopped");
345 retval = -ENOLINK;
346 }
347 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
348 if (retval) {
349 dev_kfree_skb_any (skb);
350 usb_free_urb (urb);
351 }
352}
353
354
355/*-------------------------------------------------------------------------*/
356
357static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
358{
359 if (dev->driver_info->rx_fixup
360 && !dev->driver_info->rx_fixup (dev, skb))
361 goto error;
362 // else network stack removes extra byte if we forced a short packet
363
364 if (skb->len)
2e55cc72 365 usbnet_skb_return (dev, skb);
1da177e4
LT
366 else {
367 if (netif_msg_rx_err (dev))
368 devdbg (dev, "drop");
369error:
370 dev->stats.rx_errors++;
371 skb_queue_tail (&dev->done, skb);
372 }
373}
374
375/*-------------------------------------------------------------------------*/
376
377static void rx_complete (struct urb *urb, struct pt_regs *regs)
378{
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;
383
384 skb_put (skb, urb->actual_length);
385 entry->state = rx_done;
386 entry->urb = NULL;
387
388 switch (urb_status) {
389 // success
390 case 0:
f29fc259 391 if (skb->len < dev->net->hard_header_len) {
1da177e4
LT
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);
397 }
398 break;
399
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.
404 case -EPIPE:
405 dev->stats.rx_errors++;
2e55cc72 406 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
407 // FALLTHROUGH
408
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);
414 goto block;
415
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);
427 }
428block:
429 entry->state = rx_cleanup;
430 entry->urb = urb;
431 urb = NULL;
432 break;
433
434 // data overrun ... flush fifo?
435 case -EOVERFLOW:
436 dev->stats.rx_over_errors++;
437 // FALLTHROUGH
438
439 default:
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);
444 break;
445 }
446
8728b834 447 defer_bh(dev, skb, &dev->rxq);
1da177e4
LT
448
449 if (urb) {
450 if (netif_running (dev->net)
451 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
452 rx_submit (dev, urb, GFP_ATOMIC);
453 return;
454 }
455 usb_free_urb (urb);
456 }
457 if (netif_msg_rx_err (dev))
458 devdbg (dev, "no read resubmitted");
459}
460
461static void intr_complete (struct urb *urb, struct pt_regs *regs)
462{
463 struct usbnet *dev = urb->context;
464 int status = urb->status;
465
466 switch (status) {
467 /* success */
468 case 0:
469 dev->driver_info->status(dev, urb);
470 break;
471
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);
477 return;
478
479 /* NOTE: not throttling like RX/TX, since this endpoint
480 * already polls infrequently
481 */
482 default:
483 devdbg (dev, "intr status %d", status);
484 break;
485 }
486
487 if (!netif_running (dev->net))
488 return;
489
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);
494}
495
496/*-------------------------------------------------------------------------*/
497
498// unlink pending rx/tx; completion handlers do all other cleanup
499
500static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
501{
502 unsigned long flags;
503 struct sk_buff *skb, *skbnext;
504 int count = 0;
505
506 spin_lock_irqsave (&q->lock, flags);
507 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
508 struct skb_data *entry;
509 struct urb *urb;
510 int retval;
511
512 entry = (struct skb_data *) skb->cb;
513 urb = entry->urb;
514 skbnext = skb->next;
515
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);
521 else
522 count++;
523 }
524 spin_unlock_irqrestore (&q->lock, flags);
525 return count;
526}
527
528
529/*-------------------------------------------------------------------------*/
530
531// precondition: never called in_interrupt
532
533static int usbnet_stop (struct net_device *net)
534{
535 struct usbnet *dev = netdev_priv(net);
536 int temp;
537 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
538 DECLARE_WAITQUEUE (wait, current);
539
540 netif_stop_queue (net);
541
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
546 );
547
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);
552
553 // maybe wait for deletions to finish.
b03efcfb
DM
554 while (!skb_queue_empty(&dev->rxq) &&
555 !skb_queue_empty(&dev->txq) &&
556 !skb_queue_empty(&dev->done)) {
1da177e4
LT
557 msleep(UNLINK_TIMEOUT_MS);
558 if (netif_msg_ifdown (dev))
559 devdbg (dev, "waited for %d urb completions", temp);
560 }
561 dev->wait = NULL;
562 remove_wait_queue (&unlink_wakeup, &wait);
563
564 usb_kill_urb(dev->interrupt);
565
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.
569 */
570 dev->flags = 0;
571 del_timer_sync (&dev->delay);
572 tasklet_kill (&dev->bh);
573
574 return 0;
575}
576
577/*-------------------------------------------------------------------------*/
578
579// posts reads, and enables write queuing
580
581// precondition: never called in_interrupt
582
583static int usbnet_open (struct net_device *net)
584{
585 struct usbnet *dev = netdev_priv(net);
586 int retval = 0;
587 struct driver_info *info = dev->driver_info;
588
589 // put into "known safe" state
590 if (info->reset && (retval = info->reset (dev)) < 0) {
591 if (netif_msg_ifup (dev))
592 devinfo (dev,
593 "open reset fail (%d) usbnet usb-%s-%s, %s",
594 retval,
595 dev->udev->bus->bus_name, dev->udev->devpath,
596 info->description);
597 goto done;
598 }
599
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);
604 goto done;
605 }
606
607 /* start any status interrupt transfer */
608 if (dev->interrupt) {
609 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
610 if (retval < 0) {
611 if (netif_msg_ifup (dev))
612 deverr (dev, "intr submit %d", retval);
613 goto done;
614 }
615 }
616
617 netif_start_queue (net);
618 if (netif_msg_ifup (dev)) {
619 char *framing;
620
621 if (dev->driver_info->flags & FLAG_FRAMING_NC)
622 framing = "NetChip";
623 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
624 framing = "GeneSys";
625 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
626 framing = "Zaurus";
627 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
628 framing = "RNDIS";
629 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
630 framing = "ASIX";
631 else
632 framing = "simple";
633
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,
637 framing);
638 }
639
640 // delay posting reads until we're fully open
641 tasklet_schedule (&dev->bh);
642done:
643 return retval;
644}
645
646/*-------------------------------------------------------------------------*/
647
2e55cc72
DB
648/* ethtool methods; minidrivers may need to add some more, but
649 * they'll probably want to use this base set.
650 */
651
652void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1da177e4
LT
653{
654 struct usbnet *dev = netdev_priv(net);
655
2e55cc72 656 /* REVISIT don't always return "usbnet" */
1da177e4
LT
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);
662}
2e55cc72 663EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1da177e4
LT
664
665static u32 usbnet_get_link (struct net_device *net)
666{
667 struct usbnet *dev = netdev_priv(net);
668
f29fc259 669 /* If a check_connect is defined, return its result */
1da177e4
LT
670 if (dev->driver_info->check_connect)
671 return dev->driver_info->check_connect (dev) == 0;
672
f29fc259 673 /* Otherwise, say we're up (to avoid breaking scripts) */
1da177e4
LT
674 return 1;
675}
676
2e55cc72 677u32 usbnet_get_msglevel (struct net_device *net)
1da177e4
LT
678{
679 struct usbnet *dev = netdev_priv(net);
680
681 return dev->msg_enable;
682}
2e55cc72 683EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
1da177e4 684
2e55cc72 685void usbnet_set_msglevel (struct net_device *net, u32 level)
1da177e4
LT
686{
687 struct usbnet *dev = netdev_priv(net);
688
689 dev->msg_enable = level;
690}
2e55cc72 691EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
1da177e4 692
2e55cc72
DB
693/* drivers may override default ethtool_ops in their bind() routine */
694static 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,
699};
1da177e4
LT
700
701/*-------------------------------------------------------------------------*/
702
703/* work that cannot be done in interrupt context uses keventd.
704 *
705 * NOTE: with 2.5 we could do more of this using completion callbacks,
706 * especially now that control transfers can be queued.
707 */
708static void
709kevent (void *data)
710{
711 struct usbnet *dev = data;
712 int status;
713
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);
f29fc259
DB
718 if (status < 0
719 && status != -EPIPE
720 && status != -ESHUTDOWN) {
1da177e4
LT
721 if (netif_msg_tx_err (dev))
722 deverr (dev, "can't clear tx halt, status %d",
723 status);
724 } else {
725 clear_bit (EVENT_TX_HALT, &dev->flags);
f29fc259
DB
726 if (status != -ESHUTDOWN)
727 netif_wake_queue (dev->net);
1da177e4
LT
728 }
729 }
730 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
731 unlink_urbs (dev, &dev->rxq);
732 status = usb_clear_halt (dev->udev, dev->in);
f29fc259
DB
733 if (status < 0
734 && status != -EPIPE
735 && status != -ESHUTDOWN) {
1da177e4
LT
736 if (netif_msg_rx_err (dev))
737 deverr (dev, "can't clear rx halt, status %d",
738 status);
739 } else {
740 clear_bit (EVENT_RX_HALT, &dev->flags);
741 tasklet_schedule (&dev->bh);
742 }
743 }
744
745 /* tasklet could resubmit itself forever if memory is tight */
746 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
747 struct urb *urb = NULL;
748
749 if (netif_running (dev->net))
750 urb = usb_alloc_urb (0, GFP_KERNEL);
751 else
752 clear_bit (EVENT_RX_MEMORY, &dev->flags);
753 if (urb != NULL) {
754 clear_bit (EVENT_RX_MEMORY, &dev->flags);
755 rx_submit (dev, urb, GFP_KERNEL);
756 tasklet_schedule (&dev->bh);
757 }
758 }
759
7ea13c9c
DH
760 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
761 struct driver_info *info = dev->driver_info;
762 int retval = 0;
763
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",
767 retval,
768 dev->udev->bus->bus_name, dev->udev->devpath,
769 info->description);
770 }
771 }
772
1da177e4
LT
773 if (dev->flags)
774 devdbg (dev, "kevent done, flags = 0x%lx",
775 dev->flags);
776}
777
778/*-------------------------------------------------------------------------*/
779
780static void tx_complete (struct urb *urb, struct pt_regs *regs)
781{
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;
785
786 if (urb->status == 0) {
787 dev->stats.tx_packets++;
788 dev->stats.tx_bytes += entry->length;
789 } else {
790 dev->stats.tx_errors++;
791
792 switch (urb->status) {
793 case -EPIPE:
2e55cc72 794 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1da177e4
LT
795 break;
796
797 /* software-driven interface shutdown */
798 case -ECONNRESET: // async unlink
799 case -ESHUTDOWN: // hardware gone
800 break;
801
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",
812 urb->status);
813 }
814 netif_stop_queue (dev->net);
815 break;
816 default:
817 if (netif_msg_tx_err (dev))
818 devdbg (dev, "tx err %d", entry->urb->status);
819 break;
820 }
821 }
822
823 urb->dev = NULL;
824 entry->state = tx_done;
8728b834 825 defer_bh(dev, skb, &dev->txq);
1da177e4
LT
826}
827
828/*-------------------------------------------------------------------------*/
829
830static void usbnet_tx_timeout (struct net_device *net)
831{
832 struct usbnet *dev = netdev_priv(net);
833
834 unlink_urbs (dev, &dev->txq);
835 tasklet_schedule (&dev->bh);
836
837 // FIXME: device recovery -- reset?
838}
839
840/*-------------------------------------------------------------------------*/
841
842static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
843{
844 struct usbnet *dev = netdev_priv(net);
845 int length;
846 int retval = NET_XMIT_SUCCESS;
847 struct urb *urb = NULL;
848 struct skb_data *entry;
849 struct driver_info *info = dev->driver_info;
850 unsigned long flags;
1da177e4
LT
851
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);
856 if (!skb) {
857 if (netif_msg_tx_err (dev))
858 devdbg (dev, "can't tx_fixup skb");
859 goto drop;
860 }
861 }
862 length = skb->len;
863
864 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
865 if (netif_msg_tx_err (dev))
866 devdbg (dev, "no urb");
867 goto drop;
868 }
869
870 entry = (struct skb_data *) skb->cb;
871 entry->urb = urb;
872 entry->dev = dev;
873 entry->state = tx_start;
874 entry->length = length;
875
1da177e4
LT
876 usb_fill_bulk_urb (urb, dev->udev, dev->out,
877 skb->data, skb->len, tx_complete, skb);
1da177e4
LT
878
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.
882 *
883 * FIXME zero that byte, if it doesn't require a new skb.
884 */
885 if ((length % dev->maxpacket) == 0)
886 urb->transfer_buffer_length++;
887
888 spin_lock_irqsave (&dev->txq.lock, flags);
889
1da177e4
LT
890 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
891 case -EPIPE:
892 netif_stop_queue (net);
2e55cc72 893 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1da177e4
LT
894 break;
895 default:
896 if (netif_msg_tx_err (dev))
897 devdbg (dev, "tx: submit urb err %d", retval);
898 break;
899 case 0:
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);
904 }
905 spin_unlock_irqrestore (&dev->txq.lock, flags);
906
907 if (retval) {
908 if (netif_msg_tx_err (dev))
909 devdbg (dev, "drop, code %d", retval);
910drop:
911 retval = NET_XMIT_SUCCESS;
912 dev->stats.tx_dropped++;
913 if (skb)
914 dev_kfree_skb_any (skb);
915 usb_free_urb (urb);
916 } else if (netif_msg_tx_queued (dev)) {
917 devdbg (dev, "> tx, len %d, type 0x%x",
918 length, skb->protocol);
919 }
920 return retval;
921}
922
923
924/*-------------------------------------------------------------------------*/
925
926// tasklet (work deferred from completions, in_irq) or timer
927
928static void usbnet_bh (unsigned long param)
929{
930 struct usbnet *dev = (struct usbnet *) param;
931 struct sk_buff *skb;
932 struct skb_data *entry;
933
934 while ((skb = skb_dequeue (&dev->done))) {
935 entry = (struct skb_data *) skb->cb;
936 switch (entry->state) {
937 case rx_done:
938 entry->state = rx_cleanup;
939 rx_process (dev, skb);
940 continue;
941 case tx_done:
942 case rx_cleanup:
943 usb_free_urb (entry->urb);
944 dev_kfree_skb (skb);
945 continue;
946 default:
947 devdbg (dev, "bogus skb state %d", entry->state);
948 }
949 }
950
951 // waiting for all pending urbs to complete?
952 if (dev->wait) {
953 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
954 wake_up (dev->wait);
955 }
956
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);
964
965 if (temp < qlen) {
966 struct urb *urb;
967 int i;
968
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);
972 if (urb != NULL)
973 rx_submit (dev, urb, GFP_ATOMIC);
974 }
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);
980 }
981 if (dev->txq.qlen < TX_QLEN (dev))
982 netif_wake_queue (dev->net);
983 }
984}
985
986
987\f
988/*-------------------------------------------------------------------------
989 *
990 * USB Device Driver support
991 *
992 *-------------------------------------------------------------------------*/
993
994// precondition: never called in_interrupt
995
38bde1d4 996void usbnet_disconnect (struct usb_interface *intf)
1da177e4
LT
997{
998 struct usbnet *dev;
999 struct usb_device *xdev;
1000 struct net_device *net;
1001
1002 dev = usb_get_intfdata(intf);
1003 usb_set_intfdata(intf, NULL);
1004 if (!dev)
1005 return;
1006
1007 xdev = interface_to_usbdev (intf);
1008
1009 if (netif_msg_probe (dev))
38bde1d4
DB
1010 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1011 intf->dev.driver->name,
1da177e4
LT
1012 xdev->bus->bus_name, xdev->devpath,
1013 dev->driver_info->description);
1014
1015 net = dev->net;
1016 unregister_netdev (net);
1017
1018 /* we don't hold rtnl here ... */
1019 flush_scheduled_work ();
1020
1021 if (dev->driver_info->unbind)
1022 dev->driver_info->unbind (dev, intf);
1023
1024 free_netdev(net);
1025 usb_put_dev (xdev);
1026}
38bde1d4 1027EXPORT_SYMBOL_GPL(usbnet_disconnect);
1da177e4
LT
1028
1029
1030/*-------------------------------------------------------------------------*/
1031
1da177e4
LT
1032// precondition: never called in_interrupt
1033
38bde1d4 1034int
1da177e4
LT
1035usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1036{
1037 struct usbnet *dev;
1038 struct net_device *net;
1039 struct usb_host_interface *interface;
1040 struct driver_info *info;
1041 struct usb_device *xdev;
1042 int status;
1043
1044 info = (struct driver_info *) prod->driver_info;
1045 if (!info) {
1046 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
1047 return -ENODEV;
1048 }
1049 xdev = interface_to_usbdev (udev);
1050 interface = udev->cur_altsetting;
1051
1052 usb_get_dev (xdev);
1053
1054 status = -ENOMEM;
1055
1056 // set up our own records
1057 net = alloc_etherdev(sizeof(*dev));
1058 if (!net) {
1059 dbg ("can't kmalloc dev");
1060 goto out;
1061 }
1062
1063 dev = netdev_priv(net);
1064 dev->udev = xdev;
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);
1077
1078 SET_MODULE_OWNER (net);
1079 dev->net = net;
1080 strcpy (net->name, "usb%d");
1081 memcpy (net->dev_addr, node_id, sizeof node_id);
1082
2e55cc72
DB
1083 /* rx and tx sides can use different message sizes;
1084 * bind() should set rx_urb_size in that case.
1085 */
1086 dev->hard_mtu = net->mtu + net->hard_header_len;
1da177e4
LT
1087#if 0
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;
1092#endif
1093
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;
1da177e4
LT
1101 net->ethtool_ops = &usbnet_ethtool_ops;
1102
1103 // allow device-specific bind/init procedures
1104 // NOTE net->name still not usable ...
1105 if (info->bind) {
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");
f29fc259
DB
1113
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;
2e55cc72
DB
1117 } else if (!info->in || !info->out)
1118 status = usbnet_get_endpoints (dev, udev);
1da177e4
LT
1119 else {
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);
1126 else
1127 status = 0;
1128
1129 }
1da177e4
LT
1130 if (status == 0 && dev->status)
1131 status = init_status (dev, udev);
1132 if (status < 0)
1133 goto out1;
1134
2e55cc72
DB
1135 if (!dev->rx_urb_size)
1136 dev->rx_urb_size = dev->hard_mtu;
1da177e4
LT
1137 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
1138
1139 SET_NETDEV_DEV(net, &udev->dev);
1140 status = register_netdev (net);
1141 if (status)
1142 goto out3;
1143 if (netif_msg_probe (dev))
38bde1d4 1144 devinfo (dev, "register '%s' at usb-%s-%s, %s, "
1da177e4 1145 "%02x:%02x:%02x:%02x:%02x:%02x",
38bde1d4 1146 udev->dev.driver->name,
1da177e4
LT
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]);
1152
1153 // ok, it's ready to go.
1154 usb_set_intfdata (udev, dev);
1155
1156 // start as if the link is up
1157 netif_device_attach (net);
1158
1159 return 0;
1160
1161out3:
1162 if (info->unbind)
1163 info->unbind (dev, udev);
1164out1:
1165 free_netdev(net);
1166out:
1167 usb_put_dev(xdev);
1168 return status;
1169}
38bde1d4 1170EXPORT_SYMBOL_GPL(usbnet_probe);
1da177e4
LT
1171
1172/*-------------------------------------------------------------------------*/
1173
38bde1d4
DB
1174/* FIXME these suspend/resume methods assume non-CDC style
1175 * devices, with only one interface.
1176 */
1da177e4 1177
38bde1d4 1178int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1179{
1180 struct usbnet *dev = usb_get_intfdata(intf);
1181
27d72e85
DB
1182 /* accelerate emptying of the rx and queues, to avoid
1183 * having everything error out.
1184 */
1da177e4 1185 netif_device_detach (dev->net);
27d72e85
DB
1186 (void) unlink_urbs (dev, &dev->rxq);
1187 (void) unlink_urbs (dev, &dev->txq);
1188 intf->dev.power.power_state = PMSG_SUSPEND;
1da177e4
LT
1189 return 0;
1190}
38bde1d4 1191EXPORT_SYMBOL_GPL(usbnet_suspend);
1da177e4 1192
38bde1d4 1193int usbnet_resume (struct usb_interface *intf)
1da177e4
LT
1194{
1195 struct usbnet *dev = usb_get_intfdata(intf);
1196
27d72e85 1197 intf->dev.power.power_state = PMSG_ON;
1da177e4 1198 netif_device_attach (dev->net);
27d72e85 1199 tasklet_schedule (&dev->bh);
1da177e4
LT
1200 return 0;
1201}
38bde1d4 1202EXPORT_SYMBOL_GPL(usbnet_resume);
1da177e4 1203
1da177e4 1204
1da177e4
LT
1205/*-------------------------------------------------------------------------*/
1206
f29fc259 1207static int __init usbnet_init(void)
1da177e4 1208{
090ffa9d 1209 /* compiler should optimize this out */
1da177e4
LT
1210 BUG_ON (sizeof (((struct sk_buff *)0)->cb)
1211 < sizeof (struct skb_data));
1da177e4
LT
1212
1213 random_ether_addr(node_id);
090ffa9d 1214 return 0;
1da177e4 1215}
f29fc259 1216module_init(usbnet_init);
1da177e4 1217
f29fc259 1218static void __exit usbnet_exit(void)
1da177e4 1219{
1da177e4 1220}
f29fc259 1221module_exit(usbnet_exit);
1da177e4 1222
f29fc259 1223MODULE_AUTHOR("David Brownell");
090ffa9d 1224MODULE_DESCRIPTION("USB network driver framework");
f29fc259 1225MODULE_LICENSE("GPL");
This page took 0.146623 seconds and 5 git commands to generate.