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