xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8723ds/os_dep/linux/sdio_intf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2019 Realtek Corporation.
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  *****************************************************************************/
15 #define _HCI_INTF_C_
16 
17 #include <drv_types.h>
18 #include <hal_data.h>
19 #include <platform_ops.h>
20 
21 #ifndef CONFIG_SDIO_HCI
22 #error "CONFIG_SDIO_HCI shall be on!\n"
23 #endif
24 
25 #ifdef CONFIG_RTL8822B
26 #include <rtl8822b_hal.h>	/* rtl8822bs_set_hal_ops() */
27 #endif /* CONFIG_RTL8822B */
28 
29 #ifdef CONFIG_RTL8822C
30 #include <rtl8822c_hal.h>
31 #endif /* CONFIG_RTL8822C */
32 
33 #ifdef CONFIG_RTL8723F
34 #include <rtl8723f_hal.h>	/* rtl8723fs_set_hal_ops() */
35 #endif /* CONFIG_RTL8723F */
36 
37 #ifdef CONFIG_PLATFORM_INTEL_BYT
38 #ifdef CONFIG_ACPI
39 #include <linux/acpi.h>
40 #include <linux/acpi_gpio.h>
41 #include "rtw_android.h"
42 #endif
43 static int wlan_en_gpio = -1;
44 #endif /* CONFIG_PLATFORM_INTEL_BYT */
45 
46 #ifndef dev_to_sdio_func
47 #define dev_to_sdio_func(d)     container_of(d, struct sdio_func, dev)
48 #endif
49 
50 static const struct sdio_device_id sdio_ids[] = {
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 #ifdef CONFIG_RTL8703B
67 	{ SDIO_DEVICE(0x024c, 0xB703), .driver_data = RTL8703B},
68 #endif
69 
70 #ifdef CONFIG_RTL8188F
71 	{SDIO_DEVICE(0x024c, 0xF179), .driver_data = RTL8188F},
72 #endif
73 
74 #ifdef CONFIG_RTL8188GTV
75 	{SDIO_DEVICE(0x024c, 0x018C), .driver_data = RTL8188GTV},
76 #endif
77 
78 #ifdef CONFIG_RTL8822B
79 	{SDIO_DEVICE(0x024c, 0xB822), .driver_data = RTL8822B},
80 #endif
81 
82 #ifdef CONFIG_RTL8723D
83 	{ SDIO_DEVICE(0x024c, 0xD723), .driver_data = RTL8723D},
84 	{ SDIO_DEVICE(0x024c, 0xD724), .driver_data = RTL8723D},
85 #endif
86 
87 #ifdef CONFIG_RTL8192F
88 	{ SDIO_DEVICE(0x024c, 0x818C), .driver_data = RTL8192F},/*A CUT*/
89 	{ SDIO_DEVICE(0x024c, 0xF192), .driver_data = RTL8192F},/*B CUT*/
90 	{ SDIO_DEVICE(0x024c, 0xA725), .driver_data = RTL8192F},/*8725AS*/
91 #endif /* CONFIG_RTL8192F */
92 
93 #ifdef CONFIG_RTL8821C
94 	{SDIO_DEVICE(0x024C, 0xB821), .driver_data = RTL8821C},
95 	{SDIO_DEVICE(0x024C, 0xC821), .driver_data = RTL8821C},
96 	{SDIO_DEVICE(0x024C, 0x8733), .driver_data = RTL8821C}, /* 8733AS */
97 	{SDIO_DEVICE(0x024C, 0xC80C), .driver_data = RTL8821C}, /* 8821CSH-VQ */
98 #endif
99 
100 #ifdef CONFIG_RTL8822C
101 	{SDIO_DEVICE(0x024c, 0xC822), .class = SDIO_CLASS_WLAN, .driver_data = RTL8822C},
102 	{SDIO_DEVICE(0x024c, 0xD821), .class = SDIO_CLASS_WLAN, .driver_data = RTL8822C}, /* 8821DS */
103 #endif
104 
105 #ifdef CONFIG_RTL8723F
106 	{SDIO_DEVICE(0x024c, 0xB733), .class = SDIO_CLASS_WLAN, .driver_data = RTL8723F}, /* SDIO+UART */
107 	{SDIO_DEVICE(0x024c, 0xB73A), .class = SDIO_CLASS_WLAN, .driver_data = RTL8723F}, /* SDIO multi */
108 #endif
109 
110 #if defined(RTW_ENABLE_WIFI_CONTROL_FUNC) /* temporarily add this to accept all sdio wlan id */
111 	{ SDIO_DEVICE_CLASS(SDIO_CLASS_WLAN) },
112 #endif
113 	{ /* end: all zeroes */				},
114 };
115 
116 MODULE_DEVICE_TABLE(sdio, sdio_ids);
117 
118 static int rtw_drv_init(struct sdio_func *func, const struct sdio_device_id *id);
119 static void rtw_dev_remove(struct sdio_func *func);
120 #ifdef CONFIG_SDIO_HOOK_DEV_SHUTDOWN
121 static void rtw_dev_shutdown(struct device *dev);
122 #endif
123 static int rtw_sdio_resume(struct device *dev);
124 static int rtw_sdio_suspend(struct device *dev);
125 extern void rtw_dev_unload(PADAPTER padapter);
126 
127 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
128 static const struct dev_pm_ops rtw_sdio_pm_ops = {
129 	.suspend	= rtw_sdio_suspend,
130 	.resume	= rtw_sdio_resume,
131 };
132 #endif
133 
134 struct sdio_drv_priv {
135 	struct sdio_driver r871xs_drv;
136 	int drv_registered;
137 };
138 
139 static struct sdio_drv_priv sdio_drvpriv = {
140 	.r871xs_drv.probe = rtw_drv_init,
141 	.r871xs_drv.remove = rtw_dev_remove,
142 	.r871xs_drv.name = (char *)DRV_NAME,
143 	.r871xs_drv.id_table = sdio_ids,
144 	.r871xs_drv.drv = {
145 #ifdef CONFIG_SDIO_HOOK_DEV_SHUTDOWN
146 		.shutdown = rtw_dev_shutdown,
147 #endif
148 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
149 		.pm = &rtw_sdio_pm_ops,
150 #endif
151 	}
152 };
153 
154 static struct rtw_if_operations sdio_ops = {
155 	.read		= rtw_sdio_raw_read,
156 	.write		= rtw_sdio_raw_write,
157 };
158 
sd_sync_int_hdl(struct sdio_func * func)159 static void sd_sync_int_hdl(struct sdio_func *func)
160 {
161 	struct dvobj_priv *psdpriv;
162 
163 	psdpriv = sdio_get_drvdata(func);
164 
165 	if (!dvobj_get_primary_adapter(psdpriv)) {
166 		RTW_INFO("%s primary adapter == NULL\n", __func__);
167 		return;
168 	}
169 
170 	rtw_sdio_set_irq_thd(psdpriv, current);
171 	sd_int_hdl(dvobj_get_primary_adapter(psdpriv));
172 	rtw_sdio_set_irq_thd(psdpriv, NULL);
173 }
174 
sdio_alloc_irq(struct dvobj_priv * dvobj)175 int sdio_alloc_irq(struct dvobj_priv *dvobj)
176 {
177 	PSDIO_DATA psdio_data;
178 	struct sdio_func *func;
179 	int err;
180 
181 	psdio_data = &dvobj->intf_data;
182 	func = psdio_data->func;
183 
184 	sdio_claim_host(func);
185 
186 	err = sdio_claim_irq(func, &sd_sync_int_hdl);
187 	if (err) {
188 		dvobj->drv_dbg.dbg_sdio_alloc_irq_error_cnt++;
189 		RTW_PRINT("%s: sdio_claim_irq FAIL(%d)!\n", __func__, err);
190 	} else {
191 		dvobj->drv_dbg.dbg_sdio_alloc_irq_cnt++;
192 		dvobj->irq_alloc = 1;
193 	}
194 
195 	sdio_release_host(func);
196 
197 	return err ? _FAIL : _SUCCESS;
198 }
199 
sdio_free_irq(struct dvobj_priv * dvobj)200 void sdio_free_irq(struct dvobj_priv *dvobj)
201 {
202 	PSDIO_DATA psdio_data;
203 	struct sdio_func *func;
204 	int err;
205 
206 	if (dvobj->irq_alloc) {
207 		psdio_data = &dvobj->intf_data;
208 		func = psdio_data->func;
209 
210 		if (func) {
211 			sdio_claim_host(func);
212 			err = sdio_release_irq(func);
213 			if (err) {
214 				dvobj->drv_dbg.dbg_sdio_free_irq_error_cnt++;
215 				RTW_ERR("%s: sdio_release_irq FAIL(%d)!\n", __func__, err);
216 			} else
217 				dvobj->drv_dbg.dbg_sdio_free_irq_cnt++;
218 			sdio_release_host(func);
219 		}
220 		dvobj->irq_alloc = 0;
221 	}
222 }
223 
224 #ifdef CONFIG_GPIO_WAKEUP
225 extern unsigned int oob_irq;
226 extern unsigned int oob_gpio;
gpio_hostwakeup_irq_thread(int irq,void * data)227 static irqreturn_t gpio_hostwakeup_irq_thread(int irq, void *data)
228 {
229 	PADAPTER padapter = (PADAPTER)data;
230 	RTW_PRINT("gpio_hostwakeup_irq_thread\n");
231 	/* Disable interrupt before calling handler */
232 	/* disable_irq_nosync(oob_irq); */
233 #ifdef CONFIG_PLATFORM_ARM_SUN6I
234 	return 0;
235 #else
236 	return IRQ_HANDLED;
237 #endif
238 }
239 
gpio_hostwakeup_alloc_irq(PADAPTER padapter)240 static u8 gpio_hostwakeup_alloc_irq(PADAPTER padapter)
241 {
242 	int err;
243 	u32 status = 0;
244 
245 	if (oob_irq == 0) {
246 		RTW_INFO("oob_irq ZERO!\n");
247 		return _FAIL;
248 	}
249 
250 	RTW_INFO("%s : oob_irq = %d\n", __func__, oob_irq);
251 
252 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
253 	status = IRQF_NO_SUSPEND;
254 #endif
255 
256 	if (HIGH_ACTIVE_DEV2HST)
257 		status |= IRQF_TRIGGER_RISING;
258 	else
259 		status |= IRQF_TRIGGER_FALLING;
260 
261 	err = request_threaded_irq(oob_irq, gpio_hostwakeup_irq_thread, NULL,
262 		status, "rtw_wifi_gpio_wakeup", padapter);
263 
264 	if (err < 0) {
265 		RTW_INFO("Oops: can't allocate gpio irq %d err:%d\n", oob_irq, err);
266 		return _FALSE;
267 	} else
268 		RTW_INFO("allocate gpio irq %d ok\n", oob_irq);
269 
270 #ifndef CONFIG_PLATFORM_ARM_SUN8I
271 	enable_irq_wake(oob_irq);
272 #endif
273 	return _SUCCESS;
274 }
275 
gpio_hostwakeup_free_irq(PADAPTER padapter)276 static void gpio_hostwakeup_free_irq(PADAPTER padapter)
277 {
278 	wifi_free_gpio(oob_gpio);
279 
280 	if (oob_irq == 0)
281 		return;
282 
283 #ifndef CONFIG_PLATFORM_ARM_SUN8I
284 	disable_irq_wake(oob_irq);
285 #endif
286 	free_irq(oob_irq, padapter);
287 }
288 #endif
289 
dump_sdio_card_info(void * sel,struct dvobj_priv * dvobj)290 void dump_sdio_card_info(void *sel, struct dvobj_priv *dvobj)
291 {
292 	PSDIO_DATA psdio_data = &dvobj->intf_data;
293 	struct mmc_card *card = psdio_data->card;
294 	int i;
295 
296 	RTW_PRINT_SEL(sel, "== SDIO Card Info ==\n");
297 	RTW_PRINT_SEL(sel, "  card: %p\n", card);
298 	RTW_PRINT_SEL(sel, "  clock: %d Hz\n", psdio_data->clock);
299 
300 	RTW_PRINT_SEL(sel, "  timing spec: ");
301 	switch (psdio_data->timing) {
302 	case MMC_TIMING_LEGACY:
303 		_RTW_PRINT_SEL(sel, "legacy");
304 		break;
305 	case MMC_TIMING_MMC_HS:
306 		_RTW_PRINT_SEL(sel, "mmc high-speed");
307 		break;
308 	case MMC_TIMING_SD_HS:
309 		_RTW_PRINT_SEL(sel, "sd high-speed");
310 		break;
311 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
312 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
313 	case MMC_TIMING_UHS_SDR12:
314 		_RTW_PRINT_SEL(sel, "sd uhs SDR12");
315 		break;
316 	case MMC_TIMING_UHS_SDR25:
317 		_RTW_PRINT_SEL(sel, "sd uhs SDR25");
318 		break;
319 	#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0) */
320 
321 	case MMC_TIMING_UHS_SDR50:
322 		_RTW_PRINT_SEL(sel, "sd uhs SDR50");
323 		break;
324 
325 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
326 	case MMC_TIMING_MMC_DDR52:
327 		_RTW_PRINT_SEL(sel, "mmc DDR52");
328 		break;
329 	#endif
330 
331 	case MMC_TIMING_UHS_SDR104:
332 		_RTW_PRINT_SEL(sel, "sd uhs SDR104");
333 		break;
334 	case MMC_TIMING_UHS_DDR50:
335 		_RTW_PRINT_SEL(sel, "sd uhs DDR50");
336 		break;
337 
338 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
339 	case MMC_TIMING_MMC_HS200:
340 		_RTW_PRINT_SEL(sel, "mmc HS200");
341 		break;
342 	#endif
343 
344 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
345 	case MMC_TIMING_MMC_HS400:
346 		_RTW_PRINT_SEL(sel, "mmc HS400");
347 		break;
348 	#endif
349 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
350 	default:
351 		_RTW_PRINT_SEL(sel, "unknown(%d)", psdio_data->timing);
352 		break;
353 	}
354 	_RTW_PRINT_SEL(sel, "\n");
355 
356 	RTW_PRINT_SEL(sel, "  sd3_bus_mode: %s\n", (psdio_data->sd3_bus_mode) ? "TRUE" : "FALSE");
357 
358 	rtw_warn_on(card->sdio_funcs != sdio_get_num_of_func(dvobj));
359 	RTW_PRINT_SEL(sel, "  func num: %u\n", card->sdio_funcs);
360 	for (i = 0; card->sdio_func[i]; i++) {
361 		RTW_PRINT_SEL(sel, "  func%u: %p%s\n"
362 			, card->sdio_func[i]->num, card->sdio_func[i]
363 			, psdio_data->func == card->sdio_func[i] ? " (*)" : "");
364 	}
365 
366 	RTW_PRINT_SEL(sel, "================\n");
367 }
368 
369 #define SDIO_CARD_INFO_DUMP(dvobj)	dump_sdio_card_info(RTW_DBGDUMP, dvobj)
370 
371 #ifdef DBG_SDIO
372 #if (DBG_SDIO >= 2)
rtw_sdio_dbg_reg_free(struct dvobj_priv * d)373 void rtw_sdio_dbg_reg_free(struct dvobj_priv *d)
374 {
375 	struct sdio_data *sdio;
376 	u8 *buf;
377 	u32 size;
378 
379 
380 	sdio = &d->intf_data;
381 
382 	buf = sdio->dbg_msg;
383 	size = sdio->dbg_msg_size;
384 	if (buf){
385 		sdio->dbg_msg = NULL;
386 		sdio->dbg_msg_size = 0;
387 		rtw_mfree(buf, size);
388 	}
389 
390 	buf = sdio->reg_mac;
391 	if (buf) {
392 		sdio->reg_mac = NULL;
393 		rtw_mfree(buf, 0x800);
394 	}
395 
396 	buf = sdio->reg_mac_ext;
397 	if (buf) {
398 		sdio->reg_mac_ext = NULL;
399 		rtw_mfree(buf, 0x800);
400 	}
401 
402 	buf = sdio->reg_local;
403 	if (buf) {
404 		sdio->reg_local = NULL;
405 		rtw_mfree(buf, 0x100);
406 	}
407 
408 	buf = sdio->reg_cia;
409 	if (buf) {
410 		sdio->reg_cia = NULL;
411 		rtw_mfree(buf, 0x200);
412 	}
413 }
414 
rtw_sdio_dbg_reg_alloc(struct dvobj_priv * d)415 void rtw_sdio_dbg_reg_alloc(struct dvobj_priv *d)
416 {
417 	struct sdio_data *sdio;
418 	u8 *buf;
419 
420 
421 	sdio = &d->intf_data;
422 
423 	buf = _rtw_zmalloc(0x800);
424 	if (buf)
425 		sdio->reg_mac = buf;
426 
427 	buf = _rtw_zmalloc(0x800);
428 	if (buf)
429 		sdio->reg_mac_ext = buf;
430 
431 	buf = _rtw_zmalloc(0x100);
432 	if (buf)
433 		sdio->reg_local = buf;
434 
435 	buf = _rtw_zmalloc(0x200);
436 	if (buf)
437 		sdio->reg_cia = buf;
438 }
439 #endif /* DBG_SDIO >= 2 */
440 
sdio_dbg_init(struct dvobj_priv * d)441 static void sdio_dbg_init(struct dvobj_priv *d)
442 {
443 	struct sdio_data *sdio;
444 
445 
446 	sdio = &d->intf_data;
447 
448 	sdio->cmd52_err_cnt = 0;
449 	sdio->cmd53_err_cnt = 0;
450 
451 #if (DBG_SDIO >= 1)
452 	sdio->reg_dump_mark = 0;
453 #endif /* DBG_SDIO >= 1 */
454 
455 #if (DBG_SDIO >= 3)
456 	sdio->dbg_enable = 0;
457 	sdio->err_stop = 0;
458 	sdio->err_test = 0;
459 	sdio->err_test_triggered = 0;
460 #endif /* DBG_SDIO >= 3 */
461 }
462 
sdio_dbg_deinit(struct dvobj_priv * d)463 static void sdio_dbg_deinit(struct dvobj_priv *d)
464 {
465 #if (DBG_SDIO >= 2)
466 	rtw_sdio_dbg_reg_free(d);
467 #endif /* DBG_SDIO >= 2 */
468 }
469 #endif /* DBG_SDIO */
470 
sdio_init(struct dvobj_priv * dvobj)471 u32 sdio_init(struct dvobj_priv *dvobj)
472 {
473 	PSDIO_DATA psdio_data;
474 	struct sdio_func *func;
475 	int err;
476 
477 
478 	psdio_data = &dvobj->intf_data;
479 	func = psdio_data->func;
480 
481 	/* 3 1. init SDIO bus */
482 	sdio_claim_host(func);
483 
484 	err = sdio_enable_func(func);
485 	if (err) {
486 		dvobj->drv_dbg.dbg_sdio_init_error_cnt++;
487 		RTW_PRINT("%s: sdio_enable_func FAIL(%d)!\n", __func__, err);
488 		goto release;
489 	}
490 
491 	err = sdio_set_block_size(func, 512);
492 	if (err) {
493 		dvobj->drv_dbg.dbg_sdio_init_error_cnt++;
494 		RTW_PRINT("%s: sdio_set_block_size FAIL(%d)!\n", __func__, err);
495 		goto release;
496 	}
497 	psdio_data->block_transfer_len = 512;
498 	psdio_data->tx_block_mode = 1;
499 	psdio_data->rx_block_mode = 1;
500 
501 	psdio_data->card = func->card;
502 	psdio_data->timing = func->card->host->ios.timing;
503 	psdio_data->clock = func->card->host->ios.clock;
504 	psdio_data->func_number = func->card->sdio_funcs;
505 
506 	psdio_data->sd3_bus_mode = _FALSE;
507 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
508 	if (psdio_data->timing <= MMC_TIMING_UHS_DDR50
509 		#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
510 		&& psdio_data->timing >= MMC_TIMING_UHS_SDR12
511 		#else
512 		&& psdio_data->timing >= MMC_TIMING_UHS_SDR50
513 		#endif
514 	)
515 		psdio_data->sd3_bus_mode = _TRUE;
516 #endif
517 
518 #ifdef DBG_SDIO
519 	sdio_dbg_init(dvobj);
520 #endif /* DBG_SDIO */
521 
522 	SDIO_CARD_INFO_DUMP(dvobj);
523 
524 
525 release:
526 	sdio_release_host(func);
527 
528 	if (err)
529 		return _FAIL;
530 	return _SUCCESS;
531 }
532 
sdio_deinit(struct dvobj_priv * dvobj)533 void sdio_deinit(struct dvobj_priv *dvobj)
534 {
535 	struct sdio_func *func;
536 	int err;
537 
538 
539 
540 	func = dvobj->intf_data.func;
541 
542 	if (func) {
543 		sdio_claim_host(func);
544 		err = sdio_disable_func(func);
545 		if (err) {
546 			dvobj->drv_dbg.dbg_sdio_deinit_error_cnt++;
547 			RTW_ERR("%s: sdio_disable_func(%d)\n", __func__, err);
548 		}
549 
550 		sdio_release_host(func);
551 	}
552 
553 #ifdef DBG_SDIO
554 	sdio_dbg_deinit(dvobj);
555 #endif /* DBG_SDIO */
556 }
557 
sdio_get_num_of_func(struct dvobj_priv * dvobj)558 u8 sdio_get_num_of_func(struct dvobj_priv *dvobj)
559 {
560 	return dvobj->intf_data.func_number;
561 }
562 
rtw_decide_chip_type_by_device_id(struct dvobj_priv * dvobj,const struct sdio_device_id * pdid)563 static void rtw_decide_chip_type_by_device_id(struct dvobj_priv *dvobj, const struct sdio_device_id  *pdid)
564 {
565 	dvobj->chip_type = pdid->driver_data;
566 
567 #if defined(CONFIG_RTL8188E)
568 	if (dvobj->chip_type == RTL8188E) {
569 		dvobj->HardwareType = HARDWARE_TYPE_RTL8188ES;
570 		RTW_INFO("CHIP TYPE: RTL8188E\n");
571 	}
572 #endif
573 
574 #if defined(CONFIG_RTL8723B)
575 	if (dvobj->chip_type == RTL8723B) {
576 		dvobj->HardwareType = HARDWARE_TYPE_RTL8723BS;
577 		RTW_INFO("CHIP TYPE: RTL8723B\n");
578 	}
579 #endif
580 
581 #if defined(CONFIG_RTL8821A)
582 	if (dvobj->chip_type == RTL8821) {
583 		dvobj->HardwareType = HARDWARE_TYPE_RTL8821S;
584 		RTW_INFO("CHIP TYPE: RTL8821A\n");
585 	}
586 #endif
587 
588 #if defined(CONFIG_RTL8192E)
589 	if (dvobj->chip_type == RTL8192E) {
590 		dvobj->HardwareType = HARDWARE_TYPE_RTL8192ES;
591 		RTW_INFO("CHIP TYPE: RTL8192E\n");
592 	}
593 #endif
594 
595 #if defined(CONFIG_RTL8703B)
596 	if (dvobj->chip_type == RTL8703B) {
597 		dvobj->HardwareType = HARDWARE_TYPE_RTL8703BS;
598 		RTW_INFO("CHIP TYPE: RTL8703B\n");
599 	}
600 #endif
601 
602 #if defined(CONFIG_RTL8723D)
603 	if (dvobj->chip_type == RTL8723D) {
604 		dvobj->HardwareType = HARDWARE_TYPE_RTL8723DS;
605 		RTW_INFO("CHIP TYPE: RTL8723D\n");
606 	}
607 #endif
608 
609 #if defined(CONFIG_RTL8188F)
610 	if (dvobj->chip_type == RTL8188F) {
611 		dvobj->HardwareType = HARDWARE_TYPE_RTL8188FS;
612 		RTW_INFO("CHIP TYPE: RTL8188F\n");
613 	}
614 #endif
615 
616 #if defined(CONFIG_RTL8188GTV)
617 	if (dvobj->chip_type == RTL8188GTV) {
618 		dvobj->HardwareType = HARDWARE_TYPE_RTL8188GTVS;
619 		RTW_INFO("CHIP TYPE: RTL8188GTV\n");
620 	}
621 #endif
622 
623 #if defined(CONFIG_RTL8822B)
624 	if (dvobj->chip_type == RTL8822B) {
625 		dvobj->HardwareType = HARDWARE_TYPE_RTL8822BS;
626 		RTW_INFO("CHIP TYPE: RTL8822B\n");
627 	}
628 #endif
629 
630 #if defined(CONFIG_RTL8821C)
631 	if (dvobj->chip_type == RTL8821C) {
632 		dvobj->HardwareType = HARDWARE_TYPE_RTL8821CS;
633 		RTW_INFO("CHIP TYPE: RTL8821C\n");
634 	}
635 #endif
636 
637 #if defined(CONFIG_RTL8192F)
638 	if (dvobj->chip_type == RTL8192F) {
639 		dvobj->HardwareType = HARDWARE_TYPE_RTL8192FS;
640 		RTW_INFO("CHIP TYPE: RTL8192F\n");
641 	}
642 #endif
643 
644 #if defined(CONFIG_RTL8822C)
645 	if (dvobj->chip_type == RTL8822C) {
646 		dvobj->HardwareType = HARDWARE_TYPE_RTL8822CS;
647 		RTW_INFO("CHIP TYPE: RTL8822C\n");
648 	}
649 #endif
650 
651 #if defined(CONFIG_RTL8723F)
652 	if (dvobj->chip_type == RTL8723F) {
653 		dvobj->HardwareType = HARDWARE_TYPE_RTL8723FS;
654 		RTW_INFO("CHIP TYPE: RTL8723F\n");
655 	}
656 #endif
657 }
658 
sdio_dvobj_init(struct sdio_func * func,const struct sdio_device_id * pdid)659 static struct dvobj_priv *sdio_dvobj_init(struct sdio_func *func, const struct sdio_device_id  *pdid)
660 {
661 	int status = _FAIL;
662 	struct dvobj_priv *dvobj = NULL;
663 	PSDIO_DATA psdio;
664 
665 	dvobj = devobj_init();
666 	if (dvobj == NULL)
667 		goto exit;
668 	dvobj->intf_ops = &sdio_ops;
669 
670 	sdio_set_drvdata(func, dvobj);
671 
672 	psdio = &dvobj->intf_data;
673 	psdio->func = func;
674 
675 	if (sdio_init(dvobj) != _SUCCESS) {
676 		goto free_dvobj;
677 	}
678 
679 	dvobj->interface_type = RTW_SDIO;
680 	rtw_decide_chip_type_by_device_id(dvobj, pdid);
681 
682 	rtw_reset_continual_io_error(dvobj);
683 	status = _SUCCESS;
684 
685 free_dvobj:
686 	if (status != _SUCCESS && dvobj) {
687 		sdio_set_drvdata(func, NULL);
688 
689 		devobj_deinit(dvobj);
690 
691 		dvobj = NULL;
692 	}
693 exit:
694 	return dvobj;
695 }
696 
sdio_dvobj_deinit(struct sdio_func * func)697 static void sdio_dvobj_deinit(struct sdio_func *func)
698 {
699 	struct dvobj_priv *dvobj = sdio_get_drvdata(func);
700 
701 	sdio_set_drvdata(func, NULL);
702 	if (dvobj) {
703 		sdio_deinit(dvobj);
704 		sdio_free_irq(dvobj);
705 		devobj_deinit(dvobj);
706 	}
707 
708 	return;
709 }
710 
rtw_set_hal_ops(PADAPTER padapter)711 u8 rtw_set_hal_ops(PADAPTER padapter)
712 {
713 	/* alloc memory for HAL DATA */
714 	if (rtw_hal_data_init(padapter) == _FAIL)
715 		return _FAIL;
716 
717 #if defined(CONFIG_RTL8188E)
718 	if (rtw_get_chip_type(padapter) == RTL8188E)
719 		rtl8188es_set_hal_ops(padapter);
720 #endif
721 
722 #if defined(CONFIG_RTL8723B)
723 	if (rtw_get_chip_type(padapter) == RTL8723B)
724 		rtl8723bs_set_hal_ops(padapter);
725 #endif
726 
727 #if defined(CONFIG_RTL8821A)
728 	if (rtw_get_chip_type(padapter) == RTL8821)
729 		rtl8821as_set_hal_ops(padapter);
730 #endif
731 
732 #if defined(CONFIG_RTL8192E)
733 	if (rtw_get_chip_type(padapter) == RTL8192E)
734 		rtl8192es_set_hal_ops(padapter);
735 #endif
736 
737 #if defined(CONFIG_RTL8703B)
738 	if (rtw_get_chip_type(padapter) == RTL8703B)
739 		rtl8703bs_set_hal_ops(padapter);
740 #endif
741 
742 #if defined(CONFIG_RTL8723D)
743 	if (rtw_get_chip_type(padapter) == RTL8723D)
744 		rtl8723ds_set_hal_ops(padapter);
745 #endif
746 
747 #if defined(CONFIG_RTL8188F)
748 	if (rtw_get_chip_type(padapter) == RTL8188F)
749 		rtl8188fs_set_hal_ops(padapter);
750 #endif
751 
752 #if defined(CONFIG_RTL8188GTV)
753 	if (rtw_get_chip_type(padapter) == RTL8188GTV)
754 		rtl8188gtvs_set_hal_ops(padapter);
755 #endif
756 
757 #if defined(CONFIG_RTL8822B)
758 	if (rtw_get_chip_type(padapter) == RTL8822B)
759 		rtl8822bs_set_hal_ops(padapter);
760 #endif
761 
762 #if defined(CONFIG_RTL8821C)
763 	if (rtw_get_chip_type(padapter) == RTL8821C) {
764 		if (rtl8821cs_set_hal_ops(padapter) == _FAIL)
765 			return _FAIL;
766 	}
767 #endif
768 
769 #if defined(CONFIG_RTL8192F)
770 	if (rtw_get_chip_type(padapter) == RTL8192F)
771 		rtl8192fs_set_hal_ops(padapter);
772 #endif
773 
774 #if defined(CONFIG_RTL8822C)
775 	if (rtw_get_chip_type(padapter) == RTL8822C)
776 		rtl8822cs_set_hal_ops(padapter);
777 #endif
778 
779 #if defined(CONFIG_RTL8723F)
780 	if (rtw_get_chip_type(padapter) == RTL8723F)
781 		rtl8723fs_set_hal_ops(padapter);
782 #endif
783 
784 	if (rtw_hal_ops_check(padapter) == _FAIL)
785 		return _FAIL;
786 
787 	if (hal_spec_init(padapter) == _FAIL)
788 		return _FAIL;
789 
790 	return _SUCCESS;
791 }
792 
sd_intf_start(PADAPTER padapter)793 static void sd_intf_start(PADAPTER padapter)
794 {
795 	if (padapter == NULL) {
796 		RTW_ERR("%s: padapter is NULL!\n", __func__);
797 		return;
798 	}
799 
800 	/* hal dep */
801 	rtw_hal_enable_interrupt(padapter);
802 }
803 
sd_intf_stop(PADAPTER padapter)804 static void sd_intf_stop(PADAPTER padapter)
805 {
806 	if (padapter == NULL) {
807 		RTW_ERR("%s: padapter is NULL!\n", __func__);
808 		return;
809 	}
810 
811 	/* hal dep */
812 	rtw_hal_disable_interrupt(padapter);
813 }
814 
815 
816 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
817 PADAPTER g_test_adapter = NULL;
818 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
819 
rtw_sdio_primary_adapter_init(struct dvobj_priv * dvobj)820 _adapter *rtw_sdio_primary_adapter_init(struct dvobj_priv *dvobj)
821 {
822 	int status = _FAIL;
823 	PADAPTER padapter = NULL;
824 
825 	padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter));
826 	if (padapter == NULL)
827 		goto exit;
828 
829 	if (loadparam(padapter) != _SUCCESS)
830 		goto free_adapter;
831 
832 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
833 	g_test_adapter = padapter;
834 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
835 	padapter->dvobj = dvobj;
836 
837 	rtw_set_drv_stopped(padapter);/*init*/
838 
839 	dvobj->padapters[dvobj->iface_nums++] = padapter;
840 	padapter->iface_id = IFACE_ID0;
841 
842 	/* set adapter_type/iface type for primary padapter */
843 	padapter->isprimary = _TRUE;
844 	padapter->adapter_type = PRIMARY_ADAPTER;
845 #ifdef CONFIG_MI_WITH_MBSSID_CAM
846 	padapter->hw_port = HW_PORT0;
847 #else
848 	padapter->hw_port = HW_PORT0;
849 #endif
850 
851 	/* 3 3. init driver special setting, interface, OS and hardware relative */
852 
853 	/* 4 3.1 set hardware operation functions */
854 	if (rtw_set_hal_ops(padapter) == _FAIL)
855 		goto free_hal_data;
856 
857 	/* 3 5. initialize Chip version */
858 	padapter->intf_start = &sd_intf_start;
859 	padapter->intf_stop = &sd_intf_stop;
860 
861 	if (rtw_init_io_priv(padapter, sdio_set_intf_ops) == _FAIL) {
862 		goto free_hal_data;
863 	}
864 
865 	rtw_hal_read_chip_version(padapter);
866 
867 	rtw_hal_chip_configure(padapter);
868 
869 #ifdef CONFIG_BT_COEXIST
870 	rtw_btcoex_Initialize(padapter);
871 #endif
872 	rtw_btcoex_wifionly_initialize(padapter);
873 
874 	/* 3 6. read efuse/eeprom data */
875 	if (rtw_hal_read_chip_info(padapter) == _FAIL)
876 		goto free_hal_data;
877 
878 	/* 3 7. init driver common data */
879 	if (rtw_init_drv_sw(padapter) == _FAIL) {
880 		goto free_hal_data;
881 	}
882 
883 	/* 3 8. get WLan MAC address */
884 	/* set mac addr */
885 	rtw_macaddr_cfg(adapter_mac_addr(padapter),  get_hal_mac_addr(padapter));
886 
887 #ifdef CONFIG_MI_WITH_MBSSID_CAM
888 	rtw_mbid_camid_alloc(padapter, adapter_mac_addr(padapter));
889 #endif
890 #ifdef CONFIG_P2P
891 	rtw_init_wifidirect_addrs(padapter, adapter_mac_addr(padapter), adapter_mac_addr(padapter));
892 #endif /* CONFIG_P2P */
893 
894 	rtw_hal_disable_interrupt(padapter);
895 
896 	RTW_INFO("bDriverStopped:%s, bSurpriseRemoved:%s, bup:%d, hw_init_completed:%d\n"
897 		, rtw_is_drv_stopped(padapter) ? "True" : "False"
898 		, rtw_is_surprise_removed(padapter) ? "True" : "False"
899 		, padapter->bup
900 		, rtw_get_hw_init_completed(padapter)
901 	);
902 
903 	status = _SUCCESS;
904 
905 free_hal_data:
906 	if (status != _SUCCESS && padapter->HalData)
907 		rtw_hal_free_data(padapter);
908 
909 free_adapter:
910 	if (status != _SUCCESS && padapter) {
911 		#ifdef RTW_HALMAC
912 		rtw_halmac_deinit_adapter(dvobj);
913 		#endif
914 		rtw_vmfree((u8 *)padapter, sizeof(*padapter));
915 		padapter = NULL;
916 	}
917 exit:
918 	return padapter;
919 }
920 
rtw_sdio_primary_adapter_deinit(_adapter * padapter)921 static void rtw_sdio_primary_adapter_deinit(_adapter *padapter)
922 {
923 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
924 
925 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE))
926 		rtw_disassoc_cmd(padapter, 0, RTW_CMDF_DIRECTLY);
927 
928 #ifdef CONFIG_AP_MODE
929 	if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) {
930 		free_mlme_ap_info(padapter);
931 		#ifdef CONFIG_HOSTAPD_MLME
932 		hostapd_mode_unload(padapter);
933 		#endif
934 	}
935 #endif
936 
937 #ifdef CONFIG_GPIO_WAKEUP
938 #ifdef CONFIG_PLATFORM_ARM_SUN6I
939 	sw_gpio_eint_set_enable(gpio_eint_wlan, 0);
940 	sw_gpio_irq_free(eint_wlan_handle);
941 #else
942 	gpio_hostwakeup_free_irq(padapter);
943 #endif
944 #endif
945 
946 	/*rtw_cancel_all_timer(if1);*/
947 
948 #ifdef CONFIG_WOWLAN
949 	adapter_to_pwrctl(padapter)->wowlan_mode = _FALSE;
950 	RTW_PRINT("%s wowlan_mode:%d\n", __func__, adapter_to_pwrctl(padapter)->wowlan_mode);
951 #endif /* CONFIG_WOWLAN */
952 
953 	rtw_dev_unload(padapter);
954 	RTW_INFO("+r871xu_dev_remove, hw_init_completed=%d\n", rtw_get_hw_init_completed(padapter));
955 
956 	rtw_free_drv_sw(padapter);
957 
958 	/* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */
959 	rtw_os_ndev_free(padapter);
960 
961 #ifdef RTW_HALMAC
962 	rtw_halmac_deinit_adapter(adapter_to_dvobj(padapter));
963 #endif /* RTW_HALMAC */
964 
965 	rtw_vmfree((u8 *)padapter, sizeof(_adapter));
966 
967 #ifdef CONFIG_PLATFORM_RTD2880B
968 	RTW_INFO("wlan link down\n");
969 	rtd2885_wlan_netlink_sendMsg("linkdown", "8712");
970 #endif
971 
972 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
973 	g_test_adapter = NULL;
974 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
975 }
976 
977 /*
978  * drv_init() - a device potentially for us
979  *
980  * notes: drv_init() is called when the bus driver has located a card for us to support.
981  *        We accept the new device by returning 0.
982  */
rtw_drv_init(struct sdio_func * func,const struct sdio_device_id * id)983 static int rtw_drv_init(
984 	struct sdio_func *func,
985 	const struct sdio_device_id *id)
986 {
987 	int status = _FAIL;
988 #ifdef CONFIG_CONCURRENT_MODE
989 	int i;
990 #endif
991 	PADAPTER padapter = NULL;
992 	struct dvobj_priv *dvobj;
993 
994 #ifdef CONFIG_PLATFORM_INTEL_BYT
995 
996 #ifdef CONFIG_ACPI
997 	acpi_handle handle;
998 	struct acpi_device *adev;
999 #endif
1000 
1001 #if defined(CONFIG_ACPI) && defined(CONFIG_GPIO_WAKEUP)
1002 	handle = ACPI_HANDLE(&func->dev);
1003 
1004 	if (handle) {
1005 		/* Dont try to do acpi pm for the wifi module */
1006 		if (!handle || acpi_bus_get_device(handle, &adev))
1007 			RTW_INFO("Could not get acpi pointer!\n");
1008 		else {
1009 			adev->flags.power_manageable = 0;
1010 			RTW_INFO("Disabling ACPI power management support!\n");
1011 		}
1012 		oob_gpio = acpi_get_gpio_by_index(&func->dev, 0, NULL);
1013 		RTW_INFO("rtw_drv_init: ACPI_HANDLE found oob_gpio %d!\n", oob_gpio);
1014 		wifi_configure_gpio();
1015 	} else
1016 		RTW_INFO("rtw_drv_init: ACPI_HANDLE NOT found!\n");
1017 #endif
1018 
1019 #if defined(CONFIG_ACPI)
1020 	if (&func->dev && ACPI_HANDLE(&func->dev)) {
1021 		wlan_en_gpio = acpi_get_gpio_by_index(&func->dev, 1, NULL);
1022 		RTW_INFO("rtw_drv_init: ACPI_HANDLE found wlan_en %d!\n", wlan_en_gpio);
1023 	} else
1024 		RTW_INFO("rtw_drv_init: ACPI_HANDLE NOT found!\n");
1025 #endif
1026 #endif /* CONFIG_PLATFORM_INTEL_BYT */
1027 
1028 
1029 
1030 	dvobj = sdio_dvobj_init(func, id);
1031 	if (dvobj == NULL) {
1032 		goto exit;
1033 	}
1034 
1035 	padapter = rtw_sdio_primary_adapter_init(dvobj);
1036 	if (padapter == NULL) {
1037 		RTW_INFO("rtw_init_primary_adapter Failed!\n");
1038 		goto free_dvobj;
1039 	}
1040 
1041 #ifdef CONFIG_CONCURRENT_MODE
1042 	if (padapter->registrypriv.virtual_iface_num > (CONFIG_IFACE_NUMBER - 1))
1043 		padapter->registrypriv.virtual_iface_num = (CONFIG_IFACE_NUMBER - 1);
1044 
1045 	for (i = 0; i < padapter->registrypriv.virtual_iface_num; i++) {
1046 		if (rtw_drv_add_vir_if(padapter, sdio_set_intf_ops) == NULL) {
1047 			RTW_INFO("rtw_drv_add_iface failed! (%d)\n", i);
1048 			goto free_if_vir;
1049 		}
1050 	}
1051 #endif
1052 
1053 	/* dev_alloc_name && register_netdev */
1054 	if (rtw_os_ndevs_init(dvobj) != _SUCCESS)
1055 		goto free_if_vir;
1056 
1057 #ifdef CONFIG_HOSTAPD_MLME
1058 	hostapd_mode_init(padapter);
1059 #endif
1060 
1061 #ifdef CONFIG_PLATFORM_RTD2880B
1062 	RTW_INFO("wlan link up\n");
1063 	rtd2885_wlan_netlink_sendMsg("linkup", "8712");
1064 #endif
1065 
1066 	if (sdio_alloc_irq(dvobj) != _SUCCESS)
1067 		goto os_ndevs_deinit;
1068 
1069 #ifdef CONFIG_GPIO_WAKEUP
1070 #ifdef CONFIG_PLATFORM_ARM_SUN6I
1071 	eint_wlan_handle = sw_gpio_irq_request(gpio_eint_wlan, TRIG_EDGE_NEGATIVE, (peint_handle)gpio_hostwakeup_irq_thread, NULL);
1072 	if (!eint_wlan_handle) {
1073 		RTW_INFO("%s: request irq failed\n", __func__);
1074 		return -1;
1075 	}
1076 #else
1077 	gpio_hostwakeup_alloc_irq(padapter);
1078 #endif
1079 #endif
1080 
1081 #ifdef CONFIG_GLOBAL_UI_PID
1082 	if (ui_pid[1] != 0) {
1083 		RTW_INFO("ui_pid[1]:%d\n", ui_pid[1]);
1084 		rtw_signal_process(ui_pid[1], SIGUSR2);
1085 	}
1086 #endif
1087 
1088 
1089 	status = _SUCCESS;
1090 
1091 os_ndevs_deinit:
1092 	if (status != _SUCCESS)
1093 		rtw_os_ndevs_deinit(dvobj);
1094 free_if_vir:
1095 	if (status != _SUCCESS) {
1096 		#ifdef CONFIG_CONCURRENT_MODE
1097 		rtw_drv_stop_vir_ifaces(dvobj);
1098 		rtw_drv_free_vir_ifaces(dvobj);
1099 		#endif
1100 	}
1101 
1102 	if (status != _SUCCESS && padapter)
1103 		rtw_sdio_primary_adapter_deinit(padapter);
1104 
1105 free_dvobj:
1106 	if (status != _SUCCESS)
1107 		sdio_dvobj_deinit(func);
1108 exit:
1109 	return status == _SUCCESS ? 0 : -ENODEV;
1110 }
1111 
rtw_dev_remove(struct sdio_func * func)1112 static void rtw_dev_remove(struct sdio_func *func)
1113 {
1114 	struct dvobj_priv *dvobj = sdio_get_drvdata(func);
1115 	struct pwrctrl_priv *pwrctl = dvobj_to_pwrctl(dvobj);
1116 	PADAPTER padapter = dvobj_get_primary_adapter(dvobj);
1117 
1118 
1119 
1120 	dvobj->processing_dev_remove = _TRUE;
1121 
1122 	/* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */
1123 	rtw_os_ndevs_unregister(dvobj);
1124 
1125 	if (!rtw_is_surprise_removed(padapter)) {
1126 		int err;
1127 
1128 		/* test surprise remove */
1129 		sdio_claim_host(func);
1130 		sdio_readb(func, 0, &err);
1131 		sdio_release_host(func);
1132 		if (err == -ENOMEDIUM) {
1133 			rtw_set_surprise_removed(padapter);
1134 			RTW_INFO("%s: device had been removed!\n", __func__);
1135 		}
1136 	}
1137 
1138 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
1139 	rtw_unregister_early_suspend(pwrctl);
1140 #endif
1141 
1142 	if (GET_HAL_DATA(padapter)->bFWReady == _TRUE) {
1143 		rtw_ps_deny(padapter, PS_DENY_DRV_REMOVE);
1144 		rtw_pm_set_ips(padapter, IPS_NONE);
1145 		rtw_pm_set_lps(padapter, PS_MODE_ACTIVE);
1146 		LeaveAllPowerSaveMode(padapter);
1147 	}
1148 	rtw_set_drv_stopped(padapter);	/*for stop thread*/
1149 	rtw_stop_cmd_thread(padapter);
1150 #ifdef CONFIG_CONCURRENT_MODE
1151 	rtw_drv_stop_vir_ifaces(dvobj);
1152 #endif
1153 
1154 #ifdef CONFIG_BT_COEXIST
1155 #ifdef CONFIG_BT_COEXIST_SOCKET_TRX
1156 	if (GET_HAL_DATA(padapter)->EEPROMBluetoothCoexist)
1157 		rtw_btcoex_close_socket(padapter);
1158 #endif
1159 	rtw_btcoex_HaltNotify(padapter);
1160 #endif
1161 
1162 	rtw_sdio_primary_adapter_deinit(padapter);
1163 
1164 #ifdef CONFIG_CONCURRENT_MODE
1165 	rtw_drv_free_vir_ifaces(dvobj);
1166 #endif
1167 
1168 	sdio_dvobj_deinit(func);
1169 
1170 
1171 }
1172 
1173 #ifdef CONFIG_SDIO_HOOK_DEV_SHUTDOWN
rtw_dev_shutdown(struct device * dev)1174 static void rtw_dev_shutdown(struct device *dev)
1175 {
1176 	struct sdio_func *func = dev_to_sdio_func(dev);
1177 
1178 	if (func == NULL)
1179 		return;
1180 
1181 	RTW_INFO("==> %s !\n", __func__);
1182 
1183 	rtw_dev_remove(func);
1184 
1185 	RTW_INFO("<== %s !\n", __func__);
1186 }
1187 #endif
1188 
1189 extern int pm_netdev_open(struct net_device *pnetdev, u8 bnormal);
1190 extern int pm_netdev_close(struct net_device *pnetdev, u8 bnormal);
1191 
rtw_sdio_suspend(struct device * dev)1192 static int rtw_sdio_suspend(struct device *dev)
1193 {
1194 	struct sdio_func *func = NULL;
1195 	struct dvobj_priv *psdpriv = NULL;
1196 	struct pwrctrl_priv *pwrpriv = NULL;
1197 	_adapter *padapter = NULL;
1198 	struct debug_priv *pdbgpriv = NULL;
1199 	int ret = 0;
1200 #ifdef CONFIG_RTW_SDIO_PM_KEEP_POWER
1201 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
1202 	mmc_pm_flag_t pm_flag = 0;
1203 #endif
1204 #endif
1205 
1206 	if (dev == NULL)
1207 		return ret;
1208 
1209 	func = dev_to_sdio_func(dev);
1210 	if(func == NULL)
1211 		return ret;
1212 
1213 	psdpriv = sdio_get_drvdata(func);
1214 	if (psdpriv == NULL)
1215 		goto exit;
1216 
1217 	pwrpriv = dvobj_to_pwrctl(psdpriv);
1218 	padapter = dvobj_get_primary_adapter(psdpriv);
1219 	pdbgpriv = &psdpriv->drv_dbg;
1220 	if (rtw_is_drv_stopped(padapter)) {
1221 		RTW_INFO("%s bDriverStopped == _TRUE\n", __func__);
1222 		goto exit;
1223 	}
1224 
1225 	if (pwrpriv->bInSuspend == _TRUE) {
1226 		RTW_INFO("%s bInSuspend = %d\n", __func__, pwrpriv->bInSuspend);
1227 		pdbgpriv->dbg_suspend_error_cnt++;
1228 		goto exit;
1229 	}
1230 
1231 	ret = rtw_suspend_common(padapter);
1232 
1233 exit:
1234 #ifdef CONFIG_RTW_SDIO_PM_KEEP_POWER
1235 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
1236 	/* Android 4.0 don't support WIFI close power */
1237 	/* or power down or clock will close after wifi resume, */
1238 	/* this is sprd's bug in Android 4.0, but sprd don't */
1239 	/* want to fix it. */
1240 	/* we have test power under 8723as, power consumption is ok */
1241 	pm_flag = sdio_get_host_pm_caps(func);
1242 	RTW_INFO("cmd: %s: suspend: PM flag = 0x%x\n", sdio_func_id(func), pm_flag);
1243 	if (!(pm_flag & MMC_PM_KEEP_POWER)) {
1244 		RTW_INFO("%s: cannot remain alive while host is suspended\n", sdio_func_id(func));
1245 		if (pdbgpriv)
1246 			pdbgpriv->dbg_suspend_error_cnt++;
1247 		return -ENOSYS;
1248 	} else {
1249 		RTW_INFO("cmd: suspend with MMC_PM_KEEP_POWER\n");
1250 		sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1251 	}
1252 #endif
1253 #endif
1254 
1255 	return ret;
1256 }
rtw_resume_process(_adapter * padapter)1257 int rtw_resume_process(_adapter *padapter)
1258 {
1259 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
1260 	struct dvobj_priv *psdpriv = padapter->dvobj;
1261 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1262 
1263 	if (pwrpriv->bInSuspend == _FALSE) {
1264 		pdbgpriv->dbg_resume_error_cnt++;
1265 		RTW_INFO("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend);
1266 		return -1;
1267 	}
1268 
1269 	return rtw_resume_common(padapter);
1270 }
1271 
rtw_sdio_resume(struct device * dev)1272 static int rtw_sdio_resume(struct device *dev)
1273 {
1274 	struct sdio_func *func = dev_to_sdio_func(dev);
1275 	struct dvobj_priv *psdpriv = sdio_get_drvdata(func);
1276 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv);
1277 	_adapter *padapter = dvobj_get_primary_adapter(psdpriv);
1278 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
1279 	int ret = 0;
1280 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1281 
1282 	RTW_INFO("==> %s (%s:%d)\n", __FUNCTION__, current->comm, current->pid);
1283 
1284 	pdbgpriv->dbg_resume_cnt++;
1285 
1286 #ifdef CONFIG_PLATFORM_INTEL_BYT
1287 	if (0)
1288 #else
1289 	if (pwrpriv->wowlan_mode || pwrpriv->wowlan_ap_mode)
1290 #endif
1291 	{
1292 		rtw_resume_lock_suspend();
1293 		ret = rtw_resume_process(padapter);
1294 		rtw_resume_unlock_suspend();
1295 	} else {
1296 #ifdef CONFIG_RESUME_IN_WORKQUEUE
1297 		rtw_resume_in_workqueue(pwrpriv);
1298 #else
1299 		if (rtw_is_earlysuspend_registered(pwrpriv)) {
1300 			/* jeff: bypass resume here, do in late_resume */
1301 			rtw_set_do_late_resume(pwrpriv, _TRUE);
1302 		} else {
1303 			rtw_resume_lock_suspend();
1304 			ret = rtw_resume_process(padapter);
1305 			rtw_resume_unlock_suspend();
1306 		}
1307 #endif
1308 	}
1309 	pmlmeext->last_scan_time = rtw_get_current_time();
1310 	RTW_INFO("<========  %s return %d\n", __FUNCTION__, ret);
1311 	return ret;
1312 
1313 }
1314 
rtw_drv_entry(void)1315 static int __init rtw_drv_entry(void)
1316 {
1317 	int ret = 0;
1318 
1319 	RTW_PRINT("module init start\n");
1320 	dump_drv_version(RTW_DBGDUMP);
1321 #ifdef BTCOEXVERSION
1322 	RTW_PRINT(DRV_NAME" BT-Coex version = %s\n", BTCOEXVERSION);
1323 #endif /* BTCOEXVERSION */
1324 
1325 #ifndef CONFIG_PLATFORM_INTEL_BYT
1326 	rtw_android_wifictrl_func_add();
1327 #endif /* !CONFIG_PLATFORM_INTEL_BYT */
1328 
1329 	ret = platform_wifi_power_on();
1330 	if (ret) {
1331 		RTW_INFO("%s: power on failed!!(%d)\n", __FUNCTION__, ret);
1332 		ret = -1;
1333 		goto exit;
1334 	}
1335 
1336 	sdio_drvpriv.drv_registered = _TRUE;
1337 	rtw_suspend_lock_init();
1338 	rtw_drv_proc_init();
1339 	rtw_nlrtw_init();
1340 #ifdef CONFIG_PLATFORM_CMAP_INTFS
1341 	cmap_intfs_init();
1342 #endif
1343 	rtw_ndev_notifier_register();
1344 	rtw_inetaddr_notifier_register();
1345 
1346 	ret = sdio_register_driver(&sdio_drvpriv.r871xs_drv);
1347 	if (ret != 0) {
1348 		sdio_drvpriv.drv_registered = _FALSE;
1349 		rtw_suspend_lock_uninit();
1350 		rtw_drv_proc_deinit();
1351 		rtw_nlrtw_deinit();
1352 #ifdef CONFIG_PLATFORM_CMAP_INTFS
1353 		cmap_intfs_deinit();
1354 #endif
1355 		rtw_ndev_notifier_unregister();
1356 		rtw_inetaddr_notifier_unregister();
1357 		RTW_INFO("%s: register driver failed!!(%d)\n", __FUNCTION__, ret);
1358 		goto poweroff;
1359 	}
1360 
1361 	goto exit;
1362 
1363 poweroff:
1364 	platform_wifi_power_off();
1365 
1366 exit:
1367 	RTW_PRINT("module init ret=%d\n", ret);
1368 	return ret;
1369 }
1370 
rtw_drv_halt(void)1371 static void __exit rtw_drv_halt(void)
1372 {
1373 	RTW_PRINT("module exit start\n");
1374 
1375 	sdio_drvpriv.drv_registered = _FALSE;
1376 
1377 	sdio_unregister_driver(&sdio_drvpriv.r871xs_drv);
1378 
1379 	rtw_android_wifictrl_func_del();
1380 
1381 	platform_wifi_power_off();
1382 
1383 	rtw_suspend_lock_uninit();
1384 	rtw_drv_proc_deinit();
1385 	rtw_nlrtw_deinit();
1386 #ifdef CONFIG_PLATFORM_CMAP_INTFS
1387 	cmap_intfs_deinit();
1388 #endif
1389 	rtw_ndev_notifier_unregister();
1390 	rtw_inetaddr_notifier_unregister();
1391 
1392 	RTW_PRINT("module exit success\n");
1393 
1394 	rtw_mstat_dump(RTW_DBGDUMP);
1395 }
1396 
1397 #ifdef CONFIG_PLATFORM_INTEL_BYT
rtw_sdio_set_power(int on)1398 int rtw_sdio_set_power(int on)
1399 {
1400 
1401 	if (wlan_en_gpio >= 0) {
1402 		if (on)
1403 			gpio_set_value(wlan_en_gpio, 1);
1404 		else
1405 			gpio_set_value(wlan_en_gpio, 0);
1406 	}
1407 
1408 	return 0;
1409 }
1410 #endif /* CONFIG_PLATFORM_INTEL_BYT */
1411 
1412 module_init(rtw_drv_entry);
1413 module_exit(rtw_drv_halt);
1414 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
1415 MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
1416 #endif
1417