carl9170: fix sending URBs with wrong type when using full-speed
[pandora-kernel.git] / drivers / net / wireless / ath / carl9170 / usb.c
1 /*
2  * Atheros CARL9170 driver
3  *
4  * USB - frontend
5  *
6  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
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 "carl9170.h"
48 #include "cmd.h"
49 #include "hw.h"
50 #include "fwcmd.h"
51
52 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
53 MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>");
54 MODULE_LICENSE("GPL");
55 MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
56 MODULE_FIRMWARE(CARL9170FW_NAME);
57 MODULE_ALIAS("ar9170usb");
58 MODULE_ALIAS("arusb_lnx");
59
60 /*
61  * Note:
62  *
63  * Always update our wiki's device list (located at:
64  * http://wireless.kernel.org/en/users/Drivers/ar9170/devices ),
65  * whenever you add a new device.
66  */
67 static struct usb_device_id carl9170_usb_ids[] = {
68         /* Atheros 9170 */
69         { USB_DEVICE(0x0cf3, 0x9170) },
70         /* Atheros TG121N */
71         { USB_DEVICE(0x0cf3, 0x1001) },
72         /* TP-Link TL-WN821N v2 */
73         { USB_DEVICE(0x0cf3, 0x1002), .driver_info = CARL9170_WPS_BUTTON |
74                  CARL9170_ONE_LED },
75         /* 3Com Dual Band 802.11n USB Adapter */
76         { USB_DEVICE(0x0cf3, 0x1010) },
77         /* H3C Dual Band 802.11n USB Adapter */
78         { USB_DEVICE(0x0cf3, 0x1011) },
79         /* Cace Airpcap NX */
80         { USB_DEVICE(0xcace, 0x0300) },
81         /* D-Link DWA 160 A1 */
82         { USB_DEVICE(0x07d1, 0x3c10) },
83         /* D-Link DWA 160 A2 */
84         { USB_DEVICE(0x07d1, 0x3a09) },
85         /* D-Link DWA 130 D */
86         { USB_DEVICE(0x07d1, 0x3a0f) },
87         /* Netgear WNA1000 */
88         { USB_DEVICE(0x0846, 0x9040) },
89         /* Netgear WNDA3100 (v1) */
90         { USB_DEVICE(0x0846, 0x9010) },
91         /* Netgear WN111 v2 */
92         { USB_DEVICE(0x0846, 0x9001), .driver_info = CARL9170_ONE_LED },
93         /* Zydas ZD1221 */
94         { USB_DEVICE(0x0ace, 0x1221) },
95         /* Proxim ORiNOCO 802.11n USB */
96         { USB_DEVICE(0x1435, 0x0804) },
97         /* WNC Generic 11n USB Dongle */
98         { USB_DEVICE(0x1435, 0x0326) },
99         /* ZyXEL NWD271N */
100         { USB_DEVICE(0x0586, 0x3417) },
101         /* Z-Com UB81 BG */
102         { USB_DEVICE(0x0cde, 0x0023) },
103         /* Z-Com UB82 ABG */
104         { USB_DEVICE(0x0cde, 0x0026) },
105         /* Sphairon Homelink 1202 */
106         { USB_DEVICE(0x0cde, 0x0027) },
107         /* Arcadyan WN7512 */
108         { USB_DEVICE(0x083a, 0xf522) },
109         /* Planex GWUS300 */
110         { USB_DEVICE(0x2019, 0x5304) },
111         /* IO-Data WNGDNUS2 */
112         { USB_DEVICE(0x04bb, 0x093f) },
113         /* NEC WL300NU-G */
114         { USB_DEVICE(0x0409, 0x0249) },
115         /* NEC WL300NU-AG */
116         { USB_DEVICE(0x0409, 0x02b4) },
117         /* AVM FRITZ!WLAN USB Stick N */
118         { USB_DEVICE(0x057c, 0x8401) },
119         /* AVM FRITZ!WLAN USB Stick N 2.4 */
120         { USB_DEVICE(0x057c, 0x8402) },
121         /* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
122         { USB_DEVICE(0x1668, 0x1200) },
123         /* Airlive X.USB a/b/g/n */
124         { USB_DEVICE(0x1b75, 0x9170) },
125
126         /* terminate */
127         {}
128 };
129 MODULE_DEVICE_TABLE(usb, carl9170_usb_ids);
130
131 static void carl9170_usb_submit_data_urb(struct ar9170 *ar)
132 {
133         struct urb *urb;
134         int err;
135
136         if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS)
137                 goto err_acc;
138
139         urb = usb_get_from_anchor(&ar->tx_wait);
140         if (!urb)
141                 goto err_acc;
142
143         usb_anchor_urb(urb, &ar->tx_anch);
144
145         err = usb_submit_urb(urb, GFP_ATOMIC);
146         if (unlikely(err)) {
147                 if (net_ratelimit()) {
148                         dev_err(&ar->udev->dev, "tx submit failed (%d)\n",
149                                 urb->status);
150                 }
151
152                 usb_unanchor_urb(urb);
153                 usb_anchor_urb(urb, &ar->tx_err);
154         }
155
156         usb_free_urb(urb);
157
158         if (likely(err == 0))
159                 return;
160
161 err_acc:
162         atomic_dec(&ar->tx_anch_urbs);
163 }
164
165 static void carl9170_usb_tx_data_complete(struct urb *urb)
166 {
167         struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
168
169         if (WARN_ON_ONCE(!ar)) {
170                 dev_kfree_skb_irq(urb->context);
171                 return;
172         }
173
174         atomic_dec(&ar->tx_anch_urbs);
175
176         switch (urb->status) {
177         /* everything is fine */
178         case 0:
179                 carl9170_tx_callback(ar, (void *)urb->context);
180                 break;
181
182         /* disconnect */
183         case -ENOENT:
184         case -ECONNRESET:
185         case -ENODEV:
186         case -ESHUTDOWN:
187                 /*
188                  * Defer the frame clean-up to the tasklet worker.
189                  * This is necessary, because carl9170_tx_drop
190                  * does not work in an irqsave context.
191                  */
192                 usb_anchor_urb(urb, &ar->tx_err);
193                 return;
194
195         /* a random transmission error has occurred? */
196         default:
197                 if (net_ratelimit()) {
198                         dev_err(&ar->udev->dev, "tx failed (%d)\n",
199                                 urb->status);
200                 }
201
202                 usb_anchor_urb(urb, &ar->tx_err);
203                 break;
204         }
205
206         if (likely(IS_STARTED(ar)))
207                 carl9170_usb_submit_data_urb(ar);
208 }
209
210 static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
211 {
212         struct urb *urb;
213         int err;
214
215         if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
216                 atomic_dec(&ar->tx_cmd_urbs);
217                 return 0;
218         }
219
220         urb = usb_get_from_anchor(&ar->tx_cmd);
221         if (!urb) {
222                 atomic_dec(&ar->tx_cmd_urbs);
223                 return 0;
224         }
225
226         usb_anchor_urb(urb, &ar->tx_anch);
227         err = usb_submit_urb(urb, GFP_ATOMIC);
228         if (unlikely(err)) {
229                 usb_unanchor_urb(urb);
230                 atomic_dec(&ar->tx_cmd_urbs);
231         }
232         usb_free_urb(urb);
233
234         return err;
235 }
236
237 static void carl9170_usb_cmd_complete(struct urb *urb)
238 {
239         struct ar9170 *ar = urb->context;
240         int err = 0;
241
242         if (WARN_ON_ONCE(!ar))
243                 return;
244
245         atomic_dec(&ar->tx_cmd_urbs);
246
247         switch (urb->status) {
248         /* everything is fine */
249         case 0:
250                 break;
251
252         /* disconnect */
253         case -ENOENT:
254         case -ECONNRESET:
255         case -ENODEV:
256         case -ESHUTDOWN:
257                 return;
258
259         default:
260                 err = urb->status;
261                 break;
262         }
263
264         if (!IS_INITIALIZED(ar))
265                 return;
266
267         if (err)
268                 dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
269
270         err = carl9170_usb_submit_cmd_urb(ar);
271         if (err)
272                 dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
273 }
274
275 static void carl9170_usb_rx_irq_complete(struct urb *urb)
276 {
277         struct ar9170 *ar = urb->context;
278
279         if (WARN_ON_ONCE(!ar))
280                 return;
281
282         switch (urb->status) {
283         /* everything is fine */
284         case 0:
285                 break;
286
287         /* disconnect */
288         case -ENOENT:
289         case -ECONNRESET:
290         case -ENODEV:
291         case -ESHUTDOWN:
292                 return;
293
294         default:
295                 goto resubmit;
296         }
297
298         carl9170_handle_command_response(ar, urb->transfer_buffer,
299                                          urb->actual_length);
300
301 resubmit:
302         usb_anchor_urb(urb, &ar->rx_anch);
303         if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
304                 usb_unanchor_urb(urb);
305 }
306
307 static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
308 {
309         struct urb *urb;
310         int err = 0, runs = 0;
311
312         while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
313                 (runs++ < AR9170_NUM_RX_URBS)) {
314                 err = -ENOSPC;
315                 urb = usb_get_from_anchor(&ar->rx_pool);
316                 if (urb) {
317                         usb_anchor_urb(urb, &ar->rx_anch);
318                         err = usb_submit_urb(urb, gfp);
319                         if (unlikely(err)) {
320                                 usb_unanchor_urb(urb);
321                                 usb_anchor_urb(urb, &ar->rx_pool);
322                         } else {
323                                 atomic_dec(&ar->rx_pool_urbs);
324                                 atomic_inc(&ar->rx_anch_urbs);
325                         }
326                         usb_free_urb(urb);
327                 }
328         }
329
330         return err;
331 }
332
333 static void carl9170_usb_rx_work(struct ar9170 *ar)
334 {
335         struct urb *urb;
336         int i;
337
338         for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
339                 urb = usb_get_from_anchor(&ar->rx_work);
340                 if (!urb)
341                         break;
342
343                 atomic_dec(&ar->rx_work_urbs);
344                 if (IS_INITIALIZED(ar)) {
345                         carl9170_rx(ar, urb->transfer_buffer,
346                                     urb->actual_length);
347                 }
348
349                 usb_anchor_urb(urb, &ar->rx_pool);
350                 atomic_inc(&ar->rx_pool_urbs);
351
352                 usb_free_urb(urb);
353
354                 carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
355         }
356 }
357
358 void carl9170_usb_handle_tx_err(struct ar9170 *ar)
359 {
360         struct urb *urb;
361
362         while ((urb = usb_get_from_anchor(&ar->tx_err))) {
363                 struct sk_buff *skb = (void *)urb->context;
364
365                 carl9170_tx_drop(ar, skb);
366                 carl9170_tx_callback(ar, skb);
367                 usb_free_urb(urb);
368         }
369 }
370
371 static void carl9170_usb_tasklet(unsigned long data)
372 {
373         struct ar9170 *ar = (struct ar9170 *) data;
374
375         if (!IS_INITIALIZED(ar))
376                 return;
377
378         carl9170_usb_rx_work(ar);
379
380         /*
381          * Strictly speaking: The tx scheduler is not part of the USB system.
382          * But the rx worker returns frames back to the mac80211-stack and
383          * this is the _perfect_ place to generate the next transmissions.
384          */
385         if (IS_STARTED(ar))
386                 carl9170_tx_scheduler(ar);
387 }
388
389 static void carl9170_usb_rx_complete(struct urb *urb)
390 {
391         struct ar9170 *ar = (struct ar9170 *)urb->context;
392         int err;
393
394         if (WARN_ON_ONCE(!ar))
395                 return;
396
397         atomic_dec(&ar->rx_anch_urbs);
398
399         switch (urb->status) {
400         case 0:
401                 /* rx path */
402                 usb_anchor_urb(urb, &ar->rx_work);
403                 atomic_inc(&ar->rx_work_urbs);
404                 break;
405
406         case -ENOENT:
407         case -ECONNRESET:
408         case -ENODEV:
409         case -ESHUTDOWN:
410                 /* handle disconnect events*/
411                 return;
412
413         default:
414                 /* handle all other errors */
415                 usb_anchor_urb(urb, &ar->rx_pool);
416                 atomic_inc(&ar->rx_pool_urbs);
417                 break;
418         }
419
420         err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
421         if (unlikely(err)) {
422                 /*
423                  * usb_submit_rx_urb reported a problem.
424                  * In case this is due to a rx buffer shortage,
425                  * elevate the tasklet worker priority to
426                  * the highest available level.
427                  */
428                 tasklet_hi_schedule(&ar->usb_tasklet);
429
430                 if (atomic_read(&ar->rx_anch_urbs) == 0) {
431                         /*
432                          * The system is too slow to cope with
433                          * the enormous workload. We have simply
434                          * run out of active rx urbs and this
435                          * unfortunately leads to an unpredictable
436                          * device.
437                          */
438
439                         ieee80211_queue_work(ar->hw, &ar->ping_work);
440                 }
441         } else {
442                 /*
443                  * Using anything less than _high_ priority absolutely
444                  * kills the rx performance my UP-System...
445                  */
446                 tasklet_hi_schedule(&ar->usb_tasklet);
447         }
448 }
449
450 static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
451 {
452         struct urb *urb;
453         void *buf;
454
455         buf = kmalloc(ar->fw.rx_size, gfp);
456         if (!buf)
457                 return NULL;
458
459         urb = usb_alloc_urb(0, gfp);
460         if (!urb) {
461                 kfree(buf);
462                 return NULL;
463         }
464
465         usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev,
466                           AR9170_USB_EP_RX), buf, ar->fw.rx_size,
467                           carl9170_usb_rx_complete, ar);
468
469         urb->transfer_flags |= URB_FREE_BUFFER;
470
471         return urb;
472 }
473
474 static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
475 {
476         struct urb *urb = NULL;
477         void *ibuf;
478         int err = -ENOMEM;
479
480         urb = usb_alloc_urb(0, GFP_KERNEL);
481         if (!urb)
482                 goto out;
483
484         ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
485         if (!ibuf)
486                 goto out;
487
488         usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev,
489                          AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX,
490                          carl9170_usb_rx_irq_complete, ar, 1);
491
492         urb->transfer_flags |= URB_FREE_BUFFER;
493
494         usb_anchor_urb(urb, &ar->rx_anch);
495         err = usb_submit_urb(urb, GFP_KERNEL);
496         if (err)
497                 usb_unanchor_urb(urb);
498
499 out:
500         usb_free_urb(urb);
501         return err;
502 }
503
504 static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
505 {
506         struct urb *urb;
507         int i, err = -EINVAL;
508
509         /*
510          * The driver actively maintains a second shadow
511          * pool for inactive, but fully-prepared rx urbs.
512          *
513          * The pool should help the driver to master huge
514          * workload spikes without running the risk of
515          * undersupplying the hardware or wasting time by
516          * processing rx data (streams) inside the urb
517          * completion (hardirq context).
518          */
519         for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
520                 urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
521                 if (!urb) {
522                         err = -ENOMEM;
523                         goto err_out;
524                 }
525
526                 usb_anchor_urb(urb, &ar->rx_pool);
527                 atomic_inc(&ar->rx_pool_urbs);
528                 usb_free_urb(urb);
529         }
530
531         err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
532         if (err)
533                 goto err_out;
534
535         /* the device now waiting for the firmware. */
536         carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
537         return 0;
538
539 err_out:
540
541         usb_scuttle_anchored_urbs(&ar->rx_pool);
542         usb_scuttle_anchored_urbs(&ar->rx_work);
543         usb_kill_anchored_urbs(&ar->rx_anch);
544         return err;
545 }
546
547 static int carl9170_usb_flush(struct ar9170 *ar)
548 {
549         struct urb *urb;
550         int ret, err = 0;
551
552         while ((urb = usb_get_from_anchor(&ar->tx_wait))) {
553                 struct sk_buff *skb = (void *)urb->context;
554                 carl9170_tx_drop(ar, skb);
555                 carl9170_tx_callback(ar, skb);
556                 usb_free_urb(urb);
557         }
558
559         ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000);
560         if (ret == 0)
561                 err = -ETIMEDOUT;
562
563         /* lets wait a while until the tx - queues are dried out */
564         ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000);
565         if (ret == 0)
566                 err = -ETIMEDOUT;
567
568         usb_kill_anchored_urbs(&ar->tx_anch);
569         carl9170_usb_handle_tx_err(ar);
570
571         return err;
572 }
573
574 static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
575 {
576         int err;
577
578         carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
579
580         err = carl9170_usb_flush(ar);
581         if (err)
582                 dev_err(&ar->udev->dev, "stuck tx urbs!\n");
583
584         usb_poison_anchored_urbs(&ar->tx_anch);
585         carl9170_usb_handle_tx_err(ar);
586         usb_poison_anchored_urbs(&ar->rx_anch);
587
588         tasklet_kill(&ar->usb_tasklet);
589
590         usb_scuttle_anchored_urbs(&ar->rx_work);
591         usb_scuttle_anchored_urbs(&ar->rx_pool);
592         usb_scuttle_anchored_urbs(&ar->tx_cmd);
593 }
594
595 int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
596                         const bool free_buf)
597 {
598         struct urb *urb;
599         int err = 0;
600
601         if (!IS_INITIALIZED(ar)) {
602                 err = -EPERM;
603                 goto err_free;
604         }
605
606         if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) {
607                 err = -EINVAL;
608                 goto err_free;
609         }
610
611         urb = usb_alloc_urb(0, GFP_ATOMIC);
612         if (!urb) {
613                 err = -ENOMEM;
614                 goto err_free;
615         }
616
617         if (ar->usb_ep_cmd_is_bulk)
618                 usb_fill_bulk_urb(urb, ar->udev,
619                                   usb_sndbulkpipe(ar->udev, AR9170_USB_EP_CMD),
620                                   cmd, cmd->hdr.len + 4,
621                                   carl9170_usb_cmd_complete, ar);
622         else
623                 usb_fill_int_urb(urb, ar->udev,
624                                  usb_sndintpipe(ar->udev, AR9170_USB_EP_CMD),
625                                  cmd, cmd->hdr.len + 4,
626                                  carl9170_usb_cmd_complete, ar, 1);
627
628         if (free_buf)
629                 urb->transfer_flags |= URB_FREE_BUFFER;
630
631         usb_anchor_urb(urb, &ar->tx_cmd);
632         usb_free_urb(urb);
633
634         return carl9170_usb_submit_cmd_urb(ar);
635
636 err_free:
637         if (free_buf)
638                 kfree(cmd);
639
640         return err;
641 }
642
643 int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
644         unsigned int plen, void *payload, unsigned int outlen, void *out)
645 {
646         int err = -ENOMEM;
647
648         if (!IS_ACCEPTING_CMD(ar))
649                 return -EIO;
650
651         if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
652                 might_sleep();
653
654         ar->cmd.hdr.len = plen;
655         ar->cmd.hdr.cmd = cmd;
656         /* writing multiple regs fills this buffer already */
657         if (plen && payload != (u8 *)(ar->cmd.data))
658                 memcpy(ar->cmd.data, payload, plen);
659
660         spin_lock_bh(&ar->cmd_lock);
661         ar->readbuf = (u8 *)out;
662         ar->readlen = outlen;
663         spin_unlock_bh(&ar->cmd_lock);
664
665         err = __carl9170_exec_cmd(ar, &ar->cmd, false);
666
667         if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
668                 err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
669                 if (err == 0) {
670                         err = -ETIMEDOUT;
671                         goto err_unbuf;
672                 }
673
674                 if (ar->readlen != outlen) {
675                         err = -EMSGSIZE;
676                         goto err_unbuf;
677                 }
678         }
679
680         return 0;
681
682 err_unbuf:
683         /* Maybe the device was removed in the moment we were waiting? */
684         if (IS_STARTED(ar)) {
685                 dev_err(&ar->udev->dev, "no command feedback "
686                         "received (%d).\n", err);
687
688                 /* provide some maybe useful debug information */
689                 print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
690                                      &ar->cmd, plen + 4);
691
692                 carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
693         }
694
695         /* invalidate to avoid completing the next command prematurely */
696         spin_lock_bh(&ar->cmd_lock);
697         ar->readbuf = NULL;
698         ar->readlen = 0;
699         spin_unlock_bh(&ar->cmd_lock);
700
701         return err;
702 }
703
704 void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
705 {
706         struct urb *urb;
707         struct ar9170_stream *tx_stream;
708         void *data;
709         unsigned int len;
710
711         if (!IS_STARTED(ar))
712                 goto err_drop;
713
714         urb = usb_alloc_urb(0, GFP_ATOMIC);
715         if (!urb)
716                 goto err_drop;
717
718         if (ar->fw.tx_stream) {
719                 tx_stream = (void *) (skb->data - sizeof(*tx_stream));
720
721                 len = skb->len + sizeof(*tx_stream);
722                 tx_stream->length = cpu_to_le16(len);
723                 tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
724                 data = tx_stream;
725         } else {
726                 data = skb->data;
727                 len = skb->len;
728         }
729
730         usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
731                 AR9170_USB_EP_TX), data, len,
732                 carl9170_usb_tx_data_complete, skb);
733
734         urb->transfer_flags |= URB_ZERO_PACKET;
735
736         usb_anchor_urb(urb, &ar->tx_wait);
737
738         usb_free_urb(urb);
739
740         carl9170_usb_submit_data_urb(ar);
741         return;
742
743 err_drop:
744         carl9170_tx_drop(ar, skb);
745         carl9170_tx_callback(ar, skb);
746 }
747
748 static void carl9170_release_firmware(struct ar9170 *ar)
749 {
750         if (ar->fw.fw) {
751                 release_firmware(ar->fw.fw);
752                 memset(&ar->fw, 0, sizeof(ar->fw));
753         }
754 }
755
756 void carl9170_usb_stop(struct ar9170 *ar)
757 {
758         int ret;
759
760         carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
761
762         ret = carl9170_usb_flush(ar);
763         if (ret)
764                 dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
765
766         usb_poison_anchored_urbs(&ar->tx_anch);
767         carl9170_usb_handle_tx_err(ar);
768
769         /* kill any pending command */
770         spin_lock_bh(&ar->cmd_lock);
771         ar->readlen = 0;
772         spin_unlock_bh(&ar->cmd_lock);
773         complete_all(&ar->cmd_wait);
774
775         /* This is required to prevent an early completion on _start */
776         INIT_COMPLETION(ar->cmd_wait);
777
778         /*
779          * Note:
780          * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
781          * Else we would end up with a unresponsive device...
782          */
783 }
784
785 int carl9170_usb_open(struct ar9170 *ar)
786 {
787         usb_unpoison_anchored_urbs(&ar->tx_anch);
788
789         carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
790         return 0;
791 }
792
793 static int carl9170_usb_load_firmware(struct ar9170 *ar)
794 {
795         const u8 *data;
796         u8 *buf;
797         unsigned int transfer;
798         size_t len;
799         u32 addr;
800         int err = 0;
801
802         buf = kmalloc(4096, GFP_KERNEL);
803         if (!buf) {
804                 err = -ENOMEM;
805                 goto err_out;
806         }
807
808         data = ar->fw.fw->data;
809         len = ar->fw.fw->size;
810         addr = ar->fw.address;
811
812         /* this removes the miniboot image */
813         data += ar->fw.offset;
814         len -= ar->fw.offset;
815
816         while (len) {
817                 transfer = min_t(unsigned int, len, 4096u);
818                 memcpy(buf, data, transfer);
819
820                 err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
821                                       0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
822                                       addr >> 8, 0, buf, transfer, 100);
823
824                 if (err < 0) {
825                         kfree(buf);
826                         goto err_out;
827                 }
828
829                 len -= transfer;
830                 data += transfer;
831                 addr += transfer;
832         }
833         kfree(buf);
834
835         err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
836                               0x31 /* FW DL COMPLETE */,
837                               0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
838
839         if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
840                 err = -ETIMEDOUT;
841                 goto err_out;
842         }
843
844         err = carl9170_echo_test(ar, 0x4a110123);
845         if (err)
846                 goto err_out;
847
848         /* now, start the command response counter */
849         ar->cmd_seq = -1;
850
851         return 0;
852
853 err_out:
854         dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
855         return err;
856 }
857
858 int carl9170_usb_restart(struct ar9170 *ar)
859 {
860         int err = 0;
861
862         if (ar->intf->condition != USB_INTERFACE_BOUND)
863                 return 0;
864
865         /*
866          * Disable the command response sequence counter check.
867          * We already know that the device/firmware is in a bad state.
868          * So, no extra points are awarded to anyone who reminds the
869          * driver about that.
870          */
871         ar->cmd_seq = -2;
872
873         err = carl9170_reboot(ar);
874
875         carl9170_usb_stop(ar);
876
877         if (err)
878                 goto err_out;
879
880         tasklet_schedule(&ar->usb_tasklet);
881
882         /* The reboot procedure can take quite a while to complete. */
883         msleep(1100);
884
885         err = carl9170_usb_open(ar);
886         if (err)
887                 goto err_out;
888
889         err = carl9170_usb_load_firmware(ar);
890         if (err)
891                 goto err_out;
892
893         return 0;
894
895 err_out:
896         carl9170_usb_cancel_urbs(ar);
897         return err;
898 }
899
900 void carl9170_usb_reset(struct ar9170 *ar)
901 {
902         /*
903          * This is the last resort to get the device going again
904          * without any *user replugging action*.
905          *
906          * But there is a catch: usb_reset really is like a physical
907          * *reconnect*. The mac80211 state will be lost in the process.
908          * Therefore a userspace application, which is monitoring
909          * the link must step in.
910          */
911         carl9170_usb_cancel_urbs(ar);
912
913         carl9170_usb_stop(ar);
914
915         usb_queue_reset_device(ar->intf);
916 }
917
918 static int carl9170_usb_init_device(struct ar9170 *ar)
919 {
920         int err;
921
922         /*
923          * The carl9170 firmware let's the driver know when it's
924          * ready for action. But we have to be prepared to gracefully
925          * handle all spurious [flushed] messages after each (re-)boot.
926          * Thus the command response counter remains disabled until it
927          * can be safely synchronized.
928          */
929         ar->cmd_seq = -2;
930
931         err = carl9170_usb_send_rx_irq_urb(ar);
932         if (err)
933                 goto err_out;
934
935         err = carl9170_usb_init_rx_bulk_urbs(ar);
936         if (err)
937                 goto err_unrx;
938
939         err = carl9170_usb_open(ar);
940         if (err)
941                 goto err_unrx;
942
943         mutex_lock(&ar->mutex);
944         err = carl9170_usb_load_firmware(ar);
945         mutex_unlock(&ar->mutex);
946         if (err)
947                 goto err_stop;
948
949         return 0;
950
951 err_stop:
952         carl9170_usb_stop(ar);
953
954 err_unrx:
955         carl9170_usb_cancel_urbs(ar);
956
957 err_out:
958         return err;
959 }
960
961 static void carl9170_usb_firmware_failed(struct ar9170 *ar)
962 {
963         struct device *parent = ar->udev->dev.parent;
964         struct usb_device *udev;
965
966         /*
967          * Store a copy of the usb_device pointer locally.
968          * This is because device_release_driver initiates
969          * carl9170_usb_disconnect, which in turn frees our
970          * driver context (ar).
971          */
972         udev = ar->udev;
973
974         complete(&ar->fw_load_wait);
975
976         /* unbind anything failed */
977         if (parent)
978                 device_lock(parent);
979
980         device_release_driver(&udev->dev);
981         if (parent)
982                 device_unlock(parent);
983
984         usb_put_dev(udev);
985 }
986
987 static void carl9170_usb_firmware_finish(struct ar9170 *ar)
988 {
989         int err;
990
991         err = carl9170_parse_firmware(ar);
992         if (err)
993                 goto err_freefw;
994
995         err = carl9170_usb_init_device(ar);
996         if (err)
997                 goto err_freefw;
998
999         err = carl9170_register(ar);
1000
1001         carl9170_usb_stop(ar);
1002         if (err)
1003                 goto err_unrx;
1004
1005         complete(&ar->fw_load_wait);
1006         usb_put_dev(ar->udev);
1007         return;
1008
1009 err_unrx:
1010         carl9170_usb_cancel_urbs(ar);
1011
1012 err_freefw:
1013         carl9170_release_firmware(ar);
1014         carl9170_usb_firmware_failed(ar);
1015 }
1016
1017 static void carl9170_usb_firmware_step2(const struct firmware *fw,
1018                                         void *context)
1019 {
1020         struct ar9170 *ar = context;
1021
1022         if (fw) {
1023                 ar->fw.fw = fw;
1024                 carl9170_usb_firmware_finish(ar);
1025                 return;
1026         }
1027
1028         dev_err(&ar->udev->dev, "firmware not found.\n");
1029         carl9170_usb_firmware_failed(ar);
1030 }
1031
1032 static int carl9170_usb_probe(struct usb_interface *intf,
1033                               const struct usb_device_id *id)
1034 {
1035         struct usb_endpoint_descriptor *ep;
1036         struct ar9170 *ar;
1037         struct usb_device *udev;
1038         int i, err;
1039
1040         err = usb_reset_device(interface_to_usbdev(intf));
1041         if (err)
1042                 return err;
1043
1044         ar = carl9170_alloc(sizeof(*ar));
1045         if (IS_ERR(ar))
1046                 return PTR_ERR(ar);
1047
1048         udev = interface_to_usbdev(intf);
1049         usb_get_dev(udev);
1050         ar->udev = udev;
1051         ar->intf = intf;
1052         ar->features = id->driver_info;
1053
1054         /* We need to remember the type of endpoint 4 because it differs
1055          * between high- and full-speed configuration. The high-speed
1056          * configuration specifies it as interrupt and the full-speed
1057          * configuration as bulk endpoint. This information is required
1058          * later when sending urbs to that endpoint.
1059          */
1060         for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; ++i) {
1061                 ep = &intf->cur_altsetting->endpoint[i].desc;
1062
1063                 if (usb_endpoint_num(ep) == AR9170_USB_EP_CMD &&
1064                     usb_endpoint_dir_out(ep) &&
1065                     usb_endpoint_type(ep) == USB_ENDPOINT_XFER_BULK)
1066                         ar->usb_ep_cmd_is_bulk = true;
1067         }
1068
1069         usb_set_intfdata(intf, ar);
1070         SET_IEEE80211_DEV(ar->hw, &intf->dev);
1071
1072         init_usb_anchor(&ar->rx_anch);
1073         init_usb_anchor(&ar->rx_pool);
1074         init_usb_anchor(&ar->rx_work);
1075         init_usb_anchor(&ar->tx_wait);
1076         init_usb_anchor(&ar->tx_anch);
1077         init_usb_anchor(&ar->tx_cmd);
1078         init_usb_anchor(&ar->tx_err);
1079         init_completion(&ar->cmd_wait);
1080         init_completion(&ar->fw_boot_wait);
1081         init_completion(&ar->fw_load_wait);
1082         tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet,
1083                      (unsigned long)ar);
1084
1085         atomic_set(&ar->tx_cmd_urbs, 0);
1086         atomic_set(&ar->tx_anch_urbs, 0);
1087         atomic_set(&ar->rx_work_urbs, 0);
1088         atomic_set(&ar->rx_anch_urbs, 0);
1089         atomic_set(&ar->rx_pool_urbs, 0);
1090
1091         usb_get_dev(ar->udev);
1092
1093         carl9170_set_state(ar, CARL9170_STOPPED);
1094
1095         return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
1096                 &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
1097 }
1098
1099 static void carl9170_usb_disconnect(struct usb_interface *intf)
1100 {
1101         struct ar9170 *ar = usb_get_intfdata(intf);
1102         struct usb_device *udev;
1103
1104         if (WARN_ON(!ar))
1105                 return;
1106
1107         udev = ar->udev;
1108         wait_for_completion(&ar->fw_load_wait);
1109
1110         if (IS_INITIALIZED(ar)) {
1111                 carl9170_reboot(ar);
1112                 carl9170_usb_stop(ar);
1113         }
1114
1115         carl9170_usb_cancel_urbs(ar);
1116         carl9170_unregister(ar);
1117
1118         usb_set_intfdata(intf, NULL);
1119
1120         carl9170_release_firmware(ar);
1121         carl9170_free(ar);
1122         usb_put_dev(udev);
1123 }
1124
1125 #ifdef CONFIG_PM
1126 static int carl9170_usb_suspend(struct usb_interface *intf,
1127                                 pm_message_t message)
1128 {
1129         struct ar9170 *ar = usb_get_intfdata(intf);
1130
1131         if (!ar)
1132                 return -ENODEV;
1133
1134         carl9170_usb_cancel_urbs(ar);
1135
1136         return 0;
1137 }
1138
1139 static int carl9170_usb_resume(struct usb_interface *intf)
1140 {
1141         struct ar9170 *ar = usb_get_intfdata(intf);
1142         int err;
1143
1144         if (!ar)
1145                 return -ENODEV;
1146
1147         usb_unpoison_anchored_urbs(&ar->rx_anch);
1148         carl9170_set_state(ar, CARL9170_STOPPED);
1149
1150         /*
1151          * The USB documentation demands that [for suspend] all traffic
1152          * to and from the device has to stop. This would be fine, but
1153          * there's a catch: the device[usb phy] does not come back.
1154          *
1155          * Upon resume the firmware will "kill" itself and the
1156          * boot-code sorts out the magic voodoo.
1157          * Not very nice, but there's not much what could go wrong.
1158          */
1159         msleep(1100);
1160
1161         err = carl9170_usb_init_device(ar);
1162         if (err)
1163                 goto err_unrx;
1164
1165         return 0;
1166
1167 err_unrx:
1168         carl9170_usb_cancel_urbs(ar);
1169
1170         return err;
1171 }
1172 #endif /* CONFIG_PM */
1173
1174 static struct usb_driver carl9170_driver = {
1175         .name = KBUILD_MODNAME,
1176         .probe = carl9170_usb_probe,
1177         .disconnect = carl9170_usb_disconnect,
1178         .id_table = carl9170_usb_ids,
1179         .soft_unbind = 1,
1180 #ifdef CONFIG_PM
1181         .suspend = carl9170_usb_suspend,
1182         .resume = carl9170_usb_resume,
1183         .reset_resume = carl9170_usb_resume,
1184 #endif /* CONFIG_PM */
1185 };
1186
1187 static int __init carl9170_usb_init(void)
1188 {
1189         return usb_register(&carl9170_driver);
1190 }
1191
1192 static void __exit carl9170_usb_exit(void)
1193 {
1194         usb_deregister(&carl9170_driver);
1195 }
1196
1197 module_init(carl9170_usb_init);
1198 module_exit(carl9170_usb_exit);