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