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 * http://wireless.kernel.org/en/users/Drivers/ar9170/devices ),
65 * whenever you add a new device.
67 static 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) },
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) },
125 MODULE_DEVICE_TABLE(usb
, carl9170_usb_ids
);
127 static void carl9170_usb_submit_data_urb(struct ar9170
*ar
)
132 if (atomic_inc_return(&ar
->tx_anch_urbs
) > AR9170_NUM_TX_URBS
)
135 urb
= usb_get_from_anchor(&ar
->tx_wait
);
139 usb_anchor_urb(urb
, &ar
->tx_anch
);
141 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
143 if (net_ratelimit()) {
144 dev_err(&ar
->udev
->dev
, "tx submit failed (%d)\n",
148 usb_unanchor_urb(urb
);
149 usb_anchor_urb(urb
, &ar
->tx_err
);
154 if (likely(err
== 0))
158 atomic_dec(&ar
->tx_anch_urbs
);
161 static void carl9170_usb_tx_data_complete(struct urb
*urb
)
163 struct ar9170
*ar
= usb_get_intfdata(usb_ifnum_to_if(urb
->dev
, 0));
165 if (WARN_ON_ONCE(!ar
)) {
166 dev_kfree_skb_irq(urb
->context
);
170 atomic_dec(&ar
->tx_anch_urbs
);
172 switch (urb
->status
) {
173 /* everything is fine */
175 carl9170_tx_callback(ar
, (void *)urb
->context
);
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.
188 usb_anchor_urb(urb
, &ar
->tx_err
);
191 /* a random transmission error has occurred? */
193 if (net_ratelimit()) {
194 dev_err(&ar
->udev
->dev
, "tx failed (%d)\n",
198 usb_anchor_urb(urb
, &ar
->tx_err
);
202 if (likely(IS_STARTED(ar
)))
203 carl9170_usb_submit_data_urb(ar
);
206 static int carl9170_usb_submit_cmd_urb(struct ar9170
*ar
)
211 if (atomic_inc_return(&ar
->tx_cmd_urbs
) != 1) {
212 atomic_dec(&ar
->tx_cmd_urbs
);
216 urb
= usb_get_from_anchor(&ar
->tx_cmd
);
218 atomic_dec(&ar
->tx_cmd_urbs
);
222 usb_anchor_urb(urb
, &ar
->tx_anch
);
223 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
225 usb_unanchor_urb(urb
);
226 atomic_dec(&ar
->tx_cmd_urbs
);
233 static void carl9170_usb_cmd_complete(struct urb
*urb
)
235 struct ar9170
*ar
= urb
->context
;
238 if (WARN_ON_ONCE(!ar
))
241 atomic_dec(&ar
->tx_cmd_urbs
);
243 switch (urb
->status
) {
244 /* everything is fine */
260 if (!IS_INITIALIZED(ar
))
264 dev_err(&ar
->udev
->dev
, "submit cmd cb failed (%d).\n", err
);
266 err
= carl9170_usb_submit_cmd_urb(ar
);
268 dev_err(&ar
->udev
->dev
, "submit cmd failed (%d).\n", err
);
271 static void carl9170_usb_rx_irq_complete(struct urb
*urb
)
273 struct ar9170
*ar
= urb
->context
;
275 if (WARN_ON_ONCE(!ar
))
278 switch (urb
->status
) {
279 /* everything is fine */
294 carl9170_handle_command_response(ar
, urb
->transfer_buffer
,
298 usb_anchor_urb(urb
, &ar
->rx_anch
);
299 if (unlikely(usb_submit_urb(urb
, GFP_ATOMIC
)))
300 usb_unanchor_urb(urb
);
303 static int carl9170_usb_submit_rx_urb(struct ar9170
*ar
, gfp_t gfp
)
306 int err
= 0, runs
= 0;
308 while ((atomic_read(&ar
->rx_anch_urbs
) < AR9170_NUM_RX_URBS
) &&
309 (runs
++ < AR9170_NUM_RX_URBS
)) {
311 urb
= usb_get_from_anchor(&ar
->rx_pool
);
313 usb_anchor_urb(urb
, &ar
->rx_anch
);
314 err
= usb_submit_urb(urb
, gfp
);
316 usb_unanchor_urb(urb
);
317 usb_anchor_urb(urb
, &ar
->rx_pool
);
319 atomic_dec(&ar
->rx_pool_urbs
);
320 atomic_inc(&ar
->rx_anch_urbs
);
329 static void carl9170_usb_rx_work(struct ar9170
*ar
)
334 for (i
= 0; i
< AR9170_NUM_RX_URBS_POOL
; i
++) {
335 urb
= usb_get_from_anchor(&ar
->rx_work
);
339 atomic_dec(&ar
->rx_work_urbs
);
340 if (IS_INITIALIZED(ar
)) {
341 carl9170_rx(ar
, urb
->transfer_buffer
,
345 usb_anchor_urb(urb
, &ar
->rx_pool
);
346 atomic_inc(&ar
->rx_pool_urbs
);
350 carl9170_usb_submit_rx_urb(ar
, GFP_ATOMIC
);
354 void carl9170_usb_handle_tx_err(struct ar9170
*ar
)
358 while ((urb
= usb_get_from_anchor(&ar
->tx_err
))) {
359 struct sk_buff
*skb
= (void *)urb
->context
;
361 carl9170_tx_drop(ar
, skb
);
362 carl9170_tx_callback(ar
, skb
);
367 static void carl9170_usb_tasklet(unsigned long data
)
369 struct ar9170
*ar
= (struct ar9170
*) data
;
371 if (!IS_INITIALIZED(ar
))
374 carl9170_usb_rx_work(ar
);
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.
382 carl9170_tx_scheduler(ar
);
385 static void carl9170_usb_rx_complete(struct urb
*urb
)
387 struct ar9170
*ar
= (struct ar9170
*)urb
->context
;
390 if (WARN_ON_ONCE(!ar
))
393 atomic_dec(&ar
->rx_anch_urbs
);
395 switch (urb
->status
) {
398 usb_anchor_urb(urb
, &ar
->rx_work
);
399 atomic_inc(&ar
->rx_work_urbs
);
406 /* handle disconnect events*/
410 /* handle all other errors */
411 usb_anchor_urb(urb
, &ar
->rx_pool
);
412 atomic_inc(&ar
->rx_pool_urbs
);
416 err
= carl9170_usb_submit_rx_urb(ar
, GFP_ATOMIC
);
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.
424 tasklet_hi_schedule(&ar
->usb_tasklet
);
426 if (atomic_read(&ar
->rx_anch_urbs
) == 0) {
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
435 ieee80211_queue_work(ar
->hw
, &ar
->ping_work
);
439 * Using anything less than _high_ priority absolutely
440 * kills the rx performance my UP-System...
442 tasklet_hi_schedule(&ar
->usb_tasklet
);
446 static struct urb
*carl9170_usb_alloc_rx_urb(struct ar9170
*ar
, gfp_t gfp
)
451 buf
= kmalloc(ar
->fw
.rx_size
, gfp
);
455 urb
= usb_alloc_urb(0, gfp
);
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
);
465 urb
->transfer_flags
|= URB_FREE_BUFFER
;
470 static int carl9170_usb_send_rx_irq_urb(struct ar9170
*ar
)
472 struct urb
*urb
= NULL
;
476 urb
= usb_alloc_urb(0, GFP_KERNEL
);
480 ibuf
= kmalloc(AR9170_USB_EP_CTRL_MAX
, GFP_KERNEL
);
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);
488 urb
->transfer_flags
|= URB_FREE_BUFFER
;
490 usb_anchor_urb(urb
, &ar
->rx_anch
);
491 err
= usb_submit_urb(urb
, GFP_KERNEL
);
493 usb_unanchor_urb(urb
);
500 static int carl9170_usb_init_rx_bulk_urbs(struct ar9170
*ar
)
503 int i
, err
= -EINVAL
;
506 * The driver actively maintains a second shadow
507 * pool for inactive, but fully-prepared rx urbs.
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).
515 for (i
= 0; i
< AR9170_NUM_RX_URBS_POOL
; i
++) {
516 urb
= carl9170_usb_alloc_rx_urb(ar
, GFP_KERNEL
);
522 usb_anchor_urb(urb
, &ar
->rx_pool
);
523 atomic_inc(&ar
->rx_pool_urbs
);
527 err
= carl9170_usb_submit_rx_urb(ar
, GFP_KERNEL
);
531 /* the device now waiting for the firmware. */
532 carl9170_set_state_when(ar
, CARL9170_STOPPED
, CARL9170_IDLE
);
537 usb_scuttle_anchored_urbs(&ar
->rx_pool
);
538 usb_scuttle_anchored_urbs(&ar
->rx_work
);
539 usb_kill_anchored_urbs(&ar
->rx_anch
);
543 static int carl9170_usb_flush(struct ar9170
*ar
)
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
);
555 ret
= usb_wait_anchor_empty_timeout(&ar
->tx_cmd
, 1000);
559 /* lets wait a while until the tx - queues are dried out */
560 ret
= usb_wait_anchor_empty_timeout(&ar
->tx_anch
, 1000);
564 usb_kill_anchored_urbs(&ar
->tx_anch
);
565 carl9170_usb_handle_tx_err(ar
);
570 static void carl9170_usb_cancel_urbs(struct ar9170
*ar
)
574 carl9170_set_state(ar
, CARL9170_UNKNOWN_STATE
);
576 err
= carl9170_usb_flush(ar
);
578 dev_err(&ar
->udev
->dev
, "stuck tx urbs!\n");
580 usb_poison_anchored_urbs(&ar
->tx_anch
);
581 carl9170_usb_handle_tx_err(ar
);
582 usb_poison_anchored_urbs(&ar
->rx_anch
);
584 tasklet_kill(&ar
->usb_tasklet
);
586 usb_scuttle_anchored_urbs(&ar
->rx_work
);
587 usb_scuttle_anchored_urbs(&ar
->rx_pool
);
588 usb_scuttle_anchored_urbs(&ar
->tx_cmd
);
591 int __carl9170_exec_cmd(struct ar9170
*ar
, struct carl9170_cmd
*cmd
,
597 if (!IS_INITIALIZED(ar
)) {
602 if (WARN_ON(cmd
->hdr
.len
> CARL9170_MAX_CMD_LEN
- 4)) {
607 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
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);
618 urb
->transfer_flags
|= URB_FREE_BUFFER
;
620 usb_anchor_urb(urb
, &ar
->tx_cmd
);
623 return carl9170_usb_submit_cmd_urb(ar
);
632 int carl9170_exec_cmd(struct ar9170
*ar
, const enum carl9170_cmd_oids cmd
,
633 unsigned int plen
, void *payload
, unsigned int outlen
, void *out
)
637 if (!IS_ACCEPTING_CMD(ar
))
640 if (!(cmd
& CARL9170_CMD_ASYNC_FLAG
))
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
);
649 spin_lock_bh(&ar
->cmd_lock
);
650 ar
->readbuf
= (u8
*)out
;
651 ar
->readlen
= outlen
;
652 spin_unlock_bh(&ar
->cmd_lock
);
654 err
= __carl9170_exec_cmd(ar
, &ar
->cmd
, false);
656 if (!(cmd
& CARL9170_CMD_ASYNC_FLAG
)) {
657 err
= wait_for_completion_timeout(&ar
->cmd_wait
, HZ
);
663 if (ar
->readlen
!= outlen
) {
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
);
677 /* provide some maybe useful debug information */
678 print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE
,
681 carl9170_restart(ar
, CARL9170_RR_COMMAND_TIMEOUT
);
684 /* invalidate to avoid completing the next command prematurely */
685 spin_lock_bh(&ar
->cmd_lock
);
688 spin_unlock_bh(&ar
->cmd_lock
);
693 void carl9170_usb_tx(struct ar9170
*ar
, struct sk_buff
*skb
)
696 struct ar9170_stream
*tx_stream
;
703 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
707 if (ar
->fw
.tx_stream
) {
708 tx_stream
= (void *) (skb
->data
- sizeof(*tx_stream
));
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
);
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
);
723 urb
->transfer_flags
|= URB_ZERO_PACKET
;
725 usb_anchor_urb(urb
, &ar
->tx_wait
);
729 carl9170_usb_submit_data_urb(ar
);
733 carl9170_tx_drop(ar
, skb
);
734 carl9170_tx_callback(ar
, skb
);
737 static void carl9170_release_firmware(struct ar9170
*ar
)
740 release_firmware(ar
->fw
.fw
);
741 memset(&ar
->fw
, 0, sizeof(ar
->fw
));
745 void carl9170_usb_stop(struct ar9170
*ar
)
749 carl9170_set_state_when(ar
, CARL9170_IDLE
, CARL9170_STOPPED
);
751 ret
= carl9170_usb_flush(ar
);
753 dev_err(&ar
->udev
->dev
, "kill pending tx urbs.\n");
755 usb_poison_anchored_urbs(&ar
->tx_anch
);
756 carl9170_usb_handle_tx_err(ar
);
758 /* kill any pending command */
759 spin_lock_bh(&ar
->cmd_lock
);
761 spin_unlock_bh(&ar
->cmd_lock
);
762 complete_all(&ar
->cmd_wait
);
764 /* This is required to prevent an early completion on _start */
765 INIT_COMPLETION(ar
->cmd_wait
);
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...
774 int carl9170_usb_open(struct ar9170
*ar
)
776 usb_unpoison_anchored_urbs(&ar
->tx_anch
);
778 carl9170_set_state_when(ar
, CARL9170_STOPPED
, CARL9170_IDLE
);
782 static int carl9170_usb_load_firmware(struct ar9170
*ar
)
786 unsigned int transfer
;
791 buf
= kmalloc(4096, GFP_KERNEL
);
797 data
= ar
->fw
.fw
->data
;
798 len
= ar
->fw
.fw
->size
;
799 addr
= ar
->fw
.address
;
801 /* this removes the miniboot image */
802 data
+= ar
->fw
.offset
;
803 len
-= ar
->fw
.offset
;
806 transfer
= min_t(unsigned int, len
, 4096u);
807 memcpy(buf
, data
, transfer
);
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);
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);
828 if (wait_for_completion_timeout(&ar
->fw_boot_wait
, HZ
) == 0) {
833 err
= carl9170_echo_test(ar
, 0x4a110123);
837 /* now, start the command response counter */
843 dev_err(&ar
->udev
->dev
, "firmware upload failed (%d).\n", err
);
847 int carl9170_usb_restart(struct ar9170
*ar
)
851 if (ar
->intf
->condition
!= USB_INTERFACE_BOUND
)
855 * Disable the command response sequence counter check.
856 * We already know that the device/firmware is in a bad state.
857 * So, no extra points are awarded to anyone who reminds the
862 err
= carl9170_reboot(ar
);
864 carl9170_usb_stop(ar
);
869 tasklet_schedule(&ar
->usb_tasklet
);
871 /* The reboot procedure can take quite a while to complete. */
874 err
= carl9170_usb_open(ar
);
878 err
= carl9170_usb_load_firmware(ar
);
885 carl9170_usb_cancel_urbs(ar
);
889 void carl9170_usb_reset(struct ar9170
*ar
)
892 * This is the last resort to get the device going again
893 * without any *user replugging action*.
895 * But there is a catch: usb_reset really is like a physical
896 * *reconnect*. The mac80211 state will be lost in the process.
897 * Therefore a userspace application, which is monitoring
898 * the link must step in.
900 carl9170_usb_cancel_urbs(ar
);
902 carl9170_usb_stop(ar
);
904 usb_queue_reset_device(ar
->intf
);
907 static int carl9170_usb_init_device(struct ar9170
*ar
)
912 * The carl9170 firmware let's the driver know when it's
913 * ready for action. But we have to be prepared to gracefully
914 * handle all spurious [flushed] messages after each (re-)boot.
915 * Thus the command response counter remains disabled until it
916 * can be safely synchronized.
920 err
= carl9170_usb_send_rx_irq_urb(ar
);
924 err
= carl9170_usb_init_rx_bulk_urbs(ar
);
928 err
= carl9170_usb_open(ar
);
932 mutex_lock(&ar
->mutex
);
933 err
= carl9170_usb_load_firmware(ar
);
934 mutex_unlock(&ar
->mutex
);
941 carl9170_usb_stop(ar
);
944 carl9170_usb_cancel_urbs(ar
);
950 static void carl9170_usb_firmware_failed(struct ar9170
*ar
)
952 struct device
*parent
= ar
->udev
->dev
.parent
;
953 struct usb_device
*udev
;
956 * Store a copy of the usb_device pointer locally.
957 * This is because device_release_driver initiates
958 * carl9170_usb_disconnect, which in turn frees our
959 * driver context (ar).
963 complete(&ar
->fw_load_wait
);
965 /* unbind anything failed */
969 device_release_driver(&udev
->dev
);
971 device_unlock(parent
);
976 static void carl9170_usb_firmware_finish(struct ar9170
*ar
)
980 err
= carl9170_parse_firmware(ar
);
984 err
= carl9170_usb_init_device(ar
);
988 err
= carl9170_register(ar
);
990 carl9170_usb_stop(ar
);
994 complete(&ar
->fw_load_wait
);
995 usb_put_dev(ar
->udev
);
999 carl9170_usb_cancel_urbs(ar
);
1002 carl9170_release_firmware(ar
);
1003 carl9170_usb_firmware_failed(ar
);
1006 static void carl9170_usb_firmware_step2(const struct firmware
*fw
,
1009 struct ar9170
*ar
= context
;
1013 carl9170_usb_firmware_finish(ar
);
1017 dev_err(&ar
->udev
->dev
, "firmware not found.\n");
1018 carl9170_usb_firmware_failed(ar
);
1021 static int carl9170_usb_probe(struct usb_interface
*intf
,
1022 const struct usb_device_id
*id
)
1025 struct usb_device
*udev
;
1028 err
= usb_reset_device(interface_to_usbdev(intf
));
1032 ar
= carl9170_alloc(sizeof(*ar
));
1036 udev
= interface_to_usbdev(intf
);
1040 ar
->features
= id
->driver_info
;
1042 usb_set_intfdata(intf
, ar
);
1043 SET_IEEE80211_DEV(ar
->hw
, &intf
->dev
);
1045 init_usb_anchor(&ar
->rx_anch
);
1046 init_usb_anchor(&ar
->rx_pool
);
1047 init_usb_anchor(&ar
->rx_work
);
1048 init_usb_anchor(&ar
->tx_wait
);
1049 init_usb_anchor(&ar
->tx_anch
);
1050 init_usb_anchor(&ar
->tx_cmd
);
1051 init_usb_anchor(&ar
->tx_err
);
1052 init_completion(&ar
->cmd_wait
);
1053 init_completion(&ar
->fw_boot_wait
);
1054 init_completion(&ar
->fw_load_wait
);
1055 tasklet_init(&ar
->usb_tasklet
, carl9170_usb_tasklet
,
1058 atomic_set(&ar
->tx_cmd_urbs
, 0);
1059 atomic_set(&ar
->tx_anch_urbs
, 0);
1060 atomic_set(&ar
->rx_work_urbs
, 0);
1061 atomic_set(&ar
->rx_anch_urbs
, 0);
1062 atomic_set(&ar
->rx_pool_urbs
, 0);
1064 usb_get_dev(ar
->udev
);
1066 carl9170_set_state(ar
, CARL9170_STOPPED
);
1068 return request_firmware_nowait(THIS_MODULE
, 1, CARL9170FW_NAME
,
1069 &ar
->udev
->dev
, GFP_KERNEL
, ar
, carl9170_usb_firmware_step2
);
1072 static void carl9170_usb_disconnect(struct usb_interface
*intf
)
1074 struct ar9170
*ar
= usb_get_intfdata(intf
);
1075 struct usb_device
*udev
;
1081 wait_for_completion(&ar
->fw_load_wait
);
1083 if (IS_INITIALIZED(ar
)) {
1084 carl9170_reboot(ar
);
1085 carl9170_usb_stop(ar
);
1088 carl9170_usb_cancel_urbs(ar
);
1089 carl9170_unregister(ar
);
1091 usb_set_intfdata(intf
, NULL
);
1093 carl9170_release_firmware(ar
);
1099 static int carl9170_usb_suspend(struct usb_interface
*intf
,
1100 pm_message_t message
)
1102 struct ar9170
*ar
= usb_get_intfdata(intf
);
1107 carl9170_usb_cancel_urbs(ar
);
1112 static int carl9170_usb_resume(struct usb_interface
*intf
)
1114 struct ar9170
*ar
= usb_get_intfdata(intf
);
1120 usb_unpoison_anchored_urbs(&ar
->rx_anch
);
1121 carl9170_set_state(ar
, CARL9170_STOPPED
);
1124 * The USB documentation demands that [for suspend] all traffic
1125 * to and from the device has to stop. This would be fine, but
1126 * there's a catch: the device[usb phy] does not come back.
1128 * Upon resume the firmware will "kill" itself and the
1129 * boot-code sorts out the magic voodoo.
1130 * Not very nice, but there's not much what could go wrong.
1134 err
= carl9170_usb_init_device(ar
);
1141 carl9170_usb_cancel_urbs(ar
);
1145 #endif /* CONFIG_PM */
1147 static struct usb_driver carl9170_driver
= {
1148 .name
= KBUILD_MODNAME
,
1149 .probe
= carl9170_usb_probe
,
1150 .disconnect
= carl9170_usb_disconnect
,
1151 .id_table
= carl9170_usb_ids
,
1154 .suspend
= carl9170_usb_suspend
,
1155 .resume
= carl9170_usb_resume
,
1156 .reset_resume
= carl9170_usb_resume
,
1157 #endif /* CONFIG_PM */
1160 static int __init
carl9170_usb_init(void)
1162 return usb_register(&carl9170_driver
);
1165 static void __exit
carl9170_usb_exit(void)
1167 usb_deregister(&carl9170_driver
);
1170 module_init(carl9170_usb_init
);
1171 module_exit(carl9170_usb_exit
);