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