1 /******************************************************************************
2  *
3  * Copyright(c) 2012  Realtek Corporation.
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  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  *****************************************************************************/
25 
26 /*============================================================
27  * Description:
28  *
29  * This file is for RTL8821A Co-exist mechanism
30  *
31  * History
32  * 2012/11/15 Cosa first check in.
33  *
34  *============================================================
35 */
36 /*============================================================
37  * include files
38  *============================================================
39  */
40 #include "halbt_precomp.h"
41 /*============================================================
42  * Global variables, these are static variables
43  *============================================================
44  */
45 static struct coex_dm_8821a_1ant glcoex_dm_8821a_1ant;
46 static struct coex_dm_8821a_1ant *coex_dm = &glcoex_dm_8821a_1ant;
47 static struct coex_sta_8821a_1ant glcoex_sta_8821a_1ant;
48 static struct coex_sta_8821a_1ant *coex_sta = &glcoex_sta_8821a_1ant;
49 
50 static const char *const glbt_info_src_8821a_1ant[] = {
51 	  "BT Info[wifi fw]",
52 	  "BT Info[bt rsp]",
53 	  "BT Info[bt auto report]",
54 };
55 
56 static u32	glcoex_ver_date_8821a_1ant = 20130816;
57 static u32	glcoex_ver_8821a_1ant = 0x41;
58 
59 /*============================================================
60  * local function proto type if needed
61  *
62  * local function start with halbtc8821a1ant_
63  *============================================================
64  */
65 static u8 halbtc8821a1ant_bt_rssi_state(struct btc_coexist *btcoexist,
66 					u8 level_num, u8 rssi_thresh,
67 					u8 rssi_thresh1)
68 {
69 	struct rtl_priv *rtlpriv = btcoexist->adapter;
70 	long	bt_rssi = 0;
71 	u8	bt_rssi_state = coex_sta->pre_bt_rssi_state;
72 
73 	bt_rssi = coex_sta->bt_rssi;
74 
75 	if (level_num == 2) {
76 		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
77 		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
78 			if (bt_rssi >= (rssi_thresh +
79 					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
80 				bt_rssi_state = BTC_RSSI_STATE_HIGH;
81 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
82 					 "[BTCoex], BT Rssi state switch to High\n");
83 			} else {
84 				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
85 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
86 					 "[BTCoex], BT Rssi state stay at Low\n");
87 			}
88 		} else {
89 			if (bt_rssi < rssi_thresh) {
90 				bt_rssi_state = BTC_RSSI_STATE_LOW;
91 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
92 					 "[BTCoex], BT Rssi state switch to Low\n");
93 			} else {
94 				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
95 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
96 					 "[BTCoex], BT Rssi state stay at High\n");
97 			}
98 		}
99 	} else if (level_num == 3) {
100 		if (rssi_thresh > rssi_thresh1) {
101 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
102 				 "[BTCoex], BT Rssi thresh error!!\n");
103 			return coex_sta->pre_bt_rssi_state;
104 		}
105 
106 		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
107 		    (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
108 			if (bt_rssi >= (rssi_thresh +
109 					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
110 				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
111 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
112 					 "[BTCoex], BT Rssi state switch to Medium\n");
113 			} else {
114 				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
115 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
116 					 "[BTCoex], BT Rssi state stay at Low\n");
117 			}
118 		} else if ((coex_sta->pre_bt_rssi_state ==
119 			   BTC_RSSI_STATE_MEDIUM) ||
120 			   (coex_sta->pre_bt_rssi_state ==
121 			    BTC_RSSI_STATE_STAY_MEDIUM)) {
122 			if (bt_rssi >= (rssi_thresh1 +
123 					BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
124 				bt_rssi_state = BTC_RSSI_STATE_HIGH;
125 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
126 					 "[BTCoex], BT Rssi state switch to High\n");
127 			} else if (bt_rssi < rssi_thresh) {
128 				bt_rssi_state = BTC_RSSI_STATE_LOW;
129 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
130 					 "[BTCoex], BT Rssi state switch to Low\n");
131 			} else {
132 				bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
133 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
134 					 "[BTCoex], BT Rssi state stay at Medium\n");
135 			}
136 		} else {
137 			if (bt_rssi < rssi_thresh1) {
138 				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
139 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
140 					 "[BTCoex], BT Rssi state switch to Medium\n");
141 			} else {
142 				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
143 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
144 					 "[BTCoex], BT Rssi state stay at High\n");
145 			}
146 		}
147 	}
148 	coex_sta->pre_bt_rssi_state = bt_rssi_state;
149 
150 	return bt_rssi_state;
151 }
152 
153 static u8 halbtc8821a1ant_WifiRssiState(struct btc_coexist *btcoexist,
154 					u8 index, u8 level_num, u8 rssi_thresh,
155 					u8 rssi_thresh1)
156 {
157 	struct rtl_priv *rtlpriv = btcoexist->adapter;
158 	long	wifi_rssi = 0;
159 	u8	wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
160 
161 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
162 
163 	if (level_num == 2) {
164 		if ((coex_sta->pre_wifi_rssi_state[index] ==
165 		     BTC_RSSI_STATE_LOW) ||
166 		    (coex_sta->pre_wifi_rssi_state[index] ==
167 		     BTC_RSSI_STATE_STAY_LOW)) {
168 			if (wifi_rssi >=
169 			    (rssi_thresh+BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
170 				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
171 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
172 					 "[BTCoex], wifi RSSI state switch to High\n");
173 			} else {
174 				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
175 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
176 					 "[BTCoex], wifi RSSI state stay at Low\n");
177 			}
178 		} else {
179 			if (wifi_rssi < rssi_thresh) {
180 				wifi_rssi_state = BTC_RSSI_STATE_LOW;
181 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
182 					 "[BTCoex], wifi RSSI state switch to Low\n");
183 			} else {
184 				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
185 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
186 					 "[BTCoex], wifi RSSI state stay at High\n");
187 			}
188 		}
189 	} else if (level_num == 3) {
190 		if (rssi_thresh > rssi_thresh1) {
191 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
192 				 "[BTCoex], wifi RSSI thresh error!!\n");
193 			return coex_sta->pre_wifi_rssi_state[index];
194 		}
195 
196 		if ((coex_sta->pre_wifi_rssi_state[index] ==
197 		     BTC_RSSI_STATE_LOW) ||
198 		    (coex_sta->pre_wifi_rssi_state[index] ==
199 		     BTC_RSSI_STATE_STAY_LOW)) {
200 			if (wifi_rssi >=
201 			    (rssi_thresh+BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
202 				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
203 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
204 					 "[BTCoex], wifi RSSI state switch to Medium\n");
205 			} else {
206 				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
207 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
208 					 "[BTCoex], wifi RSSI state stay at Low\n");
209 			}
210 		} else if ((coex_sta->pre_wifi_rssi_state[index] ==
211 			BTC_RSSI_STATE_MEDIUM) ||
212 			(coex_sta->pre_wifi_rssi_state[index] ==
213 			BTC_RSSI_STATE_STAY_MEDIUM)) {
214 			if (wifi_rssi >=
215 			    (rssi_thresh1 +
216 			     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
217 				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
218 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
219 					 "[BTCoex], wifi RSSI state switch to High\n");
220 			} else if (wifi_rssi < rssi_thresh) {
221 				wifi_rssi_state = BTC_RSSI_STATE_LOW;
222 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
223 					 "[BTCoex], wifi RSSI state switch to Low\n");
224 			} else {
225 				wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
226 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
227 					 "[BTCoex], wifi RSSI state stay at Medium\n");
228 			}
229 		} else {
230 			if (wifi_rssi < rssi_thresh1) {
231 				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
232 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
233 					 "[BTCoex], wifi RSSI state switch to Medium\n");
234 			} else {
235 				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
236 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
237 					 "[BTCoex], wifi RSSI state stay at High\n");
238 			}
239 		}
240 	}
241 	coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
242 
243 	return wifi_rssi_state;
244 }
245 
246 static void halbtc8821a1ant_update_ra_mask(struct btc_coexist *btcoexist,
247 					   bool force_exec, u32 dis_rate_mask)
248 {
249 	coex_dm->cur_ra_mask = dis_rate_mask;
250 
251 	if (force_exec ||
252 	    (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask)) {
253 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_ra_mask,
254 				   &coex_dm->cur_ra_mask);
255 	}
256 	coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
257 }
258 
259 static void btc8821a1ant_auto_rate_fb_retry(struct btc_coexist *btcoexist,
260 					    bool force_exec, u8 type)
261 {
262 	bool	wifi_under_b_mode = false;
263 
264 	coex_dm->cur_arfr_type = type;
265 
266 	if (force_exec ||
267 	    (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
268 		switch (coex_dm->cur_arfr_type) {
269 		case 0:	/* normal mode*/
270 			btcoexist->btc_write_4byte(btcoexist, 0x430,
271 						   coex_dm->backup_arfr_cnt1);
272 			btcoexist->btc_write_4byte(btcoexist, 0x434,
273 						   coex_dm->backup_arfr_cnt2);
274 			break;
275 		case 1:
276 			btcoexist->btc_get(btcoexist,
277 					   BTC_GET_BL_WIFI_UNDER_B_MODE,
278 					   &wifi_under_b_mode);
279 			if (wifi_under_b_mode) {
280 				btcoexist->btc_write_4byte(btcoexist, 0x430,
281 							   0x0);
282 				btcoexist->btc_write_4byte(btcoexist, 0x434,
283 							   0x01010101);
284 			} else {
285 				btcoexist->btc_write_4byte(btcoexist, 0x430,
286 							   0x0);
287 				btcoexist->btc_write_4byte(btcoexist, 0x434,
288 							   0x04030201);
289 			}
290 			break;
291 		default:
292 			break;
293 		}
294 	}
295 
296 	coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
297 }
298 
299 static void halbtc8821a1ant_retry_limit(struct btc_coexist *btcoexist,
300 					bool force_exec, u8 type)
301 {
302 	coex_dm->cur_retry_limit_type = type;
303 
304 	if (force_exec ||
305 	    (coex_dm->pre_retry_limit_type != coex_dm->cur_retry_limit_type)) {
306 		switch (coex_dm->cur_retry_limit_type) {
307 		case 0:	/* normal mode*/
308 			btcoexist->btc_write_2byte(btcoexist, 0x42a,
309 						   coex_dm->backup_retry_limit);
310 			break;
311 		case 1:	/* retry limit = 8*/
312 			btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
313 			break;
314 		default:
315 			break;
316 		}
317 	}
318 	coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
319 }
320 
321 static void halbtc8821a1ant_ampdu_max_time(struct btc_coexist *btcoexist,
322 					   bool force_exec, u8 type)
323 {
324 	coex_dm->cur_ampdu_time_type = type;
325 
326 	if (force_exec ||
327 	    (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
328 		switch (coex_dm->cur_ampdu_time_type) {
329 		case 0:	/* normal mode*/
330 			btcoexist->btc_write_1byte(btcoexist, 0x456,
331 						   coex_dm->backup_ampdu_max_time);
332 			break;
333 		case 1:	/* AMPDU timw = 0x38 * 32us*/
334 			btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
335 			break;
336 		default:
337 			break;
338 		}
339 	}
340 
341 	coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
342 }
343 
344 static void halbtc8821a1ant_limited_tx(struct btc_coexist *btcoexist,
345 				       bool force_exec, u8 ra_mask_type,
346 				       u8 arfr_type, u8 retry_limit_type,
347 				       u8 ampdu_time_type)
348 {
349 	switch (ra_mask_type) {
350 	case 0:	/* normal mode*/
351 		halbtc8821a1ant_update_ra_mask(btcoexist, force_exec, 0x0);
352 		break;
353 	case 1:	/* disable cck 1/2*/
354 		halbtc8821a1ant_update_ra_mask(btcoexist, force_exec,
355 					       0x00000003);
356 		break;
357 	case 2:	/* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4*/
358 		halbtc8821a1ant_update_ra_mask(btcoexist, force_exec,
359 					       0x0001f1f7);
360 		break;
361 	default:
362 		break;
363 	}
364 
365 	btc8821a1ant_auto_rate_fb_retry(btcoexist, force_exec, arfr_type);
366 	halbtc8821a1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
367 	halbtc8821a1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
368 }
369 
370 static void halbtc8821a1ant_limited_rx(struct btc_coexist *btcoexist,
371 				       bool force_exec, bool rej_ap_agg_pkt,
372 				       bool bt_ctrl_agg_buf_size,
373 				       u8 agg_buf_size)
374 {
375 	bool reject_rx_agg = rej_ap_agg_pkt;
376 	bool bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
377 	u8 rx_agg_size = agg_buf_size;
378 
379 	/*============================================*/
380 	/*	Rx Aggregation related setting*/
381 	/*============================================*/
382 	btcoexist->btc_set(btcoexist,
383 		 BTC_SET_BL_TO_REJ_AP_AGG_PKT, &reject_rx_agg);
384 	/* decide BT control aggregation buf size or not*/
385 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
386 			   &bt_ctrl_rx_agg_size);
387 	/* aggregation buf size, only work when BT control Rx agg size.*/
388 	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
389 	/* real update aggregation setting*/
390 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
391 }
392 
393 static void halbtc8821a1ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
394 {
395 	u32	reg_hp_tx_rx, reg_lp_tx_rx, u4_tmp;
396 	u32	reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
397 
398 	reg_hp_tx_rx = 0x770;
399 	reg_lp_tx_rx = 0x774;
400 
401 	u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_tx_rx);
402 	reg_hp_tx = u4_tmp & MASKLWORD;
403 	reg_hp_rx = (u4_tmp & MASKHWORD)>>16;
404 
405 	u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_tx_rx);
406 	reg_lp_tx = u4_tmp & MASKLWORD;
407 	reg_lp_rx = (u4_tmp & MASKHWORD)>>16;
408 
409 	coex_sta->high_priority_tx = reg_hp_tx;
410 	coex_sta->high_priority_rx = reg_hp_rx;
411 	coex_sta->low_priority_tx = reg_lp_tx;
412 	coex_sta->low_priority_rx = reg_lp_rx;
413 
414 	/* reset counter*/
415 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
416 }
417 
418 static void halbtc8821a1ant_query_bt_info(struct btc_coexist *btcoexist)
419 {
420 	struct rtl_priv *rtlpriv = btcoexist->adapter;
421 	u8 h2c_parameter[1] = {0};
422 
423 	coex_sta->c2h_bt_info_req_sent = true;
424 
425 	h2c_parameter[0] |= BIT0;	/* trigger*/
426 
427 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
428 		 "[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n",
429 		 h2c_parameter[0]);
430 
431 	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
432 }
433 
434 static void halbtc8821a1ant_update_bt_link_info(struct btc_coexist *btcoexist)
435 {
436 	struct btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
437 	bool	bt_hs_on = false;
438 
439 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
440 
441 	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
442 	bt_link_info->sco_exist = coex_sta->sco_exist;
443 	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
444 	bt_link_info->pan_exist = coex_sta->pan_exist;
445 	bt_link_info->hid_exist = coex_sta->hid_exist;
446 
447 	/* work around for HS mode.*/
448 	if (bt_hs_on) {
449 		bt_link_info->pan_exist = true;
450 		bt_link_info->bt_link_exist = true;
451 	}
452 
453 	/* check if Sco only*/
454 	if (bt_link_info->sco_exist &&
455 	    !bt_link_info->a2dp_exist &&
456 	    !bt_link_info->pan_exist &&
457 	    !bt_link_info->hid_exist)
458 		bt_link_info->sco_only = true;
459 	else
460 		bt_link_info->sco_only = false;
461 
462 	/* check if A2dp only*/
463 	if (!bt_link_info->sco_exist &&
464 	    bt_link_info->a2dp_exist &&
465 	    !bt_link_info->pan_exist &&
466 	    !bt_link_info->hid_exist)
467 		bt_link_info->a2dp_only = true;
468 	else
469 		bt_link_info->a2dp_only = false;
470 
471 	/* check if Pan only*/
472 	if (!bt_link_info->sco_exist &&
473 	    !bt_link_info->a2dp_exist &&
474 	    bt_link_info->pan_exist &&
475 	    !bt_link_info->hid_exist)
476 		bt_link_info->pan_only = true;
477 	else
478 		bt_link_info->pan_only = false;
479 
480 	/* check if Hid only*/
481 	if (!bt_link_info->sco_exist &&
482 	    !bt_link_info->a2dp_exist &&
483 	    !bt_link_info->pan_exist &&
484 	    bt_link_info->hid_exist)
485 		bt_link_info->hid_only = true;
486 	else
487 		bt_link_info->hid_only = false;
488 }
489 
490 static u8 halbtc8821a1ant_action_algorithm(struct btc_coexist *btcoexist)
491 {
492 	struct rtl_priv *rtlpriv = btcoexist->adapter;
493 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
494 	bool	bt_hs_on = false;
495 	u8	algorithm = BT_8821A_1ANT_COEX_ALGO_UNDEFINED;
496 	u8	num_of_diff_profile = 0;
497 
498 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
499 
500 	if (!bt_link_info->bt_link_exist) {
501 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
502 			 "[BTCoex], No BT link exists!!!\n");
503 		return algorithm;
504 	}
505 
506 	if (bt_link_info->sco_exist)
507 		num_of_diff_profile++;
508 	if (bt_link_info->hid_exist)
509 		num_of_diff_profile++;
510 	if (bt_link_info->pan_exist)
511 		num_of_diff_profile++;
512 	if (bt_link_info->a2dp_exist)
513 		num_of_diff_profile++;
514 
515 	if (num_of_diff_profile == 1) {
516 		if (bt_link_info->sco_exist) {
517 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
518 				 "[BTCoex], BT Profile = SCO only\n");
519 			algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
520 		} else {
521 			if (bt_link_info->hid_exist) {
522 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
523 					 "[BTCoex], BT Profile = HID only\n");
524 				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
525 			} else if (bt_link_info->a2dp_exist) {
526 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
527 					 "[BTCoex], BT Profile = A2DP only\n");
528 				algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP;
529 			} else if (bt_link_info->pan_exist) {
530 				if (bt_hs_on) {
531 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
532 						 DBG_LOUD,
533 						 "[BTCoex], BT Profile = PAN(HS) only\n");
534 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANHS;
535 				} else {
536 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
537 						 DBG_LOUD,
538 						 "[BTCoex], BT Profile = PAN(EDR) only\n");
539 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR;
540 				}
541 			}
542 		}
543 	} else if (num_of_diff_profile == 2) {
544 		if (bt_link_info->sco_exist) {
545 			if (bt_link_info->hid_exist) {
546 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
547 					 "[BTCoex], BT Profile = SCO + HID\n");
548 				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
549 			} else if (bt_link_info->a2dp_exist) {
550 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
551 					 "[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
552 				algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
553 			} else if (bt_link_info->pan_exist) {
554 				if (bt_hs_on) {
555 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
556 						 DBG_LOUD,
557 						 "[BTCoex], BT Profile = SCO + PAN(HS)\n");
558 					algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
559 				} else {
560 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
561 						 DBG_LOUD,
562 						 "[BTCoex], BT Profile = SCO + PAN(EDR)\n");
563 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
564 				}
565 			}
566 		} else {
567 			if (bt_link_info->hid_exist &&
568 			    bt_link_info->a2dp_exist) {
569 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
570 					 "[BTCoex], BT Profile = HID + A2DP\n");
571 				algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
572 			} else if (bt_link_info->hid_exist &&
573 				   bt_link_info->pan_exist) {
574 				if (bt_hs_on) {
575 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
576 						 DBG_LOUD,
577 						 "[BTCoex], BT Profile = HID + PAN(HS)\n");
578 					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
579 				} else {
580 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
581 						 DBG_LOUD,
582 						 "[BTCoex], BT Profile = HID + PAN(EDR)\n");
583 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
584 				}
585 			} else if (bt_link_info->pan_exist &&
586 				   bt_link_info->a2dp_exist) {
587 				if (bt_hs_on) {
588 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
589 						 DBG_LOUD,
590 						 "[BTCoex], BT Profile = A2DP + PAN(HS)\n");
591 					algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS;
592 				} else {
593 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
594 						 DBG_LOUD,
595 						 "[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
596 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP;
597 				}
598 			}
599 		}
600 	} else if (num_of_diff_profile == 3) {
601 		if (bt_link_info->sco_exist) {
602 			if (bt_link_info->hid_exist &&
603 			    bt_link_info->a2dp_exist) {
604 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
605 					 "[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
606 				algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
607 			} else if (bt_link_info->hid_exist &&
608 				bt_link_info->pan_exist) {
609 				if (bt_hs_on) {
610 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
611 						 DBG_LOUD,
612 						 "[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
613 					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
614 				} else {
615 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
616 						 DBG_LOUD,
617 						 "[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
618 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
619 				}
620 			} else if (bt_link_info->pan_exist &&
621 				bt_link_info->a2dp_exist) {
622 				if (bt_hs_on) {
623 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
624 						 DBG_LOUD,
625 						 "[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
626 					algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
627 				} else {
628 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
629 						 DBG_LOUD,
630 						 "[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
631 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
632 				}
633 			}
634 		} else {
635 			if (bt_link_info->hid_exist &&
636 			    bt_link_info->pan_exist &&
637 			    bt_link_info->a2dp_exist) {
638 				if (bt_hs_on) {
639 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
640 						 DBG_LOUD,
641 						 "[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
642 					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
643 				} else {
644 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
645 						 DBG_LOUD,
646 						 "[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
647 					algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
648 				}
649 			}
650 		}
651 	} else if (num_of_diff_profile >= 3) {
652 		if (bt_link_info->sco_exist) {
653 			if (bt_link_info->hid_exist &&
654 			    bt_link_info->pan_exist &&
655 			    bt_link_info->a2dp_exist) {
656 				if (bt_hs_on) {
657 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
658 						 DBG_LOUD,
659 						 "[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
660 
661 				} else {
662 					RT_TRACE(rtlpriv, COMP_BT_COEXIST,
663 						 DBG_LOUD,
664 						 "[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
665 					algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
666 				}
667 			}
668 		}
669 	}
670 	return algorithm;
671 }
672 
673 static void halbtc8821a1ant_set_bt_auto_report(struct btc_coexist *btcoexist,
674 					       bool enable_auto_report)
675 {
676 	struct rtl_priv *rtlpriv = btcoexist->adapter;
677 	u8 h2c_parameter[1] = {0};
678 
679 	h2c_parameter[0] = 0;
680 
681 	if (enable_auto_report)
682 		h2c_parameter[0] |= BIT0;
683 
684 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
685 		 "[BTCoex], BT FW auto report : %s, FW write 0x68 = 0x%x\n",
686 		 (enable_auto_report ? "Enabled!!" : "Disabled!!"),
687 		 h2c_parameter[0]);
688 
689 	btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
690 }
691 
692 static void halbtc8821a1ant_bt_auto_report(struct btc_coexist *btcoexist,
693 					   bool force_exec,
694 					   bool enable_auto_report)
695 {
696 	struct rtl_priv *rtlpriv = btcoexist->adapter;
697 
698 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
699 		 "[BTCoex], %s BT Auto report = %s\n",
700 		 (force_exec ? "force to" : ""), ((enable_auto_report) ?
701 						     "Enabled" : "Disabled"));
702 	coex_dm->cur_bt_auto_report = enable_auto_report;
703 
704 	if (!force_exec) {
705 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
706 			 "[BTCoex], pre_bt_auto_report = %d, cur_bt_auto_report = %d\n",
707 			    coex_dm->pre_bt_auto_report,
708 			    coex_dm->cur_bt_auto_report);
709 
710 		if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
711 			return;
712 	}
713 	halbtc8821a1ant_set_bt_auto_report(btcoexist, coex_dm->cur_bt_auto_report);
714 
715 	coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
716 }
717 
718 static void btc8821a1ant_set_sw_pen_tx_rate(struct btc_coexist *btcoexist,
719 					    bool low_penalty_ra)
720 {
721 	struct rtl_priv *rtlpriv = btcoexist->adapter;
722 	u8 h2c_parameter[6] = {0};
723 
724 	h2c_parameter[0] = 0x6;	/* opCode, 0x6= Retry_Penalty*/
725 
726 	if (low_penalty_ra) {
727 		h2c_parameter[1] |= BIT0;
728 		/*normal rate except MCS7/6/5, OFDM54/48/36*/
729 		h2c_parameter[2] = 0x00;
730 		h2c_parameter[3] = 0xf7;  /*MCS7 or OFDM54*/
731 		h2c_parameter[4] = 0xf8;  /*MCS6 or OFDM48*/
732 		h2c_parameter[5] = 0xf9;	/*MCS5 or OFDM36*/
733 	}
734 
735 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
736 		 "[BTCoex], set WiFi Low-Penalty Retry: %s",
737 		 (low_penalty_ra ? "ON!!" : "OFF!!"));
738 
739 	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
740 }
741 
742 static void halbtc8821a1ant_low_penalty_ra(struct btc_coexist *btcoexist,
743 					   bool force_exec, bool low_penalty_ra)
744 {
745 	coex_dm->cur_low_penalty_ra = low_penalty_ra;
746 
747 	if (!force_exec) {
748 		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
749 			return;
750 	}
751 	btc8821a1ant_set_sw_pen_tx_rate(btcoexist, coex_dm->cur_low_penalty_ra);
752 
753 	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
754 }
755 
756 static void halbtc8821a1ant_set_coex_table(struct btc_coexist *btcoexist,
757 					   u32 val0x6c0, u32 val0x6c4,
758 					   u32 val0x6c8, u8 val0x6cc)
759 {
760 	struct rtl_priv *rtlpriv = btcoexist->adapter;
761 
762 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
763 		 "[BTCoex], set coex table, set 0x6c0 = 0x%x\n", val0x6c0);
764 	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
765 
766 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
767 		 "[BTCoex], set coex table, set 0x6c4 = 0x%x\n", val0x6c4);
768 	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
769 
770 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
771 		 "[BTCoex], set coex table, set 0x6c8 = 0x%x\n", val0x6c8);
772 	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
773 
774 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
775 		 "[BTCoex], set coex table, set 0x6cc = 0x%x\n", val0x6cc);
776 	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
777 }
778 
779 static void halbtc8821a1ant_coex_table(struct btc_coexist *btcoexist,
780 				       bool force_exec, u32 val0x6c0,
781 				       u32 val0x6c4, u32 val0x6c8, u8 val0x6cc)
782 {
783 	struct rtl_priv *rtlpriv = btcoexist->adapter;
784 
785 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
786 		 "[BTCoex], %s write Coex Table 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6c8 = 0x%x, 0x6cc = 0x%x\n",
787 		    (force_exec ? "force to" : ""), val0x6c0, val0x6c4,
788 		    val0x6c8, val0x6cc);
789 	coex_dm->cur_val_0x6c0 = val0x6c0;
790 	coex_dm->cur_val_0x6c4 = val0x6c4;
791 	coex_dm->cur_val_0x6c8 = val0x6c8;
792 	coex_dm->cur_val_0x6cc = val0x6cc;
793 
794 	if (!force_exec) {
795 		if ((coex_dm->pre_val_0x6c0 == coex_dm->cur_val_0x6c0) &&
796 		    (coex_dm->pre_val_0x6c4 == coex_dm->cur_val_0x6c4) &&
797 		    (coex_dm->pre_val_0x6c8 == coex_dm->cur_val_0x6c8) &&
798 		    (coex_dm->pre_val_0x6cc == coex_dm->cur_val_0x6cc))
799 			return;
800 	}
801 	halbtc8821a1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
802 				       val0x6c8, val0x6cc);
803 
804 	coex_dm->pre_val_0x6c0 = coex_dm->cur_val_0x6c0;
805 	coex_dm->pre_val_0x6c4 = coex_dm->cur_val_0x6c4;
806 	coex_dm->pre_val_0x6c8 = coex_dm->cur_val_0x6c8;
807 	coex_dm->pre_val_0x6cc = coex_dm->cur_val_0x6cc;
808 }
809 
810 static void halbtc8821a1ant_coex_table_with_type(struct btc_coexist *btcoexist,
811 						 bool force_exec, u8 type)
812 {
813 	switch (type) {
814 	case 0:
815 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
816 					   0x55555555, 0xffffff, 0x3);
817 		break;
818 	case 1:
819 			halbtc8821a1ant_coex_table(btcoexist, force_exec,
820 						   0x55555555, 0x5a5a5a5a,
821 						   0xffffff, 0x3);
822 			break;
823 	case 2:
824 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
825 					   0x5a5a5a5a, 0xffffff, 0x3);
826 		break;
827 	case 3:
828 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
829 					   0xaaaaaaaa, 0xffffff, 0x3);
830 		break;
831 	case 4:
832 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0xffffffff,
833 					   0xffffffff, 0xffffff, 0x3);
834 		break;
835 	case 5:
836 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x5fff5fff,
837 					   0x5fff5fff, 0xffffff, 0x3);
838 		break;
839 	case 6:
840 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x55ff55ff,
841 					   0x5a5a5a5a, 0xffffff, 0x3);
842 		break;
843 	case 7:
844 		halbtc8821a1ant_coex_table(btcoexist, force_exec, 0x5afa5afa,
845 					   0x5afa5afa, 0xffffff, 0x3);
846 		break;
847 	default:
848 		break;
849 	}
850 }
851 
852 static void btc8821a1ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
853 						bool enable)
854 {
855 	struct rtl_priv *rtlpriv = btcoexist->adapter;
856 	u8	h2c_parameter[1] = {0};
857 
858 	if (enable)
859 		h2c_parameter[0] |= BIT0;	/* function enable*/
860 
861 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
862 		 "[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63 = 0x%x\n",
863 		 h2c_parameter[0]);
864 
865 	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
866 }
867 
868 static void halbtc8821a1ant_ignore_wlan_act(struct btc_coexist *btcoexist,
869 					    bool force_exec, bool enable)
870 {
871 	struct rtl_priv *rtlpriv = btcoexist->adapter;
872 
873 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
874 		 "[BTCoex], %s turn Ignore WlanAct %s\n",
875 		 (force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
876 	coex_dm->cur_ignore_wlan_act = enable;
877 
878 	if (!force_exec) {
879 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
880 			 "[BTCoex], pre_ignore_wlan_act = %d, cur_ignore_wlan_act = %d!!\n",
881 			 coex_dm->pre_ignore_wlan_act,
882 			 coex_dm->cur_ignore_wlan_act);
883 
884 		if (coex_dm->pre_ignore_wlan_act ==
885 		    coex_dm->cur_ignore_wlan_act)
886 			return;
887 	}
888 	btc8821a1ant_set_fw_ignore_wlan_act(btcoexist, enable);
889 
890 	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
891 }
892 
893 static void halbtc8821a1ant_set_fw_pstdma(struct btc_coexist *btcoexist,
894 					  u8 byte1, u8 byte2, u8 byte3,
895 					  u8 byte4, u8 byte5)
896 {
897 	struct rtl_priv *rtlpriv = btcoexist->adapter;
898 	u8 h2c_parameter[5] = {0};
899 
900 	h2c_parameter[0] = byte1;
901 	h2c_parameter[1] = byte2;
902 	h2c_parameter[2] = byte3;
903 	h2c_parameter[3] = byte4;
904 	h2c_parameter[4] = byte5;
905 
906 	coex_dm->ps_tdma_para[0] = byte1;
907 	coex_dm->ps_tdma_para[1] = byte2;
908 	coex_dm->ps_tdma_para[2] = byte3;
909 	coex_dm->ps_tdma_para[3] = byte4;
910 	coex_dm->ps_tdma_para[4] = byte5;
911 
912 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
913 		 "[BTCoex], PS-TDMA H2C cmd =0x%x%08x\n",
914 		 h2c_parameter[0],
915 		 h2c_parameter[1] << 24 |
916 		 h2c_parameter[2] << 16 |
917 		 h2c_parameter[3] << 8 |
918 		 h2c_parameter[4]);
919 	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
920 }
921 
922 static void halbtc8821a1ant_set_lps_rpwm(struct btc_coexist *btcoexist,
923 					 u8 lps_val, u8 rpwm_val)
924 {
925 	u8	lps = lps_val;
926 	u8	rpwm = rpwm_val;
927 
928 	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
929 	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
930 }
931 
932 static void halbtc8821a1ant_lps_rpwm(struct btc_coexist *btcoexist,
933 				     bool force_exec, u8 lps_val, u8 rpwm_val)
934 {
935 	struct rtl_priv *rtlpriv = btcoexist->adapter;
936 
937 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
938 		 "[BTCoex], %s set lps/rpwm = 0x%x/0x%x\n",
939 		 (force_exec ? "force to" : ""), lps_val, rpwm_val);
940 	coex_dm->cur_lps = lps_val;
941 	coex_dm->cur_rpwm = rpwm_val;
942 
943 	if (!force_exec) {
944 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
945 			 "[BTCoex], LPS-RxBeaconMode = 0x%x, LPS-RPWM = 0x%x!!\n",
946 			 coex_dm->cur_lps, coex_dm->cur_rpwm);
947 
948 		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
949 		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm)) {
950 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
951 				 "[BTCoex], LPS-RPWM_Last = 0x%x, LPS-RPWM_Now = 0x%x!!\n",
952 				 coex_dm->pre_rpwm, coex_dm->cur_rpwm);
953 
954 			return;
955 		}
956 	}
957 	halbtc8821a1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
958 
959 	coex_dm->pre_lps = coex_dm->cur_lps;
960 	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
961 }
962 
963 static void halbtc8821a1ant_sw_mechanism(struct btc_coexist *btcoexist,
964 					 bool low_penalty_ra)
965 {
966 	struct rtl_priv *rtlpriv = btcoexist->adapter;
967 
968 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
969 		 "[BTCoex], SM[LpRA] = %d\n", low_penalty_ra);
970 
971 	halbtc8821a1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
972 }
973 
974 static void halbtc8821a1ant_set_ant_path(struct btc_coexist *btcoexist,
975 					 u8 ant_pos_type, bool init_hw_cfg,
976 					 bool wifi_off)
977 {
978 	struct btc_board_info *board_info = &btcoexist->board_info;
979 	u32 u4_tmp = 0;
980 	u8 h2c_parameter[2] = {0};
981 
982 	if (init_hw_cfg) {
983 		/* 0x4c[23] = 0, 0x4c[24] = 1  Antenna control by WL/BT*/
984 		u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
985 		u4_tmp &= ~BIT23;
986 		u4_tmp |= BIT24;
987 		btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
988 
989 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x975, 0x3, 0x3);
990 		btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
991 
992 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
993 			/*tell firmware "antenna inverse"  ==>
994 			 * WRONG firmware antenna control code.==>need fw to fix
995 			 */
996 			h2c_parameter[0] = 1;
997 			h2c_parameter[1] = 1;
998 			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
999 						h2c_parameter);
1000 			/*Main Ant to  BT for IPS case 0x4c[23] = 1*/
1001 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64,
1002 							   0x1, 0x1);
1003 		} else {
1004 			/*tell firmware "no antenna inverse" ==>
1005 			 * WRONG firmware antenna control code.==>need fw to fix
1006 			 */
1007 			h2c_parameter[0] = 0;
1008 			h2c_parameter[1] = 1;
1009 			btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
1010 						h2c_parameter);
1011 			/*Aux Ant to  BT for IPS case 0x4c[23] = 1*/
1012 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64,
1013 							   0x1, 0x0);
1014 		}
1015 	} else if (wifi_off) {
1016 		/* 0x4c[24:23] = 00, Set Antenna control
1017 		 *	by BT_RFE_CTRL	BT Vendor 0xac = 0xf002
1018 		 */
1019 		u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
1020 		u4_tmp &= ~BIT23;
1021 		u4_tmp &= ~BIT24;
1022 		btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
1023 	}
1024 
1025 	/* ext switch setting*/
1026 	switch (ant_pos_type) {
1027 	case BTC_ANT_PATH_WIFI:
1028 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1029 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1030 							   0x30, 0x1);
1031 		else
1032 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1033 							   0x30, 0x2);
1034 		break;
1035 	case BTC_ANT_PATH_BT:
1036 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1037 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1038 							   0x30, 0x2);
1039 		else
1040 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1041 							   0x30, 0x1);
1042 		break;
1043 	default:
1044 	case BTC_ANT_PATH_PTA:
1045 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
1046 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1047 							   0x30, 0x1);
1048 		else
1049 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
1050 							   0x30, 0x2);
1051 		break;
1052 	}
1053 }
1054 
1055 static void halbtc8821a1ant_ps_tdma(struct btc_coexist *btcoexist,
1056 				    bool force_exec, bool turn_on, u8 type)
1057 {
1058 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1059 	u8 rssi_adjust_val = 0;
1060 
1061 	coex_dm->cur_ps_tdma_on = turn_on;
1062 	coex_dm->cur_ps_tdma = type;
1063 
1064 	if (!force_exec) {
1065 		if (coex_dm->cur_ps_tdma_on) {
1066 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1067 				 "[BTCoex], ********** TDMA(on, %d) **********\n",
1068 				 coex_dm->cur_ps_tdma);
1069 		} else {
1070 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1071 				 "[BTCoex], ********** TDMA(off, %d) **********\n",
1072 				 coex_dm->cur_ps_tdma);
1073 		}
1074 		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1075 		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1076 			return;
1077 	}
1078 	if (turn_on) {
1079 		switch (type) {
1080 		default:
1081 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x1a,
1082 						      0x1a, 0x0, 0x50);
1083 			break;
1084 		case 1:
1085 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x3a,
1086 						      0x03, 0x10, 0x50);
1087 			rssi_adjust_val = 11;
1088 			break;
1089 		case 2:
1090 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x2b,
1091 						      0x03, 0x10, 0x50);
1092 			rssi_adjust_val = 14;
1093 			break;
1094 		case 3:
1095 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x1d,
1096 						      0x1d, 0x0, 0x10);
1097 			break;
1098 		case 4:
1099 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x15,
1100 						      0x3, 0x14, 0x0);
1101 			rssi_adjust_val = 17;
1102 			break;
1103 		case 5:
1104 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x15,
1105 						      0x3, 0x11, 0x10);
1106 			break;
1107 		case 6:
1108 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xa,
1109 						      0x3, 0x0, 0x0);
1110 			break;
1111 		case 7:
1112 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xc,
1113 						      0x5, 0x0, 0x0);
1114 			break;
1115 		case 8:
1116 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x25,
1117 						      0x3, 0x10, 0x0);
1118 			break;
1119 		case 9:
1120 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x21,
1121 						      0x3, 0x10, 0x50);
1122 			rssi_adjust_val = 18;
1123 			break;
1124 		case 10:
1125 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xa,
1126 						      0xa, 0x0, 0x40);
1127 			break;
1128 		case 11:
1129 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x14,
1130 						      0x03, 0x10, 0x10);
1131 			rssi_adjust_val = 20;
1132 			break;
1133 		case 12:
1134 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x0a,
1135 						      0x0a, 0x0, 0x50);
1136 			break;
1137 		case 13:
1138 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x18,
1139 						      0x18, 0x0, 0x10);
1140 			break;
1141 		case 14:
1142 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x21,
1143 						      0x3, 0x10, 0x10);
1144 			break;
1145 		case 15:
1146 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13, 0xa,
1147 						      0x3, 0x8, 0x0);
1148 			break;
1149 		case 16:
1150 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x15,
1151 						      0x3, 0x10, 0x0);
1152 			rssi_adjust_val = 18;
1153 			break;
1154 		case 18:
1155 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93, 0x25,
1156 						      0x3, 0x10, 0x0);
1157 			rssi_adjust_val = 14;
1158 			break;
1159 		case 20:
1160 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x35,
1161 						      0x03, 0x11, 0x10);
1162 			break;
1163 		case 21:
1164 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x15,
1165 						      0x03, 0x11, 0x10);
1166 			break;
1167 		case 22:
1168 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0x25,
1169 						      0x03, 0x11, 0x10);
1170 			break;
1171 		case 23:
1172 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0x25,
1173 						      0x3, 0x31, 0x18);
1174 			rssi_adjust_val = 22;
1175 			break;
1176 		case 24:
1177 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0x15,
1178 						      0x3, 0x31, 0x18);
1179 			rssi_adjust_val = 22;
1180 			break;
1181 		case 25:
1182 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0xa,
1183 						      0x3, 0x31, 0x18);
1184 			rssi_adjust_val = 22;
1185 			break;
1186 		case 26:
1187 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0xa,
1188 						      0x3, 0x31, 0x18);
1189 			rssi_adjust_val = 22;
1190 			break;
1191 		case 27:
1192 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3, 0x25,
1193 						      0x3, 0x31, 0x98);
1194 			rssi_adjust_val = 22;
1195 			break;
1196 		case 28:
1197 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x69, 0x25,
1198 						      0x3, 0x31, 0x0);
1199 			break;
1200 		case 29:
1201 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xab, 0x1a,
1202 						      0x1a, 0x1, 0x10);
1203 			break;
1204 		case 30:
1205 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51, 0x14,
1206 						      0x3, 0x10, 0x50);
1207 			break;
1208 		case 31:
1209 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xd3, 0x1a,
1210 						      0x1a, 0, 0x58);
1211 			break;
1212 		case 32:
1213 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61, 0xa,
1214 						      0x3, 0x10, 0x0);
1215 			break;
1216 		case 33:
1217 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xa3, 0x25,
1218 						      0x3, 0x30, 0x90);
1219 			break;
1220 		case 34:
1221 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x53, 0x1a,
1222 						      0x1a, 0x0, 0x10);
1223 			break;
1224 		case 35:
1225 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x63, 0x1a,
1226 						      0x1a, 0x0, 0x10);
1227 			break;
1228 		case 36:
1229 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xd3, 0x12,
1230 						      0x3, 0x14, 0x50);
1231 			break;
1232 		}
1233 	} else {
1234 		/* disable PS tdma*/
1235 		switch (type) {
1236 		case 8: /*PTA Control*/
1237 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x8, 0x0, 0x0,
1238 						      0x0, 0x0);
1239 			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1240 						     false, false);
1241 			break;
1242 		case 0:
1243 		default:  /*Software control, Antenna at BT side*/
1244 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0, 0x0, 0x0,
1245 						      0x0, 0x0);
1246 			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1247 						     false, false);
1248 			break;
1249 		case 9:   /*Software control, Antenna at WiFi side*/
1250 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0, 0x0, 0x0,
1251 						      0x0, 0x0);
1252 			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
1253 						     false, false);
1254 			break;
1255 		case 10:	/* under 5G*/
1256 			halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0, 0x0, 0x0,
1257 						      0x8, 0x0);
1258 			halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1259 						     false, false);
1260 			break;
1261 		}
1262 	}
1263 	rssi_adjust_val = 0;
1264 	btcoexist->btc_set(btcoexist,
1265 		 BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssi_adjust_val);
1266 
1267 	/* update pre state*/
1268 	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1269 	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1270 }
1271 
1272 static bool halbtc8821a1ant_is_common_action(struct btc_coexist *btcoexist)
1273 {
1274 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1275 	bool	common = false, wifi_connected = false, wifi_busy = false;
1276 
1277 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1278 			   &wifi_connected);
1279 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1280 
1281 	if (!wifi_connected &&
1282 	    BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1283 	    coex_dm->bt_status) {
1284 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1285 			 "[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
1286 		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1287 
1288 		common = true;
1289 	} else if (wifi_connected &&
1290 		   (BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1291 		    coex_dm->bt_status)) {
1292 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1293 			 "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1294 		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1295 
1296 		common = true;
1297 	} else if (!wifi_connected &&
1298 		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1299 		    coex_dm->bt_status)) {
1300 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1301 			 "[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
1302 		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1303 
1304 		common = true;
1305 	} else if (wifi_connected &&
1306 		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1307 		   coex_dm->bt_status)) {
1308 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1309 			 "[BTCoex], Wifi connected + BT connected-idle!!\n");
1310 		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1311 
1312 		common = true;
1313 	} else if (!wifi_connected &&
1314 		   (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE !=
1315 		    coex_dm->bt_status)) {
1316 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1317 			 "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
1318 		halbtc8821a1ant_sw_mechanism(btcoexist, false);
1319 
1320 		common = true;
1321 	} else {
1322 		if (wifi_busy) {
1323 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1324 				 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1325 		} else {
1326 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1327 				 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1328 		}
1329 
1330 		common = false;
1331 	}
1332 
1333 	return common;
1334 }
1335 
1336 static void btc8821a1ant_tdma_dur_adj(struct btc_coexist *btcoexist,
1337 				      u8 wifi_status)
1338 {
1339 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1340 	static long		up, dn, m, n, wait_count;
1341 	/*0: no change, +1: increase WiFi duration, -1: decrease WiFi duration*/
1342 	long			result;
1343 	u8			retry_count = 0, bt_info_ext;
1344 
1345 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1346 		 "[BTCoex], TdmaDurationAdjustForAcl()\n");
1347 
1348 	if ((BT_8821A_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN ==
1349 	     wifi_status) ||
1350 	    (BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN ==
1351 	     wifi_status) ||
1352 	    (BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SPECIAL_PKT ==
1353 	     wifi_status)) {
1354 		if (coex_dm->cur_ps_tdma != 1 &&
1355 		    coex_dm->cur_ps_tdma != 2 &&
1356 		    coex_dm->cur_ps_tdma != 3 &&
1357 		    coex_dm->cur_ps_tdma != 9) {
1358 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1359 						true, 9);
1360 			coex_dm->tdma_adj_type = 9;
1361 
1362 			up = 0;
1363 			dn = 0;
1364 			m = 1;
1365 			n = 3;
1366 			result = 0;
1367 			wait_count = 0;
1368 		}
1369 		return;
1370 	}
1371 
1372 	if (!coex_dm->auto_tdma_adjust) {
1373 		coex_dm->auto_tdma_adjust = true;
1374 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1375 			 "[BTCoex], first run TdmaDurationAdjust()!!\n");
1376 
1377 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1378 		coex_dm->tdma_adj_type = 2;
1379 		/*============*/
1380 		up = 0;
1381 		dn = 0;
1382 		m = 1;
1383 		n = 3;
1384 		result = 0;
1385 		wait_count = 0;
1386 	} else {
1387 		/*accquire the BT TRx retry count from BT_Info byte2*/
1388 		retry_count = coex_sta->bt_retry_cnt;
1389 		bt_info_ext = coex_sta->bt_info_ext;
1390 		result = 0;
1391 		wait_count++;
1392 
1393 		if (retry_count == 0) {
1394 			/* no retry in the last 2-second duration*/
1395 			up++;
1396 			dn--;
1397 
1398 			if (dn <= 0)
1399 				dn = 0;
1400 
1401 			if (up >= n) {
1402 				/* if (retry count == 0) for 2*n seconds ,
1403 				 * make WiFi duration wider
1404 				 */
1405 				wait_count = 0;
1406 				n = 3;
1407 				up = 0;
1408 				dn = 0;
1409 				result = 1;
1410 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1411 					 "[BTCoex], Increase wifi duration!!\n");
1412 			}
1413 		} else if (retry_count <= 3) {
1414 			/* <=3 retry in the last 2-second duration*/
1415 			up--;
1416 			dn++;
1417 
1418 			if (up <= 0)
1419 				up = 0;
1420 
1421 			if (dn == 2) {
1422 				/* if retry count< 3 for 2*2 seconds,
1423 				 * shrink wifi duration
1424 				 */
1425 				if (wait_count <= 2)
1426 					m++; /* avoid bounce in two levels */
1427 				else
1428 					m = 1;
1429 
1430 				if (m >= 20) {
1431 					/* m max value is 20, max time is 120 s,
1432 					 *	recheck if adjust WiFi duration.
1433 					 */
1434 					m = 20;
1435 				}
1436 				n = 3*m;
1437 				up = 0;
1438 				dn = 0;
1439 				wait_count = 0;
1440 				result = -1;
1441 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1442 					 "[BTCoex], Decrease wifi duration for retryCounter<3!!\n");
1443 			}
1444 		} else {
1445 			/* retry count > 3, if retry count > 3 happens once,
1446 			 *	shrink WiFi duration
1447 			 */
1448 			if (wait_count == 1)
1449 				m++; /* avoid bounce in two levels */
1450 			else
1451 				m = 1;
1452 		/* m max value is 20, max time is 120 second,
1453 		 *	recheck if adjust WiFi duration.
1454 		*/
1455 			if (m >= 20)
1456 				m = 20;
1457 
1458 			n = 3*m;
1459 			up = 0;
1460 			dn = 0;
1461 			wait_count = 0;
1462 			result = -1;
1463 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1464 				 "[BTCoex], Decrease wifi duration for retryCounter>3!!\n");
1465 		}
1466 
1467 		if (result == -1) {
1468 			if ((BT_INFO_8821A_1ANT_A2DP_BASIC_RATE(bt_info_ext)) &&
1469 			    ((coex_dm->cur_ps_tdma == 1) ||
1470 			     (coex_dm->cur_ps_tdma == 2))) {
1471 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1472 							true, 9);
1473 				coex_dm->tdma_adj_type = 9;
1474 			} else if (coex_dm->cur_ps_tdma == 1) {
1475 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1476 							true, 2);
1477 				coex_dm->tdma_adj_type = 2;
1478 			} else if (coex_dm->cur_ps_tdma == 2) {
1479 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1480 							true, 9);
1481 				coex_dm->tdma_adj_type = 9;
1482 			} else if (coex_dm->cur_ps_tdma == 9) {
1483 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1484 							true, 11);
1485 				coex_dm->tdma_adj_type = 11;
1486 			}
1487 		} else if (result == 1) {
1488 			if ((BT_INFO_8821A_1ANT_A2DP_BASIC_RATE(bt_info_ext)) &&
1489 			    ((coex_dm->cur_ps_tdma == 1) ||
1490 			     (coex_dm->cur_ps_tdma == 2))) {
1491 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1492 							true, 9);
1493 				coex_dm->tdma_adj_type = 9;
1494 			} else if (coex_dm->cur_ps_tdma == 11) {
1495 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1496 							true, 9);
1497 				coex_dm->tdma_adj_type = 9;
1498 			} else if (coex_dm->cur_ps_tdma == 9) {
1499 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1500 							true, 2);
1501 				coex_dm->tdma_adj_type = 2;
1502 			} else if (coex_dm->cur_ps_tdma == 2) {
1503 				halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1504 							true, 1);
1505 				coex_dm->tdma_adj_type = 1;
1506 			}
1507 		} else {
1508 			/*no change*/
1509 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1510 				 "[BTCoex], ********** TDMA(on, %d) **********\n",
1511 				 coex_dm->cur_ps_tdma);
1512 		}
1513 
1514 		if (coex_dm->cur_ps_tdma != 1 &&
1515 		    coex_dm->cur_ps_tdma != 2 &&
1516 		    coex_dm->cur_ps_tdma != 9 &&
1517 		    coex_dm->cur_ps_tdma != 11) {
1518 			/* recover to previous adjust type*/
1519 			halbtc8821a1ant_ps_tdma(btcoexist,
1520 						NORMAL_EXEC, true,
1521 						coex_dm->tdma_adj_type);
1522 		}
1523 	}
1524 }
1525 
1526 static void btc8821a1ant_ps_tdma_check_for_pwr_save(struct btc_coexist *btcoex,
1527 						    bool new_ps_state)
1528 {
1529 	u8	lps_mode = 0x0;
1530 
1531 	btcoex->btc_get(btcoex, BTC_GET_U1_LPS_MODE, &lps_mode);
1532 
1533 	if (lps_mode) {
1534 		/* already under LPS state*/
1535 		if (new_ps_state) {
1536 			/* keep state under LPS, do nothing.*/
1537 		} else {
1538 			/* will leave LPS state, turn off psTdma first*/
1539 			halbtc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1540 		}
1541 	} else {
1542 		/* NO PS state*/
1543 		if (new_ps_state) {
1544 			/* will enter LPS state, turn off psTdma first*/
1545 			halbtc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1546 		} else {
1547 			/* keep state under NO PS state, do nothing.*/
1548 		}
1549 	}
1550 }
1551 
1552 static void halbtc8821a1ant_power_save_state(struct btc_coexist *btcoexist,
1553 					     u8 ps_type, u8 lps_val,
1554 					     u8 rpwm_val)
1555 {
1556 	bool low_pwr_disable = false;
1557 
1558 	switch (ps_type) {
1559 	case BTC_PS_WIFI_NATIVE:
1560 		/* recover to original 32k low power setting*/
1561 		low_pwr_disable = false;
1562 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1563 				   &low_pwr_disable);
1564 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1565 		break;
1566 	case BTC_PS_LPS_ON:
1567 		btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist,
1568 							true);
1569 		halbtc8821a1ant_lps_rpwm(btcoexist,
1570 					 NORMAL_EXEC, lps_val, rpwm_val);
1571 		/* when coex force to enter LPS, do not enter 32k low power.*/
1572 		low_pwr_disable = true;
1573 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1574 				   &low_pwr_disable);
1575 		/* power save must executed before psTdma.*/
1576 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1577 		break;
1578 	case BTC_PS_LPS_OFF:
1579 		btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist, false);
1580 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1581 		break;
1582 	default:
1583 		break;
1584 	}
1585 }
1586 
1587 static void halbtc8821a1ant_coex_under_5g(struct btc_coexist *btcoexist)
1588 {
1589 	halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1590 					 0x0, 0x0);
1591 	halbtc8821a1ant_ignore_wlan_act(btcoexist, NORMAL_EXEC, true);
1592 
1593 	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 10);
1594 
1595 	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1596 
1597 	halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1598 
1599 	halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 5);
1600 }
1601 
1602 static void halbtc8821a1ant_action_wifi_only(struct btc_coexist *btcoexist)
1603 {
1604 	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1605 	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 9);
1606 }
1607 
1608 static void btc8821a1ant_mon_bt_en_dis(struct btc_coexist *btcoexist)
1609 {
1610 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1611 	static bool	pre_bt_disabled;
1612 	static u32	bt_disable_cnt;
1613 	bool		bt_active = true, bt_disabled = false;
1614 
1615 	/* This function check if bt is disabled*/
1616 
1617 	if (coex_sta->high_priority_tx == 0 &&
1618 	    coex_sta->high_priority_rx == 0 &&
1619 	    coex_sta->low_priority_tx == 0 &&
1620 	    coex_sta->low_priority_rx == 0) {
1621 		bt_active = false;
1622 	}
1623 	if (coex_sta->high_priority_tx == 0xffff &&
1624 	    coex_sta->high_priority_rx == 0xffff &&
1625 	    coex_sta->low_priority_tx == 0xffff &&
1626 	    coex_sta->low_priority_rx == 0xffff) {
1627 		bt_active = false;
1628 	}
1629 	if (bt_active) {
1630 		bt_disable_cnt = 0;
1631 		bt_disabled = false;
1632 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1633 				   &bt_disabled);
1634 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1635 			 "[BTCoex], BT is enabled !!\n");
1636 	} else {
1637 		bt_disable_cnt++;
1638 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1639 			 "[BTCoex], bt all counters = 0, %d times!!\n",
1640 			 bt_disable_cnt);
1641 		if (bt_disable_cnt >= 2) {
1642 			bt_disabled = true;
1643 			btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1644 					   &bt_disabled);
1645 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1646 				 "[BTCoex], BT is disabled !!\n");
1647 			halbtc8821a1ant_action_wifi_only(btcoexist);
1648 		}
1649 	}
1650 	if (pre_bt_disabled != bt_disabled) {
1651 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1652 			 "[BTCoex], BT is from %s to %s!!\n",
1653 			    (pre_bt_disabled ? "disabled" : "enabled"),
1654 			    (bt_disabled ? "disabled" : "enabled"));
1655 		pre_bt_disabled = bt_disabled;
1656 		if (bt_disabled) {
1657 			btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1658 					   NULL);
1659 			btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1660 					   NULL);
1661 		}
1662 	}
1663 }
1664 
1665 /*=============================================*/
1666 /**/
1667 /*	Software Coex Mechanism start*/
1668 /**/
1669 /*=============================================*/
1670 
1671 /* SCO only or SCO+PAN(HS)*/
1672 static void halbtc8821a1ant_action_sco(struct btc_coexist *btcoexist)
1673 {
1674 	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1675 }
1676 
1677 static void halbtc8821a1ant_action_hid(struct btc_coexist *btcoexist)
1678 {
1679 	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1680 }
1681 
1682 /*A2DP only / PAN(EDR) only/ A2DP+PAN(HS)*/
1683 static void halbtc8821a1ant_action_a2dp(struct btc_coexist *btcoexist)
1684 {
1685 	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1686 }
1687 
1688 static void halbtc8821a1ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
1689 {
1690 	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1691 }
1692 
1693 static void halbtc8821a1ant_action_pan_edr(struct btc_coexist *btcoexist)
1694 {
1695 	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1696 }
1697 
1698 /*PAN(HS) only*/
1699 static void halbtc8821a1ant_action_pan_hs(struct btc_coexist *btcoexist)
1700 {
1701 	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1702 }
1703 
1704 /*PAN(EDR)+A2DP*/
1705 static void halbtc8821a1ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
1706 {
1707 	halbtc8821a1ant_sw_mechanism(btcoexist, false);
1708 }
1709 
1710 static void halbtc8821a1ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
1711 {
1712 	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1713 }
1714 
1715 /* HID+A2DP+PAN(EDR)*/
1716 static void btc8821a1ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
1717 {
1718 	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1719 }
1720 
1721 static void halbtc8821a1ant_action_hid_a2dp(struct btc_coexist *btcoexist)
1722 {
1723 	halbtc8821a1ant_sw_mechanism(btcoexist, true);
1724 }
1725 
1726 /*=============================================*/
1727 /**/
1728 /*	Non-Software Coex Mechanism start*/
1729 /**/
1730 /*=============================================*/
1731 
1732 static void halbtc8821a1ant_action_hs(struct btc_coexist *btcoexist)
1733 {
1734 	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1735 	halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 2);
1736 }
1737 
1738 static void halbtc8821a1ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1739 {
1740 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1741 	bool wifi_connected = false;
1742 
1743 	btcoexist->btc_get(btcoexist,
1744 		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
1745 
1746 	if (!wifi_connected) {
1747 		halbtc8821a1ant_power_save_state(btcoexist,
1748 						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1749 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1750 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1751 	} else if ((bt_link_info->sco_exist) ||
1752 		   (bt_link_info->hid_only)) {
1753 		/* SCO/HID-only busy*/
1754 		halbtc8821a1ant_power_save_state(btcoexist,
1755 						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1756 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1757 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1758 	} else {
1759 		halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_LPS_ON,
1760 						 0x50, 0x4);
1761 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 30);
1762 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1763 	}
1764 }
1765 
1766 static void btc8821a1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
1767 						  u8 wifi_status) {
1768 	/* tdma and coex table*/
1769 	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1770 
1771 	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1772 }
1773 
1774 static void btc8821a1ant_act_wifi_con_bt_acl_busy(struct btc_coexist *btcoexist,
1775 						  u8 wifi_status)
1776 {
1777 	u8		bt_rssi_state;
1778 
1779 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1780 
1781 	bt_rssi_state = halbtc8821a1ant_bt_rssi_state(btcoexist, 2, 28, 0);
1782 
1783 	if (bt_link_info->hid_only) {
1784 		/*HID*/
1785 		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1786 						      wifi_status);
1787 		coex_dm->auto_tdma_adjust = false;
1788 		return;
1789 	} else if (bt_link_info->a2dp_only) {
1790 		/*A2DP*/
1791 		if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1792 		    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1793 			btc8821a1ant_tdma_dur_adj(btcoexist, wifi_status);
1794 		} else {
1795 			/*for low BT RSSI*/
1796 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1797 						true, 11);
1798 			coex_dm->auto_tdma_adjust = false;
1799 		}
1800 
1801 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1802 	} else if (bt_link_info->hid_exist && bt_link_info->a2dp_exist) {
1803 		/*HID+A2DP*/
1804 		if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1805 		    (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1806 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1807 						true, 14);
1808 			coex_dm->auto_tdma_adjust = false;
1809 		} else {
1810 			/*for low BT RSSI*/
1811 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1812 						true, 11);
1813 			coex_dm->auto_tdma_adjust = false;
1814 		}
1815 
1816 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1817 	} else if ((bt_link_info->pan_only) ||
1818 		(bt_link_info->hid_exist && bt_link_info->pan_exist)) {
1819 		/*PAN(OPP, FTP), HID+PAN(OPP, FTP)*/
1820 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1821 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1822 		coex_dm->auto_tdma_adjust = false;
1823 	} else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1824 		   (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1825 		    bt_link_info->pan_exist)) {
1826 		/*A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP)*/
1827 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
1828 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1829 		coex_dm->auto_tdma_adjust = false;
1830 	} else {
1831 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1832 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1833 		coex_dm->auto_tdma_adjust = false;
1834 	}
1835 }
1836 
1837 static void halbtc8821a1ant_action_wifi_not_connected(
1838 	struct btc_coexist *btcoexist)
1839 {
1840 	/* power save state*/
1841 	halbtc8821a1ant_power_save_state(btcoexist,
1842 					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1843 
1844 	/* tdma and coex table*/
1845 	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1846 	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1847 }
1848 
1849 static void btc8821a1ant_act_wifi_not_conn_scan(struct btc_coexist *btcoexist)
1850 {
1851 	halbtc8821a1ant_power_save_state(btcoexist,
1852 					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1853 
1854 	halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1855 	halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1856 }
1857 
1858 static void halbtc8821a1ant_action_wifi_connected_scan(
1859 	struct btc_coexist *btcoexist) {
1860 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1861 
1862 	/* power save state*/
1863 	halbtc8821a1ant_power_save_state(btcoexist,
1864 					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1865 
1866 	/* tdma and coex table*/
1867 	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1868 		if (bt_link_info->a2dp_exist && bt_link_info->pan_exist) {
1869 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1870 						true, 22);
1871 			halbtc8821a1ant_coex_table_with_type(btcoexist,
1872 							     NORMAL_EXEC, 1);
1873 		} else {
1874 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1875 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1876 	}
1877 	} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1878 		    coex_dm->bt_status) ||
1879 		   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1880 		    coex_dm->bt_status)) {
1881 		btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1882 			BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1883 	} else {
1884 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1885 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1886 	}
1887 }
1888 
1889 static void btc8821a1ant_act_wifi_conn_sp_pkt(struct btc_coexist *btcoexist)
1890 {
1891 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1892 	bool	hs_connecting = false;
1893 
1894 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_CONNECTING, &hs_connecting);
1895 
1896 	halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1897 					 0x0, 0x0);
1898 
1899 	/* tdma and coex table*/
1900 	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1901 		if (bt_link_info->a2dp_exist && bt_link_info->pan_exist) {
1902 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1903 						true, 22);
1904 			halbtc8821a1ant_coex_table_with_type(btcoexist,
1905 							     NORMAL_EXEC, 1);
1906 		} else {
1907 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1908 						true, 20);
1909 			halbtc8821a1ant_coex_table_with_type(btcoexist,
1910 							     NORMAL_EXEC, 1);
1911 		}
1912 	} else {
1913 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1914 		halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1915 	}
1916 }
1917 
1918 static void halbtc8821a1ant_action_wifi_connected(struct btc_coexist *btcoexist)
1919 {
1920 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1921 	bool	wifi_busy = false;
1922 	bool	scan = false, link = false, roam = false;
1923 	bool	under_4way = false;
1924 
1925 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1926 		 "[BTCoex], CoexForWifiConnect()===>\n");
1927 
1928 	btcoexist->btc_get(btcoexist,
1929 		 BTC_GET_BL_WIFI_4_WAY_PROGRESS, &under_4way);
1930 	if (under_4way) {
1931 		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1932 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1933 			 "[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
1934 		return;
1935 	}
1936 
1937 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1938 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1939 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1940 	if (scan || link || roam) {
1941 		halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
1942 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1943 			 "[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
1944 		return;
1945 	}
1946 
1947 	/* power save state*/
1948 	if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY ==
1949 			coex_dm->bt_status && !btcoexist->bt_link_info.hid_only)
1950 		halbtc8821a1ant_power_save_state(btcoexist,
1951 						 BTC_PS_LPS_ON, 0x50, 0x4);
1952 	else
1953 		halbtc8821a1ant_power_save_state(btcoexist,
1954 						 BTC_PS_WIFI_NATIVE,
1955 						 0x0, 0x0);
1956 
1957 	/* tdma and coex table*/
1958 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1959 	if (!wifi_busy) {
1960 		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1961 			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1962 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1963 		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1964 			    coex_dm->bt_status) ||
1965 			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1966 			    coex_dm->bt_status)) {
1967 			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1968 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1969 		} else {
1970 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1971 						true, 5);
1972 			halbtc8821a1ant_coex_table_with_type(btcoexist,
1973 							     NORMAL_EXEC, 2);
1974 		}
1975 	} else {
1976 		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1977 			btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1978 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1979 		} else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1980 			    coex_dm->bt_status) ||
1981 			   (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1982 			    coex_dm->bt_status)) {
1983 			btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1984 				BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1985 		} else {
1986 			halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1987 						true, 5);
1988 			halbtc8821a1ant_coex_table_with_type(btcoexist,
1989 							     NORMAL_EXEC, 2);
1990 		}
1991 	}
1992 }
1993 
1994 static void btc8821a1ant_run_sw_coex_mech(struct btc_coexist *btcoexist)
1995 {
1996 	struct rtl_priv *rtlpriv = btcoexist->adapter;
1997 	u8	algorithm = 0;
1998 
1999 	algorithm = halbtc8821a1ant_action_algorithm(btcoexist);
2000 	coex_dm->cur_algorithm = algorithm;
2001 
2002 	if (!halbtc8821a1ant_is_common_action(btcoexist)) {
2003 		switch (coex_dm->cur_algorithm) {
2004 		case BT_8821A_1ANT_COEX_ALGO_SCO:
2005 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2006 				 "[BTCoex], Action algorithm = SCO\n");
2007 			halbtc8821a1ant_action_sco(btcoexist);
2008 			break;
2009 		case BT_8821A_1ANT_COEX_ALGO_HID:
2010 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2011 				 "[BTCoex], Action algorithm = HID\n");
2012 			halbtc8821a1ant_action_hid(btcoexist);
2013 			break;
2014 		case BT_8821A_1ANT_COEX_ALGO_A2DP:
2015 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2016 				 "[BTCoex], Action algorithm = A2DP\n");
2017 			halbtc8821a1ant_action_a2dp(btcoexist);
2018 			break;
2019 		case BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS:
2020 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2021 				 "[BTCoex], Action algorithm = A2DP+PAN(HS)\n");
2022 			halbtc8821a1ant_action_a2dp_pan_hs(btcoexist);
2023 			break;
2024 		case BT_8821A_1ANT_COEX_ALGO_PANEDR:
2025 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2026 				 "[BTCoex], Action algorithm = PAN(EDR)\n");
2027 			halbtc8821a1ant_action_pan_edr(btcoexist);
2028 			break;
2029 		case BT_8821A_1ANT_COEX_ALGO_PANHS:
2030 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2031 				 "[BTCoex], Action algorithm = HS mode\n");
2032 			halbtc8821a1ant_action_pan_hs(btcoexist);
2033 			break;
2034 		case BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP:
2035 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2036 				 "[BTCoex], Action algorithm = PAN+A2DP\n");
2037 			halbtc8821a1ant_action_pan_edr_a2dp(btcoexist);
2038 			break;
2039 		case BT_8821A_1ANT_COEX_ALGO_PANEDR_HID:
2040 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2041 				 "[BTCoex], Action algorithm = PAN(EDR)+HID\n");
2042 			halbtc8821a1ant_action_pan_edr_hid(btcoexist);
2043 			break;
2044 		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2045 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2046 				 "[BTCoex], Action algorithm = HID+A2DP+PAN\n");
2047 			btc8821a1ant_action_hid_a2dp_pan_edr(btcoexist);
2048 			break;
2049 		case BT_8821A_1ANT_COEX_ALGO_HID_A2DP:
2050 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2051 				 "[BTCoex], Action algorithm = HID+A2DP\n");
2052 			halbtc8821a1ant_action_hid_a2dp(btcoexist);
2053 			break;
2054 		default:
2055 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2056 				 "[BTCoex], Action algorithm = coexist All Off!!\n");
2057 			/*halbtc8821a1ant_coex_all_off(btcoexist);*/
2058 			break;
2059 		}
2060 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
2061 	}
2062 }
2063 
2064 static void halbtc8821a1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
2065 {
2066 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2067 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2068 	bool	wifi_connected = false, bt_hs_on = false;
2069 	bool	increase_scan_dev_num = false;
2070 	bool	bt_ctrl_agg_buf_size = false;
2071 	u8	agg_buf_size = 5;
2072 	u8	wifi_rssi_state = BTC_RSSI_STATE_HIGH;
2073 	bool	wifi_under_5g = false;
2074 
2075 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2076 		 "[BTCoex], RunCoexistMechanism()===>\n");
2077 
2078 	if (btcoexist->manual_control) {
2079 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2080 			 "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2081 		return;
2082 	}
2083 
2084 	if (btcoexist->stop_coex_dm) {
2085 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2086 			 "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2087 		return;
2088 	}
2089 
2090 	if (coex_sta->under_ips) {
2091 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2092 			 "[BTCoex], wifi is under IPS !!!\n");
2093 		return;
2094 	}
2095 
2096 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2097 	if (wifi_under_5g) {
2098 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2099 			 "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2100 		halbtc8821a1ant_coex_under_5g(btcoexist);
2101 		return;
2102 	}
2103 
2104 	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2105 	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2106 	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2107 		increase_scan_dev_num = true;
2108 
2109 	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2110 			   &increase_scan_dev_num);
2111 
2112 	btcoexist->btc_get(btcoexist,
2113 		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2114 
2115 	if (!bt_link_info->sco_exist && !bt_link_info->hid_exist) {
2116 		halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2117 	} else {
2118 		if (wifi_connected) {
2119 			wifi_rssi_state =
2120 				 halbtc8821a1ant_WifiRssiState(btcoexist, 1, 2,
2121 							       30, 0);
2122 			halbtc8821a1ant_limited_tx(btcoexist,
2123 						   NORMAL_EXEC, 1, 1, 1, 1);
2124 		} else {
2125 			halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC,
2126 						   0, 0, 0, 0);
2127 		}
2128 	}
2129 
2130 	if (bt_link_info->sco_exist) {
2131 		bt_ctrl_agg_buf_size = true;
2132 		agg_buf_size = 0x3;
2133 	} else if (bt_link_info->hid_exist) {
2134 		bt_ctrl_agg_buf_size = true;
2135 		agg_buf_size = 0x5;
2136 	} else if (bt_link_info->a2dp_exist || bt_link_info->pan_exist) {
2137 		bt_ctrl_agg_buf_size = true;
2138 		agg_buf_size = 0x8;
2139 	}
2140 	halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2141 				   bt_ctrl_agg_buf_size, agg_buf_size);
2142 
2143 	btc8821a1ant_run_sw_coex_mech(btcoexist);
2144 
2145 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2146 	if (coex_sta->c2h_bt_inquiry_page) {
2147 		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2148 		return;
2149 	} else if (bt_hs_on) {
2150 		halbtc8821a1ant_action_hs(btcoexist);
2151 		return;
2152 	}
2153 
2154 	if (!wifi_connected) {
2155 		bool	scan = false, link = false, roam = false;
2156 
2157 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2158 			 "[BTCoex], wifi is non connected-idle !!!\n");
2159 
2160 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2161 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2162 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2163 
2164 		if (scan || link || roam)
2165 			btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2166 		else
2167 			halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2168 	} else {
2169 		/* wifi LPS/Busy*/
2170 		halbtc8821a1ant_action_wifi_connected(btcoexist);
2171 	}
2172 }
2173 
2174 static void halbtc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2175 {
2176 	/* force to reset coex mechanism*/
2177 	/* sw all off*/
2178 	halbtc8821a1ant_sw_mechanism(btcoexist, false);
2179 
2180 	halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2181 	halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2182 }
2183 
2184 static void halbtc8821a1ant_init_hw_config(struct btc_coexist *btcoexist,
2185 					   bool back_up)
2186 {
2187 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2188 	u8	u1_tmp = 0;
2189 	bool	wifi_under_5g = false;
2190 
2191 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2192 		 "[BTCoex], 1Ant Init HW Config!!\n");
2193 
2194 	if (back_up) {
2195 		coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2196 								      0x430);
2197 		coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2198 								      0x434);
2199 		coex_dm->backup_retry_limit =
2200 			 btcoexist->btc_read_2byte(btcoexist, 0x42a);
2201 		coex_dm->backup_ampdu_max_time =
2202 			 btcoexist->btc_read_1byte(btcoexist, 0x456);
2203 	}
2204 
2205 	/* 0x790[5:0] = 0x5*/
2206 	u1_tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2207 	u1_tmp &= 0xc0;
2208 	u1_tmp |= 0x5;
2209 	btcoexist->btc_write_1byte(btcoexist, 0x790, u1_tmp);
2210 
2211 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2212 
2213 	/*Antenna config*/
2214 	if (wifi_under_5g)
2215 		halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2216 					     true, false);
2217 	else
2218 		halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2219 					     true, false);
2220 	/* PTA parameter*/
2221 	halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2222 
2223 	/* Enable counter statistics*/
2224 	/*0x76e[3] =1, WLAN_Act control by PTA*/
2225 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
2226 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
2227 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2228 }
2229 
2230 /*============================================================*/
2231 /* work around function start with wa_halbtc8821a1ant_*/
2232 /*============================================================*/
2233 /*============================================================*/
2234 /* extern function start with EXhalbtc8821a1ant_*/
2235 /*============================================================*/
2236 void ex_halbtc8821a1ant_init_hwconfig(struct btc_coexist *btcoexist)
2237 {
2238 	halbtc8821a1ant_init_hw_config(btcoexist, true);
2239 }
2240 
2241 void ex_halbtc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2242 {
2243 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2244 
2245 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2246 		 "[BTCoex], Coex Mechanism Init!!\n");
2247 
2248 	btcoexist->stop_coex_dm = false;
2249 
2250 	halbtc8821a1ant_init_coex_dm(btcoexist);
2251 
2252 	halbtc8821a1ant_query_bt_info(btcoexist);
2253 }
2254 
2255 void ex_halbtc8821a1ant_display_coex_info(struct btc_coexist *btcoexist)
2256 {
2257 	struct btc_board_info *board_info = &btcoexist->board_info;
2258 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2259 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2260 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2261 	u8 u1_tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2262 	u16 u2_tmp[4];
2263 	u32 u4_tmp[4];
2264 	bool roam = false, scan = false, link = false, wifi_under_5g = false;
2265 	bool bt_hs_on = false, wifi_busy = false;
2266 	long wifi_rssi = 0, bt_hs_rssi = 0;
2267 	u32 wifi_bw, wifi_traffic_dir;
2268 	u8 wifi_dot11_chnl, wifi_hs_chnl;
2269 	u32 fw_ver = 0, bt_patch_ver = 0;
2270 
2271 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2272 		 "\r\n ============[BT Coexist info]============");
2273 
2274 	if (btcoexist->manual_control) {
2275 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2276 			 "\r\n ============[Under Manual Control]============");
2277 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2278 			 "\r\n ==========================================");
2279 	}
2280 	if (btcoexist->stop_coex_dm) {
2281 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2282 			 "\r\n ============[Coex is STOPPED]============");
2283 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2284 			 "\r\n ==========================================");
2285 	}
2286 
2287 	if (!board_info->bt_exist) {
2288 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n BT not exists !!!");
2289 		return;
2290 	}
2291 
2292 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2293 		 "\r\n %-35s = %d/ %d/ %d",
2294 		 "Ant PG Num/ Ant Mech/ Ant Pos:",
2295 		 board_info->pg_ant_num,
2296 		 board_info->btdm_ant_num,
2297 		 board_info->btdm_ant_pos);
2298 
2299 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2300 		 "\r\n %-35s = %s / %d", "BT stack/ hci ext ver",
2301 		 ((stack_info->profile_notified) ? "Yes" : "No"),
2302 		 stack_info->hci_version);
2303 
2304 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2305 			   &bt_patch_ver);
2306 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2307 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2308 		 "\r\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2309 		 "CoexVer/ FwVer/ PatchVer",
2310 		 glcoex_ver_date_8821a_1ant,
2311 		 glcoex_ver_8821a_1ant,
2312 		 fw_ver, bt_patch_ver,
2313 		 bt_patch_ver);
2314 
2315 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION,
2316 			   &bt_hs_on);
2317 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2318 			   &wifi_dot11_chnl);
2319 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL,
2320 			   &wifi_hs_chnl);
2321 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2322 		 "\r\n %-35s = %d / %d(%d)",
2323 		 "Dot11 channel / HsChnl(HsMode)",
2324 		 wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2325 
2326 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2327 		 "\r\n %-35s = %3ph ",
2328 		 "H2C Wifi inform bt chnl Info",
2329 		 coex_dm->wifi_chnl_info);
2330 
2331 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2332 	btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2333 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2334 		 "\r\n %-35s = %d/ %d", "Wifi rssi/ HS rssi",
2335 		 (int)wifi_rssi, (int)bt_hs_rssi);
2336 
2337 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2338 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2339 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2340 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2341 		 "\r\n %-35s = %d/ %d/ %d ", "Wifi link/ roam/ scan",
2342 		 link, roam, scan);
2343 
2344 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G,
2345 			   &wifi_under_5g);
2346 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW,
2347 			   &wifi_bw);
2348 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
2349 			   &wifi_busy);
2350 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2351 			   &wifi_traffic_dir);
2352 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2353 		 "\r\n %-35s = %s / %s/ %s ", "Wifi status",
2354 		 (wifi_under_5g ? "5G" : "2.4G"),
2355 		 ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
2356 		 (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
2357 		 ((!wifi_busy) ? "idle" :
2358 		 ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
2359 		 "uplink" : "downlink")));
2360 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2361 		   "\r\n %-35s = [%s/ %d/ %d] ", "BT [status/ rssi/ retryCnt]",
2362 		   ((btcoexist->bt_info.bt_disabled) ? ("disabled") :
2363 		   ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2364 		   ((BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2365 		     coex_dm->bt_status) ?
2366 		   "non-connected idle" :
2367 		   ((BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
2368 		     coex_dm->bt_status) ?
2369 		   "connected-idle" : "busy")))),
2370 		   coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2371 
2372 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2373 		 "\r\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP",
2374 		 bt_link_info->sco_exist,
2375 		 bt_link_info->hid_exist,
2376 		 bt_link_info->pan_exist,
2377 		 bt_link_info->a2dp_exist);
2378 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
2379 
2380 	bt_info_ext = coex_sta->bt_info_ext;
2381 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2382 		 "\r\n %-35s = %s",
2383 		 "BT Info A2DP rate",
2384 		 (bt_info_ext & BIT0) ?
2385 		 "Basic rate" : "EDR rate");
2386 
2387 	for (i = 0; i < BT_INFO_SRC_8821A_1ANT_MAX; i++) {
2388 		if (coex_sta->bt_info_c2h_cnt[i]) {
2389 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2390 				 "\r\n %-35s = %7ph(%d)",
2391 				 glbt_info_src_8821a_1ant[i],
2392 				 coex_sta->bt_info_c2h[i],
2393 				 coex_sta->bt_info_c2h_cnt[i]);
2394 		}
2395 	}
2396 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2397 		 "\r\n %-35s = %s/%s, (0x%x/0x%x)",
2398 		 "PS state, IPS/LPS, (lps/rpwm)",
2399 		 ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2400 		 ((coex_sta->under_Lps ? "LPS ON" : "LPS OFF")),
2401 		 btcoexist->bt_info.lps_val,
2402 		 btcoexist->bt_info.rpwm_val);
2403 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD);
2404 
2405 	if (!btcoexist->manual_control) {
2406 		/* Sw mechanism*/
2407 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2408 			 "\r\n %-35s",
2409 			 "============[Sw mechanism]============");
2410 
2411 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2412 			 "\r\n %-35s = %d", "SM[LowPenaltyRA]",
2413 			 coex_dm->cur_low_penalty_ra);
2414 
2415 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2416 			 "\r\n %-35s = %s/ %s/ %d ",
2417 			 "DelBA/ BtCtrlAgg/ AggSize",
2418 			 (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2419 			 (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2420 			 btcoexist->bt_info.agg_buf_size);
2421 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2422 			 "\r\n %-35s = 0x%x ", "Rate Mask",
2423 			 btcoexist->bt_info.ra_mask);
2424 
2425 		/* Fw mechanism*/
2426 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "\r\n %-35s",
2427 			 "============[Fw mechanism]============");
2428 
2429 		ps_tdma_case = coex_dm->cur_ps_tdma;
2430 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2431 			 "\r\n %-35s = %5ph case-%d (auto:%d)",
2432 			 "PS TDMA",
2433 			 coex_dm->ps_tdma_para,
2434 			 ps_tdma_case,
2435 			 coex_dm->auto_tdma_adjust);
2436 
2437 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2438 			 "\r\n %-35s = 0x%x ",
2439 			 "Latest error condition(should be 0)",
2440 			   coex_dm->error_condition);
2441 
2442 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2443 			 "\r\n %-35s = %d ", "IgnWlanAct",
2444 			 coex_dm->cur_ignore_wlan_act);
2445 	}
2446 
2447 	/* Hw setting*/
2448 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2449 		 "\r\n %-35s", "============[Hw setting]============");
2450 
2451 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2452 		 "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2453 		 "backup ARFR1/ARFR2/RL/AMaxTime",
2454 		 coex_dm->backup_arfr_cnt1,
2455 		 coex_dm->backup_arfr_cnt2,
2456 		 coex_dm->backup_retry_limit,
2457 		 coex_dm->backup_ampdu_max_time);
2458 
2459 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2460 	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2461 	u2_tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2462 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2463 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2464 		 "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2465 		 "0x430/0x434/0x42a/0x456",
2466 		 u4_tmp[0], u4_tmp[1], u2_tmp[0], u1_tmp[0]);
2467 
2468 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2469 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc58);
2470 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2471 		 "\r\n %-35s = 0x%x/ 0x%x", "0x778/ 0xc58[29:25]",
2472 		 u1_tmp[0], (u4_tmp[0] & 0x3e000000) >> 25);
2473 
2474 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x8db);
2475 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2476 		 "\r\n %-35s = 0x%x", "0x8db[6:5]",
2477 		 ((u1_tmp[0] & 0x60) >> 5));
2478 
2479 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x975);
2480 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xcb4);
2481 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2482 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2483 		 "0xcb4[29:28]/0xcb4[7:0]/0x974[9:8]",
2484 		 (u4_tmp[0] & 0x30000000) >> 28,
2485 		  u4_tmp[0] & 0xff,
2486 		  u1_tmp[0] & 0x3);
2487 
2488 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2489 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2490 	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2491 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2492 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2493 		 "0x40/0x4c[24:23]/0x64[0]",
2494 		 u1_tmp[0], ((u4_tmp[0] & 0x01800000) >> 23), u1_tmp[1] & 0x1);
2495 
2496 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2497 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2498 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2499 		 "\r\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522",
2500 		 u4_tmp[0], u1_tmp[0]);
2501 
2502 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2503 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2504 		 "\r\n %-35s = 0x%x", "0xc50(dig)",
2505 		 u4_tmp[0] & 0xff);
2506 
2507 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xf48);
2508 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5d);
2509 	u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2510 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2511 		 "\r\n %-35s = 0x%x/ 0x%x", "OFDM-FA/ CCK-FA",
2512 		 u4_tmp[0], (u1_tmp[0] << 8) + u1_tmp[1]);
2513 
2514 	u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2515 	u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2516 	u4_tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2517 	u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
2518 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2519 		 "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2520 		 "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
2521 		   u4_tmp[0], u4_tmp[1], u4_tmp[2], u1_tmp[0]);
2522 
2523 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2524 		 "\r\n %-35s = %d/ %d", "0x770(high-pri rx/tx)",
2525 		 coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2526 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2527 		 "\r\n %-35s = %d/ %d", "0x774(low-pri rx/tx)",
2528 		 coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2529 #if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 1)
2530 	halbtc8821a1ant_monitor_bt_ctr(btcoexist);
2531 #endif
2532 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS);
2533 }
2534 
2535 void ex_halbtc8821a1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2536 {
2537 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2538 
2539 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2540 		return;
2541 
2542 	if (BTC_IPS_ENTER == type) {
2543 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2544 			 "[BTCoex], IPS ENTER notify\n");
2545 		coex_sta->under_ips = true;
2546 		halbtc8821a1ant_set_ant_path(btcoexist,
2547 					     BTC_ANT_PATH_BT, false, true);
2548 		/*set PTA control*/
2549 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
2550 		halbtc8821a1ant_coex_table_with_type(btcoexist,
2551 						     NORMAL_EXEC, 0);
2552 	} else if (BTC_IPS_LEAVE == type) {
2553 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2554 			 "[BTCoex], IPS LEAVE notify\n");
2555 		coex_sta->under_ips = false;
2556 
2557 		halbtc8821a1ant_run_coexist_mechanism(btcoexist);
2558 	}
2559 }
2560 
2561 void ex_halbtc8821a1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2562 {
2563 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2564 
2565 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2566 		return;
2567 
2568 	if (BTC_LPS_ENABLE == type) {
2569 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2570 			 "[BTCoex], LPS ENABLE notify\n");
2571 		coex_sta->under_Lps = true;
2572 	} else if (BTC_LPS_DISABLE == type) {
2573 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2574 			 "[BTCoex], LPS DISABLE notify\n");
2575 		coex_sta->under_Lps = false;
2576 	}
2577 }
2578 
2579 void ex_halbtc8821a1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2580 {
2581 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2582 	bool wifi_connected = false, bt_hs_on = false;
2583 
2584 	if (btcoexist->manual_control ||
2585 	    btcoexist->stop_coex_dm ||
2586 	    btcoexist->bt_info.bt_disabled)
2587 		return;
2588 
2589 	btcoexist->btc_get(btcoexist,
2590 		 BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2591 	btcoexist->btc_get(btcoexist,
2592 		 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2593 
2594 	halbtc8821a1ant_query_bt_info(btcoexist);
2595 
2596 	if (coex_sta->c2h_bt_inquiry_page) {
2597 		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2598 		return;
2599 	} else if (bt_hs_on) {
2600 		halbtc8821a1ant_action_hs(btcoexist);
2601 		return;
2602 	}
2603 
2604 	if (BTC_SCAN_START == type) {
2605 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2606 			 "[BTCoex], SCAN START notify\n");
2607 		if (!wifi_connected) {
2608 			/* non-connected scan*/
2609 			btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2610 		} else {
2611 			/* wifi is connected*/
2612 			halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
2613 		}
2614 	} else if (BTC_SCAN_FINISH == type) {
2615 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2616 			 "[BTCoex], SCAN FINISH notify\n");
2617 		if (!wifi_connected) {
2618 			/* non-connected scan*/
2619 			halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2620 		} else {
2621 			halbtc8821a1ant_action_wifi_connected(btcoexist);
2622 		}
2623 	}
2624 }
2625 
2626 void ex_halbtc8821a1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2627 {
2628 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2629 	bool	wifi_connected = false, bt_hs_on = false;
2630 
2631 	if (btcoexist->manual_control ||
2632 	    btcoexist->stop_coex_dm ||
2633 	    btcoexist->bt_info.bt_disabled)
2634 		return;
2635 
2636 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2637 	if (coex_sta->c2h_bt_inquiry_page) {
2638 		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2639 		return;
2640 	} else if (bt_hs_on) {
2641 		halbtc8821a1ant_action_hs(btcoexist);
2642 		return;
2643 	}
2644 
2645 	if (BTC_ASSOCIATE_START == type) {
2646 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2647 			 "[BTCoex], CONNECT START notify\n");
2648 		btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2649 	} else if (BTC_ASSOCIATE_FINISH == type) {
2650 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2651 			 "[BTCoex], CONNECT FINISH notify\n");
2652 
2653 		btcoexist->btc_get(btcoexist,
2654 			 BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2655 		if (!wifi_connected) {
2656 			/* non-connected scan*/
2657 			halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2658 		} else {
2659 			halbtc8821a1ant_action_wifi_connected(btcoexist);
2660 		}
2661 	}
2662 }
2663 
2664 void ex_halbtc8821a1ant_media_status_notify(struct btc_coexist *btcoexist,
2665 					    u8 type)
2666 {
2667 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2668 	u8 h2c_parameter[3] = {0};
2669 	u32 wifi_bw;
2670 	u8 wifi_central_chnl;
2671 
2672 	if (btcoexist->manual_control ||
2673 	    btcoexist->stop_coex_dm ||
2674 	    btcoexist->bt_info.bt_disabled)
2675 		return;
2676 
2677 	if (BTC_MEDIA_CONNECT == type) {
2678 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2679 			 "[BTCoex], MEDIA connect notify\n");
2680 	} else {
2681 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2682 			 "[BTCoex], MEDIA disconnect notify\n");
2683 	}
2684 
2685 	/* only 2.4G we need to inform bt the chnl mask*/
2686 	btcoexist->btc_get(btcoexist,
2687 			   BTC_GET_U1_WIFI_CENTRAL_CHNL,
2688 			   &wifi_central_chnl);
2689 	if ((BTC_MEDIA_CONNECT == type) &&
2690 	    (wifi_central_chnl <= 14)) {
2691 		/*h2c_parameter[0] = 0x1;*/
2692 		h2c_parameter[0] = 0x0;
2693 		h2c_parameter[1] = wifi_central_chnl;
2694 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2695 		if (BTC_WIFI_BW_HT40 == wifi_bw)
2696 			h2c_parameter[2] = 0x30;
2697 		else
2698 			h2c_parameter[2] = 0x20;
2699 	}
2700 
2701 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2702 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2703 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2704 
2705 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2706 		 "[BTCoex], FW write 0x66 = 0x%x\n",
2707 		 h2c_parameter[0] << 16 |
2708 		 h2c_parameter[1] << 8 |
2709 		 h2c_parameter[2]);
2710 
2711 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2712 }
2713 
2714 void ex_halbtc8821a1ant_special_packet_notify(struct btc_coexist *btcoexist,
2715 					      u8 type)
2716 {
2717 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2718 	bool bt_hs_on = false;
2719 
2720 	if (btcoexist->manual_control ||
2721 	    btcoexist->stop_coex_dm ||
2722 	    btcoexist->bt_info.bt_disabled)
2723 		return;
2724 
2725 	coex_sta->special_pkt_period_cnt = 0;
2726 
2727 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2728 	if (coex_sta->c2h_bt_inquiry_page) {
2729 		halbtc8821a1ant_action_bt_inquiry(btcoexist);
2730 		return;
2731 	} else if (bt_hs_on) {
2732 		halbtc8821a1ant_action_hs(btcoexist);
2733 		return;
2734 	}
2735 
2736 	if (BTC_PACKET_DHCP == type ||
2737 	    BTC_PACKET_EAPOL == type) {
2738 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2739 			 "[BTCoex], special Packet(%d) notify\n", type);
2740 		btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
2741 	}
2742 }
2743 
2744 void ex_halbtc8821a1ant_bt_info_notify(struct btc_coexist *btcoexist,
2745 				       u8 *tmp_buf, u8 length)
2746 {
2747 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2748 	u8 bt_info = 0;
2749 	u8 i, rsp_source = 0;
2750 	bool wifi_connected = false;
2751 	bool bt_busy = false;
2752 	bool wifi_under_5g = false;
2753 
2754 	coex_sta->c2h_bt_info_req_sent = false;
2755 
2756 	btcoexist->btc_get(btcoexist,
2757 		 BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2758 
2759 	rsp_source = tmp_buf[0]&0xf;
2760 	if (rsp_source >= BT_INFO_SRC_8821A_1ANT_MAX)
2761 		rsp_source = BT_INFO_SRC_8821A_1ANT_WIFI_FW;
2762 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
2763 
2764 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2765 		 "[BTCoex], Bt info[%d], length = %d, hex data = [",
2766 		 rsp_source, length);
2767 	for (i = 0; i < length; i++) {
2768 		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
2769 		if (i == 1)
2770 			bt_info = tmp_buf[i];
2771 		if (i == length-1) {
2772 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2773 				 "0x%02x]\n", tmp_buf[i]);
2774 		} else {
2775 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2776 				 "0x%02x, ", tmp_buf[i]);
2777 		}
2778 	}
2779 
2780 	if (BT_INFO_SRC_8821A_1ANT_WIFI_FW != rsp_source) {
2781 		coex_sta->bt_retry_cnt =	/* [3:0]*/
2782 			coex_sta->bt_info_c2h[rsp_source][2]&0xf;
2783 
2784 		coex_sta->bt_rssi =
2785 			coex_sta->bt_info_c2h[rsp_source][3]*2+10;
2786 
2787 		coex_sta->bt_info_ext =
2788 			coex_sta->bt_info_c2h[rsp_source][4];
2789 
2790 		/* Here we need to resend some wifi info to BT*/
2791 		/* because bt is reset and loss of the info.*/
2792 		if (coex_sta->bt_info_ext & BIT1) {
2793 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2794 				 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
2795 			btcoexist->btc_get(btcoexist,
2796 					   BTC_GET_BL_WIFI_CONNECTED,
2797 					   &wifi_connected);
2798 			if (wifi_connected) {
2799 				ex_halbtc8821a1ant_media_status_notify(btcoexist,
2800 							       BTC_MEDIA_CONNECT);
2801 			} else {
2802 				ex_halbtc8821a1ant_media_status_notify(btcoexist,
2803 							       BTC_MEDIA_DISCONNECT);
2804 			}
2805 		}
2806 
2807 		if ((coex_sta->bt_info_ext & BIT3) && !wifi_under_5g) {
2808 			if (!btcoexist->manual_control &&
2809 			    !btcoexist->stop_coex_dm) {
2810 				RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2811 					 "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
2812 				halbtc8821a1ant_ignore_wlan_act(btcoexist,
2813 								FORCE_EXEC,
2814 								false);
2815 			}
2816 		}
2817 #if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 0)
2818 		if (!(coex_sta->bt_info_ext & BIT4)) {
2819 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2820 				 "[BTCoex], BT ext info bit4 check, set BT to enable Auto Report!!\n");
2821 			halbtc8821a1ant_bt_auto_report(btcoexist,
2822 						       FORCE_EXEC, true);
2823 		}
2824 #endif
2825 	}
2826 
2827 	/* check BIT2 first ==> check if bt is under inquiry or page scan*/
2828 	if (bt_info & BT_INFO_8821A_1ANT_B_INQ_PAGE)
2829 		coex_sta->c2h_bt_inquiry_page = true;
2830 	else
2831 		coex_sta->c2h_bt_inquiry_page = false;
2832 
2833 	/* set link exist status*/
2834 	if (!(bt_info&BT_INFO_8821A_1ANT_B_CONNECTION)) {
2835 		coex_sta->bt_link_exist = false;
2836 		coex_sta->pan_exist = false;
2837 		coex_sta->a2dp_exist = false;
2838 		coex_sta->hid_exist = false;
2839 		coex_sta->sco_exist = false;
2840 	} else {
2841 		/* connection exists*/
2842 		coex_sta->bt_link_exist = true;
2843 		if (bt_info & BT_INFO_8821A_1ANT_B_FTP)
2844 			coex_sta->pan_exist = true;
2845 		else
2846 			coex_sta->pan_exist = false;
2847 		if (bt_info & BT_INFO_8821A_1ANT_B_A2DP)
2848 			coex_sta->a2dp_exist = true;
2849 		else
2850 			coex_sta->a2dp_exist = false;
2851 		if (bt_info & BT_INFO_8821A_1ANT_B_HID)
2852 			coex_sta->hid_exist = true;
2853 		else
2854 			coex_sta->hid_exist = false;
2855 		if (bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO)
2856 			coex_sta->sco_exist = true;
2857 		else
2858 			coex_sta->sco_exist = false;
2859 	}
2860 
2861 	halbtc8821a1ant_update_bt_link_info(btcoexist);
2862 
2863 	if (!(bt_info&BT_INFO_8821A_1ANT_B_CONNECTION)) {
2864 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
2865 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2866 			 "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
2867 	} else if (bt_info == BT_INFO_8821A_1ANT_B_CONNECTION) {
2868 		/* connection exists but no busy*/
2869 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE;
2870 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2871 			 "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
2872 	} else if ((bt_info&BT_INFO_8821A_1ANT_B_SCO_ESCO) ||
2873 		(bt_info&BT_INFO_8821A_1ANT_B_SCO_BUSY)) {
2874 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_SCO_BUSY;
2875 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2876 			 "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
2877 	} else if (bt_info&BT_INFO_8821A_1ANT_B_ACL_BUSY) {
2878 		if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
2879 			coex_dm->auto_tdma_adjust = false;
2880 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_ACL_BUSY;
2881 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2882 			 "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
2883 	} else {
2884 		coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_MAX;
2885 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2886 			 "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
2887 	}
2888 
2889 	if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2890 	    (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2891 	    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2892 		bt_busy = true;
2893 	else
2894 		bt_busy = false;
2895 	btcoexist->btc_set(btcoexist,
2896 			   BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2897 
2898 	halbtc8821a1ant_run_coexist_mechanism(btcoexist);
2899 }
2900 
2901 void ex_halbtc8821a1ant_halt_notify(struct btc_coexist *btcoexist)
2902 {
2903 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2904 
2905 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2906 		 "[BTCoex], Halt notify\n");
2907 
2908 	btcoexist->stop_coex_dm = true;
2909 
2910 	halbtc8821a1ant_set_ant_path(btcoexist,
2911 				     BTC_ANT_PATH_BT, false, true);
2912 	halbtc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2913 
2914 	halbtc8821a1ant_power_save_state(btcoexist,
2915 					 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2916 	halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
2917 
2918 	ex_halbtc8821a1ant_media_status_notify(btcoexist,
2919 					       BTC_MEDIA_DISCONNECT);
2920 }
2921 
2922 void ex_halbtc8821a1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
2923 {
2924 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2925 
2926 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2927 		 "[BTCoex], Pnp notify\n");
2928 
2929 	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
2930 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2931 			 "[BTCoex], Pnp notify to SLEEP\n");
2932 		btcoexist->stop_coex_dm = true;
2933 		halbtc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2934 		halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2935 						 0x0, 0x0);
2936 		halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 9);
2937 	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
2938 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2939 			 "[BTCoex], Pnp notify to WAKE UP\n");
2940 		btcoexist->stop_coex_dm = false;
2941 		halbtc8821a1ant_init_hw_config(btcoexist, false);
2942 		halbtc8821a1ant_init_coex_dm(btcoexist);
2943 		halbtc8821a1ant_query_bt_info(btcoexist);
2944 	}
2945 }
2946 
2947 void ex_halbtc8821a1ant_periodical(struct btc_coexist *btcoexist)
2948 {
2949 	struct rtl_priv *rtlpriv = btcoexist->adapter;
2950 	static u8	dis_ver_info_cnt;
2951 	u32		fw_ver = 0, bt_patch_ver = 0;
2952 	struct btc_board_info *board_info = &btcoexist->board_info;
2953 	struct btc_stack_info *stack_info = &btcoexist->stack_info;
2954 
2955 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2956 		 "[BTCoex], ==========================Periodical===========================\n");
2957 
2958 	if (dis_ver_info_cnt <= 5) {
2959 		dis_ver_info_cnt += 1;
2960 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2961 			 "[BTCoex], ****************************************************************\n");
2962 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2963 			 "[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
2964 			      board_info->pg_ant_num,
2965 			      board_info->btdm_ant_num,
2966 			      board_info->btdm_ant_pos);
2967 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2968 			 "[BTCoex], BT stack/ hci ext ver = %s / %d\n",
2969 			      stack_info->profile_notified ? "Yes" : "No",
2970 			      stack_info->hci_version);
2971 		btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2972 				   &bt_patch_ver);
2973 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2974 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2975 			 "[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
2976 			      glcoex_ver_date_8821a_1ant,
2977 			      glcoex_ver_8821a_1ant,
2978 			      fw_ver, bt_patch_ver,
2979 			      bt_patch_ver);
2980 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2981 			 "[BTCoex], ****************************************************************\n");
2982 	}
2983 
2984 #if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 0)
2985 	halbtc8821a1ant_query_bt_info(btcoexist);
2986 	halbtc8821a1ant_monitor_bt_ctr(btcoexist);
2987 	btc8821a1ant_mon_bt_en_dis(btcoexist);
2988 #else
2989 	if (halbtc8821a1ant_Is_wifi_status_changed(btcoexist) ||
2990 	    coex_dm->auto_tdma_adjust) {
2991 		if (coex_sta->special_pkt_period_cnt > 2)
2992 			halbtc8821a1ant_run_coexist_mechanism(btcoexist);
2993 	}
2994 
2995 	coex_sta->special_pkt_period_cnt++;
2996 #endif
2997 }
2998