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