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