1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2010 Broadcom Corporation
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/netdevice.h>
9 #include <linux/module.h>
10 #include <linux/firmware.h>
11 #include <brcmu_wifi.h>
12 #include <brcmu_utils.h>
17 #include "fwil_types.h"
18 #include "tracepoint.h"
24 MODULE_AUTHOR("Broadcom Corporation");
25 MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
26 MODULE_LICENSE("Dual BSD/GPL");
28 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME 40
29 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME 40
31 /* default boost value for RSSI_DELTA in preferred join selection */
32 #define BRCMF_JOIN_PREF_RSSI_BOOST 8
34 #define BRCMF_DEFAULT_TXGLOM_SIZE 32 /* max tx frames in glom chain */
36 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
37 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
38 MODULE_PARM_DESC(txglomsz, "Maximum tx packet chain size [SDIO]");
40 /* Debug level configuration. See debug.h for bits, sysfs modifiable */
42 module_param_named(debug, brcmf_msg_level, int, 0600);
43 MODULE_PARM_DESC(debug, "Level of debug output");
45 static int brcmf_p2p_enable;
46 module_param_named(p2pon, brcmf_p2p_enable, int, 0);
47 MODULE_PARM_DESC(p2pon, "Enable legacy p2p management functionality");
49 static int brcmf_feature_disable;
50 module_param_named(feature_disable, brcmf_feature_disable, int, 0);
51 MODULE_PARM_DESC(feature_disable, "Disable features");
53 static char brcmf_firmware_path[BRCMF_FW_ALTPATH_LEN];
54 module_param_string(alternative_fw_path, brcmf_firmware_path,
55 BRCMF_FW_ALTPATH_LEN, 0400);
56 MODULE_PARM_DESC(alternative_fw_path, "Alternative firmware path");
58 static int brcmf_fcmode;
59 module_param_named(fcmode, brcmf_fcmode, int, 0);
60 MODULE_PARM_DESC(fcmode, "Mode of firmware signalled flow control");
62 static int brcmf_roamoff;
63 module_param_named(roamoff, brcmf_roamoff, int, 0400);
64 MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
66 static int brcmf_iapp_enable;
67 module_param_named(iapp, brcmf_iapp_enable, int, 0);
68 MODULE_PARM_DESC(iapp, "Enable partial support for the obsoleted Inter-Access Point Protocol");
71 /* always succeed brcmf_bus_started() */
72 static int brcmf_ignore_probe_fail;
73 module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
74 MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
77 static struct brcmfmac_platform_data *brcmfmac_pdata;
78 struct brcmf_mp_global_t brcmf_mp_global;
80 void brcmf_c_set_joinpref_default(struct brcmf_if *ifp)
82 struct brcmf_pub *drvr = ifp->drvr;
83 struct brcmf_join_pref_params join_pref_params[2];
86 /* Setup join_pref to select target by RSSI (boost on 5GHz) */
87 join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
88 join_pref_params[0].len = 2;
89 join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
90 join_pref_params[0].band = WLC_BAND_5G;
92 join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
93 join_pref_params[1].len = 2;
94 join_pref_params[1].rssi_gain = 0;
95 join_pref_params[1].band = 0;
96 err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
97 sizeof(join_pref_params));
99 bphy_err(drvr, "Set join_pref error (%d)\n", err);
102 static int brcmf_c_download(struct brcmf_if *ifp, u16 flag,
103 struct brcmf_dload_data_le *dload_buf,
108 flag |= (DLOAD_HANDLER_VER << DLOAD_FLAG_VER_SHIFT);
109 dload_buf->flag = cpu_to_le16(flag);
110 dload_buf->dload_type = cpu_to_le16(DL_TYPE_CLM);
111 dload_buf->len = cpu_to_le32(len);
112 dload_buf->crc = cpu_to_le32(0);
113 len = sizeof(*dload_buf) + len - 1;
115 err = brcmf_fil_iovar_data_set(ifp, "clmload", dload_buf, len);
120 static int brcmf_c_process_clm_blob(struct brcmf_if *ifp)
122 struct brcmf_pub *drvr = ifp->drvr;
123 struct brcmf_bus *bus = drvr->bus_if;
124 struct brcmf_dload_data_le *chunk_buf;
125 const struct firmware *clm = NULL;
126 u8 clm_name[BRCMF_FW_NAME_LEN];
130 u16 dl_flag = DL_BEGIN;
134 brcmf_dbg(TRACE, "Enter\n");
136 memset(clm_name, 0, sizeof(clm_name));
137 err = brcmf_bus_get_board_fwname(bus, ".clm_blob", clm_name);
139 bphy_err(drvr, "get CLM blob file name failed (%d)\n", err);
144 err = firmware_request_nowarn(&clm, clm_name, bus->dev);
145 if (err || !clm_name[0]) {
146 err = brcmf_bus_get_fwname(bus, ".clm_blob", clm_name);
148 bphy_err(drvr, "get CLM blob file name failed (%d)\n", err);
152 err = firmware_request_nowarn(&clm, clm_name, bus->dev);
155 brcmf_info("no clm_blob available (err=%d), device may have limited channels available\n",
160 chunk_buf = kzalloc(sizeof(*chunk_buf) + MAX_CHUNK_LEN - 1, GFP_KERNEL);
169 if (datalen > MAX_CHUNK_LEN) {
170 chunk_len = MAX_CHUNK_LEN;
175 memcpy(chunk_buf->data, clm->data + cumulative_len, chunk_len);
177 err = brcmf_c_download(ifp, dl_flag, chunk_buf, chunk_len);
179 dl_flag &= ~DL_BEGIN;
181 cumulative_len += chunk_len;
182 datalen -= chunk_len;
183 } while ((datalen > 0) && (err == 0));
186 bphy_err(drvr, "clmload (%zu byte file) failed (%d)\n",
188 /* Retrieve clmload_status and print */
189 err = brcmf_fil_iovar_int_get(ifp, "clmload_status", &status);
191 bphy_err(drvr, "get clmload_status failed (%d)\n", err);
193 brcmf_dbg(INFO, "clmload_status=%d\n", status);
199 release_firmware(clm);
203 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
205 struct brcmf_pub *drvr = ifp->drvr;
206 s8 eventmask[BRCMF_EVENTING_MASK_LEN];
207 u8 buf[BRCMF_DCMD_SMLEN];
208 struct brcmf_bus *bus;
209 struct brcmf_rev_info_le revinfo;
210 struct brcmf_rev_info *ri;
215 /* retreive mac address */
216 err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
217 sizeof(ifp->mac_addr));
219 bphy_err(drvr, "Retrieving cur_etheraddr failed, %d\n", err);
222 memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
223 memcpy(ifp->drvr->wiphy->perm_addr, ifp->drvr->mac, ETH_ALEN);
225 bus = ifp->drvr->bus_if;
226 ri = &ifp->drvr->revinfo;
228 err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
229 &revinfo, sizeof(revinfo));
231 bphy_err(drvr, "retrieving revision info failed, %d\n", err);
232 strlcpy(ri->chipname, "UNKNOWN", sizeof(ri->chipname));
234 ri->vendorid = le32_to_cpu(revinfo.vendorid);
235 ri->deviceid = le32_to_cpu(revinfo.deviceid);
236 ri->radiorev = le32_to_cpu(revinfo.radiorev);
237 ri->corerev = le32_to_cpu(revinfo.corerev);
238 ri->boardid = le32_to_cpu(revinfo.boardid);
239 ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
240 ri->boardrev = le32_to_cpu(revinfo.boardrev);
241 ri->driverrev = le32_to_cpu(revinfo.driverrev);
242 ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
243 ri->bus = le32_to_cpu(revinfo.bus);
244 ri->phytype = le32_to_cpu(revinfo.phytype);
245 ri->phyrev = le32_to_cpu(revinfo.phyrev);
246 ri->anarev = le32_to_cpu(revinfo.anarev);
247 ri->chippkg = le32_to_cpu(revinfo.chippkg);
248 ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
250 /* use revinfo if not known yet */
252 bus->chip = le32_to_cpu(revinfo.chipnum);
253 bus->chiprev = le32_to_cpu(revinfo.chiprev);
259 brcmf_chip_name(bus->chip, bus->chiprev,
260 ri->chipname, sizeof(ri->chipname));
262 /* Do any CLM downloading */
263 err = brcmf_c_process_clm_blob(ifp);
265 bphy_err(drvr, "download CLM blob file failed, %d\n", err);
269 /* query for 'ver' to get version info from firmware */
270 memset(buf, 0, sizeof(buf));
271 err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
273 bphy_err(drvr, "Retrieving version information failed, %d\n",
280 /* Print fw version info */
281 brcmf_info("Firmware: %s %s\n", ri->chipname, buf);
283 /* locate firmware version number for ethtool */
284 ptr = strrchr(buf, ' ') + 1;
285 strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
287 /* Query for 'clmver' to get CLM version info from firmware */
288 memset(buf, 0, sizeof(buf));
289 err = brcmf_fil_iovar_data_get(ifp, "clmver", buf, sizeof(buf));
291 brcmf_dbg(TRACE, "retrieving clmver failed, %d\n", err);
293 clmver = (char *)buf;
294 /* store CLM version for adding it to revinfo debugfs file */
295 memcpy(ifp->drvr->clmver, clmver, sizeof(ifp->drvr->clmver));
297 /* Replace all newline/linefeed characters with space
300 strreplace(clmver, '\n', ' ');
302 brcmf_dbg(INFO, "CLM version = %s\n", clmver);
306 err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
308 bphy_err(drvr, "failed setting mpc\n");
312 brcmf_c_set_joinpref_default(ifp);
314 /* Setup event_msgs, enable E_IF */
315 err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
316 BRCMF_EVENTING_MASK_LEN);
318 bphy_err(drvr, "Get event_msgs error (%d)\n", err);
321 setbit(eventmask, BRCMF_E_IF);
322 err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
323 BRCMF_EVENTING_MASK_LEN);
325 bphy_err(drvr, "Set event_msgs error (%d)\n", err);
329 /* Setup default scan channel time */
330 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
331 BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
333 bphy_err(drvr, "BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
338 /* Setup default scan unassoc time */
339 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
340 BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
342 bphy_err(drvr, "BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
347 /* Enable tx beamforming, errors can be ignored (not supported) */
348 (void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
353 #ifndef CONFIG_BRCM_TRACING
354 void __brcmf_err(struct brcmf_bus *bus, const char *func, const char *fmt, ...)
356 struct va_format vaf;
364 dev_err(bus->dev, "%s: %pV", func, &vaf);
366 pr_err("%s: %pV", func, &vaf);
372 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
373 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
375 struct va_format vaf = {
382 if (brcmf_msg_level & level)
383 pr_debug("%s %pV", func, &vaf);
384 trace_brcmf_dbg(level, func, &vaf);
389 static void brcmf_mp_attach(void)
391 /* If module param firmware path is set then this will always be used,
392 * if not set then if available use the platform data version. To make
393 * sure it gets initialized at all, always copy the module param version
395 strlcpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
396 BRCMF_FW_ALTPATH_LEN);
397 if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
398 (brcmf_mp_global.firmware_path[0] == '\0')) {
399 strlcpy(brcmf_mp_global.firmware_path,
400 brcmfmac_pdata->fw_alternative_path,
401 BRCMF_FW_ALTPATH_LEN);
405 struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
406 enum brcmf_bus_type bus_type,
407 u32 chip, u32 chiprev)
409 struct brcmf_mp_device *settings;
410 struct brcmfmac_pd_device *device_pd;
414 brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
416 settings = kzalloc(sizeof(*settings), GFP_ATOMIC);
420 /* start by using the module paramaters */
421 settings->p2p_enable = !!brcmf_p2p_enable;
422 settings->feature_disable = brcmf_feature_disable;
423 settings->fcmode = brcmf_fcmode;
424 settings->roamoff = !!brcmf_roamoff;
425 settings->iapp = !!brcmf_iapp_enable;
427 settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
430 if (bus_type == BRCMF_BUSTYPE_SDIO)
431 settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
433 /* See if there is any device specific platform data configured */
435 if (brcmfmac_pdata) {
436 for (i = 0; i < brcmfmac_pdata->device_count; i++) {
437 device_pd = &brcmfmac_pdata->devices[i];
438 if ((device_pd->bus_type == bus_type) &&
439 (device_pd->id == chip) &&
440 ((device_pd->rev == chiprev) ||
441 (device_pd->rev == -1))) {
442 brcmf_dbg(INFO, "Platform data for device found\n");
443 settings->country_codes =
444 device_pd->country_codes;
445 if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
446 memcpy(&settings->bus.sdio,
447 &device_pd->bus.sdio,
448 sizeof(settings->bus.sdio));
455 /* No platform data for this device, try OF and DMI data */
456 brcmf_dmi_probe(settings, chip, chiprev);
457 brcmf_of_probe(dev, bus_type, settings);
462 void brcmf_release_module_param(struct brcmf_mp_device *module_param)
467 static int __init brcmf_common_pd_probe(struct platform_device *pdev)
469 brcmf_dbg(INFO, "Enter\n");
471 brcmfmac_pdata = dev_get_platdata(&pdev->dev);
473 if (brcmfmac_pdata->power_on)
474 brcmfmac_pdata->power_on();
479 static int brcmf_common_pd_remove(struct platform_device *pdev)
481 brcmf_dbg(INFO, "Enter\n");
483 if (brcmfmac_pdata->power_off)
484 brcmfmac_pdata->power_off();
489 static struct platform_driver brcmf_pd = {
490 .remove = brcmf_common_pd_remove,
492 .name = BRCMFMAC_PDATA_NAME,
496 static int __init brcmfmac_module_init(void)
500 /* Get the platform data (if available) for our devices */
501 err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
503 brcmf_dbg(INFO, "No platform data available.\n");
505 /* Initialize global module paramaters */
508 /* Continue the initialization by registering the different busses */
509 err = brcmf_core_init();
512 platform_driver_unregister(&brcmf_pd);
518 static void __exit brcmfmac_module_exit(void)
522 platform_driver_unregister(&brcmf_pd);
525 module_init(brcmfmac_module_init);
526 module_exit(brcmfmac_module_exit);