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