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