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