linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2011 Broadcom Corporation
   4 */
   5
   6#include <linux/kernel.h>
   7#include <linux/module.h>
   8#include <linux/firmware.h>
   9#include <linux/usb.h>
  10#include <linux/vmalloc.h>
  11
  12#include <brcmu_utils.h>
  13#include <brcm_hw_ids.h>
  14#include <brcmu_wifi.h>
  15#include "bus.h"
  16#include "debug.h"
  17#include "firmware.h"
  18#include "usb.h"
  19#include "core.h"
  20#include "common.h"
  21#include "bcdc.h"
  22
  23
  24#define IOCTL_RESP_TIMEOUT              msecs_to_jiffies(2000)
  25
  26#define BRCMF_USB_RESET_GETVER_SPINWAIT 100     /* in unit of ms */
  27#define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
  28
  29#define BRCMF_POSTBOOT_ID               0xA123  /* ID to detect if dongle
  30                                                   has boot up */
  31#define BRCMF_USB_NRXQ                  50
  32#define BRCMF_USB_NTXQ                  50
  33
  34#define BRCMF_USB_CBCTL_WRITE           0
  35#define BRCMF_USB_CBCTL_READ            1
  36#define BRCMF_USB_MAX_PKT_SIZE          1600
  37
  38BRCMF_FW_DEF(43143, "brcmfmac43143");
  39BRCMF_FW_DEF(43236B, "brcmfmac43236b");
  40BRCMF_FW_DEF(43242A, "brcmfmac43242a");
  41BRCMF_FW_DEF(43569, "brcmfmac43569");
  42BRCMF_FW_DEF(4373, "brcmfmac4373");
  43
  44static const struct brcmf_firmware_mapping brcmf_usb_fwnames[] = {
  45        BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
  46        BRCMF_FW_ENTRY(BRCM_CC_43235_CHIP_ID, 0x00000008, 43236B),
  47        BRCMF_FW_ENTRY(BRCM_CC_43236_CHIP_ID, 0x00000008, 43236B),
  48        BRCMF_FW_ENTRY(BRCM_CC_43238_CHIP_ID, 0x00000008, 43236B),
  49        BRCMF_FW_ENTRY(BRCM_CC_43242_CHIP_ID, 0xFFFFFFFF, 43242A),
  50        BRCMF_FW_ENTRY(BRCM_CC_43566_CHIP_ID, 0xFFFFFFFF, 43569),
  51        BRCMF_FW_ENTRY(BRCM_CC_43569_CHIP_ID, 0xFFFFFFFF, 43569),
  52        BRCMF_FW_ENTRY(CY_CC_4373_CHIP_ID, 0xFFFFFFFF, 4373)
  53};
  54
  55#define TRX_MAGIC               0x30524448      /* "HDR0" */
  56#define TRX_MAX_OFFSET          3               /* Max number of file offsets */
  57#define TRX_UNCOMP_IMAGE        0x20            /* Trx holds uncompressed img */
  58#define TRX_RDL_CHUNK           1500            /* size of each dl transfer */
  59#define TRX_OFFSETS_DLFWLEN_IDX 0
  60
  61/* Control messages: bRequest values */
  62#define DL_GETSTATE     0       /* returns the rdl_state_t struct */
  63#define DL_CHECK_CRC    1       /* currently unused */
  64#define DL_GO           2       /* execute downloaded image */
  65#define DL_START        3       /* initialize dl state */
  66#define DL_REBOOT       4       /* reboot the device in 2 seconds */
  67#define DL_GETVER       5       /* returns the bootrom_id_t struct */
  68#define DL_GO_PROTECTED 6       /* execute the downloaded code and set reset
  69                                 * event to occur in 2 seconds.  It is the
  70                                 * responsibility of the downloaded code to
  71                                 * clear this event
  72                                 */
  73#define DL_EXEC         7       /* jump to a supplied address */
  74#define DL_RESETCFG     8       /* To support single enum on dongle
  75                                 * - Not used by bootloader
  76                                 */
  77#define DL_DEFER_RESP_OK 9      /* Potentially defer the response to setup
  78                                 * if resp unavailable
  79                                 */
  80
  81/* states */
  82#define DL_WAITING      0       /* waiting to rx first pkt */
  83#define DL_READY        1       /* hdr was good, waiting for more of the
  84                                 * compressed image
  85                                 */
  86#define DL_BAD_HDR      2       /* hdr was corrupted */
  87#define DL_BAD_CRC      3       /* compressed image was corrupted */
  88#define DL_RUNNABLE     4       /* download was successful,waiting for go cmd */
  89#define DL_START_FAIL   5       /* failed to initialize correctly */
  90#define DL_NVRAM_TOOBIG 6       /* host specified nvram data exceeds DL_NVRAM
  91                                 * value
  92                                 */
  93#define DL_IMAGE_TOOBIG 7       /* firmware image too big */
  94
  95
  96struct trx_header_le {
  97        __le32 magic;           /* "HDR0" */
  98        __le32 len;             /* Length of file including header */
  99        __le32 crc32;           /* CRC from flag_version to end of file */
 100        __le32 flag_version;    /* 0:15 flags, 16:31 version */
 101        __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
 102                                         * header
 103                                         */
 104};
 105
 106struct rdl_state_le {
 107        __le32 state;
 108        __le32 bytes;
 109};
 110
 111struct bootrom_id_le {
 112        __le32 chip;            /* Chip id */
 113        __le32 chiprev;         /* Chip rev */
 114        __le32 ramsize;         /* Size of  RAM */
 115        __le32 remapbase;       /* Current remap base address */
 116        __le32 boardtype;       /* Type of board */
 117        __le32 boardrev;        /* Board revision */
 118};
 119
 120struct brcmf_usb_image {
 121        struct list_head list;
 122        s8 *fwname;
 123        u8 *image;
 124        int image_len;
 125};
 126
 127struct brcmf_usbdev_info {
 128        struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
 129        spinlock_t qlock;
 130        struct list_head rx_freeq;
 131        struct list_head rx_postq;
 132        struct list_head tx_freeq;
 133        struct list_head tx_postq;
 134        uint rx_pipe, tx_pipe;
 135
 136        int rx_low_watermark;
 137        int tx_low_watermark;
 138        int tx_high_watermark;
 139        int tx_freecount;
 140        bool tx_flowblock;
 141        spinlock_t tx_flowblock_lock;
 142
 143        struct brcmf_usbreq *tx_reqs;
 144        struct brcmf_usbreq *rx_reqs;
 145
 146        char fw_name[BRCMF_FW_NAME_LEN];
 147        const u8 *image;        /* buffer for combine fw and nvram */
 148        int image_len;
 149
 150        struct usb_device *usbdev;
 151        struct device *dev;
 152        struct completion dev_init_done;
 153
 154        int ctl_in_pipe, ctl_out_pipe;
 155        struct urb *ctl_urb; /* URB for control endpoint */
 156        struct usb_ctrlrequest ctl_write;
 157        struct usb_ctrlrequest ctl_read;
 158        u32 ctl_urb_actual_length;
 159        int ctl_urb_status;
 160        int ctl_completed;
 161        wait_queue_head_t ioctl_resp_wait;
 162        ulong ctl_op;
 163        u8 ifnum;
 164
 165        struct urb *bulk_urb; /* used for FW download */
 166
 167        struct brcmf_mp_device *settings;
 168};
 169
 170static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
 171                                struct brcmf_usbreq  *req);
 172
 173static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
 174{
 175        struct brcmf_bus *bus_if = dev_get_drvdata(dev);
 176        return bus_if->bus_priv.usb;
 177}
 178
 179static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
 180{
 181        return brcmf_usb_get_buspub(dev)->devinfo;
 182}
 183
 184static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
 185{
 186        return wait_event_timeout(devinfo->ioctl_resp_wait,
 187                                  devinfo->ctl_completed, IOCTL_RESP_TIMEOUT);
 188}
 189
 190static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
 191{
 192        wake_up(&devinfo->ioctl_resp_wait);
 193}
 194
 195static void
 196brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
 197{
 198        brcmf_dbg(USB, "Enter, status=%d\n", status);
 199
 200        if (unlikely(devinfo == NULL))
 201                return;
 202
 203        if (type == BRCMF_USB_CBCTL_READ) {
 204                if (status == 0)
 205                        devinfo->bus_pub.stats.rx_ctlpkts++;
 206                else
 207                        devinfo->bus_pub.stats.rx_ctlerrs++;
 208        } else if (type == BRCMF_USB_CBCTL_WRITE) {
 209                if (status == 0)
 210                        devinfo->bus_pub.stats.tx_ctlpkts++;
 211                else
 212                        devinfo->bus_pub.stats.tx_ctlerrs++;
 213        }
 214
 215        devinfo->ctl_urb_status = status;
 216        devinfo->ctl_completed = true;
 217        brcmf_usb_ioctl_resp_wake(devinfo);
 218}
 219
 220static void
 221brcmf_usb_ctlread_complete(struct urb *urb)
 222{
 223        struct brcmf_usbdev_info *devinfo =
 224                (struct brcmf_usbdev_info *)urb->context;
 225
 226        brcmf_dbg(USB, "Enter\n");
 227        devinfo->ctl_urb_actual_length = urb->actual_length;
 228        brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
 229                urb->status);
 230}
 231
 232static void
 233brcmf_usb_ctlwrite_complete(struct urb *urb)
 234{
 235        struct brcmf_usbdev_info *devinfo =
 236                (struct brcmf_usbdev_info *)urb->context;
 237
 238        brcmf_dbg(USB, "Enter\n");
 239        brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
 240                urb->status);
 241}
 242
 243static int
 244brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
 245{
 246        int ret;
 247        u16 size;
 248
 249        brcmf_dbg(USB, "Enter\n");
 250        if (devinfo == NULL || buf == NULL ||
 251            len == 0 || devinfo->ctl_urb == NULL)
 252                return -EINVAL;
 253
 254        size = len;
 255        devinfo->ctl_write.wLength = cpu_to_le16p(&size);
 256        devinfo->ctl_urb->transfer_buffer_length = size;
 257        devinfo->ctl_urb_status = 0;
 258        devinfo->ctl_urb_actual_length = 0;
 259
 260        usb_fill_control_urb(devinfo->ctl_urb,
 261                devinfo->usbdev,
 262                devinfo->ctl_out_pipe,
 263                (unsigned char *) &devinfo->ctl_write,
 264                buf, size,
 265                (usb_complete_t)brcmf_usb_ctlwrite_complete,
 266                devinfo);
 267
 268        ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
 269        if (ret < 0)
 270                brcmf_err("usb_submit_urb failed %d\n", ret);
 271
 272        return ret;
 273}
 274
 275static int
 276brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
 277{
 278        int ret;
 279        u16 size;
 280
 281        brcmf_dbg(USB, "Enter\n");
 282        if ((devinfo == NULL) || (buf == NULL) || (len == 0)
 283                || (devinfo->ctl_urb == NULL))
 284                return -EINVAL;
 285
 286        size = len;
 287        devinfo->ctl_read.wLength = cpu_to_le16p(&size);
 288        devinfo->ctl_urb->transfer_buffer_length = size;
 289
 290        devinfo->ctl_read.bRequestType = USB_DIR_IN
 291                | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
 292        devinfo->ctl_read.bRequest = 1;
 293
 294        usb_fill_control_urb(devinfo->ctl_urb,
 295                devinfo->usbdev,
 296                devinfo->ctl_in_pipe,
 297                (unsigned char *) &devinfo->ctl_read,
 298                buf, size,
 299                (usb_complete_t)brcmf_usb_ctlread_complete,
 300                devinfo);
 301
 302        ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
 303        if (ret < 0)
 304                brcmf_err("usb_submit_urb failed %d\n", ret);
 305
 306        return ret;
 307}
 308
 309static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
 310{
 311        int err = 0;
 312        int timeout = 0;
 313        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
 314        struct usb_interface *intf = to_usb_interface(dev);
 315
 316        brcmf_dbg(USB, "Enter\n");
 317
 318        err = usb_autopm_get_interface(intf);
 319        if (err)
 320                goto out;
 321
 322        if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
 323                err = -EIO;
 324                goto fail;
 325        }
 326
 327        if (test_and_set_bit(0, &devinfo->ctl_op)) {
 328                err = -EIO;
 329                goto fail;
 330        }
 331
 332        devinfo->ctl_completed = false;
 333        err = brcmf_usb_send_ctl(devinfo, buf, len);
 334        if (err) {
 335                brcmf_err("fail %d bytes: %d\n", err, len);
 336                clear_bit(0, &devinfo->ctl_op);
 337                goto fail;
 338        }
 339        timeout = brcmf_usb_ioctl_resp_wait(devinfo);
 340        if (!timeout) {
 341                brcmf_err("Txctl wait timed out\n");
 342                usb_kill_urb(devinfo->ctl_urb);
 343                err = -EIO;
 344                goto fail;
 345        }
 346        clear_bit(0, &devinfo->ctl_op);
 347
 348fail:
 349        usb_autopm_put_interface(intf);
 350out:
 351        return err;
 352}
 353
 354static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
 355{
 356        int err = 0;
 357        int timeout = 0;
 358        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
 359        struct usb_interface *intf = to_usb_interface(dev);
 360
 361        brcmf_dbg(USB, "Enter\n");
 362
 363        err = usb_autopm_get_interface(intf);
 364        if (err)
 365                goto out;
 366
 367        if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
 368                err = -EIO;
 369                goto fail;
 370        }
 371
 372        if (test_and_set_bit(0, &devinfo->ctl_op)) {
 373                err = -EIO;
 374                goto fail;
 375        }
 376
 377        devinfo->ctl_completed = false;
 378        err = brcmf_usb_recv_ctl(devinfo, buf, len);
 379        if (err) {
 380                brcmf_err("fail %d bytes: %d\n", err, len);
 381                clear_bit(0, &devinfo->ctl_op);
 382                goto fail;
 383        }
 384        timeout = brcmf_usb_ioctl_resp_wait(devinfo);
 385        err = devinfo->ctl_urb_status;
 386        if (!timeout) {
 387                brcmf_err("rxctl wait timed out\n");
 388                usb_kill_urb(devinfo->ctl_urb);
 389                err = -EIO;
 390                goto fail;
 391        }
 392        clear_bit(0, &devinfo->ctl_op);
 393fail:
 394        usb_autopm_put_interface(intf);
 395        if (!err)
 396                return devinfo->ctl_urb_actual_length;
 397out:
 398        return err;
 399}
 400
 401static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
 402                                          struct list_head *q, int *counter)
 403{
 404        unsigned long flags;
 405        struct brcmf_usbreq  *req;
 406        spin_lock_irqsave(&devinfo->qlock, flags);
 407        if (list_empty(q)) {
 408                spin_unlock_irqrestore(&devinfo->qlock, flags);
 409                return NULL;
 410        }
 411        req = list_entry(q->next, struct brcmf_usbreq, list);
 412        list_del_init(q->next);
 413        if (counter)
 414                (*counter)--;
 415        spin_unlock_irqrestore(&devinfo->qlock, flags);
 416        return req;
 417
 418}
 419
 420static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
 421                          struct list_head *q, struct brcmf_usbreq *req,
 422                          int *counter)
 423{
 424        unsigned long flags;
 425        spin_lock_irqsave(&devinfo->qlock, flags);
 426        list_add_tail(&req->list, q);
 427        if (counter)
 428                (*counter)++;
 429        spin_unlock_irqrestore(&devinfo->qlock, flags);
 430}
 431
 432static struct brcmf_usbreq *
 433brcmf_usbdev_qinit(struct list_head *q, int qsize)
 434{
 435        int i;
 436        struct brcmf_usbreq *req, *reqs;
 437
 438        reqs = kcalloc(qsize, sizeof(struct brcmf_usbreq), GFP_ATOMIC);
 439        if (reqs == NULL)
 440                return NULL;
 441
 442        req = reqs;
 443
 444        for (i = 0; i < qsize; i++) {
 445                req->urb = usb_alloc_urb(0, GFP_ATOMIC);
 446                if (!req->urb)
 447                        goto fail;
 448
 449                INIT_LIST_HEAD(&req->list);
 450                list_add_tail(&req->list, q);
 451                req++;
 452        }
 453        return reqs;
 454fail:
 455        brcmf_err("fail!\n");
 456        while (!list_empty(q)) {
 457                req = list_entry(q->next, struct brcmf_usbreq, list);
 458                if (req)
 459                        usb_free_urb(req->urb);
 460                list_del(q->next);
 461        }
 462        kfree(reqs);
 463        return NULL;
 464
 465}
 466
 467static void brcmf_usb_free_q(struct list_head *q)
 468{
 469        struct brcmf_usbreq *req, *next;
 470
 471        list_for_each_entry_safe(req, next, q, list) {
 472                if (!req->urb) {
 473                        brcmf_err("bad req\n");
 474                        break;
 475                }
 476                usb_free_urb(req->urb);
 477                list_del_init(&req->list);
 478        }
 479}
 480
 481static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
 482                                struct brcmf_usbreq *req)
 483{
 484        unsigned long flags;
 485
 486        spin_lock_irqsave(&devinfo->qlock, flags);
 487        list_del_init(&req->list);
 488        spin_unlock_irqrestore(&devinfo->qlock, flags);
 489}
 490
 491
 492static void brcmf_usb_tx_complete(struct urb *urb)
 493{
 494        struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
 495        struct brcmf_usbdev_info *devinfo = req->devinfo;
 496        unsigned long flags;
 497
 498        brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
 499                  req->skb);
 500        brcmf_usb_del_fromq(devinfo, req);
 501
 502        brcmf_proto_bcdc_txcomplete(devinfo->dev, req->skb, urb->status == 0);
 503        req->skb = NULL;
 504        brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
 505        spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
 506        if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
 507                devinfo->tx_flowblock) {
 508                brcmf_proto_bcdc_txflowblock(devinfo->dev, false);
 509                devinfo->tx_flowblock = false;
 510        }
 511        spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
 512}
 513
 514static void brcmf_usb_rx_complete(struct urb *urb)
 515{
 516        struct brcmf_usbreq  *req = (struct brcmf_usbreq *)urb->context;
 517        struct brcmf_usbdev_info *devinfo = req->devinfo;
 518        struct sk_buff *skb;
 519
 520        brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
 521        brcmf_usb_del_fromq(devinfo, req);
 522        skb = req->skb;
 523        req->skb = NULL;
 524
 525        /* zero length packets indicate usb "failure". Do not refill */
 526        if (urb->status != 0 || !urb->actual_length) {
 527                brcmu_pkt_buf_free_skb(skb);
 528                brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
 529                return;
 530        }
 531
 532        if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP ||
 533            devinfo->bus_pub.state == BRCMFMAC_USB_STATE_SLEEP) {
 534                skb_put(skb, urb->actual_length);
 535                brcmf_rx_frame(devinfo->dev, skb, true, true);
 536                brcmf_usb_rx_refill(devinfo, req);
 537                usb_mark_last_busy(urb->dev);
 538        } else {
 539                brcmu_pkt_buf_free_skb(skb);
 540                brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
 541        }
 542        return;
 543
 544}
 545
 546static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
 547                                struct brcmf_usbreq  *req)
 548{
 549        struct sk_buff *skb;
 550        int ret;
 551
 552        if (!req || !devinfo)
 553                return;
 554
 555        skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
 556        if (!skb) {
 557                brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
 558                return;
 559        }
 560        req->skb = skb;
 561
 562        usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
 563                          skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
 564                          req);
 565        req->devinfo = devinfo;
 566        brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
 567
 568        ret = usb_submit_urb(req->urb, GFP_ATOMIC);
 569        if (ret) {
 570                brcmf_usb_del_fromq(devinfo, req);
 571                brcmu_pkt_buf_free_skb(req->skb);
 572                req->skb = NULL;
 573                brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
 574        }
 575        return;
 576}
 577
 578static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
 579{
 580        struct brcmf_usbreq *req;
 581
 582        if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
 583                brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
 584                return;
 585        }
 586        while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
 587                brcmf_usb_rx_refill(devinfo, req);
 588}
 589
 590static void
 591brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
 592{
 593        struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
 594
 595        brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
 596                  devinfo->bus_pub.state, state);
 597
 598        if (devinfo->bus_pub.state == state)
 599                return;
 600
 601        devinfo->bus_pub.state = state;
 602
 603        /* update state of upper layer */
 604        if (state == BRCMFMAC_USB_STATE_DOWN) {
 605                brcmf_dbg(USB, "DBUS is down\n");
 606                brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_DOWN);
 607        } else if (state == BRCMFMAC_USB_STATE_UP) {
 608                brcmf_dbg(USB, "DBUS is up\n");
 609                brcmf_bus_change_state(bcmf_bus, BRCMF_BUS_UP);
 610        } else {
 611                brcmf_dbg(USB, "DBUS current state=%d\n", state);
 612        }
 613}
 614
 615static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
 616{
 617        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
 618        struct brcmf_usbreq  *req;
 619        int ret;
 620        unsigned long flags;
 621        struct usb_interface *intf = to_usb_interface(dev);
 622
 623        ret = usb_autopm_get_interface(intf);
 624        if (ret)
 625                goto out;
 626
 627        brcmf_dbg(USB, "Enter, skb=%p\n", skb);
 628        if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
 629                ret = -EIO;
 630                goto fail;
 631        }
 632
 633        req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
 634                                        &devinfo->tx_freecount);
 635        if (!req) {
 636                brcmf_err("no req to send\n");
 637                ret = -ENOMEM;
 638                goto fail;
 639        }
 640
 641        req->skb = skb;
 642        req->devinfo = devinfo;
 643        usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
 644                          skb->data, skb->len, brcmf_usb_tx_complete, req);
 645        req->urb->transfer_flags |= URB_ZERO_PACKET;
 646        brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
 647        ret = usb_submit_urb(req->urb, GFP_ATOMIC);
 648        if (ret) {
 649                brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
 650                brcmf_usb_del_fromq(devinfo, req);
 651                req->skb = NULL;
 652                brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
 653                              &devinfo->tx_freecount);
 654                goto fail;
 655        }
 656
 657        spin_lock_irqsave(&devinfo->tx_flowblock_lock, flags);
 658        if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
 659            !devinfo->tx_flowblock) {
 660                brcmf_proto_bcdc_txflowblock(dev, true);
 661                devinfo->tx_flowblock = true;
 662        }
 663        spin_unlock_irqrestore(&devinfo->tx_flowblock_lock, flags);
 664
 665fail:
 666        usb_autopm_put_interface(intf);
 667out:
 668        return ret;
 669}
 670
 671
 672static int brcmf_usb_up(struct device *dev)
 673{
 674        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
 675
 676        brcmf_dbg(USB, "Enter\n");
 677        if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
 678                return 0;
 679
 680        /* Success, indicate devinfo is fully up */
 681        brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
 682
 683        if (devinfo->ctl_urb) {
 684                devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
 685                devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
 686
 687                /* CTL Write */
 688                devinfo->ctl_write.bRequestType =
 689                        USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
 690                devinfo->ctl_write.bRequest = 0;
 691                devinfo->ctl_write.wValue = cpu_to_le16(0);
 692                devinfo->ctl_write.wIndex = cpu_to_le16(devinfo->ifnum);
 693
 694                /* CTL Read */
 695                devinfo->ctl_read.bRequestType =
 696                        USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
 697                devinfo->ctl_read.bRequest = 1;
 698                devinfo->ctl_read.wValue = cpu_to_le16(0);
 699                devinfo->ctl_read.wIndex = cpu_to_le16(devinfo->ifnum);
 700        }
 701        brcmf_usb_rx_fill_all(devinfo);
 702        return 0;
 703}
 704
 705static void brcmf_cancel_all_urbs(struct brcmf_usbdev_info *devinfo)
 706{
 707        int i;
 708
 709        if (devinfo->ctl_urb)
 710                usb_kill_urb(devinfo->ctl_urb);
 711        if (devinfo->bulk_urb)
 712                usb_kill_urb(devinfo->bulk_urb);
 713        if (devinfo->tx_reqs)
 714                for (i = 0; i < devinfo->bus_pub.ntxq; i++)
 715                        usb_kill_urb(devinfo->tx_reqs[i].urb);
 716        if (devinfo->rx_reqs)
 717                for (i = 0; i < devinfo->bus_pub.nrxq; i++)
 718                        usb_kill_urb(devinfo->rx_reqs[i].urb);
 719}
 720
 721static void brcmf_usb_down(struct device *dev)
 722{
 723        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
 724
 725        brcmf_dbg(USB, "Enter\n");
 726        if (devinfo == NULL)
 727                return;
 728
 729        if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
 730                return;
 731
 732        brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
 733
 734        brcmf_cancel_all_urbs(devinfo);
 735}
 736
 737static void
 738brcmf_usb_sync_complete(struct urb *urb)
 739{
 740        struct brcmf_usbdev_info *devinfo =
 741                        (struct brcmf_usbdev_info *)urb->context;
 742
 743        devinfo->ctl_completed = true;
 744        brcmf_usb_ioctl_resp_wake(devinfo);
 745}
 746
 747static int brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
 748                            void *buffer, int buflen)
 749{
 750        int ret;
 751        char *tmpbuf;
 752        u16 size;
 753
 754        if ((!devinfo) || (devinfo->ctl_urb == NULL))
 755                return -EINVAL;
 756
 757        tmpbuf = kmalloc(buflen, GFP_ATOMIC);
 758        if (!tmpbuf)
 759                return -ENOMEM;
 760
 761        size = buflen;
 762        devinfo->ctl_urb->transfer_buffer_length = size;
 763
 764        devinfo->ctl_read.wLength = cpu_to_le16p(&size);
 765        devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
 766                USB_RECIP_INTERFACE;
 767        devinfo->ctl_read.bRequest = cmd;
 768
 769        usb_fill_control_urb(devinfo->ctl_urb,
 770                devinfo->usbdev,
 771                usb_rcvctrlpipe(devinfo->usbdev, 0),
 772                (unsigned char *) &devinfo->ctl_read,
 773                (void *) tmpbuf, size,
 774                (usb_complete_t)brcmf_usb_sync_complete, devinfo);
 775
 776        devinfo->ctl_completed = false;
 777        ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
 778        if (ret < 0) {
 779                brcmf_err("usb_submit_urb failed %d\n", ret);
 780                goto finalize;
 781        }
 782
 783        if (!brcmf_usb_ioctl_resp_wait(devinfo)) {
 784                usb_kill_urb(devinfo->ctl_urb);
 785                ret = -ETIMEDOUT;
 786        } else {
 787                memcpy(buffer, tmpbuf, buflen);
 788        }
 789
 790finalize:
 791        kfree(tmpbuf);
 792        return ret;
 793}
 794
 795static bool
 796brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
 797{
 798        struct bootrom_id_le id;
 799        u32 chipid, chiprev;
 800
 801        brcmf_dbg(USB, "Enter\n");
 802
 803        if (devinfo == NULL)
 804                return false;
 805
 806        /* Check if firmware downloaded already by querying runtime ID */
 807        id.chip = cpu_to_le32(0xDEAD);
 808        brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
 809
 810        chipid = le32_to_cpu(id.chip);
 811        chiprev = le32_to_cpu(id.chiprev);
 812
 813        if ((chipid & 0x4300) == 0x4300)
 814                brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
 815        else
 816                brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
 817        if (chipid == BRCMF_POSTBOOT_ID) {
 818                brcmf_dbg(USB, "firmware already downloaded\n");
 819                brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
 820                return false;
 821        } else {
 822                devinfo->bus_pub.devid = chipid;
 823                devinfo->bus_pub.chiprev = chiprev;
 824        }
 825        return true;
 826}
 827
 828static int
 829brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
 830{
 831        struct bootrom_id_le id;
 832        u32 loop_cnt;
 833        int err;
 834
 835        brcmf_dbg(USB, "Enter\n");
 836
 837        loop_cnt = 0;
 838        do {
 839                mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
 840                loop_cnt++;
 841                id.chip = cpu_to_le32(0xDEAD);       /* Get the ID */
 842                err = brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
 843                if ((err) && (err != -ETIMEDOUT))
 844                        return err;
 845                if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
 846                        break;
 847        } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
 848
 849        if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
 850                brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
 851                          le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
 852
 853                brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
 854                return 0;
 855        } else {
 856                brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
 857                          BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
 858                return -EINVAL;
 859        }
 860}
 861
 862
 863static int
 864brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
 865{
 866        int ret;
 867
 868        if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
 869                return -EINVAL;
 870
 871        /* Prepare the URB */
 872        usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
 873                          devinfo->tx_pipe, buffer, len,
 874                          (usb_complete_t)brcmf_usb_sync_complete, devinfo);
 875
 876        devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
 877
 878        devinfo->ctl_completed = false;
 879        ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
 880        if (ret) {
 881                brcmf_err("usb_submit_urb failed %d\n", ret);
 882                return ret;
 883        }
 884        ret = brcmf_usb_ioctl_resp_wait(devinfo);
 885        return (ret == 0);
 886}
 887
 888static int
 889brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
 890{
 891        unsigned int sendlen, sent, dllen;
 892        char *bulkchunk = NULL, *dlpos;
 893        struct rdl_state_le state;
 894        u32 rdlstate, rdlbytes;
 895        int err = 0;
 896
 897        brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
 898
 899        bulkchunk = kmalloc(TRX_RDL_CHUNK, GFP_ATOMIC);
 900        if (bulkchunk == NULL) {
 901                err = -ENOMEM;
 902                goto fail;
 903        }
 904
 905        /* 1) Prepare USB boot loader for runtime image */
 906        brcmf_usb_dl_cmd(devinfo, DL_START, &state, sizeof(state));
 907
 908        rdlstate = le32_to_cpu(state.state);
 909        rdlbytes = le32_to_cpu(state.bytes);
 910
 911        /* 2) Check we are in the Waiting state */
 912        if (rdlstate != DL_WAITING) {
 913                brcmf_err("Failed to DL_START\n");
 914                err = -EINVAL;
 915                goto fail;
 916        }
 917        sent = 0;
 918        dlpos = fw;
 919        dllen = fwlen;
 920
 921        /* Get chip id and rev */
 922        while (rdlbytes != dllen) {
 923                /* Wait until the usb device reports it received all
 924                 * the bytes we sent */
 925                if ((rdlbytes == sent) && (rdlbytes != dllen)) {
 926                        if ((dllen-sent) < TRX_RDL_CHUNK)
 927                                sendlen = dllen-sent;
 928                        else
 929                                sendlen = TRX_RDL_CHUNK;
 930
 931                        /* simply avoid having to send a ZLP by ensuring we
 932                         * never have an even
 933                         * multiple of 64
 934                         */
 935                        if (!(sendlen % 64))
 936                                sendlen -= 4;
 937
 938                        /* send data */
 939                        memcpy(bulkchunk, dlpos, sendlen);
 940                        if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
 941                                                   sendlen)) {
 942                                brcmf_err("send_bulk failed\n");
 943                                err = -EINVAL;
 944                                goto fail;
 945                        }
 946
 947                        dlpos += sendlen;
 948                        sent += sendlen;
 949                }
 950                err = brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
 951                                       sizeof(state));
 952                if (err) {
 953                        brcmf_err("DL_GETSTATE Failed\n");
 954                        goto fail;
 955                }
 956
 957                rdlstate = le32_to_cpu(state.state);
 958                rdlbytes = le32_to_cpu(state.bytes);
 959
 960                /* restart if an error is reported */
 961                if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
 962                        brcmf_err("Bad Hdr or Bad CRC state %d\n",
 963                                  rdlstate);
 964                        err = -EINVAL;
 965                        goto fail;
 966                }
 967        }
 968
 969fail:
 970        kfree(bulkchunk);
 971        brcmf_dbg(USB, "Exit, err=%d\n", err);
 972        return err;
 973}
 974
 975static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
 976{
 977        int err;
 978
 979        brcmf_dbg(USB, "Enter\n");
 980
 981        if (devinfo == NULL)
 982                return -EINVAL;
 983
 984        if (devinfo->bus_pub.devid == 0xDEAD)
 985                return -EINVAL;
 986
 987        err = brcmf_usb_dl_writeimage(devinfo, fw, len);
 988        if (err == 0)
 989                devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
 990        else
 991                devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
 992        brcmf_dbg(USB, "Exit, err=%d\n", err);
 993
 994        return err;
 995}
 996
 997static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
 998{
 999        struct rdl_state_le state;
1000
1001        brcmf_dbg(USB, "Enter\n");
1002        if (!devinfo)
1003                return -EINVAL;
1004
1005        if (devinfo->bus_pub.devid == 0xDEAD)
1006                return -EINVAL;
1007
1008        /* Check we are runnable */
1009        state.state = 0;
1010        brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state, sizeof(state));
1011
1012        /* Start the image */
1013        if (state.state == cpu_to_le32(DL_RUNNABLE)) {
1014                if (brcmf_usb_dl_cmd(devinfo, DL_GO, &state, sizeof(state)))
1015                        return -ENODEV;
1016                if (brcmf_usb_resetcfg(devinfo))
1017                        return -ENODEV;
1018                /* The Dongle may go for re-enumeration. */
1019        } else {
1020                brcmf_err("Dongle not runnable\n");
1021                return -EINVAL;
1022        }
1023        brcmf_dbg(USB, "Exit\n");
1024        return 0;
1025}
1026
1027static int
1028brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
1029{
1030        int err;
1031        struct usb_interface *intf;
1032
1033        brcmf_dbg(USB, "Enter\n");
1034        if (!devinfo) {
1035                err = -ENODEV;
1036                goto out;
1037        }
1038
1039        if (!devinfo->image) {
1040                brcmf_err("No firmware!\n");
1041                err = -ENOENT;
1042                goto out;
1043        }
1044
1045        intf = to_usb_interface(devinfo->dev);
1046        err = usb_autopm_get_interface(intf);
1047        if (err)
1048                goto out;
1049
1050        err = brcmf_usb_dlstart(devinfo,
1051                (u8 *)devinfo->image, devinfo->image_len);
1052        if (err == 0)
1053                err = brcmf_usb_dlrun(devinfo);
1054
1055        usb_autopm_put_interface(intf);
1056out:
1057        return err;
1058}
1059
1060
1061static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
1062{
1063        brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
1064
1065        /* free the URBS */
1066        brcmf_usb_free_q(&devinfo->rx_freeq);
1067        brcmf_usb_free_q(&devinfo->tx_freeq);
1068
1069        usb_free_urb(devinfo->ctl_urb);
1070        usb_free_urb(devinfo->bulk_urb);
1071
1072        kfree(devinfo->tx_reqs);
1073        kfree(devinfo->rx_reqs);
1074
1075        if (devinfo->settings)
1076                brcmf_release_module_param(devinfo->settings);
1077}
1078
1079
1080static int check_file(const u8 *headers)
1081{
1082        struct trx_header_le *trx;
1083        int actual_len = -1;
1084
1085        brcmf_dbg(USB, "Enter\n");
1086        /* Extract trx header */
1087        trx = (struct trx_header_le *) headers;
1088        if (trx->magic != cpu_to_le32(TRX_MAGIC))
1089                return -1;
1090
1091        headers += sizeof(struct trx_header_le);
1092
1093        if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
1094                actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
1095                return actual_len + sizeof(struct trx_header_le);
1096        }
1097        return -1;
1098}
1099
1100
1101static
1102struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
1103                                      int nrxq, int ntxq)
1104{
1105        brcmf_dbg(USB, "Enter\n");
1106
1107        devinfo->bus_pub.nrxq = nrxq;
1108        devinfo->rx_low_watermark = nrxq / 2;
1109        devinfo->bus_pub.devinfo = devinfo;
1110        devinfo->bus_pub.ntxq = ntxq;
1111        devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
1112
1113        /* flow control when too many tx urbs posted */
1114        devinfo->tx_low_watermark = ntxq / 4;
1115        devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
1116        devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
1117
1118        /* Initialize other structure content */
1119        init_waitqueue_head(&devinfo->ioctl_resp_wait);
1120
1121        /* Initialize the spinlocks */
1122        spin_lock_init(&devinfo->qlock);
1123        spin_lock_init(&devinfo->tx_flowblock_lock);
1124
1125        INIT_LIST_HEAD(&devinfo->rx_freeq);
1126        INIT_LIST_HEAD(&devinfo->rx_postq);
1127
1128        INIT_LIST_HEAD(&devinfo->tx_freeq);
1129        INIT_LIST_HEAD(&devinfo->tx_postq);
1130
1131        devinfo->tx_flowblock = false;
1132
1133        devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
1134        if (!devinfo->rx_reqs)
1135                goto error;
1136
1137        devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
1138        if (!devinfo->tx_reqs)
1139                goto error;
1140        devinfo->tx_freecount = ntxq;
1141
1142        devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
1143        if (!devinfo->ctl_urb)
1144                goto error;
1145        devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
1146        if (!devinfo->bulk_urb)
1147                goto error;
1148
1149        return &devinfo->bus_pub;
1150
1151error:
1152        brcmf_err("failed!\n");
1153        brcmf_usb_detach(devinfo);
1154        return NULL;
1155}
1156
1157static
1158int brcmf_usb_get_fwname(struct device *dev, const char *ext, u8 *fw_name)
1159{
1160        struct brcmf_bus *bus = dev_get_drvdata(dev);
1161        struct brcmf_fw_request *fwreq;
1162        struct brcmf_fw_name fwnames[] = {
1163                { ext, fw_name },
1164        };
1165
1166        fwreq = brcmf_fw_alloc_request(bus->chip, bus->chiprev,
1167                                       brcmf_usb_fwnames,
1168                                       ARRAY_SIZE(brcmf_usb_fwnames),
1169                                       fwnames, ARRAY_SIZE(fwnames));
1170        if (!fwreq)
1171                return -ENOMEM;
1172
1173        kfree(fwreq);
1174        return 0;
1175}
1176
1177static const struct brcmf_bus_ops brcmf_usb_bus_ops = {
1178        .preinit = brcmf_usb_up,
1179        .stop = brcmf_usb_down,
1180        .txdata = brcmf_usb_tx,
1181        .txctl = brcmf_usb_tx_ctlpkt,
1182        .rxctl = brcmf_usb_rx_ctlpkt,
1183        .get_fwname = brcmf_usb_get_fwname,
1184};
1185
1186#define BRCMF_USB_FW_CODE       0
1187
1188static void brcmf_usb_probe_phase2(struct device *dev, int ret,
1189                                   struct brcmf_fw_request *fwreq)
1190{
1191        struct brcmf_bus *bus = dev_get_drvdata(dev);
1192        struct brcmf_usbdev_info *devinfo = bus->bus_priv.usb->devinfo;
1193        const struct firmware *fw;
1194
1195        if (ret)
1196                goto error;
1197
1198        brcmf_dbg(USB, "Start fw downloading\n");
1199
1200        fw = fwreq->items[BRCMF_USB_FW_CODE].binary;
1201        kfree(fwreq);
1202
1203        ret = check_file(fw->data);
1204        if (ret < 0) {
1205                brcmf_err("invalid firmware\n");
1206                release_firmware(fw);
1207                goto error;
1208        }
1209
1210        devinfo->image = fw->data;
1211        devinfo->image_len = fw->size;
1212
1213        ret = brcmf_usb_fw_download(devinfo);
1214        release_firmware(fw);
1215        if (ret)
1216                goto error;
1217
1218        ret = brcmf_alloc(devinfo->dev, devinfo->settings);
1219        if (ret)
1220                goto error;
1221
1222        /* Attach to the common driver interface */
1223        ret = brcmf_attach(devinfo->dev);
1224        if (ret)
1225                goto error;
1226
1227        complete(&devinfo->dev_init_done);
1228        return;
1229error:
1230        brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), ret);
1231        complete(&devinfo->dev_init_done);
1232        device_release_driver(dev);
1233}
1234
1235static struct brcmf_fw_request *
1236brcmf_usb_prepare_fw_request(struct brcmf_usbdev_info *devinfo)
1237{
1238        struct brcmf_fw_request *fwreq;
1239        struct brcmf_fw_name fwnames[] = {
1240                { ".bin", devinfo->fw_name },
1241        };
1242
1243        fwreq = brcmf_fw_alloc_request(devinfo->bus_pub.devid,
1244                                       devinfo->bus_pub.chiprev,
1245                                       brcmf_usb_fwnames,
1246                                       ARRAY_SIZE(brcmf_usb_fwnames),
1247                                       fwnames, ARRAY_SIZE(fwnames));
1248        if (!fwreq)
1249                return NULL;
1250
1251        fwreq->items[BRCMF_USB_FW_CODE].type = BRCMF_FW_TYPE_BINARY;
1252
1253        return fwreq;
1254}
1255
1256static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
1257{
1258        struct brcmf_bus *bus = NULL;
1259        struct brcmf_usbdev *bus_pub = NULL;
1260        struct device *dev = devinfo->dev;
1261        struct brcmf_fw_request *fwreq;
1262        int ret;
1263
1264        brcmf_dbg(USB, "Enter\n");
1265        bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
1266        if (!bus_pub)
1267                return -ENODEV;
1268
1269        bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
1270        if (!bus) {
1271                ret = -ENOMEM;
1272                goto fail;
1273        }
1274
1275        bus->dev = dev;
1276        bus_pub->bus = bus;
1277        bus->bus_priv.usb = bus_pub;
1278        dev_set_drvdata(dev, bus);
1279        bus->ops = &brcmf_usb_bus_ops;
1280        bus->proto_type = BRCMF_PROTO_BCDC;
1281        bus->always_use_fws_queue = true;
1282#ifdef CONFIG_PM
1283        bus->wowl_supported = true;
1284#endif
1285
1286        devinfo->settings = brcmf_get_module_param(bus->dev, BRCMF_BUSTYPE_USB,
1287                                                   bus_pub->devid,
1288                                                   bus_pub->chiprev);
1289        if (!devinfo->settings) {
1290                ret = -ENOMEM;
1291                goto fail;
1292        }
1293
1294        if (!brcmf_usb_dlneeded(devinfo)) {
1295                ret = brcmf_alloc(devinfo->dev, devinfo->settings);
1296                if (ret)
1297                        goto fail;
1298                ret = brcmf_attach(devinfo->dev);
1299                if (ret)
1300                        goto fail;
1301                /* we are done */
1302                complete(&devinfo->dev_init_done);
1303                return 0;
1304        }
1305        bus->chip = bus_pub->devid;
1306        bus->chiprev = bus_pub->chiprev;
1307
1308        fwreq = brcmf_usb_prepare_fw_request(devinfo);
1309        if (!fwreq) {
1310                ret = -ENOMEM;
1311                goto fail;
1312        }
1313
1314        /* request firmware here */
1315        ret = brcmf_fw_get_firmwares(dev, fwreq, brcmf_usb_probe_phase2);
1316        if (ret) {
1317                brcmf_err("firmware request failed: %d\n", ret);
1318                kfree(fwreq);
1319                goto fail;
1320        }
1321
1322        return 0;
1323
1324fail:
1325        /* Release resources in reverse order */
1326        brcmf_free(devinfo->dev);
1327        kfree(bus);
1328        brcmf_usb_detach(devinfo);
1329        return ret;
1330}
1331
1332static void
1333brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
1334{
1335        if (!devinfo)
1336                return;
1337        brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
1338
1339        brcmf_detach(devinfo->dev);
1340        brcmf_free(devinfo->dev);
1341        kfree(devinfo->bus_pub.bus);
1342        brcmf_usb_detach(devinfo);
1343}
1344
1345static int
1346brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
1347{
1348        struct usb_device *usb = interface_to_usbdev(intf);
1349        struct brcmf_usbdev_info *devinfo;
1350        struct usb_interface_descriptor *desc;
1351        struct usb_endpoint_descriptor *endpoint;
1352        int ret = 0;
1353        u32 num_of_eps;
1354        u8 endpoint_num, ep;
1355
1356        brcmf_dbg(USB, "Enter 0x%04x:0x%04x\n", id->idVendor, id->idProduct);
1357
1358        devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
1359        if (devinfo == NULL)
1360                return -ENOMEM;
1361
1362        devinfo->usbdev = usb;
1363        devinfo->dev = &usb->dev;
1364        /* Init completion, to protect for disconnect while still loading.
1365         * Necessary because of the asynchronous firmware load construction
1366         */
1367        init_completion(&devinfo->dev_init_done);
1368
1369        usb_set_intfdata(intf, devinfo);
1370
1371        intf->needs_remote_wakeup = 1;
1372
1373        /* Check that the device supports only one configuration */
1374        if (usb->descriptor.bNumConfigurations != 1) {
1375                brcmf_err("Number of configurations: %d not supported\n",
1376                          usb->descriptor.bNumConfigurations);
1377                ret = -ENODEV;
1378                goto fail;
1379        }
1380
1381        if ((usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) &&
1382            (usb->descriptor.bDeviceClass != USB_CLASS_MISC) &&
1383            (usb->descriptor.bDeviceClass != USB_CLASS_WIRELESS_CONTROLLER)) {
1384                brcmf_err("Device class: 0x%x not supported\n",
1385                          usb->descriptor.bDeviceClass);
1386                ret = -ENODEV;
1387                goto fail;
1388        }
1389
1390        desc = &intf->cur_altsetting->desc;
1391        if ((desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1392            (desc->bInterfaceSubClass != 2) ||
1393            (desc->bInterfaceProtocol != 0xff)) {
1394                brcmf_err("non WLAN interface %d: 0x%x:0x%x:0x%x\n",
1395                          desc->bInterfaceNumber, desc->bInterfaceClass,
1396                          desc->bInterfaceSubClass, desc->bInterfaceProtocol);
1397                ret = -ENODEV;
1398                goto fail;
1399        }
1400
1401        num_of_eps = desc->bNumEndpoints;
1402        for (ep = 0; ep < num_of_eps; ep++) {
1403                endpoint = &intf->cur_altsetting->endpoint[ep].desc;
1404                endpoint_num = usb_endpoint_num(endpoint);
1405                if (!usb_endpoint_xfer_bulk(endpoint))
1406                        continue;
1407                if (usb_endpoint_dir_in(endpoint)) {
1408                        if (!devinfo->rx_pipe)
1409                                devinfo->rx_pipe =
1410                                        usb_rcvbulkpipe(usb, endpoint_num);
1411                } else {
1412                        if (!devinfo->tx_pipe)
1413                                devinfo->tx_pipe =
1414                                        usb_sndbulkpipe(usb, endpoint_num);
1415                }
1416        }
1417        if (devinfo->rx_pipe == 0) {
1418                brcmf_err("No RX (in) Bulk EP found\n");
1419                ret = -ENODEV;
1420                goto fail;
1421        }
1422        if (devinfo->tx_pipe == 0) {
1423                brcmf_err("No TX (out) Bulk EP found\n");
1424                ret = -ENODEV;
1425                goto fail;
1426        }
1427
1428        devinfo->ifnum = desc->bInterfaceNumber;
1429
1430        if (usb->speed == USB_SPEED_SUPER_PLUS)
1431                brcmf_dbg(USB, "Broadcom super speed plus USB WLAN interface detected\n");
1432        else if (usb->speed == USB_SPEED_SUPER)
1433                brcmf_dbg(USB, "Broadcom super speed USB WLAN interface detected\n");
1434        else if (usb->speed == USB_SPEED_HIGH)
1435                brcmf_dbg(USB, "Broadcom high speed USB WLAN interface detected\n");
1436        else
1437                brcmf_dbg(USB, "Broadcom full speed USB WLAN interface detected\n");
1438
1439        ret = brcmf_usb_probe_cb(devinfo);
1440        if (ret)
1441                goto fail;
1442
1443        /* Success */
1444        return 0;
1445
1446fail:
1447        complete(&devinfo->dev_init_done);
1448        kfree(devinfo);
1449        usb_set_intfdata(intf, NULL);
1450        return ret;
1451}
1452
1453static void
1454brcmf_usb_disconnect(struct usb_interface *intf)
1455{
1456        struct brcmf_usbdev_info *devinfo;
1457
1458        brcmf_dbg(USB, "Enter\n");
1459        devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
1460
1461        if (devinfo) {
1462                wait_for_completion(&devinfo->dev_init_done);
1463                /* Make sure that devinfo still exists. Firmware probe routines
1464                 * may have released the device and cleared the intfdata.
1465                 */
1466                if (!usb_get_intfdata(intf))
1467                        goto done;
1468
1469                brcmf_usb_disconnect_cb(devinfo);
1470                kfree(devinfo);
1471        }
1472done:
1473        brcmf_dbg(USB, "Exit\n");
1474}
1475
1476/*
1477 * only need to signal the bus being down and update the state.
1478 */
1479static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
1480{
1481        struct usb_device *usb = interface_to_usbdev(intf);
1482        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1483
1484        brcmf_dbg(USB, "Enter\n");
1485        devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
1486        brcmf_cancel_all_urbs(devinfo);
1487        device_set_wakeup_enable(devinfo->dev, true);
1488        return 0;
1489}
1490
1491/*
1492 * (re-) start the bus.
1493 */
1494static int brcmf_usb_resume(struct usb_interface *intf)
1495{
1496        struct usb_device *usb = interface_to_usbdev(intf);
1497        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1498
1499        brcmf_dbg(USB, "Enter\n");
1500
1501        devinfo->bus_pub.state = BRCMFMAC_USB_STATE_UP;
1502        brcmf_usb_rx_fill_all(devinfo);
1503        device_set_wakeup_enable(devinfo->dev, false);
1504        return 0;
1505}
1506
1507static int brcmf_usb_reset_resume(struct usb_interface *intf)
1508{
1509        struct usb_device *usb = interface_to_usbdev(intf);
1510        struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
1511        struct brcmf_fw_request *fwreq;
1512        int ret;
1513
1514        brcmf_dbg(USB, "Enter\n");
1515
1516        fwreq = brcmf_usb_prepare_fw_request(devinfo);
1517        if (!fwreq)
1518                return -ENOMEM;
1519
1520        ret = brcmf_fw_get_firmwares(&usb->dev, fwreq, brcmf_usb_probe_phase2);
1521        if (ret < 0)
1522                kfree(fwreq);
1523
1524        return ret;
1525}
1526
1527#define BRCMF_USB_DEVICE(dev_id)        \
1528        { USB_DEVICE(BRCM_USB_VENDOR_ID_BROADCOM, dev_id) }
1529
1530#define LINKSYS_USB_DEVICE(dev_id)      \
1531        { USB_DEVICE(BRCM_USB_VENDOR_ID_LINKSYS, dev_id) }
1532
1533#define CYPRESS_USB_DEVICE(dev_id)      \
1534        { USB_DEVICE(CY_USB_VENDOR_ID_CYPRESS, dev_id) }
1535
1536static const struct usb_device_id brcmf_usb_devid_table[] = {
1537        BRCMF_USB_DEVICE(BRCM_USB_43143_DEVICE_ID),
1538        BRCMF_USB_DEVICE(BRCM_USB_43236_DEVICE_ID),
1539        BRCMF_USB_DEVICE(BRCM_USB_43242_DEVICE_ID),
1540        BRCMF_USB_DEVICE(BRCM_USB_43569_DEVICE_ID),
1541        LINKSYS_USB_DEVICE(BRCM_USB_43235_LINKSYS_DEVICE_ID),
1542        CYPRESS_USB_DEVICE(CY_USB_4373_DEVICE_ID),
1543        { USB_DEVICE(BRCM_USB_VENDOR_ID_LG, BRCM_USB_43242_LG_DEVICE_ID) },
1544        /* special entry for device with firmware loaded and running */
1545        BRCMF_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1546        CYPRESS_USB_DEVICE(BRCM_USB_BCMFW_DEVICE_ID),
1547        { /* end: all zeroes */ }
1548};
1549
1550MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
1551
1552static struct usb_driver brcmf_usbdrvr = {
1553        .name = KBUILD_MODNAME,
1554        .probe = brcmf_usb_probe,
1555        .disconnect = brcmf_usb_disconnect,
1556        .id_table = brcmf_usb_devid_table,
1557        .suspend = brcmf_usb_suspend,
1558        .resume = brcmf_usb_resume,
1559        .reset_resume = brcmf_usb_reset_resume,
1560        .supports_autosuspend = true,
1561        .disable_hub_initiated_lpm = 1,
1562};
1563
1564static int brcmf_usb_reset_device(struct device *dev, void *notused)
1565{
1566        /* device past is the usb interface so we
1567         * need to use parent here.
1568         */
1569        brcmf_dev_reset(dev->parent);
1570        return 0;
1571}
1572
1573void brcmf_usb_exit(void)
1574{
1575        struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
1576        int ret;
1577
1578        brcmf_dbg(USB, "Enter\n");
1579        ret = driver_for_each_device(drv, NULL, NULL,
1580                                     brcmf_usb_reset_device);
1581        if (ret)
1582                brcmf_err("failed to reset all usb devices %d\n", ret);
1583
1584        usb_deregister(&brcmf_usbdrvr);
1585}
1586
1587void brcmf_usb_register(void)
1588{
1589        int ret;
1590
1591        brcmf_dbg(USB, "Enter\n");
1592        ret = usb_register(&brcmf_usbdrvr);
1593        if (ret)
1594                brcmf_err("usb_register failed %d\n", ret);
1595}
1596