1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include "Mp_Precomp.h"
9 
10 /*  Global variables, these are static variables */
11 static struct coex_dm_8723b_1ant GLCoexDm8723b1Ant;
12 static struct coex_dm_8723b_1ant *pCoexDm = &GLCoexDm8723b1Ant;
13 static struct coex_sta_8723b_1ant GLCoexSta8723b1Ant;
14 static struct coex_sta_8723b_1ant *pCoexSta = &GLCoexSta8723b1Ant;
15 
16 static const char *const GLBtInfoSrc8723b1Ant[] = {
17 	"BT Info[wifi fw]",
18 	"BT Info[bt rsp]",
19 	"BT Info[bt auto report]",
20 };
21 
22 static u32 GLCoexVerDate8723b1Ant = 20140507;
23 static u32 GLCoexVer8723b1Ant = 0x4e;
24 
25 /*  local function proto type if needed */
26 /*  local function start with halbtc8723b1ant_ */
27 static u8 halbtc8723b1ant_BtRssiState(
28 	u8 levelNum, u8 rssiThresh, u8 rssiThresh1
29 )
30 {
31 	s32 btRssi = 0;
32 	u8 btRssiState = pCoexSta->preBtRssiState;
33 
34 	btRssi = pCoexSta->btRssi;
35 
36 	if (levelNum == 2) {
37 		if (
38 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_LOW) ||
39 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_STAY_LOW)
40 		) {
41 			if (btRssi >= (rssiThresh + BTC_RSSI_COEX_THRESH_TOL_8723B_1ANT)) {
42 
43 				btRssiState = BTC_RSSI_STATE_HIGH;
44 				BTC_PRINT(
45 					BTC_MSG_ALGORITHM,
46 					ALGO_BT_RSSI_STATE,
47 					("[BTCoex], BT Rssi state switch to High\n")
48 				);
49 			} else {
50 				btRssiState = BTC_RSSI_STATE_STAY_LOW;
51 				BTC_PRINT(
52 					BTC_MSG_ALGORITHM,
53 					ALGO_BT_RSSI_STATE,
54 					("[BTCoex], BT Rssi state stay at Low\n")
55 				);
56 			}
57 		} else {
58 			if (btRssi < rssiThresh) {
59 				btRssiState = BTC_RSSI_STATE_LOW;
60 				BTC_PRINT(
61 					BTC_MSG_ALGORITHM,
62 					ALGO_BT_RSSI_STATE,
63 					("[BTCoex], BT Rssi state switch to Low\n")
64 				);
65 			} else {
66 				btRssiState = BTC_RSSI_STATE_STAY_HIGH;
67 				BTC_PRINT(
68 					BTC_MSG_ALGORITHM,
69 					ALGO_BT_RSSI_STATE,
70 					("[BTCoex], BT Rssi state stay at High\n")
71 				);
72 			}
73 		}
74 	} else if (levelNum == 3) {
75 		if (rssiThresh > rssiThresh1) {
76 			BTC_PRINT(
77 				BTC_MSG_ALGORITHM,
78 				ALGO_BT_RSSI_STATE,
79 				("[BTCoex], BT Rssi thresh error!!\n")
80 			);
81 			return pCoexSta->preBtRssiState;
82 		}
83 
84 		if (
85 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_LOW) ||
86 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_STAY_LOW)
87 		) {
88 			if (btRssi >= (rssiThresh + BTC_RSSI_COEX_THRESH_TOL_8723B_1ANT)) {
89 				btRssiState = BTC_RSSI_STATE_MEDIUM;
90 				BTC_PRINT(
91 					BTC_MSG_ALGORITHM,
92 					ALGO_BT_RSSI_STATE,
93 					("[BTCoex], BT Rssi state switch to Medium\n")
94 				);
95 			} else {
96 				btRssiState = BTC_RSSI_STATE_STAY_LOW;
97 				BTC_PRINT(
98 					BTC_MSG_ALGORITHM,
99 					ALGO_BT_RSSI_STATE,
100 					("[BTCoex], BT Rssi state stay at Low\n")
101 				);
102 			}
103 		} else if (
104 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_MEDIUM) ||
105 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_STAY_MEDIUM)
106 		) {
107 			if (btRssi >= (rssiThresh1 + BTC_RSSI_COEX_THRESH_TOL_8723B_1ANT)) {
108 				btRssiState = BTC_RSSI_STATE_HIGH;
109 				BTC_PRINT(
110 					BTC_MSG_ALGORITHM,
111 					ALGO_BT_RSSI_STATE,
112 					("[BTCoex], BT Rssi state switch to High\n")
113 				);
114 			} else if (btRssi < rssiThresh) {
115 				btRssiState = BTC_RSSI_STATE_LOW;
116 				BTC_PRINT(
117 					BTC_MSG_ALGORITHM,
118 					ALGO_BT_RSSI_STATE,
119 					("[BTCoex], BT Rssi state switch to Low\n")
120 				);
121 			} else {
122 				btRssiState = BTC_RSSI_STATE_STAY_MEDIUM;
123 				BTC_PRINT(
124 					BTC_MSG_ALGORITHM,
125 					ALGO_BT_RSSI_STATE,
126 					("[BTCoex], BT Rssi state stay at Medium\n")
127 				);
128 			}
129 		} else {
130 			if (btRssi < rssiThresh1) {
131 				btRssiState = BTC_RSSI_STATE_MEDIUM;
132 				BTC_PRINT(
133 					BTC_MSG_ALGORITHM,
134 					ALGO_BT_RSSI_STATE,
135 					("[BTCoex], BT Rssi state switch to Medium\n")
136 				);
137 			} else {
138 				btRssiState = BTC_RSSI_STATE_STAY_HIGH;
139 				BTC_PRINT(
140 					BTC_MSG_ALGORITHM,
141 					ALGO_BT_RSSI_STATE,
142 					("[BTCoex], BT Rssi state stay at High\n")
143 				);
144 			}
145 		}
146 	}
147 
148 	pCoexSta->preBtRssiState = btRssiState;
149 
150 	return btRssiState;
151 }
152 
153 static void halbtc8723b1ant_UpdateRaMask(
154 	struct btc_coexist *pBtCoexist, bool bForceExec, u32 disRateMask
155 )
156 {
157 	pCoexDm->curRaMask = disRateMask;
158 
159 	if (bForceExec || (pCoexDm->preRaMask != pCoexDm->curRaMask))
160 		pBtCoexist->fBtcSet(
161 			pBtCoexist,
162 			BTC_SET_ACT_UPDATE_RAMASK,
163 			&pCoexDm->curRaMask
164 		);
165 	pCoexDm->preRaMask = pCoexDm->curRaMask;
166 }
167 
168 static void halbtc8723b1ant_AutoRateFallbackRetry(
169 	struct btc_coexist *pBtCoexist, bool bForceExec, u8 type
170 )
171 {
172 	bool bWifiUnderBMode = false;
173 
174 	pCoexDm->curArfrType = type;
175 
176 	if (bForceExec || (pCoexDm->preArfrType != pCoexDm->curArfrType)) {
177 		switch (pCoexDm->curArfrType) {
178 		case 0:	/*  normal mode */
179 			pBtCoexist->fBtcWrite4Byte(
180 				pBtCoexist, 0x430, pCoexDm->backupArfrCnt1
181 			);
182 			pBtCoexist->fBtcWrite4Byte(
183 				pBtCoexist, 0x434, pCoexDm->backupArfrCnt2
184 			);
185 			break;
186 		case 1:
187 			pBtCoexist->fBtcGet(
188 				pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode
189 			);
190 			if (bWifiUnderBMode) {
191 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x430, 0x0);
192 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x434, 0x01010101);
193 			} else {
194 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x430, 0x0);
195 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x434, 0x04030201);
196 			}
197 			break;
198 		default:
199 			break;
200 		}
201 	}
202 
203 	pCoexDm->preArfrType = pCoexDm->curArfrType;
204 }
205 
206 static void halbtc8723b1ant_RetryLimit(
207 	struct btc_coexist *pBtCoexist, bool bForceExec, u8 type
208 )
209 {
210 	pCoexDm->curRetryLimitType = type;
211 
212 	if (
213 		bForceExec ||
214 		(pCoexDm->preRetryLimitType != pCoexDm->curRetryLimitType)
215 	) {
216 		switch (pCoexDm->curRetryLimitType) {
217 		case 0:	/*  normal mode */
218 			pBtCoexist->fBtcWrite2Byte(
219 				pBtCoexist, 0x42a, pCoexDm->backupRetryLimit
220 			);
221 			break;
222 		case 1:	/*  retry limit =8 */
223 			pBtCoexist->fBtcWrite2Byte(pBtCoexist, 0x42a, 0x0808);
224 			break;
225 		default:
226 			break;
227 		}
228 	}
229 
230 	pCoexDm->preRetryLimitType = pCoexDm->curRetryLimitType;
231 }
232 
233 static void halbtc8723b1ant_AmpduMaxTime(
234 	struct btc_coexist *pBtCoexist, bool bForceExec, u8 type
235 )
236 {
237 	pCoexDm->curAmpduTimeType = type;
238 
239 	if (
240 		bForceExec || (pCoexDm->preAmpduTimeType != pCoexDm->curAmpduTimeType)
241 	) {
242 		switch (pCoexDm->curAmpduTimeType) {
243 		case 0:	/*  normal mode */
244 			pBtCoexist->fBtcWrite1Byte(
245 				pBtCoexist, 0x456, pCoexDm->backupAmpduMaxTime
246 			);
247 			break;
248 		case 1:	/*  AMPDU timw = 0x38 * 32us */
249 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x456, 0x38);
250 			break;
251 		default:
252 			break;
253 		}
254 	}
255 
256 	pCoexDm->preAmpduTimeType = pCoexDm->curAmpduTimeType;
257 }
258 
259 static void halbtc8723b1ant_LimitedTx(
260 	struct btc_coexist *pBtCoexist,
261 	bool bForceExec,
262 	u8 raMaskType,
263 	u8 arfrType,
264 	u8 retryLimitType,
265 	u8 ampduTimeType
266 )
267 {
268 	switch (raMaskType) {
269 	case 0:	/*  normal mode */
270 		halbtc8723b1ant_UpdateRaMask(pBtCoexist, bForceExec, 0x0);
271 		break;
272 	case 1:	/*  disable cck 1/2 */
273 		halbtc8723b1ant_UpdateRaMask(pBtCoexist, bForceExec, 0x00000003);
274 		break;
275 	case 2:	/*  disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4 */
276 		halbtc8723b1ant_UpdateRaMask(pBtCoexist, bForceExec, 0x0001f1f7);
277 		break;
278 	default:
279 		break;
280 	}
281 
282 	halbtc8723b1ant_AutoRateFallbackRetry(pBtCoexist, bForceExec, arfrType);
283 	halbtc8723b1ant_RetryLimit(pBtCoexist, bForceExec, retryLimitType);
284 	halbtc8723b1ant_AmpduMaxTime(pBtCoexist, bForceExec, ampduTimeType);
285 }
286 
287 static void halbtc8723b1ant_LimitedRx(
288 	struct btc_coexist *pBtCoexist,
289 	bool bForceExec,
290 	bool bRejApAggPkt,
291 	bool bBtCtrlAggBufSize,
292 	u8 aggBufSize
293 )
294 {
295 	bool bRejectRxAgg = bRejApAggPkt;
296 	bool bBtCtrlRxAggSize = bBtCtrlAggBufSize;
297 	u8 rxAggSize = aggBufSize;
298 
299 	/*  */
300 	/* 	Rx Aggregation related setting */
301 	/*  */
302 	pBtCoexist->fBtcSet(
303 		pBtCoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT, &bRejectRxAgg
304 	);
305 	/*  decide BT control aggregation buf size or not */
306 	pBtCoexist->fBtcSet(
307 		pBtCoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE, &bBtCtrlRxAggSize
308 	);
309 	/*  aggregation buf size, only work when BT control Rx aggregation size. */
310 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_AGG_BUF_SIZE, &rxAggSize);
311 	/*  real update aggregation setting */
312 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
313 
314 
315 }
316 
317 static void halbtc8723b1ant_QueryBtInfo(struct btc_coexist *pBtCoexist)
318 {
319 	u8 	H2C_Parameter[1] = {0};
320 
321 	pCoexSta->bC2hBtInfoReqSent = true;
322 
323 	H2C_Parameter[0] |= BIT0;	/*  trigger */
324 
325 	BTC_PRINT(
326 		BTC_MSG_ALGORITHM,
327 		ALGO_TRACE_FW_EXEC,
328 		("[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n", H2C_Parameter[0])
329 	);
330 
331 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x61, 1, H2C_Parameter);
332 }
333 
334 static void halbtc8723b1ant_MonitorBtCtr(struct btc_coexist *pBtCoexist)
335 {
336 	u32 regHPTxRx, regLPTxRx, u4Tmp;
337 	u32 regHPTx = 0, regHPRx = 0, regLPTx = 0, regLPRx = 0;
338 	static u8 NumOfBtCounterChk;
339 
340        /* to avoid 0x76e[3] = 1 (WLAN_Act control by PTA) during IPS */
341 	/* if (! (pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e) & 0x8)) */
342 
343 	if (pCoexSta->bUnderIps) {
344 		pCoexSta->highPriorityTx = 65535;
345 		pCoexSta->highPriorityRx = 65535;
346 		pCoexSta->lowPriorityTx = 65535;
347 		pCoexSta->lowPriorityRx = 65535;
348 		return;
349 	}
350 
351 	regHPTxRx = 0x770;
352 	regLPTxRx = 0x774;
353 
354 	u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, regHPTxRx);
355 	regHPTx = u4Tmp & bMaskLWord;
356 	regHPRx = (u4Tmp & bMaskHWord) >> 16;
357 
358 	u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, regLPTxRx);
359 	regLPTx = u4Tmp & bMaskLWord;
360 	regLPRx = (u4Tmp & bMaskHWord) >> 16;
361 
362 	pCoexSta->highPriorityTx = regHPTx;
363 	pCoexSta->highPriorityRx = regHPRx;
364 	pCoexSta->lowPriorityTx = regLPTx;
365 	pCoexSta->lowPriorityRx = regLPRx;
366 
367 	if ((pCoexSta->lowPriorityTx >= 1050) && (!pCoexSta->bC2hBtInquiryPage))
368 		pCoexSta->popEventCnt++;
369 
370 	BTC_PRINT(
371 		BTC_MSG_ALGORITHM,
372 		ALGO_TRACE,
373 		(
374 			"[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
375 			regHPRx,
376 			regHPTx,
377 			regLPRx,
378 			regLPTx
379 		)
380 	);
381 
382 	/*  reset counter */
383 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0xc);
384 
385 	if ((regHPTx == 0) && (regHPRx == 0) && (regLPTx == 0) && (regLPRx == 0)) {
386 		NumOfBtCounterChk++;
387 		if (NumOfBtCounterChk >= 3) {
388 			halbtc8723b1ant_QueryBtInfo(pBtCoexist);
389 			NumOfBtCounterChk = 0;
390 		}
391 	}
392 }
393 
394 
395 static void halbtc8723b1ant_MonitorWiFiCtr(struct btc_coexist *pBtCoexist)
396 {
397 	s32	wifiRssi = 0;
398 	bool bWifiBusy = false, bWifiUnderBMode = false;
399 	static u8 nCCKLockCounter;
400 
401 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
402 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi);
403 	pBtCoexist->fBtcGet(
404 		pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode
405 	);
406 
407 	if (pCoexSta->bUnderIps) {
408 		pCoexSta->nCRCOK_CCK = 0;
409 		pCoexSta->nCRCOK_11g = 0;
410 		pCoexSta->nCRCOK_11n = 0;
411 		pCoexSta->nCRCOK_11nAgg = 0;
412 
413 		pCoexSta->nCRCErr_CCK = 0;
414 		pCoexSta->nCRCErr_11g = 0;
415 		pCoexSta->nCRCErr_11n = 0;
416 		pCoexSta->nCRCErr_11nAgg = 0;
417 	} else {
418 		pCoexSta->nCRCOK_CCK	= pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xf88);
419 		pCoexSta->nCRCOK_11g	= pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf94);
420 		pCoexSta->nCRCOK_11n	= pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf90);
421 		pCoexSta->nCRCOK_11nAgg = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xfb8);
422 
423 		pCoexSta->nCRCErr_CCK	 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xf84);
424 		pCoexSta->nCRCErr_11g	 = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf96);
425 		pCoexSta->nCRCErr_11n	 = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf92);
426 		pCoexSta->nCRCErr_11nAgg = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xfba);
427 	}
428 
429 
430 	/* reset counter */
431 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0xf16, 0x1, 0x1);
432 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0xf16, 0x1, 0x0);
433 
434 	if (bWifiBusy && (wifiRssi >= 30) && !bWifiUnderBMode) {
435 		if (
436 			(pCoexDm->btStatus == BT_8723B_1ANT_BT_STATUS_ACL_BUSY) ||
437 			(pCoexDm->btStatus == BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY) ||
438 			(pCoexDm->btStatus == BT_8723B_1ANT_BT_STATUS_SCO_BUSY)
439 		) {
440 			if (
441 				pCoexSta->nCRCOK_CCK > (
442 					pCoexSta->nCRCOK_11g +
443 					pCoexSta->nCRCOK_11n +
444 					pCoexSta->nCRCOK_11nAgg
445 				)
446 			) {
447 				if (nCCKLockCounter < 5)
448 				 nCCKLockCounter++;
449 			} else {
450 				if (nCCKLockCounter > 0)
451 				 nCCKLockCounter--;
452 			}
453 
454 		} else {
455 			if (nCCKLockCounter > 0)
456 			  nCCKLockCounter--;
457 		}
458 	} else {
459 		if (nCCKLockCounter > 0)
460 			nCCKLockCounter--;
461 	}
462 
463 	if (!pCoexSta->bPreCCKLock) {
464 
465 		if (nCCKLockCounter >= 5)
466 		 pCoexSta->bCCKLock = true;
467 		else
468 		 pCoexSta->bCCKLock = false;
469 	} else {
470 		if (nCCKLockCounter == 0)
471 		 pCoexSta->bCCKLock = false;
472 		else
473 		 pCoexSta->bCCKLock = true;
474 	}
475 
476 	pCoexSta->bPreCCKLock =  pCoexSta->bCCKLock;
477 
478 
479 }
480 
481 static bool halbtc8723b1ant_IsWifiStatusChanged(struct btc_coexist *pBtCoexist)
482 {
483 	static bool	bPreWifiBusy, bPreUnder4way, bPreBtHsOn;
484 	bool bWifiBusy = false, bUnder4way = false, bBtHsOn = false;
485 	bool bWifiConnected = false;
486 
487 	pBtCoexist->fBtcGet(
488 		pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected
489 	);
490 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
491 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
492 	pBtCoexist->fBtcGet(
493 		pBtCoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &bUnder4way
494 	);
495 
496 	if (bWifiConnected) {
497 		if (bWifiBusy != bPreWifiBusy) {
498 			bPreWifiBusy = bWifiBusy;
499 			return true;
500 		}
501 
502 		if (bUnder4way != bPreUnder4way) {
503 			bPreUnder4way = bUnder4way;
504 			return true;
505 		}
506 
507 		if (bBtHsOn != bPreBtHsOn) {
508 			bPreBtHsOn = bBtHsOn;
509 			return true;
510 		}
511 	}
512 
513 	return false;
514 }
515 
516 static void halbtc8723b1ant_UpdateBtLinkInfo(struct btc_coexist *pBtCoexist)
517 {
518 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
519 	bool bBtHsOn = false;
520 
521 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
522 
523 	pBtLinkInfo->bBtLinkExist = pCoexSta->bBtLinkExist;
524 	pBtLinkInfo->bScoExist = pCoexSta->bScoExist;
525 	pBtLinkInfo->bA2dpExist = pCoexSta->bA2dpExist;
526 	pBtLinkInfo->bPanExist = pCoexSta->bPanExist;
527 	pBtLinkInfo->bHidExist = pCoexSta->bHidExist;
528 
529 	/*  work around for HS mode. */
530 	if (bBtHsOn) {
531 		pBtLinkInfo->bPanExist = true;
532 		pBtLinkInfo->bBtLinkExist = true;
533 	}
534 
535 	/*  check if Sco only */
536 	if (
537 		pBtLinkInfo->bScoExist &&
538 		!pBtLinkInfo->bA2dpExist &&
539 		!pBtLinkInfo->bPanExist &&
540 		!pBtLinkInfo->bHidExist
541 	)
542 		pBtLinkInfo->bScoOnly = true;
543 	else
544 		pBtLinkInfo->bScoOnly = false;
545 
546 	/*  check if A2dp only */
547 	if (
548 		!pBtLinkInfo->bScoExist &&
549 		pBtLinkInfo->bA2dpExist &&
550 		!pBtLinkInfo->bPanExist &&
551 		!pBtLinkInfo->bHidExist
552 	)
553 		pBtLinkInfo->bA2dpOnly = true;
554 	else
555 		pBtLinkInfo->bA2dpOnly = false;
556 
557 	/*  check if Pan only */
558 	if (
559 		!pBtLinkInfo->bScoExist &&
560 		!pBtLinkInfo->bA2dpExist &&
561 		pBtLinkInfo->bPanExist &&
562 		!pBtLinkInfo->bHidExist
563 	)
564 		pBtLinkInfo->bPanOnly = true;
565 	else
566 		pBtLinkInfo->bPanOnly = false;
567 
568 	/*  check if Hid only */
569 	if (
570 		!pBtLinkInfo->bScoExist &&
571 		!pBtLinkInfo->bA2dpExist &&
572 		!pBtLinkInfo->bPanExist &&
573 		pBtLinkInfo->bHidExist
574 	)
575 		pBtLinkInfo->bHidOnly = true;
576 	else
577 		pBtLinkInfo->bHidOnly = false;
578 }
579 
580 static u8 halbtc8723b1ant_ActionAlgorithm(struct btc_coexist *pBtCoexist)
581 {
582 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
583 	bool bBtHsOn = false;
584 	u8 algorithm = BT_8723B_1ANT_COEX_ALGO_UNDEFINED;
585 	u8 numOfDiffProfile = 0;
586 
587 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
588 
589 	if (!pBtLinkInfo->bBtLinkExist) {
590 		BTC_PRINT(
591 			BTC_MSG_ALGORITHM,
592 			ALGO_TRACE,
593 			("[BTCoex], No BT link exists!!!\n")
594 		);
595 		return algorithm;
596 	}
597 
598 	if (pBtLinkInfo->bScoExist)
599 		numOfDiffProfile++;
600 	if (pBtLinkInfo->bHidExist)
601 		numOfDiffProfile++;
602 	if (pBtLinkInfo->bPanExist)
603 		numOfDiffProfile++;
604 	if (pBtLinkInfo->bA2dpExist)
605 		numOfDiffProfile++;
606 
607 	if (numOfDiffProfile == 1) {
608 		if (pBtLinkInfo->bScoExist) {
609 			BTC_PRINT(
610 				BTC_MSG_ALGORITHM,
611 				ALGO_TRACE,
612 				("[BTCoex], BT Profile = SCO only\n")
613 			);
614 			algorithm = BT_8723B_1ANT_COEX_ALGO_SCO;
615 		} else {
616 			if (pBtLinkInfo->bHidExist) {
617 				BTC_PRINT(
618 					BTC_MSG_ALGORITHM,
619 					ALGO_TRACE,
620 					("[BTCoex], BT Profile = HID only\n")
621 				);
622 				algorithm = BT_8723B_1ANT_COEX_ALGO_HID;
623 			} else if (pBtLinkInfo->bA2dpExist) {
624 				BTC_PRINT(
625 					BTC_MSG_ALGORITHM,
626 					ALGO_TRACE,
627 					("[BTCoex], BT Profile = A2DP only\n")
628 				);
629 				algorithm = BT_8723B_1ANT_COEX_ALGO_A2DP;
630 			} else if (pBtLinkInfo->bPanExist) {
631 				if (bBtHsOn) {
632 					BTC_PRINT(
633 						BTC_MSG_ALGORITHM,
634 						ALGO_TRACE,
635 						("[BTCoex], BT Profile = PAN(HS) only\n")
636 					);
637 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANHS;
638 				} else {
639 					BTC_PRINT(
640 						BTC_MSG_ALGORITHM,
641 						ALGO_TRACE,
642 						("[BTCoex], BT Profile = PAN(EDR) only\n")
643 					);
644 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR;
645 				}
646 			}
647 		}
648 	} else if (numOfDiffProfile == 2) {
649 		if (pBtLinkInfo->bScoExist) {
650 			if (pBtLinkInfo->bHidExist) {
651 				BTC_PRINT(
652 					BTC_MSG_ALGORITHM,
653 					ALGO_TRACE,
654 					("[BTCoex], BT Profile = SCO + HID\n")
655 				);
656 				algorithm = BT_8723B_1ANT_COEX_ALGO_HID;
657 			} else if (pBtLinkInfo->bA2dpExist) {
658 				BTC_PRINT(
659 					BTC_MSG_ALGORITHM,
660 					ALGO_TRACE,
661 					("[BTCoex], BT Profile = SCO + A2DP ==> SCO\n")
662 				);
663 				algorithm = BT_8723B_1ANT_COEX_ALGO_SCO;
664 			} else if (pBtLinkInfo->bPanExist) {
665 				if (bBtHsOn) {
666 					BTC_PRINT(
667 						BTC_MSG_ALGORITHM,
668 						ALGO_TRACE,
669 						("[BTCoex], BT Profile = SCO + PAN(HS)\n")
670 					);
671 					algorithm = BT_8723B_1ANT_COEX_ALGO_SCO;
672 				} else {
673 					BTC_PRINT(
674 						BTC_MSG_ALGORITHM,
675 						ALGO_TRACE,
676 						("[BTCoex], BT Profile = SCO + PAN(EDR)\n")
677 					);
678 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR_HID;
679 				}
680 			}
681 		} else {
682 			if (pBtLinkInfo->bHidExist && pBtLinkInfo->bA2dpExist) {
683 				BTC_PRINT(
684 					BTC_MSG_ALGORITHM,
685 					ALGO_TRACE,
686 					("[BTCoex], BT Profile = HID + A2DP\n")
687 				);
688 				algorithm = BT_8723B_1ANT_COEX_ALGO_HID_A2DP;
689 			} else if (pBtLinkInfo->bHidExist && pBtLinkInfo->bPanExist) {
690 				if (bBtHsOn) {
691 					BTC_PRINT(
692 						BTC_MSG_ALGORITHM,
693 						ALGO_TRACE,
694 						("[BTCoex], BT Profile = HID + PAN(HS)\n")
695 					);
696 					algorithm = BT_8723B_1ANT_COEX_ALGO_HID_A2DP;
697 				} else {
698 					BTC_PRINT(
699 						BTC_MSG_ALGORITHM,
700 						ALGO_TRACE,
701 						("[BTCoex], BT Profile = HID + PAN(EDR)\n")
702 					);
703 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR_HID;
704 				}
705 			} else if (pBtLinkInfo->bPanExist && pBtLinkInfo->bA2dpExist) {
706 				if (bBtHsOn) {
707 					BTC_PRINT(
708 						BTC_MSG_ALGORITHM,
709 						ALGO_TRACE,
710 						("[BTCoex], BT Profile = A2DP + PAN(HS)\n")
711 					);
712 					algorithm = BT_8723B_1ANT_COEX_ALGO_A2DP_PANHS;
713 				} else {
714 					BTC_PRINT(
715 						BTC_MSG_ALGORITHM,
716 						ALGO_TRACE,
717 						("[BTCoex], BT Profile = A2DP + PAN(EDR)\n")
718 					);
719 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR_A2DP;
720 				}
721 			}
722 		}
723 	} else if (numOfDiffProfile == 3) {
724 		if (pBtLinkInfo->bScoExist) {
725 			if (pBtLinkInfo->bHidExist && pBtLinkInfo->bA2dpExist) {
726 				BTC_PRINT(
727 					BTC_MSG_ALGORITHM,
728 					ALGO_TRACE,
729 					("[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n")
730 				);
731 				algorithm = BT_8723B_1ANT_COEX_ALGO_HID;
732 			} else if (
733 				pBtLinkInfo->bHidExist && pBtLinkInfo->bPanExist
734 			) {
735 				if (bBtHsOn) {
736 					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BT Profile = SCO + HID + PAN(HS)\n"));
737 					algorithm = BT_8723B_1ANT_COEX_ALGO_HID_A2DP;
738 				} else {
739 					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n"));
740 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR_HID;
741 				}
742 			} else if (pBtLinkInfo->bPanExist && pBtLinkInfo->bA2dpExist) {
743 				if (bBtHsOn) {
744 					BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n"));
745 					algorithm = BT_8723B_1ANT_COEX_ALGO_SCO;
746 				} else {
747 					BTC_PRINT(
748 						BTC_MSG_ALGORITHM,
749 						ALGO_TRACE,
750 						("[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n")
751 					);
752 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR_HID;
753 				}
754 			}
755 		} else {
756 			if (
757 				pBtLinkInfo->bHidExist &&
758 				pBtLinkInfo->bPanExist &&
759 				pBtLinkInfo->bA2dpExist
760 			) {
761 				if (bBtHsOn) {
762 					BTC_PRINT(
763 						BTC_MSG_ALGORITHM,
764 						ALGO_TRACE,
765 						("[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n")
766 					);
767 					algorithm = BT_8723B_1ANT_COEX_ALGO_HID_A2DP;
768 				} else {
769 					BTC_PRINT(
770 						BTC_MSG_ALGORITHM,
771 						ALGO_TRACE,
772 						("[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n")
773 					);
774 					algorithm = BT_8723B_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
775 				}
776 			}
777 		}
778 	} else if (numOfDiffProfile >= 3) {
779 		if (pBtLinkInfo->bScoExist) {
780 			if (
781 				pBtLinkInfo->bHidExist &&
782 				pBtLinkInfo->bPanExist &&
783 				pBtLinkInfo->bA2dpExist
784 			) {
785 				if (bBtHsOn) {
786 					BTC_PRINT(
787 						BTC_MSG_ALGORITHM,
788 						ALGO_TRACE,
789 						("[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n")
790 					);
791 
792 				} else {
793 					BTC_PRINT(
794 						BTC_MSG_ALGORITHM,
795 						ALGO_TRACE,
796 						("[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR) ==>PAN(EDR)+HID\n")
797 					);
798 					algorithm = BT_8723B_1ANT_COEX_ALGO_PANEDR_HID;
799 				}
800 			}
801 		}
802 	}
803 
804 	return algorithm;
805 }
806 
807 static void halbtc8723b1ant_SetSwPenaltyTxRateAdaptive(
808 	struct btc_coexist *pBtCoexist, bool bLowPenaltyRa
809 )
810 {
811 	u8 	H2C_Parameter[6] = {0};
812 
813 	H2C_Parameter[0] = 0x6;	/*  opCode, 0x6 = Retry_Penalty */
814 
815 	if (bLowPenaltyRa) {
816 		H2C_Parameter[1] |= BIT0;
817 		H2C_Parameter[2] = 0x00;  /* normal rate except MCS7/6/5, OFDM54/48/36 */
818 		H2C_Parameter[3] = 0xf7;  /* MCS7 or OFDM54 */
819 		H2C_Parameter[4] = 0xf8;  /* MCS6 or OFDM48 */
820 		H2C_Parameter[5] = 0xf9;	/* MCS5 or OFDM36 */
821 	}
822 
823 	BTC_PRINT(
824 		BTC_MSG_ALGORITHM,
825 		ALGO_TRACE_FW_EXEC,
826 		(
827 			"[BTCoex], set WiFi Low-Penalty Retry: %s",
828 			(bLowPenaltyRa ? "ON!!" : "OFF!!")
829 		)
830 	);
831 
832 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x69, 6, H2C_Parameter);
833 }
834 
835 static void halbtc8723b1ant_LowPenaltyRa(
836 	struct btc_coexist *pBtCoexist, bool bForceExec, bool bLowPenaltyRa
837 )
838 {
839 	pCoexDm->bCurLowPenaltyRa = bLowPenaltyRa;
840 
841 	if (!bForceExec) {
842 		if (pCoexDm->bPreLowPenaltyRa == pCoexDm->bCurLowPenaltyRa)
843 			return;
844 	}
845 	halbtc8723b1ant_SetSwPenaltyTxRateAdaptive(
846 		pBtCoexist, pCoexDm->bCurLowPenaltyRa
847 	);
848 
849 	pCoexDm->bPreLowPenaltyRa = pCoexDm->bCurLowPenaltyRa;
850 }
851 
852 static void halbtc8723b1ant_SetCoexTable(
853 	struct btc_coexist *pBtCoexist,
854 	u32 val0x6c0,
855 	u32 val0x6c4,
856 	u32 val0x6c8,
857 	u8 val0x6cc
858 )
859 {
860 	BTC_PRINT(
861 		BTC_MSG_ALGORITHM,
862 		ALGO_TRACE_SW_EXEC,
863 		("[BTCoex], set coex table, set 0x6c0 = 0x%x\n", val0x6c0)
864 	);
865 	pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x6c0, val0x6c0);
866 
867 	BTC_PRINT(
868 		BTC_MSG_ALGORITHM,
869 		ALGO_TRACE_SW_EXEC,
870 		("[BTCoex], set coex table, set 0x6c4 = 0x%x\n", val0x6c4)
871 	);
872 	pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x6c4, val0x6c4);
873 
874 	BTC_PRINT(
875 		BTC_MSG_ALGORITHM,
876 		ALGO_TRACE_SW_EXEC,
877 		("[BTCoex], set coex table, set 0x6c8 = 0x%x\n", val0x6c8)
878 	);
879 	pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x6c8, val0x6c8);
880 
881 	BTC_PRINT(
882 		BTC_MSG_ALGORITHM,
883 		ALGO_TRACE_SW_EXEC,
884 		("[BTCoex], set coex table, set 0x6cc = 0x%x\n", val0x6cc)
885 	);
886 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cc, val0x6cc);
887 }
888 
889 static void halbtc8723b1ant_CoexTable(
890 	struct btc_coexist *pBtCoexist,
891 	bool bForceExec,
892 	u32 val0x6c0,
893 	u32 val0x6c4,
894 	u32 val0x6c8,
895 	u8 val0x6cc
896 )
897 {
898 	BTC_PRINT(
899 		BTC_MSG_ALGORITHM,
900 		ALGO_TRACE_SW,
901 		(
902 			"[BTCoex], %s write Coex Table 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6cc = 0x%x\n",
903 			(bForceExec ? "force to" : ""),
904 			val0x6c0, val0x6c4, val0x6cc
905 		)
906 	);
907 	pCoexDm->curVal0x6c0 = val0x6c0;
908 	pCoexDm->curVal0x6c4 = val0x6c4;
909 	pCoexDm->curVal0x6c8 = val0x6c8;
910 	pCoexDm->curVal0x6cc = val0x6cc;
911 
912 	if (!bForceExec) {
913 		if (
914 			(pCoexDm->preVal0x6c0 == pCoexDm->curVal0x6c0) &&
915 		    (pCoexDm->preVal0x6c4 == pCoexDm->curVal0x6c4) &&
916 		    (pCoexDm->preVal0x6c8 == pCoexDm->curVal0x6c8) &&
917 		    (pCoexDm->preVal0x6cc == pCoexDm->curVal0x6cc)
918 		)
919 			return;
920 	}
921 
922 	halbtc8723b1ant_SetCoexTable(
923 		pBtCoexist, val0x6c0, val0x6c4, val0x6c8, val0x6cc
924 	);
925 
926 	pCoexDm->preVal0x6c0 = pCoexDm->curVal0x6c0;
927 	pCoexDm->preVal0x6c4 = pCoexDm->curVal0x6c4;
928 	pCoexDm->preVal0x6c8 = pCoexDm->curVal0x6c8;
929 	pCoexDm->preVal0x6cc = pCoexDm->curVal0x6cc;
930 }
931 
932 static void halbtc8723b1ant_CoexTableWithType(
933 	struct btc_coexist *pBtCoexist, bool bForceExec, u8 type
934 )
935 {
936 	BTC_PRINT(
937 		BTC_MSG_ALGORITHM,
938 		ALGO_TRACE,
939 		("[BTCoex], ********** CoexTable(%d) **********\n", type)
940 	);
941 
942 	pCoexSta->nCoexTableType = type;
943 
944 	switch (type) {
945 	case 0:
946 		halbtc8723b1ant_CoexTable(
947 			pBtCoexist, bForceExec, 0x55555555, 0x55555555, 0xffffff, 0x3
948 		);
949 		break;
950 	case 1:
951 		halbtc8723b1ant_CoexTable(
952 			pBtCoexist, bForceExec, 0x55555555, 0x5a5a5a5a, 0xffffff, 0x3
953 		);
954 		break;
955 	case 2:
956 		halbtc8723b1ant_CoexTable(
957 			pBtCoexist, bForceExec, 0x5a5a5a5a, 0x5a5a5a5a, 0xffffff, 0x3
958 		);
959 		break;
960 	case 3:
961 		halbtc8723b1ant_CoexTable(
962 			pBtCoexist, bForceExec, 0xaaaa5555, 0xaaaa5a5a, 0xffffff, 0x3
963 		);
964 		break;
965 	case 4:
966 		halbtc8723b1ant_CoexTable(
967 			pBtCoexist, bForceExec, 0x55555555, 0xaaaa5a5a, 0xffffff, 0x3
968 		);
969 		break;
970 	case 5:
971 		halbtc8723b1ant_CoexTable(
972 			pBtCoexist, bForceExec, 0x5a5a5a5a, 0xaaaa5a5a, 0xffffff, 0x3
973 		);
974 		break;
975 	case 6:
976 		halbtc8723b1ant_CoexTable(
977 			pBtCoexist, bForceExec, 0x55555555, 0xaaaaaaaa, 0xffffff, 0x3
978 		);
979 		break;
980 	case 7:
981 		halbtc8723b1ant_CoexTable(
982 			pBtCoexist, bForceExec, 0xaaaaaaaa, 0xaaaaaaaa, 0xffffff, 0x3
983 		);
984 		break;
985 	default:
986 		break;
987 	}
988 }
989 
990 static void halbtc8723b1ant_SetFwIgnoreWlanAct(
991 	struct btc_coexist *pBtCoexist, bool bEnable
992 )
993 {
994 	u8 H2C_Parameter[1] = {0};
995 
996 	if (bEnable)
997 		H2C_Parameter[0] |= BIT0; /* function enable */
998 
999 	BTC_PRINT(
1000 		BTC_MSG_ALGORITHM,
1001 		ALGO_TRACE_FW_EXEC,
1002 		(
1003 			"[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63 = 0x%x\n",
1004 			H2C_Parameter[0]
1005 		)
1006 	);
1007 
1008 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x63, 1, H2C_Parameter);
1009 }
1010 
1011 static void halbtc8723b1ant_IgnoreWlanAct(
1012 	struct btc_coexist *pBtCoexist, bool bForceExec, bool bEnable
1013 )
1014 {
1015 	BTC_PRINT(
1016 		BTC_MSG_ALGORITHM,
1017 		ALGO_TRACE_FW,
1018 		(
1019 			"[BTCoex], %s turn Ignore WlanAct %s\n",
1020 			(bForceExec ? "force to" : ""),
1021 			(bEnable ? "ON" : "OFF")
1022 		)
1023 	);
1024 	pCoexDm->bCurIgnoreWlanAct = bEnable;
1025 
1026 	if (!bForceExec) {
1027 		BTC_PRINT(
1028 			BTC_MSG_ALGORITHM,
1029 			ALGO_TRACE_FW_DETAIL,
1030 			(
1031 				"[BTCoex], bPreIgnoreWlanAct = %d, bCurIgnoreWlanAct = %d!!\n",
1032 				pCoexDm->bPreIgnoreWlanAct,
1033 				pCoexDm->bCurIgnoreWlanAct
1034 			)
1035 		);
1036 
1037 		if (pCoexDm->bPreIgnoreWlanAct == pCoexDm->bCurIgnoreWlanAct)
1038 			return;
1039 	}
1040 	halbtc8723b1ant_SetFwIgnoreWlanAct(pBtCoexist, bEnable);
1041 
1042 	pCoexDm->bPreIgnoreWlanAct = pCoexDm->bCurIgnoreWlanAct;
1043 }
1044 
1045 static void halbtc8723b1ant_SetLpsRpwm(
1046 	struct btc_coexist *pBtCoexist, u8 lpsVal, u8 rpwmVal
1047 )
1048 {
1049 	u8 lps = lpsVal;
1050 	u8 rpwm = rpwmVal;
1051 
1052 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_LPS_VAL, &lps);
1053 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
1054 }
1055 
1056 static void halbtc8723b1ant_LpsRpwm(
1057 	struct btc_coexist *pBtCoexist, bool bForceExec, u8 lpsVal, u8 rpwmVal
1058 )
1059 {
1060 	BTC_PRINT(
1061 		BTC_MSG_ALGORITHM,
1062 		ALGO_TRACE_FW,
1063 		(
1064 			"[BTCoex], %s set lps/rpwm = 0x%x/0x%x\n",
1065 			(bForceExec ? "force to" : ""),
1066 			lpsVal,
1067 			rpwmVal
1068 		)
1069 	);
1070 	pCoexDm->curLps = lpsVal;
1071 	pCoexDm->curRpwm = rpwmVal;
1072 
1073 	if (!bForceExec) {
1074 		BTC_PRINT(
1075 			BTC_MSG_ALGORITHM,
1076 			ALGO_TRACE_FW_DETAIL,
1077 			(
1078 				"[BTCoex], LPS-RxBeaconMode = 0x%x , LPS-RPWM = 0x%x!!\n",
1079 				pCoexDm->curLps,
1080 				pCoexDm->curRpwm
1081 			)
1082 		);
1083 
1084 		if (
1085 			(pCoexDm->preLps == pCoexDm->curLps) &&
1086 			(pCoexDm->preRpwm == pCoexDm->curRpwm)
1087 		) {
1088 			BTC_PRINT(
1089 				BTC_MSG_ALGORITHM,
1090 				ALGO_TRACE_FW_DETAIL,
1091 				(
1092 					"[BTCoex], LPS-RPWM_Last = 0x%x , LPS-RPWM_Now = 0x%x!!\n",
1093 					pCoexDm->preRpwm,
1094 					pCoexDm->curRpwm
1095 				)
1096 			);
1097 
1098 			return;
1099 		}
1100 	}
1101 	halbtc8723b1ant_SetLpsRpwm(pBtCoexist, lpsVal, rpwmVal);
1102 
1103 	pCoexDm->preLps = pCoexDm->curLps;
1104 	pCoexDm->preRpwm = pCoexDm->curRpwm;
1105 }
1106 
1107 static void halbtc8723b1ant_SwMechanism(
1108 	struct btc_coexist *pBtCoexist, bool bLowPenaltyRA
1109 )
1110 {
1111 	BTC_PRINT(
1112 		BTC_MSG_ALGORITHM,
1113 		ALGO_BT_MONITOR,
1114 		("[BTCoex], SM[LpRA] = %d\n", bLowPenaltyRA)
1115 	);
1116 
1117 	halbtc8723b1ant_LowPenaltyRa(pBtCoexist, NORMAL_EXEC, bLowPenaltyRA);
1118 }
1119 
1120 static void halbtc8723b1ant_SetAntPath(
1121 	struct btc_coexist *pBtCoexist, u8 antPosType, bool bInitHwCfg, bool bWifiOff
1122 )
1123 {
1124 	struct btc_board_info *pBoardInfo = &pBtCoexist->boardInfo;
1125 	u32 fwVer = 0, u4Tmp = 0, cntBtCalChk = 0;
1126 	bool bPgExtSwitch = false;
1127 	bool bUseExtSwitch = false;
1128 	bool bIsInMpMode = false;
1129 	u8 H2C_Parameter[2] = {0}, u1Tmp = 0;
1130 
1131 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_EXT_SWITCH, &bPgExtSwitch);
1132 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer); /*  [31:16]=fw ver, [15:0]=fw sub ver */
1133 
1134 	if ((fwVer > 0 && fwVer < 0xc0000) || bPgExtSwitch)
1135 		bUseExtSwitch = true;
1136 
1137 	if (bInitHwCfg) {
1138 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x780); /* WiFi TRx Mask on */
1139 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x15); /* BT TRx Mask on */
1140 
1141 		if (fwVer >= 0x180000) {
1142 			/* Use H2C to set GNT_BT to HIGH */
1143 			H2C_Parameter[0] = 1;
1144 			pBtCoexist->fBtcFillH2c(pBtCoexist, 0x6E, 1, H2C_Parameter);
1145 		} else /*  set grant_bt to high */
1146 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
1147 
1148 		/* set wlan_act control by PTA */
1149 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
1150 
1151 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x1); /* BT select s0/s1 is controlled by WiFi */
1152 
1153 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x39, 0x8, 0x1);
1154 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x974, 0xff);
1155 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x944, 0x3, 0x3);
1156 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x930, 0x77);
1157 	} else if (bWifiOff) {
1158 		if (fwVer >= 0x180000) {
1159 			/* Use H2C to set GNT_BT to HIGH */
1160 			H2C_Parameter[0] = 1;
1161 			pBtCoexist->fBtcFillH2c(pBtCoexist, 0x6E, 1, H2C_Parameter);
1162 		} else /*  set grant_bt to high */
1163 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
1164 
1165 		/* set wlan_act to always low */
1166 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
1167 
1168 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_IS_IN_MP_MODE, &bIsInMpMode);
1169 		if (!bIsInMpMode)
1170 			pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x0); /* BT select s0/s1 is controlled by BT */
1171 		else
1172 			pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x1); /* BT select s0/s1 is controlled by WiFi */
1173 
1174 		/*  0x4c[24:23]= 00, Set Antenna control by BT_RFE_CTRL	BT Vendor 0xac = 0xf002 */
1175 		u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
1176 		u4Tmp &= ~BIT23;
1177 		u4Tmp &= ~BIT24;
1178 		pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x4c, u4Tmp);
1179 	} else {
1180 		/* Use H2C to set GNT_BT to LOW */
1181 		if (fwVer >= 0x180000) {
1182 			if (pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765) != 0) {
1183 				H2C_Parameter[0] = 0;
1184 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x6E, 1, H2C_Parameter);
1185 			}
1186 		} else {
1187 			/*  BT calibration check */
1188 			while (cntBtCalChk <= 20) {
1189 				u1Tmp = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x49d);
1190 				cntBtCalChk++;
1191 
1192 				if (u1Tmp & BIT0) {
1193 					BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], ########### BT is calibrating (wait cnt =%d) ###########\n", cntBtCalChk));
1194 					mdelay(50);
1195 				} else {
1196 					BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], ********** BT is NOT calibrating (wait cnt =%d)**********\n", cntBtCalChk));
1197 					break;
1198 				}
1199 			}
1200 
1201 			/*  set grant_bt to PTA */
1202 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x0);
1203 		}
1204 
1205 		if (pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e) != 0xc)
1206 			/* set wlan_act control by PTA */
1207 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0xc);
1208 	}
1209 
1210 	if (bUseExtSwitch) {
1211 		if (bInitHwCfg) {
1212 			/*  0x4c[23]= 0, 0x4c[24]= 1  Antenna control by WL/BT */
1213 			u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
1214 			u4Tmp &= ~BIT23;
1215 			u4Tmp |= BIT24;
1216 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x4c, u4Tmp);
1217 
1218 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0); /*  fixed internal switch S1->WiFi, S0->BT */
1219 
1220 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT) {
1221 				/* tell firmware "no antenna inverse" */
1222 				H2C_Parameter[0] = 0;
1223 				H2C_Parameter[1] = 1;  /* ext switch type */
1224 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1225 			} else {
1226 				/* tell firmware "antenna inverse" */
1227 				H2C_Parameter[0] = 1;
1228 				H2C_Parameter[1] = 1;  /* ext switch type */
1229 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1230 			}
1231 		}
1232 
1233 
1234 		/*  ext switch setting */
1235 		switch (antPosType) {
1236 		case BTC_ANT_PATH_WIFI:
1237 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1238 				pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x1);
1239 			else
1240 				pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x2);
1241 			break;
1242 		case BTC_ANT_PATH_BT:
1243 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1244 				pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x2);
1245 			else
1246 				pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x1);
1247 			break;
1248 		default:
1249 		case BTC_ANT_PATH_PTA:
1250 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1251 				pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x1);
1252 			else
1253 				pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x2);
1254 			break;
1255 		}
1256 
1257 	} else {
1258 		if (bInitHwCfg) {
1259 			/*  0x4c[23]= 1, 0x4c[24]= 0  Antenna control by 0x64 */
1260 			u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
1261 			u4Tmp |= BIT23;
1262 			u4Tmp &= ~BIT24;
1263 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x4c, u4Tmp);
1264 
1265 			/* Fix Ext switch Main->S1, Aux->S0 */
1266 			pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x64, 0x1, 0x0);
1267 
1268 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT) {
1269 
1270 				/* tell firmware "no antenna inverse" */
1271 				H2C_Parameter[0] = 0;
1272 				H2C_Parameter[1] = 0;  /* internal switch type */
1273 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1274 			} else {
1275 
1276 				/* tell firmware "antenna inverse" */
1277 				H2C_Parameter[0] = 1;
1278 				H2C_Parameter[1] = 0;  /* internal switch type */
1279 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1280 			}
1281 		}
1282 
1283 
1284 		/*  internal switch setting */
1285 		switch (antPosType) {
1286 		case BTC_ANT_PATH_WIFI:
1287 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1288 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
1289 			else
1290 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
1291 			break;
1292 		case BTC_ANT_PATH_BT:
1293 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1294 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
1295 			else
1296 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
1297 			break;
1298 		default:
1299 		case BTC_ANT_PATH_PTA:
1300 			if (pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1301 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x200);
1302 			else
1303 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x80);
1304 			break;
1305 		}
1306 	}
1307 }
1308 
1309 static void halbtc8723b1ant_SetFwPstdma(
1310 	struct btc_coexist *pBtCoexist, u8 byte1, u8 byte2, u8 byte3, u8 byte4, u8 byte5
1311 )
1312 {
1313 	u8 H2C_Parameter[5] = {0};
1314 	u8 realByte1 = byte1, realByte5 = byte5;
1315 	bool bApEnable = false;
1316 
1317 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
1318 
1319 	if (bApEnable) {
1320 		if (byte1 & BIT4 && !(byte1 & BIT5)) {
1321 			BTC_PRINT(
1322 				BTC_MSG_INTERFACE,
1323 				INTF_NOTIFY,
1324 				("[BTCoex], FW for 1Ant AP mode\n")
1325 			);
1326 			realByte1 &= ~BIT4;
1327 			realByte1 |= BIT5;
1328 
1329 			realByte5 |= BIT5;
1330 			realByte5 &= ~BIT6;
1331 		}
1332 	}
1333 
1334 	H2C_Parameter[0] = realByte1;
1335 	H2C_Parameter[1] = byte2;
1336 	H2C_Parameter[2] = byte3;
1337 	H2C_Parameter[3] = byte4;
1338 	H2C_Parameter[4] = realByte5;
1339 
1340 	pCoexDm->psTdmaPara[0] = realByte1;
1341 	pCoexDm->psTdmaPara[1] = byte2;
1342 	pCoexDm->psTdmaPara[2] = byte3;
1343 	pCoexDm->psTdmaPara[3] = byte4;
1344 	pCoexDm->psTdmaPara[4] = realByte5;
1345 
1346 	BTC_PRINT(
1347 		BTC_MSG_ALGORITHM,
1348 		ALGO_TRACE_FW_EXEC,
1349 		(
1350 			"[BTCoex], PS-TDMA H2C cmd = 0x%x%08x\n",
1351 			H2C_Parameter[0],
1352 			H2C_Parameter[1] << 24 |
1353 			H2C_Parameter[2] << 16 |
1354 			H2C_Parameter[3] << 8 |
1355 			H2C_Parameter[4]
1356 		)
1357 	);
1358 
1359 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x60, 5, H2C_Parameter);
1360 }
1361 
1362 
1363 static void halbtc8723b1ant_PsTdma(
1364 	struct btc_coexist *pBtCoexist, bool bForceExec, bool bTurnOn, u8 type
1365 )
1366 {
1367 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
1368 	bool bWifiBusy = false;
1369 	u8 rssiAdjustVal = 0;
1370 	u8 psTdmaByte4Val = 0x50, psTdmaByte0Val = 0x51, psTdmaByte3Val =  0x10;
1371 	s8 nWiFiDurationAdjust = 0x0;
1372 	/* u32 		fwVer = 0; */
1373 
1374 	/* BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW, ("[BTCoex], %s turn %s PS TDMA, type =%d\n", */
1375 	/* 	(bForceExec? "force to":""), (bTurnOn? "ON":"OFF"), type)); */
1376 	pCoexDm->bCurPsTdmaOn = bTurnOn;
1377 	pCoexDm->curPsTdma = type;
1378 
1379 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
1380 
1381 	if (pCoexDm->bCurPsTdmaOn) {
1382 		BTC_PRINT(
1383 			BTC_MSG_ALGORITHM,
1384 			ALGO_TRACE,
1385 			(
1386 				"[BTCoex], ********** TDMA(on, %d) **********\n",
1387 				pCoexDm->curPsTdma
1388 			)
1389 		);
1390 	} else {
1391 		BTC_PRINT(
1392 			BTC_MSG_ALGORITHM,
1393 			ALGO_TRACE,
1394 			(
1395 				"[BTCoex], ********** TDMA(off, %d) **********\n",
1396 				pCoexDm->curPsTdma
1397 			)
1398 		);
1399 	}
1400 
1401 	if (!bForceExec) {
1402 		if (
1403 			(pCoexDm->bPrePsTdmaOn == pCoexDm->bCurPsTdmaOn) &&
1404 			(pCoexDm->prePsTdma == pCoexDm->curPsTdma)
1405 		)
1406 			return;
1407 	}
1408 
1409 	if (pCoexSta->nScanAPNum <= 5)
1410 		nWiFiDurationAdjust = 5;
1411 	else if  (pCoexSta->nScanAPNum >= 40)
1412 		nWiFiDurationAdjust = -15;
1413 	else if  (pCoexSta->nScanAPNum >= 20)
1414 		nWiFiDurationAdjust = -10;
1415 
1416 	if (!pCoexSta->bForceLpsOn) { /* only for A2DP-only case 1/2/9/11 */
1417 		psTdmaByte0Val = 0x61;  /* no null-pkt */
1418 		psTdmaByte3Val = 0x11; /*  no tx-pause at BT-slot */
1419 		psTdmaByte4Val = 0x10; /*  0x778 = d/1 toggle */
1420 	}
1421 
1422 
1423 	if (bTurnOn) {
1424 		if (pBtLinkInfo->bSlaveRole)
1425 			psTdmaByte4Val = psTdmaByte4Val | 0x1;  /* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1426 
1427 
1428 		switch (type) {
1429 		default:
1430 			halbtc8723b1ant_SetFwPstdma(
1431 				pBtCoexist, 0x51, 0x1a, 0x1a, 0x0, psTdmaByte4Val
1432 			);
1433 			break;
1434 		case 1:
1435 			halbtc8723b1ant_SetFwPstdma(
1436 				pBtCoexist,
1437 				psTdmaByte0Val,
1438 				0x3a + nWiFiDurationAdjust,
1439 				0x03,
1440 				psTdmaByte3Val,
1441 				psTdmaByte4Val
1442 			);
1443 			break;
1444 		case 2:
1445 			halbtc8723b1ant_SetFwPstdma(
1446 				pBtCoexist,
1447 				psTdmaByte0Val,
1448 				0x2d + nWiFiDurationAdjust,
1449 				0x03,
1450 				psTdmaByte3Val,
1451 				psTdmaByte4Val
1452 			);
1453 			break;
1454 		case 3:
1455 			halbtc8723b1ant_SetFwPstdma(
1456 				pBtCoexist, 0x51, 0x1d, 0x1d, 0x0, 0x10
1457 			);
1458 			break;
1459 		case 4:
1460 			halbtc8723b1ant_SetFwPstdma(
1461 				pBtCoexist, 0x93, 0x15, 0x3, 0x14, 0x0
1462 			);
1463 			break;
1464 		case 5:
1465 			halbtc8723b1ant_SetFwPstdma(
1466 				pBtCoexist, 0x61, 0x15, 0x3, 0x11, 0x10
1467 			);
1468 			break;
1469 		case 6:
1470 			halbtc8723b1ant_SetFwPstdma(
1471 				pBtCoexist, 0x61, 0x20, 0x3, 0x11, 0x11
1472 			);
1473 			break;
1474 		case 7:
1475 			halbtc8723b1ant_SetFwPstdma(pBtCoexist, 0x13, 0xc, 0x5, 0x0, 0x0);
1476 			break;
1477 		case 8:
1478 			halbtc8723b1ant_SetFwPstdma(
1479 				pBtCoexist, 0x93, 0x25, 0x3, 0x10, 0x0
1480 			);
1481 			break;
1482 		case 9:
1483 			halbtc8723b1ant_SetFwPstdma(
1484 				pBtCoexist,
1485 				psTdmaByte0Val,
1486 				0x21,
1487 				0x3,
1488 				psTdmaByte3Val,
1489 				psTdmaByte4Val
1490 			);
1491 			break;
1492 		case 10:
1493 			halbtc8723b1ant_SetFwPstdma(pBtCoexist, 0x13, 0xa, 0xa, 0x0, 0x40);
1494 			break;
1495 		case 11:
1496 			halbtc8723b1ant_SetFwPstdma(
1497 				pBtCoexist,
1498 				psTdmaByte0Val,
1499 				0x21,
1500 				0x03,
1501 				psTdmaByte3Val,
1502 				psTdmaByte4Val
1503 			);
1504 			break;
1505 		case 12:
1506 			halbtc8723b1ant_SetFwPstdma(
1507 				pBtCoexist, 0x51, 0x0a, 0x0a, 0x0, 0x50
1508 			);
1509 			break;
1510 		case 13:
1511 			halbtc8723b1ant_SetFwPstdma(
1512 				pBtCoexist, 0x51, 0x12, 0x12, 0x0, 0x10
1513 			);
1514 			break;
1515 		case 14:
1516 			halbtc8723b1ant_SetFwPstdma(
1517 				pBtCoexist, 0x51, 0x21, 0x3, 0x10, psTdmaByte4Val
1518 			);
1519 			break;
1520 		case 15:
1521 			halbtc8723b1ant_SetFwPstdma(
1522 				pBtCoexist, 0x13, 0xa, 0x3, 0x8, 0x0
1523 			);
1524 			break;
1525 		case 16:
1526 			halbtc8723b1ant_SetFwPstdma(
1527 				pBtCoexist, 0x93, 0x15, 0x3, 0x10, 0x0
1528 			);
1529 			break;
1530 		case 18:
1531 			halbtc8723b1ant_SetFwPstdma(
1532 				pBtCoexist, 0x93, 0x25, 0x3, 0x10, 0x0
1533 			);
1534 			break;
1535 		case 20:
1536 			halbtc8723b1ant_SetFwPstdma(
1537 				pBtCoexist, 0x61, 0x3f, 0x03, 0x11, 0x10
1538 
1539 			);
1540 			break;
1541 		case 21:
1542 			halbtc8723b1ant_SetFwPstdma(
1543 				pBtCoexist, 0x61, 0x25, 0x03, 0x11, 0x11
1544 			);
1545 			break;
1546 		case 22:
1547 			halbtc8723b1ant_SetFwPstdma(
1548 				pBtCoexist, 0x61, 0x25, 0x03, 0x11, 0x10
1549 			);
1550 			break;
1551 		case 23:
1552 			halbtc8723b1ant_SetFwPstdma(
1553 				pBtCoexist, 0xe3, 0x25, 0x3, 0x31, 0x18
1554 			);
1555 			break;
1556 		case 24:
1557 			halbtc8723b1ant_SetFwPstdma(
1558 				pBtCoexist, 0xe3, 0x15, 0x3, 0x31, 0x18
1559 			);
1560 			break;
1561 		case 25:
1562 			halbtc8723b1ant_SetFwPstdma(
1563 				pBtCoexist, 0xe3, 0xa, 0x3, 0x31, 0x18
1564 			);
1565 			break;
1566 		case 26:
1567 			halbtc8723b1ant_SetFwPstdma(
1568 				pBtCoexist, 0xe3, 0xa, 0x3, 0x31, 0x18
1569 			);
1570 			break;
1571 		case 27:
1572 			halbtc8723b1ant_SetFwPstdma(
1573 				pBtCoexist, 0xe3, 0x25, 0x3, 0x31, 0x98
1574 			);
1575 			break;
1576 		case 28:
1577 			halbtc8723b1ant_SetFwPstdma(
1578 				pBtCoexist, 0x69, 0x25, 0x3, 0x31, 0x0
1579 			);
1580 			break;
1581 		case 29:
1582 			halbtc8723b1ant_SetFwPstdma(
1583 				pBtCoexist, 0xab, 0x1a, 0x1a, 0x1, 0x10
1584 			);
1585 			break;
1586 		case 30:
1587 			halbtc8723b1ant_SetFwPstdma(
1588 				pBtCoexist, 0x51, 0x30, 0x3, 0x10, 0x10
1589 			);
1590 			break;
1591 		case 31:
1592 			halbtc8723b1ant_SetFwPstdma(
1593 				pBtCoexist, 0xd3, 0x1a, 0x1a, 0x0, 0x58
1594 			);
1595 			break;
1596 		case 32:
1597 			halbtc8723b1ant_SetFwPstdma(
1598 				pBtCoexist, 0x61, 0x35, 0x3, 0x11, 0x11
1599 			);
1600 			break;
1601 		case 33:
1602 			halbtc8723b1ant_SetFwPstdma(
1603 				pBtCoexist, 0xa3, 0x25, 0x3, 0x30, 0x90
1604 			);
1605 			break;
1606 		case 34:
1607 			halbtc8723b1ant_SetFwPstdma(
1608 				pBtCoexist, 0x53, 0x1a, 0x1a, 0x0, 0x10
1609 			);
1610 			break;
1611 		case 35:
1612 			halbtc8723b1ant_SetFwPstdma(
1613 				pBtCoexist, 0x63, 0x1a, 0x1a, 0x0, 0x10
1614 			);
1615 			break;
1616 		case 36:
1617 			halbtc8723b1ant_SetFwPstdma(
1618 				pBtCoexist, 0xd3, 0x12, 0x3, 0x14, 0x50
1619 			);
1620 			break;
1621 		case 40: /*  SoftAP only with no sta associated, BT disable , TDMA mode for power saving */
1622 			/* here softap mode screen off will cost 70-80mA for phone */
1623 			halbtc8723b1ant_SetFwPstdma(
1624 				pBtCoexist, 0x23, 0x18, 0x00, 0x10, 0x24
1625 			);
1626 			break;
1627 		}
1628 	} else {
1629 
1630 		/*  disable PS tdma */
1631 		switch (type) {
1632 		case 8: /* PTA Control */
1633 			halbtc8723b1ant_SetFwPstdma(pBtCoexist, 0x8, 0x0, 0x0, 0x0, 0x0);
1634 			halbtc8723b1ant_SetAntPath(
1635 				pBtCoexist, BTC_ANT_PATH_PTA, false, false
1636 			);
1637 			break;
1638 		case 0:
1639 		default:  /* Software control, Antenna at BT side */
1640 			halbtc8723b1ant_SetFwPstdma(pBtCoexist, 0x0, 0x0, 0x0, 0x0, 0x0);
1641 			halbtc8723b1ant_SetAntPath(
1642 				pBtCoexist, BTC_ANT_PATH_BT, false, false
1643 			);
1644 			break;
1645 		case 9:   /* Software control, Antenna at WiFi side */
1646 			halbtc8723b1ant_SetFwPstdma(pBtCoexist, 0x0, 0x0, 0x0, 0x0, 0x0);
1647 			halbtc8723b1ant_SetAntPath(
1648 				pBtCoexist, BTC_ANT_PATH_WIFI, false, false
1649 			);
1650 			break;
1651 		}
1652 	}
1653 
1654 	rssiAdjustVal = 0;
1655 	pBtCoexist->fBtcSet(
1656 		pBtCoexist, BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssiAdjustVal
1657 	);
1658 
1659 	/*  update pre state */
1660 	pCoexDm->bPrePsTdmaOn = pCoexDm->bCurPsTdmaOn;
1661 	pCoexDm->prePsTdma = pCoexDm->curPsTdma;
1662 }
1663 
1664 static bool halbtc8723b1ant_IsCommonAction(struct btc_coexist *pBtCoexist)
1665 {
1666 	bool bCommon = false, bWifiConnected = false, bWifiBusy = false;
1667 
1668 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
1669 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
1670 
1671 	if (
1672 		!bWifiConnected &&
1673 		BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus
1674 	) {
1675 		BTC_PRINT(
1676 			BTC_MSG_ALGORITHM,
1677 			ALGO_TRACE,
1678 			("[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n")
1679 		);
1680 
1681 		/* halbtc8723b1ant_SwMechanism(pBtCoexist, false); */
1682 
1683 		bCommon = true;
1684 	} else if (
1685 		bWifiConnected &&
1686 		(BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus)
1687 	) {
1688 		BTC_PRINT(
1689 			BTC_MSG_ALGORITHM,
1690 			ALGO_TRACE,
1691 			("[BTCoex], Wifi connected + BT non connected-idle!!\n")
1692 		);
1693 
1694 		/* halbtc8723b1ant_SwMechanism(pBtCoexist, false); */
1695 
1696 		bCommon = true;
1697 	} else if (
1698 		!bWifiConnected &&
1699 		(BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus)
1700 	) {
1701 		BTC_PRINT(
1702 			BTC_MSG_ALGORITHM,
1703 			ALGO_TRACE,
1704 			("[BTCoex], Wifi non connected-idle + BT connected-idle!!\n")
1705 		);
1706 
1707 		/* halbtc8723b1ant_SwMechanism(pBtCoexist, false); */
1708 
1709 		bCommon = true;
1710 	} else if (
1711 		bWifiConnected &&
1712 		(BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus)
1713 	) {
1714 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Wifi connected + BT connected-idle!!\n"));
1715 
1716 		/* halbtc8723b1ant_SwMechanism(pBtCoexist, false); */
1717 
1718 		bCommon = true;
1719 	} else if (
1720 		!bWifiConnected &&
1721 		(BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE != pCoexDm->btStatus)
1722 	) {
1723 		BTC_PRINT(
1724 			BTC_MSG_ALGORITHM,
1725 			ALGO_TRACE,
1726 			("[BTCoex], Wifi non connected-idle + BT Busy!!\n")
1727 		);
1728 
1729 		/* halbtc8723b1ant_SwMechanism(pBtCoexist, false); */
1730 
1731 		bCommon = true;
1732 	} else {
1733 		if (bWifiBusy) {
1734 			BTC_PRINT(
1735 				BTC_MSG_ALGORITHM,
1736 				ALGO_TRACE,
1737 				("[BTCoex], Wifi Connected-Busy + BT Busy!!\n")
1738 			);
1739 		} else {
1740 			BTC_PRINT(
1741 				BTC_MSG_ALGORITHM,
1742 				ALGO_TRACE,
1743 				("[BTCoex], Wifi Connected-Idle + BT Busy!!\n")
1744 			);
1745 		}
1746 
1747 		bCommon = false;
1748 	}
1749 
1750 	return bCommon;
1751 }
1752 
1753 
1754 static void halbtc8723b1ant_TdmaDurationAdjustForAcl(
1755 	struct btc_coexist *pBtCoexist, u8 wifiStatus
1756 )
1757 {
1758 	static s32 up, dn, m, n, WaitCount;
1759 	s32 result;   /* 0: no change, +1: increase WiFi duration, -1: decrease WiFi duration */
1760 	u8 retryCount = 0, btInfoExt;
1761 
1762 	BTC_PRINT(
1763 		BTC_MSG_ALGORITHM,
1764 		ALGO_TRACE_FW,
1765 		("[BTCoex], TdmaDurationAdjustForAcl()\n")
1766 	);
1767 
1768 	if (
1769 		(BT_8723B_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN == wifiStatus) ||
1770 		(BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN == wifiStatus) ||
1771 		(BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SPECIAL_PKT == wifiStatus)
1772 	) {
1773 		if (
1774 			pCoexDm->curPsTdma != 1 &&
1775 			pCoexDm->curPsTdma != 2 &&
1776 			pCoexDm->curPsTdma != 3 &&
1777 			pCoexDm->curPsTdma != 9
1778 		) {
1779 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1780 			pCoexDm->psTdmaDuAdjType = 9;
1781 
1782 			up = 0;
1783 			dn = 0;
1784 			m = 1;
1785 			n = 3;
1786 			result = 0;
1787 			WaitCount = 0;
1788 		}
1789 		return;
1790 	}
1791 
1792 	if (!pCoexDm->bAutoTdmaAdjust) {
1793 		pCoexDm->bAutoTdmaAdjust = true;
1794 		BTC_PRINT(
1795 			BTC_MSG_ALGORITHM,
1796 			ALGO_TRACE_FW_DETAIL,
1797 			("[BTCoex], first run TdmaDurationAdjust()!!\n")
1798 		);
1799 
1800 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
1801 		pCoexDm->psTdmaDuAdjType = 2;
1802 		/*  */
1803 		up = 0;
1804 		dn = 0;
1805 		m = 1;
1806 		n = 3;
1807 		result = 0;
1808 		WaitCount = 0;
1809 	} else {
1810 		/* acquire the BT TRx retry count from BT_Info byte2 */
1811 		retryCount = pCoexSta->btRetryCnt;
1812 		btInfoExt = pCoexSta->btInfoExt;
1813 		/* BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], retryCount = %d\n", retryCount)); */
1814 		/* BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], up =%d, dn =%d, m =%d, n =%d, WaitCount =%d\n", */
1815 		/* 	up, dn, m, n, WaitCount)); */
1816 
1817 		if (pCoexSta->lowPriorityTx > 1050 || pCoexSta->lowPriorityRx > 1250)
1818 			retryCount++;
1819 
1820 		result = 0;
1821 		WaitCount++;
1822 
1823 		if (retryCount == 0) { /*  no retry in the last 2-second duration */
1824 			up++;
1825 			dn--;
1826 
1827 			if (dn <= 0)
1828 				dn = 0;
1829 
1830 			if (up >= n) { /*  if 連續 n 個2秒 retry count為0, 則調寬WiFi duration */
1831 				WaitCount = 0;
1832 				n = 3;
1833 				up = 0;
1834 				dn = 0;
1835 				result = 1;
1836 				BTC_PRINT(
1837 					BTC_MSG_ALGORITHM,
1838 					ALGO_TRACE_FW_DETAIL,
1839 					("[BTCoex], Increase wifi duration!!\n")
1840 				);
1841 			}
1842 		} else if (retryCount <= 3) { /*  <=3 retry in the last 2-second duration */
1843 			up--;
1844 			dn++;
1845 
1846 			if (up <= 0)
1847 				up = 0;
1848 
1849 			if (dn == 2) { /*  if 連續 2 個2秒 retry count< 3, 則調窄WiFi duration */
1850 				if (WaitCount <= 2)
1851 					m++; /*  避免一直在兩個level中來回 */
1852 				else
1853 					m = 1;
1854 
1855 				if (m >= 20) /* m 最大值 = 20 ' 最大120秒 recheck是否調整 WiFi duration. */
1856 					m = 20;
1857 
1858 				n = 3 * m;
1859 				up = 0;
1860 				dn = 0;
1861 				WaitCount = 0;
1862 				result = -1;
1863 				BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], Decrease wifi duration for retryCounter<3!!\n"));
1864 			}
1865 		} else { /* retry count > 3, 只要1次 retry count > 3, 則調窄WiFi duration */
1866 			if (WaitCount == 1)
1867 				m++; /*  避免一直在兩個level中來回 */
1868 			else
1869 				m = 1;
1870 
1871 			if (m >= 20) /* m 最大值 = 20 ' 最大120秒 recheck是否調整 WiFi duration. */
1872 				m = 20;
1873 
1874 			n = 3 * m;
1875 			up = 0;
1876 			dn = 0;
1877 			WaitCount = 0;
1878 			result = -1;
1879 			BTC_PRINT(
1880 				BTC_MSG_ALGORITHM,
1881 				ALGO_TRACE_FW_DETAIL,
1882 				("[BTCoex], Decrease wifi duration for retryCounter>3!!\n")
1883 			);
1884 		}
1885 
1886 		if (result == -1) {
1887 			if (
1888 				BT_INFO_8723B_1ANT_A2DP_BASIC_RATE(btInfoExt) &&
1889 				((pCoexDm->curPsTdma == 1) || (pCoexDm->curPsTdma == 2))
1890 			) {
1891 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1892 				pCoexDm->psTdmaDuAdjType = 9;
1893 			} else if (pCoexDm->curPsTdma == 1) {
1894 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
1895 				pCoexDm->psTdmaDuAdjType = 2;
1896 			} else if (pCoexDm->curPsTdma == 2) {
1897 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1898 				pCoexDm->psTdmaDuAdjType = 9;
1899 			} else if (pCoexDm->curPsTdma == 9) {
1900 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 11);
1901 				pCoexDm->psTdmaDuAdjType = 11;
1902 			}
1903 		} else if (result == 1) {
1904 			if (
1905 				BT_INFO_8723B_1ANT_A2DP_BASIC_RATE(btInfoExt) &&
1906 				((pCoexDm->curPsTdma == 1) || (pCoexDm->curPsTdma == 2))
1907 			) {
1908 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1909 				pCoexDm->psTdmaDuAdjType = 9;
1910 			} else if (pCoexDm->curPsTdma == 11) {
1911 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1912 				pCoexDm->psTdmaDuAdjType = 9;
1913 			} else if (pCoexDm->curPsTdma == 9) {
1914 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
1915 				pCoexDm->psTdmaDuAdjType = 2;
1916 			} else if (pCoexDm->curPsTdma == 2) {
1917 				halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 1);
1918 				pCoexDm->psTdmaDuAdjType = 1;
1919 			}
1920 		} else {	  /* no change */
1921 			BTC_PRINT(
1922 				BTC_MSG_ALGORITHM,
1923 				ALGO_TRACE_FW_DETAIL,
1924 				(
1925 					"[BTCoex], ********** TDMA(on, %d) **********\n",
1926 					pCoexDm->curPsTdma
1927 				)
1928 			);
1929 		}
1930 
1931 		if (
1932 			pCoexDm->curPsTdma != 1 &&
1933 			pCoexDm->curPsTdma != 2 &&
1934 			pCoexDm->curPsTdma != 9 &&
1935 			pCoexDm->curPsTdma != 11
1936 		) /*  recover to previous adjust type */
1937 			halbtc8723b1ant_PsTdma(
1938 				pBtCoexist, NORMAL_EXEC, true, pCoexDm->psTdmaDuAdjType
1939 			);
1940 	}
1941 }
1942 
1943 static void halbtc8723b1ant_PsTdmaCheckForPowerSaveState(
1944 	struct btc_coexist *pBtCoexist, bool bNewPsState
1945 )
1946 {
1947 	u8 lpsMode = 0x0;
1948 
1949 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_LPS_MODE, &lpsMode);
1950 
1951 	if (lpsMode) {	/*  already under LPS state */
1952 		if (bNewPsState) {
1953 			/*  keep state under LPS, do nothing. */
1954 		} else /*  will leave LPS state, turn off psTdma first */
1955 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
1956 	} else {						/*  NO PS state */
1957 		if (bNewPsState) /*  will enter LPS state, turn off psTdma first */
1958 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
1959 		else {
1960 			/*  keep state under NO PS state, do nothing. */
1961 		}
1962 	}
1963 }
1964 
1965 static void halbtc8723b1ant_PowerSaveState(
1966 	struct btc_coexist *pBtCoexist, u8 psType, u8 lpsVal, u8 rpwmVal
1967 )
1968 {
1969 	bool bLowPwrDisable = false;
1970 
1971 	switch (psType) {
1972 	case BTC_PS_WIFI_NATIVE:
1973 		/*  recover to original 32k low power setting */
1974 		bLowPwrDisable = false;
1975 		pBtCoexist->fBtcSet(
1976 			pBtCoexist, BTC_SET_ACT_DISABLE_LOW_POWER, &bLowPwrDisable
1977 		);
1978 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1979 		pCoexSta->bForceLpsOn = false;
1980 		break;
1981 	case BTC_PS_LPS_ON:
1982 		halbtc8723b1ant_PsTdmaCheckForPowerSaveState(pBtCoexist, true);
1983 		halbtc8723b1ant_LpsRpwm(pBtCoexist, NORMAL_EXEC, lpsVal, rpwmVal);
1984 		/*  when coex force to enter LPS, do not enter 32k low power. */
1985 		bLowPwrDisable = true;
1986 		pBtCoexist->fBtcSet(
1987 			pBtCoexist, BTC_SET_ACT_DISABLE_LOW_POWER, &bLowPwrDisable
1988 		);
1989 		/*  power save must executed before psTdma. */
1990 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1991 		pCoexSta->bForceLpsOn = true;
1992 		break;
1993 	case BTC_PS_LPS_OFF:
1994 		halbtc8723b1ant_PsTdmaCheckForPowerSaveState(pBtCoexist, false);
1995 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1996 		pCoexSta->bForceLpsOn = false;
1997 		break;
1998 	default:
1999 		break;
2000 	}
2001 }
2002 
2003 /*  */
2004 /*  */
2005 /* 	Software Coex Mechanism start */
2006 /*  */
2007 /*  */
2008 
2009 /*  */
2010 /*  */
2011 /* 	Non-Software Coex Mechanism start */
2012 /*  */
2013 /*  */
2014 static void halbtc8723b1ant_ActionWifiMultiPort(struct btc_coexist *pBtCoexist)
2015 {
2016 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2017 
2018 	halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2019 	halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2020 }
2021 
2022 static void halbtc8723b1ant_ActionHs(struct btc_coexist *pBtCoexist)
2023 {
2024 	halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 5);
2025 	halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2026 }
2027 
2028 static void halbtc8723b1ant_ActionBtInquiry(struct btc_coexist *pBtCoexist)
2029 {
2030 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2031 	bool bWifiConnected = false;
2032 	bool bApEnable = false;
2033 	bool bWifiBusy = false;
2034 	bool bBtBusy = false;
2035 
2036 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
2037 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2038 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2039 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
2040 
2041 	if (!bWifiConnected && !pCoexSta->bWiFiIsHighPriTask) {
2042 		halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2043 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2044 
2045 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2046 	} else if (
2047 		pBtLinkInfo->bScoExist ||
2048 		pBtLinkInfo->bHidExist ||
2049 		pBtLinkInfo->bA2dpExist
2050 	) {
2051 		/*  SCO/HID/A2DP busy */
2052 		halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2053 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2054 
2055 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2056 	} else if (pBtLinkInfo->bPanExist || bWifiBusy) {
2057 		halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2058 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2059 
2060 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2061 	} else {
2062 		halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2063 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2064 
2065 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2066 	}
2067 }
2068 
2069 static void halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2070 	struct btc_coexist *pBtCoexist, u8 wifiStatus
2071 )
2072 {
2073 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2074 	bool bWifiConnected = false;
2075 
2076 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2077 
2078 	/*  tdma and coex table */
2079 
2080 	if (pBtLinkInfo->bScoExist) {
2081 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 5);
2082 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2083 	} else { /* HID */
2084 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 6);
2085 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2086 	}
2087 }
2088 
2089 static void halbtc8723b1ant_ActionWifiConnectedBtAclBusy(
2090 	struct btc_coexist *pBtCoexist, u8 wifiStatus
2091 )
2092 {
2093 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2094 	halbtc8723b1ant_BtRssiState(2, 28, 0);
2095 
2096 	if ((pCoexSta->lowPriorityRx >= 1000) && (pCoexSta->lowPriorityRx != 65535))
2097 		pBtLinkInfo->bSlaveRole = true;
2098 	else
2099 		pBtLinkInfo->bSlaveRole = false;
2100 
2101 	if (pBtLinkInfo->bHidOnly) { /* HID */
2102 		halbtc8723b1ant_ActionBtScoHidOnlyBusy(pBtCoexist, wifiStatus);
2103 		pCoexDm->bAutoTdmaAdjust = false;
2104 		return;
2105 	} else if (pBtLinkInfo->bA2dpOnly) { /* A2DP */
2106 		if (BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE == wifiStatus) {
2107 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2108 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2109 			pCoexDm->bAutoTdmaAdjust = false;
2110 		} else {
2111 			halbtc8723b1ant_TdmaDurationAdjustForAcl(pBtCoexist, wifiStatus);
2112 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2113 			pCoexDm->bAutoTdmaAdjust = true;
2114 		}
2115 	} else if (pBtLinkInfo->bHidExist && pBtLinkInfo->bA2dpExist) { /* HID+A2DP */
2116 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 14);
2117 		pCoexDm->bAutoTdmaAdjust = false;
2118 
2119 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2120 	} else if (
2121 		pBtLinkInfo->bPanOnly ||
2122 		(pBtLinkInfo->bHidExist && pBtLinkInfo->bPanExist)
2123 	) { /* PAN(OPP, FTP), HID+PAN(OPP, FTP) */
2124 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 3);
2125 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2126 		pCoexDm->bAutoTdmaAdjust = false;
2127 	} else if (
2128 		(pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist) ||
2129 		(pBtLinkInfo->bHidExist && pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist)
2130 	) { /* A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP) */
2131 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 13);
2132 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2133 		pCoexDm->bAutoTdmaAdjust = false;
2134 	} else {
2135 		/* BT no-profile busy (0x9) */
2136 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2137 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2138 		pCoexDm->bAutoTdmaAdjust = false;
2139 	}
2140 }
2141 
2142 static void halbtc8723b1ant_ActionWifiNotConnected(struct btc_coexist *pBtCoexist)
2143 {
2144 	/*  power save state */
2145 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2146 
2147 	/*  tdma and coex table */
2148 	halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 8);
2149 	halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2150 }
2151 
2152 static void halbtc8723b1ant_ActionWifiNotConnectedScan(
2153 	struct btc_coexist *pBtCoexist
2154 )
2155 {
2156 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2157 
2158 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2159 
2160 	/*  tdma and coex table */
2161 	if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2162 		if (pBtLinkInfo->bA2dpExist) {
2163 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2164 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2165 		} else if (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist) {
2166 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 22);
2167 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2168 		} else {
2169 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2170 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2171 		}
2172 	} else if (
2173 		(BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2174 		(BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2175 	) {
2176 		halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2177 			pBtCoexist, BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN
2178 		);
2179 	} else {
2180 		/* Bryant Add */
2181 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2182 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2183 	}
2184 }
2185 
2186 static void halbtc8723b1ant_ActionWifiNotConnectedAssoAuth(
2187 	struct btc_coexist *pBtCoexist
2188 )
2189 {
2190 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2191 
2192 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2193 
2194 	/*  tdma and coex table */
2195 	if (
2196 		(pBtLinkInfo->bScoExist) ||
2197 		(pBtLinkInfo->bHidExist) ||
2198 		(pBtLinkInfo->bA2dpExist)
2199 	) {
2200 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2201 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2202 	} else if (pBtLinkInfo->bPanExist) {
2203 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2204 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2205 	} else {
2206 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2207 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2208 	}
2209 }
2210 
2211 static void halbtc8723b1ant_ActionWifiConnectedScan(struct btc_coexist *pBtCoexist)
2212 {
2213 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2214 
2215 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2216 
2217 	/*  tdma and coex table */
2218 	if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2219 		if (pBtLinkInfo->bA2dpExist) {
2220 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2221 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2222 		} else if (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist) {
2223 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 22);
2224 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2225 		} else {
2226 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2227 			halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2228 		}
2229 	} else if (
2230 		(BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2231 		(BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2232 	) {
2233 		halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2234 			pBtCoexist, BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN
2235 		);
2236 	} else {
2237 		/* Bryant Add */
2238 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2239 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2240 	}
2241 }
2242 
2243 static void halbtc8723b1ant_ActionWifiConnectedSpecialPacket(
2244 	struct btc_coexist *pBtCoexist
2245 )
2246 {
2247 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2248 
2249 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2250 
2251 	/*  tdma and coex table */
2252 	if (
2253 		(pBtLinkInfo->bScoExist) ||
2254 		(pBtLinkInfo->bHidExist) ||
2255 		(pBtLinkInfo->bA2dpExist)
2256 	) {
2257 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2258 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2259 	} else if (pBtLinkInfo->bPanExist) {
2260 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2261 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2262 	} else {
2263 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2264 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2265 	}
2266 }
2267 
2268 static void halbtc8723b1ant_ActionWifiConnected(struct btc_coexist *pBtCoexist)
2269 {
2270 	bool bWifiBusy = false;
2271 	bool bScan = false, bLink = false, bRoam = false;
2272 	bool bUnder4way = false, bApEnable = false;
2273 
2274 	BTC_PRINT(
2275 		BTC_MSG_ALGORITHM,
2276 		ALGO_TRACE,
2277 		("[BTCoex], CoexForWifiConnect() ===>\n")
2278 	);
2279 
2280 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &bUnder4way);
2281 	if (bUnder4way) {
2282 		halbtc8723b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
2283 		BTC_PRINT(
2284 			BTC_MSG_ALGORITHM,
2285 			ALGO_TRACE,
2286 			("[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n")
2287 		);
2288 		return;
2289 	}
2290 
2291 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2292 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2293 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2294 	if (bScan || bLink || bRoam) {
2295 		if (bScan)
2296 			halbtc8723b1ant_ActionWifiConnectedScan(pBtCoexist);
2297 		else
2298 			halbtc8723b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
2299 		BTC_PRINT(
2300 			BTC_MSG_ALGORITHM,
2301 			ALGO_TRACE,
2302 			("[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n")
2303 		);
2304 		return;
2305 	}
2306 
2307 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
2308 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2309 
2310 	/*  power save state */
2311 	if (
2312 		!bApEnable &&
2313 		BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus &&
2314 		!pBtCoexist->btLinkInfo.bHidOnly
2315 	) {
2316 		if (pBtCoexist->btLinkInfo.bA2dpOnly) { /* A2DP */
2317 			if (!bWifiBusy)
2318 				halbtc8723b1ant_PowerSaveState(
2319 					pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0
2320 				);
2321 			else { /* busy */
2322 				if  (pCoexSta->nScanAPNum >= BT_8723B_1ANT_WIFI_NOISY_THRESH)  /* no force LPS, no PS-TDMA, use pure TDMA */
2323 					halbtc8723b1ant_PowerSaveState(
2324 						pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0
2325 					);
2326 				else
2327 					halbtc8723b1ant_PowerSaveState(
2328 						pBtCoexist, BTC_PS_LPS_ON, 0x50, 0x4
2329 					);
2330 			}
2331 		} else if (
2332 			(!pCoexSta->bPanExist) &&
2333 			(!pCoexSta->bA2dpExist) &&
2334 			(!pCoexSta->bHidExist)
2335 		)
2336 			halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2337 		else
2338 			halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_LPS_ON, 0x50, 0x4);
2339 	} else
2340 		halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2341 
2342 	/*  tdma and coex table */
2343 	if (!bWifiBusy) {
2344 		if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2345 			halbtc8723b1ant_ActionWifiConnectedBtAclBusy(
2346 				pBtCoexist,
2347 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE
2348 			);
2349 		} else if (
2350 			(BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2351 			(BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2352 		) {
2353 			halbtc8723b1ant_ActionBtScoHidOnlyBusy(pBtCoexist,
2354 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2355 		} else {
2356 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2357 
2358 			if ((pCoexSta->highPriorityTx) + (pCoexSta->highPriorityRx) <= 60)
2359 				halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2360 			else
2361 				halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2362 		}
2363 	} else {
2364 		if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2365 			halbtc8723b1ant_ActionWifiConnectedBtAclBusy(
2366 				pBtCoexist,
2367 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY
2368 			);
2369 		} else if (
2370 			(BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2371 			(BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2372 		) {
2373 			halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2374 				pBtCoexist,
2375 				BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY
2376 			);
2377 		} else {
2378 			halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2379 
2380 			if ((pCoexSta->highPriorityTx) + (pCoexSta->highPriorityRx) <= 60)
2381 				halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2382 			else
2383 				halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2384 		}
2385 	}
2386 }
2387 
2388 static void halbtc8723b1ant_RunSwCoexistMechanism(struct btc_coexist *pBtCoexist)
2389 {
2390 	u8 algorithm = 0;
2391 
2392 	algorithm = halbtc8723b1ant_ActionAlgorithm(pBtCoexist);
2393 	pCoexDm->curAlgorithm = algorithm;
2394 
2395 	if (halbtc8723b1ant_IsCommonAction(pBtCoexist)) {
2396 
2397 	} else {
2398 		switch (pCoexDm->curAlgorithm) {
2399 		case BT_8723B_1ANT_COEX_ALGO_SCO:
2400 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = SCO.\n"));
2401 			/* halbtc8723b1ant_ActionSco(pBtCoexist); */
2402 			break;
2403 		case BT_8723B_1ANT_COEX_ALGO_HID:
2404 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HID.\n"));
2405 			/* halbtc8723b1ant_ActionHid(pBtCoexist); */
2406 			break;
2407 		case BT_8723B_1ANT_COEX_ALGO_A2DP:
2408 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = A2DP.\n"));
2409 			/* halbtc8723b1ant_ActionA2dp(pBtCoexist); */
2410 			break;
2411 		case BT_8723B_1ANT_COEX_ALGO_A2DP_PANHS:
2412 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = A2DP+PAN(HS).\n"));
2413 			/* halbtc8723b1ant_ActionA2dpPanHs(pBtCoexist); */
2414 			break;
2415 		case BT_8723B_1ANT_COEX_ALGO_PANEDR:
2416 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = PAN(EDR).\n"));
2417 			/* halbtc8723b1ant_ActionPanEdr(pBtCoexist); */
2418 			break;
2419 		case BT_8723B_1ANT_COEX_ALGO_PANHS:
2420 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HS mode.\n"));
2421 			/* halbtc8723b1ant_ActionPanHs(pBtCoexist); */
2422 			break;
2423 		case BT_8723B_1ANT_COEX_ALGO_PANEDR_A2DP:
2424 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = PAN+A2DP.\n"));
2425 			/* halbtc8723b1ant_ActionPanEdrA2dp(pBtCoexist); */
2426 			break;
2427 		case BT_8723B_1ANT_COEX_ALGO_PANEDR_HID:
2428 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = PAN(EDR)+HID.\n"));
2429 			/* halbtc8723b1ant_ActionPanEdrHid(pBtCoexist); */
2430 			break;
2431 		case BT_8723B_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2432 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HID+A2DP+PAN.\n"));
2433 			/* halbtc8723b1ant_ActionHidA2dpPanEdr(pBtCoexist); */
2434 			break;
2435 		case BT_8723B_1ANT_COEX_ALGO_HID_A2DP:
2436 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HID+A2DP.\n"));
2437 			/* halbtc8723b1ant_ActionHidA2dp(pBtCoexist); */
2438 			break;
2439 		default:
2440 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = coexist All Off!!\n"));
2441 			break;
2442 		}
2443 		pCoexDm->preAlgorithm = pCoexDm->curAlgorithm;
2444 	}
2445 }
2446 
2447 static void halbtc8723b1ant_RunCoexistMechanism(struct btc_coexist *pBtCoexist)
2448 {
2449 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2450 	bool bWifiConnected = false, bBtHsOn = false;
2451 	bool bIncreaseScanDevNum = false;
2452 	bool bBtCtrlAggBufSize = false;
2453 	u8 aggBufSize = 5;
2454 	u32 wifiLinkStatus = 0;
2455 	u32 numOfWifiLink = 0;
2456 
2457 	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], RunCoexistMechanism() ===>\n"));
2458 
2459 	if (pBtCoexist->bManualControl) {
2460 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n"));
2461 		return;
2462 	}
2463 
2464 	if (pBtCoexist->bStopCoexDm) {
2465 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n"));
2466 		return;
2467 	}
2468 
2469 	if (pCoexSta->bUnderIps) {
2470 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], wifi is under IPS !!!\n"));
2471 		return;
2472 	}
2473 
2474 	if (
2475 		(BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) ||
2476 		(BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2477 		(BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2478 	){
2479 		bIncreaseScanDevNum = true;
2480 	}
2481 
2482 	pBtCoexist->fBtcSet(
2483 		pBtCoexist,
2484 		BTC_SET_BL_INC_SCAN_DEV_NUM,
2485 		&bIncreaseScanDevNum
2486 	);
2487 	pBtCoexist->fBtcGet(
2488 		pBtCoexist,
2489 		BTC_GET_BL_WIFI_CONNECTED,
2490 		&bWifiConnected
2491 	);
2492 
2493 	pBtCoexist->fBtcGet(
2494 		pBtCoexist,
2495 		BTC_GET_U4_WIFI_LINK_STATUS,
2496 		&wifiLinkStatus
2497 	);
2498 	numOfWifiLink = wifiLinkStatus >> 16;
2499 
2500 	if ((numOfWifiLink >= 2) || (wifiLinkStatus & WIFI_P2P_GO_CONNECTED)) {
2501 		BTC_PRINT(
2502 			BTC_MSG_INTERFACE,
2503 			INTF_NOTIFY,
2504 			(
2505 				"############# [BTCoex],  Multi-Port numOfWifiLink = %d, wifiLinkStatus = 0x%x\n",
2506 				numOfWifiLink,
2507 				wifiLinkStatus
2508 			)
2509 		);
2510 		halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
2511 		halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize);
2512 
2513 		if ((pBtLinkInfo->bA2dpExist) && (pCoexSta->bC2hBtInquiryPage)) {
2514 			BTC_PRINT(
2515 				BTC_MSG_INTERFACE,
2516 				INTF_NOTIFY,
2517 				("############# [BTCoex],  BT Is Inquirying\n")
2518 			);
2519 			halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
2520 		} else
2521 			halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
2522 
2523 		return;
2524 	}
2525 
2526 	if ((pBtLinkInfo->bBtLinkExist) && (bWifiConnected)) {
2527 		halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 1, 1, 0, 1);
2528 
2529 		if (pBtLinkInfo->bScoExist)
2530 			halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, true, 0x5);
2531 		else
2532 			halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, true, 0x8);
2533 
2534 		halbtc8723b1ant_SwMechanism(pBtCoexist, true);
2535 		halbtc8723b1ant_RunSwCoexistMechanism(pBtCoexist);  /* just print debug message */
2536 	} else {
2537 		halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
2538 
2539 		halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, false, 0x5);
2540 
2541 		halbtc8723b1ant_SwMechanism(pBtCoexist, false);
2542 		halbtc8723b1ant_RunSwCoexistMechanism(pBtCoexist); /* just print debug message */
2543 	}
2544 
2545 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
2546 	if (pCoexSta->bC2hBtInquiryPage) {
2547 		BTC_PRINT(
2548 			BTC_MSG_INTERFACE,
2549 			INTF_NOTIFY,
2550 			("############# [BTCoex],  BT Is Inquirying\n")
2551 		);
2552 		halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
2553 		return;
2554 	} else if (bBtHsOn) {
2555 		halbtc8723b1ant_ActionHs(pBtCoexist);
2556 		return;
2557 	}
2558 
2559 
2560 	if (!bWifiConnected) {
2561 		bool bScan = false, bLink = false, bRoam = false;
2562 
2563 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], wifi is non connected-idle !!!\n"));
2564 
2565 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2566 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2567 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2568 
2569 		if (bScan || bLink || bRoam) {
2570 			 if (bScan)
2571 				halbtc8723b1ant_ActionWifiNotConnectedScan(pBtCoexist);
2572 			 else
2573 				halbtc8723b1ant_ActionWifiNotConnectedAssoAuth(pBtCoexist);
2574 		} else
2575 			halbtc8723b1ant_ActionWifiNotConnected(pBtCoexist);
2576 	} else /*  wifi LPS/Busy */
2577 		halbtc8723b1ant_ActionWifiConnected(pBtCoexist);
2578 }
2579 
2580 static void halbtc8723b1ant_InitCoexDm(struct btc_coexist *pBtCoexist)
2581 {
2582 	/*  force to reset coex mechanism */
2583 
2584 	/*  sw all off */
2585 	halbtc8723b1ant_SwMechanism(pBtCoexist, false);
2586 
2587 	/* halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 8); */
2588 	halbtc8723b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
2589 
2590 	pCoexSta->popEventCnt = 0;
2591 }
2592 
2593 static void halbtc8723b1ant_InitHwConfig(
2594 	struct btc_coexist *pBtCoexist,
2595 	bool bBackUp,
2596 	bool bWifiOnly
2597 )
2598 {
2599 	u32 u4Tmp = 0;/*  fwVer; */
2600 	u8 u1Tmpa = 0, u1Tmpb = 0;
2601 
2602 	BTC_PRINT(
2603 		BTC_MSG_INTERFACE,
2604 		INTF_INIT,
2605 		("[BTCoex], 1Ant Init HW Config!!\n")
2606 	);
2607 
2608 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x550, 0x8, 0x1);  /* enable TBTT nterrupt */
2609 
2610 	/*  0x790[5:0]= 0x5 */
2611 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x790, 0x5);
2612 
2613 	/*  Enable counter statistics */
2614 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x778, 0x1);
2615 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x40, 0x20, 0x1);
2616 
2617 	/* Antenna config */
2618 	if (bWifiOnly) {
2619 		halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_WIFI, true, false);
2620 		halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 9);
2621 	} else
2622 		halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, true, false);
2623 
2624 	/*  PTA parameter */
2625 	halbtc8723b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
2626 
2627 	u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
2628 	u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
2629 	u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
2630 
2631 	BTC_PRINT(
2632 		BTC_MSG_INTERFACE,
2633 		INTF_NOTIFY,
2634 		(
2635 			"############# [BTCoex], 0x948 = 0x%x, 0x765 = 0x%x, 0x67 = 0x%x\n",
2636 			u4Tmp,
2637 			u1Tmpa,
2638 			u1Tmpb
2639 		)
2640 	);
2641 }
2642 
2643 /*  */
2644 /*  work around function start with wa_halbtc8723b1ant_ */
2645 /*  */
2646 /*  */
2647 /*  extern function start with EXhalbtc8723b1ant_ */
2648 /*  */
2649 void EXhalbtc8723b1ant_PowerOnSetting(struct btc_coexist *pBtCoexist)
2650 {
2651 	struct btc_board_info *pBoardInfo = &pBtCoexist->boardInfo;
2652 	u8 u1Tmp = 0x0;
2653 	u16 u2Tmp = 0x0;
2654 
2655 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x67, 0x20);
2656 
2657 	/*  enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly. */
2658 	u2Tmp = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x2);
2659 	pBtCoexist->fBtcWrite2Byte(pBtCoexist, 0x2, u2Tmp | BIT0 | BIT1);
2660 
2661 	/*  set GRAN_BT = 1 */
2662 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
2663 	/*  set WLAN_ACT = 0 */
2664 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
2665 
2666 	/*  */
2667 	/*  S0 or S1 setting and Local register setting(By the setting fw can get ant number, S0/S1, ... info) */
2668 	/*  Local setting bit define */
2669 	/* 	BIT0: "0" for no antenna inverse; "1" for antenna inverse */
2670 	/* 	BIT1: "0" for internal switch; "1" for external switch */
2671 	/* 	BIT2: "0" for one antenna; "1" for two antenna */
2672 	/*  NOTE: here default all internal switch and 1-antenna ==> BIT1 = 0 and BIT2 = 0 */
2673 	if (pBtCoexist->chipInterface == BTC_INTF_USB) {
2674 		/*  fixed at S0 for USB interface */
2675 		pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
2676 
2677 		u1Tmp |= 0x1;	/*  antenna inverse */
2678 		pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0xfe08, u1Tmp);
2679 
2680 		pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_AUX_PORT;
2681 	} else {
2682 		/*  for PCIE and SDIO interface, we check efuse 0xc3[6] */
2683 		if (pBoardInfo->singleAntPath == 0) {
2684 			/*  set to S1 */
2685 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
2686 			pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT;
2687 		} else if (pBoardInfo->singleAntPath == 1) {
2688 			/*  set to S0 */
2689 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
2690 			u1Tmp |= 0x1;	/*  antenna inverse */
2691 			pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_AUX_PORT;
2692 		}
2693 
2694 		if (pBtCoexist->chipInterface == BTC_INTF_PCI)
2695 			pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0x384, u1Tmp);
2696 		else if (pBtCoexist->chipInterface == BTC_INTF_SDIO)
2697 			pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0x60, u1Tmp);
2698 	}
2699 }
2700 
2701 void EXhalbtc8723b1ant_InitHwConfig(struct btc_coexist *pBtCoexist, bool bWifiOnly)
2702 {
2703 	halbtc8723b1ant_InitHwConfig(pBtCoexist, true, bWifiOnly);
2704 }
2705 
2706 void EXhalbtc8723b1ant_InitCoexDm(struct btc_coexist *pBtCoexist)
2707 {
2708 	BTC_PRINT(
2709 		BTC_MSG_INTERFACE,
2710 		INTF_INIT,
2711 		("[BTCoex], Coex Mechanism Init!!\n")
2712 	);
2713 
2714 	pBtCoexist->bStopCoexDm = false;
2715 
2716 	halbtc8723b1ant_InitCoexDm(pBtCoexist);
2717 
2718 	halbtc8723b1ant_QueryBtInfo(pBtCoexist);
2719 }
2720 
2721 void EXhalbtc8723b1ant_DisplayCoexInfo(struct btc_coexist *pBtCoexist)
2722 {
2723 	struct btc_board_info *pBoardInfo = &pBtCoexist->boardInfo;
2724 	struct btc_stack_info *pStackInfo = &pBtCoexist->stackInfo;
2725 	struct btc_bt_link_info *pBtLinkInfo = &pBtCoexist->btLinkInfo;
2726 	u8 *cliBuf = pBtCoexist->cliBuf;
2727 	u8 u1Tmp[4], i, btInfoExt, psTdmaCase = 0;
2728 	u16 u2Tmp[4];
2729 	u32 u4Tmp[4];
2730 	bool bRoam = false;
2731 	bool bScan = false;
2732 	bool bLink = false;
2733 	bool bWifiUnder5G = false;
2734 	bool bWifiUnderBMode = false;
2735 	bool bBtHsOn = false;
2736 	bool bWifiBusy = false;
2737 	s32 wifiRssi = 0, btHsRssi = 0;
2738 	u32 wifiBw, wifiTrafficDir, faOfdm, faCck, wifiLinkStatus;
2739 	u8 wifiDot11Chnl, wifiHsChnl;
2740 	u32 fwVer = 0, btPatchVer = 0;
2741 	static u8 PopReportIn10s;
2742 
2743 	CL_SPRINTF(
2744 		cliBuf,
2745 		BT_TMP_BUF_SIZE,
2746 		"\r\n ============[BT Coexist info]============"
2747 	);
2748 	CL_PRINTF(cliBuf);
2749 
2750 	if (pBtCoexist->bManualControl) {
2751 		CL_SPRINTF(
2752 			cliBuf,
2753 			BT_TMP_BUF_SIZE,
2754 			"\r\n ============[Under Manual Control]============"
2755 		);
2756 		CL_PRINTF(cliBuf);
2757 		CL_SPRINTF(cliBuf,
2758 			BT_TMP_BUF_SIZE,
2759 			"\r\n =========================================="
2760 		);
2761 		CL_PRINTF(cliBuf);
2762 	}
2763 	if (pBtCoexist->bStopCoexDm) {
2764 		CL_SPRINTF(
2765 			cliBuf,
2766 			BT_TMP_BUF_SIZE,
2767 			"\r\n ============[Coex is STOPPED]============"
2768 		);
2769 		CL_PRINTF(cliBuf);
2770 		CL_SPRINTF(
2771 			cliBuf,
2772 			BT_TMP_BUF_SIZE,
2773 			"\r\n =========================================="
2774 		);
2775 		CL_PRINTF(cliBuf);
2776 	}
2777 
2778 	CL_SPRINTF(
2779 		cliBuf,
2780 		BT_TMP_BUF_SIZE,
2781 		"\r\n %-35s = %d/ %d/ %d", "Ant PG Num/ Ant Mech/ Ant Pos:", \
2782 		pBoardInfo->pgAntNum,
2783 		pBoardInfo->btdmAntNum,
2784 		pBoardInfo->btdmAntPos
2785 	);
2786 	CL_PRINTF(cliBuf);
2787 
2788 	CL_SPRINTF(
2789 		cliBuf,
2790 		BT_TMP_BUF_SIZE,
2791 		"\r\n %-35s = %s / %d", "BT stack/ hci ext ver", \
2792 		((pStackInfo->bProfileNotified) ? "Yes" : "No"),
2793 		pStackInfo->hciVersion
2794 	);
2795 	CL_PRINTF(cliBuf);
2796 
2797 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_BT_PATCH_VER, &btPatchVer);
2798 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);
2799 	CL_SPRINTF(
2800 		cliBuf,
2801 		BT_TMP_BUF_SIZE,
2802 		"\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)", "CoexVer/ FwVer/ PatchVer", \
2803 		GLCoexVerDate8723b1Ant,
2804 		GLCoexVer8723b1Ant,
2805 		fwVer,
2806 		btPatchVer,
2807 		btPatchVer
2808 	);
2809 	CL_PRINTF(cliBuf);
2810 
2811 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
2812 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_DOT11_CHNL, &wifiDot11Chnl);
2813 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifiHsChnl);
2814 	CL_SPRINTF(
2815 		cliBuf,
2816 		BT_TMP_BUF_SIZE,
2817 		"\r\n %-35s = %d / %d(%d)", "Dot11 channel / HsChnl(HsMode)", \
2818 		wifiDot11Chnl,
2819 		wifiHsChnl,
2820 		bBtHsOn
2821 	);
2822 	CL_PRINTF(cliBuf);
2823 
2824 	CL_SPRINTF(
2825 		cliBuf,
2826 		BT_TMP_BUF_SIZE,
2827 		"\r\n %-35s = %02x %02x %02x ", "H2C Wifi inform bt chnl Info", \
2828 		pCoexDm->wifiChnlInfo[0],
2829 		pCoexDm->wifiChnlInfo[1],
2830 		pCoexDm->wifiChnlInfo[2]
2831 	);
2832 	CL_PRINTF(cliBuf);
2833 
2834 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi);
2835 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_HS_RSSI, &btHsRssi);
2836 	CL_SPRINTF(
2837 		cliBuf,
2838 		BT_TMP_BUF_SIZE,
2839 		"\r\n %-35s = %d/ %d", "Wifi rssi/ HS rssi", \
2840 		wifiRssi - 100, btHsRssi - 100
2841 	);
2842 	CL_PRINTF(cliBuf);
2843 
2844 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2845 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2846 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2847 	CL_SPRINTF(
2848 		cliBuf,
2849 		BT_TMP_BUF_SIZE,
2850 		"\r\n %-35s = %d/ %d/ %d/ %s", "Wifi bLink/ bRoam/ bScan/ bHi-Pri", \
2851 		bLink, bRoam, bScan, ((pCoexSta->bWiFiIsHighPriTask) ? "1" : "0")
2852 	);
2853 	CL_PRINTF(cliBuf);
2854 
2855 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_5G, &bWifiUnder5G);
2856 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
2857 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2858 	pBtCoexist->fBtcGet(
2859 		pBtCoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION, &wifiTrafficDir
2860 	);
2861 	pBtCoexist->fBtcGet(
2862 		pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode
2863 	);
2864 
2865 	CL_SPRINTF(
2866 		cliBuf,
2867 		BT_TMP_BUF_SIZE,
2868 		"\r\n %-35s = %s / %s/ %s/ AP =%d/ %s ", "Wifi status", \
2869 		(bWifiUnder5G ? "5G" : "2.4G"),
2870 		((bWifiUnderBMode) ? "11b" : ((BTC_WIFI_BW_LEGACY == wifiBw) ? "11bg" : (((BTC_WIFI_BW_HT40 == wifiBw) ? "HT40" : "HT20")))),
2871 		((!bWifiBusy) ? "idle" : ((BTC_WIFI_TRAFFIC_TX == wifiTrafficDir) ? "uplink" : "downlink")),
2872 		pCoexSta->nScanAPNum,
2873 		(pCoexSta->bCCKLock) ? "Lock" : "noLock"
2874 	);
2875 	CL_PRINTF(cliBuf);
2876 
2877 	pBtCoexist->fBtcGet(
2878 		pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus
2879 	);
2880 	CL_SPRINTF(
2881 		cliBuf,
2882 		BT_TMP_BUF_SIZE,
2883 		"\r\n %-35s = %d/ %d/ %d/ %d/ %d", "sta/vwifi/hs/p2pGo/p2pGc", \
2884 		((wifiLinkStatus & WIFI_STA_CONNECTED) ? 1 : 0),
2885 		((wifiLinkStatus & WIFI_AP_CONNECTED) ? 1 : 0),
2886 		((wifiLinkStatus & WIFI_HS_CONNECTED) ? 1 : 0),
2887 		((wifiLinkStatus & WIFI_P2P_GO_CONNECTED) ? 1 : 0),
2888 		((wifiLinkStatus & WIFI_P2P_GC_CONNECTED) ? 1 : 0)
2889 	);
2890 	CL_PRINTF(cliBuf);
2891 
2892 
2893 	PopReportIn10s++;
2894 	CL_SPRINTF(
2895 		cliBuf,
2896 		BT_TMP_BUF_SIZE,
2897 		"\r\n %-35s = [%s/ %d/ %d/ %d] ", "BT [status/ rssi/ retryCnt/ popCnt]", \
2898 		((pBtCoexist->btInfo.bBtDisabled) ? ("disabled") : ((pCoexSta->bC2hBtInquiryPage) ? ("inquiry/page scan") : ((BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus) ? "non-connected idle" :
2899 		((BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus) ? "connected-idle" : "busy")))),
2900 		pCoexSta->btRssi, pCoexSta->btRetryCnt, pCoexSta->popEventCnt
2901 	);
2902 	CL_PRINTF(cliBuf);
2903 
2904 	if (PopReportIn10s >= 5) {
2905 		pCoexSta->popEventCnt = 0;
2906 		PopReportIn10s = 0;
2907 	}
2908 
2909 
2910 	CL_SPRINTF(
2911 		cliBuf,
2912 		BT_TMP_BUF_SIZE,
2913 		"\r\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP", \
2914 		pBtLinkInfo->bScoExist,
2915 		pBtLinkInfo->bHidExist,
2916 		pBtLinkInfo->bPanExist,
2917 		pBtLinkInfo->bA2dpExist
2918 	);
2919 	CL_PRINTF(cliBuf);
2920 
2921 	if (pStackInfo->bProfileNotified) {
2922 		pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_BT_LINK_INFO);
2923 	} else {
2924 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "BT Role", \
2925 		(pBtLinkInfo->bSlaveRole) ? "Slave" : "Master");
2926 		CL_PRINTF(cliBuf);
2927 	}
2928 
2929 
2930 	btInfoExt = pCoexSta->btInfoExt;
2931 	CL_SPRINTF(
2932 		cliBuf,
2933 		BT_TMP_BUF_SIZE,
2934 		"\r\n %-35s = %s", "BT Info A2DP rate", \
2935 		(btInfoExt & BIT0) ? "Basic rate" : "EDR rate"
2936 	);
2937 	CL_PRINTF(cliBuf);
2938 
2939 	for (i = 0; i < BT_INFO_SRC_8723B_1ANT_MAX; i++) {
2940 		if (pCoexSta->btInfoC2hCnt[i]) {
2941 			CL_SPRINTF(
2942 				cliBuf,
2943 				BT_TMP_BUF_SIZE,
2944 				"\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)", GLBtInfoSrc8723b1Ant[i], \
2945 				pCoexSta->btInfoC2h[i][0], pCoexSta->btInfoC2h[i][1],
2946 				pCoexSta->btInfoC2h[i][2], pCoexSta->btInfoC2h[i][3],
2947 				pCoexSta->btInfoC2h[i][4], pCoexSta->btInfoC2h[i][5],
2948 				pCoexSta->btInfoC2h[i][6], pCoexSta->btInfoC2hCnt[i]
2949 			);
2950 			CL_PRINTF(cliBuf);
2951 		}
2952 	}
2953 	CL_SPRINTF(
2954 		cliBuf,
2955 		BT_TMP_BUF_SIZE,
2956 		"\r\n %-35s = %s/%s, (0x%x/0x%x)", "PS state, IPS/LPS, (lps/rpwm)", \
2957 		(pCoexSta->bUnderIps ? "IPS ON" : "IPS OFF"),
2958 		(pCoexSta->bUnderLps ? "LPS ON" : "LPS OFF"),
2959 		pBtCoexist->btInfo.lpsVal,
2960 		pBtCoexist->btInfo.rpwmVal
2961 	);
2962 	CL_PRINTF(cliBuf);
2963 	pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
2964 
2965 	if (!pBtCoexist->bManualControl) {
2966 		/*  Sw mechanism */
2967 		CL_SPRINTF(
2968 			cliBuf,
2969 			BT_TMP_BUF_SIZE,
2970 			"\r\n %-35s", "============[Sw mechanism]============"
2971 		);
2972 		CL_PRINTF(cliBuf);
2973 
2974 		CL_SPRINTF(
2975 			cliBuf,
2976 			BT_TMP_BUF_SIZE,
2977 			"\r\n %-35s = %d", "SM[LowPenaltyRA]", \
2978 			pCoexDm->bCurLowPenaltyRa
2979 		);
2980 		CL_PRINTF(cliBuf);
2981 
2982 		CL_SPRINTF(
2983 			cliBuf,
2984 			BT_TMP_BUF_SIZE,
2985 			"\r\n %-35s = %s/ %s/ %d ", "DelBA/ BtCtrlAgg/ AggSize", \
2986 			(pBtCoexist->btInfo.bRejectAggPkt ? "Yes" : "No"),
2987 			(pBtCoexist->btInfo.bBtCtrlAggBufSize ? "Yes" : "No"),
2988 			pBtCoexist->btInfo.aggBufSize
2989 		);
2990 		CL_PRINTF(cliBuf);
2991 		CL_SPRINTF(
2992 			cliBuf,
2993 			BT_TMP_BUF_SIZE,
2994 			"\r\n %-35s = 0x%x ", "Rate Mask", \
2995 			pBtCoexist->btInfo.raMask
2996 		);
2997 		CL_PRINTF(cliBuf);
2998 
2999 		/*  Fw mechanism */
3000 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Fw mechanism]============");
3001 		CL_PRINTF(cliBuf);
3002 
3003 		psTdmaCase = pCoexDm->curPsTdma;
3004 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x case-%d (auto:%d)", "PS TDMA", \
3005 			pCoexDm->psTdmaPara[0], pCoexDm->psTdmaPara[1],
3006 			pCoexDm->psTdmaPara[2], pCoexDm->psTdmaPara[3],
3007 			pCoexDm->psTdmaPara[4], psTdmaCase, pCoexDm->bAutoTdmaAdjust);
3008 		CL_PRINTF(cliBuf);
3009 
3010 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "Coex Table Type", \
3011 			pCoexSta->nCoexTableType);
3012 		CL_PRINTF(cliBuf);
3013 
3014 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "IgnWlanAct", \
3015 			pCoexDm->bCurIgnoreWlanAct);
3016 		CL_PRINTF(cliBuf);
3017 
3018 		/*
3019 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x ", "Latest error condition(should be 0)", \
3020 			pCoexDm->errorCondition);
3021 		CL_PRINTF(cliBuf);
3022 		*/
3023 	}
3024 
3025 	/*  Hw setting */
3026 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Hw setting]============");
3027 	CL_PRINTF(cliBuf);
3028 
3029 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x", "backup ARFR1/ARFR2/RL/AMaxTime", \
3030 		pCoexDm->backupArfrCnt1, pCoexDm->backupArfrCnt2, pCoexDm->backupRetryLimit, pCoexDm->backupAmpduMaxTime);
3031 	CL_PRINTF(cliBuf);
3032 
3033 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
3034 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
3035 	u2Tmp[0] = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
3036 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
3037 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x", "0x430/0x434/0x42a/0x456", \
3038 		u4Tmp[0], u4Tmp[1], u2Tmp[0], u1Tmp[0]);
3039 	CL_PRINTF(cliBuf);
3040 
3041 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x778);
3042 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6cc);
3043 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x880);
3044 	CL_SPRINTF(
3045 		cliBuf, BT_TMP_BUF_SIZE,
3046 		"\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x778/0x6cc/0x880[29:25]", \
3047 		u1Tmp[0], u4Tmp[0],  (u4Tmp[1] & 0x3e000000) >> 25
3048 	);
3049 	CL_PRINTF(cliBuf);
3050 
3051 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
3052 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
3053 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x764);
3054 	u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e);
3055 	CL_SPRINTF(
3056 		cliBuf,
3057 		BT_TMP_BUF_SIZE,
3058 		"\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x948/ 0x67[5] / 0x764 / 0x76e", \
3059 		u4Tmp[0], ((u1Tmp[0] & 0x20) >> 5), (u4Tmp[1] & 0xffff), u1Tmp[1]
3060 	);
3061 	CL_PRINTF(cliBuf);
3062 
3063 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x92c);
3064 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x930);
3065 	u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x944);
3066 	CL_SPRINTF(
3067 		cliBuf,
3068 		BT_TMP_BUF_SIZE,
3069 		"\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]", \
3070 		u4Tmp[0] & 0x3, u4Tmp[1] & 0xff, u4Tmp[2] & 0x3
3071 	);
3072 	CL_PRINTF(cliBuf);
3073 
3074 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x39);
3075 	u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x40);
3076 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
3077 	u1Tmp[2] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x64);
3078 	CL_SPRINTF(
3079 		cliBuf,
3080 		BT_TMP_BUF_SIZE,
3081 		"\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x38[11]/0x40/0x4c[24:23]/0x64[0]", \
3082 		((u1Tmp[0] & 0x8) >> 3),
3083 		u1Tmp[1],
3084 		((u4Tmp[0] & 0x01800000) >> 23),
3085 		u1Tmp[2] & 0x1
3086 	);
3087 	CL_PRINTF(cliBuf);
3088 
3089 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x550);
3090 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x522);
3091 	CL_SPRINTF(
3092 		cliBuf,
3093 		BT_TMP_BUF_SIZE,
3094 		"\r\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522", \
3095 		u4Tmp[0], u1Tmp[0]
3096 	);
3097 	CL_PRINTF(cliBuf);
3098 
3099 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xc50);
3100 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x49c);
3101 	CL_SPRINTF(
3102 		cliBuf,
3103 		BT_TMP_BUF_SIZE,
3104 		"\r\n %-35s = 0x%x/ 0x%x", "0xc50(dig)/0x49c(null-drop)", \
3105 		u4Tmp[0] & 0xff, u1Tmp[0]
3106 	);
3107 	CL_PRINTF(cliBuf);
3108 
3109 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda0);
3110 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda4);
3111 	u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda8);
3112 	u4Tmp[3] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xcf0);
3113 
3114 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0xa5b);
3115 	u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0xa5c);
3116 
3117 	faOfdm =
3118 		((u4Tmp[0] & 0xffff0000) >> 16) +
3119 		((u4Tmp[1] & 0xffff0000) >> 16) +
3120 		(u4Tmp[1] & 0xffff) +  (u4Tmp[2] & 0xffff) + \
3121 		((u4Tmp[3] & 0xffff0000) >> 16) + (u4Tmp[3] & 0xffff);
3122 	faCck = (u1Tmp[0] << 8) + u1Tmp[1];
3123 
3124 	CL_SPRINTF(
3125 		cliBuf,
3126 		BT_TMP_BUF_SIZE,
3127 		"\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "OFDM-CCA/OFDM-FA/CCK-FA", \
3128 		u4Tmp[0] & 0xffff, faOfdm, faCck
3129 	);
3130 	CL_PRINTF(cliBuf);
3131 
3132 
3133 	CL_SPRINTF(
3134 		cliBuf,
3135 		BT_TMP_BUF_SIZE,
3136 		"\r\n %-35s = %d/ %d/ %d/ %d", "CRC_OK CCK/11g/11n/11n-Agg", \
3137 		pCoexSta->nCRCOK_CCK,
3138 		pCoexSta->nCRCOK_11g,
3139 		pCoexSta->nCRCOK_11n,
3140 		pCoexSta->nCRCOK_11nAgg
3141 	);
3142 	CL_PRINTF(cliBuf);
3143 
3144 	CL_SPRINTF(
3145 		cliBuf,
3146 		BT_TMP_BUF_SIZE,
3147 		"\r\n %-35s = %d/ %d/ %d/ %d", "CRC_Err CCK/11g/11n/11n-Agg", \
3148 		pCoexSta->nCRCErr_CCK,
3149 		pCoexSta->nCRCErr_11g,
3150 		pCoexSta->nCRCErr_11n,
3151 		pCoexSta->nCRCErr_11nAgg
3152 	);
3153 	CL_PRINTF(cliBuf);
3154 
3155 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c0);
3156 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c4);
3157 	u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c8);
3158 	CL_SPRINTF(
3159 		cliBuf,
3160 		BT_TMP_BUF_SIZE,
3161 		"\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x6c0/0x6c4/0x6c8(coexTable)", \
3162 		u4Tmp[0], u4Tmp[1], u4Tmp[2]);
3163 	CL_PRINTF(cliBuf);
3164 
3165 	CL_SPRINTF(
3166 		cliBuf,
3167 		BT_TMP_BUF_SIZE,
3168 		"\r\n %-35s = %d/ %d", "0x770(high-pri rx/tx)", \
3169 		pCoexSta->highPriorityRx, pCoexSta->highPriorityTx
3170 	);
3171 	CL_PRINTF(cliBuf);
3172 	CL_SPRINTF(
3173 		cliBuf,
3174 		BT_TMP_BUF_SIZE,
3175 		"\r\n %-35s = %d/ %d", "0x774(low-pri rx/tx)", \
3176 		pCoexSta->lowPriorityRx, pCoexSta->lowPriorityTx
3177 	);
3178 	CL_PRINTF(cliBuf);
3179 
3180 	pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_COEX_STATISTICS);
3181 }
3182 
3183 
3184 void EXhalbtc8723b1ant_IpsNotify(struct btc_coexist *pBtCoexist, u8 type)
3185 {
3186 	if (pBtCoexist->bManualControl ||	pBtCoexist->bStopCoexDm)
3187 		return;
3188 
3189 	if (BTC_IPS_ENTER == type) {
3190 		BTC_PRINT(
3191 			BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], IPS ENTER notify\n")
3192 		);
3193 		pCoexSta->bUnderIps = true;
3194 
3195 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
3196 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
3197 		halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, false, true);
3198 	} else if (BTC_IPS_LEAVE == type) {
3199 		BTC_PRINT(
3200 			BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], IPS LEAVE notify\n")
3201 		);
3202 		pCoexSta->bUnderIps = false;
3203 
3204 		halbtc8723b1ant_InitHwConfig(pBtCoexist, false, false);
3205 		halbtc8723b1ant_InitCoexDm(pBtCoexist);
3206 		halbtc8723b1ant_QueryBtInfo(pBtCoexist);
3207 	}
3208 }
3209 
3210 void EXhalbtc8723b1ant_LpsNotify(struct btc_coexist *pBtCoexist, u8 type)
3211 {
3212 	if (pBtCoexist->bManualControl || pBtCoexist->bStopCoexDm)
3213 		return;
3214 
3215 	if (BTC_LPS_ENABLE == type) {
3216 		BTC_PRINT(
3217 			BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], LPS ENABLE notify\n")
3218 		);
3219 		pCoexSta->bUnderLps = true;
3220 	} else if (BTC_LPS_DISABLE == type) {
3221 		BTC_PRINT(
3222 			BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], LPS DISABLE notify\n")
3223 		);
3224 		pCoexSta->bUnderLps = false;
3225 	}
3226 }
3227 
3228 void EXhalbtc8723b1ant_ScanNotify(struct btc_coexist *pBtCoexist, u8 type)
3229 {
3230 	bool bWifiConnected = false, bBtHsOn = false;
3231 	u32 wifiLinkStatus = 0;
3232 	u32 numOfWifiLink = 0;
3233 	bool bBtCtrlAggBufSize = false;
3234 	u8 aggBufSize = 5;
3235 
3236 	u8 u1Tmpa, u1Tmpb;
3237 	u32 u4Tmp;
3238 
3239 	if (pBtCoexist->bManualControl || pBtCoexist->bStopCoexDm)
3240 		return;
3241 
3242 	if (BTC_SCAN_START == type) {
3243 		pCoexSta->bWiFiIsHighPriTask = true;
3244 		BTC_PRINT(
3245 			BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN START notify\n")
3246 		);
3247 
3248 		halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 8);  /* Force antenna setup for no scan result issue */
3249 		u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
3250 		u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
3251 		u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
3252 
3253 
3254 		BTC_PRINT(
3255 			BTC_MSG_INTERFACE,
3256 			INTF_NOTIFY,
3257 			(
3258 				"[BTCoex], 0x948 = 0x%x, 0x765 = 0x%x, 0x67 = 0x%x\n",
3259 				u4Tmp,
3260 				u1Tmpa,
3261 				u1Tmpb
3262 			)
3263 		);
3264 	} else {
3265 		pCoexSta->bWiFiIsHighPriTask = false;
3266 		BTC_PRINT(
3267 			BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN FINISH notify\n")
3268 		);
3269 
3270 		pBtCoexist->fBtcGet(
3271 			pBtCoexist, BTC_GET_U1_AP_NUM, &pCoexSta->nScanAPNum
3272 		);
3273 	}
3274 
3275 	if (pBtCoexist->btInfo.bBtDisabled)
3276 		return;
3277 
3278 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3279 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
3280 
3281 	halbtc8723b1ant_QueryBtInfo(pBtCoexist);
3282 
3283 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
3284 	numOfWifiLink = wifiLinkStatus >> 16;
3285 
3286 	if (numOfWifiLink >= 2) {
3287 		halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3288 		halbtc8723b1ant_LimitedRx(
3289 			pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize
3290 		);
3291 		halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
3292 		return;
3293 	}
3294 
3295 	if (pCoexSta->bC2hBtInquiryPage) {
3296 		halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
3297 		return;
3298 	} else if (bBtHsOn) {
3299 		halbtc8723b1ant_ActionHs(pBtCoexist);
3300 		return;
3301 	}
3302 
3303 	if (BTC_SCAN_START == type) {
3304 		/* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN START notify\n")); */
3305 		if (!bWifiConnected)	/*  non-connected scan */
3306 			halbtc8723b1ant_ActionWifiNotConnectedScan(pBtCoexist);
3307 		else	/*  wifi is connected */
3308 			halbtc8723b1ant_ActionWifiConnectedScan(pBtCoexist);
3309 	} else if (BTC_SCAN_FINISH == type) {
3310 		/* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN FINISH notify\n")); */
3311 		if (!bWifiConnected)	/*  non-connected scan */
3312 			halbtc8723b1ant_ActionWifiNotConnected(pBtCoexist);
3313 		else
3314 			halbtc8723b1ant_ActionWifiConnected(pBtCoexist);
3315 	}
3316 }
3317 
3318 void EXhalbtc8723b1ant_ConnectNotify(struct btc_coexist *pBtCoexist, u8 type)
3319 {
3320 	bool bWifiConnected = false, bBtHsOn = false;
3321 	u32 wifiLinkStatus = 0;
3322 	u32 numOfWifiLink = 0;
3323 	bool bBtCtrlAggBufSize = false;
3324 	u8 aggBufSize = 5;
3325 
3326 	if (
3327 		pBtCoexist->bManualControl ||
3328 		pBtCoexist->bStopCoexDm ||
3329 		pBtCoexist->btInfo.bBtDisabled
3330 	)
3331 		return;
3332 
3333 	if (BTC_ASSOCIATE_START == type) {
3334 		pCoexSta->bWiFiIsHighPriTask = true;
3335 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT START notify\n"));
3336 		 pCoexDm->nArpCnt = 0;
3337 	} else {
3338 		pCoexSta->bWiFiIsHighPriTask = false;
3339 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT FINISH notify\n"));
3340 		/* pCoexDm->nArpCnt = 0; */
3341 	}
3342 
3343 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
3344 	numOfWifiLink = wifiLinkStatus >> 16;
3345 	if (numOfWifiLink >= 2) {
3346 		halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3347 		halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize);
3348 		halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
3349 		return;
3350 	}
3351 
3352 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3353 	if (pCoexSta->bC2hBtInquiryPage) {
3354 		halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
3355 		return;
3356 	} else if (bBtHsOn) {
3357 		halbtc8723b1ant_ActionHs(pBtCoexist);
3358 		return;
3359 	}
3360 
3361 	if (BTC_ASSOCIATE_START == type) {
3362 		/* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT START notify\n")); */
3363 		halbtc8723b1ant_ActionWifiNotConnectedAssoAuth(pBtCoexist);
3364 	} else if (BTC_ASSOCIATE_FINISH == type) {
3365 		/* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT FINISH notify\n")); */
3366 
3367 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
3368 		if (!bWifiConnected) /*  non-connected scan */
3369 			halbtc8723b1ant_ActionWifiNotConnected(pBtCoexist);
3370 		else
3371 			halbtc8723b1ant_ActionWifiConnected(pBtCoexist);
3372 	}
3373 }
3374 
3375 void EXhalbtc8723b1ant_MediaStatusNotify(struct btc_coexist *pBtCoexist, u8 type)
3376 {
3377 	u8 H2C_Parameter[3] = {0};
3378 	u32 wifiBw;
3379 	u8 wifiCentralChnl;
3380 	bool bWifiUnderBMode = false;
3381 
3382 	if (
3383 		pBtCoexist->bManualControl ||
3384 		pBtCoexist->bStopCoexDm ||
3385 		pBtCoexist->btInfo.bBtDisabled
3386 	)
3387 		return;
3388 
3389 	if (BTC_MEDIA_CONNECT == type) {
3390 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], MEDIA connect notify\n"));
3391 
3392 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode);
3393 
3394 		/* Set CCK Tx/Rx high Pri except 11b mode */
3395 		if (bWifiUnderBMode) {
3396 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x00); /* CCK Tx */
3397 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x00); /* CCK Rx */
3398 		} else {
3399 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x10); /* CCK Tx */
3400 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x10); /* CCK Rx */
3401 		}
3402 
3403 		pCoexDm->backupArfrCnt1 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
3404 		pCoexDm->backupArfrCnt2 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
3405 		pCoexDm->backupRetryLimit = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
3406 		pCoexDm->backupAmpduMaxTime = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
3407 	} else {
3408 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], MEDIA disconnect notify\n"));
3409 		pCoexDm->nArpCnt = 0;
3410 
3411 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x0); /* CCK Tx */
3412 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x0); /* CCK Rx */
3413 	}
3414 
3415 	/*  only 2.4G we need to inform bt the chnl mask */
3416 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifiCentralChnl);
3417 	if ((BTC_MEDIA_CONNECT == type) && (wifiCentralChnl <= 14)) {
3418 		/* H2C_Parameter[0] = 0x1; */
3419 		H2C_Parameter[0] = 0x0;
3420 		H2C_Parameter[1] = wifiCentralChnl;
3421 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
3422 
3423 		if (BTC_WIFI_BW_HT40 == wifiBw)
3424 			H2C_Parameter[2] = 0x30;
3425 		else
3426 			H2C_Parameter[2] = 0x20;
3427 	}
3428 
3429 	pCoexDm->wifiChnlInfo[0] = H2C_Parameter[0];
3430 	pCoexDm->wifiChnlInfo[1] = H2C_Parameter[1];
3431 	pCoexDm->wifiChnlInfo[2] = H2C_Parameter[2];
3432 
3433 	BTC_PRINT(
3434 		BTC_MSG_ALGORITHM,
3435 		ALGO_TRACE_FW_EXEC,
3436 		(
3437 			"[BTCoex], FW write 0x66 = 0x%x\n",
3438 			H2C_Parameter[0] << 16 | H2C_Parameter[1] << 8 | H2C_Parameter[2]
3439 		)
3440 	);
3441 
3442 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x66, 3, H2C_Parameter);
3443 }
3444 
3445 void EXhalbtc8723b1ant_SpecialPacketNotify(struct btc_coexist *pBtCoexist, u8 type)
3446 {
3447 	bool bBtHsOn = false;
3448 	u32 wifiLinkStatus = 0;
3449 	u32 numOfWifiLink = 0;
3450 	bool bBtCtrlAggBufSize = false;
3451 	u8 aggBufSize = 5;
3452 
3453 	if (
3454 		pBtCoexist->bManualControl ||
3455 		pBtCoexist->bStopCoexDm ||
3456 		pBtCoexist->btInfo.bBtDisabled
3457 	)
3458 		return;
3459 
3460 	if (
3461 		BTC_PACKET_DHCP == type ||
3462 		BTC_PACKET_EAPOL == type ||
3463 		BTC_PACKET_ARP == type
3464 	) {
3465 		if (BTC_PACKET_ARP == type) {
3466 			BTC_PRINT(
3467 				BTC_MSG_INTERFACE,
3468 				INTF_NOTIFY,
3469 				("[BTCoex], special Packet ARP notify\n")
3470 			);
3471 
3472 			pCoexDm->nArpCnt++;
3473 			BTC_PRINT(
3474 				BTC_MSG_INTERFACE,
3475 				INTF_NOTIFY,
3476 				("[BTCoex], ARP Packet Count = %d\n", pCoexDm->nArpCnt)
3477 			);
3478 
3479 			if (pCoexDm->nArpCnt >= 10) /*  if APR PKT > 10 after connect, do not go to ActionWifiConnectedSpecialPacket(pBtCoexist) */
3480 				pCoexSta->bWiFiIsHighPriTask = false;
3481 			else
3482 				pCoexSta->bWiFiIsHighPriTask = true;
3483 		} else {
3484 			pCoexSta->bWiFiIsHighPriTask = true;
3485 			BTC_PRINT(
3486 				BTC_MSG_INTERFACE,
3487 				INTF_NOTIFY,
3488 				("[BTCoex], special Packet DHCP or EAPOL notify\n")
3489 			);
3490 		}
3491 	} else {
3492 		pCoexSta->bWiFiIsHighPriTask = false;
3493 		BTC_PRINT(
3494 			BTC_MSG_INTERFACE,
3495 			INTF_NOTIFY,
3496 			("[BTCoex], special Packet [Type = %d] notify\n", type)
3497 		);
3498 	}
3499 
3500 	pCoexSta->specialPktPeriodCnt = 0;
3501 
3502 	pBtCoexist->fBtcGet(
3503 		pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus
3504 	);
3505 	numOfWifiLink = wifiLinkStatus >> 16;
3506 
3507 	if (numOfWifiLink >= 2) {
3508 		halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3509 		halbtc8723b1ant_LimitedRx(
3510 			pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize
3511 		);
3512 		halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
3513 		return;
3514 	}
3515 
3516 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3517 	if (pCoexSta->bC2hBtInquiryPage) {
3518 		halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
3519 		return;
3520 	} else if (bBtHsOn) {
3521 		halbtc8723b1ant_ActionHs(pBtCoexist);
3522 		return;
3523 	}
3524 
3525 	if (
3526 		BTC_PACKET_DHCP == type ||
3527 		BTC_PACKET_EAPOL == type ||
3528 		((BTC_PACKET_ARP == type) && (pCoexSta->bWiFiIsHighPriTask))
3529 	)
3530 		halbtc8723b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
3531 }
3532 
3533 void EXhalbtc8723b1ant_BtInfoNotify(
3534 	struct btc_coexist *pBtCoexist, u8 *tmpBuf, u8 length
3535 )
3536 {
3537 	u8 btInfo = 0;
3538 	u8 i, rspSource = 0;
3539 	bool bWifiConnected = false;
3540 	bool bBtBusy = false;
3541 
3542 	pCoexSta->bC2hBtInfoReqSent = false;
3543 
3544 	rspSource = tmpBuf[0] & 0xf;
3545 	if (rspSource >= BT_INFO_SRC_8723B_1ANT_MAX)
3546 		rspSource = BT_INFO_SRC_8723B_1ANT_WIFI_FW;
3547 	pCoexSta->btInfoC2hCnt[rspSource]++;
3548 
3549 	BTC_PRINT(
3550 		BTC_MSG_INTERFACE,
3551 		INTF_NOTIFY,
3552 		("[BTCoex], Bt info[%d], length =%d, hex data =[",
3553 		rspSource,
3554 		length)
3555 	);
3556 	for (i = 0; i < length; i++) {
3557 		pCoexSta->btInfoC2h[rspSource][i] = tmpBuf[i];
3558 		if (i == 1)
3559 			btInfo = tmpBuf[i];
3560 		if (i == length - 1)
3561 			BTC_PRINT(
3562 				BTC_MSG_INTERFACE, INTF_NOTIFY, ("0x%02x]\n", tmpBuf[i])
3563 			);
3564 		else
3565 			BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("0x%02x, ", tmpBuf[i]));
3566 	}
3567 
3568 	if (BT_INFO_SRC_8723B_1ANT_WIFI_FW != rspSource) {
3569 		pCoexSta->btRetryCnt = pCoexSta->btInfoC2h[rspSource][2] & 0xf;
3570 
3571 		if (pCoexSta->btRetryCnt >= 1)
3572 			pCoexSta->popEventCnt++;
3573 
3574 		if (pCoexSta->btInfoC2h[rspSource][2] & 0x20)
3575 			pCoexSta->bC2hBtPage = true;
3576 		else
3577 			pCoexSta->bC2hBtPage = false;
3578 
3579 		pCoexSta->btRssi = pCoexSta->btInfoC2h[rspSource][3] * 2 - 90;
3580 		/* pCoexSta->btInfoC2h[rspSource][3]*2+10; */
3581 
3582 		pCoexSta->btInfoExt = pCoexSta->btInfoC2h[rspSource][4];
3583 
3584 		pCoexSta->bBtTxRxMask = (pCoexSta->btInfoC2h[rspSource][2] & 0x40);
3585 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TX_RX_MASK, &pCoexSta->bBtTxRxMask);
3586 
3587 		if (!pCoexSta->bBtTxRxMask) {
3588 			/* BT into is responded by BT FW and BT RF REG 0x3C != 0x15 => Need to switch BT TRx Mask */
3589 			BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n"));
3590 			pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x15);
3591 		}
3592 
3593 		/*  Here we need to resend some wifi info to BT */
3594 		/*  because bt is reset and loss of the info. */
3595 		if (pCoexSta->btInfoExt & BIT1) {
3596 			BTC_PRINT(
3597 				BTC_MSG_ALGORITHM,
3598 				ALGO_TRACE,
3599 				("[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n")
3600 			);
3601 			pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
3602 			if (bWifiConnected)
3603 				EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_CONNECT);
3604 			else
3605 				EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
3606 		}
3607 
3608 		if (pCoexSta->btInfoExt & BIT3) {
3609 			if (!pBtCoexist->bManualControl && !pBtCoexist->bStopCoexDm) {
3610 				BTC_PRINT(
3611 					BTC_MSG_ALGORITHM,
3612 					ALGO_TRACE,
3613 					("[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n")
3614 				);
3615 				halbtc8723b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, false);
3616 			}
3617 		} else {
3618 			/*  BT already NOT ignore Wlan active, do nothing here. */
3619 		}
3620 	}
3621 
3622 	/*  check BIT2 first ==> check if bt is under inquiry or page scan */
3623 	if (btInfo & BT_INFO_8723B_1ANT_B_INQ_PAGE)
3624 		pCoexSta->bC2hBtInquiryPage = true;
3625 	else
3626 		pCoexSta->bC2hBtInquiryPage = false;
3627 
3628 	/*  set link exist status */
3629 	if (!(btInfo & BT_INFO_8723B_1ANT_B_CONNECTION)) {
3630 		pCoexSta->bBtLinkExist = false;
3631 		pCoexSta->bPanExist = false;
3632 		pCoexSta->bA2dpExist = false;
3633 		pCoexSta->bHidExist = false;
3634 		pCoexSta->bScoExist = false;
3635 	} else {	/*  connection exists */
3636 		pCoexSta->bBtLinkExist = true;
3637 		if (btInfo & BT_INFO_8723B_1ANT_B_FTP)
3638 			pCoexSta->bPanExist = true;
3639 		else
3640 			pCoexSta->bPanExist = false;
3641 
3642 		if (btInfo & BT_INFO_8723B_1ANT_B_A2DP)
3643 			pCoexSta->bA2dpExist = true;
3644 		else
3645 			pCoexSta->bA2dpExist = false;
3646 
3647 		if (btInfo & BT_INFO_8723B_1ANT_B_HID)
3648 			pCoexSta->bHidExist = true;
3649 		else
3650 			pCoexSta->bHidExist = false;
3651 
3652 		if (btInfo & BT_INFO_8723B_1ANT_B_SCO_ESCO)
3653 			pCoexSta->bScoExist = true;
3654 		else
3655 			pCoexSta->bScoExist = false;
3656 	}
3657 
3658 	halbtc8723b1ant_UpdateBtLinkInfo(pBtCoexist);
3659 
3660 	btInfo = btInfo & 0x1f;  /* mask profile bit for connect-ilde identification (for CSR case: A2DP idle --> 0x41) */
3661 
3662 	if (!(btInfo & BT_INFO_8723B_1ANT_B_CONNECTION)) {
3663 		pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
3664 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n"));
3665 	} else if (btInfo == BT_INFO_8723B_1ANT_B_CONNECTION)	{
3666 		/*  connection exists but no busy */
3667 		pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE;
3668 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n"));
3669 	} else if (
3670 		(btInfo & BT_INFO_8723B_1ANT_B_SCO_ESCO) ||
3671 		(btInfo & BT_INFO_8723B_1ANT_B_SCO_BUSY)
3672 	) {
3673 		pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_SCO_BUSY;
3674 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT SCO busy!!!\n"));
3675 	} else if (btInfo & BT_INFO_8723B_1ANT_B_ACL_BUSY) {
3676 		if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY != pCoexDm->btStatus)
3677 			pCoexDm->bAutoTdmaAdjust = false;
3678 
3679 		pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_ACL_BUSY;
3680 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT ACL busy!!!\n"));
3681 	} else {
3682 		pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_MAX;
3683 		BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n"));
3684 	}
3685 
3686 	if (
3687 		(BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) ||
3688 		(BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
3689 		(BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
3690 	)
3691 		bBtBusy = true;
3692 	else
3693 		bBtBusy = false;
3694 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
3695 
3696 	halbtc8723b1ant_RunCoexistMechanism(pBtCoexist);
3697 }
3698 
3699 void EXhalbtc8723b1ant_HaltNotify(struct btc_coexist *pBtCoexist)
3700 {
3701 	BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Halt notify\n"));
3702 
3703 	halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3704 	halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 0);
3705 	halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, false, true);
3706 
3707 	halbtc8723b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, true);
3708 
3709 	EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
3710 
3711 	pBtCoexist->bStopCoexDm = true;
3712 }
3713 
3714 void EXhalbtc8723b1ant_PnpNotify(struct btc_coexist *pBtCoexist, u8 pnpState)
3715 {
3716 	BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Pnp notify\n"));
3717 
3718 	if (BTC_WIFI_PNP_SLEEP == pnpState) {
3719 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Pnp notify to SLEEP\n"));
3720 
3721 		halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3722 		halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
3723 		halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
3724 		halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, false, true);
3725 
3726 		pBtCoexist->bStopCoexDm = true;
3727 	} else if (BTC_WIFI_PNP_WAKE_UP == pnpState) {
3728 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Pnp notify to WAKE UP\n"));
3729 		pBtCoexist->bStopCoexDm = false;
3730 		halbtc8723b1ant_InitHwConfig(pBtCoexist, false, false);
3731 		halbtc8723b1ant_InitCoexDm(pBtCoexist);
3732 		halbtc8723b1ant_QueryBtInfo(pBtCoexist);
3733 	}
3734 }
3735 
3736 void EXhalbtc8723b1ant_Periodical(struct btc_coexist *pBtCoexist)
3737 {
3738 	static u8 disVerInfoCnt;
3739 	u32 fwVer = 0, btPatchVer = 0;
3740 
3741 	BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], ==========================Periodical ===========================\n"));
3742 
3743 	if (disVerInfoCnt <= 5) {
3744 		disVerInfoCnt += 1;
3745 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], ****************************************************************\n"));
3746 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_BT_PATCH_VER, &btPatchVer);
3747 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);
3748 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n", \
3749 			GLCoexVerDate8723b1Ant, GLCoexVer8723b1Ant, fwVer, btPatchVer, btPatchVer));
3750 		BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], ****************************************************************\n"));
3751 	}
3752 
3753 	halbtc8723b1ant_MonitorBtCtr(pBtCoexist);
3754 	halbtc8723b1ant_MonitorWiFiCtr(pBtCoexist);
3755 
3756 	if (
3757 		halbtc8723b1ant_IsWifiStatusChanged(pBtCoexist) ||
3758 		pCoexDm->bAutoTdmaAdjust
3759 	)
3760 		halbtc8723b1ant_RunCoexistMechanism(pBtCoexist);
3761 
3762 	pCoexSta->specialPktPeriodCnt++;
3763 }
3764