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