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