Commit | Line | Data |
---|---|---|
a84fab3c CL |
1 | /* |
2 | * Atheros CARL9170 driver | |
3 | * | |
4 | * USB - frontend | |
5 | * | |
6 | * Copyright 2008, Johannes Berg <johannes@sipsolutions.net> | |
7 | * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; see the file COPYING. If not, see | |
21 | * http://www.gnu.org/licenses/. | |
22 | * | |
23 | * This file incorporates work covered by the following copyright and | |
24 | * permission notice: | |
25 | * Copyright (c) 2007-2008 Atheros Communications, Inc. | |
26 | * | |
27 | * Permission to use, copy, modify, and/or distribute this software for any | |
28 | * purpose with or without fee is hereby granted, provided that the above | |
29 | * copyright notice and this permission notice appear in all copies. | |
30 | * | |
31 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
32 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
33 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
34 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
35 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
36 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
37 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
38 | */ | |
39 | ||
40 | #include <linux/module.h> | |
41 | #include <linux/slab.h> | |
42 | #include <linux/usb.h> | |
43 | #include <linux/firmware.h> | |
44 | #include <linux/etherdevice.h> | |
45 | #include <linux/device.h> | |
46 | #include <net/mac80211.h> | |
47 | #include "carl9170.h" | |
48 | #include "cmd.h" | |
49 | #include "hw.h" | |
50 | #include "fwcmd.h" | |
51 | ||
52 | MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); | |
53 | MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>"); | |
54 | MODULE_LICENSE("GPL"); | |
55 | MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless"); | |
56 | MODULE_FIRMWARE(CARL9170FW_NAME); | |
57 | MODULE_ALIAS("ar9170usb"); | |
58 | MODULE_ALIAS("arusb_lnx"); | |
59 | ||
60 | /* | |
61 | * Note: | |
62 | * | |
63 | * Always update our wiki's device list (located at: | |
64 | * http://wireless.kernel.org/en/users/Drivers/ar9170/devices ), | |
65 | * whenever you add a new device. | |
66 | */ | |
67 | static struct usb_device_id carl9170_usb_ids[] = { | |
68 | /* Atheros 9170 */ | |
69 | { USB_DEVICE(0x0cf3, 0x9170) }, | |
70 | /* Atheros TG121N */ | |
71 | { USB_DEVICE(0x0cf3, 0x1001) }, | |
72 | /* TP-Link TL-WN821N v2 */ | |
73 | { USB_DEVICE(0x0cf3, 0x1002), .driver_info = CARL9170_WPS_BUTTON | | |
74 | CARL9170_ONE_LED }, | |
75 | /* 3Com Dual Band 802.11n USB Adapter */ | |
76 | { USB_DEVICE(0x0cf3, 0x1010) }, | |
77 | /* H3C Dual Band 802.11n USB Adapter */ | |
78 | { USB_DEVICE(0x0cf3, 0x1011) }, | |
79 | /* Cace Airpcap NX */ | |
80 | { USB_DEVICE(0xcace, 0x0300) }, | |
81 | /* D-Link DWA 160 A1 */ | |
82 | { USB_DEVICE(0x07d1, 0x3c10) }, | |
83 | /* D-Link DWA 160 A2 */ | |
84 | { USB_DEVICE(0x07d1, 0x3a09) }, | |
8df86db9 CL |
85 | /* D-Link DWA 130 D */ |
86 | { USB_DEVICE(0x07d1, 0x3a0f) }, | |
a84fab3c CL |
87 | /* Netgear WNA1000 */ |
88 | { USB_DEVICE(0x0846, 0x9040) }, | |
8df86db9 | 89 | /* Netgear WNDA3100 (v1) */ |
a84fab3c CL |
90 | { USB_DEVICE(0x0846, 0x9010) }, |
91 | /* Netgear WN111 v2 */ | |
92 | { USB_DEVICE(0x0846, 0x9001), .driver_info = CARL9170_ONE_LED }, | |
93 | /* Zydas ZD1221 */ | |
94 | { USB_DEVICE(0x0ace, 0x1221) }, | |
95 | /* Proxim ORiNOCO 802.11n USB */ | |
96 | { USB_DEVICE(0x1435, 0x0804) }, | |
97 | /* WNC Generic 11n USB Dongle */ | |
98 | { USB_DEVICE(0x1435, 0x0326) }, | |
99 | /* ZyXEL NWD271N */ | |
100 | { USB_DEVICE(0x0586, 0x3417) }, | |
101 | /* Z-Com UB81 BG */ | |
102 | { USB_DEVICE(0x0cde, 0x0023) }, | |
103 | /* Z-Com UB82 ABG */ | |
104 | { USB_DEVICE(0x0cde, 0x0026) }, | |
105 | /* Sphairon Homelink 1202 */ | |
106 | { USB_DEVICE(0x0cde, 0x0027) }, | |
107 | /* Arcadyan WN7512 */ | |
108 | { USB_DEVICE(0x083a, 0xf522) }, | |
109 | /* Planex GWUS300 */ | |
110 | { USB_DEVICE(0x2019, 0x5304) }, | |
111 | /* IO-Data WNGDNUS2 */ | |
112 | { USB_DEVICE(0x04bb, 0x093f) }, | |
113 | /* NEC WL300NU-G */ | |
114 | { USB_DEVICE(0x0409, 0x0249) }, | |
115 | /* AVM FRITZ!WLAN USB Stick N */ | |
116 | { USB_DEVICE(0x057c, 0x8401) }, | |
117 | /* AVM FRITZ!WLAN USB Stick N 2.4 */ | |
118 | { USB_DEVICE(0x057c, 0x8402) }, | |
119 | /* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */ | |
120 | { USB_DEVICE(0x1668, 0x1200) }, | |
121 | ||
122 | /* terminate */ | |
123 | {} | |
124 | }; | |
125 | MODULE_DEVICE_TABLE(usb, carl9170_usb_ids); | |
126 | ||
127 | static void carl9170_usb_submit_data_urb(struct ar9170 *ar) | |
128 | { | |
129 | struct urb *urb; | |
130 | int err; | |
131 | ||
132 | if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS) | |
133 | goto err_acc; | |
134 | ||
135 | urb = usb_get_from_anchor(&ar->tx_wait); | |
136 | if (!urb) | |
137 | goto err_acc; | |
138 | ||
139 | usb_anchor_urb(urb, &ar->tx_anch); | |
140 | ||
141 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
142 | if (unlikely(err)) { | |
143 | if (net_ratelimit()) { | |
144 | dev_err(&ar->udev->dev, "tx submit failed (%d)\n", | |
145 | urb->status); | |
146 | } | |
147 | ||
148 | usb_unanchor_urb(urb); | |
149 | usb_anchor_urb(urb, &ar->tx_err); | |
150 | } | |
151 | ||
152 | usb_free_urb(urb); | |
153 | ||
154 | if (likely(err == 0)) | |
155 | return; | |
156 | ||
157 | err_acc: | |
158 | atomic_dec(&ar->tx_anch_urbs); | |
159 | } | |
160 | ||
161 | static void carl9170_usb_tx_data_complete(struct urb *urb) | |
162 | { | |
163 | struct ar9170 *ar = (struct ar9170 *) | |
164 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); | |
165 | ||
166 | if (WARN_ON_ONCE(!ar)) { | |
167 | dev_kfree_skb_irq(urb->context); | |
168 | return; | |
169 | } | |
170 | ||
171 | atomic_dec(&ar->tx_anch_urbs); | |
172 | ||
173 | switch (urb->status) { | |
174 | /* everything is fine */ | |
175 | case 0: | |
176 | carl9170_tx_callback(ar, (void *)urb->context); | |
177 | break; | |
178 | ||
179 | /* disconnect */ | |
180 | case -ENOENT: | |
181 | case -ECONNRESET: | |
182 | case -ENODEV: | |
183 | case -ESHUTDOWN: | |
184 | /* | |
185 | * Defer the frame clean-up to the tasklet worker. | |
186 | * This is necessary, because carl9170_tx_drop | |
187 | * does not work in an irqsave context. | |
188 | */ | |
189 | usb_anchor_urb(urb, &ar->tx_err); | |
190 | return; | |
191 | ||
192 | /* a random transmission error has occurred? */ | |
193 | default: | |
194 | if (net_ratelimit()) { | |
195 | dev_err(&ar->udev->dev, "tx failed (%d)\n", | |
196 | urb->status); | |
197 | } | |
198 | ||
199 | usb_anchor_urb(urb, &ar->tx_err); | |
200 | break; | |
201 | } | |
202 | ||
203 | if (likely(IS_STARTED(ar))) | |
204 | carl9170_usb_submit_data_urb(ar); | |
205 | } | |
206 | ||
207 | static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar) | |
208 | { | |
209 | struct urb *urb; | |
210 | int err; | |
211 | ||
212 | if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) { | |
213 | atomic_dec(&ar->tx_cmd_urbs); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | urb = usb_get_from_anchor(&ar->tx_cmd); | |
218 | if (!urb) { | |
219 | atomic_dec(&ar->tx_cmd_urbs); | |
220 | return 0; | |
221 | } | |
222 | ||
223 | usb_anchor_urb(urb, &ar->tx_anch); | |
224 | err = usb_submit_urb(urb, GFP_ATOMIC); | |
225 | if (unlikely(err)) { | |
226 | usb_unanchor_urb(urb); | |
227 | atomic_dec(&ar->tx_cmd_urbs); | |
228 | } | |
229 | usb_free_urb(urb); | |
230 | ||
231 | return err; | |
232 | } | |
233 | ||
234 | static void carl9170_usb_cmd_complete(struct urb *urb) | |
235 | { | |
236 | struct ar9170 *ar = urb->context; | |
237 | int err = 0; | |
238 | ||
239 | if (WARN_ON_ONCE(!ar)) | |
240 | return; | |
241 | ||
242 | atomic_dec(&ar->tx_cmd_urbs); | |
243 | ||
244 | switch (urb->status) { | |
245 | /* everything is fine */ | |
246 | case 0: | |
247 | break; | |
248 | ||
249 | /* disconnect */ | |
250 | case -ENOENT: | |
251 | case -ECONNRESET: | |
252 | case -ENODEV: | |
253 | case -ESHUTDOWN: | |
254 | return; | |
255 | ||
256 | default: | |
257 | err = urb->status; | |
258 | break; | |
259 | } | |
260 | ||
261 | if (!IS_INITIALIZED(ar)) | |
262 | return; | |
263 | ||
264 | if (err) | |
265 | dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err); | |
266 | ||
267 | err = carl9170_usb_submit_cmd_urb(ar); | |
268 | if (err) | |
269 | dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err); | |
270 | } | |
271 | ||
272 | static void carl9170_usb_rx_irq_complete(struct urb *urb) | |
273 | { | |
274 | struct ar9170 *ar = urb->context; | |
275 | ||
276 | if (WARN_ON_ONCE(!ar)) | |
277 | return; | |
278 | ||
279 | switch (urb->status) { | |
280 | /* everything is fine */ | |
281 | case 0: | |
282 | break; | |
283 | ||
284 | /* disconnect */ | |
285 | case -ENOENT: | |
286 | case -ECONNRESET: | |
287 | case -ENODEV: | |
288 | case -ESHUTDOWN: | |
289 | return; | |
290 | ||
291 | default: | |
292 | goto resubmit; | |
293 | } | |
294 | ||
295 | carl9170_handle_command_response(ar, urb->transfer_buffer, | |
296 | urb->actual_length); | |
297 | ||
298 | resubmit: | |
299 | usb_anchor_urb(urb, &ar->rx_anch); | |
300 | if (unlikely(usb_submit_urb(urb, GFP_ATOMIC))) | |
301 | usb_unanchor_urb(urb); | |
302 | } | |
303 | ||
304 | static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp) | |
305 | { | |
306 | struct urb *urb; | |
307 | int err = 0, runs = 0; | |
308 | ||
309 | while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) && | |
310 | (runs++ < AR9170_NUM_RX_URBS)) { | |
311 | err = -ENOSPC; | |
312 | urb = usb_get_from_anchor(&ar->rx_pool); | |
313 | if (urb) { | |
314 | usb_anchor_urb(urb, &ar->rx_anch); | |
315 | err = usb_submit_urb(urb, gfp); | |
316 | if (unlikely(err)) { | |
317 | usb_unanchor_urb(urb); | |
318 | usb_anchor_urb(urb, &ar->rx_pool); | |
319 | } else { | |
320 | atomic_dec(&ar->rx_pool_urbs); | |
321 | atomic_inc(&ar->rx_anch_urbs); | |
322 | } | |
323 | usb_free_urb(urb); | |
324 | } | |
325 | } | |
326 | ||
327 | return err; | |
328 | } | |
329 | ||
330 | static void carl9170_usb_rx_work(struct ar9170 *ar) | |
331 | { | |
332 | struct urb *urb; | |
333 | int i; | |
334 | ||
335 | for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) { | |
336 | urb = usb_get_from_anchor(&ar->rx_work); | |
337 | if (!urb) | |
338 | break; | |
339 | ||
340 | atomic_dec(&ar->rx_work_urbs); | |
341 | if (IS_INITIALIZED(ar)) { | |
342 | carl9170_rx(ar, urb->transfer_buffer, | |
343 | urb->actual_length); | |
344 | } | |
345 | ||
346 | usb_anchor_urb(urb, &ar->rx_pool); | |
347 | atomic_inc(&ar->rx_pool_urbs); | |
348 | ||
349 | usb_free_urb(urb); | |
350 | ||
351 | carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC); | |
352 | } | |
353 | } | |
354 | ||
355 | void carl9170_usb_handle_tx_err(struct ar9170 *ar) | |
356 | { | |
357 | struct urb *urb; | |
358 | ||
359 | while ((urb = usb_get_from_anchor(&ar->tx_err))) { | |
360 | struct sk_buff *skb = (void *)urb->context; | |
361 | ||
362 | carl9170_tx_drop(ar, skb); | |
363 | carl9170_tx_callback(ar, skb); | |
364 | usb_free_urb(urb); | |
365 | } | |
366 | } | |
367 | ||
368 | static void carl9170_usb_tasklet(unsigned long data) | |
369 | { | |
370 | struct ar9170 *ar = (struct ar9170 *) data; | |
371 | ||
cf6487d0 CL |
372 | if (!IS_INITIALIZED(ar)) |
373 | return; | |
374 | ||
a84fab3c CL |
375 | carl9170_usb_rx_work(ar); |
376 | ||
377 | /* | |
378 | * Strictly speaking: The tx scheduler is not part of the USB system. | |
379 | * But the rx worker returns frames back to the mac80211-stack and | |
380 | * this is the _perfect_ place to generate the next transmissions. | |
381 | */ | |
382 | if (IS_STARTED(ar)) | |
383 | carl9170_tx_scheduler(ar); | |
384 | } | |
385 | ||
386 | static void carl9170_usb_rx_complete(struct urb *urb) | |
387 | { | |
388 | struct ar9170 *ar = (struct ar9170 *)urb->context; | |
389 | int err; | |
390 | ||
391 | if (WARN_ON_ONCE(!ar)) | |
392 | return; | |
393 | ||
394 | atomic_dec(&ar->rx_anch_urbs); | |
395 | ||
396 | switch (urb->status) { | |
397 | case 0: | |
398 | /* rx path */ | |
399 | usb_anchor_urb(urb, &ar->rx_work); | |
400 | atomic_inc(&ar->rx_work_urbs); | |
401 | break; | |
402 | ||
403 | case -ENOENT: | |
404 | case -ECONNRESET: | |
405 | case -ENODEV: | |
406 | case -ESHUTDOWN: | |
407 | /* handle disconnect events*/ | |
408 | return; | |
409 | ||
410 | default: | |
411 | /* handle all other errors */ | |
412 | usb_anchor_urb(urb, &ar->rx_pool); | |
413 | atomic_inc(&ar->rx_pool_urbs); | |
414 | break; | |
415 | } | |
416 | ||
417 | err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC); | |
418 | if (unlikely(err)) { | |
419 | /* | |
420 | * usb_submit_rx_urb reported a problem. | |
421 | * In case this is due to a rx buffer shortage, | |
422 | * elevate the tasklet worker priority to | |
423 | * the highest available level. | |
424 | */ | |
425 | tasklet_hi_schedule(&ar->usb_tasklet); | |
426 | ||
427 | if (atomic_read(&ar->rx_anch_urbs) == 0) { | |
428 | /* | |
429 | * The system is too slow to cope with | |
430 | * the enormous workload. We have simply | |
431 | * run out of active rx urbs and this | |
432 | * unfortunatly leads to an unpredictable | |
433 | * device. | |
434 | */ | |
435 | ||
436 | carl9170_restart(ar, CARL9170_RR_SLOW_SYSTEM); | |
437 | } | |
438 | } else { | |
439 | /* | |
440 | * Using anything less than _high_ priority absolutely | |
441 | * kills the rx performance my UP-System... | |
442 | */ | |
443 | tasklet_hi_schedule(&ar->usb_tasklet); | |
444 | } | |
445 | } | |
446 | ||
447 | static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp) | |
448 | { | |
449 | struct urb *urb; | |
450 | void *buf; | |
451 | ||
452 | buf = kmalloc(ar->fw.rx_size, gfp); | |
453 | if (!buf) | |
454 | return NULL; | |
455 | ||
456 | urb = usb_alloc_urb(0, gfp); | |
457 | if (!urb) { | |
458 | kfree(buf); | |
459 | return NULL; | |
460 | } | |
461 | ||
462 | usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev, | |
463 | AR9170_USB_EP_RX), buf, ar->fw.rx_size, | |
464 | carl9170_usb_rx_complete, ar); | |
465 | ||
466 | urb->transfer_flags |= URB_FREE_BUFFER; | |
467 | ||
468 | return urb; | |
469 | } | |
470 | ||
471 | static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar) | |
472 | { | |
473 | struct urb *urb = NULL; | |
474 | void *ibuf; | |
475 | int err = -ENOMEM; | |
476 | ||
477 | urb = usb_alloc_urb(0, GFP_KERNEL); | |
478 | if (!urb) | |
479 | goto out; | |
480 | ||
481 | ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL); | |
482 | if (!ibuf) | |
483 | goto out; | |
484 | ||
485 | usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev, | |
486 | AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX, | |
487 | carl9170_usb_rx_irq_complete, ar, 1); | |
488 | ||
489 | urb->transfer_flags |= URB_FREE_BUFFER; | |
490 | ||
491 | usb_anchor_urb(urb, &ar->rx_anch); | |
492 | err = usb_submit_urb(urb, GFP_KERNEL); | |
493 | if (err) | |
494 | usb_unanchor_urb(urb); | |
495 | ||
496 | out: | |
497 | usb_free_urb(urb); | |
498 | return err; | |
499 | } | |
500 | ||
501 | static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar) | |
502 | { | |
503 | struct urb *urb; | |
504 | int i, err = -EINVAL; | |
505 | ||
506 | /* | |
507 | * The driver actively maintains a second shadow | |
508 | * pool for inactive, but fully-prepared rx urbs. | |
509 | * | |
510 | * The pool should help the driver to master huge | |
511 | * workload spikes without running the risk of | |
512 | * undersupplying the hardware or wasting time by | |
513 | * processing rx data (streams) inside the urb | |
514 | * completion (hardirq context). | |
515 | */ | |
516 | for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) { | |
517 | urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL); | |
518 | if (!urb) { | |
519 | err = -ENOMEM; | |
520 | goto err_out; | |
521 | } | |
522 | ||
523 | usb_anchor_urb(urb, &ar->rx_pool); | |
524 | atomic_inc(&ar->rx_pool_urbs); | |
525 | usb_free_urb(urb); | |
526 | } | |
527 | ||
528 | err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL); | |
529 | if (err) | |
530 | goto err_out; | |
531 | ||
532 | /* the device now waiting for the firmware. */ | |
533 | carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE); | |
534 | return 0; | |
535 | ||
536 | err_out: | |
537 | ||
538 | usb_scuttle_anchored_urbs(&ar->rx_pool); | |
539 | usb_scuttle_anchored_urbs(&ar->rx_work); | |
540 | usb_kill_anchored_urbs(&ar->rx_anch); | |
541 | return err; | |
542 | } | |
543 | ||
544 | static int carl9170_usb_flush(struct ar9170 *ar) | |
545 | { | |
546 | struct urb *urb; | |
547 | int ret, err = 0; | |
548 | ||
549 | while ((urb = usb_get_from_anchor(&ar->tx_wait))) { | |
550 | struct sk_buff *skb = (void *)urb->context; | |
551 | carl9170_tx_drop(ar, skb); | |
552 | carl9170_tx_callback(ar, skb); | |
553 | usb_free_urb(urb); | |
554 | } | |
555 | ||
dfa31fef | 556 | ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000); |
a84fab3c CL |
557 | if (ret == 0) |
558 | err = -ETIMEDOUT; | |
559 | ||
560 | /* lets wait a while until the tx - queues are dried out */ | |
dfa31fef | 561 | ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000); |
a84fab3c CL |
562 | if (ret == 0) |
563 | err = -ETIMEDOUT; | |
564 | ||
565 | usb_kill_anchored_urbs(&ar->tx_anch); | |
566 | carl9170_usb_handle_tx_err(ar); | |
567 | ||
568 | return err; | |
569 | } | |
570 | ||
571 | static void carl9170_usb_cancel_urbs(struct ar9170 *ar) | |
572 | { | |
573 | int err; | |
574 | ||
575 | carl9170_set_state(ar, CARL9170_UNKNOWN_STATE); | |
576 | ||
577 | err = carl9170_usb_flush(ar); | |
578 | if (err) | |
579 | dev_err(&ar->udev->dev, "stuck tx urbs!\n"); | |
580 | ||
581 | usb_poison_anchored_urbs(&ar->tx_anch); | |
582 | carl9170_usb_handle_tx_err(ar); | |
583 | usb_poison_anchored_urbs(&ar->rx_anch); | |
584 | ||
585 | tasklet_kill(&ar->usb_tasklet); | |
586 | ||
587 | usb_scuttle_anchored_urbs(&ar->rx_work); | |
588 | usb_scuttle_anchored_urbs(&ar->rx_pool); | |
589 | usb_scuttle_anchored_urbs(&ar->tx_cmd); | |
590 | } | |
591 | ||
592 | int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd, | |
593 | const bool free_buf) | |
594 | { | |
595 | struct urb *urb; | |
cae7f953 | 596 | int err = 0; |
a84fab3c | 597 | |
cae7f953 CL |
598 | if (!IS_INITIALIZED(ar)) { |
599 | err = -EPERM; | |
600 | goto err_free; | |
601 | } | |
a84fab3c | 602 | |
cae7f953 CL |
603 | if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) { |
604 | err = -EINVAL; | |
605 | goto err_free; | |
606 | } | |
a84fab3c CL |
607 | |
608 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
cae7f953 CL |
609 | if (!urb) { |
610 | err = -ENOMEM; | |
611 | goto err_free; | |
612 | } | |
a84fab3c CL |
613 | |
614 | usb_fill_int_urb(urb, ar->udev, usb_sndintpipe(ar->udev, | |
615 | AR9170_USB_EP_CMD), cmd, cmd->hdr.len + 4, | |
616 | carl9170_usb_cmd_complete, ar, 1); | |
617 | ||
a84fab3c CL |
618 | if (free_buf) |
619 | urb->transfer_flags |= URB_FREE_BUFFER; | |
620 | ||
621 | usb_anchor_urb(urb, &ar->tx_cmd); | |
622 | usb_free_urb(urb); | |
623 | ||
624 | return carl9170_usb_submit_cmd_urb(ar); | |
cae7f953 CL |
625 | |
626 | err_free: | |
627 | if (free_buf) | |
628 | kfree(cmd); | |
629 | ||
630 | return err; | |
a84fab3c CL |
631 | } |
632 | ||
633 | int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd, | |
634 | unsigned int plen, void *payload, unsigned int outlen, void *out) | |
635 | { | |
636 | int err = -ENOMEM; | |
637 | ||
638 | if (!IS_ACCEPTING_CMD(ar)) | |
639 | return -EIO; | |
640 | ||
641 | if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) | |
642 | might_sleep(); | |
643 | ||
644 | ar->cmd.hdr.len = plen; | |
645 | ar->cmd.hdr.cmd = cmd; | |
646 | /* writing multiple regs fills this buffer already */ | |
647 | if (plen && payload != (u8 *)(ar->cmd.data)) | |
648 | memcpy(ar->cmd.data, payload, plen); | |
649 | ||
650 | spin_lock_bh(&ar->cmd_lock); | |
651 | ar->readbuf = (u8 *)out; | |
652 | ar->readlen = outlen; | |
653 | spin_unlock_bh(&ar->cmd_lock); | |
654 | ||
655 | err = __carl9170_exec_cmd(ar, &ar->cmd, false); | |
656 | ||
657 | if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) { | |
658 | err = wait_for_completion_timeout(&ar->cmd_wait, HZ); | |
659 | if (err == 0) { | |
660 | err = -ETIMEDOUT; | |
661 | goto err_unbuf; | |
662 | } | |
663 | ||
664 | if (ar->readlen != outlen) { | |
665 | err = -EMSGSIZE; | |
666 | goto err_unbuf; | |
667 | } | |
668 | } | |
669 | ||
670 | return 0; | |
671 | ||
672 | err_unbuf: | |
673 | /* Maybe the device was removed in the moment we were waiting? */ | |
674 | if (IS_STARTED(ar)) { | |
675 | dev_err(&ar->udev->dev, "no command feedback " | |
676 | "received (%d).\n", err); | |
677 | ||
678 | /* provide some maybe useful debug information */ | |
679 | print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE, | |
680 | &ar->cmd, plen + 4); | |
681 | ||
682 | carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT); | |
683 | } | |
684 | ||
685 | /* invalidate to avoid completing the next command prematurely */ | |
686 | spin_lock_bh(&ar->cmd_lock); | |
687 | ar->readbuf = NULL; | |
688 | ar->readlen = 0; | |
689 | spin_unlock_bh(&ar->cmd_lock); | |
690 | ||
691 | return err; | |
692 | } | |
693 | ||
694 | void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb) | |
695 | { | |
696 | struct urb *urb; | |
697 | struct ar9170_stream *tx_stream; | |
698 | void *data; | |
699 | unsigned int len; | |
700 | ||
701 | if (!IS_STARTED(ar)) | |
702 | goto err_drop; | |
703 | ||
704 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
705 | if (!urb) | |
706 | goto err_drop; | |
707 | ||
708 | if (ar->fw.tx_stream) { | |
709 | tx_stream = (void *) (skb->data - sizeof(*tx_stream)); | |
710 | ||
711 | len = skb->len + sizeof(*tx_stream); | |
712 | tx_stream->length = cpu_to_le16(len); | |
713 | tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG); | |
714 | data = tx_stream; | |
715 | } else { | |
716 | data = skb->data; | |
717 | len = skb->len; | |
718 | } | |
719 | ||
720 | usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev, | |
721 | AR9170_USB_EP_TX), data, len, | |
722 | carl9170_usb_tx_data_complete, skb); | |
723 | ||
724 | urb->transfer_flags |= URB_ZERO_PACKET; | |
725 | ||
726 | usb_anchor_urb(urb, &ar->tx_wait); | |
727 | ||
728 | usb_free_urb(urb); | |
729 | ||
730 | carl9170_usb_submit_data_urb(ar); | |
731 | return; | |
732 | ||
733 | err_drop: | |
734 | carl9170_tx_drop(ar, skb); | |
735 | carl9170_tx_callback(ar, skb); | |
736 | } | |
737 | ||
738 | static void carl9170_release_firmware(struct ar9170 *ar) | |
739 | { | |
740 | if (ar->fw.fw) { | |
741 | release_firmware(ar->fw.fw); | |
742 | memset(&ar->fw, 0, sizeof(ar->fw)); | |
743 | } | |
744 | } | |
745 | ||
746 | void carl9170_usb_stop(struct ar9170 *ar) | |
747 | { | |
748 | int ret; | |
749 | ||
750 | carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED); | |
751 | ||
752 | ret = carl9170_usb_flush(ar); | |
753 | if (ret) | |
754 | dev_err(&ar->udev->dev, "kill pending tx urbs.\n"); | |
755 | ||
756 | usb_poison_anchored_urbs(&ar->tx_anch); | |
757 | carl9170_usb_handle_tx_err(ar); | |
758 | ||
759 | /* kill any pending command */ | |
760 | spin_lock_bh(&ar->cmd_lock); | |
761 | ar->readlen = 0; | |
762 | spin_unlock_bh(&ar->cmd_lock); | |
763 | complete_all(&ar->cmd_wait); | |
764 | ||
765 | /* This is required to prevent an early completion on _start */ | |
766 | INIT_COMPLETION(ar->cmd_wait); | |
767 | ||
768 | /* | |
769 | * Note: | |
770 | * So far we freed all tx urbs, but we won't dare to touch any rx urbs. | |
771 | * Else we would end up with a unresponsive device... | |
772 | */ | |
773 | } | |
774 | ||
775 | int carl9170_usb_open(struct ar9170 *ar) | |
776 | { | |
777 | usb_unpoison_anchored_urbs(&ar->tx_anch); | |
778 | ||
779 | carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE); | |
780 | return 0; | |
781 | } | |
782 | ||
783 | static int carl9170_usb_load_firmware(struct ar9170 *ar) | |
784 | { | |
785 | const u8 *data; | |
786 | u8 *buf; | |
787 | unsigned int transfer; | |
788 | size_t len; | |
789 | u32 addr; | |
790 | int err = 0; | |
791 | ||
792 | buf = kmalloc(4096, GFP_KERNEL); | |
793 | if (!buf) { | |
794 | err = -ENOMEM; | |
795 | goto err_out; | |
796 | } | |
797 | ||
798 | data = ar->fw.fw->data; | |
799 | len = ar->fw.fw->size; | |
800 | addr = ar->fw.address; | |
801 | ||
802 | /* this removes the miniboot image */ | |
803 | data += ar->fw.offset; | |
804 | len -= ar->fw.offset; | |
805 | ||
806 | while (len) { | |
807 | transfer = min_t(unsigned int, len, 4096u); | |
808 | memcpy(buf, data, transfer); | |
809 | ||
810 | err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0), | |
811 | 0x30 /* FW DL */, 0x40 | USB_DIR_OUT, | |
812 | addr >> 8, 0, buf, transfer, 100); | |
813 | ||
814 | if (err < 0) { | |
815 | kfree(buf); | |
816 | goto err_out; | |
817 | } | |
818 | ||
819 | len -= transfer; | |
820 | data += transfer; | |
821 | addr += transfer; | |
822 | } | |
823 | kfree(buf); | |
824 | ||
825 | err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0), | |
826 | 0x31 /* FW DL COMPLETE */, | |
827 | 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200); | |
828 | ||
829 | if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) { | |
830 | err = -ETIMEDOUT; | |
831 | goto err_out; | |
832 | } | |
833 | ||
834 | err = carl9170_echo_test(ar, 0x4a110123); | |
835 | if (err) | |
836 | goto err_out; | |
837 | ||
838 | /* firmware restarts cmd counter */ | |
839 | ar->cmd_seq = -1; | |
840 | ||
841 | return 0; | |
842 | ||
843 | err_out: | |
844 | dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err); | |
845 | return err; | |
846 | } | |
847 | ||
848 | int carl9170_usb_restart(struct ar9170 *ar) | |
849 | { | |
850 | int err = 0; | |
851 | ||
852 | if (ar->intf->condition != USB_INTERFACE_BOUND) | |
853 | return 0; | |
854 | ||
855 | /* Disable command response sequence counter. */ | |
856 | ar->cmd_seq = -2; | |
857 | ||
858 | err = carl9170_reboot(ar); | |
859 | ||
860 | carl9170_usb_stop(ar); | |
861 | ||
a84fab3c CL |
862 | if (err) |
863 | goto err_out; | |
864 | ||
865 | tasklet_schedule(&ar->usb_tasklet); | |
866 | ||
867 | /* The reboot procedure can take quite a while to complete. */ | |
868 | msleep(1100); | |
869 | ||
870 | err = carl9170_usb_open(ar); | |
871 | if (err) | |
872 | goto err_out; | |
873 | ||
874 | err = carl9170_usb_load_firmware(ar); | |
875 | if (err) | |
876 | goto err_out; | |
877 | ||
878 | return 0; | |
879 | ||
880 | err_out: | |
881 | carl9170_usb_cancel_urbs(ar); | |
882 | return err; | |
883 | } | |
884 | ||
885 | void carl9170_usb_reset(struct ar9170 *ar) | |
886 | { | |
887 | /* | |
888 | * This is the last resort to get the device going again | |
889 | * without any *user replugging action*. | |
890 | * | |
891 | * But there is a catch: usb_reset really is like a physical | |
892 | * *reconnect*. The mac80211 state will be lost in the process. | |
893 | * Therefore a userspace application, which is monitoring | |
894 | * the link must step in. | |
895 | */ | |
896 | carl9170_usb_cancel_urbs(ar); | |
897 | ||
898 | carl9170_usb_stop(ar); | |
899 | ||
900 | usb_queue_reset_device(ar->intf); | |
901 | } | |
902 | ||
903 | static int carl9170_usb_init_device(struct ar9170 *ar) | |
904 | { | |
905 | int err; | |
906 | ||
907 | err = carl9170_usb_send_rx_irq_urb(ar); | |
908 | if (err) | |
909 | goto err_out; | |
910 | ||
911 | err = carl9170_usb_init_rx_bulk_urbs(ar); | |
912 | if (err) | |
913 | goto err_unrx; | |
914 | ||
915 | mutex_lock(&ar->mutex); | |
916 | err = carl9170_usb_load_firmware(ar); | |
917 | mutex_unlock(&ar->mutex); | |
918 | if (err) | |
919 | goto err_unrx; | |
920 | ||
921 | return 0; | |
922 | ||
923 | err_unrx: | |
924 | carl9170_usb_cancel_urbs(ar); | |
925 | ||
926 | err_out: | |
927 | return err; | |
928 | } | |
929 | ||
930 | static void carl9170_usb_firmware_failed(struct ar9170 *ar) | |
931 | { | |
932 | struct device *parent = ar->udev->dev.parent; | |
933 | struct usb_device *udev; | |
934 | ||
935 | /* | |
936 | * Store a copy of the usb_device pointer locally. | |
937 | * This is because device_release_driver initiates | |
938 | * carl9170_usb_disconnect, which in turn frees our | |
939 | * driver context (ar). | |
940 | */ | |
941 | udev = ar->udev; | |
942 | ||
943 | complete(&ar->fw_load_wait); | |
944 | ||
945 | /* unbind anything failed */ | |
946 | if (parent) | |
947 | device_lock(parent); | |
948 | ||
949 | device_release_driver(&udev->dev); | |
950 | if (parent) | |
951 | device_unlock(parent); | |
952 | ||
953 | usb_put_dev(udev); | |
954 | } | |
955 | ||
956 | static void carl9170_usb_firmware_finish(struct ar9170 *ar) | |
957 | { | |
958 | int err; | |
959 | ||
960 | err = carl9170_parse_firmware(ar); | |
961 | if (err) | |
962 | goto err_freefw; | |
963 | ||
964 | err = carl9170_usb_init_device(ar); | |
965 | if (err) | |
966 | goto err_freefw; | |
967 | ||
968 | err = carl9170_usb_open(ar); | |
969 | if (err) | |
970 | goto err_unrx; | |
971 | ||
972 | err = carl9170_register(ar); | |
973 | ||
974 | carl9170_usb_stop(ar); | |
975 | if (err) | |
976 | goto err_unrx; | |
977 | ||
978 | complete(&ar->fw_load_wait); | |
979 | usb_put_dev(ar->udev); | |
980 | return; | |
981 | ||
982 | err_unrx: | |
983 | carl9170_usb_cancel_urbs(ar); | |
984 | ||
985 | err_freefw: | |
986 | carl9170_release_firmware(ar); | |
987 | carl9170_usb_firmware_failed(ar); | |
988 | } | |
989 | ||
990 | static void carl9170_usb_firmware_step2(const struct firmware *fw, | |
991 | void *context) | |
992 | { | |
993 | struct ar9170 *ar = context; | |
994 | ||
995 | if (fw) { | |
996 | ar->fw.fw = fw; | |
997 | carl9170_usb_firmware_finish(ar); | |
998 | return; | |
999 | } | |
1000 | ||
1001 | dev_err(&ar->udev->dev, "firmware not found.\n"); | |
1002 | carl9170_usb_firmware_failed(ar); | |
1003 | } | |
1004 | ||
1005 | static int carl9170_usb_probe(struct usb_interface *intf, | |
1006 | const struct usb_device_id *id) | |
1007 | { | |
1008 | struct ar9170 *ar; | |
1009 | struct usb_device *udev; | |
1010 | int err; | |
1011 | ||
1012 | err = usb_reset_device(interface_to_usbdev(intf)); | |
1013 | if (err) | |
1014 | return err; | |
1015 | ||
1016 | ar = carl9170_alloc(sizeof(*ar)); | |
1017 | if (IS_ERR(ar)) | |
1018 | return PTR_ERR(ar); | |
1019 | ||
1020 | udev = interface_to_usbdev(intf); | |
1021 | usb_get_dev(udev); | |
1022 | ar->udev = udev; | |
1023 | ar->intf = intf; | |
1024 | ar->features = id->driver_info; | |
1025 | ||
1026 | usb_set_intfdata(intf, ar); | |
1027 | SET_IEEE80211_DEV(ar->hw, &intf->dev); | |
1028 | ||
1029 | init_usb_anchor(&ar->rx_anch); | |
1030 | init_usb_anchor(&ar->rx_pool); | |
1031 | init_usb_anchor(&ar->rx_work); | |
1032 | init_usb_anchor(&ar->tx_wait); | |
1033 | init_usb_anchor(&ar->tx_anch); | |
1034 | init_usb_anchor(&ar->tx_cmd); | |
1035 | init_usb_anchor(&ar->tx_err); | |
1036 | init_completion(&ar->cmd_wait); | |
1037 | init_completion(&ar->fw_boot_wait); | |
1038 | init_completion(&ar->fw_load_wait); | |
1039 | tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet, | |
1040 | (unsigned long)ar); | |
1041 | ||
1042 | atomic_set(&ar->tx_cmd_urbs, 0); | |
1043 | atomic_set(&ar->tx_anch_urbs, 0); | |
1044 | atomic_set(&ar->rx_work_urbs, 0); | |
1045 | atomic_set(&ar->rx_anch_urbs, 0); | |
1046 | atomic_set(&ar->rx_pool_urbs, 0); | |
1047 | ar->cmd_seq = -2; | |
1048 | ||
1049 | usb_get_dev(ar->udev); | |
1050 | ||
1051 | carl9170_set_state(ar, CARL9170_STOPPED); | |
1052 | ||
1053 | return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME, | |
1054 | &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2); | |
1055 | } | |
1056 | ||
1057 | static void carl9170_usb_disconnect(struct usb_interface *intf) | |
1058 | { | |
1059 | struct ar9170 *ar = usb_get_intfdata(intf); | |
1060 | struct usb_device *udev; | |
1061 | ||
1062 | if (WARN_ON(!ar)) | |
1063 | return; | |
1064 | ||
1065 | udev = ar->udev; | |
1066 | wait_for_completion(&ar->fw_load_wait); | |
1067 | ||
1068 | if (IS_INITIALIZED(ar)) { | |
1069 | carl9170_reboot(ar); | |
1070 | carl9170_usb_stop(ar); | |
1071 | } | |
1072 | ||
1073 | carl9170_usb_cancel_urbs(ar); | |
1074 | carl9170_unregister(ar); | |
1075 | ||
1076 | usb_set_intfdata(intf, NULL); | |
1077 | ||
1078 | carl9170_release_firmware(ar); | |
1079 | carl9170_free(ar); | |
1080 | usb_put_dev(udev); | |
1081 | } | |
1082 | ||
1083 | #ifdef CONFIG_PM | |
1084 | static int carl9170_usb_suspend(struct usb_interface *intf, | |
1085 | pm_message_t message) | |
1086 | { | |
1087 | struct ar9170 *ar = usb_get_intfdata(intf); | |
1088 | ||
1089 | if (!ar) | |
1090 | return -ENODEV; | |
1091 | ||
1092 | carl9170_usb_cancel_urbs(ar); | |
1093 | ||
1094 | /* | |
1095 | * firmware automatically reboots for usb suspend. | |
1096 | */ | |
1097 | ||
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | static int carl9170_usb_resume(struct usb_interface *intf) | |
1102 | { | |
1103 | struct ar9170 *ar = usb_get_intfdata(intf); | |
1104 | int err; | |
1105 | ||
1106 | if (!ar) | |
1107 | return -ENODEV; | |
1108 | ||
1109 | usb_unpoison_anchored_urbs(&ar->rx_anch); | |
1110 | ||
1111 | err = carl9170_usb_init_device(ar); | |
1112 | if (err) | |
1113 | goto err_unrx; | |
1114 | ||
1115 | err = carl9170_usb_open(ar); | |
1116 | if (err) | |
1117 | goto err_unrx; | |
1118 | ||
1119 | return 0; | |
1120 | ||
1121 | err_unrx: | |
1122 | carl9170_usb_cancel_urbs(ar); | |
1123 | ||
1124 | return err; | |
1125 | } | |
1126 | #endif /* CONFIG_PM */ | |
1127 | ||
1128 | static struct usb_driver carl9170_driver = { | |
1129 | .name = KBUILD_MODNAME, | |
1130 | .probe = carl9170_usb_probe, | |
1131 | .disconnect = carl9170_usb_disconnect, | |
1132 | .id_table = carl9170_usb_ids, | |
1133 | .soft_unbind = 1, | |
1134 | #ifdef CONFIG_PM | |
1135 | .suspend = carl9170_usb_suspend, | |
1136 | .resume = carl9170_usb_resume, | |
1137 | #endif /* CONFIG_PM */ | |
1138 | }; | |
1139 | ||
1140 | static int __init carl9170_usb_init(void) | |
1141 | { | |
1142 | return usb_register(&carl9170_driver); | |
1143 | } | |
1144 | ||
1145 | static void __exit carl9170_usb_exit(void) | |
1146 | { | |
1147 | usb_deregister(&carl9170_driver); | |
1148 | } | |
1149 | ||
1150 | module_init(carl9170_usb_init); | |
1151 | module_exit(carl9170_usb_exit); |