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