1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2012 Broadcom Corporation
6 /* FWIL is the Firmware Interface Layer. In this module the support functions
7 * are located to set and get variables to and from the firmware.
10 #include <linux/kernel.h>
11 #include <linux/netdevice.h>
12 #include <brcmu_utils.h>
13 #include <brcmu_wifi.h>
17 #include "tracepoint.h"
22 #define MAX_HEX_DUMP_LEN 64
25 static const char * const brcmf_fil_errstr
[] = {
45 "BCME_OUTOFRANGECHAN",
57 "BCME_WME_NOT_ENABLED",
58 "BCME_TSPEC_NOTFOUND",
59 "BCME_ACM_NOTSUPPORTED",
60 "BCME_NOT_WME_ASSOCIATION",
67 "BCME_NMODE_DISABLED",
72 "BCME_SERIAL_PORT_ERR",
81 static const char *brcmf_fil_get_errstr(u32 err
)
83 if (err
>= ARRAY_SIZE(brcmf_fil_errstr
))
86 return brcmf_fil_errstr
[err
];
89 static const char *brcmf_fil_get_errstr(u32 err
)
96 brcmf_fil_cmd_data(struct brcmf_if
*ifp
, u32 cmd
, void *data
, u32 len
, bool set
)
98 struct brcmf_pub
*drvr
= ifp
->drvr
;
101 if (drvr
->bus_if
->state
!= BRCMF_BUS_UP
) {
102 bphy_err(drvr
, "bus is down. we have nothing to do.\n");
107 len
= min_t(uint
, len
, BRCMF_DCMD_MAXLEN
);
109 err
= brcmf_proto_set_dcmd(drvr
, ifp
->ifidx
, cmd
,
112 err
= brcmf_proto_query_dcmd(drvr
, ifp
->ifidx
, cmd
,
116 brcmf_dbg(FIL
, "Failed: error=%d\n", err
);
117 } else if (fwerr
< 0) {
118 brcmf_dbg(FIL
, "Firmware error: %s (%d)\n",
119 brcmf_fil_get_errstr((u32
)(-fwerr
)), fwerr
);
129 brcmf_fil_cmd_data_set(struct brcmf_if
*ifp
, u32 cmd
, void *data
, u32 len
)
133 mutex_lock(&ifp
->drvr
->proto_block
);
135 brcmf_dbg(FIL
, "ifidx=%d, cmd=%d, len=%d\n", ifp
->ifidx
, cmd
, len
);
136 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data
,
137 min_t(uint
, len
, MAX_HEX_DUMP_LEN
), "data\n");
139 err
= brcmf_fil_cmd_data(ifp
, cmd
, data
, len
, true);
140 mutex_unlock(&ifp
->drvr
->proto_block
);
146 brcmf_fil_cmd_data_get(struct brcmf_if
*ifp
, u32 cmd
, void *data
, u32 len
)
150 mutex_lock(&ifp
->drvr
->proto_block
);
151 err
= brcmf_fil_cmd_data(ifp
, cmd
, data
, len
, false);
153 brcmf_dbg(FIL
, "ifidx=%d, cmd=%d, len=%d\n", ifp
->ifidx
, cmd
, len
);
154 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data
,
155 min_t(uint
, len
, MAX_HEX_DUMP_LEN
), "data\n");
157 mutex_unlock(&ifp
->drvr
->proto_block
);
164 brcmf_fil_cmd_int_set(struct brcmf_if
*ifp
, u32 cmd
, u32 data
)
167 __le32 data_le
= cpu_to_le32(data
);
169 mutex_lock(&ifp
->drvr
->proto_block
);
170 brcmf_dbg(FIL
, "ifidx=%d, cmd=%d, value=%d\n", ifp
->ifidx
, cmd
, data
);
171 err
= brcmf_fil_cmd_data(ifp
, cmd
, &data_le
, sizeof(data_le
), true);
172 mutex_unlock(&ifp
->drvr
->proto_block
);
178 brcmf_fil_cmd_int_get(struct brcmf_if
*ifp
, u32 cmd
, u32
*data
)
181 __le32 data_le
= cpu_to_le32(*data
);
183 mutex_lock(&ifp
->drvr
->proto_block
);
184 err
= brcmf_fil_cmd_data(ifp
, cmd
, &data_le
, sizeof(data_le
), false);
185 mutex_unlock(&ifp
->drvr
->proto_block
);
186 *data
= le32_to_cpu(data_le
);
187 brcmf_dbg(FIL
, "ifidx=%d, cmd=%d, value=%d\n", ifp
->ifidx
, cmd
, *data
);
193 brcmf_create_iovar(char *name
, const char *data
, u32 datalen
,
194 char *buf
, u32 buflen
)
198 len
= strlen(name
) + 1;
200 if ((len
+ datalen
) > buflen
)
203 memcpy(buf
, name
, len
);
205 /* append data onto the end of the name string */
207 memcpy(&buf
[len
], data
, datalen
);
209 return len
+ datalen
;
214 brcmf_fil_iovar_data_set(struct brcmf_if
*ifp
, char *name
, const void *data
,
217 struct brcmf_pub
*drvr
= ifp
->drvr
;
221 mutex_lock(&drvr
->proto_block
);
223 brcmf_dbg(FIL
, "ifidx=%d, name=%s, len=%d\n", ifp
->ifidx
, name
, len
);
224 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data
,
225 min_t(uint
, len
, MAX_HEX_DUMP_LEN
), "data\n");
227 buflen
= brcmf_create_iovar(name
, data
, len
, drvr
->proto_buf
,
228 sizeof(drvr
->proto_buf
));
230 err
= brcmf_fil_cmd_data(ifp
, BRCMF_C_SET_VAR
, drvr
->proto_buf
,
234 bphy_err(drvr
, "Creating iovar failed\n");
237 mutex_unlock(&drvr
->proto_block
);
242 brcmf_fil_iovar_data_get(struct brcmf_if
*ifp
, char *name
, void *data
,
245 struct brcmf_pub
*drvr
= ifp
->drvr
;
249 mutex_lock(&drvr
->proto_block
);
251 buflen
= brcmf_create_iovar(name
, data
, len
, drvr
->proto_buf
,
252 sizeof(drvr
->proto_buf
));
254 err
= brcmf_fil_cmd_data(ifp
, BRCMF_C_GET_VAR
, drvr
->proto_buf
,
257 memcpy(data
, drvr
->proto_buf
, len
);
260 bphy_err(drvr
, "Creating iovar failed\n");
263 brcmf_dbg(FIL
, "ifidx=%d, name=%s, len=%d\n", ifp
->ifidx
, name
, len
);
264 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data
,
265 min_t(uint
, len
, MAX_HEX_DUMP_LEN
), "data\n");
267 mutex_unlock(&drvr
->proto_block
);
272 brcmf_fil_iovar_int_set(struct brcmf_if
*ifp
, char *name
, u32 data
)
274 __le32 data_le
= cpu_to_le32(data
);
276 return brcmf_fil_iovar_data_set(ifp
, name
, &data_le
, sizeof(data_le
));
280 brcmf_fil_iovar_int_get(struct brcmf_if
*ifp
, char *name
, u32
*data
)
282 __le32 data_le
= cpu_to_le32(*data
);
285 err
= brcmf_fil_iovar_data_get(ifp
, name
, &data_le
, sizeof(data_le
));
287 *data
= le32_to_cpu(data_le
);
292 brcmf_create_bsscfg(s32 bsscfgidx
, char *name
, char *data
, u32 datalen
,
293 char *buf
, u32 buflen
)
295 const s8
*prefix
= "bsscfg:";
303 return brcmf_create_iovar(name
, data
, datalen
, buf
, buflen
);
305 prefixlen
= strlen(prefix
);
306 namelen
= strlen(name
) + 1; /* length of iovar name + null */
307 iolen
= prefixlen
+ namelen
+ sizeof(bsscfgidx_le
) + datalen
;
309 if (buflen
< iolen
) {
310 brcmf_err("buffer is too short\n");
316 /* copy prefix, no null */
317 memcpy(p
, prefix
, prefixlen
);
320 /* copy iovar name including null */
321 memcpy(p
, name
, namelen
);
324 /* bss config index as first data */
325 bsscfgidx_le
= cpu_to_le32(bsscfgidx
);
326 memcpy(p
, &bsscfgidx_le
, sizeof(bsscfgidx_le
));
327 p
+= sizeof(bsscfgidx_le
);
329 /* parameter buffer follows */
331 memcpy(p
, data
, datalen
);
337 brcmf_fil_bsscfg_data_set(struct brcmf_if
*ifp
, char *name
,
340 struct brcmf_pub
*drvr
= ifp
->drvr
;
344 mutex_lock(&drvr
->proto_block
);
346 brcmf_dbg(FIL
, "ifidx=%d, bsscfgidx=%d, name=%s, len=%d\n", ifp
->ifidx
,
347 ifp
->bsscfgidx
, name
, len
);
348 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data
,
349 min_t(uint
, len
, MAX_HEX_DUMP_LEN
), "data\n");
351 buflen
= brcmf_create_bsscfg(ifp
->bsscfgidx
, name
, data
, len
,
352 drvr
->proto_buf
, sizeof(drvr
->proto_buf
));
354 err
= brcmf_fil_cmd_data(ifp
, BRCMF_C_SET_VAR
, drvr
->proto_buf
,
358 bphy_err(drvr
, "Creating bsscfg failed\n");
361 mutex_unlock(&drvr
->proto_block
);
366 brcmf_fil_bsscfg_data_get(struct brcmf_if
*ifp
, char *name
,
369 struct brcmf_pub
*drvr
= ifp
->drvr
;
373 mutex_lock(&drvr
->proto_block
);
375 buflen
= brcmf_create_bsscfg(ifp
->bsscfgidx
, name
, data
, len
,
376 drvr
->proto_buf
, sizeof(drvr
->proto_buf
));
378 err
= brcmf_fil_cmd_data(ifp
, BRCMF_C_GET_VAR
, drvr
->proto_buf
,
381 memcpy(data
, drvr
->proto_buf
, len
);
384 bphy_err(drvr
, "Creating bsscfg failed\n");
386 brcmf_dbg(FIL
, "ifidx=%d, bsscfgidx=%d, name=%s, len=%d\n", ifp
->ifidx
,
387 ifp
->bsscfgidx
, name
, len
);
388 brcmf_dbg_hex_dump(BRCMF_FIL_ON(), data
,
389 min_t(uint
, len
, MAX_HEX_DUMP_LEN
), "data\n");
391 mutex_unlock(&drvr
->proto_block
);
397 brcmf_fil_bsscfg_int_set(struct brcmf_if
*ifp
, char *name
, u32 data
)
399 __le32 data_le
= cpu_to_le32(data
);
401 return brcmf_fil_bsscfg_data_set(ifp
, name
, &data_le
,
406 brcmf_fil_bsscfg_int_get(struct brcmf_if
*ifp
, char *name
, u32
*data
)
408 __le32 data_le
= cpu_to_le32(*data
);
411 err
= brcmf_fil_bsscfg_data_get(ifp
, name
, &data_le
,
414 *data
= le32_to_cpu(data_le
);