blob: d74de074a438bc285a47e4d8b2cb807bf9c05453 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownell090ffa92005-08-31 09:54:50 -07002 * USB Network driver infrastructure
David Brownellf29fc252005-08-31 09:52:31 -07003 * Copyright (C) 2000-2005 by David Brownell
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 Brownell090ffa92005-08-31 09:54:50 -070023 * kinds of full and high speed networking devices: host-to-host cables,
24 * smart usb peripherals, and actual Ethernet adapters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
David Brownell090ffa92005-08-31 09:54:50 -070026 * 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 Torvalds1da177e2005-04-16 15:20:36 -070032
33// #define DEBUG // error path messages, extra info
34// #define VERBOSE // more; success messages
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
Peter Holik03ad0322009-04-18 07:24:17 +000040#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/ethtool.h>
42#include <linux/workqueue.h>
43#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020045#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Andy Shevchenkofd1f1702010-07-23 03:18:08 +000047#include <linux/kernel.h>
Ming Leib0786b42010-11-01 07:11:54 -070048#include <linux/pm_runtime.h>
David Brownellf29fc252005-08-31 09:52:31 -070049
50#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
53/*-------------------------------------------------------------------------*/
54
55/*
56 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
57 * Several dozen bytes of IPv4 data can fit in two such transactions.
58 * One maximum size Ethernet packet takes twenty four of them.
59 * For high speed, each frame comfortably fits almost 36 max size
60 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070061 *
Ming Leia88c32a2013-07-25 13:47:53 +080062 * The goal is to let the USB host controller be busy for 5msec or
63 * more before an irq is required, under load. Jumbograms change
64 * the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Ming Leia88c32a2013-07-25 13:47:53 +080066#define MAX_QUEUE_MEMORY (60 * 1518)
67#define RX_QLEN(dev) ((dev)->rx_qlen)
68#define TX_QLEN(dev) ((dev)->tx_qlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070// reawaken network queue this soon after stopping; else watchdog barks
71#define TX_TIMEOUT_JIFFIES (5*HZ)
72
73// throttle rx/tx briefly after some faults, so khubd might disconnect()
74// us (it polls at HZ/4 usually) before we report too many false errors.
75#define THROTTLE_JIFFIES (HZ/8)
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077// between wakeups
78#define UNLINK_TIMEOUT_MS 3
79
80/*-------------------------------------------------------------------------*/
81
82// randomly generated ethernet address
83static u8 node_id [ETH_ALEN];
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static const char driver_name [] = "usbnet";
86
87/* use ethtool to change the level for any given device */
88static int msg_level = -1;
89module_param (msg_level, int, 0);
90MODULE_PARM_DESC (msg_level, "Override default message level");
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*-------------------------------------------------------------------------*/
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -070095int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 int tmp;
98 struct usb_host_interface *alt = NULL;
99 struct usb_host_endpoint *in = NULL, *out = NULL;
100 struct usb_host_endpoint *status = NULL;
101
102 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
103 unsigned ep;
104
105 in = out = status = NULL;
106 alt = intf->altsetting + tmp;
107
108 /* take the first altsetting with in-bulk + out-bulk;
109 * remember any status endpoint, just in case;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200110 * ignore other endpoints and altsettings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
112 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
113 struct usb_host_endpoint *e;
114 int intr = 0;
115
116 e = alt->endpoint + ep;
117 switch (e->desc.bmAttributes) {
118 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300119 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 continue;
121 intr = 1;
122 /* FALLTHROUGH */
123 case USB_ENDPOINT_XFER_BULK:
124 break;
125 default:
126 continue;
127 }
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300128 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!intr && !in)
130 in = e;
131 else if (intr && !status)
132 status = e;
133 } else {
134 if (!out)
135 out = e;
136 }
137 }
138 if (in && out)
139 break;
140 }
141 if (!alt || !in || !out)
142 return -EINVAL;
143
Joe Perches8e95a202009-12-03 07:58:21 +0000144 if (alt->desc.bAlternateSetting != 0 ||
145 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
147 alt->desc.bAlternateSetting);
148 if (tmp < 0)
149 return tmp;
150 }
David Brownellcb1cebb2007-02-15 18:52:30 -0800151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 dev->in = usb_rcvbulkpipe (dev->udev,
153 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
154 dev->out = usb_sndbulkpipe (dev->udev,
155 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
156 dev->status = status;
157 return 0;
158}
David Brownell2e55cc72005-08-31 09:53:10 -0700159EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Peter Holik03ad0322009-04-18 07:24:17 +0000161int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
162{
163 int tmp, i;
164 unsigned char buf [13];
165
166 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
167 if (tmp != 12) {
168 dev_dbg(&dev->udev->dev,
169 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
170 if (tmp >= 0)
171 tmp = -EINVAL;
172 return tmp;
173 }
174 for (i = tmp = 0; i < 6; i++, tmp += 2)
175 dev->net->dev_addr [i] =
Andy Shevchenkofd1f1702010-07-23 03:18:08 +0000176 (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]);
Peter Holik03ad0322009-04-18 07:24:17 +0000177 return 0;
178}
179EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
180
tom.leiming@gmail.com24ead292012-06-11 15:19:44 +0000181static void intr_complete (struct urb *urb)
182{
183 struct usbnet *dev = urb->context;
184 int status = urb->status;
185
186 switch (status) {
187 /* success */
188 case 0:
189 dev->driver_info->status(dev, urb);
190 break;
191
192 /* software-driven interface shutdown */
193 case -ENOENT: /* urb killed */
194 case -ESHUTDOWN: /* hardware gone */
195 netif_dbg(dev, ifdown, dev->net,
196 "intr shutdown, code %d\n", status);
197 return;
198
199 /* NOTE: not throttling like RX/TX, since this endpoint
200 * already polls infrequently
201 */
202 default:
203 netdev_dbg(dev->net, "intr status %d\n", status);
204 break;
205 }
206
207 if (!netif_running (dev->net))
208 return;
209
tom.leiming@gmail.com24ead292012-06-11 15:19:44 +0000210 status = usb_submit_urb (urb, GFP_ATOMIC);
211 if (status != 0)
212 netif_err(dev, timer, dev->net,
213 "intr resubmit --> %d\n", status);
214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216static int init_status (struct usbnet *dev, struct usb_interface *intf)
217{
218 char *buf = NULL;
219 unsigned pipe = 0;
220 unsigned maxp;
221 unsigned period;
222
223 if (!dev->driver_info->status)
224 return 0;
225
226 pipe = usb_rcvintpipe (dev->udev,
227 dev->status->desc.bEndpointAddress
228 & USB_ENDPOINT_NUMBER_MASK);
229 maxp = usb_maxpacket (dev->udev, pipe, 0);
230
231 /* avoid 1 msec chatter: min 8 msec poll rate */
232 period = max ((int) dev->status->desc.bInterval,
233 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
234
Christoph Lametere94b1762006-12-06 20:33:17 -0800235 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800237 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!dev->interrupt) {
239 kfree (buf);
240 return -ENOMEM;
241 } else {
242 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
243 buf, maxp, intr_complete, dev, period);
tom.leiming@gmail.com720f3d72012-04-29 22:51:02 +0000244 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dev_dbg(&intf->dev,
246 "status ep%din, %d bytes period %d\n",
247 usb_pipeendpoint(pipe), maxp, period);
248 }
249 }
David Brownell18ab4582007-05-25 12:31:32 -0700250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
Dan Williams6eecdc52013-05-06 11:29:23 +0000253/* Submit the interrupt URB if not previously submitted, increasing refcount */
254int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
255{
256 int ret = 0;
257
258 WARN_ON_ONCE(dev->interrupt == NULL);
259 if (dev->interrupt) {
260 mutex_lock(&dev->interrupt_mutex);
261
262 if (++dev->interrupt_count == 1)
263 ret = usb_submit_urb(dev->interrupt, mem_flags);
264
265 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
266 dev->interrupt_count);
267 mutex_unlock(&dev->interrupt_mutex);
268 }
269 return ret;
270}
271EXPORT_SYMBOL_GPL(usbnet_status_start);
272
273/* For resume; submit interrupt URB if previously submitted */
274static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
275{
276 int ret = 0;
277
278 mutex_lock(&dev->interrupt_mutex);
279 if (dev->interrupt_count) {
280 ret = usb_submit_urb(dev->interrupt, mem_flags);
281 dev_dbg(&dev->udev->dev,
282 "submitted interrupt URB for resume\n");
283 }
284 mutex_unlock(&dev->interrupt_mutex);
285 return ret;
286}
287
288/* Kill the interrupt URB if all submitters want it killed */
289void usbnet_status_stop(struct usbnet *dev)
290{
291 if (dev->interrupt) {
292 mutex_lock(&dev->interrupt_mutex);
293 WARN_ON(dev->interrupt_count == 0);
294
295 if (dev->interrupt_count && --dev->interrupt_count == 0)
296 usb_kill_urb(dev->interrupt);
297
298 dev_dbg(&dev->udev->dev,
299 "decremented interrupt URB count to %d\n",
300 dev->interrupt_count);
301 mutex_unlock(&dev->interrupt_mutex);
302 }
303}
304EXPORT_SYMBOL_GPL(usbnet_status_stop);
305
306/* For suspend; always kill interrupt URB */
307static void __usbnet_status_stop_force(struct usbnet *dev)
308{
309 if (dev->interrupt) {
310 mutex_lock(&dev->interrupt_mutex);
311 usb_kill_urb(dev->interrupt);
312 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
313 mutex_unlock(&dev->interrupt_mutex);
314 }
315}
316
David Brownell2e55cc72005-08-31 09:53:10 -0700317/* Passes this packet up the stack, updating its accounting.
318 * Some link protocols batch packets, so their rx_fixup paths
319 * can return clones as well as just modify the original skb.
320 */
321void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int status;
324
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300325 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
326 skb_queue_tail(&dev->rxq_pause, skb);
327 return;
328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 skb->protocol = eth_type_trans (skb, dev->net);
Herbert Xu79638372009-06-29 16:53:28 +0000331 dev->net->stats.rx_packets++;
332 dev->net->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Joe Perchesa475f602010-02-17 10:30:24 +0000334 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
335 skb->len + sizeof (struct ethhdr), skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 memset (skb->cb, 0, sizeof (struct skb_data));
Michael Rieschf9b491e2011-09-29 04:06:26 +0000337
338 if (skb_defer_rx_timestamp(skb))
339 return;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 status = netif_rx (skb);
Joe Perchesa475f602010-02-17 10:30:24 +0000342 if (status != NET_RX_SUCCESS)
343 netif_dbg(dev, rx_err, dev->net,
344 "netif_rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
David Brownell2e55cc72005-08-31 09:53:10 -0700346EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Ming Leia88c32a2013-07-25 13:47:53 +0800348/* must be called if hard_mtu or rx_urb_size changed */
349void usbnet_update_max_qlen(struct usbnet *dev)
350{
351 enum usb_device_speed speed = dev->udev->speed;
352
353 switch (speed) {
354 case USB_SPEED_HIGH:
355 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
356 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
357 break;
358 default:
359 dev->rx_qlen = dev->tx_qlen = 4;
360 }
361}
362EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*-------------------------------------------------------------------------
366 *
367 * Network Device Driver (peer link to "Host Device", from USB host)
368 *
369 *-------------------------------------------------------------------------*/
370
Stephen Hemminger777baa42009-03-20 19:35:54 +0000371int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700374 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400375 int old_hard_mtu = dev->hard_mtu;
376 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jamie Paintera99c1942006-07-27 14:17:28 -0400378 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700381 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EDOM;
383 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400384
385 dev->hard_mtu = net->mtu + net->hard_header_len;
386 if (dev->rx_urb_size == old_hard_mtu) {
387 dev->rx_urb_size = dev->hard_mtu;
388 if (dev->rx_urb_size > old_rx_urb_size)
389 usbnet_unlink_rx_urbs(dev);
390 }
391
Ming Leia88c32a2013-07-25 13:47:53 +0800392 /* max qlen depend on hard_mtu and rx_urb_size */
393 usbnet_update_max_qlen(dev);
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000397EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800399/* The caller must hold list->lock */
400static void __usbnet_queue_skb(struct sk_buff_head *list,
401 struct sk_buff *newsk, enum skb_state state)
402{
403 struct skb_data *entry = (struct skb_data *) newsk->cb;
404
405 __skb_queue_tail(list, newsk);
406 entry->state = state;
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/*-------------------------------------------------------------------------*/
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
412 * completion callbacks. 2.5 should have fixed those bugs...
413 */
414
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800415static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
416 struct sk_buff_head *list, enum skb_state state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 unsigned long flags;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800419 enum skb_state old_state;
420 struct skb_data *entry = (struct skb_data *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David S. Miller8728b832005-08-09 19:25:21 -0700422 spin_lock_irqsave(&list->lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800423 old_state = entry->state;
424 entry->state = state;
David S. Miller8728b832005-08-09 19:25:21 -0700425 __skb_unlink(skb, list);
426 spin_unlock(&list->lock);
427 spin_lock(&dev->done.lock);
428 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700430 tasklet_schedule(&dev->bh);
431 spin_unlock_irqrestore(&dev->done.lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800432 return old_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/* some work can't be done in tasklets, so we use keventd
436 *
437 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
438 * but tasklet_schedule() doesn't. hope the failure is rare.
439 */
David Brownell2e55cc72005-08-31 09:53:10 -0700440void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 set_bit (work, &dev->flags);
Steve Glendinning95320212012-11-08 06:26:21 +0000443 if (!schedule_work (&dev->kevent)) {
444 if (net_ratelimit())
445 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
446 } else {
Joe Perches60b86752010-02-17 10:30:23 +0000447 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
Steve Glendinning95320212012-11-08 06:26:21 +0000448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
David Brownell2e55cc72005-08-31 09:53:10 -0700450EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452/*-------------------------------------------------------------------------*/
453
David Howells7d12e782006-10-05 14:55:46 +0100454static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
David S. Millerdacb3972010-08-10 02:50:55 -0700456static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct sk_buff *skb;
459 struct skb_data *entry;
460 int retval = 0;
461 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700462 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Bjørn Mork70c37bf2013-01-28 23:51:28 +0000464 /* prevent rx skb allocation when error ratio is high */
465 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
466 usb_free_urb(urb);
467 return -ENOLINK;
468 }
469
Eric Dumazet7bdd4022012-03-14 06:56:25 +0000470 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
471 if (!skb) {
Joe Perchesa475f602010-02-17 10:30:24 +0000472 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
David Brownell2e55cc72005-08-31 09:53:10 -0700473 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 usb_free_urb (urb);
David S. Millerdacb3972010-08-10 02:50:55 -0700475 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 entry = (struct skb_data *) skb->cb;
479 entry->urb = urb;
480 entry->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 entry->length = 0;
482
483 usb_fill_bulk_urb (urb, dev->udev, dev->in,
484 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 spin_lock_irqsave (&dev->rxq.lock, lockflags);
487
Joe Perches8e95a202009-12-03 07:58:21 +0000488 if (netif_running (dev->net) &&
489 netif_device_present (dev->net) &&
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800490 !test_bit (EVENT_RX_HALT, &dev->flags) &&
491 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700492 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700494 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700497 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 case -ENODEV:
Joe Perchesa475f602010-02-17 10:30:24 +0000500 netif_dbg(dev, ifdown, dev->net, "device gone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 netif_device_detach (dev->net);
502 break;
David S. Millerdacb3972010-08-10 02:50:55 -0700503 case -EHOSTUNREACH:
504 retval = -ENOLINK;
505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000507 netif_dbg(dev, rx_err, dev->net,
508 "rx submit, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 tasklet_schedule (&dev->bh);
510 break;
511 case 0:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800512 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000515 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 retval = -ENOLINK;
517 }
518 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
519 if (retval) {
520 dev_kfree_skb_any (skb);
521 usb_free_urb (urb);
522 }
David S. Millerdacb3972010-08-10 02:50:55 -0700523 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526
527/*-------------------------------------------------------------------------*/
528
529static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
530{
Joe Perches8e95a202009-12-03 07:58:21 +0000531 if (dev->driver_info->rx_fixup &&
Andrzej Zaborowski7a635ea2011-03-28 12:56:33 +0000532 !dev->driver_info->rx_fixup (dev, skb)) {
533 /* With RX_ASSEMBLE, rx_fixup() must update counters */
534 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
535 dev->net->stats.rx_errors++;
536 goto done;
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 // else network stack removes extra byte if we forced a short packet
539
Alexey Orishko073285f2010-11-29 23:23:27 +0000540 if (skb->len) {
541 /* all data was already cloned from skb inside the driver */
542 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
543 dev_kfree_skb_any(skb);
544 else
545 usbnet_skb_return(dev, skb);
546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Alexey Orishko073285f2010-11-29 23:23:27 +0000548
549 netif_dbg(dev, rx_err, dev->net, "drop\n");
Alexey Orishko073285f2010-11-29 23:23:27 +0000550 dev->net->stats.rx_errors++;
Andrzej Zaborowski7a635ea2011-03-28 12:56:33 +0000551done:
Alexey Orishko073285f2010-11-29 23:23:27 +0000552 skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*-------------------------------------------------------------------------*/
556
David Howells7d12e782006-10-05 14:55:46 +0100557static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct sk_buff *skb = (struct sk_buff *) urb->context;
560 struct skb_data *entry = (struct skb_data *) skb->cb;
561 struct usbnet *dev = entry->dev;
562 int urb_status = urb->status;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800563 enum skb_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 skb_put (skb, urb->actual_length);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800566 state = rx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 entry->urb = NULL;
568
569 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700570 /* success */
571 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700572 if (skb->len < dev->net->hard_header_len) {
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800573 state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000574 dev->net->stats.rx_errors++;
575 dev->net->stats.rx_length_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000576 netif_dbg(dev, rx_err, dev->net,
577 "rx length %d\n", skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 break;
580
David Brownell18ab4582007-05-25 12:31:32 -0700581 /* stalls need manual reset. this is rare ... except that
582 * when going through USB 2.0 TTs, unplug appears this way.
Jean Delvare3ac49a12009-06-04 16:20:28 +0200583 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
David Brownell18ab4582007-05-25 12:31:32 -0700584 * storm, recovering as needed.
585 */
586 case -EPIPE:
Herbert Xu79638372009-06-29 16:53:28 +0000587 dev->net->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700588 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 // FALLTHROUGH
590
David Brownell18ab4582007-05-25 12:31:32 -0700591 /* software-driven interface shutdown */
592 case -ECONNRESET: /* async unlink */
593 case -ESHUTDOWN: /* hardware gone */
Joe Perchesa475f602010-02-17 10:30:24 +0000594 netif_dbg(dev, ifdown, dev->net,
595 "rx shutdown, code %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto block;
597
David Brownell18ab4582007-05-25 12:31:32 -0700598 /* we get controller i/o faults during khubd disconnect() delays.
599 * throttle down resubmits, to avoid log floods; just temporarily,
600 * so we still recover when the fault isn't a khubd delay.
601 */
602 case -EPROTO:
603 case -ETIME:
604 case -EILSEQ:
Herbert Xu79638372009-06-29 16:53:28 +0000605 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!timer_pending (&dev->delay)) {
607 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +0000608 netif_dbg(dev, link, dev->net,
609 "rx throttle %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611block:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800612 state = rx_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 entry->urb = urb;
614 urb = NULL;
615 break;
616
David Brownell18ab4582007-05-25 12:31:32 -0700617 /* data overrun ... flush fifo? */
618 case -EOVERFLOW:
Herbert Xu79638372009-06-29 16:53:28 +0000619 dev->net->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800621
David Brownell18ab4582007-05-25 12:31:32 -0700622 default:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800623 state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000624 dev->net->stats.rx_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000625 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
627 }
628
Bjørn Mork70c37bf2013-01-28 23:51:28 +0000629 /* stop rx if packet error rate is high */
630 if (++dev->pkt_cnt > 30) {
631 dev->pkt_cnt = 0;
632 dev->pkt_err = 0;
633 } else {
634 if (state == rx_cleanup)
635 dev->pkt_err++;
636 if (dev->pkt_err > 20)
637 set_bit(EVENT_RX_KILL, &dev->flags);
638 }
639
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800640 state = defer_bh(dev, skb, &dev->rxq, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (urb) {
Joe Perches8e95a202009-12-03 07:58:21 +0000643 if (netif_running (dev->net) &&
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800644 !test_bit (EVENT_RX_HALT, &dev->flags) &&
645 state != unlink_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 rx_submit (dev, urb, GFP_ATOMIC);
Oliver Neukum8a783352012-03-03 18:45:07 +0100647 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return;
649 }
650 usb_free_urb (urb);
651 }
Joe Perchesa475f602010-02-17 10:30:24 +0000652 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*-------------------------------------------------------------------------*/
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300656void usbnet_pause_rx(struct usbnet *dev)
657{
658 set_bit(EVENT_RX_PAUSED, &dev->flags);
659
Joe Perchesa475f602010-02-17 10:30:24 +0000660 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300661}
662EXPORT_SYMBOL_GPL(usbnet_pause_rx);
663
664void usbnet_resume_rx(struct usbnet *dev)
665{
666 struct sk_buff *skb;
667 int num = 0;
668
669 clear_bit(EVENT_RX_PAUSED, &dev->flags);
670
671 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
672 usbnet_skb_return(dev, skb);
673 num++;
674 }
675
676 tasklet_schedule(&dev->bh);
677
Joe Perchesa475f602010-02-17 10:30:24 +0000678 netif_dbg(dev, rx_status, dev->net,
679 "paused rx queue disabled, %d skbs requeued\n", num);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300680}
681EXPORT_SYMBOL_GPL(usbnet_resume_rx);
682
683void usbnet_purge_paused_rxq(struct usbnet *dev)
684{
685 skb_queue_purge(&dev->rxq_pause);
686}
687EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
688
689/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691// unlink pending rx/tx; completion handlers do all other cleanup
692
693static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
694{
695 unsigned long flags;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800696 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int count = 0;
698
699 spin_lock_irqsave (&q->lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800700 while (!skb_queue_empty(q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct skb_data *entry;
702 struct urb *urb;
703 int retval;
704
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800705 skb_queue_walk(q, skb) {
706 entry = (struct skb_data *) skb->cb;
707 if (entry->state != unlink_start)
708 goto found;
709 }
710 break;
711found:
712 entry->state = unlink_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
tom.leiming@gmail.com0956a8c2012-03-22 03:22:18 +0000715 /*
716 * Get reference count of the URB to avoid it to be
717 * freed during usb_unlink_urb, which may trigger
718 * use-after-free problem inside usb_unlink_urb since
719 * usb_unlink_urb is always racing with .complete
720 * handler(include defer_bh).
721 */
722 usb_get_urb(urb);
Sebastian Siewior4231d472012-03-07 10:19:28 +0000723 spin_unlock_irqrestore(&q->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 // during some PM-driven resume scenarios,
725 // these (async) unlinks complete immediately
726 retval = usb_unlink_urb (urb);
727 if (retval != -EINPROGRESS && retval != 0)
Joe Perches60b86752010-02-17 10:30:23 +0000728 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 else
730 count++;
tom.leiming@gmail.com0956a8c2012-03-22 03:22:18 +0000731 usb_put_urb(urb);
Sebastian Siewior4231d472012-03-07 10:19:28 +0000732 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734 spin_unlock_irqrestore (&q->lock, flags);
735 return count;
736}
737
Jamie Paintera99c1942006-07-27 14:17:28 -0400738// Flush all pending rx urbs
739// minidrivers may need to do this when the MTU changes
740
741void usbnet_unlink_rx_urbs(struct usbnet *dev)
742{
743 if (netif_running(dev->net)) {
744 (void) unlink_urbs (dev, &dev->rxq);
745 tasklet_schedule(&dev->bh);
746 }
747}
748EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750/*-------------------------------------------------------------------------*/
751
752// precondition: never called in_interrupt
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800753static void usbnet_terminate_urbs(struct usbnet *dev)
754{
755 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
756 DECLARE_WAITQUEUE(wait, current);
757 int temp;
758
759 /* ensure there are no more active urbs */
760 add_wait_queue(&unlink_wakeup, &wait);
761 set_current_state(TASK_UNINTERRUPTIBLE);
762 dev->wait = &unlink_wakeup;
763 temp = unlink_urbs(dev, &dev->txq) +
764 unlink_urbs(dev, &dev->rxq);
765
766 /* maybe wait for deletions to finish. */
767 while (!skb_queue_empty(&dev->rxq)
768 && !skb_queue_empty(&dev->txq)
769 && !skb_queue_empty(&dev->done)) {
Kulikov Vasiliy66cc42a2010-07-25 22:25:42 +0000770 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800771 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perchesa475f602010-02-17 10:30:24 +0000772 netif_dbg(dev, ifdown, dev->net,
773 "waited for %d urb completions\n", temp);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800774 }
775 set_current_state(TASK_RUNNING);
776 dev->wait = NULL;
777 remove_wait_queue(&unlink_wakeup, &wait);
778}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Stephen Hemminger777baa42009-03-20 19:35:54 +0000780int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct usbnet *dev = netdev_priv(net);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300783 struct driver_info *info = dev->driver_info;
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300784 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Ming Lei75bd0cb2011-04-28 22:37:09 +0000786 clear_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 netif_stop_queue (net);
788
Joe Perchesa475f602010-02-17 10:30:24 +0000789 netif_info(dev, ifdown, dev->net,
Ben Hutchings8ccef432010-06-08 08:20:59 +0000790 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
Joe Perchesa475f602010-02-17 10:30:24 +0000791 net->stats.rx_packets, net->stats.tx_packets,
792 net->stats.rx_errors, net->stats.tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300794 /* allow minidriver to stop correctly (wireless devices to turn off
795 * radio etc) */
796 if (info->stop) {
797 retval = info->stop(dev);
Joe Perchesa475f602010-02-17 10:30:24 +0000798 if (retval < 0)
799 netif_info(dev, ifdown, dev->net,
800 "stop fail (%d) usbnet usb-%s-%s, %s\n",
801 retval,
802 dev->udev->bus->bus_name, dev->udev->devpath,
803 info->description);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300804 }
805
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800806 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
807 usbnet_terminate_urbs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dan Williams6eecdc52013-05-06 11:29:23 +0000809 usbnet_status_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300811 usbnet_purge_paused_rxq(dev);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* deferred work (task, timer, softirq) must also stop.
814 * can't flush_scheduled_work() until we drop rtnl (later),
815 * else workers could deadlock; so make workers a NOP.
816 */
817 dev->flags = 0;
818 del_timer_sync (&dev->delay);
819 tasklet_kill (&dev->bh);
Oliver Neukuma1c088e2012-12-18 04:45:29 +0000820 if (info->manage_power &&
821 !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags))
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800822 info->manage_power(dev, 0);
823 else
824 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 return 0;
827}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000828EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830/*-------------------------------------------------------------------------*/
831
832// posts reads, and enables write queuing
833
834// precondition: never called in_interrupt
835
Stephen Hemminger777baa42009-03-20 19:35:54 +0000836int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200839 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct driver_info *info = dev->driver_info;
841
Oliver Neukuma11a6542007-08-03 13:52:19 +0200842 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000843 netif_info(dev, ifup, dev->net,
844 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
845 retval,
846 dev->udev->bus->bus_name,
847 dev->udev->devpath,
848 info->description);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200849 goto done_nopm;
850 }
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 // put into "known safe" state
853 if (info->reset && (retval = info->reset (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000854 netif_info(dev, ifup, dev->net,
855 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
856 retval,
857 dev->udev->bus->bus_name,
858 dev->udev->devpath,
859 info->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 goto done;
861 }
862
Ming Leia88c32a2013-07-25 13:47:53 +0800863 /* hard_mtu or rx_urb_size may change in reset() */
864 usbnet_update_max_qlen(dev);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 // insist peer be connected
867 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000868 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto done;
870 }
871
872 /* start any status interrupt transfer */
873 if (dev->interrupt) {
Dan Williams6eecdc52013-05-06 11:29:23 +0000874 retval = usbnet_status_start(dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (retval < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000876 netif_err(dev, ifup, dev->net,
877 "intr submit %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto done;
879 }
880 }
881
Paul Stewart68972ef2011-04-28 05:43:37 +0000882 set_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 netif_start_queue (net);
Joe Perchesa475f602010-02-17 10:30:24 +0000884 netif_info(dev, ifup, dev->net,
885 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
886 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
887 dev->net->mtu,
888 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
889 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
890 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
891 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
892 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
893 "simple");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Bjørn Mork70c37bf2013-01-28 23:51:28 +0000895 /* reset rx error state */
896 dev->pkt_cnt = 0;
897 dev->pkt_err = 0;
898 clear_bit(EVENT_RX_KILL, &dev->flags);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 // delay posting reads until we're fully open
901 tasklet_schedule (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800902 if (info->manage_power) {
903 retval = info->manage_power(dev, 1);
Oliver Neukuma1c088e2012-12-18 04:45:29 +0000904 if (retval < 0) {
905 retval = 0;
906 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
907 } else {
908 usb_autopm_put_interface(dev->intf);
909 }
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800910 }
Oliver Neukuma11a6542007-08-03 13:52:19 +0200911 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200913 usb_autopm_put_interface(dev->intf);
914done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return retval;
916}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000917EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919/*-------------------------------------------------------------------------*/
920
David Brownell2e55cc72005-08-31 09:53:10 -0700921/* ethtool methods; minidrivers may need to add some more, but
922 * they'll probably want to use this base set.
923 */
924
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200925int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
926{
927 struct usbnet *dev = netdev_priv(net);
928
929 if (!dev->mii.mdio_read)
930 return -EOPNOTSUPP;
931
932 return mii_ethtool_gset(&dev->mii, cmd);
933}
934EXPORT_SYMBOL_GPL(usbnet_get_settings);
935
936int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
937{
938 struct usbnet *dev = netdev_priv(net);
939 int retval;
940
941 if (!dev->mii.mdio_write)
942 return -EOPNOTSUPP;
943
944 retval = mii_ethtool_sset(&dev->mii, cmd);
945
946 /* link speed/duplex might have changed */
947 if (dev->driver_info->link_reset)
948 dev->driver_info->link_reset(dev);
949
Ming Leia88c32a2013-07-25 13:47:53 +0800950 /* hard_mtu or rx_urb_size may change in link_reset() */
951 usbnet_update_max_qlen(dev);
952
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200953 return retval;
954
955}
956EXPORT_SYMBOL_GPL(usbnet_set_settings);
957
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200958u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct usbnet *dev = netdev_priv(net);
961
David Brownellf29fc252005-08-31 09:52:31 -0700962 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (dev->driver_info->check_connect)
964 return dev->driver_info->check_connect (dev) == 0;
965
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200966 /* if the device has mii operations, use those */
967 if (dev->mii.mdio_read)
968 return mii_link_ok(&dev->mii);
969
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800970 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
971 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200973EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
David Brownell18ee91fa2006-11-02 12:29:12 -0800975int usbnet_nway_reset(struct net_device *net)
976{
977 struct usbnet *dev = netdev_priv(net);
978
979 if (!dev->mii.mdio_write)
980 return -EOPNOTSUPP;
981
982 return mii_nway_restart(&dev->mii);
983}
984EXPORT_SYMBOL_GPL(usbnet_nway_reset);
985
David Brownell18ee91fa2006-11-02 12:29:12 -0800986void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
987{
988 struct usbnet *dev = netdev_priv(net);
989
Phil Sutter86a2f412012-06-14 01:18:42 +0000990 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
991 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
992 strlcpy (info->fw_version, dev->driver_info->description,
David Brownell18ee91fa2006-11-02 12:29:12 -0800993 sizeof info->fw_version);
994 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
995}
996EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
997
David Brownell2e55cc72005-08-31 09:53:10 -0700998u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct usbnet *dev = netdev_priv(net);
1001
1002 return dev->msg_enable;
1003}
David Brownell2e55cc72005-08-31 09:53:10 -07001004EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
David Brownell2e55cc72005-08-31 09:53:10 -07001006void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct usbnet *dev = netdev_priv(net);
1009
1010 dev->msg_enable = level;
1011}
David Brownell2e55cc72005-08-31 09:53:10 -07001012EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David Brownell2e55cc72005-08-31 09:53:10 -07001014/* drivers may override default ethtool_ops in their bind() routine */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001015static const struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +02001016 .get_settings = usbnet_get_settings,
1017 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -07001018 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +02001019 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -08001020 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -07001021 .get_msglevel = usbnet_get_msglevel,
1022 .set_msglevel = usbnet_set_msglevel,
Richard Cochran123edb12012-04-03 22:59:41 +00001023 .get_ts_info = ethtool_op_get_ts_info,
David Brownell2e55cc72005-08-31 09:53:10 -07001024};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026/*-------------------------------------------------------------------------*/
1027
Ming Lei4b49f582013-04-11 04:40:40 +00001028static void __handle_link_change(struct usbnet *dev)
1029{
1030 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1031 return;
1032
1033 if (!netif_carrier_ok(dev->net)) {
1034 /* kill URBs for reading packets to save bus bandwidth */
1035 unlink_urbs(dev, &dev->rxq);
1036
1037 /*
1038 * tx_timeout will unlink URBs for sending packets and
1039 * tx queue is stopped by netcore after link becomes off
1040 */
1041 } else {
1042 /* submitting URBs for reading packets */
1043 tasklet_schedule(&dev->bh);
1044 }
1045
Ming Leia88c32a2013-07-25 13:47:53 +08001046 /* hard_mtu or rx_urb_size may change during link change */
1047 usbnet_update_max_qlen(dev);
1048
Ming Lei4b49f582013-04-11 04:40:40 +00001049 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/* work that cannot be done in interrupt context uses keventd.
1053 *
1054 * NOTE: with 2.5 we could do more of this using completion callbacks,
1055 * especially now that control transfers can be queued.
1056 */
1057static void
David Howellsc4028952006-11-22 14:57:56 +00001058kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
David Howellsc4028952006-11-22 14:57:56 +00001060 struct usbnet *dev =
1061 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int status;
1063
1064 /* usb_clear_halt() needs a thread context */
1065 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1066 unlink_urbs (dev, &dev->txq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001067 status = usb_autopm_get_interface(dev->intf);
1068 if (status < 0)
1069 goto fail_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 status = usb_clear_halt (dev->udev, dev->out);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001071 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +00001072 if (status < 0 &&
1073 status != -EPIPE &&
1074 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (netif_msg_tx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001076fail_pipe:
Joe Perches60b86752010-02-17 10:30:23 +00001077 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1078 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 } else {
1080 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -07001081 if (status != -ESHUTDOWN)
1082 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 }
1085 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1086 unlink_urbs (dev, &dev->rxq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001087 status = usb_autopm_get_interface(dev->intf);
1088 if (status < 0)
1089 goto fail_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 status = usb_clear_halt (dev->udev, dev->in);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001091 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +00001092 if (status < 0 &&
1093 status != -EPIPE &&
1094 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (netif_msg_rx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001096fail_halt:
Joe Perches60b86752010-02-17 10:30:23 +00001097 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1098 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 } else {
1100 clear_bit (EVENT_RX_HALT, &dev->flags);
1101 tasklet_schedule (&dev->bh);
1102 }
1103 }
1104
1105 /* tasklet could resubmit itself forever if memory is tight */
1106 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1107 struct urb *urb = NULL;
David S. Millerdacb3972010-08-10 02:50:55 -07001108 int resched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 if (netif_running (dev->net))
1111 urb = usb_alloc_urb (0, GFP_KERNEL);
1112 else
1113 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1114 if (urb != NULL) {
1115 clear_bit (EVENT_RX_MEMORY, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001116 status = usb_autopm_get_interface(dev->intf);
Jesper Juhlab607072011-02-10 10:58:45 +00001117 if (status < 0) {
1118 usb_free_urb(urb);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001119 goto fail_lowmem;
Jesper Juhlab607072011-02-10 10:58:45 +00001120 }
David S. Millerdacb3972010-08-10 02:50:55 -07001121 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1122 resched = 0;
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001123 usb_autopm_put_interface(dev->intf);
1124fail_lowmem:
David S. Millerdacb3972010-08-10 02:50:55 -07001125 if (resched)
1126 tasklet_schedule (&dev->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 }
1129
David Hollis7ea13c92005-04-22 15:07:02 -07001130 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -08001131 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -07001132 int retval = 0;
1133
1134 clear_bit (EVENT_LINK_RESET, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001135 status = usb_autopm_get_interface(dev->intf);
1136 if (status < 0)
1137 goto skip_reset;
David Hollis7ea13c92005-04-22 15:07:02 -07001138 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001139 usb_autopm_put_interface(dev->intf);
1140skip_reset:
Joe Perches60b86752010-02-17 10:30:23 +00001141 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1142 retval,
1143 dev->udev->bus->bus_name,
1144 dev->udev->devpath,
1145 info->description);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001146 } else {
1147 usb_autopm_put_interface(dev->intf);
David Hollis7ea13c92005-04-22 15:07:02 -07001148 }
Ming Lei4b49f582013-04-11 04:40:40 +00001149
1150 /* handle link change from link resetting */
1151 __handle_link_change(dev);
David Hollis7ea13c92005-04-22 15:07:02 -07001152 }
1153
Ming Lei4b49f582013-04-11 04:40:40 +00001154 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1155 __handle_link_change(dev);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (dev->flags)
Joe Perches60b86752010-02-17 10:30:23 +00001158 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161/*-------------------------------------------------------------------------*/
1162
David Howells7d12e782006-10-05 14:55:46 +01001163static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct sk_buff *skb = (struct sk_buff *) urb->context;
1166 struct skb_data *entry = (struct skb_data *) skb->cb;
1167 struct usbnet *dev = entry->dev;
1168
1169 if (urb->status == 0) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001170 if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
1171 dev->net->stats.tx_packets++;
Herbert Xu79638372009-06-29 16:53:28 +00001172 dev->net->stats.tx_bytes += entry->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 } else {
Herbert Xu79638372009-06-29 16:53:28 +00001174 dev->net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 switch (urb->status) {
1177 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -07001178 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180
1181 /* software-driven interface shutdown */
1182 case -ECONNRESET: // async unlink
1183 case -ESHUTDOWN: // hardware gone
1184 break;
1185
1186 // like rx, tx gets controller i/o faults during khubd delays
1187 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -07001188 case -EPROTO:
1189 case -ETIME:
1190 case -EILSEQ:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001191 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!timer_pending (&dev->delay)) {
1193 mod_timer (&dev->delay,
1194 jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +00001195 netif_dbg(dev, link, dev->net,
1196 "tx throttle %d\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198 netif_stop_queue (dev->net);
1199 break;
1200 default:
Joe Perchesa475f602010-02-17 10:30:24 +00001201 netif_dbg(dev, tx_err, dev->net,
1202 "tx err %d\n", entry->urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
1204 }
1205 }
1206
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001207 usb_autopm_put_interface_async(dev->intf);
Ming Lei5b6e9bc2012-04-26 11:33:46 +08001208 (void) defer_bh(dev, skb, &dev->txq, tx_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211/*-------------------------------------------------------------------------*/
1212
Stephen Hemminger777baa42009-03-20 19:35:54 +00001213void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct usbnet *dev = netdev_priv(net);
1216
1217 unlink_urbs (dev, &dev->txq);
1218 tasklet_schedule (&dev->bh);
1219
1220 // FIXME: device recovery -- reset?
1221}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001222EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224/*-------------------------------------------------------------------------*/
1225
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001226netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1227 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct usbnet *dev = netdev_priv(net);
1230 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct urb *urb = NULL;
1232 struct skb_data *entry;
1233 struct driver_info *info = dev->driver_info;
1234 unsigned long flags;
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001235 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Konstantin Khlebnikov23ba0792011-11-07 05:54:58 +00001237 if (skb)
1238 skb_tx_timestamp(skb);
Michael Rieschf9b491e2011-09-29 04:06:26 +00001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 // some devices want funky USB-level framing, for
1241 // win32 driver (usually) and/or hardware quirks
1242 if (info->tx_fixup) {
1243 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1244 if (!skb) {
Bjørn Morkbf414b32013-01-31 08:36:05 +00001245 /* packet collected; minidriver waiting for more */
1246 if (info->flags & FLAG_MULTI_PACKET)
Alexey Orishko073285f2010-11-29 23:23:27 +00001247 goto not_drop;
Bjørn Morkbf414b32013-01-31 08:36:05 +00001248 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1249 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 }
1252 length = skb->len;
1253
1254 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001255 netif_dbg(dev, tx_err, dev->net, "no urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto drop;
1257 }
1258
1259 entry = (struct skb_data *) skb->cb;
1260 entry->urb = urb;
1261 entry->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 entry->length = length;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1265 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /* don't assume the hardware handles USB_ZERO_PACKET
1268 * NOTE: strictly conforming cdc-ether devices should expect
1269 * the ZLP here, but ignore the one-byte packet.
Alexey Orishko073285f2010-11-29 23:23:27 +00001270 * NOTE2: CDC NCM specification is different from CDC ECM when
1271 * handling ZLP/short packets, so cdc_ncm driver will make short
1272 * packet itself if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Elina Pashevab4d562e32010-04-06 14:23:07 +00001274 if (length % dev->maxpacket == 0) {
1275 if (!(info->flags & FLAG_SEND_ZLP)) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001276 if (!(info->flags & FLAG_MULTI_PACKET)) {
1277 urb->transfer_buffer_length++;
1278 if (skb_tailroom(skb)) {
1279 skb->data[skb->len] = 0;
1280 __skb_put(skb, 1);
1281 }
Elina Pashevab4d562e32010-04-06 14:23:07 +00001282 }
1283 } else
1284 urb->transfer_flags |= URB_ZERO_PACKET;
Peter Korsgaard3e323f32007-06-27 08:48:15 +02001285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001287 spin_lock_irqsave(&dev->txq.lock, flags);
1288 retval = usb_autopm_get_interface_async(dev->intf);
1289 if (retval < 0) {
1290 spin_unlock_irqrestore(&dev->txq.lock, flags);
1291 goto drop;
1292 }
1293
1294#ifdef CONFIG_PM
1295 /* if this triggers the device is still a sleep */
1296 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1297 /* transmission will be done in resume */
1298 usb_anchor_urb(urb, &dev->deferred);
1299 /* no use to process more packets */
1300 netif_stop_queue(net);
Hemant Kumar39707c22012-10-25 18:17:54 +00001301 usb_put_urb(urb);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001302 spin_unlock_irqrestore(&dev->txq.lock, flags);
Joe Perches60b86752010-02-17 10:30:23 +00001303 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001304 goto deferred;
1305 }
1306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1309 case -EPIPE:
1310 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001311 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001312 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 default:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001315 usb_autopm_put_interface_async(dev->intf);
Joe Perchesa475f602010-02-17 10:30:24 +00001316 netif_dbg(dev, tx_err, dev->net,
1317 "tx: submit urb err %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 case 0:
1320 net->trans_start = jiffies;
Ming Lei5b6e9bc2012-04-26 11:33:46 +08001321 __usbnet_queue_skb(&dev->txq, skb, tx_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (dev->txq.qlen >= TX_QLEN (dev))
1323 netif_stop_queue (net);
1324 }
1325 spin_unlock_irqrestore (&dev->txq.lock, flags);
1326
1327 if (retval) {
Joe Perchesa475f602010-02-17 10:30:24 +00001328 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329drop:
Herbert Xu79638372009-06-29 16:53:28 +00001330 dev->net->stats.tx_dropped++;
Alexey Orishko073285f2010-11-29 23:23:27 +00001331not_drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (skb)
1333 dev_kfree_skb_any (skb);
1334 usb_free_urb (urb);
Joe Perchesa475f602010-02-17 10:30:24 +00001335 } else
1336 netif_dbg(dev, tx_queued, dev->net,
1337 "> tx, len %d, type 0x%x\n", length, skb->protocol);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001338#ifdef CONFIG_PM
1339deferred:
1340#endif
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001341 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001343EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Bjørn Mork85e87872012-09-02 22:26:18 +00001345static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
Ming Lei65841fd2012-06-19 21:15:53 +00001346{
1347 struct urb *urb;
1348 int i;
Bjørn Mork85e87872012-09-02 22:26:18 +00001349 int ret = 0;
Ming Lei65841fd2012-06-19 21:15:53 +00001350
1351 /* don't refill the queue all at once */
1352 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1353 urb = usb_alloc_urb(0, flags);
1354 if (urb != NULL) {
Bjørn Mork85e87872012-09-02 22:26:18 +00001355 ret = rx_submit(dev, urb, flags);
1356 if (ret)
1357 goto err;
1358 } else {
1359 ret = -ENOMEM;
1360 goto err;
Ming Lei65841fd2012-06-19 21:15:53 +00001361 }
1362 }
Bjørn Mork85e87872012-09-02 22:26:18 +00001363err:
1364 return ret;
Ming Lei65841fd2012-06-19 21:15:53 +00001365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/*-------------------------------------------------------------------------*/
1368
1369// tasklet (work deferred from completions, in_irq) or timer
1370
1371static void usbnet_bh (unsigned long param)
1372{
1373 struct usbnet *dev = (struct usbnet *) param;
1374 struct sk_buff *skb;
1375 struct skb_data *entry;
1376
1377 while ((skb = skb_dequeue (&dev->done))) {
1378 entry = (struct skb_data *) skb->cb;
1379 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001380 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 entry->state = rx_cleanup;
1382 rx_process (dev, skb);
1383 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001384 case tx_done:
1385 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 usb_free_urb (entry->urb);
1387 dev_kfree_skb (skb);
1388 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001389 default:
Joe Perches60b86752010-02-17 10:30:23 +00001390 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 }
1393
Bjørn Mork70c37bf2013-01-28 23:51:28 +00001394 /* restart RX again after disabling due to high error rate */
1395 clear_bit(EVENT_RX_KILL, &dev->flags);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 // waiting for all pending urbs to complete?
1398 if (dev->wait) {
1399 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1400 wake_up (dev->wait);
1401 }
1402
1403 // or are we maybe short a few urbs?
Joe Perches8e95a202009-12-03 07:58:21 +00001404 } else if (netif_running (dev->net) &&
1405 netif_device_present (dev->net) &&
Ming Lei4b49f582013-04-11 04:40:40 +00001406 netif_carrier_ok(dev->net) &&
Joe Perches8e95a202009-12-03 07:58:21 +00001407 !timer_pending (&dev->delay) &&
1408 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int temp = dev->rxq.qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Ming Lei65841fd2012-06-19 21:15:53 +00001411 if (temp < RX_QLEN(dev)) {
Bjørn Mork85e87872012-09-02 22:26:18 +00001412 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1413 return;
Joe Perchesa475f602010-02-17 10:30:24 +00001414 if (temp != dev->rxq.qlen)
1415 netif_dbg(dev, link, dev->net,
1416 "rxqlen %d --> %d\n",
1417 temp, dev->rxq.qlen);
Ming Lei65841fd2012-06-19 21:15:53 +00001418 if (dev->rxq.qlen < RX_QLEN(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 tasklet_schedule (&dev->bh);
1420 }
1421 if (dev->txq.qlen < TX_QLEN (dev))
1422 netif_wake_queue (dev->net);
1423 }
1424}
1425
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*-------------------------------------------------------------------------
1428 *
1429 * USB Device Driver support
1430 *
1431 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433// precondition: never called in_interrupt
1434
David Brownell38bde1d2005-08-31 09:52:45 -07001435void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct usbnet *dev;
1438 struct usb_device *xdev;
1439 struct net_device *net;
1440
1441 dev = usb_get_intfdata(intf);
1442 usb_set_intfdata(intf, NULL);
1443 if (!dev)
1444 return;
1445
1446 xdev = interface_to_usbdev (intf);
1447
Joe Perchesa475f602010-02-17 10:30:24 +00001448 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1449 intf->dev.driver->name,
1450 xdev->bus->bus_name, xdev->devpath,
1451 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 net = dev->net;
1454 unregister_netdev (net);
1455
Tejun Heo23f333a2010-12-12 16:45:14 +01001456 cancel_work_sync(&dev->kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Hemant Kumar39707c22012-10-25 18:17:54 +00001458 usb_scuttle_anchored_urbs(&dev->deferred);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (dev->driver_info->unbind)
1461 dev->driver_info->unbind (dev, intf);
1462
Paul Stewart68972ef2011-04-28 05:43:37 +00001463 usb_kill_urb(dev->interrupt);
1464 usb_free_urb(dev->interrupt);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 free_netdev(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
David Brownell38bde1d2005-08-31 09:52:45 -07001468EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Stephen Hemminger777baa42009-03-20 19:35:54 +00001470static const struct net_device_ops usbnet_netdev_ops = {
1471 .ndo_open = usbnet_open,
1472 .ndo_stop = usbnet_stop,
1473 .ndo_start_xmit = usbnet_start_xmit,
1474 .ndo_tx_timeout = usbnet_tx_timeout,
1475 .ndo_change_mtu = usbnet_change_mtu,
1476 .ndo_set_mac_address = eth_mac_addr,
1477 .ndo_validate_addr = eth_validate_addr,
1478};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480/*-------------------------------------------------------------------------*/
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482// precondition: never called in_interrupt
1483
Marcel Holtmann225794f2009-10-02 05:15:26 +00001484static struct device_type wlan_type = {
1485 .name = "wlan",
1486};
1487
1488static struct device_type wwan_type = {
1489 .name = "wwan",
1490};
1491
David Brownell38bde1d2005-08-31 09:52:45 -07001492int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1494{
1495 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001496 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct usb_host_interface *interface;
1498 struct driver_info *info;
1499 struct usb_device *xdev;
1500 int status;
David Brownell296c0242007-04-17 16:10:10 -07001501 const char *name;
Ming Leib0786b42010-11-01 07:11:54 -07001502 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1503
1504 /* usbnet already took usb runtime pm, so have to enable the feature
1505 * for usb interface, otherwise usb_autopm_get_interface may return
Alan Stern98f541c2013-05-01 12:13:54 -04001506 * failure if RUNTIME_PM is enabled.
Ming Leib0786b42010-11-01 07:11:54 -07001507 */
1508 if (!driver->supports_autosuspend) {
1509 driver->supports_autosuspend = 1;
1510 pm_runtime_enable(&udev->dev);
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
David Brownell296c0242007-04-17 16:10:10 -07001513 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 info = (struct driver_info *) prod->driver_info;
1515 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001516 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -ENODEV;
1518 }
1519 xdev = interface_to_usbdev (udev);
1520 interface = udev->cur_altsetting;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 status = -ENOMEM;
1523
1524 // set up our own records
1525 net = alloc_etherdev(sizeof(*dev));
Joe Perches41de8d42012-01-29 13:47:52 +00001526 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Ben Hutchings0dacca72010-07-02 21:49:02 -07001529 /* netdev_printk() needs this so do it as early as possible */
1530 SET_NETDEV_DEV(net, &udev->dev);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 dev = netdev_priv(net);
1533 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001534 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001536 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1538 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1539 skb_queue_head_init (&dev->rxq);
1540 skb_queue_head_init (&dev->txq);
1541 skb_queue_head_init (&dev->done);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +03001542 skb_queue_head_init(&dev->rxq_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 dev->bh.func = usbnet_bh;
1544 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001545 INIT_WORK (&dev->kevent, kevent);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001546 init_usb_anchor(&dev->deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 dev->delay.function = usbnet_bh;
1548 dev->delay.data = (unsigned long) dev;
1549 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001550 mutex_init (&dev->phy_mutex);
Dan Williams6eecdc52013-05-06 11:29:23 +00001551 mutex_init(&dev->interrupt_mutex);
1552 dev->interrupt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 dev->net = net;
1555 strcpy (net->name, "usb%d");
1556 memcpy (net->dev_addr, node_id, sizeof node_id);
1557
David Brownell2e55cc72005-08-31 09:53:10 -07001558 /* rx and tx sides can use different message sizes;
1559 * bind() should set rx_urb_size in that case.
1560 */
1561 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#if 0
1563// dma_supported() is deeply broken on almost all architectures
1564 // possible with some EHCI controllers
Yang Hongyang6a355282009-04-06 19:01:13 -07001565 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 net->features |= NETIF_F_HIGHDMA;
1567#endif
1568
Stephen Hemminger777baa42009-03-20 19:35:54 +00001569 net->netdev_ops = &usbnet_netdev_ops;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001570 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 net->ethtool_ops = &usbnet_ethtool_ops;
1572
1573 // allow device-specific bind/init procedures
1574 // NOTE net->name still not usable ...
1575 if (info->bind) {
1576 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001577 if (status < 0)
1578 goto out1;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 // heuristic: "usb%d" for links we know are two-host,
1581 // else "eth%d" when there's reasonable doubt. userspace
1582 // can rename the link if it knows better.
Joe Perches8e95a202009-12-03 07:58:21 +00001583 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
Arnd Bergmannc2613442011-04-01 20:12:02 -07001584 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1585 (net->dev_addr [0] & 0x02) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001587 /* WLAN devices should always be named "wlan%d" */
1588 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1589 strcpy(net->name, "wlan%d");
Marcel Holtmanne1e499e2009-10-02 05:15:25 +00001590 /* WWAN devices should always be named "wwan%d" */
1591 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1592 strcpy(net->name, "wwan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001593
Wei Shuai65091412013-01-21 06:00:31 +00001594 /* devices that cannot do ARP */
1595 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1596 net->flags |= IFF_NOARP;
1597
David Brownellf29fc252005-08-31 09:52:31 -07001598 /* maybe the remote can't receive an Ethernet MTU */
1599 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1600 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001601 } else if (!info->in || !info->out)
1602 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 else {
1604 dev->in = usb_rcvbulkpipe (xdev, info->in);
1605 dev->out = usb_sndbulkpipe (xdev, info->out);
1606 if (!(info->flags & FLAG_NO_SETINT))
1607 status = usb_set_interface (xdev,
1608 interface->desc.bInterfaceNumber,
1609 interface->desc.bAlternateSetting);
1610 else
1611 status = 0;
1612
1613 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001614 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 status = init_status (dev, udev);
1616 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001617 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
David Brownell2e55cc72005-08-31 09:53:10 -07001619 if (!dev->rx_urb_size)
1620 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001622
Marcel Holtmann225794f2009-10-02 05:15:26 +00001623 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1624 SET_NETDEV_DEVTYPE(net, &wlan_type);
1625 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1626 SET_NETDEV_DEVTYPE(net, &wwan_type);
1627
Ming Leia88c32a2013-07-25 13:47:53 +08001628 /* initialize max rx_qlen and tx_qlen */
1629 usbnet_update_max_qlen(dev);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 status = register_netdev (net);
1632 if (status)
tom.leiming@gmail.coma4723842012-04-29 22:51:03 +00001633 goto out4;
Joe Perchesa475f602010-02-17 10:30:24 +00001634 netif_info(dev, probe, dev->net,
1635 "register '%s' at usb-%s-%s, %s, %pM\n",
1636 udev->dev.driver->name,
1637 xdev->bus->bus_name, xdev->devpath,
1638 dev->driver_info->description,
1639 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 // ok, it's ready to go.
1642 usb_set_intfdata (udev, dev);
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 netif_device_attach (net);
1645
Ben Hutchings37e82732009-11-04 15:29:52 +00001646 if (dev->driver_info->flags & FLAG_LINK_INTR)
Ming Lei0162c552013-04-11 04:40:39 +00001647 usbnet_link_change(dev, 0, 0);
Ben Hutchings37e82732009-11-04 15:29:52 +00001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650
tom.leiming@gmail.coma4723842012-04-29 22:51:03 +00001651out4:
1652 usb_free_urb(dev->interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653out3:
1654 if (info->unbind)
1655 info->unbind (dev, udev);
1656out1:
1657 free_netdev(net);
1658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return status;
1660}
David Brownell38bde1d2005-08-31 09:52:45 -07001661EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663/*-------------------------------------------------------------------------*/
1664
Oliver Neukum36433122007-04-30 01:37:44 -07001665/*
1666 * suspend the whole driver as soon as the first interface is suspended
1667 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
David Brownell38bde1d2005-08-31 09:52:45 -07001670int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001673
Oliver Neukum36433122007-04-30 01:37:44 -07001674 if (!dev->suspend_count++) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001675 spin_lock_irq(&dev->txq.lock);
1676 /* don't autosuspend while transmitting */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001677 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
Ming Lei5eeb3132012-06-19 21:15:52 +00001678 dev->suspend_count--;
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001679 spin_unlock_irq(&dev->txq.lock);
1680 return -EBUSY;
1681 } else {
1682 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1683 spin_unlock_irq(&dev->txq.lock);
1684 }
Oliver Neukuma11a6542007-08-03 13:52:19 +02001685 /*
1686 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001687 * having everything error out.
1688 */
1689 netif_device_detach (dev->net);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001690 usbnet_terminate_urbs(dev);
Dan Williams6eecdc52013-05-06 11:29:23 +00001691 __usbnet_status_stop_force(dev);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001692
Oliver Neukuma11a6542007-08-03 13:52:19 +02001693 /*
1694 * reattach so runtime management can use and
1695 * wake the device
1696 */
1697 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return 0;
1700}
David Brownell38bde1d2005-08-31 09:52:45 -07001701EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
David Brownell38bde1d2005-08-31 09:52:45 -07001703int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct usbnet *dev = usb_get_intfdata(intf);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001706 struct sk_buff *skb;
1707 struct urb *res;
1708 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001710 if (!--dev->suspend_count) {
Dan Williams6eecdc52013-05-06 11:29:23 +00001711 /* resume interrupt URB if it was previously submitted */
1712 __usbnet_status_start_force(dev, GFP_NOIO);
Paul Stewart68972ef2011-04-28 05:43:37 +00001713
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001714 spin_lock_irq(&dev->txq.lock);
1715 while ((res = usb_get_from_anchor(&dev->deferred))) {
1716
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001717 skb = (struct sk_buff *)res->context;
1718 retval = usb_submit_urb(res, GFP_ATOMIC);
1719 if (retval < 0) {
1720 dev_kfree_skb_any(skb);
1721 usb_free_urb(res);
1722 usb_autopm_put_interface_async(dev->intf);
1723 } else {
1724 dev->net->trans_start = jiffies;
1725 __skb_queue_tail(&dev->txq, skb);
1726 }
1727 }
1728
1729 smp_mb();
1730 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1731 spin_unlock_irq(&dev->txq.lock);
Ming Lei75bd0cb2011-04-28 22:37:09 +00001732
1733 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
Ming Lei65841fd2012-06-19 21:15:53 +00001734 /* handle remote wakeup ASAP */
1735 if (!dev->wait &&
1736 netif_device_present(dev->net) &&
1737 !timer_pending(&dev->delay) &&
1738 !test_bit(EVENT_RX_HALT, &dev->flags))
Oliver Neukumab6f1482012-08-26 20:41:38 +00001739 rx_alloc_submit(dev, GFP_NOIO);
Ming Lei65841fd2012-06-19 21:15:53 +00001740
Ming Lei75bd0cb2011-04-28 22:37:09 +00001741 if (!(dev->txq.qlen >= TX_QLEN(dev)))
Alexey Orishko1aa9bc52012-03-14 04:00:24 +00001742 netif_tx_wake_all_queues(dev->net);
Ming Lei75bd0cb2011-04-28 22:37:09 +00001743 tasklet_schedule (&dev->bh);
1744 }
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001745 }
Oliver Neukum5d9d01a2012-10-11 02:50:10 +00001746
1747 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1748 usb_autopm_get_interface_no_resume(intf);
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751}
David Brownell38bde1d2005-08-31 09:52:45 -07001752EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Oliver Neukum5d9d01a2012-10-11 02:50:10 +00001754/*
1755 * Either a subdriver implements manage_power, then it is assumed to always
1756 * be ready to be suspended or it reports the readiness to be suspended
1757 * explicitly
1758 */
1759void usbnet_device_suggests_idle(struct usbnet *dev)
1760{
1761 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1762 dev->intf->needs_remote_wakeup = 1;
1763 usb_autopm_put_interface_async(dev->intf);
1764 }
1765}
1766EXPORT_SYMBOL(usbnet_device_suggests_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Oliver Neukum2dd7c8c2012-12-18 04:45:52 +00001768/*
1769 * For devices that can do without special commands
1770 */
1771int usbnet_manage_power(struct usbnet *dev, int on)
1772{
1773 dev->intf->needs_remote_wakeup = on;
1774 return 0;
1775}
1776EXPORT_SYMBOL(usbnet_manage_power);
1777
Ming Leiac649952013-04-11 04:40:30 +00001778void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1779{
1780 /* update link after link is reseted */
1781 if (link && !need_reset)
1782 netif_carrier_on(dev->net);
1783 else
1784 netif_carrier_off(dev->net);
1785
1786 if (need_reset && link)
1787 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
Ming Lei4b49f582013-04-11 04:40:40 +00001788 else
1789 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
Ming Leiac649952013-04-11 04:40:30 +00001790}
1791EXPORT_SYMBOL(usbnet_link_change);
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793/*-------------------------------------------------------------------------*/
Ming Lei0547fad2012-11-06 04:53:04 +00001794static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1795 u16 value, u16 index, void *data, u16 size)
Ming Lei877bd862012-10-24 19:46:54 +00001796{
1797 void *buf = NULL;
1798 int err = -ENOMEM;
1799
1800 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1801 " value=0x%04x index=0x%04x size=%d\n",
1802 cmd, reqtype, value, index, size);
1803
1804 if (data) {
1805 buf = kmalloc(size, GFP_KERNEL);
1806 if (!buf)
1807 goto out;
1808 }
1809
1810 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1811 cmd, reqtype, value, index, buf, size,
1812 USB_CTRL_GET_TIMEOUT);
1813 if (err > 0 && err <= size)
1814 memcpy(data, buf, err);
1815 kfree(buf);
1816out:
1817 return err;
1818}
Ming Lei877bd862012-10-24 19:46:54 +00001819
Ming Lei0547fad2012-11-06 04:53:04 +00001820static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1821 u16 value, u16 index, const void *data,
1822 u16 size)
Ming Lei877bd862012-10-24 19:46:54 +00001823{
1824 void *buf = NULL;
1825 int err = -ENOMEM;
1826
1827 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1828 " value=0x%04x index=0x%04x size=%d\n",
1829 cmd, reqtype, value, index, size);
1830
1831 if (data) {
1832 buf = kmemdup(data, size, GFP_KERNEL);
1833 if (!buf)
1834 goto out;
1835 }
1836
1837 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1838 cmd, reqtype, value, index, buf, size,
1839 USB_CTRL_SET_TIMEOUT);
1840 kfree(buf);
1841
1842out:
1843 return err;
1844}
Ming Lei0547fad2012-11-06 04:53:04 +00001845
1846/*
1847 * The function can't be called inside suspend/resume callback,
1848 * otherwise deadlock will be caused.
1849 */
1850int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1851 u16 value, u16 index, void *data, u16 size)
1852{
Ming Lei6f0a0982012-11-06 04:53:08 +00001853 int ret;
1854
1855 if (usb_autopm_get_interface(dev->intf) < 0)
1856 return -ENODEV;
1857 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1858 data, size);
1859 usb_autopm_put_interface(dev->intf);
1860 return ret;
Ming Lei0547fad2012-11-06 04:53:04 +00001861}
1862EXPORT_SYMBOL_GPL(usbnet_read_cmd);
1863
1864/*
1865 * The function can't be called inside suspend/resume callback,
1866 * otherwise deadlock will be caused.
1867 */
1868int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1869 u16 value, u16 index, const void *data, u16 size)
1870{
Ming Lei6f0a0982012-11-06 04:53:08 +00001871 int ret;
1872
1873 if (usb_autopm_get_interface(dev->intf) < 0)
1874 return -ENODEV;
1875 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1876 data, size);
1877 usb_autopm_put_interface(dev->intf);
1878 return ret;
Ming Lei0547fad2012-11-06 04:53:04 +00001879}
Ming Lei877bd862012-10-24 19:46:54 +00001880EXPORT_SYMBOL_GPL(usbnet_write_cmd);
1881
Ming Lei0547fad2012-11-06 04:53:04 +00001882/*
1883 * The function can be called inside suspend/resume callback safely
1884 * and should only be called by suspend/resume callback generally.
1885 */
1886int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1887 u16 value, u16 index, void *data, u16 size)
1888{
1889 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1890 data, size);
1891}
1892EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
1893
1894/*
1895 * The function can be called inside suspend/resume callback safely
1896 * and should only be called by suspend/resume callback generally.
1897 */
1898int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1899 u16 value, u16 index, const void *data,
1900 u16 size)
1901{
1902 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1903 data, size);
1904}
1905EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
1906
Ming Lei877bd862012-10-24 19:46:54 +00001907static void usbnet_async_cmd_cb(struct urb *urb)
1908{
1909 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
1910 int status = urb->status;
1911
1912 if (status < 0)
1913 dev_dbg(&urb->dev->dev, "%s failed with %d",
1914 __func__, status);
1915
1916 kfree(req);
1917 usb_free_urb(urb);
1918}
1919
Ming Lei0547fad2012-11-06 04:53:04 +00001920/*
1921 * The caller must make sure that device can't be put into suspend
1922 * state until the control URB completes.
1923 */
Ming Lei877bd862012-10-24 19:46:54 +00001924int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
1925 u16 value, u16 index, const void *data, u16 size)
1926{
1927 struct usb_ctrlrequest *req = NULL;
1928 struct urb *urb;
1929 int err = -ENOMEM;
1930 void *buf = NULL;
1931
1932 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1933 " value=0x%04x index=0x%04x size=%d\n",
1934 cmd, reqtype, value, index, size);
1935
1936 urb = usb_alloc_urb(0, GFP_ATOMIC);
1937 if (!urb) {
1938 netdev_err(dev->net, "Error allocating URB in"
1939 " %s!\n", __func__);
1940 goto fail;
1941 }
1942
1943 if (data) {
1944 buf = kmemdup(data, size, GFP_ATOMIC);
1945 if (!buf) {
1946 netdev_err(dev->net, "Error allocating buffer"
1947 " in %s!\n", __func__);
1948 goto fail_free;
1949 }
1950 }
1951
1952 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
Joe Perches38673c82013-02-03 17:28:11 +00001953 if (!req)
Ming Lei877bd862012-10-24 19:46:54 +00001954 goto fail_free_buf;
Ming Lei877bd862012-10-24 19:46:54 +00001955
1956 req->bRequestType = reqtype;
1957 req->bRequest = cmd;
1958 req->wValue = cpu_to_le16(value);
1959 req->wIndex = cpu_to_le16(index);
1960 req->wLength = cpu_to_le16(size);
1961
1962 usb_fill_control_urb(urb, dev->udev,
1963 usb_sndctrlpipe(dev->udev, 0),
1964 (void *)req, buf, size,
1965 usbnet_async_cmd_cb, req);
1966 urb->transfer_flags |= URB_FREE_BUFFER;
1967
1968 err = usb_submit_urb(urb, GFP_ATOMIC);
1969 if (err < 0) {
1970 netdev_err(dev->net, "Error submitting the control"
1971 " message: status=%d\n", err);
1972 goto fail_free;
1973 }
1974 return 0;
1975
1976fail_free_buf:
1977 kfree(buf);
1978fail_free:
1979 kfree(req);
1980 usb_free_urb(urb);
1981fail:
1982 return err;
1983
1984}
1985EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
1986/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
David Brownellf29fc252005-08-31 09:52:31 -07001988static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Thiago Farinac582a952011-04-17 17:49:21 -07001990 /* Compiler should optimize this out. */
1991 BUILD_BUG_ON(
1992 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Joe Perchesc7e12ea2012-07-12 19:33:07 +00001994 eth_random_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
David Brownellf29fc252005-08-31 09:52:31 -07001997module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
David Brownellf29fc252005-08-31 09:52:31 -07001999static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
David Brownellf29fc252005-08-31 09:52:31 -07002002module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
David Brownellf29fc252005-08-31 09:52:31 -07002004MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07002005MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07002006MODULE_LICENSE("GPL");