Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[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/slab.h>
42 #include <linux/usb.h>
43 #include <linux/firmware.h>
44 #include <linux/etherdevice.h>
45 #include <net/mac80211.h>
46 #include "ar9170.h"
47 #include "cmd.h"
48 #include "hw.h"
49 #include "usb.h"
50
51 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
52 MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
53 MODULE_LICENSE("GPL");
54 MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
55 MODULE_FIRMWARE("ar9170.fw");
56 MODULE_FIRMWARE("ar9170-1.fw");
57 MODULE_FIRMWARE("ar9170-2.fw");
58
59 enum ar9170_requirements {
60         AR9170_REQ_FW1_ONLY = 1,
61 };
62
63 static struct usb_device_id ar9170_usb_ids[] = {
64         /* Atheros 9170 */
65         { USB_DEVICE(0x0cf3, 0x9170) },
66         /* Atheros TG121N */
67         { USB_DEVICE(0x0cf3, 0x1001) },
68         /* TP-Link TL-WN821N v2 */
69         { USB_DEVICE(0x0cf3, 0x1002) },
70         /* Cace Airpcap NX */
71         { USB_DEVICE(0xcace, 0x0300) },
72         /* D-Link DWA 160 A1 */
73         { USB_DEVICE(0x07d1, 0x3c10) },
74         /* D-Link DWA 160 A2 */
75         { USB_DEVICE(0x07d1, 0x3a09) },
76         /* Netgear WNDA3100 */
77         { USB_DEVICE(0x0846, 0x9010) },
78         /* Netgear WN111 v2 */
79         { USB_DEVICE(0x0846, 0x9001) },
80         /* Zydas ZD1221 */
81         { USB_DEVICE(0x0ace, 0x1221) },
82         /* ZyXEL NWD271N */
83         { USB_DEVICE(0x0586, 0x3417) },
84         /* Z-Com UB81 BG */
85         { USB_DEVICE(0x0cde, 0x0023) },
86         /* Z-Com UB82 ABG */
87         { USB_DEVICE(0x0cde, 0x0026) },
88         /* Sphairon Homelink 1202 */
89         { USB_DEVICE(0x0cde, 0x0027) },
90         /* Arcadyan WN7512 */
91         { USB_DEVICE(0x083a, 0xf522) },
92         /* Planex GWUS300 */
93         { USB_DEVICE(0x2019, 0x5304) },
94         /* IO-Data WNGDNUS2 */
95         { USB_DEVICE(0x04bb, 0x093f) },
96         /* AVM FRITZ!WLAN USB Stick N */
97         { USB_DEVICE(0x057C, 0x8401) },
98         /* NEC WL300NU-G */
99         { USB_DEVICE(0x0409, 0x0249) },
100         /* AVM FRITZ!WLAN USB Stick N 2.4 */
101         { USB_DEVICE(0x057C, 0x8402), .driver_info = AR9170_REQ_FW1_ONLY },
102
103         /* terminate */
104         {}
105 };
106 MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
107
108 static void ar9170_usb_submit_urb(struct ar9170_usb *aru)
109 {
110         struct urb *urb;
111         unsigned long flags;
112         int err;
113
114         if (unlikely(!IS_STARTED(&aru->common)))
115                 return ;
116
117         spin_lock_irqsave(&aru->tx_urb_lock, flags);
118         if (atomic_read(&aru->tx_submitted_urbs) >= AR9170_NUM_TX_URBS) {
119                 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
120                 return ;
121         }
122         atomic_inc(&aru->tx_submitted_urbs);
123
124         urb = usb_get_from_anchor(&aru->tx_pending);
125         if (!urb) {
126                 atomic_dec(&aru->tx_submitted_urbs);
127                 spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
128
129                 return ;
130         }
131         spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
132
133         aru->tx_pending_urbs--;
134         usb_anchor_urb(urb, &aru->tx_submitted);
135
136         err = usb_submit_urb(urb, GFP_ATOMIC);
137         if (unlikely(err)) {
138                 if (ar9170_nag_limiter(&aru->common))
139                         dev_err(&aru->udev->dev, "submit_urb failed (%d).\n",
140                                 err);
141
142                 usb_unanchor_urb(urb);
143                 atomic_dec(&aru->tx_submitted_urbs);
144                 ar9170_tx_callback(&aru->common, urb->context);
145         }
146
147         usb_free_urb(urb);
148 }
149
150 static void ar9170_usb_tx_urb_complete_frame(struct urb *urb)
151 {
152         struct sk_buff *skb = urb->context;
153         struct ar9170_usb *aru = (struct ar9170_usb *)
154               usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
155
156         if (unlikely(!aru)) {
157                 dev_kfree_skb_irq(skb);
158                 return ;
159         }
160
161         atomic_dec(&aru->tx_submitted_urbs);
162
163         ar9170_tx_callback(&aru->common, skb);
164
165         ar9170_usb_submit_urb(aru);
166 }
167
168 static void ar9170_usb_tx_urb_complete(struct urb *urb)
169 {
170 }
171
172 static void ar9170_usb_irq_completed(struct urb *urb)
173 {
174         struct ar9170_usb *aru = urb->context;
175
176         switch (urb->status) {
177         /* everything is fine */
178         case 0:
179                 break;
180
181         /* disconnect */
182         case -ENOENT:
183         case -ECONNRESET:
184         case -ENODEV:
185         case -ESHUTDOWN:
186                 goto free;
187
188         default:
189                 goto resubmit;
190         }
191
192         ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
193                                        urb->actual_length);
194
195 resubmit:
196         usb_anchor_urb(urb, &aru->rx_submitted);
197         if (usb_submit_urb(urb, GFP_ATOMIC)) {
198                 usb_unanchor_urb(urb);
199                 goto free;
200         }
201
202         return;
203
204 free:
205         usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
206 }
207
208 static void ar9170_usb_rx_completed(struct urb *urb)
209 {
210         struct sk_buff *skb = urb->context;
211         struct ar9170_usb *aru = (struct ar9170_usb *)
212                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
213         int err;
214
215         if (!aru)
216                 goto free;
217
218         switch (urb->status) {
219         /* everything is fine */
220         case 0:
221                 break;
222
223         /* disconnect */
224         case -ENOENT:
225         case -ECONNRESET:
226         case -ENODEV:
227         case -ESHUTDOWN:
228                 goto free;
229
230         default:
231                 goto resubmit;
232         }
233
234         skb_put(skb, urb->actual_length);
235         ar9170_rx(&aru->common, skb);
236
237 resubmit:
238         skb_reset_tail_pointer(skb);
239         skb_trim(skb, 0);
240
241         usb_anchor_urb(urb, &aru->rx_submitted);
242         err = usb_submit_urb(urb, GFP_ATOMIC);
243         if (unlikely(err)) {
244                 usb_unanchor_urb(urb);
245                 goto free;
246         }
247
248         return ;
249
250 free:
251         dev_kfree_skb_irq(skb);
252 }
253
254 static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
255                                   struct urb *urb, gfp_t gfp)
256 {
257         struct sk_buff *skb;
258
259         skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
260         if (!skb)
261                 return -ENOMEM;
262
263         /* reserve some space for mac80211's radiotap */
264         skb_reserve(skb, 32);
265
266         usb_fill_bulk_urb(urb, aru->udev,
267                           usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
268                           skb->data, min(skb_tailroom(skb),
269                           AR9170_MAX_RX_BUFFER_SIZE),
270                           ar9170_usb_rx_completed, skb);
271
272         return 0;
273 }
274
275 static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
276 {
277         struct urb *urb = NULL;
278         void *ibuf;
279         int err = -ENOMEM;
280
281         /* initialize interrupt endpoint */
282         urb = usb_alloc_urb(0, GFP_KERNEL);
283         if (!urb)
284                 goto out;
285
286         ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
287         if (!ibuf)
288                 goto out;
289
290         usb_fill_int_urb(urb, aru->udev,
291                          usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
292                          64, ar9170_usb_irq_completed, aru, 1);
293         urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
294
295         usb_anchor_urb(urb, &aru->rx_submitted);
296         err = usb_submit_urb(urb, GFP_KERNEL);
297         if (err) {
298                 usb_unanchor_urb(urb);
299                 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
300                                 urb->transfer_dma);
301         }
302
303 out:
304         usb_free_urb(urb);
305         return err;
306 }
307
308 static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
309 {
310         struct urb *urb;
311         int i;
312         int err = -EINVAL;
313
314         for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
315                 err = -ENOMEM;
316                 urb = usb_alloc_urb(0, GFP_KERNEL);
317                 if (!urb)
318                         goto err_out;
319
320                 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
321                 if (err) {
322                         usb_free_urb(urb);
323                         goto err_out;
324                 }
325
326                 usb_anchor_urb(urb, &aru->rx_submitted);
327                 err = usb_submit_urb(urb, GFP_KERNEL);
328                 if (err) {
329                         usb_unanchor_urb(urb);
330                         dev_kfree_skb_any((void *) urb->transfer_buffer);
331                         usb_free_urb(urb);
332                         goto err_out;
333                 }
334                 usb_free_urb(urb);
335         }
336
337         /* the device now waiting for a firmware. */
338         aru->common.state = AR9170_IDLE;
339         return 0;
340
341 err_out:
342
343         usb_kill_anchored_urbs(&aru->rx_submitted);
344         return err;
345 }
346
347 static int ar9170_usb_flush(struct ar9170 *ar)
348 {
349         struct ar9170_usb *aru = (void *) ar;
350         struct urb *urb;
351         int ret, err = 0;
352
353         if (IS_STARTED(ar))
354                 aru->common.state = AR9170_IDLE;
355
356         usb_wait_anchor_empty_timeout(&aru->tx_pending,
357                                             msecs_to_jiffies(800));
358         while ((urb = usb_get_from_anchor(&aru->tx_pending))) {
359                 ar9170_tx_callback(&aru->common, (void *) urb->context);
360                 usb_free_urb(urb);
361         }
362
363         /* lets wait a while until the tx - queues are dried out */
364         ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
365                                             msecs_to_jiffies(100));
366         if (ret == 0)
367                 err = -ETIMEDOUT;
368
369         usb_kill_anchored_urbs(&aru->tx_submitted);
370
371         if (IS_ACCEPTING_CMD(ar))
372                 aru->common.state = AR9170_STARTED;
373
374         return err;
375 }
376
377 static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
378 {
379         int err;
380
381         aru->common.state = AR9170_UNKNOWN_STATE;
382
383         err = ar9170_usb_flush(&aru->common);
384         if (err)
385                 dev_err(&aru->udev->dev, "stuck tx urbs!\n");
386
387         usb_poison_anchored_urbs(&aru->tx_submitted);
388         usb_poison_anchored_urbs(&aru->rx_submitted);
389 }
390
391 static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
392                                unsigned int plen, void *payload,
393                                unsigned int outlen, void *out)
394 {
395         struct ar9170_usb *aru = (void *) ar;
396         struct urb *urb = NULL;
397         unsigned long flags;
398         int err = -ENOMEM;
399
400         if (unlikely(!IS_ACCEPTING_CMD(ar)))
401                 return -EPERM;
402
403         if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
404                 return -EINVAL;
405
406         urb = usb_alloc_urb(0, GFP_ATOMIC);
407         if (unlikely(!urb))
408                 goto err_free;
409
410         ar->cmdbuf[0] = cpu_to_le32(plen);
411         ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
412         /* writing multiple regs fills this buffer already */
413         if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
414                 memcpy(&ar->cmdbuf[1], payload, plen);
415
416         spin_lock_irqsave(&aru->common.cmdlock, flags);
417         aru->readbuf = (u8 *)out;
418         aru->readlen = outlen;
419         spin_unlock_irqrestore(&aru->common.cmdlock, flags);
420
421         usb_fill_int_urb(urb, aru->udev,
422                          usb_sndintpipe(aru->udev, AR9170_EP_CMD),
423                          aru->common.cmdbuf, plen + 4,
424                          ar9170_usb_tx_urb_complete, NULL, 1);
425
426         usb_anchor_urb(urb, &aru->tx_submitted);
427         err = usb_submit_urb(urb, GFP_ATOMIC);
428         if (unlikely(err)) {
429                 usb_unanchor_urb(urb);
430                 usb_free_urb(urb);
431                 goto err_unbuf;
432         }
433         usb_free_urb(urb);
434
435         err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
436         if (err == 0) {
437                 err = -ETIMEDOUT;
438                 goto err_unbuf;
439         }
440
441         if (aru->readlen != outlen) {
442                 err = -EMSGSIZE;
443                 goto err_unbuf;
444         }
445
446         return 0;
447
448 err_unbuf:
449         /* Maybe the device was removed in the second we were waiting? */
450         if (IS_STARTED(ar)) {
451                 dev_err(&aru->udev->dev, "no command feedback "
452                                          "received (%d).\n", err);
453
454                 /* provide some maybe useful debug information */
455                 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
456                                      aru->common.cmdbuf, plen + 4);
457                 dump_stack();
458         }
459
460         /* invalidate to avoid completing the next prematurely */
461         spin_lock_irqsave(&aru->common.cmdlock, flags);
462         aru->readbuf = NULL;
463         aru->readlen = 0;
464         spin_unlock_irqrestore(&aru->common.cmdlock, flags);
465
466 err_free:
467
468         return err;
469 }
470
471 static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
472 {
473         struct ar9170_usb *aru = (struct ar9170_usb *) ar;
474         struct urb *urb;
475
476         if (unlikely(!IS_STARTED(ar))) {
477                 /* Seriously, what were you drink... err... thinking!? */
478                 return -EPERM;
479         }
480
481         urb = usb_alloc_urb(0, GFP_ATOMIC);
482         if (unlikely(!urb))
483                 return -ENOMEM;
484
485         usb_fill_bulk_urb(urb, aru->udev,
486                           usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
487                           skb->data, skb->len,
488                           ar9170_usb_tx_urb_complete_frame, skb);
489         urb->transfer_flags |= URB_ZERO_PACKET;
490
491         usb_anchor_urb(urb, &aru->tx_pending);
492         aru->tx_pending_urbs++;
493
494         usb_free_urb(urb);
495
496         ar9170_usb_submit_urb(aru);
497         return 0;
498 }
499
500 static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
501 {
502         struct ar9170_usb *aru = (void *) ar;
503         unsigned long flags;
504         u32 in, out;
505
506         if (unlikely(!buffer))
507                 return ;
508
509         in = le32_to_cpup((__le32 *)buffer);
510         out = le32_to_cpu(ar->cmdbuf[0]);
511
512         /* mask off length byte */
513         out &= ~0xFF;
514
515         if (aru->readlen >= 0) {
516                 /* add expected length */
517                 out |= aru->readlen;
518         } else {
519                 /* add obtained length */
520                 out |= in & 0xFF;
521         }
522
523         /*
524          * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
525          * length and we cannot predict the correct length in advance.
526          * So we only check if we provided enough space for the data.
527          */
528         if (unlikely(out < in)) {
529                 dev_warn(&aru->udev->dev, "received invalid command response "
530                                           "got %d bytes, instead of %d bytes "
531                                           "and the resp length is %d bytes\n",
532                          in, out, len);
533                 print_hex_dump_bytes("ar9170 invalid resp: ",
534                                      DUMP_PREFIX_OFFSET, buffer, len);
535                 /*
536                  * Do not complete, then the command times out,
537                  * and we get a stack trace from there.
538                  */
539                 return ;
540         }
541
542         spin_lock_irqsave(&aru->common.cmdlock, flags);
543         if (aru->readbuf && len > 0) {
544                 memcpy(aru->readbuf, buffer + 4, len - 4);
545                 aru->readbuf = NULL;
546         }
547         complete(&aru->cmd_wait);
548         spin_unlock_irqrestore(&aru->common.cmdlock, flags);
549 }
550
551 static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
552                              size_t len, u32 addr, bool complete)
553 {
554         int transfer, err;
555         u8 *buf = kmalloc(4096, GFP_KERNEL);
556
557         if (!buf)
558                 return -ENOMEM;
559
560         while (len) {
561                 transfer = min_t(int, len, 4096);
562                 memcpy(buf, data, transfer);
563
564                 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
565                                       0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
566                                       addr >> 8, 0, buf, transfer, 1000);
567
568                 if (err < 0) {
569                         kfree(buf);
570                         return err;
571                 }
572
573                 len -= transfer;
574                 data += transfer;
575                 addr += transfer;
576         }
577         kfree(buf);
578
579         if (complete) {
580                 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
581                                       0x31 /* FW DL COMPLETE */,
582                                       0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
583         }
584
585         return 0;
586 }
587
588 static int ar9170_usb_reset(struct ar9170_usb *aru)
589 {
590         int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
591
592         if (lock) {
593                 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
594                 if (ret < 0) {
595                         dev_err(&aru->udev->dev, "unable to lock device "
596                                 "for reset (%d).\n", ret);
597                         return ret;
598                 }
599         }
600
601         ret = usb_reset_device(aru->udev);
602         if (lock)
603                 usb_unlock_device(aru->udev);
604
605         /* let it rest - for a second - */
606         msleep(1000);
607
608         return ret;
609 }
610
611 static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
612 {
613         int err;
614
615         if (!aru->init_values)
616                 goto upload_fw_start;
617
618         /* First, upload initial values to device RAM */
619         err = ar9170_usb_upload(aru, aru->init_values->data,
620                                 aru->init_values->size, 0x102800, false);
621         if (err) {
622                 dev_err(&aru->udev->dev, "firmware part 1 "
623                         "upload failed (%d).\n", err);
624                 return err;
625         }
626
627 upload_fw_start:
628
629         /* Then, upload the firmware itself and start it */
630         return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
631                                 0x200000, true);
632 }
633
634 static int ar9170_usb_init_transport(struct ar9170_usb *aru)
635 {
636         struct ar9170 *ar = (void *) &aru->common;
637         int err;
638
639         ar9170_regwrite_begin(ar);
640
641         /* Set USB Rx stream mode MAX packet number to 2 */
642         ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
643
644         /* Set USB Rx stream mode timeout to 10us */
645         ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
646
647         ar9170_regwrite_finish();
648
649         err = ar9170_regwrite_result();
650         if (err)
651                 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
652
653         return err;
654 }
655
656 static void ar9170_usb_stop(struct ar9170 *ar)
657 {
658         struct ar9170_usb *aru = (void *) ar;
659         int ret;
660
661         if (IS_ACCEPTING_CMD(ar))
662                 aru->common.state = AR9170_STOPPED;
663
664         ret = ar9170_usb_flush(ar);
665         if (ret)
666                 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
667
668         usb_poison_anchored_urbs(&aru->tx_submitted);
669
670         /*
671          * Note:
672          * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
673          * Else we would end up with a unresponsive device...
674          */
675 }
676
677 static int ar9170_usb_open(struct ar9170 *ar)
678 {
679         struct ar9170_usb *aru = (void *) ar;
680         int err;
681
682         usb_unpoison_anchored_urbs(&aru->tx_submitted);
683         err = ar9170_usb_init_transport(aru);
684         if (err) {
685                 usb_poison_anchored_urbs(&aru->tx_submitted);
686                 return err;
687         }
688
689         aru->common.state = AR9170_IDLE;
690         return 0;
691 }
692
693 static int ar9170_usb_init_device(struct ar9170_usb *aru)
694 {
695         int err;
696
697         err = ar9170_usb_alloc_rx_irq_urb(aru);
698         if (err)
699                 goto err_out;
700
701         err = ar9170_usb_alloc_rx_bulk_urbs(aru);
702         if (err)
703                 goto err_unrx;
704
705         err = ar9170_usb_upload_firmware(aru);
706         if (err) {
707                 err = ar9170_echo_test(&aru->common, 0x60d43110);
708                 if (err) {
709                         /* force user invention, by disabling the device */
710                         err = usb_driver_set_configuration(aru->udev, -1);
711                         dev_err(&aru->udev->dev, "device is in a bad state. "
712                                                  "please reconnect it!\n");
713                         goto err_unrx;
714                 }
715         }
716
717         return 0;
718
719 err_unrx:
720         ar9170_usb_cancel_urbs(aru);
721
722 err_out:
723         return err;
724 }
725
726 static void ar9170_usb_firmware_failed(struct ar9170_usb *aru)
727 {
728         struct device *parent = aru->udev->dev.parent;
729
730         /* unbind anything failed */
731         if (parent)
732                 down(&parent->sem);
733         device_release_driver(&aru->udev->dev);
734         if (parent)
735                 up(&parent->sem);
736 }
737
738 static void ar9170_usb_firmware_finish(const struct firmware *fw, void *context)
739 {
740         struct ar9170_usb *aru = context;
741         int err;
742
743         aru->firmware = fw;
744
745         if (!fw) {
746                 dev_err(&aru->udev->dev, "firmware file not found.\n");
747                 goto err_freefw;
748         }
749
750         err = ar9170_usb_init_device(aru);
751         if (err)
752                 goto err_freefw;
753
754         err = ar9170_usb_open(&aru->common);
755         if (err)
756                 goto err_unrx;
757
758         err = ar9170_register(&aru->common, &aru->udev->dev);
759
760         ar9170_usb_stop(&aru->common);
761         if (err)
762                 goto err_unrx;
763
764         return;
765
766  err_unrx:
767         ar9170_usb_cancel_urbs(aru);
768
769  err_freefw:
770         ar9170_usb_firmware_failed(aru);
771 }
772
773 static void ar9170_usb_firmware_inits(const struct firmware *fw,
774                                       void *context)
775 {
776         struct ar9170_usb *aru = context;
777         int err;
778
779         if (!fw) {
780                 dev_err(&aru->udev->dev, "file with init values not found.\n");
781                 ar9170_usb_firmware_failed(aru);
782                 return;
783         }
784
785         aru->init_values = fw;
786
787         /* ok so we have the init values -- get code for two-stage */
788
789         err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-2.fw",
790                                       &aru->udev->dev, GFP_KERNEL, aru,
791                                       ar9170_usb_firmware_finish);
792         if (err)
793                 ar9170_usb_firmware_failed(aru);
794 }
795
796 static void ar9170_usb_firmware_step2(const struct firmware *fw, void *context)
797 {
798         struct ar9170_usb *aru = context;
799         int err;
800
801         if (fw) {
802                 ar9170_usb_firmware_finish(fw, context);
803                 return;
804         }
805
806         if (aru->req_one_stage_fw) {
807                 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
808                         "not found and is required for this device\n");
809                 ar9170_usb_firmware_failed(aru);
810                 return;
811         }
812
813         dev_err(&aru->udev->dev, "ar9170.fw firmware file "
814                 "not found, trying old firmware...\n");
815
816         err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-1.fw",
817                                       &aru->udev->dev, GFP_KERNEL, aru,
818                                       ar9170_usb_firmware_inits);
819         if (err)
820                 ar9170_usb_firmware_failed(aru);
821 }
822
823 static bool ar9170_requires_one_stage(const struct usb_device_id *id)
824 {
825         if (!id->driver_info)
826                 return false;
827         if (id->driver_info == AR9170_REQ_FW1_ONLY)
828                 return true;
829         return false;
830 }
831
832 static int ar9170_usb_probe(struct usb_interface *intf,
833                         const struct usb_device_id *id)
834 {
835         struct ar9170_usb *aru;
836         struct ar9170 *ar;
837         struct usb_device *udev;
838         int err;
839
840         aru = ar9170_alloc(sizeof(*aru));
841         if (IS_ERR(aru)) {
842                 err = PTR_ERR(aru);
843                 goto out;
844         }
845
846         udev = interface_to_usbdev(intf);
847         usb_get_dev(udev);
848         aru->udev = udev;
849         aru->intf = intf;
850         ar = &aru->common;
851
852         aru->req_one_stage_fw = ar9170_requires_one_stage(id);
853
854         usb_set_intfdata(intf, aru);
855         SET_IEEE80211_DEV(ar->hw, &intf->dev);
856
857         init_usb_anchor(&aru->rx_submitted);
858         init_usb_anchor(&aru->tx_pending);
859         init_usb_anchor(&aru->tx_submitted);
860         init_completion(&aru->cmd_wait);
861         spin_lock_init(&aru->tx_urb_lock);
862
863         aru->tx_pending_urbs = 0;
864         atomic_set(&aru->tx_submitted_urbs, 0);
865
866         aru->common.stop = ar9170_usb_stop;
867         aru->common.flush = ar9170_usb_flush;
868         aru->common.open = ar9170_usb_open;
869         aru->common.tx = ar9170_usb_tx;
870         aru->common.exec_cmd = ar9170_usb_exec_cmd;
871         aru->common.callback_cmd = ar9170_usb_callback_cmd;
872
873 #ifdef CONFIG_PM
874         udev->reset_resume = 1;
875 #endif /* CONFIG_PM */
876         err = ar9170_usb_reset(aru);
877         if (err)
878                 goto err_freehw;
879
880         return request_firmware_nowait(THIS_MODULE, 1, "ar9170.fw",
881                                        &aru->udev->dev, GFP_KERNEL, aru,
882                                        ar9170_usb_firmware_step2);
883 err_freehw:
884         usb_set_intfdata(intf, NULL);
885         usb_put_dev(udev);
886         ieee80211_free_hw(ar->hw);
887 out:
888         return err;
889 }
890
891 static void ar9170_usb_disconnect(struct usb_interface *intf)
892 {
893         struct ar9170_usb *aru = usb_get_intfdata(intf);
894
895         if (!aru)
896                 return;
897
898         aru->common.state = AR9170_IDLE;
899         ar9170_unregister(&aru->common);
900         ar9170_usb_cancel_urbs(aru);
901
902         usb_put_dev(aru->udev);
903         usb_set_intfdata(intf, NULL);
904         ieee80211_free_hw(aru->common.hw);
905
906         release_firmware(aru->init_values);
907         release_firmware(aru->firmware);
908 }
909
910 #ifdef CONFIG_PM
911 static int ar9170_suspend(struct usb_interface *intf,
912                           pm_message_t  message)
913 {
914         struct ar9170_usb *aru = usb_get_intfdata(intf);
915
916         if (!aru)
917                 return -ENODEV;
918
919         aru->common.state = AR9170_IDLE;
920         ar9170_usb_cancel_urbs(aru);
921
922         return 0;
923 }
924
925 static int ar9170_resume(struct usb_interface *intf)
926 {
927         struct ar9170_usb *aru = usb_get_intfdata(intf);
928         int err;
929
930         if (!aru)
931                 return -ENODEV;
932
933         usb_unpoison_anchored_urbs(&aru->rx_submitted);
934         usb_unpoison_anchored_urbs(&aru->tx_submitted);
935
936         err = ar9170_usb_init_device(aru);
937         if (err)
938                 goto err_unrx;
939
940         err = ar9170_usb_open(&aru->common);
941         if (err)
942                 goto err_unrx;
943
944         return 0;
945
946 err_unrx:
947         aru->common.state = AR9170_IDLE;
948         ar9170_usb_cancel_urbs(aru);
949
950         return err;
951 }
952 #endif /* CONFIG_PM */
953
954 static struct usb_driver ar9170_driver = {
955         .name = "ar9170usb",
956         .probe = ar9170_usb_probe,
957         .disconnect = ar9170_usb_disconnect,
958         .id_table = ar9170_usb_ids,
959         .soft_unbind = 1,
960 #ifdef CONFIG_PM
961         .suspend = ar9170_suspend,
962         .resume = ar9170_resume,
963         .reset_resume = ar9170_resume,
964 #endif /* CONFIG_PM */
965 };
966
967 static int __init ar9170_init(void)
968 {
969         return usb_register(&ar9170_driver);
970 }
971
972 static void __exit ar9170_exit(void)
973 {
974         usb_deregister(&ar9170_driver);
975 }
976
977 module_init(ar9170_init);
978 module_exit(ar9170_exit);