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