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