net: qmi_wwan: remove 1199:9070 device id
[deliverable/linux.git] / drivers / net / usb / usbnet.c
CommitLineData
1da177e4 1/*
090ffa9d 2 * USB Network driver infrastructure
f29fc259 3 * Copyright (C) 2000-2005 by David Brownell
1da177e4 4 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
1da177e4
LT
5 *
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
9cb00073 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1da177e4
LT
18 */
19
20/*
21 * This is a generic "USB networking" framework that works with several
090ffa9d
DB
22 * kinds of full and high speed networking devices: host-to-host cables,
23 * smart usb peripherals, and actual Ethernet adapters.
1da177e4 24 *
090ffa9d
DB
25 * These devices usually differ in terms of control protocols (if they
26 * even have one!) and sometimes they define new framing to wrap or batch
27 * Ethernet packets. Otherwise, they talk to USB pretty much the same,
28 * so interface (un)binding, endpoint I/O queues, fault handling, and other
29 * issues can usefully be addressed by this framework.
30 */
1da177e4
LT
31
32// #define DEBUG // error path messages, extra info
33// #define VERBOSE // more; success messages
34
1da177e4 35#include <linux/module.h>
1da177e4
LT
36#include <linux/init.h>
37#include <linux/netdevice.h>
38#include <linux/etherdevice.h>
03ad032b 39#include <linux/ctype.h>
1da177e4
LT
40#include <linux/ethtool.h>
41#include <linux/workqueue.h>
42#include <linux/mii.h>
1da177e4 43#include <linux/usb.h>
3692e94f 44#include <linux/usb/usbnet.h>
c40a2c88 45#include <linux/usb/cdc.h>
5a0e3ad6 46#include <linux/slab.h>
fd1f170d 47#include <linux/kernel.h>
b0786b43 48#include <linux/pm_runtime.h>
f29fc259
DB
49
50#define DRIVER_VERSION "22-Aug-2005"
1da177e4
LT
51
52
53/*-------------------------------------------------------------------------*/
54
55/*
56 * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
57 * Several dozen bytes of IPv4 data can fit in two such transactions.
58 * One maximum size Ethernet packet takes twenty four of them.
59 * For high speed, each frame comfortably fits almost 36 max size
60 * Ethernet packets (so queues should be bigger).
f29fc259 61 *
a88c32ae
ML
62 * The goal is to let the USB host controller be busy for 5msec or
63 * more before an irq is required, under load. Jumbograms change
64 * the equation.
1da177e4 65 */
a88c32ae
ML
66#define MAX_QUEUE_MEMORY (60 * 1518)
67#define RX_QLEN(dev) ((dev)->rx_qlen)
68#define TX_QLEN(dev) ((dev)->tx_qlen)
1da177e4 69
1da177e4
LT
70// reawaken network queue this soon after stopping; else watchdog barks
71#define TX_TIMEOUT_JIFFIES (5*HZ)
72
37ebb549
PM
73/* throttle rx/tx briefly after some faults, so hub_wq might disconnect()
74 * us (it polls at HZ/4 usually) before we report too many false errors.
75 */
1da177e4
LT
76#define THROTTLE_JIFFIES (HZ/8)
77
1da177e4
LT
78// between wakeups
79#define UNLINK_TIMEOUT_MS 3
80
81/*-------------------------------------------------------------------------*/
82
83// randomly generated ethernet address
84static u8 node_id [ETH_ALEN];
85
1da177e4
LT
86static const char driver_name [] = "usbnet";
87
88/* use ethtool to change the level for any given device */
89static int msg_level = -1;
90module_param (msg_level, int, 0);
91MODULE_PARM_DESC (msg_level, "Override default message level");
92
1da177e4
LT
93/*-------------------------------------------------------------------------*/
94
1da177e4 95/* handles CDC Ethernet and many other network "bulk data" interfaces */
2e55cc72 96int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf)
1da177e4
LT
97{
98 int tmp;
99 struct usb_host_interface *alt = NULL;
100 struct usb_host_endpoint *in = NULL, *out = NULL;
101 struct usb_host_endpoint *status = NULL;
102
103 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
104 unsigned ep;
105
106 in = out = status = NULL;
107 alt = intf->altsetting + tmp;
108
109 /* take the first altsetting with in-bulk + out-bulk;
110 * remember any status endpoint, just in case;
70f23fd6 111 * ignore other endpoints and altsettings.
1da177e4
LT
112 */
113 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
114 struct usb_host_endpoint *e;
115 int intr = 0;
116
117 e = alt->endpoint + ep;
118 switch (e->desc.bmAttributes) {
119 case USB_ENDPOINT_XFER_INT:
fc6e2544 120 if (!usb_endpoint_dir_in(&e->desc))
1da177e4
LT
121 continue;
122 intr = 1;
123 /* FALLTHROUGH */
124 case USB_ENDPOINT_XFER_BULK:
125 break;
126 default:
127 continue;
128 }
fc6e2544 129 if (usb_endpoint_dir_in(&e->desc)) {
1da177e4
LT
130 if (!intr && !in)
131 in = e;
132 else if (intr && !status)
133 status = e;
134 } else {
135 if (!out)
136 out = e;
137 }
138 }
139 if (in && out)
140 break;
141 }
142 if (!alt || !in || !out)
143 return -EINVAL;
144
8e95a202
JP
145 if (alt->desc.bAlternateSetting != 0 ||
146 !(dev->driver_info->flags & FLAG_NO_SETINT)) {
1da177e4
LT
147 tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
148 alt->desc.bAlternateSetting);
149 if (tmp < 0)
150 return tmp;
151 }
cb1cebbe 152
1da177e4
LT
153 dev->in = usb_rcvbulkpipe (dev->udev,
154 in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
155 dev->out = usb_sndbulkpipe (dev->udev,
156 out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
157 dev->status = status;
158 return 0;
159}
2e55cc72 160EXPORT_SYMBOL_GPL(usbnet_get_endpoints);
1da177e4 161
03ad032b
PH
162int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
163{
51487ae7 164 int tmp = -1, ret;
03ad032b
PH
165 unsigned char buf [13];
166
51487ae7
AS
167 ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
168 if (ret == 12)
169 tmp = hex2bin(dev->net->dev_addr, buf, 6);
170 if (tmp < 0) {
03ad032b
PH
171 dev_dbg(&dev->udev->dev,
172 "bad MAC string %d fetch, %d\n", iMACAddress, tmp);
51487ae7
AS
173 if (ret >= 0)
174 ret = -EINVAL;
175 return ret;
03ad032b 176 }
03ad032b
PH
177 return 0;
178}
179EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
180
24ead299 181static void intr_complete (struct urb *urb)
182{
183 struct usbnet *dev = urb->context;
184 int status = urb->status;
185
186 switch (status) {
187 /* success */
188 case 0:
189 dev->driver_info->status(dev, urb);
190 break;
191
192 /* software-driven interface shutdown */
193 case -ENOENT: /* urb killed */
194 case -ESHUTDOWN: /* hardware gone */
195 netif_dbg(dev, ifdown, dev->net,
196 "intr shutdown, code %d\n", status);
197 return;
198
199 /* NOTE: not throttling like RX/TX, since this endpoint
200 * already polls infrequently
201 */
202 default:
203 netdev_dbg(dev->net, "intr status %d\n", status);
204 break;
205 }
206
24ead299 207 status = usb_submit_urb (urb, GFP_ATOMIC);
208 if (status != 0)
209 netif_err(dev, timer, dev->net,
210 "intr resubmit --> %d\n", status);
211}
1da177e4
LT
212
213static int init_status (struct usbnet *dev, struct usb_interface *intf)
214{
215 char *buf = NULL;
216 unsigned pipe = 0;
217 unsigned maxp;
218 unsigned period;
219
220 if (!dev->driver_info->status)
221 return 0;
222
223 pipe = usb_rcvintpipe (dev->udev,
224 dev->status->desc.bEndpointAddress
225 & USB_ENDPOINT_NUMBER_MASK);
226 maxp = usb_maxpacket (dev->udev, pipe, 0);
227
228 /* avoid 1 msec chatter: min 8 msec poll rate */
229 period = max ((int) dev->status->desc.bInterval,
230 (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
231
e94b1766 232 buf = kmalloc (maxp, GFP_KERNEL);
1da177e4 233 if (buf) {
e94b1766 234 dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
1da177e4
LT
235 if (!dev->interrupt) {
236 kfree (buf);
237 return -ENOMEM;
238 } else {
239 usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
240 buf, maxp, intr_complete, dev, period);
720f3d7c 241 dev->interrupt->transfer_flags |= URB_FREE_BUFFER;
1da177e4
LT
242 dev_dbg(&intf->dev,
243 "status ep%din, %d bytes period %d\n",
244 usb_pipeendpoint(pipe), maxp, period);
245 }
246 }
18ab458f 247 return 0;
1da177e4
LT
248}
249
6eecdc5f
DW
250/* Submit the interrupt URB if not previously submitted, increasing refcount */
251int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags)
252{
253 int ret = 0;
254
255 WARN_ON_ONCE(dev->interrupt == NULL);
256 if (dev->interrupt) {
257 mutex_lock(&dev->interrupt_mutex);
258
259 if (++dev->interrupt_count == 1)
260 ret = usb_submit_urb(dev->interrupt, mem_flags);
261
262 dev_dbg(&dev->udev->dev, "incremented interrupt URB count to %d\n",
263 dev->interrupt_count);
264 mutex_unlock(&dev->interrupt_mutex);
265 }
266 return ret;
267}
268EXPORT_SYMBOL_GPL(usbnet_status_start);
269
270/* For resume; submit interrupt URB if previously submitted */
271static int __usbnet_status_start_force(struct usbnet *dev, gfp_t mem_flags)
272{
273 int ret = 0;
274
275 mutex_lock(&dev->interrupt_mutex);
276 if (dev->interrupt_count) {
277 ret = usb_submit_urb(dev->interrupt, mem_flags);
278 dev_dbg(&dev->udev->dev,
279 "submitted interrupt URB for resume\n");
280 }
281 mutex_unlock(&dev->interrupt_mutex);
282 return ret;
283}
284
285/* Kill the interrupt URB if all submitters want it killed */
286void usbnet_status_stop(struct usbnet *dev)
287{
288 if (dev->interrupt) {
289 mutex_lock(&dev->interrupt_mutex);
290 WARN_ON(dev->interrupt_count == 0);
291
292 if (dev->interrupt_count && --dev->interrupt_count == 0)
293 usb_kill_urb(dev->interrupt);
294
295 dev_dbg(&dev->udev->dev,
296 "decremented interrupt URB count to %d\n",
297 dev->interrupt_count);
298 mutex_unlock(&dev->interrupt_mutex);
299 }
300}
301EXPORT_SYMBOL_GPL(usbnet_status_stop);
302
303/* For suspend; always kill interrupt URB */
304static void __usbnet_status_stop_force(struct usbnet *dev)
305{
306 if (dev->interrupt) {
307 mutex_lock(&dev->interrupt_mutex);
308 usb_kill_urb(dev->interrupt);
309 dev_dbg(&dev->udev->dev, "killed interrupt URB for suspend\n");
310 mutex_unlock(&dev->interrupt_mutex);
311 }
312}
313
2e55cc72
DB
314/* Passes this packet up the stack, updating its accounting.
315 * Some link protocols batch packets, so their rx_fixup paths
316 * can return clones as well as just modify the original skb.
317 */
318void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb)
1da177e4
LT
319{
320 int status;
321
7834ddbc
JK
322 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
323 skb_queue_tail(&dev->rxq_pause, skb);
324 return;
325 }
326
1da177e4 327 skb->protocol = eth_type_trans (skb, dev->net);
7963837f
HX
328 dev->net->stats.rx_packets++;
329 dev->net->stats.rx_bytes += skb->len;
1da177e4 330
a475f603
JP
331 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
332 skb->len + sizeof (struct ethhdr), skb->protocol);
1da177e4 333 memset (skb->cb, 0, sizeof (struct skb_data));
f9b491ec
MR
334
335 if (skb_defer_rx_timestamp(skb))
336 return;
337
1da177e4 338 status = netif_rx (skb);
a475f603
JP
339 if (status != NET_RX_SUCCESS)
340 netif_dbg(dev, rx_err, dev->net,
341 "netif_rx status %d\n", status);
1da177e4 342}
2e55cc72 343EXPORT_SYMBOL_GPL(usbnet_skb_return);
1da177e4 344
a88c32ae
ML
345/* must be called if hard_mtu or rx_urb_size changed */
346void usbnet_update_max_qlen(struct usbnet *dev)
347{
348 enum usb_device_speed speed = dev->udev->speed;
349
350 switch (speed) {
351 case USB_SPEED_HIGH:
352 dev->rx_qlen = MAX_QUEUE_MEMORY / dev->rx_urb_size;
353 dev->tx_qlen = MAX_QUEUE_MEMORY / dev->hard_mtu;
354 break;
452c447a
ML
355 case USB_SPEED_SUPER:
356 /*
357 * Not take default 5ms qlen for super speed HC to
358 * save memory, and iperf tests show 2.5ms qlen can
359 * work well
360 */
361 dev->rx_qlen = 5 * MAX_QUEUE_MEMORY / dev->rx_urb_size;
362 dev->tx_qlen = 5 * MAX_QUEUE_MEMORY / dev->hard_mtu;
363 break;
a88c32ae
ML
364 default:
365 dev->rx_qlen = dev->tx_qlen = 4;
366 }
367}
368EXPORT_SYMBOL_GPL(usbnet_update_max_qlen);
369
1da177e4
LT
370\f
371/*-------------------------------------------------------------------------
372 *
373 * Network Device Driver (peer link to "Host Device", from USB host)
374 *
375 *-------------------------------------------------------------------------*/
376
777baa47 377int usbnet_change_mtu (struct net_device *net, int new_mtu)
1da177e4
LT
378{
379 struct usbnet *dev = netdev_priv(net);
f29fc259 380 int ll_mtu = new_mtu + net->hard_header_len;
a99c1949
JP
381 int old_hard_mtu = dev->hard_mtu;
382 int old_rx_urb_size = dev->rx_urb_size;
1da177e4 383
a99c1949 384 if (new_mtu <= 0)
1da177e4 385 return -EINVAL;
1da177e4 386 // no second zero-length packet read wanted after mtu-sized packets
f29fc259 387 if ((ll_mtu % dev->maxpacket) == 0)
1da177e4
LT
388 return -EDOM;
389 net->mtu = new_mtu;
a99c1949
JP
390
391 dev->hard_mtu = net->mtu + net->hard_header_len;
392 if (dev->rx_urb_size == old_hard_mtu) {
393 dev->rx_urb_size = dev->hard_mtu;
394 if (dev->rx_urb_size > old_rx_urb_size)
395 usbnet_unlink_rx_urbs(dev);
396 }
397
a88c32ae
ML
398 /* max qlen depend on hard_mtu and rx_urb_size */
399 usbnet_update_max_qlen(dev);
400
1da177e4
LT
401 return 0;
402}
777baa47 403EXPORT_SYMBOL_GPL(usbnet_change_mtu);
1da177e4 404
5b6e9bcd
ML
405/* The caller must hold list->lock */
406static void __usbnet_queue_skb(struct sk_buff_head *list,
407 struct sk_buff *newsk, enum skb_state state)
408{
409 struct skb_data *entry = (struct skb_data *) newsk->cb;
410
411 __skb_queue_tail(list, newsk);
412 entry->state = state;
413}
414
1da177e4
LT
415/*-------------------------------------------------------------------------*/
416
1da177e4
LT
417/* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
418 * completion callbacks. 2.5 should have fixed those bugs...
419 */
420
5b6e9bcd
ML
421static enum skb_state defer_bh(struct usbnet *dev, struct sk_buff *skb,
422 struct sk_buff_head *list, enum skb_state state)
1da177e4 423{
1da177e4 424 unsigned long flags;
5b6e9bcd
ML
425 enum skb_state old_state;
426 struct skb_data *entry = (struct skb_data *) skb->cb;
1da177e4 427
8728b834 428 spin_lock_irqsave(&list->lock, flags);
5b6e9bcd
ML
429 old_state = entry->state;
430 entry->state = state;
8728b834 431 __skb_unlink(skb, list);
fcb0bb6a
ES
432
433 /* defer_bh() is never called with list == &dev->done.
434 * spin_lock_nested() tells lockdep that it is OK to take
435 * dev->done.lock here with list->lock held.
436 */
437 spin_lock_nested(&dev->done.lock, SINGLE_DEPTH_NESTING);
438
8728b834 439 __skb_queue_tail(&dev->done, skb);
1da177e4 440 if (dev->done.qlen == 1)
8728b834 441 tasklet_schedule(&dev->bh);
fcb0bb6a
ES
442 spin_unlock(&dev->done.lock);
443 spin_unlock_irqrestore(&list->lock, flags);
5b6e9bcd 444 return old_state;
1da177e4
LT
445}
446
447/* some work can't be done in tasklets, so we use keventd
448 *
449 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
450 * but tasklet_schedule() doesn't. hope the failure is rare.
451 */
2e55cc72 452void usbnet_defer_kevent (struct usbnet *dev, int work)
1da177e4
LT
453{
454 set_bit (work, &dev->flags);
9532021d
SG
455 if (!schedule_work (&dev->kevent)) {
456 if (net_ratelimit())
457 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
458 } else {
60b86755 459 netdev_dbg(dev->net, "kevent %d scheduled\n", work);
9532021d 460 }
1da177e4 461}
2e55cc72 462EXPORT_SYMBOL_GPL(usbnet_defer_kevent);
1da177e4
LT
463
464/*-------------------------------------------------------------------------*/
465
7d12e780 466static void rx_complete (struct urb *urb);
1da177e4 467
dacb3975 468static int rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags)
1da177e4
LT
469{
470 struct sk_buff *skb;
471 struct skb_data *entry;
472 int retval = 0;
473 unsigned long lockflags;
2e55cc72 474 size_t size = dev->rx_urb_size;
1da177e4 475
70c37bf9
BM
476 /* prevent rx skb allocation when error ratio is high */
477 if (test_bit(EVENT_RX_KILL, &dev->flags)) {
478 usb_free_urb(urb);
479 return -ENOLINK;
480 }
481
7bdd4027
ED
482 skb = __netdev_alloc_skb_ip_align(dev->net, size, flags);
483 if (!skb) {
a475f603 484 netif_dbg(dev, rx_err, dev->net, "no rx skb\n");
2e55cc72 485 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4 486 usb_free_urb (urb);
dacb3975 487 return -ENOMEM;
1da177e4 488 }
1da177e4
LT
489
490 entry = (struct skb_data *) skb->cb;
491 entry->urb = urb;
492 entry->dev = dev;
1da177e4
LT
493 entry->length = 0;
494
495 usb_fill_bulk_urb (urb, dev->udev, dev->in,
496 skb->data, size, rx_complete, skb);
1da177e4
LT
497
498 spin_lock_irqsave (&dev->rxq.lock, lockflags);
499
8e95a202
JP
500 if (netif_running (dev->net) &&
501 netif_device_present (dev->net) &&
69ee472f
ON
502 !test_bit (EVENT_RX_HALT, &dev->flags) &&
503 !test_bit (EVENT_DEV_ASLEEP, &dev->flags)) {
18ab458f 504 switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) {
1da177e4 505 case -EPIPE:
2e55cc72 506 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
507 break;
508 case -ENOMEM:
2e55cc72 509 usbnet_defer_kevent (dev, EVENT_RX_MEMORY);
1da177e4
LT
510 break;
511 case -ENODEV:
a475f603 512 netif_dbg(dev, ifdown, dev->net, "device gone\n");
1da177e4
LT
513 netif_device_detach (dev->net);
514 break;
dacb3975
DM
515 case -EHOSTUNREACH:
516 retval = -ENOLINK;
517 break;
1da177e4 518 default:
a475f603
JP
519 netif_dbg(dev, rx_err, dev->net,
520 "rx submit, %d\n", retval);
1da177e4
LT
521 tasklet_schedule (&dev->bh);
522 break;
523 case 0:
5b6e9bcd 524 __usbnet_queue_skb(&dev->rxq, skb, rx_start);
1da177e4
LT
525 }
526 } else {
a475f603 527 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
1da177e4
LT
528 retval = -ENOLINK;
529 }
530 spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
531 if (retval) {
532 dev_kfree_skb_any (skb);
533 usb_free_urb (urb);
534 }
dacb3975 535 return retval;
1da177e4
LT
536}
537
538
539/*-------------------------------------------------------------------------*/
540
541static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
542{
8e95a202 543 if (dev->driver_info->rx_fixup &&
7a635ea9
AZ
544 !dev->driver_info->rx_fixup (dev, skb)) {
545 /* With RX_ASSEMBLE, rx_fixup() must update counters */
546 if (!(dev->driver_info->flags & FLAG_RX_ASSEMBLE))
547 dev->net->stats.rx_errors++;
548 goto done;
549 }
1da177e4
LT
550 // else network stack removes extra byte if we forced a short packet
551
eb85569f
EG
552 /* all data was already cloned from skb inside the driver */
553 if (dev->driver_info->flags & FLAG_MULTI_PACKET)
554 goto done;
555
556 if (skb->len < ETH_HLEN) {
557 dev->net->stats.rx_errors++;
558 dev->net->stats.rx_length_errors++;
559 netif_dbg(dev, rx_err, dev->net, "rx length %d\n", skb->len);
560 } else {
561 usbnet_skb_return(dev, skb);
073285fd 562 return;
1da177e4 563 }
073285fd 564
7a635ea9 565done:
073285fd 566 skb_queue_tail(&dev->done, skb);
1da177e4
LT
567}
568
569/*-------------------------------------------------------------------------*/
570
7d12e780 571static void rx_complete (struct urb *urb)
1da177e4
LT
572{
573 struct sk_buff *skb = (struct sk_buff *) urb->context;
574 struct skb_data *entry = (struct skb_data *) skb->cb;
575 struct usbnet *dev = entry->dev;
576 int urb_status = urb->status;
5b6e9bcd 577 enum skb_state state;
1da177e4
LT
578
579 skb_put (skb, urb->actual_length);
5b6e9bcd 580 state = rx_done;
1da177e4
LT
581 entry->urb = NULL;
582
583 switch (urb_status) {
18ab458f
DB
584 /* success */
585 case 0:
1da177e4
LT
586 break;
587
18ab458f
DB
588 /* stalls need manual reset. this is rare ... except that
589 * when going through USB 2.0 TTs, unplug appears this way.
3ac49a1c 590 * we avoid the highspeed version of the ETIMEDOUT/EILSEQ
18ab458f
DB
591 * storm, recovering as needed.
592 */
593 case -EPIPE:
7963837f 594 dev->net->stats.rx_errors++;
2e55cc72 595 usbnet_defer_kevent (dev, EVENT_RX_HALT);
1da177e4
LT
596 // FALLTHROUGH
597
18ab458f
DB
598 /* software-driven interface shutdown */
599 case -ECONNRESET: /* async unlink */
600 case -ESHUTDOWN: /* hardware gone */
a475f603
JP
601 netif_dbg(dev, ifdown, dev->net,
602 "rx shutdown, code %d\n", urb_status);
1da177e4
LT
603 goto block;
604
37ebb549 605 /* we get controller i/o faults during hub_wq disconnect() delays.
18ab458f 606 * throttle down resubmits, to avoid log floods; just temporarily,
37ebb549 607 * so we still recover when the fault isn't a hub_wq delay.
18ab458f
DB
608 */
609 case -EPROTO:
610 case -ETIME:
611 case -EILSEQ:
7963837f 612 dev->net->stats.rx_errors++;
1da177e4
LT
613 if (!timer_pending (&dev->delay)) {
614 mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
a475f603
JP
615 netif_dbg(dev, link, dev->net,
616 "rx throttle %d\n", urb_status);
1da177e4
LT
617 }
618block:
5b6e9bcd 619 state = rx_cleanup;
1da177e4
LT
620 entry->urb = urb;
621 urb = NULL;
622 break;
623
18ab458f
DB
624 /* data overrun ... flush fifo? */
625 case -EOVERFLOW:
7963837f 626 dev->net->stats.rx_over_errors++;
1da177e4 627 // FALLTHROUGH
cb1cebbe 628
18ab458f 629 default:
5b6e9bcd 630 state = rx_cleanup;
7963837f 631 dev->net->stats.rx_errors++;
a475f603 632 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
1da177e4
LT
633 break;
634 }
635
70c37bf9
BM
636 /* stop rx if packet error rate is high */
637 if (++dev->pkt_cnt > 30) {
638 dev->pkt_cnt = 0;
639 dev->pkt_err = 0;
640 } else {
641 if (state == rx_cleanup)
642 dev->pkt_err++;
643 if (dev->pkt_err > 20)
644 set_bit(EVENT_RX_KILL, &dev->flags);
645 }
646
5b6e9bcd 647 state = defer_bh(dev, skb, &dev->rxq, state);
1da177e4
LT
648
649 if (urb) {
8e95a202 650 if (netif_running (dev->net) &&
5b6e9bcd
ML
651 !test_bit (EVENT_RX_HALT, &dev->flags) &&
652 state != unlink_start) {
1da177e4 653 rx_submit (dev, urb, GFP_ATOMIC);
8a783354 654 usb_mark_last_busy(dev->udev);
1da177e4
LT
655 return;
656 }
657 usb_free_urb (urb);
658 }
a475f603 659 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
1da177e4
LT
660}
661
7834ddbc
JK
662/*-------------------------------------------------------------------------*/
663void usbnet_pause_rx(struct usbnet *dev)
664{
665 set_bit(EVENT_RX_PAUSED, &dev->flags);
666
a475f603 667 netif_dbg(dev, rx_status, dev->net, "paused rx queue enabled\n");
7834ddbc
JK
668}
669EXPORT_SYMBOL_GPL(usbnet_pause_rx);
670
671void usbnet_resume_rx(struct usbnet *dev)
672{
673 struct sk_buff *skb;
674 int num = 0;
675
676 clear_bit(EVENT_RX_PAUSED, &dev->flags);
677
678 while ((skb = skb_dequeue(&dev->rxq_pause)) != NULL) {
679 usbnet_skb_return(dev, skb);
680 num++;
681 }
682
683 tasklet_schedule(&dev->bh);
684
a475f603
JP
685 netif_dbg(dev, rx_status, dev->net,
686 "paused rx queue disabled, %d skbs requeued\n", num);
7834ddbc
JK
687}
688EXPORT_SYMBOL_GPL(usbnet_resume_rx);
689
690void usbnet_purge_paused_rxq(struct usbnet *dev)
691{
692 skb_queue_purge(&dev->rxq_pause);
693}
694EXPORT_SYMBOL_GPL(usbnet_purge_paused_rxq);
695
1da177e4
LT
696/*-------------------------------------------------------------------------*/
697
698// unlink pending rx/tx; completion handlers do all other cleanup
699
700static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
701{
702 unsigned long flags;
5b6e9bcd 703 struct sk_buff *skb;
1da177e4
LT
704 int count = 0;
705
706 spin_lock_irqsave (&q->lock, flags);
5b6e9bcd 707 while (!skb_queue_empty(q)) {
1da177e4
LT
708 struct skb_data *entry;
709 struct urb *urb;
710 int retval;
711
5b6e9bcd
ML
712 skb_queue_walk(q, skb) {
713 entry = (struct skb_data *) skb->cb;
714 if (entry->state != unlink_start)
715 goto found;
716 }
717 break;
718found:
719 entry->state = unlink_start;
1da177e4 720 urb = entry->urb;
1da177e4 721
0956a8c2 722 /*
723 * Get reference count of the URB to avoid it to be
724 * freed during usb_unlink_urb, which may trigger
725 * use-after-free problem inside usb_unlink_urb since
726 * usb_unlink_urb is always racing with .complete
727 * handler(include defer_bh).
728 */
729 usb_get_urb(urb);
4231d47e 730 spin_unlock_irqrestore(&q->lock, flags);
1da177e4
LT
731 // during some PM-driven resume scenarios,
732 // these (async) unlinks complete immediately
733 retval = usb_unlink_urb (urb);
734 if (retval != -EINPROGRESS && retval != 0)
60b86755 735 netdev_dbg(dev->net, "unlink urb err, %d\n", retval);
1da177e4
LT
736 else
737 count++;
0956a8c2 738 usb_put_urb(urb);
4231d47e 739 spin_lock_irqsave(&q->lock, flags);
1da177e4
LT
740 }
741 spin_unlock_irqrestore (&q->lock, flags);
742 return count;
743}
744
a99c1949
JP
745// Flush all pending rx urbs
746// minidrivers may need to do this when the MTU changes
747
748void usbnet_unlink_rx_urbs(struct usbnet *dev)
749{
750 if (netif_running(dev->net)) {
751 (void) unlink_urbs (dev, &dev->rxq);
752 tasklet_schedule(&dev->bh);
753 }
754}
755EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs);
1da177e4
LT
756
757/*-------------------------------------------------------------------------*/
758
fcb0bb6a
ES
759static void wait_skb_queue_empty(struct sk_buff_head *q)
760{
761 unsigned long flags;
762
763 spin_lock_irqsave(&q->lock, flags);
764 while (!skb_queue_empty(q)) {
765 spin_unlock_irqrestore(&q->lock, flags);
766 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 spin_lock_irqsave(&q->lock, flags);
769 }
770 spin_unlock_irqrestore(&q->lock, flags);
771}
772
1da177e4 773// precondition: never called in_interrupt
69ee472f
ON
774static void usbnet_terminate_urbs(struct usbnet *dev)
775{
69ee472f
ON
776 DECLARE_WAITQUEUE(wait, current);
777 int temp;
778
779 /* ensure there are no more active urbs */
14a0d635 780 add_wait_queue(&dev->wait, &wait);
69ee472f 781 set_current_state(TASK_UNINTERRUPTIBLE);
69ee472f
ON
782 temp = unlink_urbs(dev, &dev->txq) +
783 unlink_urbs(dev, &dev->rxq);
784
785 /* maybe wait for deletions to finish. */
fcb0bb6a
ES
786 wait_skb_queue_empty(&dev->rxq);
787 wait_skb_queue_empty(&dev->txq);
788 wait_skb_queue_empty(&dev->done);
789 netif_dbg(dev, ifdown, dev->net,
790 "waited for %d urb completions\n", temp);
69ee472f 791 set_current_state(TASK_RUNNING);
14a0d635 792 remove_wait_queue(&dev->wait, &wait);
69ee472f 793}
1da177e4 794
777baa47 795int usbnet_stop (struct net_device *net)
1da177e4
LT
796{
797 struct usbnet *dev = netdev_priv(net);
a33e9e7f 798 struct driver_info *info = dev->driver_info;
f50791ac 799 int retval, pm, mpn;
1da177e4 800
75bd0cbd 801 clear_bit(EVENT_DEV_OPEN, &dev->flags);
1da177e4
LT
802 netif_stop_queue (net);
803
a475f603 804 netif_info(dev, ifdown, dev->net,
8ccef431 805 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
a475f603
JP
806 net->stats.rx_packets, net->stats.tx_packets,
807 net->stats.rx_errors, net->stats.tx_errors);
1da177e4 808
14a0d635
ON
809 /* to not race resume */
810 pm = usb_autopm_get_interface(dev->intf);
a33e9e7f
JK
811 /* allow minidriver to stop correctly (wireless devices to turn off
812 * radio etc) */
813 if (info->stop) {
814 retval = info->stop(dev);
a475f603
JP
815 if (retval < 0)
816 netif_info(dev, ifdown, dev->net,
817 "stop fail (%d) usbnet usb-%s-%s, %s\n",
818 retval,
819 dev->udev->bus->bus_name, dev->udev->devpath,
820 info->description);
a33e9e7f
JK
821 }
822
69ee472f
ON
823 if (!(info->flags & FLAG_AVOID_UNLINK_URBS))
824 usbnet_terminate_urbs(dev);
1da177e4 825
6eecdc5f 826 usbnet_status_stop(dev);
1da177e4 827
7834ddbc
JK
828 usbnet_purge_paused_rxq(dev);
829
f50791ac
ES
830 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
831
1da177e4
LT
832 /* deferred work (task, timer, softirq) must also stop.
833 * can't flush_scheduled_work() until we drop rtnl (later),
834 * else workers could deadlock; so make workers a NOP.
835 */
836 dev->flags = 0;
837 del_timer_sync (&dev->delay);
838 tasklet_kill (&dev->bh);
14a0d635
ON
839 if (!pm)
840 usb_autopm_put_interface(dev->intf);
841
f50791ac 842 if (info->manage_power && mpn)
69ee472f
ON
843 info->manage_power(dev, 0);
844 else
845 usb_autopm_put_interface(dev->intf);
1da177e4
LT
846
847 return 0;
848}
777baa47 849EXPORT_SYMBOL_GPL(usbnet_stop);
1da177e4
LT
850
851/*-------------------------------------------------------------------------*/
852
853// posts reads, and enables write queuing
854
855// precondition: never called in_interrupt
856
777baa47 857int usbnet_open (struct net_device *net)
1da177e4
LT
858{
859 struct usbnet *dev = netdev_priv(net);
a11a6544 860 int retval;
1da177e4
LT
861 struct driver_info *info = dev->driver_info;
862
a11a6544 863 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
a475f603
JP
864 netif_info(dev, ifup, dev->net,
865 "resumption fail (%d) usbnet usb-%s-%s, %s\n",
866 retval,
867 dev->udev->bus->bus_name,
868 dev->udev->devpath,
869 info->description);
a11a6544
ON
870 goto done_nopm;
871 }
872
1da177e4
LT
873 // put into "known safe" state
874 if (info->reset && (retval = info->reset (dev)) < 0) {
a475f603
JP
875 netif_info(dev, ifup, dev->net,
876 "open reset fail (%d) usbnet usb-%s-%s, %s\n",
877 retval,
878 dev->udev->bus->bus_name,
879 dev->udev->devpath,
880 info->description);
1da177e4
LT
881 goto done;
882 }
883
a88c32ae
ML
884 /* hard_mtu or rx_urb_size may change in reset() */
885 usbnet_update_max_qlen(dev);
886
1da177e4
LT
887 // insist peer be connected
888 if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
a475f603 889 netif_dbg(dev, ifup, dev->net, "can't open; %d\n", retval);
1da177e4
LT
890 goto done;
891 }
892
893 /* start any status interrupt transfer */
894 if (dev->interrupt) {
6eecdc5f 895 retval = usbnet_status_start(dev, GFP_KERNEL);
1da177e4 896 if (retval < 0) {
a475f603
JP
897 netif_err(dev, ifup, dev->net,
898 "intr submit %d\n", retval);
1da177e4
LT
899 goto done;
900 }
901 }
902
68972efa 903 set_bit(EVENT_DEV_OPEN, &dev->flags);
1da177e4 904 netif_start_queue (net);
a475f603
JP
905 netif_info(dev, ifup, dev->net,
906 "open: enable queueing (rx %d, tx %d) mtu %d %s framing\n",
907 (int)RX_QLEN(dev), (int)TX_QLEN(dev),
908 dev->net->mtu,
909 (dev->driver_info->flags & FLAG_FRAMING_NC) ? "NetChip" :
910 (dev->driver_info->flags & FLAG_FRAMING_GL) ? "GeneSys" :
911 (dev->driver_info->flags & FLAG_FRAMING_Z) ? "Zaurus" :
912 (dev->driver_info->flags & FLAG_FRAMING_RN) ? "RNDIS" :
913 (dev->driver_info->flags & FLAG_FRAMING_AX) ? "ASIX" :
914 "simple");
1da177e4 915
70c37bf9
BM
916 /* reset rx error state */
917 dev->pkt_cnt = 0;
918 dev->pkt_err = 0;
919 clear_bit(EVENT_RX_KILL, &dev->flags);
920
1da177e4
LT
921 // delay posting reads until we're fully open
922 tasklet_schedule (&dev->bh);
69ee472f
ON
923 if (info->manage_power) {
924 retval = info->manage_power(dev, 1);
a1c088e0
ON
925 if (retval < 0) {
926 retval = 0;
927 set_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
928 } else {
929 usb_autopm_put_interface(dev->intf);
930 }
69ee472f 931 }
a11a6544 932 return retval;
1da177e4 933done:
a11a6544
ON
934 usb_autopm_put_interface(dev->intf);
935done_nopm:
1da177e4
LT
936 return retval;
937}
777baa47 938EXPORT_SYMBOL_GPL(usbnet_open);
1da177e4
LT
939
940/*-------------------------------------------------------------------------*/
941
2e55cc72
DB
942/* ethtool methods; minidrivers may need to add some more, but
943 * they'll probably want to use this base set.
944 */
945
c41286fd
AB
946int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd)
947{
948 struct usbnet *dev = netdev_priv(net);
949
950 if (!dev->mii.mdio_read)
951 return -EOPNOTSUPP;
952
953 return mii_ethtool_gset(&dev->mii, cmd);
954}
955EXPORT_SYMBOL_GPL(usbnet_get_settings);
956
957int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd)
958{
959 struct usbnet *dev = netdev_priv(net);
960 int retval;
961
962 if (!dev->mii.mdio_write)
963 return -EOPNOTSUPP;
964
965 retval = mii_ethtool_sset(&dev->mii, cmd);
966
967 /* link speed/duplex might have changed */
968 if (dev->driver_info->link_reset)
969 dev->driver_info->link_reset(dev);
970
a88c32ae
ML
971 /* hard_mtu or rx_urb_size may change in link_reset() */
972 usbnet_update_max_qlen(dev);
973
c41286fd
AB
974 return retval;
975
976}
977EXPORT_SYMBOL_GPL(usbnet_set_settings);
978
c41286fd 979u32 usbnet_get_link (struct net_device *net)
1da177e4
LT
980{
981 struct usbnet *dev = netdev_priv(net);
982
f29fc259 983 /* If a check_connect is defined, return its result */
1da177e4
LT
984 if (dev->driver_info->check_connect)
985 return dev->driver_info->check_connect (dev) == 0;
986
c41286fd
AB
987 /* if the device has mii operations, use those */
988 if (dev->mii.mdio_read)
989 return mii_link_ok(&dev->mii);
990
05ffb3e2
BM
991 /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */
992 return ethtool_op_get_link(net);
1da177e4 993}
c41286fd 994EXPORT_SYMBOL_GPL(usbnet_get_link);
1da177e4 995
18ee91fa 996int usbnet_nway_reset(struct net_device *net)
1da177e4
LT
997{
998 struct usbnet *dev = netdev_priv(net);
999
18ee91fa
DB
1000 if (!dev->mii.mdio_write)
1001 return -EOPNOTSUPP;
1002
1003 return mii_nway_restart(&dev->mii);
1da177e4 1004}
18ee91fa 1005EXPORT_SYMBOL_GPL(usbnet_nway_reset);
1da177e4 1006
18ee91fa 1007void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
1da177e4
LT
1008{
1009 struct usbnet *dev = netdev_priv(net);
1010
86a2f415
PS
1011 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1012 strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
1013 strlcpy (info->fw_version, dev->driver_info->description,
18ee91fa
DB
1014 sizeof info->fw_version);
1015 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
1da177e4 1016}
18ee91fa 1017EXPORT_SYMBOL_GPL(usbnet_get_drvinfo);
1da177e4 1018
18ee91fa 1019u32 usbnet_get_msglevel (struct net_device *net)
c41286fd
AB
1020{
1021 struct usbnet *dev = netdev_priv(net);
1022
18ee91fa
DB
1023 return dev->msg_enable;
1024}
1025EXPORT_SYMBOL_GPL(usbnet_get_msglevel);
c41286fd 1026
18ee91fa
DB
1027void usbnet_set_msglevel (struct net_device *net, u32 level)
1028{
1029 struct usbnet *dev = netdev_priv(net);
1030
1031 dev->msg_enable = level;
c41286fd 1032}
18ee91fa 1033EXPORT_SYMBOL_GPL(usbnet_set_msglevel);
c41286fd 1034
2e55cc72 1035/* drivers may override default ethtool_ops in their bind() routine */
0fc0b732 1036static const struct ethtool_ops usbnet_ethtool_ops = {
c41286fd
AB
1037 .get_settings = usbnet_get_settings,
1038 .set_settings = usbnet_set_settings,
2e55cc72 1039 .get_link = usbnet_get_link,
c41286fd 1040 .nway_reset = usbnet_nway_reset,
18ee91fa 1041 .get_drvinfo = usbnet_get_drvinfo,
2e55cc72
DB
1042 .get_msglevel = usbnet_get_msglevel,
1043 .set_msglevel = usbnet_set_msglevel,
123edb18 1044 .get_ts_info = ethtool_op_get_ts_info,
2e55cc72 1045};
1da177e4
LT
1046
1047/*-------------------------------------------------------------------------*/
1048
4b49f58f
ML
1049static void __handle_link_change(struct usbnet *dev)
1050{
1051 if (!test_bit(EVENT_DEV_OPEN, &dev->flags))
1052 return;
1053
1054 if (!netif_carrier_ok(dev->net)) {
1055 /* kill URBs for reading packets to save bus bandwidth */
1056 unlink_urbs(dev, &dev->rxq);
1057
1058 /*
1059 * tx_timeout will unlink URBs for sending packets and
1060 * tx queue is stopped by netcore after link becomes off
1061 */
1062 } else {
1063 /* submitting URBs for reading packets */
1064 tasklet_schedule(&dev->bh);
1065 }
1066
a88c32ae
ML
1067 /* hard_mtu or rx_urb_size may change during link change */
1068 usbnet_update_max_qlen(dev);
1069
4b49f58f
ML
1070 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
1071}
1072
1efed2d0
OB
1073static void usbnet_set_rx_mode(struct net_device *net)
1074{
1075 struct usbnet *dev = netdev_priv(net);
1076
1077 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
1078}
1079
1080static void __handle_set_rx_mode(struct usbnet *dev)
1081{
1082 if (dev->driver_info->set_rx_mode)
1083 (dev->driver_info->set_rx_mode)(dev);
1084
1085 clear_bit(EVENT_SET_RX_MODE, &dev->flags);
1086}
1087
1da177e4
LT
1088/* work that cannot be done in interrupt context uses keventd.
1089 *
1090 * NOTE: with 2.5 we could do more of this using completion callbacks,
1091 * especially now that control transfers can be queued.
1092 */
1093static void
11f17ef3 1094usbnet_deferred_kevent (struct work_struct *work)
1da177e4 1095{
c4028958
DH
1096 struct usbnet *dev =
1097 container_of(work, struct usbnet, kevent);
1da177e4
LT
1098 int status;
1099
1100 /* usb_clear_halt() needs a thread context */
1101 if (test_bit (EVENT_TX_HALT, &dev->flags)) {
1102 unlink_urbs (dev, &dev->txq);
69ee472f
ON
1103 status = usb_autopm_get_interface(dev->intf);
1104 if (status < 0)
1105 goto fail_pipe;
1da177e4 1106 status = usb_clear_halt (dev->udev, dev->out);
69ee472f 1107 usb_autopm_put_interface(dev->intf);
8e95a202
JP
1108 if (status < 0 &&
1109 status != -EPIPE &&
1110 status != -ESHUTDOWN) {
1da177e4 1111 if (netif_msg_tx_err (dev))
69ee472f 1112fail_pipe:
60b86755
JP
1113 netdev_err(dev->net, "can't clear tx halt, status %d\n",
1114 status);
1da177e4
LT
1115 } else {
1116 clear_bit (EVENT_TX_HALT, &dev->flags);
f29fc259
DB
1117 if (status != -ESHUTDOWN)
1118 netif_wake_queue (dev->net);
1da177e4
LT
1119 }
1120 }
1121 if (test_bit (EVENT_RX_HALT, &dev->flags)) {
1122 unlink_urbs (dev, &dev->rxq);
69ee472f
ON
1123 status = usb_autopm_get_interface(dev->intf);
1124 if (status < 0)
1125 goto fail_halt;
1da177e4 1126 status = usb_clear_halt (dev->udev, dev->in);
69ee472f 1127 usb_autopm_put_interface(dev->intf);
8e95a202
JP
1128 if (status < 0 &&
1129 status != -EPIPE &&
1130 status != -ESHUTDOWN) {
1da177e4 1131 if (netif_msg_rx_err (dev))
69ee472f 1132fail_halt:
60b86755
JP
1133 netdev_err(dev->net, "can't clear rx halt, status %d\n",
1134 status);
1da177e4
LT
1135 } else {
1136 clear_bit (EVENT_RX_HALT, &dev->flags);
1137 tasklet_schedule (&dev->bh);
1138 }
1139 }
1140
1141 /* tasklet could resubmit itself forever if memory is tight */
1142 if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
1143 struct urb *urb = NULL;
dacb3975 1144 int resched = 1;
1da177e4
LT
1145
1146 if (netif_running (dev->net))
1147 urb = usb_alloc_urb (0, GFP_KERNEL);
1148 else
1149 clear_bit (EVENT_RX_MEMORY, &dev->flags);
1150 if (urb != NULL) {
1151 clear_bit (EVENT_RX_MEMORY, &dev->flags);
69ee472f 1152 status = usb_autopm_get_interface(dev->intf);
ab60707f
JJ
1153 if (status < 0) {
1154 usb_free_urb(urb);
69ee472f 1155 goto fail_lowmem;
ab60707f 1156 }
dacb3975
DM
1157 if (rx_submit (dev, urb, GFP_KERNEL) == -ENOLINK)
1158 resched = 0;
69ee472f
ON
1159 usb_autopm_put_interface(dev->intf);
1160fail_lowmem:
dacb3975
DM
1161 if (resched)
1162 tasklet_schedule (&dev->bh);
1da177e4
LT
1163 }
1164 }
1165
7ea13c9c 1166 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
cb1cebbe 1167 struct driver_info *info = dev->driver_info;
7ea13c9c
DH
1168 int retval = 0;
1169
1170 clear_bit (EVENT_LINK_RESET, &dev->flags);
69ee472f
ON
1171 status = usb_autopm_get_interface(dev->intf);
1172 if (status < 0)
1173 goto skip_reset;
7ea13c9c 1174 if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
69ee472f
ON
1175 usb_autopm_put_interface(dev->intf);
1176skip_reset:
60b86755
JP
1177 netdev_info(dev->net, "link reset failed (%d) usbnet usb-%s-%s, %s\n",
1178 retval,
1179 dev->udev->bus->bus_name,
1180 dev->udev->devpath,
1181 info->description);
69ee472f
ON
1182 } else {
1183 usb_autopm_put_interface(dev->intf);
7ea13c9c 1184 }
4b49f58f
ML
1185
1186 /* handle link change from link resetting */
1187 __handle_link_change(dev);
7ea13c9c
DH
1188 }
1189
4b49f58f
ML
1190 if (test_bit (EVENT_LINK_CHANGE, &dev->flags))
1191 __handle_link_change(dev);
1192
1efed2d0
OB
1193 if (test_bit (EVENT_SET_RX_MODE, &dev->flags))
1194 __handle_set_rx_mode(dev);
1195
1196
1da177e4 1197 if (dev->flags)
60b86755 1198 netdev_dbg(dev->net, "kevent done, flags = 0x%lx\n", dev->flags);
1da177e4
LT
1199}
1200
1201/*-------------------------------------------------------------------------*/
1202
7d12e780 1203static void tx_complete (struct urb *urb)
1da177e4
LT
1204{
1205 struct sk_buff *skb = (struct sk_buff *) urb->context;
1206 struct skb_data *entry = (struct skb_data *) skb->cb;
1207 struct usbnet *dev = entry->dev;
1208
1209 if (urb->status == 0) {
1e9e39f4 1210 dev->net->stats.tx_packets += entry->packets;
7963837f 1211 dev->net->stats.tx_bytes += entry->length;
1da177e4 1212 } else {
7963837f 1213 dev->net->stats.tx_errors++;
1da177e4
LT
1214
1215 switch (urb->status) {
1216 case -EPIPE:
2e55cc72 1217 usbnet_defer_kevent (dev, EVENT_TX_HALT);
1da177e4
LT
1218 break;
1219
1220 /* software-driven interface shutdown */
1221 case -ECONNRESET: // async unlink
1222 case -ESHUTDOWN: // hardware gone
1223 break;
1224
37ebb549
PM
1225 /* like rx, tx gets controller i/o faults during hub_wq
1226 * delays and so it uses the same throttling mechanism.
1227 */
38e2bfc9
PZ
1228 case -EPROTO:
1229 case -ETIME:
1230 case -EILSEQ:
69ee472f 1231 usb_mark_last_busy(dev->udev);
1da177e4
LT
1232 if (!timer_pending (&dev->delay)) {
1233 mod_timer (&dev->delay,
1234 jiffies + THROTTLE_JIFFIES);
a475f603
JP
1235 netif_dbg(dev, link, dev->net,
1236 "tx throttle %d\n", urb->status);
1da177e4
LT
1237 }
1238 netif_stop_queue (dev->net);
1239 break;
1240 default:
a475f603
JP
1241 netif_dbg(dev, tx_err, dev->net,
1242 "tx err %d\n", entry->urb->status);
1da177e4
LT
1243 break;
1244 }
1245 }
1246
69ee472f 1247 usb_autopm_put_interface_async(dev->intf);
5b6e9bcd 1248 (void) defer_bh(dev, skb, &dev->txq, tx_done);
1da177e4
LT
1249}
1250
1251/*-------------------------------------------------------------------------*/
1252
777baa47 1253void usbnet_tx_timeout (struct net_device *net)
1da177e4
LT
1254{
1255 struct usbnet *dev = netdev_priv(net);
1256
1257 unlink_urbs (dev, &dev->txq);
1258 tasklet_schedule (&dev->bh);
dbcdd4d5
ON
1259 /* this needs to be handled individually because the generic layer
1260 * doesn't know what is sufficient and could not restore private
1261 * information if a remedy of an unconditional reset were used.
1262 */
1263 if (dev->driver_info->recover)
1264 (dev->driver_info->recover)(dev);
1da177e4 1265}
777baa47 1266EXPORT_SYMBOL_GPL(usbnet_tx_timeout);
1da177e4
LT
1267
1268/*-------------------------------------------------------------------------*/
1269
638c5115
ML
1270static int build_dma_sg(const struct sk_buff *skb, struct urb *urb)
1271{
1272 unsigned num_sgs, total_len = 0;
1273 int i, s = 0;
1274
1275 num_sgs = skb_shinfo(skb)->nr_frags + 1;
1276 if (num_sgs == 1)
1277 return 0;
1278
60e453a9
ML
1279 /* reserve one for zero packet */
1280 urb->sg = kmalloc((num_sgs + 1) * sizeof(struct scatterlist),
1281 GFP_ATOMIC);
638c5115
ML
1282 if (!urb->sg)
1283 return -ENOMEM;
1284
1285 urb->num_sgs = num_sgs;
fdc3452c 1286 sg_init_table(urb->sg, urb->num_sgs + 1);
638c5115
ML
1287
1288 sg_set_buf(&urb->sg[s++], skb->data, skb_headlen(skb));
1289 total_len += skb_headlen(skb);
1290
1291 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1292 struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1293
1294 total_len += skb_frag_size(f);
1295 sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1296 f->page_offset);
1297 }
1298 urb->transfer_buffer_length = total_len;
1299
1300 return 1;
1301}
1302
25a79c41
SH
1303netdev_tx_t usbnet_start_xmit (struct sk_buff *skb,
1304 struct net_device *net)
1da177e4
LT
1305{
1306 struct usbnet *dev = netdev_priv(net);
3e4336a6 1307 unsigned int length;
1da177e4
LT
1308 struct urb *urb = NULL;
1309 struct skb_data *entry;
1310 struct driver_info *info = dev->driver_info;
1311 unsigned long flags;
25a79c41 1312 int retval;
1da177e4 1313
23ba0799
KK
1314 if (skb)
1315 skb_tx_timestamp(skb);
f9b491ec 1316
1da177e4
LT
1317 // some devices want funky USB-level framing, for
1318 // win32 driver (usually) and/or hardware quirks
1319 if (info->tx_fixup) {
1320 skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
1321 if (!skb) {
bf414b36
BM
1322 /* packet collected; minidriver waiting for more */
1323 if (info->flags & FLAG_MULTI_PACKET)
073285fd 1324 goto not_drop;
bf414b36
BM
1325 netif_dbg(dev, tx_err, dev->net, "can't tx_fixup skb\n");
1326 goto drop;
1da177e4
LT
1327 }
1328 }
1da177e4
LT
1329
1330 if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
a475f603 1331 netif_dbg(dev, tx_err, dev->net, "no urb\n");
1da177e4
LT
1332 goto drop;
1333 }
1334
1335 entry = (struct skb_data *) skb->cb;
1336 entry->urb = urb;
1337 entry->dev = dev;
1da177e4 1338
1da177e4
LT
1339 usb_fill_bulk_urb (urb, dev->udev, dev->out,
1340 skb->data, skb->len, tx_complete, skb);
638c5115
ML
1341 if (dev->can_dma_sg) {
1342 if (build_dma_sg(skb, urb) < 0)
1343 goto drop;
1344 }
60e453a9 1345 length = urb->transfer_buffer_length;
1da177e4
LT
1346
1347 /* don't assume the hardware handles USB_ZERO_PACKET
1348 * NOTE: strictly conforming cdc-ether devices should expect
1349 * the ZLP here, but ignore the one-byte packet.
073285fd
AO
1350 * NOTE2: CDC NCM specification is different from CDC ECM when
1351 * handling ZLP/short packets, so cdc_ncm driver will make short
1352 * packet itself if needed.
1da177e4 1353 */
b4d562e3
EP
1354 if (length % dev->maxpacket == 0) {
1355 if (!(info->flags & FLAG_SEND_ZLP)) {
073285fd 1356 if (!(info->flags & FLAG_MULTI_PACKET)) {
60e453a9
ML
1357 length++;
1358 if (skb_tailroom(skb) && !urb->num_sgs) {
073285fd
AO
1359 skb->data[skb->len] = 0;
1360 __skb_put(skb, 1);
60e453a9
ML
1361 } else if (urb->num_sgs)
1362 sg_set_buf(&urb->sg[urb->num_sgs++],
1363 dev->padding_pkt, 1);
b4d562e3
EP
1364 }
1365 } else
1366 urb->transfer_flags |= URB_ZERO_PACKET;
3e323f3e 1367 }
7a1e890e
BH
1368 urb->transfer_buffer_length = length;
1369
1370 if (info->flags & FLAG_MULTI_PACKET) {
1371 /* Driver has set number of packets and a length delta.
1372 * Calculate the complete length and ensure that it's
1373 * positive.
1374 */
1375 entry->length += length;
1376 if (WARN_ON_ONCE(entry->length <= 0))
1377 entry->length = length;
1378 } else {
1379 usbnet_set_skb_tx_stats(skb, 1, length);
1380 }
1da177e4 1381
69ee472f
ON
1382 spin_lock_irqsave(&dev->txq.lock, flags);
1383 retval = usb_autopm_get_interface_async(dev->intf);
1384 if (retval < 0) {
1385 spin_unlock_irqrestore(&dev->txq.lock, flags);
1386 goto drop;
1387 }
1388
1389#ifdef CONFIG_PM
1390 /* if this triggers the device is still a sleep */
1391 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
1392 /* transmission will be done in resume */
1393 usb_anchor_urb(urb, &dev->deferred);
1394 /* no use to process more packets */
1395 netif_stop_queue(net);
39707c2a 1396 usb_put_urb(urb);
69ee472f 1397 spin_unlock_irqrestore(&dev->txq.lock, flags);
60b86755 1398 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
69ee472f
ON
1399 goto deferred;
1400 }
1401#endif
1da177e4 1402
1da177e4
LT
1403 switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
1404 case -EPIPE:
1405 netif_stop_queue (net);
2e55cc72 1406 usbnet_defer_kevent (dev, EVENT_TX_HALT);
69ee472f 1407 usb_autopm_put_interface_async(dev->intf);
1da177e4
LT
1408 break;
1409 default:
69ee472f 1410 usb_autopm_put_interface_async(dev->intf);
a475f603
JP
1411 netif_dbg(dev, tx_err, dev->net,
1412 "tx: submit urb err %d\n", retval);
1da177e4
LT
1413 break;
1414 case 0:
1415 net->trans_start = jiffies;
5b6e9bcd 1416 __usbnet_queue_skb(&dev->txq, skb, tx_start);
1da177e4
LT
1417 if (dev->txq.qlen >= TX_QLEN (dev))
1418 netif_stop_queue (net);
1419 }
1420 spin_unlock_irqrestore (&dev->txq.lock, flags);
1421
1422 if (retval) {
a475f603 1423 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", retval);
1da177e4 1424drop:
7963837f 1425 dev->net->stats.tx_dropped++;
073285fd 1426not_drop:
1da177e4
LT
1427 if (skb)
1428 dev_kfree_skb_any (skb);
638c5115
ML
1429 if (urb) {
1430 kfree(urb->sg);
1431 usb_free_urb(urb);
1432 }
a475f603
JP
1433 } else
1434 netif_dbg(dev, tx_queued, dev->net,
3e4336a6 1435 "> tx, len %u, type 0x%x\n", length, skb->protocol);
69ee472f
ON
1436#ifdef CONFIG_PM
1437deferred:
1438#endif
25a79c41 1439 return NETDEV_TX_OK;
1da177e4 1440}
777baa47 1441EXPORT_SYMBOL_GPL(usbnet_start_xmit);
1da177e4 1442
85e87870 1443static int rx_alloc_submit(struct usbnet *dev, gfp_t flags)
65841fd5
ML
1444{
1445 struct urb *urb;
1446 int i;
85e87870 1447 int ret = 0;
65841fd5
ML
1448
1449 /* don't refill the queue all at once */
1450 for (i = 0; i < 10 && dev->rxq.qlen < RX_QLEN(dev); i++) {
1451 urb = usb_alloc_urb(0, flags);
1452 if (urb != NULL) {
85e87870
BM
1453 ret = rx_submit(dev, urb, flags);
1454 if (ret)
1455 goto err;
1456 } else {
1457 ret = -ENOMEM;
1458 goto err;
65841fd5
ML
1459 }
1460 }
85e87870
BM
1461err:
1462 return ret;
65841fd5
ML
1463}
1464
1da177e4
LT
1465/*-------------------------------------------------------------------------*/
1466
1467// tasklet (work deferred from completions, in_irq) or timer
1468
1469static void usbnet_bh (unsigned long param)
1470{
1471 struct usbnet *dev = (struct usbnet *) param;
1472 struct sk_buff *skb;
1473 struct skb_data *entry;
1474
1475 while ((skb = skb_dequeue (&dev->done))) {
1476 entry = (struct skb_data *) skb->cb;
1477 switch (entry->state) {
18ab458f 1478 case rx_done:
1da177e4
LT
1479 entry->state = rx_cleanup;
1480 rx_process (dev, skb);
1481 continue;
18ab458f 1482 case tx_done:
638c5115 1483 kfree(entry->urb->sg);
18ab458f 1484 case rx_cleanup:
1da177e4
LT
1485 usb_free_urb (entry->urb);
1486 dev_kfree_skb (skb);
1487 continue;
18ab458f 1488 default:
60b86755 1489 netdev_dbg(dev->net, "bogus skb state %d\n", entry->state);
1da177e4
LT
1490 }
1491 }
1492
70c37bf9
BM
1493 /* restart RX again after disabling due to high error rate */
1494 clear_bit(EVENT_RX_KILL, &dev->flags);
1495
14a0d635
ON
1496 /* waiting for all pending urbs to complete?
1497 * only then can we forgo submitting anew
1498 */
1499 if (waitqueue_active(&dev->wait)) {
1500 if (dev->txq.qlen + dev->rxq.qlen + dev->done.qlen == 0)
1501 wake_up_all(&dev->wait);
1da177e4
LT
1502
1503 // or are we maybe short a few urbs?
8e95a202
JP
1504 } else if (netif_running (dev->net) &&
1505 netif_device_present (dev->net) &&
4b49f58f 1506 netif_carrier_ok(dev->net) &&
8e95a202
JP
1507 !timer_pending (&dev->delay) &&
1508 !test_bit (EVENT_RX_HALT, &dev->flags)) {
1da177e4 1509 int temp = dev->rxq.qlen;
65841fd5
ML
1510
1511 if (temp < RX_QLEN(dev)) {
85e87870
BM
1512 if (rx_alloc_submit(dev, GFP_ATOMIC) == -ENOLINK)
1513 return;
a475f603
JP
1514 if (temp != dev->rxq.qlen)
1515 netif_dbg(dev, link, dev->net,
1516 "rxqlen %d --> %d\n",
1517 temp, dev->rxq.qlen);
65841fd5 1518 if (dev->rxq.qlen < RX_QLEN(dev))
1da177e4
LT
1519 tasklet_schedule (&dev->bh);
1520 }
1521 if (dev->txq.qlen < TX_QLEN (dev))
1522 netif_wake_queue (dev->net);
1523 }
1524}
1525
1526
1da177e4
LT
1527/*-------------------------------------------------------------------------
1528 *
1529 * USB Device Driver support
1530 *
1531 *-------------------------------------------------------------------------*/
cb1cebbe 1532
1da177e4
LT
1533// precondition: never called in_interrupt
1534
38bde1d4 1535void usbnet_disconnect (struct usb_interface *intf)
1da177e4
LT
1536{
1537 struct usbnet *dev;
1538 struct usb_device *xdev;
1539 struct net_device *net;
1540
1541 dev = usb_get_intfdata(intf);
1542 usb_set_intfdata(intf, NULL);
1543 if (!dev)
1544 return;
1545
1546 xdev = interface_to_usbdev (intf);
1547
a475f603
JP
1548 netif_info(dev, probe, dev->net, "unregister '%s' usb-%s-%s, %s\n",
1549 intf->dev.driver->name,
1550 xdev->bus->bus_name, xdev->devpath,
1551 dev->driver_info->description);
cb1cebbe 1552
1da177e4
LT
1553 net = dev->net;
1554 unregister_netdev (net);
1555
23f333a2 1556 cancel_work_sync(&dev->kevent);
1da177e4 1557
39707c2a
HK
1558 usb_scuttle_anchored_urbs(&dev->deferred);
1559
1da177e4
LT
1560 if (dev->driver_info->unbind)
1561 dev->driver_info->unbind (dev, intf);
1562
68972efa
PS
1563 usb_kill_urb(dev->interrupt);
1564 usb_free_urb(dev->interrupt);
60e453a9 1565 kfree(dev->padding_pkt);
68972efa 1566
1da177e4 1567 free_netdev(net);
1da177e4 1568}
38bde1d4 1569EXPORT_SYMBOL_GPL(usbnet_disconnect);
1da177e4 1570
777baa47
SH
1571static const struct net_device_ops usbnet_netdev_ops = {
1572 .ndo_open = usbnet_open,
1573 .ndo_stop = usbnet_stop,
1574 .ndo_start_xmit = usbnet_start_xmit,
1575 .ndo_tx_timeout = usbnet_tx_timeout,
1efed2d0 1576 .ndo_set_rx_mode = usbnet_set_rx_mode,
777baa47
SH
1577 .ndo_change_mtu = usbnet_change_mtu,
1578 .ndo_set_mac_address = eth_mac_addr,
1579 .ndo_validate_addr = eth_validate_addr,
1580};
1da177e4
LT
1581
1582/*-------------------------------------------------------------------------*/
1583
1da177e4
LT
1584// precondition: never called in_interrupt
1585
225794f8
MH
1586static struct device_type wlan_type = {
1587 .name = "wlan",
1588};
1589
1590static struct device_type wwan_type = {
1591 .name = "wwan",
1592};
1593
38bde1d4 1594int
1da177e4
LT
1595usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
1596{
1597 struct usbnet *dev;
cb1cebbe 1598 struct net_device *net;
1da177e4
LT
1599 struct usb_host_interface *interface;
1600 struct driver_info *info;
1601 struct usb_device *xdev;
1602 int status;
296c0242 1603 const char *name;
b0786b43
ML
1604 struct usb_driver *driver = to_usb_driver(udev->dev.driver);
1605
1606 /* usbnet already took usb runtime pm, so have to enable the feature
1607 * for usb interface, otherwise usb_autopm_get_interface may return
98f541c6 1608 * failure if RUNTIME_PM is enabled.
b0786b43
ML
1609 */
1610 if (!driver->supports_autosuspend) {
1611 driver->supports_autosuspend = 1;
1612 pm_runtime_enable(&udev->dev);
1613 }
1da177e4 1614
296c0242 1615 name = udev->dev.driver->name;
1da177e4
LT
1616 info = (struct driver_info *) prod->driver_info;
1617 if (!info) {
296c0242 1618 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
1da177e4
LT
1619 return -ENODEV;
1620 }
1621 xdev = interface_to_usbdev (udev);
1622 interface = udev->cur_altsetting;
1623
1da177e4
LT
1624 status = -ENOMEM;
1625
1626 // set up our own records
1627 net = alloc_etherdev(sizeof(*dev));
41de8d4c 1628 if (!net)
1da177e4 1629 goto out;
1da177e4 1630
0dacca73
BH
1631 /* netdev_printk() needs this so do it as early as possible */
1632 SET_NETDEV_DEV(net, &udev->dev);
1633
1da177e4
LT
1634 dev = netdev_priv(net);
1635 dev->udev = xdev;
a11a6544 1636 dev->intf = udev;
1da177e4 1637 dev->driver_info = info;
296c0242 1638 dev->driver_name = name;
1da177e4
LT
1639 dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1640 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
14a0d635 1641 init_waitqueue_head(&dev->wait);
1da177e4
LT
1642 skb_queue_head_init (&dev->rxq);
1643 skb_queue_head_init (&dev->txq);
1644 skb_queue_head_init (&dev->done);
7834ddbc 1645 skb_queue_head_init(&dev->rxq_pause);
1da177e4
LT
1646 dev->bh.func = usbnet_bh;
1647 dev->bh.data = (unsigned long) dev;
11f17ef3 1648 INIT_WORK (&dev->kevent, usbnet_deferred_kevent);
69ee472f 1649 init_usb_anchor(&dev->deferred);
1da177e4
LT
1650 dev->delay.function = usbnet_bh;
1651 dev->delay.data = (unsigned long) dev;
1652 init_timer (&dev->delay);
a9fc6338 1653 mutex_init (&dev->phy_mutex);
6eecdc5f
DW
1654 mutex_init(&dev->interrupt_mutex);
1655 dev->interrupt_count = 0;
1da177e4 1656
1da177e4
LT
1657 dev->net = net;
1658 strcpy (net->name, "usb%d");
1659 memcpy (net->dev_addr, node_id, sizeof node_id);
1660
2e55cc72
DB
1661 /* rx and tx sides can use different message sizes;
1662 * bind() should set rx_urb_size in that case.
1663 */
1664 dev->hard_mtu = net->mtu + net->hard_header_len;
1da177e4 1665
777baa47 1666 net->netdev_ops = &usbnet_netdev_ops;
777baa47 1667 net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
1da177e4
LT
1668 net->ethtool_ops = &usbnet_ethtool_ops;
1669
1670 // allow device-specific bind/init procedures
1671 // NOTE net->name still not usable ...
1672 if (info->bind) {
1673 status = info->bind (dev, udev);
cb1cebbe
DB
1674 if (status < 0)
1675 goto out1;
1676
1da177e4
LT
1677 // heuristic: "usb%d" for links we know are two-host,
1678 // else "eth%d" when there's reasonable doubt. userspace
1679 // can rename the link if it knows better.
8e95a202 1680 if ((dev->driver_info->flags & FLAG_ETHER) != 0 &&
c261344d
AB
1681 ((dev->driver_info->flags & FLAG_POINTTOPOINT) == 0 ||
1682 (net->dev_addr [0] & 0x02) == 0))
1da177e4 1683 strcpy (net->name, "eth%d");
6e3bbcc5
JK
1684 /* WLAN devices should always be named "wlan%d" */
1685 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1686 strcpy(net->name, "wlan%d");
e1e499ee
MH
1687 /* WWAN devices should always be named "wwan%d" */
1688 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1689 strcpy(net->name, "wwan%d");
f29fc259 1690
6509141f
WS
1691 /* devices that cannot do ARP */
1692 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
1693 net->flags |= IFF_NOARP;
1694
f29fc259
DB
1695 /* maybe the remote can't receive an Ethernet MTU */
1696 if (net->mtu > (dev->hard_mtu - net->hard_header_len))
1697 net->mtu = dev->hard_mtu - net->hard_header_len;
2e55cc72
DB
1698 } else if (!info->in || !info->out)
1699 status = usbnet_get_endpoints (dev, udev);
1da177e4
LT
1700 else {
1701 dev->in = usb_rcvbulkpipe (xdev, info->in);
1702 dev->out = usb_sndbulkpipe (xdev, info->out);
1703 if (!(info->flags & FLAG_NO_SETINT))
1704 status = usb_set_interface (xdev,
1705 interface->desc.bInterfaceNumber,
1706 interface->desc.bAlternateSetting);
1707 else
1708 status = 0;
1709
1710 }
9514bfe5 1711 if (status >= 0 && dev->status)
1da177e4
LT
1712 status = init_status (dev, udev);
1713 if (status < 0)
cb1cebbe 1714 goto out3;
1da177e4 1715
2e55cc72
DB
1716 if (!dev->rx_urb_size)
1717 dev->rx_urb_size = dev->hard_mtu;
1da177e4 1718 dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
cb1cebbe 1719
eef23b53
BM
1720 /* let userspace know we have a random address */
1721 if (ether_addr_equal(net->dev_addr, node_id))
1722 net->addr_assign_type = NET_ADDR_RANDOM;
1723
225794f8
MH
1724 if ((dev->driver_info->flags & FLAG_WLAN) != 0)
1725 SET_NETDEV_DEVTYPE(net, &wlan_type);
1726 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
1727 SET_NETDEV_DEVTYPE(net, &wwan_type);
1728
a88c32ae
ML
1729 /* initialize max rx_qlen and tx_qlen */
1730 usbnet_update_max_qlen(dev);
1731
60e453a9
ML
1732 if (dev->can_dma_sg && !(info->flags & FLAG_SEND_ZLP) &&
1733 !(info->flags & FLAG_MULTI_PACKET)) {
1734 dev->padding_pkt = kzalloc(1, GFP_KERNEL);
09b69024
WY
1735 if (!dev->padding_pkt) {
1736 status = -ENOMEM;
60e453a9 1737 goto out4;
09b69024 1738 }
60e453a9
ML
1739 }
1740
1da177e4
LT
1741 status = register_netdev (net);
1742 if (status)
60e453a9 1743 goto out5;
a475f603
JP
1744 netif_info(dev, probe, dev->net,
1745 "register '%s' at usb-%s-%s, %s, %pM\n",
1746 udev->dev.driver->name,
1747 xdev->bus->bus_name, xdev->devpath,
1748 dev->driver_info->description,
1749 net->dev_addr);
1da177e4
LT
1750
1751 // ok, it's ready to go.
1752 usb_set_intfdata (udev, dev);
1753
1da177e4
LT
1754 netif_device_attach (net);
1755
37e8273c 1756 if (dev->driver_info->flags & FLAG_LINK_INTR)
0162c554 1757 usbnet_link_change(dev, 0, 0);
37e8273c 1758
1da177e4
LT
1759 return 0;
1760
60e453a9
ML
1761out5:
1762 kfree(dev->padding_pkt);
a4723848 1763out4:
1764 usb_free_urb(dev->interrupt);
1da177e4
LT
1765out3:
1766 if (info->unbind)
1767 info->unbind (dev, udev);
1768out1:
1769 free_netdev(net);
1770out:
1da177e4
LT
1771 return status;
1772}
38bde1d4 1773EXPORT_SYMBOL_GPL(usbnet_probe);
1da177e4
LT
1774
1775/*-------------------------------------------------------------------------*/
1776
36433127
ON
1777/*
1778 * suspend the whole driver as soon as the first interface is suspended
1779 * resume only when the last interface is resumed
38bde1d4 1780 */
1da177e4 1781
38bde1d4 1782int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
1da177e4
LT
1783{
1784 struct usbnet *dev = usb_get_intfdata(intf);
cb1cebbe 1785
36433127 1786 if (!dev->suspend_count++) {
69ee472f
ON
1787 spin_lock_irq(&dev->txq.lock);
1788 /* don't autosuspend while transmitting */
5b1b0b81 1789 if (dev->txq.qlen && PMSG_IS_AUTO(message)) {
5eeb3132 1790 dev->suspend_count--;
69ee472f
ON
1791 spin_unlock_irq(&dev->txq.lock);
1792 return -EBUSY;
1793 } else {
1794 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
1795 spin_unlock_irq(&dev->txq.lock);
1796 }
a11a6544
ON
1797 /*
1798 * accelerate emptying of the rx and queues, to avoid
36433127
ON
1799 * having everything error out.
1800 */
1801 netif_device_detach (dev->net);
69ee472f 1802 usbnet_terminate_urbs(dev);
6eecdc5f 1803 __usbnet_status_stop_force(dev);
69ee472f 1804
a11a6544
ON
1805 /*
1806 * reattach so runtime management can use and
1807 * wake the device
1808 */
1809 netif_device_attach (dev->net);
36433127 1810 }
1da177e4
LT
1811 return 0;
1812}
38bde1d4 1813EXPORT_SYMBOL_GPL(usbnet_suspend);
1da177e4 1814
38bde1d4 1815int usbnet_resume (struct usb_interface *intf)
1da177e4
LT
1816{
1817 struct usbnet *dev = usb_get_intfdata(intf);
69ee472f
ON
1818 struct sk_buff *skb;
1819 struct urb *res;
1820 int retval;
1821
1822 if (!--dev->suspend_count) {
6eecdc5f
DW
1823 /* resume interrupt URB if it was previously submitted */
1824 __usbnet_status_start_force(dev, GFP_NOIO);
68972efa 1825
69ee472f
ON
1826 spin_lock_irq(&dev->txq.lock);
1827 while ((res = usb_get_from_anchor(&dev->deferred))) {
1828
69ee472f
ON
1829 skb = (struct sk_buff *)res->context;
1830 retval = usb_submit_urb(res, GFP_ATOMIC);
1831 if (retval < 0) {
1832 dev_kfree_skb_any(skb);
638c5115 1833 kfree(res->sg);
69ee472f
ON
1834 usb_free_urb(res);
1835 usb_autopm_put_interface_async(dev->intf);
1836 } else {
1837 dev->net->trans_start = jiffies;
1838 __skb_queue_tail(&dev->txq, skb);
1839 }
1840 }
1da177e4 1841
69ee472f
ON
1842 smp_mb();
1843 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
1844 spin_unlock_irq(&dev->txq.lock);
75bd0cbd
ML
1845
1846 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
14a0d635
ON
1847 /* handle remote wakeup ASAP
1848 * we cannot race against stop
1849 */
1850 if (netif_device_present(dev->net) &&
65841fd5
ML
1851 !timer_pending(&dev->delay) &&
1852 !test_bit(EVENT_RX_HALT, &dev->flags))
ab6f148d 1853 rx_alloc_submit(dev, GFP_NOIO);
65841fd5 1854
75bd0cbd 1855 if (!(dev->txq.qlen >= TX_QLEN(dev)))
1aa9bc5b 1856 netif_tx_wake_all_queues(dev->net);
75bd0cbd
ML
1857 tasklet_schedule (&dev->bh);
1858 }
69ee472f 1859 }
5d9d01a3
ON
1860
1861 if (test_and_clear_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags))
1862 usb_autopm_get_interface_no_resume(intf);
1863
1da177e4
LT
1864 return 0;
1865}
38bde1d4 1866EXPORT_SYMBOL_GPL(usbnet_resume);
1da177e4 1867
5d9d01a3
ON
1868/*
1869 * Either a subdriver implements manage_power, then it is assumed to always
1870 * be ready to be suspended or it reports the readiness to be suspended
1871 * explicitly
1872 */
1873void usbnet_device_suggests_idle(struct usbnet *dev)
1874{
1875 if (!test_and_set_bit(EVENT_DEVICE_REPORT_IDLE, &dev->flags)) {
1876 dev->intf->needs_remote_wakeup = 1;
1877 usb_autopm_put_interface_async(dev->intf);
1878 }
1879}
1880EXPORT_SYMBOL(usbnet_device_suggests_idle);
1da177e4 1881
2dd7c8cf
ON
1882/*
1883 * For devices that can do without special commands
1884 */
1885int usbnet_manage_power(struct usbnet *dev, int on)
1886{
1887 dev->intf->needs_remote_wakeup = on;
1888 return 0;
1889}
1890EXPORT_SYMBOL(usbnet_manage_power);
1891
ac64995d
ML
1892void usbnet_link_change(struct usbnet *dev, bool link, bool need_reset)
1893{
1894 /* update link after link is reseted */
1895 if (link && !need_reset)
1896 netif_carrier_on(dev->net);
1897 else
1898 netif_carrier_off(dev->net);
1899
1900 if (need_reset && link)
1901 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
4b49f58f
ML
1902 else
1903 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
ac64995d
ML
1904}
1905EXPORT_SYMBOL(usbnet_link_change);
1906
877bd862 1907/*-------------------------------------------------------------------------*/
0547fad2
ML
1908static int __usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1909 u16 value, u16 index, void *data, u16 size)
877bd862
ML
1910{
1911 void *buf = NULL;
1912 int err = -ENOMEM;
1913
1914 netdev_dbg(dev->net, "usbnet_read_cmd cmd=0x%02x reqtype=%02x"
1915 " value=0x%04x index=0x%04x size=%d\n",
1916 cmd, reqtype, value, index, size);
1917
1918 if (data) {
1919 buf = kmalloc(size, GFP_KERNEL);
1920 if (!buf)
1921 goto out;
1922 }
1923
1924 err = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1925 cmd, reqtype, value, index, buf, size,
1926 USB_CTRL_GET_TIMEOUT);
1927 if (err > 0 && err <= size)
1928 memcpy(data, buf, err);
1929 kfree(buf);
1930out:
1931 return err;
1932}
877bd862 1933
0547fad2
ML
1934static int __usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
1935 u16 value, u16 index, const void *data,
1936 u16 size)
877bd862
ML
1937{
1938 void *buf = NULL;
1939 int err = -ENOMEM;
1940
1941 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
1942 " value=0x%04x index=0x%04x size=%d\n",
1943 cmd, reqtype, value, index, size);
1944
1945 if (data) {
1946 buf = kmemdup(data, size, GFP_KERNEL);
1947 if (!buf)
1948 goto out;
1949 }
1950
1951 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1952 cmd, reqtype, value, index, buf, size,
1953 USB_CTRL_SET_TIMEOUT);
1954 kfree(buf);
1955
1956out:
1957 return err;
1958}
0547fad2 1959
c40a2c88
ON
1960int cdc_parse_cdc_header(struct usb_cdc_parsed_header *hdr,
1961 struct usb_interface *intf,
1962 u8 *buffer,
1963 int buflen)
1964{
1965 /* duplicates are ignored */
1966 struct usb_cdc_union_desc *union_header = NULL;
1967
1968 /* duplicates are not tolerated */
1969 struct usb_cdc_header_desc *header = NULL;
1970 struct usb_cdc_ether_desc *ether = NULL;
1971 struct usb_cdc_mdlm_detail_desc *detail = NULL;
1972 struct usb_cdc_mdlm_desc *desc = NULL;
1973
1974 unsigned int elength;
1975 int cnt = 0;
1976
1977 memset(hdr, 0x00, sizeof(struct usb_cdc_parsed_header));
1978 hdr->phonet_magic_present = false;
1979 while (buflen > 0) {
1980 elength = buffer[0];
1981 if (!elength) {
1982 dev_err(&intf->dev, "skipping garbage byte\n");
1983 elength = 1;
1984 goto next_desc;
1985 }
1986 if (buffer[1] != USB_DT_CS_INTERFACE) {
1987 dev_err(&intf->dev, "skipping garbage\n");
1988 goto next_desc;
1989 }
1990
1991 switch (buffer[2]) {
1992 case USB_CDC_UNION_TYPE: /* we've found it */
1993 if (elength < sizeof(struct usb_cdc_union_desc))
1994 goto next_desc;
1995 if (union_header) {
1996 dev_err(&intf->dev, "More than one union descriptor, skipping ...\n");
1997 goto next_desc;
1998 }
1999 union_header = (struct usb_cdc_union_desc *)buffer;
2000 break;
2001 case USB_CDC_COUNTRY_TYPE:
2002 if (elength < sizeof(struct usb_cdc_country_functional_desc))
2003 goto next_desc;
2004 hdr->usb_cdc_country_functional_desc =
2005 (struct usb_cdc_country_functional_desc *)buffer;
2006 break;
2007 case USB_CDC_HEADER_TYPE:
2008 if (elength != sizeof(struct usb_cdc_header_desc))
2009 goto next_desc;
2010 if (header)
2011 return -EINVAL;
2012 header = (struct usb_cdc_header_desc *)buffer;
2013 break;
2014 case USB_CDC_ACM_TYPE:
2015 if (elength < sizeof(struct usb_cdc_acm_descriptor))
2016 goto next_desc;
2017 hdr->usb_cdc_acm_descriptor =
2018 (struct usb_cdc_acm_descriptor *)buffer;
2019 break;
2020 case USB_CDC_ETHERNET_TYPE:
2021 if (elength != sizeof(struct usb_cdc_ether_desc))
2022 goto next_desc;
2023 if (ether)
2024 return -EINVAL;
2025 ether = (struct usb_cdc_ether_desc *)buffer;
2026 break;
2027 case USB_CDC_CALL_MANAGEMENT_TYPE:
2028 if (elength < sizeof(struct usb_cdc_call_mgmt_descriptor))
2029 goto next_desc;
2030 hdr->usb_cdc_call_mgmt_descriptor =
2031 (struct usb_cdc_call_mgmt_descriptor *)buffer;
2032 break;
2033 case USB_CDC_DMM_TYPE:
2034 if (elength < sizeof(struct usb_cdc_dmm_desc))
2035 goto next_desc;
2036 hdr->usb_cdc_dmm_desc =
2037 (struct usb_cdc_dmm_desc *)buffer;
2038 break;
2039 case USB_CDC_MDLM_TYPE:
2040 if (elength < sizeof(struct usb_cdc_mdlm_desc *))
2041 goto next_desc;
2042 if (desc)
2043 return -EINVAL;
2044 desc = (struct usb_cdc_mdlm_desc *)buffer;
2045 break;
2046 case USB_CDC_MDLM_DETAIL_TYPE:
2047 if (elength < sizeof(struct usb_cdc_mdlm_detail_desc *))
2048 goto next_desc;
2049 if (detail)
2050 return -EINVAL;
2051 detail = (struct usb_cdc_mdlm_detail_desc *)buffer;
2052 break;
2053 case USB_CDC_NCM_TYPE:
2054 if (elength < sizeof(struct usb_cdc_ncm_desc))
2055 goto next_desc;
2056 hdr->usb_cdc_ncm_desc = (struct usb_cdc_ncm_desc *)buffer;
2057 break;
2058 case USB_CDC_MBIM_TYPE:
2059 if (elength < sizeof(struct usb_cdc_mbim_desc))
2060 goto next_desc;
2061
2062 hdr->usb_cdc_mbim_desc = (struct usb_cdc_mbim_desc *)buffer;
2063 break;
2064 case USB_CDC_MBIM_EXTENDED_TYPE:
2065 if (elength < sizeof(struct usb_cdc_mbim_extended_desc))
2066 break;
2067 hdr->usb_cdc_mbim_extended_desc =
2068 (struct usb_cdc_mbim_extended_desc *)buffer;
2069 break;
2070 case CDC_PHONET_MAGIC_NUMBER:
2071 hdr->phonet_magic_present = true;
2072 break;
2073 default:
2074 /*
2075 * there are LOTS more CDC descriptors that
2076 * could legitimately be found here.
2077 */
2078 dev_dbg(&intf->dev, "Ignoring descriptor: type %02x, length %ud\n",
2079 buffer[2], elength);
2080 goto next_desc;
2081 }
2082 cnt++;
2083next_desc:
2084 buflen -= elength;
2085 buffer += elength;
2086 }
2087 hdr->usb_cdc_union_desc = union_header;
2088 hdr->usb_cdc_header_desc = header;
2089 hdr->usb_cdc_mdlm_detail_desc = detail;
2090 hdr->usb_cdc_mdlm_desc = desc;
2091 hdr->usb_cdc_ether_desc = ether;
2092 return cnt;
2093}
2094
2095EXPORT_SYMBOL(cdc_parse_cdc_header);
2096
0547fad2
ML
2097/*
2098 * The function can't be called inside suspend/resume callback,
2099 * otherwise deadlock will be caused.
2100 */
2101int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2102 u16 value, u16 index, void *data, u16 size)
2103{
6f0a0986
ML
2104 int ret;
2105
2106 if (usb_autopm_get_interface(dev->intf) < 0)
2107 return -ENODEV;
2108 ret = __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2109 data, size);
2110 usb_autopm_put_interface(dev->intf);
2111 return ret;
0547fad2
ML
2112}
2113EXPORT_SYMBOL_GPL(usbnet_read_cmd);
2114
2115/*
2116 * The function can't be called inside suspend/resume callback,
2117 * otherwise deadlock will be caused.
2118 */
2119int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
2120 u16 value, u16 index, const void *data, u16 size)
2121{
6f0a0986
ML
2122 int ret;
2123
2124 if (usb_autopm_get_interface(dev->intf) < 0)
2125 return -ENODEV;
2126 ret = __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2127 data, size);
2128 usb_autopm_put_interface(dev->intf);
2129 return ret;
0547fad2 2130}
877bd862
ML
2131EXPORT_SYMBOL_GPL(usbnet_write_cmd);
2132
0547fad2
ML
2133/*
2134 * The function can be called inside suspend/resume callback safely
2135 * and should only be called by suspend/resume callback generally.
2136 */
2137int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2138 u16 value, u16 index, void *data, u16 size)
2139{
2140 return __usbnet_read_cmd(dev, cmd, reqtype, value, index,
2141 data, size);
2142}
2143EXPORT_SYMBOL_GPL(usbnet_read_cmd_nopm);
2144
2145/*
2146 * The function can be called inside suspend/resume callback safely
2147 * and should only be called by suspend/resume callback generally.
2148 */
2149int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
2150 u16 value, u16 index, const void *data,
2151 u16 size)
2152{
2153 return __usbnet_write_cmd(dev, cmd, reqtype, value, index,
2154 data, size);
2155}
2156EXPORT_SYMBOL_GPL(usbnet_write_cmd_nopm);
2157
877bd862
ML
2158static void usbnet_async_cmd_cb(struct urb *urb)
2159{
2160 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
2161 int status = urb->status;
2162
2163 if (status < 0)
2164 dev_dbg(&urb->dev->dev, "%s failed with %d",
2165 __func__, status);
2166
2167 kfree(req);
2168 usb_free_urb(urb);
2169}
2170
0547fad2
ML
2171/*
2172 * The caller must make sure that device can't be put into suspend
2173 * state until the control URB completes.
2174 */
877bd862
ML
2175int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
2176 u16 value, u16 index, const void *data, u16 size)
2177{
2178 struct usb_ctrlrequest *req = NULL;
2179 struct urb *urb;
2180 int err = -ENOMEM;
2181 void *buf = NULL;
2182
2183 netdev_dbg(dev->net, "usbnet_write_cmd cmd=0x%02x reqtype=%02x"
2184 " value=0x%04x index=0x%04x size=%d\n",
2185 cmd, reqtype, value, index, size);
2186
2187 urb = usb_alloc_urb(0, GFP_ATOMIC);
2188 if (!urb) {
2189 netdev_err(dev->net, "Error allocating URB in"
2190 " %s!\n", __func__);
2191 goto fail;
2192 }
2193
2194 if (data) {
2195 buf = kmemdup(data, size, GFP_ATOMIC);
2196 if (!buf) {
2197 netdev_err(dev->net, "Error allocating buffer"
2198 " in %s!\n", __func__);
2199 goto fail_free;
2200 }
2201 }
2202
2203 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
38673c82 2204 if (!req)
877bd862 2205 goto fail_free_buf;
877bd862
ML
2206
2207 req->bRequestType = reqtype;
2208 req->bRequest = cmd;
2209 req->wValue = cpu_to_le16(value);
2210 req->wIndex = cpu_to_le16(index);
2211 req->wLength = cpu_to_le16(size);
2212
2213 usb_fill_control_urb(urb, dev->udev,
2214 usb_sndctrlpipe(dev->udev, 0),
2215 (void *)req, buf, size,
2216 usbnet_async_cmd_cb, req);
2217 urb->transfer_flags |= URB_FREE_BUFFER;
2218
2219 err = usb_submit_urb(urb, GFP_ATOMIC);
2220 if (err < 0) {
2221 netdev_err(dev->net, "Error submitting the control"
2222 " message: status=%d\n", err);
2223 goto fail_free;
2224 }
2225 return 0;
2226
2227fail_free_buf:
2228 kfree(buf);
2229fail_free:
2230 kfree(req);
2231 usb_free_urb(urb);
2232fail:
2233 return err;
2234
2235}
2236EXPORT_SYMBOL_GPL(usbnet_write_cmd_async);
1da177e4
LT
2237/*-------------------------------------------------------------------------*/
2238
f29fc259 2239static int __init usbnet_init(void)
1da177e4 2240{
c582a950
TF
2241 /* Compiler should optimize this out. */
2242 BUILD_BUG_ON(
2243 FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
1da177e4 2244
c7e12ead 2245 eth_random_addr(node_id);
cb1cebbe 2246 return 0;
1da177e4 2247}
f29fc259 2248module_init(usbnet_init);
1da177e4 2249
f29fc259 2250static void __exit usbnet_exit(void)
1da177e4 2251{
1da177e4 2252}
f29fc259 2253module_exit(usbnet_exit);
1da177e4 2254
f29fc259 2255MODULE_AUTHOR("David Brownell");
090ffa9d 2256MODULE_DESCRIPTION("USB network driver framework");
f29fc259 2257MODULE_LICENSE("GPL");
This page took 1.238598 seconds and 5 git commands to generate.