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