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