blob: 8e732930d249f6cb4796dc992a3a2e874e0a06e3 [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
Joe Perches8e95a202009-12-03 07:58:21 +0000143 if (alt->desc.bAlternateSetting != 0 ||
144 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300236 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
237 skb_queue_tail(&dev->rxq_pause, skb);
238 return;
239 }
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 skb->protocol = eth_type_trans (skb, dev->net);
Herbert Xu79638372009-06-29 16:53:28 +0000242 dev->net->stats.rx_packets++;
243 dev->net->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if (netif_msg_rx_status (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000246 netdev_dbg(dev->net, "< rx, len %zu, type 0x%x\n",
247 skb->len + sizeof (struct ethhdr), skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 memset (skb->cb, 0, sizeof (struct skb_data));
249 status = netif_rx (skb);
250 if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000251 netdev_dbg(dev->net, "netif_rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
David Brownell2e55cc72005-08-31 09:53:10 -0700253EXPORT_SYMBOL_GPL(usbnet_skb_return);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*-------------------------------------------------------------------------
257 *
258 * Network Device Driver (peer link to "Host Device", from USB host)
259 *
260 *-------------------------------------------------------------------------*/
261
Stephen Hemminger777baa42009-03-20 19:35:54 +0000262int usbnet_change_mtu (struct net_device *net, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 struct usbnet *dev = netdev_priv(net);
David Brownellf29fc252005-08-31 09:52:31 -0700265 int ll_mtu = new_mtu + net->hard_header_len;
Jamie Paintera99c1942006-07-27 14:17:28 -0400266 int old_hard_mtu = dev->hard_mtu;
267 int old_rx_urb_size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Jamie Paintera99c1942006-07-27 14:17:28 -0400269 if (new_mtu <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 // no second zero-length packet read wanted after mtu-sized packets
David Brownellf29fc252005-08-31 09:52:31 -0700272 if ((ll_mtu % dev->maxpacket) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EDOM;
274 net->mtu = new_mtu;
Jamie Paintera99c1942006-07-27 14:17:28 -0400275
276 dev->hard_mtu = net->mtu + net->hard_header_len;
277 if (dev->rx_urb_size == old_hard_mtu) {
278 dev->rx_urb_size = dev->hard_mtu;
279 if (dev->rx_urb_size > old_rx_urb_size)
280 usbnet_unlink_rx_urbs(dev);
281 }
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000285EXPORT_SYMBOL_GPL(usbnet_change_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287/*-------------------------------------------------------------------------*/
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
290 * completion callbacks. 2.5 should have fixed those bugs...
291 */
292
David S. Miller8728b832005-08-09 19:25:21 -0700293static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned long flags;
296
David S. Miller8728b832005-08-09 19:25:21 -0700297 spin_lock_irqsave(&list->lock, flags);
298 __skb_unlink(skb, list);
299 spin_unlock(&list->lock);
300 spin_lock(&dev->done.lock);
301 __skb_queue_tail(&dev->done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (dev->done.qlen == 1)
David S. Miller8728b832005-08-09 19:25:21 -0700303 tasklet_schedule(&dev->bh);
304 spin_unlock_irqrestore(&dev->done.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/* some work can't be done in tasklets, so we use keventd
308 *
309 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
310 * but tasklet_schedule() doesn't. hope the failure is rare.
311 */
David Brownell2e55cc72005-08-31 09:53:10 -0700312void usbnet_defer_kevent (struct usbnet *dev, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 set_bit (work, &dev->flags);
315 if (!schedule_work (&dev->kevent))
Joe Perches60b86752010-02-17 10:30:23 +0000316 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 else
Joe Perches60b86752010-02-17 10:30:23 +0000318 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
David Brownell2e55cc72005-08-31 09:53:10 -0700320EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322/*-------------------------------------------------------------------------*/
323
David Howells7d12e782006-10-05 14:55:46 +0100324static void rx_complete (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Al Viro55016f12005-10-21 03:21:58 -0400326static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct sk_buff *skb;
329 struct skb_data *entry;
330 int retval = 0;
331 unsigned long lockflags;
David Brownell2e55cc72005-08-31 09:53:10 -0700332 size_t size = dev->rx_urb_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
335 if (netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000336 netdev_dbg(dev->net, "no rx skb\n");
David Brownell2e55cc72005-08-31 09:53:10 -0700337 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 usb_free_urb (urb);
339 return;
340 }
341 skb_reserve (skb, NET_IP_ALIGN);
342
343 entry = (struct skb_data *) skb->cb;
344 entry->urb = urb;
345 entry->dev = dev;
346 entry->state = rx_start;
347 entry->length = 0;
348
349 usb_fill_bulk_urb (urb, dev->udev, dev->in,
350 skb->data, size, rx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 spin_lock_irqsave (&dev->rxq.lock, lockflags);
353
Joe Perches8e95a202009-12-03 07:58:21 +0000354 if (netif_running (dev->net) &&
355 netif_device_present (dev->net) &&
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800356 !test_bit (EVENT_RX_HALT, &dev->flags) &&
357 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
David Brownell18ab4582007-05-25 12:31:32 -0700358 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -0700360 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 case -ENOMEM:
David Brownell2e55cc72005-08-31 09:53:10 -0700363 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 case -ENODEV:
366 if (netif_msg_ifdown (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000367 netdev_dbg(dev->net, "device gone\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 netif_device_detach (dev->net);
369 break;
370 default:
371 if (netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000372 netdev_dbg(dev->net, "rx submit, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 tasklet_schedule (&dev->bh);
374 break;
375 case 0:
376 __skb_queue_tail (&dev->rxq, skb);
377 }
378 } else {
379 if (netif_msg_ifdown (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000380 netdev_dbg(dev->net, "rx: stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 retval = -ENOLINK;
382 }
383 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
384 if (retval) {
385 dev_kfree_skb_any (skb);
386 usb_free_urb (urb);
387 }
388}
389
390
391/*-------------------------------------------------------------------------*/
392
393static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
394{
Joe Perches8e95a202009-12-03 07:58:21 +0000395 if (dev->driver_info->rx_fixup &&
396 !dev->driver_info->rx_fixup (dev, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto error;
398 // else network stack removes extra byte if we forced a short packet
399
400 if (skb->len)
David Brownell2e55cc72005-08-31 09:53:10 -0700401 usbnet_skb_return (dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 else {
403 if (netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000404 netdev_dbg(dev->net, "drop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405error:
Herbert Xu79638372009-06-29 16:53:28 +0000406 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 skb_queue_tail (&dev->done, skb);
408 }
409}
410
411/*-------------------------------------------------------------------------*/
412
David Howells7d12e782006-10-05 14:55:46 +0100413static void rx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct sk_buff *skb = (struct sk_buff *) urb->context;
416 struct skb_data *entry = (struct skb_data *) skb->cb;
417 struct usbnet *dev = entry->dev;
418 int urb_status = urb->status;
419
420 skb_put (skb, urb->actual_length);
421 entry->state = rx_done;
422 entry->urb = NULL;
423
424 switch (urb_status) {
David Brownell18ab4582007-05-25 12:31:32 -0700425 /* success */
426 case 0:
David Brownellf29fc252005-08-31 09:52:31 -0700427 if (skb->len < dev->net->hard_header_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 entry->state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000429 dev->net->stats.rx_errors++;
430 dev->net->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000432 netdev_dbg(dev->net, "rx length %d\n", skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 break;
435
David Brownell18ab4582007-05-25 12:31:32 -0700436 /* stalls need manual reset. this is rare ... except that
437 * when going through USB 2.0 TTs, unplug appears this way.
Jean Delvare3ac49a12009-06-04 16:20:28 +0200438 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
David Brownell18ab4582007-05-25 12:31:32 -0700439 * storm, recovering as needed.
440 */
441 case -EPIPE:
Herbert Xu79638372009-06-29 16:53:28 +0000442 dev->net->stats.rx_errors++;
David Brownell2e55cc72005-08-31 09:53:10 -0700443 usbnet_defer_kevent (dev, EVENT_RX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 // FALLTHROUGH
445
David Brownell18ab4582007-05-25 12:31:32 -0700446 /* software-driven interface shutdown */
447 case -ECONNRESET: /* async unlink */
448 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (netif_msg_ifdown (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000450 netdev_dbg(dev->net, "rx shutdown, code %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto block;
452
David Brownell18ab4582007-05-25 12:31:32 -0700453 /* we get controller i/o faults during khubd disconnect() delays.
454 * throttle down resubmits, to avoid log floods; just temporarily,
455 * so we still recover when the fault isn't a khubd delay.
456 */
457 case -EPROTO:
458 case -ETIME:
459 case -EILSEQ:
Herbert Xu79638372009-06-29 16:53:28 +0000460 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!timer_pending (&dev->delay)) {
462 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
463 if (netif_msg_link (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000464 netdev_dbg(dev->net, "rx throttle %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466block:
467 entry->state = rx_cleanup;
468 entry->urb = urb;
469 urb = NULL;
470 break;
471
David Brownell18ab4582007-05-25 12:31:32 -0700472 /* data overrun ... flush fifo? */
473 case -EOVERFLOW:
Herbert Xu79638372009-06-29 16:53:28 +0000474 dev->net->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 // FALLTHROUGH
David Brownellcb1cebb2007-02-15 18:52:30 -0800476
David Brownell18ab4582007-05-25 12:31:32 -0700477 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 entry->state = rx_cleanup;
Herbert Xu79638372009-06-29 16:53:28 +0000479 dev->net->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000481 netdev_dbg(dev->net, "rx status %d\n", urb_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 }
484
David S. Miller8728b832005-08-09 19:25:21 -0700485 defer_bh(dev, skb, &dev->rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (urb) {
Joe Perches8e95a202009-12-03 07:58:21 +0000488 if (netif_running (dev->net) &&
489 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 rx_submit (dev, urb, GFP_ATOMIC);
491 return;
492 }
493 usb_free_urb (urb);
494 }
495 if (netif_msg_rx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000496 netdev_dbg(dev->net, "no read resubmitted\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
David Howells7d12e782006-10-05 14:55:46 +0100499static void intr_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct usbnet *dev = urb->context;
502 int status = urb->status;
503
504 switch (status) {
David Brownell18ab4582007-05-25 12:31:32 -0700505 /* success */
506 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dev->driver_info->status(dev, urb);
508 break;
509
David Brownell18ab4582007-05-25 12:31:32 -0700510 /* software-driven interface shutdown */
511 case -ENOENT: /* urb killed */
512 case -ESHUTDOWN: /* hardware gone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (netif_msg_ifdown (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000514 netdev_dbg(dev->net, "intr shutdown, code %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
516
David Brownell18ab4582007-05-25 12:31:32 -0700517 /* NOTE: not throttling like RX/TX, since this endpoint
518 * already polls infrequently
519 */
520 default:
Joe Perches60b86752010-02-17 10:30:23 +0000521 netdev_dbg(dev->net, "intr status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 }
524
525 if (!netif_running (dev->net))
526 return;
527
528 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
529 status = usb_submit_urb (urb, GFP_ATOMIC);
530 if (status != 0 && netif_msg_timer (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000531 netdev_err(dev->net, "intr resubmit --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*-------------------------------------------------------------------------*/
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300535void usbnet_pause_rx(struct usbnet *dev)
536{
537 set_bit(EVENT_RX_PAUSED, &dev->flags);
538
539 if (netif_msg_rx_status(dev))
Joe Perches60b86752010-02-17 10:30:23 +0000540 netdev_dbg(dev->net, "paused rx queue enabled\n");
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300541}
542EXPORT_SYMBOL_GPL(usbnet_pause_rx);
543
544void usbnet_resume_rx(struct usbnet *dev)
545{
546 struct sk_buff *skb;
547 int num = 0;
548
549 clear_bit(EVENT_RX_PAUSED, &dev->flags);
550
551 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
552 usbnet_skb_return(dev, skb);
553 num++;
554 }
555
556 tasklet_schedule(&dev->bh);
557
558 if (netif_msg_rx_status(dev))
Joe Perches60b86752010-02-17 10:30:23 +0000559 netdev_dbg(dev->net, "paused rx queue disabled, %d skbs requeued\n",
560 num);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300561}
562EXPORT_SYMBOL_GPL(usbnet_resume_rx);
563
564void usbnet_purge_paused_rxq(struct usbnet *dev)
565{
566 skb_queue_purge(&dev->rxq_pause);
567}
568EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
569
570/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572// unlink pending rx/tx; completion handlers do all other cleanup
573
574static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
575{
576 unsigned long flags;
577 struct sk_buff *skb, *skbnext;
578 int count = 0;
579
580 spin_lock_irqsave (&q->lock, flags);
David S. Miller83bfba52008-09-22 20:18:47 -0700581 skb_queue_walk_safe(q, skb, skbnext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct skb_data *entry;
583 struct urb *urb;
584 int retval;
585
586 entry = (struct skb_data *) skb->cb;
587 urb = entry->urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 // during some PM-driven resume scenarios,
590 // these (async) unlinks complete immediately
591 retval = usb_unlink_urb (urb);
592 if (retval != -EINPROGRESS && retval != 0)
Joe Perches60b86752010-02-17 10:30:23 +0000593 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 else
595 count++;
596 }
597 spin_unlock_irqrestore (&q->lock, flags);
598 return count;
599}
600
Jamie Paintera99c1942006-07-27 14:17:28 -0400601// Flush all pending rx urbs
602// minidrivers may need to do this when the MTU changes
603
604void usbnet_unlink_rx_urbs(struct usbnet *dev)
605{
606 if (netif_running(dev->net)) {
607 (void) unlink_urbs (dev, &dev->rxq);
608 tasklet_schedule(&dev->bh);
609 }
610}
611EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/*-------------------------------------------------------------------------*/
614
615// precondition: never called in_interrupt
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800616static void usbnet_terminate_urbs(struct usbnet *dev)
617{
618 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
619 DECLARE_WAITQUEUE(wait, current);
620 int temp;
621
622 /* ensure there are no more active urbs */
623 add_wait_queue(&unlink_wakeup, &wait);
624 set_current_state(TASK_UNINTERRUPTIBLE);
625 dev->wait = &unlink_wakeup;
626 temp = unlink_urbs(dev, &dev->txq) +
627 unlink_urbs(dev, &dev->rxq);
628
629 /* maybe wait for deletions to finish. */
630 while (!skb_queue_empty(&dev->rxq)
631 && !skb_queue_empty(&dev->txq)
632 && !skb_queue_empty(&dev->done)) {
633 schedule_timeout(UNLINK_TIMEOUT_MS);
634 set_current_state(TASK_UNINTERRUPTIBLE);
635 if (netif_msg_ifdown(dev))
Joe Perches60b86752010-02-17 10:30:23 +0000636 netdev_dbg(dev->net, "waited for %d urb completions\n",
637 temp);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800638 }
639 set_current_state(TASK_RUNNING);
640 dev->wait = NULL;
641 remove_wait_queue(&unlink_wakeup, &wait);
642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Stephen Hemminger777baa42009-03-20 19:35:54 +0000644int usbnet_stop (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct usbnet *dev = netdev_priv(net);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300647 struct driver_info *info = dev->driver_info;
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300648 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 netif_stop_queue (net);
651
652 if (netif_msg_ifdown (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000653 netdev_info(dev->net, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
654 net->stats.rx_packets, net->stats.tx_packets,
655 net->stats.rx_errors, net->stats.tx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300657 /* allow minidriver to stop correctly (wireless devices to turn off
658 * radio etc) */
659 if (info->stop) {
660 retval = info->stop(dev);
661 if (retval < 0 && netif_msg_ifdown(dev))
Joe Perches60b86752010-02-17 10:30:23 +0000662 netdev_info(dev->net,
663 "stop fail (%d) usbnet usb-%s-%s, %s\n",
664 retval,
665 dev->udev->bus->bus_name, dev->udev->devpath,
666 info->description);
Jussi Kivilinnaa33e9e72009-06-16 17:17:27 +0300667 }
668
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800669 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
670 usbnet_terminate_urbs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 usb_kill_urb(dev->interrupt);
673
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +0300674 usbnet_purge_paused_rxq(dev);
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* deferred work (task, timer, softirq) must also stop.
677 * can't flush_scheduled_work() until we drop rtnl (later),
678 * else workers could deadlock; so make workers a NOP.
679 */
680 dev->flags = 0;
681 del_timer_sync (&dev->delay);
682 tasklet_kill (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800683 if (info->manage_power)
684 info->manage_power(dev, 0);
685 else
686 usb_autopm_put_interface(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 return 0;
689}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000690EXPORT_SYMBOL_GPL(usbnet_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692/*-------------------------------------------------------------------------*/
693
694// posts reads, and enables write queuing
695
696// precondition: never called in_interrupt
697
Stephen Hemminger777baa42009-03-20 19:35:54 +0000698int usbnet_open (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct usbnet *dev = netdev_priv(net);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200701 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct driver_info *info = dev->driver_info;
703
Oliver Neukuma11a6542007-08-03 13:52:19 +0200704 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
705 if (netif_msg_ifup (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000706 netdev_info(dev->net,
707 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
708 retval,
709 dev->udev->bus->bus_name,
710 dev->udev->devpath,
711 info->description);
Oliver Neukuma11a6542007-08-03 13:52:19 +0200712 goto done_nopm;
713 }
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 // put into "known safe" state
716 if (info->reset && (retval = info->reset (dev)) < 0) {
717 if (netif_msg_ifup (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000718 netdev_info(dev->net,
719 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
720 retval,
721 dev->udev->bus->bus_name,
722 dev->udev->devpath,
723 info->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto done;
725 }
726
727 // insist peer be connected
728 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
729 if (netif_msg_ifup (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000730 netdev_dbg(dev->net, "can't open; %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto done;
732 }
733
734 /* start any status interrupt transfer */
735 if (dev->interrupt) {
736 retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
737 if (retval < 0) {
738 if (netif_msg_ifup (dev))
Joe Perches60b86752010-02-17 10:30:23 +0000739 netdev_err(dev->net, "intr submit %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto done;
741 }
742 }
743
744 netif_start_queue (net);
745 if (netif_msg_ifup (dev)) {
746 char *framing;
747
748 if (dev->driver_info->flags & FLAG_FRAMING_NC)
749 framing = "NetChip";
750 else if (dev->driver_info->flags & FLAG_FRAMING_GL)
751 framing = "GeneSys";
752 else if (dev->driver_info->flags & FLAG_FRAMING_Z)
753 framing = "Zaurus";
754 else if (dev->driver_info->flags & FLAG_FRAMING_RN)
755 framing = "RNDIS";
756 else if (dev->driver_info->flags & FLAG_FRAMING_AX)
757 framing = "ASIX";
758 else
759 framing = "simple";
760
Joe Perches60b86752010-02-17 10:30:23 +0000761 netdev_info(dev->net, "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
762 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
763 dev->net->mtu, framing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 // delay posting reads until we're fully open
767 tasklet_schedule (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800768 if (info->manage_power) {
769 retval = info->manage_power(dev, 1);
770 if (retval < 0)
771 goto done;
772 usb_autopm_put_interface(dev->intf);
773 }
Oliver Neukuma11a6542007-08-03 13:52:19 +0200774 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775done:
Oliver Neukuma11a6542007-08-03 13:52:19 +0200776 usb_autopm_put_interface(dev->intf);
777done_nopm:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return retval;
779}
Stephen Hemminger777baa42009-03-20 19:35:54 +0000780EXPORT_SYMBOL_GPL(usbnet_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/*-------------------------------------------------------------------------*/
783
David Brownell2e55cc72005-08-31 09:53:10 -0700784/* ethtool methods; minidrivers may need to add some more, but
785 * they'll probably want to use this base set.
786 */
787
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200788int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
789{
790 struct usbnet *dev = netdev_priv(net);
791
792 if (!dev->mii.mdio_read)
793 return -EOPNOTSUPP;
794
795 return mii_ethtool_gset(&dev->mii, cmd);
796}
797EXPORT_SYMBOL_GPL(usbnet_get_settings);
798
799int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
800{
801 struct usbnet *dev = netdev_priv(net);
802 int retval;
803
804 if (!dev->mii.mdio_write)
805 return -EOPNOTSUPP;
806
807 retval = mii_ethtool_sset(&dev->mii, cmd);
808
809 /* link speed/duplex might have changed */
810 if (dev->driver_info->link_reset)
811 dev->driver_info->link_reset(dev);
812
813 return retval;
814
815}
816EXPORT_SYMBOL_GPL(usbnet_set_settings);
817
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200818u32 usbnet_get_link (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 struct usbnet *dev = netdev_priv(net);
821
David Brownellf29fc252005-08-31 09:52:31 -0700822 /* If a check_connect is defined, return its result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (dev->driver_info->check_connect)
824 return dev->driver_info->check_connect (dev) == 0;
825
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200826 /* if the device has mii operations, use those */
827 if (dev->mii.mdio_read)
828 return mii_link_ok(&dev->mii);
829
Bjørn Mork05ffb3e2009-03-01 20:45:40 -0800830 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
831 return ethtool_op_get_link(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200833EXPORT_SYMBOL_GPL(usbnet_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
David Brownell18ee91fa2006-11-02 12:29:12 -0800835int usbnet_nway_reset(struct net_device *net)
836{
837 struct usbnet *dev = netdev_priv(net);
838
839 if (!dev->mii.mdio_write)
840 return -EOPNOTSUPP;
841
842 return mii_nway_restart(&dev->mii);
843}
844EXPORT_SYMBOL_GPL(usbnet_nway_reset);
845
David Brownell18ee91fa2006-11-02 12:29:12 -0800846void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
847{
848 struct usbnet *dev = netdev_priv(net);
849
David Brownell296c0242007-04-17 16:10:10 -0700850 strncpy (info->driver, dev->driver_name, sizeof info->driver);
David Brownell18ee91fa2006-11-02 12:29:12 -0800851 strncpy (info->version, DRIVER_VERSION, sizeof info->version);
852 strncpy (info->fw_version, dev->driver_info->description,
853 sizeof info->fw_version);
854 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
855}
856EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
857
David Brownell2e55cc72005-08-31 09:53:10 -0700858u32 usbnet_get_msglevel (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct usbnet *dev = netdev_priv(net);
861
862 return dev->msg_enable;
863}
David Brownell2e55cc72005-08-31 09:53:10 -0700864EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
David Brownell2e55cc72005-08-31 09:53:10 -0700866void usbnet_set_msglevel (struct net_device *net, u32 level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct usbnet *dev = netdev_priv(net);
869
870 dev->msg_enable = level;
871}
David Brownell2e55cc72005-08-31 09:53:10 -0700872EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
David Brownell2e55cc72005-08-31 09:53:10 -0700874/* drivers may override default ethtool_ops in their bind() routine */
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700875static const struct ethtool_ops usbnet_ethtool_ops = {
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200876 .get_settings = usbnet_get_settings,
877 .set_settings = usbnet_set_settings,
David Brownell2e55cc72005-08-31 09:53:10 -0700878 .get_link = usbnet_get_link,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200879 .nway_reset = usbnet_nway_reset,
David Brownell18ee91fa2006-11-02 12:29:12 -0800880 .get_drvinfo = usbnet_get_drvinfo,
David Brownell2e55cc72005-08-31 09:53:10 -0700881 .get_msglevel = usbnet_get_msglevel,
882 .set_msglevel = usbnet_set_msglevel,
883};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885/*-------------------------------------------------------------------------*/
886
887/* work that cannot be done in interrupt context uses keventd.
888 *
889 * NOTE: with 2.5 we could do more of this using completion callbacks,
890 * especially now that control transfers can be queued.
891 */
892static void
David Howellsc4028952006-11-22 14:57:56 +0000893kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
David Howellsc4028952006-11-22 14:57:56 +0000895 struct usbnet *dev =
896 container_of(work, struct usbnet, kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 int status;
898
899 /* usb_clear_halt() needs a thread context */
900 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
901 unlink_urbs (dev, &dev->txq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800902 status = usb_autopm_get_interface(dev->intf);
903 if (status < 0)
904 goto fail_pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 status = usb_clear_halt (dev->udev, dev->out);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800906 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +0000907 if (status < 0 &&
908 status != -EPIPE &&
909 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (netif_msg_tx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800911fail_pipe:
Joe Perches60b86752010-02-17 10:30:23 +0000912 netdev_err(dev->net, "can't clear tx halt, status %d\n",
913 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 } else {
915 clear_bit (EVENT_TX_HALT, &dev->flags);
David Brownellf29fc252005-08-31 09:52:31 -0700916 if (status != -ESHUTDOWN)
917 netif_wake_queue (dev->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 }
920 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
921 unlink_urbs (dev, &dev->rxq);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800922 status = usb_autopm_get_interface(dev->intf);
923 if (status < 0)
924 goto fail_halt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 status = usb_clear_halt (dev->udev, dev->in);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800926 usb_autopm_put_interface(dev->intf);
Joe Perches8e95a202009-12-03 07:58:21 +0000927 if (status < 0 &&
928 status != -EPIPE &&
929 status != -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (netif_msg_rx_err (dev))
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800931fail_halt:
Joe Perches60b86752010-02-17 10:30:23 +0000932 netdev_err(dev->net, "can't clear rx halt, status %d\n",
933 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 } else {
935 clear_bit (EVENT_RX_HALT, &dev->flags);
936 tasklet_schedule (&dev->bh);
937 }
938 }
939
940 /* tasklet could resubmit itself forever if memory is tight */
941 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
942 struct urb *urb = NULL;
943
944 if (netif_running (dev->net))
945 urb = usb_alloc_urb (0, GFP_KERNEL);
946 else
947 clear_bit (EVENT_RX_MEMORY, &dev->flags);
948 if (urb != NULL) {
949 clear_bit (EVENT_RX_MEMORY, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800950 status = usb_autopm_get_interface(dev->intf);
951 if (status < 0)
952 goto fail_lowmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 rx_submit (dev, urb, GFP_KERNEL);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800954 usb_autopm_put_interface(dev->intf);
955fail_lowmem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 tasklet_schedule (&dev->bh);
957 }
958 }
959
David Hollis7ea13c92005-04-22 15:07:02 -0700960 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
David Brownellcb1cebb2007-02-15 18:52:30 -0800961 struct driver_info *info = dev->driver_info;
David Hollis7ea13c92005-04-22 15:07:02 -0700962 int retval = 0;
963
964 clear_bit (EVENT_LINK_RESET, &dev->flags);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800965 status = usb_autopm_get_interface(dev->intf);
966 if (status < 0)
967 goto skip_reset;
David Hollis7ea13c92005-04-22 15:07:02 -0700968 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800969 usb_autopm_put_interface(dev->intf);
970skip_reset:
Joe Perches60b86752010-02-17 10:30:23 +0000971 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
972 retval,
973 dev->udev->bus->bus_name,
974 dev->udev->devpath,
975 info->description);
Oliver Neukum69ee472f2009-12-03 15:31:18 -0800976 } else {
977 usb_autopm_put_interface(dev->intf);
David Hollis7ea13c92005-04-22 15:07:02 -0700978 }
979 }
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (dev->flags)
Joe Perches60b86752010-02-17 10:30:23 +0000982 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985/*-------------------------------------------------------------------------*/
986
David Howells7d12e782006-10-05 14:55:46 +0100987static void tx_complete (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct sk_buff *skb = (struct sk_buff *) urb->context;
990 struct skb_data *entry = (struct skb_data *) skb->cb;
991 struct usbnet *dev = entry->dev;
992
993 if (urb->status == 0) {
Herbert Xu79638372009-06-29 16:53:28 +0000994 dev->net->stats.tx_packets++;
995 dev->net->stats.tx_bytes += entry->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 } else {
Herbert Xu79638372009-06-29 16:53:28 +0000997 dev->net->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 switch (urb->status) {
1000 case -EPIPE:
David Brownell2e55cc72005-08-31 09:53:10 -07001001 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003
1004 /* software-driven interface shutdown */
1005 case -ECONNRESET: // async unlink
1006 case -ESHUTDOWN: // hardware gone
1007 break;
1008
1009 // like rx, tx gets controller i/o faults during khubd delays
1010 // and so it uses the same throttling mechanism.
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -07001011 case -EPROTO:
1012 case -ETIME:
1013 case -EILSEQ:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001014 usb_mark_last_busy(dev->udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!timer_pending (&dev->delay)) {
1016 mod_timer (&dev->delay,
1017 jiffies + THROTTLE_JIFFIES);
1018 if (netif_msg_link (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001019 netdev_dbg(dev->net, "tx throttle %d\n",
1020 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022 netif_stop_queue (dev->net);
1023 break;
1024 default:
1025 if (netif_msg_tx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001026 netdev_dbg(dev->net, "tx err %d\n",
1027 entry->urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
1029 }
1030 }
1031
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001032 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 urb->dev = NULL;
1034 entry->state = tx_done;
David S. Miller8728b832005-08-09 19:25:21 -07001035 defer_bh(dev, skb, &dev->txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*-------------------------------------------------------------------------*/
1039
Stephen Hemminger777baa42009-03-20 19:35:54 +00001040void usbnet_tx_timeout (struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 struct usbnet *dev = netdev_priv(net);
1043
1044 unlink_urbs (dev, &dev->txq);
1045 tasklet_schedule (&dev->bh);
1046
1047 // FIXME: device recovery -- reset?
1048}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001049EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051/*-------------------------------------------------------------------------*/
1052
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001053netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1054 struct net_device *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct usbnet *dev = netdev_priv(net);
1057 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct urb *urb = NULL;
1059 struct skb_data *entry;
1060 struct driver_info *info = dev->driver_info;
1061 unsigned long flags;
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001062 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 // some devices want funky USB-level framing, for
1065 // win32 driver (usually) and/or hardware quirks
1066 if (info->tx_fixup) {
1067 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1068 if (!skb) {
1069 if (netif_msg_tx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001070 netdev_dbg(dev->net, "can't tx_fixup skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto drop;
1072 }
1073 }
1074 length = skb->len;
1075
1076 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
1077 if (netif_msg_tx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001078 netdev_dbg(dev->net, "no urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 goto drop;
1080 }
1081
1082 entry = (struct skb_data *) skb->cb;
1083 entry->urb = urb;
1084 entry->dev = dev;
1085 entry->state = tx_start;
1086 entry->length = length;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1089 skb->data, skb->len, tx_complete, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* don't assume the hardware handles USB_ZERO_PACKET
1092 * NOTE: strictly conforming cdc-ether devices should expect
1093 * the ZLP here, but ignore the one-byte packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 */
Steve Glendinningec475622009-09-22 04:00:27 +00001095 if (!(info->flags & FLAG_SEND_ZLP) && (length % dev->maxpacket) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 urb->transfer_buffer_length++;
Peter Korsgaard3e323f32007-06-27 08:48:15 +02001097 if (skb_tailroom(skb)) {
1098 skb->data[skb->len] = 0;
1099 __skb_put(skb, 1);
1100 }
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001103 spin_lock_irqsave(&dev->txq.lock, flags);
1104 retval = usb_autopm_get_interface_async(dev->intf);
1105 if (retval < 0) {
1106 spin_unlock_irqrestore(&dev->txq.lock, flags);
1107 goto drop;
1108 }
1109
1110#ifdef CONFIG_PM
1111 /* if this triggers the device is still a sleep */
1112 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1113 /* transmission will be done in resume */
1114 usb_anchor_urb(urb, &dev->deferred);
1115 /* no use to process more packets */
1116 netif_stop_queue(net);
1117 spin_unlock_irqrestore(&dev->txq.lock, flags);
Joe Perches60b86752010-02-17 10:30:23 +00001118 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001119 goto deferred;
1120 }
1121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1124 case -EPIPE:
1125 netif_stop_queue (net);
David Brownell2e55cc72005-08-31 09:53:10 -07001126 usbnet_defer_kevent (dev, EVENT_TX_HALT);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001127 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129 default:
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001130 usb_autopm_put_interface_async(dev->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (netif_msg_tx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001132 netdev_dbg(dev->net, "tx: submit urb err %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
1134 case 0:
1135 net->trans_start = jiffies;
1136 __skb_queue_tail (&dev->txq, skb);
1137 if (dev->txq.qlen >= TX_QLEN (dev))
1138 netif_stop_queue (net);
1139 }
1140 spin_unlock_irqrestore (&dev->txq.lock, flags);
1141
1142 if (retval) {
1143 if (netif_msg_tx_err (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001144 netdev_dbg(dev->net, "drop, code %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145drop:
Herbert Xu79638372009-06-29 16:53:28 +00001146 dev->net->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (skb)
1148 dev_kfree_skb_any (skb);
1149 usb_free_urb (urb);
1150 } else if (netif_msg_tx_queued (dev)) {
Joe Perches60b86752010-02-17 10:30:23 +00001151 netdev_dbg(dev->net, "> tx, len %d, type 0x%x\n",
1152 length, skb->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001154#ifdef CONFIG_PM
1155deferred:
1156#endif
Stephen Hemminger25a79c42009-08-31 19:50:45 +00001157 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
Stephen Hemminger777baa42009-03-20 19:35:54 +00001159EXPORT_SYMBOL_GPL(usbnet_start_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161/*-------------------------------------------------------------------------*/
1162
1163// tasklet (work deferred from completions, in_irq) or timer
1164
1165static void usbnet_bh (unsigned long param)
1166{
1167 struct usbnet *dev = (struct usbnet *) param;
1168 struct sk_buff *skb;
1169 struct skb_data *entry;
1170
1171 while ((skb = skb_dequeue (&dev->done))) {
1172 entry = (struct skb_data *) skb->cb;
1173 switch (entry->state) {
David Brownell18ab4582007-05-25 12:31:32 -07001174 case rx_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 entry->state = rx_cleanup;
1176 rx_process (dev, skb);
1177 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001178 case tx_done:
1179 case rx_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 usb_free_urb (entry->urb);
1181 dev_kfree_skb (skb);
1182 continue;
David Brownell18ab4582007-05-25 12:31:32 -07001183 default:
Joe Perches60b86752010-02-17 10:30:23 +00001184 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 }
1187
1188 // waiting for all pending urbs to complete?
1189 if (dev->wait) {
1190 if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
1191 wake_up (dev->wait);
1192 }
1193
1194 // or are we maybe short a few urbs?
Joe Perches8e95a202009-12-03 07:58:21 +00001195 } else if (netif_running (dev->net) &&
1196 netif_device_present (dev->net) &&
1197 !timer_pending (&dev->delay) &&
1198 !test_bit (EVENT_RX_HALT, &dev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int temp = dev->rxq.qlen;
1200 int qlen = RX_QLEN (dev);
1201
1202 if (temp < qlen) {
1203 struct urb *urb;
1204 int i;
1205
1206 // don't refill the queue all at once
1207 for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
1208 urb = usb_alloc_urb (0, GFP_ATOMIC);
1209 if (urb != NULL)
1210 rx_submit (dev, urb, GFP_ATOMIC);
1211 }
1212 if (temp != dev->rxq.qlen && netif_msg_link (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001213 netdev_dbg(dev->net, "rxqlen %d --> %d\n",
1214 temp, dev->rxq.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (dev->rxq.qlen < qlen)
1216 tasklet_schedule (&dev->bh);
1217 }
1218 if (dev->txq.qlen < TX_QLEN (dev))
1219 netif_wake_queue (dev->net);
1220 }
1221}
1222
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/*-------------------------------------------------------------------------
1225 *
1226 * USB Device Driver support
1227 *
1228 *-------------------------------------------------------------------------*/
David Brownellcb1cebb2007-02-15 18:52:30 -08001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230// precondition: never called in_interrupt
1231
David Brownell38bde1d2005-08-31 09:52:45 -07001232void usbnet_disconnect (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct usbnet *dev;
1235 struct usb_device *xdev;
1236 struct net_device *net;
1237
1238 dev = usb_get_intfdata(intf);
1239 usb_set_intfdata(intf, NULL);
1240 if (!dev)
1241 return;
1242
1243 xdev = interface_to_usbdev (intf);
1244
1245 if (netif_msg_probe (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001246 netdev_info(dev->net, "unregister '%s' usb-%s-%s, %s\n",
1247 intf->dev.driver->name,
1248 xdev->bus->bus_name, xdev->devpath,
1249 dev->driver_info->description);
David Brownellcb1cebb2007-02-15 18:52:30 -08001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 net = dev->net;
1252 unregister_netdev (net);
1253
1254 /* we don't hold rtnl here ... */
1255 flush_scheduled_work ();
1256
1257 if (dev->driver_info->unbind)
1258 dev->driver_info->unbind (dev, intf);
1259
1260 free_netdev(net);
1261 usb_put_dev (xdev);
1262}
David Brownell38bde1d2005-08-31 09:52:45 -07001263EXPORT_SYMBOL_GPL(usbnet_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Stephen Hemminger777baa42009-03-20 19:35:54 +00001265static const struct net_device_ops usbnet_netdev_ops = {
1266 .ndo_open = usbnet_open,
1267 .ndo_stop = usbnet_stop,
1268 .ndo_start_xmit = usbnet_start_xmit,
1269 .ndo_tx_timeout = usbnet_tx_timeout,
1270 .ndo_change_mtu = usbnet_change_mtu,
1271 .ndo_set_mac_address = eth_mac_addr,
1272 .ndo_validate_addr = eth_validate_addr,
1273};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275/*-------------------------------------------------------------------------*/
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277// precondition: never called in_interrupt
1278
Marcel Holtmann225794f2009-10-02 05:15:26 +00001279static struct device_type wlan_type = {
1280 .name = "wlan",
1281};
1282
1283static struct device_type wwan_type = {
1284 .name = "wwan",
1285};
1286
David Brownell38bde1d2005-08-31 09:52:45 -07001287int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1289{
1290 struct usbnet *dev;
David Brownellcb1cebb2007-02-15 18:52:30 -08001291 struct net_device *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct usb_host_interface *interface;
1293 struct driver_info *info;
1294 struct usb_device *xdev;
1295 int status;
David Brownell296c0242007-04-17 16:10:10 -07001296 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
David Brownell296c0242007-04-17 16:10:10 -07001298 name = udev->dev.driver->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 info = (struct driver_info *) prod->driver_info;
1300 if (!info) {
David Brownell296c0242007-04-17 16:10:10 -07001301 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -ENODEV;
1303 }
1304 xdev = interface_to_usbdev (udev);
1305 interface = udev->cur_altsetting;
1306
1307 usb_get_dev (xdev);
1308
1309 status = -ENOMEM;
1310
1311 // set up our own records
1312 net = alloc_etherdev(sizeof(*dev));
1313 if (!net) {
1314 dbg ("can't kmalloc dev");
1315 goto out;
1316 }
1317
1318 dev = netdev_priv(net);
1319 dev->udev = xdev;
Oliver Neukuma11a6542007-08-03 13:52:19 +02001320 dev->intf = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 dev->driver_info = info;
David Brownell296c0242007-04-17 16:10:10 -07001322 dev->driver_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1324 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1325 skb_queue_head_init (&dev->rxq);
1326 skb_queue_head_init (&dev->txq);
1327 skb_queue_head_init (&dev->done);
Jussi Kivilinna7834ddb2009-08-11 22:57:16 +03001328 skb_queue_head_init(&dev->rxq_pause);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 dev->bh.func = usbnet_bh;
1330 dev->bh.data = (unsigned long) dev;
David Howellsc4028952006-11-22 14:57:56 +00001331 INIT_WORK (&dev->kevent, kevent);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001332 init_usb_anchor(&dev->deferred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 dev->delay.function = usbnet_bh;
1334 dev->delay.data = (unsigned long) dev;
1335 init_timer (&dev->delay);
Arnd Bergmanna9fc6332006-10-09 00:08:02 +02001336 mutex_init (&dev->phy_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 dev->net = net;
1339 strcpy (net->name, "usb%d");
1340 memcpy (net->dev_addr, node_id, sizeof node_id);
1341
David Brownell2e55cc72005-08-31 09:53:10 -07001342 /* rx and tx sides can use different message sizes;
1343 * bind() should set rx_urb_size in that case.
1344 */
1345 dev->hard_mtu = net->mtu + net->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346#if 0
1347// dma_supported() is deeply broken on almost all architectures
1348 // possible with some EHCI controllers
Yang Hongyang6a355282009-04-06 19:01:13 -07001349 if (dma_supported (&udev->dev, DMA_BIT_MASK(64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 net->features |= NETIF_F_HIGHDMA;
1351#endif
1352
Stephen Hemminger777baa42009-03-20 19:35:54 +00001353 net->netdev_ops = &usbnet_netdev_ops;
Stephen Hemminger777baa42009-03-20 19:35:54 +00001354 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 net->ethtool_ops = &usbnet_ethtool_ops;
1356
1357 // allow device-specific bind/init procedures
1358 // NOTE net->name still not usable ...
1359 if (info->bind) {
1360 status = info->bind (dev, udev);
David Brownellcb1cebb2007-02-15 18:52:30 -08001361 if (status < 0)
1362 goto out1;
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 // heuristic: "usb%d" for links we know are two-host,
1365 // else "eth%d" when there's reasonable doubt. userspace
1366 // can rename the link if it knows better.
Joe Perches8e95a202009-12-03 07:58:21 +00001367 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
1368 (net->dev_addr [0] & 0x02) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 strcpy (net->name, "eth%d");
Jussi Kivilinna6e3bbcc2008-01-26 00:51:06 +02001370 /* WLAN devices should always be named "wlan%d" */
1371 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1372 strcpy(net->name, "wlan%d");
Marcel Holtmanne1e499e2009-10-02 05:15:25 +00001373 /* WWAN devices should always be named "wwan%d" */
1374 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1375 strcpy(net->name, "wwan%d");
David Brownellf29fc252005-08-31 09:52:31 -07001376
1377 /* maybe the remote can't receive an Ethernet MTU */
1378 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1379 net->mtu = dev->hard_mtu - net->hard_header_len;
David Brownell2e55cc72005-08-31 09:53:10 -07001380 } else if (!info->in || !info->out)
1381 status = usbnet_get_endpoints (dev, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 else {
1383 dev->in = usb_rcvbulkpipe (xdev, info->in);
1384 dev->out = usb_sndbulkpipe (xdev, info->out);
1385 if (!(info->flags & FLAG_NO_SETINT))
1386 status = usb_set_interface (xdev,
1387 interface->desc.bInterfaceNumber,
1388 interface->desc.bAlternateSetting);
1389 else
1390 status = 0;
1391
1392 }
Peter Korsgaard9514bfe2007-07-03 00:46:42 +02001393 if (status >= 0 && dev->status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 status = init_status (dev, udev);
1395 if (status < 0)
David Brownellcb1cebb2007-02-15 18:52:30 -08001396 goto out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
David Brownell2e55cc72005-08-31 09:53:10 -07001398 if (!dev->rx_urb_size)
1399 dev->rx_urb_size = dev->hard_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
David Brownellcb1cebb2007-02-15 18:52:30 -08001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 SET_NETDEV_DEV(net, &udev->dev);
Marcel Holtmann225794f2009-10-02 05:15:26 +00001403
1404 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1405 SET_NETDEV_DEVTYPE(net, &wlan_type);
1406 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1407 SET_NETDEV_DEVTYPE(net, &wwan_type);
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 status = register_netdev (net);
1410 if (status)
1411 goto out3;
1412 if (netif_msg_probe (dev))
Joe Perches60b86752010-02-17 10:30:23 +00001413 netdev_info(dev->net, "register '%s' at usb-%s-%s, %s, %pM\n",
1414 udev->dev.driver->name,
1415 xdev->bus->bus_name, xdev->devpath,
1416 dev->driver_info->description,
1417 net->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 // ok, it's ready to go.
1420 usb_set_intfdata (udev, dev);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 netif_device_attach (net);
1423
Ben Hutchings37e82732009-11-04 15:29:52 +00001424 if (dev->driver_info->flags & FLAG_LINK_INTR)
1425 netif_carrier_off(net);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return 0;
1428
1429out3:
1430 if (info->unbind)
1431 info->unbind (dev, udev);
1432out1:
1433 free_netdev(net);
1434out:
1435 usb_put_dev(xdev);
1436 return status;
1437}
David Brownell38bde1d2005-08-31 09:52:45 -07001438EXPORT_SYMBOL_GPL(usbnet_probe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440/*-------------------------------------------------------------------------*/
1441
Oliver Neukum36433122007-04-30 01:37:44 -07001442/*
1443 * suspend the whole driver as soon as the first interface is suspended
1444 * resume only when the last interface is resumed
David Brownell38bde1d2005-08-31 09:52:45 -07001445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
David Brownell38bde1d2005-08-31 09:52:45 -07001447int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct usbnet *dev = usb_get_intfdata(intf);
David Brownellcb1cebb2007-02-15 18:52:30 -08001450
Oliver Neukum36433122007-04-30 01:37:44 -07001451 if (!dev->suspend_count++) {
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001452 spin_lock_irq(&dev->txq.lock);
1453 /* don't autosuspend while transmitting */
1454 if (dev->txq.qlen && (message.event & PM_EVENT_AUTO)) {
1455 spin_unlock_irq(&dev->txq.lock);
1456 return -EBUSY;
1457 } else {
1458 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1459 spin_unlock_irq(&dev->txq.lock);
1460 }
Oliver Neukuma11a6542007-08-03 13:52:19 +02001461 /*
1462 * accelerate emptying of the rx and queues, to avoid
Oliver Neukum36433122007-04-30 01:37:44 -07001463 * having everything error out.
1464 */
1465 netif_device_detach (dev->net);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001466 usbnet_terminate_urbs(dev);
1467 usb_kill_urb(dev->interrupt);
1468
Oliver Neukuma11a6542007-08-03 13:52:19 +02001469 /*
1470 * reattach so runtime management can use and
1471 * wake the device
1472 */
1473 netif_device_attach (dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return 0;
1476}
David Brownell38bde1d2005-08-31 09:52:45 -07001477EXPORT_SYMBOL_GPL(usbnet_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
David Brownell38bde1d2005-08-31 09:52:45 -07001479int usbnet_resume (struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct usbnet *dev = usb_get_intfdata(intf);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001482 struct sk_buff *skb;
1483 struct urb *res;
1484 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001486 if (!--dev->suspend_count) {
1487 spin_lock_irq(&dev->txq.lock);
1488 while ((res = usb_get_from_anchor(&dev->deferred))) {
1489
1490 printk(KERN_INFO"%s has delayed data\n", __func__);
1491 skb = (struct sk_buff *)res->context;
1492 retval = usb_submit_urb(res, GFP_ATOMIC);
1493 if (retval < 0) {
1494 dev_kfree_skb_any(skb);
1495 usb_free_urb(res);
1496 usb_autopm_put_interface_async(dev->intf);
1497 } else {
1498 dev->net->trans_start = jiffies;
1499 __skb_queue_tail(&dev->txq, skb);
1500 }
1501 }
1502
1503 smp_mb();
1504 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1505 spin_unlock_irq(&dev->txq.lock);
1506 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1507 netif_start_queue(dev->net);
Oliver Neukum36433122007-04-30 01:37:44 -07001508 tasklet_schedule (&dev->bh);
Oliver Neukum69ee472f2009-12-03 15:31:18 -08001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 return 0;
1511}
David Brownell38bde1d2005-08-31 09:52:45 -07001512EXPORT_SYMBOL_GPL(usbnet_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515/*-------------------------------------------------------------------------*/
1516
David Brownellf29fc252005-08-31 09:52:31 -07001517static int __init usbnet_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
David Brownell090ffa92005-08-31 09:54:50 -07001519 /* compiler should optimize this out */
Alexey Dobriyanf8ac2322006-10-08 16:02:00 +04001520 BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 < sizeof (struct skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 random_ether_addr(node_id);
David Brownellcb1cebb2007-02-15 18:52:30 -08001524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
David Brownellf29fc252005-08-31 09:52:31 -07001526module_init(usbnet_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
David Brownellf29fc252005-08-31 09:52:31 -07001528static void __exit usbnet_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
David Brownellf29fc252005-08-31 09:52:31 -07001531module_exit(usbnet_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
David Brownellf29fc252005-08-31 09:52:31 -07001533MODULE_AUTHOR("David Brownell");
David Brownell090ffa92005-08-31 09:54:50 -07001534MODULE_DESCRIPTION("USB network driver framework");
David Brownellf29fc252005-08-31 09:52:31 -07001535MODULE_LICENSE("GPL");