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 25 enum 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 */ 48 struct 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 */ 68 static 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 78 static bool is_whitespace(char c) 79 { 80 return (c == ' ' || c == '\r' || c == '\n' || c == '\t'); 81 } 82 83 static 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); 100 proceed: 101 nvp->column++; 102 nvp->pos++; 103 return IDLE; 104 } 105 106 static 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 135 static enum nvram_parser_state 136 brcmf_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 163 static enum nvram_parser_state 164 brcmf_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 183 static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp) 184 { 185 /* final state */ 186 return END; 187 } 188 189 static 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 198 static 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 /* Add space for properties we may add */ 211 size += strlen(BRCMF_FW_DEFAULT_BOARDREV) + 1; 212 /* Alloc for extra 0 byte + roundup by 4 + length field */ 213 size += 1 + 3 + sizeof(u32); 214 nvp->nvram = kzalloc(size, GFP_KERNEL); 215 if (!nvp->nvram) 216 return -ENOMEM; 217 218 nvp->line = 1; 219 nvp->column = 1; 220 return 0; 221 } 222 223 /* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple 224 * devices. Strip it down for one device, use domain_nr/bus_nr to determine 225 * which data is to be returned. v1 is the version where nvram is stored 226 * compressed and "devpath" maps to index for valid entries. 227 */ 228 static void brcmf_fw_strip_multi_v1(struct nvram_parser *nvp, u16 domain_nr, 229 u16 bus_nr) 230 { 231 /* Device path with a leading '=' key-value separator */ 232 char pci_path[] = "=pci/?/?"; 233 size_t pci_len; 234 char pcie_path[] = "=pcie/?/?"; 235 size_t pcie_len; 236 237 u32 i, j; 238 bool found; 239 u8 *nvram; 240 u8 id; 241 242 nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL); 243 if (!nvram) 244 goto fail; 245 246 /* min length: devpath0=pcie/1/4/ + 0:x=y */ 247 if (nvp->nvram_len < BRCMF_FW_NVRAM_DEVPATH_LEN + 6) 248 goto fail; 249 250 /* First search for the devpathX and see if it is the configuration 251 * for domain_nr/bus_nr. Search complete nvp 252 */ 253 snprintf(pci_path, sizeof(pci_path), "=pci/%d/%d", domain_nr, 254 bus_nr); 255 pci_len = strlen(pci_path); 256 snprintf(pcie_path, sizeof(pcie_path), "=pcie/%d/%d", domain_nr, 257 bus_nr); 258 pcie_len = strlen(pcie_path); 259 found = false; 260 i = 0; 261 while (i < nvp->nvram_len - BRCMF_FW_NVRAM_DEVPATH_LEN) { 262 /* Format: devpathX=pcie/Y/Z/ 263 * Y = domain_nr, Z = bus_nr, X = virtual ID 264 */ 265 if (strncmp(&nvp->nvram[i], "devpath", 7) == 0 && 266 (!strncmp(&nvp->nvram[i + 8], pci_path, pci_len) || 267 !strncmp(&nvp->nvram[i + 8], pcie_path, pcie_len))) { 268 id = nvp->nvram[i + 7] - '0'; 269 found = true; 270 break; 271 } 272 while (nvp->nvram[i] != 0) 273 i++; 274 i++; 275 } 276 if (!found) 277 goto fail; 278 279 /* Now copy all valid entries, release old nvram and assign new one */ 280 i = 0; 281 j = 0; 282 while (i < nvp->nvram_len) { 283 if ((nvp->nvram[i] - '0' == id) && (nvp->nvram[i + 1] == ':')) { 284 i += 2; 285 if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0) 286 nvp->boardrev_found = true; 287 while (nvp->nvram[i] != 0) { 288 nvram[j] = nvp->nvram[i]; 289 i++; 290 j++; 291 } 292 nvram[j] = 0; 293 j++; 294 } 295 while (nvp->nvram[i] != 0) 296 i++; 297 i++; 298 } 299 kfree(nvp->nvram); 300 nvp->nvram = nvram; 301 nvp->nvram_len = j; 302 return; 303 304 fail: 305 kfree(nvram); 306 nvp->nvram_len = 0; 307 } 308 309 /* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple 310 * devices. Strip it down for one device, use domain_nr/bus_nr to determine 311 * which data is to be returned. v2 is the version where nvram is stored 312 * uncompressed, all relevant valid entries are identified by 313 * pcie/domain_nr/bus_nr: 314 */ 315 static void brcmf_fw_strip_multi_v2(struct nvram_parser *nvp, u16 domain_nr, 316 u16 bus_nr) 317 { 318 char prefix[BRCMF_FW_NVRAM_PCIEDEV_LEN]; 319 size_t len; 320 u32 i, j; 321 u8 *nvram; 322 323 nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL); 324 if (!nvram) { 325 nvp->nvram_len = 0; 326 return; 327 } 328 329 /* Copy all valid entries, release old nvram and assign new one. 330 * Valid entries are of type pcie/X/Y/ where X = domain_nr and 331 * Y = bus_nr. 332 */ 333 snprintf(prefix, sizeof(prefix), "pcie/%d/%d/", domain_nr, bus_nr); 334 len = strlen(prefix); 335 i = 0; 336 j = 0; 337 while (i < nvp->nvram_len - len) { 338 if (strncmp(&nvp->nvram[i], prefix, len) == 0) { 339 i += len; 340 if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0) 341 nvp->boardrev_found = true; 342 while (nvp->nvram[i] != 0) { 343 nvram[j] = nvp->nvram[i]; 344 i++; 345 j++; 346 } 347 nvram[j] = 0; 348 j++; 349 } 350 while (nvp->nvram[i] != 0) 351 i++; 352 i++; 353 } 354 kfree(nvp->nvram); 355 nvp->nvram = nvram; 356 nvp->nvram_len = j; 357 } 358 359 static void brcmf_fw_add_defaults(struct nvram_parser *nvp) 360 { 361 if (nvp->boardrev_found) 362 return; 363 364 memcpy(&nvp->nvram[nvp->nvram_len], &BRCMF_FW_DEFAULT_BOARDREV, 365 strlen(BRCMF_FW_DEFAULT_BOARDREV)); 366 nvp->nvram_len += strlen(BRCMF_FW_DEFAULT_BOARDREV); 367 nvp->nvram[nvp->nvram_len] = '\0'; 368 nvp->nvram_len++; 369 } 370 371 /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil 372 * and ending in a NUL. Removes carriage returns, empty lines, comment lines, 373 * and converts newlines to NULs. Shortens buffer as needed and pads with NULs. 374 * End of buffer is completed with token identifying length of buffer. 375 */ 376 static void *brcmf_fw_nvram_strip(const u8 *data, size_t data_len, 377 u32 *new_length, u16 domain_nr, u16 bus_nr) 378 { 379 struct nvram_parser nvp; 380 u32 pad; 381 u32 token; 382 __le32 token_le; 383 384 if (brcmf_init_nvram_parser(&nvp, data, data_len) < 0) 385 return NULL; 386 387 while (nvp.pos < data_len) { 388 nvp.state = nv_parser_states[nvp.state](&nvp); 389 if (nvp.state == END) 390 break; 391 } 392 if (nvp.multi_dev_v1) { 393 nvp.boardrev_found = false; 394 brcmf_fw_strip_multi_v1(&nvp, domain_nr, bus_nr); 395 } else if (nvp.multi_dev_v2) { 396 nvp.boardrev_found = false; 397 brcmf_fw_strip_multi_v2(&nvp, domain_nr, bus_nr); 398 } 399 400 if (nvp.nvram_len == 0) { 401 kfree(nvp.nvram); 402 return NULL; 403 } 404 405 brcmf_fw_add_defaults(&nvp); 406 407 pad = nvp.nvram_len; 408 *new_length = roundup(nvp.nvram_len + 1, 4); 409 while (pad != *new_length) { 410 nvp.nvram[pad] = 0; 411 pad++; 412 } 413 414 token = *new_length / 4; 415 token = (~token << 16) | (token & 0x0000FFFF); 416 token_le = cpu_to_le32(token); 417 418 memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le)); 419 *new_length += sizeof(token_le); 420 421 return nvp.nvram; 422 } 423 424 void brcmf_fw_nvram_free(void *nvram) 425 { 426 kfree(nvram); 427 } 428 429 struct brcmf_fw { 430 struct device *dev; 431 struct brcmf_fw_request *req; 432 u32 curpos; 433 void (*done)(struct device *dev, int err, struct brcmf_fw_request *req); 434 }; 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 */ 445 static 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 460 static u8 *brcmf_fw_nvram_from_efi(size_t *data_len_ret) 461 { 462 efi_guid_t guid = EFI_GUID(0x74b00bd9, 0x805a, 0x4d61, 0xb5, 0x1f, 463 0x43, 0x26, 0x81, 0x23, 0xd1, 0x13); 464 unsigned long data_len = 0; 465 efi_status_t status; 466 u8 *data = NULL; 467 468 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) 469 return NULL; 470 471 status = efi.get_variable(L"nvram", &guid, NULL, &data_len, NULL); 472 if (status != EFI_BUFFER_TOO_SMALL) 473 goto fail; 474 475 data = kmalloc(data_len, GFP_KERNEL); 476 if (!data) 477 goto fail; 478 479 status = efi.get_variable(L"nvram", &guid, NULL, &data_len, data); 480 if (status != EFI_SUCCESS) 481 goto fail; 482 483 brcmf_fw_fix_efi_nvram_ccode(data, data_len); 484 brcmf_info("Using nvram EFI variable\n"); 485 486 *data_len_ret = data_len; 487 return data; 488 fail: 489 kfree(data); 490 return NULL; 491 } 492 #else 493 static inline u8 *brcmf_fw_nvram_from_efi(size_t *data_len) { return NULL; } 494 #endif 495 496 static void brcmf_fw_free_request(struct brcmf_fw_request *req) 497 { 498 struct brcmf_fw_item *item; 499 int i; 500 501 for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) { 502 if (item->type == BRCMF_FW_TYPE_BINARY) 503 release_firmware(item->binary); 504 else if (item->type == BRCMF_FW_TYPE_NVRAM) 505 brcmf_fw_nvram_free(item->nv_data.data); 506 } 507 kfree(req); 508 } 509 510 static int brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx) 511 { 512 struct brcmf_fw *fwctx = ctx; 513 struct brcmf_fw_item *cur; 514 bool free_bcm47xx_nvram = false; 515 bool kfree_nvram = false; 516 u32 nvram_length = 0; 517 void *nvram = NULL; 518 u8 *data = NULL; 519 size_t data_len; 520 521 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev)); 522 523 cur = &fwctx->req->items[fwctx->curpos]; 524 525 if (fw && fw->data) { 526 data = (u8 *)fw->data; 527 data_len = fw->size; 528 } else { 529 if ((data = bcm47xx_nvram_get_contents(&data_len))) 530 free_bcm47xx_nvram = true; 531 else if ((data = brcmf_fw_nvram_from_efi(&data_len))) 532 kfree_nvram = true; 533 else if (!(cur->flags & BRCMF_FW_REQF_OPTIONAL)) 534 goto fail; 535 } 536 537 if (data) 538 nvram = brcmf_fw_nvram_strip(data, data_len, &nvram_length, 539 fwctx->req->domain_nr, 540 fwctx->req->bus_nr); 541 542 if (free_bcm47xx_nvram) 543 bcm47xx_nvram_release_contents(data); 544 if (kfree_nvram) 545 kfree(data); 546 547 release_firmware(fw); 548 if (!nvram && !(cur->flags & BRCMF_FW_REQF_OPTIONAL)) 549 goto fail; 550 551 brcmf_dbg(TRACE, "nvram %p len %d\n", nvram, nvram_length); 552 cur->nv_data.data = nvram; 553 cur->nv_data.len = nvram_length; 554 return 0; 555 556 fail: 557 return -ENOENT; 558 } 559 560 static int brcmf_fw_complete_request(const struct firmware *fw, 561 struct brcmf_fw *fwctx) 562 { 563 struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos]; 564 int ret = 0; 565 566 brcmf_dbg(TRACE, "firmware %s %sfound\n", cur->path, fw ? "" : "not "); 567 568 switch (cur->type) { 569 case BRCMF_FW_TYPE_NVRAM: 570 ret = brcmf_fw_request_nvram_done(fw, fwctx); 571 break; 572 case BRCMF_FW_TYPE_BINARY: 573 if (fw) 574 cur->binary = fw; 575 else 576 ret = -ENOENT; 577 break; 578 default: 579 /* something fishy here so bail out early */ 580 brcmf_err("unknown fw type: %d\n", cur->type); 581 release_firmware(fw); 582 ret = -EINVAL; 583 } 584 585 return (cur->flags & BRCMF_FW_REQF_OPTIONAL) ? 0 : ret; 586 } 587 588 static char *brcm_alt_fw_path(const char *path, const char *board_type) 589 { 590 char alt_path[BRCMF_FW_NAME_LEN]; 591 char suffix[5]; 592 593 strscpy(alt_path, path, BRCMF_FW_NAME_LEN); 594 /* At least one character + suffix */ 595 if (strlen(alt_path) < 5) 596 return NULL; 597 598 /* strip .txt or .bin at the end */ 599 strscpy(suffix, alt_path + strlen(alt_path) - 4, 5); 600 alt_path[strlen(alt_path) - 4] = 0; 601 strlcat(alt_path, ".", BRCMF_FW_NAME_LEN); 602 strlcat(alt_path, board_type, BRCMF_FW_NAME_LEN); 603 strlcat(alt_path, suffix, BRCMF_FW_NAME_LEN); 604 605 return kstrdup(alt_path, GFP_KERNEL); 606 } 607 608 static int brcmf_fw_request_firmware(const struct firmware **fw, 609 struct brcmf_fw *fwctx) 610 { 611 struct brcmf_fw_item *cur = &fwctx->req->items[fwctx->curpos]; 612 int ret; 613 614 /* Files can be board-specific, first try a board-specific path */ 615 if (cur->type == BRCMF_FW_TYPE_NVRAM && fwctx->req->board_type) { 616 char *alt_path; 617 618 alt_path = brcm_alt_fw_path(cur->path, fwctx->req->board_type); 619 if (!alt_path) 620 goto fallback; 621 622 ret = request_firmware(fw, alt_path, fwctx->dev); 623 kfree(alt_path); 624 if (ret == 0) 625 return ret; 626 } 627 628 fallback: 629 return request_firmware(fw, cur->path, fwctx->dev); 630 } 631 632 static void brcmf_fw_request_done(const struct firmware *fw, void *ctx) 633 { 634 struct brcmf_fw *fwctx = ctx; 635 int ret; 636 637 ret = brcmf_fw_complete_request(fw, fwctx); 638 639 while (ret == 0 && ++fwctx->curpos < fwctx->req->n_items) { 640 brcmf_fw_request_firmware(&fw, fwctx); 641 ret = brcmf_fw_complete_request(fw, ctx); 642 } 643 644 if (ret) { 645 brcmf_fw_free_request(fwctx->req); 646 fwctx->req = NULL; 647 } 648 fwctx->done(fwctx->dev, ret, fwctx->req); 649 kfree(fwctx); 650 } 651 652 static void brcmf_fw_request_done_alt_path(const struct firmware *fw, void *ctx) 653 { 654 struct brcmf_fw *fwctx = ctx; 655 struct brcmf_fw_item *first = &fwctx->req->items[0]; 656 int ret = 0; 657 658 /* Fall back to canonical path if board firmware not found */ 659 if (!fw) 660 ret = request_firmware_nowait(THIS_MODULE, true, first->path, 661 fwctx->dev, GFP_KERNEL, fwctx, 662 brcmf_fw_request_done); 663 664 if (fw || ret < 0) 665 brcmf_fw_request_done(fw, ctx); 666 } 667 668 static bool brcmf_fw_request_is_valid(struct brcmf_fw_request *req) 669 { 670 struct brcmf_fw_item *item; 671 int i; 672 673 if (!req->n_items) 674 return false; 675 676 for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) { 677 if (!item->path) 678 return false; 679 } 680 return true; 681 } 682 683 int brcmf_fw_get_firmwares(struct device *dev, struct brcmf_fw_request *req, 684 void (*fw_cb)(struct device *dev, int err, 685 struct brcmf_fw_request *req)) 686 { 687 struct brcmf_fw_item *first = &req->items[0]; 688 struct brcmf_fw *fwctx; 689 char *alt_path = NULL; 690 int ret; 691 692 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev)); 693 if (!fw_cb) 694 return -EINVAL; 695 696 if (!brcmf_fw_request_is_valid(req)) 697 return -EINVAL; 698 699 fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL); 700 if (!fwctx) 701 return -ENOMEM; 702 703 fwctx->dev = dev; 704 fwctx->req = req; 705 fwctx->done = fw_cb; 706 707 /* First try alternative board-specific path if any */ 708 if (fwctx->req->board_type) 709 alt_path = brcm_alt_fw_path(first->path, 710 fwctx->req->board_type); 711 if (alt_path) { 712 ret = request_firmware_nowait(THIS_MODULE, true, alt_path, 713 fwctx->dev, GFP_KERNEL, fwctx, 714 brcmf_fw_request_done_alt_path); 715 kfree(alt_path); 716 } else { 717 ret = request_firmware_nowait(THIS_MODULE, true, first->path, 718 fwctx->dev, GFP_KERNEL, fwctx, 719 brcmf_fw_request_done); 720 } 721 if (ret < 0) 722 brcmf_fw_request_done(NULL, fwctx); 723 724 return 0; 725 } 726 727 struct brcmf_fw_request * 728 brcmf_fw_alloc_request(u32 chip, u32 chiprev, 729 const struct brcmf_firmware_mapping mapping_table[], 730 u32 table_size, struct brcmf_fw_name *fwnames, 731 u32 n_fwnames) 732 { 733 struct brcmf_fw_request *fwreq; 734 char chipname[12]; 735 const char *mp_path; 736 size_t mp_path_len; 737 u32 i, j; 738 char end = '\0'; 739 740 for (i = 0; i < table_size; i++) { 741 if (mapping_table[i].chipid == chip && 742 mapping_table[i].revmask & BIT(chiprev)) 743 break; 744 } 745 746 brcmf_chip_name(chip, chiprev, chipname, sizeof(chipname)); 747 748 if (i == table_size) { 749 brcmf_err("Unknown chip %s\n", chipname); 750 return NULL; 751 } 752 753 fwreq = kzalloc(struct_size(fwreq, items, n_fwnames), GFP_KERNEL); 754 if (!fwreq) 755 return NULL; 756 757 brcmf_info("using %s for chip %s\n", 758 mapping_table[i].fw_base, chipname); 759 760 mp_path = brcmf_mp_global.firmware_path; 761 mp_path_len = strnlen(mp_path, BRCMF_FW_ALTPATH_LEN); 762 if (mp_path_len) 763 end = mp_path[mp_path_len - 1]; 764 765 fwreq->n_items = n_fwnames; 766 767 for (j = 0; j < n_fwnames; j++) { 768 fwreq->items[j].path = fwnames[j].path; 769 fwnames[j].path[0] = '\0'; 770 /* check if firmware path is provided by module parameter */ 771 if (brcmf_mp_global.firmware_path[0] != '\0') { 772 strlcpy(fwnames[j].path, mp_path, 773 BRCMF_FW_NAME_LEN); 774 775 if (end != '/') { 776 strlcat(fwnames[j].path, "/", 777 BRCMF_FW_NAME_LEN); 778 } 779 } 780 strlcat(fwnames[j].path, mapping_table[i].fw_base, 781 BRCMF_FW_NAME_LEN); 782 strlcat(fwnames[j].path, fwnames[j].extension, 783 BRCMF_FW_NAME_LEN); 784 fwreq->items[j].path = fwnames[j].path; 785 } 786 787 return fwreq; 788 } 789