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