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