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