Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / fwil.c
blob802d7cb73b80f083c764eaaf75a5ec011eb3a05c
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: error=%d\n", err);
128 } else if (fwerr < 0) {
129 brcmf_dbg(FIL, "Firmware error: %s (%d)\n",
130 brcmf_fil_get_errstr((u32)(-fwerr)), fwerr);
131 err = -EBADE;
133 if (ifp->fwil_fwerr)
134 return fwerr;
136 return err;
140 brcmf_fil_cmd_data_set(struct brcmf_if *ifp, u32 cmd, void *data, u32 len)
142 s32 err;
144 mutex_lock(&ifp->drvr->proto_block);
146 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, len=%d\n", ifp->ifidx, cmd, len);
147 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
148 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
150 err = brcmf_fil_cmd_data(ifp, cmd, data, len, true);
151 mutex_unlock(&ifp->drvr->proto_block);
153 return err;
157 brcmf_fil_cmd_data_get(struct brcmf_if *ifp, u32 cmd, void *data, u32 len)
159 s32 err;
161 mutex_lock(&ifp->drvr->proto_block);
162 err = brcmf_fil_cmd_data(ifp, cmd, data, len, false);
164 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, len=%d\n", ifp->ifidx, cmd, len);
165 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
166 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
168 mutex_unlock(&ifp->drvr->proto_block);
170 return err;
175 brcmf_fil_cmd_int_set(struct brcmf_if *ifp, u32 cmd, u32 data)
177 s32 err;
178 __le32 data_le = cpu_to_le32(data);
180 mutex_lock(&ifp->drvr->proto_block);
181 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, value=%d\n", ifp->ifidx, cmd, data);
182 err = brcmf_fil_cmd_data(ifp, cmd, &data_le, sizeof(data_le), true);
183 mutex_unlock(&ifp->drvr->proto_block);
185 return err;
189 brcmf_fil_cmd_int_get(struct brcmf_if *ifp, u32 cmd, u32 *data)
191 s32 err;
192 __le32 data_le = cpu_to_le32(*data);
194 mutex_lock(&ifp->drvr->proto_block);
195 err = brcmf_fil_cmd_data(ifp, cmd, &data_le, sizeof(data_le), false);
196 mutex_unlock(&ifp->drvr->proto_block);
197 *data = le32_to_cpu(data_le);
198 brcmf_dbg(FIL, "ifidx=%d, cmd=%d, value=%d\n", ifp->ifidx, cmd, *data);
200 return err;
203 static u32
204 brcmf_create_iovar(char *name, const char *data, u32 datalen,
205 char *buf, u32 buflen)
207 u32 len;
209 len = strlen(name) + 1;
211 if ((len + datalen) > buflen)
212 return 0;
214 memcpy(buf, name, len);
216 /* append data onto the end of the name string */
217 if (data && datalen)
218 memcpy(&buf[len], data, datalen);
220 return len + datalen;
225 brcmf_fil_iovar_data_set(struct brcmf_if *ifp, char *name, const void *data,
226 u32 len)
228 struct brcmf_pub *drvr = ifp->drvr;
229 s32 err;
230 u32 buflen;
232 mutex_lock(&drvr->proto_block);
234 brcmf_dbg(FIL, "ifidx=%d, name=%s, len=%d\n", ifp->ifidx, name, len);
235 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
236 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
238 buflen = brcmf_create_iovar(name, data, len, drvr->proto_buf,
239 sizeof(drvr->proto_buf));
240 if (buflen) {
241 err = brcmf_fil_cmd_data(ifp, BRCMF_C_SET_VAR, drvr->proto_buf,
242 buflen, true);
243 } else {
244 err = -EPERM;
245 brcmf_err("Creating iovar failed\n");
248 mutex_unlock(&drvr->proto_block);
249 return err;
253 brcmf_fil_iovar_data_get(struct brcmf_if *ifp, char *name, void *data,
254 u32 len)
256 struct brcmf_pub *drvr = ifp->drvr;
257 s32 err;
258 u32 buflen;
260 mutex_lock(&drvr->proto_block);
262 buflen = brcmf_create_iovar(name, data, len, drvr->proto_buf,
263 sizeof(drvr->proto_buf));
264 if (buflen) {
265 err = brcmf_fil_cmd_data(ifp, BRCMF_C_GET_VAR, drvr->proto_buf,
266 buflen, false);
267 if (err == 0)
268 memcpy(data, drvr->proto_buf, len);
269 } else {
270 err = -EPERM;
271 brcmf_err("Creating iovar failed\n");
274 brcmf_dbg(FIL, "ifidx=%d, name=%s, len=%d\n", ifp->ifidx, name, len);
275 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
276 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
278 mutex_unlock(&drvr->proto_block);
279 return err;
283 brcmf_fil_iovar_int_set(struct brcmf_if *ifp, char *name, u32 data)
285 __le32 data_le = cpu_to_le32(data);
287 return brcmf_fil_iovar_data_set(ifp, name, &data_le, sizeof(data_le));
291 brcmf_fil_iovar_int_get(struct brcmf_if *ifp, char *name, u32 *data)
293 __le32 data_le = cpu_to_le32(*data);
294 s32 err;
296 err = brcmf_fil_iovar_data_get(ifp, name, &data_le, sizeof(data_le));
297 if (err == 0)
298 *data = le32_to_cpu(data_le);
299 return err;
302 static u32
303 brcmf_create_bsscfg(s32 bsscfgidx, char *name, char *data, u32 datalen,
304 char *buf, u32 buflen)
306 const s8 *prefix = "bsscfg:";
307 s8 *p;
308 u32 prefixlen;
309 u32 namelen;
310 u32 iolen;
311 __le32 bsscfgidx_le;
313 if (bsscfgidx == 0)
314 return brcmf_create_iovar(name, data, datalen, buf, buflen);
316 prefixlen = strlen(prefix);
317 namelen = strlen(name) + 1; /* lengh of iovar name + null */
318 iolen = prefixlen + namelen + sizeof(bsscfgidx_le) + datalen;
320 if (buflen < iolen) {
321 brcmf_err("buffer is too short\n");
322 return 0;
325 p = buf;
327 /* copy prefix, no null */
328 memcpy(p, prefix, prefixlen);
329 p += prefixlen;
331 /* copy iovar name including null */
332 memcpy(p, name, namelen);
333 p += namelen;
335 /* bss config index as first data */
336 bsscfgidx_le = cpu_to_le32(bsscfgidx);
337 memcpy(p, &bsscfgidx_le, sizeof(bsscfgidx_le));
338 p += sizeof(bsscfgidx_le);
340 /* parameter buffer follows */
341 if (datalen)
342 memcpy(p, data, datalen);
344 return iolen;
348 brcmf_fil_bsscfg_data_set(struct brcmf_if *ifp, char *name,
349 void *data, u32 len)
351 struct brcmf_pub *drvr = ifp->drvr;
352 s32 err;
353 u32 buflen;
355 mutex_lock(&drvr->proto_block);
357 brcmf_dbg(FIL, "ifidx=%d, bsscfgidx=%d, name=%s, len=%d\n", ifp->ifidx,
358 ifp->bsscfgidx, name, len);
359 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
360 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
362 buflen = brcmf_create_bsscfg(ifp->bsscfgidx, name, data, len,
363 drvr->proto_buf, sizeof(drvr->proto_buf));
364 if (buflen) {
365 err = brcmf_fil_cmd_data(ifp, BRCMF_C_SET_VAR, drvr->proto_buf,
366 buflen, true);
367 } else {
368 err = -EPERM;
369 brcmf_err("Creating bsscfg failed\n");
372 mutex_unlock(&drvr->proto_block);
373 return err;
377 brcmf_fil_bsscfg_data_get(struct brcmf_if *ifp, char *name,
378 void *data, u32 len)
380 struct brcmf_pub *drvr = ifp->drvr;
381 s32 err;
382 u32 buflen;
384 mutex_lock(&drvr->proto_block);
386 buflen = brcmf_create_bsscfg(ifp->bsscfgidx, name, data, len,
387 drvr->proto_buf, sizeof(drvr->proto_buf));
388 if (buflen) {
389 err = brcmf_fil_cmd_data(ifp, BRCMF_C_GET_VAR, drvr->proto_buf,
390 buflen, false);
391 if (err == 0)
392 memcpy(data, drvr->proto_buf, len);
393 } else {
394 err = -EPERM;
395 brcmf_err("Creating bsscfg failed\n");
397 brcmf_dbg(FIL, "ifidx=%d, bsscfgidx=%d, name=%s, len=%d\n", ifp->ifidx,
398 ifp->bsscfgidx, name, len);
399 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data,
400 min_t(uint, len, MAX_HEX_DUMP_LEN), "data\n");
402 mutex_unlock(&drvr->proto_block);
403 return err;
408 brcmf_fil_bsscfg_int_set(struct brcmf_if *ifp, char *name, u32 data)
410 __le32 data_le = cpu_to_le32(data);
412 return brcmf_fil_bsscfg_data_set(ifp, name, &data_le,
413 sizeof(data_le));
417 brcmf_fil_bsscfg_int_get(struct brcmf_if *ifp, char *name, u32 *data)
419 __le32 data_le = cpu_to_le32(*data);
420 s32 err;
422 err = brcmf_fil_bsscfg_data_get(ifp, name, &data_le,
423 sizeof(data_le));
424 if (err == 0)
425 *data = le32_to_cpu(data_le);
426 return err;