blob: 2d947a30d29e5fcc495e3a2a8fe930108c330231 [file] [log] [blame]
Christian Lampartera84fab32010-09-06 01:09:20 +02001/*
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
52MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
53MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>");
54MODULE_LICENSE("GPL");
55MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
56MODULE_FIRMWARE(CARL9170FW_NAME);
57MODULE_ALIAS("ar9170usb");
58MODULE_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 */
67static 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) },
Christian Lamparter8df86db2010-10-30 19:46:37 +020085 /* D-Link DWA 130 D */
86 { USB_DEVICE(0x07d1, 0x3a0f) },
Christian Lampartera84fab32010-09-06 01:09:20 +020087 /* Netgear WNA1000 */
88 { USB_DEVICE(0x0846, 0x9040) },
Christian Lamparter8df86db2010-10-30 19:46:37 +020089 /* Netgear WNDA3100 (v1) */
Christian Lampartera84fab32010-09-06 01:09:20 +020090 { 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};
125MODULE_DEVICE_TABLE(usb, carl9170_usb_ids);
126
127static 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
157err_acc:
158 atomic_dec(&ar->tx_anch_urbs);
159}
160
161static void carl9170_usb_tx_data_complete(struct urb *urb)
162{
Joe Perchesb2767362010-11-30 13:42:08 -0800163 struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
Christian Lampartera84fab32010-09-06 01:09:20 +0200164
165 if (WARN_ON_ONCE(!ar)) {
166 dev_kfree_skb_irq(urb->context);
167 return;
168 }
169
170 atomic_dec(&ar->tx_anch_urbs);
171
172 switch (urb->status) {
173 /* everything is fine */
174 case 0:
175 carl9170_tx_callback(ar, (void *)urb->context);
176 break;
177
178 /* disconnect */
179 case -ENOENT:
180 case -ECONNRESET:
181 case -ENODEV:
182 case -ESHUTDOWN:
183 /*
184 * Defer the frame clean-up to the tasklet worker.
185 * This is necessary, because carl9170_tx_drop
186 * does not work in an irqsave context.
187 */
188 usb_anchor_urb(urb, &ar->tx_err);
189 return;
190
191 /* a random transmission error has occurred? */
192 default:
193 if (net_ratelimit()) {
194 dev_err(&ar->udev->dev, "tx failed (%d)\n",
195 urb->status);
196 }
197
198 usb_anchor_urb(urb, &ar->tx_err);
199 break;
200 }
201
202 if (likely(IS_STARTED(ar)))
203 carl9170_usb_submit_data_urb(ar);
204}
205
206static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
207{
208 struct urb *urb;
209 int err;
210
211 if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
212 atomic_dec(&ar->tx_cmd_urbs);
213 return 0;
214 }
215
216 urb = usb_get_from_anchor(&ar->tx_cmd);
217 if (!urb) {
218 atomic_dec(&ar->tx_cmd_urbs);
219 return 0;
220 }
221
222 usb_anchor_urb(urb, &ar->tx_anch);
223 err = usb_submit_urb(urb, GFP_ATOMIC);
224 if (unlikely(err)) {
225 usb_unanchor_urb(urb);
226 atomic_dec(&ar->tx_cmd_urbs);
227 }
228 usb_free_urb(urb);
229
230 return err;
231}
232
233static void carl9170_usb_cmd_complete(struct urb *urb)
234{
235 struct ar9170 *ar = urb->context;
236 int err = 0;
237
238 if (WARN_ON_ONCE(!ar))
239 return;
240
241 atomic_dec(&ar->tx_cmd_urbs);
242
243 switch (urb->status) {
244 /* everything is fine */
245 case 0:
246 break;
247
248 /* disconnect */
249 case -ENOENT:
250 case -ECONNRESET:
251 case -ENODEV:
252 case -ESHUTDOWN:
253 return;
254
255 default:
256 err = urb->status;
257 break;
258 }
259
260 if (!IS_INITIALIZED(ar))
261 return;
262
263 if (err)
264 dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
265
266 err = carl9170_usb_submit_cmd_urb(ar);
267 if (err)
268 dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
269}
270
271static void carl9170_usb_rx_irq_complete(struct urb *urb)
272{
273 struct ar9170 *ar = urb->context;
274
275 if (WARN_ON_ONCE(!ar))
276 return;
277
278 switch (urb->status) {
279 /* everything is fine */
280 case 0:
281 break;
282
283 /* disconnect */
284 case -ENOENT:
285 case -ECONNRESET:
286 case -ENODEV:
287 case -ESHUTDOWN:
288 return;
289
290 default:
291 goto resubmit;
292 }
293
294 carl9170_handle_command_response(ar, urb->transfer_buffer,
295 urb->actual_length);
296
297resubmit:
298 usb_anchor_urb(urb, &ar->rx_anch);
299 if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
300 usb_unanchor_urb(urb);
301}
302
303static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
304{
305 struct urb *urb;
306 int err = 0, runs = 0;
307
308 while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
309 (runs++ < AR9170_NUM_RX_URBS)) {
310 err = -ENOSPC;
311 urb = usb_get_from_anchor(&ar->rx_pool);
312 if (urb) {
313 usb_anchor_urb(urb, &ar->rx_anch);
314 err = usb_submit_urb(urb, gfp);
315 if (unlikely(err)) {
316 usb_unanchor_urb(urb);
317 usb_anchor_urb(urb, &ar->rx_pool);
318 } else {
319 atomic_dec(&ar->rx_pool_urbs);
320 atomic_inc(&ar->rx_anch_urbs);
321 }
322 usb_free_urb(urb);
323 }
324 }
325
326 return err;
327}
328
329static void carl9170_usb_rx_work(struct ar9170 *ar)
330{
331 struct urb *urb;
332 int i;
333
334 for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
335 urb = usb_get_from_anchor(&ar->rx_work);
336 if (!urb)
337 break;
338
339 atomic_dec(&ar->rx_work_urbs);
340 if (IS_INITIALIZED(ar)) {
341 carl9170_rx(ar, urb->transfer_buffer,
342 urb->actual_length);
343 }
344
345 usb_anchor_urb(urb, &ar->rx_pool);
346 atomic_inc(&ar->rx_pool_urbs);
347
348 usb_free_urb(urb);
349
350 carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
351 }
352}
353
354void carl9170_usb_handle_tx_err(struct ar9170 *ar)
355{
356 struct urb *urb;
357
358 while ((urb = usb_get_from_anchor(&ar->tx_err))) {
359 struct sk_buff *skb = (void *)urb->context;
360
361 carl9170_tx_drop(ar, skb);
362 carl9170_tx_callback(ar, skb);
363 usb_free_urb(urb);
364 }
365}
366
367static void carl9170_usb_tasklet(unsigned long data)
368{
369 struct ar9170 *ar = (struct ar9170 *) data;
370
Christian Lampartercf6487d2010-09-17 22:47:28 +0200371 if (!IS_INITIALIZED(ar))
372 return;
373
Christian Lampartera84fab32010-09-06 01:09:20 +0200374 carl9170_usb_rx_work(ar);
375
376 /*
377 * Strictly speaking: The tx scheduler is not part of the USB system.
378 * But the rx worker returns frames back to the mac80211-stack and
379 * this is the _perfect_ place to generate the next transmissions.
380 */
381 if (IS_STARTED(ar))
382 carl9170_tx_scheduler(ar);
383}
384
385static void carl9170_usb_rx_complete(struct urb *urb)
386{
387 struct ar9170 *ar = (struct ar9170 *)urb->context;
388 int err;
389
390 if (WARN_ON_ONCE(!ar))
391 return;
392
393 atomic_dec(&ar->rx_anch_urbs);
394
395 switch (urb->status) {
396 case 0:
397 /* rx path */
398 usb_anchor_urb(urb, &ar->rx_work);
399 atomic_inc(&ar->rx_work_urbs);
400 break;
401
402 case -ENOENT:
403 case -ECONNRESET:
404 case -ENODEV:
405 case -ESHUTDOWN:
406 /* handle disconnect events*/
407 return;
408
409 default:
410 /* handle all other errors */
411 usb_anchor_urb(urb, &ar->rx_pool);
412 atomic_inc(&ar->rx_pool_urbs);
413 break;
414 }
415
416 err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
417 if (unlikely(err)) {
418 /*
419 * usb_submit_rx_urb reported a problem.
420 * In case this is due to a rx buffer shortage,
421 * elevate the tasklet worker priority to
422 * the highest available level.
423 */
424 tasklet_hi_schedule(&ar->usb_tasklet);
425
426 if (atomic_read(&ar->rx_anch_urbs) == 0) {
427 /*
428 * The system is too slow to cope with
429 * the enormous workload. We have simply
430 * run out of active rx urbs and this
431 * unfortunatly leads to an unpredictable
432 * device.
433 */
434
Christian Lampartere4a668c2010-10-29 23:26:13 +0200435 ieee80211_queue_work(ar->hw, &ar->ping_work);
Christian Lampartera84fab32010-09-06 01:09:20 +0200436 }
437 } else {
438 /*
439 * Using anything less than _high_ priority absolutely
440 * kills the rx performance my UP-System...
441 */
442 tasklet_hi_schedule(&ar->usb_tasklet);
443 }
444}
445
446static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
447{
448 struct urb *urb;
449 void *buf;
450
451 buf = kmalloc(ar->fw.rx_size, gfp);
452 if (!buf)
453 return NULL;
454
455 urb = usb_alloc_urb(0, gfp);
456 if (!urb) {
457 kfree(buf);
458 return NULL;
459 }
460
461 usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev,
462 AR9170_USB_EP_RX), buf, ar->fw.rx_size,
463 carl9170_usb_rx_complete, ar);
464
465 urb->transfer_flags |= URB_FREE_BUFFER;
466
467 return urb;
468}
469
470static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
471{
472 struct urb *urb = NULL;
473 void *ibuf;
474 int err = -ENOMEM;
475
476 urb = usb_alloc_urb(0, GFP_KERNEL);
477 if (!urb)
478 goto out;
479
480 ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
481 if (!ibuf)
482 goto out;
483
484 usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev,
485 AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX,
486 carl9170_usb_rx_irq_complete, ar, 1);
487
488 urb->transfer_flags |= URB_FREE_BUFFER;
489
490 usb_anchor_urb(urb, &ar->rx_anch);
491 err = usb_submit_urb(urb, GFP_KERNEL);
492 if (err)
493 usb_unanchor_urb(urb);
494
495out:
496 usb_free_urb(urb);
497 return err;
498}
499
500static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
501{
502 struct urb *urb;
503 int i, err = -EINVAL;
504
505 /*
506 * The driver actively maintains a second shadow
507 * pool for inactive, but fully-prepared rx urbs.
508 *
509 * The pool should help the driver to master huge
510 * workload spikes without running the risk of
511 * undersupplying the hardware or wasting time by
512 * processing rx data (streams) inside the urb
513 * completion (hardirq context).
514 */
515 for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
516 urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
517 if (!urb) {
518 err = -ENOMEM;
519 goto err_out;
520 }
521
522 usb_anchor_urb(urb, &ar->rx_pool);
523 atomic_inc(&ar->rx_pool_urbs);
524 usb_free_urb(urb);
525 }
526
527 err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
528 if (err)
529 goto err_out;
530
531 /* the device now waiting for the firmware. */
532 carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
533 return 0;
534
535err_out:
536
537 usb_scuttle_anchored_urbs(&ar->rx_pool);
538 usb_scuttle_anchored_urbs(&ar->rx_work);
539 usb_kill_anchored_urbs(&ar->rx_anch);
540 return err;
541}
542
543static int carl9170_usb_flush(struct ar9170 *ar)
544{
545 struct urb *urb;
546 int ret, err = 0;
547
548 while ((urb = usb_get_from_anchor(&ar->tx_wait))) {
549 struct sk_buff *skb = (void *)urb->context;
550 carl9170_tx_drop(ar, skb);
551 carl9170_tx_callback(ar, skb);
552 usb_free_urb(urb);
553 }
554
Christian Lamparterdfa31fe2010-11-15 15:11:26 +0100555 ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000);
Christian Lampartera84fab32010-09-06 01:09:20 +0200556 if (ret == 0)
557 err = -ETIMEDOUT;
558
559 /* lets wait a while until the tx - queues are dried out */
Christian Lamparterdfa31fe2010-11-15 15:11:26 +0100560 ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000);
Christian Lampartera84fab32010-09-06 01:09:20 +0200561 if (ret == 0)
562 err = -ETIMEDOUT;
563
564 usb_kill_anchored_urbs(&ar->tx_anch);
565 carl9170_usb_handle_tx_err(ar);
566
567 return err;
568}
569
570static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
571{
572 int err;
573
574 carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
575
576 err = carl9170_usb_flush(ar);
577 if (err)
578 dev_err(&ar->udev->dev, "stuck tx urbs!\n");
579
580 usb_poison_anchored_urbs(&ar->tx_anch);
581 carl9170_usb_handle_tx_err(ar);
582 usb_poison_anchored_urbs(&ar->rx_anch);
583
584 tasklet_kill(&ar->usb_tasklet);
585
586 usb_scuttle_anchored_urbs(&ar->rx_work);
587 usb_scuttle_anchored_urbs(&ar->rx_pool);
588 usb_scuttle_anchored_urbs(&ar->tx_cmd);
589}
590
591int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
592 const bool free_buf)
593{
594 struct urb *urb;
Christian Lampartercae7f952010-10-16 01:38:46 +0200595 int err = 0;
Christian Lampartera84fab32010-09-06 01:09:20 +0200596
Christian Lampartercae7f952010-10-16 01:38:46 +0200597 if (!IS_INITIALIZED(ar)) {
598 err = -EPERM;
599 goto err_free;
600 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200601
Christian Lampartercae7f952010-10-16 01:38:46 +0200602 if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) {
603 err = -EINVAL;
604 goto err_free;
605 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200606
607 urb = usb_alloc_urb(0, GFP_ATOMIC);
Christian Lampartercae7f952010-10-16 01:38:46 +0200608 if (!urb) {
609 err = -ENOMEM;
610 goto err_free;
611 }
Christian Lampartera84fab32010-09-06 01:09:20 +0200612
613 usb_fill_int_urb(urb, ar->udev, usb_sndintpipe(ar->udev,
614 AR9170_USB_EP_CMD), cmd, cmd->hdr.len + 4,
615 carl9170_usb_cmd_complete, ar, 1);
616
Christian Lampartera84fab32010-09-06 01:09:20 +0200617 if (free_buf)
618 urb->transfer_flags |= URB_FREE_BUFFER;
619
620 usb_anchor_urb(urb, &ar->tx_cmd);
621 usb_free_urb(urb);
622
623 return carl9170_usb_submit_cmd_urb(ar);
Christian Lampartercae7f952010-10-16 01:38:46 +0200624
625err_free:
626 if (free_buf)
627 kfree(cmd);
628
629 return err;
Christian Lampartera84fab32010-09-06 01:09:20 +0200630}
631
632int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
633 unsigned int plen, void *payload, unsigned int outlen, void *out)
634{
635 int err = -ENOMEM;
636
637 if (!IS_ACCEPTING_CMD(ar))
638 return -EIO;
639
640 if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
641 might_sleep();
642
643 ar->cmd.hdr.len = plen;
644 ar->cmd.hdr.cmd = cmd;
645 /* writing multiple regs fills this buffer already */
646 if (plen && payload != (u8 *)(ar->cmd.data))
647 memcpy(ar->cmd.data, payload, plen);
648
649 spin_lock_bh(&ar->cmd_lock);
650 ar->readbuf = (u8 *)out;
651 ar->readlen = outlen;
652 spin_unlock_bh(&ar->cmd_lock);
653
654 err = __carl9170_exec_cmd(ar, &ar->cmd, false);
655
656 if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
657 err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
658 if (err == 0) {
659 err = -ETIMEDOUT;
660 goto err_unbuf;
661 }
662
663 if (ar->readlen != outlen) {
664 err = -EMSGSIZE;
665 goto err_unbuf;
666 }
667 }
668
669 return 0;
670
671err_unbuf:
672 /* Maybe the device was removed in the moment we were waiting? */
673 if (IS_STARTED(ar)) {
674 dev_err(&ar->udev->dev, "no command feedback "
675 "received (%d).\n", err);
676
677 /* provide some maybe useful debug information */
678 print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
679 &ar->cmd, plen + 4);
680
681 carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
682 }
683
684 /* invalidate to avoid completing the next command prematurely */
685 spin_lock_bh(&ar->cmd_lock);
686 ar->readbuf = NULL;
687 ar->readlen = 0;
688 spin_unlock_bh(&ar->cmd_lock);
689
690 return err;
691}
692
693void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
694{
695 struct urb *urb;
696 struct ar9170_stream *tx_stream;
697 void *data;
698 unsigned int len;
699
700 if (!IS_STARTED(ar))
701 goto err_drop;
702
703 urb = usb_alloc_urb(0, GFP_ATOMIC);
704 if (!urb)
705 goto err_drop;
706
707 if (ar->fw.tx_stream) {
708 tx_stream = (void *) (skb->data - sizeof(*tx_stream));
709
710 len = skb->len + sizeof(*tx_stream);
711 tx_stream->length = cpu_to_le16(len);
712 tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
713 data = tx_stream;
714 } else {
715 data = skb->data;
716 len = skb->len;
717 }
718
719 usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
720 AR9170_USB_EP_TX), data, len,
721 carl9170_usb_tx_data_complete, skb);
722
723 urb->transfer_flags |= URB_ZERO_PACKET;
724
725 usb_anchor_urb(urb, &ar->tx_wait);
726
727 usb_free_urb(urb);
728
729 carl9170_usb_submit_data_urb(ar);
730 return;
731
732err_drop:
733 carl9170_tx_drop(ar, skb);
734 carl9170_tx_callback(ar, skb);
735}
736
737static void carl9170_release_firmware(struct ar9170 *ar)
738{
739 if (ar->fw.fw) {
740 release_firmware(ar->fw.fw);
741 memset(&ar->fw, 0, sizeof(ar->fw));
742 }
743}
744
745void carl9170_usb_stop(struct ar9170 *ar)
746{
747 int ret;
748
749 carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
750
751 ret = carl9170_usb_flush(ar);
752 if (ret)
753 dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
754
755 usb_poison_anchored_urbs(&ar->tx_anch);
756 carl9170_usb_handle_tx_err(ar);
757
758 /* kill any pending command */
759 spin_lock_bh(&ar->cmd_lock);
760 ar->readlen = 0;
761 spin_unlock_bh(&ar->cmd_lock);
762 complete_all(&ar->cmd_wait);
763
764 /* This is required to prevent an early completion on _start */
765 INIT_COMPLETION(ar->cmd_wait);
766
767 /*
768 * Note:
769 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
770 * Else we would end up with a unresponsive device...
771 */
772}
773
774int carl9170_usb_open(struct ar9170 *ar)
775{
776 usb_unpoison_anchored_urbs(&ar->tx_anch);
777
778 carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
779 return 0;
780}
781
782static int carl9170_usb_load_firmware(struct ar9170 *ar)
783{
784 const u8 *data;
785 u8 *buf;
786 unsigned int transfer;
787 size_t len;
788 u32 addr;
789 int err = 0;
790
791 buf = kmalloc(4096, GFP_KERNEL);
792 if (!buf) {
793 err = -ENOMEM;
794 goto err_out;
795 }
796
797 data = ar->fw.fw->data;
798 len = ar->fw.fw->size;
799 addr = ar->fw.address;
800
801 /* this removes the miniboot image */
802 data += ar->fw.offset;
803 len -= ar->fw.offset;
804
805 while (len) {
806 transfer = min_t(unsigned int, len, 4096u);
807 memcpy(buf, data, transfer);
808
809 err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
810 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
811 addr >> 8, 0, buf, transfer, 100);
812
813 if (err < 0) {
814 kfree(buf);
815 goto err_out;
816 }
817
818 len -= transfer;
819 data += transfer;
820 addr += transfer;
821 }
822 kfree(buf);
823
824 err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
825 0x31 /* FW DL COMPLETE */,
826 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
827
828 if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
829 err = -ETIMEDOUT;
830 goto err_out;
831 }
832
833 err = carl9170_echo_test(ar, 0x4a110123);
834 if (err)
835 goto err_out;
836
837 /* firmware restarts cmd counter */
838 ar->cmd_seq = -1;
839
840 return 0;
841
842err_out:
843 dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
844 return err;
845}
846
847int carl9170_usb_restart(struct ar9170 *ar)
848{
849 int err = 0;
850
851 if (ar->intf->condition != USB_INTERFACE_BOUND)
852 return 0;
853
854 /* Disable command response sequence counter. */
855 ar->cmd_seq = -2;
856
857 err = carl9170_reboot(ar);
858
859 carl9170_usb_stop(ar);
860
Christian Lampartera84fab32010-09-06 01:09:20 +0200861 if (err)
862 goto err_out;
863
864 tasklet_schedule(&ar->usb_tasklet);
865
866 /* The reboot procedure can take quite a while to complete. */
867 msleep(1100);
868
869 err = carl9170_usb_open(ar);
870 if (err)
871 goto err_out;
872
873 err = carl9170_usb_load_firmware(ar);
874 if (err)
875 goto err_out;
876
877 return 0;
878
879err_out:
880 carl9170_usb_cancel_urbs(ar);
881 return err;
882}
883
884void carl9170_usb_reset(struct ar9170 *ar)
885{
886 /*
887 * This is the last resort to get the device going again
888 * without any *user replugging action*.
889 *
890 * But there is a catch: usb_reset really is like a physical
891 * *reconnect*. The mac80211 state will be lost in the process.
892 * Therefore a userspace application, which is monitoring
893 * the link must step in.
894 */
895 carl9170_usb_cancel_urbs(ar);
896
897 carl9170_usb_stop(ar);
898
899 usb_queue_reset_device(ar->intf);
900}
901
902static int carl9170_usb_init_device(struct ar9170 *ar)
903{
904 int err;
905
906 err = carl9170_usb_send_rx_irq_urb(ar);
907 if (err)
908 goto err_out;
909
910 err = carl9170_usb_init_rx_bulk_urbs(ar);
911 if (err)
912 goto err_unrx;
913
914 mutex_lock(&ar->mutex);
915 err = carl9170_usb_load_firmware(ar);
916 mutex_unlock(&ar->mutex);
917 if (err)
918 goto err_unrx;
919
920 return 0;
921
922err_unrx:
923 carl9170_usb_cancel_urbs(ar);
924
925err_out:
926 return err;
927}
928
929static void carl9170_usb_firmware_failed(struct ar9170 *ar)
930{
931 struct device *parent = ar->udev->dev.parent;
932 struct usb_device *udev;
933
934 /*
935 * Store a copy of the usb_device pointer locally.
936 * This is because device_release_driver initiates
937 * carl9170_usb_disconnect, which in turn frees our
938 * driver context (ar).
939 */
940 udev = ar->udev;
941
942 complete(&ar->fw_load_wait);
943
944 /* unbind anything failed */
945 if (parent)
946 device_lock(parent);
947
948 device_release_driver(&udev->dev);
949 if (parent)
950 device_unlock(parent);
951
952 usb_put_dev(udev);
953}
954
955static void carl9170_usb_firmware_finish(struct ar9170 *ar)
956{
957 int err;
958
959 err = carl9170_parse_firmware(ar);
960 if (err)
961 goto err_freefw;
962
963 err = carl9170_usb_init_device(ar);
964 if (err)
965 goto err_freefw;
966
967 err = carl9170_usb_open(ar);
968 if (err)
969 goto err_unrx;
970
971 err = carl9170_register(ar);
972
973 carl9170_usb_stop(ar);
974 if (err)
975 goto err_unrx;
976
977 complete(&ar->fw_load_wait);
978 usb_put_dev(ar->udev);
979 return;
980
981err_unrx:
982 carl9170_usb_cancel_urbs(ar);
983
984err_freefw:
985 carl9170_release_firmware(ar);
986 carl9170_usb_firmware_failed(ar);
987}
988
989static void carl9170_usb_firmware_step2(const struct firmware *fw,
990 void *context)
991{
992 struct ar9170 *ar = context;
993
994 if (fw) {
995 ar->fw.fw = fw;
996 carl9170_usb_firmware_finish(ar);
997 return;
998 }
999
1000 dev_err(&ar->udev->dev, "firmware not found.\n");
1001 carl9170_usb_firmware_failed(ar);
1002}
1003
1004static int carl9170_usb_probe(struct usb_interface *intf,
1005 const struct usb_device_id *id)
1006{
1007 struct ar9170 *ar;
1008 struct usb_device *udev;
1009 int err;
1010
1011 err = usb_reset_device(interface_to_usbdev(intf));
1012 if (err)
1013 return err;
1014
1015 ar = carl9170_alloc(sizeof(*ar));
1016 if (IS_ERR(ar))
1017 return PTR_ERR(ar);
1018
1019 udev = interface_to_usbdev(intf);
1020 usb_get_dev(udev);
1021 ar->udev = udev;
1022 ar->intf = intf;
1023 ar->features = id->driver_info;
1024
1025 usb_set_intfdata(intf, ar);
1026 SET_IEEE80211_DEV(ar->hw, &intf->dev);
1027
1028 init_usb_anchor(&ar->rx_anch);
1029 init_usb_anchor(&ar->rx_pool);
1030 init_usb_anchor(&ar->rx_work);
1031 init_usb_anchor(&ar->tx_wait);
1032 init_usb_anchor(&ar->tx_anch);
1033 init_usb_anchor(&ar->tx_cmd);
1034 init_usb_anchor(&ar->tx_err);
1035 init_completion(&ar->cmd_wait);
1036 init_completion(&ar->fw_boot_wait);
1037 init_completion(&ar->fw_load_wait);
1038 tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet,
1039 (unsigned long)ar);
1040
1041 atomic_set(&ar->tx_cmd_urbs, 0);
1042 atomic_set(&ar->tx_anch_urbs, 0);
1043 atomic_set(&ar->rx_work_urbs, 0);
1044 atomic_set(&ar->rx_anch_urbs, 0);
1045 atomic_set(&ar->rx_pool_urbs, 0);
1046 ar->cmd_seq = -2;
1047
1048 usb_get_dev(ar->udev);
1049
1050 carl9170_set_state(ar, CARL9170_STOPPED);
1051
1052 return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
1053 &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
1054}
1055
1056static void carl9170_usb_disconnect(struct usb_interface *intf)
1057{
1058 struct ar9170 *ar = usb_get_intfdata(intf);
1059 struct usb_device *udev;
1060
1061 if (WARN_ON(!ar))
1062 return;
1063
1064 udev = ar->udev;
1065 wait_for_completion(&ar->fw_load_wait);
1066
1067 if (IS_INITIALIZED(ar)) {
1068 carl9170_reboot(ar);
1069 carl9170_usb_stop(ar);
1070 }
1071
1072 carl9170_usb_cancel_urbs(ar);
1073 carl9170_unregister(ar);
1074
1075 usb_set_intfdata(intf, NULL);
1076
1077 carl9170_release_firmware(ar);
1078 carl9170_free(ar);
1079 usb_put_dev(udev);
1080}
1081
1082#ifdef CONFIG_PM
1083static int carl9170_usb_suspend(struct usb_interface *intf,
1084 pm_message_t message)
1085{
1086 struct ar9170 *ar = usb_get_intfdata(intf);
1087
1088 if (!ar)
1089 return -ENODEV;
1090
1091 carl9170_usb_cancel_urbs(ar);
1092
1093 /*
1094 * firmware automatically reboots for usb suspend.
1095 */
1096
1097 return 0;
1098}
1099
1100static int carl9170_usb_resume(struct usb_interface *intf)
1101{
1102 struct ar9170 *ar = usb_get_intfdata(intf);
1103 int err;
1104
1105 if (!ar)
1106 return -ENODEV;
1107
1108 usb_unpoison_anchored_urbs(&ar->rx_anch);
1109
1110 err = carl9170_usb_init_device(ar);
1111 if (err)
1112 goto err_unrx;
1113
1114 err = carl9170_usb_open(ar);
1115 if (err)
1116 goto err_unrx;
1117
1118 return 0;
1119
1120err_unrx:
1121 carl9170_usb_cancel_urbs(ar);
1122
1123 return err;
1124}
1125#endif /* CONFIG_PM */
1126
1127static struct usb_driver carl9170_driver = {
1128 .name = KBUILD_MODNAME,
1129 .probe = carl9170_usb_probe,
1130 .disconnect = carl9170_usb_disconnect,
1131 .id_table = carl9170_usb_ids,
1132 .soft_unbind = 1,
1133#ifdef CONFIG_PM
1134 .suspend = carl9170_usb_suspend,
1135 .resume = carl9170_usb_resume,
1136#endif /* CONFIG_PM */
1137};
1138
1139static int __init carl9170_usb_init(void)
1140{
1141 return usb_register(&carl9170_driver);
1142}
1143
1144static void __exit carl9170_usb_exit(void)
1145{
1146 usb_deregister(&carl9170_driver);
1147}
1148
1149module_init(carl9170_usb_init);
1150module_exit(carl9170_usb_exit);