15b2fc499SJeff Garzik /* 25b2fc499SJeff Garzik * USB Network driver infrastructure 35b2fc499SJeff Garzik * Copyright (C) 2000-2005 by David Brownell 45b2fc499SJeff Garzik * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> 55b2fc499SJeff Garzik * 65b2fc499SJeff Garzik * This program is free software; you can redistribute it and/or modify 75b2fc499SJeff Garzik * it under the terms of the GNU General Public License as published by 85b2fc499SJeff Garzik * the Free Software Foundation; either version 2 of the License, or 95b2fc499SJeff Garzik * (at your option) any later version. 105b2fc499SJeff Garzik * 115b2fc499SJeff Garzik * This program is distributed in the hope that it will be useful, 125b2fc499SJeff Garzik * but WITHOUT ANY WARRANTY; without even the implied warranty of 135b2fc499SJeff Garzik * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 145b2fc499SJeff Garzik * GNU General Public License for more details. 155b2fc499SJeff Garzik * 165b2fc499SJeff Garzik * You should have received a copy of the GNU General Public License 175b2fc499SJeff Garzik * along with this program; if not, write to the Free Software 185b2fc499SJeff Garzik * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 195b2fc499SJeff Garzik */ 205b2fc499SJeff Garzik 215b2fc499SJeff Garzik /* 225b2fc499SJeff Garzik * This is a generic "USB networking" framework that works with several 235b2fc499SJeff Garzik * kinds of full and high speed networking devices: host-to-host cables, 245b2fc499SJeff Garzik * smart usb peripherals, and actual Ethernet adapters. 255b2fc499SJeff Garzik * 265b2fc499SJeff Garzik * These devices usually differ in terms of control protocols (if they 275b2fc499SJeff Garzik * even have one!) and sometimes they define new framing to wrap or batch 285b2fc499SJeff Garzik * Ethernet packets. Otherwise, they talk to USB pretty much the same, 295b2fc499SJeff Garzik * so interface (un)binding, endpoint I/O queues, fault handling, and other 305b2fc499SJeff Garzik * issues can usefully be addressed by this framework. 315b2fc499SJeff Garzik */ 325b2fc499SJeff Garzik 335b2fc499SJeff Garzik // #define DEBUG // error path messages, extra info 345b2fc499SJeff Garzik // #define VERBOSE // more; success messages 355b2fc499SJeff Garzik 365b2fc499SJeff Garzik #include <linux/module.h> 375b2fc499SJeff Garzik #include <linux/init.h> 385b2fc499SJeff Garzik #include <linux/netdevice.h> 395b2fc499SJeff Garzik #include <linux/etherdevice.h> 4003ad032bSPeter Holik #include <linux/ctype.h> 415b2fc499SJeff Garzik #include <linux/ethtool.h> 425b2fc499SJeff Garzik #include <linux/workqueue.h> 435b2fc499SJeff Garzik #include <linux/mii.h> 445b2fc499SJeff Garzik #include <linux/usb.h> 453692e94fSJussi Kivilinna #include <linux/usb/usbnet.h> 465a0e3ad6STejun Heo #include <linux/slab.h> 47fd1f170dSAndy Shevchenko #include <linux/kernel.h> 48b0786b43SMing Lei #include <linux/pm_runtime.h> 495b2fc499SJeff Garzik 505b2fc499SJeff Garzik #define DRIVER_VERSION "22-Aug-2005" 515b2fc499SJeff Garzik 525b2fc499SJeff Garzik 535b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 545b2fc499SJeff Garzik 555b2fc499SJeff Garzik /* 565b2fc499SJeff Garzik * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. 575b2fc499SJeff Garzik * Several dozen bytes of IPv4 data can fit in two such transactions. 585b2fc499SJeff Garzik * One maximum size Ethernet packet takes twenty four of them. 595b2fc499SJeff Garzik * For high speed, each frame comfortably fits almost 36 max size 605b2fc499SJeff Garzik * Ethernet packets (so queues should be bigger). 615b2fc499SJeff Garzik * 625b2fc499SJeff Garzik * REVISIT qlens should be members of 'struct usbnet'; the goal is to 635b2fc499SJeff Garzik * let the USB host controller be busy for 5msec or more before an irq 645b2fc499SJeff Garzik * is required, under load. Jumbograms change the equation. 655b2fc499SJeff Garzik */ 665b2fc499SJeff Garzik #define RX_MAX_QUEUE_MEMORY (60 * 1518) 675b2fc499SJeff Garzik #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \ 685b2fc499SJeff Garzik (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4) 695b2fc499SJeff Garzik #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \ 705b2fc499SJeff Garzik (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4) 715b2fc499SJeff Garzik 725b2fc499SJeff Garzik // reawaken network queue this soon after stopping; else watchdog barks 735b2fc499SJeff Garzik #define TX_TIMEOUT_JIFFIES (5*HZ) 745b2fc499SJeff Garzik 755b2fc499SJeff Garzik // throttle rx/tx briefly after some faults, so khubd might disconnect() 765b2fc499SJeff Garzik // us (it polls at HZ/4 usually) before we report too many false errors. 775b2fc499SJeff Garzik #define THROTTLE_JIFFIES (HZ/8) 785b2fc499SJeff Garzik 795b2fc499SJeff Garzik // between wakeups 805b2fc499SJeff Garzik #define UNLINK_TIMEOUT_MS 3 815b2fc499SJeff Garzik 825b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 835b2fc499SJeff Garzik 845b2fc499SJeff Garzik // randomly generated ethernet address 855b2fc499SJeff Garzik static u8 node_id [ETH_ALEN]; 865b2fc499SJeff Garzik 875b2fc499SJeff Garzik static const char driver_name [] = "usbnet"; 885b2fc499SJeff Garzik 895b2fc499SJeff Garzik /* use ethtool to change the level for any given device */ 905b2fc499SJeff Garzik static int msg_level = -1; 915b2fc499SJeff Garzik module_param (msg_level, int, 0); 925b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level"); 935b2fc499SJeff Garzik 945b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 955b2fc499SJeff Garzik 965b2fc499SJeff Garzik /* handles CDC Ethernet and many other network "bulk data" interfaces */ 975b2fc499SJeff Garzik int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) 985b2fc499SJeff Garzik { 995b2fc499SJeff Garzik int tmp; 1005b2fc499SJeff Garzik struct usb_host_interface *alt = NULL; 1015b2fc499SJeff Garzik struct usb_host_endpoint *in = NULL, *out = NULL; 1025b2fc499SJeff Garzik struct usb_host_endpoint *status = NULL; 1035b2fc499SJeff Garzik 1045b2fc499SJeff Garzik for (tmp = 0; tmp < intf->num_altsetting; tmp++) { 1055b2fc499SJeff Garzik unsigned ep; 1065b2fc499SJeff Garzik 1075b2fc499SJeff Garzik in = out = status = NULL; 1085b2fc499SJeff Garzik alt = intf->altsetting + tmp; 1095b2fc499SJeff Garzik 1105b2fc499SJeff Garzik /* take the first altsetting with in-bulk + out-bulk; 1115b2fc499SJeff Garzik * remember any status endpoint, just in case; 11270f23fd6SJustin P. Mattock * ignore other endpoints and altsettings. 1135b2fc499SJeff Garzik */ 1145b2fc499SJeff Garzik for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { 1155b2fc499SJeff Garzik struct usb_host_endpoint *e; 1165b2fc499SJeff Garzik int intr = 0; 1175b2fc499SJeff Garzik 1185b2fc499SJeff Garzik e = alt->endpoint + ep; 1195b2fc499SJeff Garzik switch (e->desc.bmAttributes) { 1205b2fc499SJeff Garzik case USB_ENDPOINT_XFER_INT: 1215b2fc499SJeff Garzik if (!usb_endpoint_dir_in(&e->desc)) 1225b2fc499SJeff Garzik continue; 1235b2fc499SJeff Garzik intr = 1; 1245b2fc499SJeff Garzik /* FALLTHROUGH */ 1255b2fc499SJeff Garzik case USB_ENDPOINT_XFER_BULK: 1265b2fc499SJeff Garzik break; 1275b2fc499SJeff Garzik default: 1285b2fc499SJeff Garzik continue; 1295b2fc499SJeff Garzik } 1305b2fc499SJeff Garzik if (usb_endpoint_dir_in(&e->desc)) { 1315b2fc499SJeff Garzik if (!intr && !in) 1325b2fc499SJeff Garzik in = e; 1335b2fc499SJeff Garzik else if (intr && !status) 1345b2fc499SJeff Garzik status = e; 1355b2fc499SJeff Garzik } else { 1365b2fc499SJeff Garzik if (!out) 1375b2fc499SJeff Garzik out = e; 1385b2fc499SJeff Garzik } 1395b2fc499SJeff Garzik } 1405b2fc499SJeff Garzik if (in && out) 1415b2fc499SJeff Garzik break; 1425b2fc499SJeff Garzik } 1435b2fc499SJeff Garzik if (!alt || !in || !out) 1445b2fc499SJeff Garzik return -EINVAL; 1455b2fc499SJeff Garzik 1468e95a202SJoe Perches if (alt->desc.bAlternateSetting != 0 || 1478e95a202SJoe Perches !(dev->driver_info->flags & FLAG_NO_SETINT)) { 1485b2fc499SJeff Garzik tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, 1495b2fc499SJeff Garzik alt->desc.bAlternateSetting); 1505b2fc499SJeff Garzik if (tmp < 0) 1515b2fc499SJeff Garzik return tmp; 1525b2fc499SJeff Garzik } 1535b2fc499SJeff Garzik 1545b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (dev->udev, 1555b2fc499SJeff Garzik in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1565b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (dev->udev, 1575b2fc499SJeff Garzik out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); 1585b2fc499SJeff Garzik dev->status = status; 1595b2fc499SJeff Garzik return 0; 1605b2fc499SJeff Garzik } 1615b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_endpoints); 1625b2fc499SJeff Garzik 16303ad032bSPeter Holik int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress) 16403ad032bSPeter Holik { 16503ad032bSPeter Holik int tmp, i; 16603ad032bSPeter Holik unsigned char buf [13]; 16703ad032bSPeter Holik 16803ad032bSPeter Holik tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf); 16903ad032bSPeter Holik if (tmp != 12) { 17003ad032bSPeter Holik dev_dbg(&dev->udev->dev, 17103ad032bSPeter Holik "bad MAC string %d fetch, %d\n", iMACAddress, tmp); 17203ad032bSPeter Holik if (tmp >= 0) 17303ad032bSPeter Holik tmp = -EINVAL; 17403ad032bSPeter Holik return tmp; 17503ad032bSPeter Holik } 17603ad032bSPeter Holik for (i = tmp = 0; i < 6; i++, tmp += 2) 17703ad032bSPeter Holik dev->net->dev_addr [i] = 178fd1f170dSAndy Shevchenko (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]); 17903ad032bSPeter Holik return 0; 18003ad032bSPeter Holik } 18103ad032bSPeter Holik EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); 18203ad032bSPeter Holik 18324ead299Stom.leiming@gmail.com static void intr_complete (struct urb *urb) 18424ead299Stom.leiming@gmail.com { 18524ead299Stom.leiming@gmail.com struct usbnet *dev = urb->context; 18624ead299Stom.leiming@gmail.com int status = urb->status; 18724ead299Stom.leiming@gmail.com 18824ead299Stom.leiming@gmail.com switch (status) { 18924ead299Stom.leiming@gmail.com /* success */ 19024ead299Stom.leiming@gmail.com case 0: 19124ead299Stom.leiming@gmail.com dev->driver_info->status(dev, urb); 19224ead299Stom.leiming@gmail.com break; 19324ead299Stom.leiming@gmail.com 19424ead299Stom.leiming@gmail.com /* software-driven interface shutdown */ 19524ead299Stom.leiming@gmail.com case -ENOENT: /* urb killed */ 19624ead299Stom.leiming@gmail.com case -ESHUTDOWN: /* hardware gone */ 19724ead299Stom.leiming@gmail.com netif_dbg(dev, ifdown, dev->net, 19824ead299Stom.leiming@gmail.com "intr shutdown, code %d\n", status); 19924ead299Stom.leiming@gmail.com return; 20024ead299Stom.leiming@gmail.com 20124ead299Stom.leiming@gmail.com /* NOTE: not throttling like RX/TX, since this endpoint 20224ead299Stom.leiming@gmail.com * already polls infrequently 20324ead299Stom.leiming@gmail.com */ 20424ead299Stom.leiming@gmail.com default: 20524ead299Stom.leiming@gmail.com netdev_dbg(dev->net, "intr status %d\n", status); 20624ead299Stom.leiming@gmail.com break; 20724ead299Stom.leiming@gmail.com } 20824ead299Stom.leiming@gmail.com 20924ead299Stom.leiming@gmail.com if (!netif_running (dev->net)) 21024ead299Stom.leiming@gmail.com return; 21124ead299Stom.leiming@gmail.com 21224ead299Stom.leiming@gmail.com status = usb_submit_urb (urb, GFP_ATOMIC); 21324ead299Stom.leiming@gmail.com if (status != 0) 21424ead299Stom.leiming@gmail.com netif_err(dev, timer, dev->net, 21524ead299Stom.leiming@gmail.com "intr resubmit --> %d\n", status); 21624ead299Stom.leiming@gmail.com } 2175b2fc499SJeff Garzik 2185b2fc499SJeff Garzik static int init_status (struct usbnet *dev, struct usb_interface *intf) 2195b2fc499SJeff Garzik { 2205b2fc499SJeff Garzik char *buf = NULL; 2215b2fc499SJeff Garzik unsigned pipe = 0; 2225b2fc499SJeff Garzik unsigned maxp; 2235b2fc499SJeff Garzik unsigned period; 2245b2fc499SJeff Garzik 2255b2fc499SJeff Garzik if (!dev->driver_info->status) 2265b2fc499SJeff Garzik return 0; 2275b2fc499SJeff Garzik 2285b2fc499SJeff Garzik pipe = usb_rcvintpipe (dev->udev, 2295b2fc499SJeff Garzik dev->status->desc.bEndpointAddress 2305b2fc499SJeff Garzik & USB_ENDPOINT_NUMBER_MASK); 2315b2fc499SJeff Garzik maxp = usb_maxpacket (dev->udev, pipe, 0); 2325b2fc499SJeff Garzik 2335b2fc499SJeff Garzik /* avoid 1 msec chatter: min 8 msec poll rate */ 2345b2fc499SJeff Garzik period = max ((int) dev->status->desc.bInterval, 2355b2fc499SJeff Garzik (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); 2365b2fc499SJeff Garzik 2375b2fc499SJeff Garzik buf = kmalloc (maxp, GFP_KERNEL); 2385b2fc499SJeff Garzik if (buf) { 2395b2fc499SJeff Garzik dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); 2405b2fc499SJeff Garzik if (!dev->interrupt) { 2415b2fc499SJeff Garzik kfree (buf); 2425b2fc499SJeff Garzik return -ENOMEM; 2435b2fc499SJeff Garzik } else { 2445b2fc499SJeff Garzik usb_fill_int_urb(dev->interrupt, dev->udev, pipe, 2455b2fc499SJeff Garzik buf, maxp, intr_complete, dev, period); 246720f3d7cStom.leiming@gmail.com dev->interrupt->transfer_flags |= URB_FREE_BUFFER; 2475b2fc499SJeff Garzik dev_dbg(&intf->dev, 2485b2fc499SJeff Garzik "status ep%din, %d bytes period %d\n", 2495b2fc499SJeff Garzik usb_pipeendpoint(pipe), maxp, period); 2505b2fc499SJeff Garzik } 2515b2fc499SJeff Garzik } 2525b2fc499SJeff Garzik return 0; 2535b2fc499SJeff Garzik } 2545b2fc499SJeff Garzik 2555b2fc499SJeff Garzik /* Passes this packet up the stack, updating its accounting. 2565b2fc499SJeff Garzik * Some link protocols batch packets, so their rx_fixup paths 2575b2fc499SJeff Garzik * can return clones as well as just modify the original skb. 2585b2fc499SJeff Garzik */ 2595b2fc499SJeff Garzik void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) 2605b2fc499SJeff Garzik { 2615b2fc499SJeff Garzik int status; 2625b2fc499SJeff Garzik 2637834ddbcSJussi Kivilinna if (test_bit(EVENT_RX_PAUSED, &dev->flags)) { 2647834ddbcSJussi Kivilinna skb_queue_tail(&dev->rxq_pause, skb); 2657834ddbcSJussi Kivilinna return; 2667834ddbcSJussi Kivilinna } 2677834ddbcSJussi Kivilinna 2685b2fc499SJeff Garzik skb->protocol = eth_type_trans (skb, dev->net); 2697963837fSHerbert Xu dev->net->stats.rx_packets++; 2707963837fSHerbert Xu dev->net->stats.rx_bytes += skb->len; 2715b2fc499SJeff Garzik 272a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n", 2735b2fc499SJeff Garzik skb->len + sizeof (struct ethhdr), skb->protocol); 2745b2fc499SJeff Garzik memset (skb->cb, 0, sizeof (struct skb_data)); 275f9b491ecSMichael Riesch 276f9b491ecSMichael Riesch if (skb_defer_rx_timestamp(skb)) 277f9b491ecSMichael Riesch return; 278f9b491ecSMichael Riesch 2795b2fc499SJeff Garzik status = netif_rx (skb); 280a475f603SJoe Perches if (status != NET_RX_SUCCESS) 281a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 282a475f603SJoe Perches "netif_rx status %d\n", status); 2835b2fc499SJeff Garzik } 2845b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_skb_return); 2855b2fc499SJeff Garzik 2865b2fc499SJeff Garzik 2875b2fc499SJeff Garzik /*------------------------------------------------------------------------- 2885b2fc499SJeff Garzik * 2895b2fc499SJeff Garzik * Network Device Driver (peer link to "Host Device", from USB host) 2905b2fc499SJeff Garzik * 2915b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 2925b2fc499SJeff Garzik 293777baa47SStephen Hemminger int usbnet_change_mtu (struct net_device *net, int new_mtu) 2945b2fc499SJeff Garzik { 2955b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 2965b2fc499SJeff Garzik int ll_mtu = new_mtu + net->hard_header_len; 2975b2fc499SJeff Garzik int old_hard_mtu = dev->hard_mtu; 2985b2fc499SJeff Garzik int old_rx_urb_size = dev->rx_urb_size; 2995b2fc499SJeff Garzik 3005b2fc499SJeff Garzik if (new_mtu <= 0) 3015b2fc499SJeff Garzik return -EINVAL; 3025b2fc499SJeff Garzik // no second zero-length packet read wanted after mtu-sized packets 3035b2fc499SJeff Garzik if ((ll_mtu % dev->maxpacket) == 0) 3045b2fc499SJeff Garzik return -EDOM; 3055b2fc499SJeff Garzik net->mtu = new_mtu; 3065b2fc499SJeff Garzik 3075b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 3085b2fc499SJeff Garzik if (dev->rx_urb_size == old_hard_mtu) { 3095b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 3105b2fc499SJeff Garzik if (dev->rx_urb_size > old_rx_urb_size) 3115b2fc499SJeff Garzik usbnet_unlink_rx_urbs(dev); 3125b2fc499SJeff Garzik } 3135b2fc499SJeff Garzik 3145b2fc499SJeff Garzik return 0; 3155b2fc499SJeff Garzik } 316777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_change_mtu); 3175b2fc499SJeff Garzik 3185b6e9bcdSMing Lei /* The caller must hold list->lock */ 3195b6e9bcdSMing Lei static void __usbnet_queue_skb(struct sk_buff_head *list, 3205b6e9bcdSMing Lei struct sk_buff *newsk, enum skb_state state) 3215b6e9bcdSMing Lei { 3225b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) newsk->cb; 3235b6e9bcdSMing Lei 3245b6e9bcdSMing Lei __skb_queue_tail(list, newsk); 3255b6e9bcdSMing Lei entry->state = state; 3265b6e9bcdSMing Lei } 3275b6e9bcdSMing Lei 3285b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 3295b2fc499SJeff Garzik 3305b2fc499SJeff Garzik /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from 3315b2fc499SJeff Garzik * completion callbacks. 2.5 should have fixed those bugs... 3325b2fc499SJeff Garzik */ 3335b2fc499SJeff Garzik 3345b6e9bcdSMing Lei static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb, 3355b6e9bcdSMing Lei struct sk_buff_head *list, enum skb_state state) 3365b2fc499SJeff Garzik { 3375b2fc499SJeff Garzik unsigned long flags; 3385b6e9bcdSMing Lei enum skb_state old_state; 3395b6e9bcdSMing Lei struct skb_data *entry = (struct skb_data *) skb->cb; 3405b2fc499SJeff Garzik 3415b2fc499SJeff Garzik spin_lock_irqsave(&list->lock, flags); 3425b6e9bcdSMing Lei old_state = entry->state; 3435b6e9bcdSMing Lei entry->state = state; 3445b2fc499SJeff Garzik __skb_unlink(skb, list); 3455b2fc499SJeff Garzik spin_unlock(&list->lock); 3465b2fc499SJeff Garzik spin_lock(&dev->done.lock); 3475b2fc499SJeff Garzik __skb_queue_tail(&dev->done, skb); 3485b2fc499SJeff Garzik if (dev->done.qlen == 1) 3495b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 3505b2fc499SJeff Garzik spin_unlock_irqrestore(&dev->done.lock, flags); 3515b6e9bcdSMing Lei return old_state; 3525b2fc499SJeff Garzik } 3535b2fc499SJeff Garzik 3545b2fc499SJeff Garzik /* some work can't be done in tasklets, so we use keventd 3555b2fc499SJeff Garzik * 3565b2fc499SJeff Garzik * NOTE: annoying asymmetry: if it's active, schedule_work() fails, 3575b2fc499SJeff Garzik * but tasklet_schedule() doesn't. hope the failure is rare. 3585b2fc499SJeff Garzik */ 3595b2fc499SJeff Garzik void usbnet_defer_kevent (struct usbnet *dev, int work) 3605b2fc499SJeff Garzik { 3615b2fc499SJeff Garzik set_bit (work, &dev->flags); 3629532021dSSteve Glendinning if (!schedule_work (&dev->kevent)) { 3639532021dSSteve Glendinning if (net_ratelimit()) 36460b86755SJoe Perches netdev_err(dev->net, "kevent %d may have been dropped\n", work); 3659532021dSSteve Glendinning } else { 36660b86755SJoe Perches netdev_dbg(dev->net, "kevent %d scheduled\n", work); 3675b2fc499SJeff Garzik } 3689532021dSSteve Glendinning } 3695b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_defer_kevent); 3705b2fc499SJeff Garzik 3715b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 3725b2fc499SJeff Garzik 3735b2fc499SJeff Garzik static void rx_complete (struct urb *urb); 3745b2fc499SJeff Garzik 375dacb3975SDavid S. Miller static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) 3765b2fc499SJeff Garzik { 3775b2fc499SJeff Garzik struct sk_buff *skb; 3785b2fc499SJeff Garzik struct skb_data *entry; 3795b2fc499SJeff Garzik int retval = 0; 3805b2fc499SJeff Garzik unsigned long lockflags; 3815b2fc499SJeff Garzik size_t size = dev->rx_urb_size; 3825b2fc499SJeff Garzik 38370c37bf9SBjørn Mork /* prevent rx skb allocation when error ratio is high */ 38470c37bf9SBjørn Mork if (test_bit(EVENT_RX_KILL, &dev->flags)) { 38570c37bf9SBjørn Mork usb_free_urb(urb); 38670c37bf9SBjørn Mork return -ENOLINK; 38770c37bf9SBjørn Mork } 38870c37bf9SBjørn Mork 3897bdd4027SEric Dumazet skb = __netdev_alloc_skb_ip_align(dev->net, size, flags); 3907bdd4027SEric Dumazet if (!skb) { 391a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no rx skb\n"); 3925b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 3935b2fc499SJeff Garzik usb_free_urb (urb); 394dacb3975SDavid S. Miller return -ENOMEM; 3955b2fc499SJeff Garzik } 3965b2fc499SJeff Garzik 3975b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 3985b2fc499SJeff Garzik entry->urb = urb; 3995b2fc499SJeff Garzik entry->dev = dev; 4005b2fc499SJeff Garzik entry->length = 0; 4015b2fc499SJeff Garzik 4025b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->in, 4035b2fc499SJeff Garzik skb->data, size, rx_complete, skb); 4045b2fc499SJeff Garzik 4055b2fc499SJeff Garzik spin_lock_irqsave (&dev->rxq.lock, lockflags); 4065b2fc499SJeff Garzik 4078e95a202SJoe Perches if (netif_running (dev->net) && 4088e95a202SJoe Perches netif_device_present (dev->net) && 40969ee472fSOliver Neukum !test_bit (EVENT_RX_HALT, &dev->flags) && 41069ee472fSOliver Neukum !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { 4115b2fc499SJeff Garzik switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { 4125b2fc499SJeff Garzik case -EPIPE: 4135b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 4145b2fc499SJeff Garzik break; 4155b2fc499SJeff Garzik case -ENOMEM: 4165b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_MEMORY); 4175b2fc499SJeff Garzik break; 4185b2fc499SJeff Garzik case -ENODEV: 419a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "device gone\n"); 4205b2fc499SJeff Garzik netif_device_detach (dev->net); 4215b2fc499SJeff Garzik break; 422dacb3975SDavid S. Miller case -EHOSTUNREACH: 423dacb3975SDavid S. Miller retval = -ENOLINK; 424dacb3975SDavid S. Miller break; 4255b2fc499SJeff Garzik default: 426a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 427a475f603SJoe Perches "rx submit, %d\n", retval); 4285b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 4295b2fc499SJeff Garzik break; 4305b2fc499SJeff Garzik case 0: 4315b6e9bcdSMing Lei __usbnet_queue_skb(&dev->rxq, skb, rx_start); 4325b2fc499SJeff Garzik } 4335b2fc499SJeff Garzik } else { 434a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "rx: stopped\n"); 4355b2fc499SJeff Garzik retval = -ENOLINK; 4365b2fc499SJeff Garzik } 4375b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->rxq.lock, lockflags); 4385b2fc499SJeff Garzik if (retval) { 4395b2fc499SJeff Garzik dev_kfree_skb_any (skb); 4405b2fc499SJeff Garzik usb_free_urb (urb); 4415b2fc499SJeff Garzik } 442dacb3975SDavid S. Miller return retval; 4435b2fc499SJeff Garzik } 4445b2fc499SJeff Garzik 4455b2fc499SJeff Garzik 4465b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4475b2fc499SJeff Garzik 4485b2fc499SJeff Garzik static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) 4495b2fc499SJeff Garzik { 4508e95a202SJoe Perches if (dev->driver_info->rx_fixup && 4517a635ea9SAndrzej Zaborowski !dev->driver_info->rx_fixup (dev, skb)) { 4527a635ea9SAndrzej Zaborowski /* With RX_ASSEMBLE, rx_fixup() must update counters */ 4537a635ea9SAndrzej Zaborowski if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE)) 4547a635ea9SAndrzej Zaborowski dev->net->stats.rx_errors++; 4557a635ea9SAndrzej Zaborowski goto done; 4567a635ea9SAndrzej Zaborowski } 4575b2fc499SJeff Garzik // else network stack removes extra byte if we forced a short packet 4585b2fc499SJeff Garzik 459073285fdSAlexey Orishko if (skb->len) { 460073285fdSAlexey Orishko /* all data was already cloned from skb inside the driver */ 461073285fdSAlexey Orishko if (dev->driver_info->flags & FLAG_MULTI_PACKET) 462073285fdSAlexey Orishko dev_kfree_skb_any(skb); 463073285fdSAlexey Orishko else 4645b2fc499SJeff Garzik usbnet_skb_return(dev, skb); 465073285fdSAlexey Orishko return; 466073285fdSAlexey Orishko } 467073285fdSAlexey Orishko 468a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "drop\n"); 4697963837fSHerbert Xu dev->net->stats.rx_errors++; 4707a635ea9SAndrzej Zaborowski done: 4715b2fc499SJeff Garzik skb_queue_tail(&dev->done, skb); 4725b2fc499SJeff Garzik } 4735b2fc499SJeff Garzik 4745b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 4755b2fc499SJeff Garzik 4765b2fc499SJeff Garzik static void rx_complete (struct urb *urb) 4775b2fc499SJeff Garzik { 4785b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 4795b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 4805b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 4815b2fc499SJeff Garzik int urb_status = urb->status; 4825b6e9bcdSMing Lei enum skb_state state; 4835b2fc499SJeff Garzik 4845b2fc499SJeff Garzik skb_put (skb, urb->actual_length); 4855b6e9bcdSMing Lei state = rx_done; 4865b2fc499SJeff Garzik entry->urb = NULL; 4875b2fc499SJeff Garzik 4885b2fc499SJeff Garzik switch (urb_status) { 48918ab458fSDavid Brownell /* success */ 4905b2fc499SJeff Garzik case 0: 4915b2fc499SJeff Garzik if (skb->len < dev->net->hard_header_len) { 4925b6e9bcdSMing Lei state = rx_cleanup; 4937963837fSHerbert Xu dev->net->stats.rx_errors++; 4947963837fSHerbert Xu dev->net->stats.rx_length_errors++; 495a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 496a475f603SJoe Perches "rx length %d\n", skb->len); 4975b2fc499SJeff Garzik } 4985b2fc499SJeff Garzik break; 4995b2fc499SJeff Garzik 50018ab458fSDavid Brownell /* stalls need manual reset. this is rare ... except that 50118ab458fSDavid Brownell * when going through USB 2.0 TTs, unplug appears this way. 5023ac49a1cSJean Delvare * we avoid the highspeed version of the ETIMEDOUT/EILSEQ 50318ab458fSDavid Brownell * storm, recovering as needed. 50418ab458fSDavid Brownell */ 5055b2fc499SJeff Garzik case -EPIPE: 5067963837fSHerbert Xu dev->net->stats.rx_errors++; 5075b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_RX_HALT); 5085b2fc499SJeff Garzik // FALLTHROUGH 5095b2fc499SJeff Garzik 51018ab458fSDavid Brownell /* software-driven interface shutdown */ 51118ab458fSDavid Brownell case -ECONNRESET: /* async unlink */ 51218ab458fSDavid Brownell case -ESHUTDOWN: /* hardware gone */ 513a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 514a475f603SJoe Perches "rx shutdown, code %d\n", urb_status); 5155b2fc499SJeff Garzik goto block; 5165b2fc499SJeff Garzik 51718ab458fSDavid Brownell /* we get controller i/o faults during khubd disconnect() delays. 51818ab458fSDavid Brownell * throttle down resubmits, to avoid log floods; just temporarily, 51918ab458fSDavid Brownell * so we still recover when the fault isn't a khubd delay. 52018ab458fSDavid Brownell */ 5215b2fc499SJeff Garzik case -EPROTO: 5225b2fc499SJeff Garzik case -ETIME: 5235b2fc499SJeff Garzik case -EILSEQ: 5247963837fSHerbert Xu dev->net->stats.rx_errors++; 5255b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 5265b2fc499SJeff Garzik mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); 527a475f603SJoe Perches netif_dbg(dev, link, dev->net, 528a475f603SJoe Perches "rx throttle %d\n", urb_status); 5295b2fc499SJeff Garzik } 5305b2fc499SJeff Garzik block: 5315b6e9bcdSMing Lei state = rx_cleanup; 5325b2fc499SJeff Garzik entry->urb = urb; 5335b2fc499SJeff Garzik urb = NULL; 5345b2fc499SJeff Garzik break; 5355b2fc499SJeff Garzik 53618ab458fSDavid Brownell /* data overrun ... flush fifo? */ 5375b2fc499SJeff Garzik case -EOVERFLOW: 5387963837fSHerbert Xu dev->net->stats.rx_over_errors++; 5395b2fc499SJeff Garzik // FALLTHROUGH 5405b2fc499SJeff Garzik 5415b2fc499SJeff Garzik default: 5425b6e9bcdSMing Lei state = rx_cleanup; 5437963837fSHerbert Xu dev->net->stats.rx_errors++; 544a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status); 5455b2fc499SJeff Garzik break; 5465b2fc499SJeff Garzik } 5475b2fc499SJeff Garzik 54870c37bf9SBjørn Mork /* stop rx if packet error rate is high */ 54970c37bf9SBjørn Mork if (++dev->pkt_cnt > 30) { 55070c37bf9SBjørn Mork dev->pkt_cnt = 0; 55170c37bf9SBjørn Mork dev->pkt_err = 0; 55270c37bf9SBjørn Mork } else { 55370c37bf9SBjørn Mork if (state == rx_cleanup) 55470c37bf9SBjørn Mork dev->pkt_err++; 55570c37bf9SBjørn Mork if (dev->pkt_err > 20) 55670c37bf9SBjørn Mork set_bit(EVENT_RX_KILL, &dev->flags); 55770c37bf9SBjørn Mork } 55870c37bf9SBjørn Mork 5595b6e9bcdSMing Lei state = defer_bh(dev, skb, &dev->rxq, state); 5605b2fc499SJeff Garzik 5615b2fc499SJeff Garzik if (urb) { 5628e95a202SJoe Perches if (netif_running (dev->net) && 5635b6e9bcdSMing Lei !test_bit (EVENT_RX_HALT, &dev->flags) && 5645b6e9bcdSMing Lei state != unlink_start) { 5655b2fc499SJeff Garzik rx_submit (dev, urb, GFP_ATOMIC); 5668a783354SOliver Neukum usb_mark_last_busy(dev->udev); 5675b2fc499SJeff Garzik return; 5685b2fc499SJeff Garzik } 5695b2fc499SJeff Garzik usb_free_urb (urb); 5705b2fc499SJeff Garzik } 571a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n"); 5725b2fc499SJeff Garzik } 5735b2fc499SJeff Garzik 5745b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 5757834ddbcSJussi Kivilinna void usbnet_pause_rx(struct usbnet *dev) 5767834ddbcSJussi Kivilinna { 5777834ddbcSJussi Kivilinna set_bit(EVENT_RX_PAUSED, &dev->flags); 5787834ddbcSJussi Kivilinna 579a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n"); 5807834ddbcSJussi Kivilinna } 5817834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_pause_rx); 5827834ddbcSJussi Kivilinna 5837834ddbcSJussi Kivilinna void usbnet_resume_rx(struct usbnet *dev) 5847834ddbcSJussi Kivilinna { 5857834ddbcSJussi Kivilinna struct sk_buff *skb; 5867834ddbcSJussi Kivilinna int num = 0; 5877834ddbcSJussi Kivilinna 5887834ddbcSJussi Kivilinna clear_bit(EVENT_RX_PAUSED, &dev->flags); 5897834ddbcSJussi Kivilinna 5907834ddbcSJussi Kivilinna while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { 5917834ddbcSJussi Kivilinna usbnet_skb_return(dev, skb); 5927834ddbcSJussi Kivilinna num++; 5937834ddbcSJussi Kivilinna } 5947834ddbcSJussi Kivilinna 5957834ddbcSJussi Kivilinna tasklet_schedule(&dev->bh); 5967834ddbcSJussi Kivilinna 597a475f603SJoe Perches netif_dbg(dev, rx_status, dev->net, 598a475f603SJoe Perches "paused rx queue disabled, %d skbs requeued\n", num); 5997834ddbcSJussi Kivilinna } 6007834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_resume_rx); 6017834ddbcSJussi Kivilinna 6027834ddbcSJussi Kivilinna void usbnet_purge_paused_rxq(struct usbnet *dev) 6037834ddbcSJussi Kivilinna { 6047834ddbcSJussi Kivilinna skb_queue_purge(&dev->rxq_pause); 6057834ddbcSJussi Kivilinna } 6067834ddbcSJussi Kivilinna EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); 6077834ddbcSJussi Kivilinna 6087834ddbcSJussi Kivilinna /*-------------------------------------------------------------------------*/ 6095b2fc499SJeff Garzik 6105b2fc499SJeff Garzik // unlink pending rx/tx; completion handlers do all other cleanup 6115b2fc499SJeff Garzik 6125b2fc499SJeff Garzik static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) 6135b2fc499SJeff Garzik { 6145b2fc499SJeff Garzik unsigned long flags; 6155b6e9bcdSMing Lei struct sk_buff *skb; 6165b2fc499SJeff Garzik int count = 0; 6175b2fc499SJeff Garzik 6185b2fc499SJeff Garzik spin_lock_irqsave (&q->lock, flags); 6195b6e9bcdSMing Lei while (!skb_queue_empty(q)) { 6205b2fc499SJeff Garzik struct skb_data *entry; 6215b2fc499SJeff Garzik struct urb *urb; 6225b2fc499SJeff Garzik int retval; 6235b2fc499SJeff Garzik 6245b6e9bcdSMing Lei skb_queue_walk(q, skb) { 6255b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 6265b6e9bcdSMing Lei if (entry->state != unlink_start) 6275b6e9bcdSMing Lei goto found; 6285b6e9bcdSMing Lei } 6295b6e9bcdSMing Lei break; 6305b6e9bcdSMing Lei found: 6315b6e9bcdSMing Lei entry->state = unlink_start; 6325b2fc499SJeff Garzik urb = entry->urb; 6335b2fc499SJeff Garzik 6340956a8c2Stom.leiming@gmail.com /* 6350956a8c2Stom.leiming@gmail.com * Get reference count of the URB to avoid it to be 6360956a8c2Stom.leiming@gmail.com * freed during usb_unlink_urb, which may trigger 6370956a8c2Stom.leiming@gmail.com * use-after-free problem inside usb_unlink_urb since 6380956a8c2Stom.leiming@gmail.com * usb_unlink_urb is always racing with .complete 6390956a8c2Stom.leiming@gmail.com * handler(include defer_bh). 6400956a8c2Stom.leiming@gmail.com */ 6410956a8c2Stom.leiming@gmail.com usb_get_urb(urb); 6424231d47eSSebastian Siewior spin_unlock_irqrestore(&q->lock, flags); 6435b2fc499SJeff Garzik // during some PM-driven resume scenarios, 6445b2fc499SJeff Garzik // these (async) unlinks complete immediately 6455b2fc499SJeff Garzik retval = usb_unlink_urb (urb); 6465b2fc499SJeff Garzik if (retval != -EINPROGRESS && retval != 0) 64760b86755SJoe Perches netdev_dbg(dev->net, "unlink urb err, %d\n", retval); 6485b2fc499SJeff Garzik else 6495b2fc499SJeff Garzik count++; 6500956a8c2Stom.leiming@gmail.com usb_put_urb(urb); 6514231d47eSSebastian Siewior spin_lock_irqsave(&q->lock, flags); 6525b2fc499SJeff Garzik } 6535b2fc499SJeff Garzik spin_unlock_irqrestore (&q->lock, flags); 6545b2fc499SJeff Garzik return count; 6555b2fc499SJeff Garzik } 6565b2fc499SJeff Garzik 6575b2fc499SJeff Garzik // Flush all pending rx urbs 6585b2fc499SJeff Garzik // minidrivers may need to do this when the MTU changes 6595b2fc499SJeff Garzik 6605b2fc499SJeff Garzik void usbnet_unlink_rx_urbs(struct usbnet *dev) 6615b2fc499SJeff Garzik { 6625b2fc499SJeff Garzik if (netif_running(dev->net)) { 6635b2fc499SJeff Garzik (void) unlink_urbs (dev, &dev->rxq); 6645b2fc499SJeff Garzik tasklet_schedule(&dev->bh); 6655b2fc499SJeff Garzik } 6665b2fc499SJeff Garzik } 6675b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); 6685b2fc499SJeff Garzik 6695b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 6705b2fc499SJeff Garzik 6715b2fc499SJeff Garzik // precondition: never called in_interrupt 67269ee472fSOliver Neukum static void usbnet_terminate_urbs(struct usbnet *dev) 67369ee472fSOliver Neukum { 67469ee472fSOliver Neukum DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup); 67569ee472fSOliver Neukum DECLARE_WAITQUEUE(wait, current); 67669ee472fSOliver Neukum int temp; 67769ee472fSOliver Neukum 67869ee472fSOliver Neukum /* ensure there are no more active urbs */ 67969ee472fSOliver Neukum add_wait_queue(&unlink_wakeup, &wait); 68069ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 68169ee472fSOliver Neukum dev->wait = &unlink_wakeup; 68269ee472fSOliver Neukum temp = unlink_urbs(dev, &dev->txq) + 68369ee472fSOliver Neukum unlink_urbs(dev, &dev->rxq); 68469ee472fSOliver Neukum 68569ee472fSOliver Neukum /* maybe wait for deletions to finish. */ 68669ee472fSOliver Neukum while (!skb_queue_empty(&dev->rxq) 68769ee472fSOliver Neukum && !skb_queue_empty(&dev->txq) 68869ee472fSOliver Neukum && !skb_queue_empty(&dev->done)) { 68966cc42a4SKulikov Vasiliy schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); 69069ee472fSOliver Neukum set_current_state(TASK_UNINTERRUPTIBLE); 691a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, 692a475f603SJoe Perches "waited for %d urb completions\n", temp); 69369ee472fSOliver Neukum } 69469ee472fSOliver Neukum set_current_state(TASK_RUNNING); 69569ee472fSOliver Neukum dev->wait = NULL; 69669ee472fSOliver Neukum remove_wait_queue(&unlink_wakeup, &wait); 69769ee472fSOliver Neukum } 6985b2fc499SJeff Garzik 699777baa47SStephen Hemminger int usbnet_stop (struct net_device *net) 7005b2fc499SJeff Garzik { 7015b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 702a33e9e7fSJussi Kivilinna struct driver_info *info = dev->driver_info; 703a33e9e7fSJussi Kivilinna int retval; 7045b2fc499SJeff Garzik 70575bd0cbdSMing Lei clear_bit(EVENT_DEV_OPEN, &dev->flags); 7065b2fc499SJeff Garzik netif_stop_queue (net); 7075b2fc499SJeff Garzik 708a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 7098ccef431SBen Hutchings "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", 7107963837fSHerbert Xu net->stats.rx_packets, net->stats.tx_packets, 71160b86755SJoe Perches net->stats.rx_errors, net->stats.tx_errors); 7125b2fc499SJeff Garzik 713a33e9e7fSJussi Kivilinna /* allow minidriver to stop correctly (wireless devices to turn off 714a33e9e7fSJussi Kivilinna * radio etc) */ 715a33e9e7fSJussi Kivilinna if (info->stop) { 716a33e9e7fSJussi Kivilinna retval = info->stop(dev); 717a475f603SJoe Perches if (retval < 0) 718a475f603SJoe Perches netif_info(dev, ifdown, dev->net, 71960b86755SJoe Perches "stop fail (%d) usbnet usb-%s-%s, %s\n", 720a33e9e7fSJussi Kivilinna retval, 721a33e9e7fSJussi Kivilinna dev->udev->bus->bus_name, dev->udev->devpath, 722a33e9e7fSJussi Kivilinna info->description); 723a33e9e7fSJussi Kivilinna } 724a33e9e7fSJussi Kivilinna 72569ee472fSOliver Neukum if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) 72669ee472fSOliver Neukum usbnet_terminate_urbs(dev); 7275b2fc499SJeff Garzik 7285b2fc499SJeff Garzik usb_kill_urb(dev->interrupt); 7295b2fc499SJeff Garzik 7307834ddbcSJussi Kivilinna usbnet_purge_paused_rxq(dev); 7317834ddbcSJussi Kivilinna 7325b2fc499SJeff Garzik /* deferred work (task, timer, softirq) must also stop. 7335b2fc499SJeff Garzik * can't flush_scheduled_work() until we drop rtnl (later), 7345b2fc499SJeff Garzik * else workers could deadlock; so make workers a NOP. 7355b2fc499SJeff Garzik */ 7365b2fc499SJeff Garzik dev->flags = 0; 7375b2fc499SJeff Garzik del_timer_sync (&dev->delay); 7385b2fc499SJeff Garzik tasklet_kill (&dev->bh); 739a1c088e0SOliver Neukum if (info->manage_power && 740a1c088e0SOliver Neukum !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags)) 74169ee472fSOliver Neukum info->manage_power(dev, 0); 74269ee472fSOliver Neukum else 743a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 7445b2fc499SJeff Garzik 7455b2fc499SJeff Garzik return 0; 7465b2fc499SJeff Garzik } 747777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_stop); 7485b2fc499SJeff Garzik 7495b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 7505b2fc499SJeff Garzik 7515b2fc499SJeff Garzik // posts reads, and enables write queuing 7525b2fc499SJeff Garzik 7535b2fc499SJeff Garzik // precondition: never called in_interrupt 7545b2fc499SJeff Garzik 755777baa47SStephen Hemminger int usbnet_open (struct net_device *net) 7565b2fc499SJeff Garzik { 7575b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 758a11a6544SOliver Neukum int retval; 7595b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 7605b2fc499SJeff Garzik 761a11a6544SOliver Neukum if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { 762a475f603SJoe Perches netif_info(dev, ifup, dev->net, 76360b86755SJoe Perches "resumption fail (%d) usbnet usb-%s-%s, %s\n", 764a11a6544SOliver Neukum retval, 76560b86755SJoe Perches dev->udev->bus->bus_name, 76660b86755SJoe Perches dev->udev->devpath, 767a11a6544SOliver Neukum info->description); 768a11a6544SOliver Neukum goto done_nopm; 769a11a6544SOliver Neukum } 770a11a6544SOliver Neukum 7715b2fc499SJeff Garzik // put into "known safe" state 7725b2fc499SJeff Garzik if (info->reset && (retval = info->reset (dev)) < 0) { 773a475f603SJoe Perches netif_info(dev, ifup, dev->net, 77460b86755SJoe Perches "open reset fail (%d) usbnet usb-%s-%s, %s\n", 7755b2fc499SJeff Garzik retval, 77660b86755SJoe Perches dev->udev->bus->bus_name, 77760b86755SJoe Perches dev->udev->devpath, 7785b2fc499SJeff Garzik info->description); 7795b2fc499SJeff Garzik goto done; 7805b2fc499SJeff Garzik } 7815b2fc499SJeff Garzik 7825b2fc499SJeff Garzik // insist peer be connected 7835b2fc499SJeff Garzik if (info->check_connect && (retval = info->check_connect (dev)) < 0) { 784a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval); 7855b2fc499SJeff Garzik goto done; 7865b2fc499SJeff Garzik } 7875b2fc499SJeff Garzik 7885b2fc499SJeff Garzik /* start any status interrupt transfer */ 7895b2fc499SJeff Garzik if (dev->interrupt) { 7905b2fc499SJeff Garzik retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); 7915b2fc499SJeff Garzik if (retval < 0) { 792a475f603SJoe Perches netif_err(dev, ifup, dev->net, 793a475f603SJoe Perches "intr submit %d\n", retval); 7945b2fc499SJeff Garzik goto done; 7955b2fc499SJeff Garzik } 7965b2fc499SJeff Garzik } 7975b2fc499SJeff Garzik 79868972efaSPaul Stewart set_bit(EVENT_DEV_OPEN, &dev->flags); 7995b2fc499SJeff Garzik netif_start_queue (net); 800a475f603SJoe Perches netif_info(dev, ifup, dev->net, 801a475f603SJoe Perches "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", 80260b86755SJoe Perches (int)RX_QLEN(dev), (int)TX_QLEN(dev), 803a475f603SJoe Perches dev->net->mtu, 804a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : 805a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : 806a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : 807a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : 808a475f603SJoe Perches (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : 809a475f603SJoe Perches "simple"); 8105b2fc499SJeff Garzik 81170c37bf9SBjørn Mork /* reset rx error state */ 81270c37bf9SBjørn Mork dev->pkt_cnt = 0; 81370c37bf9SBjørn Mork dev->pkt_err = 0; 81470c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 81570c37bf9SBjørn Mork 8165b2fc499SJeff Garzik // delay posting reads until we're fully open 8175b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 81869ee472fSOliver Neukum if (info->manage_power) { 81969ee472fSOliver Neukum retval = info->manage_power(dev, 1); 820a1c088e0SOliver Neukum if (retval < 0) { 821a1c088e0SOliver Neukum retval = 0; 822a1c088e0SOliver Neukum set_bit(EVENT_NO_RUNTIME_PM, &dev->flags); 823a1c088e0SOliver Neukum } else { 82469ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 82569ee472fSOliver Neukum } 826a1c088e0SOliver Neukum } 827a11a6544SOliver Neukum return retval; 8285b2fc499SJeff Garzik done: 829a11a6544SOliver Neukum usb_autopm_put_interface(dev->intf); 830a11a6544SOliver Neukum done_nopm: 8315b2fc499SJeff Garzik return retval; 8325b2fc499SJeff Garzik } 833777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_open); 8345b2fc499SJeff Garzik 8355b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 8365b2fc499SJeff Garzik 8375b2fc499SJeff Garzik /* ethtool methods; minidrivers may need to add some more, but 8385b2fc499SJeff Garzik * they'll probably want to use this base set. 8395b2fc499SJeff Garzik */ 8405b2fc499SJeff Garzik 8415b2fc499SJeff Garzik int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) 8425b2fc499SJeff Garzik { 8435b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 8445b2fc499SJeff Garzik 8455b2fc499SJeff Garzik if (!dev->mii.mdio_read) 8465b2fc499SJeff Garzik return -EOPNOTSUPP; 8475b2fc499SJeff Garzik 8485b2fc499SJeff Garzik return mii_ethtool_gset(&dev->mii, cmd); 8495b2fc499SJeff Garzik } 8505b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_settings); 8515b2fc499SJeff Garzik 8525b2fc499SJeff Garzik int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) 8535b2fc499SJeff Garzik { 8545b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 8555b2fc499SJeff Garzik int retval; 8565b2fc499SJeff Garzik 8575b2fc499SJeff Garzik if (!dev->mii.mdio_write) 8585b2fc499SJeff Garzik return -EOPNOTSUPP; 8595b2fc499SJeff Garzik 8605b2fc499SJeff Garzik retval = mii_ethtool_sset(&dev->mii, cmd); 8615b2fc499SJeff Garzik 8625b2fc499SJeff Garzik /* link speed/duplex might have changed */ 8635b2fc499SJeff Garzik if (dev->driver_info->link_reset) 8645b2fc499SJeff Garzik dev->driver_info->link_reset(dev); 8655b2fc499SJeff Garzik 8665b2fc499SJeff Garzik return retval; 8675b2fc499SJeff Garzik 8685b2fc499SJeff Garzik } 8695b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_settings); 8705b2fc499SJeff Garzik 8715b2fc499SJeff Garzik u32 usbnet_get_link (struct net_device *net) 8725b2fc499SJeff Garzik { 8735b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 8745b2fc499SJeff Garzik 8755b2fc499SJeff Garzik /* If a check_connect is defined, return its result */ 8765b2fc499SJeff Garzik if (dev->driver_info->check_connect) 8775b2fc499SJeff Garzik return dev->driver_info->check_connect (dev) == 0; 8785b2fc499SJeff Garzik 8795b2fc499SJeff Garzik /* if the device has mii operations, use those */ 8805b2fc499SJeff Garzik if (dev->mii.mdio_read) 8815b2fc499SJeff Garzik return mii_link_ok(&dev->mii); 8825b2fc499SJeff Garzik 88305ffb3e2SBjørn Mork /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ 88405ffb3e2SBjørn Mork return ethtool_op_get_link(net); 8855b2fc499SJeff Garzik } 8865b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_link); 8875b2fc499SJeff Garzik 8885b2fc499SJeff Garzik int usbnet_nway_reset(struct net_device *net) 8895b2fc499SJeff Garzik { 8905b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 8915b2fc499SJeff Garzik 8925b2fc499SJeff Garzik if (!dev->mii.mdio_write) 8935b2fc499SJeff Garzik return -EOPNOTSUPP; 8945b2fc499SJeff Garzik 8955b2fc499SJeff Garzik return mii_nway_restart(&dev->mii); 8965b2fc499SJeff Garzik } 8975b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_nway_reset); 8985b2fc499SJeff Garzik 8995b2fc499SJeff Garzik void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) 9005b2fc499SJeff Garzik { 9015b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9025b2fc499SJeff Garzik 90386a2f415SPhil Sutter strlcpy (info->driver, dev->driver_name, sizeof info->driver); 90486a2f415SPhil Sutter strlcpy (info->version, DRIVER_VERSION, sizeof info->version); 90586a2f415SPhil Sutter strlcpy (info->fw_version, dev->driver_info->description, 9065b2fc499SJeff Garzik sizeof info->fw_version); 9075b2fc499SJeff Garzik usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); 9085b2fc499SJeff Garzik } 9095b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); 9105b2fc499SJeff Garzik 9115b2fc499SJeff Garzik u32 usbnet_get_msglevel (struct net_device *net) 9125b2fc499SJeff Garzik { 9135b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9145b2fc499SJeff Garzik 9155b2fc499SJeff Garzik return dev->msg_enable; 9165b2fc499SJeff Garzik } 9175b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_get_msglevel); 9185b2fc499SJeff Garzik 9195b2fc499SJeff Garzik void usbnet_set_msglevel (struct net_device *net, u32 level) 9205b2fc499SJeff Garzik { 9215b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 9225b2fc499SJeff Garzik 9235b2fc499SJeff Garzik dev->msg_enable = level; 9245b2fc499SJeff Garzik } 9255b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_set_msglevel); 9265b2fc499SJeff Garzik 9275b2fc499SJeff Garzik /* drivers may override default ethtool_ops in their bind() routine */ 9280fc0b732SStephen Hemminger static const struct ethtool_ops usbnet_ethtool_ops = { 9295b2fc499SJeff Garzik .get_settings = usbnet_get_settings, 9305b2fc499SJeff Garzik .set_settings = usbnet_set_settings, 9315b2fc499SJeff Garzik .get_link = usbnet_get_link, 9325b2fc499SJeff Garzik .nway_reset = usbnet_nway_reset, 9335b2fc499SJeff Garzik .get_drvinfo = usbnet_get_drvinfo, 9345b2fc499SJeff Garzik .get_msglevel = usbnet_get_msglevel, 9355b2fc499SJeff Garzik .set_msglevel = usbnet_set_msglevel, 936123edb18SRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 9375b2fc499SJeff Garzik }; 9385b2fc499SJeff Garzik 9395b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 9405b2fc499SJeff Garzik 9415b2fc499SJeff Garzik /* work that cannot be done in interrupt context uses keventd. 9425b2fc499SJeff Garzik * 9435b2fc499SJeff Garzik * NOTE: with 2.5 we could do more of this using completion callbacks, 9445b2fc499SJeff Garzik * especially now that control transfers can be queued. 9455b2fc499SJeff Garzik */ 9465b2fc499SJeff Garzik static void 9475b2fc499SJeff Garzik kevent (struct work_struct *work) 9485b2fc499SJeff Garzik { 9495b2fc499SJeff Garzik struct usbnet *dev = 9505b2fc499SJeff Garzik container_of(work, struct usbnet, kevent); 9515b2fc499SJeff Garzik int status; 9525b2fc499SJeff Garzik 9535b2fc499SJeff Garzik /* usb_clear_halt() needs a thread context */ 9545b2fc499SJeff Garzik if (test_bit (EVENT_TX_HALT, &dev->flags)) { 9555b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 95669ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 95769ee472fSOliver Neukum if (status < 0) 95869ee472fSOliver Neukum goto fail_pipe; 9595b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->out); 96069ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 9618e95a202SJoe Perches if (status < 0 && 9628e95a202SJoe Perches status != -EPIPE && 9638e95a202SJoe Perches status != -ESHUTDOWN) { 9645b2fc499SJeff Garzik if (netif_msg_tx_err (dev)) 96569ee472fSOliver Neukum fail_pipe: 96660b86755SJoe Perches netdev_err(dev->net, "can't clear tx halt, status %d\n", 9675b2fc499SJeff Garzik status); 9685b2fc499SJeff Garzik } else { 9695b2fc499SJeff Garzik clear_bit (EVENT_TX_HALT, &dev->flags); 9705b2fc499SJeff Garzik if (status != -ESHUTDOWN) 9715b2fc499SJeff Garzik netif_wake_queue (dev->net); 9725b2fc499SJeff Garzik } 9735b2fc499SJeff Garzik } 9745b2fc499SJeff Garzik if (test_bit (EVENT_RX_HALT, &dev->flags)) { 9755b2fc499SJeff Garzik unlink_urbs (dev, &dev->rxq); 97669ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 97769ee472fSOliver Neukum if (status < 0) 97869ee472fSOliver Neukum goto fail_halt; 9795b2fc499SJeff Garzik status = usb_clear_halt (dev->udev, dev->in); 98069ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 9818e95a202SJoe Perches if (status < 0 && 9828e95a202SJoe Perches status != -EPIPE && 9838e95a202SJoe Perches status != -ESHUTDOWN) { 9845b2fc499SJeff Garzik if (netif_msg_rx_err (dev)) 98569ee472fSOliver Neukum fail_halt: 98660b86755SJoe Perches netdev_err(dev->net, "can't clear rx halt, status %d\n", 9875b2fc499SJeff Garzik status); 9885b2fc499SJeff Garzik } else { 9895b2fc499SJeff Garzik clear_bit (EVENT_RX_HALT, &dev->flags); 9905b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 9915b2fc499SJeff Garzik } 9925b2fc499SJeff Garzik } 9935b2fc499SJeff Garzik 9945b2fc499SJeff Garzik /* tasklet could resubmit itself forever if memory is tight */ 9955b2fc499SJeff Garzik if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { 9965b2fc499SJeff Garzik struct urb *urb = NULL; 997dacb3975SDavid S. Miller int resched = 1; 9985b2fc499SJeff Garzik 9995b2fc499SJeff Garzik if (netif_running (dev->net)) 10005b2fc499SJeff Garzik urb = usb_alloc_urb (0, GFP_KERNEL); 10015b2fc499SJeff Garzik else 10025b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 10035b2fc499SJeff Garzik if (urb != NULL) { 10045b2fc499SJeff Garzik clear_bit (EVENT_RX_MEMORY, &dev->flags); 100569ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 1006ab60707fSJesper Juhl if (status < 0) { 1007ab60707fSJesper Juhl usb_free_urb(urb); 100869ee472fSOliver Neukum goto fail_lowmem; 1009ab60707fSJesper Juhl } 1010dacb3975SDavid S. Miller if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) 1011dacb3975SDavid S. Miller resched = 0; 101269ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 101369ee472fSOliver Neukum fail_lowmem: 1014dacb3975SDavid S. Miller if (resched) 10155b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 10165b2fc499SJeff Garzik } 10175b2fc499SJeff Garzik } 10185b2fc499SJeff Garzik 10195b2fc499SJeff Garzik if (test_bit (EVENT_LINK_RESET, &dev->flags)) { 10205b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 10215b2fc499SJeff Garzik int retval = 0; 10225b2fc499SJeff Garzik 10235b2fc499SJeff Garzik clear_bit (EVENT_LINK_RESET, &dev->flags); 102469ee472fSOliver Neukum status = usb_autopm_get_interface(dev->intf); 102569ee472fSOliver Neukum if (status < 0) 102669ee472fSOliver Neukum goto skip_reset; 10275b2fc499SJeff Garzik if(info->link_reset && (retval = info->link_reset(dev)) < 0) { 102869ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 102969ee472fSOliver Neukum skip_reset: 103060b86755SJoe Perches netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", 10315b2fc499SJeff Garzik retval, 103260b86755SJoe Perches dev->udev->bus->bus_name, 103360b86755SJoe Perches dev->udev->devpath, 10345b2fc499SJeff Garzik info->description); 103569ee472fSOliver Neukum } else { 103669ee472fSOliver Neukum usb_autopm_put_interface(dev->intf); 10375b2fc499SJeff Garzik } 10385b2fc499SJeff Garzik } 10395b2fc499SJeff Garzik 10405b2fc499SJeff Garzik if (dev->flags) 104160b86755SJoe Perches netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); 10425b2fc499SJeff Garzik } 10435b2fc499SJeff Garzik 10445b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 10455b2fc499SJeff Garzik 10465b2fc499SJeff Garzik static void tx_complete (struct urb *urb) 10475b2fc499SJeff Garzik { 10485b2fc499SJeff Garzik struct sk_buff *skb = (struct sk_buff *) urb->context; 10495b2fc499SJeff Garzik struct skb_data *entry = (struct skb_data *) skb->cb; 10505b2fc499SJeff Garzik struct usbnet *dev = entry->dev; 10515b2fc499SJeff Garzik 10525b2fc499SJeff Garzik if (urb->status == 0) { 1053073285fdSAlexey Orishko if (!(dev->driver_info->flags & FLAG_MULTI_PACKET)) 10547963837fSHerbert Xu dev->net->stats.tx_packets++; 10557963837fSHerbert Xu dev->net->stats.tx_bytes += entry->length; 10565b2fc499SJeff Garzik } else { 10577963837fSHerbert Xu dev->net->stats.tx_errors++; 10585b2fc499SJeff Garzik 10595b2fc499SJeff Garzik switch (urb->status) { 10605b2fc499SJeff Garzik case -EPIPE: 10615b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 10625b2fc499SJeff Garzik break; 10635b2fc499SJeff Garzik 10645b2fc499SJeff Garzik /* software-driven interface shutdown */ 10655b2fc499SJeff Garzik case -ECONNRESET: // async unlink 10665b2fc499SJeff Garzik case -ESHUTDOWN: // hardware gone 10675b2fc499SJeff Garzik break; 10685b2fc499SJeff Garzik 10695b2fc499SJeff Garzik // like rx, tx gets controller i/o faults during khubd delays 10705b2fc499SJeff Garzik // and so it uses the same throttling mechanism. 10715b2fc499SJeff Garzik case -EPROTO: 10725b2fc499SJeff Garzik case -ETIME: 10735b2fc499SJeff Garzik case -EILSEQ: 107469ee472fSOliver Neukum usb_mark_last_busy(dev->udev); 10755b2fc499SJeff Garzik if (!timer_pending (&dev->delay)) { 10765b2fc499SJeff Garzik mod_timer (&dev->delay, 10775b2fc499SJeff Garzik jiffies + THROTTLE_JIFFIES); 1078a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1079a475f603SJoe Perches "tx throttle %d\n", urb->status); 10805b2fc499SJeff Garzik } 10815b2fc499SJeff Garzik netif_stop_queue (dev->net); 10825b2fc499SJeff Garzik break; 10835b2fc499SJeff Garzik default: 1084a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1085a475f603SJoe Perches "tx err %d\n", entry->urb->status); 10865b2fc499SJeff Garzik break; 10875b2fc499SJeff Garzik } 10885b2fc499SJeff Garzik } 10895b2fc499SJeff Garzik 109069ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 10915b6e9bcdSMing Lei (void) defer_bh(dev, skb, &dev->txq, tx_done); 10925b2fc499SJeff Garzik } 10935b2fc499SJeff Garzik 10945b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 10955b2fc499SJeff Garzik 1096777baa47SStephen Hemminger void usbnet_tx_timeout (struct net_device *net) 10975b2fc499SJeff Garzik { 10985b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 10995b2fc499SJeff Garzik 11005b2fc499SJeff Garzik unlink_urbs (dev, &dev->txq); 11015b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 11025b2fc499SJeff Garzik 11035b2fc499SJeff Garzik // FIXME: device recovery -- reset? 11045b2fc499SJeff Garzik } 1105777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_tx_timeout); 11065b2fc499SJeff Garzik 11075b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 11085b2fc499SJeff Garzik 110925a79c41SStephen Hemminger netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, 111025a79c41SStephen Hemminger struct net_device *net) 11115b2fc499SJeff Garzik { 11125b2fc499SJeff Garzik struct usbnet *dev = netdev_priv(net); 11135b2fc499SJeff Garzik int length; 11145b2fc499SJeff Garzik struct urb *urb = NULL; 11155b2fc499SJeff Garzik struct skb_data *entry; 11165b2fc499SJeff Garzik struct driver_info *info = dev->driver_info; 11175b2fc499SJeff Garzik unsigned long flags; 111825a79c41SStephen Hemminger int retval; 11195b2fc499SJeff Garzik 112023ba0799SKonstantin Khlebnikov if (skb) 1121f9b491ecSMichael Riesch skb_tx_timestamp(skb); 1122f9b491ecSMichael Riesch 11235b2fc499SJeff Garzik // some devices want funky USB-level framing, for 11245b2fc499SJeff Garzik // win32 driver (usually) and/or hardware quirks 11255b2fc499SJeff Garzik if (info->tx_fixup) { 11265b2fc499SJeff Garzik skb = info->tx_fixup (dev, skb, GFP_ATOMIC); 11275b2fc499SJeff Garzik if (!skb) { 1128bf414b36SBjørn Mork /* packet collected; minidriver waiting for more */ 1129bf414b36SBjørn Mork if (info->flags & FLAG_MULTI_PACKET) 1130bf414b36SBjørn Mork goto not_drop; 1131a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); 11325b2fc499SJeff Garzik goto drop; 11335b2fc499SJeff Garzik } 11345b2fc499SJeff Garzik } 11355b2fc499SJeff Garzik length = skb->len; 11365b2fc499SJeff Garzik 11375b2fc499SJeff Garzik if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { 1138a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "no urb\n"); 11395b2fc499SJeff Garzik goto drop; 11405b2fc499SJeff Garzik } 11415b2fc499SJeff Garzik 11425b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 11435b2fc499SJeff Garzik entry->urb = urb; 11445b2fc499SJeff Garzik entry->dev = dev; 11455b2fc499SJeff Garzik entry->length = length; 11465b2fc499SJeff Garzik 11475b2fc499SJeff Garzik usb_fill_bulk_urb (urb, dev->udev, dev->out, 11485b2fc499SJeff Garzik skb->data, skb->len, tx_complete, skb); 11495b2fc499SJeff Garzik 11505b2fc499SJeff Garzik /* don't assume the hardware handles USB_ZERO_PACKET 11515b2fc499SJeff Garzik * NOTE: strictly conforming cdc-ether devices should expect 11525b2fc499SJeff Garzik * the ZLP here, but ignore the one-byte packet. 1153073285fdSAlexey Orishko * NOTE2: CDC NCM specification is different from CDC ECM when 1154073285fdSAlexey Orishko * handling ZLP/short packets, so cdc_ncm driver will make short 1155073285fdSAlexey Orishko * packet itself if needed. 11565b2fc499SJeff Garzik */ 1157b4d562e3SElina Pasheva if (length % dev->maxpacket == 0) { 1158b4d562e3SElina Pasheva if (!(info->flags & FLAG_SEND_ZLP)) { 1159073285fdSAlexey Orishko if (!(info->flags & FLAG_MULTI_PACKET)) { 11605b2fc499SJeff Garzik urb->transfer_buffer_length++; 11613e323f3eSPeter Korsgaard if (skb_tailroom(skb)) { 11623e323f3eSPeter Korsgaard skb->data[skb->len] = 0; 11633e323f3eSPeter Korsgaard __skb_put(skb, 1); 11643e323f3eSPeter Korsgaard } 1165073285fdSAlexey Orishko } 1166b4d562e3SElina Pasheva } else 1167b4d562e3SElina Pasheva urb->transfer_flags |= URB_ZERO_PACKET; 11683e323f3eSPeter Korsgaard } 11695b2fc499SJeff Garzik 11705b2fc499SJeff Garzik spin_lock_irqsave(&dev->txq.lock, flags); 117169ee472fSOliver Neukum retval = usb_autopm_get_interface_async(dev->intf); 117269ee472fSOliver Neukum if (retval < 0) { 117369ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 117469ee472fSOliver Neukum goto drop; 117569ee472fSOliver Neukum } 117669ee472fSOliver Neukum 117769ee472fSOliver Neukum #ifdef CONFIG_PM 117869ee472fSOliver Neukum /* if this triggers the device is still a sleep */ 117969ee472fSOliver Neukum if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { 118069ee472fSOliver Neukum /* transmission will be done in resume */ 118169ee472fSOliver Neukum usb_anchor_urb(urb, &dev->deferred); 118269ee472fSOliver Neukum /* no use to process more packets */ 118369ee472fSOliver Neukum netif_stop_queue(net); 118439707c2aSHemant Kumar usb_put_urb(urb); 118569ee472fSOliver Neukum spin_unlock_irqrestore(&dev->txq.lock, flags); 118660b86755SJoe Perches netdev_dbg(dev->net, "Delaying transmission for resumption\n"); 118769ee472fSOliver Neukum goto deferred; 118869ee472fSOliver Neukum } 118969ee472fSOliver Neukum #endif 11905b2fc499SJeff Garzik 11915b2fc499SJeff Garzik switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { 11925b2fc499SJeff Garzik case -EPIPE: 11935b2fc499SJeff Garzik netif_stop_queue (net); 11945b2fc499SJeff Garzik usbnet_defer_kevent (dev, EVENT_TX_HALT); 119569ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 11965b2fc499SJeff Garzik break; 11975b2fc499SJeff Garzik default: 119869ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 1199a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, 1200a475f603SJoe Perches "tx: submit urb err %d\n", retval); 12015b2fc499SJeff Garzik break; 12025b2fc499SJeff Garzik case 0: 12035b2fc499SJeff Garzik net->trans_start = jiffies; 12045b6e9bcdSMing Lei __usbnet_queue_skb(&dev->txq, skb, tx_start); 12055b2fc499SJeff Garzik if (dev->txq.qlen >= TX_QLEN (dev)) 12065b2fc499SJeff Garzik netif_stop_queue (net); 12075b2fc499SJeff Garzik } 12085b2fc499SJeff Garzik spin_unlock_irqrestore (&dev->txq.lock, flags); 12095b2fc499SJeff Garzik 12105b2fc499SJeff Garzik if (retval) { 1211a475f603SJoe Perches netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); 12125b2fc499SJeff Garzik drop: 12137963837fSHerbert Xu dev->net->stats.tx_dropped++; 1214073285fdSAlexey Orishko not_drop: 12155b2fc499SJeff Garzik if (skb) 12165b2fc499SJeff Garzik dev_kfree_skb_any (skb); 12175b2fc499SJeff Garzik usb_free_urb (urb); 1218a475f603SJoe Perches } else 1219a475f603SJoe Perches netif_dbg(dev, tx_queued, dev->net, 1220a475f603SJoe Perches "> tx, len %d, type 0x%x\n", length, skb->protocol); 122169ee472fSOliver Neukum #ifdef CONFIG_PM 122269ee472fSOliver Neukum deferred: 122369ee472fSOliver Neukum #endif 122425a79c41SStephen Hemminger return NETDEV_TX_OK; 12255b2fc499SJeff Garzik } 1226777baa47SStephen Hemminger EXPORT_SYMBOL_GPL(usbnet_start_xmit); 12275b2fc499SJeff Garzik 122885e87870SBjørn Mork static int rx_alloc_submit(struct usbnet *dev, gfp_t flags) 122965841fd5SMing Lei { 123065841fd5SMing Lei struct urb *urb; 123165841fd5SMing Lei int i; 123285e87870SBjørn Mork int ret = 0; 123365841fd5SMing Lei 123465841fd5SMing Lei /* don't refill the queue all at once */ 123565841fd5SMing Lei for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) { 123665841fd5SMing Lei urb = usb_alloc_urb(0, flags); 123765841fd5SMing Lei if (urb != NULL) { 123885e87870SBjørn Mork ret = rx_submit(dev, urb, flags); 123985e87870SBjørn Mork if (ret) 124085e87870SBjørn Mork goto err; 124185e87870SBjørn Mork } else { 124285e87870SBjørn Mork ret = -ENOMEM; 124385e87870SBjørn Mork goto err; 124465841fd5SMing Lei } 124565841fd5SMing Lei } 124685e87870SBjørn Mork err: 124785e87870SBjørn Mork return ret; 124865841fd5SMing Lei } 124965841fd5SMing Lei 12505b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 12515b2fc499SJeff Garzik 12525b2fc499SJeff Garzik // tasklet (work deferred from completions, in_irq) or timer 12535b2fc499SJeff Garzik 12545b2fc499SJeff Garzik static void usbnet_bh (unsigned long param) 12555b2fc499SJeff Garzik { 12565b2fc499SJeff Garzik struct usbnet *dev = (struct usbnet *) param; 12575b2fc499SJeff Garzik struct sk_buff *skb; 12585b2fc499SJeff Garzik struct skb_data *entry; 12595b2fc499SJeff Garzik 12605b2fc499SJeff Garzik while ((skb = skb_dequeue (&dev->done))) { 12615b2fc499SJeff Garzik entry = (struct skb_data *) skb->cb; 12625b2fc499SJeff Garzik switch (entry->state) { 12635b2fc499SJeff Garzik case rx_done: 12645b2fc499SJeff Garzik entry->state = rx_cleanup; 12655b2fc499SJeff Garzik rx_process (dev, skb); 12665b2fc499SJeff Garzik continue; 12675b2fc499SJeff Garzik case tx_done: 12685b2fc499SJeff Garzik case rx_cleanup: 12695b2fc499SJeff Garzik usb_free_urb (entry->urb); 12705b2fc499SJeff Garzik dev_kfree_skb (skb); 12715b2fc499SJeff Garzik continue; 12725b2fc499SJeff Garzik default: 127360b86755SJoe Perches netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); 12745b2fc499SJeff Garzik } 12755b2fc499SJeff Garzik } 12765b2fc499SJeff Garzik 127770c37bf9SBjørn Mork /* restart RX again after disabling due to high error rate */ 127870c37bf9SBjørn Mork clear_bit(EVENT_RX_KILL, &dev->flags); 127970c37bf9SBjørn Mork 12805b2fc499SJeff Garzik // waiting for all pending urbs to complete? 12815b2fc499SJeff Garzik if (dev->wait) { 12825b2fc499SJeff Garzik if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { 12835b2fc499SJeff Garzik wake_up (dev->wait); 12845b2fc499SJeff Garzik } 12855b2fc499SJeff Garzik 12865b2fc499SJeff Garzik // or are we maybe short a few urbs? 12878e95a202SJoe Perches } else if (netif_running (dev->net) && 12888e95a202SJoe Perches netif_device_present (dev->net) && 12898e95a202SJoe Perches !timer_pending (&dev->delay) && 12908e95a202SJoe Perches !test_bit (EVENT_RX_HALT, &dev->flags)) { 12915b2fc499SJeff Garzik int temp = dev->rxq.qlen; 12925b2fc499SJeff Garzik 129365841fd5SMing Lei if (temp < RX_QLEN(dev)) { 129485e87870SBjørn Mork if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK) 129585e87870SBjørn Mork return; 1296a475f603SJoe Perches if (temp != dev->rxq.qlen) 1297a475f603SJoe Perches netif_dbg(dev, link, dev->net, 1298a475f603SJoe Perches "rxqlen %d --> %d\n", 12995b2fc499SJeff Garzik temp, dev->rxq.qlen); 130065841fd5SMing Lei if (dev->rxq.qlen < RX_QLEN(dev)) 13015b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 13025b2fc499SJeff Garzik } 13035b2fc499SJeff Garzik if (dev->txq.qlen < TX_QLEN (dev)) 13045b2fc499SJeff Garzik netif_wake_queue (dev->net); 13055b2fc499SJeff Garzik } 13065b2fc499SJeff Garzik } 13075b2fc499SJeff Garzik 13085b2fc499SJeff Garzik 13095b2fc499SJeff Garzik /*------------------------------------------------------------------------- 13105b2fc499SJeff Garzik * 13115b2fc499SJeff Garzik * USB Device Driver support 13125b2fc499SJeff Garzik * 13135b2fc499SJeff Garzik *-------------------------------------------------------------------------*/ 13145b2fc499SJeff Garzik 13155b2fc499SJeff Garzik // precondition: never called in_interrupt 13165b2fc499SJeff Garzik 13175b2fc499SJeff Garzik void usbnet_disconnect (struct usb_interface *intf) 13185b2fc499SJeff Garzik { 13195b2fc499SJeff Garzik struct usbnet *dev; 13205b2fc499SJeff Garzik struct usb_device *xdev; 13215b2fc499SJeff Garzik struct net_device *net; 13225b2fc499SJeff Garzik 13235b2fc499SJeff Garzik dev = usb_get_intfdata(intf); 13245b2fc499SJeff Garzik usb_set_intfdata(intf, NULL); 13255b2fc499SJeff Garzik if (!dev) 13265b2fc499SJeff Garzik return; 13275b2fc499SJeff Garzik 13285b2fc499SJeff Garzik xdev = interface_to_usbdev (intf); 13295b2fc499SJeff Garzik 1330a475f603SJoe Perches netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", 13315b2fc499SJeff Garzik intf->dev.driver->name, 13325b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 13335b2fc499SJeff Garzik dev->driver_info->description); 13345b2fc499SJeff Garzik 13355b2fc499SJeff Garzik net = dev->net; 13365b2fc499SJeff Garzik unregister_netdev (net); 13375b2fc499SJeff Garzik 133823f333a2STejun Heo cancel_work_sync(&dev->kevent); 13395b2fc499SJeff Garzik 134039707c2aSHemant Kumar usb_scuttle_anchored_urbs(&dev->deferred); 134139707c2aSHemant Kumar 13425b2fc499SJeff Garzik if (dev->driver_info->unbind) 13435b2fc499SJeff Garzik dev->driver_info->unbind (dev, intf); 13445b2fc499SJeff Garzik 134568972efaSPaul Stewart usb_kill_urb(dev->interrupt); 134668972efaSPaul Stewart usb_free_urb(dev->interrupt); 134768972efaSPaul Stewart 13485b2fc499SJeff Garzik free_netdev(net); 13495b2fc499SJeff Garzik } 13505b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_disconnect); 13515b2fc499SJeff Garzik 1352777baa47SStephen Hemminger static const struct net_device_ops usbnet_netdev_ops = { 1353777baa47SStephen Hemminger .ndo_open = usbnet_open, 1354777baa47SStephen Hemminger .ndo_stop = usbnet_stop, 1355777baa47SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 1356777baa47SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 1357777baa47SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu, 1358777baa47SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 1359777baa47SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 1360777baa47SStephen Hemminger }; 13615b2fc499SJeff Garzik 13625b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 13635b2fc499SJeff Garzik 13645b2fc499SJeff Garzik // precondition: never called in_interrupt 13655b2fc499SJeff Garzik 1366225794f8SMarcel Holtmann static struct device_type wlan_type = { 1367225794f8SMarcel Holtmann .name = "wlan", 1368225794f8SMarcel Holtmann }; 1369225794f8SMarcel Holtmann 1370225794f8SMarcel Holtmann static struct device_type wwan_type = { 1371225794f8SMarcel Holtmann .name = "wwan", 1372225794f8SMarcel Holtmann }; 1373225794f8SMarcel Holtmann 13745b2fc499SJeff Garzik int 13755b2fc499SJeff Garzik usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) 13765b2fc499SJeff Garzik { 13775b2fc499SJeff Garzik struct usbnet *dev; 13785b2fc499SJeff Garzik struct net_device *net; 13795b2fc499SJeff Garzik struct usb_host_interface *interface; 13805b2fc499SJeff Garzik struct driver_info *info; 13815b2fc499SJeff Garzik struct usb_device *xdev; 13825b2fc499SJeff Garzik int status; 13835b2fc499SJeff Garzik const char *name; 1384b0786b43SMing Lei struct usb_driver *driver = to_usb_driver(udev->dev.driver); 1385b0786b43SMing Lei 1386b0786b43SMing Lei /* usbnet already took usb runtime pm, so have to enable the feature 1387b0786b43SMing Lei * for usb interface, otherwise usb_autopm_get_interface may return 1388b0786b43SMing Lei * failure if USB_SUSPEND(RUNTIME_PM) is enabled. 1389b0786b43SMing Lei */ 1390b0786b43SMing Lei if (!driver->supports_autosuspend) { 1391b0786b43SMing Lei driver->supports_autosuspend = 1; 1392b0786b43SMing Lei pm_runtime_enable(&udev->dev); 1393b0786b43SMing Lei } 13945b2fc499SJeff Garzik 13955b2fc499SJeff Garzik name = udev->dev.driver->name; 13965b2fc499SJeff Garzik info = (struct driver_info *) prod->driver_info; 13975b2fc499SJeff Garzik if (!info) { 13985b2fc499SJeff Garzik dev_dbg (&udev->dev, "blacklisted by %s\n", name); 13995b2fc499SJeff Garzik return -ENODEV; 14005b2fc499SJeff Garzik } 14015b2fc499SJeff Garzik xdev = interface_to_usbdev (udev); 14025b2fc499SJeff Garzik interface = udev->cur_altsetting; 14035b2fc499SJeff Garzik 14045b2fc499SJeff Garzik status = -ENOMEM; 14055b2fc499SJeff Garzik 14065b2fc499SJeff Garzik // set up our own records 14075b2fc499SJeff Garzik net = alloc_etherdev(sizeof(*dev)); 140841de8d4cSJoe Perches if (!net) 14095b2fc499SJeff Garzik goto out; 14105b2fc499SJeff Garzik 14110dacca73SBen Hutchings /* netdev_printk() needs this so do it as early as possible */ 14120dacca73SBen Hutchings SET_NETDEV_DEV(net, &udev->dev); 14130dacca73SBen Hutchings 14145b2fc499SJeff Garzik dev = netdev_priv(net); 14155b2fc499SJeff Garzik dev->udev = xdev; 1416a11a6544SOliver Neukum dev->intf = udev; 14175b2fc499SJeff Garzik dev->driver_info = info; 14185b2fc499SJeff Garzik dev->driver_name = name; 14195b2fc499SJeff Garzik dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV 14205b2fc499SJeff Garzik | NETIF_MSG_PROBE | NETIF_MSG_LINK); 14215b2fc499SJeff Garzik skb_queue_head_init (&dev->rxq); 14225b2fc499SJeff Garzik skb_queue_head_init (&dev->txq); 14235b2fc499SJeff Garzik skb_queue_head_init (&dev->done); 14247834ddbcSJussi Kivilinna skb_queue_head_init(&dev->rxq_pause); 14255b2fc499SJeff Garzik dev->bh.func = usbnet_bh; 14265b2fc499SJeff Garzik dev->bh.data = (unsigned long) dev; 14275b2fc499SJeff Garzik INIT_WORK (&dev->kevent, kevent); 142869ee472fSOliver Neukum init_usb_anchor(&dev->deferred); 14295b2fc499SJeff Garzik dev->delay.function = usbnet_bh; 14305b2fc499SJeff Garzik dev->delay.data = (unsigned long) dev; 14315b2fc499SJeff Garzik init_timer (&dev->delay); 14325b2fc499SJeff Garzik mutex_init (&dev->phy_mutex); 14335b2fc499SJeff Garzik 14345b2fc499SJeff Garzik dev->net = net; 14355b2fc499SJeff Garzik strcpy (net->name, "usb%d"); 14365b2fc499SJeff Garzik memcpy (net->dev_addr, node_id, sizeof node_id); 14375b2fc499SJeff Garzik 14385b2fc499SJeff Garzik /* rx and tx sides can use different message sizes; 14395b2fc499SJeff Garzik * bind() should set rx_urb_size in that case. 14405b2fc499SJeff Garzik */ 14415b2fc499SJeff Garzik dev->hard_mtu = net->mtu + net->hard_header_len; 14425b2fc499SJeff Garzik #if 0 14435b2fc499SJeff Garzik // dma_supported() is deeply broken on almost all architectures 14445b2fc499SJeff Garzik // possible with some EHCI controllers 14456a35528aSYang Hongyang if (dma_supported (&udev->dev, DMA_BIT_MASK(64))) 14465b2fc499SJeff Garzik net->features |= NETIF_F_HIGHDMA; 14475b2fc499SJeff Garzik #endif 14485b2fc499SJeff Garzik 1449777baa47SStephen Hemminger net->netdev_ops = &usbnet_netdev_ops; 1450777baa47SStephen Hemminger net->watchdog_timeo = TX_TIMEOUT_JIFFIES; 14515b2fc499SJeff Garzik net->ethtool_ops = &usbnet_ethtool_ops; 14525b2fc499SJeff Garzik 14535b2fc499SJeff Garzik // allow device-specific bind/init procedures 14545b2fc499SJeff Garzik // NOTE net->name still not usable ... 14555b2fc499SJeff Garzik if (info->bind) { 14565b2fc499SJeff Garzik status = info->bind (dev, udev); 14575b2fc499SJeff Garzik if (status < 0) 14585b2fc499SJeff Garzik goto out1; 14595b2fc499SJeff Garzik 14605b2fc499SJeff Garzik // heuristic: "usb%d" for links we know are two-host, 14615b2fc499SJeff Garzik // else "eth%d" when there's reasonable doubt. userspace 14625b2fc499SJeff Garzik // can rename the link if it knows better. 14638e95a202SJoe Perches if ((dev->driver_info->flags & FLAG_ETHER) != 0 && 1464c261344dSArnd Bergmann ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 || 1465c261344dSArnd Bergmann (net->dev_addr [0] & 0x02) == 0)) 14665b2fc499SJeff Garzik strcpy (net->name, "eth%d"); 14676e3bbcc5SJussi Kivilinna /* WLAN devices should always be named "wlan%d" */ 14686e3bbcc5SJussi Kivilinna if ((dev->driver_info->flags & FLAG_WLAN) != 0) 14696e3bbcc5SJussi Kivilinna strcpy(net->name, "wlan%d"); 1470e1e499eeSMarcel Holtmann /* WWAN devices should always be named "wwan%d" */ 1471e1e499eeSMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1472e1e499eeSMarcel Holtmann strcpy(net->name, "wwan%d"); 14735b2fc499SJeff Garzik 14746509141fSWei Shuai /* devices that cannot do ARP */ 14756509141fSWei Shuai if ((dev->driver_info->flags & FLAG_NOARP) != 0) 14766509141fSWei Shuai net->flags |= IFF_NOARP; 14776509141fSWei Shuai 14785b2fc499SJeff Garzik /* maybe the remote can't receive an Ethernet MTU */ 14795b2fc499SJeff Garzik if (net->mtu > (dev->hard_mtu - net->hard_header_len)) 14805b2fc499SJeff Garzik net->mtu = dev->hard_mtu - net->hard_header_len; 14815b2fc499SJeff Garzik } else if (!info->in || !info->out) 14825b2fc499SJeff Garzik status = usbnet_get_endpoints (dev, udev); 14835b2fc499SJeff Garzik else { 14845b2fc499SJeff Garzik dev->in = usb_rcvbulkpipe (xdev, info->in); 14855b2fc499SJeff Garzik dev->out = usb_sndbulkpipe (xdev, info->out); 14865b2fc499SJeff Garzik if (!(info->flags & FLAG_NO_SETINT)) 14875b2fc499SJeff Garzik status = usb_set_interface (xdev, 14885b2fc499SJeff Garzik interface->desc.bInterfaceNumber, 14895b2fc499SJeff Garzik interface->desc.bAlternateSetting); 14905b2fc499SJeff Garzik else 14915b2fc499SJeff Garzik status = 0; 14925b2fc499SJeff Garzik 14935b2fc499SJeff Garzik } 14949514bfe5SPeter Korsgaard if (status >= 0 && dev->status) 14955b2fc499SJeff Garzik status = init_status (dev, udev); 14965b2fc499SJeff Garzik if (status < 0) 14975b2fc499SJeff Garzik goto out3; 14985b2fc499SJeff Garzik 14995b2fc499SJeff Garzik if (!dev->rx_urb_size) 15005b2fc499SJeff Garzik dev->rx_urb_size = dev->hard_mtu; 15015b2fc499SJeff Garzik dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); 15025b2fc499SJeff Garzik 1503225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WLAN) != 0) 1504225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wlan_type); 1505225794f8SMarcel Holtmann if ((dev->driver_info->flags & FLAG_WWAN) != 0) 1506225794f8SMarcel Holtmann SET_NETDEV_DEVTYPE(net, &wwan_type); 1507225794f8SMarcel Holtmann 15085b2fc499SJeff Garzik status = register_netdev (net); 15095b2fc499SJeff Garzik if (status) 1510a4723848Stom.leiming@gmail.com goto out4; 1511a475f603SJoe Perches netif_info(dev, probe, dev->net, 1512a475f603SJoe Perches "register '%s' at usb-%s-%s, %s, %pM\n", 15135b2fc499SJeff Garzik udev->dev.driver->name, 15145b2fc499SJeff Garzik xdev->bus->bus_name, xdev->devpath, 15155b2fc499SJeff Garzik dev->driver_info->description, 1516e174961cSJohannes Berg net->dev_addr); 15175b2fc499SJeff Garzik 15185b2fc499SJeff Garzik // ok, it's ready to go. 15195b2fc499SJeff Garzik usb_set_intfdata (udev, dev); 15205b2fc499SJeff Garzik 15215b2fc499SJeff Garzik netif_device_attach (net); 15225b2fc499SJeff Garzik 152337e8273cSBen Hutchings if (dev->driver_info->flags & FLAG_LINK_INTR) 1524*0162c554SMing Lei usbnet_link_change(dev, 0, 0); 152537e8273cSBen Hutchings 15265b2fc499SJeff Garzik return 0; 15275b2fc499SJeff Garzik 1528a4723848Stom.leiming@gmail.com out4: 1529a4723848Stom.leiming@gmail.com usb_free_urb(dev->interrupt); 15305b2fc499SJeff Garzik out3: 15315b2fc499SJeff Garzik if (info->unbind) 15325b2fc499SJeff Garzik info->unbind (dev, udev); 15335b2fc499SJeff Garzik out1: 15345b2fc499SJeff Garzik free_netdev(net); 15355b2fc499SJeff Garzik out: 15365b2fc499SJeff Garzik return status; 15375b2fc499SJeff Garzik } 15385b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_probe); 15395b2fc499SJeff Garzik 15405b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 15415b2fc499SJeff Garzik 154236433127SOliver Neukum /* 154336433127SOliver Neukum * suspend the whole driver as soon as the first interface is suspended 154436433127SOliver Neukum * resume only when the last interface is resumed 15455b2fc499SJeff Garzik */ 15465b2fc499SJeff Garzik 15475b2fc499SJeff Garzik int usbnet_suspend (struct usb_interface *intf, pm_message_t message) 15485b2fc499SJeff Garzik { 15495b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 15505b2fc499SJeff Garzik 155136433127SOliver Neukum if (!dev->suspend_count++) { 155269ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 155369ee472fSOliver Neukum /* don't autosuspend while transmitting */ 15545b1b0b81SAlan Stern if (dev->txq.qlen && PMSG_IS_AUTO(message)) { 15555eeb3132SMing Lei dev->suspend_count--; 155669ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 155769ee472fSOliver Neukum return -EBUSY; 155869ee472fSOliver Neukum } else { 155969ee472fSOliver Neukum set_bit(EVENT_DEV_ASLEEP, &dev->flags); 156069ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 156169ee472fSOliver Neukum } 1562a11a6544SOliver Neukum /* 1563a11a6544SOliver Neukum * accelerate emptying of the rx and queues, to avoid 15645b2fc499SJeff Garzik * having everything error out. 15655b2fc499SJeff Garzik */ 15665b2fc499SJeff Garzik netif_device_detach (dev->net); 156769ee472fSOliver Neukum usbnet_terminate_urbs(dev); 156869ee472fSOliver Neukum usb_kill_urb(dev->interrupt); 156969ee472fSOliver Neukum 1570a11a6544SOliver Neukum /* 1571a11a6544SOliver Neukum * reattach so runtime management can use and 1572a11a6544SOliver Neukum * wake the device 1573a11a6544SOliver Neukum */ 1574a11a6544SOliver Neukum netif_device_attach (dev->net); 157536433127SOliver Neukum } 15765b2fc499SJeff Garzik return 0; 15775b2fc499SJeff Garzik } 15785b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_suspend); 15795b2fc499SJeff Garzik 15805b2fc499SJeff Garzik int usbnet_resume (struct usb_interface *intf) 15815b2fc499SJeff Garzik { 15825b2fc499SJeff Garzik struct usbnet *dev = usb_get_intfdata(intf); 158369ee472fSOliver Neukum struct sk_buff *skb; 158469ee472fSOliver Neukum struct urb *res; 158569ee472fSOliver Neukum int retval; 15865b2fc499SJeff Garzik 158769ee472fSOliver Neukum if (!--dev->suspend_count) { 158868972efaSPaul Stewart /* resume interrupt URBs */ 158968972efaSPaul Stewart if (dev->interrupt && test_bit(EVENT_DEV_OPEN, &dev->flags)) 159068972efaSPaul Stewart usb_submit_urb(dev->interrupt, GFP_NOIO); 159168972efaSPaul Stewart 159269ee472fSOliver Neukum spin_lock_irq(&dev->txq.lock); 159369ee472fSOliver Neukum while ((res = usb_get_from_anchor(&dev->deferred))) { 159469ee472fSOliver Neukum 159569ee472fSOliver Neukum skb = (struct sk_buff *)res->context; 159669ee472fSOliver Neukum retval = usb_submit_urb(res, GFP_ATOMIC); 159769ee472fSOliver Neukum if (retval < 0) { 159869ee472fSOliver Neukum dev_kfree_skb_any(skb); 159969ee472fSOliver Neukum usb_free_urb(res); 160069ee472fSOliver Neukum usb_autopm_put_interface_async(dev->intf); 160169ee472fSOliver Neukum } else { 160269ee472fSOliver Neukum dev->net->trans_start = jiffies; 160369ee472fSOliver Neukum __skb_queue_tail(&dev->txq, skb); 160469ee472fSOliver Neukum } 160569ee472fSOliver Neukum } 160669ee472fSOliver Neukum 160769ee472fSOliver Neukum smp_mb(); 160869ee472fSOliver Neukum clear_bit(EVENT_DEV_ASLEEP, &dev->flags); 160969ee472fSOliver Neukum spin_unlock_irq(&dev->txq.lock); 161075bd0cbdSMing Lei 161175bd0cbdSMing Lei if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { 161265841fd5SMing Lei /* handle remote wakeup ASAP */ 161365841fd5SMing Lei if (!dev->wait && 161465841fd5SMing Lei netif_device_present(dev->net) && 161565841fd5SMing Lei !timer_pending(&dev->delay) && 161665841fd5SMing Lei !test_bit(EVENT_RX_HALT, &dev->flags)) 1617ab6f148dSOliver Neukum rx_alloc_submit(dev, GFP_NOIO); 161865841fd5SMing Lei 161969ee472fSOliver Neukum if (!(dev->txq.qlen >= TX_QLEN(dev))) 16201aa9bc5bSAlexey Orishko netif_tx_wake_all_queues(dev->net); 16215b2fc499SJeff Garzik tasklet_schedule (&dev->bh); 162269ee472fSOliver Neukum } 162375bd0cbdSMing Lei } 16245d9d01a3SOliver Neukum 16255d9d01a3SOliver Neukum if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) 16265d9d01a3SOliver Neukum usb_autopm_get_interface_no_resume(intf); 16275d9d01a3SOliver Neukum 16285b2fc499SJeff Garzik return 0; 16295b2fc499SJeff Garzik } 16305b2fc499SJeff Garzik EXPORT_SYMBOL_GPL(usbnet_resume); 16315b2fc499SJeff Garzik 16325d9d01a3SOliver Neukum /* 16335d9d01a3SOliver Neukum * Either a subdriver implements manage_power, then it is assumed to always 16345d9d01a3SOliver Neukum * be ready to be suspended or it reports the readiness to be suspended 16355d9d01a3SOliver Neukum * explicitly 16365d9d01a3SOliver Neukum */ 16375d9d01a3SOliver Neukum void usbnet_device_suggests_idle(struct usbnet *dev) 16385d9d01a3SOliver Neukum { 16395d9d01a3SOliver Neukum if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) { 16405d9d01a3SOliver Neukum dev->intf->needs_remote_wakeup = 1; 16415d9d01a3SOliver Neukum usb_autopm_put_interface_async(dev->intf); 16425d9d01a3SOliver Neukum } 16435d9d01a3SOliver Neukum } 16445d9d01a3SOliver Neukum EXPORT_SYMBOL(usbnet_device_suggests_idle); 16455b2fc499SJeff Garzik 16462dd7c8cfSOliver Neukum /* 16472dd7c8cfSOliver Neukum * For devices that can do without special commands 16482dd7c8cfSOliver Neukum */ 16492dd7c8cfSOliver Neukum int usbnet_manage_power(struct usbnet *dev, int on) 16502dd7c8cfSOliver Neukum { 16512dd7c8cfSOliver Neukum dev->intf->needs_remote_wakeup = on; 16522dd7c8cfSOliver Neukum return 0; 16532dd7c8cfSOliver Neukum } 16542dd7c8cfSOliver Neukum EXPORT_SYMBOL(usbnet_manage_power); 16552dd7c8cfSOliver Neukum 1656ac64995dSMing Lei void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset) 1657ac64995dSMing Lei { 1658ac64995dSMing Lei /* update link after link is reseted */ 1659ac64995dSMing Lei if (link && !need_reset) 1660ac64995dSMing Lei netif_carrier_on(dev->net); 1661ac64995dSMing Lei else 1662ac64995dSMing Lei netif_carrier_off(dev->net); 1663ac64995dSMing Lei 1664ac64995dSMing Lei if (need_reset && link) 1665ac64995dSMing Lei usbnet_defer_kevent(dev, EVENT_LINK_RESET); 1666ac64995dSMing Lei } 1667ac64995dSMing Lei EXPORT_SYMBOL(usbnet_link_change); 1668ac64995dSMing Lei 16695b2fc499SJeff Garzik /*-------------------------------------------------------------------------*/ 16700547fad2SMing Lei static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 1671877bd862SMing Lei u16 value, u16 index, void *data, u16 size) 1672877bd862SMing Lei { 1673877bd862SMing Lei void *buf = NULL; 1674877bd862SMing Lei int err = -ENOMEM; 1675877bd862SMing Lei 1676877bd862SMing Lei netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x" 1677877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1678877bd862SMing Lei cmd, reqtype, value, index, size); 1679877bd862SMing Lei 1680877bd862SMing Lei if (data) { 1681877bd862SMing Lei buf = kmalloc(size, GFP_KERNEL); 1682877bd862SMing Lei if (!buf) 1683877bd862SMing Lei goto out; 1684877bd862SMing Lei } 1685877bd862SMing Lei 1686877bd862SMing Lei err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 1687877bd862SMing Lei cmd, reqtype, value, index, buf, size, 1688877bd862SMing Lei USB_CTRL_GET_TIMEOUT); 1689877bd862SMing Lei if (err > 0 && err <= size) 1690877bd862SMing Lei memcpy(data, buf, err); 1691877bd862SMing Lei kfree(buf); 1692877bd862SMing Lei out: 1693877bd862SMing Lei return err; 1694877bd862SMing Lei } 1695877bd862SMing Lei 16960547fad2SMing Lei static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 16970547fad2SMing Lei u16 value, u16 index, const void *data, 16980547fad2SMing Lei u16 size) 1699877bd862SMing Lei { 1700877bd862SMing Lei void *buf = NULL; 1701877bd862SMing Lei int err = -ENOMEM; 1702877bd862SMing Lei 1703877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1704877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1705877bd862SMing Lei cmd, reqtype, value, index, size); 1706877bd862SMing Lei 1707877bd862SMing Lei if (data) { 1708877bd862SMing Lei buf = kmemdup(data, size, GFP_KERNEL); 1709877bd862SMing Lei if (!buf) 1710877bd862SMing Lei goto out; 1711877bd862SMing Lei } 1712877bd862SMing Lei 1713877bd862SMing Lei err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 1714877bd862SMing Lei cmd, reqtype, value, index, buf, size, 1715877bd862SMing Lei USB_CTRL_SET_TIMEOUT); 1716877bd862SMing Lei kfree(buf); 1717877bd862SMing Lei 1718877bd862SMing Lei out: 1719877bd862SMing Lei return err; 1720877bd862SMing Lei } 17210547fad2SMing Lei 17220547fad2SMing Lei /* 17230547fad2SMing Lei * The function can't be called inside suspend/resume callback, 17240547fad2SMing Lei * otherwise deadlock will be caused. 17250547fad2SMing Lei */ 17260547fad2SMing Lei int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 17270547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 17280547fad2SMing Lei { 17296f0a0986SMing Lei int ret; 17306f0a0986SMing Lei 17316f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 17326f0a0986SMing Lei return -ENODEV; 17336f0a0986SMing Lei ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index, 17340547fad2SMing Lei data, size); 17356f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 17366f0a0986SMing Lei return ret; 17370547fad2SMing Lei } 17380547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd); 17390547fad2SMing Lei 17400547fad2SMing Lei /* 17410547fad2SMing Lei * The function can't be called inside suspend/resume callback, 17420547fad2SMing Lei * otherwise deadlock will be caused. 17430547fad2SMing Lei */ 17440547fad2SMing Lei int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype, 17450547fad2SMing Lei u16 value, u16 index, const void *data, u16 size) 17460547fad2SMing Lei { 17476f0a0986SMing Lei int ret; 17486f0a0986SMing Lei 17496f0a0986SMing Lei if (usb_autopm_get_interface(dev->intf) < 0) 17506f0a0986SMing Lei return -ENODEV; 17516f0a0986SMing Lei ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index, 17520547fad2SMing Lei data, size); 17536f0a0986SMing Lei usb_autopm_put_interface(dev->intf); 17546f0a0986SMing Lei return ret; 17550547fad2SMing Lei } 1756877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd); 1757877bd862SMing Lei 17580547fad2SMing Lei /* 17590547fad2SMing Lei * The function can be called inside suspend/resume callback safely 17600547fad2SMing Lei * and should only be called by suspend/resume callback generally. 17610547fad2SMing Lei */ 17620547fad2SMing Lei int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 17630547fad2SMing Lei u16 value, u16 index, void *data, u16 size) 17640547fad2SMing Lei { 17650547fad2SMing Lei return __usbnet_read_cmd(dev, cmd, reqtype, value, index, 17660547fad2SMing Lei data, size); 17670547fad2SMing Lei } 17680547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm); 17690547fad2SMing Lei 17700547fad2SMing Lei /* 17710547fad2SMing Lei * The function can be called inside suspend/resume callback safely 17720547fad2SMing Lei * and should only be called by suspend/resume callback generally. 17730547fad2SMing Lei */ 17740547fad2SMing Lei int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype, 17750547fad2SMing Lei u16 value, u16 index, const void *data, 17760547fad2SMing Lei u16 size) 17770547fad2SMing Lei { 17780547fad2SMing Lei return __usbnet_write_cmd(dev, cmd, reqtype, value, index, 17790547fad2SMing Lei data, size); 17800547fad2SMing Lei } 17810547fad2SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm); 17820547fad2SMing Lei 1783877bd862SMing Lei static void usbnet_async_cmd_cb(struct urb *urb) 1784877bd862SMing Lei { 1785877bd862SMing Lei struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context; 1786877bd862SMing Lei int status = urb->status; 1787877bd862SMing Lei 1788877bd862SMing Lei if (status < 0) 1789877bd862SMing Lei dev_dbg(&urb->dev->dev, "%s failed with %d", 1790877bd862SMing Lei __func__, status); 1791877bd862SMing Lei 1792877bd862SMing Lei kfree(req); 1793877bd862SMing Lei usb_free_urb(urb); 1794877bd862SMing Lei } 1795877bd862SMing Lei 17960547fad2SMing Lei /* 17970547fad2SMing Lei * The caller must make sure that device can't be put into suspend 17980547fad2SMing Lei * state until the control URB completes. 17990547fad2SMing Lei */ 1800877bd862SMing Lei int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype, 1801877bd862SMing Lei u16 value, u16 index, const void *data, u16 size) 1802877bd862SMing Lei { 1803877bd862SMing Lei struct usb_ctrlrequest *req = NULL; 1804877bd862SMing Lei struct urb *urb; 1805877bd862SMing Lei int err = -ENOMEM; 1806877bd862SMing Lei void *buf = NULL; 1807877bd862SMing Lei 1808877bd862SMing Lei netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x" 1809877bd862SMing Lei " value=0x%04x index=0x%04x size=%d\n", 1810877bd862SMing Lei cmd, reqtype, value, index, size); 1811877bd862SMing Lei 1812877bd862SMing Lei urb = usb_alloc_urb(0, GFP_ATOMIC); 1813877bd862SMing Lei if (!urb) { 1814877bd862SMing Lei netdev_err(dev->net, "Error allocating URB in" 1815877bd862SMing Lei " %s!\n", __func__); 1816877bd862SMing Lei goto fail; 1817877bd862SMing Lei } 1818877bd862SMing Lei 1819877bd862SMing Lei if (data) { 1820877bd862SMing Lei buf = kmemdup(data, size, GFP_ATOMIC); 1821877bd862SMing Lei if (!buf) { 1822877bd862SMing Lei netdev_err(dev->net, "Error allocating buffer" 1823877bd862SMing Lei " in %s!\n", __func__); 1824877bd862SMing Lei goto fail_free; 1825877bd862SMing Lei } 1826877bd862SMing Lei } 1827877bd862SMing Lei 1828877bd862SMing Lei req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC); 182938673c82SJoe Perches if (!req) 1830877bd862SMing Lei goto fail_free_buf; 1831877bd862SMing Lei 1832877bd862SMing Lei req->bRequestType = reqtype; 1833877bd862SMing Lei req->bRequest = cmd; 1834877bd862SMing Lei req->wValue = cpu_to_le16(value); 1835877bd862SMing Lei req->wIndex = cpu_to_le16(index); 1836877bd862SMing Lei req->wLength = cpu_to_le16(size); 1837877bd862SMing Lei 1838877bd862SMing Lei usb_fill_control_urb(urb, dev->udev, 1839877bd862SMing Lei usb_sndctrlpipe(dev->udev, 0), 1840877bd862SMing Lei (void *)req, buf, size, 1841877bd862SMing Lei usbnet_async_cmd_cb, req); 1842877bd862SMing Lei urb->transfer_flags |= URB_FREE_BUFFER; 1843877bd862SMing Lei 1844877bd862SMing Lei err = usb_submit_urb(urb, GFP_ATOMIC); 1845877bd862SMing Lei if (err < 0) { 1846877bd862SMing Lei netdev_err(dev->net, "Error submitting the control" 1847877bd862SMing Lei " message: status=%d\n", err); 1848877bd862SMing Lei goto fail_free; 1849877bd862SMing Lei } 1850877bd862SMing Lei return 0; 1851877bd862SMing Lei 1852877bd862SMing Lei fail_free_buf: 1853877bd862SMing Lei kfree(buf); 1854877bd862SMing Lei fail_free: 1855877bd862SMing Lei kfree(req); 1856877bd862SMing Lei usb_free_urb(urb); 1857877bd862SMing Lei fail: 1858877bd862SMing Lei return err; 1859877bd862SMing Lei 1860877bd862SMing Lei } 1861877bd862SMing Lei EXPORT_SYMBOL_GPL(usbnet_write_cmd_async); 1862877bd862SMing Lei /*-------------------------------------------------------------------------*/ 18635b2fc499SJeff Garzik 18645b2fc499SJeff Garzik static int __init usbnet_init(void) 18655b2fc499SJeff Garzik { 1866c582a950SThiago Farina /* Compiler should optimize this out. */ 1867c582a950SThiago Farina BUILD_BUG_ON( 1868c582a950SThiago Farina FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data)); 18695b2fc499SJeff Garzik 1870c7e12eadSJoe Perches eth_random_addr(node_id); 18715b2fc499SJeff Garzik return 0; 18725b2fc499SJeff Garzik } 18735b2fc499SJeff Garzik module_init(usbnet_init); 18745b2fc499SJeff Garzik 18755b2fc499SJeff Garzik static void __exit usbnet_exit(void) 18765b2fc499SJeff Garzik { 18775b2fc499SJeff Garzik } 18785b2fc499SJeff Garzik module_exit(usbnet_exit); 18795b2fc499SJeff Garzik 18805b2fc499SJeff Garzik MODULE_AUTHOR("David Brownell"); 18815b2fc499SJeff Garzik MODULE_DESCRIPTION("USB network driver framework"); 18825b2fc499SJeff Garzik MODULE_LICENSE("GPL"); 1883