GNU Linux-libre 4.19.314-gnu1
[releases.git] / drivers / staging / rtl8723bs / hal / hal_btcoex.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2013 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define __HAL_BTCOEX_C__
8
9 #include <hal_data.h>
10 #include <rtw_debug.h>
11 #include <hal_btcoex.h>
12 #include <Mp_Precomp.h>
13
14 /*              Global variables */
15 static const char *const BtProfileString[] = {
16         "NONE",
17         "A2DP",
18         "PAN",
19         "HID",
20         "SCO",
21 };
22
23 static const char *const BtSpecString[] = {
24         "1.0b",
25         "1.1",
26         "1.2",
27         "2.0+EDR",
28         "2.1+EDR",
29         "3.0+HS",
30         "4.0",
31 };
32
33 static const char *const BtLinkRoleString[] = {
34         "Master",
35         "Slave",
36 };
37
38 static const char *const h2cStaString[] = {
39         "successful",
40         "h2c busy",
41         "rf off",
42         "fw not read",
43 };
44
45 static const char *const ioStaString[] = {
46         "success",
47         "can not IO",
48         "rf off",
49         "fw not read",
50         "wait io timeout",
51         "invalid len",
52         "idle Q empty",
53         "insert waitQ fail",
54         "unknown fail",
55         "wrong level",
56         "h2c stopped",
57 };
58
59 BTC_COEXIST GLBtCoexist;
60 static u8 GLBtcWiFiInScanState;
61 static u8 GLBtcWiFiInIQKState;
62
63 u32 GLBtcDbgType[BTC_MSG_MAX];
64 static u8 GLBtcDbgBuf[BT_TMP_BUF_SIZE];
65
66 typedef struct _btcoexdbginfo {
67         u8 *info;
68         u32 size; /*  buffer total size */
69         u32 len; /*  now used length */
70 } BTCDBGINFO, *PBTCDBGINFO;
71
72 static BTCDBGINFO GLBtcDbgInfo;
73
74 #define BT_Operation(Adapter)                                           false
75
76 static void DBG_BT_INFO_INIT(PBTCDBGINFO pinfo, u8 *pbuf, u32 size)
77 {
78         if (NULL == pinfo)
79                 return;
80
81         memset(pinfo, 0, sizeof(BTCDBGINFO));
82
83         if (pbuf && size) {
84                 pinfo->info = pbuf;
85                 pinfo->size = size;
86         }
87 }
88
89 void DBG_BT_INFO(u8 *dbgmsg)
90 {
91         PBTCDBGINFO pinfo;
92         u32 msglen;
93         u8 *pbuf;
94
95
96         pinfo = &GLBtcDbgInfo;
97
98         if (NULL == pinfo->info)
99                 return;
100
101         msglen = strlen(dbgmsg);
102         if (pinfo->len + msglen > pinfo->size)
103                 return;
104
105         pbuf = pinfo->info + pinfo->len;
106         memcpy(pbuf, dbgmsg, msglen);
107         pinfo->len += msglen;
108 }
109
110 /*  */
111 /*              Debug related function */
112 /*  */
113 static u8 halbtcoutsrc_IsBtCoexistAvailable(PBTC_COEXIST pBtCoexist)
114 {
115         if (!pBtCoexist->bBinded ||
116                 NULL == pBtCoexist->Adapter){
117                 return false;
118         }
119         return true;
120 }
121
122 static void halbtcoutsrc_DbgInit(void)
123 {
124         u8 i;
125
126         for (i = 0; i < BTC_MSG_MAX; i++)
127                 GLBtcDbgType[i] = 0;
128
129         GLBtcDbgType[BTC_MSG_INTERFACE]                 =       \
130 /*                      INTF_INIT                                                               | */
131 /*                      INTF_NOTIFY                                                     | */
132                         0;
133
134         GLBtcDbgType[BTC_MSG_ALGORITHM]                 =       \
135 /*                      ALGO_BT_RSSI_STATE                                      | */
136 /*                      ALGO_WIFI_RSSI_STATE                                    | */
137 /*                      ALGO_BT_MONITOR                                         | */
138 /*                      ALGO_TRACE                                                      | */
139 /*                      ALGO_TRACE_FW                                           | */
140 /*                      ALGO_TRACE_FW_DETAIL                            | */
141 /*                      ALGO_TRACE_FW_EXEC                                      | */
142 /*                      ALGO_TRACE_SW                                           | */
143 /*                      ALGO_TRACE_SW_DETAIL                            | */
144 /*                      ALGO_TRACE_SW_EXEC                                      | */
145                         0;
146 }
147
148 static void halbtcoutsrc_LeaveLps(PBTC_COEXIST pBtCoexist)
149 {
150         struct adapter *padapter;
151
152
153         padapter = pBtCoexist->Adapter;
154
155         pBtCoexist->btInfo.bBtCtrlLps = true;
156         pBtCoexist->btInfo.bBtLpsOn = false;
157
158         rtw_btcoex_LPS_Leave(padapter);
159 }
160
161 static void halbtcoutsrc_EnterLps(PBTC_COEXIST pBtCoexist)
162 {
163         struct adapter *padapter;
164
165
166         padapter = pBtCoexist->Adapter;
167
168         pBtCoexist->btInfo.bBtCtrlLps = true;
169         pBtCoexist->btInfo.bBtLpsOn = true;
170
171         rtw_btcoex_LPS_Enter(padapter);
172 }
173
174 static void halbtcoutsrc_NormalLps(PBTC_COEXIST pBtCoexist)
175 {
176         struct adapter *padapter;
177
178
179         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, ("[BTCoex], Normal LPS behavior!!!\n"));
180
181         padapter = pBtCoexist->Adapter;
182
183         if (pBtCoexist->btInfo.bBtCtrlLps) {
184                 pBtCoexist->btInfo.bBtLpsOn = false;
185                 rtw_btcoex_LPS_Leave(padapter);
186                 pBtCoexist->btInfo.bBtCtrlLps = false;
187
188                 /*  recover the LPS state to the original */
189         }
190 }
191
192 /*
193  *  Constraint:
194  *   1. this function will request pwrctrl->lock
195  */
196 static void halbtcoutsrc_LeaveLowPower(PBTC_COEXIST pBtCoexist)
197 {
198         struct adapter *padapter;
199         struct hal_com_data *pHalData;
200         s32 ready;
201         unsigned long stime;
202         unsigned long utime;
203         u32 timeout; /*  unit: ms */
204
205
206         padapter = pBtCoexist->Adapter;
207         pHalData = GET_HAL_DATA(padapter);
208         ready = _FAIL;
209 #ifdef LPS_RPWM_WAIT_MS
210         timeout = LPS_RPWM_WAIT_MS;
211 #else /*  !LPS_RPWM_WAIT_MS */
212         timeout = 30;
213 #endif /*  !LPS_RPWM_WAIT_MS */
214
215         stime = jiffies;
216         do {
217                 ready = rtw_register_task_alive(padapter, BTCOEX_ALIVE);
218                 if (_SUCCESS == ready)
219                         break;
220
221                 utime = jiffies_to_msecs(jiffies - stime);
222                 if (utime > timeout)
223                         break;
224
225                 msleep(1);
226         } while (1);
227 }
228
229 /*
230  *  Constraint:
231  *   1. this function will request pwrctrl->lock
232  */
233 static void halbtcoutsrc_NormalLowPower(PBTC_COEXIST pBtCoexist)
234 {
235         struct adapter *padapter;
236
237
238         padapter = pBtCoexist->Adapter;
239         rtw_unregister_task_alive(padapter, BTCOEX_ALIVE);
240 }
241
242 static void halbtcoutsrc_DisableLowPower(PBTC_COEXIST pBtCoexist, u8 bLowPwrDisable)
243 {
244         pBtCoexist->btInfo.bBtDisableLowPwr = bLowPwrDisable;
245         if (bLowPwrDisable)
246                 halbtcoutsrc_LeaveLowPower(pBtCoexist);         /*  leave 32k low power. */
247         else
248                 halbtcoutsrc_NormalLowPower(pBtCoexist);        /*  original 32k low power behavior. */
249 }
250
251 static void halbtcoutsrc_AggregationCheck(PBTC_COEXIST pBtCoexist)
252 {
253         struct adapter *padapter;
254         bool bNeedToAct;
255
256
257         padapter = pBtCoexist->Adapter;
258         bNeedToAct = false;
259
260         if (pBtCoexist->btInfo.bRejectAggPkt)
261                 rtw_btcoex_RejectApAggregatedPacket(padapter, true);
262         else{
263
264                 if (pBtCoexist->btInfo.bPreBtCtrlAggBufSize !=
265                         pBtCoexist->btInfo.bBtCtrlAggBufSize){
266
267                         bNeedToAct = true;
268                         pBtCoexist->btInfo.bPreBtCtrlAggBufSize = pBtCoexist->btInfo.bBtCtrlAggBufSize;
269                 }
270
271                 if (pBtCoexist->btInfo.bBtCtrlAggBufSize) {
272                         if (pBtCoexist->btInfo.preAggBufSize !=
273                                 pBtCoexist->btInfo.aggBufSize){
274                                 bNeedToAct = true;
275                         }
276                         pBtCoexist->btInfo.preAggBufSize = pBtCoexist->btInfo.aggBufSize;
277                 }
278
279                 if (bNeedToAct) {
280                         rtw_btcoex_RejectApAggregatedPacket(padapter, true);
281                         rtw_btcoex_RejectApAggregatedPacket(padapter, false);
282                 }
283         }
284 }
285
286 static u8 halbtcoutsrc_IsWifiBusy(struct adapter *padapter)
287 {
288         struct mlme_priv *pmlmepriv;
289
290
291         pmlmepriv = &padapter->mlmepriv;
292
293         if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == true) {
294                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
295                         return true;
296                 if (true == pmlmepriv->LinkDetectInfo.bBusyTraffic)
297                         return true;
298         }
299
300         return false;
301 }
302
303 static u32 _halbtcoutsrc_GetWifiLinkStatus(struct adapter *padapter)
304 {
305         struct mlme_priv *pmlmepriv;
306         u8 bp2p;
307         u32 portConnectedStatus;
308
309
310         pmlmepriv = &padapter->mlmepriv;
311         bp2p = false;
312         portConnectedStatus = 0;
313
314         if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == true) {
315                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
316                         if (true == bp2p)
317                                 portConnectedStatus |= WIFI_P2P_GO_CONNECTED;
318                         else
319                                 portConnectedStatus |= WIFI_AP_CONNECTED;
320                 } else {
321                         if (true == bp2p)
322                                 portConnectedStatus |= WIFI_P2P_GC_CONNECTED;
323                         else
324                                 portConnectedStatus |= WIFI_STA_CONNECTED;
325                 }
326         }
327
328         return portConnectedStatus;
329 }
330
331 static u32 halbtcoutsrc_GetWifiLinkStatus(PBTC_COEXIST pBtCoexist)
332 {
333         /*  */
334         /*  return value: */
335         /*  [31:16]=> connected port number */
336         /*  [15:0]=> port connected bit define */
337         /*  */
338
339         struct adapter *padapter;
340         u32 retVal;
341         u32 portConnectedStatus, numOfConnectedPort;
342
343
344         padapter = pBtCoexist->Adapter;
345         portConnectedStatus = 0;
346         numOfConnectedPort = 0;
347
348         retVal = _halbtcoutsrc_GetWifiLinkStatus(padapter);
349         if (retVal) {
350                 portConnectedStatus |= retVal;
351                 numOfConnectedPort++;
352         }
353
354         retVal = (numOfConnectedPort << 16) | portConnectedStatus;
355
356         return retVal;
357 }
358
359 static u32 halbtcoutsrc_GetBtPatchVer(PBTC_COEXIST pBtCoexist)
360 {
361         return pBtCoexist->btInfo.btRealFwVer;
362 }
363
364 static s32 halbtcoutsrc_GetWifiRssi(struct adapter *padapter)
365 {
366         struct hal_com_data *pHalData;
367         s32 UndecoratedSmoothedPWDB = 0;
368
369
370         pHalData = GET_HAL_DATA(padapter);
371
372         UndecoratedSmoothedPWDB = pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB;
373
374         return UndecoratedSmoothedPWDB;
375 }
376
377 static u8 halbtcoutsrc_GetWifiScanAPNum(struct adapter *padapter)
378 {
379         struct mlme_ext_priv *pmlmeext;
380         static u8 scan_AP_num;
381
382         pmlmeext = &padapter->mlmeextpriv;
383
384         if (GLBtcWiFiInScanState == false) {
385                 if (pmlmeext->sitesurvey_res.bss_cnt > 0xFF)
386                         scan_AP_num = 0xFF;
387                 else
388                         scan_AP_num = (u8)pmlmeext->sitesurvey_res.bss_cnt;
389         }
390
391         return scan_AP_num;
392 }
393
394 static u8 halbtcoutsrc_Get(void *pBtcContext, u8 getType, void *pOutBuf)
395 {
396         PBTC_COEXIST pBtCoexist;
397         struct adapter *padapter;
398         struct hal_com_data *pHalData;
399         struct mlme_ext_priv *mlmeext;
400         u8 *pu8;
401         s32 *pS4Tmp;
402         u32 *pU4Tmp;
403         u8 *pU1Tmp;
404         u8 ret;
405
406
407         pBtCoexist = (PBTC_COEXIST)pBtcContext;
408         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
409                 return false;
410
411         padapter = pBtCoexist->Adapter;
412         pHalData = GET_HAL_DATA(padapter);
413         mlmeext = &padapter->mlmeextpriv;
414         pu8 = pOutBuf;
415         pS4Tmp = pOutBuf;
416         pU4Tmp = pOutBuf;
417         pU1Tmp = pOutBuf;
418         ret = true;
419
420         switch (getType) {
421         case BTC_GET_BL_HS_OPERATION:
422                 *pu8 = false;
423                 ret = false;
424                 break;
425
426         case BTC_GET_BL_HS_CONNECTING:
427                 *pu8 = false;
428                 ret = false;
429                 break;
430
431         case BTC_GET_BL_WIFI_CONNECTED:
432                 *pu8 = check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE);
433                 break;
434
435         case BTC_GET_BL_WIFI_BUSY:
436                 *pu8 = halbtcoutsrc_IsWifiBusy(padapter);
437                 break;
438
439         case BTC_GET_BL_WIFI_SCAN:
440                 /* Use the value of the new variable GLBtcWiFiInScanState to judge whether WiFi is in scan state or not, since the originally used flag
441                         WIFI_SITE_MONITOR in fwstate may not be cleared in time */
442                 *pu8 = GLBtcWiFiInScanState;
443                 break;
444
445         case BTC_GET_BL_WIFI_LINK:
446                 *pu8 = check_fwstate(&padapter->mlmepriv, WIFI_UNDER_LINKING);
447                 break;
448
449         case BTC_GET_BL_WIFI_ROAM:
450                 *pu8 = check_fwstate(&padapter->mlmepriv, WIFI_UNDER_LINKING);
451                 break;
452
453         case BTC_GET_BL_WIFI_4_WAY_PROGRESS:
454                 *pu8 = false;
455                 break;
456
457         case BTC_GET_BL_WIFI_UNDER_5G:
458                 *pu8 = pHalData->CurrentBandType == 1;
459                 break;
460
461         case BTC_GET_BL_WIFI_AP_MODE_ENABLE:
462                 *pu8 = check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE);
463                 break;
464
465         case BTC_GET_BL_WIFI_ENABLE_ENCRYPTION:
466                 *pu8 = padapter->securitypriv.dot11PrivacyAlgrthm == 0 ? false : true;
467                 break;
468
469         case BTC_GET_BL_WIFI_UNDER_B_MODE:
470                 if (mlmeext->cur_wireless_mode == WIRELESS_11B)
471                         *pu8 = true;
472                 else
473                         *pu8 = false;
474                 break;
475
476         case BTC_GET_BL_WIFI_IS_IN_MP_MODE:
477                 *pu8 = false;
478                 break;
479
480         case BTC_GET_BL_EXT_SWITCH:
481                 *pu8 = false;
482                 break;
483
484         case BTC_GET_S4_WIFI_RSSI:
485                 *pS4Tmp = halbtcoutsrc_GetWifiRssi(padapter);
486                 break;
487
488         case BTC_GET_S4_HS_RSSI:
489                 *pS4Tmp = 0;
490                 ret = false;
491                 break;
492
493         case BTC_GET_U4_WIFI_BW:
494                 if (IsLegacyOnly(mlmeext->cur_wireless_mode))
495                         *pU4Tmp = BTC_WIFI_BW_LEGACY;
496                 else if (pHalData->CurrentChannelBW == CHANNEL_WIDTH_20)
497                         *pU4Tmp = BTC_WIFI_BW_HT20;
498                 else if (pHalData->CurrentChannelBW == CHANNEL_WIDTH_40)
499                         *pU4Tmp = BTC_WIFI_BW_HT40;
500                 else
501                         *pU4Tmp = BTC_WIFI_BW_HT40; /* todo */
502                 break;
503
504         case BTC_GET_U4_WIFI_TRAFFIC_DIRECTION:
505                 {
506                         PRT_LINK_DETECT_T plinkinfo;
507                         plinkinfo = &padapter->mlmepriv.LinkDetectInfo;
508
509                         if (plinkinfo->NumTxOkInPeriod > plinkinfo->NumRxOkInPeriod)
510                                 *pU4Tmp = BTC_WIFI_TRAFFIC_TX;
511                         else
512                                 *pU4Tmp = BTC_WIFI_TRAFFIC_RX;
513                 }
514                 break;
515
516         case BTC_GET_U4_WIFI_FW_VER:
517                 *pU4Tmp = pHalData->FirmwareVersion << 16;
518                 *pU4Tmp |= pHalData->FirmwareSubVersion;
519                 break;
520
521         case BTC_GET_U4_WIFI_LINK_STATUS:
522                 *pU4Tmp = halbtcoutsrc_GetWifiLinkStatus(pBtCoexist);
523                 break;
524
525         case BTC_GET_U4_BT_PATCH_VER:
526                 *pU4Tmp = halbtcoutsrc_GetBtPatchVer(pBtCoexist);
527                 break;
528
529         case BTC_GET_U1_WIFI_DOT11_CHNL:
530                 *pU1Tmp = padapter->mlmeextpriv.cur_channel;
531                 break;
532
533         case BTC_GET_U1_WIFI_CENTRAL_CHNL:
534                 *pU1Tmp = pHalData->CurrentChannel;
535                 break;
536
537         case BTC_GET_U1_WIFI_HS_CHNL:
538                 *pU1Tmp = 0;
539                 ret = false;
540                 break;
541
542         case BTC_GET_U1_MAC_PHY_MODE:
543                 *pU1Tmp = BTC_SMSP;
544 /*                      *pU1Tmp = BTC_DMSP; */
545 /*                      *pU1Tmp = BTC_DMDP; */
546 /*                      *pU1Tmp = BTC_MP_UNKNOWN; */
547                 break;
548
549         case BTC_GET_U1_AP_NUM:
550                 *pU1Tmp = halbtcoutsrc_GetWifiScanAPNum(padapter);
551                 break;
552
553         /* 1Ant =========== */
554         case BTC_GET_U1_LPS_MODE:
555                 *pU1Tmp = padapter->dvobj->pwrctl_priv.pwr_mode;
556                 break;
557
558         default:
559                 ret = false;
560                 break;
561         }
562
563         return ret;
564 }
565
566 static u8 halbtcoutsrc_Set(void *pBtcContext, u8 setType, void *pInBuf)
567 {
568         PBTC_COEXIST pBtCoexist;
569         struct adapter *padapter;
570         struct hal_com_data *pHalData;
571         u8 *pu8;
572         u8 *pU1Tmp;
573         u32 *pU4Tmp;
574         u8 ret;
575
576
577         pBtCoexist = (PBTC_COEXIST)pBtcContext;
578         padapter = pBtCoexist->Adapter;
579         pHalData = GET_HAL_DATA(padapter);
580         pu8 = pInBuf;
581         pU1Tmp = pInBuf;
582         pU4Tmp = pInBuf;
583         ret = true;
584
585         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
586                 return false;
587
588         switch (setType) {
589         /*  set some u8 type variables. */
590         case BTC_SET_BL_BT_DISABLE:
591                 pBtCoexist->btInfo.bBtDisabled = *pu8;
592                 break;
593
594         case BTC_SET_BL_BT_TRAFFIC_BUSY:
595                 pBtCoexist->btInfo.bBtBusy = *pu8;
596                 break;
597
598         case BTC_SET_BL_BT_LIMITED_DIG:
599                 pBtCoexist->btInfo.bLimitedDig = *pu8;
600                 break;
601
602         case BTC_SET_BL_FORCE_TO_ROAM:
603                 pBtCoexist->btInfo.bForceToRoam = *pu8;
604                 break;
605
606         case BTC_SET_BL_TO_REJ_AP_AGG_PKT:
607                 pBtCoexist->btInfo.bRejectAggPkt = *pu8;
608                 break;
609
610         case BTC_SET_BL_BT_CTRL_AGG_SIZE:
611                 pBtCoexist->btInfo.bBtCtrlAggBufSize = *pu8;
612                 break;
613
614         case BTC_SET_BL_INC_SCAN_DEV_NUM:
615                 pBtCoexist->btInfo.bIncreaseScanDevNum = *pu8;
616                 break;
617
618         case BTC_SET_BL_BT_TX_RX_MASK:
619                 pBtCoexist->btInfo.bBtTxRxMask = *pu8;
620                 break;
621
622         /*  set some u8 type variables. */
623         case BTC_SET_U1_RSSI_ADJ_VAL_FOR_AGC_TABLE_ON:
624                 pBtCoexist->btInfo.rssiAdjustForAgcTableOn = *pU1Tmp;
625                 break;
626
627         case BTC_SET_U1_AGG_BUF_SIZE:
628                 pBtCoexist->btInfo.aggBufSize = *pU1Tmp;
629                 break;
630
631         /*  the following are some action which will be triggered */
632         case BTC_SET_ACT_GET_BT_RSSI:
633                 ret = false;
634                 break;
635
636         case BTC_SET_ACT_AGGREGATE_CTRL:
637                 halbtcoutsrc_AggregationCheck(pBtCoexist);
638                 break;
639
640         /* 1Ant =========== */
641         /*  set some u8 type variables. */
642         case BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE:
643                 pBtCoexist->btInfo.rssiAdjustFor1AntCoexType = *pU1Tmp;
644                 break;
645
646         case BTC_SET_U1_LPS_VAL:
647                 pBtCoexist->btInfo.lpsVal = *pU1Tmp;
648                 break;
649
650         case BTC_SET_U1_RPWM_VAL:
651                 pBtCoexist->btInfo.rpwmVal = *pU1Tmp;
652                 break;
653
654         /*  the following are some action which will be triggered */
655         case BTC_SET_ACT_LEAVE_LPS:
656                 halbtcoutsrc_LeaveLps(pBtCoexist);
657                 break;
658
659         case BTC_SET_ACT_ENTER_LPS:
660                 halbtcoutsrc_EnterLps(pBtCoexist);
661                 break;
662
663         case BTC_SET_ACT_NORMAL_LPS:
664                 halbtcoutsrc_NormalLps(pBtCoexist);
665                 break;
666
667         case BTC_SET_ACT_DISABLE_LOW_POWER:
668                 halbtcoutsrc_DisableLowPower(pBtCoexist, *pu8);
669                 break;
670
671         case BTC_SET_ACT_UPDATE_RAMASK:
672                 pBtCoexist->btInfo.raMask = *pU4Tmp;
673
674                 if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE) == true) {
675                         struct sta_info *psta;
676                         struct wlan_bssid_ex *cur_network;
677
678                         cur_network = &padapter->mlmeextpriv.mlmext_info.network;
679                         psta = rtw_get_stainfo(&padapter->stapriv, cur_network->MacAddress);
680                         rtw_hal_update_ra_mask(psta, 0);
681                 }
682                 break;
683
684         case BTC_SET_ACT_SEND_MIMO_PS:
685                 ret = false;
686                 break;
687
688         case BTC_SET_ACT_CTRL_BT_INFO:
689                 ret = false;
690                 break;
691
692         case BTC_SET_ACT_CTRL_BT_COEX:
693                 ret = false;
694                 break;
695         case BTC_SET_ACT_CTRL_8723B_ANT:
696                 ret = false;
697                 break;
698         /*  */
699         default:
700                 ret = false;
701                 break;
702         }
703
704         return ret;
705 }
706
707 static void halbtcoutsrc_DisplayFwPwrModeCmd(PBTC_COEXIST pBtCoexist)
708 {
709         u8 *cliBuf = pBtCoexist->cliBuf;
710
711         CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x %02x ", "Power mode cmd ", \
712                 pBtCoexist->pwrModeVal[0], pBtCoexist->pwrModeVal[1],
713                 pBtCoexist->pwrModeVal[2], pBtCoexist->pwrModeVal[3],
714                 pBtCoexist->pwrModeVal[4], pBtCoexist->pwrModeVal[5]);
715         CL_PRINTF(cliBuf);
716 }
717
718 /*  */
719 /*              IO related function */
720 /*  */
721 static u8 halbtcoutsrc_Read1Byte(void *pBtcContext, u32 RegAddr)
722 {
723         PBTC_COEXIST pBtCoexist;
724         struct adapter *padapter;
725
726
727         pBtCoexist = (PBTC_COEXIST)pBtcContext;
728         padapter = pBtCoexist->Adapter;
729
730         return rtw_read8(padapter, RegAddr);
731 }
732
733 static u16 halbtcoutsrc_Read2Byte(void *pBtcContext, u32 RegAddr)
734 {
735         PBTC_COEXIST pBtCoexist;
736         struct adapter *padapter;
737
738
739         pBtCoexist = (PBTC_COEXIST)pBtcContext;
740         padapter = pBtCoexist->Adapter;
741
742         return  rtw_read16(padapter, RegAddr);
743 }
744
745 static u32 halbtcoutsrc_Read4Byte(void *pBtcContext, u32 RegAddr)
746 {
747         PBTC_COEXIST pBtCoexist;
748         struct adapter *padapter;
749
750
751         pBtCoexist = (PBTC_COEXIST)pBtcContext;
752         padapter = pBtCoexist->Adapter;
753
754         return  rtw_read32(padapter, RegAddr);
755 }
756
757 static void halbtcoutsrc_Write1Byte(void *pBtcContext, u32 RegAddr, u8 Data)
758 {
759         PBTC_COEXIST pBtCoexist;
760         struct adapter *padapter;
761
762
763         pBtCoexist = (PBTC_COEXIST)pBtcContext;
764         padapter = pBtCoexist->Adapter;
765
766         rtw_write8(padapter, RegAddr, Data);
767 }
768
769 static void halbtcoutsrc_BitMaskWrite1Byte(void *pBtcContext, u32 regAddr, u8 bitMask, u8 data1b)
770 {
771         PBTC_COEXIST pBtCoexist;
772         struct adapter *padapter;
773         u8 originalValue, bitShift;
774         u8 i;
775
776
777         pBtCoexist = (PBTC_COEXIST)pBtcContext;
778         padapter = pBtCoexist->Adapter;
779         originalValue = 0;
780         bitShift = 0;
781
782         if (bitMask != 0xFF) {
783                 originalValue = rtw_read8(padapter, regAddr);
784
785                 for (i = 0; i <= 7; i++) {
786                         if ((bitMask>>i)&0x1)
787                                 break;
788                 }
789                 bitShift = i;
790
791                 data1b = (originalValue & ~bitMask) | ((data1b << bitShift) & bitMask);
792         }
793
794         rtw_write8(padapter, regAddr, data1b);
795 }
796
797 static void halbtcoutsrc_Write2Byte(void *pBtcContext, u32 RegAddr, u16 Data)
798 {
799         PBTC_COEXIST pBtCoexist;
800         struct adapter *padapter;
801
802
803         pBtCoexist = (PBTC_COEXIST)pBtcContext;
804         padapter = pBtCoexist->Adapter;
805
806         rtw_write16(padapter, RegAddr, Data);
807 }
808
809 static void halbtcoutsrc_Write4Byte(void *pBtcContext, u32 RegAddr, u32 Data)
810 {
811         PBTC_COEXIST pBtCoexist;
812         struct adapter *padapter;
813
814
815         pBtCoexist = (PBTC_COEXIST)pBtcContext;
816         padapter = pBtCoexist->Adapter;
817
818         rtw_write32(padapter, RegAddr, Data);
819 }
820
821 static void halbtcoutsrc_WriteLocalReg1Byte(void *pBtcContext, u32 RegAddr, u8 Data)
822 {
823         PBTC_COEXIST            pBtCoexist = (PBTC_COEXIST)pBtcContext;
824         struct adapter *Adapter = pBtCoexist->Adapter;
825
826         if (BTC_INTF_SDIO == pBtCoexist->chipInterface) {
827                 rtw_write8(Adapter, SDIO_LOCAL_BASE | RegAddr, Data);
828         } else {
829                 rtw_write8(Adapter, RegAddr, Data);
830         }
831 }
832
833 static void halbtcoutsrc_SetBbReg(void *pBtcContext, u32 RegAddr, u32 BitMask, u32 Data)
834 {
835         PBTC_COEXIST pBtCoexist;
836         struct adapter *padapter;
837
838
839         pBtCoexist = (PBTC_COEXIST)pBtcContext;
840         padapter = pBtCoexist->Adapter;
841
842         PHY_SetBBReg(padapter, RegAddr, BitMask, Data);
843 }
844
845
846 static u32 halbtcoutsrc_GetBbReg(void *pBtcContext, u32 RegAddr, u32 BitMask)
847 {
848         PBTC_COEXIST pBtCoexist;
849         struct adapter *padapter;
850
851
852         pBtCoexist = (PBTC_COEXIST)pBtcContext;
853         padapter = pBtCoexist->Adapter;
854
855         return PHY_QueryBBReg(padapter, RegAddr, BitMask);
856 }
857
858 static void halbtcoutsrc_SetRfReg(void *pBtcContext, u8 eRFPath, u32 RegAddr, u32 BitMask, u32 Data)
859 {
860         PBTC_COEXIST pBtCoexist;
861         struct adapter *padapter;
862
863
864         pBtCoexist = (PBTC_COEXIST)pBtcContext;
865         padapter = pBtCoexist->Adapter;
866
867         PHY_SetRFReg(padapter, eRFPath, RegAddr, BitMask, Data);
868 }
869
870 static u32 halbtcoutsrc_GetRfReg(void *pBtcContext, u8 eRFPath, u32 RegAddr, u32 BitMask)
871 {
872         PBTC_COEXIST pBtCoexist;
873         struct adapter *padapter;
874
875
876         pBtCoexist = (PBTC_COEXIST)pBtcContext;
877         padapter = pBtCoexist->Adapter;
878
879         return PHY_QueryRFReg(padapter, eRFPath, RegAddr, BitMask);
880 }
881
882 static void halbtcoutsrc_SetBtReg(void *pBtcContext, u8 RegType, u32 RegAddr, u32 Data)
883 {
884         PBTC_COEXIST pBtCoexist;
885         struct adapter *padapter;
886         u8 CmdBuffer1[4] = {0};
887         u8 CmdBuffer2[4] = {0};
888         u8 *AddrToSet = (u8 *)&RegAddr;
889         u8 *ValueToSet = (u8 *)&Data;
890         u8 OperVer = 0;
891         u8 ReqNum = 0;
892
893         pBtCoexist = (PBTC_COEXIST)pBtcContext;
894         padapter = pBtCoexist->Adapter;
895
896         CmdBuffer1[0] |= (OperVer & 0x0f);                                              /* Set OperVer */
897         CmdBuffer1[0] |= ((ReqNum << 4) & 0xf0);                                /* Set ReqNum */
898         CmdBuffer1[1] = 0x0d;                                                                   /* Set OpCode to BT_LO_OP_WRITE_REG_VALUE */
899         CmdBuffer1[2] = ValueToSet[0];                                                  /* Set WriteRegValue */
900         rtw_hal_fill_h2c_cmd(padapter, 0x67, 4, &(CmdBuffer1[0]));
901
902         msleep(200);
903         ReqNum++;
904
905         CmdBuffer2[0] |= (OperVer & 0x0f);                                              /* Set OperVer */
906         CmdBuffer2[0] |= ((ReqNum << 4) & 0xf0);                                /* Set ReqNum */
907         CmdBuffer2[1] = 0x0c;                                                                   /* Set OpCode of BT_LO_OP_WRITE_REG_ADDR */
908         CmdBuffer2[3] = AddrToSet[0];                                                   /* Set WriteRegAddr */
909         rtw_hal_fill_h2c_cmd(padapter, 0x67, 4, &(CmdBuffer2[0]));
910 }
911
912 static u32 halbtcoutsrc_GetBtReg(void *pBtcContext, u8 RegType, u32 RegAddr)
913 {
914         /* To be implemented. Always return 0 temporarily */
915         return 0;
916 }
917
918 static void halbtcoutsrc_FillH2cCmd(void *pBtcContext, u8 elementId, u32 cmdLen, u8 *pCmdBuffer)
919 {
920         PBTC_COEXIST pBtCoexist;
921         struct adapter *padapter;
922
923
924         pBtCoexist = (PBTC_COEXIST)pBtcContext;
925         padapter = pBtCoexist->Adapter;
926
927         rtw_hal_fill_h2c_cmd(padapter, elementId, cmdLen, pCmdBuffer);
928 }
929
930 static void halbtcoutsrc_DisplayDbgMsg(void *pBtcContext, u8 dispType)
931 {
932         PBTC_COEXIST pBtCoexist;
933
934
935         pBtCoexist = (PBTC_COEXIST)pBtcContext;
936         switch (dispType) {
937         case BTC_DBG_DISP_COEX_STATISTICS:
938                 break;
939         case BTC_DBG_DISP_BT_LINK_INFO:
940                 break;
941         case BTC_DBG_DISP_FW_PWR_MODE_CMD:
942                 halbtcoutsrc_DisplayFwPwrModeCmd(pBtCoexist);
943                 break;
944         default:
945                 break;
946         }
947 }
948
949 /*  */
950 /*              Extern functions called by other module */
951 /*  */
952 static u8 EXhalbtcoutsrc_BindBtCoexWithAdapter(void *padapter)
953 {
954         PBTC_COEXIST            pBtCoexist = &GLBtCoexist;
955
956         if (pBtCoexist->bBinded)
957                 return false;
958         else
959                 pBtCoexist->bBinded = true;
960
961         pBtCoexist->statistics.cntBind++;
962
963         pBtCoexist->Adapter = padapter;
964
965         pBtCoexist->stackInfo.bProfileNotified = false;
966
967         pBtCoexist->btInfo.bBtCtrlAggBufSize = false;
968         pBtCoexist->btInfo.aggBufSize = 5;
969
970         pBtCoexist->btInfo.bIncreaseScanDevNum = false;
971
972         /*  set default antenna position to main  port */
973         pBtCoexist->boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT;
974
975         return true;
976 }
977
978 u8 EXhalbtcoutsrc_InitlizeVariables(void *padapter)
979 {
980         PBTC_COEXIST pBtCoexist = &GLBtCoexist;
981
982         /* pBtCoexist->statistics.cntBind++; */
983
984         halbtcoutsrc_DbgInit();
985
986         pBtCoexist->chipInterface = BTC_INTF_SDIO;
987
988         EXhalbtcoutsrc_BindBtCoexWithAdapter(padapter);
989
990         pBtCoexist->fBtcRead1Byte = halbtcoutsrc_Read1Byte;
991         pBtCoexist->fBtcWrite1Byte = halbtcoutsrc_Write1Byte;
992         pBtCoexist->fBtcWrite1ByteBitMask = halbtcoutsrc_BitMaskWrite1Byte;
993         pBtCoexist->fBtcRead2Byte = halbtcoutsrc_Read2Byte;
994         pBtCoexist->fBtcWrite2Byte = halbtcoutsrc_Write2Byte;
995         pBtCoexist->fBtcRead4Byte = halbtcoutsrc_Read4Byte;
996         pBtCoexist->fBtcWrite4Byte = halbtcoutsrc_Write4Byte;
997         pBtCoexist->fBtcWriteLocalReg1Byte = halbtcoutsrc_WriteLocalReg1Byte;
998
999         pBtCoexist->fBtcSetBbReg = halbtcoutsrc_SetBbReg;
1000         pBtCoexist->fBtcGetBbReg = halbtcoutsrc_GetBbReg;
1001
1002         pBtCoexist->fBtcSetRfReg = halbtcoutsrc_SetRfReg;
1003         pBtCoexist->fBtcGetRfReg = halbtcoutsrc_GetRfReg;
1004
1005         pBtCoexist->fBtcFillH2c = halbtcoutsrc_FillH2cCmd;
1006         pBtCoexist->fBtcDispDbgMsg = halbtcoutsrc_DisplayDbgMsg;
1007
1008         pBtCoexist->fBtcGet = halbtcoutsrc_Get;
1009         pBtCoexist->fBtcSet = halbtcoutsrc_Set;
1010         pBtCoexist->fBtcGetBtReg = halbtcoutsrc_GetBtReg;
1011         pBtCoexist->fBtcSetBtReg = halbtcoutsrc_SetBtReg;
1012
1013         pBtCoexist->cliBuf = &GLBtcDbgBuf[0];
1014
1015         pBtCoexist->boardInfo.singleAntPath = 0;
1016
1017         GLBtcWiFiInScanState = false;
1018
1019         GLBtcWiFiInIQKState = false;
1020
1021         return true;
1022 }
1023
1024 void EXhalbtcoutsrc_PowerOnSetting(PBTC_COEXIST pBtCoexist)
1025 {
1026         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1027                 return;
1028
1029         /* Power on setting function is only added in 8723B currently */
1030         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1031                 EXhalbtc8723b2ant_PowerOnSetting(pBtCoexist);
1032         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1033                 EXhalbtc8723b1ant_PowerOnSetting(pBtCoexist);
1034 }
1035
1036 void EXhalbtcoutsrc_InitHwConfig(PBTC_COEXIST pBtCoexist, u8 bWifiOnly)
1037 {
1038         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1039                 return;
1040
1041         pBtCoexist->statistics.cntInitHwConfig++;
1042
1043         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1044                 EXhalbtc8723b2ant_InitHwConfig(pBtCoexist, bWifiOnly);
1045         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1046                 EXhalbtc8723b1ant_InitHwConfig(pBtCoexist, bWifiOnly);
1047 }
1048
1049 void EXhalbtcoutsrc_InitCoexDm(PBTC_COEXIST pBtCoexist)
1050 {
1051         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1052                 return;
1053
1054         pBtCoexist->statistics.cntInitCoexDm++;
1055
1056         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1057                 EXhalbtc8723b2ant_InitCoexDm(pBtCoexist);
1058         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1059                 EXhalbtc8723b1ant_InitCoexDm(pBtCoexist);
1060
1061         pBtCoexist->bInitilized = true;
1062 }
1063
1064 void EXhalbtcoutsrc_IpsNotify(PBTC_COEXIST pBtCoexist, u8 type)
1065 {
1066         u8 ipsType;
1067
1068         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1069                 return;
1070
1071         pBtCoexist->statistics.cntIpsNotify++;
1072         if (pBtCoexist->bManualControl)
1073                 return;
1074
1075         if (IPS_NONE == type)
1076                 ipsType = BTC_IPS_LEAVE;
1077         else
1078                 ipsType = BTC_IPS_ENTER;
1079
1080         /*  All notify is called in cmd thread, don't need to leave low power again */
1081 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1082
1083         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1084                 EXhalbtc8723b2ant_IpsNotify(pBtCoexist, ipsType);
1085         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1086                 EXhalbtc8723b1ant_IpsNotify(pBtCoexist, ipsType);
1087
1088 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1089 }
1090
1091 void EXhalbtcoutsrc_LpsNotify(PBTC_COEXIST pBtCoexist, u8 type)
1092 {
1093         u8 lpsType;
1094
1095
1096         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1097                 return;
1098
1099         pBtCoexist->statistics.cntLpsNotify++;
1100         if (pBtCoexist->bManualControl)
1101                 return;
1102
1103         if (PS_MODE_ACTIVE == type)
1104                 lpsType = BTC_LPS_DISABLE;
1105         else
1106                 lpsType = BTC_LPS_ENABLE;
1107
1108         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1109                 EXhalbtc8723b2ant_LpsNotify(pBtCoexist, lpsType);
1110         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1111                 EXhalbtc8723b1ant_LpsNotify(pBtCoexist, lpsType);
1112 }
1113
1114 void EXhalbtcoutsrc_ScanNotify(PBTC_COEXIST pBtCoexist, u8 type)
1115 {
1116         u8 scanType;
1117
1118         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1119                 return;
1120         pBtCoexist->statistics.cntScanNotify++;
1121         if (pBtCoexist->bManualControl)
1122                 return;
1123
1124         if (type) {
1125                 scanType = BTC_SCAN_START;
1126                 GLBtcWiFiInScanState = true;
1127         } else {
1128                 scanType = BTC_SCAN_FINISH;
1129                 GLBtcWiFiInScanState = false;
1130         }
1131
1132         /*  All notify is called in cmd thread, don't need to leave low power again */
1133 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1134
1135         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1136                 EXhalbtc8723b2ant_ScanNotify(pBtCoexist, scanType);
1137         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1138                 EXhalbtc8723b1ant_ScanNotify(pBtCoexist, scanType);
1139
1140 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1141 }
1142
1143 void EXhalbtcoutsrc_ConnectNotify(PBTC_COEXIST pBtCoexist, u8 action)
1144 {
1145         u8 assoType;
1146
1147         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1148                 return;
1149         pBtCoexist->statistics.cntConnectNotify++;
1150         if (pBtCoexist->bManualControl)
1151                 return;
1152
1153         if (action)
1154                 assoType = BTC_ASSOCIATE_START;
1155         else
1156                 assoType = BTC_ASSOCIATE_FINISH;
1157
1158         /*  All notify is called in cmd thread, don't need to leave low power again */
1159 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1160
1161         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1162                 EXhalbtc8723b2ant_ConnectNotify(pBtCoexist, assoType);
1163         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1164                 EXhalbtc8723b1ant_ConnectNotify(pBtCoexist, assoType);
1165
1166 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1167 }
1168
1169 void EXhalbtcoutsrc_MediaStatusNotify(PBTC_COEXIST pBtCoexist, RT_MEDIA_STATUS mediaStatus)
1170 {
1171         u8 mStatus;
1172
1173         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1174                 return;
1175
1176         pBtCoexist->statistics.cntMediaStatusNotify++;
1177         if (pBtCoexist->bManualControl)
1178                 return;
1179
1180         if (RT_MEDIA_CONNECT == mediaStatus)
1181                 mStatus = BTC_MEDIA_CONNECT;
1182         else
1183                 mStatus = BTC_MEDIA_DISCONNECT;
1184
1185         /*  All notify is called in cmd thread, don't need to leave low power again */
1186 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1187
1188         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1189                 EXhalbtc8723b2ant_MediaStatusNotify(pBtCoexist, mStatus);
1190         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1191                 EXhalbtc8723b1ant_MediaStatusNotify(pBtCoexist, mStatus);
1192
1193 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1194 }
1195
1196 void EXhalbtcoutsrc_SpecialPacketNotify(PBTC_COEXIST pBtCoexist, u8 pktType)
1197 {
1198         u8 packetType;
1199
1200         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1201                 return;
1202         pBtCoexist->statistics.cntSpecialPacketNotify++;
1203         if (pBtCoexist->bManualControl)
1204                 return;
1205
1206         if (PACKET_DHCP == pktType)
1207                 packetType = BTC_PACKET_DHCP;
1208         else if (PACKET_EAPOL == pktType)
1209                 packetType = BTC_PACKET_EAPOL;
1210         else if (PACKET_ARP == pktType)
1211                 packetType = BTC_PACKET_ARP;
1212         else{
1213                 packetType = BTC_PACKET_UNKNOWN;
1214                 return;
1215         }
1216
1217         /*  All notify is called in cmd thread, don't need to leave low power again */
1218 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1219
1220         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1221                 EXhalbtc8723b2ant_SpecialPacketNotify(pBtCoexist, packetType);
1222         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1223                 EXhalbtc8723b1ant_SpecialPacketNotify(pBtCoexist, packetType);
1224
1225 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1226 }
1227
1228 void EXhalbtcoutsrc_BtInfoNotify(PBTC_COEXIST pBtCoexist, u8 *tmpBuf, u8 length)
1229 {
1230         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1231                 return;
1232
1233         pBtCoexist->statistics.cntBtInfoNotify++;
1234
1235         /*  All notify is called in cmd thread, don't need to leave low power again */
1236 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1237
1238         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1239                 EXhalbtc8723b2ant_BtInfoNotify(pBtCoexist, tmpBuf, length);
1240         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1241                 EXhalbtc8723b1ant_BtInfoNotify(pBtCoexist, tmpBuf, length);
1242
1243 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1244 }
1245
1246 void EXhalbtcoutsrc_HaltNotify(PBTC_COEXIST pBtCoexist)
1247 {
1248         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1249                 return;
1250
1251         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1252                 EXhalbtc8723b2ant_HaltNotify(pBtCoexist);
1253         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1254                 EXhalbtc8723b1ant_HaltNotify(pBtCoexist);
1255
1256         pBtCoexist->bBinded = false;
1257 }
1258
1259 void EXhalbtcoutsrc_PnpNotify(PBTC_COEXIST pBtCoexist, u8 pnpState)
1260 {
1261         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1262                 return;
1263
1264         /*  */
1265         /*  currently only 1ant we have to do the notification, */
1266         /*  once pnp is notified to sleep state, we have to leave LPS that we can sleep normally. */
1267         /*  */
1268
1269         if (pBtCoexist->boardInfo.btdmAntNum == 1)
1270                 EXhalbtc8723b1ant_PnpNotify(pBtCoexist, pnpState);
1271         else if (pBtCoexist->boardInfo.btdmAntNum == 2)
1272                 EXhalbtc8723b2ant_PnpNotify(pBtCoexist, pnpState);
1273 }
1274
1275 void EXhalbtcoutsrc_Periodical(PBTC_COEXIST pBtCoexist)
1276 {
1277         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1278                 return;
1279         pBtCoexist->statistics.cntPeriodical++;
1280
1281         /*  Periodical should be called in cmd thread, */
1282         /*  don't need to leave low power again */
1283 /*      halbtcoutsrc_LeaveLowPower(pBtCoexist); */
1284
1285         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1286                 EXhalbtc8723b2ant_Periodical(pBtCoexist);
1287         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1288                 EXhalbtc8723b1ant_Periodical(pBtCoexist);
1289
1290 /*      halbtcoutsrc_NormalLowPower(pBtCoexist); */
1291 }
1292
1293 void EXhalbtcoutsrc_SetChipType(u8 chipType)
1294 {
1295         GLBtCoexist.boardInfo.btChipType = BTC_CHIP_RTL8723B;
1296 }
1297
1298 void EXhalbtcoutsrc_SetAntNum(u8 type, u8 antNum)
1299 {
1300         if (BT_COEX_ANT_TYPE_PG == type) {
1301                 GLBtCoexist.boardInfo.pgAntNum = antNum;
1302                 GLBtCoexist.boardInfo.btdmAntNum = antNum;
1303         } else if (BT_COEX_ANT_TYPE_ANTDIV == type) {
1304                 GLBtCoexist.boardInfo.btdmAntNum = antNum;
1305                 /* GLBtCoexist.boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT; */
1306         } else if (BT_COEX_ANT_TYPE_DETECTED == type) {
1307                 GLBtCoexist.boardInfo.btdmAntNum = antNum;
1308                 /* GLBtCoexist.boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT; */
1309         }
1310 }
1311
1312 /*  */
1313 /*  Currently used by 8723b only, S0 or S1 */
1314 /*  */
1315 void EXhalbtcoutsrc_SetSingleAntPath(u8 singleAntPath)
1316 {
1317         GLBtCoexist.boardInfo.singleAntPath = singleAntPath;
1318 }
1319
1320 void EXhalbtcoutsrc_DisplayBtCoexInfo(PBTC_COEXIST pBtCoexist)
1321 {
1322         if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist))
1323                 return;
1324
1325         halbtcoutsrc_LeaveLowPower(pBtCoexist);
1326
1327         if (pBtCoexist->boardInfo.btdmAntNum == 2)
1328                 EXhalbtc8723b2ant_DisplayCoexInfo(pBtCoexist);
1329         else if (pBtCoexist->boardInfo.btdmAntNum == 1)
1330                 EXhalbtc8723b1ant_DisplayCoexInfo(pBtCoexist);
1331
1332         halbtcoutsrc_NormalLowPower(pBtCoexist);
1333 }
1334
1335 /*
1336  * Description:
1337  *Run BT-Coexist mechansim or not
1338  *
1339  */
1340 void hal_btcoex_SetBTCoexist(struct adapter *padapter, u8 bBtExist)
1341 {
1342         struct hal_com_data *pHalData;
1343
1344
1345         pHalData = GET_HAL_DATA(padapter);
1346         pHalData->bt_coexist.bBtExist = bBtExist;
1347 }
1348
1349 /*
1350  * Dewcription:
1351  *Check is co-exist mechanism enabled or not
1352  *
1353  * Return:
1354  *true  Enable BT co-exist mechanism
1355  *false Disable BT co-exist mechanism
1356  */
1357 u8 hal_btcoex_IsBtExist(struct adapter *padapter)
1358 {
1359         struct hal_com_data *pHalData;
1360
1361
1362         pHalData = GET_HAL_DATA(padapter);
1363         return pHalData->bt_coexist.bBtExist;
1364 }
1365
1366 u8 hal_btcoex_IsBtDisabled(struct adapter *padapter)
1367 {
1368         if (!hal_btcoex_IsBtExist(padapter))
1369                 return true;
1370
1371         if (GLBtCoexist.btInfo.bBtDisabled)
1372                 return true;
1373         else
1374                 return false;
1375 }
1376
1377 void hal_btcoex_SetChipType(struct adapter *padapter, u8 chipType)
1378 {
1379         struct hal_com_data *pHalData;
1380
1381
1382         pHalData = GET_HAL_DATA(padapter);
1383         pHalData->bt_coexist.btChipType = chipType;
1384
1385         EXhalbtcoutsrc_SetChipType(chipType);
1386 }
1387
1388 void hal_btcoex_SetPgAntNum(struct adapter *padapter, u8 antNum)
1389 {
1390         struct hal_com_data *pHalData;
1391
1392
1393         pHalData = GET_HAL_DATA(padapter);
1394
1395         pHalData->bt_coexist.btTotalAntNum = antNum;
1396         EXhalbtcoutsrc_SetAntNum(BT_COEX_ANT_TYPE_PG, antNum);
1397 }
1398
1399 void hal_btcoex_SetSingleAntPath(struct adapter *padapter, u8 singleAntPath)
1400 {
1401         EXhalbtcoutsrc_SetSingleAntPath(singleAntPath);
1402 }
1403
1404 u8 hal_btcoex_Initialize(struct adapter *padapter)
1405 {
1406         memset(&GLBtCoexist, 0, sizeof(GLBtCoexist));
1407         return EXhalbtcoutsrc_InitlizeVariables((void *)padapter);
1408 }
1409
1410 void hal_btcoex_PowerOnSetting(struct adapter *padapter)
1411 {
1412         EXhalbtcoutsrc_PowerOnSetting(&GLBtCoexist);
1413 }
1414
1415 void hal_btcoex_InitHwConfig(struct adapter *padapter, u8 bWifiOnly)
1416 {
1417         if (!hal_btcoex_IsBtExist(padapter))
1418                 return;
1419
1420         EXhalbtcoutsrc_InitHwConfig(&GLBtCoexist, bWifiOnly);
1421         EXhalbtcoutsrc_InitCoexDm(&GLBtCoexist);
1422 }
1423
1424 void hal_btcoex_IpsNotify(struct adapter *padapter, u8 type)
1425 {
1426         EXhalbtcoutsrc_IpsNotify(&GLBtCoexist, type);
1427 }
1428
1429 void hal_btcoex_LpsNotify(struct adapter *padapter, u8 type)
1430 {
1431         EXhalbtcoutsrc_LpsNotify(&GLBtCoexist, type);
1432 }
1433
1434 void hal_btcoex_ScanNotify(struct adapter *padapter, u8 type)
1435 {
1436         EXhalbtcoutsrc_ScanNotify(&GLBtCoexist, type);
1437 }
1438
1439 void hal_btcoex_ConnectNotify(struct adapter *padapter, u8 action)
1440 {
1441         EXhalbtcoutsrc_ConnectNotify(&GLBtCoexist, action);
1442 }
1443
1444 void hal_btcoex_MediaStatusNotify(struct adapter *padapter, u8 mediaStatus)
1445 {
1446         EXhalbtcoutsrc_MediaStatusNotify(&GLBtCoexist, mediaStatus);
1447 }
1448
1449 void hal_btcoex_SpecialPacketNotify(struct adapter *padapter, u8 pktType)
1450 {
1451         EXhalbtcoutsrc_SpecialPacketNotify(&GLBtCoexist, pktType);
1452 }
1453
1454 void hal_btcoex_IQKNotify(struct adapter *padapter, u8 state)
1455 {
1456         GLBtcWiFiInIQKState = state;
1457 }
1458
1459 void hal_btcoex_BtInfoNotify(struct adapter *padapter, u8 length, u8 *tmpBuf)
1460 {
1461         if (GLBtcWiFiInIQKState == true)
1462                 return;
1463
1464         EXhalbtcoutsrc_BtInfoNotify(&GLBtCoexist, tmpBuf, length);
1465 }
1466
1467 void hal_btcoex_SuspendNotify(struct adapter *padapter, u8 state)
1468 {
1469         if (state == 1)
1470                 state = BTC_WIFI_PNP_SLEEP;
1471         else
1472                 state = BTC_WIFI_PNP_WAKE_UP;
1473
1474         EXhalbtcoutsrc_PnpNotify(&GLBtCoexist, state);
1475 }
1476
1477 void hal_btcoex_HaltNotify(struct adapter *padapter)
1478 {
1479         EXhalbtcoutsrc_HaltNotify(&GLBtCoexist);
1480 }
1481
1482 void hal_btcoex_Hanlder(struct adapter *padapter)
1483 {
1484         EXhalbtcoutsrc_Periodical(&GLBtCoexist);
1485 }
1486
1487 s32 hal_btcoex_IsBTCoexCtrlAMPDUSize(struct adapter *padapter)
1488 {
1489         return (s32)GLBtCoexist.btInfo.bBtCtrlAggBufSize;
1490 }
1491
1492 void hal_btcoex_SetManualControl(struct adapter *padapter, u8 bmanual)
1493 {
1494         GLBtCoexist.bManualControl = bmanual;
1495 }
1496
1497 u8 hal_btcoex_IsBtControlLps(struct adapter *padapter)
1498 {
1499         if (hal_btcoex_IsBtExist(padapter) == false)
1500                 return false;
1501
1502         if (GLBtCoexist.btInfo.bBtDisabled)
1503                 return false;
1504
1505         if (GLBtCoexist.btInfo.bBtCtrlLps)
1506                 return true;
1507
1508         return false;
1509 }
1510
1511 u8 hal_btcoex_IsLpsOn(struct adapter *padapter)
1512 {
1513         if (hal_btcoex_IsBtExist(padapter) == false)
1514                 return false;
1515
1516         if (GLBtCoexist.btInfo.bBtDisabled)
1517                 return false;
1518
1519         if (GLBtCoexist.btInfo.bBtLpsOn)
1520                 return true;
1521
1522         return false;
1523 }
1524
1525 u8 hal_btcoex_RpwmVal(struct adapter *padapter)
1526 {
1527         return GLBtCoexist.btInfo.rpwmVal;
1528 }
1529
1530 u8 hal_btcoex_LpsVal(struct adapter *padapter)
1531 {
1532         return GLBtCoexist.btInfo.lpsVal;
1533 }
1534
1535 u32 hal_btcoex_GetRaMask(struct adapter *padapter)
1536 {
1537         if (!hal_btcoex_IsBtExist(padapter))
1538                 return 0;
1539
1540         if (GLBtCoexist.btInfo.bBtDisabled)
1541                 return 0;
1542
1543         if (GLBtCoexist.boardInfo.btdmAntNum != 1)
1544                 return 0;
1545
1546         return GLBtCoexist.btInfo.raMask;
1547 }
1548
1549 void hal_btcoex_RecordPwrMode(struct adapter *padapter, u8 *pCmdBuf, u8 cmdLen)
1550 {
1551         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE_FW_EXEC, ("[BTCoex], FW write pwrModeCmd = 0x%04x%08x\n",
1552                 pCmdBuf[0]<<8|pCmdBuf[1],
1553                 pCmdBuf[2]<<24|pCmdBuf[3]<<16|pCmdBuf[4]<<8|pCmdBuf[5]));
1554
1555         memcpy(GLBtCoexist.pwrModeVal, pCmdBuf, cmdLen);
1556 }
1557
1558 void hal_btcoex_DisplayBtCoexInfo(struct adapter *padapter, u8 *pbuf, u32 bufsize)
1559 {
1560         PBTCDBGINFO pinfo;
1561
1562
1563         pinfo = &GLBtcDbgInfo;
1564         DBG_BT_INFO_INIT(pinfo, pbuf, bufsize);
1565         EXhalbtcoutsrc_DisplayBtCoexInfo(&GLBtCoexist);
1566         DBG_BT_INFO_INIT(pinfo, NULL, 0);
1567 }
1568
1569 void hal_btcoex_SetDBG(struct adapter *padapter, u32 *pDbgModule)
1570 {
1571         u32 i;
1572
1573
1574         if (NULL == pDbgModule)
1575                 return;
1576
1577         for (i = 0; i < BTC_MSG_MAX; i++)
1578                 GLBtcDbgType[i] = pDbgModule[i];
1579 }
1580
1581 u32 hal_btcoex_GetDBG(struct adapter *padapter, u8 *pStrBuf, u32 bufSize)
1582 {
1583         s32 count;
1584         u8 *pstr;
1585         u32 leftSize;
1586
1587
1588         if ((NULL == pStrBuf) || (0 == bufSize))
1589                 return 0;
1590
1591         pstr = pStrBuf;
1592         leftSize = bufSize;
1593 /*      DBG_871X(FUNC_ADPT_FMT ": bufsize =%d\n", FUNC_ADPT_ARG(padapter), bufSize); */
1594
1595         count = rtw_sprintf(pstr, leftSize, "#define DBG\t%d\n", DBG);
1596         if ((count < 0) || (count >= leftSize))
1597                 goto exit;
1598         pstr += count;
1599         leftSize -= count;
1600
1601         count = rtw_sprintf(pstr, leftSize, "BTCOEX Debug Setting:\n");
1602         if ((count < 0) || (count >= leftSize))
1603                 goto exit;
1604         pstr += count;
1605         leftSize -= count;
1606
1607         count = rtw_sprintf(pstr, leftSize,
1608                 "INTERFACE / ALGORITHM: 0x%08X / 0x%08X\n\n",
1609                 GLBtcDbgType[BTC_MSG_INTERFACE],
1610                 GLBtcDbgType[BTC_MSG_ALGORITHM]);
1611         if ((count < 0) || (count >= leftSize))
1612                 goto exit;
1613         pstr += count;
1614         leftSize -= count;
1615
1616         count = rtw_sprintf(pstr, leftSize, "INTERFACE Debug Setting Definition:\n");
1617         if ((count < 0) || (count >= leftSize))
1618                 goto exit;
1619         pstr += count;
1620         leftSize -= count;
1621         count = rtw_sprintf(pstr, leftSize, "\tbit[0]=%d for INTF_INIT\n",
1622                 (GLBtcDbgType[BTC_MSG_INTERFACE]&INTF_INIT)?1:0);
1623         if ((count < 0) || (count >= leftSize))
1624                 goto exit;
1625         pstr += count;
1626         leftSize -= count;
1627         count = rtw_sprintf(pstr, leftSize, "\tbit[2]=%d for INTF_NOTIFY\n\n",
1628                 (GLBtcDbgType[BTC_MSG_INTERFACE]&INTF_NOTIFY)?1:0);
1629         if ((count < 0) || (count >= leftSize))
1630                 goto exit;
1631         pstr += count;
1632         leftSize -= count;
1633
1634         count = rtw_sprintf(pstr, leftSize, "ALGORITHM Debug Setting Definition:\n");
1635         if ((count < 0) || (count >= leftSize))
1636                 goto exit;
1637         pstr += count;
1638         leftSize -= count;
1639         count = rtw_sprintf(pstr, leftSize, "\tbit[0]=%d for BT_RSSI_STATE\n",
1640                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_BT_RSSI_STATE)?1:0);
1641         if ((count < 0) || (count >= leftSize))
1642                 goto exit;
1643         pstr += count;
1644         leftSize -= count;
1645         count = rtw_sprintf(pstr, leftSize, "\tbit[1]=%d for WIFI_RSSI_STATE\n",
1646                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_WIFI_RSSI_STATE)?1:0);
1647         if ((count < 0) || (count >= leftSize))
1648                 goto exit;
1649         pstr += count;
1650         leftSize -= count;
1651         count = rtw_sprintf(pstr, leftSize, "\tbit[2]=%d for BT_MONITOR\n",
1652                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_BT_MONITOR)?1:0);
1653         if ((count < 0) || (count >= leftSize))
1654                 goto exit;
1655         pstr += count;
1656         leftSize -= count;
1657         count = rtw_sprintf(pstr, leftSize, "\tbit[3]=%d for TRACE\n",
1658                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE)?1:0);
1659         if ((count < 0) || (count >= leftSize))
1660                 goto exit;
1661         pstr += count;
1662         leftSize -= count;
1663         count = rtw_sprintf(pstr, leftSize, "\tbit[4]=%d for TRACE_FW\n",
1664                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_FW)?1:0);
1665         if ((count < 0) || (count >= leftSize))
1666                 goto exit;
1667         pstr += count;
1668         leftSize -= count;
1669         count = rtw_sprintf(pstr, leftSize, "\tbit[5]=%d for TRACE_FW_DETAIL\n",
1670                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_FW_DETAIL)?1:0);
1671         if ((count < 0) || (count >= leftSize))
1672                 goto exit;
1673         pstr += count;
1674         leftSize -= count;
1675         count = rtw_sprintf(pstr, leftSize, "\tbit[6]=%d for TRACE_FW_EXEC\n",
1676                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_FW_EXEC)?1:0);
1677         if ((count < 0) || (count >= leftSize))
1678                 goto exit;
1679         pstr += count;
1680         leftSize -= count;
1681         count = rtw_sprintf(pstr, leftSize, "\tbit[7]=%d for TRACE_SW\n",
1682                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_SW)?1:0);
1683         if ((count < 0) || (count >= leftSize))
1684                 goto exit;
1685         pstr += count;
1686         leftSize -= count;
1687         count = rtw_sprintf(pstr, leftSize, "\tbit[8]=%d for TRACE_SW_DETAIL\n",
1688                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_SW_DETAIL)?1:0);
1689         if ((count < 0) || (count >= leftSize))
1690                 goto exit;
1691         pstr += count;
1692         leftSize -= count;
1693         count = rtw_sprintf(pstr, leftSize, "\tbit[9]=%d for TRACE_SW_EXEC\n",
1694                 (GLBtcDbgType[BTC_MSG_ALGORITHM]&ALGO_TRACE_SW_EXEC)?1:0);
1695         if ((count < 0) || (count >= leftSize))
1696                 goto exit;
1697         pstr += count;
1698         leftSize -= count;
1699
1700 exit:
1701         count = pstr - pStrBuf;
1702 /*      DBG_871X(FUNC_ADPT_FMT ": usedsize =%d\n", FUNC_ADPT_ARG(padapter), count); */
1703
1704         return count;
1705 }