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