ar9170usb: fix spurious firmware related message
[linux/fpc-iii.git] / drivers / net / wireless / ath / ar9170 / usb.c
blob007eb85fc67e27a4f6b11b9cf94129f142bf3b81
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 /* Cace Airpcap NX */
68 { USB_DEVICE(0xcace, 0x0300) },
69 /* D-Link DWA 160A */
70 { USB_DEVICE(0x07d1, 0x3c10) },
71 /* Netgear WNDA3100 */
72 { USB_DEVICE(0x0846, 0x9010) },
73 /* Netgear WN111 v2 */
74 { USB_DEVICE(0x0846, 0x9001) },
75 /* Zydas ZD1221 */
76 { USB_DEVICE(0x0ace, 0x1221) },
77 /* ZyXEL NWD271N */
78 { USB_DEVICE(0x0586, 0x3417) },
79 /* Z-Com UB81 BG */
80 { USB_DEVICE(0x0cde, 0x0023) },
81 /* Z-Com UB82 ABG */
82 { USB_DEVICE(0x0cde, 0x0026) },
83 /* Arcadyan WN7512 */
84 { USB_DEVICE(0x083a, 0xf522) },
85 /* Planex GWUS300 */
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 },
94 /* terminate */
97 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
99 static void ar9170_usb_submit_urb(struct ar9170_usb *aru)
101 struct urb *urb;
102 unsigned long flags;
103 int err;
105 if (unlikely(!IS_STARTED(&aru->common)))
106 return ;
108 spin_lock_irqsave(&aru->tx_urb_lock, flags);
109 if (aru->tx_submitted_urbs >= AR9170_NUM_TX_URBS) {
110 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
111 return ;
113 aru->tx_submitted_urbs++;
115 urb = usb_get_from_anchor(&aru->tx_pending);
116 if (!urb) {
117 aru->tx_submitted_urbs--;
118 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
120 return ;
122 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
124 aru->tx_pending_urbs--;
125 usb_anchor_urb(urb, &aru->tx_submitted);
127 err = usb_submit_urb(urb, GFP_ATOMIC);
128 if (unlikely(err)) {
129 if (ar9170_nag_limiter(&aru->common))
130 dev_err(&aru->udev->dev, "submit_urb failed (%d).\n",
131 err);
133 usb_unanchor_urb(urb);
134 aru->tx_submitted_urbs--;
135 ar9170_tx_callback(&aru->common, urb->context);
138 usb_free_urb(urb);
141 static void ar9170_usb_tx_urb_complete_frame(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));
147 if (unlikely(!aru)) {
148 dev_kfree_skb_irq(skb);
149 return ;
152 aru->tx_submitted_urbs--;
154 ar9170_tx_callback(&aru->common, skb);
156 ar9170_usb_submit_urb(aru);
159 static void ar9170_usb_tx_urb_complete(struct urb *urb)
163 static void ar9170_usb_irq_completed(struct urb *urb)
165 struct ar9170_usb *aru = urb->context;
167 switch (urb->status) {
168 /* everything is fine */
169 case 0:
170 break;
172 /* disconnect */
173 case -ENOENT:
174 case -ECONNRESET:
175 case -ENODEV:
176 case -ESHUTDOWN:
177 goto free;
179 default:
180 goto resubmit;
183 ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
184 urb->actual_length);
186 resubmit:
187 usb_anchor_urb(urb, &aru->rx_submitted);
188 if (usb_submit_urb(urb, GFP_ATOMIC)) {
189 usb_unanchor_urb(urb);
190 goto free;
193 return;
195 free:
196 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
199 static void ar9170_usb_rx_completed(struct urb *urb)
201 struct sk_buff *skb = urb->context;
202 struct ar9170_usb *aru = (struct ar9170_usb *)
203 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
204 int err;
206 if (!aru)
207 goto free;
209 switch (urb->status) {
210 /* everything is fine */
211 case 0:
212 break;
214 /* disconnect */
215 case -ENOENT:
216 case -ECONNRESET:
217 case -ENODEV:
218 case -ESHUTDOWN:
219 goto free;
221 default:
222 goto resubmit;
225 skb_put(skb, urb->actual_length);
226 ar9170_rx(&aru->common, skb);
228 resubmit:
229 skb_reset_tail_pointer(skb);
230 skb_trim(skb, 0);
232 usb_anchor_urb(urb, &aru->rx_submitted);
233 err = usb_submit_urb(urb, GFP_ATOMIC);
234 if (unlikely(err)) {
235 usb_unanchor_urb(urb);
236 goto free;
239 return ;
241 free:
242 dev_kfree_skb_irq(skb);
245 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
246 struct urb *urb, gfp_t gfp)
248 struct sk_buff *skb;
250 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
251 if (!skb)
252 return -ENOMEM;
254 /* reserve some space for mac80211's radiotap */
255 skb_reserve(skb, 32);
257 usb_fill_bulk_urb(urb, aru->udev,
258 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
259 skb->data, min(skb_tailroom(skb),
260 AR9170_MAX_RX_BUFFER_SIZE),
261 ar9170_usb_rx_completed, skb);
263 return 0;
266 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
268 struct urb *urb = NULL;
269 void *ibuf;
270 int err = -ENOMEM;
272 /* initialize interrupt endpoint */
273 urb = usb_alloc_urb(0, GFP_KERNEL);
274 if (!urb)
275 goto out;
277 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
278 if (!ibuf)
279 goto out;
281 usb_fill_int_urb(urb, aru->udev,
282 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
283 64, ar9170_usb_irq_completed, aru, 1);
284 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
286 usb_anchor_urb(urb, &aru->rx_submitted);
287 err = usb_submit_urb(urb, GFP_KERNEL);
288 if (err) {
289 usb_unanchor_urb(urb);
290 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
291 urb->transfer_dma);
294 out:
295 usb_free_urb(urb);
296 return err;
299 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
301 struct urb *urb;
302 int i;
303 int err = -EINVAL;
305 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
306 err = -ENOMEM;
307 urb = usb_alloc_urb(0, GFP_KERNEL);
308 if (!urb)
309 goto err_out;
311 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
312 if (err) {
313 usb_free_urb(urb);
314 goto err_out;
317 usb_anchor_urb(urb, &aru->rx_submitted);
318 err = usb_submit_urb(urb, GFP_KERNEL);
319 if (err) {
320 usb_unanchor_urb(urb);
321 dev_kfree_skb_any((void *) urb->transfer_buffer);
322 usb_free_urb(urb);
323 goto err_out;
325 usb_free_urb(urb);
328 /* the device now waiting for a firmware. */
329 aru->common.state = AR9170_IDLE;
330 return 0;
332 err_out:
334 usb_kill_anchored_urbs(&aru->rx_submitted);
335 return err;
338 static int ar9170_usb_flush(struct ar9170 *ar)
340 struct ar9170_usb *aru = (void *) ar;
341 struct urb *urb;
342 int ret, err = 0;
344 if (IS_STARTED(ar))
345 aru->common.state = AR9170_IDLE;
347 usb_wait_anchor_empty_timeout(&aru->tx_pending,
348 msecs_to_jiffies(800));
349 while ((urb = usb_get_from_anchor(&aru->tx_pending))) {
350 ar9170_tx_callback(&aru->common, (void *) urb->context);
351 usb_free_urb(urb);
354 /* lets wait a while until the tx - queues are dried out */
355 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
356 msecs_to_jiffies(100));
357 if (ret == 0)
358 err = -ETIMEDOUT;
360 usb_kill_anchored_urbs(&aru->tx_submitted);
362 if (IS_ACCEPTING_CMD(ar))
363 aru->common.state = AR9170_STARTED;
365 return err;
368 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
370 int err;
372 aru->common.state = AR9170_UNKNOWN_STATE;
374 err = ar9170_usb_flush(&aru->common);
375 if (err)
376 dev_err(&aru->udev->dev, "stuck tx urbs!\n");
378 usb_poison_anchored_urbs(&aru->tx_submitted);
379 usb_poison_anchored_urbs(&aru->rx_submitted);
382 static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
383 unsigned int plen, void *payload,
384 unsigned int outlen, void *out)
386 struct ar9170_usb *aru = (void *) ar;
387 struct urb *urb = NULL;
388 unsigned long flags;
389 int err = -ENOMEM;
391 if (unlikely(!IS_ACCEPTING_CMD(ar)))
392 return -EPERM;
394 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
395 return -EINVAL;
397 urb = usb_alloc_urb(0, GFP_ATOMIC);
398 if (unlikely(!urb))
399 goto err_free;
401 ar->cmdbuf[0] = cpu_to_le32(plen);
402 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
403 /* writing multiple regs fills this buffer already */
404 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
405 memcpy(&ar->cmdbuf[1], payload, plen);
407 spin_lock_irqsave(&aru->common.cmdlock, flags);
408 aru->readbuf = (u8 *)out;
409 aru->readlen = outlen;
410 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
412 usb_fill_int_urb(urb, aru->udev,
413 usb_sndbulkpipe(aru->udev, AR9170_EP_CMD),
414 aru->common.cmdbuf, plen + 4,
415 ar9170_usb_tx_urb_complete, NULL, 1);
417 usb_anchor_urb(urb, &aru->tx_submitted);
418 err = usb_submit_urb(urb, GFP_ATOMIC);
419 if (unlikely(err)) {
420 usb_unanchor_urb(urb);
421 usb_free_urb(urb);
422 goto err_unbuf;
424 usb_free_urb(urb);
426 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
427 if (err == 0) {
428 err = -ETIMEDOUT;
429 goto err_unbuf;
432 if (aru->readlen != outlen) {
433 err = -EMSGSIZE;
434 goto err_unbuf;
437 return 0;
439 err_unbuf:
440 /* Maybe the device was removed in the second we were waiting? */
441 if (IS_STARTED(ar)) {
442 dev_err(&aru->udev->dev, "no command feedback "
443 "received (%d).\n", err);
445 /* provide some maybe useful debug information */
446 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
447 aru->common.cmdbuf, plen + 4);
448 dump_stack();
451 /* invalidate to avoid completing the next prematurely */
452 spin_lock_irqsave(&aru->common.cmdlock, flags);
453 aru->readbuf = NULL;
454 aru->readlen = 0;
455 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
457 err_free:
459 return err;
462 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
464 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
465 struct urb *urb;
467 if (unlikely(!IS_STARTED(ar))) {
468 /* Seriously, what were you drink... err... thinking!? */
469 return -EPERM;
472 urb = usb_alloc_urb(0, GFP_ATOMIC);
473 if (unlikely(!urb))
474 return -ENOMEM;
476 usb_fill_bulk_urb(urb, aru->udev,
477 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
478 skb->data, skb->len,
479 ar9170_usb_tx_urb_complete_frame, skb);
480 urb->transfer_flags |= URB_ZERO_PACKET;
482 usb_anchor_urb(urb, &aru->tx_pending);
483 aru->tx_pending_urbs++;
485 usb_free_urb(urb);
487 ar9170_usb_submit_urb(aru);
488 return 0;
491 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
493 struct ar9170_usb *aru = (void *) ar;
494 unsigned long flags;
495 u32 in, out;
497 if (unlikely(!buffer))
498 return ;
500 in = le32_to_cpup((__le32 *)buffer);
501 out = le32_to_cpu(ar->cmdbuf[0]);
503 /* mask off length byte */
504 out &= ~0xFF;
506 if (aru->readlen >= 0) {
507 /* add expected length */
508 out |= aru->readlen;
509 } else {
510 /* add obtained length */
511 out |= in & 0xFF;
515 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
516 * length and we cannot predict the correct length in advance.
517 * So we only check if we provided enough space for the data.
519 if (unlikely(out < in)) {
520 dev_warn(&aru->udev->dev, "received invalid command response "
521 "got %d bytes, instead of %d bytes "
522 "and the resp length is %d bytes\n",
523 in, out, len);
524 print_hex_dump_bytes("ar9170 invalid resp: ",
525 DUMP_PREFIX_OFFSET, buffer, len);
527 * Do not complete, then the command times out,
528 * and we get a stack trace from there.
530 return ;
533 spin_lock_irqsave(&aru->common.cmdlock, flags);
534 if (aru->readbuf && len > 0) {
535 memcpy(aru->readbuf, buffer + 4, len - 4);
536 aru->readbuf = NULL;
538 complete(&aru->cmd_wait);
539 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
542 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
543 size_t len, u32 addr, bool complete)
545 int transfer, err;
546 u8 *buf = kmalloc(4096, GFP_KERNEL);
548 if (!buf)
549 return -ENOMEM;
551 while (len) {
552 transfer = min_t(int, len, 4096);
553 memcpy(buf, data, transfer);
555 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
556 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
557 addr >> 8, 0, buf, transfer, 1000);
559 if (err < 0) {
560 kfree(buf);
561 return err;
564 len -= transfer;
565 data += transfer;
566 addr += transfer;
568 kfree(buf);
570 if (complete) {
571 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
572 0x31 /* FW DL COMPLETE */,
573 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
576 return 0;
579 static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
581 int err = 0;
583 err = request_firmware(&aru->firmware, "ar9170.fw",
584 &aru->udev->dev);
585 if (!err) {
586 aru->init_values = NULL;
587 return 0;
590 if (aru->req_one_stage_fw) {
591 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
592 "not found and is required for this device\n");
593 return -EINVAL;
596 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
597 "not found, trying old firmware...\n");
599 err = request_firmware(&aru->init_values, "ar9170-1.fw",
600 &aru->udev->dev);
601 if (err) {
602 dev_err(&aru->udev->dev, "file with init values not found.\n");
603 return err;
606 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
607 if (err) {
608 release_firmware(aru->init_values);
609 dev_err(&aru->udev->dev, "firmware file not found.\n");
610 return err;
613 return err;
616 static int ar9170_usb_reset(struct ar9170_usb *aru)
618 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
620 if (lock) {
621 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
622 if (ret < 0) {
623 dev_err(&aru->udev->dev, "unable to lock device "
624 "for reset (%d).\n", ret);
625 return ret;
629 ret = usb_reset_device(aru->udev);
630 if (lock)
631 usb_unlock_device(aru->udev);
633 /* let it rest - for a second - */
634 msleep(1000);
636 return ret;
639 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
641 int err;
643 if (!aru->init_values)
644 goto upload_fw_start;
646 /* First, upload initial values to device RAM */
647 err = ar9170_usb_upload(aru, aru->init_values->data,
648 aru->init_values->size, 0x102800, false);
649 if (err) {
650 dev_err(&aru->udev->dev, "firmware part 1 "
651 "upload failed (%d).\n", err);
652 return err;
655 upload_fw_start:
657 /* Then, upload the firmware itself and start it */
658 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
659 0x200000, true);
662 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
664 struct ar9170 *ar = (void *) &aru->common;
665 int err;
667 ar9170_regwrite_begin(ar);
669 /* Set USB Rx stream mode MAX packet number to 2 */
670 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
672 /* Set USB Rx stream mode timeout to 10us */
673 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
675 ar9170_regwrite_finish();
677 err = ar9170_regwrite_result();
678 if (err)
679 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
681 return err;
684 static void ar9170_usb_stop(struct ar9170 *ar)
686 struct ar9170_usb *aru = (void *) ar;
687 int ret;
689 if (IS_ACCEPTING_CMD(ar))
690 aru->common.state = AR9170_STOPPED;
692 ret = ar9170_usb_flush(ar);
693 if (ret)
694 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
696 usb_poison_anchored_urbs(&aru->tx_submitted);
699 * Note:
700 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
701 * Else we would end up with a unresponsive device...
705 static int ar9170_usb_open(struct ar9170 *ar)
707 struct ar9170_usb *aru = (void *) ar;
708 int err;
710 usb_unpoison_anchored_urbs(&aru->tx_submitted);
711 err = ar9170_usb_init_transport(aru);
712 if (err) {
713 usb_poison_anchored_urbs(&aru->tx_submitted);
714 return err;
717 aru->common.state = AR9170_IDLE;
718 return 0;
721 static int ar9170_usb_init_device(struct ar9170_usb *aru)
723 int err;
725 err = ar9170_usb_alloc_rx_irq_urb(aru);
726 if (err)
727 goto err_out;
729 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
730 if (err)
731 goto err_unrx;
733 err = ar9170_usb_upload_firmware(aru);
734 if (err) {
735 err = ar9170_echo_test(&aru->common, 0x60d43110);
736 if (err) {
737 /* force user invention, by disabling the device */
738 err = usb_driver_set_configuration(aru->udev, -1);
739 dev_err(&aru->udev->dev, "device is in a bad state. "
740 "please reconnect it!\n");
741 goto err_unrx;
745 return 0;
747 err_unrx:
748 ar9170_usb_cancel_urbs(aru);
750 err_out:
751 return err;
754 static bool ar9170_requires_one_stage(const struct usb_device_id *id)
756 if (!id->driver_info)
757 return false;
758 if (id->driver_info == AR9170_REQ_FW1_ONLY)
759 return true;
760 return false;
763 static int ar9170_usb_probe(struct usb_interface *intf,
764 const struct usb_device_id *id)
766 struct ar9170_usb *aru;
767 struct ar9170 *ar;
768 struct usb_device *udev;
769 int err;
771 aru = ar9170_alloc(sizeof(*aru));
772 if (IS_ERR(aru)) {
773 err = PTR_ERR(aru);
774 goto out;
777 udev = interface_to_usbdev(intf);
778 usb_get_dev(udev);
779 aru->udev = udev;
780 aru->intf = intf;
781 ar = &aru->common;
783 aru->req_one_stage_fw = ar9170_requires_one_stage(id);
785 usb_set_intfdata(intf, aru);
786 SET_IEEE80211_DEV(ar->hw, &udev->dev);
788 init_usb_anchor(&aru->rx_submitted);
789 init_usb_anchor(&aru->tx_pending);
790 init_usb_anchor(&aru->tx_submitted);
791 init_completion(&aru->cmd_wait);
792 spin_lock_init(&aru->tx_urb_lock);
794 aru->tx_pending_urbs = 0;
795 aru->tx_submitted_urbs = 0;
797 aru->common.stop = ar9170_usb_stop;
798 aru->common.flush = ar9170_usb_flush;
799 aru->common.open = ar9170_usb_open;
800 aru->common.tx = ar9170_usb_tx;
801 aru->common.exec_cmd = ar9170_usb_exec_cmd;
802 aru->common.callback_cmd = ar9170_usb_callback_cmd;
804 #ifdef CONFIG_PM
805 udev->reset_resume = 1;
806 #endif /* CONFIG_PM */
807 err = ar9170_usb_reset(aru);
808 if (err)
809 goto err_freehw;
811 err = ar9170_usb_request_firmware(aru);
812 if (err)
813 goto err_freehw;
815 err = ar9170_usb_init_device(aru);
816 if (err)
817 goto err_freefw;
819 err = ar9170_usb_open(ar);
820 if (err)
821 goto err_unrx;
823 err = ar9170_register(ar, &udev->dev);
825 ar9170_usb_stop(ar);
826 if (err)
827 goto err_unrx;
829 return 0;
831 err_unrx:
832 ar9170_usb_cancel_urbs(aru);
834 err_freefw:
835 release_firmware(aru->init_values);
836 release_firmware(aru->firmware);
838 err_freehw:
839 usb_set_intfdata(intf, NULL);
840 usb_put_dev(udev);
841 ieee80211_free_hw(ar->hw);
842 out:
843 return err;
846 static void ar9170_usb_disconnect(struct usb_interface *intf)
848 struct ar9170_usb *aru = usb_get_intfdata(intf);
850 if (!aru)
851 return;
853 aru->common.state = AR9170_IDLE;
854 ar9170_unregister(&aru->common);
855 ar9170_usb_cancel_urbs(aru);
857 release_firmware(aru->init_values);
858 release_firmware(aru->firmware);
860 usb_put_dev(aru->udev);
861 usb_set_intfdata(intf, NULL);
862 ieee80211_free_hw(aru->common.hw);
865 #ifdef CONFIG_PM
866 static int ar9170_suspend(struct usb_interface *intf,
867 pm_message_t message)
869 struct ar9170_usb *aru = usb_get_intfdata(intf);
871 if (!aru)
872 return -ENODEV;
874 aru->common.state = AR9170_IDLE;
875 ar9170_usb_cancel_urbs(aru);
877 return 0;
880 static int ar9170_resume(struct usb_interface *intf)
882 struct ar9170_usb *aru = usb_get_intfdata(intf);
883 int err;
885 if (!aru)
886 return -ENODEV;
888 usb_unpoison_anchored_urbs(&aru->rx_submitted);
889 usb_unpoison_anchored_urbs(&aru->tx_submitted);
891 err = ar9170_usb_init_device(aru);
892 if (err)
893 goto err_unrx;
895 err = ar9170_usb_open(&aru->common);
896 if (err)
897 goto err_unrx;
899 return 0;
901 err_unrx:
902 aru->common.state = AR9170_IDLE;
903 ar9170_usb_cancel_urbs(aru);
905 return err;
907 #endif /* CONFIG_PM */
909 static struct usb_driver ar9170_driver = {
910 .name = "ar9170usb",
911 .probe = ar9170_usb_probe,
912 .disconnect = ar9170_usb_disconnect,
913 .id_table = ar9170_usb_ids,
914 .soft_unbind = 1,
915 #ifdef CONFIG_PM
916 .suspend = ar9170_suspend,
917 .resume = ar9170_resume,
918 .reset_resume = ar9170_resume,
919 #endif /* CONFIG_PM */
922 static int __init ar9170_init(void)
924 return usb_register(&ar9170_driver);
927 static void __exit ar9170_exit(void)
929 usb_deregister(&ar9170_driver);
932 module_init(ar9170_init);
933 module_exit(ar9170_exit);