blob: c94de6243140f00044fb9a2ee8d593006356e511 [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>
David Brownellf29fc252005-08-31 09:52:31 -070046
47#define DRIVER_VERSION "22-Aug-2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
50/*-------------------------------------------------------------------------*/
51
52/*
53 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
54 * Several dozen bytes of IPv4 data can fit in two such transactions.
55 * One maximum size Ethernet packet takes twenty four of them.
56 * For high speed, each frame comfortably fits almost 36 max size
57 * Ethernet packets (so queues should be bigger).
David Brownellf29fc252005-08-31 09:52:31 -070058 *
59 * REVISIT qlens should be members of 'struct usbnet'; the goal is to
60 * let the USB host controller be busy for 5msec or more before an irq
61 * is required, under load. Jumbograms change the equation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Jamie Paintera99c1942006-07-27 14:17:28 -040063#define RX_MAX_QUEUE_MEMORY (60 * 1518)
64#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
65 (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4)
66#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \
67 (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069// reawaken network queue this soon after stopping; else watchdog barks
70#define TX_TIMEOUT_JIFFIES (5*HZ)
71
72// throttle rx/tx briefly after some faults, so khubd might disconnect()
73// us (it polls at HZ/4 usually) before we report too many false errors.
74#define THROTTLE_JIFFIES (HZ/8)
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076// between wakeups
77#define UNLINK_TIMEOUT_MS 3
78
79/*-------------------------------------------------------------------------*/
80
81// randomly generated ethernet address
82static u8 node_id [ETH_ALEN];
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static const char driver_name [] = "usbnet";
85
86/* use ethtool to change the level for any given device */
87static int msg_level = -1;
88module_param (msg_level, int, 0);
89MODULE_PARM_DESC (msg_level, "Override default message level");
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*-------------------------------------------------------------------------*/
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* handles CDC Ethernet and many other network "bulk data" interfaces */
David Brownell2e55cc72005-08-31 09:53:10 -070094int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int tmp;
97 struct usb_host_interface *alt = NULL;
98 struct usb_host_endpoint *in = NULL, *out = NULL;
99 struct usb_host_endpoint *status = NULL;
100
101 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
102 unsigned ep;
103
104 in = out = status = NULL;
105 alt = intf->altsetting + tmp;
106
107 /* take the first altsetting with in-bulk + out-bulk;
108 * remember any status endpoint, just in case;
109 * ignore other endpoints and altsetttings.
110 */
111 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
112 struct usb_host_endpoint *e;
113 int intr = 0;
114
115 e = alt->endpoint + ep;
116 switch (e->desc.bmAttributes) {
117 case USB_ENDPOINT_XFER_INT:
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300118 if (!usb_endpoint_dir_in(&e->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 continue;
120 intr = 1;
121 /* FALLTHROUGH */
122 case USB_ENDPOINT_XFER_BULK:
123 break;
124 default:
125 continue;
126 }
Luiz Fernando N. Capitulinofc6e2542006-10-26 13:03:01 -0300127 if (usb_endpoint_dir_in(&e->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!intr && !in)
129 in = e;
130 else if (intr && !status)
131 status = e;
132 } else {
133 if (!out)
134 out = e;
135 }
136 }
137 if (in && out)
138 break;
139 }
140 if (!alt || !in || !out)
141 return -EINVAL;
142
143 if (alt->desc.bAlternateSetting != 0
144 || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
145 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
146 alt->desc.bAlternateSetting);
147 if (tmp < 0)
148 return tmp;
149 }
David Brownellcb1cebb2007-02-15 18:52:30 -0800150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 dev->in = usb_rcvbulkpipe (dev->udev,
152 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
153 dev->out = usb_sndbulkpipe (dev->udev,
154 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155 dev->status = status;
156 return 0;
157}
David Brownell2e55cc72005-08-31 09:53:10 -0700158EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Peter Holik03ad0322009-04-18 07:24:17 +0000160static u8 nibble(unsigned char c)
161{
162 if (likely(isdigit(c)))
163 return c - '0';
164 c = toupper(c);
165 if (likely(isxdigit(c)))
166 return 10 + c - 'A';
167 return 0;
168}
169
170int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
171{
172 int tmp, i;
173 unsigned char buf [13];
174
175 tmp = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
176 if (tmp != 12) {
177 dev_dbg(&dev->udev->dev,
178 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
179 if (tmp >= 0)
180 tmp = -EINVAL;
181 return tmp;
182 }
183 for (i = tmp = 0; i < 6; i++, tmp += 2)
184 dev->net->dev_addr [i] =
185 (nibble(buf [tmp]) << 4) + nibble(buf [tmp + 1]);
186 return 0;
187}
188EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
189
David Howells7d12e782006-10-05 14:55:46 +0100190static void intr_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192static int init_status (struct usbnet *dev, struct usb_interface *intf)
193{
194 char *buf = NULL;
195 unsigned pipe = 0;
196 unsigned maxp;
197 unsigned period;
198
199 if (!dev->driver_info->status)
200 return 0;
201
202 pipe = usb_rcvintpipe (dev->udev,
203 dev->status->desc.bEndpointAddress
204 & USB_ENDPOINT_NUMBER_MASK);
205 maxp = usb_maxpacket (dev->udev, pipe, 0);
206
207 /* avoid 1 msec chatter: min 8 msec poll rate */
208 period = max ((int) dev->status->desc.bInterval,
209 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
210
Christoph Lametere94b1762006-12-06 20:33:17 -0800211 buf = kmalloc (maxp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (buf) {
Christoph Lametere94b1762006-12-06 20:33:17 -0800213 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!dev->interrupt) {
215 kfree (buf);
216 return -ENOMEM;
217 } else {
218 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
219 buf, maxp, intr_complete, dev, period);
220 dev_dbg(&intf->dev,
221 "status ep%din, %d bytes period %d\n",
222 usb_pipeendpoint(pipe), maxp, period);
223 }
224 }
David Brownell18ab4582007-05-25 12:31:32 -0700225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
David Brownell2e55cc72005-08-31 09:53:10 -0700228/* Passes this packet up the stack, updating its accounting.
229 * Some link protocols batch packets, so their rx_fixup paths
230 * can return clones as well as just modify the original skb.
231 */
232void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int status;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 skb->protocol = eth_type_trans (skb, dev->net);
237 dev->stats.rx_packets++;
238 dev->stats.rx_bytes += skb->len;
239
240 if (netif_msg_rx_status (dev))
Al Viro5330e922005-04-26 11:26:53 -0700241 devdbg (dev, "< rx, len %zu, type 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 skb->len + sizeof (struct ethhdr), skb->protocol);
243 memset (skb->cb, 0, sizeof (struct skb_data));
244 status = netif_rx (skb);
245 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
246 devdbg (dev, "netif_rx status %d", status);
247}
David Brownell2e55cc72005-08-31 09:53:10 -0700248EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*-------------------------------------------------------------------------
252 *
253 * Network Device Driver (peer link to "Host Device", from USB host)
254 *
255 *-------------------------------------------------------------------------*/
256
Stephen Hemminger777baa42009-03-20 19:35:54 +0000257int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700260 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400261 int old_hard_mtu = dev->hard_mtu;
262 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jamie Paintera99c1942006-07-27 14:17:28 -0400264 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700267 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EDOM;
269 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400270
271 dev->hard_mtu = net->mtu + net->hard_header_len;
272 if (dev->rx_urb_size == old_hard_mtu) {
273 dev->rx_urb_size = dev->hard_mtu;
274 if (dev->rx_urb_size > old_rx_urb_size)
275 usbnet_unlink_rx_urbs(dev);
276 }
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000280EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/*-------------------------------------------------------------------------*/
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
285 * completion callbacks. 2.5 should have fixed those bugs...
286 */
287
David S. Miller8728b832005-08-09 19:25:21 -0700288static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned long flags;
291
David S. Miller8728b832005-08-09 19:25:21 -0700292 spin_lock_irqsave(&list->lock, flags);
293 __skb_unlink(skb, list);
294 spin_unlock(&list->lock);
295 spin_lock(&dev->done.lock);
296 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700298 tasklet_schedule(&dev->bh);
299 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/* some work can't be done in tasklets, so we use keventd
303 *
304 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
305 * but tasklet_schedule() doesn't. hope the failure is rare.
306 */
David Brownell2e55cc72005-08-31 09:53:10 -0700307void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 set_bit (work, &dev->flags);
310 if (!schedule_work (&dev->kevent))
311 deverr (dev, "kevent %d may have been dropped", work);
312 else
313 devdbg (dev, "kevent %d scheduled", work);
314}
David Brownell2e55cc72005-08-31 09:53:10 -0700315EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/*-------------------------------------------------------------------------*/
318
David Howells7d12e782006-10-05 14:55:46 +0100319static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Al Viro55016f12005-10-21 03:21:58 -0400321static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct sk_buff *skb;
324 struct skb_data *entry;
325 int retval = 0;
326 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700327 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
330 if (netif_msg_rx_err (dev))
331 devdbg (dev, "no rx skb");
David Brownell2e55cc72005-08-31 09:53:10 -0700332 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 usb_free_urb (urb);
334 return;
335 }
336 skb_reserve (skb, NET_IP_ALIGN);
337
338 entry = (struct skb_data *) skb->cb;
339 entry->urb = urb;
340 entry->dev = dev;
341 entry->state = rx_start;
342 entry->length = 0;
343
344 usb_fill_bulk_urb (urb, dev->udev, dev->in,
345 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 spin_lock_irqsave (&dev->rxq.lock, lockflags);
348
349 if (netif_running (dev->net)
350 && netif_device_present (dev->net)
351 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700352 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700354 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700357 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359 case -ENODEV:
360 if (netif_msg_ifdown (dev))
361 devdbg (dev, "device gone");
362 netif_device_detach (dev->net);
363 break;
364 default:
365 if (netif_msg_rx_err (dev))
366 devdbg (dev, "rx submit, %d", retval);
367 tasklet_schedule (&dev->bh);
368 break;
369 case 0:
370 __skb_queue_tail (&dev->rxq, skb);
371 }
372 } else {
373 if (netif_msg_ifdown (dev))
374 devdbg (dev, "rx: stopped");
375 retval = -ENOLINK;
376 }
377 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
378 if (retval) {
379 dev_kfree_skb_any (skb);
380 usb_free_urb (urb);
381 }
382}
383
384
385/*-------------------------------------------------------------------------*/
386
387static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
388{
389 if (dev->driver_info->rx_fixup
390 && !dev->driver_info->rx_fixup (dev, skb))
391 goto error;
392 // else network stack removes extra byte if we forced a short packet
393
394 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700395 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 else {
397 if (netif_msg_rx_err (dev))
398 devdbg (dev, "drop");
399error:
400 dev->stats.rx_errors++;
401 skb_queue_tail (&dev->done, skb);
402 }
403}
404
405/*-------------------------------------------------------------------------*/
406
David Howells7d12e782006-10-05 14:55:46 +0100407static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct sk_buff *skb = (struct sk_buff *) urb->context;
410 struct skb_data *entry = (struct skb_data *) skb->cb;
411 struct usbnet *dev = entry->dev;
412 int urb_status = urb->status;
413
414 skb_put (skb, urb->actual_length);
415 entry->state = rx_done;
416 entry->urb = NULL;
417
418 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700419 /* success */
420 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700421 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 entry->state = rx_cleanup;
423 dev->stats.rx_errors++;
424 dev->stats.rx_length_errors++;
425 if (netif_msg_rx_err (dev))
426 devdbg (dev, "rx length %d", skb->len);
427 }
428 break;
429
David Brownell18ab4582007-05-25 12:31:32 -0700430 /* stalls need manual reset. this is rare ... except that
431 * when going through USB 2.0 TTs, unplug appears this way.
432 * we avoid the highspeed version of the ETIMEOUT/EILSEQ
433 * storm, recovering as needed.
434 */
435 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 dev->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700437 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 // FALLTHROUGH
439
David Brownell18ab4582007-05-25 12:31:32 -0700440 /* software-driven interface shutdown */
441 case -ECONNRESET: /* async unlink */
442 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (netif_msg_ifdown (dev))
444 devdbg (dev, "rx shutdown, code %d", urb_status);
445 goto block;
446
David Brownell18ab4582007-05-25 12:31:32 -0700447 /* we get controller i/o faults during khubd disconnect() delays.
448 * throttle down resubmits, to avoid log floods; just temporarily,
449 * so we still recover when the fault isn't a khubd delay.
450 */
451 case -EPROTO:
452 case -ETIME:
453 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 dev->stats.rx_errors++;
455 if (!timer_pending (&dev->delay)) {
456 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
457 if (netif_msg_link (dev))
458 devdbg (dev, "rx throttle %d", urb_status);
459 }
460block:
461 entry->state = rx_cleanup;
462 entry->urb = urb;
463 urb = NULL;
464 break;
465
David Brownell18ab4582007-05-25 12:31:32 -0700466 /* data overrun ... flush fifo? */
467 case -EOVERFLOW:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dev->stats.rx_over_errors++;
469 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800470
David Brownell18ab4582007-05-25 12:31:32 -0700471 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 entry->state = rx_cleanup;
473 dev->stats.rx_errors++;
474 if (netif_msg_rx_err (dev))
475 devdbg (dev, "rx status %d", urb_status);
476 break;
477 }
478
David S. Miller8728b832005-08-09 19:25:21 -0700479 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (urb) {
482 if (netif_running (dev->net)
483 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
484 rx_submit (dev, urb, GFP_ATOMIC);
485 return;
486 }
487 usb_free_urb (urb);
488 }
489 if (netif_msg_rx_err (dev))
490 devdbg (dev, "no read resubmitted");
491}
492
David Howells7d12e782006-10-05 14:55:46 +0100493static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct usbnet *dev = urb->context;
496 int status = urb->status;
497
498 switch (status) {
David Brownell18ab4582007-05-25 12:31:32 -0700499 /* success */
500 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dev->driver_info->status(dev, urb);
502 break;
503
David Brownell18ab4582007-05-25 12:31:32 -0700504 /* software-driven interface shutdown */
505 case -ENOENT: /* urb killed */
506 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (netif_msg_ifdown (dev))
508 devdbg (dev, "intr shutdown, code %d", status);
509 return;
510
David Brownell18ab4582007-05-25 12:31:32 -0700511 /* NOTE: not throttling like RX/TX, since this endpoint
512 * already polls infrequently
513 */
514 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 devdbg (dev, "intr status %d", status);
516 break;
517 }
518
519 if (!netif_running (dev->net))
520 return;
521
522 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
523 status = usb_submit_urb (urb, GFP_ATOMIC);
524 if (status != 0 && netif_msg_timer (dev))
525 deverr(dev, "intr resubmit --> %d", status);
526}
527
528/*-------------------------------------------------------------------------*/
529
530// unlink pending rx/tx; completion handlers do all other cleanup
531
532static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
533{
534 unsigned long flags;
535 struct sk_buff *skb, *skbnext;
536 int count = 0;
537
538 spin_lock_irqsave (&q->lock, flags);
David S. Miller83bfba52008-09-22 20:18:47 -0700539 skb_queue_walk_safe(q, skb, skbnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct skb_data *entry;
541 struct urb *urb;
542 int retval;
543
544 entry = (struct skb_data *) skb->cb;
545 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 // during some PM-driven resume scenarios,
548 // these (async) unlinks complete immediately
549 retval = usb_unlink_urb (urb);
550 if (retval != -EINPROGRESS && retval != 0)
551 devdbg (dev, "unlink urb err, %d", retval);
552 else
553 count++;
554 }
555 spin_unlock_irqrestore (&q->lock, flags);
556 return count;
557}
558
Jamie Paintera99c1942006-07-27 14:17:28 -0400559// Flush all pending rx urbs
560// minidrivers may need to do this when the MTU changes
561
562void usbnet_unlink_rx_urbs(struct usbnet *dev)
563{
564 if (netif_running(dev->net)) {
565 (void) unlink_urbs (dev, &dev->rxq);
566 tasklet_schedule(&dev->bh);
567 }
568}
569EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571/*-------------------------------------------------------------------------*/
572
573// precondition: never called in_interrupt
574
Stephen Hemminger777baa42009-03-20 19:35:54 +0000575int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct usbnet *dev = netdev_priv(net);
578 int temp;
Peter Zijlstra7259f0d2006-10-29 22:46:36 -0800579 DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 DECLARE_WAITQUEUE (wait, current);
581
582 netif_stop_queue (net);
583
584 if (netif_msg_ifdown (dev))
585 devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
David Brownellcb1cebb2007-02-15 18:52:30 -0800586 dev->stats.rx_packets, dev->stats.tx_packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 dev->stats.rx_errors, dev->stats.tx_errors
588 );
589
590 // ensure there are no more active urbs
591 add_wait_queue (&unlink_wakeup, &wait);
592 dev->wait = &unlink_wakeup;
593 temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
594
595 // maybe wait for deletions to finish.
David Brownell18ab4582007-05-25 12:31:32 -0700596 while (!skb_queue_empty(&dev->rxq)
597 && !skb_queue_empty(&dev->txq)
598 && !skb_queue_empty(&dev->done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 msleep(UNLINK_TIMEOUT_MS);
600 if (netif_msg_ifdown (dev))
601 devdbg (dev, "waited for %d urb completions", temp);
602 }
603 dev->wait = NULL;
David Brownellcb1cebb2007-02-15 18:52:30 -0800604 remove_wait_queue (&unlink_wakeup, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 usb_kill_urb(dev->interrupt);
607
608 /* deferred work (task, timer, softirq) must also stop.
609 * can't flush_scheduled_work() until we drop rtnl (later),
610 * else workers could deadlock; so make workers a NOP.
611 */
612 dev->flags = 0;
613 del_timer_sync (&dev->delay);
614 tasklet_kill (&dev->bh);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200615 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 return 0;
618}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000619EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621/*-------------------------------------------------------------------------*/
622
623// posts reads, and enables write queuing
624
625// precondition: never called in_interrupt
626
Stephen Hemminger777baa42009-03-20 19:35:54 +0000627int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200630 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct driver_info *info = dev->driver_info;
632
Oliver Neukuma11a6542007-08-03 13:52:19 +0200633 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
634 if (netif_msg_ifup (dev))
635 devinfo (dev,
636 "resumption fail (%d) usbnet usb-%s-%s, %s",
637 retval,
638 dev->udev->bus->bus_name, dev->udev->devpath,
639 info->description);
640 goto done_nopm;
641 }
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 // put into "known safe" state
644 if (info->reset && (retval = info->reset (dev)) < 0) {
645 if (netif_msg_ifup (dev))
646 devinfo (dev,
647 "open reset fail (%d) usbnet usb-%s-%s, %s",
648 retval,
649 dev->udev->bus->bus_name, dev->udev->devpath,
650 info->description);
651 goto done;
652 }
653
654 // insist peer be connected
655 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
656 if (netif_msg_ifup (dev))
657 devdbg (dev, "can't open; %d", retval);
658 goto done;
659 }
660
661 /* start any status interrupt transfer */
662 if (dev->interrupt) {
663 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
664 if (retval < 0) {
665 if (netif_msg_ifup (dev))
666 deverr (dev, "intr submit %d", retval);
667 goto done;
668 }
669 }
670
671 netif_start_queue (net);
672 if (netif_msg_ifup (dev)) {
673 char *framing;
674
675 if (dev->driver_info->flags & FLAG_FRAMING_NC)
676 framing = "NetChip";
677 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
678 framing = "GeneSys";
679 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
680 framing = "Zaurus";
681 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
682 framing = "RNDIS";
683 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
684 framing = "ASIX";
685 else
686 framing = "simple";
687
688 devinfo (dev, "open: enable queueing "
689 "(rx %d, tx %d) mtu %d %s framing",
Randy Dunlap25d94e62006-08-07 15:56:40 -0700690 (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 framing);
692 }
693
694 // delay posting reads until we're fully open
695 tasklet_schedule (&dev->bh);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200696 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200698 usb_autopm_put_interface(dev->intf);
699done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return retval;
701}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000702EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704/*-------------------------------------------------------------------------*/
705
David Brownell2e55cc72005-08-31 09:53:10 -0700706/* ethtool methods; minidrivers may need to add some more, but
707 * they'll probably want to use this base set.
708 */
709
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200710int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
711{
712 struct usbnet *dev = netdev_priv(net);
713
714 if (!dev->mii.mdio_read)
715 return -EOPNOTSUPP;
716
717 return mii_ethtool_gset(&dev->mii, cmd);
718}
719EXPORT_SYMBOL_GPL(usbnet_get_settings);
720
721int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
722{
723 struct usbnet *dev = netdev_priv(net);
724 int retval;
725
726 if (!dev->mii.mdio_write)
727 return -EOPNOTSUPP;
728
729 retval = mii_ethtool_sset(&dev->mii, cmd);
730
731 /* link speed/duplex might have changed */
732 if (dev->driver_info->link_reset)
733 dev->driver_info->link_reset(dev);
734
735 return retval;
736
737}
738EXPORT_SYMBOL_GPL(usbnet_set_settings);
739
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200740u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 struct usbnet *dev = netdev_priv(net);
743
David Brownellf29fc252005-08-31 09:52:31 -0700744 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (dev->driver_info->check_connect)
746 return dev->driver_info->check_connect (dev) == 0;
747
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200748 /* if the device has mii operations, use those */
749 if (dev->mii.mdio_read)
750 return mii_link_ok(&dev->mii);
751
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800752 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
753 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200755EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
David Brownell18ee91fa2006-11-02 12:29:12 -0800757int usbnet_nway_reset(struct net_device *net)
758{
759 struct usbnet *dev = netdev_priv(net);
760
761 if (!dev->mii.mdio_write)
762 return -EOPNOTSUPP;
763
764 return mii_nway_restart(&dev->mii);
765}
766EXPORT_SYMBOL_GPL(usbnet_nway_reset);
767
David Brownell18ee91fa2006-11-02 12:29:12 -0800768void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
769{
770 struct usbnet *dev = netdev_priv(net);
771
David Brownell296c0242007-04-17 16:10:10 -0700772 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800773 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
774 strncpy (info->fw_version, dev->driver_info->description,
775 sizeof info->fw_version);
776 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
777}
778EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
779
David Brownell2e55cc72005-08-31 09:53:10 -0700780u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct usbnet *dev = netdev_priv(net);
783
784 return dev->msg_enable;
785}
David Brownell2e55cc72005-08-31 09:53:10 -0700786EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David Brownell2e55cc72005-08-31 09:53:10 -0700788void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct usbnet *dev = netdev_priv(net);
791
792 dev->msg_enable = level;
793}
David Brownell2e55cc72005-08-31 09:53:10 -0700794EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
David Brownell2e55cc72005-08-31 09:53:10 -0700796/* drivers may override default ethtool_ops in their bind() routine */
797static struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200798 .get_settings = usbnet_get_settings,
799 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700800 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200801 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800802 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700803 .get_msglevel = usbnet_get_msglevel,
804 .set_msglevel = usbnet_set_msglevel,
805};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807/*-------------------------------------------------------------------------*/
808
809/* work that cannot be done in interrupt context uses keventd.
810 *
811 * NOTE: with 2.5 we could do more of this using completion callbacks,
812 * especially now that control transfers can be queued.
813 */
814static void
David Howellsc4028952006-11-22 14:57:56 +0000815kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
David Howellsc4028952006-11-22 14:57:56 +0000817 struct usbnet *dev =
818 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 int status;
820
821 /* usb_clear_halt() needs a thread context */
822 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
823 unlink_urbs (dev, &dev->txq);
824 status = usb_clear_halt (dev->udev, dev->out);
David Brownellf29fc252005-08-31 09:52:31 -0700825 if (status < 0
826 && status != -EPIPE
827 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (netif_msg_tx_err (dev))
829 deverr (dev, "can't clear tx halt, status %d",
830 status);
831 } else {
832 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700833 if (status != -ESHUTDOWN)
834 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836 }
837 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
838 unlink_urbs (dev, &dev->rxq);
839 status = usb_clear_halt (dev->udev, dev->in);
David Brownellf29fc252005-08-31 09:52:31 -0700840 if (status < 0
841 && status != -EPIPE
842 && status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (netif_msg_rx_err (dev))
844 deverr (dev, "can't clear rx halt, status %d",
845 status);
846 } else {
847 clear_bit (EVENT_RX_HALT, &dev->flags);
848 tasklet_schedule (&dev->bh);
849 }
850 }
851
852 /* tasklet could resubmit itself forever if memory is tight */
853 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
854 struct urb *urb = NULL;
855
856 if (netif_running (dev->net))
857 urb = usb_alloc_urb (0, GFP_KERNEL);
858 else
859 clear_bit (EVENT_RX_MEMORY, &dev->flags);
860 if (urb != NULL) {
861 clear_bit (EVENT_RX_MEMORY, &dev->flags);
862 rx_submit (dev, urb, GFP_KERNEL);
863 tasklet_schedule (&dev->bh);
864 }
865 }
866
David Hollis7ea13c92005-04-22 15:07:02 -0700867 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800868 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -0700869 int retval = 0;
870
871 clear_bit (EVENT_LINK_RESET, &dev->flags);
872 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
873 devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
874 retval,
875 dev->udev->bus->bus_name, dev->udev->devpath,
876 info->description);
877 }
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (dev->flags)
881 devdbg (dev, "kevent done, flags = 0x%lx",
882 dev->flags);
883}
884
885/*-------------------------------------------------------------------------*/
886
David Howells7d12e782006-10-05 14:55:46 +0100887static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct sk_buff *skb = (struct sk_buff *) urb->context;
890 struct skb_data *entry = (struct skb_data *) skb->cb;
891 struct usbnet *dev = entry->dev;
892
893 if (urb->status == 0) {
894 dev->stats.tx_packets++;
895 dev->stats.tx_bytes += entry->length;
896 } else {
897 dev->stats.tx_errors++;
898
899 switch (urb->status) {
900 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700901 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903
904 /* software-driven interface shutdown */
905 case -ECONNRESET: // async unlink
906 case -ESHUTDOWN: // hardware gone
907 break;
908
909 // like rx, tx gets controller i/o faults during khubd delays
910 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700911 case -EPROTO:
912 case -ETIME:
913 case -EILSEQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!timer_pending (&dev->delay)) {
915 mod_timer (&dev->delay,
916 jiffies + THROTTLE_JIFFIES);
917 if (netif_msg_link (dev))
918 devdbg (dev, "tx throttle %d",
919 urb->status);
920 }
921 netif_stop_queue (dev->net);
922 break;
923 default:
924 if (netif_msg_tx_err (dev))
925 devdbg (dev, "tx err %d", entry->urb->status);
926 break;
927 }
928 }
929
930 urb->dev = NULL;
931 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -0700932 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/*-------------------------------------------------------------------------*/
936
Stephen Hemminger777baa42009-03-20 19:35:54 +0000937void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct usbnet *dev = netdev_priv(net);
940
941 unlink_urbs (dev, &dev->txq);
942 tasklet_schedule (&dev->bh);
943
944 // FIXME: device recovery -- reset?
945}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000946EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948/*-------------------------------------------------------------------------*/
949
Stephen Hemminger777baa42009-03-20 19:35:54 +0000950int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct usbnet *dev = netdev_priv(net);
953 int length;
954 int retval = NET_XMIT_SUCCESS;
955 struct urb *urb = NULL;
956 struct skb_data *entry;
957 struct driver_info *info = dev->driver_info;
958 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 // some devices want funky USB-level framing, for
961 // win32 driver (usually) and/or hardware quirks
962 if (info->tx_fixup) {
963 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
964 if (!skb) {
965 if (netif_msg_tx_err (dev))
966 devdbg (dev, "can't tx_fixup skb");
967 goto drop;
968 }
969 }
970 length = skb->len;
971
972 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
973 if (netif_msg_tx_err (dev))
974 devdbg (dev, "no urb");
975 goto drop;
976 }
977
978 entry = (struct skb_data *) skb->cb;
979 entry->urb = urb;
980 entry->dev = dev;
981 entry->state = tx_start;
982 entry->length = length;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 usb_fill_bulk_urb (urb, dev->udev, dev->out,
985 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* don't assume the hardware handles USB_ZERO_PACKET
988 * NOTE: strictly conforming cdc-ether devices should expect
989 * the ZLP here, but ignore the one-byte packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Peter Korsgaard3e323f32007-06-27 08:48:15 +0200991 if ((length % dev->maxpacket) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 urb->transfer_buffer_length++;
Peter Korsgaard3e323f32007-06-27 08:48:15 +0200993 if (skb_tailroom(skb)) {
994 skb->data[skb->len] = 0;
995 __skb_put(skb, 1);
996 }
997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 spin_lock_irqsave (&dev->txq.lock, flags);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1002 case -EPIPE:
1003 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001004 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006 default:
1007 if (netif_msg_tx_err (dev))
1008 devdbg (dev, "tx: submit urb err %d", retval);
1009 break;
1010 case 0:
1011 net->trans_start = jiffies;
1012 __skb_queue_tail (&dev->txq, skb);
1013 if (dev->txq.qlen >= TX_QLEN (dev))
1014 netif_stop_queue (net);
1015 }
1016 spin_unlock_irqrestore (&dev->txq.lock, flags);
1017
1018 if (retval) {
1019 if (netif_msg_tx_err (dev))
1020 devdbg (dev, "drop, code %d", retval);
1021drop:
1022 retval = NET_XMIT_SUCCESS;
1023 dev->stats.tx_dropped++;
1024 if (skb)
1025 dev_kfree_skb_any (skb);
1026 usb_free_urb (urb);
1027 } else if (netif_msg_tx_queued (dev)) {
1028 devdbg (dev, "> tx, len %d, type 0x%x",
1029 length, skb->protocol);
1030 }
1031 return retval;
1032}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001033EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035/*-------------------------------------------------------------------------*/
1036
1037// tasklet (work deferred from completions, in_irq) or timer
1038
1039static void usbnet_bh (unsigned long param)
1040{
1041 struct usbnet *dev = (struct usbnet *) param;
1042 struct sk_buff *skb;
1043 struct skb_data *entry;
1044
1045 while ((skb = skb_dequeue (&dev->done))) {
1046 entry = (struct skb_data *) skb->cb;
1047 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001048 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 entry->state = rx_cleanup;
1050 rx_process (dev, skb);
1051 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001052 case tx_done:
1053 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 usb_free_urb (entry->urb);
1055 dev_kfree_skb (skb);
1056 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001057 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 devdbg (dev, "bogus skb state %d", entry->state);
1059 }
1060 }
1061
1062 // waiting for all pending urbs to complete?
1063 if (dev->wait) {
1064 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1065 wake_up (dev->wait);
1066 }
1067
1068 // or are we maybe short a few urbs?
1069 } else if (netif_running (dev->net)
1070 && netif_device_present (dev->net)
1071 && !timer_pending (&dev->delay)
1072 && !test_bit (EVENT_RX_HALT, &dev->flags)) {
1073 int temp = dev->rxq.qlen;
1074 int qlen = RX_QLEN (dev);
1075
1076 if (temp < qlen) {
1077 struct urb *urb;
1078 int i;
1079
1080 // don't refill the queue all at once
1081 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1082 urb = usb_alloc_urb (0, GFP_ATOMIC);
1083 if (urb != NULL)
1084 rx_submit (dev, urb, GFP_ATOMIC);
1085 }
1086 if (temp != dev->rxq.qlen && netif_msg_link (dev))
1087 devdbg (dev, "rxqlen %d --> %d",
1088 temp, dev->rxq.qlen);
1089 if (dev->rxq.qlen < qlen)
1090 tasklet_schedule (&dev->bh);
1091 }
1092 if (dev->txq.qlen < TX_QLEN (dev))
1093 netif_wake_queue (dev->net);
1094 }
1095}
1096
1097
1098
1099/*-------------------------------------------------------------------------
1100 *
1101 * USB Device Driver support
1102 *
1103 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105// precondition: never called in_interrupt
1106
David Brownell38bde1d2005-08-31 09:52:45 -07001107void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 struct usbnet *dev;
1110 struct usb_device *xdev;
1111 struct net_device *net;
1112
1113 dev = usb_get_intfdata(intf);
1114 usb_set_intfdata(intf, NULL);
1115 if (!dev)
1116 return;
1117
1118 xdev = interface_to_usbdev (intf);
1119
1120 if (netif_msg_probe (dev))
David Brownell38bde1d2005-08-31 09:52:45 -07001121 devinfo (dev, "unregister '%s' usb-%s-%s, %s",
1122 intf->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 xdev->bus->bus_name, xdev->devpath,
1124 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 net = dev->net;
1127 unregister_netdev (net);
1128
1129 /* we don't hold rtnl here ... */
1130 flush_scheduled_work ();
1131
1132 if (dev->driver_info->unbind)
1133 dev->driver_info->unbind (dev, intf);
1134
1135 free_netdev(net);
1136 usb_put_dev (xdev);
1137}
David Brownell38bde1d2005-08-31 09:52:45 -07001138EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Stephen Hemminger777baa42009-03-20 19:35:54 +00001140static const struct net_device_ops usbnet_netdev_ops = {
1141 .ndo_open = usbnet_open,
1142 .ndo_stop = usbnet_stop,
1143 .ndo_start_xmit = usbnet_start_xmit,
1144 .ndo_tx_timeout = usbnet_tx_timeout,
1145 .ndo_change_mtu = usbnet_change_mtu,
1146 .ndo_set_mac_address = eth_mac_addr,
1147 .ndo_validate_addr = eth_validate_addr,
1148};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150/*-------------------------------------------------------------------------*/
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152// precondition: never called in_interrupt
1153
David Brownell38bde1d2005-08-31 09:52:45 -07001154int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1156{
1157 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001158 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct usb_host_interface *interface;
1160 struct driver_info *info;
1161 struct usb_device *xdev;
1162 int status;
David Brownell296c0242007-04-17 16:10:10 -07001163 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
David Brownell296c0242007-04-17 16:10:10 -07001165 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 info = (struct driver_info *) prod->driver_info;
1167 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001168 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -ENODEV;
1170 }
1171 xdev = interface_to_usbdev (udev);
1172 interface = udev->cur_altsetting;
1173
1174 usb_get_dev (xdev);
1175
1176 status = -ENOMEM;
1177
1178 // set up our own records
1179 net = alloc_etherdev(sizeof(*dev));
1180 if (!net) {
1181 dbg ("can't kmalloc dev");
1182 goto out;
1183 }
1184
1185 dev = netdev_priv(net);
1186 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001187 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001189 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1191 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1192 skb_queue_head_init (&dev->rxq);
1193 skb_queue_head_init (&dev->txq);
1194 skb_queue_head_init (&dev->done);
1195 dev->bh.func = usbnet_bh;
1196 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001197 INIT_WORK (&dev->kevent, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 dev->delay.function = usbnet_bh;
1199 dev->delay.data = (unsigned long) dev;
1200 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001201 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dev->net = net;
1204 strcpy (net->name, "usb%d");
1205 memcpy (net->dev_addr, node_id, sizeof node_id);
1206
David Brownell2e55cc72005-08-31 09:53:10 -07001207 /* rx and tx sides can use different message sizes;
1208 * bind() should set rx_urb_size in that case.
1209 */
1210 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211#if 0
1212// dma_supported() is deeply broken on almost all architectures
1213 // possible with some EHCI controllers
Yang Hongyang6a355282009-04-06 19:01:13 -07001214 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 net->features |= NETIF_F_HIGHDMA;
1216#endif
1217
Stephen Hemminger777baa42009-03-20 19:35:54 +00001218 net->netdev_ops = &usbnet_netdev_ops;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001219 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 net->ethtool_ops = &usbnet_ethtool_ops;
1221
1222 // allow device-specific bind/init procedures
1223 // NOTE net->name still not usable ...
1224 if (info->bind) {
1225 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001226 if (status < 0)
1227 goto out1;
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 // heuristic: "usb%d" for links we know are two-host,
1230 // else "eth%d" when there's reasonable doubt. userspace
1231 // can rename the link if it knows better.
1232 if ((dev->driver_info->flags & FLAG_ETHER) != 0
1233 && (net->dev_addr [0] & 0x02) == 0)
1234 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001235 /* WLAN devices should always be named "wlan%d" */
1236 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1237 strcpy(net->name, "wlan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001238
1239 /* maybe the remote can't receive an Ethernet MTU */
1240 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1241 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001242 } else if (!info->in || !info->out)
1243 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 else {
1245 dev->in = usb_rcvbulkpipe (xdev, info->in);
1246 dev->out = usb_sndbulkpipe (xdev, info->out);
1247 if (!(info->flags & FLAG_NO_SETINT))
1248 status = usb_set_interface (xdev,
1249 interface->desc.bInterfaceNumber,
1250 interface->desc.bAlternateSetting);
1251 else
1252 status = 0;
1253
1254 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001255 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 status = init_status (dev, udev);
1257 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001258 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
David Brownell2e55cc72005-08-31 09:53:10 -07001260 if (!dev->rx_urb_size)
1261 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 SET_NETDEV_DEV(net, &udev->dev);
1265 status = register_netdev (net);
1266 if (status)
1267 goto out3;
1268 if (netif_msg_probe (dev))
Johannes Berge1749612008-10-27 15:59:26 -07001269 devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM",
David Brownell38bde1d2005-08-31 09:52:45 -07001270 udev->dev.driver->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 xdev->bus->bus_name, xdev->devpath,
1272 dev->driver_info->description,
Johannes Berge1749612008-10-27 15:59:26 -07001273 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 // ok, it's ready to go.
1276 usb_set_intfdata (udev, dev);
1277
1278 // start as if the link is up
1279 netif_device_attach (net);
1280
1281 return 0;
1282
1283out3:
1284 if (info->unbind)
1285 info->unbind (dev, udev);
1286out1:
1287 free_netdev(net);
1288out:
1289 usb_put_dev(xdev);
1290 return status;
1291}
David Brownell38bde1d2005-08-31 09:52:45 -07001292EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294/*-------------------------------------------------------------------------*/
1295
Oliver Neukum36433122007-04-30 01:37:44 -07001296/*
1297 * suspend the whole driver as soon as the first interface is suspended
1298 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
David Brownell38bde1d2005-08-31 09:52:45 -07001301int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001304
Oliver Neukum36433122007-04-30 01:37:44 -07001305 if (!dev->suspend_count++) {
Oliver Neukuma11a6542007-08-03 13:52:19 +02001306 /*
1307 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001308 * having everything error out.
1309 */
1310 netif_device_detach (dev->net);
1311 (void) unlink_urbs (dev, &dev->rxq);
1312 (void) unlink_urbs (dev, &dev->txq);
Oliver Neukuma11a6542007-08-03 13:52:19 +02001313 /*
1314 * reattach so runtime management can use and
1315 * wake the device
1316 */
1317 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
1320}
David Brownell38bde1d2005-08-31 09:52:45 -07001321EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
David Brownell38bde1d2005-08-31 09:52:45 -07001323int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct usbnet *dev = usb_get_intfdata(intf);
1326
Oliver Neukuma11a6542007-08-03 13:52:19 +02001327 if (!--dev->suspend_count)
Oliver Neukum36433122007-04-30 01:37:44 -07001328 tasklet_schedule (&dev->bh);
Oliver Neukuma11a6542007-08-03 13:52:19 +02001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return 0;
1331}
David Brownell38bde1d2005-08-31 09:52:45 -07001332EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335/*-------------------------------------------------------------------------*/
1336
David Brownellf29fc252005-08-31 09:52:31 -07001337static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
David Brownell090ffa92005-08-31 09:54:50 -07001339 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001340 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 random_ether_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
David Brownellf29fc252005-08-31 09:52:31 -07001346module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
David Brownellf29fc252005-08-31 09:52:31 -07001348static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
David Brownellf29fc252005-08-31 09:52:31 -07001351module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
David Brownellf29fc252005-08-31 09:52:31 -07001353MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001354MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001355MODULE_LICENSE("GPL");