Commit | Line | Data |
---|---|---|
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 | |
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 | |
090ffa9d DB |
23 | * kinds of full and high speed networking devices: host-to-host cables, |
24 | * smart usb peripherals, and actual Ethernet adapters. | |
1da177e4 | 25 | * |
090ffa9d DB |
26 | * 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 | */ | |
1da177e4 LT |
32 | |
33 | // #define DEBUG // error path messages, extra info | |
34 | // #define VERBOSE // more; success messages | |
35 | ||
1da177e4 | 36 | #include <linux/module.h> |
1da177e4 LT |
37 | #include <linux/init.h> |
38 | #include <linux/netdevice.h> | |
39 | #include <linux/etherdevice.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> |
f29fc259 DB |
45 | |
46 | #define DRIVER_VERSION "22-Aug-2005" | |
1da177e4 LT |
47 | |
48 | ||
49 | /*-------------------------------------------------------------------------*/ | |
50 | ||
51 | /* | |
52 | * Nineteen USB 1.1 max size bulk transactions per frame (ms), max. | |
53 | * Several dozen bytes of IPv4 data can fit in two such transactions. | |
54 | * One maximum size Ethernet packet takes twenty four of them. | |
55 | * For high speed, each frame comfortably fits almost 36 max size | |
56 | * Ethernet packets (so queues should be bigger). | |
f29fc259 DB |
57 | * |
58 | * REVISIT qlens should be members of 'struct usbnet'; the goal is to | |
59 | * let the USB host controller be busy for 5msec or more before an irq | |
60 | * is required, under load. Jumbograms change the equation. | |
1da177e4 | 61 | */ |
a99c1949 JP |
62 | #define RX_MAX_QUEUE_MEMORY (60 * 1518) |
63 | #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \ | |
64 | (RX_MAX_QUEUE_MEMORY/(dev)->rx_urb_size) : 4) | |
65 | #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? \ | |
66 | (RX_MAX_QUEUE_MEMORY/(dev)->hard_mtu) : 4) | |
1da177e4 | 67 | |
1da177e4 LT |
68 | // reawaken network queue this soon after stopping; else watchdog barks |
69 | #define TX_TIMEOUT_JIFFIES (5*HZ) | |
70 | ||
71 | // throttle rx/tx briefly after some faults, so khubd might disconnect() | |
72 | // us (it polls at HZ/4 usually) before we report too many false errors. | |
73 | #define THROTTLE_JIFFIES (HZ/8) | |
74 | ||
1da177e4 LT |
75 | // between wakeups |
76 | #define UNLINK_TIMEOUT_MS 3 | |
77 | ||
78 | /*-------------------------------------------------------------------------*/ | |
79 | ||
80 | // randomly generated ethernet address | |
81 | static u8 node_id [ETH_ALEN]; | |
82 | ||
1da177e4 LT |
83 | static const char driver_name [] = "usbnet"; |
84 | ||
85 | /* use ethtool to change the level for any given device */ | |
86 | static int msg_level = -1; | |
87 | module_param (msg_level, int, 0); | |
88 | MODULE_PARM_DESC (msg_level, "Override default message level"); | |
89 | ||
1da177e4 LT |
90 | /*-------------------------------------------------------------------------*/ |
91 | ||
1da177e4 | 92 | /* handles CDC Ethernet and many other network "bulk data" interfaces */ |
2e55cc72 | 93 | int usbnet_get_endpoints(struct usbnet *dev, struct usb_interface *intf) |
1da177e4 LT |
94 | { |
95 | int tmp; | |
96 | struct usb_host_interface *alt = NULL; | |
97 | struct usb_host_endpoint *in = NULL, *out = NULL; | |
98 | struct usb_host_endpoint *status = NULL; | |
99 | ||
100 | for (tmp = 0; tmp < intf->num_altsetting; tmp++) { | |
101 | unsigned ep; | |
102 | ||
103 | in = out = status = NULL; | |
104 | alt = intf->altsetting + tmp; | |
105 | ||
106 | /* take the first altsetting with in-bulk + out-bulk; | |
107 | * remember any status endpoint, just in case; | |
108 | * ignore other endpoints and altsetttings. | |
109 | */ | |
110 | for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) { | |
111 | struct usb_host_endpoint *e; | |
112 | int intr = 0; | |
113 | ||
114 | e = alt->endpoint + ep; | |
115 | switch (e->desc.bmAttributes) { | |
116 | case USB_ENDPOINT_XFER_INT: | |
fc6e2544 | 117 | if (!usb_endpoint_dir_in(&e->desc)) |
1da177e4 LT |
118 | continue; |
119 | intr = 1; | |
120 | /* FALLTHROUGH */ | |
121 | case USB_ENDPOINT_XFER_BULK: | |
122 | break; | |
123 | default: | |
124 | continue; | |
125 | } | |
fc6e2544 | 126 | if (usb_endpoint_dir_in(&e->desc)) { |
1da177e4 LT |
127 | if (!intr && !in) |
128 | in = e; | |
129 | else if (intr && !status) | |
130 | status = e; | |
131 | } else { | |
132 | if (!out) | |
133 | out = e; | |
134 | } | |
135 | } | |
136 | if (in && out) | |
137 | break; | |
138 | } | |
139 | if (!alt || !in || !out) | |
140 | return -EINVAL; | |
141 | ||
142 | if (alt->desc.bAlternateSetting != 0 | |
143 | || !(dev->driver_info->flags & FLAG_NO_SETINT)) { | |
144 | tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber, | |
145 | alt->desc.bAlternateSetting); | |
146 | if (tmp < 0) | |
147 | return tmp; | |
148 | } | |
cb1cebbe | 149 | |
1da177e4 LT |
150 | dev->in = usb_rcvbulkpipe (dev->udev, |
151 | in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); | |
152 | dev->out = usb_sndbulkpipe (dev->udev, | |
153 | out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); | |
154 | dev->status = status; | |
155 | return 0; | |
156 | } | |
2e55cc72 | 157 | EXPORT_SYMBOL_GPL(usbnet_get_endpoints); |
1da177e4 | 158 | |
7d12e780 | 159 | static void intr_complete (struct urb *urb); |
1da177e4 LT |
160 | |
161 | static int init_status (struct usbnet *dev, struct usb_interface *intf) | |
162 | { | |
163 | char *buf = NULL; | |
164 | unsigned pipe = 0; | |
165 | unsigned maxp; | |
166 | unsigned period; | |
167 | ||
168 | if (!dev->driver_info->status) | |
169 | return 0; | |
170 | ||
171 | pipe = usb_rcvintpipe (dev->udev, | |
172 | dev->status->desc.bEndpointAddress | |
173 | & USB_ENDPOINT_NUMBER_MASK); | |
174 | maxp = usb_maxpacket (dev->udev, pipe, 0); | |
175 | ||
176 | /* avoid 1 msec chatter: min 8 msec poll rate */ | |
177 | period = max ((int) dev->status->desc.bInterval, | |
178 | (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3); | |
179 | ||
e94b1766 | 180 | buf = kmalloc (maxp, GFP_KERNEL); |
1da177e4 | 181 | if (buf) { |
e94b1766 | 182 | dev->interrupt = usb_alloc_urb (0, GFP_KERNEL); |
1da177e4 LT |
183 | if (!dev->interrupt) { |
184 | kfree (buf); | |
185 | return -ENOMEM; | |
186 | } else { | |
187 | usb_fill_int_urb(dev->interrupt, dev->udev, pipe, | |
188 | buf, maxp, intr_complete, dev, period); | |
189 | dev_dbg(&intf->dev, | |
190 | "status ep%din, %d bytes period %d\n", | |
191 | usb_pipeendpoint(pipe), maxp, period); | |
192 | } | |
193 | } | |
18ab458f | 194 | return 0; |
1da177e4 LT |
195 | } |
196 | ||
2e55cc72 DB |
197 | /* Passes this packet up the stack, updating its accounting. |
198 | * Some link protocols batch packets, so their rx_fixup paths | |
199 | * can return clones as well as just modify the original skb. | |
200 | */ | |
201 | void usbnet_skb_return (struct usbnet *dev, struct sk_buff *skb) | |
1da177e4 LT |
202 | { |
203 | int status; | |
204 | ||
1da177e4 LT |
205 | skb->protocol = eth_type_trans (skb, dev->net); |
206 | dev->stats.rx_packets++; | |
207 | dev->stats.rx_bytes += skb->len; | |
208 | ||
209 | if (netif_msg_rx_status (dev)) | |
5330e927 | 210 | devdbg (dev, "< rx, len %zu, type 0x%x", |
1da177e4 LT |
211 | skb->len + sizeof (struct ethhdr), skb->protocol); |
212 | memset (skb->cb, 0, sizeof (struct skb_data)); | |
213 | status = netif_rx (skb); | |
214 | if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev)) | |
215 | devdbg (dev, "netif_rx status %d", status); | |
216 | } | |
2e55cc72 | 217 | EXPORT_SYMBOL_GPL(usbnet_skb_return); |
1da177e4 | 218 | |
1da177e4 LT |
219 | \f |
220 | /*------------------------------------------------------------------------- | |
221 | * | |
222 | * Network Device Driver (peer link to "Host Device", from USB host) | |
223 | * | |
224 | *-------------------------------------------------------------------------*/ | |
225 | ||
226 | static int usbnet_change_mtu (struct net_device *net, int new_mtu) | |
227 | { | |
228 | struct usbnet *dev = netdev_priv(net); | |
f29fc259 | 229 | int ll_mtu = new_mtu + net->hard_header_len; |
a99c1949 JP |
230 | int old_hard_mtu = dev->hard_mtu; |
231 | int old_rx_urb_size = dev->rx_urb_size; | |
1da177e4 | 232 | |
a99c1949 | 233 | if (new_mtu <= 0) |
1da177e4 | 234 | return -EINVAL; |
1da177e4 | 235 | // no second zero-length packet read wanted after mtu-sized packets |
f29fc259 | 236 | if ((ll_mtu % dev->maxpacket) == 0) |
1da177e4 LT |
237 | return -EDOM; |
238 | net->mtu = new_mtu; | |
a99c1949 JP |
239 | |
240 | dev->hard_mtu = net->mtu + net->hard_header_len; | |
241 | if (dev->rx_urb_size == old_hard_mtu) { | |
242 | dev->rx_urb_size = dev->hard_mtu; | |
243 | if (dev->rx_urb_size > old_rx_urb_size) | |
244 | usbnet_unlink_rx_urbs(dev); | |
245 | } | |
246 | ||
1da177e4 LT |
247 | return 0; |
248 | } | |
249 | ||
250 | /*-------------------------------------------------------------------------*/ | |
251 | ||
1da177e4 LT |
252 | /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from |
253 | * completion callbacks. 2.5 should have fixed those bugs... | |
254 | */ | |
255 | ||
8728b834 | 256 | static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list) |
1da177e4 | 257 | { |
1da177e4 LT |
258 | unsigned long flags; |
259 | ||
8728b834 DM |
260 | spin_lock_irqsave(&list->lock, flags); |
261 | __skb_unlink(skb, list); | |
262 | spin_unlock(&list->lock); | |
263 | spin_lock(&dev->done.lock); | |
264 | __skb_queue_tail(&dev->done, skb); | |
1da177e4 | 265 | if (dev->done.qlen == 1) |
8728b834 DM |
266 | tasklet_schedule(&dev->bh); |
267 | spin_unlock_irqrestore(&dev->done.lock, flags); | |
1da177e4 LT |
268 | } |
269 | ||
270 | /* some work can't be done in tasklets, so we use keventd | |
271 | * | |
272 | * NOTE: annoying asymmetry: if it's active, schedule_work() fails, | |
273 | * but tasklet_schedule() doesn't. hope the failure is rare. | |
274 | */ | |
2e55cc72 | 275 | void usbnet_defer_kevent (struct usbnet *dev, int work) |
1da177e4 LT |
276 | { |
277 | set_bit (work, &dev->flags); | |
278 | if (!schedule_work (&dev->kevent)) | |
279 | deverr (dev, "kevent %d may have been dropped", work); | |
280 | else | |
281 | devdbg (dev, "kevent %d scheduled", work); | |
282 | } | |
2e55cc72 | 283 | EXPORT_SYMBOL_GPL(usbnet_defer_kevent); |
1da177e4 LT |
284 | |
285 | /*-------------------------------------------------------------------------*/ | |
286 | ||
7d12e780 | 287 | static void rx_complete (struct urb *urb); |
1da177e4 | 288 | |
55016f10 | 289 | static void rx_submit (struct usbnet *dev, struct urb *urb, gfp_t flags) |
1da177e4 LT |
290 | { |
291 | struct sk_buff *skb; | |
292 | struct skb_data *entry; | |
293 | int retval = 0; | |
294 | unsigned long lockflags; | |
2e55cc72 | 295 | size_t size = dev->rx_urb_size; |
1da177e4 | 296 | |
1da177e4 LT |
297 | if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) { |
298 | if (netif_msg_rx_err (dev)) | |
299 | devdbg (dev, "no rx skb"); | |
2e55cc72 | 300 | usbnet_defer_kevent (dev, EVENT_RX_MEMORY); |
1da177e4 LT |
301 | usb_free_urb (urb); |
302 | return; | |
303 | } | |
304 | skb_reserve (skb, NET_IP_ALIGN); | |
305 | ||
306 | entry = (struct skb_data *) skb->cb; | |
307 | entry->urb = urb; | |
308 | entry->dev = dev; | |
309 | entry->state = rx_start; | |
310 | entry->length = 0; | |
311 | ||
312 | usb_fill_bulk_urb (urb, dev->udev, dev->in, | |
313 | skb->data, size, rx_complete, skb); | |
1da177e4 LT |
314 | |
315 | spin_lock_irqsave (&dev->rxq.lock, lockflags); | |
316 | ||
317 | if (netif_running (dev->net) | |
318 | && netif_device_present (dev->net) | |
319 | && !test_bit (EVENT_RX_HALT, &dev->flags)) { | |
18ab458f | 320 | switch (retval = usb_submit_urb (urb, GFP_ATOMIC)) { |
1da177e4 | 321 | case -EPIPE: |
2e55cc72 | 322 | usbnet_defer_kevent (dev, EVENT_RX_HALT); |
1da177e4 LT |
323 | break; |
324 | case -ENOMEM: | |
2e55cc72 | 325 | usbnet_defer_kevent (dev, EVENT_RX_MEMORY); |
1da177e4 LT |
326 | break; |
327 | case -ENODEV: | |
328 | if (netif_msg_ifdown (dev)) | |
329 | devdbg (dev, "device gone"); | |
330 | netif_device_detach (dev->net); | |
331 | break; | |
332 | default: | |
333 | if (netif_msg_rx_err (dev)) | |
334 | devdbg (dev, "rx submit, %d", retval); | |
335 | tasklet_schedule (&dev->bh); | |
336 | break; | |
337 | case 0: | |
338 | __skb_queue_tail (&dev->rxq, skb); | |
339 | } | |
340 | } else { | |
341 | if (netif_msg_ifdown (dev)) | |
342 | devdbg (dev, "rx: stopped"); | |
343 | retval = -ENOLINK; | |
344 | } | |
345 | spin_unlock_irqrestore (&dev->rxq.lock, lockflags); | |
346 | if (retval) { | |
347 | dev_kfree_skb_any (skb); | |
348 | usb_free_urb (urb); | |
349 | } | |
350 | } | |
351 | ||
352 | ||
353 | /*-------------------------------------------------------------------------*/ | |
354 | ||
355 | static inline void rx_process (struct usbnet *dev, struct sk_buff *skb) | |
356 | { | |
357 | if (dev->driver_info->rx_fixup | |
358 | && !dev->driver_info->rx_fixup (dev, skb)) | |
359 | goto error; | |
360 | // else network stack removes extra byte if we forced a short packet | |
361 | ||
362 | if (skb->len) | |
2e55cc72 | 363 | usbnet_skb_return (dev, skb); |
1da177e4 LT |
364 | else { |
365 | if (netif_msg_rx_err (dev)) | |
366 | devdbg (dev, "drop"); | |
367 | error: | |
368 | dev->stats.rx_errors++; | |
369 | skb_queue_tail (&dev->done, skb); | |
370 | } | |
371 | } | |
372 | ||
373 | /*-------------------------------------------------------------------------*/ | |
374 | ||
7d12e780 | 375 | static void rx_complete (struct urb *urb) |
1da177e4 LT |
376 | { |
377 | struct sk_buff *skb = (struct sk_buff *) urb->context; | |
378 | struct skb_data *entry = (struct skb_data *) skb->cb; | |
379 | struct usbnet *dev = entry->dev; | |
380 | int urb_status = urb->status; | |
381 | ||
382 | skb_put (skb, urb->actual_length); | |
383 | entry->state = rx_done; | |
384 | entry->urb = NULL; | |
385 | ||
386 | switch (urb_status) { | |
18ab458f DB |
387 | /* success */ |
388 | case 0: | |
f29fc259 | 389 | if (skb->len < dev->net->hard_header_len) { |
1da177e4 LT |
390 | entry->state = rx_cleanup; |
391 | dev->stats.rx_errors++; | |
392 | dev->stats.rx_length_errors++; | |
393 | if (netif_msg_rx_err (dev)) | |
394 | devdbg (dev, "rx length %d", skb->len); | |
395 | } | |
396 | break; | |
397 | ||
18ab458f DB |
398 | /* stalls need manual reset. this is rare ... except that |
399 | * when going through USB 2.0 TTs, unplug appears this way. | |
400 | * we avoid the highspeed version of the ETIMEOUT/EILSEQ | |
401 | * storm, recovering as needed. | |
402 | */ | |
403 | case -EPIPE: | |
1da177e4 | 404 | dev->stats.rx_errors++; |
2e55cc72 | 405 | usbnet_defer_kevent (dev, EVENT_RX_HALT); |
1da177e4 LT |
406 | // FALLTHROUGH |
407 | ||
18ab458f DB |
408 | /* software-driven interface shutdown */ |
409 | case -ECONNRESET: /* async unlink */ | |
410 | case -ESHUTDOWN: /* hardware gone */ | |
1da177e4 LT |
411 | if (netif_msg_ifdown (dev)) |
412 | devdbg (dev, "rx shutdown, code %d", urb_status); | |
413 | goto block; | |
414 | ||
18ab458f DB |
415 | /* we get controller i/o faults during khubd disconnect() delays. |
416 | * throttle down resubmits, to avoid log floods; just temporarily, | |
417 | * so we still recover when the fault isn't a khubd delay. | |
418 | */ | |
419 | case -EPROTO: | |
420 | case -ETIME: | |
421 | case -EILSEQ: | |
1da177e4 LT |
422 | dev->stats.rx_errors++; |
423 | if (!timer_pending (&dev->delay)) { | |
424 | mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES); | |
425 | if (netif_msg_link (dev)) | |
426 | devdbg (dev, "rx throttle %d", urb_status); | |
427 | } | |
428 | block: | |
429 | entry->state = rx_cleanup; | |
430 | entry->urb = urb; | |
431 | urb = NULL; | |
432 | break; | |
433 | ||
18ab458f DB |
434 | /* data overrun ... flush fifo? */ |
435 | case -EOVERFLOW: | |
1da177e4 LT |
436 | dev->stats.rx_over_errors++; |
437 | // FALLTHROUGH | |
cb1cebbe | 438 | |
18ab458f | 439 | default: |
1da177e4 LT |
440 | entry->state = rx_cleanup; |
441 | dev->stats.rx_errors++; | |
442 | if (netif_msg_rx_err (dev)) | |
443 | devdbg (dev, "rx status %d", urb_status); | |
444 | break; | |
445 | } | |
446 | ||
8728b834 | 447 | defer_bh(dev, skb, &dev->rxq); |
1da177e4 LT |
448 | |
449 | if (urb) { | |
450 | if (netif_running (dev->net) | |
451 | && !test_bit (EVENT_RX_HALT, &dev->flags)) { | |
452 | rx_submit (dev, urb, GFP_ATOMIC); | |
453 | return; | |
454 | } | |
455 | usb_free_urb (urb); | |
456 | } | |
457 | if (netif_msg_rx_err (dev)) | |
458 | devdbg (dev, "no read resubmitted"); | |
459 | } | |
460 | ||
7d12e780 | 461 | static void intr_complete (struct urb *urb) |
1da177e4 LT |
462 | { |
463 | struct usbnet *dev = urb->context; | |
464 | int status = urb->status; | |
465 | ||
466 | switch (status) { | |
18ab458f DB |
467 | /* success */ |
468 | case 0: | |
1da177e4 LT |
469 | dev->driver_info->status(dev, urb); |
470 | break; | |
471 | ||
18ab458f DB |
472 | /* software-driven interface shutdown */ |
473 | case -ENOENT: /* urb killed */ | |
474 | case -ESHUTDOWN: /* hardware gone */ | |
1da177e4 LT |
475 | if (netif_msg_ifdown (dev)) |
476 | devdbg (dev, "intr shutdown, code %d", status); | |
477 | return; | |
478 | ||
18ab458f DB |
479 | /* NOTE: not throttling like RX/TX, since this endpoint |
480 | * already polls infrequently | |
481 | */ | |
482 | default: | |
1da177e4 LT |
483 | devdbg (dev, "intr status %d", status); |
484 | break; | |
485 | } | |
486 | ||
487 | if (!netif_running (dev->net)) | |
488 | return; | |
489 | ||
490 | memset(urb->transfer_buffer, 0, urb->transfer_buffer_length); | |
491 | status = usb_submit_urb (urb, GFP_ATOMIC); | |
492 | if (status != 0 && netif_msg_timer (dev)) | |
493 | deverr(dev, "intr resubmit --> %d", status); | |
494 | } | |
495 | ||
496 | /*-------------------------------------------------------------------------*/ | |
497 | ||
498 | // unlink pending rx/tx; completion handlers do all other cleanup | |
499 | ||
500 | static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q) | |
501 | { | |
502 | unsigned long flags; | |
503 | struct sk_buff *skb, *skbnext; | |
504 | int count = 0; | |
505 | ||
506 | spin_lock_irqsave (&q->lock, flags); | |
83bfba5f | 507 | skb_queue_walk_safe(q, skb, skbnext) { |
1da177e4 LT |
508 | struct skb_data *entry; |
509 | struct urb *urb; | |
510 | int retval; | |
511 | ||
512 | entry = (struct skb_data *) skb->cb; | |
513 | urb = entry->urb; | |
1da177e4 LT |
514 | |
515 | // during some PM-driven resume scenarios, | |
516 | // these (async) unlinks complete immediately | |
517 | retval = usb_unlink_urb (urb); | |
518 | if (retval != -EINPROGRESS && retval != 0) | |
519 | devdbg (dev, "unlink urb err, %d", retval); | |
520 | else | |
521 | count++; | |
522 | } | |
523 | spin_unlock_irqrestore (&q->lock, flags); | |
524 | return count; | |
525 | } | |
526 | ||
a99c1949 JP |
527 | // Flush all pending rx urbs |
528 | // minidrivers may need to do this when the MTU changes | |
529 | ||
530 | void usbnet_unlink_rx_urbs(struct usbnet *dev) | |
531 | { | |
532 | if (netif_running(dev->net)) { | |
533 | (void) unlink_urbs (dev, &dev->rxq); | |
534 | tasklet_schedule(&dev->bh); | |
535 | } | |
536 | } | |
537 | EXPORT_SYMBOL_GPL(usbnet_unlink_rx_urbs); | |
1da177e4 LT |
538 | |
539 | /*-------------------------------------------------------------------------*/ | |
540 | ||
541 | // precondition: never called in_interrupt | |
542 | ||
543 | static int usbnet_stop (struct net_device *net) | |
544 | { | |
545 | struct usbnet *dev = netdev_priv(net); | |
546 | int temp; | |
7259f0d0 | 547 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK (unlink_wakeup); |
1da177e4 LT |
548 | DECLARE_WAITQUEUE (wait, current); |
549 | ||
550 | netif_stop_queue (net); | |
551 | ||
552 | if (netif_msg_ifdown (dev)) | |
553 | devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld", | |
cb1cebbe | 554 | dev->stats.rx_packets, dev->stats.tx_packets, |
1da177e4 LT |
555 | dev->stats.rx_errors, dev->stats.tx_errors |
556 | ); | |
557 | ||
558 | // ensure there are no more active urbs | |
559 | add_wait_queue (&unlink_wakeup, &wait); | |
560 | dev->wait = &unlink_wakeup; | |
561 | temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq); | |
562 | ||
563 | // maybe wait for deletions to finish. | |
18ab458f DB |
564 | while (!skb_queue_empty(&dev->rxq) |
565 | && !skb_queue_empty(&dev->txq) | |
566 | && !skb_queue_empty(&dev->done)) { | |
1da177e4 LT |
567 | msleep(UNLINK_TIMEOUT_MS); |
568 | if (netif_msg_ifdown (dev)) | |
569 | devdbg (dev, "waited for %d urb completions", temp); | |
570 | } | |
571 | dev->wait = NULL; | |
cb1cebbe | 572 | remove_wait_queue (&unlink_wakeup, &wait); |
1da177e4 LT |
573 | |
574 | usb_kill_urb(dev->interrupt); | |
575 | ||
576 | /* deferred work (task, timer, softirq) must also stop. | |
577 | * can't flush_scheduled_work() until we drop rtnl (later), | |
578 | * else workers could deadlock; so make workers a NOP. | |
579 | */ | |
580 | dev->flags = 0; | |
581 | del_timer_sync (&dev->delay); | |
582 | tasklet_kill (&dev->bh); | |
a11a6544 | 583 | usb_autopm_put_interface(dev->intf); |
1da177e4 LT |
584 | |
585 | return 0; | |
586 | } | |
587 | ||
588 | /*-------------------------------------------------------------------------*/ | |
589 | ||
590 | // posts reads, and enables write queuing | |
591 | ||
592 | // precondition: never called in_interrupt | |
593 | ||
594 | static int usbnet_open (struct net_device *net) | |
595 | { | |
596 | struct usbnet *dev = netdev_priv(net); | |
a11a6544 | 597 | int retval; |
1da177e4 LT |
598 | struct driver_info *info = dev->driver_info; |
599 | ||
a11a6544 ON |
600 | if ((retval = usb_autopm_get_interface(dev->intf)) < 0) { |
601 | if (netif_msg_ifup (dev)) | |
602 | devinfo (dev, | |
603 | "resumption fail (%d) usbnet usb-%s-%s, %s", | |
604 | retval, | |
605 | dev->udev->bus->bus_name, dev->udev->devpath, | |
606 | info->description); | |
607 | goto done_nopm; | |
608 | } | |
609 | ||
1da177e4 LT |
610 | // put into "known safe" state |
611 | if (info->reset && (retval = info->reset (dev)) < 0) { | |
612 | if (netif_msg_ifup (dev)) | |
613 | devinfo (dev, | |
614 | "open reset fail (%d) usbnet usb-%s-%s, %s", | |
615 | retval, | |
616 | dev->udev->bus->bus_name, dev->udev->devpath, | |
617 | info->description); | |
618 | goto done; | |
619 | } | |
620 | ||
621 | // insist peer be connected | |
622 | if (info->check_connect && (retval = info->check_connect (dev)) < 0) { | |
623 | if (netif_msg_ifup (dev)) | |
624 | devdbg (dev, "can't open; %d", retval); | |
625 | goto done; | |
626 | } | |
627 | ||
628 | /* start any status interrupt transfer */ | |
629 | if (dev->interrupt) { | |
630 | retval = usb_submit_urb (dev->interrupt, GFP_KERNEL); | |
631 | if (retval < 0) { | |
632 | if (netif_msg_ifup (dev)) | |
633 | deverr (dev, "intr submit %d", retval); | |
634 | goto done; | |
635 | } | |
636 | } | |
637 | ||
638 | netif_start_queue (net); | |
639 | if (netif_msg_ifup (dev)) { | |
640 | char *framing; | |
641 | ||
642 | if (dev->driver_info->flags & FLAG_FRAMING_NC) | |
643 | framing = "NetChip"; | |
644 | else if (dev->driver_info->flags & FLAG_FRAMING_GL) | |
645 | framing = "GeneSys"; | |
646 | else if (dev->driver_info->flags & FLAG_FRAMING_Z) | |
647 | framing = "Zaurus"; | |
648 | else if (dev->driver_info->flags & FLAG_FRAMING_RN) | |
649 | framing = "RNDIS"; | |
650 | else if (dev->driver_info->flags & FLAG_FRAMING_AX) | |
651 | framing = "ASIX"; | |
652 | else | |
653 | framing = "simple"; | |
654 | ||
655 | devinfo (dev, "open: enable queueing " | |
656 | "(rx %d, tx %d) mtu %d %s framing", | |
25d94e68 | 657 | (int)RX_QLEN (dev), (int)TX_QLEN (dev), dev->net->mtu, |
1da177e4 LT |
658 | framing); |
659 | } | |
660 | ||
661 | // delay posting reads until we're fully open | |
662 | tasklet_schedule (&dev->bh); | |
a11a6544 | 663 | return retval; |
1da177e4 | 664 | done: |
a11a6544 ON |
665 | usb_autopm_put_interface(dev->intf); |
666 | done_nopm: | |
1da177e4 LT |
667 | return retval; |
668 | } | |
669 | ||
670 | /*-------------------------------------------------------------------------*/ | |
671 | ||
2e55cc72 DB |
672 | /* ethtool methods; minidrivers may need to add some more, but |
673 | * they'll probably want to use this base set. | |
674 | */ | |
675 | ||
c41286fd AB |
676 | int usbnet_get_settings (struct net_device *net, struct ethtool_cmd *cmd) |
677 | { | |
678 | struct usbnet *dev = netdev_priv(net); | |
679 | ||
680 | if (!dev->mii.mdio_read) | |
681 | return -EOPNOTSUPP; | |
682 | ||
683 | return mii_ethtool_gset(&dev->mii, cmd); | |
684 | } | |
685 | EXPORT_SYMBOL_GPL(usbnet_get_settings); | |
686 | ||
687 | int usbnet_set_settings (struct net_device *net, struct ethtool_cmd *cmd) | |
688 | { | |
689 | struct usbnet *dev = netdev_priv(net); | |
690 | int retval; | |
691 | ||
692 | if (!dev->mii.mdio_write) | |
693 | return -EOPNOTSUPP; | |
694 | ||
695 | retval = mii_ethtool_sset(&dev->mii, cmd); | |
696 | ||
697 | /* link speed/duplex might have changed */ | |
698 | if (dev->driver_info->link_reset) | |
699 | dev->driver_info->link_reset(dev); | |
700 | ||
701 | return retval; | |
702 | ||
703 | } | |
704 | EXPORT_SYMBOL_GPL(usbnet_set_settings); | |
705 | ||
c41286fd | 706 | u32 usbnet_get_link (struct net_device *net) |
1da177e4 LT |
707 | { |
708 | struct usbnet *dev = netdev_priv(net); | |
709 | ||
f29fc259 | 710 | /* If a check_connect is defined, return its result */ |
1da177e4 LT |
711 | if (dev->driver_info->check_connect) |
712 | return dev->driver_info->check_connect (dev) == 0; | |
713 | ||
c41286fd AB |
714 | /* if the device has mii operations, use those */ |
715 | if (dev->mii.mdio_read) | |
716 | return mii_link_ok(&dev->mii); | |
717 | ||
05ffb3e2 BM |
718 | /* Otherwise, dtrt for drivers calling netif_carrier_{on,off} */ |
719 | return ethtool_op_get_link(net); | |
1da177e4 | 720 | } |
c41286fd | 721 | EXPORT_SYMBOL_GPL(usbnet_get_link); |
1da177e4 | 722 | |
18ee91fa | 723 | int usbnet_nway_reset(struct net_device *net) |
1da177e4 LT |
724 | { |
725 | struct usbnet *dev = netdev_priv(net); | |
726 | ||
18ee91fa DB |
727 | if (!dev->mii.mdio_write) |
728 | return -EOPNOTSUPP; | |
729 | ||
730 | return mii_nway_restart(&dev->mii); | |
1da177e4 | 731 | } |
18ee91fa | 732 | EXPORT_SYMBOL_GPL(usbnet_nway_reset); |
1da177e4 | 733 | |
18ee91fa | 734 | void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info) |
1da177e4 LT |
735 | { |
736 | struct usbnet *dev = netdev_priv(net); | |
737 | ||
296c0242 | 738 | strncpy (info->driver, dev->driver_name, sizeof info->driver); |
18ee91fa DB |
739 | strncpy (info->version, DRIVER_VERSION, sizeof info->version); |
740 | strncpy (info->fw_version, dev->driver_info->description, | |
741 | sizeof info->fw_version); | |
742 | usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info); | |
1da177e4 | 743 | } |
18ee91fa | 744 | EXPORT_SYMBOL_GPL(usbnet_get_drvinfo); |
1da177e4 | 745 | |
18ee91fa | 746 | u32 usbnet_get_msglevel (struct net_device *net) |
c41286fd AB |
747 | { |
748 | struct usbnet *dev = netdev_priv(net); | |
749 | ||
18ee91fa DB |
750 | return dev->msg_enable; |
751 | } | |
752 | EXPORT_SYMBOL_GPL(usbnet_get_msglevel); | |
c41286fd | 753 | |
18ee91fa DB |
754 | void usbnet_set_msglevel (struct net_device *net, u32 level) |
755 | { | |
756 | struct usbnet *dev = netdev_priv(net); | |
757 | ||
758 | dev->msg_enable = level; | |
c41286fd | 759 | } |
18ee91fa | 760 | EXPORT_SYMBOL_GPL(usbnet_set_msglevel); |
c41286fd | 761 | |
2e55cc72 DB |
762 | /* drivers may override default ethtool_ops in their bind() routine */ |
763 | static struct ethtool_ops usbnet_ethtool_ops = { | |
c41286fd AB |
764 | .get_settings = usbnet_get_settings, |
765 | .set_settings = usbnet_set_settings, | |
2e55cc72 | 766 | .get_link = usbnet_get_link, |
c41286fd | 767 | .nway_reset = usbnet_nway_reset, |
18ee91fa | 768 | .get_drvinfo = usbnet_get_drvinfo, |
2e55cc72 DB |
769 | .get_msglevel = usbnet_get_msglevel, |
770 | .set_msglevel = usbnet_set_msglevel, | |
771 | }; | |
1da177e4 LT |
772 | |
773 | /*-------------------------------------------------------------------------*/ | |
774 | ||
775 | /* work that cannot be done in interrupt context uses keventd. | |
776 | * | |
777 | * NOTE: with 2.5 we could do more of this using completion callbacks, | |
778 | * especially now that control transfers can be queued. | |
779 | */ | |
780 | static void | |
c4028958 | 781 | kevent (struct work_struct *work) |
1da177e4 | 782 | { |
c4028958 DH |
783 | struct usbnet *dev = |
784 | container_of(work, struct usbnet, kevent); | |
1da177e4 LT |
785 | int status; |
786 | ||
787 | /* usb_clear_halt() needs a thread context */ | |
788 | if (test_bit (EVENT_TX_HALT, &dev->flags)) { | |
789 | unlink_urbs (dev, &dev->txq); | |
790 | status = usb_clear_halt (dev->udev, dev->out); | |
f29fc259 DB |
791 | if (status < 0 |
792 | && status != -EPIPE | |
793 | && status != -ESHUTDOWN) { | |
1da177e4 LT |
794 | if (netif_msg_tx_err (dev)) |
795 | deverr (dev, "can't clear tx halt, status %d", | |
796 | status); | |
797 | } else { | |
798 | clear_bit (EVENT_TX_HALT, &dev->flags); | |
f29fc259 DB |
799 | if (status != -ESHUTDOWN) |
800 | netif_wake_queue (dev->net); | |
1da177e4 LT |
801 | } |
802 | } | |
803 | if (test_bit (EVENT_RX_HALT, &dev->flags)) { | |
804 | unlink_urbs (dev, &dev->rxq); | |
805 | status = usb_clear_halt (dev->udev, dev->in); | |
f29fc259 DB |
806 | if (status < 0 |
807 | && status != -EPIPE | |
808 | && status != -ESHUTDOWN) { | |
1da177e4 LT |
809 | if (netif_msg_rx_err (dev)) |
810 | deverr (dev, "can't clear rx halt, status %d", | |
811 | status); | |
812 | } else { | |
813 | clear_bit (EVENT_RX_HALT, &dev->flags); | |
814 | tasklet_schedule (&dev->bh); | |
815 | } | |
816 | } | |
817 | ||
818 | /* tasklet could resubmit itself forever if memory is tight */ | |
819 | if (test_bit (EVENT_RX_MEMORY, &dev->flags)) { | |
820 | struct urb *urb = NULL; | |
821 | ||
822 | if (netif_running (dev->net)) | |
823 | urb = usb_alloc_urb (0, GFP_KERNEL); | |
824 | else | |
825 | clear_bit (EVENT_RX_MEMORY, &dev->flags); | |
826 | if (urb != NULL) { | |
827 | clear_bit (EVENT_RX_MEMORY, &dev->flags); | |
828 | rx_submit (dev, urb, GFP_KERNEL); | |
829 | tasklet_schedule (&dev->bh); | |
830 | } | |
831 | } | |
832 | ||
7ea13c9c | 833 | if (test_bit (EVENT_LINK_RESET, &dev->flags)) { |
cb1cebbe | 834 | struct driver_info *info = dev->driver_info; |
7ea13c9c DH |
835 | int retval = 0; |
836 | ||
837 | clear_bit (EVENT_LINK_RESET, &dev->flags); | |
838 | if(info->link_reset && (retval = info->link_reset(dev)) < 0) { | |
839 | devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s", | |
840 | retval, | |
841 | dev->udev->bus->bus_name, dev->udev->devpath, | |
842 | info->description); | |
843 | } | |
844 | } | |
845 | ||
1da177e4 LT |
846 | if (dev->flags) |
847 | devdbg (dev, "kevent done, flags = 0x%lx", | |
848 | dev->flags); | |
849 | } | |
850 | ||
851 | /*-------------------------------------------------------------------------*/ | |
852 | ||
7d12e780 | 853 | static void tx_complete (struct urb *urb) |
1da177e4 LT |
854 | { |
855 | struct sk_buff *skb = (struct sk_buff *) urb->context; | |
856 | struct skb_data *entry = (struct skb_data *) skb->cb; | |
857 | struct usbnet *dev = entry->dev; | |
858 | ||
859 | if (urb->status == 0) { | |
860 | dev->stats.tx_packets++; | |
861 | dev->stats.tx_bytes += entry->length; | |
862 | } else { | |
863 | dev->stats.tx_errors++; | |
864 | ||
865 | switch (urb->status) { | |
866 | case -EPIPE: | |
2e55cc72 | 867 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
1da177e4 LT |
868 | break; |
869 | ||
870 | /* software-driven interface shutdown */ | |
871 | case -ECONNRESET: // async unlink | |
872 | case -ESHUTDOWN: // hardware gone | |
873 | break; | |
874 | ||
875 | // like rx, tx gets controller i/o faults during khubd delays | |
876 | // and so it uses the same throttling mechanism. | |
38e2bfc9 PZ |
877 | case -EPROTO: |
878 | case -ETIME: | |
879 | case -EILSEQ: | |
1da177e4 LT |
880 | if (!timer_pending (&dev->delay)) { |
881 | mod_timer (&dev->delay, | |
882 | jiffies + THROTTLE_JIFFIES); | |
883 | if (netif_msg_link (dev)) | |
884 | devdbg (dev, "tx throttle %d", | |
885 | urb->status); | |
886 | } | |
887 | netif_stop_queue (dev->net); | |
888 | break; | |
889 | default: | |
890 | if (netif_msg_tx_err (dev)) | |
891 | devdbg (dev, "tx err %d", entry->urb->status); | |
892 | break; | |
893 | } | |
894 | } | |
895 | ||
896 | urb->dev = NULL; | |
897 | entry->state = tx_done; | |
8728b834 | 898 | defer_bh(dev, skb, &dev->txq); |
1da177e4 LT |
899 | } |
900 | ||
901 | /*-------------------------------------------------------------------------*/ | |
902 | ||
903 | static void usbnet_tx_timeout (struct net_device *net) | |
904 | { | |
905 | struct usbnet *dev = netdev_priv(net); | |
906 | ||
907 | unlink_urbs (dev, &dev->txq); | |
908 | tasklet_schedule (&dev->bh); | |
909 | ||
910 | // FIXME: device recovery -- reset? | |
911 | } | |
912 | ||
913 | /*-------------------------------------------------------------------------*/ | |
914 | ||
915 | static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net) | |
916 | { | |
917 | struct usbnet *dev = netdev_priv(net); | |
918 | int length; | |
919 | int retval = NET_XMIT_SUCCESS; | |
920 | struct urb *urb = NULL; | |
921 | struct skb_data *entry; | |
922 | struct driver_info *info = dev->driver_info; | |
923 | unsigned long flags; | |
1da177e4 LT |
924 | |
925 | // some devices want funky USB-level framing, for | |
926 | // win32 driver (usually) and/or hardware quirks | |
927 | if (info->tx_fixup) { | |
928 | skb = info->tx_fixup (dev, skb, GFP_ATOMIC); | |
929 | if (!skb) { | |
930 | if (netif_msg_tx_err (dev)) | |
931 | devdbg (dev, "can't tx_fixup skb"); | |
932 | goto drop; | |
933 | } | |
934 | } | |
935 | length = skb->len; | |
936 | ||
937 | if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) { | |
938 | if (netif_msg_tx_err (dev)) | |
939 | devdbg (dev, "no urb"); | |
940 | goto drop; | |
941 | } | |
942 | ||
943 | entry = (struct skb_data *) skb->cb; | |
944 | entry->urb = urb; | |
945 | entry->dev = dev; | |
946 | entry->state = tx_start; | |
947 | entry->length = length; | |
948 | ||
1da177e4 LT |
949 | usb_fill_bulk_urb (urb, dev->udev, dev->out, |
950 | skb->data, skb->len, tx_complete, skb); | |
1da177e4 LT |
951 | |
952 | /* don't assume the hardware handles USB_ZERO_PACKET | |
953 | * NOTE: strictly conforming cdc-ether devices should expect | |
954 | * the ZLP here, but ignore the one-byte packet. | |
1da177e4 | 955 | */ |
3e323f3e | 956 | if ((length % dev->maxpacket) == 0) { |
1da177e4 | 957 | urb->transfer_buffer_length++; |
3e323f3e PK |
958 | if (skb_tailroom(skb)) { |
959 | skb->data[skb->len] = 0; | |
960 | __skb_put(skb, 1); | |
961 | } | |
962 | } | |
1da177e4 LT |
963 | |
964 | spin_lock_irqsave (&dev->txq.lock, flags); | |
965 | ||
1da177e4 LT |
966 | switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) { |
967 | case -EPIPE: | |
968 | netif_stop_queue (net); | |
2e55cc72 | 969 | usbnet_defer_kevent (dev, EVENT_TX_HALT); |
1da177e4 LT |
970 | break; |
971 | default: | |
972 | if (netif_msg_tx_err (dev)) | |
973 | devdbg (dev, "tx: submit urb err %d", retval); | |
974 | break; | |
975 | case 0: | |
976 | net->trans_start = jiffies; | |
977 | __skb_queue_tail (&dev->txq, skb); | |
978 | if (dev->txq.qlen >= TX_QLEN (dev)) | |
979 | netif_stop_queue (net); | |
980 | } | |
981 | spin_unlock_irqrestore (&dev->txq.lock, flags); | |
982 | ||
983 | if (retval) { | |
984 | if (netif_msg_tx_err (dev)) | |
985 | devdbg (dev, "drop, code %d", retval); | |
986 | drop: | |
987 | retval = NET_XMIT_SUCCESS; | |
988 | dev->stats.tx_dropped++; | |
989 | if (skb) | |
990 | dev_kfree_skb_any (skb); | |
991 | usb_free_urb (urb); | |
992 | } else if (netif_msg_tx_queued (dev)) { | |
993 | devdbg (dev, "> tx, len %d, type 0x%x", | |
994 | length, skb->protocol); | |
995 | } | |
996 | return retval; | |
997 | } | |
998 | ||
999 | ||
1000 | /*-------------------------------------------------------------------------*/ | |
1001 | ||
1002 | // tasklet (work deferred from completions, in_irq) or timer | |
1003 | ||
1004 | static void usbnet_bh (unsigned long param) | |
1005 | { | |
1006 | struct usbnet *dev = (struct usbnet *) param; | |
1007 | struct sk_buff *skb; | |
1008 | struct skb_data *entry; | |
1009 | ||
1010 | while ((skb = skb_dequeue (&dev->done))) { | |
1011 | entry = (struct skb_data *) skb->cb; | |
1012 | switch (entry->state) { | |
18ab458f | 1013 | case rx_done: |
1da177e4 LT |
1014 | entry->state = rx_cleanup; |
1015 | rx_process (dev, skb); | |
1016 | continue; | |
18ab458f DB |
1017 | case tx_done: |
1018 | case rx_cleanup: | |
1da177e4 LT |
1019 | usb_free_urb (entry->urb); |
1020 | dev_kfree_skb (skb); | |
1021 | continue; | |
18ab458f | 1022 | default: |
1da177e4 LT |
1023 | devdbg (dev, "bogus skb state %d", entry->state); |
1024 | } | |
1025 | } | |
1026 | ||
1027 | // waiting for all pending urbs to complete? | |
1028 | if (dev->wait) { | |
1029 | if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) { | |
1030 | wake_up (dev->wait); | |
1031 | } | |
1032 | ||
1033 | // or are we maybe short a few urbs? | |
1034 | } else if (netif_running (dev->net) | |
1035 | && netif_device_present (dev->net) | |
1036 | && !timer_pending (&dev->delay) | |
1037 | && !test_bit (EVENT_RX_HALT, &dev->flags)) { | |
1038 | int temp = dev->rxq.qlen; | |
1039 | int qlen = RX_QLEN (dev); | |
1040 | ||
1041 | if (temp < qlen) { | |
1042 | struct urb *urb; | |
1043 | int i; | |
1044 | ||
1045 | // don't refill the queue all at once | |
1046 | for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) { | |
1047 | urb = usb_alloc_urb (0, GFP_ATOMIC); | |
1048 | if (urb != NULL) | |
1049 | rx_submit (dev, urb, GFP_ATOMIC); | |
1050 | } | |
1051 | if (temp != dev->rxq.qlen && netif_msg_link (dev)) | |
1052 | devdbg (dev, "rxqlen %d --> %d", | |
1053 | temp, dev->rxq.qlen); | |
1054 | if (dev->rxq.qlen < qlen) | |
1055 | tasklet_schedule (&dev->bh); | |
1056 | } | |
1057 | if (dev->txq.qlen < TX_QLEN (dev)) | |
1058 | netif_wake_queue (dev->net); | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | ||
1063 | \f | |
1064 | /*------------------------------------------------------------------------- | |
1065 | * | |
1066 | * USB Device Driver support | |
1067 | * | |
1068 | *-------------------------------------------------------------------------*/ | |
cb1cebbe | 1069 | |
1da177e4 LT |
1070 | // precondition: never called in_interrupt |
1071 | ||
38bde1d4 | 1072 | void usbnet_disconnect (struct usb_interface *intf) |
1da177e4 LT |
1073 | { |
1074 | struct usbnet *dev; | |
1075 | struct usb_device *xdev; | |
1076 | struct net_device *net; | |
1077 | ||
1078 | dev = usb_get_intfdata(intf); | |
1079 | usb_set_intfdata(intf, NULL); | |
1080 | if (!dev) | |
1081 | return; | |
1082 | ||
1083 | xdev = interface_to_usbdev (intf); | |
1084 | ||
1085 | if (netif_msg_probe (dev)) | |
38bde1d4 DB |
1086 | devinfo (dev, "unregister '%s' usb-%s-%s, %s", |
1087 | intf->dev.driver->name, | |
1da177e4 LT |
1088 | xdev->bus->bus_name, xdev->devpath, |
1089 | dev->driver_info->description); | |
cb1cebbe | 1090 | |
1da177e4 LT |
1091 | net = dev->net; |
1092 | unregister_netdev (net); | |
1093 | ||
1094 | /* we don't hold rtnl here ... */ | |
1095 | flush_scheduled_work (); | |
1096 | ||
1097 | if (dev->driver_info->unbind) | |
1098 | dev->driver_info->unbind (dev, intf); | |
1099 | ||
1100 | free_netdev(net); | |
1101 | usb_put_dev (xdev); | |
1102 | } | |
38bde1d4 | 1103 | EXPORT_SYMBOL_GPL(usbnet_disconnect); |
1da177e4 LT |
1104 | |
1105 | ||
1106 | /*-------------------------------------------------------------------------*/ | |
1107 | ||
1da177e4 LT |
1108 | // precondition: never called in_interrupt |
1109 | ||
38bde1d4 | 1110 | int |
1da177e4 LT |
1111 | usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod) |
1112 | { | |
1113 | struct usbnet *dev; | |
cb1cebbe | 1114 | struct net_device *net; |
1da177e4 LT |
1115 | struct usb_host_interface *interface; |
1116 | struct driver_info *info; | |
1117 | struct usb_device *xdev; | |
1118 | int status; | |
296c0242 | 1119 | const char *name; |
1da177e4 | 1120 | |
296c0242 | 1121 | name = udev->dev.driver->name; |
1da177e4 LT |
1122 | info = (struct driver_info *) prod->driver_info; |
1123 | if (!info) { | |
296c0242 | 1124 | dev_dbg (&udev->dev, "blacklisted by %s\n", name); |
1da177e4 LT |
1125 | return -ENODEV; |
1126 | } | |
1127 | xdev = interface_to_usbdev (udev); | |
1128 | interface = udev->cur_altsetting; | |
1129 | ||
1130 | usb_get_dev (xdev); | |
1131 | ||
1132 | status = -ENOMEM; | |
1133 | ||
1134 | // set up our own records | |
1135 | net = alloc_etherdev(sizeof(*dev)); | |
1136 | if (!net) { | |
1137 | dbg ("can't kmalloc dev"); | |
1138 | goto out; | |
1139 | } | |
1140 | ||
1141 | dev = netdev_priv(net); | |
1142 | dev->udev = xdev; | |
a11a6544 | 1143 | dev->intf = udev; |
1da177e4 | 1144 | dev->driver_info = info; |
296c0242 | 1145 | dev->driver_name = name; |
1da177e4 LT |
1146 | dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV |
1147 | | NETIF_MSG_PROBE | NETIF_MSG_LINK); | |
1148 | skb_queue_head_init (&dev->rxq); | |
1149 | skb_queue_head_init (&dev->txq); | |
1150 | skb_queue_head_init (&dev->done); | |
1151 | dev->bh.func = usbnet_bh; | |
1152 | dev->bh.data = (unsigned long) dev; | |
c4028958 | 1153 | INIT_WORK (&dev->kevent, kevent); |
1da177e4 LT |
1154 | dev->delay.function = usbnet_bh; |
1155 | dev->delay.data = (unsigned long) dev; | |
1156 | init_timer (&dev->delay); | |
a9fc6338 | 1157 | mutex_init (&dev->phy_mutex); |
1da177e4 | 1158 | |
1da177e4 LT |
1159 | dev->net = net; |
1160 | strcpy (net->name, "usb%d"); | |
1161 | memcpy (net->dev_addr, node_id, sizeof node_id); | |
1162 | ||
2e55cc72 DB |
1163 | /* rx and tx sides can use different message sizes; |
1164 | * bind() should set rx_urb_size in that case. | |
1165 | */ | |
1166 | dev->hard_mtu = net->mtu + net->hard_header_len; | |
1da177e4 LT |
1167 | #if 0 |
1168 | // dma_supported() is deeply broken on almost all architectures | |
1169 | // possible with some EHCI controllers | |
1170 | if (dma_supported (&udev->dev, DMA_64BIT_MASK)) | |
1171 | net->features |= NETIF_F_HIGHDMA; | |
1172 | #endif | |
1173 | ||
1174 | net->change_mtu = usbnet_change_mtu; | |
1da177e4 LT |
1175 | net->hard_start_xmit = usbnet_start_xmit; |
1176 | net->open = usbnet_open; | |
1177 | net->stop = usbnet_stop; | |
1178 | net->watchdog_timeo = TX_TIMEOUT_JIFFIES; | |
1179 | net->tx_timeout = usbnet_tx_timeout; | |
1da177e4 LT |
1180 | net->ethtool_ops = &usbnet_ethtool_ops; |
1181 | ||
1182 | // allow device-specific bind/init procedures | |
1183 | // NOTE net->name still not usable ... | |
1184 | if (info->bind) { | |
1185 | status = info->bind (dev, udev); | |
cb1cebbe DB |
1186 | if (status < 0) |
1187 | goto out1; | |
1188 | ||
1da177e4 LT |
1189 | // heuristic: "usb%d" for links we know are two-host, |
1190 | // else "eth%d" when there's reasonable doubt. userspace | |
1191 | // can rename the link if it knows better. | |
1192 | if ((dev->driver_info->flags & FLAG_ETHER) != 0 | |
1193 | && (net->dev_addr [0] & 0x02) == 0) | |
1194 | strcpy (net->name, "eth%d"); | |
6e3bbcc5 JK |
1195 | /* WLAN devices should always be named "wlan%d" */ |
1196 | if ((dev->driver_info->flags & FLAG_WLAN) != 0) | |
1197 | strcpy(net->name, "wlan%d"); | |
f29fc259 DB |
1198 | |
1199 | /* maybe the remote can't receive an Ethernet MTU */ | |
1200 | if (net->mtu > (dev->hard_mtu - net->hard_header_len)) | |
1201 | net->mtu = dev->hard_mtu - net->hard_header_len; | |
2e55cc72 DB |
1202 | } else if (!info->in || !info->out) |
1203 | status = usbnet_get_endpoints (dev, udev); | |
1da177e4 LT |
1204 | else { |
1205 | dev->in = usb_rcvbulkpipe (xdev, info->in); | |
1206 | dev->out = usb_sndbulkpipe (xdev, info->out); | |
1207 | if (!(info->flags & FLAG_NO_SETINT)) | |
1208 | status = usb_set_interface (xdev, | |
1209 | interface->desc.bInterfaceNumber, | |
1210 | interface->desc.bAlternateSetting); | |
1211 | else | |
1212 | status = 0; | |
1213 | ||
1214 | } | |
9514bfe5 | 1215 | if (status >= 0 && dev->status) |
1da177e4 LT |
1216 | status = init_status (dev, udev); |
1217 | if (status < 0) | |
cb1cebbe | 1218 | goto out3; |
1da177e4 | 1219 | |
2e55cc72 DB |
1220 | if (!dev->rx_urb_size) |
1221 | dev->rx_urb_size = dev->hard_mtu; | |
1da177e4 | 1222 | dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1); |
cb1cebbe | 1223 | |
1da177e4 LT |
1224 | SET_NETDEV_DEV(net, &udev->dev); |
1225 | status = register_netdev (net); | |
1226 | if (status) | |
1227 | goto out3; | |
1228 | if (netif_msg_probe (dev)) | |
e174961c | 1229 | devinfo (dev, "register '%s' at usb-%s-%s, %s, %pM", |
38bde1d4 | 1230 | udev->dev.driver->name, |
1da177e4 LT |
1231 | xdev->bus->bus_name, xdev->devpath, |
1232 | dev->driver_info->description, | |
e174961c | 1233 | net->dev_addr); |
1da177e4 LT |
1234 | |
1235 | // ok, it's ready to go. | |
1236 | usb_set_intfdata (udev, dev); | |
1237 | ||
1238 | // start as if the link is up | |
1239 | netif_device_attach (net); | |
1240 | ||
1241 | return 0; | |
1242 | ||
1243 | out3: | |
1244 | if (info->unbind) | |
1245 | info->unbind (dev, udev); | |
1246 | out1: | |
1247 | free_netdev(net); | |
1248 | out: | |
1249 | usb_put_dev(xdev); | |
1250 | return status; | |
1251 | } | |
38bde1d4 | 1252 | EXPORT_SYMBOL_GPL(usbnet_probe); |
1da177e4 LT |
1253 | |
1254 | /*-------------------------------------------------------------------------*/ | |
1255 | ||
36433127 ON |
1256 | /* |
1257 | * suspend the whole driver as soon as the first interface is suspended | |
1258 | * resume only when the last interface is resumed | |
38bde1d4 | 1259 | */ |
1da177e4 | 1260 | |
38bde1d4 | 1261 | int usbnet_suspend (struct usb_interface *intf, pm_message_t message) |
1da177e4 LT |
1262 | { |
1263 | struct usbnet *dev = usb_get_intfdata(intf); | |
cb1cebbe | 1264 | |
36433127 | 1265 | if (!dev->suspend_count++) { |
a11a6544 ON |
1266 | /* |
1267 | * accelerate emptying of the rx and queues, to avoid | |
36433127 ON |
1268 | * having everything error out. |
1269 | */ | |
1270 | netif_device_detach (dev->net); | |
1271 | (void) unlink_urbs (dev, &dev->rxq); | |
1272 | (void) unlink_urbs (dev, &dev->txq); | |
a11a6544 ON |
1273 | /* |
1274 | * reattach so runtime management can use and | |
1275 | * wake the device | |
1276 | */ | |
1277 | netif_device_attach (dev->net); | |
36433127 | 1278 | } |
1da177e4 LT |
1279 | return 0; |
1280 | } | |
38bde1d4 | 1281 | EXPORT_SYMBOL_GPL(usbnet_suspend); |
1da177e4 | 1282 | |
38bde1d4 | 1283 | int usbnet_resume (struct usb_interface *intf) |
1da177e4 LT |
1284 | { |
1285 | struct usbnet *dev = usb_get_intfdata(intf); | |
1286 | ||
a11a6544 | 1287 | if (!--dev->suspend_count) |
36433127 | 1288 | tasklet_schedule (&dev->bh); |
a11a6544 | 1289 | |
1da177e4 LT |
1290 | return 0; |
1291 | } | |
38bde1d4 | 1292 | EXPORT_SYMBOL_GPL(usbnet_resume); |
1da177e4 | 1293 | |
1da177e4 | 1294 | |
1da177e4 LT |
1295 | /*-------------------------------------------------------------------------*/ |
1296 | ||
f29fc259 | 1297 | static int __init usbnet_init(void) |
1da177e4 | 1298 | { |
090ffa9d | 1299 | /* compiler should optimize this out */ |
f8ac232a | 1300 | BUILD_BUG_ON (sizeof (((struct sk_buff *)0)->cb) |
1da177e4 | 1301 | < sizeof (struct skb_data)); |
1da177e4 LT |
1302 | |
1303 | random_ether_addr(node_id); | |
cb1cebbe | 1304 | return 0; |
1da177e4 | 1305 | } |
f29fc259 | 1306 | module_init(usbnet_init); |
1da177e4 | 1307 | |
f29fc259 | 1308 | static void __exit usbnet_exit(void) |
1da177e4 | 1309 | { |
1da177e4 | 1310 | } |
f29fc259 | 1311 | module_exit(usbnet_exit); |
1da177e4 | 1312 | |
f29fc259 | 1313 | MODULE_AUTHOR("David Brownell"); |
090ffa9d | 1314 | MODULE_DESCRIPTION("USB network driver framework"); |
f29fc259 | 1315 | MODULE_LICENSE("GPL"); |