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