blob: d7c5df17f03090b8c4ed2f6b435540b517efdc91 [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>
Hemant Kumar6d2c7472012-03-06 23:39:29 -080038#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
Peter Holik03ad0322009-04-18 07:24:17 +000041#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/ethtool.h>
43#include <linux/workqueue.h>
44#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020046#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Andy Shevchenkofd1f1702010-07-23 03:18:08 +000048#include <linux/kernel.h>
Ming Leib0786b42010-11-01 07:11:54 -070049#include <linux/pm_runtime.h>
David Brownellf29fc252005-08-31 09:52:31 -070050
51#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
54/*-------------------------------------------------------------------------*/
55
56/*
57 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
58 * Several dozen bytes of IPv4 data can fit in two such transactions.
59 * One maximum size Ethernet packet takes twenty four of them.
60 * For high speed, each frame comfortably fits almost 36 max size
61 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070062 *
63 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
64 * let the USB host controller be busy for 5msec or more before an irq
65 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Jamie Paintera99c1942006-07-27 14:17:28 -040067#define RX_MAX_QUEUE_MEMORY (60 * 1518)
68#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
69 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
70#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
71 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073// reawaken network queue this soon after stopping; else watchdog barks
74#define TX_TIMEOUT_JIFFIES (5*HZ)
75
76// throttle rx/tx briefly after some faults, so khubd might disconnect()
77// us (it polls at HZ/4 usually) before we report too many false errors.
78#define THROTTLE_JIFFIES (HZ/8)
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080// between wakeups
81#define UNLINK_TIMEOUT_MS 3
82
83/*-------------------------------------------------------------------------*/
84
85// randomly generated ethernet address
86static u8 node_id [ETH_ALEN];
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static const char driver_name [] = "usbnet";
89
Hemant Kumar131eb4b2012-11-26 19:47:18 -080090struct workqueue_struct *usbnet_wq;
Hemant Kumar1e8ddb52012-07-02 13:39:22 -070091
Hemant Kumaref59a422012-08-15 16:57:00 -070092static DECLARE_WAIT_QUEUE_HEAD(unlink_wakeup);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* use ethtool to change the level for any given device */
95static int msg_level = -1;
96module_param (msg_level, int, 0);
97MODULE_PARM_DESC (msg_level, "Override default message level");
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*-------------------------------------------------------------------------*/
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -0700102int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int tmp;
105 struct usb_host_interface *alt = NULL;
106 struct usb_host_endpoint *in = NULL, *out = NULL;
107 struct usb_host_endpoint *status = NULL;
108
109 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
110 unsigned ep;
111
112 in = out = status = NULL;
113 alt = intf->altsetting + tmp;
114
115 /* take the first altsetting with in-bulk + out-bulk;
116 * remember any status endpoint, just in case;
Justin P. Mattock70f23fd2011-05-10 10:16:21 +0200117 * ignore other endpoints and altsettings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
120 struct usb_host_endpoint *e;
121 int intr = 0;
122
123 e = alt->endpoint + ep;
124 switch (e->desc.bmAttributes) {
125 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300126 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 continue;
128 intr = 1;
129 /* FALLTHROUGH */
130 case USB_ENDPOINT_XFER_BULK:
131 break;
132 default:
133 continue;
134 }
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300135 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!intr && !in)
137 in = e;
138 else if (intr && !status)
139 status = e;
140 } else {
141 if (!out)
142 out = e;
143 }
144 }
145 if (in && out)
146 break;
147 }
148 if (!alt || !in || !out)
149 return -EINVAL;
150
Joe Perches8e95a202009-12-03 07:58:21 +0000151 if (alt->desc.bAlternateSetting != 0 ||
152 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
154 alt->desc.bAlternateSetting);
155 if (tmp < 0)
156 return tmp;
157 }
David Brownellcb1cebb2007-02-15 18:52:30 -0800158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 dev->in = usb_rcvbulkpipe (dev->udev,
160 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
161 dev->out = usb_sndbulkpipe (dev->udev,
162 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
163 dev->status = status;
164 return 0;
165}
David Brownell2e55cc72005-08-31 09:53:10 -0700166EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Peter Holik03ad0322009-04-18 07:24:17 +0000168int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
169{
170 int tmp, i;
171 unsigned char buf [13];
172
173 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
174 if (tmp != 12) {
175 dev_dbg(&dev->udev->dev,
176 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
177 if (tmp >= 0)
178 tmp = -EINVAL;
179 return tmp;
180 }
181 for (i = tmp = 0; i < 6; i++, tmp += 2)
182 dev->net->dev_addr [i] =
Andy Shevchenkofd1f1702010-07-23 03:18:08 +0000183 (hex_to_bin(buf[tmp]) << 4) + hex_to_bin(buf[tmp + 1]);
Peter Holik03ad0322009-04-18 07:24:17 +0000184 return 0;
185}
186EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
187
David Howells7d12e782006-10-05 14:55:46 +0100188static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190static int init_status (struct usbnet *dev, struct usb_interface *intf)
191{
192 char *buf = NULL;
193 unsigned pipe = 0;
194 unsigned maxp;
195 unsigned period;
196
197 if (!dev->driver_info->status)
198 return 0;
199
200 pipe = usb_rcvintpipe (dev->udev,
201 dev->status->desc.bEndpointAddress
202 & USB_ENDPOINT_NUMBER_MASK);
203 maxp = usb_maxpacket (dev->udev, pipe, 0);
204
205 /* avoid 1 msec chatter: min 8 msec poll rate */
206 period = max ((int) dev->status->desc.bInterval,
207 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
208
Christoph Lametere94b1762006-12-06 20:33:17 -0800209 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800211 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!dev->interrupt) {
213 kfree (buf);
214 return -ENOMEM;
215 } else {
216 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
217 buf, maxp, intr_complete, dev, period);
tom.leiming@gmail.com720f3d72012-04-29 22:51:02 +0000218 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dev_dbg(&intf->dev,
220 "status ep%din, %d bytes period %d\n",
221 usb_pipeendpoint(pipe), maxp, period);
222 }
223 }
David Brownell18ab4582007-05-25 12:31:32 -0700224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
David Brownell2e55cc72005-08-31 09:53:10 -0700227/* Passes this packet up the stack, updating its accounting.
228 * Some link protocols batch packets, so their rx_fixup paths
229 * can return clones as well as just modify the original skb.
230 */
231void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int status;
234
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300235 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
236 skb_queue_tail(&dev->rxq_pause, skb);
237 return;
238 }
239
Hemant Kumar37c35e42011-09-14 23:44:19 -0700240 if (!skb->protocol)
241 skb->protocol = eth_type_trans(skb, dev->net);
242
Herbert Xu79638372009-06-29 16:53:28 +0000243 dev->net->stats.rx_packets++;
244 dev->net->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Joe Perchesa475f602010-02-17 10:30:24 +0000246 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
247 skb->len + sizeof (struct ethhdr), skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 memset (skb->cb, 0, sizeof (struct skb_data));
Michael Rieschf9b491e2011-09-29 04:06:26 +0000249
250 if (skb_defer_rx_timestamp(skb))
251 return;
252
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700253 status = netif_rx_ni(skb);
Joe Perchesa475f602010-02-17 10:30:24 +0000254 if (status != NET_RX_SUCCESS)
255 netif_dbg(dev, rx_err, dev->net,
256 "netif_rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
David Brownell2e55cc72005-08-31 09:53:10 -0700258EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*-------------------------------------------------------------------------
262 *
263 * Network Device Driver (peer link to "Host Device", from USB host)
264 *
265 *-------------------------------------------------------------------------*/
266
Stephen Hemminger777baa42009-03-20 19:35:54 +0000267int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700270 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400271 int old_hard_mtu = dev->hard_mtu;
272 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Jamie Paintera99c1942006-07-27 14:17:28 -0400274 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700277 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -EDOM;
279 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400280
281 dev->hard_mtu = net->mtu + net->hard_header_len;
282 if (dev->rx_urb_size == old_hard_mtu) {
283 dev->rx_urb_size = dev->hard_mtu;
284 if (dev->rx_urb_size > old_rx_urb_size)
285 usbnet_unlink_rx_urbs(dev);
286 }
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000290EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800292/* The caller must hold list->lock */
293static void __usbnet_queue_skb(struct sk_buff_head *list,
294 struct sk_buff *newsk, enum skb_state state)
295{
296 struct skb_data *entry = (struct skb_data *) newsk->cb;
297
298 __skb_queue_tail(list, newsk);
299 entry->state = state;
300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*-------------------------------------------------------------------------*/
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
305 * completion callbacks. 2.5 should have fixed those bugs...
306 */
307
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800308static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
309 struct sk_buff_head *list, enum skb_state state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned long flags;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800312 enum skb_state old_state;
313 struct skb_data *entry = (struct skb_data *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
David S. Miller8728b832005-08-09 19:25:21 -0700315 spin_lock_irqsave(&list->lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800316 old_state = entry->state;
317 entry->state = state;
David S. Miller8728b832005-08-09 19:25:21 -0700318 __skb_unlink(skb, list);
319 spin_unlock(&list->lock);
320 spin_lock(&dev->done.lock);
321 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (dev->done.qlen == 1)
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700323 queue_work(usbnet_wq, &dev->bh_w);
David S. Miller8728b832005-08-09 19:25:21 -0700324 spin_unlock_irqrestore(&dev->done.lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800325 return old_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/* some work can't be done in tasklets, so we use keventd
329 *
330 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
331 * but tasklet_schedule() doesn't. hope the failure is rare.
332 */
David Brownell2e55cc72005-08-31 09:53:10 -0700333void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 set_bit (work, &dev->flags);
Steve Glendinning5ae14242012-11-08 06:26:21 +0000336 if (!schedule_work (&dev->kevent)) {
337 if (net_ratelimit())
338 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
339 } else {
Joe Perches60b86752010-02-17 10:30:23 +0000340 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
Steve Glendinning5ae14242012-11-08 06:26:21 +0000341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
David Brownell2e55cc72005-08-31 09:53:10 -0700343EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345/*-------------------------------------------------------------------------*/
346
David S. Millerdacb3972010-08-10 02:50:55 -0700347static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct sk_buff *skb;
350 struct skb_data *entry;
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800351 usb_complete_t complete_fn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int retval = 0;
353 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700354 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Eric Dumazet7bdd4022012-03-14 06:56:25 +0000356 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
357 if (!skb) {
Joe Perchesa475f602010-02-17 10:30:24 +0000358 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
David Brownell2e55cc72005-08-31 09:53:10 -0700359 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 usb_free_urb (urb);
David S. Millerdacb3972010-08-10 02:50:55 -0700361 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Hemant Kumar6d2c7472012-03-06 23:39:29 -0800364 if (dev->net->type != ARPHRD_RAWIP)
365 skb_reserve(skb, NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 entry = (struct skb_data *) skb->cb;
368 entry->urb = urb;
369 entry->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 entry->length = 0;
371
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800372 if (dev->driver_info->rx_complete)
373 complete_fn = dev->driver_info->rx_complete;
374 else
375 complete_fn = rx_complete;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 usb_fill_bulk_urb (urb, dev->udev, dev->in,
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800378 skb->data, size, complete_fn, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 spin_lock_irqsave (&dev->rxq.lock, lockflags);
381
Joe Perches8e95a202009-12-03 07:58:21 +0000382 if (netif_running (dev->net) &&
383 netif_device_present (dev->net) &&
Oliver Neukum69ee4722009-12-03 15:31:18 -0800384 !test_bit (EVENT_RX_HALT, &dev->flags) &&
385 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700386 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700388 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
390 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700391 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
393 case -ENODEV:
Joe Perchesa475f602010-02-17 10:30:24 +0000394 netif_dbg(dev, ifdown, dev->net, "device gone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 netif_device_detach (dev->net);
396 break;
David S. Millerdacb3972010-08-10 02:50:55 -0700397 case -EHOSTUNREACH:
398 retval = -ENOLINK;
399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000401 netif_dbg(dev, rx_err, dev->net,
402 "rx submit, %d\n", retval);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700403 queue_work(usbnet_wq, &dev->bh_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405 case 0:
Hemant Kumar279188a2012-04-03 13:32:48 -0700406 usb_mark_last_busy(dev->udev);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800407 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000410 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 retval = -ENOLINK;
412 }
413 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
414 if (retval) {
415 dev_kfree_skb_any (skb);
416 usb_free_urb (urb);
417 }
David S. Millerdacb3972010-08-10 02:50:55 -0700418 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421
422/*-------------------------------------------------------------------------*/
423
424static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
425{
Joe Perches8e95a202009-12-03 07:58:21 +0000426 if (dev->driver_info->rx_fixup &&
Andrzej Zaborowski7a635ea2011-03-28 12:56:33 +0000427 !dev->driver_info->rx_fixup (dev, skb)) {
428 /* With RX_ASSEMBLE, rx_fixup() must update counters */
429 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
430 dev->net->stats.rx_errors++;
431 goto done;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 // else network stack removes extra byte if we forced a short packet
434
Alexey Orishko073285f2010-11-29 23:23:27 +0000435 if (skb->len) {
436 /* all data was already cloned from skb inside the driver */
437 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
438 dev_kfree_skb_any(skb);
439 else
440 usbnet_skb_return(dev, skb);
441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Alexey Orishko073285f2010-11-29 23:23:27 +0000443
444 netif_dbg(dev, rx_err, dev->net, "drop\n");
Alexey Orishko073285f2010-11-29 23:23:27 +0000445 dev->net->stats.rx_errors++;
Andrzej Zaborowski7a635ea2011-03-28 12:56:33 +0000446done:
Alexey Orishko073285f2010-11-29 23:23:27 +0000447 skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*-------------------------------------------------------------------------*/
451
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800452void rx_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct sk_buff *skb = (struct sk_buff *) urb->context;
455 struct skb_data *entry = (struct skb_data *) skb->cb;
456 struct usbnet *dev = entry->dev;
457 int urb_status = urb->status;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800458 enum skb_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 skb_put (skb, urb->actual_length);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800461 state = rx_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 entry->urb = NULL;
463
464 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700465 /* success */
466 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700467 if (skb->len < dev->net->hard_header_len) {
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800468 state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000469 dev->net->stats.rx_errors++;
470 dev->net->stats.rx_length_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000471 netif_dbg(dev, rx_err, dev->net,
472 "rx length %d\n", skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 break;
475
David Brownell18ab4582007-05-25 12:31:32 -0700476 /* stalls need manual reset. this is rare ... except that
477 * when going through USB 2.0 TTs, unplug appears this way.
Jean Delvare3ac49a12009-06-04 16:20:28 +0200478 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
David Brownell18ab4582007-05-25 12:31:32 -0700479 * storm, recovering as needed.
480 */
481 case -EPIPE:
Herbert Xu79638372009-06-29 16:53:28 +0000482 dev->net->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700483 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 // FALLTHROUGH
485
David Brownell18ab4582007-05-25 12:31:32 -0700486 /* software-driven interface shutdown */
487 case -ECONNRESET: /* async unlink */
488 case -ESHUTDOWN: /* hardware gone */
Joe Perchesa475f602010-02-17 10:30:24 +0000489 netif_dbg(dev, ifdown, dev->net,
490 "rx shutdown, code %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto block;
492
David Brownell18ab4582007-05-25 12:31:32 -0700493 /* we get controller i/o faults during khubd disconnect() delays.
494 * throttle down resubmits, to avoid log floods; just temporarily,
495 * so we still recover when the fault isn't a khubd delay.
496 */
497 case -EPROTO:
498 case -ETIME:
499 case -EILSEQ:
Herbert Xu79638372009-06-29 16:53:28 +0000500 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!timer_pending (&dev->delay)) {
502 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +0000503 netif_dbg(dev, link, dev->net,
504 "rx throttle %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506block:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800507 state = rx_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 entry->urb = urb;
509 urb = NULL;
510 break;
511
David Brownell18ab4582007-05-25 12:31:32 -0700512 /* data overrun ... flush fifo? */
513 case -EOVERFLOW:
Herbert Xu79638372009-06-29 16:53:28 +0000514 dev->net->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800516
David Brownell18ab4582007-05-25 12:31:32 -0700517 default:
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800518 state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000519 dev->net->stats.rx_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000520 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522 }
523
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800524 state = defer_bh(dev, skb, &dev->rxq, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (urb) {
Joe Perches8e95a202009-12-03 07:58:21 +0000527 if (netif_running (dev->net) &&
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800528 !test_bit (EVENT_RX_HALT, &dev->flags) &&
529 state != unlink_start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 rx_submit (dev, urb, GFP_ATOMIC);
Oliver Neukum8a783352012-03-03 18:45:07 +0100531 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return;
533 }
534 usb_free_urb (urb);
535 }
Joe Perchesa475f602010-02-17 10:30:24 +0000536 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800538EXPORT_SYMBOL_GPL(rx_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
David Howells7d12e782006-10-05 14:55:46 +0100540static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct usbnet *dev = urb->context;
543 int status = urb->status;
544
545 switch (status) {
David Brownell18ab4582007-05-25 12:31:32 -0700546 /* success */
547 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dev->driver_info->status(dev, urb);
549 break;
550
David Brownell18ab4582007-05-25 12:31:32 -0700551 /* software-driven interface shutdown */
552 case -ENOENT: /* urb killed */
553 case -ESHUTDOWN: /* hardware gone */
Joe Perchesa475f602010-02-17 10:30:24 +0000554 netif_dbg(dev, ifdown, dev->net,
555 "intr shutdown, code %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return;
557
David Brownell18ab4582007-05-25 12:31:32 -0700558 /* NOTE: not throttling like RX/TX, since this endpoint
559 * already polls infrequently
560 */
561 default:
Joe Perches60b86752010-02-17 10:30:23 +0000562 netdev_dbg(dev->net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 }
565
566 if (!netif_running (dev->net))
567 return;
568
569 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
570 status = usb_submit_urb (urb, GFP_ATOMIC);
Joe Perchesa475f602010-02-17 10:30:24 +0000571 if (status != 0)
572 netif_err(dev, timer, dev->net,
573 "intr resubmit --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*-------------------------------------------------------------------------*/
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300577void usbnet_pause_rx(struct usbnet *dev)
578{
579 set_bit(EVENT_RX_PAUSED, &dev->flags);
580
Joe Perchesa475f602010-02-17 10:30:24 +0000581 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300582}
583EXPORT_SYMBOL_GPL(usbnet_pause_rx);
584
585void usbnet_resume_rx(struct usbnet *dev)
586{
587 struct sk_buff *skb;
588 int num = 0;
589
590 clear_bit(EVENT_RX_PAUSED, &dev->flags);
591
592 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
593 usbnet_skb_return(dev, skb);
594 num++;
595 }
596
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700597 queue_work(usbnet_wq, &dev->bh_w);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300598
Joe Perchesa475f602010-02-17 10:30:24 +0000599 netif_dbg(dev, rx_status, dev->net,
600 "paused rx queue disabled, %d skbs requeued\n", num);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300601}
602EXPORT_SYMBOL_GPL(usbnet_resume_rx);
603
604void usbnet_purge_paused_rxq(struct usbnet *dev)
605{
606 skb_queue_purge(&dev->rxq_pause);
607}
608EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
609
610/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612// unlink pending rx/tx; completion handlers do all other cleanup
613
614static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
615{
616 unsigned long flags;
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800617 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int count = 0;
619
620 spin_lock_irqsave (&q->lock, flags);
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800621 while (!skb_queue_empty(q)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct skb_data *entry;
623 struct urb *urb;
624 int retval;
625
Ming Lei5b6e9bc2012-04-26 11:33:46 +0800626 skb_queue_walk(q, skb) {
627 entry = (struct skb_data *) skb->cb;
628 if (entry->state != unlink_start)
629 goto found;
630 }
631 break;
632found:
633 entry->state = unlink_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
tom.leiming@gmail.com0956a8c2012-03-22 03:22:18 +0000636 /*
637 * Get reference count of the URB to avoid it to be
638 * freed during usb_unlink_urb, which may trigger
639 * use-after-free problem inside usb_unlink_urb since
640 * usb_unlink_urb is always racing with .complete
641 * handler(include defer_bh).
642 */
643 usb_get_urb(urb);
Sebastian Siewior4231d472012-03-07 10:19:28 +0000644 spin_unlock_irqrestore(&q->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 // during some PM-driven resume scenarios,
646 // these (async) unlinks complete immediately
647 retval = usb_unlink_urb (urb);
648 if (retval != -EINPROGRESS && retval != 0)
Joe Perches60b86752010-02-17 10:30:23 +0000649 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 else
651 count++;
tom.leiming@gmail.com0956a8c2012-03-22 03:22:18 +0000652 usb_put_urb(urb);
Sebastian Siewior4231d472012-03-07 10:19:28 +0000653 spin_lock_irqsave(&q->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 spin_unlock_irqrestore (&q->lock, flags);
656 return count;
657}
658
Jamie Paintera99c1942006-07-27 14:17:28 -0400659// Flush all pending rx urbs
660// minidrivers may need to do this when the MTU changes
661
662void usbnet_unlink_rx_urbs(struct usbnet *dev)
663{
664 if (netif_running(dev->net)) {
665 (void) unlink_urbs (dev, &dev->rxq);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700666 queue_work(usbnet_wq, &dev->bh_w);
Jamie Paintera99c1942006-07-27 14:17:28 -0400667 }
668}
669EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/*-------------------------------------------------------------------------*/
672
673// precondition: never called in_interrupt
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800674void usbnet_terminate_urbs(struct usbnet *dev)
Oliver Neukum69ee4722009-12-03 15:31:18 -0800675{
Oliver Neukum69ee4722009-12-03 15:31:18 -0800676 DECLARE_WAITQUEUE(wait, current);
677 int temp;
678
679 /* ensure there are no more active urbs */
680 add_wait_queue(&unlink_wakeup, &wait);
681 set_current_state(TASK_UNINTERRUPTIBLE);
682 dev->wait = &unlink_wakeup;
683 temp = unlink_urbs(dev, &dev->txq) +
684 unlink_urbs(dev, &dev->rxq);
685
686 /* maybe wait for deletions to finish. */
687 while (!skb_queue_empty(&dev->rxq)
688 && !skb_queue_empty(&dev->txq)
689 && !skb_queue_empty(&dev->done)) {
Kulikov Vasiliy66cc42a2010-07-25 22:25:42 +0000690 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
Oliver Neukum69ee4722009-12-03 15:31:18 -0800691 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perchesa475f602010-02-17 10:30:24 +0000692 netif_dbg(dev, ifdown, dev->net,
693 "waited for %d urb completions\n", temp);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800694 }
695 set_current_state(TASK_RUNNING);
696 dev->wait = NULL;
697 remove_wait_queue(&unlink_wakeup, &wait);
698}
Hemant Kumar131eb4b2012-11-26 19:47:18 -0800699EXPORT_SYMBOL_GPL(usbnet_terminate_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Stephen Hemminger777baa42009-03-20 19:35:54 +0000701int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct usbnet *dev = netdev_priv(net);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300704 struct driver_info *info = dev->driver_info;
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300705 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Ming Lei75bd0cb2011-04-28 22:37:09 +0000707 clear_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 netif_stop_queue (net);
709
Joe Perchesa475f602010-02-17 10:30:24 +0000710 netif_info(dev, ifdown, dev->net,
Ben Hutchings8ccef432010-06-08 08:20:59 +0000711 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
Joe Perchesa475f602010-02-17 10:30:24 +0000712 net->stats.rx_packets, net->stats.tx_packets,
713 net->stats.rx_errors, net->stats.tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300715 /* allow minidriver to stop correctly (wireless devices to turn off
716 * radio etc) */
717 if (info->stop) {
718 retval = info->stop(dev);
Joe Perchesa475f602010-02-17 10:30:24 +0000719 if (retval < 0)
720 netif_info(dev, ifdown, dev->net,
721 "stop fail (%d) usbnet usb-%s-%s, %s\n",
722 retval,
723 dev->udev->bus->bus_name, dev->udev->devpath,
724 info->description);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300725 }
726
Oliver Neukum69ee4722009-12-03 15:31:18 -0800727 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
728 usbnet_terminate_urbs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 usb_kill_urb(dev->interrupt);
731
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300732 usbnet_purge_paused_rxq(dev);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* deferred work (task, timer, softirq) must also stop.
735 * can't flush_scheduled_work() until we drop rtnl (later),
736 * else workers could deadlock; so make workers a NOP.
737 */
738 dev->flags = 0;
739 del_timer_sync (&dev->delay);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700740 cancel_work_sync(&dev->bh_w);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800741 if (info->manage_power)
742 info->manage_power(dev, 0);
743 else
744 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 return 0;
747}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000748EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750/*-------------------------------------------------------------------------*/
751
752// posts reads, and enables write queuing
753
754// precondition: never called in_interrupt
755
Stephen Hemminger777baa42009-03-20 19:35:54 +0000756int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200759 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct driver_info *info = dev->driver_info;
761
Oliver Neukuma11a6542007-08-03 13:52:19 +0200762 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000763 netif_info(dev, ifup, dev->net,
764 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
765 retval,
766 dev->udev->bus->bus_name,
767 dev->udev->devpath,
768 info->description);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200769 goto done_nopm;
770 }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 // put into "known safe" state
773 if (info->reset && (retval = info->reset (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000774 netif_info(dev, ifup, dev->net,
775 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
776 retval,
777 dev->udev->bus->bus_name,
778 dev->udev->devpath,
779 info->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto done;
781 }
782
783 // insist peer be connected
784 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000785 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto done;
787 }
788
789 /* start any status interrupt transfer */
790 if (dev->interrupt) {
791 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
792 if (retval < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000793 netif_err(dev, ifup, dev->net,
794 "intr submit %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto done;
796 }
797 }
798
Paul Stewart68972ef2011-04-28 05:43:37 +0000799 set_bit(EVENT_DEV_OPEN, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 netif_start_queue (net);
Joe Perchesa475f602010-02-17 10:30:24 +0000801 netif_info(dev, ifup, dev->net,
802 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
803 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
804 dev->net->mtu,
805 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
806 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
807 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
808 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
809 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
810 "simple");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 // delay posting reads until we're fully open
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700813 queue_work(usbnet_wq, &dev->bh_w);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800814 if (info->manage_power) {
815 retval = info->manage_power(dev, 1);
816 if (retval < 0)
817 goto done;
818 usb_autopm_put_interface(dev->intf);
819 }
Oliver Neukuma11a6542007-08-03 13:52:19 +0200820 return retval;
Joe Perchesa475f602010-02-17 10:30:24 +0000821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200823 usb_autopm_put_interface(dev->intf);
824done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return retval;
826}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000827EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829/*-------------------------------------------------------------------------*/
830
David Brownell2e55cc72005-08-31 09:53:10 -0700831/* ethtool methods; minidrivers may need to add some more, but
832 * they'll probably want to use this base set.
833 */
834
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200835int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
836{
837 struct usbnet *dev = netdev_priv(net);
838
839 if (!dev->mii.mdio_read)
840 return -EOPNOTSUPP;
841
842 return mii_ethtool_gset(&dev->mii, cmd);
843}
844EXPORT_SYMBOL_GPL(usbnet_get_settings);
845
846int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
847{
848 struct usbnet *dev = netdev_priv(net);
849 int retval;
850
851 if (!dev->mii.mdio_write)
852 return -EOPNOTSUPP;
853
854 retval = mii_ethtool_sset(&dev->mii, cmd);
855
856 /* link speed/duplex might have changed */
857 if (dev->driver_info->link_reset)
858 dev->driver_info->link_reset(dev);
859
860 return retval;
861
862}
863EXPORT_SYMBOL_GPL(usbnet_set_settings);
864
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200865u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct usbnet *dev = netdev_priv(net);
868
David Brownellf29fc252005-08-31 09:52:31 -0700869 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (dev->driver_info->check_connect)
871 return dev->driver_info->check_connect (dev) == 0;
872
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200873 /* if the device has mii operations, use those */
874 if (dev->mii.mdio_read)
875 return mii_link_ok(&dev->mii);
876
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800877 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
878 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200880EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
David Brownell18ee91fa2006-11-02 12:29:12 -0800882int usbnet_nway_reset(struct net_device *net)
883{
884 struct usbnet *dev = netdev_priv(net);
885
886 if (!dev->mii.mdio_write)
887 return -EOPNOTSUPP;
888
889 return mii_nway_restart(&dev->mii);
890}
891EXPORT_SYMBOL_GPL(usbnet_nway_reset);
892
David Brownell18ee91fa2006-11-02 12:29:12 -0800893void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
894{
895 struct usbnet *dev = netdev_priv(net);
896
David Brownell296c0242007-04-17 16:10:10 -0700897 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800898 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
899 strncpy (info->fw_version, dev->driver_info->description,
900 sizeof info->fw_version);
901 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
902}
903EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
904
David Brownell2e55cc72005-08-31 09:53:10 -0700905u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct usbnet *dev = netdev_priv(net);
908
909 return dev->msg_enable;
910}
David Brownell2e55cc72005-08-31 09:53:10 -0700911EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
David Brownell2e55cc72005-08-31 09:53:10 -0700913void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct usbnet *dev = netdev_priv(net);
916
917 dev->msg_enable = level;
918}
David Brownell2e55cc72005-08-31 09:53:10 -0700919EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
David Brownell2e55cc72005-08-31 09:53:10 -0700921/* drivers may override default ethtool_ops in their bind() routine */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700922static const struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200923 .get_settings = usbnet_get_settings,
924 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700925 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200926 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800927 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700928 .get_msglevel = usbnet_get_msglevel,
929 .set_msglevel = usbnet_set_msglevel,
930};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932/*-------------------------------------------------------------------------*/
933
934/* work that cannot be done in interrupt context uses keventd.
935 *
936 * NOTE: with 2.5 we could do more of this using completion callbacks,
937 * especially now that control transfers can be queued.
938 */
939static void
David Howellsc4028952006-11-22 14:57:56 +0000940kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
David Howellsc4028952006-11-22 14:57:56 +0000942 struct usbnet *dev =
943 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int status;
945
946 /* usb_clear_halt() needs a thread context */
947 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
948 unlink_urbs (dev, &dev->txq);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800949 status = usb_autopm_get_interface(dev->intf);
950 if (status < 0)
951 goto fail_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 status = usb_clear_halt (dev->udev, dev->out);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800953 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +0000954 if (status < 0 &&
955 status != -EPIPE &&
956 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (netif_msg_tx_err (dev))
Oliver Neukum69ee4722009-12-03 15:31:18 -0800958fail_pipe:
Joe Perches60b86752010-02-17 10:30:23 +0000959 netdev_err(dev->net, "can't clear tx halt, status %d\n",
960 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 } else {
962 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700963 if (status != -ESHUTDOWN)
964 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966 }
967 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
968 unlink_urbs (dev, &dev->rxq);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800969 status = usb_autopm_get_interface(dev->intf);
970 if (status < 0)
971 goto fail_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 status = usb_clear_halt (dev->udev, dev->in);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800973 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +0000974 if (status < 0 &&
975 status != -EPIPE &&
976 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (netif_msg_rx_err (dev))
Oliver Neukum69ee4722009-12-03 15:31:18 -0800978fail_halt:
Joe Perches60b86752010-02-17 10:30:23 +0000979 netdev_err(dev->net, "can't clear rx halt, status %d\n",
980 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 } else {
982 clear_bit (EVENT_RX_HALT, &dev->flags);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -0700983 queue_work(usbnet_wq, &dev->bh_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 }
986
987 /* tasklet could resubmit itself forever if memory is tight */
988 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
989 struct urb *urb = NULL;
David S. Millerdacb3972010-08-10 02:50:55 -0700990 int resched = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (netif_running (dev->net))
993 urb = usb_alloc_urb (0, GFP_KERNEL);
994 else
995 clear_bit (EVENT_RX_MEMORY, &dev->flags);
996 if (urb != NULL) {
997 clear_bit (EVENT_RX_MEMORY, &dev->flags);
Oliver Neukum69ee4722009-12-03 15:31:18 -0800998 status = usb_autopm_get_interface(dev->intf);
Jesper Juhlab607072011-02-10 10:58:45 +0000999 if (status < 0) {
1000 usb_free_urb(urb);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001001 goto fail_lowmem;
Jesper Juhlab607072011-02-10 10:58:45 +00001002 }
David S. Millerdacb3972010-08-10 02:50:55 -07001003 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1004 resched = 0;
Oliver Neukum69ee4722009-12-03 15:31:18 -08001005 usb_autopm_put_interface(dev->intf);
1006fail_lowmem:
David S. Millerdacb3972010-08-10 02:50:55 -07001007 if (resched)
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001008 queue_work(usbnet_wq, &dev->bh_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010 }
1011
David Hollis7ea13c92005-04-22 15:07:02 -07001012 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -08001013 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -07001014 int retval = 0;
1015
1016 clear_bit (EVENT_LINK_RESET, &dev->flags);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001017 status = usb_autopm_get_interface(dev->intf);
1018 if (status < 0)
1019 goto skip_reset;
David Hollis7ea13c92005-04-22 15:07:02 -07001020 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
Oliver Neukum69ee4722009-12-03 15:31:18 -08001021 usb_autopm_put_interface(dev->intf);
1022skip_reset:
Joe Perches60b86752010-02-17 10:30:23 +00001023 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1024 retval,
1025 dev->udev->bus->bus_name,
1026 dev->udev->devpath,
1027 info->description);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001028 } else {
1029 usb_autopm_put_interface(dev->intf);
David Hollis7ea13c92005-04-22 15:07:02 -07001030 }
1031 }
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (dev->flags)
Joe Perches60b86752010-02-17 10:30:23 +00001034 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/*-------------------------------------------------------------------------*/
1038
David Howells7d12e782006-10-05 14:55:46 +01001039static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct sk_buff *skb = (struct sk_buff *) urb->context;
1042 struct skb_data *entry = (struct skb_data *) skb->cb;
1043 struct usbnet *dev = entry->dev;
1044
1045 if (urb->status == 0) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001046 if (!(dev->driver_info->flags & FLAG_MULTI_PACKET))
1047 dev->net->stats.tx_packets++;
Herbert Xu79638372009-06-29 16:53:28 +00001048 dev->net->stats.tx_bytes += entry->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 } else {
Herbert Xu79638372009-06-29 16:53:28 +00001050 dev->net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 switch (urb->status) {
1053 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -07001054 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056
1057 /* software-driven interface shutdown */
1058 case -ECONNRESET: // async unlink
1059 case -ESHUTDOWN: // hardware gone
1060 break;
1061
1062 // like rx, tx gets controller i/o faults during khubd delays
1063 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -07001064 case -EPROTO:
1065 case -ETIME:
1066 case -EILSEQ:
Oliver Neukum69ee4722009-12-03 15:31:18 -08001067 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!timer_pending (&dev->delay)) {
1069 mod_timer (&dev->delay,
1070 jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +00001071 netif_dbg(dev, link, dev->net,
1072 "tx throttle %d\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074 netif_stop_queue (dev->net);
1075 break;
1076 default:
Joe Perchesa475f602010-02-17 10:30:24 +00001077 netif_dbg(dev, tx_err, dev->net,
1078 "tx err %d\n", entry->urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 break;
1080 }
1081 }
1082
Oliver Neukum69ee4722009-12-03 15:31:18 -08001083 usb_autopm_put_interface_async(dev->intf);
Ming Lei5b6e9bc2012-04-26 11:33:46 +08001084 (void) defer_bh(dev, skb, &dev->txq, tx_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087/*-------------------------------------------------------------------------*/
1088
Stephen Hemminger777baa42009-03-20 19:35:54 +00001089void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct usbnet *dev = netdev_priv(net);
1092
1093 unlink_urbs (dev, &dev->txq);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001094 queue_work(usbnet_wq, &dev->bh_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 // FIXME: device recovery -- reset?
1097}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001098EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100/*-------------------------------------------------------------------------*/
1101
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001102netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1103 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 struct usbnet *dev = netdev_priv(net);
1106 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct urb *urb = NULL;
1108 struct skb_data *entry;
1109 struct driver_info *info = dev->driver_info;
1110 unsigned long flags;
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001111 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Konstantin Khlebnikov23ba0792011-11-07 05:54:58 +00001113 if (skb)
1114 skb_tx_timestamp(skb);
Michael Rieschf9b491e2011-09-29 04:06:26 +00001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 // some devices want funky USB-level framing, for
1117 // win32 driver (usually) and/or hardware quirks
1118 if (info->tx_fixup) {
1119 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1120 if (!skb) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001121 if (netif_msg_tx_err(dev)) {
1122 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1123 goto drop;
1124 } else {
1125 /* cdc_ncm collected packet; waits for more */
1126 goto not_drop;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 }
1130 length = skb->len;
1131
1132 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001133 netif_dbg(dev, tx_err, dev->net, "no urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto drop;
1135 }
1136
1137 entry = (struct skb_data *) skb->cb;
1138 entry->urb = urb;
1139 entry->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 entry->length = length;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1143 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /* don't assume the hardware handles USB_ZERO_PACKET
1146 * NOTE: strictly conforming cdc-ether devices should expect
1147 * the ZLP here, but ignore the one-byte packet.
Alexey Orishko073285f2010-11-29 23:23:27 +00001148 * NOTE2: CDC NCM specification is different from CDC ECM when
1149 * handling ZLP/short packets, so cdc_ncm driver will make short
1150 * packet itself if needed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Elina Pashevab4d562e2010-04-06 14:23:07 +00001152 if (length % dev->maxpacket == 0) {
1153 if (!(info->flags & FLAG_SEND_ZLP)) {
Alexey Orishko073285f2010-11-29 23:23:27 +00001154 if (!(info->flags & FLAG_MULTI_PACKET)) {
1155 urb->transfer_buffer_length++;
1156 if (skb_tailroom(skb)) {
1157 skb->data[skb->len] = 0;
1158 __skb_put(skb, 1);
1159 }
Elina Pashevab4d562e2010-04-06 14:23:07 +00001160 }
1161 } else
1162 urb->transfer_flags |= URB_ZERO_PACKET;
Peter Korsgaard3e323f32007-06-27 08:48:15 +02001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Oliver Neukum69ee4722009-12-03 15:31:18 -08001165 spin_lock_irqsave(&dev->txq.lock, flags);
1166 retval = usb_autopm_get_interface_async(dev->intf);
1167 if (retval < 0) {
1168 spin_unlock_irqrestore(&dev->txq.lock, flags);
1169 goto drop;
1170 }
1171
1172#ifdef CONFIG_PM
1173 /* if this triggers the device is still a sleep */
1174 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1175 /* transmission will be done in resume */
1176 usb_anchor_urb(urb, &dev->deferred);
1177 /* no use to process more packets */
1178 netif_stop_queue(net);
Hemant Kumarfa476f72012-10-09 16:22:18 -07001179 usb_put_urb(urb);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001180 spin_unlock_irqrestore(&dev->txq.lock, flags);
Joe Perches60b86752010-02-17 10:30:23 +00001181 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
Oliver Neukum69ee4722009-12-03 15:31:18 -08001182 goto deferred;
1183 }
1184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1187 case -EPIPE:
1188 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001189 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001190 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
1192 default:
Oliver Neukum69ee4722009-12-03 15:31:18 -08001193 usb_autopm_put_interface_async(dev->intf);
Joe Perchesa475f602010-02-17 10:30:24 +00001194 netif_dbg(dev, tx_err, dev->net,
1195 "tx: submit urb err %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197 case 0:
1198 net->trans_start = jiffies;
Ming Lei5b6e9bc2012-04-26 11:33:46 +08001199 __usbnet_queue_skb(&dev->txq, skb, tx_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (dev->txq.qlen >= TX_QLEN (dev))
1201 netif_stop_queue (net);
1202 }
1203 spin_unlock_irqrestore (&dev->txq.lock, flags);
1204
1205 if (retval) {
Joe Perchesa475f602010-02-17 10:30:24 +00001206 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207drop:
Herbert Xu79638372009-06-29 16:53:28 +00001208 dev->net->stats.tx_dropped++;
Alexey Orishko073285f2010-11-29 23:23:27 +00001209not_drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (skb)
1211 dev_kfree_skb_any (skb);
1212 usb_free_urb (urb);
Joe Perchesa475f602010-02-17 10:30:24 +00001213 } else
1214 netif_dbg(dev, tx_queued, dev->net,
1215 "> tx, len %d, type 0x%x\n", length, skb->protocol);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001216#ifdef CONFIG_PM
1217deferred:
1218#endif
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001219 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001221EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223/*-------------------------------------------------------------------------*/
1224
1225// tasklet (work deferred from completions, in_irq) or timer
1226
1227static void usbnet_bh (unsigned long param)
1228{
1229 struct usbnet *dev = (struct usbnet *) param;
1230 struct sk_buff *skb;
1231 struct skb_data *entry;
1232
1233 while ((skb = skb_dequeue (&dev->done))) {
1234 entry = (struct skb_data *) skb->cb;
1235 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001236 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 entry->state = rx_cleanup;
1238 rx_process (dev, skb);
1239 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001240 case tx_done:
1241 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 usb_free_urb (entry->urb);
1243 dev_kfree_skb (skb);
1244 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001245 default:
Joe Perches60b86752010-02-17 10:30:23 +00001246 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248 }
1249
1250 // waiting for all pending urbs to complete?
1251 if (dev->wait) {
1252 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
Hemant Kumaref59a422012-08-15 16:57:00 -07001253 wake_up(&unlink_wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
1256 // or are we maybe short a few urbs?
Joe Perches8e95a202009-12-03 07:58:21 +00001257 } else if (netif_running (dev->net) &&
1258 netif_device_present (dev->net) &&
1259 !timer_pending (&dev->delay) &&
1260 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int temp = dev->rxq.qlen;
1262 int qlen = RX_QLEN (dev);
1263
1264 if (temp < qlen) {
1265 struct urb *urb;
1266 int i;
1267
1268 // don't refill the queue all at once
1269 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1270 urb = usb_alloc_urb (0, GFP_ATOMIC);
David S. Millerdacb3972010-08-10 02:50:55 -07001271 if (urb != NULL) {
1272 if (rx_submit (dev, urb, GFP_ATOMIC) ==
1273 -ENOLINK)
1274 return;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Joe Perchesa475f602010-02-17 10:30:24 +00001277 if (temp != dev->rxq.qlen)
1278 netif_dbg(dev, link, dev->net,
1279 "rxqlen %d --> %d\n",
1280 temp, dev->rxq.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (dev->rxq.qlen < qlen)
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001282 queue_work(usbnet_wq, &dev->bh_w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 if (dev->txq.qlen < TX_QLEN (dev))
1285 netif_wake_queue (dev->net);
1286 }
1287}
1288
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001289static void usbnet_bh_w(struct work_struct *work)
1290{
1291 struct usbnet *dev =
1292 container_of(work, struct usbnet, bh_w);
1293 unsigned long param = (unsigned long)dev;
1294
1295 usbnet_bh(param);
1296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298/*-------------------------------------------------------------------------
1299 *
1300 * USB Device Driver support
1301 *
1302 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304// precondition: never called in_interrupt
1305
David Brownell38bde1d2005-08-31 09:52:45 -07001306void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct usbnet *dev;
1309 struct usb_device *xdev;
1310 struct net_device *net;
1311
1312 dev = usb_get_intfdata(intf);
1313 usb_set_intfdata(intf, NULL);
1314 if (!dev)
1315 return;
1316
1317 xdev = interface_to_usbdev (intf);
1318
Joe Perchesa475f602010-02-17 10:30:24 +00001319 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1320 intf->dev.driver->name,
1321 xdev->bus->bus_name, xdev->devpath,
1322 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 net = dev->net;
1325 unregister_netdev (net);
1326
Tejun Heo23f333a2010-12-12 16:45:14 +01001327 cancel_work_sync(&dev->kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Hemant Kumarfa476f72012-10-09 16:22:18 -07001329 usb_scuttle_anchored_urbs(&dev->deferred);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (dev->driver_info->unbind)
1332 dev->driver_info->unbind (dev, intf);
1333
Paul Stewart68972ef2011-04-28 05:43:37 +00001334 usb_kill_urb(dev->interrupt);
1335 usb_free_urb(dev->interrupt);
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 free_netdev(net);
1338 usb_put_dev (xdev);
1339}
David Brownell38bde1d2005-08-31 09:52:45 -07001340EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Stephen Hemminger777baa42009-03-20 19:35:54 +00001342static const struct net_device_ops usbnet_netdev_ops = {
1343 .ndo_open = usbnet_open,
1344 .ndo_stop = usbnet_stop,
1345 .ndo_start_xmit = usbnet_start_xmit,
1346 .ndo_tx_timeout = usbnet_tx_timeout,
1347 .ndo_change_mtu = usbnet_change_mtu,
1348 .ndo_set_mac_address = eth_mac_addr,
1349 .ndo_validate_addr = eth_validate_addr,
1350};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352/*-------------------------------------------------------------------------*/
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354// precondition: never called in_interrupt
1355
Marcel Holtmann225794f2009-10-02 05:15:26 +00001356static struct device_type wlan_type = {
1357 .name = "wlan",
1358};
1359
1360static struct device_type wwan_type = {
1361 .name = "wwan",
1362};
1363
David Brownell38bde1d2005-08-31 09:52:45 -07001364int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1366{
1367 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001368 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct usb_host_interface *interface;
1370 struct driver_info *info;
1371 struct usb_device *xdev;
1372 int status;
David Brownell296c0242007-04-17 16:10:10 -07001373 const char *name;
Ming Leib0786b42010-11-01 07:11:54 -07001374 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1375
1376 /* usbnet already took usb runtime pm, so have to enable the feature
1377 * for usb interface, otherwise usb_autopm_get_interface may return
1378 * failure if USB_SUSPEND(RUNTIME_PM) is enabled.
1379 */
1380 if (!driver->supports_autosuspend) {
1381 driver->supports_autosuspend = 1;
1382 pm_runtime_enable(&udev->dev);
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
David Brownell296c0242007-04-17 16:10:10 -07001385 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 info = (struct driver_info *) prod->driver_info;
1387 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001388 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return -ENODEV;
1390 }
1391 xdev = interface_to_usbdev (udev);
1392 interface = udev->cur_altsetting;
1393
1394 usb_get_dev (xdev);
1395
1396 status = -ENOMEM;
1397
1398 // set up our own records
1399 net = alloc_etherdev(sizeof(*dev));
Joe Perches41de8d42012-01-29 13:47:52 +00001400 if (!net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Ben Hutchings0dacca72010-07-02 21:49:02 -07001403 /* netdev_printk() needs this so do it as early as possible */
1404 SET_NETDEV_DEV(net, &udev->dev);
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 dev = netdev_priv(net);
1407 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001408 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001410 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1412 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1413 skb_queue_head_init (&dev->rxq);
1414 skb_queue_head_init (&dev->txq);
1415 skb_queue_head_init (&dev->done);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +03001416 skb_queue_head_init(&dev->rxq_pause);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001417 INIT_WORK(&dev->bh_w, usbnet_bh_w);
David Howellsc4028952006-11-22 14:57:56 +00001418 INIT_WORK (&dev->kevent, kevent);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001419 init_usb_anchor(&dev->deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 dev->delay.function = usbnet_bh;
1421 dev->delay.data = (unsigned long) dev;
1422 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001423 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 dev->net = net;
1426 strcpy (net->name, "usb%d");
1427 memcpy (net->dev_addr, node_id, sizeof node_id);
1428
David Brownell2e55cc72005-08-31 09:53:10 -07001429 /* rx and tx sides can use different message sizes;
1430 * bind() should set rx_urb_size in that case.
1431 */
1432 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433#if 0
1434// dma_supported() is deeply broken on almost all architectures
1435 // possible with some EHCI controllers
Yang Hongyang6a355282009-04-06 19:01:13 -07001436 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 net->features |= NETIF_F_HIGHDMA;
1438#endif
1439
Stephen Hemminger777baa42009-03-20 19:35:54 +00001440 net->netdev_ops = &usbnet_netdev_ops;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001441 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 net->ethtool_ops = &usbnet_ethtool_ops;
1443
1444 // allow device-specific bind/init procedures
1445 // NOTE net->name still not usable ...
1446 if (info->bind) {
1447 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001448 if (status < 0)
1449 goto out1;
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 // heuristic: "usb%d" for links we know are two-host,
1452 // else "eth%d" when there's reasonable doubt. userspace
1453 // can rename the link if it knows better.
Joe Perches8e95a202009-12-03 07:58:21 +00001454 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
Arnd Bergmannc2613442011-04-01 20:12:02 -07001455 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1456 (net->dev_addr [0] & 0x02) == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001458 /* WLAN devices should always be named "wlan%d" */
1459 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1460 strcpy(net->name, "wlan%d");
Marcel Holtmanne1e499e2009-10-02 05:15:25 +00001461 /* WWAN devices should always be named "wwan%d" */
1462 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1463 strcpy(net->name, "wwan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001464
1465 /* maybe the remote can't receive an Ethernet MTU */
1466 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1467 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001468 } else if (!info->in || !info->out)
1469 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 else {
1471 dev->in = usb_rcvbulkpipe (xdev, info->in);
1472 dev->out = usb_sndbulkpipe (xdev, info->out);
1473 if (!(info->flags & FLAG_NO_SETINT))
1474 status = usb_set_interface (xdev,
1475 interface->desc.bInterfaceNumber,
1476 interface->desc.bAlternateSetting);
1477 else
1478 status = 0;
1479
1480 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001481 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 status = init_status (dev, udev);
1483 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001484 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
David Brownell2e55cc72005-08-31 09:53:10 -07001486 if (!dev->rx_urb_size)
1487 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001489
Marcel Holtmann225794f2009-10-02 05:15:26 +00001490 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1491 SET_NETDEV_DEVTYPE(net, &wlan_type);
1492 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1493 SET_NETDEV_DEVTYPE(net, &wwan_type);
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 status = register_netdev (net);
1496 if (status)
tom.leiming@gmail.coma4723842012-04-29 22:51:03 +00001497 goto out4;
Joe Perchesa475f602010-02-17 10:30:24 +00001498 netif_info(dev, probe, dev->net,
1499 "register '%s' at usb-%s-%s, %s, %pM\n",
1500 udev->dev.driver->name,
1501 xdev->bus->bus_name, xdev->devpath,
1502 dev->driver_info->description,
1503 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 // ok, it's ready to go.
1506 usb_set_intfdata (udev, dev);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 netif_device_attach (net);
1509
Ben Hutchings37e82732009-11-04 15:29:52 +00001510 if (dev->driver_info->flags & FLAG_LINK_INTR)
1511 netif_carrier_off(net);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514
tom.leiming@gmail.coma4723842012-04-29 22:51:03 +00001515out4:
1516 usb_free_urb(dev->interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517out3:
1518 if (info->unbind)
1519 info->unbind (dev, udev);
1520out1:
1521 free_netdev(net);
1522out:
1523 usb_put_dev(xdev);
1524 return status;
1525}
David Brownell38bde1d2005-08-31 09:52:45 -07001526EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528/*-------------------------------------------------------------------------*/
1529
Oliver Neukum36433122007-04-30 01:37:44 -07001530/*
1531 * suspend the whole driver as soon as the first interface is suspended
1532 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
David Brownell38bde1d2005-08-31 09:52:45 -07001535int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001538
Oliver Neukum36433122007-04-30 01:37:44 -07001539 if (!dev->suspend_count++) {
Oliver Neukum69ee4722009-12-03 15:31:18 -08001540 spin_lock_irq(&dev->txq.lock);
1541 /* don't autosuspend while transmitting */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001542 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
Ming Lei2038ae22012-06-19 21:15:52 +00001543 dev->suspend_count--;
Oliver Neukum69ee4722009-12-03 15:31:18 -08001544 spin_unlock_irq(&dev->txq.lock);
1545 return -EBUSY;
1546 } else {
1547 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1548 spin_unlock_irq(&dev->txq.lock);
1549 }
Oliver Neukuma11a6542007-08-03 13:52:19 +02001550 /*
1551 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001552 * having everything error out.
1553 */
1554 netif_device_detach (dev->net);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001555 usbnet_terminate_urbs(dev);
1556 usb_kill_urb(dev->interrupt);
1557
Oliver Neukuma11a6542007-08-03 13:52:19 +02001558 /*
1559 * reattach so runtime management can use and
1560 * wake the device
1561 */
1562 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 return 0;
1565}
David Brownell38bde1d2005-08-31 09:52:45 -07001566EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
David Brownell38bde1d2005-08-31 09:52:45 -07001568int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct usbnet *dev = usb_get_intfdata(intf);
Oliver Neukum69ee4722009-12-03 15:31:18 -08001571 struct sk_buff *skb;
1572 struct urb *res;
1573 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Oliver Neukum69ee4722009-12-03 15:31:18 -08001575 if (!--dev->suspend_count) {
Paul Stewart68972ef2011-04-28 05:43:37 +00001576 /* resume interrupt URBs */
1577 if (dev->interrupt && test_bit(EVENT_DEV_OPEN, &dev->flags))
1578 usb_submit_urb(dev->interrupt, GFP_NOIO);
1579
Oliver Neukum69ee4722009-12-03 15:31:18 -08001580 spin_lock_irq(&dev->txq.lock);
1581 while ((res = usb_get_from_anchor(&dev->deferred))) {
1582
Oliver Neukum69ee4722009-12-03 15:31:18 -08001583 skb = (struct sk_buff *)res->context;
1584 retval = usb_submit_urb(res, GFP_ATOMIC);
1585 if (retval < 0) {
1586 dev_kfree_skb_any(skb);
1587 usb_free_urb(res);
1588 usb_autopm_put_interface_async(dev->intf);
1589 } else {
1590 dev->net->trans_start = jiffies;
1591 __skb_queue_tail(&dev->txq, skb);
1592 }
1593 }
1594
1595 smp_mb();
1596 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1597 spin_unlock_irq(&dev->txq.lock);
Ming Lei75bd0cb2011-04-28 22:37:09 +00001598
1599 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
1600 if (!(dev->txq.qlen >= TX_QLEN(dev)))
Alexey Orishko1aa9bc52012-03-14 04:00:24 +00001601 netif_tx_wake_all_queues(dev->net);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001602 queue_work(usbnet_wq, &dev->bh_w);
Ming Lei75bd0cb2011-04-28 22:37:09 +00001603 }
Oliver Neukum69ee4722009-12-03 15:31:18 -08001604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return 0;
1606}
David Brownell38bde1d2005-08-31 09:52:45 -07001607EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610/*-------------------------------------------------------------------------*/
1611
David Brownellf29fc252005-08-31 09:52:31 -07001612static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Thiago Farinac582a952011-04-17 17:49:21 -07001614 /* Compiler should optimize this out. */
1615 BUILD_BUG_ON(
1616 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 random_ether_addr(node_id);
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001619
1620 usbnet_wq = create_singlethread_workqueue("usbnet");
1621 if (!usbnet_wq) {
1622 pr_err("%s: Unable to create workqueue:usbnet\n", __func__);
1623 return -ENOMEM;
1624 }
1625
David Brownellcb1cebb2007-02-15 18:52:30 -08001626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
David Brownellf29fc252005-08-31 09:52:31 -07001628module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
David Brownellf29fc252005-08-31 09:52:31 -07001630static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Hemant Kumar1e8ddb52012-07-02 13:39:22 -07001632 destroy_workqueue(usbnet_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
David Brownellf29fc252005-08-31 09:52:31 -07001634module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
David Brownellf29fc252005-08-31 09:52:31 -07001636MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001637MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001638MODULE_LICENSE("GPL");