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(0x0586, 0x3417) },
75 { USB_DEVICE(0x0cde, 0x0023) },
77 { USB_DEVICE(0x0cde, 0x0026) },
79 { USB_DEVICE(0x083a, 0xf522) },
81 { USB_DEVICE(0x2019, 0x5304) },
82 /* IO-Data WNGDNUS2 */
83 { USB_DEVICE(0x04bb, 0x093f) },
88 MODULE_DEVICE_TABLE(usb
, ar9170_usb_ids
);
90 static void ar9170_usb_tx_urb_complete_free(struct urb
*urb
)
92 struct sk_buff
*skb
= urb
->context
;
93 struct ar9170_usb
*aru
= (struct ar9170_usb
*)
94 usb_get_intfdata(usb_ifnum_to_if(urb
->dev
, 0));
97 dev_kfree_skb_irq(skb
);
101 ar9170_handle_tx_status(&aru
->common
, skb
, false,
102 AR9170_TX_STATUS_COMPLETE
);
105 static void ar9170_usb_tx_urb_complete(struct urb
*urb
)
109 static void ar9170_usb_irq_completed(struct urb
*urb
)
111 struct ar9170_usb
*aru
= urb
->context
;
113 switch (urb
->status
) {
114 /* everything is fine */
129 print_hex_dump_bytes("ar9170 irq: ", DUMP_PREFIX_OFFSET
,
130 urb
->transfer_buffer
, urb
->actual_length
);
133 usb_anchor_urb(urb
, &aru
->rx_submitted
);
134 if (usb_submit_urb(urb
, GFP_ATOMIC
)) {
135 usb_unanchor_urb(urb
);
142 usb_buffer_free(aru
->udev
, 64, urb
->transfer_buffer
, urb
->transfer_dma
);
145 static void ar9170_usb_rx_completed(struct urb
*urb
)
147 struct sk_buff
*skb
= urb
->context
;
148 struct ar9170_usb
*aru
= (struct ar9170_usb
*)
149 usb_get_intfdata(usb_ifnum_to_if(urb
->dev
, 0));
155 switch (urb
->status
) {
156 /* everything is fine */
171 skb_put(skb
, urb
->actual_length
);
172 ar9170_rx(&aru
->common
, skb
);
175 skb_reset_tail_pointer(skb
);
178 usb_anchor_urb(urb
, &aru
->rx_submitted
);
179 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
181 usb_unanchor_urb(urb
);
182 dev_kfree_skb_irq(skb
);
188 dev_kfree_skb_irq(skb
);
192 static int ar9170_usb_prep_rx_urb(struct ar9170_usb
*aru
,
193 struct urb
*urb
, gfp_t gfp
)
197 skb
= __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE
+ 32, gfp
);
201 /* reserve some space for mac80211's radiotap */
202 skb_reserve(skb
, 32);
204 usb_fill_bulk_urb(urb
, aru
->udev
,
205 usb_rcvbulkpipe(aru
->udev
, AR9170_EP_RX
),
206 skb
->data
, min(skb_tailroom(skb
),
207 AR9170_MAX_RX_BUFFER_SIZE
),
208 ar9170_usb_rx_completed
, skb
);
213 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb
*aru
)
215 struct urb
*urb
= NULL
;
219 /* initialize interrupt endpoint */
220 urb
= usb_alloc_urb(0, GFP_KERNEL
);
224 ibuf
= usb_buffer_alloc(aru
->udev
, 64, GFP_KERNEL
, &urb
->transfer_dma
);
228 usb_fill_int_urb(urb
, aru
->udev
,
229 usb_rcvintpipe(aru
->udev
, AR9170_EP_IRQ
), ibuf
,
230 64, ar9170_usb_irq_completed
, aru
, 1);
231 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
233 usb_anchor_urb(urb
, &aru
->rx_submitted
);
234 err
= usb_submit_urb(urb
, GFP_KERNEL
);
236 usb_unanchor_urb(urb
);
237 usb_buffer_free(aru
->udev
, 64, urb
->transfer_buffer
,
246 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb
*aru
)
252 for (i
= 0; i
< AR9170_NUM_RX_URBS
; i
++) {
254 urb
= usb_alloc_urb(0, GFP_KERNEL
);
258 err
= ar9170_usb_prep_rx_urb(aru
, urb
, GFP_KERNEL
);
264 usb_anchor_urb(urb
, &aru
->rx_submitted
);
265 err
= usb_submit_urb(urb
, GFP_KERNEL
);
267 usb_unanchor_urb(urb
);
268 dev_kfree_skb_any((void *) urb
->transfer_buffer
);
275 /* the device now waiting for a firmware. */
276 aru
->common
.state
= AR9170_IDLE
;
281 usb_kill_anchored_urbs(&aru
->rx_submitted
);
285 static void ar9170_usb_cancel_urbs(struct ar9170_usb
*aru
)
289 aru
->common
.state
= AR9170_UNKNOWN_STATE
;
291 usb_unlink_anchored_urbs(&aru
->tx_submitted
);
293 /* give the LED OFF command and the deauth frame a chance to air. */
294 ret
= usb_wait_anchor_empty_timeout(&aru
->tx_submitted
,
295 msecs_to_jiffies(100));
297 dev_err(&aru
->udev
->dev
, "kill pending tx urbs.\n");
298 usb_poison_anchored_urbs(&aru
->tx_submitted
);
300 usb_poison_anchored_urbs(&aru
->rx_submitted
);
303 static int ar9170_usb_exec_cmd(struct ar9170
*ar
, enum ar9170_cmd cmd
,
304 unsigned int plen
, void *payload
,
305 unsigned int outlen
, void *out
)
307 struct ar9170_usb
*aru
= (void *) ar
;
308 struct urb
*urb
= NULL
;
312 if (unlikely(!IS_ACCEPTING_CMD(ar
)))
315 if (WARN_ON(plen
> AR9170_MAX_CMD_LEN
- 4))
318 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
322 ar
->cmdbuf
[0] = cpu_to_le32(plen
);
323 ar
->cmdbuf
[0] |= cpu_to_le32(cmd
<< 8);
324 /* writing multiple regs fills this buffer already */
325 if (plen
&& payload
!= (u8
*)(&ar
->cmdbuf
[1]))
326 memcpy(&ar
->cmdbuf
[1], payload
, plen
);
328 spin_lock_irqsave(&aru
->common
.cmdlock
, flags
);
329 aru
->readbuf
= (u8
*)out
;
330 aru
->readlen
= outlen
;
331 spin_unlock_irqrestore(&aru
->common
.cmdlock
, flags
);
333 usb_fill_int_urb(urb
, aru
->udev
,
334 usb_sndbulkpipe(aru
->udev
, AR9170_EP_CMD
),
335 aru
->common
.cmdbuf
, plen
+ 4,
336 ar9170_usb_tx_urb_complete
, NULL
, 1);
338 usb_anchor_urb(urb
, &aru
->tx_submitted
);
339 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
341 usb_unanchor_urb(urb
);
347 err
= wait_for_completion_timeout(&aru
->cmd_wait
, HZ
);
353 if (outlen
>= 0 && aru
->readlen
!= outlen
) {
361 /* Maybe the device was removed in the second we were waiting? */
362 if (IS_STARTED(ar
)) {
363 dev_err(&aru
->udev
->dev
, "no command feedback "
364 "received (%d).\n", err
);
366 /* provide some maybe useful debug information */
367 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE
,
368 aru
->common
.cmdbuf
, plen
+ 4);
372 /* invalidate to avoid completing the next prematurely */
373 spin_lock_irqsave(&aru
->common
.cmdlock
, flags
);
376 spin_unlock_irqrestore(&aru
->common
.cmdlock
, flags
);
383 static int ar9170_usb_tx(struct ar9170
*ar
, struct sk_buff
*skb
,
384 bool txstatus_needed
, unsigned int extra_len
)
386 struct ar9170_usb
*aru
= (struct ar9170_usb
*) ar
;
390 if (unlikely(!IS_STARTED(ar
))) {
391 /* Seriously, what were you drink... err... thinking!? */
395 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
399 usb_fill_bulk_urb(urb
, aru
->udev
,
400 usb_sndbulkpipe(aru
->udev
, AR9170_EP_TX
),
401 skb
->data
, skb
->len
+ extra_len
, (txstatus_needed
?
402 ar9170_usb_tx_urb_complete
:
403 ar9170_usb_tx_urb_complete_free
), skb
);
404 urb
->transfer_flags
|= URB_ZERO_PACKET
;
406 usb_anchor_urb(urb
, &aru
->tx_submitted
);
407 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
409 usb_unanchor_urb(urb
);
415 static void ar9170_usb_callback_cmd(struct ar9170
*ar
, u32 len
, void *buffer
)
417 struct ar9170_usb
*aru
= (void *) ar
;
424 in
= le32_to_cpup((__le32
*)buffer
);
425 out
= le32_to_cpu(ar
->cmdbuf
[0]);
427 /* mask off length byte */
430 if (aru
->readlen
>= 0) {
431 /* add expected length */
434 /* add obtained length */
439 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
440 * length and we cannot predict the correct length in advance.
441 * So we only check if we provided enough space for the data.
443 if (unlikely(out
< in
)) {
444 dev_warn(&aru
->udev
->dev
, "received invalid command response "
445 "got %d bytes, instead of %d bytes "
446 "and the resp length is %d bytes\n",
448 print_hex_dump_bytes("ar9170 invalid resp: ",
449 DUMP_PREFIX_OFFSET
, buffer
, len
);
451 * Do not complete, then the command times out,
452 * and we get a stack trace from there.
457 spin_lock_irqsave(&aru
->common
.cmdlock
, flags
);
458 if (aru
->readbuf
&& len
> 0) {
459 memcpy(aru
->readbuf
, buffer
+ 4, len
- 4);
462 complete(&aru
->cmd_wait
);
463 spin_unlock_irqrestore(&aru
->common
.cmdlock
, flags
);
466 static int ar9170_usb_upload(struct ar9170_usb
*aru
, const void *data
,
467 size_t len
, u32 addr
, bool complete
)
470 u8
*buf
= kmalloc(4096, GFP_KERNEL
);
476 transfer
= min_t(int, len
, 4096);
477 memcpy(buf
, data
, transfer
);
479 err
= usb_control_msg(aru
->udev
, usb_sndctrlpipe(aru
->udev
, 0),
480 0x30 /* FW DL */, 0x40 | USB_DIR_OUT
,
481 addr
>> 8, 0, buf
, transfer
, 1000);
495 err
= usb_control_msg(aru
->udev
, usb_sndctrlpipe(aru
->udev
, 0),
496 0x31 /* FW DL COMPLETE */,
497 0x40 | USB_DIR_OUT
, 0, 0, NULL
, 0, 5000);
503 static int ar9170_usb_request_firmware(struct ar9170_usb
*aru
)
507 err
= request_firmware(&aru
->init_values
, "ar9170-1.fw",
510 dev_err(&aru
->udev
->dev
, "file with init values not found.\n");
514 err
= request_firmware(&aru
->firmware
, "ar9170-2.fw", &aru
->udev
->dev
);
516 release_firmware(aru
->init_values
);
517 dev_err(&aru
->udev
->dev
, "firmware file not found.\n");
524 static int ar9170_usb_reset(struct ar9170_usb
*aru
)
526 int ret
, lock
= (aru
->intf
->condition
!= USB_INTERFACE_BINDING
);
529 ret
= usb_lock_device_for_reset(aru
->udev
, aru
->intf
);
531 dev_err(&aru
->udev
->dev
, "unable to lock device "
532 "for reset (%d).\n", ret
);
537 ret
= usb_reset_device(aru
->udev
);
539 usb_unlock_device(aru
->udev
);
541 /* let it rest - for a second - */
547 static int ar9170_usb_upload_firmware(struct ar9170_usb
*aru
)
551 /* First, upload initial values to device RAM */
552 err
= ar9170_usb_upload(aru
, aru
->init_values
->data
,
553 aru
->init_values
->size
, 0x102800, false);
555 dev_err(&aru
->udev
->dev
, "firmware part 1 "
556 "upload failed (%d).\n", err
);
560 /* Then, upload the firmware itself and start it */
561 return ar9170_usb_upload(aru
, aru
->firmware
->data
, aru
->firmware
->size
,
565 static int ar9170_usb_init_transport(struct ar9170_usb
*aru
)
567 struct ar9170
*ar
= (void *) &aru
->common
;
570 ar9170_regwrite_begin(ar
);
572 /* Set USB Rx stream mode MAX packet number to 2 */
573 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD
, 0x4);
575 /* Set USB Rx stream mode timeout to 10us */
576 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL
, 0x80);
578 ar9170_regwrite_finish();
580 err
= ar9170_regwrite_result();
582 dev_err(&aru
->udev
->dev
, "USB setup failed (%d).\n", err
);
587 static void ar9170_usb_stop(struct ar9170
*ar
)
589 struct ar9170_usb
*aru
= (void *) ar
;
592 if (IS_ACCEPTING_CMD(ar
))
593 aru
->common
.state
= AR9170_STOPPED
;
595 /* lets wait a while until the tx - queues are dried out */
596 ret
= usb_wait_anchor_empty_timeout(&aru
->tx_submitted
,
597 msecs_to_jiffies(1000));
599 dev_err(&aru
->udev
->dev
, "kill pending tx urbs.\n");
601 usb_poison_anchored_urbs(&aru
->tx_submitted
);
605 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
606 * Else we would end up with a unresponsive device...
610 static int ar9170_usb_open(struct ar9170
*ar
)
612 struct ar9170_usb
*aru
= (void *) ar
;
615 usb_unpoison_anchored_urbs(&aru
->tx_submitted
);
616 err
= ar9170_usb_init_transport(aru
);
618 usb_poison_anchored_urbs(&aru
->tx_submitted
);
622 aru
->common
.state
= AR9170_IDLE
;
626 static int ar9170_usb_init_device(struct ar9170_usb
*aru
)
630 err
= ar9170_usb_alloc_rx_irq_urb(aru
);
634 err
= ar9170_usb_alloc_rx_bulk_urbs(aru
);
638 err
= ar9170_usb_upload_firmware(aru
);
640 err
= ar9170_echo_test(&aru
->common
, 0x60d43110);
642 /* force user invention, by disabling the device */
643 err
= usb_driver_set_configuration(aru
->udev
, -1);
644 dev_err(&aru
->udev
->dev
, "device is in a bad state. "
645 "please reconnect it!\n");
653 ar9170_usb_cancel_urbs(aru
);
659 static int ar9170_usb_probe(struct usb_interface
*intf
,
660 const struct usb_device_id
*id
)
662 struct ar9170_usb
*aru
;
664 struct usb_device
*udev
;
667 aru
= ar9170_alloc(sizeof(*aru
));
673 udev
= interface_to_usbdev(intf
);
679 usb_set_intfdata(intf
, aru
);
680 SET_IEEE80211_DEV(ar
->hw
, &udev
->dev
);
682 init_usb_anchor(&aru
->rx_submitted
);
683 init_usb_anchor(&aru
->tx_submitted
);
684 init_completion(&aru
->cmd_wait
);
686 aru
->common
.stop
= ar9170_usb_stop
;
687 aru
->common
.open
= ar9170_usb_open
;
688 aru
->common
.tx
= ar9170_usb_tx
;
689 aru
->common
.exec_cmd
= ar9170_usb_exec_cmd
;
690 aru
->common
.callback_cmd
= ar9170_usb_callback_cmd
;
692 err
= ar9170_usb_reset(aru
);
696 err
= ar9170_usb_request_firmware(aru
);
700 err
= ar9170_usb_init_device(aru
);
704 err
= ar9170_usb_open(ar
);
708 err
= ar9170_register(ar
, &udev
->dev
);
717 ar9170_usb_cancel_urbs(aru
);
720 release_firmware(aru
->init_values
);
721 release_firmware(aru
->firmware
);
724 usb_set_intfdata(intf
, NULL
);
726 ieee80211_free_hw(ar
->hw
);
731 static void ar9170_usb_disconnect(struct usb_interface
*intf
)
733 struct ar9170_usb
*aru
= usb_get_intfdata(intf
);
738 aru
->common
.state
= AR9170_IDLE
;
739 ar9170_unregister(&aru
->common
);
740 ar9170_usb_cancel_urbs(aru
);
742 release_firmware(aru
->init_values
);
743 release_firmware(aru
->firmware
);
745 usb_put_dev(aru
->udev
);
746 usb_set_intfdata(intf
, NULL
);
747 ieee80211_free_hw(aru
->common
.hw
);
751 static int ar9170_suspend(struct usb_interface
*intf
,
752 pm_message_t message
)
754 struct ar9170_usb
*aru
= usb_get_intfdata(intf
);
759 aru
->common
.state
= AR9170_IDLE
;
760 ar9170_usb_cancel_urbs(aru
);
765 static int ar9170_resume(struct usb_interface
*intf
)
767 struct ar9170_usb
*aru
= usb_get_intfdata(intf
);
773 usb_unpoison_anchored_urbs(&aru
->rx_submitted
);
774 usb_unpoison_anchored_urbs(&aru
->tx_submitted
);
777 * FIXME: firmware upload will fail on resume.
778 * but this is better than a hang!
781 err
= ar9170_usb_init_device(aru
);
785 err
= ar9170_usb_open(&aru
->common
);
792 aru
->common
.state
= AR9170_IDLE
;
793 ar9170_usb_cancel_urbs(aru
);
797 #endif /* CONFIG_PM */
799 static struct usb_driver ar9170_driver
= {
801 .probe
= ar9170_usb_probe
,
802 .disconnect
= ar9170_usb_disconnect
,
803 .id_table
= ar9170_usb_ids
,
806 .suspend
= ar9170_suspend
,
807 .resume
= ar9170_resume
,
808 #endif /* CONFIG_PM */
811 static int __init
ar9170_init(void)
813 return usb_register(&ar9170_driver
);
816 static void __exit
ar9170_exit(void)
818 usb_deregister(&ar9170_driver
);
821 module_init(ar9170_init
);
822 module_exit(ar9170_exit
);