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