blob: f752698669d218526d92c8d8c54c15192d525441 [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
Christian Lamparter66d00812009-05-28 17:04:27 +0200105 if (unlikely(!aru)) {
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100106 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
Christian Lamparter66d00812009-05-28 17:04:27 +0200138 ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
139 urb->actual_length);
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100140
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);
Christian Lamparter66d00812009-05-28 17:04:27 +0200189 if (unlikely(err)) {
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100190 usb_unanchor_urb(urb);
Christian Lamparter66d00812009-05-28 17:04:27 +0200191 goto free;
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100192 }
193
194 return ;
195
196free:
197 dev_kfree_skb_irq(skb);
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100198}
199
200static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
201 struct urb *urb, gfp_t gfp)
202{
203 struct sk_buff *skb;
204
205 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
206 if (!skb)
207 return -ENOMEM;
208
209 /* reserve some space for mac80211's radiotap */
210 skb_reserve(skb, 32);
211
212 usb_fill_bulk_urb(urb, aru->udev,
213 usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
214 skb->data, min(skb_tailroom(skb),
215 AR9170_MAX_RX_BUFFER_SIZE),
216 ar9170_usb_rx_completed, skb);
217
218 return 0;
219}
220
221static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
222{
223 struct urb *urb = NULL;
224 void *ibuf;
225 int err = -ENOMEM;
226
227 /* initialize interrupt endpoint */
228 urb = usb_alloc_urb(0, GFP_KERNEL);
229 if (!urb)
230 goto out;
231
232 ibuf = usb_buffer_alloc(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
233 if (!ibuf)
234 goto out;
235
236 usb_fill_int_urb(urb, aru->udev,
237 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
238 64, ar9170_usb_irq_completed, aru, 1);
239 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
240
241 usb_anchor_urb(urb, &aru->rx_submitted);
242 err = usb_submit_urb(urb, GFP_KERNEL);
243 if (err) {
244 usb_unanchor_urb(urb);
245 usb_buffer_free(aru->udev, 64, urb->transfer_buffer,
246 urb->transfer_dma);
247 }
248
249out:
250 usb_free_urb(urb);
251 return err;
252}
253
254static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
255{
256 struct urb *urb;
257 int i;
258 int err = -EINVAL;
259
260 for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
261 err = -ENOMEM;
262 urb = usb_alloc_urb(0, GFP_KERNEL);
263 if (!urb)
264 goto err_out;
265
266 err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
267 if (err) {
268 usb_free_urb(urb);
269 goto err_out;
270 }
271
272 usb_anchor_urb(urb, &aru->rx_submitted);
273 err = usb_submit_urb(urb, GFP_KERNEL);
274 if (err) {
275 usb_unanchor_urb(urb);
276 dev_kfree_skb_any((void *) urb->transfer_buffer);
277 usb_free_urb(urb);
278 goto err_out;
279 }
280 usb_free_urb(urb);
281 }
282
283 /* the device now waiting for a firmware. */
284 aru->common.state = AR9170_IDLE;
285 return 0;
286
287err_out:
288
289 usb_kill_anchored_urbs(&aru->rx_submitted);
290 return err;
291}
292
293static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
294{
295 int ret;
296
297 aru->common.state = AR9170_UNKNOWN_STATE;
298
299 usb_unlink_anchored_urbs(&aru->tx_submitted);
300
301 /* give the LED OFF command and the deauth frame a chance to air. */
302 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
303 msecs_to_jiffies(100));
304 if (ret == 0)
305 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
306 usb_poison_anchored_urbs(&aru->tx_submitted);
307
308 usb_poison_anchored_urbs(&aru->rx_submitted);
309}
310
311static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
312 unsigned int plen, void *payload,
313 unsigned int outlen, void *out)
314{
315 struct ar9170_usb *aru = (void *) ar;
316 struct urb *urb = NULL;
317 unsigned long flags;
318 int err = -ENOMEM;
319
320 if (unlikely(!IS_ACCEPTING_CMD(ar)))
321 return -EPERM;
322
323 if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
324 return -EINVAL;
325
326 urb = usb_alloc_urb(0, GFP_ATOMIC);
327 if (unlikely(!urb))
328 goto err_free;
329
330 ar->cmdbuf[0] = cpu_to_le32(plen);
331 ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
332 /* writing multiple regs fills this buffer already */
333 if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
334 memcpy(&ar->cmdbuf[1], payload, plen);
335
336 spin_lock_irqsave(&aru->common.cmdlock, flags);
337 aru->readbuf = (u8 *)out;
338 aru->readlen = outlen;
339 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
340
341 usb_fill_int_urb(urb, aru->udev,
342 usb_sndbulkpipe(aru->udev, AR9170_EP_CMD),
343 aru->common.cmdbuf, plen + 4,
344 ar9170_usb_tx_urb_complete, NULL, 1);
345
346 usb_anchor_urb(urb, &aru->tx_submitted);
347 err = usb_submit_urb(urb, GFP_ATOMIC);
Christian Lamparter66d00812009-05-28 17:04:27 +0200348 if (unlikely(err)) {
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100349 usb_unanchor_urb(urb);
350 usb_free_urb(urb);
351 goto err_unbuf;
352 }
353 usb_free_urb(urb);
354
355 err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
356 if (err == 0) {
357 err = -ETIMEDOUT;
358 goto err_unbuf;
359 }
360
Roel Kluinba5101d2009-04-26 14:40:59 +0200361 if (aru->readlen != outlen) {
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100362 err = -EMSGSIZE;
363 goto err_unbuf;
364 }
365
366 return 0;
367
368err_unbuf:
369 /* Maybe the device was removed in the second we were waiting? */
370 if (IS_STARTED(ar)) {
371 dev_err(&aru->udev->dev, "no command feedback "
372 "received (%d).\n", err);
373
374 /* provide some maybe useful debug information */
375 print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
376 aru->common.cmdbuf, plen + 4);
377 dump_stack();
378 }
379
380 /* invalidate to avoid completing the next prematurely */
381 spin_lock_irqsave(&aru->common.cmdlock, flags);
382 aru->readbuf = NULL;
383 aru->readlen = 0;
384 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
385
386err_free:
387
388 return err;
389}
390
391static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb,
392 bool txstatus_needed, unsigned int extra_len)
393{
394 struct ar9170_usb *aru = (struct ar9170_usb *) ar;
395 struct urb *urb;
396 int err;
397
398 if (unlikely(!IS_STARTED(ar))) {
399 /* Seriously, what were you drink... err... thinking!? */
400 return -EPERM;
401 }
402
403 urb = usb_alloc_urb(0, GFP_ATOMIC);
404 if (unlikely(!urb))
405 return -ENOMEM;
406
407 usb_fill_bulk_urb(urb, aru->udev,
408 usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
409 skb->data, skb->len + extra_len, (txstatus_needed ?
410 ar9170_usb_tx_urb_complete :
411 ar9170_usb_tx_urb_complete_free), skb);
412 urb->transfer_flags |= URB_ZERO_PACKET;
413
414 usb_anchor_urb(urb, &aru->tx_submitted);
415 err = usb_submit_urb(urb, GFP_ATOMIC);
416 if (unlikely(err))
417 usb_unanchor_urb(urb);
418
419 usb_free_urb(urb);
420 return err;
421}
422
423static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
424{
425 struct ar9170_usb *aru = (void *) ar;
426 unsigned long flags;
427 u32 in, out;
428
Christian Lamparter66d00812009-05-28 17:04:27 +0200429 if (unlikely(!buffer))
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100430 return ;
431
432 in = le32_to_cpup((__le32 *)buffer);
433 out = le32_to_cpu(ar->cmdbuf[0]);
434
435 /* mask off length byte */
436 out &= ~0xFF;
437
438 if (aru->readlen >= 0) {
439 /* add expected length */
440 out |= aru->readlen;
441 } else {
442 /* add obtained length */
443 out |= in & 0xFF;
444 }
445
446 /*
447 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
448 * length and we cannot predict the correct length in advance.
449 * So we only check if we provided enough space for the data.
450 */
451 if (unlikely(out < in)) {
452 dev_warn(&aru->udev->dev, "received invalid command response "
453 "got %d bytes, instead of %d bytes "
454 "and the resp length is %d bytes\n",
455 in, out, len);
456 print_hex_dump_bytes("ar9170 invalid resp: ",
457 DUMP_PREFIX_OFFSET, buffer, len);
458 /*
459 * Do not complete, then the command times out,
460 * and we get a stack trace from there.
461 */
462 return ;
463 }
464
465 spin_lock_irqsave(&aru->common.cmdlock, flags);
466 if (aru->readbuf && len > 0) {
467 memcpy(aru->readbuf, buffer + 4, len - 4);
468 aru->readbuf = NULL;
469 }
470 complete(&aru->cmd_wait);
471 spin_unlock_irqrestore(&aru->common.cmdlock, flags);
472}
473
474static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
475 size_t len, u32 addr, bool complete)
476{
477 int transfer, err;
478 u8 *buf = kmalloc(4096, GFP_KERNEL);
479
480 if (!buf)
481 return -ENOMEM;
482
483 while (len) {
484 transfer = min_t(int, len, 4096);
485 memcpy(buf, data, transfer);
486
487 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
488 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
489 addr >> 8, 0, buf, transfer, 1000);
490
491 if (err < 0) {
492 kfree(buf);
493 return err;
494 }
495
496 len -= transfer;
497 data += transfer;
498 addr += transfer;
499 }
500 kfree(buf);
501
502 if (complete) {
503 err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
504 0x31 /* FW DL COMPLETE */,
505 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
506 }
507
508 return 0;
509}
510
511static int ar9170_usb_request_firmware(struct ar9170_usb *aru)
512{
513 int err = 0;
514
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400515 err = request_firmware(&aru->firmware, "ar9170.fw",
516 &aru->udev->dev);
517 if (!err) {
518 aru->init_values = NULL;
519 return 0;
520 }
521
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -0400522 if (aru->req_one_stage_fw) {
523 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
524 "not found and is required for this device\n");
525 return -EINVAL;
526 }
527
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400528 dev_err(&aru->udev->dev, "ar9170.fw firmware file "
529 "not found, trying old firmware...\n");
530
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100531 err = request_firmware(&aru->init_values, "ar9170-1.fw",
532 &aru->udev->dev);
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100533
534 err = request_firmware(&aru->firmware, "ar9170-2.fw", &aru->udev->dev);
535 if (err) {
536 release_firmware(aru->init_values);
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400537 dev_err(&aru->udev->dev, "file with init values not found.\n");
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100538 return err;
539 }
540
541 return err;
542}
543
544static int ar9170_usb_reset(struct ar9170_usb *aru)
545{
546 int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
547
548 if (lock) {
549 ret = usb_lock_device_for_reset(aru->udev, aru->intf);
550 if (ret < 0) {
551 dev_err(&aru->udev->dev, "unable to lock device "
552 "for reset (%d).\n", ret);
553 return ret;
554 }
555 }
556
557 ret = usb_reset_device(aru->udev);
558 if (lock)
559 usb_unlock_device(aru->udev);
560
561 /* let it rest - for a second - */
562 msleep(1000);
563
564 return ret;
565}
566
567static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
568{
569 int err;
570
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400571 if (!aru->init_values)
572 goto upload_fw_start;
573
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100574 /* First, upload initial values to device RAM */
575 err = ar9170_usb_upload(aru, aru->init_values->data,
576 aru->init_values->size, 0x102800, false);
577 if (err) {
578 dev_err(&aru->udev->dev, "firmware part 1 "
579 "upload failed (%d).\n", err);
580 return err;
581 }
582
Luis R. Rodriguezc768b582009-05-28 17:36:04 -0400583upload_fw_start:
584
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100585 /* Then, upload the firmware itself and start it */
586 return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
587 0x200000, true);
588}
589
590static int ar9170_usb_init_transport(struct ar9170_usb *aru)
591{
592 struct ar9170 *ar = (void *) &aru->common;
593 int err;
594
595 ar9170_regwrite_begin(ar);
596
597 /* Set USB Rx stream mode MAX packet number to 2 */
598 ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
599
600 /* Set USB Rx stream mode timeout to 10us */
601 ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
602
603 ar9170_regwrite_finish();
604
605 err = ar9170_regwrite_result();
606 if (err)
607 dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
608
609 return err;
610}
611
612static void ar9170_usb_stop(struct ar9170 *ar)
613{
614 struct ar9170_usb *aru = (void *) ar;
615 int ret;
616
617 if (IS_ACCEPTING_CMD(ar))
618 aru->common.state = AR9170_STOPPED;
619
620 /* lets wait a while until the tx - queues are dried out */
621 ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
622 msecs_to_jiffies(1000));
623 if (ret == 0)
624 dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
625
626 usb_poison_anchored_urbs(&aru->tx_submitted);
627
628 /*
629 * Note:
630 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
631 * Else we would end up with a unresponsive device...
632 */
633}
634
635static int ar9170_usb_open(struct ar9170 *ar)
636{
637 struct ar9170_usb *aru = (void *) ar;
638 int err;
639
640 usb_unpoison_anchored_urbs(&aru->tx_submitted);
641 err = ar9170_usb_init_transport(aru);
642 if (err) {
643 usb_poison_anchored_urbs(&aru->tx_submitted);
644 return err;
645 }
646
647 aru->common.state = AR9170_IDLE;
648 return 0;
649}
650
Christian Lampartere10a9df2009-04-18 17:12:18 +0200651static int ar9170_usb_init_device(struct ar9170_usb *aru)
652{
653 int err;
654
655 err = ar9170_usb_alloc_rx_irq_urb(aru);
656 if (err)
657 goto err_out;
658
659 err = ar9170_usb_alloc_rx_bulk_urbs(aru);
660 if (err)
661 goto err_unrx;
662
663 err = ar9170_usb_upload_firmware(aru);
664 if (err) {
665 err = ar9170_echo_test(&aru->common, 0x60d43110);
666 if (err) {
667 /* force user invention, by disabling the device */
668 err = usb_driver_set_configuration(aru->udev, -1);
669 dev_err(&aru->udev->dev, "device is in a bad state. "
670 "please reconnect it!\n");
671 goto err_unrx;
672 }
673 }
674
675 return 0;
676
677err_unrx:
678 ar9170_usb_cancel_urbs(aru);
679
680err_out:
681 return err;
682}
683
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -0400684static bool ar9170_requires_one_stage(const struct usb_device_id *id)
685{
686 if (!id->driver_info)
687 return false;
688 if (id->driver_info == AR9170_REQ_FW1_ONLY)
689 return true;
690 return false;
691}
692
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100693static int ar9170_usb_probe(struct usb_interface *intf,
694 const struct usb_device_id *id)
695{
696 struct ar9170_usb *aru;
697 struct ar9170 *ar;
698 struct usb_device *udev;
699 int err;
700
701 aru = ar9170_alloc(sizeof(*aru));
702 if (IS_ERR(aru)) {
703 err = PTR_ERR(aru);
704 goto out;
705 }
706
707 udev = interface_to_usbdev(intf);
708 usb_get_dev(udev);
709 aru->udev = udev;
710 aru->intf = intf;
711 ar = &aru->common;
712
Luis R. Rodriguezbdf6d322009-05-28 17:36:05 -0400713 aru->req_one_stage_fw = ar9170_requires_one_stage(id);
714
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100715 usb_set_intfdata(intf, aru);
716 SET_IEEE80211_DEV(ar->hw, &udev->dev);
717
718 init_usb_anchor(&aru->rx_submitted);
719 init_usb_anchor(&aru->tx_submitted);
720 init_completion(&aru->cmd_wait);
721
722 aru->common.stop = ar9170_usb_stop;
723 aru->common.open = ar9170_usb_open;
724 aru->common.tx = ar9170_usb_tx;
725 aru->common.exec_cmd = ar9170_usb_exec_cmd;
726 aru->common.callback_cmd = ar9170_usb_callback_cmd;
727
Alexander Beregalov8a713042009-05-04 20:46:25 +0400728#ifdef CONFIG_PM
Christian Lamparter90ccda92009-04-25 21:32:09 +0200729 udev->reset_resume = 1;
Christian Lamparter66d00812009-05-28 17:04:27 +0200730#endif /* CONFIG_PM */
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100731 err = ar9170_usb_reset(aru);
732 if (err)
Christian Lampartere10a9df2009-04-18 17:12:18 +0200733 goto err_freehw;
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100734
735 err = ar9170_usb_request_firmware(aru);
736 if (err)
Christian Lampartere10a9df2009-04-18 17:12:18 +0200737 goto err_freehw;
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100738
Christian Lampartere10a9df2009-04-18 17:12:18 +0200739 err = ar9170_usb_init_device(aru);
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100740 if (err)
741 goto err_freefw;
742
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100743 err = ar9170_usb_open(ar);
744 if (err)
745 goto err_unrx;
746
747 err = ar9170_register(ar, &udev->dev);
748
749 ar9170_usb_stop(ar);
750 if (err)
751 goto err_unrx;
752
753 return 0;
754
755err_unrx:
756 ar9170_usb_cancel_urbs(aru);
757
758err_freefw:
759 release_firmware(aru->init_values);
760 release_firmware(aru->firmware);
761
Christian Lampartere10a9df2009-04-18 17:12:18 +0200762err_freehw:
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100763 usb_set_intfdata(intf, NULL);
764 usb_put_dev(udev);
765 ieee80211_free_hw(ar->hw);
766out:
767 return err;
768}
769
770static void ar9170_usb_disconnect(struct usb_interface *intf)
771{
772 struct ar9170_usb *aru = usb_get_intfdata(intf);
773
774 if (!aru)
775 return;
776
777 aru->common.state = AR9170_IDLE;
778 ar9170_unregister(&aru->common);
779 ar9170_usb_cancel_urbs(aru);
780
781 release_firmware(aru->init_values);
782 release_firmware(aru->firmware);
783
784 usb_put_dev(aru->udev);
785 usb_set_intfdata(intf, NULL);
786 ieee80211_free_hw(aru->common.hw);
787}
788
Christian Lampartere10a9df2009-04-18 17:12:18 +0200789#ifdef CONFIG_PM
790static int ar9170_suspend(struct usb_interface *intf,
791 pm_message_t message)
792{
793 struct ar9170_usb *aru = usb_get_intfdata(intf);
794
795 if (!aru)
796 return -ENODEV;
797
798 aru->common.state = AR9170_IDLE;
799 ar9170_usb_cancel_urbs(aru);
800
801 return 0;
802}
803
804static int ar9170_resume(struct usb_interface *intf)
805{
806 struct ar9170_usb *aru = usb_get_intfdata(intf);
807 int err;
808
809 if (!aru)
810 return -ENODEV;
811
812 usb_unpoison_anchored_urbs(&aru->rx_submitted);
813 usb_unpoison_anchored_urbs(&aru->tx_submitted);
814
Christian Lampartere10a9df2009-04-18 17:12:18 +0200815 err = ar9170_usb_init_device(aru);
816 if (err)
817 goto err_unrx;
818
819 err = ar9170_usb_open(&aru->common);
820 if (err)
821 goto err_unrx;
822
823 return 0;
824
825err_unrx:
826 aru->common.state = AR9170_IDLE;
827 ar9170_usb_cancel_urbs(aru);
828
829 return err;
830}
831#endif /* CONFIG_PM */
832
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100833static struct usb_driver ar9170_driver = {
834 .name = "ar9170usb",
835 .probe = ar9170_usb_probe,
836 .disconnect = ar9170_usb_disconnect,
837 .id_table = ar9170_usb_ids,
838 .soft_unbind = 1,
Christian Lampartere10a9df2009-04-18 17:12:18 +0200839#ifdef CONFIG_PM
840 .suspend = ar9170_suspend,
841 .resume = ar9170_resume,
Christian Lamparter90ccda92009-04-25 21:32:09 +0200842 .reset_resume = ar9170_resume,
Christian Lampartere10a9df2009-04-18 17:12:18 +0200843#endif /* CONFIG_PM */
Christian Lamparterb63a2cb2009-03-21 23:05:48 +0100844};
845
846static int __init ar9170_init(void)
847{
848 return usb_register(&ar9170_driver);
849}
850
851static void __exit ar9170_exit(void)
852{
853 usb_deregister(&ar9170_driver);
854}
855
856module_init(ar9170_init);
857module_exit(ar9170_exit);