blob: 7177abc78dc6b664fbe722a8bb18263ad7c3d1b1 [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>
David Brownellf29fc252005-08-31 09:52:31 -070047
48#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50
51/*-------------------------------------------------------------------------*/
52
53/*
54 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
55 * Several dozen bytes of IPv4 data can fit in two such transactions.
56 * One maximum size Ethernet packet takes twenty four of them.
57 * For high speed, each frame comfortably fits almost 36 max size
58 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070059 *
60 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
61 * let the USB host controller be busy for 5msec or more before an irq
62 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Jamie Paintera99c1942006-07-27 14:17:28 -040064#define RX_MAX_QUEUE_MEMORY (60 * 1518)
65#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
66 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
67#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
68 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
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;
110 * ignore other endpoints and altsetttings.
111 */
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 +0000161static u8 nibble(unsigned char c)
162{
163 if (likely(isdigit(c)))
164 return c - '0';
165 c = toupper(c);
166 if (likely(isxdigit(c)))
167 return 10 + c - 'A';
168 return 0;
169}
170
171int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
172{
173 int tmp, i;
174 unsigned char buf [13];
175
176 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
177 if (tmp != 12) {
178 dev_dbg(&dev->udev->dev,
179 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
180 if (tmp >= 0)
181 tmp = -EINVAL;
182 return tmp;
183 }
184 for (i = tmp = 0; i < 6; i++, tmp += 2)
185 dev->net->dev_addr [i] =
186 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
187 return 0;
188}
189EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
190
David Howells7d12e782006-10-05 14:55:46 +0100191static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193static int init_status (struct usbnet *dev, struct usb_interface *intf)
194{
195 char *buf = NULL;
196 unsigned pipe = 0;
197 unsigned maxp;
198 unsigned period;
199
200 if (!dev->driver_info->status)
201 return 0;
202
203 pipe = usb_rcvintpipe (dev->udev,
204 dev->status->desc.bEndpointAddress
205 & USB_ENDPOINT_NUMBER_MASK);
206 maxp = usb_maxpacket (dev->udev, pipe, 0);
207
208 /* avoid 1 msec chatter: min 8 msec poll rate */
209 period = max ((int) dev->status->desc.bInterval,
210 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
211
Christoph Lametere94b1762006-12-06 20:33:17 -0800212 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800214 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!dev->interrupt) {
216 kfree (buf);
217 return -ENOMEM;
218 } else {
219 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
220 buf, maxp, intr_complete, dev, period);
221 dev_dbg(&intf->dev,
222 "status ep%din, %d bytes period %d\n",
223 usb_pipeendpoint(pipe), maxp, period);
224 }
225 }
David Brownell18ab4582007-05-25 12:31:32 -0700226 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
David Brownell2e55cc72005-08-31 09:53:10 -0700229/* Passes this packet up the stack, updating its accounting.
230 * Some link protocols batch packets, so their rx_fixup paths
231 * can return clones as well as just modify the original skb.
232 */
233void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int status;
236
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300237 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
238 skb_queue_tail(&dev->rxq_pause, skb);
239 return;
240 }
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 skb->protocol = eth_type_trans (skb, dev->net);
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));
249 status = netif_rx (skb);
Joe Perchesa475f602010-02-17 10:30:24 +0000250 if (status != NET_RX_SUCCESS)
251 netif_dbg(dev, rx_err, dev->net,
252 "netif_rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
David Brownell2e55cc72005-08-31 09:53:10 -0700254EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*-------------------------------------------------------------------------
258 *
259 * Network Device Driver (peer link to "Host Device", from USB host)
260 *
261 *-------------------------------------------------------------------------*/
262
Stephen Hemminger777baa42009-03-20 19:35:54 +0000263int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700266 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400267 int old_hard_mtu = dev->hard_mtu;
268 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jamie Paintera99c1942006-07-27 14:17:28 -0400270 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700273 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -EDOM;
275 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400276
277 dev->hard_mtu = net->mtu + net->hard_header_len;
278 if (dev->rx_urb_size == old_hard_mtu) {
279 dev->rx_urb_size = dev->hard_mtu;
280 if (dev->rx_urb_size > old_rx_urb_size)
281 usbnet_unlink_rx_urbs(dev);
282 }
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000286EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/*-------------------------------------------------------------------------*/
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
291 * completion callbacks. 2.5 should have fixed those bugs...
292 */
293
David S. Miller8728b832005-08-09 19:25:21 -0700294static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned long flags;
297
David S. Miller8728b832005-08-09 19:25:21 -0700298 spin_lock_irqsave(&list->lock, flags);
299 __skb_unlink(skb, list);
300 spin_unlock(&list->lock);
301 spin_lock(&dev->done.lock);
302 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700304 tasklet_schedule(&dev->bh);
305 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/* some work can't be done in tasklets, so we use keventd
309 *
310 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
311 * but tasklet_schedule() doesn't. hope the failure is rare.
312 */
David Brownell2e55cc72005-08-31 09:53:10 -0700313void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 set_bit (work, &dev->flags);
316 if (!schedule_work (&dev->kevent))
Joe Perches60b86752010-02-17 10:30:23 +0000317 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 else
Joe Perches60b86752010-02-17 10:30:23 +0000319 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
David Brownell2e55cc72005-08-31 09:53:10 -0700321EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323/*-------------------------------------------------------------------------*/
324
David Howells7d12e782006-10-05 14:55:46 +0100325static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Al Viro55016f12005-10-21 03:21:58 -0400327static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct sk_buff *skb;
330 struct skb_data *entry;
331 int retval = 0;
332 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700333 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
Joe Perchesa475f602010-02-17 10:30:24 +0000336 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
David Brownell2e55cc72005-08-31 09:53:10 -0700337 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 usb_free_urb (urb);
339 return;
340 }
341 skb_reserve (skb, NET_IP_ALIGN);
342
343 entry = (struct skb_data *) skb->cb;
344 entry->urb = urb;
345 entry->dev = dev;
346 entry->state = rx_start;
347 entry->length = 0;
348
349 usb_fill_bulk_urb (urb, dev->udev, dev->in,
350 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 spin_lock_irqsave (&dev->rxq.lock, lockflags);
353
Joe Perches8e95a202009-12-03 07:58:21 +0000354 if (netif_running (dev->net) &&
355 netif_device_present (dev->net) &&
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800356 !test_bit (EVENT_RX_HALT, &dev->flags) &&
357 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700358 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700360 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700363 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 case -ENODEV:
Joe Perchesa475f602010-02-17 10:30:24 +0000366 netif_dbg(dev, ifdown, dev->net, "device gone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 netif_device_detach (dev->net);
368 break;
369 default:
Joe Perchesa475f602010-02-17 10:30:24 +0000370 netif_dbg(dev, rx_err, dev->net,
371 "rx submit, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 tasklet_schedule (&dev->bh);
373 break;
374 case 0:
375 __skb_queue_tail (&dev->rxq, skb);
376 }
377 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000378 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 retval = -ENOLINK;
380 }
381 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
382 if (retval) {
383 dev_kfree_skb_any (skb);
384 usb_free_urb (urb);
385 }
386}
387
388
389/*-------------------------------------------------------------------------*/
390
391static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
392{
Joe Perches8e95a202009-12-03 07:58:21 +0000393 if (dev->driver_info->rx_fixup &&
394 !dev->driver_info->rx_fixup (dev, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto error;
396 // else network stack removes extra byte if we forced a short packet
397
398 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700399 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 else {
Joe Perchesa475f602010-02-17 10:30:24 +0000401 netif_dbg(dev, rx_err, dev->net, "drop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402error:
Herbert Xu79638372009-06-29 16:53:28 +0000403 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 skb_queue_tail (&dev->done, skb);
405 }
406}
407
408/*-------------------------------------------------------------------------*/
409
David Howells7d12e782006-10-05 14:55:46 +0100410static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 struct sk_buff *skb = (struct sk_buff *) urb->context;
413 struct skb_data *entry = (struct skb_data *) skb->cb;
414 struct usbnet *dev = entry->dev;
415 int urb_status = urb->status;
416
417 skb_put (skb, urb->actual_length);
418 entry->state = rx_done;
419 entry->urb = NULL;
420
421 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700422 /* success */
423 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700424 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 entry->state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000426 dev->net->stats.rx_errors++;
427 dev->net->stats.rx_length_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000428 netif_dbg(dev, rx_err, dev->net,
429 "rx length %d\n", skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 break;
432
David Brownell18ab4582007-05-25 12:31:32 -0700433 /* stalls need manual reset. this is rare ... except that
434 * when going through USB 2.0 TTs, unplug appears this way.
Jean Delvare3ac49a12009-06-04 16:20:28 +0200435 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
David Brownell18ab4582007-05-25 12:31:32 -0700436 * storm, recovering as needed.
437 */
438 case -EPIPE:
Herbert Xu79638372009-06-29 16:53:28 +0000439 dev->net->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700440 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 // FALLTHROUGH
442
David Brownell18ab4582007-05-25 12:31:32 -0700443 /* software-driven interface shutdown */
444 case -ECONNRESET: /* async unlink */
445 case -ESHUTDOWN: /* hardware gone */
Joe Perchesa475f602010-02-17 10:30:24 +0000446 netif_dbg(dev, ifdown, dev->net,
447 "rx shutdown, code %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto block;
449
David Brownell18ab4582007-05-25 12:31:32 -0700450 /* we get controller i/o faults during khubd disconnect() delays.
451 * throttle down resubmits, to avoid log floods; just temporarily,
452 * so we still recover when the fault isn't a khubd delay.
453 */
454 case -EPROTO:
455 case -ETIME:
456 case -EILSEQ:
Herbert Xu79638372009-06-29 16:53:28 +0000457 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!timer_pending (&dev->delay)) {
459 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +0000460 netif_dbg(dev, link, dev->net,
461 "rx throttle %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463block:
464 entry->state = rx_cleanup;
465 entry->urb = urb;
466 urb = NULL;
467 break;
468
David Brownell18ab4582007-05-25 12:31:32 -0700469 /* data overrun ... flush fifo? */
470 case -EOVERFLOW:
Herbert Xu79638372009-06-29 16:53:28 +0000471 dev->net->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800473
David Brownell18ab4582007-05-25 12:31:32 -0700474 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 entry->state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000476 dev->net->stats.rx_errors++;
Joe Perchesa475f602010-02-17 10:30:24 +0000477 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 }
480
David S. Miller8728b832005-08-09 19:25:21 -0700481 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (urb) {
Joe Perches8e95a202009-12-03 07:58:21 +0000484 if (netif_running (dev->net) &&
485 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 rx_submit (dev, urb, GFP_ATOMIC);
487 return;
488 }
489 usb_free_urb (urb);
490 }
Joe Perchesa475f602010-02-17 10:30:24 +0000491 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
David Howells7d12e782006-10-05 14:55:46 +0100494static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct usbnet *dev = urb->context;
497 int status = urb->status;
498
499 switch (status) {
David Brownell18ab4582007-05-25 12:31:32 -0700500 /* success */
501 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dev->driver_info->status(dev, urb);
503 break;
504
David Brownell18ab4582007-05-25 12:31:32 -0700505 /* software-driven interface shutdown */
506 case -ENOENT: /* urb killed */
507 case -ESHUTDOWN: /* hardware gone */
Joe Perchesa475f602010-02-17 10:30:24 +0000508 netif_dbg(dev, ifdown, dev->net,
509 "intr shutdown, code %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return;
511
David Brownell18ab4582007-05-25 12:31:32 -0700512 /* NOTE: not throttling like RX/TX, since this endpoint
513 * already polls infrequently
514 */
515 default:
Joe Perches60b86752010-02-17 10:30:23 +0000516 netdev_dbg(dev->net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518 }
519
520 if (!netif_running (dev->net))
521 return;
522
523 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
524 status = usb_submit_urb (urb, GFP_ATOMIC);
Joe Perchesa475f602010-02-17 10:30:24 +0000525 if (status != 0)
526 netif_err(dev, timer, dev->net,
527 "intr resubmit --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*-------------------------------------------------------------------------*/
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300531void usbnet_pause_rx(struct usbnet *dev)
532{
533 set_bit(EVENT_RX_PAUSED, &dev->flags);
534
Joe Perchesa475f602010-02-17 10:30:24 +0000535 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300536}
537EXPORT_SYMBOL_GPL(usbnet_pause_rx);
538
539void usbnet_resume_rx(struct usbnet *dev)
540{
541 struct sk_buff *skb;
542 int num = 0;
543
544 clear_bit(EVENT_RX_PAUSED, &dev->flags);
545
546 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
547 usbnet_skb_return(dev, skb);
548 num++;
549 }
550
551 tasklet_schedule(&dev->bh);
552
Joe Perchesa475f602010-02-17 10:30:24 +0000553 netif_dbg(dev, rx_status, dev->net,
554 "paused rx queue disabled, %d skbs requeued\n", num);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300555}
556EXPORT_SYMBOL_GPL(usbnet_resume_rx);
557
558void usbnet_purge_paused_rxq(struct usbnet *dev)
559{
560 skb_queue_purge(&dev->rxq_pause);
561}
562EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
563
564/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566// unlink pending rx/tx; completion handlers do all other cleanup
567
568static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
569{
570 unsigned long flags;
571 struct sk_buff *skb, *skbnext;
572 int count = 0;
573
574 spin_lock_irqsave (&q->lock, flags);
David S. Miller83bfba52008-09-22 20:18:47 -0700575 skb_queue_walk_safe(q, skb, skbnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct skb_data *entry;
577 struct urb *urb;
578 int retval;
579
580 entry = (struct skb_data *) skb->cb;
581 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 // during some PM-driven resume scenarios,
584 // these (async) unlinks complete immediately
585 retval = usb_unlink_urb (urb);
586 if (retval != -EINPROGRESS && retval != 0)
Joe Perches60b86752010-02-17 10:30:23 +0000587 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 else
589 count++;
590 }
591 spin_unlock_irqrestore (&q->lock, flags);
592 return count;
593}
594
Jamie Paintera99c1942006-07-27 14:17:28 -0400595// Flush all pending rx urbs
596// minidrivers may need to do this when the MTU changes
597
598void usbnet_unlink_rx_urbs(struct usbnet *dev)
599{
600 if (netif_running(dev->net)) {
601 (void) unlink_urbs (dev, &dev->rxq);
602 tasklet_schedule(&dev->bh);
603 }
604}
605EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/*-------------------------------------------------------------------------*/
608
609// precondition: never called in_interrupt
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800610static void usbnet_terminate_urbs(struct usbnet *dev)
611{
612 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
613 DECLARE_WAITQUEUE(wait, current);
614 int temp;
615
616 /* ensure there are no more active urbs */
617 add_wait_queue(&unlink_wakeup, &wait);
618 set_current_state(TASK_UNINTERRUPTIBLE);
619 dev->wait = &unlink_wakeup;
620 temp = unlink_urbs(dev, &dev->txq) +
621 unlink_urbs(dev, &dev->rxq);
622
623 /* maybe wait for deletions to finish. */
624 while (!skb_queue_empty(&dev->rxq)
625 && !skb_queue_empty(&dev->txq)
626 && !skb_queue_empty(&dev->done)) {
627 schedule_timeout(UNLINK_TIMEOUT_MS);
628 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perchesa475f602010-02-17 10:30:24 +0000629 netif_dbg(dev, ifdown, dev->net,
630 "waited for %d urb completions\n", temp);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800631 }
632 set_current_state(TASK_RUNNING);
633 dev->wait = NULL;
634 remove_wait_queue(&unlink_wakeup, &wait);
635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Stephen Hemminger777baa42009-03-20 19:35:54 +0000637int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct usbnet *dev = netdev_priv(net);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300640 struct driver_info *info = dev->driver_info;
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300641 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 netif_stop_queue (net);
644
Joe Perchesa475f602010-02-17 10:30:24 +0000645 netif_info(dev, ifdown, dev->net,
646 "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
647 net->stats.rx_packets, net->stats.tx_packets,
648 net->stats.rx_errors, net->stats.tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300650 /* allow minidriver to stop correctly (wireless devices to turn off
651 * radio etc) */
652 if (info->stop) {
653 retval = info->stop(dev);
Joe Perchesa475f602010-02-17 10:30:24 +0000654 if (retval < 0)
655 netif_info(dev, ifdown, dev->net,
656 "stop fail (%d) usbnet usb-%s-%s, %s\n",
657 retval,
658 dev->udev->bus->bus_name, dev->udev->devpath,
659 info->description);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300660 }
661
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800662 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
663 usbnet_terminate_urbs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 usb_kill_urb(dev->interrupt);
666
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300667 usbnet_purge_paused_rxq(dev);
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* deferred work (task, timer, softirq) must also stop.
670 * can't flush_scheduled_work() until we drop rtnl (later),
671 * else workers could deadlock; so make workers a NOP.
672 */
673 dev->flags = 0;
674 del_timer_sync (&dev->delay);
675 tasklet_kill (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800676 if (info->manage_power)
677 info->manage_power(dev, 0);
678 else
679 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return 0;
682}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000683EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685/*-------------------------------------------------------------------------*/
686
687// posts reads, and enables write queuing
688
689// precondition: never called in_interrupt
690
Stephen Hemminger777baa42009-03-20 19:35:54 +0000691int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200694 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct driver_info *info = dev->driver_info;
696
Oliver Neukuma11a6542007-08-03 13:52:19 +0200697 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000698 netif_info(dev, ifup, dev->net,
699 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
700 retval,
701 dev->udev->bus->bus_name,
702 dev->udev->devpath,
703 info->description);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200704 goto done_nopm;
705 }
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 // put into "known safe" state
708 if (info->reset && (retval = info->reset (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000709 netif_info(dev, ifup, dev->net,
710 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
711 retval,
712 dev->udev->bus->bus_name,
713 dev->udev->devpath,
714 info->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto done;
716 }
717
718 // insist peer be connected
719 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000720 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto done;
722 }
723
724 /* start any status interrupt transfer */
725 if (dev->interrupt) {
726 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
727 if (retval < 0) {
Joe Perchesa475f602010-02-17 10:30:24 +0000728 netif_err(dev, ifup, dev->net,
729 "intr submit %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto done;
731 }
732 }
733
734 netif_start_queue (net);
Joe Perchesa475f602010-02-17 10:30:24 +0000735 netif_info(dev, ifup, dev->net,
736 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
737 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
738 dev->net->mtu,
739 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
740 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
741 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
742 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
743 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
744 "simple");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 // delay posting reads until we're fully open
747 tasklet_schedule (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800748 if (info->manage_power) {
749 retval = info->manage_power(dev, 1);
750 if (retval < 0)
751 goto done;
752 usb_autopm_put_interface(dev->intf);
753 }
Oliver Neukuma11a6542007-08-03 13:52:19 +0200754 return retval;
Joe Perchesa475f602010-02-17 10:30:24 +0000755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200757 usb_autopm_put_interface(dev->intf);
758done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return retval;
760}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000761EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763/*-------------------------------------------------------------------------*/
764
David Brownell2e55cc72005-08-31 09:53:10 -0700765/* ethtool methods; minidrivers may need to add some more, but
766 * they'll probably want to use this base set.
767 */
768
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200769int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
770{
771 struct usbnet *dev = netdev_priv(net);
772
773 if (!dev->mii.mdio_read)
774 return -EOPNOTSUPP;
775
776 return mii_ethtool_gset(&dev->mii, cmd);
777}
778EXPORT_SYMBOL_GPL(usbnet_get_settings);
779
780int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
781{
782 struct usbnet *dev = netdev_priv(net);
783 int retval;
784
785 if (!dev->mii.mdio_write)
786 return -EOPNOTSUPP;
787
788 retval = mii_ethtool_sset(&dev->mii, cmd);
789
790 /* link speed/duplex might have changed */
791 if (dev->driver_info->link_reset)
792 dev->driver_info->link_reset(dev);
793
794 return retval;
795
796}
797EXPORT_SYMBOL_GPL(usbnet_set_settings);
798
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200799u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct usbnet *dev = netdev_priv(net);
802
David Brownellf29fc252005-08-31 09:52:31 -0700803 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (dev->driver_info->check_connect)
805 return dev->driver_info->check_connect (dev) == 0;
806
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200807 /* if the device has mii operations, use those */
808 if (dev->mii.mdio_read)
809 return mii_link_ok(&dev->mii);
810
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800811 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
812 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200814EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
David Brownell18ee91fa2006-11-02 12:29:12 -0800816int usbnet_nway_reset(struct net_device *net)
817{
818 struct usbnet *dev = netdev_priv(net);
819
820 if (!dev->mii.mdio_write)
821 return -EOPNOTSUPP;
822
823 return mii_nway_restart(&dev->mii);
824}
825EXPORT_SYMBOL_GPL(usbnet_nway_reset);
826
David Brownell18ee91fa2006-11-02 12:29:12 -0800827void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
828{
829 struct usbnet *dev = netdev_priv(net);
830
David Brownell296c0242007-04-17 16:10:10 -0700831 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800832 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
833 strncpy (info->fw_version, dev->driver_info->description,
834 sizeof info->fw_version);
835 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
836}
837EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
838
David Brownell2e55cc72005-08-31 09:53:10 -0700839u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct usbnet *dev = netdev_priv(net);
842
843 return dev->msg_enable;
844}
David Brownell2e55cc72005-08-31 09:53:10 -0700845EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
David Brownell2e55cc72005-08-31 09:53:10 -0700847void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct usbnet *dev = netdev_priv(net);
850
851 dev->msg_enable = level;
852}
David Brownell2e55cc72005-08-31 09:53:10 -0700853EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
David Brownell2e55cc72005-08-31 09:53:10 -0700855/* drivers may override default ethtool_ops in their bind() routine */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700856static const struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200857 .get_settings = usbnet_get_settings,
858 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700859 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200860 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800861 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700862 .get_msglevel = usbnet_get_msglevel,
863 .set_msglevel = usbnet_set_msglevel,
864};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866/*-------------------------------------------------------------------------*/
867
868/* work that cannot be done in interrupt context uses keventd.
869 *
870 * NOTE: with 2.5 we could do more of this using completion callbacks,
871 * especially now that control transfers can be queued.
872 */
873static void
David Howellsc4028952006-11-22 14:57:56 +0000874kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
David Howellsc4028952006-11-22 14:57:56 +0000876 struct usbnet *dev =
877 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int status;
879
880 /* usb_clear_halt() needs a thread context */
881 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
882 unlink_urbs (dev, &dev->txq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800883 status = usb_autopm_get_interface(dev->intf);
884 if (status < 0)
885 goto fail_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 status = usb_clear_halt (dev->udev, dev->out);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800887 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +0000888 if (status < 0 &&
889 status != -EPIPE &&
890 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (netif_msg_tx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800892fail_pipe:
Joe Perches60b86752010-02-17 10:30:23 +0000893 netdev_err(dev->net, "can't clear tx halt, status %d\n",
894 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 } else {
896 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700897 if (status != -ESHUTDOWN)
898 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 }
901 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
902 unlink_urbs (dev, &dev->rxq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800903 status = usb_autopm_get_interface(dev->intf);
904 if (status < 0)
905 goto fail_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 status = usb_clear_halt (dev->udev, dev->in);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800907 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +0000908 if (status < 0 &&
909 status != -EPIPE &&
910 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (netif_msg_rx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800912fail_halt:
Joe Perches60b86752010-02-17 10:30:23 +0000913 netdev_err(dev->net, "can't clear rx halt, status %d\n",
914 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 } else {
916 clear_bit (EVENT_RX_HALT, &dev->flags);
917 tasklet_schedule (&dev->bh);
918 }
919 }
920
921 /* tasklet could resubmit itself forever if memory is tight */
922 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
923 struct urb *urb = NULL;
924
925 if (netif_running (dev->net))
926 urb = usb_alloc_urb (0, GFP_KERNEL);
927 else
928 clear_bit (EVENT_RX_MEMORY, &dev->flags);
929 if (urb != NULL) {
930 clear_bit (EVENT_RX_MEMORY, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800931 status = usb_autopm_get_interface(dev->intf);
932 if (status < 0)
933 goto fail_lowmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 rx_submit (dev, urb, GFP_KERNEL);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800935 usb_autopm_put_interface(dev->intf);
936fail_lowmem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 tasklet_schedule (&dev->bh);
938 }
939 }
940
David Hollis7ea13c92005-04-22 15:07:02 -0700941 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800942 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -0700943 int retval = 0;
944
945 clear_bit (EVENT_LINK_RESET, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800946 status = usb_autopm_get_interface(dev->intf);
947 if (status < 0)
948 goto skip_reset;
David Hollis7ea13c92005-04-22 15:07:02 -0700949 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800950 usb_autopm_put_interface(dev->intf);
951skip_reset:
Joe Perches60b86752010-02-17 10:30:23 +0000952 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
953 retval,
954 dev->udev->bus->bus_name,
955 dev->udev->devpath,
956 info->description);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800957 } else {
958 usb_autopm_put_interface(dev->intf);
David Hollis7ea13c92005-04-22 15:07:02 -0700959 }
960 }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (dev->flags)
Joe Perches60b86752010-02-17 10:30:23 +0000963 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*-------------------------------------------------------------------------*/
967
David Howells7d12e782006-10-05 14:55:46 +0100968static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct sk_buff *skb = (struct sk_buff *) urb->context;
971 struct skb_data *entry = (struct skb_data *) skb->cb;
972 struct usbnet *dev = entry->dev;
973
974 if (urb->status == 0) {
Herbert Xu79638372009-06-29 16:53:28 +0000975 dev->net->stats.tx_packets++;
976 dev->net->stats.tx_bytes += entry->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 } else {
Herbert Xu79638372009-06-29 16:53:28 +0000978 dev->net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 switch (urb->status) {
981 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700982 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984
985 /* software-driven interface shutdown */
986 case -ECONNRESET: // async unlink
987 case -ESHUTDOWN: // hardware gone
988 break;
989
990 // like rx, tx gets controller i/o faults during khubd delays
991 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700992 case -EPROTO:
993 case -ETIME:
994 case -EILSEQ:
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800995 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!timer_pending (&dev->delay)) {
997 mod_timer (&dev->delay,
998 jiffies + THROTTLE_JIFFIES);
Joe Perchesa475f602010-02-17 10:30:24 +0000999 netif_dbg(dev, link, dev->net,
1000 "tx throttle %d\n", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002 netif_stop_queue (dev->net);
1003 break;
1004 default:
Joe Perchesa475f602010-02-17 10:30:24 +00001005 netif_dbg(dev, tx_err, dev->net,
1006 "tx err %d\n", entry->urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 }
1009 }
1010
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001011 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 urb->dev = NULL;
1013 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -07001014 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*-------------------------------------------------------------------------*/
1018
Stephen Hemminger777baa42009-03-20 19:35:54 +00001019void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct usbnet *dev = netdev_priv(net);
1022
1023 unlink_urbs (dev, &dev->txq);
1024 tasklet_schedule (&dev->bh);
1025
1026 // FIXME: device recovery -- reset?
1027}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001028EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030/*-------------------------------------------------------------------------*/
1031
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001032netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1033 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 struct usbnet *dev = netdev_priv(net);
1036 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct urb *urb = NULL;
1038 struct skb_data *entry;
1039 struct driver_info *info = dev->driver_info;
1040 unsigned long flags;
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001041 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 // some devices want funky USB-level framing, for
1044 // win32 driver (usually) and/or hardware quirks
1045 if (info->tx_fixup) {
1046 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1047 if (!skb) {
Joe Perchesa475f602010-02-17 10:30:24 +00001048 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 goto drop;
1050 }
1051 }
1052 length = skb->len;
1053
1054 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001055 netif_dbg(dev, tx_err, dev->net, "no urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto drop;
1057 }
1058
1059 entry = (struct skb_data *) skb->cb;
1060 entry->urb = urb;
1061 entry->dev = dev;
1062 entry->state = tx_start;
1063 entry->length = length;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1066 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /* don't assume the hardware handles USB_ZERO_PACKET
1069 * NOTE: strictly conforming cdc-ether devices should expect
1070 * the ZLP here, but ignore the one-byte packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Steve Glendinningec475622009-09-22 04:00:27 +00001072 if (!(info->flags & FLAG_SEND_ZLP) && (length % dev->maxpacket) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 urb->transfer_buffer_length++;
Peter Korsgaard3e323f32007-06-27 08:48:15 +02001074 if (skb_tailroom(skb)) {
1075 skb->data[skb->len] = 0;
1076 __skb_put(skb, 1);
1077 }
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001080 spin_lock_irqsave(&dev->txq.lock, flags);
1081 retval = usb_autopm_get_interface_async(dev->intf);
1082 if (retval < 0) {
1083 spin_unlock_irqrestore(&dev->txq.lock, flags);
1084 goto drop;
1085 }
1086
1087#ifdef CONFIG_PM
1088 /* if this triggers the device is still a sleep */
1089 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1090 /* transmission will be done in resume */
1091 usb_anchor_urb(urb, &dev->deferred);
1092 /* no use to process more packets */
1093 netif_stop_queue(net);
1094 spin_unlock_irqrestore(&dev->txq.lock, flags);
Joe Perches60b86752010-02-17 10:30:23 +00001095 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001096 goto deferred;
1097 }
1098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1101 case -EPIPE:
1102 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001103 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001104 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 break;
1106 default:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001107 usb_autopm_put_interface_async(dev->intf);
Joe Perchesa475f602010-02-17 10:30:24 +00001108 netif_dbg(dev, tx_err, dev->net,
1109 "tx: submit urb err %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 break;
1111 case 0:
1112 net->trans_start = jiffies;
1113 __skb_queue_tail (&dev->txq, skb);
1114 if (dev->txq.qlen >= TX_QLEN (dev))
1115 netif_stop_queue (net);
1116 }
1117 spin_unlock_irqrestore (&dev->txq.lock, flags);
1118
1119 if (retval) {
Joe Perchesa475f602010-02-17 10:30:24 +00001120 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121drop:
Herbert Xu79638372009-06-29 16:53:28 +00001122 dev->net->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (skb)
1124 dev_kfree_skb_any (skb);
1125 usb_free_urb (urb);
Joe Perchesa475f602010-02-17 10:30:24 +00001126 } else
1127 netif_dbg(dev, tx_queued, dev->net,
1128 "> tx, len %d, type 0x%x\n", length, skb->protocol);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001129#ifdef CONFIG_PM
1130deferred:
1131#endif
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001132 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001134EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136/*-------------------------------------------------------------------------*/
1137
1138// tasklet (work deferred from completions, in_irq) or timer
1139
1140static void usbnet_bh (unsigned long param)
1141{
1142 struct usbnet *dev = (struct usbnet *) param;
1143 struct sk_buff *skb;
1144 struct skb_data *entry;
1145
1146 while ((skb = skb_dequeue (&dev->done))) {
1147 entry = (struct skb_data *) skb->cb;
1148 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001149 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 entry->state = rx_cleanup;
1151 rx_process (dev, skb);
1152 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001153 case tx_done:
1154 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 usb_free_urb (entry->urb);
1156 dev_kfree_skb (skb);
1157 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001158 default:
Joe Perches60b86752010-02-17 10:30:23 +00001159 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 }
1162
1163 // waiting for all pending urbs to complete?
1164 if (dev->wait) {
1165 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1166 wake_up (dev->wait);
1167 }
1168
1169 // or are we maybe short a few urbs?
Joe Perches8e95a202009-12-03 07:58:21 +00001170 } else if (netif_running (dev->net) &&
1171 netif_device_present (dev->net) &&
1172 !timer_pending (&dev->delay) &&
1173 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int temp = dev->rxq.qlen;
1175 int qlen = RX_QLEN (dev);
1176
1177 if (temp < qlen) {
1178 struct urb *urb;
1179 int i;
1180
1181 // don't refill the queue all at once
1182 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1183 urb = usb_alloc_urb (0, GFP_ATOMIC);
1184 if (urb != NULL)
1185 rx_submit (dev, urb, GFP_ATOMIC);
1186 }
Joe Perchesa475f602010-02-17 10:30:24 +00001187 if (temp != dev->rxq.qlen)
1188 netif_dbg(dev, link, dev->net,
1189 "rxqlen %d --> %d\n",
1190 temp, dev->rxq.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (dev->rxq.qlen < qlen)
1192 tasklet_schedule (&dev->bh);
1193 }
1194 if (dev->txq.qlen < TX_QLEN (dev))
1195 netif_wake_queue (dev->net);
1196 }
1197}
1198
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200/*-------------------------------------------------------------------------
1201 *
1202 * USB Device Driver support
1203 *
1204 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206// precondition: never called in_interrupt
1207
David Brownell38bde1d2005-08-31 09:52:45 -07001208void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct usbnet *dev;
1211 struct usb_device *xdev;
1212 struct net_device *net;
1213
1214 dev = usb_get_intfdata(intf);
1215 usb_set_intfdata(intf, NULL);
1216 if (!dev)
1217 return;
1218
1219 xdev = interface_to_usbdev (intf);
1220
Joe Perchesa475f602010-02-17 10:30:24 +00001221 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1222 intf->dev.driver->name,
1223 xdev->bus->bus_name, xdev->devpath,
1224 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 net = dev->net;
1227 unregister_netdev (net);
1228
1229 /* we don't hold rtnl here ... */
1230 flush_scheduled_work ();
1231
1232 if (dev->driver_info->unbind)
1233 dev->driver_info->unbind (dev, intf);
1234
1235 free_netdev(net);
1236 usb_put_dev (xdev);
1237}
David Brownell38bde1d2005-08-31 09:52:45 -07001238EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Stephen Hemminger777baa42009-03-20 19:35:54 +00001240static const struct net_device_ops usbnet_netdev_ops = {
1241 .ndo_open = usbnet_open,
1242 .ndo_stop = usbnet_stop,
1243 .ndo_start_xmit = usbnet_start_xmit,
1244 .ndo_tx_timeout = usbnet_tx_timeout,
1245 .ndo_change_mtu = usbnet_change_mtu,
1246 .ndo_set_mac_address = eth_mac_addr,
1247 .ndo_validate_addr = eth_validate_addr,
1248};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250/*-------------------------------------------------------------------------*/
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252// precondition: never called in_interrupt
1253
Marcel Holtmann225794f2009-10-02 05:15:26 +00001254static struct device_type wlan_type = {
1255 .name = "wlan",
1256};
1257
1258static struct device_type wwan_type = {
1259 .name = "wwan",
1260};
1261
David Brownell38bde1d2005-08-31 09:52:45 -07001262int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1264{
1265 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001266 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct usb_host_interface *interface;
1268 struct driver_info *info;
1269 struct usb_device *xdev;
1270 int status;
David Brownell296c0242007-04-17 16:10:10 -07001271 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
David Brownell296c0242007-04-17 16:10:10 -07001273 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 info = (struct driver_info *) prod->driver_info;
1275 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001276 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -ENODEV;
1278 }
1279 xdev = interface_to_usbdev (udev);
1280 interface = udev->cur_altsetting;
1281
1282 usb_get_dev (xdev);
1283
1284 status = -ENOMEM;
1285
1286 // set up our own records
1287 net = alloc_etherdev(sizeof(*dev));
1288 if (!net) {
1289 dbg ("can't kmalloc dev");
1290 goto out;
1291 }
1292
1293 dev = netdev_priv(net);
1294 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001295 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001297 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1299 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1300 skb_queue_head_init (&dev->rxq);
1301 skb_queue_head_init (&dev->txq);
1302 skb_queue_head_init (&dev->done);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +03001303 skb_queue_head_init(&dev->rxq_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 dev->bh.func = usbnet_bh;
1305 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001306 INIT_WORK (&dev->kevent, kevent);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001307 init_usb_anchor(&dev->deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 dev->delay.function = usbnet_bh;
1309 dev->delay.data = (unsigned long) dev;
1310 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001311 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 dev->net = net;
1314 strcpy (net->name, "usb%d");
1315 memcpy (net->dev_addr, node_id, sizeof node_id);
1316
David Brownell2e55cc72005-08-31 09:53:10 -07001317 /* rx and tx sides can use different message sizes;
1318 * bind() should set rx_urb_size in that case.
1319 */
1320 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321#if 0
1322// dma_supported() is deeply broken on almost all architectures
1323 // possible with some EHCI controllers
Yang Hongyang6a355282009-04-06 19:01:13 -07001324 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 net->features |= NETIF_F_HIGHDMA;
1326#endif
1327
Stephen Hemminger777baa42009-03-20 19:35:54 +00001328 net->netdev_ops = &usbnet_netdev_ops;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001329 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 net->ethtool_ops = &usbnet_ethtool_ops;
1331
1332 // allow device-specific bind/init procedures
1333 // NOTE net->name still not usable ...
1334 if (info->bind) {
1335 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001336 if (status < 0)
1337 goto out1;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 // heuristic: "usb%d" for links we know are two-host,
1340 // else "eth%d" when there's reasonable doubt. userspace
1341 // can rename the link if it knows better.
Joe Perches8e95a202009-12-03 07:58:21 +00001342 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1343 (net->dev_addr [0] & 0x02) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001345 /* WLAN devices should always be named "wlan%d" */
1346 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1347 strcpy(net->name, "wlan%d");
Marcel Holtmanne1e499e2009-10-02 05:15:25 +00001348 /* WWAN devices should always be named "wwan%d" */
1349 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1350 strcpy(net->name, "wwan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001351
1352 /* maybe the remote can't receive an Ethernet MTU */
1353 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1354 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001355 } else if (!info->in || !info->out)
1356 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 else {
1358 dev->in = usb_rcvbulkpipe (xdev, info->in);
1359 dev->out = usb_sndbulkpipe (xdev, info->out);
1360 if (!(info->flags & FLAG_NO_SETINT))
1361 status = usb_set_interface (xdev,
1362 interface->desc.bInterfaceNumber,
1363 interface->desc.bAlternateSetting);
1364 else
1365 status = 0;
1366
1367 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001368 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 status = init_status (dev, udev);
1370 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001371 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
David Brownell2e55cc72005-08-31 09:53:10 -07001373 if (!dev->rx_urb_size)
1374 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 SET_NETDEV_DEV(net, &udev->dev);
Marcel Holtmann225794f2009-10-02 05:15:26 +00001378
1379 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1380 SET_NETDEV_DEVTYPE(net, &wlan_type);
1381 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1382 SET_NETDEV_DEVTYPE(net, &wwan_type);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 status = register_netdev (net);
1385 if (status)
1386 goto out3;
Joe Perchesa475f602010-02-17 10:30:24 +00001387 netif_info(dev, probe, dev->net,
1388 "register '%s' at usb-%s-%s, %s, %pM\n",
1389 udev->dev.driver->name,
1390 xdev->bus->bus_name, xdev->devpath,
1391 dev->driver_info->description,
1392 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 // ok, it's ready to go.
1395 usb_set_intfdata (udev, dev);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 netif_device_attach (net);
1398
Ben Hutchings37e82732009-11-04 15:29:52 +00001399 if (dev->driver_info->flags & FLAG_LINK_INTR)
1400 netif_carrier_off(net);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return 0;
1403
1404out3:
1405 if (info->unbind)
1406 info->unbind (dev, udev);
1407out1:
1408 free_netdev(net);
1409out:
1410 usb_put_dev(xdev);
1411 return status;
1412}
David Brownell38bde1d2005-08-31 09:52:45 -07001413EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415/*-------------------------------------------------------------------------*/
1416
Oliver Neukum36433122007-04-30 01:37:44 -07001417/*
1418 * suspend the whole driver as soon as the first interface is suspended
1419 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
David Brownell38bde1d2005-08-31 09:52:45 -07001422int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001425
Oliver Neukum36433122007-04-30 01:37:44 -07001426 if (!dev->suspend_count++) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001427 spin_lock_irq(&dev->txq.lock);
1428 /* don't autosuspend while transmitting */
1429 if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) {
1430 spin_unlock_irq(&dev->txq.lock);
1431 return -EBUSY;
1432 } else {
1433 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1434 spin_unlock_irq(&dev->txq.lock);
1435 }
Oliver Neukuma11a6542007-08-03 13:52:19 +02001436 /*
1437 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001438 * having everything error out.
1439 */
1440 netif_device_detach (dev->net);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001441 usbnet_terminate_urbs(dev);
1442 usb_kill_urb(dev->interrupt);
1443
Oliver Neukuma11a6542007-08-03 13:52:19 +02001444 /*
1445 * reattach so runtime management can use and
1446 * wake the device
1447 */
1448 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 0;
1451}
David Brownell38bde1d2005-08-31 09:52:45 -07001452EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
David Brownell38bde1d2005-08-31 09:52:45 -07001454int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 struct usbnet *dev = usb_get_intfdata(intf);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001457 struct sk_buff *skb;
1458 struct urb *res;
1459 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001461 if (!--dev->suspend_count) {
1462 spin_lock_irq(&dev->txq.lock);
1463 while ((res = usb_get_from_anchor(&dev->deferred))) {
1464
1465 printk(KERN_INFO"%s has delayed data\n", __func__);
1466 skb = (struct sk_buff *)res->context;
1467 retval = usb_submit_urb(res, GFP_ATOMIC);
1468 if (retval < 0) {
1469 dev_kfree_skb_any(skb);
1470 usb_free_urb(res);
1471 usb_autopm_put_interface_async(dev->intf);
1472 } else {
1473 dev->net->trans_start = jiffies;
1474 __skb_queue_tail(&dev->txq, skb);
1475 }
1476 }
1477
1478 smp_mb();
1479 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1480 spin_unlock_irq(&dev->txq.lock);
1481 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1482 netif_start_queue(dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001483 tasklet_schedule (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return 0;
1486}
David Brownell38bde1d2005-08-31 09:52:45 -07001487EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490/*-------------------------------------------------------------------------*/
1491
David Brownellf29fc252005-08-31 09:52:31 -07001492static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
David Brownell090ffa92005-08-31 09:54:50 -07001494 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001495 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 random_ether_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
David Brownellf29fc252005-08-31 09:52:31 -07001501module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
David Brownellf29fc252005-08-31 09:52:31 -07001503static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
David Brownellf29fc252005-08-31 09:52:31 -07001506module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
David Brownellf29fc252005-08-31 09:52:31 -07001508MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001509MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001510MODULE_LICENSE("GPL");