blob: 71579e4d2fce6b8d6f608746d515aa8f40afce51 [file] [log] [blame]
Christian Lamparterb63a2cb2009-03-21 23:05:48 +01001/*
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
Christian Lamparterde00c042009-03-24 16:21:55 +010050MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
Christian Lamparterb63a2cb2009-03-21 23:05:48 +010051MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
52MODULE_LICENSE("GPL");
Christian Lamparterde00c042009-03-24 16:21:55 +010053MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
Luis R. Rodriguezc768b582009-05-28 17:36:04 -040054MODULE_FIRMWARE("ar9170.fw");
Christian Lamparterb63a2cb2009-03-21 23:05:48 +010055MODULE_FIRMWARE("ar9170-1.fw");
56MODULE_FIRMWARE("ar9170-2.fw");
57
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -040058enum ar9170_requirements {
59 AR9170_REQ_FW1_ONLY = 1,
60};
61
Christian Lamparterb63a2cb2009-03-21 23:05:48 +010062static struct usb_device_id ar9170_usb_ids[] = {
63 /* Atheros 9170 */
64 { USB_DEVICE(0x0cf3, 0x9170) },
65 /* Atheros TG121N */
66 { USB_DEVICE(0x0cf3, 0x1001) },
Christian Lamparter4fc298b2009-03-24 21:58:08 +010067 /* Cace Airpcap NX */
68 { USB_DEVICE(0xcace, 0x0300) },
Christian Lamparterb63a2cb2009-03-21 23:05:48 +010069 /* D-Link DWA 160A */
70 { USB_DEVICE(0x07d1, 0x3c10) },
71 /* Netgear WNDA3100 */
72 { USB_DEVICE(0x0846, 0x9010) },
73 /* Netgear WN111 v2 */
74 { USB_DEVICE(0x0846, 0x9001) },
75 /* Zydas ZD1221 */
76 { USB_DEVICE(0x0ace, 0x1221) },
Christian Lamparter957b0512009-04-17 14:52:23 +020077 /* ZyXEL NWD271N */
78 { USB_DEVICE(0x0586, 0x3417) },
Christian Lamparterb63a2cb2009-03-21 23:05:48 +010079 /* Z-Com UB81 BG */
80 { USB_DEVICE(0x0cde, 0x0023) },
81 /* Z-Com UB82 ABG */
82 { USB_DEVICE(0x0cde, 0x0026) },
83 /* Arcadyan WN7512 */
84 { USB_DEVICE(0x083a, 0xf522) },
85 /* Planex GWUS300 */
86 { USB_DEVICE(0x2019, 0x5304) },
87 /* IO-Data WNGDNUS2 */
88 { USB_DEVICE(0x04bb, 0x093f) },
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -040089 /* AVM FRITZ!WLAN USB Stick N */
90 { USB_DEVICE(0x057C, 0x8401) },
91 /* AVM FRITZ!WLAN USB Stick N 2.4 */
92 { USB_DEVICE(0x057C, 0x8402), .driver_info = AR9170_REQ_FW1_ONLY },
Christian Lamparterb63a2cb2009-03-21 23:05:48 +010093
94 /* terminate */
95 {}
96};
97MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
98
99static void ar9170_usb_tx_urb_complete_free(struct urb *urb)
100{
101 struct sk_buff *skb = urb->context;
102 struct ar9170_usb *aru = (struct ar9170_usb *)
103 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
104
105 if (!aru) {
106 dev_kfree_skb_irq(skb);
107 return ;
108 }
109
110 ar9170_handle_tx_status(&aru->common, skb, false,
111 AR9170_TX_STATUS_COMPLETE);
112}
113
114static void ar9170_usb_tx_urb_complete(struct urb *urb)
115{
116}
117
118static void ar9170_usb_irq_completed(struct urb *urb)
119{
120 struct ar9170_usb *aru = urb->context;
121
122 switch (urb->status) {
123 /* everything is fine */
124 case 0:
125 break;
126
127 /* disconnect */
128 case -ENOENT:
129 case -ECONNRESET:
130 case -ENODEV:
131 case -ESHUTDOWN:
132 goto free;
133
134 default:
135 goto resubmit;
136 }
137
138 print_hex_dump_bytes("ar9170 irq: ", DUMP_PREFIX_OFFSET,
139 urb->transfer_buffer, urb->actual_length);
140
141resubmit:
142 usb_anchor_urb(urb, &aru->rx_submitted);
143 if (usb_submit_urb(urb, GFP_ATOMIC)) {
144 usb_unanchor_urb(urb);
145 goto free;
146 }
147
148 return;
149
150free:
151 usb_buffer_free(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
152}
153
154static void ar9170_usb_rx_completed(struct urb *urb)
155{
156 struct sk_buff *skb = urb->context;
157 struct ar9170_usb *aru = (struct ar9170_usb *)
158 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
159 int err;
160
161 if (!aru)
162 goto free;
163
164 switch (urb->status) {
165 /* everything is fine */
166 case 0:
167 break;
168
169 /* disconnect */
170 case -ENOENT:
171 case -ECONNRESET:
172 case -ENODEV:
173 case -ESHUTDOWN:
174 goto free;
175
176 default:
177 goto resubmit;
178 }
179
180 skb_put(skb, urb->actual_length);
181 ar9170_rx(&aru->common, skb);
182
183resubmit:
184 skb_reset_tail_pointer(skb);
185 skb_trim(skb, 0);
186
187 usb_anchor_urb(urb, &aru->rx_submitted);
188 err = usb_submit_urb(urb, GFP_ATOMIC);
189 if (err) {
190 usb_unanchor_urb(urb);
191 dev_kfree_skb_irq(skb);
192 }
193
194 return ;
195
196free:
197 dev_kfree_skb_irq(skb);
198 return;
199}
200
201static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
202 struct urb *urb, gfp_t gfp)
203{
204 struct sk_buff *skb;
205
206 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
207 if (!skb)
208 return -ENOMEM;
209
210 /* reserve some space for mac80211's radiotap */
211 skb_reserve(skb, 32);
212
213 usb_fill_bulk_urb(urb, aru->udev,
214 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
215 skb->data, min(skb_tailroom(skb),
216 AR9170_MAX_RX_BUFFER_SIZE),
217 ar9170_usb_rx_completed, skb);
218
219 return 0;
220}
221
222static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
223{
224 struct urb *urb = NULL;
225 void *ibuf;
226 int err = -ENOMEM;
227
228 /* initialize interrupt endpoint */
229 urb = usb_alloc_urb(0, GFP_KERNEL);
230 if (!urb)
231 goto out;
232
233 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
234 if (!ibuf)
235 goto out;
236
237 usb_fill_int_urb(urb, aru->udev,
238 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
239 64, ar9170_usb_irq_completed, aru, 1);
240 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
241
242 usb_anchor_urb(urb, &aru->rx_submitted);
243 err = usb_submit_urb(urb, GFP_KERNEL);
244 if (err) {
245 usb_unanchor_urb(urb);
246 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
247 urb->transfer_dma);
248 }
249
250out:
251 usb_free_urb(urb);
252 return err;
253}
254
255static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
256{
257 struct urb *urb;
258 int i;
259 int err = -EINVAL;
260
261 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
262 err = -ENOMEM;
263 urb = usb_alloc_urb(0, GFP_KERNEL);
264 if (!urb)
265 goto err_out;
266
267 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
268 if (err) {
269 usb_free_urb(urb);
270 goto err_out;
271 }
272
273 usb_anchor_urb(urb, &aru->rx_submitted);
274 err = usb_submit_urb(urb, GFP_KERNEL);
275 if (err) {
276 usb_unanchor_urb(urb);
277 dev_kfree_skb_any((void *) urb->transfer_buffer);
278 usb_free_urb(urb);
279 goto err_out;
280 }
281 usb_free_urb(urb);
282 }
283
284 /* the device now waiting for a firmware. */
285 aru->common.state = AR9170_IDLE;
286 return 0;
287
288err_out:
289
290 usb_kill_anchored_urbs(&aru->rx_submitted);
291 return err;
292}
293
294static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
295{
296 int ret;
297
298 aru->common.state = AR9170_UNKNOWN_STATE;
299
300 usb_unlink_anchored_urbs(&aru->tx_submitted);
301
302 /* give the LED OFF command and the deauth frame a chance to air. */
303 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
304 msecs_to_jiffies(100));
305 if (ret == 0)
306 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
307 usb_poison_anchored_urbs(&aru->tx_submitted);
308
309 usb_poison_anchored_urbs(&aru->rx_submitted);
310}
311
312static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
313 unsigned int plen, void *payload,
314 unsigned int outlen, void *out)
315{
316 struct ar9170_usb *aru = (void *) ar;
317 struct urb *urb = NULL;
318 unsigned long flags;
319 int err = -ENOMEM;
320
321 if (unlikely(!IS_ACCEPTING_CMD(ar)))
322 return -EPERM;
323
324 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
325 return -EINVAL;
326
327 urb = usb_alloc_urb(0, GFP_ATOMIC);
328 if (unlikely(!urb))
329 goto err_free;
330
331 ar->cmdbuf[0] = cpu_to_le32(plen);
332 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
333 /* writing multiple regs fills this buffer already */
334 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
335 memcpy(&ar->cmdbuf[1], payload, plen);
336
337 spin_lock_irqsave(&aru->common.cmdlock, flags);
338 aru->readbuf = (u8 *)out;
339 aru->readlen = outlen;
340 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
341
342 usb_fill_int_urb(urb, aru->udev,
343 usb_sndbulkpipe(aru->udev, AR9170_EP_CMD),
344 aru->common.cmdbuf, plen + 4,
345 ar9170_usb_tx_urb_complete, NULL, 1);
346
347 usb_anchor_urb(urb, &aru->tx_submitted);
348 err = usb_submit_urb(urb, GFP_ATOMIC);
349 if (err) {
350 usb_unanchor_urb(urb);
351 usb_free_urb(urb);
352 goto err_unbuf;
353 }
354 usb_free_urb(urb);
355
356 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
357 if (err == 0) {
358 err = -ETIMEDOUT;
359 goto err_unbuf;
360 }
361
Roel Kluinba5101d2009-04-26 14:40:59 +0200362 if (aru->readlen != outlen) {
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100363 err = -EMSGSIZE;
364 goto err_unbuf;
365 }
366
367 return 0;
368
369err_unbuf:
370 /* Maybe the device was removed in the second we were waiting? */
371 if (IS_STARTED(ar)) {
372 dev_err(&aru->udev->dev, "no command feedback "
373 "received (%d).\n", err);
374
375 /* provide some maybe useful debug information */
376 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
377 aru->common.cmdbuf, plen + 4);
378 dump_stack();
379 }
380
381 /* invalidate to avoid completing the next prematurely */
382 spin_lock_irqsave(&aru->common.cmdlock, flags);
383 aru->readbuf = NULL;
384 aru->readlen = 0;
385 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
386
387err_free:
388
389 return err;
390}
391
392static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb,
393 bool txstatus_needed, unsigned int extra_len)
394{
395 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
396 struct urb *urb;
397 int err;
398
399 if (unlikely(!IS_STARTED(ar))) {
400 /* Seriously, what were you drink... err... thinking!? */
401 return -EPERM;
402 }
403
404 urb = usb_alloc_urb(0, GFP_ATOMIC);
405 if (unlikely(!urb))
406 return -ENOMEM;
407
408 usb_fill_bulk_urb(urb, aru->udev,
409 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
410 skb->data, skb->len + extra_len, (txstatus_needed ?
411 ar9170_usb_tx_urb_complete :
412 ar9170_usb_tx_urb_complete_free), skb);
413 urb->transfer_flags |= URB_ZERO_PACKET;
414
415 usb_anchor_urb(urb, &aru->tx_submitted);
416 err = usb_submit_urb(urb, GFP_ATOMIC);
417 if (unlikely(err))
418 usb_unanchor_urb(urb);
419
420 usb_free_urb(urb);
421 return err;
422}
423
424static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
425{
426 struct ar9170_usb *aru = (void *) ar;
427 unsigned long flags;
428 u32 in, out;
429
430 if (!buffer)
431 return ;
432
433 in = le32_to_cpup((__le32 *)buffer);
434 out = le32_to_cpu(ar->cmdbuf[0]);
435
436 /* mask off length byte */
437 out &= ~0xFF;
438
439 if (aru->readlen >= 0) {
440 /* add expected length */
441 out |= aru->readlen;
442 } else {
443 /* add obtained length */
444 out |= in & 0xFF;
445 }
446
447 /*
448 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
449 * length and we cannot predict the correct length in advance.
450 * So we only check if we provided enough space for the data.
451 */
452 if (unlikely(out < in)) {
453 dev_warn(&aru->udev->dev, "received invalid command response "
454 "got %d bytes, instead of %d bytes "
455 "and the resp length is %d bytes\n",
456 in, out, len);
457 print_hex_dump_bytes("ar9170 invalid resp: ",
458 DUMP_PREFIX_OFFSET, buffer, len);
459 /*
460 * Do not complete, then the command times out,
461 * and we get a stack trace from there.
462 */
463 return ;
464 }
465
466 spin_lock_irqsave(&aru->common.cmdlock, flags);
467 if (aru->readbuf && len > 0) {
468 memcpy(aru->readbuf, buffer + 4, len - 4);
469 aru->readbuf = NULL;
470 }
471 complete(&aru->cmd_wait);
472 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
473}
474
475static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
476 size_t len, u32 addr, bool complete)
477{
478 int transfer, err;
479 u8 *buf = kmalloc(4096, GFP_KERNEL);
480
481 if (!buf)
482 return -ENOMEM;
483
484 while (len) {
485 transfer = min_t(int, len, 4096);
486 memcpy(buf, data, transfer);
487
488 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
489 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
490 addr >> 8, 0, buf, transfer, 1000);
491
492 if (err < 0) {
493 kfree(buf);
494 return err;
495 }
496
497 len -= transfer;
498 data += transfer;
499 addr += transfer;
500 }
501 kfree(buf);
502
503 if (complete) {
504 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
505 0x31 /* FW DL COMPLETE */,
506 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
507 }
508
509 return 0;
510}
511
512static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
513{
514 int err = 0;
515
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400516 err = request_firmware(&aru->firmware, "ar9170.fw",
517 &aru->udev->dev);
518 if (!err) {
519 aru->init_values = NULL;
520 return 0;
521 }
522
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -0400523 if (aru->req_one_stage_fw) {
524 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
525 "not found and is required for this device\n");
526 return -EINVAL;
527 }
528
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400529 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
530 "not found, trying old firmware...\n");
531
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100532 err = request_firmware(&aru->init_values, "ar9170-1.fw",
533 &aru->udev->dev);
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100534
535 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
536 if (err) {
537 release_firmware(aru->init_values);
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400538 dev_err(&aru->udev->dev, "file with init values not found.\n");
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100539 return err;
540 }
541
542 return err;
543}
544
545static int ar9170_usb_reset(struct ar9170_usb *aru)
546{
547 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
548
549 if (lock) {
550 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
551 if (ret < 0) {
552 dev_err(&aru->udev->dev, "unable to lock device "
553 "for reset (%d).\n", ret);
554 return ret;
555 }
556 }
557
558 ret = usb_reset_device(aru->udev);
559 if (lock)
560 usb_unlock_device(aru->udev);
561
562 /* let it rest - for a second - */
563 msleep(1000);
564
565 return ret;
566}
567
568static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
569{
570 int err;
571
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400572 if (!aru->init_values)
573 goto upload_fw_start;
574
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100575 /* First, upload initial values to device RAM */
576 err = ar9170_usb_upload(aru, aru->init_values->data,
577 aru->init_values->size, 0x102800, false);
578 if (err) {
579 dev_err(&aru->udev->dev, "firmware part 1 "
580 "upload failed (%d).\n", err);
581 return err;
582 }
583
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400584upload_fw_start:
585
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100586 /* Then, upload the firmware itself and start it */
587 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
588 0x200000, true);
589}
590
591static int ar9170_usb_init_transport(struct ar9170_usb *aru)
592{
593 struct ar9170 *ar = (void *) &aru->common;
594 int err;
595
596 ar9170_regwrite_begin(ar);
597
598 /* Set USB Rx stream mode MAX packet number to 2 */
599 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
600
601 /* Set USB Rx stream mode timeout to 10us */
602 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
603
604 ar9170_regwrite_finish();
605
606 err = ar9170_regwrite_result();
607 if (err)
608 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
609
610 return err;
611}
612
613static void ar9170_usb_stop(struct ar9170 *ar)
614{
615 struct ar9170_usb *aru = (void *) ar;
616 int ret;
617
618 if (IS_ACCEPTING_CMD(ar))
619 aru->common.state = AR9170_STOPPED;
620
621 /* lets wait a while until the tx - queues are dried out */
622 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
623 msecs_to_jiffies(1000));
624 if (ret == 0)
625 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
626
627 usb_poison_anchored_urbs(&aru->tx_submitted);
628
629 /*
630 * Note:
631 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
632 * Else we would end up with a unresponsive device...
633 */
634}
635
636static int ar9170_usb_open(struct ar9170 *ar)
637{
638 struct ar9170_usb *aru = (void *) ar;
639 int err;
640
641 usb_unpoison_anchored_urbs(&aru->tx_submitted);
642 err = ar9170_usb_init_transport(aru);
643 if (err) {
644 usb_poison_anchored_urbs(&aru->tx_submitted);
645 return err;
646 }
647
648 aru->common.state = AR9170_IDLE;
649 return 0;
650}
651
Christian Lampartere10a9df2009-04-18 17:12:18 +0200652static int ar9170_usb_init_device(struct ar9170_usb *aru)
653{
654 int err;
655
656 err = ar9170_usb_alloc_rx_irq_urb(aru);
657 if (err)
658 goto err_out;
659
660 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
661 if (err)
662 goto err_unrx;
663
664 err = ar9170_usb_upload_firmware(aru);
665 if (err) {
666 err = ar9170_echo_test(&aru->common, 0x60d43110);
667 if (err) {
668 /* force user invention, by disabling the device */
669 err = usb_driver_set_configuration(aru->udev, -1);
670 dev_err(&aru->udev->dev, "device is in a bad state. "
671 "please reconnect it!\n");
672 goto err_unrx;
673 }
674 }
675
676 return 0;
677
678err_unrx:
679 ar9170_usb_cancel_urbs(aru);
680
681err_out:
682 return err;
683}
684
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -0400685static bool ar9170_requires_one_stage(const struct usb_device_id *id)
686{
687 if (!id->driver_info)
688 return false;
689 if (id->driver_info == AR9170_REQ_FW1_ONLY)
690 return true;
691 return false;
692}
693
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100694static int ar9170_usb_probe(struct usb_interface *intf,
695 const struct usb_device_id *id)
696{
697 struct ar9170_usb *aru;
698 struct ar9170 *ar;
699 struct usb_device *udev;
700 int err;
701
702 aru = ar9170_alloc(sizeof(*aru));
703 if (IS_ERR(aru)) {
704 err = PTR_ERR(aru);
705 goto out;
706 }
707
708 udev = interface_to_usbdev(intf);
709 usb_get_dev(udev);
710 aru->udev = udev;
711 aru->intf = intf;
712 ar = &aru->common;
713
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -0400714 aru->req_one_stage_fw = ar9170_requires_one_stage(id);
715
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100716 usb_set_intfdata(intf, aru);
717 SET_IEEE80211_DEV(ar->hw, &udev->dev);
718
719 init_usb_anchor(&aru->rx_submitted);
720 init_usb_anchor(&aru->tx_submitted);
721 init_completion(&aru->cmd_wait);
722
723 aru->common.stop = ar9170_usb_stop;
724 aru->common.open = ar9170_usb_open;
725 aru->common.tx = ar9170_usb_tx;
726 aru->common.exec_cmd = ar9170_usb_exec_cmd;
727 aru->common.callback_cmd = ar9170_usb_callback_cmd;
728
Alexander Beregalov8a713042009-05-04 20:46:25 +0400729#ifdef CONFIG_PM
Christian Lamparter90ccda92009-04-25 21:32:09 +0200730 udev->reset_resume = 1;
Alexander Beregalov8a713042009-05-04 20:46:25 +0400731#endif
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100732 err = ar9170_usb_reset(aru);
733 if (err)
Christian Lampartere10a9df2009-04-18 17:12:18 +0200734 goto err_freehw;
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100735
736 err = ar9170_usb_request_firmware(aru);
737 if (err)
Christian Lampartere10a9df2009-04-18 17:12:18 +0200738 goto err_freehw;
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100739
Christian Lampartere10a9df2009-04-18 17:12:18 +0200740 err = ar9170_usb_init_device(aru);
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100741 if (err)
742 goto err_freefw;
743
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100744 err = ar9170_usb_open(ar);
745 if (err)
746 goto err_unrx;
747
748 err = ar9170_register(ar, &udev->dev);
749
750 ar9170_usb_stop(ar);
751 if (err)
752 goto err_unrx;
753
754 return 0;
755
756err_unrx:
757 ar9170_usb_cancel_urbs(aru);
758
759err_freefw:
760 release_firmware(aru->init_values);
761 release_firmware(aru->firmware);
762
Christian Lampartere10a9df2009-04-18 17:12:18 +0200763err_freehw:
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100764 usb_set_intfdata(intf, NULL);
765 usb_put_dev(udev);
766 ieee80211_free_hw(ar->hw);
767out:
768 return err;
769}
770
771static void ar9170_usb_disconnect(struct usb_interface *intf)
772{
773 struct ar9170_usb *aru = usb_get_intfdata(intf);
774
775 if (!aru)
776 return;
777
778 aru->common.state = AR9170_IDLE;
779 ar9170_unregister(&aru->common);
780 ar9170_usb_cancel_urbs(aru);
781
782 release_firmware(aru->init_values);
783 release_firmware(aru->firmware);
784
785 usb_put_dev(aru->udev);
786 usb_set_intfdata(intf, NULL);
787 ieee80211_free_hw(aru->common.hw);
788}
789
Christian Lampartere10a9df2009-04-18 17:12:18 +0200790#ifdef CONFIG_PM
791static int ar9170_suspend(struct usb_interface *intf,
792 pm_message_t message)
793{
794 struct ar9170_usb *aru = usb_get_intfdata(intf);
795
796 if (!aru)
797 return -ENODEV;
798
799 aru->common.state = AR9170_IDLE;
800 ar9170_usb_cancel_urbs(aru);
801
802 return 0;
803}
804
805static int ar9170_resume(struct usb_interface *intf)
806{
807 struct ar9170_usb *aru = usb_get_intfdata(intf);
808 int err;
809
810 if (!aru)
811 return -ENODEV;
812
813 usb_unpoison_anchored_urbs(&aru->rx_submitted);
814 usb_unpoison_anchored_urbs(&aru->tx_submitted);
815
816 /*
817 * FIXME: firmware upload will fail on resume.
818 * but this is better than a hang!
819 */
820
821 err = ar9170_usb_init_device(aru);
822 if (err)
823 goto err_unrx;
824
825 err = ar9170_usb_open(&aru->common);
826 if (err)
827 goto err_unrx;
828
829 return 0;
830
831err_unrx:
832 aru->common.state = AR9170_IDLE;
833 ar9170_usb_cancel_urbs(aru);
834
835 return err;
836}
837#endif /* CONFIG_PM */
838
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100839static struct usb_driver ar9170_driver = {
840 .name = "ar9170usb",
841 .probe = ar9170_usb_probe,
842 .disconnect = ar9170_usb_disconnect,
843 .id_table = ar9170_usb_ids,
844 .soft_unbind = 1,
Christian Lampartere10a9df2009-04-18 17:12:18 +0200845#ifdef CONFIG_PM
846 .suspend = ar9170_suspend,
847 .resume = ar9170_resume,
Christian Lamparter90ccda92009-04-25 21:32:09 +0200848 .reset_resume = ar9170_resume,
Christian Lampartere10a9df2009-04-18 17:12:18 +0200849#endif /* CONFIG_PM */
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100850};
851
852static int __init ar9170_init(void)
853{
854 return usb_register(&ar9170_driver);
855}
856
857static void __exit ar9170_exit(void)
858{
859 usb_deregister(&ar9170_driver);
860}
861
862module_init(ar9170_init);
863module_exit(ar9170_exit);