Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / fwil.c
blobfc5751116d99bf89f46769eb57bee290f6ae35c8
1 /*
2 * Copyright (c) 2012 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 /* FWIL is the Firmware Interface Layer. In this module the support functions
18 * are located to set and get variables to and from the firmware.
21 #include <linux/kernel.h>
22 #include <linux/netdevice.h>
23 #include <brcmu_utils.h>
24 #include <brcmu_wifi.h>
25 #include "core.h"
26 #include "bus.h"
27 #include "debug.h"
28 #include "tracepoint.h"
29 #include "fwil.h"
30 #include "proto.h"
33 #define MAX_HEX_DUMP_LEN 64
35 #ifdef DEBUG
36 static const char * const brcmf_fil_errstr[] = {
37 "BCME_OK",
38 "BCME_ERROR",
39 "BCME_BADARG",
40 "BCME_BADOPTION",
41 "BCME_NOTUP",
42 "BCME_NOTDOWN",
43 "BCME_NOTAP",
44 "BCME_NOTSTA",
45 "BCME_BADKEYIDX",
46 "BCME_RADIOOFF",
47 "BCME_NOTBANDLOCKED",
48 "BCME_NOCLK",
49 "BCME_BADRATESET",
50 "BCME_BADBAND",
51 "BCME_BUFTOOSHORT",
52 "BCME_BUFTOOLONG",
53 "BCME_BUSY",
54 "BCME_NOTASSOCIATED",
55 "BCME_BADSSIDLEN",
56 "BCME_OUTOFRANGECHAN",
57 "BCME_BADCHAN",
58 "BCME_BADADDR",
59 "BCME_NORESOURCE",
60 "BCME_UNSUPPORTED",
61 "BCME_BADLEN",
62 "BCME_NOTREADY",
63 "BCME_EPERM",
64 "BCME_NOMEM",
65 "BCME_ASSOCIATED",
66 "BCME_RANGE",
67 "BCME_NOTFOUND",
68 "BCME_WME_NOT_ENABLED",
69 "BCME_TSPEC_NOTFOUND",
70 "BCME_ACM_NOTSUPPORTED",
71 "BCME_NOT_WME_ASSOCIATION",
72 "BCME_SDIO_ERROR",
73 "BCME_DONGLE_DOWN",
74 "BCME_VERSION",
75 "BCME_TXFAIL",
76 "BCME_RXFAIL",
77 "BCME_NODEVICE",
78 "BCME_NMODE_DISABLED",
79 "BCME_NONRESIDENT",
80 "BCME_SCANREJECT",
81 "BCME_USAGE_ERROR",
82 "BCME_IOCTL_ERROR",
83 "BCME_SERIAL_PORT_ERR",
84 "BCME_DISABLED",
85 "BCME_DECERR",
86 "BCME_ENCERR",
87 "BCME_MICERR",
88 "BCME_REPLAY",
89 "BCME_IE_NOTFOUND",
92 static const char *brcmf_fil_get_errstr(u32 err)
94 if (err >= ARRAY_SIZE(brcmf_fil_errstr))
95 return "(unknown)";
97 return brcmf_fil_errstr[err];
99 #else
100 static const char *brcmf_fil_get_errstr(u32 err)
102 return "";
104 #endif /* DEBUG */
106 static s32
107 brcmf_fil_cmd_data(struct brcmf_if *ifp, u32 cmd, void *data, u32 len, bool set)
109 struct brcmf_pub *drvr = ifp->drvr;
110 s32 err, fwerr;
112 if (drvr->bus_if->state != BRCMF_BUS_UP) {
113 brcmf_err("bus is down. we have nothing to do.\n");
114 return -EIO;
117 if (data != NULL)
118 len = min_t(uint, len, BRCMF_DCMD_MAXLEN);
119 if (set)
120 err = brcmf_proto_set_dcmd(drvr, ifp->ifidx, cmd,
121 data, len, &fwerr);
122 else
123 err = brcmf_proto_query_dcmd(drvr, ifp->ifidx, cmd,
124 data, len, &fwerr);
126 if (err) {
127 brcmf_dbg(FIL, "Failed: %s (%d)\n",
128 brcmf_fil_get_errstr((u32)(-err)), err);
129 } else if (fwerr < 0) {
130 brcmf_dbg(FIL, "Firmware error: %s (%d)\n",
131 brcmf_fil_get_errstr((u32)(-fwerr)), fwerr);
132 err = -EBADE;
134 if (ifp->fwil_fwerr)
135 return fwerr;
137 return err;
141 brcmf_fil_cmd_data_set(struct brcmf_if *ifp, u32 cmd, void *data, u32 len)
143 s32 err;
145 mutex_lock(&ifp->drvr->proto_block);
147 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, len=%d\n", ifp->ifidx, cmd, len);
148 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
149 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
151 err = brcmf_fil_cmd_data(ifp, cmd, data, len, true);
152 mutex_unlock(&ifp->drvr->proto_block);
154 return err;
158 brcmf_fil_cmd_data_get(struct brcmf_if *ifp, u32 cmd, void *data, u32 len)
160 s32 err;
162 mutex_lock(&ifp->drvr->proto_block);
163 err = brcmf_fil_cmd_data(ifp, cmd, data, len, false);
165 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, len=%d\n", ifp->ifidx, cmd, len);
166 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
167 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
169 mutex_unlock(&ifp->drvr->proto_block);
171 return err;
176 brcmf_fil_cmd_int_set(struct brcmf_if *ifp, u32 cmd, u32 data)
178 s32 err;
179 __le32 data_le = cpu_to_le32(data);
181 mutex_lock(&ifp->drvr->proto_block);
182 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, value=%d\n", ifp->ifidx, cmd, data);
183 err = brcmf_fil_cmd_data(ifp, cmd, &data_le, sizeof(data_le), true);
184 mutex_unlock(&ifp->drvr->proto_block);
186 return err;
190 brcmf_fil_cmd_int_get(struct brcmf_if *ifp, u32 cmd, u32 *data)
192 s32 err;
193 __le32 data_le = cpu_to_le32(*data);
195 mutex_lock(&ifp->drvr->proto_block);
196 err = brcmf_fil_cmd_data(ifp, cmd, &data_le, sizeof(data_le), false);
197 mutex_unlock(&ifp->drvr->proto_block);
198 *data = le32_to_cpu(data_le);
199 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, value=%d\n", ifp->ifidx, cmd, *data);
201 return err;
204 static u32
205 brcmf_create_iovar(char *name, const char *data, u32 datalen,
206 char *buf, u32 buflen)
208 u32 len;
210 len = strlen(name) + 1;
212 if ((len + datalen) > buflen)
213 return 0;
215 memcpy(buf, name, len);
217 /* append data onto the end of the name string */
218 if (data && datalen)
219 memcpy(&buf[len], data, datalen);
221 return len + datalen;
226 brcmf_fil_iovar_data_set(struct brcmf_if *ifp, char *name, const void *data,
227 u32 len)
229 struct brcmf_pub *drvr = ifp->drvr;
230 s32 err;
231 u32 buflen;
233 mutex_lock(&drvr->proto_block);
235 brcmf_dbg(FIL, "ifidx=%d, name=%s, len=%d\n", ifp->ifidx, name, len);
236 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
237 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
239 buflen = brcmf_create_iovar(name, data, len, drvr->proto_buf,
240 sizeof(drvr->proto_buf));
241 if (buflen) {
242 err = brcmf_fil_cmd_data(ifp, BRCMF_C_SET_VAR, drvr->proto_buf,
243 buflen, true);
244 } else {
245 err = -EPERM;
246 brcmf_err("Creating iovar failed\n");
249 mutex_unlock(&drvr->proto_block);
250 return err;
254 brcmf_fil_iovar_data_get(struct brcmf_if *ifp, char *name, void *data,
255 u32 len)
257 struct brcmf_pub *drvr = ifp->drvr;
258 s32 err;
259 u32 buflen;
261 mutex_lock(&drvr->proto_block);
263 buflen = brcmf_create_iovar(name, data, len, drvr->proto_buf,
264 sizeof(drvr->proto_buf));
265 if (buflen) {
266 err = brcmf_fil_cmd_data(ifp, BRCMF_C_GET_VAR, drvr->proto_buf,
267 buflen, false);
268 if (err == 0)
269 memcpy(data, drvr->proto_buf, len);
270 } else {
271 err = -EPERM;
272 brcmf_err("Creating iovar failed\n");
275 brcmf_dbg(FIL, "ifidx=%d, name=%s, len=%d\n", ifp->ifidx, name, len);
276 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
277 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
279 mutex_unlock(&drvr->proto_block);
280 return err;
284 brcmf_fil_iovar_int_set(struct brcmf_if *ifp, char *name, u32 data)
286 __le32 data_le = cpu_to_le32(data);
288 return brcmf_fil_iovar_data_set(ifp, name, &data_le, sizeof(data_le));
292 brcmf_fil_iovar_int_get(struct brcmf_if *ifp, char *name, u32 *data)
294 __le32 data_le = cpu_to_le32(*data);
295 s32 err;
297 err = brcmf_fil_iovar_data_get(ifp, name, &data_le, sizeof(data_le));
298 if (err == 0)
299 *data = le32_to_cpu(data_le);
300 return err;
303 static u32
304 brcmf_create_bsscfg(s32 bsscfgidx, char *name, char *data, u32 datalen,
305 char *buf, u32 buflen)
307 const s8 *prefix = "bsscfg:";
308 s8 *p;
309 u32 prefixlen;
310 u32 namelen;
311 u32 iolen;
312 __le32 bsscfgidx_le;
314 if (bsscfgidx == 0)
315 return brcmf_create_iovar(name, data, datalen, buf, buflen);
317 prefixlen = strlen(prefix);
318 namelen = strlen(name) + 1; /* lengh of iovar name + null */
319 iolen = prefixlen + namelen + sizeof(bsscfgidx_le) + datalen;
321 if (buflen < iolen) {
322 brcmf_err("buffer is too short\n");
323 return 0;
326 p = buf;
328 /* copy prefix, no null */
329 memcpy(p, prefix, prefixlen);
330 p += prefixlen;
332 /* copy iovar name including null */
333 memcpy(p, name, namelen);
334 p += namelen;
336 /* bss config index as first data */
337 bsscfgidx_le = cpu_to_le32(bsscfgidx);
338 memcpy(p, &bsscfgidx_le, sizeof(bsscfgidx_le));
339 p += sizeof(bsscfgidx_le);
341 /* parameter buffer follows */
342 if (datalen)
343 memcpy(p, data, datalen);
345 return iolen;
349 brcmf_fil_bsscfg_data_set(struct brcmf_if *ifp, char *name,
350 void *data, u32 len)
352 struct brcmf_pub *drvr = ifp->drvr;
353 s32 err;
354 u32 buflen;
356 mutex_lock(&drvr->proto_block);
358 brcmf_dbg(FIL, "ifidx=%d, bsscfgidx=%d, name=%s, len=%d\n", ifp->ifidx,
359 ifp->bsscfgidx, name, len);
360 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
361 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
363 buflen = brcmf_create_bsscfg(ifp->bsscfgidx, name, data, len,
364 drvr->proto_buf, sizeof(drvr->proto_buf));
365 if (buflen) {
366 err = brcmf_fil_cmd_data(ifp, BRCMF_C_SET_VAR, drvr->proto_buf,
367 buflen, true);
368 } else {
369 err = -EPERM;
370 brcmf_err("Creating bsscfg failed\n");
373 mutex_unlock(&drvr->proto_block);
374 return err;
378 brcmf_fil_bsscfg_data_get(struct brcmf_if *ifp, char *name,
379 void *data, u32 len)
381 struct brcmf_pub *drvr = ifp->drvr;
382 s32 err;
383 u32 buflen;
385 mutex_lock(&drvr->proto_block);
387 buflen = brcmf_create_bsscfg(ifp->bsscfgidx, name, data, len,
388 drvr->proto_buf, sizeof(drvr->proto_buf));
389 if (buflen) {
390 err = brcmf_fil_cmd_data(ifp, BRCMF_C_GET_VAR, drvr->proto_buf,
391 buflen, false);
392 if (err == 0)
393 memcpy(data, drvr->proto_buf, len);
394 } else {
395 err = -EPERM;
396 brcmf_err("Creating bsscfg failed\n");
398 brcmf_dbg(FIL, "ifidx=%d, bsscfgidx=%d, name=%s, len=%d\n", ifp->ifidx,
399 ifp->bsscfgidx, name, len);
400 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
401 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
403 mutex_unlock(&drvr->proto_block);
404 return err;
409 brcmf_fil_bsscfg_int_set(struct brcmf_if *ifp, char *name, u32 data)
411 __le32 data_le = cpu_to_le32(data);
413 return brcmf_fil_bsscfg_data_set(ifp, name, &data_le,
414 sizeof(data_le));
418 brcmf_fil_bsscfg_int_get(struct brcmf_if *ifp, char *name, u32 *data)
420 __le32 data_le = cpu_to_le32(*data);
421 s32 err;
423 err = brcmf_fil_bsscfg_data_get(ifp, name, &data_le,
424 sizeof(data_le));
425 if (err == 0)
426 *data = le32_to_cpu(data_le);
427 return err;