blob: 8494bb53ebdc9f33abee1d8e5a27f7d0dea3cc17 [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
tom.leiming@gmail.com24ead292012-06-11 15:19:44 +0000207 status = usb_submit_urb (urb, GFP_ATOMIC);
208 if (status != 0)
209 netif_err(dev, timer, dev->net,
210 "intr resubmit --> %d\n", status);
211}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213static int init_status (struct usbnet *dev, struct usb_interface *intf)
214{
215 char *buf = NULL;
216 unsigned pipe = 0;
217 unsigned maxp;
218 unsigned period;
219
220 if (!dev->driver_info->status)
221 return 0;
222
223 pipe = usb_rcvintpipe (dev->udev,
224 dev->status->desc.bEndpointAddress
225 & USB_ENDPOINT_NUMBER_MASK);
226 maxp = usb_maxpacket (dev->udev, pipe, 0);
227
228 /* avoid 1 msec chatter: min 8 msec poll rate */
229 period = max ((int) dev->status->desc.bInterval,
230 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
231
Christoph Lametere94b1762006-12-06 20:33:17 -0800232 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800234 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!dev->interrupt) {
236 kfree (buf);
237 return -ENOMEM;
238 } else {
239 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
240 buf, maxp, intr_complete, dev, period);
tom.leiming@gmail.com720f3d72012-04-29 22:51:02 +0000241 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 dev_dbg(&intf->dev,
243 "status ep%din, %d bytes period %d\n",
244 usb_pipeendpoint(pipe), maxp, period);
245 }
246 }
David Brownell18ab4582007-05-25 12:31:32 -0700247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Dan Williams6eecdc52013-05-06 11:29:23 +0000250/* Submit the interrupt URB if not previously submitted, increasing refcount */
251int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
252{
253 int ret = 0;
254
255 WARN_ON_ONCE(dev->interrupt == NULL);
256 if (dev->interrupt) {
257 mutex_lock(&dev->interrupt_mutex);
258
259 if (++dev->interrupt_count == 1)
260 ret = usb_submit_urb(dev->interrupt, mem_flags);
261
262 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
263 dev->interrupt_count);
264 mutex_unlock(&dev->interrupt_mutex);
265 }
266 return ret;
267}
268EXPORT_SYMBOL_GPL(usbnet_status_start);
269
270/* For resume; submit interrupt URB if previously submitted */
271static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
272{
273 int ret = 0;
274
275 mutex_lock(&dev->interrupt_mutex);
276 if (dev->interrupt_count) {
277 ret = usb_submit_urb(dev->interrupt, mem_flags);
278 dev_dbg(&dev->udev->dev,
279 "submitted interrupt URB for resume\n");
280 }
281 mutex_unlock(&dev->interrupt_mutex);
282 return ret;
283}
284
285/* Kill the interrupt URB if all submitters want it killed */
286void usbnet_status_stop(struct usbnet *dev)
287{
288 if (dev->interrupt) {
289 mutex_lock(&dev->interrupt_mutex);
290 WARN_ON(dev->interrupt_count == 0);
291
292 if (dev->interrupt_count && --dev->interrupt_count == 0)
293 usb_kill_urb(dev->interrupt);
294
295 dev_dbg(&dev->udev->dev,
296 "decremented interrupt URB count to %d\n",
297 dev->interrupt_count);
298 mutex_unlock(&dev->interrupt_mutex);
299 }
300}
301EXPORT_SYMBOL_GPL(usbnet_status_stop);
302
303/* For suspend; always kill interrupt URB */
304static void __usbnet_status_stop_force(struct usbnet *dev)
305{
306 if (dev->interrupt) {
307 mutex_lock(&dev->interrupt_mutex);
308 usb_kill_urb(dev->interrupt);
309 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
310 mutex_unlock(&dev->interrupt_mutex);
311 }
312}
313
David Brownell2e55cc72005-08-31 09:53:10 -0700314/* Passes this packet up the stack, updating its accounting.
315 * Some link protocols batch packets, so their rx_fixup paths
316 * can return clones as well as just modify the original skb.
317 */
318void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int status;
321
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300322 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
323 skb_queue_tail(&dev->rxq_pause, skb);
324 return;
325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 skb->protocol = eth_type_trans (skb, dev->net);
Herbert Xu79638372009-06-29 16:53:28 +0000328 dev->net->stats.rx_packets++;
329 dev->net->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Joe Perchesa475f602010-02-17 10:30:24 +0000331 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
332 skb->len + sizeof (struct ethhdr), skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 memset (skb->cb, 0, sizeof (struct skb_data));
Michael Rieschf9b491e2011-09-29 04:06:26 +0000334
335 if (skb_defer_rx_timestamp(skb))
336 return;
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 status = netif_rx (skb);
Joe Perchesa475f602010-02-17 10:30:24 +0000339 if (status != NET_RX_SUCCESS)
340 netif_dbg(dev, rx_err, dev->net,
341 "netif_rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
David Brownell2e55cc72005-08-31 09:53:10 -0700343EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Ming Leia88c32a2013-07-25 13:47:53 +0800345/* must be called if hard_mtu or rx_urb_size changed */
346void usbnet_update_max_qlen(struct usbnet *dev)
347{
348 enum usb_device_speed speed = dev->udev->speed;
349
350 switch (speed) {
351 case USB_SPEED_HIGH:
352 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
353 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
354 break;
Ming Lei452c4472013-07-25 13:47:54 +0800355 case USB_SPEED_SUPER:
356 /*
357 * Not take default 5ms qlen for super speed HC to
358 * save memory, and iperf tests show 2.5ms qlen can
359 * work well
360 */
361 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
362 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
363 break;
Ming Leia88c32a2013-07-25 13:47:53 +0800364 default:
365 dev->rx_qlen = dev->tx_qlen = 4;
366 }
367}
368EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*-------------------------------------------------------------------------
372 *
373 * Network Device Driver (peer link to "Host Device", from USB host)
374 *
375 *-------------------------------------------------------------------------*/
376
Stephen Hemminger777baa42009-03-20 19:35:54 +0000377int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700380 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400381 int old_hard_mtu = dev->hard_mtu;
382 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jamie Paintera99c1942006-07-27 14:17:28 -0400384 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700387 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EDOM;
389 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400390
391 dev->hard_mtu = net->mtu + net->hard_header_len;
392 if (dev->rx_urb_size == old_hard_mtu) {
393 dev->rx_urb_size = dev->hard_mtu;
394 if (dev->rx_urb_size > old_rx_urb_size)
395 usbnet_unlink_rx_urbs(dev);
396 }
397
Ming Leia88c32a2013-07-25 13:47:53 +0800398 /* max qlen depend on hard_mtu and rx_urb_size */
399 usbnet_update_max_qlen(dev);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000403EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800405/* The caller must hold list->lock */
406static void __usbnet_queue_skb(struct sk_buff_head *list,
407 struct sk_buff *newsk, enum skb_state state)
408{
409 struct skb_data *entry = (struct skb_data *) newsk->cb;
410
411 __skb_queue_tail(list, newsk);
412 entry->state = state;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*-------------------------------------------------------------------------*/
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
418 * completion callbacks. 2.5 should have fixed those bugs...
419 */
420
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800421static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
422 struct sk_buff_head *list, enum skb_state state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned long flags;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800425 enum skb_state old_state;
426 struct skb_data *entry = (struct skb_data *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
David S. Miller8728b832005-08-09 19:25:21 -0700428 spin_lock_irqsave(&list->lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800429 old_state = entry->state;
430 entry->state = state;
David S. Miller8728b832005-08-09 19:25:21 -0700431 __skb_unlink(skb, list);
432 spin_unlock(&list->lock);
433 spin_lock(&dev->done.lock);
434 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700436 tasklet_schedule(&dev->bh);
437 spin_unlock_irqrestore(&dev->done.lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800438 return old_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/* some work can't be done in tasklets, so we use keventd
442 *
443 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
444 * but tasklet_schedule() doesn't. hope the failure is rare.
445 */
David Brownell2e55cc72005-08-31 09:53:10 -0700446void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 set_bit (work, &dev->flags);
Steve Glendinning95320212012-11-08 06:26:21 +0000449 if (!schedule_work (&dev->kevent)) {
450 if (net_ratelimit())
451 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
452 } else {
Joe Perches60b86752010-02-17 10:30:23 +0000453 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
Steve Glendinning95320212012-11-08 06:26:21 +0000454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
David Brownell2e55cc72005-08-31 09:53:10 -0700456EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458/*-------------------------------------------------------------------------*/
459
David Howells7d12e782006-10-05 14:55:46 +0100460static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
David S. Millerdacb3972010-08-10 02:50:55 -0700462static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct sk_buff *skb;
465 struct skb_data *entry;
466 int retval = 0;
467 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700468 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Bjørn Mork70c37bf2013-01-28 23:51:28 +0000470 /* prevent rx skb allocation when error ratio is high */
471 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
472 usb_free_urb(urb);
473 return -ENOLINK;
474 }
475
Eric Dumazet7bdd4022012-03-14 06:56:25 +0000476 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
477 if (!skb) {
Joe Perchesa475f602010-02-17 10:30:24 +0000478 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
David Brownell2e55cc72005-08-31 09:53:10 -0700479 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 usb_free_urb (urb);
David S. Millerdacb3972010-08-10 02:50:55 -0700481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 entry = (struct skb_data *) skb->cb;
485 entry->urb = urb;
486 entry->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 entry->length = 0;
488
489 usb_fill_bulk_urb (urb, dev->udev, dev->in,
490 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 spin_lock_irqsave (&dev->rxq.lock, lockflags);
493
Joe Perches8e95a202009-12-03 07:58:21 +0000494 if (netif_running (dev->net) &&
495 netif_device_present (dev->net) &&
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800496 !test_bit (EVENT_RX_HALT, &dev->flags) &&
497 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700498 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700500 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700503 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case -ENODEV:
Joe Perchesa475f602010-02-17 10:30:24 +0000506 netif_dbg(dev, ifdown, dev->net, "device gone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 netif_device_detach (dev->net);
508 break;
David S. Millerdacb3972010-08-10 02:50:55 -0700509 case -EHOSTUNREACH:
510 retval = -ENOLINK;
511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000513 netif_dbg(dev, rx_err, dev->net,
514 "rx submit, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 tasklet_schedule (&dev->bh);
516 break;
517 case 0:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800518 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000521 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 retval = -ENOLINK;
523 }
524 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
525 if (retval) {
526 dev_kfree_skb_any (skb);
527 usb_free_urb (urb);
528 }
David S. Millerdacb3972010-08-10 02:50:55 -0700529 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532
533/*-------------------------------------------------------------------------*/
534
535static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
536{
Joe Perches8e95a202009-12-03 07:58:21 +0000537 if (dev->driver_info->rx_fixup &&
Andrzej Zaborowski7a635ea2011-03-28 12:56:33 +0000538 !dev->driver_info->rx_fixup (dev, skb)) {
539 /* With RX_ASSEMBLE, rx_fixup() must update counters */
540 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
541 dev->net->stats.rx_errors++;
542 goto done;
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 // else network stack removes extra byte if we forced a short packet
545
Alexey Orishko073285f2010-11-29 23:23:27 +0000546 if (skb->len) {
547 /* all data was already cloned from skb inside the driver */
548 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
549 dev_kfree_skb_any(skb);
550 else
551 usbnet_skb_return(dev, skb);
552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Alexey Orishko073285f2010-11-29 23:23:27 +0000554
555 netif_dbg(dev, rx_err, dev->net, "drop\n");
Alexey Orishko073285f2010-11-29 23:23:27 +0000556 dev->net->stats.rx_errors++;
Andrzej Zaborowski7a635ea2011-03-28 12:56:33 +0000557done:
Alexey Orishko073285f2010-11-29 23:23:27 +0000558 skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*-------------------------------------------------------------------------*/
562
David Howells7d12e782006-10-05 14:55:46 +0100563static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct sk_buff *skb = (struct sk_buff *) urb->context;
566 struct skb_data *entry = (struct skb_data *) skb->cb;
567 struct usbnet *dev = entry->dev;
568 int urb_status = urb->status;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800569 enum skb_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 skb_put (skb, urb->actual_length);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800572 state = rx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 entry->urb = NULL;
574
575 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700576 /* success */
577 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700578 if (skb->len < dev->net->hard_header_len) {
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800579 state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000580 dev->net->stats.rx_errors++;
581 dev->net->stats.rx_length_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000582 netif_dbg(dev, rx_err, dev->net,
583 "rx length %d\n", skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 break;
586
David Brownell18ab4582007-05-25 12:31:32 -0700587 /* stalls need manual reset. this is rare ... except that
588 * when going through USB 2.0 TTs, unplug appears this way.
Jean Delvare3ac49a12009-06-04 16:20:28 +0200589 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
David Brownell18ab4582007-05-25 12:31:32 -0700590 * storm, recovering as needed.
591 */
592 case -EPIPE:
Herbert Xu79638372009-06-29 16:53:28 +0000593 dev->net->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700594 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 // FALLTHROUGH
596
David Brownell18ab4582007-05-25 12:31:32 -0700597 /* software-driven interface shutdown */
598 case -ECONNRESET: /* async unlink */
599 case -ESHUTDOWN: /* hardware gone */
Joe Perchesa475f602010-02-17 10:30:24 +0000600 netif_dbg(dev, ifdown, dev->net,
601 "rx shutdown, code %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto block;
603
David Brownell18ab4582007-05-25 12:31:32 -0700604 /* we get controller i/o faults during khubd disconnect() delays.
605 * throttle down resubmits, to avoid log floods; just temporarily,
606 * so we still recover when the fault isn't a khubd delay.
607 */
608 case -EPROTO:
609 case -ETIME:
610 case -EILSEQ:
Herbert Xu79638372009-06-29 16:53:28 +0000611 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!timer_pending (&dev->delay)) {
613 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +0000614 netif_dbg(dev, link, dev->net,
615 "rx throttle %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617block:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800618 state = rx_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 entry->urb = urb;
620 urb = NULL;
621 break;
622
David Brownell18ab4582007-05-25 12:31:32 -0700623 /* data overrun ... flush fifo? */
624 case -EOVERFLOW:
Herbert Xu79638372009-06-29 16:53:28 +0000625 dev->net->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800627
David Brownell18ab4582007-05-25 12:31:32 -0700628 default:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800629 state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000630 dev->net->stats.rx_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000631 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
633 }
634
Bjørn Mork70c37bf2013-01-28 23:51:28 +0000635 /* stop rx if packet error rate is high */
636 if (++dev->pkt_cnt > 30) {
637 dev->pkt_cnt = 0;
638 dev->pkt_err = 0;
639 } else {
640 if (state == rx_cleanup)
641 dev->pkt_err++;
642 if (dev->pkt_err > 20)
643 set_bit(EVENT_RX_KILL, &dev->flags);
644 }
645
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800646 state = defer_bh(dev, skb, &dev->rxq, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (urb) {
Joe Perches8e95a202009-12-03 07:58:21 +0000649 if (netif_running (dev->net) &&
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800650 !test_bit (EVENT_RX_HALT, &dev->flags) &&
651 state != unlink_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 rx_submit (dev, urb, GFP_ATOMIC);
Oliver Neukum8a783352012-03-03 18:45:07 +0100653 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return;
655 }
656 usb_free_urb (urb);
657 }
Joe Perchesa475f602010-02-17 10:30:24 +0000658 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*-------------------------------------------------------------------------*/
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300662void usbnet_pause_rx(struct usbnet *dev)
663{
664 set_bit(EVENT_RX_PAUSED, &dev->flags);
665
Joe Perchesa475f602010-02-17 10:30:24 +0000666 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300667}
668EXPORT_SYMBOL_GPL(usbnet_pause_rx);
669
670void usbnet_resume_rx(struct usbnet *dev)
671{
672 struct sk_buff *skb;
673 int num = 0;
674
675 clear_bit(EVENT_RX_PAUSED, &dev->flags);
676
677 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
678 usbnet_skb_return(dev, skb);
679 num++;
680 }
681
682 tasklet_schedule(&dev->bh);
683
Joe Perchesa475f602010-02-17 10:30:24 +0000684 netif_dbg(dev, rx_status, dev->net,
685 "paused rx queue disabled, %d skbs requeued\n", num);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300686}
687EXPORT_SYMBOL_GPL(usbnet_resume_rx);
688
689void usbnet_purge_paused_rxq(struct usbnet *dev)
690{
691 skb_queue_purge(&dev->rxq_pause);
692}
693EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
694
695/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697// unlink pending rx/tx; completion handlers do all other cleanup
698
699static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
700{
701 unsigned long flags;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800702 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int count = 0;
704
705 spin_lock_irqsave (&q->lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800706 while (!skb_queue_empty(q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct skb_data *entry;
708 struct urb *urb;
709 int retval;
710
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800711 skb_queue_walk(q, skb) {
712 entry = (struct skb_data *) skb->cb;
713 if (entry->state != unlink_start)
714 goto found;
715 }
716 break;
717found:
718 entry->state = unlink_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
tom.leiming@gmail.com0956a8c2012-03-22 03:22:18 +0000721 /*
722 * Get reference count of the URB to avoid it to be
723 * freed during usb_unlink_urb, which may trigger
724 * use-after-free problem inside usb_unlink_urb since
725 * usb_unlink_urb is always racing with .complete
726 * handler(include defer_bh).
727 */
728 usb_get_urb(urb);
Sebastian Siewior4231d472012-03-07 10:19:28 +0000729 spin_unlock_irqrestore(&q->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 // during some PM-driven resume scenarios,
731 // these (async) unlinks complete immediately
732 retval = usb_unlink_urb (urb);
733 if (retval != -EINPROGRESS && retval != 0)
Joe Perches60b86752010-02-17 10:30:23 +0000734 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 else
736 count++;
tom.leiming@gmail.com0956a8c2012-03-22 03:22:18 +0000737 usb_put_urb(urb);
Sebastian Siewior4231d472012-03-07 10:19:28 +0000738 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 spin_unlock_irqrestore (&q->lock, flags);
741 return count;
742}
743
Jamie Paintera99c1942006-07-27 14:17:28 -0400744// Flush all pending rx urbs
745// minidrivers may need to do this when the MTU changes
746
747void usbnet_unlink_rx_urbs(struct usbnet *dev)
748{
749 if (netif_running(dev->net)) {
750 (void) unlink_urbs (dev, &dev->rxq);
751 tasklet_schedule(&dev->bh);
752 }
753}
754EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756/*-------------------------------------------------------------------------*/
757
758// precondition: never called in_interrupt
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800759static void usbnet_terminate_urbs(struct usbnet *dev)
760{
761 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
762 DECLARE_WAITQUEUE(wait, current);
763 int temp;
764
765 /* ensure there are no more active urbs */
766 add_wait_queue(&unlink_wakeup, &wait);
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 dev->wait = &unlink_wakeup;
769 temp = unlink_urbs(dev, &dev->txq) +
770 unlink_urbs(dev, &dev->rxq);
771
772 /* maybe wait for deletions to finish. */
773 while (!skb_queue_empty(&dev->rxq)
774 && !skb_queue_empty(&dev->txq)
775 && !skb_queue_empty(&dev->done)) {
Kulikov Vasiliy66cc42a42010-07-25 22:25:42 +0000776 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800777 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perchesa475f602010-02-17 10:30:24 +0000778 netif_dbg(dev, ifdown, dev->net,
779 "waited for %d urb completions\n", temp);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800780 }
781 set_current_state(TASK_RUNNING);
782 dev->wait = NULL;
783 remove_wait_queue(&unlink_wakeup, &wait);
784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Stephen Hemminger777baa42009-03-20 19:35:54 +0000786int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct usbnet *dev = netdev_priv(net);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300789 struct driver_info *info = dev->driver_info;
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300790 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Ming Lei75bd0cb2011-04-28 22:37:09 +0000792 clear_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 netif_stop_queue (net);
794
Joe Perchesa475f602010-02-17 10:30:24 +0000795 netif_info(dev, ifdown, dev->net,
Ben Hutchings8ccef432010-06-08 08:20:59 +0000796 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
Joe Perchesa475f602010-02-17 10:30:24 +0000797 net->stats.rx_packets, net->stats.tx_packets,
798 net->stats.rx_errors, net->stats.tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300800 /* allow minidriver to stop correctly (wireless devices to turn off
801 * radio etc) */
802 if (info->stop) {
803 retval = info->stop(dev);
Joe Perchesa475f602010-02-17 10:30:24 +0000804 if (retval < 0)
805 netif_info(dev, ifdown, dev->net,
806 "stop fail (%d) usbnet usb-%s-%s, %s\n",
807 retval,
808 dev->udev->bus->bus_name, dev->udev->devpath,
809 info->description);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300810 }
811
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800812 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
813 usbnet_terminate_urbs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Dan Williams6eecdc52013-05-06 11:29:23 +0000815 usbnet_status_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300817 usbnet_purge_paused_rxq(dev);
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* deferred work (task, timer, softirq) must also stop.
820 * can't flush_scheduled_work() until we drop rtnl (later),
821 * else workers could deadlock; so make workers a NOP.
822 */
823 dev->flags = 0;
824 del_timer_sync (&dev->delay);
825 tasklet_kill (&dev->bh);
Oliver Neukuma1c088e2012-12-18 04:45:29 +0000826 if (info->manage_power &&
827 !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags))
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800828 info->manage_power(dev, 0);
829 else
830 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 return 0;
833}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000834EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836/*-------------------------------------------------------------------------*/
837
838// posts reads, and enables write queuing
839
840// precondition: never called in_interrupt
841
Stephen Hemminger777baa42009-03-20 19:35:54 +0000842int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200845 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct driver_info *info = dev->driver_info;
847
Oliver Neukuma11a6542007-08-03 13:52:19 +0200848 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000849 netif_info(dev, ifup, dev->net,
850 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
851 retval,
852 dev->udev->bus->bus_name,
853 dev->udev->devpath,
854 info->description);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200855 goto done_nopm;
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 // put into "known safe" state
859 if (info->reset && (retval = info->reset (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000860 netif_info(dev, ifup, dev->net,
861 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
862 retval,
863 dev->udev->bus->bus_name,
864 dev->udev->devpath,
865 info->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto done;
867 }
868
Ming Leia88c32a2013-07-25 13:47:53 +0800869 /* hard_mtu or rx_urb_size may change in reset() */
870 usbnet_update_max_qlen(dev);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 // insist peer be connected
873 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000874 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto done;
876 }
877
878 /* start any status interrupt transfer */
879 if (dev->interrupt) {
Dan Williams6eecdc52013-05-06 11:29:23 +0000880 retval = usbnet_status_start(dev, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (retval < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000882 netif_err(dev, ifup, dev->net,
883 "intr submit %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 goto done;
885 }
886 }
887
Paul Stewart68972ef2011-04-28 05:43:37 +0000888 set_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 netif_start_queue (net);
Joe Perchesa475f602010-02-17 10:30:24 +0000890 netif_info(dev, ifup, dev->net,
891 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
892 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
893 dev->net->mtu,
894 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
895 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
896 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
897 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
898 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
899 "simple");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Bjørn Mork70c37bf2013-01-28 23:51:28 +0000901 /* reset rx error state */
902 dev->pkt_cnt = 0;
903 dev->pkt_err = 0;
904 clear_bit(EVENT_RX_KILL, &dev->flags);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 // delay posting reads until we're fully open
907 tasklet_schedule (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800908 if (info->manage_power) {
909 retval = info->manage_power(dev, 1);
Oliver Neukuma1c088e2012-12-18 04:45:29 +0000910 if (retval < 0) {
911 retval = 0;
912 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
913 } else {
914 usb_autopm_put_interface(dev->intf);
915 }
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800916 }
Oliver Neukuma11a6542007-08-03 13:52:19 +0200917 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200919 usb_autopm_put_interface(dev->intf);
920done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return retval;
922}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000923EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925/*-------------------------------------------------------------------------*/
926
David Brownell2e55cc72005-08-31 09:53:10 -0700927/* ethtool methods; minidrivers may need to add some more, but
928 * they'll probably want to use this base set.
929 */
930
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200931int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
932{
933 struct usbnet *dev = netdev_priv(net);
934
935 if (!dev->mii.mdio_read)
936 return -EOPNOTSUPP;
937
938 return mii_ethtool_gset(&dev->mii, cmd);
939}
940EXPORT_SYMBOL_GPL(usbnet_get_settings);
941
942int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
943{
944 struct usbnet *dev = netdev_priv(net);
945 int retval;
946
947 if (!dev->mii.mdio_write)
948 return -EOPNOTSUPP;
949
950 retval = mii_ethtool_sset(&dev->mii, cmd);
951
952 /* link speed/duplex might have changed */
953 if (dev->driver_info->link_reset)
954 dev->driver_info->link_reset(dev);
955
Ming Leia88c32a2013-07-25 13:47:53 +0800956 /* hard_mtu or rx_urb_size may change in link_reset() */
957 usbnet_update_max_qlen(dev);
958
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200959 return retval;
960
961}
962EXPORT_SYMBOL_GPL(usbnet_set_settings);
963
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200964u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct usbnet *dev = netdev_priv(net);
967
David Brownellf29fc252005-08-31 09:52:31 -0700968 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (dev->driver_info->check_connect)
970 return dev->driver_info->check_connect (dev) == 0;
971
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200972 /* if the device has mii operations, use those */
973 if (dev->mii.mdio_read)
974 return mii_link_ok(&dev->mii);
975
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800976 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
977 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200979EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
David Brownell18ee91fa2006-11-02 12:29:12 -0800981int usbnet_nway_reset(struct net_device *net)
982{
983 struct usbnet *dev = netdev_priv(net);
984
985 if (!dev->mii.mdio_write)
986 return -EOPNOTSUPP;
987
988 return mii_nway_restart(&dev->mii);
989}
990EXPORT_SYMBOL_GPL(usbnet_nway_reset);
991
David Brownell18ee91fa2006-11-02 12:29:12 -0800992void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
993{
994 struct usbnet *dev = netdev_priv(net);
995
Phil Sutter86a2f412012-06-14 01:18:42 +0000996 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
997 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
998 strlcpy (info->fw_version, dev->driver_info->description,
David Brownell18ee91fa2006-11-02 12:29:12 -0800999 sizeof info->fw_version);
1000 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1001}
1002EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1003
David Brownell2e55cc72005-08-31 09:53:10 -07001004u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct usbnet *dev = netdev_priv(net);
1007
1008 return dev->msg_enable;
1009}
David Brownell2e55cc72005-08-31 09:53:10 -07001010EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David Brownell2e55cc72005-08-31 09:53:10 -07001012void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct usbnet *dev = netdev_priv(net);
1015
1016 dev->msg_enable = level;
1017}
David Brownell2e55cc72005-08-31 09:53:10 -07001018EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
David Brownell2e55cc72005-08-31 09:53:10 -07001020/* drivers may override default ethtool_ops in their bind() routine */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001021static const struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +02001022 .get_settings = usbnet_get_settings,
1023 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -07001024 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +02001025 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -08001026 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -07001027 .get_msglevel = usbnet_get_msglevel,
1028 .set_msglevel = usbnet_set_msglevel,
Richard Cochran123edb12012-04-03 22:59:41 +00001029 .get_ts_info = ethtool_op_get_ts_info,
David Brownell2e55cc72005-08-31 09:53:10 -07001030};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032/*-------------------------------------------------------------------------*/
1033
Ming Lei4b49f582013-04-11 04:40:40 +00001034static void __handle_link_change(struct usbnet *dev)
1035{
1036 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1037 return;
1038
1039 if (!netif_carrier_ok(dev->net)) {
1040 /* kill URBs for reading packets to save bus bandwidth */
1041 unlink_urbs(dev, &dev->rxq);
1042
1043 /*
1044 * tx_timeout will unlink URBs for sending packets and
1045 * tx queue is stopped by netcore after link becomes off
1046 */
1047 } else {
1048 /* submitting URBs for reading packets */
1049 tasklet_schedule(&dev->bh);
1050 }
1051
Ming Leia88c32a2013-07-25 13:47:53 +08001052 /* hard_mtu or rx_urb_size may change during link change */
1053 usbnet_update_max_qlen(dev);
1054
Ming Lei4b49f582013-04-11 04:40:40 +00001055 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058/* work that cannot be done in interrupt context uses keventd.
1059 *
1060 * NOTE: with 2.5 we could do more of this using completion callbacks,
1061 * especially now that control transfers can be queued.
1062 */
1063static void
David Howellsc4028952006-11-22 14:57:56 +00001064kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
David Howellsc4028952006-11-22 14:57:56 +00001066 struct usbnet *dev =
1067 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int status;
1069
1070 /* usb_clear_halt() needs a thread context */
1071 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1072 unlink_urbs (dev, &dev->txq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001073 status = usb_autopm_get_interface(dev->intf);
1074 if (status < 0)
1075 goto fail_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 status = usb_clear_halt (dev->udev, dev->out);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001077 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +00001078 if (status < 0 &&
1079 status != -EPIPE &&
1080 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (netif_msg_tx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001082fail_pipe:
Joe Perches60b86752010-02-17 10:30:23 +00001083 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1084 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 } else {
1086 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -07001087 if (status != -ESHUTDOWN)
1088 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 }
1091 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1092 unlink_urbs (dev, &dev->rxq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001093 status = usb_autopm_get_interface(dev->intf);
1094 if (status < 0)
1095 goto fail_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 status = usb_clear_halt (dev->udev, dev->in);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001097 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +00001098 if (status < 0 &&
1099 status != -EPIPE &&
1100 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (netif_msg_rx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001102fail_halt:
Joe Perches60b86752010-02-17 10:30:23 +00001103 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1104 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 } else {
1106 clear_bit (EVENT_RX_HALT, &dev->flags);
1107 tasklet_schedule (&dev->bh);
1108 }
1109 }
1110
1111 /* tasklet could resubmit itself forever if memory is tight */
1112 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1113 struct urb *urb = NULL;
David S. Millerdacb3972010-08-10 02:50:55 -07001114 int resched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (netif_running (dev->net))
1117 urb = usb_alloc_urb (0, GFP_KERNEL);
1118 else
1119 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1120 if (urb != NULL) {
1121 clear_bit (EVENT_RX_MEMORY, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001122 status = usb_autopm_get_interface(dev->intf);
Jesper Juhlab607072011-02-10 10:58:45 +00001123 if (status < 0) {
1124 usb_free_urb(urb);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001125 goto fail_lowmem;
Jesper Juhlab607072011-02-10 10:58:45 +00001126 }
David S. Millerdacb3972010-08-10 02:50:55 -07001127 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1128 resched = 0;
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001129 usb_autopm_put_interface(dev->intf);
1130fail_lowmem:
David S. Millerdacb3972010-08-10 02:50:55 -07001131 if (resched)
1132 tasklet_schedule (&dev->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 }
1135
David Hollis7ea13c92005-04-22 15:07:02 -07001136 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -08001137 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -07001138 int retval = 0;
1139
1140 clear_bit (EVENT_LINK_RESET, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001141 status = usb_autopm_get_interface(dev->intf);
1142 if (status < 0)
1143 goto skip_reset;
David Hollis7ea13c92005-04-22 15:07:02 -07001144 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001145 usb_autopm_put_interface(dev->intf);
1146skip_reset:
Joe Perches60b86752010-02-17 10:30:23 +00001147 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1148 retval,
1149 dev->udev->bus->bus_name,
1150 dev->udev->devpath,
1151 info->description);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001152 } else {
1153 usb_autopm_put_interface(dev->intf);
David Hollis7ea13c92005-04-22 15:07:02 -07001154 }
Ming Lei4b49f582013-04-11 04:40:40 +00001155
1156 /* handle link change from link resetting */
1157 __handle_link_change(dev);
David Hollis7ea13c92005-04-22 15:07:02 -07001158 }
1159
Ming Lei4b49f582013-04-11 04:40:40 +00001160 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1161 __handle_link_change(dev);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (dev->flags)
Joe Perches60b86752010-02-17 10:30:23 +00001164 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167/*-------------------------------------------------------------------------*/
1168
David Howells7d12e782006-10-05 14:55:46 +01001169static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct sk_buff *skb = (struct sk_buff *) urb->context;
1172 struct skb_data *entry = (struct skb_data *) skb->cb;
1173 struct usbnet *dev = entry->dev;
1174
1175 if (urb->status == 0) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001176 if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
1177 dev->net->stats.tx_packets++;
Herbert Xu79638372009-06-29 16:53:28 +00001178 dev->net->stats.tx_bytes += entry->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else {
Herbert Xu79638372009-06-29 16:53:28 +00001180 dev->net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 switch (urb->status) {
1183 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -07001184 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186
1187 /* software-driven interface shutdown */
1188 case -ECONNRESET: // async unlink
1189 case -ESHUTDOWN: // hardware gone
1190 break;
1191
1192 // like rx, tx gets controller i/o faults during khubd delays
1193 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -07001194 case -EPROTO:
1195 case -ETIME:
1196 case -EILSEQ:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001197 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (!timer_pending (&dev->delay)) {
1199 mod_timer (&dev->delay,
1200 jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +00001201 netif_dbg(dev, link, dev->net,
1202 "tx throttle %d\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204 netif_stop_queue (dev->net);
1205 break;
1206 default:
Joe Perchesa475f602010-02-17 10:30:24 +00001207 netif_dbg(dev, tx_err, dev->net,
1208 "tx err %d\n", entry->urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
1210 }
1211 }
1212
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001213 usb_autopm_put_interface_async(dev->intf);
Ming Lei5b6e9bc2012-04-26 11:33:46 +08001214 (void) defer_bh(dev, skb, &dev->txq, tx_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217/*-------------------------------------------------------------------------*/
1218
Stephen Hemminger777baa42009-03-20 19:35:54 +00001219void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct usbnet *dev = netdev_priv(net);
1222
1223 unlink_urbs (dev, &dev->txq);
1224 tasklet_schedule (&dev->bh);
1225
1226 // FIXME: device recovery -- reset?
1227}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001228EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230/*-------------------------------------------------------------------------*/
1231
Ming Lei638c5112013-08-08 21:48:24 +08001232static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1233{
1234 unsigned num_sgs, total_len = 0;
1235 int i, s = 0;
1236
1237 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1238 if (num_sgs == 1)
1239 return 0;
1240
Ming Lei60e453a2013-09-23 20:59:35 +08001241 /* reserve one for zero packet */
1242 urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
1243 GFP_ATOMIC);
Ming Lei638c5112013-08-08 21:48:24 +08001244 if (!urb->sg)
1245 return -ENOMEM;
1246
1247 urb->num_sgs = num_sgs;
1248 sg_init_table(urb->sg, urb->num_sgs);
1249
1250 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1251 total_len += skb_headlen(skb);
1252
1253 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1254 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1255
1256 total_len += skb_frag_size(f);
1257 sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1258 f->page_offset);
1259 }
1260 urb->transfer_buffer_length = total_len;
1261
1262 return 1;
1263}
1264
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001265netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1266 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct usbnet *dev = netdev_priv(net);
1269 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct urb *urb = NULL;
1271 struct skb_data *entry;
1272 struct driver_info *info = dev->driver_info;
1273 unsigned long flags;
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001274 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Konstantin Khlebnikov23ba0792011-11-07 05:54:58 +00001276 if (skb)
1277 skb_tx_timestamp(skb);
Michael Rieschf9b491e2011-09-29 04:06:26 +00001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 // some devices want funky USB-level framing, for
1280 // win32 driver (usually) and/or hardware quirks
1281 if (info->tx_fixup) {
1282 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1283 if (!skb) {
Bjørn Morkbf414b32013-01-31 08:36:05 +00001284 /* packet collected; minidriver waiting for more */
1285 if (info->flags & FLAG_MULTI_PACKET)
Alexey Orishko073285f2010-11-29 23:23:27 +00001286 goto not_drop;
Bjørn Morkbf414b32013-01-31 08:36:05 +00001287 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1288 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001293 netif_dbg(dev, tx_err, dev->net, "no urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto drop;
1295 }
1296
1297 entry = (struct skb_data *) skb->cb;
1298 entry->urb = urb;
1299 entry->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1302 skb->data, skb->len, tx_complete, skb);
Ming Lei638c5112013-08-08 21:48:24 +08001303 if (dev->can_dma_sg) {
1304 if (build_dma_sg(skb, urb) < 0)
1305 goto drop;
1306 }
Ming Lei60e453a2013-09-23 20:59:35 +08001307 length = urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /* don't assume the hardware handles USB_ZERO_PACKET
1310 * NOTE: strictly conforming cdc-ether devices should expect
1311 * the ZLP here, but ignore the one-byte packet.
Alexey Orishko073285f2010-11-29 23:23:27 +00001312 * NOTE2: CDC NCM specification is different from CDC ECM when
1313 * handling ZLP/short packets, so cdc_ncm driver will make short
1314 * packet itself if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
Elina Pashevab4d562e32010-04-06 14:23:07 +00001316 if (length % dev->maxpacket == 0) {
1317 if (!(info->flags & FLAG_SEND_ZLP)) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001318 if (!(info->flags & FLAG_MULTI_PACKET)) {
Ming Lei60e453a2013-09-23 20:59:35 +08001319 length++;
1320 if (skb_tailroom(skb) && !urb->num_sgs) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001321 skb->data[skb->len] = 0;
1322 __skb_put(skb, 1);
Ming Lei60e453a2013-09-23 20:59:35 +08001323 } else if (urb->num_sgs)
1324 sg_set_buf(&urb->sg[urb->num_sgs++],
1325 dev->padding_pkt, 1);
Elina Pashevab4d562e32010-04-06 14:23:07 +00001326 }
1327 } else
1328 urb->transfer_flags |= URB_ZERO_PACKET;
Peter Korsgaard3e323f32007-06-27 08:48:15 +02001329 }
Ming Lei60e453a2013-09-23 20:59:35 +08001330 entry->length = urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001332 spin_lock_irqsave(&dev->txq.lock, flags);
1333 retval = usb_autopm_get_interface_async(dev->intf);
1334 if (retval < 0) {
1335 spin_unlock_irqrestore(&dev->txq.lock, flags);
1336 goto drop;
1337 }
1338
1339#ifdef CONFIG_PM
1340 /* if this triggers the device is still a sleep */
1341 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1342 /* transmission will be done in resume */
1343 usb_anchor_urb(urb, &dev->deferred);
1344 /* no use to process more packets */
1345 netif_stop_queue(net);
Hemant Kumar39707c22012-10-25 18:17:54 +00001346 usb_put_urb(urb);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001347 spin_unlock_irqrestore(&dev->txq.lock, flags);
Joe Perches60b86752010-02-17 10:30:23 +00001348 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001349 goto deferred;
1350 }
1351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1354 case -EPIPE:
1355 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001356 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001357 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 break;
1359 default:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001360 usb_autopm_put_interface_async(dev->intf);
Joe Perchesa475f602010-02-17 10:30:24 +00001361 netif_dbg(dev, tx_err, dev->net,
1362 "tx: submit urb err %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
1364 case 0:
1365 net->trans_start = jiffies;
Ming Lei5b6e9bc2012-04-26 11:33:46 +08001366 __usbnet_queue_skb(&dev->txq, skb, tx_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (dev->txq.qlen >= TX_QLEN (dev))
1368 netif_stop_queue (net);
1369 }
1370 spin_unlock_irqrestore (&dev->txq.lock, flags);
1371
1372 if (retval) {
Joe Perchesa475f602010-02-17 10:30:24 +00001373 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374drop:
Herbert Xu79638372009-06-29 16:53:28 +00001375 dev->net->stats.tx_dropped++;
Alexey Orishko073285f2010-11-29 23:23:27 +00001376not_drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (skb)
1378 dev_kfree_skb_any (skb);
Ming Lei638c5112013-08-08 21:48:24 +08001379 if (urb) {
1380 kfree(urb->sg);
1381 usb_free_urb(urb);
1382 }
Joe Perchesa475f602010-02-17 10:30:24 +00001383 } else
1384 netif_dbg(dev, tx_queued, dev->net,
1385 "> tx, len %d, type 0x%x\n", length, skb->protocol);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001386#ifdef CONFIG_PM
1387deferred:
1388#endif
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001389 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001391EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Bjørn Mork85e87872012-09-02 22:26:18 +00001393static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
Ming Lei65841fd2012-06-19 21:15:53 +00001394{
1395 struct urb *urb;
1396 int i;
Bjørn Mork85e87872012-09-02 22:26:18 +00001397 int ret = 0;
Ming Lei65841fd2012-06-19 21:15:53 +00001398
1399 /* don't refill the queue all at once */
1400 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1401 urb = usb_alloc_urb(0, flags);
1402 if (urb != NULL) {
Bjørn Mork85e87872012-09-02 22:26:18 +00001403 ret = rx_submit(dev, urb, flags);
1404 if (ret)
1405 goto err;
1406 } else {
1407 ret = -ENOMEM;
1408 goto err;
Ming Lei65841fd2012-06-19 21:15:53 +00001409 }
1410 }
Bjørn Mork85e87872012-09-02 22:26:18 +00001411err:
1412 return ret;
Ming Lei65841fd2012-06-19 21:15:53 +00001413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/*-------------------------------------------------------------------------*/
1416
1417// tasklet (work deferred from completions, in_irq) or timer
1418
1419static void usbnet_bh (unsigned long param)
1420{
1421 struct usbnet *dev = (struct usbnet *) param;
1422 struct sk_buff *skb;
1423 struct skb_data *entry;
1424
1425 while ((skb = skb_dequeue (&dev->done))) {
1426 entry = (struct skb_data *) skb->cb;
1427 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001428 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 entry->state = rx_cleanup;
1430 rx_process (dev, skb);
1431 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001432 case tx_done:
Ming Lei638c5112013-08-08 21:48:24 +08001433 kfree(entry->urb->sg);
David Brownell18ab4582007-05-25 12:31:32 -07001434 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 usb_free_urb (entry->urb);
1436 dev_kfree_skb (skb);
1437 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001438 default:
Joe Perches60b86752010-02-17 10:30:23 +00001439 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 }
1442
Bjørn Mork70c37bf2013-01-28 23:51:28 +00001443 /* restart RX again after disabling due to high error rate */
1444 clear_bit(EVENT_RX_KILL, &dev->flags);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 // waiting for all pending urbs to complete?
1447 if (dev->wait) {
1448 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1449 wake_up (dev->wait);
1450 }
1451
1452 // or are we maybe short a few urbs?
Joe Perches8e95a202009-12-03 07:58:21 +00001453 } else if (netif_running (dev->net) &&
1454 netif_device_present (dev->net) &&
Ming Lei4b49f582013-04-11 04:40:40 +00001455 netif_carrier_ok(dev->net) &&
Joe Perches8e95a202009-12-03 07:58:21 +00001456 !timer_pending (&dev->delay) &&
1457 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int temp = dev->rxq.qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Ming Lei65841fd2012-06-19 21:15:53 +00001460 if (temp < RX_QLEN(dev)) {
Bjørn Mork85e87872012-09-02 22:26:18 +00001461 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1462 return;
Joe Perchesa475f602010-02-17 10:30:24 +00001463 if (temp != dev->rxq.qlen)
1464 netif_dbg(dev, link, dev->net,
1465 "rxqlen %d --> %d\n",
1466 temp, dev->rxq.qlen);
Ming Lei65841fd2012-06-19 21:15:53 +00001467 if (dev->rxq.qlen < RX_QLEN(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 tasklet_schedule (&dev->bh);
1469 }
1470 if (dev->txq.qlen < TX_QLEN (dev))
1471 netif_wake_queue (dev->net);
1472 }
1473}
1474
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*-------------------------------------------------------------------------
1477 *
1478 * USB Device Driver support
1479 *
1480 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482// precondition: never called in_interrupt
1483
David Brownell38bde1d2005-08-31 09:52:45 -07001484void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 struct usbnet *dev;
1487 struct usb_device *xdev;
1488 struct net_device *net;
1489
1490 dev = usb_get_intfdata(intf);
1491 usb_set_intfdata(intf, NULL);
1492 if (!dev)
1493 return;
1494
1495 xdev = interface_to_usbdev (intf);
1496
Joe Perchesa475f602010-02-17 10:30:24 +00001497 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1498 intf->dev.driver->name,
1499 xdev->bus->bus_name, xdev->devpath,
1500 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 net = dev->net;
1503 unregister_netdev (net);
1504
Tejun Heo23f333a2010-12-12 16:45:14 +01001505 cancel_work_sync(&dev->kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Hemant Kumar39707c22012-10-25 18:17:54 +00001507 usb_scuttle_anchored_urbs(&dev->deferred);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (dev->driver_info->unbind)
1510 dev->driver_info->unbind (dev, intf);
1511
Paul Stewart68972ef2011-04-28 05:43:37 +00001512 usb_kill_urb(dev->interrupt);
1513 usb_free_urb(dev->interrupt);
Ming Lei60e453a2013-09-23 20:59:35 +08001514 kfree(dev->padding_pkt);
Paul Stewart68972ef2011-04-28 05:43:37 +00001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 free_netdev(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
David Brownell38bde1d2005-08-31 09:52:45 -07001518EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Stephen Hemminger777baa42009-03-20 19:35:54 +00001520static const struct net_device_ops usbnet_netdev_ops = {
1521 .ndo_open = usbnet_open,
1522 .ndo_stop = usbnet_stop,
1523 .ndo_start_xmit = usbnet_start_xmit,
1524 .ndo_tx_timeout = usbnet_tx_timeout,
1525 .ndo_change_mtu = usbnet_change_mtu,
1526 .ndo_set_mac_address = eth_mac_addr,
1527 .ndo_validate_addr = eth_validate_addr,
1528};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530/*-------------------------------------------------------------------------*/
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532// precondition: never called in_interrupt
1533
Marcel Holtmann225794f2009-10-02 05:15:26 +00001534static struct device_type wlan_type = {
1535 .name = "wlan",
1536};
1537
1538static struct device_type wwan_type = {
1539 .name = "wwan",
1540};
1541
David Brownell38bde1d2005-08-31 09:52:45 -07001542int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1544{
1545 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001546 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct usb_host_interface *interface;
1548 struct driver_info *info;
1549 struct usb_device *xdev;
1550 int status;
David Brownell296c0242007-04-17 16:10:10 -07001551 const char *name;
Ming Leib0786b42010-11-01 07:11:54 -07001552 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1553
1554 /* usbnet already took usb runtime pm, so have to enable the feature
1555 * for usb interface, otherwise usb_autopm_get_interface may return
Alan Stern98f541c2013-05-01 12:13:54 -04001556 * failure if RUNTIME_PM is enabled.
Ming Leib0786b42010-11-01 07:11:54 -07001557 */
1558 if (!driver->supports_autosuspend) {
1559 driver->supports_autosuspend = 1;
1560 pm_runtime_enable(&udev->dev);
1561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
David Brownell296c0242007-04-17 16:10:10 -07001563 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 info = (struct driver_info *) prod->driver_info;
1565 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001566 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -ENODEV;
1568 }
1569 xdev = interface_to_usbdev (udev);
1570 interface = udev->cur_altsetting;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 status = -ENOMEM;
1573
1574 // set up our own records
1575 net = alloc_etherdev(sizeof(*dev));
Joe Perches41de8d42012-01-29 13:47:52 +00001576 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Ben Hutchings0dacca72010-07-02 21:49:02 -07001579 /* netdev_printk() needs this so do it as early as possible */
1580 SET_NETDEV_DEV(net, &udev->dev);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 dev = netdev_priv(net);
1583 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001584 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001586 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1588 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1589 skb_queue_head_init (&dev->rxq);
1590 skb_queue_head_init (&dev->txq);
1591 skb_queue_head_init (&dev->done);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +03001592 skb_queue_head_init(&dev->rxq_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 dev->bh.func = usbnet_bh;
1594 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001595 INIT_WORK (&dev->kevent, kevent);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001596 init_usb_anchor(&dev->deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 dev->delay.function = usbnet_bh;
1598 dev->delay.data = (unsigned long) dev;
1599 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001600 mutex_init (&dev->phy_mutex);
Dan Williams6eecdc52013-05-06 11:29:23 +00001601 mutex_init(&dev->interrupt_mutex);
1602 dev->interrupt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 dev->net = net;
1605 strcpy (net->name, "usb%d");
1606 memcpy (net->dev_addr, node_id, sizeof node_id);
1607
David Brownell2e55cc72005-08-31 09:53:10 -07001608 /* rx and tx sides can use different message sizes;
1609 * bind() should set rx_urb_size in that case.
1610 */
1611 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#if 0
1613// dma_supported() is deeply broken on almost all architectures
1614 // possible with some EHCI controllers
Yang Hongyang6a355282009-04-06 19:01:13 -07001615 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 net->features |= NETIF_F_HIGHDMA;
1617#endif
1618
Stephen Hemminger777baa42009-03-20 19:35:54 +00001619 net->netdev_ops = &usbnet_netdev_ops;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001620 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 net->ethtool_ops = &usbnet_ethtool_ops;
1622
1623 // allow device-specific bind/init procedures
1624 // NOTE net->name still not usable ...
1625 if (info->bind) {
1626 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001627 if (status < 0)
1628 goto out1;
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 // heuristic: "usb%d" for links we know are two-host,
1631 // else "eth%d" when there's reasonable doubt. userspace
1632 // can rename the link if it knows better.
Joe Perches8e95a202009-12-03 07:58:21 +00001633 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
Arnd Bergmannc2613442011-04-01 20:12:02 -07001634 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1635 (net->dev_addr [0] & 0x02) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001637 /* WLAN devices should always be named "wlan%d" */
1638 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1639 strcpy(net->name, "wlan%d");
Marcel Holtmanne1e499e2009-10-02 05:15:25 +00001640 /* WWAN devices should always be named "wwan%d" */
1641 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1642 strcpy(net->name, "wwan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001643
Wei Shuai65091412013-01-21 06:00:31 +00001644 /* devices that cannot do ARP */
1645 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1646 net->flags |= IFF_NOARP;
1647
David Brownellf29fc252005-08-31 09:52:31 -07001648 /* maybe the remote can't receive an Ethernet MTU */
1649 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1650 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001651 } else if (!info->in || !info->out)
1652 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 else {
1654 dev->in = usb_rcvbulkpipe (xdev, info->in);
1655 dev->out = usb_sndbulkpipe (xdev, info->out);
1656 if (!(info->flags & FLAG_NO_SETINT))
1657 status = usb_set_interface (xdev,
1658 interface->desc.bInterfaceNumber,
1659 interface->desc.bAlternateSetting);
1660 else
1661 status = 0;
1662
1663 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001664 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 status = init_status (dev, udev);
1666 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001667 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
David Brownell2e55cc72005-08-31 09:53:10 -07001669 if (!dev->rx_urb_size)
1670 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001672
Bjørn Morkeef23b52013-09-04 09:42:32 +02001673 /* let userspace know we have a random address */
1674 if (ether_addr_equal(net->dev_addr, node_id))
1675 net->addr_assign_type = NET_ADDR_RANDOM;
1676
Marcel Holtmann225794f2009-10-02 05:15:26 +00001677 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1678 SET_NETDEV_DEVTYPE(net, &wlan_type);
1679 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1680 SET_NETDEV_DEVTYPE(net, &wwan_type);
1681
Ming Leia88c32a2013-07-25 13:47:53 +08001682 /* initialize max rx_qlen and tx_qlen */
1683 usbnet_update_max_qlen(dev);
1684
Ming Lei60e453a2013-09-23 20:59:35 +08001685 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1686 !(info->flags & FLAG_MULTI_PACKET)) {
1687 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
Wei Yongjun09b69022013-10-12 14:24:08 +08001688 if (!dev->padding_pkt) {
1689 status = -ENOMEM;
Ming Lei60e453a2013-09-23 20:59:35 +08001690 goto out4;
Wei Yongjun09b69022013-10-12 14:24:08 +08001691 }
Ming Lei60e453a2013-09-23 20:59:35 +08001692 }
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 status = register_netdev (net);
1695 if (status)
Ming Lei60e453a2013-09-23 20:59:35 +08001696 goto out5;
Joe Perchesa475f602010-02-17 10:30:24 +00001697 netif_info(dev, probe, dev->net,
1698 "register '%s' at usb-%s-%s, %s, %pM\n",
1699 udev->dev.driver->name,
1700 xdev->bus->bus_name, xdev->devpath,
1701 dev->driver_info->description,
1702 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 // ok, it's ready to go.
1705 usb_set_intfdata (udev, dev);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 netif_device_attach (net);
1708
Ben Hutchings37e82732009-11-04 15:29:52 +00001709 if (dev->driver_info->flags & FLAG_LINK_INTR)
Ming Lei0162c552013-04-11 04:40:39 +00001710 usbnet_link_change(dev, 0, 0);
Ben Hutchings37e82732009-11-04 15:29:52 +00001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
1713
Ming Lei60e453a2013-09-23 20:59:35 +08001714out5:
1715 kfree(dev->padding_pkt);
tom.leiming@gmail.coma4723842012-04-29 22:51:03 +00001716out4:
1717 usb_free_urb(dev->interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718out3:
1719 if (info->unbind)
1720 info->unbind (dev, udev);
1721out1:
1722 free_netdev(net);
1723out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return status;
1725}
David Brownell38bde1d2005-08-31 09:52:45 -07001726EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728/*-------------------------------------------------------------------------*/
1729
Oliver Neukum36433122007-04-30 01:37:44 -07001730/*
1731 * suspend the whole driver as soon as the first interface is suspended
1732 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
David Brownell38bde1d2005-08-31 09:52:45 -07001735int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001738
Oliver Neukum36433122007-04-30 01:37:44 -07001739 if (!dev->suspend_count++) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001740 spin_lock_irq(&dev->txq.lock);
1741 /* don't autosuspend while transmitting */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001742 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
Ming Lei5eeb3132012-06-19 21:15:52 +00001743 dev->suspend_count--;
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001744 spin_unlock_irq(&dev->txq.lock);
1745 return -EBUSY;
1746 } else {
1747 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1748 spin_unlock_irq(&dev->txq.lock);
1749 }
Oliver Neukuma11a6542007-08-03 13:52:19 +02001750 /*
1751 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001752 * having everything error out.
1753 */
1754 netif_device_detach (dev->net);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001755 usbnet_terminate_urbs(dev);
Dan Williams6eecdc52013-05-06 11:29:23 +00001756 __usbnet_status_stop_force(dev);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001757
Oliver Neukuma11a6542007-08-03 13:52:19 +02001758 /*
1759 * reattach so runtime management can use and
1760 * wake the device
1761 */
1762 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return 0;
1765}
David Brownell38bde1d2005-08-31 09:52:45 -07001766EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
David Brownell38bde1d2005-08-31 09:52:45 -07001768int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 struct usbnet *dev = usb_get_intfdata(intf);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001771 struct sk_buff *skb;
1772 struct urb *res;
1773 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001775 if (!--dev->suspend_count) {
Dan Williams6eecdc52013-05-06 11:29:23 +00001776 /* resume interrupt URB if it was previously submitted */
1777 __usbnet_status_start_force(dev, GFP_NOIO);
Paul Stewart68972ef2011-04-28 05:43:37 +00001778
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001779 spin_lock_irq(&dev->txq.lock);
1780 while ((res = usb_get_from_anchor(&dev->deferred))) {
1781
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001782 skb = (struct sk_buff *)res->context;
1783 retval = usb_submit_urb(res, GFP_ATOMIC);
1784 if (retval < 0) {
1785 dev_kfree_skb_any(skb);
Ming Lei638c5112013-08-08 21:48:24 +08001786 kfree(res->sg);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001787 usb_free_urb(res);
1788 usb_autopm_put_interface_async(dev->intf);
1789 } else {
1790 dev->net->trans_start = jiffies;
1791 __skb_queue_tail(&dev->txq, skb);
1792 }
1793 }
1794
1795 smp_mb();
1796 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1797 spin_unlock_irq(&dev->txq.lock);
Ming Lei75bd0cb2011-04-28 22:37:09 +00001798
1799 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
Ming Lei65841fd2012-06-19 21:15:53 +00001800 /* handle remote wakeup ASAP */
1801 if (!dev->wait &&
1802 netif_device_present(dev->net) &&
1803 !timer_pending(&dev->delay) &&
1804 !test_bit(EVENT_RX_HALT, &dev->flags))
Oliver Neukumab6f1482012-08-26 20:41:38 +00001805 rx_alloc_submit(dev, GFP_NOIO);
Ming Lei65841fd2012-06-19 21:15:53 +00001806
Ming Lei75bd0cb2011-04-28 22:37:09 +00001807 if (!(dev->txq.qlen >= TX_QLEN(dev)))
Alexey Orishko1aa9bc52012-03-14 04:00:24 +00001808 netif_tx_wake_all_queues(dev->net);
Ming Lei75bd0cb2011-04-28 22:37:09 +00001809 tasklet_schedule (&dev->bh);
1810 }
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001811 }
Oliver Neukum5d9d01a32012-10-11 02:50:10 +00001812
1813 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1814 usb_autopm_get_interface_no_resume(intf);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817}
David Brownell38bde1d2005-08-31 09:52:45 -07001818EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Oliver Neukum5d9d01a32012-10-11 02:50:10 +00001820/*
1821 * Either a subdriver implements manage_power, then it is assumed to always
1822 * be ready to be suspended or it reports the readiness to be suspended
1823 * explicitly
1824 */
1825void usbnet_device_suggests_idle(struct usbnet *dev)
1826{
1827 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1828 dev->intf->needs_remote_wakeup = 1;
1829 usb_autopm_put_interface_async(dev->intf);
1830 }
1831}
1832EXPORT_SYMBOL(usbnet_device_suggests_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Oliver Neukum2dd7c8c2012-12-18 04:45:52 +00001834/*
1835 * For devices that can do without special commands
1836 */
1837int usbnet_manage_power(struct usbnet *dev, int on)
1838{
1839 dev->intf->needs_remote_wakeup = on;
1840 return 0;
1841}
1842EXPORT_SYMBOL(usbnet_manage_power);
1843
Ming Leiac649952013-04-11 04:40:30 +00001844void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1845{
1846 /* update link after link is reseted */
1847 if (link && !need_reset)
1848 netif_carrier_on(dev->net);
1849 else
1850 netif_carrier_off(dev->net);
1851
1852 if (need_reset && link)
1853 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
Ming Lei4b49f582013-04-11 04:40:40 +00001854 else
1855 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
Ming Leiac649952013-04-11 04:40:30 +00001856}
1857EXPORT_SYMBOL(usbnet_link_change);
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859/*-------------------------------------------------------------------------*/
Ming Lei0547fad2012-11-06 04:53:04 +00001860static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1861 u16 value, u16 index, void *data, u16 size)
Ming Lei877bd862012-10-24 19:46:54 +00001862{
1863 void *buf = NULL;
1864 int err = -ENOMEM;
1865
1866 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1867 " value=0x%04x index=0x%04x size=%d\n",
1868 cmd, reqtype, value, index, size);
1869
1870 if (data) {
1871 buf = kmalloc(size, GFP_KERNEL);
1872 if (!buf)
1873 goto out;
1874 }
1875
1876 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1877 cmd, reqtype, value, index, buf, size,
1878 USB_CTRL_GET_TIMEOUT);
1879 if (err > 0 && err <= size)
1880 memcpy(data, buf, err);
1881 kfree(buf);
1882out:
1883 return err;
1884}
Ming Lei877bd862012-10-24 19:46:54 +00001885
Ming Lei0547fad2012-11-06 04:53:04 +00001886static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1887 u16 value, u16 index, const void *data,
1888 u16 size)
Ming Lei877bd862012-10-24 19:46:54 +00001889{
1890 void *buf = NULL;
1891 int err = -ENOMEM;
1892
1893 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1894 " value=0x%04x index=0x%04x size=%d\n",
1895 cmd, reqtype, value, index, size);
1896
1897 if (data) {
1898 buf = kmemdup(data, size, GFP_KERNEL);
1899 if (!buf)
1900 goto out;
1901 }
1902
1903 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1904 cmd, reqtype, value, index, buf, size,
1905 USB_CTRL_SET_TIMEOUT);
1906 kfree(buf);
1907
1908out:
1909 return err;
1910}
Ming Lei0547fad2012-11-06 04:53:04 +00001911
1912/*
1913 * The function can't be called inside suspend/resume callback,
1914 * otherwise deadlock will be caused.
1915 */
1916int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1917 u16 value, u16 index, void *data, u16 size)
1918{
Ming Lei6f0a0982012-11-06 04:53:08 +00001919 int ret;
1920
1921 if (usb_autopm_get_interface(dev->intf) < 0)
1922 return -ENODEV;
1923 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1924 data, size);
1925 usb_autopm_put_interface(dev->intf);
1926 return ret;
Ming Lei0547fad2012-11-06 04:53:04 +00001927}
1928EXPORT_SYMBOL_GPL(usbnet_read_cmd);
1929
1930/*
1931 * The function can't be called inside suspend/resume callback,
1932 * otherwise deadlock will be caused.
1933 */
1934int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1935 u16 value, u16 index, const void *data, u16 size)
1936{
Ming Lei6f0a0982012-11-06 04:53:08 +00001937 int ret;
1938
1939 if (usb_autopm_get_interface(dev->intf) < 0)
1940 return -ENODEV;
1941 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1942 data, size);
1943 usb_autopm_put_interface(dev->intf);
1944 return ret;
Ming Lei0547fad2012-11-06 04:53:04 +00001945}
Ming Lei877bd862012-10-24 19:46:54 +00001946EXPORT_SYMBOL_GPL(usbnet_write_cmd);
1947
Ming Lei0547fad2012-11-06 04:53:04 +00001948/*
1949 * The function can be called inside suspend/resume callback safely
1950 * and should only be called by suspend/resume callback generally.
1951 */
1952int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1953 u16 value, u16 index, void *data, u16 size)
1954{
1955 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
1956 data, size);
1957}
1958EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
1959
1960/*
1961 * The function can be called inside suspend/resume callback safely
1962 * and should only be called by suspend/resume callback generally.
1963 */
1964int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
1965 u16 value, u16 index, const void *data,
1966 u16 size)
1967{
1968 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
1969 data, size);
1970}
1971EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
1972
Ming Lei877bd862012-10-24 19:46:54 +00001973static void usbnet_async_cmd_cb(struct urb *urb)
1974{
1975 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
1976 int status = urb->status;
1977
1978 if (status < 0)
1979 dev_dbg(&urb->dev->dev, "%s failed with %d",
1980 __func__, status);
1981
1982 kfree(req);
1983 usb_free_urb(urb);
1984}
1985
Ming Lei0547fad2012-11-06 04:53:04 +00001986/*
1987 * The caller must make sure that device can't be put into suspend
1988 * state until the control URB completes.
1989 */
Ming Lei877bd862012-10-24 19:46:54 +00001990int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
1991 u16 value, u16 index, const void *data, u16 size)
1992{
1993 struct usb_ctrlrequest *req = NULL;
1994 struct urb *urb;
1995 int err = -ENOMEM;
1996 void *buf = NULL;
1997
1998 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1999 " value=0x%04x index=0x%04x size=%d\n",
2000 cmd, reqtype, value, index, size);
2001
2002 urb = usb_alloc_urb(0, GFP_ATOMIC);
2003 if (!urb) {
2004 netdev_err(dev->net, "Error allocating URB in"
2005 " %s!\n", __func__);
2006 goto fail;
2007 }
2008
2009 if (data) {
2010 buf = kmemdup(data, size, GFP_ATOMIC);
2011 if (!buf) {
2012 netdev_err(dev->net, "Error allocating buffer"
2013 " in %s!\n", __func__);
2014 goto fail_free;
2015 }
2016 }
2017
2018 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
Joe Perches38673c82013-02-03 17:28:11 +00002019 if (!req)
Ming Lei877bd862012-10-24 19:46:54 +00002020 goto fail_free_buf;
Ming Lei877bd862012-10-24 19:46:54 +00002021
2022 req->bRequestType = reqtype;
2023 req->bRequest = cmd;
2024 req->wValue = cpu_to_le16(value);
2025 req->wIndex = cpu_to_le16(index);
2026 req->wLength = cpu_to_le16(size);
2027
2028 usb_fill_control_urb(urb, dev->udev,
2029 usb_sndctrlpipe(dev->udev, 0),
2030 (void *)req, buf, size,
2031 usbnet_async_cmd_cb, req);
2032 urb->transfer_flags |= URB_FREE_BUFFER;
2033
2034 err = usb_submit_urb(urb, GFP_ATOMIC);
2035 if (err < 0) {
2036 netdev_err(dev->net, "Error submitting the control"
2037 " message: status=%d\n", err);
2038 goto fail_free;
2039 }
2040 return 0;
2041
2042fail_free_buf:
2043 kfree(buf);
2044fail_free:
2045 kfree(req);
2046 usb_free_urb(urb);
2047fail:
2048 return err;
2049
2050}
2051EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
2052/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
David Brownellf29fc252005-08-31 09:52:31 -07002054static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Thiago Farinac582a952011-04-17 17:49:21 -07002056 /* Compiler should optimize this out. */
2057 BUILD_BUG_ON(
2058 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Joe Perchesc7e12ea2012-07-12 19:33:07 +00002060 eth_random_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08002061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
David Brownellf29fc252005-08-31 09:52:31 -07002063module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
David Brownellf29fc252005-08-31 09:52:31 -07002065static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
David Brownellf29fc252005-08-31 09:52:31 -07002068module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
David Brownellf29fc252005-08-31 09:52:31 -07002070MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07002071MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07002072MODULE_LICENSE("GPL");