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