xref: /freebsd/sys/contrib/dev/broadcom/brcm80211/brcmfmac/common.c (revision 117d9331fedeb5c5657a796423c4f7b311f40770)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 
6 #if defined(__FreeBSD__)
7 #define	LINUXKPI_PARAM_PREFIX	brcmfmac_
8 #endif
9 
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/netdevice.h>
13 #include <linux/module.h>
14 #include <linux/firmware.h>
15 #include <brcmu_wifi.h>
16 #include <brcmu_utils.h>
17 #include "core.h"
18 #include "bus.h"
19 #include "debug.h"
20 #include "fwil.h"
21 #include "fwil_types.h"
22 #include "tracepoint.h"
23 #include "common.h"
24 #include "of.h"
25 #include "firmware.h"
26 #include "chip.h"
27 
28 MODULE_AUTHOR("Broadcom Corporation");
29 MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
30 MODULE_LICENSE("Dual BSD/GPL");
31 #if defined(__FreeBSD__)
32 MODULE_DEPEND(brcmfmac, brcmutil, 1, 1, 1);
33 MODULE_DEPEND(brcmfmac, linuxkpi, 1, 1, 1);
34 MODULE_DEPEND(brcmfmac, linuxkpi_wlan, 1, 1, 1);
35 MODULE_DEPEND(brcmfmac, lindebugfs, 1, 1, 1);   /* XXX-BZ someone should fix this */
36 #endif
37 
38 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME	40
39 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME	40
40 
41 /* default boost value for RSSI_DELTA in preferred join selection */
42 #define BRCMF_JOIN_PREF_RSSI_BOOST	8
43 
44 #define BRCMF_DEFAULT_TXGLOM_SIZE	32  /* max tx frames in glom chain */
45 
46 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
47 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
48 MODULE_PARM_DESC(txglomsz, "Maximum tx packet chain size [SDIO]");
49 
50 /* Debug level configuration. See debug.h for bits, sysfs modifiable */
51 int brcmf_msg_level;
52 module_param_named(debug, brcmf_msg_level, int, 0600);
53 MODULE_PARM_DESC(debug, "Level of debug output");
54 
55 static int brcmf_p2p_enable;
56 module_param_named(p2pon, brcmf_p2p_enable, int, 0);
57 MODULE_PARM_DESC(p2pon, "Enable legacy p2p management functionality");
58 
59 static int brcmf_feature_disable;
60 module_param_named(feature_disable, brcmf_feature_disable, int, 0);
61 MODULE_PARM_DESC(feature_disable, "Disable features");
62 
63 static char brcmf_firmware_path[BRCMF_FW_ALTPATH_LEN];
64 module_param_string(alternative_fw_path, brcmf_firmware_path,
65 		    BRCMF_FW_ALTPATH_LEN, 0400);
66 MODULE_PARM_DESC(alternative_fw_path, "Alternative firmware path");
67 
68 static int brcmf_fcmode;
69 module_param_named(fcmode, brcmf_fcmode, int, 0);
70 MODULE_PARM_DESC(fcmode, "Mode of firmware signalled flow control");
71 
72 static int brcmf_roamoff;
73 module_param_named(roamoff, brcmf_roamoff, int, 0400);
74 MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
75 
76 static int brcmf_iapp_enable;
77 module_param_named(iapp, brcmf_iapp_enable, int, 0);
78 MODULE_PARM_DESC(iapp, "Enable partial support for the obsoleted Inter-Access Point Protocol");
79 
80 #ifdef DEBUG
81 /* always succeed brcmf_bus_started() */
82 static int brcmf_ignore_probe_fail;
83 module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
84 MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
85 #endif
86 
87 static struct brcmfmac_platform_data *brcmfmac_pdata;
88 struct brcmf_mp_global_t brcmf_mp_global;
89 
brcmf_c_set_joinpref_default(struct brcmf_if * ifp)90 void brcmf_c_set_joinpref_default(struct brcmf_if *ifp)
91 {
92 	struct brcmf_pub *drvr = ifp->drvr;
93 	struct brcmf_join_pref_params join_pref_params[2];
94 	int err;
95 
96 	/* Setup join_pref to select target by RSSI (boost on 5GHz) */
97 	join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
98 	join_pref_params[0].len = 2;
99 	join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
100 	join_pref_params[0].band = WLC_BAND_5G;
101 
102 	join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
103 	join_pref_params[1].len = 2;
104 	join_pref_params[1].rssi_gain = 0;
105 	join_pref_params[1].band = 0;
106 	err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
107 				       sizeof(join_pref_params));
108 	if (err)
109 		bphy_err(drvr, "Set join_pref error (%d)\n", err);
110 }
111 
brcmf_c_download(struct brcmf_if * ifp,u16 flag,struct brcmf_dload_data_le * dload_buf,u32 len,const char * var)112 static int brcmf_c_download(struct brcmf_if *ifp, u16 flag,
113 			    struct brcmf_dload_data_le *dload_buf,
114 			    u32 len, const char *var)
115 {
116 	s32 err;
117 
118 	flag |= (DLOAD_HANDLER_VER << DLOAD_FLAG_VER_SHIFT);
119 	dload_buf->flag = cpu_to_le16(flag);
120 	dload_buf->dload_type = cpu_to_le16(DL_TYPE_CLM);
121 	dload_buf->len = cpu_to_le32(len);
122 	dload_buf->crc = cpu_to_le32(0);
123 
124 	err = brcmf_fil_iovar_data_set(ifp, var, dload_buf,
125 				       struct_size(dload_buf, data, len));
126 
127 	return err;
128 }
129 
brcmf_c_download_blob(struct brcmf_if * ifp,const void * data,size_t size,const char * loadvar,const char * statvar)130 static int brcmf_c_download_blob(struct brcmf_if *ifp,
131 #if defined(__linux__)
132 				 const void *data, size_t size,
133 #elif defined(__FreeBSD__)
134 				 const u8 *data, size_t size,
135 #endif
136 				 const char *loadvar, const char *statvar)
137 {
138 	struct brcmf_pub *drvr = ifp->drvr;
139 	struct brcmf_dload_data_le *chunk_buf;
140 	u32 chunk_len;
141 	u32 datalen;
142 	u32 cumulative_len;
143 	u16 dl_flag = DL_BEGIN;
144 	u32 status;
145 	s32 err;
146 
147 	brcmf_dbg(TRACE, "Enter\n");
148 
149 	chunk_buf = kzalloc_flex(*chunk_buf, data, MAX_CHUNK_LEN);
150 	if (!chunk_buf) {
151 		err = -ENOMEM;
152 		return -ENOMEM;
153 	}
154 
155 	datalen = size;
156 	cumulative_len = 0;
157 	do {
158 		if (datalen > MAX_CHUNK_LEN) {
159 			chunk_len = MAX_CHUNK_LEN;
160 		} else {
161 			chunk_len = datalen;
162 			dl_flag |= DL_END;
163 		}
164 		memcpy(chunk_buf->data, data + cumulative_len, chunk_len);
165 
166 		err = brcmf_c_download(ifp, dl_flag, chunk_buf, chunk_len,
167 				       loadvar);
168 
169 		dl_flag &= ~DL_BEGIN;
170 
171 		cumulative_len += chunk_len;
172 		datalen -= chunk_len;
173 	} while ((datalen > 0) && (err == 0));
174 
175 	if (err) {
176 		bphy_err(drvr, "%s (%zu byte file) failed (%d)\n",
177 			 loadvar, size, err);
178 		/* Retrieve status and print */
179 		err = brcmf_fil_iovar_int_get(ifp, statvar, &status);
180 		if (err)
181 			bphy_err(drvr, "get %s failed (%d)\n", statvar, err);
182 		else
183 			brcmf_dbg(INFO, "%s=%d\n", statvar, status);
184 		err = -EIO;
185 	}
186 
187 	kfree(chunk_buf);
188 	return err;
189 }
190 
brcmf_c_process_clm_blob(struct brcmf_if * ifp)191 static int brcmf_c_process_clm_blob(struct brcmf_if *ifp)
192 {
193 	struct brcmf_pub *drvr = ifp->drvr;
194 	struct brcmf_bus *bus = drvr->bus_if;
195 	const struct firmware *fw = NULL;
196 	s32 err;
197 
198 	brcmf_dbg(TRACE, "Enter\n");
199 
200 	err = brcmf_bus_get_blob(bus, &fw, BRCMF_BLOB_CLM);
201 	if (err || !fw) {
202 		brcmf_info("no clm_blob available (err=%d), device may have limited channels available\n",
203 			   err);
204 		return 0;
205 	}
206 
207 	err = brcmf_c_download_blob(ifp, fw->data, fw->size,
208 				    "clmload", "clmload_status");
209 
210 	release_firmware(fw);
211 	return err;
212 }
213 
brcmf_c_process_txcap_blob(struct brcmf_if * ifp)214 static int brcmf_c_process_txcap_blob(struct brcmf_if *ifp)
215 {
216 	struct brcmf_pub *drvr = ifp->drvr;
217 	struct brcmf_bus *bus = drvr->bus_if;
218 	const struct firmware *fw = NULL;
219 	s32 err;
220 
221 	brcmf_dbg(TRACE, "Enter\n");
222 
223 	err = brcmf_bus_get_blob(bus, &fw, BRCMF_BLOB_TXCAP);
224 	if (err || !fw) {
225 		brcmf_info("no txcap_blob available (err=%d)\n", err);
226 		return 0;
227 	}
228 
229 	brcmf_info("TxCap blob found, loading\n");
230 	err = brcmf_c_download_blob(ifp, fw->data, fw->size,
231 				    "txcapload", "txcapload_status");
232 
233 	release_firmware(fw);
234 	return err;
235 }
236 
brcmf_c_set_cur_etheraddr(struct brcmf_if * ifp,const u8 * addr)237 int brcmf_c_set_cur_etheraddr(struct brcmf_if *ifp, const u8 *addr)
238 {
239 	s32 err;
240 
241 	err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", addr, ETH_ALEN);
242 	if (err < 0)
243 		bphy_err(ifp->drvr, "Setting cur_etheraddr failed, %d\n", err);
244 
245 	return err;
246 }
247 
248 /* On some boards there is no eeprom to hold the nvram, in this case instead
249  * a board specific nvram is loaded from /lib/firmware. On most boards the
250  * macaddr setting in the /lib/firmware nvram file is ignored because the
251  * wifibt chip has a unique MAC programmed into the chip itself.
252  * But in some cases the actual MAC from the /lib/firmware nvram file gets
253  * used, leading to MAC conflicts.
254  * The MAC addresses in the troublesome nvram files seem to all come from
255  * the same nvram file template, so we only need to check for 1 known
256  * address to detect this.
257  */
258 static const u8 brcmf_default_mac_address[ETH_ALEN] = {
259 	0x00, 0x90, 0x4c, 0xc5, 0x12, 0x38
260 };
261 
brcmf_c_process_cal_blob(struct brcmf_if * ifp)262 static int brcmf_c_process_cal_blob(struct brcmf_if *ifp)
263 {
264 	struct brcmf_pub *drvr = ifp->drvr;
265 	struct brcmf_mp_device *settings = drvr->settings;
266 	s32 err;
267 
268 	brcmf_dbg(TRACE, "Enter\n");
269 
270 	if (!settings->cal_blob || !settings->cal_size)
271 		return 0;
272 
273 	brcmf_info("Calibration blob provided by platform, loading\n");
274 	err = brcmf_c_download_blob(ifp, settings->cal_blob, settings->cal_size,
275 				    "calload", "calload_status");
276 	return err;
277 }
278 
brcmf_c_preinit_dcmds(struct brcmf_if * ifp)279 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
280 {
281 	struct brcmf_pub *drvr = ifp->drvr;
282 	struct brcmf_fweh_info *fweh = drvr->fweh;
283 	u8 buf[BRCMF_DCMD_SMLEN];
284 	struct brcmf_bus *bus;
285 	struct brcmf_rev_info_le revinfo;
286 	struct brcmf_rev_info *ri;
287 	char *clmver;
288 	char *ptr;
289 	s32 err;
290 
291 	if (is_valid_ether_addr(ifp->mac_addr)) {
292 		/* set mac address */
293 		err = brcmf_c_set_cur_etheraddr(ifp, ifp->mac_addr);
294 		if (err < 0)
295 			goto done;
296 	} else {
297 		/* retrieve mac address */
298 		err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
299 					       sizeof(ifp->mac_addr));
300 		if (err < 0) {
301 			bphy_err(drvr, "Retrieving cur_etheraddr failed, %d\n", err);
302 			goto done;
303 		}
304 
305 		if (ether_addr_equal_unaligned(ifp->mac_addr, brcmf_default_mac_address)) {
306 			bphy_err(drvr, "Default MAC is used, replacing with random MAC to avoid conflicts\n");
307 			eth_random_addr(ifp->mac_addr);
308 			ifp->ndev->addr_assign_type = NET_ADDR_RANDOM;
309 			err = brcmf_c_set_cur_etheraddr(ifp, ifp->mac_addr);
310 			if (err < 0)
311 				goto done;
312 		}
313 	}
314 
315 	memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
316 	memcpy(ifp->drvr->wiphy->perm_addr, ifp->drvr->mac, ETH_ALEN);
317 
318 	bus = ifp->drvr->bus_if;
319 	ri = &ifp->drvr->revinfo;
320 
321 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
322 				     &revinfo, sizeof(revinfo));
323 	if (err < 0) {
324 		bphy_err(drvr, "retrieving revision info failed, %d\n", err);
325 		strscpy(ri->chipname, "UNKNOWN", sizeof(ri->chipname));
326 	} else {
327 		ri->vendorid = le32_to_cpu(revinfo.vendorid);
328 		ri->deviceid = le32_to_cpu(revinfo.deviceid);
329 		ri->radiorev = le32_to_cpu(revinfo.radiorev);
330 		ri->corerev = le32_to_cpu(revinfo.corerev);
331 		ri->boardid = le32_to_cpu(revinfo.boardid);
332 		ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
333 		ri->boardrev = le32_to_cpu(revinfo.boardrev);
334 		ri->driverrev = le32_to_cpu(revinfo.driverrev);
335 		ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
336 		ri->bus = le32_to_cpu(revinfo.bus);
337 		ri->phytype = le32_to_cpu(revinfo.phytype);
338 		ri->phyrev = le32_to_cpu(revinfo.phyrev);
339 		ri->anarev = le32_to_cpu(revinfo.anarev);
340 		ri->chippkg = le32_to_cpu(revinfo.chippkg);
341 		ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
342 
343 		/* use revinfo if not known yet */
344 		if (!bus->chip) {
345 			bus->chip = le32_to_cpu(revinfo.chipnum);
346 			bus->chiprev = le32_to_cpu(revinfo.chiprev);
347 		}
348 	}
349 	ri->result = err;
350 
351 	if (bus->chip)
352 		brcmf_chip_name(bus->chip, bus->chiprev,
353 				ri->chipname, sizeof(ri->chipname));
354 
355 	/* Do any CLM downloading */
356 	err = brcmf_c_process_clm_blob(ifp);
357 	if (err < 0) {
358 		bphy_err(drvr, "download CLM blob file failed, %d\n", err);
359 		goto done;
360 	}
361 
362 	/* Do TxCap downloading, if needed */
363 	err = brcmf_c_process_txcap_blob(ifp);
364 	if (err < 0) {
365 		bphy_err(drvr, "download TxCap blob file failed, %d\n", err);
366 		goto done;
367 	}
368 
369 	/* Download external calibration blob, if available */
370 	err = brcmf_c_process_cal_blob(ifp);
371 	if (err < 0) {
372 		bphy_err(drvr, "download calibration blob file failed, %d\n", err);
373 		goto done;
374 	}
375 
376 	/* query for 'ver' to get version info from firmware */
377 	memset(buf, 0, sizeof(buf));
378 	err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
379 	if (err < 0) {
380 		bphy_err(drvr, "Retrieving version information failed, %d\n",
381 			 err);
382 		goto done;
383 	}
384 	buf[sizeof(buf) - 1] = '\0';
385 	ptr = (char *)buf;
386 	strsep(&ptr, "\n");
387 
388 	/* Print fw version info */
389 	brcmf_info("Firmware: %s %s\n", ri->chipname, buf);
390 
391 	/* locate firmware version number for ethtool */
392 	ptr = strrchr(buf, ' ');
393 	if (!ptr) {
394 		bphy_err(drvr, "Retrieving version number failed");
395 		goto done;
396 	}
397 	strscpy(ifp->drvr->fwver, ptr + 1, sizeof(ifp->drvr->fwver));
398 
399 	/* Query for 'clmver' to get CLM version info from firmware */
400 	memset(buf, 0, sizeof(buf));
401 	err = brcmf_fil_iovar_data_get(ifp, "clmver", buf, sizeof(buf));
402 	if (err) {
403 		brcmf_dbg(TRACE, "retrieving clmver failed, %d\n", err);
404 	} else {
405 		buf[sizeof(buf) - 1] = '\0';
406 		clmver = (char *)buf;
407 
408 		/* Replace all newline/linefeed characters with space
409 		 * character
410 		 */
411 		strreplace(clmver, '\n', ' ');
412 
413 		/* store CLM version for adding it to revinfo debugfs file */
414 		memcpy(ifp->drvr->clmver, clmver, sizeof(ifp->drvr->clmver));
415 
416 		brcmf_dbg(INFO, "CLM version = %s\n", clmver);
417 	}
418 
419 	/* set mpc */
420 	err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
421 	if (err) {
422 		bphy_err(drvr, "failed setting mpc\n");
423 		goto done;
424 	}
425 
426 	brcmf_c_set_joinpref_default(ifp);
427 
428 	/* Setup event_msgs, enable E_IF */
429 	err = brcmf_fil_iovar_data_get(ifp, "event_msgs", fweh->event_mask,
430 				       fweh->event_mask_len);
431 	if (err) {
432 		bphy_err(drvr, "Get event_msgs error (%d)\n", err);
433 		goto done;
434 	}
435 	/*
436 	 * BRCMF_E_IF can safely be used to set the appropriate bit
437 	 * in the event_mask as the firmware event code is guaranteed
438 	 * to match the value of BRCMF_E_IF because it is old cruft
439 	 * that all vendors have.
440 	 */
441 	setbit(fweh->event_mask, BRCMF_E_IF);
442 	err = brcmf_fil_iovar_data_set(ifp, "event_msgs", fweh->event_mask,
443 				       fweh->event_mask_len);
444 	if (err) {
445 		bphy_err(drvr, "Set event_msgs error (%d)\n", err);
446 		goto done;
447 	}
448 
449 	/* Setup default scan channel time */
450 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
451 				    BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
452 	if (err) {
453 		bphy_err(drvr, "BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
454 			 err);
455 		goto done;
456 	}
457 
458 	/* Setup default scan unassoc time */
459 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
460 				    BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
461 	if (err) {
462 		bphy_err(drvr, "BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
463 			 err);
464 		goto done;
465 	}
466 
467 	/* Enable tx beamforming, errors can be ignored (not supported) */
468 	(void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
469 done:
470 	return err;
471 }
472 
473 #ifndef CONFIG_BRCM_TRACING
__brcmf_err(struct brcmf_bus * bus,const char * func,const char * fmt,...)474 void __brcmf_err(struct brcmf_bus *bus, const char *func, const char *fmt, ...)
475 {
476 	struct va_format vaf;
477 	va_list args;
478 
479 	va_start(args, fmt);
480 
481 	vaf.fmt = fmt;
482 	vaf.va = &args;
483 #if defined(__linux__)
484 	if (bus)
485 		dev_err(bus->dev, "%s: %pV", func, &vaf);
486 	else
487 		pr_err("%s: %pV", func, &vaf);
488 #elif defined(__FreeBSD__)
489 	{
490 	char *str;
491 	vasprintf(&str, M_KMALLOC, vaf.fmt, args);
492 	if (bus)
493 		dev_err(bus->dev, "ERROR: %s: %s", func, str);
494 	else
495 		pr_err("ERROR: %s: %s", func, str);
496 	free(str, M_KMALLOC);
497 	}
498 #endif
499 
500 	va_end(args);
501 }
502 #endif
503 
504 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
__brcmf_dbg(u32 level,const char * func,const char * fmt,...)505 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
506 {
507 	struct va_format vaf = {
508 		.fmt = fmt,
509 	};
510 	va_list args;
511 
512 	va_start(args, fmt);
513 	vaf.va = &args;
514 	if (brcmf_msg_level & level)
515 #if defined(__linux__)
516 		pr_debug("%s %pV", func, &vaf);
517 #elif defined(__FreeBSD__)
518 	{
519 		char *str;
520 		vasprintf(&str, M_KMALLOC, vaf.fmt, args);
521 		pr_debug("%s %s", func, str);
522 		free(str, M_KMALLOC);
523 	}
524 #endif
525 	trace_brcmf_dbg(level, func, &vaf);
526 	va_end(args);
527 }
528 BRCMF_EXPORT_SYMBOL_GPL(__brcmf_dbg);
529 #endif
530 
brcmf_mp_attach(void)531 static void brcmf_mp_attach(void)
532 {
533 	/* If module param firmware path is set then this will always be used,
534 	 * if not set then if available use the platform data version. To make
535 	 * sure it gets initialized at all, always copy the module param version
536 	 */
537 	strscpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
538 		BRCMF_FW_ALTPATH_LEN);
539 	if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
540 	    (brcmf_mp_global.firmware_path[0] == '\0')) {
541 		strscpy(brcmf_mp_global.firmware_path,
542 			brcmfmac_pdata->fw_alternative_path,
543 			BRCMF_FW_ALTPATH_LEN);
544 	}
545 }
546 
brcmf_get_module_param(struct device * dev,enum brcmf_bus_type bus_type,u32 chip,u32 chiprev)547 struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
548 					       enum brcmf_bus_type bus_type,
549 					       u32 chip, u32 chiprev)
550 {
551 	struct brcmf_mp_device *settings;
552 	struct brcmfmac_pd_device *device_pd;
553 	bool found;
554 	int i;
555 
556 	brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
557 		  chiprev);
558 	settings = kzalloc_obj(*settings, GFP_ATOMIC);
559 	if (!settings)
560 		return NULL;
561 
562 	/* start by using the module parameters */
563 	settings->p2p_enable = !!brcmf_p2p_enable;
564 	settings->feature_disable = brcmf_feature_disable;
565 	settings->fcmode = brcmf_fcmode;
566 	settings->roamoff = !!brcmf_roamoff;
567 	settings->iapp = !!brcmf_iapp_enable;
568 #ifdef DEBUG
569 	settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
570 #endif
571 
572 	if (bus_type == BRCMF_BUSTYPE_SDIO)
573 		settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
574 
575 	/* See if there is any device specific platform data configured */
576 	found = false;
577 	if (brcmfmac_pdata) {
578 		for (i = 0; i < brcmfmac_pdata->device_count; i++) {
579 			device_pd = &brcmfmac_pdata->devices[i];
580 			if ((device_pd->bus_type == bus_type) &&
581 			    (device_pd->id == chip) &&
582 			    ((device_pd->rev == chiprev) ||
583 			     (device_pd->rev == -1))) {
584 				brcmf_dbg(INFO, "Platform data for device found\n");
585 				settings->country_codes =
586 						device_pd->country_codes;
587 				if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
588 					memcpy(&settings->bus.sdio,
589 					       &device_pd->bus.sdio,
590 					       sizeof(settings->bus.sdio));
591 				found = true;
592 				break;
593 			}
594 		}
595 	}
596 	if (!found) {
597 		/* No platform data for this device, try OF and DMI data */
598 		brcmf_dmi_probe(settings, chip, chiprev);
599 		if (brcmf_of_probe(dev, bus_type, settings) == -EPROBE_DEFER) {
600 			kfree(settings);
601 			return ERR_PTR(-EPROBE_DEFER);
602 		}
603 		brcmf_acpi_probe(dev, bus_type, settings);
604 	}
605 	return settings;
606 }
607 
brcmf_release_module_param(struct brcmf_mp_device * module_param)608 void brcmf_release_module_param(struct brcmf_mp_device *module_param)
609 {
610 	kfree(module_param);
611 }
612 
brcmf_common_pd_probe(struct platform_device * pdev)613 static int __init brcmf_common_pd_probe(struct platform_device *pdev)
614 {
615 	brcmf_dbg(INFO, "Enter\n");
616 
617 	brcmfmac_pdata = dev_get_platdata(&pdev->dev);
618 
619 	if (brcmfmac_pdata->power_on)
620 		brcmfmac_pdata->power_on();
621 
622 	return 0;
623 }
624 
brcmf_common_pd_remove(struct platform_device * pdev)625 static void brcmf_common_pd_remove(struct platform_device *pdev)
626 {
627 	brcmf_dbg(INFO, "Enter\n");
628 
629 	if (brcmfmac_pdata->power_off)
630 		brcmfmac_pdata->power_off();
631 }
632 
633 static struct platform_driver brcmf_pd = {
634 	.remove		= brcmf_common_pd_remove,
635 	.driver		= {
636 		.name	= BRCMFMAC_PDATA_NAME,
637 	}
638 };
639 
brcmfmac_module_init(void)640 static int __init brcmfmac_module_init(void)
641 {
642 	int err;
643 
644 	/* Get the platform data (if available) for our devices */
645 	err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
646 	if (err == -ENODEV)
647 		brcmf_dbg(INFO, "No platform data available.\n");
648 
649 	/* Initialize global module parameters */
650 	brcmf_mp_attach();
651 
652 	/* Continue the initialization by registering the different busses */
653 	err = brcmf_core_init();
654 	if (err) {
655 		if (brcmfmac_pdata)
656 			platform_driver_unregister(&brcmf_pd);
657 	}
658 
659 	return err;
660 }
661 
brcmfmac_module_exit(void)662 static void __exit brcmfmac_module_exit(void)
663 {
664 	brcmf_core_exit();
665 	if (brcmfmac_pdata)
666 		platform_driver_unregister(&brcmf_pd);
667 }
668 
669 module_init(brcmfmac_module_init);
670 module_exit(brcmfmac_module_exit);
671 
672