OSDN Git Service

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