xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rtl8189es/hal/btc/HalBtc8703b1Ant.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 //============================================================
3 // Description:
4 //
5 // This file is for RTL8703B Co-exist mechanism
6 //
7 // History
8 // 2012/11/15 Cosa first check in.
9 //
10 //============================================================
11 
12 //============================================================
13 // include files
14 //============================================================
15 #include "Mp_Precomp.h"
16 
17 #if WPP_SOFTWARE_TRACE
18 #include "HalBtc8703b1Ant.tmh"
19 #endif
20 
21 //#include <Math.h>
22 #if(BT_30_SUPPORT == 1)
23 //============================================================
24 // Global variables, these are static variables
25 //============================================================
26 static COEX_DM_8703B_1ANT		GLCoexDm8703b1Ant;
27 static PCOEX_DM_8703B_1ANT 	pCoexDm=&GLCoexDm8703b1Ant;
28 static COEX_STA_8703B_1ANT		GLCoexSta8703b1Ant;
29 static PCOEX_STA_8703B_1ANT	pCoexSta=&GLCoexSta8703b1Ant;
30 static PSDSCAN_STA_8703B_1ANT	GLPsdScan8703b1Ant;
31 static PPSDSCAN_STA_8703B_1ANT pPsdScan = &GLPsdScan8703b1Ant;
32 
33 
34 const char *const GLBtInfoSrc8703b1Ant[]={
35 	"BT Info[wifi fw]",
36 	"BT Info[bt rsp]",
37 	"BT Info[bt auto report]",
38 };
39 
40 u4Byte	GLCoexVerDate8703b1Ant=20141215;
41 u4Byte	GLCoexVer8703b1Ant=0x56;
42 
43 //============================================================
44 // local function proto type if needed
45 //============================================================
46 //============================================================
47 // local function start with halbtc8703b1ant_
48 //============================================================
49 u1Byte
halbtc8703b1ant_BtRssiState(u1Byte levelNum,u1Byte rssiThresh,u1Byte rssiThresh1)50 halbtc8703b1ant_BtRssiState(
51 	u1Byte			levelNum,
52 	u1Byte			rssiThresh,
53 	u1Byte			rssiThresh1
54 	)
55 {
56 	s4Byte			btRssi=0;
57 	u1Byte			btRssiState=pCoexSta->preBtRssiState;
58 
59 	btRssi = pCoexSta->btRssi;
60 
61 	if(levelNum == 2)
62 	{
63 		if( (pCoexSta->preBtRssiState == BTC_RSSI_STATE_LOW) ||
64 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_STAY_LOW))
65 		{
66 			if(btRssi >= (rssiThresh+BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
67 			{
68 				btRssiState = BTC_RSSI_STATE_HIGH;
69 			}
70 			else
71 			{
72 				btRssiState = BTC_RSSI_STATE_STAY_LOW;
73 			}
74 		}
75 		else
76 		{
77 			if(btRssi < rssiThresh)
78 			{
79 				btRssiState = BTC_RSSI_STATE_LOW;
80 			}
81 			else
82 			{
83 				btRssiState = BTC_RSSI_STATE_STAY_HIGH;
84 			}
85 		}
86 	}
87 	else if(levelNum == 3)
88 	{
89 		if(rssiThresh > rssiThresh1)
90 		{
91 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Rssi thresh error!!\n"));
92 			return pCoexSta->preBtRssiState;
93 		}
94 
95 		if( (pCoexSta->preBtRssiState == BTC_RSSI_STATE_LOW) ||
96 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_STAY_LOW))
97 		{
98 			if(btRssi >= (rssiThresh+BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
99 			{
100 				btRssiState = BTC_RSSI_STATE_MEDIUM;
101 			}
102 			else
103 			{
104 				btRssiState = BTC_RSSI_STATE_STAY_LOW;
105 			}
106 		}
107 		else if( (pCoexSta->preBtRssiState == BTC_RSSI_STATE_MEDIUM) ||
108 			(pCoexSta->preBtRssiState == BTC_RSSI_STATE_STAY_MEDIUM))
109 		{
110 			if(btRssi >= (rssiThresh1+BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
111 			{
112 				btRssiState = BTC_RSSI_STATE_HIGH;
113 			}
114 			else if(btRssi < rssiThresh)
115 			{
116 				btRssiState = BTC_RSSI_STATE_LOW;
117 			}
118 			else
119 			{
120 				btRssiState = BTC_RSSI_STATE_STAY_MEDIUM;
121 			}
122 		}
123 		else
124 		{
125 			if(btRssi < rssiThresh1)
126 			{
127 				btRssiState = BTC_RSSI_STATE_MEDIUM;
128 			}
129 			else
130 			{
131 				btRssiState = BTC_RSSI_STATE_STAY_HIGH;
132 			}
133 		}
134 	}
135 
136 	pCoexSta->preBtRssiState = btRssiState;
137 
138 	return btRssiState;
139 }
140 
141 u1Byte
halbtc8703b1ant_WifiRssiState(IN PBTC_COEXIST pBtCoexist,IN u1Byte index,IN u1Byte levelNum,IN u1Byte rssiThresh,IN u1Byte rssiThresh1)142 halbtc8703b1ant_WifiRssiState(
143 	IN	PBTC_COEXIST		pBtCoexist,
144 	IN	u1Byte			index,
145 	IN	u1Byte			levelNum,
146 	IN	u1Byte			rssiThresh,
147 	IN	u1Byte			rssiThresh1
148 	)
149 {
150 	s4Byte			wifiRssi=0;
151 	u1Byte			wifiRssiState=pCoexSta->preWifiRssiState[index];
152 
153 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi);
154 
155 	if(levelNum == 2)
156 	{
157 		if( (pCoexSta->preWifiRssiState[index] == BTC_RSSI_STATE_LOW) ||
158 			(pCoexSta->preWifiRssiState[index] == BTC_RSSI_STATE_STAY_LOW))
159 		{
160 			if(wifiRssi >= (rssiThresh+BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
161 			{
162 				wifiRssiState = BTC_RSSI_STATE_HIGH;
163 			}
164 			else
165 			{
166 				wifiRssiState = BTC_RSSI_STATE_STAY_LOW;
167 			}
168 		}
169 		else
170 		{
171 			if(wifiRssi < rssiThresh)
172 			{
173 				wifiRssiState = BTC_RSSI_STATE_LOW;
174 			}
175 			else
176 			{
177 				wifiRssiState = BTC_RSSI_STATE_STAY_HIGH;
178 			}
179 		}
180 	}
181 	else if(levelNum == 3)
182 	{
183 		if(rssiThresh > rssiThresh1)
184 		{
185 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], wifi RSSI thresh error!!\n"));
186 			return pCoexSta->preWifiRssiState[index];
187 		}
188 
189 		if( (pCoexSta->preWifiRssiState[index] == BTC_RSSI_STATE_LOW) ||
190 			(pCoexSta->preWifiRssiState[index] == BTC_RSSI_STATE_STAY_LOW))
191 		{
192 			if(wifiRssi >= (rssiThresh+BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
193 			{
194 				wifiRssiState = BTC_RSSI_STATE_MEDIUM;
195 			}
196 			else
197 			{
198 				wifiRssiState = BTC_RSSI_STATE_STAY_LOW;
199 			}
200 		}
201 		else if( (pCoexSta->preWifiRssiState[index] == BTC_RSSI_STATE_MEDIUM) ||
202 			(pCoexSta->preWifiRssiState[index] == BTC_RSSI_STATE_STAY_MEDIUM))
203 		{
204 			if(wifiRssi >= (rssiThresh1+BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
205 			{
206 				wifiRssiState = BTC_RSSI_STATE_HIGH;
207 			}
208 			else if(wifiRssi < rssiThresh)
209 			{
210 				wifiRssiState = BTC_RSSI_STATE_LOW;
211 			}
212 			else
213 			{
214 				wifiRssiState = BTC_RSSI_STATE_STAY_MEDIUM;
215 			}
216 		}
217 		else
218 		{
219 			if(wifiRssi < rssiThresh1)
220 			{
221 				wifiRssiState = BTC_RSSI_STATE_MEDIUM;
222 			}
223 			else
224 			{
225 				wifiRssiState = BTC_RSSI_STATE_STAY_HIGH;
226 			}
227 		}
228 	}
229 
230 	pCoexSta->preWifiRssiState[index] = wifiRssiState;
231 
232 	return wifiRssiState;
233 }
234 
235 VOID
halbtc8703b1ant_UpdateRaMask(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u4Byte disRateMask)236 halbtc8703b1ant_UpdateRaMask(
237 	IN	PBTC_COEXIST		pBtCoexist,
238 	IN	BOOLEAN				bForceExec,
239 	IN	u4Byte				disRateMask
240 	)
241 {
242 	pCoexDm->curRaMask = disRateMask;
243 
244 	if( bForceExec || (pCoexDm->preRaMask != pCoexDm->curRaMask))
245 	{
246 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_UPDATE_RAMASK, &pCoexDm->curRaMask);
247 	}
248 	pCoexDm->preRaMask = pCoexDm->curRaMask;
249 }
250 
251 VOID
halbtc8703b1ant_AutoRateFallbackRetry(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u1Byte type)252 halbtc8703b1ant_AutoRateFallbackRetry(
253 	IN	PBTC_COEXIST		pBtCoexist,
254 	IN	BOOLEAN				bForceExec,
255 	IN	u1Byte				type
256 	)
257 {
258 	BOOLEAN	bWifiUnderBMode=FALSE;
259 
260 	pCoexDm->curArfrType = type;
261 
262 	if( bForceExec || (pCoexDm->preArfrType != pCoexDm->curArfrType))
263 	{
264 		switch(pCoexDm->curArfrType)
265 		{
266 			case 0:	// normal mode
267 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x430, pCoexDm->backupArfrCnt1);
268 				pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x434, pCoexDm->backupArfrCnt2);
269 				break;
270 			case 1:
271 				pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode);
272 				if(bWifiUnderBMode)
273 				{
274 					pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x430, 0x0);
275 					pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x434, 0x01010101);
276 				}
277 				else
278 				{
279 					pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x430, 0x0);
280 					pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x434, 0x04030201);
281 				}
282 				break;
283 			default:
284 				break;
285 		}
286 	}
287 
288 	pCoexDm->preArfrType = pCoexDm->curArfrType;
289 }
290 
291 VOID
halbtc8703b1ant_RetryLimit(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u1Byte type)292 halbtc8703b1ant_RetryLimit(
293 	IN	PBTC_COEXIST		pBtCoexist,
294 	IN	BOOLEAN				bForceExec,
295 	IN	u1Byte				type
296 	)
297 {
298 	pCoexDm->curRetryLimitType = type;
299 
300 	if( bForceExec || (pCoexDm->preRetryLimitType != pCoexDm->curRetryLimitType))
301 	{
302 		switch(pCoexDm->curRetryLimitType)
303 		{
304 			case 0:	// normal mode
305 				pBtCoexist->fBtcWrite2Byte(pBtCoexist, 0x42a, pCoexDm->backupRetryLimit);
306 				break;
307 			case 1:	// retry limit=8
308 				pBtCoexist->fBtcWrite2Byte(pBtCoexist, 0x42a, 0x0808);
309 				break;
310 			default:
311 				break;
312 		}
313 	}
314 
315 	pCoexDm->preRetryLimitType = pCoexDm->curRetryLimitType;
316 }
317 
318 VOID
halbtc8703b1ant_AmpduMaxTime(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u1Byte type)319 halbtc8703b1ant_AmpduMaxTime(
320 	IN	PBTC_COEXIST		pBtCoexist,
321 	IN	BOOLEAN				bForceExec,
322 	IN	u1Byte				type
323 	)
324 {
325 	pCoexDm->curAmpduTimeType = type;
326 
327 	if( bForceExec || (pCoexDm->preAmpduTimeType != pCoexDm->curAmpduTimeType))
328 	{
329 		switch(pCoexDm->curAmpduTimeType)
330 		{
331 			case 0:	// normal mode
332 				pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x456, pCoexDm->backupAmpduMaxTime);
333 				break;
334 			case 1:	// AMPDU timw = 0x38 * 32us
335 				pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x456, 0x38);
336 				break;
337 			default:
338 				break;
339 		}
340 	}
341 
342 	pCoexDm->preAmpduTimeType = pCoexDm->curAmpduTimeType;
343 }
344 
345 VOID
halbtc8703b1ant_LimitedTx(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u1Byte raMaskType,IN u1Byte arfrType,IN u1Byte retryLimitType,IN u1Byte ampduTimeType)346 halbtc8703b1ant_LimitedTx(
347 	IN	PBTC_COEXIST		pBtCoexist,
348 	IN	BOOLEAN				bForceExec,
349 	IN	u1Byte				raMaskType,
350 	IN	u1Byte				arfrType,
351 	IN	u1Byte				retryLimitType,
352 	IN	u1Byte				ampduTimeType
353 	)
354 {
355 	switch(raMaskType)
356 	{
357 		case 0:	// normal mode
358 			halbtc8703b1ant_UpdateRaMask(pBtCoexist, bForceExec, 0x0);
359 			break;
360 		case 1:	// disable cck 1/2
361 			halbtc8703b1ant_UpdateRaMask(pBtCoexist, bForceExec, 0x00000003);
362 			break;
363 		case 2:	// disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4
364 			halbtc8703b1ant_UpdateRaMask(pBtCoexist, bForceExec, 0x0001f1f7);
365 			break;
366 		default:
367 			break;
368 	}
369 
370 	halbtc8703b1ant_AutoRateFallbackRetry(pBtCoexist, bForceExec, arfrType);
371 	halbtc8703b1ant_RetryLimit(pBtCoexist, bForceExec, retryLimitType);
372 	halbtc8703b1ant_AmpduMaxTime(pBtCoexist, bForceExec, ampduTimeType);
373 }
374 
375 VOID
halbtc8703b1ant_LimitedRx(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN BOOLEAN bRejApAggPkt,IN BOOLEAN bBtCtrlAggBufSize,IN u1Byte aggBufSize)376 halbtc8703b1ant_LimitedRx(
377 	IN	PBTC_COEXIST		pBtCoexist,
378 	IN	BOOLEAN				bForceExec,
379 	IN	BOOLEAN				bRejApAggPkt,
380 	IN	BOOLEAN				bBtCtrlAggBufSize,
381 	IN	u1Byte				aggBufSize
382 	)
383 {
384 	BOOLEAN	bRejectRxAgg=bRejApAggPkt;
385 	BOOLEAN	bBtCtrlRxAggSize=bBtCtrlAggBufSize;
386 	u1Byte	rxAggSize=aggBufSize;
387 
388 	//============================================
389 	//	Rx Aggregation related setting
390 	//============================================
391 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT, &bRejectRxAgg);
392 	// decide BT control aggregation buf size or not
393 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE, &bBtCtrlRxAggSize);
394 	// aggregation buf size, only work when BT control Rx aggregation size.
395 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_AGG_BUF_SIZE, &rxAggSize);
396 	// real update aggregation setting
397 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
398 
399 
400 }
401 
402 VOID
halbtc8703b1ant_QueryBtInfo(IN PBTC_COEXIST pBtCoexist)403 halbtc8703b1ant_QueryBtInfo(
404 	IN	PBTC_COEXIST		pBtCoexist
405 	)
406 {
407 	u1Byte			H2C_Parameter[1] ={0};
408 
409 	pCoexSta->bC2hBtInfoReqSent = TRUE;
410 
411 	H2C_Parameter[0] |= BIT0;	// trigger
412 
413 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], Query Bt Info, FW write 0x61=0x%x\n",
414 		H2C_Parameter[0]));
415 
416 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x61, 1, H2C_Parameter);
417 }
418 
419 VOID
halbtc8703b1ant_MonitorBtCtr(IN PBTC_COEXIST pBtCoexist)420 halbtc8703b1ant_MonitorBtCtr(
421 	IN	PBTC_COEXIST		pBtCoexist
422 	)
423 {
424 	u4Byte 			regHPTxRx, regLPTxRx, u4Tmp, u4Tmp1;
425 	u4Byte			regHPTx=0, regHPRx=0, regLPTx=0, regLPRx=0;
426 	u1Byte			u1Tmp, u1Tmp1;
427 	s4Byte			wifiRssi;
428 	static u1Byte		NumOfBtCounterChk = 0;
429 
430        //to avoid 0x76e[3] = 1 (WLAN_Act control by PTA) during IPS
431 	//if (! (pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e) & 0x8) )
432 
433 	if (pCoexSta->bUnderIps)
434 	{
435 		//pCoexSta->highPriorityTx = 65535;
436 		//pCoexSta->highPriorityRx = 65535;
437 		//pCoexSta->lowPriorityTx = 65535;
438 		//pCoexSta->lowPriorityRx = 65535;
439 		//return;
440 	}
441 
442 	regHPTxRx = 0x770;
443 	regLPTxRx = 0x774;
444 
445 	u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, regHPTxRx);
446 	regHPTx = u4Tmp & bMaskLWord;
447 	regHPRx = (u4Tmp & bMaskHWord)>>16;
448 
449 	u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, regLPTxRx);
450 	regLPTx = u4Tmp & bMaskLWord;
451 	regLPRx = (u4Tmp & bMaskHWord)>>16;
452 
453 	pCoexSta->highPriorityTx = regHPTx;
454 	pCoexSta->highPriorityRx = regHPRx;
455 	pCoexSta->lowPriorityTx = regLPTx;
456 	pCoexSta->lowPriorityRx = regLPRx;
457 
458 	if( (pCoexSta->lowPriorityTx > 1150)  && (!pCoexSta->bC2hBtInquiryPage))
459 		pCoexSta->popEventCnt++;
460 
461 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
462 		regHPRx, regHPTx, regLPRx, regLPTx));
463 
464 	// reset counter
465 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0xc);
466 
467 	if ((regHPTx == 0) && (regHPRx ==0) && (regLPTx == 0) && (regLPRx == 0))
468 	{
469 		NumOfBtCounterChk++;
470 		if (NumOfBtCounterChk >= 3)
471 {
472 			halbtc8703b1ant_QueryBtInfo(pBtCoexist);
473 			NumOfBtCounterChk = 0;
474 		}
475 	}
476 }
477 
478 
479 VOID
halbtc8703b1ant_MonitorWiFiCtr(IN PBTC_COEXIST pBtCoexist)480 halbtc8703b1ant_MonitorWiFiCtr(
481 	IN	PBTC_COEXIST		pBtCoexist
482 	)
483 {
484 	u4Byte 	u4Tmp;
485 	u2Byte 	u2Tmp[3];
486 	s4Byte	wifiRssi=0;
487 	BOOLEAN bWifiBusy = FALSE, bWifiUnderBMode = FALSE;
488 	static u1Byte nCCKLockCounter = 0;
489 	u4Byte	TotalCnt;
490 
491 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
492 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi);
493 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode);
494 
495 	if (pCoexSta->bUnderIps)
496 	{
497 		pCoexSta->nCRCOK_CCK = 0;
498 		pCoexSta->nCRCOK_11g = 0;
499 		pCoexSta->nCRCOK_11n = 0;
500 		pCoexSta->nCRCOK_11nAgg = 0;
501 
502 		pCoexSta->nCRCErr_CCK = 0;
503 		pCoexSta->nCRCErr_11g = 0;
504 		pCoexSta->nCRCErr_11n = 0;
505 		pCoexSta->nCRCErr_11nAgg = 0;
506 	}
507 	else
508 	{
509 		pCoexSta->nCRCOK_CCK	= pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xf88);
510 		pCoexSta->nCRCOK_11g 	= pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf94);
511 		pCoexSta->nCRCOK_11n	= pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf90);
512 		pCoexSta->nCRCOK_11nAgg= pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xfb8);
513 
514 		pCoexSta->nCRCErr_CCK 	 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xf84);
515 		pCoexSta->nCRCErr_11g 	 = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf96);
516 		pCoexSta->nCRCErr_11n 	 = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xf92);
517 		pCoexSta->nCRCErr_11nAgg = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0xfba);
518 	}
519 
520 
521 	//reset counter
522 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0xf16, 0x1, 0x1);
523 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0xf16, 0x1, 0x0);
524 
525 	if ( (bWifiBusy) && (wifiRssi >= 30) && (!bWifiUnderBMode))
526 	{
527 		TotalCnt = pCoexSta->nCRCOK_CCK + pCoexSta->nCRCOK_11g + pCoexSta->nCRCOK_11n +
528 				pCoexSta->nCRCOK_11nAgg;
529 
530 		if ( (pCoexDm->btStatus == BT_8703B_1ANT_BT_STATUS_ACL_BUSY) ||
531 			(pCoexDm->btStatus == BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY) ||
532 			(pCoexDm->btStatus == BT_8703B_1ANT_BT_STATUS_SCO_BUSY) )
533 		{
534 			if (pCoexSta->nCRCOK_CCK >(TotalCnt -pCoexSta->nCRCOK_CCK))
535 			{
536 				if (nCCKLockCounter < 3)
537 				 nCCKLockCounter++;
538 			}
539 			else
540 			{
541 				if (nCCKLockCounter > 0)
542 				 nCCKLockCounter--;
543 			}
544 
545 		}
546 		else
547 		{
548 			if (nCCKLockCounter > 0)
549 			  nCCKLockCounter--;
550 		}
551 	}
552 	else
553 	{
554 		if (nCCKLockCounter > 0)
555 			nCCKLockCounter--;
556 	}
557 
558 	if (!pCoexSta->bPreCCKLock)
559 	{
560 
561 		if (nCCKLockCounter >= 3)
562 		 pCoexSta->bCCKLock = TRUE;
563 		else
564 		 pCoexSta->bCCKLock = FALSE;
565 	}
566 	else
567 	{
568 		if (nCCKLockCounter == 0)
569 		 pCoexSta->bCCKLock = FALSE;
570 		else
571 		 pCoexSta->bCCKLock = TRUE;
572 	}
573 
574 	if  (pCoexSta->bCCKLock)
575 		pCoexSta->bCCKEverLock = TRUE;
576 
577 	pCoexSta->bPreCCKLock =  pCoexSta->bCCKLock;
578 
579 
580 }
581 
582 BOOLEAN
halbtc8703b1ant_IsWifiStatusChanged(IN PBTC_COEXIST pBtCoexist)583 halbtc8703b1ant_IsWifiStatusChanged(
584 	IN	PBTC_COEXIST		pBtCoexist
585 	)
586 {
587 	static BOOLEAN	bPreWifiBusy=FALSE, bPreUnder4way=FALSE, bPreBtHsOn=FALSE;
588 	BOOLEAN bWifiBusy=FALSE, bUnder4way=FALSE, bBtHsOn=FALSE;
589 	BOOLEAN bWifiConnected=FALSE;
590 
591 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
592 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
593 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
594 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &bUnder4way);
595 
596 	if(bWifiConnected)
597 	{
598 		if(bWifiBusy != bPreWifiBusy)
599 		{
600 			bPreWifiBusy = bWifiBusy;
601 			return TRUE;
602 		}
603 		if(bUnder4way != bPreUnder4way)
604 		{
605 			bPreUnder4way = bUnder4way;
606 			return TRUE;
607 		}
608 		if(bBtHsOn != bPreBtHsOn)
609 		{
610 			bPreBtHsOn = bBtHsOn;
611 			return TRUE;
612 		}
613 	}
614 
615 	return FALSE;
616 }
617 
618 VOID
halbtc8703b1ant_UpdateBtLinkInfo(IN PBTC_COEXIST pBtCoexist)619 halbtc8703b1ant_UpdateBtLinkInfo(
620 	IN	PBTC_COEXIST		pBtCoexist
621 	)
622 {
623 	PBTC_BT_LINK_INFO	pBtLinkInfo=&pBtCoexist->btLinkInfo;
624 	BOOLEAN				bBtHsOn=FALSE;
625 
626 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
627 
628 	pBtLinkInfo->bBtLinkExist = pCoexSta->bBtLinkExist;
629 	pBtLinkInfo->bScoExist = pCoexSta->bScoExist;
630 	pBtLinkInfo->bA2dpExist = pCoexSta->bA2dpExist;
631 	pBtLinkInfo->bPanExist = pCoexSta->bPanExist;
632 	pBtLinkInfo->bHidExist = pCoexSta->bHidExist;
633 	pBtLinkInfo->bBtHiPriLinkExist = pCoexSta->bBtHiPriLinkExist;
634 
635 	// work around for HS mode.
636 	if(bBtHsOn)
637 	{
638 		pBtLinkInfo->bPanExist = TRUE;
639 		pBtLinkInfo->bBtLinkExist = TRUE;
640 	}
641 
642 	// check if Sco only
643 	if( pBtLinkInfo->bScoExist &&
644 		!pBtLinkInfo->bA2dpExist &&
645 		!pBtLinkInfo->bPanExist &&
646 		!pBtLinkInfo->bHidExist )
647 		pBtLinkInfo->bScoOnly = TRUE;
648 	else
649 		pBtLinkInfo->bScoOnly = FALSE;
650 
651 	// check if A2dp only
652 	if( !pBtLinkInfo->bScoExist &&
653 		pBtLinkInfo->bA2dpExist &&
654 		!pBtLinkInfo->bPanExist &&
655 		!pBtLinkInfo->bHidExist )
656 		pBtLinkInfo->bA2dpOnly = TRUE;
657 	else
658 		pBtLinkInfo->bA2dpOnly = FALSE;
659 
660 	// check if Pan only
661 	if( !pBtLinkInfo->bScoExist &&
662 		!pBtLinkInfo->bA2dpExist &&
663 		pBtLinkInfo->bPanExist &&
664 		!pBtLinkInfo->bHidExist )
665 		pBtLinkInfo->bPanOnly = TRUE;
666 	else
667 		pBtLinkInfo->bPanOnly = FALSE;
668 
669 	// check if Hid only
670 	if( !pBtLinkInfo->bScoExist &&
671 		!pBtLinkInfo->bA2dpExist &&
672 		!pBtLinkInfo->bPanExist &&
673 		pBtLinkInfo->bHidExist )
674 		pBtLinkInfo->bHidOnly = TRUE;
675 	else
676 		pBtLinkInfo->bHidOnly = FALSE;
677 }
678 
679 u1Byte
halbtc8703b1ant_ActionAlgorithm(IN PBTC_COEXIST pBtCoexist)680 halbtc8703b1ant_ActionAlgorithm(
681 	IN	PBTC_COEXIST		pBtCoexist
682 	)
683 {
684 	PBTC_BT_LINK_INFO	pBtLinkInfo=&pBtCoexist->btLinkInfo;
685 	BOOLEAN				bBtHsOn=FALSE;
686 	u1Byte				algorithm=BT_8703B_1ANT_COEX_ALGO_UNDEFINED;
687 	u1Byte				numOfDiffProfile=0;
688 
689 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
690 
691 	if(!pBtLinkInfo->bBtLinkExist)
692 	{
693 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], No BT link exists!!!\n"));
694 		return algorithm;
695 	}
696 
697 	if(pBtLinkInfo->bScoExist)
698 		numOfDiffProfile++;
699 	if(pBtLinkInfo->bHidExist)
700 		numOfDiffProfile++;
701 	if(pBtLinkInfo->bPanExist)
702 		numOfDiffProfile++;
703 	if(pBtLinkInfo->bA2dpExist)
704 		numOfDiffProfile++;
705 
706 	if(numOfDiffProfile == 1)
707 	{
708 		if(pBtLinkInfo->bScoExist)
709 		{
710 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO only\n"));
711 			algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
712 		}
713 		else
714 		{
715 			if(pBtLinkInfo->bHidExist)
716 			{
717 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = HID only\n"));
718 				algorithm = BT_8703B_1ANT_COEX_ALGO_HID;
719 			}
720 			else if(pBtLinkInfo->bA2dpExist)
721 			{
722 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = A2DP only\n"));
723 				algorithm = BT_8703B_1ANT_COEX_ALGO_A2DP;
724 			}
725 			else if(pBtLinkInfo->bPanExist)
726 			{
727 				if(bBtHsOn)
728 				{
729 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = PAN(HS) only\n"));
730 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANHS;
731 				}
732 				else
733 				{
734 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = PAN(EDR) only\n"));
735 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR;
736 				}
737 			}
738 		}
739 	}
740 	else if(numOfDiffProfile == 2)
741 	{
742 		if(pBtLinkInfo->bScoExist)
743 		{
744 			if(pBtLinkInfo->bHidExist)
745 			{
746 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + HID\n"));
747 				algorithm = BT_8703B_1ANT_COEX_ALGO_HID;
748 			}
749 			else if(pBtLinkInfo->bA2dpExist)
750 			{
751 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + A2DP ==> SCO\n"));
752 				algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
753 			}
754 			else if(pBtLinkInfo->bPanExist)
755 			{
756 				if(bBtHsOn)
757 				{
758 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + PAN(HS)\n"));
759 					algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
760 				}
761 				else
762 				{
763 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + PAN(EDR)\n"));
764 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
765 				}
766 			}
767 		}
768 		else
769 		{
770 			if( pBtLinkInfo->bHidExist &&
771 				pBtLinkInfo->bA2dpExist )
772 			{
773 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = HID + A2DP\n"));
774 				algorithm = BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
775 			}
776 			else if( pBtLinkInfo->bHidExist &&
777 				pBtLinkInfo->bPanExist )
778 			{
779 				if(bBtHsOn)
780 				{
781 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = HID + PAN(HS)\n"));
782 					algorithm = BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
783 				}
784 				else
785 				{
786 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = HID + PAN(EDR)\n"));
787 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
788 				}
789 			}
790 			else if( pBtLinkInfo->bPanExist &&
791 				pBtLinkInfo->bA2dpExist )
792 			{
793 				if(bBtHsOn)
794 				{
795 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = A2DP + PAN(HS)\n"));
796 					algorithm = BT_8703B_1ANT_COEX_ALGO_A2DP_PANHS;
797 				}
798 				else
799 				{
800 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = A2DP + PAN(EDR)\n"));
801 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR_A2DP;
802 				}
803 			}
804 		}
805 	}
806 	else if(numOfDiffProfile == 3)
807 	{
808 		if(pBtLinkInfo->bScoExist)
809 		{
810 			if( pBtLinkInfo->bHidExist &&
811 				pBtLinkInfo->bA2dpExist )
812 			{
813 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n"));
814 				algorithm = BT_8703B_1ANT_COEX_ALGO_HID;
815 			}
816 			else if( pBtLinkInfo->bHidExist &&
817 				pBtLinkInfo->bPanExist )
818 			{
819 				if(bBtHsOn)
820 				{
821 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + HID + PAN(HS)\n"));
822 					algorithm = BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
823 				}
824 				else
825 				{
826 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n"));
827 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
828 				}
829 			}
830 			else if( pBtLinkInfo->bPanExist &&
831 				pBtLinkInfo->bA2dpExist )
832 			{
833 				if(bBtHsOn)
834 				{
835 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n"));
836 					algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
837 				}
838 				else
839 				{
840 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n"));
841 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
842 				}
843 			}
844 		}
845 		else
846 		{
847 			if( pBtLinkInfo->bHidExist &&
848 				pBtLinkInfo->bPanExist &&
849 				pBtLinkInfo->bA2dpExist )
850 			{
851 				if(bBtHsOn)
852 				{
853 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n"));
854 					algorithm = BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
855 				}
856 				else
857 				{
858 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n"));
859 					algorithm = BT_8703B_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
860 				}
861 			}
862 		}
863 	}
864 	else if(numOfDiffProfile >= 3)
865 	{
866 		if(pBtLinkInfo->bScoExist)
867 		{
868 			if( pBtLinkInfo->bHidExist &&
869 				pBtLinkInfo->bPanExist &&
870 				pBtLinkInfo->bA2dpExist )
871 			{
872 				if(bBtHsOn)
873 				{
874 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n"));
875 
876 				}
877 				else
878 				{
879 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n"));
880 					algorithm = BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
881 				}
882 			}
883 		}
884 	}
885 
886 	return algorithm;
887 }
888 
889 VOID
halbtc8703b1ant_SetBtAutoReport(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bEnableAutoReport)890 halbtc8703b1ant_SetBtAutoReport(
891 	IN	PBTC_COEXIST		pBtCoexist,
892 	IN	BOOLEAN			bEnableAutoReport
893 	)
894 {
895 	u1Byte			H2C_Parameter[1] ={0};
896 
897 	H2C_Parameter[0] = 0;
898 
899 	if(bEnableAutoReport)
900 	{
901 		H2C_Parameter[0] |= BIT0;
902 	}
903 
904 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], BT FW auto report : %s, FW write 0x68=0x%x\n",
905 		(bEnableAutoReport? "Enabled!!":"Disabled!!"), H2C_Parameter[0]));
906 
907 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x68, 1, H2C_Parameter);
908 }
909 
910 VOID
halbtc8703b1ant_BtAutoReport(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN BOOLEAN bEnableAutoReport)911 halbtc8703b1ant_BtAutoReport(
912 	IN	PBTC_COEXIST		pBtCoexist,
913 	IN	BOOLEAN			bForceExec,
914 	IN	BOOLEAN			bEnableAutoReport
915 	)
916 {
917 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], %s BT Auto report = %s\n",
918 		(bForceExec? "force to":""), ((bEnableAutoReport)? "Enabled":"Disabled")));
919 	pCoexDm->bCurBtAutoReport = bEnableAutoReport;
920 
921 	if(!bForceExec)
922 	{
923 		if(pCoexDm->bPreBtAutoReport == pCoexDm->bCurBtAutoReport)
924 			return;
925 	}
926 	halbtc8703b1ant_SetBtAutoReport(pBtCoexist, pCoexDm->bCurBtAutoReport);
927 
928 	pCoexDm->bPreBtAutoReport = pCoexDm->bCurBtAutoReport;
929 }
930 
931 VOID
halbtc8703b1ant_SetSwPenaltyTxRateAdaptive(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bLowPenaltyRa)932 halbtc8703b1ant_SetSwPenaltyTxRateAdaptive(
933 	IN	PBTC_COEXIST		pBtCoexist,
934 	IN	BOOLEAN			bLowPenaltyRa
935 	)
936 {
937 	u1Byte			H2C_Parameter[6] ={0};
938 
939 	H2C_Parameter[0] = 0x6;	// opCode, 0x6= Retry_Penalty
940 
941 	if(bLowPenaltyRa)
942 	{
943 		H2C_Parameter[1] |= BIT0;
944 		H2C_Parameter[2] = 0x00;  //normal rate except MCS7/6/5, OFDM54/48/36
945 		H2C_Parameter[3] = 0xf7;  //MCS7 or OFDM54
946 		H2C_Parameter[4] = 0xf8;  //MCS6 or OFDM48
947 		H2C_Parameter[5] = 0xf9;	//MCS5 or OFDM36
948 	}
949 
950 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set WiFi Low-Penalty Retry: %s",
951 		(bLowPenaltyRa? "ON!!":"OFF!!") ));
952 
953 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x69, 6, H2C_Parameter);
954 }
955 
956 VOID
halbtc8703b1ant_LowPenaltyRa(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN BOOLEAN bLowPenaltyRa)957 halbtc8703b1ant_LowPenaltyRa(
958 	IN	PBTC_COEXIST		pBtCoexist,
959 	IN	BOOLEAN			bForceExec,
960 	IN	BOOLEAN			bLowPenaltyRa
961 	)
962 {
963 	pCoexDm->bCurLowPenaltyRa = bLowPenaltyRa;
964 
965 	if(!bForceExec)
966 	{
967 		if(pCoexDm->bPreLowPenaltyRa == pCoexDm->bCurLowPenaltyRa)
968 			return;
969 	}
970 	halbtc8703b1ant_SetSwPenaltyTxRateAdaptive(pBtCoexist, pCoexDm->bCurLowPenaltyRa);
971 
972 	pCoexDm->bPreLowPenaltyRa = pCoexDm->bCurLowPenaltyRa;
973 }
974 
975 VOID
halbtc8703b1ant_SetCoexTable(IN PBTC_COEXIST pBtCoexist,IN u4Byte val0x6c0,IN u4Byte val0x6c4,IN u4Byte val0x6c8,IN u1Byte val0x6cc)976 halbtc8703b1ant_SetCoexTable(
977 	IN	PBTC_COEXIST	pBtCoexist,
978 	IN	u4Byte		val0x6c0,
979 	IN	u4Byte		val0x6c4,
980 	IN	u4Byte		val0x6c8,
981 	IN	u1Byte		val0x6cc
982 	)
983 {
984 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set coex table, set 0x6c0=0x%x\n", val0x6c0));
985 	pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x6c0, val0x6c0);
986 
987 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set coex table, set 0x6c4=0x%x\n", val0x6c4));
988 	pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x6c4, val0x6c4);
989 
990 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set coex table, set 0x6c8=0x%x\n", val0x6c8));
991 	pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x6c8, val0x6c8);
992 
993 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set coex table, set 0x6cc=0x%x\n", val0x6cc));
994 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cc, val0x6cc);
995 }
996 
997 VOID
halbtc8703b1ant_CoexTable(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u4Byte val0x6c0,IN u4Byte val0x6c4,IN u4Byte val0x6c8,IN u1Byte val0x6cc)998 halbtc8703b1ant_CoexTable(
999 	IN	PBTC_COEXIST		pBtCoexist,
1000 	IN	BOOLEAN			bForceExec,
1001 	IN	u4Byte			val0x6c0,
1002 	IN	u4Byte			val0x6c4,
1003 	IN	u4Byte			val0x6c8,
1004 	IN	u1Byte			val0x6cc
1005 	)
1006 {
1007 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], %s write Coex Table 0x6c0=0x%x, 0x6c4=0x%x, 0x6cc=0x%x\n",
1008 		(bForceExec? "force to":""), val0x6c0, val0x6c4, val0x6cc));
1009 	pCoexDm->curVal0x6c0 = val0x6c0;
1010 	pCoexDm->curVal0x6c4 = val0x6c4;
1011 	pCoexDm->curVal0x6c8 = val0x6c8;
1012 	pCoexDm->curVal0x6cc = val0x6cc;
1013 
1014 	if(!bForceExec)
1015 	{
1016 		if( (pCoexDm->preVal0x6c0 == pCoexDm->curVal0x6c0) &&
1017 			(pCoexDm->preVal0x6c4 == pCoexDm->curVal0x6c4) &&
1018 			(pCoexDm->preVal0x6c8 == pCoexDm->curVal0x6c8) &&
1019 			(pCoexDm->preVal0x6cc == pCoexDm->curVal0x6cc) )
1020 			return;
1021 	}
1022 	halbtc8703b1ant_SetCoexTable(pBtCoexist, val0x6c0, val0x6c4, val0x6c8, val0x6cc);
1023 
1024 	pCoexDm->preVal0x6c0 = pCoexDm->curVal0x6c0;
1025 	pCoexDm->preVal0x6c4 = pCoexDm->curVal0x6c4;
1026 	pCoexDm->preVal0x6c8 = pCoexDm->curVal0x6c8;
1027 	pCoexDm->preVal0x6cc = pCoexDm->curVal0x6cc;
1028 }
1029 
1030 VOID
halbtc8703b1ant_CoexTableWithType(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u1Byte type)1031 halbtc8703b1ant_CoexTableWithType(
1032 	IN	PBTC_COEXIST		pBtCoexist,
1033 	IN	BOOLEAN				bForceExec,
1034 	IN	u1Byte				type
1035 	)
1036 {
1037 	PBTC_BOARD_INFO	pBoardInfo=&pBtCoexist->boardInfo;
1038 
1039 #if BT_8703B_1ANT_ANTDET_ENABLE
1040 #if BT_8703B_1ANT_ANTDET_COEXMECHANISMSWITCH_ENABLE
1041 	if (pBoardInfo->btdmAntNumByAntDet == 2)
1042 	{
1043 		if (type == 3)
1044 			type = 14;
1045 		else if (type == 4)
1046 			type  = 13;
1047 		else if (type == 5)
1048 			type = 8;
1049 	}
1050 #endif
1051 #endif
1052 
1053 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], ********** CoexTable(%d) **********\n", type));
1054 
1055 	pCoexSta->nCoexTableType = type;
1056 
1057 	switch(type)
1058 	{
1059 		case 0:
1060 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55555555, 0x55555555, 0xffffff, 0x3);
1061 			break;
1062 		case 1:
1063 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55555555, 0x5a5a5a5a, 0xffffff, 0x3);
1064 			break;
1065 		case 2:
1066 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x5a5a5a5a, 0x5a5a5a5a, 0xffffff, 0x3);
1067 			break;
1068 		case 3:
1069 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55555555, 0x5a5a5a5a, 0xffffff, 0x3);
1070 			break;
1071 		case 4:
1072 			if ((pCoexSta->nScanAPNum > 5) && (pCoexSta->bCCKEverLock) && (pCoexSta->bCCKLock))
1073 				halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55aa5555, 0xaaaaaaaa, 0xffffff, 0x3);
1074 			else if (pCoexSta->bCCKEverLock)
1075 				halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55555555, 0xaaaaaaaa, 0xffffff, 0x3);
1076 			else
1077 				halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55555555, 0xaaaa5a5a, 0xffffff, 0x3);
1078 			break;
1079 		case 5:
1080 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x5a5a5a5a, 0x5a5a5a5a, 0xffffff, 0x3);
1081 			break;
1082 		case 6:
1083 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55555555, 0xaaaaaaaa, 0xffffff, 0x3);
1084 			break;
1085 		case 7:
1086 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0xaaaaaaaa, 0xaaaaaaaa, 0xffffff, 0x3);
1087 			break;
1088 		case 8:
1089 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55dd55dd, 0x5ada5ada, 0xffffff, 0x3);
1090 			break;
1091 		case 9:
1092 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55dd55dd, 0x5ada5ada, 0xffffff, 0x3);
1093 			break;
1094 		case 10:
1095 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55dd55dd, 0x5ada5ada, 0xffffff, 0x3);
1096 			break;
1097 		case 11:
1098 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55dd55dd, 0x5ada5ada, 0xffffff, 0x3);
1099 			break;
1100 		case 12:
1101 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55dd55dd, 0x5ada5ada, 0xffffff, 0x3);
1102 			break;
1103 		case 13:
1104 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x5fff5fff, 0xaaaaaaaa, 0xffffff, 0x3);
1105 			break;
1106 		case 14:
1107 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x5fff5fff, 0x5ada5ada, 0xffffff, 0x3);
1108 			break;
1109 		case 15:
1110 			halbtc8703b1ant_CoexTable(pBtCoexist, bForceExec, 0x55dd55dd, 0xaaaaaaaa, 0xffffff, 0x3);
1111 			break;
1112 		default:
1113 			break;
1114 	}
1115 }
1116 
1117 VOID
halbtc8703b1ant_SetFwIgnoreWlanAct(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bEnable)1118 halbtc8703b1ant_SetFwIgnoreWlanAct(
1119 	IN	PBTC_COEXIST		pBtCoexist,
1120 	IN	BOOLEAN			bEnable
1121 	)
1122 {
1123 	u1Byte			H2C_Parameter[1] ={0};
1124 
1125 	if(bEnable)
1126 	{
1127 		H2C_Parameter[0] |= BIT0;		// function enable
1128 	}
1129 
1130 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63=0x%x\n",
1131 		H2C_Parameter[0]));
1132 
1133 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x63, 1, H2C_Parameter);
1134 }
1135 
1136 VOID
halbtc8703b1ant_IgnoreWlanAct(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN BOOLEAN bEnable)1137 halbtc8703b1ant_IgnoreWlanAct(
1138 	IN	PBTC_COEXIST		pBtCoexist,
1139 	IN	BOOLEAN			bForceExec,
1140 	IN	BOOLEAN			bEnable
1141 	)
1142 {
1143 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], %s turn Ignore WlanAct %s\n",
1144 		(bForceExec? "force to":""), (bEnable? "ON":"OFF")));
1145 	pCoexDm->bCurIgnoreWlanAct = bEnable;
1146 
1147 	if(!bForceExec)
1148 	{
1149 		if(pCoexDm->bPreIgnoreWlanAct == pCoexDm->bCurIgnoreWlanAct)
1150 			return;
1151 	}
1152 	halbtc8703b1ant_SetFwIgnoreWlanAct(pBtCoexist, bEnable);
1153 
1154 	pCoexDm->bPreIgnoreWlanAct = pCoexDm->bCurIgnoreWlanAct;
1155 }
1156 
1157 VOID
halbtc8703b1ant_SetLpsRpwm(IN PBTC_COEXIST pBtCoexist,IN u1Byte lpsVal,IN u1Byte rpwmVal)1158 halbtc8703b1ant_SetLpsRpwm(
1159 	IN	PBTC_COEXIST		pBtCoexist,
1160 	IN	u1Byte			lpsVal,
1161 	IN	u1Byte			rpwmVal
1162 	)
1163 {
1164 	u1Byte	lps=lpsVal;
1165 	u1Byte	rpwm=rpwmVal;
1166 
1167 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_LPS_VAL, &lps);
1168 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
1169 }
1170 
1171 VOID
halbtc8703b1ant_LpsRpwm(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN u1Byte lpsVal,IN u1Byte rpwmVal)1172 halbtc8703b1ant_LpsRpwm(
1173 	IN	PBTC_COEXIST		pBtCoexist,
1174 	IN	BOOLEAN			bForceExec,
1175 	IN	u1Byte			lpsVal,
1176 	IN	u1Byte			rpwmVal
1177 	)
1178 {
1179 	BOOLEAN	bForceExecPwrCmd=FALSE;
1180 
1181 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], %s set lps/rpwm=0x%x/0x%x \n",
1182 		(bForceExec? "force to":""), lpsVal, rpwmVal));
1183 	pCoexDm->curLps = lpsVal;
1184 	pCoexDm->curRpwm = rpwmVal;
1185 
1186 	if(!bForceExec)
1187 	{
1188 		if( (pCoexDm->preLps == pCoexDm->curLps) &&
1189 			(pCoexDm->preRpwm == pCoexDm->curRpwm) )
1190 		{
1191 			return;
1192 		}
1193 	}
1194 	halbtc8703b1ant_SetLpsRpwm(pBtCoexist, lpsVal, rpwmVal);
1195 
1196 	pCoexDm->preLps = pCoexDm->curLps;
1197 	pCoexDm->preRpwm = pCoexDm->curRpwm;
1198 }
1199 
1200 VOID
halbtc8703b1ant_SwMechanism(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bLowPenaltyRA)1201 halbtc8703b1ant_SwMechanism(
1202 	IN	PBTC_COEXIST	pBtCoexist,
1203 	IN	BOOLEAN 		bLowPenaltyRA
1204 	)
1205 {
1206 	halbtc8703b1ant_LowPenaltyRa(pBtCoexist, NORMAL_EXEC, bLowPenaltyRA);
1207 }
1208 
1209 VOID
halbtc8703b1ant_SetAntPath(IN PBTC_COEXIST pBtCoexist,IN u1Byte antPosType,IN BOOLEAN bForceExec,IN BOOLEAN bInitHwCfg,IN BOOLEAN bWifiOff)1210 halbtc8703b1ant_SetAntPath(
1211 	IN	PBTC_COEXIST		pBtCoexist,
1212 	IN	u1Byte				antPosType,
1213 	IN	BOOLEAN				bForceExec,
1214 	IN	BOOLEAN				bInitHwCfg,
1215 	IN	BOOLEAN				bWifiOff
1216 	)
1217 {
1218 	PBTC_BOARD_INFO pBoardInfo=&pBtCoexist->boardInfo;
1219 	u4Byte			fwVer=0, u4Tmp=0, cntBtCalChk=0;
1220 	BOOLEAN			bPgExtSwitch=FALSE;
1221 	BOOLEAN			bUseExtSwitch=FALSE;
1222 	BOOLEAN			bIsInMpMode = FALSE;
1223 	u1Byte			H2C_Parameter[2] ={0}, u1Tmp = 0;
1224 
1225 	pCoexDm->curAntPosType = antPosType;
1226 
1227 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_EXT_SWITCH, &bPgExtSwitch);
1228 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);	// [31:16]=fw ver, [15:0]=fw sub ver
1229 
1230 	if((fwVer>0 && fwVer<0xc0000) || bPgExtSwitch)
1231 		bUseExtSwitch = TRUE;
1232 
1233 #if BT_8703B_1ANT_ANTDET_ENABLE
1234 #if BT_8703B_1ANT_ANTDET_COEXMECHANISMSWITCH_ENABLE
1235 		if (antPosType == BTC_ANT_PATH_PTA)
1236 		{
1237 			if ((pBoardInfo->btdmAntDetFinish) && (pBoardInfo->btdmAntNumByAntDet == 2))
1238 			{
1239 				if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1240 					antPosType = BTC_ANT_PATH_WIFI;
1241 				else
1242 					antPosType = BTC_ANT_PATH_BT;
1243 			}
1244 		}
1245 #endif
1246 #endif
1247 
1248 	if(bInitHwCfg)
1249 	{
1250 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x780); //WiFi TRx Mask on
1251 		//remove due to interrupt is disabled that polling c2h will fail and delay 100ms.
1252 		//pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x15); //BT TRx Mask on
1253 
1254 		if(fwVer >= 0x180000)
1255 		{
1256 		/* Use H2C to set GNT_BT to HIGH */
1257 		H2C_Parameter[0] = 1;
1258 		pBtCoexist->fBtcFillH2c(pBtCoexist, 0x6E, 1, H2C_Parameter);
1259 		}
1260 		else
1261 		{
1262 			// set grant_bt to high
1263 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
1264 		}
1265 		//set wlan_act control by PTA
1266 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
1267 
1268 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x0); //BT select s0/s1 is controlled by BT
1269 
1270 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x39, 0x8, 0x1);
1271 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x974, 0xff);
1272 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x944, 0x3, 0x3);
1273 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x930, 0x77);
1274 	}
1275 	else if(bWifiOff)
1276 	{
1277 		if(fwVer >= 0x180000)
1278 		{
1279 		/* Use H2C to set GNT_BT to HIGH */
1280 		H2C_Parameter[0] = 1;
1281 		pBtCoexist->fBtcFillH2c(pBtCoexist, 0x6E, 1, H2C_Parameter);
1282 		}
1283 		else
1284 		{
1285 			// set grant_bt to high
1286 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
1287 		}
1288 		//set wlan_act to always low
1289 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
1290 
1291 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_IS_IN_MP_MODE, &bIsInMpMode);
1292 		if(!bIsInMpMode)
1293 			pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x0); //BT select s0/s1 is controlled by BT
1294 		else
1295 			pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x1); //BT select s0/s1 is controlled by WiFi
1296 
1297 		// 0x4c[24:23]=00, Set Antenna control by BT_RFE_CTRL	BT Vendor 0xac=0xf002
1298 		u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
1299 		u4Tmp &= ~BIT23;
1300 		u4Tmp &= ~BIT24;
1301 		pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x4c, u4Tmp);
1302 	}
1303 	else
1304 	{
1305 		/* Use H2C to set GNT_BT to LOW */
1306 		if(fwVer >= 0x180000)
1307 		{
1308 		if (pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765) != 0)
1309 		{
1310 			H2C_Parameter[0] = 0;
1311 			pBtCoexist->fBtcFillH2c(pBtCoexist, 0x6E, 1, H2C_Parameter);
1312 		}
1313 		}
1314 		else
1315 		{
1316 			// BT calibration check
1317 			while(cntBtCalChk <= 20)
1318 			{
1319 				u1Tmp = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x49d);
1320 				cntBtCalChk++;
1321 				if(u1Tmp & BIT0)
1322 				{
1323 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ########### BT is calibrating (wait cnt=%d) ###########\n", cntBtCalChk));
1324 					delay_ms(50);
1325 				}
1326 				else
1327 				{
1328 					RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ********** BT is NOT calibrating (wait cnt=%d)**********\n", cntBtCalChk));
1329 					break;
1330 				}
1331 			}
1332 
1333 			// set grant_bt to PTA
1334 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x0);
1335 		}
1336 
1337 		if (pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e) != 0xc)
1338 		{
1339 			//set wlan_act control by PTA
1340 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0xc);
1341 		}
1342 
1343 		pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x1); //BT select s0/s1 is controlled by WiFi
1344 	}
1345 
1346 	if(bUseExtSwitch)
1347 	{
1348 		if(bInitHwCfg)
1349 		{
1350 			// 0x4c[23]=0, 0x4c[24]=1  Antenna control by WL/BT
1351 			u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
1352 			u4Tmp &=~BIT23;
1353 			u4Tmp |= BIT24;
1354 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x4c, u4Tmp);
1355 
1356 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0); // fixed internal switch S1->WiFi, S0->BT
1357 
1358 			if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1359 			{
1360 				//tell firmware "no antenna inverse"
1361 				H2C_Parameter[0] = 0;
1362 				H2C_Parameter[1] = 1;  //ext switch type
1363 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1364 			}
1365 			else
1366 			{
1367 				//tell firmware "antenna inverse"
1368 				H2C_Parameter[0] = 1;
1369 				H2C_Parameter[1] = 1;  //ext switch type
1370 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1371 			}
1372 		}
1373 
1374 		if(bForceExec || (pCoexDm->curAntPosType != pCoexDm->preAntPosType))
1375 		{
1376 			// ext switch setting
1377 			switch(antPosType)
1378 			{
1379 				case BTC_ANT_PATH_WIFI:
1380 					if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1381 						pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x1);
1382 					else
1383 						pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x2);
1384 					break;
1385 				case BTC_ANT_PATH_BT:
1386 					if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1387 						pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x2);
1388 					else
1389 						pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x1);
1390 					break;
1391 				default:
1392 				case BTC_ANT_PATH_PTA:
1393 					if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1394 						pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x1);
1395 					else
1396 						pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x92c, 0x3, 0x2);
1397 					break;
1398 			}
1399 		}
1400 	}
1401 	else
1402 	{
1403 		if(bInitHwCfg)
1404 		{
1405 			// 0x4c[23]=1, 0x4c[24]=0  Antenna control by 0x64
1406 			u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
1407 			u4Tmp |= BIT23;
1408 			u4Tmp &=~BIT24;
1409 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x4c, u4Tmp);
1410 
1411 			//Fix Ext switch Main->S1, Aux->S0
1412 			pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x64, 0x1, 0x0);
1413 
1414 			if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1415 			{
1416 
1417 				//tell firmware "no antenna inverse"
1418 				H2C_Parameter[0] = 0;
1419 				H2C_Parameter[1] = 0;  //internal switch type
1420 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1421 			}
1422 			else
1423 			{
1424 
1425 				//tell firmware "antenna inverse"
1426 				H2C_Parameter[0] = 1;
1427 				H2C_Parameter[1] = 0;  //internal switch type
1428 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x65, 2, H2C_Parameter);
1429 			}
1430 		}
1431 
1432 		if(bForceExec || (pCoexDm->curAntPosType != pCoexDm->preAntPosType))
1433 		{
1434 			// internal switch setting
1435 			switch(antPosType)
1436 			{
1437 				case BTC_ANT_PATH_WIFI:
1438 					if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1439 						pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
1440 					else
1441 						pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
1442 					break;
1443 				case BTC_ANT_PATH_BT:
1444 					if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1445  						pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
1446 					else
1447 						pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
1448 					break;
1449 				default:
1450 				case BTC_ANT_PATH_PTA:
1451 					if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
1452 						pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x200);
1453 					else
1454 						pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x80);
1455 					break;
1456 			}
1457 		}
1458 	}
1459 
1460 	pCoexDm->preAntPosType = pCoexDm->curAntPosType;
1461 }
1462 
1463 VOID
halbtc8703b1ant_SetAntPathDCut(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bAntennaAux,IN BOOLEAN bExtSwitch,IN BOOLEAN bTwoAntenna,IN u1Byte antennaPos,IN u1Byte wifiState)1464 halbtc8703b1ant_SetAntPathDCut(
1465 	IN	PBTC_COEXIST		pBtCoexist,
1466 	IN	BOOLEAN				bAntennaAux,	//For 1-Ant--> 1: Antenna at S0, 0: Antenna at S1. Set 0 for 2-Ant
1467 	IN	BOOLEAN				bExtSwitch,		// 1: Ext Switch (SPDT) exist on module, 0: no Ext Switch (SPDT) exist on module
1468 	IN	BOOLEAN				bTwoAntenna,	// 1: 2-Antenna, 0:1-Antenna
1469 	IN	u1Byte				antennaPos,		//Set Antenna Pos, For 1-Ant: BTC_ANT_PATH_WIFI, BTC_ANT_PATH_BT, BTC_ANT_PATH_PTA, For 2-Ant:BTC_ANT_WIFI_AT_MAIN, BTC_ANT_WIFI_AT_Aux
1470 	IN	u1Byte				wifiState		//BTC_WIFI_STAT_INIT, BTC_WIFI_STAT_IQK, BTC_WIFI_STAT_NORMAL_OFF, BTC_WIFI_STAT_MP_OFF, BTC_WIFI_STAT_NORMAL, BTC_WIFI_STAT_ANT_DIV
1471 	)
1472 {
1473 #if 0
1474 	u1Byte	dataLen=5;
1475 	u1Byte	buf[6] = {0};
1476 
1477 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], set BT Ant, bAntennaAux/bExtSwitch/bTwoAntenna/antennaPos/wifiState=%d/%d/%d/%d/%d\n",
1478 		bAntennaAux, bExtSwitch, bTwoAntenna, antennaPos, wifiState));
1479 
1480 	buf[0] = dataLen;
1481 
1482 	if(bAntennaAux)
1483 		buf[1] = 0x1;
1484 
1485 	if(bExtSwitch)
1486 		buf[2] = 0x1;
1487 
1488 	if(bTwoAntenna)
1489 		buf[3] = 0x1;
1490 
1491 	buf[4] = antennaPos;
1492 
1493 	buf[5] = wifiState;
1494 
1495 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_CTRL_8703B_ANT, (PVOID)&buf[0]);
1496 #endif
1497 }
1498 
1499 VOID
halbtc8703b1ant_SetFwPstdma(IN PBTC_COEXIST pBtCoexist,IN u1Byte byte1,IN u1Byte byte2,IN u1Byte byte3,IN u1Byte byte4,IN u1Byte byte5)1500 halbtc8703b1ant_SetFwPstdma(
1501 	IN	PBTC_COEXIST		pBtCoexist,
1502 	IN	u1Byte			byte1,
1503 	IN	u1Byte			byte2,
1504 	IN	u1Byte			byte3,
1505 	IN	u1Byte			byte4,
1506 	IN	u1Byte			byte5
1507 	)
1508 {
1509 	u1Byte			H2C_Parameter[5] ={0};
1510 	u1Byte			realByte1=byte1, realByte5=byte5;
1511 	BOOLEAN			bApEnable=FALSE;
1512 
1513 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
1514 
1515 	if(bApEnable)
1516 	{
1517 		if(byte1&BIT4 && !(byte1&BIT5))
1518 		{
1519 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], FW for 1Ant AP mode\n"));
1520 			realByte1 &= ~BIT4;
1521 			realByte1 |= BIT5;
1522 
1523 			realByte5 |= BIT5;
1524 			realByte5 &= ~BIT6;
1525 		}
1526 	}
1527 
1528 	H2C_Parameter[0] = realByte1;
1529 	H2C_Parameter[1] = byte2;
1530 	H2C_Parameter[2] = byte3;
1531 	H2C_Parameter[3] = byte4;
1532 	H2C_Parameter[4] = realByte5;
1533 
1534 	pCoexDm->psTdmaPara[0] = realByte1;
1535 	pCoexDm->psTdmaPara[1] = byte2;
1536 	pCoexDm->psTdmaPara[2] = byte3;
1537 	pCoexDm->psTdmaPara[3] = byte4;
1538 	pCoexDm->psTdmaPara[4] = realByte5;
1539 
1540 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], PS-TDMA H2C cmd =0x%x%08x\n",
1541 		H2C_Parameter[0],
1542 		H2C_Parameter[1]<<24|H2C_Parameter[2]<<16|H2C_Parameter[3]<<8|H2C_Parameter[4]));
1543 
1544 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x60, 5, H2C_Parameter);
1545 }
1546 
1547 
1548 VOID
halbtc8703b1ant_PsTdma(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bForceExec,IN BOOLEAN bTurnOn,IN u1Byte type)1549 halbtc8703b1ant_PsTdma(
1550 	IN	PBTC_COEXIST		pBtCoexist,
1551 	IN	BOOLEAN			bForceExec,
1552 	IN	BOOLEAN			bTurnOn,
1553 	IN	u1Byte			type
1554 	)
1555 {
1556 	PBTC_BOARD_INFO	pBoardInfo=&pBtCoexist->boardInfo;
1557 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
1558 	BOOLEAN			bTurnOnByCnt=FALSE, bWifiBusy=FALSE, bWiFiNoisy=FALSE;
1559 	u1Byte			psTdmaTypeByCnt=0, rssiAdjustVal=0;
1560 	u1Byte			psTdmaByte4Val = 0x50, psTdmaByte0Val = 0x51, psTdmaByte3Val =  0x10;
1561 	s1Byte			nWiFiDurationAdjust = 0x0;
1562 	static BOOLEAN	 bPreWifiBusy=FALSE;
1563 
1564 	pCoexDm->bCurPsTdmaOn = bTurnOn;
1565 	pCoexDm->curPsTdma = type;
1566 
1567 #if BT_8703B_1ANT_ANTDET_ENABLE
1568 #if BT_8703B_1ANT_ANTDET_COEXMECHANISMSWITCH_ENABLE
1569 	if (pBoardInfo->btdmAntNumByAntDet == 2)
1570 	{
1571 		if (bTurnOn)
1572 			type = type +100;  //for WiFi RSSI low or BT RSSI low
1573 		else
1574 			type = 1; //always translate to TDMA(off,1) for TDMA-off case
1575 	}
1576 
1577 #endif
1578 #endif
1579 
1580 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
1581 
1582 	if (bWifiBusy != bPreWifiBusy)
1583 	{
1584 		bForceExec = TRUE;
1585 		bPreWifiBusy = bWifiBusy;
1586 	}
1587 
1588 	if (pCoexDm->bCurPsTdmaOn)
1589 	{
1590 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ********** TDMA(on, %d) **********\n",
1591 				pCoexDm->curPsTdma));
1592 	}
1593 	else
1594 	{
1595 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ********** TDMA(off, %d) **********\n",
1596 				pCoexDm->curPsTdma));
1597 	}
1598 
1599 	if(!bForceExec)
1600 	{
1601 		if( (pCoexDm->bPrePsTdmaOn == pCoexDm->bCurPsTdmaOn) &&
1602 			(pCoexDm->prePsTdma == pCoexDm->curPsTdma) )
1603 			return;
1604 	}
1605 
1606 	if (pCoexSta->nScanAPNum <= 5)
1607 		nWiFiDurationAdjust = 5;
1608 		//nWiFiDurationAdjust = 2;
1609 	else if  (pCoexSta->nScanAPNum >= 40)
1610 		nWiFiDurationAdjust = -15;
1611 	else if  (pCoexSta->nScanAPNum >= 20)
1612 		nWiFiDurationAdjust = -10;
1613 
1614 	if ((type == 1) || (type == 2) || (type == 9) || (type == 11) || (type == 101)
1615 		|| (type == 102) || (type == 109) || (type == 101))
1616 	{
1617 		if (!pCoexSta->bForceLpsOn)  //Native power save TDMA, only for A2DP-only case 1/2/9/11 while wifi noisy threshold > 30
1618 	{
1619 		psTdmaByte0Val = 0x61;  //no null-pkt
1620 		psTdmaByte3Val = 0x11; // no tx-pause at BT-slot
1621 		psTdmaByte4Val = 0x10; // 0x778 = d/1 toggle, no dynamic slot
1622 	}
1623 		else
1624 		{
1625 			psTdmaByte0Val = 0x51;  //null-pkt
1626 			psTdmaByte3Val = 0x10; //tx-pause at BT-slot
1627 			psTdmaByte4Val = 0x50; // 0x778 = d/1 toggle, dynamic slot
1628 		}
1629 	}
1630 	else if ((type == 3) || (type == 13) || (type == 14) || (type == 103) || (type == 113) || (type == 114))
1631 	{
1632 		psTdmaByte0Val = 0x51;  //null-pkt
1633 		psTdmaByte3Val = 0x10; //tx-pause at BT-slot
1634 		psTdmaByte4Val = 0x10; // 0x778 = d/1 toggle, no dynamic slot
1635 #if 0
1636 		if (!bWifiBusy)
1637 			psTdmaByte4Val = psTdmaByte4Val | 0x1;  //0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts)
1638 #endif
1639 	}
1640 	else  //native power save case
1641 	{
1642 		psTdmaByte0Val = 0x61;  //no null-pkt
1643 		psTdmaByte3Val = 0x11; // no tx-pause at BT-slot
1644 		psTdmaByte4Val = 0x11; // 0x778 = d/1 toggle, no dynamic slot
1645 		//psTdmaByte4Va is not defne for 0x778 = d/1, 1/1 case
1646 		}
1647 
1648 	//if (pBtLinkInfo->bSlaveRole == TRUE)
1649 	if ((pBtLinkInfo->bSlaveRole == TRUE)	&& (pBtLinkInfo->bA2dpExist))
1650 		psTdmaByte4Val = psTdmaByte4Val | 0x1;  //0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts)
1651 
1652 	if (type > 100)
1653 	{
1654 		psTdmaByte0Val = psTdmaByte0Val | 0x82; //set antenna control by SW
1655 		psTdmaByte3Val = psTdmaByte3Val | 0x60;  //set antenna no toggle, control by antenna diversity
1656 	}
1657 
1658 
1659 	if(bTurnOn)
1660 	{
1661 		switch(type)
1662 		{
1663 			default:
1664 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x51, 0x1a, 0x1a, 0x0, psTdmaByte4Val);
1665 				break;
1666 			case 1:
1667 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x3a+nWiFiDurationAdjust, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1668 				break;
1669 			case 2:
1670 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x2d+nWiFiDurationAdjust, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1671 				break;
1672 			case 3:
1673 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x3a, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1674 				break;
1675 			case 4:
1676 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x93, 0x15, 0x3, 0x14, 0x0);
1677 				break;
1678 			case 5:
1679 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x15, 0x3, psTdmaByte3Val, 0x11);
1680 				break;
1681 			case 6:
1682 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x20, 0x3, psTdmaByte3Val, 0x11);
1683 				break;
1684 			case 7:
1685 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x13, 0xc, 0x5, 0x0, 0x0);
1686 				break;
1687 			case 8:
1688 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x93, 0x25, 0x3, 0x10, 0x0);
1689 				break;
1690 			case 9:
1691 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1692 				break;
1693 			case 10:
1694 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x13, 0xa, 0xa, 0x0, 0x40);
1695 				break;
1696 			case 11:
1697 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1698 				break;
1699 			case 12:
1700 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x51, 0x0a, 0x0a, 0x0, 0x50);
1701 				break;
1702 			case 13:
1703 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1704 				break;
1705 			case 14:
1706 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1707 				break;
1708 			case 15:
1709 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x13, 0xa, 0x3, 0x8, 0x0);
1710 				break;
1711 			case 16:
1712 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x93, 0x15, 0x3, 0x10, 0x0);
1713 				break;
1714 			case 18:
1715 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x93, 0x25, 0x3, 0x10, 0x0);
1716 				break;
1717 			case 20:
1718 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x3f, 0x03, psTdmaByte3Val, 0x10);
1719 				break;
1720 			case 21:
1721 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x61, 0x25, 0x03, 0x11, 0x11);
1722 				break;
1723 			case 22:
1724 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x25, 0x03, psTdmaByte3Val, 0x10);
1725 				break;
1726 			case 23:
1727 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xe3, 0x25, 0x3, 0x31, 0x18);
1728 				break;
1729 			case 24:
1730 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xe3, 0x15, 0x3, 0x31, 0x18);
1731 				break;
1732 			case 25:
1733 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xe3, 0xa, 0x3, 0x31, 0x18);
1734 				break;
1735 			case 26:
1736 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xe3, 0xa, 0x3, 0x31, 0x18);
1737 				break;
1738 			case 27:
1739 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xe3, 0x25, 0x3, 0x31, 0x98);
1740 				break;
1741 			case 28:
1742 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x69, 0x25, 0x3, 0x31, 0x0);
1743 				break;
1744 			case 29:
1745 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xab, 0x1a, 0x1a, 0x1, 0x10);
1746 				break;
1747 			case 30:
1748 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x51, 0x30, 0x3, 0x10, 0x10);
1749 				break;
1750 			case 31:
1751 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xd3, 0x1a, 0x1a, 0, 0x58);
1752 				break;
1753 			case 32:
1754 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x35, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1755 				break;
1756 			case 33:
1757 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x35, 0x3, psTdmaByte3Val, 0x10);
1758 				break;
1759 			case 34:
1760 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x53, 0x1a, 0x1a, 0x0, 0x10);
1761 				break;
1762 			case 35:
1763 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x63, 0x1a, 0x1a, 0x0, 0x10);
1764 				break;
1765 			case 36:
1766 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0xd3, 0x12, 0x3, 0x14, 0x50);
1767 				break;
1768 			case 40: // SoftAP only with no sta associated,BT disable ,TDMA mode for power saving
1769 				/* here softap mode screen off will cost 70-80mA for phone */
1770 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x23, 0x18, 0x00, 0x10, 0x24);
1771 				break;
1772 
1773 			//for 1-Ant translate to 2-Ant
1774 			case 101:
1775 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x3a+nWiFiDurationAdjust, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1776 				break;
1777 			case 102:
1778 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x2d+nWiFiDurationAdjust, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1779 				break;
1780 			case 103:
1781 				//halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x51, 0x1d, 0x1d, 0x0, psTdmaByte4Val);
1782 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x3a, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1783 				break;
1784 			case 105:
1785 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x15, 0x3, psTdmaByte3Val, 0x11);
1786 				break;
1787 			case 106:
1788 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x20, 0x3, psTdmaByte3Val, 0x11);
1789 				break;
1790 			case 109:
1791 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1792 				break;
1793 			case 111:
1794 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1795 				break;
1796 			case 113:
1797 				//halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x51, 0x12, 0x12, 0x0, psTdmaByte4Val);
1798 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1799 				break;
1800 			case 114:
1801 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x21, 0x3, psTdmaByte3Val, psTdmaByte4Val);
1802 				break;
1803 			case 120:
1804 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x3f, 0x03, psTdmaByte3Val, 0x10);
1805 				break;
1806 			case 122:
1807 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x25, 0x03, psTdmaByte3Val, 0x10);
1808 				break;
1809 			case 132:
1810 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x25, 0x03, psTdmaByte3Val, psTdmaByte4Val);
1811 				break;
1812 			case 133:
1813 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, psTdmaByte0Val, 0x25, 0x03, psTdmaByte3Val, 0x11);
1814 				break;
1815 
1816 		}
1817 	}
1818 	else
1819 	{
1820 
1821 		// disable PS tdma
1822 		switch(type)
1823 		{
1824 			case 8: //PTA Control
1825 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x8, 0x0, 0x0, 0x0, 0x0);
1826 				break;
1827 			case 0:
1828 			default:  //Software control, Antenna at BT side
1829 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x0, 0x0, 0x0, 0x0, 0x0);
1830 				break;
1831 			case 1: // 2-Ant, 0x778=3, antenna control by antenna diversity
1832 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x0, 0x0, 0x0, 0x48, 0x0);
1833 				break;
1834 #if 0
1835 			case 9:   //Software control, Antenna at WiFi side
1836 				halbtc8703b1ant_SetFwPstdma(pBtCoexist, 0x0, 0x0, 0x0, 0x0, 0x0);
1837 				//halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_WIFI, BTC_WIFI_STAT_NORMAL);
1838 				halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_WIFI, FALSE, FALSE);
1839 				break;
1840 #endif
1841 		}
1842 	}
1843 	rssiAdjustVal =0;
1844 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssiAdjustVal);
1845 
1846 
1847 	RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
1848 		pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948),   pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765), pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67)));
1849 
1850 	// update pre state
1851 	pCoexDm->bPrePsTdmaOn = pCoexDm->bCurPsTdmaOn;
1852 	pCoexDm->prePsTdma = pCoexDm->curPsTdma;
1853 }
1854 
1855 BOOLEAN
halbtc8703b1ant_IsCommonAction(IN PBTC_COEXIST pBtCoexist)1856 halbtc8703b1ant_IsCommonAction(
1857 	IN	PBTC_COEXIST		pBtCoexist
1858 	)
1859 {
1860 	BOOLEAN			bCommon=FALSE, bWifiConnected=FALSE, bWifiBusy=FALSE;
1861 
1862 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
1863 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
1864 
1865 	if(!bWifiConnected &&
1866 		BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus)
1867 	{
1868 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n"));
1869 
1870  		//halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
1871 
1872 		bCommon = TRUE;
1873 	}
1874 	else if(bWifiConnected &&
1875 		(BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus) )
1876 	{
1877 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi connected + BT non connected-idle!!\n"));
1878 
1879 		//halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
1880 
1881 		bCommon = TRUE;
1882 	}
1883 	else if(!bWifiConnected &&
1884 		(BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus) )
1885 	{
1886 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi non connected-idle + BT connected-idle!!\n"));
1887 
1888 		//halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
1889 
1890 		bCommon = TRUE;
1891 	}
1892 	else if(bWifiConnected &&
1893 		(BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus) )
1894 	{
1895 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi connected + BT connected-idle!!\n"));
1896 
1897 		//halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
1898 
1899 		bCommon = TRUE;
1900 	}
1901 	else if(!bWifiConnected &&
1902 		(BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE != pCoexDm->btStatus) )
1903 	{
1904 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi non connected-idle + BT Busy!!\n"));
1905 
1906 		//halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
1907 
1908 		bCommon = TRUE;
1909 	}
1910 	else
1911 	{
1912 		if (bWifiBusy)
1913 		{
1914 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi Connected-Busy + BT Busy!!\n"));
1915 		}
1916 		else
1917 		{
1918 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Wifi Connected-Idle + BT Busy!!\n"));
1919 		}
1920 
1921 		bCommon = FALSE;
1922 	}
1923 
1924 	return bCommon;
1925 }
1926 
1927 
1928 VOID
halbtc8703b1ant_TdmaDurationAdjustForAcl(IN PBTC_COEXIST pBtCoexist,IN u1Byte wifiStatus)1929 halbtc8703b1ant_TdmaDurationAdjustForAcl(
1930 	IN	PBTC_COEXIST		pBtCoexist,
1931 	IN	u1Byte				wifiStatus
1932 	)
1933 {
1934 	static s4Byte		up,dn,m,n,WaitCount;
1935 	s4Byte			result;   //0: no change, +1: increase WiFi duration, -1: decrease WiFi duration
1936 	u1Byte			retryCount=0, btInfoExt;
1937 	static BOOLEAN	bPreWifiBusy=FALSE;
1938 	BOOLEAN			bWifiBusy = FALSE;
1939 
1940 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], TdmaDurationAdjustForAcl()\n"));
1941 
1942 	if(BT_8703B_1ANT_WIFI_STATUS_CONNECTED_BUSY == wifiStatus)
1943 		bWifiBusy = TRUE;
1944 	else
1945 		bWifiBusy = FALSE;
1946 
1947 	if( (BT_8703B_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN == wifiStatus) ||
1948 		(BT_8703B_1ANT_WIFI_STATUS_CONNECTED_SCAN == wifiStatus) ||
1949 		(BT_8703B_1ANT_WIFI_STATUS_CONNECTED_SPECIAL_PKT == wifiStatus) )
1950 	{
1951 		if( pCoexDm->curPsTdma != 1 &&
1952 			pCoexDm->curPsTdma != 2 &&
1953 			pCoexDm->curPsTdma != 3 &&
1954 			pCoexDm->curPsTdma != 9 )
1955 		{
1956 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 9);
1957 			pCoexDm->psTdmaDuAdjType = 9;
1958 
1959 			up = 0;
1960 			dn = 0;
1961 			m = 1;
1962 			n= 3;
1963 			result = 0;
1964 			WaitCount = 0;
1965 		}
1966 		return;
1967 	}
1968 
1969 	if(!pCoexDm->bAutoTdmaAdjust)
1970 	{
1971 		pCoexDm->bAutoTdmaAdjust = TRUE;
1972 		RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], first run TdmaDurationAdjust()!!\n"));
1973 
1974 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 2);
1975 		pCoexDm->psTdmaDuAdjType = 2;
1976 		//============
1977 		up = 0;
1978 		dn = 0;
1979 		m = 1;
1980 		n= 3;
1981 		result = 0;
1982 		WaitCount = 0;
1983 	}
1984 	else
1985 	{
1986 		//accquire the BT TRx retry count from BT_Info byte2
1987 		retryCount = pCoexSta->btRetryCnt;
1988 		btInfoExt = pCoexSta->btInfoExt;
1989 
1990 		if ( (pCoexSta->lowPriorityTx) > 1050 ||  (pCoexSta->lowPriorityRx) > 1250 )
1991 			retryCount++;
1992 
1993 		result = 0;
1994 		WaitCount++;
1995 
1996 		if(retryCount == 0)  // no retry in the last 2-second duration
1997 		{
1998 			up++;
1999 			dn--;
2000 
2001 			if (dn <= 0)
2002 				dn = 0;
2003 
2004 			if(up >= n)	// if �s�� n ��2�� retry count��0, �h�ռeWiFi duration
2005 			{
2006 				WaitCount = 0;
2007 				n = 3;
2008 				up = 0;
2009 				dn = 0;
2010 				result = 1;
2011 				RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], Increase wifi duration!!\n"));
2012 			}
2013 		}
2014 		else if (retryCount <= 3)	// <=3 retry in the last 2-second duration
2015 		{
2016 			up--;
2017 			dn++;
2018 
2019 			if (up <= 0)
2020 				up = 0;
2021 
2022 			if (dn == 2)	// if �s�� 2 ��2�� retry count< 3, �h�կ�WiFi duration
2023 			{
2024 				if (WaitCount <= 2)
2025 					m++; // �קK�@���b���level���Ӧ^
2026 				else
2027 					m = 1;
2028 
2029 				if ( m >= 20) //m �̤j�� = 20 ' �̤j120�� recheck�O�_�վ� WiFi duration.
2030 					m = 20;
2031 
2032 				n = 3*m;
2033 				up = 0;
2034 				dn = 0;
2035 				WaitCount = 0;
2036 				result = -1;
2037 				RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], Decrease wifi duration for retryCounter<3!!\n"));
2038 			}
2039 		}
2040 		else  //retry count > 3, �u�n1�� retry count > 3, �h�կ�WiFi duration
2041 		{
2042 			if (WaitCount == 1)
2043 				m++; // �קK�@���b���level���Ӧ^
2044 			else
2045 				m = 1;
2046 
2047 			if ( m >= 20) //m �̤j�� = 20 ' �̤j120�� recheck�O�_�վ� WiFi duration.
2048 				m = 20;
2049 
2050 			n = 3*m;
2051 			up = 0;
2052 			dn = 0;
2053 			WaitCount = 0;
2054 			result = -1;
2055 			RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], Decrease wifi duration for retryCounter>3!!\n"));
2056 		}
2057 
2058 		if(result == -1)
2059 		{
2060 /*			if( (BT_INFO_8703B_1ANT_A2DP_BASIC_RATE(btInfoExt)) &&
2061 				((pCoexDm->curPsTdma == 1) ||(pCoexDm->curPsTdma == 2)) )
2062 			{
2063 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 9);
2064 				pCoexDm->psTdmaDuAdjType = 9;
2065 			}
2066 			else */ if(pCoexDm->curPsTdma == 1)
2067 			{
2068 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 2);
2069 				pCoexDm->psTdmaDuAdjType = 2;
2070 			}
2071 			else if(pCoexDm->curPsTdma == 2)
2072 			{
2073 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 9);
2074 				pCoexDm->psTdmaDuAdjType = 9;
2075 			}
2076 			else if(pCoexDm->curPsTdma == 9)
2077 			{
2078 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 11);
2079 				pCoexDm->psTdmaDuAdjType = 11;
2080 			}
2081 		}
2082 		else if(result == 1)
2083 		{
2084 /*			if( (BT_INFO_8703B_1ANT_A2DP_BASIC_RATE(btInfoExt)) &&
2085 				((pCoexDm->curPsTdma == 1) ||(pCoexDm->curPsTdma == 2)) )
2086 			{
2087 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 9);
2088 				pCoexDm->psTdmaDuAdjType = 9;
2089 			}
2090 			else */ if(pCoexDm->curPsTdma == 11)
2091 			{
2092 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 9);
2093 				pCoexDm->psTdmaDuAdjType = 9;
2094 			}
2095 			else if(pCoexDm->curPsTdma == 9)
2096 			{
2097 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 2);
2098 				pCoexDm->psTdmaDuAdjType = 2;
2099 			}
2100 			else if(pCoexDm->curPsTdma == 2)
2101 			{
2102 				halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 1);
2103 				pCoexDm->psTdmaDuAdjType = 1;
2104 			}
2105 		}
2106 		else	  //no change
2107 		{
2108 			/* Bryant Modify
2109 			if(bWifiBusy != bPreWifiBusy)  //if busy / idle change
2110 			{
2111 				bPreWifiBusy = bWifiBusy;
2112 				halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, TRUE, pCoexDm->curPsTdma);
2113 			}
2114 			*/
2115 
2116 			RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], ********** TDMA(on, %d) **********\n",
2117 				pCoexDm->curPsTdma));
2118 		}
2119 
2120 		if( pCoexDm->curPsTdma != 1 &&
2121 			pCoexDm->curPsTdma != 2 &&
2122 			pCoexDm->curPsTdma != 9 &&
2123 			pCoexDm->curPsTdma != 11 )
2124 		{
2125 			// recover to previous adjust type
2126 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, pCoexDm->psTdmaDuAdjType);
2127 		}
2128 	}
2129 }
2130 
2131 VOID
halbtc8703b1ant_PsTdmaCheckForPowerSaveState(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bNewPsState)2132 halbtc8703b1ant_PsTdmaCheckForPowerSaveState(
2133 	IN	PBTC_COEXIST		pBtCoexist,
2134 	IN	BOOLEAN			bNewPsState
2135 	)
2136 {
2137 	u1Byte	lpsMode=0x0;
2138 
2139 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_LPS_MODE, &lpsMode);
2140 
2141 	if(lpsMode)	// already under LPS state
2142 	{
2143 		if(bNewPsState)
2144 		{
2145 			// keep state under LPS, do nothing.
2146 		}
2147 		else
2148 		{
2149 			// will leave LPS state, turn off psTdma first
2150 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2151 		}
2152 	}
2153 	else						// NO PS state
2154 	{
2155 		if(bNewPsState)
2156 		{
2157 			// will enter LPS state, turn off psTdma first
2158 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2159 		}
2160 		else
2161 		{
2162 			// keep state under NO PS state, do nothing.
2163 		}
2164 	}
2165 }
2166 
2167 VOID
halbtc8703b1ant_PowerSaveState(IN PBTC_COEXIST pBtCoexist,IN u1Byte psType,IN u1Byte lpsVal,IN u1Byte rpwmVal)2168 halbtc8703b1ant_PowerSaveState(
2169 	IN	PBTC_COEXIST		pBtCoexist,
2170 	IN	u1Byte				psType,
2171 	IN	u1Byte				lpsVal,
2172 	IN	u1Byte				rpwmVal
2173 	)
2174 {
2175 	BOOLEAN		bLowPwrDisable=FALSE;
2176 
2177 	switch(psType)
2178 	{
2179 		case BTC_PS_WIFI_NATIVE:
2180 			// recover to original 32k low power setting
2181 			bLowPwrDisable = FALSE;
2182 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_DISABLE_LOW_POWER, &bLowPwrDisable);
2183 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
2184 			pCoexSta->bForceLpsOn = FALSE;
2185 			break;
2186 		case BTC_PS_LPS_ON:
2187 			halbtc8703b1ant_PsTdmaCheckForPowerSaveState(pBtCoexist, TRUE);
2188 			halbtc8703b1ant_LpsRpwm(pBtCoexist, NORMAL_EXEC, lpsVal, rpwmVal);
2189 			// when coex force to enter LPS, do not enter 32k low power.
2190 			bLowPwrDisable = TRUE;
2191 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_DISABLE_LOW_POWER, &bLowPwrDisable);
2192 			// power save must executed before psTdma.
2193 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_ENTER_LPS, NULL);
2194 			pCoexSta->bForceLpsOn = TRUE;
2195 			break;
2196 		case BTC_PS_LPS_OFF:
2197 			halbtc8703b1ant_PsTdmaCheckForPowerSaveState(pBtCoexist, FALSE);
2198 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
2199 			pCoexSta->bForceLpsOn = FALSE;
2200 			break;
2201 		default:
2202 			break;
2203 	}
2204 }
2205 
2206 VOID
halbtc8703b1ant_ActionWifiOnly(IN PBTC_COEXIST pBtCoexist)2207 halbtc8703b1ant_ActionWifiOnly(
2208 	IN	PBTC_COEXIST		pBtCoexist
2209 	)
2210 {
2211 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
2212 	halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);
2213 	halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, FORCE_EXEC, FALSE, FALSE);
2214 }
2215 
2216 VOID
halbtc8703b1ant_MonitorBtEnableDisable(IN PBTC_COEXIST pBtCoexist)2217 halbtc8703b1ant_MonitorBtEnableDisable(
2218 	IN 	PBTC_COEXIST		pBtCoexist
2219 	)
2220 {
2221 	static BOOLEAN	bPreBtDisabled=FALSE;
2222 	static u4Byte		btDisableCnt=0;
2223 	BOOLEAN			bBtActive=TRUE, bBtDisabled=FALSE;
2224 
2225 	// This function check if bt is disabled
2226 
2227 	if(	pCoexSta->highPriorityTx == 0 &&
2228 		pCoexSta->highPriorityRx == 0 &&
2229 		pCoexSta->lowPriorityTx == 0 &&
2230 		pCoexSta->lowPriorityRx == 0)
2231 	{
2232 		bBtActive = FALSE;
2233 	}
2234 	if(	pCoexSta->highPriorityTx == 0xffff &&
2235 		pCoexSta->highPriorityRx == 0xffff &&
2236 		pCoexSta->lowPriorityTx == 0xffff &&
2237 		pCoexSta->lowPriorityRx == 0xffff)
2238 	{
2239 		bBtActive = FALSE;
2240 	}
2241 	if(bBtActive)
2242 	{
2243 		btDisableCnt = 0;
2244 		bBtDisabled = FALSE;
2245 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_DISABLE, &bBtDisabled);
2246 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT is enabled !!\n"));
2247 	}
2248 	else
2249 	{
2250 		btDisableCnt++;
2251 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], bt all counters=0, %d times!!\n",
2252 				btDisableCnt));
2253 		if(btDisableCnt >= 2)
2254 		{
2255 			bBtDisabled = TRUE;
2256 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_DISABLE, &bBtDisabled);
2257 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT is disabled !!\n"));
2258 			halbtc8703b1ant_ActionWifiOnly(pBtCoexist);
2259 		}
2260 	}
2261 	if(bPreBtDisabled != bBtDisabled)
2262 	{
2263 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT is from %s to %s!!\n",
2264 			(bPreBtDisabled ? "disabled":"enabled"),
2265 			(bBtDisabled ? "disabled":"enabled")));
2266 		bPreBtDisabled = bBtDisabled;
2267 		if(!bBtDisabled)
2268 		{
2269 		}
2270 		else
2271 		{
2272 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
2273 			pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
2274 		}
2275 	}
2276 }
2277 
2278 //=============================================
2279 //
2280 //	Software Coex Mechanism start
2281 //
2282 //=============================================
2283 
2284 // SCO only or SCO+PAN(HS)
2285 
2286 /*
2287 VOID
2288 halbtc8703b1ant_ActionSco(
2289 	IN	PBTC_COEXIST		pBtCoexist
2290 	)
2291 {
2292 	halbtc8703b1ant_SwMechanism(pBtCoexist, TRUE);
2293 }
2294 
2295 
2296 VOID
2297 halbtc8703b1ant_ActionHid(
2298 	IN	PBTC_COEXIST		pBtCoexist
2299 	)
2300 {
2301 	halbtc8703b1ant_SwMechanism(pBtCoexist, TRUE);
2302 }
2303 
2304 //A2DP only / PAN(EDR) only/ A2DP+PAN(HS)
2305 VOID
2306 halbtc8703b1ant_ActionA2dp(
2307 	IN	PBTC_COEXIST		pBtCoexist
2308 	)
2309 {
2310 	halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
2311 }
2312 
2313 VOID
2314 halbtc8703b1ant_ActionA2dpPanHs(
2315 	IN	PBTC_COEXIST		pBtCoexist
2316 	)
2317 {
2318 	halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
2319 }
2320 
2321 VOID
2322 halbtc8703b1ant_ActionPanEdr(
2323 	IN	PBTC_COEXIST		pBtCoexist
2324 	)
2325 {
2326 	halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
2327 }
2328 
2329 //PAN(HS) only
2330 VOID
2331 halbtc8703b1ant_ActionPanHs(
2332 	IN	PBTC_COEXIST		pBtCoexist
2333 	)
2334 {
2335 	halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
2336 }
2337 
2338 //PAN(EDR)+A2DP
2339 VOID
2340 halbtc8703b1ant_ActionPanEdrA2dp(
2341 	IN	PBTC_COEXIST		pBtCoexist
2342 	)
2343 {
2344 	halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
2345 }
2346 
2347 VOID
2348 halbtc8703b1ant_ActionPanEdrHid(
2349 	IN	PBTC_COEXIST		pBtCoexist
2350 	)
2351 {
2352 	halbtc8703b1ant_SwMechanism(pBtCoexist, TRUE);
2353 }
2354 
2355 // HID+A2DP+PAN(EDR)
2356 VOID
2357 halbtc8703b1ant_ActionHidA2dpPanEdr(
2358 	IN	PBTC_COEXIST		pBtCoexist
2359 	)
2360 {
2361 	halbtc8703b1ant_SwMechanism(pBtCoexist, TRUE);
2362 }
2363 
2364 VOID
2365 halbtc8703b1ant_ActionHidA2dp(
2366 	IN	PBTC_COEXIST		pBtCoexist
2367 	)
2368 {
2369 	halbtc8703b1ant_SwMechanism(pBtCoexist, TRUE);
2370 }
2371 
2372 */
2373 
2374 //=============================================
2375 //
2376 //	Non-Software Coex Mechanism start
2377 //
2378 //=============================================
2379 VOID
halbtc8703b1ant_ActionBtWhckTest(IN PBTC_COEXIST pBtCoexist)2380 halbtc8703b1ant_ActionBtWhckTest(
2381 	IN	PBTC_COEXIST		pBtCoexist
2382 	)
2383 {
2384 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2385 
2386 	halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2387 	halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2388 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2389 }
2390 
2391 VOID
halbtc8703b1ant_ActionWifiMultiPort(IN PBTC_COEXIST pBtCoexist)2392 halbtc8703b1ant_ActionWifiMultiPort(
2393 	IN	PBTC_COEXIST		pBtCoexist
2394 	)
2395 {
2396 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2397 
2398 	halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2399 	halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2400 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2401 }
2402 
2403 VOID
halbtc8703b1ant_ActionHs(IN PBTC_COEXIST pBtCoexist)2404 halbtc8703b1ant_ActionHs(
2405 	IN	PBTC_COEXIST		pBtCoexist
2406 	)
2407 {
2408 	halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 5);
2409 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2410 }
2411 
2412 VOID
halbtc8703b1ant_ActionBtInquiry(IN PBTC_COEXIST pBtCoexist)2413 halbtc8703b1ant_ActionBtInquiry(
2414 	IN	PBTC_COEXIST		pBtCoexist
2415 	)
2416 {
2417 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2418 	BOOLEAN			bWifiConnected=FALSE, bApEnable=FALSE, bWifiBusy=FALSE, bBtBusy=FALSE;
2419 
2420 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
2421 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2422 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2423 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
2424 
2425 	if ( (!bWifiConnected) && (!pCoexSta->bWiFiIsHighPriTask) )
2426 	{
2427 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2428 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2429 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2430 	 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2431 	}
2432 	else if( (pBtLinkInfo->bScoExist) || 	(pBtLinkInfo->bHidExist) || 	(pBtLinkInfo->bA2dpExist)  )
2433 	{
2434 		// SCO/HID/A2DP busy
2435 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2436 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2437 
2438 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2439 	}
2440 	else if ( (pBtLinkInfo->bPanExist) || (bWifiBusy) )
2441 	{
2442 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2443 
2444 		//for BT inquiry/page fail after S4 resume
2445 		//halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 20);
2446 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2447 
2448 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2449 	}
2450 	else
2451 	{
2452 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2453 
2454 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2455 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2456 	 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2457 
2458 
2459 		//halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2460 		//halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2461 	}
2462 }
2463 
2464 VOID
halbtc8703b1ant_ActionBtScoHidOnlyBusy(IN PBTC_COEXIST pBtCoexist,IN u1Byte wifiStatus)2465 halbtc8703b1ant_ActionBtScoHidOnlyBusy(
2466 	IN	PBTC_COEXIST		pBtCoexist,
2467 	IN	u1Byte				wifiStatus
2468 	)
2469 {
2470 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2471 	BOOLEAN	bWifiConnected=FALSE;
2472 	u1Byte	wifiRssiState=BTC_RSSI_STATE_HIGH;
2473 
2474 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2475 
2476 	// tdma and coex table
2477 
2478 	if(pBtLinkInfo->bScoExist)
2479 	{
2480 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 5);
2481 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2482 	}
2483 	else //HID
2484 	{
2485 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 6);
2486 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2487 	}
2488 }
2489 
2490 VOID
halbtc8703b1ant_ActionWifiConnectedBtAclBusy(IN PBTC_COEXIST pBtCoexist,IN u1Byte wifiStatus)2491 halbtc8703b1ant_ActionWifiConnectedBtAclBusy(
2492 	IN	PBTC_COEXIST		pBtCoexist,
2493 	IN	u1Byte				wifiStatus
2494 	)
2495 {
2496 	u1Byte		btRssiState;
2497 
2498 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2499 	btRssiState = halbtc8703b1ant_BtRssiState(2, 28, 0);
2500 
2501 	if ( (pCoexSta->lowPriorityRx >= 950)  && (!pCoexSta->bUnderIps) )
2502 	{
2503 		pBtLinkInfo->bSlaveRole = TRUE;
2504 	}
2505 	else
2506 	{
2507 		pBtLinkInfo->bSlaveRole = FALSE;
2508 	}
2509 
2510 	if(pBtLinkInfo->bHidOnly)  //HID
2511 	{
2512 		halbtc8703b1ant_ActionBtScoHidOnlyBusy(pBtCoexist, wifiStatus);
2513 		pCoexDm->bAutoTdmaAdjust = FALSE;
2514 		return;
2515 	}
2516 	else if(pBtLinkInfo->bA2dpOnly)  //A2DP
2517 	{
2518 		if(BT_8703B_1ANT_WIFI_STATUS_CONNECTED_IDLE == wifiStatus)
2519 		{
2520 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2521 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2522 			pCoexDm->bAutoTdmaAdjust = FALSE;
2523 		}
2524 		else
2525 		{
2526 			halbtc8703b1ant_TdmaDurationAdjustForAcl(pBtCoexist, wifiStatus);
2527 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2528 			pCoexDm->bAutoTdmaAdjust = TRUE;
2529 		}
2530 	}
2531 	else if ( ((pBtLinkInfo->bA2dpExist) && (pBtLinkInfo->bPanExist)) ||
2532 		       (pBtLinkInfo->bHidExist&&pBtLinkInfo->bA2dpExist&&pBtLinkInfo->bPanExist) ) //A2DP+PAN(OPP,FTP), HID+A2DP+PAN(OPP,FTP)
2533 	{
2534 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 13);
2535 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2536 		pCoexDm->bAutoTdmaAdjust = FALSE;
2537 	}
2538 	else if(pBtLinkInfo->bHidExist&&pBtLinkInfo->bA2dpExist)  //HID+A2DP
2539 	{
2540 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 14);
2541 		pCoexDm->bAutoTdmaAdjust = FALSE;
2542 
2543 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 3);
2544 	}
2545 	else if( (pBtLinkInfo->bPanOnly) || (pBtLinkInfo->bHidExist&&pBtLinkInfo->bPanExist) ) //PAN(OPP,FTP), HID+PAN(OPP,FTP)
2546 	{
2547 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 3);
2548 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2549 		pCoexDm->bAutoTdmaAdjust = FALSE;
2550 	}
2551 	else
2552 	{
2553 		//BT no-profile busy (0x9)
2554 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 33);
2555 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2556 		pCoexDm->bAutoTdmaAdjust = FALSE;
2557 	}
2558 }
2559 
2560 VOID
halbtc8703b1ant_ActionWifiNotConnected(IN PBTC_COEXIST pBtCoexist)2561 halbtc8703b1ant_ActionWifiNotConnected(
2562 	IN	PBTC_COEXIST		pBtCoexist
2563 	)
2564 {
2565 	// power save state
2566 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2567 
2568 	// tdma and coex table
2569 	halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);
2570 	halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2571 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2572 }
2573 
2574 VOID
halbtc8703b1ant_ActionWifiNotConnectedScan(IN PBTC_COEXIST pBtCoexist)2575 halbtc8703b1ant_ActionWifiNotConnectedScan(
2576 	IN	PBTC_COEXIST		pBtCoexist
2577 	)
2578 {
2579 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2580 
2581 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2582 
2583 	// tdma and coex table
2584 	if(BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus)
2585 	{
2586 		if (pBtLinkInfo->bA2dpExist)
2587 		{
2588 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2589 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2590 		}
2591 		else if (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist)
2592 		{
2593 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 22);
2594 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2595 		}
2596 	 	else
2597 	 	{
2598 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 20);
2599 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2600 		}
2601 	}
2602 	else if( (BT_8703B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2603 			(BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus) )
2604 	{
2605 		halbtc8703b1ant_ActionBtScoHidOnlyBusy(pBtCoexist,
2606 			BT_8703B_1ANT_WIFI_STATUS_CONNECTED_SCAN);
2607 	}
2608 	else
2609 	{
2610 		//Bryant Add
2611 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2612 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2613 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2614 	}
2615 }
2616 
2617 VOID
halbtc8703b1ant_ActionWifiNotConnectedAssoAuth(IN PBTC_COEXIST pBtCoexist)2618 halbtc8703b1ant_ActionWifiNotConnectedAssoAuth(
2619 	IN	PBTC_COEXIST		pBtCoexist
2620 	)
2621 {
2622 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2623 
2624 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2625 
2626 	// tdma and coex table
2627 	if( (pBtLinkInfo->bScoExist)  || (pBtLinkInfo->bHidExist) ||  (pBtLinkInfo->bA2dpExist) )
2628 	{
2629 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2630 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 4);
2631 	}
2632 	else if (pBtLinkInfo->bPanExist)
2633 	{
2634 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 20);
2635 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 4);
2636 	}
2637 	else
2638 	{
2639 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2640 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2641 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 2);
2642 	}
2643 }
2644 
2645 VOID
halbtc8703b1ant_ActionWifiConnectedScan(IN PBTC_COEXIST pBtCoexist)2646 halbtc8703b1ant_ActionWifiConnectedScan(
2647 	IN	PBTC_COEXIST		pBtCoexist
2648 	)
2649 {
2650 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2651 
2652 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2653 
2654 	// tdma and coex table
2655 	if(BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus)
2656 	{
2657 		if (pBtLinkInfo->bA2dpExist)
2658 		{
2659 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2660 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2661 		}
2662 		else if (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist)
2663 		{
2664 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 22);
2665 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2666 		}
2667 	 	else
2668 	 	{
2669 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 20);
2670 			halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2671 	}
2672 	}
2673 	else if( (BT_8703B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2674 			(BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus) )
2675 	{
2676 		halbtc8703b1ant_ActionBtScoHidOnlyBusy(pBtCoexist,
2677 			BT_8703B_1ANT_WIFI_STATUS_CONNECTED_SCAN);
2678 	}
2679 	else
2680 	{
2681 		//Bryant Add
2682 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2683 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2684 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2685 	}
2686 }
2687 
2688 VOID
halbtc8703b1ant_ActionWifiConnectedSpecialPacket(IN PBTC_COEXIST pBtCoexist)2689 halbtc8703b1ant_ActionWifiConnectedSpecialPacket(
2690 	IN	PBTC_COEXIST		pBtCoexist
2691 	)
2692 {
2693 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2694 	BOOLEAN bWifiBusy = FALSE;
2695 
2696 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2697 
2698 	//no special packet process for both WiFi and BT very busy
2699 	if ((bWifiBusy) && ((pBtLinkInfo->bPanExist) || (pCoexSta->nNumOfProfile >= 2)))
2700 	 return;
2701 
2702 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2703 
2704 	// tdma and coex table
2705 	if((pBtLinkInfo->bScoExist) || (pBtLinkInfo->bHidExist))
2706 	{
2707 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2708 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2709 	}
2710 	else if  (pBtLinkInfo->bA2dpExist)
2711 	{
2712 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 32);
2713 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2714 	}
2715 	else if(pBtLinkInfo->bPanExist)
2716 	{
2717 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, TRUE, 20);
2718 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2719 	}
2720 	else
2721 	{
2722 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2723 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2724 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2725 	}
2726 }
2727 
2728 VOID
halbtc8703b1ant_ActionWifiConnected(IN PBTC_COEXIST pBtCoexist)2729 halbtc8703b1ant_ActionWifiConnected(
2730 	IN	PBTC_COEXIST		pBtCoexist
2731 	)
2732 {
2733 	BOOLEAN 	bWifiBusy=FALSE;
2734 	BOOLEAN		bScan=FALSE, bLink=FALSE, bRoam=FALSE;
2735 	BOOLEAN		bUnder4way=FALSE, bApEnable=FALSE;
2736 	u4Byte		wifiBw;
2737 
2738 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CoexForWifiConnect()===>\n"));
2739 
2740 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &bUnder4way);
2741 	if(bUnder4way)
2742 	{
2743 		halbtc8703b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
2744 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n"));
2745 		return;
2746 	}
2747 
2748 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2749 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2750 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2751 	if(bScan || bLink || bRoam)
2752 	{
2753 		if(bScan)
2754 			halbtc8703b1ant_ActionWifiConnectedScan(pBtCoexist);
2755 		else
2756 			halbtc8703b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
2757 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n"));
2758 		return;
2759 	}
2760 
2761 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
2762 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2763 
2764 	// power save state
2765 	if(!bApEnable && BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus && !pBtCoexist->btLinkInfo.bHidOnly)
2766 	{
2767 		if(pBtCoexist->btLinkInfo.bA2dpOnly)	//A2DP
2768 			{
2769 			if(!bWifiBusy)
2770 				halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2771 			else //busy
2772 			{
2773 				if  (pCoexSta->nScanAPNum >= BT_8703B_1ANT_WIFI_NOISY_THRESH)  //no force LPS, no PS-TDMA, use pure TDMA
2774 				{
2775 			halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2776 				}
2777 				else
2778 				{
2779 					halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_LPS_ON, 0x50, 0x4);
2780 				}
2781 			}
2782 		}
2783 		else if ((pCoexSta->bPanExist == FALSE) && (pCoexSta->bA2dpExist == FALSE) && (pCoexSta->bHidExist == FALSE))
2784  			halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2785 		else
2786 			halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_LPS_ON, 0x50, 0x4);
2787 	}
2788 	else
2789 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2790 
2791 	// tdma and coex table
2792 	if(!bWifiBusy)
2793 	{
2794 		if(BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus)
2795 		{
2796 			halbtc8703b1ant_ActionWifiConnectedBtAclBusy(pBtCoexist,
2797 				BT_8703B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2798 		}
2799 		else if( (BT_8703B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2800 			(BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus) )
2801 		{
2802 			halbtc8703b1ant_ActionBtScoHidOnlyBusy(pBtCoexist,
2803 				BT_8703B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2804 		}
2805 		else
2806 		{
2807 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2808 			halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2809 			if ( (pCoexSta->highPriorityTx) + (pCoexSta->highPriorityRx) <= 60 )
2810 				halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2811 			else
2812 				halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2813 		}
2814 	}
2815 	else
2816 	{
2817 		if(BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus)
2818 		{
2819 			halbtc8703b1ant_ActionWifiConnectedBtAclBusy(pBtCoexist,
2820 				BT_8703B_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2821 		}
2822 		else if( (BT_8703B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2823 			(BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus) )
2824 		{
2825 			halbtc8703b1ant_ActionBtScoHidOnlyBusy(pBtCoexist,
2826 				BT_8703B_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2827 		}
2828 		else
2829 		{
2830 			halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
2831 			halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, FALSE, FALSE);
2832 			if ( (pCoexSta->highPriorityTx) + (pCoexSta->highPriorityRx) <= 60 )
2833 				halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2834 			else
2835 				halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2836 		}
2837 	}
2838 }
2839 
2840 VOID
halbtc8703b1ant_RunSwCoexistMechanism(IN PBTC_COEXIST pBtCoexist)2841 halbtc8703b1ant_RunSwCoexistMechanism(
2842 	IN	PBTC_COEXIST		pBtCoexist
2843 	)
2844 {
2845 	u1Byte				algorithm=0;
2846 
2847 	algorithm = halbtc8703b1ant_ActionAlgorithm(pBtCoexist);
2848 	pCoexDm->curAlgorithm = algorithm;
2849 
2850 	if(halbtc8703b1ant_IsCommonAction(pBtCoexist))
2851 	{
2852 
2853 	}
2854 	else
2855 	{
2856 		switch(pCoexDm->curAlgorithm)
2857 		{
2858 			case BT_8703B_1ANT_COEX_ALGO_SCO:
2859 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = SCO.\n"));
2860 				//halbtc8703b1ant_ActionSco(pBtCoexist);
2861 				break;
2862 			case BT_8703B_1ANT_COEX_ALGO_HID:
2863 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = HID.\n"));
2864 				//halbtc8703b1ant_ActionHid(pBtCoexist);
2865 				break;
2866 			case BT_8703B_1ANT_COEX_ALGO_A2DP:
2867 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = A2DP.\n"));
2868 				//halbtc8703b1ant_ActionA2dp(pBtCoexist);
2869 				break;
2870 			case BT_8703B_1ANT_COEX_ALGO_A2DP_PANHS:
2871 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = A2DP+PAN(HS).\n"));
2872 				//halbtc8703b1ant_ActionA2dpPanHs(pBtCoexist);
2873 				break;
2874 			case BT_8703B_1ANT_COEX_ALGO_PANEDR:
2875 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = PAN(EDR).\n"));
2876 				//halbtc8703b1ant_ActionPanEdr(pBtCoexist);
2877 				break;
2878 			case BT_8703B_1ANT_COEX_ALGO_PANHS:
2879 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = HS mode.\n"));
2880 				//halbtc8703b1ant_ActionPanHs(pBtCoexist);
2881 				break;
2882 			case BT_8703B_1ANT_COEX_ALGO_PANEDR_A2DP:
2883 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = PAN+A2DP.\n"));
2884 				//halbtc8703b1ant_ActionPanEdrA2dp(pBtCoexist);
2885 				break;
2886 			case BT_8703B_1ANT_COEX_ALGO_PANEDR_HID:
2887 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = PAN(EDR)+HID.\n"));
2888 				//halbtc8703b1ant_ActionPanEdrHid(pBtCoexist);
2889 				break;
2890 			case BT_8703B_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2891 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = HID+A2DP+PAN.\n"));
2892 				//halbtc8703b1ant_ActionHidA2dpPanEdr(pBtCoexist);
2893 				break;
2894 			case BT_8703B_1ANT_COEX_ALGO_HID_A2DP:
2895 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = HID+A2DP.\n"));
2896 				//halbtc8703b1ant_ActionHidA2dp(pBtCoexist);
2897 				break;
2898 			default:
2899 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Action algorithm = coexist All Off!!\n"));
2900 				//halbtc8703b1ant_CoexAllOff(pBtCoexist);
2901 				break;
2902 		}
2903 		pCoexDm->preAlgorithm = pCoexDm->curAlgorithm;
2904 	}
2905 }
2906 
2907 VOID
halbtc8703b1ant_RunCoexistMechanism(IN PBTC_COEXIST pBtCoexist)2908 halbtc8703b1ant_RunCoexistMechanism(
2909 	IN	PBTC_COEXIST		pBtCoexist
2910 	)
2911 {
2912 	PBTC_BT_LINK_INFO pBtLinkInfo=&pBtCoexist->btLinkInfo;
2913 	BOOLEAN	bWifiConnected=FALSE, bBtHsOn=FALSE;
2914 	BOOLEAN	bIncreaseScanDevNum=FALSE;
2915 	BOOLEAN	bBtCtrlAggBufSize=FALSE;
2916 	BOOLEAN	bMiracastPlusBt=FALSE;
2917 	u1Byte	aggBufSize=5;
2918 	u1Byte	wifiRssiState=BTC_RSSI_STATE_HIGH;
2919 	u4Byte	wifiLinkStatus=0;
2920 	u4Byte	numOfWifiLink=0, wifiBw;
2921 	u1Byte	iotPeer=BTC_IOT_PEER_UNKNOWN;
2922 
2923 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], RunCoexistMechanism()===>\n"));
2924 
2925 	if(pBtCoexist->bManualControl)
2926 	{
2927 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n"));
2928 		return;
2929 	}
2930 
2931 	if(pBtCoexist->bStopCoexDm)
2932 	{
2933 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n"));
2934 		return;
2935 	}
2936 
2937 	if(pCoexSta->bUnderIps)
2938 	{
2939 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], wifi is under IPS !!!\n"));
2940 		return;
2941 	}
2942 
2943 	if(pCoexSta->bBtWhckTest)
2944 	{
2945 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT is under WHCK TEST!!!\n"));
2946 		halbtc8703b1ant_ActionBtWhckTest(pBtCoexist);
2947 		return;
2948 	}
2949 
2950 	if( (BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) ||
2951 		(BT_8703B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2952 		(BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus) )
2953 	{
2954 		bIncreaseScanDevNum = TRUE;
2955 	}
2956 
2957 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_INC_SCAN_DEV_NUM, &bIncreaseScanDevNum);
2958 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2959 
2960 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
2961 	numOfWifiLink = wifiLinkStatus>>16;
2962 
2963 	if((numOfWifiLink>=2) || (wifiLinkStatus&WIFI_P2P_GO_CONNECTED))
2964 	{
2965 		RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex],  Multi-Port numOfWifiLink = %d, wifiLinkStatus = 0x%x\n", numOfWifiLink,wifiLinkStatus) );
2966 
2967 		if(pBtLinkInfo->bBtLinkExist)
2968 		{
2969 			halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 1, 1, 0, 1);
2970 			bMiracastPlusBt = TRUE;
2971 		}
2972 		else
2973 		{
2974 		halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
2975 			bMiracastPlusBt = FALSE;
2976 		}
2977 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_MIRACAST_PLUS_BT, &bMiracastPlusBt);
2978 		halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, bBtCtrlAggBufSize, aggBufSize);
2979 
2980 		if ( (pBtLinkInfo->bA2dpExist) && (pCoexSta->bC2hBtInquiryPage) )
2981 		{
2982 			RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex],  BT Is Inquirying \n") );
2983 			halbtc8703b1ant_ActionBtInquiry(pBtCoexist);
2984 		}
2985 		else
2986 		halbtc8703b1ant_ActionWifiMultiPort(pBtCoexist);
2987 
2988 		return;
2989 	}
2990 	else
2991 	{
2992 		bMiracastPlusBt = FALSE;
2993 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_MIRACAST_PLUS_BT, &bMiracastPlusBt);
2994 	}
2995 
2996 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
2997 
2998 	if ( (pBtLinkInfo->bBtLinkExist) && (bWifiConnected) )
2999 	{
3000 		halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 1, 1, 0, 1);
3001 
3002 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_IOT_PEER, &iotPeer);
3003 
3004 		if(BTC_IOT_PEER_CISCO != iotPeer)
3005 		{
3006 			if(pBtLinkInfo->bScoExist)//if (pBtLinkInfo->bBtHiPriLinkExist)
3007 				halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, TRUE, FALSE, 0x5);
3008 			else
3009 				halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, FALSE, 0x5);
3010 		}
3011 		else
3012 		{
3013 			if(pBtLinkInfo->bScoExist)
3014 				halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, TRUE, FALSE, 0x5);
3015 			else
3016 			{
3017 				if (BTC_WIFI_BW_HT40==wifiBw)
3018 					halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, TRUE, 0x10);
3019 				else
3020 					halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, TRUE, 0x8);
3021 			}
3022 		}
3023 
3024 		halbtc8703b1ant_SwMechanism(pBtCoexist, TRUE);
3025 		halbtc8703b1ant_RunSwCoexistMechanism(pBtCoexist);  //just print debug message
3026 	}
3027 	else
3028 	{
3029 		halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3030 
3031 		halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, FALSE, 0x5);
3032 
3033 		halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
3034 		halbtc8703b1ant_RunSwCoexistMechanism(pBtCoexist); ////just print debug message
3035 	}
3036 
3037 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3038 	if(pCoexSta->bC2hBtInquiryPage)
3039 	{
3040 		RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex],  BT Is Inquirying \n") );
3041 		halbtc8703b1ant_ActionBtInquiry(pBtCoexist);
3042 		return;
3043 	}
3044 	else if(bBtHsOn)
3045 	{
3046 		halbtc8703b1ant_ActionHs(pBtCoexist);
3047 		return;
3048 	}
3049 
3050 
3051 	if(!bWifiConnected)
3052 	{
3053 		BOOLEAN	bScan=FALSE, bLink=FALSE, bRoam=FALSE;
3054 
3055 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], wifi is non connected-idle !!!\n"));
3056 
3057 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
3058 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
3059 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
3060 
3061 		if(bScan || bLink || bRoam)
3062 		{
3063 			 if (bScan)
3064 		   		halbtc8703b1ant_ActionWifiNotConnectedScan(pBtCoexist);
3065 			 else
3066 		    		halbtc8703b1ant_ActionWifiNotConnectedAssoAuth(pBtCoexist);
3067 		}
3068 		else
3069 			halbtc8703b1ant_ActionWifiNotConnected(pBtCoexist);
3070 	}
3071 	else	// wifi LPS/Busy
3072 	{
3073 		halbtc8703b1ant_ActionWifiConnected(pBtCoexist);
3074 	}
3075 }
3076 
3077 u4Byte
halbtc8703b1ant_PSD_Log2Base(IN PBTC_COEXIST pBtCoexist,IN u4Byte val)3078 halbtc8703b1ant_PSD_Log2Base(
3079 	IN	PBTC_COEXIST		pBtCoexist,
3080 	IN	u4Byte				val
3081 
3082 	)
3083 {
3084 	u1Byte 	i,j;
3085 	u4Byte	tmp, tmp2, val_integerdB=0, tindex, shiftcount=0;
3086 	u4Byte 	result,val_fractiondB=0,Table_fraction[21]= {0,432, 332, 274, 232, 200,
3087 								   174, 151,132,115,100,86,74,62,51,42,
3088 								   32,23,15,7,0};
3089 
3090 	if (val == 0)
3091 	 return 0;
3092 
3093 	tmp = val;
3094 
3095 	while(1)
3096 	{
3097 		if (tmp == 1)
3098 			break;
3099 		else
3100 		{
3101 			tmp = (tmp >> 1);
3102 			shiftcount++;
3103 		}
3104 	}
3105 
3106 
3107 	val_integerdB = shiftcount+1;
3108 
3109 	tmp2=1;
3110 	for (j=1; j<= val_integerdB;j++)
3111 	  tmp2 = tmp2*2;
3112 
3113 	tmp = (val*100) /tmp2;
3114 	tindex = tmp/5;
3115 
3116 	if (tindex > 20)
3117 	 tindex = 20;
3118 
3119 	val_fractiondB = Table_fraction[tindex];
3120 
3121 	result = val_integerdB*100 - val_fractiondB;
3122 
3123 	return (result);
3124 
3125 
3126 }
3127 
3128 VOID
halbtc8703b1ant_InitCoexDm(IN PBTC_COEXIST pBtCoexist)3129 halbtc8703b1ant_InitCoexDm(
3130 	IN	PBTC_COEXIST		pBtCoexist
3131 	)
3132 {
3133 	// force to reset coex mechanism
3134 
3135 	// sw all off
3136 	halbtc8703b1ant_SwMechanism(pBtCoexist, FALSE);
3137 
3138 	//halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);
3139 	//halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
3140 
3141 	pCoexSta->popEventCnt = 0;
3142 }
3143 
3144 VOID
halbtc8703b1ant_InitHwConfig(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bBackUp,IN BOOLEAN bWifiOnly)3145 halbtc8703b1ant_InitHwConfig(
3146 	IN	PBTC_COEXIST		pBtCoexist,
3147 	IN	BOOLEAN				bBackUp,
3148 	IN	BOOLEAN				bWifiOnly
3149 	)
3150 {
3151 	PBTC_BOARD_INFO		pBoardInfo=&pBtCoexist->boardInfo;
3152 	u4Byte				u4Tmp=0;//, fwVer;
3153 	u2Byte				u2Tmp=0;
3154 	u1Byte				u1Tmp=0, u1Tmpa=0, u1Tmpb=0;
3155 	u1Byte				H2C_Parameter[2] ={0};
3156 
3157 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], 1Ant Init HW Config!!\n"));
3158 
3159 	pPsdScan->nAntDet_IsAntDetAvailable = FALSE;
3160 
3161 #if 0//move to BTC_MEDIA_CONNECT
3162 	if(bBackUp)
3163 	{
3164 		pCoexDm->backupArfrCnt1 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
3165 		pCoexDm->backupArfrCnt2 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
3166 		pCoexDm->backupRetryLimit = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
3167 		pCoexDm->backupAmpduMaxTime = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
3168 	}
3169 #endif
3170 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x550, 0x8, 0x1);  //enable TBTT nterrupt
3171 
3172 	// 0x790[5:0]=0x5
3173 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x790, 0x5);
3174 
3175 	// Enable counter statistics
3176 	//pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0xc); //0x76e[3] =1, WLAN_Act control by PTA
3177 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x778, 0x1);
3178 	pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x40, 0x20, 0x1);
3179 
3180 
3181 	//pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x67, 0x20, 0x1); //BT select s0/s1 is controlled by WiFi
3182 
3183 	halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);
3184 
3185 	//Antenna config
3186 	if(bWifiOnly)
3187 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_WIFI, FORCE_EXEC, TRUE, FALSE);
3188 	else
3189 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, TRUE, FALSE);
3190 
3191 #if 0
3192 	if(bWifiOnly)
3193 	{
3194 		halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_WIFI, BTC_WIFI_STAT_INIT);
3195 		halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);
3196 	}
3197 	else
3198 		halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_BT, BTC_WIFI_STAT_INIT);
3199 #endif
3200 
3201 
3202 
3203 	// PTA parameter
3204 	halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
3205 
3206 	u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
3207 	u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
3208 	u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
3209 
3210 	 RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
3211 			u4Tmp,  u1Tmpa, u1Tmpb));
3212 }
3213 
3214 
3215 
3216 
3217 
3218 VOID
halbtc8703b1ant_MechanismSwitch(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bSwitchTo2Antenna)3219 halbtc8703b1ant_MechanismSwitch(
3220 	IN	PBTC_COEXIST		pBtCoexist,
3221 	IN	BOOLEAN				bSwitchTo2Antenna
3222 	)
3223 {
3224 
3225 	if (bSwitchTo2Antenna)  // 1-Ant -> 2-Ant
3226 	{
3227 		//un-lock TRx Mask setup for 8703b f-cut
3228 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdd, 0x80, 0x1);
3229 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdf, 0x1, 0x1);
3230 		//WiFi TRx Mask on
3231 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3232 
3233 		//BT TRx Mask un-lock 0x2c[0], 0x30[0] = 1
3234 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x2c, 0x7c45);
3235 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x30, 0x7c45);
3236 
3237 		//BT TRx Mask on
3238 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x1);
3239 
3240 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, FALSE, FALSE);
3241 	}
3242 	else
3243 	{
3244 		//WiFi TRx Mask on
3245 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x780);
3246 
3247 		//lock TRx Mask setup for 8703b f-cut
3248 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdd, 0x80, 0x0);
3249 		pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdf, 0x1, 0x0);
3250 
3251 		//BT TRx Mask on
3252 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x15);
3253 
3254 		//BT TRx Mask ock 0x2c[0], 0x30[0]  = 0
3255 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x2c, 0x7c44);
3256 		pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x30, 0x7c44);
3257 
3258 
3259 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, FORCE_EXEC, FALSE, FALSE);
3260 	}
3261 
3262 }
3263 
3264 
3265 
3266 
3267 VOID
halbtc8703b1ant_PSD_ShowAntennaDetectResult(IN PBTC_COEXIST pBtCoexist)3268 halbtc8703b1ant_PSD_ShowAntennaDetectResult(
3269 	IN	PBTC_COEXIST		pBtCoexist
3270 	)
3271 {
3272 	pu1Byte		cliBuf=pBtCoexist->cliBuf;
3273 	PBTC_BOARD_INFO 	pBoardInfo=&pBtCoexist->boardInfo;
3274 
3275 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n============[Antenna Detection info]  ============\n");
3276 	CL_PRINTF(cliBuf);
3277 
3278 	if (pPsdScan->nAntDet_Result == 1)
3279 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s (>%d)", "Ant Det Result", "2-Antenna (Bad-Isolation)",
3280 				BT_8703B_1ANT_ANTDET_PSDTHRES_2ANT_BADISOLATION);
3281 	else if (pPsdScan->nAntDet_Result == 2)
3282 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s (%d~%d)", "Ant Det Result", "2-Antenna (Good-Isolation)",
3283 				BT_8703B_1ANT_ANTDET_PSDTHRES_2ANT_GOODISOLATION+pPsdScan->nAntDet_ThresOffset, BT_8703B_1ANT_ANTDET_PSDTHRES_2ANT_BADISOLATION);
3284 	else
3285 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s (%d~%d)", "Ant Det Result", "1-Antenna",
3286 				BT_8703B_1ANT_ANTDET_PSDTHRES_1ANT, BT_8703B_1ANT_ANTDET_PSDTHRES_2ANT_GOODISOLATION+pPsdScan->nAntDet_ThresOffset);
3287 
3288 	CL_PRINTF(cliBuf);
3289 
3290 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s ", "Antenna Detection Finish",
3291 							(pBoardInfo->btdmAntDetFinish? "Yes":"No"));
3292 	CL_PRINTF(cliBuf);
3293 
3294 	switch(pPsdScan->nAntDet_Result)
3295 	{
3296 		case 0:
3297 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is not available)");
3298 			break;
3299 		case 1:  // 2-Ant bad-isolation
3300 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is available)");
3301 			break;
3302 		case 2:  // 2-Ant good-isolation
3303 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is available)");
3304 			break;
3305 		case 3:  // 1-Ant
3306 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is available)");
3307 			break;
3308 		case 4:
3309 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(Uncertainty result)");
3310 			break;
3311 		case 5:
3312 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(Pre-Scan fai)");
3313 			break;
3314 		case 6:
3315 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(WiFi is Scanning)");
3316 			break;
3317 		case 7:
3318 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is not idle)");
3319 			break;
3320 		case 8:
3321 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(Abort by WiFi Scanning)");
3322 			break;
3323 		case 9:
3324 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(Antenna Init is not ready)");
3325 			break;
3326 		case 10:
3327 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is Inquiry or page)");
3328 			break;
3329 		case 11:
3330 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "(BT is Disabled)");
3331 			break;
3332 }
3333 	CL_PRINTF(cliBuf);
3334 
3335 
3336 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "Ant Detect Total Count",pPsdScan->bAntDet_TryCount);
3337 	CL_PRINTF(cliBuf);
3338 
3339 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "Ant Detect Fail Count",pPsdScan->bAntDet_FailCount);
3340 	CL_PRINTF(cliBuf);
3341 
3342 	if ( (!pBoardInfo->btdmAntDetFinish) && (pPsdScan->nAntDet_Result != 5) )
3343 	 return;
3344 
3345 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "BT Response",(pPsdScan->nAntDet_Result? "ok":"fail"));
3346 	CL_PRINTF(cliBuf);
3347 
3348 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ms", "BT Tx Time", pPsdScan->nAntDet_BTTxTime);
3349 	CL_PRINTF(cliBuf);
3350 
3351 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "BT Tx Ch", pPsdScan->nAntDet_BTLEChannel);
3352 	CL_PRINTF(cliBuf);
3353 
3354 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d", "WiFi PSD Cent-Ch/Offset/Span",
3355 				pPsdScan->nRealCentFreq, pPsdScan->nRealOffset, pPsdScan->nRealSpan);
3356 	CL_PRINTF(cliBuf);
3357 
3358 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d dB", "PSD Pre-Scan Peak Value", pPsdScan->nAntDet_PrePSDScanPeakVal/100);
3359 	CL_PRINTF(cliBuf);
3360 
3361 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s (<= %d)", "PSD Pre-Scan result",
3362 				(pPsdScan->nAntDet_Result != 5? "ok":"fail"), BT_8703B_1ANT_ANTDET_PSDTHRES_BACKGROUND+pPsdScan->nAntDet_ThresOffset);
3363 	CL_PRINTF(cliBuf);
3364 
3365 	if (pPsdScan->nAntDet_Result == 5)
3366 		return;
3367 
3368 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s dB", "PSD Scan Peak Value", pPsdScan->nAntDet_PeakVal);
3369 	CL_PRINTF(cliBuf);
3370 
3371 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s MHz", "PSD Scan Peak Freq", pPsdScan->nAntDet_PeakFreq);
3372 	CL_PRINTF(cliBuf);
3373 
3374 
3375 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "TFBGA Package",
3376 			(pBoardInfo->bTfbgaPackage)?  "Yes":"No");
3377 	CL_PRINTF(cliBuf);
3378 
3379 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "PSD Threshold Offset", pPsdScan->nAntDet_ThresOffset);
3380 	CL_PRINTF(cliBuf);
3381 
3382 }
3383 
3384 VOID
halbtc8703b1ant_PSD_ShowData(IN PBTC_COEXIST pBtCoexist)3385 halbtc8703b1ant_PSD_ShowData(
3386 	IN	PBTC_COEXIST		pBtCoexist
3387 	)
3388 {
3389 	pu1Byte		cliBuf=pBtCoexist->cliBuf;
3390 	u4Byte		nDeltaFreqPerPoint;
3391 	u4Byte		freq,freq1,freq2,n=0,i=0, j=0, m=0, PsdRep1, PsdRep2;
3392 
3393 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n\n============[PSD info]  (%d)============\n",
3394 		 	pPsdScan->nPSDGenCount);
3395 	CL_PRINTF(cliBuf);
3396 
3397 	if (pPsdScan->nPSDGenCount == 0)
3398 	{
3399 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n No Data !!\n");
3400 		CL_PRINTF(cliBuf);
3401 		return;
3402 	}
3403 
3404 	if (pPsdScan->nPSDPoint == 0)
3405 		nDeltaFreqPerPoint = 0;
3406 	else
3407 		nDeltaFreqPerPoint = pPsdScan->nPSDBandWidth/pPsdScan->nPSDPoint;
3408 
3409 	//if (pPsdScan->bIsPSDShowMaxOnly)
3410 	if (0)
3411 	{
3412 		PsdRep1 = pPsdScan->nPSDMaxValue/100;
3413 		PsdRep2 = pPsdScan->nPSDMaxValue - PsdRep1 * 100;
3414 
3415 		freq = ((pPsdScan->nRealCentFreq-20) * 1000000 + pPsdScan->nPSDMaxValuePoint * nDeltaFreqPerPoint);
3416 		freq1 = freq/1000000;
3417 		freq2 = freq/1000 - freq1 * 1000;
3418 
3419 		if (freq2 < 100)
3420 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Freq = %d.0%d MHz",
3421 			  freq1, freq2);
3422 		else
3423 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Freq = %d.%d MHz",
3424 			  freq1, freq2);
3425 
3426 		if (PsdRep2 < 10)
3427 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, ", Value = %d.0%d dB, (%d) \n",
3428 			  PsdRep1, PsdRep2, pPsdScan->nPSDMaxValue);
3429 		else
3430 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, ", Value = %d.%d dB, (%d)\n",
3431 			  PsdRep1, PsdRep2, pPsdScan->nPSDMaxValue);
3432 
3433 		CL_PRINTF(cliBuf);
3434 	}
3435 	else
3436 	{
3437 		m = pPsdScan->nPSDStartPoint;
3438 		n = pPsdScan->nPSDStartPoint;
3439 		i = 1;
3440 		j = 1;
3441 
3442 	 while(1)
3443 	 {
3444 		do
3445 		{
3446 			freq = ((pPsdScan->nRealCentFreq-20) * 1000000 + m * nDeltaFreqPerPoint);
3447 			freq1 = freq/1000000;
3448 			freq2 = freq/1000 - freq1 * 1000;
3449 
3450 			if (i ==1)
3451 			{
3452 				if (freq2 == 0)
3453 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Freq%6d.000", freq1);
3454 				else if (freq2 < 100)
3455 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Freq%6d.0%2d", freq1,freq2);
3456 				else
3457 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Freq%6d.%3d", freq1,freq2);
3458 			}
3459 			else if  ( (i%8 == 0) || (m == pPsdScan->nPSDStopPoint) )
3460 			{
3461 				if (freq2 == 0)
3462 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%6d.000\n", freq1);
3463 				else if (freq2 < 100)
3464 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%6d.0%2d\n", freq1,freq2);
3465 				else
3466 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%6d.%3d\n", freq1,freq2);
3467 			}
3468 			else
3469 			{
3470 				if (freq2 == 0)
3471 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%6d.000", freq1);
3472 				else if (freq2 < 100)
3473 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%6d.0%2d", freq1,freq2);
3474 				else
3475 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%6d.%3d", freq1,freq2);
3476 			}
3477 
3478 			i++;
3479 			m++;
3480 			CL_PRINTF(cliBuf);
3481 
3482 		}while(  (i <= 8) && (m <= pPsdScan->nPSDStopPoint));
3483 
3484 
3485 		do
3486 		{
3487 			PsdRep1 = pPsdScan->nPSDReport_MaxHold[n]/100;
3488 			PsdRep2 = pPsdScan->nPSDReport_MaxHold[n] - PsdRep1 * 100;
3489 
3490 			if (j ==1)
3491 			{
3492 				if (PsdRep2 <10)
3493 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Val %7d.0%d", PsdRep1,PsdRep2);
3494 				else
3495 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n Val %7d.%d", PsdRep1,PsdRep2);
3496 			}
3497 			else if ( (j%8 == 0)  || (n == pPsdScan->nPSDStopPoint) )
3498 			{
3499 				if (PsdRep2 <10)
3500 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%7d.0%d\n", PsdRep1,PsdRep2);
3501 				else
3502 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%7d.%d\n", PsdRep1,PsdRep2);
3503 			}
3504 			else
3505 			{
3506 				if (PsdRep2 <10)
3507 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%7d.0%d", PsdRep1,PsdRep2);
3508 				else
3509 					CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "%7d.%d", PsdRep1,PsdRep2);
3510 			}
3511 
3512 			j++;
3513 			n++;
3514 			CL_PRINTF(cliBuf);
3515 
3516 		} while(  (j <= 8) && (n <= pPsdScan->nPSDStopPoint));
3517 
3518 		if  ( (m > pPsdScan->nPSDStopPoint) || (n > pPsdScan->nPSDStopPoint) )
3519 		 break;
3520 		else
3521 		{
3522 			i = 1;
3523 			j = 1;
3524 		}
3525 
3526 	 }
3527 	}
3528 
3529 
3530 }
3531 
3532 VOID
halbtc8703b1ant_PSD_MaxHoldData(IN PBTC_COEXIST pBtCoexist,IN u4Byte GenCount)3533 halbtc8703b1ant_PSD_MaxHoldData(
3534 	IN	PBTC_COEXIST		pBtCoexist,
3535 	IN	u4Byte				GenCount
3536 	)
3537 {
3538 		 u4Byte	i=0, i_max=0, val_max=0, j;
3539 
3540 		if (GenCount== 1)
3541 		{
3542 			memcpy(pPsdScan->nPSDReport_MaxHold, pPsdScan->nPSDReport, BT_8703B_1ANT_ANTDET_PSD_POINTS*sizeof(u4Byte));
3543 
3544 			for (i= pPsdScan->nPSDStartPoint; i<=pPsdScan->nPSDStopPoint; i++)
3545 			{
3546 				//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), Max_Hold i = %d, PSDReport = %d dB\n", i,  pPsdScan->nPSDReport_MaxHold[i]));
3547 			}
3548 
3549 			pPsdScan->nPSDMaxValuePoint = 0;
3550 			pPsdScan->nPSDMaxValue = 0;
3551 
3552 		}
3553 		else
3554 		{
3555 			for (i= pPsdScan->nPSDStartPoint; i<=pPsdScan->nPSDStopPoint; i++)
3556 			{
3557 				if (pPsdScan->nPSDReport[i] > pPsdScan->nPSDReport_MaxHold[i])
3558 			 	pPsdScan->nPSDReport_MaxHold[i] = pPsdScan->nPSDReport[i];
3559 
3560 				//search Max Value
3561 				if (i ==pPsdScan->nPSDStartPoint )
3562 				{
3563 					i_max = i;
3564 					val_max = pPsdScan->nPSDReport_MaxHold[i];
3565 				}
3566 				else
3567 				{
3568 					if (pPsdScan->nPSDReport_MaxHold[i] > val_max)
3569 					{
3570 						i_max = i;
3571 						val_max = pPsdScan->nPSDReport_MaxHold[i];
3572 					}
3573 				}
3574 
3575 				//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), Max_Hold i = %d, PSDReport = %d dB\n", i,  pPsdScan->nPSDReport_MaxHold[i]));
3576 
3577 			}
3578 
3579 			pPsdScan->nPSDMaxValuePoint = i_max;
3580 			pPsdScan->nPSDMaxValue = val_max;
3581 
3582 			//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), Max_Hold i_Max = %d, PSDReport_Max = %d dB\n", pPsdScan->nPSDMaxValuePoint
3583 			//		,pPsdScan->nPSDMaxValue));
3584 		}
3585 
3586 
3587 }
3588 
3589 u4Byte
halbtc8703b1ant_PSD_GetData(IN PBTC_COEXIST pBtCoexist,IN u4Byte nPoint)3590 halbtc8703b1ant_PSD_GetData(
3591 	IN	PBTC_COEXIST		pBtCoexist,
3592 	IN	u4Byte				nPoint
3593 	)
3594 {
3595 	//reg 0x808[9:0]: FFT data x
3596 	//reg 0x808[22]: 0-->1 to get 1 FFT data y
3597 	//reg 0x8b4[15:0]: FFT data y report
3598 
3599 	u4Byte val = 0, psd_report =0;
3600 
3601 	val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x808);
3602 
3603 	val &= 0xffbffc00;
3604 	val |= nPoint;
3605 
3606 	 pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x808, val);
3607 
3608 	val |= 0x00400000;
3609 	 pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x808, val);
3610 
3611 
3612 	val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x8b4);
3613 
3614 	psd_report = val & 0x0000ffff;
3615 
3616 	return psd_report;
3617 }
3618 
3619 
3620 VOID
halbtc8703b1ant_PSD_SweepPoint(IN PBTC_COEXIST pBtCoexist,IN u4Byte centFreq,IN s4Byte offset,IN u4Byte span,IN u4Byte points,IN u4Byte avgnum)3621 halbtc8703b1ant_PSD_SweepPoint(
3622 IN	PBTC_COEXIST			pBtCoexist,
3623 	IN	u4Byte				centFreq,
3624 	IN	s4Byte				offset,
3625 	IN	u4Byte				span,
3626 	IN	u4Byte				points,
3627 	IN	u4Byte				avgnum
3628 	)
3629 {
3630 	u4Byte	 i,val,n,k=0;
3631 	u4Byte	nPoints=0, psd_report=0;
3632 	u4Byte	nStartP=0, nStopP=0, nDeltaFreqPerPoint=156250;
3633 	u4Byte    nPSDCenterFreq=20*10^6, freq,freq1,freq2;
3634 	BOOLEAN outloop = FALSE;
3635 	u1Byte	 flag = 0;
3636 	u4Byte 	tmp, PsdRep1, PsdRep2;
3637 	u4Byte	WiFi_OriginalChannel = 1;
3638 
3639 	pPsdScan->bIsPSDRunning = TRUE;
3640 
3641 	do
3642 	{
3643 		switch(flag)
3644 		{
3645 		   case 0:  //Get PSD parameters
3646 		   default:
3647 			//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), centFreq=0x%x, offset=0x%x, span=0x%x\n",
3648 		       //     centFreq, offset, span));
3649 
3650 			pPsdScan->nPSDBandWidth = 40*1000000;
3651 			pPsdScan->nPSDPoint = points;
3652 			pPsdScan->nPSDStartBase = points/2;
3653 			pPsdScan->nPSDAvgNum = avgnum;
3654 			pPsdScan->nRealCentFreq = centFreq;
3655 			pPsdScan->nRealOffset = offset;
3656 			pPsdScan->nRealSpan = span;
3657 
3658 
3659 			nPoints = pPsdScan->nPSDPoint;
3660 			nDeltaFreqPerPoint = pPsdScan->nPSDBandWidth/pPsdScan->nPSDPoint;
3661 
3662 			//PSD point setup
3663 			val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x808);
3664 			val &= 0xffff0fff;
3665 
3666 			switch(pPsdScan->nPSDPoint)
3667 			{
3668 				case 128:
3669 					val |= 0x0;
3670 					break;
3671 				case 256:
3672 				default:
3673 					val |=0x00004000;
3674 				 	break;
3675 				case 512:
3676 					val |= 0x00008000;
3677 					break;
3678 				case 1024:
3679 					val |= 0x0000c000;
3680 					break;
3681 			}
3682 
3683 			switch(pPsdScan->nPSDAvgNum)
3684 			{
3685 				case 1:
3686 					val |= 0x0;
3687 					break;
3688 				case 8:
3689 					val |=0x00001000;
3690 				 	break;
3691 				case 16:
3692 					val |= 0x00002000;
3693 					break;
3694 				case 32:
3695 				default:
3696 					val |= 0x00003000;
3697 					break;
3698 			}
3699 			 pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x808, val);
3700 
3701 			//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), PSD BW= %d, DeltaFreq=%d\n"
3702 		 	//	, pPsdScan->nPSDBandWidth, nDeltaFreqPerPoint));
3703 			flag = 1;
3704 			break;
3705 		  case 1:	  //calculate the PSD point index from freq/offset/span
3706 		  	nPSDCenterFreq = pPsdScan->nPSDBandWidth /2 +offset*(1000000);
3707 			//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), PSD Center Freq = %d\n", (centFreq + offset)));
3708 
3709 			nStartP = pPsdScan->nPSDStartBase + (nPSDCenterFreq - span *(1000000)/2) /nDeltaFreqPerPoint;
3710 			pPsdScan->nPSDStartPoint = nStartP - pPsdScan->nPSDStartBase;
3711 			//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), Start PSD Poin Matrix Index = %d\n", pPsdScan->nPSDStartPoint));
3712 
3713 			nStopP = pPsdScan->nPSDStartBase + (nPSDCenterFreq + span *(1000000)/2) /nDeltaFreqPerPoint;
3714 			pPsdScan->nPSDStopPoint = nStopP - pPsdScan->nPSDStartBase-1;
3715 			//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), Stop PSD Poin Matrix Index = %d\n",pPsdScan->nPSDStopPoint));
3716 
3717 			flag = 2;
3718 			break;
3719 		  case 2:  //set RF channel/BW/Mode
3720 
3721 		  	//set 3-wire off
3722 		  	val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x88c);
3723 			val |= 0x00300000;
3724 			 pBtCoexist->fBtcWrite4Byte(pBtCoexist,0x88c,val);
3725 
3726 			//CCK off
3727 			val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x800);
3728 			val &= 0xfeffffff;
3729 			pBtCoexist->fBtcWrite4Byte(pBtCoexist,0x800,val);
3730 
3731 			//store WiFi original channel
3732 			WiFi_OriginalChannel = pBtCoexist->fBtcGetRfReg(pBtCoexist, BTC_RF_A, 0x18, 0x3ff);
3733 
3734 			//Set RF channel
3735 		       if (centFreq == 2484)
3736 				pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x18, 0x3ff, 0xe);
3737 			else
3738 				pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x18, 0x3ff, (centFreq-2412)/5 + 1); //WiFi TRx Mask on
3739 
3740 			//Set  RF mode = Rx, RF Gain = 0x8a0
3741 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x0, 0xfffff, 0x308a0);
3742 
3743 			//Set RF Rx filter corner
3744 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1e, 0xfffff, 0x3e4);
3745 
3746 			//Set TRx mask off
3747 			//un-lock TRx Mask setup
3748 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdd, 0x80, 0x1);
3749 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdf, 0x1, 0x1);
3750 
3751 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
3752 
3753 			flag = 3;
3754 			break;
3755 		  case 3:
3756 		  	memset(pPsdScan->nPSDReport,0, pPsdScan->nPSDPoint*sizeof(u4Byte));
3757 			nStartP = pPsdScan->nPSDStartPoint + pPsdScan->nPSDStartBase;
3758 			nStopP = pPsdScan->nPSDStopPoint +  pPsdScan->nPSDStartBase + 1;
3759 
3760 			i = nStartP;
3761 
3762 			while (i < nStopP)
3763 			{
3764 				if (i >= nPoints)
3765 				{
3766 					psd_report = halbtc8703b1ant_PSD_GetData(pBtCoexist,i-nPoints);
3767 				}
3768 				else
3769 				{
3770 					psd_report = halbtc8703b1ant_PSD_GetData(pBtCoexist,i);
3771 				}
3772 
3773 				if (psd_report == 0)
3774 					tmp = 0;
3775 				else
3776 					//tmp =  20*log10((double)psd_report);
3777 					//20*log2(x)/log2(10), log2Base return theresult of the psd_report*100
3778 					tmp = 6 * halbtc8703b1ant_PSD_Log2Base(pBtCoexist, psd_report);
3779 
3780 				n = i-pPsdScan->nPSDStartBase;
3781 				pPsdScan->nPSDReport[n] =  tmp;
3782 				PsdRep1 = pPsdScan->nPSDReport[n] /100;
3783 				PsdRep2 = pPsdScan->nPSDReport[n] - PsdRep1 * 100;
3784 
3785 				freq =  ((centFreq-20) * 1000000 + n * nDeltaFreqPerPoint);
3786 				freq1 = freq/1000000;
3787 				freq2 = freq/1000 - freq1 * 1000;
3788 /*
3789 				if (freq2 < 100)
3790 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), i = %d (%d.0%d MHz)", n, freq1, freq2));
3791 				else
3792 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint(), i = %d (%d.%d MHz)", n, freq1, freq2));
3793 
3794 				if (PsdRep2 < 10)
3795 					RT_TRACE(COMP_COEX, DBG_LOUD, (", PSDReport = %d (%d.0%d dB)\n",psd_report, PsdRep1, PsdRep2));
3796 				else
3797 					RT_TRACE(COMP_COEX, DBG_LOUD, (", PSDReport = %d (%d.%d dB)\n",psd_report, PsdRep1,PsdRep2));
3798 */
3799 				i++;
3800 
3801 				k=0;
3802 
3803 				//Add Delay between PSD point
3804 				while(1)
3805 				{
3806 					if (k++ > 20000)
3807 					 break;
3808 				}
3809 
3810 				//RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx SweepPSDPoint()==============\n"));
3811 			}
3812 
3813 			flag = 100;
3814 			break;
3815 		  case 99:	//error
3816 
3817 			outloop = TRUE;
3818 			break;
3819 		  case 100: //recovery
3820 
3821 		  	//set 3-wire on
3822 		  	val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x88c);
3823 			val &=0xffcfffff;
3824 			pBtCoexist->fBtcWrite4Byte(pBtCoexist,0x88c,val);
3825 
3826 			//CCK on
3827 			val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x800);
3828 			val |= 0x01000000;
3829 			pBtCoexist->fBtcWrite4Byte(pBtCoexist,0x800,val);
3830 
3831 			//PSD off
3832 			val = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x808);
3833 			val &=0xffbfffff;
3834 			pBtCoexist->fBtcWrite4Byte(pBtCoexist,0x808,val);
3835 
3836 		  	//TRx Mask on
3837 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x780);
3838 
3839 			//lock TRx Mask setup
3840 		  	pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdd, 0x80, 0x0);
3841 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0xdf, 0x1, 0x0);
3842 
3843 			//Set RF Rx filter corner
3844 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1e, 0xfffff, 0x0);
3845 
3846 			//restore WiFi original channel
3847 			pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x18, 0x3ff, WiFi_OriginalChannel);
3848 
3849 			outloop = TRUE;
3850 			break;
3851 
3852 		}
3853 
3854 	}while (!outloop);
3855 
3856 
3857 
3858 	pPsdScan->bIsPSDRunning = FALSE;
3859 
3860 
3861 }
3862 
3863 VOID
halbtc8703b1ant_PSD_AntennaDetection(IN PBTC_COEXIST pBtCoexist,IN u4Byte BTTxTime,IN u4Byte BTLEChannel)3864 halbtc8703b1ant_PSD_AntennaDetection(
3865 	IN	PBTC_COEXIST			pBtCoexist,
3866 	IN	u4Byte					BTTxTime,
3867 	IN	u4Byte					BTLEChannel
3868 	)
3869 {
3870 	u4Byte 	realseconds = 0, i=0, j=0;
3871 	u4Byte	WLPSD_CentFreq = 2484, WLPSD_Span = 2, WLPSD_SweepCount = 50;
3872 	s4Byte 	WLPSD_Offset = -4;
3873 	u1Byte 	BTLECh[13] = {3,6,8,11,13,16,18,21,23,26,28,31,33};
3874 
3875 	u1Byte	H2C_Parameter[3] ={0},u1Tmpa,u1Tmpb;
3876 
3877 	u1Byte	state=0;
3878 	BOOLEAN		outloop = FALSE, BTResp = FALSE, bScan ,bRoam;
3879 	u4Byte		freq,freq1,freq2,PsdRep1, PsdRep2, nDeltaFreqPerPoint,u4Tmp;
3880 	PBTC_BOARD_INFO		pBoardInfo=&pBtCoexist->boardInfo;
3881 
3882 	pBoardInfo->btdmAntDetFinish = FALSE;
3883 	memset(pPsdScan->nAntDet_PeakVal, 0, 16*sizeof(UCHAR));
3884 	memset(pPsdScan->nAntDet_PeakFreq, 0, 16*sizeof(UCHAR));
3885 
3886 	if (pBoardInfo->bTfbgaPackage) //for TFBGA
3887 		pPsdScan->nAntDet_ThresOffset = 5;
3888 	else
3889 		pPsdScan->nAntDet_ThresOffset = 0;
3890 
3891 	do
3892 	{
3893 		switch(state)
3894 		{
3895 			case 0:
3896 				if (BTLEChannel == 39)
3897 				 WLPSD_CentFreq = 2484;
3898 				else
3899 				{
3900 					for (i=1; i<=13; i++)
3901 					{
3902 						if (BTLECh[i-1] == BTLEChannel)
3903 						{
3904 							 WLPSD_CentFreq = 2412 + (i-1) * 5;
3905 							 break;
3906 						}
3907 					}
3908 
3909 					if (i == 14)
3910 					{
3911 
3912 						RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Abort!!, Invalid LE channel = %d\n ",  BTLEChannel));
3913 						outloop = TRUE;
3914 						break;
3915 					}
3916 				}
3917 
3918 				WLPSD_SweepCount = BTTxTime * 238 /100;  //BTTxTime/0.42
3919 
3920 				if (WLPSD_SweepCount % 5 != 0)
3921 					WLPSD_SweepCount = (WLPSD_SweepCount/5 + 1) * 5;
3922 
3923 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), BT_LETxTime=%d,  BT_LECh = %d\n", BTTxTime, BTLEChannel));
3924 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), WLPSD_CentFreq=%d,  WLPSD_Offset = %d, WLPSD_Span = %d, WLPSD_SweepCount = %d\n",
3925 									WLPSD_CentFreq, WLPSD_Offset, WLPSD_Span,WLPSD_SweepCount));
3926 
3927 				state = 1;
3928 				break;
3929 			case 1: //stop coex DM & set antenna path
3930 	//Stop Coex DM
3931 	pBtCoexist->bStopCoexDm = TRUE;
3932 
3933 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Stop Coex DM!!\n"));
3934 
3935 				//set native power save
3936 				halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3937 
3938 				//Set TDMA off,
3939 				halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 0);
3940 
3941 				//Set coex table
3942 				halbtc8703b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
3943 
3944 				if(pBoardInfo->btdmAntPos == BTC_ANTENNA_AT_MAIN_PORT)
3945 				{
3946 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Antenna at Main Port\n"));
3947 				}
3948 				else
3949 				{
3950 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Antenna at Aux Port\n"));
3951 				}
3952 
3953 	//Set Antenna path, switch WiFi to un-certain antenna port
3954 	halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, FALSE, FALSE);
3955 
3956 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Set Antenna to BT!!\n"));
3957 
3958 				//Set AFH mask on at WiFi channel 2472MHz +/- 10MHz
3959 				H2C_Parameter[0] = 0x1;
3960 				H2C_Parameter[1] = 0xd;
3961 				H2C_Parameter[2] = 0x14;
3962 
3963 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Set AFH on, Cent-Ch= %d,  Mask=%d\n",
3964 									H2C_Parameter[1],H2C_Parameter[2]));
3965 
3966 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x66, 3, H2C_Parameter);
3967 
3968 				u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
3969        			u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
3970 				u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x778);
3971 
3972 				 RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x778=0x%x\n",
3973 	   	      				u4Tmp,  u1Tmpa, u1Tmpb));
3974 
3975 				state =2;
3976 				break;
3977 			case 2:	//Pre-sweep background psd
3978 				for (pPsdScan->nPSDGenCount=1; pPsdScan->nPSDGenCount<=3; pPsdScan->nPSDGenCount++)
3979 	{
3980 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), PSDGenCount = %d\n ",  pPsdScan->nPSDGenCount));
3981 					halbtc8703b1ant_PSD_SweepPoint(pBtCoexist, WLPSD_CentFreq, WLPSD_Offset, WLPSD_Span, BT_8703B_1ANT_ANTDET_PSD_POINTS, BT_8703B_1ANT_ANTDET_PSD_AVGNUM);
3982 					halbtc8703b1ant_PSD_MaxHoldData(pBtCoexist,  pPsdScan->nPSDGenCount);
3983 				}
3984 
3985 				pPsdScan->nAntDet_PrePSDScanPeakVal = pPsdScan->nPSDMaxValue;
3986 
3987 				if (pPsdScan->nPSDMaxValue > (BT_8703B_1ANT_ANTDET_PSDTHRES_BACKGROUND+pPsdScan->nAntDet_ThresOffset)*100)
3988 				{
3989 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Abort Antenna Detection!! becaus background = %d > thres (%d)\n",
3990 						pPsdScan->nPSDMaxValue/100, BT_8703B_1ANT_ANTDET_PSDTHRES_BACKGROUND+pPsdScan->nAntDet_ThresOffset));
3991 					pBoardInfo->btdmAntDetFinish = FALSE;
3992 					pBoardInfo->btdmAntNumByAntDet = 1;
3993 					pPsdScan->nAntDet_Result = 5;
3994 					state = 99;
3995 				}
3996 				else
3997 				{
3998 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Start Antenna Detection!! becaus background = %d <= thres (%d)\n",
3999 						pPsdScan->nPSDMaxValue/100, BT_8703B_1ANT_ANTDET_PSDTHRES_BACKGROUND+pPsdScan->nAntDet_ThresOffset));
4000 					state = 3;
4001 				}
4002 				break;
4003 			case 3:
4004 				BTResp = pBtCoexist->fBtcSetBtAntDetection(pBtCoexist, (u1Byte)(BTTxTime&0xff), (u1Byte)(BTLEChannel&0xff));
4005 
4006 				for (pPsdScan->nPSDGenCount=1; pPsdScan->nPSDGenCount<=WLPSD_SweepCount; pPsdScan->nPSDGenCount++)
4007 		{
4008 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), PSDGenCount = %d\n ",  pPsdScan->nPSDGenCount));
4009 					halbtc8703b1ant_PSD_SweepPoint(pBtCoexist, WLPSD_CentFreq, WLPSD_Offset, WLPSD_Span, BT_8703B_1ANT_ANTDET_PSD_POINTS, BT_8703B_1ANT_ANTDET_PSD_AVGNUM);
4010 					halbtc8703b1ant_PSD_MaxHoldData(pBtCoexist,  pPsdScan->nPSDGenCount);
4011 
4012 					pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
4013 					pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
4014 
4015 					if (bScan ||bRoam)
4016 			{
4017 						pBoardInfo->btdmAntDetFinish = FALSE;
4018 						pBoardInfo->btdmAntNumByAntDet = 1;
4019 						pPsdScan->nAntDet_Result = 8;
4020 						state = 99;
4021 						break;
4022 			}
4023 		}
4024 
4025 				pPsdScan->nAntDet_PSDScanPeakVal = pPsdScan->nPSDMaxValue;
4026 				pPsdScan->nAntDet_PSDScanPeakFreq = pPsdScan->nPSDMaxValuePoint;
4027 				state = 4;
4028 				break;
4029 			case 4:
4030 
4031 				if (pPsdScan->nPSDPoint == 0)
4032 					nDeltaFreqPerPoint = 0;
4033 		else
4034 					nDeltaFreqPerPoint = pPsdScan->nPSDBandWidth/pPsdScan->nPSDPoint;
4035 
4036 				PsdRep1 = pPsdScan->nPSDMaxValue/100;
4037 				PsdRep2 = pPsdScan->nPSDMaxValue - PsdRep1 * 100;
4038 
4039 				freq = ((pPsdScan->nRealCentFreq-20) * 1000000 + pPsdScan->nPSDMaxValuePoint * nDeltaFreqPerPoint);
4040 				freq1 = freq/1000000;
4041 				freq2 = freq/1000 - freq1 * 1000;
4042 
4043 				if (freq2 < 100)
4044 		{
4045 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Max Value: Freq = %d.0%d MHz", freq1, freq2));
4046 					CL_SPRINTF(pPsdScan->nAntDet_PeakFreq, BT_8703B_1ANT_ANTDET_BUF_LEN, "%d.0%d", freq1,freq2);
4047 				}
4048 				else
4049 			{
4050 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Max Value: Freq = %d.%d MHz",  freq1, freq2));
4051 					CL_SPRINTF(pPsdScan->nAntDet_PeakFreq, BT_8703B_1ANT_ANTDET_BUF_LEN, "%d.%d", freq1,freq2);
4052 				}
4053 
4054 				if (PsdRep2 < 10)
4055 				{
4056 					RT_TRACE(COMP_COEX, DBG_LOUD, (", Value = %d.0%d dB\n",   PsdRep1, PsdRep2));
4057 					CL_SPRINTF(pPsdScan->nAntDet_PeakVal, BT_8703B_1ANT_ANTDET_BUF_LEN, "%d.0%d", PsdRep1,PsdRep2);
4058 				}
4059 				else
4060 				{
4061 					RT_TRACE(COMP_COEX, DBG_LOUD, (", Value = %d.%d dB\n",PsdRep1, PsdRep2));
4062 					CL_SPRINTF(pPsdScan->nAntDet_PeakVal, BT_8703B_1ANT_ANTDET_BUF_LEN, "%d.%d", PsdRep1,PsdRep2);
4063 				}
4064 
4065 				pPsdScan->nAntDet_IsBTReplyAvailable = TRUE;
4066 
4067 				if (BTResp == FALSE)
4068 				{
4069 					pPsdScan->nAntDet_IsBTReplyAvailable = FALSE;
4070 					pPsdScan->nAntDet_Result = 0;
4071 					pBoardInfo->btdmAntDetFinish = FALSE;
4072 					pBoardInfo->btdmAntNumByAntDet = 1;
4073 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), BT Response = Fail \n "));
4074 				}
4075 				else if (pPsdScan->nPSDMaxValue > (BT_8703B_1ANT_ANTDET_PSDTHRES_2ANT_BADISOLATION)*100)
4076 				{
4077 					pPsdScan->nAntDet_Result = 1;
4078 					pBoardInfo->btdmAntDetFinish = TRUE;
4079 					pBoardInfo->btdmAntNumByAntDet = 2;
4080 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Detect Result = 2-Ant, Bad-Isolation!! \n"));
4081 				}
4082 				else if (pPsdScan->nPSDMaxValue > (BT_8703B_1ANT_ANTDET_PSDTHRES_2ANT_GOODISOLATION+pPsdScan->nAntDet_ThresOffset)*100)
4083 					{
4084 					pPsdScan->nAntDet_Result = 2;
4085 					pBoardInfo->btdmAntDetFinish = TRUE;
4086 					pBoardInfo->btdmAntNumByAntDet = 2;
4087 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Detect Result = 2-Ant, Good-Isolation!! \n"));
4088 				}
4089 				else if (pPsdScan->nPSDMaxValue > (BT_8703B_1ANT_ANTDET_PSDTHRES_1ANT)*100)
4090 				{
4091 					pPsdScan->nAntDet_Result = 3;
4092 					pBoardInfo->btdmAntDetFinish = TRUE;
4093 					pBoardInfo->btdmAntNumByAntDet = 1;
4094 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Detect Result = 1-Ant!!\n"));
4095 					}
4096 				else
4097 				{
4098 					pPsdScan->nAntDet_Result = 4;
4099 					pBoardInfo->btdmAntDetFinish = FALSE;
4100 					pBoardInfo->btdmAntNumByAntDet = 1;
4101 					RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Detect Result = 1-Ant, un-certainity!!\n"));
4102 				}
4103 
4104 				state = 99;
4105 				break;
4106 			case 99:  //restore setup
4107 
4108 				//Set AFH mask off at WiFi channel 2472MHz +/- 10MHz
4109 				H2C_Parameter[0] = 0x0;
4110 				H2C_Parameter[1] = 0x0;
4111 				H2C_Parameter[2] = 0x0;
4112 
4113 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Set AFH on, Cent-Ch= %d,  Mask=%d\n",
4114 					H2C_Parameter[1],H2C_Parameter[2]));
4115 
4116 				pBtCoexist->fBtcFillH2c(pBtCoexist, 0x66, 3, H2C_Parameter);
4117 
4118 				//Set Antenna Path
4119 				halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, FORCE_EXEC, FALSE, FALSE);
4120 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Set Antenna to PTA\n!!"));
4121 
4122 				//Resume Coex DM
4123 				pBtCoexist->bStopCoexDm = FALSE;
4124 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Resume Coex DM\n!!"));
4125 
4126 				//stimulate coex running
4127 				halbtc8703b1ant_RunCoexistMechanism(pBtCoexist);
4128 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Stimulate Coex running\n!!"));
4129 
4130 				outloop = TRUE;
4131 				break;
4132 			}
4133 
4134 	}while(!outloop);
4135 
4136 
4137 
4138 		}
4139 
4140 VOID
halbtc8703b1ant_PSD_AntennaDetectionCheck(IN PBTC_COEXIST pBtCoexist)4141 halbtc8703b1ant_PSD_AntennaDetectionCheck(
4142 	IN	PBTC_COEXIST			pBtCoexist
4143 	)
4144 {
4145 	static u4Byte AntDetCount = 0, AntDetFailCount = 0;
4146 	PBTC_BOARD_INFO 	pBoardInfo=&pBtCoexist->boardInfo;
4147 
4148 	BOOLEAN bScan, bRoam;
4149 
4150 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
4151 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
4152 
4153 
4154 	pPsdScan->nAntDet_BTTxTime = 20;  //0.42ms*50 = 20ms
4155 	pPsdScan->nAntDet_BTLEChannel = 39;
4156 
4157 	AntDetCount++;
4158 
4159 	pPsdScan->bAntDet_TryCount = AntDetCount;
4160 
4161 	if (bScan ||bRoam)
4162 		{
4163 		pBoardInfo->btdmAntDetFinish = FALSE;
4164 		pPsdScan->nAntDet_Result = 6;
4165 	}
4166 	else if(pBtCoexist->btInfo.bBtDisabled)
4167 	{
4168 		pBoardInfo->btdmAntDetFinish = FALSE;
4169 		pPsdScan->nAntDet_Result = 11;
4170 	}
4171 	else if (pCoexSta->nNumOfProfile >= 1)
4172 	{
4173 		pBoardInfo->btdmAntDetFinish = FALSE;
4174 		pPsdScan->nAntDet_Result = 7;
4175 	}
4176 	else if (!pPsdScan->nAntDet_IsAntDetAvailable)  //Antenna initial setup is not ready
4177 	{
4178 		pBoardInfo->btdmAntDetFinish = FALSE;
4179 		pPsdScan->nAntDet_Result = 9;
4180 	}
4181 	else if (pCoexSta->bC2hBtInquiryPage)
4182 		{
4183 		pBoardInfo->btdmAntDetFinish = FALSE;
4184 		pPsdScan->nAntDet_Result = 10;
4185 		}
4186 		else
4187 		{
4188 		halbtc8703b1ant_PSD_AntennaDetection(pBtCoexist,  pPsdScan->nAntDet_BTTxTime, pPsdScan->nAntDet_BTLEChannel);
4189 		}
4190 
4191 	if (!pBoardInfo->btdmAntDetFinish)
4192 		AntDetFailCount++;
4193 
4194 	pPsdScan->bAntDet_FailCount = AntDetFailCount;
4195 
4196 }
4197 
4198 
4199 //============================================================
4200 // work around function start with wa_halbtc8703b1ant_
4201 //============================================================
4202 //============================================================
4203 // extern function start with EXhalbtc8703b1ant_
4204 //============================================================
4205 VOID
EXhalbtc8703b1ant_PowerOnSetting(IN PBTC_COEXIST pBtCoexist)4206 EXhalbtc8703b1ant_PowerOnSetting(
4207 	IN	PBTC_COEXIST		pBtCoexist
4208 	)
4209 {
4210 	PBTC_BOARD_INFO 	pBoardInfo=&pBtCoexist->boardInfo;
4211 	u1Byte u1Tmp=0x0;
4212 	u2Byte u2Tmp=0x0;
4213 
4214 	RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx Execute 8703b 1-Ant PowerOn Setting xxxxxxxxxxxxxxxx!!\n"));
4215 
4216 	RT_TRACE(COMP_COEX, DBG_LOUD, ("Ant Det Finish = %s, Ant Det Number  = %d\n",
4217 		 (pBoardInfo->btdmAntDetFinish? "Yes":"No"), pBoardInfo->btdmAntNumByAntDet));
4218 
4219 	pBtCoexist->bStopCoexDm = TRUE;
4220 
4221 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x67, 0x20);
4222 
4223 	// enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly.
4224 	u2Tmp = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x2);
4225 	pBtCoexist->fBtcWrite2Byte(pBtCoexist, 0x2, u2Tmp|BIT0|BIT1);
4226 
4227 	// set GRAN_BT = 1
4228 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
4229 	// set WLAN_ACT = 0
4230 	pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
4231 
4232 	//
4233 	// S0 or S1 setting and Local register setting(By the setting fw can get ant number, S0/S1, ... info)
4234 	// Local setting bit define
4235 	//	BIT0: "0" for no antenna inverse; "1" for antenna inverse
4236 	//	BIT1: "0" for internal switch; "1" for external switch
4237 	//	BIT2: "0" for one antenna; "1" for two antenna
4238 	// NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and BIT2=0
4239 	if(pBtCoexist->chipInterface == BTC_INTF_USB)
4240 	{
4241 		// fixed at S0 for USB interface
4242 		pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
4243 
4244 	 	u1Tmp |= 0x1;	// antenna inverse
4245 		pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0xfe08, u1Tmp);
4246 
4247 		pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_AUX_PORT;
4248 	}
4249 	else
4250 	{
4251 		// for PCIE and SDIO interface, we check efuse 0xc3[6]
4252 		if(pBoardInfo->singleAntPath == 0)
4253 		{
4254 			// set to S1
4255 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
4256 			pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT;
4257 		}
4258 		else if(pBoardInfo->singleAntPath == 1)
4259 		{
4260 			// set to S0
4261 			pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
4262 			u1Tmp |= 0x1;	// antenna inverse
4263 			pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_AUX_PORT;
4264 		}
4265 
4266 		if(pBtCoexist->chipInterface == BTC_INTF_PCI)
4267 		{
4268 			pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0x384, u1Tmp);
4269 		}
4270 		else if(pBtCoexist->chipInterface == BTC_INTF_SDIO)
4271 		{
4272 			pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0x60, u1Tmp);
4273 		}
4274 	}
4275 }
4276 
4277 VOID
EXhalbtc8703b1ant_PreLoadFirmware(IN PBTC_COEXIST pBtCoexist)4278 EXhalbtc8703b1ant_PreLoadFirmware(
4279 	IN	PBTC_COEXIST		pBtCoexist
4280 	)
4281 {
4282 }
4283 
4284 VOID
EXhalbtc8703b1ant_InitHwConfig(IN PBTC_COEXIST pBtCoexist,IN BOOLEAN bWifiOnly)4285 EXhalbtc8703b1ant_InitHwConfig(
4286 	IN	PBTC_COEXIST		pBtCoexist,
4287 	IN	BOOLEAN				bWifiOnly
4288 	)
4289 {
4290 	halbtc8703b1ant_InitHwConfig(pBtCoexist, TRUE, bWifiOnly);
4291 	pBtCoexist->bStopCoexDm = FALSE;
4292 }
4293 
4294 VOID
EXhalbtc8703b1ant_InitCoexDm(IN PBTC_COEXIST pBtCoexist)4295 EXhalbtc8703b1ant_InitCoexDm(
4296 	IN	PBTC_COEXIST		pBtCoexist
4297 	)
4298 {
4299 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Coex Mechanism Init!!\n"));
4300 
4301 	pBtCoexist->bStopCoexDm = FALSE;
4302 
4303 	halbtc8703b1ant_InitCoexDm(pBtCoexist);
4304 
4305 	halbtc8703b1ant_QueryBtInfo(pBtCoexist);
4306 }
4307 
4308 VOID
EXhalbtc8703b1ant_DisplayCoexInfo(IN PBTC_COEXIST pBtCoexist)4309 EXhalbtc8703b1ant_DisplayCoexInfo(
4310 	IN	PBTC_COEXIST		pBtCoexist
4311 	)
4312 {
4313 	PBTC_BOARD_INFO		pBoardInfo=&pBtCoexist->boardInfo;
4314 	PBTC_STACK_INFO		pStackInfo=&pBtCoexist->stackInfo;
4315 	PBTC_BT_LINK_INFO	pBtLinkInfo=&pBtCoexist->btLinkInfo;
4316 	pu1Byte				cliBuf=pBtCoexist->cliBuf;
4317 	u1Byte				u1Tmp[4], i, btInfoExt, psTdmaCase=0;
4318 	u2Byte				u2Tmp[4];
4319 	u4Byte				u4Tmp[4];
4320 	u4Byte				faOfdm, faCck;
4321 	u4Byte				fwVer=0, btPatchVer=0;
4322 	static u1Byte			PopReportIn10s = 0;
4323 
4324 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n ============[BT Coexist info]============");
4325 	CL_PRINTF(cliBuf);
4326 
4327 	if(pBtCoexist->bManualControl)
4328 	{
4329 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n ============[Under Manual Control]============");
4330 		CL_PRINTF(cliBuf);
4331 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n ==========================================");
4332 		CL_PRINTF(cliBuf);
4333 	}
4334 	if(pBtCoexist->bStopCoexDm)
4335 	{
4336 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n ============[Coex is STOPPED]============");
4337 		CL_PRINTF(cliBuf);
4338 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n ==========================================");
4339 		CL_PRINTF(cliBuf);
4340 	}
4341 
4342 	if (pPsdScan->bAntDet_TryCount == 0)
4343 	{
4344 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d", "Ant PG Num/ Mech/ Pos",
4345 		pBoardInfo->pgAntNum, pBoardInfo->btdmAntNum, pBoardInfo->btdmAntPos);
4346 	CL_PRINTF(cliBuf);
4347 	}
4348 	else
4349 	{
4350 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d  (%d/%d/%d)", "Ant PG Num/ Mech(Ant_Det)/ Pos",
4351 		pBoardInfo->pgAntNum, pBoardInfo->btdmAntNumByAntDet, pBoardInfo->btdmAntPos,
4352 		pPsdScan->bAntDet_TryCount, pPsdScan->bAntDet_FailCount, pPsdScan->nAntDet_Result);
4353 		CL_PRINTF(cliBuf);
4354 
4355 		if (pBoardInfo->btdmAntDetFinish)
4356 		{
4357 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "Ant Det PSD Value",  pPsdScan->nAntDet_PeakVal);
4358 			CL_PRINTF(cliBuf);
4359 		}
4360 	}
4361 
4362 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s / %d", "BT stack/ hci ext ver", \
4363 		((pStackInfo->bProfileNotified)? "Yes":"No"), pStackInfo->hciVersion);
4364 	CL_PRINTF(cliBuf);
4365 
4366 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_BT_PATCH_VER, &btPatchVer);
4367 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);
4368 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)", "CoexVer/ FwVer/ PatchVer", \
4369 		GLCoexVerDate8703b1Ant, GLCoexVer8703b1Ant, fwVer, btPatchVer, btPatchVer);
4370 	CL_PRINTF(cliBuf);
4371 
4372 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x ", "Wifi channel informed to BT", \
4373 		pCoexDm->wifiChnlInfo[0], pCoexDm->wifiChnlInfo[1],
4374 		pCoexDm->wifiChnlInfo[2]);
4375 	CL_PRINTF(cliBuf);
4376 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s", "WifibHiPri/ Ccklock/ CckEverLock", \
4377 		(pCoexSta->bWiFiIsHighPriTask? "Yes":"No"),
4378 		(pCoexSta->bCCKLock? "Yes":"No"),
4379 		(pCoexSta->bCCKEverLock? "Yes":"No"));
4380 	CL_PRINTF(cliBuf);
4381 
4382 	// wifi status
4383 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Wifi Status]============");
4384 	CL_PRINTF(cliBuf);
4385 	pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_WIFI_STATUS);
4386 
4387 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[BT Status]============");
4388 	CL_PRINTF(cliBuf);
4389 
4390 	PopReportIn10s++;
4391 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s/ %d/ %d/ %d] ", "BT [status/ rssi/ retryCnt/ popCnt]", \
4392 		((pBtCoexist->btInfo.bBtDisabled)? ("disabled"):	((pCoexSta->bC2hBtInquiryPage)?("inquiry/page scan"):((BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus)? "non-connected idle":
4393 		(  (BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus)? "connected-idle":"busy")))),
4394 		pCoexSta->btRssi, pCoexSta->btRetryCnt, pCoexSta->popEventCnt);
4395 	CL_PRINTF(cliBuf);
4396 
4397 	if (PopReportIn10s >= 5)
4398 	{
4399 		pCoexSta->popEventCnt = 0;
4400 		PopReportIn10s = 0;
4401 	}
4402 
4403 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d / %d / %d / %d / %d", "SCO/HID/PAN/A2DP/Hi-Pri/ErrNotify", \
4404 		pBtLinkInfo->bScoExist, pBtLinkInfo->bHidExist, pBtLinkInfo->bPanExist, pBtLinkInfo->bA2dpExist, pBtLinkInfo->bBtHiPriLinkExist, pCoexSta->wrongProfileNotification);
4405 	CL_PRINTF(cliBuf);
4406 
4407 	if (pStackInfo->bProfileNotified)
4408 	{
4409 	pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_BT_LINK_INFO);
4410 	}
4411 	else
4412 	{
4413 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "BT Role", \
4414 		(pBtLinkInfo->bSlaveRole )? "Slave":"Master");
4415 		CL_PRINTF(cliBuf);
4416 	}
4417 
4418 	btInfoExt = pCoexSta->btInfoExt;
4419 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "BT Info A2DP rate", \
4420 		(btInfoExt&BIT0)? "Basic rate":"EDR rate");
4421 	CL_PRINTF(cliBuf);
4422 
4423 	for(i=0; i<BT_INFO_SRC_8703B_1ANT_MAX; i++)
4424 	{
4425 		if(pCoexSta->btInfoC2hCnt[i])
4426 		{
4427 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)", GLBtInfoSrc8703b1Ant[i], \
4428 				pCoexSta->btInfoC2h[i][0], pCoexSta->btInfoC2h[i][1],
4429 				pCoexSta->btInfoC2h[i][2], pCoexSta->btInfoC2h[i][3],
4430 				pCoexSta->btInfoC2h[i][4], pCoexSta->btInfoC2h[i][5],
4431 				pCoexSta->btInfoC2h[i][6], pCoexSta->btInfoC2hCnt[i]);
4432 			CL_PRINTF(cliBuf);
4433 		}
4434 	}
4435 
4436 
4437 	if(pBtCoexist->bManualControl)
4438 	{
4439 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Sw mechanism] (before Manual)============");
4440 	}
4441 	else
4442 	{
4443 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Sw mechanism]============");
4444 	}
4445 
4446 		CL_PRINTF(cliBuf);
4447 
4448 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "SM[LowPenaltyRA]", \
4449 			pCoexDm->bCurLowPenaltyRa);
4450 		CL_PRINTF(cliBuf);
4451 
4452 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %d ", "DelBA/ BtCtrlAgg/ AggSize", \
4453 			(pBtCoexist->btInfo.bRejectAggPkt? "Yes":"No"), (pBtCoexist->btInfo.bBtCtrlAggBufSize? "Yes":"No"),
4454 				pBtCoexist->btInfo.aggBufSize);
4455 		CL_PRINTF(cliBuf);
4456 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x ", "Rate Mask", \
4457 				pBtCoexist->btInfo.raMask);
4458 		CL_PRINTF(cliBuf);
4459 
4460 		// Fw mechanism
4461 		if(pBtCoexist->bManualControl)
4462 		{
4463 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Fw mechanism] (before Manual) ============");
4464 		}
4465 		else
4466 		{
4467 			CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Fw mechanism]============");
4468 		}
4469 
4470 		CL_PRINTF(cliBuf);
4471 
4472 		psTdmaCase = pCoexDm->curPsTdma;
4473 		if (pBoardInfo->btdmAntNumByAntDet == 2)
4474 		{
4475 			if (pCoexDm->bCurPsTdmaOn)
4476 				psTdmaCase = psTdmaCase +100;  //for WiFi RSSI low or BT RSSI low
4477 			else
4478 				psTdmaCase = 1; //always translate to TDMA(off,1) for TDMA-off case
4479 		}
4480 
4481 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x case-%d (%s,%s)", "PS TDMA", \
4482 			pCoexDm->psTdmaPara[0], pCoexDm->psTdmaPara[1],
4483 			pCoexDm->psTdmaPara[2], pCoexDm->psTdmaPara[3],
4484 			pCoexDm->psTdmaPara[4], psTdmaCase,
4485 			(pCoexDm->bCurPsTdmaOn? "On":"Off"),
4486 			(pCoexDm->bAutoTdmaAdjust? "Adj":"Fix") );
4487 
4488 		CL_PRINTF(cliBuf);
4489 
4490 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "Coex Table Type", \
4491 			pCoexSta->nCoexTableType);
4492 		CL_PRINTF(cliBuf);
4493 
4494 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "IgnWlanAct", \
4495 			pCoexDm->bCurIgnoreWlanAct);
4496 		CL_PRINTF(cliBuf);
4497 
4498 		/*
4499 		CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x ", "Latest error condition(should be 0)", \
4500 			pCoexDm->errorCondition);
4501 		CL_PRINTF(cliBuf);
4502 		*/
4503 
4504 	// Hw setting
4505 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Hw setting]============");
4506 	CL_PRINTF(cliBuf);
4507 
4508 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x", "backup ARFR1/ARFR2/RL/AMaxTime", \
4509 		pCoexDm->backupArfrCnt1, pCoexDm->backupArfrCnt2, pCoexDm->backupRetryLimit, pCoexDm->backupAmpduMaxTime);
4510 	CL_PRINTF(cliBuf);
4511 
4512 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
4513 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
4514 	u2Tmp[0] = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
4515 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
4516 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x", "0x430/0x434/0x42a/0x456", \
4517 		u4Tmp[0], u4Tmp[1], u2Tmp[0], u1Tmp[0]);
4518 	CL_PRINTF(cliBuf);
4519 
4520 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x778);
4521 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6cc);
4522 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x880);
4523 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x778/0x6cc/0x880[29:25]", \
4524 		u1Tmp[0], u4Tmp[0],  (u4Tmp[1]&0x3e000000) >> 25);
4525 	CL_PRINTF(cliBuf);
4526 
4527 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
4528 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
4529 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x764);
4530 	u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e);
4531 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x948/ 0x67[5] / 0x764 / 0x76e", \
4532 		u4Tmp[0], ((u1Tmp[0]&0x20)>> 5), (u4Tmp[1] & 0xffff), u1Tmp[1]);
4533 	CL_PRINTF(cliBuf);
4534 
4535 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x92c);
4536 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x930);
4537 	u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x944);
4538 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]", \
4539 		u4Tmp[0]&0x3, u4Tmp[1]&0xff, u4Tmp[2]&0x3);
4540 	CL_PRINTF(cliBuf);
4541 
4542 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x39);
4543 	u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x40);
4544 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
4545 	u1Tmp[2] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x64);
4546 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x38[11]/0x40/0x4c[24:23]/0x64[0]", \
4547 		((u1Tmp[0] & 0x8)>>3), u1Tmp[1], ((u4Tmp[0]&0x01800000)>>23), u1Tmp[2]&0x1);
4548 	CL_PRINTF(cliBuf);
4549 
4550 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x550);
4551 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x522);
4552 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522", \
4553 		u4Tmp[0], u1Tmp[0]);
4554 	CL_PRINTF(cliBuf);
4555 
4556 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xc50);
4557 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x49c);
4558 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x", "0xc50(dig)/0x49c(null-drop)", \
4559 		u4Tmp[0]&0xff, u1Tmp[0]);
4560 	CL_PRINTF(cliBuf);
4561 
4562 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda0);
4563 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda4);
4564 	u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda8);
4565 	u4Tmp[3] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xcf0);
4566 
4567 	u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0xa5b);
4568 	u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0xa5c);
4569 
4570 	faOfdm = ((u4Tmp[0]&0xffff0000) >> 16) +  ((u4Tmp[1]&0xffff0000) >> 16) + (u4Tmp[1] & 0xffff) +  (u4Tmp[2] & 0xffff) + \
4571 		             ((u4Tmp[3]&0xffff0000) >> 16) + (u4Tmp[3] & 0xffff) ;
4572 	faCck = (u1Tmp[0] << 8) + u1Tmp[1];
4573 
4574 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "OFDM-CCA/OFDM-FA/CCK-FA", \
4575 		u4Tmp[0]&0xffff, faOfdm, faCck);
4576 	CL_PRINTF(cliBuf);
4577 
4578 
4579 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d", "CRC_OK CCK/11g/11n/11n-Agg", \
4580 		pCoexSta->nCRCOK_CCK, pCoexSta->nCRCOK_11g, pCoexSta->nCRCOK_11n, pCoexSta->nCRCOK_11nAgg);
4581 	CL_PRINTF(cliBuf);
4582 
4583 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d", "CRC_Err CCK/11g/11n/11n-Agg", \
4584 		pCoexSta->nCRCErr_CCK, pCoexSta->nCRCErr_11g, pCoexSta->nCRCErr_11n, pCoexSta->nCRCErr_11nAgg);
4585 	CL_PRINTF(cliBuf);
4586 
4587 	u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c0);
4588 	u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c4);
4589 	u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c8);
4590 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x6c0/0x6c4/0x6c8(coexTable)", \
4591 		u4Tmp[0], u4Tmp[1], u4Tmp[2]);
4592 	CL_PRINTF(cliBuf);
4593 
4594 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d", "0x770(high-pri rx/tx)", \
4595 		pCoexSta->highPriorityRx, pCoexSta->highPriorityTx);
4596 	CL_PRINTF(cliBuf);
4597 	CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d", "0x774(low-pri rx/tx)", \
4598 		pCoexSta->lowPriorityRx, pCoexSta->lowPriorityTx);
4599 	CL_PRINTF(cliBuf);
4600 #if(BT_AUTO_REPORT_ONLY_8703B_1ANT == 1)
4601 	//halbtc8703b1ant_MonitorBtCtr(pBtCoexist);
4602 #endif
4603 	pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_COEX_STATISTICS);
4604 }
4605 
4606 
4607 VOID
EXhalbtc8703b1ant_IpsNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)4608 EXhalbtc8703b1ant_IpsNotify(
4609 	IN	PBTC_COEXIST		pBtCoexist,
4610 	IN	u1Byte			type
4611 	)
4612 {
4613 	u4Byte	u4Tmp=0;
4614 
4615 	if(pBtCoexist->bManualControl ||	pBtCoexist->bStopCoexDm)
4616 		return;
4617 
4618 	if(BTC_IPS_ENTER == type)
4619 	{
4620 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], IPS ENTER notify\n"));
4621 		pCoexSta->bUnderIps = TRUE;
4622 
4623 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0);
4624 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, FALSE, TRUE);
4625 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
4626 		//halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF);
4627 	}
4628 	else if(BTC_IPS_LEAVE == type)
4629 	{
4630 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], IPS LEAVE notify\n"));
4631 
4632 		halbtc8703b1ant_InitHwConfig(pBtCoexist, FALSE, FALSE);
4633 		halbtc8703b1ant_InitCoexDm(pBtCoexist);
4634 		halbtc8703b1ant_QueryBtInfo(pBtCoexist);
4635 
4636 		pCoexSta->bUnderIps = FALSE;
4637 	}
4638 }
4639 
4640 VOID
EXhalbtc8703b1ant_LpsNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)4641 EXhalbtc8703b1ant_LpsNotify(
4642 	IN	PBTC_COEXIST		pBtCoexist,
4643 	IN	u1Byte			type
4644 	)
4645 {
4646 	if(pBtCoexist->bManualControl || pBtCoexist->bStopCoexDm)
4647 		return;
4648 
4649 	if(BTC_LPS_ENABLE == type)
4650 	{
4651 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], LPS ENABLE notify\n"));
4652 		pCoexSta->bUnderLps = TRUE;
4653 	}
4654 	else if(BTC_LPS_DISABLE == type)
4655 	{
4656 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], LPS DISABLE notify\n"));
4657 		pCoexSta->bUnderLps = FALSE;
4658 	}
4659 }
4660 
4661 VOID
EXhalbtc8703b1ant_ScanNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)4662 EXhalbtc8703b1ant_ScanNotify(
4663 	IN	PBTC_COEXIST		pBtCoexist,
4664 	IN	u1Byte			type
4665 	)
4666 {
4667 	BOOLEAN bWifiConnected=FALSE, bBtHsOn=FALSE;
4668 	u4Byte	wifiLinkStatus=0;
4669 	u4Byte	numOfWifiLink=0;
4670 	BOOLEAN	bBtCtrlAggBufSize=FALSE;
4671 	u1Byte	aggBufSize=5;
4672 
4673 	u1Byte u1Tmpa, u1Tmpb;
4674 	u4Byte u4Tmp;
4675 
4676 	if(pBtCoexist->bManualControl ||
4677 		pBtCoexist->bStopCoexDm )
4678 		return;
4679 
4680 	if(BTC_SCAN_START == type)
4681 	{
4682 		pCoexSta->bWiFiIsHighPriTask = TRUE;
4683 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], SCAN START notify\n"));
4684 		pPsdScan->nAntDet_IsAntDetAvailable = TRUE;
4685 		halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);  //Force antenna setup for no scan result issue
4686 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, FORCE_EXEC, FALSE, FALSE);
4687 		u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
4688 		u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
4689 		u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
4690 
4691 
4692 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
4693 			u4Tmp,  u1Tmpa, u1Tmpb));
4694 	}
4695 	else
4696 	{
4697 		pCoexSta->bWiFiIsHighPriTask = FALSE;
4698 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], SCAN FINISH notify\n"));
4699 
4700 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_AP_NUM, &pCoexSta->nScanAPNum);
4701 	}
4702 
4703 	if(pBtCoexist->btInfo.bBtDisabled)
4704 		return;
4705 
4706 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
4707 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
4708 
4709 	halbtc8703b1ant_QueryBtInfo(pBtCoexist);
4710 
4711 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
4712 	numOfWifiLink = wifiLinkStatus>>16;
4713 	if(numOfWifiLink >= 2)
4714 	{
4715 		halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
4716 		halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, bBtCtrlAggBufSize, aggBufSize);
4717 		halbtc8703b1ant_ActionWifiMultiPort(pBtCoexist);
4718 		return;
4719 	}
4720 
4721 	if(pCoexSta->bC2hBtInquiryPage)
4722 	{
4723 		halbtc8703b1ant_ActionBtInquiry(pBtCoexist);
4724 		return;
4725 	}
4726 	else if(bBtHsOn)
4727 	{
4728 		halbtc8703b1ant_ActionHs(pBtCoexist);
4729 		return;
4730 	}
4731 
4732 	if(BTC_SCAN_START == type)
4733 	{
4734 		//RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], SCAN START notify\n"));
4735 		if(!bWifiConnected)	// non-connected scan
4736 		{
4737 			halbtc8703b1ant_ActionWifiNotConnectedScan(pBtCoexist);
4738 		}
4739 		else	// wifi is connected
4740 		{
4741 			halbtc8703b1ant_ActionWifiConnectedScan(pBtCoexist);
4742 		}
4743 	}
4744 	else if(BTC_SCAN_FINISH == type)
4745 	{
4746 		//RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], SCAN FINISH notify\n"));
4747 		if(!bWifiConnected)	// non-connected scan
4748 		{
4749 			halbtc8703b1ant_ActionWifiNotConnected(pBtCoexist);
4750 		}
4751 		else
4752 		{
4753 			halbtc8703b1ant_ActionWifiConnected(pBtCoexist);
4754 		}
4755 	}
4756 }
4757 
4758 VOID
EXhalbtc8703b1ant_ConnectNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)4759 EXhalbtc8703b1ant_ConnectNotify(
4760 	IN	PBTC_COEXIST		pBtCoexist,
4761 	IN	u1Byte			type
4762 	)
4763 {
4764 	BOOLEAN	bWifiConnected=FALSE, bBtHsOn=FALSE;
4765 	u4Byte	wifiLinkStatus=0;
4766 	u4Byte	numOfWifiLink=0;
4767 	BOOLEAN	bBtCtrlAggBufSize=FALSE;
4768 	u1Byte	aggBufSize=5;
4769 
4770 	if(pBtCoexist->bManualControl ||
4771 		pBtCoexist->bStopCoexDm ||
4772 		pBtCoexist->btInfo.bBtDisabled )
4773 		return;
4774 
4775 	if(BTC_ASSOCIATE_START == type)
4776 	{
4777 		pCoexSta->bWiFiIsHighPriTask = TRUE;
4778 		 pPsdScan->nAntDet_IsAntDetAvailable = TRUE;
4779 		halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);  //Force antenna setup for no scan result issue
4780 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, FORCE_EXEC, FALSE, FALSE);
4781 		 RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CONNECT START notify\n"));
4782 		 pCoexDm->nArpCnt = 0;
4783 	}
4784 	else
4785 	{
4786 		pCoexSta->bWiFiIsHighPriTask = FALSE;
4787 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CONNECT FINISH notify\n"));
4788 		//pCoexDm->nArpCnt = 0;
4789 	}
4790 
4791 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
4792 	numOfWifiLink = wifiLinkStatus>>16;
4793 	if(numOfWifiLink >= 2)
4794 	{
4795 		halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
4796 		halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, bBtCtrlAggBufSize, aggBufSize);
4797 		halbtc8703b1ant_ActionWifiMultiPort(pBtCoexist);
4798 		return;
4799 	}
4800 
4801 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
4802 	if(pCoexSta->bC2hBtInquiryPage)
4803 	{
4804 		halbtc8703b1ant_ActionBtInquiry(pBtCoexist);
4805 		return;
4806 	}
4807 	else if(bBtHsOn)
4808 	{
4809 		halbtc8703b1ant_ActionHs(pBtCoexist);
4810 		return;
4811 	}
4812 
4813 	if(BTC_ASSOCIATE_START == type)
4814 	{
4815 		//RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CONNECT START notify\n"));
4816 		halbtc8703b1ant_ActionWifiNotConnectedAssoAuth(pBtCoexist);
4817 	}
4818 	else if(BTC_ASSOCIATE_FINISH == type)
4819 	{
4820 		//RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CONNECT FINISH notify\n"));
4821 
4822 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
4823 		if(!bWifiConnected) // non-connected scan
4824 		{
4825 			halbtc8703b1ant_ActionWifiNotConnected(pBtCoexist);
4826 		}
4827 		else
4828 		{
4829 			halbtc8703b1ant_ActionWifiConnected(pBtCoexist);
4830 		}
4831 	}
4832 }
4833 
4834 VOID
EXhalbtc8703b1ant_MediaStatusNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)4835 EXhalbtc8703b1ant_MediaStatusNotify(
4836 	IN	PBTC_COEXIST			pBtCoexist,
4837 	IN	u1Byte				type
4838 	)
4839 {
4840 	u1Byte			H2C_Parameter[3] ={0};
4841 	u4Byte			wifiBw;
4842 	u1Byte			wifiCentralChnl;
4843 	BOOLEAN			bWifiUnderBMode = FALSE;
4844 
4845 	if(pBtCoexist->bManualControl ||
4846 		pBtCoexist->bStopCoexDm ||
4847 		pBtCoexist->btInfo.bBtDisabled )
4848 		return;
4849 
4850 	if(BTC_MEDIA_CONNECT == type)
4851 	{
4852 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], MEDIA connect notify\n"));
4853 		halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 8);  //Force antenna setup for no scan result issue
4854 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_PTA, FORCE_EXEC, FALSE, FALSE);
4855 		pPsdScan->nAntDet_IsAntDetAvailable = TRUE;
4856 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode);
4857 
4858 		//Set CCK Tx/Rx high Pri except 11b mode
4859 		if (bWifiUnderBMode)
4860 		{
4861 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x00); //CCK Tx
4862 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x00); //CCK Rx
4863 		}
4864 		else
4865 		{
4866 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x10); //CCK Tx
4867 			pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x10); //CCK Rx
4868 		}
4869 
4870 		pCoexDm->backupArfrCnt1 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
4871 		pCoexDm->backupArfrCnt2 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
4872 		pCoexDm->backupRetryLimit = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
4873 		pCoexDm->backupAmpduMaxTime = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
4874 	}
4875 	else
4876 	{
4877 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], MEDIA disconnect notify\n"));
4878 		pCoexDm->nArpCnt = 0;
4879 
4880 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x0); //CCK Tx
4881 		pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x0); //CCK Rx
4882 
4883 		pCoexSta->bCCKEverLock = FALSE;
4884 	}
4885 
4886 	// only 2.4G we need to inform bt the chnl mask
4887 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifiCentralChnl);
4888 	if( (BTC_MEDIA_CONNECT == type) &&
4889 		(wifiCentralChnl <= 14) )
4890 	{
4891 		//H2C_Parameter[0] = 0x1;
4892 		H2C_Parameter[0] = 0x0;
4893 		H2C_Parameter[1] = wifiCentralChnl;
4894 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
4895 		if(BTC_WIFI_BW_HT40 == wifiBw)
4896 			H2C_Parameter[2] = 0x30;
4897 		else
4898 			H2C_Parameter[2] = 0x20;
4899 	}
4900 
4901 	pCoexDm->wifiChnlInfo[0] = H2C_Parameter[0];
4902 	pCoexDm->wifiChnlInfo[1] = H2C_Parameter[1];
4903 	pCoexDm->wifiChnlInfo[2] = H2C_Parameter[2];
4904 
4905 	RT_TRACE(COMP_COEX, DBG_TRACE, ("[BTCoex], FW write 0x66=0x%x\n",
4906 		H2C_Parameter[0]<<16|H2C_Parameter[1]<<8|H2C_Parameter[2]));
4907 
4908 	pBtCoexist->fBtcFillH2c(pBtCoexist, 0x66, 3, H2C_Parameter);
4909 }
4910 
4911 VOID
EXhalbtc8703b1ant_SpecialPacketNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)4912 EXhalbtc8703b1ant_SpecialPacketNotify(
4913 	IN	PBTC_COEXIST			pBtCoexist,
4914 	IN	u1Byte				type
4915 	)
4916 {
4917 	BOOLEAN	bBtHsOn=FALSE;
4918 	u4Byte	wifiLinkStatus=0;
4919 	u4Byte	numOfWifiLink=0;
4920 	BOOLEAN	bBtCtrlAggBufSize=FALSE, bUnder4way=FALSE;
4921 	u1Byte	aggBufSize=5;
4922 
4923 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &bUnder4way);
4924 
4925 	if(pBtCoexist->bManualControl ||
4926 		pBtCoexist->bStopCoexDm ||
4927 		pBtCoexist->btInfo.bBtDisabled )
4928 		return;
4929 
4930 	if( BTC_PACKET_DHCP == type ||
4931 		BTC_PACKET_EAPOL == type ||
4932 		BTC_PACKET_ARP == type )
4933 	{
4934 		if (BTC_PACKET_ARP == type)
4935 		{
4936 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], special Packet ARP notify\n"));
4937 
4938 			pCoexDm->nArpCnt++;
4939 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ARP Packet Count = %d\n", pCoexDm->nArpCnt));
4940 
4941 			if((pCoexDm->nArpCnt >= 10) && (!bUnder4way)) // if APR PKT > 10 after connect, do not go to ActionWifiConnectedSpecialPacket(pBtCoexist)
4942 			{
4943 				pCoexSta->bWiFiIsHighPriTask = FALSE;
4944 			}
4945 			else
4946 			{
4947 				pCoexSta->bWiFiIsHighPriTask = TRUE;
4948 			}
4949 		}
4950 		else
4951 		{
4952 			pCoexSta->bWiFiIsHighPriTask = TRUE;
4953 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], special Packet DHCP or EAPOL notify\n"));
4954 		}
4955 	}
4956 	else
4957 	{
4958 		pCoexSta->bWiFiIsHighPriTask = FALSE;
4959 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], special Packet [Type = %d] notify\n", type));
4960 	}
4961 
4962 	pCoexSta->specialPktPeriodCnt = 0;
4963 
4964 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
4965 	numOfWifiLink = wifiLinkStatus>>16;
4966 	if(numOfWifiLink >= 2)
4967 	{
4968 		halbtc8703b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
4969 		halbtc8703b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, FALSE, bBtCtrlAggBufSize, aggBufSize);
4970 		halbtc8703b1ant_ActionWifiMultiPort(pBtCoexist);
4971 		return;
4972 	}
4973 
4974 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
4975 	if(pCoexSta->bC2hBtInquiryPage)
4976 	{
4977 		halbtc8703b1ant_ActionBtInquiry(pBtCoexist);
4978 		return;
4979 	}
4980 	else if(bBtHsOn)
4981 	{
4982 		halbtc8703b1ant_ActionHs(pBtCoexist);
4983 		return;
4984 	}
4985 
4986 	if( BTC_PACKET_DHCP == type ||
4987 		BTC_PACKET_EAPOL == type ||
4988 		( (BTC_PACKET_ARP == type ) && (pCoexSta->bWiFiIsHighPriTask) ) )
4989 		{
4990 		halbtc8703b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
4991 	}
4992 }
4993 
4994 VOID
EXhalbtc8703b1ant_BtInfoNotify(IN PBTC_COEXIST pBtCoexist,IN pu1Byte tmpBuf,IN u1Byte length)4995 EXhalbtc8703b1ant_BtInfoNotify(
4996 	IN	PBTC_COEXIST		pBtCoexist,
4997 	IN	pu1Byte			tmpBuf,
4998 	IN	u1Byte			length
4999 	)
5000 {
5001 	PBTC_BT_LINK_INFO	pBtLinkInfo=&pBtCoexist->btLinkInfo;
5002 	u1Byte				btInfo=0;
5003 	u1Byte				i, rspSource=0;
5004 	BOOLEAN				bWifiConnected=FALSE;
5005 	BOOLEAN				bBtBusy=FALSE;
5006 	PBTC_BOARD_INFO 	pBoardInfo=&pBtCoexist->boardInfo;
5007 
5008 	pCoexSta->bC2hBtInfoReqSent = FALSE;
5009 
5010 	rspSource = tmpBuf[0]&0xf;
5011 	if(rspSource >= BT_INFO_SRC_8703B_1ANT_MAX)
5012 		rspSource = BT_INFO_SRC_8703B_1ANT_WIFI_FW;
5013 	pCoexSta->btInfoC2hCnt[rspSource]++;
5014 
5015 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Bt info[%d], length=%d, hex data=[", rspSource, length));
5016 	for(i=0; i<length; i++)
5017 	{
5018 		pCoexSta->btInfoC2h[rspSource][i] = tmpBuf[i];
5019 		if(i == 1)
5020 			btInfo = tmpBuf[i];
5021 		if(i == length-1)
5022 		{
5023 			RT_TRACE(COMP_COEX, DBG_LOUD, ("0x%02x]\n", tmpBuf[i]));
5024 		}
5025 		else
5026 		{
5027 			RT_TRACE(COMP_COEX, DBG_LOUD, ("0x%02x, ", tmpBuf[i]));
5028 		}
5029 	}
5030 
5031 	// if 0xff, it means BT is under WHCK test
5032 	if (btInfo == 0xff)
5033 		pCoexSta->bBtWhckTest = TRUE;
5034 	else
5035 		pCoexSta->bBtWhckTest = FALSE;
5036 
5037 	if(BT_INFO_SRC_8703B_1ANT_WIFI_FW != rspSource)
5038 	{
5039 		pCoexSta->btRetryCnt =	// [3:0]
5040 			pCoexSta->btInfoC2h[rspSource][2]&0xf;
5041 
5042 		if (pCoexSta->btRetryCnt >= 1)
5043 			pCoexSta->popEventCnt++;
5044 
5045 		if (pCoexSta->btInfoC2h[rspSource][2]&0x20)
5046 			pCoexSta->bC2hBtPage = TRUE;
5047 		else
5048 			pCoexSta->bC2hBtPage = FALSE;
5049 
5050 		pCoexSta->btRssi =
5051 			pCoexSta->btInfoC2h[rspSource][3]*2-90;
5052 			//pCoexSta->btInfoC2h[rspSource][3]*2+10;
5053 
5054 		pCoexSta->btInfoExt =
5055 			pCoexSta->btInfoC2h[rspSource][4];
5056 
5057 		pCoexSta->bBtTxRxMask = (pCoexSta->btInfoC2h[rspSource][2]&0x40);
5058 		pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TX_RX_MASK, &pCoexSta->bBtTxRxMask);
5059 
5060 #if BT_8703B_1ANT_ANTDET_ENABLE
5061 #if BT_8703B_1ANT_ANTDET_COEXMECHANISMSWITCH_ENABLE
5062 		if ((pBoardInfo->btdmAntDetFinish) && (pBoardInfo->btdmAntNumByAntDet == 2))
5063 		{
5064 			if(pCoexSta->bBtTxRxMask)
5065 			{
5066 				/* BT into is responded by BT FW and BT RF REG 0x3C != 0x15 => Need to switch BT TRx Mask */
5067 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x1\n"));
5068 
5069 				//BT TRx Mask un-lock 0x2c[0], 0x30[0] = 1
5070 				pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x2c, 0x7c45);
5071 				pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x30, 0x7c45);
5072 
5073 				pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x1);
5074 			}
5075 		}
5076 		else
5077 #endif
5078 #endif
5079 
5080 		{
5081 			if(!pCoexSta->bBtTxRxMask)
5082 			{
5083 				/* BT into is responded by BT FW and BT RF REG 0x3C != 0x15 => Need to switch BT TRx Mask */
5084 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n"));
5085 				pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x15);
5086 
5087 				//BT TRx Mask lock 0x2c[0], 0x30[0] = 0
5088 				pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x2c, 0x7c44);
5089 				pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x30, 0x7c44);
5090 			}
5091 		}
5092 
5093 		// Here we need to resend some wifi info to BT
5094 		// because bt is reset and loss of the info.
5095 		if(pCoexSta->btInfoExt & BIT1)
5096 		{
5097 			RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n"));
5098 			pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
5099 			if(bWifiConnected)
5100 			{
5101 				EXhalbtc8703b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_CONNECT);
5102 			}
5103 			else
5104 			{
5105 				EXhalbtc8703b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
5106 			}
5107 		}
5108 
5109 		if(pCoexSta->btInfoExt & BIT3)
5110 		{
5111 			if(!pBtCoexist->bManualControl && !pBtCoexist->bStopCoexDm)
5112 			{
5113 				RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n"));
5114 				halbtc8703b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, FALSE);
5115 			}
5116 		}
5117 		else
5118 		{
5119 			// BT already NOT ignore Wlan active, do nothing here.
5120 		}
5121 #if(BT_AUTO_REPORT_ONLY_8703B_1ANT == 0)
5122 		if( (pCoexSta->btInfoExt & BIT4) )
5123 		{
5124 			// BT auto report already enabled, do nothing
5125 		}
5126 		else
5127 		{
5128 			halbtc8703b1ant_BtAutoReport(pBtCoexist, FORCE_EXEC, TRUE);
5129 		}
5130 #endif
5131 	}
5132 
5133 	// check BIT2 first ==> check if bt is under inquiry or page scan
5134 	if(btInfo & BT_INFO_8703B_1ANT_B_INQ_PAGE)
5135 		pCoexSta->bC2hBtInquiryPage = TRUE;
5136 	else
5137 		pCoexSta->bC2hBtInquiryPage = FALSE;
5138 
5139 	pCoexSta->nNumOfProfile = 0;
5140 
5141 	// set link exist status
5142 	if(!(btInfo&BT_INFO_8703B_1ANT_B_CONNECTION))
5143 	{
5144 		pCoexSta->bBtLinkExist = FALSE;
5145 		pCoexSta->bPanExist = FALSE;
5146 		pCoexSta->bA2dpExist = FALSE;
5147 		pCoexSta->bHidExist = FALSE;
5148 		pCoexSta->bScoExist = FALSE;
5149 
5150 		pCoexSta->bBtHiPriLinkExist = FALSE;
5151 	}
5152 	else	// connection exists
5153 	{
5154 		pCoexSta->bBtLinkExist = TRUE;
5155 		if(btInfo & BT_INFO_8703B_1ANT_B_FTP)
5156 		{
5157 			pCoexSta->bPanExist = TRUE;
5158 			pCoexSta->nNumOfProfile++;
5159 		}
5160 		else
5161 			pCoexSta->bPanExist = FALSE;
5162 		if(btInfo & BT_INFO_8703B_1ANT_B_A2DP)
5163 		{
5164 			pCoexSta->bA2dpExist = TRUE;
5165 			pCoexSta->nNumOfProfile++;
5166 		}
5167 		else
5168 			pCoexSta->bA2dpExist = FALSE;
5169 		if(btInfo & BT_INFO_8703B_1ANT_B_HID)
5170 		{
5171 			pCoexSta->bHidExist = TRUE;
5172 			pCoexSta->nNumOfProfile++;
5173 		}
5174 		else
5175 			pCoexSta->bHidExist = FALSE;
5176 		if(btInfo & BT_INFO_8703B_1ANT_B_SCO_ESCO)
5177 		{
5178 			pCoexSta->bScoExist = TRUE;
5179 			pCoexSta->nNumOfProfile++;
5180 		}
5181 		else
5182 			pCoexSta->bScoExist = FALSE;
5183 
5184 		if ((pCoexSta->bHidExist == FALSE) && (pCoexSta->bC2hBtInquiryPage == FALSE) &&( pCoexSta->bScoExist == FALSE))
5185 		{
5186 			if (pCoexSta->highPriorityTx  + pCoexSta->highPriorityRx >= 160)
5187 			{
5188 				pCoexSta->bHidExist = TRUE;
5189 				pCoexSta->wrongProfileNotification++;
5190 				pCoexSta->nNumOfProfile++;
5191 				btInfo = btInfo | 0x28;
5192 			}
5193 		}
5194 
5195 		//Add Hi-Pri Tx/Rx counter to avoid false detection
5196 		if (((pCoexSta->bHidExist) || (pCoexSta->bScoExist)) && (pCoexSta->highPriorityTx + pCoexSta->highPriorityRx >= 160)
5197 			 && (!pCoexSta->bC2hBtInquiryPage))
5198 			pCoexSta->bBtHiPriLinkExist = TRUE;
5199 
5200 		if((btInfo&BT_INFO_8703B_1ANT_B_ACL_BUSY) && (pCoexSta->nNumOfProfile == 0))
5201 		{
5202 			if (pCoexSta->lowPriorityTx + pCoexSta->lowPriorityRx >= 160)
5203 			{
5204 				pCoexSta->bPanExist = TRUE;
5205 				pCoexSta->nNumOfProfile++;
5206 				pCoexSta->wrongProfileNotification++;
5207 				btInfo = btInfo | 0x88;
5208 			}
5209 		}
5210 	}
5211 
5212 	halbtc8703b1ant_UpdateBtLinkInfo(pBtCoexist);
5213 
5214 	btInfo = btInfo & 0x1f;  //mask profile bit for connect-ilde identification ( for CSR case: A2DP idle --> 0x41)
5215 
5216 	if(!(btInfo&BT_INFO_8703B_1ANT_B_CONNECTION))
5217 	{
5218 		pCoexDm->btStatus = BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
5219 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n"));
5220 	}
5221 	else if(btInfo == BT_INFO_8703B_1ANT_B_CONNECTION)	// connection exists but no busy
5222 	{
5223 		pCoexDm->btStatus = BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE;
5224 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n"));
5225 	}
5226 	else if((btInfo&BT_INFO_8703B_1ANT_B_SCO_ESCO) ||
5227 		(btInfo&BT_INFO_8703B_1ANT_B_SCO_BUSY))
5228 	{
5229 		pCoexDm->btStatus = BT_8703B_1ANT_BT_STATUS_SCO_BUSY;
5230 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BtInfoNotify(), BT SCO busy!!!\n"));
5231 	}
5232 	else if(btInfo&BT_INFO_8703B_1ANT_B_ACL_BUSY)
5233 	{
5234 		if(BT_8703B_1ANT_BT_STATUS_ACL_BUSY != pCoexDm->btStatus)
5235 			pCoexDm->bAutoTdmaAdjust = FALSE;
5236 		pCoexDm->btStatus = BT_8703B_1ANT_BT_STATUS_ACL_BUSY;
5237 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BtInfoNotify(), BT ACL busy!!!\n"));
5238 	}
5239 	else
5240 	{
5241 		pCoexDm->btStatus = BT_8703B_1ANT_BT_STATUS_MAX;
5242 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n"));
5243 	}
5244 
5245 	if( (BT_8703B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) ||
5246 		(BT_8703B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
5247 		(BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus) )
5248 		bBtBusy = TRUE;
5249 	else
5250 		bBtBusy = FALSE;
5251 	pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
5252 
5253 	halbtc8703b1ant_RunCoexistMechanism(pBtCoexist);
5254 }
5255 
5256 VOID
EXhalbtc8703b1ant_RfStatusNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte type)5257 EXhalbtc8703b1ant_RfStatusNotify(
5258 	IN	PBTC_COEXIST			pBtCoexist,
5259 	IN	u1Byte					type
5260 	)
5261 {
5262 	u4Byte	u4Tmp;
5263 	u1Byte	u1Tmpa,u1Tmpb, u1Tmpc;
5264 
5265 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], RF Status notify\n"));
5266 
5267 	if(BTC_RF_ON == type)
5268 	{
5269 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], RF is turned ON!!\n"));
5270 		pBtCoexist->bStopCoexDm = FALSE;
5271 	}
5272 	else if(BTC_RF_OFF == type)
5273 	{
5274 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], RF is turned OFF!!\n"));
5275 
5276 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
5277 		halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 0);
5278 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, FALSE, TRUE);
5279 		//halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF);
5280 
5281 		halbtc8703b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, TRUE);
5282 		pBtCoexist->bStopCoexDm = TRUE;
5283 
5284 		u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
5285 		u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
5286  		u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
5287 		u1Tmpc = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e);
5288 
5289 
5290 		RT_TRACE(COMP_COEX, DBG_LOUD, ("############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x, 0x76e=0x%x\n",
5291 			u4Tmp,  u1Tmpa, u1Tmpb, u1Tmpc));
5292 
5293 	}
5294 }
5295 
5296 VOID
EXhalbtc8703b1ant_HaltNotify(IN PBTC_COEXIST pBtCoexist)5297 EXhalbtc8703b1ant_HaltNotify(
5298 	IN	PBTC_COEXIST			pBtCoexist
5299 	)
5300 {
5301 	u4Byte	u4Tmp;
5302 
5303 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Halt notify\n"));
5304 
5305 	halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
5306 	halbtc8703b1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 0);
5307 	halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, FALSE, TRUE);
5308 	//halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF);
5309 
5310 	halbtc8703b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, TRUE);
5311 
5312 	EXhalbtc8703b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
5313 
5314 	pBtCoexist->bStopCoexDm = TRUE;
5315 }
5316 
5317 VOID
EXhalbtc8703b1ant_PnpNotify(IN PBTC_COEXIST pBtCoexist,IN u1Byte pnpState)5318 EXhalbtc8703b1ant_PnpNotify(
5319 	IN	PBTC_COEXIST			pBtCoexist,
5320 	IN	u1Byte				pnpState
5321 	)
5322 {
5323 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Pnp notify\n"));
5324 
5325 	if(BTC_WIFI_PNP_SLEEP == pnpState)
5326 	{
5327 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Pnp notify to SLEEP\n"));
5328 
5329 		halbtc8703b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
5330 		halbtc8703b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0);
5331 		halbtc8703b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, FORCE_EXEC, FALSE, TRUE);
5332 		halbtc8703b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
5333 		//halbtc8703b1ant_SetAntPathDCut(pBtCoexist, FALSE, FALSE, FALSE, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF);
5334 
5335 		pBtCoexist->bStopCoexDm = TRUE;
5336 	}
5337 	else if(BTC_WIFI_PNP_WAKE_UP == pnpState)
5338 	{
5339 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Pnp notify to WAKE UP\n"));
5340 		pBtCoexist->bStopCoexDm = FALSE;
5341 		halbtc8703b1ant_InitHwConfig(pBtCoexist, FALSE, FALSE);
5342 		halbtc8703b1ant_InitCoexDm(pBtCoexist);
5343 		halbtc8703b1ant_QueryBtInfo(pBtCoexist);
5344 	}
5345 }
5346 
5347 VOID
EXhalbtc8703b1ant_CoexDmReset(IN PBTC_COEXIST pBtCoexist)5348 EXhalbtc8703b1ant_CoexDmReset(
5349 	IN	PBTC_COEXIST			pBtCoexist
5350 	)
5351 {
5352 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], *****************Coex DM Reset*****************\n"));
5353 
5354 	halbtc8703b1ant_InitHwConfig(pBtCoexist, FALSE, FALSE);
5355 	//pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x1, 0xfffff, 0x0);
5356 	//pBtCoexist->fBtcSetRfReg(pBtCoexist, BTC_RF_A, 0x2, 0xfffff, 0x0);
5357 	halbtc8703b1ant_InitCoexDm(pBtCoexist);
5358 }
5359 
5360 VOID
EXhalbtc8703b1ant_Periodical(IN PBTC_COEXIST pBtCoexist)5361 EXhalbtc8703b1ant_Periodical(
5362 	IN	PBTC_COEXIST			pBtCoexist
5363 	)
5364 {
5365 	static u1Byte		disVerInfoCnt=0;
5366 	u4Byte				fwVer=0, btPatchVer=0;
5367 	PBTC_BOARD_INFO		pBoardInfo=&pBtCoexist->boardInfo;
5368 	PBTC_STACK_INFO		pStackInfo=&pBtCoexist->stackInfo;
5369 
5370 	RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ==========================Periodical===========================\n"));
5371 
5372 	if(disVerInfoCnt <= 5)
5373 	{
5374 		disVerInfoCnt += 1;
5375 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ****************************************************************\n"));
5376 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
5377 			pBoardInfo->pgAntNum, pBoardInfo->btdmAntNum, pBoardInfo->btdmAntPos));
5378 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], BT stack/ hci ext ver = %s / %d\n",
5379 			((pStackInfo->bProfileNotified)? "Yes":"No"), pStackInfo->hciVersion));
5380 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_BT_PATCH_VER, &btPatchVer);
5381 		pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);
5382 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
5383 			GLCoexVerDate8703b1Ant, GLCoexVer8703b1Ant, fwVer, btPatchVer, btPatchVer));
5384 		RT_TRACE(COMP_COEX, DBG_LOUD, ("[BTCoex], ****************************************************************\n"));
5385 	}
5386 
5387 #if(BT_AUTO_REPORT_ONLY_8703B_1ANT == 0)
5388 	halbtc8703b1ant_QueryBtInfo(pBtCoexist);
5389 	halbtc8703b1ant_MonitorBtEnableDisable(pBtCoexist);
5390 #else
5391 	halbtc8703b1ant_MonitorBtCtr(pBtCoexist);
5392 	halbtc8703b1ant_MonitorWiFiCtr(pBtCoexist);
5393 #if BT_8703B_1ANT_ANTDET_ENABLE
5394 	halbtc8703b1ant_MonitorBtEnableDisable(pBtCoexist);
5395 #endif
5396 
5397 	if( halbtc8703b1ant_IsWifiStatusChanged(pBtCoexist) ||
5398 		pCoexDm->bAutoTdmaAdjust )
5399 	{
5400 
5401 		halbtc8703b1ant_RunCoexistMechanism(pBtCoexist);
5402 	}
5403 
5404 	pCoexSta->specialPktPeriodCnt++;
5405 
5406 	// sample to set bt to execute Ant detection
5407 	//pBtCoexist->fBtcSetBtAntDetection(pBtCoexist, 20, 14);
5408 /*
5409 	if (pPsdScan->bIsAntDetEnable)
5410 	{
5411 		 if (pPsdScan->nPSDGenCount > pPsdScan->realseconds)
5412 			pPsdScan->nPSDGenCount = 0;
5413 
5414 		halbtc8703b1ant_AntennaDetection(pBtCoexist, pPsdScan->realcentFreq,  pPsdScan->realoffset, pPsdScan->realspan,  pPsdScan->realseconds);
5415 		pPsdScan->nPSDGenTotalCount +=2;
5416 		pPsdScan->nPSDGenCount += 2;
5417 	}
5418 */
5419 #endif
5420 }
5421 
5422 VOID
EXhalbtc8703b1ant_AntennaDetection(IN PBTC_COEXIST pBtCoexist,IN u4Byte centFreq,IN u4Byte offset,IN u4Byte span,IN u4Byte seconds)5423 EXhalbtc8703b1ant_AntennaDetection(
5424 	IN	PBTC_COEXIST			pBtCoexist,
5425 	IN	u4Byte					centFreq,
5426 	IN	u4Byte					offset,
5427 	IN	u4Byte					span,
5428 	IN	u4Byte					seconds
5429 	)
5430 {
5431 	static u4Byte AntDetCount = 0, AntDetFailCount = 0;
5432 	PBTC_BOARD_INFO 	pBoardInfo=&pBtCoexist->boardInfo;
5433 	BOOLEAN bScan, bRoam;
5434 
5435 #if BT_8703B_1ANT_ANTDET_ENABLE
5436 
5437 	if (seconds == 0)
5438 	{
5439 		pPsdScan->bAntDet_TryCount	= 0;
5440 		pPsdScan->bAntDet_FailCount	= 0;
5441 		AntDetCount = 0;
5442 		AntDetFailCount = 0;
5443 		pBoardInfo->btdmAntDetFinish = FALSE;
5444 		pBoardInfo->btdmAntNumByAntDet = 1;
5445 		return;
5446 	}
5447 
5448 	if (!pBoardInfo->btdmAntDetFinish)
5449 	{
5450 		pPsdScan->nAntDet_IntevalCount = pPsdScan->nAntDet_IntevalCount+2;
5451 
5452 		if (pPsdScan->nAntDet_IntevalCount >= BT_8703B_1ANT_ANTDET_RETRY_INTERVAL)
5453 		{
5454 			RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Antenna Det Timer is up, Try Detect!!\n"));
5455 			halbtc8703b1ant_PSD_AntennaDetectionCheck(pBtCoexist);
5456 
5457 			if (pBoardInfo->btdmAntDetFinish)
5458 			{
5459 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Antenna Det Success!!\n"));
5460 #if 1
5461 				if (pBoardInfo->btdmAntNumByAntDet == 2)
5462 					halbtc8703b1ant_MechanismSwitch(pBtCoexist, TRUE);
5463 				else
5464 					halbtc8703b1ant_MechanismSwitch(pBtCoexist, FALSE);
5465 #endif
5466 			}
5467 			else
5468 				RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Antenna Det Fail!!\n"));
5469 
5470 			pPsdScan->nAntDet_IntevalCount = 0;
5471 		}
5472 	else
5473 	{
5474 			RT_TRACE(COMP_COEX, DBG_LOUD, ("xxxxxxxxxxxxxxxx AntennaDetect(), Antenna Det Timer is not up! (%d)\n", pPsdScan->nAntDet_IntevalCount));
5475 		}
5476 
5477 	}
5478 #endif
5479 
5480 
5481 /*
5482 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
5483 	pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
5484 
5485 
5486 	pPsdScan->nAntDet_BTTxTime = seconds;  //0.42ms*50 = 20ms
5487 	pPsdScan->nAntDet_BTLEChannel = centFreq;
5488 
5489 	if (seconds == 0)
5490 		{
5491 		pPsdScan->bAntDet_TryCount	= 0;
5492 		pPsdScan->bAntDet_FailCount	= 0;
5493 		AntDetCount = 0;
5494 		AntDetFailCount = 0;
5495 		pBoardInfo->btdmAntDetFinish = FALSE;
5496 		pBoardInfo->btdmAntNumByAntDet = 1;
5497 			 return;
5498 	}
5499 	else
5500 	{
5501  		AntDetCount++;
5502 
5503 		pPsdScan->bAntDet_TryCount = AntDetCount;
5504 
5505 		if (bScan ||bRoam)
5506 		{
5507 			pBoardInfo->btdmAntDetFinish = FALSE;
5508 			pPsdScan->nAntDet_Result = 6;
5509 		}
5510 		else if (pCoexSta->nNumOfProfile >= 1)
5511 		{
5512 			pBoardInfo->btdmAntDetFinish = FALSE;
5513 			pPsdScan->nAntDet_Result = 7;
5514 		}
5515 		else if (!pPsdScan->nAntDet_IsAntDetAvailable)  //Antenna initial setup is not ready
5516 		{
5517 			pBoardInfo->btdmAntDetFinish = FALSE;
5518 			pPsdScan->nAntDet_Result = 9;
5519 		}
5520 		else if (pCoexSta->bC2hBtInquiryPage)
5521 		{
5522 			pBoardInfo->btdmAntDetFinish = FALSE;
5523 			pPsdScan->nAntDet_Result = 10;
5524 		}
5525 		else
5526 		{
5527 			//halbtc8703b1ant_PSD_AntennaDetection(pBtCoexist,  pPsdScan->nAntDet_BTTxTime, pPsdScan->nAntDet_BTLEChannel);
5528 	}
5529 
5530 		if (!pBoardInfo->btdmAntDetFinish)
5531 			AntDetFailCount++;
5532 
5533 		pPsdScan->bAntDet_FailCount = AntDetFailCount;
5534 	}
5535 */
5536 }
5537 
5538 VOID
EXhalbtc8703b1ant_AntennaIsolation(IN PBTC_COEXIST pBtCoexist,IN u4Byte centFreq,IN u4Byte offset,IN u4Byte span,IN u4Byte seconds)5539 EXhalbtc8703b1ant_AntennaIsolation(
5540 	IN	PBTC_COEXIST			pBtCoexist,
5541 	IN	u4Byte					centFreq,
5542 	IN	u4Byte					offset,
5543 	IN	u4Byte					span,
5544 	IN	u4Byte					seconds
5545 	)
5546 {
5547 
5548 
5549 }
5550 
5551 VOID
EXhalbtc8703b1ant_PSDScan(IN PBTC_COEXIST pBtCoexist,IN u4Byte centFreq,IN u4Byte offset,IN u4Byte span,IN u4Byte seconds)5552 EXhalbtc8703b1ant_PSDScan(
5553 	IN	PBTC_COEXIST			pBtCoexist,
5554 	IN	u4Byte					centFreq,
5555 	IN	u4Byte					offset,
5556 	IN	u4Byte					span,
5557 	IN	u4Byte					seconds
5558 	)
5559 {
5560 
5561 
5562 }
5563 
5564 VOID
EXhalbtc8703b1ant_DisplayAntDetection(IN PBTC_COEXIST pBtCoexist)5565 EXhalbtc8703b1ant_DisplayAntDetection(
5566 	IN	PBTC_COEXIST			pBtCoexist
5567 	)
5568 {
5569 	PBTC_BOARD_INFO 	pBoardInfo=&pBtCoexist->boardInfo;
5570 
5571 #if BT_8703B_1ANT_ANTDET_ENABLE
5572 	if (pPsdScan->bAntDet_TryCount != 0)
5573 	{
5574 		halbtc8703b1ant_PSD_ShowAntennaDetectResult(pBtCoexist);
5575 
5576 		if (pBoardInfo->btdmAntDetFinish)
5577 			halbtc8703b1ant_PSD_ShowData(pBtCoexist);
5578 		return;
5579 	}
5580 #endif
5581 
5582 	//halbtc8703b1ant_ShowPSDData(pBtCoexist);
5583 }
5584 
5585 
5586 #endif
5587 
5588