ar9170: wait for asynchronous firmware loading
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / ath / ar9170 / usb.c
blob24dc555f5ff1bcf8059b2c3dc360dbd5856b77fe
1 /*
2 * Atheros AR9170 driver
4 * USB - frontend
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
24 * permission notice:
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>
45 #include "ar9170.h"
46 #include "cmd.h"
47 #include "hw.h"
48 #include "usb.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[] = {
63 /* Atheros 9170 */
64 { USB_DEVICE(0x0cf3, 0x9170) },
65 /* Atheros TG121N */
66 { USB_DEVICE(0x0cf3, 0x1001) },
67 /* TP-Link TL-WN821N v2 */
68 { USB_DEVICE(0x0cf3, 0x1002) },
69 /* Cace Airpcap NX */
70 { USB_DEVICE(0xcace, 0x0300) },
71 /* D-Link DWA 160 A1 */
72 { USB_DEVICE(0x07d1, 0x3c10) },
73 /* D-Link DWA 160 A2 */
74 { USB_DEVICE(0x07d1, 0x3a09) },
75 /* Netgear WNDA3100 */
76 { USB_DEVICE(0x0846, 0x9010) },
77 /* Netgear WN111 v2 */
78 { USB_DEVICE(0x0846, 0x9001) },
79 /* Zydas ZD1221 */
80 { USB_DEVICE(0x0ace, 0x1221) },
81 /* ZyXEL NWD271N */
82 { USB_DEVICE(0x0586, 0x3417) },
83 /* Z-Com UB81 BG */
84 { USB_DEVICE(0x0cde, 0x0023) },
85 /* Z-Com UB82 ABG */
86 { USB_DEVICE(0x0cde, 0x0026) },
87 /* Sphairon Homelink 1202 */
88 { USB_DEVICE(0x0cde, 0x0027) },
89 /* Arcadyan WN7512 */
90 { USB_DEVICE(0x083a, 0xf522) },
91 /* Planex GWUS300 */
92 { USB_DEVICE(0x2019, 0x5304) },
93 /* IO-Data WNGDNUS2 */
94 { USB_DEVICE(0x04bb, 0x093f) },
95 /* AVM FRITZ!WLAN USB Stick N */
96 { USB_DEVICE(0x057C, 0x8401) },
97 /* NEC WL300NU-G */
98 { USB_DEVICE(0x0409, 0x0249) },
99 /* AVM FRITZ!WLAN USB Stick N 2.4 */
100 { USB_DEVICE(0x057C, 0x8402), .driver_info = AR9170_REQ_FW1_ONLY },
102 /* terminate */
105 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
107 static void ar9170_usb_submit_urb(struct ar9170_usb *aru)
109 struct urb *urb;
110 unsigned long flags;
111 int err;
113 if (unlikely(!IS_STARTED(&aru->common)))
114 return ;
116 spin_lock_irqsave(&aru->tx_urb_lock, flags);
117 if (atomic_read(&aru->tx_submitted_urbs) >= AR9170_NUM_TX_URBS) {
118 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
119 return ;
121 atomic_inc(&aru->tx_submitted_urbs);
123 urb = usb_get_from_anchor(&aru->tx_pending);
124 if (!urb) {
125 atomic_dec(&aru->tx_submitted_urbs);
126 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
128 return ;
130 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
132 aru->tx_pending_urbs--;
133 usb_anchor_urb(urb, &aru->tx_submitted);
135 err = usb_submit_urb(urb, GFP_ATOMIC);
136 if (unlikely(err)) {
137 if (ar9170_nag_limiter(&aru->common))
138 dev_err(&aru->udev->dev, "submit_urb failed (%d).\n",
139 err);
141 usb_unanchor_urb(urb);
142 atomic_dec(&aru->tx_submitted_urbs);
143 ar9170_tx_callback(&aru->common, urb->context);
146 usb_free_urb(urb);
149 static void ar9170_usb_tx_urb_complete_frame(struct urb *urb)
151 struct sk_buff *skb = urb->context;
152 struct ar9170_usb *aru = (struct ar9170_usb *)
153 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
155 if (unlikely(!aru)) {
156 dev_kfree_skb_irq(skb);
157 return ;
160 atomic_dec(&aru->tx_submitted_urbs);
162 ar9170_tx_callback(&aru->common, skb);
164 ar9170_usb_submit_urb(aru);
167 static void ar9170_usb_tx_urb_complete(struct urb *urb)
171 static void ar9170_usb_irq_completed(struct urb *urb)
173 struct ar9170_usb *aru = urb->context;
175 switch (urb->status) {
176 /* everything is fine */
177 case 0:
178 break;
180 /* disconnect */
181 case -ENOENT:
182 case -ECONNRESET:
183 case -ENODEV:
184 case -ESHUTDOWN:
185 goto free;
187 default:
188 goto resubmit;
191 ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
192 urb->actual_length);
194 resubmit:
195 usb_anchor_urb(urb, &aru->rx_submitted);
196 if (usb_submit_urb(urb, GFP_ATOMIC)) {
197 usb_unanchor_urb(urb);
198 goto free;
201 return;
203 free:
204 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
207 static void ar9170_usb_rx_completed(struct urb *urb)
209 struct sk_buff *skb = urb->context;
210 struct ar9170_usb *aru = (struct ar9170_usb *)
211 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
212 int err;
214 if (!aru)
215 goto free;
217 switch (urb->status) {
218 /* everything is fine */
219 case 0:
220 break;
222 /* disconnect */
223 case -ENOENT:
224 case -ECONNRESET:
225 case -ENODEV:
226 case -ESHUTDOWN:
227 goto free;
229 default:
230 goto resubmit;
233 skb_put(skb, urb->actual_length);
234 ar9170_rx(&aru->common, skb);
236 resubmit:
237 skb_reset_tail_pointer(skb);
238 skb_trim(skb, 0);
240 usb_anchor_urb(urb, &aru->rx_submitted);
241 err = usb_submit_urb(urb, GFP_ATOMIC);
242 if (unlikely(err)) {
243 usb_unanchor_urb(urb);
244 goto free;
247 return ;
249 free:
250 dev_kfree_skb_irq(skb);
253 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
254 struct urb *urb, gfp_t gfp)
256 struct sk_buff *skb;
258 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
259 if (!skb)
260 return -ENOMEM;
262 /* reserve some space for mac80211's radiotap */
263 skb_reserve(skb, 32);
265 usb_fill_bulk_urb(urb, aru->udev,
266 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
267 skb->data, min(skb_tailroom(skb),
268 AR9170_MAX_RX_BUFFER_SIZE),
269 ar9170_usb_rx_completed, skb);
271 return 0;
274 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
276 struct urb *urb = NULL;
277 void *ibuf;
278 int err = -ENOMEM;
280 /* initialize interrupt endpoint */
281 urb = usb_alloc_urb(0, GFP_KERNEL);
282 if (!urb)
283 goto out;
285 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
286 if (!ibuf)
287 goto out;
289 usb_fill_int_urb(urb, aru->udev,
290 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
291 64, ar9170_usb_irq_completed, aru, 1);
292 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
294 usb_anchor_urb(urb, &aru->rx_submitted);
295 err = usb_submit_urb(urb, GFP_KERNEL);
296 if (err) {
297 usb_unanchor_urb(urb);
298 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
299 urb->transfer_dma);
302 out:
303 usb_free_urb(urb);
304 return err;
307 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
309 struct urb *urb;
310 int i;
311 int err = -EINVAL;
313 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
314 err = -ENOMEM;
315 urb = usb_alloc_urb(0, GFP_KERNEL);
316 if (!urb)
317 goto err_out;
319 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
320 if (err) {
321 usb_free_urb(urb);
322 goto err_out;
325 usb_anchor_urb(urb, &aru->rx_submitted);
326 err = usb_submit_urb(urb, GFP_KERNEL);
327 if (err) {
328 usb_unanchor_urb(urb);
329 dev_kfree_skb_any((void *) urb->transfer_buffer);
330 usb_free_urb(urb);
331 goto err_out;
333 usb_free_urb(urb);
336 /* the device now waiting for a firmware. */
337 aru->common.state = AR9170_IDLE;
338 return 0;
340 err_out:
342 usb_kill_anchored_urbs(&aru->rx_submitted);
343 return err;
346 static int ar9170_usb_flush(struct ar9170 *ar)
348 struct ar9170_usb *aru = (void *) ar;
349 struct urb *urb;
350 int ret, err = 0;
352 if (IS_STARTED(ar))
353 aru->common.state = AR9170_IDLE;
355 usb_wait_anchor_empty_timeout(&aru->tx_pending,
356 msecs_to_jiffies(800));
357 while ((urb = usb_get_from_anchor(&aru->tx_pending))) {
358 ar9170_tx_callback(&aru->common, (void *) urb->context);
359 usb_free_urb(urb);
362 /* lets wait a while until the tx - queues are dried out */
363 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
364 msecs_to_jiffies(100));
365 if (ret == 0)
366 err = -ETIMEDOUT;
368 usb_kill_anchored_urbs(&aru->tx_submitted);
370 if (IS_ACCEPTING_CMD(ar))
371 aru->common.state = AR9170_STARTED;
373 return err;
376 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
378 int err;
380 aru->common.state = AR9170_UNKNOWN_STATE;
382 err = ar9170_usb_flush(&aru->common);
383 if (err)
384 dev_err(&aru->udev->dev, "stuck tx urbs!\n");
386 usb_poison_anchored_urbs(&aru->tx_submitted);
387 usb_poison_anchored_urbs(&aru->rx_submitted);
390 static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
391 unsigned int plen, void *payload,
392 unsigned int outlen, void *out)
394 struct ar9170_usb *aru = (void *) ar;
395 struct urb *urb = NULL;
396 unsigned long flags;
397 int err = -ENOMEM;
399 if (unlikely(!IS_ACCEPTING_CMD(ar)))
400 return -EPERM;
402 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
403 return -EINVAL;
405 urb = usb_alloc_urb(0, GFP_ATOMIC);
406 if (unlikely(!urb))
407 goto err_free;
409 ar->cmdbuf[0] = cpu_to_le32(plen);
410 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
411 /* writing multiple regs fills this buffer already */
412 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
413 memcpy(&ar->cmdbuf[1], payload, plen);
415 spin_lock_irqsave(&aru->common.cmdlock, flags);
416 aru->readbuf = (u8 *)out;
417 aru->readlen = outlen;
418 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
420 usb_fill_int_urb(urb, aru->udev,
421 usb_sndintpipe(aru->udev, AR9170_EP_CMD),
422 aru->common.cmdbuf, plen + 4,
423 ar9170_usb_tx_urb_complete, NULL, 1);
425 usb_anchor_urb(urb, &aru->tx_submitted);
426 err = usb_submit_urb(urb, GFP_ATOMIC);
427 if (unlikely(err)) {
428 usb_unanchor_urb(urb);
429 usb_free_urb(urb);
430 goto err_unbuf;
432 usb_free_urb(urb);
434 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
435 if (err == 0) {
436 err = -ETIMEDOUT;
437 goto err_unbuf;
440 if (aru->readlen != outlen) {
441 err = -EMSGSIZE;
442 goto err_unbuf;
445 return 0;
447 err_unbuf:
448 /* Maybe the device was removed in the second we were waiting? */
449 if (IS_STARTED(ar)) {
450 dev_err(&aru->udev->dev, "no command feedback "
451 "received (%d).\n", err);
453 /* provide some maybe useful debug information */
454 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
455 aru->common.cmdbuf, plen + 4);
456 dump_stack();
459 /* invalidate to avoid completing the next prematurely */
460 spin_lock_irqsave(&aru->common.cmdlock, flags);
461 aru->readbuf = NULL;
462 aru->readlen = 0;
463 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
465 err_free:
467 return err;
470 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
472 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
473 struct urb *urb;
475 if (unlikely(!IS_STARTED(ar))) {
476 /* Seriously, what were you drink... err... thinking!? */
477 return -EPERM;
480 urb = usb_alloc_urb(0, GFP_ATOMIC);
481 if (unlikely(!urb))
482 return -ENOMEM;
484 usb_fill_bulk_urb(urb, aru->udev,
485 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
486 skb->data, skb->len,
487 ar9170_usb_tx_urb_complete_frame, skb);
488 urb->transfer_flags |= URB_ZERO_PACKET;
490 usb_anchor_urb(urb, &aru->tx_pending);
491 aru->tx_pending_urbs++;
493 usb_free_urb(urb);
495 ar9170_usb_submit_urb(aru);
496 return 0;
499 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
501 struct ar9170_usb *aru = (void *) ar;
502 unsigned long flags;
503 u32 in, out;
505 if (unlikely(!buffer))
506 return ;
508 in = le32_to_cpup((__le32 *)buffer);
509 out = le32_to_cpu(ar->cmdbuf[0]);
511 /* mask off length byte */
512 out &= ~0xFF;
514 if (aru->readlen >= 0) {
515 /* add expected length */
516 out |= aru->readlen;
517 } else {
518 /* add obtained length */
519 out |= in & 0xFF;
523 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
524 * length and we cannot predict the correct length in advance.
525 * So we only check if we provided enough space for the data.
527 if (unlikely(out < in)) {
528 dev_warn(&aru->udev->dev, "received invalid command response "
529 "got %d bytes, instead of %d bytes "
530 "and the resp length is %d bytes\n",
531 in, out, len);
532 print_hex_dump_bytes("ar9170 invalid resp: ",
533 DUMP_PREFIX_OFFSET, buffer, len);
535 * Do not complete, then the command times out,
536 * and we get a stack trace from there.
538 return ;
541 spin_lock_irqsave(&aru->common.cmdlock, flags);
542 if (aru->readbuf && len > 0) {
543 memcpy(aru->readbuf, buffer + 4, len - 4);
544 aru->readbuf = NULL;
546 complete(&aru->cmd_wait);
547 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
550 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
551 size_t len, u32 addr, bool complete)
553 int transfer, err;
554 u8 *buf = kmalloc(4096, GFP_KERNEL);
556 if (!buf)
557 return -ENOMEM;
559 while (len) {
560 transfer = min_t(int, len, 4096);
561 memcpy(buf, data, transfer);
563 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
564 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
565 addr >> 8, 0, buf, transfer, 1000);
567 if (err < 0) {
568 kfree(buf);
569 return err;
572 len -= transfer;
573 data += transfer;
574 addr += transfer;
576 kfree(buf);
578 if (complete) {
579 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
580 0x31 /* FW DL COMPLETE */,
581 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
584 return 0;
587 static int ar9170_usb_reset(struct ar9170_usb *aru)
589 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
591 if (lock) {
592 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
593 if (ret < 0) {
594 dev_err(&aru->udev->dev, "unable to lock device "
595 "for reset (%d).\n", ret);
596 return ret;
600 ret = usb_reset_device(aru->udev);
601 if (lock)
602 usb_unlock_device(aru->udev);
604 /* let it rest - for a second - */
605 msleep(1000);
607 return ret;
610 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
612 int err;
614 if (!aru->init_values)
615 goto upload_fw_start;
617 /* First, upload initial values to device RAM */
618 err = ar9170_usb_upload(aru, aru->init_values->data,
619 aru->init_values->size, 0x102800, false);
620 if (err) {
621 dev_err(&aru->udev->dev, "firmware part 1 "
622 "upload failed (%d).\n", err);
623 return err;
626 upload_fw_start:
628 /* Then, upload the firmware itself and start it */
629 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
630 0x200000, true);
633 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
635 struct ar9170 *ar = (void *) &aru->common;
636 int err;
638 ar9170_regwrite_begin(ar);
640 /* Set USB Rx stream mode MAX packet number to 2 */
641 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
643 /* Set USB Rx stream mode timeout to 10us */
644 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
646 ar9170_regwrite_finish();
648 err = ar9170_regwrite_result();
649 if (err)
650 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
652 return err;
655 static void ar9170_usb_stop(struct ar9170 *ar)
657 struct ar9170_usb *aru = (void *) ar;
658 int ret;
660 if (IS_ACCEPTING_CMD(ar))
661 aru->common.state = AR9170_STOPPED;
663 ret = ar9170_usb_flush(ar);
664 if (ret)
665 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
667 usb_poison_anchored_urbs(&aru->tx_submitted);
670 * Note:
671 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
672 * Else we would end up with a unresponsive device...
676 static int ar9170_usb_open(struct ar9170 *ar)
678 struct ar9170_usb *aru = (void *) ar;
679 int err;
681 usb_unpoison_anchored_urbs(&aru->tx_submitted);
682 err = ar9170_usb_init_transport(aru);
683 if (err) {
684 usb_poison_anchored_urbs(&aru->tx_submitted);
685 return err;
688 aru->common.state = AR9170_IDLE;
689 return 0;
692 static int ar9170_usb_init_device(struct ar9170_usb *aru)
694 int err;
696 err = ar9170_usb_alloc_rx_irq_urb(aru);
697 if (err)
698 goto err_out;
700 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
701 if (err)
702 goto err_unrx;
704 err = ar9170_usb_upload_firmware(aru);
705 if (err) {
706 err = ar9170_echo_test(&aru->common, 0x60d43110);
707 if (err) {
708 /* force user invention, by disabling the device */
709 err = usb_driver_set_configuration(aru->udev, -1);
710 dev_err(&aru->udev->dev, "device is in a bad state. "
711 "please reconnect it!\n");
712 goto err_unrx;
716 return 0;
718 err_unrx:
719 ar9170_usb_cancel_urbs(aru);
721 err_out:
722 return err;
725 static void ar9170_usb_firmware_failed(struct ar9170_usb *aru)
727 struct device *parent = aru->udev->dev.parent;
729 complete(&aru->firmware_loading_complete);
731 /* unbind anything failed */
732 if (parent)
733 down(&parent->sem);
734 device_release_driver(&aru->udev->dev);
735 if (parent)
736 up(&parent->sem);
738 usb_put_dev(aru->udev);
741 static void ar9170_usb_firmware_finish(const struct firmware *fw, void *context)
743 struct ar9170_usb *aru = context;
744 int err;
746 aru->firmware = fw;
748 if (!fw) {
749 dev_err(&aru->udev->dev, "firmware file not found.\n");
750 goto err_freefw;
753 err = ar9170_usb_init_device(aru);
754 if (err)
755 goto err_freefw;
757 err = ar9170_usb_open(&aru->common);
758 if (err)
759 goto err_unrx;
761 err = ar9170_register(&aru->common, &aru->udev->dev);
763 ar9170_usb_stop(&aru->common);
764 if (err)
765 goto err_unrx;
767 complete(&aru->firmware_loading_complete);
768 usb_put_dev(aru->udev);
769 return;
771 err_unrx:
772 ar9170_usb_cancel_urbs(aru);
774 err_freefw:
775 ar9170_usb_firmware_failed(aru);
778 static void ar9170_usb_firmware_inits(const struct firmware *fw,
779 void *context)
781 struct ar9170_usb *aru = context;
782 int err;
784 if (!fw) {
785 dev_err(&aru->udev->dev, "file with init values not found.\n");
786 ar9170_usb_firmware_failed(aru);
787 return;
790 aru->init_values = fw;
792 /* ok so we have the init values -- get code for two-stage */
794 err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-2.fw",
795 &aru->udev->dev, GFP_KERNEL, aru,
796 ar9170_usb_firmware_finish);
797 if (err)
798 ar9170_usb_firmware_failed(aru);
801 static void ar9170_usb_firmware_step2(const struct firmware *fw, void *context)
803 struct ar9170_usb *aru = context;
804 int err;
806 if (fw) {
807 ar9170_usb_firmware_finish(fw, context);
808 return;
811 if (aru->req_one_stage_fw) {
812 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
813 "not found and is required for this device\n");
814 ar9170_usb_firmware_failed(aru);
815 return;
818 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
819 "not found, trying old firmware...\n");
821 err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-1.fw",
822 &aru->udev->dev, GFP_KERNEL, aru,
823 ar9170_usb_firmware_inits);
824 if (err)
825 ar9170_usb_firmware_failed(aru);
828 static bool ar9170_requires_one_stage(const struct usb_device_id *id)
830 if (!id->driver_info)
831 return false;
832 if (id->driver_info == AR9170_REQ_FW1_ONLY)
833 return true;
834 return false;
837 static int ar9170_usb_probe(struct usb_interface *intf,
838 const struct usb_device_id *id)
840 struct ar9170_usb *aru;
841 struct ar9170 *ar;
842 struct usb_device *udev;
843 int err;
845 aru = ar9170_alloc(sizeof(*aru));
846 if (IS_ERR(aru)) {
847 err = PTR_ERR(aru);
848 goto out;
851 udev = interface_to_usbdev(intf);
852 usb_get_dev(udev);
853 aru->udev = udev;
854 aru->intf = intf;
855 ar = &aru->common;
857 aru->req_one_stage_fw = ar9170_requires_one_stage(id);
859 usb_set_intfdata(intf, aru);
860 SET_IEEE80211_DEV(ar->hw, &intf->dev);
862 init_usb_anchor(&aru->rx_submitted);
863 init_usb_anchor(&aru->tx_pending);
864 init_usb_anchor(&aru->tx_submitted);
865 init_completion(&aru->cmd_wait);
866 init_completion(&aru->firmware_loading_complete);
867 spin_lock_init(&aru->tx_urb_lock);
869 aru->tx_pending_urbs = 0;
870 atomic_set(&aru->tx_submitted_urbs, 0);
872 aru->common.stop = ar9170_usb_stop;
873 aru->common.flush = ar9170_usb_flush;
874 aru->common.open = ar9170_usb_open;
875 aru->common.tx = ar9170_usb_tx;
876 aru->common.exec_cmd = ar9170_usb_exec_cmd;
877 aru->common.callback_cmd = ar9170_usb_callback_cmd;
879 #ifdef CONFIG_PM
880 udev->reset_resume = 1;
881 #endif /* CONFIG_PM */
882 err = ar9170_usb_reset(aru);
883 if (err)
884 goto err_freehw;
886 usb_get_dev(aru->udev);
887 return request_firmware_nowait(THIS_MODULE, 1, "ar9170.fw",
888 &aru->udev->dev, GFP_KERNEL, aru,
889 ar9170_usb_firmware_step2);
890 err_freehw:
891 usb_set_intfdata(intf, NULL);
892 usb_put_dev(udev);
893 ieee80211_free_hw(ar->hw);
894 out:
895 return err;
898 static void ar9170_usb_disconnect(struct usb_interface *intf)
900 struct ar9170_usb *aru = usb_get_intfdata(intf);
902 if (!aru)
903 return;
905 aru->common.state = AR9170_IDLE;
907 wait_for_completion(&aru->firmware_loading_complete);
909 ar9170_unregister(&aru->common);
910 ar9170_usb_cancel_urbs(aru);
912 usb_put_dev(aru->udev);
913 usb_set_intfdata(intf, NULL);
914 ieee80211_free_hw(aru->common.hw);
916 release_firmware(aru->init_values);
917 release_firmware(aru->firmware);
920 #ifdef CONFIG_PM
921 static int ar9170_suspend(struct usb_interface *intf,
922 pm_message_t message)
924 struct ar9170_usb *aru = usb_get_intfdata(intf);
926 if (!aru)
927 return -ENODEV;
929 aru->common.state = AR9170_IDLE;
930 ar9170_usb_cancel_urbs(aru);
932 return 0;
935 static int ar9170_resume(struct usb_interface *intf)
937 struct ar9170_usb *aru = usb_get_intfdata(intf);
938 int err;
940 if (!aru)
941 return -ENODEV;
943 usb_unpoison_anchored_urbs(&aru->rx_submitted);
944 usb_unpoison_anchored_urbs(&aru->tx_submitted);
946 err = ar9170_usb_init_device(aru);
947 if (err)
948 goto err_unrx;
950 err = ar9170_usb_open(&aru->common);
951 if (err)
952 goto err_unrx;
954 return 0;
956 err_unrx:
957 aru->common.state = AR9170_IDLE;
958 ar9170_usb_cancel_urbs(aru);
960 return err;
962 #endif /* CONFIG_PM */
964 static struct usb_driver ar9170_driver = {
965 .name = "ar9170usb",
966 .probe = ar9170_usb_probe,
967 .disconnect = ar9170_usb_disconnect,
968 .id_table = ar9170_usb_ids,
969 .soft_unbind = 1,
970 #ifdef CONFIG_PM
971 .suspend = ar9170_suspend,
972 .resume = ar9170_resume,
973 .reset_resume = ar9170_resume,
974 #endif /* CONFIG_PM */
977 static int __init ar9170_init(void)
979 return usb_register(&ar9170_driver);
982 static void __exit ar9170_exit(void)
984 usb_deregister(&ar9170_driver);
987 module_init(ar9170_init);
988 module_exit(ar9170_exit);