xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bcmdhd/dhd_linux_platdev.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Linux platform device for DHD WLAN adapter
3  *
4  * Copyright (C) 2020, Broadcom.
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *
21  * <<Broadcom-WL-IPTag/Open:>>
22  *
23  * $Id$
24  */
25 #include <typedefs.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/platform_device.h>
30 #include <bcmutils.h>
31 #include <linux_osl.h>
32 #include <dhd_dbg.h>
33 #include <dngl_stats.h>
34 #include <dhd.h>
35 #include <dhd_bus.h>
36 #include <dhd_linux.h>
37 #if defined(OEM_ANDROID)
38 #include <wl_android.h>
39 #endif
40 #if defined(CONFIG_WIFI_CONTROL_FUNC) || defined(CUSTOMER_HW4)
41 #include <linux/wlan_plat.h>
42 #else
43 #include <dhd_plat.h>
44 #endif /* CONFIG_WIFI_CONTROL_FUNC */
45 #ifdef CONFIG_DTS
46 #include<linux/regulator/consumer.h>
47 #include<linux/of_gpio.h>
48 #endif /* CONFIG_DTS */
49 
50 #if defined(CUSTOMER_HW) || defined(BCMDHD_PLATDEV)
51 extern int dhd_wlan_init_plat_data(wifi_adapter_info_t *adapter);
52 extern void dhd_wlan_deinit_plat_data(wifi_adapter_info_t *adapter);
53 #endif /* CUSTOMER_HW */
54 
55 #define WIFI_PLAT_NAME		"bcmdhd_wlan"
56 #define WIFI_PLAT_NAME2		"bcm4329_wlan"
57 #define WIFI_PLAT_EXT		"bcmdhd_wifi_platform"
58 
59 #ifdef DHD_WIFI_SHUTDOWN
60 extern void wifi_plat_dev_drv_shutdown(struct platform_device *pdev);
61 #endif
62 
63 #ifdef CONFIG_DTS
64 struct regulator *wifi_regulator = NULL;
65 #endif /* CONFIG_DTS */
66 
67 bool cfg_multichip = FALSE;
68 bcmdhd_wifi_platdata_t *dhd_wifi_platdata = NULL;
69 static int wifi_plat_dev_probe_ret = 0;
70 static bool is_power_on = FALSE;
71 /* XXX Some Qualcomm based CUSTOMER_HW4 platforms are using platform
72  * device structure even if the Kernel uses device tree structure.
73  * Therefore, the CONFIG_ARCH_MSM condition is temporarly remained
74  * to support in this case.
75  */
76 #if !defined(CONFIG_DTS)
77 #if defined(DHD_OF_SUPPORT)
78 static bool dts_enabled = TRUE;
79 extern struct resource dhd_wlan_resources;
80 extern struct wifi_platform_data dhd_wlan_control;
81 #else
82 static bool dts_enabled = FALSE;
83 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
84 #pragma GCC diagnostic push
85 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
86 #endif
87 struct resource dhd_wlan_resources = {0};
88 extern struct wifi_platform_data dhd_wlan_control;
89 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
90 #pragma GCC diagnostic pop
91 #endif
92 #endif /* CONFIG_OF && !defined(CONFIG_ARCH_MSM) */
93 #endif /* !defind(CONFIG_DTS) */
94 
95 static int dhd_wifi_platform_load(void);
96 
97 extern void* wl_cfg80211_get_dhdp(struct net_device *dev);
98 
99 #ifdef BCMDHD_MODULAR
100 //extern int dhd_wlan_init(void);
101 //extern int dhd_wlan_deinit(void);
102 #ifdef WBRC
103 extern int wbrc_init(void);
104 extern void wbrc_exit(void);
105 #endif /* WBRC */
106 #endif /* BCMDHD_MODULAR */
107 
108 #ifdef ENABLE_4335BT_WAR
109 extern int bcm_bt_lock(int cookie);
110 extern void bcm_bt_unlock(int cookie);
111 static int lock_cookie_wifi = 'W' | 'i'<<8 | 'F'<<16 | 'i'<<24;	/* cookie is "WiFi" */
112 #endif /* ENABLE_4335BT_WAR */
113 
114 #ifdef BCM4335_XTAL_WAR
115 extern bool check_bcm4335_rev(void);
116 #endif /* BCM4335_XTAL_WAR */
117 
dhd_wifi_platform_attach_adapter(uint32 bus_type,uint32 bus_num,uint32 slot_num,unsigned long status)118 wifi_adapter_info_t* dhd_wifi_platform_attach_adapter(uint32 bus_type,
119 	uint32 bus_num, uint32 slot_num, unsigned long status)
120 {
121 	int i;
122 
123 	if (dhd_wifi_platdata == NULL)
124 		return NULL;
125 
126 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
127 		wifi_adapter_info_t *adapter = &dhd_wifi_platdata->adapters[i];
128 		if ((adapter->bus_type == -1 || adapter->bus_type == bus_type) &&
129 			(adapter->bus_num == -1 || adapter->bus_num == bus_num) &&
130 			(adapter->slot_num == -1 || adapter->slot_num == slot_num)
131 #if defined(ENABLE_INSMOD_NO_FW_LOAD)
132 			&& (wifi_chk_adapter_status(adapter, status))
133 #endif
134 		) {
135 			DHD_ERROR(("attach adapter info '%s'\n", adapter->name));
136 			return adapter;
137 		}
138 	}
139 	return NULL;
140 }
141 
dhd_wifi_platform_get_adapter(uint32 bus_type,uint32 bus_num,uint32 slot_num)142 wifi_adapter_info_t* dhd_wifi_platform_get_adapter(uint32 bus_type, uint32 bus_num, uint32 slot_num)
143 {
144 	int i;
145 
146 	if (dhd_wifi_platdata == NULL)
147 		return NULL;
148 
149 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
150 		wifi_adapter_info_t *adapter = &dhd_wifi_platdata->adapters[i];
151 		if ((adapter->bus_type == -1 || adapter->bus_type == bus_type) &&
152 			(adapter->bus_num == -1 || adapter->bus_num == bus_num) &&
153 			(adapter->slot_num == -1 || adapter->slot_num == slot_num)) {
154 			DHD_TRACE(("found adapter info '%s'\n", adapter->name));
155 			return adapter;
156 		}
157 	}
158 	return NULL;
159 }
160 
wifi_platform_prealloc(wifi_adapter_info_t * adapter,int section,unsigned long size)161 void* wifi_platform_prealloc(wifi_adapter_info_t *adapter, int section, unsigned long size)
162 {
163 	void *alloc_ptr = NULL;
164 	struct wifi_platform_data *plat_data;
165 
166 	if (!adapter || !adapter->wifi_plat_data)
167 		return NULL;
168 	plat_data = adapter->wifi_plat_data;
169 	if (plat_data->mem_prealloc) {
170 #if defined(BCMDHD_MDRIVER) && !defined(DHD_STATIC_IN_DRIVER)
171 		alloc_ptr = plat_data->mem_prealloc(adapter->bus_type, adapter->index, section, size);
172 #else
173 		alloc_ptr = plat_data->mem_prealloc(section, size);
174 #endif
175 		if (alloc_ptr) {
176 			DHD_INFO(("success alloc section %d\n", section));
177 			if (size != 0L)
178 				bzero(alloc_ptr, size);
179 			return alloc_ptr;
180 		}
181 	} else
182 		return NULL;
183 
184 	DHD_ERROR(("%s: failed to alloc static mem section %d\n", __FUNCTION__, section));
185 	return NULL;
186 }
187 
wifi_platform_get_prealloc_func_ptr(wifi_adapter_info_t * adapter)188 void* wifi_platform_get_prealloc_func_ptr(wifi_adapter_info_t *adapter)
189 {
190 	struct wifi_platform_data *plat_data;
191 
192 	if (!adapter || !adapter->wifi_plat_data)
193 		return NULL;
194 	plat_data = adapter->wifi_plat_data;
195 	return plat_data->mem_prealloc;
196 }
197 
wifi_platform_get_irq_number(wifi_adapter_info_t * adapter,unsigned long * irq_flags_ptr)198 int wifi_platform_get_irq_number(wifi_adapter_info_t *adapter, unsigned long *irq_flags_ptr)
199 {
200 	if (adapter == NULL)
201 		return -1;
202 	if (irq_flags_ptr)
203 		*irq_flags_ptr = adapter->intr_flags;
204 	return adapter->irq_num;
205 }
206 
wifi_platform_set_power(wifi_adapter_info_t * adapter,bool on,unsigned long msec)207 int wifi_platform_set_power(wifi_adapter_info_t *adapter, bool on, unsigned long msec)
208 {
209 	int err = 0;
210 #ifndef CONFIG_DTS
211 	struct wifi_platform_data *plat_data;
212 #endif
213 #ifdef BT_OVER_SDIO
214 	if (is_power_on == on) {
215 		return -EINVAL;
216 	}
217 #endif /* BT_OVER_SDIO */
218 	if (on) {
219 		wifi_set_adapter_status(adapter, WIFI_STATUS_POWER_ON);
220 	} else {
221 		wifi_clr_adapter_status(adapter, WIFI_STATUS_POWER_ON);
222 	}
223 #ifdef CONFIG_DTS
224 	if (on) {
225 		printf("======== PULL WL_REG_ON HIGH! ========\n");
226 		err = regulator_enable(wifi_regulator);
227 		is_power_on = TRUE;
228 	}
229 	else {
230 		printf("======== PULL WL_REG_ON LOW! ========\n");
231 		err = regulator_disable(wifi_regulator);
232 		is_power_on = FALSE;
233 	}
234 	if (err < 0) {
235 		DHD_ERROR(("%s: regulator enable/disable failed", __FUNCTION__));
236 		goto fail;
237 	}
238 #else
239 	if (!adapter || !adapter->wifi_plat_data) {
240 		err = -EINVAL;
241 		goto fail;
242 	}
243 	plat_data = adapter->wifi_plat_data;
244 
245 	DHD_ERROR(("%s = %d, delay: %lu msec\n", __FUNCTION__, on, msec));
246 	if (plat_data->set_power) {
247 #ifdef ENABLE_4335BT_WAR
248 		if (on) {
249 			printk("WiFi: trying to acquire BT lock\n");
250 			if (bcm_bt_lock(lock_cookie_wifi) != 0)
251 				printk("** WiFi: timeout in acquiring bt lock**\n");
252 			printk("%s: btlock acquired\n", __FUNCTION__);
253 		}
254 		else {
255 			/* For a exceptional case, release btlock */
256 			bcm_bt_unlock(lock_cookie_wifi);
257 		}
258 #endif /* ENABLE_4335BT_WAR */
259 
260 		err = plat_data->set_power(on, adapter);
261 	}
262 
263 	if (msec && !err)
264 		OSL_SLEEP(msec);
265 
266 	if (on && !err)
267 		is_power_on = TRUE;
268 	else
269 		is_power_on = FALSE;
270 
271 #endif /* CONFIG_DTS */
272 
273 	return err;
274 fail:
275 	if (on) {
276 		wifi_clr_adapter_status(adapter, WIFI_STATUS_POWER_ON);
277 	} else {
278 		wifi_set_adapter_status(adapter, WIFI_STATUS_POWER_ON);
279 	}
280 	return err;
281 }
282 
wifi_platform_bus_enumerate(wifi_adapter_info_t * adapter,bool device_present)283 int wifi_platform_bus_enumerate(wifi_adapter_info_t *adapter, bool device_present)
284 {
285 	int err = 0;
286 	struct wifi_platform_data *plat_data;
287 
288 	if (!adapter || !adapter->wifi_plat_data)
289 		return -EINVAL;
290 	plat_data = adapter->wifi_plat_data;
291 
292 	DHD_ERROR(("%s device present %d\n", __FUNCTION__, device_present));
293 	if (plat_data->set_carddetect) {
294 		err = plat_data->set_carddetect(device_present);
295 	}
296 	return err;
297 
298 }
299 
wifi_platform_get_mac_addr(wifi_adapter_info_t * adapter,unsigned char * buf,int ifidx)300 int wifi_platform_get_mac_addr(wifi_adapter_info_t *adapter, unsigned char *buf,
301 	int ifidx)
302 {
303 	struct wifi_platform_data *plat_data;
304 
305 	DHD_ERROR(("%s\n", __FUNCTION__));
306 	if (!buf || !adapter || !adapter->wifi_plat_data)
307 		return -EINVAL;
308 	plat_data = adapter->wifi_plat_data;
309 	if (plat_data->get_mac_addr) {
310 		return plat_data->get_mac_addr(buf, ifidx);
311 	}
312 	return -EOPNOTSUPP;
313 }
314 
315 #ifdef DHD_COREDUMP
wifi_platform_set_coredump(wifi_adapter_info_t * adapter,const char * buf,int buf_len,const char * info)316 int wifi_platform_set_coredump(wifi_adapter_info_t *adapter, const char *buf,
317 	int buf_len, const char *info)
318 {
319 	struct wifi_platform_data *plat_data;
320 
321 	DHD_ERROR(("%s\n", __FUNCTION__));
322 	if (!buf || !adapter || !adapter->wifi_plat_data)
323 		return -EINVAL;
324 	plat_data = adapter->wifi_plat_data;
325 	if (plat_data->set_coredump) {
326 		return plat_data->set_coredump(buf, buf_len, info);
327 	}
328 	return -EOPNOTSUPP;
329 }
330 #endif /* DHD_COREDUMP */
331 
332 void *
333 #ifdef CUSTOM_COUNTRY_CODE
wifi_platform_get_country_code(wifi_adapter_info_t * adapter,char * ccode,u32 flags)334 wifi_platform_get_country_code(wifi_adapter_info_t *adapter, char *ccode, u32 flags)
335 #else
336 wifi_platform_get_country_code(wifi_adapter_info_t *adapter, char *ccode)
337 #endif /* CUSTOM_COUNTRY_CODE */
338 {
339 	/* get_country_code was added after 2.6.39 */
340 #if	(LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
341 	struct wifi_platform_data *plat_data;
342 
343 	if (!ccode || !adapter || !adapter->wifi_plat_data)
344 		return NULL;
345 	plat_data = adapter->wifi_plat_data;
346 
347 	DHD_TRACE(("%s\n", __FUNCTION__));
348 	if (plat_data->get_country_code) {
349 #ifdef CUSTOM_FORCE_NODFS_FLAG
350 		return plat_data->get_country_code(ccode, flags);
351 #else
352 		return plat_data->get_country_code(ccode);
353 #endif /* CUSTOM_COUNTRY_CODE */
354 	}
355 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) */
356 
357 	return NULL;
358 }
359 
360 #ifndef CUSTOMER_HW
wifi_plat_dev_drv_probe(struct platform_device * pdev)361 static int wifi_plat_dev_drv_probe(struct platform_device *pdev)
362 {
363 	struct resource *resource;
364 	wifi_adapter_info_t *adapter;
365 #if defined(CONFIG_DTS) && defined(CUSTOMER_OOB)
366 	int irq, gpio;
367 #endif /* CONFIG_DTS */
368 
369 	/* Android style wifi platform data device ("bcmdhd_wlan" or "bcm4329_wlan")
370 	 * is kept for backward compatibility and supports only 1 adapter
371 	 */
372 	ASSERT(dhd_wifi_platdata != NULL);
373 	ASSERT(dhd_wifi_platdata->num_adapters == 1);
374 	adapter = &dhd_wifi_platdata->adapters[0];
375 #if defined(CONFIG_WIFI_CONTROL_FUNC)
376 	adapter->wifi_plat_data = (struct wifi_platform_data *)(pdev->dev.platform_data);
377 #else
378 	adapter->wifi_plat_data = (void *)&dhd_wlan_control;
379 #endif
380 
381 #ifdef BCMDHD_PLATDEV
382 	adapter->pdev = pdev;
383 	wifi_plat_dev_probe_ret = dhd_wlan_init_plat_data(adapter);
384 	if (!wifi_plat_dev_probe_ret)
385 		wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
386 	return wifi_plat_dev_probe_ret;
387 #endif
388 
389 	resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcmdhd_wlan_irq");
390 	if (resource == NULL)
391 		resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcm4329_wlan_irq");
392 	if (resource) {
393 		adapter->irq_num = resource->start;
394 		adapter->intr_flags = resource->flags & IRQF_TRIGGER_MASK;
395 #ifdef DHD_ISR_NO_SUSPEND
396 		adapter->intr_flags |= IRQF_NO_SUSPEND;
397 #endif
398 	}
399 
400 #ifdef CONFIG_DTS
401 	wifi_regulator = regulator_get(&pdev->dev, "wlreg_on");
402 	if (wifi_regulator == NULL) {
403 		DHD_ERROR(("%s regulator is null\n", __FUNCTION__));
404 		return -1;
405 	}
406 
407 #if defined(CUSTOMER_OOB)
408 	/* This is to get the irq for the OOB */
409 	gpio = of_get_gpio(pdev->dev.of_node, 0);
410 
411 	if (gpio < 0) {
412 		DHD_ERROR(("%s gpio information is incorrect\n", __FUNCTION__));
413 		return -1;
414 	}
415 	irq = gpio_to_irq(gpio);
416 	if (irq < 0) {
417 		DHD_ERROR(("%s irq information is incorrect\n", __FUNCTION__));
418 		return -1;
419 	}
420 	adapter->irq_num = irq;
421 
422 	/* need to change the flags according to our requirement */
423 #ifdef HW_OOB
424 	adapter->intr_flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
425 		IORESOURCE_IRQ_SHAREABLE;
426 #else
427 	adapter->intr_flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE |
428 		IORESOURCE_IRQ_SHAREABLE;
429 #endif
430 #endif
431 #endif /* CONFIG_DTS */
432 
433 	wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
434 	return wifi_plat_dev_probe_ret;
435 }
436 
wifi_plat_dev_drv_remove(struct platform_device * pdev)437 static int wifi_plat_dev_drv_remove(struct platform_device *pdev)
438 {
439 	wifi_adapter_info_t *adapter;
440 
441 	/* Android style wifi platform data device ("bcmdhd_wlan" or "bcm4329_wlan")
442 	 * is kept for backward compatibility and supports only 1 adapter
443 	 */
444 	ASSERT(dhd_wifi_platdata != NULL);
445 	ASSERT(dhd_wifi_platdata->num_adapters == 1);
446 	adapter = &dhd_wifi_platdata->adapters[0];
447 	if (is_power_on) {
448 #ifdef BCMPCIE
449 		wifi_platform_bus_enumerate(adapter, FALSE);
450 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
451 #else
452 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
453 		wifi_platform_bus_enumerate(adapter, FALSE);
454 #endif /* BCMPCIE */
455 	}
456 
457 #ifdef CONFIG_DTS
458 	regulator_put(wifi_regulator);
459 #endif /* CONFIG_DTS */
460 #ifdef BCMDHD_PLATDEV
461 	dhd_wlan_deinit_plat_data(adapter);
462 #endif
463 	return 0;
464 }
465 
wifi_plat_dev_drv_suspend(struct platform_device * pdev,pm_message_t state)466 static int wifi_plat_dev_drv_suspend(struct platform_device *pdev, pm_message_t state)
467 {
468 	DHD_TRACE(("##> %s\n", __FUNCTION__));
469 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) && \
470 	defined(BCMSDIO)
471 	bcmsdh_oob_intr_set(0);
472 #endif /* (OOB_INTR_ONLY) */
473 	return 0;
474 }
475 
wifi_plat_dev_drv_resume(struct platform_device * pdev)476 static int wifi_plat_dev_drv_resume(struct platform_device *pdev)
477 {
478 	DHD_TRACE(("##> %s\n", __FUNCTION__));
479 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) && \
480 	defined(BCMSDIO)
481 	if (dhd_os_check_if_up(wl_cfg80211_get_dhdp()))
482 		bcmsdh_oob_intr_set(1);
483 #endif /* (OOB_INTR_ONLY) */
484 	return 0;
485 }
486 
487 #ifdef CONFIG_DTS
488 static const struct of_device_id wifi_device_dt_match[] = {
489 	{ .compatible = "android,bcmdhd_wlan", },
490 	{},
491 };
492 #endif /* CONFIG_DTS */
493 
494 static struct platform_driver wifi_platform_dev_driver = {
495 	.probe          = wifi_plat_dev_drv_probe,
496 	.remove         = wifi_plat_dev_drv_remove,
497 	.suspend        = wifi_plat_dev_drv_suspend,
498 	.resume         = wifi_plat_dev_drv_resume,
499 #ifdef DHD_WIFI_SHUTDOWN
500 	.shutdown       = wifi_plat_dev_drv_shutdown,
501 #endif /* DHD_WIFI_SHUTDOWN */
502 	.driver         = {
503 	.name   = WIFI_PLAT_NAME,
504 #ifdef CONFIG_DTS
505 	.of_match_table = wifi_device_dt_match,
506 #endif /* CONFIG_DTS */
507 	}
508 };
509 
510 static struct platform_driver wifi_platform_dev_driver_legacy = {
511 	.probe          = wifi_plat_dev_drv_probe,
512 	.remove         = wifi_plat_dev_drv_remove,
513 	.suspend        = wifi_plat_dev_drv_suspend,
514 	.resume         = wifi_plat_dev_drv_resume,
515 #ifdef DHD_WIFI_SHUTDOWN
516 	.shutdown       = wifi_plat_dev_drv_shutdown,
517 #endif /* DHD_WIFI_SHUTDOWN */
518 	.driver         = {
519 	.name	= WIFI_PLAT_NAME2,
520 	}
521 };
522 
523 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 3, 0)
wifi_platdev_match(struct device * dev,const void * data)524 static int wifi_platdev_match(struct device *dev, const void *data)
525 #else
526 static int wifi_platdev_match(struct device *dev, void *data)
527 #endif /* LINUX_VER >= 5.3.0 */
528 {
529 	char *name = (char*)data;
530 	const struct platform_device *pdev;
531 	GCC_DIAGNOSTIC_PUSH_SUPPRESS_CAST();
532 	pdev = to_platform_device(dev);
533 	GCC_DIAGNOSTIC_POP();
534 
535 	if (strcmp(pdev->name, name) == 0) {
536 		DHD_ERROR(("found wifi platform device %s\n", name));
537 		return TRUE;
538 	}
539 
540 	return FALSE;
541 }
542 #endif
543 
wifi_ctrlfunc_register_drv(void)544 static int wifi_ctrlfunc_register_drv(void)
545 {
546 	wifi_adapter_info_t *adapter;
547 
548 #ifndef CUSTOMER_HW
549 	int err = 0;
550 	struct device *dev1, *dev2;
551 	dev1 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME, wifi_platdev_match);
552 	dev2 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME2, wifi_platdev_match);
553 #endif
554 
555 #ifdef BCMDHD_MODULAR
556 //	dhd_wlan_init();
557 #ifdef WBRC
558 	wbrc_init();
559 #endif /* WBRC */
560 #endif /* BCMDHD_MODULAR */
561 
562 #if !defined(CONFIG_DTS) && !defined(CUSTOMER_HW)
563 	if (!dts_enabled) {
564 		if (dev1 == NULL && dev2 == NULL) {
565 			DHD_ERROR(("no wifi platform data, skip\n"));
566 			return -ENXIO;
567 		}
568 	}
569 #endif /* !defined(CONFIG_DTS) */
570 
571 	/* multi-chip support not enabled, build one adapter information for
572 	 * DHD (either SDIO, USB or PCIe)
573 	 */
574 	adapter = kzalloc(sizeof(wifi_adapter_info_t), GFP_KERNEL);
575 	if (adapter == NULL) {
576 		DHD_ERROR(("%s:adapter alloc failed", __FUNCTION__));
577 		return -ENOMEM;
578 	}
579 	adapter->name = "DHD generic adapter";
580 	adapter->index = 0;
581 	adapter->bus_type = -1;
582 	adapter->bus_num = -1;
583 	adapter->slot_num = -1;
584 	adapter->irq_num = -1;
585 	is_power_on = FALSE;
586 	wifi_plat_dev_probe_ret = 0;
587 	dhd_wifi_platdata = kzalloc(sizeof(bcmdhd_wifi_platdata_t), GFP_KERNEL);
588 	dhd_wifi_platdata->num_adapters = 1;
589 	dhd_wifi_platdata->adapters = adapter;
590 	init_waitqueue_head(&adapter->status_event);
591 
592 #ifndef CUSTOMER_HW
593 	if (dev1) {
594 		err = platform_driver_register(&wifi_platform_dev_driver);
595 		if (err) {
596 			DHD_ERROR(("%s: failed to register wifi ctrl func driver\n",
597 				__FUNCTION__));
598 			return err;
599 		}
600 	}
601 	if (dev2) {
602 		err = platform_driver_register(&wifi_platform_dev_driver_legacy);
603 		if (err) {
604 			DHD_ERROR(("%s: failed to register wifi ctrl func legacy driver\n",
605 				__FUNCTION__));
606 			return err;
607 		}
608 	}
609 #endif
610 
611 #if !defined(CONFIG_DTS)
612 	if (dts_enabled) {
613 		adapter->wifi_plat_data = (void *)&dhd_wlan_control;
614 #ifdef CUSTOMER_HW
615 		wifi_plat_dev_probe_ret = dhd_wlan_init_plat_data(adapter);
616 		if (wifi_plat_dev_probe_ret)
617 			return wifi_plat_dev_probe_ret;
618 #endif
619 #ifdef DHD_ISR_NO_SUSPEND
620 		adapter->intr_flags |= IRQF_NO_SUSPEND;
621 #endif
622 		wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
623 	}
624 #endif /* !defined(CONFIG_DTS) */
625 
626 #if defined(CONFIG_DTS) && !defined(CUSTOMER_HW)
627 	wifi_plat_dev_probe_ret = platform_driver_register(&wifi_platform_dev_driver);
628 #endif /* CONFIG_DTS */
629 
630 	/* return probe function's return value if registeration succeeded */
631 	return wifi_plat_dev_probe_ret;
632 }
633 
wifi_ctrlfunc_unregister_drv(void)634 void wifi_ctrlfunc_unregister_drv(void)
635 {
636 #ifndef CONFIG_DTS
637 	wifi_adapter_info_t *adapter = NULL;
638 #endif
639 
640 #if defined(CONFIG_DTS) && !defined(CUSTOMER_HW)
641 	DHD_ERROR(("unregister wifi platform drivers\n"));
642 	platform_driver_unregister(&wifi_platform_dev_driver);
643 #else
644 #ifndef CUSTOMER_HW
645 	struct device *dev1, *dev2;
646 	dev1 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME, wifi_platdev_match);
647 	dev2 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME2, wifi_platdev_match);
648 	if (!dts_enabled)
649 		if (dev1 == NULL && dev2 == NULL)
650 			return;
651 #endif
652 	DHD_ERROR(("unregister wifi platform drivers\n"));
653 #ifndef CUSTOMER_HW
654 	if (dev1)
655 		platform_driver_unregister(&wifi_platform_dev_driver);
656 	if (dev2)
657 		platform_driver_unregister(&wifi_platform_dev_driver_legacy);
658 #endif
659 	if (dts_enabled) {
660 		adapter = &dhd_wifi_platdata->adapters[0];
661 		if (is_power_on) {
662 			wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
663 		}
664 		wifi_platform_bus_enumerate(adapter, FALSE);
665 	}
666 #ifdef BCMDHD_MODULAR
667 //	dhd_wlan_deinit();
668 #ifdef WBRC
669 	wbrc_exit();
670 #endif /* WBRC */
671 #endif /* BCMDHD_MODULAR */
672 
673 #endif /* !defined(CONFIG_DTS) */
674 
675 #if defined(CUSTOMER_HW)
676 	dhd_wlan_deinit_plat_data(adapter);
677 #endif
678 
679 	kfree(dhd_wifi_platdata->adapters);
680 	dhd_wifi_platdata->adapters = NULL;
681 	dhd_wifi_platdata->num_adapters = 0;
682 	kfree(dhd_wifi_platdata);
683 	dhd_wifi_platdata = NULL;
684 }
685 
686 #ifndef CUSTOMER_HW
bcmdhd_wifi_plat_dev_drv_probe(struct platform_device * pdev)687 static int bcmdhd_wifi_plat_dev_drv_probe(struct platform_device *pdev)
688 {
689 	dhd_wifi_platdata = (bcmdhd_wifi_platdata_t *)(pdev->dev.platform_data);
690 
691 	return dhd_wifi_platform_load();
692 }
693 
bcmdhd_wifi_plat_dev_drv_remove(struct platform_device * pdev)694 static int bcmdhd_wifi_plat_dev_drv_remove(struct platform_device *pdev)
695 {
696 	int i;
697 	wifi_adapter_info_t *adapter;
698 	ASSERT(dhd_wifi_platdata != NULL);
699 
700 	/* power down all adapters */
701 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
702 		adapter = &dhd_wifi_platdata->adapters[i];
703 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
704 		wifi_platform_bus_enumerate(adapter, FALSE);
705 	}
706 	return 0;
707 }
708 
709 static struct platform_driver dhd_wifi_platform_dev_driver = {
710 	.probe          = bcmdhd_wifi_plat_dev_drv_probe,
711 	.remove         = bcmdhd_wifi_plat_dev_drv_remove,
712 	.driver         = {
713 	.name   = WIFI_PLAT_EXT,
714 	}
715 };
716 #endif
717 
dhd_wifi_platform_register_drv(void)718 int dhd_wifi_platform_register_drv(void)
719 {
720 	int err = 0;
721 #ifndef CUSTOMER_HW
722 	struct device *dev;
723 
724 	/* register Broadcom wifi platform data driver if multi-chip is enabled,
725 	 * otherwise use Android style wifi platform data (aka wifi control function)
726 	 * if it exists
727 	 *
728 	 * to support multi-chip DHD, Broadcom wifi platform data device must
729 	 * be added in kernel early boot (e.g. board config file).
730 	 */
731 	if (cfg_multichip) {
732 		dev = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_EXT, wifi_platdev_match);
733 		if (dev == NULL) {
734 			DHD_ERROR(("bcmdhd wifi platform data device not found!!\n"));
735 			return -ENXIO;
736 		}
737 		err = platform_driver_register(&dhd_wifi_platform_dev_driver);
738 	} else
739 #endif
740 	{
741 		err = wifi_ctrlfunc_register_drv();
742 
743 		/* no wifi ctrl func either, load bus directly and ignore this error */
744 		if (err) {
745 			if (err == -ENXIO) {
746 				/* wifi ctrl function does not exist */
747 				err = dhd_wifi_platform_load();
748 			} else {
749 				/* unregister driver due to initialization failure */
750 				wifi_ctrlfunc_unregister_drv();
751 			}
752 		}
753 	}
754 
755 	return err;
756 }
757 
758 #ifdef BCMPCIE
dhd_wifi_platform_load_pcie(void)759 static int dhd_wifi_platform_load_pcie(void)
760 {
761 	int err = 0;
762 	int i;
763 	wifi_adapter_info_t *adapter;
764 
765 	BCM_REFERENCE(i);
766 	BCM_REFERENCE(adapter);
767 
768 	if (dhd_wifi_platdata == NULL) {
769 		/* XXX For x86 Bringup PC or BRIX */
770 		err = dhd_bus_register();
771 	} else {
772 #ifdef DHD_SUPPORT_HDM
773 		if (dhd_download_fw_on_driverload || hdm_trigger_init)
774 #else
775 		if (dhd_download_fw_on_driverload)
776 #endif /* DHD_SUPPORT_HDM */
777 		{
778 			/* power up all adapters */
779 			for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
780 				int retry = POWERUP_MAX_RETRY;
781 				adapter = &dhd_wifi_platdata->adapters[i];
782 
783 				DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
784 				DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
785 					adapter->irq_num, adapter->intr_flags, adapter->fw_path,
786 					adapter->nv_path));
787 				DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
788 					adapter->bus_type, adapter->bus_num, adapter->slot_num));
789 
790 				do {
791 					err = wifi_platform_set_power(adapter,
792 						TRUE, WIFI_TURNON_DELAY);
793 					if (err) {
794 						DHD_ERROR(("failed to power up %s,"
795 							" %d retry left\n",
796 							adapter->name, retry));
797 						/* WL_REG_ON state unknown, Power off forcely */
798 						wifi_platform_set_power(adapter,
799 							FALSE, WIFI_TURNOFF_DELAY);
800 						continue;
801 					} else {
802 						err = wifi_platform_bus_enumerate(adapter, TRUE);
803 						if (err) {
804 							DHD_ERROR(("failed to enumerate bus %s, "
805 								"%d retry left\n",
806 								adapter->name, retry));
807 							wifi_platform_set_power(adapter, FALSE,
808 								WIFI_TURNOFF_DELAY);
809 						} else {
810 							break;
811 						}
812 					}
813 				} while (retry--);
814 
815 				if (retry < 0) {
816 					DHD_ERROR(("failed to power up %s, max retry reached**\n",
817 						adapter->name));
818 					return -ENODEV;
819 				}
820 			}
821 		}
822 
823 		err = dhd_bus_register();
824 
825 		if (err) {
826 			DHD_ERROR(("%s: pcie_register_driver failed\n", __FUNCTION__));
827 			if (dhd_download_fw_on_driverload) {
828 				/* power down all adapters */
829 				for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
830 					adapter = &dhd_wifi_platdata->adapters[i];
831 					wifi_platform_bus_enumerate(adapter, FALSE);
832 					wifi_platform_set_power(adapter,
833 						FALSE, WIFI_TURNOFF_DELAY);
834 				}
835 			}
836 		}
837 	}
838 
839 	return err;
840 }
841 #else
dhd_wifi_platform_load_pcie(void)842 static int dhd_wifi_platform_load_pcie(void)
843 {
844 	return 0;
845 }
846 #endif /* BCMPCIE  */
847 
dhd_wifi_platform_unregister_drv(void)848 void dhd_wifi_platform_unregister_drv(void)
849 {
850 #ifndef CUSTOMER_HW
851 	if (cfg_multichip)
852 		platform_driver_unregister(&dhd_wifi_platform_dev_driver);
853 	else
854 #endif
855 		wifi_ctrlfunc_unregister_drv();
856 }
857 
858 extern int dhd_watchdog_prio;
859 extern int dhd_dpc_prio;
860 extern uint dhd_deferred_tx;
861 #if defined(OEM_ANDROID) && (defined(BCMLXSDMMC) || defined(BCMDBUS))
862 extern struct semaphore dhd_registration_sem;
863 #endif /* defined(OEM_ANDROID) && defined(BCMLXSDMMC) */
864 
865 #ifdef BCMSDIO
dhd_wifi_platform_load_sdio(void)866 static int dhd_wifi_platform_load_sdio(void)
867 {
868 	int i;
869 	int err = 0;
870 	wifi_adapter_info_t *adapter;
871 
872 	BCM_REFERENCE(i);
873 	BCM_REFERENCE(adapter);
874 	/* Sanity check on the module parameters
875 	 * - Both watchdog and DPC as tasklets are ok
876 	 * - If both watchdog and DPC are threads, TX must be deferred
877 	 */
878 	if (!(dhd_watchdog_prio < 0 && dhd_dpc_prio < 0) &&
879 		!(dhd_watchdog_prio >= 0 && dhd_dpc_prio >= 0 && dhd_deferred_tx))
880 		return -EINVAL;
881 
882 #if defined(OEM_ANDROID) && defined(BCMLXSDMMC) && !defined(DHD_PRELOAD)
883 	sema_init(&dhd_registration_sem, 0);
884 #endif
885 
886 	if (dhd_wifi_platdata == NULL) {
887 		DHD_ERROR(("DHD wifi platform data is required for Android build\n"));
888 		DHD_ERROR(("DHD registering bus directly\n"));
889 		/* x86 bring-up PC needs no power-up operations */
890 		err = dhd_bus_register();
891 		return err;
892 	}
893 
894 #if defined(OEM_ANDROID) && defined(BCMLXSDMMC) && !defined(DHD_PRELOAD)
895 	/* power up all adapters */
896 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
897 		bool chip_up = FALSE;
898 		int retry = POWERUP_MAX_RETRY;
899 #ifndef DHD_INSMOD_NOWAIT
900 		struct semaphore dhd_chipup_sem;
901 #endif
902 
903 		adapter = &dhd_wifi_platdata->adapters[i];
904 
905 		DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
906 		DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
907 			adapter->irq_num, adapter->intr_flags, adapter->fw_path, adapter->nv_path));
908 		DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
909 			adapter->bus_type, adapter->bus_num, adapter->slot_num));
910 
911 		do {
912 #ifdef DHD_INSMOD_NOWAIT
913 			err = wifi_platform_set_power(adapter, TRUE, WIFI_TURNON_DELAY);
914 			if (err) {
915 				DHD_ERROR(("%s: wifi pwr on error ! \n", __FUNCTION__));
916 				wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
917 				continue;
918 			} else {
919 				wifi_platform_bus_enumerate(adapter, TRUE);
920 				chip_up = TRUE;
921 				break;
922 			}
923 #else
924 			sema_init(&dhd_chipup_sem, 0);
925 			err = dhd_bus_reg_sdio_notify(&dhd_chipup_sem);
926 			if (err) {
927 				DHD_ERROR(("%s dhd_bus_reg_sdio_notify fail(%d)\n\n",
928 					__FUNCTION__, err));
929 				return err;
930 			}
931 			err = wifi_platform_set_power(adapter, TRUE, WIFI_TURNON_DELAY);
932 			if (err) {
933 				DHD_ERROR(("%s: wifi pwr on error ! \n", __FUNCTION__));
934 				dhd_bus_unreg_sdio_notify();
935 				/* WL_REG_ON state unknown, Power off forcely */
936 				wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
937 				continue;
938 			} else {
939 				wifi_platform_bus_enumerate(adapter, TRUE);
940 			}
941 
942 			if (down_timeout(&dhd_chipup_sem, msecs_to_jiffies(POWERUP_WAIT_MS)) == 0) {
943 				dhd_bus_unreg_sdio_notify();
944 				chip_up = TRUE;
945 				break;
946 			}
947 
948 			DHD_ERROR(("failed to power up %s, %d retry left\n", adapter->name, retry));
949 			dhd_bus_unreg_sdio_notify();
950 			wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
951 			wifi_platform_bus_enumerate(adapter, FALSE);
952 #endif
953 		} while (retry--);
954 
955 		if (!chip_up) {
956 			DHD_ERROR(("failed to power up %s, max retry reached**\n", adapter->name));
957 			return -ENODEV;
958 		}
959 
960 	}
961 
962 	err = dhd_bus_register();
963 
964 	if (err) {
965 		DHD_ERROR(("%s: sdio_register_driver failed\n", __FUNCTION__));
966 		goto fail;
967 	}
968 
969 #ifndef DHD_INSMOD_NOWAIT
970 	/*
971 	 * Wait till MMC sdio_register_driver callback called and made driver attach.
972 	 * It's needed to make sync up exit from dhd insmod  and
973 	 * Kernel MMC sdio device callback registration
974 	 */
975 	err = down_timeout(&dhd_registration_sem, msecs_to_jiffies(DHD_REGISTRATION_TIMEOUT));
976 	if (err) {
977 		DHD_ERROR(("%s: sdio_register_driver timeout or error \n", __FUNCTION__));
978 		dhd_bus_unregister();
979 		goto fail;
980 	}
981 #endif
982 
983 	return err;
984 
985 fail:
986 	/* power down all adapters */
987 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
988 		adapter = &dhd_wifi_platdata->adapters[i];
989 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
990 		wifi_platform_bus_enumerate(adapter, FALSE);
991 	}
992 #else
993 	/* x86 bring-up PC needs no power-up operations */
994 	err = dhd_bus_register();
995 #endif /* defined(OEM_ANDROID) && defined(BCMLXSDMMC) */
996 
997 	return err;
998 }
999 #else /* BCMSDIO */
dhd_wifi_platform_load_sdio(void)1000 static int dhd_wifi_platform_load_sdio(void)
1001 {
1002 	return 0;
1003 }
1004 #endif /* BCMSDIO */
1005 
1006 #ifdef BCMDBUS
dhd_wifi_platform_load_usb(void)1007 static int dhd_wifi_platform_load_usb(void)
1008 {
1009 	int err = 0;
1010 #if !defined(DHD_PRELOAD)
1011 	wifi_adapter_info_t *adapter;
1012 	s32 timeout = -1;
1013 	int i;
1014 #endif
1015 
1016 #if !defined(DHD_PRELOAD)
1017 	/* power down all adapters */
1018 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
1019 		adapter = &dhd_wifi_platdata->adapters[i];
1020 		wifi_platform_set_power(adapter, FALSE, 0);
1021 		if (err) {
1022 			DHD_ERROR(("failed to wifi_platform_set_power off %s\n", adapter->name));
1023 			goto exit;
1024 		}
1025 	}
1026 	OSL_SLEEP(200);
1027 #endif
1028 
1029 	err = dhd_bus_register();
1030 	if (err) {
1031 		DHD_ERROR(("%s: usb_register failed\n", __FUNCTION__));
1032 		goto exit;
1033 	}
1034 
1035 #if !defined(DHD_PRELOAD)
1036 	/* power up all adapters */
1037 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
1038 		adapter = &dhd_wifi_platdata->adapters[i];
1039 		DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
1040 		DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
1041 			adapter->irq_num, adapter->intr_flags, adapter->fw_path, adapter->nv_path));
1042 		DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
1043 			adapter->bus_type, adapter->bus_num, adapter->slot_num));
1044 		err = wifi_platform_set_power(adapter, TRUE, WIFI_TURNON_DELAY);
1045 		if (err) {
1046 			DHD_ERROR(("failed to wifi_platform_set_power on %s\n", adapter->name));
1047 			goto fail;
1048 		}
1049 		timeout = wait_event_interruptible_timeout(adapter->status_event,
1050 			wifi_get_adapter_status(adapter, WIFI_STATUS_NET_ATTACHED),
1051 			msecs_to_jiffies(DHD_REGISTRATION_TIMEOUT));
1052 		if (timeout <= 0) {
1053 			err = -1;
1054 			DHD_ERROR(("%s: usb_register_driver timeout\n", __FUNCTION__));
1055 			goto fail;
1056 		}
1057 	}
1058 #endif
1059 
1060 exit:
1061 	return err;
1062 
1063 #if !defined(DHD_PRELOAD)
1064 fail:
1065 	dhd_bus_unregister();
1066 	/* power down all adapters */
1067 	for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
1068 		adapter = &dhd_wifi_platdata->adapters[i];
1069 		wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
1070 	}
1071 
1072 	return err;
1073 #endif
1074 }
1075 #else /* BCMDBUS */
dhd_wifi_platform_load_usb(void)1076 static int dhd_wifi_platform_load_usb(void)
1077 {
1078 	return 0;
1079 }
1080 #endif /* BCMDBUS */
1081 
dhd_wifi_platform_load()1082 static int dhd_wifi_platform_load()
1083 {
1084 	int err = 0;
1085 	printf("%s: Enter\n", __FUNCTION__);
1086 
1087 #if defined(OEM_ANDROID)
1088 	wl_android_init();
1089 #endif /* OEM_ANDROID */
1090 
1091 	if ((err = dhd_wifi_platform_load_usb()))
1092 		goto end;
1093 	else if ((err = dhd_wifi_platform_load_sdio()))
1094 		goto end;
1095 	else
1096 		err = dhd_wifi_platform_load_pcie();
1097 
1098 end:
1099 #if defined(OEM_ANDROID)
1100 	if (err)
1101 		wl_android_exit();
1102 #if !defined(MULTIPLE_SUPPLICANT)
1103 	else
1104 		wl_android_post_init();
1105 #endif
1106 #endif /* OEM_ANDROID */
1107 
1108 	return err;
1109 }
1110