| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 2 | * USB Network driver infrastructure |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 3 | * Copyright (C) 2000-2005 by David Brownell |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * This is a generic "USB networking" framework that works with several |
| David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 23 | * kinds of full and high speed networking devices: host-to-host cables, |
| 24 | * smart usb peripherals, and actual Ethernet adapters. |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * |
| David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 26 | * These devices usually differ in terms of control protocols (if they |
| 27 | * even have one!) and sometimes they define new framing to wrap or batch |
| 28 | * Ethernet packets. Otherwise, they talk to USB pretty much the same, |
| 29 | * so interface (un)binding, endpoint I/O queues, fault handling, and other |
| 30 | * issues can usefully be addressed by this framework. |
| 31 | */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | // #define DEBUG // error path messages, extra info |
| 34 | // #define VERBOSE // more; success messages |
| 35 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/module.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/init.h> |
| Hemant Kumar | 6d2c747 | 2012-03-06 23:39:29 -0800 | [diff] [blame] | 38 | #include <linux/if_arp.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/netdevice.h> |
| 40 | #include <linux/etherdevice.h> |
| Peter Holik | 03ad032 | 2009-04-18 07:24:17 +0000 | [diff] [blame] | 41 | #include <linux/ctype.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/ethtool.h> |
| 43 | #include <linux/workqueue.h> |
| 44 | #include <linux/mii.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/usb.h> |
| Jussi Kivilinna | 3692e94 | 2008-01-26 00:51:45 +0200 | [diff] [blame] | 46 | #include <linux/usb/usbnet.h> |
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 47 | #include <linux/slab.h> |
| Andy Shevchenko | fd1f170 | 2010-07-23 03:18:08 +0000 | [diff] [blame] | 48 | #include <linux/kernel.h> |
| Ming Lei | b0786b4 | 2010-11-01 07:11:54 -0700 | [diff] [blame] | 49 | #include <linux/pm_runtime.h> |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 50 | |
| 51 | #define DRIVER_VERSION "22-Aug-2005" |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /*-------------------------------------------------------------------------*/ |
| 55 | |
| 56 | /* |
| 57 | * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. |
| 58 | * Several dozen bytes of IPv4 data can fit in two such transactions. |
| 59 | * One maximum size Ethernet packet takes twenty four of them. |
| 60 | * For high speed, each frame comfortably fits almost 36 max size |
| 61 | * Ethernet packets (so queues should be bigger). |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 62 | * |
| 63 | * REVISIT qlens should be members of 'struct usbnet'; the goal is to |
| 64 | * let the USB host controller be busy for 5msec or more before an irq |
| 65 | * is required, under load. Jumbograms change the equation. |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
| Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 67 | #define RX_MAX_QUEUE_MEMORY (60 * 1518) |
| 68 | #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \ |
| 69 | (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4) |
| 70 | #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \ |
| 71 | (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | // reawaken network queue this soon after stopping; else watchdog barks |
| 74 | #define TX_TIMEOUT_JIFFIES (5*HZ) |
| 75 | |
| 76 | // throttle rx/tx briefly after some faults, so khubd might disconnect() |
| 77 | // us (it polls at HZ/4 usually) before we report too many false errors. |
| 78 | #define THROTTLE_JIFFIES (HZ/8) |
| 79 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | // between wakeups |
| 81 | #define UNLINK_TIMEOUT_MS 3 |
| 82 | |
| 83 | /*-------------------------------------------------------------------------*/ |
| 84 | |
| 85 | // randomly generated ethernet address |
| 86 | static u8 node_id [ETH_ALEN]; |
| 87 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | static const char driver_name [] = "usbnet"; |
| 89 | |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 90 | static struct workqueue_struct *usbnet_wq; |
| 91 | |
| Hemant Kumar | ef59a42 | 2012-08-15 16:57:00 -0700 | [diff] [blame] | 92 | static DECLARE_WAIT_QUEUE_HEAD(unlink_wakeup); |
| 93 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* use ethtool to change the level for any given device */ |
| 95 | static int msg_level = -1; |
| 96 | module_param (msg_level, int, 0); |
| 97 | MODULE_PARM_DESC (msg_level, "Override default message level"); |
| 98 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /*-------------------------------------------------------------------------*/ |
| 100 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* handles CDC Ethernet and many other network "bulk data" interfaces */ |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 102 | int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | int tmp; |
| 105 | struct usb_host_interface *alt = NULL; |
| 106 | struct usb_host_endpoint *in = NULL, *out = NULL; |
| 107 | struct usb_host_endpoint *status = NULL; |
| 108 | |
| 109 | for (tmp = 0; tmp < intf->num_altsetting; tmp++) { |
| 110 | unsigned ep; |
| 111 | |
| 112 | in = out = status = NULL; |
| 113 | alt = intf->altsetting + tmp; |
| 114 | |
| 115 | /* take the first altsetting with in-bulk + out-bulk; |
| 116 | * remember any status endpoint, just in case; |
| Justin P. Mattock | 70f23fd | 2011-05-10 10:16:21 +0200 | [diff] [blame] | 117 | * ignore other endpoints and altsettings. |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | */ |
| 119 | for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { |
| 120 | struct usb_host_endpoint *e; |
| 121 | int intr = 0; |
| 122 | |
| 123 | e = alt->endpoint + ep; |
| 124 | switch (e->desc.bmAttributes) { |
| 125 | case USB_ENDPOINT_XFER_INT: |
| Luiz Fernando N. Capitulino | fc6e254 | 2006-10-26 13:03:01 -0300 | [diff] [blame] | 126 | if (!usb_endpoint_dir_in(&e->desc)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | continue; |
| 128 | intr = 1; |
| 129 | /* FALLTHROUGH */ |
| 130 | case USB_ENDPOINT_XFER_BULK: |
| 131 | break; |
| 132 | default: |
| 133 | continue; |
| 134 | } |
| Luiz Fernando N. Capitulino | fc6e254 | 2006-10-26 13:03:01 -0300 | [diff] [blame] | 135 | if (usb_endpoint_dir_in(&e->desc)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (!intr && !in) |
| 137 | in = e; |
| 138 | else if (intr && !status) |
| 139 | status = e; |
| 140 | } else { |
| 141 | if (!out) |
| 142 | out = e; |
| 143 | } |
| 144 | } |
| 145 | if (in && out) |
| 146 | break; |
| 147 | } |
| 148 | if (!alt || !in || !out) |
| 149 | return -EINVAL; |
| 150 | |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 151 | if (alt->desc.bAlternateSetting != 0 || |
| 152 | !(dev->driver_info->flags & FLAG_NO_SETINT)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, |
| 154 | alt->desc.bAlternateSetting); |
| 155 | if (tmp < 0) |
| 156 | return tmp; |
| 157 | } |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 158 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | dev->in = usb_rcvbulkpipe (dev->udev, |
| 160 | in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 161 | dev->out = usb_sndbulkpipe (dev->udev, |
| 162 | out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
| 163 | dev->status = status; |
| 164 | return 0; |
| 165 | } |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 166 | EXPORT_SYMBOL_GPL(usbnet_get_endpoints); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| Peter Holik | 03ad032 | 2009-04-18 07:24:17 +0000 | [diff] [blame] | 168 | int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress) |
| 169 | { |
| 170 | int tmp, i; |
| 171 | unsigned char buf [13]; |
| 172 | |
| 173 | tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf); |
| 174 | if (tmp != 12) { |
| 175 | dev_dbg(&dev->udev->dev, |
| 176 | "bad MAC string %d fetch, %d\n", iMACAddress, tmp); |
| 177 | if (tmp >= 0) |
| 178 | tmp = -EINVAL; |
| 179 | return tmp; |
| 180 | } |
| 181 | for (i = tmp = 0; i < 6; i++, tmp += 2) |
| 182 | dev->net->dev_addr [i] = |
| Andy Shevchenko | fd1f170 | 2010-07-23 03:18:08 +0000 | [diff] [blame] | 183 | (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]); |
| Peter Holik | 03ad032 | 2009-04-18 07:24:17 +0000 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr); |
| 187 | |
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 188 | static void intr_complete (struct urb *urb); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | static int init_status (struct usbnet *dev, struct usb_interface *intf) |
| 191 | { |
| 192 | char *buf = NULL; |
| 193 | unsigned pipe = 0; |
| 194 | unsigned maxp; |
| 195 | unsigned period; |
| 196 | |
| 197 | if (!dev->driver_info->status) |
| 198 | return 0; |
| 199 | |
| 200 | pipe = usb_rcvintpipe (dev->udev, |
| 201 | dev->status->desc.bEndpointAddress |
| 202 | & USB_ENDPOINT_NUMBER_MASK); |
| 203 | maxp = usb_maxpacket (dev->udev, pipe, 0); |
| 204 | |
| 205 | /* avoid 1 msec chatter: min 8 msec poll rate */ |
| 206 | period = max ((int) dev->status->desc.bInterval, |
| 207 | (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); |
| 208 | |
| Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 209 | buf = kmalloc (maxp, GFP_KERNEL); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (buf) { |
| Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 211 | dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (!dev->interrupt) { |
| 213 | kfree (buf); |
| 214 | return -ENOMEM; |
| 215 | } else { |
| 216 | usb_fill_int_urb(dev->interrupt, dev->udev, pipe, |
| 217 | buf, maxp, intr_complete, dev, period); |
| tom.leiming@gmail.com | 720f3d7 | 2012-04-29 22:51:02 +0000 | [diff] [blame] | 218 | dev->interrupt->transfer_flags |= URB_FREE_BUFFER; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | dev_dbg(&intf->dev, |
| 220 | "status ep%din, %d bytes period %d\n", |
| 221 | usb_pipeendpoint(pipe), maxp, period); |
| 222 | } |
| 223 | } |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 224 | return 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 227 | /* Passes this packet up the stack, updating its accounting. |
| 228 | * Some link protocols batch packets, so their rx_fixup paths |
| 229 | * can return clones as well as just modify the original skb. |
| 230 | */ |
| 231 | void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | int status; |
| 234 | |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 235 | if (test_bit(EVENT_RX_PAUSED, &dev->flags)) { |
| 236 | skb_queue_tail(&dev->rxq_pause, skb); |
| 237 | return; |
| 238 | } |
| 239 | |
| Hemant Kumar | 37c35e4 | 2011-09-14 23:44:19 -0700 | [diff] [blame] | 240 | if (!skb->protocol) |
| 241 | skb->protocol = eth_type_trans(skb, dev->net); |
| 242 | |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 243 | dev->net->stats.rx_packets++; |
| 244 | dev->net->stats.rx_bytes += skb->len; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 246 | netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n", |
| 247 | skb->len + sizeof (struct ethhdr), skb->protocol); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | memset (skb->cb, 0, sizeof (struct skb_data)); |
| Michael Riesch | f9b491e | 2011-09-29 04:06:26 +0000 | [diff] [blame] | 249 | |
| 250 | if (skb_defer_rx_timestamp(skb)) |
| 251 | return; |
| 252 | |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 253 | status = netif_rx_ni(skb); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 254 | if (status != NET_RX_SUCCESS) |
| 255 | netif_dbg(dev, rx_err, dev->net, |
| 256 | "netif_rx status %d\n", status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 258 | EXPORT_SYMBOL_GPL(usbnet_skb_return); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | /*------------------------------------------------------------------------- |
| 262 | * |
| 263 | * Network Device Driver (peer link to "Host Device", from USB host) |
| 264 | * |
| 265 | *-------------------------------------------------------------------------*/ |
| 266 | |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 267 | int usbnet_change_mtu (struct net_device *net, int new_mtu) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
| 269 | struct usbnet *dev = netdev_priv(net); |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 270 | int ll_mtu = new_mtu + net->hard_header_len; |
| Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 271 | int old_hard_mtu = dev->hard_mtu; |
| 272 | int old_rx_urb_size = dev->rx_urb_size; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 274 | if (new_mtu <= 0) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return -EINVAL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | // no second zero-length packet read wanted after mtu-sized packets |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 277 | if ((ll_mtu % dev->maxpacket) == 0) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return -EDOM; |
| 279 | net->mtu = new_mtu; |
| Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 280 | |
| 281 | dev->hard_mtu = net->mtu + net->hard_header_len; |
| 282 | if (dev->rx_urb_size == old_hard_mtu) { |
| 283 | dev->rx_urb_size = dev->hard_mtu; |
| 284 | if (dev->rx_urb_size > old_rx_urb_size) |
| 285 | usbnet_unlink_rx_urbs(dev); |
| 286 | } |
| 287 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 290 | EXPORT_SYMBOL_GPL(usbnet_change_mtu); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 292 | /* The caller must hold list->lock */ |
| 293 | static void __usbnet_queue_skb(struct sk_buff_head *list, |
| 294 | struct sk_buff *newsk, enum skb_state state) |
| 295 | { |
| 296 | struct skb_data *entry = (struct skb_data *) newsk->cb; |
| 297 | |
| 298 | __skb_queue_tail(list, newsk); |
| 299 | entry->state = state; |
| 300 | } |
| 301 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | /*-------------------------------------------------------------------------*/ |
| 303 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from |
| 305 | * completion callbacks. 2.5 should have fixed those bugs... |
| 306 | */ |
| 307 | |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 308 | static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb, |
| 309 | struct sk_buff_head *list, enum skb_state state) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | unsigned long flags; |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 312 | enum skb_state old_state; |
| 313 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 315 | spin_lock_irqsave(&list->lock, flags); |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 316 | old_state = entry->state; |
| 317 | entry->state = state; |
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 318 | __skb_unlink(skb, list); |
| 319 | spin_unlock(&list->lock); |
| 320 | spin_lock(&dev->done.lock); |
| 321 | __skb_queue_tail(&dev->done, skb); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (dev->done.qlen == 1) |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 323 | queue_work(usbnet_wq, &dev->bh_w); |
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 324 | spin_unlock_irqrestore(&dev->done.lock, flags); |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 325 | return old_state; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* some work can't be done in tasklets, so we use keventd |
| 329 | * |
| 330 | * NOTE: annoying asymmetry: if it's active, schedule_work() fails, |
| 331 | * but tasklet_schedule() doesn't. hope the failure is rare. |
| 332 | */ |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 333 | void usbnet_defer_kevent (struct usbnet *dev, int work) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | set_bit (work, &dev->flags); |
| Steve Glendinning | 5ae1424 | 2012-11-08 06:26:21 +0000 | [diff] [blame] | 336 | if (!schedule_work (&dev->kevent)) { |
| 337 | if (net_ratelimit()) |
| 338 | netdev_err(dev->net, "kevent %d may have been dropped\n", work); |
| 339 | } else { |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 340 | netdev_dbg(dev->net, "kevent %d scheduled\n", work); |
| Steve Glendinning | 5ae1424 | 2012-11-08 06:26:21 +0000 | [diff] [blame] | 341 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(usbnet_defer_kevent); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | /*-------------------------------------------------------------------------*/ |
| 346 | |
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 347 | static void rx_complete (struct urb *urb); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 349 | static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
| 351 | struct sk_buff *skb; |
| 352 | struct skb_data *entry; |
| 353 | int retval = 0; |
| 354 | unsigned long lockflags; |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 355 | size_t size = dev->rx_urb_size; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| Eric Dumazet | 7bdd402 | 2012-03-14 06:56:25 +0000 | [diff] [blame] | 357 | skb = __netdev_alloc_skb_ip_align(dev->net, size, flags); |
| 358 | if (!skb) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 359 | netif_dbg(dev, rx_err, dev->net, "no rx skb\n"); |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 360 | usbnet_defer_kevent (dev, EVENT_RX_MEMORY); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | usb_free_urb (urb); |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 362 | return -ENOMEM; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| Hemant Kumar | 6d2c747 | 2012-03-06 23:39:29 -0800 | [diff] [blame] | 365 | if (dev->net->type != ARPHRD_RAWIP) |
| 366 | skb_reserve(skb, NET_IP_ALIGN); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | entry = (struct skb_data *) skb->cb; |
| 369 | entry->urb = urb; |
| 370 | entry->dev = dev; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | entry->length = 0; |
| 372 | |
| 373 | usb_fill_bulk_urb (urb, dev->udev, dev->in, |
| 374 | skb->data, size, rx_complete, skb); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | spin_lock_irqsave (&dev->rxq.lock, lockflags); |
| 377 | |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 378 | if (netif_running (dev->net) && |
| 379 | netif_device_present (dev->net) && |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 380 | !test_bit (EVENT_RX_HALT, &dev->flags) && |
| 381 | !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) { |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 382 | switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | case -EPIPE: |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 384 | usbnet_defer_kevent (dev, EVENT_RX_HALT); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | break; |
| 386 | case -ENOMEM: |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 387 | usbnet_defer_kevent (dev, EVENT_RX_MEMORY); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | break; |
| 389 | case -ENODEV: |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 390 | netif_dbg(dev, ifdown, dev->net, "device gone\n"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | netif_device_detach (dev->net); |
| 392 | break; |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 393 | case -EHOSTUNREACH: |
| 394 | retval = -ENOLINK; |
| 395 | break; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | default: |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 397 | netif_dbg(dev, rx_err, dev->net, |
| 398 | "rx submit, %d\n", retval); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 399 | queue_work(usbnet_wq, &dev->bh_w); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | break; |
| 401 | case 0: |
| Hemant Kumar | 279188a | 2012-04-03 13:32:48 -0700 | [diff] [blame] | 402 | usb_mark_last_busy(dev->udev); |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 403 | __usbnet_queue_skb(&dev->rxq, skb, rx_start); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | } else { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 406 | netif_dbg(dev, ifdown, dev->net, "rx: stopped\n"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | retval = -ENOLINK; |
| 408 | } |
| 409 | spin_unlock_irqrestore (&dev->rxq.lock, lockflags); |
| 410 | if (retval) { |
| 411 | dev_kfree_skb_any (skb); |
| 412 | usb_free_urb (urb); |
| 413 | } |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 414 | return retval; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | /*-------------------------------------------------------------------------*/ |
| 419 | |
| 420 | static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) |
| 421 | { |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 422 | if (dev->driver_info->rx_fixup && |
| Andrzej Zaborowski | 7a635ea | 2011-03-28 12:56:33 +0000 | [diff] [blame] | 423 | !dev->driver_info->rx_fixup (dev, skb)) { |
| 424 | /* With RX_ASSEMBLE, rx_fixup() must update counters */ |
| 425 | if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE)) |
| 426 | dev->net->stats.rx_errors++; |
| 427 | goto done; |
| 428 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | // else network stack removes extra byte if we forced a short packet |
| 430 | |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 431 | if (skb->len) { |
| 432 | /* all data was already cloned from skb inside the driver */ |
| 433 | if (dev->driver_info->flags & FLAG_MULTI_PACKET) |
| 434 | dev_kfree_skb_any(skb); |
| 435 | else |
| 436 | usbnet_skb_return(dev, skb); |
| 437 | return; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 439 | |
| 440 | netif_dbg(dev, rx_err, dev->net, "drop\n"); |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 441 | dev->net->stats.rx_errors++; |
| Andrzej Zaborowski | 7a635ea | 2011-03-28 12:56:33 +0000 | [diff] [blame] | 442 | done: |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 443 | skb_queue_tail(&dev->done, skb); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /*-------------------------------------------------------------------------*/ |
| 447 | |
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 448 | static void rx_complete (struct urb *urb) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 451 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 452 | struct usbnet *dev = entry->dev; |
| 453 | int urb_status = urb->status; |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 454 | enum skb_state state; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | skb_put (skb, urb->actual_length); |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 457 | state = rx_done; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | entry->urb = NULL; |
| 459 | |
| 460 | switch (urb_status) { |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 461 | /* success */ |
| 462 | case 0: |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 463 | if (skb->len < dev->net->hard_header_len) { |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 464 | state = rx_cleanup; |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 465 | dev->net->stats.rx_errors++; |
| 466 | dev->net->stats.rx_length_errors++; |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 467 | netif_dbg(dev, rx_err, dev->net, |
| 468 | "rx length %d\n", skb->len); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | break; |
| 471 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 472 | /* stalls need manual reset. this is rare ... except that |
| 473 | * when going through USB 2.0 TTs, unplug appears this way. |
| Jean Delvare | 3ac49a1 | 2009-06-04 16:20:28 +0200 | [diff] [blame] | 474 | * we avoid the highspeed version of the ETIMEDOUT/EILSEQ |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 475 | * storm, recovering as needed. |
| 476 | */ |
| 477 | case -EPIPE: |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 478 | dev->net->stats.rx_errors++; |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 479 | usbnet_defer_kevent (dev, EVENT_RX_HALT); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | // FALLTHROUGH |
| 481 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 482 | /* software-driven interface shutdown */ |
| 483 | case -ECONNRESET: /* async unlink */ |
| 484 | case -ESHUTDOWN: /* hardware gone */ |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 485 | netif_dbg(dev, ifdown, dev->net, |
| 486 | "rx shutdown, code %d\n", urb_status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | goto block; |
| 488 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 489 | /* we get controller i/o faults during khubd disconnect() delays. |
| 490 | * throttle down resubmits, to avoid log floods; just temporarily, |
| 491 | * so we still recover when the fault isn't a khubd delay. |
| 492 | */ |
| 493 | case -EPROTO: |
| 494 | case -ETIME: |
| 495 | case -EILSEQ: |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 496 | dev->net->stats.rx_errors++; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (!timer_pending (&dev->delay)) { |
| 498 | mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 499 | netif_dbg(dev, link, dev->net, |
| 500 | "rx throttle %d\n", urb_status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | block: |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 503 | state = rx_cleanup; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | entry->urb = urb; |
| 505 | urb = NULL; |
| 506 | break; |
| 507 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 508 | /* data overrun ... flush fifo? */ |
| 509 | case -EOVERFLOW: |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 510 | dev->net->stats.rx_over_errors++; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | // FALLTHROUGH |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 512 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 513 | default: |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 514 | state = rx_cleanup; |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 515 | dev->net->stats.rx_errors++; |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 516 | netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 520 | state = defer_bh(dev, skb, &dev->rxq, state); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
| 522 | if (urb) { |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 523 | if (netif_running (dev->net) && |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 524 | !test_bit (EVENT_RX_HALT, &dev->flags) && |
| 525 | state != unlink_start) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | rx_submit (dev, urb, GFP_ATOMIC); |
| Oliver Neukum | 8a78335 | 2012-03-03 18:45:07 +0100 | [diff] [blame] | 527 | usb_mark_last_busy(dev->udev); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return; |
| 529 | } |
| 530 | usb_free_urb (urb); |
| 531 | } |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 532 | netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 535 | static void intr_complete (struct urb *urb) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
| 537 | struct usbnet *dev = urb->context; |
| 538 | int status = urb->status; |
| 539 | |
| 540 | switch (status) { |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 541 | /* success */ |
| 542 | case 0: |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | dev->driver_info->status(dev, urb); |
| 544 | break; |
| 545 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 546 | /* software-driven interface shutdown */ |
| 547 | case -ENOENT: /* urb killed */ |
| 548 | case -ESHUTDOWN: /* hardware gone */ |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 549 | netif_dbg(dev, ifdown, dev->net, |
| 550 | "intr shutdown, code %d\n", status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return; |
| 552 | |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 553 | /* NOTE: not throttling like RX/TX, since this endpoint |
| 554 | * already polls infrequently |
| 555 | */ |
| 556 | default: |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 557 | netdev_dbg(dev->net, "intr status %d\n", status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | |
| 561 | if (!netif_running (dev->net)) |
| 562 | return; |
| 563 | |
| 564 | memset(urb->transfer_buffer, 0, urb->transfer_buffer_length); |
| 565 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 566 | if (status != 0) |
| 567 | netif_err(dev, timer, dev->net, |
| 568 | "intr resubmit --> %d\n", status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /*-------------------------------------------------------------------------*/ |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 572 | void usbnet_pause_rx(struct usbnet *dev) |
| 573 | { |
| 574 | set_bit(EVENT_RX_PAUSED, &dev->flags); |
| 575 | |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 576 | netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n"); |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 577 | } |
| 578 | EXPORT_SYMBOL_GPL(usbnet_pause_rx); |
| 579 | |
| 580 | void usbnet_resume_rx(struct usbnet *dev) |
| 581 | { |
| 582 | struct sk_buff *skb; |
| 583 | int num = 0; |
| 584 | |
| 585 | clear_bit(EVENT_RX_PAUSED, &dev->flags); |
| 586 | |
| 587 | while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) { |
| 588 | usbnet_skb_return(dev, skb); |
| 589 | num++; |
| 590 | } |
| 591 | |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 592 | queue_work(usbnet_wq, &dev->bh_w); |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 593 | |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 594 | netif_dbg(dev, rx_status, dev->net, |
| 595 | "paused rx queue disabled, %d skbs requeued\n", num); |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 596 | } |
| 597 | EXPORT_SYMBOL_GPL(usbnet_resume_rx); |
| 598 | |
| 599 | void usbnet_purge_paused_rxq(struct usbnet *dev) |
| 600 | { |
| 601 | skb_queue_purge(&dev->rxq_pause); |
| 602 | } |
| 603 | EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq); |
| 604 | |
| 605 | /*-------------------------------------------------------------------------*/ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
| 607 | // unlink pending rx/tx; completion handlers do all other cleanup |
| 608 | |
| 609 | static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) |
| 610 | { |
| 611 | unsigned long flags; |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 612 | struct sk_buff *skb; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | int count = 0; |
| 614 | |
| 615 | spin_lock_irqsave (&q->lock, flags); |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 616 | while (!skb_queue_empty(q)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | struct skb_data *entry; |
| 618 | struct urb *urb; |
| 619 | int retval; |
| 620 | |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 621 | skb_queue_walk(q, skb) { |
| 622 | entry = (struct skb_data *) skb->cb; |
| 623 | if (entry->state != unlink_start) |
| 624 | goto found; |
| 625 | } |
| 626 | break; |
| 627 | found: |
| 628 | entry->state = unlink_start; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | urb = entry->urb; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
| tom.leiming@gmail.com | 0956a8c | 2012-03-22 03:22:18 +0000 | [diff] [blame] | 631 | /* |
| 632 | * Get reference count of the URB to avoid it to be |
| 633 | * freed during usb_unlink_urb, which may trigger |
| 634 | * use-after-free problem inside usb_unlink_urb since |
| 635 | * usb_unlink_urb is always racing with .complete |
| 636 | * handler(include defer_bh). |
| 637 | */ |
| 638 | usb_get_urb(urb); |
| Sebastian Siewior | 4231d47 | 2012-03-07 10:19:28 +0000 | [diff] [blame] | 639 | spin_unlock_irqrestore(&q->lock, flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | // during some PM-driven resume scenarios, |
| 641 | // these (async) unlinks complete immediately |
| 642 | retval = usb_unlink_urb (urb); |
| 643 | if (retval != -EINPROGRESS && retval != 0) |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 644 | netdev_dbg(dev->net, "unlink urb err, %d\n", retval); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | else |
| 646 | count++; |
| tom.leiming@gmail.com | 0956a8c | 2012-03-22 03:22:18 +0000 | [diff] [blame] | 647 | usb_put_urb(urb); |
| Sebastian Siewior | 4231d47 | 2012-03-07 10:19:28 +0000 | [diff] [blame] | 648 | spin_lock_irqsave(&q->lock, flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | spin_unlock_irqrestore (&q->lock, flags); |
| 651 | return count; |
| 652 | } |
| 653 | |
| Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 654 | // Flush all pending rx urbs |
| 655 | // minidrivers may need to do this when the MTU changes |
| 656 | |
| 657 | void usbnet_unlink_rx_urbs(struct usbnet *dev) |
| 658 | { |
| 659 | if (netif_running(dev->net)) { |
| 660 | (void) unlink_urbs (dev, &dev->rxq); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 661 | queue_work(usbnet_wq, &dev->bh_w); |
| Jamie Painter | a99c194 | 2006-07-27 14:17:28 -0400 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
| 666 | /*-------------------------------------------------------------------------*/ |
| 667 | |
| 668 | // precondition: never called in_interrupt |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 669 | static void usbnet_terminate_urbs(struct usbnet *dev) |
| 670 | { |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 671 | DECLARE_WAITQUEUE(wait, current); |
| 672 | int temp; |
| 673 | |
| 674 | /* ensure there are no more active urbs */ |
| 675 | add_wait_queue(&unlink_wakeup, &wait); |
| 676 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 677 | dev->wait = &unlink_wakeup; |
| 678 | temp = unlink_urbs(dev, &dev->txq) + |
| 679 | unlink_urbs(dev, &dev->rxq); |
| 680 | |
| 681 | /* maybe wait for deletions to finish. */ |
| 682 | while (!skb_queue_empty(&dev->rxq) |
| 683 | && !skb_queue_empty(&dev->txq) |
| 684 | && !skb_queue_empty(&dev->done)) { |
| Kulikov Vasiliy | 66cc42a | 2010-07-25 22:25:42 +0000 | [diff] [blame] | 685 | schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS)); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 686 | set_current_state(TASK_UNINTERRUPTIBLE); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 687 | netif_dbg(dev, ifdown, dev->net, |
| 688 | "waited for %d urb completions\n", temp); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 689 | } |
| 690 | set_current_state(TASK_RUNNING); |
| 691 | dev->wait = NULL; |
| 692 | remove_wait_queue(&unlink_wakeup, &wait); |
| 693 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 695 | int usbnet_stop (struct net_device *net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
| 697 | struct usbnet *dev = netdev_priv(net); |
| Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 698 | struct driver_info *info = dev->driver_info; |
| Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 699 | int retval; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| Ming Lei | 75bd0cb | 2011-04-28 22:37:09 +0000 | [diff] [blame] | 701 | clear_bit(EVENT_DEV_OPEN, &dev->flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | netif_stop_queue (net); |
| 703 | |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 704 | netif_info(dev, ifdown, dev->net, |
| Ben Hutchings | 8ccef43 | 2010-06-08 08:20:59 +0000 | [diff] [blame] | 705 | "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n", |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 706 | net->stats.rx_packets, net->stats.tx_packets, |
| 707 | net->stats.rx_errors, net->stats.tx_errors); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
| Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 709 | /* allow minidriver to stop correctly (wireless devices to turn off |
| 710 | * radio etc) */ |
| 711 | if (info->stop) { |
| 712 | retval = info->stop(dev); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 713 | if (retval < 0) |
| 714 | netif_info(dev, ifdown, dev->net, |
| 715 | "stop fail (%d) usbnet usb-%s-%s, %s\n", |
| 716 | retval, |
| 717 | dev->udev->bus->bus_name, dev->udev->devpath, |
| 718 | info->description); |
| Jussi Kivilinna | a33e9e7 | 2009-06-16 17:17:27 +0300 | [diff] [blame] | 719 | } |
| 720 | |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 721 | if (!(info->flags & FLAG_AVOID_UNLINK_URBS)) |
| 722 | usbnet_terminate_urbs(dev); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | usb_kill_urb(dev->interrupt); |
| 725 | |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 726 | usbnet_purge_paused_rxq(dev); |
| 727 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | /* deferred work (task, timer, softirq) must also stop. |
| 729 | * can't flush_scheduled_work() until we drop rtnl (later), |
| 730 | * else workers could deadlock; so make workers a NOP. |
| 731 | */ |
| 732 | dev->flags = 0; |
| 733 | del_timer_sync (&dev->delay); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 734 | cancel_work_sync(&dev->bh_w); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 735 | if (info->manage_power) |
| 736 | info->manage_power(dev, 0); |
| 737 | else |
| 738 | usb_autopm_put_interface(dev->intf); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | return 0; |
| 741 | } |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 742 | EXPORT_SYMBOL_GPL(usbnet_stop); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | /*-------------------------------------------------------------------------*/ |
| 745 | |
| 746 | // posts reads, and enables write queuing |
| 747 | |
| 748 | // precondition: never called in_interrupt |
| 749 | |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 750 | int usbnet_open (struct net_device *net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
| 752 | struct usbnet *dev = netdev_priv(net); |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 753 | int retval; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | struct driver_info *info = dev->driver_info; |
| 755 | |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 756 | if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 757 | netif_info(dev, ifup, dev->net, |
| 758 | "resumption fail (%d) usbnet usb-%s-%s, %s\n", |
| 759 | retval, |
| 760 | dev->udev->bus->bus_name, |
| 761 | dev->udev->devpath, |
| 762 | info->description); |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 763 | goto done_nopm; |
| 764 | } |
| 765 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | // put into "known safe" state |
| 767 | if (info->reset && (retval = info->reset (dev)) < 0) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 768 | netif_info(dev, ifup, dev->net, |
| 769 | "open reset fail (%d) usbnet usb-%s-%s, %s\n", |
| 770 | retval, |
| 771 | dev->udev->bus->bus_name, |
| 772 | dev->udev->devpath, |
| 773 | info->description); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | goto done; |
| 775 | } |
| 776 | |
| 777 | // insist peer be connected |
| 778 | if (info->check_connect && (retval = info->check_connect (dev)) < 0) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 779 | netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | goto done; |
| 781 | } |
| 782 | |
| 783 | /* start any status interrupt transfer */ |
| 784 | if (dev->interrupt) { |
| 785 | retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); |
| 786 | if (retval < 0) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 787 | netif_err(dev, ifup, dev->net, |
| 788 | "intr submit %d\n", retval); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | goto done; |
| 790 | } |
| 791 | } |
| 792 | |
| Paul Stewart | 68972ef | 2011-04-28 05:43:37 +0000 | [diff] [blame] | 793 | set_bit(EVENT_DEV_OPEN, &dev->flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | netif_start_queue (net); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 795 | netif_info(dev, ifup, dev->net, |
| 796 | "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n", |
| 797 | (int)RX_QLEN(dev), (int)TX_QLEN(dev), |
| 798 | dev->net->mtu, |
| 799 | (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" : |
| 800 | (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" : |
| 801 | (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" : |
| 802 | (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" : |
| 803 | (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" : |
| 804 | "simple"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
| 806 | // delay posting reads until we're fully open |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 807 | queue_work(usbnet_wq, &dev->bh_w); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 808 | if (info->manage_power) { |
| 809 | retval = info->manage_power(dev, 1); |
| 810 | if (retval < 0) |
| 811 | goto done; |
| 812 | usb_autopm_put_interface(dev->intf); |
| 813 | } |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 814 | return retval; |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 815 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | done: |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 817 | usb_autopm_put_interface(dev->intf); |
| 818 | done_nopm: |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return retval; |
| 820 | } |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 821 | EXPORT_SYMBOL_GPL(usbnet_open); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | /*-------------------------------------------------------------------------*/ |
| 824 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 825 | /* ethtool methods; minidrivers may need to add some more, but |
| 826 | * they'll probably want to use this base set. |
| 827 | */ |
| 828 | |
| Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 829 | int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) |
| 830 | { |
| 831 | struct usbnet *dev = netdev_priv(net); |
| 832 | |
| 833 | if (!dev->mii.mdio_read) |
| 834 | return -EOPNOTSUPP; |
| 835 | |
| 836 | return mii_ethtool_gset(&dev->mii, cmd); |
| 837 | } |
| 838 | EXPORT_SYMBOL_GPL(usbnet_get_settings); |
| 839 | |
| 840 | int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) |
| 841 | { |
| 842 | struct usbnet *dev = netdev_priv(net); |
| 843 | int retval; |
| 844 | |
| 845 | if (!dev->mii.mdio_write) |
| 846 | return -EOPNOTSUPP; |
| 847 | |
| 848 | retval = mii_ethtool_sset(&dev->mii, cmd); |
| 849 | |
| 850 | /* link speed/duplex might have changed */ |
| 851 | if (dev->driver_info->link_reset) |
| 852 | dev->driver_info->link_reset(dev); |
| 853 | |
| 854 | return retval; |
| 855 | |
| 856 | } |
| 857 | EXPORT_SYMBOL_GPL(usbnet_set_settings); |
| 858 | |
| Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 859 | u32 usbnet_get_link (struct net_device *net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | struct usbnet *dev = netdev_priv(net); |
| 862 | |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 863 | /* If a check_connect is defined, return its result */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | if (dev->driver_info->check_connect) |
| 865 | return dev->driver_info->check_connect (dev) == 0; |
| 866 | |
| Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 867 | /* if the device has mii operations, use those */ |
| 868 | if (dev->mii.mdio_read) |
| 869 | return mii_link_ok(&dev->mii); |
| 870 | |
| Bjørn Mork | 05ffb3e | 2009-03-01 20:45:40 -0800 | [diff] [blame] | 871 | /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ |
| 872 | return ethtool_op_get_link(net); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 874 | EXPORT_SYMBOL_GPL(usbnet_get_link); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 876 | int usbnet_nway_reset(struct net_device *net) |
| 877 | { |
| 878 | struct usbnet *dev = netdev_priv(net); |
| 879 | |
| 880 | if (!dev->mii.mdio_write) |
| 881 | return -EOPNOTSUPP; |
| 882 | |
| 883 | return mii_nway_restart(&dev->mii); |
| 884 | } |
| 885 | EXPORT_SYMBOL_GPL(usbnet_nway_reset); |
| 886 | |
| David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 887 | void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) |
| 888 | { |
| 889 | struct usbnet *dev = netdev_priv(net); |
| 890 | |
| David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 891 | strncpy (info->driver, dev->driver_name, sizeof info->driver); |
| David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 892 | strncpy (info->version, DRIVER_VERSION, sizeof info->version); |
| 893 | strncpy (info->fw_version, dev->driver_info->description, |
| 894 | sizeof info->fw_version); |
| 895 | usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); |
| 896 | } |
| 897 | EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); |
| 898 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 899 | u32 usbnet_get_msglevel (struct net_device *net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
| 901 | struct usbnet *dev = netdev_priv(net); |
| 902 | |
| 903 | return dev->msg_enable; |
| 904 | } |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 905 | EXPORT_SYMBOL_GPL(usbnet_get_msglevel); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 907 | void usbnet_set_msglevel (struct net_device *net, u32 level) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | { |
| 909 | struct usbnet *dev = netdev_priv(net); |
| 910 | |
| 911 | dev->msg_enable = level; |
| 912 | } |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 913 | EXPORT_SYMBOL_GPL(usbnet_set_msglevel); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 915 | /* drivers may override default ethtool_ops in their bind() routine */ |
| Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 916 | static const struct ethtool_ops usbnet_ethtool_ops = { |
| Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 917 | .get_settings = usbnet_get_settings, |
| 918 | .set_settings = usbnet_set_settings, |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 919 | .get_link = usbnet_get_link, |
| Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 920 | .nway_reset = usbnet_nway_reset, |
| David Brownell | 18ee91fa | 2006-11-02 12:29:12 -0800 | [diff] [blame] | 921 | .get_drvinfo = usbnet_get_drvinfo, |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 922 | .get_msglevel = usbnet_get_msglevel, |
| 923 | .set_msglevel = usbnet_set_msglevel, |
| 924 | }; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
| 926 | /*-------------------------------------------------------------------------*/ |
| 927 | |
| 928 | /* work that cannot be done in interrupt context uses keventd. |
| 929 | * |
| 930 | * NOTE: with 2.5 we could do more of this using completion callbacks, |
| 931 | * especially now that control transfers can be queued. |
| 932 | */ |
| 933 | static void |
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 934 | kevent (struct work_struct *work) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 936 | struct usbnet *dev = |
| 937 | container_of(work, struct usbnet, kevent); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | int status; |
| 939 | |
| 940 | /* usb_clear_halt() needs a thread context */ |
| 941 | if (test_bit (EVENT_TX_HALT, &dev->flags)) { |
| 942 | unlink_urbs (dev, &dev->txq); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 943 | status = usb_autopm_get_interface(dev->intf); |
| 944 | if (status < 0) |
| 945 | goto fail_pipe; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | status = usb_clear_halt (dev->udev, dev->out); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 947 | usb_autopm_put_interface(dev->intf); |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 948 | if (status < 0 && |
| 949 | status != -EPIPE && |
| 950 | status != -ESHUTDOWN) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | if (netif_msg_tx_err (dev)) |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 952 | fail_pipe: |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 953 | netdev_err(dev->net, "can't clear tx halt, status %d\n", |
| 954 | status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } else { |
| 956 | clear_bit (EVENT_TX_HALT, &dev->flags); |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 957 | if (status != -ESHUTDOWN) |
| 958 | netif_wake_queue (dev->net); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | if (test_bit (EVENT_RX_HALT, &dev->flags)) { |
| 962 | unlink_urbs (dev, &dev->rxq); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 963 | status = usb_autopm_get_interface(dev->intf); |
| 964 | if (status < 0) |
| 965 | goto fail_halt; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | status = usb_clear_halt (dev->udev, dev->in); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 967 | usb_autopm_put_interface(dev->intf); |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 968 | if (status < 0 && |
| 969 | status != -EPIPE && |
| 970 | status != -ESHUTDOWN) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | if (netif_msg_rx_err (dev)) |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 972 | fail_halt: |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 973 | netdev_err(dev->net, "can't clear rx halt, status %d\n", |
| 974 | status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } else { |
| 976 | clear_bit (EVENT_RX_HALT, &dev->flags); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 977 | queue_work(usbnet_wq, &dev->bh_w); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
| 981 | /* tasklet could resubmit itself forever if memory is tight */ |
| 982 | if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { |
| 983 | struct urb *urb = NULL; |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 984 | int resched = 1; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | if (netif_running (dev->net)) |
| 987 | urb = usb_alloc_urb (0, GFP_KERNEL); |
| 988 | else |
| 989 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
| 990 | if (urb != NULL) { |
| 991 | clear_bit (EVENT_RX_MEMORY, &dev->flags); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 992 | status = usb_autopm_get_interface(dev->intf); |
| Jesper Juhl | ab60707 | 2011-02-10 10:58:45 +0000 | [diff] [blame] | 993 | if (status < 0) { |
| 994 | usb_free_urb(urb); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 995 | goto fail_lowmem; |
| Jesper Juhl | ab60707 | 2011-02-10 10:58:45 +0000 | [diff] [blame] | 996 | } |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 997 | if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK) |
| 998 | resched = 0; |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 999 | usb_autopm_put_interface(dev->intf); |
| 1000 | fail_lowmem: |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 1001 | if (resched) |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1002 | queue_work(usbnet_wq, &dev->bh_w); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1006 | if (test_bit (EVENT_LINK_RESET, &dev->flags)) { |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1007 | struct driver_info *info = dev->driver_info; |
| David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1008 | int retval = 0; |
| 1009 | |
| 1010 | clear_bit (EVENT_LINK_RESET, &dev->flags); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1011 | status = usb_autopm_get_interface(dev->intf); |
| 1012 | if (status < 0) |
| 1013 | goto skip_reset; |
| David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1014 | if(info->link_reset && (retval = info->link_reset(dev)) < 0) { |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1015 | usb_autopm_put_interface(dev->intf); |
| 1016 | skip_reset: |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 1017 | netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n", |
| 1018 | retval, |
| 1019 | dev->udev->bus->bus_name, |
| 1020 | dev->udev->devpath, |
| 1021 | info->description); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1022 | } else { |
| 1023 | usb_autopm_put_interface(dev->intf); |
| David Hollis | 7ea13c9 | 2005-04-22 15:07:02 -0700 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | if (dev->flags) |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 1028 | netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /*-------------------------------------------------------------------------*/ |
| 1032 | |
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1033 | static void tx_complete (struct urb *urb) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | { |
| 1035 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 1036 | struct skb_data *entry = (struct skb_data *) skb->cb; |
| 1037 | struct usbnet *dev = entry->dev; |
| 1038 | |
| 1039 | if (urb->status == 0) { |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 1040 | if (!(dev->driver_info->flags & FLAG_MULTI_PACKET)) |
| 1041 | dev->net->stats.tx_packets++; |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 1042 | dev->net->stats.tx_bytes += entry->length; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } else { |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 1044 | dev->net->stats.tx_errors++; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
| 1046 | switch (urb->status) { |
| 1047 | case -EPIPE: |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1048 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | break; |
| 1050 | |
| 1051 | /* software-driven interface shutdown */ |
| 1052 | case -ECONNRESET: // async unlink |
| 1053 | case -ESHUTDOWN: // hardware gone |
| 1054 | break; |
| 1055 | |
| 1056 | // like rx, tx gets controller i/o faults during khubd delays |
| 1057 | // and so it uses the same throttling mechanism. |
| Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 1058 | case -EPROTO: |
| 1059 | case -ETIME: |
| 1060 | case -EILSEQ: |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1061 | usb_mark_last_busy(dev->udev); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | if (!timer_pending (&dev->delay)) { |
| 1063 | mod_timer (&dev->delay, |
| 1064 | jiffies + THROTTLE_JIFFIES); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1065 | netif_dbg(dev, link, dev->net, |
| 1066 | "tx throttle %d\n", urb->status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | netif_stop_queue (dev->net); |
| 1069 | break; |
| 1070 | default: |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1071 | netif_dbg(dev, tx_err, dev->net, |
| 1072 | "tx err %d\n", entry->urb->status); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | break; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1077 | usb_autopm_put_interface_async(dev->intf); |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 1078 | (void) defer_bh(dev, skb, &dev->txq, tx_done); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /*-------------------------------------------------------------------------*/ |
| 1082 | |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1083 | void usbnet_tx_timeout (struct net_device *net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | { |
| 1085 | struct usbnet *dev = netdev_priv(net); |
| 1086 | |
| 1087 | unlink_urbs (dev, &dev->txq); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1088 | queue_work(usbnet_wq, &dev->bh_w); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
| 1090 | // FIXME: device recovery -- reset? |
| 1091 | } |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1092 | EXPORT_SYMBOL_GPL(usbnet_tx_timeout); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
| 1094 | /*-------------------------------------------------------------------------*/ |
| 1095 | |
| Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1096 | netdev_tx_t usbnet_start_xmit (struct sk_buff *skb, |
| 1097 | struct net_device *net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | { |
| 1099 | struct usbnet *dev = netdev_priv(net); |
| 1100 | int length; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | struct urb *urb = NULL; |
| 1102 | struct skb_data *entry; |
| 1103 | struct driver_info *info = dev->driver_info; |
| 1104 | unsigned long flags; |
| Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1105 | int retval; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
| Konstantin Khlebnikov | 23ba079 | 2011-11-07 05:54:58 +0000 | [diff] [blame] | 1107 | if (skb) |
| 1108 | skb_tx_timestamp(skb); |
| Michael Riesch | f9b491e | 2011-09-29 04:06:26 +0000 | [diff] [blame] | 1109 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | // some devices want funky USB-level framing, for |
| 1111 | // win32 driver (usually) and/or hardware quirks |
| 1112 | if (info->tx_fixup) { |
| 1113 | skb = info->tx_fixup (dev, skb, GFP_ATOMIC); |
| 1114 | if (!skb) { |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 1115 | if (netif_msg_tx_err(dev)) { |
| 1116 | netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n"); |
| 1117 | goto drop; |
| 1118 | } else { |
| 1119 | /* cdc_ncm collected packet; waits for more */ |
| 1120 | goto not_drop; |
| 1121 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | length = skb->len; |
| 1125 | |
| 1126 | if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1127 | netif_dbg(dev, tx_err, dev->net, "no urb\n"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | goto drop; |
| 1129 | } |
| 1130 | |
| 1131 | entry = (struct skb_data *) skb->cb; |
| 1132 | entry->urb = urb; |
| 1133 | entry->dev = dev; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | entry->length = length; |
| 1135 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | usb_fill_bulk_urb (urb, dev->udev, dev->out, |
| 1137 | skb->data, skb->len, tx_complete, skb); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | |
| 1139 | /* don't assume the hardware handles USB_ZERO_PACKET |
| 1140 | * NOTE: strictly conforming cdc-ether devices should expect |
| 1141 | * the ZLP here, but ignore the one-byte packet. |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 1142 | * NOTE2: CDC NCM specification is different from CDC ECM when |
| 1143 | * handling ZLP/short packets, so cdc_ncm driver will make short |
| 1144 | * packet itself if needed. |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | */ |
| Elina Pasheva | b4d562e | 2010-04-06 14:23:07 +0000 | [diff] [blame] | 1146 | if (length % dev->maxpacket == 0) { |
| 1147 | if (!(info->flags & FLAG_SEND_ZLP)) { |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 1148 | if (!(info->flags & FLAG_MULTI_PACKET)) { |
| 1149 | urb->transfer_buffer_length++; |
| 1150 | if (skb_tailroom(skb)) { |
| 1151 | skb->data[skb->len] = 0; |
| 1152 | __skb_put(skb, 1); |
| 1153 | } |
| Elina Pasheva | b4d562e | 2010-04-06 14:23:07 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } else |
| 1156 | urb->transfer_flags |= URB_ZERO_PACKET; |
| Peter Korsgaard | 3e323f3 | 2007-06-27 08:48:15 +0200 | [diff] [blame] | 1157 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1159 | spin_lock_irqsave(&dev->txq.lock, flags); |
| 1160 | retval = usb_autopm_get_interface_async(dev->intf); |
| 1161 | if (retval < 0) { |
| 1162 | spin_unlock_irqrestore(&dev->txq.lock, flags); |
| 1163 | goto drop; |
| 1164 | } |
| 1165 | |
| 1166 | #ifdef CONFIG_PM |
| 1167 | /* if this triggers the device is still a sleep */ |
| 1168 | if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) { |
| 1169 | /* transmission will be done in resume */ |
| 1170 | usb_anchor_urb(urb, &dev->deferred); |
| 1171 | /* no use to process more packets */ |
| 1172 | netif_stop_queue(net); |
| Hemant Kumar | fa476f7 | 2012-10-09 16:22:18 -0700 | [diff] [blame] | 1173 | usb_put_urb(urb); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1174 | spin_unlock_irqrestore(&dev->txq.lock, flags); |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 1175 | netdev_dbg(dev->net, "Delaying transmission for resumption\n"); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1176 | goto deferred; |
| 1177 | } |
| 1178 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { |
| 1181 | case -EPIPE: |
| 1182 | netif_stop_queue (net); |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1183 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1184 | usb_autopm_put_interface_async(dev->intf); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | break; |
| 1186 | default: |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1187 | usb_autopm_put_interface_async(dev->intf); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1188 | netif_dbg(dev, tx_err, dev->net, |
| 1189 | "tx: submit urb err %d\n", retval); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | break; |
| 1191 | case 0: |
| 1192 | net->trans_start = jiffies; |
| Ming Lei | 5b6e9bc | 2012-04-26 11:33:46 +0800 | [diff] [blame] | 1193 | __usbnet_queue_skb(&dev->txq, skb, tx_start); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | if (dev->txq.qlen >= TX_QLEN (dev)) |
| 1195 | netif_stop_queue (net); |
| 1196 | } |
| 1197 | spin_unlock_irqrestore (&dev->txq.lock, flags); |
| 1198 | |
| 1199 | if (retval) { |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1200 | netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | drop: |
| Herbert Xu | 7963837 | 2009-06-29 16:53:28 +0000 | [diff] [blame] | 1202 | dev->net->stats.tx_dropped++; |
| Alexey Orishko | 073285f | 2010-11-29 23:23:27 +0000 | [diff] [blame] | 1203 | not_drop: |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | if (skb) |
| 1205 | dev_kfree_skb_any (skb); |
| 1206 | usb_free_urb (urb); |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1207 | } else |
| 1208 | netif_dbg(dev, tx_queued, dev->net, |
| 1209 | "> tx, len %d, type 0x%x\n", length, skb->protocol); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1210 | #ifdef CONFIG_PM |
| 1211 | deferred: |
| 1212 | #endif |
| Stephen Hemminger | 25a79c4 | 2009-08-31 19:50:45 +0000 | [diff] [blame] | 1213 | return NETDEV_TX_OK; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1215 | EXPORT_SYMBOL_GPL(usbnet_start_xmit); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
| 1217 | /*-------------------------------------------------------------------------*/ |
| 1218 | |
| 1219 | // tasklet (work deferred from completions, in_irq) or timer |
| 1220 | |
| 1221 | static void usbnet_bh (unsigned long param) |
| 1222 | { |
| 1223 | struct usbnet *dev = (struct usbnet *) param; |
| 1224 | struct sk_buff *skb; |
| 1225 | struct skb_data *entry; |
| 1226 | |
| 1227 | while ((skb = skb_dequeue (&dev->done))) { |
| 1228 | entry = (struct skb_data *) skb->cb; |
| 1229 | switch (entry->state) { |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1230 | case rx_done: |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | entry->state = rx_cleanup; |
| 1232 | rx_process (dev, skb); |
| 1233 | continue; |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1234 | case tx_done: |
| 1235 | case rx_cleanup: |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | usb_free_urb (entry->urb); |
| 1237 | dev_kfree_skb (skb); |
| 1238 | continue; |
| David Brownell | 18ab458 | 2007-05-25 12:31:32 -0700 | [diff] [blame] | 1239 | default: |
| Joe Perches | 60b8675 | 2010-02-17 10:30:23 +0000 | [diff] [blame] | 1240 | netdev_dbg(dev->net, "bogus skb state %d\n", entry->state); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | // waiting for all pending urbs to complete? |
| 1245 | if (dev->wait) { |
| 1246 | if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { |
| Hemant Kumar | ef59a42 | 2012-08-15 16:57:00 -0700 | [diff] [blame] | 1247 | wake_up(&unlink_wakeup); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | // or are we maybe short a few urbs? |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 1251 | } else if (netif_running (dev->net) && |
| 1252 | netif_device_present (dev->net) && |
| 1253 | !timer_pending (&dev->delay) && |
| 1254 | !test_bit (EVENT_RX_HALT, &dev->flags)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | int temp = dev->rxq.qlen; |
| 1256 | int qlen = RX_QLEN (dev); |
| 1257 | |
| 1258 | if (temp < qlen) { |
| 1259 | struct urb *urb; |
| 1260 | int i; |
| 1261 | |
| 1262 | // don't refill the queue all at once |
| 1263 | for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) { |
| 1264 | urb = usb_alloc_urb (0, GFP_ATOMIC); |
| David S. Miller | dacb397 | 2010-08-10 02:50:55 -0700 | [diff] [blame] | 1265 | if (urb != NULL) { |
| 1266 | if (rx_submit (dev, urb, GFP_ATOMIC) == |
| 1267 | -ENOLINK) |
| 1268 | return; |
| 1269 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | } |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1271 | if (temp != dev->rxq.qlen) |
| 1272 | netif_dbg(dev, link, dev->net, |
| 1273 | "rxqlen %d --> %d\n", |
| 1274 | temp, dev->rxq.qlen); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | if (dev->rxq.qlen < qlen) |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1276 | queue_work(usbnet_wq, &dev->bh_w); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | } |
| 1278 | if (dev->txq.qlen < TX_QLEN (dev)) |
| 1279 | netif_wake_queue (dev->net); |
| 1280 | } |
| 1281 | } |
| 1282 | |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1283 | static void usbnet_bh_w(struct work_struct *work) |
| 1284 | { |
| 1285 | struct usbnet *dev = |
| 1286 | container_of(work, struct usbnet, bh_w); |
| 1287 | unsigned long param = (unsigned long)dev; |
| 1288 | |
| 1289 | usbnet_bh(param); |
| 1290 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | /*------------------------------------------------------------------------- |
| 1293 | * |
| 1294 | * USB Device Driver support |
| 1295 | * |
| 1296 | *-------------------------------------------------------------------------*/ |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1297 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | // precondition: never called in_interrupt |
| 1299 | |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1300 | void usbnet_disconnect (struct usb_interface *intf) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | { |
| 1302 | struct usbnet *dev; |
| 1303 | struct usb_device *xdev; |
| 1304 | struct net_device *net; |
| 1305 | |
| 1306 | dev = usb_get_intfdata(intf); |
| 1307 | usb_set_intfdata(intf, NULL); |
| 1308 | if (!dev) |
| 1309 | return; |
| 1310 | |
| 1311 | xdev = interface_to_usbdev (intf); |
| 1312 | |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1313 | netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n", |
| 1314 | intf->dev.driver->name, |
| 1315 | xdev->bus->bus_name, xdev->devpath, |
| 1316 | dev->driver_info->description); |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1317 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | net = dev->net; |
| 1319 | unregister_netdev (net); |
| 1320 | |
| Tejun Heo | 23f333a | 2010-12-12 16:45:14 +0100 | [diff] [blame] | 1321 | cancel_work_sync(&dev->kevent); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| Hemant Kumar | fa476f7 | 2012-10-09 16:22:18 -0700 | [diff] [blame] | 1323 | usb_scuttle_anchored_urbs(&dev->deferred); |
| 1324 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | if (dev->driver_info->unbind) |
| 1326 | dev->driver_info->unbind (dev, intf); |
| 1327 | |
| Paul Stewart | 68972ef | 2011-04-28 05:43:37 +0000 | [diff] [blame] | 1328 | usb_kill_urb(dev->interrupt); |
| 1329 | usb_free_urb(dev->interrupt); |
| 1330 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | free_netdev(net); |
| 1332 | usb_put_dev (xdev); |
| 1333 | } |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1334 | EXPORT_SYMBOL_GPL(usbnet_disconnect); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1336 | static const struct net_device_ops usbnet_netdev_ops = { |
| 1337 | .ndo_open = usbnet_open, |
| 1338 | .ndo_stop = usbnet_stop, |
| 1339 | .ndo_start_xmit = usbnet_start_xmit, |
| 1340 | .ndo_tx_timeout = usbnet_tx_timeout, |
| 1341 | .ndo_change_mtu = usbnet_change_mtu, |
| 1342 | .ndo_set_mac_address = eth_mac_addr, |
| 1343 | .ndo_validate_addr = eth_validate_addr, |
| 1344 | }; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | /*-------------------------------------------------------------------------*/ |
| 1347 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | // precondition: never called in_interrupt |
| 1349 | |
| Marcel Holtmann | 225794f | 2009-10-02 05:15:26 +0000 | [diff] [blame] | 1350 | static struct device_type wlan_type = { |
| 1351 | .name = "wlan", |
| 1352 | }; |
| 1353 | |
| 1354 | static struct device_type wwan_type = { |
| 1355 | .name = "wwan", |
| 1356 | }; |
| 1357 | |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1358 | int |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) |
| 1360 | { |
| 1361 | struct usbnet *dev; |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1362 | struct net_device *net; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | struct usb_host_interface *interface; |
| 1364 | struct driver_info *info; |
| 1365 | struct usb_device *xdev; |
| 1366 | int status; |
| David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1367 | const char *name; |
| Ming Lei | b0786b4 | 2010-11-01 07:11:54 -0700 | [diff] [blame] | 1368 | struct usb_driver *driver = to_usb_driver(udev->dev.driver); |
| 1369 | |
| 1370 | /* usbnet already took usb runtime pm, so have to enable the feature |
| 1371 | * for usb interface, otherwise usb_autopm_get_interface may return |
| 1372 | * failure if USB_SUSPEND(RUNTIME_PM) is enabled. |
| 1373 | */ |
| 1374 | if (!driver->supports_autosuspend) { |
| 1375 | driver->supports_autosuspend = 1; |
| 1376 | pm_runtime_enable(&udev->dev); |
| 1377 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
| David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1379 | name = udev->dev.driver->name; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | info = (struct driver_info *) prod->driver_info; |
| 1381 | if (!info) { |
| David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1382 | dev_dbg (&udev->dev, "blacklisted by %s\n", name); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | return -ENODEV; |
| 1384 | } |
| 1385 | xdev = interface_to_usbdev (udev); |
| 1386 | interface = udev->cur_altsetting; |
| 1387 | |
| 1388 | usb_get_dev (xdev); |
| 1389 | |
| 1390 | status = -ENOMEM; |
| 1391 | |
| 1392 | // set up our own records |
| 1393 | net = alloc_etherdev(sizeof(*dev)); |
| Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1394 | if (!net) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | goto out; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
| Ben Hutchings | 0dacca7 | 2010-07-02 21:49:02 -0700 | [diff] [blame] | 1397 | /* netdev_printk() needs this so do it as early as possible */ |
| 1398 | SET_NETDEV_DEV(net, &udev->dev); |
| 1399 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | dev = netdev_priv(net); |
| 1401 | dev->udev = xdev; |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1402 | dev->intf = udev; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | dev->driver_info = info; |
| David Brownell | 296c024 | 2007-04-17 16:10:10 -0700 | [diff] [blame] | 1404 | dev->driver_name = name; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV |
| 1406 | | NETIF_MSG_PROBE | NETIF_MSG_LINK); |
| 1407 | skb_queue_head_init (&dev->rxq); |
| 1408 | skb_queue_head_init (&dev->txq); |
| 1409 | skb_queue_head_init (&dev->done); |
| Jussi Kivilinna | 7834ddb | 2009-08-11 22:57:16 +0300 | [diff] [blame] | 1410 | skb_queue_head_init(&dev->rxq_pause); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1411 | INIT_WORK(&dev->bh_w, usbnet_bh_w); |
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1412 | INIT_WORK (&dev->kevent, kevent); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1413 | init_usb_anchor(&dev->deferred); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | dev->delay.function = usbnet_bh; |
| 1415 | dev->delay.data = (unsigned long) dev; |
| 1416 | init_timer (&dev->delay); |
| Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 1417 | mutex_init (&dev->phy_mutex); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | dev->net = net; |
| 1420 | strcpy (net->name, "usb%d"); |
| 1421 | memcpy (net->dev_addr, node_id, sizeof node_id); |
| 1422 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1423 | /* rx and tx sides can use different message sizes; |
| 1424 | * bind() should set rx_urb_size in that case. |
| 1425 | */ |
| 1426 | dev->hard_mtu = net->mtu + net->hard_header_len; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | #if 0 |
| 1428 | // dma_supported() is deeply broken on almost all architectures |
| 1429 | // possible with some EHCI controllers |
| Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 1430 | if (dma_supported (&udev->dev, DMA_BIT_MASK(64))) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | net->features |= NETIF_F_HIGHDMA; |
| 1432 | #endif |
| 1433 | |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1434 | net->netdev_ops = &usbnet_netdev_ops; |
| Stephen Hemminger | 777baa4 | 2009-03-20 19:35:54 +0000 | [diff] [blame] | 1435 | net->watchdog_timeo = TX_TIMEOUT_JIFFIES; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | net->ethtool_ops = &usbnet_ethtool_ops; |
| 1437 | |
| 1438 | // allow device-specific bind/init procedures |
| 1439 | // NOTE net->name still not usable ... |
| 1440 | if (info->bind) { |
| 1441 | status = info->bind (dev, udev); |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1442 | if (status < 0) |
| 1443 | goto out1; |
| 1444 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | // heuristic: "usb%d" for links we know are two-host, |
| 1446 | // else "eth%d" when there's reasonable doubt. userspace |
| 1447 | // can rename the link if it knows better. |
| Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 1448 | if ((dev->driver_info->flags & FLAG_ETHER) != 0 && |
| Arnd Bergmann | c261344 | 2011-04-01 20:12:02 -0700 | [diff] [blame] | 1449 | ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 || |
| 1450 | (net->dev_addr [0] & 0x02) == 0)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | strcpy (net->name, "eth%d"); |
| Jussi Kivilinna | 6e3bbcc | 2008-01-26 00:51:06 +0200 | [diff] [blame] | 1452 | /* WLAN devices should always be named "wlan%d" */ |
| 1453 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) |
| 1454 | strcpy(net->name, "wlan%d"); |
| Marcel Holtmann | e1e499e | 2009-10-02 05:15:25 +0000 | [diff] [blame] | 1455 | /* WWAN devices should always be named "wwan%d" */ |
| 1456 | if ((dev->driver_info->flags & FLAG_WWAN) != 0) |
| 1457 | strcpy(net->name, "wwan%d"); |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1458 | |
| 1459 | /* maybe the remote can't receive an Ethernet MTU */ |
| 1460 | if (net->mtu > (dev->hard_mtu - net->hard_header_len)) |
| 1461 | net->mtu = dev->hard_mtu - net->hard_header_len; |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1462 | } else if (!info->in || !info->out) |
| 1463 | status = usbnet_get_endpoints (dev, udev); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | else { |
| 1465 | dev->in = usb_rcvbulkpipe (xdev, info->in); |
| 1466 | dev->out = usb_sndbulkpipe (xdev, info->out); |
| 1467 | if (!(info->flags & FLAG_NO_SETINT)) |
| 1468 | status = usb_set_interface (xdev, |
| 1469 | interface->desc.bInterfaceNumber, |
| 1470 | interface->desc.bAlternateSetting); |
| 1471 | else |
| 1472 | status = 0; |
| 1473 | |
| 1474 | } |
| Peter Korsgaard | 9514bfe | 2007-07-03 00:46:42 +0200 | [diff] [blame] | 1475 | if (status >= 0 && dev->status) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | status = init_status (dev, udev); |
| 1477 | if (status < 0) |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1478 | goto out3; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
| David Brownell | 2e55cc7 | 2005-08-31 09:53:10 -0700 | [diff] [blame] | 1480 | if (!dev->rx_urb_size) |
| 1481 | dev->rx_urb_size = dev->hard_mtu; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1483 | |
| Marcel Holtmann | 225794f | 2009-10-02 05:15:26 +0000 | [diff] [blame] | 1484 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) |
| 1485 | SET_NETDEV_DEVTYPE(net, &wlan_type); |
| 1486 | if ((dev->driver_info->flags & FLAG_WWAN) != 0) |
| 1487 | SET_NETDEV_DEVTYPE(net, &wwan_type); |
| 1488 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | status = register_netdev (net); |
| 1490 | if (status) |
| tom.leiming@gmail.com | a472384 | 2012-04-29 22:51:03 +0000 | [diff] [blame] | 1491 | goto out4; |
| Joe Perches | a475f60 | 2010-02-17 10:30:24 +0000 | [diff] [blame] | 1492 | netif_info(dev, probe, dev->net, |
| 1493 | "register '%s' at usb-%s-%s, %s, %pM\n", |
| 1494 | udev->dev.driver->name, |
| 1495 | xdev->bus->bus_name, xdev->devpath, |
| 1496 | dev->driver_info->description, |
| 1497 | net->dev_addr); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | |
| 1499 | // ok, it's ready to go. |
| 1500 | usb_set_intfdata (udev, dev); |
| 1501 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | netif_device_attach (net); |
| 1503 | |
| Ben Hutchings | 37e8273 | 2009-11-04 15:29:52 +0000 | [diff] [blame] | 1504 | if (dev->driver_info->flags & FLAG_LINK_INTR) |
| 1505 | netif_carrier_off(net); |
| 1506 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | return 0; |
| 1508 | |
| tom.leiming@gmail.com | a472384 | 2012-04-29 22:51:03 +0000 | [diff] [blame] | 1509 | out4: |
| 1510 | usb_free_urb(dev->interrupt); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | out3: |
| 1512 | if (info->unbind) |
| 1513 | info->unbind (dev, udev); |
| 1514 | out1: |
| 1515 | free_netdev(net); |
| 1516 | out: |
| 1517 | usb_put_dev(xdev); |
| 1518 | return status; |
| 1519 | } |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1520 | EXPORT_SYMBOL_GPL(usbnet_probe); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | /*-------------------------------------------------------------------------*/ |
| 1523 | |
| Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1524 | /* |
| 1525 | * suspend the whole driver as soon as the first interface is suspended |
| 1526 | * resume only when the last interface is resumed |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1527 | */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1529 | int usbnet_suspend (struct usb_interface *intf, pm_message_t message) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | { |
| 1531 | struct usbnet *dev = usb_get_intfdata(intf); |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1532 | |
| Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1533 | if (!dev->suspend_count++) { |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1534 | spin_lock_irq(&dev->txq.lock); |
| 1535 | /* don't autosuspend while transmitting */ |
| Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 1536 | if (dev->txq.qlen && PMSG_IS_AUTO(message)) { |
| Ming Lei | 2038ae2 | 2012-06-19 21:15:52 +0000 | [diff] [blame] | 1537 | dev->suspend_count--; |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1538 | spin_unlock_irq(&dev->txq.lock); |
| 1539 | return -EBUSY; |
| 1540 | } else { |
| 1541 | set_bit(EVENT_DEV_ASLEEP, &dev->flags); |
| 1542 | spin_unlock_irq(&dev->txq.lock); |
| 1543 | } |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1544 | /* |
| 1545 | * accelerate emptying of the rx and queues, to avoid |
| Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1546 | * having everything error out. |
| 1547 | */ |
| 1548 | netif_device_detach (dev->net); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1549 | usbnet_terminate_urbs(dev); |
| 1550 | usb_kill_urb(dev->interrupt); |
| 1551 | |
| Oliver Neukum | a11a654 | 2007-08-03 13:52:19 +0200 | [diff] [blame] | 1552 | /* |
| 1553 | * reattach so runtime management can use and |
| 1554 | * wake the device |
| 1555 | */ |
| 1556 | netif_device_attach (dev->net); |
| Oliver Neukum | 3643312 | 2007-04-30 01:37:44 -0700 | [diff] [blame] | 1557 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1560 | EXPORT_SYMBOL_GPL(usbnet_suspend); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1562 | int usbnet_resume (struct usb_interface *intf) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
| 1564 | struct usbnet *dev = usb_get_intfdata(intf); |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1565 | struct sk_buff *skb; |
| 1566 | struct urb *res; |
| 1567 | int retval; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1569 | if (!--dev->suspend_count) { |
| Paul Stewart | 68972ef | 2011-04-28 05:43:37 +0000 | [diff] [blame] | 1570 | /* resume interrupt URBs */ |
| 1571 | if (dev->interrupt && test_bit(EVENT_DEV_OPEN, &dev->flags)) |
| 1572 | usb_submit_urb(dev->interrupt, GFP_NOIO); |
| 1573 | |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1574 | spin_lock_irq(&dev->txq.lock); |
| 1575 | while ((res = usb_get_from_anchor(&dev->deferred))) { |
| 1576 | |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1577 | skb = (struct sk_buff *)res->context; |
| 1578 | retval = usb_submit_urb(res, GFP_ATOMIC); |
| 1579 | if (retval < 0) { |
| 1580 | dev_kfree_skb_any(skb); |
| 1581 | usb_free_urb(res); |
| 1582 | usb_autopm_put_interface_async(dev->intf); |
| 1583 | } else { |
| 1584 | dev->net->trans_start = jiffies; |
| 1585 | __skb_queue_tail(&dev->txq, skb); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | smp_mb(); |
| 1590 | clear_bit(EVENT_DEV_ASLEEP, &dev->flags); |
| 1591 | spin_unlock_irq(&dev->txq.lock); |
| Ming Lei | 75bd0cb | 2011-04-28 22:37:09 +0000 | [diff] [blame] | 1592 | |
| 1593 | if (test_bit(EVENT_DEV_OPEN, &dev->flags)) { |
| 1594 | if (!(dev->txq.qlen >= TX_QLEN(dev))) |
| Alexey Orishko | 1aa9bc5 | 2012-03-14 04:00:24 +0000 | [diff] [blame] | 1595 | netif_tx_wake_all_queues(dev->net); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1596 | queue_work(usbnet_wq, &dev->bh_w); |
| Ming Lei | 75bd0cb | 2011-04-28 22:37:09 +0000 | [diff] [blame] | 1597 | } |
| Oliver Neukum | 69ee472 | 2009-12-03 15:31:18 -0800 | [diff] [blame] | 1598 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| David Brownell | 38bde1d | 2005-08-31 09:52:45 -0700 | [diff] [blame] | 1601 | EXPORT_SYMBOL_GPL(usbnet_resume); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | |
| 1604 | /*-------------------------------------------------------------------------*/ |
| 1605 | |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1606 | static int __init usbnet_init(void) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | { |
| Thiago Farina | c582a95 | 2011-04-17 17:49:21 -0700 | [diff] [blame] | 1608 | /* Compiler should optimize this out. */ |
| 1609 | BUILD_BUG_ON( |
| 1610 | FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data)); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
| 1612 | random_ether_addr(node_id); |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1613 | |
| 1614 | usbnet_wq = create_singlethread_workqueue("usbnet"); |
| 1615 | if (!usbnet_wq) { |
| 1616 | pr_err("%s: Unable to create workqueue:usbnet\n", __func__); |
| 1617 | return -ENOMEM; |
| 1618 | } |
| 1619 | |
| David Brownell | cb1cebb | 2007-02-15 18:52:30 -0800 | [diff] [blame] | 1620 | return 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1622 | module_init(usbnet_init); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1624 | static void __exit usbnet_exit(void) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | { |
| Hemant Kumar | 1e8ddb5 | 2012-07-02 13:39:22 -0700 | [diff] [blame] | 1626 | destroy_workqueue(usbnet_wq); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | } |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1628 | module_exit(usbnet_exit); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1630 | MODULE_AUTHOR("David Brownell"); |
| David Brownell | 090ffa9 | 2005-08-31 09:54:50 -0700 | [diff] [blame] | 1631 | MODULE_DESCRIPTION("USB network driver framework"); |
| David Brownell | f29fc25 | 2005-08-31 09:52:31 -0700 | [diff] [blame] | 1632 | MODULE_LICENSE("GPL"); |