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