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, see <http://www.gnu.org/licenses/>. 18 */ 19 20 /* 21 * This is a generic "USB networking" framework that works with several 22 * kinds of full and high speed networking devices: host-to-host cables, 23 * smart usb peripherals, and actual Ethernet adapters. 24 * 25 * These devices usually differ in terms of control protocols (if they 26 * even have one!) and sometimes they define new framing to wrap or batch 27 * Ethernet packets. Otherwise, they talk to USB pretty much the same, 28 * so interface (un)binding, endpoint I/O queues, fault handling, and other 29 * issues can usefully be addressed by this framework. 30 */ 31 32 // #define DEBUG // error path messages, extra info 33 // #define VERBOSE // more; success messages 34 35 #include <linux/module.h> 36 #include <linux/init.h> 37 #include <linux/netdevice.h> 38 #include <linux/etherdevice.h> 39 #include <linux/ctype.h> 40 #include <linux/ethtool.h> 41 #include <linux/workqueue.h> 42 #include <linux/mii.h> 43 #include <linux/usb.h> 44 #include <linux/usb/usbnet.h> 45 #include <linux/usb/cdc.h> 46 #include <linux/slab.h> 47 #include <linux/kernel.h> 48 #include <linux/pm_runtime.h> 49 50 #define DRIVER_VERSION "22-Aug-2005" 51 52 53 /*-------------------------------------------------------------------------*/ 54 55 /* 56 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. 57 * Several dozen bytes of IPv4 data can fit in two such transactions. 58 * One maximum size Ethernet packet takes twenty four of them. 59 * For high speed, each frame comfortably fits almost 36 max size 60 * Ethernet packets (so queues should be bigger). 61 * 62 * The goal is to let the USB host controller be busy for 5msec or 63 * more before an irq is required, under load. Jumbograms change 64 * the equation. 65 */ 66 #define MAX_QUEUE_MEMORY (60 * 1518) 67 #define RX_QLEN(dev) ((dev)->rx_qlen) 68 #define TX_QLEN(dev) ((dev)->tx_qlen) 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 hub_wq might disconnect() 74 * us (it polls at HZ/4 usually) before we report too many false errors. 75 */ 76 #define THROTTLE_JIFFIES (HZ/8) 77 78 // between wakeups 79 #define UNLINK_TIMEOUT_MS 3 80 81 /*-------------------------------------------------------------------------*/ 82 83 // randomly generated ethernet address 84 static u8 node_id [ETH_ALEN]; 85 86 static const char driver_name [] = "usbnet"; 87 88 /* use ethtool to change the level for any given device */ 89 static int msg_level = -1; 90 module_param (msg_level, int, 0); 91 MODULE_PARM_DESC (msg_level, "Override default message level"); 92 93 /*-------------------------------------------------------------------------*/ 94 95 /* handles CDC Ethernet and many other network "bulk data" interfaces */ 96 int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) 97 { 98 int tmp; 99 struct usb_host_interface *alt = NULL; 100 struct usb_host_endpoint *in = NULL, *out = NULL; 101 struct usb_host_endpoint *status = NULL; 102 103 for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 104 unsigned ep; 105 106 in = out = status = NULL; 107 alt = intf->altsetting + tmp; 108 109 /* take the first altsetting with in-bulk + out-bulk; 110 * remember any status endpoint, just in case; 111 * ignore other endpoints and altsettings. 112 */ 113 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 114 struct usb_host_endpoint *e; 115 int intr = 0; 116 117 e = alt->endpoint + ep; 118 switch (e->desc.bmAttributes) { 119 case USB_ENDPOINT_XFER_INT: 120 if (!usb_endpoint_dir_in(&e->desc)) 121 continue; 122 intr = 1; 123 /* FALLTHROUGH */ 124 case USB_ENDPOINT_XFER_BULK: 125 break; 126 default: 127 continue; 128 } 129 if (usb_endpoint_dir_in(&e->desc)) { 130 if (!intr && !in) 131 in = e; 132 else if (intr && !status) 133 status = e; 134 } else { 135 if (!out) 136 out = e; 137 } 138 } 139 if (in && out) 140 break; 141 } 142 if (!alt || !in || !out) 143 return -EINVAL; 144 145 if (alt->desc.bAlternateSetting != 0 || 146 !(dev->driver_info->flags & FLAG_NO_SETINT)) { 147 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, 148 alt->desc.bAlternateSetting); 149 if (tmp < 0) 150 return tmp; 151 } 152 153 dev->in = usb_rcvbulkpipe (dev->udev, 154 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 155 dev->out = usb_sndbulkpipe (dev->udev, 156 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 157 dev->status = status; 158 return 0; 159 } 160 EXPORT_SYMBOL_GPL(usbnet_get_endpoints); 161 162 int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress) 163 { 164 int tmp = -1, ret; 165 unsigned char buf [13]; 166 167 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf); 168 if (ret == 12) 169 tmp = hex2bin(dev->net->dev_addr, buf, 6); 170 if (tmp < 0) { 171 dev_dbg(&dev->udev->dev, 172 "bad MAC string %d fetch, %d\n", iMACAddress, tmp); 173 if (ret >= 0) 174 ret = -EINVAL; 175 return ret; 176 } 177 return 0; 178 } 179 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); 180 181 static void intr_complete (struct urb *urb) 182 { 183 struct usbnet *dev = urb->context; 184 int status = urb->status; 185 186 switch (status) { 187 /* success */ 188 case 0: 189 dev->driver_info->status(dev, urb); 190 break; 191 192 /* software-driven interface shutdown */ 193 case -ENOENT: /* urb killed */ 194 case -ESHUTDOWN: /* hardware gone */ 195 netif_dbg(dev, ifdown, dev->net, 196 "intr shutdown, code %d\n", status); 197 return; 198 199 /* NOTE: not throttling like RX/TX, since this endpoint 200 * already polls infrequently 201 */ 202 default: 203 netdev_dbg(dev->net, "intr status %d\n", status); 204 break; 205 } 206 207 status = usb_submit_urb (urb, GFP_ATOMIC); 208 if (status != 0) 209 netif_err(dev, timer, dev->net, 210 "intr resubmit --> %d\n", status); 211 } 212 213 static int init_status (struct usbnet *dev, struct usb_interface *intf) 214 { 215 char *buf = NULL; 216 unsigned pipe = 0; 217 unsigned maxp; 218 unsigned period; 219 220 if (!dev->driver_info->status) 221 return 0; 222 223 pipe = usb_rcvintpipe (dev->udev, 224 dev->status->desc.bEndpointAddress 225 & USB_ENDPOINT_NUMBER_MASK); 226 maxp = usb_maxpacket (dev->udev, pipe, 0); 227 228 /* avoid 1 msec chatter: min 8 msec poll rate */ 229 period = max ((int) dev->status->desc.bInterval, 230 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 231 232 buf = kmalloc (maxp, GFP_KERNEL); 233 if (buf) { 234 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); 235 if (!dev->interrupt) { 236 kfree (buf); 237 return -ENOMEM; 238 } else { 239 usb_fill_int_urb(dev->interrupt, dev->udev, pipe, 240 buf, maxp, intr_complete, dev, period); 241 dev->interrupt->transfer_flags |= URB_FREE_BUFFER; 242 dev_dbg(&intf->dev, 243 "status ep%din, %d bytes period %d\n", 244 usb_pipeendpoint(pipe), maxp, period); 245 } 246 } 247 return 0; 248 } 249 250 /* Submit the interrupt URB if not previously submitted, increasing refcount */ 251 int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags) 252 { 253 int ret = 0; 254 255 WARN_ON_ONCE(dev->interrupt == NULL); 256 if (dev->interrupt) { 257 mutex_lock(&dev->interrupt_mutex); 258 259 if (++dev->interrupt_count == 1) 260 ret = usb_submit_urb(dev->interrupt, mem_flags); 261 262 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n", 263 dev->interrupt_count); 264 mutex_unlock(&dev->interrupt_mutex); 265 } 266 return ret; 267 } 268 EXPORT_SYMBOL_GPL(usbnet_status_start); 269 270 /* For resume; submit interrupt URB if previously submitted */ 271 static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags) 272 { 273 int ret = 0; 274 275 mutex_lock(&dev->interrupt_mutex); 276 if (dev->interrupt_count) { 277 ret = usb_submit_urb(dev->interrupt, mem_flags); 278 dev_dbg(&dev->udev->dev, 279 "submitted interrupt URB for resume\n"); 280 } 281 mutex_unlock(&dev->interrupt_mutex); 282 return ret; 283 } 284 285 /* Kill the interrupt URB if all submitters want it killed */ 286 void usbnet_status_stop(struct usbnet *dev) 287 { 288 if (dev->interrupt) { 289 mutex_lock(&dev->interrupt_mutex); 290 WARN_ON(dev->interrupt_count == 0); 291 292 if (dev->interrupt_count && --dev->interrupt_count == 0) 293 usb_kill_urb(dev->interrupt); 294 295 dev_dbg(&dev->udev->dev, 296 "decremented interrupt URB count to %d\n", 297 dev->interrupt_count); 298 mutex_unlock(&dev->interrupt_mutex); 299 } 300 } 301 EXPORT_SYMBOL_GPL(usbnet_status_stop); 302 303 /* For suspend; always kill interrupt URB */ 304 static void __usbnet_status_stop_force(struct usbnet *dev) 305 { 306 if (dev->interrupt) { 307 mutex_lock(&dev->interrupt_mutex); 308 usb_kill_urb(dev->interrupt); 309 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n"); 310 mutex_unlock(&dev->interrupt_mutex); 311 } 312 } 313 314 /* Passes this packet up the stack, updating its accounting. 315 * Some link protocols batch packets, so their rx_fixup paths 316 * can return clones as well as just modify the original skb. 317 */ 318 void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) 319 { 320 int status; 321 322 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) { 323 skb_queue_tail(&dev->rxq_pause, skb); 324 return; 325 } 326 327 skb->protocol = eth_type_trans (skb, dev->net); 328 dev->net->stats.rx_packets++; 329 dev->net->stats.rx_bytes += skb->len; 330 331 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n", 332 skb->len + sizeof (struct ethhdr), skb->protocol); 333 memset (skb->cb, 0, sizeof (struct skb_data)); 334 335 if (skb_defer_rx_timestamp(skb)) 336 return; 337 338 status = netif_rx (skb); 339 if (status != NET_RX_SUCCESS) 340 netif_dbg(dev, rx_err, dev->net, 341 "netif_rx status %d\n", status); 342 } 343 EXPORT_SYMBOL_GPL(usbnet_skb_return); 344 345 /* must be called if hard_mtu or rx_urb_size changed */ 346 void usbnet_update_max_qlen(struct usbnet *dev) 347 { 348 enum usb_device_speed speed = dev->udev->speed; 349 350 switch (speed) { 351 case USB_SPEED_HIGH: 352 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size; 353 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu; 354 break; 355 case USB_SPEED_SUPER: 356 /* 357 * Not take default 5ms qlen for super speed HC to 358 * save memory, and iperf tests show 2.5ms qlen can 359 * work well 360 */ 361 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size; 362 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu; 363 break; 364 default: 365 dev->rx_qlen = dev->tx_qlen = 4; 366 } 367 } 368 EXPORT_SYMBOL_GPL(usbnet_update_max_qlen); 369 370 371 /*------------------------------------------------------------------------- 372 * 373 * Network Device Driver (peer link to "Host Device", from USB host) 374 * 375 *-------------------------------------------------------------------------*/ 376 377 int usbnet_change_mtu (struct net_device *net, int new_mtu) 378 { 379 struct usbnet *dev = netdev_priv(net); 380 int ll_mtu = new_mtu + net->hard_header_len; 381 int old_hard_mtu = dev->hard_mtu; 382 int old_rx_urb_size = dev->rx_urb_size; 383 384 if (new_mtu <= 0) 385 return -EINVAL; 386 // no second zero-length packet read wanted after mtu-sized packets 387 if ((ll_mtu % dev->maxpacket) == 0) 388 return -EDOM; 389 net->mtu = new_mtu; 390 391 dev->hard_mtu = net->mtu + net->hard_header_len; 392 if (dev->rx_urb_size == old_hard_mtu) { 393 dev->rx_urb_size = dev->hard_mtu; 394 if (dev->rx_urb_size > old_rx_urb_size) 395 usbnet_unlink_rx_urbs(dev); 396 } 397 398 /* max qlen depend on hard_mtu and rx_urb_size */ 399 usbnet_update_max_qlen(dev); 400 401 return 0; 402 } 403 EXPORT_SYMBOL_GPL(usbnet_change_mtu); 404 405 /* The caller must hold list->lock */ 406 static void __usbnet_queue_skb(struct sk_buff_head *list, 407 struct sk_buff *newsk, enum skb_state state) 408 { 409 struct skb_data *entry = (struct skb_data *) newsk->cb; 410 411 __skb_queue_tail(list, newsk); 412 entry->state = state; 413 } 414 415 /*-------------------------------------------------------------------------*/ 416 417 /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from 418 * completion callbacks. 2.5 should have fixed those bugs... 419 */ 420 421 static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb, 422 struct sk_buff_head *list, enum skb_state state) 423 { 424 unsigned long flags; 425 enum skb_state old_state; 426 struct skb_data *entry = (struct skb_data *) skb->cb; 427 428 spin_lock_irqsave(&list->lock, flags); 429 old_state = entry->state; 430 entry->state = state; 431 __skb_unlink(skb, list); 432 433 /* defer_bh() is never called with list == &dev->done. 434 * spin_lock_nested() tells lockdep that it is OK to take 435 * dev->done.lock here with list->lock held. 436 */ 437 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING); 438 439 __skb_queue_tail(&dev->done, skb); 440 if (dev->done.qlen == 1) 441 tasklet_schedule(&dev->bh); 442 spin_unlock(&dev->done.lock); 443 spin_unlock_irqrestore(&list->lock, flags); 444 return old_state; 445 } 446 447 /* some work can't be done in tasklets, so we use keventd 448 * 449 * NOTE: annoying asymmetry: if it's active, schedule_work() fails, 450 * but tasklet_schedule() doesn't. hope the failure is rare. 451 */ 452 void usbnet_defer_kevent (struct usbnet *dev, int work) 453 { 454 set_bit (work, &dev->flags); 455 if (!schedule_work (&dev->kevent)) { 456 if (net_ratelimit()) 457 netdev_err(dev->net, "kevent %d may have been dropped\n", work); 458 } else { 459 netdev_dbg(dev->net, "kevent %d scheduled\n", work); 460 } 461 } 462 EXPORT_SYMBOL_GPL(usbnet_defer_kevent); 463 464 /*-------------------------------------------------------------------------*/ 465 466 static void rx_complete (struct urb *urb); 467 468 static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) 469 { 470 struct sk_buff *skb; 471 struct skb_data *entry; 472 int retval = 0; 473 unsigned long lockflags; 474 size_t size = dev->rx_urb_size; 475 476 /* prevent rx skb allocation when error ratio is high */ 477 if (test_bit(EVENT_RX_KILL, &dev->flags)) { 478 usb_free_urb(urb); 479 return -ENOLINK; 480 } 481 482 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags); 483 if (!skb) { 484 netif_dbg(dev, rx_err, dev->net, "no rx skb\n"); 485 usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 486 usb_free_urb (urb); 487 return -ENOMEM; 488 } 489 490 entry = (struct skb_data *) skb->cb; 491 entry->urb = urb; 492 entry->dev = dev; 493 entry->length = 0; 494 495 usb_fill_bulk_urb (urb, dev->udev, dev->in, 496 skb->data, size, rx_complete, skb); 497 498 spin_lock_irqsave (&dev->rxq.lock, lockflags); 499 500 if (netif_running (dev->net) && 501 netif_device_present (dev->net) && 502 !test_bit (EVENT_RX_HALT, &dev->flags) && 503 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { 504 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { 505 case -EPIPE: 506 usbnet_defer_kevent (dev, EVENT_RX_HALT); 507 break; 508 case -ENOMEM: 509 usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 510 break; 511 case -ENODEV: 512 netif_dbg(dev, ifdown, dev->net, "device gone\n"); 513 netif_device_detach (dev->net); 514 break; 515 case -EHOSTUNREACH: 516 retval = -ENOLINK; 517 break; 518 default: 519 netif_dbg(dev, rx_err, dev->net, 520 "rx submit, %d\n", retval); 521 tasklet_schedule (&dev->bh); 522 break; 523 case 0: 524 __usbnet_queue_skb(&dev->rxq, skb, rx_start); 525 } 526 } else { 527 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n"); 528 retval = -ENOLINK; 529 } 530 spin_unlock_irqrestore (&dev->rxq.lock, lockflags); 531 if (retval) { 532 dev_kfree_skb_any (skb); 533 usb_free_urb (urb); 534 } 535 return retval; 536 } 537 538 539 /*-------------------------------------------------------------------------*/ 540 541 static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) 542 { 543 if (dev->driver_info->rx_fixup && 544 !dev->driver_info->rx_fixup (dev, skb)) { 545 /* With RX_ASSEMBLE, rx_fixup() must update counters */ 546 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE)) 547 dev->net->stats.rx_errors++; 548 goto done; 549 } 550 // else network stack removes extra byte if we forced a short packet 551 552 /* all data was already cloned from skb inside the driver */ 553 if (dev->driver_info->flags & FLAG_MULTI_PACKET) 554 goto done; 555 556 if (skb->len < ETH_HLEN) { 557 dev->net->stats.rx_errors++; 558 dev->net->stats.rx_length_errors++; 559 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len); 560 } else { 561 usbnet_skb_return(dev, skb); 562 return; 563 } 564 565 done: 566 skb_queue_tail(&dev->done, skb); 567 } 568 569 /*-------------------------------------------------------------------------*/ 570 571 static void rx_complete (struct urb *urb) 572 { 573 struct sk_buff *skb = (struct sk_buff *) urb->context; 574 struct skb_data *entry = (struct skb_data *) skb->cb; 575 struct usbnet *dev = entry->dev; 576 int urb_status = urb->status; 577 enum skb_state state; 578 579 skb_put (skb, urb->actual_length); 580 state = rx_done; 581 entry->urb = NULL; 582 583 switch (urb_status) { 584 /* success */ 585 case 0: 586 break; 587 588 /* stalls need manual reset. this is rare ... except that 589 * when going through USB 2.0 TTs, unplug appears this way. 590 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ 591 * storm, recovering as needed. 592 */ 593 case -EPIPE: 594 dev->net->stats.rx_errors++; 595 usbnet_defer_kevent (dev, EVENT_RX_HALT); 596 // FALLTHROUGH 597 598 /* software-driven interface shutdown */ 599 case -ECONNRESET: /* async unlink */ 600 case -ESHUTDOWN: /* hardware gone */ 601 netif_dbg(dev, ifdown, dev->net, 602 "rx shutdown, code %d\n", urb_status); 603 goto block; 604 605 /* we get controller i/o faults during hub_wq disconnect() delays. 606 * throttle down resubmits, to avoid log floods; just temporarily, 607 * so we still recover when the fault isn't a hub_wq delay. 608 */ 609 case -EPROTO: 610 case -ETIME: 611 case -EILSEQ: 612 dev->net->stats.rx_errors++; 613 if (!timer_pending (&dev->delay)) { 614 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); 615 netif_dbg(dev, link, dev->net, 616 "rx throttle %d\n", urb_status); 617 } 618 block: 619 state = rx_cleanup; 620 entry->urb = urb; 621 urb = NULL; 622 break; 623 624 /* data overrun ... flush fifo? */ 625 case -EOVERFLOW: 626 dev->net->stats.rx_over_errors++; 627 // FALLTHROUGH 628 629 default: 630 state = rx_cleanup; 631 dev->net->stats.rx_errors++; 632 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status); 633 break; 634 } 635 636 /* stop rx if packet error rate is high */ 637 if (++dev->pkt_cnt > 30) { 638 dev->pkt_cnt = 0; 639 dev->pkt_err = 0; 640 } else { 641 if (state == rx_cleanup) 642 dev->pkt_err++; 643 if (dev->pkt_err > 20) 644 set_bit(EVENT_RX_KILL, &dev->flags); 645 } 646 647 state = defer_bh(dev, skb, &dev->rxq, state); 648 649 if (urb) { 650 if (netif_running (dev->net) && 651 !test_bit (EVENT_RX_HALT, &dev->flags) && 652 state != unlink_start) { 653 rx_submit (dev, urb, GFP_ATOMIC); 654 usb_mark_last_busy(dev->udev); 655 return; 656 } 657 usb_free_urb (urb); 658 } 659 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n"); 660 } 661 662 /*-------------------------------------------------------------------------*/ 663 void usbnet_pause_rx(struct usbnet *dev) 664 { 665 set_bit(EVENT_RX_PAUSED, &dev->flags); 666 667 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n"); 668 } 669 EXPORT_SYMBOL_GPL(usbnet_pause_rx); 670 671 void usbnet_resume_rx(struct usbnet *dev) 672 { 673 struct sk_buff *skb; 674 int num = 0; 675 676 clear_bit(EVENT_RX_PAUSED, &dev->flags); 677 678 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { 679 usbnet_skb_return(dev, skb); 680 num++; 681 } 682 683 tasklet_schedule(&dev->bh); 684 685 netif_dbg(dev, rx_status, dev->net, 686 "paused rx queue disabled, %d skbs requeued\n", num); 687 } 688 EXPORT_SYMBOL_GPL(usbnet_resume_rx); 689 690 void usbnet_purge_paused_rxq(struct usbnet *dev) 691 { 692 skb_queue_purge(&dev->rxq_pause); 693 } 694 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); 695 696 /*-------------------------------------------------------------------------*/ 697 698 // unlink pending rx/tx; completion handlers do all other cleanup 699 700 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 701 { 702 unsigned long flags; 703 struct sk_buff *skb; 704 int count = 0; 705 706 spin_lock_irqsave (&q->lock, flags); 707 while (!skb_queue_empty(q)) { 708 struct skb_data *entry; 709 struct urb *urb; 710 int retval; 711 712 skb_queue_walk(q, skb) { 713 entry = (struct skb_data *) skb->cb; 714 if (entry->state != unlink_start) 715 goto found; 716 } 717 break; 718 found: 719 entry->state = unlink_start; 720 urb = entry->urb; 721 722 /* 723 * Get reference count of the URB to avoid it to be 724 * freed during usb_unlink_urb, which may trigger 725 * use-after-free problem inside usb_unlink_urb since 726 * usb_unlink_urb is always racing with .complete 727 * handler(include defer_bh). 728 */ 729 usb_get_urb(urb); 730 spin_unlock_irqrestore(&q->lock, flags); 731 // during some PM-driven resume scenarios, 732 // these (async) unlinks complete immediately 733 retval = usb_unlink_urb (urb); 734 if (retval != -EINPROGRESS && retval != 0) 735 netdev_dbg(dev->net, "unlink urb err, %d\n", retval); 736 else 737 count++; 738 usb_put_urb(urb); 739 spin_lock_irqsave(&q->lock, flags); 740 } 741 spin_unlock_irqrestore (&q->lock, flags); 742 return count; 743 } 744 745 // Flush all pending rx urbs 746 // minidrivers may need to do this when the MTU changes 747 748 void usbnet_unlink_rx_urbs(struct usbnet *dev) 749 { 750 if (netif_running(dev->net)) { 751 (void) unlink_urbs (dev, &dev->rxq); 752 tasklet_schedule(&dev->bh); 753 } 754 } 755 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); 756 757 /*-------------------------------------------------------------------------*/ 758 759 static void wait_skb_queue_empty(struct sk_buff_head *q) 760 { 761 unsigned long flags; 762 763 spin_lock_irqsave(&q->lock, flags); 764 while (!skb_queue_empty(q)) { 765 spin_unlock_irqrestore(&q->lock, flags); 766 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); 767 set_current_state(TASK_UNINTERRUPTIBLE); 768 spin_lock_irqsave(&q->lock, flags); 769 } 770 spin_unlock_irqrestore(&q->lock, flags); 771 } 772 773 // precondition: never called in_interrupt 774 static void usbnet_terminate_urbs(struct usbnet *dev) 775 { 776 DECLARE_WAITQUEUE(wait, current); 777 int temp; 778 779 /* ensure there are no more active urbs */ 780 add_wait_queue(&dev->wait, &wait); 781 set_current_state(TASK_UNINTERRUPTIBLE); 782 temp = unlink_urbs(dev, &dev->txq) + 783 unlink_urbs(dev, &dev->rxq); 784 785 /* maybe wait for deletions to finish. */ 786 wait_skb_queue_empty(&dev->rxq); 787 wait_skb_queue_empty(&dev->txq); 788 wait_skb_queue_empty(&dev->done); 789 netif_dbg(dev, ifdown, dev->net, 790 "waited for %d urb completions\n", temp); 791 set_current_state(TASK_RUNNING); 792 remove_wait_queue(&dev->wait, &wait); 793 } 794 795 int usbnet_stop (struct net_device *net) 796 { 797 struct usbnet *dev = netdev_priv(net); 798 struct driver_info *info = dev->driver_info; 799 int retval, pm, mpn; 800 801 clear_bit(EVENT_DEV_OPEN, &dev->flags); 802 netif_stop_queue (net); 803 804 netif_info(dev, ifdown, dev->net, 805 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", 806 net->stats.rx_packets, net->stats.tx_packets, 807 net->stats.rx_errors, net->stats.tx_errors); 808 809 /* to not race resume */ 810 pm = usb_autopm_get_interface(dev->intf); 811 /* allow minidriver to stop correctly (wireless devices to turn off 812 * radio etc) */ 813 if (info->stop) { 814 retval = info->stop(dev); 815 if (retval < 0) 816 netif_info(dev, ifdown, dev->net, 817 "stop fail (%d) usbnet usb-%s-%s, %s\n", 818 retval, 819 dev->udev->bus->bus_name, dev->udev->devpath, 820 info->description); 821 } 822 823 if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) 824 usbnet_terminate_urbs(dev); 825 826 usbnet_status_stop(dev); 827 828 usbnet_purge_paused_rxq(dev); 829 830 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 831 832 /* deferred work (task, timer, softirq) must also stop. 833 * can't flush_scheduled_work() until we drop rtnl (later), 834 * else workers could deadlock; so make workers a NOP. 835 */ 836 dev->flags = 0; 837 del_timer_sync (&dev->delay); 838 tasklet_kill (&dev->bh); 839 if (!pm) 840 usb_autopm_put_interface(dev->intf); 841 842 if (info->manage_power && mpn) 843 info->manage_power(dev, 0); 844 else 845 usb_autopm_put_interface(dev->intf); 846 847 return 0; 848 } 849 EXPORT_SYMBOL_GPL(usbnet_stop); 850 851 /*-------------------------------------------------------------------------*/ 852 853 // posts reads, and enables write queuing 854 855 // precondition: never called in_interrupt 856 857 int usbnet_open (struct net_device *net) 858 { 859 struct usbnet *dev = netdev_priv(net); 860 int retval; 861 struct driver_info *info = dev->driver_info; 862 863 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { 864 netif_info(dev, ifup, dev->net, 865 "resumption fail (%d) usbnet usb-%s-%s, %s\n", 866 retval, 867 dev->udev->bus->bus_name, 868 dev->udev->devpath, 869 info->description); 870 goto done_nopm; 871 } 872 873 // put into "known safe" state 874 if (info->reset && (retval = info->reset (dev)) < 0) { 875 netif_info(dev, ifup, dev->net, 876 "open reset fail (%d) usbnet usb-%s-%s, %s\n", 877 retval, 878 dev->udev->bus->bus_name, 879 dev->udev->devpath, 880 info->description); 881 goto done; 882 } 883 884 /* hard_mtu or rx_urb_size may change in reset() */ 885 usbnet_update_max_qlen(dev); 886 887 // insist peer be connected 888 if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 889 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval); 890 goto done; 891 } 892 893 /* start any status interrupt transfer */ 894 if (dev->interrupt) { 895 retval = usbnet_status_start(dev, GFP_KERNEL); 896 if (retval < 0) { 897 netif_err(dev, ifup, dev->net, 898 "intr submit %d\n", retval); 899 goto done; 900 } 901 } 902 903 set_bit(EVENT_DEV_OPEN, &dev->flags); 904 netif_start_queue (net); 905 netif_info(dev, ifup, dev->net, 906 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", 907 (int)RX_QLEN(dev), (int)TX_QLEN(dev), 908 dev->net->mtu, 909 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : 910 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : 911 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : 912 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : 913 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : 914 "simple"); 915 916 /* reset rx error state */ 917 dev->pkt_cnt = 0; 918 dev->pkt_err = 0; 919 clear_bit(EVENT_RX_KILL, &dev->flags); 920 921 // delay posting reads until we're fully open 922 tasklet_schedule (&dev->bh); 923 if (info->manage_power) { 924 retval = info->manage_power(dev, 1); 925 if (retval < 0) { 926 retval = 0; 927 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 928 } else { 929 usb_autopm_put_interface(dev->intf); 930 } 931 } 932 return retval; 933 done: 934 usb_autopm_put_interface(dev->intf); 935 done_nopm: 936 return retval; 937 } 938 EXPORT_SYMBOL_GPL(usbnet_open); 939 940 /*-------------------------------------------------------------------------*/ 941 942 /* ethtool methods; minidrivers may need to add some more, but 943 * they'll probably want to use this base set. 944 */ 945 946 int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) 947 { 948 struct usbnet *dev = netdev_priv(net); 949 950 if (!dev->mii.mdio_read) 951 return -EOPNOTSUPP; 952 953 return mii_ethtool_gset(&dev->mii, cmd); 954 } 955 EXPORT_SYMBOL_GPL(usbnet_get_settings); 956 957 int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) 958 { 959 struct usbnet *dev = netdev_priv(net); 960 int retval; 961 962 if (!dev->mii.mdio_write) 963 return -EOPNOTSUPP; 964 965 retval = mii_ethtool_sset(&dev->mii, cmd); 966 967 /* link speed/duplex might have changed */ 968 if (dev->driver_info->link_reset) 969 dev->driver_info->link_reset(dev); 970 971 /* hard_mtu or rx_urb_size may change in link_reset() */ 972 usbnet_update_max_qlen(dev); 973 974 return retval; 975 976 } 977 EXPORT_SYMBOL_GPL(usbnet_set_settings); 978 979 u32 usbnet_get_link (struct net_device *net) 980 { 981 struct usbnet *dev = netdev_priv(net); 982 983 /* If a check_connect is defined, return its result */ 984 if (dev->driver_info->check_connect) 985 return dev->driver_info->check_connect (dev) == 0; 986 987 /* if the device has mii operations, use those */ 988 if (dev->mii.mdio_read) 989 return mii_link_ok(&dev->mii); 990 991 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ 992 return ethtool_op_get_link(net); 993 } 994 EXPORT_SYMBOL_GPL(usbnet_get_link); 995 996 int usbnet_nway_reset(struct net_device *net) 997 { 998 struct usbnet *dev = netdev_priv(net); 999 1000 if (!dev->mii.mdio_write) 1001 return -EOPNOTSUPP; 1002 1003 return mii_nway_restart(&dev->mii); 1004 } 1005 EXPORT_SYMBOL_GPL(usbnet_nway_reset); 1006 1007 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 1008 { 1009 struct usbnet *dev = netdev_priv(net); 1010 1011 strlcpy (info->driver, dev->driver_name, sizeof info->driver); 1012 strlcpy (info->version, DRIVER_VERSION, sizeof info->version); 1013 strlcpy (info->fw_version, dev->driver_info->description, 1014 sizeof info->fw_version); 1015 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 1016 } 1017 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 1018 1019 u32 usbnet_get_msglevel (struct net_device *net) 1020 { 1021 struct usbnet *dev = netdev_priv(net); 1022 1023 return dev->msg_enable; 1024 } 1025 EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 1026 1027 void usbnet_set_msglevel (struct net_device *net, u32 level) 1028 { 1029 struct usbnet *dev = netdev_priv(net); 1030 1031 dev->msg_enable = level; 1032 } 1033 EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 1034 1035 /* drivers may override default ethtool_ops in their bind() routine */ 1036 static const struct ethtool_ops usbnet_ethtool_ops = { 1037 .get_settings = usbnet_get_settings, 1038 .set_settings = usbnet_set_settings, 1039 .get_link = usbnet_get_link, 1040 .nway_reset = usbnet_nway_reset, 1041 .get_drvinfo = usbnet_get_drvinfo, 1042 .get_msglevel = usbnet_get_msglevel, 1043 .set_msglevel = usbnet_set_msglevel, 1044 .get_ts_info = ethtool_op_get_ts_info, 1045 }; 1046 1047 /*-------------------------------------------------------------------------*/ 1048 1049 static void __handle_link_change(struct usbnet *dev) 1050 { 1051 if (!test_bit(EVENT_DEV_OPEN, &dev->flags)) 1052 return; 1053 1054 if (!netif_carrier_ok(dev->net)) { 1055 /* kill URBs for reading packets to save bus bandwidth */ 1056 unlink_urbs(dev, &dev->rxq); 1057 1058 /* 1059 * tx_timeout will unlink URBs for sending packets and 1060 * tx queue is stopped by netcore after link becomes off 1061 */ 1062 } else { 1063 /* submitting URBs for reading packets */ 1064 tasklet_schedule(&dev->bh); 1065 } 1066 1067 /* hard_mtu or rx_urb_size may change during link change */ 1068 usbnet_update_max_qlen(dev); 1069 1070 clear_bit(EVENT_LINK_CHANGE, &dev->flags); 1071 } 1072 1073 static void usbnet_set_rx_mode(struct net_device *net) 1074 { 1075 struct usbnet *dev = netdev_priv(net); 1076 1077 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE); 1078 } 1079 1080 static void __handle_set_rx_mode(struct usbnet *dev) 1081 { 1082 if (dev->driver_info->set_rx_mode) 1083 (dev->driver_info->set_rx_mode)(dev); 1084 1085 clear_bit(EVENT_SET_RX_MODE, &dev->flags); 1086 } 1087 1088 /* work that cannot be done in interrupt context uses keventd. 1089 * 1090 * NOTE: with 2.5 we could do more of this using completion callbacks, 1091 * especially now that control transfers can be queued. 1092 */ 1093 static void 1094 usbnet_deferred_kevent (struct work_struct *work) 1095 { 1096 struct usbnet *dev = 1097 container_of(work, struct usbnet, kevent); 1098 int status; 1099 1100 /* usb_clear_halt() needs a thread context */ 1101 if (test_bit (EVENT_TX_HALT, &dev->flags)) { 1102 unlink_urbs (dev, &dev->txq); 1103 status = usb_autopm_get_interface(dev->intf); 1104 if (status < 0) 1105 goto fail_pipe; 1106 status = usb_clear_halt (dev->udev, dev->out); 1107 usb_autopm_put_interface(dev->intf); 1108 if (status < 0 && 1109 status != -EPIPE && 1110 status != -ESHUTDOWN) { 1111 if (netif_msg_tx_err (dev)) 1112 fail_pipe: 1113 netdev_err(dev->net, "can't clear tx halt, status %d\n", 1114 status); 1115 } else { 1116 clear_bit (EVENT_TX_HALT, &dev->flags); 1117 if (status != -ESHUTDOWN) 1118 netif_wake_queue (dev->net); 1119 } 1120 } 1121 if (test_bit (EVENT_RX_HALT, &dev->flags)) { 1122 unlink_urbs (dev, &dev->rxq); 1123 status = usb_autopm_get_interface(dev->intf); 1124 if (status < 0) 1125 goto fail_halt; 1126 status = usb_clear_halt (dev->udev, dev->in); 1127 usb_autopm_put_interface(dev->intf); 1128 if (status < 0 && 1129 status != -EPIPE && 1130 status != -ESHUTDOWN) { 1131 if (netif_msg_rx_err (dev)) 1132 fail_halt: 1133 netdev_err(dev->net, "can't clear rx halt, status %d\n", 1134 status); 1135 } else { 1136 clear_bit (EVENT_RX_HALT, &dev->flags); 1137 tasklet_schedule (&dev->bh); 1138 } 1139 } 1140 1141 /* tasklet could resubmit itself forever if memory is tight */ 1142 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 1143 struct urb *urb = NULL; 1144 int resched = 1; 1145 1146 if (netif_running (dev->net)) 1147 urb = usb_alloc_urb (0, GFP_KERNEL); 1148 else 1149 clear_bit (EVENT_RX_MEMORY, &dev->flags); 1150 if (urb != NULL) { 1151 clear_bit (EVENT_RX_MEMORY, &dev->flags); 1152 status = usb_autopm_get_interface(dev->intf); 1153 if (status < 0) { 1154 usb_free_urb(urb); 1155 goto fail_lowmem; 1156 } 1157 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) 1158 resched = 0; 1159 usb_autopm_put_interface(dev->intf); 1160 fail_lowmem: 1161 if (resched) 1162 tasklet_schedule (&dev->bh); 1163 } 1164 } 1165 1166 if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 1167 struct driver_info *info = dev->driver_info; 1168 int retval = 0; 1169 1170 clear_bit (EVENT_LINK_RESET, &dev->flags); 1171 status = usb_autopm_get_interface(dev->intf); 1172 if (status < 0) 1173 goto skip_reset; 1174 if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 1175 usb_autopm_put_interface(dev->intf); 1176 skip_reset: 1177 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", 1178 retval, 1179 dev->udev->bus->bus_name, 1180 dev->udev->devpath, 1181 info->description); 1182 } else { 1183 usb_autopm_put_interface(dev->intf); 1184 } 1185 1186 /* handle link change from link resetting */ 1187 __handle_link_change(dev); 1188 } 1189 1190 if (test_bit (EVENT_LINK_CHANGE, &dev->flags)) 1191 __handle_link_change(dev); 1192 1193 if (test_bit (EVENT_SET_RX_MODE, &dev->flags)) 1194 __handle_set_rx_mode(dev); 1195 1196 1197 if (dev->flags) 1198 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); 1199 } 1200 1201 /*-------------------------------------------------------------------------*/ 1202 1203 static void tx_complete (struct urb *urb) 1204 { 1205 struct sk_buff *skb = (struct sk_buff *) urb->context; 1206 struct skb_data *entry = (struct skb_data *) skb->cb; 1207 struct usbnet *dev = entry->dev; 1208 1209 if (urb->status == 0) { 1210 dev->net->stats.tx_packets += entry->packets; 1211 dev->net->stats.tx_bytes += entry->length; 1212 } else { 1213 dev->net->stats.tx_errors++; 1214 1215 switch (urb->status) { 1216 case -EPIPE: 1217 usbnet_defer_kevent (dev, EVENT_TX_HALT); 1218 break; 1219 1220 /* software-driven interface shutdown */ 1221 case -ECONNRESET: // async unlink 1222 case -ESHUTDOWN: // hardware gone 1223 break; 1224 1225 /* like rx, tx gets controller i/o faults during hub_wq 1226 * delays and so it uses the same throttling mechanism. 1227 */ 1228 case -EPROTO: 1229 case -ETIME: 1230 case -EILSEQ: 1231 usb_mark_last_busy(dev->udev); 1232 if (!timer_pending (&dev->delay)) { 1233 mod_timer (&dev->delay, 1234 jiffies + THROTTLE_JIFFIES); 1235 netif_dbg(dev, link, dev->net, 1236 "tx throttle %d\n", urb->status); 1237 } 1238 netif_stop_queue (dev->net); 1239 break; 1240 default: 1241 netif_dbg(dev, tx_err, dev->net, 1242 "tx err %d\n", entry->urb->status); 1243 break; 1244 } 1245 } 1246 1247 usb_autopm_put_interface_async(dev->intf); 1248 (void) defer_bh(dev, skb, &dev->txq, tx_done); 1249 } 1250 1251 /*-------------------------------------------------------------------------*/ 1252 1253 void usbnet_tx_timeout (struct net_device *net) 1254 { 1255 struct usbnet *dev = netdev_priv(net); 1256 1257 unlink_urbs (dev, &dev->txq); 1258 tasklet_schedule (&dev->bh); 1259 /* this needs to be handled individually because the generic layer 1260 * doesn't know what is sufficient and could not restore private 1261 * information if a remedy of an unconditional reset were used. 1262 */ 1263 if (dev->driver_info->recover) 1264 (dev->driver_info->recover)(dev); 1265 } 1266 EXPORT_SYMBOL_GPL(usbnet_tx_timeout); 1267 1268 /*-------------------------------------------------------------------------*/ 1269 1270 static int build_dma_sg(const struct sk_buff *skb, struct urb *urb) 1271 { 1272 unsigned num_sgs, total_len = 0; 1273 int i, s = 0; 1274 1275 num_sgs = skb_shinfo(skb)->nr_frags + 1; 1276 if (num_sgs == 1) 1277 return 0; 1278 1279 /* reserve one for zero packet */ 1280 urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist), 1281 GFP_ATOMIC); 1282 if (!urb->sg) 1283 return -ENOMEM; 1284 1285 urb->num_sgs = num_sgs; 1286 sg_init_table(urb->sg, urb->num_sgs + 1); 1287 1288 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb)); 1289 total_len += skb_headlen(skb); 1290 1291 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1292 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i]; 1293 1294 total_len += skb_frag_size(f); 1295 sg_set_page(&urb->sg[i + s], f->page.p, f->size, 1296 f->page_offset); 1297 } 1298 urb->transfer_buffer_length = total_len; 1299 1300 return 1; 1301 } 1302 1303 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, 1304 struct net_device *net) 1305 { 1306 struct usbnet *dev = netdev_priv(net); 1307 unsigned int length; 1308 struct urb *urb = NULL; 1309 struct skb_data *entry; 1310 struct driver_info *info = dev->driver_info; 1311 unsigned long flags; 1312 int retval; 1313 1314 if (skb) 1315 skb_tx_timestamp(skb); 1316 1317 // some devices want funky USB-level framing, for 1318 // win32 driver (usually) and/or hardware quirks 1319 if (info->tx_fixup) { 1320 skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 1321 if (!skb) { 1322 /* packet collected; minidriver waiting for more */ 1323 if (info->flags & FLAG_MULTI_PACKET) 1324 goto not_drop; 1325 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); 1326 goto drop; 1327 } 1328 } 1329 1330 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 1331 netif_dbg(dev, tx_err, dev->net, "no urb\n"); 1332 goto drop; 1333 } 1334 1335 entry = (struct skb_data *) skb->cb; 1336 entry->urb = urb; 1337 entry->dev = dev; 1338 1339 usb_fill_bulk_urb (urb, dev->udev, dev->out, 1340 skb->data, skb->len, tx_complete, skb); 1341 if (dev->can_dma_sg) { 1342 if (build_dma_sg(skb, urb) < 0) 1343 goto drop; 1344 } 1345 length = urb->transfer_buffer_length; 1346 1347 /* don't assume the hardware handles USB_ZERO_PACKET 1348 * NOTE: strictly conforming cdc-ether devices should expect 1349 * the ZLP here, but ignore the one-byte packet. 1350 * NOTE2: CDC NCM specification is different from CDC ECM when 1351 * handling ZLP/short packets, so cdc_ncm driver will make short 1352 * packet itself if needed. 1353 */ 1354 if (length % dev->maxpacket == 0) { 1355 if (!(info->flags & FLAG_SEND_ZLP)) { 1356 if (!(info->flags & FLAG_MULTI_PACKET)) { 1357 length++; 1358 if (skb_tailroom(skb) && !urb->num_sgs) { 1359 skb->data[skb->len] = 0; 1360 __skb_put(skb, 1); 1361 } else if (urb->num_sgs) 1362 sg_set_buf(&urb->sg[urb->num_sgs++], 1363 dev->padding_pkt, 1); 1364 } 1365 } else 1366 urb->transfer_flags |= URB_ZERO_PACKET; 1367 } 1368 urb->transfer_buffer_length = length; 1369 1370 if (info->flags & FLAG_MULTI_PACKET) { 1371 /* Driver has set number of packets and a length delta. 1372 * Calculate the complete length and ensure that it's 1373 * positive. 1374 */ 1375 entry->length += length; 1376 if (WARN_ON_ONCE(entry->length <= 0)) 1377 entry->length = length; 1378 } else { 1379 usbnet_set_skb_tx_stats(skb, 1, length); 1380 } 1381 1382 spin_lock_irqsave(&dev->txq.lock, flags); 1383 retval = usb_autopm_get_interface_async(dev->intf); 1384 if (retval < 0) { 1385 spin_unlock_irqrestore(&dev->txq.lock, flags); 1386 goto drop; 1387 } 1388 1389 #ifdef CONFIG_PM 1390 /* if this triggers the device is still a sleep */ 1391 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { 1392 /* transmission will be done in resume */ 1393 usb_anchor_urb(urb, &dev->deferred); 1394 /* no use to process more packets */ 1395 netif_stop_queue(net); 1396 usb_put_urb(urb); 1397 spin_unlock_irqrestore(&dev->txq.lock, flags); 1398 netdev_dbg(dev->net, "Delaying transmission for resumption\n"); 1399 goto deferred; 1400 } 1401 #endif 1402 1403 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 1404 case -EPIPE: 1405 netif_stop_queue (net); 1406 usbnet_defer_kevent (dev, EVENT_TX_HALT); 1407 usb_autopm_put_interface_async(dev->intf); 1408 break; 1409 default: 1410 usb_autopm_put_interface_async(dev->intf); 1411 netif_dbg(dev, tx_err, dev->net, 1412 "tx: submit urb err %d\n", retval); 1413 break; 1414 case 0: 1415 net->trans_start = jiffies; 1416 __usbnet_queue_skb(&dev->txq, skb, tx_start); 1417 if (dev->txq.qlen >= TX_QLEN (dev)) 1418 netif_stop_queue (net); 1419 } 1420 spin_unlock_irqrestore (&dev->txq.lock, flags); 1421 1422 if (retval) { 1423 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); 1424 drop: 1425 dev->net->stats.tx_dropped++; 1426 not_drop: 1427 if (skb) 1428 dev_kfree_skb_any (skb); 1429 if (urb) { 1430 kfree(urb->sg); 1431 usb_free_urb(urb); 1432 } 1433 } else 1434 netif_dbg(dev, tx_queued, dev->net, 1435 "> tx, len %u, type 0x%x\n", length, skb->protocol); 1436 #ifdef CONFIG_PM 1437 deferred: 1438 #endif 1439 return NETDEV_TX_OK; 1440 } 1441 EXPORT_SYMBOL_GPL(usbnet_start_xmit); 1442 1443 static int rx_alloc_submit(struct usbnet *dev, gfp_t flags) 1444 { 1445 struct urb *urb; 1446 int i; 1447 int ret = 0; 1448 1449 /* don't refill the queue all at once */ 1450 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) { 1451 urb = usb_alloc_urb(0, flags); 1452 if (urb != NULL) { 1453 ret = rx_submit(dev, urb, flags); 1454 if (ret) 1455 goto err; 1456 } else { 1457 ret = -ENOMEM; 1458 goto err; 1459 } 1460 } 1461 err: 1462 return ret; 1463 } 1464 1465 /*-------------------------------------------------------------------------*/ 1466 1467 // tasklet (work deferred from completions, in_irq) or timer 1468 1469 static void usbnet_bh (unsigned long param) 1470 { 1471 struct usbnet *dev = (struct usbnet *) param; 1472 struct sk_buff *skb; 1473 struct skb_data *entry; 1474 1475 while ((skb = skb_dequeue (&dev->done))) { 1476 entry = (struct skb_data *) skb->cb; 1477 switch (entry->state) { 1478 case rx_done: 1479 entry->state = rx_cleanup; 1480 rx_process (dev, skb); 1481 continue; 1482 case tx_done: 1483 kfree(entry->urb->sg); 1484 case rx_cleanup: 1485 usb_free_urb (entry->urb); 1486 dev_kfree_skb (skb); 1487 continue; 1488 default: 1489 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); 1490 } 1491 } 1492 1493 /* restart RX again after disabling due to high error rate */ 1494 clear_bit(EVENT_RX_KILL, &dev->flags); 1495 1496 /* waiting for all pending urbs to complete? 1497 * only then can we forgo submitting anew 1498 */ 1499 if (waitqueue_active(&dev->wait)) { 1500 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0) 1501 wake_up_all(&dev->wait); 1502 1503 // or are we maybe short a few urbs? 1504 } else if (netif_running (dev->net) && 1505 netif_device_present (dev->net) && 1506 netif_carrier_ok(dev->net) && 1507 !timer_pending (&dev->delay) && 1508 !test_bit (EVENT_RX_HALT, &dev->flags)) { 1509 int temp = dev->rxq.qlen; 1510 1511 if (temp < RX_QLEN(dev)) { 1512 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK) 1513 return; 1514 if (temp != dev->rxq.qlen) 1515 netif_dbg(dev, link, dev->net, 1516 "rxqlen %d --> %d\n", 1517 temp, dev->rxq.qlen); 1518 if (dev->rxq.qlen < RX_QLEN(dev)) 1519 tasklet_schedule (&dev->bh); 1520 } 1521 if (dev->txq.qlen < TX_QLEN (dev)) 1522 netif_wake_queue (dev->net); 1523 } 1524 } 1525 1526 1527 /*------------------------------------------------------------------------- 1528 * 1529 * USB Device Driver support 1530 * 1531 *-------------------------------------------------------------------------*/ 1532 1533 // precondition: never called in_interrupt 1534 1535 void usbnet_disconnect (struct usb_interface *intf) 1536 { 1537 struct usbnet *dev; 1538 struct usb_device *xdev; 1539 struct net_device *net; 1540 1541 dev = usb_get_intfdata(intf); 1542 usb_set_intfdata(intf, NULL); 1543 if (!dev) 1544 return; 1545 1546 xdev = interface_to_usbdev (intf); 1547 1548 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", 1549 intf->dev.driver->name, 1550 xdev->bus->bus_name, xdev->devpath, 1551 dev->driver_info->description); 1552 1553 net = dev->net; 1554 unregister_netdev (net); 1555 1556 cancel_work_sync(&dev->kevent); 1557 1558 usb_scuttle_anchored_urbs(&dev->deferred); 1559 1560 if (dev->driver_info->unbind) 1561 dev->driver_info->unbind (dev, intf); 1562 1563 usb_kill_urb(dev->interrupt); 1564 usb_free_urb(dev->interrupt); 1565 kfree(dev->padding_pkt); 1566 1567 free_netdev(net); 1568 } 1569 EXPORT_SYMBOL_GPL(usbnet_disconnect); 1570 1571 static const struct net_device_ops usbnet_netdev_ops = { 1572 .ndo_open = usbnet_open, 1573 .ndo_stop = usbnet_stop, 1574 .ndo_start_xmit = usbnet_start_xmit, 1575 .ndo_tx_timeout = usbnet_tx_timeout, 1576 .ndo_set_rx_mode = usbnet_set_rx_mode, 1577 .ndo_change_mtu = usbnet_change_mtu, 1578 .ndo_set_mac_address = eth_mac_addr, 1579 .ndo_validate_addr = eth_validate_addr, 1580 }; 1581 1582 /*-------------------------------------------------------------------------*/ 1583 1584 // precondition: never called in_interrupt 1585 1586 static struct device_type wlan_type = { 1587 .name = "wlan", 1588 }; 1589 1590 static struct device_type wwan_type = { 1591 .name = "wwan", 1592 }; 1593 1594 int 1595 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 1596 { 1597 struct usbnet *dev; 1598 struct net_device *net; 1599 struct usb_host_interface *interface; 1600 struct driver_info *info; 1601 struct usb_device *xdev; 1602 int status; 1603 const char *name; 1604 struct usb_driver *driver = to_usb_driver(udev->dev.driver); 1605 1606 /* usbnet already took usb runtime pm, so have to enable the feature 1607 * for usb interface, otherwise usb_autopm_get_interface may return 1608 * failure if RUNTIME_PM is enabled. 1609 */ 1610 if (!driver->supports_autosuspend) { 1611 driver->supports_autosuspend = 1; 1612 pm_runtime_enable(&udev->dev); 1613 } 1614 1615 name = udev->dev.driver->name; 1616 info = (struct driver_info *) prod->driver_info; 1617 if (!info) { 1618 dev_dbg (&udev->dev, "blacklisted by %s\n", name); 1619 return -ENODEV; 1620 } 1621 xdev = interface_to_usbdev (udev); 1622 interface = udev->cur_altsetting; 1623 1624 status = -ENOMEM; 1625 1626 // set up our own records 1627 net = alloc_etherdev(sizeof(*dev)); 1628 if (!net) 1629 goto out; 1630 1631 /* netdev_printk() needs this so do it as early as possible */ 1632 SET_NETDEV_DEV(net, &udev->dev); 1633 1634 dev = netdev_priv(net); 1635 dev->udev = xdev; 1636 dev->intf = udev; 1637 dev->driver_info = info; 1638 dev->driver_name = name; 1639 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 1640 | NETIF_MSG_PROBE | NETIF_MSG_LINK); 1641 init_waitqueue_head(&dev->wait); 1642 skb_queue_head_init (&dev->rxq); 1643 skb_queue_head_init (&dev->txq); 1644 skb_queue_head_init (&dev->done); 1645 skb_queue_head_init(&dev->rxq_pause); 1646 dev->bh.func = usbnet_bh; 1647 dev->bh.data = (unsigned long) dev; 1648 INIT_WORK (&dev->kevent, usbnet_deferred_kevent); 1649 init_usb_anchor(&dev->deferred); 1650 dev->delay.function = usbnet_bh; 1651 dev->delay.data = (unsigned long) dev; 1652 init_timer (&dev->delay); 1653 mutex_init (&dev->phy_mutex); 1654 mutex_init(&dev->interrupt_mutex); 1655 dev->interrupt_count = 0; 1656 1657 dev->net = net; 1658 strcpy (net->name, "usb%d"); 1659 memcpy (net->dev_addr, node_id, sizeof node_id); 1660 1661 /* rx and tx sides can use different message sizes; 1662 * bind() should set rx_urb_size in that case. 1663 */ 1664 dev->hard_mtu = net->mtu + net->hard_header_len; 1665 1666 net->netdev_ops = &usbnet_netdev_ops; 1667 net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 1668 net->ethtool_ops = &usbnet_ethtool_ops; 1669 1670 // allow device-specific bind/init procedures 1671 // NOTE net->name still not usable ... 1672 if (info->bind) { 1673 status = info->bind (dev, udev); 1674 if (status < 0) 1675 goto out1; 1676 1677 // heuristic: "usb%d" for links we know are two-host, 1678 // else "eth%d" when there's reasonable doubt. userspace 1679 // can rename the link if it knows better. 1680 if ((dev->driver_info->flags & FLAG_ETHER) != 0 && 1681 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 || 1682 (net->dev_addr [0] & 0x02) == 0)) 1683 strcpy (net->name, "eth%d"); 1684 /* WLAN devices should always be named "wlan%d" */ 1685 if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1686 strcpy(net->name, "wlan%d"); 1687 /* WWAN devices should always be named "wwan%d" */ 1688 if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1689 strcpy(net->name, "wwan%d"); 1690 1691 /* devices that cannot do ARP */ 1692 if ((dev->driver_info->flags & FLAG_NOARP) != 0) 1693 net->flags |= IFF_NOARP; 1694 1695 /* maybe the remote can't receive an Ethernet MTU */ 1696 if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 1697 net->mtu = dev->hard_mtu - net->hard_header_len; 1698 } else if (!info->in || !info->out) 1699 status = usbnet_get_endpoints (dev, udev); 1700 else { 1701 dev->in = usb_rcvbulkpipe (xdev, info->in); 1702 dev->out = usb_sndbulkpipe (xdev, info->out); 1703 if (!(info->flags & FLAG_NO_SETINT)) 1704 status = usb_set_interface (xdev, 1705 interface->desc.bInterfaceNumber, 1706 interface->desc.bAlternateSetting); 1707 else 1708 status = 0; 1709 1710 } 1711 if (status >= 0 && dev->status) 1712 status = init_status (dev, udev); 1713 if (status < 0) 1714 goto out3; 1715 1716 if (!dev->rx_urb_size) 1717 dev->rx_urb_size = dev->hard_mtu; 1718 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); 1719 1720 /* let userspace know we have a random address */ 1721 if (ether_addr_equal(net->dev_addr, node_id)) 1722 net->addr_assign_type = NET_ADDR_RANDOM; 1723 1724 if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1725 SET_NETDEV_DEVTYPE(net, &wlan_type); 1726 if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1727 SET_NETDEV_DEVTYPE(net, &wwan_type); 1728 1729 /* initialize max rx_qlen and tx_qlen */ 1730 usbnet_update_max_qlen(dev); 1731 1732 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) && 1733 !(info->flags & FLAG_MULTI_PACKET)) { 1734 dev->padding_pkt = kzalloc(1, GFP_KERNEL); 1735 if (!dev->padding_pkt) { 1736 status = -ENOMEM; 1737 goto out4; 1738 } 1739 } 1740 1741 status = register_netdev (net); 1742 if (status) 1743 goto out5; 1744 netif_info(dev, probe, dev->net, 1745 "register '%s' at usb-%s-%s, %s, %pM\n", 1746 udev->dev.driver->name, 1747 xdev->bus->bus_name, xdev->devpath, 1748 dev->driver_info->description, 1749 net->dev_addr); 1750 1751 // ok, it's ready to go. 1752 usb_set_intfdata (udev, dev); 1753 1754 netif_device_attach (net); 1755 1756 if (dev->driver_info->flags & FLAG_LINK_INTR) 1757 usbnet_link_change(dev, 0, 0); 1758 1759 return 0; 1760 1761 out5: 1762 kfree(dev->padding_pkt); 1763 out4: 1764 usb_free_urb(dev->interrupt); 1765 out3: 1766 if (info->unbind) 1767 info->unbind (dev, udev); 1768 out1: 1769 free_netdev(net); 1770 out: 1771 return status; 1772 } 1773 EXPORT_SYMBOL_GPL(usbnet_probe); 1774 1775 /*-------------------------------------------------------------------------*/ 1776 1777 /* 1778 * suspend the whole driver as soon as the first interface is suspended 1779 * resume only when the last interface is resumed 1780 */ 1781 1782 int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 1783 { 1784 struct usbnet *dev = usb_get_intfdata(intf); 1785 1786 if (!dev->suspend_count++) { 1787 spin_lock_irq(&dev->txq.lock); 1788 /* don't autosuspend while transmitting */ 1789 if (dev->txq.qlen && PMSG_IS_AUTO(message)) { 1790 dev->suspend_count--; 1791 spin_unlock_irq(&dev->txq.lock); 1792 return -EBUSY; 1793 } else { 1794 set_bit(EVENT_DEV_ASLEEP, &dev->flags); 1795 spin_unlock_irq(&dev->txq.lock); 1796 } 1797 /* 1798 * accelerate emptying of the rx and queues, to avoid 1799 * having everything error out. 1800 */ 1801 netif_device_detach (dev->net); 1802 usbnet_terminate_urbs(dev); 1803 __usbnet_status_stop_force(dev); 1804 1805 /* 1806 * reattach so runtime management can use and 1807 * wake the device 1808 */ 1809 netif_device_attach (dev->net); 1810 } 1811 return 0; 1812 } 1813 EXPORT_SYMBOL_GPL(usbnet_suspend); 1814 1815 int usbnet_resume (struct usb_interface *intf) 1816 { 1817 struct usbnet *dev = usb_get_intfdata(intf); 1818 struct sk_buff *skb; 1819 struct urb *res; 1820 int retval; 1821 1822 if (!--dev->suspend_count) { 1823 /* resume interrupt URB if it was previously submitted */ 1824 __usbnet_status_start_force(dev, GFP_NOIO); 1825 1826 spin_lock_irq(&dev->txq.lock); 1827 while ((res = usb_get_from_anchor(&dev->deferred))) { 1828 1829 skb = (struct sk_buff *)res->context; 1830 retval = usb_submit_urb(res, GFP_ATOMIC); 1831 if (retval < 0) { 1832 dev_kfree_skb_any(skb); 1833 kfree(res->sg); 1834 usb_free_urb(res); 1835 usb_autopm_put_interface_async(dev->intf); 1836 } else { 1837 dev->net->trans_start = jiffies; 1838 __skb_queue_tail(&dev->txq, skb); 1839 } 1840 } 1841 1842 smp_mb(); 1843 clear_bit(EVENT_DEV_ASLEEP, &dev->flags); 1844 spin_unlock_irq(&dev->txq.lock); 1845 1846 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { 1847 /* handle remote wakeup ASAP 1848 * we cannot race against stop 1849 */ 1850 if (netif_device_present(dev->net) && 1851 !timer_pending(&dev->delay) && 1852 !test_bit(EVENT_RX_HALT, &dev->flags)) 1853 rx_alloc_submit(dev, GFP_NOIO); 1854 1855 if (!(dev->txq.qlen >= TX_QLEN(dev))) 1856 netif_tx_wake_all_queues(dev->net); 1857 tasklet_schedule (&dev->bh); 1858 } 1859 } 1860 1861 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) 1862 usb_autopm_get_interface_no_resume(intf); 1863 1864 return 0; 1865 } 1866 EXPORT_SYMBOL_GPL(usbnet_resume); 1867 1868 /* 1869 * Either a subdriver implements manage_power, then it is assumed to always 1870 * be ready to be suspended or it reports the readiness to be suspended 1871 * explicitly 1872 */ 1873 void usbnet_device_suggests_idle(struct usbnet *dev) 1874 { 1875 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) { 1876 dev->intf->needs_remote_wakeup = 1; 1877 usb_autopm_put_interface_async(dev->intf); 1878 } 1879 } 1880 EXPORT_SYMBOL(usbnet_device_suggests_idle); 1881 1882 /* 1883 * For devices that can do without special commands 1884 */ 1885 int usbnet_manage_power(struct usbnet *dev, int on) 1886 { 1887 dev->intf->needs_remote_wakeup = on; 1888 return 0; 1889 } 1890 EXPORT_SYMBOL(usbnet_manage_power); 1891 1892 void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset) 1893 { 1894 /* update link after link is reseted */ 1895 if (link && !need_reset) 1896 netif_carrier_on(dev->net); 1897 else 1898 netif_carrier_off(dev->net); 1899 1900 if (need_reset && link) 1901 usbnet_defer_kevent(dev, EVENT_LINK_RESET); 1902 else 1903 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE); 1904 } 1905 EXPORT_SYMBOL(usbnet_link_change); 1906 1907 /*-------------------------------------------------------------------------*/ 1908 static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 1909 u16 value, u16 index, void *data, u16 size) 1910 { 1911 void *buf = NULL; 1912 int err = -ENOMEM; 1913 1914 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x" 1915 " value=0x%04x index=0x%04x size=%d\n", 1916 cmd, reqtype, value, index, size); 1917 1918 if (data) { 1919 buf = kmalloc(size, GFP_KERNEL); 1920 if (!buf) 1921 goto out; 1922 } 1923 1924 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 1925 cmd, reqtype, value, index, buf, size, 1926 USB_CTRL_GET_TIMEOUT); 1927 if (err > 0 && err <= size) 1928 memcpy(data, buf, err); 1929 kfree(buf); 1930 out: 1931 return err; 1932 } 1933 1934 static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 1935 u16 value, u16 index, const void *data, 1936 u16 size) 1937 { 1938 void *buf = NULL; 1939 int err = -ENOMEM; 1940 1941 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1942 " value=0x%04x index=0x%04x size=%d\n", 1943 cmd, reqtype, value, index, size); 1944 1945 if (data) { 1946 buf = kmemdup(data, size, GFP_KERNEL); 1947 if (!buf) 1948 goto out; 1949 } 1950 1951 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 1952 cmd, reqtype, value, index, buf, size, 1953 USB_CTRL_SET_TIMEOUT); 1954 kfree(buf); 1955 1956 out: 1957 return err; 1958 } 1959 1960 int cdc_parse_cdc_header(struct usb_cdc_parsed_header *hdr, 1961 struct usb_interface *intf, 1962 u8 *buffer, 1963 int buflen) 1964 { 1965 /* duplicates are ignored */ 1966 struct usb_cdc_union_desc *union_header = NULL; 1967 1968 /* duplicates are not tolerated */ 1969 struct usb_cdc_header_desc *header = NULL; 1970 struct usb_cdc_ether_desc *ether = NULL; 1971 struct usb_cdc_mdlm_detail_desc *detail = NULL; 1972 struct usb_cdc_mdlm_desc *desc = NULL; 1973 1974 unsigned int elength; 1975 int cnt = 0; 1976 1977 memset(hdr, 0x00, sizeof(struct usb_cdc_parsed_header)); 1978 hdr->phonet_magic_present = false; 1979 while (buflen > 0) { 1980 elength = buffer[0]; 1981 if (!elength) { 1982 dev_err(&intf->dev, "skipping garbage byte\n"); 1983 elength = 1; 1984 goto next_desc; 1985 } 1986 if (buffer[1] != USB_DT_CS_INTERFACE) { 1987 dev_err(&intf->dev, "skipping garbage\n"); 1988 goto next_desc; 1989 } 1990 1991 switch (buffer[2]) { 1992 case USB_CDC_UNION_TYPE: /* we've found it */ 1993 if (elength < sizeof(struct usb_cdc_union_desc)) 1994 goto next_desc; 1995 if (union_header) { 1996 dev_err(&intf->dev, "More than one union descriptor, skipping ...\n"); 1997 goto next_desc; 1998 } 1999 union_header = (struct usb_cdc_union_desc *)buffer; 2000 break; 2001 case USB_CDC_COUNTRY_TYPE: 2002 if (elength < sizeof(struct usb_cdc_country_functional_desc)) 2003 goto next_desc; 2004 hdr->usb_cdc_country_functional_desc = 2005 (struct usb_cdc_country_functional_desc *)buffer; 2006 break; 2007 case USB_CDC_HEADER_TYPE: 2008 if (elength != sizeof(struct usb_cdc_header_desc)) 2009 goto next_desc; 2010 if (header) 2011 return -EINVAL; 2012 header = (struct usb_cdc_header_desc *)buffer; 2013 break; 2014 case USB_CDC_ACM_TYPE: 2015 if (elength < sizeof(struct usb_cdc_acm_descriptor)) 2016 goto next_desc; 2017 hdr->usb_cdc_acm_descriptor = 2018 (struct usb_cdc_acm_descriptor *)buffer; 2019 break; 2020 case USB_CDC_ETHERNET_TYPE: 2021 if (elength != sizeof(struct usb_cdc_ether_desc)) 2022 goto next_desc; 2023 if (ether) 2024 return -EINVAL; 2025 ether = (struct usb_cdc_ether_desc *)buffer; 2026 break; 2027 case USB_CDC_CALL_MANAGEMENT_TYPE: 2028 if (elength < sizeof(struct usb_cdc_call_mgmt_descriptor)) 2029 goto next_desc; 2030 hdr->usb_cdc_call_mgmt_descriptor = 2031 (struct usb_cdc_call_mgmt_descriptor *)buffer; 2032 break; 2033 case USB_CDC_DMM_TYPE: 2034 if (elength < sizeof(struct usb_cdc_dmm_desc)) 2035 goto next_desc; 2036 hdr->usb_cdc_dmm_desc = 2037 (struct usb_cdc_dmm_desc *)buffer; 2038 break; 2039 case USB_CDC_MDLM_TYPE: 2040 if (elength < sizeof(struct usb_cdc_mdlm_desc *)) 2041 goto next_desc; 2042 if (desc) 2043 return -EINVAL; 2044 desc = (struct usb_cdc_mdlm_desc *)buffer; 2045 break; 2046 case USB_CDC_MDLM_DETAIL_TYPE: 2047 if (elength < sizeof(struct usb_cdc_mdlm_detail_desc *)) 2048 goto next_desc; 2049 if (detail) 2050 return -EINVAL; 2051 detail = (struct usb_cdc_mdlm_detail_desc *)buffer; 2052 break; 2053 case USB_CDC_NCM_TYPE: 2054 if (elength < sizeof(struct usb_cdc_ncm_desc)) 2055 goto next_desc; 2056 hdr->usb_cdc_ncm_desc = (struct usb_cdc_ncm_desc *)buffer; 2057 break; 2058 case USB_CDC_MBIM_TYPE: 2059 if (elength < sizeof(struct usb_cdc_mbim_desc)) 2060 goto next_desc; 2061 2062 hdr->usb_cdc_mbim_desc = (struct usb_cdc_mbim_desc *)buffer; 2063 break; 2064 case USB_CDC_MBIM_EXTENDED_TYPE: 2065 if (elength < sizeof(struct usb_cdc_mbim_extended_desc)) 2066 break; 2067 hdr->usb_cdc_mbim_extended_desc = 2068 (struct usb_cdc_mbim_extended_desc *)buffer; 2069 break; 2070 case CDC_PHONET_MAGIC_NUMBER: 2071 hdr->phonet_magic_present = true; 2072 break; 2073 default: 2074 /* 2075 * there are LOTS more CDC descriptors that 2076 * could legitimately be found here. 2077 */ 2078 dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %ud\n", 2079 buffer[2], elength); 2080 goto next_desc; 2081 } 2082 cnt++; 2083 next_desc: 2084 buflen -= elength; 2085 buffer += elength; 2086 } 2087 hdr->usb_cdc_union_desc = union_header; 2088 hdr->usb_cdc_header_desc = header; 2089 hdr->usb_cdc_mdlm_detail_desc = detail; 2090 hdr->usb_cdc_mdlm_desc = desc; 2091 hdr->usb_cdc_ether_desc = ether; 2092 return cnt; 2093 } 2094 2095 EXPORT_SYMBOL(cdc_parse_cdc_header); 2096 2097 /* 2098 * The function can't be called inside suspend/resume callback, 2099 * otherwise deadlock will be caused. 2100 */ 2101 int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 2102 u16 value, u16 index, void *data, u16 size) 2103 { 2104 int ret; 2105 2106 if (usb_autopm_get_interface(dev->intf) < 0) 2107 return -ENODEV; 2108 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index, 2109 data, size); 2110 usb_autopm_put_interface(dev->intf); 2111 return ret; 2112 } 2113 EXPORT_SYMBOL_GPL(usbnet_read_cmd); 2114 2115 /* 2116 * The function can't be called inside suspend/resume callback, 2117 * otherwise deadlock will be caused. 2118 */ 2119 int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 2120 u16 value, u16 index, const void *data, u16 size) 2121 { 2122 int ret; 2123 2124 if (usb_autopm_get_interface(dev->intf) < 0) 2125 return -ENODEV; 2126 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index, 2127 data, size); 2128 usb_autopm_put_interface(dev->intf); 2129 return ret; 2130 } 2131 EXPORT_SYMBOL_GPL(usbnet_write_cmd); 2132 2133 /* 2134 * The function can be called inside suspend/resume callback safely 2135 * and should only be called by suspend/resume callback generally. 2136 */ 2137 int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 2138 u16 value, u16 index, void *data, u16 size) 2139 { 2140 return __usbnet_read_cmd(dev, cmd, reqtype, value, index, 2141 data, size); 2142 } 2143 EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm); 2144 2145 /* 2146 * The function can be called inside suspend/resume callback safely 2147 * and should only be called by suspend/resume callback generally. 2148 */ 2149 int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 2150 u16 value, u16 index, const void *data, 2151 u16 size) 2152 { 2153 return __usbnet_write_cmd(dev, cmd, reqtype, value, index, 2154 data, size); 2155 } 2156 EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm); 2157 2158 static void usbnet_async_cmd_cb(struct urb *urb) 2159 { 2160 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; 2161 int status = urb->status; 2162 2163 if (status < 0) 2164 dev_dbg(&urb->dev->dev, "%s failed with %d", 2165 __func__, status); 2166 2167 kfree(req); 2168 usb_free_urb(urb); 2169 } 2170 2171 /* 2172 * The caller must make sure that device can't be put into suspend 2173 * state until the control URB completes. 2174 */ 2175 int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype, 2176 u16 value, u16 index, const void *data, u16 size) 2177 { 2178 struct usb_ctrlrequest *req = NULL; 2179 struct urb *urb; 2180 int err = -ENOMEM; 2181 void *buf = NULL; 2182 2183 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 2184 " value=0x%04x index=0x%04x size=%d\n", 2185 cmd, reqtype, value, index, size); 2186 2187 urb = usb_alloc_urb(0, GFP_ATOMIC); 2188 if (!urb) { 2189 netdev_err(dev->net, "Error allocating URB in" 2190 " %s!\n", __func__); 2191 goto fail; 2192 } 2193 2194 if (data) { 2195 buf = kmemdup(data, size, GFP_ATOMIC); 2196 if (!buf) { 2197 netdev_err(dev->net, "Error allocating buffer" 2198 " in %s!\n", __func__); 2199 goto fail_free; 2200 } 2201 } 2202 2203 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); 2204 if (!req) 2205 goto fail_free_buf; 2206 2207 req->bRequestType = reqtype; 2208 req->bRequest = cmd; 2209 req->wValue = cpu_to_le16(value); 2210 req->wIndex = cpu_to_le16(index); 2211 req->wLength = cpu_to_le16(size); 2212 2213 usb_fill_control_urb(urb, dev->udev, 2214 usb_sndctrlpipe(dev->udev, 0), 2215 (void *)req, buf, size, 2216 usbnet_async_cmd_cb, req); 2217 urb->transfer_flags |= URB_FREE_BUFFER; 2218 2219 err = usb_submit_urb(urb, GFP_ATOMIC); 2220 if (err < 0) { 2221 netdev_err(dev->net, "Error submitting the control" 2222 " message: status=%d\n", err); 2223 goto fail_free; 2224 } 2225 return 0; 2226 2227 fail_free_buf: 2228 kfree(buf); 2229 fail_free: 2230 kfree(req); 2231 usb_free_urb(urb); 2232 fail: 2233 return err; 2234 2235 } 2236 EXPORT_SYMBOL_GPL(usbnet_write_cmd_async); 2237 /*-------------------------------------------------------------------------*/ 2238 2239 static int __init usbnet_init(void) 2240 { 2241 /* Compiler should optimize this out. */ 2242 BUILD_BUG_ON( 2243 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data)); 2244 2245 eth_random_addr(node_id); 2246 return 0; 2247 } 2248 module_init(usbnet_init); 2249 2250 static void __exit usbnet_exit(void) 2251 { 2252 } 2253 module_exit(usbnet_exit); 2254 2255 MODULE_AUTHOR("David Brownell"); 2256 MODULE_DESCRIPTION("USB network driver framework"); 2257 MODULE_LICENSE("GPL"); 2258