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>
29 #define BRCMF_FW_MAX_NVRAM_SIZE 64000
30 #define BRCMF_FW_NVRAM_DEVPATH_LEN 19 /* devpath0=pcie/1/4/ */
31 #define BRCMF_FW_NVRAM_PCIEDEV_LEN 10 /* pcie/1/4/ + \0 */
32 #define BRCMF_FW_DEFAULT_BOARDREV "boardrev=0xff"
34 enum nvram_parser_state
{
43 * struct nvram_parser - internal info for parser.
45 * @state: current parser state.
46 * @data: input buffer being parsed.
47 * @nvram: output buffer with parse result.
48 * @nvram_len: lenght of parse result.
49 * @line: current line.
50 * @column: current column in line.
51 * @pos: byte offset in input buffer.
52 * @entry: start position of key,value entry.
53 * @multi_dev_v1: detect pcie multi device v1 (compressed).
54 * @multi_dev_v2: detect pcie multi device v2.
55 * @boardrev_found: nvram contains boardrev information.
58 enum nvram_parser_state state
;
72 * is_nvram_char() - check if char is a valid one for NVRAM entry
74 * It accepts all printable ASCII chars except for '#' which opens a comment.
75 * Please note that ' ' (space) while accepted is not a valid key name char.
77 static bool is_nvram_char(char c
)
79 /* comment marker excluded */
83 /* key and value may have any other readable character */
84 return (c
>= 0x20 && c
< 0x7f);
87 static bool is_whitespace(char c
)
89 return (c
== ' ' || c
== '\r' || c
== '\n' || c
== '\t');
92 static enum nvram_parser_state
brcmf_nvram_handle_idle(struct nvram_parser
*nvp
)
96 c
= nvp
->data
[nvp
->pos
];
99 if (is_whitespace(c
) || c
== '\0')
103 if (is_nvram_char(c
)) {
104 nvp
->entry
= nvp
->pos
;
107 brcmf_dbg(INFO
, "warning: ln=%d:col=%d: ignoring invalid character\n",
108 nvp
->line
, nvp
->column
);
115 static enum nvram_parser_state
brcmf_nvram_handle_key(struct nvram_parser
*nvp
)
117 enum nvram_parser_state st
= nvp
->state
;
120 c
= nvp
->data
[nvp
->pos
];
122 /* ignore RAW1 by treating as comment */
123 if (strncmp(&nvp
->data
[nvp
->entry
], "RAW1", 4) == 0)
127 if (strncmp(&nvp
->data
[nvp
->entry
], "devpath", 7) == 0)
128 nvp
->multi_dev_v1
= true;
129 if (strncmp(&nvp
->data
[nvp
->entry
], "pcie/", 5) == 0)
130 nvp
->multi_dev_v2
= true;
131 if (strncmp(&nvp
->data
[nvp
->entry
], "boardrev", 8) == 0)
132 nvp
->boardrev_found
= true;
133 } else if (!is_nvram_char(c
) || c
== ' ') {
134 brcmf_dbg(INFO
, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n",
135 nvp
->line
, nvp
->column
);
144 static enum nvram_parser_state
145 brcmf_nvram_handle_value(struct nvram_parser
*nvp
)
152 c
= nvp
->data
[nvp
->pos
];
153 if (!is_nvram_char(c
)) {
154 /* key,value pair complete */
155 ekv
= (u8
*)&nvp
->data
[nvp
->pos
];
156 skv
= (u8
*)&nvp
->data
[nvp
->entry
];
158 if (nvp
->nvram_len
+ cplen
+ 1 >= BRCMF_FW_MAX_NVRAM_SIZE
)
160 /* copy to output buffer */
161 memcpy(&nvp
->nvram
[nvp
->nvram_len
], skv
, cplen
);
162 nvp
->nvram_len
+= cplen
;
163 nvp
->nvram
[nvp
->nvram_len
] = '\0';
172 static enum nvram_parser_state
173 brcmf_nvram_handle_comment(struct nvram_parser
*nvp
)
177 sol
= (char *)&nvp
->data
[nvp
->pos
];
178 eoc
= strchr(sol
, '\n');
180 eoc
= strchr(sol
, '\0');
185 /* eat all moving to next line */
188 nvp
->pos
+= (eoc
- sol
) + 1;
192 static enum nvram_parser_state
brcmf_nvram_handle_end(struct nvram_parser
*nvp
)
198 static enum nvram_parser_state
199 (*nv_parser_states
[])(struct nvram_parser
*nvp
) = {
200 brcmf_nvram_handle_idle
,
201 brcmf_nvram_handle_key
,
202 brcmf_nvram_handle_value
,
203 brcmf_nvram_handle_comment
,
204 brcmf_nvram_handle_end
207 static int brcmf_init_nvram_parser(struct nvram_parser
*nvp
,
208 const u8
*data
, size_t data_len
)
212 memset(nvp
, 0, sizeof(*nvp
));
214 /* Limit size to MAX_NVRAM_SIZE, some files contain lot of comment */
215 if (data_len
> BRCMF_FW_MAX_NVRAM_SIZE
)
216 size
= BRCMF_FW_MAX_NVRAM_SIZE
;
219 /* Alloc for extra 0 byte + roundup by 4 + length field */
220 size
+= 1 + 3 + sizeof(u32
);
221 nvp
->nvram
= kzalloc(size
, GFP_KERNEL
);
230 /* brcmf_fw_strip_multi_v1 :Some nvram files contain settings for multiple
231 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
232 * which data is to be returned. v1 is the version where nvram is stored
233 * compressed and "devpath" maps to index for valid entries.
235 static void brcmf_fw_strip_multi_v1(struct nvram_parser
*nvp
, u16 domain_nr
,
238 /* Device path with a leading '=' key-value separator */
239 char pci_path
[] = "=pci/?/?";
241 char pcie_path
[] = "=pcie/?/?";
249 nvram
= kzalloc(nvp
->nvram_len
+ 1 + 3 + sizeof(u32
), GFP_KERNEL
);
253 /* min length: devpath0=pcie/1/4/ + 0:x=y */
254 if (nvp
->nvram_len
< BRCMF_FW_NVRAM_DEVPATH_LEN
+ 6)
257 /* First search for the devpathX and see if it is the configuration
258 * for domain_nr/bus_nr. Search complete nvp
260 snprintf(pci_path
, sizeof(pci_path
), "=pci/%d/%d", domain_nr
,
262 pci_len
= strlen(pci_path
);
263 snprintf(pcie_path
, sizeof(pcie_path
), "=pcie/%d/%d", domain_nr
,
265 pcie_len
= strlen(pcie_path
);
268 while (i
< nvp
->nvram_len
- BRCMF_FW_NVRAM_DEVPATH_LEN
) {
269 /* Format: devpathX=pcie/Y/Z/
270 * Y = domain_nr, Z = bus_nr, X = virtual ID
272 if (strncmp(&nvp
->nvram
[i
], "devpath", 7) == 0 &&
273 (!strncmp(&nvp
->nvram
[i
+ 8], pci_path
, pci_len
) ||
274 !strncmp(&nvp
->nvram
[i
+ 8], pcie_path
, pcie_len
))) {
275 id
= nvp
->nvram
[i
+ 7] - '0';
279 while (nvp
->nvram
[i
] != 0)
286 /* Now copy all valid entries, release old nvram and assign new one */
289 while (i
< nvp
->nvram_len
) {
290 if ((nvp
->nvram
[i
] - '0' == id
) && (nvp
->nvram
[i
+ 1] == ':')) {
292 if (strncmp(&nvp
->nvram
[i
], "boardrev", 8) == 0)
293 nvp
->boardrev_found
= true;
294 while (nvp
->nvram
[i
] != 0) {
295 nvram
[j
] = nvp
->nvram
[i
];
302 while (nvp
->nvram
[i
] != 0)
316 /* brcmf_fw_strip_multi_v2 :Some nvram files contain settings for multiple
317 * devices. Strip it down for one device, use domain_nr/bus_nr to determine
318 * which data is to be returned. v2 is the version where nvram is stored
319 * uncompressed, all relevant valid entries are identified by
320 * pcie/domain_nr/bus_nr:
322 static void brcmf_fw_strip_multi_v2(struct nvram_parser
*nvp
, u16 domain_nr
,
325 char prefix
[BRCMF_FW_NVRAM_PCIEDEV_LEN
];
330 nvram
= kzalloc(nvp
->nvram_len
+ 1 + 3 + sizeof(u32
), GFP_KERNEL
);
334 /* Copy all valid entries, release old nvram and assign new one.
335 * Valid entries are of type pcie/X/Y/ where X = domain_nr and
338 snprintf(prefix
, sizeof(prefix
), "pcie/%d/%d/", domain_nr
, bus_nr
);
339 len
= strlen(prefix
);
342 while (i
< nvp
->nvram_len
- len
) {
343 if (strncmp(&nvp
->nvram
[i
], prefix
, len
) == 0) {
345 if (strncmp(&nvp
->nvram
[i
], "boardrev", 8) == 0)
346 nvp
->boardrev_found
= true;
347 while (nvp
->nvram
[i
] != 0) {
348 nvram
[j
] = nvp
->nvram
[i
];
355 while (nvp
->nvram
[i
] != 0)
368 static void brcmf_fw_add_defaults(struct nvram_parser
*nvp
)
370 if (nvp
->boardrev_found
)
373 memcpy(&nvp
->nvram
[nvp
->nvram_len
], &BRCMF_FW_DEFAULT_BOARDREV
,
374 strlen(BRCMF_FW_DEFAULT_BOARDREV
));
375 nvp
->nvram_len
+= strlen(BRCMF_FW_DEFAULT_BOARDREV
);
376 nvp
->nvram
[nvp
->nvram_len
] = '\0';
380 /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil
381 * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
382 * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
383 * End of buffer is completed with token identifying length of buffer.
385 static void *brcmf_fw_nvram_strip(const u8
*data
, size_t data_len
,
386 u32
*new_length
, u16 domain_nr
, u16 bus_nr
)
388 struct nvram_parser nvp
;
393 if (brcmf_init_nvram_parser(&nvp
, data
, data_len
) < 0)
396 while (nvp
.pos
< data_len
) {
397 nvp
.state
= nv_parser_states
[nvp
.state
](&nvp
);
398 if (nvp
.state
== END
)
401 if (nvp
.multi_dev_v1
) {
402 nvp
.boardrev_found
= false;
403 brcmf_fw_strip_multi_v1(&nvp
, domain_nr
, bus_nr
);
404 } else if (nvp
.multi_dev_v2
) {
405 nvp
.boardrev_found
= false;
406 brcmf_fw_strip_multi_v2(&nvp
, domain_nr
, bus_nr
);
409 if (nvp
.nvram_len
== 0) {
414 brcmf_fw_add_defaults(&nvp
);
417 *new_length
= roundup(nvp
.nvram_len
+ 1, 4);
418 while (pad
!= *new_length
) {
423 token
= *new_length
/ 4;
424 token
= (~token
<< 16) | (token
& 0x0000FFFF);
425 token_le
= cpu_to_le32(token
);
427 memcpy(&nvp
.nvram
[*new_length
], &token_le
, sizeof(token_le
));
428 *new_length
+= sizeof(token_le
);
433 void brcmf_fw_nvram_free(void *nvram
)
441 const struct firmware
*code
;
442 const char *nvram_name
;
445 void (*done
)(struct device
*dev
, int err
, const struct firmware
*fw
,
446 void *nvram_image
, u32 nvram_len
);
449 static void brcmf_fw_request_nvram_done(const struct firmware
*fw
, void *ctx
)
451 struct brcmf_fw
*fwctx
= ctx
;
452 u32 nvram_length
= 0;
458 brcmf_dbg(TRACE
, "enter: dev=%s\n", dev_name(fwctx
->dev
));
459 if (fw
&& fw
->data
) {
460 data
= (u8
*)fw
->data
;
464 data
= bcm47xx_nvram_get_contents(&data_len
);
465 if (!data
&& !(fwctx
->flags
& BRCMF_FW_REQ_NV_OPTIONAL
))
471 nvram
= brcmf_fw_nvram_strip(data
, data_len
, &nvram_length
,
472 fwctx
->domain_nr
, fwctx
->bus_nr
);
475 bcm47xx_nvram_release_contents(data
);
476 release_firmware(fw
);
477 if (!nvram
&& !(fwctx
->flags
& BRCMF_FW_REQ_NV_OPTIONAL
))
480 fwctx
->done(fwctx
->dev
, 0, fwctx
->code
, nvram
, nvram_length
);
485 brcmf_dbg(TRACE
, "failed: dev=%s\n", dev_name(fwctx
->dev
));
486 release_firmware(fwctx
->code
);
487 fwctx
->done(fwctx
->dev
, -ENOENT
, NULL
, NULL
, 0);
491 static void brcmf_fw_request_code_done(const struct firmware
*fw
, void *ctx
)
493 struct brcmf_fw
*fwctx
= ctx
;
496 brcmf_dbg(TRACE
, "enter: dev=%s\n", dev_name(fwctx
->dev
));
501 /* only requested code so done here */
502 if (!(fwctx
->flags
& BRCMF_FW_REQUEST_NVRAM
))
506 ret
= request_firmware_nowait(THIS_MODULE
, true, fwctx
->nvram_name
,
507 fwctx
->dev
, GFP_KERNEL
, fwctx
,
508 brcmf_fw_request_nvram_done
);
510 /* pass NULL to nvram callback for bcm47xx fallback */
512 brcmf_fw_request_nvram_done(NULL
, fwctx
);
516 brcmf_dbg(TRACE
, "failed: dev=%s\n", dev_name(fwctx
->dev
));
518 fwctx
->done(fwctx
->dev
, ret
, fw
, NULL
, 0);
522 int brcmf_fw_get_firmwares_pcie(struct device
*dev
, u16 flags
,
523 const char *code
, const char *nvram
,
524 void (*fw_cb
)(struct device
*dev
, int err
,
525 const struct firmware
*fw
,
526 void *nvram_image
, u32 nvram_len
),
527 u16 domain_nr
, u16 bus_nr
)
529 struct brcmf_fw
*fwctx
;
531 brcmf_dbg(TRACE
, "enter: dev=%s\n", dev_name(dev
));
535 if ((flags
& BRCMF_FW_REQUEST_NVRAM
) && !nvram
)
538 fwctx
= kzalloc(sizeof(*fwctx
), GFP_KERNEL
);
543 fwctx
->flags
= flags
;
545 if (flags
& BRCMF_FW_REQUEST_NVRAM
)
546 fwctx
->nvram_name
= nvram
;
547 fwctx
->domain_nr
= domain_nr
;
548 fwctx
->bus_nr
= bus_nr
;
550 return request_firmware_nowait(THIS_MODULE
, true, code
, dev
,
552 brcmf_fw_request_code_done
);
555 int brcmf_fw_get_firmwares(struct device
*dev
, u16 flags
,
556 const char *code
, const char *nvram
,
557 void (*fw_cb
)(struct device
*dev
, int err
,
558 const struct firmware
*fw
,
559 void *nvram_image
, u32 nvram_len
))
561 return brcmf_fw_get_firmwares_pcie(dev
, flags
, code
, nvram
, fw_cb
, 0,
565 int brcmf_fw_map_chip_to_name(u32 chip
, u32 chiprev
,
566 struct brcmf_firmware_mapping mapping_table
[],
567 u32 table_size
, char fw_name
[BRCMF_FW_NAME_LEN
],
568 char nvram_name
[BRCMF_FW_NAME_LEN
])
573 for (i
= 0; i
< table_size
; i
++) {
574 if (mapping_table
[i
].chipid
== chip
&&
575 mapping_table
[i
].revmask
& BIT(chiprev
))
579 if (i
== table_size
) {
580 brcmf_err("Unknown chipid %d [%d]\n", chip
, chiprev
);
584 /* check if firmware path is provided by module parameter */
585 if (brcmf_mp_global
.firmware_path
[0] != '\0') {
586 strlcpy(fw_name
, brcmf_mp_global
.firmware_path
,
588 if ((nvram_name
) && (mapping_table
[i
].nvram
))
589 strlcpy(nvram_name
, brcmf_mp_global
.firmware_path
,
592 end
= brcmf_mp_global
.firmware_path
[
593 strlen(brcmf_mp_global
.firmware_path
) - 1];
595 strlcat(fw_name
, "/", BRCMF_FW_NAME_LEN
);
596 if ((nvram_name
) && (mapping_table
[i
].nvram
))
597 strlcat(nvram_name
, "/", BRCMF_FW_NAME_LEN
);
600 strlcat(fw_name
, mapping_table
[i
].fw
, BRCMF_FW_NAME_LEN
);
601 if ((nvram_name
) && (mapping_table
[i
].nvram
))
602 strlcat(nvram_name
, mapping_table
[i
].nvram
, BRCMF_FW_NAME_LEN
);
604 brcmf_info("using %s for chip %#08x(%d) rev %#08x\n",
605 fw_name
, chip
, chip
, chiprev
);