11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 25b2fc499SJeff Garzik /* 35b2fc499SJeff Garzik * USB Network driver infrastructure 45b2fc499SJeff Garzik * Copyright (C) 2000-2005 by David Brownell 55b2fc499SJeff Garzik * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> 65b2fc499SJeff Garzik */ 75b2fc499SJeff Garzik 85b2fc499SJeff Garzik /* 95b2fc499SJeff Garzik * This is a generic "USB networking" framework that works with several 105b2fc499SJeff Garzik * kinds of full and high speed networking devices: host-to-host cables, 115b2fc499SJeff Garzik * smart usb peripherals, and actual Ethernet adapters. 125b2fc499SJeff Garzik * 135b2fc499SJeff Garzik * These devices usually differ in terms of control protocols (if they 145b2fc499SJeff Garzik * even have one!) and sometimes they define new framing to wrap or batch 155b2fc499SJeff Garzik * Ethernet packets. Otherwise, they talk to USB pretty much the same, 165b2fc499SJeff Garzik * so interface (un)binding, endpoint I/O queues, fault handling, and other 175b2fc499SJeff Garzik * issues can usefully be addressed by this framework. 185b2fc499SJeff Garzik */ 195b2fc499SJeff Garzik 205b2fc499SJeff Garzik // #define DEBUG // error path messages, extra info 215b2fc499SJeff Garzik // #define VERBOSE // more; success messages 225b2fc499SJeff Garzik 235b2fc499SJeff Garzik #include <linux/module.h> 245b2fc499SJeff Garzik #include <linux/init.h> 255b2fc499SJeff Garzik #include <linux/netdevice.h> 265b2fc499SJeff Garzik #include <linux/etherdevice.h> 2703ad032bSPeter Holik #include <linux/ctype.h> 285b2fc499SJeff Garzik #include <linux/ethtool.h> 295b2fc499SJeff Garzik #include <linux/workqueue.h> 305b2fc499SJeff Garzik #include <linux/mii.h> 315b2fc499SJeff Garzik #include <linux/usb.h> 323692e94fSJussi Kivilinna #include <linux/usb/usbnet.h> 335a0e3ad6STejun Heo #include <linux/slab.h> 34fd1f170dSAndy Shevchenko #include <linux/kernel.h> 35b0786b43SMing Lei #include <linux/pm_runtime.h> 365b2fc499SJeff Garzik 375b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 385b2fc499SJeff Garzik 395b2fc499SJeff Garzik /* 405b2fc499SJeff Garzik * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. 415b2fc499SJeff Garzik * Several dozen bytes of IPv4 data can fit in two such transactions. 425b2fc499SJeff Garzik * One maximum size Ethernet packet takes twenty four of them. 435b2fc499SJeff Garzik * For high speed, each frame comfortably fits almost 36 max size 445b2fc499SJeff Garzik * Ethernet packets (so queues should be bigger). 455b2fc499SJeff Garzik * 46a88c32aeSMing Lei * The goal is to let the USB host controller be busy for 5msec or 47a88c32aeSMing Lei * more before an irq is required, under load. Jumbograms change 48a88c32aeSMing Lei * the equation. 495b2fc499SJeff Garzik */ 50a88c32aeSMing Lei #define MAX_QUEUE_MEMORY (60 * 1518) 51a88c32aeSMing Lei #define RX_QLEN(dev) ((dev)->rx_qlen) 52a88c32aeSMing Lei #define TX_QLEN(dev) ((dev)->tx_qlen) 535b2fc499SJeff Garzik 545b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks 555b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES (5*HZ) 565b2fc499SJeff Garzik 5737ebb549SPetr Mladek /* throttle rx/tx briefly after some faults, so hub_wq might disconnect() 5837ebb549SPetr Mladek * us (it polls at HZ/4 usually) before we report too many false errors. 5937ebb549SPetr Mladek */ 605b2fc499SJeff Garzik #define THROTTLE_JIFFIES (HZ/8) 615b2fc499SJeff Garzik 625b2fc499SJeff Garzik // between wakeups 635b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS 3 645b2fc499SJeff Garzik 655b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 665b2fc499SJeff Garzik 675b2fc499SJeff Garzik // randomly generated ethernet address 685b2fc499SJeff Garzik static u8 node_id [ETH_ALEN]; 695b2fc499SJeff Garzik 705b2fc499SJeff Garzik /* use ethtool to change the level for any given device */ 715b2fc499SJeff Garzik static int msg_level = -1; 725b2fc499SJeff Garzik module_param (msg_level, int, 0); 735b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level"); 745b2fc499SJeff Garzik 755b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 765b2fc499SJeff Garzik 7747889068SYajun Deng static const char * const usbnet_event_names[] = { 7847889068SYajun Deng [EVENT_TX_HALT] = "EVENT_TX_HALT", 7947889068SYajun Deng [EVENT_RX_HALT] = "EVENT_RX_HALT", 8047889068SYajun Deng [EVENT_RX_MEMORY] = "EVENT_RX_MEMORY", 8147889068SYajun Deng [EVENT_STS_SPLIT] = "EVENT_STS_SPLIT", 8247889068SYajun Deng [EVENT_LINK_RESET] = "EVENT_LINK_RESET", 8347889068SYajun Deng [EVENT_RX_PAUSED] = "EVENT_RX_PAUSED", 8447889068SYajun Deng [EVENT_DEV_ASLEEP] = "EVENT_DEV_ASLEEP", 8547889068SYajun Deng [EVENT_DEV_OPEN] = "EVENT_DEV_OPEN", 8647889068SYajun Deng [EVENT_DEVICE_REPORT_IDLE] = "EVENT_DEVICE_REPORT_IDLE", 8747889068SYajun Deng [EVENT_NO_RUNTIME_PM] = "EVENT_NO_RUNTIME_PM", 8847889068SYajun Deng [EVENT_RX_KILL] = "EVENT_RX_KILL", 8947889068SYajun Deng [EVENT_LINK_CHANGE] = "EVENT_LINK_CHANGE", 9047889068SYajun Deng [EVENT_SET_RX_MODE] = "EVENT_SET_RX_MODE", 9147889068SYajun Deng [EVENT_NO_IP_ALIGN] = "EVENT_NO_IP_ALIGN", 9247889068SYajun Deng }; 9347889068SYajun Deng 945b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */ 955b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) 965b2fc499SJeff Garzik { 975b2fc499SJeff Garzik int tmp; 985b2fc499SJeff Garzik struct usb_host_interface *alt = NULL; 995b2fc499SJeff Garzik struct usb_host_endpoint *in = NULL, *out = NULL; 1005b2fc499SJeff Garzik struct usb_host_endpoint *status = NULL; 1015b2fc499SJeff Garzik 1025b2fc499SJeff Garzik for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 1035b2fc499SJeff Garzik unsigned ep; 1045b2fc499SJeff Garzik 1055b2fc499SJeff Garzik in = out = status = NULL; 1065b2fc499SJeff Garzik alt = intf->altsetting + tmp; 1075b2fc499SJeff Garzik 1085b2fc499SJeff Garzik /* take the first altsetting with in-bulk + out-bulk; 1095b2fc499SJeff Garzik * remember any status endpoint, just in case; 11070f23fd6SJustin P. Mattock * ignore other endpoints and altsettings. 1115b2fc499SJeff Garzik */ 1125b2fc499SJeff Garzik for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 1135b2fc499SJeff Garzik struct usb_host_endpoint *e; 1145b2fc499SJeff Garzik int intr = 0; 1155b2fc499SJeff Garzik 1165b2fc499SJeff Garzik e = alt->endpoint + ep; 1178d3d7c20SBjørn Mork 1188d3d7c20SBjørn Mork /* ignore endpoints which cannot transfer data */ 1198d3d7c20SBjørn Mork if (!usb_endpoint_maxp(&e->desc)) 1208d3d7c20SBjørn Mork continue; 1218d3d7c20SBjørn Mork 1225b2fc499SJeff Garzik switch (e->desc.bmAttributes) { 1235b2fc499SJeff Garzik case USB_ENDPOINT_XFER_INT: 1245b2fc499SJeff Garzik if (!usb_endpoint_dir_in(&e->desc)) 1255b2fc499SJeff Garzik continue; 1265b2fc499SJeff Garzik intr = 1; 127df561f66SGustavo A. R. Silva fallthrough; 1285b2fc499SJeff Garzik case USB_ENDPOINT_XFER_BULK: 1295b2fc499SJeff Garzik break; 1305b2fc499SJeff Garzik default: 1315b2fc499SJeff Garzik continue; 1325b2fc499SJeff Garzik } 1335b2fc499SJeff Garzik if (usb_endpoint_dir_in(&e->desc)) { 1345b2fc499SJeff Garzik if (!intr && !in) 1355b2fc499SJeff Garzik in = e; 1365b2fc499SJeff Garzik else if (intr && !status) 1375b2fc499SJeff Garzik status = e; 1385b2fc499SJeff Garzik } else { 1395b2fc499SJeff Garzik if (!out) 1405b2fc499SJeff Garzik out = e; 1415b2fc499SJeff Garzik } 1425b2fc499SJeff Garzik } 1435b2fc499SJeff Garzik if (in && out) 1445b2fc499SJeff Garzik break; 1455b2fc499SJeff Garzik } 1465b2fc499SJeff Garzik if (!alt || !in || !out) 1475b2fc499SJeff Garzik return -EINVAL; 1485b2fc499SJeff Garzik 1498e95a202SJoe Perches if (alt->desc.bAlternateSetting != 0 || 1508e95a202SJoe Perches !(dev->driver_info->flags & FLAG_NO_SETINT)) { 1515b2fc499SJeff Garzik tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, 1525b2fc499SJeff Garzik alt->desc.bAlternateSetting); 1535b2fc499SJeff Garzik if (tmp < 0) 1545b2fc499SJeff Garzik return tmp; 1555b2fc499SJeff Garzik } 1565b2fc499SJeff Garzik 1575b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (dev->udev, 1585b2fc499SJeff Garzik in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1595b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (dev->udev, 1605b2fc499SJeff Garzik out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1615b2fc499SJeff Garzik dev->status = status; 1625b2fc499SJeff Garzik return 0; 1635b2fc499SJeff Garzik } 1645b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints); 1655b2fc499SJeff Garzik 16603ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress) 16703ad032bSPeter Holik { 168*2674e7eaSJakub Kicinski u8 addr[ETH_ALEN]; 16951487ae7SAndy Shevchenko int tmp = -1, ret; 17003ad032bSPeter Holik unsigned char buf [13]; 17103ad032bSPeter Holik 17251487ae7SAndy Shevchenko ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf); 17351487ae7SAndy Shevchenko if (ret == 12) 174*2674e7eaSJakub Kicinski tmp = hex2bin(addr, buf, 6); 17551487ae7SAndy Shevchenko if (tmp < 0) { 17603ad032bSPeter Holik dev_dbg(&dev->udev->dev, 17703ad032bSPeter Holik "bad MAC string %d fetch, %d\n", iMACAddress, tmp); 17851487ae7SAndy Shevchenko if (ret >= 0) 17951487ae7SAndy Shevchenko ret = -EINVAL; 18051487ae7SAndy Shevchenko return ret; 18103ad032bSPeter Holik } 182*2674e7eaSJakub Kicinski eth_hw_addr_set(dev->net, addr); 18303ad032bSPeter Holik return 0; 18403ad032bSPeter Holik } 18503ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); 18603ad032bSPeter Holik 18724ead299Stom.leiming@gmail.com static void intr_complete (struct urb *urb) 18824ead299Stom.leiming@gmail.com { 18924ead299Stom.leiming@gmail.com struct usbnet *dev = urb->context; 19024ead299Stom.leiming@gmail.com int status = urb->status; 19124ead299Stom.leiming@gmail.com 19224ead299Stom.leiming@gmail.com switch (status) { 19324ead299Stom.leiming@gmail.com /* success */ 19424ead299Stom.leiming@gmail.com case 0: 19524ead299Stom.leiming@gmail.com dev->driver_info->status(dev, urb); 19624ead299Stom.leiming@gmail.com break; 19724ead299Stom.leiming@gmail.com 19824ead299Stom.leiming@gmail.com /* software-driven interface shutdown */ 19924ead299Stom.leiming@gmail.com case -ENOENT: /* urb killed */ 20024ead299Stom.leiming@gmail.com case -ESHUTDOWN: /* hardware gone */ 20124ead299Stom.leiming@gmail.com netif_dbg(dev, ifdown, dev->net, 20224ead299Stom.leiming@gmail.com "intr shutdown, code %d\n", status); 20324ead299Stom.leiming@gmail.com return; 20424ead299Stom.leiming@gmail.com 20524ead299Stom.leiming@gmail.com /* NOTE: not throttling like RX/TX, since this endpoint 20624ead299Stom.leiming@gmail.com * already polls infrequently 20724ead299Stom.leiming@gmail.com */ 20824ead299Stom.leiming@gmail.com default: 20924ead299Stom.leiming@gmail.com netdev_dbg(dev->net, "intr status %d\n", status); 21024ead299Stom.leiming@gmail.com break; 21124ead299Stom.leiming@gmail.com } 21224ead299Stom.leiming@gmail.com 21324ead299Stom.leiming@gmail.com status = usb_submit_urb (urb, GFP_ATOMIC); 21424ead299Stom.leiming@gmail.com if (status != 0) 21524ead299Stom.leiming@gmail.com netif_err(dev, timer, dev->net, 21624ead299Stom.leiming@gmail.com "intr resubmit --> %d\n", status); 21724ead299Stom.leiming@gmail.com } 2185b2fc499SJeff Garzik 2195b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf) 2205b2fc499SJeff Garzik { 2215b2fc499SJeff Garzik char *buf = NULL; 2225b2fc499SJeff Garzik unsigned pipe = 0; 2235b2fc499SJeff Garzik unsigned maxp; 2245b2fc499SJeff Garzik unsigned period; 2255b2fc499SJeff Garzik 2265b2fc499SJeff Garzik if (!dev->driver_info->status) 2275b2fc499SJeff Garzik return 0; 2285b2fc499SJeff Garzik 2295b2fc499SJeff Garzik pipe = usb_rcvintpipe (dev->udev, 2305b2fc499SJeff Garzik dev->status->desc.bEndpointAddress 2315b2fc499SJeff Garzik & USB_ENDPOINT_NUMBER_MASK); 2325b2fc499SJeff Garzik maxp = usb_maxpacket (dev->udev, pipe, 0); 2335b2fc499SJeff Garzik 2345b2fc499SJeff Garzik /* avoid 1 msec chatter: min 8 msec poll rate */ 2355b2fc499SJeff Garzik period = max ((int) dev->status->desc.bInterval, 2365b2fc499SJeff Garzik (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 2375b2fc499SJeff Garzik 2385b2fc499SJeff Garzik buf = kmalloc (maxp, GFP_KERNEL); 2395b2fc499SJeff Garzik if (buf) { 2405b2fc499SJeff Garzik dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); 2415b2fc499SJeff Garzik if (!dev->interrupt) { 2425b2fc499SJeff Garzik kfree (buf); 2435b2fc499SJeff Garzik return -ENOMEM; 2445b2fc499SJeff Garzik } else { 2455b2fc499SJeff Garzik usb_fill_int_urb(dev->interrupt, dev->udev, pipe, 2465b2fc499SJeff Garzik buf, maxp, intr_complete, dev, period); 247720f3d7cStom.leiming@gmail.com dev->interrupt->transfer_flags |= URB_FREE_BUFFER; 2485b2fc499SJeff Garzik dev_dbg(&intf->dev, 2495b2fc499SJeff Garzik "status ep%din, %d bytes period %d\n", 2505b2fc499SJeff Garzik usb_pipeendpoint(pipe), maxp, period); 2515b2fc499SJeff Garzik } 2525b2fc499SJeff Garzik } 2535b2fc499SJeff Garzik return 0; 2545b2fc499SJeff Garzik } 2555b2fc499SJeff Garzik 2566eecdc5fSDan Williams /* Submit the interrupt URB if not previously submitted, increasing refcount */ 2576eecdc5fSDan Williams int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags) 2586eecdc5fSDan Williams { 2596eecdc5fSDan Williams int ret = 0; 2606eecdc5fSDan Williams 2616eecdc5fSDan Williams WARN_ON_ONCE(dev->interrupt == NULL); 2626eecdc5fSDan Williams if (dev->interrupt) { 2636eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2646eecdc5fSDan Williams 2656eecdc5fSDan Williams if (++dev->interrupt_count == 1) 2666eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags); 2676eecdc5fSDan Williams 2686eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n", 2696eecdc5fSDan Williams dev->interrupt_count); 2706eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2716eecdc5fSDan Williams } 2726eecdc5fSDan Williams return ret; 2736eecdc5fSDan Williams } 2746eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_start); 2756eecdc5fSDan Williams 2766eecdc5fSDan Williams /* For resume; submit interrupt URB if previously submitted */ 2776eecdc5fSDan Williams static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags) 2786eecdc5fSDan Williams { 2796eecdc5fSDan Williams int ret = 0; 2806eecdc5fSDan Williams 2816eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2826eecdc5fSDan Williams if (dev->interrupt_count) { 2836eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags); 2846eecdc5fSDan Williams dev_dbg(&dev->udev->dev, 2856eecdc5fSDan Williams "submitted interrupt URB for resume\n"); 2866eecdc5fSDan Williams } 2876eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2886eecdc5fSDan Williams return ret; 2896eecdc5fSDan Williams } 2906eecdc5fSDan Williams 2916eecdc5fSDan Williams /* Kill the interrupt URB if all submitters want it killed */ 2926eecdc5fSDan Williams void usbnet_status_stop(struct usbnet *dev) 2936eecdc5fSDan Williams { 2946eecdc5fSDan Williams if (dev->interrupt) { 2956eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2966eecdc5fSDan Williams WARN_ON(dev->interrupt_count == 0); 2976eecdc5fSDan Williams 2986eecdc5fSDan Williams if (dev->interrupt_count && --dev->interrupt_count == 0) 2996eecdc5fSDan Williams usb_kill_urb(dev->interrupt); 3006eecdc5fSDan Williams 3016eecdc5fSDan Williams dev_dbg(&dev->udev->dev, 3026eecdc5fSDan Williams "decremented interrupt URB count to %d\n", 3036eecdc5fSDan Williams dev->interrupt_count); 3046eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 3056eecdc5fSDan Williams } 3066eecdc5fSDan Williams } 3076eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_stop); 3086eecdc5fSDan Williams 3096eecdc5fSDan Williams /* For suspend; always kill interrupt URB */ 3106eecdc5fSDan Williams static void __usbnet_status_stop_force(struct usbnet *dev) 3116eecdc5fSDan Williams { 3126eecdc5fSDan Williams if (dev->interrupt) { 3136eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 3146eecdc5fSDan Williams usb_kill_urb(dev->interrupt); 3156eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n"); 3166eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 3176eecdc5fSDan Williams } 3186eecdc5fSDan Williams } 3196eecdc5fSDan Williams 3205b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting. 3215b2fc499SJeff Garzik * Some link protocols batch packets, so their rx_fixup paths 3225b2fc499SJeff Garzik * can return clones as well as just modify the original skb. 3235b2fc499SJeff Garzik */ 3245b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) 3255b2fc499SJeff Garzik { 326af0c351cSHeiner Kallweit struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats); 3272695578bSEric Dumazet unsigned long flags; 3285b2fc499SJeff Garzik int status; 3295b2fc499SJeff Garzik 3307834ddbcSJussi Kivilinna if (test_bit(EVENT_RX_PAUSED, &dev->flags)) { 3317834ddbcSJussi Kivilinna skb_queue_tail(&dev->rxq_pause, skb); 3327834ddbcSJussi Kivilinna return; 3337834ddbcSJussi Kivilinna } 3347834ddbcSJussi Kivilinna 33581e0ce79SBjørn Mork /* only update if unset to allow minidriver rx_fixup override */ 33681e0ce79SBjørn Mork if (skb->protocol == 0) 3375b2fc499SJeff Garzik skb->protocol = eth_type_trans (skb, dev->net); 33881e0ce79SBjørn Mork 3392695578bSEric Dumazet flags = u64_stats_update_begin_irqsave(&stats64->syncp); 340c8b5d129SGreg Ungerer stats64->rx_packets++; 341c8b5d129SGreg Ungerer stats64->rx_bytes += skb->len; 3422695578bSEric Dumazet u64_stats_update_end_irqrestore(&stats64->syncp, flags); 3435b2fc499SJeff Garzik 344a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n", 3455b2fc499SJeff Garzik skb->len + sizeof (struct ethhdr), skb->protocol); 3465b2fc499SJeff Garzik memset (skb->cb, 0, sizeof (struct skb_data)); 347f9b491ecSMichael Riesch 348f9b491ecSMichael Riesch if (skb_defer_rx_timestamp(skb)) 349f9b491ecSMichael Riesch return; 350f9b491ecSMichael Riesch 3515b2fc499SJeff Garzik status = netif_rx (skb); 352a475f603SJoe Perches if (status != NET_RX_SUCCESS) 353a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 354a475f603SJoe Perches "netif_rx status %d\n", status); 3555b2fc499SJeff Garzik } 3565b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return); 3575b2fc499SJeff Garzik 358a88c32aeSMing Lei /* must be called if hard_mtu or rx_urb_size changed */ 359a88c32aeSMing Lei void usbnet_update_max_qlen(struct usbnet *dev) 360a88c32aeSMing Lei { 361a88c32aeSMing Lei enum usb_device_speed speed = dev->udev->speed; 362a88c32aeSMing Lei 363280ceaedSOliver Neukum if (!dev->rx_urb_size || !dev->hard_mtu) 364280ceaedSOliver Neukum goto insanity; 365a88c32aeSMing Lei switch (speed) { 366a88c32aeSMing Lei case USB_SPEED_HIGH: 367a88c32aeSMing Lei dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size; 368a88c32aeSMing Lei dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu; 369a88c32aeSMing Lei break; 370452c447aSMing Lei case USB_SPEED_SUPER: 371ea079842SOliver Neukum case USB_SPEED_SUPER_PLUS: 372452c447aSMing Lei /* 373452c447aSMing Lei * Not take default 5ms qlen for super speed HC to 374452c447aSMing Lei * save memory, and iperf tests show 2.5ms qlen can 375452c447aSMing Lei * work well 376452c447aSMing Lei */ 377452c447aSMing Lei dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size; 378452c447aSMing Lei dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu; 379452c447aSMing Lei break; 380a88c32aeSMing Lei default: 381280ceaedSOliver Neukum insanity: 382a88c32aeSMing Lei dev->rx_qlen = dev->tx_qlen = 4; 383a88c32aeSMing Lei } 384a88c32aeSMing Lei } 385a88c32aeSMing Lei EXPORT_SYMBOL_GPL(usbnet_update_max_qlen); 386a88c32aeSMing Lei 3875b2fc499SJeff Garzik 3885b2fc499SJeff Garzik /*------------------------------------------------------------------------- 3895b2fc499SJeff Garzik * 3905b2fc499SJeff Garzik * Network Device Driver (peer link to "Host Device", from USB host) 3915b2fc499SJeff Garzik * 3925b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 3935b2fc499SJeff Garzik 394777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu) 3955b2fc499SJeff Garzik { 3965b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 3975b2fc499SJeff Garzik int ll_mtu = new_mtu + net->hard_header_len; 3985b2fc499SJeff Garzik int old_hard_mtu = dev->hard_mtu; 3995b2fc499SJeff Garzik int old_rx_urb_size = dev->rx_urb_size; 4005b2fc499SJeff Garzik 4015b2fc499SJeff Garzik // no second zero-length packet read wanted after mtu-sized packets 4025b2fc499SJeff Garzik if ((ll_mtu % dev->maxpacket) == 0) 4035b2fc499SJeff Garzik return -EDOM; 4045b2fc499SJeff Garzik net->mtu = new_mtu; 4055b2fc499SJeff Garzik 4065b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 4075b2fc499SJeff Garzik if (dev->rx_urb_size == old_hard_mtu) { 4085b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 40943daa96bSSoohoon Lee if (dev->rx_urb_size > old_rx_urb_size) { 41043daa96bSSoohoon Lee usbnet_pause_rx(dev); 4115b2fc499SJeff Garzik usbnet_unlink_rx_urbs(dev); 41243daa96bSSoohoon Lee usbnet_resume_rx(dev); 41343daa96bSSoohoon Lee } 4145b2fc499SJeff Garzik } 4155b2fc499SJeff Garzik 416a88c32aeSMing Lei /* max qlen depend on hard_mtu and rx_urb_size */ 417a88c32aeSMing Lei usbnet_update_max_qlen(dev); 418a88c32aeSMing Lei 4195b2fc499SJeff Garzik return 0; 4205b2fc499SJeff Garzik } 421777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu); 4225b2fc499SJeff Garzik 4235b6e9bcdSMing Lei /* The caller must hold list->lock */ 4245b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list, 4255b6e9bcdSMing Lei struct sk_buff *newsk, enum skb_state state) 4265b6e9bcdSMing Lei { 4275b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) newsk->cb; 4285b6e9bcdSMing Lei 4295b6e9bcdSMing Lei __skb_queue_tail(list, newsk); 4305b6e9bcdSMing Lei entry->state = state; 4315b6e9bcdSMing Lei } 4325b6e9bcdSMing Lei 4335b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4345b2fc499SJeff Garzik 4355b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from 4365b2fc499SJeff Garzik * completion callbacks. 2.5 should have fixed those bugs... 4375b2fc499SJeff Garzik */ 4385b2fc499SJeff Garzik 4395b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb, 4405b6e9bcdSMing Lei struct sk_buff_head *list, enum skb_state state) 4415b2fc499SJeff Garzik { 4425b2fc499SJeff Garzik unsigned long flags; 4435b6e9bcdSMing Lei enum skb_state old_state; 4445b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) skb->cb; 4455b2fc499SJeff Garzik 4465b2fc499SJeff Garzik spin_lock_irqsave(&list->lock, flags); 4475b6e9bcdSMing Lei old_state = entry->state; 4485b6e9bcdSMing Lei entry->state = state; 4495b2fc499SJeff Garzik __skb_unlink(skb, list); 450fcb0bb6aSEugene Shatokhin 451fcb0bb6aSEugene Shatokhin /* defer_bh() is never called with list == &dev->done. 452fcb0bb6aSEugene Shatokhin * spin_lock_nested() tells lockdep that it is OK to take 453fcb0bb6aSEugene Shatokhin * dev->done.lock here with list->lock held. 454fcb0bb6aSEugene Shatokhin */ 455fcb0bb6aSEugene Shatokhin spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING); 456fcb0bb6aSEugene Shatokhin 4575b2fc499SJeff Garzik __skb_queue_tail(&dev->done, skb); 4585b2fc499SJeff Garzik if (dev->done.qlen == 1) 4595b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 460fcb0bb6aSEugene Shatokhin spin_unlock(&dev->done.lock); 461fcb0bb6aSEugene Shatokhin spin_unlock_irqrestore(&list->lock, flags); 4625b6e9bcdSMing Lei return old_state; 4635b2fc499SJeff Garzik } 4645b2fc499SJeff Garzik 4655b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd 4665b2fc499SJeff Garzik * 4675b2fc499SJeff Garzik * NOTE: annoying asymmetry: if it's active, schedule_work() fails, 4685b2fc499SJeff Garzik * but tasklet_schedule() doesn't. hope the failure is rare. 4695b2fc499SJeff Garzik */ 4705b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work) 4715b2fc499SJeff Garzik { 4725b2fc499SJeff Garzik set_bit (work, &dev->flags); 473ab18a9c9SOliver Neukum if (!schedule_work (&dev->kevent)) 47447889068SYajun Deng netdev_dbg(dev->net, "kevent %s may have been dropped\n", usbnet_event_names[work]); 475ab18a9c9SOliver Neukum else 47647889068SYajun Deng netdev_dbg(dev->net, "kevent %s scheduled\n", usbnet_event_names[work]); 4775b2fc499SJeff Garzik } 4785b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent); 4795b2fc499SJeff Garzik 4805b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4815b2fc499SJeff Garzik 4825b2fc499SJeff Garzik static void rx_complete (struct urb *urb); 4835b2fc499SJeff Garzik 484dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) 4855b2fc499SJeff Garzik { 4865b2fc499SJeff Garzik struct sk_buff *skb; 4875b2fc499SJeff Garzik struct skb_data *entry; 4885b2fc499SJeff Garzik int retval = 0; 4895b2fc499SJeff Garzik unsigned long lockflags; 4905b2fc499SJeff Garzik size_t size = dev->rx_urb_size; 4915b2fc499SJeff Garzik 49270c37bf9SBjørn Mork /* prevent rx skb allocation when error ratio is high */ 49370c37bf9SBjørn Mork if (test_bit(EVENT_RX_KILL, &dev->flags)) { 49470c37bf9SBjørn Mork usb_free_urb(urb); 49570c37bf9SBjørn Mork return -ENOLINK; 49670c37bf9SBjørn Mork } 49770c37bf9SBjørn Mork 498a4abd7a8SBjørn Mork if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags)) 499a4abd7a8SBjørn Mork skb = __netdev_alloc_skb(dev->net, size, flags); 500a4abd7a8SBjørn Mork else 5017bdd4027SEric Dumazet skb = __netdev_alloc_skb_ip_align(dev->net, size, flags); 5027bdd4027SEric Dumazet if (!skb) { 503a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no rx skb\n"); 5045b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 5055b2fc499SJeff Garzik usb_free_urb (urb); 506dacb3975SDavid S. Miller return -ENOMEM; 5075b2fc499SJeff Garzik } 5085b2fc499SJeff Garzik 5095b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 5105b2fc499SJeff Garzik entry->urb = urb; 5115b2fc499SJeff Garzik entry->dev = dev; 5125b2fc499SJeff Garzik entry->length = 0; 5135b2fc499SJeff Garzik 5145b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->in, 5155b2fc499SJeff Garzik skb->data, size, rx_complete, skb); 5165b2fc499SJeff Garzik 5175b2fc499SJeff Garzik spin_lock_irqsave (&dev->rxq.lock, lockflags); 5185b2fc499SJeff Garzik 5198e95a202SJoe Perches if (netif_running (dev->net) && 5208e95a202SJoe Perches netif_device_present (dev->net) && 521ad70411aSKloetzke Jan test_bit(EVENT_DEV_OPEN, &dev->flags) && 52269ee472fSOliver Neukum !test_bit (EVENT_RX_HALT, &dev->flags) && 52369ee472fSOliver Neukum !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { 5245b2fc499SJeff Garzik switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { 5255b2fc499SJeff Garzik case -EPIPE: 5265b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 5275b2fc499SJeff Garzik break; 5285b2fc499SJeff Garzik case -ENOMEM: 5295b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 5305b2fc499SJeff Garzik break; 5315b2fc499SJeff Garzik case -ENODEV: 532a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "device gone\n"); 5335b2fc499SJeff Garzik netif_device_detach (dev->net); 5345b2fc499SJeff Garzik break; 535dacb3975SDavid S. Miller case -EHOSTUNREACH: 536dacb3975SDavid S. Miller retval = -ENOLINK; 537dacb3975SDavid S. Miller break; 5385b2fc499SJeff Garzik default: 539a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 540a475f603SJoe Perches "rx submit, %d\n", retval); 5415b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 5425b2fc499SJeff Garzik break; 5435b2fc499SJeff Garzik case 0: 5445b6e9bcdSMing Lei __usbnet_queue_skb(&dev->rxq, skb, rx_start); 5455b2fc499SJeff Garzik } 5465b2fc499SJeff Garzik } else { 547a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "rx: stopped\n"); 5485b2fc499SJeff Garzik retval = -ENOLINK; 5495b2fc499SJeff Garzik } 5505b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->rxq.lock, lockflags); 5515b2fc499SJeff Garzik if (retval) { 5525b2fc499SJeff Garzik dev_kfree_skb_any (skb); 5535b2fc499SJeff Garzik usb_free_urb (urb); 5545b2fc499SJeff Garzik } 555dacb3975SDavid S. Miller return retval; 5565b2fc499SJeff Garzik } 5575b2fc499SJeff Garzik 5585b2fc499SJeff Garzik 5595b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5605b2fc499SJeff Garzik 5615b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) 5625b2fc499SJeff Garzik { 5638e95a202SJoe Perches if (dev->driver_info->rx_fixup && 5647a635ea9SAndrzej Zaborowski !dev->driver_info->rx_fixup (dev, skb)) { 5657a635ea9SAndrzej Zaborowski /* With RX_ASSEMBLE, rx_fixup() must update counters */ 5667a635ea9SAndrzej Zaborowski if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE)) 5677a635ea9SAndrzej Zaborowski dev->net->stats.rx_errors++; 5687a635ea9SAndrzej Zaborowski goto done; 5697a635ea9SAndrzej Zaborowski } 5705b2fc499SJeff Garzik // else network stack removes extra byte if we forced a short packet 5715b2fc499SJeff Garzik 572073285fdSAlexey Orishko /* all data was already cloned from skb inside the driver */ 573073285fdSAlexey Orishko if (dev->driver_info->flags & FLAG_MULTI_PACKET) 574eb85569fSEmil Goode goto done; 575eb85569fSEmil Goode 576eb85569fSEmil Goode if (skb->len < ETH_HLEN) { 577eb85569fSEmil Goode dev->net->stats.rx_errors++; 578eb85569fSEmil Goode dev->net->stats.rx_length_errors++; 579eb85569fSEmil Goode netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len); 580eb85569fSEmil Goode } else { 5815b2fc499SJeff Garzik usbnet_skb_return(dev, skb); 582073285fdSAlexey Orishko return; 583073285fdSAlexey Orishko } 584073285fdSAlexey Orishko 5857a635ea9SAndrzej Zaborowski done: 5865b2fc499SJeff Garzik skb_queue_tail(&dev->done, skb); 5875b2fc499SJeff Garzik } 5885b2fc499SJeff Garzik 5895b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5905b2fc499SJeff Garzik 5915b2fc499SJeff Garzik static void rx_complete (struct urb *urb) 5925b2fc499SJeff Garzik { 5935b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 5945b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 5955b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 5965b2fc499SJeff Garzik int urb_status = urb->status; 5975b6e9bcdSMing Lei enum skb_state state; 5985b2fc499SJeff Garzik 5995b2fc499SJeff Garzik skb_put (skb, urb->actual_length); 6005b6e9bcdSMing Lei state = rx_done; 6015b2fc499SJeff Garzik entry->urb = NULL; 6025b2fc499SJeff Garzik 6035b2fc499SJeff Garzik switch (urb_status) { 60418ab458fSDavid Brownell /* success */ 6055b2fc499SJeff Garzik case 0: 6065b2fc499SJeff Garzik break; 6075b2fc499SJeff Garzik 60818ab458fSDavid Brownell /* stalls need manual reset. this is rare ... except that 60918ab458fSDavid Brownell * when going through USB 2.0 TTs, unplug appears this way. 6103ac49a1cSJean Delvare * we avoid the highspeed version of the ETIMEDOUT/EILSEQ 61118ab458fSDavid Brownell * storm, recovering as needed. 61218ab458fSDavid Brownell */ 6135b2fc499SJeff Garzik case -EPIPE: 6147963837fSHerbert Xu dev->net->stats.rx_errors++; 6155b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 6161a10d0bcSGustavo A. R. Silva fallthrough; 6175b2fc499SJeff Garzik 61818ab458fSDavid Brownell /* software-driven interface shutdown */ 61918ab458fSDavid Brownell case -ECONNRESET: /* async unlink */ 62018ab458fSDavid Brownell case -ESHUTDOWN: /* hardware gone */ 621a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 622a475f603SJoe Perches "rx shutdown, code %d\n", urb_status); 6235b2fc499SJeff Garzik goto block; 6245b2fc499SJeff Garzik 62537ebb549SPetr Mladek /* we get controller i/o faults during hub_wq disconnect() delays. 62618ab458fSDavid Brownell * throttle down resubmits, to avoid log floods; just temporarily, 62737ebb549SPetr Mladek * so we still recover when the fault isn't a hub_wq delay. 62818ab458fSDavid Brownell */ 6295b2fc499SJeff Garzik case -EPROTO: 6305b2fc499SJeff Garzik case -ETIME: 6315b2fc499SJeff Garzik case -EILSEQ: 6327963837fSHerbert Xu dev->net->stats.rx_errors++; 6335b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 6345b2fc499SJeff Garzik mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); 635a475f603SJoe Perches netif_dbg(dev, link, dev->net, 636a475f603SJoe Perches "rx throttle %d\n", urb_status); 6375b2fc499SJeff Garzik } 6385b2fc499SJeff Garzik block: 6395b6e9bcdSMing Lei state = rx_cleanup; 6405b2fc499SJeff Garzik entry->urb = urb; 6415b2fc499SJeff Garzik urb = NULL; 6425b2fc499SJeff Garzik break; 6435b2fc499SJeff Garzik 64418ab458fSDavid Brownell /* data overrun ... flush fifo? */ 6455b2fc499SJeff Garzik case -EOVERFLOW: 6467963837fSHerbert Xu dev->net->stats.rx_over_errors++; 647df561f66SGustavo A. R. Silva fallthrough; 6485b2fc499SJeff Garzik 6495b2fc499SJeff Garzik default: 6505b6e9bcdSMing Lei state = rx_cleanup; 6517963837fSHerbert Xu dev->net->stats.rx_errors++; 652a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status); 6535b2fc499SJeff Garzik break; 6545b2fc499SJeff Garzik } 6555b2fc499SJeff Garzik 65670c37bf9SBjørn Mork /* stop rx if packet error rate is high */ 65770c37bf9SBjørn Mork if (++dev->pkt_cnt > 30) { 65870c37bf9SBjørn Mork dev->pkt_cnt = 0; 65970c37bf9SBjørn Mork dev->pkt_err = 0; 66070c37bf9SBjørn Mork } else { 66170c37bf9SBjørn Mork if (state == rx_cleanup) 66270c37bf9SBjørn Mork dev->pkt_err++; 66370c37bf9SBjørn Mork if (dev->pkt_err > 20) 66470c37bf9SBjørn Mork set_bit(EVENT_RX_KILL, &dev->flags); 66570c37bf9SBjørn Mork } 66670c37bf9SBjørn Mork 6675b6e9bcdSMing Lei state = defer_bh(dev, skb, &dev->rxq, state); 6685b2fc499SJeff Garzik 6695b2fc499SJeff Garzik if (urb) { 6708e95a202SJoe Perches if (netif_running (dev->net) && 6715b6e9bcdSMing Lei !test_bit (EVENT_RX_HALT, &dev->flags) && 6725b6e9bcdSMing Lei state != unlink_start) { 6735b2fc499SJeff Garzik rx_submit (dev, urb, GFP_ATOMIC); 6748a783354SOliver Neukum usb_mark_last_busy(dev->udev); 6755b2fc499SJeff Garzik return; 6765b2fc499SJeff Garzik } 6775b2fc499SJeff Garzik usb_free_urb (urb); 6785b2fc499SJeff Garzik } 679a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n"); 6805b2fc499SJeff Garzik } 6815b2fc499SJeff Garzik 6825b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 6837834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev) 6847834ddbcSJussi Kivilinna { 6857834ddbcSJussi Kivilinna set_bit(EVENT_RX_PAUSED, &dev->flags); 6867834ddbcSJussi Kivilinna 687a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n"); 6887834ddbcSJussi Kivilinna } 6897834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx); 6907834ddbcSJussi Kivilinna 6917834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev) 6927834ddbcSJussi Kivilinna { 6937834ddbcSJussi Kivilinna struct sk_buff *skb; 6947834ddbcSJussi Kivilinna int num = 0; 6957834ddbcSJussi Kivilinna 6967834ddbcSJussi Kivilinna clear_bit(EVENT_RX_PAUSED, &dev->flags); 6977834ddbcSJussi Kivilinna 6987834ddbcSJussi Kivilinna while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { 6997834ddbcSJussi Kivilinna usbnet_skb_return(dev, skb); 7007834ddbcSJussi Kivilinna num++; 7017834ddbcSJussi Kivilinna } 7027834ddbcSJussi Kivilinna 7037834ddbcSJussi Kivilinna tasklet_schedule(&dev->bh); 7047834ddbcSJussi Kivilinna 705a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, 706a475f603SJoe Perches "paused rx queue disabled, %d skbs requeued\n", num); 7077834ddbcSJussi Kivilinna } 7087834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx); 7097834ddbcSJussi Kivilinna 7107834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev) 7117834ddbcSJussi Kivilinna { 7127834ddbcSJussi Kivilinna skb_queue_purge(&dev->rxq_pause); 7137834ddbcSJussi Kivilinna } 7147834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); 7157834ddbcSJussi Kivilinna 7167834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/ 7175b2fc499SJeff Garzik 7185b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup 7195b2fc499SJeff Garzik 7205b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 7215b2fc499SJeff Garzik { 7225b2fc499SJeff Garzik unsigned long flags; 7235b6e9bcdSMing Lei struct sk_buff *skb; 7245b2fc499SJeff Garzik int count = 0; 7255b2fc499SJeff Garzik 7265b2fc499SJeff Garzik spin_lock_irqsave (&q->lock, flags); 7275b6e9bcdSMing Lei while (!skb_queue_empty(q)) { 7285b2fc499SJeff Garzik struct skb_data *entry; 7295b2fc499SJeff Garzik struct urb *urb; 7305b2fc499SJeff Garzik int retval; 7315b2fc499SJeff Garzik 7325b6e9bcdSMing Lei skb_queue_walk(q, skb) { 7335b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 7345b6e9bcdSMing Lei if (entry->state != unlink_start) 7355b6e9bcdSMing Lei goto found; 7365b6e9bcdSMing Lei } 7375b6e9bcdSMing Lei break; 7385b6e9bcdSMing Lei found: 7395b6e9bcdSMing Lei entry->state = unlink_start; 7405b2fc499SJeff Garzik urb = entry->urb; 7415b2fc499SJeff Garzik 7420956a8c2Stom.leiming@gmail.com /* 7430956a8c2Stom.leiming@gmail.com * Get reference count of the URB to avoid it to be 7440956a8c2Stom.leiming@gmail.com * freed during usb_unlink_urb, which may trigger 7450956a8c2Stom.leiming@gmail.com * use-after-free problem inside usb_unlink_urb since 7460956a8c2Stom.leiming@gmail.com * usb_unlink_urb is always racing with .complete 7470956a8c2Stom.leiming@gmail.com * handler(include defer_bh). 7480956a8c2Stom.leiming@gmail.com */ 7490956a8c2Stom.leiming@gmail.com usb_get_urb(urb); 7504231d47eSSebastian Siewior spin_unlock_irqrestore(&q->lock, flags); 7515b2fc499SJeff Garzik // during some PM-driven resume scenarios, 7525b2fc499SJeff Garzik // these (async) unlinks complete immediately 7535b2fc499SJeff Garzik retval = usb_unlink_urb (urb); 7545b2fc499SJeff Garzik if (retval != -EINPROGRESS && retval != 0) 75560b86755SJoe Perches netdev_dbg(dev->net, "unlink urb err, %d\n", retval); 7565b2fc499SJeff Garzik else 7575b2fc499SJeff Garzik count++; 7580956a8c2Stom.leiming@gmail.com usb_put_urb(urb); 7594231d47eSSebastian Siewior spin_lock_irqsave(&q->lock, flags); 7605b2fc499SJeff Garzik } 7615b2fc499SJeff Garzik spin_unlock_irqrestore (&q->lock, flags); 7625b2fc499SJeff Garzik return count; 7635b2fc499SJeff Garzik } 7645b2fc499SJeff Garzik 7655b2fc499SJeff Garzik // Flush all pending rx urbs 7665b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes 7675b2fc499SJeff Garzik 7685b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev) 7695b2fc499SJeff Garzik { 7705b2fc499SJeff Garzik if (netif_running(dev->net)) { 7715b2fc499SJeff Garzik (void) unlink_urbs (dev, &dev->rxq); 7725b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 7735b2fc499SJeff Garzik } 7745b2fc499SJeff Garzik } 7755b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); 7765b2fc499SJeff Garzik 7775b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 7785b2fc499SJeff Garzik 779fcb0bb6aSEugene Shatokhin static void wait_skb_queue_empty(struct sk_buff_head *q) 780fcb0bb6aSEugene Shatokhin { 781fcb0bb6aSEugene Shatokhin unsigned long flags; 782fcb0bb6aSEugene Shatokhin 783fcb0bb6aSEugene Shatokhin spin_lock_irqsave(&q->lock, flags); 784fcb0bb6aSEugene Shatokhin while (!skb_queue_empty(q)) { 785fcb0bb6aSEugene Shatokhin spin_unlock_irqrestore(&q->lock, flags); 786fcb0bb6aSEugene Shatokhin schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); 787fcb0bb6aSEugene Shatokhin set_current_state(TASK_UNINTERRUPTIBLE); 788fcb0bb6aSEugene Shatokhin spin_lock_irqsave(&q->lock, flags); 789fcb0bb6aSEugene Shatokhin } 790fcb0bb6aSEugene Shatokhin spin_unlock_irqrestore(&q->lock, flags); 791fcb0bb6aSEugene Shatokhin } 792fcb0bb6aSEugene Shatokhin 7935b2fc499SJeff Garzik // precondition: never called in_interrupt 79469ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev) 79569ee472fSOliver Neukum { 79669ee472fSOliver Neukum DECLARE_WAITQUEUE(wait, current); 79769ee472fSOliver Neukum int temp; 79869ee472fSOliver Neukum 79969ee472fSOliver Neukum /* ensure there are no more active urbs */ 80014a0d635SOliver Neukum add_wait_queue(&dev->wait, &wait); 80169ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 80269ee472fSOliver Neukum temp = unlink_urbs(dev, &dev->txq) + 80369ee472fSOliver Neukum unlink_urbs(dev, &dev->rxq); 80469ee472fSOliver Neukum 80569ee472fSOliver Neukum /* maybe wait for deletions to finish. */ 806fcb0bb6aSEugene Shatokhin wait_skb_queue_empty(&dev->rxq); 807fcb0bb6aSEugene Shatokhin wait_skb_queue_empty(&dev->txq); 808fcb0bb6aSEugene Shatokhin wait_skb_queue_empty(&dev->done); 809a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 810a475f603SJoe Perches "waited for %d urb completions\n", temp); 81169ee472fSOliver Neukum set_current_state(TASK_RUNNING); 81214a0d635SOliver Neukum remove_wait_queue(&dev->wait, &wait); 81369ee472fSOliver Neukum } 8145b2fc499SJeff Garzik 815777baa47SStephen Hemminger int usbnet_stop (struct net_device *net) 8165b2fc499SJeff Garzik { 8175b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 818f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info; 819f50791acSEugene Shatokhin int retval, pm, mpn; 8205b2fc499SJeff Garzik 82175bd0cbdSMing Lei clear_bit(EVENT_DEV_OPEN, &dev->flags); 8225b2fc499SJeff Garzik netif_stop_queue (net); 8235b2fc499SJeff Garzik 824a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 8258ccef431SBen Hutchings "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", 8267963837fSHerbert Xu net->stats.rx_packets, net->stats.tx_packets, 82760b86755SJoe Perches net->stats.rx_errors, net->stats.tx_errors); 8285b2fc499SJeff Garzik 82914a0d635SOliver Neukum /* to not race resume */ 83014a0d635SOliver Neukum pm = usb_autopm_get_interface(dev->intf); 831a33e9e7fSJussi Kivilinna /* allow minidriver to stop correctly (wireless devices to turn off 832a33e9e7fSJussi Kivilinna * radio etc) */ 833a33e9e7fSJussi Kivilinna if (info->stop) { 834a33e9e7fSJussi Kivilinna retval = info->stop(dev); 835a475f603SJoe Perches if (retval < 0) 836a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 83760b86755SJoe Perches "stop fail (%d) usbnet usb-%s-%s, %s\n", 838a33e9e7fSJussi Kivilinna retval, 839a33e9e7fSJussi Kivilinna dev->udev->bus->bus_name, dev->udev->devpath, 840a33e9e7fSJussi Kivilinna info->description); 841a33e9e7fSJussi Kivilinna } 842a33e9e7fSJussi Kivilinna 84369ee472fSOliver Neukum if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) 84469ee472fSOliver Neukum usbnet_terminate_urbs(dev); 8455b2fc499SJeff Garzik 8466eecdc5fSDan Williams usbnet_status_stop(dev); 8475b2fc499SJeff Garzik 8487834ddbcSJussi Kivilinna usbnet_purge_paused_rxq(dev); 8497834ddbcSJussi Kivilinna 850f50791acSEugene Shatokhin mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 851f50791acSEugene Shatokhin 8525b2fc499SJeff Garzik /* deferred work (task, timer, softirq) must also stop. 8535b2fc499SJeff Garzik * can't flush_scheduled_work() until we drop rtnl (later), 8545b2fc499SJeff Garzik * else workers could deadlock; so make workers a NOP. 8555b2fc499SJeff Garzik */ 8565b2fc499SJeff Garzik dev->flags = 0; 8575b2fc499SJeff Garzik del_timer_sync (&dev->delay); 8585b2fc499SJeff Garzik tasklet_kill (&dev->bh); 85914a0d635SOliver Neukum if (!pm) 86014a0d635SOliver Neukum usb_autopm_put_interface(dev->intf); 86114a0d635SOliver Neukum 862f50791acSEugene Shatokhin if (info->manage_power && mpn) 86369ee472fSOliver Neukum info->manage_power(dev, 0); 86469ee472fSOliver Neukum else 865a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 8665b2fc499SJeff Garzik 8675b2fc499SJeff Garzik return 0; 8685b2fc499SJeff Garzik } 869777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop); 8705b2fc499SJeff Garzik 8715b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 8725b2fc499SJeff Garzik 8735b2fc499SJeff Garzik // posts reads, and enables write queuing 8745b2fc499SJeff Garzik 8755b2fc499SJeff Garzik // precondition: never called in_interrupt 8765b2fc499SJeff Garzik 877777baa47SStephen Hemminger int usbnet_open (struct net_device *net) 8785b2fc499SJeff Garzik { 8795b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 880a11a6544SOliver Neukum int retval; 881f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info; 8825b2fc499SJeff Garzik 883a11a6544SOliver Neukum if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { 884a475f603SJoe Perches netif_info(dev, ifup, dev->net, 88560b86755SJoe Perches "resumption fail (%d) usbnet usb-%s-%s, %s\n", 886a11a6544SOliver Neukum retval, 88760b86755SJoe Perches dev->udev->bus->bus_name, 88860b86755SJoe Perches dev->udev->devpath, 889a11a6544SOliver Neukum info->description); 890a11a6544SOliver Neukum goto done_nopm; 891a11a6544SOliver Neukum } 892a11a6544SOliver Neukum 8935b2fc499SJeff Garzik // put into "known safe" state 8945b2fc499SJeff Garzik if (info->reset && (retval = info->reset (dev)) < 0) { 895a475f603SJoe Perches netif_info(dev, ifup, dev->net, 89660b86755SJoe Perches "open reset fail (%d) usbnet usb-%s-%s, %s\n", 8975b2fc499SJeff Garzik retval, 89860b86755SJoe Perches dev->udev->bus->bus_name, 89960b86755SJoe Perches dev->udev->devpath, 9005b2fc499SJeff Garzik info->description); 9015b2fc499SJeff Garzik goto done; 9025b2fc499SJeff Garzik } 9035b2fc499SJeff Garzik 904a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change in reset() */ 905a88c32aeSMing Lei usbnet_update_max_qlen(dev); 906a88c32aeSMing Lei 9075b2fc499SJeff Garzik // insist peer be connected 9085b2fc499SJeff Garzik if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 9094d8c79b7SGrant Grundler netif_err(dev, ifup, dev->net, "can't open; %d\n", retval); 9105b2fc499SJeff Garzik goto done; 9115b2fc499SJeff Garzik } 9125b2fc499SJeff Garzik 9135b2fc499SJeff Garzik /* start any status interrupt transfer */ 9145b2fc499SJeff Garzik if (dev->interrupt) { 9156eecdc5fSDan Williams retval = usbnet_status_start(dev, GFP_KERNEL); 9165b2fc499SJeff Garzik if (retval < 0) { 917a475f603SJoe Perches netif_err(dev, ifup, dev->net, 918a475f603SJoe Perches "intr submit %d\n", retval); 9195b2fc499SJeff Garzik goto done; 9205b2fc499SJeff Garzik } 9215b2fc499SJeff Garzik } 9225b2fc499SJeff Garzik 92368972efaSPaul Stewart set_bit(EVENT_DEV_OPEN, &dev->flags); 9245b2fc499SJeff Garzik netif_start_queue (net); 925a475f603SJoe Perches netif_info(dev, ifup, dev->net, 926a475f603SJoe Perches "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", 92760b86755SJoe Perches (int)RX_QLEN(dev), (int)TX_QLEN(dev), 928a475f603SJoe Perches dev->net->mtu, 929a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : 930a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : 931a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : 932a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : 933a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : 934a475f603SJoe Perches "simple"); 9355b2fc499SJeff Garzik 93670c37bf9SBjørn Mork /* reset rx error state */ 93770c37bf9SBjørn Mork dev->pkt_cnt = 0; 93870c37bf9SBjørn Mork dev->pkt_err = 0; 93970c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 94070c37bf9SBjørn Mork 9415b2fc499SJeff Garzik // delay posting reads until we're fully open 9425b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 94369ee472fSOliver Neukum if (info->manage_power) { 94469ee472fSOliver Neukum retval = info->manage_power(dev, 1); 945a1c088e0SOliver Neukum if (retval < 0) { 946a1c088e0SOliver Neukum retval = 0; 947a1c088e0SOliver Neukum set_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 948a1c088e0SOliver Neukum } else { 94969ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 95069ee472fSOliver Neukum } 951a1c088e0SOliver Neukum } 952a11a6544SOliver Neukum return retval; 9535b2fc499SJeff Garzik done: 954a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 955a11a6544SOliver Neukum done_nopm: 9565b2fc499SJeff Garzik return retval; 9575b2fc499SJeff Garzik } 958777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open); 9595b2fc499SJeff Garzik 9605b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 9615b2fc499SJeff Garzik 9625b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but 9635b2fc499SJeff Garzik * they'll probably want to use this base set. 9645b2fc499SJeff Garzik */ 9655b2fc499SJeff Garzik 96677651900SOliver Neukum /* These methods are written on the assumption that the device 96777651900SOliver Neukum * uses MII 96877651900SOliver Neukum */ 96977651900SOliver Neukum int usbnet_get_link_ksettings_mii(struct net_device *net, 9708bae3551SPhilippe Reynes struct ethtool_link_ksettings *cmd) 9718bae3551SPhilippe Reynes { 9728bae3551SPhilippe Reynes struct usbnet *dev = netdev_priv(net); 9738bae3551SPhilippe Reynes 9748bae3551SPhilippe Reynes if (!dev->mii.mdio_read) 9758bae3551SPhilippe Reynes return -EOPNOTSUPP; 9768bae3551SPhilippe Reynes 97782c01a84Syuval.shaia@oracle.com mii_ethtool_get_link_ksettings(&dev->mii, cmd); 97882c01a84Syuval.shaia@oracle.com 97982c01a84Syuval.shaia@oracle.com return 0; 9808bae3551SPhilippe Reynes } 98177651900SOliver Neukum EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings_mii); 9828bae3551SPhilippe Reynes 983956baa99SOliver Neukum int usbnet_get_link_ksettings_internal(struct net_device *net, 984956baa99SOliver Neukum struct ethtool_link_ksettings *cmd) 985956baa99SOliver Neukum { 986956baa99SOliver Neukum struct usbnet *dev = netdev_priv(net); 987956baa99SOliver Neukum 988956baa99SOliver Neukum /* the assumption that speed is equal on tx and rx 989956baa99SOliver Neukum * is deeply engrained into the networking layer. 990956baa99SOliver Neukum * For wireless stuff it is not true. 991956baa99SOliver Neukum * We assume that rx_speed matters more. 992956baa99SOliver Neukum */ 993956baa99SOliver Neukum if (dev->rx_speed != SPEED_UNSET) 994956baa99SOliver Neukum cmd->base.speed = dev->rx_speed / 1000000; 995956baa99SOliver Neukum else if (dev->tx_speed != SPEED_UNSET) 996956baa99SOliver Neukum cmd->base.speed = dev->tx_speed / 1000000; 997956baa99SOliver Neukum else 998956baa99SOliver Neukum cmd->base.speed = SPEED_UNKNOWN; 999956baa99SOliver Neukum 1000956baa99SOliver Neukum return 0; 1001956baa99SOliver Neukum } 1002956baa99SOliver Neukum EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings_internal); 1003956baa99SOliver Neukum 100477651900SOliver Neukum int usbnet_set_link_ksettings_mii(struct net_device *net, 10058bae3551SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 10068bae3551SPhilippe Reynes { 10078bae3551SPhilippe Reynes struct usbnet *dev = netdev_priv(net); 10088bae3551SPhilippe Reynes int retval; 10098bae3551SPhilippe Reynes 10108bae3551SPhilippe Reynes if (!dev->mii.mdio_write) 10118bae3551SPhilippe Reynes return -EOPNOTSUPP; 10128bae3551SPhilippe Reynes 10138bae3551SPhilippe Reynes retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd); 10148bae3551SPhilippe Reynes 10158bae3551SPhilippe Reynes /* link speed/duplex might have changed */ 10168bae3551SPhilippe Reynes if (dev->driver_info->link_reset) 10178bae3551SPhilippe Reynes dev->driver_info->link_reset(dev); 10188bae3551SPhilippe Reynes 10198bae3551SPhilippe Reynes /* hard_mtu or rx_urb_size may change in link_reset() */ 10208bae3551SPhilippe Reynes usbnet_update_max_qlen(dev); 10218bae3551SPhilippe Reynes 10228bae3551SPhilippe Reynes return retval; 10238bae3551SPhilippe Reynes } 102477651900SOliver Neukum EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings_mii); 10258bae3551SPhilippe Reynes 10265b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net) 10275b2fc499SJeff Garzik { 10285b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10295b2fc499SJeff Garzik 10305b2fc499SJeff Garzik /* If a check_connect is defined, return its result */ 10315b2fc499SJeff Garzik if (dev->driver_info->check_connect) 10325b2fc499SJeff Garzik return dev->driver_info->check_connect (dev) == 0; 10335b2fc499SJeff Garzik 10345b2fc499SJeff Garzik /* if the device has mii operations, use those */ 10355b2fc499SJeff Garzik if (dev->mii.mdio_read) 10365b2fc499SJeff Garzik return mii_link_ok(&dev->mii); 10375b2fc499SJeff Garzik 103805ffb3e2SBjørn Mork /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ 103905ffb3e2SBjørn Mork return ethtool_op_get_link(net); 10405b2fc499SJeff Garzik } 10415b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link); 10425b2fc499SJeff Garzik 10435b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net) 10445b2fc499SJeff Garzik { 10455b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10465b2fc499SJeff Garzik 10475b2fc499SJeff Garzik if (!dev->mii.mdio_write) 10485b2fc499SJeff Garzik return -EOPNOTSUPP; 10495b2fc499SJeff Garzik 10505b2fc499SJeff Garzik return mii_nway_restart(&dev->mii); 10515b2fc499SJeff Garzik } 10525b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset); 10535b2fc499SJeff Garzik 10545b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 10555b2fc499SJeff Garzik { 10565b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10575b2fc499SJeff Garzik 105886a2f415SPhil Sutter strlcpy (info->driver, dev->driver_name, sizeof info->driver); 105986a2f415SPhil Sutter strlcpy (info->fw_version, dev->driver_info->description, 10605b2fc499SJeff Garzik sizeof info->fw_version); 10615b2fc499SJeff Garzik usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 10625b2fc499SJeff Garzik } 10635b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 10645b2fc499SJeff Garzik 10655b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net) 10665b2fc499SJeff Garzik { 10675b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10685b2fc499SJeff Garzik 10695b2fc499SJeff Garzik return dev->msg_enable; 10705b2fc499SJeff Garzik } 10715b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 10725b2fc499SJeff Garzik 10735b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level) 10745b2fc499SJeff Garzik { 10755b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10765b2fc499SJeff Garzik 10775b2fc499SJeff Garzik dev->msg_enable = level; 10785b2fc499SJeff Garzik } 10795b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 10805b2fc499SJeff Garzik 10815b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */ 10820fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = { 10835b2fc499SJeff Garzik .get_link = usbnet_get_link, 10845b2fc499SJeff Garzik .nway_reset = usbnet_nway_reset, 10855b2fc499SJeff Garzik .get_drvinfo = usbnet_get_drvinfo, 10865b2fc499SJeff Garzik .get_msglevel = usbnet_get_msglevel, 10875b2fc499SJeff Garzik .set_msglevel = usbnet_set_msglevel, 1088123edb18SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 108977651900SOliver Neukum .get_link_ksettings = usbnet_get_link_ksettings_mii, 109077651900SOliver Neukum .set_link_ksettings = usbnet_set_link_ksettings_mii, 10915b2fc499SJeff Garzik }; 10925b2fc499SJeff Garzik 10935b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 10945b2fc499SJeff Garzik 10954b49f58fSMing Lei static void __handle_link_change(struct usbnet *dev) 10964b49f58fSMing Lei { 10974b49f58fSMing Lei if (!test_bit(EVENT_DEV_OPEN, &dev->flags)) 10984b49f58fSMing Lei return; 10994b49f58fSMing Lei 11004b49f58fSMing Lei if (!netif_carrier_ok(dev->net)) { 11014b49f58fSMing Lei /* kill URBs for reading packets to save bus bandwidth */ 11024b49f58fSMing Lei unlink_urbs(dev, &dev->rxq); 11034b49f58fSMing Lei 11044b49f58fSMing Lei /* 11054b49f58fSMing Lei * tx_timeout will unlink URBs for sending packets and 11064b49f58fSMing Lei * tx queue is stopped by netcore after link becomes off 11074b49f58fSMing Lei */ 11084b49f58fSMing Lei } else { 11094b49f58fSMing Lei /* submitting URBs for reading packets */ 11104b49f58fSMing Lei tasklet_schedule(&dev->bh); 11114b49f58fSMing Lei } 11124b49f58fSMing Lei 1113a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change during link change */ 1114a88c32aeSMing Lei usbnet_update_max_qlen(dev); 1115a88c32aeSMing Lei 11164b49f58fSMing Lei clear_bit(EVENT_LINK_CHANGE, &dev->flags); 11174b49f58fSMing Lei } 11184b49f58fSMing Lei 11191ea2b748SBjørn Mork void usbnet_set_rx_mode(struct net_device *net) 11201efed2d0SOlivier Blin { 11211efed2d0SOlivier Blin struct usbnet *dev = netdev_priv(net); 11221efed2d0SOlivier Blin 11231efed2d0SOlivier Blin usbnet_defer_kevent(dev, EVENT_SET_RX_MODE); 11241efed2d0SOlivier Blin } 11251ea2b748SBjørn Mork EXPORT_SYMBOL_GPL(usbnet_set_rx_mode); 11261efed2d0SOlivier Blin 11271efed2d0SOlivier Blin static void __handle_set_rx_mode(struct usbnet *dev) 11281efed2d0SOlivier Blin { 11291efed2d0SOlivier Blin if (dev->driver_info->set_rx_mode) 11301efed2d0SOlivier Blin (dev->driver_info->set_rx_mode)(dev); 11311efed2d0SOlivier Blin 11321efed2d0SOlivier Blin clear_bit(EVENT_SET_RX_MODE, &dev->flags); 11331efed2d0SOlivier Blin } 11341efed2d0SOlivier Blin 11355b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd. 11365b2fc499SJeff Garzik * 11375b2fc499SJeff Garzik * NOTE: with 2.5 we could do more of this using completion callbacks, 11385b2fc499SJeff Garzik * especially now that control transfers can be queued. 11395b2fc499SJeff Garzik */ 11405b2fc499SJeff Garzik static void 114111f17ef3SOliver Neukum usbnet_deferred_kevent (struct work_struct *work) 11425b2fc499SJeff Garzik { 11435b2fc499SJeff Garzik struct usbnet *dev = 11445b2fc499SJeff Garzik container_of(work, struct usbnet, kevent); 11455b2fc499SJeff Garzik int status; 11465b2fc499SJeff Garzik 11475b2fc499SJeff Garzik /* usb_clear_halt() needs a thread context */ 11485b2fc499SJeff Garzik if (test_bit (EVENT_TX_HALT, &dev->flags)) { 11495b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 115069ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 115169ee472fSOliver Neukum if (status < 0) 115269ee472fSOliver Neukum goto fail_pipe; 11535b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->out); 115469ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 11558e95a202SJoe Perches if (status < 0 && 11568e95a202SJoe Perches status != -EPIPE && 11578e95a202SJoe Perches status != -ESHUTDOWN) { 11585b2fc499SJeff Garzik if (netif_msg_tx_err (dev)) 115969ee472fSOliver Neukum fail_pipe: 116060b86755SJoe Perches netdev_err(dev->net, "can't clear tx halt, status %d\n", 11615b2fc499SJeff Garzik status); 11625b2fc499SJeff Garzik } else { 11635b2fc499SJeff Garzik clear_bit (EVENT_TX_HALT, &dev->flags); 11645b2fc499SJeff Garzik if (status != -ESHUTDOWN) 11655b2fc499SJeff Garzik netif_wake_queue (dev->net); 11665b2fc499SJeff Garzik } 11675b2fc499SJeff Garzik } 11685b2fc499SJeff Garzik if (test_bit (EVENT_RX_HALT, &dev->flags)) { 11695b2fc499SJeff Garzik unlink_urbs (dev, &dev->rxq); 117069ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 117169ee472fSOliver Neukum if (status < 0) 117269ee472fSOliver Neukum goto fail_halt; 11735b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->in); 117469ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 11758e95a202SJoe Perches if (status < 0 && 11768e95a202SJoe Perches status != -EPIPE && 11778e95a202SJoe Perches status != -ESHUTDOWN) { 11785b2fc499SJeff Garzik if (netif_msg_rx_err (dev)) 117969ee472fSOliver Neukum fail_halt: 118060b86755SJoe Perches netdev_err(dev->net, "can't clear rx halt, status %d\n", 11815b2fc499SJeff Garzik status); 11825b2fc499SJeff Garzik } else { 11835b2fc499SJeff Garzik clear_bit (EVENT_RX_HALT, &dev->flags); 11845b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 11855b2fc499SJeff Garzik } 11865b2fc499SJeff Garzik } 11875b2fc499SJeff Garzik 11885b2fc499SJeff Garzik /* tasklet could resubmit itself forever if memory is tight */ 11895b2fc499SJeff Garzik if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 11905b2fc499SJeff Garzik struct urb *urb = NULL; 1191dacb3975SDavid S. Miller int resched = 1; 11925b2fc499SJeff Garzik 11935b2fc499SJeff Garzik if (netif_running (dev->net)) 11945b2fc499SJeff Garzik urb = usb_alloc_urb (0, GFP_KERNEL); 11955b2fc499SJeff Garzik else 11965b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 11975b2fc499SJeff Garzik if (urb != NULL) { 11985b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 119969ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 1200ab60707fSJesper Juhl if (status < 0) { 1201ab60707fSJesper Juhl usb_free_urb(urb); 120269ee472fSOliver Neukum goto fail_lowmem; 1203ab60707fSJesper Juhl } 1204dacb3975SDavid S. Miller if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) 1205dacb3975SDavid S. Miller resched = 0; 120669ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 120769ee472fSOliver Neukum fail_lowmem: 1208dacb3975SDavid S. Miller if (resched) 12095b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 12105b2fc499SJeff Garzik } 12115b2fc499SJeff Garzik } 12125b2fc499SJeff Garzik 12135b2fc499SJeff Garzik if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 1214f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info; 12155b2fc499SJeff Garzik int retval = 0; 12165b2fc499SJeff Garzik 12175b2fc499SJeff Garzik clear_bit (EVENT_LINK_RESET, &dev->flags); 121869ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 121969ee472fSOliver Neukum if (status < 0) 122069ee472fSOliver Neukum goto skip_reset; 12215b2fc499SJeff Garzik if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 122269ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 122369ee472fSOliver Neukum skip_reset: 122460b86755SJoe Perches netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", 12255b2fc499SJeff Garzik retval, 122660b86755SJoe Perches dev->udev->bus->bus_name, 122760b86755SJoe Perches dev->udev->devpath, 12285b2fc499SJeff Garzik info->description); 122969ee472fSOliver Neukum } else { 123069ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 12315b2fc499SJeff Garzik } 12324b49f58fSMing Lei 12334b49f58fSMing Lei /* handle link change from link resetting */ 12344b49f58fSMing Lei __handle_link_change(dev); 12355b2fc499SJeff Garzik } 12365b2fc499SJeff Garzik 12374b49f58fSMing Lei if (test_bit (EVENT_LINK_CHANGE, &dev->flags)) 12384b49f58fSMing Lei __handle_link_change(dev); 12394b49f58fSMing Lei 12401efed2d0SOlivier Blin if (test_bit (EVENT_SET_RX_MODE, &dev->flags)) 12411efed2d0SOlivier Blin __handle_set_rx_mode(dev); 12421efed2d0SOlivier Blin 12431efed2d0SOlivier Blin 12445b2fc499SJeff Garzik if (dev->flags) 124560b86755SJoe Perches netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); 12465b2fc499SJeff Garzik } 12475b2fc499SJeff Garzik 12485b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 12495b2fc499SJeff Garzik 12505b2fc499SJeff Garzik static void tx_complete (struct urb *urb) 12515b2fc499SJeff Garzik { 12525b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 12535b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 12545b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 12555b2fc499SJeff Garzik 12565b2fc499SJeff Garzik if (urb->status == 0) { 1257af0c351cSHeiner Kallweit struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats); 12582695578bSEric Dumazet unsigned long flags; 1259c8b5d129SGreg Ungerer 12602695578bSEric Dumazet flags = u64_stats_update_begin_irqsave(&stats64->syncp); 1261c8b5d129SGreg Ungerer stats64->tx_packets += entry->packets; 1262c8b5d129SGreg Ungerer stats64->tx_bytes += entry->length; 12632695578bSEric Dumazet u64_stats_update_end_irqrestore(&stats64->syncp, flags); 12645b2fc499SJeff Garzik } else { 12657963837fSHerbert Xu dev->net->stats.tx_errors++; 12665b2fc499SJeff Garzik 12675b2fc499SJeff Garzik switch (urb->status) { 12685b2fc499SJeff Garzik case -EPIPE: 12695b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 12705b2fc499SJeff Garzik break; 12715b2fc499SJeff Garzik 12725b2fc499SJeff Garzik /* software-driven interface shutdown */ 12735b2fc499SJeff Garzik case -ECONNRESET: // async unlink 12745b2fc499SJeff Garzik case -ESHUTDOWN: // hardware gone 12755b2fc499SJeff Garzik break; 12765b2fc499SJeff Garzik 127737ebb549SPetr Mladek /* like rx, tx gets controller i/o faults during hub_wq 127837ebb549SPetr Mladek * delays and so it uses the same throttling mechanism. 127937ebb549SPetr Mladek */ 12805b2fc499SJeff Garzik case -EPROTO: 12815b2fc499SJeff Garzik case -ETIME: 12825b2fc499SJeff Garzik case -EILSEQ: 128369ee472fSOliver Neukum usb_mark_last_busy(dev->udev); 12845b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 12855b2fc499SJeff Garzik mod_timer (&dev->delay, 12865b2fc499SJeff Garzik jiffies + THROTTLE_JIFFIES); 1287a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1288a475f603SJoe Perches "tx throttle %d\n", urb->status); 12895b2fc499SJeff Garzik } 12905b2fc499SJeff Garzik netif_stop_queue (dev->net); 12915b2fc499SJeff Garzik break; 12925b2fc499SJeff Garzik default: 1293a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1294a475f603SJoe Perches "tx err %d\n", entry->urb->status); 12955b2fc499SJeff Garzik break; 12965b2fc499SJeff Garzik } 12975b2fc499SJeff Garzik } 12985b2fc499SJeff Garzik 129969ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 13005b6e9bcdSMing Lei (void) defer_bh(dev, skb, &dev->txq, tx_done); 13015b2fc499SJeff Garzik } 13025b2fc499SJeff Garzik 13035b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 13045b2fc499SJeff Garzik 13050290bd29SMichael S. Tsirkin void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue) 13065b2fc499SJeff Garzik { 13075b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 13085b2fc499SJeff Garzik 13095b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 13105b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 1311dbcdd4d5SOliver Neukum /* this needs to be handled individually because the generic layer 1312dbcdd4d5SOliver Neukum * doesn't know what is sufficient and could not restore private 1313dbcdd4d5SOliver Neukum * information if a remedy of an unconditional reset were used. 1314dbcdd4d5SOliver Neukum */ 1315dbcdd4d5SOliver Neukum if (dev->driver_info->recover) 1316dbcdd4d5SOliver Neukum (dev->driver_info->recover)(dev); 13175b2fc499SJeff Garzik } 1318777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout); 13195b2fc499SJeff Garzik 13205b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 13215b2fc499SJeff Garzik 1322638c5115SMing Lei static int build_dma_sg(const struct sk_buff *skb, struct urb *urb) 1323638c5115SMing Lei { 1324638c5115SMing Lei unsigned num_sgs, total_len = 0; 1325638c5115SMing Lei int i, s = 0; 1326638c5115SMing Lei 1327638c5115SMing Lei num_sgs = skb_shinfo(skb)->nr_frags + 1; 1328638c5115SMing Lei if (num_sgs == 1) 1329638c5115SMing Lei return 0; 1330638c5115SMing Lei 133160e453a9SMing Lei /* reserve one for zero packet */ 13326da2ec56SKees Cook urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist), 133360e453a9SMing Lei GFP_ATOMIC); 1334638c5115SMing Lei if (!urb->sg) 1335638c5115SMing Lei return -ENOMEM; 1336638c5115SMing Lei 1337638c5115SMing Lei urb->num_sgs = num_sgs; 1338fdc3452cSBjørn Mork sg_init_table(urb->sg, urb->num_sgs + 1); 1339638c5115SMing Lei 1340638c5115SMing Lei sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb)); 1341638c5115SMing Lei total_len += skb_headlen(skb); 1342638c5115SMing Lei 1343638c5115SMing Lei for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1344d7840976SMatthew Wilcox (Oracle) skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 1345638c5115SMing Lei 1346638c5115SMing Lei total_len += skb_frag_size(f); 1347d7840976SMatthew Wilcox (Oracle) sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f), 1348b54c9d5bSJonathan Lemon skb_frag_off(f)); 1349638c5115SMing Lei } 1350638c5115SMing Lei urb->transfer_buffer_length = total_len; 1351638c5115SMing Lei 1352638c5115SMing Lei return 1; 1353638c5115SMing Lei } 1354638c5115SMing Lei 135525a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, 135625a79c41SStephen Hemminger struct net_device *net) 13575b2fc499SJeff Garzik { 13585b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 13593e4336a6SJason A. Donenfeld unsigned int length; 13605b2fc499SJeff Garzik struct urb *urb = NULL; 13615b2fc499SJeff Garzik struct skb_data *entry; 1362f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info; 13635b2fc499SJeff Garzik unsigned long flags; 136425a79c41SStephen Hemminger int retval; 13655b2fc499SJeff Garzik 136623ba0799SKonstantin Khlebnikov if (skb) 1367f9b491ecSMichael Riesch skb_tx_timestamp(skb); 1368f9b491ecSMichael Riesch 13695b2fc499SJeff Garzik // some devices want funky USB-level framing, for 13705b2fc499SJeff Garzik // win32 driver (usually) and/or hardware quirks 13715b2fc499SJeff Garzik if (info->tx_fixup) { 13725b2fc499SJeff Garzik skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 13735b2fc499SJeff Garzik if (!skb) { 1374bf414b36SBjørn Mork /* packet collected; minidriver waiting for more */ 1375bf414b36SBjørn Mork if (info->flags & FLAG_MULTI_PACKET) 1376bf414b36SBjørn Mork goto not_drop; 1377a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); 13785b2fc499SJeff Garzik goto drop; 13795b2fc499SJeff Garzik } 13805b2fc499SJeff Garzik } 13815b2fc499SJeff Garzik 13825b2fc499SJeff Garzik if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 1383a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "no urb\n"); 13845b2fc499SJeff Garzik goto drop; 13855b2fc499SJeff Garzik } 13865b2fc499SJeff Garzik 13875b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 13885b2fc499SJeff Garzik entry->urb = urb; 13895b2fc499SJeff Garzik entry->dev = dev; 13905b2fc499SJeff Garzik 13915b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->out, 13925b2fc499SJeff Garzik skb->data, skb->len, tx_complete, skb); 1393638c5115SMing Lei if (dev->can_dma_sg) { 1394638c5115SMing Lei if (build_dma_sg(skb, urb) < 0) 1395638c5115SMing Lei goto drop; 1396638c5115SMing Lei } 139760e453a9SMing Lei length = urb->transfer_buffer_length; 13985b2fc499SJeff Garzik 13995b2fc499SJeff Garzik /* don't assume the hardware handles USB_ZERO_PACKET 14005b2fc499SJeff Garzik * NOTE: strictly conforming cdc-ether devices should expect 14015b2fc499SJeff Garzik * the ZLP here, but ignore the one-byte packet. 1402073285fdSAlexey Orishko * NOTE2: CDC NCM specification is different from CDC ECM when 1403073285fdSAlexey Orishko * handling ZLP/short packets, so cdc_ncm driver will make short 1404073285fdSAlexey Orishko * packet itself if needed. 14055b2fc499SJeff Garzik */ 1406b4d562e3SElina Pasheva if (length % dev->maxpacket == 0) { 1407b4d562e3SElina Pasheva if (!(info->flags & FLAG_SEND_ZLP)) { 1408073285fdSAlexey Orishko if (!(info->flags & FLAG_MULTI_PACKET)) { 140960e453a9SMing Lei length++; 141060e453a9SMing Lei if (skb_tailroom(skb) && !urb->num_sgs) { 14113e323f3eSPeter Korsgaard skb->data[skb->len] = 0; 14123e323f3eSPeter Korsgaard __skb_put(skb, 1); 141360e453a9SMing Lei } else if (urb->num_sgs) 141460e453a9SMing Lei sg_set_buf(&urb->sg[urb->num_sgs++], 141560e453a9SMing Lei dev->padding_pkt, 1); 1416073285fdSAlexey Orishko } 1417b4d562e3SElina Pasheva } else 1418b4d562e3SElina Pasheva urb->transfer_flags |= URB_ZERO_PACKET; 14193e323f3eSPeter Korsgaard } 14207a1e890eSBen Hutchings urb->transfer_buffer_length = length; 14217a1e890eSBen Hutchings 14227a1e890eSBen Hutchings if (info->flags & FLAG_MULTI_PACKET) { 14237a1e890eSBen Hutchings /* Driver has set number of packets and a length delta. 14247a1e890eSBen Hutchings * Calculate the complete length and ensure that it's 14257a1e890eSBen Hutchings * positive. 14267a1e890eSBen Hutchings */ 14277a1e890eSBen Hutchings entry->length += length; 14287a1e890eSBen Hutchings if (WARN_ON_ONCE(entry->length <= 0)) 14297a1e890eSBen Hutchings entry->length = length; 14307a1e890eSBen Hutchings } else { 14317a1e890eSBen Hutchings usbnet_set_skb_tx_stats(skb, 1, length); 14327a1e890eSBen Hutchings } 14335b2fc499SJeff Garzik 14345b2fc499SJeff Garzik spin_lock_irqsave(&dev->txq.lock, flags); 143569ee472fSOliver Neukum retval = usb_autopm_get_interface_async(dev->intf); 143669ee472fSOliver Neukum if (retval < 0) { 143769ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 143869ee472fSOliver Neukum goto drop; 143969ee472fSOliver Neukum } 1440ad70411aSKloetzke Jan if (netif_queue_stopped(net)) { 1441ad70411aSKloetzke Jan usb_autopm_put_interface_async(dev->intf); 1442ad70411aSKloetzke Jan spin_unlock_irqrestore(&dev->txq.lock, flags); 1443ad70411aSKloetzke Jan goto drop; 1444ad70411aSKloetzke Jan } 144569ee472fSOliver Neukum 144669ee472fSOliver Neukum #ifdef CONFIG_PM 144769ee472fSOliver Neukum /* if this triggers the device is still a sleep */ 144869ee472fSOliver Neukum if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { 144969ee472fSOliver Neukum /* transmission will be done in resume */ 145069ee472fSOliver Neukum usb_anchor_urb(urb, &dev->deferred); 145169ee472fSOliver Neukum /* no use to process more packets */ 145269ee472fSOliver Neukum netif_stop_queue(net); 145339707c2aSHemant Kumar usb_put_urb(urb); 145469ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 145560b86755SJoe Perches netdev_dbg(dev->net, "Delaying transmission for resumption\n"); 145669ee472fSOliver Neukum goto deferred; 145769ee472fSOliver Neukum } 145869ee472fSOliver Neukum #endif 14595b2fc499SJeff Garzik 14605b2fc499SJeff Garzik switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 14615b2fc499SJeff Garzik case -EPIPE: 14625b2fc499SJeff Garzik netif_stop_queue (net); 14635b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 146469ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 14655b2fc499SJeff Garzik break; 14665b2fc499SJeff Garzik default: 146769ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 1468a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1469a475f603SJoe Perches "tx: submit urb err %d\n", retval); 14705b2fc499SJeff Garzik break; 14715b2fc499SJeff Garzik case 0: 1472860e9538SFlorian Westphal netif_trans_update(net); 14735b6e9bcdSMing Lei __usbnet_queue_skb(&dev->txq, skb, tx_start); 14745b2fc499SJeff Garzik if (dev->txq.qlen >= TX_QLEN (dev)) 14755b2fc499SJeff Garzik netif_stop_queue (net); 14765b2fc499SJeff Garzik } 14775b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->txq.lock, flags); 14785b2fc499SJeff Garzik 14795b2fc499SJeff Garzik if (retval) { 1480a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); 14815b2fc499SJeff Garzik drop: 14827963837fSHerbert Xu dev->net->stats.tx_dropped++; 1483073285fdSAlexey Orishko not_drop: 14845b2fc499SJeff Garzik if (skb) 14855b2fc499SJeff Garzik dev_kfree_skb_any (skb); 1486638c5115SMing Lei if (urb) { 1487638c5115SMing Lei kfree(urb->sg); 14885b2fc499SJeff Garzik usb_free_urb(urb); 1489638c5115SMing Lei } 1490a475f603SJoe Perches } else 1491a475f603SJoe Perches netif_dbg(dev, tx_queued, dev->net, 14923e4336a6SJason A. Donenfeld "> tx, len %u, type 0x%x\n", length, skb->protocol); 149369ee472fSOliver Neukum #ifdef CONFIG_PM 149469ee472fSOliver Neukum deferred: 149569ee472fSOliver Neukum #endif 149625a79c41SStephen Hemminger return NETDEV_TX_OK; 14975b2fc499SJeff Garzik } 1498777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit); 14995b2fc499SJeff Garzik 150085e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags) 150165841fd5SMing Lei { 150265841fd5SMing Lei struct urb *urb; 150365841fd5SMing Lei int i; 150485e87870SBjørn Mork int ret = 0; 150565841fd5SMing Lei 150665841fd5SMing Lei /* don't refill the queue all at once */ 150765841fd5SMing Lei for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) { 150865841fd5SMing Lei urb = usb_alloc_urb(0, flags); 150965841fd5SMing Lei if (urb != NULL) { 151085e87870SBjørn Mork ret = rx_submit(dev, urb, flags); 151185e87870SBjørn Mork if (ret) 151285e87870SBjørn Mork goto err; 151385e87870SBjørn Mork } else { 151485e87870SBjørn Mork ret = -ENOMEM; 151585e87870SBjørn Mork goto err; 151665841fd5SMing Lei } 151765841fd5SMing Lei } 151885e87870SBjørn Mork err: 151985e87870SBjørn Mork return ret; 152065841fd5SMing Lei } 152165841fd5SMing Lei 15225b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 15235b2fc499SJeff Garzik 15245b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer 15255b2fc499SJeff Garzik 15262183c1a6SKees Cook static void usbnet_bh (struct timer_list *t) 15275b2fc499SJeff Garzik { 15282183c1a6SKees Cook struct usbnet *dev = from_timer(dev, t, delay); 15295b2fc499SJeff Garzik struct sk_buff *skb; 15305b2fc499SJeff Garzik struct skb_data *entry; 15315b2fc499SJeff Garzik 15325b2fc499SJeff Garzik while ((skb = skb_dequeue (&dev->done))) { 15335b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 15345b2fc499SJeff Garzik switch (entry->state) { 15355b2fc499SJeff Garzik case rx_done: 15365b2fc499SJeff Garzik entry->state = rx_cleanup; 15375b2fc499SJeff Garzik rx_process (dev, skb); 15385b2fc499SJeff Garzik continue; 15395b2fc499SJeff Garzik case tx_done: 1540638c5115SMing Lei kfree(entry->urb->sg); 1541df561f66SGustavo A. R. Silva fallthrough; 15425b2fc499SJeff Garzik case rx_cleanup: 15435b2fc499SJeff Garzik usb_free_urb (entry->urb); 15445b2fc499SJeff Garzik dev_kfree_skb (skb); 15455b2fc499SJeff Garzik continue; 15465b2fc499SJeff Garzik default: 154760b86755SJoe Perches netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); 15485b2fc499SJeff Garzik } 15495b2fc499SJeff Garzik } 15505b2fc499SJeff Garzik 155170c37bf9SBjørn Mork /* restart RX again after disabling due to high error rate */ 155270c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 155370c37bf9SBjørn Mork 155414a0d635SOliver Neukum /* waiting for all pending urbs to complete? 155514a0d635SOliver Neukum * only then can we forgo submitting anew 155614a0d635SOliver Neukum */ 155714a0d635SOliver Neukum if (waitqueue_active(&dev->wait)) { 155814a0d635SOliver Neukum if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0) 155914a0d635SOliver Neukum wake_up_all(&dev->wait); 15605b2fc499SJeff Garzik 15615b2fc499SJeff Garzik // or are we maybe short a few urbs? 15628e95a202SJoe Perches } else if (netif_running (dev->net) && 15638e95a202SJoe Perches netif_device_present (dev->net) && 15644b49f58fSMing Lei netif_carrier_ok(dev->net) && 15658e95a202SJoe Perches !timer_pending(&dev->delay) && 156643daa96bSSoohoon Lee !test_bit(EVENT_RX_PAUSED, &dev->flags) && 15678e95a202SJoe Perches !test_bit(EVENT_RX_HALT, &dev->flags)) { 15685b2fc499SJeff Garzik int temp = dev->rxq.qlen; 15695b2fc499SJeff Garzik 157065841fd5SMing Lei if (temp < RX_QLEN(dev)) { 157185e87870SBjørn Mork if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK) 157285e87870SBjørn Mork return; 1573a475f603SJoe Perches if (temp != dev->rxq.qlen) 1574a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1575a475f603SJoe Perches "rxqlen %d --> %d\n", 15765b2fc499SJeff Garzik temp, dev->rxq.qlen); 157765841fd5SMing Lei if (dev->rxq.qlen < RX_QLEN(dev)) 15785b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 15795b2fc499SJeff Garzik } 15805b2fc499SJeff Garzik if (dev->txq.qlen < TX_QLEN (dev)) 15815b2fc499SJeff Garzik netif_wake_queue (dev->net); 15825b2fc499SJeff Garzik } 15835b2fc499SJeff Garzik } 15845b2fc499SJeff Garzik 1585c955e329SEmil Renner Berthing static void usbnet_bh_tasklet(struct tasklet_struct *t) 15862eb1d3f4SPhong Tran { 1587c955e329SEmil Renner Berthing struct usbnet *dev = from_tasklet(dev, t, bh); 15882eb1d3f4SPhong Tran 1589c955e329SEmil Renner Berthing usbnet_bh(&dev->delay); 15902eb1d3f4SPhong Tran } 15912eb1d3f4SPhong Tran 15925b2fc499SJeff Garzik 15935b2fc499SJeff Garzik /*------------------------------------------------------------------------- 15945b2fc499SJeff Garzik * 15955b2fc499SJeff Garzik * USB Device Driver support 15965b2fc499SJeff Garzik * 15975b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 15985b2fc499SJeff Garzik 15995b2fc499SJeff Garzik // precondition: never called in_interrupt 16005b2fc499SJeff Garzik 16015b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf) 16025b2fc499SJeff Garzik { 16035b2fc499SJeff Garzik struct usbnet *dev; 16045b2fc499SJeff Garzik struct usb_device *xdev; 16055b2fc499SJeff Garzik struct net_device *net; 16065b2fc499SJeff Garzik 16075b2fc499SJeff Garzik dev = usb_get_intfdata(intf); 16085b2fc499SJeff Garzik usb_set_intfdata(intf, NULL); 16095b2fc499SJeff Garzik if (!dev) 16105b2fc499SJeff Garzik return; 16115b2fc499SJeff Garzik 16125b2fc499SJeff Garzik xdev = interface_to_usbdev (intf); 16135b2fc499SJeff Garzik 1614a475f603SJoe Perches netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", 16155b2fc499SJeff Garzik intf->dev.driver->name, 16165b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 16175b2fc499SJeff Garzik dev->driver_info->description); 16185b2fc499SJeff Garzik 16192c9d6c2bSOleksij Rempel if (dev->driver_info->unbind) 16202c9d6c2bSOleksij Rempel dev->driver_info->unbind(dev, intf); 16212c9d6c2bSOleksij Rempel 16225b2fc499SJeff Garzik net = dev->net; 16235b2fc499SJeff Garzik unregister_netdev (net); 16245b2fc499SJeff Garzik 162523f333a2STejun Heo cancel_work_sync(&dev->kevent); 16265b2fc499SJeff Garzik 162739707c2aSHemant Kumar usb_scuttle_anchored_urbs(&dev->deferred); 162839707c2aSHemant Kumar 162968972efaSPaul Stewart usb_kill_urb(dev->interrupt); 163068972efaSPaul Stewart usb_free_urb(dev->interrupt); 163160e453a9SMing Lei kfree(dev->padding_pkt); 163268972efaSPaul Stewart 1633af0c351cSHeiner Kallweit free_percpu(net->tstats); 16345b2fc499SJeff Garzik free_netdev(net); 16355b2fc499SJeff Garzik } 16365b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect); 16375b2fc499SJeff Garzik 1638777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = { 1639777baa47SStephen Hemminger .ndo_open = usbnet_open, 1640777baa47SStephen Hemminger .ndo_stop = usbnet_stop, 1641777baa47SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 1642777baa47SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 16431efed2d0SOlivier Blin .ndo_set_rx_mode = usbnet_set_rx_mode, 1644777baa47SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu, 1645af0c351cSHeiner Kallweit .ndo_get_stats64 = dev_get_tstats64, 1646777baa47SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1647777baa47SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1648777baa47SStephen Hemminger }; 16495b2fc499SJeff Garzik 16505b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 16515b2fc499SJeff Garzik 16525b2fc499SJeff Garzik // precondition: never called in_interrupt 16535b2fc499SJeff Garzik 1654225794f8SMarcel Holtmann static struct device_type wlan_type = { 1655225794f8SMarcel Holtmann .name = "wlan", 1656225794f8SMarcel Holtmann }; 1657225794f8SMarcel Holtmann 1658225794f8SMarcel Holtmann static struct device_type wwan_type = { 1659225794f8SMarcel Holtmann .name = "wwan", 1660225794f8SMarcel Holtmann }; 1661225794f8SMarcel Holtmann 16625b2fc499SJeff Garzik int 16635b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 16645b2fc499SJeff Garzik { 16655b2fc499SJeff Garzik struct usbnet *dev; 16665b2fc499SJeff Garzik struct net_device *net; 16675b2fc499SJeff Garzik struct usb_host_interface *interface; 1668f3edc2dbSBen Dooks const struct driver_info *info; 16695b2fc499SJeff Garzik struct usb_device *xdev; 16705b2fc499SJeff Garzik int status; 16715b2fc499SJeff Garzik const char *name; 1672b0786b43SMing Lei struct usb_driver *driver = to_usb_driver(udev->dev.driver); 1673b0786b43SMing Lei 1674b0786b43SMing Lei /* usbnet already took usb runtime pm, so have to enable the feature 1675b0786b43SMing Lei * for usb interface, otherwise usb_autopm_get_interface may return 167698f541c6SAlan Stern * failure if RUNTIME_PM is enabled. 1677b0786b43SMing Lei */ 1678b0786b43SMing Lei if (!driver->supports_autosuspend) { 1679b0786b43SMing Lei driver->supports_autosuspend = 1; 1680b0786b43SMing Lei pm_runtime_enable(&udev->dev); 1681b0786b43SMing Lei } 16825b2fc499SJeff Garzik 16835b2fc499SJeff Garzik name = udev->dev.driver->name; 1684f3edc2dbSBen Dooks info = (const struct driver_info *) prod->driver_info; 16855b2fc499SJeff Garzik if (!info) { 16865b2fc499SJeff Garzik dev_dbg (&udev->dev, "blacklisted by %s\n", name); 16875b2fc499SJeff Garzik return -ENODEV; 16885b2fc499SJeff Garzik } 16895b2fc499SJeff Garzik xdev = interface_to_usbdev (udev); 16905b2fc499SJeff Garzik interface = udev->cur_altsetting; 16915b2fc499SJeff Garzik 16925b2fc499SJeff Garzik status = -ENOMEM; 16935b2fc499SJeff Garzik 16945b2fc499SJeff Garzik // set up our own records 16955b2fc499SJeff Garzik net = alloc_etherdev(sizeof(*dev)); 169641de8d4cSJoe Perches if (!net) 16975b2fc499SJeff Garzik goto out; 16985b2fc499SJeff Garzik 16990dacca73SBen Hutchings /* netdev_printk() needs this so do it as early as possible */ 17000dacca73SBen Hutchings SET_NETDEV_DEV(net, &udev->dev); 17010dacca73SBen Hutchings 17025b2fc499SJeff Garzik dev = netdev_priv(net); 17035b2fc499SJeff Garzik dev->udev = xdev; 1704a11a6544SOliver Neukum dev->intf = udev; 17055b2fc499SJeff Garzik dev->driver_info = info; 17065b2fc499SJeff Garzik dev->driver_name = name; 1707956baa99SOliver Neukum dev->rx_speed = SPEED_UNSET; 1708956baa99SOliver Neukum dev->tx_speed = SPEED_UNSET; 1709c8b5d129SGreg Ungerer 1710af0c351cSHeiner Kallweit net->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 1711af0c351cSHeiner Kallweit if (!net->tstats) 1712c8b5d129SGreg Ungerer goto out0; 1713c8b5d129SGreg Ungerer 17145b2fc499SJeff Garzik dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 17155b2fc499SJeff Garzik | NETIF_MSG_PROBE | NETIF_MSG_LINK); 171614a0d635SOliver Neukum init_waitqueue_head(&dev->wait); 17175b2fc499SJeff Garzik skb_queue_head_init (&dev->rxq); 17185b2fc499SJeff Garzik skb_queue_head_init (&dev->txq); 17195b2fc499SJeff Garzik skb_queue_head_init (&dev->done); 17207834ddbcSJussi Kivilinna skb_queue_head_init(&dev->rxq_pause); 1721c955e329SEmil Renner Berthing tasklet_setup(&dev->bh, usbnet_bh_tasklet); 172211f17ef3SOliver Neukum INIT_WORK (&dev->kevent, usbnet_deferred_kevent); 172369ee472fSOliver Neukum init_usb_anchor(&dev->deferred); 17242183c1a6SKees Cook timer_setup(&dev->delay, usbnet_bh, 0); 17255b2fc499SJeff Garzik mutex_init (&dev->phy_mutex); 17266eecdc5fSDan Williams mutex_init(&dev->interrupt_mutex); 17276eecdc5fSDan Williams dev->interrupt_count = 0; 17285b2fc499SJeff Garzik 17295b2fc499SJeff Garzik dev->net = net; 1730493c3ca6SLen Baker strscpy(net->name, "usb%d", sizeof(net->name)); 1731*2674e7eaSJakub Kicinski eth_hw_addr_set(net, node_id); 17325b2fc499SJeff Garzik 17335b2fc499SJeff Garzik /* rx and tx sides can use different message sizes; 17345b2fc499SJeff Garzik * bind() should set rx_urb_size in that case. 17355b2fc499SJeff Garzik */ 17365b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 1737f77f0aeeSJarod Wilson net->min_mtu = 0; 1738f77f0aeeSJarod Wilson net->max_mtu = ETH_MAX_MTU; 17395b2fc499SJeff Garzik 1740777baa47SStephen Hemminger net->netdev_ops = &usbnet_netdev_ops; 1741777baa47SStephen Hemminger net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 17425b2fc499SJeff Garzik net->ethtool_ops = &usbnet_ethtool_ops; 17435b2fc499SJeff Garzik 17445b2fc499SJeff Garzik // allow device-specific bind/init procedures 17455b2fc499SJeff Garzik // NOTE net->name still not usable ... 17465b2fc499SJeff Garzik if (info->bind) { 17475b2fc499SJeff Garzik status = info->bind (dev, udev); 17485b2fc499SJeff Garzik if (status < 0) 17495b2fc499SJeff Garzik goto out1; 17505b2fc499SJeff Garzik 17515b2fc499SJeff Garzik // heuristic: "usb%d" for links we know are two-host, 17525b2fc499SJeff Garzik // else "eth%d" when there's reasonable doubt. userspace 17535b2fc499SJeff Garzik // can rename the link if it knows better. 17548e95a202SJoe Perches if ((dev->driver_info->flags & FLAG_ETHER) != 0 && 1755c261344dSArnd Bergmann ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 || 1756c261344dSArnd Bergmann (net->dev_addr [0] & 0x02) == 0)) 1757493c3ca6SLen Baker strscpy(net->name, "eth%d", sizeof(net->name)); 17586e3bbcc5SJussi Kivilinna /* WLAN devices should always be named "wlan%d" */ 17596e3bbcc5SJussi Kivilinna if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1760493c3ca6SLen Baker strscpy(net->name, "wlan%d", sizeof(net->name)); 1761e1e499eeSMarcel Holtmann /* WWAN devices should always be named "wwan%d" */ 1762e1e499eeSMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1763493c3ca6SLen Baker strscpy(net->name, "wwan%d", sizeof(net->name)); 17645b2fc499SJeff Garzik 17656509141fSWei Shuai /* devices that cannot do ARP */ 17666509141fSWei Shuai if ((dev->driver_info->flags & FLAG_NOARP) != 0) 17676509141fSWei Shuai net->flags |= IFF_NOARP; 17686509141fSWei Shuai 17695b2fc499SJeff Garzik /* maybe the remote can't receive an Ethernet MTU */ 17705b2fc499SJeff Garzik if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 17715b2fc499SJeff Garzik net->mtu = dev->hard_mtu - net->hard_header_len; 17725b2fc499SJeff Garzik } else if (!info->in || !info->out) 17735b2fc499SJeff Garzik status = usbnet_get_endpoints (dev, udev); 17745b2fc499SJeff Garzik else { 17755b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (xdev, info->in); 17765b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (xdev, info->out); 17775b2fc499SJeff Garzik if (!(info->flags & FLAG_NO_SETINT)) 17785b2fc499SJeff Garzik status = usb_set_interface (xdev, 17795b2fc499SJeff Garzik interface->desc.bInterfaceNumber, 17805b2fc499SJeff Garzik interface->desc.bAlternateSetting); 17815b2fc499SJeff Garzik else 17825b2fc499SJeff Garzik status = 0; 17835b2fc499SJeff Garzik 17845b2fc499SJeff Garzik } 17859514bfe5SPeter Korsgaard if (status >= 0 && dev->status) 17865b2fc499SJeff Garzik status = init_status (dev, udev); 17875b2fc499SJeff Garzik if (status < 0) 17885b2fc499SJeff Garzik goto out3; 17895b2fc499SJeff Garzik 17905b2fc499SJeff Garzik if (!dev->rx_urb_size) 17915b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 17925b2fc499SJeff Garzik dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); 1793397430b5SOliver Neukum if (dev->maxpacket == 0) { 1794397430b5SOliver Neukum /* that is a broken device */ 1795397430b5SOliver Neukum goto out4; 1796397430b5SOliver Neukum } 17975b2fc499SJeff Garzik 1798eef23b53SBjørn Mork /* let userspace know we have a random address */ 1799eef23b53SBjørn Mork if (ether_addr_equal(net->dev_addr, node_id)) 1800eef23b53SBjørn Mork net->addr_assign_type = NET_ADDR_RANDOM; 1801eef23b53SBjørn Mork 1802225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1803225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wlan_type); 1804225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1805225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wwan_type); 1806225794f8SMarcel Holtmann 1807a88c32aeSMing Lei /* initialize max rx_qlen and tx_qlen */ 1808a88c32aeSMing Lei usbnet_update_max_qlen(dev); 1809a88c32aeSMing Lei 181060e453a9SMing Lei if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) && 181160e453a9SMing Lei !(info->flags & FLAG_MULTI_PACKET)) { 181260e453a9SMing Lei dev->padding_pkt = kzalloc(1, GFP_KERNEL); 181309b69024SWei Yongjun if (!dev->padding_pkt) { 181409b69024SWei Yongjun status = -ENOMEM; 181560e453a9SMing Lei goto out4; 181660e453a9SMing Lei } 181709b69024SWei Yongjun } 181860e453a9SMing Lei 18195b2fc499SJeff Garzik status = register_netdev (net); 18205b2fc499SJeff Garzik if (status) 182160e453a9SMing Lei goto out5; 1822a475f603SJoe Perches netif_info(dev, probe, dev->net, 1823a475f603SJoe Perches "register '%s' at usb-%s-%s, %s, %pM\n", 18245b2fc499SJeff Garzik udev->dev.driver->name, 18255b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 18265b2fc499SJeff Garzik dev->driver_info->description, 1827e174961cSJohannes Berg net->dev_addr); 18285b2fc499SJeff Garzik 18295b2fc499SJeff Garzik // ok, it's ready to go. 18305b2fc499SJeff Garzik usb_set_intfdata (udev, dev); 18315b2fc499SJeff Garzik 18325b2fc499SJeff Garzik netif_device_attach (net); 18335b2fc499SJeff Garzik 183437e8273cSBen Hutchings if (dev->driver_info->flags & FLAG_LINK_INTR) 18350162c554SMing Lei usbnet_link_change(dev, 0, 0); 183637e8273cSBen Hutchings 18375b2fc499SJeff Garzik return 0; 18385b2fc499SJeff Garzik 183960e453a9SMing Lei out5: 184060e453a9SMing Lei kfree(dev->padding_pkt); 1841a4723848Stom.leiming@gmail.com out4: 1842a4723848Stom.leiming@gmail.com usb_free_urb(dev->interrupt); 18435b2fc499SJeff Garzik out3: 18445b2fc499SJeff Garzik if (info->unbind) 18455b2fc499SJeff Garzik info->unbind (dev, udev); 18465b2fc499SJeff Garzik out1: 18471666984cSOliver Neukum /* subdrivers must undo all they did in bind() if they 18481666984cSOliver Neukum * fail it, but we may fail later and a deferred kevent 18491666984cSOliver Neukum * may trigger an error resubmitting itself and, worse, 18501666984cSOliver Neukum * schedule a timer. So we kill it all just in case. 18511666984cSOliver Neukum */ 18521666984cSOliver Neukum cancel_work_sync(&dev->kevent); 18531666984cSOliver Neukum del_timer_sync(&dev->delay); 1854af0c351cSHeiner Kallweit free_percpu(net->tstats); 1855c8b5d129SGreg Ungerer out0: 18565b2fc499SJeff Garzik free_netdev(net); 18575b2fc499SJeff Garzik out: 18585b2fc499SJeff Garzik return status; 18595b2fc499SJeff Garzik } 18605b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe); 18615b2fc499SJeff Garzik 18625b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 18635b2fc499SJeff Garzik 186436433127SOliver Neukum /* 186536433127SOliver Neukum * suspend the whole driver as soon as the first interface is suspended 186636433127SOliver Neukum * resume only when the last interface is resumed 18675b2fc499SJeff Garzik */ 18685b2fc499SJeff Garzik 18695b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 18705b2fc499SJeff Garzik { 18715b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 18725b2fc499SJeff Garzik 187336433127SOliver Neukum if (!dev->suspend_count++) { 187469ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 187569ee472fSOliver Neukum /* don't autosuspend while transmitting */ 18765b1b0b81SAlan Stern if (dev->txq.qlen && PMSG_IS_AUTO(message)) { 18775eeb3132SMing Lei dev->suspend_count--; 187869ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 187969ee472fSOliver Neukum return -EBUSY; 188069ee472fSOliver Neukum } else { 188169ee472fSOliver Neukum set_bit(EVENT_DEV_ASLEEP, &dev->flags); 188269ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 188369ee472fSOliver Neukum } 1884a11a6544SOliver Neukum /* 1885a11a6544SOliver Neukum * accelerate emptying of the rx and queues, to avoid 18865b2fc499SJeff Garzik * having everything error out. 18875b2fc499SJeff Garzik */ 18885b2fc499SJeff Garzik netif_device_detach (dev->net); 188969ee472fSOliver Neukum usbnet_terminate_urbs(dev); 18906eecdc5fSDan Williams __usbnet_status_stop_force(dev); 189169ee472fSOliver Neukum 1892a11a6544SOliver Neukum /* 1893a11a6544SOliver Neukum * reattach so runtime management can use and 1894a11a6544SOliver Neukum * wake the device 1895a11a6544SOliver Neukum */ 1896a11a6544SOliver Neukum netif_device_attach (dev->net); 189736433127SOliver Neukum } 18985b2fc499SJeff Garzik return 0; 18995b2fc499SJeff Garzik } 19005b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend); 19015b2fc499SJeff Garzik 19025b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf) 19035b2fc499SJeff Garzik { 19045b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 190569ee472fSOliver Neukum struct sk_buff *skb; 190669ee472fSOliver Neukum struct urb *res; 190769ee472fSOliver Neukum int retval; 19085b2fc499SJeff Garzik 190969ee472fSOliver Neukum if (!--dev->suspend_count) { 19106eecdc5fSDan Williams /* resume interrupt URB if it was previously submitted */ 19116eecdc5fSDan Williams __usbnet_status_start_force(dev, GFP_NOIO); 191268972efaSPaul Stewart 191369ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 191469ee472fSOliver Neukum while ((res = usb_get_from_anchor(&dev->deferred))) { 191569ee472fSOliver Neukum 191669ee472fSOliver Neukum skb = (struct sk_buff *)res->context; 191769ee472fSOliver Neukum retval = usb_submit_urb(res, GFP_ATOMIC); 191869ee472fSOliver Neukum if (retval < 0) { 191969ee472fSOliver Neukum dev_kfree_skb_any(skb); 1920638c5115SMing Lei kfree(res->sg); 192169ee472fSOliver Neukum usb_free_urb(res); 192269ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 192369ee472fSOliver Neukum } else { 1924860e9538SFlorian Westphal netif_trans_update(dev->net); 192569ee472fSOliver Neukum __skb_queue_tail(&dev->txq, skb); 192669ee472fSOliver Neukum } 192769ee472fSOliver Neukum } 192869ee472fSOliver Neukum 192969ee472fSOliver Neukum smp_mb(); 193069ee472fSOliver Neukum clear_bit(EVENT_DEV_ASLEEP, &dev->flags); 193169ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 193275bd0cbdSMing Lei 193375bd0cbdSMing Lei if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { 193414a0d635SOliver Neukum /* handle remote wakeup ASAP 193514a0d635SOliver Neukum * we cannot race against stop 193614a0d635SOliver Neukum */ 193714a0d635SOliver Neukum if (netif_device_present(dev->net) && 193865841fd5SMing Lei !timer_pending(&dev->delay) && 193965841fd5SMing Lei !test_bit(EVENT_RX_HALT, &dev->flags)) 1940ab6f148dSOliver Neukum rx_alloc_submit(dev, GFP_NOIO); 194165841fd5SMing Lei 194269ee472fSOliver Neukum if (!(dev->txq.qlen >= TX_QLEN(dev))) 19431aa9bc5bSAlexey Orishko netif_tx_wake_all_queues(dev->net); 19445b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 194569ee472fSOliver Neukum } 194675bd0cbdSMing Lei } 19475d9d01a3SOliver Neukum 19485d9d01a3SOliver Neukum if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) 19495d9d01a3SOliver Neukum usb_autopm_get_interface_no_resume(intf); 19505d9d01a3SOliver Neukum 19515b2fc499SJeff Garzik return 0; 19525b2fc499SJeff Garzik } 19535b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume); 19545b2fc499SJeff Garzik 19555d9d01a3SOliver Neukum /* 19565d9d01a3SOliver Neukum * Either a subdriver implements manage_power, then it is assumed to always 19575d9d01a3SOliver Neukum * be ready to be suspended or it reports the readiness to be suspended 19585d9d01a3SOliver Neukum * explicitly 19595d9d01a3SOliver Neukum */ 19605d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev) 19615d9d01a3SOliver Neukum { 19625d9d01a3SOliver Neukum if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) { 19635d9d01a3SOliver Neukum dev->intf->needs_remote_wakeup = 1; 19645d9d01a3SOliver Neukum usb_autopm_put_interface_async(dev->intf); 19655d9d01a3SOliver Neukum } 19665d9d01a3SOliver Neukum } 19675d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle); 19685b2fc499SJeff Garzik 19692dd7c8cfSOliver Neukum /* 19702dd7c8cfSOliver Neukum * For devices that can do without special commands 19712dd7c8cfSOliver Neukum */ 19722dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on) 19732dd7c8cfSOliver Neukum { 19742dd7c8cfSOliver Neukum dev->intf->needs_remote_wakeup = on; 19752dd7c8cfSOliver Neukum return 0; 19762dd7c8cfSOliver Neukum } 19772dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power); 19782dd7c8cfSOliver Neukum 1979ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset) 1980ac64995dSMing Lei { 1981ac64995dSMing Lei /* update link after link is reseted */ 1982ac64995dSMing Lei if (link && !need_reset) 1983ac64995dSMing Lei netif_carrier_on(dev->net); 1984ac64995dSMing Lei else 1985ac64995dSMing Lei netif_carrier_off(dev->net); 1986ac64995dSMing Lei 1987ac64995dSMing Lei if (need_reset && link) 1988ac64995dSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_RESET); 19894b49f58fSMing Lei else 19904b49f58fSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_CHANGE); 1991ac64995dSMing Lei } 1992ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change); 1993ac64995dSMing Lei 19945b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 19950547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 1996877bd862SMing Lei u16 value, u16 index, void *data, u16 size) 1997877bd862SMing Lei { 1998877bd862SMing Lei void *buf = NULL; 1999877bd862SMing Lei int err = -ENOMEM; 2000877bd862SMing Lei 2001877bd862SMing Lei netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x" 2002877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 2003877bd862SMing Lei cmd, reqtype, value, index, size); 2004877bd862SMing Lei 20056c22fce0SOliver Neukum if (size) { 2006877bd862SMing Lei buf = kmalloc(size, GFP_KERNEL); 2007877bd862SMing Lei if (!buf) 2008877bd862SMing Lei goto out; 2009877bd862SMing Lei } 2010877bd862SMing Lei 2011877bd862SMing Lei err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 2012877bd862SMing Lei cmd, reqtype, value, index, buf, size, 2013877bd862SMing Lei USB_CTRL_GET_TIMEOUT); 20146c22fce0SOliver Neukum if (err > 0 && err <= size) { 20156c22fce0SOliver Neukum if (data) 2016877bd862SMing Lei memcpy(data, buf, err); 20176c22fce0SOliver Neukum else 20186c22fce0SOliver Neukum netdev_dbg(dev->net, 20196c22fce0SOliver Neukum "Huh? Data requested but thrown away.\n"); 20206c22fce0SOliver Neukum } 2021877bd862SMing Lei kfree(buf); 2022877bd862SMing Lei out: 2023877bd862SMing Lei return err; 2024877bd862SMing Lei } 2025877bd862SMing Lei 20260547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 20270547fad2SMing Lei u16 value, u16 index, const void *data, 20280547fad2SMing Lei u16 size) 2029877bd862SMing Lei { 2030877bd862SMing Lei void *buf = NULL; 2031877bd862SMing Lei int err = -ENOMEM; 2032877bd862SMing Lei 2033877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 2034877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 2035877bd862SMing Lei cmd, reqtype, value, index, size); 2036877bd862SMing Lei 2037877bd862SMing Lei if (data) { 2038877bd862SMing Lei buf = kmemdup(data, size, GFP_KERNEL); 2039877bd862SMing Lei if (!buf) 2040877bd862SMing Lei goto out; 20416c22fce0SOliver Neukum } else { 20426c22fce0SOliver Neukum if (size) { 20436c22fce0SOliver Neukum WARN_ON_ONCE(1); 20446c22fce0SOliver Neukum err = -EINVAL; 20456c22fce0SOliver Neukum goto out; 20466c22fce0SOliver Neukum } 2047877bd862SMing Lei } 2048877bd862SMing Lei 2049877bd862SMing Lei err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 2050877bd862SMing Lei cmd, reqtype, value, index, buf, size, 2051877bd862SMing Lei USB_CTRL_SET_TIMEOUT); 2052877bd862SMing Lei kfree(buf); 2053877bd862SMing Lei 2054877bd862SMing Lei out: 2055877bd862SMing Lei return err; 2056877bd862SMing Lei } 20570547fad2SMing Lei 20580547fad2SMing Lei /* 20590547fad2SMing Lei * The function can't be called inside suspend/resume callback, 20600547fad2SMing Lei * otherwise deadlock will be caused. 20610547fad2SMing Lei */ 20620547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 20630547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 20640547fad2SMing Lei { 20656f0a0986SMing Lei int ret; 20666f0a0986SMing Lei 20676f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 20686f0a0986SMing Lei return -ENODEV; 20696f0a0986SMing Lei ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index, 20700547fad2SMing Lei data, size); 20716f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 20726f0a0986SMing Lei return ret; 20730547fad2SMing Lei } 20740547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd); 20750547fad2SMing Lei 20760547fad2SMing Lei /* 20770547fad2SMing Lei * The function can't be called inside suspend/resume callback, 20780547fad2SMing Lei * otherwise deadlock will be caused. 20790547fad2SMing Lei */ 20800547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 20810547fad2SMing Lei u16 value, u16 index, const void *data, u16 size) 20820547fad2SMing Lei { 20836f0a0986SMing Lei int ret; 20846f0a0986SMing Lei 20856f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 20866f0a0986SMing Lei return -ENODEV; 20876f0a0986SMing Lei ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index, 20880547fad2SMing Lei data, size); 20896f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 20906f0a0986SMing Lei return ret; 20910547fad2SMing Lei } 2092877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd); 2093877bd862SMing Lei 20940547fad2SMing Lei /* 20950547fad2SMing Lei * The function can be called inside suspend/resume callback safely 20960547fad2SMing Lei * and should only be called by suspend/resume callback generally. 20970547fad2SMing Lei */ 20980547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 20990547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 21000547fad2SMing Lei { 21010547fad2SMing Lei return __usbnet_read_cmd(dev, cmd, reqtype, value, index, 21020547fad2SMing Lei data, size); 21030547fad2SMing Lei } 21040547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm); 21050547fad2SMing Lei 21060547fad2SMing Lei /* 21070547fad2SMing Lei * The function can be called inside suspend/resume callback safely 21080547fad2SMing Lei * and should only be called by suspend/resume callback generally. 21090547fad2SMing Lei */ 21100547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 21110547fad2SMing Lei u16 value, u16 index, const void *data, 21120547fad2SMing Lei u16 size) 21130547fad2SMing Lei { 21140547fad2SMing Lei return __usbnet_write_cmd(dev, cmd, reqtype, value, index, 21150547fad2SMing Lei data, size); 21160547fad2SMing Lei } 21170547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm); 21180547fad2SMing Lei 2119877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb) 2120877bd862SMing Lei { 2121877bd862SMing Lei struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; 2122877bd862SMing Lei int status = urb->status; 2123877bd862SMing Lei 2124877bd862SMing Lei if (status < 0) 2125877bd862SMing Lei dev_dbg(&urb->dev->dev, "%s failed with %d", 2126877bd862SMing Lei __func__, status); 2127877bd862SMing Lei 2128877bd862SMing Lei kfree(req); 2129877bd862SMing Lei usb_free_urb(urb); 2130877bd862SMing Lei } 2131877bd862SMing Lei 21320547fad2SMing Lei /* 21330547fad2SMing Lei * The caller must make sure that device can't be put into suspend 21340547fad2SMing Lei * state until the control URB completes. 21350547fad2SMing Lei */ 2136877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype, 2137877bd862SMing Lei u16 value, u16 index, const void *data, u16 size) 2138877bd862SMing Lei { 2139877bd862SMing Lei struct usb_ctrlrequest *req = NULL; 2140877bd862SMing Lei struct urb *urb; 2141877bd862SMing Lei int err = -ENOMEM; 2142877bd862SMing Lei void *buf = NULL; 2143877bd862SMing Lei 2144877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 2145877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 2146877bd862SMing Lei cmd, reqtype, value, index, size); 2147877bd862SMing Lei 2148877bd862SMing Lei urb = usb_alloc_urb(0, GFP_ATOMIC); 2149ef6cd130SWolfram Sang if (!urb) 2150877bd862SMing Lei goto fail; 2151877bd862SMing Lei 2152877bd862SMing Lei if (data) { 2153877bd862SMing Lei buf = kmemdup(data, size, GFP_ATOMIC); 2154877bd862SMing Lei if (!buf) { 2155877bd862SMing Lei netdev_err(dev->net, "Error allocating buffer" 2156877bd862SMing Lei " in %s!\n", __func__); 2157877bd862SMing Lei goto fail_free; 2158877bd862SMing Lei } 2159877bd862SMing Lei } 2160877bd862SMing Lei 2161877bd862SMing Lei req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); 216238673c82SJoe Perches if (!req) 2163877bd862SMing Lei goto fail_free_buf; 2164877bd862SMing Lei 2165877bd862SMing Lei req->bRequestType = reqtype; 2166877bd862SMing Lei req->bRequest = cmd; 2167877bd862SMing Lei req->wValue = cpu_to_le16(value); 2168877bd862SMing Lei req->wIndex = cpu_to_le16(index); 2169877bd862SMing Lei req->wLength = cpu_to_le16(size); 2170877bd862SMing Lei 2171877bd862SMing Lei usb_fill_control_urb(urb, dev->udev, 2172877bd862SMing Lei usb_sndctrlpipe(dev->udev, 0), 2173877bd862SMing Lei (void *)req, buf, size, 2174877bd862SMing Lei usbnet_async_cmd_cb, req); 2175877bd862SMing Lei urb->transfer_flags |= URB_FREE_BUFFER; 2176877bd862SMing Lei 2177877bd862SMing Lei err = usb_submit_urb(urb, GFP_ATOMIC); 2178877bd862SMing Lei if (err < 0) { 2179877bd862SMing Lei netdev_err(dev->net, "Error submitting the control" 2180877bd862SMing Lei " message: status=%d\n", err); 2181877bd862SMing Lei goto fail_free; 2182877bd862SMing Lei } 2183877bd862SMing Lei return 0; 2184877bd862SMing Lei 2185877bd862SMing Lei fail_free_buf: 2186877bd862SMing Lei kfree(buf); 2187877bd862SMing Lei fail_free: 2188877bd862SMing Lei kfree(req); 2189877bd862SMing Lei usb_free_urb(urb); 2190877bd862SMing Lei fail: 2191877bd862SMing Lei return err; 2192877bd862SMing Lei 2193877bd862SMing Lei } 2194877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async); 2195877bd862SMing Lei /*-------------------------------------------------------------------------*/ 21965b2fc499SJeff Garzik 21975b2fc499SJeff Garzik static int __init usbnet_init(void) 21985b2fc499SJeff Garzik { 2199c582a950SThiago Farina /* Compiler should optimize this out. */ 2200c582a950SThiago Farina BUILD_BUG_ON( 2201c593642cSPankaj Bharadiya sizeof_field(struct sk_buff, cb) < sizeof(struct skb_data)); 22025b2fc499SJeff Garzik 2203c7e12eadSJoe Perches eth_random_addr(node_id); 22045b2fc499SJeff Garzik return 0; 22055b2fc499SJeff Garzik } 22065b2fc499SJeff Garzik module_init(usbnet_init); 22075b2fc499SJeff Garzik 22085b2fc499SJeff Garzik static void __exit usbnet_exit(void) 22095b2fc499SJeff Garzik { 22105b2fc499SJeff Garzik } 22115b2fc499SJeff Garzik module_exit(usbnet_exit); 22125b2fc499SJeff Garzik 22135b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell"); 22145b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework"); 22155b2fc499SJeff Garzik MODULE_LICENSE("GPL"); 2216