linux/drivers/net/wireless/ath/ath9k/hif_usb.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2010-2011 Atheros Communications Inc.
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17#include <asm/unaligned.h>
  18#include "htc.h"
  19
  20MODULE_FIRMWARE(HTC_7010_MODULE_FW);
  21MODULE_FIRMWARE(HTC_9271_MODULE_FW);
  22
  23static struct usb_device_id ath9k_hif_usb_ids[] = {
  24        { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
  25        { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
  26        { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
  27        { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
  28        { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
  29        { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
  30        { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
  31        { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
  32        { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
  33        { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
  34        { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
  35        { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
  36        { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
  37        { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
  38        { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
  39        { USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
  40
  41        { USB_DEVICE(0x0cf3, 0x7015),
  42          .driver_info = AR9287_USB },  /* Atheros */
  43        { USB_DEVICE(0x1668, 0x1200),
  44          .driver_info = AR9287_USB },  /* Verizon */
  45
  46        { USB_DEVICE(0x0cf3, 0x7010),
  47          .driver_info = AR9280_USB },  /* Atheros */
  48        { USB_DEVICE(0x0846, 0x9018),
  49          .driver_info = AR9280_USB },  /* Netgear WNDA3200 */
  50        { USB_DEVICE(0x083A, 0xA704),
  51          .driver_info = AR9280_USB },  /* SMC Networks */
  52        { USB_DEVICE(0x0411, 0x017f),
  53          .driver_info = AR9280_USB },  /* Sony UWA-BR100 */
  54        { USB_DEVICE(0x0411, 0x0197),
  55          .driver_info = AR9280_USB },  /* Buffalo WLI-UV-AG300P */
  56        { USB_DEVICE(0x04da, 0x3904),
  57          .driver_info = AR9280_USB },
  58
  59        { USB_DEVICE(0x0cf3, 0x20ff),
  60          .driver_info = STORAGE_DEVICE },
  61
  62        { },
  63};
  64
  65MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
  66
  67static int __hif_usb_tx(struct hif_device_usb *hif_dev);
  68
  69static void hif_usb_regout_cb(struct urb *urb)
  70{
  71        struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
  72
  73        switch (urb->status) {
  74        case 0:
  75                break;
  76        case -ENOENT:
  77        case -ECONNRESET:
  78        case -ENODEV:
  79        case -ESHUTDOWN:
  80                goto free;
  81        default:
  82                break;
  83        }
  84
  85        if (cmd) {
  86                ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
  87                                          cmd->skb, true);
  88                kfree(cmd);
  89        }
  90
  91        return;
  92free:
  93        kfree_skb(cmd->skb);
  94        kfree(cmd);
  95}
  96
  97static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
  98                               struct sk_buff *skb)
  99{
 100        struct urb *urb;
 101        struct cmd_buf *cmd;
 102        int ret = 0;
 103
 104        urb = usb_alloc_urb(0, GFP_KERNEL);
 105        if (urb == NULL)
 106                return -ENOMEM;
 107
 108        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 109        if (cmd == NULL) {
 110                usb_free_urb(urb);
 111                return -ENOMEM;
 112        }
 113
 114        cmd->skb = skb;
 115        cmd->hif_dev = hif_dev;
 116
 117        usb_fill_int_urb(urb, hif_dev->udev,
 118                         usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
 119                         skb->data, skb->len,
 120                         hif_usb_regout_cb, cmd, 1);
 121
 122        usb_anchor_urb(urb, &hif_dev->regout_submitted);
 123        ret = usb_submit_urb(urb, GFP_KERNEL);
 124        if (ret) {
 125                usb_unanchor_urb(urb);
 126                kfree(cmd);
 127        }
 128        usb_free_urb(urb);
 129
 130        return ret;
 131}
 132
 133static void hif_usb_mgmt_cb(struct urb *urb)
 134{
 135        struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
 136        struct hif_device_usb *hif_dev;
 137        bool txok = true;
 138
 139        if (!cmd || !cmd->skb || !cmd->hif_dev)
 140                return;
 141
 142        hif_dev = cmd->hif_dev;
 143
 144        switch (urb->status) {
 145        case 0:
 146                break;
 147        case -ENOENT:
 148        case -ECONNRESET:
 149        case -ENODEV:
 150        case -ESHUTDOWN:
 151                txok = false;
 152
 153                /*
 154                 * If the URBs are being flushed, no need to complete
 155                 * this packet.
 156                 */
 157                spin_lock(&hif_dev->tx.tx_lock);
 158                if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
 159                        spin_unlock(&hif_dev->tx.tx_lock);
 160                        dev_kfree_skb_any(cmd->skb);
 161                        kfree(cmd);
 162                        return;
 163                }
 164                spin_unlock(&hif_dev->tx.tx_lock);
 165
 166                break;
 167        default:
 168                txok = false;
 169                break;
 170        }
 171
 172        skb_pull(cmd->skb, 4);
 173        ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
 174                                  cmd->skb, txok);
 175        kfree(cmd);
 176}
 177
 178static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
 179                             struct sk_buff *skb)
 180{
 181        struct urb *urb;
 182        struct cmd_buf *cmd;
 183        int ret = 0;
 184        __le16 *hdr;
 185
 186        urb = usb_alloc_urb(0, GFP_ATOMIC);
 187        if (urb == NULL)
 188                return -ENOMEM;
 189
 190        cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
 191        if (cmd == NULL) {
 192                usb_free_urb(urb);
 193                return -ENOMEM;
 194        }
 195
 196        cmd->skb = skb;
 197        cmd->hif_dev = hif_dev;
 198
 199        hdr = (__le16 *) skb_push(skb, 4);
 200        *hdr++ = cpu_to_le16(skb->len - 4);
 201        *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
 202
 203        usb_fill_bulk_urb(urb, hif_dev->udev,
 204                         usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
 205                         skb->data, skb->len,
 206                         hif_usb_mgmt_cb, cmd);
 207
 208        usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
 209        ret = usb_submit_urb(urb, GFP_ATOMIC);
 210        if (ret) {
 211                usb_unanchor_urb(urb);
 212                kfree(cmd);
 213        }
 214        usb_free_urb(urb);
 215
 216        return ret;
 217}
 218
 219static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
 220                                         struct sk_buff_head *list)
 221{
 222        struct sk_buff *skb;
 223
 224        while ((skb = __skb_dequeue(list)) != NULL) {
 225                dev_kfree_skb_any(skb);
 226        }
 227}
 228
 229static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
 230                                            struct sk_buff_head *queue,
 231                                            bool txok)
 232{
 233        struct sk_buff *skb;
 234
 235        while ((skb = __skb_dequeue(queue)) != NULL) {
 236#ifdef CONFIG_ATH9K_HTC_DEBUGFS
 237                int ln = skb->len;
 238#endif
 239                ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
 240                                          skb, txok);
 241                if (txok) {
 242                        TX_STAT_INC(skb_success);
 243                        TX_STAT_ADD(skb_success_bytes, ln);
 244                }
 245                else
 246                        TX_STAT_INC(skb_failed);
 247        }
 248}
 249
 250static void hif_usb_tx_cb(struct urb *urb)
 251{
 252        struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
 253        struct hif_device_usb *hif_dev;
 254        bool txok = true;
 255
 256        if (!tx_buf || !tx_buf->hif_dev)
 257                return;
 258
 259        hif_dev = tx_buf->hif_dev;
 260
 261        switch (urb->status) {
 262        case 0:
 263                break;
 264        case -ENOENT:
 265        case -ECONNRESET:
 266        case -ENODEV:
 267        case -ESHUTDOWN:
 268                txok = false;
 269
 270                /*
 271                 * If the URBs are being flushed, no need to add this
 272                 * URB to the free list.
 273                 */
 274                spin_lock(&hif_dev->tx.tx_lock);
 275                if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
 276                        spin_unlock(&hif_dev->tx.tx_lock);
 277                        ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
 278                        return;
 279                }
 280                spin_unlock(&hif_dev->tx.tx_lock);
 281
 282                break;
 283        default:
 284                txok = false;
 285                break;
 286        }
 287
 288        ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
 289
 290        /* Re-initialize the SKB queue */
 291        tx_buf->len = tx_buf->offset = 0;
 292        __skb_queue_head_init(&tx_buf->skb_queue);
 293
 294        /* Add this TX buffer to the free list */
 295        spin_lock(&hif_dev->tx.tx_lock);
 296        list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
 297        hif_dev->tx.tx_buf_cnt++;
 298        if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
 299                __hif_usb_tx(hif_dev); /* Check for pending SKBs */
 300        TX_STAT_INC(buf_completed);
 301        spin_unlock(&hif_dev->tx.tx_lock);
 302}
 303
 304/* TX lock has to be taken */
 305static int __hif_usb_tx(struct hif_device_usb *hif_dev)
 306{
 307        struct tx_buf *tx_buf = NULL;
 308        struct sk_buff *nskb = NULL;
 309        int ret = 0, i;
 310        u16 tx_skb_cnt = 0;
 311        u8 *buf;
 312        __le16 *hdr;
 313
 314        if (hif_dev->tx.tx_skb_cnt == 0)
 315                return 0;
 316
 317        /* Check if a free TX buffer is available */
 318        if (list_empty(&hif_dev->tx.tx_buf))
 319                return 0;
 320
 321        tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
 322        list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
 323        hif_dev->tx.tx_buf_cnt--;
 324
 325        tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
 326
 327        for (i = 0; i < tx_skb_cnt; i++) {
 328                nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
 329
 330                /* Should never be NULL */
 331                BUG_ON(!nskb);
 332
 333                hif_dev->tx.tx_skb_cnt--;
 334
 335                buf = tx_buf->buf;
 336                buf += tx_buf->offset;
 337                hdr = (__le16 *)buf;
 338                *hdr++ = cpu_to_le16(nskb->len);
 339                *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
 340                buf += 4;
 341                memcpy(buf, nskb->data, nskb->len);
 342                tx_buf->len = nskb->len + 4;
 343
 344                if (i < (tx_skb_cnt - 1))
 345                        tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
 346
 347                if (i == (tx_skb_cnt - 1))
 348                        tx_buf->len += tx_buf->offset;
 349
 350                __skb_queue_tail(&tx_buf->skb_queue, nskb);
 351                TX_STAT_INC(skb_queued);
 352        }
 353
 354        usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
 355                          usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
 356                          tx_buf->buf, tx_buf->len,
 357                          hif_usb_tx_cb, tx_buf);
 358
 359        ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
 360        if (ret) {
 361                tx_buf->len = tx_buf->offset = 0;
 362                ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
 363                __skb_queue_head_init(&tx_buf->skb_queue);
 364                list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
 365                hif_dev->tx.tx_buf_cnt++;
 366        }
 367
 368        if (!ret)
 369                TX_STAT_INC(buf_queued);
 370
 371        return ret;
 372}
 373
 374static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
 375{
 376        struct ath9k_htc_tx_ctl *tx_ctl;
 377        unsigned long flags;
 378        int ret = 0;
 379
 380        spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
 381
 382        if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
 383                spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 384                return -ENODEV;
 385        }
 386
 387        /* Check if the max queue count has been reached */
 388        if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
 389                spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 390                return -ENOMEM;
 391        }
 392
 393        spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 394
 395        tx_ctl = HTC_SKB_CB(skb);
 396
 397        /* Mgmt/Beacon frames don't use the TX buffer pool */
 398        if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
 399            (tx_ctl->type == ATH9K_HTC_BEACON)) {
 400                ret = hif_usb_send_mgmt(hif_dev, skb);
 401        }
 402
 403        spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
 404
 405        if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
 406            (tx_ctl->type == ATH9K_HTC_AMPDU)) {
 407                __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
 408                hif_dev->tx.tx_skb_cnt++;
 409        }
 410
 411        /* Check if AMPDUs have to be sent immediately */
 412        if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
 413            (hif_dev->tx.tx_skb_cnt < 2)) {
 414                __hif_usb_tx(hif_dev);
 415        }
 416
 417        spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 418
 419        return ret;
 420}
 421
 422static void hif_usb_start(void *hif_handle)
 423{
 424        struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
 425        unsigned long flags;
 426
 427        hif_dev->flags |= HIF_USB_START;
 428
 429        spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
 430        hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
 431        spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 432}
 433
 434static void hif_usb_stop(void *hif_handle)
 435{
 436        struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
 437        struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
 438        unsigned long flags;
 439
 440        spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
 441        ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
 442        hif_dev->tx.tx_skb_cnt = 0;
 443        hif_dev->tx.flags |= HIF_USB_TX_STOP;
 444        spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 445
 446        /* The pending URBs have to be canceled. */
 447        list_for_each_entry_safe(tx_buf, tx_buf_tmp,
 448                                 &hif_dev->tx.tx_pending, list) {
 449                usb_kill_urb(tx_buf->urb);
 450        }
 451
 452        usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
 453}
 454
 455static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
 456{
 457        struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
 458        int ret = 0;
 459
 460        switch (pipe_id) {
 461        case USB_WLAN_TX_PIPE:
 462                ret = hif_usb_send_tx(hif_dev, skb);
 463                break;
 464        case USB_REG_OUT_PIPE:
 465                ret = hif_usb_send_regout(hif_dev, skb);
 466                break;
 467        default:
 468                dev_err(&hif_dev->udev->dev,
 469                        "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
 470                ret = -EINVAL;
 471                break;
 472        }
 473
 474        return ret;
 475}
 476
 477static inline bool check_index(struct sk_buff *skb, u8 idx)
 478{
 479        struct ath9k_htc_tx_ctl *tx_ctl;
 480
 481        tx_ctl = HTC_SKB_CB(skb);
 482
 483        if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
 484            (tx_ctl->sta_idx == idx))
 485                return true;
 486
 487        return false;
 488}
 489
 490static void hif_usb_sta_drain(void *hif_handle, u8 idx)
 491{
 492        struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
 493        struct sk_buff *skb, *tmp;
 494        unsigned long flags;
 495
 496        spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
 497
 498        skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
 499                if (check_index(skb, idx)) {
 500                        __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
 501                        ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
 502                                                  skb, false);
 503                        hif_dev->tx.tx_skb_cnt--;
 504                        TX_STAT_INC(skb_failed);
 505                }
 506        }
 507
 508        spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 509}
 510
 511static struct ath9k_htc_hif hif_usb = {
 512        .transport = ATH9K_HIF_USB,
 513        .name = "ath9k_hif_usb",
 514
 515        .control_ul_pipe = USB_REG_OUT_PIPE,
 516        .control_dl_pipe = USB_REG_IN_PIPE,
 517
 518        .start = hif_usb_start,
 519        .stop = hif_usb_stop,
 520        .sta_drain = hif_usb_sta_drain,
 521        .send = hif_usb_send,
 522};
 523
 524static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
 525                                    struct sk_buff *skb)
 526{
 527        struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
 528        int index = 0, i = 0, len = skb->len;
 529        int rx_remain_len, rx_pkt_len;
 530        u16 pool_index = 0;
 531        u8 *ptr;
 532
 533        spin_lock(&hif_dev->rx_lock);
 534
 535        rx_remain_len = hif_dev->rx_remain_len;
 536        rx_pkt_len = hif_dev->rx_transfer_len;
 537
 538        if (rx_remain_len != 0) {
 539                struct sk_buff *remain_skb = hif_dev->remain_skb;
 540
 541                if (remain_skb) {
 542                        ptr = (u8 *) remain_skb->data;
 543
 544                        index = rx_remain_len;
 545                        rx_remain_len -= hif_dev->rx_pad_len;
 546                        ptr += rx_pkt_len;
 547
 548                        memcpy(ptr, skb->data, rx_remain_len);
 549
 550                        rx_pkt_len += rx_remain_len;
 551                        hif_dev->rx_remain_len = 0;
 552                        skb_put(remain_skb, rx_pkt_len);
 553
 554                        skb_pool[pool_index++] = remain_skb;
 555
 556                } else {
 557                        index = rx_remain_len;
 558                }
 559        }
 560
 561        spin_unlock(&hif_dev->rx_lock);
 562
 563        while (index < len) {
 564                u16 pkt_len;
 565                u16 pkt_tag;
 566                u16 pad_len;
 567                int chk_idx;
 568
 569                ptr = (u8 *) skb->data;
 570
 571                pkt_len = get_unaligned_le16(ptr + index);
 572                pkt_tag = get_unaligned_le16(ptr + index + 2);
 573
 574                if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
 575                        RX_STAT_INC(skb_dropped);
 576                        return;
 577                }
 578
 579                pad_len = 4 - (pkt_len & 0x3);
 580                if (pad_len == 4)
 581                        pad_len = 0;
 582
 583                chk_idx = index;
 584                index = index + 4 + pkt_len + pad_len;
 585
 586                if (index > MAX_RX_BUF_SIZE) {
 587                        spin_lock(&hif_dev->rx_lock);
 588                        hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
 589                        hif_dev->rx_transfer_len =
 590                                MAX_RX_BUF_SIZE - chk_idx - 4;
 591                        hif_dev->rx_pad_len = pad_len;
 592
 593                        nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
 594                        if (!nskb) {
 595                                dev_err(&hif_dev->udev->dev,
 596                                        "ath9k_htc: RX memory allocation error\n");
 597                                spin_unlock(&hif_dev->rx_lock);
 598                                goto err;
 599                        }
 600                        skb_reserve(nskb, 32);
 601                        RX_STAT_INC(skb_allocated);
 602
 603                        memcpy(nskb->data, &(skb->data[chk_idx+4]),
 604                               hif_dev->rx_transfer_len);
 605
 606                        /* Record the buffer pointer */
 607                        hif_dev->remain_skb = nskb;
 608                        spin_unlock(&hif_dev->rx_lock);
 609                } else {
 610                        nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
 611                        if (!nskb) {
 612                                dev_err(&hif_dev->udev->dev,
 613                                        "ath9k_htc: RX memory allocation error\n");
 614                                goto err;
 615                        }
 616                        skb_reserve(nskb, 32);
 617                        RX_STAT_INC(skb_allocated);
 618
 619                        memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
 620                        skb_put(nskb, pkt_len);
 621                        skb_pool[pool_index++] = nskb;
 622                }
 623        }
 624
 625err:
 626        for (i = 0; i < pool_index; i++) {
 627                RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len);
 628                ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
 629                                 skb_pool[i]->len, USB_WLAN_RX_PIPE);
 630                RX_STAT_INC(skb_completed);
 631        }
 632}
 633
 634static void ath9k_hif_usb_rx_cb(struct urb *urb)
 635{
 636        struct sk_buff *skb = (struct sk_buff *) urb->context;
 637        struct hif_device_usb *hif_dev =
 638                usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
 639        int ret;
 640
 641        if (!skb)
 642                return;
 643
 644        if (!hif_dev)
 645                goto free;
 646
 647        switch (urb->status) {
 648        case 0:
 649                break;
 650        case -ENOENT:
 651        case -ECONNRESET:
 652        case -ENODEV:
 653        case -ESHUTDOWN:
 654                goto free;
 655        default:
 656                goto resubmit;
 657        }
 658
 659        if (likely(urb->actual_length != 0)) {
 660                skb_put(skb, urb->actual_length);
 661                ath9k_hif_usb_rx_stream(hif_dev, skb);
 662        }
 663
 664resubmit:
 665        skb_reset_tail_pointer(skb);
 666        skb_trim(skb, 0);
 667
 668        usb_anchor_urb(urb, &hif_dev->rx_submitted);
 669        ret = usb_submit_urb(urb, GFP_ATOMIC);
 670        if (ret) {
 671                usb_unanchor_urb(urb);
 672                goto free;
 673        }
 674
 675        return;
 676free:
 677        kfree_skb(skb);
 678}
 679
 680static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
 681{
 682        struct sk_buff *skb = (struct sk_buff *) urb->context;
 683        struct sk_buff *nskb;
 684        struct hif_device_usb *hif_dev =
 685                usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
 686        int ret;
 687
 688        if (!skb)
 689                return;
 690
 691        if (!hif_dev)
 692                goto free;
 693
 694        switch (urb->status) {
 695        case 0:
 696                break;
 697        case -ENOENT:
 698        case -ECONNRESET:
 699        case -ENODEV:
 700        case -ESHUTDOWN:
 701                goto free;
 702        default:
 703                skb_reset_tail_pointer(skb);
 704                skb_trim(skb, 0);
 705
 706                goto resubmit;
 707        }
 708
 709        if (likely(urb->actual_length != 0)) {
 710                skb_put(skb, urb->actual_length);
 711
 712                /* Process the command first */
 713                ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
 714                                 skb->len, USB_REG_IN_PIPE);
 715
 716
 717                nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
 718                if (!nskb) {
 719                        dev_err(&hif_dev->udev->dev,
 720                                "ath9k_htc: REG_IN memory allocation failure\n");
 721                        urb->context = NULL;
 722                        return;
 723                }
 724
 725                usb_fill_int_urb(urb, hif_dev->udev,
 726                                 usb_rcvintpipe(hif_dev->udev,
 727                                                 USB_REG_IN_PIPE),
 728                                 nskb->data, MAX_REG_IN_BUF_SIZE,
 729                                 ath9k_hif_usb_reg_in_cb, nskb, 1);
 730        }
 731
 732resubmit:
 733        usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
 734        ret = usb_submit_urb(urb, GFP_ATOMIC);
 735        if (ret) {
 736                usb_unanchor_urb(urb);
 737                goto free;
 738        }
 739
 740        return;
 741free:
 742        kfree_skb(skb);
 743        urb->context = NULL;
 744}
 745
 746static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
 747{
 748        struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
 749        unsigned long flags;
 750
 751        list_for_each_entry_safe(tx_buf, tx_buf_tmp,
 752                                 &hif_dev->tx.tx_buf, list) {
 753                usb_kill_urb(tx_buf->urb);
 754                list_del(&tx_buf->list);
 755                usb_free_urb(tx_buf->urb);
 756                kfree(tx_buf->buf);
 757                kfree(tx_buf);
 758        }
 759
 760        spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
 761        hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
 762        spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
 763
 764        list_for_each_entry_safe(tx_buf, tx_buf_tmp,
 765                                 &hif_dev->tx.tx_pending, list) {
 766                usb_kill_urb(tx_buf->urb);
 767                list_del(&tx_buf->list);
 768                usb_free_urb(tx_buf->urb);
 769                kfree(tx_buf->buf);
 770                kfree(tx_buf);
 771        }
 772
 773        usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
 774}
 775
 776static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
 777{
 778        struct tx_buf *tx_buf;
 779        int i;
 780
 781        INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
 782        INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
 783        spin_lock_init(&hif_dev->tx.tx_lock);
 784        __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
 785        init_usb_anchor(&hif_dev->mgmt_submitted);
 786
 787        for (i = 0; i < MAX_TX_URB_NUM; i++) {
 788                tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
 789                if (!tx_buf)
 790                        goto err;
 791
 792                tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
 793                if (!tx_buf->buf)
 794                        goto err;
 795
 796                tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
 797                if (!tx_buf->urb)
 798                        goto err;
 799
 800                tx_buf->hif_dev = hif_dev;
 801                __skb_queue_head_init(&tx_buf->skb_queue);
 802
 803                list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
 804        }
 805
 806        hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
 807
 808        return 0;
 809err:
 810        if (tx_buf) {
 811                kfree(tx_buf->buf);
 812                kfree(tx_buf);
 813        }
 814        ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
 815        return -ENOMEM;
 816}
 817
 818static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
 819{
 820        usb_kill_anchored_urbs(&hif_dev->rx_submitted);
 821}
 822
 823static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
 824{
 825        struct urb *urb = NULL;
 826        struct sk_buff *skb = NULL;
 827        int i, ret;
 828
 829        init_usb_anchor(&hif_dev->rx_submitted);
 830        spin_lock_init(&hif_dev->rx_lock);
 831
 832        for (i = 0; i < MAX_RX_URB_NUM; i++) {
 833
 834                /* Allocate URB */
 835                urb = usb_alloc_urb(0, GFP_KERNEL);
 836                if (urb == NULL) {
 837                        ret = -ENOMEM;
 838                        goto err_urb;
 839                }
 840
 841                /* Allocate buffer */
 842                skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
 843                if (!skb) {
 844                        ret = -ENOMEM;
 845                        goto err_skb;
 846                }
 847
 848                usb_fill_bulk_urb(urb, hif_dev->udev,
 849                                  usb_rcvbulkpipe(hif_dev->udev,
 850                                                  USB_WLAN_RX_PIPE),
 851                                  skb->data, MAX_RX_BUF_SIZE,
 852                                  ath9k_hif_usb_rx_cb, skb);
 853
 854                /* Anchor URB */
 855                usb_anchor_urb(urb, &hif_dev->rx_submitted);
 856
 857                /* Submit URB */
 858                ret = usb_submit_urb(urb, GFP_KERNEL);
 859                if (ret) {
 860                        usb_unanchor_urb(urb);
 861                        goto err_submit;
 862                }
 863
 864                /*
 865                 * Drop reference count.
 866                 * This ensures that the URB is freed when killing them.
 867                 */
 868                usb_free_urb(urb);
 869        }
 870
 871        return 0;
 872
 873err_submit:
 874        kfree_skb(skb);
 875err_skb:
 876        usb_free_urb(urb);
 877err_urb:
 878        ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
 879        return ret;
 880}
 881
 882static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
 883{
 884        usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
 885}
 886
 887static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
 888{
 889        struct urb *urb = NULL;
 890        struct sk_buff *skb = NULL;
 891        int i, ret;
 892
 893        init_usb_anchor(&hif_dev->reg_in_submitted);
 894
 895        for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
 896
 897                /* Allocate URB */
 898                urb = usb_alloc_urb(0, GFP_KERNEL);
 899                if (urb == NULL) {
 900                        ret = -ENOMEM;
 901                        goto err_urb;
 902                }
 903
 904                /* Allocate buffer */
 905                skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
 906                if (!skb) {
 907                        ret = -ENOMEM;
 908                        goto err_skb;
 909                }
 910
 911                usb_fill_int_urb(urb, hif_dev->udev,
 912                                  usb_rcvintpipe(hif_dev->udev,
 913                                                  USB_REG_IN_PIPE),
 914                                  skb->data, MAX_REG_IN_BUF_SIZE,
 915                                  ath9k_hif_usb_reg_in_cb, skb, 1);
 916
 917                /* Anchor URB */
 918                usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
 919
 920                /* Submit URB */
 921                ret = usb_submit_urb(urb, GFP_KERNEL);
 922                if (ret) {
 923                        usb_unanchor_urb(urb);
 924                        goto err_submit;
 925                }
 926
 927                /*
 928                 * Drop reference count.
 929                 * This ensures that the URB is freed when killing them.
 930                 */
 931                usb_free_urb(urb);
 932        }
 933
 934        return 0;
 935
 936err_submit:
 937        kfree_skb(skb);
 938err_skb:
 939        usb_free_urb(urb);
 940err_urb:
 941        ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
 942        return ret;
 943}
 944
 945static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
 946{
 947        /* Register Write */
 948        init_usb_anchor(&hif_dev->regout_submitted);
 949
 950        /* TX */
 951        if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
 952                goto err;
 953
 954        /* RX */
 955        if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
 956                goto err_rx;
 957
 958        /* Register Read */
 959        if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
 960                goto err_reg;
 961
 962        return 0;
 963err_reg:
 964        ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
 965err_rx:
 966        ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
 967err:
 968        return -ENOMEM;
 969}
 970
 971static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
 972{
 973        usb_kill_anchored_urbs(&hif_dev->regout_submitted);
 974        ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
 975        ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
 976        ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
 977}
 978
 979static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
 980{
 981        int transfer, err;
 982        const void *data = hif_dev->fw_data;
 983        size_t len = hif_dev->fw_size;
 984        u32 addr = AR9271_FIRMWARE;
 985        u8 *buf = kzalloc(4096, GFP_KERNEL);
 986        u32 firm_offset;
 987
 988        if (!buf)
 989                return -ENOMEM;
 990
 991        while (len) {
 992                transfer = min_t(size_t, len, 4096);
 993                memcpy(buf, data, transfer);
 994
 995                err = usb_control_msg(hif_dev->udev,
 996                                      usb_sndctrlpipe(hif_dev->udev, 0),
 997                                      FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
 998                                      addr >> 8, 0, buf, transfer, HZ);
 999                if (err < 0) {
1000                        kfree(buf);
1001                        return err;
1002                }
1003
1004                len -= transfer;
1005                data += transfer;
1006                addr += transfer;
1007        }
1008        kfree(buf);
1009
1010        if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1011                firm_offset = AR7010_FIRMWARE_TEXT;
1012        else
1013                firm_offset = AR9271_FIRMWARE_TEXT;
1014
1015        /*
1016         * Issue FW download complete command to firmware.
1017         */
1018        err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1019                              FIRMWARE_DOWNLOAD_COMP,
1020                              0x40 | USB_DIR_OUT,
1021                              firm_offset >> 8, 0, NULL, 0, HZ);
1022        if (err)
1023                return -EIO;
1024
1025        dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1026                 hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
1027
1028        return 0;
1029}
1030
1031static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
1032{
1033        int ret;
1034
1035        ret = ath9k_hif_usb_download_fw(hif_dev);
1036        if (ret) {
1037                dev_err(&hif_dev->udev->dev,
1038                        "ath9k_htc: Firmware - %s download failed\n",
1039                        hif_dev->fw_name);
1040                return ret;
1041        }
1042
1043        /* Alloc URBs */
1044        ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1045        if (ret) {
1046                dev_err(&hif_dev->udev->dev,
1047                        "ath9k_htc: Unable to allocate URBs\n");
1048                return ret;
1049        }
1050
1051        return 0;
1052}
1053
1054static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1055{
1056        ath9k_hif_usb_dealloc_urbs(hif_dev);
1057}
1058
1059/*
1060 * If initialization fails or the FW cannot be retrieved,
1061 * detach the device.
1062 */
1063static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
1064{
1065        struct device *dev = &hif_dev->udev->dev;
1066        struct device *parent = dev->parent;
1067
1068        complete_all(&hif_dev->fw_done);
1069
1070        if (parent)
1071                device_lock(parent);
1072
1073        device_release_driver(dev);
1074
1075        if (parent)
1076                device_unlock(parent);
1077}
1078
1079static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
1080
1081/* taken from iwlwifi */
1082static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
1083                                      bool first)
1084{
1085        char index[8], *chip;
1086        int ret;
1087
1088        if (first) {
1089                if (htc_use_dev_fw) {
1090                        hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
1091                        sprintf(index, "%s", "dev");
1092                } else {
1093                        hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
1094                        sprintf(index, "%d", hif_dev->fw_minor_index);
1095                }
1096        } else {
1097                hif_dev->fw_minor_index--;
1098                sprintf(index, "%d", hif_dev->fw_minor_index);
1099        }
1100
1101        /* test for FW 1.3 */
1102        if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
1103                const char *filename;
1104
1105                if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1106                        filename = FIRMWARE_AR7010_1_1;
1107                else
1108                        filename = FIRMWARE_AR9271;
1109
1110                /* expected fw locations:
1111                 * - htc_9271.fw   (stable version 1.3, depricated)
1112                 */
1113                snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1114                         "%s", filename);
1115
1116        } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
1117                dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
1118
1119                return -ENOENT;
1120        } else {
1121                if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1122                        chip = "7010";
1123                else
1124                        chip = "9271";
1125
1126                /* expected fw locations:
1127                 * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
1128                 * - ath9k_htc/htc_9271-1.4.0.fw   (stable version)
1129                 */
1130                snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1131                         "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
1132                         chip, MAJOR_VERSION_REQ, index);
1133        }
1134
1135        ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
1136                                      &hif_dev->udev->dev, GFP_KERNEL,
1137                                      hif_dev, ath9k_hif_usb_firmware_cb);
1138        if (ret) {
1139                dev_err(&hif_dev->udev->dev,
1140                        "ath9k_htc: Async request for firmware %s failed\n",
1141                        hif_dev->fw_name);
1142                return ret;
1143        }
1144
1145        dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
1146                 hif_dev->fw_name);
1147
1148        return ret;
1149}
1150
1151static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
1152{
1153        struct hif_device_usb *hif_dev = context;
1154        int ret;
1155
1156        if (!fw) {
1157                ret = ath9k_hif_request_firmware(hif_dev, false);
1158                if (!ret)
1159                        return;
1160
1161                dev_err(&hif_dev->udev->dev,
1162                        "ath9k_htc: Failed to get firmware %s\n",
1163                        hif_dev->fw_name);
1164                goto err_fw;
1165        }
1166
1167        hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1168                                                 &hif_dev->udev->dev);
1169        if (hif_dev->htc_handle == NULL)
1170                goto err_dev_alloc;
1171
1172        hif_dev->fw_data = fw->data;
1173        hif_dev->fw_size = fw->size;
1174
1175        /* Proceed with initialization */
1176
1177        ret = ath9k_hif_usb_dev_init(hif_dev);
1178        if (ret)
1179                goto err_dev_init;
1180
1181        ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1182                                &hif_dev->interface->dev,
1183                                hif_dev->usb_device_id->idProduct,
1184                                hif_dev->udev->product,
1185                                hif_dev->usb_device_id->driver_info);
1186        if (ret) {
1187                ret = -EINVAL;
1188                goto err_htc_hw_init;
1189        }
1190
1191        release_firmware(fw);
1192        hif_dev->flags |= HIF_USB_READY;
1193        complete_all(&hif_dev->fw_done);
1194
1195        return;
1196
1197err_htc_hw_init:
1198        ath9k_hif_usb_dev_deinit(hif_dev);
1199err_dev_init:
1200        ath9k_htc_hw_free(hif_dev->htc_handle);
1201err_dev_alloc:
1202        release_firmware(fw);
1203err_fw:
1204        ath9k_hif_usb_firmware_fail(hif_dev);
1205}
1206
1207/*
1208 * An exact copy of the function from zd1211rw.
1209 */
1210static int send_eject_command(struct usb_interface *interface)
1211{
1212        struct usb_device *udev = interface_to_usbdev(interface);
1213        struct usb_host_interface *iface_desc = &interface->altsetting[0];
1214        struct usb_endpoint_descriptor *endpoint;
1215        unsigned char *cmd;
1216        u8 bulk_out_ep;
1217        int r;
1218
1219        /* Find bulk out endpoint */
1220        for (r = 1; r >= 0; r--) {
1221                endpoint = &iface_desc->endpoint[r].desc;
1222                if (usb_endpoint_dir_out(endpoint) &&
1223                    usb_endpoint_xfer_bulk(endpoint)) {
1224                        bulk_out_ep = endpoint->bEndpointAddress;
1225                        break;
1226                }
1227        }
1228        if (r == -1) {
1229                dev_err(&udev->dev,
1230                        "ath9k_htc: Could not find bulk out endpoint\n");
1231                return -ENODEV;
1232        }
1233
1234        cmd = kzalloc(31, GFP_KERNEL);
1235        if (cmd == NULL)
1236                return -ENODEV;
1237
1238        /* USB bulk command block */
1239        cmd[0] = 0x55;  /* bulk command signature */
1240        cmd[1] = 0x53;  /* bulk command signature */
1241        cmd[2] = 0x42;  /* bulk command signature */
1242        cmd[3] = 0x43;  /* bulk command signature */
1243        cmd[14] = 6;    /* command length */
1244
1245        cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1246        cmd[19] = 0x2;  /* eject disc */
1247
1248        dev_info(&udev->dev, "Ejecting storage device...\n");
1249        r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1250                cmd, 31, NULL, 2000);
1251        kfree(cmd);
1252        if (r)
1253                return r;
1254
1255        /* At this point, the device disconnects and reconnects with the real
1256         * ID numbers. */
1257
1258        usb_set_intfdata(interface, NULL);
1259        return 0;
1260}
1261
1262static int ath9k_hif_usb_probe(struct usb_interface *interface,
1263                               const struct usb_device_id *id)
1264{
1265        struct usb_device *udev = interface_to_usbdev(interface);
1266        struct hif_device_usb *hif_dev;
1267        int ret = 0;
1268
1269        if (id->driver_info == STORAGE_DEVICE)
1270                return send_eject_command(interface);
1271
1272        hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1273        if (!hif_dev) {
1274                ret = -ENOMEM;
1275                goto err_alloc;
1276        }
1277
1278        usb_get_dev(udev);
1279
1280        hif_dev->udev = udev;
1281        hif_dev->interface = interface;
1282        hif_dev->usb_device_id = id;
1283#ifdef CONFIG_PM
1284        udev->reset_resume = 1;
1285#endif
1286        usb_set_intfdata(interface, hif_dev);
1287
1288        init_completion(&hif_dev->fw_done);
1289
1290        ret = ath9k_hif_request_firmware(hif_dev, true);
1291        if (ret)
1292                goto err_fw_req;
1293
1294        return ret;
1295
1296err_fw_req:
1297        usb_set_intfdata(interface, NULL);
1298        kfree(hif_dev);
1299        usb_put_dev(udev);
1300err_alloc:
1301        return ret;
1302}
1303
1304static void ath9k_hif_usb_reboot(struct usb_device *udev)
1305{
1306        u32 reboot_cmd = 0xffffffff;
1307        void *buf;
1308        int ret;
1309
1310        buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1311        if (!buf)
1312                return;
1313
1314        ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
1315                           buf, 4, NULL, HZ);
1316        if (ret)
1317                dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1318
1319        kfree(buf);
1320}
1321
1322static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1323{
1324        struct usb_device *udev = interface_to_usbdev(interface);
1325        struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1326        bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1327
1328        if (!hif_dev)
1329                return;
1330
1331        wait_for_completion(&hif_dev->fw_done);
1332
1333        if (hif_dev->flags & HIF_USB_READY) {
1334                ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1335                ath9k_htc_hw_free(hif_dev->htc_handle);
1336                ath9k_hif_usb_dev_deinit(hif_dev);
1337        }
1338
1339        usb_set_intfdata(interface, NULL);
1340
1341        /* If firmware was loaded we should drop it
1342         * go back to first stage bootloader. */
1343        if (!unplugged && (hif_dev->flags & HIF_USB_READY))
1344                ath9k_hif_usb_reboot(udev);
1345
1346        kfree(hif_dev);
1347        dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1348        usb_put_dev(udev);
1349}
1350
1351#ifdef CONFIG_PM
1352static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1353                                 pm_message_t message)
1354{
1355        struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1356
1357        /*
1358         * The device has to be set to FULLSLEEP mode in case no
1359         * interface is up.
1360         */
1361        if (!(hif_dev->flags & HIF_USB_START))
1362                ath9k_htc_suspend(hif_dev->htc_handle);
1363
1364        wait_for_completion(&hif_dev->fw_done);
1365
1366        if (hif_dev->flags & HIF_USB_READY)
1367                ath9k_hif_usb_dealloc_urbs(hif_dev);
1368
1369        return 0;
1370}
1371
1372static int ath9k_hif_usb_resume(struct usb_interface *interface)
1373{
1374        struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1375        struct htc_target *htc_handle = hif_dev->htc_handle;
1376        int ret;
1377        const struct firmware *fw;
1378
1379        ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1380        if (ret)
1381                return ret;
1382
1383        if (hif_dev->flags & HIF_USB_READY) {
1384                /* request cached firmware during suspend/resume cycle */
1385                ret = request_firmware(&fw, hif_dev->fw_name,
1386                                       &hif_dev->udev->dev);
1387                if (ret)
1388                        goto fail_resume;
1389
1390                hif_dev->fw_data = fw->data;
1391                hif_dev->fw_size = fw->size;
1392                ret = ath9k_hif_usb_download_fw(hif_dev);
1393                release_firmware(fw);
1394                if (ret)
1395                        goto fail_resume;
1396        } else {
1397                ath9k_hif_usb_dealloc_urbs(hif_dev);
1398                return -EIO;
1399        }
1400
1401        mdelay(100);
1402
1403        ret = ath9k_htc_resume(htc_handle);
1404
1405        if (ret)
1406                goto fail_resume;
1407
1408        return 0;
1409
1410fail_resume:
1411        ath9k_hif_usb_dealloc_urbs(hif_dev);
1412
1413        return ret;
1414}
1415#endif
1416
1417static struct usb_driver ath9k_hif_usb_driver = {
1418        .name = KBUILD_MODNAME,
1419        .probe = ath9k_hif_usb_probe,
1420        .disconnect = ath9k_hif_usb_disconnect,
1421#ifdef CONFIG_PM
1422        .suspend = ath9k_hif_usb_suspend,
1423        .resume = ath9k_hif_usb_resume,
1424        .reset_resume = ath9k_hif_usb_resume,
1425#endif
1426        .id_table = ath9k_hif_usb_ids,
1427        .soft_unbind = 1,
1428        .disable_hub_initiated_lpm = 1,
1429};
1430
1431int ath9k_hif_usb_init(void)
1432{
1433        return usb_register(&ath9k_hif_usb_driver);
1434}
1435
1436void ath9k_hif_usb_exit(void)
1437{
1438        usb_deregister(&ath9k_hif_usb_driver);
1439}
1440