2 * Atheros AR9170 driver
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, Christian Lamparter <chunkeey@web.de>
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/usb.h>
42 #include <linux/firmware.h>
43 #include <linux/etherdevice.h>
44 #include <net/mac80211.h>
50 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
51 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
52 MODULE_LICENSE("GPL");
53 MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
54 MODULE_FIRMWARE("ar9170.fw");
55 MODULE_FIRMWARE("ar9170-1.fw");
56 MODULE_FIRMWARE("ar9170-2.fw");
58 enum ar9170_requirements {
59 AR9170_REQ_FW1_ONLY = 1,
62 static struct usb_device_id ar9170_usb_ids[] = {
64 { USB_DEVICE(0x0cf3, 0x9170) },
66 { USB_DEVICE(0x0cf3, 0x1001) },
68 { USB_DEVICE(0xcace, 0x0300) },
70 { USB_DEVICE(0x07d1, 0x3c10) },
71 /* Netgear WNDA3100 */
72 { USB_DEVICE(0x0846, 0x9010) },
73 /* Netgear WN111 v2 */
74 { USB_DEVICE(0x0846, 0x9001) },
76 { USB_DEVICE(0x0ace, 0x1221) },
78 { USB_DEVICE(0x0586, 0x3417) },
80 { USB_DEVICE(0x0cde, 0x0023) },
82 { USB_DEVICE(0x0cde, 0x0026) },
84 { USB_DEVICE(0x083a, 0xf522) },
86 { USB_DEVICE(0x2019, 0x5304) },
87 /* IO-Data WNGDNUS2 */
88 { USB_DEVICE(0x04bb, 0x093f) },
89 /* AVM FRITZ!WLAN USB Stick N */
90 { USB_DEVICE(0x057C, 0x8401) },
91 /* AVM FRITZ!WLAN USB Stick N 2.4 */
92 { USB_DEVICE(0x057C, 0x8402), .driver_info = AR9170_REQ_FW1_ONLY },
97 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
99 static void ar9170_usb_tx_urb_complete_free(struct urb *urb)
101 struct sk_buff *skb = urb->context;
102 struct ar9170_usb *aru = (struct ar9170_usb *)
103 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
105 if (unlikely(!aru)) {
106 dev_kfree_skb_irq(skb);
110 ar9170_handle_tx_status(&aru->common, skb, false,
111 AR9170_TX_STATUS_COMPLETE);
114 static void ar9170_usb_tx_urb_complete(struct urb *urb)
118 static void ar9170_usb_irq_completed(struct urb *urb)
120 struct ar9170_usb *aru = urb->context;
122 switch (urb->status) {
123 /* everything is fine */
138 ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
142 usb_anchor_urb(urb, &aru->rx_submitted);
143 if (usb_submit_urb(urb, GFP_ATOMIC)) {
144 usb_unanchor_urb(urb);
151 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
154 static void ar9170_usb_rx_completed(struct urb *urb)
156 struct sk_buff *skb = urb->context;
157 struct ar9170_usb *aru = (struct ar9170_usb *)
158 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
164 switch (urb->status) {
165 /* everything is fine */
180 skb_put(skb, urb->actual_length);
181 ar9170_rx(&aru->common, skb);
184 skb_reset_tail_pointer(skb);
187 usb_anchor_urb(urb, &aru->rx_submitted);
188 err = usb_submit_urb(urb, GFP_ATOMIC);
190 usb_unanchor_urb(urb);
197 dev_kfree_skb_irq(skb);
200 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
201 struct urb *urb, gfp_t gfp)
205 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
209 /* reserve some space for mac80211's radiotap */
210 skb_reserve(skb, 32);
212 usb_fill_bulk_urb(urb, aru->udev,
213 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
214 skb->data, min(skb_tailroom(skb),
215 AR9170_MAX_RX_BUFFER_SIZE),
216 ar9170_usb_rx_completed, skb);
221 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
223 struct urb *urb = NULL;
227 /* initialize interrupt endpoint */
228 urb = usb_alloc_urb(0, GFP_KERNEL);
232 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
236 usb_fill_int_urb(urb, aru->udev,
237 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
238 64, ar9170_usb_irq_completed, aru, 1);
239 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
241 usb_anchor_urb(urb, &aru->rx_submitted);
242 err = usb_submit_urb(urb, GFP_KERNEL);
244 usb_unanchor_urb(urb);
245 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
254 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
260 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
262 urb = usb_alloc_urb(0, GFP_KERNEL);
266 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
272 usb_anchor_urb(urb, &aru->rx_submitted);
273 err = usb_submit_urb(urb, GFP_KERNEL);
275 usb_unanchor_urb(urb);
276 dev_kfree_skb_any((void *) urb->transfer_buffer);
283 /* the device now waiting for a firmware. */
284 aru->common.state = AR9170_IDLE;
289 usb_kill_anchored_urbs(&aru->rx_submitted);
293 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
297 aru->common.state = AR9170_UNKNOWN_STATE;
299 usb_unlink_anchored_urbs(&aru->tx_submitted);
301 /* give the LED OFF command and the deauth frame a chance to air. */
302 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
303 msecs_to_jiffies(100));
305 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
306 usb_poison_anchored_urbs(&aru->tx_submitted);
308 usb_poison_anchored_urbs(&aru->rx_submitted);
311 static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
312 unsigned int plen, void *payload,
313 unsigned int outlen, void *out)
315 struct ar9170_usb *aru = (void *) ar;
316 struct urb *urb = NULL;
320 if (unlikely(!IS_ACCEPTING_CMD(ar)))
323 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
326 urb = usb_alloc_urb(0, GFP_ATOMIC);
330 ar->cmdbuf[0] = cpu_to_le32(plen);
331 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
332 /* writing multiple regs fills this buffer already */
333 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
334 memcpy(&ar->cmdbuf[1], payload, plen);
336 spin_lock_irqsave(&aru->common.cmdlock, flags);
337 aru->readbuf = (u8 *)out;
338 aru->readlen = outlen;
339 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
341 usb_fill_int_urb(urb, aru->udev,
342 usb_sndbulkpipe(aru->udev, AR9170_EP_CMD),
343 aru->common.cmdbuf, plen + 4,
344 ar9170_usb_tx_urb_complete, NULL, 1);
346 usb_anchor_urb(urb, &aru->tx_submitted);
347 err = usb_submit_urb(urb, GFP_ATOMIC);
349 usb_unanchor_urb(urb);
355 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
361 if (aru->readlen != outlen) {
369 /* Maybe the device was removed in the second we were waiting? */
370 if (IS_STARTED(ar)) {
371 dev_err(&aru->udev->dev, "no command feedback "
372 "received (%d).\n", err);
374 /* provide some maybe useful debug information */
375 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
376 aru->common.cmdbuf, plen + 4);
380 /* invalidate to avoid completing the next prematurely */
381 spin_lock_irqsave(&aru->common.cmdlock, flags);
384 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
391 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb,
392 bool txstatus_needed, unsigned int extra_len)
394 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
398 if (unlikely(!IS_STARTED(ar))) {
399 /* Seriously, what were you drink... err... thinking!? */
403 urb = usb_alloc_urb(0, GFP_ATOMIC);
407 usb_fill_bulk_urb(urb, aru->udev,
408 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
409 skb->data, skb->len + extra_len, (txstatus_needed ?
410 ar9170_usb_tx_urb_complete :
411 ar9170_usb_tx_urb_complete_free), skb);
412 urb->transfer_flags |= URB_ZERO_PACKET;
414 usb_anchor_urb(urb, &aru->tx_submitted);
415 err = usb_submit_urb(urb, GFP_ATOMIC);
417 usb_unanchor_urb(urb);
423 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
425 struct ar9170_usb *aru = (void *) ar;
429 if (unlikely(!buffer))
432 in = le32_to_cpup((__le32 *)buffer);
433 out = le32_to_cpu(ar->cmdbuf[0]);
435 /* mask off length byte */
438 if (aru->readlen >= 0) {
439 /* add expected length */
442 /* add obtained length */
447 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
448 * length and we cannot predict the correct length in advance.
449 * So we only check if we provided enough space for the data.
451 if (unlikely(out < in)) {
452 dev_warn(&aru->udev->dev, "received invalid command response "
453 "got %d bytes, instead of %d bytes "
454 "and the resp length is %d bytes\n",
456 print_hex_dump_bytes("ar9170 invalid resp: ",
457 DUMP_PREFIX_OFFSET, buffer, len);
459 * Do not complete, then the command times out,
460 * and we get a stack trace from there.
465 spin_lock_irqsave(&aru->common.cmdlock, flags);
466 if (aru->readbuf && len > 0) {
467 memcpy(aru->readbuf, buffer + 4, len - 4);
470 complete(&aru->cmd_wait);
471 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
474 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
475 size_t len, u32 addr, bool complete)
478 u8 *buf = kmalloc(4096, GFP_KERNEL);
484 transfer = min_t(int, len, 4096);
485 memcpy(buf, data, transfer);
487 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
488 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
489 addr >> 8, 0, buf, transfer, 1000);
503 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
504 0x31 /* FW DL COMPLETE */,
505 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
511 static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
515 err = request_firmware(&aru->firmware, "ar9170.fw",
518 aru->init_values = NULL;
522 if (aru->req_one_stage_fw) {
523 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
524 "not found and is required for this device\n");
528 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
529 "not found, trying old firmware...\n");
531 err = request_firmware(&aru->init_values, "ar9170-1.fw",
534 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
536 release_firmware(aru->init_values);
537 dev_err(&aru->udev->dev, "file with init values not found.\n");
544 static int ar9170_usb_reset(struct ar9170_usb *aru)
546 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
549 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
551 dev_err(&aru->udev->dev, "unable to lock device "
552 "for reset (%d).\n", ret);
557 ret = usb_reset_device(aru->udev);
559 usb_unlock_device(aru->udev);
561 /* let it rest - for a second - */
567 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
571 if (!aru->init_values)
572 goto upload_fw_start;
574 /* First, upload initial values to device RAM */
575 err = ar9170_usb_upload(aru, aru->init_values->data,
576 aru->init_values->size, 0x102800, false);
578 dev_err(&aru->udev->dev, "firmware part 1 "
579 "upload failed (%d).\n", err);
585 /* Then, upload the firmware itself and start it */
586 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
590 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
592 struct ar9170 *ar = (void *) &aru->common;
595 ar9170_regwrite_begin(ar);
597 /* Set USB Rx stream mode MAX packet number to 2 */
598 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
600 /* Set USB Rx stream mode timeout to 10us */
601 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
603 ar9170_regwrite_finish();
605 err = ar9170_regwrite_result();
607 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
612 static void ar9170_usb_stop(struct ar9170 *ar)
614 struct ar9170_usb *aru = (void *) ar;
617 if (IS_ACCEPTING_CMD(ar))
618 aru->common.state = AR9170_STOPPED;
620 /* lets wait a while until the tx - queues are dried out */
621 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
622 msecs_to_jiffies(1000));
624 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
626 usb_poison_anchored_urbs(&aru->tx_submitted);
630 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
631 * Else we would end up with a unresponsive device...
635 static int ar9170_usb_open(struct ar9170 *ar)
637 struct ar9170_usb *aru = (void *) ar;
640 usb_unpoison_anchored_urbs(&aru->tx_submitted);
641 err = ar9170_usb_init_transport(aru);
643 usb_poison_anchored_urbs(&aru->tx_submitted);
647 aru->common.state = AR9170_IDLE;
651 static int ar9170_usb_init_device(struct ar9170_usb *aru)
655 err = ar9170_usb_alloc_rx_irq_urb(aru);
659 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
663 err = ar9170_usb_upload_firmware(aru);
665 err = ar9170_echo_test(&aru->common, 0x60d43110);
667 /* force user invention, by disabling the device */
668 err = usb_driver_set_configuration(aru->udev, -1);
669 dev_err(&aru->udev->dev, "device is in a bad state. "
670 "please reconnect it!\n");
678 ar9170_usb_cancel_urbs(aru);
684 static bool ar9170_requires_one_stage(const struct usb_device_id *id)
686 if (!id->driver_info)
688 if (id->driver_info == AR9170_REQ_FW1_ONLY)
693 static int ar9170_usb_probe(struct usb_interface *intf,
694 const struct usb_device_id *id)
696 struct ar9170_usb *aru;
698 struct usb_device *udev;
701 aru = ar9170_alloc(sizeof(*aru));
707 udev = interface_to_usbdev(intf);
713 aru->req_one_stage_fw = ar9170_requires_one_stage(id);
715 usb_set_intfdata(intf, aru);
716 SET_IEEE80211_DEV(ar->hw, &udev->dev);
718 init_usb_anchor(&aru->rx_submitted);
719 init_usb_anchor(&aru->tx_submitted);
720 init_completion(&aru->cmd_wait);
722 aru->common.stop = ar9170_usb_stop;
723 aru->common.open = ar9170_usb_open;
724 aru->common.tx = ar9170_usb_tx;
725 aru->common.exec_cmd = ar9170_usb_exec_cmd;
726 aru->common.callback_cmd = ar9170_usb_callback_cmd;
729 udev->reset_resume = 1;
730 #endif /* CONFIG_PM */
731 err = ar9170_usb_reset(aru);
735 err = ar9170_usb_request_firmware(aru);
739 err = ar9170_usb_init_device(aru);
743 err = ar9170_usb_open(ar);
747 err = ar9170_register(ar, &udev->dev);
756 ar9170_usb_cancel_urbs(aru);
759 release_firmware(aru->init_values);
760 release_firmware(aru->firmware);
763 usb_set_intfdata(intf, NULL);
765 ieee80211_free_hw(ar->hw);
770 static void ar9170_usb_disconnect(struct usb_interface *intf)
772 struct ar9170_usb *aru = usb_get_intfdata(intf);
777 aru->common.state = AR9170_IDLE;
778 ar9170_unregister(&aru->common);
779 ar9170_usb_cancel_urbs(aru);
781 release_firmware(aru->init_values);
782 release_firmware(aru->firmware);
784 usb_put_dev(aru->udev);
785 usb_set_intfdata(intf, NULL);
786 ieee80211_free_hw(aru->common.hw);
790 static int ar9170_suspend(struct usb_interface *intf,
791 pm_message_t message)
793 struct ar9170_usb *aru = usb_get_intfdata(intf);
798 aru->common.state = AR9170_IDLE;
799 ar9170_usb_cancel_urbs(aru);
804 static int ar9170_resume(struct usb_interface *intf)
806 struct ar9170_usb *aru = usb_get_intfdata(intf);
812 usb_unpoison_anchored_urbs(&aru->rx_submitted);
813 usb_unpoison_anchored_urbs(&aru->tx_submitted);
815 err = ar9170_usb_init_device(aru);
819 err = ar9170_usb_open(&aru->common);
826 aru->common.state = AR9170_IDLE;
827 ar9170_usb_cancel_urbs(aru);
831 #endif /* CONFIG_PM */
833 static struct usb_driver ar9170_driver = {
835 .probe = ar9170_usb_probe,
836 .disconnect = ar9170_usb_disconnect,
837 .id_table = ar9170_usb_ids,
840 .suspend = ar9170_suspend,
841 .resume = ar9170_resume,
842 .reset_resume = ar9170_resume,
843 #endif /* CONFIG_PM */
846 static int __init ar9170_init(void)
848 return usb_register(&ar9170_driver);
851 static void __exit ar9170_exit(void)
853 usb_deregister(&ar9170_driver);
856 module_init(ar9170_init);
857 module_exit(ar9170_exit);