1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/netdevice.h>
20 #include <linux/module.h>
21 #include <brcmu_wifi.h>
22 #include <brcmu_utils.h>
23 #include "core.h"
24 #include "bus.h"
25 #include "debug.h"
26 #include "fwil.h"
27 #include "fwil_types.h"
28 #include "tracepoint.h"
29 #include "common.h"
30 #include "of.h"
31 
32 MODULE_AUTHOR("Broadcom Corporation");
33 MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
34 MODULE_LICENSE("Dual BSD/GPL");
35 
36 const u8 ALLFFMAC[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
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, S_IRUSR | S_IWUSR);
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, S_IRUSR);
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, S_IRUSR);
74 MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
75 
76 #ifdef DEBUG
77 /* always succeed brcmf_bus_started() */
78 static int brcmf_ignore_probe_fail;
79 module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
80 MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
81 #endif
82 
83 static struct brcmfmac_platform_data *brcmfmac_pdata;
84 struct brcmf_mp_global_t brcmf_mp_global;
85 
86 void brcmf_c_set_joinpref_default(struct brcmf_if *ifp)
87 {
88 	struct brcmf_join_pref_params join_pref_params[2];
89 	int err;
90 
91 	/* Setup join_pref to select target by RSSI (boost on 5GHz) */
92 	join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
93 	join_pref_params[0].len = 2;
94 	join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
95 	join_pref_params[0].band = WLC_BAND_5G;
96 
97 	join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
98 	join_pref_params[1].len = 2;
99 	join_pref_params[1].rssi_gain = 0;
100 	join_pref_params[1].band = 0;
101 	err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
102 				       sizeof(join_pref_params));
103 	if (err)
104 		brcmf_err("Set join_pref error (%d)\n", err);
105 }
106 
107 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
108 {
109 	s8 eventmask[BRCMF_EVENTING_MASK_LEN];
110 	u8 buf[BRCMF_DCMD_SMLEN];
111 	struct brcmf_rev_info_le revinfo;
112 	struct brcmf_rev_info *ri;
113 	char *ptr;
114 	s32 err;
115 
116 	/* retreive mac address */
117 	err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
118 				       sizeof(ifp->mac_addr));
119 	if (err < 0) {
120 		brcmf_err("Retreiving cur_etheraddr failed, %d\n", err);
121 		goto done;
122 	}
123 	memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
124 
125 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
126 				     &revinfo, sizeof(revinfo));
127 	ri = &ifp->drvr->revinfo;
128 	if (err < 0) {
129 		brcmf_err("retrieving revision info failed, %d\n", err);
130 	} else {
131 		ri->vendorid = le32_to_cpu(revinfo.vendorid);
132 		ri->deviceid = le32_to_cpu(revinfo.deviceid);
133 		ri->radiorev = le32_to_cpu(revinfo.radiorev);
134 		ri->chiprev = le32_to_cpu(revinfo.chiprev);
135 		ri->corerev = le32_to_cpu(revinfo.corerev);
136 		ri->boardid = le32_to_cpu(revinfo.boardid);
137 		ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
138 		ri->boardrev = le32_to_cpu(revinfo.boardrev);
139 		ri->driverrev = le32_to_cpu(revinfo.driverrev);
140 		ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
141 		ri->bus = le32_to_cpu(revinfo.bus);
142 		ri->chipnum = le32_to_cpu(revinfo.chipnum);
143 		ri->phytype = le32_to_cpu(revinfo.phytype);
144 		ri->phyrev = le32_to_cpu(revinfo.phyrev);
145 		ri->anarev = le32_to_cpu(revinfo.anarev);
146 		ri->chippkg = le32_to_cpu(revinfo.chippkg);
147 		ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
148 	}
149 	ri->result = err;
150 
151 	/* query for 'ver' to get version info from firmware */
152 	memset(buf, 0, sizeof(buf));
153 	strcpy(buf, "ver");
154 	err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
155 	if (err < 0) {
156 		brcmf_err("Retreiving version information failed, %d\n",
157 			  err);
158 		goto done;
159 	}
160 	ptr = (char *)buf;
161 	strsep(&ptr, "\n");
162 
163 	/* Print fw version info */
164 	brcmf_err("Firmware version = %s\n", buf);
165 
166 	/* locate firmware version number for ethtool */
167 	ptr = strrchr(buf, ' ') + 1;
168 	strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
169 
170 	/* set mpc */
171 	err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
172 	if (err) {
173 		brcmf_err("failed setting mpc\n");
174 		goto done;
175 	}
176 
177 	brcmf_c_set_joinpref_default(ifp);
178 
179 	/* Setup event_msgs, enable E_IF */
180 	err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
181 				       BRCMF_EVENTING_MASK_LEN);
182 	if (err) {
183 		brcmf_err("Get event_msgs error (%d)\n", err);
184 		goto done;
185 	}
186 	setbit(eventmask, BRCMF_E_IF);
187 	err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
188 				       BRCMF_EVENTING_MASK_LEN);
189 	if (err) {
190 		brcmf_err("Set event_msgs error (%d)\n", err);
191 		goto done;
192 	}
193 
194 	/* Setup default scan channel time */
195 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
196 				    BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
197 	if (err) {
198 		brcmf_err("BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
199 			  err);
200 		goto done;
201 	}
202 
203 	/* Setup default scan unassoc time */
204 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
205 				    BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
206 	if (err) {
207 		brcmf_err("BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
208 			  err);
209 		goto done;
210 	}
211 
212 	/* Enable tx beamforming, errors can be ignored (not supported) */
213 	(void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
214 
215 	/* do bus specific preinit here */
216 	err = brcmf_bus_preinit(ifp->drvr->bus_if);
217 done:
218 	return err;
219 }
220 
221 #ifndef CONFIG_BRCM_TRACING
222 void __brcmf_err(const char *func, const char *fmt, ...)
223 {
224 	struct va_format vaf;
225 	va_list args;
226 
227 	va_start(args, fmt);
228 
229 	vaf.fmt = fmt;
230 	vaf.va = &args;
231 	pr_err("%s: %pV", func, &vaf);
232 
233 	va_end(args);
234 }
235 #endif
236 
237 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
238 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
239 {
240 	struct va_format vaf = {
241 		.fmt = fmt,
242 	};
243 	va_list args;
244 
245 	va_start(args, fmt);
246 	vaf.va = &args;
247 	if (brcmf_msg_level & level)
248 		pr_debug("%s %pV", func, &vaf);
249 	trace_brcmf_dbg(level, func, &vaf);
250 	va_end(args);
251 }
252 #endif
253 
254 static void brcmf_mp_attach(void)
255 {
256 	/* If module param firmware path is set then this will always be used,
257 	 * if not set then if available use the platform data version. To make
258 	 * sure it gets initialized at all, always copy the module param version
259 	 */
260 	strlcpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
261 		BRCMF_FW_ALTPATH_LEN);
262 	if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
263 	    (brcmf_mp_global.firmware_path[0] == '\0')) {
264 		strlcpy(brcmf_mp_global.firmware_path,
265 			brcmfmac_pdata->fw_alternative_path,
266 			BRCMF_FW_ALTPATH_LEN);
267 	}
268 }
269 
270 struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
271 					       enum brcmf_bus_type bus_type,
272 					       u32 chip, u32 chiprev)
273 {
274 	struct brcmf_mp_device *settings;
275 	struct brcmfmac_pd_device *device_pd;
276 	bool found;
277 	int i;
278 
279 	brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
280 		  chiprev);
281 	settings = kzalloc(sizeof(*settings), GFP_ATOMIC);
282 	if (!settings)
283 		return NULL;
284 
285 	/* start by using the module paramaters */
286 	settings->p2p_enable = !!brcmf_p2p_enable;
287 	settings->feature_disable = brcmf_feature_disable;
288 	settings->fcmode = brcmf_fcmode;
289 	settings->roamoff = !!brcmf_roamoff;
290 #ifdef DEBUG
291 	settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
292 #endif
293 
294 	if (bus_type == BRCMF_BUSTYPE_SDIO)
295 		settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
296 
297 	/* See if there is any device specific platform data configured */
298 	found = false;
299 	if (brcmfmac_pdata) {
300 		for (i = 0; i < brcmfmac_pdata->device_count; i++) {
301 			device_pd = &brcmfmac_pdata->devices[i];
302 			if ((device_pd->bus_type == bus_type) &&
303 			    (device_pd->id == chip) &&
304 			    ((device_pd->rev == chiprev) ||
305 			     (device_pd->rev == -1))) {
306 				brcmf_dbg(INFO, "Platform data for device found\n");
307 				settings->country_codes =
308 						device_pd->country_codes;
309 				if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
310 					memcpy(&settings->bus.sdio,
311 					       &device_pd->bus.sdio,
312 					       sizeof(settings->bus.sdio));
313 				found = true;
314 				break;
315 			}
316 		}
317 	}
318 	if (!found) {
319 		/* No platform data for this device, try OF (Open Firwmare) */
320 		brcmf_of_probe(dev, bus_type, settings);
321 	}
322 	return settings;
323 }
324 
325 void brcmf_release_module_param(struct brcmf_mp_device *module_param)
326 {
327 	kfree(module_param);
328 }
329 
330 static int __init brcmf_common_pd_probe(struct platform_device *pdev)
331 {
332 	brcmf_dbg(INFO, "Enter\n");
333 
334 	brcmfmac_pdata = dev_get_platdata(&pdev->dev);
335 
336 	if (brcmfmac_pdata->power_on)
337 		brcmfmac_pdata->power_on();
338 
339 	return 0;
340 }
341 
342 static int brcmf_common_pd_remove(struct platform_device *pdev)
343 {
344 	brcmf_dbg(INFO, "Enter\n");
345 
346 	if (brcmfmac_pdata->power_off)
347 		brcmfmac_pdata->power_off();
348 
349 	return 0;
350 }
351 
352 static struct platform_driver brcmf_pd = {
353 	.remove		= brcmf_common_pd_remove,
354 	.driver		= {
355 		.name	= BRCMFMAC_PDATA_NAME,
356 	}
357 };
358 
359 static int __init brcmfmac_module_init(void)
360 {
361 	int err;
362 
363 	/* Initialize debug system first */
364 	brcmf_debugfs_init();
365 
366 	/* Get the platform data (if available) for our devices */
367 	err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
368 	if (err == -ENODEV)
369 		brcmf_dbg(INFO, "No platform data available.\n");
370 
371 	/* Initialize global module paramaters */
372 	brcmf_mp_attach();
373 
374 	/* Continue the initialization by registering the different busses */
375 	err = brcmf_core_init();
376 	if (err) {
377 		brcmf_debugfs_exit();
378 		if (brcmfmac_pdata)
379 			platform_driver_unregister(&brcmf_pd);
380 	}
381 
382 	return err;
383 }
384 
385 static void __exit brcmfmac_module_exit(void)
386 {
387 	brcmf_core_exit();
388 	if (brcmfmac_pdata)
389 		platform_driver_unregister(&brcmf_pd);
390 	brcmf_debugfs_exit();
391 }
392 
393 module_init(brcmfmac_module_init);
394 module_exit(brcmfmac_module_exit);
395 
396