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_usb_free(card);
 648
 649        mwifiex_dbg(adapter, FATAL,
 650                    "%s: removing card\n", __func__);
 651        mwifiex_remove_card(adapter);
 652
 653        usb_put_dev(interface_to_usbdev(intf));
 654}
 655
 656static struct usb_driver mwifiex_usb_driver = {
 657        .name = "mwifiex_usb",
 658        .probe = mwifiex_usb_probe,
 659        .disconnect = mwifiex_usb_disconnect,
 660        .id_table = mwifiex_usb_table,
 661        .suspend = mwifiex_usb_suspend,
 662        .resume = mwifiex_usb_resume,
 663        .soft_unbind = 1,
 664};
 665
 666static int mwifiex_write_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
 667                                   u32 *len, u8 ep, u32 timeout)
 668{
 669        struct usb_card_rec *card = adapter->card;
 670        int actual_length, ret;
 671
 672        if (!(*len % card->bulk_out_maxpktsize))
 673                (*len)++;
 674
 675        /* Send the data block */
 676        ret = usb_bulk_msg(card->udev, usb_sndbulkpipe(card->udev, ep), pbuf,
 677                           *len, &actual_length, timeout);
 678        if (ret) {
 679                mwifiex_dbg(adapter, ERROR,
 680                            "usb_bulk_msg for tx failed: %d\n", ret);
 681                return ret;
 682        }
 683
 684        *len = actual_length;
 685
 686        return ret;
 687}
 688
 689static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *pbuf,
 690                                  u32 *len, u8 ep, u32 timeout)
 691{
 692        struct usb_card_rec *card = adapter->card;
 693        int actual_length, ret;
 694
 695        /* Receive the data response */
 696        ret = usb_bulk_msg(card->udev, usb_rcvbulkpipe(card->udev, ep), pbuf,
 697                           *len, &actual_length, timeout);
 698        if (ret) {
 699                mwifiex_dbg(adapter, ERROR,
 700                            "usb_bulk_msg for rx failed: %d\n", ret);
 701                return ret;
 702        }
 703
 704        *len = actual_length;
 705
 706        return ret;
 707}
 708
 709static void mwifiex_usb_port_resync(struct mwifiex_adapter *adapter)
 710{
 711        struct usb_card_rec *card = adapter->card;
 712        u8 active_port = MWIFIEX_USB_EP_DATA;
 713        struct mwifiex_private *priv = NULL;
 714        int i;
 715
 716        if (adapter->usb_mc_status) {
 717                for (i = 0; i < adapter->priv_num; i++) {
 718                        priv = adapter->priv[i];
 719                        if (!priv)
 720                                continue;
 721                        if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
 722                             !priv->bss_started) ||
 723                            (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
 724                             !priv->media_connected))
 725                                priv->usb_port = MWIFIEX_USB_EP_DATA;
 726                }
 727                for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
 728                        card->port[i].block_status = false;
 729        } else {
 730                for (i = 0; i < adapter->priv_num; i++) {
 731                        priv = adapter->priv[i];
 732                        if (!priv)
 733                                continue;
 734                        if ((priv->bss_role == MWIFIEX_BSS_ROLE_UAP &&
 735                             priv->bss_started) ||
 736                            (priv->bss_role == MWIFIEX_BSS_ROLE_STA &&
 737                             priv->media_connected)) {
 738                                active_port = priv->usb_port;
 739                                break;
 740                        }
 741                }
 742                for (i = 0; i < adapter->priv_num; i++) {
 743                        priv = adapter->priv[i];
 744                        if (priv)
 745                                priv->usb_port = active_port;
 746                }
 747                for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
 748                        if (active_port == card->port[i].tx_data_ep)
 749                                card->port[i].block_status = false;
 750                        else
 751                                card->port[i].block_status = true;
 752                }
 753        }
 754}
 755
 756static bool mwifiex_usb_is_port_ready(struct mwifiex_private *priv)
 757{
 758        struct usb_card_rec *card = priv->adapter->card;
 759        int idx;
 760
 761        for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
 762                if (priv->usb_port == card->port[idx].tx_data_ep)
 763                        return !card->port[idx].block_status;
 764        }
 765
 766        return false;
 767}
 768
 769static inline u8 mwifiex_usb_data_sent(struct mwifiex_adapter *adapter)
 770{
 771        struct usb_card_rec *card = adapter->card;
 772        int i;
 773
 774        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++)
 775                if (!card->port[i].block_status)
 776                        return false;
 777
 778        return true;
 779}
 780
 781static int mwifiex_usb_construct_send_urb(struct mwifiex_adapter *adapter,
 782                                          struct usb_tx_data_port *port, u8 ep,
 783                                          struct urb_context *context,
 784                                          struct sk_buff *skb_send)
 785{
 786        struct usb_card_rec *card = adapter->card;
 787        int ret = -EINPROGRESS;
 788        struct urb *tx_urb;
 789
 790        context->adapter = adapter;
 791        context->ep = ep;
 792        context->skb = skb_send;
 793        tx_urb = context->urb;
 794
 795        if (ep == card->tx_cmd_ep &&
 796            card->tx_cmd_ep_type == USB_ENDPOINT_XFER_INT)
 797                usb_fill_int_urb(tx_urb, card->udev,
 798                                 usb_sndintpipe(card->udev, ep), skb_send->data,
 799                                 skb_send->len, mwifiex_usb_tx_complete,
 800                                 (void *)context, card->tx_cmd_interval);
 801        else
 802                usb_fill_bulk_urb(tx_urb, card->udev,
 803                                  usb_sndbulkpipe(card->udev, ep),
 804                                  skb_send->data, skb_send->len,
 805                                  mwifiex_usb_tx_complete, (void *)context);
 806
 807        tx_urb->transfer_flags |= URB_ZERO_PACKET;
 808
 809        if (ep == card->tx_cmd_ep)
 810                atomic_inc(&card->tx_cmd_urb_pending);
 811        else
 812                atomic_inc(&port->tx_data_urb_pending);
 813
 814        if (ep != card->tx_cmd_ep &&
 815            atomic_read(&port->tx_data_urb_pending) ==
 816                                        MWIFIEX_TX_DATA_URB) {
 817                port->block_status = true;
 818                adapter->data_sent = mwifiex_usb_data_sent(adapter);
 819                ret = -ENOSR;
 820        }
 821
 822        if (usb_submit_urb(tx_urb, GFP_ATOMIC)) {
 823                mwifiex_dbg(adapter, ERROR,
 824                            "%s: usb_submit_urb failed\n", __func__);
 825                if (ep == card->tx_cmd_ep) {
 826                        atomic_dec(&card->tx_cmd_urb_pending);
 827                } else {
 828                        atomic_dec(&port->tx_data_urb_pending);
 829                        port->block_status = false;
 830                        adapter->data_sent = false;
 831                        if (port->tx_data_ix)
 832                                port->tx_data_ix--;
 833                        else
 834                                port->tx_data_ix = MWIFIEX_TX_DATA_URB;
 835                }
 836                ret = -1;
 837        }
 838
 839        return ret;
 840}
 841
 842static int mwifiex_usb_prepare_tx_aggr_skb(struct mwifiex_adapter *adapter,
 843                                           struct usb_tx_data_port *port,
 844                                           struct sk_buff **skb_send)
 845{
 846        struct sk_buff *skb_aggr, *skb_tmp;
 847        u8 *payload, pad;
 848        u16 align = adapter->bus_aggr.tx_aggr_align;
 849        struct mwifiex_txinfo *tx_info = NULL;
 850        bool is_txinfo_set = false;
 851
 852        /* Packets in aggr_list will be send in either skb_aggr or
 853         * write complete, delete the tx_aggr timer
 854         */
 855        if (port->tx_aggr.timer_cnxt.is_hold_timer_set) {
 856                del_timer(&port->tx_aggr.timer_cnxt.hold_timer);
 857                port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
 858                port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
 859        }
 860
 861        skb_aggr = mwifiex_alloc_dma_align_buf(port->tx_aggr.aggr_len,
 862                                               GFP_ATOMIC);
 863        if (!skb_aggr) {
 864                mwifiex_dbg(adapter, ERROR,
 865                            "%s: alloc skb_aggr failed\n", __func__);
 866
 867                while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list)))
 868                        mwifiex_write_data_complete(adapter, skb_tmp, 0, -1);
 869
 870                port->tx_aggr.aggr_num = 0;
 871                port->tx_aggr.aggr_len = 0;
 872                return -EBUSY;
 873        }
 874
 875        tx_info = MWIFIEX_SKB_TXCB(skb_aggr);
 876        memset(tx_info, 0, sizeof(*tx_info));
 877
 878        while ((skb_tmp = skb_dequeue(&port->tx_aggr.aggr_list))) {
 879                /* padding for aligning next packet header*/
 880                pad = (align - (skb_tmp->len & (align - 1))) % align;
 881                payload = skb_put(skb_aggr, skb_tmp->len + pad);
 882                memcpy(payload, skb_tmp->data, skb_tmp->len);
 883                if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
 884                        /* do not padding for last packet*/
 885                        *(u16 *)payload = cpu_to_le16(skb_tmp->len);
 886                        *(u16 *)&payload[2] =
 887                                cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
 888                        skb_trim(skb_aggr, skb_aggr->len - pad);
 889                } else {
 890                        /* add aggregation interface header */
 891                        *(u16 *)payload = cpu_to_le16(skb_tmp->len + pad);
 892                        *(u16 *)&payload[2] =
 893                                cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2);
 894                }
 895
 896                if (!is_txinfo_set) {
 897                        tx_info->bss_num = MWIFIEX_SKB_TXCB(skb_tmp)->bss_num;
 898                        tx_info->bss_type = MWIFIEX_SKB_TXCB(skb_tmp)->bss_type;
 899                        is_txinfo_set = true;
 900                }
 901
 902                port->tx_aggr.aggr_num--;
 903                port->tx_aggr.aggr_len -= (skb_tmp->len + pad);
 904                mwifiex_write_data_complete(adapter, skb_tmp, 0, 0);
 905        }
 906
 907        tx_info->pkt_len = skb_aggr->len -
 908                        (sizeof(struct txpd) + adapter->intf_hdr_len);
 909        tx_info->flags |= MWIFIEX_BUF_FLAG_AGGR_PKT;
 910
 911        port->tx_aggr.aggr_num = 0;
 912        port->tx_aggr.aggr_len = 0;
 913        *skb_send = skb_aggr;
 914
 915        return 0;
 916}
 917
 918/* This function prepare data packet to be send under usb tx aggregation
 919 * protocol, check current usb aggregation status, link packet to aggrgation
 920 * list if possible, work flow as below:
 921 * (1) if only 1 packet available, add usb tx aggregation header and send.
 922 * (2) if packet is able to aggregated, link it to current aggregation list.
 923 * (3) if packet is not able to aggregated, aggregate and send exist packets
 924 *     in aggrgation list. Then, link packet in the list if there is more
 925 *     packet in transmit queue, otherwise try to transmit single packet.
 926 */
 927static int mwifiex_usb_aggr_tx_data(struct mwifiex_adapter *adapter, u8 ep,
 928                                    struct sk_buff *skb,
 929                                    struct mwifiex_tx_param *tx_param,
 930                                    struct usb_tx_data_port *port)
 931{
 932        u8 *payload, pad;
 933        u16 align = adapter->bus_aggr.tx_aggr_align;
 934        struct sk_buff *skb_send = NULL;
 935        struct urb_context *context = NULL;
 936        struct txpd *local_tx_pd =
 937                (struct txpd *)((u8 *)skb->data + adapter->intf_hdr_len);
 938        u8 f_send_aggr_buf = 0;
 939        u8 f_send_cur_buf = 0;
 940        u8 f_precopy_cur_buf = 0;
 941        u8 f_postcopy_cur_buf = 0;
 942        u32 timeout;
 943        int ret;
 944
 945        /* padding to ensure each packet alginment */
 946        pad = (align - (skb->len & (align - 1))) % align;
 947
 948        if (tx_param && tx_param->next_pkt_len) {
 949                /* next packet available in tx queue*/
 950                if (port->tx_aggr.aggr_len + skb->len + pad >
 951                    adapter->bus_aggr.tx_aggr_max_size) {
 952                        f_send_aggr_buf = 1;
 953                        f_postcopy_cur_buf = 1;
 954                } else {
 955                        /* current packet could be aggregated*/
 956                        f_precopy_cur_buf = 1;
 957
 958                        if (port->tx_aggr.aggr_len + skb->len + pad +
 959                            tx_param->next_pkt_len >
 960                            adapter->bus_aggr.tx_aggr_max_size ||
 961                            port->tx_aggr.aggr_num + 2 >
 962                            adapter->bus_aggr.tx_aggr_max_num) {
 963                            /* next packet could not be aggregated
 964                             * send current aggregation buffer
 965                             */
 966                                f_send_aggr_buf = 1;
 967                        }
 968                }
 969        } else {
 970                /* last packet in tx queue */
 971                if (port->tx_aggr.aggr_num > 0) {
 972                        /* pending packets in aggregation buffer*/
 973                        if (port->tx_aggr.aggr_len + skb->len + pad >
 974                            adapter->bus_aggr.tx_aggr_max_size) {
 975                                /* current packet not be able to aggregated,
 976                                 * send aggr buffer first, then send packet.
 977                                 */
 978                                f_send_cur_buf = 1;
 979                        } else {
 980                                /* last packet, Aggregation and send */
 981                                f_precopy_cur_buf = 1;
 982                        }
 983
 984                        f_send_aggr_buf = 1;
 985                } else {
 986                        /* no pending packets in aggregation buffer,
 987                         * send current packet immediately
 988                         */
 989                         f_send_cur_buf = 1;
 990                }
 991        }
 992
 993        if (local_tx_pd->flags & MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET) {
 994                /* Send NULL packet immediately*/
 995                if (f_precopy_cur_buf) {
 996                        if (skb_queue_empty(&port->tx_aggr.aggr_list)) {
 997                                f_precopy_cur_buf = 0;
 998                                f_send_aggr_buf = 0;
 999                                f_send_cur_buf = 1;
1000                        } else {
1001                                f_send_aggr_buf = 1;
1002                        }
1003                } else if (f_postcopy_cur_buf) {
1004                        f_send_cur_buf = 1;
1005                        f_postcopy_cur_buf = 0;
1006                }
1007        }
1008
1009        if (f_precopy_cur_buf) {
1010                skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1011                port->tx_aggr.aggr_len += (skb->len + pad);
1012                port->tx_aggr.aggr_num++;
1013                if (f_send_aggr_buf)
1014                        goto send_aggr_buf;
1015
1016                /* packet will not been send immediately,
1017                 * set a timer to make sure it will be sent under
1018                 * strict time limit. Dynamically fit the timeout
1019                 * value, according to packets number in aggr_list
1020                 */
1021                if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1022                        port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1023                                        MWIFIEX_USB_TX_AGGR_TMO_MIN;
1024                        timeout =
1025                                port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1026                        mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1027                                  jiffies + msecs_to_jiffies(timeout));
1028                        port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1029                } else {
1030                        if (port->tx_aggr.timer_cnxt.hold_tmo_msecs <
1031                            MWIFIEX_USB_TX_AGGR_TMO_MAX) {
1032                                /* Dyanmic fit timeout */
1033                                timeout =
1034                                ++port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1035                                mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1036                                          jiffies + msecs_to_jiffies(timeout));
1037                        }
1038                }
1039        }
1040
1041send_aggr_buf:
1042        if (f_send_aggr_buf) {
1043                ret = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1044                if (!ret) {
1045                        context = &port->tx_data_list[port->tx_data_ix++];
1046                        ret = mwifiex_usb_construct_send_urb(adapter, port, ep,
1047                                                             context, skb_send);
1048                        if (ret == -1)
1049                                mwifiex_write_data_complete(adapter, skb_send,
1050                                                            0, -1);
1051                }
1052        }
1053
1054        if (f_send_cur_buf) {
1055                if (f_send_aggr_buf) {
1056                        if (atomic_read(&port->tx_data_urb_pending) >=
1057                            MWIFIEX_TX_DATA_URB) {
1058                                port->block_status = true;
1059                                adapter->data_sent =
1060                                        mwifiex_usb_data_sent(adapter);
1061                                /* no available urb, postcopy packet*/
1062                                f_postcopy_cur_buf = 1;
1063                                goto postcopy_cur_buf;
1064                        }
1065
1066                        if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1067                                port->tx_data_ix = 0;
1068                }
1069
1070                payload = skb->data;
1071                *(u16 *)&payload[2] =
1072                        cpu_to_le16(MWIFIEX_TYPE_AGGR_DATA_V2 | 0x80);
1073                *(u16 *)payload = cpu_to_le16(skb->len);
1074                skb_send = skb;
1075                context = &port->tx_data_list[port->tx_data_ix++];
1076                return mwifiex_usb_construct_send_urb(adapter, port, ep,
1077                                                      context, skb_send);
1078        }
1079
1080postcopy_cur_buf:
1081        if (f_postcopy_cur_buf) {
1082                skb_queue_tail(&port->tx_aggr.aggr_list, skb);
1083                port->tx_aggr.aggr_len += (skb->len + pad);
1084                port->tx_aggr.aggr_num++;
1085                /* New aggregation begin, start timer */
1086                if (!port->tx_aggr.timer_cnxt.is_hold_timer_set) {
1087                        port->tx_aggr.timer_cnxt.hold_tmo_msecs =
1088                                        MWIFIEX_USB_TX_AGGR_TMO_MIN;
1089                        timeout = port->tx_aggr.timer_cnxt.hold_tmo_msecs;
1090                        mod_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1091                                  jiffies + msecs_to_jiffies(timeout));
1092                        port->tx_aggr.timer_cnxt.is_hold_timer_set = true;
1093                }
1094        }
1095
1096        return -EINPROGRESS;
1097}
1098
1099static void mwifiex_usb_tx_aggr_tmo(unsigned long context)
1100{
1101        struct urb_context *urb_cnxt = NULL;
1102        struct sk_buff *skb_send = NULL;
1103        struct tx_aggr_tmr_cnxt *timer_context =
1104                (struct tx_aggr_tmr_cnxt *)context;
1105        struct mwifiex_adapter *adapter = timer_context->adapter;
1106        struct usb_tx_data_port *port = timer_context->port;
1107        unsigned long flags;
1108        int err = 0;
1109
1110        spin_lock_irqsave(&port->tx_aggr_lock, flags);
1111        err = mwifiex_usb_prepare_tx_aggr_skb(adapter, port, &skb_send);
1112        if (err) {
1113                mwifiex_dbg(adapter, ERROR,
1114                            "prepare tx aggr skb failed, err=%d\n", err);
1115                goto unlock;
1116        }
1117
1118        if (atomic_read(&port->tx_data_urb_pending) >=
1119            MWIFIEX_TX_DATA_URB) {
1120                port->block_status = true;
1121                adapter->data_sent =
1122                        mwifiex_usb_data_sent(adapter);
1123                err = -1;
1124                goto done;
1125        }
1126
1127        if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1128                port->tx_data_ix = 0;
1129
1130        urb_cnxt = &port->tx_data_list[port->tx_data_ix++];
1131        err = mwifiex_usb_construct_send_urb(adapter, port, port->tx_data_ep,
1132                                             urb_cnxt, skb_send);
1133done:
1134        if (err == -1)
1135                mwifiex_write_data_complete(adapter, skb_send, 0, -1);
1136unlock:
1137        spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1138}
1139
1140/* This function write a command/data packet to card. */
1141static int mwifiex_usb_host_to_card(struct mwifiex_adapter *adapter, u8 ep,
1142                                    struct sk_buff *skb,
1143                                    struct mwifiex_tx_param *tx_param)
1144{
1145        struct usb_card_rec *card = adapter->card;
1146        struct urb_context *context = NULL;
1147        struct usb_tx_data_port *port = NULL;
1148        unsigned long flags;
1149        int idx, ret;
1150
1151        if (adapter->is_suspended) {
1152                mwifiex_dbg(adapter, ERROR,
1153                            "%s: not allowed while suspended\n", __func__);
1154                return -1;
1155        }
1156
1157        if (adapter->surprise_removed) {
1158                mwifiex_dbg(adapter, ERROR, "%s: device removed\n", __func__);
1159                return -1;
1160        }
1161
1162        mwifiex_dbg(adapter, INFO, "%s: ep=%d\n", __func__, ep);
1163
1164        if (ep == card->tx_cmd_ep) {
1165                context = &card->tx_cmd;
1166        } else {
1167                /* get the data port structure for endpoint */
1168                for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1169                        if (ep == card->port[idx].tx_data_ep) {
1170                                port = &card->port[idx];
1171                                if (atomic_read(&port->tx_data_urb_pending)
1172                                    >= MWIFIEX_TX_DATA_URB) {
1173                                        port->block_status = true;
1174                                        adapter->data_sent =
1175                                                mwifiex_usb_data_sent(adapter);
1176                                        return -EBUSY;
1177                                }
1178                                if (port->tx_data_ix >= MWIFIEX_TX_DATA_URB)
1179                                        port->tx_data_ix = 0;
1180                                break;
1181                        }
1182                }
1183
1184                if (!port) {
1185                        mwifiex_dbg(adapter, ERROR, "Wrong usb tx data port\n");
1186                        return -1;
1187                }
1188
1189                if (adapter->bus_aggr.enable) {
1190                        spin_lock_irqsave(&port->tx_aggr_lock, flags);
1191                        ret =  mwifiex_usb_aggr_tx_data(adapter, ep, skb,
1192                                                        tx_param, port);
1193                        spin_unlock_irqrestore(&port->tx_aggr_lock, flags);
1194                        return ret;
1195                }
1196
1197                context = &port->tx_data_list[port->tx_data_ix++];
1198        }
1199
1200        return mwifiex_usb_construct_send_urb(adapter, port, ep, context, skb);
1201}
1202
1203static int mwifiex_usb_tx_init(struct mwifiex_adapter *adapter)
1204{
1205        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1206        struct usb_tx_data_port *port;
1207        int i, j;
1208
1209        card->tx_cmd.adapter = adapter;
1210        card->tx_cmd.ep = card->tx_cmd_ep;
1211
1212        card->tx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1213        if (!card->tx_cmd.urb)
1214                return -ENOMEM;
1215
1216        for (i = 0; i < MWIFIEX_TX_DATA_PORT; i++) {
1217                port = &card->port[i];
1218                if (!port->tx_data_ep)
1219                        continue;
1220                port->tx_data_ix = 0;
1221                skb_queue_head_init(&port->tx_aggr.aggr_list);
1222                if (port->tx_data_ep == MWIFIEX_USB_EP_DATA)
1223                        port->block_status = false;
1224                else
1225                        port->block_status = true;
1226                for (j = 0; j < MWIFIEX_TX_DATA_URB; j++) {
1227                        port->tx_data_list[j].adapter = adapter;
1228                        port->tx_data_list[j].ep = port->tx_data_ep;
1229                        port->tx_data_list[j].urb =
1230                                        usb_alloc_urb(0, GFP_KERNEL);
1231                        if (!port->tx_data_list[j].urb)
1232                                return -ENOMEM;
1233                }
1234
1235                port->tx_aggr.timer_cnxt.adapter = adapter;
1236                port->tx_aggr.timer_cnxt.port = port;
1237                port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1238                port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1239                setup_timer(&port->tx_aggr.timer_cnxt.hold_timer,
1240                            mwifiex_usb_tx_aggr_tmo,
1241                            (unsigned long)&port->tx_aggr.timer_cnxt);
1242        }
1243
1244        return 0;
1245}
1246
1247static int mwifiex_usb_rx_init(struct mwifiex_adapter *adapter)
1248{
1249        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1250        int i;
1251
1252        card->rx_cmd.adapter = adapter;
1253        card->rx_cmd.ep = card->rx_cmd_ep;
1254
1255        card->rx_cmd.urb = usb_alloc_urb(0, GFP_KERNEL);
1256        if (!card->rx_cmd.urb)
1257                return -ENOMEM;
1258
1259        card->rx_cmd.skb = dev_alloc_skb(MWIFIEX_RX_CMD_BUF_SIZE);
1260        if (!card->rx_cmd.skb)
1261                return -ENOMEM;
1262
1263        if (mwifiex_usb_submit_rx_urb(&card->rx_cmd, MWIFIEX_RX_CMD_BUF_SIZE))
1264                return -1;
1265
1266        for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1267                card->rx_data_list[i].adapter = adapter;
1268                card->rx_data_list[i].ep = card->rx_data_ep;
1269
1270                card->rx_data_list[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1271                if (!card->rx_data_list[i].urb)
1272                        return -1;
1273                if (mwifiex_usb_submit_rx_urb(&card->rx_data_list[i],
1274                                              MWIFIEX_RX_DATA_BUF_SIZE))
1275                        return -1;
1276        }
1277
1278        return 0;
1279}
1280
1281/* This function register usb device and initialize parameter. */
1282static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
1283{
1284        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1285
1286        card->adapter = adapter;
1287
1288        switch (le16_to_cpu(card->udev->descriptor.idProduct)) {
1289        case USB8997_PID_1:
1290        case USB8997_PID_2:
1291                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_4K;
1292                strcpy(adapter->fw_name, USB8997_DEFAULT_FW_NAME);
1293                adapter->ext_scan = true;
1294                break;
1295        case USB8766_PID_1:
1296        case USB8766_PID_2:
1297                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1298                strcpy(adapter->fw_name, USB8766_DEFAULT_FW_NAME);
1299                adapter->ext_scan = true;
1300                break;
1301        case USB8801_PID_1:
1302        case USB8801_PID_2:
1303                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1304                strcpy(adapter->fw_name, USB8801_DEFAULT_FW_NAME);
1305                adapter->ext_scan = false;
1306                break;
1307        case USB8797_PID_1:
1308        case USB8797_PID_2:
1309        default:
1310                adapter->tx_buf_size = MWIFIEX_TX_DATA_BUF_SIZE_2K;
1311                strcpy(adapter->fw_name, USB8797_DEFAULT_FW_NAME);
1312                break;
1313        }
1314
1315        adapter->usb_mc_status = false;
1316        adapter->usb_mc_setup = false;
1317
1318        return 0;
1319}
1320
1321static void mwifiex_usb_cleanup_tx_aggr(struct mwifiex_adapter *adapter)
1322{
1323        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1324        struct usb_tx_data_port *port;
1325        struct sk_buff *skb_tmp;
1326        int idx;
1327
1328        for (idx = 0; idx < MWIFIEX_TX_DATA_PORT; idx++) {
1329                port = &card->port[idx];
1330                if (adapter->bus_aggr.enable)
1331                        while ((skb_tmp =
1332                                skb_dequeue(&port->tx_aggr.aggr_list)))
1333                                mwifiex_write_data_complete(adapter, skb_tmp,
1334                                                            0, -1);
1335                del_timer_sync(&port->tx_aggr.timer_cnxt.hold_timer);
1336                port->tx_aggr.timer_cnxt.is_hold_timer_set = false;
1337                port->tx_aggr.timer_cnxt.hold_tmo_msecs = 0;
1338        }
1339}
1340
1341static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
1342{
1343        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1344
1345        mwifiex_usb_cleanup_tx_aggr(adapter);
1346
1347        card->adapter = NULL;
1348}
1349
1350static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
1351                                    struct mwifiex_fw_image *fw)
1352{
1353        int ret = 0;
1354        u8 *firmware = fw->fw_buf, *recv_buff;
1355        u32 retries = USB8XXX_FW_MAX_RETRY + 1;
1356        u32 dlen;
1357        u32 fw_seqnum = 0, tlen = 0, dnld_cmd = 0;
1358        struct fw_data *fwdata;
1359        struct fw_sync_header sync_fw;
1360        u8 check_winner = 1;
1361
1362        if (!firmware) {
1363                mwifiex_dbg(adapter, ERROR,
1364                            "No firmware image found! Terminating download\n");
1365                ret = -1;
1366                goto fw_exit;
1367        }
1368
1369        /* Allocate memory for transmit */
1370        fwdata = kzalloc(FW_DNLD_TX_BUF_SIZE, GFP_KERNEL);
1371        if (!fwdata) {
1372                ret = -ENOMEM;
1373                goto fw_exit;
1374        }
1375
1376        /* Allocate memory for receive */
1377        recv_buff = kzalloc(FW_DNLD_RX_BUF_SIZE, GFP_KERNEL);
1378        if (!recv_buff) {
1379                ret = -ENOMEM;
1380                goto cleanup;
1381        }
1382
1383        do {
1384                /* Send pseudo data to check winner status first */
1385                if (check_winner) {
1386                        memset(&fwdata->fw_hdr, 0, sizeof(struct fw_header));
1387                        dlen = 0;
1388                } else {
1389                        /* copy the header of the fw_data to get the length */
1390                        memcpy(&fwdata->fw_hdr, &firmware[tlen],
1391                               sizeof(struct fw_header));
1392
1393                        dlen = le32_to_cpu(fwdata->fw_hdr.data_len);
1394                        dnld_cmd = le32_to_cpu(fwdata->fw_hdr.dnld_cmd);
1395                        tlen += sizeof(struct fw_header);
1396
1397                        /* Command 7 doesn't have data length field */
1398                        if (dnld_cmd == FW_CMD_7)
1399                                dlen = 0;
1400
1401                        memcpy(fwdata->data, &firmware[tlen], dlen);
1402
1403                        fwdata->seq_num = cpu_to_le32(fw_seqnum);
1404                        tlen += dlen;
1405                }
1406
1407                /* If the send/receive fails or CRC occurs then retry */
1408                while (--retries) {
1409                        u8 *buf = (u8 *)fwdata;
1410                        u32 len = FW_DATA_XMIT_SIZE;
1411
1412                        /* send the firmware block */
1413                        ret = mwifiex_write_data_sync(adapter, buf, &len,
1414                                                MWIFIEX_USB_EP_CMD_EVENT,
1415                                                MWIFIEX_USB_TIMEOUT);
1416                        if (ret) {
1417                                mwifiex_dbg(adapter, ERROR,
1418                                            "write_data_sync: failed: %d\n",
1419                                            ret);
1420                                continue;
1421                        }
1422
1423                        buf = recv_buff;
1424                        len = FW_DNLD_RX_BUF_SIZE;
1425
1426                        /* Receive the firmware block response */
1427                        ret = mwifiex_read_data_sync(adapter, buf, &len,
1428                                                MWIFIEX_USB_EP_CMD_EVENT,
1429                                                MWIFIEX_USB_TIMEOUT);
1430                        if (ret) {
1431                                mwifiex_dbg(adapter, ERROR,
1432                                            "read_data_sync: failed: %d\n",
1433                                            ret);
1434                                continue;
1435                        }
1436
1437                        memcpy(&sync_fw, recv_buff,
1438                               sizeof(struct fw_sync_header));
1439
1440                        /* check 1st firmware block resp for highest bit set */
1441                        if (check_winner) {
1442                                if (le32_to_cpu(sync_fw.cmd) & 0x80000000) {
1443                                        mwifiex_dbg(adapter, WARN,
1444                                                    "USB is not the winner %#x\n",
1445                                                    sync_fw.cmd);
1446
1447                                        /* returning success */
1448                                        ret = 0;
1449                                        goto cleanup;
1450                                }
1451
1452                                mwifiex_dbg(adapter, MSG,
1453                                            "start to download FW...\n");
1454
1455                                check_winner = 0;
1456                                break;
1457                        }
1458
1459                        /* check the firmware block response for CRC errors */
1460                        if (sync_fw.cmd) {
1461                                mwifiex_dbg(adapter, ERROR,
1462                                            "FW received block with CRC %#x\n",
1463                                            sync_fw.cmd);
1464                                ret = -1;
1465                                continue;
1466                        }
1467
1468                        retries = USB8XXX_FW_MAX_RETRY + 1;
1469                        break;
1470                }
1471                fw_seqnum++;
1472        } while ((dnld_cmd != FW_HAS_LAST_BLOCK) && retries);
1473
1474cleanup:
1475        mwifiex_dbg(adapter, MSG,
1476                    "info: FW download over, size %d bytes\n", tlen);
1477
1478        kfree(recv_buff);
1479        kfree(fwdata);
1480
1481        if (retries)
1482                ret = 0;
1483fw_exit:
1484        return ret;
1485}
1486
1487static int mwifiex_usb_dnld_fw(struct mwifiex_adapter *adapter,
1488                        struct mwifiex_fw_image *fw)
1489{
1490        int ret;
1491        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1492
1493        if (card->usb_boot_state == USB8XXX_FW_DNLD) {
1494                ret = mwifiex_prog_fw_w_helper(adapter, fw);
1495                if (ret)
1496                        return -1;
1497
1498                /* Boot state changes after successful firmware download */
1499                if (card->usb_boot_state == USB8XXX_FW_DNLD)
1500                        return -1;
1501        }
1502
1503        ret = mwifiex_usb_rx_init(adapter);
1504        if (!ret)
1505                ret = mwifiex_usb_tx_init(adapter);
1506
1507        return ret;
1508}
1509
1510static void mwifiex_submit_rx_urb(struct mwifiex_adapter *adapter, u8 ep)
1511{
1512        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1513
1514        skb_push(card->rx_cmd.skb, INTF_HEADER_LEN);
1515        if ((ep == card->rx_cmd_ep) &&
1516            (!atomic_read(&card->rx_cmd_urb_pending)))
1517                mwifiex_usb_submit_rx_urb(&card->rx_cmd,
1518                                          MWIFIEX_RX_CMD_BUF_SIZE);
1519
1520        return;
1521}
1522
1523static int mwifiex_usb_cmd_event_complete(struct mwifiex_adapter *adapter,
1524                                       struct sk_buff *skb)
1525{
1526        mwifiex_submit_rx_urb(adapter, MWIFIEX_USB_EP_CMD_EVENT);
1527
1528        return 0;
1529}
1530
1531/* This function wakes up the card. */
1532static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
1533{
1534        /* Simulation of HS_AWAKE event */
1535        adapter->pm_wakeup_fw_try = false;
1536        del_timer(&adapter->wakeup_timer);
1537        adapter->pm_wakeup_card_req = false;
1538        adapter->ps_state = PS_STATE_AWAKE;
1539
1540        return 0;
1541}
1542
1543static void mwifiex_usb_submit_rem_rx_urbs(struct mwifiex_adapter *adapter)
1544{
1545        struct usb_card_rec *card = (struct usb_card_rec *)adapter->card;
1546        int i;
1547        struct urb_context *ctx;
1548
1549        for (i = 0; i < MWIFIEX_RX_DATA_URB; i++) {
1550                if (card->rx_data_list[i].skb)
1551                        continue;
1552                ctx = &card->rx_data_list[i];
1553                mwifiex_usb_submit_rx_urb(ctx, MWIFIEX_RX_DATA_BUF_SIZE);
1554        }
1555}
1556
1557/* This function is called after the card has woken up. */
1558static inline int
1559mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
1560{
1561        return 0;
1562}
1563
1564static struct mwifiex_if_ops usb_ops = {
1565        .register_dev =         mwifiex_register_dev,
1566        .unregister_dev =       mwifiex_unregister_dev,
1567        .wakeup =               mwifiex_pm_wakeup_card,
1568        .wakeup_complete =      mwifiex_pm_wakeup_card_complete,
1569
1570        /* USB specific */
1571        .dnld_fw =              mwifiex_usb_dnld_fw,
1572        .cmdrsp_complete =      mwifiex_usb_cmd_event_complete,
1573        .event_complete =       mwifiex_usb_cmd_event_complete,
1574        .host_to_card =         mwifiex_usb_host_to_card,
1575        .submit_rem_rx_urbs =   mwifiex_usb_submit_rem_rx_urbs,
1576        .multi_port_resync =    mwifiex_usb_port_resync,
1577        .is_port_ready =        mwifiex_usb_is_port_ready,
1578};
1579
1580module_usb_driver(mwifiex_usb_driver);
1581
1582MODULE_AUTHOR("Marvell International Ltd.");
1583MODULE_DESCRIPTION("Marvell WiFi-Ex USB Driver version" USB_VERSION);
1584MODULE_VERSION(USB_VERSION);
1585MODULE_LICENSE("GPL v2");
1586MODULE_FIRMWARE(USB8766_DEFAULT_FW_NAME);
1587MODULE_FIRMWARE(USB8797_DEFAULT_FW_NAME);
1588MODULE_FIRMWARE(USB8801_DEFAULT_FW_NAME);
1589MODULE_FIRMWARE(USB8997_DEFAULT_FW_NAME);
1590