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