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 179cb00073SJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 185b2fc499SJeff Garzik */ 195b2fc499SJeff Garzik 205b2fc499SJeff Garzik /* 215b2fc499SJeff Garzik * This is a generic "USB networking" framework that works with several 225b2fc499SJeff Garzik * kinds of full and high speed networking devices: host-to-host cables, 235b2fc499SJeff Garzik * smart usb peripherals, and actual Ethernet adapters. 245b2fc499SJeff Garzik * 255b2fc499SJeff Garzik * These devices usually differ in terms of control protocols (if they 265b2fc499SJeff Garzik * even have one!) and sometimes they define new framing to wrap or batch 275b2fc499SJeff Garzik * Ethernet packets. Otherwise, they talk to USB pretty much the same, 285b2fc499SJeff Garzik * so interface (un)binding, endpoint I/O queues, fault handling, and other 295b2fc499SJeff Garzik * issues can usefully be addressed by this framework. 305b2fc499SJeff Garzik */ 315b2fc499SJeff Garzik 325b2fc499SJeff Garzik // #define DEBUG // error path messages, extra info 335b2fc499SJeff Garzik // #define VERBOSE // more; success messages 345b2fc499SJeff Garzik 355b2fc499SJeff Garzik #include <linux/module.h> 365b2fc499SJeff Garzik #include <linux/init.h> 375b2fc499SJeff Garzik #include <linux/netdevice.h> 385b2fc499SJeff Garzik #include <linux/etherdevice.h> 3903ad032bSPeter Holik #include <linux/ctype.h> 405b2fc499SJeff Garzik #include <linux/ethtool.h> 415b2fc499SJeff Garzik #include <linux/workqueue.h> 425b2fc499SJeff Garzik #include <linux/mii.h> 435b2fc499SJeff Garzik #include <linux/usb.h> 443692e94fSJussi Kivilinna #include <linux/usb/usbnet.h> 455a0e3ad6STejun Heo #include <linux/slab.h> 46fd1f170dSAndy Shevchenko #include <linux/kernel.h> 47b0786b43SMing Lei #include <linux/pm_runtime.h> 485b2fc499SJeff Garzik 495b2fc499SJeff Garzik #define DRIVER_VERSION "22-Aug-2005" 505b2fc499SJeff Garzik 515b2fc499SJeff Garzik 525b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 535b2fc499SJeff Garzik 545b2fc499SJeff Garzik /* 555b2fc499SJeff Garzik * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. 565b2fc499SJeff Garzik * Several dozen bytes of IPv4 data can fit in two such transactions. 575b2fc499SJeff Garzik * One maximum size Ethernet packet takes twenty four of them. 585b2fc499SJeff Garzik * For high speed, each frame comfortably fits almost 36 max size 595b2fc499SJeff Garzik * Ethernet packets (so queues should be bigger). 605b2fc499SJeff Garzik * 61a88c32aeSMing Lei * The goal is to let the USB host controller be busy for 5msec or 62a88c32aeSMing Lei * more before an irq is required, under load. Jumbograms change 63a88c32aeSMing Lei * the equation. 645b2fc499SJeff Garzik */ 65a88c32aeSMing Lei #define MAX_QUEUE_MEMORY (60 * 1518) 66a88c32aeSMing Lei #define RX_QLEN(dev) ((dev)->rx_qlen) 67a88c32aeSMing Lei #define TX_QLEN(dev) ((dev)->tx_qlen) 685b2fc499SJeff Garzik 695b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks 705b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES (5*HZ) 715b2fc499SJeff Garzik 725b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect() 735b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors. 745b2fc499SJeff Garzik #define THROTTLE_JIFFIES (HZ/8) 755b2fc499SJeff Garzik 765b2fc499SJeff Garzik // between wakeups 775b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS 3 785b2fc499SJeff Garzik 795b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 805b2fc499SJeff Garzik 815b2fc499SJeff Garzik // randomly generated ethernet address 825b2fc499SJeff Garzik static u8 node_id [ETH_ALEN]; 835b2fc499SJeff Garzik 845b2fc499SJeff Garzik static const char driver_name [] = "usbnet"; 855b2fc499SJeff Garzik 865b2fc499SJeff Garzik /* use ethtool to change the level for any given device */ 875b2fc499SJeff Garzik static int msg_level = -1; 885b2fc499SJeff Garzik module_param (msg_level, int, 0); 895b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level"); 905b2fc499SJeff Garzik 915b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 925b2fc499SJeff Garzik 935b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */ 945b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) 955b2fc499SJeff Garzik { 965b2fc499SJeff Garzik int tmp; 975b2fc499SJeff Garzik struct usb_host_interface *alt = NULL; 985b2fc499SJeff Garzik struct usb_host_endpoint *in = NULL, *out = NULL; 995b2fc499SJeff Garzik struct usb_host_endpoint *status = NULL; 1005b2fc499SJeff Garzik 1015b2fc499SJeff Garzik for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 1025b2fc499SJeff Garzik unsigned ep; 1035b2fc499SJeff Garzik 1045b2fc499SJeff Garzik in = out = status = NULL; 1055b2fc499SJeff Garzik alt = intf->altsetting + tmp; 1065b2fc499SJeff Garzik 1075b2fc499SJeff Garzik /* take the first altsetting with in-bulk + out-bulk; 1085b2fc499SJeff Garzik * remember any status endpoint, just in case; 10970f23fd6SJustin P. Mattock * ignore other endpoints and altsettings. 1105b2fc499SJeff Garzik */ 1115b2fc499SJeff Garzik for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 1125b2fc499SJeff Garzik struct usb_host_endpoint *e; 1135b2fc499SJeff Garzik int intr = 0; 1145b2fc499SJeff Garzik 1155b2fc499SJeff Garzik e = alt->endpoint + ep; 1165b2fc499SJeff Garzik switch (e->desc.bmAttributes) { 1175b2fc499SJeff Garzik case USB_ENDPOINT_XFER_INT: 1185b2fc499SJeff Garzik if (!usb_endpoint_dir_in(&e->desc)) 1195b2fc499SJeff Garzik continue; 1205b2fc499SJeff Garzik intr = 1; 1215b2fc499SJeff Garzik /* FALLTHROUGH */ 1225b2fc499SJeff Garzik case USB_ENDPOINT_XFER_BULK: 1235b2fc499SJeff Garzik break; 1245b2fc499SJeff Garzik default: 1255b2fc499SJeff Garzik continue; 1265b2fc499SJeff Garzik } 1275b2fc499SJeff Garzik if (usb_endpoint_dir_in(&e->desc)) { 1285b2fc499SJeff Garzik if (!intr && !in) 1295b2fc499SJeff Garzik in = e; 1305b2fc499SJeff Garzik else if (intr && !status) 1315b2fc499SJeff Garzik status = e; 1325b2fc499SJeff Garzik } else { 1335b2fc499SJeff Garzik if (!out) 1345b2fc499SJeff Garzik out = e; 1355b2fc499SJeff Garzik } 1365b2fc499SJeff Garzik } 1375b2fc499SJeff Garzik if (in && out) 1385b2fc499SJeff Garzik break; 1395b2fc499SJeff Garzik } 1405b2fc499SJeff Garzik if (!alt || !in || !out) 1415b2fc499SJeff Garzik return -EINVAL; 1425b2fc499SJeff Garzik 1438e95a202SJoe Perches if (alt->desc.bAlternateSetting != 0 || 1448e95a202SJoe Perches !(dev->driver_info->flags & FLAG_NO_SETINT)) { 1455b2fc499SJeff Garzik tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, 1465b2fc499SJeff Garzik alt->desc.bAlternateSetting); 1475b2fc499SJeff Garzik if (tmp < 0) 1485b2fc499SJeff Garzik return tmp; 1495b2fc499SJeff Garzik } 1505b2fc499SJeff Garzik 1515b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (dev->udev, 1525b2fc499SJeff Garzik in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1535b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (dev->udev, 1545b2fc499SJeff Garzik out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1555b2fc499SJeff Garzik dev->status = status; 1565b2fc499SJeff Garzik return 0; 1575b2fc499SJeff Garzik } 1585b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints); 1595b2fc499SJeff Garzik 16003ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress) 16103ad032bSPeter Holik { 16203ad032bSPeter Holik int tmp, i; 16303ad032bSPeter Holik unsigned char buf [13]; 16403ad032bSPeter Holik 16503ad032bSPeter Holik tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf); 16603ad032bSPeter Holik if (tmp != 12) { 16703ad032bSPeter Holik dev_dbg(&dev->udev->dev, 16803ad032bSPeter Holik "bad MAC string %d fetch, %d\n", iMACAddress, tmp); 16903ad032bSPeter Holik if (tmp >= 0) 17003ad032bSPeter Holik tmp = -EINVAL; 17103ad032bSPeter Holik return tmp; 17203ad032bSPeter Holik } 17303ad032bSPeter Holik for (i = tmp = 0; i < 6; i++, tmp += 2) 17403ad032bSPeter Holik dev->net->dev_addr [i] = 175fd1f170dSAndy Shevchenko (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]); 17603ad032bSPeter Holik return 0; 17703ad032bSPeter Holik } 17803ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); 17903ad032bSPeter Holik 18024ead299Stom.leiming@gmail.com static void intr_complete (struct urb *urb) 18124ead299Stom.leiming@gmail.com { 18224ead299Stom.leiming@gmail.com struct usbnet *dev = urb->context; 18324ead299Stom.leiming@gmail.com int status = urb->status; 18424ead299Stom.leiming@gmail.com 18524ead299Stom.leiming@gmail.com switch (status) { 18624ead299Stom.leiming@gmail.com /* success */ 18724ead299Stom.leiming@gmail.com case 0: 18824ead299Stom.leiming@gmail.com dev->driver_info->status(dev, urb); 18924ead299Stom.leiming@gmail.com break; 19024ead299Stom.leiming@gmail.com 19124ead299Stom.leiming@gmail.com /* software-driven interface shutdown */ 19224ead299Stom.leiming@gmail.com case -ENOENT: /* urb killed */ 19324ead299Stom.leiming@gmail.com case -ESHUTDOWN: /* hardware gone */ 19424ead299Stom.leiming@gmail.com netif_dbg(dev, ifdown, dev->net, 19524ead299Stom.leiming@gmail.com "intr shutdown, code %d\n", status); 19624ead299Stom.leiming@gmail.com return; 19724ead299Stom.leiming@gmail.com 19824ead299Stom.leiming@gmail.com /* NOTE: not throttling like RX/TX, since this endpoint 19924ead299Stom.leiming@gmail.com * already polls infrequently 20024ead299Stom.leiming@gmail.com */ 20124ead299Stom.leiming@gmail.com default: 20224ead299Stom.leiming@gmail.com netdev_dbg(dev->net, "intr status %d\n", status); 20324ead299Stom.leiming@gmail.com break; 20424ead299Stom.leiming@gmail.com } 20524ead299Stom.leiming@gmail.com 20624ead299Stom.leiming@gmail.com status = usb_submit_urb (urb, GFP_ATOMIC); 20724ead299Stom.leiming@gmail.com if (status != 0) 20824ead299Stom.leiming@gmail.com netif_err(dev, timer, dev->net, 20924ead299Stom.leiming@gmail.com "intr resubmit --> %d\n", status); 21024ead299Stom.leiming@gmail.com } 2115b2fc499SJeff Garzik 2125b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf) 2135b2fc499SJeff Garzik { 2145b2fc499SJeff Garzik char *buf = NULL; 2155b2fc499SJeff Garzik unsigned pipe = 0; 2165b2fc499SJeff Garzik unsigned maxp; 2175b2fc499SJeff Garzik unsigned period; 2185b2fc499SJeff Garzik 2195b2fc499SJeff Garzik if (!dev->driver_info->status) 2205b2fc499SJeff Garzik return 0; 2215b2fc499SJeff Garzik 2225b2fc499SJeff Garzik pipe = usb_rcvintpipe (dev->udev, 2235b2fc499SJeff Garzik dev->status->desc.bEndpointAddress 2245b2fc499SJeff Garzik & USB_ENDPOINT_NUMBER_MASK); 2255b2fc499SJeff Garzik maxp = usb_maxpacket (dev->udev, pipe, 0); 2265b2fc499SJeff Garzik 2275b2fc499SJeff Garzik /* avoid 1 msec chatter: min 8 msec poll rate */ 2285b2fc499SJeff Garzik period = max ((int) dev->status->desc.bInterval, 2295b2fc499SJeff Garzik (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 2305b2fc499SJeff Garzik 2315b2fc499SJeff Garzik buf = kmalloc (maxp, GFP_KERNEL); 2325b2fc499SJeff Garzik if (buf) { 2335b2fc499SJeff Garzik dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); 2345b2fc499SJeff Garzik if (!dev->interrupt) { 2355b2fc499SJeff Garzik kfree (buf); 2365b2fc499SJeff Garzik return -ENOMEM; 2375b2fc499SJeff Garzik } else { 2385b2fc499SJeff Garzik usb_fill_int_urb(dev->interrupt, dev->udev, pipe, 2395b2fc499SJeff Garzik buf, maxp, intr_complete, dev, period); 240720f3d7cStom.leiming@gmail.com dev->interrupt->transfer_flags |= URB_FREE_BUFFER; 2415b2fc499SJeff Garzik dev_dbg(&intf->dev, 2425b2fc499SJeff Garzik "status ep%din, %d bytes period %d\n", 2435b2fc499SJeff Garzik usb_pipeendpoint(pipe), maxp, period); 2445b2fc499SJeff Garzik } 2455b2fc499SJeff Garzik } 2465b2fc499SJeff Garzik return 0; 2475b2fc499SJeff Garzik } 2485b2fc499SJeff Garzik 2496eecdc5fSDan Williams /* Submit the interrupt URB if not previously submitted, increasing refcount */ 2506eecdc5fSDan Williams int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags) 2516eecdc5fSDan Williams { 2526eecdc5fSDan Williams int ret = 0; 2536eecdc5fSDan Williams 2546eecdc5fSDan Williams WARN_ON_ONCE(dev->interrupt == NULL); 2556eecdc5fSDan Williams if (dev->interrupt) { 2566eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2576eecdc5fSDan Williams 2586eecdc5fSDan Williams if (++dev->interrupt_count == 1) 2596eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags); 2606eecdc5fSDan Williams 2616eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n", 2626eecdc5fSDan Williams dev->interrupt_count); 2636eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2646eecdc5fSDan Williams } 2656eecdc5fSDan Williams return ret; 2666eecdc5fSDan Williams } 2676eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_start); 2686eecdc5fSDan Williams 2696eecdc5fSDan Williams /* For resume; submit interrupt URB if previously submitted */ 2706eecdc5fSDan Williams static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags) 2716eecdc5fSDan Williams { 2726eecdc5fSDan Williams int ret = 0; 2736eecdc5fSDan Williams 2746eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2756eecdc5fSDan Williams if (dev->interrupt_count) { 2766eecdc5fSDan Williams ret = usb_submit_urb(dev->interrupt, mem_flags); 2776eecdc5fSDan Williams dev_dbg(&dev->udev->dev, 2786eecdc5fSDan Williams "submitted interrupt URB for resume\n"); 2796eecdc5fSDan Williams } 2806eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2816eecdc5fSDan Williams return ret; 2826eecdc5fSDan Williams } 2836eecdc5fSDan Williams 2846eecdc5fSDan Williams /* Kill the interrupt URB if all submitters want it killed */ 2856eecdc5fSDan Williams void usbnet_status_stop(struct usbnet *dev) 2866eecdc5fSDan Williams { 2876eecdc5fSDan Williams if (dev->interrupt) { 2886eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 2896eecdc5fSDan Williams WARN_ON(dev->interrupt_count == 0); 2906eecdc5fSDan Williams 2916eecdc5fSDan Williams if (dev->interrupt_count && --dev->interrupt_count == 0) 2926eecdc5fSDan Williams usb_kill_urb(dev->interrupt); 2936eecdc5fSDan Williams 2946eecdc5fSDan Williams dev_dbg(&dev->udev->dev, 2956eecdc5fSDan Williams "decremented interrupt URB count to %d\n", 2966eecdc5fSDan Williams dev->interrupt_count); 2976eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 2986eecdc5fSDan Williams } 2996eecdc5fSDan Williams } 3006eecdc5fSDan Williams EXPORT_SYMBOL_GPL(usbnet_status_stop); 3016eecdc5fSDan Williams 3026eecdc5fSDan Williams /* For suspend; always kill interrupt URB */ 3036eecdc5fSDan Williams static void __usbnet_status_stop_force(struct usbnet *dev) 3046eecdc5fSDan Williams { 3056eecdc5fSDan Williams if (dev->interrupt) { 3066eecdc5fSDan Williams mutex_lock(&dev->interrupt_mutex); 3076eecdc5fSDan Williams usb_kill_urb(dev->interrupt); 3086eecdc5fSDan Williams dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n"); 3096eecdc5fSDan Williams mutex_unlock(&dev->interrupt_mutex); 3106eecdc5fSDan Williams } 3116eecdc5fSDan Williams } 3126eecdc5fSDan Williams 3135b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting. 3145b2fc499SJeff Garzik * Some link protocols batch packets, so their rx_fixup paths 3155b2fc499SJeff Garzik * can return clones as well as just modify the original skb. 3165b2fc499SJeff Garzik */ 3175b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) 3185b2fc499SJeff Garzik { 3195b2fc499SJeff Garzik int status; 3205b2fc499SJeff Garzik 3217834ddbcSJussi Kivilinna if (test_bit(EVENT_RX_PAUSED, &dev->flags)) { 3227834ddbcSJussi Kivilinna skb_queue_tail(&dev->rxq_pause, skb); 3237834ddbcSJussi Kivilinna return; 3247834ddbcSJussi Kivilinna } 3257834ddbcSJussi Kivilinna 3265b2fc499SJeff Garzik skb->protocol = eth_type_trans (skb, dev->net); 3277963837fSHerbert Xu dev->net->stats.rx_packets++; 3287963837fSHerbert Xu dev->net->stats.rx_bytes += skb->len; 3295b2fc499SJeff Garzik 330a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n", 3315b2fc499SJeff Garzik skb->len + sizeof (struct ethhdr), skb->protocol); 3325b2fc499SJeff Garzik memset (skb->cb, 0, sizeof (struct skb_data)); 333f9b491ecSMichael Riesch 334f9b491ecSMichael Riesch if (skb_defer_rx_timestamp(skb)) 335f9b491ecSMichael Riesch return; 336f9b491ecSMichael Riesch 3375b2fc499SJeff Garzik status = netif_rx (skb); 338a475f603SJoe Perches if (status != NET_RX_SUCCESS) 339a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 340a475f603SJoe Perches "netif_rx status %d\n", status); 3415b2fc499SJeff Garzik } 3425b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return); 3435b2fc499SJeff Garzik 344a88c32aeSMing Lei /* must be called if hard_mtu or rx_urb_size changed */ 345a88c32aeSMing Lei void usbnet_update_max_qlen(struct usbnet *dev) 346a88c32aeSMing Lei { 347a88c32aeSMing Lei enum usb_device_speed speed = dev->udev->speed; 348a88c32aeSMing Lei 349a88c32aeSMing Lei switch (speed) { 350a88c32aeSMing Lei case USB_SPEED_HIGH: 351a88c32aeSMing Lei dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size; 352a88c32aeSMing Lei dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu; 353a88c32aeSMing Lei break; 354452c447aSMing Lei case USB_SPEED_SUPER: 355452c447aSMing Lei /* 356452c447aSMing Lei * Not take default 5ms qlen for super speed HC to 357452c447aSMing Lei * save memory, and iperf tests show 2.5ms qlen can 358452c447aSMing Lei * work well 359452c447aSMing Lei */ 360452c447aSMing Lei dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size; 361452c447aSMing Lei dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu; 362452c447aSMing Lei break; 363a88c32aeSMing Lei default: 364a88c32aeSMing Lei dev->rx_qlen = dev->tx_qlen = 4; 365a88c32aeSMing Lei } 366a88c32aeSMing Lei } 367a88c32aeSMing Lei EXPORT_SYMBOL_GPL(usbnet_update_max_qlen); 368a88c32aeSMing Lei 3695b2fc499SJeff Garzik 3705b2fc499SJeff Garzik /*------------------------------------------------------------------------- 3715b2fc499SJeff Garzik * 3725b2fc499SJeff Garzik * Network Device Driver (peer link to "Host Device", from USB host) 3735b2fc499SJeff Garzik * 3745b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 3755b2fc499SJeff Garzik 376777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu) 3775b2fc499SJeff Garzik { 3785b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 3795b2fc499SJeff Garzik int ll_mtu = new_mtu + net->hard_header_len; 3805b2fc499SJeff Garzik int old_hard_mtu = dev->hard_mtu; 3815b2fc499SJeff Garzik int old_rx_urb_size = dev->rx_urb_size; 3825b2fc499SJeff Garzik 3835b2fc499SJeff Garzik if (new_mtu <= 0) 3845b2fc499SJeff Garzik return -EINVAL; 3855b2fc499SJeff Garzik // no second zero-length packet read wanted after mtu-sized packets 3865b2fc499SJeff Garzik if ((ll_mtu % dev->maxpacket) == 0) 3875b2fc499SJeff Garzik return -EDOM; 3885b2fc499SJeff Garzik net->mtu = new_mtu; 3895b2fc499SJeff Garzik 3905b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 3915b2fc499SJeff Garzik if (dev->rx_urb_size == old_hard_mtu) { 3925b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 3935b2fc499SJeff Garzik if (dev->rx_urb_size > old_rx_urb_size) 3945b2fc499SJeff Garzik usbnet_unlink_rx_urbs(dev); 3955b2fc499SJeff Garzik } 3965b2fc499SJeff Garzik 397a88c32aeSMing Lei /* max qlen depend on hard_mtu and rx_urb_size */ 398a88c32aeSMing Lei usbnet_update_max_qlen(dev); 399a88c32aeSMing Lei 4005b2fc499SJeff Garzik return 0; 4015b2fc499SJeff Garzik } 402777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu); 4035b2fc499SJeff Garzik 4045b6e9bcdSMing Lei /* The caller must hold list->lock */ 4055b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list, 4065b6e9bcdSMing Lei struct sk_buff *newsk, enum skb_state state) 4075b6e9bcdSMing Lei { 4085b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) newsk->cb; 4095b6e9bcdSMing Lei 4105b6e9bcdSMing Lei __skb_queue_tail(list, newsk); 4115b6e9bcdSMing Lei entry->state = state; 4125b6e9bcdSMing Lei } 4135b6e9bcdSMing Lei 4145b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4155b2fc499SJeff Garzik 4165b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from 4175b2fc499SJeff Garzik * completion callbacks. 2.5 should have fixed those bugs... 4185b2fc499SJeff Garzik */ 4195b2fc499SJeff Garzik 4205b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb, 4215b6e9bcdSMing Lei struct sk_buff_head *list, enum skb_state state) 4225b2fc499SJeff Garzik { 4235b2fc499SJeff Garzik unsigned long flags; 4245b6e9bcdSMing Lei enum skb_state old_state; 4255b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) skb->cb; 4265b2fc499SJeff Garzik 4275b2fc499SJeff Garzik spin_lock_irqsave(&list->lock, flags); 4285b6e9bcdSMing Lei old_state = entry->state; 4295b6e9bcdSMing Lei entry->state = state; 4305b2fc499SJeff Garzik __skb_unlink(skb, list); 4315b2fc499SJeff Garzik spin_unlock(&list->lock); 4325b2fc499SJeff Garzik spin_lock(&dev->done.lock); 4335b2fc499SJeff Garzik __skb_queue_tail(&dev->done, skb); 4345b2fc499SJeff Garzik if (dev->done.qlen == 1) 4355b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 4365b2fc499SJeff Garzik spin_unlock_irqrestore(&dev->done.lock, flags); 4375b6e9bcdSMing Lei return old_state; 4385b2fc499SJeff Garzik } 4395b2fc499SJeff Garzik 4405b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd 4415b2fc499SJeff Garzik * 4425b2fc499SJeff Garzik * NOTE: annoying asymmetry: if it's active, schedule_work() fails, 4435b2fc499SJeff Garzik * but tasklet_schedule() doesn't. hope the failure is rare. 4445b2fc499SJeff Garzik */ 4455b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work) 4465b2fc499SJeff Garzik { 4475b2fc499SJeff Garzik set_bit (work, &dev->flags); 4489532021dSSteve Glendinning if (!schedule_work (&dev->kevent)) { 4499532021dSSteve Glendinning if (net_ratelimit()) 45060b86755SJoe Perches netdev_err(dev->net, "kevent %d may have been dropped\n", work); 4519532021dSSteve Glendinning } else { 45260b86755SJoe Perches netdev_dbg(dev->net, "kevent %d scheduled\n", work); 4535b2fc499SJeff Garzik } 4549532021dSSteve Glendinning } 4555b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent); 4565b2fc499SJeff Garzik 4575b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4585b2fc499SJeff Garzik 4595b2fc499SJeff Garzik static void rx_complete (struct urb *urb); 4605b2fc499SJeff Garzik 461dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) 4625b2fc499SJeff Garzik { 4635b2fc499SJeff Garzik struct sk_buff *skb; 4645b2fc499SJeff Garzik struct skb_data *entry; 4655b2fc499SJeff Garzik int retval = 0; 4665b2fc499SJeff Garzik unsigned long lockflags; 4675b2fc499SJeff Garzik size_t size = dev->rx_urb_size; 4685b2fc499SJeff Garzik 46970c37bf9SBjørn Mork /* prevent rx skb allocation when error ratio is high */ 47070c37bf9SBjørn Mork if (test_bit(EVENT_RX_KILL, &dev->flags)) { 47170c37bf9SBjørn Mork usb_free_urb(urb); 47270c37bf9SBjørn Mork return -ENOLINK; 47370c37bf9SBjørn Mork } 47470c37bf9SBjørn Mork 4757bdd4027SEric Dumazet skb = __netdev_alloc_skb_ip_align(dev->net, size, flags); 4767bdd4027SEric Dumazet if (!skb) { 477a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no rx skb\n"); 4785b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 4795b2fc499SJeff Garzik usb_free_urb (urb); 480dacb3975SDavid S. Miller return -ENOMEM; 4815b2fc499SJeff Garzik } 4825b2fc499SJeff Garzik 4835b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 4845b2fc499SJeff Garzik entry->urb = urb; 4855b2fc499SJeff Garzik entry->dev = dev; 4865b2fc499SJeff Garzik entry->length = 0; 4875b2fc499SJeff Garzik 4885b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->in, 4895b2fc499SJeff Garzik skb->data, size, rx_complete, skb); 4905b2fc499SJeff Garzik 4915b2fc499SJeff Garzik spin_lock_irqsave (&dev->rxq.lock, lockflags); 4925b2fc499SJeff Garzik 4938e95a202SJoe Perches if (netif_running (dev->net) && 4948e95a202SJoe Perches netif_device_present (dev->net) && 49569ee472fSOliver Neukum !test_bit (EVENT_RX_HALT, &dev->flags) && 49669ee472fSOliver Neukum !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { 4975b2fc499SJeff Garzik switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { 4985b2fc499SJeff Garzik case -EPIPE: 4995b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 5005b2fc499SJeff Garzik break; 5015b2fc499SJeff Garzik case -ENOMEM: 5025b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 5035b2fc499SJeff Garzik break; 5045b2fc499SJeff Garzik case -ENODEV: 505a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "device gone\n"); 5065b2fc499SJeff Garzik netif_device_detach (dev->net); 5075b2fc499SJeff Garzik break; 508dacb3975SDavid S. Miller case -EHOSTUNREACH: 509dacb3975SDavid S. Miller retval = -ENOLINK; 510dacb3975SDavid S. Miller break; 5115b2fc499SJeff Garzik default: 512a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 513a475f603SJoe Perches "rx submit, %d\n", retval); 5145b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 5155b2fc499SJeff Garzik break; 5165b2fc499SJeff Garzik case 0: 5175b6e9bcdSMing Lei __usbnet_queue_skb(&dev->rxq, skb, rx_start); 5185b2fc499SJeff Garzik } 5195b2fc499SJeff Garzik } else { 520a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "rx: stopped\n"); 5215b2fc499SJeff Garzik retval = -ENOLINK; 5225b2fc499SJeff Garzik } 5235b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->rxq.lock, lockflags); 5245b2fc499SJeff Garzik if (retval) { 5255b2fc499SJeff Garzik dev_kfree_skb_any (skb); 5265b2fc499SJeff Garzik usb_free_urb (urb); 5275b2fc499SJeff Garzik } 528dacb3975SDavid S. Miller return retval; 5295b2fc499SJeff Garzik } 5305b2fc499SJeff Garzik 5315b2fc499SJeff Garzik 5325b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5335b2fc499SJeff Garzik 5345b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) 5355b2fc499SJeff Garzik { 5368e95a202SJoe Perches if (dev->driver_info->rx_fixup && 5377a635ea9SAndrzej Zaborowski !dev->driver_info->rx_fixup (dev, skb)) { 5387a635ea9SAndrzej Zaborowski /* With RX_ASSEMBLE, rx_fixup() must update counters */ 5397a635ea9SAndrzej Zaborowski if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE)) 5407a635ea9SAndrzej Zaborowski dev->net->stats.rx_errors++; 5417a635ea9SAndrzej Zaborowski goto done; 5427a635ea9SAndrzej Zaborowski } 5435b2fc499SJeff Garzik // else network stack removes extra byte if we forced a short packet 5445b2fc499SJeff Garzik 545073285fdSAlexey Orishko /* all data was already cloned from skb inside the driver */ 546073285fdSAlexey Orishko if (dev->driver_info->flags & FLAG_MULTI_PACKET) 547*eb85569fSEmil Goode goto done; 548*eb85569fSEmil Goode 549*eb85569fSEmil Goode if (skb->len < ETH_HLEN) { 550*eb85569fSEmil Goode dev->net->stats.rx_errors++; 551*eb85569fSEmil Goode dev->net->stats.rx_length_errors++; 552*eb85569fSEmil Goode netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len); 553*eb85569fSEmil Goode } else { 5545b2fc499SJeff Garzik usbnet_skb_return(dev, skb); 555073285fdSAlexey Orishko return; 556073285fdSAlexey Orishko } 557073285fdSAlexey Orishko 5587a635ea9SAndrzej Zaborowski done: 5595b2fc499SJeff Garzik skb_queue_tail(&dev->done, skb); 5605b2fc499SJeff Garzik } 5615b2fc499SJeff Garzik 5625b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5635b2fc499SJeff Garzik 5645b2fc499SJeff Garzik static void rx_complete (struct urb *urb) 5655b2fc499SJeff Garzik { 5665b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 5675b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 5685b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 5695b2fc499SJeff Garzik int urb_status = urb->status; 5705b6e9bcdSMing Lei enum skb_state state; 5715b2fc499SJeff Garzik 5725b2fc499SJeff Garzik skb_put (skb, urb->actual_length); 5735b6e9bcdSMing Lei state = rx_done; 5745b2fc499SJeff Garzik entry->urb = NULL; 5755b2fc499SJeff Garzik 5765b2fc499SJeff Garzik switch (urb_status) { 57718ab458fSDavid Brownell /* success */ 5785b2fc499SJeff Garzik case 0: 5795b2fc499SJeff Garzik break; 5805b2fc499SJeff Garzik 58118ab458fSDavid Brownell /* stalls need manual reset. this is rare ... except that 58218ab458fSDavid Brownell * when going through USB 2.0 TTs, unplug appears this way. 5833ac49a1cSJean Delvare * we avoid the highspeed version of the ETIMEDOUT/EILSEQ 58418ab458fSDavid Brownell * storm, recovering as needed. 58518ab458fSDavid Brownell */ 5865b2fc499SJeff Garzik case -EPIPE: 5877963837fSHerbert Xu dev->net->stats.rx_errors++; 5885b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 5895b2fc499SJeff Garzik // FALLTHROUGH 5905b2fc499SJeff Garzik 59118ab458fSDavid Brownell /* software-driven interface shutdown */ 59218ab458fSDavid Brownell case -ECONNRESET: /* async unlink */ 59318ab458fSDavid Brownell case -ESHUTDOWN: /* hardware gone */ 594a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 595a475f603SJoe Perches "rx shutdown, code %d\n", urb_status); 5965b2fc499SJeff Garzik goto block; 5975b2fc499SJeff Garzik 59818ab458fSDavid Brownell /* we get controller i/o faults during khubd disconnect() delays. 59918ab458fSDavid Brownell * throttle down resubmits, to avoid log floods; just temporarily, 60018ab458fSDavid Brownell * so we still recover when the fault isn't a khubd delay. 60118ab458fSDavid Brownell */ 6025b2fc499SJeff Garzik case -EPROTO: 6035b2fc499SJeff Garzik case -ETIME: 6045b2fc499SJeff Garzik case -EILSEQ: 6057963837fSHerbert Xu dev->net->stats.rx_errors++; 6065b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 6075b2fc499SJeff Garzik mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); 608a475f603SJoe Perches netif_dbg(dev, link, dev->net, 609a475f603SJoe Perches "rx throttle %d\n", urb_status); 6105b2fc499SJeff Garzik } 6115b2fc499SJeff Garzik block: 6125b6e9bcdSMing Lei state = rx_cleanup; 6135b2fc499SJeff Garzik entry->urb = urb; 6145b2fc499SJeff Garzik urb = NULL; 6155b2fc499SJeff Garzik break; 6165b2fc499SJeff Garzik 61718ab458fSDavid Brownell /* data overrun ... flush fifo? */ 6185b2fc499SJeff Garzik case -EOVERFLOW: 6197963837fSHerbert Xu dev->net->stats.rx_over_errors++; 6205b2fc499SJeff Garzik // FALLTHROUGH 6215b2fc499SJeff Garzik 6225b2fc499SJeff Garzik default: 6235b6e9bcdSMing Lei state = rx_cleanup; 6247963837fSHerbert Xu dev->net->stats.rx_errors++; 625a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status); 6265b2fc499SJeff Garzik break; 6275b2fc499SJeff Garzik } 6285b2fc499SJeff Garzik 62970c37bf9SBjørn Mork /* stop rx if packet error rate is high */ 63070c37bf9SBjørn Mork if (++dev->pkt_cnt > 30) { 63170c37bf9SBjørn Mork dev->pkt_cnt = 0; 63270c37bf9SBjørn Mork dev->pkt_err = 0; 63370c37bf9SBjørn Mork } else { 63470c37bf9SBjørn Mork if (state == rx_cleanup) 63570c37bf9SBjørn Mork dev->pkt_err++; 63670c37bf9SBjørn Mork if (dev->pkt_err > 20) 63770c37bf9SBjørn Mork set_bit(EVENT_RX_KILL, &dev->flags); 63870c37bf9SBjørn Mork } 63970c37bf9SBjørn Mork 6405b6e9bcdSMing Lei state = defer_bh(dev, skb, &dev->rxq, state); 6415b2fc499SJeff Garzik 6425b2fc499SJeff Garzik if (urb) { 6438e95a202SJoe Perches if (netif_running (dev->net) && 6445b6e9bcdSMing Lei !test_bit (EVENT_RX_HALT, &dev->flags) && 6455b6e9bcdSMing Lei state != unlink_start) { 6465b2fc499SJeff Garzik rx_submit (dev, urb, GFP_ATOMIC); 6478a783354SOliver Neukum usb_mark_last_busy(dev->udev); 6485b2fc499SJeff Garzik return; 6495b2fc499SJeff Garzik } 6505b2fc499SJeff Garzik usb_free_urb (urb); 6515b2fc499SJeff Garzik } 652a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n"); 6535b2fc499SJeff Garzik } 6545b2fc499SJeff Garzik 6555b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 6567834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev) 6577834ddbcSJussi Kivilinna { 6587834ddbcSJussi Kivilinna set_bit(EVENT_RX_PAUSED, &dev->flags); 6597834ddbcSJussi Kivilinna 660a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n"); 6617834ddbcSJussi Kivilinna } 6627834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx); 6637834ddbcSJussi Kivilinna 6647834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev) 6657834ddbcSJussi Kivilinna { 6667834ddbcSJussi Kivilinna struct sk_buff *skb; 6677834ddbcSJussi Kivilinna int num = 0; 6687834ddbcSJussi Kivilinna 6697834ddbcSJussi Kivilinna clear_bit(EVENT_RX_PAUSED, &dev->flags); 6707834ddbcSJussi Kivilinna 6717834ddbcSJussi Kivilinna while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { 6727834ddbcSJussi Kivilinna usbnet_skb_return(dev, skb); 6737834ddbcSJussi Kivilinna num++; 6747834ddbcSJussi Kivilinna } 6757834ddbcSJussi Kivilinna 6767834ddbcSJussi Kivilinna tasklet_schedule(&dev->bh); 6777834ddbcSJussi Kivilinna 678a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, 679a475f603SJoe Perches "paused rx queue disabled, %d skbs requeued\n", num); 6807834ddbcSJussi Kivilinna } 6817834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx); 6827834ddbcSJussi Kivilinna 6837834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev) 6847834ddbcSJussi Kivilinna { 6857834ddbcSJussi Kivilinna skb_queue_purge(&dev->rxq_pause); 6867834ddbcSJussi Kivilinna } 6877834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); 6887834ddbcSJussi Kivilinna 6897834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/ 6905b2fc499SJeff Garzik 6915b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup 6925b2fc499SJeff Garzik 6935b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 6945b2fc499SJeff Garzik { 6955b2fc499SJeff Garzik unsigned long flags; 6965b6e9bcdSMing Lei struct sk_buff *skb; 6975b2fc499SJeff Garzik int count = 0; 6985b2fc499SJeff Garzik 6995b2fc499SJeff Garzik spin_lock_irqsave (&q->lock, flags); 7005b6e9bcdSMing Lei while (!skb_queue_empty(q)) { 7015b2fc499SJeff Garzik struct skb_data *entry; 7025b2fc499SJeff Garzik struct urb *urb; 7035b2fc499SJeff Garzik int retval; 7045b2fc499SJeff Garzik 7055b6e9bcdSMing Lei skb_queue_walk(q, skb) { 7065b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 7075b6e9bcdSMing Lei if (entry->state != unlink_start) 7085b6e9bcdSMing Lei goto found; 7095b6e9bcdSMing Lei } 7105b6e9bcdSMing Lei break; 7115b6e9bcdSMing Lei found: 7125b6e9bcdSMing Lei entry->state = unlink_start; 7135b2fc499SJeff Garzik urb = entry->urb; 7145b2fc499SJeff Garzik 7150956a8c2Stom.leiming@gmail.com /* 7160956a8c2Stom.leiming@gmail.com * Get reference count of the URB to avoid it to be 7170956a8c2Stom.leiming@gmail.com * freed during usb_unlink_urb, which may trigger 7180956a8c2Stom.leiming@gmail.com * use-after-free problem inside usb_unlink_urb since 7190956a8c2Stom.leiming@gmail.com * usb_unlink_urb is always racing with .complete 7200956a8c2Stom.leiming@gmail.com * handler(include defer_bh). 7210956a8c2Stom.leiming@gmail.com */ 7220956a8c2Stom.leiming@gmail.com usb_get_urb(urb); 7234231d47eSSebastian Siewior spin_unlock_irqrestore(&q->lock, flags); 7245b2fc499SJeff Garzik // during some PM-driven resume scenarios, 7255b2fc499SJeff Garzik // these (async) unlinks complete immediately 7265b2fc499SJeff Garzik retval = usb_unlink_urb (urb); 7275b2fc499SJeff Garzik if (retval != -EINPROGRESS && retval != 0) 72860b86755SJoe Perches netdev_dbg(dev->net, "unlink urb err, %d\n", retval); 7295b2fc499SJeff Garzik else 7305b2fc499SJeff Garzik count++; 7310956a8c2Stom.leiming@gmail.com usb_put_urb(urb); 7324231d47eSSebastian Siewior spin_lock_irqsave(&q->lock, flags); 7335b2fc499SJeff Garzik } 7345b2fc499SJeff Garzik spin_unlock_irqrestore (&q->lock, flags); 7355b2fc499SJeff Garzik return count; 7365b2fc499SJeff Garzik } 7375b2fc499SJeff Garzik 7385b2fc499SJeff Garzik // Flush all pending rx urbs 7395b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes 7405b2fc499SJeff Garzik 7415b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev) 7425b2fc499SJeff Garzik { 7435b2fc499SJeff Garzik if (netif_running(dev->net)) { 7445b2fc499SJeff Garzik (void) unlink_urbs (dev, &dev->rxq); 7455b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 7465b2fc499SJeff Garzik } 7475b2fc499SJeff Garzik } 7485b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); 7495b2fc499SJeff Garzik 7505b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 7515b2fc499SJeff Garzik 7525b2fc499SJeff Garzik // precondition: never called in_interrupt 75369ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev) 75469ee472fSOliver Neukum { 75569ee472fSOliver Neukum DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup); 75669ee472fSOliver Neukum DECLARE_WAITQUEUE(wait, current); 75769ee472fSOliver Neukum int temp; 75869ee472fSOliver Neukum 75969ee472fSOliver Neukum /* ensure there are no more active urbs */ 76069ee472fSOliver Neukum add_wait_queue(&unlink_wakeup, &wait); 76169ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 76269ee472fSOliver Neukum dev->wait = &unlink_wakeup; 76369ee472fSOliver Neukum temp = unlink_urbs(dev, &dev->txq) + 76469ee472fSOliver Neukum unlink_urbs(dev, &dev->rxq); 76569ee472fSOliver Neukum 76669ee472fSOliver Neukum /* maybe wait for deletions to finish. */ 76769ee472fSOliver Neukum while (!skb_queue_empty(&dev->rxq) 76869ee472fSOliver Neukum && !skb_queue_empty(&dev->txq) 76969ee472fSOliver Neukum && !skb_queue_empty(&dev->done)) { 77066cc42a4SKulikov Vasiliy schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); 77169ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 772a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 773a475f603SJoe Perches "waited for %d urb completions\n", temp); 77469ee472fSOliver Neukum } 77569ee472fSOliver Neukum set_current_state(TASK_RUNNING); 77669ee472fSOliver Neukum dev->wait = NULL; 77769ee472fSOliver Neukum remove_wait_queue(&unlink_wakeup, &wait); 77869ee472fSOliver Neukum } 7795b2fc499SJeff Garzik 780777baa47SStephen Hemminger int usbnet_stop (struct net_device *net) 7815b2fc499SJeff Garzik { 7825b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 783a33e9e7fSJussi Kivilinna struct driver_info *info = dev->driver_info; 784a33e9e7fSJussi Kivilinna int retval; 7855b2fc499SJeff Garzik 78675bd0cbdSMing Lei clear_bit(EVENT_DEV_OPEN, &dev->flags); 7875b2fc499SJeff Garzik netif_stop_queue (net); 7885b2fc499SJeff Garzik 789a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 7908ccef431SBen Hutchings "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", 7917963837fSHerbert Xu net->stats.rx_packets, net->stats.tx_packets, 79260b86755SJoe Perches net->stats.rx_errors, net->stats.tx_errors); 7935b2fc499SJeff Garzik 794a33e9e7fSJussi Kivilinna /* allow minidriver to stop correctly (wireless devices to turn off 795a33e9e7fSJussi Kivilinna * radio etc) */ 796a33e9e7fSJussi Kivilinna if (info->stop) { 797a33e9e7fSJussi Kivilinna retval = info->stop(dev); 798a475f603SJoe Perches if (retval < 0) 799a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 80060b86755SJoe Perches "stop fail (%d) usbnet usb-%s-%s, %s\n", 801a33e9e7fSJussi Kivilinna retval, 802a33e9e7fSJussi Kivilinna dev->udev->bus->bus_name, dev->udev->devpath, 803a33e9e7fSJussi Kivilinna info->description); 804a33e9e7fSJussi Kivilinna } 805a33e9e7fSJussi Kivilinna 80669ee472fSOliver Neukum if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) 80769ee472fSOliver Neukum usbnet_terminate_urbs(dev); 8085b2fc499SJeff Garzik 8096eecdc5fSDan Williams usbnet_status_stop(dev); 8105b2fc499SJeff Garzik 8117834ddbcSJussi Kivilinna usbnet_purge_paused_rxq(dev); 8127834ddbcSJussi Kivilinna 8135b2fc499SJeff Garzik /* deferred work (task, timer, softirq) must also stop. 8145b2fc499SJeff Garzik * can't flush_scheduled_work() until we drop rtnl (later), 8155b2fc499SJeff Garzik * else workers could deadlock; so make workers a NOP. 8165b2fc499SJeff Garzik */ 8175b2fc499SJeff Garzik dev->flags = 0; 8185b2fc499SJeff Garzik del_timer_sync (&dev->delay); 8195b2fc499SJeff Garzik tasklet_kill (&dev->bh); 820a1c088e0SOliver Neukum if (info->manage_power && 821a1c088e0SOliver Neukum !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags)) 82269ee472fSOliver Neukum info->manage_power(dev, 0); 82369ee472fSOliver Neukum else 824a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 8255b2fc499SJeff Garzik 8265b2fc499SJeff Garzik return 0; 8275b2fc499SJeff Garzik } 828777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop); 8295b2fc499SJeff Garzik 8305b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 8315b2fc499SJeff Garzik 8325b2fc499SJeff Garzik // posts reads, and enables write queuing 8335b2fc499SJeff Garzik 8345b2fc499SJeff Garzik // precondition: never called in_interrupt 8355b2fc499SJeff Garzik 836777baa47SStephen Hemminger int usbnet_open (struct net_device *net) 8375b2fc499SJeff Garzik { 8385b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 839a11a6544SOliver Neukum int retval; 8405b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 8415b2fc499SJeff Garzik 842a11a6544SOliver Neukum if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { 843a475f603SJoe Perches netif_info(dev, ifup, dev->net, 84460b86755SJoe Perches "resumption fail (%d) usbnet usb-%s-%s, %s\n", 845a11a6544SOliver Neukum retval, 84660b86755SJoe Perches dev->udev->bus->bus_name, 84760b86755SJoe Perches dev->udev->devpath, 848a11a6544SOliver Neukum info->description); 849a11a6544SOliver Neukum goto done_nopm; 850a11a6544SOliver Neukum } 851a11a6544SOliver Neukum 8525b2fc499SJeff Garzik // put into "known safe" state 8535b2fc499SJeff Garzik if (info->reset && (retval = info->reset (dev)) < 0) { 854a475f603SJoe Perches netif_info(dev, ifup, dev->net, 85560b86755SJoe Perches "open reset fail (%d) usbnet usb-%s-%s, %s\n", 8565b2fc499SJeff Garzik retval, 85760b86755SJoe Perches dev->udev->bus->bus_name, 85860b86755SJoe Perches dev->udev->devpath, 8595b2fc499SJeff Garzik info->description); 8605b2fc499SJeff Garzik goto done; 8615b2fc499SJeff Garzik } 8625b2fc499SJeff Garzik 863a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change in reset() */ 864a88c32aeSMing Lei usbnet_update_max_qlen(dev); 865a88c32aeSMing Lei 8665b2fc499SJeff Garzik // insist peer be connected 8675b2fc499SJeff Garzik if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 868a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval); 8695b2fc499SJeff Garzik goto done; 8705b2fc499SJeff Garzik } 8715b2fc499SJeff Garzik 8725b2fc499SJeff Garzik /* start any status interrupt transfer */ 8735b2fc499SJeff Garzik if (dev->interrupt) { 8746eecdc5fSDan Williams retval = usbnet_status_start(dev, GFP_KERNEL); 8755b2fc499SJeff Garzik if (retval < 0) { 876a475f603SJoe Perches netif_err(dev, ifup, dev->net, 877a475f603SJoe Perches "intr submit %d\n", retval); 8785b2fc499SJeff Garzik goto done; 8795b2fc499SJeff Garzik } 8805b2fc499SJeff Garzik } 8815b2fc499SJeff Garzik 88268972efaSPaul Stewart set_bit(EVENT_DEV_OPEN, &dev->flags); 8835b2fc499SJeff Garzik netif_start_queue (net); 884a475f603SJoe Perches netif_info(dev, ifup, dev->net, 885a475f603SJoe Perches "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", 88660b86755SJoe Perches (int)RX_QLEN(dev), (int)TX_QLEN(dev), 887a475f603SJoe Perches dev->net->mtu, 888a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : 889a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : 890a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : 891a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : 892a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : 893a475f603SJoe Perches "simple"); 8945b2fc499SJeff Garzik 89570c37bf9SBjørn Mork /* reset rx error state */ 89670c37bf9SBjørn Mork dev->pkt_cnt = 0; 89770c37bf9SBjørn Mork dev->pkt_err = 0; 89870c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 89970c37bf9SBjørn Mork 9005b2fc499SJeff Garzik // delay posting reads until we're fully open 9015b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 90269ee472fSOliver Neukum if (info->manage_power) { 90369ee472fSOliver Neukum retval = info->manage_power(dev, 1); 904a1c088e0SOliver Neukum if (retval < 0) { 905a1c088e0SOliver Neukum retval = 0; 906a1c088e0SOliver Neukum set_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 907a1c088e0SOliver Neukum } else { 90869ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 90969ee472fSOliver Neukum } 910a1c088e0SOliver Neukum } 911a11a6544SOliver Neukum return retval; 9125b2fc499SJeff Garzik done: 913a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 914a11a6544SOliver Neukum done_nopm: 9155b2fc499SJeff Garzik return retval; 9165b2fc499SJeff Garzik } 917777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open); 9185b2fc499SJeff Garzik 9195b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 9205b2fc499SJeff Garzik 9215b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but 9225b2fc499SJeff Garzik * they'll probably want to use this base set. 9235b2fc499SJeff Garzik */ 9245b2fc499SJeff Garzik 9255b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) 9265b2fc499SJeff Garzik { 9275b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9285b2fc499SJeff Garzik 9295b2fc499SJeff Garzik if (!dev->mii.mdio_read) 9305b2fc499SJeff Garzik return -EOPNOTSUPP; 9315b2fc499SJeff Garzik 9325b2fc499SJeff Garzik return mii_ethtool_gset(&dev->mii, cmd); 9335b2fc499SJeff Garzik } 9345b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings); 9355b2fc499SJeff Garzik 9365b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) 9375b2fc499SJeff Garzik { 9385b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9395b2fc499SJeff Garzik int retval; 9405b2fc499SJeff Garzik 9415b2fc499SJeff Garzik if (!dev->mii.mdio_write) 9425b2fc499SJeff Garzik return -EOPNOTSUPP; 9435b2fc499SJeff Garzik 9445b2fc499SJeff Garzik retval = mii_ethtool_sset(&dev->mii, cmd); 9455b2fc499SJeff Garzik 9465b2fc499SJeff Garzik /* link speed/duplex might have changed */ 9475b2fc499SJeff Garzik if (dev->driver_info->link_reset) 9485b2fc499SJeff Garzik dev->driver_info->link_reset(dev); 9495b2fc499SJeff Garzik 950a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change in link_reset() */ 951a88c32aeSMing Lei usbnet_update_max_qlen(dev); 952a88c32aeSMing Lei 9535b2fc499SJeff Garzik return retval; 9545b2fc499SJeff Garzik 9555b2fc499SJeff Garzik } 9565b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings); 9575b2fc499SJeff Garzik 9585b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net) 9595b2fc499SJeff Garzik { 9605b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9615b2fc499SJeff Garzik 9625b2fc499SJeff Garzik /* If a check_connect is defined, return its result */ 9635b2fc499SJeff Garzik if (dev->driver_info->check_connect) 9645b2fc499SJeff Garzik return dev->driver_info->check_connect (dev) == 0; 9655b2fc499SJeff Garzik 9665b2fc499SJeff Garzik /* if the device has mii operations, use those */ 9675b2fc499SJeff Garzik if (dev->mii.mdio_read) 9685b2fc499SJeff Garzik return mii_link_ok(&dev->mii); 9695b2fc499SJeff Garzik 97005ffb3e2SBjørn Mork /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ 97105ffb3e2SBjørn Mork return ethtool_op_get_link(net); 9725b2fc499SJeff Garzik } 9735b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link); 9745b2fc499SJeff Garzik 9755b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net) 9765b2fc499SJeff Garzik { 9775b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9785b2fc499SJeff Garzik 9795b2fc499SJeff Garzik if (!dev->mii.mdio_write) 9805b2fc499SJeff Garzik return -EOPNOTSUPP; 9815b2fc499SJeff Garzik 9825b2fc499SJeff Garzik return mii_nway_restart(&dev->mii); 9835b2fc499SJeff Garzik } 9845b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset); 9855b2fc499SJeff Garzik 9865b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 9875b2fc499SJeff Garzik { 9885b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9895b2fc499SJeff Garzik 99086a2f415SPhil Sutter strlcpy (info->driver, dev->driver_name, sizeof info->driver); 99186a2f415SPhil Sutter strlcpy (info->version, DRIVER_VERSION, sizeof info->version); 99286a2f415SPhil Sutter strlcpy (info->fw_version, dev->driver_info->description, 9935b2fc499SJeff Garzik sizeof info->fw_version); 9945b2fc499SJeff Garzik usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 9955b2fc499SJeff Garzik } 9965b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 9975b2fc499SJeff Garzik 9985b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net) 9995b2fc499SJeff Garzik { 10005b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10015b2fc499SJeff Garzik 10025b2fc499SJeff Garzik return dev->msg_enable; 10035b2fc499SJeff Garzik } 10045b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 10055b2fc499SJeff Garzik 10065b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level) 10075b2fc499SJeff Garzik { 10085b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10095b2fc499SJeff Garzik 10105b2fc499SJeff Garzik dev->msg_enable = level; 10115b2fc499SJeff Garzik } 10125b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 10135b2fc499SJeff Garzik 10145b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */ 10150fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = { 10165b2fc499SJeff Garzik .get_settings = usbnet_get_settings, 10175b2fc499SJeff Garzik .set_settings = usbnet_set_settings, 10185b2fc499SJeff Garzik .get_link = usbnet_get_link, 10195b2fc499SJeff Garzik .nway_reset = usbnet_nway_reset, 10205b2fc499SJeff Garzik .get_drvinfo = usbnet_get_drvinfo, 10215b2fc499SJeff Garzik .get_msglevel = usbnet_get_msglevel, 10225b2fc499SJeff Garzik .set_msglevel = usbnet_set_msglevel, 1023123edb18SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 10245b2fc499SJeff Garzik }; 10255b2fc499SJeff Garzik 10265b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 10275b2fc499SJeff Garzik 10284b49f58fSMing Lei static void __handle_link_change(struct usbnet *dev) 10294b49f58fSMing Lei { 10304b49f58fSMing Lei if (!test_bit(EVENT_DEV_OPEN, &dev->flags)) 10314b49f58fSMing Lei return; 10324b49f58fSMing Lei 10334b49f58fSMing Lei if (!netif_carrier_ok(dev->net)) { 10344b49f58fSMing Lei /* kill URBs for reading packets to save bus bandwidth */ 10354b49f58fSMing Lei unlink_urbs(dev, &dev->rxq); 10364b49f58fSMing Lei 10374b49f58fSMing Lei /* 10384b49f58fSMing Lei * tx_timeout will unlink URBs for sending packets and 10394b49f58fSMing Lei * tx queue is stopped by netcore after link becomes off 10404b49f58fSMing Lei */ 10414b49f58fSMing Lei } else { 10424b49f58fSMing Lei /* submitting URBs for reading packets */ 10434b49f58fSMing Lei tasklet_schedule(&dev->bh); 10444b49f58fSMing Lei } 10454b49f58fSMing Lei 1046a88c32aeSMing Lei /* hard_mtu or rx_urb_size may change during link change */ 1047a88c32aeSMing Lei usbnet_update_max_qlen(dev); 1048a88c32aeSMing Lei 10494b49f58fSMing Lei clear_bit(EVENT_LINK_CHANGE, &dev->flags); 10504b49f58fSMing Lei } 10514b49f58fSMing Lei 10525b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd. 10535b2fc499SJeff Garzik * 10545b2fc499SJeff Garzik * NOTE: with 2.5 we could do more of this using completion callbacks, 10555b2fc499SJeff Garzik * especially now that control transfers can be queued. 10565b2fc499SJeff Garzik */ 10575b2fc499SJeff Garzik static void 10585b2fc499SJeff Garzik kevent (struct work_struct *work) 10595b2fc499SJeff Garzik { 10605b2fc499SJeff Garzik struct usbnet *dev = 10615b2fc499SJeff Garzik container_of(work, struct usbnet, kevent); 10625b2fc499SJeff Garzik int status; 10635b2fc499SJeff Garzik 10645b2fc499SJeff Garzik /* usb_clear_halt() needs a thread context */ 10655b2fc499SJeff Garzik if (test_bit (EVENT_TX_HALT, &dev->flags)) { 10665b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 106769ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 106869ee472fSOliver Neukum if (status < 0) 106969ee472fSOliver Neukum goto fail_pipe; 10705b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->out); 107169ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 10728e95a202SJoe Perches if (status < 0 && 10738e95a202SJoe Perches status != -EPIPE && 10748e95a202SJoe Perches status != -ESHUTDOWN) { 10755b2fc499SJeff Garzik if (netif_msg_tx_err (dev)) 107669ee472fSOliver Neukum fail_pipe: 107760b86755SJoe Perches netdev_err(dev->net, "can't clear tx halt, status %d\n", 10785b2fc499SJeff Garzik status); 10795b2fc499SJeff Garzik } else { 10805b2fc499SJeff Garzik clear_bit (EVENT_TX_HALT, &dev->flags); 10815b2fc499SJeff Garzik if (status != -ESHUTDOWN) 10825b2fc499SJeff Garzik netif_wake_queue (dev->net); 10835b2fc499SJeff Garzik } 10845b2fc499SJeff Garzik } 10855b2fc499SJeff Garzik if (test_bit (EVENT_RX_HALT, &dev->flags)) { 10865b2fc499SJeff Garzik unlink_urbs (dev, &dev->rxq); 108769ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 108869ee472fSOliver Neukum if (status < 0) 108969ee472fSOliver Neukum goto fail_halt; 10905b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->in); 109169ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 10928e95a202SJoe Perches if (status < 0 && 10938e95a202SJoe Perches status != -EPIPE && 10948e95a202SJoe Perches status != -ESHUTDOWN) { 10955b2fc499SJeff Garzik if (netif_msg_rx_err (dev)) 109669ee472fSOliver Neukum fail_halt: 109760b86755SJoe Perches netdev_err(dev->net, "can't clear rx halt, status %d\n", 10985b2fc499SJeff Garzik status); 10995b2fc499SJeff Garzik } else { 11005b2fc499SJeff Garzik clear_bit (EVENT_RX_HALT, &dev->flags); 11015b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 11025b2fc499SJeff Garzik } 11035b2fc499SJeff Garzik } 11045b2fc499SJeff Garzik 11055b2fc499SJeff Garzik /* tasklet could resubmit itself forever if memory is tight */ 11065b2fc499SJeff Garzik if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 11075b2fc499SJeff Garzik struct urb *urb = NULL; 1108dacb3975SDavid S. Miller int resched = 1; 11095b2fc499SJeff Garzik 11105b2fc499SJeff Garzik if (netif_running (dev->net)) 11115b2fc499SJeff Garzik urb = usb_alloc_urb (0, GFP_KERNEL); 11125b2fc499SJeff Garzik else 11135b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 11145b2fc499SJeff Garzik if (urb != NULL) { 11155b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 111669ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 1117ab60707fSJesper Juhl if (status < 0) { 1118ab60707fSJesper Juhl usb_free_urb(urb); 111969ee472fSOliver Neukum goto fail_lowmem; 1120ab60707fSJesper Juhl } 1121dacb3975SDavid S. Miller if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) 1122dacb3975SDavid S. Miller resched = 0; 112369ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 112469ee472fSOliver Neukum fail_lowmem: 1125dacb3975SDavid S. Miller if (resched) 11265b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 11275b2fc499SJeff Garzik } 11285b2fc499SJeff Garzik } 11295b2fc499SJeff Garzik 11305b2fc499SJeff Garzik if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 11315b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 11325b2fc499SJeff Garzik int retval = 0; 11335b2fc499SJeff Garzik 11345b2fc499SJeff Garzik clear_bit (EVENT_LINK_RESET, &dev->flags); 113569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 113669ee472fSOliver Neukum if (status < 0) 113769ee472fSOliver Neukum goto skip_reset; 11385b2fc499SJeff Garzik if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 113969ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 114069ee472fSOliver Neukum skip_reset: 114160b86755SJoe Perches netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", 11425b2fc499SJeff Garzik retval, 114360b86755SJoe Perches dev->udev->bus->bus_name, 114460b86755SJoe Perches dev->udev->devpath, 11455b2fc499SJeff Garzik info->description); 114669ee472fSOliver Neukum } else { 114769ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 11485b2fc499SJeff Garzik } 11494b49f58fSMing Lei 11504b49f58fSMing Lei /* handle link change from link resetting */ 11514b49f58fSMing Lei __handle_link_change(dev); 11525b2fc499SJeff Garzik } 11535b2fc499SJeff Garzik 11544b49f58fSMing Lei if (test_bit (EVENT_LINK_CHANGE, &dev->flags)) 11554b49f58fSMing Lei __handle_link_change(dev); 11564b49f58fSMing Lei 11575b2fc499SJeff Garzik if (dev->flags) 115860b86755SJoe Perches netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); 11595b2fc499SJeff Garzik } 11605b2fc499SJeff Garzik 11615b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 11625b2fc499SJeff Garzik 11635b2fc499SJeff Garzik static void tx_complete (struct urb *urb) 11645b2fc499SJeff Garzik { 11655b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 11665b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 11675b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 11685b2fc499SJeff Garzik 11695b2fc499SJeff Garzik if (urb->status == 0) { 1170073285fdSAlexey Orishko if (!(dev->driver_info->flags & FLAG_MULTI_PACKET)) 11717963837fSHerbert Xu dev->net->stats.tx_packets++; 11727963837fSHerbert Xu dev->net->stats.tx_bytes += entry->length; 11735b2fc499SJeff Garzik } else { 11747963837fSHerbert Xu dev->net->stats.tx_errors++; 11755b2fc499SJeff Garzik 11765b2fc499SJeff Garzik switch (urb->status) { 11775b2fc499SJeff Garzik case -EPIPE: 11785b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 11795b2fc499SJeff Garzik break; 11805b2fc499SJeff Garzik 11815b2fc499SJeff Garzik /* software-driven interface shutdown */ 11825b2fc499SJeff Garzik case -ECONNRESET: // async unlink 11835b2fc499SJeff Garzik case -ESHUTDOWN: // hardware gone 11845b2fc499SJeff Garzik break; 11855b2fc499SJeff Garzik 11865b2fc499SJeff Garzik // like rx, tx gets controller i/o faults during khubd delays 11875b2fc499SJeff Garzik // and so it uses the same throttling mechanism. 11885b2fc499SJeff Garzik case -EPROTO: 11895b2fc499SJeff Garzik case -ETIME: 11905b2fc499SJeff Garzik case -EILSEQ: 119169ee472fSOliver Neukum usb_mark_last_busy(dev->udev); 11925b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 11935b2fc499SJeff Garzik mod_timer (&dev->delay, 11945b2fc499SJeff Garzik jiffies + THROTTLE_JIFFIES); 1195a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1196a475f603SJoe Perches "tx throttle %d\n", urb->status); 11975b2fc499SJeff Garzik } 11985b2fc499SJeff Garzik netif_stop_queue (dev->net); 11995b2fc499SJeff Garzik break; 12005b2fc499SJeff Garzik default: 1201a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1202a475f603SJoe Perches "tx err %d\n", entry->urb->status); 12035b2fc499SJeff Garzik break; 12045b2fc499SJeff Garzik } 12055b2fc499SJeff Garzik } 12065b2fc499SJeff Garzik 120769ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 12085b6e9bcdSMing Lei (void) defer_bh(dev, skb, &dev->txq, tx_done); 12095b2fc499SJeff Garzik } 12105b2fc499SJeff Garzik 12115b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 12125b2fc499SJeff Garzik 1213777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net) 12145b2fc499SJeff Garzik { 12155b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 12165b2fc499SJeff Garzik 12175b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 12185b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 12195b2fc499SJeff Garzik 12205b2fc499SJeff Garzik // FIXME: device recovery -- reset? 12215b2fc499SJeff Garzik } 1222777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout); 12235b2fc499SJeff Garzik 12245b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 12255b2fc499SJeff Garzik 1226638c5115SMing Lei static int build_dma_sg(const struct sk_buff *skb, struct urb *urb) 1227638c5115SMing Lei { 1228638c5115SMing Lei unsigned num_sgs, total_len = 0; 1229638c5115SMing Lei int i, s = 0; 1230638c5115SMing Lei 1231638c5115SMing Lei num_sgs = skb_shinfo(skb)->nr_frags + 1; 1232638c5115SMing Lei if (num_sgs == 1) 1233638c5115SMing Lei return 0; 1234638c5115SMing Lei 123560e453a9SMing Lei /* reserve one for zero packet */ 123660e453a9SMing Lei urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist), 123760e453a9SMing Lei GFP_ATOMIC); 1238638c5115SMing Lei if (!urb->sg) 1239638c5115SMing Lei return -ENOMEM; 1240638c5115SMing Lei 1241638c5115SMing Lei urb->num_sgs = num_sgs; 1242fdc3452cSBjørn Mork sg_init_table(urb->sg, urb->num_sgs + 1); 1243638c5115SMing Lei 1244638c5115SMing Lei sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb)); 1245638c5115SMing Lei total_len += skb_headlen(skb); 1246638c5115SMing Lei 1247638c5115SMing Lei for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1248638c5115SMing Lei struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i]; 1249638c5115SMing Lei 1250638c5115SMing Lei total_len += skb_frag_size(f); 1251638c5115SMing Lei sg_set_page(&urb->sg[i + s], f->page.p, f->size, 1252638c5115SMing Lei f->page_offset); 1253638c5115SMing Lei } 1254638c5115SMing Lei urb->transfer_buffer_length = total_len; 1255638c5115SMing Lei 1256638c5115SMing Lei return 1; 1257638c5115SMing Lei } 1258638c5115SMing Lei 125925a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, 126025a79c41SStephen Hemminger struct net_device *net) 12615b2fc499SJeff Garzik { 12625b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 12635b2fc499SJeff Garzik int length; 12645b2fc499SJeff Garzik struct urb *urb = NULL; 12655b2fc499SJeff Garzik struct skb_data *entry; 12665b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 12675b2fc499SJeff Garzik unsigned long flags; 126825a79c41SStephen Hemminger int retval; 12695b2fc499SJeff Garzik 127023ba0799SKonstantin Khlebnikov if (skb) 1271f9b491ecSMichael Riesch skb_tx_timestamp(skb); 1272f9b491ecSMichael Riesch 12735b2fc499SJeff Garzik // some devices want funky USB-level framing, for 12745b2fc499SJeff Garzik // win32 driver (usually) and/or hardware quirks 12755b2fc499SJeff Garzik if (info->tx_fixup) { 12765b2fc499SJeff Garzik skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 12775b2fc499SJeff Garzik if (!skb) { 1278bf414b36SBjørn Mork /* packet collected; minidriver waiting for more */ 1279bf414b36SBjørn Mork if (info->flags & FLAG_MULTI_PACKET) 1280bf414b36SBjørn Mork goto not_drop; 1281a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); 12825b2fc499SJeff Garzik goto drop; 12835b2fc499SJeff Garzik } 12845b2fc499SJeff Garzik } 12855b2fc499SJeff Garzik 12865b2fc499SJeff Garzik if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 1287a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "no urb\n"); 12885b2fc499SJeff Garzik goto drop; 12895b2fc499SJeff Garzik } 12905b2fc499SJeff Garzik 12915b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 12925b2fc499SJeff Garzik entry->urb = urb; 12935b2fc499SJeff Garzik entry->dev = dev; 12945b2fc499SJeff Garzik 12955b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->out, 12965b2fc499SJeff Garzik skb->data, skb->len, tx_complete, skb); 1297638c5115SMing Lei if (dev->can_dma_sg) { 1298638c5115SMing Lei if (build_dma_sg(skb, urb) < 0) 1299638c5115SMing Lei goto drop; 1300638c5115SMing Lei } 130160e453a9SMing Lei length = urb->transfer_buffer_length; 13025b2fc499SJeff Garzik 13035b2fc499SJeff Garzik /* don't assume the hardware handles USB_ZERO_PACKET 13045b2fc499SJeff Garzik * NOTE: strictly conforming cdc-ether devices should expect 13055b2fc499SJeff Garzik * the ZLP here, but ignore the one-byte packet. 1306073285fdSAlexey Orishko * NOTE2: CDC NCM specification is different from CDC ECM when 1307073285fdSAlexey Orishko * handling ZLP/short packets, so cdc_ncm driver will make short 1308073285fdSAlexey Orishko * packet itself if needed. 13095b2fc499SJeff Garzik */ 1310b4d562e3SElina Pasheva if (length % dev->maxpacket == 0) { 1311b4d562e3SElina Pasheva if (!(info->flags & FLAG_SEND_ZLP)) { 1312073285fdSAlexey Orishko if (!(info->flags & FLAG_MULTI_PACKET)) { 131360e453a9SMing Lei length++; 131460e453a9SMing Lei if (skb_tailroom(skb) && !urb->num_sgs) { 13153e323f3eSPeter Korsgaard skb->data[skb->len] = 0; 13163e323f3eSPeter Korsgaard __skb_put(skb, 1); 131760e453a9SMing Lei } else if (urb->num_sgs) 131860e453a9SMing Lei sg_set_buf(&urb->sg[urb->num_sgs++], 131960e453a9SMing Lei dev->padding_pkt, 1); 1320073285fdSAlexey Orishko } 1321b4d562e3SElina Pasheva } else 1322b4d562e3SElina Pasheva urb->transfer_flags |= URB_ZERO_PACKET; 13233e323f3eSPeter Korsgaard } 132460e453a9SMing Lei entry->length = urb->transfer_buffer_length = length; 13255b2fc499SJeff Garzik 13265b2fc499SJeff Garzik spin_lock_irqsave(&dev->txq.lock, flags); 132769ee472fSOliver Neukum retval = usb_autopm_get_interface_async(dev->intf); 132869ee472fSOliver Neukum if (retval < 0) { 132969ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 133069ee472fSOliver Neukum goto drop; 133169ee472fSOliver Neukum } 133269ee472fSOliver Neukum 133369ee472fSOliver Neukum #ifdef CONFIG_PM 133469ee472fSOliver Neukum /* if this triggers the device is still a sleep */ 133569ee472fSOliver Neukum if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { 133669ee472fSOliver Neukum /* transmission will be done in resume */ 133769ee472fSOliver Neukum usb_anchor_urb(urb, &dev->deferred); 133869ee472fSOliver Neukum /* no use to process more packets */ 133969ee472fSOliver Neukum netif_stop_queue(net); 134039707c2aSHemant Kumar usb_put_urb(urb); 134169ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 134260b86755SJoe Perches netdev_dbg(dev->net, "Delaying transmission for resumption\n"); 134369ee472fSOliver Neukum goto deferred; 134469ee472fSOliver Neukum } 134569ee472fSOliver Neukum #endif 13465b2fc499SJeff Garzik 13475b2fc499SJeff Garzik switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 13485b2fc499SJeff Garzik case -EPIPE: 13495b2fc499SJeff Garzik netif_stop_queue (net); 13505b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 135169ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 13525b2fc499SJeff Garzik break; 13535b2fc499SJeff Garzik default: 135469ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 1355a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1356a475f603SJoe Perches "tx: submit urb err %d\n", retval); 13575b2fc499SJeff Garzik break; 13585b2fc499SJeff Garzik case 0: 13595b2fc499SJeff Garzik net->trans_start = jiffies; 13605b6e9bcdSMing Lei __usbnet_queue_skb(&dev->txq, skb, tx_start); 13615b2fc499SJeff Garzik if (dev->txq.qlen >= TX_QLEN (dev)) 13625b2fc499SJeff Garzik netif_stop_queue (net); 13635b2fc499SJeff Garzik } 13645b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->txq.lock, flags); 13655b2fc499SJeff Garzik 13665b2fc499SJeff Garzik if (retval) { 1367a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); 13685b2fc499SJeff Garzik drop: 13697963837fSHerbert Xu dev->net->stats.tx_dropped++; 1370073285fdSAlexey Orishko not_drop: 13715b2fc499SJeff Garzik if (skb) 13725b2fc499SJeff Garzik dev_kfree_skb_any (skb); 1373638c5115SMing Lei if (urb) { 1374638c5115SMing Lei kfree(urb->sg); 13755b2fc499SJeff Garzik usb_free_urb(urb); 1376638c5115SMing Lei } 1377a475f603SJoe Perches } else 1378a475f603SJoe Perches netif_dbg(dev, tx_queued, dev->net, 1379a475f603SJoe Perches "> tx, len %d, type 0x%x\n", length, skb->protocol); 138069ee472fSOliver Neukum #ifdef CONFIG_PM 138169ee472fSOliver Neukum deferred: 138269ee472fSOliver Neukum #endif 138325a79c41SStephen Hemminger return NETDEV_TX_OK; 13845b2fc499SJeff Garzik } 1385777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit); 13865b2fc499SJeff Garzik 138785e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags) 138865841fd5SMing Lei { 138965841fd5SMing Lei struct urb *urb; 139065841fd5SMing Lei int i; 139185e87870SBjørn Mork int ret = 0; 139265841fd5SMing Lei 139365841fd5SMing Lei /* don't refill the queue all at once */ 139465841fd5SMing Lei for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) { 139565841fd5SMing Lei urb = usb_alloc_urb(0, flags); 139665841fd5SMing Lei if (urb != NULL) { 139785e87870SBjørn Mork ret = rx_submit(dev, urb, flags); 139885e87870SBjørn Mork if (ret) 139985e87870SBjørn Mork goto err; 140085e87870SBjørn Mork } else { 140185e87870SBjørn Mork ret = -ENOMEM; 140285e87870SBjørn Mork goto err; 140365841fd5SMing Lei } 140465841fd5SMing Lei } 140585e87870SBjørn Mork err: 140685e87870SBjørn Mork return ret; 140765841fd5SMing Lei } 140865841fd5SMing Lei 14095b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 14105b2fc499SJeff Garzik 14115b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer 14125b2fc499SJeff Garzik 14135b2fc499SJeff Garzik static void usbnet_bh (unsigned long param) 14145b2fc499SJeff Garzik { 14155b2fc499SJeff Garzik struct usbnet *dev = (struct usbnet *) param; 14165b2fc499SJeff Garzik struct sk_buff *skb; 14175b2fc499SJeff Garzik struct skb_data *entry; 14185b2fc499SJeff Garzik 14195b2fc499SJeff Garzik while ((skb = skb_dequeue (&dev->done))) { 14205b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 14215b2fc499SJeff Garzik switch (entry->state) { 14225b2fc499SJeff Garzik case rx_done: 14235b2fc499SJeff Garzik entry->state = rx_cleanup; 14245b2fc499SJeff Garzik rx_process (dev, skb); 14255b2fc499SJeff Garzik continue; 14265b2fc499SJeff Garzik case tx_done: 1427638c5115SMing Lei kfree(entry->urb->sg); 14285b2fc499SJeff Garzik case rx_cleanup: 14295b2fc499SJeff Garzik usb_free_urb (entry->urb); 14305b2fc499SJeff Garzik dev_kfree_skb (skb); 14315b2fc499SJeff Garzik continue; 14325b2fc499SJeff Garzik default: 143360b86755SJoe Perches netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); 14345b2fc499SJeff Garzik } 14355b2fc499SJeff Garzik } 14365b2fc499SJeff Garzik 143770c37bf9SBjørn Mork /* restart RX again after disabling due to high error rate */ 143870c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 143970c37bf9SBjørn Mork 14405b2fc499SJeff Garzik // waiting for all pending urbs to complete? 14415b2fc499SJeff Garzik if (dev->wait) { 14425b2fc499SJeff Garzik if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { 14435b2fc499SJeff Garzik wake_up (dev->wait); 14445b2fc499SJeff Garzik } 14455b2fc499SJeff Garzik 14465b2fc499SJeff Garzik // or are we maybe short a few urbs? 14478e95a202SJoe Perches } else if (netif_running (dev->net) && 14488e95a202SJoe Perches netif_device_present (dev->net) && 14494b49f58fSMing Lei netif_carrier_ok(dev->net) && 14508e95a202SJoe Perches !timer_pending (&dev->delay) && 14518e95a202SJoe Perches !test_bit (EVENT_RX_HALT, &dev->flags)) { 14525b2fc499SJeff Garzik int temp = dev->rxq.qlen; 14535b2fc499SJeff Garzik 145465841fd5SMing Lei if (temp < RX_QLEN(dev)) { 145585e87870SBjørn Mork if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK) 145685e87870SBjørn Mork return; 1457a475f603SJoe Perches if (temp != dev->rxq.qlen) 1458a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1459a475f603SJoe Perches "rxqlen %d --> %d\n", 14605b2fc499SJeff Garzik temp, dev->rxq.qlen); 146165841fd5SMing Lei if (dev->rxq.qlen < RX_QLEN(dev)) 14625b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 14635b2fc499SJeff Garzik } 14645b2fc499SJeff Garzik if (dev->txq.qlen < TX_QLEN (dev)) 14655b2fc499SJeff Garzik netif_wake_queue (dev->net); 14665b2fc499SJeff Garzik } 14675b2fc499SJeff Garzik } 14685b2fc499SJeff Garzik 14695b2fc499SJeff Garzik 14705b2fc499SJeff Garzik /*------------------------------------------------------------------------- 14715b2fc499SJeff Garzik * 14725b2fc499SJeff Garzik * USB Device Driver support 14735b2fc499SJeff Garzik * 14745b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 14755b2fc499SJeff Garzik 14765b2fc499SJeff Garzik // precondition: never called in_interrupt 14775b2fc499SJeff Garzik 14785b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf) 14795b2fc499SJeff Garzik { 14805b2fc499SJeff Garzik struct usbnet *dev; 14815b2fc499SJeff Garzik struct usb_device *xdev; 14825b2fc499SJeff Garzik struct net_device *net; 14835b2fc499SJeff Garzik 14845b2fc499SJeff Garzik dev = usb_get_intfdata(intf); 14855b2fc499SJeff Garzik usb_set_intfdata(intf, NULL); 14865b2fc499SJeff Garzik if (!dev) 14875b2fc499SJeff Garzik return; 14885b2fc499SJeff Garzik 14895b2fc499SJeff Garzik xdev = interface_to_usbdev (intf); 14905b2fc499SJeff Garzik 1491a475f603SJoe Perches netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", 14925b2fc499SJeff Garzik intf->dev.driver->name, 14935b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 14945b2fc499SJeff Garzik dev->driver_info->description); 14955b2fc499SJeff Garzik 14965b2fc499SJeff Garzik net = dev->net; 14975b2fc499SJeff Garzik unregister_netdev (net); 14985b2fc499SJeff Garzik 149923f333a2STejun Heo cancel_work_sync(&dev->kevent); 15005b2fc499SJeff Garzik 150139707c2aSHemant Kumar usb_scuttle_anchored_urbs(&dev->deferred); 150239707c2aSHemant Kumar 15035b2fc499SJeff Garzik if (dev->driver_info->unbind) 15045b2fc499SJeff Garzik dev->driver_info->unbind (dev, intf); 15055b2fc499SJeff Garzik 150668972efaSPaul Stewart usb_kill_urb(dev->interrupt); 150768972efaSPaul Stewart usb_free_urb(dev->interrupt); 150860e453a9SMing Lei kfree(dev->padding_pkt); 150968972efaSPaul Stewart 15105b2fc499SJeff Garzik free_netdev(net); 15115b2fc499SJeff Garzik } 15125b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect); 15135b2fc499SJeff Garzik 1514777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = { 1515777baa47SStephen Hemminger .ndo_open = usbnet_open, 1516777baa47SStephen Hemminger .ndo_stop = usbnet_stop, 1517777baa47SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 1518777baa47SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 1519777baa47SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu, 1520777baa47SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1521777baa47SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1522777baa47SStephen Hemminger }; 15235b2fc499SJeff Garzik 15245b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 15255b2fc499SJeff Garzik 15265b2fc499SJeff Garzik // precondition: never called in_interrupt 15275b2fc499SJeff Garzik 1528225794f8SMarcel Holtmann static struct device_type wlan_type = { 1529225794f8SMarcel Holtmann .name = "wlan", 1530225794f8SMarcel Holtmann }; 1531225794f8SMarcel Holtmann 1532225794f8SMarcel Holtmann static struct device_type wwan_type = { 1533225794f8SMarcel Holtmann .name = "wwan", 1534225794f8SMarcel Holtmann }; 1535225794f8SMarcel Holtmann 15365b2fc499SJeff Garzik int 15375b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 15385b2fc499SJeff Garzik { 15395b2fc499SJeff Garzik struct usbnet *dev; 15405b2fc499SJeff Garzik struct net_device *net; 15415b2fc499SJeff Garzik struct usb_host_interface *interface; 15425b2fc499SJeff Garzik struct driver_info *info; 15435b2fc499SJeff Garzik struct usb_device *xdev; 15445b2fc499SJeff Garzik int status; 15455b2fc499SJeff Garzik const char *name; 1546b0786b43SMing Lei struct usb_driver *driver = to_usb_driver(udev->dev.driver); 1547b0786b43SMing Lei 1548b0786b43SMing Lei /* usbnet already took usb runtime pm, so have to enable the feature 1549b0786b43SMing Lei * for usb interface, otherwise usb_autopm_get_interface may return 155098f541c6SAlan Stern * failure if RUNTIME_PM is enabled. 1551b0786b43SMing Lei */ 1552b0786b43SMing Lei if (!driver->supports_autosuspend) { 1553b0786b43SMing Lei driver->supports_autosuspend = 1; 1554b0786b43SMing Lei pm_runtime_enable(&udev->dev); 1555b0786b43SMing Lei } 15565b2fc499SJeff Garzik 15575b2fc499SJeff Garzik name = udev->dev.driver->name; 15585b2fc499SJeff Garzik info = (struct driver_info *) prod->driver_info; 15595b2fc499SJeff Garzik if (!info) { 15605b2fc499SJeff Garzik dev_dbg (&udev->dev, "blacklisted by %s\n", name); 15615b2fc499SJeff Garzik return -ENODEV; 15625b2fc499SJeff Garzik } 15635b2fc499SJeff Garzik xdev = interface_to_usbdev (udev); 15645b2fc499SJeff Garzik interface = udev->cur_altsetting; 15655b2fc499SJeff Garzik 15665b2fc499SJeff Garzik status = -ENOMEM; 15675b2fc499SJeff Garzik 15685b2fc499SJeff Garzik // set up our own records 15695b2fc499SJeff Garzik net = alloc_etherdev(sizeof(*dev)); 157041de8d4cSJoe Perches if (!net) 15715b2fc499SJeff Garzik goto out; 15725b2fc499SJeff Garzik 15730dacca73SBen Hutchings /* netdev_printk() needs this so do it as early as possible */ 15740dacca73SBen Hutchings SET_NETDEV_DEV(net, &udev->dev); 15750dacca73SBen Hutchings 15765b2fc499SJeff Garzik dev = netdev_priv(net); 15775b2fc499SJeff Garzik dev->udev = xdev; 1578a11a6544SOliver Neukum dev->intf = udev; 15795b2fc499SJeff Garzik dev->driver_info = info; 15805b2fc499SJeff Garzik dev->driver_name = name; 15815b2fc499SJeff Garzik dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 15825b2fc499SJeff Garzik | NETIF_MSG_PROBE | NETIF_MSG_LINK); 15835b2fc499SJeff Garzik skb_queue_head_init (&dev->rxq); 15845b2fc499SJeff Garzik skb_queue_head_init (&dev->txq); 15855b2fc499SJeff Garzik skb_queue_head_init (&dev->done); 15867834ddbcSJussi Kivilinna skb_queue_head_init(&dev->rxq_pause); 15875b2fc499SJeff Garzik dev->bh.func = usbnet_bh; 15885b2fc499SJeff Garzik dev->bh.data = (unsigned long) dev; 15895b2fc499SJeff Garzik INIT_WORK (&dev->kevent, kevent); 159069ee472fSOliver Neukum init_usb_anchor(&dev->deferred); 15915b2fc499SJeff Garzik dev->delay.function = usbnet_bh; 15925b2fc499SJeff Garzik dev->delay.data = (unsigned long) dev; 15935b2fc499SJeff Garzik init_timer (&dev->delay); 15945b2fc499SJeff Garzik mutex_init (&dev->phy_mutex); 15956eecdc5fSDan Williams mutex_init(&dev->interrupt_mutex); 15966eecdc5fSDan Williams dev->interrupt_count = 0; 15975b2fc499SJeff Garzik 15985b2fc499SJeff Garzik dev->net = net; 15995b2fc499SJeff Garzik strcpy (net->name, "usb%d"); 16005b2fc499SJeff Garzik memcpy (net->dev_addr, node_id, sizeof node_id); 16015b2fc499SJeff Garzik 16025b2fc499SJeff Garzik /* rx and tx sides can use different message sizes; 16035b2fc499SJeff Garzik * bind() should set rx_urb_size in that case. 16045b2fc499SJeff Garzik */ 16055b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 16065b2fc499SJeff Garzik #if 0 16075b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures 16085b2fc499SJeff Garzik // possible with some EHCI controllers 16096a35528aSYang Hongyang if (dma_supported (&udev->dev, DMA_BIT_MASK(64))) 16105b2fc499SJeff Garzik net->features |= NETIF_F_HIGHDMA; 16115b2fc499SJeff Garzik #endif 16125b2fc499SJeff Garzik 1613777baa47SStephen Hemminger net->netdev_ops = &usbnet_netdev_ops; 1614777baa47SStephen Hemminger net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 16155b2fc499SJeff Garzik net->ethtool_ops = &usbnet_ethtool_ops; 16165b2fc499SJeff Garzik 16175b2fc499SJeff Garzik // allow device-specific bind/init procedures 16185b2fc499SJeff Garzik // NOTE net->name still not usable ... 16195b2fc499SJeff Garzik if (info->bind) { 16205b2fc499SJeff Garzik status = info->bind (dev, udev); 16215b2fc499SJeff Garzik if (status < 0) 16225b2fc499SJeff Garzik goto out1; 16235b2fc499SJeff Garzik 16245b2fc499SJeff Garzik // heuristic: "usb%d" for links we know are two-host, 16255b2fc499SJeff Garzik // else "eth%d" when there's reasonable doubt. userspace 16265b2fc499SJeff Garzik // can rename the link if it knows better. 16278e95a202SJoe Perches if ((dev->driver_info->flags & FLAG_ETHER) != 0 && 1628c261344dSArnd Bergmann ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 || 1629c261344dSArnd Bergmann (net->dev_addr [0] & 0x02) == 0)) 16305b2fc499SJeff Garzik strcpy (net->name, "eth%d"); 16316e3bbcc5SJussi Kivilinna /* WLAN devices should always be named "wlan%d" */ 16326e3bbcc5SJussi Kivilinna if ((dev->driver_info->flags & FLAG_WLAN) != 0) 16336e3bbcc5SJussi Kivilinna strcpy(net->name, "wlan%d"); 1634e1e499eeSMarcel Holtmann /* WWAN devices should always be named "wwan%d" */ 1635e1e499eeSMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1636e1e499eeSMarcel Holtmann strcpy(net->name, "wwan%d"); 16375b2fc499SJeff Garzik 16386509141fSWei Shuai /* devices that cannot do ARP */ 16396509141fSWei Shuai if ((dev->driver_info->flags & FLAG_NOARP) != 0) 16406509141fSWei Shuai net->flags |= IFF_NOARP; 16416509141fSWei Shuai 16425b2fc499SJeff Garzik /* maybe the remote can't receive an Ethernet MTU */ 16435b2fc499SJeff Garzik if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 16445b2fc499SJeff Garzik net->mtu = dev->hard_mtu - net->hard_header_len; 16455b2fc499SJeff Garzik } else if (!info->in || !info->out) 16465b2fc499SJeff Garzik status = usbnet_get_endpoints (dev, udev); 16475b2fc499SJeff Garzik else { 16485b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (xdev, info->in); 16495b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (xdev, info->out); 16505b2fc499SJeff Garzik if (!(info->flags & FLAG_NO_SETINT)) 16515b2fc499SJeff Garzik status = usb_set_interface (xdev, 16525b2fc499SJeff Garzik interface->desc.bInterfaceNumber, 16535b2fc499SJeff Garzik interface->desc.bAlternateSetting); 16545b2fc499SJeff Garzik else 16555b2fc499SJeff Garzik status = 0; 16565b2fc499SJeff Garzik 16575b2fc499SJeff Garzik } 16589514bfe5SPeter Korsgaard if (status >= 0 && dev->status) 16595b2fc499SJeff Garzik status = init_status (dev, udev); 16605b2fc499SJeff Garzik if (status < 0) 16615b2fc499SJeff Garzik goto out3; 16625b2fc499SJeff Garzik 16635b2fc499SJeff Garzik if (!dev->rx_urb_size) 16645b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 16655b2fc499SJeff Garzik dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); 16665b2fc499SJeff Garzik 1667eef23b53SBjørn Mork /* let userspace know we have a random address */ 1668eef23b53SBjørn Mork if (ether_addr_equal(net->dev_addr, node_id)) 1669eef23b53SBjørn Mork net->addr_assign_type = NET_ADDR_RANDOM; 1670eef23b53SBjørn Mork 1671225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1672225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wlan_type); 1673225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1674225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wwan_type); 1675225794f8SMarcel Holtmann 1676a88c32aeSMing Lei /* initialize max rx_qlen and tx_qlen */ 1677a88c32aeSMing Lei usbnet_update_max_qlen(dev); 1678a88c32aeSMing Lei 167960e453a9SMing Lei if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) && 168060e453a9SMing Lei !(info->flags & FLAG_MULTI_PACKET)) { 168160e453a9SMing Lei dev->padding_pkt = kzalloc(1, GFP_KERNEL); 168209b69024SWei Yongjun if (!dev->padding_pkt) { 168309b69024SWei Yongjun status = -ENOMEM; 168460e453a9SMing Lei goto out4; 168560e453a9SMing Lei } 168609b69024SWei Yongjun } 168760e453a9SMing Lei 16885b2fc499SJeff Garzik status = register_netdev (net); 16895b2fc499SJeff Garzik if (status) 169060e453a9SMing Lei goto out5; 1691a475f603SJoe Perches netif_info(dev, probe, dev->net, 1692a475f603SJoe Perches "register '%s' at usb-%s-%s, %s, %pM\n", 16935b2fc499SJeff Garzik udev->dev.driver->name, 16945b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 16955b2fc499SJeff Garzik dev->driver_info->description, 1696e174961cSJohannes Berg net->dev_addr); 16975b2fc499SJeff Garzik 16985b2fc499SJeff Garzik // ok, it's ready to go. 16995b2fc499SJeff Garzik usb_set_intfdata (udev, dev); 17005b2fc499SJeff Garzik 17015b2fc499SJeff Garzik netif_device_attach (net); 17025b2fc499SJeff Garzik 170337e8273cSBen Hutchings if (dev->driver_info->flags & FLAG_LINK_INTR) 17040162c554SMing Lei usbnet_link_change(dev, 0, 0); 170537e8273cSBen Hutchings 17065b2fc499SJeff Garzik return 0; 17075b2fc499SJeff Garzik 170860e453a9SMing Lei out5: 170960e453a9SMing Lei kfree(dev->padding_pkt); 1710a4723848Stom.leiming@gmail.com out4: 1711a4723848Stom.leiming@gmail.com usb_free_urb(dev->interrupt); 17125b2fc499SJeff Garzik out3: 17135b2fc499SJeff Garzik if (info->unbind) 17145b2fc499SJeff Garzik info->unbind (dev, udev); 17155b2fc499SJeff Garzik out1: 17165b2fc499SJeff Garzik free_netdev(net); 17175b2fc499SJeff Garzik out: 17185b2fc499SJeff Garzik return status; 17195b2fc499SJeff Garzik } 17205b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe); 17215b2fc499SJeff Garzik 17225b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 17235b2fc499SJeff Garzik 172436433127SOliver Neukum /* 172536433127SOliver Neukum * suspend the whole driver as soon as the first interface is suspended 172636433127SOliver Neukum * resume only when the last interface is resumed 17275b2fc499SJeff Garzik */ 17285b2fc499SJeff Garzik 17295b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 17305b2fc499SJeff Garzik { 17315b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 17325b2fc499SJeff Garzik 173336433127SOliver Neukum if (!dev->suspend_count++) { 173469ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 173569ee472fSOliver Neukum /* don't autosuspend while transmitting */ 17365b1b0b81SAlan Stern if (dev->txq.qlen && PMSG_IS_AUTO(message)) { 17375eeb3132SMing Lei dev->suspend_count--; 173869ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 173969ee472fSOliver Neukum return -EBUSY; 174069ee472fSOliver Neukum } else { 174169ee472fSOliver Neukum set_bit(EVENT_DEV_ASLEEP, &dev->flags); 174269ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 174369ee472fSOliver Neukum } 1744a11a6544SOliver Neukum /* 1745a11a6544SOliver Neukum * accelerate emptying of the rx and queues, to avoid 17465b2fc499SJeff Garzik * having everything error out. 17475b2fc499SJeff Garzik */ 17485b2fc499SJeff Garzik netif_device_detach (dev->net); 174969ee472fSOliver Neukum usbnet_terminate_urbs(dev); 17506eecdc5fSDan Williams __usbnet_status_stop_force(dev); 175169ee472fSOliver Neukum 1752a11a6544SOliver Neukum /* 1753a11a6544SOliver Neukum * reattach so runtime management can use and 1754a11a6544SOliver Neukum * wake the device 1755a11a6544SOliver Neukum */ 1756a11a6544SOliver Neukum netif_device_attach (dev->net); 175736433127SOliver Neukum } 17585b2fc499SJeff Garzik return 0; 17595b2fc499SJeff Garzik } 17605b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend); 17615b2fc499SJeff Garzik 17625b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf) 17635b2fc499SJeff Garzik { 17645b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 176569ee472fSOliver Neukum struct sk_buff *skb; 176669ee472fSOliver Neukum struct urb *res; 176769ee472fSOliver Neukum int retval; 17685b2fc499SJeff Garzik 176969ee472fSOliver Neukum if (!--dev->suspend_count) { 17706eecdc5fSDan Williams /* resume interrupt URB if it was previously submitted */ 17716eecdc5fSDan Williams __usbnet_status_start_force(dev, GFP_NOIO); 177268972efaSPaul Stewart 177369ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 177469ee472fSOliver Neukum while ((res = usb_get_from_anchor(&dev->deferred))) { 177569ee472fSOliver Neukum 177669ee472fSOliver Neukum skb = (struct sk_buff *)res->context; 177769ee472fSOliver Neukum retval = usb_submit_urb(res, GFP_ATOMIC); 177869ee472fSOliver Neukum if (retval < 0) { 177969ee472fSOliver Neukum dev_kfree_skb_any(skb); 1780638c5115SMing Lei kfree(res->sg); 178169ee472fSOliver Neukum usb_free_urb(res); 178269ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 178369ee472fSOliver Neukum } else { 178469ee472fSOliver Neukum dev->net->trans_start = jiffies; 178569ee472fSOliver Neukum __skb_queue_tail(&dev->txq, skb); 178669ee472fSOliver Neukum } 178769ee472fSOliver Neukum } 178869ee472fSOliver Neukum 178969ee472fSOliver Neukum smp_mb(); 179069ee472fSOliver Neukum clear_bit(EVENT_DEV_ASLEEP, &dev->flags); 179169ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 179275bd0cbdSMing Lei 179375bd0cbdSMing Lei if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { 179465841fd5SMing Lei /* handle remote wakeup ASAP */ 179565841fd5SMing Lei if (!dev->wait && 179665841fd5SMing Lei netif_device_present(dev->net) && 179765841fd5SMing Lei !timer_pending(&dev->delay) && 179865841fd5SMing Lei !test_bit(EVENT_RX_HALT, &dev->flags)) 1799ab6f148dSOliver Neukum rx_alloc_submit(dev, GFP_NOIO); 180065841fd5SMing Lei 180169ee472fSOliver Neukum if (!(dev->txq.qlen >= TX_QLEN(dev))) 18021aa9bc5bSAlexey Orishko netif_tx_wake_all_queues(dev->net); 18035b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 180469ee472fSOliver Neukum } 180575bd0cbdSMing Lei } 18065d9d01a3SOliver Neukum 18075d9d01a3SOliver Neukum if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) 18085d9d01a3SOliver Neukum usb_autopm_get_interface_no_resume(intf); 18095d9d01a3SOliver Neukum 18105b2fc499SJeff Garzik return 0; 18115b2fc499SJeff Garzik } 18125b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume); 18135b2fc499SJeff Garzik 18145d9d01a3SOliver Neukum /* 18155d9d01a3SOliver Neukum * Either a subdriver implements manage_power, then it is assumed to always 18165d9d01a3SOliver Neukum * be ready to be suspended or it reports the readiness to be suspended 18175d9d01a3SOliver Neukum * explicitly 18185d9d01a3SOliver Neukum */ 18195d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev) 18205d9d01a3SOliver Neukum { 18215d9d01a3SOliver Neukum if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) { 18225d9d01a3SOliver Neukum dev->intf->needs_remote_wakeup = 1; 18235d9d01a3SOliver Neukum usb_autopm_put_interface_async(dev->intf); 18245d9d01a3SOliver Neukum } 18255d9d01a3SOliver Neukum } 18265d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle); 18275b2fc499SJeff Garzik 18282dd7c8cfSOliver Neukum /* 18292dd7c8cfSOliver Neukum * For devices that can do without special commands 18302dd7c8cfSOliver Neukum */ 18312dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on) 18322dd7c8cfSOliver Neukum { 18332dd7c8cfSOliver Neukum dev->intf->needs_remote_wakeup = on; 18342dd7c8cfSOliver Neukum return 0; 18352dd7c8cfSOliver Neukum } 18362dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power); 18372dd7c8cfSOliver Neukum 1838ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset) 1839ac64995dSMing Lei { 1840ac64995dSMing Lei /* update link after link is reseted */ 1841ac64995dSMing Lei if (link && !need_reset) 1842ac64995dSMing Lei netif_carrier_on(dev->net); 1843ac64995dSMing Lei else 1844ac64995dSMing Lei netif_carrier_off(dev->net); 1845ac64995dSMing Lei 1846ac64995dSMing Lei if (need_reset && link) 1847ac64995dSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_RESET); 18484b49f58fSMing Lei else 18494b49f58fSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_CHANGE); 1850ac64995dSMing Lei } 1851ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change); 1852ac64995dSMing Lei 18535b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 18540547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 1855877bd862SMing Lei u16 value, u16 index, void *data, u16 size) 1856877bd862SMing Lei { 1857877bd862SMing Lei void *buf = NULL; 1858877bd862SMing Lei int err = -ENOMEM; 1859877bd862SMing Lei 1860877bd862SMing Lei netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x" 1861877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1862877bd862SMing Lei cmd, reqtype, value, index, size); 1863877bd862SMing Lei 1864877bd862SMing Lei if (data) { 1865877bd862SMing Lei buf = kmalloc(size, GFP_KERNEL); 1866877bd862SMing Lei if (!buf) 1867877bd862SMing Lei goto out; 1868877bd862SMing Lei } 1869877bd862SMing Lei 1870877bd862SMing Lei err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 1871877bd862SMing Lei cmd, reqtype, value, index, buf, size, 1872877bd862SMing Lei USB_CTRL_GET_TIMEOUT); 1873877bd862SMing Lei if (err > 0 && err <= size) 1874877bd862SMing Lei memcpy(data, buf, err); 1875877bd862SMing Lei kfree(buf); 1876877bd862SMing Lei out: 1877877bd862SMing Lei return err; 1878877bd862SMing Lei } 1879877bd862SMing Lei 18800547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 18810547fad2SMing Lei u16 value, u16 index, const void *data, 18820547fad2SMing Lei u16 size) 1883877bd862SMing Lei { 1884877bd862SMing Lei void *buf = NULL; 1885877bd862SMing Lei int err = -ENOMEM; 1886877bd862SMing Lei 1887877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1888877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1889877bd862SMing Lei cmd, reqtype, value, index, size); 1890877bd862SMing Lei 1891877bd862SMing Lei if (data) { 1892877bd862SMing Lei buf = kmemdup(data, size, GFP_KERNEL); 1893877bd862SMing Lei if (!buf) 1894877bd862SMing Lei goto out; 1895877bd862SMing Lei } 1896877bd862SMing Lei 1897877bd862SMing Lei err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 1898877bd862SMing Lei cmd, reqtype, value, index, buf, size, 1899877bd862SMing Lei USB_CTRL_SET_TIMEOUT); 1900877bd862SMing Lei kfree(buf); 1901877bd862SMing Lei 1902877bd862SMing Lei out: 1903877bd862SMing Lei return err; 1904877bd862SMing Lei } 19050547fad2SMing Lei 19060547fad2SMing Lei /* 19070547fad2SMing Lei * The function can't be called inside suspend/resume callback, 19080547fad2SMing Lei * otherwise deadlock will be caused. 19090547fad2SMing Lei */ 19100547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 19110547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 19120547fad2SMing Lei { 19136f0a0986SMing Lei int ret; 19146f0a0986SMing Lei 19156f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 19166f0a0986SMing Lei return -ENODEV; 19176f0a0986SMing Lei ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index, 19180547fad2SMing Lei data, size); 19196f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 19206f0a0986SMing Lei return ret; 19210547fad2SMing Lei } 19220547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd); 19230547fad2SMing Lei 19240547fad2SMing Lei /* 19250547fad2SMing Lei * The function can't be called inside suspend/resume callback, 19260547fad2SMing Lei * otherwise deadlock will be caused. 19270547fad2SMing Lei */ 19280547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 19290547fad2SMing Lei u16 value, u16 index, const void *data, u16 size) 19300547fad2SMing Lei { 19316f0a0986SMing Lei int ret; 19326f0a0986SMing Lei 19336f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 19346f0a0986SMing Lei return -ENODEV; 19356f0a0986SMing Lei ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index, 19360547fad2SMing Lei data, size); 19376f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 19386f0a0986SMing Lei return ret; 19390547fad2SMing Lei } 1940877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd); 1941877bd862SMing Lei 19420547fad2SMing Lei /* 19430547fad2SMing Lei * The function can be called inside suspend/resume callback safely 19440547fad2SMing Lei * and should only be called by suspend/resume callback generally. 19450547fad2SMing Lei */ 19460547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 19470547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 19480547fad2SMing Lei { 19490547fad2SMing Lei return __usbnet_read_cmd(dev, cmd, reqtype, value, index, 19500547fad2SMing Lei data, size); 19510547fad2SMing Lei } 19520547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm); 19530547fad2SMing Lei 19540547fad2SMing Lei /* 19550547fad2SMing Lei * The function can be called inside suspend/resume callback safely 19560547fad2SMing Lei * and should only be called by suspend/resume callback generally. 19570547fad2SMing Lei */ 19580547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 19590547fad2SMing Lei u16 value, u16 index, const void *data, 19600547fad2SMing Lei u16 size) 19610547fad2SMing Lei { 19620547fad2SMing Lei return __usbnet_write_cmd(dev, cmd, reqtype, value, index, 19630547fad2SMing Lei data, size); 19640547fad2SMing Lei } 19650547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm); 19660547fad2SMing Lei 1967877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb) 1968877bd862SMing Lei { 1969877bd862SMing Lei struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; 1970877bd862SMing Lei int status = urb->status; 1971877bd862SMing Lei 1972877bd862SMing Lei if (status < 0) 1973877bd862SMing Lei dev_dbg(&urb->dev->dev, "%s failed with %d", 1974877bd862SMing Lei __func__, status); 1975877bd862SMing Lei 1976877bd862SMing Lei kfree(req); 1977877bd862SMing Lei usb_free_urb(urb); 1978877bd862SMing Lei } 1979877bd862SMing Lei 19800547fad2SMing Lei /* 19810547fad2SMing Lei * The caller must make sure that device can't be put into suspend 19820547fad2SMing Lei * state until the control URB completes. 19830547fad2SMing Lei */ 1984877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype, 1985877bd862SMing Lei u16 value, u16 index, const void *data, u16 size) 1986877bd862SMing Lei { 1987877bd862SMing Lei struct usb_ctrlrequest *req = NULL; 1988877bd862SMing Lei struct urb *urb; 1989877bd862SMing Lei int err = -ENOMEM; 1990877bd862SMing Lei void *buf = NULL; 1991877bd862SMing Lei 1992877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1993877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1994877bd862SMing Lei cmd, reqtype, value, index, size); 1995877bd862SMing Lei 1996877bd862SMing Lei urb = usb_alloc_urb(0, GFP_ATOMIC); 1997877bd862SMing Lei if (!urb) { 1998877bd862SMing Lei netdev_err(dev->net, "Error allocating URB in" 1999877bd862SMing Lei " %s!\n", __func__); 2000877bd862SMing Lei goto fail; 2001877bd862SMing Lei } 2002877bd862SMing Lei 2003877bd862SMing Lei if (data) { 2004877bd862SMing Lei buf = kmemdup(data, size, GFP_ATOMIC); 2005877bd862SMing Lei if (!buf) { 2006877bd862SMing Lei netdev_err(dev->net, "Error allocating buffer" 2007877bd862SMing Lei " in %s!\n", __func__); 2008877bd862SMing Lei goto fail_free; 2009877bd862SMing Lei } 2010877bd862SMing Lei } 2011877bd862SMing Lei 2012877bd862SMing Lei req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); 201338673c82SJoe Perches if (!req) 2014877bd862SMing Lei goto fail_free_buf; 2015877bd862SMing Lei 2016877bd862SMing Lei req->bRequestType = reqtype; 2017877bd862SMing Lei req->bRequest = cmd; 2018877bd862SMing Lei req->wValue = cpu_to_le16(value); 2019877bd862SMing Lei req->wIndex = cpu_to_le16(index); 2020877bd862SMing Lei req->wLength = cpu_to_le16(size); 2021877bd862SMing Lei 2022877bd862SMing Lei usb_fill_control_urb(urb, dev->udev, 2023877bd862SMing Lei usb_sndctrlpipe(dev->udev, 0), 2024877bd862SMing Lei (void *)req, buf, size, 2025877bd862SMing Lei usbnet_async_cmd_cb, req); 2026877bd862SMing Lei urb->transfer_flags |= URB_FREE_BUFFER; 2027877bd862SMing Lei 2028877bd862SMing Lei err = usb_submit_urb(urb, GFP_ATOMIC); 2029877bd862SMing Lei if (err < 0) { 2030877bd862SMing Lei netdev_err(dev->net, "Error submitting the control" 2031877bd862SMing Lei " message: status=%d\n", err); 2032877bd862SMing Lei goto fail_free; 2033877bd862SMing Lei } 2034877bd862SMing Lei return 0; 2035877bd862SMing Lei 2036877bd862SMing Lei fail_free_buf: 2037877bd862SMing Lei kfree(buf); 2038877bd862SMing Lei fail_free: 2039877bd862SMing Lei kfree(req); 2040877bd862SMing Lei usb_free_urb(urb); 2041877bd862SMing Lei fail: 2042877bd862SMing Lei return err; 2043877bd862SMing Lei 2044877bd862SMing Lei } 2045877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async); 2046877bd862SMing Lei /*-------------------------------------------------------------------------*/ 20475b2fc499SJeff Garzik 20485b2fc499SJeff Garzik static int __init usbnet_init(void) 20495b2fc499SJeff Garzik { 2050c582a950SThiago Farina /* Compiler should optimize this out. */ 2051c582a950SThiago Farina BUILD_BUG_ON( 2052c582a950SThiago Farina FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data)); 20535b2fc499SJeff Garzik 2054c7e12eadSJoe Perches eth_random_addr(node_id); 20555b2fc499SJeff Garzik return 0; 20565b2fc499SJeff Garzik } 20575b2fc499SJeff Garzik module_init(usbnet_init); 20585b2fc499SJeff Garzik 20595b2fc499SJeff Garzik static void __exit usbnet_exit(void) 20605b2fc499SJeff Garzik { 20615b2fc499SJeff Garzik } 20625b2fc499SJeff Garzik module_exit(usbnet_exit); 20635b2fc499SJeff Garzik 20645b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell"); 20655b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework"); 20665b2fc499SJeff Garzik MODULE_LICENSE("GPL"); 2067