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(0x07d1, 0x3c10) },
64 /* Netgear WNDA3100 */
65 { USB_DEVICE(0x0846, 0x9010) },
66 /* Netgear WN111 v2 */
67 { USB_DEVICE(0x0846, 0x9001) },
69 { USB_DEVICE(0x0ace, 0x1221) },
71 { USB_DEVICE(0x0cde, 0x0023) },
73 { USB_DEVICE(0x0cde, 0x0026) },
75 { USB_DEVICE(0x083a, 0xf522) },
77 { USB_DEVICE(0x2019, 0x5304) },
78 /* IO-Data WNGDNUS2 */
79 { USB_DEVICE(0x04bb, 0x093f) },
84 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
86 static void ar9170_usb_tx_urb_complete_free(struct urb *urb)
88 struct sk_buff *skb = urb->context;
89 struct ar9170_usb *aru = (struct ar9170_usb *)
90 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
93 dev_kfree_skb_irq(skb);
97 ar9170_handle_tx_status(&aru->common, skb, false,
98 AR9170_TX_STATUS_COMPLETE);
101 static void ar9170_usb_tx_urb_complete(struct urb *urb)
105 static void ar9170_usb_irq_completed(struct urb *urb)
107 struct ar9170_usb *aru = urb->context;
109 switch (urb->status) {
110 /* everything is fine */
125 print_hex_dump_bytes("ar9170 irq: ", DUMP_PREFIX_OFFSET,
126 urb->transfer_buffer, urb->actual_length);
129 usb_anchor_urb(urb, &aru->rx_submitted);
130 if (usb_submit_urb(urb, GFP_ATOMIC)) {
131 usb_unanchor_urb(urb);
138 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
141 static void ar9170_usb_rx_completed(struct urb *urb)
143 struct sk_buff *skb = urb->context;
144 struct ar9170_usb *aru = (struct ar9170_usb *)
145 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
151 switch (urb->status) {
152 /* everything is fine */
167 skb_put(skb, urb->actual_length);
168 ar9170_rx(&aru->common, skb);
171 skb_reset_tail_pointer(skb);
174 usb_anchor_urb(urb, &aru->rx_submitted);
175 err = usb_submit_urb(urb, GFP_ATOMIC);
177 usb_unanchor_urb(urb);
178 dev_kfree_skb_irq(skb);
184 dev_kfree_skb_irq(skb);
188 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
189 struct urb *urb, gfp_t gfp)
193 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
197 /* reserve some space for mac80211's radiotap */
198 skb_reserve(skb, 32);
200 usb_fill_bulk_urb(urb, aru->udev,
201 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
202 skb->data, min(skb_tailroom(skb),
203 AR9170_MAX_RX_BUFFER_SIZE),
204 ar9170_usb_rx_completed, skb);
209 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
211 struct urb *urb = NULL;
215 /* initialize interrupt endpoint */
216 urb = usb_alloc_urb(0, GFP_KERNEL);
220 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
224 usb_fill_int_urb(urb, aru->udev,
225 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
226 64, ar9170_usb_irq_completed, aru, 1);
227 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
229 usb_anchor_urb(urb, &aru->rx_submitted);
230 err = usb_submit_urb(urb, GFP_KERNEL);
232 usb_unanchor_urb(urb);
233 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
242 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
248 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
250 urb = usb_alloc_urb(0, GFP_KERNEL);
254 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
260 usb_anchor_urb(urb, &aru->rx_submitted);
261 err = usb_submit_urb(urb, GFP_KERNEL);
263 usb_unanchor_urb(urb);
264 dev_kfree_skb_any((void *) urb->transfer_buffer);
271 /* the device now waiting for a firmware. */
272 aru->common.state = AR9170_IDLE;
277 usb_kill_anchored_urbs(&aru->rx_submitted);
281 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
285 aru->common.state = AR9170_UNKNOWN_STATE;
287 usb_unlink_anchored_urbs(&aru->tx_submitted);
289 /* give the LED OFF command and the deauth frame a chance to air. */
290 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
291 msecs_to_jiffies(100));
293 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
294 usb_poison_anchored_urbs(&aru->tx_submitted);
296 usb_poison_anchored_urbs(&aru->rx_submitted);
299 static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
300 unsigned int plen, void *payload,
301 unsigned int outlen, void *out)
303 struct ar9170_usb *aru = (void *) ar;
304 struct urb *urb = NULL;
308 if (unlikely(!IS_ACCEPTING_CMD(ar)))
311 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
314 urb = usb_alloc_urb(0, GFP_ATOMIC);
318 ar->cmdbuf[0] = cpu_to_le32(plen);
319 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
320 /* writing multiple regs fills this buffer already */
321 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
322 memcpy(&ar->cmdbuf[1], payload, plen);
324 spin_lock_irqsave(&aru->common.cmdlock, flags);
325 aru->readbuf = (u8 *)out;
326 aru->readlen = outlen;
327 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
329 usb_fill_int_urb(urb, aru->udev,
330 usb_sndbulkpipe(aru->udev, AR9170_EP_CMD),
331 aru->common.cmdbuf, plen + 4,
332 ar9170_usb_tx_urb_complete, NULL, 1);
334 usb_anchor_urb(urb, &aru->tx_submitted);
335 err = usb_submit_urb(urb, GFP_ATOMIC);
337 usb_unanchor_urb(urb);
343 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
349 if (outlen >= 0 && aru->readlen != outlen) {
357 /* Maybe the device was removed in the second we were waiting? */
358 if (IS_STARTED(ar)) {
359 dev_err(&aru->udev->dev, "no command feedback "
360 "received (%d).\n", err);
362 /* provide some maybe useful debug information */
363 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
364 aru->common.cmdbuf, plen + 4);
368 /* invalidate to avoid completing the next prematurely */
369 spin_lock_irqsave(&aru->common.cmdlock, flags);
372 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
379 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb,
380 bool txstatus_needed, unsigned int extra_len)
382 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
386 if (unlikely(!IS_STARTED(ar))) {
387 /* Seriously, what were you drink... err... thinking!? */
391 urb = usb_alloc_urb(0, GFP_ATOMIC);
395 usb_fill_bulk_urb(urb, aru->udev,
396 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
397 skb->data, skb->len + extra_len, (txstatus_needed ?
398 ar9170_usb_tx_urb_complete :
399 ar9170_usb_tx_urb_complete_free), skb);
400 urb->transfer_flags |= URB_ZERO_PACKET;
402 usb_anchor_urb(urb, &aru->tx_submitted);
403 err = usb_submit_urb(urb, GFP_ATOMIC);
405 usb_unanchor_urb(urb);
411 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
413 struct ar9170_usb *aru = (void *) ar;
420 in = le32_to_cpup((__le32 *)buffer);
421 out = le32_to_cpu(ar->cmdbuf[0]);
423 /* mask off length byte */
426 if (aru->readlen >= 0) {
427 /* add expected length */
430 /* add obtained length */
435 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
436 * length and we cannot predict the correct length in advance.
437 * So we only check if we provided enough space for the data.
439 if (unlikely(out < in)) {
440 dev_warn(&aru->udev->dev, "received invalid command response "
441 "got %d bytes, instead of %d bytes "
442 "and the resp length is %d bytes\n",
444 print_hex_dump_bytes("ar9170 invalid resp: ",
445 DUMP_PREFIX_OFFSET, buffer, len);
447 * Do not complete, then the command times out,
448 * and we get a stack trace from there.
453 spin_lock_irqsave(&aru->common.cmdlock, flags);
454 if (aru->readbuf && len > 0) {
455 memcpy(aru->readbuf, buffer + 4, len - 4);
458 complete(&aru->cmd_wait);
459 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
462 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
463 size_t len, u32 addr, bool complete)
466 u8 *buf = kmalloc(4096, GFP_KERNEL);
472 transfer = min_t(int, len, 4096);
473 memcpy(buf, data, transfer);
475 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
476 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
477 addr >> 8, 0, buf, transfer, 1000);
491 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
492 0x31 /* FW DL COMPLETE */,
493 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
499 static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
503 err = request_firmware(&aru->init_values, "ar9170-1.fw",
506 dev_err(&aru->udev->dev, "file with init values not found.\n");
510 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
512 release_firmware(aru->init_values);
513 dev_err(&aru->udev->dev, "firmware file not found.\n");
520 static int ar9170_usb_reset(struct ar9170_usb *aru)
522 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
525 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
527 dev_err(&aru->udev->dev, "unable to lock device "
528 "for reset (%d).\n", ret);
533 ret = usb_reset_device(aru->udev);
535 usb_unlock_device(aru->udev);
537 /* let it rest - for a second - */
543 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
547 /* First, upload initial values to device RAM */
548 err = ar9170_usb_upload(aru, aru->init_values->data,
549 aru->init_values->size, 0x102800, false);
551 dev_err(&aru->udev->dev, "firmware part 1 "
552 "upload failed (%d).\n", err);
556 /* Then, upload the firmware itself and start it */
557 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
561 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
563 struct ar9170 *ar = (void *) &aru->common;
566 ar9170_regwrite_begin(ar);
568 /* Set USB Rx stream mode MAX packet number to 2 */
569 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
571 /* Set USB Rx stream mode timeout to 10us */
572 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
574 ar9170_regwrite_finish();
576 err = ar9170_regwrite_result();
578 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
583 static void ar9170_usb_stop(struct ar9170 *ar)
585 struct ar9170_usb *aru = (void *) ar;
588 if (IS_ACCEPTING_CMD(ar))
589 aru->common.state = AR9170_STOPPED;
591 /* lets wait a while until the tx - queues are dried out */
592 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
593 msecs_to_jiffies(1000));
595 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
597 usb_poison_anchored_urbs(&aru->tx_submitted);
601 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
602 * Else we would end up with a unresponsive device...
606 static int ar9170_usb_open(struct ar9170 *ar)
608 struct ar9170_usb *aru = (void *) ar;
611 usb_unpoison_anchored_urbs(&aru->tx_submitted);
612 err = ar9170_usb_init_transport(aru);
614 usb_poison_anchored_urbs(&aru->tx_submitted);
618 aru->common.state = AR9170_IDLE;
622 static int ar9170_usb_probe(struct usb_interface *intf,
623 const struct usb_device_id *id)
625 struct ar9170_usb *aru;
627 struct usb_device *udev;
630 aru = ar9170_alloc(sizeof(*aru));
636 udev = interface_to_usbdev(intf);
642 usb_set_intfdata(intf, aru);
643 SET_IEEE80211_DEV(ar->hw, &udev->dev);
645 init_usb_anchor(&aru->rx_submitted);
646 init_usb_anchor(&aru->tx_submitted);
647 init_completion(&aru->cmd_wait);
649 aru->common.stop = ar9170_usb_stop;
650 aru->common.open = ar9170_usb_open;
651 aru->common.tx = ar9170_usb_tx;
652 aru->common.exec_cmd = ar9170_usb_exec_cmd;
653 aru->common.callback_cmd = ar9170_usb_callback_cmd;
655 err = ar9170_usb_reset(aru);
659 err = ar9170_usb_request_firmware(aru);
663 err = ar9170_usb_alloc_rx_irq_urb(aru);
667 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
671 err = ar9170_usb_upload_firmware(aru);
673 err = ar9170_echo_test(&aru->common, 0x60d43110);
675 /* force user invention, by disabling the device */
676 err = usb_driver_set_configuration(aru->udev, -1);
677 dev_err(&aru->udev->dev, "device is in a bad state. "
678 "please reconnect it!\n");
683 err = ar9170_usb_open(ar);
687 err = ar9170_register(ar, &udev->dev);
696 ar9170_usb_cancel_urbs(aru);
699 release_firmware(aru->init_values);
700 release_firmware(aru->firmware);
703 usb_set_intfdata(intf, NULL);
705 ieee80211_free_hw(ar->hw);
710 static void ar9170_usb_disconnect(struct usb_interface *intf)
712 struct ar9170_usb *aru = usb_get_intfdata(intf);
717 aru->common.state = AR9170_IDLE;
718 ar9170_unregister(&aru->common);
719 ar9170_usb_cancel_urbs(aru);
721 release_firmware(aru->init_values);
722 release_firmware(aru->firmware);
724 usb_put_dev(aru->udev);
725 usb_set_intfdata(intf, NULL);
726 ieee80211_free_hw(aru->common.hw);
729 static struct usb_driver ar9170_driver = {
731 .probe = ar9170_usb_probe,
732 .disconnect = ar9170_usb_disconnect,
733 .id_table = ar9170_usb_ids,
737 static int __init ar9170_init(void)
739 return usb_register(&ar9170_driver);
742 static void __exit ar9170_exit(void)
744 usb_deregister(&ar9170_driver);
747 module_init(ar9170_init);
748 module_exit(ar9170_exit);