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) },
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 },
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) },
98 { USB_DEVICE(0x0586, 0x3417) },
100 { USB_DEVICE(0x0cde, 0x0023) },
102 { USB_DEVICE(0x0cde, 0x0026) },
103 /* Sphairon Homelink 1202 */
104 { USB_DEVICE(0x0cde, 0x0027) },
105 /* Arcadyan WN7512 */
106 { USB_DEVICE(0x083a, 0xf522) },
108 { USB_DEVICE(0x2019, 0x5304) },
109 /* IO-Data WNGDNUS2 */
110 { USB_DEVICE(0x04bb, 0x093f) },
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) },
123 MODULE_DEVICE_TABLE(usb, carl9170_usb_ids);
125 static void carl9170_usb_submit_data_urb(struct ar9170 *ar)
130 if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS)
133 urb = usb_get_from_anchor(&ar->tx_wait);
137 usb_anchor_urb(urb, &ar->tx_anch);
139 err = usb_submit_urb(urb, GFP_ATOMIC);
141 if (net_ratelimit()) {
142 dev_err(&ar->udev->dev, "tx submit failed (%d)\n",
146 usb_unanchor_urb(urb);
147 usb_anchor_urb(urb, &ar->tx_err);
152 if (likely(err == 0))
156 atomic_dec(&ar->tx_anch_urbs);
159 static void carl9170_usb_tx_data_complete(struct urb *urb)
161 struct ar9170 *ar = (struct ar9170 *)
162 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
164 if (WARN_ON_ONCE(!ar)) {
165 dev_kfree_skb_irq(urb->context);
169 atomic_dec(&ar->tx_anch_urbs);
171 switch (urb->status) {
172 /* everything is fine */
174 carl9170_tx_callback(ar, (void *)urb->context);
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.
187 usb_anchor_urb(urb, &ar->tx_err);
190 /* a random transmission error has occurred? */
192 if (net_ratelimit()) {
193 dev_err(&ar->udev->dev, "tx failed (%d)\n",
197 usb_anchor_urb(urb, &ar->tx_err);
201 if (likely(IS_STARTED(ar)))
202 carl9170_usb_submit_data_urb(ar);
205 static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
210 if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
211 atomic_dec(&ar->tx_cmd_urbs);
215 urb = usb_get_from_anchor(&ar->tx_cmd);
217 atomic_dec(&ar->tx_cmd_urbs);
221 usb_anchor_urb(urb, &ar->tx_anch);
222 err = usb_submit_urb(urb, GFP_ATOMIC);
224 usb_unanchor_urb(urb);
225 atomic_dec(&ar->tx_cmd_urbs);
232 static void carl9170_usb_cmd_complete(struct urb *urb)
234 struct ar9170 *ar = urb->context;
237 if (WARN_ON_ONCE(!ar))
240 atomic_dec(&ar->tx_cmd_urbs);
242 switch (urb->status) {
243 /* everything is fine */
259 if (!IS_INITIALIZED(ar))
263 dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
265 err = carl9170_usb_submit_cmd_urb(ar);
267 dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
270 static void carl9170_usb_rx_irq_complete(struct urb *urb)
272 struct ar9170 *ar = urb->context;
274 if (WARN_ON_ONCE(!ar))
277 switch (urb->status) {
278 /* everything is fine */
293 carl9170_handle_command_response(ar, urb->transfer_buffer,
297 usb_anchor_urb(urb, &ar->rx_anch);
298 if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
299 usb_unanchor_urb(urb);
302 static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
305 int err = 0, runs = 0;
307 while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
308 (runs++ < AR9170_NUM_RX_URBS)) {
310 urb = usb_get_from_anchor(&ar->rx_pool);
312 usb_anchor_urb(urb, &ar->rx_anch);
313 err = usb_submit_urb(urb, gfp);
315 usb_unanchor_urb(urb);
316 usb_anchor_urb(urb, &ar->rx_pool);
318 atomic_dec(&ar->rx_pool_urbs);
319 atomic_inc(&ar->rx_anch_urbs);
328 static void carl9170_usb_rx_work(struct ar9170 *ar)
333 for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
334 urb = usb_get_from_anchor(&ar->rx_work);
338 atomic_dec(&ar->rx_work_urbs);
339 if (IS_INITIALIZED(ar)) {
340 carl9170_rx(ar, urb->transfer_buffer,
344 usb_anchor_urb(urb, &ar->rx_pool);
345 atomic_inc(&ar->rx_pool_urbs);
349 carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
353 void carl9170_usb_handle_tx_err(struct ar9170 *ar)
357 while ((urb = usb_get_from_anchor(&ar->tx_err))) {
358 struct sk_buff *skb = (void *)urb->context;
360 carl9170_tx_drop(ar, skb);
361 carl9170_tx_callback(ar, skb);
366 static void carl9170_usb_tasklet(unsigned long data)
368 struct ar9170 *ar = (struct ar9170 *) data;
370 carl9170_usb_rx_work(ar);
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.
378 carl9170_tx_scheduler(ar);
381 static void carl9170_usb_rx_complete(struct urb *urb)
383 struct ar9170 *ar = (struct ar9170 *)urb->context;
386 if (WARN_ON_ONCE(!ar))
389 atomic_dec(&ar->rx_anch_urbs);
391 switch (urb->status) {
394 usb_anchor_urb(urb, &ar->rx_work);
395 atomic_inc(&ar->rx_work_urbs);
402 /* handle disconnect events*/
406 /* handle all other errors */
407 usb_anchor_urb(urb, &ar->rx_pool);
408 atomic_inc(&ar->rx_pool_urbs);
412 err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
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.
420 tasklet_hi_schedule(&ar->usb_tasklet);
422 if (atomic_read(&ar->rx_anch_urbs) == 0) {
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
431 carl9170_restart(ar, CARL9170_RR_SLOW_SYSTEM);
435 * Using anything less than _high_ priority absolutely
436 * kills the rx performance my UP-System...
438 tasklet_hi_schedule(&ar->usb_tasklet);
442 static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
447 buf = kmalloc(ar->fw.rx_size, gfp);
451 urb = usb_alloc_urb(0, gfp);
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);
461 urb->transfer_flags |= URB_FREE_BUFFER;
466 static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
468 struct urb *urb = NULL;
472 urb = usb_alloc_urb(0, GFP_KERNEL);
476 ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
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);
484 urb->transfer_flags |= URB_FREE_BUFFER;
486 usb_anchor_urb(urb, &ar->rx_anch);
487 err = usb_submit_urb(urb, GFP_KERNEL);
489 usb_unanchor_urb(urb);
496 static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
499 int i, err = -EINVAL;
502 * The driver actively maintains a second shadow
503 * pool for inactive, but fully-prepared rx urbs.
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).
511 for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
512 urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
518 usb_anchor_urb(urb, &ar->rx_pool);
519 atomic_inc(&ar->rx_pool_urbs);
523 err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
527 /* the device now waiting for the firmware. */
528 carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
533 usb_scuttle_anchored_urbs(&ar->rx_pool);
534 usb_scuttle_anchored_urbs(&ar->rx_work);
535 usb_kill_anchored_urbs(&ar->rx_anch);
539 static int carl9170_usb_flush(struct ar9170 *ar)
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);
551 ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, HZ);
555 /* lets wait a while until the tx - queues are dried out */
556 ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, HZ);
560 usb_kill_anchored_urbs(&ar->tx_anch);
561 carl9170_usb_handle_tx_err(ar);
566 static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
570 carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
572 err = carl9170_usb_flush(ar);
574 dev_err(&ar->udev->dev, "stuck tx urbs!\n");
576 usb_poison_anchored_urbs(&ar->tx_anch);
577 carl9170_usb_handle_tx_err(ar);
578 usb_poison_anchored_urbs(&ar->rx_anch);
580 tasklet_kill(&ar->usb_tasklet);
582 usb_scuttle_anchored_urbs(&ar->rx_work);
583 usb_scuttle_anchored_urbs(&ar->rx_pool);
584 usb_scuttle_anchored_urbs(&ar->tx_cmd);
587 int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
592 if (!IS_INITIALIZED(ar))
595 if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4))
598 urb = usb_alloc_urb(0, GFP_ATOMIC);
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);
606 urb->transfer_flags |= URB_ZERO_PACKET;
609 urb->transfer_flags |= URB_FREE_BUFFER;
611 usb_anchor_urb(urb, &ar->tx_cmd);
614 return carl9170_usb_submit_cmd_urb(ar);
617 int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
618 unsigned int plen, void *payload, unsigned int outlen, void *out)
622 if (!IS_ACCEPTING_CMD(ar))
625 if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
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);
634 spin_lock_bh(&ar->cmd_lock);
635 ar->readbuf = (u8 *)out;
636 ar->readlen = outlen;
637 spin_unlock_bh(&ar->cmd_lock);
639 err = __carl9170_exec_cmd(ar, &ar->cmd, false);
641 if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
642 err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
648 if (ar->readlen != outlen) {
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);
662 /* provide some maybe useful debug information */
663 print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
666 carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
669 /* invalidate to avoid completing the next command prematurely */
670 spin_lock_bh(&ar->cmd_lock);
673 spin_unlock_bh(&ar->cmd_lock);
678 void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
681 struct ar9170_stream *tx_stream;
688 urb = usb_alloc_urb(0, GFP_ATOMIC);
692 if (ar->fw.tx_stream) {
693 tx_stream = (void *) (skb->data - sizeof(*tx_stream));
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);
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);
708 urb->transfer_flags |= URB_ZERO_PACKET;
710 usb_anchor_urb(urb, &ar->tx_wait);
714 carl9170_usb_submit_data_urb(ar);
718 carl9170_tx_drop(ar, skb);
719 carl9170_tx_callback(ar, skb);
722 static void carl9170_release_firmware(struct ar9170 *ar)
725 release_firmware(ar->fw.fw);
726 memset(&ar->fw, 0, sizeof(ar->fw));
730 void carl9170_usb_stop(struct ar9170 *ar)
734 carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
736 ret = carl9170_usb_flush(ar);
738 dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
740 usb_poison_anchored_urbs(&ar->tx_anch);
741 carl9170_usb_handle_tx_err(ar);
743 /* kill any pending command */
744 spin_lock_bh(&ar->cmd_lock);
746 spin_unlock_bh(&ar->cmd_lock);
747 complete_all(&ar->cmd_wait);
749 /* This is required to prevent an early completion on _start */
750 INIT_COMPLETION(ar->cmd_wait);
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...
759 int carl9170_usb_open(struct ar9170 *ar)
761 usb_unpoison_anchored_urbs(&ar->tx_anch);
763 carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
767 static int carl9170_usb_load_firmware(struct ar9170 *ar)
771 unsigned int transfer;
776 buf = kmalloc(4096, GFP_KERNEL);
782 data = ar->fw.fw->data;
783 len = ar->fw.fw->size;
784 addr = ar->fw.address;
786 /* this removes the miniboot image */
787 data += ar->fw.offset;
788 len -= ar->fw.offset;
791 transfer = min_t(unsigned int, len, 4096u);
792 memcpy(buf, data, transfer);
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);
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);
813 if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
818 err = carl9170_echo_test(ar, 0x4a110123);
822 /* firmware restarts cmd counter */
828 dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
832 int carl9170_usb_restart(struct ar9170 *ar)
836 if (ar->intf->condition != USB_INTERFACE_BOUND)
839 /* Disable command response sequence counter. */
842 err = carl9170_reboot(ar);
844 carl9170_usb_stop(ar);
846 carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
851 tasklet_schedule(&ar->usb_tasklet);
853 /* The reboot procedure can take quite a while to complete. */
856 err = carl9170_usb_open(ar);
860 err = carl9170_usb_load_firmware(ar);
867 carl9170_usb_cancel_urbs(ar);
871 void carl9170_usb_reset(struct ar9170 *ar)
874 * This is the last resort to get the device going again
875 * without any *user replugging action*.
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.
882 carl9170_usb_cancel_urbs(ar);
884 carl9170_usb_stop(ar);
886 usb_queue_reset_device(ar->intf);
889 static int carl9170_usb_init_device(struct ar9170 *ar)
893 err = carl9170_usb_send_rx_irq_urb(ar);
897 err = carl9170_usb_init_rx_bulk_urbs(ar);
901 mutex_lock(&ar->mutex);
902 err = carl9170_usb_load_firmware(ar);
903 mutex_unlock(&ar->mutex);
910 carl9170_usb_cancel_urbs(ar);
916 static void carl9170_usb_firmware_failed(struct ar9170 *ar)
918 struct device *parent = ar->udev->dev.parent;
919 struct usb_device *udev;
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).
929 complete(&ar->fw_load_wait);
931 /* unbind anything failed */
935 device_release_driver(&udev->dev);
937 device_unlock(parent);
942 static void carl9170_usb_firmware_finish(struct ar9170 *ar)
946 err = carl9170_parse_firmware(ar);
950 err = carl9170_usb_init_device(ar);
954 err = carl9170_usb_open(ar);
958 err = carl9170_register(ar);
960 carl9170_usb_stop(ar);
964 complete(&ar->fw_load_wait);
965 usb_put_dev(ar->udev);
969 carl9170_usb_cancel_urbs(ar);
972 carl9170_release_firmware(ar);
973 carl9170_usb_firmware_failed(ar);
976 static void carl9170_usb_firmware_step2(const struct firmware *fw,
979 struct ar9170 *ar = context;
983 carl9170_usb_firmware_finish(ar);
987 dev_err(&ar->udev->dev, "firmware not found.\n");
988 carl9170_usb_firmware_failed(ar);
991 static int carl9170_usb_probe(struct usb_interface *intf,
992 const struct usb_device_id *id)
995 struct usb_device *udev;
998 err = usb_reset_device(interface_to_usbdev(intf));
1002 ar = carl9170_alloc(sizeof(*ar));
1006 udev = interface_to_usbdev(intf);
1010 ar->features = id->driver_info;
1012 usb_set_intfdata(intf, ar);
1013 SET_IEEE80211_DEV(ar->hw, &intf->dev);
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,
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);
1035 usb_get_dev(ar->udev);
1037 carl9170_set_state(ar, CARL9170_STOPPED);
1039 return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
1040 &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
1043 static void carl9170_usb_disconnect(struct usb_interface *intf)
1045 struct ar9170 *ar = usb_get_intfdata(intf);
1046 struct usb_device *udev;
1052 wait_for_completion(&ar->fw_load_wait);
1054 if (IS_INITIALIZED(ar)) {
1055 carl9170_reboot(ar);
1056 carl9170_usb_stop(ar);
1059 carl9170_usb_cancel_urbs(ar);
1060 carl9170_unregister(ar);
1062 usb_set_intfdata(intf, NULL);
1064 carl9170_release_firmware(ar);
1070 static int carl9170_usb_suspend(struct usb_interface *intf,
1071 pm_message_t message)
1073 struct ar9170 *ar = usb_get_intfdata(intf);
1078 carl9170_usb_cancel_urbs(ar);
1081 * firmware automatically reboots for usb suspend.
1087 static int carl9170_usb_resume(struct usb_interface *intf)
1089 struct ar9170 *ar = usb_get_intfdata(intf);
1095 usb_unpoison_anchored_urbs(&ar->rx_anch);
1097 err = carl9170_usb_init_device(ar);
1101 err = carl9170_usb_open(ar);
1108 carl9170_usb_cancel_urbs(ar);
1112 #endif /* CONFIG_PM */
1114 static 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,
1121 .suspend = carl9170_usb_suspend,
1122 .resume = carl9170_usb_resume,
1123 #endif /* CONFIG_PM */
1126 static int __init carl9170_usb_init(void)
1128 return usb_register(&carl9170_driver);
1131 static void __exit carl9170_usb_exit(void)
1133 usb_deregister(&carl9170_driver);
1136 module_init(carl9170_usb_init);
1137 module_exit(carl9170_usb_exit);