GNU Linux-libre 4.19.263-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 == true)
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         bool bWifiBusy = false;
1762
1763         BTC_PRINT(
1764                 BTC_MSG_ALGORITHM,
1765                 ALGO_TRACE_FW,
1766                 ("[BTCoex], TdmaDurationAdjustForAcl()\n")
1767         );
1768
1769         if (BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY == wifiStatus)
1770                 bWifiBusy = true;
1771         else
1772                 bWifiBusy = false;
1773
1774         if (
1775                 (BT_8723B_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN == wifiStatus) ||
1776                 (BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN == wifiStatus) ||
1777                 (BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SPECIAL_PKT == wifiStatus)
1778         ) {
1779                 if (
1780                         pCoexDm->curPsTdma != 1 &&
1781                         pCoexDm->curPsTdma != 2 &&
1782                         pCoexDm->curPsTdma != 3 &&
1783                         pCoexDm->curPsTdma != 9
1784                 ) {
1785                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1786                         pCoexDm->psTdmaDuAdjType = 9;
1787
1788                         up = 0;
1789                         dn = 0;
1790                         m = 1;
1791                         n = 3;
1792                         result = 0;
1793                         WaitCount = 0;
1794                 }
1795                 return;
1796         }
1797
1798         if (!pCoexDm->bAutoTdmaAdjust) {
1799                 pCoexDm->bAutoTdmaAdjust = true;
1800                 BTC_PRINT(
1801                         BTC_MSG_ALGORITHM,
1802                         ALGO_TRACE_FW_DETAIL,
1803                         ("[BTCoex], first run TdmaDurationAdjust()!!\n")
1804                 );
1805
1806                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
1807                 pCoexDm->psTdmaDuAdjType = 2;
1808                 /*  */
1809                 up = 0;
1810                 dn = 0;
1811                 m = 1;
1812                 n = 3;
1813                 result = 0;
1814                 WaitCount = 0;
1815         } else {
1816                 /* accquire the BT TRx retry count from BT_Info byte2 */
1817                 retryCount = pCoexSta->btRetryCnt;
1818                 btInfoExt = pCoexSta->btInfoExt;
1819                 /* BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], retryCount = %d\n", retryCount)); */
1820                 /* BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], up =%d, dn =%d, m =%d, n =%d, WaitCount =%d\n", */
1821                 /*      up, dn, m, n, WaitCount)); */
1822
1823                 if (pCoexSta->lowPriorityTx > 1050 || pCoexSta->lowPriorityRx > 1250)
1824                         retryCount++;
1825
1826                 result = 0;
1827                 WaitCount++;
1828
1829                 if (retryCount == 0) { /*  no retry in the last 2-second duration */
1830                         up++;
1831                         dn--;
1832
1833                         if (dn <= 0)
1834                                 dn = 0;
1835
1836                         if (up >= n) { /*  if 連續 n 個2秒 retry count為0, 則調寬WiFi duration */
1837                                 WaitCount = 0;
1838                                 n = 3;
1839                                 up = 0;
1840                                 dn = 0;
1841                                 result = 1;
1842                                 BTC_PRINT(
1843                                         BTC_MSG_ALGORITHM,
1844                                         ALGO_TRACE_FW_DETAIL,
1845                                         ("[BTCoex], Increase wifi duration!!\n")
1846                                 );
1847                         }
1848                 } else if (retryCount <= 3) { /*  <=3 retry in the last 2-second duration */
1849                         up--;
1850                         dn++;
1851
1852                         if (up <= 0)
1853                                 up = 0;
1854
1855                         if (dn == 2) { /*  if 連續 2 個2秒 retry count< 3, 則調窄WiFi duration */
1856                                 if (WaitCount <= 2)
1857                                         m++; /*  避免一直在兩個level中來回 */
1858                                 else
1859                                         m = 1;
1860
1861                                 if (m >= 20) /* m 最大值 = 20 ' 最大120秒 recheck是否調整 WiFi duration. */
1862                                         m = 20;
1863
1864                                 n = 3*m;
1865                                 up = 0;
1866                                 dn = 0;
1867                                 WaitCount = 0;
1868                                 result = -1;
1869                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], Decrease wifi duration for retryCounter<3!!\n"));
1870                         }
1871                 } else { /* retry count > 3, 只要1次 retry count > 3, 則調窄WiFi duration */
1872                         if (WaitCount == 1)
1873                                 m++; /*  避免一直在兩個level中來回 */
1874                         else
1875                                 m = 1;
1876
1877                         if (m >= 20) /* m 最大值 = 20 ' 最大120秒 recheck是否調整 WiFi duration. */
1878                                 m = 20;
1879
1880                         n = 3*m;
1881                         up = 0;
1882                         dn = 0;
1883                         WaitCount = 0;
1884                         result = -1;
1885                         BTC_PRINT(
1886                                 BTC_MSG_ALGORITHM,
1887                                 ALGO_TRACE_FW_DETAIL,
1888                                 ("[BTCoex], Decrease wifi duration for retryCounter>3!!\n")
1889                         );
1890                 }
1891
1892                 if (result == -1) {
1893                         if (
1894                                 BT_INFO_8723B_1ANT_A2DP_BASIC_RATE(btInfoExt) &&
1895                                 ((pCoexDm->curPsTdma == 1) || (pCoexDm->curPsTdma == 2))
1896                         ) {
1897                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1898                                 pCoexDm->psTdmaDuAdjType = 9;
1899                         } else if (pCoexDm->curPsTdma == 1) {
1900                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
1901                                 pCoexDm->psTdmaDuAdjType = 2;
1902                         } else if (pCoexDm->curPsTdma == 2) {
1903                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1904                                 pCoexDm->psTdmaDuAdjType = 9;
1905                         } else if (pCoexDm->curPsTdma == 9) {
1906                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 11);
1907                                 pCoexDm->psTdmaDuAdjType = 11;
1908                         }
1909                 } else if (result == 1) {
1910                         if (
1911                                 BT_INFO_8723B_1ANT_A2DP_BASIC_RATE(btInfoExt) &&
1912                                 ((pCoexDm->curPsTdma == 1) || (pCoexDm->curPsTdma == 2))
1913                         ) {
1914                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1915                                 pCoexDm->psTdmaDuAdjType = 9;
1916                         } else if (pCoexDm->curPsTdma == 11) {
1917                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
1918                                 pCoexDm->psTdmaDuAdjType = 9;
1919                         } else if (pCoexDm->curPsTdma == 9) {
1920                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
1921                                 pCoexDm->psTdmaDuAdjType = 2;
1922                         } else if (pCoexDm->curPsTdma == 2) {
1923                                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 1);
1924                                 pCoexDm->psTdmaDuAdjType = 1;
1925                         }
1926                 } else {          /* no change */
1927                         BTC_PRINT(
1928                                 BTC_MSG_ALGORITHM,
1929                                 ALGO_TRACE_FW_DETAIL,
1930                                 (
1931                                         "[BTCoex], ********** TDMA(on, %d) **********\n",
1932                                         pCoexDm->curPsTdma
1933                                 )
1934                         );
1935                 }
1936
1937                 if (
1938                         pCoexDm->curPsTdma != 1 &&
1939                         pCoexDm->curPsTdma != 2 &&
1940                         pCoexDm->curPsTdma != 9 &&
1941                         pCoexDm->curPsTdma != 11
1942                 ) /*  recover to previous adjust type */
1943                         halbtc8723b1ant_PsTdma(
1944                                 pBtCoexist, NORMAL_EXEC, true, pCoexDm->psTdmaDuAdjType
1945                         );
1946         }
1947 }
1948
1949 static void halbtc8723b1ant_PsTdmaCheckForPowerSaveState(
1950         PBTC_COEXIST pBtCoexist, bool bNewPsState
1951 )
1952 {
1953         u8 lpsMode = 0x0;
1954
1955         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_LPS_MODE, &lpsMode);
1956
1957         if (lpsMode) {  /*  already under LPS state */
1958                 if (bNewPsState) {
1959                         /*  keep state under LPS, do nothing. */
1960                 } else /*  will leave LPS state, turn off psTdma first */
1961                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
1962         } else {                                                /*  NO PS state */
1963                 if (bNewPsState) /*  will enter LPS state, turn off psTdma first */
1964                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
1965                 else {
1966                         /*  keep state under NO PS state, do nothing. */
1967                 }
1968         }
1969 }
1970
1971 static void halbtc8723b1ant_PowerSaveState(
1972         PBTC_COEXIST pBtCoexist, u8 psType, u8 lpsVal, u8 rpwmVal
1973 )
1974 {
1975         bool bLowPwrDisable = false;
1976
1977         switch (psType) {
1978         case BTC_PS_WIFI_NATIVE:
1979                 /*  recover to original 32k low power setting */
1980                 bLowPwrDisable = false;
1981                 pBtCoexist->fBtcSet(
1982                         pBtCoexist, BTC_SET_ACT_DISABLE_LOW_POWER, &bLowPwrDisable
1983                 );
1984                 pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1985                 pCoexSta->bForceLpsOn = false;
1986                 break;
1987         case BTC_PS_LPS_ON:
1988                 halbtc8723b1ant_PsTdmaCheckForPowerSaveState(pBtCoexist, true);
1989                 halbtc8723b1ant_LpsRpwm(pBtCoexist, NORMAL_EXEC, lpsVal, rpwmVal);
1990                 /*  when coex force to enter LPS, do not enter 32k low power. */
1991                 bLowPwrDisable = true;
1992                 pBtCoexist->fBtcSet(
1993                         pBtCoexist, BTC_SET_ACT_DISABLE_LOW_POWER, &bLowPwrDisable
1994                 );
1995                 /*  power save must executed before psTdma. */
1996                 pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1997                 pCoexSta->bForceLpsOn = true;
1998                 break;
1999         case BTC_PS_LPS_OFF:
2000                 halbtc8723b1ant_PsTdmaCheckForPowerSaveState(pBtCoexist, false);
2001                 pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
2002                 pCoexSta->bForceLpsOn = false;
2003                 break;
2004         default:
2005                 break;
2006         }
2007 }
2008
2009 /*  */
2010 /*  */
2011 /*      Software Coex Mechanism start */
2012 /*  */
2013 /*  */
2014
2015 /*  */
2016 /*  */
2017 /*      Non-Software Coex Mechanism start */
2018 /*  */
2019 /*  */
2020 static void halbtc8723b1ant_ActionWifiMultiPort(PBTC_COEXIST pBtCoexist)
2021 {
2022         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2023
2024         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2025         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2026 }
2027
2028 static void halbtc8723b1ant_ActionHs(PBTC_COEXIST pBtCoexist)
2029 {
2030         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 5);
2031         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2032 }
2033
2034 static void halbtc8723b1ant_ActionBtInquiry(PBTC_COEXIST pBtCoexist)
2035 {
2036         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2037         bool bWifiConnected = false;
2038         bool bApEnable = false;
2039         bool bWifiBusy = false;
2040         bool bBtBusy = false;
2041
2042         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
2043         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2044         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2045         pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
2046
2047         if (!bWifiConnected && !pCoexSta->bWiFiIsHighPriTask) {
2048                 halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2049                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2050
2051                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2052         } else if (
2053                 pBtLinkInfo->bScoExist ||
2054                 pBtLinkInfo->bHidExist ||
2055                 pBtLinkInfo->bA2dpExist
2056         ) {
2057                 /*  SCO/HID/A2DP busy */
2058                 halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2059                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2060
2061                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2062         } else if (pBtLinkInfo->bPanExist || bWifiBusy) {
2063                 halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2064                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2065
2066                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2067         } else {
2068                 halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2069                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2070
2071                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2072         }
2073 }
2074
2075 static void halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2076         PBTC_COEXIST pBtCoexist, u8 wifiStatus
2077 )
2078 {
2079         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2080         bool bWifiConnected = false;
2081
2082         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
2083
2084         /*  tdma and coex table */
2085
2086         if (pBtLinkInfo->bScoExist) {
2087                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 5);
2088                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2089         } else { /* HID */
2090                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 6);
2091                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 5);
2092         }
2093 }
2094
2095 static void halbtc8723b1ant_ActionWifiConnectedBtAclBusy(
2096         PBTC_COEXIST pBtCoexist, u8 wifiStatus
2097 )
2098 {
2099         u8 btRssiState;
2100
2101         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2102         btRssiState = halbtc8723b1ant_BtRssiState(2, 28, 0);
2103
2104         if ((pCoexSta->lowPriorityRx >= 1000) && (pCoexSta->lowPriorityRx != 65535))
2105                 pBtLinkInfo->bSlaveRole = true;
2106         else
2107                 pBtLinkInfo->bSlaveRole = false;
2108
2109         if (pBtLinkInfo->bHidOnly) { /* HID */
2110                 halbtc8723b1ant_ActionBtScoHidOnlyBusy(pBtCoexist, wifiStatus);
2111                 pCoexDm->bAutoTdmaAdjust = false;
2112                 return;
2113         } else if (pBtLinkInfo->bA2dpOnly) { /* A2DP */
2114                 if (BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE == wifiStatus) {
2115                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2116                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2117                         pCoexDm->bAutoTdmaAdjust = false;
2118                 } else {
2119                         halbtc8723b1ant_TdmaDurationAdjustForAcl(pBtCoexist, wifiStatus);
2120                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2121                         pCoexDm->bAutoTdmaAdjust = true;
2122                 }
2123         } else if (pBtLinkInfo->bHidExist && pBtLinkInfo->bA2dpExist) { /* HID+A2DP */
2124                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 14);
2125                 pCoexDm->bAutoTdmaAdjust = false;
2126
2127                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2128         } else if (
2129                 pBtLinkInfo->bPanOnly ||
2130                 (pBtLinkInfo->bHidExist && pBtLinkInfo->bPanExist)
2131         ) { /* PAN(OPP, FTP), HID+PAN(OPP, FTP) */
2132                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 3);
2133                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2134                 pCoexDm->bAutoTdmaAdjust = false;
2135         } else if (
2136                 (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist) ||
2137                 (pBtLinkInfo->bHidExist && pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist)
2138         ) { /* A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP) */
2139                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 13);
2140                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2141                 pCoexDm->bAutoTdmaAdjust = false;
2142         } else {
2143                 /* BT no-profile busy (0x9) */
2144                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2145                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2146                 pCoexDm->bAutoTdmaAdjust = false;
2147         }
2148 }
2149
2150 static void halbtc8723b1ant_ActionWifiNotConnected(PBTC_COEXIST pBtCoexist)
2151 {
2152         /*  power save state */
2153         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2154
2155         /*  tdma and coex table */
2156         halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 8);
2157         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
2158 }
2159
2160 static void halbtc8723b1ant_ActionWifiNotConnectedScan(
2161         PBTC_COEXIST pBtCoexist
2162 )
2163 {
2164         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2165
2166         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2167
2168         /*  tdma and coex table */
2169         if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2170                 if (pBtLinkInfo->bA2dpExist) {
2171                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2172                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2173                 } else if (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist) {
2174                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 22);
2175                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2176                 } else {
2177                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2178                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2179                 }
2180         } else if (
2181                 (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2182                 (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2183         ) {
2184                 halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2185                         pBtCoexist, BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN
2186                 );
2187         } else {
2188                 /* Bryant Add */
2189                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2190                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2191         }
2192 }
2193
2194 static void halbtc8723b1ant_ActionWifiNotConnectedAssoAuth(
2195         PBTC_COEXIST pBtCoexist
2196 )
2197 {
2198         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2199
2200         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2201
2202         /*  tdma and coex table */
2203         if (
2204                 (pBtLinkInfo->bScoExist) ||
2205                 (pBtLinkInfo->bHidExist) ||
2206                 (pBtLinkInfo->bA2dpExist)
2207         ) {
2208                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2209                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2210         } else if (pBtLinkInfo->bPanExist) {
2211                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2212                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2213         } else {
2214                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2215                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2216         }
2217 }
2218
2219 static void halbtc8723b1ant_ActionWifiConnectedScan(PBTC_COEXIST pBtCoexist)
2220 {
2221         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2222
2223         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2224
2225         /*  tdma and coex table */
2226         if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2227                 if (pBtLinkInfo->bA2dpExist) {
2228                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2229                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2230                 } else if (pBtLinkInfo->bA2dpExist && pBtLinkInfo->bPanExist) {
2231                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 22);
2232                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2233                 } else {
2234                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2235                         halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2236                 }
2237         } else if (
2238                 (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2239                 (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2240         ) {
2241                 halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2242                         pBtCoexist, BT_8723B_1ANT_WIFI_STATUS_CONNECTED_SCAN
2243                 );
2244         } else {
2245                 /* Bryant Add */
2246                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2247                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2248         }
2249 }
2250
2251 static void halbtc8723b1ant_ActionWifiConnectedSpecialPacket(
2252         PBTC_COEXIST pBtCoexist
2253 )
2254 {
2255         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2256
2257         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2258
2259         /*  tdma and coex table */
2260         if (
2261                 (pBtLinkInfo->bScoExist) ||
2262                 (pBtLinkInfo->bHidExist) ||
2263                 (pBtLinkInfo->bA2dpExist)
2264         ) {
2265                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 32);
2266                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2267         } else if (pBtLinkInfo->bPanExist) {
2268                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 20);
2269                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 4);
2270         } else {
2271                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2272                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2273         }
2274 }
2275
2276 static void halbtc8723b1ant_ActionWifiConnected(PBTC_COEXIST pBtCoexist)
2277 {
2278         bool bWifiBusy = false;
2279         bool bScan = false, bLink = false, bRoam = false;
2280         bool bUnder4way = false, bApEnable = false;
2281
2282         BTC_PRINT(
2283                 BTC_MSG_ALGORITHM,
2284                 ALGO_TRACE,
2285                 ("[BTCoex], CoexForWifiConnect() ===>\n")
2286         );
2287
2288         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS, &bUnder4way);
2289         if (bUnder4way) {
2290                 halbtc8723b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
2291                 BTC_PRINT(
2292                         BTC_MSG_ALGORITHM,
2293                         ALGO_TRACE,
2294                         ("[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n")
2295                 );
2296                 return;
2297         }
2298
2299         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2300         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2301         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2302         if (bScan || bLink || bRoam) {
2303                 if (bScan)
2304                         halbtc8723b1ant_ActionWifiConnectedScan(pBtCoexist);
2305                 else
2306                         halbtc8723b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
2307                 BTC_PRINT(
2308                         BTC_MSG_ALGORITHM,
2309                         ALGO_TRACE,
2310                         ("[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n")
2311                 );
2312                 return;
2313         }
2314
2315         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
2316         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2317
2318         /*  power save state */
2319         if (
2320                 !bApEnable &&
2321                 BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus &&
2322                 !pBtCoexist->btLinkInfo.bHidOnly
2323         ) {
2324                 if (pBtCoexist->btLinkInfo.bA2dpOnly) { /* A2DP */
2325                         if (!bWifiBusy)
2326                                 halbtc8723b1ant_PowerSaveState(
2327                                         pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0
2328                                 );
2329                         else { /* busy */
2330                                 if  (pCoexSta->nScanAPNum >= BT_8723B_1ANT_WIFI_NOISY_THRESH)  /* no force LPS, no PS-TDMA, use pure TDMA */
2331                                         halbtc8723b1ant_PowerSaveState(
2332                                                 pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0
2333                                         );
2334                                 else
2335                                         halbtc8723b1ant_PowerSaveState(
2336                                                 pBtCoexist, BTC_PS_LPS_ON, 0x50, 0x4
2337                                         );
2338                         }
2339                 } else if (
2340                         (pCoexSta->bPanExist == false) &&
2341                         (pCoexSta->bA2dpExist == false) &&
2342                         (pCoexSta->bHidExist == false)
2343                 )
2344                         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2345                 else
2346                         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_LPS_ON, 0x50, 0x4);
2347         } else
2348                 halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2349
2350         /*  tdma and coex table */
2351         if (!bWifiBusy) {
2352                 if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2353                         halbtc8723b1ant_ActionWifiConnectedBtAclBusy(
2354                                 pBtCoexist,
2355                                 BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE
2356                         );
2357                 } else if (
2358                         (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2359                         (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2360                 ) {
2361                         halbtc8723b1ant_ActionBtScoHidOnlyBusy(pBtCoexist,
2362                                 BT_8723B_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2363                 } else {
2364                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2365
2366                         if ((pCoexSta->highPriorityTx) + (pCoexSta->highPriorityRx) <= 60)
2367                                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2368                         else
2369                                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2370                 }
2371         } else {
2372                 if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) {
2373                         halbtc8723b1ant_ActionWifiConnectedBtAclBusy(
2374                                 pBtCoexist,
2375                                 BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY
2376                         );
2377                 } else if (
2378                         (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2379                         (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2380                 ) {
2381                         halbtc8723b1ant_ActionBtScoHidOnlyBusy(
2382                                 pBtCoexist,
2383                                 BT_8723B_1ANT_WIFI_STATUS_CONNECTED_BUSY
2384                         );
2385                 } else {
2386                         halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 8);
2387
2388                         if ((pCoexSta->highPriorityTx) + (pCoexSta->highPriorityRx) <= 60)
2389                                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
2390                         else
2391                                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 7);
2392                 }
2393         }
2394 }
2395
2396 static void halbtc8723b1ant_RunSwCoexistMechanism(PBTC_COEXIST pBtCoexist)
2397 {
2398         u8 algorithm = 0;
2399
2400         algorithm = halbtc8723b1ant_ActionAlgorithm(pBtCoexist);
2401         pCoexDm->curAlgorithm = algorithm;
2402
2403         if (halbtc8723b1ant_IsCommonAction(pBtCoexist)) {
2404
2405         } else {
2406                 switch (pCoexDm->curAlgorithm) {
2407                 case BT_8723B_1ANT_COEX_ALGO_SCO:
2408                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = SCO.\n"));
2409                         /* halbtc8723b1ant_ActionSco(pBtCoexist); */
2410                         break;
2411                 case BT_8723B_1ANT_COEX_ALGO_HID:
2412                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HID.\n"));
2413                         /* halbtc8723b1ant_ActionHid(pBtCoexist); */
2414                         break;
2415                 case BT_8723B_1ANT_COEX_ALGO_A2DP:
2416                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = A2DP.\n"));
2417                         /* halbtc8723b1ant_ActionA2dp(pBtCoexist); */
2418                         break;
2419                 case BT_8723B_1ANT_COEX_ALGO_A2DP_PANHS:
2420                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = A2DP+PAN(HS).\n"));
2421                         /* halbtc8723b1ant_ActionA2dpPanHs(pBtCoexist); */
2422                         break;
2423                 case BT_8723B_1ANT_COEX_ALGO_PANEDR:
2424                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = PAN(EDR).\n"));
2425                         /* halbtc8723b1ant_ActionPanEdr(pBtCoexist); */
2426                         break;
2427                 case BT_8723B_1ANT_COEX_ALGO_PANHS:
2428                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HS mode.\n"));
2429                         /* halbtc8723b1ant_ActionPanHs(pBtCoexist); */
2430                         break;
2431                 case BT_8723B_1ANT_COEX_ALGO_PANEDR_A2DP:
2432                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = PAN+A2DP.\n"));
2433                         /* halbtc8723b1ant_ActionPanEdrA2dp(pBtCoexist); */
2434                         break;
2435                 case BT_8723B_1ANT_COEX_ALGO_PANEDR_HID:
2436                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = PAN(EDR)+HID.\n"));
2437                         /* halbtc8723b1ant_ActionPanEdrHid(pBtCoexist); */
2438                         break;
2439                 case BT_8723B_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2440                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HID+A2DP+PAN.\n"));
2441                         /* halbtc8723b1ant_ActionHidA2dpPanEdr(pBtCoexist); */
2442                         break;
2443                 case BT_8723B_1ANT_COEX_ALGO_HID_A2DP:
2444                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = HID+A2DP.\n"));
2445                         /* halbtc8723b1ant_ActionHidA2dp(pBtCoexist); */
2446                         break;
2447                 default:
2448                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Action algorithm = coexist All Off!!\n"));
2449                         break;
2450                 }
2451                 pCoexDm->preAlgorithm = pCoexDm->curAlgorithm;
2452         }
2453 }
2454
2455 static void halbtc8723b1ant_RunCoexistMechanism(PBTC_COEXIST pBtCoexist)
2456 {
2457         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2458         bool bWifiConnected = false, bBtHsOn = false;
2459         bool bIncreaseScanDevNum = false;
2460         bool bBtCtrlAggBufSize = false;
2461         u8 aggBufSize = 5;
2462         u32 wifiLinkStatus = 0;
2463         u32 numOfWifiLink = 0;
2464
2465         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], RunCoexistMechanism() ===>\n"));
2466
2467         if (pBtCoexist->bManualControl) {
2468                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n"));
2469                 return;
2470         }
2471
2472         if (pBtCoexist->bStopCoexDm) {
2473                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n"));
2474                 return;
2475         }
2476
2477         if (pCoexSta->bUnderIps) {
2478                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], wifi is under IPS !!!\n"));
2479                 return;
2480         }
2481
2482         if (
2483                 (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) ||
2484                 (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
2485                 (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
2486         ){
2487                 bIncreaseScanDevNum = true;
2488         }
2489
2490         pBtCoexist->fBtcSet(
2491                 pBtCoexist,
2492                 BTC_SET_BL_INC_SCAN_DEV_NUM,
2493                 &bIncreaseScanDevNum
2494         );
2495         pBtCoexist->fBtcGet(
2496                 pBtCoexist,
2497                 BTC_GET_BL_WIFI_CONNECTED,
2498                 &bWifiConnected
2499         );
2500
2501         pBtCoexist->fBtcGet(
2502                 pBtCoexist,
2503                 BTC_GET_U4_WIFI_LINK_STATUS,
2504                 &wifiLinkStatus
2505         );
2506         numOfWifiLink = wifiLinkStatus>>16;
2507
2508         if ((numOfWifiLink >= 2) || (wifiLinkStatus&WIFI_P2P_GO_CONNECTED)) {
2509                 BTC_PRINT(
2510                         BTC_MSG_INTERFACE,
2511                         INTF_NOTIFY,
2512                         (
2513                                 "############# [BTCoex],  Multi-Port numOfWifiLink = %d, wifiLinkStatus = 0x%x\n",
2514                                 numOfWifiLink,
2515                                 wifiLinkStatus
2516                         )
2517                 );
2518                 halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
2519                 halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize);
2520
2521                 if ((pBtLinkInfo->bA2dpExist) && (pCoexSta->bC2hBtInquiryPage)) {
2522                         BTC_PRINT(
2523                                 BTC_MSG_INTERFACE,
2524                                 INTF_NOTIFY,
2525                                 ("############# [BTCoex],  BT Is Inquirying\n")
2526                         );
2527                         halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
2528                 } else
2529                         halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
2530
2531                 return;
2532         }
2533
2534         if ((pBtLinkInfo->bBtLinkExist) && (bWifiConnected)) {
2535                 halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 1, 1, 0, 1);
2536
2537                 if (pBtLinkInfo->bScoExist)
2538                         halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, true, 0x5);
2539                 else
2540                         halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, true, 0x8);
2541
2542                 halbtc8723b1ant_SwMechanism(pBtCoexist, true);
2543                 halbtc8723b1ant_RunSwCoexistMechanism(pBtCoexist);  /* just print debug message */
2544         } else {
2545                 halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
2546
2547                 halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, false, 0x5);
2548
2549                 halbtc8723b1ant_SwMechanism(pBtCoexist, false);
2550                 halbtc8723b1ant_RunSwCoexistMechanism(pBtCoexist); /* just print debug message */
2551         }
2552
2553         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
2554         if (pCoexSta->bC2hBtInquiryPage) {
2555                 BTC_PRINT(
2556                         BTC_MSG_INTERFACE,
2557                         INTF_NOTIFY,
2558                         ("############# [BTCoex],  BT Is Inquirying\n")
2559                 );
2560                 halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
2561                 return;
2562         } else if (bBtHsOn) {
2563                 halbtc8723b1ant_ActionHs(pBtCoexist);
2564                 return;
2565         }
2566
2567
2568         if (!bWifiConnected) {
2569                 bool bScan = false, bLink = false, bRoam = false;
2570
2571                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], wifi is non connected-idle !!!\n"));
2572
2573                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2574                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2575                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2576
2577                 if (bScan || bLink || bRoam) {
2578                          if (bScan)
2579                                 halbtc8723b1ant_ActionWifiNotConnectedScan(pBtCoexist);
2580                          else
2581                                 halbtc8723b1ant_ActionWifiNotConnectedAssoAuth(pBtCoexist);
2582                 } else
2583                         halbtc8723b1ant_ActionWifiNotConnected(pBtCoexist);
2584         } else /*  wifi LPS/Busy */
2585                 halbtc8723b1ant_ActionWifiConnected(pBtCoexist);
2586 }
2587
2588 static void halbtc8723b1ant_InitCoexDm(PBTC_COEXIST pBtCoexist)
2589 {
2590         /*  force to reset coex mechanism */
2591
2592         /*  sw all off */
2593         halbtc8723b1ant_SwMechanism(pBtCoexist, false);
2594
2595         /* halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 8); */
2596         halbtc8723b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
2597
2598         pCoexSta->popEventCnt = 0;
2599 }
2600
2601 static void halbtc8723b1ant_InitHwConfig(
2602         PBTC_COEXIST pBtCoexist,
2603         bool bBackUp,
2604         bool bWifiOnly
2605 )
2606 {
2607         u32 u4Tmp = 0;/*  fwVer; */
2608         u8 u1Tmpa = 0, u1Tmpb = 0;
2609
2610         BTC_PRINT(
2611                 BTC_MSG_INTERFACE,
2612                 INTF_INIT,
2613                 ("[BTCoex], 1Ant Init HW Config!!\n")
2614         );
2615
2616         pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x550, 0x8, 0x1);  /* enable TBTT nterrupt */
2617
2618         /*  0x790[5:0]= 0x5 */
2619         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x790, 0x5);
2620
2621         /*  Enable counter statistics */
2622         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x778, 0x1);
2623         pBtCoexist->fBtcWrite1ByteBitMask(pBtCoexist, 0x40, 0x20, 0x1);
2624
2625         /* Antenna config */
2626         if (bWifiOnly) {
2627                 halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_WIFI, true, false);
2628                 halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 9);
2629         } else
2630                 halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, true, false);
2631
2632         /*  PTA parameter */
2633         halbtc8723b1ant_CoexTableWithType(pBtCoexist, FORCE_EXEC, 0);
2634
2635         u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
2636         u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
2637         u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
2638
2639         BTC_PRINT(
2640                 BTC_MSG_INTERFACE,
2641                 INTF_NOTIFY,
2642                 (
2643                         "############# [BTCoex], 0x948 = 0x%x, 0x765 = 0x%x, 0x67 = 0x%x\n",
2644                         u4Tmp,
2645                         u1Tmpa,
2646                         u1Tmpb
2647                 )
2648         );
2649 }
2650
2651 /*  */
2652 /*  work around function start with wa_halbtc8723b1ant_ */
2653 /*  */
2654 /*  */
2655 /*  extern function start with EXhalbtc8723b1ant_ */
2656 /*  */
2657 void EXhalbtc8723b1ant_PowerOnSetting(PBTC_COEXIST pBtCoexist)
2658 {
2659         PBTC_BOARD_INFO pBoardInfo = &pBtCoexist->boardInfo;
2660         u8 u1Tmp = 0x0;
2661         u16 u2Tmp = 0x0;
2662
2663         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x67, 0x20);
2664
2665         /*  enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly. */
2666         u2Tmp = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x2);
2667         pBtCoexist->fBtcWrite2Byte(pBtCoexist, 0x2, u2Tmp|BIT0|BIT1);
2668
2669         /*  set GRAN_BT = 1 */
2670         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x765, 0x18);
2671         /*  set WLAN_ACT = 0 */
2672         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x76e, 0x4);
2673
2674         /*  */
2675         /*  S0 or S1 setting and Local register setting(By the setting fw can get ant number, S0/S1, ... info) */
2676         /*  Local setting bit define */
2677         /*      BIT0: "0" for no antenna inverse; "1" for antenna inverse */
2678         /*      BIT1: "0" for internal switch; "1" for external switch */
2679         /*      BIT2: "0" for one antenna; "1" for two antenna */
2680         /*  NOTE: here default all internal switch and 1-antenna ==> BIT1 = 0 and BIT2 = 0 */
2681         if (pBtCoexist->chipInterface == BTC_INTF_USB) {
2682                 /*  fixed at S0 for USB interface */
2683                 pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
2684
2685                 u1Tmp |= 0x1;   /*  antenna inverse */
2686                 pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0xfe08, u1Tmp);
2687
2688                 pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_AUX_PORT;
2689         } else {
2690                 /*  for PCIE and SDIO interface, we check efuse 0xc3[6] */
2691                 if (pBoardInfo->singleAntPath == 0) {
2692                         /*  set to S1 */
2693                         pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x280);
2694                         pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT;
2695                 } else if (pBoardInfo->singleAntPath == 1) {
2696                         /*  set to S0 */
2697                         pBtCoexist->fBtcWrite4Byte(pBtCoexist, 0x948, 0x0);
2698                         u1Tmp |= 0x1;   /*  antenna inverse */
2699                         pBoardInfo->btdmAntPos = BTC_ANTENNA_AT_AUX_PORT;
2700                 }
2701
2702                 if (pBtCoexist->chipInterface == BTC_INTF_PCI)
2703                         pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0x384, u1Tmp);
2704                 else if (pBtCoexist->chipInterface == BTC_INTF_SDIO)
2705                         pBtCoexist->fBtcWriteLocalReg1Byte(pBtCoexist, 0x60, u1Tmp);
2706         }
2707 }
2708
2709 void EXhalbtc8723b1ant_InitHwConfig(PBTC_COEXIST pBtCoexist, bool bWifiOnly)
2710 {
2711         halbtc8723b1ant_InitHwConfig(pBtCoexist, true, bWifiOnly);
2712 }
2713
2714 void EXhalbtc8723b1ant_InitCoexDm(PBTC_COEXIST pBtCoexist)
2715 {
2716         BTC_PRINT(
2717                 BTC_MSG_INTERFACE,
2718                 INTF_INIT,
2719                 ("[BTCoex], Coex Mechanism Init!!\n")
2720         );
2721
2722         pBtCoexist->bStopCoexDm = false;
2723
2724         halbtc8723b1ant_InitCoexDm(pBtCoexist);
2725
2726         halbtc8723b1ant_QueryBtInfo(pBtCoexist);
2727 }
2728
2729 void EXhalbtc8723b1ant_DisplayCoexInfo(PBTC_COEXIST pBtCoexist)
2730 {
2731         PBTC_BOARD_INFO pBoardInfo = &pBtCoexist->boardInfo;
2732         PBTC_STACK_INFO pStackInfo = &pBtCoexist->stackInfo;
2733         PBTC_BT_LINK_INFO pBtLinkInfo = &pBtCoexist->btLinkInfo;
2734         u8 *cliBuf = pBtCoexist->cliBuf;
2735         u8 u1Tmp[4], i, btInfoExt, psTdmaCase = 0;
2736         u16 u2Tmp[4];
2737         u32 u4Tmp[4];
2738         bool bRoam = false;
2739         bool bScan = false;
2740         bool bLink = false;
2741         bool bWifiUnder5G = false;
2742         bool bWifiUnderBMode = false;
2743         bool bBtHsOn = false;
2744         bool bWifiBusy = false;
2745         s32 wifiRssi = 0, btHsRssi = 0;
2746         u32 wifiBw, wifiTrafficDir, faOfdm, faCck, wifiLinkStatus;
2747         u8 wifiDot11Chnl, wifiHsChnl;
2748         u32 fwVer = 0, btPatchVer = 0;
2749         static u8 PopReportIn10s;
2750
2751         CL_SPRINTF(
2752                 cliBuf,
2753                 BT_TMP_BUF_SIZE,
2754                 "\r\n ============[BT Coexist info]============"
2755         );
2756         CL_PRINTF(cliBuf);
2757
2758         if (pBtCoexist->bManualControl) {
2759                 CL_SPRINTF(
2760                         cliBuf,
2761                         BT_TMP_BUF_SIZE,
2762                         "\r\n ============[Under Manual Control]============"
2763                 );
2764                 CL_PRINTF(cliBuf);
2765                 CL_SPRINTF(cliBuf,
2766                         BT_TMP_BUF_SIZE,
2767                         "\r\n =========================================="
2768                 );
2769                 CL_PRINTF(cliBuf);
2770         }
2771         if (pBtCoexist->bStopCoexDm) {
2772                 CL_SPRINTF(
2773                         cliBuf,
2774                         BT_TMP_BUF_SIZE,
2775                         "\r\n ============[Coex is STOPPED]============"
2776                 );
2777                 CL_PRINTF(cliBuf);
2778                 CL_SPRINTF(
2779                         cliBuf,
2780                         BT_TMP_BUF_SIZE,
2781                         "\r\n =========================================="
2782                 );
2783                 CL_PRINTF(cliBuf);
2784         }
2785
2786         CL_SPRINTF(
2787                 cliBuf,
2788                 BT_TMP_BUF_SIZE,
2789                 "\r\n %-35s = %d/ %d/ %d", "Ant PG Num/ Ant Mech/ Ant Pos:", \
2790                 pBoardInfo->pgAntNum,
2791                 pBoardInfo->btdmAntNum,
2792                 pBoardInfo->btdmAntPos
2793         );
2794         CL_PRINTF(cliBuf);
2795
2796         CL_SPRINTF(
2797                 cliBuf,
2798                 BT_TMP_BUF_SIZE,
2799                 "\r\n %-35s = %s / %d", "BT stack/ hci ext ver", \
2800                 ((pStackInfo->bProfileNotified) ? "Yes" : "No"),
2801                 pStackInfo->hciVersion
2802         );
2803         CL_PRINTF(cliBuf);
2804
2805         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_BT_PATCH_VER, &btPatchVer);
2806         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);
2807         CL_SPRINTF(
2808                 cliBuf,
2809                 BT_TMP_BUF_SIZE,
2810                 "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)", "CoexVer/ FwVer/ PatchVer", \
2811                 GLCoexVerDate8723b1Ant,
2812                 GLCoexVer8723b1Ant,
2813                 fwVer,
2814                 btPatchVer,
2815                 btPatchVer
2816         );
2817         CL_PRINTF(cliBuf);
2818
2819         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
2820         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_DOT11_CHNL, &wifiDot11Chnl);
2821         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifiHsChnl);
2822         CL_SPRINTF(
2823                 cliBuf,
2824                 BT_TMP_BUF_SIZE,
2825                 "\r\n %-35s = %d / %d(%d)", "Dot11 channel / HsChnl(HsMode)", \
2826                 wifiDot11Chnl,
2827                 wifiHsChnl,
2828                 bBtHsOn
2829         );
2830         CL_PRINTF(cliBuf);
2831
2832         CL_SPRINTF(
2833                 cliBuf,
2834                 BT_TMP_BUF_SIZE,
2835                 "\r\n %-35s = %02x %02x %02x ", "H2C Wifi inform bt chnl Info", \
2836                 pCoexDm->wifiChnlInfo[0],
2837                 pCoexDm->wifiChnlInfo[1],
2838                 pCoexDm->wifiChnlInfo[2]
2839         );
2840         CL_PRINTF(cliBuf);
2841
2842         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi);
2843         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_S4_HS_RSSI, &btHsRssi);
2844         CL_SPRINTF(
2845                 cliBuf,
2846                 BT_TMP_BUF_SIZE,
2847                 "\r\n %-35s = %d/ %d", "Wifi rssi/ HS rssi", \
2848                 wifiRssi-100, btHsRssi-100
2849         );
2850         CL_PRINTF(cliBuf);
2851
2852         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
2853         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
2854         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
2855         CL_SPRINTF(
2856                 cliBuf,
2857                 BT_TMP_BUF_SIZE,
2858                 "\r\n %-35s = %d/ %d/ %d/ %s", "Wifi bLink/ bRoam/ bScan/ bHi-Pri", \
2859                 bLink, bRoam, bScan, ((pCoexSta->bWiFiIsHighPriTask) ? "1" : "0")
2860         );
2861         CL_PRINTF(cliBuf);
2862
2863         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_5G, &bWifiUnder5G);
2864         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
2865         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
2866         pBtCoexist->fBtcGet(
2867                 pBtCoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION, &wifiTrafficDir
2868         );
2869         pBtCoexist->fBtcGet(
2870                 pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode
2871         );
2872
2873         CL_SPRINTF(
2874                 cliBuf,
2875                 BT_TMP_BUF_SIZE,
2876                 "\r\n %-35s = %s / %s/ %s/ AP =%d/ %s ", "Wifi status", \
2877                 (bWifiUnder5G ? "5G" : "2.4G"),
2878                 ((bWifiUnderBMode) ? "11b" : ((BTC_WIFI_BW_LEGACY == wifiBw) ? "11bg" : (((BTC_WIFI_BW_HT40 == wifiBw) ? "HT40" : "HT20")))),
2879                 ((!bWifiBusy) ? "idle" : ((BTC_WIFI_TRAFFIC_TX == wifiTrafficDir) ? "uplink" : "downlink")),
2880                 pCoexSta->nScanAPNum,
2881                 (pCoexSta->bCCKLock) ? "Lock" : "noLock"
2882         );
2883         CL_PRINTF(cliBuf);
2884
2885         pBtCoexist->fBtcGet(
2886                 pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus
2887         );
2888         CL_SPRINTF(
2889                 cliBuf,
2890                 BT_TMP_BUF_SIZE,
2891                 "\r\n %-35s = %d/ %d/ %d/ %d/ %d", "sta/vwifi/hs/p2pGo/p2pGc", \
2892                 ((wifiLinkStatus&WIFI_STA_CONNECTED) ? 1 : 0),
2893                 ((wifiLinkStatus&WIFI_AP_CONNECTED) ? 1 : 0),
2894                 ((wifiLinkStatus&WIFI_HS_CONNECTED) ? 1 : 0),
2895                 ((wifiLinkStatus&WIFI_P2P_GO_CONNECTED) ? 1 : 0),
2896                 ((wifiLinkStatus&WIFI_P2P_GC_CONNECTED) ? 1 : 0)
2897         );
2898         CL_PRINTF(cliBuf);
2899
2900
2901         PopReportIn10s++;
2902         CL_SPRINTF(
2903                 cliBuf,
2904                 BT_TMP_BUF_SIZE,
2905                 "\r\n %-35s = [%s/ %d/ %d/ %d] ", "BT [status/ rssi/ retryCnt/ popCnt]", \
2906                 ((pBtCoexist->btInfo.bBtDisabled) ? ("disabled") : ((pCoexSta->bC2hBtInquiryPage) ? ("inquiry/page scan") : ((BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE == pCoexDm->btStatus) ? "non-connected idle" :
2907                 ((BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus) ? "connected-idle" : "busy")))),
2908                 pCoexSta->btRssi, pCoexSta->btRetryCnt, pCoexSta->popEventCnt
2909         );
2910         CL_PRINTF(cliBuf);
2911
2912         if (PopReportIn10s >= 5) {
2913                 pCoexSta->popEventCnt = 0;
2914                 PopReportIn10s = 0;
2915         }
2916
2917
2918         CL_SPRINTF(
2919                 cliBuf,
2920                 BT_TMP_BUF_SIZE,
2921                 "\r\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP", \
2922                 pBtLinkInfo->bScoExist,
2923                 pBtLinkInfo->bHidExist,
2924                 pBtLinkInfo->bPanExist,
2925                 pBtLinkInfo->bA2dpExist
2926         );
2927         CL_PRINTF(cliBuf);
2928
2929         if (pStackInfo->bProfileNotified) {
2930                 pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_BT_LINK_INFO);
2931         } else {
2932                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "BT Role", \
2933                 (pBtLinkInfo->bSlaveRole) ? "Slave" : "Master");
2934                 CL_PRINTF(cliBuf);
2935         }
2936
2937
2938         btInfoExt = pCoexSta->btInfoExt;
2939         CL_SPRINTF(
2940                 cliBuf,
2941                 BT_TMP_BUF_SIZE,
2942                 "\r\n %-35s = %s", "BT Info A2DP rate", \
2943                 (btInfoExt&BIT0) ? "Basic rate" : "EDR rate"
2944         );
2945         CL_PRINTF(cliBuf);
2946
2947         for (i = 0; i < BT_INFO_SRC_8723B_1ANT_MAX; i++) {
2948                 if (pCoexSta->btInfoC2hCnt[i]) {
2949                         CL_SPRINTF(
2950                                 cliBuf,
2951                                 BT_TMP_BUF_SIZE,
2952                                 "\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)", GLBtInfoSrc8723b1Ant[i], \
2953                                 pCoexSta->btInfoC2h[i][0], pCoexSta->btInfoC2h[i][1],
2954                                 pCoexSta->btInfoC2h[i][2], pCoexSta->btInfoC2h[i][3],
2955                                 pCoexSta->btInfoC2h[i][4], pCoexSta->btInfoC2h[i][5],
2956                                 pCoexSta->btInfoC2h[i][6], pCoexSta->btInfoC2hCnt[i]
2957                         );
2958                         CL_PRINTF(cliBuf);
2959                 }
2960         }
2961         CL_SPRINTF(
2962                 cliBuf,
2963                 BT_TMP_BUF_SIZE,
2964                 "\r\n %-35s = %s/%s, (0x%x/0x%x)", "PS state, IPS/LPS, (lps/rpwm)", \
2965                 (pCoexSta->bUnderIps ? "IPS ON" : "IPS OFF"),
2966                 (pCoexSta->bUnderLps ? "LPS ON" : "LPS OFF"),
2967                 pBtCoexist->btInfo.lpsVal,
2968                 pBtCoexist->btInfo.rpwmVal
2969         );
2970         CL_PRINTF(cliBuf);
2971         pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
2972
2973         if (!pBtCoexist->bManualControl) {
2974                 /*  Sw mechanism */
2975                 CL_SPRINTF(
2976                         cliBuf,
2977                         BT_TMP_BUF_SIZE,
2978                         "\r\n %-35s", "============[Sw mechanism]============"
2979                 );
2980                 CL_PRINTF(cliBuf);
2981
2982                 CL_SPRINTF(
2983                         cliBuf,
2984                         BT_TMP_BUF_SIZE,
2985                         "\r\n %-35s = %d", "SM[LowPenaltyRA]", \
2986                         pCoexDm->bCurLowPenaltyRa
2987                 );
2988                 CL_PRINTF(cliBuf);
2989
2990                 CL_SPRINTF(
2991                         cliBuf,
2992                         BT_TMP_BUF_SIZE,
2993                         "\r\n %-35s = %s/ %s/ %d ", "DelBA/ BtCtrlAgg/ AggSize", \
2994                         (pBtCoexist->btInfo.bRejectAggPkt ? "Yes" : "No"),
2995                         (pBtCoexist->btInfo.bBtCtrlAggBufSize ? "Yes" : "No"),
2996                         pBtCoexist->btInfo.aggBufSize
2997                 );
2998                 CL_PRINTF(cliBuf);
2999                 CL_SPRINTF(
3000                         cliBuf,
3001                         BT_TMP_BUF_SIZE,
3002                         "\r\n %-35s = 0x%x ", "Rate Mask", \
3003                         pBtCoexist->btInfo.raMask
3004                 );
3005                 CL_PRINTF(cliBuf);
3006
3007                 /*  Fw mechanism */
3008                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Fw mechanism]============");
3009                 CL_PRINTF(cliBuf);
3010
3011                 psTdmaCase = pCoexDm->curPsTdma;
3012                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x case-%d (auto:%d)", "PS TDMA", \
3013                         pCoexDm->psTdmaPara[0], pCoexDm->psTdmaPara[1],
3014                         pCoexDm->psTdmaPara[2], pCoexDm->psTdmaPara[3],
3015                         pCoexDm->psTdmaPara[4], psTdmaCase, pCoexDm->bAutoTdmaAdjust);
3016                 CL_PRINTF(cliBuf);
3017
3018                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "Coex Table Type", \
3019                         pCoexSta->nCoexTableType);
3020                 CL_PRINTF(cliBuf);
3021
3022                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "IgnWlanAct", \
3023                         pCoexDm->bCurIgnoreWlanAct);
3024                 CL_PRINTF(cliBuf);
3025
3026                 /*
3027                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x ", "Latest error condition(should be 0)", \
3028                         pCoexDm->errorCondition);
3029                 CL_PRINTF(cliBuf);
3030                 */
3031         }
3032
3033         /*  Hw setting */
3034         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Hw setting]============");
3035         CL_PRINTF(cliBuf);
3036
3037         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x", "backup ARFR1/ARFR2/RL/AMaxTime", \
3038                 pCoexDm->backupArfrCnt1, pCoexDm->backupArfrCnt2, pCoexDm->backupRetryLimit, pCoexDm->backupAmpduMaxTime);
3039         CL_PRINTF(cliBuf);
3040
3041         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
3042         u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
3043         u2Tmp[0] = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
3044         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
3045         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x", "0x430/0x434/0x42a/0x456", \
3046                 u4Tmp[0], u4Tmp[1], u2Tmp[0], u1Tmp[0]);
3047         CL_PRINTF(cliBuf);
3048
3049         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x778);
3050         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6cc);
3051         u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x880);
3052         CL_SPRINTF(
3053                 cliBuf, BT_TMP_BUF_SIZE,
3054                 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x778/0x6cc/0x880[29:25]", \
3055                 u1Tmp[0], u4Tmp[0],  (u4Tmp[1]&0x3e000000) >> 25
3056         );
3057         CL_PRINTF(cliBuf);
3058
3059         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
3060         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
3061         u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x764);
3062         u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x76e);
3063         CL_SPRINTF(
3064                 cliBuf,
3065                 BT_TMP_BUF_SIZE,
3066                 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x948/ 0x67[5] / 0x764 / 0x76e", \
3067                 u4Tmp[0], ((u1Tmp[0]&0x20) >> 5), (u4Tmp[1] & 0xffff), u1Tmp[1]
3068         );
3069         CL_PRINTF(cliBuf);
3070
3071         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x92c);
3072         u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x930);
3073         u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x944);
3074         CL_SPRINTF(
3075                 cliBuf,
3076                 BT_TMP_BUF_SIZE,
3077                 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x92c[1:0]/ 0x930[7:0]/0x944[1:0]", \
3078                 u4Tmp[0]&0x3, u4Tmp[1]&0xff, u4Tmp[2]&0x3
3079         );
3080         CL_PRINTF(cliBuf);
3081
3082         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x39);
3083         u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x40);
3084         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x4c);
3085         u1Tmp[2] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x64);
3086         CL_SPRINTF(
3087                 cliBuf,
3088                 BT_TMP_BUF_SIZE,
3089                 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x38[11]/0x40/0x4c[24:23]/0x64[0]", \
3090                 ((u1Tmp[0] & 0x8)>>3),
3091                 u1Tmp[1],
3092                 ((u4Tmp[0]&0x01800000)>>23),
3093                 u1Tmp[2]&0x1
3094         );
3095         CL_PRINTF(cliBuf);
3096
3097         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x550);
3098         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x522);
3099         CL_SPRINTF(
3100                 cliBuf,
3101                 BT_TMP_BUF_SIZE,
3102                 "\r\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522", \
3103                 u4Tmp[0], u1Tmp[0]
3104         );
3105         CL_PRINTF(cliBuf);
3106
3107         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xc50);
3108         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x49c);
3109         CL_SPRINTF(
3110                 cliBuf,
3111                 BT_TMP_BUF_SIZE,
3112                 "\r\n %-35s = 0x%x/ 0x%x", "0xc50(dig)/0x49c(null-drop)", \
3113                 u4Tmp[0]&0xff, u1Tmp[0]
3114         );
3115         CL_PRINTF(cliBuf);
3116
3117         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda0);
3118         u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda4);
3119         u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xda8);
3120         u4Tmp[3] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0xcf0);
3121
3122         u1Tmp[0] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0xa5b);
3123         u1Tmp[1] = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0xa5c);
3124
3125         faOfdm =
3126                 ((u4Tmp[0]&0xffff0000) >> 16) +
3127                 ((u4Tmp[1]&0xffff0000) >> 16) +
3128                 (u4Tmp[1] & 0xffff) +  (u4Tmp[2] & 0xffff) + \
3129                 ((u4Tmp[3]&0xffff0000) >> 16) + (u4Tmp[3] & 0xffff);
3130         faCck = (u1Tmp[0] << 8) + u1Tmp[1];
3131
3132         CL_SPRINTF(
3133                 cliBuf,
3134                 BT_TMP_BUF_SIZE,
3135                 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "OFDM-CCA/OFDM-FA/CCK-FA", \
3136                 u4Tmp[0]&0xffff, faOfdm, faCck
3137         );
3138         CL_PRINTF(cliBuf);
3139
3140
3141         CL_SPRINTF(
3142                 cliBuf,
3143                 BT_TMP_BUF_SIZE,
3144                 "\r\n %-35s = %d/ %d/ %d/ %d", "CRC_OK CCK/11g/11n/11n-Agg", \
3145                 pCoexSta->nCRCOK_CCK,
3146                 pCoexSta->nCRCOK_11g,
3147                 pCoexSta->nCRCOK_11n,
3148                 pCoexSta->nCRCOK_11nAgg
3149         );
3150         CL_PRINTF(cliBuf);
3151
3152         CL_SPRINTF(
3153                 cliBuf,
3154                 BT_TMP_BUF_SIZE,
3155                 "\r\n %-35s = %d/ %d/ %d/ %d", "CRC_Err CCK/11g/11n/11n-Agg", \
3156                 pCoexSta->nCRCErr_CCK,
3157                 pCoexSta->nCRCErr_11g,
3158                 pCoexSta->nCRCErr_11n,
3159                 pCoexSta->nCRCErr_11nAgg
3160         );
3161         CL_PRINTF(cliBuf);
3162
3163         u4Tmp[0] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c0);
3164         u4Tmp[1] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c4);
3165         u4Tmp[2] = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x6c8);
3166         CL_SPRINTF(
3167                 cliBuf,
3168                 BT_TMP_BUF_SIZE,
3169                 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x6c0/0x6c4/0x6c8(coexTable)", \
3170                 u4Tmp[0], u4Tmp[1], u4Tmp[2]);
3171         CL_PRINTF(cliBuf);
3172
3173         CL_SPRINTF(
3174                 cliBuf,
3175                 BT_TMP_BUF_SIZE,
3176                 "\r\n %-35s = %d/ %d", "0x770(high-pri rx/tx)", \
3177                 pCoexSta->highPriorityRx, pCoexSta->highPriorityTx
3178         );
3179         CL_PRINTF(cliBuf);
3180         CL_SPRINTF(
3181                 cliBuf,
3182                 BT_TMP_BUF_SIZE,
3183                 "\r\n %-35s = %d/ %d", "0x774(low-pri rx/tx)", \
3184                 pCoexSta->lowPriorityRx, pCoexSta->lowPriorityTx
3185         );
3186         CL_PRINTF(cliBuf);
3187
3188         pBtCoexist->fBtcDispDbgMsg(pBtCoexist, BTC_DBG_DISP_COEX_STATISTICS);
3189 }
3190
3191
3192 void EXhalbtc8723b1ant_IpsNotify(PBTC_COEXIST pBtCoexist, u8 type)
3193 {
3194         if (pBtCoexist->bManualControl ||       pBtCoexist->bStopCoexDm)
3195                 return;
3196
3197         if (BTC_IPS_ENTER == type) {
3198                 BTC_PRINT(
3199                         BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], IPS ENTER notify\n")
3200                 );
3201                 pCoexSta->bUnderIps = true;
3202
3203                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
3204                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 0);
3205                 halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, false, true);
3206         } else if (BTC_IPS_LEAVE == type) {
3207                 BTC_PRINT(
3208                         BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], IPS LEAVE notify\n")
3209                 );
3210                 pCoexSta->bUnderIps = false;
3211
3212                 halbtc8723b1ant_InitHwConfig(pBtCoexist, false, false);
3213                 halbtc8723b1ant_InitCoexDm(pBtCoexist);
3214                 halbtc8723b1ant_QueryBtInfo(pBtCoexist);
3215         }
3216 }
3217
3218 void EXhalbtc8723b1ant_LpsNotify(PBTC_COEXIST pBtCoexist, u8 type)
3219 {
3220         if (pBtCoexist->bManualControl || pBtCoexist->bStopCoexDm)
3221                 return;
3222
3223         if (BTC_LPS_ENABLE == type) {
3224                 BTC_PRINT(
3225                         BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], LPS ENABLE notify\n")
3226                 );
3227                 pCoexSta->bUnderLps = true;
3228         } else if (BTC_LPS_DISABLE == type) {
3229                 BTC_PRINT(
3230                         BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], LPS DISABLE notify\n")
3231                 );
3232                 pCoexSta->bUnderLps = false;
3233         }
3234 }
3235
3236 void EXhalbtc8723b1ant_ScanNotify(PBTC_COEXIST pBtCoexist, u8 type)
3237 {
3238         bool bWifiConnected = false, bBtHsOn = false;
3239         u32 wifiLinkStatus = 0;
3240         u32 numOfWifiLink = 0;
3241         bool bBtCtrlAggBufSize = false;
3242         u8 aggBufSize = 5;
3243
3244         u8 u1Tmpa, u1Tmpb;
3245         u32 u4Tmp;
3246
3247         if (pBtCoexist->bManualControl || pBtCoexist->bStopCoexDm)
3248                 return;
3249
3250         if (BTC_SCAN_START == type) {
3251                 pCoexSta->bWiFiIsHighPriTask = true;
3252                 BTC_PRINT(
3253                         BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN START notify\n")
3254                 );
3255
3256                 halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 8);  /* Force antenna setup for no scan result issue */
3257                 u4Tmp = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x948);
3258                 u1Tmpa = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x765);
3259                 u1Tmpb = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x67);
3260
3261
3262                 BTC_PRINT(
3263                         BTC_MSG_INTERFACE,
3264                         INTF_NOTIFY,
3265                         (
3266                                 "[BTCoex], 0x948 = 0x%x, 0x765 = 0x%x, 0x67 = 0x%x\n",
3267                                 u4Tmp,
3268                                 u1Tmpa,
3269                                 u1Tmpb
3270                         )
3271                 );
3272         } else {
3273                 pCoexSta->bWiFiIsHighPriTask = false;
3274                 BTC_PRINT(
3275                         BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN FINISH notify\n")
3276                 );
3277
3278                 pBtCoexist->fBtcGet(
3279                         pBtCoexist, BTC_GET_U1_AP_NUM, &pCoexSta->nScanAPNum
3280                 );
3281         }
3282
3283         if (pBtCoexist->btInfo.bBtDisabled)
3284                 return;
3285
3286         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3287         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
3288
3289         halbtc8723b1ant_QueryBtInfo(pBtCoexist);
3290
3291         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
3292         numOfWifiLink = wifiLinkStatus>>16;
3293
3294         if (numOfWifiLink >= 2) {
3295                 halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3296                 halbtc8723b1ant_LimitedRx(
3297                         pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize
3298                 );
3299                 halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
3300                 return;
3301         }
3302
3303         if (pCoexSta->bC2hBtInquiryPage) {
3304                 halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
3305                 return;
3306         } else if (bBtHsOn) {
3307                 halbtc8723b1ant_ActionHs(pBtCoexist);
3308                 return;
3309         }
3310
3311         if (BTC_SCAN_START == type) {
3312                 /* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN START notify\n")); */
3313                 if (!bWifiConnected)    /*  non-connected scan */
3314                         halbtc8723b1ant_ActionWifiNotConnectedScan(pBtCoexist);
3315                 else    /*  wifi is connected */
3316                         halbtc8723b1ant_ActionWifiConnectedScan(pBtCoexist);
3317         } else if (BTC_SCAN_FINISH == type) {
3318                 /* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN FINISH notify\n")); */
3319                 if (!bWifiConnected)    /*  non-connected scan */
3320                         halbtc8723b1ant_ActionWifiNotConnected(pBtCoexist);
3321                 else
3322                         halbtc8723b1ant_ActionWifiConnected(pBtCoexist);
3323         }
3324 }
3325
3326 void EXhalbtc8723b1ant_ConnectNotify(PBTC_COEXIST pBtCoexist, u8 type)
3327 {
3328         bool bWifiConnected = false, bBtHsOn = false;
3329         u32 wifiLinkStatus = 0;
3330         u32 numOfWifiLink = 0;
3331         bool bBtCtrlAggBufSize = false;
3332         u8 aggBufSize = 5;
3333
3334         if (
3335                 pBtCoexist->bManualControl ||
3336                 pBtCoexist->bStopCoexDm ||
3337                 pBtCoexist->btInfo.bBtDisabled
3338         )
3339                 return;
3340
3341         if (BTC_ASSOCIATE_START == type) {
3342                 pCoexSta->bWiFiIsHighPriTask = true;
3343                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT START notify\n"));
3344                  pCoexDm->nArpCnt = 0;
3345         } else {
3346                 pCoexSta->bWiFiIsHighPriTask = false;
3347                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT FINISH notify\n"));
3348                 /* pCoexDm->nArpCnt = 0; */
3349         }
3350
3351         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus);
3352         numOfWifiLink = wifiLinkStatus>>16;
3353         if (numOfWifiLink >= 2) {
3354                 halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3355                 halbtc8723b1ant_LimitedRx(pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize);
3356                 halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
3357                 return;
3358         }
3359
3360         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3361         if (pCoexSta->bC2hBtInquiryPage) {
3362                 halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
3363                 return;
3364         } else if (bBtHsOn) {
3365                 halbtc8723b1ant_ActionHs(pBtCoexist);
3366                 return;
3367         }
3368
3369         if (BTC_ASSOCIATE_START == type) {
3370                 /* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT START notify\n")); */
3371                 halbtc8723b1ant_ActionWifiNotConnectedAssoAuth(pBtCoexist);
3372         } else if (BTC_ASSOCIATE_FINISH == type) {
3373                 /* BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT FINISH notify\n")); */
3374
3375                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
3376                 if (!bWifiConnected) /*  non-connected scan */
3377                         halbtc8723b1ant_ActionWifiNotConnected(pBtCoexist);
3378                 else
3379                         halbtc8723b1ant_ActionWifiConnected(pBtCoexist);
3380         }
3381 }
3382
3383 void EXhalbtc8723b1ant_MediaStatusNotify(PBTC_COEXIST pBtCoexist, u8 type)
3384 {
3385         u8 H2C_Parameter[3] = {0};
3386         u32 wifiBw;
3387         u8 wifiCentralChnl;
3388         bool bWifiUnderBMode = false;
3389
3390         if (
3391                 pBtCoexist->bManualControl ||
3392                 pBtCoexist->bStopCoexDm ||
3393                 pBtCoexist->btInfo.bBtDisabled
3394         )
3395                 return;
3396
3397         if (BTC_MEDIA_CONNECT == type) {
3398                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], MEDIA connect notify\n"));
3399
3400                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode);
3401
3402                 /* Set CCK Tx/Rx high Pri except 11b mode */
3403                 if (bWifiUnderBMode) {
3404                         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x00); /* CCK Tx */
3405                         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x00); /* CCK Rx */
3406                 } else {
3407                         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x10); /* CCK Tx */
3408                         pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x10); /* CCK Rx */
3409                 }
3410
3411                 pCoexDm->backupArfrCnt1 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x430);
3412                 pCoexDm->backupArfrCnt2 = pBtCoexist->fBtcRead4Byte(pBtCoexist, 0x434);
3413                 pCoexDm->backupRetryLimit = pBtCoexist->fBtcRead2Byte(pBtCoexist, 0x42a);
3414                 pCoexDm->backupAmpduMaxTime = pBtCoexist->fBtcRead1Byte(pBtCoexist, 0x456);
3415         } else {
3416                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], MEDIA disconnect notify\n"));
3417                 pCoexDm->nArpCnt = 0;
3418
3419                 pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cd, 0x0); /* CCK Tx */
3420                 pBtCoexist->fBtcWrite1Byte(pBtCoexist, 0x6cf, 0x0); /* CCK Rx */
3421         }
3422
3423         /*  only 2.4G we need to inform bt the chnl mask */
3424         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL, &wifiCentralChnl);
3425         if ((BTC_MEDIA_CONNECT == type) && (wifiCentralChnl <= 14)) {
3426                 /* H2C_Parameter[0] = 0x1; */
3427                 H2C_Parameter[0] = 0x0;
3428                 H2C_Parameter[1] = wifiCentralChnl;
3429                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
3430
3431                 if (BTC_WIFI_BW_HT40 == wifiBw)
3432                         H2C_Parameter[2] = 0x30;
3433                 else
3434                         H2C_Parameter[2] = 0x20;
3435         }
3436
3437         pCoexDm->wifiChnlInfo[0] = H2C_Parameter[0];
3438         pCoexDm->wifiChnlInfo[1] = H2C_Parameter[1];
3439         pCoexDm->wifiChnlInfo[2] = H2C_Parameter[2];
3440
3441         BTC_PRINT(
3442                 BTC_MSG_ALGORITHM,
3443                 ALGO_TRACE_FW_EXEC,
3444                 (
3445                         "[BTCoex], FW write 0x66 = 0x%x\n",
3446                         H2C_Parameter[0]<<16 | H2C_Parameter[1]<<8 | H2C_Parameter[2]
3447                 )
3448         );
3449
3450         pBtCoexist->fBtcFillH2c(pBtCoexist, 0x66, 3, H2C_Parameter);
3451 }
3452
3453 void EXhalbtc8723b1ant_SpecialPacketNotify(PBTC_COEXIST pBtCoexist, u8 type)
3454 {
3455         bool bBtHsOn = false;
3456         u32 wifiLinkStatus = 0;
3457         u32 numOfWifiLink = 0;
3458         bool bBtCtrlAggBufSize = false;
3459         u8 aggBufSize = 5;
3460
3461         if (
3462                 pBtCoexist->bManualControl ||
3463                 pBtCoexist->bStopCoexDm ||
3464                 pBtCoexist->btInfo.bBtDisabled
3465         )
3466                 return;
3467
3468         if (
3469                 BTC_PACKET_DHCP == type ||
3470                 BTC_PACKET_EAPOL == type ||
3471                 BTC_PACKET_ARP == type
3472         ) {
3473                 if (BTC_PACKET_ARP == type) {
3474                         BTC_PRINT(
3475                                 BTC_MSG_INTERFACE,
3476                                 INTF_NOTIFY,
3477                                 ("[BTCoex], special Packet ARP notify\n")
3478                         );
3479
3480                         pCoexDm->nArpCnt++;
3481                         BTC_PRINT(
3482                                 BTC_MSG_INTERFACE,
3483                                 INTF_NOTIFY,
3484                                 ("[BTCoex], ARP Packet Count = %d\n", pCoexDm->nArpCnt)
3485                         );
3486
3487                         if (pCoexDm->nArpCnt >= 10) /*  if APR PKT > 10 after connect, do not go to ActionWifiConnectedSpecialPacket(pBtCoexist) */
3488                                 pCoexSta->bWiFiIsHighPriTask = false;
3489                         else
3490                                 pCoexSta->bWiFiIsHighPriTask = true;
3491                 } else {
3492                         pCoexSta->bWiFiIsHighPriTask = true;
3493                         BTC_PRINT(
3494                                 BTC_MSG_INTERFACE,
3495                                 INTF_NOTIFY,
3496                                 ("[BTCoex], special Packet DHCP or EAPOL notify\n")
3497                         );
3498                 }
3499         } else {
3500                 pCoexSta->bWiFiIsHighPriTask = false;
3501                 BTC_PRINT(
3502                         BTC_MSG_INTERFACE,
3503                         INTF_NOTIFY,
3504                         ("[BTCoex], special Packet [Type = %d] notify\n", type)
3505                 );
3506         }
3507
3508         pCoexSta->specialPktPeriodCnt = 0;
3509
3510         pBtCoexist->fBtcGet(
3511                 pBtCoexist, BTC_GET_U4_WIFI_LINK_STATUS, &wifiLinkStatus
3512         );
3513         numOfWifiLink = wifiLinkStatus>>16;
3514
3515         if (numOfWifiLink >= 2) {
3516                 halbtc8723b1ant_LimitedTx(pBtCoexist, NORMAL_EXEC, 0, 0, 0, 0);
3517                 halbtc8723b1ant_LimitedRx(
3518                         pBtCoexist, NORMAL_EXEC, false, bBtCtrlAggBufSize, aggBufSize
3519                 );
3520                 halbtc8723b1ant_ActionWifiMultiPort(pBtCoexist);
3521                 return;
3522         }
3523
3524         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
3525         if (pCoexSta->bC2hBtInquiryPage) {
3526                 halbtc8723b1ant_ActionBtInquiry(pBtCoexist);
3527                 return;
3528         } else if (bBtHsOn) {
3529                 halbtc8723b1ant_ActionHs(pBtCoexist);
3530                 return;
3531         }
3532
3533         if (
3534                 BTC_PACKET_DHCP == type ||
3535                 BTC_PACKET_EAPOL == type ||
3536                 ((BTC_PACKET_ARP == type) && (pCoexSta->bWiFiIsHighPriTask))
3537         )
3538                 halbtc8723b1ant_ActionWifiConnectedSpecialPacket(pBtCoexist);
3539 }
3540
3541 void EXhalbtc8723b1ant_BtInfoNotify(
3542         PBTC_COEXIST pBtCoexist, u8 *tmpBuf, u8 length
3543 )
3544 {
3545         u8 btInfo = 0;
3546         u8 i, rspSource = 0;
3547         bool bWifiConnected = false;
3548         bool bBtBusy = false;
3549
3550         pCoexSta->bC2hBtInfoReqSent = false;
3551
3552         rspSource = tmpBuf[0]&0xf;
3553         if (rspSource >= BT_INFO_SRC_8723B_1ANT_MAX)
3554                 rspSource = BT_INFO_SRC_8723B_1ANT_WIFI_FW;
3555         pCoexSta->btInfoC2hCnt[rspSource]++;
3556
3557         BTC_PRINT(
3558                 BTC_MSG_INTERFACE,
3559                 INTF_NOTIFY,
3560                 ("[BTCoex], Bt info[%d], length =%d, hex data =[",
3561                 rspSource,
3562                 length)
3563         );
3564         for (i = 0; i < length; i++) {
3565                 pCoexSta->btInfoC2h[rspSource][i] = tmpBuf[i];
3566                 if (i == 1)
3567                         btInfo = tmpBuf[i];
3568                 if (i == length-1)
3569                         BTC_PRINT(
3570                                 BTC_MSG_INTERFACE, INTF_NOTIFY, ("0x%02x]\n", tmpBuf[i])
3571                         );
3572                 else
3573                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("0x%02x, ", tmpBuf[i]));
3574         }
3575
3576         if (BT_INFO_SRC_8723B_1ANT_WIFI_FW != rspSource) {
3577                 pCoexSta->btRetryCnt = pCoexSta->btInfoC2h[rspSource][2]&0xf;
3578
3579                 if (pCoexSta->btRetryCnt >= 1)
3580                         pCoexSta->popEventCnt++;
3581
3582                 if (pCoexSta->btInfoC2h[rspSource][2]&0x20)
3583                         pCoexSta->bC2hBtPage = true;
3584                 else
3585                         pCoexSta->bC2hBtPage = false;
3586
3587                 pCoexSta->btRssi = pCoexSta->btInfoC2h[rspSource][3]*2-90;
3588                 /* pCoexSta->btInfoC2h[rspSource][3]*2+10; */
3589
3590                 pCoexSta->btInfoExt = pCoexSta->btInfoC2h[rspSource][4];
3591
3592                 pCoexSta->bBtTxRxMask = (pCoexSta->btInfoC2h[rspSource][2]&0x40);
3593                 pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TX_RX_MASK, &pCoexSta->bBtTxRxMask);
3594
3595                 if (!pCoexSta->bBtTxRxMask) {
3596                         /* BT into is responded by BT FW and BT RF REG 0x3C != 0x15 => Need to switch BT TRx Mask */
3597                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n"));
3598                         pBtCoexist->fBtcSetBtReg(pBtCoexist, BTC_BT_REG_RF, 0x3c, 0x15);
3599                 }
3600
3601                 /*  Here we need to resend some wifi info to BT */
3602                 /*  because bt is reset and loss of the info. */
3603                 if (pCoexSta->btInfoExt & BIT1) {
3604                         BTC_PRINT(
3605                                 BTC_MSG_ALGORITHM,
3606                                 ALGO_TRACE,
3607                                 ("[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n")
3608                         );
3609                         pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
3610                         if (bWifiConnected)
3611                                 EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_CONNECT);
3612                         else
3613                                 EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
3614                 }
3615
3616                 if (pCoexSta->btInfoExt & BIT3) {
3617                         if (!pBtCoexist->bManualControl && !pBtCoexist->bStopCoexDm) {
3618                                 BTC_PRINT(
3619                                         BTC_MSG_ALGORITHM,
3620                                         ALGO_TRACE,
3621                                         ("[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n")
3622                                 );
3623                                 halbtc8723b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, false);
3624                         }
3625                 } else {
3626                         /*  BT already NOT ignore Wlan active, do nothing here. */
3627                 }
3628         }
3629
3630         /*  check BIT2 first ==> check if bt is under inquiry or page scan */
3631         if (btInfo & BT_INFO_8723B_1ANT_B_INQ_PAGE)
3632                 pCoexSta->bC2hBtInquiryPage = true;
3633         else
3634                 pCoexSta->bC2hBtInquiryPage = false;
3635
3636         /*  set link exist status */
3637         if (!(btInfo&BT_INFO_8723B_1ANT_B_CONNECTION)) {
3638                 pCoexSta->bBtLinkExist = false;
3639                 pCoexSta->bPanExist = false;
3640                 pCoexSta->bA2dpExist = false;
3641                 pCoexSta->bHidExist = false;
3642                 pCoexSta->bScoExist = false;
3643         } else {        /*  connection exists */
3644                 pCoexSta->bBtLinkExist = true;
3645                 if (btInfo & BT_INFO_8723B_1ANT_B_FTP)
3646                         pCoexSta->bPanExist = true;
3647                 else
3648                         pCoexSta->bPanExist = false;
3649
3650                 if (btInfo & BT_INFO_8723B_1ANT_B_A2DP)
3651                         pCoexSta->bA2dpExist = true;
3652                 else
3653                         pCoexSta->bA2dpExist = false;
3654
3655                 if (btInfo & BT_INFO_8723B_1ANT_B_HID)
3656                         pCoexSta->bHidExist = true;
3657                 else
3658                         pCoexSta->bHidExist = false;
3659
3660                 if (btInfo & BT_INFO_8723B_1ANT_B_SCO_ESCO)
3661                         pCoexSta->bScoExist = true;
3662                 else
3663                         pCoexSta->bScoExist = false;
3664         }
3665
3666         halbtc8723b1ant_UpdateBtLinkInfo(pBtCoexist);
3667
3668         btInfo = btInfo & 0x1f;  /* mask profile bit for connect-ilde identification (for CSR case: A2DP idle --> 0x41) */
3669
3670         if (!(btInfo&BT_INFO_8723B_1ANT_B_CONNECTION)) {
3671                 pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
3672                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n"));
3673         } else if (btInfo == BT_INFO_8723B_1ANT_B_CONNECTION)   {
3674                 /*  connection exists but no busy */
3675                 pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_CONNECTED_IDLE;
3676                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n"));
3677         } else if (
3678                 (btInfo&BT_INFO_8723B_1ANT_B_SCO_ESCO) ||
3679                 (btInfo&BT_INFO_8723B_1ANT_B_SCO_BUSY)
3680         ) {
3681                 pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_SCO_BUSY;
3682                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT SCO busy!!!\n"));
3683         } else if (btInfo&BT_INFO_8723B_1ANT_B_ACL_BUSY) {
3684                 if (BT_8723B_1ANT_BT_STATUS_ACL_BUSY != pCoexDm->btStatus)
3685                         pCoexDm->bAutoTdmaAdjust = false;
3686
3687                 pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_ACL_BUSY;
3688                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT ACL busy!!!\n"));
3689         } else {
3690                 pCoexDm->btStatus = BT_8723B_1ANT_BT_STATUS_MAX;
3691                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n"));
3692         }
3693
3694         if (
3695                 (BT_8723B_1ANT_BT_STATUS_ACL_BUSY == pCoexDm->btStatus) ||
3696                 (BT_8723B_1ANT_BT_STATUS_SCO_BUSY == pCoexDm->btStatus) ||
3697                 (BT_8723B_1ANT_BT_STATUS_ACL_SCO_BUSY == pCoexDm->btStatus)
3698         )
3699                 bBtBusy = true;
3700         else
3701                 bBtBusy = false;
3702         pBtCoexist->fBtcSet(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
3703
3704         halbtc8723b1ant_RunCoexistMechanism(pBtCoexist);
3705 }
3706
3707 void EXhalbtc8723b1ant_HaltNotify(PBTC_COEXIST pBtCoexist)
3708 {
3709         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Halt notify\n"));
3710
3711         halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3712         halbtc8723b1ant_PsTdma(pBtCoexist, FORCE_EXEC, false, 0);
3713         halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, false, true);
3714
3715         halbtc8723b1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, true);
3716
3717         EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
3718
3719         pBtCoexist->bStopCoexDm = true;
3720 }
3721
3722 void EXhalbtc8723b1ant_PnpNotify(PBTC_COEXIST pBtCoexist, u8 pnpState)
3723 {
3724         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Pnp notify\n"));
3725
3726         if (BTC_WIFI_PNP_SLEEP == pnpState) {
3727                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Pnp notify to SLEEP\n"));
3728
3729                 halbtc8723b1ant_PowerSaveState(pBtCoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3730                 halbtc8723b1ant_PsTdma(pBtCoexist, NORMAL_EXEC, false, 0);
3731                 halbtc8723b1ant_CoexTableWithType(pBtCoexist, NORMAL_EXEC, 2);
3732                 halbtc8723b1ant_SetAntPath(pBtCoexist, BTC_ANT_PATH_BT, false, true);
3733
3734                 pBtCoexist->bStopCoexDm = true;
3735         } else if (BTC_WIFI_PNP_WAKE_UP == pnpState) {
3736                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Pnp notify to WAKE UP\n"));
3737                 pBtCoexist->bStopCoexDm = false;
3738                 halbtc8723b1ant_InitHwConfig(pBtCoexist, false, false);
3739                 halbtc8723b1ant_InitCoexDm(pBtCoexist);
3740                 halbtc8723b1ant_QueryBtInfo(pBtCoexist);
3741         }
3742 }
3743
3744 void EXhalbtc8723b1ant_Periodical(PBTC_COEXIST pBtCoexist)
3745 {
3746         static u8 disVerInfoCnt;
3747         u32 fwVer = 0, btPatchVer = 0;
3748
3749         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], ==========================Periodical ===========================\n"));
3750
3751         if (disVerInfoCnt <= 5) {
3752                 disVerInfoCnt += 1;
3753                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], ****************************************************************\n"));
3754                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_BT_PATCH_VER, &btPatchVer);
3755                 pBtCoexist->fBtcGet(pBtCoexist, BTC_GET_U4_WIFI_FW_VER, &fwVer);
3756                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n", \
3757                         GLCoexVerDate8723b1Ant, GLCoexVer8723b1Ant, fwVer, btPatchVer, btPatchVer));
3758                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], ****************************************************************\n"));
3759         }
3760
3761         halbtc8723b1ant_MonitorBtCtr(pBtCoexist);
3762         halbtc8723b1ant_MonitorWiFiCtr(pBtCoexist);
3763
3764         if (
3765                 halbtc8723b1ant_IsWifiStatusChanged(pBtCoexist) ||
3766                 pCoexDm->bAutoTdmaAdjust
3767         )
3768                 halbtc8723b1ant_RunCoexistMechanism(pBtCoexist);
3769
3770         pCoexSta->specialPktPeriodCnt++;
3771 }