1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2013 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define __HAL_BTCOEX_C__
8 
9 #include <hal_data.h>
10 #include <rtw_debug.h>
11 #include <hal_btcoex.h>
12 #include <Mp_Precomp.h>
13 
14 /* 		Global variables */
15 static const char *const BtProfileString[] = {
16 	"NONE",
17 	"A2DP",
18 	"PAN",
19 	"HID",
20 	"SCO",
21 };
22 
23 static const char *const BtSpecString[] = {
24 	"1.0b",
25 	"1.1",
26 	"1.2",
27 	"2.0+EDR",
28 	"2.1+EDR",
29 	"3.0+HS",
30 	"4.0",
31 };
32 
33 static const char *const BtLinkRoleString[] = {
34 	"Master",
35 	"Slave",
36 };
37 
38 static const char *const h2cStaString[] = {
39 	"successful",
40 	"h2c busy",
41 	"rf off",
42 	"fw not read",
43 };
44 
45 static const char *const ioStaString[] = {
46 	"success",
47 	"can not IO",
48 	"rf off",
49 	"fw not read",
50 	"wait io timeout",
51 	"invalid len",
52 	"idle Q empty",
53 	"insert waitQ fail",
54 	"unknown fail",
55 	"wrong level",
56 	"h2c stopped",
57 };
58 
59 BTC_COEXIST GLBtCoexist;
60 static u8 GLBtcWiFiInScanState;
61 static u8 GLBtcWiFiInIQKState;
62 
63 u32 GLBtcDbgType[BTC_MSG_MAX];
64 static u8 GLBtcDbgBuf[BT_TMP_BUF_SIZE];
65 
66 typedef struct _btcoexdbginfo {
67 	u8 *info;
68 	u32 size; /*  buffer total size */
69 	u32 len; /*  now used length */
70 } BTCDBGINFO, *PBTCDBGINFO;
71 
72 static BTCDBGINFO GLBtcDbgInfo;
73 
74 #define	BT_Operation(Adapter)						false
75 
76 static void DBG_BT_INFO_INIT(PBTCDBGINFO pinfo, u8 *pbuf, u32 size)
77 {
78 	if (!pinfo)
79 		return;
80 
81 	memset(pinfo, 0, sizeof(BTCDBGINFO));
82 
83 	if (pbuf && size) {
84 		pinfo->info = pbuf;
85 		pinfo->size = size;
86 	}
87 }
88 
89 void DBG_BT_INFO(u8 *dbgmsg)
90 {
91 	PBTCDBGINFO pinfo;
92 	u32 msglen;
93 	u8 *pbuf;
94 
95 
96 	pinfo = &GLBtcDbgInfo;
97 
98 	if (!pinfo->info)
99 		return;
100 
101 	msglen = strlen(dbgmsg);
102 	if (pinfo->len + msglen > pinfo->size)
103 		return;
104 
105 	pbuf = pinfo->info + pinfo->len;
106 	memcpy(pbuf, dbgmsg, msglen);
107 	pinfo->len += msglen;
108 }
109 
110 /*  */
111 /* 		Debug related function */
112 /*  */
113 static u8 halbtcoutsrc_IsBtCoexistAvailable(PBTC_COEXIST pBtCoexist)
114 {
115 	if (!pBtCoexist->bBinded || !pBtCoexist->Adapter)
116 		return false;
117 
118 	return true;
119 }
120 
121 static void halbtcoutsrc_DbgInit(void)
122 {
123 	u8 i;
124 
125 	for (i = 0; i < BTC_MSG_MAX; i++)
126 		GLBtcDbgType[i] = 0;
127 
128 	GLBtcDbgType[BTC_MSG_INTERFACE]			=	\
129 /* 			INTF_INIT								| */
130 /* 			INTF_NOTIFY							| */
131 			0;
132 
133 	GLBtcDbgType[BTC_MSG_ALGORITHM]			=	\
134 /* 			ALGO_BT_RSSI_STATE					| */
135 /* 			ALGO_WIFI_RSSI_STATE					| */
136 /* 			ALGO_BT_MONITOR						| */
137 /* 			ALGO_TRACE							| */
138 /* 			ALGO_TRACE_FW						| */
139 /* 			ALGO_TRACE_FW_DETAIL				| */
140 /* 			ALGO_TRACE_FW_EXEC					| */
141 /* 			ALGO_TRACE_SW						| */
142 /* 			ALGO_TRACE_SW_DETAIL				| */
143 /* 			ALGO_TRACE_SW_EXEC					| */
144 			0;
145 }
146 
147 static void halbtcoutsrc_LeaveLps(PBTC_COEXIST pBtCoexist)
148 {
149 	struct adapter *padapter;
150 
151 
152 	padapter = pBtCoexist->Adapter;
153 
154 	pBtCoexist->btInfo.bBtCtrlLps = true;
155 	pBtCoexist->btInfo.bBtLpsOn = false;
156 
157 	rtw_btcoex_LPS_Leave(padapter);
158 }
159 
160 static void halbtcoutsrc_EnterLps(PBTC_COEXIST pBtCoexist)
161 {
162 	struct adapter *padapter;
163 
164 
165 	padapter = pBtCoexist->Adapter;
166 
167 	pBtCoexist->btInfo.bBtCtrlLps = true;
168 	pBtCoexist->btInfo.bBtLpsOn = true;
169 
170 	rtw_btcoex_LPS_Enter(padapter);
171 }
172 
173 static void halbtcoutsrc_NormalLps(PBTC_COEXIST pBtCoexist)
174 {
175 	struct adapter *padapter;
176 
177 
178 	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Normal LPS behavior!!!\n"));
179 
180 	padapter = pBtCoexist->Adapter;
181 
182 	if (pBtCoexist->btInfo.bBtCtrlLps) {
183 		pBtCoexist->btInfo.bBtLpsOn = false;
184 		rtw_btcoex_LPS_Leave(padapter);
185 		pBtCoexist->btInfo.bBtCtrlLps = false;
186 
187 		/*  recover the LPS state to the original */
188 	}
189 }
190 
191 /*
192  *  Constraint:
193  *   1. this function will request pwrctrl->lock
194  */
195 static void halbtcoutsrc_LeaveLowPower(PBTC_COEXIST pBtCoexist)
196 {
197 	struct adapter *padapter;
198 	s32 ready;
199 	unsigned long stime;
200 	unsigned long utime;
201 	u32 timeout; /*  unit: ms */
202 
203 
204 	padapter = pBtCoexist->Adapter;
205 	ready = _FAIL;
206 #ifdef LPS_RPWM_WAIT_MS
207 	timeout = LPS_RPWM_WAIT_MS;
208 #else /*  !LPS_RPWM_WAIT_MS */
209 	timeout = 30;
210 #endif /*  !LPS_RPWM_WAIT_MS */
211 
212 	stime = jiffies;
213 	do {
214 		ready = rtw_register_task_alive(padapter, BTCOEX_ALIVE);
215 		if (_SUCCESS == ready)
216 			break;
217 
218 		utime = jiffies_to_msecs(jiffies - stime);
219 		if (utime > timeout)
220 			break;
221 
222 		msleep(1);
223 	} while (1);
224 }
225 
226 /*
227  *  Constraint:
228  *   1. this function will request pwrctrl->lock
229  */
230 static void halbtcoutsrc_NormalLowPower(PBTC_COEXIST pBtCoexist)
231 {
232 	struct adapter *padapter;
233 
234 
235 	padapter = pBtCoexist->Adapter;
236 	rtw_unregister_task_alive(padapter, BTCOEX_ALIVE);
237 }
238 
239 static void halbtcoutsrc_DisableLowPower(PBTC_COEXIST pBtCoexist, u8 bLowPwrDisable)
240 {
241 	pBtCoexist->btInfo.bBtDisableLowPwr = bLowPwrDisable;
242 	if (bLowPwrDisable)
243 		halbtcoutsrc_LeaveLowPower(pBtCoexist);		/*  leave 32k low power. */
244 	else
245 		halbtcoutsrc_NormalLowPower(pBtCoexist);	/*  original 32k low power behavior. */
246 }
247 
248 static void halbtcoutsrc_AggregationCheck(PBTC_COEXIST pBtCoexist)
249 {
250 	struct adapter *padapter;
251 	bool bNeedToAct;
252 
253 
254 	padapter = pBtCoexist->Adapter;
255 	bNeedToAct = false;
256 
257 	if (pBtCoexist->btInfo.bRejectAggPkt) {
258 		rtw_btcoex_RejectApAggregatedPacket(padapter, true);
259 	} else {
260 		if (pBtCoexist->btInfo.bPreBtCtrlAggBufSize !=
261 			pBtCoexist->btInfo.bBtCtrlAggBufSize) {
262 			bNeedToAct = true;
263 			pBtCoexist->btInfo.bPreBtCtrlAggBufSize = pBtCoexist->btInfo.bBtCtrlAggBufSize;
264 		}
265 
266 		if (pBtCoexist->btInfo.bBtCtrlAggBufSize) {
267 			if (pBtCoexist->btInfo.preAggBufSize !=
268 				pBtCoexist->btInfo.aggBufSize){
269 				bNeedToAct = true;
270 			}
271 			pBtCoexist->btInfo.preAggBufSize = pBtCoexist->btInfo.aggBufSize;
272 		}
273 
274 		if (bNeedToAct) {
275 			rtw_btcoex_RejectApAggregatedPacket(padapter, true);
276 			rtw_btcoex_RejectApAggregatedPacket(padapter, false);
277 		}
278 	}
279 }
280 
281 static u8 halbtcoutsrc_IsWifiBusy(struct adapter *padapter)
282 {
283 	struct mlme_priv *pmlmepriv;
284 
285 
286 	pmlmepriv = &padapter->mlmepriv;
287 
288 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == true) {
289 		if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
290 			return true;
291 		if (pmlmepriv->LinkDetectInfo.bBusyTraffic)
292 			return true;
293 	}
294 
295 	return false;
296 }
297 
298 static u32 _halbtcoutsrc_GetWifiLinkStatus(struct adapter *padapter)
299 {
300 	struct mlme_priv *pmlmepriv;
301 	u8 bp2p;
302 	u32 portConnectedStatus;
303 
304 
305 	pmlmepriv = &padapter->mlmepriv;
306 	bp2p = false;
307 	portConnectedStatus = 0;
308 
309 	if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == true) {
310 		if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
311 			if (bp2p)
312 				portConnectedStatus |= WIFI_P2P_GO_CONNECTED;
313 			else
314 				portConnectedStatus |= WIFI_AP_CONNECTED;
315 		} else {
316 			if (bp2p)
317 				portConnectedStatus |= WIFI_P2P_GC_CONNECTED;
318 			else
319 				portConnectedStatus |= WIFI_STA_CONNECTED;
320 		}
321 	}
322 
323 	return portConnectedStatus;
324 }
325 
326 static u32 halbtcoutsrc_GetWifiLinkStatus(PBTC_COEXIST pBtCoexist)
327 {
328 	/*  */
329 	/*  return value: */
330 	/*  [31:16]=> connected port number */
331 	/*  [15:0]=> port connected bit define */
332 	/*  */
333 
334 	struct adapter *padapter;
335 	u32 retVal;
336 	u32 portConnectedStatus, numOfConnectedPort;
337 
338 
339 	padapter = pBtCoexist->Adapter;
340 	portConnectedStatus = 0;
341 	numOfConnectedPort = 0;
342 
343 	retVal = _halbtcoutsrc_GetWifiLinkStatus(padapter);
344 	if (retVal) {
345 		portConnectedStatus |= retVal;
346 		numOfConnectedPort++;
347 	}
348 
349 	retVal = (numOfConnectedPort << 16) | portConnectedStatus;
350 
351 	return retVal;
352 }
353 
354 static u32 halbtcoutsrc_GetBtPatchVer(PBTC_COEXIST pBtCoexist)
355 {
356 	return pBtCoexist->btInfo.btRealFwVer;
357 }
358 
359 static s32 halbtcoutsrc_GetWifiRssi(struct adapter *padapter)
360 {
361 	struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
362 
363 	return pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB;
364 }
365 
366 static u8 halbtcoutsrc_GetWifiScanAPNum(struct adapter *padapter)
367 {
368 	struct mlme_ext_priv *pmlmeext;
369 	static u8 scan_AP_num;
370 
371 	pmlmeext = &padapter->mlmeextpriv;
372 
373 	if (!GLBtcWiFiInScanState) {
374 		if (pmlmeext->sitesurvey_res.bss_cnt > 0xFF)
375 			scan_AP_num = 0xFF;
376 		else
377 			scan_AP_num = (u8)pmlmeext->sitesurvey_res.bss_cnt;
378 	}
379 
380 	return scan_AP_num;
381 }
382 
383 static u8 halbtcoutsrc_Get(void *pBtcContext, u8 getType, void *pOutBuf)
384 {
385 	PBTC_COEXIST pBtCoexist;
386 	struct adapter *padapter;
387 	struct hal_com_data *pHalData;
388 	struct mlme_ext_priv *mlmeext;
389 	u8 *pu8;
390 	s32 *pS4Tmp;
391 	u32 *pU4Tmp;
392 	u8 ret;
393 
394 
395 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
396 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
397 		return false;
398 
399 	padapter = pBtCoexist->Adapter;
400 	pHalData = GET_HAL_DATA(padapter);
401 	mlmeext = &padapter->mlmeextpriv;
402 	pu8 = pOutBuf;
403 	pS4Tmp = pOutBuf;
404 	pU4Tmp = pOutBuf;
405 	ret = true;
406 
407 	switch (getType) {
408 	case BTC_GET_BL_HS_OPERATION:
409 		*pu8 = false;
410 		ret = false;
411 		break;
412 
413 	case BTC_GET_BL_HS_CONNECTING:
414 		*pu8 = false;
415 		ret = false;
416 		break;
417 
418 	case BTC_GET_BL_WIFI_CONNECTED:
419 		*pu8 = check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE);
420 		break;
421 
422 	case BTC_GET_BL_WIFI_BUSY:
423 		*pu8 = halbtcoutsrc_IsWifiBusy(padapter);
424 		break;
425 
426 	case BTC_GET_BL_WIFI_SCAN:
427 		/* Use the value of the new variable GLBtcWiFiInScanState to judge whether WiFi is in scan state or not, since the originally used flag
428 			WIFI_SITE_MONITOR in fwstate may not be cleared in time */
429 		*pu8 = GLBtcWiFiInScanState;
430 		break;
431 
432 	case BTC_GET_BL_WIFI_LINK:
433 		*pu8 = check_fwstate(&padapter->mlmepriv, WIFI_UNDER_LINKING);
434 		break;
435 
436 	case BTC_GET_BL_WIFI_ROAM:
437 		*pu8 = check_fwstate(&padapter->mlmepriv, WIFI_UNDER_LINKING);
438 		break;
439 
440 	case BTC_GET_BL_WIFI_4_WAY_PROGRESS:
441 		*pu8 = false;
442 		break;
443 
444 	case BTC_GET_BL_WIFI_UNDER_5G:
445 		*pu8 = pHalData->CurrentBandType == 1;
446 		break;
447 
448 	case BTC_GET_BL_WIFI_AP_MODE_ENABLE:
449 		*pu8 = check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE);
450 		break;
451 
452 	case BTC_GET_BL_WIFI_ENABLE_ENCRYPTION:
453 		*pu8 = padapter->securitypriv.dot11PrivacyAlgrthm == 0 ? false : true;
454 		break;
455 
456 	case BTC_GET_BL_WIFI_UNDER_B_MODE:
457 		if (mlmeext->cur_wireless_mode == WIRELESS_11B)
458 			*pu8 = true;
459 		else
460 			*pu8 = false;
461 		break;
462 
463 	case BTC_GET_BL_WIFI_IS_IN_MP_MODE:
464 		*pu8 = false;
465 		break;
466 
467 	case BTC_GET_BL_EXT_SWITCH:
468 		*pu8 = false;
469 		break;
470 
471 	case BTC_GET_S4_WIFI_RSSI:
472 		*pS4Tmp = halbtcoutsrc_GetWifiRssi(padapter);
473 		break;
474 
475 	case BTC_GET_S4_HS_RSSI:
476 		*pS4Tmp = 0;
477 		ret = false;
478 		break;
479 
480 	case BTC_GET_U4_WIFI_BW:
481 		if (IsLegacyOnly(mlmeext->cur_wireless_mode))
482 			*pU4Tmp = BTC_WIFI_BW_LEGACY;
483 		else if (pHalData->CurrentChannelBW == CHANNEL_WIDTH_20)
484 			*pU4Tmp = BTC_WIFI_BW_HT20;
485 		else
486 			*pU4Tmp = BTC_WIFI_BW_HT40;
487 		break;
488 
489 	case BTC_GET_U4_WIFI_TRAFFIC_DIRECTION:
490 		{
491 			PRT_LINK_DETECT_T plinkinfo;
492 			plinkinfo = &padapter->mlmepriv.LinkDetectInfo;
493 
494 			if (plinkinfo->NumTxOkInPeriod > plinkinfo->NumRxOkInPeriod)
495 				*pU4Tmp = BTC_WIFI_TRAFFIC_TX;
496 			else
497 				*pU4Tmp = BTC_WIFI_TRAFFIC_RX;
498 		}
499 		break;
500 
501 	case BTC_GET_U4_WIFI_FW_VER:
502 		*pU4Tmp = pHalData->FirmwareVersion << 16;
503 		*pU4Tmp |= pHalData->FirmwareSubVersion;
504 		break;
505 
506 	case BTC_GET_U4_WIFI_LINK_STATUS:
507 		*pU4Tmp = halbtcoutsrc_GetWifiLinkStatus(pBtCoexist);
508 		break;
509 
510 	case BTC_GET_U4_BT_PATCH_VER:
511 		*pU4Tmp = halbtcoutsrc_GetBtPatchVer(pBtCoexist);
512 		break;
513 
514 	case BTC_GET_U1_WIFI_DOT11_CHNL:
515 		*pu8 = padapter->mlmeextpriv.cur_channel;
516 		break;
517 
518 	case BTC_GET_U1_WIFI_CENTRAL_CHNL:
519 		*pu8 = pHalData->CurrentChannel;
520 		break;
521 
522 	case BTC_GET_U1_WIFI_HS_CHNL:
523 		*pu8 = 0;
524 		ret = false;
525 		break;
526 
527 	case BTC_GET_U1_MAC_PHY_MODE:
528 		*pu8 = BTC_SMSP;
529 /* 			*pU1Tmp = BTC_DMSP; */
530 /* 			*pU1Tmp = BTC_DMDP; */
531 /* 			*pU1Tmp = BTC_MP_UNKNOWN; */
532 		break;
533 
534 	case BTC_GET_U1_AP_NUM:
535 		*pu8 = halbtcoutsrc_GetWifiScanAPNum(padapter);
536 		break;
537 
538 	/* 1Ant =========== */
539 	case BTC_GET_U1_LPS_MODE:
540 		*pu8 = padapter->dvobj->pwrctl_priv.pwr_mode;
541 		break;
542 
543 	default:
544 		ret = false;
545 		break;
546 	}
547 
548 	return ret;
549 }
550 
551 static u8 halbtcoutsrc_Set(void *pBtcContext, u8 setType, void *pInBuf)
552 {
553 	PBTC_COEXIST pBtCoexist;
554 	struct adapter *padapter;
555 	u8 *pu8;
556 	u32 *pU4Tmp;
557 	u8 ret;
558 
559 
560 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
561 	padapter = pBtCoexist->Adapter;
562 	pu8 = pInBuf;
563 	pU4Tmp = pInBuf;
564 	ret = true;
565 
566 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
567 		return false;
568 
569 	switch (setType) {
570 	/*  set some u8 type variables. */
571 	case BTC_SET_BL_BT_DISABLE:
572 		pBtCoexist->btInfo.bBtDisabled = *pu8;
573 		break;
574 
575 	case BTC_SET_BL_BT_TRAFFIC_BUSY:
576 		pBtCoexist->btInfo.bBtBusy = *pu8;
577 		break;
578 
579 	case BTC_SET_BL_BT_LIMITED_DIG:
580 		pBtCoexist->btInfo.bLimitedDig = *pu8;
581 		break;
582 
583 	case BTC_SET_BL_FORCE_TO_ROAM:
584 		pBtCoexist->btInfo.bForceToRoam = *pu8;
585 		break;
586 
587 	case BTC_SET_BL_TO_REJ_AP_AGG_PKT:
588 		pBtCoexist->btInfo.bRejectAggPkt = *pu8;
589 		break;
590 
591 	case BTC_SET_BL_BT_CTRL_AGG_SIZE:
592 		pBtCoexist->btInfo.bBtCtrlAggBufSize = *pu8;
593 		break;
594 
595 	case BTC_SET_BL_INC_SCAN_DEV_NUM:
596 		pBtCoexist->btInfo.bIncreaseScanDevNum = *pu8;
597 		break;
598 
599 	case BTC_SET_BL_BT_TX_RX_MASK:
600 		pBtCoexist->btInfo.bBtTxRxMask = *pu8;
601 		break;
602 
603 	/*  set some u8 type variables. */
604 	case BTC_SET_U1_RSSI_ADJ_VAL_FOR_AGC_TABLE_ON:
605 		pBtCoexist->btInfo.rssiAdjustForAgcTableOn = *pu8;
606 		break;
607 
608 	case BTC_SET_U1_AGG_BUF_SIZE:
609 		pBtCoexist->btInfo.aggBufSize = *pu8;
610 		break;
611 
612 	/*  the following are some action which will be triggered */
613 	case BTC_SET_ACT_GET_BT_RSSI:
614 		ret = false;
615 		break;
616 
617 	case BTC_SET_ACT_AGGREGATE_CTRL:
618 		halbtcoutsrc_AggregationCheck(pBtCoexist);
619 		break;
620 
621 	/* 1Ant =========== */
622 	/*  set some u8 type variables. */
623 	case BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE:
624 		pBtCoexist->btInfo.rssiAdjustFor1AntCoexType = *pu8;
625 		break;
626 
627 	case BTC_SET_U1_LPS_VAL:
628 		pBtCoexist->btInfo.lpsVal = *pu8;
629 		break;
630 
631 	case BTC_SET_U1_RPWM_VAL:
632 		pBtCoexist->btInfo.rpwmVal = *pu8;
633 		break;
634 
635 	/*  the following are some action which will be triggered */
636 	case BTC_SET_ACT_LEAVE_LPS:
637 		halbtcoutsrc_LeaveLps(pBtCoexist);
638 		break;
639 
640 	case BTC_SET_ACT_ENTER_LPS:
641 		halbtcoutsrc_EnterLps(pBtCoexist);
642 		break;
643 
644 	case BTC_SET_ACT_NORMAL_LPS:
645 		halbtcoutsrc_NormalLps(pBtCoexist);
646 		break;
647 
648 	case BTC_SET_ACT_DISABLE_LOW_POWER:
649 		halbtcoutsrc_DisableLowPower(pBtCoexist, *pu8);
650 		break;
651 
652 	case BTC_SET_ACT_UPDATE_RAMASK:
653 		pBtCoexist->btInfo.raMask = *pU4Tmp;
654 
655 		if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE) == true) {
656 			struct sta_info *psta;
657 			struct wlan_bssid_ex *cur_network;
658 
659 			cur_network = &padapter->mlmeextpriv.mlmext_info.network;
660 			psta = rtw_get_stainfo(&padapter->stapriv, cur_network->MacAddress);
661 			rtw_hal_update_ra_mask(psta, 0);
662 		}
663 		break;
664 
665 	case BTC_SET_ACT_SEND_MIMO_PS:
666 		ret = false;
667 		break;
668 
669 	case BTC_SET_ACT_CTRL_BT_INFO:
670 		ret = false;
671 		break;
672 
673 	case BTC_SET_ACT_CTRL_BT_COEX:
674 		ret = false;
675 		break;
676 	case BTC_SET_ACT_CTRL_8723B_ANT:
677 		ret = false;
678 		break;
679 	/*  */
680 	default:
681 		ret = false;
682 		break;
683 	}
684 
685 	return ret;
686 }
687 
688 static void halbtcoutsrc_DisplayFwPwrModeCmd(PBTC_COEXIST pBtCoexist)
689 {
690 	u8 *cliBuf = pBtCoexist->cliBuf;
691 
692 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x %02x ", "Power mode cmd ", \
693 		pBtCoexist->pwrModeVal[0], pBtCoexist->pwrModeVal[1],
694 		pBtCoexist->pwrModeVal[2], pBtCoexist->pwrModeVal[3],
695 		pBtCoexist->pwrModeVal[4], pBtCoexist->pwrModeVal[5]);
696 	CL_PRINTF(cliBuf);
697 }
698 
699 /*  */
700 /* 		IO related function */
701 /*  */
702 static u8 halbtcoutsrc_Read1Byte(void *pBtcContext, u32 RegAddr)
703 {
704 	PBTC_COEXIST pBtCoexist;
705 	struct adapter *padapter;
706 
707 
708 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
709 	padapter = pBtCoexist->Adapter;
710 
711 	return rtw_read8(padapter, RegAddr);
712 }
713 
714 static u16 halbtcoutsrc_Read2Byte(void *pBtcContext, u32 RegAddr)
715 {
716 	PBTC_COEXIST pBtCoexist;
717 	struct adapter *padapter;
718 
719 
720 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
721 	padapter = pBtCoexist->Adapter;
722 
723 	return	rtw_read16(padapter, RegAddr);
724 }
725 
726 static u32 halbtcoutsrc_Read4Byte(void *pBtcContext, u32 RegAddr)
727 {
728 	PBTC_COEXIST pBtCoexist;
729 	struct adapter *padapter;
730 
731 
732 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
733 	padapter = pBtCoexist->Adapter;
734 
735 	return	rtw_read32(padapter, RegAddr);
736 }
737 
738 static void halbtcoutsrc_Write1Byte(void *pBtcContext, u32 RegAddr, u8 Data)
739 {
740 	PBTC_COEXIST pBtCoexist;
741 	struct adapter *padapter;
742 
743 
744 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
745 	padapter = pBtCoexist->Adapter;
746 
747 	rtw_write8(padapter, RegAddr, Data);
748 }
749 
750 static void halbtcoutsrc_BitMaskWrite1Byte(void *pBtcContext, u32 regAddr, u8 bitMask, u8 data1b)
751 {
752 	PBTC_COEXIST pBtCoexist;
753 	struct adapter *padapter;
754 	u8 originalValue, bitShift;
755 	u8 i;
756 
757 
758 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
759 	padapter = pBtCoexist->Adapter;
760 	originalValue = 0;
761 	bitShift = 0;
762 
763 	if (bitMask != 0xFF) {
764 		originalValue = rtw_read8(padapter, regAddr);
765 
766 		for (i = 0; i <= 7; i++) {
767 			if ((bitMask>>i)&0x1)
768 				break;
769 		}
770 		bitShift = i;
771 
772 		data1b = (originalValue & ~bitMask) | ((data1b << bitShift) & bitMask);
773 	}
774 
775 	rtw_write8(padapter, regAddr, data1b);
776 }
777 
778 static void halbtcoutsrc_Write2Byte(void *pBtcContext, u32 RegAddr, u16 Data)
779 {
780 	PBTC_COEXIST pBtCoexist;
781 	struct adapter *padapter;
782 
783 
784 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
785 	padapter = pBtCoexist->Adapter;
786 
787 	rtw_write16(padapter, RegAddr, Data);
788 }
789 
790 static void halbtcoutsrc_Write4Byte(void *pBtcContext, u32 RegAddr, u32 Data)
791 {
792 	PBTC_COEXIST pBtCoexist;
793 	struct adapter *padapter;
794 
795 
796 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
797 	padapter = pBtCoexist->Adapter;
798 
799 	rtw_write32(padapter, RegAddr, Data);
800 }
801 
802 static void halbtcoutsrc_WriteLocalReg1Byte(void *pBtcContext, u32 RegAddr, u8 Data)
803 {
804 	PBTC_COEXIST		pBtCoexist = (PBTC_COEXIST)pBtcContext;
805 	struct adapter *Adapter = pBtCoexist->Adapter;
806 
807 	if (BTC_INTF_SDIO == pBtCoexist->chipInterface)
808 		rtw_write8(Adapter, SDIO_LOCAL_BASE | RegAddr, Data);
809 	else
810 		rtw_write8(Adapter, RegAddr, Data);
811 }
812 
813 static void halbtcoutsrc_SetBbReg(void *pBtcContext, u32 RegAddr, u32 BitMask, u32 Data)
814 {
815 	PBTC_COEXIST pBtCoexist;
816 	struct adapter *padapter;
817 
818 
819 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
820 	padapter = pBtCoexist->Adapter;
821 
822 	PHY_SetBBReg(padapter, RegAddr, BitMask, Data);
823 }
824 
825 
826 static u32 halbtcoutsrc_GetBbReg(void *pBtcContext, u32 RegAddr, u32 BitMask)
827 {
828 	PBTC_COEXIST pBtCoexist;
829 	struct adapter *padapter;
830 
831 
832 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
833 	padapter = pBtCoexist->Adapter;
834 
835 	return PHY_QueryBBReg(padapter, RegAddr, BitMask);
836 }
837 
838 static void halbtcoutsrc_SetRfReg(void *pBtcContext, u8 eRFPath, u32 RegAddr, u32 BitMask, u32 Data)
839 {
840 	PBTC_COEXIST pBtCoexist;
841 	struct adapter *padapter;
842 
843 
844 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
845 	padapter = pBtCoexist->Adapter;
846 
847 	PHY_SetRFReg(padapter, eRFPath, RegAddr, BitMask, Data);
848 }
849 
850 static u32 halbtcoutsrc_GetRfReg(void *pBtcContext, u8 eRFPath, u32 RegAddr, u32 BitMask)
851 {
852 	PBTC_COEXIST pBtCoexist;
853 	struct adapter *padapter;
854 
855 
856 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
857 	padapter = pBtCoexist->Adapter;
858 
859 	return PHY_QueryRFReg(padapter, eRFPath, RegAddr, BitMask);
860 }
861 
862 static void halbtcoutsrc_SetBtReg(void *pBtcContext, u8 RegType, u32 RegAddr, u32 Data)
863 {
864 	PBTC_COEXIST pBtCoexist;
865 	struct adapter *padapter;
866 	u8 CmdBuffer1[4] = {0};
867 	u8 CmdBuffer2[4] = {0};
868 	u8 *AddrToSet = (u8 *)&RegAddr;
869 	u8 *ValueToSet = (u8 *)&Data;
870 	u8 OperVer = 0;
871 	u8 ReqNum = 0;
872 
873 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
874 	padapter = pBtCoexist->Adapter;
875 
876 	CmdBuffer1[0] |= (OperVer & 0x0f);						/* Set OperVer */
877 	CmdBuffer1[0] |= ((ReqNum << 4) & 0xf0);				/* Set ReqNum */
878 	CmdBuffer1[1] = 0x0d;									/* Set OpCode to BT_LO_OP_WRITE_REG_VALUE */
879 	CmdBuffer1[2] = ValueToSet[0];							/* Set WriteRegValue */
880 	rtw_hal_fill_h2c_cmd(padapter, 0x67, 4, &(CmdBuffer1[0]));
881 
882 	msleep(200);
883 	ReqNum++;
884 
885 	CmdBuffer2[0] |= (OperVer & 0x0f);						/* Set OperVer */
886 	CmdBuffer2[0] |= ((ReqNum << 4) & 0xf0);				/* Set ReqNum */
887 	CmdBuffer2[1] = 0x0c;									/* Set OpCode of BT_LO_OP_WRITE_REG_ADDR */
888 	CmdBuffer2[3] = AddrToSet[0];							/* Set WriteRegAddr */
889 	rtw_hal_fill_h2c_cmd(padapter, 0x67, 4, &(CmdBuffer2[0]));
890 }
891 
892 static u32 halbtcoutsrc_GetBtReg(void *pBtcContext, u8 RegType, u32 RegAddr)
893 {
894 	/* To be implemented. Always return 0 temporarily */
895 	return 0;
896 }
897 
898 static void halbtcoutsrc_FillH2cCmd(void *pBtcContext, u8 elementId, u32 cmdLen, u8 *pCmdBuffer)
899 {
900 	PBTC_COEXIST pBtCoexist;
901 	struct adapter *padapter;
902 
903 
904 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
905 	padapter = pBtCoexist->Adapter;
906 
907 	rtw_hal_fill_h2c_cmd(padapter, elementId, cmdLen, pCmdBuffer);
908 }
909 
910 static void halbtcoutsrc_DisplayDbgMsg(void *pBtcContext, u8 dispType)
911 {
912 	PBTC_COEXIST pBtCoexist;
913 
914 
915 	pBtCoexist = (PBTC_COEXIST)pBtcContext;
916 	switch (dispType) {
917 	case BTC_DBG_DISP_COEX_STATISTICS:
918 		break;
919 	case BTC_DBG_DISP_BT_LINK_INFO:
920 		break;
921 	case BTC_DBG_DISP_FW_PWR_MODE_CMD:
922 		halbtcoutsrc_DisplayFwPwrModeCmd(pBtCoexist);
923 		break;
924 	default:
925 		break;
926 	}
927 }
928 
929 /*  */
930 /* 		Extern functions called by other module */
931 /*  */
932 static u8 EXhalbtcoutsrc_BindBtCoexWithAdapter(void *padapter)
933 {
934 	PBTC_COEXIST		pBtCoexist = &GLBtCoexist;
935 
936 	if (pBtCoexist->bBinded)
937 		return false;
938 	else
939 		pBtCoexist->bBinded = true;
940 
941 	pBtCoexist->statistics.cntBind++;
942 
943 	pBtCoexist->Adapter = padapter;
944 
945 	pBtCoexist->stackInfo.bProfileNotified = false;
946 
947 	pBtCoexist->btInfo.bBtCtrlAggBufSize = false;
948 	pBtCoexist->btInfo.aggBufSize = 5;
949 
950 	pBtCoexist->btInfo.bIncreaseScanDevNum = false;
951 
952 	/*  set default antenna position to main  port */
953 	pBtCoexist->boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT;
954 
955 	return true;
956 }
957 
958 void hal_btcoex_Initialize(void *padapter)
959 {
960 	PBTC_COEXIST pBtCoexist;
961 
962 	memset(&GLBtCoexist, 0, sizeof(GLBtCoexist));
963 
964 	pBtCoexist = &GLBtCoexist;
965 
966 	/* pBtCoexist->statistics.cntBind++; */
967 
968 	halbtcoutsrc_DbgInit();
969 
970 	pBtCoexist->chipInterface = BTC_INTF_SDIO;
971 
972 	EXhalbtcoutsrc_BindBtCoexWithAdapter(padapter);
973 
974 	pBtCoexist->fBtcRead1Byte = halbtcoutsrc_Read1Byte;
975 	pBtCoexist->fBtcWrite1Byte = halbtcoutsrc_Write1Byte;
976 	pBtCoexist->fBtcWrite1ByteBitMask = halbtcoutsrc_BitMaskWrite1Byte;
977 	pBtCoexist->fBtcRead2Byte = halbtcoutsrc_Read2Byte;
978 	pBtCoexist->fBtcWrite2Byte = halbtcoutsrc_Write2Byte;
979 	pBtCoexist->fBtcRead4Byte = halbtcoutsrc_Read4Byte;
980 	pBtCoexist->fBtcWrite4Byte = halbtcoutsrc_Write4Byte;
981 	pBtCoexist->fBtcWriteLocalReg1Byte = halbtcoutsrc_WriteLocalReg1Byte;
982 
983 	pBtCoexist->fBtcSetBbReg = halbtcoutsrc_SetBbReg;
984 	pBtCoexist->fBtcGetBbReg = halbtcoutsrc_GetBbReg;
985 
986 	pBtCoexist->fBtcSetRfReg = halbtcoutsrc_SetRfReg;
987 	pBtCoexist->fBtcGetRfReg = halbtcoutsrc_GetRfReg;
988 
989 	pBtCoexist->fBtcFillH2c = halbtcoutsrc_FillH2cCmd;
990 	pBtCoexist->fBtcDispDbgMsg = halbtcoutsrc_DisplayDbgMsg;
991 
992 	pBtCoexist->fBtcGet = halbtcoutsrc_Get;
993 	pBtCoexist->fBtcSet = halbtcoutsrc_Set;
994 	pBtCoexist->fBtcGetBtReg = halbtcoutsrc_GetBtReg;
995 	pBtCoexist->fBtcSetBtReg = halbtcoutsrc_SetBtReg;
996 
997 	pBtCoexist->cliBuf = &GLBtcDbgBuf[0];
998 
999 	pBtCoexist->boardInfo.singleAntPath = 0;
1000 
1001 	GLBtcWiFiInScanState = false;
1002 
1003 	GLBtcWiFiInIQKState = false;
1004 }
1005 
1006 void EXhalbtcoutsrc_PowerOnSetting(PBTC_COEXIST pBtCoexist)
1007 {
1008 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1009 		return;
1010 
1011 	/* Power on setting function is only added in 8723B currently */
1012 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1013 		EXhalbtc8723b2ant_PowerOnSetting(pBtCoexist);
1014 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1015 		EXhalbtc8723b1ant_PowerOnSetting(pBtCoexist);
1016 }
1017 
1018 void EXhalbtcoutsrc_InitHwConfig(PBTC_COEXIST pBtCoexist, u8 bWifiOnly)
1019 {
1020 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1021 		return;
1022 
1023 	pBtCoexist->statistics.cntInitHwConfig++;
1024 
1025 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1026 		EXhalbtc8723b2ant_InitHwConfig(pBtCoexist, bWifiOnly);
1027 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1028 		EXhalbtc8723b1ant_InitHwConfig(pBtCoexist, bWifiOnly);
1029 }
1030 
1031 void EXhalbtcoutsrc_InitCoexDm(PBTC_COEXIST pBtCoexist)
1032 {
1033 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1034 		return;
1035 
1036 	pBtCoexist->statistics.cntInitCoexDm++;
1037 
1038 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1039 		EXhalbtc8723b2ant_InitCoexDm(pBtCoexist);
1040 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1041 		EXhalbtc8723b1ant_InitCoexDm(pBtCoexist);
1042 
1043 	pBtCoexist->bInitilized = true;
1044 }
1045 
1046 void EXhalbtcoutsrc_IpsNotify(PBTC_COEXIST pBtCoexist, u8 type)
1047 {
1048 	u8 ipsType;
1049 
1050 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1051 		return;
1052 
1053 	pBtCoexist->statistics.cntIpsNotify++;
1054 	if (pBtCoexist->bManualControl)
1055 		return;
1056 
1057 	if (IPS_NONE == type)
1058 		ipsType = BTC_IPS_LEAVE;
1059 	else
1060 		ipsType = BTC_IPS_ENTER;
1061 
1062 	/*  All notify is called in cmd thread, don't need to leave low power again */
1063 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1064 
1065 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1066 		EXhalbtc8723b2ant_IpsNotify(pBtCoexist, ipsType);
1067 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1068 		EXhalbtc8723b1ant_IpsNotify(pBtCoexist, ipsType);
1069 
1070 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1071 }
1072 
1073 void EXhalbtcoutsrc_LpsNotify(PBTC_COEXIST pBtCoexist, u8 type)
1074 {
1075 	u8 lpsType;
1076 
1077 
1078 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1079 		return;
1080 
1081 	pBtCoexist->statistics.cntLpsNotify++;
1082 	if (pBtCoexist->bManualControl)
1083 		return;
1084 
1085 	if (PS_MODE_ACTIVE == type)
1086 		lpsType = BTC_LPS_DISABLE;
1087 	else
1088 		lpsType = BTC_LPS_ENABLE;
1089 
1090 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1091 		EXhalbtc8723b2ant_LpsNotify(pBtCoexist, lpsType);
1092 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1093 		EXhalbtc8723b1ant_LpsNotify(pBtCoexist, lpsType);
1094 }
1095 
1096 void EXhalbtcoutsrc_ScanNotify(PBTC_COEXIST pBtCoexist, u8 type)
1097 {
1098 	u8 scanType;
1099 
1100 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1101 		return;
1102 	pBtCoexist->statistics.cntScanNotify++;
1103 	if (pBtCoexist->bManualControl)
1104 		return;
1105 
1106 	if (type) {
1107 		scanType = BTC_SCAN_START;
1108 		GLBtcWiFiInScanState = true;
1109 	} else {
1110 		scanType = BTC_SCAN_FINISH;
1111 		GLBtcWiFiInScanState = false;
1112 	}
1113 
1114 	/*  All notify is called in cmd thread, don't need to leave low power again */
1115 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1116 
1117 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1118 		EXhalbtc8723b2ant_ScanNotify(pBtCoexist, scanType);
1119 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1120 		EXhalbtc8723b1ant_ScanNotify(pBtCoexist, scanType);
1121 
1122 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1123 }
1124 
1125 void EXhalbtcoutsrc_ConnectNotify(PBTC_COEXIST pBtCoexist, u8 action)
1126 {
1127 	u8 assoType;
1128 
1129 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1130 		return;
1131 	pBtCoexist->statistics.cntConnectNotify++;
1132 	if (pBtCoexist->bManualControl)
1133 		return;
1134 
1135 	if (action)
1136 		assoType = BTC_ASSOCIATE_START;
1137 	else
1138 		assoType = BTC_ASSOCIATE_FINISH;
1139 
1140 	/*  All notify is called in cmd thread, don't need to leave low power again */
1141 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1142 
1143 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1144 		EXhalbtc8723b2ant_ConnectNotify(pBtCoexist, assoType);
1145 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1146 		EXhalbtc8723b1ant_ConnectNotify(pBtCoexist, assoType);
1147 
1148 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1149 }
1150 
1151 void EXhalbtcoutsrc_MediaStatusNotify(PBTC_COEXIST pBtCoexist, RT_MEDIA_STATUS mediaStatus)
1152 {
1153 	u8 mStatus;
1154 
1155 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1156 		return;
1157 
1158 	pBtCoexist->statistics.cntMediaStatusNotify++;
1159 	if (pBtCoexist->bManualControl)
1160 		return;
1161 
1162 	if (RT_MEDIA_CONNECT == mediaStatus)
1163 		mStatus = BTC_MEDIA_CONNECT;
1164 	else
1165 		mStatus = BTC_MEDIA_DISCONNECT;
1166 
1167 	/*  All notify is called in cmd thread, don't need to leave low power again */
1168 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1169 
1170 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1171 		EXhalbtc8723b2ant_MediaStatusNotify(pBtCoexist, mStatus);
1172 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1173 		EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, mStatus);
1174 
1175 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1176 }
1177 
1178 void EXhalbtcoutsrc_SpecialPacketNotify(PBTC_COEXIST pBtCoexist, u8 pktType)
1179 {
1180 	u8 packetType;
1181 
1182 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1183 		return;
1184 	pBtCoexist->statistics.cntSpecialPacketNotify++;
1185 	if (pBtCoexist->bManualControl)
1186 		return;
1187 
1188 	if (PACKET_DHCP == pktType) {
1189 		packetType = BTC_PACKET_DHCP;
1190 	} else if (PACKET_EAPOL == pktType) {
1191 		packetType = BTC_PACKET_EAPOL;
1192 	} else if (PACKET_ARP == pktType) {
1193 		packetType = BTC_PACKET_ARP;
1194 	} else {
1195 		return;
1196 	}
1197 
1198 	/*  All notify is called in cmd thread, don't need to leave low power again */
1199 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1200 
1201 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1202 		EXhalbtc8723b2ant_SpecialPacketNotify(pBtCoexist, packetType);
1203 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1204 		EXhalbtc8723b1ant_SpecialPacketNotify(pBtCoexist, packetType);
1205 
1206 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1207 }
1208 
1209 void EXhalbtcoutsrc_BtInfoNotify(PBTC_COEXIST pBtCoexist, u8 *tmpBuf, u8 length)
1210 {
1211 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1212 		return;
1213 
1214 	pBtCoexist->statistics.cntBtInfoNotify++;
1215 
1216 	/*  All notify is called in cmd thread, don't need to leave low power again */
1217 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1218 
1219 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1220 		EXhalbtc8723b2ant_BtInfoNotify(pBtCoexist, tmpBuf, length);
1221 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1222 		EXhalbtc8723b1ant_BtInfoNotify(pBtCoexist, tmpBuf, length);
1223 
1224 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1225 }
1226 
1227 void EXhalbtcoutsrc_HaltNotify(PBTC_COEXIST pBtCoexist)
1228 {
1229 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1230 		return;
1231 
1232 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1233 		EXhalbtc8723b2ant_HaltNotify(pBtCoexist);
1234 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1235 		EXhalbtc8723b1ant_HaltNotify(pBtCoexist);
1236 
1237 	pBtCoexist->bBinded = false;
1238 }
1239 
1240 void EXhalbtcoutsrc_PnpNotify(PBTC_COEXIST pBtCoexist, u8 pnpState)
1241 {
1242 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1243 		return;
1244 
1245 	/*  */
1246 	/*  currently only 1ant we have to do the notification, */
1247 	/*  once pnp is notified to sleep state, we have to leave LPS that we can sleep normally. */
1248 	/*  */
1249 
1250 	if (pBtCoexist->boardInfo.btdmAntNum == 1)
1251 		EXhalbtc8723b1ant_PnpNotify(pBtCoexist, pnpState);
1252 	else if (pBtCoexist->boardInfo.btdmAntNum == 2)
1253 		EXhalbtc8723b2ant_PnpNotify(pBtCoexist, pnpState);
1254 }
1255 
1256 void EXhalbtcoutsrc_Periodical(PBTC_COEXIST pBtCoexist)
1257 {
1258 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1259 		return;
1260 	pBtCoexist->statistics.cntPeriodical++;
1261 
1262 	/*  Periodical should be called in cmd thread, */
1263 	/*  don't need to leave low power again */
1264 /* 	halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1265 
1266 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1267 		EXhalbtc8723b2ant_Periodical(pBtCoexist);
1268 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1269 		EXhalbtc8723b1ant_Periodical(pBtCoexist);
1270 
1271 /* 	halbtcoutsrc_NormalLowPower(pBtCoexist); */
1272 }
1273 
1274 void EXhalbtcoutsrc_SetChipType(u8 chipType)
1275 {
1276 	GLBtCoexist.boardInfo.btChipType = BTC_CHIP_RTL8723B;
1277 }
1278 
1279 void EXhalbtcoutsrc_SetAntNum(u8 type, u8 antNum)
1280 {
1281 	if (BT_COEX_ANT_TYPE_PG == type) {
1282 		GLBtCoexist.boardInfo.pgAntNum = antNum;
1283 		GLBtCoexist.boardInfo.btdmAntNum = antNum;
1284 	} else if (BT_COEX_ANT_TYPE_ANTDIV == type) {
1285 		GLBtCoexist.boardInfo.btdmAntNum = antNum;
1286 		/* GLBtCoexist.boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT; */
1287 	} else if (BT_COEX_ANT_TYPE_DETECTED == type) {
1288 		GLBtCoexist.boardInfo.btdmAntNum = antNum;
1289 		/* GLBtCoexist.boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT; */
1290 	}
1291 }
1292 
1293 /*  */
1294 /*  Currently used by 8723b only, S0 or S1 */
1295 /*  */
1296 void EXhalbtcoutsrc_SetSingleAntPath(u8 singleAntPath)
1297 {
1298 	GLBtCoexist.boardInfo.singleAntPath = singleAntPath;
1299 }
1300 
1301 void EXhalbtcoutsrc_DisplayBtCoexInfo(PBTC_COEXIST pBtCoexist)
1302 {
1303 	if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1304 		return;
1305 
1306 	halbtcoutsrc_LeaveLowPower(pBtCoexist);
1307 
1308 	if (pBtCoexist->boardInfo.btdmAntNum == 2)
1309 		EXhalbtc8723b2ant_DisplayCoexInfo(pBtCoexist);
1310 	else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1311 		EXhalbtc8723b1ant_DisplayCoexInfo(pBtCoexist);
1312 
1313 	halbtcoutsrc_NormalLowPower(pBtCoexist);
1314 }
1315 
1316 /*
1317  * Description:
1318  *Run BT-Coexist mechansim or not
1319  *
1320  */
1321 void hal_btcoex_SetBTCoexist(struct adapter *padapter, u8 bBtExist)
1322 {
1323 	struct hal_com_data *pHalData;
1324 
1325 
1326 	pHalData = GET_HAL_DATA(padapter);
1327 	pHalData->bt_coexist.bBtExist = bBtExist;
1328 }
1329 
1330 /*
1331  * Dewcription:
1332  *Check is co-exist mechanism enabled or not
1333  *
1334  * Return:
1335  *true	Enable BT co-exist mechanism
1336  *false	Disable BT co-exist mechanism
1337  */
1338 bool hal_btcoex_IsBtExist(struct adapter *padapter)
1339 {
1340 	struct hal_com_data *pHalData;
1341 
1342 
1343 	pHalData = GET_HAL_DATA(padapter);
1344 	return pHalData->bt_coexist.bBtExist;
1345 }
1346 
1347 bool hal_btcoex_IsBtDisabled(struct adapter *padapter)
1348 {
1349 	if (!hal_btcoex_IsBtExist(padapter))
1350 		return true;
1351 
1352 	if (GLBtCoexist.btInfo.bBtDisabled)
1353 		return true;
1354 	else
1355 		return false;
1356 }
1357 
1358 void hal_btcoex_SetChipType(struct adapter *padapter, u8 chipType)
1359 {
1360 	struct hal_com_data *pHalData;
1361 
1362 
1363 	pHalData = GET_HAL_DATA(padapter);
1364 	pHalData->bt_coexist.btChipType = chipType;
1365 
1366 	EXhalbtcoutsrc_SetChipType(chipType);
1367 }
1368 
1369 void hal_btcoex_SetPgAntNum(struct adapter *padapter, u8 antNum)
1370 {
1371 	struct hal_com_data *pHalData;
1372 
1373 
1374 	pHalData = GET_HAL_DATA(padapter);
1375 
1376 	pHalData->bt_coexist.btTotalAntNum = antNum;
1377 	EXhalbtcoutsrc_SetAntNum(BT_COEX_ANT_TYPE_PG, antNum);
1378 }
1379 
1380 void hal_btcoex_SetSingleAntPath(struct adapter *padapter, u8 singleAntPath)
1381 {
1382 	EXhalbtcoutsrc_SetSingleAntPath(singleAntPath);
1383 }
1384 
1385 void hal_btcoex_PowerOnSetting(struct adapter *padapter)
1386 {
1387 	EXhalbtcoutsrc_PowerOnSetting(&GLBtCoexist);
1388 }
1389 
1390 void hal_btcoex_InitHwConfig(struct adapter *padapter, u8 bWifiOnly)
1391 {
1392 	if (!hal_btcoex_IsBtExist(padapter))
1393 		return;
1394 
1395 	EXhalbtcoutsrc_InitHwConfig(&GLBtCoexist, bWifiOnly);
1396 	EXhalbtcoutsrc_InitCoexDm(&GLBtCoexist);
1397 }
1398 
1399 void hal_btcoex_IpsNotify(struct adapter *padapter, u8 type)
1400 {
1401 	EXhalbtcoutsrc_IpsNotify(&GLBtCoexist, type);
1402 }
1403 
1404 void hal_btcoex_LpsNotify(struct adapter *padapter, u8 type)
1405 {
1406 	EXhalbtcoutsrc_LpsNotify(&GLBtCoexist, type);
1407 }
1408 
1409 void hal_btcoex_ScanNotify(struct adapter *padapter, u8 type)
1410 {
1411 	EXhalbtcoutsrc_ScanNotify(&GLBtCoexist, type);
1412 }
1413 
1414 void hal_btcoex_ConnectNotify(struct adapter *padapter, u8 action)
1415 {
1416 	EXhalbtcoutsrc_ConnectNotify(&GLBtCoexist, action);
1417 }
1418 
1419 void hal_btcoex_MediaStatusNotify(struct adapter *padapter, u8 mediaStatus)
1420 {
1421 	EXhalbtcoutsrc_MediaStatusNotify(&GLBtCoexist, mediaStatus);
1422 }
1423 
1424 void hal_btcoex_SpecialPacketNotify(struct adapter *padapter, u8 pktType)
1425 {
1426 	EXhalbtcoutsrc_SpecialPacketNotify(&GLBtCoexist, pktType);
1427 }
1428 
1429 void hal_btcoex_IQKNotify(struct adapter *padapter, u8 state)
1430 {
1431 	GLBtcWiFiInIQKState = state;
1432 }
1433 
1434 void hal_btcoex_BtInfoNotify(struct adapter *padapter, u8 length, u8 *tmpBuf)
1435 {
1436 	if (GLBtcWiFiInIQKState)
1437 		return;
1438 
1439 	EXhalbtcoutsrc_BtInfoNotify(&GLBtCoexist, tmpBuf, length);
1440 }
1441 
1442 void hal_btcoex_SuspendNotify(struct adapter *padapter, u8 state)
1443 {
1444 	if (state == 1)
1445 		state = BTC_WIFI_PNP_SLEEP;
1446 	else
1447 		state = BTC_WIFI_PNP_WAKE_UP;
1448 
1449 	EXhalbtcoutsrc_PnpNotify(&GLBtCoexist, state);
1450 }
1451 
1452 void hal_btcoex_HaltNotify(struct adapter *padapter)
1453 {
1454 	EXhalbtcoutsrc_HaltNotify(&GLBtCoexist);
1455 }
1456 
1457 void hal_btcoex_Handler(struct adapter *padapter)
1458 {
1459 	EXhalbtcoutsrc_Periodical(&GLBtCoexist);
1460 }
1461 
1462 s32 hal_btcoex_IsBTCoexCtrlAMPDUSize(struct adapter *padapter)
1463 {
1464 	return (s32)GLBtCoexist.btInfo.bBtCtrlAggBufSize;
1465 }
1466 
1467 void hal_btcoex_SetManualControl(struct adapter *padapter, u8 bmanual)
1468 {
1469 	GLBtCoexist.bManualControl = bmanual;
1470 }
1471 
1472 bool hal_btcoex_IsBtControlLps(struct adapter *padapter)
1473 {
1474 	if (!hal_btcoex_IsBtExist(padapter))
1475 		return false;
1476 
1477 	if (GLBtCoexist.btInfo.bBtDisabled)
1478 		return false;
1479 
1480 	if (GLBtCoexist.btInfo.bBtCtrlLps)
1481 		return true;
1482 
1483 	return false;
1484 }
1485 
1486 bool hal_btcoex_IsLpsOn(struct adapter *padapter)
1487 {
1488 	if (!hal_btcoex_IsBtExist(padapter))
1489 		return false;
1490 
1491 	if (GLBtCoexist.btInfo.bBtDisabled)
1492 		return false;
1493 
1494 	if (GLBtCoexist.btInfo.bBtLpsOn)
1495 		return true;
1496 
1497 	return false;
1498 }
1499 
1500 u8 hal_btcoex_RpwmVal(struct adapter *padapter)
1501 {
1502 	return GLBtCoexist.btInfo.rpwmVal;
1503 }
1504 
1505 u8 hal_btcoex_LpsVal(struct adapter *padapter)
1506 {
1507 	return GLBtCoexist.btInfo.lpsVal;
1508 }
1509 
1510 u32 hal_btcoex_GetRaMask(struct adapter *padapter)
1511 {
1512 	if (!hal_btcoex_IsBtExist(padapter))
1513 		return 0;
1514 
1515 	if (GLBtCoexist.btInfo.bBtDisabled)
1516 		return 0;
1517 
1518 	if (GLBtCoexist.boardInfo.btdmAntNum != 1)
1519 		return 0;
1520 
1521 	return GLBtCoexist.btInfo.raMask;
1522 }
1523 
1524 void hal_btcoex_RecordPwrMode(struct adapter *padapter, u8 *pCmdBuf, u8 cmdLen)
1525 {
1526 	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC, ("[BTCoex], FW write pwrModeCmd = 0x%04x%08x\n",
1527 		pCmdBuf[0]<<8|pCmdBuf[1],
1528 		pCmdBuf[2]<<24|pCmdBuf[3]<<16|pCmdBuf[4]<<8|pCmdBuf[5]));
1529 
1530 	memcpy(GLBtCoexist.pwrModeVal, pCmdBuf, cmdLen);
1531 }
1532 
1533 void hal_btcoex_DisplayBtCoexInfo(struct adapter *padapter, u8 *pbuf, u32 bufsize)
1534 {
1535 	PBTCDBGINFO pinfo;
1536 
1537 
1538 	pinfo = &GLBtcDbgInfo;
1539 	DBG_BT_INFO_INIT(pinfo, pbuf, bufsize);
1540 	EXhalbtcoutsrc_DisplayBtCoexInfo(&GLBtCoexist);
1541 	DBG_BT_INFO_INIT(pinfo, NULL, 0);
1542 }
1543 
1544 void hal_btcoex_SetDBG(struct adapter *padapter, u32 *pDbgModule)
1545 {
1546 	u32 i;
1547 
1548 
1549 	if (!pDbgModule)
1550 		return;
1551 
1552 	for (i = 0; i < BTC_MSG_MAX; i++)
1553 		GLBtcDbgType[i] = pDbgModule[i];
1554 }
1555 
1556 u32 hal_btcoex_GetDBG(struct adapter *padapter, u8 *pStrBuf, u32 bufSize)
1557 {
1558 	s32 count;
1559 	u8 *pstr;
1560 	u32 leftSize;
1561 
1562 
1563 	if (!pStrBuf || bufSize == 0)
1564 		return 0;
1565 
1566 	pstr = pStrBuf;
1567 	leftSize = bufSize;
1568 /* 	DBG_871X(FUNC_ADPT_FMT ": bufsize =%d\n", FUNC_ADPT_ARG(padapter), bufSize); */
1569 
1570 	count = rtw_sprintf(pstr, leftSize, "#define DBG\t%d\n", DBG);
1571 	if ((count < 0) || (count >= leftSize))
1572 		goto exit;
1573 	pstr += count;
1574 	leftSize -= count;
1575 
1576 	count = rtw_sprintf(pstr, leftSize, "BTCOEX Debug Setting:\n");
1577 	if ((count < 0) || (count >= leftSize))
1578 		goto exit;
1579 	pstr += count;
1580 	leftSize -= count;
1581 
1582 	count = rtw_sprintf(pstr, leftSize,
1583 		"INTERFACE / ALGORITHM: 0x%08X / 0x%08X\n\n",
1584 		GLBtcDbgType[BTC_MSG_INTERFACE],
1585 		GLBtcDbgType[BTC_MSG_ALGORITHM]);
1586 	if ((count < 0) || (count >= leftSize))
1587 		goto exit;
1588 	pstr += count;
1589 	leftSize -= count;
1590 
1591 	count = rtw_sprintf(pstr, leftSize, "INTERFACE Debug Setting Definition:\n");
1592 	if ((count < 0) || (count >= leftSize))
1593 		goto exit;
1594 	pstr += count;
1595 	leftSize -= count;
1596 	count = rtw_sprintf(pstr, leftSize, "\tbit[0]=%d for INTF_INIT\n",
1597 		(GLBtcDbgType[BTC_MSG_INTERFACE]&INTF_INIT)?1:0);
1598 	if ((count < 0) || (count >= leftSize))
1599 		goto exit;
1600 	pstr += count;
1601 	leftSize -= count;
1602 	count = rtw_sprintf(pstr, leftSize, "\tbit[2]=%d for INTF_NOTIFY\n\n",
1603 		(GLBtcDbgType[BTC_MSG_INTERFACE]&INTF_NOTIFY)?1:0);
1604 	if ((count < 0) || (count >= leftSize))
1605 		goto exit;
1606 	pstr += count;
1607 	leftSize -= count;
1608 
1609 	count = rtw_sprintf(pstr, leftSize, "ALGORITHM Debug Setting Definition:\n");
1610 	if ((count < 0) || (count >= leftSize))
1611 		goto exit;
1612 	pstr += count;
1613 	leftSize -= count;
1614 	count = rtw_sprintf(pstr, leftSize, "\tbit[0]=%d for BT_RSSI_STATE\n",
1615 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_BT_RSSI_STATE)?1:0);
1616 	if ((count < 0) || (count >= leftSize))
1617 		goto exit;
1618 	pstr += count;
1619 	leftSize -= count;
1620 	count = rtw_sprintf(pstr, leftSize, "\tbit[1]=%d for WIFI_RSSI_STATE\n",
1621 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_WIFI_RSSI_STATE)?1:0);
1622 	if ((count < 0) || (count >= leftSize))
1623 		goto exit;
1624 	pstr += count;
1625 	leftSize -= count;
1626 	count = rtw_sprintf(pstr, leftSize, "\tbit[2]=%d for BT_MONITOR\n",
1627 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_BT_MONITOR)?1:0);
1628 	if ((count < 0) || (count >= leftSize))
1629 		goto exit;
1630 	pstr += count;
1631 	leftSize -= count;
1632 	count = rtw_sprintf(pstr, leftSize, "\tbit[3]=%d for TRACE\n",
1633 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE)?1:0);
1634 	if ((count < 0) || (count >= leftSize))
1635 		goto exit;
1636 	pstr += count;
1637 	leftSize -= count;
1638 	count = rtw_sprintf(pstr, leftSize, "\tbit[4]=%d for TRACE_FW\n",
1639 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_FW)?1:0);
1640 	if ((count < 0) || (count >= leftSize))
1641 		goto exit;
1642 	pstr += count;
1643 	leftSize -= count;
1644 	count = rtw_sprintf(pstr, leftSize, "\tbit[5]=%d for TRACE_FW_DETAIL\n",
1645 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_FW_DETAIL)?1:0);
1646 	if ((count < 0) || (count >= leftSize))
1647 		goto exit;
1648 	pstr += count;
1649 	leftSize -= count;
1650 	count = rtw_sprintf(pstr, leftSize, "\tbit[6]=%d for TRACE_FW_EXEC\n",
1651 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_FW_EXEC)?1:0);
1652 	if ((count < 0) || (count >= leftSize))
1653 		goto exit;
1654 	pstr += count;
1655 	leftSize -= count;
1656 	count = rtw_sprintf(pstr, leftSize, "\tbit[7]=%d for TRACE_SW\n",
1657 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_SW)?1:0);
1658 	if ((count < 0) || (count >= leftSize))
1659 		goto exit;
1660 	pstr += count;
1661 	leftSize -= count;
1662 	count = rtw_sprintf(pstr, leftSize, "\tbit[8]=%d for TRACE_SW_DETAIL\n",
1663 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_SW_DETAIL)?1:0);
1664 	if ((count < 0) || (count >= leftSize))
1665 		goto exit;
1666 	pstr += count;
1667 	leftSize -= count;
1668 	count = rtw_sprintf(pstr, leftSize, "\tbit[9]=%d for TRACE_SW_EXEC\n",
1669 		(GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_SW_EXEC)?1:0);
1670 	if ((count < 0) || (count >= leftSize))
1671 		goto exit;
1672 	pstr += count;
1673 	leftSize -= count;
1674 
1675 exit:
1676 	count = pstr - pStrBuf;
1677 /* 	DBG_871X(FUNC_ADPT_FMT ": usedsize =%d\n", FUNC_ADPT_ARG(padapter), count); */
1678 
1679 	return count;
1680 }
1681