firmware.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright (c) 2013 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. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/device.h>
  19. #include <linux/firmware.h>
  20. #include <linux/module.h>
  21. #include <linux/bcm47xx_nvram.h>
  22. #include "debug.h"
  23. #include "firmware.h"
  24. #include "core.h"
  25. #include "common.h"
  26. #include "chip.h"
  27. #define BRCMF_FW_MAX_NVRAM_SIZE 64000
  28. #define BRCMF_FW_NVRAM_DEVPATH_LEN 19 /* devpath0=pcie/1/4/ */
  29. #define BRCMF_FW_NVRAM_PCIEDEV_LEN 10 /* pcie/1/4/ + \0 */
  30. #define BRCMF_FW_DEFAULT_BOARDREV "boardrev=0xff"
  31. enum nvram_parser_state {
  32. IDLE,
  33. KEY,
  34. VALUE,
  35. COMMENT,
  36. END
  37. };
  38. /**
  39. * struct nvram_parser - internal info for parser.
  40. *
  41. * @state: current parser state.
  42. * @data: input buffer being parsed.
  43. * @nvram: output buffer with parse result.
  44. * @nvram_len: lenght of parse result.
  45. * @line: current line.
  46. * @column: current column in line.
  47. * @pos: byte offset in input buffer.
  48. * @entry: start position of key,value entry.
  49. * @multi_dev_v1: detect pcie multi device v1 (compressed).
  50. * @multi_dev_v2: detect pcie multi device v2.
  51. * @boardrev_found: nvram contains boardrev information.
  52. */
  53. struct nvram_parser {
  54. enum nvram_parser_state state;
  55. const u8 *data;
  56. u8 *nvram;
  57. u32 nvram_len;
  58. u32 line;
  59. u32 column;
  60. u32 pos;
  61. u32 entry;
  62. bool multi_dev_v1;
  63. bool multi_dev_v2;
  64. bool boardrev_found;
  65. };
  66. /**
  67. * is_nvram_char() - check if char is a valid one for NVRAM entry
  68. *
  69. * It accepts all printable ASCII chars except for '#' which opens a comment.
  70. * Please note that ' ' (space) while accepted is not a valid key name char.
  71. */
  72. static bool is_nvram_char(char c)
  73. {
  74. /* comment marker excluded */
  75. if (c == '#')
  76. return false;
  77. /* key and value may have any other readable character */
  78. return (c >= 0x20 && c < 0x7f);
  79. }
  80. static bool is_whitespace(char c)
  81. {
  82. return (c == ' ' || c == '\r' || c == '\n' || c == '\t');
  83. }
  84. static enum nvram_parser_state brcmf_nvram_handle_idle(struct nvram_parser *nvp)
  85. {
  86. char c;
  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. static enum nvram_parser_state brcmf_nvram_handle_key(struct nvram_parser *nvp)
  106. {
  107. enum nvram_parser_state st = nvp->state;
  108. char c;
  109. c = nvp->data[nvp->pos];
  110. if (c == '=') {
  111. /* ignore RAW1 by treating as comment */
  112. if (strncmp(&nvp->data[nvp->entry], "RAW1", 4) == 0)
  113. st = COMMENT;
  114. else
  115. st = VALUE;
  116. if (strncmp(&nvp->data[nvp->entry], "devpath", 7) == 0)
  117. nvp->multi_dev_v1 = true;
  118. if (strncmp(&nvp->data[nvp->entry], "pcie/", 5) == 0)
  119. nvp->multi_dev_v2 = true;
  120. if (strncmp(&nvp->data[nvp->entry], "boardrev", 8) == 0)
  121. nvp->boardrev_found = true;
  122. } else if (!is_nvram_char(c) || c == ' ') {
  123. brcmf_dbg(INFO, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n",
  124. nvp->line, nvp->column);
  125. return COMMENT;
  126. }
  127. nvp->column++;
  128. nvp->pos++;
  129. return st;
  130. }
  131. static enum nvram_parser_state
  132. brcmf_nvram_handle_value(struct nvram_parser *nvp)
  133. {
  134. char c;
  135. char *skv;
  136. char *ekv;
  137. u32 cplen;
  138. c = nvp->data[nvp->pos];
  139. if (!is_nvram_char(c)) {
  140. /* key,value pair complete */
  141. ekv = (u8 *)&nvp->data[nvp->pos];
  142. skv = (u8 *)&nvp->data[nvp->entry];
  143. cplen = ekv - skv;
  144. if (nvp->nvram_len + cplen + 1 >= BRCMF_FW_MAX_NVRAM_SIZE)
  145. return END;
  146. /* copy to output buffer */
  147. memcpy(&nvp->nvram[nvp->nvram_len], skv, cplen);
  148. nvp->nvram_len += cplen;
  149. nvp->nvram[nvp->nvram_len] = '\0';
  150. nvp->nvram_len++;
  151. return IDLE;
  152. }
  153. nvp->pos++;
  154. nvp->column++;
  155. return VALUE;
  156. }
  157. static enum nvram_parser_state
  158. brcmf_nvram_handle_comment(struct nvram_parser *nvp)
  159. {
  160. char *eoc, *sol;
  161. sol = (char *)&nvp->data[nvp->pos];
  162. eoc = strchr(sol, '\n');
  163. if (!eoc) {
  164. eoc = strchr(sol, '\0');
  165. if (!eoc)
  166. return END;
  167. }
  168. /* eat all moving to next line */
  169. nvp->line++;
  170. nvp->column = 1;
  171. nvp->pos += (eoc - sol) + 1;
  172. return IDLE;
  173. }
  174. static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp)
  175. {
  176. /* final state */
  177. return END;
  178. }
  179. static enum nvram_parser_state
  180. (*nv_parser_states[])(struct nvram_parser *nvp) = {
  181. brcmf_nvram_handle_idle,
  182. brcmf_nvram_handle_key,
  183. brcmf_nvram_handle_value,
  184. brcmf_nvram_handle_comment,
  185. brcmf_nvram_handle_end
  186. };
  187. static int brcmf_init_nvram_parser(struct nvram_parser *nvp,
  188. const u8 *data, size_t data_len)
  189. {
  190. size_t size;
  191. memset(nvp, 0, sizeof(*nvp));
  192. nvp->data = data;
  193. /* Limit size to MAX_NVRAM_SIZE, some files contain lot of comment */
  194. if (data_len > BRCMF_FW_MAX_NVRAM_SIZE)
  195. size = BRCMF_FW_MAX_NVRAM_SIZE;
  196. else
  197. size = data_len;
  198. /* Alloc for extra 0 byte + roundup by 4 + length field */
  199. size += 1 + 3 + sizeof(u32);
  200. nvp->nvram = kzalloc(size, GFP_KERNEL);
  201. if (!nvp->nvram)
  202. return -ENOMEM;
  203. nvp->line = 1;
  204. nvp->column = 1;
  205. return 0;
  206. }
  207. /* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple
  208. * devices. Strip it down for one device, use domain_nr/bus_nr to determine
  209. * which data is to be returned. v1 is the version where nvram is stored
  210. * compressed and "devpath" maps to index for valid entries.
  211. */
  212. static void brcmf_fw_strip_multi_v1(struct nvram_parser *nvp, u16 domain_nr,
  213. u16 bus_nr)
  214. {
  215. /* Device path with a leading '=' key-value separator */
  216. char pci_path[] = "=pci/?/?";
  217. size_t pci_len;
  218. char pcie_path[] = "=pcie/?/?";
  219. size_t pcie_len;
  220. u32 i, j;
  221. bool found;
  222. u8 *nvram;
  223. u8 id;
  224. nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
  225. if (!nvram)
  226. goto fail;
  227. /* min length: devpath0=pcie/1/4/ + 0:x=y */
  228. if (nvp->nvram_len < BRCMF_FW_NVRAM_DEVPATH_LEN + 6)
  229. goto fail;
  230. /* First search for the devpathX and see if it is the configuration
  231. * for domain_nr/bus_nr. Search complete nvp
  232. */
  233. snprintf(pci_path, sizeof(pci_path), "=pci/%d/%d", domain_nr,
  234. bus_nr);
  235. pci_len = strlen(pci_path);
  236. snprintf(pcie_path, sizeof(pcie_path), "=pcie/%d/%d", domain_nr,
  237. bus_nr);
  238. pcie_len = strlen(pcie_path);
  239. found = false;
  240. i = 0;
  241. while (i < nvp->nvram_len - BRCMF_FW_NVRAM_DEVPATH_LEN) {
  242. /* Format: devpathX=pcie/Y/Z/
  243. * Y = domain_nr, Z = bus_nr, X = virtual ID
  244. */
  245. if (strncmp(&nvp->nvram[i], "devpath", 7) == 0 &&
  246. (!strncmp(&nvp->nvram[i + 8], pci_path, pci_len) ||
  247. !strncmp(&nvp->nvram[i + 8], pcie_path, pcie_len))) {
  248. id = nvp->nvram[i + 7] - '0';
  249. found = true;
  250. break;
  251. }
  252. while (nvp->nvram[i] != 0)
  253. i++;
  254. i++;
  255. }
  256. if (!found)
  257. goto fail;
  258. /* Now copy all valid entries, release old nvram and assign new one */
  259. i = 0;
  260. j = 0;
  261. while (i < nvp->nvram_len) {
  262. if ((nvp->nvram[i] - '0' == id) && (nvp->nvram[i + 1] == ':')) {
  263. i += 2;
  264. if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0)
  265. nvp->boardrev_found = true;
  266. while (nvp->nvram[i] != 0) {
  267. nvram[j] = nvp->nvram[i];
  268. i++;
  269. j++;
  270. }
  271. nvram[j] = 0;
  272. j++;
  273. }
  274. while (nvp->nvram[i] != 0)
  275. i++;
  276. i++;
  277. }
  278. kfree(nvp->nvram);
  279. nvp->nvram = nvram;
  280. nvp->nvram_len = j;
  281. return;
  282. fail:
  283. kfree(nvram);
  284. nvp->nvram_len = 0;
  285. }
  286. /* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple
  287. * devices. Strip it down for one device, use domain_nr/bus_nr to determine
  288. * which data is to be returned. v2 is the version where nvram is stored
  289. * uncompressed, all relevant valid entries are identified by
  290. * pcie/domain_nr/bus_nr:
  291. */
  292. static void brcmf_fw_strip_multi_v2(struct nvram_parser *nvp, u16 domain_nr,
  293. u16 bus_nr)
  294. {
  295. char prefix[BRCMF_FW_NVRAM_PCIEDEV_LEN];
  296. size_t len;
  297. u32 i, j;
  298. u8 *nvram;
  299. nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
  300. if (!nvram)
  301. goto fail;
  302. /* Copy all valid entries, release old nvram and assign new one.
  303. * Valid entries are of type pcie/X/Y/ where X = domain_nr and
  304. * Y = bus_nr.
  305. */
  306. snprintf(prefix, sizeof(prefix), "pcie/%d/%d/", domain_nr, bus_nr);
  307. len = strlen(prefix);
  308. i = 0;
  309. j = 0;
  310. while (i < nvp->nvram_len - len) {
  311. if (strncmp(&nvp->nvram[i], prefix, len) == 0) {
  312. i += len;
  313. if (strncmp(&nvp->nvram[i], "boardrev", 8) == 0)
  314. nvp->boardrev_found = true;
  315. while (nvp->nvram[i] != 0) {
  316. nvram[j] = nvp->nvram[i];
  317. i++;
  318. j++;
  319. }
  320. nvram[j] = 0;
  321. j++;
  322. }
  323. while (nvp->nvram[i] != 0)
  324. i++;
  325. i++;
  326. }
  327. kfree(nvp->nvram);
  328. nvp->nvram = nvram;
  329. nvp->nvram_len = j;
  330. return;
  331. fail:
  332. kfree(nvram);
  333. nvp->nvram_len = 0;
  334. }
  335. static void brcmf_fw_add_defaults(struct nvram_parser *nvp)
  336. {
  337. if (nvp->boardrev_found)
  338. return;
  339. memcpy(&nvp->nvram[nvp->nvram_len], &BRCMF_FW_DEFAULT_BOARDREV,
  340. strlen(BRCMF_FW_DEFAULT_BOARDREV));
  341. nvp->nvram_len += strlen(BRCMF_FW_DEFAULT_BOARDREV);
  342. nvp->nvram[nvp->nvram_len] = '\0';
  343. nvp->nvram_len++;
  344. }
  345. /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil
  346. * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
  347. * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
  348. * End of buffer is completed with token identifying length of buffer.
  349. */
  350. static void *brcmf_fw_nvram_strip(const u8 *data, size_t data_len,
  351. u32 *new_length, u16 domain_nr, u16 bus_nr)
  352. {
  353. struct nvram_parser nvp;
  354. u32 pad;
  355. u32 token;
  356. __le32 token_le;
  357. if (brcmf_init_nvram_parser(&nvp, data, data_len) < 0)
  358. return NULL;
  359. while (nvp.pos < data_len) {
  360. nvp.state = nv_parser_states[nvp.state](&nvp);
  361. if (nvp.state == END)
  362. break;
  363. }
  364. if (nvp.multi_dev_v1) {
  365. nvp.boardrev_found = false;
  366. brcmf_fw_strip_multi_v1(&nvp, domain_nr, bus_nr);
  367. } else if (nvp.multi_dev_v2) {
  368. nvp.boardrev_found = false;
  369. brcmf_fw_strip_multi_v2(&nvp, domain_nr, bus_nr);
  370. }
  371. if (nvp.nvram_len == 0) {
  372. kfree(nvp.nvram);
  373. return NULL;
  374. }
  375. brcmf_fw_add_defaults(&nvp);
  376. pad = nvp.nvram_len;
  377. *new_length = roundup(nvp.nvram_len + 1, 4);
  378. while (pad != *new_length) {
  379. nvp.nvram[pad] = 0;
  380. pad++;
  381. }
  382. token = *new_length / 4;
  383. token = (~token << 16) | (token & 0x0000FFFF);
  384. token_le = cpu_to_le32(token);
  385. memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le));
  386. *new_length += sizeof(token_le);
  387. return nvp.nvram;
  388. }
  389. void brcmf_fw_nvram_free(void *nvram)
  390. {
  391. kfree(nvram);
  392. }
  393. struct brcmf_fw {
  394. struct device *dev;
  395. struct brcmf_fw_request *req;
  396. u32 curpos;
  397. void (*done)(struct device *dev, int err, struct brcmf_fw_request *req);
  398. };
  399. static void brcmf_fw_request_done(const struct firmware *fw, void *ctx);
  400. static void brcmf_fw_free_request(struct brcmf_fw_request *req)
  401. {
  402. struct brcmf_fw_item *item;
  403. int i;
  404. for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) {
  405. if (item->type == BRCMF_FW_TYPE_BINARY)
  406. release_firmware(item->binary);
  407. else if (item->type == BRCMF_FW_TYPE_NVRAM)
  408. brcmf_fw_nvram_free(item->nv_data.data);
  409. }
  410. kfree(req);
  411. }
  412. static int brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx)
  413. {
  414. struct brcmf_fw *fwctx = ctx;
  415. struct brcmf_fw_item *cur;
  416. u32 nvram_length = 0;
  417. void *nvram = NULL;
  418. u8 *data = NULL;
  419. size_t data_len;
  420. bool raw_nvram;
  421. brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
  422. cur = &fwctx->req->items[fwctx->curpos];
  423. if (fw && fw->data) {
  424. data = (u8 *)fw->data;
  425. data_len = fw->size;
  426. raw_nvram = false;
  427. } else {
  428. data = bcm47xx_nvram_get_contents(&data_len);
  429. if (!data && !(cur->flags & BRCMF_FW_REQF_OPTIONAL))
  430. goto fail;
  431. raw_nvram = true;
  432. }
  433. if (data)
  434. nvram = brcmf_fw_nvram_strip(data, data_len, &nvram_length,
  435. fwctx->req->domain_nr,
  436. fwctx->req->bus_nr);
  437. if (raw_nvram)
  438. bcm47xx_nvram_release_contents(data);
  439. release_firmware(fw);
  440. if (!nvram && !(cur->flags & BRCMF_FW_REQF_OPTIONAL))
  441. goto fail;
  442. brcmf_dbg(TRACE, "nvram %p len %d\n", nvram, nvram_length);
  443. cur->nv_data.data = nvram;
  444. cur->nv_data.len = nvram_length;
  445. return 0;
  446. fail:
  447. return -ENOENT;
  448. }
  449. static int brcmf_fw_request_next_item(struct brcmf_fw *fwctx, bool async)
  450. {
  451. struct brcmf_fw_item *cur;
  452. const struct firmware *fw = NULL;
  453. int ret;
  454. cur = &fwctx->req->items[fwctx->curpos];
  455. brcmf_dbg(TRACE, "%srequest for %s\n", async ? "async " : "",
  456. cur->path);
  457. if (async)
  458. ret = request_firmware_nowait(THIS_MODULE, true, cur->path,
  459. fwctx->dev, GFP_KERNEL, fwctx,
  460. brcmf_fw_request_done);
  461. else
  462. ret = request_firmware(&fw, cur->path, fwctx->dev);
  463. if (ret < 0) {
  464. brcmf_fw_request_done(NULL, fwctx);
  465. } else if (!async && fw) {
  466. brcmf_dbg(TRACE, "firmware %s %sfound\n", cur->path,
  467. fw ? "" : "not ");
  468. if (cur->type == BRCMF_FW_TYPE_BINARY)
  469. cur->binary = fw;
  470. else if (cur->type == BRCMF_FW_TYPE_NVRAM)
  471. brcmf_fw_request_nvram_done(fw, fwctx);
  472. else
  473. release_firmware(fw);
  474. return -EAGAIN;
  475. }
  476. return 0;
  477. }
  478. static void brcmf_fw_request_done(const struct firmware *fw, void *ctx)
  479. {
  480. struct brcmf_fw *fwctx = ctx;
  481. struct brcmf_fw_item *cur;
  482. int ret = 0;
  483. cur = &fwctx->req->items[fwctx->curpos];
  484. brcmf_dbg(TRACE, "enter: firmware %s %sfound\n", cur->path,
  485. fw ? "" : "not ");
  486. if (!fw)
  487. ret = -ENOENT;
  488. switch (cur->type) {
  489. case BRCMF_FW_TYPE_NVRAM:
  490. ret = brcmf_fw_request_nvram_done(fw, fwctx);
  491. break;
  492. case BRCMF_FW_TYPE_BINARY:
  493. cur->binary = fw;
  494. break;
  495. default:
  496. /* something fishy here so bail out early */
  497. brcmf_err("unknown fw type: %d\n", cur->type);
  498. release_firmware(fw);
  499. ret = -EINVAL;
  500. goto fail;
  501. }
  502. if (ret < 0 && !(cur->flags & BRCMF_FW_REQF_OPTIONAL))
  503. goto fail;
  504. do {
  505. if (++fwctx->curpos == fwctx->req->n_items) {
  506. ret = 0;
  507. goto done;
  508. }
  509. ret = brcmf_fw_request_next_item(fwctx, false);
  510. } while (ret == -EAGAIN);
  511. return;
  512. fail:
  513. brcmf_dbg(TRACE, "failed err=%d: dev=%s, fw=%s\n", ret,
  514. dev_name(fwctx->dev), cur->path);
  515. brcmf_fw_free_request(fwctx->req);
  516. fwctx->req = NULL;
  517. done:
  518. fwctx->done(fwctx->dev, ret, fwctx->req);
  519. kfree(fwctx);
  520. }
  521. static bool brcmf_fw_request_is_valid(struct brcmf_fw_request *req)
  522. {
  523. struct brcmf_fw_item *item;
  524. int i;
  525. if (!req->n_items)
  526. return false;
  527. for (i = 0, item = &req->items[0]; i < req->n_items; i++, item++) {
  528. if (!item->path)
  529. return false;
  530. }
  531. return true;
  532. }
  533. int brcmf_fw_get_firmwares(struct device *dev, struct brcmf_fw_request *req,
  534. void (*fw_cb)(struct device *dev, int err,
  535. struct brcmf_fw_request *req))
  536. {
  537. struct brcmf_fw *fwctx;
  538. brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
  539. if (!fw_cb)
  540. return -EINVAL;
  541. if (!brcmf_fw_request_is_valid(req))
  542. return -EINVAL;
  543. fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL);
  544. if (!fwctx)
  545. return -ENOMEM;
  546. fwctx->dev = dev;
  547. fwctx->req = req;
  548. fwctx->done = fw_cb;
  549. brcmf_fw_request_next_item(fwctx, true);
  550. return 0;
  551. }
  552. struct brcmf_fw_request *
  553. brcmf_fw_alloc_request(u32 chip, u32 chiprev,
  554. const struct brcmf_firmware_mapping mapping_table[],
  555. u32 table_size, struct brcmf_fw_name *fwnames,
  556. u32 n_fwnames)
  557. {
  558. struct brcmf_fw_request *fwreq;
  559. char chipname[12];
  560. const char *mp_path;
  561. size_t mp_path_len;
  562. u32 i, j;
  563. char end = '\0';
  564. size_t reqsz;
  565. for (i = 0; i < table_size; i++) {
  566. if (mapping_table[i].chipid == chip &&
  567. mapping_table[i].revmask & BIT(chiprev))
  568. break;
  569. }
  570. if (i == table_size) {
  571. brcmf_err("Unknown chipid %d [%d]\n", chip, chiprev);
  572. return NULL;
  573. }
  574. reqsz = sizeof(*fwreq) + n_fwnames * sizeof(struct brcmf_fw_item);
  575. fwreq = kzalloc(reqsz, GFP_KERNEL);
  576. if (!fwreq)
  577. return NULL;
  578. brcmf_chip_name(chip, chiprev, chipname, sizeof(chipname));
  579. brcmf_info("using %s for chip %s\n",
  580. mapping_table[i].fw_base, chipname);
  581. mp_path = brcmf_mp_global.firmware_path;
  582. mp_path_len = strnlen(mp_path, BRCMF_FW_ALTPATH_LEN);
  583. if (mp_path_len)
  584. end = mp_path[mp_path_len - 1];
  585. fwreq->n_items = n_fwnames;
  586. for (j = 0; j < n_fwnames; j++) {
  587. fwreq->items[j].path = fwnames[j].path;
  588. /* check if firmware path is provided by module parameter */
  589. if (brcmf_mp_global.firmware_path[0] != '\0') {
  590. strlcpy(fwnames[j].path, mp_path,
  591. BRCMF_FW_NAME_LEN);
  592. if (end != '/') {
  593. strlcat(fwnames[j].path, "/",
  594. BRCMF_FW_NAME_LEN);
  595. }
  596. }
  597. strlcat(fwnames[j].path, mapping_table[i].fw_base,
  598. BRCMF_FW_NAME_LEN);
  599. strlcat(fwnames[j].path, fwnames[j].extension,
  600. BRCMF_FW_NAME_LEN);
  601. fwreq->items[j].path = fwnames[j].path;
  602. }
  603. return fwreq;
  604. }