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