15b2fc499SJeff Garzik /* 25b2fc499SJeff Garzik * USB Network driver infrastructure 35b2fc499SJeff Garzik * Copyright (C) 2000-2005 by David Brownell 45b2fc499SJeff Garzik * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> 55b2fc499SJeff Garzik * 65b2fc499SJeff Garzik * This program is free software; you can redistribute it and/or modify 75b2fc499SJeff Garzik * it under the terms of the GNU General Public License as published by 85b2fc499SJeff Garzik * the Free Software Foundation; either version 2 of the License, or 95b2fc499SJeff Garzik * (at your option) any later version. 105b2fc499SJeff Garzik * 115b2fc499SJeff Garzik * This program is distributed in the hope that it will be useful, 125b2fc499SJeff Garzik * but WITHOUT ANY WARRANTY; without even the implied warranty of 135b2fc499SJeff Garzik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 145b2fc499SJeff Garzik * GNU General Public License for more details. 155b2fc499SJeff Garzik * 165b2fc499SJeff Garzik * You should have received a copy of the GNU General Public License 175b2fc499SJeff Garzik * along with this program; if not, write to the Free Software 185b2fc499SJeff Garzik * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 195b2fc499SJeff Garzik */ 205b2fc499SJeff Garzik 215b2fc499SJeff Garzik /* 225b2fc499SJeff Garzik * This is a generic "USB networking" framework that works with several 235b2fc499SJeff Garzik * kinds of full and high speed networking devices: host-to-host cables, 245b2fc499SJeff Garzik * smart usb peripherals, and actual Ethernet adapters. 255b2fc499SJeff Garzik * 265b2fc499SJeff Garzik * These devices usually differ in terms of control protocols (if they 275b2fc499SJeff Garzik * even have one!) and sometimes they define new framing to wrap or batch 285b2fc499SJeff Garzik * Ethernet packets. Otherwise, they talk to USB pretty much the same, 295b2fc499SJeff Garzik * so interface (un)binding, endpoint I/O queues, fault handling, and other 305b2fc499SJeff Garzik * issues can usefully be addressed by this framework. 315b2fc499SJeff Garzik */ 325b2fc499SJeff Garzik 335b2fc499SJeff Garzik // #define DEBUG // error path messages, extra info 345b2fc499SJeff Garzik // #define VERBOSE // more; success messages 355b2fc499SJeff Garzik 365b2fc499SJeff Garzik #include <linux/module.h> 375b2fc499SJeff Garzik #include <linux/init.h> 385b2fc499SJeff Garzik #include <linux/netdevice.h> 395b2fc499SJeff Garzik #include <linux/etherdevice.h> 4003ad032bSPeter Holik #include <linux/ctype.h> 415b2fc499SJeff Garzik #include <linux/ethtool.h> 425b2fc499SJeff Garzik #include <linux/workqueue.h> 435b2fc499SJeff Garzik #include <linux/mii.h> 445b2fc499SJeff Garzik #include <linux/usb.h> 453692e94fSJussi Kivilinna #include <linux/usb/usbnet.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47fd1f170dSAndy Shevchenko #include <linux/kernel.h> 48b0786b43SMing Lei #include <linux/pm_runtime.h> 495b2fc499SJeff Garzik 505b2fc499SJeff Garzik #define DRIVER_VERSION "22-Aug-2005" 515b2fc499SJeff Garzik 525b2fc499SJeff Garzik 535b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 545b2fc499SJeff Garzik 555b2fc499SJeff Garzik /* 565b2fc499SJeff Garzik * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. 575b2fc499SJeff Garzik * Several dozen bytes of IPv4 data can fit in two such transactions. 585b2fc499SJeff Garzik * One maximum size Ethernet packet takes twenty four of them. 595b2fc499SJeff Garzik * For high speed, each frame comfortably fits almost 36 max size 605b2fc499SJeff Garzik * Ethernet packets (so queues should be bigger). 615b2fc499SJeff Garzik * 62a88c32aeSMing Lei * The goal is to let the USB host controller be busy for 5msec or 63a88c32aeSMing Lei * more before an irq is required, under load. Jumbograms change 64a88c32aeSMing Lei * the equation. 655b2fc499SJeff Garzik */ 66a88c32aeSMing Lei #define MAX_QUEUE_MEMORY (60 * 1518) 67a88c32aeSMing Lei #define RX_QLEN(dev) ((dev)->rx_qlen) 68a88c32aeSMing Lei #define TX_QLEN(dev) ((dev)->tx_qlen) 695b2fc499SJeff Garzik 705b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks 715b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES (5*HZ) 725b2fc499SJeff Garzik 735b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect() 745b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors. 755b2fc499SJeff Garzik #define THROTTLE_JIFFIES (HZ/8) 765b2fc499SJeff Garzik 775b2fc499SJeff Garzik // between wakeups 785b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS 3 795b2fc499SJeff Garzik 805b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 815b2fc499SJeff Garzik 825b2fc499SJeff Garzik // randomly generated ethernet address 835b2fc499SJeff Garzik static u8 node_id [ETH_ALEN]; 845b2fc499SJeff Garzik 855b2fc499SJeff Garzik static const char driver_name [] = "usbnet"; 865b2fc499SJeff Garzik 875b2fc499SJeff Garzik /* use ethtool to change the level for any given device */ 885b2fc499SJeff Garzik static int msg_level = -1; 895b2fc499SJeff Garzik module_param (msg_level, int, 0); 905b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level"); 915b2fc499SJeff Garzik 925b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 935b2fc499SJeff Garzik 945b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */ 955b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) 965b2fc499SJeff Garzik { 975b2fc499SJeff Garzik int tmp; 985b2fc499SJeff Garzik struct usb_host_interface *alt = NULL; 995b2fc499SJeff Garzik struct usb_host_endpoint *in = NULL, *out = NULL; 1005b2fc499SJeff Garzik struct usb_host_endpoint *status = NULL; 1015b2fc499SJeff Garzik 1025b2fc499SJeff Garzik for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 1035b2fc499SJeff Garzik unsigned ep; 1045b2fc499SJeff Garzik 1055b2fc499SJeff Garzik in = out = status = NULL; 1065b2fc499SJeff Garzik alt = intf->altsetting + tmp; 1075b2fc499SJeff Garzik 1085b2fc499SJeff Garzik /* take the first altsetting with in-bulk + out-bulk; 1095b2fc499SJeff Garzik * remember any status endpoint, just in case; 11070f23fd6SJustin P. Mattock * ignore other endpoints and altsettings. 1115b2fc499SJeff Garzik */ 1125b2fc499SJeff Garzik for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 1135b2fc499SJeff Garzik struct usb_host_endpoint *e; 1145b2fc499SJeff Garzik int intr = 0; 1155b2fc499SJeff Garzik 1165b2fc499SJeff Garzik e = alt->endpoint + ep; 1175b2fc499SJeff Garzik switch (e->desc.bmAttributes) { 1185b2fc499SJeff Garzik case USB_ENDPOINT_XFER_INT: 1195b2fc499SJeff Garzik if (!usb_endpoint_dir_in(&e->desc)) 1205b2fc499SJeff Garzik continue; 1215b2fc499SJeff Garzik intr = 1; 1225b2fc499SJeff Garzik /* FALLTHROUGH */ 1235b2fc499SJeff Garzik case USB_ENDPOINT_XFER_BULK: 1245b2fc499SJeff Garzik break; 1255b2fc499SJeff Garzik default: 1265b2fc499SJeff Garzik continue; 1275b2fc499SJeff Garzik } 1285b2fc499SJeff Garzik if (usb_endpoint_dir_in(&e->desc)) { 1295b2fc499SJeff Garzik if (!intr && !in) 1305b2fc499SJeff Garzik in = e; 1315b2fc499SJeff Garzik else if (intr && !status) 1325b2fc499SJeff Garzik status = e; 1335b2fc499SJeff Garzik } else { 1345b2fc499SJeff Garzik if (!out) 1355b2fc499SJeff Garzik out = e; 1365b2fc499SJeff Garzik } 1375b2fc499SJeff Garzik } 1385b2fc499SJeff Garzik if (in && out) 1395b2fc499SJeff Garzik break; 1405b2fc499SJeff Garzik } 1415b2fc499SJeff Garzik if (!alt || !in || !out) 1425b2fc499SJeff Garzik return -EINVAL; 1435b2fc499SJeff Garzik 1448e95a202SJoe Perches if (alt->desc.bAlternateSetting != 0 || 1458e95a202SJoe Perches !(dev->driver_info->flags & FLAG_NO_SETINT)) { 1465b2fc499SJeff Garzik tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, 1475b2fc499SJeff Garzik alt->desc.bAlternateSetting); 1485b2fc499SJeff Garzik if (tmp < 0) 1495b2fc499SJeff Garzik return tmp; 1505b2fc499SJeff Garzik } 1515b2fc499SJeff Garzik 1525b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (dev->udev, 1535b2fc499SJeff Garzik in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1545b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (dev->udev, 1555b2fc499SJeff Garzik out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1565b2fc499SJeff Garzik dev->status = status; 1575b2fc499SJeff Garzik return 0; 1585b2fc499SJeff Garzik } 1595b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints); 1605b2fc499SJeff Garzik 16103ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress) 16203ad032bSPeter Holik { 16303ad032bSPeter Holik int tmp, i; 16403ad032bSPeter Holik unsigned char buf [13]; 16503ad032bSPeter Holik 16603ad032bSPeter Holik tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf); 16703ad032bSPeter Holik if (tmp != 12) { 16803ad032bSPeter Holik dev_dbg(&dev->udev->dev, 16903ad032bSPeter Holik "bad MAC string %d fetch, %d\n", iMACAddress, tmp); 17003ad032bSPeter Holik if (tmp >= 0) 17103ad032bSPeter Holik tmp = -EINVAL; 17203ad032bSPeter Holik return tmp; 17303ad032bSPeter Holik } 17403ad032bSPeter Holik for (i = tmp = 0; i < 6; i++, tmp += 2) 17503ad032bSPeter Holik dev->net->dev_addr [i] = 176fd1f170dSAndy Shevchenko (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]); 17703ad032bSPeter Holik return 0; 17803ad032bSPeter Holik } 17903ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); 18003ad032bSPeter Holik 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 if (!netif_running (dev->net)) 20824ead299Stom.leiming@gmail.com return; 20924ead299Stom.leiming@gmail.com 21024ead299Stom.leiming@gmail.com status = usb_submit_urb (urb, GFP_ATOMIC); 21124ead299Stom.leiming@gmail.com if (status != 0) 21224ead299Stom.leiming@gmail.com netif_err(dev, timer, dev->net, 21324ead299Stom.leiming@gmail.com "intr resubmit --> %d\n", status); 21424ead299Stom.leiming@gmail.com } 2155b2fc499SJeff Garzik 2165b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf) 2175b2fc499SJeff Garzik { 2185b2fc499SJeff Garzik char *buf = NULL; 2195b2fc499SJeff Garzik unsigned pipe = 0; 2205b2fc499SJeff Garzik unsigned maxp; 2215b2fc499SJeff Garzik unsigned period; 2225b2fc499SJeff Garzik 2235b2fc499SJeff Garzik if (!dev->driver_info->status) 2245b2fc499SJeff Garzik return 0; 2255b2fc499SJeff Garzik 2265b2fc499SJeff Garzik pipe = usb_rcvintpipe (dev->udev, 2275b2fc499SJeff Garzik dev->status->desc.bEndpointAddress 2285b2fc499SJeff Garzik & USB_ENDPOINT_NUMBER_MASK); 2295b2fc499SJeff Garzik maxp = usb_maxpacket (dev->udev, pipe, 0); 2305b2fc499SJeff Garzik 2315b2fc499SJeff Garzik /* avoid 1 msec chatter: min 8 msec poll rate */ 2325b2fc499SJeff Garzik period = max ((int) dev->status->desc.bInterval, 2335b2fc499SJeff Garzik (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 2345b2fc499SJeff Garzik 2355b2fc499SJeff Garzik buf = kmalloc (maxp, GFP_KERNEL); 2365b2fc499SJeff Garzik if (buf) { 2375b2fc499SJeff Garzik dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); 2385b2fc499SJeff Garzik if (!dev->interrupt) { 2395b2fc499SJeff Garzik kfree (buf); 2405b2fc499SJeff Garzik return -ENOMEM; 2415b2fc499SJeff Garzik } else { 2425b2fc499SJeff Garzik usb_fill_int_urb(dev->interrupt, dev->udev, pipe, 2435b2fc499SJeff Garzik buf, maxp, intr_complete, dev, period); 244720f3d7cStom.leiming@gmail.com dev->interrupt->transfer_flags |= URB_FREE_BUFFER; 2455b2fc499SJeff Garzik dev_dbg(&intf->dev, 2465b2fc499SJeff Garzik "status ep%din, %d bytes period %d\n", 2475b2fc499SJeff Garzik usb_pipeendpoint(pipe), maxp, period); 2485b2fc499SJeff Garzik } 2495b2fc499SJeff Garzik } 2505b2fc499SJeff Garzik return 0; 2515b2fc499SJeff Garzik } 2525b2fc499SJeff Garzik 2536eecdc5fSDan Williams /* Submit the interrupt URB if not previously submitted, increasing refcount */ 2546eecdc5fSDan Williams int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags) 2556eecdc5fSDan Williams { 2566eecdc5fSDan Williams int ret = 0; 2576eecdc5fSDan Williams 2586eecdc5fSDan Williams WARN_ON_ONCE(dev->interrupt == NULL); 2596eecdc5fSDan Williams if (dev->interrupt) { 2606eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2616eecdc5fSDan Williams 2626eecdc5fSDan Williams if (++dev->interrupt_count == 1) 2636eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags); 2646eecdc5fSDan Williams 2656eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n", 2666eecdc5fSDan Williams dev->interrupt_count); 2676eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2686eecdc5fSDan Williams } 2696eecdc5fSDan Williams return ret; 2706eecdc5fSDan Williams } 2716eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_start); 2726eecdc5fSDan Williams 2736eecdc5fSDan Williams /* For resume; submit interrupt URB if previously submitted */ 2746eecdc5fSDan Williams static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags) 2756eecdc5fSDan Williams { 2766eecdc5fSDan Williams int ret = 0; 2776eecdc5fSDan Williams 2786eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2796eecdc5fSDan Williams if (dev->interrupt_count) { 2806eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags); 2816eecdc5fSDan Williams dev_dbg(&dev->udev->dev, 2826eecdc5fSDan Williams "submitted interrupt URB for resume\n"); 2836eecdc5fSDan Williams } 2846eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2856eecdc5fSDan Williams return ret; 2866eecdc5fSDan Williams } 2876eecdc5fSDan Williams 2886eecdc5fSDan Williams /* Kill the interrupt URB if all submitters want it killed */ 2896eecdc5fSDan Williams void usbnet_status_stop(struct usbnet *dev) 2906eecdc5fSDan Williams { 2916eecdc5fSDan Williams if (dev->interrupt) { 2926eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2936eecdc5fSDan Williams WARN_ON(dev->interrupt_count == 0); 2946eecdc5fSDan Williams 2956eecdc5fSDan Williams if (dev->interrupt_count && --dev->interrupt_count == 0) 2966eecdc5fSDan Williams usb_kill_urb(dev->interrupt); 2976eecdc5fSDan Williams 2986eecdc5fSDan Williams dev_dbg(&dev->udev->dev, 2996eecdc5fSDan Williams "decremented interrupt URB count to %d\n", 3006eecdc5fSDan Williams dev->interrupt_count); 3016eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 3026eecdc5fSDan Williams } 3036eecdc5fSDan Williams } 3046eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_stop); 3056eecdc5fSDan Williams 3066eecdc5fSDan Williams /* For suspend; always kill interrupt URB */ 3076eecdc5fSDan Williams static void __usbnet_status_stop_force(struct usbnet *dev) 3086eecdc5fSDan Williams { 3096eecdc5fSDan Williams if (dev->interrupt) { 3106eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 3116eecdc5fSDan Williams usb_kill_urb(dev->interrupt); 3126eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n"); 3136eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 3146eecdc5fSDan Williams } 3156eecdc5fSDan Williams } 3166eecdc5fSDan Williams 3175b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting. 3185b2fc499SJeff Garzik * Some link protocols batch packets, so their rx_fixup paths 3195b2fc499SJeff Garzik * can return clones as well as just modify the original skb. 3205b2fc499SJeff Garzik */ 3215b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) 3225b2fc499SJeff Garzik { 3235b2fc499SJeff Garzik int status; 3245b2fc499SJeff Garzik 3257834ddbcSJussi Kivilinna if (test_bit(EVENT_RX_PAUSED, &dev->flags)) { 3267834ddbcSJussi Kivilinna skb_queue_tail(&dev->rxq_pause, skb); 3277834ddbcSJussi Kivilinna return; 3287834ddbcSJussi Kivilinna } 3297834ddbcSJussi Kivilinna 3305b2fc499SJeff Garzik skb->protocol = eth_type_trans (skb, dev->net); 3317963837fSHerbert Xu dev->net->stats.rx_packets++; 3327963837fSHerbert Xu dev->net->stats.rx_bytes += skb->len; 3335b2fc499SJeff Garzik 334a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n", 3355b2fc499SJeff Garzik skb->len + sizeof (struct ethhdr), skb->protocol); 3365b2fc499SJeff Garzik memset (skb->cb, 0, sizeof (struct skb_data)); 337f9b491ecSMichael Riesch 338f9b491ecSMichael Riesch if (skb_defer_rx_timestamp(skb)) 339f9b491ecSMichael Riesch return; 340f9b491ecSMichael Riesch 3415b2fc499SJeff Garzik status = netif_rx (skb); 342a475f603SJoe Perches if (status != NET_RX_SUCCESS) 343a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 344a475f603SJoe Perches "netif_rx status %d\n", status); 3455b2fc499SJeff Garzik } 3465b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return); 3475b2fc499SJeff Garzik 348a88c32aeSMing Lei /* must be called if hard_mtu or rx_urb_size changed */ 349a88c32aeSMing Lei void usbnet_update_max_qlen(struct usbnet *dev) 350a88c32aeSMing Lei { 351a88c32aeSMing Lei enum usb_device_speed speed = dev->udev->speed; 352a88c32aeSMing Lei 353a88c32aeSMing Lei switch (speed) { 354a88c32aeSMing Lei case USB_SPEED_HIGH: 355a88c32aeSMing Lei dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size; 356a88c32aeSMing Lei dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu; 357a88c32aeSMing Lei break; 358*452c447aSMing Lei case USB_SPEED_SUPER: 359*452c447aSMing Lei /* 360*452c447aSMing Lei * Not take default 5ms qlen for super speed HC to 361*452c447aSMing Lei * save memory, and iperf tests show 2.5ms qlen can 362*452c447aSMing Lei * work well 363*452c447aSMing Lei */ 364*452c447aSMing Lei dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size; 365*452c447aSMing Lei dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu; 366*452c447aSMing Lei break; 367a88c32aeSMing Lei default: 368a88c32aeSMing Lei dev->rx_qlen = dev->tx_qlen = 4; 369a88c32aeSMing Lei } 370a88c32aeSMing Lei } 371a88c32aeSMing Lei EXPORT_SYMBOL_GPL(usbnet_update_max_qlen); 372a88c32aeSMing Lei 3735b2fc499SJeff Garzik 3745b2fc499SJeff Garzik /*------------------------------------------------------------------------- 3755b2fc499SJeff Garzik * 3765b2fc499SJeff Garzik * Network Device Driver (peer link to "Host Device", from USB host) 3775b2fc499SJeff Garzik * 3785b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 3795b2fc499SJeff Garzik 380777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu) 3815b2fc499SJeff Garzik { 3825b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 3835b2fc499SJeff Garzik int ll_mtu = new_mtu + net->hard_header_len; 3845b2fc499SJeff Garzik int old_hard_mtu = dev->hard_mtu; 3855b2fc499SJeff Garzik int old_rx_urb_size = dev->rx_urb_size; 3865b2fc499SJeff Garzik 3875b2fc499SJeff Garzik if (new_mtu <= 0) 3885b2fc499SJeff Garzik return -EINVAL; 3895b2fc499SJeff Garzik // no second zero-length packet read wanted after mtu-sized packets 3905b2fc499SJeff Garzik if ((ll_mtu % dev->maxpacket) == 0) 3915b2fc499SJeff Garzik return -EDOM; 3925b2fc499SJeff Garzik net->mtu = new_mtu; 3935b2fc499SJeff Garzik 3945b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 3955b2fc499SJeff Garzik if (dev->rx_urb_size == old_hard_mtu) { 3965b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 3975b2fc499SJeff Garzik if (dev->rx_urb_size > old_rx_urb_size) 3985b2fc499SJeff Garzik usbnet_unlink_rx_urbs(dev); 3995b2fc499SJeff Garzik } 4005b2fc499SJeff Garzik 401a88c32aeSMing Lei /* max qlen depend on hard_mtu and rx_urb_size */ 402a88c32aeSMing Lei usbnet_update_max_qlen(dev); 403a88c32aeSMing Lei 4045b2fc499SJeff Garzik return 0; 4055b2fc499SJeff Garzik } 406777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu); 4075b2fc499SJeff Garzik 4085b6e9bcdSMing Lei /* The caller must hold list->lock */ 4095b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list, 4105b6e9bcdSMing Lei struct sk_buff *newsk, enum skb_state state) 4115b6e9bcdSMing Lei { 4125b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) newsk->cb; 4135b6e9bcdSMing Lei 4145b6e9bcdSMing Lei __skb_queue_tail(list, newsk); 4155b6e9bcdSMing Lei entry->state = state; 4165b6e9bcdSMing Lei } 4175b6e9bcdSMing Lei 4185b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4195b2fc499SJeff Garzik 4205b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from 4215b2fc499SJeff Garzik * completion callbacks. 2.5 should have fixed those bugs... 4225b2fc499SJeff Garzik */ 4235b2fc499SJeff Garzik 4245b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb, 4255b6e9bcdSMing Lei struct sk_buff_head *list, enum skb_state state) 4265b2fc499SJeff Garzik { 4275b2fc499SJeff Garzik unsigned long flags; 4285b6e9bcdSMing Lei enum skb_state old_state; 4295b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) skb->cb; 4305b2fc499SJeff Garzik 4315b2fc499SJeff Garzik spin_lock_irqsave(&list->lock, flags); 4325b6e9bcdSMing Lei old_state = entry->state; 4335b6e9bcdSMing Lei entry->state = state; 4345b2fc499SJeff Garzik __skb_unlink(skb, list); 4355b2fc499SJeff Garzik spin_unlock(&list->lock); 4365b2fc499SJeff Garzik spin_lock(&dev->done.lock); 4375b2fc499SJeff Garzik __skb_queue_tail(&dev->done, skb); 4385b2fc499SJeff Garzik if (dev->done.qlen == 1) 4395b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 4405b2fc499SJeff Garzik spin_unlock_irqrestore(&dev->done.lock, flags); 4415b6e9bcdSMing Lei return old_state; 4425b2fc499SJeff Garzik } 4435b2fc499SJeff Garzik 4445b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd 4455b2fc499SJeff Garzik * 4465b2fc499SJeff Garzik * NOTE: annoying asymmetry: if it's active, schedule_work() fails, 4475b2fc499SJeff Garzik * but tasklet_schedule() doesn't. hope the failure is rare. 4485b2fc499SJeff Garzik */ 4495b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work) 4505b2fc499SJeff Garzik { 4515b2fc499SJeff Garzik set_bit (work, &dev->flags); 4529532021dSSteve Glendinning if (!schedule_work (&dev->kevent)) { 4539532021dSSteve Glendinning if (net_ratelimit()) 45460b86755SJoe Perches netdev_err(dev->net, "kevent %d may have been dropped\n", work); 4559532021dSSteve Glendinning } else { 45660b86755SJoe Perches netdev_dbg(dev->net, "kevent %d scheduled\n", work); 4575b2fc499SJeff Garzik } 4589532021dSSteve Glendinning } 4595b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent); 4605b2fc499SJeff Garzik 4615b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4625b2fc499SJeff Garzik 4635b2fc499SJeff Garzik static void rx_complete (struct urb *urb); 4645b2fc499SJeff Garzik 465dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) 4665b2fc499SJeff Garzik { 4675b2fc499SJeff Garzik struct sk_buff *skb; 4685b2fc499SJeff Garzik struct skb_data *entry; 4695b2fc499SJeff Garzik int retval = 0; 4705b2fc499SJeff Garzik unsigned long lockflags; 4715b2fc499SJeff Garzik size_t size = dev->rx_urb_size; 4725b2fc499SJeff Garzik 47370c37bf9SBjørn Mork /* prevent rx skb allocation when error ratio is high */ 47470c37bf9SBjørn Mork if (test_bit(EVENT_RX_KILL, &dev->flags)) { 47570c37bf9SBjørn Mork usb_free_urb(urb); 47670c37bf9SBjørn Mork return -ENOLINK; 47770c37bf9SBjørn Mork } 47870c37bf9SBjørn Mork 4797bdd4027SEric Dumazet skb = __netdev_alloc_skb_ip_align(dev->net, size, flags); 4807bdd4027SEric Dumazet if (!skb) { 481a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no rx skb\n"); 4825b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 4835b2fc499SJeff Garzik usb_free_urb (urb); 484dacb3975SDavid S. Miller return -ENOMEM; 4855b2fc499SJeff Garzik } 4865b2fc499SJeff Garzik 4875b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 4885b2fc499SJeff Garzik entry->urb = urb; 4895b2fc499SJeff Garzik entry->dev = dev; 4905b2fc499SJeff Garzik entry->length = 0; 4915b2fc499SJeff Garzik 4925b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->in, 4935b2fc499SJeff Garzik skb->data, size, rx_complete, skb); 4945b2fc499SJeff Garzik 4955b2fc499SJeff Garzik spin_lock_irqsave (&dev->rxq.lock, lockflags); 4965b2fc499SJeff Garzik 4978e95a202SJoe Perches if (netif_running (dev->net) && 4988e95a202SJoe Perches netif_device_present (dev->net) && 49969ee472fSOliver Neukum !test_bit (EVENT_RX_HALT, &dev->flags) && 50069ee472fSOliver Neukum !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { 5015b2fc499SJeff Garzik switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { 5025b2fc499SJeff Garzik case -EPIPE: 5035b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 5045b2fc499SJeff Garzik break; 5055b2fc499SJeff Garzik case -ENOMEM: 5065b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 5075b2fc499SJeff Garzik break; 5085b2fc499SJeff Garzik case -ENODEV: 509a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "device gone\n"); 5105b2fc499SJeff Garzik netif_device_detach (dev->net); 5115b2fc499SJeff Garzik break; 512dacb3975SDavid S. Miller case -EHOSTUNREACH: 513dacb3975SDavid S. Miller retval = -ENOLINK; 514dacb3975SDavid S. Miller break; 5155b2fc499SJeff Garzik default: 516a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 517a475f603SJoe Perches "rx submit, %d\n", retval); 5185b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 5195b2fc499SJeff Garzik break; 5205b2fc499SJeff Garzik case 0: 5215b6e9bcdSMing Lei __usbnet_queue_skb(&dev->rxq, skb, rx_start); 5225b2fc499SJeff Garzik } 5235b2fc499SJeff Garzik } else { 524a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "rx: stopped\n"); 5255b2fc499SJeff Garzik retval = -ENOLINK; 5265b2fc499SJeff Garzik } 5275b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->rxq.lock, lockflags); 5285b2fc499SJeff Garzik if (retval) { 5295b2fc499SJeff Garzik dev_kfree_skb_any (skb); 5305b2fc499SJeff Garzik usb_free_urb (urb); 5315b2fc499SJeff Garzik } 532dacb3975SDavid S. Miller return retval; 5335b2fc499SJeff Garzik } 5345b2fc499SJeff Garzik 5355b2fc499SJeff Garzik 5365b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5375b2fc499SJeff Garzik 5385b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) 5395b2fc499SJeff Garzik { 5408e95a202SJoe Perches if (dev->driver_info->rx_fixup && 5417a635ea9SAndrzej Zaborowski !dev->driver_info->rx_fixup (dev, skb)) { 5427a635ea9SAndrzej Zaborowski /* With RX_ASSEMBLE, rx_fixup() must update counters */ 5437a635ea9SAndrzej Zaborowski if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE)) 5447a635ea9SAndrzej Zaborowski dev->net->stats.rx_errors++; 5457a635ea9SAndrzej Zaborowski goto done; 5467a635ea9SAndrzej Zaborowski } 5475b2fc499SJeff Garzik // else network stack removes extra byte if we forced a short packet 5485b2fc499SJeff Garzik 549073285fdSAlexey Orishko if (skb->len) { 550073285fdSAlexey Orishko /* all data was already cloned from skb inside the driver */ 551073285fdSAlexey Orishko if (dev->driver_info->flags & FLAG_MULTI_PACKET) 552073285fdSAlexey Orishko dev_kfree_skb_any(skb); 553073285fdSAlexey Orishko else 5545b2fc499SJeff Garzik usbnet_skb_return(dev, skb); 555073285fdSAlexey Orishko return; 556073285fdSAlexey Orishko } 557073285fdSAlexey Orishko 558a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "drop\n"); 5597963837fSHerbert Xu dev->net->stats.rx_errors++; 5607a635ea9SAndrzej Zaborowski done: 5615b2fc499SJeff Garzik skb_queue_tail(&dev->done, skb); 5625b2fc499SJeff Garzik } 5635b2fc499SJeff Garzik 5645b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5655b2fc499SJeff Garzik 5665b2fc499SJeff Garzik static void rx_complete (struct urb *urb) 5675b2fc499SJeff Garzik { 5685b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 5695b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 5705b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 5715b2fc499SJeff Garzik int urb_status = urb->status; 5725b6e9bcdSMing Lei enum skb_state state; 5735b2fc499SJeff Garzik 5745b2fc499SJeff Garzik skb_put (skb, urb->actual_length); 5755b6e9bcdSMing Lei state = rx_done; 5765b2fc499SJeff Garzik entry->urb = NULL; 5775b2fc499SJeff Garzik 5785b2fc499SJeff Garzik switch (urb_status) { 57918ab458fSDavid Brownell /* success */ 5805b2fc499SJeff Garzik case 0: 5815b2fc499SJeff Garzik if (skb->len < dev->net->hard_header_len) { 5825b6e9bcdSMing Lei state = rx_cleanup; 5837963837fSHerbert Xu dev->net->stats.rx_errors++; 5847963837fSHerbert Xu dev->net->stats.rx_length_errors++; 585a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 586a475f603SJoe Perches "rx length %d\n", skb->len); 5875b2fc499SJeff Garzik } 5885b2fc499SJeff Garzik break; 5895b2fc499SJeff Garzik 59018ab458fSDavid Brownell /* stalls need manual reset. this is rare ... except that 59118ab458fSDavid Brownell * when going through USB 2.0 TTs, unplug appears this way. 5923ac49a1cSJean Delvare * we avoid the highspeed version of the ETIMEDOUT/EILSEQ 59318ab458fSDavid Brownell * storm, recovering as needed. 59418ab458fSDavid Brownell */ 5955b2fc499SJeff Garzik case -EPIPE: 5967963837fSHerbert Xu dev->net->stats.rx_errors++; 5975b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 5985b2fc499SJeff Garzik // FALLTHROUGH 5995b2fc499SJeff Garzik 60018ab458fSDavid Brownell /* software-driven interface shutdown */ 60118ab458fSDavid Brownell case -ECONNRESET: /* async unlink */ 60218ab458fSDavid Brownell case -ESHUTDOWN: /* hardware gone */ 603a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 604a475f603SJoe Perches "rx shutdown, code %d\n", urb_status); 6055b2fc499SJeff Garzik goto block; 6065b2fc499SJeff Garzik 60718ab458fSDavid Brownell /* we get controller i/o faults during khubd disconnect() delays. 60818ab458fSDavid Brownell * throttle down resubmits, to avoid log floods; just temporarily, 60918ab458fSDavid Brownell * so we still recover when the fault isn't a khubd delay. 61018ab458fSDavid Brownell */ 6115b2fc499SJeff Garzik case -EPROTO: 6125b2fc499SJeff Garzik case -ETIME: 6135b2fc499SJeff Garzik case -EILSEQ: 6147963837fSHerbert Xu dev->net->stats.rx_errors++; 6155b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 6165b2fc499SJeff Garzik mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); 617a475f603SJoe Perches netif_dbg(dev, link, dev->net, 618a475f603SJoe Perches "rx throttle %d\n", urb_status); 6195b2fc499SJeff Garzik } 6205b2fc499SJeff Garzik block: 6215b6e9bcdSMing Lei state = rx_cleanup; 6225b2fc499SJeff Garzik entry->urb = urb; 6235b2fc499SJeff Garzik urb = NULL; 6245b2fc499SJeff Garzik break; 6255b2fc499SJeff Garzik 62618ab458fSDavid Brownell /* data overrun ... flush fifo? */ 6275b2fc499SJeff Garzik case -EOVERFLOW: 6287963837fSHerbert Xu dev->net->stats.rx_over_errors++; 6295b2fc499SJeff Garzik // FALLTHROUGH 6305b2fc499SJeff Garzik 6315b2fc499SJeff Garzik default: 6325b6e9bcdSMing Lei state = rx_cleanup; 6337963837fSHerbert Xu dev->net->stats.rx_errors++; 634a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status); 6355b2fc499SJeff Garzik break; 6365b2fc499SJeff Garzik } 6375b2fc499SJeff Garzik 63870c37bf9SBjørn Mork /* stop rx if packet error rate is high */ 63970c37bf9SBjørn Mork if (++dev->pkt_cnt > 30) { 64070c37bf9SBjørn Mork dev->pkt_cnt = 0; 64170c37bf9SBjørn Mork dev->pkt_err = 0; 64270c37bf9SBjørn Mork } else { 64370c37bf9SBjørn Mork if (state == rx_cleanup) 64470c37bf9SBjørn Mork dev->pkt_err++; 64570c37bf9SBjørn Mork if (dev->pkt_err > 20) 64670c37bf9SBjørn Mork set_bit(EVENT_RX_KILL, &dev->flags); 64770c37bf9SBjørn Mork } 64870c37bf9SBjørn Mork 6495b6e9bcdSMing Lei state = defer_bh(dev, skb, &dev->rxq, state); 6505b2fc499SJeff Garzik 6515b2fc499SJeff Garzik if (urb) { 6528e95a202SJoe Perches if (netif_running (dev->net) && 6535b6e9bcdSMing Lei !test_bit (EVENT_RX_HALT, &dev->flags) && 6545b6e9bcdSMing Lei state != unlink_start) { 6555b2fc499SJeff Garzik rx_submit (dev, urb, GFP_ATOMIC); 6568a783354SOliver Neukum usb_mark_last_busy(dev->udev); 6575b2fc499SJeff Garzik return; 6585b2fc499SJeff Garzik } 6595b2fc499SJeff Garzik usb_free_urb (urb); 6605b2fc499SJeff Garzik } 661a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n"); 6625b2fc499SJeff Garzik } 6635b2fc499SJeff Garzik 6645b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 6657834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev) 6667834ddbcSJussi Kivilinna { 6677834ddbcSJussi Kivilinna set_bit(EVENT_RX_PAUSED, &dev->flags); 6687834ddbcSJussi Kivilinna 669a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n"); 6707834ddbcSJussi Kivilinna } 6717834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx); 6727834ddbcSJussi Kivilinna 6737834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev) 6747834ddbcSJussi Kivilinna { 6757834ddbcSJussi Kivilinna struct sk_buff *skb; 6767834ddbcSJussi Kivilinna int num = 0; 6777834ddbcSJussi Kivilinna 6787834ddbcSJussi Kivilinna clear_bit(EVENT_RX_PAUSED, &dev->flags); 6797834ddbcSJussi Kivilinna 6807834ddbcSJussi Kivilinna while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { 6817834ddbcSJussi Kivilinna usbnet_skb_return(dev, skb); 6827834ddbcSJussi Kivilinna num++; 6837834ddbcSJussi Kivilinna } 6847834ddbcSJussi Kivilinna 6857834ddbcSJussi Kivilinna tasklet_schedule(&dev->bh); 6867834ddbcSJussi Kivilinna 687a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, 688a475f603SJoe Perches "paused rx queue disabled, %d skbs requeued\n", num); 6897834ddbcSJussi Kivilinna } 6907834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx); 6917834ddbcSJussi Kivilinna 6927834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev) 6937834ddbcSJussi Kivilinna { 6947834ddbcSJussi Kivilinna skb_queue_purge(&dev->rxq_pause); 6957834ddbcSJussi Kivilinna } 6967834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); 6977834ddbcSJussi Kivilinna 6987834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/ 6995b2fc499SJeff Garzik 7005b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup 7015b2fc499SJeff Garzik 7025b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 7035b2fc499SJeff Garzik { 7045b2fc499SJeff Garzik unsigned long flags; 7055b6e9bcdSMing Lei struct sk_buff *skb; 7065b2fc499SJeff Garzik int count = 0; 7075b2fc499SJeff Garzik 7085b2fc499SJeff Garzik spin_lock_irqsave (&q->lock, flags); 7095b6e9bcdSMing Lei while (!skb_queue_empty(q)) { 7105b2fc499SJeff Garzik struct skb_data *entry; 7115b2fc499SJeff Garzik struct urb *urb; 7125b2fc499SJeff Garzik int retval; 7135b2fc499SJeff Garzik 7145b6e9bcdSMing Lei skb_queue_walk(q, skb) { 7155b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 7165b6e9bcdSMing Lei if (entry->state != unlink_start) 7175b6e9bcdSMing Lei goto found; 7185b6e9bcdSMing Lei } 7195b6e9bcdSMing Lei break; 7205b6e9bcdSMing Lei found: 7215b6e9bcdSMing Lei entry->state = unlink_start; 7225b2fc499SJeff Garzik urb = entry->urb; 7235b2fc499SJeff Garzik 7240956a8c2Stom.leiming@gmail.com /* 7250956a8c2Stom.leiming@gmail.com * Get reference count of the URB to avoid it to be 7260956a8c2Stom.leiming@gmail.com * freed during usb_unlink_urb, which may trigger 7270956a8c2Stom.leiming@gmail.com * use-after-free problem inside usb_unlink_urb since 7280956a8c2Stom.leiming@gmail.com * usb_unlink_urb is always racing with .complete 7290956a8c2Stom.leiming@gmail.com * handler(include defer_bh). 7300956a8c2Stom.leiming@gmail.com */ 7310956a8c2Stom.leiming@gmail.com usb_get_urb(urb); 7324231d47eSSebastian Siewior spin_unlock_irqrestore(&q->lock, flags); 7335b2fc499SJeff Garzik // during some PM-driven resume scenarios, 7345b2fc499SJeff Garzik // these (async) unlinks complete immediately 7355b2fc499SJeff Garzik retval = usb_unlink_urb (urb); 7365b2fc499SJeff Garzik if (retval != -EINPROGRESS && retval != 0) 73760b86755SJoe Perches netdev_dbg(dev->net, "unlink urb err, %d\n", retval); 7385b2fc499SJeff Garzik else 7395b2fc499SJeff Garzik count++; 7400956a8c2Stom.leiming@gmail.com usb_put_urb(urb); 7414231d47eSSebastian Siewior spin_lock_irqsave(&q->lock, flags); 7425b2fc499SJeff Garzik } 7435b2fc499SJeff Garzik spin_unlock_irqrestore (&q->lock, flags); 7445b2fc499SJeff Garzik return count; 7455b2fc499SJeff Garzik } 7465b2fc499SJeff Garzik 7475b2fc499SJeff Garzik // Flush all pending rx urbs 7485b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes 7495b2fc499SJeff Garzik 7505b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev) 7515b2fc499SJeff Garzik { 7525b2fc499SJeff Garzik if (netif_running(dev->net)) { 7535b2fc499SJeff Garzik (void) unlink_urbs (dev, &dev->rxq); 7545b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 7555b2fc499SJeff Garzik } 7565b2fc499SJeff Garzik } 7575b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); 7585b2fc499SJeff Garzik 7595b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 7605b2fc499SJeff Garzik 7615b2fc499SJeff Garzik // precondition: never called in_interrupt 76269ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev) 76369ee472fSOliver Neukum { 76469ee472fSOliver Neukum DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup); 76569ee472fSOliver Neukum DECLARE_WAITQUEUE(wait, current); 76669ee472fSOliver Neukum int temp; 76769ee472fSOliver Neukum 76869ee472fSOliver Neukum /* ensure there are no more active urbs */ 76969ee472fSOliver Neukum add_wait_queue(&unlink_wakeup, &wait); 77069ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 77169ee472fSOliver Neukum dev->wait = &unlink_wakeup; 77269ee472fSOliver Neukum temp = unlink_urbs(dev, &dev->txq) + 77369ee472fSOliver Neukum unlink_urbs(dev, &dev->rxq); 77469ee472fSOliver Neukum 77569ee472fSOliver Neukum /* maybe wait for deletions to finish. */ 77669ee472fSOliver Neukum while (!skb_queue_empty(&dev->rxq) 77769ee472fSOliver Neukum && !skb_queue_empty(&dev->txq) 77869ee472fSOliver Neukum && !skb_queue_empty(&dev->done)) { 77966cc42a4SKulikov Vasiliy schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); 78069ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 781a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 782a475f603SJoe Perches "waited for %d urb completions\n", temp); 78369ee472fSOliver Neukum } 78469ee472fSOliver Neukum set_current_state(TASK_RUNNING); 78569ee472fSOliver Neukum dev->wait = NULL; 78669ee472fSOliver Neukum remove_wait_queue(&unlink_wakeup, &wait); 78769ee472fSOliver Neukum } 7885b2fc499SJeff Garzik 789777baa47SStephen Hemminger int usbnet_stop (struct net_device *net) 7905b2fc499SJeff Garzik { 7915b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 792a33e9e7fSJussi Kivilinna struct driver_info *info = dev->driver_info; 793a33e9e7fSJussi Kivilinna int retval; 7945b2fc499SJeff Garzik 79575bd0cbdSMing Lei clear_bit(EVENT_DEV_OPEN, &dev->flags); 7965b2fc499SJeff Garzik netif_stop_queue (net); 7975b2fc499SJeff Garzik 798a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 7998ccef431SBen Hutchings "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", 8007963837fSHerbert Xu net->stats.rx_packets, net->stats.tx_packets, 80160b86755SJoe Perches net->stats.rx_errors, net->stats.tx_errors); 8025b2fc499SJeff Garzik 803a33e9e7fSJussi Kivilinna /* allow minidriver to stop correctly (wireless devices to turn off 804a33e9e7fSJussi Kivilinna * radio etc) */ 805a33e9e7fSJussi Kivilinna if (info->stop) { 806a33e9e7fSJussi Kivilinna retval = info->stop(dev); 807a475f603SJoe Perches if (retval < 0) 808a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 80960b86755SJoe Perches "stop fail (%d) usbnet usb-%s-%s, %s\n", 810a33e9e7fSJussi Kivilinna retval, 811a33e9e7fSJussi Kivilinna dev->udev->bus->bus_name, dev->udev->devpath, 812a33e9e7fSJussi Kivilinna info->description); 813a33e9e7fSJussi Kivilinna } 814a33e9e7fSJussi Kivilinna 81569ee472fSOliver Neukum if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) 81669ee472fSOliver Neukum usbnet_terminate_urbs(dev); 8175b2fc499SJeff Garzik 8186eecdc5fSDan Williams usbnet_status_stop(dev); 8195b2fc499SJeff Garzik 8207834ddbcSJussi Kivilinna usbnet_purge_paused_rxq(dev); 8217834ddbcSJussi Kivilinna 8225b2fc499SJeff Garzik /* deferred work (task, timer, softirq) must also stop. 8235b2fc499SJeff Garzik * can't flush_scheduled_work() until we drop rtnl (later), 8245b2fc499SJeff Garzik * else workers could deadlock; so make workers a NOP. 8255b2fc499SJeff Garzik */ 8265b2fc499SJeff Garzik dev->flags = 0; 8275b2fc499SJeff Garzik del_timer_sync (&dev->delay); 8285b2fc499SJeff Garzik tasklet_kill (&dev->bh); 829a1c088e0SOliver Neukum if (info->manage_power && 830a1c088e0SOliver Neukum !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags)) 83169ee472fSOliver Neukum info->manage_power(dev, 0); 83269ee472fSOliver Neukum else 833a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 8345b2fc499SJeff Garzik 8355b2fc499SJeff Garzik return 0; 8365b2fc499SJeff Garzik } 837777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop); 8385b2fc499SJeff Garzik 8395b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 8405b2fc499SJeff Garzik 8415b2fc499SJeff Garzik // posts reads, and enables write queuing 8425b2fc499SJeff Garzik 8435b2fc499SJeff Garzik // precondition: never called in_interrupt 8445b2fc499SJeff Garzik 845777baa47SStephen Hemminger int usbnet_open (struct net_device *net) 8465b2fc499SJeff Garzik { 8475b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 848a11a6544SOliver Neukum int retval; 8495b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 8505b2fc499SJeff Garzik 851a11a6544SOliver Neukum if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { 852a475f603SJoe Perches netif_info(dev, ifup, dev->net, 85360b86755SJoe Perches "resumption fail (%d) usbnet usb-%s-%s, %s\n", 854a11a6544SOliver Neukum retval, 85560b86755SJoe Perches dev->udev->bus->bus_name, 85660b86755SJoe Perches dev->udev->devpath, 857a11a6544SOliver Neukum info->description); 858a11a6544SOliver Neukum goto done_nopm; 859a11a6544SOliver Neukum } 860a11a6544SOliver Neukum 8615b2fc499SJeff Garzik // put into "known safe" state 8625b2fc499SJeff Garzik if (info->reset && (retval = info->reset (dev)) < 0) { 863a475f603SJoe Perches netif_info(dev, ifup, dev->net, 86460b86755SJoe Perches "open reset fail (%d) usbnet usb-%s-%s, %s\n", 8655b2fc499SJeff Garzik retval, 86660b86755SJoe Perches dev->udev->bus->bus_name, 86760b86755SJoe Perches dev->udev->devpath, 8685b2fc499SJeff Garzik info->description); 8695b2fc499SJeff Garzik goto done; 8705b2fc499SJeff Garzik } 8715b2fc499SJeff Garzik 872a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change in reset() */ 873a88c32aeSMing Lei usbnet_update_max_qlen(dev); 874a88c32aeSMing Lei 8755b2fc499SJeff Garzik // insist peer be connected 8765b2fc499SJeff Garzik if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 877a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval); 8785b2fc499SJeff Garzik goto done; 8795b2fc499SJeff Garzik } 8805b2fc499SJeff Garzik 8815b2fc499SJeff Garzik /* start any status interrupt transfer */ 8825b2fc499SJeff Garzik if (dev->interrupt) { 8836eecdc5fSDan Williams retval = usbnet_status_start(dev, GFP_KERNEL); 8845b2fc499SJeff Garzik if (retval < 0) { 885a475f603SJoe Perches netif_err(dev, ifup, dev->net, 886a475f603SJoe Perches "intr submit %d\n", retval); 8875b2fc499SJeff Garzik goto done; 8885b2fc499SJeff Garzik } 8895b2fc499SJeff Garzik } 8905b2fc499SJeff Garzik 89168972efaSPaul Stewart set_bit(EVENT_DEV_OPEN, &dev->flags); 8925b2fc499SJeff Garzik netif_start_queue (net); 893a475f603SJoe Perches netif_info(dev, ifup, dev->net, 894a475f603SJoe Perches "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", 89560b86755SJoe Perches (int)RX_QLEN(dev), (int)TX_QLEN(dev), 896a475f603SJoe Perches dev->net->mtu, 897a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : 898a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : 899a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : 900a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : 901a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : 902a475f603SJoe Perches "simple"); 9035b2fc499SJeff Garzik 90470c37bf9SBjørn Mork /* reset rx error state */ 90570c37bf9SBjørn Mork dev->pkt_cnt = 0; 90670c37bf9SBjørn Mork dev->pkt_err = 0; 90770c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 90870c37bf9SBjørn Mork 9095b2fc499SJeff Garzik // delay posting reads until we're fully open 9105b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 91169ee472fSOliver Neukum if (info->manage_power) { 91269ee472fSOliver Neukum retval = info->manage_power(dev, 1); 913a1c088e0SOliver Neukum if (retval < 0) { 914a1c088e0SOliver Neukum retval = 0; 915a1c088e0SOliver Neukum set_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 916a1c088e0SOliver Neukum } else { 91769ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 91869ee472fSOliver Neukum } 919a1c088e0SOliver Neukum } 920a11a6544SOliver Neukum return retval; 9215b2fc499SJeff Garzik done: 922a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 923a11a6544SOliver Neukum done_nopm: 9245b2fc499SJeff Garzik return retval; 9255b2fc499SJeff Garzik } 926777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open); 9275b2fc499SJeff Garzik 9285b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 9295b2fc499SJeff Garzik 9305b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but 9315b2fc499SJeff Garzik * they'll probably want to use this base set. 9325b2fc499SJeff Garzik */ 9335b2fc499SJeff Garzik 9345b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) 9355b2fc499SJeff Garzik { 9365b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9375b2fc499SJeff Garzik 9385b2fc499SJeff Garzik if (!dev->mii.mdio_read) 9395b2fc499SJeff Garzik return -EOPNOTSUPP; 9405b2fc499SJeff Garzik 9415b2fc499SJeff Garzik return mii_ethtool_gset(&dev->mii, cmd); 9425b2fc499SJeff Garzik } 9435b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings); 9445b2fc499SJeff Garzik 9455b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) 9465b2fc499SJeff Garzik { 9475b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9485b2fc499SJeff Garzik int retval; 9495b2fc499SJeff Garzik 9505b2fc499SJeff Garzik if (!dev->mii.mdio_write) 9515b2fc499SJeff Garzik return -EOPNOTSUPP; 9525b2fc499SJeff Garzik 9535b2fc499SJeff Garzik retval = mii_ethtool_sset(&dev->mii, cmd); 9545b2fc499SJeff Garzik 9555b2fc499SJeff Garzik /* link speed/duplex might have changed */ 9565b2fc499SJeff Garzik if (dev->driver_info->link_reset) 9575b2fc499SJeff Garzik dev->driver_info->link_reset(dev); 9585b2fc499SJeff Garzik 959a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change in link_reset() */ 960a88c32aeSMing Lei usbnet_update_max_qlen(dev); 961a88c32aeSMing Lei 9625b2fc499SJeff Garzik return retval; 9635b2fc499SJeff Garzik 9645b2fc499SJeff Garzik } 9655b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings); 9665b2fc499SJeff Garzik 9675b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net) 9685b2fc499SJeff Garzik { 9695b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9705b2fc499SJeff Garzik 9715b2fc499SJeff Garzik /* If a check_connect is defined, return its result */ 9725b2fc499SJeff Garzik if (dev->driver_info->check_connect) 9735b2fc499SJeff Garzik return dev->driver_info->check_connect (dev) == 0; 9745b2fc499SJeff Garzik 9755b2fc499SJeff Garzik /* if the device has mii operations, use those */ 9765b2fc499SJeff Garzik if (dev->mii.mdio_read) 9775b2fc499SJeff Garzik return mii_link_ok(&dev->mii); 9785b2fc499SJeff Garzik 97905ffb3e2SBjørn Mork /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ 98005ffb3e2SBjørn Mork return ethtool_op_get_link(net); 9815b2fc499SJeff Garzik } 9825b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link); 9835b2fc499SJeff Garzik 9845b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net) 9855b2fc499SJeff Garzik { 9865b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9875b2fc499SJeff Garzik 9885b2fc499SJeff Garzik if (!dev->mii.mdio_write) 9895b2fc499SJeff Garzik return -EOPNOTSUPP; 9905b2fc499SJeff Garzik 9915b2fc499SJeff Garzik return mii_nway_restart(&dev->mii); 9925b2fc499SJeff Garzik } 9935b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset); 9945b2fc499SJeff Garzik 9955b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 9965b2fc499SJeff Garzik { 9975b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9985b2fc499SJeff Garzik 99986a2f415SPhil Sutter strlcpy (info->driver, dev->driver_name, sizeof info->driver); 100086a2f415SPhil Sutter strlcpy (info->version, DRIVER_VERSION, sizeof info->version); 100186a2f415SPhil Sutter strlcpy (info->fw_version, dev->driver_info->description, 10025b2fc499SJeff Garzik sizeof info->fw_version); 10035b2fc499SJeff Garzik usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 10045b2fc499SJeff Garzik } 10055b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 10065b2fc499SJeff Garzik 10075b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net) 10085b2fc499SJeff Garzik { 10095b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10105b2fc499SJeff Garzik 10115b2fc499SJeff Garzik return dev->msg_enable; 10125b2fc499SJeff Garzik } 10135b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 10145b2fc499SJeff Garzik 10155b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level) 10165b2fc499SJeff Garzik { 10175b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10185b2fc499SJeff Garzik 10195b2fc499SJeff Garzik dev->msg_enable = level; 10205b2fc499SJeff Garzik } 10215b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 10225b2fc499SJeff Garzik 10235b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */ 10240fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = { 10255b2fc499SJeff Garzik .get_settings = usbnet_get_settings, 10265b2fc499SJeff Garzik .set_settings = usbnet_set_settings, 10275b2fc499SJeff Garzik .get_link = usbnet_get_link, 10285b2fc499SJeff Garzik .nway_reset = usbnet_nway_reset, 10295b2fc499SJeff Garzik .get_drvinfo = usbnet_get_drvinfo, 10305b2fc499SJeff Garzik .get_msglevel = usbnet_get_msglevel, 10315b2fc499SJeff Garzik .set_msglevel = usbnet_set_msglevel, 1032123edb18SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 10335b2fc499SJeff Garzik }; 10345b2fc499SJeff Garzik 10355b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 10365b2fc499SJeff Garzik 10374b49f58fSMing Lei static void __handle_link_change(struct usbnet *dev) 10384b49f58fSMing Lei { 10394b49f58fSMing Lei if (!test_bit(EVENT_DEV_OPEN, &dev->flags)) 10404b49f58fSMing Lei return; 10414b49f58fSMing Lei 10424b49f58fSMing Lei if (!netif_carrier_ok(dev->net)) { 10434b49f58fSMing Lei /* kill URBs for reading packets to save bus bandwidth */ 10444b49f58fSMing Lei unlink_urbs(dev, &dev->rxq); 10454b49f58fSMing Lei 10464b49f58fSMing Lei /* 10474b49f58fSMing Lei * tx_timeout will unlink URBs for sending packets and 10484b49f58fSMing Lei * tx queue is stopped by netcore after link becomes off 10494b49f58fSMing Lei */ 10504b49f58fSMing Lei } else { 10514b49f58fSMing Lei /* submitting URBs for reading packets */ 10524b49f58fSMing Lei tasklet_schedule(&dev->bh); 10534b49f58fSMing Lei } 10544b49f58fSMing Lei 1055a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change during link change */ 1056a88c32aeSMing Lei usbnet_update_max_qlen(dev); 1057a88c32aeSMing Lei 10584b49f58fSMing Lei clear_bit(EVENT_LINK_CHANGE, &dev->flags); 10594b49f58fSMing Lei } 10604b49f58fSMing Lei 10615b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd. 10625b2fc499SJeff Garzik * 10635b2fc499SJeff Garzik * NOTE: with 2.5 we could do more of this using completion callbacks, 10645b2fc499SJeff Garzik * especially now that control transfers can be queued. 10655b2fc499SJeff Garzik */ 10665b2fc499SJeff Garzik static void 10675b2fc499SJeff Garzik kevent (struct work_struct *work) 10685b2fc499SJeff Garzik { 10695b2fc499SJeff Garzik struct usbnet *dev = 10705b2fc499SJeff Garzik container_of(work, struct usbnet, kevent); 10715b2fc499SJeff Garzik int status; 10725b2fc499SJeff Garzik 10735b2fc499SJeff Garzik /* usb_clear_halt() needs a thread context */ 10745b2fc499SJeff Garzik if (test_bit (EVENT_TX_HALT, &dev->flags)) { 10755b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 107669ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 107769ee472fSOliver Neukum if (status < 0) 107869ee472fSOliver Neukum goto fail_pipe; 10795b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->out); 108069ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 10818e95a202SJoe Perches if (status < 0 && 10828e95a202SJoe Perches status != -EPIPE && 10838e95a202SJoe Perches status != -ESHUTDOWN) { 10845b2fc499SJeff Garzik if (netif_msg_tx_err (dev)) 108569ee472fSOliver Neukum fail_pipe: 108660b86755SJoe Perches netdev_err(dev->net, "can't clear tx halt, status %d\n", 10875b2fc499SJeff Garzik status); 10885b2fc499SJeff Garzik } else { 10895b2fc499SJeff Garzik clear_bit (EVENT_TX_HALT, &dev->flags); 10905b2fc499SJeff Garzik if (status != -ESHUTDOWN) 10915b2fc499SJeff Garzik netif_wake_queue (dev->net); 10925b2fc499SJeff Garzik } 10935b2fc499SJeff Garzik } 10945b2fc499SJeff Garzik if (test_bit (EVENT_RX_HALT, &dev->flags)) { 10955b2fc499SJeff Garzik unlink_urbs (dev, &dev->rxq); 109669ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 109769ee472fSOliver Neukum if (status < 0) 109869ee472fSOliver Neukum goto fail_halt; 10995b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->in); 110069ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 11018e95a202SJoe Perches if (status < 0 && 11028e95a202SJoe Perches status != -EPIPE && 11038e95a202SJoe Perches status != -ESHUTDOWN) { 11045b2fc499SJeff Garzik if (netif_msg_rx_err (dev)) 110569ee472fSOliver Neukum fail_halt: 110660b86755SJoe Perches netdev_err(dev->net, "can't clear rx halt, status %d\n", 11075b2fc499SJeff Garzik status); 11085b2fc499SJeff Garzik } else { 11095b2fc499SJeff Garzik clear_bit (EVENT_RX_HALT, &dev->flags); 11105b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 11115b2fc499SJeff Garzik } 11125b2fc499SJeff Garzik } 11135b2fc499SJeff Garzik 11145b2fc499SJeff Garzik /* tasklet could resubmit itself forever if memory is tight */ 11155b2fc499SJeff Garzik if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 11165b2fc499SJeff Garzik struct urb *urb = NULL; 1117dacb3975SDavid S. Miller int resched = 1; 11185b2fc499SJeff Garzik 11195b2fc499SJeff Garzik if (netif_running (dev->net)) 11205b2fc499SJeff Garzik urb = usb_alloc_urb (0, GFP_KERNEL); 11215b2fc499SJeff Garzik else 11225b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 11235b2fc499SJeff Garzik if (urb != NULL) { 11245b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 112569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 1126ab60707fSJesper Juhl if (status < 0) { 1127ab60707fSJesper Juhl usb_free_urb(urb); 112869ee472fSOliver Neukum goto fail_lowmem; 1129ab60707fSJesper Juhl } 1130dacb3975SDavid S. Miller if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) 1131dacb3975SDavid S. Miller resched = 0; 113269ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 113369ee472fSOliver Neukum fail_lowmem: 1134dacb3975SDavid S. Miller if (resched) 11355b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 11365b2fc499SJeff Garzik } 11375b2fc499SJeff Garzik } 11385b2fc499SJeff Garzik 11395b2fc499SJeff Garzik if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 11405b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 11415b2fc499SJeff Garzik int retval = 0; 11425b2fc499SJeff Garzik 11435b2fc499SJeff Garzik clear_bit (EVENT_LINK_RESET, &dev->flags); 114469ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 114569ee472fSOliver Neukum if (status < 0) 114669ee472fSOliver Neukum goto skip_reset; 11475b2fc499SJeff Garzik if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 114869ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 114969ee472fSOliver Neukum skip_reset: 115060b86755SJoe Perches netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", 11515b2fc499SJeff Garzik retval, 115260b86755SJoe Perches dev->udev->bus->bus_name, 115360b86755SJoe Perches dev->udev->devpath, 11545b2fc499SJeff Garzik info->description); 115569ee472fSOliver Neukum } else { 115669ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 11575b2fc499SJeff Garzik } 11584b49f58fSMing Lei 11594b49f58fSMing Lei /* handle link change from link resetting */ 11604b49f58fSMing Lei __handle_link_change(dev); 11615b2fc499SJeff Garzik } 11625b2fc499SJeff Garzik 11634b49f58fSMing Lei if (test_bit (EVENT_LINK_CHANGE, &dev->flags)) 11644b49f58fSMing Lei __handle_link_change(dev); 11654b49f58fSMing Lei 11665b2fc499SJeff Garzik if (dev->flags) 116760b86755SJoe Perches netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); 11685b2fc499SJeff Garzik } 11695b2fc499SJeff Garzik 11705b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 11715b2fc499SJeff Garzik 11725b2fc499SJeff Garzik static void tx_complete (struct urb *urb) 11735b2fc499SJeff Garzik { 11745b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 11755b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 11765b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 11775b2fc499SJeff Garzik 11785b2fc499SJeff Garzik if (urb->status == 0) { 1179073285fdSAlexey Orishko if (!(dev->driver_info->flags & FLAG_MULTI_PACKET)) 11807963837fSHerbert Xu dev->net->stats.tx_packets++; 11817963837fSHerbert Xu dev->net->stats.tx_bytes += entry->length; 11825b2fc499SJeff Garzik } else { 11837963837fSHerbert Xu dev->net->stats.tx_errors++; 11845b2fc499SJeff Garzik 11855b2fc499SJeff Garzik switch (urb->status) { 11865b2fc499SJeff Garzik case -EPIPE: 11875b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 11885b2fc499SJeff Garzik break; 11895b2fc499SJeff Garzik 11905b2fc499SJeff Garzik /* software-driven interface shutdown */ 11915b2fc499SJeff Garzik case -ECONNRESET: // async unlink 11925b2fc499SJeff Garzik case -ESHUTDOWN: // hardware gone 11935b2fc499SJeff Garzik break; 11945b2fc499SJeff Garzik 11955b2fc499SJeff Garzik // like rx, tx gets controller i/o faults during khubd delays 11965b2fc499SJeff Garzik // and so it uses the same throttling mechanism. 11975b2fc499SJeff Garzik case -EPROTO: 11985b2fc499SJeff Garzik case -ETIME: 11995b2fc499SJeff Garzik case -EILSEQ: 120069ee472fSOliver Neukum usb_mark_last_busy(dev->udev); 12015b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 12025b2fc499SJeff Garzik mod_timer (&dev->delay, 12035b2fc499SJeff Garzik jiffies + THROTTLE_JIFFIES); 1204a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1205a475f603SJoe Perches "tx throttle %d\n", urb->status); 12065b2fc499SJeff Garzik } 12075b2fc499SJeff Garzik netif_stop_queue (dev->net); 12085b2fc499SJeff Garzik break; 12095b2fc499SJeff Garzik default: 1210a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1211a475f603SJoe Perches "tx err %d\n", entry->urb->status); 12125b2fc499SJeff Garzik break; 12135b2fc499SJeff Garzik } 12145b2fc499SJeff Garzik } 12155b2fc499SJeff Garzik 121669ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 12175b6e9bcdSMing Lei (void) defer_bh(dev, skb, &dev->txq, tx_done); 12185b2fc499SJeff Garzik } 12195b2fc499SJeff Garzik 12205b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 12215b2fc499SJeff Garzik 1222777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net) 12235b2fc499SJeff Garzik { 12245b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 12255b2fc499SJeff Garzik 12265b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 12275b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 12285b2fc499SJeff Garzik 12295b2fc499SJeff Garzik // FIXME: device recovery -- reset? 12305b2fc499SJeff Garzik } 1231777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout); 12325b2fc499SJeff Garzik 12335b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 12345b2fc499SJeff Garzik 123525a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, 123625a79c41SStephen Hemminger struct net_device *net) 12375b2fc499SJeff Garzik { 12385b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 12395b2fc499SJeff Garzik int length; 12405b2fc499SJeff Garzik struct urb *urb = NULL; 12415b2fc499SJeff Garzik struct skb_data *entry; 12425b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 12435b2fc499SJeff Garzik unsigned long flags; 124425a79c41SStephen Hemminger int retval; 12455b2fc499SJeff Garzik 124623ba0799SKonstantin Khlebnikov if (skb) 1247f9b491ecSMichael Riesch skb_tx_timestamp(skb); 1248f9b491ecSMichael Riesch 12495b2fc499SJeff Garzik // some devices want funky USB-level framing, for 12505b2fc499SJeff Garzik // win32 driver (usually) and/or hardware quirks 12515b2fc499SJeff Garzik if (info->tx_fixup) { 12525b2fc499SJeff Garzik skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 12535b2fc499SJeff Garzik if (!skb) { 1254bf414b36SBjørn Mork /* packet collected; minidriver waiting for more */ 1255bf414b36SBjørn Mork if (info->flags & FLAG_MULTI_PACKET) 1256bf414b36SBjørn Mork goto not_drop; 1257a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); 12585b2fc499SJeff Garzik goto drop; 12595b2fc499SJeff Garzik } 12605b2fc499SJeff Garzik } 12615b2fc499SJeff Garzik length = skb->len; 12625b2fc499SJeff Garzik 12635b2fc499SJeff Garzik if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 1264a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "no urb\n"); 12655b2fc499SJeff Garzik goto drop; 12665b2fc499SJeff Garzik } 12675b2fc499SJeff Garzik 12685b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 12695b2fc499SJeff Garzik entry->urb = urb; 12705b2fc499SJeff Garzik entry->dev = dev; 12715b2fc499SJeff Garzik entry->length = length; 12725b2fc499SJeff Garzik 12735b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->out, 12745b2fc499SJeff Garzik skb->data, skb->len, tx_complete, skb); 12755b2fc499SJeff Garzik 12765b2fc499SJeff Garzik /* don't assume the hardware handles USB_ZERO_PACKET 12775b2fc499SJeff Garzik * NOTE: strictly conforming cdc-ether devices should expect 12785b2fc499SJeff Garzik * the ZLP here, but ignore the one-byte packet. 1279073285fdSAlexey Orishko * NOTE2: CDC NCM specification is different from CDC ECM when 1280073285fdSAlexey Orishko * handling ZLP/short packets, so cdc_ncm driver will make short 1281073285fdSAlexey Orishko * packet itself if needed. 12825b2fc499SJeff Garzik */ 1283b4d562e3SElina Pasheva if (length % dev->maxpacket == 0) { 1284b4d562e3SElina Pasheva if (!(info->flags & FLAG_SEND_ZLP)) { 1285073285fdSAlexey Orishko if (!(info->flags & FLAG_MULTI_PACKET)) { 12865b2fc499SJeff Garzik urb->transfer_buffer_length++; 12873e323f3eSPeter Korsgaard if (skb_tailroom(skb)) { 12883e323f3eSPeter Korsgaard skb->data[skb->len] = 0; 12893e323f3eSPeter Korsgaard __skb_put(skb, 1); 12903e323f3eSPeter Korsgaard } 1291073285fdSAlexey Orishko } 1292b4d562e3SElina Pasheva } else 1293b4d562e3SElina Pasheva urb->transfer_flags |= URB_ZERO_PACKET; 12943e323f3eSPeter Korsgaard } 12955b2fc499SJeff Garzik 12965b2fc499SJeff Garzik spin_lock_irqsave(&dev->txq.lock, flags); 129769ee472fSOliver Neukum retval = usb_autopm_get_interface_async(dev->intf); 129869ee472fSOliver Neukum if (retval < 0) { 129969ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 130069ee472fSOliver Neukum goto drop; 130169ee472fSOliver Neukum } 130269ee472fSOliver Neukum 130369ee472fSOliver Neukum #ifdef CONFIG_PM 130469ee472fSOliver Neukum /* if this triggers the device is still a sleep */ 130569ee472fSOliver Neukum if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { 130669ee472fSOliver Neukum /* transmission will be done in resume */ 130769ee472fSOliver Neukum usb_anchor_urb(urb, &dev->deferred); 130869ee472fSOliver Neukum /* no use to process more packets */ 130969ee472fSOliver Neukum netif_stop_queue(net); 131039707c2aSHemant Kumar usb_put_urb(urb); 131169ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 131260b86755SJoe Perches netdev_dbg(dev->net, "Delaying transmission for resumption\n"); 131369ee472fSOliver Neukum goto deferred; 131469ee472fSOliver Neukum } 131569ee472fSOliver Neukum #endif 13165b2fc499SJeff Garzik 13175b2fc499SJeff Garzik switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 13185b2fc499SJeff Garzik case -EPIPE: 13195b2fc499SJeff Garzik netif_stop_queue (net); 13205b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 132169ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 13225b2fc499SJeff Garzik break; 13235b2fc499SJeff Garzik default: 132469ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 1325a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1326a475f603SJoe Perches "tx: submit urb err %d\n", retval); 13275b2fc499SJeff Garzik break; 13285b2fc499SJeff Garzik case 0: 13295b2fc499SJeff Garzik net->trans_start = jiffies; 13305b6e9bcdSMing Lei __usbnet_queue_skb(&dev->txq, skb, tx_start); 13315b2fc499SJeff Garzik if (dev->txq.qlen >= TX_QLEN (dev)) 13325b2fc499SJeff Garzik netif_stop_queue (net); 13335b2fc499SJeff Garzik } 13345b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->txq.lock, flags); 13355b2fc499SJeff Garzik 13365b2fc499SJeff Garzik if (retval) { 1337a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); 13385b2fc499SJeff Garzik drop: 13397963837fSHerbert Xu dev->net->stats.tx_dropped++; 1340073285fdSAlexey Orishko not_drop: 13415b2fc499SJeff Garzik if (skb) 13425b2fc499SJeff Garzik dev_kfree_skb_any (skb); 13435b2fc499SJeff Garzik usb_free_urb (urb); 1344a475f603SJoe Perches } else 1345a475f603SJoe Perches netif_dbg(dev, tx_queued, dev->net, 1346a475f603SJoe Perches "> tx, len %d, type 0x%x\n", length, skb->protocol); 134769ee472fSOliver Neukum #ifdef CONFIG_PM 134869ee472fSOliver Neukum deferred: 134969ee472fSOliver Neukum #endif 135025a79c41SStephen Hemminger return NETDEV_TX_OK; 13515b2fc499SJeff Garzik } 1352777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit); 13535b2fc499SJeff Garzik 135485e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags) 135565841fd5SMing Lei { 135665841fd5SMing Lei struct urb *urb; 135765841fd5SMing Lei int i; 135885e87870SBjørn Mork int ret = 0; 135965841fd5SMing Lei 136065841fd5SMing Lei /* don't refill the queue all at once */ 136165841fd5SMing Lei for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) { 136265841fd5SMing Lei urb = usb_alloc_urb(0, flags); 136365841fd5SMing Lei if (urb != NULL) { 136485e87870SBjørn Mork ret = rx_submit(dev, urb, flags); 136585e87870SBjørn Mork if (ret) 136685e87870SBjørn Mork goto err; 136785e87870SBjørn Mork } else { 136885e87870SBjørn Mork ret = -ENOMEM; 136985e87870SBjørn Mork goto err; 137065841fd5SMing Lei } 137165841fd5SMing Lei } 137285e87870SBjørn Mork err: 137385e87870SBjørn Mork return ret; 137465841fd5SMing Lei } 137565841fd5SMing Lei 13765b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 13775b2fc499SJeff Garzik 13785b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer 13795b2fc499SJeff Garzik 13805b2fc499SJeff Garzik static void usbnet_bh (unsigned long param) 13815b2fc499SJeff Garzik { 13825b2fc499SJeff Garzik struct usbnet *dev = (struct usbnet *) param; 13835b2fc499SJeff Garzik struct sk_buff *skb; 13845b2fc499SJeff Garzik struct skb_data *entry; 13855b2fc499SJeff Garzik 13865b2fc499SJeff Garzik while ((skb = skb_dequeue (&dev->done))) { 13875b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 13885b2fc499SJeff Garzik switch (entry->state) { 13895b2fc499SJeff Garzik case rx_done: 13905b2fc499SJeff Garzik entry->state = rx_cleanup; 13915b2fc499SJeff Garzik rx_process (dev, skb); 13925b2fc499SJeff Garzik continue; 13935b2fc499SJeff Garzik case tx_done: 13945b2fc499SJeff Garzik case rx_cleanup: 13955b2fc499SJeff Garzik usb_free_urb (entry->urb); 13965b2fc499SJeff Garzik dev_kfree_skb (skb); 13975b2fc499SJeff Garzik continue; 13985b2fc499SJeff Garzik default: 139960b86755SJoe Perches netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); 14005b2fc499SJeff Garzik } 14015b2fc499SJeff Garzik } 14025b2fc499SJeff Garzik 140370c37bf9SBjørn Mork /* restart RX again after disabling due to high error rate */ 140470c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 140570c37bf9SBjørn Mork 14065b2fc499SJeff Garzik // waiting for all pending urbs to complete? 14075b2fc499SJeff Garzik if (dev->wait) { 14085b2fc499SJeff Garzik if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { 14095b2fc499SJeff Garzik wake_up (dev->wait); 14105b2fc499SJeff Garzik } 14115b2fc499SJeff Garzik 14125b2fc499SJeff Garzik // or are we maybe short a few urbs? 14138e95a202SJoe Perches } else if (netif_running (dev->net) && 14148e95a202SJoe Perches netif_device_present (dev->net) && 14154b49f58fSMing Lei netif_carrier_ok(dev->net) && 14168e95a202SJoe Perches !timer_pending (&dev->delay) && 14178e95a202SJoe Perches !test_bit (EVENT_RX_HALT, &dev->flags)) { 14185b2fc499SJeff Garzik int temp = dev->rxq.qlen; 14195b2fc499SJeff Garzik 142065841fd5SMing Lei if (temp < RX_QLEN(dev)) { 142185e87870SBjørn Mork if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK) 142285e87870SBjørn Mork return; 1423a475f603SJoe Perches if (temp != dev->rxq.qlen) 1424a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1425a475f603SJoe Perches "rxqlen %d --> %d\n", 14265b2fc499SJeff Garzik temp, dev->rxq.qlen); 142765841fd5SMing Lei if (dev->rxq.qlen < RX_QLEN(dev)) 14285b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 14295b2fc499SJeff Garzik } 14305b2fc499SJeff Garzik if (dev->txq.qlen < TX_QLEN (dev)) 14315b2fc499SJeff Garzik netif_wake_queue (dev->net); 14325b2fc499SJeff Garzik } 14335b2fc499SJeff Garzik } 14345b2fc499SJeff Garzik 14355b2fc499SJeff Garzik 14365b2fc499SJeff Garzik /*------------------------------------------------------------------------- 14375b2fc499SJeff Garzik * 14385b2fc499SJeff Garzik * USB Device Driver support 14395b2fc499SJeff Garzik * 14405b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 14415b2fc499SJeff Garzik 14425b2fc499SJeff Garzik // precondition: never called in_interrupt 14435b2fc499SJeff Garzik 14445b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf) 14455b2fc499SJeff Garzik { 14465b2fc499SJeff Garzik struct usbnet *dev; 14475b2fc499SJeff Garzik struct usb_device *xdev; 14485b2fc499SJeff Garzik struct net_device *net; 14495b2fc499SJeff Garzik 14505b2fc499SJeff Garzik dev = usb_get_intfdata(intf); 14515b2fc499SJeff Garzik usb_set_intfdata(intf, NULL); 14525b2fc499SJeff Garzik if (!dev) 14535b2fc499SJeff Garzik return; 14545b2fc499SJeff Garzik 14555b2fc499SJeff Garzik xdev = interface_to_usbdev (intf); 14565b2fc499SJeff Garzik 1457a475f603SJoe Perches netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", 14585b2fc499SJeff Garzik intf->dev.driver->name, 14595b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 14605b2fc499SJeff Garzik dev->driver_info->description); 14615b2fc499SJeff Garzik 14625b2fc499SJeff Garzik net = dev->net; 14635b2fc499SJeff Garzik unregister_netdev (net); 14645b2fc499SJeff Garzik 146523f333a2STejun Heo cancel_work_sync(&dev->kevent); 14665b2fc499SJeff Garzik 146739707c2aSHemant Kumar usb_scuttle_anchored_urbs(&dev->deferred); 146839707c2aSHemant Kumar 14695b2fc499SJeff Garzik if (dev->driver_info->unbind) 14705b2fc499SJeff Garzik dev->driver_info->unbind (dev, intf); 14715b2fc499SJeff Garzik 147268972efaSPaul Stewart usb_kill_urb(dev->interrupt); 147368972efaSPaul Stewart usb_free_urb(dev->interrupt); 147468972efaSPaul Stewart 14755b2fc499SJeff Garzik free_netdev(net); 14765b2fc499SJeff Garzik } 14775b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect); 14785b2fc499SJeff Garzik 1479777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = { 1480777baa47SStephen Hemminger .ndo_open = usbnet_open, 1481777baa47SStephen Hemminger .ndo_stop = usbnet_stop, 1482777baa47SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 1483777baa47SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 1484777baa47SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu, 1485777baa47SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1486777baa47SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1487777baa47SStephen Hemminger }; 14885b2fc499SJeff Garzik 14895b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 14905b2fc499SJeff Garzik 14915b2fc499SJeff Garzik // precondition: never called in_interrupt 14925b2fc499SJeff Garzik 1493225794f8SMarcel Holtmann static struct device_type wlan_type = { 1494225794f8SMarcel Holtmann .name = "wlan", 1495225794f8SMarcel Holtmann }; 1496225794f8SMarcel Holtmann 1497225794f8SMarcel Holtmann static struct device_type wwan_type = { 1498225794f8SMarcel Holtmann .name = "wwan", 1499225794f8SMarcel Holtmann }; 1500225794f8SMarcel Holtmann 15015b2fc499SJeff Garzik int 15025b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 15035b2fc499SJeff Garzik { 15045b2fc499SJeff Garzik struct usbnet *dev; 15055b2fc499SJeff Garzik struct net_device *net; 15065b2fc499SJeff Garzik struct usb_host_interface *interface; 15075b2fc499SJeff Garzik struct driver_info *info; 15085b2fc499SJeff Garzik struct usb_device *xdev; 15095b2fc499SJeff Garzik int status; 15105b2fc499SJeff Garzik const char *name; 1511b0786b43SMing Lei struct usb_driver *driver = to_usb_driver(udev->dev.driver); 1512b0786b43SMing Lei 1513b0786b43SMing Lei /* usbnet already took usb runtime pm, so have to enable the feature 1514b0786b43SMing Lei * for usb interface, otherwise usb_autopm_get_interface may return 151598f541c6SAlan Stern * failure if RUNTIME_PM is enabled. 1516b0786b43SMing Lei */ 1517b0786b43SMing Lei if (!driver->supports_autosuspend) { 1518b0786b43SMing Lei driver->supports_autosuspend = 1; 1519b0786b43SMing Lei pm_runtime_enable(&udev->dev); 1520b0786b43SMing Lei } 15215b2fc499SJeff Garzik 15225b2fc499SJeff Garzik name = udev->dev.driver->name; 15235b2fc499SJeff Garzik info = (struct driver_info *) prod->driver_info; 15245b2fc499SJeff Garzik if (!info) { 15255b2fc499SJeff Garzik dev_dbg (&udev->dev, "blacklisted by %s\n", name); 15265b2fc499SJeff Garzik return -ENODEV; 15275b2fc499SJeff Garzik } 15285b2fc499SJeff Garzik xdev = interface_to_usbdev (udev); 15295b2fc499SJeff Garzik interface = udev->cur_altsetting; 15305b2fc499SJeff Garzik 15315b2fc499SJeff Garzik status = -ENOMEM; 15325b2fc499SJeff Garzik 15335b2fc499SJeff Garzik // set up our own records 15345b2fc499SJeff Garzik net = alloc_etherdev(sizeof(*dev)); 153541de8d4cSJoe Perches if (!net) 15365b2fc499SJeff Garzik goto out; 15375b2fc499SJeff Garzik 15380dacca73SBen Hutchings /* netdev_printk() needs this so do it as early as possible */ 15390dacca73SBen Hutchings SET_NETDEV_DEV(net, &udev->dev); 15400dacca73SBen Hutchings 15415b2fc499SJeff Garzik dev = netdev_priv(net); 15425b2fc499SJeff Garzik dev->udev = xdev; 1543a11a6544SOliver Neukum dev->intf = udev; 15445b2fc499SJeff Garzik dev->driver_info = info; 15455b2fc499SJeff Garzik dev->driver_name = name; 15465b2fc499SJeff Garzik dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 15475b2fc499SJeff Garzik | NETIF_MSG_PROBE | NETIF_MSG_LINK); 15485b2fc499SJeff Garzik skb_queue_head_init (&dev->rxq); 15495b2fc499SJeff Garzik skb_queue_head_init (&dev->txq); 15505b2fc499SJeff Garzik skb_queue_head_init (&dev->done); 15517834ddbcSJussi Kivilinna skb_queue_head_init(&dev->rxq_pause); 15525b2fc499SJeff Garzik dev->bh.func = usbnet_bh; 15535b2fc499SJeff Garzik dev->bh.data = (unsigned long) dev; 15545b2fc499SJeff Garzik INIT_WORK (&dev->kevent, kevent); 155569ee472fSOliver Neukum init_usb_anchor(&dev->deferred); 15565b2fc499SJeff Garzik dev->delay.function = usbnet_bh; 15575b2fc499SJeff Garzik dev->delay.data = (unsigned long) dev; 15585b2fc499SJeff Garzik init_timer (&dev->delay); 15595b2fc499SJeff Garzik mutex_init (&dev->phy_mutex); 15606eecdc5fSDan Williams mutex_init(&dev->interrupt_mutex); 15616eecdc5fSDan Williams dev->interrupt_count = 0; 15625b2fc499SJeff Garzik 15635b2fc499SJeff Garzik dev->net = net; 15645b2fc499SJeff Garzik strcpy (net->name, "usb%d"); 15655b2fc499SJeff Garzik memcpy (net->dev_addr, node_id, sizeof node_id); 15665b2fc499SJeff Garzik 15675b2fc499SJeff Garzik /* rx and tx sides can use different message sizes; 15685b2fc499SJeff Garzik * bind() should set rx_urb_size in that case. 15695b2fc499SJeff Garzik */ 15705b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 15715b2fc499SJeff Garzik #if 0 15725b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures 15735b2fc499SJeff Garzik // possible with some EHCI controllers 15746a35528aSYang Hongyang if (dma_supported (&udev->dev, DMA_BIT_MASK(64))) 15755b2fc499SJeff Garzik net->features |= NETIF_F_HIGHDMA; 15765b2fc499SJeff Garzik #endif 15775b2fc499SJeff Garzik 1578777baa47SStephen Hemminger net->netdev_ops = &usbnet_netdev_ops; 1579777baa47SStephen Hemminger net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 15805b2fc499SJeff Garzik net->ethtool_ops = &usbnet_ethtool_ops; 15815b2fc499SJeff Garzik 15825b2fc499SJeff Garzik // allow device-specific bind/init procedures 15835b2fc499SJeff Garzik // NOTE net->name still not usable ... 15845b2fc499SJeff Garzik if (info->bind) { 15855b2fc499SJeff Garzik status = info->bind (dev, udev); 15865b2fc499SJeff Garzik if (status < 0) 15875b2fc499SJeff Garzik goto out1; 15885b2fc499SJeff Garzik 15895b2fc499SJeff Garzik // heuristic: "usb%d" for links we know are two-host, 15905b2fc499SJeff Garzik // else "eth%d" when there's reasonable doubt. userspace 15915b2fc499SJeff Garzik // can rename the link if it knows better. 15928e95a202SJoe Perches if ((dev->driver_info->flags & FLAG_ETHER) != 0 && 1593c261344dSArnd Bergmann ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 || 1594c261344dSArnd Bergmann (net->dev_addr [0] & 0x02) == 0)) 15955b2fc499SJeff Garzik strcpy (net->name, "eth%d"); 15966e3bbcc5SJussi Kivilinna /* WLAN devices should always be named "wlan%d" */ 15976e3bbcc5SJussi Kivilinna if ((dev->driver_info->flags & FLAG_WLAN) != 0) 15986e3bbcc5SJussi Kivilinna strcpy(net->name, "wlan%d"); 1599e1e499eeSMarcel Holtmann /* WWAN devices should always be named "wwan%d" */ 1600e1e499eeSMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1601e1e499eeSMarcel Holtmann strcpy(net->name, "wwan%d"); 16025b2fc499SJeff Garzik 16036509141fSWei Shuai /* devices that cannot do ARP */ 16046509141fSWei Shuai if ((dev->driver_info->flags & FLAG_NOARP) != 0) 16056509141fSWei Shuai net->flags |= IFF_NOARP; 16066509141fSWei Shuai 16075b2fc499SJeff Garzik /* maybe the remote can't receive an Ethernet MTU */ 16085b2fc499SJeff Garzik if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 16095b2fc499SJeff Garzik net->mtu = dev->hard_mtu - net->hard_header_len; 16105b2fc499SJeff Garzik } else if (!info->in || !info->out) 16115b2fc499SJeff Garzik status = usbnet_get_endpoints (dev, udev); 16125b2fc499SJeff Garzik else { 16135b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (xdev, info->in); 16145b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (xdev, info->out); 16155b2fc499SJeff Garzik if (!(info->flags & FLAG_NO_SETINT)) 16165b2fc499SJeff Garzik status = usb_set_interface (xdev, 16175b2fc499SJeff Garzik interface->desc.bInterfaceNumber, 16185b2fc499SJeff Garzik interface->desc.bAlternateSetting); 16195b2fc499SJeff Garzik else 16205b2fc499SJeff Garzik status = 0; 16215b2fc499SJeff Garzik 16225b2fc499SJeff Garzik } 16239514bfe5SPeter Korsgaard if (status >= 0 && dev->status) 16245b2fc499SJeff Garzik status = init_status (dev, udev); 16255b2fc499SJeff Garzik if (status < 0) 16265b2fc499SJeff Garzik goto out3; 16275b2fc499SJeff Garzik 16285b2fc499SJeff Garzik if (!dev->rx_urb_size) 16295b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 16305b2fc499SJeff Garzik dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); 16315b2fc499SJeff Garzik 1632225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1633225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wlan_type); 1634225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1635225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wwan_type); 1636225794f8SMarcel Holtmann 1637a88c32aeSMing Lei /* initialize max rx_qlen and tx_qlen */ 1638a88c32aeSMing Lei usbnet_update_max_qlen(dev); 1639a88c32aeSMing Lei 16405b2fc499SJeff Garzik status = register_netdev (net); 16415b2fc499SJeff Garzik if (status) 1642a4723848Stom.leiming@gmail.com goto out4; 1643a475f603SJoe Perches netif_info(dev, probe, dev->net, 1644a475f603SJoe Perches "register '%s' at usb-%s-%s, %s, %pM\n", 16455b2fc499SJeff Garzik udev->dev.driver->name, 16465b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 16475b2fc499SJeff Garzik dev->driver_info->description, 1648e174961cSJohannes Berg net->dev_addr); 16495b2fc499SJeff Garzik 16505b2fc499SJeff Garzik // ok, it's ready to go. 16515b2fc499SJeff Garzik usb_set_intfdata (udev, dev); 16525b2fc499SJeff Garzik 16535b2fc499SJeff Garzik netif_device_attach (net); 16545b2fc499SJeff Garzik 165537e8273cSBen Hutchings if (dev->driver_info->flags & FLAG_LINK_INTR) 16560162c554SMing Lei usbnet_link_change(dev, 0, 0); 165737e8273cSBen Hutchings 16585b2fc499SJeff Garzik return 0; 16595b2fc499SJeff Garzik 1660a4723848Stom.leiming@gmail.com out4: 1661a4723848Stom.leiming@gmail.com usb_free_urb(dev->interrupt); 16625b2fc499SJeff Garzik out3: 16635b2fc499SJeff Garzik if (info->unbind) 16645b2fc499SJeff Garzik info->unbind (dev, udev); 16655b2fc499SJeff Garzik out1: 16665b2fc499SJeff Garzik free_netdev(net); 16675b2fc499SJeff Garzik out: 16685b2fc499SJeff Garzik return status; 16695b2fc499SJeff Garzik } 16705b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe); 16715b2fc499SJeff Garzik 16725b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 16735b2fc499SJeff Garzik 167436433127SOliver Neukum /* 167536433127SOliver Neukum * suspend the whole driver as soon as the first interface is suspended 167636433127SOliver Neukum * resume only when the last interface is resumed 16775b2fc499SJeff Garzik */ 16785b2fc499SJeff Garzik 16795b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 16805b2fc499SJeff Garzik { 16815b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 16825b2fc499SJeff Garzik 168336433127SOliver Neukum if (!dev->suspend_count++) { 168469ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 168569ee472fSOliver Neukum /* don't autosuspend while transmitting */ 16865b1b0b81SAlan Stern if (dev->txq.qlen && PMSG_IS_AUTO(message)) { 16875eeb3132SMing Lei dev->suspend_count--; 168869ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 168969ee472fSOliver Neukum return -EBUSY; 169069ee472fSOliver Neukum } else { 169169ee472fSOliver Neukum set_bit(EVENT_DEV_ASLEEP, &dev->flags); 169269ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 169369ee472fSOliver Neukum } 1694a11a6544SOliver Neukum /* 1695a11a6544SOliver Neukum * accelerate emptying of the rx and queues, to avoid 16965b2fc499SJeff Garzik * having everything error out. 16975b2fc499SJeff Garzik */ 16985b2fc499SJeff Garzik netif_device_detach (dev->net); 169969ee472fSOliver Neukum usbnet_terminate_urbs(dev); 17006eecdc5fSDan Williams __usbnet_status_stop_force(dev); 170169ee472fSOliver Neukum 1702a11a6544SOliver Neukum /* 1703a11a6544SOliver Neukum * reattach so runtime management can use and 1704a11a6544SOliver Neukum * wake the device 1705a11a6544SOliver Neukum */ 1706a11a6544SOliver Neukum netif_device_attach (dev->net); 170736433127SOliver Neukum } 17085b2fc499SJeff Garzik return 0; 17095b2fc499SJeff Garzik } 17105b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend); 17115b2fc499SJeff Garzik 17125b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf) 17135b2fc499SJeff Garzik { 17145b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 171569ee472fSOliver Neukum struct sk_buff *skb; 171669ee472fSOliver Neukum struct urb *res; 171769ee472fSOliver Neukum int retval; 17185b2fc499SJeff Garzik 171969ee472fSOliver Neukum if (!--dev->suspend_count) { 17206eecdc5fSDan Williams /* resume interrupt URB if it was previously submitted */ 17216eecdc5fSDan Williams __usbnet_status_start_force(dev, GFP_NOIO); 172268972efaSPaul Stewart 172369ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 172469ee472fSOliver Neukum while ((res = usb_get_from_anchor(&dev->deferred))) { 172569ee472fSOliver Neukum 172669ee472fSOliver Neukum skb = (struct sk_buff *)res->context; 172769ee472fSOliver Neukum retval = usb_submit_urb(res, GFP_ATOMIC); 172869ee472fSOliver Neukum if (retval < 0) { 172969ee472fSOliver Neukum dev_kfree_skb_any(skb); 173069ee472fSOliver Neukum usb_free_urb(res); 173169ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 173269ee472fSOliver Neukum } else { 173369ee472fSOliver Neukum dev->net->trans_start = jiffies; 173469ee472fSOliver Neukum __skb_queue_tail(&dev->txq, skb); 173569ee472fSOliver Neukum } 173669ee472fSOliver Neukum } 173769ee472fSOliver Neukum 173869ee472fSOliver Neukum smp_mb(); 173969ee472fSOliver Neukum clear_bit(EVENT_DEV_ASLEEP, &dev->flags); 174069ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 174175bd0cbdSMing Lei 174275bd0cbdSMing Lei if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { 174365841fd5SMing Lei /* handle remote wakeup ASAP */ 174465841fd5SMing Lei if (!dev->wait && 174565841fd5SMing Lei netif_device_present(dev->net) && 174665841fd5SMing Lei !timer_pending(&dev->delay) && 174765841fd5SMing Lei !test_bit(EVENT_RX_HALT, &dev->flags)) 1748ab6f148dSOliver Neukum rx_alloc_submit(dev, GFP_NOIO); 174965841fd5SMing Lei 175069ee472fSOliver Neukum if (!(dev->txq.qlen >= TX_QLEN(dev))) 17511aa9bc5bSAlexey Orishko netif_tx_wake_all_queues(dev->net); 17525b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 175369ee472fSOliver Neukum } 175475bd0cbdSMing Lei } 17555d9d01a3SOliver Neukum 17565d9d01a3SOliver Neukum if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) 17575d9d01a3SOliver Neukum usb_autopm_get_interface_no_resume(intf); 17585d9d01a3SOliver Neukum 17595b2fc499SJeff Garzik return 0; 17605b2fc499SJeff Garzik } 17615b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume); 17625b2fc499SJeff Garzik 17635d9d01a3SOliver Neukum /* 17645d9d01a3SOliver Neukum * Either a subdriver implements manage_power, then it is assumed to always 17655d9d01a3SOliver Neukum * be ready to be suspended or it reports the readiness to be suspended 17665d9d01a3SOliver Neukum * explicitly 17675d9d01a3SOliver Neukum */ 17685d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev) 17695d9d01a3SOliver Neukum { 17705d9d01a3SOliver Neukum if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) { 17715d9d01a3SOliver Neukum dev->intf->needs_remote_wakeup = 1; 17725d9d01a3SOliver Neukum usb_autopm_put_interface_async(dev->intf); 17735d9d01a3SOliver Neukum } 17745d9d01a3SOliver Neukum } 17755d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle); 17765b2fc499SJeff Garzik 17772dd7c8cfSOliver Neukum /* 17782dd7c8cfSOliver Neukum * For devices that can do without special commands 17792dd7c8cfSOliver Neukum */ 17802dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on) 17812dd7c8cfSOliver Neukum { 17822dd7c8cfSOliver Neukum dev->intf->needs_remote_wakeup = on; 17832dd7c8cfSOliver Neukum return 0; 17842dd7c8cfSOliver Neukum } 17852dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power); 17862dd7c8cfSOliver Neukum 1787ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset) 1788ac64995dSMing Lei { 1789ac64995dSMing Lei /* update link after link is reseted */ 1790ac64995dSMing Lei if (link && !need_reset) 1791ac64995dSMing Lei netif_carrier_on(dev->net); 1792ac64995dSMing Lei else 1793ac64995dSMing Lei netif_carrier_off(dev->net); 1794ac64995dSMing Lei 1795ac64995dSMing Lei if (need_reset && link) 1796ac64995dSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_RESET); 17974b49f58fSMing Lei else 17984b49f58fSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_CHANGE); 1799ac64995dSMing Lei } 1800ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change); 1801ac64995dSMing Lei 18025b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 18030547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 1804877bd862SMing Lei u16 value, u16 index, void *data, u16 size) 1805877bd862SMing Lei { 1806877bd862SMing Lei void *buf = NULL; 1807877bd862SMing Lei int err = -ENOMEM; 1808877bd862SMing Lei 1809877bd862SMing Lei netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x" 1810877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1811877bd862SMing Lei cmd, reqtype, value, index, size); 1812877bd862SMing Lei 1813877bd862SMing Lei if (data) { 1814877bd862SMing Lei buf = kmalloc(size, GFP_KERNEL); 1815877bd862SMing Lei if (!buf) 1816877bd862SMing Lei goto out; 1817877bd862SMing Lei } 1818877bd862SMing Lei 1819877bd862SMing Lei err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 1820877bd862SMing Lei cmd, reqtype, value, index, buf, size, 1821877bd862SMing Lei USB_CTRL_GET_TIMEOUT); 1822877bd862SMing Lei if (err > 0 && err <= size) 1823877bd862SMing Lei memcpy(data, buf, err); 1824877bd862SMing Lei kfree(buf); 1825877bd862SMing Lei out: 1826877bd862SMing Lei return err; 1827877bd862SMing Lei } 1828877bd862SMing Lei 18290547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 18300547fad2SMing Lei u16 value, u16 index, const void *data, 18310547fad2SMing Lei u16 size) 1832877bd862SMing Lei { 1833877bd862SMing Lei void *buf = NULL; 1834877bd862SMing Lei int err = -ENOMEM; 1835877bd862SMing Lei 1836877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1837877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1838877bd862SMing Lei cmd, reqtype, value, index, size); 1839877bd862SMing Lei 1840877bd862SMing Lei if (data) { 1841877bd862SMing Lei buf = kmemdup(data, size, GFP_KERNEL); 1842877bd862SMing Lei if (!buf) 1843877bd862SMing Lei goto out; 1844877bd862SMing Lei } 1845877bd862SMing Lei 1846877bd862SMing Lei err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 1847877bd862SMing Lei cmd, reqtype, value, index, buf, size, 1848877bd862SMing Lei USB_CTRL_SET_TIMEOUT); 1849877bd862SMing Lei kfree(buf); 1850877bd862SMing Lei 1851877bd862SMing Lei out: 1852877bd862SMing Lei return err; 1853877bd862SMing Lei } 18540547fad2SMing Lei 18550547fad2SMing Lei /* 18560547fad2SMing Lei * The function can't be called inside suspend/resume callback, 18570547fad2SMing Lei * otherwise deadlock will be caused. 18580547fad2SMing Lei */ 18590547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 18600547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 18610547fad2SMing Lei { 18626f0a0986SMing Lei int ret; 18636f0a0986SMing Lei 18646f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 18656f0a0986SMing Lei return -ENODEV; 18666f0a0986SMing Lei ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index, 18670547fad2SMing Lei data, size); 18686f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 18696f0a0986SMing Lei return ret; 18700547fad2SMing Lei } 18710547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd); 18720547fad2SMing Lei 18730547fad2SMing Lei /* 18740547fad2SMing Lei * The function can't be called inside suspend/resume callback, 18750547fad2SMing Lei * otherwise deadlock will be caused. 18760547fad2SMing Lei */ 18770547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 18780547fad2SMing Lei u16 value, u16 index, const void *data, u16 size) 18790547fad2SMing Lei { 18806f0a0986SMing Lei int ret; 18816f0a0986SMing Lei 18826f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 18836f0a0986SMing Lei return -ENODEV; 18846f0a0986SMing Lei ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index, 18850547fad2SMing Lei data, size); 18866f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 18876f0a0986SMing Lei return ret; 18880547fad2SMing Lei } 1889877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd); 1890877bd862SMing Lei 18910547fad2SMing Lei /* 18920547fad2SMing Lei * The function can be called inside suspend/resume callback safely 18930547fad2SMing Lei * and should only be called by suspend/resume callback generally. 18940547fad2SMing Lei */ 18950547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 18960547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 18970547fad2SMing Lei { 18980547fad2SMing Lei return __usbnet_read_cmd(dev, cmd, reqtype, value, index, 18990547fad2SMing Lei data, size); 19000547fad2SMing Lei } 19010547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm); 19020547fad2SMing Lei 19030547fad2SMing Lei /* 19040547fad2SMing Lei * The function can be called inside suspend/resume callback safely 19050547fad2SMing Lei * and should only be called by suspend/resume callback generally. 19060547fad2SMing Lei */ 19070547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 19080547fad2SMing Lei u16 value, u16 index, const void *data, 19090547fad2SMing Lei u16 size) 19100547fad2SMing Lei { 19110547fad2SMing Lei return __usbnet_write_cmd(dev, cmd, reqtype, value, index, 19120547fad2SMing Lei data, size); 19130547fad2SMing Lei } 19140547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm); 19150547fad2SMing Lei 1916877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb) 1917877bd862SMing Lei { 1918877bd862SMing Lei struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; 1919877bd862SMing Lei int status = urb->status; 1920877bd862SMing Lei 1921877bd862SMing Lei if (status < 0) 1922877bd862SMing Lei dev_dbg(&urb->dev->dev, "%s failed with %d", 1923877bd862SMing Lei __func__, status); 1924877bd862SMing Lei 1925877bd862SMing Lei kfree(req); 1926877bd862SMing Lei usb_free_urb(urb); 1927877bd862SMing Lei } 1928877bd862SMing Lei 19290547fad2SMing Lei /* 19300547fad2SMing Lei * The caller must make sure that device can't be put into suspend 19310547fad2SMing Lei * state until the control URB completes. 19320547fad2SMing Lei */ 1933877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype, 1934877bd862SMing Lei u16 value, u16 index, const void *data, u16 size) 1935877bd862SMing Lei { 1936877bd862SMing Lei struct usb_ctrlrequest *req = NULL; 1937877bd862SMing Lei struct urb *urb; 1938877bd862SMing Lei int err = -ENOMEM; 1939877bd862SMing Lei void *buf = NULL; 1940877bd862SMing Lei 1941877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1942877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1943877bd862SMing Lei cmd, reqtype, value, index, size); 1944877bd862SMing Lei 1945877bd862SMing Lei urb = usb_alloc_urb(0, GFP_ATOMIC); 1946877bd862SMing Lei if (!urb) { 1947877bd862SMing Lei netdev_err(dev->net, "Error allocating URB in" 1948877bd862SMing Lei " %s!\n", __func__); 1949877bd862SMing Lei goto fail; 1950877bd862SMing Lei } 1951877bd862SMing Lei 1952877bd862SMing Lei if (data) { 1953877bd862SMing Lei buf = kmemdup(data, size, GFP_ATOMIC); 1954877bd862SMing Lei if (!buf) { 1955877bd862SMing Lei netdev_err(dev->net, "Error allocating buffer" 1956877bd862SMing Lei " in %s!\n", __func__); 1957877bd862SMing Lei goto fail_free; 1958877bd862SMing Lei } 1959877bd862SMing Lei } 1960877bd862SMing Lei 1961877bd862SMing Lei req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); 196238673c82SJoe Perches if (!req) 1963877bd862SMing Lei goto fail_free_buf; 1964877bd862SMing Lei 1965877bd862SMing Lei req->bRequestType = reqtype; 1966877bd862SMing Lei req->bRequest = cmd; 1967877bd862SMing Lei req->wValue = cpu_to_le16(value); 1968877bd862SMing Lei req->wIndex = cpu_to_le16(index); 1969877bd862SMing Lei req->wLength = cpu_to_le16(size); 1970877bd862SMing Lei 1971877bd862SMing Lei usb_fill_control_urb(urb, dev->udev, 1972877bd862SMing Lei usb_sndctrlpipe(dev->udev, 0), 1973877bd862SMing Lei (void *)req, buf, size, 1974877bd862SMing Lei usbnet_async_cmd_cb, req); 1975877bd862SMing Lei urb->transfer_flags |= URB_FREE_BUFFER; 1976877bd862SMing Lei 1977877bd862SMing Lei err = usb_submit_urb(urb, GFP_ATOMIC); 1978877bd862SMing Lei if (err < 0) { 1979877bd862SMing Lei netdev_err(dev->net, "Error submitting the control" 1980877bd862SMing Lei " message: status=%d\n", err); 1981877bd862SMing Lei goto fail_free; 1982877bd862SMing Lei } 1983877bd862SMing Lei return 0; 1984877bd862SMing Lei 1985877bd862SMing Lei fail_free_buf: 1986877bd862SMing Lei kfree(buf); 1987877bd862SMing Lei fail_free: 1988877bd862SMing Lei kfree(req); 1989877bd862SMing Lei usb_free_urb(urb); 1990877bd862SMing Lei fail: 1991877bd862SMing Lei return err; 1992877bd862SMing Lei 1993877bd862SMing Lei } 1994877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async); 1995877bd862SMing Lei /*-------------------------------------------------------------------------*/ 19965b2fc499SJeff Garzik 19975b2fc499SJeff Garzik static int __init usbnet_init(void) 19985b2fc499SJeff Garzik { 1999c582a950SThiago Farina /* Compiler should optimize this out. */ 2000c582a950SThiago Farina BUILD_BUG_ON( 2001c582a950SThiago Farina FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data)); 20025b2fc499SJeff Garzik 2003c7e12eadSJoe Perches eth_random_addr(node_id); 20045b2fc499SJeff Garzik return 0; 20055b2fc499SJeff Garzik } 20065b2fc499SJeff Garzik module_init(usbnet_init); 20075b2fc499SJeff Garzik 20085b2fc499SJeff Garzik static void __exit usbnet_exit(void) 20095b2fc499SJeff Garzik { 20105b2fc499SJeff Garzik } 20115b2fc499SJeff Garzik module_exit(usbnet_exit); 20125b2fc499SJeff Garzik 20135b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell"); 20145b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework"); 20155b2fc499SJeff Garzik MODULE_LICENSE("GPL"); 2016