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