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