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