]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / firmware.c
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
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>
23
24 #include "debug.h"
25 #include "firmware.h"
26 #include "core.h"
27 #include "common.h"
28
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"
33
34 enum nvram_parser_state {
35 IDLE,
36 KEY,
37 VALUE,
38 COMMENT,
39 END
40 };
41
42 /**
43 * struct nvram_parser - internal info for parser.
44 *
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.
56 */
57 struct nvram_parser {
58 enum nvram_parser_state state;
59 const u8 *data;
60 u8 *nvram;
61 u32 nvram_len;
62 u32 line;
63 u32 column;
64 u32 pos;
65 u32 entry;
66 bool multi_dev_v1;
67 bool multi_dev_v2;
68 bool boardrev_found;
69 };
70
71 /**
72 * is_nvram_char() - check if char is a valid one for NVRAM entry
73 *
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.
76 */
77 static bool is_nvram_char(char c)
78 {
79 /* comment marker excluded */
80 if (c == '#')
81 return false;
82
83 /* key and value may have any other readable character */
84 return (c >= 0x20 && c < 0x7f);
85 }
86
87 static bool is_whitespace(char c)
88 {
89 return (c == ' ' || c == '\r' || c == '\n' || c == '\t');
90 }
91
92 static enum nvram_parser_state brcmf_nvram_handle_idle(struct nvram_parser *nvp)
93 {
94 char c;
95
96 c = nvp->data[nvp->pos];
97 if (c == '\n')
98 return COMMENT;
99 if (is_whitespace(c) || c == '\0')
100 goto proceed;
101 if (c == '#')
102 return COMMENT;
103 if (is_nvram_char(c)) {
104 nvp->entry = nvp->pos;
105 return KEY;
106 }
107 brcmf_dbg(INFO, "warning: ln=%d:col=%d: ignoring invalid character\n",
108 nvp->line, nvp->column);
109 proceed:
110 nvp->column++;
111 nvp->pos++;
112 return IDLE;
113 }
114
115 static enum nvram_parser_state brcmf_nvram_handle_key(struct nvram_parser *nvp)
116 {
117 enum nvram_parser_state st = nvp->state;
118 char c;
119
120 c = nvp->data[nvp->pos];
121 if (c == '=') {
122 /* ignore RAW1 by treating as comment */
123 if (strncmp(&nvp->data[nvp->entry], "RAW1", 4) == 0)
124 st = COMMENT;
125 else
126 st = VALUE;
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);
136 return COMMENT;
137 }
138
139 nvp->column++;
140 nvp->pos++;
141 return st;
142 }
143
144 static enum nvram_parser_state
145 brcmf_nvram_handle_value(struct nvram_parser *nvp)
146 {
147 char c;
148 char *skv;
149 char *ekv;
150 u32 cplen;
151
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];
157 cplen = ekv - skv;
158 if (nvp->nvram_len + cplen + 1 >= BRCMF_FW_MAX_NVRAM_SIZE)
159 return END;
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';
164 nvp->nvram_len++;
165 return IDLE;
166 }
167 nvp->pos++;
168 nvp->column++;
169 return VALUE;
170 }
171
172 static enum nvram_parser_state
173 brcmf_nvram_handle_comment(struct nvram_parser *nvp)
174 {
175 char *eoc, *sol;
176
177 sol = (char *)&nvp->data[nvp->pos];
178 eoc = strchr(sol, '\n');
179 if (!eoc) {
180 eoc = strchr(sol, '\0');
181 if (!eoc)
182 return END;
183 }
184
185 /* eat all moving to next line */
186 nvp->line++;
187 nvp->column = 1;
188 nvp->pos += (eoc - sol) + 1;
189 return IDLE;
190 }
191
192 static enum nvram_parser_state brcmf_nvram_handle_end(struct nvram_parser *nvp)
193 {
194 /* final state */
195 return END;
196 }
197
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
205 };
206
207 static int brcmf_init_nvram_parser(struct nvram_parser *nvp,
208 const u8 *data, size_t data_len)
209 {
210 size_t size;
211
212 memset(nvp, 0, sizeof(*nvp));
213 nvp->data = data;
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;
217 else
218 size = data_len;
219 /* Alloc for extra 0 byte + roundup by 4 + length field */
220 size += 1 + 3 + sizeof(u32);
221 nvp->nvram = kzalloc(size, GFP_KERNEL);
222 if (!nvp->nvram)
223 return -ENOMEM;
224
225 nvp->line = 1;
226 nvp->column = 1;
227 return 0;
228 }
229
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.
234 */
235 static void brcmf_fw_strip_multi_v1(struct nvram_parser *nvp, u16 domain_nr,
236 u16 bus_nr)
237 {
238 /* Device path with a leading '=' key-value separator */
239 char pci_path[] = "=pci/?/?";
240 size_t pci_len;
241 char pcie_path[] = "=pcie/?/?";
242 size_t pcie_len;
243
244 u32 i, j;
245 bool found;
246 u8 *nvram;
247 u8 id;
248
249 nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
250 if (!nvram)
251 goto fail;
252
253 /* min length: devpath0=pcie/1/4/ + 0:x=y */
254 if (nvp->nvram_len < BRCMF_FW_NVRAM_DEVPATH_LEN + 6)
255 goto fail;
256
257 /* First search for the devpathX and see if it is the configuration
258 * for domain_nr/bus_nr. Search complete nvp
259 */
260 snprintf(pci_path, sizeof(pci_path), "=pci/%d/%d", domain_nr,
261 bus_nr);
262 pci_len = strlen(pci_path);
263 snprintf(pcie_path, sizeof(pcie_path), "=pcie/%d/%d", domain_nr,
264 bus_nr);
265 pcie_len = strlen(pcie_path);
266 found = false;
267 i = 0;
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
271 */
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';
276 found = true;
277 break;
278 }
279 while (nvp->nvram[i] != 0)
280 i++;
281 i++;
282 }
283 if (!found)
284 goto fail;
285
286 /* Now copy all valid entries, release old nvram and assign new one */
287 i = 0;
288 j = 0;
289 while (i < nvp->nvram_len) {
290 if ((nvp->nvram[i] - '0' == id) && (nvp->nvram[i + 1] == ':')) {
291 i += 2;
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];
296 i++;
297 j++;
298 }
299 nvram[j] = 0;
300 j++;
301 }
302 while (nvp->nvram[i] != 0)
303 i++;
304 i++;
305 }
306 kfree(nvp->nvram);
307 nvp->nvram = nvram;
308 nvp->nvram_len = j;
309 return;
310
311 fail:
312 kfree(nvram);
313 nvp->nvram_len = 0;
314 }
315
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:
321 */
322 static void brcmf_fw_strip_multi_v2(struct nvram_parser *nvp, u16 domain_nr,
323 u16 bus_nr)
324 {
325 char prefix[BRCMF_FW_NVRAM_PCIEDEV_LEN];
326 size_t len;
327 u32 i, j;
328 u8 *nvram;
329
330 nvram = kzalloc(nvp->nvram_len + 1 + 3 + sizeof(u32), GFP_KERNEL);
331 if (!nvram)
332 goto fail;
333
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
336 * Y = bus_nr.
337 */
338 snprintf(prefix, sizeof(prefix), "pcie/%d/%d/", domain_nr, bus_nr);
339 len = strlen(prefix);
340 i = 0;
341 j = 0;
342 while (i < nvp->nvram_len - len) {
343 if (strncmp(&nvp->nvram[i], prefix, len) == 0) {
344 i += len;
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];
349 i++;
350 j++;
351 }
352 nvram[j] = 0;
353 j++;
354 }
355 while (nvp->nvram[i] != 0)
356 i++;
357 i++;
358 }
359 kfree(nvp->nvram);
360 nvp->nvram = nvram;
361 nvp->nvram_len = j;
362 return;
363 fail:
364 kfree(nvram);
365 nvp->nvram_len = 0;
366 }
367
368 static void brcmf_fw_add_defaults(struct nvram_parser *nvp)
369 {
370 if (nvp->boardrev_found)
371 return;
372
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';
377 nvp->nvram_len++;
378 }
379
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.
384 */
385 static void *brcmf_fw_nvram_strip(const u8 *data, size_t data_len,
386 u32 *new_length, u16 domain_nr, u16 bus_nr)
387 {
388 struct nvram_parser nvp;
389 u32 pad;
390 u32 token;
391 __le32 token_le;
392
393 if (brcmf_init_nvram_parser(&nvp, data, data_len) < 0)
394 return NULL;
395
396 while (nvp.pos < data_len) {
397 nvp.state = nv_parser_states[nvp.state](&nvp);
398 if (nvp.state == END)
399 break;
400 }
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);
407 }
408
409 if (nvp.nvram_len == 0) {
410 kfree(nvp.nvram);
411 return NULL;
412 }
413
414 brcmf_fw_add_defaults(&nvp);
415
416 pad = nvp.nvram_len;
417 *new_length = roundup(nvp.nvram_len + 1, 4);
418 while (pad != *new_length) {
419 nvp.nvram[pad] = 0;
420 pad++;
421 }
422
423 token = *new_length / 4;
424 token = (~token << 16) | (token & 0x0000FFFF);
425 token_le = cpu_to_le32(token);
426
427 memcpy(&nvp.nvram[*new_length], &token_le, sizeof(token_le));
428 *new_length += sizeof(token_le);
429
430 return nvp.nvram;
431 }
432
433 void brcmf_fw_nvram_free(void *nvram)
434 {
435 kfree(nvram);
436 }
437
438 struct brcmf_fw {
439 struct device *dev;
440 u16 flags;
441 const struct firmware *code;
442 const char *nvram_name;
443 u16 domain_nr;
444 u16 bus_nr;
445 void (*done)(struct device *dev, int err, const struct firmware *fw,
446 void *nvram_image, u32 nvram_len);
447 };
448
449 static void brcmf_fw_request_nvram_done(const struct firmware *fw, void *ctx)
450 {
451 struct brcmf_fw *fwctx = ctx;
452 u32 nvram_length = 0;
453 void *nvram = NULL;
454 u8 *data = NULL;
455 size_t data_len;
456 bool raw_nvram;
457
458 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
459 if (fw && fw->data) {
460 data = (u8 *)fw->data;
461 data_len = fw->size;
462 raw_nvram = false;
463 } else {
464 data = bcm47xx_nvram_get_contents(&data_len);
465 if (!data && !(fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL))
466 goto fail;
467 raw_nvram = true;
468 }
469
470 if (data)
471 nvram = brcmf_fw_nvram_strip(data, data_len, &nvram_length,
472 fwctx->domain_nr, fwctx->bus_nr);
473
474 if (raw_nvram)
475 bcm47xx_nvram_release_contents(data);
476 release_firmware(fw);
477 if (!nvram && !(fwctx->flags & BRCMF_FW_REQ_NV_OPTIONAL))
478 goto fail;
479
480 fwctx->done(fwctx->dev, 0, fwctx->code, nvram, nvram_length);
481 kfree(fwctx);
482 return;
483
484 fail:
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);
488 kfree(fwctx);
489 }
490
491 static void brcmf_fw_request_code_done(const struct firmware *fw, void *ctx)
492 {
493 struct brcmf_fw *fwctx = ctx;
494 int ret = 0;
495
496 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(fwctx->dev));
497 if (!fw) {
498 ret = -ENOENT;
499 goto fail;
500 }
501 /* only requested code so done here */
502 if (!(fwctx->flags & BRCMF_FW_REQUEST_NVRAM))
503 goto done;
504
505 fwctx->code = fw;
506 ret = request_firmware_nowait(THIS_MODULE, true, fwctx->nvram_name,
507 fwctx->dev, GFP_KERNEL, fwctx,
508 brcmf_fw_request_nvram_done);
509
510 /* pass NULL to nvram callback for bcm47xx fallback */
511 if (ret)
512 brcmf_fw_request_nvram_done(NULL, fwctx);
513 return;
514
515 fail:
516 brcmf_dbg(TRACE, "failed: dev=%s\n", dev_name(fwctx->dev));
517 done:
518 fwctx->done(fwctx->dev, ret, fw, NULL, 0);
519 kfree(fwctx);
520 }
521
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)
528 {
529 struct brcmf_fw *fwctx;
530
531 brcmf_dbg(TRACE, "enter: dev=%s\n", dev_name(dev));
532 if (!fw_cb || !code)
533 return -EINVAL;
534
535 if ((flags & BRCMF_FW_REQUEST_NVRAM) && !nvram)
536 return -EINVAL;
537
538 fwctx = kzalloc(sizeof(*fwctx), GFP_KERNEL);
539 if (!fwctx)
540 return -ENOMEM;
541
542 fwctx->dev = dev;
543 fwctx->flags = flags;
544 fwctx->done = fw_cb;
545 if (flags & BRCMF_FW_REQUEST_NVRAM)
546 fwctx->nvram_name = nvram;
547 fwctx->domain_nr = domain_nr;
548 fwctx->bus_nr = bus_nr;
549
550 return request_firmware_nowait(THIS_MODULE, true, code, dev,
551 GFP_KERNEL, fwctx,
552 brcmf_fw_request_code_done);
553 }
554
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))
560 {
561 return brcmf_fw_get_firmwares_pcie(dev, flags, code, nvram, fw_cb, 0,
562 0);
563 }
564
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])
569 {
570 u32 i;
571 char end;
572
573 for (i = 0; i < table_size; i++) {
574 if (mapping_table[i].chipid == chip &&
575 mapping_table[i].revmask & BIT(chiprev))
576 break;
577 }
578
579 if (i == table_size) {
580 brcmf_err("Unknown chipid %d [%d]\n", chip, chiprev);
581 return -ENODEV;
582 }
583
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,
587 BRCMF_FW_NAME_LEN);
588 if ((nvram_name) && (mapping_table[i].nvram))
589 strlcpy(nvram_name, brcmf_mp_global.firmware_path,
590 BRCMF_FW_NAME_LEN);
591
592 end = brcmf_mp_global.firmware_path[
593 strlen(brcmf_mp_global.firmware_path) - 1];
594 if (end != '/') {
595 strlcat(fw_name, "/", BRCMF_FW_NAME_LEN);
596 if ((nvram_name) && (mapping_table[i].nvram))
597 strlcat(nvram_name, "/", BRCMF_FW_NAME_LEN);
598 }
599 }
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);
603
604 return 0;
605 }
606