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