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 #include <linux/module.h>
215b2fc499SJeff Garzik #include <linux/init.h>
225b2fc499SJeff Garzik #include <linux/netdevice.h>
235b2fc499SJeff Garzik #include <linux/etherdevice.h>
2403ad032bSPeter Holik #include <linux/ctype.h>
255b2fc499SJeff Garzik #include <linux/ethtool.h>
265b2fc499SJeff Garzik #include <linux/workqueue.h>
275b2fc499SJeff Garzik #include <linux/mii.h>
285b2fc499SJeff Garzik #include <linux/usb.h>
293692e94fSJussi Kivilinna #include <linux/usb/usbnet.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
31fd1f170dSAndy Shevchenko #include <linux/kernel.h>
32b0786b43SMing Lei #include <linux/pm_runtime.h>
335b2fc499SJeff Garzik
345b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
355b2fc499SJeff Garzik
365b2fc499SJeff Garzik /*
375b2fc499SJeff Garzik * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
385b2fc499SJeff Garzik * Several dozen bytes of IPv4 data can fit in two such transactions.
395b2fc499SJeff Garzik * One maximum size Ethernet packet takes twenty four of them.
405b2fc499SJeff Garzik * For high speed, each frame comfortably fits almost 36 max size
415b2fc499SJeff Garzik * Ethernet packets (so queues should be bigger).
425b2fc499SJeff Garzik *
43a88c32aeSMing Lei * The goal is to let the USB host controller be busy for 5msec or
44a88c32aeSMing Lei * more before an irq is required, under load. Jumbograms change
45a88c32aeSMing Lei * the equation.
465b2fc499SJeff Garzik */
47a88c32aeSMing Lei #define MAX_QUEUE_MEMORY (60 * 1518)
48a88c32aeSMing Lei #define RX_QLEN(dev) ((dev)->rx_qlen)
49a88c32aeSMing Lei #define TX_QLEN(dev) ((dev)->tx_qlen)
505b2fc499SJeff Garzik
515b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks
525b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES (5*HZ)
535b2fc499SJeff Garzik
5437ebb549SPetr Mladek /* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
5537ebb549SPetr Mladek * us (it polls at HZ/4 usually) before we report too many false errors.
5637ebb549SPetr Mladek */
575b2fc499SJeff Garzik #define THROTTLE_JIFFIES (HZ/8)
585b2fc499SJeff Garzik
595b2fc499SJeff Garzik // between wakeups
605b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS 3
615b2fc499SJeff Garzik
625b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
635b2fc499SJeff Garzik
645b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
655b2fc499SJeff Garzik static int msg_level = -1;
665b2fc499SJeff Garzik module_param (msg_level, int, 0);
675b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
685b2fc499SJeff Garzik
695b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
705b2fc499SJeff Garzik
7147889068SYajun Deng static const char * const usbnet_event_names[] = {
7247889068SYajun Deng [EVENT_TX_HALT] = "EVENT_TX_HALT",
7347889068SYajun Deng [EVENT_RX_HALT] = "EVENT_RX_HALT",
7447889068SYajun Deng [EVENT_RX_MEMORY] = "EVENT_RX_MEMORY",
7547889068SYajun Deng [EVENT_STS_SPLIT] = "EVENT_STS_SPLIT",
7647889068SYajun Deng [EVENT_LINK_RESET] = "EVENT_LINK_RESET",
7747889068SYajun Deng [EVENT_RX_PAUSED] = "EVENT_RX_PAUSED",
7847889068SYajun Deng [EVENT_DEV_ASLEEP] = "EVENT_DEV_ASLEEP",
7947889068SYajun Deng [EVENT_DEV_OPEN] = "EVENT_DEV_OPEN",
8047889068SYajun Deng [EVENT_DEVICE_REPORT_IDLE] = "EVENT_DEVICE_REPORT_IDLE",
8147889068SYajun Deng [EVENT_NO_RUNTIME_PM] = "EVENT_NO_RUNTIME_PM",
8247889068SYajun Deng [EVENT_RX_KILL] = "EVENT_RX_KILL",
8347889068SYajun Deng [EVENT_LINK_CHANGE] = "EVENT_LINK_CHANGE",
8447889068SYajun Deng [EVENT_SET_RX_MODE] = "EVENT_SET_RX_MODE",
8547889068SYajun Deng [EVENT_NO_IP_ALIGN] = "EVENT_NO_IP_ALIGN",
8647889068SYajun Deng };
8747889068SYajun Deng
885b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */
usbnet_get_endpoints(struct usbnet * dev,struct usb_interface * intf)895b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
905b2fc499SJeff Garzik {
915b2fc499SJeff Garzik int tmp;
925b2fc499SJeff Garzik struct usb_host_interface *alt = NULL;
935b2fc499SJeff Garzik struct usb_host_endpoint *in = NULL, *out = NULL;
945b2fc499SJeff Garzik struct usb_host_endpoint *status = NULL;
955b2fc499SJeff Garzik
965b2fc499SJeff Garzik for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
975b2fc499SJeff Garzik unsigned ep;
985b2fc499SJeff Garzik
995b2fc499SJeff Garzik in = out = status = NULL;
1005b2fc499SJeff Garzik alt = intf->altsetting + tmp;
1015b2fc499SJeff Garzik
1025b2fc499SJeff Garzik /* take the first altsetting with in-bulk + out-bulk;
1035b2fc499SJeff Garzik * remember any status endpoint, just in case;
10470f23fd6SJustin P. Mattock * ignore other endpoints and altsettings.
1055b2fc499SJeff Garzik */
1065b2fc499SJeff Garzik for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
1075b2fc499SJeff Garzik struct usb_host_endpoint *e;
1085b2fc499SJeff Garzik int intr = 0;
1095b2fc499SJeff Garzik
1105b2fc499SJeff Garzik e = alt->endpoint + ep;
1118d3d7c20SBjørn Mork
1128d3d7c20SBjørn Mork /* ignore endpoints which cannot transfer data */
1138d3d7c20SBjørn Mork if (!usb_endpoint_maxp(&e->desc))
1148d3d7c20SBjørn Mork continue;
1158d3d7c20SBjørn Mork
1165b2fc499SJeff Garzik switch (e->desc.bmAttributes) {
1175b2fc499SJeff Garzik case USB_ENDPOINT_XFER_INT:
1185b2fc499SJeff Garzik if (!usb_endpoint_dir_in(&e->desc))
1195b2fc499SJeff Garzik continue;
1205b2fc499SJeff Garzik intr = 1;
121df561f66SGustavo A. R. Silva fallthrough;
1225b2fc499SJeff Garzik case USB_ENDPOINT_XFER_BULK:
1235b2fc499SJeff Garzik break;
1245b2fc499SJeff Garzik default:
1255b2fc499SJeff Garzik continue;
1265b2fc499SJeff Garzik }
1275b2fc499SJeff Garzik if (usb_endpoint_dir_in(&e->desc)) {
1285b2fc499SJeff Garzik if (!intr && !in)
1295b2fc499SJeff Garzik in = e;
1305b2fc499SJeff Garzik else if (intr && !status)
1315b2fc499SJeff Garzik status = e;
1325b2fc499SJeff Garzik } else {
1335b2fc499SJeff Garzik if (!out)
1345b2fc499SJeff Garzik out = e;
1355b2fc499SJeff Garzik }
1365b2fc499SJeff Garzik }
1375b2fc499SJeff Garzik if (in && out)
1385b2fc499SJeff Garzik break;
1395b2fc499SJeff Garzik }
1405b2fc499SJeff Garzik if (!alt || !in || !out)
1415b2fc499SJeff Garzik return -EINVAL;
1425b2fc499SJeff Garzik
1438e95a202SJoe Perches if (alt->desc.bAlternateSetting != 0 ||
1448e95a202SJoe Perches !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1455b2fc499SJeff Garzik tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
1465b2fc499SJeff Garzik alt->desc.bAlternateSetting);
1475b2fc499SJeff Garzik if (tmp < 0)
1485b2fc499SJeff Garzik return tmp;
1495b2fc499SJeff Garzik }
1505b2fc499SJeff Garzik
1515b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (dev->udev,
1525b2fc499SJeff Garzik in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1535b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (dev->udev,
1545b2fc499SJeff Garzik out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
1555b2fc499SJeff Garzik dev->status = status;
1565b2fc499SJeff Garzik return 0;
1575b2fc499SJeff Garzik }
1585b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1595b2fc499SJeff Garzik
usbnet_get_ethernet_addr(struct usbnet * dev,int iMACAddress)16003ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
16103ad032bSPeter Holik {
1622674e7eaSJakub Kicinski u8 addr[ETH_ALEN];
16351487ae7SAndy Shevchenko int tmp = -1, ret;
16403ad032bSPeter Holik unsigned char buf [13];
16503ad032bSPeter Holik
16651487ae7SAndy Shevchenko ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
16751487ae7SAndy Shevchenko if (ret == 12)
1682674e7eaSJakub Kicinski tmp = hex2bin(addr, buf, 6);
16951487ae7SAndy Shevchenko if (tmp < 0) {
17003ad032bSPeter Holik dev_dbg(&dev->udev->dev,
17103ad032bSPeter Holik "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
17251487ae7SAndy Shevchenko if (ret >= 0)
17351487ae7SAndy Shevchenko ret = -EINVAL;
17451487ae7SAndy Shevchenko return ret;
17503ad032bSPeter Holik }
1762674e7eaSJakub Kicinski eth_hw_addr_set(dev->net, addr);
17703ad032bSPeter Holik return 0;
17803ad032bSPeter Holik }
17903ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
18003ad032bSPeter Holik
intr_complete(struct urb * urb)18124ead299Stom.leiming@gmail.com static void intr_complete (struct urb *urb)
18224ead299Stom.leiming@gmail.com {
18324ead299Stom.leiming@gmail.com struct usbnet *dev = urb->context;
18424ead299Stom.leiming@gmail.com int status = urb->status;
18524ead299Stom.leiming@gmail.com
18624ead299Stom.leiming@gmail.com switch (status) {
18724ead299Stom.leiming@gmail.com /* success */
18824ead299Stom.leiming@gmail.com case 0:
18924ead299Stom.leiming@gmail.com dev->driver_info->status(dev, urb);
19024ead299Stom.leiming@gmail.com break;
19124ead299Stom.leiming@gmail.com
19224ead299Stom.leiming@gmail.com /* software-driven interface shutdown */
19324ead299Stom.leiming@gmail.com case -ENOENT: /* urb killed */
19424ead299Stom.leiming@gmail.com case -ESHUTDOWN: /* hardware gone */
19524ead299Stom.leiming@gmail.com netif_dbg(dev, ifdown, dev->net,
19624ead299Stom.leiming@gmail.com "intr shutdown, code %d\n", status);
19724ead299Stom.leiming@gmail.com return;
19824ead299Stom.leiming@gmail.com
19924ead299Stom.leiming@gmail.com /* NOTE: not throttling like RX/TX, since this endpoint
20024ead299Stom.leiming@gmail.com * already polls infrequently
20124ead299Stom.leiming@gmail.com */
20224ead299Stom.leiming@gmail.com default:
20324ead299Stom.leiming@gmail.com netdev_dbg(dev->net, "intr status %d\n", status);
20424ead299Stom.leiming@gmail.com break;
20524ead299Stom.leiming@gmail.com }
20624ead299Stom.leiming@gmail.com
20724ead299Stom.leiming@gmail.com status = usb_submit_urb (urb, GFP_ATOMIC);
20824ead299Stom.leiming@gmail.com if (status != 0)
20924ead299Stom.leiming@gmail.com netif_err(dev, timer, dev->net,
21024ead299Stom.leiming@gmail.com "intr resubmit --> %d\n", status);
21124ead299Stom.leiming@gmail.com }
2125b2fc499SJeff Garzik
init_status(struct usbnet * dev,struct usb_interface * intf)2135b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf)
2145b2fc499SJeff Garzik {
2155b2fc499SJeff Garzik char *buf = NULL;
2165b2fc499SJeff Garzik unsigned pipe = 0;
2175b2fc499SJeff Garzik unsigned maxp;
2185b2fc499SJeff Garzik unsigned period;
2195b2fc499SJeff Garzik
2205b2fc499SJeff Garzik if (!dev->driver_info->status)
2215b2fc499SJeff Garzik return 0;
2225b2fc499SJeff Garzik
2235b2fc499SJeff Garzik pipe = usb_rcvintpipe (dev->udev,
2245b2fc499SJeff Garzik dev->status->desc.bEndpointAddress
2255b2fc499SJeff Garzik & USB_ENDPOINT_NUMBER_MASK);
226e13adbfaSVincent Mailhol maxp = usb_maxpacket(dev->udev, pipe);
2275b2fc499SJeff Garzik
2285b2fc499SJeff Garzik /* avoid 1 msec chatter: min 8 msec poll rate */
2295b2fc499SJeff Garzik period = max ((int) dev->status->desc.bInterval,
2305b2fc499SJeff Garzik (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
2315b2fc499SJeff Garzik
2325b2fc499SJeff Garzik buf = kmalloc (maxp, GFP_KERNEL);
2335b2fc499SJeff Garzik if (buf) {
2345b2fc499SJeff Garzik dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
2355b2fc499SJeff Garzik if (!dev->interrupt) {
2365b2fc499SJeff Garzik kfree (buf);
2375b2fc499SJeff Garzik return -ENOMEM;
2385b2fc499SJeff Garzik } else {
2395b2fc499SJeff Garzik usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
2405b2fc499SJeff Garzik buf, maxp, intr_complete, dev, period);
241720f3d7cStom.leiming@gmail.com dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
2425b2fc499SJeff Garzik dev_dbg(&intf->dev,
2435b2fc499SJeff Garzik "status ep%din, %d bytes period %d\n",
2445b2fc499SJeff Garzik usb_pipeendpoint(pipe), maxp, period);
2455b2fc499SJeff Garzik }
2465b2fc499SJeff Garzik }
2475b2fc499SJeff Garzik return 0;
2485b2fc499SJeff Garzik }
2495b2fc499SJeff Garzik
2506eecdc5fSDan Williams /* Submit the interrupt URB if not previously submitted, increasing refcount */
usbnet_status_start(struct usbnet * dev,gfp_t mem_flags)2516eecdc5fSDan Williams int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
2526eecdc5fSDan Williams {
2536eecdc5fSDan Williams int ret = 0;
2546eecdc5fSDan Williams
2556eecdc5fSDan Williams WARN_ON_ONCE(dev->interrupt == NULL);
2566eecdc5fSDan Williams if (dev->interrupt) {
2576eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex);
2586eecdc5fSDan Williams
2596eecdc5fSDan Williams if (++dev->interrupt_count == 1)
2606eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags);
2616eecdc5fSDan Williams
2626eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
2636eecdc5fSDan Williams dev->interrupt_count);
2646eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex);
2656eecdc5fSDan Williams }
2666eecdc5fSDan Williams return ret;
2676eecdc5fSDan Williams }
2686eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_start);
2696eecdc5fSDan Williams
2706eecdc5fSDan Williams /* For resume; submit interrupt URB if previously submitted */
__usbnet_status_start_force(struct usbnet * dev,gfp_t mem_flags)2716eecdc5fSDan Williams static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
2726eecdc5fSDan Williams {
2736eecdc5fSDan Williams int ret = 0;
2746eecdc5fSDan Williams
2756eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex);
2766eecdc5fSDan Williams if (dev->interrupt_count) {
2776eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags);
2786eecdc5fSDan Williams dev_dbg(&dev->udev->dev,
2796eecdc5fSDan Williams "submitted interrupt URB for resume\n");
2806eecdc5fSDan Williams }
2816eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex);
2826eecdc5fSDan Williams return ret;
2836eecdc5fSDan Williams }
2846eecdc5fSDan Williams
2856eecdc5fSDan Williams /* Kill the interrupt URB if all submitters want it killed */
usbnet_status_stop(struct usbnet * dev)2866eecdc5fSDan Williams void usbnet_status_stop(struct usbnet *dev)
2876eecdc5fSDan Williams {
2886eecdc5fSDan Williams if (dev->interrupt) {
2896eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex);
2906eecdc5fSDan Williams WARN_ON(dev->interrupt_count == 0);
2916eecdc5fSDan Williams
2926eecdc5fSDan Williams if (dev->interrupt_count && --dev->interrupt_count == 0)
2936eecdc5fSDan Williams usb_kill_urb(dev->interrupt);
2946eecdc5fSDan Williams
2956eecdc5fSDan Williams dev_dbg(&dev->udev->dev,
2966eecdc5fSDan Williams "decremented interrupt URB count to %d\n",
2976eecdc5fSDan Williams dev->interrupt_count);
2986eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex);
2996eecdc5fSDan Williams }
3006eecdc5fSDan Williams }
3016eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_stop);
3026eecdc5fSDan Williams
3036eecdc5fSDan Williams /* For suspend; always kill interrupt URB */
__usbnet_status_stop_force(struct usbnet * dev)3046eecdc5fSDan Williams static void __usbnet_status_stop_force(struct usbnet *dev)
3056eecdc5fSDan Williams {
3066eecdc5fSDan Williams if (dev->interrupt) {
3076eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex);
3086eecdc5fSDan Williams usb_kill_urb(dev->interrupt);
3096eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
3106eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex);
3116eecdc5fSDan Williams }
3126eecdc5fSDan Williams }
3136eecdc5fSDan Williams
3145b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting.
3155b2fc499SJeff Garzik * Some link protocols batch packets, so their rx_fixup paths
3165b2fc499SJeff Garzik * can return clones as well as just modify the original skb.
3175b2fc499SJeff Garzik */
usbnet_skb_return(struct usbnet * dev,struct sk_buff * skb)3185b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
3195b2fc499SJeff Garzik {
320af0c351cSHeiner Kallweit struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats);
3212695578bSEric Dumazet unsigned long flags;
3225b2fc499SJeff Garzik int status;
3235b2fc499SJeff Garzik
3247834ddbcSJussi Kivilinna if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
3257834ddbcSJussi Kivilinna skb_queue_tail(&dev->rxq_pause, skb);
3267834ddbcSJussi Kivilinna return;
3277834ddbcSJussi Kivilinna }
3287834ddbcSJussi Kivilinna
32981e0ce79SBjørn Mork /* only update if unset to allow minidriver rx_fixup override */
33081e0ce79SBjørn Mork if (skb->protocol == 0)
3315b2fc499SJeff Garzik skb->protocol = eth_type_trans (skb, dev->net);
33281e0ce79SBjørn Mork
3332695578bSEric Dumazet flags = u64_stats_update_begin_irqsave(&stats64->syncp);
3349962acefSEric Dumazet u64_stats_inc(&stats64->rx_packets);
3359962acefSEric Dumazet u64_stats_add(&stats64->rx_bytes, skb->len);
3362695578bSEric Dumazet u64_stats_update_end_irqrestore(&stats64->syncp, flags);
3375b2fc499SJeff Garzik
338a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
3395b2fc499SJeff Garzik skb->len + sizeof (struct ethhdr), skb->protocol);
3405b2fc499SJeff Garzik memset (skb->cb, 0, sizeof (struct skb_data));
341f9b491ecSMichael Riesch
342f9b491ecSMichael Riesch if (skb_defer_rx_timestamp(skb))
343f9b491ecSMichael Riesch return;
344f9b491ecSMichael Riesch
3455b2fc499SJeff Garzik status = netif_rx (skb);
346a475f603SJoe Perches if (status != NET_RX_SUCCESS)
347a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net,
348a475f603SJoe Perches "netif_rx status %d\n", status);
3495b2fc499SJeff Garzik }
3505b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return);
3515b2fc499SJeff Garzik
352a88c32aeSMing Lei /* must be called if hard_mtu or rx_urb_size changed */
usbnet_update_max_qlen(struct usbnet * dev)353a88c32aeSMing Lei void usbnet_update_max_qlen(struct usbnet *dev)
354a88c32aeSMing Lei {
355a88c32aeSMing Lei enum usb_device_speed speed = dev->udev->speed;
356a88c32aeSMing Lei
357280ceaedSOliver Neukum if (!dev->rx_urb_size || !dev->hard_mtu)
358280ceaedSOliver Neukum goto insanity;
359a88c32aeSMing Lei switch (speed) {
360a88c32aeSMing Lei case USB_SPEED_HIGH:
361a88c32aeSMing Lei dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
362a88c32aeSMing Lei dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
363a88c32aeSMing Lei break;
364452c447aSMing Lei case USB_SPEED_SUPER:
365ea079842SOliver Neukum case USB_SPEED_SUPER_PLUS:
366452c447aSMing Lei /*
367452c447aSMing Lei * Not take default 5ms qlen for super speed HC to
368452c447aSMing Lei * save memory, and iperf tests show 2.5ms qlen can
369452c447aSMing Lei * work well
370452c447aSMing Lei */
371452c447aSMing Lei dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
372452c447aSMing Lei dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
373452c447aSMing Lei break;
374a88c32aeSMing Lei default:
375280ceaedSOliver Neukum insanity:
376a88c32aeSMing Lei dev->rx_qlen = dev->tx_qlen = 4;
377a88c32aeSMing Lei }
378a88c32aeSMing Lei }
379a88c32aeSMing Lei EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
380a88c32aeSMing Lei
381efe3e6b5SXie Shaowen
3825b2fc499SJeff Garzik /*-------------------------------------------------------------------------
3835b2fc499SJeff Garzik *
3845b2fc499SJeff Garzik * Network Device Driver (peer link to "Host Device", from USB host)
3855b2fc499SJeff Garzik *
3865b2fc499SJeff Garzik *-------------------------------------------------------------------------*/
3875b2fc499SJeff Garzik
usbnet_change_mtu(struct net_device * net,int new_mtu)388777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu)
3895b2fc499SJeff Garzik {
3905b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
3915b2fc499SJeff Garzik int ll_mtu = new_mtu + net->hard_header_len;
3925b2fc499SJeff Garzik int old_hard_mtu = dev->hard_mtu;
3935b2fc499SJeff Garzik int old_rx_urb_size = dev->rx_urb_size;
3945b2fc499SJeff Garzik
3955b2fc499SJeff Garzik // no second zero-length packet read wanted after mtu-sized packets
3965b2fc499SJeff Garzik if ((ll_mtu % dev->maxpacket) == 0)
3975b2fc499SJeff Garzik return -EDOM;
3985b2fc499SJeff Garzik net->mtu = new_mtu;
3995b2fc499SJeff Garzik
4005b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len;
4015b2fc499SJeff Garzik if (dev->rx_urb_size == old_hard_mtu) {
4025b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu;
40343daa96bSSoohoon Lee if (dev->rx_urb_size > old_rx_urb_size) {
40443daa96bSSoohoon Lee usbnet_pause_rx(dev);
4055b2fc499SJeff Garzik usbnet_unlink_rx_urbs(dev);
40643daa96bSSoohoon Lee usbnet_resume_rx(dev);
40743daa96bSSoohoon Lee }
4085b2fc499SJeff Garzik }
4095b2fc499SJeff Garzik
410a88c32aeSMing Lei /* max qlen depend on hard_mtu and rx_urb_size */
411a88c32aeSMing Lei usbnet_update_max_qlen(dev);
412a88c32aeSMing Lei
4135b2fc499SJeff Garzik return 0;
4145b2fc499SJeff Garzik }
415777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu);
4165b2fc499SJeff Garzik
4175b6e9bcdSMing Lei /* The caller must hold list->lock */
__usbnet_queue_skb(struct sk_buff_head * list,struct sk_buff * newsk,enum skb_state state)4185b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list,
4195b6e9bcdSMing Lei struct sk_buff *newsk, enum skb_state state)
4205b6e9bcdSMing Lei {
4215b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) newsk->cb;
4225b6e9bcdSMing Lei
4235b6e9bcdSMing Lei __skb_queue_tail(list, newsk);
4245b6e9bcdSMing Lei entry->state = state;
4255b6e9bcdSMing Lei }
4265b6e9bcdSMing Lei
4275b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4285b2fc499SJeff Garzik
4295b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
4305b2fc499SJeff Garzik * completion callbacks. 2.5 should have fixed those bugs...
4315b2fc499SJeff Garzik */
4325b2fc499SJeff Garzik
defer_bh(struct usbnet * dev,struct sk_buff * skb,struct sk_buff_head * list,enum skb_state state)4335b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
4345b6e9bcdSMing Lei struct sk_buff_head *list, enum skb_state state)
4355b2fc499SJeff Garzik {
4365b2fc499SJeff Garzik unsigned long flags;
4375b6e9bcdSMing Lei enum skb_state old_state;
4385b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) skb->cb;
4395b2fc499SJeff Garzik
4405b2fc499SJeff Garzik spin_lock_irqsave(&list->lock, flags);
4415b6e9bcdSMing Lei old_state = entry->state;
4425b6e9bcdSMing Lei entry->state = state;
4435b2fc499SJeff Garzik __skb_unlink(skb, list);
444fcb0bb6aSEugene Shatokhin
445fcb0bb6aSEugene Shatokhin /* defer_bh() is never called with list == &dev->done.
446fcb0bb6aSEugene Shatokhin * spin_lock_nested() tells lockdep that it is OK to take
447fcb0bb6aSEugene Shatokhin * dev->done.lock here with list->lock held.
448fcb0bb6aSEugene Shatokhin */
449fcb0bb6aSEugene Shatokhin spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
450fcb0bb6aSEugene Shatokhin
4515b2fc499SJeff Garzik __skb_queue_tail(&dev->done, skb);
4525b2fc499SJeff Garzik if (dev->done.qlen == 1)
4535b2fc499SJeff Garzik tasklet_schedule(&dev->bh);
454fcb0bb6aSEugene Shatokhin spin_unlock(&dev->done.lock);
455fcb0bb6aSEugene Shatokhin spin_unlock_irqrestore(&list->lock, flags);
4565b6e9bcdSMing Lei return old_state;
4575b2fc499SJeff Garzik }
4585b2fc499SJeff Garzik
4595b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd
4605b2fc499SJeff Garzik *
4615b2fc499SJeff Garzik * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
4625b2fc499SJeff Garzik * but tasklet_schedule() doesn't. hope the failure is rare.
4635b2fc499SJeff Garzik */
usbnet_defer_kevent(struct usbnet * dev,int work)4645b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work)
4655b2fc499SJeff Garzik {
4665b2fc499SJeff Garzik set_bit (work, &dev->flags);
4671e44ee6cSOliver Neukum if (!usbnet_going_away(dev)) {
468ab18a9c9SOliver Neukum if (!schedule_work(&dev->kevent))
4691e44ee6cSOliver Neukum netdev_dbg(dev->net,
4701e44ee6cSOliver Neukum "kevent %s may have been dropped\n",
4711e44ee6cSOliver Neukum usbnet_event_names[work]);
472ab18a9c9SOliver Neukum else
4731e44ee6cSOliver Neukum netdev_dbg(dev->net,
4741e44ee6cSOliver Neukum "kevent %s scheduled\n", usbnet_event_names[work]);
4751e44ee6cSOliver Neukum }
4765b2fc499SJeff Garzik }
4775b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
4785b2fc499SJeff Garzik
4795b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
4805b2fc499SJeff Garzik
4815b2fc499SJeff Garzik static void rx_complete (struct urb *urb);
4825b2fc499SJeff Garzik
rx_submit(struct usbnet * dev,struct urb * urb,gfp_t flags)483dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
4845b2fc499SJeff Garzik {
4855b2fc499SJeff Garzik struct sk_buff *skb;
4865b2fc499SJeff Garzik struct skb_data *entry;
4875b2fc499SJeff Garzik int retval = 0;
4885b2fc499SJeff Garzik unsigned long lockflags;
4895b2fc499SJeff Garzik size_t size = dev->rx_urb_size;
4905b2fc499SJeff Garzik
49170c37bf9SBjørn Mork /* prevent rx skb allocation when error ratio is high */
49270c37bf9SBjørn Mork if (test_bit(EVENT_RX_KILL, &dev->flags)) {
49370c37bf9SBjørn Mork usb_free_urb(urb);
49470c37bf9SBjørn Mork return -ENOLINK;
49570c37bf9SBjørn Mork }
49670c37bf9SBjørn Mork
497a4abd7a8SBjørn Mork if (test_bit(EVENT_NO_IP_ALIGN, &dev->flags))
498a4abd7a8SBjørn Mork skb = __netdev_alloc_skb(dev->net, size, flags);
499a4abd7a8SBjørn Mork else
5007bdd4027SEric Dumazet skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
5017bdd4027SEric Dumazet if (!skb) {
502a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
5035b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
5045b2fc499SJeff Garzik usb_free_urb (urb);
505dacb3975SDavid S. Miller return -ENOMEM;
5065b2fc499SJeff Garzik }
5075b2fc499SJeff Garzik
5085b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb;
5095b2fc499SJeff Garzik entry->urb = urb;
5105b2fc499SJeff Garzik entry->dev = dev;
5115b2fc499SJeff Garzik entry->length = 0;
5125b2fc499SJeff Garzik
5135b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->in,
5145b2fc499SJeff Garzik skb->data, size, rx_complete, skb);
5155b2fc499SJeff Garzik
5165b2fc499SJeff Garzik spin_lock_irqsave (&dev->rxq.lock, lockflags);
5175b2fc499SJeff Garzik
5188e95a202SJoe Perches if (netif_running (dev->net) &&
5198e95a202SJoe Perches netif_device_present (dev->net) &&
520ad70411aSKloetzke Jan test_bit(EVENT_DEV_OPEN, &dev->flags) &&
52169ee472fSOliver Neukum !test_bit (EVENT_RX_HALT, &dev->flags) &&
52269ee472fSOliver Neukum !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
5235b2fc499SJeff Garzik switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
5245b2fc499SJeff Garzik case -EPIPE:
5255b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT);
5265b2fc499SJeff Garzik break;
5275b2fc499SJeff Garzik case -ENOMEM:
5285b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
5295b2fc499SJeff Garzik break;
5305b2fc499SJeff Garzik case -ENODEV:
531a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "device gone\n");
5325b2fc499SJeff Garzik netif_device_detach (dev->net);
5335b2fc499SJeff Garzik break;
534dacb3975SDavid S. Miller case -EHOSTUNREACH:
535dacb3975SDavid S. Miller retval = -ENOLINK;
536dacb3975SDavid S. Miller break;
5375b2fc499SJeff Garzik default:
538a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net,
539a475f603SJoe Perches "rx submit, %d\n", retval);
5405b2fc499SJeff Garzik tasklet_schedule (&dev->bh);
5415b2fc499SJeff Garzik break;
5425b2fc499SJeff Garzik case 0:
5431e44ee6cSOliver Neukum if (!usbnet_going_away(dev))
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
rx_process(struct usbnet * dev,struct sk_buff * skb)561fb59bf28SLeesoo Ahn static inline int 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++;
568fb59bf28SLeesoo Ahn return -EPROTO;
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)
574fb59bf28SLeesoo Ahn return -EALREADY;
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);
580fb59bf28SLeesoo Ahn return -EPROTO;
581073285fdSAlexey Orishko }
582073285fdSAlexey Orishko
583fb59bf28SLeesoo Ahn usbnet_skb_return(dev, skb);
584fb59bf28SLeesoo Ahn return 0;
5855b2fc499SJeff Garzik }
5865b2fc499SJeff Garzik
5875b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
5885b2fc499SJeff Garzik
rx_complete(struct urb * urb)5895b2fc499SJeff Garzik static void rx_complete (struct urb *urb)
5905b2fc499SJeff Garzik {
5915b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context;
5925b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb;
5935b2fc499SJeff Garzik struct usbnet *dev = entry->dev;
5945b2fc499SJeff Garzik int urb_status = urb->status;
5955b6e9bcdSMing Lei enum skb_state state;
5965b2fc499SJeff Garzik
5975b2fc499SJeff Garzik skb_put (skb, urb->actual_length);
5985b6e9bcdSMing Lei state = rx_done;
5995b2fc499SJeff Garzik entry->urb = NULL;
6005b2fc499SJeff Garzik
6015b2fc499SJeff Garzik switch (urb_status) {
60218ab458fSDavid Brownell /* success */
6035b2fc499SJeff Garzik case 0:
6045b2fc499SJeff Garzik break;
6055b2fc499SJeff Garzik
60618ab458fSDavid Brownell /* stalls need manual reset. this is rare ... except that
60718ab458fSDavid Brownell * when going through USB 2.0 TTs, unplug appears this way.
6083ac49a1cSJean Delvare * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
60918ab458fSDavid Brownell * storm, recovering as needed.
61018ab458fSDavid Brownell */
6115b2fc499SJeff Garzik case -EPIPE:
6127963837fSHerbert Xu dev->net->stats.rx_errors++;
6135b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT);
6141a10d0bcSGustavo A. R. Silva fallthrough;
6155b2fc499SJeff Garzik
61618ab458fSDavid Brownell /* software-driven interface shutdown */
61718ab458fSDavid Brownell case -ECONNRESET: /* async unlink */
61818ab458fSDavid Brownell case -ESHUTDOWN: /* hardware gone */
619a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net,
620a475f603SJoe Perches "rx shutdown, code %d\n", urb_status);
6215b2fc499SJeff Garzik goto block;
6225b2fc499SJeff Garzik
62337ebb549SPetr Mladek /* we get controller i/o faults during hub_wq disconnect() delays.
62418ab458fSDavid Brownell * throttle down resubmits, to avoid log floods; just temporarily,
62537ebb549SPetr Mladek * so we still recover when the fault isn't a hub_wq delay.
62618ab458fSDavid Brownell */
6275b2fc499SJeff Garzik case -EPROTO:
6285b2fc499SJeff Garzik case -ETIME:
6295b2fc499SJeff Garzik case -EILSEQ:
6307963837fSHerbert Xu dev->net->stats.rx_errors++;
6315b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) {
6325b2fc499SJeff Garzik mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
633a475f603SJoe Perches netif_dbg(dev, link, dev->net,
634a475f603SJoe Perches "rx throttle %d\n", urb_status);
6355b2fc499SJeff Garzik }
6365b2fc499SJeff Garzik block:
6375b6e9bcdSMing Lei state = rx_cleanup;
6385b2fc499SJeff Garzik entry->urb = urb;
6395b2fc499SJeff Garzik urb = NULL;
6405b2fc499SJeff Garzik break;
6415b2fc499SJeff Garzik
64218ab458fSDavid Brownell /* data overrun ... flush fifo? */
6435b2fc499SJeff Garzik case -EOVERFLOW:
6447963837fSHerbert Xu dev->net->stats.rx_over_errors++;
645df561f66SGustavo A. R. Silva fallthrough;
6465b2fc499SJeff Garzik
6475b2fc499SJeff Garzik default:
6485b6e9bcdSMing Lei state = rx_cleanup;
6497963837fSHerbert Xu dev->net->stats.rx_errors++;
650a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
6515b2fc499SJeff Garzik break;
6525b2fc499SJeff Garzik }
6535b2fc499SJeff Garzik
65470c37bf9SBjørn Mork /* stop rx if packet error rate is high */
65570c37bf9SBjørn Mork if (++dev->pkt_cnt > 30) {
65670c37bf9SBjørn Mork dev->pkt_cnt = 0;
65770c37bf9SBjørn Mork dev->pkt_err = 0;
65870c37bf9SBjørn Mork } else {
65970c37bf9SBjørn Mork if (state == rx_cleanup)
66070c37bf9SBjørn Mork dev->pkt_err++;
66170c37bf9SBjørn Mork if (dev->pkt_err > 20)
66270c37bf9SBjørn Mork set_bit(EVENT_RX_KILL, &dev->flags);
66370c37bf9SBjørn Mork }
66470c37bf9SBjørn Mork
6655b6e9bcdSMing Lei state = defer_bh(dev, skb, &dev->rxq, state);
6665b2fc499SJeff Garzik
6675b2fc499SJeff Garzik if (urb) {
6688e95a202SJoe Perches if (netif_running (dev->net) &&
6695b6e9bcdSMing Lei !test_bit (EVENT_RX_HALT, &dev->flags) &&
6705b6e9bcdSMing Lei state != unlink_start) {
6715b2fc499SJeff Garzik rx_submit (dev, urb, GFP_ATOMIC);
6728a783354SOliver Neukum usb_mark_last_busy(dev->udev);
6735b2fc499SJeff Garzik return;
6745b2fc499SJeff Garzik }
6755b2fc499SJeff Garzik usb_free_urb (urb);
6765b2fc499SJeff Garzik }
677a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
6785b2fc499SJeff Garzik }
6795b2fc499SJeff Garzik
6805b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
usbnet_pause_rx(struct usbnet * dev)6817834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev)
6827834ddbcSJussi Kivilinna {
6837834ddbcSJussi Kivilinna set_bit(EVENT_RX_PAUSED, &dev->flags);
6847834ddbcSJussi Kivilinna
685a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
6867834ddbcSJussi Kivilinna }
6877834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx);
6887834ddbcSJussi Kivilinna
usbnet_resume_rx(struct usbnet * dev)6897834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev)
6907834ddbcSJussi Kivilinna {
6917834ddbcSJussi Kivilinna struct sk_buff *skb;
6927834ddbcSJussi Kivilinna int num = 0;
6937834ddbcSJussi Kivilinna
6947834ddbcSJussi Kivilinna clear_bit(EVENT_RX_PAUSED, &dev->flags);
6957834ddbcSJussi Kivilinna
6967834ddbcSJussi Kivilinna while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
6977834ddbcSJussi Kivilinna usbnet_skb_return(dev, skb);
6987834ddbcSJussi Kivilinna num++;
6997834ddbcSJussi Kivilinna }
7007834ddbcSJussi Kivilinna
7017834ddbcSJussi Kivilinna tasklet_schedule(&dev->bh);
7027834ddbcSJussi Kivilinna
703a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net,
704a475f603SJoe Perches "paused rx queue disabled, %d skbs requeued\n", num);
7057834ddbcSJussi Kivilinna }
7067834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx);
7077834ddbcSJussi Kivilinna
usbnet_purge_paused_rxq(struct usbnet * dev)7087834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev)
7097834ddbcSJussi Kivilinna {
7107834ddbcSJussi Kivilinna skb_queue_purge(&dev->rxq_pause);
7117834ddbcSJussi Kivilinna }
7127834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
7137834ddbcSJussi Kivilinna
7147834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/
7155b2fc499SJeff Garzik
7165b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup
7175b2fc499SJeff Garzik
unlink_urbs(struct usbnet * dev,struct sk_buff_head * q)7185b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
7195b2fc499SJeff Garzik {
7205b2fc499SJeff Garzik unsigned long flags;
7215b6e9bcdSMing Lei struct sk_buff *skb;
7225b2fc499SJeff Garzik int count = 0;
7235b2fc499SJeff Garzik
7245b2fc499SJeff Garzik spin_lock_irqsave (&q->lock, flags);
7255b6e9bcdSMing Lei while (!skb_queue_empty(q)) {
7265b2fc499SJeff Garzik struct skb_data *entry;
7275b2fc499SJeff Garzik struct urb *urb;
7285b2fc499SJeff Garzik int retval;
7295b2fc499SJeff Garzik
7305b6e9bcdSMing Lei skb_queue_walk(q, skb) {
7315b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb;
7325b6e9bcdSMing Lei if (entry->state != unlink_start)
7335b6e9bcdSMing Lei goto found;
7345b6e9bcdSMing Lei }
7355b6e9bcdSMing Lei break;
7365b6e9bcdSMing Lei found:
7375b6e9bcdSMing Lei entry->state = unlink_start;
7385b2fc499SJeff Garzik urb = entry->urb;
7395b2fc499SJeff Garzik
7400956a8c2Stom.leiming@gmail.com /*
7410956a8c2Stom.leiming@gmail.com * Get reference count of the URB to avoid it to be
7420956a8c2Stom.leiming@gmail.com * freed during usb_unlink_urb, which may trigger
7430956a8c2Stom.leiming@gmail.com * use-after-free problem inside usb_unlink_urb since
7440956a8c2Stom.leiming@gmail.com * usb_unlink_urb is always racing with .complete
7450956a8c2Stom.leiming@gmail.com * handler(include defer_bh).
7460956a8c2Stom.leiming@gmail.com */
7470956a8c2Stom.leiming@gmail.com usb_get_urb(urb);
7484231d47eSSebastian Siewior spin_unlock_irqrestore(&q->lock, flags);
7495b2fc499SJeff Garzik // during some PM-driven resume scenarios,
7505b2fc499SJeff Garzik // these (async) unlinks complete immediately
7515b2fc499SJeff Garzik retval = usb_unlink_urb (urb);
7525b2fc499SJeff Garzik if (retval != -EINPROGRESS && retval != 0)
75360b86755SJoe Perches netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
7545b2fc499SJeff Garzik else
7555b2fc499SJeff Garzik count++;
7560956a8c2Stom.leiming@gmail.com usb_put_urb(urb);
7574231d47eSSebastian Siewior spin_lock_irqsave(&q->lock, flags);
7585b2fc499SJeff Garzik }
7595b2fc499SJeff Garzik spin_unlock_irqrestore (&q->lock, flags);
7605b2fc499SJeff Garzik return count;
7615b2fc499SJeff Garzik }
7625b2fc499SJeff Garzik
7635b2fc499SJeff Garzik // Flush all pending rx urbs
7645b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes
7655b2fc499SJeff Garzik
usbnet_unlink_rx_urbs(struct usbnet * dev)7665b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev)
7675b2fc499SJeff Garzik {
7685b2fc499SJeff Garzik if (netif_running(dev->net)) {
7695b2fc499SJeff Garzik (void) unlink_urbs (dev, &dev->rxq);
7705b2fc499SJeff Garzik tasklet_schedule(&dev->bh);
7715b2fc499SJeff Garzik }
7725b2fc499SJeff Garzik }
7735b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
7745b2fc499SJeff Garzik
7755b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
7765b2fc499SJeff Garzik
wait_skb_queue_empty(struct sk_buff_head * q)777fcb0bb6aSEugene Shatokhin static void wait_skb_queue_empty(struct sk_buff_head *q)
778fcb0bb6aSEugene Shatokhin {
779fcb0bb6aSEugene Shatokhin unsigned long flags;
780fcb0bb6aSEugene Shatokhin
781fcb0bb6aSEugene Shatokhin spin_lock_irqsave(&q->lock, flags);
782fcb0bb6aSEugene Shatokhin while (!skb_queue_empty(q)) {
783fcb0bb6aSEugene Shatokhin spin_unlock_irqrestore(&q->lock, flags);
784fcb0bb6aSEugene Shatokhin schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
785fcb0bb6aSEugene Shatokhin set_current_state(TASK_UNINTERRUPTIBLE);
786fcb0bb6aSEugene Shatokhin spin_lock_irqsave(&q->lock, flags);
787fcb0bb6aSEugene Shatokhin }
788fcb0bb6aSEugene Shatokhin spin_unlock_irqrestore(&q->lock, flags);
789fcb0bb6aSEugene Shatokhin }
790fcb0bb6aSEugene Shatokhin
7915b2fc499SJeff Garzik // precondition: never called in_interrupt
usbnet_terminate_urbs(struct usbnet * dev)79269ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev)
79369ee472fSOliver Neukum {
79469ee472fSOliver Neukum DECLARE_WAITQUEUE(wait, current);
79569ee472fSOliver Neukum int temp;
79669ee472fSOliver Neukum
79769ee472fSOliver Neukum /* ensure there are no more active urbs */
79814a0d635SOliver Neukum add_wait_queue(&dev->wait, &wait);
79969ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE);
80069ee472fSOliver Neukum temp = unlink_urbs(dev, &dev->txq) +
80169ee472fSOliver Neukum unlink_urbs(dev, &dev->rxq);
80269ee472fSOliver Neukum
80369ee472fSOliver Neukum /* maybe wait for deletions to finish. */
804fcb0bb6aSEugene Shatokhin wait_skb_queue_empty(&dev->rxq);
805fcb0bb6aSEugene Shatokhin wait_skb_queue_empty(&dev->txq);
806fcb0bb6aSEugene Shatokhin wait_skb_queue_empty(&dev->done);
807a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net,
808a475f603SJoe Perches "waited for %d urb completions\n", temp);
80969ee472fSOliver Neukum set_current_state(TASK_RUNNING);
81014a0d635SOliver Neukum remove_wait_queue(&dev->wait, &wait);
81169ee472fSOliver Neukum }
8125b2fc499SJeff Garzik
usbnet_stop(struct net_device * net)813777baa47SStephen Hemminger int usbnet_stop (struct net_device *net)
8145b2fc499SJeff Garzik {
8155b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
816f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info;
817f50791acSEugene Shatokhin int retval, pm, mpn;
8185b2fc499SJeff Garzik
81975bd0cbdSMing Lei clear_bit(EVENT_DEV_OPEN, &dev->flags);
8205b2fc499SJeff Garzik netif_stop_queue (net);
8215b2fc499SJeff Garzik
822a475f603SJoe Perches netif_info(dev, ifdown, dev->net,
8238ccef431SBen Hutchings "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
8247963837fSHerbert Xu net->stats.rx_packets, net->stats.tx_packets,
82560b86755SJoe Perches net->stats.rx_errors, net->stats.tx_errors);
8265b2fc499SJeff Garzik
82714a0d635SOliver Neukum /* to not race resume */
82814a0d635SOliver Neukum pm = usb_autopm_get_interface(dev->intf);
829a33e9e7fSJussi Kivilinna /* allow minidriver to stop correctly (wireless devices to turn off
830a33e9e7fSJussi Kivilinna * radio etc) */
831a33e9e7fSJussi Kivilinna if (info->stop) {
832a33e9e7fSJussi Kivilinna retval = info->stop(dev);
833a475f603SJoe Perches if (retval < 0)
834a475f603SJoe Perches netif_info(dev, ifdown, dev->net,
83560b86755SJoe Perches "stop fail (%d) usbnet usb-%s-%s, %s\n",
836a33e9e7fSJussi Kivilinna retval,
837a33e9e7fSJussi Kivilinna dev->udev->bus->bus_name, dev->udev->devpath,
838a33e9e7fSJussi Kivilinna info->description);
839a33e9e7fSJussi Kivilinna }
840a33e9e7fSJussi Kivilinna
84169ee472fSOliver Neukum if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
84269ee472fSOliver Neukum usbnet_terminate_urbs(dev);
8435b2fc499SJeff Garzik
8446eecdc5fSDan Williams usbnet_status_stop(dev);
8455b2fc499SJeff Garzik
8467834ddbcSJussi Kivilinna usbnet_purge_paused_rxq(dev);
8477834ddbcSJussi Kivilinna
848f50791acSEugene Shatokhin mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
849f50791acSEugene Shatokhin
850a69e617eSLukas Wunner /* deferred work (timer, softirq, task) must also stop */
8515b2fc499SJeff Garzik dev->flags = 0;
8525b2fc499SJeff Garzik del_timer_sync(&dev->delay);
8535b2fc499SJeff Garzik tasklet_kill(&dev->bh);
854a69e617eSLukas Wunner cancel_work_sync(&dev->kevent);
8551e44ee6cSOliver Neukum
8561e44ee6cSOliver Neukum /* We have cyclic dependencies. Those calls are needed
8571e44ee6cSOliver Neukum * to break a cycle. We cannot fall into the gaps because
8581e44ee6cSOliver Neukum * we have a flag
8591e44ee6cSOliver Neukum */
8601e44ee6cSOliver Neukum tasklet_kill(&dev->bh);
8611e44ee6cSOliver Neukum del_timer_sync(&dev->delay);
8621e44ee6cSOliver Neukum cancel_work_sync(&dev->kevent);
8631e44ee6cSOliver Neukum
86414a0d635SOliver Neukum if (!pm)
86514a0d635SOliver Neukum usb_autopm_put_interface(dev->intf);
86614a0d635SOliver Neukum
867f50791acSEugene Shatokhin if (info->manage_power && mpn)
86869ee472fSOliver Neukum info->manage_power(dev, 0);
86969ee472fSOliver Neukum else
870a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf);
8715b2fc499SJeff Garzik
8725b2fc499SJeff Garzik return 0;
8735b2fc499SJeff Garzik }
874777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop);
8755b2fc499SJeff Garzik
8765b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
8775b2fc499SJeff Garzik
8785b2fc499SJeff Garzik // posts reads, and enables write queuing
8795b2fc499SJeff Garzik
8805b2fc499SJeff Garzik // precondition: never called in_interrupt
8815b2fc499SJeff Garzik
usbnet_open(struct net_device * net)882777baa47SStephen Hemminger int usbnet_open (struct net_device *net)
8835b2fc499SJeff Garzik {
8845b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
885a11a6544SOliver Neukum int retval;
886f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info;
8875b2fc499SJeff Garzik
888a11a6544SOliver Neukum if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
889a475f603SJoe Perches netif_info(dev, ifup, dev->net,
89060b86755SJoe Perches "resumption fail (%d) usbnet usb-%s-%s, %s\n",
891a11a6544SOliver Neukum retval,
89260b86755SJoe Perches dev->udev->bus->bus_name,
89360b86755SJoe Perches dev->udev->devpath,
894a11a6544SOliver Neukum info->description);
895a11a6544SOliver Neukum goto done_nopm;
896a11a6544SOliver Neukum }
897a11a6544SOliver Neukum
8985b2fc499SJeff Garzik // put into "known safe" state
8995b2fc499SJeff Garzik if (info->reset && (retval = info->reset (dev)) < 0) {
900a475f603SJoe Perches netif_info(dev, ifup, dev->net,
90160b86755SJoe Perches "open reset fail (%d) usbnet usb-%s-%s, %s\n",
9025b2fc499SJeff Garzik retval,
90360b86755SJoe Perches dev->udev->bus->bus_name,
90460b86755SJoe Perches dev->udev->devpath,
9055b2fc499SJeff Garzik info->description);
9065b2fc499SJeff Garzik goto done;
9075b2fc499SJeff Garzik }
9085b2fc499SJeff Garzik
909a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change in reset() */
910a88c32aeSMing Lei usbnet_update_max_qlen(dev);
911a88c32aeSMing Lei
9125b2fc499SJeff Garzik // insist peer be connected
9135b2fc499SJeff Garzik if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
9144d8c79b7SGrant Grundler netif_err(dev, ifup, dev->net, "can't open; %d\n", retval);
9155b2fc499SJeff Garzik goto done;
9165b2fc499SJeff Garzik }
9175b2fc499SJeff Garzik
9185b2fc499SJeff Garzik /* start any status interrupt transfer */
9195b2fc499SJeff Garzik if (dev->interrupt) {
9206eecdc5fSDan Williams retval = usbnet_status_start(dev, GFP_KERNEL);
9215b2fc499SJeff Garzik if (retval < 0) {
922a475f603SJoe Perches netif_err(dev, ifup, dev->net,
923a475f603SJoe Perches "intr submit %d\n", retval);
9245b2fc499SJeff Garzik goto done;
9255b2fc499SJeff Garzik }
9265b2fc499SJeff Garzik }
9275b2fc499SJeff Garzik
92868972efaSPaul Stewart set_bit(EVENT_DEV_OPEN, &dev->flags);
9295b2fc499SJeff Garzik netif_start_queue (net);
930a475f603SJoe Perches netif_info(dev, ifup, dev->net,
931a475f603SJoe Perches "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
93260b86755SJoe Perches (int)RX_QLEN(dev), (int)TX_QLEN(dev),
933a475f603SJoe Perches dev->net->mtu,
934a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
935a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
936a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
937a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
938a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
939a475f603SJoe Perches "simple");
9405b2fc499SJeff Garzik
94170c37bf9SBjørn Mork /* reset rx error state */
94270c37bf9SBjørn Mork dev->pkt_cnt = 0;
94370c37bf9SBjørn Mork dev->pkt_err = 0;
94470c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags);
94570c37bf9SBjørn Mork
9465b2fc499SJeff Garzik // delay posting reads until we're fully open
9475b2fc499SJeff Garzik tasklet_schedule (&dev->bh);
94869ee472fSOliver Neukum if (info->manage_power) {
94969ee472fSOliver Neukum retval = info->manage_power(dev, 1);
950a1c088e0SOliver Neukum if (retval < 0) {
951a1c088e0SOliver Neukum retval = 0;
952a1c088e0SOliver Neukum set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
953a1c088e0SOliver Neukum } else {
95469ee472fSOliver Neukum usb_autopm_put_interface(dev->intf);
95569ee472fSOliver Neukum }
956a1c088e0SOliver Neukum }
957a11a6544SOliver Neukum return retval;
9585b2fc499SJeff Garzik done:
959a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf);
960a11a6544SOliver Neukum done_nopm:
9615b2fc499SJeff Garzik return retval;
9625b2fc499SJeff Garzik }
963777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open);
9645b2fc499SJeff Garzik
9655b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
9665b2fc499SJeff Garzik
9675b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but
9685b2fc499SJeff Garzik * they'll probably want to use this base set.
9695b2fc499SJeff Garzik */
9705b2fc499SJeff Garzik
97177651900SOliver Neukum /* These methods are written on the assumption that the device
97277651900SOliver Neukum * uses MII
97377651900SOliver Neukum */
usbnet_get_link_ksettings_mii(struct net_device * net,struct ethtool_link_ksettings * cmd)97477651900SOliver Neukum int usbnet_get_link_ksettings_mii(struct net_device *net,
9758bae3551SPhilippe Reynes struct ethtool_link_ksettings *cmd)
9768bae3551SPhilippe Reynes {
9778bae3551SPhilippe Reynes struct usbnet *dev = netdev_priv(net);
9788bae3551SPhilippe Reynes
9798bae3551SPhilippe Reynes if (!dev->mii.mdio_read)
9808bae3551SPhilippe Reynes return -EOPNOTSUPP;
9818bae3551SPhilippe Reynes
98282c01a84Syuval.shaia@oracle.com mii_ethtool_get_link_ksettings(&dev->mii, cmd);
98382c01a84Syuval.shaia@oracle.com
98482c01a84Syuval.shaia@oracle.com return 0;
9858bae3551SPhilippe Reynes }
98677651900SOliver Neukum EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings_mii);
9878bae3551SPhilippe Reynes
usbnet_get_link_ksettings_internal(struct net_device * net,struct ethtool_link_ksettings * cmd)988956baa99SOliver Neukum int usbnet_get_link_ksettings_internal(struct net_device *net,
989956baa99SOliver Neukum struct ethtool_link_ksettings *cmd)
990956baa99SOliver Neukum {
991956baa99SOliver Neukum struct usbnet *dev = netdev_priv(net);
992956baa99SOliver Neukum
993956baa99SOliver Neukum /* the assumption that speed is equal on tx and rx
994956baa99SOliver Neukum * is deeply engrained into the networking layer.
995956baa99SOliver Neukum * For wireless stuff it is not true.
996956baa99SOliver Neukum * We assume that rx_speed matters more.
997956baa99SOliver Neukum */
998956baa99SOliver Neukum if (dev->rx_speed != SPEED_UNSET)
999956baa99SOliver Neukum cmd->base.speed = dev->rx_speed / 1000000;
1000956baa99SOliver Neukum else if (dev->tx_speed != SPEED_UNSET)
1001956baa99SOliver Neukum cmd->base.speed = dev->tx_speed / 1000000;
1002956baa99SOliver Neukum else
1003956baa99SOliver Neukum cmd->base.speed = SPEED_UNKNOWN;
1004956baa99SOliver Neukum
1005956baa99SOliver Neukum return 0;
1006956baa99SOliver Neukum }
1007956baa99SOliver Neukum EXPORT_SYMBOL_GPL(usbnet_get_link_ksettings_internal);
1008956baa99SOliver Neukum
usbnet_set_link_ksettings_mii(struct net_device * net,const struct ethtool_link_ksettings * cmd)100977651900SOliver Neukum int usbnet_set_link_ksettings_mii(struct net_device *net,
10108bae3551SPhilippe Reynes const struct ethtool_link_ksettings *cmd)
10118bae3551SPhilippe Reynes {
10128bae3551SPhilippe Reynes struct usbnet *dev = netdev_priv(net);
10138bae3551SPhilippe Reynes int retval;
10148bae3551SPhilippe Reynes
10158bae3551SPhilippe Reynes if (!dev->mii.mdio_write)
10168bae3551SPhilippe Reynes return -EOPNOTSUPP;
10178bae3551SPhilippe Reynes
10188bae3551SPhilippe Reynes retval = mii_ethtool_set_link_ksettings(&dev->mii, cmd);
10198bae3551SPhilippe Reynes
10208bae3551SPhilippe Reynes /* link speed/duplex might have changed */
10218bae3551SPhilippe Reynes if (dev->driver_info->link_reset)
10228bae3551SPhilippe Reynes dev->driver_info->link_reset(dev);
10238bae3551SPhilippe Reynes
10248bae3551SPhilippe Reynes /* hard_mtu or rx_urb_size may change in link_reset() */
10258bae3551SPhilippe Reynes usbnet_update_max_qlen(dev);
10268bae3551SPhilippe Reynes
10278bae3551SPhilippe Reynes return retval;
10288bae3551SPhilippe Reynes }
102977651900SOliver Neukum EXPORT_SYMBOL_GPL(usbnet_set_link_ksettings_mii);
10308bae3551SPhilippe Reynes
usbnet_get_link(struct net_device * net)10315b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net)
10325b2fc499SJeff Garzik {
10335b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
10345b2fc499SJeff Garzik
10355b2fc499SJeff Garzik /* If a check_connect is defined, return its result */
10365b2fc499SJeff Garzik if (dev->driver_info->check_connect)
10375b2fc499SJeff Garzik return dev->driver_info->check_connect (dev) == 0;
10385b2fc499SJeff Garzik
10395b2fc499SJeff Garzik /* if the device has mii operations, use those */
10405b2fc499SJeff Garzik if (dev->mii.mdio_read)
10415b2fc499SJeff Garzik return mii_link_ok(&dev->mii);
10425b2fc499SJeff Garzik
104305ffb3e2SBjørn Mork /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
104405ffb3e2SBjørn Mork return ethtool_op_get_link(net);
10455b2fc499SJeff Garzik }
10465b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link);
10475b2fc499SJeff Garzik
usbnet_nway_reset(struct net_device * net)10485b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net)
10495b2fc499SJeff Garzik {
10505b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
10515b2fc499SJeff Garzik
10525b2fc499SJeff Garzik if (!dev->mii.mdio_write)
10535b2fc499SJeff Garzik return -EOPNOTSUPP;
10545b2fc499SJeff Garzik
10555b2fc499SJeff Garzik return mii_nway_restart(&dev->mii);
10565b2fc499SJeff Garzik }
10575b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset);
10585b2fc499SJeff Garzik
usbnet_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)10595b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
10605b2fc499SJeff Garzik {
10615b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
10625b2fc499SJeff Garzik
1063fb3ceec1SWolfram Sang strscpy(info->driver, dev->driver_name, sizeof(info->driver));
1064fb3ceec1SWolfram Sang strscpy(info->fw_version, dev->driver_info->description,
1065fb3ceec1SWolfram Sang sizeof(info->fw_version));
10665b2fc499SJeff Garzik usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
10675b2fc499SJeff Garzik }
10685b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
10695b2fc499SJeff Garzik
usbnet_get_msglevel(struct net_device * net)10705b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net)
10715b2fc499SJeff Garzik {
10725b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
10735b2fc499SJeff Garzik
10745b2fc499SJeff Garzik return dev->msg_enable;
10755b2fc499SJeff Garzik }
10765b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
10775b2fc499SJeff Garzik
usbnet_set_msglevel(struct net_device * net,u32 level)10785b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level)
10795b2fc499SJeff Garzik {
10805b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
10815b2fc499SJeff Garzik
10825b2fc499SJeff Garzik dev->msg_enable = level;
10835b2fc499SJeff Garzik }
10845b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
10855b2fc499SJeff Garzik
10865b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */
10870fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = {
10885b2fc499SJeff Garzik .get_link = usbnet_get_link,
10895b2fc499SJeff Garzik .nway_reset = usbnet_nway_reset,
10905b2fc499SJeff Garzik .get_drvinfo = usbnet_get_drvinfo,
10915b2fc499SJeff Garzik .get_msglevel = usbnet_get_msglevel,
10925b2fc499SJeff Garzik .set_msglevel = usbnet_set_msglevel,
1093123edb18SRichard Cochran .get_ts_info = ethtool_op_get_ts_info,
109477651900SOliver Neukum .get_link_ksettings = usbnet_get_link_ksettings_mii,
109577651900SOliver Neukum .set_link_ksettings = usbnet_set_link_ksettings_mii,
10965b2fc499SJeff Garzik };
10975b2fc499SJeff Garzik
10985b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
10995b2fc499SJeff Garzik
__handle_link_change(struct usbnet * dev)11004b49f58fSMing Lei static void __handle_link_change(struct usbnet *dev)
11014b49f58fSMing Lei {
11024b49f58fSMing Lei if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
11034b49f58fSMing Lei return;
11044b49f58fSMing Lei
11054b49f58fSMing Lei if (!netif_carrier_ok(dev->net)) {
11064b49f58fSMing Lei /* kill URBs for reading packets to save bus bandwidth */
11074b49f58fSMing Lei unlink_urbs(dev, &dev->rxq);
11084b49f58fSMing Lei
11094b49f58fSMing Lei /*
11104b49f58fSMing Lei * tx_timeout will unlink URBs for sending packets and
11114b49f58fSMing Lei * tx queue is stopped by netcore after link becomes off
11124b49f58fSMing Lei */
11134b49f58fSMing Lei } else {
11144b49f58fSMing Lei /* submitting URBs for reading packets */
11154b49f58fSMing Lei tasklet_schedule(&dev->bh);
11164b49f58fSMing Lei }
11174b49f58fSMing Lei
1118a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change during link change */
1119a88c32aeSMing Lei usbnet_update_max_qlen(dev);
1120a88c32aeSMing Lei
11214b49f58fSMing Lei clear_bit(EVENT_LINK_CHANGE, &dev->flags);
11224b49f58fSMing Lei }
11234b49f58fSMing Lei
usbnet_set_rx_mode(struct net_device * net)11241ea2b748SBjørn Mork void usbnet_set_rx_mode(struct net_device *net)
11251efed2d0SOlivier Blin {
11261efed2d0SOlivier Blin struct usbnet *dev = netdev_priv(net);
11271efed2d0SOlivier Blin
11281efed2d0SOlivier Blin usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
11291efed2d0SOlivier Blin }
11301ea2b748SBjørn Mork EXPORT_SYMBOL_GPL(usbnet_set_rx_mode);
11311efed2d0SOlivier Blin
__handle_set_rx_mode(struct usbnet * dev)11321efed2d0SOlivier Blin static void __handle_set_rx_mode(struct usbnet *dev)
11331efed2d0SOlivier Blin {
11341efed2d0SOlivier Blin if (dev->driver_info->set_rx_mode)
11351efed2d0SOlivier Blin (dev->driver_info->set_rx_mode)(dev);
11361efed2d0SOlivier Blin
11371efed2d0SOlivier Blin clear_bit(EVENT_SET_RX_MODE, &dev->flags);
11381efed2d0SOlivier Blin }
11391efed2d0SOlivier Blin
11405b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd.
11415b2fc499SJeff Garzik *
11425b2fc499SJeff Garzik * NOTE: with 2.5 we could do more of this using completion callbacks,
11435b2fc499SJeff Garzik * especially now that control transfers can be queued.
11445b2fc499SJeff Garzik */
11455b2fc499SJeff Garzik static void
usbnet_deferred_kevent(struct work_struct * work)114611f17ef3SOliver Neukum usbnet_deferred_kevent (struct work_struct *work)
11475b2fc499SJeff Garzik {
11485b2fc499SJeff Garzik struct usbnet *dev =
11495b2fc499SJeff Garzik container_of(work, struct usbnet, kevent);
11505b2fc499SJeff Garzik int status;
11515b2fc499SJeff Garzik
11525b2fc499SJeff Garzik /* usb_clear_halt() needs a thread context */
11535b2fc499SJeff Garzik if (test_bit (EVENT_TX_HALT, &dev->flags)) {
11545b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq);
115569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf);
115669ee472fSOliver Neukum if (status < 0)
115769ee472fSOliver Neukum goto fail_pipe;
11585b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->out);
115969ee472fSOliver Neukum usb_autopm_put_interface(dev->intf);
11608e95a202SJoe Perches if (status < 0 &&
11618e95a202SJoe Perches status != -EPIPE &&
11628e95a202SJoe Perches status != -ESHUTDOWN) {
11635b2fc499SJeff Garzik if (netif_msg_tx_err (dev))
116469ee472fSOliver Neukum fail_pipe:
116560b86755SJoe Perches netdev_err(dev->net, "can't clear tx halt, status %d\n",
11665b2fc499SJeff Garzik status);
11675b2fc499SJeff Garzik } else {
11685b2fc499SJeff Garzik clear_bit (EVENT_TX_HALT, &dev->flags);
11695b2fc499SJeff Garzik if (status != -ESHUTDOWN)
11705b2fc499SJeff Garzik netif_wake_queue (dev->net);
11715b2fc499SJeff Garzik }
11725b2fc499SJeff Garzik }
11735b2fc499SJeff Garzik if (test_bit (EVENT_RX_HALT, &dev->flags)) {
11745b2fc499SJeff Garzik unlink_urbs (dev, &dev->rxq);
117569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf);
117669ee472fSOliver Neukum if (status < 0)
117769ee472fSOliver Neukum goto fail_halt;
11785b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->in);
117969ee472fSOliver Neukum usb_autopm_put_interface(dev->intf);
11808e95a202SJoe Perches if (status < 0 &&
11818e95a202SJoe Perches status != -EPIPE &&
11828e95a202SJoe Perches status != -ESHUTDOWN) {
11835b2fc499SJeff Garzik if (netif_msg_rx_err (dev))
118469ee472fSOliver Neukum fail_halt:
118560b86755SJoe Perches netdev_err(dev->net, "can't clear rx halt, status %d\n",
11865b2fc499SJeff Garzik status);
11875b2fc499SJeff Garzik } else {
11885b2fc499SJeff Garzik clear_bit (EVENT_RX_HALT, &dev->flags);
11891e44ee6cSOliver Neukum if (!usbnet_going_away(dev))
11905b2fc499SJeff Garzik tasklet_schedule(&dev->bh);
11915b2fc499SJeff Garzik }
11925b2fc499SJeff Garzik }
11935b2fc499SJeff Garzik
11945b2fc499SJeff Garzik /* tasklet could resubmit itself forever if memory is tight */
11955b2fc499SJeff Garzik if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
11965b2fc499SJeff Garzik struct urb *urb = NULL;
1197dacb3975SDavid S. Miller int resched = 1;
11985b2fc499SJeff Garzik
11995b2fc499SJeff Garzik if (netif_running (dev->net))
12005b2fc499SJeff Garzik urb = usb_alloc_urb (0, GFP_KERNEL);
12015b2fc499SJeff Garzik else
12025b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags);
12035b2fc499SJeff Garzik if (urb != NULL) {
12045b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags);
120569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf);
1206ab60707fSJesper Juhl if (status < 0) {
1207ab60707fSJesper Juhl usb_free_urb(urb);
120869ee472fSOliver Neukum goto fail_lowmem;
1209ab60707fSJesper Juhl }
1210dacb3975SDavid S. Miller if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1211dacb3975SDavid S. Miller resched = 0;
121269ee472fSOliver Neukum usb_autopm_put_interface(dev->intf);
121369ee472fSOliver Neukum fail_lowmem:
1214dacb3975SDavid S. Miller if (resched)
12151e44ee6cSOliver Neukum if (!usbnet_going_away(dev))
12165b2fc499SJeff Garzik tasklet_schedule(&dev->bh);
12175b2fc499SJeff Garzik }
12185b2fc499SJeff Garzik }
12195b2fc499SJeff Garzik
12205b2fc499SJeff Garzik if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1221f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info;
12225b2fc499SJeff Garzik int retval = 0;
12235b2fc499SJeff Garzik
12245b2fc499SJeff Garzik clear_bit (EVENT_LINK_RESET, &dev->flags);
122569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf);
122669ee472fSOliver Neukum if (status < 0)
122769ee472fSOliver Neukum goto skip_reset;
12285b2fc499SJeff Garzik if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
122969ee472fSOliver Neukum usb_autopm_put_interface(dev->intf);
123069ee472fSOliver Neukum skip_reset:
123160b86755SJoe Perches netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
12325b2fc499SJeff Garzik retval,
123360b86755SJoe Perches dev->udev->bus->bus_name,
123460b86755SJoe Perches dev->udev->devpath,
12355b2fc499SJeff Garzik info->description);
123669ee472fSOliver Neukum } else {
123769ee472fSOliver Neukum usb_autopm_put_interface(dev->intf);
12385b2fc499SJeff Garzik }
12394b49f58fSMing Lei
12404b49f58fSMing Lei /* handle link change from link resetting */
12414b49f58fSMing Lei __handle_link_change(dev);
12425b2fc499SJeff Garzik }
12435b2fc499SJeff Garzik
12444b49f58fSMing Lei if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
12454b49f58fSMing Lei __handle_link_change(dev);
12464b49f58fSMing Lei
12471efed2d0SOlivier Blin if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
12481efed2d0SOlivier Blin __handle_set_rx_mode(dev);
12491efed2d0SOlivier Blin
12501efed2d0SOlivier Blin
12515b2fc499SJeff Garzik if (dev->flags)
125260b86755SJoe Perches netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
12535b2fc499SJeff Garzik }
12545b2fc499SJeff Garzik
12555b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
12565b2fc499SJeff Garzik
tx_complete(struct urb * urb)12575b2fc499SJeff Garzik static void tx_complete (struct urb *urb)
12585b2fc499SJeff Garzik {
12595b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context;
12605b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb;
12615b2fc499SJeff Garzik struct usbnet *dev = entry->dev;
12625b2fc499SJeff Garzik
12635b2fc499SJeff Garzik if (urb->status == 0) {
1264af0c351cSHeiner Kallweit struct pcpu_sw_netstats *stats64 = this_cpu_ptr(dev->net->tstats);
12652695578bSEric Dumazet unsigned long flags;
1266c8b5d129SGreg Ungerer
12672695578bSEric Dumazet flags = u64_stats_update_begin_irqsave(&stats64->syncp);
12689962acefSEric Dumazet u64_stats_add(&stats64->tx_packets, entry->packets);
12699962acefSEric Dumazet u64_stats_add(&stats64->tx_bytes, entry->length);
12702695578bSEric Dumazet u64_stats_update_end_irqrestore(&stats64->syncp, flags);
12715b2fc499SJeff Garzik } else {
12727963837fSHerbert Xu dev->net->stats.tx_errors++;
12735b2fc499SJeff Garzik
12745b2fc499SJeff Garzik switch (urb->status) {
12755b2fc499SJeff Garzik case -EPIPE:
12765b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT);
12775b2fc499SJeff Garzik break;
12785b2fc499SJeff Garzik
12795b2fc499SJeff Garzik /* software-driven interface shutdown */
12805b2fc499SJeff Garzik case -ECONNRESET: // async unlink
12815b2fc499SJeff Garzik case -ESHUTDOWN: // hardware gone
12825b2fc499SJeff Garzik break;
12835b2fc499SJeff Garzik
128437ebb549SPetr Mladek /* like rx, tx gets controller i/o faults during hub_wq
128537ebb549SPetr Mladek * delays and so it uses the same throttling mechanism.
128637ebb549SPetr Mladek */
12875b2fc499SJeff Garzik case -EPROTO:
12885b2fc499SJeff Garzik case -ETIME:
12895b2fc499SJeff Garzik case -EILSEQ:
129069ee472fSOliver Neukum usb_mark_last_busy(dev->udev);
12915b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) {
12925b2fc499SJeff Garzik mod_timer (&dev->delay,
12935b2fc499SJeff Garzik jiffies + THROTTLE_JIFFIES);
1294a475f603SJoe Perches netif_dbg(dev, link, dev->net,
1295a475f603SJoe Perches "tx throttle %d\n", urb->status);
12965b2fc499SJeff Garzik }
12975b2fc499SJeff Garzik netif_stop_queue (dev->net);
12985b2fc499SJeff Garzik break;
12995b2fc499SJeff Garzik default:
1300a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net,
1301a475f603SJoe Perches "tx err %d\n", entry->urb->status);
13025b2fc499SJeff Garzik break;
13035b2fc499SJeff Garzik }
13045b2fc499SJeff Garzik }
13055b2fc499SJeff Garzik
130669ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf);
13075b6e9bcdSMing Lei (void) defer_bh(dev, skb, &dev->txq, tx_done);
13085b2fc499SJeff Garzik }
13095b2fc499SJeff Garzik
13105b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
13115b2fc499SJeff Garzik
usbnet_tx_timeout(struct net_device * net,unsigned int txqueue)13120290bd29SMichael S. Tsirkin void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue)
13135b2fc499SJeff Garzik {
13145b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
13155b2fc499SJeff Garzik
13165b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq);
13175b2fc499SJeff Garzik tasklet_schedule (&dev->bh);
1318dbcdd4d5SOliver Neukum /* this needs to be handled individually because the generic layer
1319dbcdd4d5SOliver Neukum * doesn't know what is sufficient and could not restore private
1320dbcdd4d5SOliver Neukum * information if a remedy of an unconditional reset were used.
1321dbcdd4d5SOliver Neukum */
1322dbcdd4d5SOliver Neukum if (dev->driver_info->recover)
1323dbcdd4d5SOliver Neukum (dev->driver_info->recover)(dev);
13245b2fc499SJeff Garzik }
1325777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
13265b2fc499SJeff Garzik
13275b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
13285b2fc499SJeff Garzik
build_dma_sg(const struct sk_buff * skb,struct urb * urb)1329638c5115SMing Lei static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1330638c5115SMing Lei {
1331638c5115SMing Lei unsigned num_sgs, total_len = 0;
1332638c5115SMing Lei int i, s = 0;
1333638c5115SMing Lei
1334638c5115SMing Lei num_sgs = skb_shinfo(skb)->nr_frags + 1;
1335638c5115SMing Lei if (num_sgs == 1)
1336638c5115SMing Lei return 0;
1337638c5115SMing Lei
133860e453a9SMing Lei /* reserve one for zero packet */
13396da2ec56SKees Cook urb->sg = kmalloc_array(num_sgs + 1, sizeof(struct scatterlist),
134060e453a9SMing Lei GFP_ATOMIC);
1341638c5115SMing Lei if (!urb->sg)
1342638c5115SMing Lei return -ENOMEM;
1343638c5115SMing Lei
1344638c5115SMing Lei urb->num_sgs = num_sgs;
1345fdc3452cSBjørn Mork sg_init_table(urb->sg, urb->num_sgs + 1);
1346638c5115SMing Lei
1347638c5115SMing Lei sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1348638c5115SMing Lei total_len += skb_headlen(skb);
1349638c5115SMing Lei
1350638c5115SMing Lei for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1351d7840976SMatthew Wilcox (Oracle) skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1352638c5115SMing Lei
1353638c5115SMing Lei total_len += skb_frag_size(f);
1354d7840976SMatthew Wilcox (Oracle) sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f),
1355b54c9d5bSJonathan Lemon skb_frag_off(f));
1356638c5115SMing Lei }
1357638c5115SMing Lei urb->transfer_buffer_length = total_len;
1358638c5115SMing Lei
1359638c5115SMing Lei return 1;
1360638c5115SMing Lei }
1361638c5115SMing Lei
usbnet_start_xmit(struct sk_buff * skb,struct net_device * net)136225a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
136325a79c41SStephen Hemminger struct net_device *net)
13645b2fc499SJeff Garzik {
13655b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net);
13663e4336a6SJason A. Donenfeld unsigned int length;
13675b2fc499SJeff Garzik struct urb *urb = NULL;
13685b2fc499SJeff Garzik struct skb_data *entry;
1369f3edc2dbSBen Dooks const struct driver_info *info = dev->driver_info;
13705b2fc499SJeff Garzik unsigned long flags;
137125a79c41SStephen Hemminger int retval;
13725b2fc499SJeff Garzik
137323ba0799SKonstantin Khlebnikov if (skb)
1374f9b491ecSMichael Riesch skb_tx_timestamp(skb);
1375f9b491ecSMichael Riesch
13765b2fc499SJeff Garzik // some devices want funky USB-level framing, for
13775b2fc499SJeff Garzik // win32 driver (usually) and/or hardware quirks
13785b2fc499SJeff Garzik if (info->tx_fixup) {
13795b2fc499SJeff Garzik skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
13805b2fc499SJeff Garzik if (!skb) {
1381bf414b36SBjørn Mork /* packet collected; minidriver waiting for more */
1382bf414b36SBjørn Mork if (info->flags & FLAG_MULTI_PACKET)
1383bf414b36SBjørn Mork goto not_drop;
1384a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
13855b2fc499SJeff Garzik goto drop;
13865b2fc499SJeff Garzik }
13875b2fc499SJeff Garzik }
13885b2fc499SJeff Garzik
13895b2fc499SJeff Garzik if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1390a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "no urb\n");
13915b2fc499SJeff Garzik goto drop;
13925b2fc499SJeff Garzik }
13935b2fc499SJeff Garzik
13945b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb;
13955b2fc499SJeff Garzik entry->urb = urb;
13965b2fc499SJeff Garzik entry->dev = dev;
13975b2fc499SJeff Garzik
13985b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->out,
13995b2fc499SJeff Garzik skb->data, skb->len, tx_complete, skb);
1400638c5115SMing Lei if (dev->can_dma_sg) {
1401638c5115SMing Lei if (build_dma_sg(skb, urb) < 0)
1402638c5115SMing Lei goto drop;
1403638c5115SMing Lei }
140460e453a9SMing Lei length = urb->transfer_buffer_length;
14055b2fc499SJeff Garzik
14065b2fc499SJeff Garzik /* don't assume the hardware handles USB_ZERO_PACKET
14075b2fc499SJeff Garzik * NOTE: strictly conforming cdc-ether devices should expect
14085b2fc499SJeff Garzik * the ZLP here, but ignore the one-byte packet.
1409073285fdSAlexey Orishko * NOTE2: CDC NCM specification is different from CDC ECM when
1410073285fdSAlexey Orishko * handling ZLP/short packets, so cdc_ncm driver will make short
1411073285fdSAlexey Orishko * packet itself if needed.
14125b2fc499SJeff Garzik */
1413b4d562e3SElina Pasheva if (length % dev->maxpacket == 0) {
1414b4d562e3SElina Pasheva if (!(info->flags & FLAG_SEND_ZLP)) {
1415073285fdSAlexey Orishko if (!(info->flags & FLAG_MULTI_PACKET)) {
141660e453a9SMing Lei length++;
141760e453a9SMing Lei if (skb_tailroom(skb) && !urb->num_sgs) {
14183e323f3eSPeter Korsgaard skb->data[skb->len] = 0;
14193e323f3eSPeter Korsgaard __skb_put(skb, 1);
142060e453a9SMing Lei } else if (urb->num_sgs)
142160e453a9SMing Lei sg_set_buf(&urb->sg[urb->num_sgs++],
142260e453a9SMing Lei dev->padding_pkt, 1);
1423073285fdSAlexey Orishko }
1424b4d562e3SElina Pasheva } else
1425b4d562e3SElina Pasheva urb->transfer_flags |= URB_ZERO_PACKET;
14263e323f3eSPeter Korsgaard }
14277a1e890eSBen Hutchings urb->transfer_buffer_length = length;
14287a1e890eSBen Hutchings
14297a1e890eSBen Hutchings if (info->flags & FLAG_MULTI_PACKET) {
14307a1e890eSBen Hutchings /* Driver has set number of packets and a length delta.
14317a1e890eSBen Hutchings * Calculate the complete length and ensure that it's
14327a1e890eSBen Hutchings * positive.
14337a1e890eSBen Hutchings */
14347a1e890eSBen Hutchings entry->length += length;
14357a1e890eSBen Hutchings if (WARN_ON_ONCE(entry->length <= 0))
14367a1e890eSBen Hutchings entry->length = length;
14377a1e890eSBen Hutchings } else {
14387a1e890eSBen Hutchings usbnet_set_skb_tx_stats(skb, 1, length);
14397a1e890eSBen Hutchings }
14405b2fc499SJeff Garzik
14415b2fc499SJeff Garzik spin_lock_irqsave(&dev->txq.lock, flags);
144269ee472fSOliver Neukum retval = usb_autopm_get_interface_async(dev->intf);
144369ee472fSOliver Neukum if (retval < 0) {
144469ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags);
144569ee472fSOliver Neukum goto drop;
144669ee472fSOliver Neukum }
1447ad70411aSKloetzke Jan if (netif_queue_stopped(net)) {
1448ad70411aSKloetzke Jan usb_autopm_put_interface_async(dev->intf);
1449ad70411aSKloetzke Jan spin_unlock_irqrestore(&dev->txq.lock, flags);
1450ad70411aSKloetzke Jan goto drop;
1451ad70411aSKloetzke Jan }
145269ee472fSOliver Neukum
145369ee472fSOliver Neukum #ifdef CONFIG_PM
145469ee472fSOliver Neukum /* if this triggers the device is still a sleep */
145569ee472fSOliver Neukum if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
145669ee472fSOliver Neukum /* transmission will be done in resume */
145769ee472fSOliver Neukum usb_anchor_urb(urb, &dev->deferred);
145869ee472fSOliver Neukum /* no use to process more packets */
145969ee472fSOliver Neukum netif_stop_queue(net);
146039707c2aSHemant Kumar usb_put_urb(urb);
146169ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags);
146260b86755SJoe Perches netdev_dbg(dev->net, "Delaying transmission for resumption\n");
146369ee472fSOliver Neukum goto deferred;
146469ee472fSOliver Neukum }
146569ee472fSOliver Neukum #endif
14665b2fc499SJeff Garzik
14675b2fc499SJeff Garzik switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
14685b2fc499SJeff Garzik case -EPIPE:
14695b2fc499SJeff Garzik netif_stop_queue (net);
14705b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT);
147169ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf);
14725b2fc499SJeff Garzik break;
14735b2fc499SJeff Garzik default:
147469ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf);
1475a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net,
1476a475f603SJoe Perches "tx: submit urb err %d\n", retval);
14775b2fc499SJeff Garzik break;
14785b2fc499SJeff Garzik case 0:
1479860e9538SFlorian Westphal netif_trans_update(net);
14805b6e9bcdSMing Lei __usbnet_queue_skb(&dev->txq, skb, tx_start);
14815b2fc499SJeff Garzik if (dev->txq.qlen >= TX_QLEN (dev))
14825b2fc499SJeff Garzik netif_stop_queue (net);
14835b2fc499SJeff Garzik }
14845b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->txq.lock, flags);
14855b2fc499SJeff Garzik
14865b2fc499SJeff Garzik if (retval) {
1487a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
14885b2fc499SJeff Garzik drop:
14897963837fSHerbert Xu dev->net->stats.tx_dropped++;
1490073285fdSAlexey Orishko not_drop:
14915b2fc499SJeff Garzik if (skb)
14925b2fc499SJeff Garzik dev_kfree_skb_any (skb);
1493638c5115SMing Lei if (urb) {
1494638c5115SMing Lei kfree(urb->sg);
14955b2fc499SJeff Garzik usb_free_urb(urb);
1496638c5115SMing Lei }
1497a475f603SJoe Perches } else
1498a475f603SJoe Perches netif_dbg(dev, tx_queued, dev->net,
14993e4336a6SJason A. Donenfeld "> tx, len %u, type 0x%x\n", length, skb->protocol);
150069ee472fSOliver Neukum #ifdef CONFIG_PM
150169ee472fSOliver Neukum deferred:
150269ee472fSOliver Neukum #endif
150325a79c41SStephen Hemminger return NETDEV_TX_OK;
15045b2fc499SJeff Garzik }
1505777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit);
15065b2fc499SJeff Garzik
rx_alloc_submit(struct usbnet * dev,gfp_t flags)150785e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
150865841fd5SMing Lei {
150965841fd5SMing Lei struct urb *urb;
151065841fd5SMing Lei int i;
151185e87870SBjørn Mork int ret = 0;
151265841fd5SMing Lei
151365841fd5SMing Lei /* don't refill the queue all at once */
151465841fd5SMing Lei for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
151565841fd5SMing Lei urb = usb_alloc_urb(0, flags);
151665841fd5SMing Lei if (urb != NULL) {
151785e87870SBjørn Mork ret = rx_submit(dev, urb, flags);
151885e87870SBjørn Mork if (ret)
151985e87870SBjørn Mork goto err;
152085e87870SBjørn Mork } else {
152185e87870SBjørn Mork ret = -ENOMEM;
152285e87870SBjørn Mork goto err;
152365841fd5SMing Lei }
152465841fd5SMing Lei }
152585e87870SBjørn Mork err:
152685e87870SBjørn Mork return ret;
152765841fd5SMing Lei }
152865841fd5SMing Lei
usb_free_skb(struct sk_buff * skb)1529fb59bf28SLeesoo Ahn static inline void usb_free_skb(struct sk_buff *skb)
1530fb59bf28SLeesoo Ahn {
1531fb59bf28SLeesoo Ahn struct skb_data *entry = (struct skb_data *)skb->cb;
1532fb59bf28SLeesoo Ahn
1533fb59bf28SLeesoo Ahn usb_free_urb(entry->urb);
1534fb59bf28SLeesoo Ahn dev_kfree_skb(skb);
1535fb59bf28SLeesoo Ahn }
1536fb59bf28SLeesoo Ahn
15375b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
15385b2fc499SJeff Garzik
15395b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer
15405b2fc499SJeff Garzik
usbnet_bh(struct timer_list * t)15412183c1a6SKees Cook static void usbnet_bh (struct timer_list *t)
15425b2fc499SJeff Garzik {
15432183c1a6SKees Cook struct usbnet *dev = from_timer(dev, t, delay);
15445b2fc499SJeff Garzik struct sk_buff *skb;
15455b2fc499SJeff Garzik struct skb_data *entry;
15465b2fc499SJeff Garzik
15475b2fc499SJeff Garzik while ((skb = skb_dequeue (&dev->done))) {
15485b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb;
15495b2fc499SJeff Garzik switch (entry->state) {
15505b2fc499SJeff Garzik case rx_done:
1551fb59bf28SLeesoo Ahn if (rx_process(dev, skb))
1552fb59bf28SLeesoo Ahn usb_free_skb(skb);
15535b2fc499SJeff Garzik continue;
15545b2fc499SJeff Garzik case tx_done:
1555638c5115SMing Lei kfree(entry->urb->sg);
1556df561f66SGustavo A. R. Silva fallthrough;
15575b2fc499SJeff Garzik case rx_cleanup:
1558fb59bf28SLeesoo Ahn usb_free_skb(skb);
15595b2fc499SJeff Garzik continue;
15605b2fc499SJeff Garzik default:
156160b86755SJoe Perches netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
15625b2fc499SJeff Garzik }
15635b2fc499SJeff Garzik }
15645b2fc499SJeff Garzik
156570c37bf9SBjørn Mork /* restart RX again after disabling due to high error rate */
156670c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags);
156770c37bf9SBjørn Mork
156814a0d635SOliver Neukum /* waiting for all pending urbs to complete?
156914a0d635SOliver Neukum * only then can we forgo submitting anew
157014a0d635SOliver Neukum */
157114a0d635SOliver Neukum if (waitqueue_active(&dev->wait)) {
157214a0d635SOliver Neukum if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
157314a0d635SOliver Neukum wake_up_all(&dev->wait);
15745b2fc499SJeff Garzik
15755b2fc499SJeff Garzik // or are we maybe short a few urbs?
15768e95a202SJoe Perches } else if (netif_running (dev->net) &&
15778e95a202SJoe Perches netif_device_present (dev->net) &&
15784b49f58fSMing Lei netif_carrier_ok(dev->net) &&
15791e44ee6cSOliver Neukum !usbnet_going_away(dev) &&
15808e95a202SJoe Perches !timer_pending(&dev->delay) &&
158143daa96bSSoohoon Lee !test_bit(EVENT_RX_PAUSED, &dev->flags) &&
15828e95a202SJoe Perches !test_bit(EVENT_RX_HALT, &dev->flags)) {
15835b2fc499SJeff Garzik int temp = dev->rxq.qlen;
15845b2fc499SJeff Garzik
158565841fd5SMing Lei if (temp < RX_QLEN(dev)) {
158685e87870SBjørn Mork if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
158785e87870SBjørn Mork return;
1588a475f603SJoe Perches if (temp != dev->rxq.qlen)
1589a475f603SJoe Perches netif_dbg(dev, link, dev->net,
1590a475f603SJoe Perches "rxqlen %d --> %d\n",
15915b2fc499SJeff Garzik temp, dev->rxq.qlen);
159265841fd5SMing Lei if (dev->rxq.qlen < RX_QLEN(dev))
15935b2fc499SJeff Garzik tasklet_schedule (&dev->bh);
15945b2fc499SJeff Garzik }
15955b2fc499SJeff Garzik if (dev->txq.qlen < TX_QLEN (dev))
15965b2fc499SJeff Garzik netif_wake_queue (dev->net);
15975b2fc499SJeff Garzik }
15985b2fc499SJeff Garzik }
15995b2fc499SJeff Garzik
usbnet_bh_tasklet(struct tasklet_struct * t)1600c955e329SEmil Renner Berthing static void usbnet_bh_tasklet(struct tasklet_struct *t)
16012eb1d3f4SPhong Tran {
1602c955e329SEmil Renner Berthing struct usbnet *dev = from_tasklet(dev, t, bh);
16032eb1d3f4SPhong Tran
1604c955e329SEmil Renner Berthing usbnet_bh(&dev->delay);
16052eb1d3f4SPhong Tran }
16062eb1d3f4SPhong Tran
16075b2fc499SJeff Garzik
16085b2fc499SJeff Garzik /*-------------------------------------------------------------------------
16095b2fc499SJeff Garzik *
16105b2fc499SJeff Garzik * USB Device Driver support
16115b2fc499SJeff Garzik *
16125b2fc499SJeff Garzik *-------------------------------------------------------------------------*/
16135b2fc499SJeff Garzik
16145b2fc499SJeff Garzik // precondition: never called in_interrupt
16155b2fc499SJeff Garzik
usbnet_disconnect(struct usb_interface * intf)16165b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf)
16175b2fc499SJeff Garzik {
16185b2fc499SJeff Garzik struct usbnet *dev;
16195b2fc499SJeff Garzik struct usb_device *xdev;
16205b2fc499SJeff Garzik struct net_device *net;
1621a4320615SPeilin Ye struct urb *urb;
16225b2fc499SJeff Garzik
16235b2fc499SJeff Garzik dev = usb_get_intfdata(intf);
16245b2fc499SJeff Garzik usb_set_intfdata(intf, NULL);
16255b2fc499SJeff Garzik if (!dev)
16265b2fc499SJeff Garzik return;
16271e44ee6cSOliver Neukum usbnet_mark_going_away(dev);
16285b2fc499SJeff Garzik
16295b2fc499SJeff Garzik xdev = interface_to_usbdev (intf);
16305b2fc499SJeff Garzik
1631a475f603SJoe Perches netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
16325b2fc499SJeff Garzik intf->dev.driver->name,
16335b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath,
16345b2fc499SJeff Garzik dev->driver_info->description);
16355b2fc499SJeff Garzik
16365b2fc499SJeff Garzik net = dev->net;
16375b2fc499SJeff Garzik unregister_netdev (net);
16385b2fc499SJeff Garzik
1639a4320615SPeilin Ye while ((urb = usb_get_from_anchor(&dev->deferred))) {
1640a4320615SPeilin Ye dev_kfree_skb(urb->context);
1641a4320615SPeilin Ye kfree(urb->sg);
1642a4320615SPeilin Ye usb_free_urb(urb);
1643a4320615SPeilin Ye }
164439707c2aSHemant Kumar
1645d1408f6bSLukas Wunner if (dev->driver_info->unbind)
1646d1408f6bSLukas Wunner dev->driver_info->unbind(dev, intf);
1647d1408f6bSLukas Wunner
164868972efaSPaul Stewart usb_kill_urb(dev->interrupt);
164968972efaSPaul Stewart usb_free_urb(dev->interrupt);
165060e453a9SMing Lei kfree(dev->padding_pkt);
165168972efaSPaul Stewart
1652af0c351cSHeiner Kallweit free_percpu(net->tstats);
16535b2fc499SJeff Garzik free_netdev(net);
16545b2fc499SJeff Garzik }
16555b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect);
16565b2fc499SJeff Garzik
1657777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = {
1658777baa47SStephen Hemminger .ndo_open = usbnet_open,
1659777baa47SStephen Hemminger .ndo_stop = usbnet_stop,
1660777baa47SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit,
1661777baa47SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout,
16621efed2d0SOlivier Blin .ndo_set_rx_mode = usbnet_set_rx_mode,
1663777baa47SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu,
1664af0c351cSHeiner Kallweit .ndo_get_stats64 = dev_get_tstats64,
1665777baa47SStephen Hemminger .ndo_set_mac_address = eth_mac_addr,
1666777baa47SStephen Hemminger .ndo_validate_addr = eth_validate_addr,
1667777baa47SStephen Hemminger };
16685b2fc499SJeff Garzik
16695b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
16705b2fc499SJeff Garzik
16715b2fc499SJeff Garzik // precondition: never called in_interrupt
16725b2fc499SJeff Garzik
1673225794f8SMarcel Holtmann static struct device_type wlan_type = {
1674225794f8SMarcel Holtmann .name = "wlan",
1675225794f8SMarcel Holtmann };
1676225794f8SMarcel Holtmann
1677225794f8SMarcel Holtmann static struct device_type wwan_type = {
1678225794f8SMarcel Holtmann .name = "wwan",
1679225794f8SMarcel Holtmann };
1680225794f8SMarcel Holtmann
16815b2fc499SJeff Garzik int
usbnet_probe(struct usb_interface * udev,const struct usb_device_id * prod)16825b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
16835b2fc499SJeff Garzik {
16845b2fc499SJeff Garzik struct usbnet *dev;
16855b2fc499SJeff Garzik struct net_device *net;
16865b2fc499SJeff Garzik struct usb_host_interface *interface;
1687f3edc2dbSBen Dooks const struct driver_info *info;
16885b2fc499SJeff Garzik struct usb_device *xdev;
16895b2fc499SJeff Garzik int status;
16905b2fc499SJeff Garzik const char *name;
1691b0786b43SMing Lei struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1692b0786b43SMing Lei
1693b0786b43SMing Lei /* usbnet already took usb runtime pm, so have to enable the feature
1694b0786b43SMing Lei * for usb interface, otherwise usb_autopm_get_interface may return
169598f541c6SAlan Stern * failure if RUNTIME_PM is enabled.
1696b0786b43SMing Lei */
1697b0786b43SMing Lei if (!driver->supports_autosuspend) {
1698b0786b43SMing Lei driver->supports_autosuspend = 1;
1699b0786b43SMing Lei pm_runtime_enable(&udev->dev);
1700b0786b43SMing Lei }
17015b2fc499SJeff Garzik
17025b2fc499SJeff Garzik name = udev->dev.driver->name;
1703f3edc2dbSBen Dooks info = (const struct driver_info *) prod->driver_info;
17045b2fc499SJeff Garzik if (!info) {
17055b2fc499SJeff Garzik dev_dbg (&udev->dev, "blacklisted by %s\n", name);
17065b2fc499SJeff Garzik return -ENODEV;
17075b2fc499SJeff Garzik }
17085b2fc499SJeff Garzik xdev = interface_to_usbdev (udev);
17095b2fc499SJeff Garzik interface = udev->cur_altsetting;
17105b2fc499SJeff Garzik
17115b2fc499SJeff Garzik status = -ENOMEM;
17125b2fc499SJeff Garzik
17135b2fc499SJeff Garzik // set up our own records
17145b2fc499SJeff Garzik net = alloc_etherdev(sizeof(*dev));
171541de8d4cSJoe Perches if (!net)
17165b2fc499SJeff Garzik goto out;
17175b2fc499SJeff Garzik
17180dacca73SBen Hutchings /* netdev_printk() needs this so do it as early as possible */
17190dacca73SBen Hutchings SET_NETDEV_DEV(net, &udev->dev);
17200dacca73SBen Hutchings
17215b2fc499SJeff Garzik dev = netdev_priv(net);
17225b2fc499SJeff Garzik dev->udev = xdev;
1723a11a6544SOliver Neukum dev->intf = udev;
17245b2fc499SJeff Garzik dev->driver_info = info;
17255b2fc499SJeff Garzik dev->driver_name = name;
1726956baa99SOliver Neukum dev->rx_speed = SPEED_UNSET;
1727956baa99SOliver Neukum dev->tx_speed = SPEED_UNSET;
1728c8b5d129SGreg Ungerer
1729af0c351cSHeiner Kallweit net->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1730af0c351cSHeiner Kallweit if (!net->tstats)
1731c8b5d129SGreg Ungerer goto out0;
1732c8b5d129SGreg Ungerer
17335b2fc499SJeff Garzik dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
17345b2fc499SJeff Garzik | NETIF_MSG_PROBE | NETIF_MSG_LINK);
173514a0d635SOliver Neukum init_waitqueue_head(&dev->wait);
17365b2fc499SJeff Garzik skb_queue_head_init (&dev->rxq);
17375b2fc499SJeff Garzik skb_queue_head_init (&dev->txq);
17385b2fc499SJeff Garzik skb_queue_head_init (&dev->done);
17397834ddbcSJussi Kivilinna skb_queue_head_init(&dev->rxq_pause);
1740c955e329SEmil Renner Berthing tasklet_setup(&dev->bh, usbnet_bh_tasklet);
174111f17ef3SOliver Neukum INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
174269ee472fSOliver Neukum init_usb_anchor(&dev->deferred);
17432183c1a6SKees Cook timer_setup(&dev->delay, usbnet_bh, 0);
17445b2fc499SJeff Garzik mutex_init (&dev->phy_mutex);
17456eecdc5fSDan Williams mutex_init(&dev->interrupt_mutex);
17466eecdc5fSDan Williams dev->interrupt_count = 0;
17475b2fc499SJeff Garzik
17485b2fc499SJeff Garzik dev->net = net;
1749493c3ca6SLen Baker strscpy(net->name, "usb%d", sizeof(net->name));
17505b2fc499SJeff Garzik
17515b2fc499SJeff Garzik /* rx and tx sides can use different message sizes;
17525b2fc499SJeff Garzik * bind() should set rx_urb_size in that case.
17535b2fc499SJeff Garzik */
17545b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len;
1755f77f0aeeSJarod Wilson net->min_mtu = 0;
1756f77f0aeeSJarod Wilson net->max_mtu = ETH_MAX_MTU;
17575b2fc499SJeff Garzik
1758777baa47SStephen Hemminger net->netdev_ops = &usbnet_netdev_ops;
1759777baa47SStephen Hemminger net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
17605b2fc499SJeff Garzik net->ethtool_ops = &usbnet_ethtool_ops;
17615b2fc499SJeff Garzik
17625b2fc499SJeff Garzik // allow device-specific bind/init procedures
17635b2fc499SJeff Garzik // NOTE net->name still not usable ...
17645b2fc499SJeff Garzik if (info->bind) {
17655b2fc499SJeff Garzik status = info->bind (dev, udev);
17665b2fc499SJeff Garzik if (status < 0)
17675b2fc499SJeff Garzik goto out1;
17685b2fc499SJeff Garzik
17695b2fc499SJeff Garzik // heuristic: "usb%d" for links we know are two-host,
17705b2fc499SJeff Garzik // else "eth%d" when there's reasonable doubt. userspace
17715b2fc499SJeff Garzik // can rename the link if it knows better.
17728e95a202SJoe Perches if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1773c261344dSArnd Bergmann ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1774*7caee37cSOliver Neukum /* somebody touched it*/
1775*7caee37cSOliver Neukum !is_zero_ether_addr(net->dev_addr)))
1776493c3ca6SLen Baker strscpy(net->name, "eth%d", sizeof(net->name));
17776e3bbcc5SJussi Kivilinna /* WLAN devices should always be named "wlan%d" */
17786e3bbcc5SJussi Kivilinna if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1779493c3ca6SLen Baker strscpy(net->name, "wlan%d", sizeof(net->name));
1780e1e499eeSMarcel Holtmann /* WWAN devices should always be named "wwan%d" */
1781e1e499eeSMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1782493c3ca6SLen Baker strscpy(net->name, "wwan%d", sizeof(net->name));
17835b2fc499SJeff Garzik
17846509141fSWei Shuai /* devices that cannot do ARP */
17856509141fSWei Shuai if ((dev->driver_info->flags & FLAG_NOARP) != 0)
17866509141fSWei Shuai net->flags |= IFF_NOARP;
17876509141fSWei Shuai
17885b2fc499SJeff Garzik /* maybe the remote can't receive an Ethernet MTU */
17895b2fc499SJeff Garzik if (net->mtu > (dev->hard_mtu - net->hard_header_len))
17905b2fc499SJeff Garzik net->mtu = dev->hard_mtu - net->hard_header_len;
17915b2fc499SJeff Garzik } else if (!info->in || !info->out)
17925b2fc499SJeff Garzik status = usbnet_get_endpoints (dev, udev);
17935b2fc499SJeff Garzik else {
17945e1627cbSAlan Stern u8 ep_addrs[3] = {
17955e1627cbSAlan Stern info->in + USB_DIR_IN, info->out + USB_DIR_OUT, 0
17965e1627cbSAlan Stern };
17975e1627cbSAlan Stern
17985b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (xdev, info->in);
17995b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (xdev, info->out);
18005b2fc499SJeff Garzik if (!(info->flags & FLAG_NO_SETINT))
18015b2fc499SJeff Garzik status = usb_set_interface (xdev,
18025b2fc499SJeff Garzik interface->desc.bInterfaceNumber,
18035b2fc499SJeff Garzik interface->desc.bAlternateSetting);
18045b2fc499SJeff Garzik else
18055b2fc499SJeff Garzik status = 0;
18065b2fc499SJeff Garzik
18075e1627cbSAlan Stern if (status == 0 && !usb_check_bulk_endpoints(udev, ep_addrs))
18085e1627cbSAlan Stern status = -EINVAL;
18095b2fc499SJeff Garzik }
18109514bfe5SPeter Korsgaard if (status >= 0 && dev->status)
18115b2fc499SJeff Garzik status = init_status (dev, udev);
18125b2fc499SJeff Garzik if (status < 0)
18135b2fc499SJeff Garzik goto out3;
18145b2fc499SJeff Garzik
18155b2fc499SJeff Garzik if (!dev->rx_urb_size)
18165b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu;
1817e13adbfaSVincent Mailhol dev->maxpacket = usb_maxpacket(dev->udev, dev->out);
1818397430b5SOliver Neukum if (dev->maxpacket == 0) {
1819397430b5SOliver Neukum /* that is a broken device */
18206f7c8869SWang Hai status = -ENODEV;
1821397430b5SOliver Neukum goto out4;
1822397430b5SOliver Neukum }
18235b2fc499SJeff Garzik
182407200e31SOliver Neukum /* this flags the device for user space */
182507200e31SOliver Neukum if (!is_valid_ether_addr(net->dev_addr))
182607200e31SOliver Neukum eth_hw_addr_random(net);
1827eef23b53SBjørn Mork
1828225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1829225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wlan_type);
1830225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1831225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wwan_type);
1832225794f8SMarcel Holtmann
1833a88c32aeSMing Lei /* initialize max rx_qlen and tx_qlen */
1834a88c32aeSMing Lei usbnet_update_max_qlen(dev);
1835a88c32aeSMing Lei
183660e453a9SMing Lei if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
183760e453a9SMing Lei !(info->flags & FLAG_MULTI_PACKET)) {
183860e453a9SMing Lei dev->padding_pkt = kzalloc(1, GFP_KERNEL);
183909b69024SWei Yongjun if (!dev->padding_pkt) {
184009b69024SWei Yongjun status = -ENOMEM;
184160e453a9SMing Lei goto out4;
184260e453a9SMing Lei }
184309b69024SWei Yongjun }
184460e453a9SMing Lei
18455b2fc499SJeff Garzik status = register_netdev (net);
18465b2fc499SJeff Garzik if (status)
184760e453a9SMing Lei goto out5;
1848a475f603SJoe Perches netif_info(dev, probe, dev->net,
1849a475f603SJoe Perches "register '%s' at usb-%s-%s, %s, %pM\n",
18505b2fc499SJeff Garzik udev->dev.driver->name,
18515b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath,
18525b2fc499SJeff Garzik dev->driver_info->description,
1853e174961cSJohannes Berg net->dev_addr);
18545b2fc499SJeff Garzik
18555b2fc499SJeff Garzik // ok, it's ready to go.
18565b2fc499SJeff Garzik usb_set_intfdata (udev, dev);
18575b2fc499SJeff Garzik
18585b2fc499SJeff Garzik netif_device_attach (net);
18595b2fc499SJeff Garzik
186037e8273cSBen Hutchings if (dev->driver_info->flags & FLAG_LINK_INTR)
18610162c554SMing Lei usbnet_link_change(dev, 0, 0);
186237e8273cSBen Hutchings
18635b2fc499SJeff Garzik return 0;
18645b2fc499SJeff Garzik
186560e453a9SMing Lei out5:
186660e453a9SMing Lei kfree(dev->padding_pkt);
1867a4723848Stom.leiming@gmail.com out4:
1868a4723848Stom.leiming@gmail.com usb_free_urb(dev->interrupt);
18695b2fc499SJeff Garzik out3:
18705b2fc499SJeff Garzik if (info->unbind)
18715b2fc499SJeff Garzik info->unbind (dev, udev);
18725b2fc499SJeff Garzik out1:
18731666984cSOliver Neukum /* subdrivers must undo all they did in bind() if they
18741666984cSOliver Neukum * fail it, but we may fail later and a deferred kevent
18751666984cSOliver Neukum * may trigger an error resubmitting itself and, worse,
18761666984cSOliver Neukum * schedule a timer. So we kill it all just in case.
18771666984cSOliver Neukum */
1878bc52115cSOliver Neukum usbnet_mark_going_away(dev);
18791666984cSOliver Neukum cancel_work_sync(&dev->kevent);
18801666984cSOliver Neukum del_timer_sync(&dev->delay);
1881af0c351cSHeiner Kallweit free_percpu(net->tstats);
1882c8b5d129SGreg Ungerer out0:
18835b2fc499SJeff Garzik free_netdev(net);
18845b2fc499SJeff Garzik out:
18855b2fc499SJeff Garzik return status;
18865b2fc499SJeff Garzik }
18875b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe);
18885b2fc499SJeff Garzik
18895b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
18905b2fc499SJeff Garzik
189136433127SOliver Neukum /*
189236433127SOliver Neukum * suspend the whole driver as soon as the first interface is suspended
189336433127SOliver Neukum * resume only when the last interface is resumed
18945b2fc499SJeff Garzik */
18955b2fc499SJeff Garzik
usbnet_suspend(struct usb_interface * intf,pm_message_t message)18965b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
18975b2fc499SJeff Garzik {
18985b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf);
18995b2fc499SJeff Garzik
190036433127SOliver Neukum if (!dev->suspend_count++) {
190169ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock);
190269ee472fSOliver Neukum /* don't autosuspend while transmitting */
19035b1b0b81SAlan Stern if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
19045eeb3132SMing Lei dev->suspend_count--;
190569ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock);
190669ee472fSOliver Neukum return -EBUSY;
190769ee472fSOliver Neukum } else {
190869ee472fSOliver Neukum set_bit(EVENT_DEV_ASLEEP, &dev->flags);
190969ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock);
191069ee472fSOliver Neukum }
1911a11a6544SOliver Neukum /*
1912a11a6544SOliver Neukum * accelerate emptying of the rx and queues, to avoid
19135b2fc499SJeff Garzik * having everything error out.
19145b2fc499SJeff Garzik */
19155b2fc499SJeff Garzik netif_device_detach (dev->net);
191669ee472fSOliver Neukum usbnet_terminate_urbs(dev);
19176eecdc5fSDan Williams __usbnet_status_stop_force(dev);
191869ee472fSOliver Neukum
1919a11a6544SOliver Neukum /*
1920a11a6544SOliver Neukum * reattach so runtime management can use and
1921a11a6544SOliver Neukum * wake the device
1922a11a6544SOliver Neukum */
1923a11a6544SOliver Neukum netif_device_attach (dev->net);
192436433127SOliver Neukum }
19255b2fc499SJeff Garzik return 0;
19265b2fc499SJeff Garzik }
19275b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend);
19285b2fc499SJeff Garzik
usbnet_resume(struct usb_interface * intf)19295b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf)
19305b2fc499SJeff Garzik {
19315b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf);
193269ee472fSOliver Neukum struct sk_buff *skb;
193369ee472fSOliver Neukum struct urb *res;
193469ee472fSOliver Neukum int retval;
19355b2fc499SJeff Garzik
193669ee472fSOliver Neukum if (!--dev->suspend_count) {
19376eecdc5fSDan Williams /* resume interrupt URB if it was previously submitted */
19386eecdc5fSDan Williams __usbnet_status_start_force(dev, GFP_NOIO);
193968972efaSPaul Stewart
194069ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock);
194169ee472fSOliver Neukum while ((res = usb_get_from_anchor(&dev->deferred))) {
194269ee472fSOliver Neukum
194369ee472fSOliver Neukum skb = (struct sk_buff *)res->context;
194469ee472fSOliver Neukum retval = usb_submit_urb(res, GFP_ATOMIC);
194569ee472fSOliver Neukum if (retval < 0) {
194669ee472fSOliver Neukum dev_kfree_skb_any(skb);
1947638c5115SMing Lei kfree(res->sg);
194869ee472fSOliver Neukum usb_free_urb(res);
194969ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf);
195069ee472fSOliver Neukum } else {
1951860e9538SFlorian Westphal netif_trans_update(dev->net);
195269ee472fSOliver Neukum __skb_queue_tail(&dev->txq, skb);
195369ee472fSOliver Neukum }
195469ee472fSOliver Neukum }
195569ee472fSOliver Neukum
195669ee472fSOliver Neukum smp_mb();
195769ee472fSOliver Neukum clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
195869ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock);
195975bd0cbdSMing Lei
196075bd0cbdSMing Lei if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
196114a0d635SOliver Neukum /* handle remote wakeup ASAP
196214a0d635SOliver Neukum * we cannot race against stop
196314a0d635SOliver Neukum */
196414a0d635SOliver Neukum if (netif_device_present(dev->net) &&
196565841fd5SMing Lei !timer_pending(&dev->delay) &&
196665841fd5SMing Lei !test_bit(EVENT_RX_HALT, &dev->flags))
1967ab6f148dSOliver Neukum rx_alloc_submit(dev, GFP_NOIO);
196865841fd5SMing Lei
196969ee472fSOliver Neukum if (!(dev->txq.qlen >= TX_QLEN(dev)))
19701aa9bc5bSAlexey Orishko netif_tx_wake_all_queues(dev->net);
19715b2fc499SJeff Garzik tasklet_schedule (&dev->bh);
197269ee472fSOliver Neukum }
197375bd0cbdSMing Lei }
19745d9d01a3SOliver Neukum
19755d9d01a3SOliver Neukum if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
19765d9d01a3SOliver Neukum usb_autopm_get_interface_no_resume(intf);
19775d9d01a3SOliver Neukum
19785b2fc499SJeff Garzik return 0;
19795b2fc499SJeff Garzik }
19805b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume);
19815b2fc499SJeff Garzik
19825d9d01a3SOliver Neukum /*
19835d9d01a3SOliver Neukum * Either a subdriver implements manage_power, then it is assumed to always
19845d9d01a3SOliver Neukum * be ready to be suspended or it reports the readiness to be suspended
19855d9d01a3SOliver Neukum * explicitly
19865d9d01a3SOliver Neukum */
usbnet_device_suggests_idle(struct usbnet * dev)19875d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev)
19885d9d01a3SOliver Neukum {
19895d9d01a3SOliver Neukum if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
19905d9d01a3SOliver Neukum dev->intf->needs_remote_wakeup = 1;
19915d9d01a3SOliver Neukum usb_autopm_put_interface_async(dev->intf);
19925d9d01a3SOliver Neukum }
19935d9d01a3SOliver Neukum }
19945d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle);
19955b2fc499SJeff Garzik
19962dd7c8cfSOliver Neukum /*
19972dd7c8cfSOliver Neukum * For devices that can do without special commands
19982dd7c8cfSOliver Neukum */
usbnet_manage_power(struct usbnet * dev,int on)19992dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on)
20002dd7c8cfSOliver Neukum {
20012dd7c8cfSOliver Neukum dev->intf->needs_remote_wakeup = on;
20022dd7c8cfSOliver Neukum return 0;
20032dd7c8cfSOliver Neukum }
20042dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power);
20052dd7c8cfSOliver Neukum
usbnet_link_change(struct usbnet * dev,bool link,bool need_reset)2006ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
2007ac64995dSMing Lei {
2008ac64995dSMing Lei /* update link after link is reseted */
2009ac64995dSMing Lei if (link && !need_reset)
2010ac64995dSMing Lei netif_carrier_on(dev->net);
2011ac64995dSMing Lei else
2012ac64995dSMing Lei netif_carrier_off(dev->net);
2013ac64995dSMing Lei
2014ac64995dSMing Lei if (need_reset && link)
2015ac64995dSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_RESET);
20164b49f58fSMing Lei else
20174b49f58fSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
2018ac64995dSMing Lei }
2019ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change);
2020ac64995dSMing Lei
20215b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/
__usbnet_read_cmd(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,void * data,u16 size)20220547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2023877bd862SMing Lei u16 value, u16 index, void *data, u16 size)
2024877bd862SMing Lei {
2025877bd862SMing Lei void *buf = NULL;
2026877bd862SMing Lei int err = -ENOMEM;
2027877bd862SMing Lei
2028877bd862SMing Lei netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
2029877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n",
2030877bd862SMing Lei cmd, reqtype, value, index, size);
2031877bd862SMing Lei
20326c22fce0SOliver Neukum if (size) {
2033e65af540SOliver Neukum buf = kmalloc(size, GFP_NOIO);
2034877bd862SMing Lei if (!buf)
2035877bd862SMing Lei goto out;
2036877bd862SMing Lei }
2037877bd862SMing Lei
2038877bd862SMing Lei err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
2039877bd862SMing Lei cmd, reqtype, value, index, buf, size,
2040877bd862SMing Lei USB_CTRL_GET_TIMEOUT);
20416c22fce0SOliver Neukum if (err > 0 && err <= size) {
20426c22fce0SOliver Neukum if (data)
2043877bd862SMing Lei memcpy(data, buf, err);
20446c22fce0SOliver Neukum else
20456c22fce0SOliver Neukum netdev_dbg(dev->net,
20466c22fce0SOliver Neukum "Huh? Data requested but thrown away.\n");
20476c22fce0SOliver Neukum }
2048877bd862SMing Lei kfree(buf);
2049877bd862SMing Lei out:
2050877bd862SMing Lei return err;
2051877bd862SMing Lei }
2052877bd862SMing Lei
__usbnet_write_cmd(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,const void * data,u16 size)20530547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20540547fad2SMing Lei u16 value, u16 index, const void *data,
20550547fad2SMing Lei u16 size)
2056877bd862SMing Lei {
2057877bd862SMing Lei void *buf = NULL;
2058877bd862SMing Lei int err = -ENOMEM;
2059877bd862SMing Lei
2060877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2061877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n",
2062877bd862SMing Lei cmd, reqtype, value, index, size);
2063877bd862SMing Lei
2064877bd862SMing Lei if (data) {
2065e65af540SOliver Neukum buf = kmemdup(data, size, GFP_NOIO);
2066877bd862SMing Lei if (!buf)
2067877bd862SMing Lei goto out;
20686c22fce0SOliver Neukum } else {
20696c22fce0SOliver Neukum if (size) {
20706c22fce0SOliver Neukum WARN_ON_ONCE(1);
20716c22fce0SOliver Neukum err = -EINVAL;
20726c22fce0SOliver Neukum goto out;
20736c22fce0SOliver Neukum }
2074877bd862SMing Lei }
2075877bd862SMing Lei
2076877bd862SMing Lei err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
2077877bd862SMing Lei cmd, reqtype, value, index, buf, size,
2078877bd862SMing Lei USB_CTRL_SET_TIMEOUT);
2079877bd862SMing Lei kfree(buf);
2080877bd862SMing Lei
2081877bd862SMing Lei out:
2082877bd862SMing Lei return err;
2083877bd862SMing Lei }
20840547fad2SMing Lei
20850547fad2SMing Lei /*
20860547fad2SMing Lei * The function can't be called inside suspend/resume callback,
20870547fad2SMing Lei * otherwise deadlock will be caused.
20880547fad2SMing Lei */
usbnet_read_cmd(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,void * data,u16 size)20890547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
20900547fad2SMing Lei u16 value, u16 index, void *data, u16 size)
20910547fad2SMing Lei {
20926f0a0986SMing Lei int ret;
20936f0a0986SMing Lei
20946f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0)
20956f0a0986SMing Lei return -ENODEV;
20966f0a0986SMing Lei ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
20970547fad2SMing Lei data, size);
20986f0a0986SMing Lei usb_autopm_put_interface(dev->intf);
20996f0a0986SMing Lei return ret;
21000547fad2SMing Lei }
21010547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd);
21020547fad2SMing Lei
21030547fad2SMing Lei /*
21040547fad2SMing Lei * The function can't be called inside suspend/resume callback,
21050547fad2SMing Lei * otherwise deadlock will be caused.
21060547fad2SMing Lei */
usbnet_write_cmd(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,const void * data,u16 size)21070547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
21080547fad2SMing Lei u16 value, u16 index, const void *data, u16 size)
21090547fad2SMing Lei {
21106f0a0986SMing Lei int ret;
21116f0a0986SMing Lei
21126f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0)
21136f0a0986SMing Lei return -ENODEV;
21146f0a0986SMing Lei ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
21150547fad2SMing Lei data, size);
21166f0a0986SMing Lei usb_autopm_put_interface(dev->intf);
21176f0a0986SMing Lei return ret;
21180547fad2SMing Lei }
2119877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2120877bd862SMing Lei
21210547fad2SMing Lei /*
21220547fad2SMing Lei * The function can be called inside suspend/resume callback safely
21230547fad2SMing Lei * and should only be called by suspend/resume callback generally.
21240547fad2SMing Lei */
usbnet_read_cmd_nopm(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,void * data,u16 size)21250547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
21260547fad2SMing Lei u16 value, u16 index, void *data, u16 size)
21270547fad2SMing Lei {
21280547fad2SMing Lei return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
21290547fad2SMing Lei data, size);
21300547fad2SMing Lei }
21310547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
21320547fad2SMing Lei
21330547fad2SMing Lei /*
21340547fad2SMing Lei * The function can be called inside suspend/resume callback safely
21350547fad2SMing Lei * and should only be called by suspend/resume callback generally.
21360547fad2SMing Lei */
usbnet_write_cmd_nopm(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,const void * data,u16 size)21370547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
21380547fad2SMing Lei u16 value, u16 index, const void *data,
21390547fad2SMing Lei u16 size)
21400547fad2SMing Lei {
21410547fad2SMing Lei return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
21420547fad2SMing Lei data, size);
21430547fad2SMing Lei }
21440547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
21450547fad2SMing Lei
usbnet_async_cmd_cb(struct urb * urb)2146877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb)
2147877bd862SMing Lei {
2148877bd862SMing Lei struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2149877bd862SMing Lei int status = urb->status;
2150877bd862SMing Lei
2151877bd862SMing Lei if (status < 0)
2152877bd862SMing Lei dev_dbg(&urb->dev->dev, "%s failed with %d",
2153877bd862SMing Lei __func__, status);
2154877bd862SMing Lei
2155877bd862SMing Lei kfree(req);
2156877bd862SMing Lei usb_free_urb(urb);
2157877bd862SMing Lei }
2158877bd862SMing Lei
21590547fad2SMing Lei /*
21600547fad2SMing Lei * The caller must make sure that device can't be put into suspend
21610547fad2SMing Lei * state until the control URB completes.
21620547fad2SMing Lei */
usbnet_write_cmd_async(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,const void * data,u16 size)2163877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2164877bd862SMing Lei u16 value, u16 index, const void *data, u16 size)
2165877bd862SMing Lei {
2166b55a21b7SOliver Neukum struct usb_ctrlrequest *req;
2167877bd862SMing Lei struct urb *urb;
2168877bd862SMing Lei int err = -ENOMEM;
2169877bd862SMing Lei void *buf = NULL;
2170877bd862SMing Lei
2171877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2172877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n",
2173877bd862SMing Lei cmd, reqtype, value, index, size);
2174877bd862SMing Lei
2175877bd862SMing Lei urb = usb_alloc_urb(0, GFP_ATOMIC);
2176ef6cd130SWolfram Sang if (!urb)
2177877bd862SMing Lei goto fail;
2178877bd862SMing Lei
2179877bd862SMing Lei if (data) {
2180877bd862SMing Lei buf = kmemdup(data, size, GFP_ATOMIC);
2181877bd862SMing Lei if (!buf) {
2182877bd862SMing Lei netdev_err(dev->net, "Error allocating buffer"
2183877bd862SMing Lei " in %s!\n", __func__);
2184b55a21b7SOliver Neukum goto fail_free_urb;
2185877bd862SMing Lei }
2186877bd862SMing Lei }
2187877bd862SMing Lei
2188877bd862SMing Lei req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
218938673c82SJoe Perches if (!req)
2190877bd862SMing Lei goto fail_free_buf;
2191877bd862SMing Lei
2192877bd862SMing Lei req->bRequestType = reqtype;
2193877bd862SMing Lei req->bRequest = cmd;
2194877bd862SMing Lei req->wValue = cpu_to_le16(value);
2195877bd862SMing Lei req->wIndex = cpu_to_le16(index);
2196877bd862SMing Lei req->wLength = cpu_to_le16(size);
2197877bd862SMing Lei
2198877bd862SMing Lei usb_fill_control_urb(urb, dev->udev,
2199877bd862SMing Lei usb_sndctrlpipe(dev->udev, 0),
2200877bd862SMing Lei (void *)req, buf, size,
2201877bd862SMing Lei usbnet_async_cmd_cb, req);
2202877bd862SMing Lei urb->transfer_flags |= URB_FREE_BUFFER;
2203877bd862SMing Lei
2204877bd862SMing Lei err = usb_submit_urb(urb, GFP_ATOMIC);
2205877bd862SMing Lei if (err < 0) {
2206877bd862SMing Lei netdev_err(dev->net, "Error submitting the control"
2207877bd862SMing Lei " message: status=%d\n", err);
2208b55a21b7SOliver Neukum goto fail_free_all;
2209877bd862SMing Lei }
2210877bd862SMing Lei return 0;
2211877bd862SMing Lei
2212b55a21b7SOliver Neukum fail_free_all:
2213b55a21b7SOliver Neukum kfree(req);
2214877bd862SMing Lei fail_free_buf:
2215877bd862SMing Lei kfree(buf);
2216b55a21b7SOliver Neukum /*
2217b55a21b7SOliver Neukum * avoid a double free
2218b55a21b7SOliver Neukum * needed because the flag can be set only
2219b55a21b7SOliver Neukum * after filling the URB
2220b55a21b7SOliver Neukum */
2221b55a21b7SOliver Neukum urb->transfer_flags = 0;
2222b55a21b7SOliver Neukum fail_free_urb:
2223877bd862SMing Lei usb_free_urb(urb);
2224877bd862SMing Lei fail:
2225877bd862SMing Lei return err;
2226877bd862SMing Lei
2227877bd862SMing Lei }
2228877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2229877bd862SMing Lei /*-------------------------------------------------------------------------*/
22305b2fc499SJeff Garzik
usbnet_init(void)22315b2fc499SJeff Garzik static int __init usbnet_init(void)
22325b2fc499SJeff Garzik {
2233c582a950SThiago Farina /* Compiler should optimize this out. */
2234c582a950SThiago Farina BUILD_BUG_ON(
2235c593642cSPankaj Bharadiya sizeof_field(struct sk_buff, cb) < sizeof(struct skb_data));
22365b2fc499SJeff Garzik
22375b2fc499SJeff Garzik return 0;
22385b2fc499SJeff Garzik }
22395b2fc499SJeff Garzik module_init(usbnet_init);
22405b2fc499SJeff Garzik
usbnet_exit(void)22415b2fc499SJeff Garzik static void __exit usbnet_exit(void)
22425b2fc499SJeff Garzik {
22435b2fc499SJeff Garzik }
22445b2fc499SJeff Garzik module_exit(usbnet_exit);
22455b2fc499SJeff Garzik
22465b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell");
22475b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework");
22485b2fc499SJeff Garzik MODULE_LICENSE("GPL");
2249