ar9170: wait for asynchronous firmware loading
[pandora-kernel.git] / drivers / net / wireless / ath / ar9170 / usb.c
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
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");
57
58 enum ar9170_requirements {
59         AR9170_REQ_FW1_ONLY = 1,
60 };
61
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 },
101
102         /* terminate */
103         {}
104 };
105 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
106
107 static void ar9170_usb_submit_urb(struct ar9170_usb *aru)
108 {
109         struct urb *urb;
110         unsigned long flags;
111         int err;
112
113         if (unlikely(!IS_STARTED(&aru->common)))
114                 return ;
115
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 ;
120         }
121         atomic_inc(&aru->tx_submitted_urbs);
122
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);
127
128                 return ;
129         }
130         spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
131
132         aru->tx_pending_urbs--;
133         usb_anchor_urb(urb, &aru->tx_submitted);
134
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);
140
141                 usb_unanchor_urb(urb);
142                 atomic_dec(&aru->tx_submitted_urbs);
143                 ar9170_tx_callback(&aru->common, urb->context);
144         }
145
146         usb_free_urb(urb);
147 }
148
149 static void ar9170_usb_tx_urb_complete_frame(struct urb *urb)
150 {
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));
154
155         if (unlikely(!aru)) {
156                 dev_kfree_skb_irq(skb);
157                 return ;
158         }
159
160         atomic_dec(&aru->tx_submitted_urbs);
161
162         ar9170_tx_callback(&aru->common, skb);
163
164         ar9170_usb_submit_urb(aru);
165 }
166
167 static void ar9170_usb_tx_urb_complete(struct urb *urb)
168 {
169 }
170
171 static void ar9170_usb_irq_completed(struct urb *urb)
172 {
173         struct ar9170_usb *aru = urb->context;
174
175         switch (urb->status) {
176         /* everything is fine */
177         case 0:
178                 break;
179
180         /* disconnect */
181         case -ENOENT:
182         case -ECONNRESET:
183         case -ENODEV:
184         case -ESHUTDOWN:
185                 goto free;
186
187         default:
188                 goto resubmit;
189         }
190
191         ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
192                                        urb->actual_length);
193
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;
199         }
200
201         return;
202
203 free:
204         usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
205 }
206
207 static void ar9170_usb_rx_completed(struct urb *urb)
208 {
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;
213
214         if (!aru)
215                 goto free;
216
217         switch (urb->status) {
218         /* everything is fine */
219         case 0:
220                 break;
221
222         /* disconnect */
223         case -ENOENT:
224         case -ECONNRESET:
225         case -ENODEV:
226         case -ESHUTDOWN:
227                 goto free;
228
229         default:
230                 goto resubmit;
231         }
232
233         skb_put(skb, urb->actual_length);
234         ar9170_rx(&aru->common, skb);
235
236 resubmit:
237         skb_reset_tail_pointer(skb);
238         skb_trim(skb, 0);
239
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;
245         }
246
247         return ;
248
249 free:
250         dev_kfree_skb_irq(skb);
251 }
252
253 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
254                                   struct urb *urb, gfp_t gfp)
255 {
256         struct sk_buff *skb;
257
258         skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
259         if (!skb)
260                 return -ENOMEM;
261
262         /* reserve some space for mac80211's radiotap */
263         skb_reserve(skb, 32);
264
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);
270
271         return 0;
272 }
273
274 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
275 {
276         struct urb *urb = NULL;
277         void *ibuf;
278         int err = -ENOMEM;
279
280         /* initialize interrupt endpoint */
281         urb = usb_alloc_urb(0, GFP_KERNEL);
282         if (!urb)
283                 goto out;
284
285         ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
286         if (!ibuf)
287                 goto out;
288
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;
293
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);
300         }
301
302 out:
303         usb_free_urb(urb);
304         return err;
305 }
306
307 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
308 {
309         struct urb *urb;
310         int i;
311         int err = -EINVAL;
312
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;
318
319                 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
320                 if (err) {
321                         usb_free_urb(urb);
322                         goto err_out;
323                 }
324
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;
332                 }
333                 usb_free_urb(urb);
334         }
335
336         /* the device now waiting for a firmware. */
337         aru->common.state = AR9170_IDLE;
338         return 0;
339
340 err_out:
341
342         usb_kill_anchored_urbs(&aru->rx_submitted);
343         return err;
344 }
345
346 static int ar9170_usb_flush(struct ar9170 *ar)
347 {
348         struct ar9170_usb *aru = (void *) ar;
349         struct urb *urb;
350         int ret, err = 0;
351
352         if (IS_STARTED(ar))
353                 aru->common.state = AR9170_IDLE;
354
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);
360         }
361
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;
367
368         usb_kill_anchored_urbs(&aru->tx_submitted);
369
370         if (IS_ACCEPTING_CMD(ar))
371                 aru->common.state = AR9170_STARTED;
372
373         return err;
374 }
375
376 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
377 {
378         int err;
379
380         aru->common.state = AR9170_UNKNOWN_STATE;
381
382         err = ar9170_usb_flush(&aru->common);
383         if (err)
384                 dev_err(&aru->udev->dev, "stuck tx urbs!\n");
385
386         usb_poison_anchored_urbs(&aru->tx_submitted);
387         usb_poison_anchored_urbs(&aru->rx_submitted);
388 }
389
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)
393 {
394         struct ar9170_usb *aru = (void *) ar;
395         struct urb *urb = NULL;
396         unsigned long flags;
397         int err = -ENOMEM;
398
399         if (unlikely(!IS_ACCEPTING_CMD(ar)))
400                 return -EPERM;
401
402         if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
403                 return -EINVAL;
404
405         urb = usb_alloc_urb(0, GFP_ATOMIC);
406         if (unlikely(!urb))
407                 goto err_free;
408
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);
414
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);
419
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);
424
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;
431         }
432         usb_free_urb(urb);
433
434         err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
435         if (err == 0) {
436                 err = -ETIMEDOUT;
437                 goto err_unbuf;
438         }
439
440         if (aru->readlen != outlen) {
441                 err = -EMSGSIZE;
442                 goto err_unbuf;
443         }
444
445         return 0;
446
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);
452
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();
457         }
458
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);
464
465 err_free:
466
467         return err;
468 }
469
470 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
471 {
472         struct ar9170_usb *aru = (struct ar9170_usb *) ar;
473         struct urb *urb;
474
475         if (unlikely(!IS_STARTED(ar))) {
476                 /* Seriously, what were you drink... err... thinking!? */
477                 return -EPERM;
478         }
479
480         urb = usb_alloc_urb(0, GFP_ATOMIC);
481         if (unlikely(!urb))
482                 return -ENOMEM;
483
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;
489
490         usb_anchor_urb(urb, &aru->tx_pending);
491         aru->tx_pending_urbs++;
492
493         usb_free_urb(urb);
494
495         ar9170_usb_submit_urb(aru);
496         return 0;
497 }
498
499 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
500 {
501         struct ar9170_usb *aru = (void *) ar;
502         unsigned long flags;
503         u32 in, out;
504
505         if (unlikely(!buffer))
506                 return ;
507
508         in = le32_to_cpup((__le32 *)buffer);
509         out = le32_to_cpu(ar->cmdbuf[0]);
510
511         /* mask off length byte */
512         out &= ~0xFF;
513
514         if (aru->readlen >= 0) {
515                 /* add expected length */
516                 out |= aru->readlen;
517         } else {
518                 /* add obtained length */
519                 out |= in & 0xFF;
520         }
521
522         /*
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.
526          */
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);
534                 /*
535                  * Do not complete, then the command times out,
536                  * and we get a stack trace from there.
537                  */
538                 return ;
539         }
540
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;
545         }
546         complete(&aru->cmd_wait);
547         spin_unlock_irqrestore(&aru->common.cmdlock, flags);
548 }
549
550 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
551                              size_t len, u32 addr, bool complete)
552 {
553         int transfer, err;
554         u8 *buf = kmalloc(4096, GFP_KERNEL);
555
556         if (!buf)
557                 return -ENOMEM;
558
559         while (len) {
560                 transfer = min_t(int, len, 4096);
561                 memcpy(buf, data, transfer);
562
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);
566
567                 if (err < 0) {
568                         kfree(buf);
569                         return err;
570                 }
571
572                 len -= transfer;
573                 data += transfer;
574                 addr += transfer;
575         }
576         kfree(buf);
577
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);
582         }
583
584         return 0;
585 }
586
587 static int ar9170_usb_reset(struct ar9170_usb *aru)
588 {
589         int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
590
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;
597                 }
598         }
599
600         ret = usb_reset_device(aru->udev);
601         if (lock)
602                 usb_unlock_device(aru->udev);
603
604         /* let it rest - for a second - */
605         msleep(1000);
606
607         return ret;
608 }
609
610 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
611 {
612         int err;
613
614         if (!aru->init_values)
615                 goto upload_fw_start;
616
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;
624         }
625
626 upload_fw_start:
627
628         /* Then, upload the firmware itself and start it */
629         return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
630                                 0x200000, true);
631 }
632
633 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
634 {
635         struct ar9170 *ar = (void *) &aru->common;
636         int err;
637
638         ar9170_regwrite_begin(ar);
639
640         /* Set USB Rx stream mode MAX packet number to 2 */
641         ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
642
643         /* Set USB Rx stream mode timeout to 10us */
644         ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
645
646         ar9170_regwrite_finish();
647
648         err = ar9170_regwrite_result();
649         if (err)
650                 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
651
652         return err;
653 }
654
655 static void ar9170_usb_stop(struct ar9170 *ar)
656 {
657         struct ar9170_usb *aru = (void *) ar;
658         int ret;
659
660         if (IS_ACCEPTING_CMD(ar))
661                 aru->common.state = AR9170_STOPPED;
662
663         ret = ar9170_usb_flush(ar);
664         if (ret)
665                 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
666
667         usb_poison_anchored_urbs(&aru->tx_submitted);
668
669         /*
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...
673          */
674 }
675
676 static int ar9170_usb_open(struct ar9170 *ar)
677 {
678         struct ar9170_usb *aru = (void *) ar;
679         int err;
680
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;
686         }
687
688         aru->common.state = AR9170_IDLE;
689         return 0;
690 }
691
692 static int ar9170_usb_init_device(struct ar9170_usb *aru)
693 {
694         int err;
695
696         err = ar9170_usb_alloc_rx_irq_urb(aru);
697         if (err)
698                 goto err_out;
699
700         err = ar9170_usb_alloc_rx_bulk_urbs(aru);
701         if (err)
702                 goto err_unrx;
703
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;
713                 }
714         }
715
716         return 0;
717
718 err_unrx:
719         ar9170_usb_cancel_urbs(aru);
720
721 err_out:
722         return err;
723 }
724
725 static void ar9170_usb_firmware_failed(struct ar9170_usb *aru)
726 {
727         struct device *parent = aru->udev->dev.parent;
728
729         complete(&aru->firmware_loading_complete);
730
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);
737
738         usb_put_dev(aru->udev);
739 }
740
741 static void ar9170_usb_firmware_finish(const struct firmware *fw, void *context)
742 {
743         struct ar9170_usb *aru = context;
744         int err;
745
746         aru->firmware = fw;
747
748         if (!fw) {
749                 dev_err(&aru->udev->dev, "firmware file not found.\n");
750                 goto err_freefw;
751         }
752
753         err = ar9170_usb_init_device(aru);
754         if (err)
755                 goto err_freefw;
756
757         err = ar9170_usb_open(&aru->common);
758         if (err)
759                 goto err_unrx;
760
761         err = ar9170_register(&aru->common, &aru->udev->dev);
762
763         ar9170_usb_stop(&aru->common);
764         if (err)
765                 goto err_unrx;
766
767         complete(&aru->firmware_loading_complete);
768         usb_put_dev(aru->udev);
769         return;
770
771  err_unrx:
772         ar9170_usb_cancel_urbs(aru);
773
774  err_freefw:
775         ar9170_usb_firmware_failed(aru);
776 }
777
778 static void ar9170_usb_firmware_inits(const struct firmware *fw,
779                                       void *context)
780 {
781         struct ar9170_usb *aru = context;
782         int err;
783
784         if (!fw) {
785                 dev_err(&aru->udev->dev, "file with init values not found.\n");
786                 ar9170_usb_firmware_failed(aru);
787                 return;
788         }
789
790         aru->init_values = fw;
791
792         /* ok so we have the init values -- get code for two-stage */
793
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);
799 }
800
801 static void ar9170_usb_firmware_step2(const struct firmware *fw, void *context)
802 {
803         struct ar9170_usb *aru = context;
804         int err;
805
806         if (fw) {
807                 ar9170_usb_firmware_finish(fw, context);
808                 return;
809         }
810
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;
816         }
817
818         dev_err(&aru->udev->dev, "ar9170.fw firmware file "
819                 "not found, trying old firmware...\n");
820
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);
826 }
827
828 static bool ar9170_requires_one_stage(const struct usb_device_id *id)
829 {
830         if (!id->driver_info)
831                 return false;
832         if (id->driver_info == AR9170_REQ_FW1_ONLY)
833                 return true;
834         return false;
835 }
836
837 static int ar9170_usb_probe(struct usb_interface *intf,
838                         const struct usb_device_id *id)
839 {
840         struct ar9170_usb *aru;
841         struct ar9170 *ar;
842         struct usb_device *udev;
843         int err;
844
845         aru = ar9170_alloc(sizeof(*aru));
846         if (IS_ERR(aru)) {
847                 err = PTR_ERR(aru);
848                 goto out;
849         }
850
851         udev = interface_to_usbdev(intf);
852         usb_get_dev(udev);
853         aru->udev = udev;
854         aru->intf = intf;
855         ar = &aru->common;
856
857         aru->req_one_stage_fw = ar9170_requires_one_stage(id);
858
859         usb_set_intfdata(intf, aru);
860         SET_IEEE80211_DEV(ar->hw, &intf->dev);
861
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);
868
869         aru->tx_pending_urbs = 0;
870         atomic_set(&aru->tx_submitted_urbs, 0);
871
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;
878
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;
885
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;
896 }
897
898 static void ar9170_usb_disconnect(struct usb_interface *intf)
899 {
900         struct ar9170_usb *aru = usb_get_intfdata(intf);
901
902         if (!aru)
903                 return;
904
905         aru->common.state = AR9170_IDLE;
906
907         wait_for_completion(&aru->firmware_loading_complete);
908
909         ar9170_unregister(&aru->common);
910         ar9170_usb_cancel_urbs(aru);
911
912         usb_put_dev(aru->udev);
913         usb_set_intfdata(intf, NULL);
914         ieee80211_free_hw(aru->common.hw);
915
916         release_firmware(aru->init_values);
917         release_firmware(aru->firmware);
918 }
919
920 #ifdef CONFIG_PM
921 static int ar9170_suspend(struct usb_interface *intf,
922                           pm_message_t  message)
923 {
924         struct ar9170_usb *aru = usb_get_intfdata(intf);
925
926         if (!aru)
927                 return -ENODEV;
928
929         aru->common.state = AR9170_IDLE;
930         ar9170_usb_cancel_urbs(aru);
931
932         return 0;
933 }
934
935 static int ar9170_resume(struct usb_interface *intf)
936 {
937         struct ar9170_usb *aru = usb_get_intfdata(intf);
938         int err;
939
940         if (!aru)
941                 return -ENODEV;
942
943         usb_unpoison_anchored_urbs(&aru->rx_submitted);
944         usb_unpoison_anchored_urbs(&aru->tx_submitted);
945
946         err = ar9170_usb_init_device(aru);
947         if (err)
948                 goto err_unrx;
949
950         err = ar9170_usb_open(&aru->common);
951         if (err)
952                 goto err_unrx;
953
954         return 0;
955
956 err_unrx:
957         aru->common.state = AR9170_IDLE;
958         ar9170_usb_cancel_urbs(aru);
959
960         return err;
961 }
962 #endif /* CONFIG_PM */
963
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 */
975 };
976
977 static int __init ar9170_init(void)
978 {
979         return usb_register(&ar9170_driver);
980 }
981
982 static void __exit ar9170_exit(void)
983 {
984         usb_deregister(&ar9170_driver);
985 }
986
987 module_init(ar9170_init);
988 module_exit(ar9170_exit);