Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.15-rc6 1220 lines 33 kB view raw
1/* 2 * USB Network driver infrastructure 3 * Copyright (C) 2000-2005 by David Brownell 4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> 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 23 * kinds of full and high speed networking devices: host-to-host cables, 24 * smart usb peripherals, and actual Ethernet adapters. 25 * 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 */ 32 33// #define DEBUG // error path messages, extra info 34// #define VERBOSE // more; success messages 35 36#include <linux/config.h> 37#include <linux/module.h> 38#include <linux/sched.h> 39#include <linux/init.h> 40#include <linux/netdevice.h> 41#include <linux/etherdevice.h> 42#include <linux/ethtool.h> 43#include <linux/workqueue.h> 44#include <linux/mii.h> 45#include <linux/usb.h> 46 47#include "usbnet.h" 48 49#define DRIVER_VERSION "22-Aug-2005" 50 51 52/*-------------------------------------------------------------------------*/ 53 54/* 55 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. 56 * Several dozen bytes of IPv4 data can fit in two such transactions. 57 * One maximum size Ethernet packet takes twenty four of them. 58 * For high speed, each frame comfortably fits almost 36 max size 59 * Ethernet packets (so queues should be bigger). 60 * 61 * REVISIT qlens should be members of 'struct usbnet'; the goal is to 62 * let the USB host controller be busy for 5msec or more before an irq 63 * is required, under load. Jumbograms change the equation. 64 */ 65#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4) 66#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4) 67 68// reawaken network queue this soon after stopping; else watchdog barks 69#define TX_TIMEOUT_JIFFIES (5*HZ) 70 71// throttle rx/tx briefly after some faults, so khubd might disconnect() 72// us (it polls at HZ/4 usually) before we report too many false errors. 73#define THROTTLE_JIFFIES (HZ/8) 74 75// between wakeups 76#define UNLINK_TIMEOUT_MS 3 77 78/*-------------------------------------------------------------------------*/ 79 80// randomly generated ethernet address 81static u8 node_id [ETH_ALEN]; 82 83static const char driver_name [] = "usbnet"; 84 85/* use ethtool to change the level for any given device */ 86static int msg_level = -1; 87module_param (msg_level, int, 0); 88MODULE_PARM_DESC (msg_level, "Override default message level"); 89 90/*-------------------------------------------------------------------------*/ 91 92/* handles CDC Ethernet and many other network "bulk data" interfaces */ 93int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) 94{ 95 int tmp; 96 struct usb_host_interface *alt = NULL; 97 struct usb_host_endpoint *in = NULL, *out = NULL; 98 struct usb_host_endpoint *status = NULL; 99 100 for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 101 unsigned ep; 102 103 in = out = status = NULL; 104 alt = intf->altsetting + tmp; 105 106 /* take the first altsetting with in-bulk + out-bulk; 107 * remember any status endpoint, just in case; 108 * ignore other endpoints and altsetttings. 109 */ 110 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 111 struct usb_host_endpoint *e; 112 int intr = 0; 113 114 e = alt->endpoint + ep; 115 switch (e->desc.bmAttributes) { 116 case USB_ENDPOINT_XFER_INT: 117 if (!(e->desc.bEndpointAddress & USB_DIR_IN)) 118 continue; 119 intr = 1; 120 /* FALLTHROUGH */ 121 case USB_ENDPOINT_XFER_BULK: 122 break; 123 default: 124 continue; 125 } 126 if (e->desc.bEndpointAddress & USB_DIR_IN) { 127 if (!intr && !in) 128 in = e; 129 else if (intr && !status) 130 status = e; 131 } else { 132 if (!out) 133 out = e; 134 } 135 } 136 if (in && out) 137 break; 138 } 139 if (!alt || !in || !out) 140 return -EINVAL; 141 142 if (alt->desc.bAlternateSetting != 0 143 || !(dev->driver_info->flags & FLAG_NO_SETINT)) { 144 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, 145 alt->desc.bAlternateSetting); 146 if (tmp < 0) 147 return tmp; 148 } 149 150 dev->in = usb_rcvbulkpipe (dev->udev, 151 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 152 dev->out = usb_sndbulkpipe (dev->udev, 153 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 154 dev->status = status; 155 return 0; 156} 157EXPORT_SYMBOL_GPL(usbnet_get_endpoints); 158 159static void intr_complete (struct urb *urb, struct pt_regs *regs); 160 161static int init_status (struct usbnet *dev, struct usb_interface *intf) 162{ 163 char *buf = NULL; 164 unsigned pipe = 0; 165 unsigned maxp; 166 unsigned period; 167 168 if (!dev->driver_info->status) 169 return 0; 170 171 pipe = usb_rcvintpipe (dev->udev, 172 dev->status->desc.bEndpointAddress 173 & USB_ENDPOINT_NUMBER_MASK); 174 maxp = usb_maxpacket (dev->udev, pipe, 0); 175 176 /* avoid 1 msec chatter: min 8 msec poll rate */ 177 period = max ((int) dev->status->desc.bInterval, 178 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 179 180 buf = kmalloc (maxp, SLAB_KERNEL); 181 if (buf) { 182 dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL); 183 if (!dev->interrupt) { 184 kfree (buf); 185 return -ENOMEM; 186 } else { 187 usb_fill_int_urb(dev->interrupt, dev->udev, pipe, 188 buf, maxp, intr_complete, dev, period); 189 dev_dbg(&intf->dev, 190 "status ep%din, %d bytes period %d\n", 191 usb_pipeendpoint(pipe), maxp, period); 192 } 193 } 194 return 0; 195} 196 197/* Passes this packet up the stack, updating its accounting. 198 * Some link protocols batch packets, so their rx_fixup paths 199 * can return clones as well as just modify the original skb. 200 */ 201void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) 202{ 203 int status; 204 205 skb->dev = dev->net; 206 skb->protocol = eth_type_trans (skb, dev->net); 207 dev->stats.rx_packets++; 208 dev->stats.rx_bytes += skb->len; 209 210 if (netif_msg_rx_status (dev)) 211 devdbg (dev, "< rx, len %zu, type 0x%x", 212 skb->len + sizeof (struct ethhdr), skb->protocol); 213 memset (skb->cb, 0, sizeof (struct skb_data)); 214 status = netif_rx (skb); 215 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev)) 216 devdbg (dev, "netif_rx status %d", status); 217} 218EXPORT_SYMBOL_GPL(usbnet_skb_return); 219 220 221/*------------------------------------------------------------------------- 222 * 223 * Network Device Driver (peer link to "Host Device", from USB host) 224 * 225 *-------------------------------------------------------------------------*/ 226 227static int usbnet_change_mtu (struct net_device *net, int new_mtu) 228{ 229 struct usbnet *dev = netdev_priv(net); 230 int ll_mtu = new_mtu + net->hard_header_len; 231 232 if (new_mtu <= 0 || ll_mtu > dev->hard_mtu) 233 return -EINVAL; 234 // no second zero-length packet read wanted after mtu-sized packets 235 if ((ll_mtu % dev->maxpacket) == 0) 236 return -EDOM; 237 net->mtu = new_mtu; 238 return 0; 239} 240 241/*-------------------------------------------------------------------------*/ 242 243static struct net_device_stats *usbnet_get_stats (struct net_device *net) 244{ 245 struct usbnet *dev = netdev_priv(net); 246 return &dev->stats; 247} 248 249/*-------------------------------------------------------------------------*/ 250 251/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from 252 * completion callbacks. 2.5 should have fixed those bugs... 253 */ 254 255static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list) 256{ 257 unsigned long flags; 258 259 spin_lock_irqsave(&list->lock, flags); 260 __skb_unlink(skb, list); 261 spin_unlock(&list->lock); 262 spin_lock(&dev->done.lock); 263 __skb_queue_tail(&dev->done, skb); 264 if (dev->done.qlen == 1) 265 tasklet_schedule(&dev->bh); 266 spin_unlock_irqrestore(&dev->done.lock, flags); 267} 268 269/* some work can't be done in tasklets, so we use keventd 270 * 271 * NOTE: annoying asymmetry: if it's active, schedule_work() fails, 272 * but tasklet_schedule() doesn't. hope the failure is rare. 273 */ 274void usbnet_defer_kevent (struct usbnet *dev, int work) 275{ 276 set_bit (work, &dev->flags); 277 if (!schedule_work (&dev->kevent)) 278 deverr (dev, "kevent %d may have been dropped", work); 279 else 280 devdbg (dev, "kevent %d scheduled", work); 281} 282EXPORT_SYMBOL_GPL(usbnet_defer_kevent); 283 284/*-------------------------------------------------------------------------*/ 285 286static void rx_complete (struct urb *urb, struct pt_regs *regs); 287 288static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) 289{ 290 struct sk_buff *skb; 291 struct skb_data *entry; 292 int retval = 0; 293 unsigned long lockflags; 294 size_t size = dev->rx_urb_size; 295 296 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) { 297 if (netif_msg_rx_err (dev)) 298 devdbg (dev, "no rx skb"); 299 usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 300 usb_free_urb (urb); 301 return; 302 } 303 skb_reserve (skb, NET_IP_ALIGN); 304 305 entry = (struct skb_data *) skb->cb; 306 entry->urb = urb; 307 entry->dev = dev; 308 entry->state = rx_start; 309 entry->length = 0; 310 311 usb_fill_bulk_urb (urb, dev->udev, dev->in, 312 skb->data, size, rx_complete, skb); 313 314 spin_lock_irqsave (&dev->rxq.lock, lockflags); 315 316 if (netif_running (dev->net) 317 && netif_device_present (dev->net) 318 && !test_bit (EVENT_RX_HALT, &dev->flags)) { 319 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){ 320 case -EPIPE: 321 usbnet_defer_kevent (dev, EVENT_RX_HALT); 322 break; 323 case -ENOMEM: 324 usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 325 break; 326 case -ENODEV: 327 if (netif_msg_ifdown (dev)) 328 devdbg (dev, "device gone"); 329 netif_device_detach (dev->net); 330 break; 331 default: 332 if (netif_msg_rx_err (dev)) 333 devdbg (dev, "rx submit, %d", retval); 334 tasklet_schedule (&dev->bh); 335 break; 336 case 0: 337 __skb_queue_tail (&dev->rxq, skb); 338 } 339 } else { 340 if (netif_msg_ifdown (dev)) 341 devdbg (dev, "rx: stopped"); 342 retval = -ENOLINK; 343 } 344 spin_unlock_irqrestore (&dev->rxq.lock, lockflags); 345 if (retval) { 346 dev_kfree_skb_any (skb); 347 usb_free_urb (urb); 348 } 349} 350 351 352/*-------------------------------------------------------------------------*/ 353 354static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) 355{ 356 if (dev->driver_info->rx_fixup 357 && !dev->driver_info->rx_fixup (dev, skb)) 358 goto error; 359 // else network stack removes extra byte if we forced a short packet 360 361 if (skb->len) 362 usbnet_skb_return (dev, skb); 363 else { 364 if (netif_msg_rx_err (dev)) 365 devdbg (dev, "drop"); 366error: 367 dev->stats.rx_errors++; 368 skb_queue_tail (&dev->done, skb); 369 } 370} 371 372/*-------------------------------------------------------------------------*/ 373 374static void rx_complete (struct urb *urb, struct pt_regs *regs) 375{ 376 struct sk_buff *skb = (struct sk_buff *) urb->context; 377 struct skb_data *entry = (struct skb_data *) skb->cb; 378 struct usbnet *dev = entry->dev; 379 int urb_status = urb->status; 380 381 skb_put (skb, urb->actual_length); 382 entry->state = rx_done; 383 entry->urb = NULL; 384 385 switch (urb_status) { 386 // success 387 case 0: 388 if (skb->len < dev->net->hard_header_len) { 389 entry->state = rx_cleanup; 390 dev->stats.rx_errors++; 391 dev->stats.rx_length_errors++; 392 if (netif_msg_rx_err (dev)) 393 devdbg (dev, "rx length %d", skb->len); 394 } 395 break; 396 397 // stalls need manual reset. this is rare ... except that 398 // when going through USB 2.0 TTs, unplug appears this way. 399 // we avoid the highspeed version of the ETIMEOUT/EILSEQ 400 // storm, recovering as needed. 401 case -EPIPE: 402 dev->stats.rx_errors++; 403 usbnet_defer_kevent (dev, EVENT_RX_HALT); 404 // FALLTHROUGH 405 406 // software-driven interface shutdown 407 case -ECONNRESET: // async unlink 408 case -ESHUTDOWN: // hardware gone 409 if (netif_msg_ifdown (dev)) 410 devdbg (dev, "rx shutdown, code %d", urb_status); 411 goto block; 412 413 // we get controller i/o faults during khubd disconnect() delays. 414 // throttle down resubmits, to avoid log floods; just temporarily, 415 // so we still recover when the fault isn't a khubd delay. 416 case -EPROTO: // ehci 417 case -ETIMEDOUT: // ohci 418 case -EILSEQ: // uhci 419 dev->stats.rx_errors++; 420 if (!timer_pending (&dev->delay)) { 421 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); 422 if (netif_msg_link (dev)) 423 devdbg (dev, "rx throttle %d", urb_status); 424 } 425block: 426 entry->state = rx_cleanup; 427 entry->urb = urb; 428 urb = NULL; 429 break; 430 431 // data overrun ... flush fifo? 432 case -EOVERFLOW: 433 dev->stats.rx_over_errors++; 434 // FALLTHROUGH 435 436 default: 437 entry->state = rx_cleanup; 438 dev->stats.rx_errors++; 439 if (netif_msg_rx_err (dev)) 440 devdbg (dev, "rx status %d", urb_status); 441 break; 442 } 443 444 defer_bh(dev, skb, &dev->rxq); 445 446 if (urb) { 447 if (netif_running (dev->net) 448 && !test_bit (EVENT_RX_HALT, &dev->flags)) { 449 rx_submit (dev, urb, GFP_ATOMIC); 450 return; 451 } 452 usb_free_urb (urb); 453 } 454 if (netif_msg_rx_err (dev)) 455 devdbg (dev, "no read resubmitted"); 456} 457 458static void intr_complete (struct urb *urb, struct pt_regs *regs) 459{ 460 struct usbnet *dev = urb->context; 461 int status = urb->status; 462 463 switch (status) { 464 /* success */ 465 case 0: 466 dev->driver_info->status(dev, urb); 467 break; 468 469 /* software-driven interface shutdown */ 470 case -ENOENT: // urb killed 471 case -ESHUTDOWN: // hardware gone 472 if (netif_msg_ifdown (dev)) 473 devdbg (dev, "intr shutdown, code %d", status); 474 return; 475 476 /* NOTE: not throttling like RX/TX, since this endpoint 477 * already polls infrequently 478 */ 479 default: 480 devdbg (dev, "intr status %d", status); 481 break; 482 } 483 484 if (!netif_running (dev->net)) 485 return; 486 487 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length); 488 status = usb_submit_urb (urb, GFP_ATOMIC); 489 if (status != 0 && netif_msg_timer (dev)) 490 deverr(dev, "intr resubmit --> %d", status); 491} 492 493/*-------------------------------------------------------------------------*/ 494 495// unlink pending rx/tx; completion handlers do all other cleanup 496 497static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 498{ 499 unsigned long flags; 500 struct sk_buff *skb, *skbnext; 501 int count = 0; 502 503 spin_lock_irqsave (&q->lock, flags); 504 for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) { 505 struct skb_data *entry; 506 struct urb *urb; 507 int retval; 508 509 entry = (struct skb_data *) skb->cb; 510 urb = entry->urb; 511 skbnext = skb->next; 512 513 // during some PM-driven resume scenarios, 514 // these (async) unlinks complete immediately 515 retval = usb_unlink_urb (urb); 516 if (retval != -EINPROGRESS && retval != 0) 517 devdbg (dev, "unlink urb err, %d", retval); 518 else 519 count++; 520 } 521 spin_unlock_irqrestore (&q->lock, flags); 522 return count; 523} 524 525 526/*-------------------------------------------------------------------------*/ 527 528// precondition: never called in_interrupt 529 530static int usbnet_stop (struct net_device *net) 531{ 532 struct usbnet *dev = netdev_priv(net); 533 int temp; 534 DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup); 535 DECLARE_WAITQUEUE (wait, current); 536 537 netif_stop_queue (net); 538 539 if (netif_msg_ifdown (dev)) 540 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld", 541 dev->stats.rx_packets, dev->stats.tx_packets, 542 dev->stats.rx_errors, dev->stats.tx_errors 543 ); 544 545 // ensure there are no more active urbs 546 add_wait_queue (&unlink_wakeup, &wait); 547 dev->wait = &unlink_wakeup; 548 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq); 549 550 // maybe wait for deletions to finish. 551 while (!skb_queue_empty(&dev->rxq) && 552 !skb_queue_empty(&dev->txq) && 553 !skb_queue_empty(&dev->done)) { 554 msleep(UNLINK_TIMEOUT_MS); 555 if (netif_msg_ifdown (dev)) 556 devdbg (dev, "waited for %d urb completions", temp); 557 } 558 dev->wait = NULL; 559 remove_wait_queue (&unlink_wakeup, &wait); 560 561 usb_kill_urb(dev->interrupt); 562 563 /* deferred work (task, timer, softirq) must also stop. 564 * can't flush_scheduled_work() until we drop rtnl (later), 565 * else workers could deadlock; so make workers a NOP. 566 */ 567 dev->flags = 0; 568 del_timer_sync (&dev->delay); 569 tasklet_kill (&dev->bh); 570 571 return 0; 572} 573 574/*-------------------------------------------------------------------------*/ 575 576// posts reads, and enables write queuing 577 578// precondition: never called in_interrupt 579 580static int usbnet_open (struct net_device *net) 581{ 582 struct usbnet *dev = netdev_priv(net); 583 int retval = 0; 584 struct driver_info *info = dev->driver_info; 585 586 // put into "known safe" state 587 if (info->reset && (retval = info->reset (dev)) < 0) { 588 if (netif_msg_ifup (dev)) 589 devinfo (dev, 590 "open reset fail (%d) usbnet usb-%s-%s, %s", 591 retval, 592 dev->udev->bus->bus_name, dev->udev->devpath, 593 info->description); 594 goto done; 595 } 596 597 // insist peer be connected 598 if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 599 if (netif_msg_ifup (dev)) 600 devdbg (dev, "can't open; %d", retval); 601 goto done; 602 } 603 604 /* start any status interrupt transfer */ 605 if (dev->interrupt) { 606 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); 607 if (retval < 0) { 608 if (netif_msg_ifup (dev)) 609 deverr (dev, "intr submit %d", retval); 610 goto done; 611 } 612 } 613 614 netif_start_queue (net); 615 if (netif_msg_ifup (dev)) { 616 char *framing; 617 618 if (dev->driver_info->flags & FLAG_FRAMING_NC) 619 framing = "NetChip"; 620 else if (dev->driver_info->flags & FLAG_FRAMING_GL) 621 framing = "GeneSys"; 622 else if (dev->driver_info->flags & FLAG_FRAMING_Z) 623 framing = "Zaurus"; 624 else if (dev->driver_info->flags & FLAG_FRAMING_RN) 625 framing = "RNDIS"; 626 else if (dev->driver_info->flags & FLAG_FRAMING_AX) 627 framing = "ASIX"; 628 else 629 framing = "simple"; 630 631 devinfo (dev, "open: enable queueing " 632 "(rx %d, tx %d) mtu %d %s framing", 633 RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu, 634 framing); 635 } 636 637 // delay posting reads until we're fully open 638 tasklet_schedule (&dev->bh); 639done: 640 return retval; 641} 642 643/*-------------------------------------------------------------------------*/ 644 645/* ethtool methods; minidrivers may need to add some more, but 646 * they'll probably want to use this base set. 647 */ 648 649void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 650{ 651 struct usbnet *dev = netdev_priv(net); 652 653 /* REVISIT don't always return "usbnet" */ 654 strncpy (info->driver, driver_name, sizeof info->driver); 655 strncpy (info->version, DRIVER_VERSION, sizeof info->version); 656 strncpy (info->fw_version, dev->driver_info->description, 657 sizeof info->fw_version); 658 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 659} 660EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 661 662static u32 usbnet_get_link (struct net_device *net) 663{ 664 struct usbnet *dev = netdev_priv(net); 665 666 /* If a check_connect is defined, return its result */ 667 if (dev->driver_info->check_connect) 668 return dev->driver_info->check_connect (dev) == 0; 669 670 /* Otherwise, say we're up (to avoid breaking scripts) */ 671 return 1; 672} 673 674u32 usbnet_get_msglevel (struct net_device *net) 675{ 676 struct usbnet *dev = netdev_priv(net); 677 678 return dev->msg_enable; 679} 680EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 681 682void usbnet_set_msglevel (struct net_device *net, u32 level) 683{ 684 struct usbnet *dev = netdev_priv(net); 685 686 dev->msg_enable = level; 687} 688EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 689 690/* drivers may override default ethtool_ops in their bind() routine */ 691static struct ethtool_ops usbnet_ethtool_ops = { 692 .get_drvinfo = usbnet_get_drvinfo, 693 .get_link = usbnet_get_link, 694 .get_msglevel = usbnet_get_msglevel, 695 .set_msglevel = usbnet_set_msglevel, 696}; 697 698/*-------------------------------------------------------------------------*/ 699 700/* work that cannot be done in interrupt context uses keventd. 701 * 702 * NOTE: with 2.5 we could do more of this using completion callbacks, 703 * especially now that control transfers can be queued. 704 */ 705static void 706kevent (void *data) 707{ 708 struct usbnet *dev = data; 709 int status; 710 711 /* usb_clear_halt() needs a thread context */ 712 if (test_bit (EVENT_TX_HALT, &dev->flags)) { 713 unlink_urbs (dev, &dev->txq); 714 status = usb_clear_halt (dev->udev, dev->out); 715 if (status < 0 716 && status != -EPIPE 717 && status != -ESHUTDOWN) { 718 if (netif_msg_tx_err (dev)) 719 deverr (dev, "can't clear tx halt, status %d", 720 status); 721 } else { 722 clear_bit (EVENT_TX_HALT, &dev->flags); 723 if (status != -ESHUTDOWN) 724 netif_wake_queue (dev->net); 725 } 726 } 727 if (test_bit (EVENT_RX_HALT, &dev->flags)) { 728 unlink_urbs (dev, &dev->rxq); 729 status = usb_clear_halt (dev->udev, dev->in); 730 if (status < 0 731 && status != -EPIPE 732 && status != -ESHUTDOWN) { 733 if (netif_msg_rx_err (dev)) 734 deverr (dev, "can't clear rx halt, status %d", 735 status); 736 } else { 737 clear_bit (EVENT_RX_HALT, &dev->flags); 738 tasklet_schedule (&dev->bh); 739 } 740 } 741 742 /* tasklet could resubmit itself forever if memory is tight */ 743 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 744 struct urb *urb = NULL; 745 746 if (netif_running (dev->net)) 747 urb = usb_alloc_urb (0, GFP_KERNEL); 748 else 749 clear_bit (EVENT_RX_MEMORY, &dev->flags); 750 if (urb != NULL) { 751 clear_bit (EVENT_RX_MEMORY, &dev->flags); 752 rx_submit (dev, urb, GFP_KERNEL); 753 tasklet_schedule (&dev->bh); 754 } 755 } 756 757 if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 758 struct driver_info *info = dev->driver_info; 759 int retval = 0; 760 761 clear_bit (EVENT_LINK_RESET, &dev->flags); 762 if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 763 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s", 764 retval, 765 dev->udev->bus->bus_name, dev->udev->devpath, 766 info->description); 767 } 768 } 769 770 if (dev->flags) 771 devdbg (dev, "kevent done, flags = 0x%lx", 772 dev->flags); 773} 774 775/*-------------------------------------------------------------------------*/ 776 777static void tx_complete (struct urb *urb, struct pt_regs *regs) 778{ 779 struct sk_buff *skb = (struct sk_buff *) urb->context; 780 struct skb_data *entry = (struct skb_data *) skb->cb; 781 struct usbnet *dev = entry->dev; 782 783 if (urb->status == 0) { 784 dev->stats.tx_packets++; 785 dev->stats.tx_bytes += entry->length; 786 } else { 787 dev->stats.tx_errors++; 788 789 switch (urb->status) { 790 case -EPIPE: 791 usbnet_defer_kevent (dev, EVENT_TX_HALT); 792 break; 793 794 /* software-driven interface shutdown */ 795 case -ECONNRESET: // async unlink 796 case -ESHUTDOWN: // hardware gone 797 break; 798 799 // like rx, tx gets controller i/o faults during khubd delays 800 // and so it uses the same throttling mechanism. 801 case -EPROTO: // ehci 802 case -ETIMEDOUT: // ohci 803 case -EILSEQ: // uhci 804 if (!timer_pending (&dev->delay)) { 805 mod_timer (&dev->delay, 806 jiffies + THROTTLE_JIFFIES); 807 if (netif_msg_link (dev)) 808 devdbg (dev, "tx throttle %d", 809 urb->status); 810 } 811 netif_stop_queue (dev->net); 812 break; 813 default: 814 if (netif_msg_tx_err (dev)) 815 devdbg (dev, "tx err %d", entry->urb->status); 816 break; 817 } 818 } 819 820 urb->dev = NULL; 821 entry->state = tx_done; 822 defer_bh(dev, skb, &dev->txq); 823} 824 825/*-------------------------------------------------------------------------*/ 826 827static void usbnet_tx_timeout (struct net_device *net) 828{ 829 struct usbnet *dev = netdev_priv(net); 830 831 unlink_urbs (dev, &dev->txq); 832 tasklet_schedule (&dev->bh); 833 834 // FIXME: device recovery -- reset? 835} 836 837/*-------------------------------------------------------------------------*/ 838 839static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net) 840{ 841 struct usbnet *dev = netdev_priv(net); 842 int length; 843 int retval = NET_XMIT_SUCCESS; 844 struct urb *urb = NULL; 845 struct skb_data *entry; 846 struct driver_info *info = dev->driver_info; 847 unsigned long flags; 848 849 // some devices want funky USB-level framing, for 850 // win32 driver (usually) and/or hardware quirks 851 if (info->tx_fixup) { 852 skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 853 if (!skb) { 854 if (netif_msg_tx_err (dev)) 855 devdbg (dev, "can't tx_fixup skb"); 856 goto drop; 857 } 858 } 859 length = skb->len; 860 861 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 862 if (netif_msg_tx_err (dev)) 863 devdbg (dev, "no urb"); 864 goto drop; 865 } 866 867 entry = (struct skb_data *) skb->cb; 868 entry->urb = urb; 869 entry->dev = dev; 870 entry->state = tx_start; 871 entry->length = length; 872 873 usb_fill_bulk_urb (urb, dev->udev, dev->out, 874 skb->data, skb->len, tx_complete, skb); 875 876 /* don't assume the hardware handles USB_ZERO_PACKET 877 * NOTE: strictly conforming cdc-ether devices should expect 878 * the ZLP here, but ignore the one-byte packet. 879 * 880 * FIXME zero that byte, if it doesn't require a new skb. 881 */ 882 if ((length % dev->maxpacket) == 0) 883 urb->transfer_buffer_length++; 884 885 spin_lock_irqsave (&dev->txq.lock, flags); 886 887 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 888 case -EPIPE: 889 netif_stop_queue (net); 890 usbnet_defer_kevent (dev, EVENT_TX_HALT); 891 break; 892 default: 893 if (netif_msg_tx_err (dev)) 894 devdbg (dev, "tx: submit urb err %d", retval); 895 break; 896 case 0: 897 net->trans_start = jiffies; 898 __skb_queue_tail (&dev->txq, skb); 899 if (dev->txq.qlen >= TX_QLEN (dev)) 900 netif_stop_queue (net); 901 } 902 spin_unlock_irqrestore (&dev->txq.lock, flags); 903 904 if (retval) { 905 if (netif_msg_tx_err (dev)) 906 devdbg (dev, "drop, code %d", retval); 907drop: 908 retval = NET_XMIT_SUCCESS; 909 dev->stats.tx_dropped++; 910 if (skb) 911 dev_kfree_skb_any (skb); 912 usb_free_urb (urb); 913 } else if (netif_msg_tx_queued (dev)) { 914 devdbg (dev, "> tx, len %d, type 0x%x", 915 length, skb->protocol); 916 } 917 return retval; 918} 919 920 921/*-------------------------------------------------------------------------*/ 922 923// tasklet (work deferred from completions, in_irq) or timer 924 925static void usbnet_bh (unsigned long param) 926{ 927 struct usbnet *dev = (struct usbnet *) param; 928 struct sk_buff *skb; 929 struct skb_data *entry; 930 931 while ((skb = skb_dequeue (&dev->done))) { 932 entry = (struct skb_data *) skb->cb; 933 switch (entry->state) { 934 case rx_done: 935 entry->state = rx_cleanup; 936 rx_process (dev, skb); 937 continue; 938 case tx_done: 939 case rx_cleanup: 940 usb_free_urb (entry->urb); 941 dev_kfree_skb (skb); 942 continue; 943 default: 944 devdbg (dev, "bogus skb state %d", entry->state); 945 } 946 } 947 948 // waiting for all pending urbs to complete? 949 if (dev->wait) { 950 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { 951 wake_up (dev->wait); 952 } 953 954 // or are we maybe short a few urbs? 955 } else if (netif_running (dev->net) 956 && netif_device_present (dev->net) 957 && !timer_pending (&dev->delay) 958 && !test_bit (EVENT_RX_HALT, &dev->flags)) { 959 int temp = dev->rxq.qlen; 960 int qlen = RX_QLEN (dev); 961 962 if (temp < qlen) { 963 struct urb *urb; 964 int i; 965 966 // don't refill the queue all at once 967 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) { 968 urb = usb_alloc_urb (0, GFP_ATOMIC); 969 if (urb != NULL) 970 rx_submit (dev, urb, GFP_ATOMIC); 971 } 972 if (temp != dev->rxq.qlen && netif_msg_link (dev)) 973 devdbg (dev, "rxqlen %d --> %d", 974 temp, dev->rxq.qlen); 975 if (dev->rxq.qlen < qlen) 976 tasklet_schedule (&dev->bh); 977 } 978 if (dev->txq.qlen < TX_QLEN (dev)) 979 netif_wake_queue (dev->net); 980 } 981} 982 983 984 985/*------------------------------------------------------------------------- 986 * 987 * USB Device Driver support 988 * 989 *-------------------------------------------------------------------------*/ 990 991// precondition: never called in_interrupt 992 993void usbnet_disconnect (struct usb_interface *intf) 994{ 995 struct usbnet *dev; 996 struct usb_device *xdev; 997 struct net_device *net; 998 999 dev = usb_get_intfdata(intf); 1000 usb_set_intfdata(intf, NULL); 1001 if (!dev) 1002 return; 1003 1004 xdev = interface_to_usbdev (intf); 1005 1006 if (netif_msg_probe (dev)) 1007 devinfo (dev, "unregister '%s' usb-%s-%s, %s", 1008 intf->dev.driver->name, 1009 xdev->bus->bus_name, xdev->devpath, 1010 dev->driver_info->description); 1011 1012 net = dev->net; 1013 unregister_netdev (net); 1014 1015 /* we don't hold rtnl here ... */ 1016 flush_scheduled_work (); 1017 1018 if (dev->driver_info->unbind) 1019 dev->driver_info->unbind (dev, intf); 1020 1021 free_netdev(net); 1022 usb_put_dev (xdev); 1023} 1024EXPORT_SYMBOL_GPL(usbnet_disconnect); 1025 1026 1027/*-------------------------------------------------------------------------*/ 1028 1029// precondition: never called in_interrupt 1030 1031int 1032usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 1033{ 1034 struct usbnet *dev; 1035 struct net_device *net; 1036 struct usb_host_interface *interface; 1037 struct driver_info *info; 1038 struct usb_device *xdev; 1039 int status; 1040 1041 info = (struct driver_info *) prod->driver_info; 1042 if (!info) { 1043 dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name); 1044 return -ENODEV; 1045 } 1046 xdev = interface_to_usbdev (udev); 1047 interface = udev->cur_altsetting; 1048 1049 usb_get_dev (xdev); 1050 1051 status = -ENOMEM; 1052 1053 // set up our own records 1054 net = alloc_etherdev(sizeof(*dev)); 1055 if (!net) { 1056 dbg ("can't kmalloc dev"); 1057 goto out; 1058 } 1059 1060 dev = netdev_priv(net); 1061 dev->udev = xdev; 1062 dev->driver_info = info; 1063 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 1064 | NETIF_MSG_PROBE | NETIF_MSG_LINK); 1065 skb_queue_head_init (&dev->rxq); 1066 skb_queue_head_init (&dev->txq); 1067 skb_queue_head_init (&dev->done); 1068 dev->bh.func = usbnet_bh; 1069 dev->bh.data = (unsigned long) dev; 1070 INIT_WORK (&dev->kevent, kevent, dev); 1071 dev->delay.function = usbnet_bh; 1072 dev->delay.data = (unsigned long) dev; 1073 init_timer (&dev->delay); 1074 1075 SET_MODULE_OWNER (net); 1076 dev->net = net; 1077 strcpy (net->name, "usb%d"); 1078 memcpy (net->dev_addr, node_id, sizeof node_id); 1079 1080 /* rx and tx sides can use different message sizes; 1081 * bind() should set rx_urb_size in that case. 1082 */ 1083 dev->hard_mtu = net->mtu + net->hard_header_len; 1084#if 0 1085// dma_supported() is deeply broken on almost all architectures 1086 // possible with some EHCI controllers 1087 if (dma_supported (&udev->dev, DMA_64BIT_MASK)) 1088 net->features |= NETIF_F_HIGHDMA; 1089#endif 1090 1091 net->change_mtu = usbnet_change_mtu; 1092 net->get_stats = usbnet_get_stats; 1093 net->hard_start_xmit = usbnet_start_xmit; 1094 net->open = usbnet_open; 1095 net->stop = usbnet_stop; 1096 net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 1097 net->tx_timeout = usbnet_tx_timeout; 1098 net->ethtool_ops = &usbnet_ethtool_ops; 1099 1100 // allow device-specific bind/init procedures 1101 // NOTE net->name still not usable ... 1102 if (info->bind) { 1103 status = info->bind (dev, udev); 1104 // heuristic: "usb%d" for links we know are two-host, 1105 // else "eth%d" when there's reasonable doubt. userspace 1106 // can rename the link if it knows better. 1107 if ((dev->driver_info->flags & FLAG_ETHER) != 0 1108 && (net->dev_addr [0] & 0x02) == 0) 1109 strcpy (net->name, "eth%d"); 1110 1111 /* maybe the remote can't receive an Ethernet MTU */ 1112 if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 1113 net->mtu = dev->hard_mtu - net->hard_header_len; 1114 } else if (!info->in || !info->out) 1115 status = usbnet_get_endpoints (dev, udev); 1116 else { 1117 dev->in = usb_rcvbulkpipe (xdev, info->in); 1118 dev->out = usb_sndbulkpipe (xdev, info->out); 1119 if (!(info->flags & FLAG_NO_SETINT)) 1120 status = usb_set_interface (xdev, 1121 interface->desc.bInterfaceNumber, 1122 interface->desc.bAlternateSetting); 1123 else 1124 status = 0; 1125 1126 } 1127 if (status == 0 && dev->status) 1128 status = init_status (dev, udev); 1129 if (status < 0) 1130 goto out1; 1131 1132 if (!dev->rx_urb_size) 1133 dev->rx_urb_size = dev->hard_mtu; 1134 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); 1135 1136 SET_NETDEV_DEV(net, &udev->dev); 1137 status = register_netdev (net); 1138 if (status) 1139 goto out3; 1140 if (netif_msg_probe (dev)) 1141 devinfo (dev, "register '%s' at usb-%s-%s, %s, " 1142 "%02x:%02x:%02x:%02x:%02x:%02x", 1143 udev->dev.driver->name, 1144 xdev->bus->bus_name, xdev->devpath, 1145 dev->driver_info->description, 1146 net->dev_addr [0], net->dev_addr [1], 1147 net->dev_addr [2], net->dev_addr [3], 1148 net->dev_addr [4], net->dev_addr [5]); 1149 1150 // ok, it's ready to go. 1151 usb_set_intfdata (udev, dev); 1152 1153 // start as if the link is up 1154 netif_device_attach (net); 1155 1156 return 0; 1157 1158out3: 1159 if (info->unbind) 1160 info->unbind (dev, udev); 1161out1: 1162 free_netdev(net); 1163out: 1164 usb_put_dev(xdev); 1165 return status; 1166} 1167EXPORT_SYMBOL_GPL(usbnet_probe); 1168 1169/*-------------------------------------------------------------------------*/ 1170 1171/* FIXME these suspend/resume methods assume non-CDC style 1172 * devices, with only one interface. 1173 */ 1174 1175int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 1176{ 1177 struct usbnet *dev = usb_get_intfdata(intf); 1178 1179 /* accelerate emptying of the rx and queues, to avoid 1180 * having everything error out. 1181 */ 1182 netif_device_detach (dev->net); 1183 (void) unlink_urbs (dev, &dev->rxq); 1184 (void) unlink_urbs (dev, &dev->txq); 1185 return 0; 1186} 1187EXPORT_SYMBOL_GPL(usbnet_suspend); 1188 1189int usbnet_resume (struct usb_interface *intf) 1190{ 1191 struct usbnet *dev = usb_get_intfdata(intf); 1192 1193 netif_device_attach (dev->net); 1194 tasklet_schedule (&dev->bh); 1195 return 0; 1196} 1197EXPORT_SYMBOL_GPL(usbnet_resume); 1198 1199 1200/*-------------------------------------------------------------------------*/ 1201 1202static int __init usbnet_init(void) 1203{ 1204 /* compiler should optimize this out */ 1205 BUG_ON (sizeof (((struct sk_buff *)0)->cb) 1206 < sizeof (struct skb_data)); 1207 1208 random_ether_addr(node_id); 1209 return 0; 1210} 1211module_init(usbnet_init); 1212 1213static void __exit usbnet_exit(void) 1214{ 1215} 1216module_exit(usbnet_exit); 1217 1218MODULE_AUTHOR("David Brownell"); 1219MODULE_DESCRIPTION("USB network driver framework"); 1220MODULE_LICENSE("GPL");