]> Pileus Git - ~andy/linux/blob - drivers/staging/rtl8821ae/btcoexist/habtc8723a1ant.c
Staging: rtl8812ae: Add Realtek 8821 PCI WIFI driver
[~andy/linux] / drivers / staging / rtl8821ae / btcoexist / habtc8723a1ant.c
1 //============================================================
2 // Description:
3 //
4 // This file is for RTL8723A Co-exist mechanism
5 //
6 // History
7 // 2012/08/22 Cosa first check in.
8 // 2012/11/14 Cosa Revise for 8723A 1Ant out sourcing.
9 //
10 //============================================================
11
12 //============================================================
13 // include files
14 //============================================================
15 #include "Mp_Precomp.h"
16 #if(BT_30_SUPPORT == 1)
17 //============================================================
18 // Global variables, these are static variables
19 //============================================================
20 static COEX_DM_8723A_1ANT       GLCoexDm8723a1Ant;
21 static PCOEX_DM_8723A_1ANT      pCoexDm=&GLCoexDm8723a1Ant;
22 static COEX_STA_8723A_1ANT      GLCoexSta8723a1Ant;
23 static PCOEX_STA_8723A_1ANT     pCoexSta=&GLCoexSta8723a1Ant;
24
25 const char *const GLBtInfoSrc8723a1Ant[]={
26         "BT Info[wifi fw]",
27         "BT Info[bt rsp]",
28         "BT Info[bt auto report]",
29 };
30
31 //============================================================
32 // local function proto type if needed
33 //============================================================
34 //============================================================
35 // local function start with halbtc8723a1ant_
36 //============================================================
37 VOID
38 halbtc8723a1ant_Reg0x550Bit3(
39         IN      PBTC_COEXIST            pBtCoexist,
40         IN      BOOLEAN                 bSet
41         )
42 {
43         u1Byte  u1tmp=0;
44         
45         u1tmp = pBtCoexist->btc_read_1byte(pBtCoexist, 0x550);
46         if(bSet)
47         {
48                 u1tmp |= BIT3;
49         }
50         else
51         {
52                 u1tmp &= ~BIT3;
53         }
54         pBtCoexist->btc_write_1byte(pBtCoexist, 0x550, u1tmp);
55         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], set 0x550[3]=%d\n", (bSet? 1:0)));
56 }
57
58 VOID
59 halbtc8723a1ant_NotifyFwScan(
60         IN      PBTC_COEXIST            pBtCoexist,
61         IN      u1Byte                  scanType
62         )
63 {
64         u1Byte                  H2C_Parameter[1] ={0};
65         
66         if(BTC_SCAN_START == scanType)
67                 H2C_Parameter[0] = 0x1;
68
69         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC, ("[BTCoex], Notify FW for wifi scan, write 0x3b=0x%x\n", 
70                 H2C_Parameter[0]));
71
72         pBtCoexist->btc_fill_h2c(pBtCoexist, 0x3b, 1, H2C_Parameter);
73 }
74
75 VOID
76 halbtc8723a1ant_QueryBtInfo(
77         IN      PBTC_COEXIST            pBtCoexist
78         )
79 {
80         u1Byte                  H2C_Parameter[1] ={0};
81
82         pCoexSta->bC2hBtInfoReqSent = true;
83
84         H2C_Parameter[0] |= BIT0;       // trigger
85
86         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC, ("[BTCoex], Query Bt Info, FW write 0x38=0x%x\n", 
87                 H2C_Parameter[0]));
88
89         pBtCoexist->btc_fill_h2c(pBtCoexist, 0x38, 1, H2C_Parameter);
90 }
91
92 VOID
93 halbtc8723a1ant_SetSwRfRxLpfCorner(
94         IN      PBTC_COEXIST            pBtCoexist,
95         IN      BOOLEAN                 bRxRfShrinkOn
96         )
97 {
98         if(bRxRfShrinkOn)
99         {
100                 //Shrink RF Rx LPF corner
101                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], Shrink RF Rx LPF corner!!\n"));
102                 pBtCoexist->btc_set_rf_reg(pBtCoexist, BTC_RF_A, 0x1e, 0xfffff, 0xf0ff7);
103         }
104         else
105         {
106                 //Resume RF Rx LPF corner
107                 // After initialized, we can use pCoexDm->btRf0x1eBackup
108                 if(pBtCoexist->initilized)
109                 {
110                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], Resume RF Rx LPF corner!!\n"));
111                         pBtCoexist->btc_set_rf_reg(pBtCoexist, BTC_RF_A, 0x1e, 0xfffff, pCoexDm->btRf0x1eBackup);
112                 }
113         }
114 }
115
116 VOID
117 halbtc8723a1ant_RfShrink(
118         IN      PBTC_COEXIST            pBtCoexist,
119         IN      BOOLEAN                 bForceExec,
120         IN      BOOLEAN                 bRxRfShrinkOn
121         )
122 {
123         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW, ("[BTCoex], %s turn Rx RF Shrink = %s\n",  
124                 (bForceExec? "force to":""), ((bRxRfShrinkOn)? "ON":"OFF")));
125         pCoexDm->bCurRfRxLpfShrink = bRxRfShrinkOn;
126
127         if(!bForceExec)
128         {
129                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_DETAIL, ("[BTCoex], bPreRfRxLpfShrink=%d, bCurRfRxLpfShrink=%d\n", 
130                         pCoexDm->bPreRfRxLpfShrink, pCoexDm->bCurRfRxLpfShrink));
131
132                 if(pCoexDm->bPreRfRxLpfShrink == pCoexDm->bCurRfRxLpfShrink) 
133                         return;
134         }
135         halbtc8723a1ant_SetSwRfRxLpfCorner(pBtCoexist, pCoexDm->bCurRfRxLpfShrink);
136
137         pCoexDm->bPreRfRxLpfShrink = pCoexDm->bCurRfRxLpfShrink;
138 }
139
140 VOID
141 halbtc8723a1ant_SetSwPenaltyTxRateAdaptive(
142         IN      PBTC_COEXIST            pBtCoexist,
143         IN      BOOLEAN                 bLowPenaltyRa
144         )
145 {
146         u1Byte  tmpU1;
147
148         tmpU1 = pBtCoexist->btc_read_1byte(pBtCoexist, 0x4fd);
149         tmpU1 |= BIT0;
150         if(bLowPenaltyRa)
151         {
152                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], Tx rate adaptive, set low penalty!!\n"));
153                 tmpU1 &= ~BIT2;
154         }
155         else
156         {
157                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], Tx rate adaptive, set normal!!\n"));
158                 tmpU1 |= BIT2;
159         }
160
161         pBtCoexist->btc_write_1byte(pBtCoexist, 0x4fd, tmpU1);
162 }
163
164 VOID
165 halbtc8723a1ant_LowPenaltyRa(
166         IN      PBTC_COEXIST            pBtCoexist,
167         IN      BOOLEAN                 bForceExec,
168         IN      BOOLEAN                 bLowPenaltyRa
169         )
170 {
171         return;
172         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW, ("[BTCoex], %s turn LowPenaltyRA = %s\n",  
173                 (bForceExec? "force to":""), ((bLowPenaltyRa)? "ON":"OFF")));
174         pCoexDm->bCurLowPenaltyRa = bLowPenaltyRa;
175
176         if(!bForceExec)
177         {
178                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_DETAIL, ("[BTCoex], bPreLowPenaltyRa=%d, bCurLowPenaltyRa=%d\n", 
179                         pCoexDm->bPreLowPenaltyRa, pCoexDm->bCurLowPenaltyRa));
180
181                 if(pCoexDm->bPreLowPenaltyRa == pCoexDm->bCurLowPenaltyRa) 
182                         return;
183         }
184         halbtc8723a1ant_SetSwPenaltyTxRateAdaptive(pBtCoexist, pCoexDm->bCurLowPenaltyRa);
185
186         pCoexDm->bPreLowPenaltyRa = pCoexDm->bCurLowPenaltyRa;
187 }
188
189 VOID
190 halbtc8723a1ant_SetCoexTable(
191         IN      PBTC_COEXIST    pBtCoexist,
192         IN      u4Byte          val0x6c0,
193         IN      u4Byte          val0x6c8,
194         IN      u1Byte          val0x6cc
195         )
196 {
197         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], set coex table, set 0x6c0=0x%x\n", val0x6c0));
198         pBtCoexist->btc_write_4byte(pBtCoexist, 0x6c0, val0x6c0);
199
200         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], set coex table, set 0x6c8=0x%x\n", val0x6c8));
201         pBtCoexist->btc_write_4byte(pBtCoexist, 0x6c8, val0x6c8);
202
203         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_EXEC, ("[BTCoex], set coex table, set 0x6cc=0x%x\n", val0x6cc));
204         pBtCoexist->btc_write_1byte(pBtCoexist, 0x6cc, val0x6cc);
205 }
206
207 VOID
208 halbtc8723a1ant_CoexTable(
209         IN      PBTC_COEXIST            pBtCoexist,
210         IN      BOOLEAN                 bForceExec,
211         IN      u4Byte                  val0x6c0,
212         IN      u4Byte                  val0x6c8,
213         IN      u1Byte                  val0x6cc
214         )
215 {
216         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW, ("[BTCoex], %s write Coex Table 0x6c0=0x%x, 0x6c8=0x%x, 0x6cc=0x%x\n", 
217                 (bForceExec? "force to":""), val0x6c0, val0x6c8, val0x6cc));
218         pCoexDm->curVal0x6c0 = val0x6c0;
219         pCoexDm->curVal0x6c8 = val0x6c8;
220         pCoexDm->curVal0x6cc = val0x6cc;
221
222         if(!bForceExec)
223         {
224                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_DETAIL, ("[BTCoex], preVal0x6c0=0x%x, preVal0x6c8=0x%x, preVal0x6cc=0x%x !!\n", 
225                         pCoexDm->preVal0x6c0, pCoexDm->preVal0x6c8, pCoexDm->preVal0x6cc));
226                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_SW_DETAIL, ("[BTCoex], curVal0x6c0=0x%x, curVal0x6c8=0x%x, curVal0x6cc=0x%x !!\n", 
227                         pCoexDm->curVal0x6c0, pCoexDm->curVal0x6c8, pCoexDm->curVal0x6cc));
228         
229                 if( (pCoexDm->preVal0x6c0 == pCoexDm->curVal0x6c0) &&
230                         (pCoexDm->preVal0x6c8 == pCoexDm->curVal0x6c8) &&
231                         (pCoexDm->preVal0x6cc == pCoexDm->curVal0x6cc) )
232                         return;
233         }
234         halbtc8723a1ant_SetCoexTable(pBtCoexist, val0x6c0, val0x6c8, val0x6cc);
235
236         pCoexDm->preVal0x6c0 = pCoexDm->curVal0x6c0;
237         pCoexDm->preVal0x6c8 = pCoexDm->curVal0x6c8;
238         pCoexDm->preVal0x6cc = pCoexDm->curVal0x6cc;
239 }
240
241 VOID
242 halbtc8723a1ant_SetFwIgnoreWlanAct(
243         IN      PBTC_COEXIST            pBtCoexist,
244         IN      BOOLEAN                 bEnable
245         )
246 {
247         u1Byte                  H2C_Parameter[1] ={0};
248                 
249         if(bEnable)
250         {
251                 H2C_Parameter[0] |= BIT0;               // function enable
252         }
253         
254         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC, ("[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x25=0x%x\n", 
255                 H2C_Parameter[0]));
256
257         pBtCoexist->btc_fill_h2c(pBtCoexist, 0x25, 1, H2C_Parameter);   
258 }
259
260 VOID
261 halbtc8723a1ant_IgnoreWlanAct(
262         IN      PBTC_COEXIST            pBtCoexist,
263         IN      BOOLEAN                 bForceExec,
264         IN      BOOLEAN                 bEnable
265         )
266 {
267         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW, ("[BTCoex], %s turn Ignore WlanAct %s\n", 
268                 (bForceExec? "force to":""), (bEnable? "ON":"OFF")));
269         pCoexDm->bCurIgnoreWlanAct = bEnable;
270
271         if(!bForceExec)
272         {
273                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], bPreIgnoreWlanAct = %d, bCurIgnoreWlanAct = %d!!\n", 
274                         pCoexDm->bPreIgnoreWlanAct, pCoexDm->bCurIgnoreWlanAct));
275
276                 if(pCoexDm->bPreIgnoreWlanAct == pCoexDm->bCurIgnoreWlanAct)
277                         return;
278         }
279         halbtc8723a1ant_SetFwIgnoreWlanAct(pBtCoexist, bEnable);
280
281         pCoexDm->bPreIgnoreWlanAct = pCoexDm->bCurIgnoreWlanAct;
282 }
283
284 VOID
285 halbtc8723a1ant_SetFwPstdma(
286         IN      PBTC_COEXIST            pBtCoexist,
287         IN      u1Byte                  type,
288         IN      u1Byte                  byte1,
289         IN      u1Byte                  byte2,
290         IN      u1Byte                  byte3,
291         IN      u1Byte                  byte4,
292         IN      u1Byte                  byte5
293         )
294 {
295         u1Byte                  H2C_Parameter[5] ={0};
296         u1Byte                  realByte1=byte1, realByte5=byte5;
297         BOOLEAN                 bApEnable=FALSE;
298
299         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, &bApEnable);
300
301         // byte1[1:0] != 0 means enable pstdma
302         // for 2Ant bt coexist, if byte1 != 0 means enable pstdma
303         if(byte1)
304         {
305                 if(bApEnable)
306                 {
307                         if(type != 5 && type != 12)
308                         {
309                                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], FW for 1Ant AP mode\n"));
310                                 realByte1 &= ~BIT4;
311                                 realByte1 |= BIT5;
312
313                                 realByte5 |= BIT5;
314                                 realByte5 &= ~BIT6;
315                         }
316                 }
317         }
318         H2C_Parameter[0] = realByte1;   
319         H2C_Parameter[1] = byte2;       
320         H2C_Parameter[2] = byte3;
321         H2C_Parameter[3] = byte4;
322         H2C_Parameter[4] = realByte5;
323
324         pCoexDm->psTdmaPara[0] = realByte1;
325         pCoexDm->psTdmaPara[1] = byte2;
326         pCoexDm->psTdmaPara[2] = byte3;
327         pCoexDm->psTdmaPara[3] = byte4;
328         pCoexDm->psTdmaPara[4] = realByte5;
329         
330         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC, ("[BTCoex], FW write 0x3a(5bytes)=0x%x%08x\n", 
331                 H2C_Parameter[0], 
332                 H2C_Parameter[1]<<24|H2C_Parameter[2]<<16|H2C_Parameter[3]<<8|H2C_Parameter[4]));
333
334         pBtCoexist->btc_fill_h2c(pBtCoexist, 0x3a, 5, H2C_Parameter);
335 }
336
337 VOID
338 halbtc8723a1ant_PsTdma(
339         IN      PBTC_COEXIST            pBtCoexist,
340         IN      BOOLEAN                 bForceExec,
341         IN      BOOLEAN                 bTurnOn,
342         IN      u1Byte                  type
343         )
344 {
345         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW, ("[BTCoex], %s turn %s PS TDMA, type=%d\n", 
346                 (bForceExec? "force to":""), (bTurnOn? "ON":"OFF"), type));             
347         pCoexDm->bCurPsTdmaOn = bTurnOn;
348         pCoexDm->curPsTdma = type;
349
350         if(!bForceExec)
351         {
352                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], bPrePsTdmaOn = %d, bCurPsTdmaOn = %d!!\n", 
353                         pCoexDm->bPrePsTdmaOn, pCoexDm->bCurPsTdmaOn));
354                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_DETAIL, ("[BTCoex], prePsTdma = %d, curPsTdma = %d!!\n", 
355                         pCoexDm->prePsTdma, pCoexDm->curPsTdma));
356
357                 if( (pCoexDm->bPrePsTdmaOn == pCoexDm->bCurPsTdmaOn) &&
358                         (pCoexDm->prePsTdma == pCoexDm->curPsTdma) )
359                         return;
360         }       
361         if(pCoexDm->bCurPsTdmaOn)
362         {
363                 switch(pCoexDm->curPsTdma)
364                 {
365                         case 1:
366                         default:
367                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x1a, 0x1a, 0x0, 0x40);
368                                 break;
369                         case 2:
370                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x12, 0x12, 0x0, 0x40);
371                                 break;
372                         case 3:
373                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x93, 0x3f, 0x3, 0x10, 0x40);
374                                 break;
375                         case 4:
376                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x93, 0x15, 0x3, 0x10, 0x0);
377                                 break;
378                         case 5:
379                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0xa9, 0x15, 0x3, 0x35, 0xc0);
380                                 break;
381                         
382                         case 8: 
383                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x93, 0x25, 0x3, 0x10, 0x0);
384                                 break;
385                         case 9: 
386                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0xa, 0xa, 0x0, 0x40);
387                                 break;
388                         case 10:        
389                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0xa, 0xa, 0x0, 0x40);
390                                 break;
391                         case 11:        
392                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x5, 0x5, 0x0, 0x40);
393                                 break;
394                         case 12:
395                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0xa9, 0xa, 0x3, 0x15, 0xc0);
396                                 break;
397         
398                         case 18:
399                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x93, 0x25, 0x3, 0x10, 0x0);
400                                 break;                  
401
402                         case 20:
403                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x2a, 0x2a, 0x0, 0x0);
404                                 break;
405                         case 21:
406                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x93, 0x20, 0x3, 0x10, 0x40);
407                                 break;
408                         case 22:
409                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x1a, 0x1a, 0x2, 0x40);
410                                 break;
411                         case 23:
412                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x12, 0x12, 0x2, 0x40);
413                                 break;
414                         case 24:
415                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0xa, 0xa, 0x2, 0x40);
416                                 break;
417                         case 25:
418                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x5, 0x5, 0x2, 0x40);
419                                 break;
420                         case 26:
421                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x93, 0x25, 0x3, 0x10, 0x0);
422                                 break;
423                         case 27:
424                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x13, 0x5, 0x5, 0x2, 0x40);
425                                 break;
426                         case 28:
427                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x3, 0x2f, 0x2f, 0x0, 0x0);
428                                 break;
429
430                 }
431         }
432         else
433         {
434                 // disable PS tdma
435                 switch(pCoexDm->curPsTdma)
436                 {
437                         case 8:
438                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x8, 0x0, 0x0, 0x0, 0x0);         
439                                 break;
440                         case 0:
441                         default:
442                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x0, 0x0, 0x0, 0x0, 0x0);
443                                 pBtCoexist->btc_write_2byte(pBtCoexist, 0x860, 0x210);
444                                 break;
445                         case 9:
446                                 halbtc8723a1ant_SetFwPstdma(pBtCoexist, type, 0x0, 0x0, 0x0, 0x0, 0x0);
447                                 pBtCoexist->btc_write_2byte(pBtCoexist, 0x860, 0x110);
448                                 break;
449
450                 }
451         }
452
453         // update pre state
454         pCoexDm->bPrePsTdmaOn = pCoexDm->bCurPsTdmaOn;
455         pCoexDm->prePsTdma = pCoexDm->curPsTdma;
456 }
457
458
459 VOID
460 halbtc8723a1ant_CoexAllOff(
461         IN      PBTC_COEXIST            pBtCoexist
462         )
463 {
464         // fw all off
465         halbtc8723a1ant_IgnoreWlanAct(pBtCoexist, NORMAL_EXEC, FALSE);
466         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0);
467
468         // sw all off
469         halbtc8723a1ant_LowPenaltyRa(pBtCoexist, NORMAL_EXEC, FALSE);
470         halbtc8723a1ant_RfShrink(pBtCoexist, NORMAL_EXEC, FALSE);
471
472         // hw all off
473         halbtc8723a1ant_CoexTable(pBtCoexist, NORMAL_EXEC, 0x55555555, 0xffff, 0x3);
474 }
475
476 VOID
477 halbtc8723a1ant_InitCoexDm(
478         IN      PBTC_COEXIST            pBtCoexist
479         )
480 {
481         // force to reset coex mechanism
482         halbtc8723a1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, FALSE);
483 }
484
485 VOID
486 halbtc8723a1ant_BtEnableAction(
487         IN      PBTC_COEXIST            pBtCoexist
488         )
489 {
490         halbtc8723a1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, FALSE);
491 }
492
493 VOID
494 halbtc8723a1ant_MonitorBtCtr(
495         IN      PBTC_COEXIST            pBtCoexist
496         )
497 {
498         u4Byte                  regHPTxRx, regLPTxRx, u4Tmp;
499         u4Byte                  regHPTx=0, regHPRx=0, regLPTx=0, regLPRx=0;
500         u1Byte                  u1Tmp;
501         
502         regHPTxRx = 0x770;
503         regLPTxRx = 0x774;
504
505         u4Tmp = pBtCoexist->btc_read_4byte(pBtCoexist, regHPTxRx);
506         regHPTx = u4Tmp & MASKLWORD;
507         regHPRx = (u4Tmp & MASKHWORD)>>16;
508
509         u4Tmp = pBtCoexist->btc_read_4byte(pBtCoexist, regLPTxRx);
510         regLPTx = u4Tmp & MASKLWORD;
511         regLPRx = (u4Tmp & MASKHWORD)>>16;
512                 
513         pCoexSta->highPriorityTx = regHPTx;
514         pCoexSta->highPriorityRx = regHPRx;
515         pCoexSta->lowPriorityTx = regLPTx;
516         pCoexSta->lowPriorityRx = regLPRx;
517
518         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR, ("[BTCoex], High Priority Tx/Rx (reg 0x%x)=0x%x(%d)/0x%x(%d)\n", 
519                 regHPTxRx, regHPTx, regHPTx, regHPRx, regHPRx));
520         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR, ("[BTCoex], Low Priority Tx/Rx (reg 0x%x)=0x%x(%d)/0x%x(%d)\n", 
521                 regLPTxRx, regLPTx, regLPTx, regLPRx, regLPRx));
522
523         // reset counter
524         pBtCoexist->btc_write_1byte(pBtCoexist, 0x76e, 0xc);
525 }
526
527 VOID
528 halbtc8723a1ant_MonitorBtEnableDisable(
529         IN      PBTC_COEXIST            pBtCoexist
530         )
531 {
532         static BOOLEAN  bPreBtDisabled=FALSE;
533         static u4Byte   btDisableCnt=0;
534         BOOLEAN                 bBtActive=true, bBtDisabled=FALSE;
535
536         // This function check if bt is disabled
537         
538         if(     pCoexSta->highPriorityTx == 0 &&
539                 pCoexSta->highPriorityRx == 0 &&
540                 pCoexSta->lowPriorityTx == 0 &&
541                 pCoexSta->lowPriorityRx == 0)
542         {
543                 bBtActive = FALSE;
544         }
545         if(     pCoexSta->highPriorityTx == 0xffff &&
546                 pCoexSta->highPriorityRx == 0xffff &&
547                 pCoexSta->lowPriorityTx == 0xffff &&
548                 pCoexSta->lowPriorityRx == 0xffff)
549         {
550                 bBtActive = FALSE;
551         }
552         if(bBtActive)
553         {
554                 btDisableCnt = 0;
555                 bBtDisabled = FALSE;
556                 pBtCoexist->btc_set(pBtCoexist, BTC_SET_BL_BT_DISABLE, &bBtDisabled);
557                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR, ("[BTCoex], BT is enabled !!\n"));
558         }
559         else
560         {
561                 btDisableCnt++;
562                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR, ("[BTCoex], bt all counters=0, %d times!!\n", 
563                                 btDisableCnt));
564                 if(btDisableCnt >= 2)
565                 {
566                         bBtDisabled = true;
567                         pBtCoexist->btc_set(pBtCoexist, BTC_SET_BL_BT_DISABLE, &bBtDisabled);
568                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR, ("[BTCoex], BT is disabled !!\n"));
569                 }
570         }
571         if(bPreBtDisabled != bBtDisabled)
572         {
573                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_BT_MONITOR, ("[BTCoex], BT is from %s to %s!!\n", 
574                         (bPreBtDisabled ? "disabled":"enabled"), 
575                         (bBtDisabled ? "disabled":"enabled")));
576                 bPreBtDisabled = bBtDisabled;
577                 if(!bBtDisabled)
578                 {
579                         halbtc8723a1ant_BtEnableAction(pBtCoexist);
580                 }
581                 else
582                 {
583                         pBtCoexist->btc_set(pBtCoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
584                 }
585         }
586 }
587
588 VOID
589 halbtc8723a1ant_TdmaDurationAdjust(
590         IN      PBTC_COEXIST            pBtCoexist
591         )
592 {
593         static s4Byte           up,dn,m,n,WaitCount;
594         s4Byte                  result;   //0: no change, +1: increase WiFi duration, -1: decrease WiFi duration
595         u1Byte                  retryCount=0;
596         u1Byte                  btState;
597         BOOLEAN                 bScan=FALSE, bLink=FALSE, bRoam=FALSE;
598         u4Byte                  wifiBw;
599         
600         pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
601         btState = pCoexDm->btStatus;
602
603         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], TdmaDurationAdjust()\n"));
604         if(pCoexDm->psTdmaGlobalCnt != pCoexDm->psTdmaMonitorCnt)
605         {
606                 pCoexDm->psTdmaMonitorCnt = 0;
607                 pCoexDm->psTdmaGlobalCnt = 0;
608         }
609         if(pCoexDm->psTdmaMonitorCnt == 0)
610         {
611                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], first run BT A2DP + WiFi busy state!!\n"));
612                 if(btState == BT_STATE_8723A_1ANT_ACL_ONLY_BUSY)
613                 {
614                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 1);
615                         pCoexDm->psTdmaDuAdjType = 1;
616                 }
617                 else
618                 {
619                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 22);
620                         pCoexDm->psTdmaDuAdjType = 22;
621                 }
622                 //============
623                 up = 0;
624                 dn = 0;
625                 m = 1;
626                 n= 3;
627                 result = 0;
628                 WaitCount = 0;
629         }
630         else
631         {
632                 //accquire the BT TRx retry count from BT_Info byte2
633                 retryCount = pCoexSta->btRetryCnt;
634                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], retryCount = %d\n", retryCount));
635                 result = 0;
636                 WaitCount++; 
637                   
638                 if(retryCount == 0)  // no retry in the last 2-second duration
639                 {
640                         up++;
641                         dn--;
642
643                         if (dn <= 0)
644                                 dn = 0;                          
645
646                         if(up >= n) // if ³sÄò n ­Ó2¬í retry count¬°0, «h½Õ¼eWiFi duration
647                         {
648                                 WaitCount = 0; 
649                                 n = 3;
650                                 up = 0;
651                                 dn = 0;
652                                 result = 1; 
653                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Increase wifi duration!!\n"));
654                         }
655                 }
656                 else if (retryCount <= 3)       // <=3 retry in the last 2-second duration
657                 {
658                         up--; 
659                         dn++;
660
661                         if (up <= 0)
662                                 up = 0;
663
664                         if (dn == 2)    // if ³sÄò 2 ­Ó2¬í retry count< 3, «h½Õ¯¶WiFi duration
665                         {
666                                 if (WaitCount <= 2)
667                                         m++; // ÁקK¤@ª½¦b¨â­Ólevel¤¤¨Ó¦^
668                                 else
669                                         m = 1;
670
671                                 if ( m >= 20) //m ³Ì¤j­È = 20 ' ³Ì¤j120¬í recheck¬O§_½Õ¾ã WiFi duration.
672                                         m = 20;
673
674                                 n = 3*m;
675                                 up = 0;
676                                 dn = 0;
677                                 WaitCount = 0;  
678                                 result = -1; 
679                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Decrease wifi duration for retryCounter<3!!\n"));
680                         }
681                 }
682                 else  //retry count > 3, ¥u­n1¦¸ retry count > 3, «h½Õ¯¶WiFi duration
683                 {
684                         if (WaitCount == 1)
685                                 m++; // ÁקK¤@ª½¦b¨â­Ólevel¤¤¨Ó¦^
686                         else
687                                 m = 1;
688
689                         if ( m >= 20) //m ³Ì¤j­È = 20 ' ³Ì¤j120¬í recheck¬O§_½Õ¾ã WiFi duration.
690                                 m = 20;
691
692                         n = 3*m;
693                         up = 0;
694                         dn = 0;
695                         WaitCount = 0; 
696                         result = -1;
697                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Decrease wifi duration for retryCounter>3!!\n"));
698                 }
699                 
700                 {
701                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], BT TxRx counter H+L <= 1200\n"));
702                         if(btState != BT_STATE_8723A_1ANT_ACL_ONLY_BUSY)
703                         {
704                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], NOT ACL only busy!\n"));
705                                 if(BTC_WIFI_BW_HT40 != wifiBw)
706                                 {
707                                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], 20MHz\n"));
708                                         if(result == -1)
709                                         {
710                                                 if(pCoexDm->curPsTdma == 22)
711                                                 {
712                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 23);
713                                                         pCoexDm->psTdmaDuAdjType = 23;
714                                                 }
715                                                 else if(pCoexDm->curPsTdma == 23)
716                                                 {
717                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 24);
718                                                         pCoexDm->psTdmaDuAdjType = 24;
719                                                 }
720                                                 else if(pCoexDm->curPsTdma == 24)
721                                                 {
722                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 25);
723                                                         pCoexDm->psTdmaDuAdjType = 25;
724                                                 }
725                                         } 
726                                         else if (result == 1)
727                                         {
728                                                 if(pCoexDm->curPsTdma == 25)
729                                                 {
730                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 24);
731                                                         pCoexDm->psTdmaDuAdjType = 24;
732                                                 }
733                                                 else if(pCoexDm->curPsTdma == 24)
734                                                 {
735                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 23);
736                                                         pCoexDm->psTdmaDuAdjType = 23;
737                                                 }
738                                                 else if(pCoexDm->curPsTdma == 23)
739                                                 {
740                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 22);
741                                                         pCoexDm->psTdmaDuAdjType = 22;
742                                                 }
743                                         }
744                                         // error handle, if not in the following state,
745                                         // set psTdma again.
746                                         if( (pCoexDm->psTdmaDuAdjType != 22) &&
747                                                 (pCoexDm->psTdmaDuAdjType != 23) &&
748                                                 (pCoexDm->psTdmaDuAdjType != 24) &&
749                                                 (pCoexDm->psTdmaDuAdjType != 25) )
750                                         {
751                                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], duration case out of handle!!\n"));
752                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 23);
753                                                 pCoexDm->psTdmaDuAdjType = 23;
754                                         }
755                                 }
756                                 else
757                                 {
758                                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], 40MHz\n"));
759                                         if(result == -1)
760                                         {
761                                                 if(pCoexDm->curPsTdma == 23)
762                                                 {
763                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 24);
764                                                         pCoexDm->psTdmaDuAdjType = 24;
765                                                 }
766                                                 else if(pCoexDm->curPsTdma == 24)
767                                                 {
768                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 25);
769                                                         pCoexDm->psTdmaDuAdjType = 25;
770                                                 }
771                                                 else if(pCoexDm->curPsTdma == 25)
772                                                 {
773                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 27);
774                                                         pCoexDm->psTdmaDuAdjType = 27;
775                                                 }
776                                         } 
777                                         else if (result == 1)
778                                         {
779                                                 if(pCoexDm->curPsTdma == 27)
780                                                 {
781                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 25);
782                                                         pCoexDm->psTdmaDuAdjType = 25;
783                                                 }
784                                                 else if(pCoexDm->curPsTdma == 25)
785                                                 {
786                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 24);
787                                                         pCoexDm->psTdmaDuAdjType = 24;
788                                                 }
789                                                 else if(pCoexDm->curPsTdma == 24)
790                                                 {
791                                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 23);
792                                                         pCoexDm->psTdmaDuAdjType = 23;
793                                                 }
794                                         }
795                                         // error handle, if not in the following state,
796                                         // set psTdma again.
797                                         if( (pCoexDm->psTdmaDuAdjType != 23) &&
798                                                 (pCoexDm->psTdmaDuAdjType != 24) &&
799                                                 (pCoexDm->psTdmaDuAdjType != 25) &&
800                                                 (pCoexDm->psTdmaDuAdjType != 27) )
801                                         {
802                                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], duration case out of handle!!\n"));
803                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 24);
804                                                 pCoexDm->psTdmaDuAdjType = 24;
805                                         }
806                                 }
807                         }
808                         else
809                         {
810                                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], ACL only busy\n"));
811                                 if (result == -1)
812                                 {
813                                         if(pCoexDm->curPsTdma == 1)
814                                         {
815                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
816                                                 pCoexDm->psTdmaDuAdjType = 2;
817                                         }
818                                         else if(pCoexDm->curPsTdma == 2)
819                                         {
820                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
821                                                 pCoexDm->psTdmaDuAdjType = 9;
822                                         }
823                                         else if(pCoexDm->curPsTdma == 9)
824                                         {
825                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 11);
826                                                 pCoexDm->psTdmaDuAdjType = 11;
827                                         }
828                                 }
829                                 else if (result == 1)
830                                 {
831                                         if(pCoexDm->curPsTdma == 11)
832                                         {
833                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 9);
834                                                 pCoexDm->psTdmaDuAdjType = 9;
835                                         }
836                                         else if(pCoexDm->curPsTdma == 9)
837                                         {
838                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
839                                                 pCoexDm->psTdmaDuAdjType = 2;
840                                         }
841                                         else if(pCoexDm->curPsTdma == 2)
842                                         {
843                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 1);
844                                                 pCoexDm->psTdmaDuAdjType = 1;
845                                         }
846                                 }
847
848                                 // error handle, if not in the following state,
849                                 // set psTdma again.
850                                 if( (pCoexDm->psTdmaDuAdjType != 1) &&
851                                         (pCoexDm->psTdmaDuAdjType != 2) &&
852                                         (pCoexDm->psTdmaDuAdjType != 9) &&
853                                         (pCoexDm->psTdmaDuAdjType != 11) )
854                                 {
855                                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], duration case out of handle!!\n"));
856                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 2);
857                                         pCoexDm->psTdmaDuAdjType = 2;
858                                 }
859                         }
860                 }
861         }
862
863         // if current PsTdma not match with the recorded one (when scan, dhcp...), 
864         // then we have to adjust it back to the previous record one.
865         if(pCoexDm->curPsTdma != pCoexDm->psTdmaDuAdjType)
866         {
867                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], PsTdma type dismatch!!!, curPsTdma=%d, recordPsTdma=%d\n", 
868                         pCoexDm->curPsTdma, pCoexDm->psTdmaDuAdjType));
869
870                 pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
871                 pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
872                 pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
873
874                 if( !bScan && !bLink && !bRoam)
875                 {
876                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, pCoexDm->psTdmaDuAdjType);
877                 }
878                 else
879                 {
880                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], roaming/link/scan is under progress, will adjust next time!!!\n"));
881                 }
882         }
883         pCoexDm->psTdmaMonitorCnt++;
884 }
885
886
887 VOID
888 halbtc8723a1ant_CoexForWifiConnect(
889         IN      PBTC_COEXIST            pBtCoexist
890         )
891 {
892         BOOLEAN         bWifiConnected=FALSE, bWifiBusy=FALSE;
893         u1Byte          btState, btInfoOriginal=0;
894
895         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
896
897         btState = pCoexDm->btStatus;
898         btInfoOriginal = pCoexSta->btInfoC2h[BT_INFO_SRC_8723A_1ANT_BT_RSP][0];
899
900         if(bWifiConnected)
901         {
902                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], wifi connected!!\n"));
903                 pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
904                 
905                 if( !bWifiBusy &&
906                         ((BT_STATE_8723A_1ANT_NO_CONNECTION == btState) ||
907                         (BT_STATE_8723A_1ANT_CONNECT_IDLE == btState)) )
908                 {
909                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], [Wifi is idle] or [Bt is non connected idle or Bt is connected idle]!!\n"));
910
911                         if(BT_STATE_8723A_1ANT_NO_CONNECTION == btState)
912                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 9);
913                         else if(BT_STATE_8723A_1ANT_CONNECT_IDLE == btState)
914                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0);
915
916                         pBtCoexist->btc_setBbReg(pBtCoexist, 0x880, 0xff000000, 0xc0);
917                 }
918                 else
919                 {
920                         if( (BT_STATE_8723A_1ANT_SCO_ONLY_BUSY == btState) ||
921                                 (BT_STATE_8723A_1ANT_ACL_SCO_BUSY == btState) ||
922                                 (BT_STATE_8723A_1ANT_HID_BUSY == btState) ||
923                                 (BT_STATE_8723A_1ANT_HID_SCO_BUSY == btState) )
924                         {
925                                 pBtCoexist->btc_setBbReg(pBtCoexist, 0x880, 0xff000000, 0x60);
926                         }
927                         else
928                         {
929                                 pBtCoexist->btc_setBbReg(pBtCoexist, 0x880, 0xff000000, 0xc0);
930                         }
931                         switch(btState)
932                         {
933                                 case BT_STATE_8723A_1ANT_NO_CONNECTION:
934                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 5);
935                                         break;
936                                 case BT_STATE_8723A_1ANT_CONNECT_IDLE:
937                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 12);
938                                         break;
939                                 case BT_STATE_8723A_1ANT_INQ_OR_PAG:
940                                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 10);
941                                         break;
942                                 case BT_STATE_8723A_1ANT_SCO_ONLY_BUSY:
943                                 case BT_STATE_8723A_1ANT_ACL_SCO_BUSY:
944                                 case BT_STATE_8723A_1ANT_HID_BUSY:
945                                 case BT_STATE_8723A_1ANT_HID_SCO_BUSY:
946                                         halbtc8723a1ant_TdmaDurationAdjust(pBtCoexist);
947                                         break;
948                                 case BT_STATE_8723A_1ANT_ACL_ONLY_BUSY:
949                                         if (btInfoOriginal&BT_INFO_8723A_1ANT_B_A2DP)
950                                         {
951                                                 halbtc8723a1ant_TdmaDurationAdjust(pBtCoexist);
952                                         }
953                                         else if(btInfoOriginal&BT_INFO_8723A_1ANT_B_FTP)
954                                         {
955                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 1);
956                                         }
957                                         else if( (btInfoOriginal&BT_INFO_8723A_1ANT_B_A2DP) &&
958                                                         (btInfoOriginal&BT_INFO_8723A_1ANT_B_FTP) )
959                                         {
960                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 6);
961                                         }
962                                         else
963                                         {
964                                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 1);
965                                         }
966                                         break;
967                                 default:
968                                         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], error!!!, undefined case in halbtc8723a1ant_CoexForWifiConnect()!!\n"));
969                                         break;
970                         }
971                 }
972         }
973         else
974         {
975                 BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], wifi is disconnected!!\n"));
976         }
977
978         pCoexDm->psTdmaGlobalCnt++;
979 }
980
981 //============================================================
982 // work around function start with wa_halbtc8723a1ant_
983 //============================================================
984 VOID
985 wa_halbtc8723a1ant_MonitorC2h(
986         IN      PBTC_COEXIST                    pBtCoexist
987         )
988 {
989         u1Byte  tmp1b=0x0;
990         u4Byte  curC2hTotalCnt=0x0;
991         static u4Byte   preC2hTotalCnt=0x0, sameCntPollingTime=0x0;
992
993         curC2hTotalCnt+=pCoexSta->btInfoC2hCnt[BT_INFO_SRC_8723A_1ANT_BT_RSP];
994
995         if(curC2hTotalCnt == preC2hTotalCnt)
996         {
997                 sameCntPollingTime++;
998         }
999         else
1000         {
1001                 preC2hTotalCnt = curC2hTotalCnt;
1002                 sameCntPollingTime = 0;
1003         }
1004
1005         if(sameCntPollingTime >= 2)
1006         {
1007                 tmp1b = pBtCoexist->btc_read_1byte(pBtCoexist, 0x1af);
1008                 if(tmp1b != 0x0)
1009                 {
1010                         pCoexSta->c2hHangDetectCnt++;
1011                         pBtCoexist->btc_write_1byte(pBtCoexist, 0x1af, 0x0);
1012                 }
1013         }
1014 }
1015
1016 //============================================================
1017 // extern function start with EXhalbtc8723a1ant_
1018 //============================================================
1019 VOID
1020 EXhalbtc8723a1ant_InitHwConfig(
1021         IN      PBTC_COEXIST            pBtCoexist
1022         )
1023 {
1024         BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], 1Ant Init HW Config!!\n"));
1025
1026         // backup rf 0x1e value
1027         pCoexDm->btRf0x1eBackup = 
1028                 pBtCoexist->btc_get_rf_reg(pBtCoexist, BTC_RF_A, 0x1e, 0xfffff);
1029
1030         pBtCoexist->btc_write_1byte(pBtCoexist, 0x40, 0x20);
1031
1032         // enable counter statistics
1033         pBtCoexist->btc_write_1byte(pBtCoexist, 0x76e, 0x4);
1034         
1035         // coex table
1036         pBtCoexist->btc_write_1byte(pBtCoexist, 0x6cc, 0x0);                    // 1-Ant coex
1037         pBtCoexist->btc_write_4byte(pBtCoexist, 0x6c8, 0xffff);         // wifi break table
1038         pBtCoexist->btc_write_4byte(pBtCoexist, 0x6c4, 0x55555555);     //coex table
1039
1040         // antenna switch control parameter
1041         pBtCoexist->btc_write_4byte(pBtCoexist, 0x858, 0xaaaaaaaa);
1042         
1043         pBtCoexist->btc_write_2byte(pBtCoexist, 0x860, 0x210);  //set antenna at wifi side if ANTSW is software control
1044         pBtCoexist->btc_write_4byte(pBtCoexist, 0x870, 0x300);  //SPDT(connected with TRSW) control by hardware PTA
1045         pBtCoexist->btc_write_4byte(pBtCoexist, 0x874, 0x22804000);     //ANTSW keep by GNT_BT
1046
1047         // coexistence parameters
1048         pBtCoexist->btc_write_1byte(pBtCoexist, 0x778, 0x1);    // enable RTK mode PTA
1049 }
1050
1051 VOID
1052 EXhalbtc8723a1ant_InitCoexDm(
1053         IN      PBTC_COEXIST            pBtCoexist
1054         )
1055 {
1056         BTC_PRINT(BTC_MSG_INTERFACE, INTF_INIT, ("[BTCoex], Coex Mechanism Init!!\n"));
1057         
1058         halbtc8723a1ant_InitCoexDm(pBtCoexist);
1059 }
1060
1061 VOID
1062 EXhalbtc8723a1ant_DisplayCoexInfo(
1063         IN      PBTC_COEXIST            pBtCoexist
1064         )
1065 {
1066         struct btc_board_info *         pBoardInfo=&pBtCoexist->board_info;
1067         PBTC_STACK_INFO         pStackInfo=&pBtCoexist->stack_info;
1068         pu1Byte                         cliBuf=pBtCoexist->cli_buf;
1069         u1Byte                          u1Tmp[4], i, btInfoExt, psTdmaCase=0;
1070         u4Byte                          u4Tmp[4];
1071         BOOLEAN                         bRoam=FALSE, bScan=FALSE, bLink=FALSE, bWifiUnder5G=FALSE;
1072         BOOLEAN                         bBtHsOn=FALSE, bWifiBusy=FALSE;
1073         s4Byte                          wifiRssi=0, btHsRssi=0;
1074         u4Byte                          wifiBw, wifiTrafficDir;
1075         u1Byte                          wifiDot11Chnl, wifiHsChnl;
1076
1077         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n ============[BT Coexist info]============");
1078         CL_PRINTF(cliBuf);
1079
1080         if(!pBoardInfo->bt_exist)
1081         {
1082                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n BT not exists !!!");
1083                 CL_PRINTF(cliBuf);
1084                 return;
1085         }
1086
1087         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d ", "Ant PG number/ Ant mechanism:", \
1088                 pBoardInfo->pg_ant_num, pBoardInfo->btdm_ant_num);
1089         CL_PRINTF(cliBuf);      
1090         
1091         if(pBtCoexist->manual_control)
1092         {
1093                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "[Action Manual control]!!");
1094                 CL_PRINTF(cliBuf);
1095         }
1096         
1097         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s / %d", "BT stack/ hci ext ver", \
1098                 ((pStackInfo->bProfileNotified)? "Yes":"No"), pStackInfo->hciVersion);
1099         CL_PRINTF(cliBuf);
1100
1101         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
1102         pBtCoexist->btc_get(pBtCoexist, BTC_GET_U1_WIFI_DOT11_CHNL, &wifiDot11Chnl);
1103         pBtCoexist->btc_get(pBtCoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifiHsChnl);
1104         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d(%d)", "Dot11 channel / HsChnl(HsMode)", \
1105                 wifiDot11Chnl, wifiHsChnl, bBtHsOn);
1106         CL_PRINTF(cliBuf);
1107
1108         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x ", "H2C Wifi inform bt chnl Info", \
1109                 pCoexDm->wifiChnlInfo[0], pCoexDm->wifiChnlInfo[1],
1110                 pCoexDm->wifiChnlInfo[2]);
1111         CL_PRINTF(cliBuf);
1112
1113         pBtCoexist->btc_get(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi);
1114         pBtCoexist->btc_get(pBtCoexist, BTC_GET_S4_HS_RSSI, &btHsRssi);
1115         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d", "Wifi rssi/ HS rssi", \
1116                 wifiRssi, btHsRssi);
1117         CL_PRINTF(cliBuf);
1118
1119         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
1120         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
1121         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
1122         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d ", "Wifi bLink/ bRoam/ bScan", \
1123                 bLink, bRoam, bScan);
1124         CL_PRINTF(cliBuf);
1125
1126         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_UNDER_5G, &bWifiUnder5G);
1127         pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw);
1128         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy);
1129         pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION, &wifiTrafficDir);
1130         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s / %s/ %s ", "Wifi status", \
1131                 (bWifiUnder5G? "5G":"2.4G"),
1132                 ((BTC_WIFI_BW_LEGACY==wifiBw)? "Legacy": (((BTC_WIFI_BW_HT40==wifiBw)? "HT40":"HT20"))),
1133                 ((!bWifiBusy)? "idle": ((BTC_WIFI_TRAFFIC_TX==wifiTrafficDir)? "uplink":"downlink")));
1134         CL_PRINTF(cliBuf);
1135
1136         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s/ %d/ %d] ", "BT [status/ rssi/ retryCnt]", \
1137                 ((pCoexSta->bC2hBtInquiryPage)?("inquiry/page scan"):((BT_8723A_1ANT_BT_STATUS_IDLE == pCoexDm->btStatus)? "idle":(  (BT_8723A_1ANT_BT_STATUS_CONNECTED_IDLE == pCoexDm->btStatus)? "connected-idle":"busy"))),
1138                 pCoexSta->btRssi, pCoexSta->btRetryCnt);
1139         CL_PRINTF(cliBuf);
1140         
1141         if(pStackInfo->bProfileNotified)
1142         {                       
1143                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP", \
1144                         pStackInfo->bScoExist, pStackInfo->bHidExist, pStackInfo->bPanExist, pStackInfo->bA2dpExist);
1145                 CL_PRINTF(cliBuf);      
1146
1147                 pBtCoexist->btc_disp_dbg_msg(pBtCoexist, BTC_DBG_DISP_BT_LINK_INFO);
1148         }
1149
1150         btInfoExt = pCoexSta->btInfoExt;
1151         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "BT Info A2DP rate", \
1152                 (btInfoExt&BIT0)? "Basic rate":"EDR rate");
1153         CL_PRINTF(cliBuf);      
1154
1155         for(i=0; i<BT_INFO_SRC_8723A_1ANT_MAX; i++)
1156         {
1157                 if(pCoexSta->btInfoC2hCnt[i])
1158                 {                               
1159                         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)", GLBtInfoSrc8723a1Ant[i], \
1160                                 pCoexSta->btInfoC2h[i][0], pCoexSta->btInfoC2h[i][1],
1161                                 pCoexSta->btInfoC2h[i][2], pCoexSta->btInfoC2h[i][3],
1162                                 pCoexSta->btInfoC2h[i][4], pCoexSta->btInfoC2h[i][5],
1163                                 pCoexSta->btInfoC2h[i][6], pCoexSta->btInfoC2hCnt[i]);
1164                         CL_PRINTF(cliBuf);
1165                 }
1166         }
1167
1168         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "write 0x1af=0x0 num", \
1169                 pCoexSta->c2hHangDetectCnt);
1170         CL_PRINTF(cliBuf);
1171         
1172         // Sw mechanism 
1173         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Sw mechanism]============");
1174         CL_PRINTF(cliBuf);
1175         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d", "SM1[ShRf/ LpRA/ LimDig]", \
1176                 pCoexDm->bCurRfRxLpfShrink, pCoexDm->bCurLowPenaltyRa, pCoexDm->limited_dig);
1177         CL_PRINTF(cliBuf);
1178
1179         // Fw mechanism         
1180         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Fw mechanism]============");
1181         CL_PRINTF(cliBuf);      
1182         
1183         if(!pBtCoexist->manual_control)
1184         {
1185                 psTdmaCase = pCoexDm->curPsTdma;
1186                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x case-%d", "PS TDMA", \
1187                         pCoexDm->psTdmaPara[0], pCoexDm->psTdmaPara[1],
1188                         pCoexDm->psTdmaPara[2], pCoexDm->psTdmaPara[3],
1189                         pCoexDm->psTdmaPara[4], psTdmaCase);
1190                 CL_PRINTF(cliBuf);
1191         
1192                 CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "IgnWlanAct", \
1193                         pCoexDm->bCurIgnoreWlanAct);
1194                 CL_PRINTF(cliBuf);
1195         }
1196
1197         // Hw setting           
1198         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Hw setting]============");
1199         CL_PRINTF(cliBuf);      
1200
1201         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "RF-A, 0x1e initVal", \
1202                 pCoexDm->btRf0x1eBackup);
1203         CL_PRINTF(cliBuf);
1204
1205         u1Tmp[0] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x778);
1206         u1Tmp[1] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x783);
1207         u1Tmp[2] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x796);
1208         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x778/ 0x783/ 0x796", \
1209                 u1Tmp[0], u1Tmp[1], u1Tmp[2]);
1210         CL_PRINTF(cliBuf);
1211
1212         u4Tmp[0] = pBtCoexist->btc_read_4byte(pBtCoexist, 0x880);
1213         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x880", \
1214                 u4Tmp[0]);
1215         CL_PRINTF(cliBuf);
1216
1217         u1Tmp[0] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x40);
1218         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x40", \
1219                 u1Tmp[0]);
1220         CL_PRINTF(cliBuf);
1221
1222         u4Tmp[0] = pBtCoexist->btc_read_4byte(pBtCoexist, 0x550);
1223         u1Tmp[0] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x522);
1224         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522", \
1225                 u4Tmp[0], u1Tmp[0]);
1226         CL_PRINTF(cliBuf);
1227
1228         u4Tmp[0] = pBtCoexist->btc_read_4byte(pBtCoexist, 0x484);
1229         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x484(rate adaptive)", \
1230                 u4Tmp[0]);
1231         CL_PRINTF(cliBuf);
1232
1233         u4Tmp[0] = pBtCoexist->btc_read_4byte(pBtCoexist, 0xc50);
1234         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0xc50(dig)", \
1235                 u4Tmp[0]);
1236         CL_PRINTF(cliBuf);
1237
1238         u4Tmp[0] = pBtCoexist->btc_read_4byte(pBtCoexist, 0xda0);
1239         u4Tmp[1] = pBtCoexist->btc_read_4byte(pBtCoexist, 0xda4);
1240         u4Tmp[2] = pBtCoexist->btc_read_4byte(pBtCoexist, 0xda8);
1241         u4Tmp[3] = pBtCoexist->btc_read_4byte(pBtCoexist, 0xdac);
1242         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0xda0/0xda4/0xda8/0xdac(FA cnt)", \
1243                 u4Tmp[0], u4Tmp[1], u4Tmp[2], u4Tmp[3]);
1244         CL_PRINTF(cliBuf);
1245
1246         u4Tmp[0] = pBtCoexist->btc_read_4byte(pBtCoexist, 0x6c0);
1247         u4Tmp[1] = pBtCoexist->btc_read_4byte(pBtCoexist, 0x6c4);
1248         u4Tmp[2] = pBtCoexist->btc_read_4byte(pBtCoexist, 0x6c8);
1249         u1Tmp[0] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x6cc);
1250         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)", \
1251                 u4Tmp[0], u4Tmp[1], u4Tmp[2], u1Tmp[0]);
1252         CL_PRINTF(cliBuf);
1253
1254         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d", "0x770 (hp rx[31:16]/tx[15:0])", \
1255                 pCoexSta->highPriorityRx, pCoexSta->highPriorityTx);
1256         CL_PRINTF(cliBuf);
1257         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d", "0x774(lp rx[31:16]/tx[15:0])", \
1258                 pCoexSta->lowPriorityRx, pCoexSta->lowPriorityTx);
1259         CL_PRINTF(cliBuf);
1260
1261         // Tx mgnt queue hang or not, 0x41b should = 0xf, ex: 0xd ==>hang
1262         u1Tmp[0] = pBtCoexist->btc_read_1byte(pBtCoexist, 0x41b);
1263         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x41b (mgntQ hang chk == 0xf)", \
1264                 u1Tmp[0]);
1265         CL_PRINTF(cliBuf);      
1266
1267         pBtCoexist->btc_disp_dbg_msg(pBtCoexist, BTC_DBG_DISP_COEX_STATISTICS);
1268 }
1269
1270
1271 VOID
1272 EXhalbtc8723a1ant_IpsNotify(
1273         IN      PBTC_COEXIST            pBtCoexist,
1274         IN      u1Byte                  type
1275         )
1276 {
1277         if(BTC_IPS_ENTER == type)
1278         {
1279                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], IPS ENTER notify\n"));
1280                 halbtc8723a1ant_CoexAllOff(pBtCoexist);
1281         }
1282         else if(BTC_IPS_LEAVE == type)
1283         {
1284                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], IPS LEAVE notify\n"));
1285                 //halbtc8723a1ant_InitCoexDm(pBtCoexist);
1286         }
1287 }
1288
1289 VOID
1290 EXhalbtc8723a1ant_LpsNotify(
1291         IN      PBTC_COEXIST            pBtCoexist,
1292         IN      u1Byte                  type
1293         )
1294 {
1295         if(BTC_LPS_ENABLE == type)
1296         {
1297                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], LPS ENABLE notify\n"));
1298         }
1299         else if(BTC_LPS_DISABLE == type)
1300         {
1301                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], LPS DISABLE notify\n"));
1302                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 8);
1303         }
1304 }
1305
1306 VOID
1307 EXhalbtc8723a1ant_ScanNotify(
1308         IN      PBTC_COEXIST            pBtCoexist,
1309         IN      u1Byte                  type
1310         )
1311 {
1312         BOOLEAN         bWifiConnected=FALSE;
1313         
1314         halbtc8723a1ant_NotifyFwScan(pBtCoexist, type);
1315
1316         if(pBtCoexist->btInfo.bBtDisabled)
1317         {
1318                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 9); 
1319         }
1320         else
1321         {
1322                 pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
1323                 if(BTC_SCAN_START == type)
1324                 {
1325                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN START notify\n"));
1326                         if(!bWifiConnected)     // non-connected scan
1327                         {
1328                                 //set 0x550[3]=1 before PsTdma
1329                                 halbtc8723a1ant_Reg0x550Bit3(pBtCoexist, true);
1330                         }
1331
1332                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 4);
1333                 }
1334                 else if(BTC_SCAN_FINISH == type)
1335                 {
1336                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], SCAN FINISH notify\n"));
1337                         if(!bWifiConnected)     // non-connected scan
1338                         {
1339                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0); 
1340                         }
1341                         else
1342                         {
1343                                 halbtc8723a1ant_CoexForWifiConnect(pBtCoexist);
1344                         }
1345                 }
1346         }
1347 }
1348
1349 VOID
1350 EXhalbtc8723a1ant_ConnectNotify(
1351         IN      PBTC_COEXIST            pBtCoexist,
1352         IN      u1Byte                  type
1353         )
1354 {
1355         BOOLEAN         bWifiConnected=FALSE;
1356                 
1357         if(pBtCoexist->btInfo.bBtDisabled)
1358         {
1359                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 9); 
1360         }
1361         else
1362         {
1363                 if(BTC_ASSOCIATE_START == type)
1364                 {
1365                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT START notify\n"));
1366                         //set 0x550[3]=1 before PsTdma
1367                         halbtc8723a1ant_Reg0x550Bit3(pBtCoexist, true);
1368                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 8);       // extend wifi slot     
1369                 }
1370                 else if(BTC_ASSOCIATE_FINISH == type)
1371                 {
1372                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], CONNECT FINISH notify\n"));
1373                         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
1374                         if(!bWifiConnected)     // non-connected scan
1375                         {
1376                                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0);
1377                         }
1378                         else
1379                         {
1380                                 halbtc8723a1ant_CoexForWifiConnect(pBtCoexist);
1381                         }
1382                 }
1383         }
1384 }
1385
1386 VOID
1387 EXhalbtc8723a1ant_MediaStatusNotify(
1388         IN      PBTC_COEXIST                    pBtCoexist,
1389         IN      u1Byte                          type
1390         )
1391 {
1392         if(BTC_MEDIA_CONNECT == type)
1393         {
1394                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], MEDIA connect notify\n"));
1395         }
1396         else
1397         {
1398                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], MEDIA disconnect notify\n"));
1399         }
1400 }
1401
1402 VOID
1403 EXhalbtc8723a1ant_SpecialPacketNotify(
1404         IN      PBTC_COEXIST                    pBtCoexist,
1405         IN      u1Byte                          type
1406         )
1407 {
1408         if(type == BTC_PACKET_DHCP)
1409         {
1410                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], DHCP Packet notify\n"));
1411                 if(pBtCoexist->btInfo.bBtDisabled)
1412                 {
1413                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 9);      
1414                 }
1415                 else
1416                 {
1417                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, true, 18);
1418                 }               
1419         }
1420 }
1421
1422 VOID
1423 EXhalbtc8723a1ant_BtInfoNotify(
1424         IN      PBTC_COEXIST            pBtCoexist,
1425         IN      pu1Byte                 tmpBuf,
1426         IN      u1Byte                  length
1427         )
1428 {
1429         u1Byte                  btInfo=0;
1430         u1Byte                  i, rspSource=0;
1431         BOOLEAN                 bBtHsOn=FALSE, bBtBusy=FALSE, bForceLps=FALSE;
1432
1433         pCoexSta->bC2hBtInfoReqSent = FALSE;
1434         
1435         rspSource = BT_INFO_SRC_8723A_1ANT_BT_RSP;
1436         pCoexSta->btInfoC2hCnt[rspSource]++;
1437
1438         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], Bt info[%d], length=%d, hex data=[", rspSource, length));
1439         for(i=0; i<length; i++)
1440         {
1441                 pCoexSta->btInfoC2h[rspSource][i] = tmpBuf[i];
1442                 if(i == 0)
1443                         btInfo = tmpBuf[i];
1444                 if(i == length-1)
1445                 {
1446                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("0x%02x]\n", tmpBuf[i]));
1447                 }
1448                 else
1449                 {
1450                         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("0x%02x, ", tmpBuf[i]));
1451                 }
1452         }
1453
1454         if(BT_INFO_SRC_8723A_1ANT_WIFI_FW != rspSource)
1455         {
1456                 pCoexSta->btRetryCnt =
1457                         pCoexSta->btInfoC2h[rspSource][1];
1458
1459                 pCoexSta->btRssi =
1460                         pCoexSta->btInfoC2h[rspSource][2]*2+10;
1461
1462                 pCoexSta->btInfoExt = 
1463                         pCoexSta->btInfoC2h[rspSource][3];
1464         }
1465                 
1466         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_HS_OPERATION, &bBtHsOn);
1467         // check BIT2 first ==> check if bt is under inquiry or page scan
1468         if(btInfo & BT_INFO_8723A_1ANT_B_INQ_PAGE)
1469         {
1470                 pCoexSta->bC2hBtInquiryPage = true;
1471         }
1472         else
1473         {
1474                 pCoexSta->bC2hBtInquiryPage = FALSE;
1475         }
1476         btInfo &= ~BIT2;
1477         if(!(btInfo & BIT0))
1478         {
1479                 pCoexDm->btStatus = BT_STATE_8723A_1ANT_NO_CONNECTION;
1480                 bForceLps = FALSE;
1481         }
1482         else
1483         {
1484                 bForceLps = true;
1485                 if(btInfo == 0x1)
1486                 {
1487                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_CONNECT_IDLE;
1488                 }
1489                 else if(btInfo == 0x9)
1490                 {
1491                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_ACL_ONLY_BUSY;
1492                         bBtBusy = true;
1493                 }
1494                 else if(btInfo == 0x13)
1495                 {
1496                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_SCO_ONLY_BUSY;
1497                         bBtBusy = true;
1498                 }
1499                 else if(btInfo == 0x1b)
1500                 {
1501                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_ACL_SCO_BUSY;
1502                         bBtBusy = true;
1503                 }
1504                 else if(btInfo == 0x29)
1505                 {
1506                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_HID_BUSY;
1507                         bBtBusy = true;
1508                 }
1509                 else if(btInfo == 0x3b)
1510                 {
1511                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_HID_SCO_BUSY;
1512                         bBtBusy = true;
1513                 }
1514         }
1515         pBtCoexist->btc_set(pBtCoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bBtBusy);
1516         pBtCoexist->btc_set(pBtCoexist, BTC_SET_BL_BT_LIMITED_DIG, &bBtBusy);
1517         if(bForceLps)
1518                 pBtCoexist->btc_set(pBtCoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1519         else
1520                 pBtCoexist->btc_set(pBtCoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1521
1522         if( (BT_STATE_8723A_1ANT_NO_CONNECTION == pCoexDm->btStatus) ||
1523                 (BT_STATE_8723A_1ANT_CONNECT_IDLE == pCoexDm->btStatus) )
1524         {
1525                 if(pCoexSta->bC2hBtInquiryPage)
1526                         pCoexDm->btStatus = BT_STATE_8723A_1ANT_INQ_OR_PAG;
1527         }
1528 }
1529
1530 VOID
1531 EXhalbtc8723a1ant_StackOperationNotify(
1532         IN      PBTC_COEXIST                    pBtCoexist,
1533         IN      u1Byte                          type
1534         )
1535 {
1536         if(BTC_STACK_OP_INQ_PAGE_PAIR_START == type)
1537         {
1538                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], StackOP Inquiry/page/pair start notify\n"));
1539         }
1540         else if(BTC_STACK_OP_INQ_PAGE_PAIR_FINISH == type)
1541         {
1542                 BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], StackOP Inquiry/page/pair finish notify\n"));
1543         }
1544 }
1545
1546 VOID
1547 EXhalbtc8723a1ant_HaltNotify(
1548         IN      PBTC_COEXIST                    pBtCoexist
1549         )
1550 {
1551         halbtc8723a1ant_PsTdma(pBtCoexist, FORCE_EXEC, FALSE, 0);
1552         
1553         halbtc8723a1ant_LowPenaltyRa(pBtCoexist, FORCE_EXEC, FALSE);
1554         halbtc8723a1ant_RfShrink(pBtCoexist, FORCE_EXEC, FALSE);
1555
1556         halbtc8723a1ant_IgnoreWlanAct(pBtCoexist, FORCE_EXEC, true);
1557         EXhalbtc8723a1ant_MediaStatusNotify(pBtCoexist, BTC_MEDIA_DISCONNECT);
1558 }
1559
1560 VOID
1561 EXhalbtc8723a1ant_Periodical(
1562         IN      PBTC_COEXIST                    pBtCoexist
1563         )
1564 {
1565         BOOLEAN         bScan=FALSE, bLink=FALSE, bRoam=FALSE, bWifiConnected=FALSE;
1566         
1567         BTC_PRINT(BTC_MSG_INTERFACE, INTF_NOTIFY, ("[BTCoex], 1Ant Periodical!!\n"));
1568         
1569         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan);
1570         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink);
1571         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam);
1572         pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_CONNECTED, &bWifiConnected);
1573
1574         // work around for c2h hang
1575         wa_halbtc8723a1ant_MonitorC2h(pBtCoexist);      
1576
1577         halbtc8723a1ant_QueryBtInfo(pBtCoexist);
1578         halbtc8723a1ant_MonitorBtCtr(pBtCoexist);
1579         halbtc8723a1ant_MonitorBtEnableDisable(pBtCoexist);
1580
1581         
1582         if(bScan)
1583                 return;
1584         if(bLink)
1585                 return;
1586
1587         if(bWifiConnected)
1588         {
1589                 if(pBtCoexist->btInfo.bBtDisabled)
1590                 {
1591                         halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 9);
1592                         
1593                         halbtc8723a1ant_LowPenaltyRa(pBtCoexist, NORMAL_EXEC, FALSE);
1594                         halbtc8723a1ant_RfShrink(pBtCoexist, NORMAL_EXEC, FALSE);
1595                 }
1596                 else
1597                 {
1598                         halbtc8723a1ant_LowPenaltyRa(pBtCoexist, NORMAL_EXEC, true);
1599                         halbtc8723a1ant_RfShrink(pBtCoexist, NORMAL_EXEC, FALSE);
1600                         halbtc8723a1ant_CoexForWifiConnect(pBtCoexist);
1601                 }
1602         }
1603         else
1604         {
1605                 halbtc8723a1ant_PsTdma(pBtCoexist, NORMAL_EXEC, FALSE, 0);
1606
1607                 halbtc8723a1ant_LowPenaltyRa(pBtCoexist, NORMAL_EXEC, FALSE);
1608                 halbtc8723a1ant_RfShrink(pBtCoexist, NORMAL_EXEC, FALSE);
1609         }
1610 }
1611
1612
1613 #endif
1614