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