ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723bs / os_dep / linux / sdio_intf.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _HCI_INTF_C_
21
22 #include <drv_types.h>
23 #include <platform_ops.h>
24
25 #ifndef CONFIG_SDIO_HCI
26 #error "CONFIG_SDIO_HCI shall be on!\n"
27 #endif
28
29 #ifdef CONFIG_PLATFORM_INTEL_BYT
30 #ifdef CONFIG_ACPI
31 #include <linux/acpi.h>
32 #include <linux/acpi_gpio.h>
33 #include "rtw_android.h"
34 #endif
35 static int wlan_en_gpio = -1;
36 #endif //CONFIG_PLATFORM_INTEL_BYT
37
38 #ifndef dev_to_sdio_func
39 #define dev_to_sdio_func(d)     container_of(d, struct sdio_func, dev)
40 #endif
41
42 #ifdef CONFIG_WOWLAN
43 static struct mmc_host *mmc_host = NULL;
44 #endif
45
46 static const struct sdio_device_id sdio_ids[] =
47 {
48 #ifdef CONFIG_RTL8723A
49         { SDIO_DEVICE(0x024c, 0x8723),.driver_data = RTL8723A},
50 #endif //CONFIG_RTL8723A
51 #ifdef CONFIG_RTL8723B
52         { SDIO_DEVICE(0x024c, 0xB723),.driver_data = RTL8723B},
53 #endif
54 #ifdef CONFIG_RTL8188E
55         { SDIO_DEVICE(0x024c, 0x8179),.driver_data = RTL8188E},
56 #endif //CONFIG_RTL8188E
57
58 #ifdef CONFIG_RTL8821A
59         { SDIO_DEVICE(0x024c, 0x8821),.driver_data = RTL8821},
60 #endif //CONFIG_RTL8821A
61
62 #ifdef CONFIG_RTL8192E
63         { SDIO_DEVICE(0x024c, 0x818B),.driver_data = RTL8192E},
64 #endif //CONFIG_RTL8192E
65
66 #if defined(RTW_ENABLE_WIFI_CONTROL_FUNC) /* temporarily add this to accept all sdio wlan id */
67         { SDIO_DEVICE_CLASS(SDIO_CLASS_WLAN) },
68 #endif
69         { /* end: all zeroes */                         },
70 };
71
72 MODULE_DEVICE_TABLE(sdio, sdio_ids);
73
74 static int rtw_drv_init(struct sdio_func *func, const struct sdio_device_id *id);
75 static void rtw_dev_remove(struct sdio_func *func);
76 static int rtw_sdio_resume(struct device *dev);
77 static int rtw_sdio_suspend(struct device *dev);
78 extern void rtw_dev_unload(PADAPTER padapter);
79
80 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)) 
81 static const struct dev_pm_ops rtw_sdio_pm_ops = {
82         .suspend        = rtw_sdio_suspend,
83         .resume = rtw_sdio_resume,
84 };
85 #endif
86         
87 struct sdio_drv_priv {
88         struct sdio_driver r871xs_drv;
89         int drv_registered;
90 };
91
92 static struct sdio_drv_priv sdio_drvpriv = {
93         .r871xs_drv.probe = rtw_drv_init,
94         .r871xs_drv.remove = rtw_dev_remove,
95         .r871xs_drv.name = (char*)DRV_NAME,
96         .r871xs_drv.id_table = sdio_ids,
97         #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)) 
98         .r871xs_drv.drv = {
99                 .pm = &rtw_sdio_pm_ops,
100         }
101         #endif
102 };
103
104 static void sd_sync_int_hdl(struct sdio_func *func)
105 {
106         struct dvobj_priv *psdpriv;
107
108         psdpriv = sdio_get_drvdata(func);
109
110         if (!psdpriv->if1) {
111                 DBG_871X("%s if1 == NULL\n", __func__);
112                 return;
113         }
114
115         rtw_sdio_set_irq_thd(psdpriv, current);
116         sd_int_hdl(psdpriv->if1);
117         rtw_sdio_set_irq_thd(psdpriv, NULL);
118 }
119
120 int sdio_alloc_irq(struct dvobj_priv *dvobj)
121 {
122         PSDIO_DATA psdio_data;
123         struct sdio_func *func;
124         int err;
125
126         psdio_data = &dvobj->intf_data;
127         func = psdio_data->func;
128
129         sdio_claim_host(func);
130
131         err = sdio_claim_irq(func, &sd_sync_int_hdl);
132         if (err)
133         {
134                 dvobj->drv_dbg.dbg_sdio_alloc_irq_error_cnt++;
135                 printk(KERN_CRIT "%s: sdio_claim_irq FAIL(%d)!\n", __func__, err);
136         }
137         else
138         {
139                 dvobj->drv_dbg.dbg_sdio_alloc_irq_cnt++;
140                 dvobj->irq_alloc = 1;
141         }
142
143         sdio_release_host(func);
144
145         return err?_FAIL:_SUCCESS;
146 }
147
148 void sdio_free_irq(struct dvobj_priv *dvobj)
149 {
150     PSDIO_DATA psdio_data;
151     struct sdio_func *func;
152     int err;
153
154     if (dvobj->irq_alloc) {
155         psdio_data = &dvobj->intf_data;
156         func = psdio_data->func;
157
158         if (func) {
159             sdio_claim_host(func);
160             err = sdio_release_irq(func);
161             if (err)
162             {
163                                 dvobj->drv_dbg.dbg_sdio_free_irq_error_cnt++;
164                                 DBG_871X_LEVEL(_drv_err_,"%s: sdio_release_irq FAIL(%d)!\n", __func__, err);
165             }
166             else
167                 dvobj->drv_dbg.dbg_sdio_free_irq_cnt++;
168             sdio_release_host(func);
169         }
170         dvobj->irq_alloc = 0;
171     }
172 }
173
174 #ifdef CONFIG_GPIO_WAKEUP
175 extern unsigned int oob_irq;
176 extern unsigned int oob_gpio;
177 static irqreturn_t gpio_hostwakeup_irq_thread(int irq, void *data)
178 {
179         PADAPTER padapter = (PADAPTER)data;
180         DBG_871X_LEVEL(_drv_always_, "gpio_hostwakeup_irq_thread\n");
181         /* Disable interrupt before calling handler */
182         //disable_irq_nosync(oob_irq);
183         rtw_lock_suspend_timeout(HZ/2);
184 #ifdef CONFIG_PLATFORM_ARM_SUN6I
185         return 0;
186 #else
187         return IRQ_HANDLED;
188 #endif
189 }
190
191 static u8 gpio_hostwakeup_alloc_irq(PADAPTER padapter)
192 {
193         int err;
194         if (oob_irq == 0) {
195                 DBG_871X("oob_irq ZERO!\n");
196                 return _FAIL;
197         }
198
199         DBG_871X("%s : oob_irq = %d\n", __func__, oob_irq);
200         
201         /* dont set it IRQF_TRIGGER_LOW, or wowlan */
202         /* power is high after suspend */
203         /* and failing can prevent can not sleep issue if */
204         /* wifi gpio12 pin is not linked with CPU */
205         err = request_threaded_irq(oob_irq, gpio_hostwakeup_irq_thread, NULL,
206                 //IRQF_TRIGGER_LOW | IRQF_ONESHOT,
207                 IRQF_TRIGGER_FALLING,
208                 "rtw_wifi_gpio_wakeup", padapter);
209         if (err < 0) {
210                 DBG_871X("Oops: can't allocate gpio irq %d err:%d\n", oob_irq, err);
211                 return _FALSE;
212         } else {
213                 DBG_871X("allocate gpio irq %d ok\n", oob_irq);
214         }
215         
216 #ifndef CONFIG_PLATFORM_ARM_SUN8I       
217         enable_irq_wake(oob_irq);
218 #endif
219         return _SUCCESS;
220 }
221
222 static void gpio_hostwakeup_free_irq(PADAPTER padapter)
223 {
224         wifi_free_gpio(oob_gpio);
225
226         if (oob_irq == 0)
227                 return;
228                 
229 #ifndef CONFIG_PLATFORM_ARM_SUN8I
230         disable_irq_wake(oob_irq);
231 #endif
232         free_irq(oob_irq, padapter);
233 }
234 #endif
235
236 static u32 sdio_init(struct dvobj_priv *dvobj)
237 {
238         PSDIO_DATA psdio_data;
239         struct sdio_func *func;
240         int err;
241
242 _func_enter_;
243
244         psdio_data = &dvobj->intf_data;
245         func = psdio_data->func;
246
247         //3 1. init SDIO bus
248         sdio_claim_host(func);
249
250         err = sdio_enable_func(func);
251         if (err) {
252                 dvobj->drv_dbg.dbg_sdio_init_error_cnt++;
253                 DBG_8192C(KERN_CRIT "%s: sdio_enable_func FAIL(%d)!\n", __func__, err);
254                 goto release;
255         }
256
257         err = sdio_set_block_size(func, 512);
258         if (err) {
259                 dvobj->drv_dbg.dbg_sdio_init_error_cnt++;
260                 DBG_8192C(KERN_CRIT "%s: sdio_set_block_size FAIL(%d)!\n", __func__, err);
261                 goto release;
262         }
263         psdio_data->block_transfer_len = 512;
264         psdio_data->tx_block_mode = 1;
265         psdio_data->rx_block_mode = 1;
266
267 release:
268         sdio_release_host(func);
269
270 exit:
271 _func_exit_;
272
273         if (err) return _FAIL;
274         return _SUCCESS;
275 }
276
277 static void sdio_deinit(struct dvobj_priv *dvobj)
278 {
279         struct sdio_func *func;
280         int err;
281
282
283         RT_TRACE(_module_hci_intfs_c_, _drv_notice_, ("+sdio_deinit\n"));
284
285         func = dvobj->intf_data.func;
286
287         if (func) {
288                 sdio_claim_host(func);
289                 err = sdio_disable_func(func);
290                 if (err)
291                 {
292                         dvobj->drv_dbg.dbg_sdio_deinit_error_cnt++;
293                         DBG_8192C(KERN_ERR "%s: sdio_disable_func(%d)\n", __func__, err);
294                 }
295
296                 if (dvobj->irq_alloc) {
297                         err = sdio_release_irq(func);
298                         if (err)
299                         {
300                                 dvobj->drv_dbg.dbg_sdio_free_irq_error_cnt++;
301                                 DBG_8192C(KERN_ERR "%s: sdio_release_irq(%d)\n", __func__, err);
302                         }
303                         else
304                                 dvobj->drv_dbg.dbg_sdio_free_irq_cnt++;
305                 }
306
307                 sdio_release_host(func);
308         }
309 }
310 static struct dvobj_priv *sdio_dvobj_init(struct sdio_func *func)
311 {
312         int status = _FAIL;
313         struct dvobj_priv *dvobj = NULL;
314         PSDIO_DATA psdio;
315 _func_enter_;
316
317         if((dvobj = devobj_init()) == NULL) {
318                 goto exit;
319         }
320
321         sdio_set_drvdata(func, dvobj);
322
323         psdio = &dvobj->intf_data;
324         psdio->func = func;
325
326         if (sdio_init(dvobj) != _SUCCESS) {
327                 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("%s: initialize SDIO Failed!\n", __FUNCTION__));
328                 goto free_dvobj;
329         }
330         rtw_reset_continual_io_error(dvobj);
331         status = _SUCCESS;
332
333 free_dvobj:
334         if (status != _SUCCESS && dvobj) {
335                 sdio_set_drvdata(func, NULL);
336                 
337                 devobj_deinit(dvobj);
338                 
339                 dvobj = NULL;
340         }
341 exit:
342 _func_exit_;
343         return dvobj;
344 }
345
346 static void sdio_dvobj_deinit(struct sdio_func *func)
347 {
348         struct dvobj_priv *dvobj = sdio_get_drvdata(func);
349 _func_enter_;
350
351         sdio_set_drvdata(func, NULL);
352         if (dvobj) {
353                 sdio_deinit(dvobj);
354                 devobj_deinit(dvobj);
355         }
356
357 _func_exit_;
358         return;
359 }
360 static void rtw_decide_chip_type_by_device_id(PADAPTER padapter, const struct sdio_device_id  *pdid)
361 {
362         padapter->chip_type = pdid->driver_data;
363
364 #if defined(CONFIG_RTL8723A)
365         if( padapter->chip_type == RTL8723A){
366                 padapter->HardwareType = HARDWARE_TYPE_RTL8723AS;
367                 DBG_871X("CHIP TYPE: RTL8723A\n");
368         }
369 #endif
370
371 #if defined(CONFIG_RTL8188E)
372         if(padapter->chip_type == RTL8188E){
373                 padapter->HardwareType = HARDWARE_TYPE_RTL8188ES;
374                 DBG_871X("CHIP TYPE: RTL8188E\n");
375         }
376 #endif
377
378 #if defined(CONFIG_RTL8723B)
379         padapter->chip_type = RTL8723B;
380         padapter->HardwareType = HARDWARE_TYPE_RTL8723BS;
381 #endif
382
383 #if defined(CONFIG_RTL8821A)
384         if (padapter->chip_type == RTL8821) {
385                 padapter->HardwareType = HARDWARE_TYPE_RTL8821S;
386                 DBG_871X("CHIP TYPE: RTL8821A\n");
387         }
388 #endif
389
390 #if defined(CONFIG_RTL8192E)
391         if (padapter->chip_type == RTL8192E) {
392                 padapter->HardwareType = HARDWARE_TYPE_RTL8192E;
393                 DBG_871X("CHIP TYPE: RTL8192E\n");
394         }
395 #endif
396
397
398 }
399
400 void rtw_set_hal_ops(PADAPTER padapter)
401 {
402         //alloc memory for HAL DATA
403         rtw_hal_data_init(padapter);
404         
405 #if defined(CONFIG_RTL8723A)
406         if( padapter->chip_type == RTL8723A){
407                 rtl8723as_set_hal_ops(padapter);
408         }
409 #endif
410 #if defined(CONFIG_RTL8188E)
411         if(padapter->chip_type == RTL8188E){
412                 rtl8188es_set_hal_ops(padapter);
413         }
414 #endif
415 #if defined(CONFIG_RTL8723B)
416         if(padapter->chip_type == RTL8723B){
417                 rtl8723bs_set_hal_ops(padapter);
418         }
419 #endif
420 #if defined(CONFIG_RTL8821A)
421         if(padapter->chip_type == RTL8821){
422                 rtl8821as_set_hal_ops(padapter);
423         }
424 #endif
425
426 #if defined(CONFIG_RTL8192E)
427         if(padapter->chip_type == RTL8192E){
428                 rtl8192es_set_hal_ops(padapter);
429         }
430 #endif
431
432 }
433
434 static void sd_intf_start(PADAPTER padapter)
435 {
436         if (padapter == NULL) {
437                 DBG_8192C(KERN_ERR "%s: padapter is NULL!\n", __func__);
438                 return;
439         }
440
441         // hal dep
442         rtw_hal_enable_interrupt(padapter);
443 }
444
445 static void sd_intf_stop(PADAPTER padapter)
446 {
447         if (padapter == NULL) {
448                 DBG_8192C(KERN_ERR "%s: padapter is NULL!\n", __func__);
449                 return;
450         }
451
452         // hal dep
453         rtw_hal_disable_interrupt(padapter);
454 }
455
456
457 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
458 PADAPTER g_test_adapter = NULL;
459 #endif // RTW_SUPPORT_PLATFORM_SHUTDOWN
460
461 _adapter *rtw_sdio_if1_init(struct dvobj_priv *dvobj, const struct sdio_device_id  *pdid)
462 {
463         int status = _FAIL;
464         struct net_device *pnetdev;
465         PADAPTER padapter = NULL;
466         
467         if ((padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter))) == NULL) {
468                 goto exit;
469         }
470
471 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
472         g_test_adapter = padapter;
473 #endif // RTW_SUPPORT_PLATFORM_SHUTDOWN
474         padapter->dvobj = dvobj;
475         dvobj->if1 = padapter;
476
477         padapter->bDriverStopped=_TRUE;
478
479         dvobj->padapters[dvobj->iface_nums++] = padapter;
480         padapter->iface_id = IFACE_ID0;
481
482 #if defined(CONFIG_CONCURRENT_MODE) || defined(CONFIG_DUALMAC_CONCURRENT)
483         //set adapter_type/iface type for primary padapter
484         padapter->isprimary = _TRUE;
485         padapter->adapter_type = PRIMARY_ADAPTER;       
486         #ifndef CONFIG_HWPORT_SWAP
487         padapter->iface_type = IFACE_PORT0;
488         #else
489         padapter->iface_type = IFACE_PORT1;
490         #endif
491 #endif
492
493         padapter->interface_type = RTW_SDIO;
494         rtw_decide_chip_type_by_device_id(padapter, pdid);
495         
496         //3 1. init network device data
497         pnetdev = rtw_init_netdev(padapter);
498         if (!pnetdev)
499                 goto free_adapter;
500         
501         SET_NETDEV_DEV(pnetdev, dvobj_to_dev(dvobj));
502
503         padapter = rtw_netdev_priv(pnetdev);
504
505 #ifdef CONFIG_IOCTL_CFG80211
506         rtw_wdev_alloc(padapter, dvobj_to_dev(dvobj));
507 #endif
508
509         //3 3. init driver special setting, interface, OS and hardware relative
510
511         //4 3.1 set hardware operation functions
512         rtw_set_hal_ops(padapter);
513
514
515         //3 5. initialize Chip version
516         padapter->intf_start = &sd_intf_start;
517         padapter->intf_stop = &sd_intf_stop;
518
519         padapter->intf_init = &sdio_init;
520         padapter->intf_deinit = &sdio_deinit;
521         padapter->intf_alloc_irq = &sdio_alloc_irq;
522         padapter->intf_free_irq = &sdio_free_irq;
523
524         if (rtw_init_io_priv(padapter, sdio_set_intf_ops) == _FAIL)
525         {
526                 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
527                         ("rtw_drv_init: Can't init io_priv\n"));
528                 goto free_hal_data;
529         }
530
531         rtw_hal_read_chip_version(padapter);
532
533         rtw_hal_chip_configure(padapter);
534
535 #ifdef CONFIG_BT_COEXIST
536         rtw_btcoex_Initialize(padapter);
537 #endif // CONFIG_BT_COEXIST
538
539         //3 6. read efuse/eeprom data
540         rtw_hal_read_chip_info(padapter);
541
542         //3 7. init driver common data
543         if (rtw_init_drv_sw(padapter) == _FAIL) {
544                 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
545                          ("rtw_drv_init: Initialize driver software resource Failed!\n"));
546                 goto free_hal_data;
547         }
548
549         //3 8. get WLan MAC address
550         // set mac addr
551         rtw_macaddr_cfg(padapter->eeprompriv.mac_addr);
552         rtw_init_wifidirect_addrs(padapter, padapter->eeprompriv.mac_addr, padapter->eeprompriv.mac_addr);
553
554         rtw_hal_disable_interrupt(padapter);
555
556         DBG_871X("bDriverStopped:%d, bSurpriseRemoved:%d, bup:%d, hw_init_completed:%d\n"
557                 ,padapter->bDriverStopped
558                 ,padapter->bSurpriseRemoved
559                 ,padapter->bup
560                 ,padapter->hw_init_completed
561         );
562         
563         status = _SUCCESS;
564         
565 free_hal_data:
566         if(status != _SUCCESS && padapter->HalData)
567                 rtw_mfree(padapter->HalData, sizeof(*(padapter->HalData)));
568
569 free_wdev:
570         if(status != _SUCCESS) {
571                 #ifdef CONFIG_IOCTL_CFG80211
572                 rtw_wdev_unregister(padapter->rtw_wdev);
573                 rtw_wdev_free(padapter->rtw_wdev);
574                 #endif
575         }
576
577 free_adapter:
578         if (status != _SUCCESS) {
579                 if (pnetdev)
580                         rtw_free_netdev(pnetdev);
581                 else
582                         rtw_vmfree((u8*)padapter, sizeof(*padapter));
583                 padapter = NULL;
584         }
585 exit:
586         return padapter;
587 }
588
589 static void rtw_sdio_if1_deinit(_adapter *if1)
590 {
591         struct net_device *pnetdev = if1->pnetdev;
592         struct mlme_priv *pmlmepriv= &if1->mlmepriv;
593
594         if(check_fwstate(pmlmepriv, _FW_LINKED))
595                 rtw_disassoc_cmd(if1, 0, _FALSE);
596
597 #ifdef CONFIG_AP_MODE
598         free_mlme_ap_info(if1);
599         #ifdef CONFIG_HOSTAPD_MLME
600         hostapd_mode_unload(if1);
601         #endif
602 #endif
603
604 #ifdef CONFIG_GPIO_WAKEUP
605 #ifdef CONFIG_PLATFORM_ARM_SUN6I 
606         sw_gpio_eint_set_enable(gpio_eint_wlan, 0);
607         sw_gpio_irq_free(eint_wlan_handle);
608 #else  
609         gpio_hostwakeup_free_irq(if1);
610 #endif
611 #endif
612
613         rtw_cancel_all_timer(if1);
614
615 #ifdef CONFIG_WOWLAN
616         adapter_to_pwrctl(if1)->wowlan_mode=_FALSE;
617         DBG_871X_LEVEL(_drv_always_, "%s wowlan_mode:%d\n", __func__, adapter_to_pwrctl(if1)->wowlan_mode);
618 #endif //CONFIG_WOWLAN
619
620         rtw_dev_unload(if1);
621         DBG_871X("+r871xu_dev_remove, hw_init_completed=%d\n", if1->hw_init_completed);
622         
623         rtw_handle_dualmac(if1, 0);
624
625 #ifdef CONFIG_IOCTL_CFG80211
626         if (if1->rtw_wdev) {
627                 rtw_wdev_free(if1->rtw_wdev);
628         }
629 #endif
630
631         rtw_free_drv_sw(if1);
632
633         if(pnetdev)
634                 rtw_free_netdev(pnetdev);
635
636 #ifdef CONFIG_PLATFORM_RTD2880B
637         DBG_871X("wlan link down\n");
638         rtd2885_wlan_netlink_sendMsg("linkdown", "8712");
639 #endif
640
641 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
642         g_test_adapter = NULL;
643 #endif // RTW_SUPPORT_PLATFORM_SHUTDOWN
644 }
645
646 /*
647  * drv_init() - a device potentially for us
648  *
649  * notes: drv_init() is called when the bus driver has located a card for us to support.
650  *        We accept the new device by returning 0.
651  */
652 static int rtw_drv_init(
653         struct sdio_func *func,
654         const struct sdio_device_id *id)
655 {
656         int status = _FAIL;
657         struct net_device *pnetdev;
658         PADAPTER if1 = NULL, if2 = NULL;
659         struct dvobj_priv *dvobj;
660
661 #ifdef CONFIG_PLATFORM_INTEL_BYT
662
663 #ifdef CONFIG_ACPI
664         acpi_handle handle;
665         struct acpi_device *adev;
666 #endif
667
668 #if defined(CONFIG_ACPI) && defined(CONFIG_GPIO_WAKEUP)
669         handle = ACPI_HANDLE(&func->dev);
670
671         if (handle) {
672                 /* Dont try to do acpi pm for the wifi module */
673                 if (!handle || acpi_bus_get_device(handle, &adev))
674                         DBG_871X("Could not get acpi pointer!\n");
675                 else {
676                         adev->flags.power_manageable = 0;
677                         DBG_871X("Disabling ACPI power management support!\n");
678                 }
679                 oob_gpio = acpi_get_gpio_by_index(&func->dev, 0, NULL);
680                 DBG_871X("rtw_drv_init: ACPI_HANDLE found oob_gpio %d!\n", oob_gpio);
681                 wifi_configure_gpio();
682         }
683         else
684                 DBG_871X("rtw_drv_init: ACPI_HANDLE NOT found!\n");
685 #endif
686
687 #if defined(CONFIG_ACPI)
688         if (&func->dev && ACPI_HANDLE(&func->dev)) {
689                 wlan_en_gpio = acpi_get_gpio_by_index(&func->dev, 1, NULL);
690                 DBG_871X("rtw_drv_init: ACPI_HANDLE found wlan_en %d!\n", wlan_en_gpio);
691         }
692         else
693                 DBG_871X("rtw_drv_init: ACPI_HANDLE NOT found!\n");
694 #endif
695 #endif //CONFIG_PLATFORM_INTEL_BYT
696
697
698         RT_TRACE(_module_hci_intfs_c_, _drv_info_,
699                 ("+rtw_drv_init: vendor=0x%04x device=0x%04x class=0x%02x\n",
700                 func->vendor, func->device, func->class));
701
702         if ((dvobj = sdio_dvobj_init(func)) == NULL) {
703                 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("initialize device object priv Failed!\n"));
704                 goto exit;
705         }
706
707         if ((if1 = rtw_sdio_if1_init(dvobj, id)) == NULL) {
708                 DBG_871X("rtw_init_primary_adapter Failed!\n");
709                 goto free_dvobj;
710         }
711
712 #ifdef CONFIG_CONCURRENT_MODE
713         if ((if2 = rtw_drv_if2_init(if1, sdio_set_intf_ops)) == NULL) {
714                 goto free_if1;
715         }
716 #endif
717
718         //dev_alloc_name && register_netdev
719         if((status = rtw_drv_register_netdev(if1)) != _SUCCESS) {
720                 goto free_if2;
721         }
722
723 #ifdef CONFIG_HOSTAPD_MLME
724         hostapd_mode_init(if1);
725 #endif
726
727 #ifdef CONFIG_PLATFORM_RTD2880B
728         DBG_871X("wlan link up\n");
729         rtd2885_wlan_netlink_sendMsg("linkup", "8712");
730 #endif
731
732         if (sdio_alloc_irq(dvobj) != _SUCCESS)
733                 goto free_if2;
734
735 #ifdef  CONFIG_GPIO_WAKEUP
736 #ifdef CONFIG_PLATFORM_ARM_SUN6I
737         eint_wlan_handle = sw_gpio_irq_request(gpio_eint_wlan, TRIG_EDGE_NEGATIVE,(peint_handle)gpio_hostwakeup_irq_thread, NULL);
738         if (!eint_wlan_handle) {
739                DBG_871X( "%s: request irq failed\n",__func__);
740                return -1;
741   }
742 #else
743         gpio_hostwakeup_alloc_irq(if1);
744 #endif
745 #endif
746
747 #ifdef CONFIG_GLOBAL_UI_PID
748         if(ui_pid[1]!=0) {
749                 DBG_871X("ui_pid[1]:%d\n",ui_pid[1]);
750                 rtw_signal_process(ui_pid[1], SIGUSR2);
751         }
752 #endif
753
754         RT_TRACE(_module_hci_intfs_c_,_drv_err_,("-871x_drv - drv_init, success!\n"));
755
756         status = _SUCCESS;
757
758 free_if2:
759         if(status != _SUCCESS && if2) {
760                 #ifdef CONFIG_CONCURRENT_MODE
761                 rtw_drv_if2_stop(if2);
762                 rtw_drv_if2_free(if2);
763                 #endif
764         }
765 free_if1:
766         if (status != _SUCCESS && if1) {
767                 rtw_sdio_if1_deinit(if1);
768         }
769 free_dvobj:
770         if (status != _SUCCESS)
771                 sdio_dvobj_deinit(func);
772 exit:
773         return status == _SUCCESS?0:-ENODEV;
774 }
775
776 static void rtw_dev_remove(struct sdio_func *func)
777 {
778         struct dvobj_priv *dvobj = sdio_get_drvdata(func);
779         struct pwrctrl_priv *pwrctl = dvobj_to_pwrctl(dvobj);
780         PADAPTER padapter = dvobj->if1;
781
782 _func_enter_;
783
784         RT_TRACE(_module_hci_intfs_c_, _drv_notice_, ("+rtw_dev_remove\n"));
785
786         dvobj->processing_dev_remove = _TRUE;
787
788         rtw_unregister_netdevs(dvobj);
789
790         if (padapter->bSurpriseRemoved == _FALSE) {
791                 int err;
792
793                 /* test surprise remove */
794                 sdio_claim_host(func);
795                 sdio_readb(func, 0, &err);
796                 sdio_release_host(func);
797                 if (err == -ENOMEDIUM) {
798                         padapter->bSurpriseRemoved = _TRUE;
799                         DBG_871X(KERN_NOTICE "%s: device had been removed!\n", __func__);
800                 }
801         }
802
803 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
804         rtw_unregister_early_suspend(pwrctl);
805 #endif
806
807         rtw_ps_deny(padapter, PS_DENY_DRV_REMOVE);
808
809         rtw_pm_set_ips(padapter, IPS_NONE);
810         rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
811
812         LeaveAllPowerSaveMode(padapter);
813
814 #ifdef CONFIG_CONCURRENT_MODE
815         rtw_drv_if2_stop(dvobj->if2);
816 #endif
817
818 #ifdef CONFIG_BT_COEXIST
819         rtw_btcoex_HaltNotify(padapter);
820 #endif
821
822         rtw_sdio_if1_deinit(padapter);
823
824 #ifdef CONFIG_CONCURRENT_MODE
825         rtw_drv_if2_free(dvobj->if2);
826 #endif
827
828         sdio_dvobj_deinit(func);
829
830         RT_TRACE(_module_hci_intfs_c_, _drv_notice_, ("-rtw_dev_remove\n"));
831
832 _func_exit_;
833 }
834 extern int pm_netdev_open(struct net_device *pnetdev,u8 bnormal);
835 extern int pm_netdev_close(struct net_device *pnetdev,u8 bnormal);
836
837 static int rtw_sdio_suspend(struct device *dev)
838 {
839         struct sdio_func *func =dev_to_sdio_func(dev);
840         struct dvobj_priv *psdpriv = sdio_get_drvdata(func);
841         struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv);
842         _adapter *padapter = psdpriv->if1;
843         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
844         int ret = 0;
845         u8 ch, bw, offset;
846
847         if(padapter->bDriverStopped == _TRUE)
848         {
849                 DBG_871X("%s bDriverStopped = %d\n", __FUNCTION__, padapter->bDriverStopped);
850                 goto exit;
851         }
852
853         if (pwrpriv->bInSuspend == _TRUE)
854         {
855                 DBG_871X("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend);
856                 pdbgpriv->dbg_suspend_error_cnt++;
857                 goto exit;
858         }
859
860         ret = rtw_suspend_common(padapter);             
861
862 exit:
863 #ifdef CONFIG_RTW_SDIO_PM_KEEP_POWER 
864 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34))
865         //Android 4.0 don't support WIFI close power
866         //or power down or clock will close after wifi resume,
867         //this is sprd's bug in Android 4.0, but sprd don't
868         //want to fix it.
869         //we have test power under 8723as, power consumption is ok
870         if (func) {
871                 mmc_pm_flag_t pm_flag = 0;
872                 pm_flag = sdio_get_host_pm_caps(func);
873                 DBG_871X("cmd: %s: suspend: PM flag = 0x%x\n", sdio_func_id(func), pm_flag);
874                 if (!(pm_flag & MMC_PM_KEEP_POWER)) {
875                         DBG_871X("%s: cannot remain alive while host is suspended\n", sdio_func_id(func));
876                         pdbgpriv->dbg_suspend_error_cnt++;
877                         return -ENOSYS;
878                 } else {
879                         DBG_871X("cmd: suspend with MMC_PM_KEEP_POWER\n");
880                         sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
881                 }
882         }
883 #endif  
884 #endif
885         return ret;
886 }
887 int rtw_resume_process(_adapter *padapter)
888 {
889         struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
890         struct dvobj_priv *psdpriv = padapter->dvobj;
891         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
892                 
893         if (pwrpriv->bInSuspend == _FALSE)
894         {
895                 pdbgpriv->dbg_resume_error_cnt++;
896                 DBG_871X("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend);
897                 return -1;
898         }
899         
900         return rtw_resume_common(padapter);
901 }
902
903 static int rtw_sdio_resume(struct device *dev)
904 {
905         struct sdio_func *func =dev_to_sdio_func(dev);
906         struct dvobj_priv *psdpriv = sdio_get_drvdata(func);
907         struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv);
908         _adapter *padapter = psdpriv->if1;
909         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
910         int ret = 0;
911         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
912
913         DBG_871X("==> %s (%s:%d)\n",__FUNCTION__, current->comm, current->pid);
914
915         pdbgpriv->dbg_resume_cnt++;
916
917         if(pwrpriv->bInternalAutoSuspend)
918         {
919                 ret = rtw_resume_process(padapter);
920         }
921         else
922         {
923 #ifdef CONFIG_PLATFORM_INTEL_BYT
924                 if(0)
925 #else
926                 if(pwrpriv->wowlan_mode || pwrpriv->wowlan_ap_mode)
927 #endif
928                 {
929                         rtw_resume_lock_suspend();                      
930                         ret = rtw_resume_process(padapter);
931                         rtw_resume_unlock_suspend();
932                 }
933                 else
934                 {
935 #ifdef CONFIG_RESUME_IN_WORKQUEUE
936                         rtw_resume_in_workqueue(pwrpriv);
937 #else                   
938                         if (rtw_is_earlysuspend_registered(pwrpriv))
939                         {
940                                 /* jeff: bypass resume here, do in late_resume */
941                                 rtw_set_do_late_resume(pwrpriv, _TRUE);
942                         }       
943                         else
944                         {
945                                 rtw_resume_lock_suspend();                      
946                                 ret = rtw_resume_process(padapter);
947                                 rtw_resume_unlock_suspend();
948                         }
949 #endif
950                 }
951         }
952         pmlmeext->last_scan_time = rtw_get_current_time();
953         DBG_871X("<========  %s return %d\n", __FUNCTION__, ret);
954         return ret;
955
956 }
957
958 static int  rtw_drv_entry(void)
959 {
960         int ret = 0;
961
962         DBG_871X_LEVEL(_drv_always_, "module init start\n");
963         dump_drv_version(RTW_DBGDUMP);
964 #ifdef BTCOEXVERSION
965         DBG_871X_LEVEL(_drv_always_, DRV_NAME" BT-Coex version = %s\n", BTCOEXVERSION);
966 #endif // BTCOEXVERSION
967
968         ret = platform_wifi_power_on();
969         if (ret)
970         {
971                 DBG_871X("%s: power on failed!!(%d)\n", __FUNCTION__, ret);
972                 ret = -1;
973                 goto exit;
974         }
975
976         sdio_drvpriv.drv_registered = _TRUE;
977         rtw_suspend_lock_init();
978         rtw_drv_proc_init();
979         rtw_ndev_notifier_register();
980
981         ret = sdio_register_driver(&sdio_drvpriv.r871xs_drv);
982         if (ret != 0)
983         {
984                 sdio_drvpriv.drv_registered = _FALSE;
985                 rtw_suspend_lock_uninit();
986                 rtw_drv_proc_deinit();
987                 rtw_ndev_notifier_unregister();
988                 DBG_871X("%s: register driver failed!!(%d)\n", __FUNCTION__, ret);
989                 goto poweroff;
990         }
991
992 #ifndef CONFIG_PLATFORM_INTEL_BYT
993         rtw_android_wifictrl_func_add();
994 #endif //!CONFIG_PLATFORM_INTEL_BYT
995         goto exit;
996
997 poweroff:
998         platform_wifi_power_off();
999
1000 exit:
1001         DBG_871X_LEVEL(_drv_always_, "module init ret=%d\n", ret);
1002         return ret;
1003 }
1004
1005 static void  rtw_drv_halt(void)
1006 {
1007         DBG_871X_LEVEL(_drv_always_, "module exit start\n");
1008
1009         sdio_drvpriv.drv_registered = _FALSE;
1010
1011         sdio_unregister_driver(&sdio_drvpriv.r871xs_drv);
1012
1013         rtw_android_wifictrl_func_del();
1014
1015         platform_wifi_power_off();
1016
1017         rtw_suspend_lock_uninit();
1018         rtw_drv_proc_deinit();
1019         rtw_ndev_notifier_unregister();
1020
1021         DBG_871X_LEVEL(_drv_always_, "module exit success\n");
1022
1023         rtw_mstat_dump(RTW_DBGDUMP);
1024 }
1025
1026 #ifdef CONFIG_PLATFORM_INTEL_BYT
1027 int rtw_sdio_set_power(int on)
1028 {
1029
1030         if(wlan_en_gpio >= 0){
1031                 if(on)
1032                         gpio_set_value(wlan_en_gpio,1);
1033                 else
1034                         gpio_set_value(wlan_en_gpio,0);
1035         }
1036
1037         return 0;
1038 }
1039 #endif //CONFIG_PLATFORM_INTEL_BYT
1040
1041 #include "wifi_version.h"
1042 #include <linux/rfkill-wlan.h>
1043 extern int rockchip_wifi_power(int on);
1044 extern int rockchip_wifi_set_carddetect(int val);
1045 extern int get_wifi_chip_type(void);
1046
1047 int rockchip_wifi_init_module_rtkwifi(void)
1048 {
1049 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
1050     int type = get_wifi_chip_type();
1051     if (type < WIFI_AP6XXX_SERIES || type == WIFI_ESP8089) return 0;
1052 #endif
1053     printk("\n");
1054     printk("=======================================================\n");
1055     printk("==== Launching Wi-Fi driver! (Powered by Rockchip) ====\n");
1056     printk("=======================================================\n");
1057     printk("Realtek 8723BS SDIO WiFi driver (Powered by Rockchip,Ver %s) init.\n", RTL8723BS_DRV_VERSION);
1058
1059     rockchip_wifi_power(1);
1060     rockchip_wifi_set_carddetect(1);
1061     return rtw_drv_entry();
1062 }
1063
1064 void rockchip_wifi_exit_module_rtkwifi(void)
1065 {
1066 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
1067     int type = get_wifi_chip_type();
1068     if (type < WIFI_AP6XXX_SERIES || type == WIFI_ESP8089) return;
1069 #endif
1070     printk("\n");
1071     printk("=======================================================\n");
1072     printk("==== Dislaunching Wi-Fi driver! (Powered by Rockchip) ====\n");
1073     printk("=======================================================\n");
1074     printk("Realtek 8723BS SDIO WiFi driver (Powered by Rockchip,Ver %s) init.\n", RTL8723BS_DRV_VERSION);
1075
1076     rtw_drv_halt();
1077
1078     rockchip_wifi_set_carddetect(0);
1079     rockchip_wifi_power(0);
1080 }
1081
1082 #ifdef CONFIG_WIFI_LOAD_DRIVER_WHEN_KERNEL_BOOTUP
1083 late_initcall(rockchip_wifi_init_module_rtkwifi);
1084 module_exit(rockchip_wifi_exit_module_rtkwifi);
1085 #else
1086 EXPORT_SYMBOL(rockchip_wifi_init_module_rtkwifi);
1087 EXPORT_SYMBOL(rockchip_wifi_exit_module_rtkwifi);
1088 #endif
1089 //module_init(rtw_drv_entry);
1090 //module_exit(rtw_drv_halt);
1091