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