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>
26 char brcmf_firmware_path[BRCMF_FW_PATH_LEN];
27 module_param_string(firmware_path, brcmf_firmware_path,
28 BRCMF_FW_PATH_LEN, 0440);
30 enum nvram_parser_state {
39 * struct nvram_parser - internal info for parser.
41 * @state: current parser state.
42 * @fwnv: 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.
51 enum nvram_parser_state state;
52 const struct firmware *fwnv;
61 static bool is_nvram_char(char c)
63 /* comment marker excluded */
67 /* key and value may have any other readable character */
68 return (c > 0x20 && c < 0x7f);
71 static bool is_whitespace(char c)
73 return (c == ' ' || c == '\r' || c == '\n' || c == '\t');
76 static enum nvram_parser_state brcmf_nvram_handle_idle(struct nvram_parser *nvp)
80 c = nvp->fwnv->data[nvp->pos];
87 if (is_nvram_char(c)) {
88 nvp->entry = nvp->pos;
91 brcmf_dbg(INFO, "warning: ln=%d:col=%d: ignoring invalid character\n",
92 nvp->line, nvp->column);
99 static enum nvram_parser_state brcmf_nvram_handle_key(struct nvram_parser *nvp)
101 enum nvram_parser_state st = nvp->state;
104 c = nvp->fwnv->data[nvp->pos];
107 } else if (!is_nvram_char(c)) {
108 brcmf_dbg(INFO, "warning: ln=%d:col=%d: '=' expected, skip invalid key entry\n",
109 nvp->line, nvp->column);
118 static enum nvram_parser_state
119 brcmf_nvram_handle_value(struct nvram_parser *nvp)
126 c = nvp->fwnv->data[nvp->pos];
127 if (!is_nvram_char(c)) {
128 /* key,value pair complete */
129 ekv = (u8 *)&nvp->fwnv->data[nvp->pos];
130 skv = (u8 *)&nvp->fwnv->data[nvp->entry];
132 /* copy to output buffer */
133 memcpy(&nvp->nvram[nvp->nvram_len], skv, cplen);
134 nvp->nvram_len += cplen;
135 nvp->nvram[nvp->nvram_len] = '\0';
144 static enum nvram_parser_state
145 brcmf_nvram_handle_comment(struct nvram_parser *nvp)
149 sol = (char *)&nvp->fwnv->data[nvp->pos];
150 eol = strchr(sol, '\n');
154 /* eat all moving to next line */
157 nvp->pos += (eol - sol) + 1;
161 static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp)
167 static enum nvram_parser_state
168 (*nv_parser_states[])(struct nvram_parser *nvp) = {
169 brcmf_nvram_handle_idle,
170 brcmf_nvram_handle_key,
171 brcmf_nvram_handle_value,
172 brcmf_nvram_handle_comment,
173 brcmf_nvram_handle_end
176 static int brcmf_init_nvram_parser(struct nvram_parser *nvp,
177 const struct firmware *nv)
179 memset(nvp, 0, sizeof(*nvp));
181 /* Alloc for extra 0 byte + roundup by 4 + length field */
182 nvp->nvram = kzalloc(nv->size + 1 + 3 + sizeof(u32), GFP_KERNEL);
191 /* brcmf_nvram_strip :Takes a buffer of "<var>=<value>\n" lines read from a fil
192 * and ending in a NUL. Removes carriage returns, empty lines, comment lines,
193 * and converts newlines to NULs. Shortens buffer as needed and pads with NULs.
194 * End of buffer is completed with token identifying length of buffer.
196 static void *brcmf_fw_nvram_strip(const struct firmware *nv, u32 *new_length)
198 struct nvram_parser nvp;
203 if (brcmf_init_nvram_parser(&nvp, nv) < 0)
206 while (nvp.pos < nv->size) {
207 nvp.state = nv_parser_states[nvp.state](&nvp);
208 if (nvp.state == END)
212 *new_length = roundup(nvp.nvram_len + 1, 4);
213 while (pad != *new_length) {
218 token = *new_length / 4;
219 token = (~token << 16) | (token & 0x0000FFFF);
220 token_le = cpu_to_le32(token);
222 memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le));
223 *new_length += sizeof(token_le);
228 void brcmf_fw_nvram_free(void *nvram)
236 const struct firmware *code;
237 const char *nvram_name;
238 void (*done)(struct device *dev, const struct firmware *fw,
239 void *nvram_image, u32 nvram_len);
242 static void brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx)
244 struct brcmf_fw *fwctx = ctx;
245 u32 nvram_length = 0;
248 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
249 if (!fw && !(fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL))
253 nvram = brcmf_fw_nvram_strip(fw, &nvram_length);
254 release_firmware(fw);
255 if (!nvram && !(fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL))
259 fwctx->done(fwctx->dev, fwctx->code, nvram, nvram_length);
264 brcmf_dbg(TRACE, "failed: dev=%s\n", dev_name(fwctx->dev));
266 release_firmware(fwctx->code);
267 device_release_driver(fwctx->dev);
271 static void brcmf_fw_request_code_done(const struct firmware *fw, void *ctx)
273 struct brcmf_fw *fwctx = ctx;
276 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
280 /* only requested code so done here */
281 if (!(fwctx->flags & BRCMF_FW_REQUEST_NVRAM)) {
282 fwctx->done(fwctx->dev, fw, NULL, 0);
287 ret = request_firmware_nowait(THIS_MODULE, true, fwctx->nvram_name,
288 fwctx->dev, GFP_KERNEL, fwctx,
289 brcmf_fw_request_nvram_done);
294 /* when nvram is optional call .done() callback here */
295 if (fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL) {
296 fwctx->done(fwctx->dev, fw, NULL, 0);
301 /* failed nvram request */
302 release_firmware(fw);
304 brcmf_dbg(TRACE, "failed: dev=%s\n", dev_name(fwctx->dev));
305 device_release_driver(fwctx->dev);
309 int brcmf_fw_get_firmwares(struct device *dev, u16 flags,
310 const char *code, const char *nvram,
311 void (*fw_cb)(struct device *dev,
312 const struct firmware *fw,
313 void *nvram_image, u32 nvram_len))
315 struct brcmf_fw *fwctx;
317 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
321 if ((flags & BRCMF_FW_REQUEST_NVRAM) && !nvram)
324 fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL);
329 fwctx->flags = flags;
331 if (flags & BRCMF_FW_REQUEST_NVRAM)
332 fwctx->nvram_name = nvram;
334 return request_firmware_nowait(THIS_MODULE, true, code, dev,
336 brcmf_fw_request_code_done);