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