xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8852bs/os_dep/linux/sdio_intf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2021 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 <platform_ops.h>
19 #include <rtw_trx_sdio.h>
20 
21 #ifndef CONFIG_SDIO_HCI
22 #error "CONFIG_SDIO_HCI shall be on!\n"
23 #endif
24 
25 #ifndef dev_to_sdio_func
26 #define dev_to_sdio_func(d)     container_of(d, struct sdio_func, dev)
27 #endif
28 
29 static const struct sdio_device_id sdio_ids[] = {
30 
31 #ifdef CONFIG_RTL8852A
32 	{SDIO_DEVICE(0x024c, 0x8852), .class = SDIO_CLASS_WLAN, .driver_data = RTL8852A},
33 	{SDIO_DEVICE(0x024c, 0xa852), .class = SDIO_CLASS_WLAN, .driver_data = RTL8852A},
34 #endif
35 
36 #ifdef CONFIG_RTL8852B
37 	{SDIO_DEVICE(0x024c, 0xb852), .class = SDIO_CLASS_WLAN, .driver_data = RTL8852B},
38 #endif
39 
40 #if defined(RTW_ENABLE_WIFI_CONTROL_FUNC) /* temporarily add this to accept all sdio wlan id */
41 	{ SDIO_DEVICE_CLASS(SDIO_CLASS_WLAN) },
42 #endif
43 	{ /* end: all zeroes */				},
44 };
45 
46 MODULE_DEVICE_TABLE(sdio, sdio_ids);
47 
48 static int rtw_dev_probe(struct sdio_func *func, const struct sdio_device_id *id);
49 static void rtw_dev_remove(struct sdio_func *func);
50 #ifdef CONFIG_SDIO_HOOK_DEV_SHUTDOWN
51 static void rtw_dev_shutdown(struct device *dev);
52 #endif
53 static int rtw_sdio_resume(struct device *dev);
54 static int rtw_sdio_suspend(struct device *dev);
55 
56 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
57 static const struct dev_pm_ops rtw_sdio_pm_ops = {
58 	.suspend = rtw_sdio_suspend,
59 	.resume	= rtw_sdio_resume,
60 };
61 #endif
62 
63 struct sdio_drv_priv {
64 	struct sdio_driver rtw_sdio_drv;
65 	int drv_registered;
66 };
67 
68 static struct sdio_drv_priv sdio_drvpriv = {
69 	.rtw_sdio_drv.probe = rtw_dev_probe,
70 	.rtw_sdio_drv.remove = rtw_dev_remove,
71 	.rtw_sdio_drv.name = (char *)DRV_NAME,
72 	.rtw_sdio_drv.id_table = sdio_ids,
73 	.rtw_sdio_drv.drv = {
74 #ifdef CONFIG_SDIO_HOOK_DEV_SHUTDOWN
75 		.shutdown = rtw_dev_shutdown,
76 #endif
77 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
78 		.pm = &rtw_sdio_pm_ops,
79 #endif
80 	}
81 };
82 
83 
sd_sync_int_hdl(struct sdio_func * func)84 static void sd_sync_int_hdl(struct sdio_func *func)
85 {
86 	struct dvobj_priv *dvobj;
87 
88 	dvobj = sdio_get_drvdata(func);
89 
90 	if (RTW_CANNOT_RUN(dvobj))
91 		return;
92 
93 	rtw_sdio_set_irq_thd(dvobj, current);
94 	/*sd_int_hdl(dvobj_get_primary_adapter(psdpriv));*/
95 	rtw_phl_interrupt_handler(dvobj->phl);
96 	rtw_sdio_set_irq_thd(dvobj, NULL);
97 }
98 
rtw_sdio_alloc_irq(struct dvobj_priv * dvobj)99 int rtw_sdio_alloc_irq(struct dvobj_priv *dvobj)
100 {
101 	PSDIO_DATA psdio_data;
102 	struct sdio_func *func;
103 	int err;
104 
105 	psdio_data = dvobj_to_sdio(dvobj);
106 	func = psdio_data->func;
107 
108 	sdio_claim_host(func);
109 
110 	err = sdio_claim_irq(func, &sd_sync_int_hdl);
111 	if (err) {
112 		dvobj->drv_dbg.dbg_sdio_alloc_irq_error_cnt++;
113 		RTW_PRINT("%s: sdio_claim_irq FAIL(%d)!\n", __func__, err);
114 	} else {
115 		dvobj->drv_dbg.dbg_sdio_alloc_irq_cnt++;
116 		psdio_data->irq_alloc = 1;
117 	}
118 
119 	sdio_release_host(func);
120 
121 	return err ? _FAIL : _SUCCESS;
122 }
123 
rtw_sdio_free_irq(struct dvobj_priv * dvobj)124 void rtw_sdio_free_irq(struct dvobj_priv *dvobj)
125 {
126 	PSDIO_DATA psdio_data;
127 	struct sdio_func *func;
128 	int err;
129 
130 	psdio_data = dvobj_to_sdio(dvobj);
131 	if (psdio_data && psdio_data->irq_alloc) {
132 		func = psdio_data->func;
133 
134 		if (func) {
135 			sdio_claim_host(func);
136 			err = sdio_release_irq(func);
137 			if (err) {
138 				dvobj->drv_dbg.dbg_sdio_free_irq_error_cnt++;
139 				RTW_ERR("%s: sdio_release_irq FAIL(%d)!\n", __func__, err);
140 			} else
141 				dvobj->drv_dbg.dbg_sdio_free_irq_cnt++;
142 			sdio_release_host(func);
143 		}
144 		psdio_data->irq_alloc = 0;
145 	}
146 }
147 
148 #ifdef CONFIG_GPIO_WAKEUP
149 extern unsigned int oob_irq;
150 extern unsigned int oob_gpio;
gpio_hostwakeup_irq_thread(int irq,void * data)151 static irqreturn_t gpio_hostwakeup_irq_thread(int irq, void *data)
152 {
153 	_adapter *padapter = (_adapter *)data;
154 	RTW_PRINT("gpio_hostwakeup_irq_thread\n");
155 	/* Disable interrupt before calling handler */
156 	/* disable_irq_nosync(oob_irq); */
157 #ifdef CONFIG_PLATFORM_ARM_SUN6I
158 	return 0;
159 #else
160 	return IRQ_HANDLED;
161 #endif
162 }
163 
gpio_hostwakeup_alloc_irq(_adapter * padapter)164 static u8 gpio_hostwakeup_alloc_irq(_adapter *padapter)
165 {
166 	int err;
167 	u32 status = 0;
168 
169 	if (oob_irq == 0) {
170 		RTW_INFO("oob_irq ZERO!\n");
171 		return _FAIL;
172 	}
173 
174 	RTW_INFO("%s : oob_irq = %d\n", __func__, oob_irq);
175 
176 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32))
177 	status = IRQF_NO_SUSPEND;
178 #endif
179 
180 	if (HIGH_ACTIVE_DEV2HST)
181 		status |= IRQF_TRIGGER_RISING;
182 	else
183 		status |= IRQF_TRIGGER_FALLING;
184 
185 	err = request_threaded_irq(oob_irq, gpio_hostwakeup_irq_thread, NULL,
186 		status, "rtw_wifi_gpio_wakeup", padapter);
187 
188 	if (err < 0) {
189 		RTW_INFO("Oops: can't allocate gpio irq %d err:%d\n", oob_irq, err);
190 		return _FALSE;
191 	} else
192 		RTW_INFO("allocate gpio irq %d ok\n", oob_irq);
193 
194 #ifndef CONFIG_PLATFORM_ARM_SUN8I
195 	enable_irq_wake(oob_irq);
196 #endif
197 	return _SUCCESS;
198 }
199 
gpio_hostwakeup_free_irq(_adapter * padapter)200 static void gpio_hostwakeup_free_irq(_adapter *padapter)
201 {
202 
203 	if (oob_irq == 0)
204 		return;
205 
206 #ifndef CONFIG_PLATFORM_ARM_SUN8I
207 	disable_irq_wake(oob_irq);
208 #endif
209 	free_irq(oob_irq, padapter);
210 }
211 #endif
212 
213 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
214 /*
215  * mmc_blksz_for_byte_mode() & mmc_card_broken_byte_mode_512() has been moved
216  * to drivers/mmc/core/card.h from include/linux/mmc/card.h since kernel v4.11.
217  */
mmc_blksz_for_byte_mode(const struct mmc_card * c)218 static inline int mmc_blksz_for_byte_mode(const struct mmc_card *c)
219 {
220 	return c->quirks & MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
221 }
222 
mmc_card_broken_byte_mode_512(const struct mmc_card * c)223 static inline int mmc_card_broken_byte_mode_512(const struct mmc_card *c)
224 {
225 	return c->quirks & MMC_QUIRK_BROKEN_BYTE_MODE_512;
226 }
227 #endif /* kernel >= v4.11 */
228 
229 /*
230  * Calculate the maximum byte mode transfer size
231  */
sdio_max_byte_size(struct sdio_func * func)232 static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
233 {
234 	unsigned mval =	func->card->host->max_blk_size;
235 
236 	if (mmc_blksz_for_byte_mode(func->card))
237 		mval = min(mval, func->cur_blksize);
238 	else
239 		mval = min(mval, func->max_blksize);
240 
241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
242 	if (mmc_card_broken_byte_mode_512(func->card))
243 		return min(mval, 511u);
244 #endif /* kernel v3.3 */
245 
246 	return min(mval, 512u); /* maximum size for byte mode */
247 }
248 
dump_sdio_card_info(void * sel,struct dvobj_priv * dvobj)249 void dump_sdio_card_info(void *sel, struct dvobj_priv *dvobj)
250 {
251 	PSDIO_DATA psdio_data = dvobj_to_sdio(dvobj);
252 	struct mmc_card *card = psdio_data->card;
253 	int i;
254 
255 	RTW_PRINT_SEL(sel, "== SDIO Card Info ==\n");
256 	RTW_PRINT_SEL(sel, "  card: %p\n", card);
257 	RTW_PRINT_SEL(sel, "  clock: %d Hz\n", psdio_data->clock);
258 
259 	RTW_PRINT_SEL(sel, "  timing spec: ");
260 	switch (psdio_data->timing) {
261 	case MMC_TIMING_LEGACY:
262 		_RTW_PRINT_SEL(sel, "legacy");
263 		break;
264 	case MMC_TIMING_MMC_HS:
265 		_RTW_PRINT_SEL(sel, "mmc high-speed");
266 		break;
267 	case MMC_TIMING_SD_HS:
268 		_RTW_PRINT_SEL(sel, "sd high-speed");
269 		break;
270 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
271 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
272 	case MMC_TIMING_UHS_SDR12:
273 		_RTW_PRINT_SEL(sel, "sd uhs SDR12");
274 		break;
275 	case MMC_TIMING_UHS_SDR25:
276 		_RTW_PRINT_SEL(sel, "sd uhs SDR25");
277 		break;
278 	#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0) */
279 
280 	case MMC_TIMING_UHS_SDR50:
281 		_RTW_PRINT_SEL(sel, "sd uhs SDR50");
282 		break;
283 
284 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
285 	case MMC_TIMING_MMC_DDR52:
286 		_RTW_PRINT_SEL(sel, "mmc DDR52");
287 		break;
288 	#endif
289 
290 	case MMC_TIMING_UHS_SDR104:
291 		_RTW_PRINT_SEL(sel, "sd uhs SDR104");
292 		break;
293 	case MMC_TIMING_UHS_DDR50:
294 		_RTW_PRINT_SEL(sel, "sd uhs DDR50");
295 		break;
296 
297 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)
298 	case MMC_TIMING_MMC_HS200:
299 		_RTW_PRINT_SEL(sel, "mmc HS200");
300 		break;
301 	#endif
302 
303 	#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
304 	case MMC_TIMING_MMC_HS400:
305 		_RTW_PRINT_SEL(sel, "mmc HS400");
306 		break;
307 	#endif
308 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0) */
309 	default:
310 		_RTW_PRINT_SEL(sel, "unknown(%d)", psdio_data->timing);
311 		break;
312 	}
313 	_RTW_PRINT_SEL(sel, "\n");
314 
315 	RTW_PRINT_SEL(sel, "  sd3_bus_mode: %s\n", (psdio_data->sd3_bus_mode) ? "TRUE" : "FALSE");
316 
317 	rtw_warn_on(card->sdio_funcs != rtw_sdio_get_num_of_func(dvobj));
318 	RTW_PRINT_SEL(sel, "  func num: %u\n", card->sdio_funcs);
319 	for (i = 0; card->sdio_func[i]; i++) {
320 		RTW_PRINT_SEL(sel, "  func%u: %p%s\n"
321 			, card->sdio_func[i]->num, card->sdio_func[i]
322 			, psdio_data->func == card->sdio_func[i] ? " (*)" : "");
323 	}
324 
325 	RTW_PRINT_SEL(sel, "  max_byte_size: %u\n", psdio_data->max_byte_size);
326 
327 	RTW_PRINT_SEL(sel, "================\n");
328 }
329 
330 #define SDIO_CARD_INFO_DUMP(dvobj)	dump_sdio_card_info(RTW_DBGDUMP, dvobj)
331 
332 #ifdef DBG_SDIO
333 #if (DBG_SDIO >= 2)
rtw_sdio_dbg_reg_free(struct dvobj_priv * d)334 void rtw_sdio_dbg_reg_free(struct dvobj_priv *d)
335 {
336 	struct sdio_data *sdio;
337 	u8 *buf;
338 	u32 size;
339 
340 
341 	sdio = dvobj_to_sdio(d);
342 
343 	buf = sdio->dbg_msg;
344 	size = sdio->dbg_msg_size;
345 	if (buf){
346 		sdio->dbg_msg = NULL;
347 		sdio->dbg_msg_size = 0;
348 		rtw_mfree(buf, size);
349 	}
350 
351 	buf = sdio->reg_mac;
352 	if (buf) {
353 		sdio->reg_mac = NULL;
354 		rtw_mfree(buf, 0x800);
355 	}
356 
357 	buf = sdio->reg_mac_ext;
358 	if (buf) {
359 		sdio->reg_mac_ext = NULL;
360 		rtw_mfree(buf, 0x800);
361 	}
362 
363 	buf = sdio->reg_local;
364 	if (buf) {
365 		sdio->reg_local = NULL;
366 		rtw_mfree(buf, 0x100);
367 	}
368 
369 	buf = sdio->reg_cia;
370 	if (buf) {
371 		sdio->reg_cia = NULL;
372 		rtw_mfree(buf, 0x200);
373 	}
374 }
375 
rtw_sdio_dbg_reg_alloc(struct dvobj_priv * d)376 void rtw_sdio_dbg_reg_alloc(struct dvobj_priv *d)
377 {
378 	struct sdio_data *sdio;
379 	u8 *buf;
380 
381 
382 	sdio = dvobj_to_sdio(d);
383 
384 	buf = _rtw_zmalloc(0x800);
385 	if (buf)
386 		sdio->reg_mac = buf;
387 
388 	buf = _rtw_zmalloc(0x800);
389 	if (buf)
390 		sdio->reg_mac_ext = buf;
391 
392 	buf = _rtw_zmalloc(0x100);
393 	if (buf)
394 		sdio->reg_local = buf;
395 
396 	buf = _rtw_zmalloc(0x200);
397 	if (buf)
398 		sdio->reg_cia = buf;
399 }
400 #endif /* DBG_SDIO >= 2 */
401 
sdio_dbg_init(struct dvobj_priv * d)402 static void sdio_dbg_init(struct dvobj_priv *d)
403 {
404 	struct sdio_data *sdio;
405 
406 
407 	sdio = dvobj_to_sdio(d);
408 
409 	sdio->cmd52_err_cnt = 0;
410 	sdio->cmd53_err_cnt = 0;
411 
412 #if (DBG_SDIO >= 1)
413 	sdio->reg_dump_mark = 0;
414 #endif /* DBG_SDIO >= 1 */
415 
416 #if (DBG_SDIO >= 3)
417 	sdio->dbg_enable = 0;
418 	sdio->err_stop = 0;
419 	sdio->err_test = 0;
420 	sdio->err_test_triggered = 0;
421 #endif /* DBG_SDIO >= 3 */
422 }
423 
sdio_dbg_deinit(struct dvobj_priv * d)424 static void sdio_dbg_deinit(struct dvobj_priv *d)
425 {
426 #if (DBG_SDIO >= 2)
427 	rtw_sdio_dbg_reg_free(d);
428 #endif /* DBG_SDIO >= 2 */
429 }
430 #endif /* DBG_SDIO */
431 
rtw_sdio_init(struct dvobj_priv * dvobj)432 u32 rtw_sdio_init(struct dvobj_priv *dvobj)
433 {
434 	PSDIO_DATA psdio_data;
435 	struct sdio_func *func;
436 	int err;
437 
438 
439 	psdio_data = dvobj_to_sdio(dvobj);
440 	func = psdio_data->func;
441 
442 	/* 3 1. init SDIO bus */
443 	sdio_claim_host(func);
444 
445 	err = sdio_enable_func(func);
446 	if (err) {
447 		dvobj->drv_dbg.dbg_sdio_init_error_cnt++;
448 		RTW_PRINT("%s: sdio_enable_func FAIL(%d)!\n", __func__, err);
449 		goto release;
450 	}
451 
452 	err = sdio_set_block_size(func, 512);
453 	if (err) {
454 		dvobj->drv_dbg.dbg_sdio_init_error_cnt++;
455 		RTW_PRINT("%s: sdio_set_block_size FAIL(%d)!\n", __func__, err);
456 		goto release;
457 	}
458 	psdio_data->block_transfer_len = 512;
459 	psdio_data->tx_block_mode = 1;
460 	psdio_data->rx_block_mode = 1;
461 
462 	psdio_data->card = func->card;
463 	psdio_data->timing = func->card->host->ios.timing;
464 	psdio_data->clock = func->card->host->ios.clock;
465 	psdio_data->func_number = func->card->sdio_funcs;
466 
467 	psdio_data->sd3_bus_mode = _FALSE;
468 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 0, 0)
469 	if (psdio_data->timing <= MMC_TIMING_UHS_DDR50
470 		#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
471 		&& psdio_data->timing >= MMC_TIMING_UHS_SDR12
472 		#else
473 		&& psdio_data->timing >= MMC_TIMING_UHS_SDR50
474 		#endif
475 	)
476 		psdio_data->sd3_bus_mode = _TRUE;
477 #endif
478 
479 	psdio_data->max_byte_size = sdio_max_byte_size(func);
480 
481 #ifdef DBG_SDIO
482 	sdio_dbg_init(dvobj);
483 #endif /* DBG_SDIO */
484 
485 	SDIO_CARD_INFO_DUMP(dvobj);
486 
487 
488 release:
489 	sdio_release_host(func);
490 
491 	if (err)
492 		return _FAIL;
493 	return _SUCCESS;
494 }
495 
rtw_sdio_deinit(struct dvobj_priv * dvobj)496 void rtw_sdio_deinit(struct dvobj_priv *dvobj)
497 {
498 	struct sdio_func *func;
499 	int err;
500 
501 	func = dvobj_to_sdio(dvobj)->func;
502 
503 	if (func) {
504 		sdio_claim_host(func);
505 		err = sdio_disable_func(func);
506 		if (err) {
507 			dvobj->drv_dbg.dbg_sdio_deinit_error_cnt++;
508 			RTW_ERR("%s: sdio_disable_func(%d)\n", __func__, err);
509 		}
510 
511 		sdio_release_host(func);
512 	}
513 
514 #ifdef DBG_SDIO
515 	sdio_dbg_deinit(dvobj);
516 #endif /* DBG_SDIO */
517 }
518 
rtw_sdio_get_num_of_func(struct dvobj_priv * dvobj)519 u8 rtw_sdio_get_num_of_func(struct dvobj_priv *dvobj)
520 {
521 	return dvobj_to_sdio(dvobj)->func_number;
522 }
523 
sdio_dvobj_init(struct sdio_func * func,const struct sdio_device_id * pdid)524 static struct dvobj_priv *sdio_dvobj_init(struct sdio_func *func,
525 					const struct sdio_device_id *pdid)
526 {
527 	int status = _FAIL;
528 	struct dvobj_priv *dvobj = NULL;
529 	struct sdio_data *psdio;
530 
531 
532 	dvobj = devobj_init();
533 	if (dvobj == NULL)
534 		goto exit;
535 
536 	sdio_set_drvdata(func, dvobj);
537 
538 	psdio = dvobj_to_sdio(dvobj);
539 	psdio->func = func;
540 
541 	psdio->tmpbuf_sz = 32;
542 	psdio->tmpbuf = rtw_malloc(psdio->tmpbuf_sz);
543 	if (!psdio->tmpbuf) {
544 		psdio->tmpbuf_sz = 0;
545 		goto free_dvobj;
546 	}
547 
548 	if (rtw_sdio_init(dvobj) != _SUCCESS) {
549 		goto free_dvobj;
550 	}
551 
552 	dvobj->interface_type = RTW_HCI_SDIO;
553 	dvobj->ic_id = pdid->driver_data;
554 	dvobj->intf_ops = &sdio_ops;
555 
556 	rtw_reset_continual_io_error(dvobj);
557 	status = _SUCCESS;
558 
559 free_dvobj:
560 	if (status != _SUCCESS && dvobj) {
561 		sdio_set_drvdata(func, NULL);
562 		devobj_deinit(dvobj);
563 		dvobj = NULL;
564 	}
565 exit:
566 	return dvobj;
567 }
568 
sdio_dvobj_deinit(struct sdio_func * func)569 static void sdio_dvobj_deinit(struct sdio_func *func)
570 {
571 	struct dvobj_priv *dvobj = sdio_get_drvdata(func);
572 	struct sdio_data *sdio;
573 
574 
575 	sdio_set_drvdata(func, NULL);
576 	if (dvobj) {
577 		rtw_sdio_deinit(dvobj);
578 		rtw_sdio_free_irq(dvobj);
579 
580 		sdio = dvobj_to_sdio(dvobj);
581 		if (sdio->tmpbuf_sz) {
582 			rtw_mfree(sdio->tmpbuf, sdio->tmpbuf_sz);
583 			sdio->tmpbuf_sz = 0;
584 			sdio->tmpbuf = NULL;
585 		}
586 
587 		devobj_deinit(dvobj);
588 	}
589 
590 	return;
591 }
592 
593 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
594 _adapter * g_test_adapter = NULL;
595 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
596 
rtw_sdio_primary_adapter_init(struct dvobj_priv * dvobj)597 _adapter *rtw_sdio_primary_adapter_init(struct dvobj_priv *dvobj)
598 {
599 	int status = _FAIL;
600 	_adapter *padapter = NULL;
601 	u8 hw_mac_addr[ETH_ALEN] = {0};
602 
603 	padapter = (_adapter *)rtw_zvmalloc(sizeof(*padapter));
604 	if (padapter == NULL)
605 		goto exit;
606 
607 	/*registry_priv*/
608 	if (rtw_load_registry(padapter) != _SUCCESS)
609 		goto free_adapter;
610 
611 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
612 	g_test_adapter = padapter;
613 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
614 	padapter->dvobj = dvobj;
615 
616 	dvobj->padapters[dvobj->iface_nums++] = padapter;
617 	padapter->iface_id = IFACE_ID0;
618 
619 	/* set adapter_type/iface type for primary padapter */
620 	padapter->isprimary = _TRUE;
621 	padapter->adapter_type = PRIMARY_ADAPTER;
622 
623 	padapter->hw_port = HW_PORT0;
624 
625 	/* 3 7. init driver common data */
626 	if (rtw_init_drv_sw(padapter) == _FAIL) {
627 		goto free_adapter;
628 	}
629 
630 	/* get mac addr */
631 	rtw_hw_get_mac_addr(dvobj, hw_mac_addr);
632 
633 	/* set mac addr */
634 	rtw_macaddr_cfg(adapter_mac_addr(padapter), hw_mac_addr);
635 
636 	RTW_INFO("bDriverStopped:%s, bSurpriseRemoved:%s, netif_up:%d, hw_init_completed:%d\n"
637 		, dev_is_drv_stopped(dvobj) ? "True" : "False"
638 		, dev_is_surprise_removed(dvobj) ? "True" : "False"
639 		, padapter->netif_up
640 		, rtw_hw_get_init_completed(dvobj)
641 	);
642 
643 	status = _SUCCESS;
644 
645 free_adapter:
646 	if (status != _SUCCESS && padapter) {
647 		rtw_vmfree((u8 *)padapter, sizeof(*padapter));
648 		padapter = NULL;
649 	}
650 exit:
651 	return padapter;
652 }
653 
rtw_sdio_primary_adapter_deinit(_adapter * padapter)654 static void rtw_sdio_primary_adapter_deinit(_adapter *padapter)
655 {
656 
657 #ifdef CONFIG_GPIO_WAKEUP
658 #ifdef CONFIG_PLATFORM_ARM_SUN6I
659 	sw_gpio_eint_set_enable(gpio_eint_wlan, 0);
660 	sw_gpio_irq_free(eint_wlan_handle);
661 #else
662 	gpio_hostwakeup_free_irq(padapter);
663 #endif
664 #endif
665 	rtw_free_drv_sw(padapter);
666 
667 	/* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */
668 	rtw_os_ndev_free(padapter);
669 
670 	rtw_vmfree((u8 *)padapter, sizeof(_adapter));
671 #ifdef RTW_SUPPORT_PLATFORM_SHUTDOWN
672 	g_test_adapter = NULL;
673 #endif /* RTW_SUPPORT_PLATFORM_SHUTDOWN */
674 }
675 
676 /*
677  * drv_init() - a device potentially for us
678  *
679  * notes: drv_init() is called when the bus driver has located a card for us to support.
680  *        We accept the new device by returning 0.
681  */
rtw_dev_probe(struct sdio_func * func,const struct sdio_device_id * id)682 static int rtw_dev_probe(
683 	struct sdio_func *func,
684 	const struct sdio_device_id *id)
685 {
686 	_adapter *padapter = NULL;
687 	struct dvobj_priv *dvobj;
688 
689 	RTW_INFO("+%s\n", __func__);
690 
691 	dvobj = sdio_dvobj_init(func, id);
692 	if (dvobj == NULL) {
693 		RTW_ERR("dvobj == NULL\n");
694 		goto exit;
695 	}
696 
697 	if (devobj_trx_resource_init(dvobj) == _FAIL)
698 		goto free_dvobj;
699 
700 	/*init hw - register and get chip-info and hw capability*/
701 	if (rtw_hw_init(dvobj) == _FAIL) {
702 		RTW_ERR("rtw_hw_init Failed!\n");
703 		goto free_trx_reso;
704 	}
705 
706 	padapter = rtw_sdio_primary_adapter_init(dvobj);
707 	if (padapter == NULL) {
708 		RTW_INFO("rtw_init_primary_adapter Failed!\n");
709 		goto free_hw;
710 	}
711 
712 #ifdef CONFIG_CONCURRENT_MODE
713 	if (rtw_drv_add_vir_ifaces(dvobj) == _FAIL)
714 		goto free_if_vir;
715 #endif
716 
717 	/*init data of dvobj from registary and ic spec*/
718 	if (devobj_data_init(dvobj) == _FAIL) {
719 		RTW_ERR("devobj_data_init Failed!\n");
720 		/*free self beacuse of the function donnot clean memory when fail*/
721 		goto free_dvobj_data;
722 	}
723 
724 	/* dev_alloc_name && register_netdev */
725 	if (rtw_os_ndevs_init(dvobj) != _SUCCESS)
726 		goto free_dvobj_data;
727 
728 #ifdef CONFIG_HOSTAPD_MLME
729 	hostapd_mode_init(padapter);
730 #endif
731 
732 	if (rtw_sdio_alloc_irq(dvobj) != _SUCCESS)
733 		goto os_ndevs_deinit;
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 		RTW_INFO("%s: request irq failed\n", __func__);
740 		goto os_ndevs_deinit;
741 	}
742 	#else
743 	gpio_hostwakeup_alloc_irq(padapter);
744 	#endif
745 #endif/*CONFIG_GPIO_WAKEUP*/
746 
747 #ifdef CONFIG_GLOBAL_UI_PID
748 	if (ui_pid[1] != 0) {
749 		RTW_INFO("ui_pid[1]:%d\n", ui_pid[1]);
750 		rtw_signal_process(ui_pid[1], SIGUSR2);
751 	}
752 #endif
753 	RTW_INFO("-%s success\n", __func__);
754 
755 	return 0; /*_SUCCESS*/
756 
757 
758 os_ndevs_deinit:
759 	rtw_os_ndevs_deinit(dvobj);
760 
761 free_dvobj_data:
762 	devobj_data_deinit(dvobj);
763 
764 #ifdef CONFIG_CONCURRENT_MODE
765 free_if_vir:
766 	rtw_drv_stop_vir_ifaces(dvobj);
767 	rtw_drv_free_vir_ifaces(dvobj);
768 #endif
769 	if (padapter)
770 		rtw_sdio_primary_adapter_deinit(padapter);
771 free_hw:
772 	rtw_hw_deinit(dvobj);
773 
774 free_trx_reso:
775 	devobj_trx_resource_deinit(dvobj);
776 
777 free_dvobj:
778 	sdio_dvobj_deinit(func);
779 exit:
780 	return -ENODEV;
781 }
782 
rtw_dev_remove(struct sdio_func * func)783 static void rtw_dev_remove(struct sdio_func *func)
784 {
785 	struct dvobj_priv *dvobj = sdio_get_drvdata(func);
786 	struct pwrctrl_priv *pwrctl = dvobj_to_pwrctl(dvobj);
787 	_adapter *padapter = dvobj_get_primary_adapter(dvobj);
788 
789 	RTW_INFO("+%s\n", __func__);
790 
791 	dvobj->processing_dev_remove = _TRUE;
792 
793 	/* TODO: use rtw_os_ndevs_deinit instead at the first stage of driver's dev deinit function */
794 	rtw_os_ndevs_unregister(dvobj);
795 
796 	if (!dev_is_surprise_removed(dvobj)) {
797 		int err;
798 
799 		/* test surprise remove */
800 		sdio_claim_host(func);
801 		sdio_readb(func, 0, &err);
802 		sdio_release_host(func);
803 		if (err == -ENOMEDIUM) {
804 			dev_set_surprise_removed(dvobj);
805 			RTW_INFO("%s: device had been removed!\n", __func__);
806 		}
807 	}
808 
809 #if defined(CONFIG_HAS_EARLYSUSPEND) || defined(CONFIG_ANDROID_POWER)
810 	rtw_unregister_early_suspend(pwrctl);
811 #endif
812 #if 0 /*GEORGIA_TODO_FIXIT*/
813 	if (GET_PHL_COM(adapter_to_dvobj(padapter))->fw_ready == _TRUE) {
814 		rtw_ps_deny(padapter, PS_DENY_DRV_REMOVE);
815 		rtw_pm_set_ips(padapter, IPS_NONE);
816 		rtw_pm_set_lps(padapter, PM_PS_MODE_ACTIVE);
817 		LeaveAllPowerSaveMode(padapter);
818 	}
819 #endif
820 	dev_set_drv_stopped(adapter_to_dvobj(padapter));	/*for stop thread*/
821 #if 0 /*#ifdef CONFIG_CORE_CMD_THREAD*/
822 	rtw_stop_cmd_thread(padapter);
823 #endif
824 
825 #ifdef CONFIG_CONCURRENT_MODE
826 	rtw_drv_stop_vir_ifaces(dvobj);
827 #endif
828 	rtw_drv_stop_prim_iface(padapter);
829 
830 	rtw_hw_stop(dvobj);
831 	dev_set_surprise_removed(dvobj);
832 
833 	rtw_sdio_primary_adapter_deinit(padapter);
834 
835 #ifdef CONFIG_CONCURRENT_MODE
836 	rtw_drv_free_vir_ifaces(dvobj);
837 #endif
838 	rtw_hw_deinit(dvobj);
839 	devobj_data_deinit(dvobj);
840 	devobj_trx_resource_deinit(dvobj);
841 	sdio_dvobj_deinit(func);
842 
843 	RTW_INFO("-%s done\n", __func__);
844 }
845 
846 #ifdef CONFIG_SDIO_HOOK_DEV_SHUTDOWN
rtw_dev_shutdown(struct device * dev)847 static void rtw_dev_shutdown(struct device *dev)
848 {
849 	struct sdio_func *func = dev_to_sdio_func(dev);
850 
851 	if (func == NULL)
852 		return;
853 
854 	RTW_INFO("==> %s !\n", __func__);
855 
856 	rtw_dev_remove(func);
857 
858 	RTW_INFO("<== %s !\n", __func__);
859 }
860 #endif
861 
rtw_sdio_suspend(struct device * dev)862 static int rtw_sdio_suspend(struct device *dev)
863 {
864 	struct sdio_func *func = dev_to_sdio_func(dev);
865 	struct dvobj_priv *psdpriv;
866 	struct pwrctrl_priv *pwrpriv = NULL;
867 	_adapter *padapter = NULL;
868 	struct debug_priv *pdbgpriv = NULL;
869 	int ret = 0;
870 #ifdef CONFIG_RTW_SDIO_PM_KEEP_POWER
871 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
872 	mmc_pm_flag_t pm_flag = 0;
873 #endif
874 #endif
875 
876 	if (dev == NULL)
877 		goto exit;
878 
879 	psdpriv = sdio_get_drvdata(func);
880 	if (psdpriv == NULL)
881 		goto exit;
882 
883 	pwrpriv = dvobj_to_pwrctl(psdpriv);
884 	padapter = dvobj_get_primary_adapter(psdpriv);
885 	pdbgpriv = &psdpriv->drv_dbg;
886 	if (dev_is_drv_stopped(adapter_to_dvobj(padapter))) {
887 		RTW_INFO("%s bDriverStopped == _TRUE\n", __func__);
888 		goto exit;
889 	}
890 
891 	if (pwrpriv->bInSuspend == _TRUE) {
892 		RTW_INFO("%s bInSuspend = %d\n", __func__, pwrpriv->bInSuspend);
893 		pdbgpriv->dbg_suspend_error_cnt++;
894 		goto exit;
895 	}
896 
897 	ret = rtw_suspend_common(padapter);
898 
899 #ifdef CONFIG_RTW_SDIO_PM_KEEP_POWER
900 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 34))
901 	/* Android 4.0 don't support WIFI close power */
902 	/* or power down or clock will close after wifi resume, */
903 	/* this is sprd's bug in Android 4.0, but sprd don't */
904 	/* want to fix it. */
905 	/* we have test power under 8723as, power consumption is ok */
906 	pm_flag = sdio_get_host_pm_caps(func);
907 	RTW_INFO("cmd: %s: suspend: PM flag = 0x%x\n", sdio_func_id(func), pm_flag);
908 	if (!(pm_flag & MMC_PM_KEEP_POWER)) {
909 		RTW_INFO("%s: cannot remain alive while host is suspended\n", sdio_func_id(func));
910 		if (pdbgpriv)
911 			pdbgpriv->dbg_suspend_error_cnt++;
912 		return -ENOSYS;
913 	} else {
914 		RTW_INFO("cmd: suspend with MMC_PM_KEEP_POWER\n");
915 		sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
916 	}
917 #endif
918 #endif
919 exit:
920 	return ret;
921 }
rtw_resume_process(_adapter * padapter)922 static int rtw_resume_process(_adapter *padapter)
923 {
924 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
925 	struct dvobj_priv *psdpriv = padapter->dvobj;
926 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
927 
928 	if (pwrpriv->bInSuspend == _FALSE) {
929 		pdbgpriv->dbg_resume_error_cnt++;
930 		RTW_INFO("%s bInSuspend = %d\n", __FUNCTION__, pwrpriv->bInSuspend);
931 		return -1;
932 	}
933 
934 	return rtw_resume_common(padapter);
935 }
936 
rtw_sdio_resume(struct device * dev)937 static int rtw_sdio_resume(struct device *dev)
938 {
939 	struct sdio_func *func = dev_to_sdio_func(dev);
940 	struct dvobj_priv *psdpriv = sdio_get_drvdata(func);
941 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv);
942 	_adapter *padapter = dvobj_get_primary_adapter(psdpriv);
943 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
944 	int ret = 0;
945 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
946 
947 	RTW_INFO("==> %s (%s:%d)\n", __FUNCTION__, current->comm, current->pid);
948 
949 	pdbgpriv->dbg_resume_cnt++;
950 
951 	if (pwrpriv->wowlan_mode || pwrpriv->wowlan_ap_mode) {
952 		rtw_resume_lock_suspend();
953 		ret = rtw_resume_process(padapter);
954 		rtw_resume_unlock_suspend();
955 	} else {
956 #ifdef CONFIG_RESUME_IN_WORKQUEUE
957 		rtw_resume_in_workqueue(pwrpriv);
958 #else
959 		if (rtw_is_earlysuspend_registered(pwrpriv)) {
960 			/* jeff: bypass resume here, do in late_resume */
961 			rtw_set_do_late_resume(pwrpriv, _TRUE);
962 		} else {
963 			rtw_resume_lock_suspend();
964 			ret = rtw_resume_process(padapter);
965 			rtw_resume_unlock_suspend();
966 		}
967 #endif
968 	}
969 	pmlmeext->last_scan_time = rtw_get_current_time();
970 	RTW_INFO("<========  %s return %d\n", __FUNCTION__, ret);
971 	return ret;
972 
973 }
974 
rtw_drv_entry(void)975 static int __init rtw_drv_entry(void)
976 {
977 	int ret = 0;
978 
979 	RTW_PRINT("module init start\n");
980 	dump_drv_version(RTW_DBGDUMP);
981 #ifdef BTCOEXVERSION
982 	RTW_PRINT(DRV_NAME" BT-Coex version = %s\n", BTCOEXVERSION);
983 #endif /* BTCOEXVERSION */
984 
985 #if (defined(CONFIG_RTKM) && defined(CONFIG_RTKM_BUILT_IN))
986 	ret = rtkm_prealloc_init();
987 	if (ret) {
988 		RTW_INFO("%s: pre-allocate memory failed!!(%d)\n", __FUNCTION__,
989 			 ret);
990 		goto exit;
991 	}
992 #endif /* CONFIG_RTKM */
993 
994 	rtw_android_wifictrl_func_add();
995 
996 	ret = platform_wifi_power_on();
997 	if (ret) {
998 		RTW_INFO("%s: power on failed!!(%d)\n", __FUNCTION__, ret);
999 		ret = -1;
1000 		goto exit;
1001 	}
1002 
1003 	sdio_drvpriv.drv_registered = _TRUE;
1004 	rtw_suspend_lock_init();
1005 	rtw_drv_proc_init();
1006 	rtw_nlrtw_init();
1007 	rtw_ndev_notifier_register();
1008 	rtw_inetaddr_notifier_register();
1009 
1010 	ret = sdio_register_driver(&sdio_drvpriv.rtw_sdio_drv);
1011 	if (ret != 0) {
1012 		sdio_drvpriv.drv_registered = _FALSE;
1013 		rtw_suspend_lock_uninit();
1014 		rtw_drv_proc_deinit();
1015 		rtw_nlrtw_deinit();
1016 		rtw_ndev_notifier_unregister();
1017 		rtw_inetaddr_notifier_unregister();
1018 		RTW_INFO("%s: register driver failed!!(%d)\n", __FUNCTION__, ret);
1019 		goto poweroff;
1020 	}
1021 
1022 	goto exit;
1023 
1024 poweroff:
1025 	platform_wifi_power_off();
1026 
1027 exit:
1028 	RTW_PRINT("module init ret=%d\n", ret);
1029 	return ret;
1030 }
1031 
rtw_drv_halt(void)1032 static void __exit rtw_drv_halt(void)
1033 {
1034 	RTW_PRINT("module exit start\n");
1035 
1036 	sdio_drvpriv.drv_registered = _FALSE;
1037 
1038 	sdio_unregister_driver(&sdio_drvpriv.rtw_sdio_drv);
1039 
1040 	rtw_android_wifictrl_func_del();
1041 
1042 	platform_wifi_power_off();
1043 
1044 	rtw_suspend_lock_uninit();
1045 	rtw_drv_proc_deinit();
1046 	rtw_nlrtw_deinit();
1047 	rtw_ndev_notifier_unregister();
1048 	rtw_inetaddr_notifier_unregister();
1049 
1050 	RTW_PRINT("module exit success\n");
1051 
1052 	rtw_mstat_dump(RTW_DBGDUMP);
1053 
1054 #if (defined(CONFIG_RTKM) && defined(CONFIG_RTKM_BUILT_IN))
1055 	rtkm_prealloc_destroy();
1056 #elif (defined(CONFIG_RTKM) && defined(CONFIG_RTKM_STANDALONE))
1057 	rtkm_dump_mstatus(RTW_DBGDUMP);
1058 #endif /* CONFIG_RTKM */
1059 }
1060 
1061 module_init(rtw_drv_entry);
1062 module_exit(rtw_drv_halt);
1063 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
1064 MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
1065 #endif
1066