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