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 clear_bit(EVENT_RX_PAUSED, &dev->flags); 706 707 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { 708 usbnet_skb_return(dev, skb); 709 num++; 710 } 711 712 queue_work(system_bh_wq, &dev->bh_work); 713 714 netif_dbg(dev, rx_status, dev->net, 715 "paused rx queue disabled, %d skbs requeued\n", num); 716 } 717 EXPORT_SYMBOL_GPL(usbnet_resume_rx); 718 719 void usbnet_purge_paused_rxq(struct usbnet *dev) 720 { 721 skb_queue_purge(&dev->rxq_pause); 722 } 723 EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); 724 725 /*-------------------------------------------------------------------------*/ 726 727 // unlink pending rx/tx; completion handlers do all other cleanup 728 729 static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 730 { 731 unsigned long flags; 732 struct sk_buff *skb; 733 int count = 0; 734 735 spin_lock_irqsave (&q->lock, flags); 736 while (!skb_queue_empty(q)) { 737 struct skb_data *entry; 738 struct urb *urb; 739 int retval; 740 741 skb_queue_walk(q, skb) { 742 entry = (struct skb_data *) skb->cb; 743 if (entry->state != unlink_start) 744 goto found; 745 } 746 break; 747 found: 748 entry->state = unlink_start; 749 urb = entry->urb; 750 751 /* 752 * Get reference count of the URB to avoid it to be 753 * freed during usb_unlink_urb, which may trigger 754 * use-after-free problem inside usb_unlink_urb since 755 * usb_unlink_urb is always racing with .complete 756 * handler(include defer_bh). 757 */ 758 usb_get_urb(urb); 759 spin_unlock_irqrestore(&q->lock, flags); 760 // during some PM-driven resume scenarios, 761 // these (async) unlinks complete immediately 762 retval = usb_unlink_urb (urb); 763 if (retval != -EINPROGRESS && retval != 0) 764 netdev_dbg(dev->net, "unlink urb err, %d\n", retval); 765 else 766 count++; 767 usb_put_urb(urb); 768 spin_lock_irqsave(&q->lock, flags); 769 } 770 spin_unlock_irqrestore (&q->lock, flags); 771 return count; 772 } 773 774 // Flush all pending rx urbs 775 // minidrivers may need to do this when the MTU changes 776 777 void usbnet_unlink_rx_urbs(struct usbnet *dev) 778 { 779 if (netif_running(dev->net)) { 780 (void) unlink_urbs (dev, &dev->rxq); 781 queue_work(system_bh_wq, &dev->bh_work); 782 } 783 } 784 EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); 785 786 /*-------------------------------------------------------------------------*/ 787 788 static void wait_skb_queue_empty(struct sk_buff_head *q) 789 { 790 unsigned long flags; 791 792 spin_lock_irqsave(&q->lock, flags); 793 while (!skb_queue_empty(q)) { 794 spin_unlock_irqrestore(&q->lock, flags); 795 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); 796 set_current_state(TASK_UNINTERRUPTIBLE); 797 spin_lock_irqsave(&q->lock, flags); 798 } 799 spin_unlock_irqrestore(&q->lock, flags); 800 } 801 802 // precondition: never called in_interrupt 803 static void usbnet_terminate_urbs(struct usbnet *dev) 804 { 805 DECLARE_WAITQUEUE(wait, current); 806 int temp; 807 808 /* ensure there are no more active urbs */ 809 add_wait_queue(&dev->wait, &wait); 810 set_current_state(TASK_UNINTERRUPTIBLE); 811 temp = unlink_urbs(dev, &dev->txq) + 812 unlink_urbs(dev, &dev->rxq); 813 814 /* maybe wait for deletions to finish. */ 815 wait_skb_queue_empty(&dev->rxq); 816 wait_skb_queue_empty(&dev->txq); 817 wait_skb_queue_empty(&dev->done); 818 netif_dbg(dev, ifdown, dev->net, 819 "waited for %d urb completions\n", temp); 820 set_current_state(TASK_RUNNING); 821 remove_wait_queue(&dev->wait, &wait); 822 } 823 824 int usbnet_stop (struct net_device *net) 825 { 826 struct usbnet *dev = netdev_priv(net); 827 const struct driver_info *info = dev->driver_info; 828 int retval, pm, mpn; 829 830 clear_bit(EVENT_DEV_OPEN, &dev->flags); 831 netif_stop_queue (net); 832 833 netif_info(dev, ifdown, dev->net, 834 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", 835 net->stats.rx_packets, net->stats.tx_packets, 836 net->stats.rx_errors, net->stats.tx_errors); 837 838 /* to not race resume */ 839 pm = usb_autopm_get_interface(dev->intf); 840 /* allow minidriver to stop correctly (wireless devices to turn off 841 * radio etc) */ 842 if (info->stop) { 843 retval = info->stop(dev); 844 if (retval < 0) 845 netif_info(dev, ifdown, dev->net, 846 "stop fail (%d) usbnet usb-%s-%s, %s\n", 847 retval, 848 dev->udev->bus->bus_name, dev->udev->devpath, 849 info->description); 850 } 851 852 if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) 853 usbnet_terminate_urbs(dev); 854 855 usbnet_status_stop(dev); 856 857 usbnet_purge_paused_rxq(dev); 858 859 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 860 861 /* deferred work (timer, softirq, task) must also stop */ 862 dev->flags = 0; 863 timer_delete_sync(&dev->delay); 864 cancel_work_sync(&dev->bh_work); 865 cancel_work_sync(&dev->kevent); 866 867 /* We have cyclic dependencies. Those calls are needed 868 * to break a cycle. We cannot fall into the gaps because 869 * we have a flag 870 */ 871 cancel_work_sync(&dev->bh_work); 872 timer_delete_sync(&dev->delay); 873 cancel_work_sync(&dev->kevent); 874 875 if (!pm) 876 usb_autopm_put_interface(dev->intf); 877 878 if (info->manage_power && mpn) 879 info->manage_power(dev, 0); 880 else 881 usb_autopm_put_interface(dev->intf); 882 883 return 0; 884 } 885 EXPORT_SYMBOL_GPL(usbnet_stop); 886 887 /*-------------------------------------------------------------------------*/ 888 889 // posts reads, and enables write queuing 890 891 // precondition: never called in_interrupt 892 893 int usbnet_open (struct net_device *net) 894 { 895 struct usbnet *dev = netdev_priv(net); 896 int retval; 897 const struct driver_info *info = dev->driver_info; 898 899 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { 900 netif_info(dev, ifup, dev->net, 901 "resumption fail (%d) usbnet usb-%s-%s, %s\n", 902 retval, 903 dev->udev->bus->bus_name, 904 dev->udev->devpath, 905 info->description); 906 goto done_nopm; 907 } 908 909 // put into "known safe" state 910 if (info->reset && (retval = info->reset (dev)) < 0) { 911 netif_info(dev, ifup, dev->net, 912 "open reset fail (%d) usbnet usb-%s-%s, %s\n", 913 retval, 914 dev->udev->bus->bus_name, 915 dev->udev->devpath, 916 info->description); 917 goto done; 918 } 919 920 /* hard_mtu or rx_urb_size may change in reset() */ 921 usbnet_update_max_qlen(dev); 922 923 // insist peer be connected 924 if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 925 netif_err(dev, ifup, dev->net, "can't open; %d\n", retval); 926 goto done; 927 } 928 929 /* start any status interrupt transfer */ 930 if (dev->interrupt) { 931 retval = usbnet_status_start(dev, GFP_KERNEL); 932 if (retval < 0) { 933 netif_err(dev, ifup, dev->net, 934 "intr submit %d\n", retval); 935 goto done; 936 } 937 } 938 939 set_bit(EVENT_DEV_OPEN, &dev->flags); 940 netif_start_queue (net); 941 netif_info(dev, ifup, dev->net, 942 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", 943 (int)RX_QLEN(dev), (int)TX_QLEN(dev), 944 dev->net->mtu, 945 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : 946 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : 947 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : 948 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : 949 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : 950 "simple"); 951 952 /* reset rx error state */ 953 dev->pkt_cnt = 0; 954 dev->pkt_err = 0; 955 clear_bit(EVENT_RX_KILL, &dev->flags); 956 957 // delay posting reads until we're fully open 958 queue_work(system_bh_wq, &dev->bh_work); 959 if (info->manage_power) { 960 retval = info->manage_power(dev, 1); 961 if (retval < 0) { 962 retval = 0; 963 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 964 } else { 965 usb_autopm_put_interface(dev->intf); 966 } 967 } 968 return retval; 969 done: 970 usb_autopm_put_interface(dev->intf); 971 done_nopm: 972 return retval; 973 } 974 EXPORT_SYMBOL_GPL(usbnet_open); 975 976 /*-------------------------------------------------------------------------*/ 977 978 /* ethtool methods; minidrivers may need to add some more, but 979 * they'll probably want to use this base set. 980 */ 981 982 /* These methods are written on the assumption that the device 983 * uses MII 984 */ 985 int usbnet_get_link_ksettings_mii(struct net_device *net, 986 struct ethtool_link_ksettings *cmd) 987 { 988 struct usbnet *dev = netdev_priv(net); 989 990 if (!dev->mii.mdio_read) 991 return -EOPNOTSUPP; 992 993 mii_ethtool_get_link_ksettings(&dev->mii, cmd); 994 995 return 0; 996 } 997 EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings_mii); 998 999 int usbnet_get_link_ksettings_internal(struct net_device *net, 1000 struct ethtool_link_ksettings *cmd) 1001 { 1002 struct usbnet *dev = netdev_priv(net); 1003 1004 /* the assumption that speed is equal on tx and rx 1005 * is deeply engrained into the networking layer. 1006 * For wireless stuff it is not true. 1007 * We assume that rx_speed matters more. 1008 */ 1009 if (dev->rx_speed != SPEED_UNSET) 1010 cmd->base.speed = dev->rx_speed / 1000000; 1011 else if (dev->tx_speed != SPEED_UNSET) 1012 cmd->base.speed = dev->tx_speed / 1000000; 1013 else 1014 cmd->base.speed = SPEED_UNKNOWN; 1015 1016 /* The standard "Universal Serial Bus Class Definitions 1017 * for Communications Devices v1.2" does not specify 1018 * anything about duplex status. 1019 * So set it DUPLEX_UNKNOWN instead of default DUPLEX_HALF. 1020 */ 1021 cmd->base.duplex = DUPLEX_UNKNOWN; 1022 1023 return 0; 1024 } 1025 EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings_internal); 1026 1027 int usbnet_set_link_ksettings_mii(struct net_device *net, 1028 const struct ethtool_link_ksettings *cmd) 1029 { 1030 struct usbnet *dev = netdev_priv(net); 1031 int retval; 1032 1033 if (!dev->mii.mdio_write) 1034 return -EOPNOTSUPP; 1035 1036 retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd); 1037 1038 /* link speed/duplex might have changed */ 1039 if (dev->driver_info->link_reset) 1040 dev->driver_info->link_reset(dev); 1041 1042 /* hard_mtu or rx_urb_size may change in link_reset() */ 1043 usbnet_update_max_qlen(dev); 1044 1045 return retval; 1046 } 1047 EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings_mii); 1048 1049 u32 usbnet_get_link (struct net_device *net) 1050 { 1051 struct usbnet *dev = netdev_priv(net); 1052 1053 /* If a check_connect is defined, return its result */ 1054 if (dev->driver_info->check_connect) 1055 return dev->driver_info->check_connect (dev) == 0; 1056 1057 /* if the device has mii operations, use those */ 1058 if (dev->mii.mdio_read) 1059 return mii_link_ok(&dev->mii); 1060 1061 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ 1062 return ethtool_op_get_link(net); 1063 } 1064 EXPORT_SYMBOL_GPL(usbnet_get_link); 1065 1066 int usbnet_nway_reset(struct net_device *net) 1067 { 1068 struct usbnet *dev = netdev_priv(net); 1069 1070 if (!dev->mii.mdio_write) 1071 return -EOPNOTSUPP; 1072 1073 return mii_nway_restart(&dev->mii); 1074 } 1075 EXPORT_SYMBOL_GPL(usbnet_nway_reset); 1076 1077 void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 1078 { 1079 struct usbnet *dev = netdev_priv(net); 1080 1081 strscpy(info->driver, dev->driver_name, sizeof(info->driver)); 1082 strscpy(info->fw_version, dev->driver_info->description, 1083 sizeof(info->fw_version)); 1084 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 1085 } 1086 EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 1087 1088 u32 usbnet_get_msglevel (struct net_device *net) 1089 { 1090 struct usbnet *dev = netdev_priv(net); 1091 1092 return dev->msg_enable; 1093 } 1094 EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 1095 1096 void usbnet_set_msglevel (struct net_device *net, u32 level) 1097 { 1098 struct usbnet *dev = netdev_priv(net); 1099 1100 dev->msg_enable = level; 1101 } 1102 EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 1103 1104 /* drivers may override default ethtool_ops in their bind() routine */ 1105 static const struct ethtool_ops usbnet_ethtool_ops = { 1106 .get_link = usbnet_get_link, 1107 .nway_reset = usbnet_nway_reset, 1108 .get_drvinfo = usbnet_get_drvinfo, 1109 .get_msglevel = usbnet_get_msglevel, 1110 .set_msglevel = usbnet_set_msglevel, 1111 .get_ts_info = ethtool_op_get_ts_info, 1112 .get_link_ksettings = usbnet_get_link_ksettings_mii, 1113 .set_link_ksettings = usbnet_set_link_ksettings_mii, 1114 }; 1115 1116 /*-------------------------------------------------------------------------*/ 1117 1118 static void __handle_link_change(struct usbnet *dev) 1119 { 1120 if (!test_bit(EVENT_DEV_OPEN, &dev->flags)) 1121 return; 1122 1123 if (!netif_carrier_ok(dev->net)) { 1124 /* kill URBs for reading packets to save bus bandwidth */ 1125 unlink_urbs(dev, &dev->rxq); 1126 1127 /* 1128 * tx_timeout will unlink URBs for sending packets and 1129 * tx queue is stopped by netcore after link becomes off 1130 */ 1131 } else { 1132 /* submitting URBs for reading packets */ 1133 queue_work(system_bh_wq, &dev->bh_work); 1134 } 1135 1136 /* hard_mtu or rx_urb_size may change during link change */ 1137 usbnet_update_max_qlen(dev); 1138 1139 clear_bit(EVENT_LINK_CHANGE, &dev->flags); 1140 } 1141 1142 void usbnet_set_rx_mode(struct net_device *net) 1143 { 1144 struct usbnet *dev = netdev_priv(net); 1145 1146 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE); 1147 } 1148 EXPORT_SYMBOL_GPL(usbnet_set_rx_mode); 1149 1150 static void __handle_set_rx_mode(struct usbnet *dev) 1151 { 1152 if (dev->driver_info->set_rx_mode) 1153 (dev->driver_info->set_rx_mode)(dev); 1154 1155 clear_bit(EVENT_SET_RX_MODE, &dev->flags); 1156 } 1157 1158 /* work that cannot be done in interrupt context uses keventd. 1159 * 1160 * NOTE: with 2.5 we could do more of this using completion callbacks, 1161 * especially now that control transfers can be queued. 1162 */ 1163 static void 1164 usbnet_deferred_kevent (struct work_struct *work) 1165 { 1166 struct usbnet *dev = 1167 container_of(work, struct usbnet, kevent); 1168 int status; 1169 1170 /* usb_clear_halt() needs a thread context */ 1171 if (test_bit (EVENT_TX_HALT, &dev->flags)) { 1172 unlink_urbs (dev, &dev->txq); 1173 status = usb_autopm_get_interface(dev->intf); 1174 if (status < 0) 1175 goto fail_pipe; 1176 status = usb_clear_halt (dev->udev, dev->out); 1177 usb_autopm_put_interface(dev->intf); 1178 if (status < 0 && 1179 status != -EPIPE && 1180 status != -ESHUTDOWN) { 1181 if (netif_msg_tx_err (dev)) 1182 fail_pipe: 1183 netdev_err(dev->net, "can't clear tx halt, status %d\n", 1184 status); 1185 } else { 1186 clear_bit (EVENT_TX_HALT, &dev->flags); 1187 if (status != -ESHUTDOWN) 1188 netif_wake_queue (dev->net); 1189 } 1190 } 1191 if (test_bit (EVENT_RX_HALT, &dev->flags)) { 1192 unlink_urbs (dev, &dev->rxq); 1193 status = usb_autopm_get_interface(dev->intf); 1194 if (status < 0) 1195 goto fail_halt; 1196 status = usb_clear_halt (dev->udev, dev->in); 1197 usb_autopm_put_interface(dev->intf); 1198 if (status < 0 && 1199 status != -EPIPE && 1200 status != -ESHUTDOWN) { 1201 if (netif_msg_rx_err (dev)) 1202 fail_halt: 1203 netdev_err(dev->net, "can't clear rx halt, status %d\n", 1204 status); 1205 } else { 1206 clear_bit (EVENT_RX_HALT, &dev->flags); 1207 if (!usbnet_going_away(dev)) 1208 queue_work(system_bh_wq, &dev->bh_work); 1209 } 1210 } 1211 1212 /* work could resubmit itself forever if memory is tight */ 1213 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 1214 struct urb *urb = NULL; 1215 int resched = 1; 1216 1217 if (netif_running (dev->net)) 1218 urb = usb_alloc_urb (0, GFP_KERNEL); 1219 else 1220 clear_bit (EVENT_RX_MEMORY, &dev->flags); 1221 if (urb != NULL) { 1222 clear_bit (EVENT_RX_MEMORY, &dev->flags); 1223 status = usb_autopm_get_interface(dev->intf); 1224 if (status < 0) { 1225 usb_free_urb(urb); 1226 goto fail_lowmem; 1227 } 1228 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) 1229 resched = 0; 1230 usb_autopm_put_interface(dev->intf); 1231 fail_lowmem: 1232 if (resched) 1233 if (!usbnet_going_away(dev)) 1234 queue_work(system_bh_wq, &dev->bh_work); 1235 } 1236 } 1237 1238 if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 1239 const struct driver_info *info = dev->driver_info; 1240 int retval = 0; 1241 1242 clear_bit (EVENT_LINK_RESET, &dev->flags); 1243 status = usb_autopm_get_interface(dev->intf); 1244 if (status < 0) 1245 goto skip_reset; 1246 if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 1247 usb_autopm_put_interface(dev->intf); 1248 skip_reset: 1249 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", 1250 retval, 1251 dev->udev->bus->bus_name, 1252 dev->udev->devpath, 1253 info->description); 1254 } else { 1255 usb_autopm_put_interface(dev->intf); 1256 } 1257 1258 /* handle link change from link resetting */ 1259 __handle_link_change(dev); 1260 } 1261 1262 if (test_bit (EVENT_LINK_CHANGE, &dev->flags)) 1263 __handle_link_change(dev); 1264 1265 if (test_bit (EVENT_SET_RX_MODE, &dev->flags)) 1266 __handle_set_rx_mode(dev); 1267 1268 1269 if (dev->flags) 1270 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); 1271 } 1272 1273 /*-------------------------------------------------------------------------*/ 1274 1275 static void tx_complete (struct urb *urb) 1276 { 1277 struct sk_buff *skb = (struct sk_buff *) urb->context; 1278 struct skb_data *entry = (struct skb_data *) skb->cb; 1279 struct usbnet *dev = entry->dev; 1280 1281 if (urb->status == 0) { 1282 struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats); 1283 unsigned long flags; 1284 1285 flags = u64_stats_update_begin_irqsave(&stats64->syncp); 1286 u64_stats_add(&stats64->tx_packets, entry->packets); 1287 u64_stats_add(&stats64->tx_bytes, entry->length); 1288 u64_stats_update_end_irqrestore(&stats64->syncp, flags); 1289 } else { 1290 dev->net->stats.tx_errors++; 1291 1292 switch (urb->status) { 1293 case -EPIPE: 1294 usbnet_defer_kevent (dev, EVENT_TX_HALT); 1295 break; 1296 1297 /* software-driven interface shutdown */ 1298 case -ECONNRESET: // async unlink 1299 case -ESHUTDOWN: // hardware gone 1300 break; 1301 1302 /* like rx, tx gets controller i/o faults during hub_wq 1303 * delays and so it uses the same throttling mechanism. 1304 */ 1305 case -EPROTO: 1306 case -ETIME: 1307 case -EILSEQ: 1308 usb_mark_last_busy(dev->udev); 1309 if (!timer_pending (&dev->delay)) { 1310 mod_timer (&dev->delay, 1311 jiffies + THROTTLE_JIFFIES); 1312 netif_dbg(dev, link, dev->net, 1313 "tx throttle %d\n", urb->status); 1314 } 1315 netif_stop_queue (dev->net); 1316 break; 1317 default: 1318 netif_dbg(dev, tx_err, dev->net, 1319 "tx err %d\n", entry->urb->status); 1320 break; 1321 } 1322 } 1323 1324 usb_autopm_put_interface_async(dev->intf); 1325 (void) defer_bh(dev, skb, &dev->txq, tx_done); 1326 } 1327 1328 /*-------------------------------------------------------------------------*/ 1329 1330 void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue) 1331 { 1332 struct usbnet *dev = netdev_priv(net); 1333 1334 unlink_urbs (dev, &dev->txq); 1335 queue_work(system_bh_wq, &dev->bh_work); 1336 /* this needs to be handled individually because the generic layer 1337 * doesn't know what is sufficient and could not restore private 1338 * information if a remedy of an unconditional reset were used. 1339 */ 1340 if (dev->driver_info->recover) 1341 (dev->driver_info->recover)(dev); 1342 } 1343 EXPORT_SYMBOL_GPL(usbnet_tx_timeout); 1344 1345 /*-------------------------------------------------------------------------*/ 1346 1347 static int build_dma_sg(const struct sk_buff *skb, struct urb *urb) 1348 { 1349 unsigned num_sgs, total_len = 0; 1350 int i, s = 0; 1351 1352 num_sgs = skb_shinfo(skb)->nr_frags + 1; 1353 if (num_sgs == 1) 1354 return 0; 1355 1356 /* reserve one for zero packet */ 1357 urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist), 1358 GFP_ATOMIC); 1359 if (!urb->sg) 1360 return -ENOMEM; 1361 1362 urb->num_sgs = num_sgs; 1363 sg_init_table(urb->sg, urb->num_sgs + 1); 1364 1365 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb)); 1366 total_len += skb_headlen(skb); 1367 1368 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1369 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 1370 1371 total_len += skb_frag_size(f); 1372 sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f), 1373 skb_frag_off(f)); 1374 } 1375 urb->transfer_buffer_length = total_len; 1376 1377 return 1; 1378 } 1379 1380 netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, 1381 struct net_device *net) 1382 { 1383 struct usbnet *dev = netdev_priv(net); 1384 unsigned int length; 1385 struct urb *urb = NULL; 1386 struct skb_data *entry; 1387 const struct driver_info *info = dev->driver_info; 1388 unsigned long flags; 1389 int retval; 1390 1391 if (skb) 1392 skb_tx_timestamp(skb); 1393 1394 // some devices want funky USB-level framing, for 1395 // win32 driver (usually) and/or hardware quirks 1396 if (info->tx_fixup) { 1397 skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 1398 if (!skb) { 1399 /* packet collected; minidriver waiting for more */ 1400 if (info->flags & FLAG_MULTI_PACKET) 1401 goto not_drop; 1402 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); 1403 goto drop; 1404 } 1405 } 1406 1407 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 1408 netif_dbg(dev, tx_err, dev->net, "no urb\n"); 1409 goto drop; 1410 } 1411 1412 entry = (struct skb_data *) skb->cb; 1413 entry->urb = urb; 1414 entry->dev = dev; 1415 1416 usb_fill_bulk_urb (urb, dev->udev, dev->out, 1417 skb->data, skb->len, tx_complete, skb); 1418 if (dev->can_dma_sg) { 1419 if (build_dma_sg(skb, urb) < 0) 1420 goto drop; 1421 } 1422 length = urb->transfer_buffer_length; 1423 1424 /* don't assume the hardware handles USB_ZERO_PACKET 1425 * NOTE: strictly conforming cdc-ether devices should expect 1426 * the ZLP here, but ignore the one-byte packet. 1427 * NOTE2: CDC NCM specification is different from CDC ECM when 1428 * handling ZLP/short packets, so cdc_ncm driver will make short 1429 * packet itself if needed. 1430 */ 1431 if (length % dev->maxpacket == 0) { 1432 if (!(info->flags & FLAG_SEND_ZLP)) { 1433 if (!(info->flags & FLAG_MULTI_PACKET)) { 1434 length++; 1435 if (skb_tailroom(skb) && !urb->num_sgs) { 1436 skb->data[skb->len] = 0; 1437 __skb_put(skb, 1); 1438 } else if (urb->num_sgs) 1439 sg_set_buf(&urb->sg[urb->num_sgs++], 1440 dev->padding_pkt, 1); 1441 } 1442 } else 1443 urb->transfer_flags |= URB_ZERO_PACKET; 1444 } 1445 urb->transfer_buffer_length = length; 1446 1447 if (info->flags & FLAG_MULTI_PACKET) { 1448 /* Driver has set number of packets and a length delta. 1449 * Calculate the complete length and ensure that it's 1450 * positive. 1451 */ 1452 entry->length += length; 1453 if (WARN_ON_ONCE(entry->length <= 0)) 1454 entry->length = length; 1455 } else { 1456 usbnet_set_skb_tx_stats(skb, 1, length); 1457 } 1458 1459 spin_lock_irqsave(&dev->txq.lock, flags); 1460 retval = usb_autopm_get_interface_async(dev->intf); 1461 if (retval < 0) { 1462 spin_unlock_irqrestore(&dev->txq.lock, flags); 1463 goto drop; 1464 } 1465 if (netif_queue_stopped(net)) { 1466 usb_autopm_put_interface_async(dev->intf); 1467 spin_unlock_irqrestore(&dev->txq.lock, flags); 1468 goto drop; 1469 } 1470 1471 #ifdef CONFIG_PM 1472 /* if this triggers the device is still a sleep */ 1473 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { 1474 /* transmission will be done in resume */ 1475 usb_anchor_urb(urb, &dev->deferred); 1476 /* no use to process more packets */ 1477 netif_stop_queue(net); 1478 usb_put_urb(urb); 1479 spin_unlock_irqrestore(&dev->txq.lock, flags); 1480 netdev_dbg(dev->net, "Delaying transmission for resumption\n"); 1481 goto deferred; 1482 } 1483 #endif 1484 1485 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 1486 case -EPIPE: 1487 netif_stop_queue (net); 1488 usbnet_defer_kevent (dev, EVENT_TX_HALT); 1489 usb_autopm_put_interface_async(dev->intf); 1490 break; 1491 default: 1492 usb_autopm_put_interface_async(dev->intf); 1493 netif_dbg(dev, tx_err, dev->net, 1494 "tx: submit urb err %d\n", retval); 1495 break; 1496 case 0: 1497 netif_trans_update(net); 1498 __usbnet_queue_skb(&dev->txq, skb, tx_start); 1499 if (dev->txq.qlen >= TX_QLEN (dev)) 1500 netif_stop_queue (net); 1501 } 1502 spin_unlock_irqrestore (&dev->txq.lock, flags); 1503 1504 if (retval) { 1505 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); 1506 drop: 1507 dev->net->stats.tx_dropped++; 1508 not_drop: 1509 if (skb) 1510 dev_kfree_skb_any (skb); 1511 if (urb) { 1512 kfree(urb->sg); 1513 usb_free_urb(urb); 1514 } 1515 } else 1516 netif_dbg(dev, tx_queued, dev->net, 1517 "> tx, len %u, type 0x%x\n", length, skb->protocol); 1518 #ifdef CONFIG_PM 1519 deferred: 1520 #endif 1521 return NETDEV_TX_OK; 1522 } 1523 EXPORT_SYMBOL_GPL(usbnet_start_xmit); 1524 1525 static int rx_alloc_submit(struct usbnet *dev, gfp_t flags) 1526 { 1527 struct urb *urb; 1528 int i; 1529 int ret = 0; 1530 1531 /* don't refill the queue all at once */ 1532 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) { 1533 urb = usb_alloc_urb(0, flags); 1534 if (urb != NULL) { 1535 ret = rx_submit(dev, urb, flags); 1536 if (ret) 1537 goto err; 1538 } else { 1539 ret = -ENOMEM; 1540 goto err; 1541 } 1542 } 1543 err: 1544 return ret; 1545 } 1546 1547 static inline void usb_free_skb(struct sk_buff *skb) 1548 { 1549 struct skb_data *entry = (struct skb_data *)skb->cb; 1550 1551 usb_free_urb(entry->urb); 1552 dev_kfree_skb(skb); 1553 } 1554 1555 /*-------------------------------------------------------------------------*/ 1556 1557 // work (work deferred from completions, in_irq) or timer 1558 1559 static void usbnet_bh (struct timer_list *t) 1560 { 1561 struct usbnet *dev = timer_container_of(dev, t, delay); 1562 struct sk_buff *skb; 1563 struct skb_data *entry; 1564 1565 while ((skb = skb_dequeue (&dev->done))) { 1566 entry = (struct skb_data *) skb->cb; 1567 switch (entry->state) { 1568 case rx_done: 1569 if (rx_process(dev, skb)) 1570 usb_free_skb(skb); 1571 continue; 1572 case tx_done: 1573 kfree(entry->urb->sg); 1574 fallthrough; 1575 case rx_cleanup: 1576 usb_free_skb(skb); 1577 continue; 1578 default: 1579 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); 1580 } 1581 } 1582 1583 /* restart RX again after disabling due to high error rate */ 1584 clear_bit(EVENT_RX_KILL, &dev->flags); 1585 1586 /* waiting for all pending urbs to complete? 1587 * only then can we forgo submitting anew 1588 */ 1589 if (waitqueue_active(&dev->wait)) { 1590 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0) 1591 wake_up_all(&dev->wait); 1592 1593 // or are we maybe short a few urbs? 1594 } else if (netif_running (dev->net) && 1595 netif_device_present (dev->net) && 1596 netif_carrier_ok(dev->net) && 1597 !usbnet_going_away(dev) && 1598 !timer_pending(&dev->delay) && 1599 !test_bit(EVENT_RX_PAUSED, &dev->flags) && 1600 !test_bit(EVENT_RX_HALT, &dev->flags)) { 1601 int temp = dev->rxq.qlen; 1602 1603 if (temp < RX_QLEN(dev)) { 1604 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK) 1605 return; 1606 if (temp != dev->rxq.qlen) 1607 netif_dbg(dev, link, dev->net, 1608 "rxqlen %d --> %d\n", 1609 temp, dev->rxq.qlen); 1610 if (dev->rxq.qlen < RX_QLEN(dev)) 1611 queue_work(system_bh_wq, &dev->bh_work); 1612 } 1613 if (dev->txq.qlen < TX_QLEN (dev)) 1614 netif_wake_queue (dev->net); 1615 } 1616 } 1617 1618 static void usbnet_bh_work(struct work_struct *work) 1619 { 1620 struct usbnet *dev = from_work(dev, work, bh_work); 1621 1622 usbnet_bh(&dev->delay); 1623 } 1624 1625 1626 /*------------------------------------------------------------------------- 1627 * 1628 * USB Device Driver support 1629 * 1630 *-------------------------------------------------------------------------*/ 1631 1632 // precondition: never called in_interrupt 1633 1634 void usbnet_disconnect (struct usb_interface *intf) 1635 { 1636 struct usbnet *dev; 1637 struct usb_device *xdev; 1638 struct net_device *net; 1639 struct urb *urb; 1640 1641 dev = usb_get_intfdata(intf); 1642 usb_set_intfdata(intf, NULL); 1643 if (!dev) 1644 return; 1645 usbnet_mark_going_away(dev); 1646 1647 xdev = interface_to_usbdev (intf); 1648 1649 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", 1650 intf->dev.driver->name, 1651 xdev->bus->bus_name, xdev->devpath, 1652 dev->driver_info->description); 1653 1654 net = dev->net; 1655 unregister_netdev (net); 1656 1657 while ((urb = usb_get_from_anchor(&dev->deferred))) { 1658 dev_kfree_skb(urb->context); 1659 kfree(urb->sg); 1660 usb_free_urb(urb); 1661 } 1662 1663 if (dev->driver_info->unbind) 1664 dev->driver_info->unbind(dev, intf); 1665 1666 usb_kill_urb(dev->interrupt); 1667 usb_free_urb(dev->interrupt); 1668 kfree(dev->padding_pkt); 1669 1670 free_netdev(net); 1671 } 1672 EXPORT_SYMBOL_GPL(usbnet_disconnect); 1673 1674 static const struct net_device_ops usbnet_netdev_ops = { 1675 .ndo_open = usbnet_open, 1676 .ndo_stop = usbnet_stop, 1677 .ndo_start_xmit = usbnet_start_xmit, 1678 .ndo_tx_timeout = usbnet_tx_timeout, 1679 .ndo_set_rx_mode = usbnet_set_rx_mode, 1680 .ndo_change_mtu = usbnet_change_mtu, 1681 .ndo_set_mac_address = eth_mac_addr, 1682 .ndo_validate_addr = eth_validate_addr, 1683 }; 1684 1685 /*-------------------------------------------------------------------------*/ 1686 1687 // precondition: never called in_interrupt 1688 1689 static const struct device_type wlan_type = { 1690 .name = "wlan", 1691 }; 1692 1693 static const struct device_type wwan_type = { 1694 .name = "wwan", 1695 }; 1696 1697 int 1698 usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 1699 { 1700 struct usbnet *dev; 1701 struct net_device *net; 1702 struct usb_host_interface *interface; 1703 const struct driver_info *info; 1704 struct usb_device *xdev; 1705 int status; 1706 const char *name; 1707 struct usb_driver *driver = to_usb_driver(udev->dev.driver); 1708 1709 /* usbnet already took usb runtime pm, so have to enable the feature 1710 * for usb interface, otherwise usb_autopm_get_interface may return 1711 * failure if RUNTIME_PM is enabled. 1712 */ 1713 if (!driver->supports_autosuspend) { 1714 driver->supports_autosuspend = 1; 1715 pm_runtime_enable(&udev->dev); 1716 } 1717 1718 name = udev->dev.driver->name; 1719 info = (const struct driver_info *) prod->driver_info; 1720 if (!info) { 1721 dev_dbg (&udev->dev, "blacklisted by %s\n", name); 1722 return -ENODEV; 1723 } 1724 xdev = interface_to_usbdev (udev); 1725 interface = udev->cur_altsetting; 1726 1727 status = -ENOMEM; 1728 1729 // set up our own records 1730 net = alloc_etherdev(sizeof(*dev)); 1731 if (!net) 1732 goto out; 1733 1734 /* netdev_printk() needs this so do it as early as possible */ 1735 SET_NETDEV_DEV(net, &udev->dev); 1736 1737 dev = netdev_priv(net); 1738 dev->udev = xdev; 1739 dev->intf = udev; 1740 dev->driver_info = info; 1741 dev->driver_name = name; 1742 dev->rx_speed = SPEED_UNSET; 1743 dev->tx_speed = SPEED_UNSET; 1744 1745 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 1746 | NETIF_MSG_PROBE | NETIF_MSG_LINK); 1747 init_waitqueue_head(&dev->wait); 1748 skb_queue_head_init (&dev->rxq); 1749 skb_queue_head_init (&dev->txq); 1750 skb_queue_head_init (&dev->done); 1751 skb_queue_head_init(&dev->rxq_pause); 1752 INIT_WORK(&dev->bh_work, usbnet_bh_work); 1753 INIT_WORK (&dev->kevent, usbnet_deferred_kevent); 1754 init_usb_anchor(&dev->deferred); 1755 timer_setup(&dev->delay, usbnet_bh, 0); 1756 mutex_init (&dev->phy_mutex); 1757 mutex_init(&dev->interrupt_mutex); 1758 dev->interrupt_count = 0; 1759 1760 dev->net = net; 1761 strscpy(net->name, "usb%d", sizeof(net->name)); 1762 1763 /* rx and tx sides can use different message sizes; 1764 * bind() should set rx_urb_size in that case. 1765 */ 1766 dev->hard_mtu = net->mtu + net->hard_header_len; 1767 net->min_mtu = 0; 1768 net->max_mtu = ETH_MAX_MTU; 1769 1770 net->netdev_ops = &usbnet_netdev_ops; 1771 net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 1772 net->ethtool_ops = &usbnet_ethtool_ops; 1773 net->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 1774 1775 // allow device-specific bind/init procedures 1776 // NOTE net->name still not usable ... 1777 if (info->bind) { 1778 status = info->bind (dev, udev); 1779 if (status < 0) 1780 goto out1; 1781 1782 /* heuristic: rename to "eth%d" if we are not sure this link 1783 * is two-host (these links keep "usb%d") 1784 */ 1785 if ((dev->driver_info->flags & FLAG_ETHER) != 0 && 1786 !usbnet_needs_usb_name_format(dev, net)) 1787 strscpy(net->name, "eth%d", sizeof(net->name)); 1788 /* WLAN devices should always be named "wlan%d" */ 1789 if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1790 strscpy(net->name, "wlan%d", sizeof(net->name)); 1791 /* WWAN devices should always be named "wwan%d" */ 1792 if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1793 strscpy(net->name, "wwan%d", sizeof(net->name)); 1794 1795 /* devices that cannot do ARP */ 1796 if ((dev->driver_info->flags & FLAG_NOARP) != 0) 1797 net->flags |= IFF_NOARP; 1798 1799 /* maybe the remote can't receive an Ethernet MTU */ 1800 if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 1801 net->mtu = dev->hard_mtu - net->hard_header_len; 1802 } else if (!info->in || !info->out) 1803 status = usbnet_get_endpoints (dev, udev); 1804 else { 1805 u8 ep_addrs[3] = { 1806 info->in + USB_DIR_IN, info->out + USB_DIR_OUT, 0 1807 }; 1808 1809 dev->in = usb_rcvbulkpipe (xdev, info->in); 1810 dev->out = usb_sndbulkpipe (xdev, info->out); 1811 if (!(info->flags & FLAG_NO_SETINT)) 1812 status = usb_set_interface (xdev, 1813 interface->desc.bInterfaceNumber, 1814 interface->desc.bAlternateSetting); 1815 else 1816 status = 0; 1817 1818 if (status == 0 && !usb_check_bulk_endpoints(udev, ep_addrs)) 1819 status = -EINVAL; 1820 } 1821 if (status >= 0 && dev->status) 1822 status = init_status (dev, udev); 1823 if (status < 0) 1824 goto out3; 1825 1826 if (!dev->rx_urb_size) 1827 dev->rx_urb_size = dev->hard_mtu; 1828 dev->maxpacket = usb_maxpacket(dev->udev, dev->out); 1829 if (dev->maxpacket == 0) { 1830 /* that is a broken device */ 1831 status = -ENODEV; 1832 goto out4; 1833 } 1834 1835 /* this flags the device for user space */ 1836 if (!is_valid_ether_addr(net->dev_addr)) 1837 eth_hw_addr_random(net); 1838 1839 if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1840 SET_NETDEV_DEVTYPE(net, &wlan_type); 1841 if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1842 SET_NETDEV_DEVTYPE(net, &wwan_type); 1843 1844 /* initialize max rx_qlen and tx_qlen */ 1845 usbnet_update_max_qlen(dev); 1846 1847 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) && 1848 !(info->flags & FLAG_MULTI_PACKET)) { 1849 dev->padding_pkt = kzalloc(1, GFP_KERNEL); 1850 if (!dev->padding_pkt) { 1851 status = -ENOMEM; 1852 goto out4; 1853 } 1854 } 1855 1856 status = register_netdev (net); 1857 if (status) 1858 goto out5; 1859 netif_info(dev, probe, dev->net, 1860 "register '%s' at usb-%s-%s, %s, %pM\n", 1861 udev->dev.driver->name, 1862 xdev->bus->bus_name, xdev->devpath, 1863 dev->driver_info->description, 1864 net->dev_addr); 1865 1866 // ok, it's ready to go. 1867 usb_set_intfdata (udev, dev); 1868 1869 netif_device_attach (net); 1870 1871 if (dev->driver_info->flags & FLAG_LINK_INTR) 1872 usbnet_link_change(dev, 0, 0); 1873 1874 return 0; 1875 1876 out5: 1877 kfree(dev->padding_pkt); 1878 out4: 1879 usb_free_urb(dev->interrupt); 1880 out3: 1881 if (info->unbind) 1882 info->unbind (dev, udev); 1883 out1: 1884 /* subdrivers must undo all they did in bind() if they 1885 * fail it, but we may fail later and a deferred kevent 1886 * may trigger an error resubmitting itself and, worse, 1887 * schedule a timer. So we kill it all just in case. 1888 */ 1889 usbnet_mark_going_away(dev); 1890 cancel_work_sync(&dev->kevent); 1891 timer_delete_sync(&dev->delay); 1892 free_netdev(net); 1893 out: 1894 return status; 1895 } 1896 EXPORT_SYMBOL_GPL(usbnet_probe); 1897 1898 /*-------------------------------------------------------------------------*/ 1899 1900 /* 1901 * suspend the whole driver as soon as the first interface is suspended 1902 * resume only when the last interface is resumed 1903 */ 1904 1905 int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 1906 { 1907 struct usbnet *dev = usb_get_intfdata(intf); 1908 1909 if (!dev->suspend_count++) { 1910 spin_lock_irq(&dev->txq.lock); 1911 /* don't autosuspend while transmitting */ 1912 if (dev->txq.qlen && PMSG_IS_AUTO(message)) { 1913 dev->suspend_count--; 1914 spin_unlock_irq(&dev->txq.lock); 1915 return -EBUSY; 1916 } else { 1917 set_bit(EVENT_DEV_ASLEEP, &dev->flags); 1918 spin_unlock_irq(&dev->txq.lock); 1919 } 1920 /* 1921 * accelerate emptying of the rx and queues, to avoid 1922 * having everything error out. 1923 */ 1924 netif_device_detach (dev->net); 1925 usbnet_terminate_urbs(dev); 1926 __usbnet_status_stop_force(dev); 1927 1928 /* 1929 * reattach so runtime management can use and 1930 * wake the device 1931 */ 1932 netif_device_attach (dev->net); 1933 } 1934 return 0; 1935 } 1936 EXPORT_SYMBOL_GPL(usbnet_suspend); 1937 1938 int usbnet_resume (struct usb_interface *intf) 1939 { 1940 struct usbnet *dev = usb_get_intfdata(intf); 1941 struct sk_buff *skb; 1942 struct urb *res; 1943 int retval; 1944 1945 if (!--dev->suspend_count) { 1946 /* resume interrupt URB if it was previously submitted */ 1947 __usbnet_status_start_force(dev, GFP_NOIO); 1948 1949 spin_lock_irq(&dev->txq.lock); 1950 while ((res = usb_get_from_anchor(&dev->deferred))) { 1951 1952 skb = (struct sk_buff *)res->context; 1953 retval = usb_submit_urb(res, GFP_ATOMIC); 1954 if (retval < 0) { 1955 dev_kfree_skb_any(skb); 1956 kfree(res->sg); 1957 usb_free_urb(res); 1958 usb_autopm_put_interface_async(dev->intf); 1959 } else { 1960 netif_trans_update(dev->net); 1961 __skb_queue_tail(&dev->txq, skb); 1962 } 1963 } 1964 1965 smp_mb(); 1966 clear_bit(EVENT_DEV_ASLEEP, &dev->flags); 1967 spin_unlock_irq(&dev->txq.lock); 1968 1969 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { 1970 /* handle remote wakeup ASAP 1971 * we cannot race against stop 1972 */ 1973 if (netif_device_present(dev->net) && 1974 !timer_pending(&dev->delay) && 1975 !test_bit(EVENT_RX_HALT, &dev->flags)) 1976 rx_alloc_submit(dev, GFP_NOIO); 1977 1978 if (!(dev->txq.qlen >= TX_QLEN(dev))) 1979 netif_tx_wake_all_queues(dev->net); 1980 queue_work(system_bh_wq, &dev->bh_work); 1981 } 1982 } 1983 1984 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) 1985 usb_autopm_get_interface_no_resume(intf); 1986 1987 return 0; 1988 } 1989 EXPORT_SYMBOL_GPL(usbnet_resume); 1990 1991 /* 1992 * Either a subdriver implements manage_power, then it is assumed to always 1993 * be ready to be suspended or it reports the readiness to be suspended 1994 * explicitly 1995 */ 1996 void usbnet_device_suggests_idle(struct usbnet *dev) 1997 { 1998 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) { 1999 dev->intf->needs_remote_wakeup = 1; 2000 usb_autopm_put_interface_async(dev->intf); 2001 } 2002 } 2003 EXPORT_SYMBOL(usbnet_device_suggests_idle); 2004 2005 /* 2006 * For devices that can do without special commands 2007 */ 2008 int usbnet_manage_power(struct usbnet *dev, int on) 2009 { 2010 dev->intf->needs_remote_wakeup = on; 2011 return 0; 2012 } 2013 EXPORT_SYMBOL(usbnet_manage_power); 2014 2015 void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset) 2016 { 2017 /* update link after link is reseted */ 2018 if (link && !need_reset) 2019 netif_carrier_on(dev->net); 2020 else 2021 netif_carrier_off(dev->net); 2022 2023 if (need_reset && link) 2024 usbnet_defer_kevent(dev, EVENT_LINK_RESET); 2025 else 2026 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE); 2027 } 2028 EXPORT_SYMBOL(usbnet_link_change); 2029 2030 /*-------------------------------------------------------------------------*/ 2031 static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 2032 u16 value, u16 index, void *data, u16 size) 2033 { 2034 void *buf = NULL; 2035 int err = -ENOMEM; 2036 2037 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x" 2038 " value=0x%04x index=0x%04x size=%d\n", 2039 cmd, reqtype, value, index, size); 2040 2041 if (size) { 2042 buf = kmalloc(size, GFP_NOIO); 2043 if (!buf) 2044 goto out; 2045 } 2046 2047 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 2048 cmd, reqtype, value, index, buf, size, 2049 USB_CTRL_GET_TIMEOUT); 2050 if (err > 0 && err <= size) { 2051 if (data) 2052 memcpy(data, buf, err); 2053 else 2054 netdev_dbg(dev->net, 2055 "Huh? Data requested but thrown away.\n"); 2056 } 2057 kfree(buf); 2058 out: 2059 return err; 2060 } 2061 2062 static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 2063 u16 value, u16 index, const void *data, 2064 u16 size) 2065 { 2066 void *buf = NULL; 2067 int err = -ENOMEM; 2068 2069 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 2070 " value=0x%04x index=0x%04x size=%d\n", 2071 cmd, reqtype, value, index, size); 2072 2073 if (data) { 2074 buf = kmemdup(data, size, GFP_NOIO); 2075 if (!buf) 2076 goto out; 2077 } else { 2078 if (size) { 2079 WARN_ON_ONCE(1); 2080 err = -EINVAL; 2081 goto out; 2082 } 2083 } 2084 2085 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 2086 cmd, reqtype, value, index, buf, size, 2087 USB_CTRL_SET_TIMEOUT); 2088 kfree(buf); 2089 2090 out: 2091 return err; 2092 } 2093 2094 /* 2095 * The function can't be called inside suspend/resume callback, 2096 * otherwise deadlock will be caused. 2097 */ 2098 int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 2099 u16 value, u16 index, void *data, u16 size) 2100 { 2101 int ret; 2102 2103 if (usb_autopm_get_interface(dev->intf) < 0) 2104 return -ENODEV; 2105 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index, 2106 data, size); 2107 usb_autopm_put_interface(dev->intf); 2108 return ret; 2109 } 2110 EXPORT_SYMBOL_GPL(usbnet_read_cmd); 2111 2112 /* 2113 * The function can't be called inside suspend/resume callback, 2114 * otherwise deadlock will be caused. 2115 */ 2116 int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 2117 u16 value, u16 index, const void *data, u16 size) 2118 { 2119 int ret; 2120 2121 if (usb_autopm_get_interface(dev->intf) < 0) 2122 return -ENODEV; 2123 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index, 2124 data, size); 2125 usb_autopm_put_interface(dev->intf); 2126 return ret; 2127 } 2128 EXPORT_SYMBOL_GPL(usbnet_write_cmd); 2129 2130 /* 2131 * The function can be called inside suspend/resume callback safely 2132 * and should only be called by suspend/resume callback generally. 2133 */ 2134 int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 2135 u16 value, u16 index, void *data, u16 size) 2136 { 2137 return __usbnet_read_cmd(dev, cmd, reqtype, value, index, 2138 data, size); 2139 } 2140 EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm); 2141 2142 /* 2143 * The function can be called inside suspend/resume callback safely 2144 * and should only be called by suspend/resume callback generally. 2145 */ 2146 int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 2147 u16 value, u16 index, const void *data, 2148 u16 size) 2149 { 2150 return __usbnet_write_cmd(dev, cmd, reqtype, value, index, 2151 data, size); 2152 } 2153 EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm); 2154 2155 static void usbnet_async_cmd_cb(struct urb *urb) 2156 { 2157 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; 2158 int status = urb->status; 2159 2160 if (status < 0) 2161 dev_dbg(&urb->dev->dev, "%s failed with %d", 2162 __func__, status); 2163 2164 kfree(req); 2165 usb_free_urb(urb); 2166 } 2167 2168 /* 2169 * The caller must make sure that device can't be put into suspend 2170 * state until the control URB completes. 2171 */ 2172 int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype, 2173 u16 value, u16 index, const void *data, u16 size) 2174 { 2175 struct usb_ctrlrequest *req; 2176 struct urb *urb; 2177 int err = -ENOMEM; 2178 void *buf = NULL; 2179 2180 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 2181 " value=0x%04x index=0x%04x size=%d\n", 2182 cmd, reqtype, value, index, size); 2183 2184 urb = usb_alloc_urb(0, GFP_ATOMIC); 2185 if (!urb) 2186 goto fail; 2187 2188 if (data) { 2189 buf = kmemdup(data, size, GFP_ATOMIC); 2190 if (!buf) { 2191 netdev_err(dev->net, "Error allocating buffer" 2192 " in %s!\n", __func__); 2193 goto fail_free_urb; 2194 } 2195 } 2196 2197 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); 2198 if (!req) 2199 goto fail_free_buf; 2200 2201 req->bRequestType = reqtype; 2202 req->bRequest = cmd; 2203 req->wValue = cpu_to_le16(value); 2204 req->wIndex = cpu_to_le16(index); 2205 req->wLength = cpu_to_le16(size); 2206 2207 usb_fill_control_urb(urb, dev->udev, 2208 usb_sndctrlpipe(dev->udev, 0), 2209 (void *)req, buf, size, 2210 usbnet_async_cmd_cb, req); 2211 urb->transfer_flags |= URB_FREE_BUFFER; 2212 2213 err = usb_submit_urb(urb, GFP_ATOMIC); 2214 if (err < 0) { 2215 netdev_err(dev->net, "Error submitting the control" 2216 " message: status=%d\n", err); 2217 goto fail_free_all; 2218 } 2219 return 0; 2220 2221 fail_free_all: 2222 kfree(req); 2223 fail_free_buf: 2224 kfree(buf); 2225 /* 2226 * avoid a double free 2227 * needed because the flag can be set only 2228 * after filling the URB 2229 */ 2230 urb->transfer_flags = 0; 2231 fail_free_urb: 2232 usb_free_urb(urb); 2233 fail: 2234 return err; 2235 2236 } 2237 EXPORT_SYMBOL_GPL(usbnet_write_cmd_async); 2238 /*-------------------------------------------------------------------------*/ 2239 2240 static int __init usbnet_init(void) 2241 { 2242 /* Compiler should optimize this out. */ 2243 BUILD_BUG_ON( 2244 sizeof_field(struct sk_buff, cb) < sizeof(struct skb_data)); 2245 2246 return 0; 2247 } 2248 module_init(usbnet_init); 2249 2250 static void __exit usbnet_exit(void) 2251 { 2252 } 2253 module_exit(usbnet_exit); 2254 2255 MODULE_AUTHOR("David Brownell"); 2256 MODULE_DESCRIPTION("USB network driver framework"); 2257 MODULE_LICENSE("GPL"); 2258