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