linux/drivers/net/wireless/marvell/mwifiex/usb.c
<<
>>
Prefs
   1/*
   2 * Marvell Wireless LAN device driver: USB specific handling
   3 *
   4 * Copyright (C) 2012-2014, Marvell International Ltd.
   5 *
   6 * This software file (the "File") is distributed by Marvell International
   7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
   8 * (the "License").  You may use, redistribute and/or modify this File in
   9 * accordance with the terms and conditions of the License, a copy of which
  10 * is available by writing to the Free Software Foundation, Inc.,
  11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13 *
  14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16 * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
  17 * this warranty disclaimer.
  18 */
  19
  20#include "main.h"
  21#include "usb.h"
  22
  23#define USB_VERSION     "1.0"
  24
  25static struct mwifiex_if_ops usb_ops;
  26
  27static const struct usb_device_id mwifiex_usb_table[] = {
  28        /* 8766 */
  29        {USB_DEVICE(USB8XXX_VID, USB8766_PID_1)},
  30        {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8766_PID_2,
  31                                       USB_CLASS_VENDOR_SPEC,
  32                                       USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  33        /* 8797 */
  34        {USB_DEVICE(USB8XXX_VID, USB8797_PID_1)},
  35        {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8797_PID_2,
  36                                       USB_CLASS_VENDOR_SPEC,
  37                                       USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  38        /* 8801 */
  39        {USB_DEVICE(USB8XXX_VID, USB8801_PID_1)},
  40        {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8801_PID_2,
  41                                       USB_CLASS_VENDOR_SPEC,
  42                                       USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  43        /* 8997 */
  44        {USB_DEVICE(USB8XXX_VID, USB8997_PID_1)},
  45        {USB_DEVICE_AND_INTERFACE_INFO(USB8XXX_VID, USB8997_PID_2,
  46                                       USB_CLASS_VENDOR_SPEC,
  47                                       USB_SUBCLASS_VENDOR_SPEC, 0xff)},
  48        { }     /* Terminating entry */
  49};
  50
  51MODULE_DEVICE_TABLE(usb, mwifiex_usb_table);
  52
  53static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size);
  54
  55/* This function handles received packet. Necessary action is taken based on
  56 * cmd/event/data.
  57 */
  58static int mwifiex_usb_recv(struct mwifiex_adapter *adapter,
  59                            struct sk_buff *skb, u8 ep)
  60{
  61        u32 recv_type;
  62        __le32 tmp;
  63        int ret;
  64
  65        if (adapter->hs_activated)
  66                mwifiex_process_hs_config(adapter);
  67
  68        if (skb->len < INTF_HEADER_LEN) {
  69                mwifiex_dbg(adapter, ERROR,
  70                            "%s: invalid skb->len\n", __func__);
  71                return -1;
  72        }
  73
  74        switch (ep) {
  75        case MWIFIEX_USB_EP_CMD_EVENT:
  76                mwifiex_dbg(adapter, EVENT,
  77                            "%s: EP_CMD_EVENT\n", __func__);
  78                skb_copy_from_linear_data(skb, &tmp, INTF_HEADER_LEN);
  79                recv_type = le32_to_cpu(tmp);
  80                skb_pull(skb, INTF_HEADER_LEN);
  81
  82                switch (recv_type) {
  83                case MWIFIEX_USB_TYPE_CMD:
  84                        if (skb->len > MWIFIEX_SIZE_OF_CMD_BUFFER) {
  85                                mwifiex_dbg(adapter, ERROR,
  86                                            "CMD: skb->len too large\n");
  87                                ret = -1;
  88                                goto exit_restore_skb;
  89                        } else if (!adapter->curr_cmd) {
  90                                mwifiex_dbg(adapter, WARN, "CMD: no curr_cmd\n");
  91                                if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
  92                                        mwifiex_process_sleep_confirm_resp(
  93                                                        adapter, skb->data,
  94                                                        skb->len);
  95                                        ret = 0;
  96                                        goto exit_restore_skb;
  97                                }
  98                                ret = -1;
  99                                goto exit_restore_skb;
 100                        }
 101
 102                        adapter->curr_cmd->resp_skb = skb;
 103                        adapter->cmd_resp_received = true;
 104                        break;
 105                case MWIFIEX_USB_TYPE_EVENT:
 106                        if (skb->len < sizeof(u32)) {
 107                                mwifiex_dbg(adapter, ERROR,
 108                                            "EVENT: skb->len too small\n");
 109                                ret = -1;
 110                                goto exit_restore_skb;
 111                        }
 112                        skb_copy_from_linear_data(skb, &tmp, sizeof(u32));
 113                        adapter->event_cause = le32_to_cpu(tmp);
 114                        mwifiex_dbg(adapter, EVENT,
 115                                    "event_cause %#x\n", adapter->event_cause);
 116
 117                        if (skb->len > MAX_EVENT_SIZE) {
 118                                mwifiex_dbg(adapter, ERROR,
 119                                            "EVENT: event body too large\n");
 120                                ret = -1;
 121                                goto exit_restore_skb;
 122                        }
 123
 124                        memcpy(adapter->event_body, skb->data +
 125                               MWIFIEX_EVENT_HEADER_LEN, skb->len);
 126
 127                        adapter->event_received = true;
 128                        adapter->event_skb = skb;
 129                        break;
 130                default:
 131                        mwifiex_dbg(adapter, ERROR,
 132                                    "unknown recv_type %#x\n", recv_type);
 133                        return -1;
 134                }
 135                break;
 136        case MWIFIEX_USB_EP_DATA:
 137                mwifiex_dbg(adapter, DATA, "%s: EP_DATA\n", __func__);
 138                if (skb->len > MWIFIEX_RX_DATA_BUF_SIZE) {
 139                        mwifiex_dbg(adapter, ERROR,
 140                                    "DATA: skb->len too large\n");
 141                        return -1;
 142                }
 143
 144                skb_queue_tail(&adapter->rx_data_q, skb);
 145                adapter->data_received = true;
 146                atomic_inc(&adapter->rx_pending);
 147                break;
 148        default:
 149                mwifiex_dbg(adapter, ERROR,
 150                            "%s: unknown endport %#x\n", __func__, ep);
 151                return -1;
 152        }
 153
 154        return -EINPROGRESS;
 155
 156exit_restore_skb:
 157        /* The buffer will be reused for further cmds/events */
 158        skb_push(skb, INTF_HEADER_LEN);
 159
 160        return ret;
 161}
 162
 163static void mwifiex_usb_rx_complete(struct urb *urb)
 164{
 165        struct urb_context *context = (struct urb_context *)urb->context;
 166        struct mwifiex_adapter *adapter = context->adapter;
 167        struct sk_buff *skb = context->skb;
 168        struct usb_card_rec *card;
 169        int recv_length = urb->actual_length;
 170        int size, status;
 171
 172        if (!adapter || !adapter->card) {
 173                pr_err("mwifiex adapter or card structure is not valid\n");
 174                return;
 175        }
 176
 177        card = (struct usb_card_rec *)adapter->card;
 178        if (card->rx_cmd_ep == context->ep)
 179                atomic_dec(&card->rx_cmd_urb_pending);
 180        else
 181                atomic_dec(&card->rx_data_urb_pending);
 182
 183        if (recv_length) {
 184                if (urb->status || (adapter->surprise_removed)) {
 185                        mwifiex_dbg(adapter, ERROR,
 186                                    "URB status is failed: %d\n", urb->status);
 187                        /* Do not free skb in case of command ep */
 188                        if (card->rx_cmd_ep != context->ep)
 189                                dev_kfree_skb_any(skb);
 190                        goto setup_for_next;
 191                }
 192                if (skb->len > recv_length)
 193                        skb_trim(skb, recv_length);
 194                else
 195                        skb_put(skb, recv_length - skb->len);
 196
 197                status = mwifiex_usb_recv(adapter, skb, context->ep);
 198
 199                mwifiex_dbg(adapter, INFO,
 200                            "info: recv_length=%d, status=%d\n",
 201                            recv_length, status);
 202                if (status == -EINPROGRESS) {
 203                        mwifiex_queue_main_work(adapter);
 204
 205                        /* urb for data_ep is re-submitted now;
 206                         * urb for cmd_ep will be re-submitted in callback
 207                         * mwifiex_usb_recv_complete
 208                         */
 209                        if (card->rx_cmd_ep == context->ep)
 210                                return;
 211                } else {
 212                        if (status == -1)
 213                                mwifiex_dbg(adapter, ERROR,
 214                                            "received data processing failed!\n");
 215
 216                        /* Do not free skb in case of command ep */
 217                        if (card->rx_cmd_ep != context->ep)
 218                                dev_kfree_skb_any(skb);
 219                }
 220        } else if (urb->status) {
 221                if (!adapter->is_suspended) {
 222                        mwifiex_dbg(adapter, FATAL,
 223                                    "Card is removed: %d\n", urb->status);
 224                        adapter->surprise_removed = true;
 225                }
 226                dev_kfree_skb_any(skb);
 227                return;
 228        } else {
 229                /* Do not free skb in case of command ep */
 230                if (card->rx_cmd_ep != context->ep)
 231                        dev_kfree_skb_any(skb);
 232
 233                /* fall through setup_for_next */
 234        }
 235
 236setup_for_next:
 237        if (card->rx_cmd_ep == context->ep)
 238                size = MWIFIEX_RX_CMD_BUF_SIZE;
 239        else
 240                size = MWIFIEX_RX_DATA_BUF_SIZE;
 241
 242        if (card->rx_cmd_ep == context->ep) {
 243                mwifiex_usb_submit_rx_urb(context, size);
 244        } else {
 245                if (atomic_read(&adapter->rx_pending) <= HIGH_RX_PENDING) {
 246                        mwifiex_usb_submit_rx_urb(context, size);
 247                } else {
 248                        context->skb = NULL;
 249                }
 250        }
 251
 252        return;
 253}
 254
 255static void mwifiex_usb_tx_complete(struct urb *urb)
 256{
 257        struct urb_context *context = (struct urb_context *)(urb->context);
 258        struct mwifiex_adapter *adapter = context->adapter;
 259        struct usb_card_rec *card = adapter->card;
 260        struct usb_tx_data_port *port;
 261        int i;
 262
 263        mwifiex_dbg(adapter, INFO,
 264                    "%s: status: %d\n", __func__, urb->status);
 265
 266        if (context->ep == card->tx_cmd_ep) {
 267                mwifiex_dbg(adapter, CMD,
 268                            "%s: CMD\n", __func__);
 269                atomic_dec(&card->tx_cmd_urb_pending);
 270                adapter->cmd_sent = false;
 271        } else {
 272                mwifiex_dbg(adapter, DATA,
 273                            "%s: DATA\n", __func__);
 274                mwifiex_write_data_complete(adapter, context->skb, 0,
 275                                            urb->status ? -1 : 0);
 276                for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
 277                        port = &card->port[i];
 278                        if (context->ep == port->tx_data_ep) {
 279                                atomic_dec(&port->tx_data_urb_pending);
 280                                port->block_status = false;
 281                                break;
 282                        }
 283                }
 284                adapter->data_sent = false;
 285        }
 286
 287        if (card->mc_resync_flag)
 288                mwifiex_multi_chan_resync(adapter);
 289
 290        mwifiex_queue_main_work(adapter);
 291
 292        return;
 293}
 294
 295static int mwifiex_usb_submit_rx_urb(struct urb_context *ctx, int size)
 296{
 297        struct mwifiex_adapter *adapter = ctx->adapter;
 298        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
 299
 300        if (card->rx_cmd_ep != ctx->ep) {
 301                ctx->skb = dev_alloc_skb(size);
 302                if (!ctx->skb) {
 303                        mwifiex_dbg(adapter, ERROR,
 304                                    "%s: dev_alloc_skb failed\n", __func__);
 305                        return -ENOMEM;
 306                }
 307        }
 308
 309        if (card->rx_cmd_ep == ctx->ep &&
 310            card->rx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
 311                usb_fill_int_urb(ctx->urb, card->udev,
 312                                 usb_rcvintpipe(card->udev, ctx->ep),
 313                                 ctx->skb->data, size, mwifiex_usb_rx_complete,
 314                                 (void *)ctx, card->rx_cmd_interval);
 315        else
 316                usb_fill_bulk_urb(ctx->urb, card->udev,
 317                                  usb_rcvbulkpipe(card->udev, ctx->ep),
 318                                  ctx->skb->data, size, mwifiex_usb_rx_complete,
 319                                  (void *)ctx);
 320
 321        if (card->rx_cmd_ep == ctx->ep)
 322                atomic_inc(&card->rx_cmd_urb_pending);
 323        else
 324                atomic_inc(&card->rx_data_urb_pending);
 325
 326        if (usb_submit_urb(ctx->urb, GFP_ATOMIC)) {
 327                mwifiex_dbg(adapter, ERROR, "usb_submit_urb failed\n");
 328                dev_kfree_skb_any(ctx->skb);
 329                ctx->skb = NULL;
 330
 331                if (card->rx_cmd_ep == ctx->ep)
 332                        atomic_dec(&card->rx_cmd_urb_pending);
 333                else
 334                        atomic_dec(&card->rx_data_urb_pending);
 335
 336                return -1;
 337        }
 338
 339        return 0;
 340}
 341
 342static void mwifiex_usb_free(struct usb_card_rec *card)
 343{
 344        struct usb_tx_data_port *port;
 345        int i, j;
 346
 347        if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
 348                usb_kill_urb(card->rx_cmd.urb);
 349
 350        usb_free_urb(card->rx_cmd.urb);
 351        card->rx_cmd.urb = NULL;
 352
 353        if (atomic_read(&card->rx_data_urb_pending))
 354                for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
 355                        if (card->rx_data_list[i].urb)
 356                                usb_kill_urb(card->rx_data_list[i].urb);
 357
 358        for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
 359                usb_free_urb(card->rx_data_list[i].urb);
 360                card->rx_data_list[i].urb = NULL;
 361        }
 362
 363        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
 364                port = &card->port[i];
 365                for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
 366                        usb_kill_urb(port->tx_data_list[j].urb);
 367                        usb_free_urb(port->tx_data_list[j].urb);
 368                        port->tx_data_list[j].urb = NULL;
 369                }
 370        }
 371
 372        usb_free_urb(card->tx_cmd.urb);
 373        card->tx_cmd.urb = NULL;
 374
 375        return;
 376}
 377
 378/* This function probes an mwifiex device and registers it. It allocates
 379 * the card structure, initiates the device registration and initialization
 380 * procedure by adding a logical interface.
 381 */
 382static int mwifiex_usb_probe(struct usb_interface *intf,
 383                             const struct usb_device_id *id)
 384{
 385        struct usb_device *udev = interface_to_usbdev(intf);
 386        struct usb_host_interface *iface_desc = intf->cur_altsetting;
 387        struct usb_endpoint_descriptor *epd;
 388        int ret, i;
 389        struct usb_card_rec *card;
 390        u16 id_vendor, id_product, bcd_device;
 391
 392        card = devm_kzalloc(&intf->dev, sizeof(*card), GFP_KERNEL);
 393        if (!card)
 394                return -ENOMEM;
 395
 396        init_completion(&card->fw_done);
 397
 398        id_vendor = le16_to_cpu(udev->descriptor.idVendor);
 399        id_product = le16_to_cpu(udev->descriptor.idProduct);
 400        bcd_device = le16_to_cpu(udev->descriptor.bcdDevice);
 401        pr_debug("info: VID/PID = %X/%X, Boot2 version = %X\n",
 402                 id_vendor, id_product, bcd_device);
 403
 404        /* PID_1 is used for firmware downloading only */
 405        switch (id_product) {
 406        case USB8766_PID_1:
 407        case USB8797_PID_1:
 408        case USB8801_PID_1:
 409        case USB8997_PID_1:
 410                card->usb_boot_state = USB8XXX_FW_DNLD;
 411                break;
 412        case USB8766_PID_2:
 413        case USB8797_PID_2:
 414        case USB8801_PID_2:
 415        case USB8997_PID_2:
 416                card->usb_boot_state = USB8XXX_FW_READY;
 417                break;
 418        default:
 419                pr_warn("unknown id_product %#x\n", id_product);
 420                card->usb_boot_state = USB8XXX_FW_DNLD;
 421                break;
 422        }
 423
 424        card->udev = udev;
 425        card->intf = intf;
 426
 427        pr_debug("info: bcdUSB=%#x Device Class=%#x SubClass=%#x Protocol=%#x\n",
 428                 le16_to_cpu(udev->descriptor.bcdUSB),
 429                 udev->descriptor.bDeviceClass,
 430                 udev->descriptor.bDeviceSubClass,
 431                 udev->descriptor.bDeviceProtocol);
 432
 433        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
 434                epd = &iface_desc->endpoint[i].desc;
 435                if (usb_endpoint_dir_in(epd) &&
 436                    usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
 437                    (usb_endpoint_xfer_bulk(epd) ||
 438                     usb_endpoint_xfer_int(epd))) {
 439                        card->rx_cmd_ep_type = usb_endpoint_type(epd);
 440                        card->rx_cmd_interval = epd->bInterval;
 441                        pr_debug("info: Rx CMD/EVT:: max pkt size: %d, addr: %d, ep_type: %d\n",
 442                                 le16_to_cpu(epd->wMaxPacketSize),
 443                                 epd->bEndpointAddress, card->rx_cmd_ep_type);
 444                        card->rx_cmd_ep = usb_endpoint_num(epd);
 445                        atomic_set(&card->rx_cmd_urb_pending, 0);
 446                }
 447                if (usb_endpoint_dir_in(epd) &&
 448                    usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
 449                    usb_endpoint_xfer_bulk(epd)) {
 450                        pr_debug("info: bulk IN: max pkt size: %d, addr: %d\n",
 451                                 le16_to_cpu(epd->wMaxPacketSize),
 452                                 epd->bEndpointAddress);
 453                        card->rx_data_ep = usb_endpoint_num(epd);
 454                        atomic_set(&card->rx_data_urb_pending, 0);
 455                }
 456                if (usb_endpoint_dir_out(epd) &&
 457                    usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA &&
 458                    usb_endpoint_xfer_bulk(epd)) {
 459                        pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
 460                                 le16_to_cpu(epd->wMaxPacketSize),
 461                                 epd->bEndpointAddress);
 462                        card->port[0].tx_data_ep = usb_endpoint_num(epd);
 463                        atomic_set(&card->port[0].tx_data_urb_pending, 0);
 464                }
 465                if (usb_endpoint_dir_out(epd) &&
 466                    usb_endpoint_num(epd) == MWIFIEX_USB_EP_DATA_CH2 &&
 467                    usb_endpoint_xfer_bulk(epd)) {
 468                        pr_debug("info: bulk OUT chan2:\t"
 469                                 "max pkt size: %d, addr: %d\n",
 470                                 le16_to_cpu(epd->wMaxPacketSize),
 471                                 epd->bEndpointAddress);
 472                        card->port[1].tx_data_ep = usb_endpoint_num(epd);
 473                        atomic_set(&card->port[1].tx_data_urb_pending, 0);
 474                }
 475                if (usb_endpoint_dir_out(epd) &&
 476                    usb_endpoint_num(epd) == MWIFIEX_USB_EP_CMD_EVENT &&
 477                    (usb_endpoint_xfer_bulk(epd) ||
 478                     usb_endpoint_xfer_int(epd))) {
 479                        card->tx_cmd_ep_type = usb_endpoint_type(epd);
 480                        card->tx_cmd_interval = epd->bInterval;
 481                        pr_debug("info: bulk OUT: max pkt size: %d, addr: %d\n",
 482                                 le16_to_cpu(epd->wMaxPacketSize),
 483                                 epd->bEndpointAddress);
 484                        pr_debug("info: Tx CMD:: max pkt size: %d, addr: %d, ep_type: %d\n",
 485                                 le16_to_cpu(epd->wMaxPacketSize),
 486                                 epd->bEndpointAddress, card->tx_cmd_ep_type);
 487                        card->tx_cmd_ep = usb_endpoint_num(epd);
 488                        atomic_set(&card->tx_cmd_urb_pending, 0);
 489                        card->bulk_out_maxpktsize =
 490                                        le16_to_cpu(epd->wMaxPacketSize);
 491                }
 492        }
 493
 494        usb_set_intfdata(intf, card);
 495
 496        ret = mwifiex_add_card(card, &card->fw_done, &usb_ops,
 497                               MWIFIEX_USB, &card->udev->dev);
 498        if (ret) {
 499                pr_err("%s: mwifiex_add_card failed: %d\n", __func__, ret);
 500                usb_reset_device(udev);
 501                return ret;
 502        }
 503
 504        usb_get_dev(udev);
 505
 506        return 0;
 507}
 508
 509/* Kernel needs to suspend all functions separately. Therefore all
 510 * registered functions must have drivers with suspend and resume
 511 * methods. Failing that the kernel simply removes the whole card.
 512 *
 513 * If already not suspended, this function allocates and sends a
 514 * 'host sleep activate' request to the firmware and turns off the traffic.
 515 */
 516static int mwifiex_usb_suspend(struct usb_interface *intf, pm_message_t message)
 517{
 518        struct usb_card_rec *card = usb_get_intfdata(intf);
 519        struct mwifiex_adapter *adapter;
 520        struct usb_tx_data_port *port;
 521        int i, j;
 522
 523        /* Might still be loading firmware */
 524        wait_for_completion(&card->fw_done);
 525
 526        adapter = card->adapter;
 527        if (!adapter) {
 528                dev_err(&intf->dev, "card is not valid\n");
 529                return 0;
 530        }
 531
 532        if (unlikely(adapter->is_suspended))
 533                mwifiex_dbg(adapter, WARN,
 534                            "Device already suspended\n");
 535
 536        /* Enable the Host Sleep */
 537        if (!mwifiex_enable_hs(adapter)) {
 538                mwifiex_dbg(adapter, ERROR,
 539                            "cmd: failed to suspend\n");
 540                adapter->hs_enabling = false;
 541                return -EFAULT;
 542        }
 543
 544
 545        /* 'is_suspended' flag indicates device is suspended.
 546         * It must be set here before the usb_kill_urb() calls. Reason
 547         * is in the complete handlers, urb->status(= -ENOENT) and
 548         * this flag is used in combination to distinguish between a
 549         * 'suspended' state and a 'disconnect' one.
 550         */
 551        adapter->is_suspended = true;
 552        adapter->hs_enabling = false;
 553
 554        if (atomic_read(&card->rx_cmd_urb_pending) && card->rx_cmd.urb)
 555                usb_kill_urb(card->rx_cmd.urb);
 556
 557        if (atomic_read(&card->rx_data_urb_pending))
 558                for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
 559                        if (card->rx_data_list[i].urb)
 560                                usb_kill_urb(card->rx_data_list[i].urb);
 561
 562        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
 563                port = &card->port[i];
 564                for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
 565                        if (port->tx_data_list[j].urb)
 566                                usb_kill_urb(port->tx_data_list[j].urb);
 567                }
 568        }
 569
 570        if (card->tx_cmd.urb)
 571                usb_kill_urb(card->tx_cmd.urb);
 572
 573        return 0;
 574}
 575
 576/* Kernel needs to suspend all functions separately. Therefore all
 577 * registered functions must have drivers with suspend and resume
 578 * methods. Failing that the kernel simply removes the whole card.
 579 *
 580 * If already not resumed, this function turns on the traffic and
 581 * sends a 'host sleep cancel' request to the firmware.
 582 */
 583static int mwifiex_usb_resume(struct usb_interface *intf)
 584{
 585        struct usb_card_rec *card = usb_get_intfdata(intf);
 586        struct mwifiex_adapter *adapter;
 587        int i;
 588
 589        if (!card->adapter) {
 590                dev_err(&intf->dev, "%s: card->adapter is NULL\n",
 591                        __func__);
 592                return 0;
 593        }
 594        adapter = card->adapter;
 595
 596        if (unlikely(!adapter->is_suspended)) {
 597                mwifiex_dbg(adapter, WARN,
 598                            "Device already resumed\n");
 599                return 0;
 600        }
 601
 602        /* Indicate device resumed. The netdev queue will be resumed only
 603         * after the urbs have been re-submitted
 604         */
 605        adapter->is_suspended = false;
 606
 607        if (!atomic_read(&card->rx_data_urb_pending))
 608                for (i = 0; i < MWIFIEX_RX_DATA_URB; i++)
 609                        mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
 610                                                  MWIFIEX_RX_DATA_BUF_SIZE);
 611
 612        if (!atomic_read(&card->rx_cmd_urb_pending)) {
 613                card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
 614                if (card->rx_cmd.skb)
 615                        mwifiex_usb_submit_rx_urb(&card->rx_cmd,
 616                                                  MWIFIEX_RX_CMD_BUF_SIZE);
 617        }
 618
 619        /* Disable Host Sleep */
 620        if (adapter->hs_activated)
 621                mwifiex_cancel_hs(mwifiex_get_priv(adapter,
 622                                                   MWIFIEX_BSS_ROLE_ANY),
 623                                  MWIFIEX_ASYNC_CMD);
 624
 625        return 0;
 626}
 627
 628static void mwifiex_usb_disconnect(struct usb_interface *intf)
 629{
 630        struct usb_card_rec *card = usb_get_intfdata(intf);
 631        struct mwifiex_adapter *adapter;
 632
 633        wait_for_completion(&card->fw_done);
 634
 635        adapter = card->adapter;
 636        if (!adapter || !adapter->priv_num)
 637                return;
 638
 639        if (card->udev->state != USB_STATE_NOTATTACHED && !adapter->mfg_mode) {
 640                mwifiex_deauthenticate_all(adapter);
 641
 642                mwifiex_init_shutdown_fw(mwifiex_get_priv(adapter,
 643                                                          MWIFIEX_BSS_ROLE_ANY),
 644                                         MWIFIEX_FUNC_SHUTDOWN);
 645        }
 646
 647        mwifiex_dbg(adapter, FATAL,
 648                    "%s: removing card\n", __func__);
 649        mwifiex_remove_card(adapter);
 650
 651        usb_put_dev(interface_to_usbdev(intf));
 652}
 653
 654static void mwifiex_usb_coredump(struct device *dev)
 655{
 656        struct usb_interface *intf = to_usb_interface(dev);
 657        struct usb_card_rec *card = usb_get_intfdata(intf);
 658
 659        mwifiex_fw_dump_event(mwifiex_get_priv(card->adapter,
 660                                               MWIFIEX_BSS_ROLE_ANY));
 661}
 662
 663static struct usb_driver mwifiex_usb_driver = {
 664        .name = "mwifiex_usb",
 665        .probe = mwifiex_usb_probe,
 666        .disconnect = mwifiex_usb_disconnect,
 667        .id_table = mwifiex_usb_table,
 668        .suspend = mwifiex_usb_suspend,
 669        .resume = mwifiex_usb_resume,
 670        .soft_unbind = 1,
 671        .drvwrap.driver = {
 672                .coredump = mwifiex_usb_coredump,
 673        },
 674};
 675
 676static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
 677                                   u32 *len, u8 ep, u32 timeout)
 678{
 679        struct usb_card_rec *card = adapter->card;
 680        int actual_length, ret;
 681
 682        if (!(*len % card->bulk_out_maxpktsize))
 683                (*len)++;
 684
 685        /* Send the data block */
 686        ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
 687                           *len, &actual_length, timeout);
 688        if (ret) {
 689                mwifiex_dbg(adapter, ERROR,
 690                            "usb_bulk_msg for tx failed: %d\n", ret);
 691                return ret;
 692        }
 693
 694        *len = actual_length;
 695
 696        return ret;
 697}
 698
 699static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
 700                                  u32 *len, u8 ep, u32 timeout)
 701{
 702        struct usb_card_rec *card = adapter->card;
 703        int actual_length, ret;
 704
 705        /* Receive the data response */
 706        ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
 707                           *len, &actual_length, timeout);
 708        if (ret) {
 709                mwifiex_dbg(adapter, ERROR,
 710                            "usb_bulk_msg for rx failed: %d\n", ret);
 711                return ret;
 712        }
 713
 714        *len = actual_length;
 715
 716        return ret;
 717}
 718
 719static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
 720{
 721        struct usb_card_rec *card = adapter->card;
 722        u8 active_port = MWIFIEX_USB_EP_DATA;
 723        struct mwifiex_private *priv = NULL;
 724        int i;
 725
 726        if (adapter->usb_mc_status) {
 727                for (i = 0; i < adapter->priv_num; i++) {
 728                        priv = adapter->priv[i];
 729                        if (!priv)
 730                                continue;
 731                        if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
 732                             !priv->bss_started) ||
 733                            (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
 734                             !priv->media_connected))
 735                                priv->usb_port = MWIFIEX_USB_EP_DATA;
 736                }
 737                for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
 738                        card->port[i].block_status = false;
 739        } else {
 740                for (i = 0; i < adapter->priv_num; i++) {
 741                        priv = adapter->priv[i];
 742                        if (!priv)
 743                                continue;
 744                        if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
 745                             priv->bss_started) ||
 746                            (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
 747                             priv->media_connected)) {
 748                                active_port = priv->usb_port;
 749                                break;
 750                        }
 751                }
 752                for (i = 0; i < adapter->priv_num; i++) {
 753                        priv = adapter->priv[i];
 754                        if (priv)
 755                                priv->usb_port = active_port;
 756                }
 757                for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
 758                        if (active_port == card->port[i].tx_data_ep)
 759                                card->port[i].block_status = false;
 760                        else
 761                                card->port[i].block_status = true;
 762                }
 763        }
 764}
 765
 766static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
 767{
 768        struct usb_card_rec *card = priv->adapter->card;
 769        int idx;
 770
 771        for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
 772                if (priv->usb_port == card->port[idx].tx_data_ep)
 773                        return !card->port[idx].block_status;
 774        }
 775
 776        return false;
 777}
 778
 779static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
 780{
 781        struct usb_card_rec *card = adapter->card;
 782        int i;
 783
 784        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
 785                if (!card->port[i].block_status)
 786                        return false;
 787
 788        return true;
 789}
 790
 791static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter,
 792                                          struct usb_tx_data_port *port, u8 ep,
 793                                          struct urb_context *context,
 794                                          struct sk_buff *skb_send)
 795{
 796        struct usb_card_rec *card = adapter->card;
 797        int ret = -EINPROGRESS;
 798        struct urb *tx_urb;
 799
 800        context->adapter = adapter;
 801        context->ep = ep;
 802        context->skb = skb_send;
 803        tx_urb = context->urb;
 804
 805        if (ep == card->tx_cmd_ep &&
 806            card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
 807                usb_fill_int_urb(tx_urb, card->udev,
 808                                 usb_sndintpipe(card->udev, ep), skb_send->data,
 809                                 skb_send->len, mwifiex_usb_tx_complete,
 810                                 (void *)context, card->tx_cmd_interval);
 811        else
 812                usb_fill_bulk_urb(tx_urb, card->udev,
 813                                  usb_sndbulkpipe(card->udev, ep),
 814                                  skb_send->data, skb_send->len,
 815                                  mwifiex_usb_tx_complete, (void *)context);
 816
 817        tx_urb->transfer_flags |= URB_ZERO_PACKET;
 818
 819        if (ep == card->tx_cmd_ep)
 820                atomic_inc(&card->tx_cmd_urb_pending);
 821        else
 822                atomic_inc(&port->tx_data_urb_pending);
 823
 824        if (ep != card->tx_cmd_ep &&
 825            atomic_read(&port->tx_data_urb_pending) ==
 826                                        MWIFIEX_TX_DATA_URB) {
 827                port->block_status = true;
 828                adapter->data_sent = mwifiex_usb_data_sent(adapter);
 829                ret = -ENOSR;
 830        }
 831
 832        if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
 833                mwifiex_dbg(adapter, ERROR,
 834                            "%s: usb_submit_urb failed\n", __func__);
 835                if (ep == card->tx_cmd_ep) {
 836                        atomic_dec(&card->tx_cmd_urb_pending);
 837                } else {
 838                        atomic_dec(&port->tx_data_urb_pending);
 839                        port->block_status = false;
 840                        adapter->data_sent = false;
 841                        if (port->tx_data_ix)
 842                                port->tx_data_ix--;
 843                        else
 844                                port->tx_data_ix = MWIFIEX_TX_DATA_URB;
 845                }
 846                ret = -1;
 847        }
 848
 849        return ret;
 850}
 851
 852static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter,
 853                                           struct usb_tx_data_port *port,
 854                                           struct sk_buff **skb_send)
 855{
 856        struct sk_buff *skb_aggr, *skb_tmp;
 857        u8 *payload, pad;
 858        u16 align = adapter->bus_aggr.tx_aggr_align;
 859        struct mwifiex_txinfo *tx_info = NULL;
 860        bool is_txinfo_set = false;
 861
 862        /* Packets in aggr_list will be send in either skb_aggr or
 863         * write complete, delete the tx_aggr timer
 864         */
 865        if (port->tx_aggr.timer_cnxt.is_hold_timer_set) {
 866                del_timer(&port->tx_aggr.timer_cnxt.hold_timer);
 867                port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
 868                port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
 869        }
 870
 871        skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len,
 872                                               GFP_ATOMIC);
 873        if (!skb_aggr) {
 874                mwifiex_dbg(adapter, ERROR,
 875                            "%s: alloc skb_aggr failed\n", __func__);
 876
 877                while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list)))
 878                        mwifiex_write_data_complete(adapter, skb_tmp, 0, -1);
 879
 880                port->tx_aggr.aggr_num = 0;
 881                port->tx_aggr.aggr_len = 0;
 882                return -EBUSY;
 883        }
 884
 885        tx_info = MWIFIEX_SKB_TXCB(skb_aggr);
 886        memset(tx_info, 0, sizeof(*tx_info));
 887
 888        while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) {
 889                /* padding for aligning next packet header*/
 890                pad = (align - (skb_tmp->len & (align - 1))) % align;
 891                payload = skb_put(skb_aggr, skb_tmp->len + pad);
 892                memcpy(payload, skb_tmp->data, skb_tmp->len);
 893                if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
 894                        /* do not padding for last packet*/
 895                        *(u16 *)payload = cpu_to_le16(skb_tmp->len);
 896                        *(u16 *)&payload[2] =
 897                                cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
 898                        skb_trim(skb_aggr, skb_aggr->len - pad);
 899                } else {
 900                        /* add aggregation interface header */
 901                        *(u16 *)payload = cpu_to_le16(skb_tmp->len + pad);
 902                        *(u16 *)&payload[2] =
 903                                cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2);
 904                }
 905
 906                if (!is_txinfo_set) {
 907                        tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num;
 908                        tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type;
 909                        is_txinfo_set = true;
 910                }
 911
 912                port->tx_aggr.aggr_num--;
 913                port->tx_aggr.aggr_len -= (skb_tmp->len + pad);
 914                mwifiex_write_data_complete(adapter, skb_tmp, 0, 0);
 915        }
 916
 917        tx_info->pkt_len = skb_aggr->len -
 918                        (sizeof(struct txpd) + adapter->intf_hdr_len);
 919        tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT;
 920
 921        port->tx_aggr.aggr_num = 0;
 922        port->tx_aggr.aggr_len = 0;
 923        *skb_send = skb_aggr;
 924
 925        return 0;
 926}
 927
 928/* This function prepare data packet to be send under usb tx aggregation
 929 * protocol, check current usb aggregation status, link packet to aggrgation
 930 * list if possible, work flow as below:
 931 * (1) if only 1 packet available, add usb tx aggregation header and send.
 932 * (2) if packet is able to aggregated, link it to current aggregation list.
 933 * (3) if packet is not able to aggregated, aggregate and send exist packets
 934 *     in aggrgation list. Then, link packet in the list if there is more
 935 *     packet in transmit queue, otherwise try to transmit single packet.
 936 */
 937static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep,
 938                                    struct sk_buff *skb,
 939                                    struct mwifiex_tx_param *tx_param,
 940                                    struct usb_tx_data_port *port)
 941{
 942        u8 *payload, pad;
 943        u16 align = adapter->bus_aggr.tx_aggr_align;
 944        struct sk_buff *skb_send = NULL;
 945        struct urb_context *context = NULL;
 946        struct txpd *local_tx_pd =
 947                (struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len);
 948        u8 f_send_aggr_buf = 0;
 949        u8 f_send_cur_buf = 0;
 950        u8 f_precopy_cur_buf = 0;
 951        u8 f_postcopy_cur_buf = 0;
 952        u32 timeout;
 953        int ret;
 954
 955        /* padding to ensure each packet alginment */
 956        pad = (align - (skb->len & (align - 1))) % align;
 957
 958        if (tx_param && tx_param->next_pkt_len) {
 959                /* next packet available in tx queue*/
 960                if (port->tx_aggr.aggr_len + skb->len + pad >
 961                    adapter->bus_aggr.tx_aggr_max_size) {
 962                        f_send_aggr_buf = 1;
 963                        f_postcopy_cur_buf = 1;
 964                } else {
 965                        /* current packet could be aggregated*/
 966                        f_precopy_cur_buf = 1;
 967
 968                        if (port->tx_aggr.aggr_len + skb->len + pad +
 969                            tx_param->next_pkt_len >
 970                            adapter->bus_aggr.tx_aggr_max_size ||
 971                            port->tx_aggr.aggr_num + 2 >
 972                            adapter->bus_aggr.tx_aggr_max_num) {
 973                            /* next packet could not be aggregated
 974                             * send current aggregation buffer
 975                             */
 976                                f_send_aggr_buf = 1;
 977                        }
 978                }
 979        } else {
 980                /* last packet in tx queue */
 981                if (port->tx_aggr.aggr_num > 0) {
 982                        /* pending packets in aggregation buffer*/
 983                        if (port->tx_aggr.aggr_len + skb->len + pad >
 984                            adapter->bus_aggr.tx_aggr_max_size) {
 985                                /* current packet not be able to aggregated,
 986                                 * send aggr buffer first, then send packet.
 987                                 */
 988                                f_send_cur_buf = 1;
 989                        } else {
 990                                /* last packet, Aggregation and send */
 991                                f_precopy_cur_buf = 1;
 992                        }
 993
 994                        f_send_aggr_buf = 1;
 995                } else {
 996                        /* no pending packets in aggregation buffer,
 997                         * send current packet immediately
 998                         */
 999                         f_send_cur_buf = 1;
1000                }
1001        }
1002
1003        if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) {
1004                /* Send NULL packet immediately*/
1005                if (f_precopy_cur_buf) {
1006                        if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
1007                                f_precopy_cur_buf = 0;
1008                                f_send_aggr_buf = 0;
1009                                f_send_cur_buf = 1;
1010                        } else {
1011                                f_send_aggr_buf = 1;
1012                        }
1013                } else if (f_postcopy_cur_buf) {
1014                        f_send_cur_buf = 1;
1015                        f_postcopy_cur_buf = 0;
1016                }
1017        }
1018
1019        if (f_precopy_cur_buf) {
1020                skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1021                port->tx_aggr.aggr_len += (skb->len + pad);
1022                port->tx_aggr.aggr_num++;
1023                if (f_send_aggr_buf)
1024                        goto send_aggr_buf;
1025
1026                /* packet will not been send immediately,
1027                 * set a timer to make sure it will be sent under
1028                 * strict time limit. Dynamically fit the timeout
1029                 * value, according to packets number in aggr_list
1030                 */
1031                if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1032                        port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1033                                        MWIFIEX_USB_TX_AGGR_TMO_MIN;
1034                        timeout =
1035                                port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1036                        mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1037                                  jiffies + msecs_to_jiffies(timeout));
1038                        port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1039                } else {
1040                        if (port->tx_aggr.timer_cnxt.hold_tmo_msecs <
1041                            MWIFIEX_USB_TX_AGGR_TMO_MAX) {
1042                                /* Dyanmic fit timeout */
1043                                timeout =
1044                                ++port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1045                                mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1046                                          jiffies + msecs_to_jiffies(timeout));
1047                        }
1048                }
1049        }
1050
1051send_aggr_buf:
1052        if (f_send_aggr_buf) {
1053                ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1054                if (!ret) {
1055                        context = &port->tx_data_list[port->tx_data_ix++];
1056                        ret = mwifiex_usb_construct_send_urb(adapter, port, ep,
1057                                                             context, skb_send);
1058                        if (ret == -1)
1059                                mwifiex_write_data_complete(adapter, skb_send,
1060                                                            0, -1);
1061                }
1062        }
1063
1064        if (f_send_cur_buf) {
1065                if (f_send_aggr_buf) {
1066                        if (atomic_read(&port->tx_data_urb_pending) >=
1067                            MWIFIEX_TX_DATA_URB) {
1068                                port->block_status = true;
1069                                adapter->data_sent =
1070                                        mwifiex_usb_data_sent(adapter);
1071                                /* no available urb, postcopy packet*/
1072                                f_postcopy_cur_buf = 1;
1073                                goto postcopy_cur_buf;
1074                        }
1075
1076                        if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1077                                port->tx_data_ix = 0;
1078                }
1079
1080                payload = skb->data;
1081                *(u16 *)&payload[2] =
1082                        cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
1083                *(u16 *)payload = cpu_to_le16(skb->len);
1084                skb_send = skb;
1085                context = &port->tx_data_list[port->tx_data_ix++];
1086                return mwifiex_usb_construct_send_urb(adapter, port, ep,
1087                                                      context, skb_send);
1088        }
1089
1090postcopy_cur_buf:
1091        if (f_postcopy_cur_buf) {
1092                skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1093                port->tx_aggr.aggr_len += (skb->len + pad);
1094                port->tx_aggr.aggr_num++;
1095                /* New aggregation begin, start timer */
1096                if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1097                        port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1098                                        MWIFIEX_USB_TX_AGGR_TMO_MIN;
1099                        timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1100                        mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1101                                  jiffies + msecs_to_jiffies(timeout));
1102                        port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1103                }
1104        }
1105
1106        return -EINPROGRESS;
1107}
1108
1109static void mwifiex_usb_tx_aggr_tmo(struct timer_list *t)
1110{
1111        struct urb_context *urb_cnxt = NULL;
1112        struct sk_buff *skb_send = NULL;
1113        struct tx_aggr_tmr_cnxt *timer_context =
1114                from_timer(timer_context, t, hold_timer);
1115        struct mwifiex_adapter *adapter = timer_context->adapter;
1116        struct usb_tx_data_port *port = timer_context->port;
1117        unsigned long flags;
1118        int err = 0;
1119
1120        spin_lock_irqsave(&port->tx_aggr_lock, flags);
1121        err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1122        if (err) {
1123                mwifiex_dbg(adapter, ERROR,
1124                            "prepare tx aggr skb failed, err=%d\n", err);
1125                goto unlock;
1126        }
1127
1128        if (atomic_read(&port->tx_data_urb_pending) >=
1129            MWIFIEX_TX_DATA_URB) {
1130                port->block_status = true;
1131                adapter->data_sent =
1132                        mwifiex_usb_data_sent(adapter);
1133                err = -1;
1134                goto done;
1135        }
1136
1137        if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1138                port->tx_data_ix = 0;
1139
1140        urb_cnxt = &port->tx_data_list[port->tx_data_ix++];
1141        err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep,
1142                                             urb_cnxt, skb_send);
1143done:
1144        if (err == -1)
1145                mwifiex_write_data_complete(adapter, skb_send, 0, -1);
1146unlock:
1147        spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1148}
1149
1150/* This function write a command/data packet to card. */
1151static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
1152                                    struct sk_buff *skb,
1153                                    struct mwifiex_tx_param *tx_param)
1154{
1155        struct usb_card_rec *card = adapter->card;
1156        struct urb_context *context = NULL;
1157        struct usb_tx_data_port *port = NULL;
1158        unsigned long flags;
1159        int idx, ret;
1160
1161        if (adapter->is_suspended) {
1162                mwifiex_dbg(adapter, ERROR,
1163                            "%s: not allowed while suspended\n", __func__);
1164                return -1;
1165        }
1166
1167        if (adapter->surprise_removed) {
1168                mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
1169                return -1;
1170        }
1171
1172        mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
1173
1174        if (ep == card->tx_cmd_ep) {
1175                context = &card->tx_cmd;
1176        } else {
1177                /* get the data port structure for endpoint */
1178                for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1179                        if (ep == card->port[idx].tx_data_ep) {
1180                                port = &card->port[idx];
1181                                if (atomic_read(&port->tx_data_urb_pending)
1182                                    >= MWIFIEX_TX_DATA_URB) {
1183                                        port->block_status = true;
1184                                        adapter->data_sent =
1185                                                mwifiex_usb_data_sent(adapter);
1186                                        return -EBUSY;
1187                                }
1188                                if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1189                                        port->tx_data_ix = 0;
1190                                break;
1191                        }
1192                }
1193
1194                if (!port) {
1195                        mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
1196                        return -1;
1197                }
1198
1199                if (adapter->bus_aggr.enable) {
1200                        spin_lock_irqsave(&port->tx_aggr_lock, flags);
1201                        ret =  mwifiex_usb_aggr_tx_data(adapter, ep, skb,
1202                                                        tx_param, port);
1203                        spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1204                        return ret;
1205                }
1206
1207                context = &port->tx_data_list[port->tx_data_ix++];
1208        }
1209
1210        return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb);
1211}
1212
1213static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
1214{
1215        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1216        struct usb_tx_data_port *port;
1217        int i, j;
1218
1219        card->tx_cmd.adapter = adapter;
1220        card->tx_cmd.ep = card->tx_cmd_ep;
1221
1222        card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1223        if (!card->tx_cmd.urb)
1224                return -ENOMEM;
1225
1226        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1227                port = &card->port[i];
1228                if (!port->tx_data_ep)
1229                        continue;
1230                port->tx_data_ix = 0;
1231                skb_queue_head_init(&port->tx_aggr.aggr_list);
1232                if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
1233                        port->block_status = false;
1234                else
1235                        port->block_status = true;
1236                for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
1237                        port->tx_data_list[j].adapter = adapter;
1238                        port->tx_data_list[j].ep = port->tx_data_ep;
1239                        port->tx_data_list[j].urb =
1240                                        usb_alloc_urb(0, GFP_KERNEL);
1241                        if (!port->tx_data_list[j].urb)
1242                                return -ENOMEM;
1243                }
1244
1245                port->tx_aggr.timer_cnxt.adapter = adapter;
1246                port->tx_aggr.timer_cnxt.port = port;
1247                port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1248                port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1249                timer_setup(&port->tx_aggr.timer_cnxt.hold_timer,
1250                            mwifiex_usb_tx_aggr_tmo, 0);
1251        }
1252
1253        return 0;
1254}
1255
1256static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
1257{
1258        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1259        int i;
1260
1261        card->rx_cmd.adapter = adapter;
1262        card->rx_cmd.ep = card->rx_cmd_ep;
1263
1264        card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1265        if (!card->rx_cmd.urb)
1266                return -ENOMEM;
1267
1268        card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
1269        if (!card->rx_cmd.skb)
1270                return -ENOMEM;
1271
1272        if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
1273                return -1;
1274
1275        for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1276                card->rx_data_list[i].adapter = adapter;
1277                card->rx_data_list[i].ep = card->rx_data_ep;
1278
1279                card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1280                if (!card->rx_data_list[i].urb)
1281                        return -1;
1282                if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
1283                                              MWIFIEX_RX_DATA_BUF_SIZE))
1284                        return -1;
1285        }
1286
1287        return 0;
1288}
1289
1290/* This function register usb device and initialize parameter. */
1291static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1292{
1293        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1294
1295        card->adapter = adapter;
1296
1297        switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
1298        case USB8997_PID_1:
1299        case USB8997_PID_2:
1300                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
1301                strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
1302                adapter->ext_scan = true;
1303                break;
1304        case USB8766_PID_1:
1305        case USB8766_PID_2:
1306                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1307                strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
1308                adapter->ext_scan = true;
1309                break;
1310        case USB8801_PID_1:
1311        case USB8801_PID_2:
1312                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1313                strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
1314                adapter->ext_scan = false;
1315                break;
1316        case USB8797_PID_1:
1317        case USB8797_PID_2:
1318        default:
1319                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1320                strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
1321                break;
1322        }
1323
1324        adapter->usb_mc_status = false;
1325        adapter->usb_mc_setup = false;
1326
1327        return 0;
1328}
1329
1330static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter)
1331{
1332        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1333        struct usb_tx_data_port *port;
1334        struct sk_buff *skb_tmp;
1335        int idx;
1336
1337        for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1338                port = &card->port[idx];
1339                if (adapter->bus_aggr.enable)
1340                        while ((skb_tmp =
1341                                skb_dequeue(&port->tx_aggr.aggr_list)))
1342                                mwifiex_write_data_complete(adapter, skb_tmp,
1343                                                            0, -1);
1344                del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer);
1345                port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1346                port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1347        }
1348}
1349
1350static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1351{
1352        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1353
1354        mwifiex_usb_free(card);
1355
1356        mwifiex_usb_cleanup_tx_aggr(adapter);
1357
1358        card->adapter = NULL;
1359}
1360
1361static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1362                                    struct mwifiex_fw_image *fw)
1363{
1364        int ret = 0;
1365        u8 *firmware = fw->fw_buf, *recv_buff;
1366        u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1367        u32 dlen;
1368        u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1369        struct fw_data *fwdata;
1370        struct fw_sync_header sync_fw;
1371        u8 check_winner = 1;
1372
1373        if (!firmware) {
1374                mwifiex_dbg(adapter, ERROR,
1375                            "No firmware image found! Terminating download\n");
1376                ret = -1;
1377                goto fw_exit;
1378        }
1379
1380        /* Allocate memory for transmit */
1381        fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1382        if (!fwdata) {
1383                ret = -ENOMEM;
1384                goto fw_exit;
1385        }
1386
1387        /* Allocate memory for receive */
1388        recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1389        if (!recv_buff) {
1390                ret = -ENOMEM;
1391                goto cleanup;
1392        }
1393
1394        do {
1395                /* Send pseudo data to check winner status first */
1396                if (check_winner) {
1397                        memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1398                        dlen = 0;
1399                } else {
1400                        /* copy the header of the fw_data to get the length */
1401                        memcpy(&fwdata->fw_hdr, &firmware[tlen],
1402                               sizeof(struct fw_header));
1403
1404                        dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1405                        dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1406                        tlen += sizeof(struct fw_header);
1407
1408                        /* Command 7 doesn't have data length field */
1409                        if (dnld_cmd == FW_CMD_7)
1410                                dlen = 0;
1411
1412                        memcpy(fwdata->data, &firmware[tlen], dlen);
1413
1414                        fwdata->seq_num = cpu_to_le32(fw_seqnum);
1415                        tlen += dlen;
1416                }
1417
1418                /* If the send/receive fails or CRC occurs then retry */
1419                while (--retries) {
1420                        u8 *buf = (u8 *)fwdata;
1421                        u32 len = FW_DATA_XMIT_SIZE;
1422
1423                        /* send the firmware block */
1424                        ret = mwifiex_write_data_sync(adapter, buf, &len,
1425                                                MWIFIEX_USB_EP_CMD_EVENT,
1426                                                MWIFIEX_USB_TIMEOUT);
1427                        if (ret) {
1428                                mwifiex_dbg(adapter, ERROR,
1429                                            "write_data_sync: failed: %d\n",
1430                                            ret);
1431                                continue;
1432                        }
1433
1434                        buf = recv_buff;
1435                        len = FW_DNLD_RX_BUF_SIZE;
1436
1437                        /* Receive the firmware block response */
1438                        ret = mwifiex_read_data_sync(adapter, buf, &len,
1439                                                MWIFIEX_USB_EP_CMD_EVENT,
1440                                                MWIFIEX_USB_TIMEOUT);
1441                        if (ret) {
1442                                mwifiex_dbg(adapter, ERROR,
1443                                            "read_data_sync: failed: %d\n",
1444                                            ret);
1445                                continue;
1446                        }
1447
1448                        memcpy(&sync_fw, recv_buff,
1449                               sizeof(struct fw_sync_header));
1450
1451                        /* check 1st firmware block resp for highest bit set */
1452                        if (check_winner) {
1453                                if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1454                                        mwifiex_dbg(adapter, WARN,
1455                                                    "USB is not the winner %#x\n",
1456                                                    sync_fw.cmd);
1457
1458                                        /* returning success */
1459                                        ret = 0;
1460                                        goto cleanup;
1461                                }
1462
1463                                mwifiex_dbg(adapter, MSG,
1464                                            "start to download FW...\n");
1465
1466                                check_winner = 0;
1467                                break;
1468                        }
1469
1470                        /* check the firmware block response for CRC errors */
1471                        if (sync_fw.cmd) {
1472                                mwifiex_dbg(adapter, ERROR,
1473                                            "FW received block with CRC %#x\n",
1474                                            sync_fw.cmd);
1475                                ret = -1;
1476                                continue;
1477                        }
1478
1479                        retries = USB8XXX_FW_MAX_RETRY + 1;
1480                        break;
1481                }
1482                fw_seqnum++;
1483        } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1484
1485cleanup:
1486        mwifiex_dbg(adapter, MSG,
1487                    "info: FW download over, size %d bytes\n", tlen);
1488
1489        kfree(recv_buff);
1490        kfree(fwdata);
1491
1492        if (retries)
1493                ret = 0;
1494fw_exit:
1495        return ret;
1496}
1497
1498static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1499                        struct mwifiex_fw_image *fw)
1500{
1501        int ret;
1502        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1503
1504        if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1505                ret = mwifiex_prog_fw_w_helper(adapter, fw);
1506                if (ret)
1507                        return -1;
1508
1509                /* Boot state changes after successful firmware download */
1510                if (card->usb_boot_state == USB8XXX_FW_DNLD)
1511                        return -1;
1512        }
1513
1514        ret = mwifiex_usb_rx_init(adapter);
1515        if (!ret)
1516                ret = mwifiex_usb_tx_init(adapter);
1517
1518        return ret;
1519}
1520
1521static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1522{
1523        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1524
1525        skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1526        if ((ep == card->rx_cmd_ep) &&
1527            (!atomic_read(&card->rx_cmd_urb_pending)))
1528                mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1529                                          MWIFIEX_RX_CMD_BUF_SIZE);
1530
1531        return;
1532}
1533
1534static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1535                                       struct sk_buff *skb)
1536{
1537        mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1538
1539        return 0;
1540}
1541
1542/* This function wakes up the card. */
1543static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1544{
1545        /* Simulation of HS_AWAKE event */
1546        adapter->pm_wakeup_fw_try = false;
1547        del_timer(&adapter->wakeup_timer);
1548        adapter->pm_wakeup_card_req = false;
1549        adapter->ps_state = PS_STATE_AWAKE;
1550
1551        return 0;
1552}
1553
1554static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1555{
1556        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1557        int i;
1558        struct urb_context *ctx;
1559
1560        for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1561                if (card->rx_data_list[i].skb)
1562                        continue;
1563                ctx = &card->rx_data_list[i];
1564                mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1565        }
1566}
1567
1568/* This function is called after the card has woken up. */
1569static inline int
1570mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1571{
1572        return 0;
1573}
1574
1575static struct mwifiex_if_ops usb_ops = {
1576        .register_dev =         mwifiex_register_dev,
1577        .unregister_dev =       mwifiex_unregister_dev,
1578        .wakeup =               mwifiex_pm_wakeup_card,
1579        .wakeup_complete =      mwifiex_pm_wakeup_card_complete,
1580
1581        /* USB specific */
1582        .dnld_fw =              mwifiex_usb_dnld_fw,
1583        .cmdrsp_complete =      mwifiex_usb_cmd_event_complete,
1584        .event_complete =       mwifiex_usb_cmd_event_complete,
1585        .host_to_card =         mwifiex_usb_host_to_card,
1586        .submit_rem_rx_urbs =   mwifiex_usb_submit_rem_rx_urbs,
1587        .multi_port_resync =    mwifiex_usb_port_resync,
1588        .is_port_ready =        mwifiex_usb_is_port_ready,
1589};
1590
1591module_usb_driver(mwifiex_usb_driver);
1592
1593MODULE_AUTHOR("Marvell International Ltd.");
1594MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1595MODULE_VERSION(USB_VERSION);
1596MODULE_LICENSE("GPL v2");
1597MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
1598MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
1599MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
1600MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);
1601