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