2 * Atheros CARL9170 driver
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
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.
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.
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/.
23 * This file incorporates work covered by the following copyright and
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
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.
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.
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>
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");
63 * Always update our wiki's device list (located at:
64 * https://wireless.wiki.kernel.org/en/users/Drivers/ar9170/devices ),
65 * whenever you add a new device.
67 static const struct usb_device_id carl9170_usb_ids
[] = {
69 { USB_DEVICE(0x0cf3, 0x9170) },
71 { USB_DEVICE(0x0cf3, 0x1001) },
72 /* TP-Link TL-WN821N v2 */
73 { USB_DEVICE(0x0cf3, 0x1002), .driver_info
= CARL9170_WPS_BUTTON
|
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) },
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 /* D-Link DWA 130 D */
86 { USB_DEVICE(0x07d1, 0x3a0f) },
88 { USB_DEVICE(0x0846, 0x9040) },
89 /* Netgear WNDA3100 (v1) */
90 { USB_DEVICE(0x0846, 0x9010) },
91 /* Netgear WN111 v2 */
92 { USB_DEVICE(0x0846, 0x9001), .driver_info
= CARL9170_ONE_LED
},
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) },
100 { USB_DEVICE(0x0586, 0x3417) },
102 { USB_DEVICE(0x0cde, 0x0023) },
104 { USB_DEVICE(0x0cde, 0x0026) },
105 /* Sphairon Homelink 1202 */
106 { USB_DEVICE(0x0cde, 0x0027) },
107 /* Arcadyan WN7512 */
108 { USB_DEVICE(0x083a, 0xf522) },
110 { USB_DEVICE(0x2019, 0x5304) },
111 /* IO-Data WNGDNUS2 */
112 { USB_DEVICE(0x04bb, 0x093f) },
114 { USB_DEVICE(0x0409, 0x0249) },
116 { USB_DEVICE(0x0409, 0x02b4) },
117 /* AVM FRITZ!WLAN USB Stick N */
118 { USB_DEVICE(0x057c, 0x8401) },
119 /* AVM FRITZ!WLAN USB Stick N 2.4 */
120 { USB_DEVICE(0x057c, 0x8402) },
121 /* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
122 { USB_DEVICE(0x1668, 0x1200) },
123 /* Airlive X.USB a/b/g/n */
124 { USB_DEVICE(0x1b75, 0x9170) },
129 MODULE_DEVICE_TABLE(usb
, carl9170_usb_ids
);
131 static struct usb_driver carl9170_driver
;
133 static void carl9170_usb_submit_data_urb(struct ar9170
*ar
)
138 if (atomic_inc_return(&ar
->tx_anch_urbs
) > AR9170_NUM_TX_URBS
)
141 urb
= usb_get_from_anchor(&ar
->tx_wait
);
145 usb_anchor_urb(urb
, &ar
->tx_anch
);
147 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
149 if (net_ratelimit()) {
150 dev_err(&ar
->udev
->dev
, "tx submit failed (%d)\n",
154 usb_unanchor_urb(urb
);
155 usb_anchor_urb(urb
, &ar
->tx_err
);
160 if (likely(err
== 0))
164 atomic_dec(&ar
->tx_anch_urbs
);
167 static void carl9170_usb_tx_data_complete(struct urb
*urb
)
169 struct ar9170
*ar
= usb_get_intfdata(usb_ifnum_to_if(urb
->dev
, 0));
171 if (WARN_ON_ONCE(!ar
)) {
172 dev_kfree_skb_irq(urb
->context
);
176 atomic_dec(&ar
->tx_anch_urbs
);
178 switch (urb
->status
) {
179 /* everything is fine */
181 carl9170_tx_callback(ar
, (void *)urb
->context
);
190 * Defer the frame clean-up to the tasklet worker.
191 * This is necessary, because carl9170_tx_drop
192 * does not work in an irqsave context.
194 usb_anchor_urb(urb
, &ar
->tx_err
);
197 /* a random transmission error has occurred? */
199 if (net_ratelimit()) {
200 dev_err(&ar
->udev
->dev
, "tx failed (%d)\n",
204 usb_anchor_urb(urb
, &ar
->tx_err
);
208 if (likely(IS_STARTED(ar
)))
209 carl9170_usb_submit_data_urb(ar
);
212 static int carl9170_usb_submit_cmd_urb(struct ar9170
*ar
)
217 if (atomic_inc_return(&ar
->tx_cmd_urbs
) != 1) {
218 atomic_dec(&ar
->tx_cmd_urbs
);
222 urb
= usb_get_from_anchor(&ar
->tx_cmd
);
224 atomic_dec(&ar
->tx_cmd_urbs
);
228 usb_anchor_urb(urb
, &ar
->tx_anch
);
229 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
231 usb_unanchor_urb(urb
);
232 atomic_dec(&ar
->tx_cmd_urbs
);
239 static void carl9170_usb_cmd_complete(struct urb
*urb
)
241 struct ar9170
*ar
= urb
->context
;
244 if (WARN_ON_ONCE(!ar
))
247 atomic_dec(&ar
->tx_cmd_urbs
);
249 switch (urb
->status
) {
250 /* everything is fine */
266 if (!IS_INITIALIZED(ar
))
270 dev_err(&ar
->udev
->dev
, "submit cmd cb failed (%d).\n", err
);
272 err
= carl9170_usb_submit_cmd_urb(ar
);
274 dev_err(&ar
->udev
->dev
, "submit cmd failed (%d).\n", err
);
277 static void carl9170_usb_rx_irq_complete(struct urb
*urb
)
279 struct ar9170
*ar
= urb
->context
;
281 if (WARN_ON_ONCE(!ar
))
284 switch (urb
->status
) {
285 /* everything is fine */
301 * While the carl9170 firmware does not use this EP, the
302 * firmware loader in the EEPROM unfortunately does.
303 * Therefore we need to be ready to handle out-of-band
304 * responses and traps in case the firmware crashed and
305 * the loader took over again.
307 carl9170_handle_command_response(ar
, urb
->transfer_buffer
,
311 usb_anchor_urb(urb
, &ar
->rx_anch
);
312 if (unlikely(usb_submit_urb(urb
, GFP_ATOMIC
)))
313 usb_unanchor_urb(urb
);
316 static int carl9170_usb_submit_rx_urb(struct ar9170
*ar
, gfp_t gfp
)
319 int err
= 0, runs
= 0;
321 while ((atomic_read(&ar
->rx_anch_urbs
) < AR9170_NUM_RX_URBS
) &&
322 (runs
++ < AR9170_NUM_RX_URBS
)) {
324 urb
= usb_get_from_anchor(&ar
->rx_pool
);
326 usb_anchor_urb(urb
, &ar
->rx_anch
);
327 err
= usb_submit_urb(urb
, gfp
);
329 usb_unanchor_urb(urb
);
330 usb_anchor_urb(urb
, &ar
->rx_pool
);
332 atomic_dec(&ar
->rx_pool_urbs
);
333 atomic_inc(&ar
->rx_anch_urbs
);
342 static void carl9170_usb_rx_work(struct ar9170
*ar
)
347 for (i
= 0; i
< AR9170_NUM_RX_URBS_POOL
; i
++) {
348 urb
= usb_get_from_anchor(&ar
->rx_work
);
352 atomic_dec(&ar
->rx_work_urbs
);
353 if (IS_INITIALIZED(ar
)) {
354 carl9170_rx(ar
, urb
->transfer_buffer
,
358 usb_anchor_urb(urb
, &ar
->rx_pool
);
359 atomic_inc(&ar
->rx_pool_urbs
);
363 carl9170_usb_submit_rx_urb(ar
, GFP_ATOMIC
);
367 void carl9170_usb_handle_tx_err(struct ar9170
*ar
)
371 while ((urb
= usb_get_from_anchor(&ar
->tx_err
))) {
372 struct sk_buff
*skb
= (void *)urb
->context
;
374 carl9170_tx_drop(ar
, skb
);
375 carl9170_tx_callback(ar
, skb
);
380 static void carl9170_usb_tasklet(struct tasklet_struct
*t
)
382 struct ar9170
*ar
= from_tasklet(ar
, t
, usb_tasklet
);
384 if (!IS_INITIALIZED(ar
))
387 carl9170_usb_rx_work(ar
);
390 * Strictly speaking: The tx scheduler is not part of the USB system.
391 * But the rx worker returns frames back to the mac80211-stack and
392 * this is the _perfect_ place to generate the next transmissions.
395 carl9170_tx_scheduler(ar
);
398 static void carl9170_usb_rx_complete(struct urb
*urb
)
400 struct ar9170
*ar
= (struct ar9170
*)urb
->context
;
403 if (WARN_ON_ONCE(!ar
))
406 atomic_dec(&ar
->rx_anch_urbs
);
408 switch (urb
->status
) {
411 usb_anchor_urb(urb
, &ar
->rx_work
);
412 atomic_inc(&ar
->rx_work_urbs
);
419 /* handle disconnect events*/
423 /* handle all other errors */
424 usb_anchor_urb(urb
, &ar
->rx_pool
);
425 atomic_inc(&ar
->rx_pool_urbs
);
429 err
= carl9170_usb_submit_rx_urb(ar
, GFP_ATOMIC
);
432 * usb_submit_rx_urb reported a problem.
433 * In case this is due to a rx buffer shortage,
434 * elevate the tasklet worker priority to
435 * the highest available level.
437 tasklet_hi_schedule(&ar
->usb_tasklet
);
439 if (atomic_read(&ar
->rx_anch_urbs
) == 0) {
441 * The system is too slow to cope with
442 * the enormous workload. We have simply
443 * run out of active rx urbs and this
444 * unfortunately leads to an unpredictable
448 ieee80211_queue_work(ar
->hw
, &ar
->ping_work
);
452 * Using anything less than _high_ priority absolutely
453 * kills the rx performance my UP-System...
455 tasklet_hi_schedule(&ar
->usb_tasklet
);
459 static struct urb
*carl9170_usb_alloc_rx_urb(struct ar9170
*ar
, gfp_t gfp
)
464 buf
= kmalloc(ar
->fw
.rx_size
, gfp
);
468 urb
= usb_alloc_urb(0, gfp
);
474 usb_fill_bulk_urb(urb
, ar
->udev
, usb_rcvbulkpipe(ar
->udev
,
475 AR9170_USB_EP_RX
), buf
, ar
->fw
.rx_size
,
476 carl9170_usb_rx_complete
, ar
);
478 urb
->transfer_flags
|= URB_FREE_BUFFER
;
483 static int carl9170_usb_send_rx_irq_urb(struct ar9170
*ar
)
485 struct urb
*urb
= NULL
;
489 urb
= usb_alloc_urb(0, GFP_KERNEL
);
493 ibuf
= kmalloc(AR9170_USB_EP_CTRL_MAX
, GFP_KERNEL
);
497 usb_fill_int_urb(urb
, ar
->udev
, usb_rcvintpipe(ar
->udev
,
498 AR9170_USB_EP_IRQ
), ibuf
, AR9170_USB_EP_CTRL_MAX
,
499 carl9170_usb_rx_irq_complete
, ar
, 1);
501 urb
->transfer_flags
|= URB_FREE_BUFFER
;
503 usb_anchor_urb(urb
, &ar
->rx_anch
);
504 err
= usb_submit_urb(urb
, GFP_KERNEL
);
506 usb_unanchor_urb(urb
);
513 static int carl9170_usb_init_rx_bulk_urbs(struct ar9170
*ar
)
516 int i
, err
= -EINVAL
;
519 * The driver actively maintains a second shadow
520 * pool for inactive, but fully-prepared rx urbs.
522 * The pool should help the driver to master huge
523 * workload spikes without running the risk of
524 * undersupplying the hardware or wasting time by
525 * processing rx data (streams) inside the urb
526 * completion (hardirq context).
528 for (i
= 0; i
< AR9170_NUM_RX_URBS_POOL
; i
++) {
529 urb
= carl9170_usb_alloc_rx_urb(ar
, GFP_KERNEL
);
535 usb_anchor_urb(urb
, &ar
->rx_pool
);
536 atomic_inc(&ar
->rx_pool_urbs
);
540 err
= carl9170_usb_submit_rx_urb(ar
, GFP_KERNEL
);
544 /* the device now waiting for the firmware. */
545 carl9170_set_state_when(ar
, CARL9170_STOPPED
, CARL9170_IDLE
);
550 usb_scuttle_anchored_urbs(&ar
->rx_pool
);
551 usb_scuttle_anchored_urbs(&ar
->rx_work
);
552 usb_kill_anchored_urbs(&ar
->rx_anch
);
556 static int carl9170_usb_flush(struct ar9170
*ar
)
561 while ((urb
= usb_get_from_anchor(&ar
->tx_wait
))) {
562 struct sk_buff
*skb
= (void *)urb
->context
;
563 carl9170_tx_drop(ar
, skb
);
564 carl9170_tx_callback(ar
, skb
);
568 ret
= usb_wait_anchor_empty_timeout(&ar
->tx_cmd
, 1000);
572 /* lets wait a while until the tx - queues are dried out */
573 ret
= usb_wait_anchor_empty_timeout(&ar
->tx_anch
, 1000);
577 usb_kill_anchored_urbs(&ar
->tx_anch
);
578 carl9170_usb_handle_tx_err(ar
);
583 static void carl9170_usb_cancel_urbs(struct ar9170
*ar
)
587 carl9170_set_state(ar
, CARL9170_UNKNOWN_STATE
);
589 err
= carl9170_usb_flush(ar
);
591 dev_err(&ar
->udev
->dev
, "stuck tx urbs!\n");
593 usb_poison_anchored_urbs(&ar
->tx_anch
);
594 carl9170_usb_handle_tx_err(ar
);
595 usb_poison_anchored_urbs(&ar
->rx_anch
);
597 tasklet_kill(&ar
->usb_tasklet
);
599 usb_scuttle_anchored_urbs(&ar
->rx_work
);
600 usb_scuttle_anchored_urbs(&ar
->rx_pool
);
601 usb_scuttle_anchored_urbs(&ar
->tx_cmd
);
604 int __carl9170_exec_cmd(struct ar9170
*ar
, struct carl9170_cmd
*cmd
,
610 if (!IS_INITIALIZED(ar
)) {
615 if (WARN_ON(cmd
->hdr
.len
> CARL9170_MAX_CMD_LEN
- 4)) {
620 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
626 if (ar
->usb_ep_cmd_is_bulk
)
627 usb_fill_bulk_urb(urb
, ar
->udev
,
628 usb_sndbulkpipe(ar
->udev
, AR9170_USB_EP_CMD
),
629 cmd
, cmd
->hdr
.len
+ 4,
630 carl9170_usb_cmd_complete
, ar
);
632 usb_fill_int_urb(urb
, ar
->udev
,
633 usb_sndintpipe(ar
->udev
, AR9170_USB_EP_CMD
),
634 cmd
, cmd
->hdr
.len
+ 4,
635 carl9170_usb_cmd_complete
, ar
, 1);
638 urb
->transfer_flags
|= URB_FREE_BUFFER
;
640 usb_anchor_urb(urb
, &ar
->tx_cmd
);
643 return carl9170_usb_submit_cmd_urb(ar
);
652 int carl9170_exec_cmd(struct ar9170
*ar
, const enum carl9170_cmd_oids cmd
,
653 unsigned int plen
, void *payload
, unsigned int outlen
, void *out
)
656 unsigned long time_left
;
658 if (!IS_ACCEPTING_CMD(ar
))
661 if (!(cmd
& CARL9170_CMD_ASYNC_FLAG
))
664 ar
->cmd
.hdr
.len
= plen
;
665 ar
->cmd
.hdr
.cmd
= cmd
;
666 /* writing multiple regs fills this buffer already */
667 if (plen
&& payload
!= (u8
*)(ar
->cmd
.data
))
668 memcpy(ar
->cmd
.data
, payload
, plen
);
670 spin_lock_bh(&ar
->cmd_lock
);
671 ar
->readbuf
= (u8
*)out
;
672 ar
->readlen
= outlen
;
673 spin_unlock_bh(&ar
->cmd_lock
);
675 reinit_completion(&ar
->cmd_wait
);
676 err
= __carl9170_exec_cmd(ar
, &ar
->cmd
, false);
678 if (!(cmd
& CARL9170_CMD_ASYNC_FLAG
)) {
679 time_left
= wait_for_completion_timeout(&ar
->cmd_wait
, HZ
);
680 if (time_left
== 0) {
685 if (ar
->readlen
!= outlen
) {
694 /* Maybe the device was removed in the moment we were waiting? */
695 if (IS_STARTED(ar
)) {
696 dev_err(&ar
->udev
->dev
, "no command feedback "
697 "received (%d).\n", err
);
699 /* provide some maybe useful debug information */
700 print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE
,
703 carl9170_restart(ar
, CARL9170_RR_COMMAND_TIMEOUT
);
706 /* invalidate to avoid completing the next command prematurely */
707 spin_lock_bh(&ar
->cmd_lock
);
710 spin_unlock_bh(&ar
->cmd_lock
);
715 void carl9170_usb_tx(struct ar9170
*ar
, struct sk_buff
*skb
)
718 struct ar9170_stream
*tx_stream
;
725 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
729 if (ar
->fw
.tx_stream
) {
730 tx_stream
= (void *) (skb
->data
- sizeof(*tx_stream
));
732 len
= skb
->len
+ sizeof(*tx_stream
);
733 tx_stream
->length
= cpu_to_le16(len
);
734 tx_stream
->tag
= cpu_to_le16(AR9170_TX_STREAM_TAG
);
741 usb_fill_bulk_urb(urb
, ar
->udev
, usb_sndbulkpipe(ar
->udev
,
742 AR9170_USB_EP_TX
), data
, len
,
743 carl9170_usb_tx_data_complete
, skb
);
745 urb
->transfer_flags
|= URB_ZERO_PACKET
;
747 usb_anchor_urb(urb
, &ar
->tx_wait
);
751 carl9170_usb_submit_data_urb(ar
);
755 carl9170_tx_drop(ar
, skb
);
756 carl9170_tx_callback(ar
, skb
);
759 static void carl9170_release_firmware(struct ar9170
*ar
)
762 release_firmware(ar
->fw
.fw
);
763 memset(&ar
->fw
, 0, sizeof(ar
->fw
));
767 void carl9170_usb_stop(struct ar9170
*ar
)
771 carl9170_set_state_when(ar
, CARL9170_IDLE
, CARL9170_STOPPED
);
773 ret
= carl9170_usb_flush(ar
);
775 dev_err(&ar
->udev
->dev
, "kill pending tx urbs.\n");
777 usb_poison_anchored_urbs(&ar
->tx_anch
);
778 carl9170_usb_handle_tx_err(ar
);
780 /* kill any pending command */
781 spin_lock_bh(&ar
->cmd_lock
);
783 spin_unlock_bh(&ar
->cmd_lock
);
784 complete(&ar
->cmd_wait
);
788 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
789 * Else we would end up with a unresponsive device...
793 int carl9170_usb_open(struct ar9170
*ar
)
795 usb_unpoison_anchored_urbs(&ar
->tx_anch
);
797 carl9170_set_state_when(ar
, CARL9170_STOPPED
, CARL9170_IDLE
);
801 static int carl9170_usb_load_firmware(struct ar9170
*ar
)
805 unsigned int transfer
;
810 buf
= kmalloc(4096, GFP_KERNEL
);
816 data
= ar
->fw
.fw
->data
;
817 len
= ar
->fw
.fw
->size
;
818 addr
= ar
->fw
.address
;
820 /* this removes the miniboot image */
821 data
+= ar
->fw
.offset
;
822 len
-= ar
->fw
.offset
;
825 transfer
= min_t(unsigned int, len
, 4096u);
826 memcpy(buf
, data
, transfer
);
828 err
= usb_control_msg(ar
->udev
, usb_sndctrlpipe(ar
->udev
, 0),
829 0x30 /* FW DL */, 0x40 | USB_DIR_OUT
,
830 addr
>> 8, 0, buf
, transfer
, 100);
843 err
= usb_control_msg(ar
->udev
, usb_sndctrlpipe(ar
->udev
, 0),
844 0x31 /* FW DL COMPLETE */,
845 0x40 | USB_DIR_OUT
, 0, 0, NULL
, 0, 200);
847 if (wait_for_completion_timeout(&ar
->fw_boot_wait
, HZ
) == 0) {
852 err
= carl9170_echo_test(ar
, 0x4a110123);
856 /* now, start the command response counter */
862 dev_err(&ar
->udev
->dev
, "firmware upload failed (%d).\n", err
);
866 int carl9170_usb_restart(struct ar9170
*ar
)
870 if (ar
->intf
->condition
!= USB_INTERFACE_BOUND
)
874 * Disable the command response sequence counter check.
875 * We already know that the device/firmware is in a bad state.
876 * So, no extra points are awarded to anyone who reminds the
881 err
= carl9170_reboot(ar
);
883 carl9170_usb_stop(ar
);
888 tasklet_schedule(&ar
->usb_tasklet
);
890 /* The reboot procedure can take quite a while to complete. */
893 err
= carl9170_usb_open(ar
);
897 err
= carl9170_usb_load_firmware(ar
);
904 carl9170_usb_cancel_urbs(ar
);
908 void carl9170_usb_reset(struct ar9170
*ar
)
911 * This is the last resort to get the device going again
912 * without any *user replugging action*.
914 * But there is a catch: usb_reset really is like a physical
915 * *reconnect*. The mac80211 state will be lost in the process.
916 * Therefore a userspace application, which is monitoring
917 * the link must step in.
919 carl9170_usb_cancel_urbs(ar
);
921 carl9170_usb_stop(ar
);
923 usb_queue_reset_device(ar
->intf
);
926 static int carl9170_usb_init_device(struct ar9170
*ar
)
931 * The carl9170 firmware let's the driver know when it's
932 * ready for action. But we have to be prepared to gracefully
933 * handle all spurious [flushed] messages after each (re-)boot.
934 * Thus the command response counter remains disabled until it
935 * can be safely synchronized.
939 err
= carl9170_usb_send_rx_irq_urb(ar
);
943 err
= carl9170_usb_init_rx_bulk_urbs(ar
);
947 err
= carl9170_usb_open(ar
);
951 mutex_lock(&ar
->mutex
);
952 err
= carl9170_usb_load_firmware(ar
);
953 mutex_unlock(&ar
->mutex
);
960 carl9170_usb_stop(ar
);
963 carl9170_usb_cancel_urbs(ar
);
969 static void carl9170_usb_firmware_failed(struct ar9170
*ar
)
971 /* Store a copies of the usb_interface and usb_device pointer locally.
972 * This is because release_driver initiates carl9170_usb_disconnect,
973 * which in turn frees our driver context (ar).
975 struct usb_interface
*intf
= ar
->intf
;
976 struct usb_device
*udev
= ar
->udev
;
978 complete(&ar
->fw_load_wait
);
979 /* at this point 'ar' could be already freed. Don't use it anymore */
982 /* unbind anything failed */
983 usb_lock_device(udev
);
984 usb_driver_release_interface(&carl9170_driver
, intf
);
985 usb_unlock_device(udev
);
990 static void carl9170_usb_firmware_finish(struct ar9170
*ar
)
992 struct usb_interface
*intf
= ar
->intf
;
995 err
= carl9170_parse_firmware(ar
);
999 err
= carl9170_usb_init_device(ar
);
1003 err
= carl9170_register(ar
);
1005 carl9170_usb_stop(ar
);
1009 complete(&ar
->fw_load_wait
);
1014 carl9170_usb_cancel_urbs(ar
);
1017 carl9170_release_firmware(ar
);
1018 carl9170_usb_firmware_failed(ar
);
1021 static void carl9170_usb_firmware_step2(const struct firmware
*fw
,
1024 struct ar9170
*ar
= context
;
1028 carl9170_usb_firmware_finish(ar
);
1032 dev_err(&ar
->udev
->dev
, "firmware not found.\n");
1033 carl9170_usb_firmware_failed(ar
);
1036 static int carl9170_usb_probe(struct usb_interface
*intf
,
1037 const struct usb_device_id
*id
)
1039 struct usb_endpoint_descriptor
*ep
;
1041 struct usb_device
*udev
;
1044 err
= usb_reset_device(interface_to_usbdev(intf
));
1048 ar
= carl9170_alloc(sizeof(*ar
));
1052 udev
= interface_to_usbdev(intf
);
1055 ar
->features
= id
->driver_info
;
1057 /* We need to remember the type of endpoint 4 because it differs
1058 * between high- and full-speed configuration. The high-speed
1059 * configuration specifies it as interrupt and the full-speed
1060 * configuration as bulk endpoint. This information is required
1061 * later when sending urbs to that endpoint.
1063 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; ++i
) {
1064 ep
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
1066 if (usb_endpoint_num(ep
) == AR9170_USB_EP_CMD
&&
1067 usb_endpoint_dir_out(ep
) &&
1068 usb_endpoint_type(ep
) == USB_ENDPOINT_XFER_BULK
)
1069 ar
->usb_ep_cmd_is_bulk
= true;
1072 usb_set_intfdata(intf
, ar
);
1073 SET_IEEE80211_DEV(ar
->hw
, &intf
->dev
);
1075 init_usb_anchor(&ar
->rx_anch
);
1076 init_usb_anchor(&ar
->rx_pool
);
1077 init_usb_anchor(&ar
->rx_work
);
1078 init_usb_anchor(&ar
->tx_wait
);
1079 init_usb_anchor(&ar
->tx_anch
);
1080 init_usb_anchor(&ar
->tx_cmd
);
1081 init_usb_anchor(&ar
->tx_err
);
1082 init_completion(&ar
->cmd_wait
);
1083 init_completion(&ar
->fw_boot_wait
);
1084 init_completion(&ar
->fw_load_wait
);
1085 tasklet_setup(&ar
->usb_tasklet
, carl9170_usb_tasklet
);
1087 atomic_set(&ar
->tx_cmd_urbs
, 0);
1088 atomic_set(&ar
->tx_anch_urbs
, 0);
1089 atomic_set(&ar
->rx_work_urbs
, 0);
1090 atomic_set(&ar
->rx_anch_urbs
, 0);
1091 atomic_set(&ar
->rx_pool_urbs
, 0);
1095 carl9170_set_state(ar
, CARL9170_STOPPED
);
1097 err
= request_firmware_nowait(THIS_MODULE
, 1, CARL9170FW_NAME
,
1098 &ar
->udev
->dev
, GFP_KERNEL
, ar
, carl9170_usb_firmware_step2
);
1106 static void carl9170_usb_disconnect(struct usb_interface
*intf
)
1108 struct ar9170
*ar
= usb_get_intfdata(intf
);
1113 wait_for_completion(&ar
->fw_load_wait
);
1115 if (IS_INITIALIZED(ar
)) {
1116 carl9170_reboot(ar
);
1117 carl9170_usb_stop(ar
);
1120 carl9170_usb_cancel_urbs(ar
);
1121 carl9170_unregister(ar
);
1123 usb_set_intfdata(intf
, NULL
);
1125 carl9170_release_firmware(ar
);
1130 static int carl9170_usb_suspend(struct usb_interface
*intf
,
1131 pm_message_t message
)
1133 struct ar9170
*ar
= usb_get_intfdata(intf
);
1138 carl9170_usb_cancel_urbs(ar
);
1143 static int carl9170_usb_resume(struct usb_interface
*intf
)
1145 struct ar9170
*ar
= usb_get_intfdata(intf
);
1151 usb_unpoison_anchored_urbs(&ar
->rx_anch
);
1152 carl9170_set_state(ar
, CARL9170_STOPPED
);
1155 * The USB documentation demands that [for suspend] all traffic
1156 * to and from the device has to stop. This would be fine, but
1157 * there's a catch: the device[usb phy] does not come back.
1159 * Upon resume the firmware will "kill" itself and the
1160 * boot-code sorts out the magic voodoo.
1161 * Not very nice, but there's not much what could go wrong.
1165 err
= carl9170_usb_init_device(ar
);
1172 carl9170_usb_cancel_urbs(ar
);
1176 #endif /* CONFIG_PM */
1178 static struct usb_driver carl9170_driver
= {
1179 .name
= KBUILD_MODNAME
,
1180 .probe
= carl9170_usb_probe
,
1181 .disconnect
= carl9170_usb_disconnect
,
1182 .id_table
= carl9170_usb_ids
,
1185 .suspend
= carl9170_usb_suspend
,
1186 .resume
= carl9170_usb_resume
,
1187 .reset_resume
= carl9170_usb_resume
,
1188 #endif /* CONFIG_PM */
1189 .disable_hub_initiated_lpm
= 1,
1192 module_usb_driver(carl9170_driver
);