linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2013 Broadcom Corporation
   4 */
   5
   6#include <linux/efi.h>
   7#include <linux/kernel.h>
   8#include <linux/slab.h>
   9#include <linux/device.h>
  10#include <linux/firmware.h>
  11#include <linux/module.h>
  12#include <linux/bcm47xx_nvram.h>
  13
  14#include "debug.h"
  15#include "firmware.h"
  16#include "core.h"
  17#include "common.h"
  18#include "chip.h"
  19
  20#define BRCMF_FW_MAX_NVRAM_SIZE                 64000
  21#define BRCMF_FW_NVRAM_DEVPATH_LEN              19      /* devpath0=pcie/1/4/ */
  22#define BRCMF_FW_NVRAM_PCIEDEV_LEN              10      /* pcie/1/4/ + \0 */
  23#define BRCMF_FW_DEFAULT_BOARDREV               "boardrev=0xff"
  24
  25enum nvram_parser_state {
  26        IDLE,
  27        KEY,
  28        VALUE,
  29        COMMENT,
  30        END
  31};
  32
  33/**
  34 * struct nvram_parser - internal info for parser.
  35 *
  36 * @state: current parser state.
  37 * @data: input buffer being parsed.
  38 * @nvram: output buffer with parse result.
  39 * @nvram_len: length of parse result.
  40 * @line: current line.
  41 * @column: current column in line.
  42 * @pos: byte offset in input buffer.
  43 * @entry: start position of key,value entry.
  44 * @multi_dev_v1: detect pcie multi device v1 (compressed).
  45 * @multi_dev_v2: detect pcie multi device v2.
  46 * @boardrev_found: nvram contains boardrev information.
  47 */
  48struct nvram_parser {
  49        enum nvram_parser_state state;
  50        const u8 *data;
  51        u8 *nvram;
  52        u32 nvram_len;
  53        u32 line;
  54        u32 column;
  55        u32 pos;
  56        u32 entry;
  57        bool multi_dev_v1;
  58        bool multi_dev_v2;
  59        bool boardrev_found;
  60};
  61
  62/*
  63 * is_nvram_char() - check if char is a valid one for NVRAM entry
  64 *
  65 * It accepts all printable ASCII chars except for '#' which opens a comment.
  66 * Please note that ' ' (space) while accepted is not a valid key name char.
  67 */
  68static bool is_nvram_char(char c)
  69{
  70        /* comment marker excluded */
  71        if (c == '#')
  72                return false;
  73
  74        /* key and value may have any other readable character */
  75        return (c >= 0x20 && c < 0x7f);
  76}
  77
  78static bool is_whitespace(char c)
  79{
  80        return (c == ' ' || c == '\r' || c == '\n' || c == '\t');
  81}
  82
  83static enum nvram_parser_state brcmf_nvram_handle_idle(struct nvram_parser *nvp)
  84{
  85        char c;
  86
  87        c = nvp->data[nvp->pos];
  88        if (c == '\n')
  89                return COMMENT;
  90        if (is_whitespace(c) || c == '\0')
  91                goto proceed;
  92        if (c == '#')
  93                return COMMENT;
  94        if (is_nvram_char(c)) {
  95                nvp->entry = nvp->pos;
  96                return KEY;
  97        }
  98        brcmf_dbg(INFO, "warning: ln=%d:col=%d: ignoring invalid character\n",
  99                  nvp->line, nvp->column);
 100proceed:
 101        nvp->column++;
 102        nvp->pos++;
 103        return IDLE;
 104}
 105
 106static enum nvram_parser_state brcmf_nvram_handle_key(struct nvram_parser *nvp)
 107{
 108        enum nvram_parser_state st = nvp->state;
 109        char c;
 110
 111        c = nvp->data[nvp->pos];
 112        if (c == '=') {
 113                /* ignore RAW1 by treating as comment */
 114                if (strncmp(&nvp->data[nvp->entry], "RAW1", 4) == 0)
 115                        st = COMMENT;
 116                else
 117                        st = VALUE;
 118                if (strncmp(&nvp->data[nvp->entry], "devpath", 7) == 0)
 119                        nvp->multi_dev_v1 = true;
 120                if (strncmp(&nvp->data[nvp->entry], "pcie/", 5) == 0)
 121                        nvp->multi_dev_v2 = true;
 122                if (strncmp(&nvp->data[nvp->entry], "boardrev", 8) == 0)
 123                        nvp->boardrev_found = true;
 124        } else if (!is_nvram_char(c) || c == ' ') {
 125                brcmf_dbg(INFO, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n",
 126                          nvp->line, nvp->column);
 127                return COMMENT;
 128        }
 129
 130        nvp->column++;
 131        nvp->pos++;
 132        return st;
 133}
 134
 135static enum nvram_parser_state
 136brcmf_nvram_handle_value(struct nvram_parser *nvp)
 137{
 138        char c;
 139        char *skv;
 140        char *ekv;
 141        u32 cplen;
 142
 143        c = nvp->data[nvp->pos];
 144        if (!is_nvram_char(c)) {
 145                /* key,value pair complete */
 146                ekv = (u8 *)&nvp->data[nvp->pos];
 147                skv = (u8 *)&nvp->data[nvp->entry];
 148                cplen = ekv - skv;
 149                if (nvp->nvram_len + cplen + 1 >= BRCMF_FW_MAX_NVRAM_SIZE)
 150                        return END;
 151                /* copy to output buffer */
 152                memcpy(&nvp->nvram[nvp->nvram_len], skv, cplen);
 153                nvp->nvram_len += cplen;
 154                nvp->nvram[nvp->nvram_len] = '\0';
 155                nvp->nvram_len++;
 156                return IDLE;
 157        }
 158        nvp->pos++;
 159        nvp->column++;
 160        return VALUE;
 161}
 162
 163static enum nvram_parser_state
 164brcmf_nvram_handle_comment(struct nvram_parser *nvp)
 165{
 166        char *eoc, *sol;
 167
 168        sol = (char *)&nvp->data[nvp->pos];
 169        eoc = strchr(sol, '\n');
 170        if (!eoc) {
 171                eoc = strchr(sol, '\0');
 172                if (!eoc)
 173                        return END;
 174        }
 175
 176        /* eat all moving to next line */
 177        nvp->line++;
 178        nvp->column = 1;
 179        nvp->pos += (eoc - sol) + 1;
 180        return IDLE;
 181}
 182
 183static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp)
 184{
 185        /* final state */
 186        return END;
 187}
 188
 189static enum nvram_parser_state
 190(*nv_parser_states[])(struct nvram_parser *nvp) = {
 191        brcmf_nvram_handle_idle,
 192        brcmf_nvram_handle_key,
 193        brcmf_nvram_handle_value,
 194        brcmf_nvram_handle_comment,
 195        brcmf_nvram_handle_end
 196};
 197
 198static int brcmf_init_nvram_parser(struct nvram_parser *nvp,
 199                                   const u8 *data, size_t data_len)
 200{
 201        size_t size;
 202
 203        memset(nvp, 0, sizeof(*nvp));
 204        nvp->data = data;
 205        /* Limit size to MAX_NVRAM_SIZE, some files contain lot of comment */
 206        if (data_len > BRCMF_FW_MAX_NVRAM_SIZE)
 207                size = BRCMF_FW_MAX_NVRAM_SIZE;
 208        else
 209                size = data_len;
 210        /* Alloc for extra 0 byte + roundup by 4 + length field */
 211        size += 1 + 3 + sizeof(u32);
 212        nvp->nvram = kzalloc(size, GFP_KERNEL);
 213        if (!nvp->nvram)
 214                return -ENOMEM;
 215
 216        nvp->line = 1;
 217        nvp->column = 1;
 218        return 0;
 219}
 220
 221/* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple
 222 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
 223 * which data is to be returned. v1 is the version where nvram is stored
 224 * compressed and "devpath" maps to index for valid entries.
 225 */
 226static void brcmf_fw_strip_multi_v1(struct nvram_parser *nvp, u16 domain_nr,
 227                                    u16 bus_nr)
 228{
 229        /* Device path with a leading '=' key-value separator */
 230        char pci_path[] = "=pci/?/?";
 231        size_t pci_len;
 232        char pcie_path[] = "=pcie/?/?";
 233        size_t pcie_len;
 234
 235        u32 i, j;
 236        bool found;
 237        u8 *nvram;
 238        u8 id;
 239
 240        nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
 241        if (!nvram)
 242                goto fail;
 243
 244        /* min length: devpath0=pcie/1/4/ + 0:x=y */
 245        if (nvp->nvram_len < BRCMF_FW_NVRAM_DEVPATH_LEN + 6)
 246                goto fail;
 247
 248        /* First search for the devpathX and see if it is the configuration
 249         * for domain_nr/bus_nr. Search complete nvp
 250         */
 251        snprintf(pci_path, sizeof(pci_path), "=pci/%d/%d", domain_nr,
 252                 bus_nr);
 253        pci_len = strlen(pci_path);
 254        snprintf(pcie_path, sizeof(pcie_path), "=pcie/%d/%d", domain_nr,
 255                 bus_nr);
 256        pcie_len = strlen(pcie_path);
 257        found = false;
 258        i = 0;
 259        while (i < nvp->nvram_len - BRCMF_FW_NVRAM_DEVPATH_LEN) {
 260                /* Format: devpathX=pcie/Y/Z/
 261                 * Y = domain_nr, Z = bus_nr, X = virtual ID
 262                 */
 263                if (strncmp(&nvp->nvram[i], "devpath", 7) == 0 &&
 264                    (!strncmp(&nvp->nvram[i + 8], pci_path, pci_len) ||
 265                     !strncmp(&nvp->nvram[i + 8], pcie_path, pcie_len))) {
 266                        id = nvp->nvram[i + 7] - '0';
 267                        found = true;
 268                        break;
 269                }
 270                while (nvp->nvram[i] != 0)
 271                        i++;
 272                i++;
 273        }
 274        if (!found)
 275                goto fail;
 276
 277        /* Now copy all valid entries, release old nvram and assign new one */
 278        i = 0;
 279        j = 0;
 280        while (i < nvp->nvram_len) {
 281                if ((nvp->nvram[i] - '0' == id) && (nvp->nvram[i + 1] == ':')) {
 282                        i += 2;
 283                        if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0)
 284                                nvp->boardrev_found = true;
 285                        while (nvp->nvram[i] != 0) {
 286                                nvram[j] = nvp->nvram[i];
 287                                i++;
 288                                j++;
 289                        }
 290                        nvram[j] = 0;
 291                        j++;
 292                }
 293                while (nvp->nvram[i] != 0)
 294                        i++;
 295                i++;
 296        }
 297        kfree(nvp->nvram);
 298        nvp->nvram = nvram;
 299        nvp->nvram_len = j;
 300        return;
 301
 302fail:
 303        kfree(nvram);
 304        nvp->nvram_len = 0;
 305}
 306
 307/* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple
 308 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
 309 * which data is to be returned. v2 is the version where nvram is stored
 310 * uncompressed, all relevant valid entries are identified by
 311 * pcie/domain_nr/bus_nr:
 312 */
 313static void brcmf_fw_strip_multi_v2(struct nvram_parser *nvp, u16 domain_nr,
 314                                    u16 bus_nr)
 315{
 316        char prefix[BRCMF_FW_NVRAM_PCIEDEV_LEN];
 317        size_t len;
 318        u32 i, j;
 319        u8 *nvram;
 320
 321        nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
 322        if (!nvram) {
 323                nvp->nvram_len = 0;
 324                return;
 325        }
 326
 327        /* Copy all valid entries, release old nvram and assign new one.
 328         * Valid entries are of type pcie/X/Y/ where X = domain_nr and
 329         * Y = bus_nr.
 330         */
 331        snprintf(prefix, sizeof(prefix), "pcie/%d/%d/", domain_nr, bus_nr);
 332        len = strlen(prefix);
 333        i = 0;
 334        j = 0;
 335        while (i < nvp->nvram_len - len) {
 336                if (strncmp(&nvp->nvram[i], prefix, len) == 0) {
 337                        i += len;
 338                        if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0)
 339                                nvp->boardrev_found = true;
 340                        while (nvp->nvram[i] != 0) {
 341                                nvram[j] = nvp->nvram[i];
 342                                i++;
 343                                j++;
 344                        }
 345                        nvram[j] = 0;
 346                        j++;
 347                }
 348                while (nvp->nvram[i] != 0)
 349                        i++;
 350                i++;
 351        }
 352        kfree(nvp->nvram);
 353        nvp->nvram = nvram;
 354        nvp->nvram_len = j;
 355}
 356
 357static void brcmf_fw_add_defaults(struct nvram_parser *nvp)
 358{
 359        if (nvp->boardrev_found)
 360                return;
 361
 362        memcpy(&nvp->nvram[nvp->nvram_len], &BRCMF_FW_DEFAULT_BOARDREV,
 363               strlen(BRCMF_FW_DEFAULT_BOARDREV));
 364        nvp->nvram_len += strlen(BRCMF_FW_DEFAULT_BOARDREV);
 365        nvp->nvram[nvp->nvram_len] = '\0';
 366        nvp->nvram_len++;
 367}
 368
 369/* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil
 370 * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
 371 * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
 372 * End of buffer is completed with token identifying length of buffer.
 373 */
 374static void *brcmf_fw_nvram_strip(const u8 *data, size_t data_len,
 375                                  u32 *new_length, u16 domain_nr, u16 bus_nr)
 376{
 377        struct nvram_parser nvp;
 378        u32 pad;
 379        u32 token;
 380        __le32 token_le;
 381
 382        if (brcmf_init_nvram_parser(&nvp, data, data_len) < 0)
 383                return NULL;
 384
 385        while (nvp.pos < data_len) {
 386                nvp.state = nv_parser_states[nvp.state](&nvp);
 387                if (nvp.state == END)
 388                        break;
 389        }
 390        if (nvp.multi_dev_v1) {
 391                nvp.boardrev_found = false;
 392                brcmf_fw_strip_multi_v1(&nvp, domain_nr, bus_nr);
 393        } else if (nvp.multi_dev_v2) {
 394                nvp.boardrev_found = false;
 395                brcmf_fw_strip_multi_v2(&nvp, domain_nr, bus_nr);
 396        }
 397
 398        if (nvp.nvram_len == 0) {
 399                kfree(nvp.nvram);
 400                return NULL;
 401        }
 402
 403        brcmf_fw_add_defaults(&nvp);
 404
 405        pad = nvp.nvram_len;
 406        *new_length = roundup(nvp.nvram_len + 1, 4);
 407        while (pad != *new_length) {
 408                nvp.nvram[pad] = 0;
 409                pad++;
 410        }
 411
 412        token = *new_length / 4;
 413        token = (~token << 16) | (token & 0x0000FFFF);
 414        token_le = cpu_to_le32(token);
 415
 416        memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le));
 417        *new_length += sizeof(token_le);
 418
 419        return nvp.nvram;
 420}
 421
 422void brcmf_fw_nvram_free(void *nvram)
 423{
 424        kfree(nvram);
 425}
 426
 427struct brcmf_fw {
 428        struct device *dev;
 429        struct brcmf_fw_request *req;
 430        u32 curpos;
 431        void (*done)(struct device *dev, int err, struct brcmf_fw_request *req);
 432};
 433
 434static void brcmf_fw_request_done(const struct firmware *fw, void *ctx);
 435
 436#ifdef CONFIG_EFI
 437/* In some cases the EFI-var stored nvram contains "ccode=ALL" or "ccode=XV"
 438 * to specify "worldwide" compatible settings, but these 2 ccode-s do not work
 439 * properly. "ccode=ALL" causes channels 12 and 13 to not be available,
 440 * "ccode=XV" causes all 5GHz channels to not be available. So we replace both
 441 * with "ccode=X2" which allows channels 12+13 and 5Ghz channels in
 442 * no-Initiate-Radiation mode. This means that we will never send on these
 443 * channels without first having received valid wifi traffic on the channel.
 444 */
 445static void brcmf_fw_fix_efi_nvram_ccode(char *data, unsigned long data_len)
 446{
 447        char *ccode;
 448
 449        ccode = strnstr((char *)data, "ccode=ALL", data_len);
 450        if (!ccode)
 451                ccode = strnstr((char *)data, "ccode=XV\r", data_len);
 452        if (!ccode)
 453                return;
 454
 455        ccode[6] = 'X';
 456        ccode[7] = '2';
 457        ccode[8] = '\r';
 458}
 459
 460static u8 *brcmf_fw_nvram_from_efi(size_t *data_len_ret)
 461{
 462        const u16 name[] = { 'n', 'v', 'r', 'a', 'm', 0 };
 463        struct efivar_entry *nvram_efivar;
 464        unsigned long data_len = 0;
 465        u8 *data = NULL;
 466        int err;
 467
 468        nvram_efivar = kzalloc(sizeof(*nvram_efivar), GFP_KERNEL);
 469        if (!nvram_efivar)
 470                return NULL;
 471
 472        memcpy(&nvram_efivar->var.VariableName, name, sizeof(name));
 473        nvram_efivar->var.VendorGuid = EFI_GUID(0x74b00bd9, 0x805a, 0x4d61,
 474                                                0xb5, 0x1f, 0x43, 0x26,
 475                                                0x81, 0x23, 0xd1, 0x13);
 476
 477        err = efivar_entry_size(nvram_efivar, &data_len);
 478        if (err)
 479                goto fail;
 480
 481        data = kmalloc(data_len, GFP_KERNEL);
 482        if (!data)
 483                goto fail;
 484
 485        err = efivar_entry_get(nvram_efivar, NULL, &data_len, data);
 486        if (err)
 487                goto fail;
 488
 489        brcmf_fw_fix_efi_nvram_ccode(data, data_len);
 490        brcmf_info("Using nvram EFI variable\n");
 491
 492        kfree(nvram_efivar);
 493        *data_len_ret = data_len;
 494        return data;
 495
 496fail:
 497        kfree(data);
 498        kfree(nvram_efivar);
 499        return NULL;
 500}
 501#else
 502static inline u8 *brcmf_fw_nvram_from_efi(size_t *data_len) { return NULL; }
 503#endif
 504
 505static void brcmf_fw_free_request(struct brcmf_fw_request *req)
 506{
 507        struct brcmf_fw_item *item;
 508        int i;
 509
 510        for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) {
 511                if (item->type == BRCMF_FW_TYPE_BINARY)
 512                        release_firmware(item->binary);
 513                else if (item->type == BRCMF_FW_TYPE_NVRAM)
 514                        brcmf_fw_nvram_free(item->nv_data.data);
 515        }
 516        kfree(req);
 517}
 518
 519static int brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx)
 520{
 521        struct brcmf_fw *fwctx = ctx;
 522        struct brcmf_fw_item *cur;
 523        bool free_bcm47xx_nvram = false;
 524        bool kfree_nvram = false;
 525        u32 nvram_length = 0;
 526        void *nvram = NULL;
 527        u8 *data = NULL;
 528        size_t data_len;
 529
 530        brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
 531
 532        cur = &fwctx->req->items[fwctx->curpos];
 533
 534        if (fw && fw->data) {
 535                data = (u8 *)fw->data;
 536                data_len = fw->size;
 537        } else {
 538                if ((data = bcm47xx_nvram_get_contents(&data_len)))
 539                        free_bcm47xx_nvram = true;
 540                else if ((data = brcmf_fw_nvram_from_efi(&data_len)))
 541                        kfree_nvram = true;
 542                else if (!(cur->flags & BRCMF_FW_REQF_OPTIONAL))
 543                        goto fail;
 544        }
 545
 546        if (data)
 547                nvram = brcmf_fw_nvram_strip(data, data_len, &nvram_length,
 548                                             fwctx->req->domain_nr,
 549                                             fwctx->req->bus_nr);
 550
 551        if (free_bcm47xx_nvram)
 552                bcm47xx_nvram_release_contents(data);
 553        if (kfree_nvram)
 554                kfree(data);
 555
 556        release_firmware(fw);
 557        if (!nvram && !(cur->flags & BRCMF_FW_REQF_OPTIONAL))
 558                goto fail;
 559
 560        brcmf_dbg(TRACE, "nvram %p len %d\n", nvram, nvram_length);
 561        cur->nv_data.data = nvram;
 562        cur->nv_data.len = nvram_length;
 563        return 0;
 564
 565fail:
 566        return -ENOENT;
 567}
 568
 569static int brcmf_fw_complete_request(const struct firmware *fw,
 570                                     struct brcmf_fw *fwctx)
 571{
 572        struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos];
 573        int ret = 0;
 574
 575        brcmf_dbg(TRACE, "firmware %s %sfound\n", cur->path, fw ? "" : "not ");
 576
 577        switch (cur->type) {
 578        case BRCMF_FW_TYPE_NVRAM:
 579                ret = brcmf_fw_request_nvram_done(fw, fwctx);
 580                break;
 581        case BRCMF_FW_TYPE_BINARY:
 582                if (fw)
 583                        cur->binary = fw;
 584                else
 585                        ret = -ENOENT;
 586                break;
 587        default:
 588                /* something fishy here so bail out early */
 589                brcmf_err("unknown fw type: %d\n", cur->type);
 590                release_firmware(fw);
 591                ret = -EINVAL;
 592        }
 593
 594        return (cur->flags & BRCMF_FW_REQF_OPTIONAL) ? 0 : ret;
 595}
 596
 597static int brcmf_fw_request_firmware(const struct firmware **fw,
 598                                     struct brcmf_fw *fwctx)
 599{
 600        struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos];
 601        int ret;
 602
 603        /* nvram files are board-specific, first try a board-specific path */
 604        if (cur->type == BRCMF_FW_TYPE_NVRAM && fwctx->req->board_type) {
 605                char alt_path[BRCMF_FW_NAME_LEN];
 606
 607                strlcpy(alt_path, cur->path, BRCMF_FW_NAME_LEN);
 608                /* strip .txt at the end */
 609                alt_path[strlen(alt_path) - 4] = 0;
 610                strlcat(alt_path, ".", BRCMF_FW_NAME_LEN);
 611                strlcat(alt_path, fwctx->req->board_type, BRCMF_FW_NAME_LEN);
 612                strlcat(alt_path, ".txt", BRCMF_FW_NAME_LEN);
 613
 614                ret = request_firmware(fw, alt_path, fwctx->dev);
 615                if (ret == 0)
 616                        return ret;
 617        }
 618
 619        return request_firmware(fw, cur->path, fwctx->dev);
 620}
 621
 622static void brcmf_fw_request_done(const struct firmware *fw, void *ctx)
 623{
 624        struct brcmf_fw *fwctx = ctx;
 625        int ret;
 626
 627        ret = brcmf_fw_complete_request(fw, fwctx);
 628
 629        while (ret == 0 && ++fwctx->curpos < fwctx->req->n_items) {
 630                brcmf_fw_request_firmware(&fw, fwctx);
 631                ret = brcmf_fw_complete_request(fw, ctx);
 632        }
 633
 634        if (ret) {
 635                brcmf_fw_free_request(fwctx->req);
 636                fwctx->req = NULL;
 637        }
 638        fwctx->done(fwctx->dev, ret, fwctx->req);
 639        kfree(fwctx);
 640}
 641
 642static bool brcmf_fw_request_is_valid(struct brcmf_fw_request *req)
 643{
 644        struct brcmf_fw_item *item;
 645        int i;
 646
 647        if (!req->n_items)
 648                return false;
 649
 650        for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) {
 651                if (!item->path)
 652                        return false;
 653        }
 654        return true;
 655}
 656
 657int brcmf_fw_get_firmwares(struct device *dev, struct brcmf_fw_request *req,
 658                           void (*fw_cb)(struct device *dev, int err,
 659                                         struct brcmf_fw_request *req))
 660{
 661        struct brcmf_fw_item *first = &req->items[0];
 662        struct brcmf_fw *fwctx;
 663        int ret;
 664
 665        brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
 666        if (!fw_cb)
 667                return -EINVAL;
 668
 669        if (!brcmf_fw_request_is_valid(req))
 670                return -EINVAL;
 671
 672        fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL);
 673        if (!fwctx)
 674                return -ENOMEM;
 675
 676        fwctx->dev = dev;
 677        fwctx->req = req;
 678        fwctx->done = fw_cb;
 679
 680        ret = request_firmware_nowait(THIS_MODULE, true, first->path,
 681                                      fwctx->dev, GFP_KERNEL, fwctx,
 682                                      brcmf_fw_request_done);
 683        if (ret < 0)
 684                brcmf_fw_request_done(NULL, fwctx);
 685
 686        return 0;
 687}
 688
 689struct brcmf_fw_request *
 690brcmf_fw_alloc_request(u32 chip, u32 chiprev,
 691                       const struct brcmf_firmware_mapping mapping_table[],
 692                       u32 table_size, struct brcmf_fw_name *fwnames,
 693                       u32 n_fwnames)
 694{
 695        struct brcmf_fw_request *fwreq;
 696        char chipname[12];
 697        const char *mp_path;
 698        size_t mp_path_len;
 699        u32 i, j;
 700        char end = '\0';
 701
 702        for (i = 0; i < table_size; i++) {
 703                if (mapping_table[i].chipid == chip &&
 704                    mapping_table[i].revmask & BIT(chiprev))
 705                        break;
 706        }
 707
 708        brcmf_chip_name(chip, chiprev, chipname, sizeof(chipname));
 709
 710        if (i == table_size) {
 711                brcmf_err("Unknown chip %s\n", chipname);
 712                return NULL;
 713        }
 714
 715        fwreq = kzalloc(struct_size(fwreq, items, n_fwnames), GFP_KERNEL);
 716        if (!fwreq)
 717                return NULL;
 718
 719        brcmf_info("using %s for chip %s\n",
 720                   mapping_table[i].fw_base, chipname);
 721
 722        mp_path = brcmf_mp_global.firmware_path;
 723        mp_path_len = strnlen(mp_path, BRCMF_FW_ALTPATH_LEN);
 724        if (mp_path_len)
 725                end = mp_path[mp_path_len - 1];
 726
 727        fwreq->n_items = n_fwnames;
 728
 729        for (j = 0; j < n_fwnames; j++) {
 730                fwreq->items[j].path = fwnames[j].path;
 731                fwnames[j].path[0] = '\0';
 732                /* check if firmware path is provided by module parameter */
 733                if (brcmf_mp_global.firmware_path[0] != '\0') {
 734                        strlcpy(fwnames[j].path, mp_path,
 735                                BRCMF_FW_NAME_LEN);
 736
 737                        if (end != '/') {
 738                                strlcat(fwnames[j].path, "/",
 739                                        BRCMF_FW_NAME_LEN);
 740                        }
 741                }
 742                strlcat(fwnames[j].path, mapping_table[i].fw_base,
 743                        BRCMF_FW_NAME_LEN);
 744                strlcat(fwnames[j].path, fwnames[j].extension,
 745                        BRCMF_FW_NAME_LEN);
 746                fwreq->items[j].path = fwnames[j].path;
 747        }
 748
 749        return fwreq;
 750}
 751