dhd_linux_platdev.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Linux platform device for DHD WLAN adapter
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: dhd_linux_platdev.c 697969 2017-05-05 16:36:11Z $
  30. */
  31. #include <typedefs.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/platform_device.h>
  36. #include <bcmutils.h>
  37. #include <linux_osl.h>
  38. #include <dhd_dbg.h>
  39. #include <dngl_stats.h>
  40. #include <dhd.h>
  41. #include <dhd_bus.h>
  42. #include <dhd_linux.h>
  43. #if defined(OEM_ANDROID)
  44. #include <wl_android.h>
  45. #endif // endif
  46. #if defined(CONFIG_WIFI_CONTROL_FUNC)
  47. #include <linux/wlan_plat.h>
  48. #endif // endif
  49. #ifdef CONFIG_DTS
  50. #include<linux/regulator/consumer.h>
  51. #include<linux/of_gpio.h>
  52. #endif /* CONFIG_DTS */
  53. #define WIFI_PLAT_NAME "bcmdhd_wlan"
  54. #define WIFI_PLAT_NAME2 "bcm4329_wlan"
  55. #define WIFI_PLAT_EXT "bcmdhd_wifi_platform"
  56. #ifdef DHD_WIFI_SHUTDOWN
  57. extern void wifi_plat_dev_drv_shutdown(struct platform_device *pdev);
  58. #endif // endif
  59. #ifdef CONFIG_DTS
  60. struct regulator *wifi_regulator = NULL;
  61. #endif /* CONFIG_DTS */
  62. bool cfg_multichip = FALSE;
  63. bcmdhd_wifi_platdata_t *dhd_wifi_platdata = NULL;
  64. static int wifi_plat_dev_probe_ret = 0;
  65. static bool is_power_on = FALSE;
  66. #if !defined(CONFIG_DTS)
  67. #if defined(DHD_OF_SUPPORT)
  68. static bool dts_enabled = TRUE;
  69. extern struct resource dhd_wlan_resources;
  70. extern struct wifi_platform_data dhd_wlan_control;
  71. #else
  72. static bool dts_enabled = FALSE;
  73. #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
  74. #pragma GCC diagnostic push
  75. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  76. #endif // endif
  77. struct resource dhd_wlan_resources = {0};
  78. struct wifi_platform_data dhd_wlan_control = {0};
  79. #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
  80. #pragma GCC diagnostic pop
  81. #endif // endif
  82. #endif /* CONFIG_OF && !defined(CONFIG_ARCH_MSM) */
  83. #endif /* !defind(CONFIG_DTS) */
  84. static int dhd_wifi_platform_load(void);
  85. extern void* wl_cfg80211_get_dhdp(struct net_device *dev);
  86. #ifdef BOARD_HIKEY
  87. extern int dhd_wlan_init(void);
  88. extern int dhd_wlan_deinit(void);
  89. #endif /* BOARD_HIKEY */
  90. #ifdef ENABLE_4335BT_WAR
  91. extern int bcm_bt_lock(int cookie);
  92. extern void bcm_bt_unlock(int cookie);
  93. static int lock_cookie_wifi = 'W' | 'i'<<8 | 'F'<<16 | 'i'<<24; /* cookie is "WiFi" */
  94. #endif /* ENABLE_4335BT_WAR */
  95. wifi_adapter_info_t* dhd_wifi_platform_get_adapter(uint32 bus_type, uint32 bus_num, uint32 slot_num)
  96. {
  97. int i;
  98. if (dhd_wifi_platdata == NULL)
  99. return NULL;
  100. for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
  101. wifi_adapter_info_t *adapter = &dhd_wifi_platdata->adapters[i];
  102. if ((adapter->bus_type == -1 || adapter->bus_type == bus_type) &&
  103. (adapter->bus_num == -1 || adapter->bus_num == bus_num) &&
  104. (adapter->slot_num == -1 || adapter->slot_num == slot_num)) {
  105. DHD_TRACE(("found adapter info '%s'\n", adapter->name));
  106. return adapter;
  107. }
  108. }
  109. return NULL;
  110. }
  111. void* wifi_platform_prealloc(wifi_adapter_info_t *adapter, int section, unsigned long size)
  112. {
  113. void *alloc_ptr = NULL;
  114. struct wifi_platform_data *plat_data;
  115. if (!adapter || !adapter->wifi_plat_data)
  116. return NULL;
  117. plat_data = adapter->wifi_plat_data;
  118. if (plat_data->mem_prealloc) {
  119. alloc_ptr = plat_data->mem_prealloc(section, size);
  120. if (alloc_ptr) {
  121. DHD_INFO(("success alloc section %d\n", section));
  122. if (size != 0L)
  123. bzero(alloc_ptr, size);
  124. return alloc_ptr;
  125. }
  126. }
  127. DHD_ERROR(("%s: failed to alloc static mem section %d\n", __FUNCTION__, section));
  128. return NULL;
  129. }
  130. void* wifi_platform_get_prealloc_func_ptr(wifi_adapter_info_t *adapter)
  131. {
  132. struct wifi_platform_data *plat_data;
  133. if (!adapter || !adapter->wifi_plat_data)
  134. return NULL;
  135. plat_data = adapter->wifi_plat_data;
  136. return plat_data->mem_prealloc;
  137. }
  138. int wifi_platform_get_irq_number(wifi_adapter_info_t *adapter, unsigned long *irq_flags_ptr)
  139. {
  140. if (adapter == NULL)
  141. return -1;
  142. if (irq_flags_ptr)
  143. *irq_flags_ptr = adapter->intr_flags;
  144. return adapter->irq_num;
  145. }
  146. int wifi_platform_set_power(wifi_adapter_info_t *adapter, bool on, unsigned long msec)
  147. {
  148. int err = 0;
  149. #ifdef CONFIG_DTS
  150. if (on) {
  151. err = regulator_enable(wifi_regulator);
  152. is_power_on = TRUE;
  153. }
  154. else {
  155. err = regulator_disable(wifi_regulator);
  156. is_power_on = FALSE;
  157. }
  158. if (err < 0)
  159. DHD_ERROR(("%s: regulator enable/disable failed", __FUNCTION__));
  160. #else
  161. struct wifi_platform_data *plat_data;
  162. if (!adapter || !adapter->wifi_plat_data)
  163. return -EINVAL;
  164. plat_data = adapter->wifi_plat_data;
  165. DHD_ERROR(("%s = %d, delay: %lu msec\n", __FUNCTION__, on, msec));
  166. if (plat_data->set_power) {
  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. err = plat_data->set_power(on);
  180. }
  181. if (msec && !err)
  182. OSL_SLEEP(msec);
  183. if (on && !err)
  184. is_power_on = TRUE;
  185. else
  186. is_power_on = FALSE;
  187. #endif /* CONFIG_DTS */
  188. return err;
  189. }
  190. int wifi_platform_bus_enumerate(wifi_adapter_info_t *adapter, bool device_present)
  191. {
  192. int err = 0;
  193. struct wifi_platform_data *plat_data;
  194. if (!adapter || !adapter->wifi_plat_data)
  195. return -EINVAL;
  196. plat_data = adapter->wifi_plat_data;
  197. DHD_ERROR(("%s device present %d\n", __FUNCTION__, device_present));
  198. if (plat_data->set_carddetect) {
  199. err = plat_data->set_carddetect(device_present);
  200. }
  201. return err;
  202. }
  203. int wifi_platform_get_mac_addr(wifi_adapter_info_t *adapter, unsigned char *buf)
  204. {
  205. struct wifi_platform_data *plat_data;
  206. DHD_ERROR(("%s\n", __FUNCTION__));
  207. if (!buf || !adapter || !adapter->wifi_plat_data)
  208. return -EINVAL;
  209. plat_data = adapter->wifi_plat_data;
  210. if (plat_data->get_mac_addr) {
  211. return plat_data->get_mac_addr(buf);
  212. }
  213. return -EOPNOTSUPP;
  214. }
  215. #ifdef CUSTOM_COUNTRY_CODE
  216. void *wifi_platform_get_country_code(wifi_adapter_info_t *adapter, char *ccode, u32 flags)
  217. #else
  218. void *wifi_platform_get_country_code(wifi_adapter_info_t *adapter, char *ccode)
  219. #endif /* CUSTOM_COUNTRY_CODE */
  220. {
  221. /* get_country_code was added after 2.6.39 */
  222. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
  223. struct wifi_platform_data *plat_data;
  224. if (!ccode || !adapter || !adapter->wifi_plat_data)
  225. return NULL;
  226. plat_data = adapter->wifi_plat_data;
  227. DHD_TRACE(("%s\n", __FUNCTION__));
  228. if (plat_data->get_country_code) {
  229. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 58))
  230. return plat_data->get_country_code(ccode, WLAN_PLAT_NODFS_FLAG);
  231. #else
  232. #ifdef CUSTOM_COUNTRY_CODE
  233. return plat_data->get_country_code(ccode, flags);
  234. #else
  235. return plat_data->get_country_code(ccode);
  236. #endif /* CUSTOM_COUNTRY_CODE */
  237. #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 58)) */
  238. }
  239. #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) */
  240. return NULL;
  241. }
  242. static int wifi_plat_dev_drv_probe(struct platform_device *pdev)
  243. {
  244. struct resource *resource;
  245. wifi_adapter_info_t *adapter;
  246. #ifdef CONFIG_DTS
  247. int irq, gpio;
  248. #endif /* CONFIG_DTS */
  249. /* Android style wifi platform data device ("bcmdhd_wlan" or "bcm4329_wlan")
  250. * is kept for backward compatibility and supports only 1 adapter
  251. */
  252. ASSERT(dhd_wifi_platdata != NULL);
  253. ASSERT(dhd_wifi_platdata->num_adapters == 1);
  254. adapter = &dhd_wifi_platdata->adapters[0];
  255. adapter->wifi_plat_data = (struct wifi_platform_data *)(pdev->dev.platform_data);
  256. resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcmdhd_wlan_irq");
  257. if (resource == NULL)
  258. resource = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "bcm4329_wlan_irq");
  259. if (resource) {
  260. adapter->irq_num = resource->start;
  261. adapter->intr_flags = resource->flags & IRQF_TRIGGER_MASK;
  262. #ifdef DHD_ISR_NO_SUSPEND
  263. adapter->intr_flags |= IRQF_NO_SUSPEND;
  264. #endif // endif
  265. }
  266. #ifdef CONFIG_DTS
  267. wifi_regulator = regulator_get(&pdev->dev, "wlreg_on");
  268. if (wifi_regulator == NULL) {
  269. DHD_ERROR(("%s regulator is null\n", __FUNCTION__));
  270. return -1;
  271. }
  272. /* This is to get the irq for the OOB */
  273. gpio = of_get_gpio(pdev->dev.of_node, 0);
  274. if (gpio < 0) {
  275. DHD_ERROR(("%s gpio information is incorrect\n", __FUNCTION__));
  276. return -1;
  277. }
  278. irq = gpio_to_irq(gpio);
  279. if (irq < 0) {
  280. DHD_ERROR(("%s irq information is incorrect\n", __FUNCTION__));
  281. return -1;
  282. }
  283. adapter->irq_num = irq;
  284. /* need to change the flags according to our requirement */
  285. adapter->intr_flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
  286. IORESOURCE_IRQ_SHAREABLE;
  287. #endif /* CONFIG_DTS */
  288. wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
  289. return wifi_plat_dev_probe_ret;
  290. }
  291. static int wifi_plat_dev_drv_remove(struct platform_device *pdev)
  292. {
  293. wifi_adapter_info_t *adapter;
  294. /* Android style wifi platform data device ("bcmdhd_wlan" or "bcm4329_wlan")
  295. * is kept for backward compatibility and supports only 1 adapter
  296. */
  297. ASSERT(dhd_wifi_platdata != NULL);
  298. ASSERT(dhd_wifi_platdata->num_adapters == 1);
  299. adapter = &dhd_wifi_platdata->adapters[0];
  300. if (is_power_on) {
  301. #ifdef BCMPCIE
  302. wifi_platform_bus_enumerate(adapter, FALSE);
  303. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  304. #else
  305. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  306. wifi_platform_bus_enumerate(adapter, FALSE);
  307. #endif /* BCMPCIE */
  308. }
  309. #ifdef CONFIG_DTS
  310. regulator_put(wifi_regulator);
  311. #endif /* CONFIG_DTS */
  312. return 0;
  313. }
  314. static int wifi_plat_dev_drv_suspend(struct platform_device *pdev, pm_message_t state)
  315. {
  316. DHD_TRACE(("##> %s\n", __FUNCTION__));
  317. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) && \
  318. defined(BCMSDIO)
  319. bcmsdh_oob_intr_set(0);
  320. #endif /* (OOB_INTR_ONLY) */
  321. return 0;
  322. }
  323. static int wifi_plat_dev_drv_resume(struct platform_device *pdev)
  324. {
  325. DHD_TRACE(("##> %s\n", __FUNCTION__));
  326. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 39)) && defined(OOB_INTR_ONLY) && \
  327. defined(BCMSDIO)
  328. if (dhd_os_check_if_up(wl_cfg80211_get_dhdp()))
  329. bcmsdh_oob_intr_set(1);
  330. #endif /* (OOB_INTR_ONLY) */
  331. return 0;
  332. }
  333. #ifdef CONFIG_DTS
  334. static const struct of_device_id wifi_device_dt_match[] = {
  335. { .compatible = "android,bcmdhd_wlan", },
  336. {},
  337. };
  338. #endif /* CONFIG_DTS */
  339. static struct platform_driver wifi_platform_dev_driver = {
  340. .probe = wifi_plat_dev_drv_probe,
  341. .remove = wifi_plat_dev_drv_remove,
  342. .suspend = wifi_plat_dev_drv_suspend,
  343. .resume = wifi_plat_dev_drv_resume,
  344. #ifdef DHD_WIFI_SHUTDOWN
  345. .shutdown = wifi_plat_dev_drv_shutdown,
  346. #endif // endif
  347. .driver = {
  348. .name = WIFI_PLAT_NAME,
  349. #ifdef CONFIG_DTS
  350. .of_match_table = wifi_device_dt_match,
  351. #endif /* CONFIG_DTS */
  352. }
  353. };
  354. static struct platform_driver wifi_platform_dev_driver_legacy = {
  355. .probe = wifi_plat_dev_drv_probe,
  356. .remove = wifi_plat_dev_drv_remove,
  357. .suspend = wifi_plat_dev_drv_suspend,
  358. .resume = wifi_plat_dev_drv_resume,
  359. #ifdef DHD_WIFI_SHUTDOWN
  360. .shutdown = wifi_plat_dev_drv_shutdown,
  361. #endif // endif
  362. .driver = {
  363. .name = WIFI_PLAT_NAME2,
  364. }
  365. };
  366. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0))
  367. static int wifi_platdev_match(struct device *dev, const void *data)
  368. #else
  369. static int wifi_platdev_match(struct device *dev, void *data)
  370. #endif /* LINUX_VERSION_CODE */
  371. {
  372. const char *name = (const char*)data;
  373. #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
  374. #pragma GCC diagnostic push
  375. #pragma GCC diagnostic ignored "-Wcast-qual"
  376. #endif // endif
  377. const struct platform_device *pdev = to_platform_device(dev);
  378. #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
  379. #pragma GCC diagnostic pop
  380. #endif // endif
  381. if (strcmp(pdev->name, name) == 0) {
  382. DHD_ERROR(("found wifi platform device %s\n", name));
  383. return TRUE;
  384. }
  385. return FALSE;
  386. }
  387. static int wifi_ctrlfunc_register_drv(void)
  388. {
  389. int err = 0;
  390. struct device *dev1, *dev2;
  391. wifi_adapter_info_t *adapter;
  392. dev1 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME, wifi_platdev_match);
  393. dev2 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME2, wifi_platdev_match);
  394. #ifdef BOARD_HIKEY_MODULAR
  395. dhd_wlan_init();
  396. #endif /* BOARD_HIKEY_MODULAR */
  397. #if !defined(CONFIG_DTS)
  398. if (!dts_enabled) {
  399. if (dev1 == NULL && dev2 == NULL) {
  400. DHD_ERROR(("no wifi platform data, skip\n"));
  401. return -ENXIO;
  402. }
  403. }
  404. #endif /* !defined(CONFIG_DTS) */
  405. /* multi-chip support not enabled, build one adapter information for
  406. * DHD (either SDIO, USB or PCIe)
  407. */
  408. adapter = kzalloc(sizeof(wifi_adapter_info_t), GFP_KERNEL);
  409. if (adapter == NULL) {
  410. DHD_ERROR(("%s:adapter alloc failed", __FUNCTION__));
  411. return ENOMEM;
  412. }
  413. adapter->name = "DHD generic adapter";
  414. adapter->bus_type = -1;
  415. adapter->bus_num = -1;
  416. adapter->slot_num = -1;
  417. adapter->irq_num = -1;
  418. is_power_on = FALSE;
  419. wifi_plat_dev_probe_ret = 0;
  420. dhd_wifi_platdata = kzalloc(sizeof(bcmdhd_wifi_platdata_t), GFP_KERNEL);
  421. dhd_wifi_platdata->num_adapters = 1;
  422. dhd_wifi_platdata->adapters = adapter;
  423. if (dev1) {
  424. err = platform_driver_register(&wifi_platform_dev_driver);
  425. if (err) {
  426. DHD_ERROR(("%s: failed to register wifi ctrl func driver\n",
  427. __FUNCTION__));
  428. return err;
  429. }
  430. }
  431. if (dev2) {
  432. err = platform_driver_register(&wifi_platform_dev_driver_legacy);
  433. if (err) {
  434. DHD_ERROR(("%s: failed to register wifi ctrl func legacy driver\n",
  435. __FUNCTION__));
  436. return err;
  437. }
  438. }
  439. #if !defined(CONFIG_DTS)
  440. if (dts_enabled) {
  441. struct resource *resource;
  442. adapter->wifi_plat_data = (void *)&dhd_wlan_control;
  443. resource = &dhd_wlan_resources;
  444. adapter->irq_num = resource->start;
  445. adapter->intr_flags = resource->flags & IRQF_TRIGGER_MASK;
  446. #ifdef DHD_ISR_NO_SUSPEND
  447. adapter->intr_flags |= IRQF_NO_SUSPEND;
  448. #endif // endif
  449. wifi_plat_dev_probe_ret = dhd_wifi_platform_load();
  450. }
  451. #endif /* !defined(CONFIG_DTS) */
  452. #ifdef CONFIG_DTS
  453. wifi_plat_dev_probe_ret = platform_driver_register(&wifi_platform_dev_driver);
  454. #endif /* CONFIG_DTS */
  455. /* return probe function's return value if registeration succeeded */
  456. return wifi_plat_dev_probe_ret;
  457. }
  458. void wifi_ctrlfunc_unregister_drv(void)
  459. {
  460. #ifdef CONFIG_DTS
  461. DHD_ERROR(("unregister wifi platform drivers\n"));
  462. platform_driver_unregister(&wifi_platform_dev_driver);
  463. #else
  464. struct device *dev1, *dev2;
  465. dev1 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME, wifi_platdev_match);
  466. dev2 = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_NAME2, wifi_platdev_match);
  467. if (!dts_enabled)
  468. if (dev1 == NULL && dev2 == NULL)
  469. return;
  470. DHD_ERROR(("unregister wifi platform drivers\n"));
  471. if (dev1)
  472. platform_driver_unregister(&wifi_platform_dev_driver);
  473. if (dev2)
  474. platform_driver_unregister(&wifi_platform_dev_driver_legacy);
  475. if (dts_enabled) {
  476. wifi_adapter_info_t *adapter;
  477. adapter = &dhd_wifi_platdata->adapters[0];
  478. if (is_power_on) {
  479. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  480. wifi_platform_bus_enumerate(adapter, FALSE);
  481. }
  482. }
  483. #ifdef BOARD_HIKEY_MODULAR
  484. dhd_wlan_deinit();
  485. #endif /* BOARD_HIKEY_MODULAR */
  486. #endif /* !defined(CONFIG_DTS) */
  487. kfree(dhd_wifi_platdata->adapters);
  488. dhd_wifi_platdata->adapters = NULL;
  489. dhd_wifi_platdata->num_adapters = 0;
  490. kfree(dhd_wifi_platdata);
  491. dhd_wifi_platdata = NULL;
  492. }
  493. static int bcmdhd_wifi_plat_dev_drv_probe(struct platform_device *pdev)
  494. {
  495. dhd_wifi_platdata = (bcmdhd_wifi_platdata_t *)(pdev->dev.platform_data);
  496. return dhd_wifi_platform_load();
  497. }
  498. static int bcmdhd_wifi_plat_dev_drv_remove(struct platform_device *pdev)
  499. {
  500. int i;
  501. wifi_adapter_info_t *adapter;
  502. ASSERT(dhd_wifi_platdata != NULL);
  503. /* power down all adapters */
  504. for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
  505. adapter = &dhd_wifi_platdata->adapters[i];
  506. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  507. wifi_platform_bus_enumerate(adapter, FALSE);
  508. }
  509. return 0;
  510. }
  511. static struct platform_driver dhd_wifi_platform_dev_driver = {
  512. .probe = bcmdhd_wifi_plat_dev_drv_probe,
  513. .remove = bcmdhd_wifi_plat_dev_drv_remove,
  514. .driver = {
  515. .name = WIFI_PLAT_EXT,
  516. }
  517. };
  518. int dhd_wifi_platform_register_drv(void)
  519. {
  520. int err = 0;
  521. struct device *dev;
  522. /* register Broadcom wifi platform data driver if multi-chip is enabled,
  523. * otherwise use Android style wifi platform data (aka wifi control function)
  524. * if it exists
  525. *
  526. * to support multi-chip DHD, Broadcom wifi platform data device must
  527. * be added in kernel early boot (e.g. board config file).
  528. */
  529. if (cfg_multichip) {
  530. dev = bus_find_device(&platform_bus_type, NULL, WIFI_PLAT_EXT, wifi_platdev_match);
  531. if (dev == NULL) {
  532. DHD_ERROR(("bcmdhd wifi platform data device not found!!\n"));
  533. return -ENXIO;
  534. }
  535. err = platform_driver_register(&dhd_wifi_platform_dev_driver);
  536. } else {
  537. err = wifi_ctrlfunc_register_drv();
  538. /* no wifi ctrl func either, load bus directly and ignore this error */
  539. if (err) {
  540. if (err == -ENXIO) {
  541. /* wifi ctrl function does not exist */
  542. err = dhd_wifi_platform_load();
  543. } else {
  544. /* unregister driver due to initialization failure */
  545. wifi_ctrlfunc_unregister_drv();
  546. }
  547. }
  548. }
  549. return err;
  550. }
  551. #ifdef BCMPCIE
  552. static int dhd_wifi_platform_load_pcie(void)
  553. {
  554. int err = 0;
  555. int i;
  556. wifi_adapter_info_t *adapter;
  557. BCM_REFERENCE(i);
  558. BCM_REFERENCE(adapter);
  559. if (dhd_wifi_platdata == NULL) {
  560. err = dhd_bus_register();
  561. } else {
  562. if (dhd_download_fw_on_driverload) {
  563. /* power up all adapters */
  564. for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
  565. int retry = POWERUP_MAX_RETRY;
  566. adapter = &dhd_wifi_platdata->adapters[i];
  567. DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
  568. DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
  569. adapter->irq_num, adapter->intr_flags, adapter->fw_path,
  570. adapter->nv_path));
  571. DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
  572. adapter->bus_type, adapter->bus_num, adapter->slot_num));
  573. do {
  574. err = wifi_platform_set_power(adapter,
  575. TRUE, WIFI_TURNON_DELAY);
  576. if (err) {
  577. DHD_ERROR(("failed to power up %s,"
  578. " %d retry left\n",
  579. adapter->name, retry));
  580. /* WL_REG_ON state unknown, Power off forcely */
  581. wifi_platform_set_power(adapter,
  582. FALSE, WIFI_TURNOFF_DELAY);
  583. continue;
  584. } else {
  585. err = wifi_platform_bus_enumerate(adapter, TRUE);
  586. if (err) {
  587. DHD_ERROR(("failed to enumerate bus %s, "
  588. "%d retry left\n",
  589. adapter->name, retry));
  590. wifi_platform_set_power(adapter, FALSE,
  591. WIFI_TURNOFF_DELAY);
  592. } else {
  593. break;
  594. }
  595. }
  596. } while (retry--);
  597. if (retry < 0) {
  598. DHD_ERROR(("failed to power up %s, max retry reached**\n",
  599. adapter->name));
  600. return -ENODEV;
  601. }
  602. }
  603. }
  604. err = dhd_bus_register();
  605. if (err) {
  606. DHD_ERROR(("%s: pcie_register_driver failed\n", __FUNCTION__));
  607. if (dhd_download_fw_on_driverload) {
  608. /* power down all adapters */
  609. for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
  610. adapter = &dhd_wifi_platdata->adapters[i];
  611. wifi_platform_bus_enumerate(adapter, FALSE);
  612. wifi_platform_set_power(adapter,
  613. FALSE, WIFI_TURNOFF_DELAY);
  614. }
  615. }
  616. }
  617. }
  618. return err;
  619. }
  620. #else
  621. static int dhd_wifi_platform_load_pcie(void)
  622. {
  623. return 0;
  624. }
  625. #endif /* BCMPCIE */
  626. void dhd_wifi_platform_unregister_drv(void)
  627. {
  628. if (cfg_multichip)
  629. platform_driver_unregister(&dhd_wifi_platform_dev_driver);
  630. else
  631. wifi_ctrlfunc_unregister_drv();
  632. }
  633. extern int dhd_watchdog_prio;
  634. extern int dhd_dpc_prio;
  635. extern uint dhd_deferred_tx;
  636. #if defined(OEM_ANDROID) && defined(BCMLXSDMMC)
  637. extern struct semaphore dhd_registration_sem;
  638. #endif /* defined(OEM_ANDROID) && defined(BCMLXSDMMC) */
  639. #ifdef BCMSDIO
  640. static int dhd_wifi_platform_load_sdio(void)
  641. {
  642. int i;
  643. int err = 0;
  644. wifi_adapter_info_t *adapter;
  645. BCM_REFERENCE(i);
  646. BCM_REFERENCE(adapter);
  647. /* Sanity check on the module parameters
  648. * - Both watchdog and DPC as tasklets are ok
  649. * - If both watchdog and DPC are threads, TX must be deferred
  650. */
  651. if (!(dhd_watchdog_prio < 0 && dhd_dpc_prio < 0) &&
  652. !(dhd_watchdog_prio >= 0 && dhd_dpc_prio >= 0 && dhd_deferred_tx))
  653. return -EINVAL;
  654. #if defined(OEM_ANDROID) && defined(BCMLXSDMMC)
  655. sema_init(&dhd_registration_sem, 0);
  656. #endif // endif
  657. if (dhd_wifi_platdata == NULL) {
  658. DHD_ERROR(("DHD wifi platform data is required for Android build\n"));
  659. DHD_ERROR(("DHD registering bus directly\n"));
  660. /* x86 bring-up PC needs no power-up operations */
  661. err = dhd_bus_register();
  662. return err;
  663. }
  664. #if defined(OEM_ANDROID) && defined(BCMLXSDMMC)
  665. /* power up all adapters */
  666. for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
  667. bool chip_up = FALSE;
  668. int retry = POWERUP_MAX_RETRY;
  669. struct semaphore dhd_chipup_sem;
  670. adapter = &dhd_wifi_platdata->adapters[i];
  671. DHD_ERROR(("Power-up adapter '%s'\n", adapter->name));
  672. DHD_INFO((" - irq %d [flags %d], firmware: %s, nvram: %s\n",
  673. adapter->irq_num, adapter->intr_flags, adapter->fw_path, adapter->nv_path));
  674. DHD_INFO((" - bus type %d, bus num %d, slot num %d\n\n",
  675. adapter->bus_type, adapter->bus_num, adapter->slot_num));
  676. do {
  677. sema_init(&dhd_chipup_sem, 0);
  678. err = dhd_bus_reg_sdio_notify(&dhd_chipup_sem);
  679. if (err) {
  680. DHD_ERROR(("%s dhd_bus_reg_sdio_notify fail(%d)\n\n",
  681. __FUNCTION__, err));
  682. return err;
  683. }
  684. err = wifi_platform_set_power(adapter, TRUE, WIFI_TURNON_DELAY);
  685. if (err) {
  686. DHD_ERROR(("%s: wifi pwr on error ! \n", __FUNCTION__));
  687. dhd_bus_unreg_sdio_notify();
  688. /* WL_REG_ON state unknown, Power off forcely */
  689. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  690. continue;
  691. } else {
  692. wifi_platform_bus_enumerate(adapter, TRUE);
  693. }
  694. if (down_timeout(&dhd_chipup_sem, msecs_to_jiffies(POWERUP_WAIT_MS)) == 0) {
  695. dhd_bus_unreg_sdio_notify();
  696. chip_up = TRUE;
  697. break;
  698. }
  699. DHD_ERROR(("failed to power up %s, %d retry left\n", adapter->name, retry));
  700. dhd_bus_unreg_sdio_notify();
  701. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  702. wifi_platform_bus_enumerate(adapter, FALSE);
  703. } while (retry--);
  704. if (!chip_up) {
  705. DHD_ERROR(("failed to power up %s, max retry reached**\n", adapter->name));
  706. return -ENODEV;
  707. }
  708. }
  709. err = dhd_bus_register();
  710. if (err) {
  711. DHD_ERROR(("%s: sdio_register_driver failed\n", __FUNCTION__));
  712. goto fail;
  713. }
  714. /*
  715. * Wait till MMC sdio_register_driver callback called and made driver attach.
  716. * It's needed to make sync up exit from dhd insmod and
  717. * Kernel MMC sdio device callback registration
  718. */
  719. err = down_timeout(&dhd_registration_sem, msecs_to_jiffies(DHD_REGISTRATION_TIMEOUT));
  720. if (err) {
  721. DHD_ERROR(("%s: sdio_register_driver timeout or error \n", __FUNCTION__));
  722. dhd_bus_unregister();
  723. goto fail;
  724. }
  725. return err;
  726. fail:
  727. /* power down all adapters */
  728. for (i = 0; i < dhd_wifi_platdata->num_adapters; i++) {
  729. adapter = &dhd_wifi_platdata->adapters[i];
  730. wifi_platform_set_power(adapter, FALSE, WIFI_TURNOFF_DELAY);
  731. wifi_platform_bus_enumerate(adapter, FALSE);
  732. }
  733. #endif /* defined(OEM_ANDROID) && defined(BCMLXSDMMC) */
  734. return err;
  735. }
  736. #else /* BCMSDIO */
  737. static int dhd_wifi_platform_load_sdio(void)
  738. {
  739. return 0;
  740. }
  741. #endif /* BCMSDIO */
  742. static int dhd_wifi_platform_load_usb(void)
  743. {
  744. return 0;
  745. }
  746. static int dhd_wifi_platform_load()
  747. {
  748. int err = 0;
  749. #if defined(OEM_ANDROID)
  750. wl_android_init();
  751. #endif /* OEM_ANDROID */
  752. if ((err = dhd_wifi_platform_load_usb()))
  753. goto end;
  754. else if ((err = dhd_wifi_platform_load_sdio()))
  755. goto end;
  756. else
  757. err = dhd_wifi_platform_load_pcie();
  758. end:
  759. #if defined(OEM_ANDROID)
  760. if (err)
  761. wl_android_exit();
  762. else
  763. wl_android_post_init();
  764. #endif /* OEM_ANDROID */
  765. return err;
  766. }