2 * Copyright (c) 2013 Broadcom Corporation
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.
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.
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/firmware.h>
21 #include <linux/module.h>
22 #include <linux/bcm47xx_nvram.h>
30 #define BRCMF_FW_MAX_NVRAM_SIZE 64000
31 #define BRCMF_FW_NVRAM_DEVPATH_LEN 19 /* devpath0=pcie/1/4/ */
32 #define BRCMF_FW_NVRAM_PCIEDEV_LEN 10 /* pcie/1/4/ + \0 */
33 #define BRCMF_FW_DEFAULT_BOARDREV "boardrev=0xff"
35 enum nvram_parser_state
{
44 * struct nvram_parser - internal info for parser.
46 * @state: current parser state.
47 * @data: input buffer being parsed.
48 * @nvram: output buffer with parse result.
49 * @nvram_len: lenght of parse result.
50 * @line: current line.
51 * @column: current column in line.
52 * @pos: byte offset in input buffer.
53 * @entry: start position of key,value entry.
54 * @multi_dev_v1: detect pcie multi device v1 (compressed).
55 * @multi_dev_v2: detect pcie multi device v2.
56 * @boardrev_found: nvram contains boardrev information.
59 enum nvram_parser_state state
;
73 * is_nvram_char() - check if char is a valid one for NVRAM entry
75 * It accepts all printable ASCII chars except for '#' which opens a comment.
76 * Please note that ' ' (space) while accepted is not a valid key name char.
78 static bool is_nvram_char(char c
)
80 /* comment marker excluded */
84 /* key and value may have any other readable character */
85 return (c
>= 0x20 && c
< 0x7f);
88 static bool is_whitespace(char c
)
90 return (c
== ' ' || c
== '\r' || c
== '\n' || c
== '\t');
93 static enum nvram_parser_state
brcmf_nvram_handle_idle(struct nvram_parser
*nvp
)
97 c
= nvp
->data
[nvp
->pos
];
100 if (is_whitespace(c
) || c
== '\0')
104 if (is_nvram_char(c
)) {
105 nvp
->entry
= nvp
->pos
;
108 brcmf_dbg(INFO
, "warning: ln=%d:col=%d: ignoring invalid character\n",
109 nvp
->line
, nvp
->column
);
116 static enum nvram_parser_state
brcmf_nvram_handle_key(struct nvram_parser
*nvp
)
118 enum nvram_parser_state st
= nvp
->state
;
121 c
= nvp
->data
[nvp
->pos
];
123 /* ignore RAW1 by treating as comment */
124 if (strncmp(&nvp
->data
[nvp
->entry
], "RAW1", 4) == 0)
128 if (strncmp(&nvp
->data
[nvp
->entry
], "devpath", 7) == 0)
129 nvp
->multi_dev_v1
= true;
130 if (strncmp(&nvp
->data
[nvp
->entry
], "pcie/", 5) == 0)
131 nvp
->multi_dev_v2
= true;
132 if (strncmp(&nvp
->data
[nvp
->entry
], "boardrev", 8) == 0)
133 nvp
->boardrev_found
= true;
134 } else if (!is_nvram_char(c
) || c
== ' ') {
135 brcmf_dbg(INFO
, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n",
136 nvp
->line
, nvp
->column
);
145 static enum nvram_parser_state
146 brcmf_nvram_handle_value(struct nvram_parser
*nvp
)
153 c
= nvp
->data
[nvp
->pos
];
154 if (!is_nvram_char(c
)) {
155 /* key,value pair complete */
156 ekv
= (u8
*)&nvp
->data
[nvp
->pos
];
157 skv
= (u8
*)&nvp
->data
[nvp
->entry
];
159 if (nvp
->nvram_len
+ cplen
+ 1 >= BRCMF_FW_MAX_NVRAM_SIZE
)
161 /* copy to output buffer */
162 memcpy(&nvp
->nvram
[nvp
->nvram_len
], skv
, cplen
);
163 nvp
->nvram_len
+= cplen
;
164 nvp
->nvram
[nvp
->nvram_len
] = '\0';
173 static enum nvram_parser_state
174 brcmf_nvram_handle_comment(struct nvram_parser
*nvp
)
178 sol
= (char *)&nvp
->data
[nvp
->pos
];
179 eoc
= strchr(sol
, '\n');
181 eoc
= strchr(sol
, '\0');
186 /* eat all moving to next line */
189 nvp
->pos
+= (eoc
- sol
) + 1;
193 static enum nvram_parser_state
brcmf_nvram_handle_end(struct nvram_parser
*nvp
)
199 static enum nvram_parser_state
200 (*nv_parser_states
[])(struct nvram_parser
*nvp
) = {
201 brcmf_nvram_handle_idle
,
202 brcmf_nvram_handle_key
,
203 brcmf_nvram_handle_value
,
204 brcmf_nvram_handle_comment
,
205 brcmf_nvram_handle_end
208 static int brcmf_init_nvram_parser(struct nvram_parser
*nvp
,
209 const u8
*data
, size_t data_len
)
213 memset(nvp
, 0, sizeof(*nvp
));
215 /* Limit size to MAX_NVRAM_SIZE, some files contain lot of comment */
216 if (data_len
> BRCMF_FW_MAX_NVRAM_SIZE
)
217 size
= BRCMF_FW_MAX_NVRAM_SIZE
;
220 /* Alloc for extra 0 byte + roundup by 4 + length field */
221 size
+= 1 + 3 + sizeof(u32
);
222 nvp
->nvram
= kzalloc(size
, GFP_KERNEL
);
231 /* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple
232 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
233 * which data is to be returned. v1 is the version where nvram is stored
234 * compressed and "devpath" maps to index for valid entries.
236 static void brcmf_fw_strip_multi_v1(struct nvram_parser
*nvp
, u16 domain_nr
,
239 /* Device path with a leading '=' key-value separator */
240 char pci_path
[] = "=pci/?/?";
242 char pcie_path
[] = "=pcie/?/?";
250 nvram
= kzalloc(nvp
->nvram_len
+ 1 + 3 + sizeof(u32
), GFP_KERNEL
);
254 /* min length: devpath0=pcie/1/4/ + 0:x=y */
255 if (nvp
->nvram_len
< BRCMF_FW_NVRAM_DEVPATH_LEN
+ 6)
258 /* First search for the devpathX and see if it is the configuration
259 * for domain_nr/bus_nr. Search complete nvp
261 snprintf(pci_path
, sizeof(pci_path
), "=pci/%d/%d", domain_nr
,
263 pci_len
= strlen(pci_path
);
264 snprintf(pcie_path
, sizeof(pcie_path
), "=pcie/%d/%d", domain_nr
,
266 pcie_len
= strlen(pcie_path
);
269 while (i
< nvp
->nvram_len
- BRCMF_FW_NVRAM_DEVPATH_LEN
) {
270 /* Format: devpathX=pcie/Y/Z/
271 * Y = domain_nr, Z = bus_nr, X = virtual ID
273 if (strncmp(&nvp
->nvram
[i
], "devpath", 7) == 0 &&
274 (!strncmp(&nvp
->nvram
[i
+ 8], pci_path
, pci_len
) ||
275 !strncmp(&nvp
->nvram
[i
+ 8], pcie_path
, pcie_len
))) {
276 id
= nvp
->nvram
[i
+ 7] - '0';
280 while (nvp
->nvram
[i
] != 0)
287 /* Now copy all valid entries, release old nvram and assign new one */
290 while (i
< nvp
->nvram_len
) {
291 if ((nvp
->nvram
[i
] - '0' == id
) && (nvp
->nvram
[i
+ 1] == ':')) {
293 if (strncmp(&nvp
->nvram
[i
], "boardrev", 8) == 0)
294 nvp
->boardrev_found
= true;
295 while (nvp
->nvram
[i
] != 0) {
296 nvram
[j
] = nvp
->nvram
[i
];
303 while (nvp
->nvram
[i
] != 0)
317 /* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple
318 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
319 * which data is to be returned. v2 is the version where nvram is stored
320 * uncompressed, all relevant valid entries are identified by
321 * pcie/domain_nr/bus_nr:
323 static void brcmf_fw_strip_multi_v2(struct nvram_parser
*nvp
, u16 domain_nr
,
326 char prefix
[BRCMF_FW_NVRAM_PCIEDEV_LEN
];
331 nvram
= kzalloc(nvp
->nvram_len
+ 1 + 3 + sizeof(u32
), GFP_KERNEL
);
335 /* Copy all valid entries, release old nvram and assign new one.
336 * Valid entries are of type pcie/X/Y/ where X = domain_nr and
339 snprintf(prefix
, sizeof(prefix
), "pcie/%d/%d/", domain_nr
, bus_nr
);
340 len
= strlen(prefix
);
343 while (i
< nvp
->nvram_len
- len
) {
344 if (strncmp(&nvp
->nvram
[i
], prefix
, len
) == 0) {
346 if (strncmp(&nvp
->nvram
[i
], "boardrev", 8) == 0)
347 nvp
->boardrev_found
= true;
348 while (nvp
->nvram
[i
] != 0) {
349 nvram
[j
] = nvp
->nvram
[i
];
356 while (nvp
->nvram
[i
] != 0)
369 static void brcmf_fw_add_defaults(struct nvram_parser
*nvp
)
371 if (nvp
->boardrev_found
)
374 memcpy(&nvp
->nvram
[nvp
->nvram_len
], &BRCMF_FW_DEFAULT_BOARDREV
,
375 strlen(BRCMF_FW_DEFAULT_BOARDREV
));
376 nvp
->nvram_len
+= strlen(BRCMF_FW_DEFAULT_BOARDREV
);
377 nvp
->nvram
[nvp
->nvram_len
] = '\0';
381 /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil
382 * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
383 * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
384 * End of buffer is completed with token identifying length of buffer.
386 static void *brcmf_fw_nvram_strip(const u8
*data
, size_t data_len
,
387 u32
*new_length
, u16 domain_nr
, u16 bus_nr
)
389 struct nvram_parser nvp
;
394 if (brcmf_init_nvram_parser(&nvp
, data
, data_len
) < 0)
397 while (nvp
.pos
< data_len
) {
398 nvp
.state
= nv_parser_states
[nvp
.state
](&nvp
);
399 if (nvp
.state
== END
)
402 if (nvp
.multi_dev_v1
) {
403 nvp
.boardrev_found
= false;
404 brcmf_fw_strip_multi_v1(&nvp
, domain_nr
, bus_nr
);
405 } else if (nvp
.multi_dev_v2
) {
406 nvp
.boardrev_found
= false;
407 brcmf_fw_strip_multi_v2(&nvp
, domain_nr
, bus_nr
);
410 if (nvp
.nvram_len
== 0) {
415 brcmf_fw_add_defaults(&nvp
);
418 *new_length
= roundup(nvp
.nvram_len
+ 1, 4);
419 while (pad
!= *new_length
) {
424 token
= *new_length
/ 4;
425 token
= (~token
<< 16) | (token
& 0x0000FFFF);
426 token_le
= cpu_to_le32(token
);
428 memcpy(&nvp
.nvram
[*new_length
], &token_le
, sizeof(token_le
));
429 *new_length
+= sizeof(token_le
);
434 void brcmf_fw_nvram_free(void *nvram
)
441 struct brcmf_fw_request
*req
;
443 void (*done
)(struct device
*dev
, int err
, struct brcmf_fw_request
*req
);
446 static void brcmf_fw_request_done(const struct firmware
*fw
, void *ctx
);
448 static void brcmf_fw_free_request(struct brcmf_fw_request
*req
)
450 struct brcmf_fw_item
*item
;
453 for (i
= 0, item
= &req
->items
[0]; i
< req
->n_items
; i
++, item
++) {
454 if (item
->type
== BRCMF_FW_TYPE_BINARY
)
455 release_firmware(item
->binary
);
456 else if (item
->type
== BRCMF_FW_TYPE_NVRAM
)
457 brcmf_fw_nvram_free(item
->nv_data
.data
);
462 static int brcmf_fw_request_nvram_done(const struct firmware
*fw
, void *ctx
)
464 struct brcmf_fw
*fwctx
= ctx
;
465 struct brcmf_fw_item
*cur
;
466 u32 nvram_length
= 0;
472 brcmf_dbg(TRACE
, "enter: dev=%s\n", dev_name(fwctx
->dev
));
474 cur
= &fwctx
->req
->items
[fwctx
->curpos
];
476 if (fw
&& fw
->data
) {
477 data
= (u8
*)fw
->data
;
481 data
= bcm47xx_nvram_get_contents(&data_len
);
482 if (!data
&& !(cur
->flags
& BRCMF_FW_REQF_OPTIONAL
))
488 nvram
= brcmf_fw_nvram_strip(data
, data_len
, &nvram_length
,
489 fwctx
->req
->domain_nr
,
493 bcm47xx_nvram_release_contents(data
);
494 release_firmware(fw
);
495 if (!nvram
&& !(cur
->flags
& BRCMF_FW_REQF_OPTIONAL
))
498 brcmf_dbg(TRACE
, "nvram %p len %d\n", nvram
, nvram_length
);
499 cur
->nv_data
.data
= nvram
;
500 cur
->nv_data
.len
= nvram_length
;
507 static int brcmf_fw_request_next_item(struct brcmf_fw
*fwctx
, bool async
)
509 struct brcmf_fw_item
*cur
;
510 const struct firmware
*fw
= NULL
;
513 cur
= &fwctx
->req
->items
[fwctx
->curpos
];
515 brcmf_dbg(TRACE
, "%srequest for %s\n", async
? "async " : "",
519 ret
= request_firmware_nowait(THIS_MODULE
, true, cur
->path
,
520 fwctx
->dev
, GFP_KERNEL
, fwctx
,
521 brcmf_fw_request_done
);
523 ret
= request_firmware(&fw
, cur
->path
, fwctx
->dev
);
526 brcmf_fw_request_done(NULL
, fwctx
);
527 } else if (!async
&& fw
) {
528 brcmf_dbg(TRACE
, "firmware %s %sfound\n", cur
->path
,
530 if (cur
->type
== BRCMF_FW_TYPE_BINARY
)
532 else if (cur
->type
== BRCMF_FW_TYPE_NVRAM
)
533 brcmf_fw_request_nvram_done(fw
, fwctx
);
535 release_firmware(fw
);
542 static void brcmf_fw_request_done(const struct firmware
*fw
, void *ctx
)
544 struct brcmf_fw
*fwctx
= ctx
;
545 struct brcmf_fw_item
*cur
;
548 cur
= &fwctx
->req
->items
[fwctx
->curpos
];
550 brcmf_dbg(TRACE
, "enter: firmware %s %sfound\n", cur
->path
,
557 case BRCMF_FW_TYPE_NVRAM
:
558 ret
= brcmf_fw_request_nvram_done(fw
, fwctx
);
560 case BRCMF_FW_TYPE_BINARY
:
564 /* something fishy here so bail out early */
565 brcmf_err("unknown fw type: %d\n", cur
->type
);
566 release_firmware(fw
);
571 if (ret
< 0 && !(cur
->flags
& BRCMF_FW_REQF_OPTIONAL
))
575 if (++fwctx
->curpos
== fwctx
->req
->n_items
) {
580 ret
= brcmf_fw_request_next_item(fwctx
, false);
581 } while (ret
== -EAGAIN
);
586 brcmf_dbg(TRACE
, "failed err=%d: dev=%s, fw=%s\n", ret
,
587 dev_name(fwctx
->dev
), cur
->path
);
588 brcmf_fw_free_request(fwctx
->req
);
591 fwctx
->done(fwctx
->dev
, ret
, fwctx
->req
);
595 static bool brcmf_fw_request_is_valid(struct brcmf_fw_request
*req
)
597 struct brcmf_fw_item
*item
;
603 for (i
= 0, item
= &req
->items
[0]; i
< req
->n_items
; i
++, item
++) {
610 int brcmf_fw_get_firmwares(struct device
*dev
, struct brcmf_fw_request
*req
,
611 void (*fw_cb
)(struct device
*dev
, int err
,
612 struct brcmf_fw_request
*req
))
614 struct brcmf_fw
*fwctx
;
616 brcmf_dbg(TRACE
, "enter: dev=%s\n", dev_name(dev
));
620 if (!brcmf_fw_request_is_valid(req
))
623 fwctx
= kzalloc(sizeof(*fwctx
), GFP_KERNEL
);
631 brcmf_fw_request_next_item(fwctx
, true);
635 struct brcmf_fw_request
*
636 brcmf_fw_alloc_request(u32 chip
, u32 chiprev
,
637 const struct brcmf_firmware_mapping mapping_table
[],
638 u32 table_size
, struct brcmf_fw_name
*fwnames
,
641 struct brcmf_fw_request
*fwreq
;
649 for (i
= 0; i
< table_size
; i
++) {
650 if (mapping_table
[i
].chipid
== chip
&&
651 mapping_table
[i
].revmask
& BIT(chiprev
))
655 if (i
== table_size
) {
656 brcmf_err("Unknown chipid %d [%d]\n", chip
, chiprev
);
660 reqsz
= sizeof(*fwreq
) + n_fwnames
* sizeof(struct brcmf_fw_item
);
661 fwreq
= kzalloc(reqsz
, GFP_KERNEL
);
665 brcmf_chip_name(chip
, chiprev
, chipname
, sizeof(chipname
));
667 brcmf_info("using %s for chip %s\n",
668 mapping_table
[i
].fw_base
, chipname
);
670 mp_path
= brcmf_mp_global
.firmware_path
;
671 mp_path_len
= strnlen(mp_path
, BRCMF_FW_ALTPATH_LEN
);
673 end
= mp_path
[mp_path_len
- 1];
675 fwreq
->n_items
= n_fwnames
;
677 for (j
= 0; j
< n_fwnames
; j
++) {
678 fwreq
->items
[j
].path
= fwnames
[j
].path
;
679 /* check if firmware path is provided by module parameter */
680 if (brcmf_mp_global
.firmware_path
[0] != '\0') {
681 strlcpy(fwnames
[j
].path
, mp_path
,
685 strlcat(fwnames
[j
].path
, "/",
689 strlcat(fwnames
[j
].path
, mapping_table
[i
].fw_base
,
691 strlcat(fwnames
[j
].path
, fwnames
[j
].extension
,
693 fwreq
->items
[j
].path
= fwnames
[j
].path
;