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