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