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