1 /* 2 * Atheros CARL9170 driver 3 * 4 * USB - frontend 5 * 6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net> 7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com> 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/slab.h> 42 #include <linux/usb.h> 43 #include <linux/firmware.h> 44 #include <linux/etherdevice.h> 45 #include <linux/device.h> 46 #include <net/mac80211.h> 47 #include "carl9170.h" 48 #include "cmd.h" 49 #include "hw.h" 50 #include "fwcmd.h" 51 52 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); 53 MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>"); 54 MODULE_LICENSE("GPL"); 55 MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless"); 56 MODULE_FIRMWARE(CARL9170FW_NAME); 57 MODULE_ALIAS("ar9170usb"); 58 MODULE_ALIAS("arusb_lnx"); 59 60 /* 61 * Note: 62 * 63 * Always update our wiki's device list (located at: 64 * https://wireless.wiki.kernel.org/en/users/Drivers/ar9170/devices ), 65 * whenever you add a new device. 66 */ 67 static const struct usb_device_id carl9170_usb_ids[] = { 68 /* Atheros 9170 */ 69 { USB_DEVICE(0x0cf3, 0x9170) }, 70 /* Atheros TG121N */ 71 { USB_DEVICE(0x0cf3, 0x1001) }, 72 /* TP-Link TL-WN821N v2 */ 73 { USB_DEVICE(0x0cf3, 0x1002), .driver_info = CARL9170_WPS_BUTTON | 74 CARL9170_ONE_LED }, 75 /* 3Com Dual Band 802.11n USB Adapter */ 76 { USB_DEVICE(0x0cf3, 0x1010) }, 77 /* H3C Dual Band 802.11n USB Adapter */ 78 { USB_DEVICE(0x0cf3, 0x1011) }, 79 /* Cace Airpcap NX */ 80 { USB_DEVICE(0xcace, 0x0300) }, 81 /* D-Link DWA 160 A1 */ 82 { USB_DEVICE(0x07d1, 0x3c10) }, 83 /* D-Link DWA 160 A2 */ 84 { USB_DEVICE(0x07d1, 0x3a09) }, 85 /* D-Link DWA 130 D */ 86 { USB_DEVICE(0x07d1, 0x3a0f) }, 87 /* Netgear WNA1000 */ 88 { USB_DEVICE(0x0846, 0x9040) }, 89 /* Netgear WNDA3100 (v1) */ 90 { USB_DEVICE(0x0846, 0x9010) }, 91 /* Netgear WN111 v2 */ 92 { USB_DEVICE(0x0846, 0x9001), .driver_info = CARL9170_ONE_LED }, 93 /* Zydas ZD1221 */ 94 { USB_DEVICE(0x0ace, 0x1221) }, 95 /* Proxim ORiNOCO 802.11n USB */ 96 { USB_DEVICE(0x1435, 0x0804) }, 97 /* WNC Generic 11n USB Dongle */ 98 { USB_DEVICE(0x1435, 0x0326) }, 99 /* ZyXEL NWD271N */ 100 { USB_DEVICE(0x0586, 0x3417) }, 101 /* Z-Com UB81 BG */ 102 { USB_DEVICE(0x0cde, 0x0023) }, 103 /* Z-Com UB82 ABG */ 104 { USB_DEVICE(0x0cde, 0x0026) }, 105 /* Sphairon Homelink 1202 */ 106 { USB_DEVICE(0x0cde, 0x0027) }, 107 /* Arcadyan WN7512 */ 108 { USB_DEVICE(0x083a, 0xf522) }, 109 /* Planex GWUS300 */ 110 { USB_DEVICE(0x2019, 0x5304) }, 111 /* IO-Data WNGDNUS2 */ 112 { USB_DEVICE(0x04bb, 0x093f) }, 113 /* NEC WL300NU-G */ 114 { USB_DEVICE(0x0409, 0x0249) }, 115 /* NEC WL300NU-AG */ 116 { USB_DEVICE(0x0409, 0x02b4) }, 117 /* AVM FRITZ!WLAN USB Stick N */ 118 { USB_DEVICE(0x057c, 0x8401) }, 119 /* AVM FRITZ!WLAN USB Stick N 2.4 */ 120 { USB_DEVICE(0x057c, 0x8402) }, 121 /* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */ 122 { USB_DEVICE(0x1668, 0x1200) }, 123 /* Airlive X.USB a/b/g/n */ 124 { USB_DEVICE(0x1b75, 0x9170) }, 125 126 /* terminate */ 127 {} 128 }; 129 MODULE_DEVICE_TABLE(usb, carl9170_usb_ids); 130 131 static struct usb_driver carl9170_driver; 132 133 static void carl9170_usb_submit_data_urb(struct ar9170 *ar) 134 { 135 struct urb *urb; 136 int err; 137 138 if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS) 139 goto err_acc; 140 141 urb = usb_get_from_anchor(&ar->tx_wait); 142 if (!urb) 143 goto err_acc; 144 145 usb_anchor_urb(urb, &ar->tx_anch); 146 147 err = usb_submit_urb(urb, GFP_ATOMIC); 148 if (unlikely(err)) { 149 if (net_ratelimit()) { 150 dev_err(&ar->udev->dev, "tx submit failed (%d)\n", 151 urb->status); 152 } 153 154 usb_unanchor_urb(urb); 155 usb_anchor_urb(urb, &ar->tx_err); 156 } 157 158 usb_free_urb(urb); 159 160 if (likely(err == 0)) 161 return; 162 163 err_acc: 164 atomic_dec(&ar->tx_anch_urbs); 165 } 166 167 static void carl9170_usb_tx_data_complete(struct urb *urb) 168 { 169 struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); 170 171 if (WARN_ON_ONCE(!ar)) { 172 dev_kfree_skb_irq(urb->context); 173 return; 174 } 175 176 atomic_dec(&ar->tx_anch_urbs); 177 178 switch (urb->status) { 179 /* everything is fine */ 180 case 0: 181 carl9170_tx_callback(ar, urb->context); 182 break; 183 184 /* disconnect */ 185 case -ENOENT: 186 case -ECONNRESET: 187 case -ENODEV: 188 case -ESHUTDOWN: 189 /* 190 * Defer the frame clean-up to the tasklet worker. 191 * This is necessary, because carl9170_tx_drop 192 * does not work in an irqsave context. 193 */ 194 usb_anchor_urb(urb, &ar->tx_err); 195 return; 196 197 /* a random transmission error has occurred? */ 198 default: 199 if (net_ratelimit()) { 200 dev_err(&ar->udev->dev, "tx failed (%d)\n", 201 urb->status); 202 } 203 204 usb_anchor_urb(urb, &ar->tx_err); 205 break; 206 } 207 208 if (likely(IS_STARTED(ar))) 209 carl9170_usb_submit_data_urb(ar); 210 } 211 212 static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar) 213 { 214 struct urb *urb; 215 int err; 216 217 if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) { 218 atomic_dec(&ar->tx_cmd_urbs); 219 return 0; 220 } 221 222 urb = usb_get_from_anchor(&ar->tx_cmd); 223 if (!urb) { 224 atomic_dec(&ar->tx_cmd_urbs); 225 return 0; 226 } 227 228 usb_anchor_urb(urb, &ar->tx_anch); 229 err = usb_submit_urb(urb, GFP_ATOMIC); 230 if (unlikely(err)) { 231 usb_unanchor_urb(urb); 232 atomic_dec(&ar->tx_cmd_urbs); 233 } 234 usb_free_urb(urb); 235 236 return err; 237 } 238 239 static void carl9170_usb_cmd_complete(struct urb *urb) 240 { 241 struct ar9170 *ar = urb->context; 242 int err = 0; 243 244 if (WARN_ON_ONCE(!ar)) 245 return; 246 247 atomic_dec(&ar->tx_cmd_urbs); 248 249 switch (urb->status) { 250 /* everything is fine */ 251 case 0: 252 break; 253 254 /* disconnect */ 255 case -ENOENT: 256 case -ECONNRESET: 257 case -ENODEV: 258 case -ESHUTDOWN: 259 return; 260 261 default: 262 err = urb->status; 263 break; 264 } 265 266 if (!IS_INITIALIZED(ar)) 267 return; 268 269 if (err) 270 dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err); 271 272 err = carl9170_usb_submit_cmd_urb(ar); 273 if (err) 274 dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err); 275 } 276 277 static void carl9170_usb_rx_irq_complete(struct urb *urb) 278 { 279 struct ar9170 *ar = urb->context; 280 281 if (WARN_ON_ONCE(!ar)) 282 return; 283 284 switch (urb->status) { 285 /* everything is fine */ 286 case 0: 287 break; 288 289 /* disconnect */ 290 case -ENOENT: 291 case -ECONNRESET: 292 case -ENODEV: 293 case -ESHUTDOWN: 294 return; 295 296 default: 297 goto resubmit; 298 } 299 300 /* 301 * While the carl9170 firmware does not use this EP, the 302 * firmware loader in the EEPROM unfortunately does. 303 * Therefore we need to be ready to handle out-of-band 304 * responses and traps in case the firmware crashed and 305 * the loader took over again. 306 */ 307 carl9170_handle_command_response(ar, urb->transfer_buffer, 308 urb->actual_length); 309 310 resubmit: 311 usb_anchor_urb(urb, &ar->rx_anch); 312 if (unlikely(usb_submit_urb(urb, GFP_ATOMIC))) 313 usb_unanchor_urb(urb); 314 } 315 316 static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp) 317 { 318 struct urb *urb; 319 int err = 0, runs = 0; 320 321 while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) && 322 (runs++ < AR9170_NUM_RX_URBS)) { 323 err = -ENOSPC; 324 urb = usb_get_from_anchor(&ar->rx_pool); 325 if (urb) { 326 usb_anchor_urb(urb, &ar->rx_anch); 327 err = usb_submit_urb(urb, gfp); 328 if (unlikely(err)) { 329 usb_unanchor_urb(urb); 330 usb_anchor_urb(urb, &ar->rx_pool); 331 } else { 332 atomic_dec(&ar->rx_pool_urbs); 333 atomic_inc(&ar->rx_anch_urbs); 334 } 335 usb_free_urb(urb); 336 } 337 } 338 339 return err; 340 } 341 342 static void carl9170_usb_rx_work(struct ar9170 *ar) 343 { 344 struct urb *urb; 345 int i; 346 347 for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) { 348 urb = usb_get_from_anchor(&ar->rx_work); 349 if (!urb) 350 break; 351 352 atomic_dec(&ar->rx_work_urbs); 353 if (IS_INITIALIZED(ar)) { 354 carl9170_rx(ar, urb->transfer_buffer, 355 urb->actual_length); 356 } 357 358 usb_anchor_urb(urb, &ar->rx_pool); 359 atomic_inc(&ar->rx_pool_urbs); 360 361 usb_free_urb(urb); 362 363 carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC); 364 } 365 } 366 367 void carl9170_usb_handle_tx_err(struct ar9170 *ar) 368 { 369 struct urb *urb; 370 371 while ((urb = usb_get_from_anchor(&ar->tx_err))) { 372 struct sk_buff *skb = urb->context; 373 374 carl9170_tx_drop(ar, skb); 375 carl9170_tx_callback(ar, skb); 376 usb_free_urb(urb); 377 } 378 } 379 380 static void carl9170_usb_tasklet(struct tasklet_struct *t) 381 { 382 struct ar9170 *ar = from_tasklet(ar, t, usb_tasklet); 383 384 if (!IS_INITIALIZED(ar)) 385 return; 386 387 carl9170_usb_rx_work(ar); 388 389 /* 390 * Strictly speaking: The tx scheduler is not part of the USB system. 391 * But the rx worker returns frames back to the mac80211-stack and 392 * this is the _perfect_ place to generate the next transmissions. 393 */ 394 if (IS_STARTED(ar)) 395 carl9170_tx_scheduler(ar); 396 } 397 398 static void carl9170_usb_rx_complete(struct urb *urb) 399 { 400 struct ar9170 *ar = urb->context; 401 int err; 402 403 if (WARN_ON_ONCE(!ar)) 404 return; 405 406 atomic_dec(&ar->rx_anch_urbs); 407 408 switch (urb->status) { 409 case 0: 410 /* rx path */ 411 usb_anchor_urb(urb, &ar->rx_work); 412 atomic_inc(&ar->rx_work_urbs); 413 break; 414 415 case -ENOENT: 416 case -ECONNRESET: 417 case -ENODEV: 418 case -ESHUTDOWN: 419 /* handle disconnect events*/ 420 return; 421 422 default: 423 /* handle all other errors */ 424 usb_anchor_urb(urb, &ar->rx_pool); 425 atomic_inc(&ar->rx_pool_urbs); 426 break; 427 } 428 429 err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC); 430 if (unlikely(err)) { 431 /* 432 * usb_submit_rx_urb reported a problem. 433 * In case this is due to a rx buffer shortage, 434 * elevate the tasklet worker priority to 435 * the highest available level. 436 */ 437 tasklet_hi_schedule(&ar->usb_tasklet); 438 439 if (atomic_read(&ar->rx_anch_urbs) == 0) { 440 /* 441 * At this point, either the system is too slow to 442 * cope with the enormous workload (so we have simply 443 * run out of active rx urbs and this unfortunately 444 * leads to an unpredictable device), or the device 445 * is not fully functional after an unsuccessful 446 * firmware loading attempts (so it doesn't pass 447 * ieee80211_register_hw() and there is no internal 448 * workqueue at all). 449 */ 450 451 if (ar->registered) 452 ieee80211_queue_work(ar->hw, &ar->ping_work); 453 else 454 pr_warn_once("device %s is not registered\n", 455 dev_name(&ar->udev->dev)); 456 } 457 } else { 458 /* 459 * Using anything less than _high_ priority absolutely 460 * kills the rx performance my UP-System... 461 */ 462 tasklet_hi_schedule(&ar->usb_tasklet); 463 } 464 } 465 466 static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp) 467 { 468 struct urb *urb; 469 void *buf; 470 471 buf = kmalloc(ar->fw.rx_size, gfp); 472 if (!buf) 473 return NULL; 474 475 urb = usb_alloc_urb(0, gfp); 476 if (!urb) { 477 kfree(buf); 478 return NULL; 479 } 480 481 usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev, 482 AR9170_USB_EP_RX), buf, ar->fw.rx_size, 483 carl9170_usb_rx_complete, ar); 484 485 urb->transfer_flags |= URB_FREE_BUFFER; 486 487 return urb; 488 } 489 490 static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar) 491 { 492 struct urb *urb = NULL; 493 void *ibuf; 494 int err = -ENOMEM; 495 496 urb = usb_alloc_urb(0, GFP_KERNEL); 497 if (!urb) 498 goto out; 499 500 ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL); 501 if (!ibuf) 502 goto out; 503 504 usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev, 505 AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX, 506 carl9170_usb_rx_irq_complete, ar, 1); 507 508 urb->transfer_flags |= URB_FREE_BUFFER; 509 510 usb_anchor_urb(urb, &ar->rx_anch); 511 err = usb_submit_urb(urb, GFP_KERNEL); 512 if (err) 513 usb_unanchor_urb(urb); 514 515 out: 516 usb_free_urb(urb); 517 return err; 518 } 519 520 static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar) 521 { 522 struct urb *urb; 523 int i, err = -EINVAL; 524 525 /* 526 * The driver actively maintains a second shadow 527 * pool for inactive, but fully-prepared rx urbs. 528 * 529 * The pool should help the driver to master huge 530 * workload spikes without running the risk of 531 * undersupplying the hardware or wasting time by 532 * processing rx data (streams) inside the urb 533 * completion (hardirq context). 534 */ 535 for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) { 536 urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL); 537 if (!urb) { 538 err = -ENOMEM; 539 goto err_out; 540 } 541 542 usb_anchor_urb(urb, &ar->rx_pool); 543 atomic_inc(&ar->rx_pool_urbs); 544 usb_free_urb(urb); 545 } 546 547 err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL); 548 if (err) 549 goto err_out; 550 551 /* the device now waiting for the firmware. */ 552 carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE); 553 return 0; 554 555 err_out: 556 557 usb_scuttle_anchored_urbs(&ar->rx_pool); 558 usb_scuttle_anchored_urbs(&ar->rx_work); 559 usb_kill_anchored_urbs(&ar->rx_anch); 560 return err; 561 } 562 563 static int carl9170_usb_flush(struct ar9170 *ar) 564 { 565 struct urb *urb; 566 int ret, err = 0; 567 568 while ((urb = usb_get_from_anchor(&ar->tx_wait))) { 569 struct sk_buff *skb = urb->context; 570 carl9170_tx_drop(ar, skb); 571 carl9170_tx_callback(ar, skb); 572 usb_free_urb(urb); 573 } 574 575 ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000); 576 if (ret == 0) 577 err = -ETIMEDOUT; 578 579 /* lets wait a while until the tx - queues are dried out */ 580 ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000); 581 if (ret == 0) 582 err = -ETIMEDOUT; 583 584 usb_kill_anchored_urbs(&ar->tx_anch); 585 carl9170_usb_handle_tx_err(ar); 586 587 return err; 588 } 589 590 static void carl9170_usb_cancel_urbs(struct ar9170 *ar) 591 { 592 int err; 593 594 carl9170_set_state(ar, CARL9170_UNKNOWN_STATE); 595 596 err = carl9170_usb_flush(ar); 597 if (err) 598 dev_err(&ar->udev->dev, "stuck tx urbs!\n"); 599 600 usb_poison_anchored_urbs(&ar->tx_anch); 601 carl9170_usb_handle_tx_err(ar); 602 usb_poison_anchored_urbs(&ar->rx_anch); 603 604 tasklet_kill(&ar->usb_tasklet); 605 606 usb_scuttle_anchored_urbs(&ar->rx_work); 607 usb_scuttle_anchored_urbs(&ar->rx_pool); 608 usb_scuttle_anchored_urbs(&ar->tx_cmd); 609 } 610 611 int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd, 612 const bool free_buf) 613 { 614 struct urb *urb; 615 int err = 0; 616 617 if (!IS_INITIALIZED(ar)) { 618 err = -EPERM; 619 goto err_free; 620 } 621 622 if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) { 623 err = -EINVAL; 624 goto err_free; 625 } 626 627 urb = usb_alloc_urb(0, GFP_ATOMIC); 628 if (!urb) { 629 err = -ENOMEM; 630 goto err_free; 631 } 632 633 if (ar->usb_ep_cmd_is_bulk) 634 usb_fill_bulk_urb(urb, ar->udev, 635 usb_sndbulkpipe(ar->udev, AR9170_USB_EP_CMD), 636 cmd, cmd->hdr.len + 4, 637 carl9170_usb_cmd_complete, ar); 638 else 639 usb_fill_int_urb(urb, ar->udev, 640 usb_sndintpipe(ar->udev, AR9170_USB_EP_CMD), 641 cmd, cmd->hdr.len + 4, 642 carl9170_usb_cmd_complete, ar, 1); 643 644 if (free_buf) 645 urb->transfer_flags |= URB_FREE_BUFFER; 646 647 usb_anchor_urb(urb, &ar->tx_cmd); 648 usb_free_urb(urb); 649 650 return carl9170_usb_submit_cmd_urb(ar); 651 652 err_free: 653 if (free_buf) 654 kfree(cmd); 655 656 return err; 657 } 658 659 int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd, 660 unsigned int plen, void *payload, unsigned int outlen, void *out) 661 { 662 int err = -ENOMEM; 663 unsigned long time_left; 664 665 if (!IS_ACCEPTING_CMD(ar)) 666 return -EIO; 667 668 if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) 669 might_sleep(); 670 671 ar->cmd.hdr.len = plen; 672 ar->cmd.hdr.cmd = cmd; 673 /* writing multiple regs fills this buffer already */ 674 if (plen && payload != (u8 *)(ar->cmd.data)) 675 memcpy(ar->cmd.data, payload, plen); 676 677 spin_lock_bh(&ar->cmd_lock); 678 ar->readbuf = out; 679 ar->readlen = outlen; 680 spin_unlock_bh(&ar->cmd_lock); 681 682 reinit_completion(&ar->cmd_wait); 683 err = __carl9170_exec_cmd(ar, &ar->cmd, false); 684 685 if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) { 686 time_left = wait_for_completion_timeout(&ar->cmd_wait, HZ); 687 if (time_left == 0) { 688 err = -ETIMEDOUT; 689 goto err_unbuf; 690 } 691 692 if (ar->readlen != outlen) { 693 err = -EMSGSIZE; 694 goto err_unbuf; 695 } 696 } 697 698 return 0; 699 700 err_unbuf: 701 /* Maybe the device was removed in the moment we were waiting? */ 702 if (IS_STARTED(ar)) { 703 dev_err(&ar->udev->dev, "no command feedback " 704 "received (%d).\n", err); 705 706 /* provide some maybe useful debug information */ 707 print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE, 708 &ar->cmd, plen + 4); 709 710 carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT); 711 } 712 713 /* invalidate to avoid completing the next command prematurely */ 714 spin_lock_bh(&ar->cmd_lock); 715 ar->readbuf = NULL; 716 ar->readlen = 0; 717 spin_unlock_bh(&ar->cmd_lock); 718 719 return err; 720 } 721 722 void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb) 723 { 724 struct urb *urb; 725 struct ar9170_stream *tx_stream; 726 void *data; 727 unsigned int len; 728 729 if (!IS_STARTED(ar)) 730 goto err_drop; 731 732 urb = usb_alloc_urb(0, GFP_ATOMIC); 733 if (!urb) 734 goto err_drop; 735 736 if (ar->fw.tx_stream) { 737 tx_stream = (void *) (skb->data - sizeof(*tx_stream)); 738 739 len = skb->len + sizeof(*tx_stream); 740 tx_stream->length = cpu_to_le16(len); 741 tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG); 742 data = tx_stream; 743 } else { 744 data = skb->data; 745 len = skb->len; 746 } 747 748 usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev, 749 AR9170_USB_EP_TX), data, len, 750 carl9170_usb_tx_data_complete, skb); 751 752 urb->transfer_flags |= URB_ZERO_PACKET; 753 754 usb_anchor_urb(urb, &ar->tx_wait); 755 756 usb_free_urb(urb); 757 758 carl9170_usb_submit_data_urb(ar); 759 return; 760 761 err_drop: 762 carl9170_tx_drop(ar, skb); 763 carl9170_tx_callback(ar, skb); 764 } 765 766 static void carl9170_release_firmware(struct ar9170 *ar) 767 { 768 if (ar->fw.fw) { 769 release_firmware(ar->fw.fw); 770 memset(&ar->fw, 0, sizeof(ar->fw)); 771 } 772 } 773 774 void carl9170_usb_stop(struct ar9170 *ar) 775 { 776 int ret; 777 778 carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED); 779 780 ret = carl9170_usb_flush(ar); 781 if (ret) 782 dev_err(&ar->udev->dev, "kill pending tx urbs.\n"); 783 784 usb_poison_anchored_urbs(&ar->tx_anch); 785 carl9170_usb_handle_tx_err(ar); 786 787 /* kill any pending command */ 788 spin_lock_bh(&ar->cmd_lock); 789 ar->readlen = 0; 790 spin_unlock_bh(&ar->cmd_lock); 791 complete(&ar->cmd_wait); 792 793 /* 794 * Note: 795 * So far we freed all tx urbs, but we won't dare to touch any rx urbs. 796 * Else we would end up with a unresponsive device... 797 */ 798 } 799 800 int carl9170_usb_open(struct ar9170 *ar) 801 { 802 usb_unpoison_anchored_urbs(&ar->tx_anch); 803 804 carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE); 805 return 0; 806 } 807 808 static int carl9170_usb_load_firmware(struct ar9170 *ar) 809 { 810 const u8 *data; 811 u8 *buf; 812 unsigned int transfer; 813 size_t len; 814 u32 addr; 815 int err = 0; 816 817 buf = kmalloc(4096, GFP_KERNEL); 818 if (!buf) { 819 err = -ENOMEM; 820 goto err_out; 821 } 822 823 data = ar->fw.fw->data; 824 len = ar->fw.fw->size; 825 addr = ar->fw.address; 826 827 /* this removes the miniboot image */ 828 data += ar->fw.offset; 829 len -= ar->fw.offset; 830 831 while (len) { 832 transfer = min_t(unsigned int, len, 4096u); 833 memcpy(buf, data, transfer); 834 835 err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0), 836 0x30 /* FW DL */, 0x40 | USB_DIR_OUT, 837 addr >> 8, 0, buf, transfer, 100); 838 839 if (err < 0) { 840 kfree(buf); 841 goto err_out; 842 } 843 844 len -= transfer; 845 data += transfer; 846 addr += transfer; 847 } 848 kfree(buf); 849 850 err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0), 851 0x31 /* FW DL COMPLETE */, 852 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200); 853 854 if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) { 855 err = -ETIMEDOUT; 856 goto err_out; 857 } 858 859 err = carl9170_echo_test(ar, 0x4a110123); 860 if (err) 861 goto err_out; 862 863 /* now, start the command response counter */ 864 ar->cmd_seq = -1; 865 866 return 0; 867 868 err_out: 869 dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err); 870 return err; 871 } 872 873 int carl9170_usb_restart(struct ar9170 *ar) 874 { 875 int err = 0; 876 877 if (ar->intf->condition != USB_INTERFACE_BOUND) 878 return 0; 879 880 /* 881 * Disable the command response sequence counter check. 882 * We already know that the device/firmware is in a bad state. 883 * So, no extra points are awarded to anyone who reminds the 884 * driver about that. 885 */ 886 ar->cmd_seq = -2; 887 888 err = carl9170_reboot(ar); 889 890 carl9170_usb_stop(ar); 891 892 if (err) 893 goto err_out; 894 895 tasklet_schedule(&ar->usb_tasklet); 896 897 /* The reboot procedure can take quite a while to complete. */ 898 msleep(1100); 899 900 err = carl9170_usb_open(ar); 901 if (err) 902 goto err_out; 903 904 err = carl9170_usb_load_firmware(ar); 905 if (err) 906 goto err_out; 907 908 return 0; 909 910 err_out: 911 carl9170_usb_cancel_urbs(ar); 912 return err; 913 } 914 915 void carl9170_usb_reset(struct ar9170 *ar) 916 { 917 /* 918 * This is the last resort to get the device going again 919 * without any *user replugging action*. 920 * 921 * But there is a catch: usb_reset really is like a physical 922 * *reconnect*. The mac80211 state will be lost in the process. 923 * Therefore a userspace application, which is monitoring 924 * the link must step in. 925 */ 926 carl9170_usb_cancel_urbs(ar); 927 928 carl9170_usb_stop(ar); 929 930 usb_queue_reset_device(ar->intf); 931 } 932 933 static int carl9170_usb_init_device(struct ar9170 *ar) 934 { 935 int err; 936 937 /* 938 * The carl9170 firmware let's the driver know when it's 939 * ready for action. But we have to be prepared to gracefully 940 * handle all spurious [flushed] messages after each (re-)boot. 941 * Thus the command response counter remains disabled until it 942 * can be safely synchronized. 943 */ 944 ar->cmd_seq = -2; 945 946 err = carl9170_usb_send_rx_irq_urb(ar); 947 if (err) 948 goto err_out; 949 950 err = carl9170_usb_init_rx_bulk_urbs(ar); 951 if (err) 952 goto err_unrx; 953 954 err = carl9170_usb_open(ar); 955 if (err) 956 goto err_unrx; 957 958 mutex_lock(&ar->mutex); 959 err = carl9170_usb_load_firmware(ar); 960 mutex_unlock(&ar->mutex); 961 if (err) 962 goto err_stop; 963 964 return 0; 965 966 err_stop: 967 carl9170_usb_stop(ar); 968 969 err_unrx: 970 carl9170_usb_cancel_urbs(ar); 971 972 err_out: 973 return err; 974 } 975 976 static void carl9170_usb_firmware_failed(struct ar9170 *ar) 977 { 978 /* Store a copies of the usb_interface and usb_device pointer locally. 979 * This is because release_driver initiates carl9170_usb_disconnect, 980 * which in turn frees our driver context (ar). 981 */ 982 struct usb_interface *intf = ar->intf; 983 struct usb_device *udev = ar->udev; 984 985 complete(&ar->fw_load_wait); 986 /* at this point 'ar' could be already freed. Don't use it anymore */ 987 ar = NULL; 988 989 /* unbind anything failed */ 990 usb_lock_device(udev); 991 usb_driver_release_interface(&carl9170_driver, intf); 992 usb_unlock_device(udev); 993 994 usb_put_intf(intf); 995 } 996 997 static void carl9170_usb_firmware_finish(struct ar9170 *ar) 998 { 999 struct usb_interface *intf = ar->intf; 1000 int err; 1001 1002 err = carl9170_parse_firmware(ar); 1003 if (err) 1004 goto err_freefw; 1005 1006 err = carl9170_usb_init_device(ar); 1007 if (err) 1008 goto err_freefw; 1009 1010 err = carl9170_register(ar); 1011 1012 carl9170_usb_stop(ar); 1013 if (err) 1014 goto err_unrx; 1015 1016 complete(&ar->fw_load_wait); 1017 usb_put_intf(intf); 1018 return; 1019 1020 err_unrx: 1021 carl9170_usb_cancel_urbs(ar); 1022 1023 err_freefw: 1024 carl9170_release_firmware(ar); 1025 carl9170_usb_firmware_failed(ar); 1026 } 1027 1028 static void carl9170_usb_firmware_step2(const struct firmware *fw, 1029 void *context) 1030 { 1031 struct ar9170 *ar = context; 1032 1033 if (fw) { 1034 ar->fw.fw = fw; 1035 carl9170_usb_firmware_finish(ar); 1036 return; 1037 } 1038 1039 dev_err(&ar->udev->dev, "firmware not found.\n"); 1040 carl9170_usb_firmware_failed(ar); 1041 } 1042 1043 static int carl9170_usb_probe(struct usb_interface *intf, 1044 const struct usb_device_id *id) 1045 { 1046 struct usb_endpoint_descriptor *ep; 1047 struct ar9170 *ar; 1048 struct usb_device *udev; 1049 int i, err; 1050 1051 err = usb_reset_device(interface_to_usbdev(intf)); 1052 if (err) 1053 return err; 1054 1055 ar = carl9170_alloc(sizeof(*ar)); 1056 if (IS_ERR(ar)) 1057 return PTR_ERR(ar); 1058 1059 udev = interface_to_usbdev(intf); 1060 ar->udev = udev; 1061 ar->intf = intf; 1062 ar->features = id->driver_info; 1063 1064 /* We need to remember the type of endpoint 4 because it differs 1065 * between high- and full-speed configuration. The high-speed 1066 * configuration specifies it as interrupt and the full-speed 1067 * configuration as bulk endpoint. This information is required 1068 * later when sending urbs to that endpoint. 1069 */ 1070 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; ++i) { 1071 ep = &intf->cur_altsetting->endpoint[i].desc; 1072 1073 if (usb_endpoint_num(ep) == AR9170_USB_EP_CMD && 1074 usb_endpoint_dir_out(ep) && 1075 usb_endpoint_type(ep) == USB_ENDPOINT_XFER_BULK) 1076 ar->usb_ep_cmd_is_bulk = true; 1077 } 1078 1079 /* Verify that all expected endpoints are present */ 1080 if (ar->usb_ep_cmd_is_bulk) { 1081 u8 bulk_ep_addr[] = { 1082 AR9170_USB_EP_RX | USB_DIR_IN, 1083 AR9170_USB_EP_TX | USB_DIR_OUT, 1084 AR9170_USB_EP_CMD | USB_DIR_OUT, 1085 0}; 1086 u8 int_ep_addr[] = { 1087 AR9170_USB_EP_IRQ | USB_DIR_IN, 1088 0}; 1089 if (!usb_check_bulk_endpoints(intf, bulk_ep_addr) || 1090 !usb_check_int_endpoints(intf, int_ep_addr)) 1091 err = -ENODEV; 1092 } else { 1093 u8 bulk_ep_addr[] = { 1094 AR9170_USB_EP_RX | USB_DIR_IN, 1095 AR9170_USB_EP_TX | USB_DIR_OUT, 1096 0}; 1097 u8 int_ep_addr[] = { 1098 AR9170_USB_EP_IRQ | USB_DIR_IN, 1099 AR9170_USB_EP_CMD | USB_DIR_OUT, 1100 0}; 1101 if (!usb_check_bulk_endpoints(intf, bulk_ep_addr) || 1102 !usb_check_int_endpoints(intf, int_ep_addr)) 1103 err = -ENODEV; 1104 } 1105 1106 if (err) { 1107 carl9170_free(ar); 1108 return err; 1109 } 1110 1111 usb_set_intfdata(intf, ar); 1112 SET_IEEE80211_DEV(ar->hw, &intf->dev); 1113 1114 init_usb_anchor(&ar->rx_anch); 1115 init_usb_anchor(&ar->rx_pool); 1116 init_usb_anchor(&ar->rx_work); 1117 init_usb_anchor(&ar->tx_wait); 1118 init_usb_anchor(&ar->tx_anch); 1119 init_usb_anchor(&ar->tx_cmd); 1120 init_usb_anchor(&ar->tx_err); 1121 init_completion(&ar->cmd_wait); 1122 init_completion(&ar->fw_boot_wait); 1123 init_completion(&ar->fw_load_wait); 1124 tasklet_setup(&ar->usb_tasklet, carl9170_usb_tasklet); 1125 1126 atomic_set(&ar->tx_cmd_urbs, 0); 1127 atomic_set(&ar->tx_anch_urbs, 0); 1128 atomic_set(&ar->rx_work_urbs, 0); 1129 atomic_set(&ar->rx_anch_urbs, 0); 1130 atomic_set(&ar->rx_pool_urbs, 0); 1131 1132 usb_get_intf(intf); 1133 1134 carl9170_set_state(ar, CARL9170_STOPPED); 1135 1136 err = request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME, 1137 &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2); 1138 if (err) { 1139 usb_put_intf(intf); 1140 carl9170_free(ar); 1141 } 1142 return err; 1143 } 1144 1145 static void carl9170_usb_disconnect(struct usb_interface *intf) 1146 { 1147 struct ar9170 *ar = usb_get_intfdata(intf); 1148 1149 if (WARN_ON(!ar)) 1150 return; 1151 1152 wait_for_completion(&ar->fw_load_wait); 1153 1154 if (IS_INITIALIZED(ar)) { 1155 carl9170_reboot(ar); 1156 carl9170_usb_stop(ar); 1157 } 1158 1159 carl9170_usb_cancel_urbs(ar); 1160 carl9170_unregister(ar); 1161 1162 usb_set_intfdata(intf, NULL); 1163 1164 carl9170_release_firmware(ar); 1165 carl9170_free(ar); 1166 } 1167 1168 #ifdef CONFIG_PM 1169 static int carl9170_usb_suspend(struct usb_interface *intf, 1170 pm_message_t message) 1171 { 1172 struct ar9170 *ar = usb_get_intfdata(intf); 1173 1174 if (!ar) 1175 return -ENODEV; 1176 1177 carl9170_usb_cancel_urbs(ar); 1178 1179 return 0; 1180 } 1181 1182 static int carl9170_usb_resume(struct usb_interface *intf) 1183 { 1184 struct ar9170 *ar = usb_get_intfdata(intf); 1185 int err; 1186 1187 if (!ar) 1188 return -ENODEV; 1189 1190 usb_unpoison_anchored_urbs(&ar->rx_anch); 1191 carl9170_set_state(ar, CARL9170_STOPPED); 1192 1193 /* 1194 * The USB documentation demands that [for suspend] all traffic 1195 * to and from the device has to stop. This would be fine, but 1196 * there's a catch: the device[usb phy] does not come back. 1197 * 1198 * Upon resume the firmware will "kill" itself and the 1199 * boot-code sorts out the magic voodoo. 1200 * Not very nice, but there's not much what could go wrong. 1201 */ 1202 msleep(1100); 1203 1204 err = carl9170_usb_init_device(ar); 1205 if (err) 1206 goto err_unrx; 1207 1208 return 0; 1209 1210 err_unrx: 1211 carl9170_usb_cancel_urbs(ar); 1212 1213 return err; 1214 } 1215 #endif /* CONFIG_PM */ 1216 1217 static struct usb_driver carl9170_driver = { 1218 .name = KBUILD_MODNAME, 1219 .probe = carl9170_usb_probe, 1220 .disconnect = carl9170_usb_disconnect, 1221 .id_table = carl9170_usb_ids, 1222 .soft_unbind = 1, 1223 #ifdef CONFIG_PM 1224 .suspend = carl9170_usb_suspend, 1225 .resume = carl9170_usb_resume, 1226 .reset_resume = carl9170_usb_resume, 1227 #endif /* CONFIG_PM */ 1228 .disable_hub_initiated_lpm = 1, 1229 }; 1230 1231 module_usb_driver(carl9170_driver); 1232