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-1.fw");
55 MODULE_FIRMWARE("ar9170-2.fw");
57 static struct usb_device_id ar9170_usb_ids[] = {
59 { USB_DEVICE(0x0cf3, 0x9170) },
61 { USB_DEVICE(0x0cf3, 0x1001) },
63 { USB_DEVICE(0xcace, 0x0300) },
65 { USB_DEVICE(0x07d1, 0x3c10) },
66 /* Netgear WNDA3100 */
67 { USB_DEVICE(0x0846, 0x9010) },
68 /* Netgear WN111 v2 */
69 { USB_DEVICE(0x0846, 0x9001) },
71 { USB_DEVICE(0x0ace, 0x1221) },
73 { USB_DEVICE(0x0cde, 0x0023) },
75 { USB_DEVICE(0x0cde, 0x0026) },
77 { USB_DEVICE(0x083a, 0xf522) },
79 { USB_DEVICE(0x2019, 0x5304) },
80 /* IO-Data WNGDNUS2 */
81 { USB_DEVICE(0x04bb, 0x093f) },
86 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
88 static void ar9170_usb_tx_urb_complete_free(struct urb *urb)
90 struct sk_buff *skb = urb->context;
91 struct ar9170_usb *aru = (struct ar9170_usb *)
92 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
95 dev_kfree_skb_irq(skb);
99 ar9170_handle_tx_status(&aru->common, skb, false,
100 AR9170_TX_STATUS_COMPLETE);
103 static void ar9170_usb_tx_urb_complete(struct urb *urb)
107 static void ar9170_usb_irq_completed(struct urb *urb)
109 struct ar9170_usb *aru = urb->context;
111 switch (urb->status) {
112 /* everything is fine */
127 print_hex_dump_bytes("ar9170 irq: ", DUMP_PREFIX_OFFSET,
128 urb->transfer_buffer, urb->actual_length);
131 usb_anchor_urb(urb, &aru->rx_submitted);
132 if (usb_submit_urb(urb, GFP_ATOMIC)) {
133 usb_unanchor_urb(urb);
140 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
143 static void ar9170_usb_rx_completed(struct urb *urb)
145 struct sk_buff *skb = urb->context;
146 struct ar9170_usb *aru = (struct ar9170_usb *)
147 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
153 switch (urb->status) {
154 /* everything is fine */
169 skb_put(skb, urb->actual_length);
170 ar9170_rx(&aru->common, skb);
173 skb_reset_tail_pointer(skb);
176 usb_anchor_urb(urb, &aru->rx_submitted);
177 err = usb_submit_urb(urb, GFP_ATOMIC);
179 usb_unanchor_urb(urb);
180 dev_kfree_skb_irq(skb);
186 dev_kfree_skb_irq(skb);
190 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
191 struct urb *urb, gfp_t gfp)
195 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
199 /* reserve some space for mac80211's radiotap */
200 skb_reserve(skb, 32);
202 usb_fill_bulk_urb(urb, aru->udev,
203 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
204 skb->data, min(skb_tailroom(skb),
205 AR9170_MAX_RX_BUFFER_SIZE),
206 ar9170_usb_rx_completed, skb);
211 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
213 struct urb *urb = NULL;
217 /* initialize interrupt endpoint */
218 urb = usb_alloc_urb(0, GFP_KERNEL);
222 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
226 usb_fill_int_urb(urb, aru->udev,
227 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
228 64, ar9170_usb_irq_completed, aru, 1);
229 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
231 usb_anchor_urb(urb, &aru->rx_submitted);
232 err = usb_submit_urb(urb, GFP_KERNEL);
234 usb_unanchor_urb(urb);
235 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
244 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
250 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
252 urb = usb_alloc_urb(0, GFP_KERNEL);
256 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
262 usb_anchor_urb(urb, &aru->rx_submitted);
263 err = usb_submit_urb(urb, GFP_KERNEL);
265 usb_unanchor_urb(urb);
266 dev_kfree_skb_any((void *) urb->transfer_buffer);
273 /* the device now waiting for a firmware. */
274 aru->common.state = AR9170_IDLE;
279 usb_kill_anchored_urbs(&aru->rx_submitted);
283 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
287 aru->common.state = AR9170_UNKNOWN_STATE;
289 usb_unlink_anchored_urbs(&aru->tx_submitted);
291 /* give the LED OFF command and the deauth frame a chance to air. */
292 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
293 msecs_to_jiffies(100));
295 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
296 usb_poison_anchored_urbs(&aru->tx_submitted);
298 usb_poison_anchored_urbs(&aru->rx_submitted);
301 static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
302 unsigned int plen, void *payload,
303 unsigned int outlen, void *out)
305 struct ar9170_usb *aru = (void *) ar;
306 struct urb *urb = NULL;
310 if (unlikely(!IS_ACCEPTING_CMD(ar)))
313 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
316 urb = usb_alloc_urb(0, GFP_ATOMIC);
320 ar->cmdbuf[0] = cpu_to_le32(plen);
321 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
322 /* writing multiple regs fills this buffer already */
323 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
324 memcpy(&ar->cmdbuf[1], payload, plen);
326 spin_lock_irqsave(&aru->common.cmdlock, flags);
327 aru->readbuf = (u8 *)out;
328 aru->readlen = outlen;
329 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
331 usb_fill_int_urb(urb, aru->udev,
332 usb_sndbulkpipe(aru->udev, AR9170_EP_CMD),
333 aru->common.cmdbuf, plen + 4,
334 ar9170_usb_tx_urb_complete, NULL, 1);
336 usb_anchor_urb(urb, &aru->tx_submitted);
337 err = usb_submit_urb(urb, GFP_ATOMIC);
339 usb_unanchor_urb(urb);
345 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
351 if (outlen >= 0 && aru->readlen != outlen) {
359 /* Maybe the device was removed in the second we were waiting? */
360 if (IS_STARTED(ar)) {
361 dev_err(&aru->udev->dev, "no command feedback "
362 "received (%d).\n", err);
364 /* provide some maybe useful debug information */
365 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
366 aru->common.cmdbuf, plen + 4);
370 /* invalidate to avoid completing the next prematurely */
371 spin_lock_irqsave(&aru->common.cmdlock, flags);
374 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
381 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb,
382 bool txstatus_needed, unsigned int extra_len)
384 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
388 if (unlikely(!IS_STARTED(ar))) {
389 /* Seriously, what were you drink... err... thinking!? */
393 urb = usb_alloc_urb(0, GFP_ATOMIC);
397 usb_fill_bulk_urb(urb, aru->udev,
398 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
399 skb->data, skb->len + extra_len, (txstatus_needed ?
400 ar9170_usb_tx_urb_complete :
401 ar9170_usb_tx_urb_complete_free), skb);
402 urb->transfer_flags |= URB_ZERO_PACKET;
404 usb_anchor_urb(urb, &aru->tx_submitted);
405 err = usb_submit_urb(urb, GFP_ATOMIC);
407 usb_unanchor_urb(urb);
413 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
415 struct ar9170_usb *aru = (void *) ar;
422 in = le32_to_cpup((__le32 *)buffer);
423 out = le32_to_cpu(ar->cmdbuf[0]);
425 /* mask off length byte */
428 if (aru->readlen >= 0) {
429 /* add expected length */
432 /* add obtained length */
437 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
438 * length and we cannot predict the correct length in advance.
439 * So we only check if we provided enough space for the data.
441 if (unlikely(out < in)) {
442 dev_warn(&aru->udev->dev, "received invalid command response "
443 "got %d bytes, instead of %d bytes "
444 "and the resp length is %d bytes\n",
446 print_hex_dump_bytes("ar9170 invalid resp: ",
447 DUMP_PREFIX_OFFSET, buffer, len);
449 * Do not complete, then the command times out,
450 * and we get a stack trace from there.
455 spin_lock_irqsave(&aru->common.cmdlock, flags);
456 if (aru->readbuf && len > 0) {
457 memcpy(aru->readbuf, buffer + 4, len - 4);
460 complete(&aru->cmd_wait);
461 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
464 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
465 size_t len, u32 addr, bool complete)
468 u8 *buf = kmalloc(4096, GFP_KERNEL);
474 transfer = min_t(int, len, 4096);
475 memcpy(buf, data, transfer);
477 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
478 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
479 addr >> 8, 0, buf, transfer, 1000);
493 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
494 0x31 /* FW DL COMPLETE */,
495 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
501 static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
505 err = request_firmware(&aru->init_values, "ar9170-1.fw",
508 dev_err(&aru->udev->dev, "file with init values not found.\n");
512 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
514 release_firmware(aru->init_values);
515 dev_err(&aru->udev->dev, "firmware file not found.\n");
522 static int ar9170_usb_reset(struct ar9170_usb *aru)
524 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
527 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
529 dev_err(&aru->udev->dev, "unable to lock device "
530 "for reset (%d).\n", ret);
535 ret = usb_reset_device(aru->udev);
537 usb_unlock_device(aru->udev);
539 /* let it rest - for a second - */
545 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
549 /* First, upload initial values to device RAM */
550 err = ar9170_usb_upload(aru, aru->init_values->data,
551 aru->init_values->size, 0x102800, false);
553 dev_err(&aru->udev->dev, "firmware part 1 "
554 "upload failed (%d).\n", err);
558 /* Then, upload the firmware itself and start it */
559 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
563 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
565 struct ar9170 *ar = (void *) &aru->common;
568 ar9170_regwrite_begin(ar);
570 /* Set USB Rx stream mode MAX packet number to 2 */
571 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
573 /* Set USB Rx stream mode timeout to 10us */
574 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
576 ar9170_regwrite_finish();
578 err = ar9170_regwrite_result();
580 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
585 static void ar9170_usb_stop(struct ar9170 *ar)
587 struct ar9170_usb *aru = (void *) ar;
590 if (IS_ACCEPTING_CMD(ar))
591 aru->common.state = AR9170_STOPPED;
593 /* lets wait a while until the tx - queues are dried out */
594 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
595 msecs_to_jiffies(1000));
597 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
599 usb_poison_anchored_urbs(&aru->tx_submitted);
603 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
604 * Else we would end up with a unresponsive device...
608 static int ar9170_usb_open(struct ar9170 *ar)
610 struct ar9170_usb *aru = (void *) ar;
613 usb_unpoison_anchored_urbs(&aru->tx_submitted);
614 err = ar9170_usb_init_transport(aru);
616 usb_poison_anchored_urbs(&aru->tx_submitted);
620 aru->common.state = AR9170_IDLE;
624 static int ar9170_usb_probe(struct usb_interface *intf,
625 const struct usb_device_id *id)
627 struct ar9170_usb *aru;
629 struct usb_device *udev;
632 aru = ar9170_alloc(sizeof(*aru));
638 udev = interface_to_usbdev(intf);
644 usb_set_intfdata(intf, aru);
645 SET_IEEE80211_DEV(ar->hw, &udev->dev);
647 init_usb_anchor(&aru->rx_submitted);
648 init_usb_anchor(&aru->tx_submitted);
649 init_completion(&aru->cmd_wait);
651 aru->common.stop = ar9170_usb_stop;
652 aru->common.open = ar9170_usb_open;
653 aru->common.tx = ar9170_usb_tx;
654 aru->common.exec_cmd = ar9170_usb_exec_cmd;
655 aru->common.callback_cmd = ar9170_usb_callback_cmd;
657 err = ar9170_usb_reset(aru);
661 err = ar9170_usb_request_firmware(aru);
665 err = ar9170_usb_alloc_rx_irq_urb(aru);
669 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
673 err = ar9170_usb_upload_firmware(aru);
675 err = ar9170_echo_test(&aru->common, 0x60d43110);
677 /* force user invention, by disabling the device */
678 err = usb_driver_set_configuration(aru->udev, -1);
679 dev_err(&aru->udev->dev, "device is in a bad state. "
680 "please reconnect it!\n");
685 err = ar9170_usb_open(ar);
689 err = ar9170_register(ar, &udev->dev);
698 ar9170_usb_cancel_urbs(aru);
701 release_firmware(aru->init_values);
702 release_firmware(aru->firmware);
705 usb_set_intfdata(intf, NULL);
707 ieee80211_free_hw(ar->hw);
712 static void ar9170_usb_disconnect(struct usb_interface *intf)
714 struct ar9170_usb *aru = usb_get_intfdata(intf);
719 aru->common.state = AR9170_IDLE;
720 ar9170_unregister(&aru->common);
721 ar9170_usb_cancel_urbs(aru);
723 release_firmware(aru->init_values);
724 release_firmware(aru->firmware);
726 usb_put_dev(aru->udev);
727 usb_set_intfdata(intf, NULL);
728 ieee80211_free_hw(aru->common.hw);
731 static struct usb_driver ar9170_driver = {
733 .probe = ar9170_usb_probe,
734 .disconnect = ar9170_usb_disconnect,
735 .id_table = ar9170_usb_ids,
739 static int __init ar9170_init(void)
741 return usb_register(&ar9170_driver);
744 static void __exit ar9170_exit(void)
746 usb_deregister(&ar9170_driver);
749 module_init(ar9170_init);
750 module_exit(ar9170_exit);