blob: 659654f458806b36eeab21a8f84bb4aa450c0af2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/ethtool.h>
41#include <linux/workqueue.h>
42#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020044#include <linux/usb/usbnet.h>
David Brownellf29fc252005-08-31 09:52:31 -070045
46#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48
49/*-------------------------------------------------------------------------*/
50
51/*
52 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
53 * Several dozen bytes of IPv4 data can fit in two such transactions.
54 * One maximum size Ethernet packet takes twenty four of them.
55 * For high speed, each frame comfortably fits almost 36 max size
56 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070057 *
58 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
59 * let the USB host controller be busy for 5msec or more before an irq
60 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Jamie Paintera99c1942006-07-27 14:17:28 -040062#define RX_MAX_QUEUE_MEMORY (60 * 1518)
63#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
64 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
65#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
66 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068// reawaken network queue this soon after stopping; else watchdog barks
69#define TX_TIMEOUT_JIFFIES (5*HZ)
70
71// throttle rx/tx briefly after some faults, so khubd might disconnect()
72// us (it polls at HZ/4 usually) before we report too many false errors.
73#define THROTTLE_JIFFIES (HZ/8)
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075// between wakeups
76#define UNLINK_TIMEOUT_MS 3
77
78/*-------------------------------------------------------------------------*/
79
80// randomly generated ethernet address
81static u8 node_id [ETH_ALEN];
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static const char driver_name [] = "usbnet";
84
85/* use ethtool to change the level for any given device */
86static int msg_level = -1;
87module_param (msg_level, int, 0);
88MODULE_PARM_DESC (msg_level, "Override default message level");
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*-------------------------------------------------------------------------*/
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -070093int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 int tmp;
96 struct usb_host_interface *alt = NULL;
97 struct usb_host_endpoint *in = NULL, *out = NULL;
98 struct usb_host_endpoint *status = NULL;
99
100 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
101 unsigned ep;
102
103 in = out = status = NULL;
104 alt = intf->altsetting + tmp;
105
106 /* take the first altsetting with in-bulk + out-bulk;
107 * remember any status endpoint, just in case;
108 * ignore other endpoints and altsetttings.
109 */
110 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
111 struct usb_host_endpoint *e;
112 int intr = 0;
113
114 e = alt->endpoint + ep;
115 switch (e->desc.bmAttributes) {
116 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300117 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 continue;
119 intr = 1;
120 /* FALLTHROUGH */
121 case USB_ENDPOINT_XFER_BULK:
122 break;
123 default:
124 continue;
125 }
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 if (!intr && !in)
128 in = e;
129 else if (intr && !status)
130 status = e;
131 } else {
132 if (!out)
133 out = e;
134 }
135 }
136 if (in && out)
137 break;
138 }
139 if (!alt || !in || !out)
140 return -EINVAL;
141
142 if (alt->desc.bAlternateSetting != 0
143 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
144 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
145 alt->desc.bAlternateSetting);
146 if (tmp < 0)
147 return tmp;
148 }
David Brownellcb1cebb2007-02-15 18:52:30 -0800149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 dev->in = usb_rcvbulkpipe (dev->udev,
151 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
152 dev->out = usb_sndbulkpipe (dev->udev,
153 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
154 dev->status = status;
155 return 0;
156}
David Brownell2e55cc72005-08-31 09:53:10 -0700157EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
David Howells7d12e782006-10-05 14:55:46 +0100159static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161static int init_status (struct usbnet *dev, struct usb_interface *intf)
162{
163 char *buf = NULL;
164 unsigned pipe = 0;
165 unsigned maxp;
166 unsigned period;
167
168 if (!dev->driver_info->status)
169 return 0;
170
171 pipe = usb_rcvintpipe (dev->udev,
172 dev->status->desc.bEndpointAddress
173 & USB_ENDPOINT_NUMBER_MASK);
174 maxp = usb_maxpacket (dev->udev, pipe, 0);
175
176 /* avoid 1 msec chatter: min 8 msec poll rate */
177 period = max ((int) dev->status->desc.bInterval,
178 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
179
Christoph Lametere94b1762006-12-06 20:33:17 -0800180 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800182 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!dev->interrupt) {
184 kfree (buf);
185 return -ENOMEM;
186 } else {
187 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
188 buf, maxp, intr_complete, dev, period);
189 dev_dbg(&intf->dev,
190 "status ep%din, %d bytes period %d\n",
191 usb_pipeendpoint(pipe), maxp, period);
192 }
193 }
David Brownell18ab4582007-05-25 12:31:32 -0700194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
David Brownell2e55cc72005-08-31 09:53:10 -0700197/* Passes this packet up the stack, updating its accounting.
198 * Some link protocols batch packets, so their rx_fixup paths
199 * can return clones as well as just modify the original skb.
200 */
201void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int status;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 skb->protocol = eth_type_trans (skb, dev->net);
206 dev->stats.rx_packets++;
207 dev->stats.rx_bytes += skb->len;
208
209 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700210 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 skb->len + sizeof (struct ethhdr), skb->protocol);
212 memset (skb->cb, 0, sizeof (struct skb_data));
213 status = netif_rx (skb);
214 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
215 devdbg (dev, "netif_rx status %d", status);
216}
David Brownell2e55cc72005-08-31 09:53:10 -0700217EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*-------------------------------------------------------------------------
221 *
222 * Network Device Driver (peer link to "Host Device", from USB host)
223 *
224 *-------------------------------------------------------------------------*/
225
Stephen Hemminger777baa42009-03-20 19:35:54 +0000226int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700229 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400230 int old_hard_mtu = dev->hard_mtu;
231 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jamie Paintera99c1942006-07-27 14:17:28 -0400233 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700236 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return -EDOM;
238 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400239
240 dev->hard_mtu = net->mtu + net->hard_header_len;
241 if (dev->rx_urb_size == old_hard_mtu) {
242 dev->rx_urb_size = dev->hard_mtu;
243 if (dev->rx_urb_size > old_rx_urb_size)
244 usbnet_unlink_rx_urbs(dev);
245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000249EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/*-------------------------------------------------------------------------*/
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
254 * completion callbacks. 2.5 should have fixed those bugs...
255 */
256
David S. Miller8728b832005-08-09 19:25:21 -0700257static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long flags;
260
David S. Miller8728b832005-08-09 19:25:21 -0700261 spin_lock_irqsave(&list->lock, flags);
262 __skb_unlink(skb, list);
263 spin_unlock(&list->lock);
264 spin_lock(&dev->done.lock);
265 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700267 tasklet_schedule(&dev->bh);
268 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/* some work can't be done in tasklets, so we use keventd
272 *
273 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
274 * but tasklet_schedule() doesn't. hope the failure is rare.
275 */
David Brownell2e55cc72005-08-31 09:53:10 -0700276void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 set_bit (work, &dev->flags);
279 if (!schedule_work (&dev->kevent))
280 deverr (dev, "kevent %d may have been dropped", work);
281 else
282 devdbg (dev, "kevent %d scheduled", work);
283}
David Brownell2e55cc72005-08-31 09:53:10 -0700284EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/*-------------------------------------------------------------------------*/
287
David Howells7d12e782006-10-05 14:55:46 +0100288static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Al Viro55016f12005-10-21 03:21:58 -0400290static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct sk_buff *skb;
293 struct skb_data *entry;
294 int retval = 0;
295 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700296 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
299 if (netif_msg_rx_err (dev))
300 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -0700301 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 usb_free_urb (urb);
303 return;
304 }
305 skb_reserve (skb, NET_IP_ALIGN);
306
307 entry = (struct skb_data *) skb->cb;
308 entry->urb = urb;
309 entry->dev = dev;
310 entry->state = rx_start;
311 entry->length = 0;
312
313 usb_fill_bulk_urb (urb, dev->udev, dev->in,
314 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 spin_lock_irqsave (&dev->rxq.lock, lockflags);
317
318 if (netif_running (dev->net)
319 && netif_device_present (dev->net)
320 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700321 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700323 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700326 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 case -ENODEV:
329 if (netif_msg_ifdown (dev))
330 devdbg (dev, "device gone");
331 netif_device_detach (dev->net);
332 break;
333 default:
334 if (netif_msg_rx_err (dev))
335 devdbg (dev, "rx submit, %d", retval);
336 tasklet_schedule (&dev->bh);
337 break;
338 case 0:
339 __skb_queue_tail (&dev->rxq, skb);
340 }
341 } else {
342 if (netif_msg_ifdown (dev))
343 devdbg (dev, "rx: stopped");
344 retval = -ENOLINK;
345 }
346 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
347 if (retval) {
348 dev_kfree_skb_any (skb);
349 usb_free_urb (urb);
350 }
351}
352
353
354/*-------------------------------------------------------------------------*/
355
356static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
357{
358 if (dev->driver_info->rx_fixup
359 && !dev->driver_info->rx_fixup (dev, skb))
360 goto error;
361 // else network stack removes extra byte if we forced a short packet
362
363 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700364 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 else {
366 if (netif_msg_rx_err (dev))
367 devdbg (dev, "drop");
368error:
369 dev->stats.rx_errors++;
370 skb_queue_tail (&dev->done, skb);
371 }
372}
373
374/*-------------------------------------------------------------------------*/
375
David Howells7d12e782006-10-05 14:55:46 +0100376static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct sk_buff *skb = (struct sk_buff *) urb->context;
379 struct skb_data *entry = (struct skb_data *) skb->cb;
380 struct usbnet *dev = entry->dev;
381 int urb_status = urb->status;
382
383 skb_put (skb, urb->actual_length);
384 entry->state = rx_done;
385 entry->urb = NULL;
386
387 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700388 /* success */
389 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700390 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 entry->state = rx_cleanup;
392 dev->stats.rx_errors++;
393 dev->stats.rx_length_errors++;
394 if (netif_msg_rx_err (dev))
395 devdbg (dev, "rx length %d", skb->len);
396 }
397 break;
398
David Brownell18ab4582007-05-25 12:31:32 -0700399 /* stalls need manual reset. this is rare ... except that
400 * when going through USB 2.0 TTs, unplug appears this way.
401 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
402 * storm, recovering as needed.
403 */
404 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700406 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 // FALLTHROUGH
408
David Brownell18ab4582007-05-25 12:31:32 -0700409 /* software-driven interface shutdown */
410 case -ECONNRESET: /* async unlink */
411 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (netif_msg_ifdown (dev))
413 devdbg (dev, "rx shutdown, code %d", urb_status);
414 goto block;
415
David Brownell18ab4582007-05-25 12:31:32 -0700416 /* we get controller i/o faults during khubd disconnect() delays.
417 * throttle down resubmits, to avoid log floods; just temporarily,
418 * so we still recover when the fault isn't a khubd delay.
419 */
420 case -EPROTO:
421 case -ETIME:
422 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dev->stats.rx_errors++;
424 if (!timer_pending (&dev->delay)) {
425 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
426 if (netif_msg_link (dev))
427 devdbg (dev, "rx throttle %d", urb_status);
428 }
429block:
430 entry->state = rx_cleanup;
431 entry->urb = urb;
432 urb = NULL;
433 break;
434
David Brownell18ab4582007-05-25 12:31:32 -0700435 /* data overrun ... flush fifo? */
436 case -EOVERFLOW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 dev->stats.rx_over_errors++;
438 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800439
David Brownell18ab4582007-05-25 12:31:32 -0700440 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 entry->state = rx_cleanup;
442 dev->stats.rx_errors++;
443 if (netif_msg_rx_err (dev))
444 devdbg (dev, "rx status %d", urb_status);
445 break;
446 }
447
David S. Miller8728b832005-08-09 19:25:21 -0700448 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (urb) {
451 if (netif_running (dev->net)
452 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
453 rx_submit (dev, urb, GFP_ATOMIC);
454 return;
455 }
456 usb_free_urb (urb);
457 }
458 if (netif_msg_rx_err (dev))
459 devdbg (dev, "no read resubmitted");
460}
461
David Howells7d12e782006-10-05 14:55:46 +0100462static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct usbnet *dev = urb->context;
465 int status = urb->status;
466
467 switch (status) {
David Brownell18ab4582007-05-25 12:31:32 -0700468 /* success */
469 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dev->driver_info->status(dev, urb);
471 break;
472
David Brownell18ab4582007-05-25 12:31:32 -0700473 /* software-driven interface shutdown */
474 case -ENOENT: /* urb killed */
475 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (netif_msg_ifdown (dev))
477 devdbg (dev, "intr shutdown, code %d", status);
478 return;
479
David Brownell18ab4582007-05-25 12:31:32 -0700480 /* NOTE: not throttling like RX/TX, since this endpoint
481 * already polls infrequently
482 */
483 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 devdbg (dev, "intr status %d", status);
485 break;
486 }
487
488 if (!netif_running (dev->net))
489 return;
490
491 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
492 status = usb_submit_urb (urb, GFP_ATOMIC);
493 if (status != 0 && netif_msg_timer (dev))
494 deverr(dev, "intr resubmit --> %d", status);
495}
496
497/*-------------------------------------------------------------------------*/
498
499// unlink pending rx/tx; completion handlers do all other cleanup
500
501static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
502{
503 unsigned long flags;
504 struct sk_buff *skb, *skbnext;
505 int count = 0;
506
507 spin_lock_irqsave (&q->lock, flags);
David S. Miller83bfba52008-09-22 20:18:47 -0700508 skb_queue_walk_safe(q, skb, skbnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct skb_data *entry;
510 struct urb *urb;
511 int retval;
512
513 entry = (struct skb_data *) skb->cb;
514 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 // during some PM-driven resume scenarios,
517 // these (async) unlinks complete immediately
518 retval = usb_unlink_urb (urb);
519 if (retval != -EINPROGRESS && retval != 0)
520 devdbg (dev, "unlink urb err, %d", retval);
521 else
522 count++;
523 }
524 spin_unlock_irqrestore (&q->lock, flags);
525 return count;
526}
527
Jamie Paintera99c1942006-07-27 14:17:28 -0400528// Flush all pending rx urbs
529// minidrivers may need to do this when the MTU changes
530
531void usbnet_unlink_rx_urbs(struct usbnet *dev)
532{
533 if (netif_running(dev->net)) {
534 (void) unlink_urbs (dev, &dev->rxq);
535 tasklet_schedule(&dev->bh);
536 }
537}
538EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540/*-------------------------------------------------------------------------*/
541
542// precondition: never called in_interrupt
543
Stephen Hemminger777baa42009-03-20 19:35:54 +0000544int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct usbnet *dev = netdev_priv(net);
547 int temp;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800548 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 DECLARE_WAITQUEUE (wait, current);
550
551 netif_stop_queue (net);
552
553 if (netif_msg_ifdown (dev))
554 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
David Brownellcb1cebb2007-02-15 18:52:30 -0800555 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 dev->stats.rx_errors, dev->stats.tx_errors
557 );
558
559 // ensure there are no more active urbs
560 add_wait_queue (&unlink_wakeup, &wait);
561 dev->wait = &unlink_wakeup;
562 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
563
564 // maybe wait for deletions to finish.
David Brownell18ab4582007-05-25 12:31:32 -0700565 while (!skb_queue_empty(&dev->rxq)
566 && !skb_queue_empty(&dev->txq)
567 && !skb_queue_empty(&dev->done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 msleep(UNLINK_TIMEOUT_MS);
569 if (netif_msg_ifdown (dev))
570 devdbg (dev, "waited for %d urb completions", temp);
571 }
572 dev->wait = NULL;
David Brownellcb1cebb2007-02-15 18:52:30 -0800573 remove_wait_queue (&unlink_wakeup, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 usb_kill_urb(dev->interrupt);
576
577 /* deferred work (task, timer, softirq) must also stop.
578 * can't flush_scheduled_work() until we drop rtnl (later),
579 * else workers could deadlock; so make workers a NOP.
580 */
581 dev->flags = 0;
582 del_timer_sync (&dev->delay);
583 tasklet_kill (&dev->bh);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200584 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 return 0;
587}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000588EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590/*-------------------------------------------------------------------------*/
591
592// posts reads, and enables write queuing
593
594// precondition: never called in_interrupt
595
Stephen Hemminger777baa42009-03-20 19:35:54 +0000596int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200599 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct driver_info *info = dev->driver_info;
601
Oliver Neukuma11a6542007-08-03 13:52:19 +0200602 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
603 if (netif_msg_ifup (dev))
604 devinfo (dev,
605 "resumption fail (%d) usbnet usb-%s-%s, %s",
606 retval,
607 dev->udev->bus->bus_name, dev->udev->devpath,
608 info->description);
609 goto done_nopm;
610 }
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 // put into "known safe" state
613 if (info->reset && (retval = info->reset (dev)) < 0) {
614 if (netif_msg_ifup (dev))
615 devinfo (dev,
616 "open reset fail (%d) usbnet usb-%s-%s, %s",
617 retval,
618 dev->udev->bus->bus_name, dev->udev->devpath,
619 info->description);
620 goto done;
621 }
622
623 // insist peer be connected
624 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
625 if (netif_msg_ifup (dev))
626 devdbg (dev, "can't open; %d", retval);
627 goto done;
628 }
629
630 /* start any status interrupt transfer */
631 if (dev->interrupt) {
632 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
633 if (retval < 0) {
634 if (netif_msg_ifup (dev))
635 deverr (dev, "intr submit %d", retval);
636 goto done;
637 }
638 }
639
640 netif_start_queue (net);
641 if (netif_msg_ifup (dev)) {
642 char *framing;
643
644 if (dev->driver_info->flags & FLAG_FRAMING_NC)
645 framing = "NetChip";
646 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
647 framing = "GeneSys";
648 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
649 framing = "Zaurus";
650 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
651 framing = "RNDIS";
652 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
653 framing = "ASIX";
654 else
655 framing = "simple";
656
657 devinfo (dev, "open: enable queueing "
658 "(rx %d, tx %d) mtu %d %s framing",
Randy Dunlap25d94e62006-08-07 15:56:40 -0700659 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 framing);
661 }
662
663 // delay posting reads until we're fully open
664 tasklet_schedule (&dev->bh);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200665 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200667 usb_autopm_put_interface(dev->intf);
668done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return retval;
670}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000671EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/*-------------------------------------------------------------------------*/
674
David Brownell2e55cc72005-08-31 09:53:10 -0700675/* ethtool methods; minidrivers may need to add some more, but
676 * they'll probably want to use this base set.
677 */
678
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200679int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
680{
681 struct usbnet *dev = netdev_priv(net);
682
683 if (!dev->mii.mdio_read)
684 return -EOPNOTSUPP;
685
686 return mii_ethtool_gset(&dev->mii, cmd);
687}
688EXPORT_SYMBOL_GPL(usbnet_get_settings);
689
690int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
691{
692 struct usbnet *dev = netdev_priv(net);
693 int retval;
694
695 if (!dev->mii.mdio_write)
696 return -EOPNOTSUPP;
697
698 retval = mii_ethtool_sset(&dev->mii, cmd);
699
700 /* link speed/duplex might have changed */
701 if (dev->driver_info->link_reset)
702 dev->driver_info->link_reset(dev);
703
704 return retval;
705
706}
707EXPORT_SYMBOL_GPL(usbnet_set_settings);
708
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200709u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct usbnet *dev = netdev_priv(net);
712
David Brownellf29fc252005-08-31 09:52:31 -0700713 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (dev->driver_info->check_connect)
715 return dev->driver_info->check_connect (dev) == 0;
716
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200717 /* if the device has mii operations, use those */
718 if (dev->mii.mdio_read)
719 return mii_link_ok(&dev->mii);
720
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800721 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
722 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200724EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David Brownell18ee91fa2006-11-02 12:29:12 -0800726int usbnet_nway_reset(struct net_device *net)
727{
728 struct usbnet *dev = netdev_priv(net);
729
730 if (!dev->mii.mdio_write)
731 return -EOPNOTSUPP;
732
733 return mii_nway_restart(&dev->mii);
734}
735EXPORT_SYMBOL_GPL(usbnet_nway_reset);
736
David Brownell18ee91fa2006-11-02 12:29:12 -0800737void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
738{
739 struct usbnet *dev = netdev_priv(net);
740
David Brownell296c0242007-04-17 16:10:10 -0700741 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800742 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
743 strncpy (info->fw_version, dev->driver_info->description,
744 sizeof info->fw_version);
745 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
746}
747EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
748
David Brownell2e55cc72005-08-31 09:53:10 -0700749u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct usbnet *dev = netdev_priv(net);
752
753 return dev->msg_enable;
754}
David Brownell2e55cc72005-08-31 09:53:10 -0700755EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
David Brownell2e55cc72005-08-31 09:53:10 -0700757void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct usbnet *dev = netdev_priv(net);
760
761 dev->msg_enable = level;
762}
David Brownell2e55cc72005-08-31 09:53:10 -0700763EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Brownell2e55cc72005-08-31 09:53:10 -0700765/* drivers may override default ethtool_ops in their bind() routine */
766static struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200767 .get_settings = usbnet_get_settings,
768 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700769 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200770 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800771 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700772 .get_msglevel = usbnet_get_msglevel,
773 .set_msglevel = usbnet_set_msglevel,
774};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776/*-------------------------------------------------------------------------*/
777
778/* work that cannot be done in interrupt context uses keventd.
779 *
780 * NOTE: with 2.5 we could do more of this using completion callbacks,
781 * especially now that control transfers can be queued.
782 */
783static void
David Howellsc4028952006-11-22 14:57:56 +0000784kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
David Howellsc4028952006-11-22 14:57:56 +0000786 struct usbnet *dev =
787 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int status;
789
790 /* usb_clear_halt() needs a thread context */
791 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
792 unlink_urbs (dev, &dev->txq);
793 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -0700794 if (status < 0
795 && status != -EPIPE
796 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (netif_msg_tx_err (dev))
798 deverr (dev, "can't clear tx halt, status %d",
799 status);
800 } else {
801 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700802 if (status != -ESHUTDOWN)
803 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
806 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
807 unlink_urbs (dev, &dev->rxq);
808 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -0700809 if (status < 0
810 && status != -EPIPE
811 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (netif_msg_rx_err (dev))
813 deverr (dev, "can't clear rx halt, status %d",
814 status);
815 } else {
816 clear_bit (EVENT_RX_HALT, &dev->flags);
817 tasklet_schedule (&dev->bh);
818 }
819 }
820
821 /* tasklet could resubmit itself forever if memory is tight */
822 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
823 struct urb *urb = NULL;
824
825 if (netif_running (dev->net))
826 urb = usb_alloc_urb (0, GFP_KERNEL);
827 else
828 clear_bit (EVENT_RX_MEMORY, &dev->flags);
829 if (urb != NULL) {
830 clear_bit (EVENT_RX_MEMORY, &dev->flags);
831 rx_submit (dev, urb, GFP_KERNEL);
832 tasklet_schedule (&dev->bh);
833 }
834 }
835
David Hollis7ea13c92005-04-22 15:07:02 -0700836 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800837 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -0700838 int retval = 0;
839
840 clear_bit (EVENT_LINK_RESET, &dev->flags);
841 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
842 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
843 retval,
844 dev->udev->bus->bus_name, dev->udev->devpath,
845 info->description);
846 }
847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (dev->flags)
850 devdbg (dev, "kevent done, flags = 0x%lx",
851 dev->flags);
852}
853
854/*-------------------------------------------------------------------------*/
855
David Howells7d12e782006-10-05 14:55:46 +0100856static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct sk_buff *skb = (struct sk_buff *) urb->context;
859 struct skb_data *entry = (struct skb_data *) skb->cb;
860 struct usbnet *dev = entry->dev;
861
862 if (urb->status == 0) {
863 dev->stats.tx_packets++;
864 dev->stats.tx_bytes += entry->length;
865 } else {
866 dev->stats.tx_errors++;
867
868 switch (urb->status) {
869 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700870 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872
873 /* software-driven interface shutdown */
874 case -ECONNRESET: // async unlink
875 case -ESHUTDOWN: // hardware gone
876 break;
877
878 // like rx, tx gets controller i/o faults during khubd delays
879 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700880 case -EPROTO:
881 case -ETIME:
882 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!timer_pending (&dev->delay)) {
884 mod_timer (&dev->delay,
885 jiffies + THROTTLE_JIFFIES);
886 if (netif_msg_link (dev))
887 devdbg (dev, "tx throttle %d",
888 urb->status);
889 }
890 netif_stop_queue (dev->net);
891 break;
892 default:
893 if (netif_msg_tx_err (dev))
894 devdbg (dev, "tx err %d", entry->urb->status);
895 break;
896 }
897 }
898
899 urb->dev = NULL;
900 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -0700901 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*-------------------------------------------------------------------------*/
905
Stephen Hemminger777baa42009-03-20 19:35:54 +0000906void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct usbnet *dev = netdev_priv(net);
909
910 unlink_urbs (dev, &dev->txq);
911 tasklet_schedule (&dev->bh);
912
913 // FIXME: device recovery -- reset?
914}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000915EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917/*-------------------------------------------------------------------------*/
918
Stephen Hemminger777baa42009-03-20 19:35:54 +0000919int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct usbnet *dev = netdev_priv(net);
922 int length;
923 int retval = NET_XMIT_SUCCESS;
924 struct urb *urb = NULL;
925 struct skb_data *entry;
926 struct driver_info *info = dev->driver_info;
927 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 // some devices want funky USB-level framing, for
930 // win32 driver (usually) and/or hardware quirks
931 if (info->tx_fixup) {
932 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
933 if (!skb) {
934 if (netif_msg_tx_err (dev))
935 devdbg (dev, "can't tx_fixup skb");
936 goto drop;
937 }
938 }
939 length = skb->len;
940
941 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
942 if (netif_msg_tx_err (dev))
943 devdbg (dev, "no urb");
944 goto drop;
945 }
946
947 entry = (struct skb_data *) skb->cb;
948 entry->urb = urb;
949 entry->dev = dev;
950 entry->state = tx_start;
951 entry->length = length;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 usb_fill_bulk_urb (urb, dev->udev, dev->out,
954 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* don't assume the hardware handles USB_ZERO_PACKET
957 * NOTE: strictly conforming cdc-ether devices should expect
958 * the ZLP here, but ignore the one-byte packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
Peter Korsgaard3e323f32007-06-27 08:48:15 +0200960 if ((length % dev->maxpacket) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 urb->transfer_buffer_length++;
Peter Korsgaard3e323f32007-06-27 08:48:15 +0200962 if (skb_tailroom(skb)) {
963 skb->data[skb->len] = 0;
964 __skb_put(skb, 1);
965 }
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 spin_lock_irqsave (&dev->txq.lock, flags);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
971 case -EPIPE:
972 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -0700973 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
975 default:
976 if (netif_msg_tx_err (dev))
977 devdbg (dev, "tx: submit urb err %d", retval);
978 break;
979 case 0:
980 net->trans_start = jiffies;
981 __skb_queue_tail (&dev->txq, skb);
982 if (dev->txq.qlen >= TX_QLEN (dev))
983 netif_stop_queue (net);
984 }
985 spin_unlock_irqrestore (&dev->txq.lock, flags);
986
987 if (retval) {
988 if (netif_msg_tx_err (dev))
989 devdbg (dev, "drop, code %d", retval);
990drop:
991 retval = NET_XMIT_SUCCESS;
992 dev->stats.tx_dropped++;
993 if (skb)
994 dev_kfree_skb_any (skb);
995 usb_free_urb (urb);
996 } else if (netif_msg_tx_queued (dev)) {
997 devdbg (dev, "> tx, len %d, type 0x%x",
998 length, skb->protocol);
999 }
1000 return retval;
1001}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001002EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004/*-------------------------------------------------------------------------*/
1005
1006// tasklet (work deferred from completions, in_irq) or timer
1007
1008static void usbnet_bh (unsigned long param)
1009{
1010 struct usbnet *dev = (struct usbnet *) param;
1011 struct sk_buff *skb;
1012 struct skb_data *entry;
1013
1014 while ((skb = skb_dequeue (&dev->done))) {
1015 entry = (struct skb_data *) skb->cb;
1016 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001017 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 entry->state = rx_cleanup;
1019 rx_process (dev, skb);
1020 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001021 case tx_done:
1022 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 usb_free_urb (entry->urb);
1024 dev_kfree_skb (skb);
1025 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001026 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 devdbg (dev, "bogus skb state %d", entry->state);
1028 }
1029 }
1030
1031 // waiting for all pending urbs to complete?
1032 if (dev->wait) {
1033 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1034 wake_up (dev->wait);
1035 }
1036
1037 // or are we maybe short a few urbs?
1038 } else if (netif_running (dev->net)
1039 && netif_device_present (dev->net)
1040 && !timer_pending (&dev->delay)
1041 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1042 int temp = dev->rxq.qlen;
1043 int qlen = RX_QLEN (dev);
1044
1045 if (temp < qlen) {
1046 struct urb *urb;
1047 int i;
1048
1049 // don't refill the queue all at once
1050 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1051 urb = usb_alloc_urb (0, GFP_ATOMIC);
1052 if (urb != NULL)
1053 rx_submit (dev, urb, GFP_ATOMIC);
1054 }
1055 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1056 devdbg (dev, "rxqlen %d --> %d",
1057 temp, dev->rxq.qlen);
1058 if (dev->rxq.qlen < qlen)
1059 tasklet_schedule (&dev->bh);
1060 }
1061 if (dev->txq.qlen < TX_QLEN (dev))
1062 netif_wake_queue (dev->net);
1063 }
1064}
1065
1066
1067
1068/*-------------------------------------------------------------------------
1069 *
1070 * USB Device Driver support
1071 *
1072 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074// precondition: never called in_interrupt
1075
David Brownell38bde1d2005-08-31 09:52:45 -07001076void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct usbnet *dev;
1079 struct usb_device *xdev;
1080 struct net_device *net;
1081
1082 dev = usb_get_intfdata(intf);
1083 usb_set_intfdata(intf, NULL);
1084 if (!dev)
1085 return;
1086
1087 xdev = interface_to_usbdev (intf);
1088
1089 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001090 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1091 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 xdev->bus->bus_name, xdev->devpath,
1093 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 net = dev->net;
1096 unregister_netdev (net);
1097
1098 /* we don't hold rtnl here ... */
1099 flush_scheduled_work ();
1100
1101 if (dev->driver_info->unbind)
1102 dev->driver_info->unbind (dev, intf);
1103
1104 free_netdev(net);
1105 usb_put_dev (xdev);
1106}
David Brownell38bde1d2005-08-31 09:52:45 -07001107EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Stephen Hemminger777baa42009-03-20 19:35:54 +00001109static const struct net_device_ops usbnet_netdev_ops = {
1110 .ndo_open = usbnet_open,
1111 .ndo_stop = usbnet_stop,
1112 .ndo_start_xmit = usbnet_start_xmit,
1113 .ndo_tx_timeout = usbnet_tx_timeout,
1114 .ndo_change_mtu = usbnet_change_mtu,
1115 .ndo_set_mac_address = eth_mac_addr,
1116 .ndo_validate_addr = eth_validate_addr,
1117};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119/*-------------------------------------------------------------------------*/
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121// precondition: never called in_interrupt
1122
David Brownell38bde1d2005-08-31 09:52:45 -07001123int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1125{
1126 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001127 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct usb_host_interface *interface;
1129 struct driver_info *info;
1130 struct usb_device *xdev;
1131 int status;
David Brownell296c0242007-04-17 16:10:10 -07001132 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
David Brownell296c0242007-04-17 16:10:10 -07001134 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 info = (struct driver_info *) prod->driver_info;
1136 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001137 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return -ENODEV;
1139 }
1140 xdev = interface_to_usbdev (udev);
1141 interface = udev->cur_altsetting;
1142
1143 usb_get_dev (xdev);
1144
1145 status = -ENOMEM;
1146
1147 // set up our own records
1148 net = alloc_etherdev(sizeof(*dev));
1149 if (!net) {
1150 dbg ("can't kmalloc dev");
1151 goto out;
1152 }
1153
1154 dev = netdev_priv(net);
1155 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001156 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001158 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1160 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1161 skb_queue_head_init (&dev->rxq);
1162 skb_queue_head_init (&dev->txq);
1163 skb_queue_head_init (&dev->done);
1164 dev->bh.func = usbnet_bh;
1165 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001166 INIT_WORK (&dev->kevent, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 dev->delay.function = usbnet_bh;
1168 dev->delay.data = (unsigned long) dev;
1169 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001170 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 dev->net = net;
1173 strcpy (net->name, "usb%d");
1174 memcpy (net->dev_addr, node_id, sizeof node_id);
1175
David Brownell2e55cc72005-08-31 09:53:10 -07001176 /* rx and tx sides can use different message sizes;
1177 * bind() should set rx_urb_size in that case.
1178 */
1179 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180#if 0
1181// dma_supported() is deeply broken on almost all architectures
1182 // possible with some EHCI controllers
1183 if (dma_supported (&udev->dev, DMA_64BIT_MASK))
1184 net->features |= NETIF_F_HIGHDMA;
1185#endif
1186
Stephen Hemminger777baa42009-03-20 19:35:54 +00001187 net->netdev_ops = &usbnet_netdev_ops;
1188#ifdef CONFIG_COMPAT_NET_DEV_OPS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 net->hard_start_xmit = usbnet_start_xmit;
1190 net->open = usbnet_open;
1191 net->stop = usbnet_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 net->tx_timeout = usbnet_tx_timeout;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001193#endif
1194 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 net->ethtool_ops = &usbnet_ethtool_ops;
1196
1197 // allow device-specific bind/init procedures
1198 // NOTE net->name still not usable ...
1199 if (info->bind) {
1200 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001201 if (status < 0)
1202 goto out1;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 // heuristic: "usb%d" for links we know are two-host,
1205 // else "eth%d" when there's reasonable doubt. userspace
1206 // can rename the link if it knows better.
1207 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1208 && (net->dev_addr [0] & 0x02) == 0)
1209 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001210 /* WLAN devices should always be named "wlan%d" */
1211 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1212 strcpy(net->name, "wlan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001213
1214 /* maybe the remote can't receive an Ethernet MTU */
1215 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1216 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001217 } else if (!info->in || !info->out)
1218 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 else {
1220 dev->in = usb_rcvbulkpipe (xdev, info->in);
1221 dev->out = usb_sndbulkpipe (xdev, info->out);
1222 if (!(info->flags & FLAG_NO_SETINT))
1223 status = usb_set_interface (xdev,
1224 interface->desc.bInterfaceNumber,
1225 interface->desc.bAlternateSetting);
1226 else
1227 status = 0;
1228
1229 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001230 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 status = init_status (dev, udev);
1232 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001233 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
David Brownell2e55cc72005-08-31 09:53:10 -07001235 if (!dev->rx_urb_size)
1236 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 SET_NETDEV_DEV(net, &udev->dev);
1240 status = register_netdev (net);
1241 if (status)
1242 goto out3;
1243 if (netif_msg_probe (dev))
Johannes Berge1749612008-10-27 15:59:26 -07001244 devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
David Brownell38bde1d2005-08-31 09:52:45 -07001245 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 xdev->bus->bus_name, xdev->devpath,
1247 dev->driver_info->description,
Johannes Berge1749612008-10-27 15:59:26 -07001248 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 // ok, it's ready to go.
1251 usb_set_intfdata (udev, dev);
1252
1253 // start as if the link is up
1254 netif_device_attach (net);
1255
1256 return 0;
1257
1258out3:
1259 if (info->unbind)
1260 info->unbind (dev, udev);
1261out1:
1262 free_netdev(net);
1263out:
1264 usb_put_dev(xdev);
1265 return status;
1266}
David Brownell38bde1d2005-08-31 09:52:45 -07001267EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269/*-------------------------------------------------------------------------*/
1270
Oliver Neukum36433122007-04-30 01:37:44 -07001271/*
1272 * suspend the whole driver as soon as the first interface is suspended
1273 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
David Brownell38bde1d2005-08-31 09:52:45 -07001276int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001279
Oliver Neukum36433122007-04-30 01:37:44 -07001280 if (!dev->suspend_count++) {
Oliver Neukuma11a6542007-08-03 13:52:19 +02001281 /*
1282 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001283 * having everything error out.
1284 */
1285 netif_device_detach (dev->net);
1286 (void) unlink_urbs (dev, &dev->rxq);
1287 (void) unlink_urbs (dev, &dev->txq);
Oliver Neukuma11a6542007-08-03 13:52:19 +02001288 /*
1289 * reattach so runtime management can use and
1290 * wake the device
1291 */
1292 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
David Brownell38bde1d2005-08-31 09:52:45 -07001296EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
David Brownell38bde1d2005-08-31 09:52:45 -07001298int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct usbnet *dev = usb_get_intfdata(intf);
1301
Oliver Neukuma11a6542007-08-03 13:52:19 +02001302 if (!--dev->suspend_count)
Oliver Neukum36433122007-04-30 01:37:44 -07001303 tasklet_schedule (&dev->bh);
Oliver Neukuma11a6542007-08-03 13:52:19 +02001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return 0;
1306}
David Brownell38bde1d2005-08-31 09:52:45 -07001307EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310/*-------------------------------------------------------------------------*/
1311
David Brownellf29fc252005-08-31 09:52:31 -07001312static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
David Brownell090ffa92005-08-31 09:54:50 -07001314 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001315 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 random_ether_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
David Brownellf29fc252005-08-31 09:52:31 -07001321module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
David Brownellf29fc252005-08-31 09:52:31 -07001323static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
David Brownellf29fc252005-08-31 09:52:31 -07001326module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
David Brownellf29fc252005-08-31 09:52:31 -07001328MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001329MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001330MODULE_LICENSE("GPL");