ath9k: Add sanity check for beacon_int in adhoc/mesh case
[linux-block.git] / drivers / net / wireless / ath / ar9170 / usb.c
CommitLineData
b63a2cb3
CL
1/*
2 * Atheros AR9170 driver
3 *
4 * USB - frontend
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, Christian Lamparter <chunkeey@web.de>
8 *
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.
13 *
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.
18 *
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/.
22 *
23 * This file incorporates work covered by the following copyright and
24 * permission notice:
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
26 *
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.
30 *
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.
38 */
39
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"
49
de00c04e 50MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
b63a2cb3
CL
51MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
52MODULE_LICENSE("GPL");
de00c04e 53MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
b63a2cb3
CL
54MODULE_FIRMWARE("ar9170-1.fw");
55MODULE_FIRMWARE("ar9170-2.fw");
56
57static struct usb_device_id ar9170_usb_ids[] = {
58 /* Atheros 9170 */
59 { USB_DEVICE(0x0cf3, 0x9170) },
60 /* Atheros TG121N */
61 { USB_DEVICE(0x0cf3, 0x1001) },
4fc298b8
CL
62 /* Cace Airpcap NX */
63 { USB_DEVICE(0xcace, 0x0300) },
b63a2cb3
CL
64 /* D-Link DWA 160A */
65 { USB_DEVICE(0x07d1, 0x3c10) },
66 /* Netgear WNDA3100 */
67 { USB_DEVICE(0x0846, 0x9010) },
68 /* Netgear WN111 v2 */
69 { USB_DEVICE(0x0846, 0x9001) },
70 /* Zydas ZD1221 */
71 { USB_DEVICE(0x0ace, 0x1221) },
957b0516
CL
72 /* ZyXEL NWD271N */
73 { USB_DEVICE(0x0586, 0x3417) },
b63a2cb3
CL
74 /* Z-Com UB81 BG */
75 { USB_DEVICE(0x0cde, 0x0023) },
76 /* Z-Com UB82 ABG */
77 { USB_DEVICE(0x0cde, 0x0026) },
78 /* Arcadyan WN7512 */
79 { USB_DEVICE(0x083a, 0xf522) },
80 /* Planex GWUS300 */
81 { USB_DEVICE(0x2019, 0x5304) },
82 /* IO-Data WNGDNUS2 */
83 { USB_DEVICE(0x04bb, 0x093f) },
84
85 /* terminate */
86 {}
87};
88MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
89
90static void ar9170_usb_tx_urb_complete_free(struct urb *urb)
91{
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));
95
96 if (!aru) {
97 dev_kfree_skb_irq(skb);
98 return ;
99 }
100
101 ar9170_handle_tx_status(&aru->common, skb, false,
102 AR9170_TX_STATUS_COMPLETE);
103}
104
105static void ar9170_usb_tx_urb_complete(struct urb *urb)
106{
107}
108
109static void ar9170_usb_irq_completed(struct urb *urb)
110{
111 struct ar9170_usb *aru = urb->context;
112
113 switch (urb->status) {
114 /* everything is fine */
115 case 0:
116 break;
117
118 /* disconnect */
119 case -ENOENT:
120 case -ECONNRESET:
121 case -ENODEV:
122 case -ESHUTDOWN:
123 goto free;
124
125 default:
126 goto resubmit;
127 }
128
129 print_hex_dump_bytes("ar9170 irq: ", DUMP_PREFIX_OFFSET,
130 urb->transfer_buffer, urb->actual_length);
131
132resubmit:
133 usb_anchor_urb(urb, &aru->rx_submitted);
134 if (usb_submit_urb(urb, GFP_ATOMIC)) {
135 usb_unanchor_urb(urb);
136 goto free;
137 }
138
139 return;
140
141free:
142 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
143}
144
145static void ar9170_usb_rx_completed(struct urb *urb)
146{
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));
150 int err;
151
152 if (!aru)
153 goto free;
154
155 switch (urb->status) {
156 /* everything is fine */
157 case 0:
158 break;
159
160 /* disconnect */
161 case -ENOENT:
162 case -ECONNRESET:
163 case -ENODEV:
164 case -ESHUTDOWN:
165 goto free;
166
167 default:
168 goto resubmit;
169 }
170
171 skb_put(skb, urb->actual_length);
172 ar9170_rx(&aru->common, skb);
173
174resubmit:
175 skb_reset_tail_pointer(skb);
176 skb_trim(skb, 0);
177
178 usb_anchor_urb(urb, &aru->rx_submitted);
179 err = usb_submit_urb(urb, GFP_ATOMIC);
180 if (err) {
181 usb_unanchor_urb(urb);
182 dev_kfree_skb_irq(skb);
183 }
184
185 return ;
186
187free:
188 dev_kfree_skb_irq(skb);
189 return;
190}
191
192static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
193 struct urb *urb, gfp_t gfp)
194{
195 struct sk_buff *skb;
196
197 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
198 if (!skb)
199 return -ENOMEM;
200
201 /* reserve some space for mac80211's radiotap */
202 skb_reserve(skb, 32);
203
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);
209
210 return 0;
211}
212
213static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
214{
215 struct urb *urb = NULL;
216 void *ibuf;
217 int err = -ENOMEM;
218
219 /* initialize interrupt endpoint */
220 urb = usb_alloc_urb(0, GFP_KERNEL);
221 if (!urb)
222 goto out;
223
224 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
225 if (!ibuf)
226 goto out;
227
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;
232
233 usb_anchor_urb(urb, &aru->rx_submitted);
234 err = usb_submit_urb(urb, GFP_KERNEL);
235 if (err) {
236 usb_unanchor_urb(urb);
237 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
238 urb->transfer_dma);
239 }
240
241out:
242 usb_free_urb(urb);
243 return err;
244}
245
246static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
247{
248 struct urb *urb;
249 int i;
250 int err = -EINVAL;
251
252 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
253 err = -ENOMEM;
254 urb = usb_alloc_urb(0, GFP_KERNEL);
255 if (!urb)
256 goto err_out;
257
258 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
259 if (err) {
260 usb_free_urb(urb);
261 goto err_out;
262 }
263
264 usb_anchor_urb(urb, &aru->rx_submitted);
265 err = usb_submit_urb(urb, GFP_KERNEL);
266 if (err) {
267 usb_unanchor_urb(urb);
268 dev_kfree_skb_any((void *) urb->transfer_buffer);
269 usb_free_urb(urb);
270 goto err_out;
271 }
272 usb_free_urb(urb);
273 }
274
275 /* the device now waiting for a firmware. */
276 aru->common.state = AR9170_IDLE;
277 return 0;
278
279err_out:
280
281 usb_kill_anchored_urbs(&aru->rx_submitted);
282 return err;
283}
284
285static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
286{
287 int ret;
288
289 aru->common.state = AR9170_UNKNOWN_STATE;
290
291 usb_unlink_anchored_urbs(&aru->tx_submitted);
292
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));
296 if (ret == 0)
297 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
298 usb_poison_anchored_urbs(&aru->tx_submitted);
299
300 usb_poison_anchored_urbs(&aru->rx_submitted);
301}
302
303static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
304 unsigned int plen, void *payload,
305 unsigned int outlen, void *out)
306{
307 struct ar9170_usb *aru = (void *) ar;
308 struct urb *urb = NULL;
309 unsigned long flags;
310 int err = -ENOMEM;
311
312 if (unlikely(!IS_ACCEPTING_CMD(ar)))
313 return -EPERM;
314
315 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
316 return -EINVAL;
317
318 urb = usb_alloc_urb(0, GFP_ATOMIC);
319 if (unlikely(!urb))
320 goto err_free;
321
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);
327
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);
332
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);
337
338 usb_anchor_urb(urb, &aru->tx_submitted);
339 err = usb_submit_urb(urb, GFP_ATOMIC);
340 if (err) {
341 usb_unanchor_urb(urb);
342 usb_free_urb(urb);
343 goto err_unbuf;
344 }
345 usb_free_urb(urb);
346
347 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
348 if (err == 0) {
349 err = -ETIMEDOUT;
350 goto err_unbuf;
351 }
352
ba5101d0 353 if (aru->readlen != outlen) {
b63a2cb3
CL
354 err = -EMSGSIZE;
355 goto err_unbuf;
356 }
357
358 return 0;
359
360err_unbuf:
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);
365
366 /* provide some maybe useful debug information */
367 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
368 aru->common.cmdbuf, plen + 4);
369 dump_stack();
370 }
371
372 /* invalidate to avoid completing the next prematurely */
373 spin_lock_irqsave(&aru->common.cmdlock, flags);
374 aru->readbuf = NULL;
375 aru->readlen = 0;
376 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
377
378err_free:
379
380 return err;
381}
382
383static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb,
384 bool txstatus_needed, unsigned int extra_len)
385{
386 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
387 struct urb *urb;
388 int err;
389
390 if (unlikely(!IS_STARTED(ar))) {
391 /* Seriously, what were you drink... err... thinking!? */
392 return -EPERM;
393 }
394
395 urb = usb_alloc_urb(0, GFP_ATOMIC);
396 if (unlikely(!urb))
397 return -ENOMEM;
398
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;
405
406 usb_anchor_urb(urb, &aru->tx_submitted);
407 err = usb_submit_urb(urb, GFP_ATOMIC);
408 if (unlikely(err))
409 usb_unanchor_urb(urb);
410
411 usb_free_urb(urb);
412 return err;
413}
414
415static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
416{
417 struct ar9170_usb *aru = (void *) ar;
418 unsigned long flags;
419 u32 in, out;
420
421 if (!buffer)
422 return ;
423
424 in = le32_to_cpup((__le32 *)buffer);
425 out = le32_to_cpu(ar->cmdbuf[0]);
426
427 /* mask off length byte */
428 out &= ~0xFF;
429
430 if (aru->readlen >= 0) {
431 /* add expected length */
432 out |= aru->readlen;
433 } else {
434 /* add obtained length */
435 out |= in & 0xFF;
436 }
437
438 /*
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.
442 */
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",
447 in, out, len);
448 print_hex_dump_bytes("ar9170 invalid resp: ",
449 DUMP_PREFIX_OFFSET, buffer, len);
450 /*
451 * Do not complete, then the command times out,
452 * and we get a stack trace from there.
453 */
454 return ;
455 }
456
457 spin_lock_irqsave(&aru->common.cmdlock, flags);
458 if (aru->readbuf && len > 0) {
459 memcpy(aru->readbuf, buffer + 4, len - 4);
460 aru->readbuf = NULL;
461 }
462 complete(&aru->cmd_wait);
463 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
464}
465
466static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
467 size_t len, u32 addr, bool complete)
468{
469 int transfer, err;
470 u8 *buf = kmalloc(4096, GFP_KERNEL);
471
472 if (!buf)
473 return -ENOMEM;
474
475 while (len) {
476 transfer = min_t(int, len, 4096);
477 memcpy(buf, data, transfer);
478
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);
482
483 if (err < 0) {
484 kfree(buf);
485 return err;
486 }
487
488 len -= transfer;
489 data += transfer;
490 addr += transfer;
491 }
492 kfree(buf);
493
494 if (complete) {
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);
498 }
499
500 return 0;
501}
502
503static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
504{
505 int err = 0;
506
507 err = request_firmware(&aru->init_values, "ar9170-1.fw",
508 &aru->udev->dev);
509 if (err) {
510 dev_err(&aru->udev->dev, "file with init values not found.\n");
511 return err;
512 }
513
514 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
515 if (err) {
516 release_firmware(aru->init_values);
517 dev_err(&aru->udev->dev, "firmware file not found.\n");
518 return err;
519 }
520
521 return err;
522}
523
524static int ar9170_usb_reset(struct ar9170_usb *aru)
525{
526 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
527
528 if (lock) {
529 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
530 if (ret < 0) {
531 dev_err(&aru->udev->dev, "unable to lock device "
532 "for reset (%d).\n", ret);
533 return ret;
534 }
535 }
536
537 ret = usb_reset_device(aru->udev);
538 if (lock)
539 usb_unlock_device(aru->udev);
540
541 /* let it rest - for a second - */
542 msleep(1000);
543
544 return ret;
545}
546
547static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
548{
549 int err;
550
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);
554 if (err) {
555 dev_err(&aru->udev->dev, "firmware part 1 "
556 "upload failed (%d).\n", err);
557 return err;
558 }
559
560 /* Then, upload the firmware itself and start it */
561 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
562 0x200000, true);
563}
564
565static int ar9170_usb_init_transport(struct ar9170_usb *aru)
566{
567 struct ar9170 *ar = (void *) &aru->common;
568 int err;
569
570 ar9170_regwrite_begin(ar);
571
572 /* Set USB Rx stream mode MAX packet number to 2 */
573 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
574
575 /* Set USB Rx stream mode timeout to 10us */
576 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
577
578 ar9170_regwrite_finish();
579
580 err = ar9170_regwrite_result();
581 if (err)
582 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
583
584 return err;
585}
586
587static void ar9170_usb_stop(struct ar9170 *ar)
588{
589 struct ar9170_usb *aru = (void *) ar;
590 int ret;
591
592 if (IS_ACCEPTING_CMD(ar))
593 aru->common.state = AR9170_STOPPED;
594
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));
598 if (ret == 0)
599 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
600
601 usb_poison_anchored_urbs(&aru->tx_submitted);
602
603 /*
604 * Note:
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...
607 */
608}
609
610static int ar9170_usb_open(struct ar9170 *ar)
611{
612 struct ar9170_usb *aru = (void *) ar;
613 int err;
614
615 usb_unpoison_anchored_urbs(&aru->tx_submitted);
616 err = ar9170_usb_init_transport(aru);
617 if (err) {
618 usb_poison_anchored_urbs(&aru->tx_submitted);
619 return err;
620 }
621
622 aru->common.state = AR9170_IDLE;
623 return 0;
624}
625
e10a9dfc
CL
626static int ar9170_usb_init_device(struct ar9170_usb *aru)
627{
628 int err;
629
630 err = ar9170_usb_alloc_rx_irq_urb(aru);
631 if (err)
632 goto err_out;
633
634 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
635 if (err)
636 goto err_unrx;
637
638 err = ar9170_usb_upload_firmware(aru);
639 if (err) {
640 err = ar9170_echo_test(&aru->common, 0x60d43110);
641 if (err) {
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");
646 goto err_unrx;
647 }
648 }
649
650 return 0;
651
652err_unrx:
653 ar9170_usb_cancel_urbs(aru);
654
655err_out:
656 return err;
657}
658
b63a2cb3
CL
659static int ar9170_usb_probe(struct usb_interface *intf,
660 const struct usb_device_id *id)
661{
662 struct ar9170_usb *aru;
663 struct ar9170 *ar;
664 struct usb_device *udev;
665 int err;
666
667 aru = ar9170_alloc(sizeof(*aru));
668 if (IS_ERR(aru)) {
669 err = PTR_ERR(aru);
670 goto out;
671 }
672
673 udev = interface_to_usbdev(intf);
674 usb_get_dev(udev);
675 aru->udev = udev;
676 aru->intf = intf;
677 ar = &aru->common;
678
679 usb_set_intfdata(intf, aru);
680 SET_IEEE80211_DEV(ar->hw, &udev->dev);
681
682 init_usb_anchor(&aru->rx_submitted);
683 init_usb_anchor(&aru->tx_submitted);
684 init_completion(&aru->cmd_wait);
685
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;
691
8a713040 692#ifdef CONFIG_PM
90ccda9b 693 udev->reset_resume = 1;
8a713040 694#endif
b63a2cb3
CL
695 err = ar9170_usb_reset(aru);
696 if (err)
e10a9dfc 697 goto err_freehw;
b63a2cb3
CL
698
699 err = ar9170_usb_request_firmware(aru);
700 if (err)
e10a9dfc 701 goto err_freehw;
b63a2cb3 702
e10a9dfc 703 err = ar9170_usb_init_device(aru);
b63a2cb3
CL
704 if (err)
705 goto err_freefw;
706
b63a2cb3
CL
707 err = ar9170_usb_open(ar);
708 if (err)
709 goto err_unrx;
710
711 err = ar9170_register(ar, &udev->dev);
712
713 ar9170_usb_stop(ar);
714 if (err)
715 goto err_unrx;
716
717 return 0;
718
719err_unrx:
720 ar9170_usb_cancel_urbs(aru);
721
722err_freefw:
723 release_firmware(aru->init_values);
724 release_firmware(aru->firmware);
725
e10a9dfc 726err_freehw:
b63a2cb3
CL
727 usb_set_intfdata(intf, NULL);
728 usb_put_dev(udev);
729 ieee80211_free_hw(ar->hw);
730out:
731 return err;
732}
733
734static void ar9170_usb_disconnect(struct usb_interface *intf)
735{
736 struct ar9170_usb *aru = usb_get_intfdata(intf);
737
738 if (!aru)
739 return;
740
741 aru->common.state = AR9170_IDLE;
742 ar9170_unregister(&aru->common);
743 ar9170_usb_cancel_urbs(aru);
744
745 release_firmware(aru->init_values);
746 release_firmware(aru->firmware);
747
748 usb_put_dev(aru->udev);
749 usb_set_intfdata(intf, NULL);
750 ieee80211_free_hw(aru->common.hw);
751}
752
e10a9dfc
CL
753#ifdef CONFIG_PM
754static int ar9170_suspend(struct usb_interface *intf,
755 pm_message_t message)
756{
757 struct ar9170_usb *aru = usb_get_intfdata(intf);
758
759 if (!aru)
760 return -ENODEV;
761
762 aru->common.state = AR9170_IDLE;
763 ar9170_usb_cancel_urbs(aru);
764
765 return 0;
766}
767
768static int ar9170_resume(struct usb_interface *intf)
769{
770 struct ar9170_usb *aru = usb_get_intfdata(intf);
771 int err;
772
773 if (!aru)
774 return -ENODEV;
775
776 usb_unpoison_anchored_urbs(&aru->rx_submitted);
777 usb_unpoison_anchored_urbs(&aru->tx_submitted);
778
779 /*
780 * FIXME: firmware upload will fail on resume.
781 * but this is better than a hang!
782 */
783
784 err = ar9170_usb_init_device(aru);
785 if (err)
786 goto err_unrx;
787
788 err = ar9170_usb_open(&aru->common);
789 if (err)
790 goto err_unrx;
791
792 return 0;
793
794err_unrx:
795 aru->common.state = AR9170_IDLE;
796 ar9170_usb_cancel_urbs(aru);
797
798 return err;
799}
800#endif /* CONFIG_PM */
801
b63a2cb3
CL
802static struct usb_driver ar9170_driver = {
803 .name = "ar9170usb",
804 .probe = ar9170_usb_probe,
805 .disconnect = ar9170_usb_disconnect,
806 .id_table = ar9170_usb_ids,
807 .soft_unbind = 1,
e10a9dfc
CL
808#ifdef CONFIG_PM
809 .suspend = ar9170_suspend,
810 .resume = ar9170_resume,
90ccda9b 811 .reset_resume = ar9170_resume,
e10a9dfc 812#endif /* CONFIG_PM */
b63a2cb3
CL
813};
814
815static int __init ar9170_init(void)
816{
817 return usb_register(&ar9170_driver);
818}
819
820static void __exit ar9170_exit(void)
821{
822 usb_deregister(&ar9170_driver);
823}
824
825module_init(ar9170_init);
826module_exit(ar9170_exit);