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