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