xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rtl8723bu/hal/btc/HalBtc8192e1Ant.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* ************************************************************
3  * Description:
4  *
5  * This file is for RTL8192E Co-exist mechanism
6  *
7  * History
8  * 2012/11/15 Cosa first check in.
9  *
10  * ************************************************************ */
11 
12 /* ************************************************************
13  * include files
14  * ************************************************************ */
15 #include "Mp_Precomp.h"
16 
17 #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1)
18 
19 #if (RTL8192E_SUPPORT == 1)
20 /* ************************************************************
21  * Global variables, these are static variables
22  * ************************************************************ */
23 static u8	 *trace_buf = &gl_btc_trace_buf[0];
24 static struct  coex_dm_8192e_1ant		glcoex_dm_8192e_1ant;
25 static struct  coex_dm_8192e_1ant	*coex_dm = &glcoex_dm_8192e_1ant;
26 static struct  coex_sta_8192e_1ant		glcoex_sta_8192e_1ant;
27 static struct  coex_sta_8192e_1ant	*coex_sta = &glcoex_sta_8192e_1ant;
28 
29 const char *const glbt_info_src_8192e_1ant[] = {
30 	"BT Info[wifi fw]",
31 	"BT Info[bt rsp]",
32 	"BT Info[bt auto report]",
33 };
34 
35 u32	glcoex_ver_date_8192e_1ant = 20140527;
36 u32	glcoex_ver_8192e_1ant = 0x4f;
37 
38 /* ************************************************************
39  * local function proto type if needed
40  * ************************************************************
41  * ************************************************************
42  * local function start with halbtc8192e1ant_
43  * ************************************************************ */
halbtc8192e1ant_bt_rssi_state(u8 level_num,u8 rssi_thresh,u8 rssi_thresh1)44 u8 halbtc8192e1ant_bt_rssi_state(u8 level_num, u8 rssi_thresh, u8 rssi_thresh1)
45 {
46 	s32			bt_rssi = 0;
47 	u8			bt_rssi_state = coex_sta->pre_bt_rssi_state;
48 
49 	bt_rssi = coex_sta->bt_rssi;
50 
51 	if (level_num == 2) {
52 		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
53 		    (coex_sta->pre_bt_rssi_state ==
54 		     BTC_RSSI_STATE_STAY_LOW)) {
55 			if (bt_rssi >= (rssi_thresh +
56 					BTC_RSSI_COEX_THRESH_TOL_8192E_1ANT))
57 				bt_rssi_state = BTC_RSSI_STATE_HIGH;
58 			else
59 				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
60 		} else {
61 			if (bt_rssi < rssi_thresh)
62 				bt_rssi_state = BTC_RSSI_STATE_LOW;
63 			else
64 				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
65 		}
66 	} else if (level_num == 3) {
67 		if (rssi_thresh > rssi_thresh1) {
68 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
69 				    "[BTCoex], BT Rssi thresh error!!\n");
70 			BTC_TRACE(trace_buf);
71 			return coex_sta->pre_bt_rssi_state;
72 		}
73 
74 		if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
75 		    (coex_sta->pre_bt_rssi_state ==
76 		     BTC_RSSI_STATE_STAY_LOW)) {
77 			if (bt_rssi >= (rssi_thresh +
78 					BTC_RSSI_COEX_THRESH_TOL_8192E_1ANT))
79 				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
80 			else
81 				bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
82 		} else if ((coex_sta->pre_bt_rssi_state ==
83 			    BTC_RSSI_STATE_MEDIUM) ||
84 			   (coex_sta->pre_bt_rssi_state ==
85 			    BTC_RSSI_STATE_STAY_MEDIUM)) {
86 			if (bt_rssi >= (rssi_thresh1 +
87 					BTC_RSSI_COEX_THRESH_TOL_8192E_1ANT))
88 				bt_rssi_state = BTC_RSSI_STATE_HIGH;
89 			else if (bt_rssi < rssi_thresh)
90 				bt_rssi_state = BTC_RSSI_STATE_LOW;
91 			else
92 				bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
93 		} else {
94 			if (bt_rssi < rssi_thresh1)
95 				bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
96 			else
97 				bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
98 		}
99 	}
100 
101 	coex_sta->pre_bt_rssi_state = bt_rssi_state;
102 
103 	return bt_rssi_state;
104 }
105 
halbtc8192e1ant_wifi_rssi_state(IN struct btc_coexist * btcoexist,IN u8 index,IN u8 level_num,IN u8 rssi_thresh,IN u8 rssi_thresh1)106 u8 halbtc8192e1ant_wifi_rssi_state(IN struct btc_coexist *btcoexist,
107 	   IN u8 index, IN u8 level_num, IN u8 rssi_thresh, IN u8 rssi_thresh1)
108 {
109 	s32			wifi_rssi = 0;
110 	u8			wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
111 
112 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
113 
114 	if (level_num == 2) {
115 		if ((coex_sta->pre_wifi_rssi_state[index] == BTC_RSSI_STATE_LOW)
116 		    ||
117 		    (coex_sta->pre_wifi_rssi_state[index] ==
118 		     BTC_RSSI_STATE_STAY_LOW)) {
119 			if (wifi_rssi >= (rssi_thresh +
120 					  BTC_RSSI_COEX_THRESH_TOL_8192E_1ANT))
121 				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
122 			else
123 				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
124 		} else {
125 			if (wifi_rssi < rssi_thresh)
126 				wifi_rssi_state = BTC_RSSI_STATE_LOW;
127 			else
128 				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
129 		}
130 	} else if (level_num == 3) {
131 		if (rssi_thresh > rssi_thresh1) {
132 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
133 				    "[BTCoex], wifi RSSI thresh error!!\n");
134 			BTC_TRACE(trace_buf);
135 			return coex_sta->pre_wifi_rssi_state[index];
136 		}
137 
138 		if ((coex_sta->pre_wifi_rssi_state[index] == BTC_RSSI_STATE_LOW)
139 		    ||
140 		    (coex_sta->pre_wifi_rssi_state[index] ==
141 		     BTC_RSSI_STATE_STAY_LOW)) {
142 			if (wifi_rssi >= (rssi_thresh +
143 					  BTC_RSSI_COEX_THRESH_TOL_8192E_1ANT))
144 				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
145 			else
146 				wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
147 		} else if ((coex_sta->pre_wifi_rssi_state[index] ==
148 			    BTC_RSSI_STATE_MEDIUM) ||
149 			   (coex_sta->pre_wifi_rssi_state[index] ==
150 			    BTC_RSSI_STATE_STAY_MEDIUM)) {
151 			if (wifi_rssi >= (rssi_thresh1 +
152 					  BTC_RSSI_COEX_THRESH_TOL_8192E_1ANT))
153 				wifi_rssi_state = BTC_RSSI_STATE_HIGH;
154 			else if (wifi_rssi < rssi_thresh)
155 				wifi_rssi_state = BTC_RSSI_STATE_LOW;
156 			else
157 				wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
158 		} else {
159 			if (wifi_rssi < rssi_thresh1)
160 				wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
161 			else
162 				wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
163 		}
164 	}
165 
166 	coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
167 
168 	return wifi_rssi_state;
169 }
170 
halbtc8192e1ant_update_ra_mask(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u32 dis_rate_mask)171 void halbtc8192e1ant_update_ra_mask(IN struct btc_coexist *btcoexist,
172 				    IN boolean force_exec, IN u32 dis_rate_mask)
173 {
174 	coex_dm->cur_ra_mask = dis_rate_mask;
175 
176 	if (force_exec || (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask))
177 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_RAMASK,
178 				   &coex_dm->cur_ra_mask);
179 	coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
180 }
181 
halbtc8192e1ant_auto_rate_fallback_retry(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 type)182 void halbtc8192e1ant_auto_rate_fallback_retry(IN struct btc_coexist *btcoexist,
183 		IN boolean force_exec, IN u8 type)
184 {
185 	boolean	wifi_under_b_mode = false;
186 
187 	coex_dm->cur_arfr_type = type;
188 
189 	if (force_exec || (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
190 		switch (coex_dm->cur_arfr_type) {
191 		case 0:	/* normal mode */
192 			btcoexist->btc_write_4byte(btcoexist, 0x430,
193 						   coex_dm->backup_arfr_cnt1);
194 			btcoexist->btc_write_4byte(btcoexist, 0x434,
195 						   coex_dm->backup_arfr_cnt2);
196 			break;
197 		case 1:
198 			btcoexist->btc_get(btcoexist,
199 					   BTC_GET_BL_WIFI_UNDER_B_MODE,
200 					   &wifi_under_b_mode);
201 			if (wifi_under_b_mode) {
202 				btcoexist->btc_write_4byte(btcoexist,
203 							   0x430, 0x0);
204 				btcoexist->btc_write_4byte(btcoexist,
205 							   0x434, 0x01010101);
206 			} else {
207 				btcoexist->btc_write_4byte(btcoexist,
208 							   0x430, 0x0);
209 				btcoexist->btc_write_4byte(btcoexist,
210 							   0x434, 0x04030201);
211 			}
212 			break;
213 		default:
214 			break;
215 		}
216 	}
217 
218 	coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
219 }
220 
halbtc8192e1ant_retry_limit(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 type)221 void halbtc8192e1ant_retry_limit(IN struct btc_coexist *btcoexist,
222 				 IN boolean force_exec, IN u8 type)
223 {
224 	coex_dm->cur_retry_limit_type = type;
225 
226 	if (force_exec ||
227 	    (coex_dm->pre_retry_limit_type !=
228 	     coex_dm->cur_retry_limit_type)) {
229 		switch (coex_dm->cur_retry_limit_type) {
230 		case 0:	/* normal mode */
231 			btcoexist->btc_write_2byte(btcoexist, 0x42a,
232 						   coex_dm->backup_retry_limit);
233 			break;
234 		case 1:	/* retry limit=8 */
235 			btcoexist->btc_write_2byte(btcoexist, 0x42a,
236 						   0x0808);
237 			break;
238 		default:
239 			break;
240 		}
241 	}
242 
243 	coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
244 }
245 
halbtc8192e1ant_ampdu_max_time(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 type)246 void halbtc8192e1ant_ampdu_max_time(IN struct btc_coexist *btcoexist,
247 				    IN boolean force_exec, IN u8 type)
248 {
249 	coex_dm->cur_ampdu_time_type = type;
250 
251 	if (force_exec ||
252 	    (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
253 		switch (coex_dm->cur_ampdu_time_type) {
254 		case 0:	/* normal mode */
255 			btcoexist->btc_write_1byte(btcoexist, 0x456,
256 					   coex_dm->backup_ampdu_max_time);
257 			break;
258 		case 1:	/* AMPDU timw = 0x38 * 32us */
259 			btcoexist->btc_write_1byte(btcoexist, 0x456,
260 						   0x38);
261 			break;
262 		default:
263 			break;
264 		}
265 	}
266 
267 	coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
268 }
269 
halbtc8192e1ant_limited_tx(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 ra_mask_type,IN u8 arfr_type,IN u8 retry_limit_type,IN u8 ampdu_time_type)270 void halbtc8192e1ant_limited_tx(IN struct btc_coexist *btcoexist,
271 		IN boolean force_exec, IN u8 ra_mask_type, IN u8 arfr_type,
272 				IN u8 retry_limit_type, IN u8 ampdu_time_type)
273 {
274 	switch (ra_mask_type) {
275 	case 0:	/* normal mode */
276 		halbtc8192e1ant_update_ra_mask(btcoexist, force_exec,
277 					       0x0);
278 		break;
279 	case 1:	/* disable cck 1/2 */
280 		halbtc8192e1ant_update_ra_mask(btcoexist, force_exec,
281 					       0x00000003);
282 		break;
283 	case 2:	/* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4 */
284 		halbtc8192e1ant_update_ra_mask(btcoexist, force_exec,
285 					       0x0001f1f7);
286 		break;
287 	default:
288 		break;
289 	}
290 
291 	halbtc8192e1ant_auto_rate_fallback_retry(btcoexist, force_exec,
292 			arfr_type);
293 	halbtc8192e1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
294 	halbtc8192e1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
295 }
296 
halbtc8192e1ant_limited_rx(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean rej_ap_agg_pkt,IN boolean bt_ctrl_agg_buf_size,IN u8 agg_buf_size)297 void halbtc8192e1ant_limited_rx(IN struct btc_coexist *btcoexist,
298 			IN boolean force_exec, IN boolean rej_ap_agg_pkt,
299 			IN boolean bt_ctrl_agg_buf_size, IN u8 agg_buf_size)
300 {
301 	boolean	reject_rx_agg = rej_ap_agg_pkt;
302 	boolean	bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
303 	u8	rx_agg_size = agg_buf_size;
304 
305 	/* ============================================ */
306 	/*	Rx Aggregation related setting */
307 	/* ============================================ */
308 	btcoexist->btc_set(btcoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT,
309 			   &reject_rx_agg);
310 	/* decide BT control aggregation buf size or not */
311 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
312 			   &bt_ctrl_rx_agg_size);
313 	/* aggregation buf size, only work when BT control Rx aggregation size. */
314 	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
315 	/* real update aggregation setting */
316 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
317 
318 
319 }
320 
halbtc8192e1ant_query_bt_info(IN struct btc_coexist * btcoexist)321 void halbtc8192e1ant_query_bt_info(IN struct btc_coexist *btcoexist)
322 {
323 	u8			h2c_parameter[1] = {0};
324 
325 	coex_sta->c2h_bt_info_req_sent = true;
326 
327 	h2c_parameter[0] |= BIT(0);	/* trigger */
328 
329 	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
330 }
331 
halbtc8192e1ant_monitor_bt_ctr(IN struct btc_coexist * btcoexist)332 void halbtc8192e1ant_monitor_bt_ctr(IN struct btc_coexist *btcoexist)
333 {
334 	u32			reg_hp_txrx, reg_lp_txrx, u32tmp;
335 	u32			reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
336 	static u8		num_of_bt_counter_chk = 0;
337 
338 	/* to avoid 0x76e[3] = 1 (WLAN_Act control by PTA) during IPS */
339 	/* if (! (btcoexist->btc_read_1byte(btcoexist, 0x76e) & 0x8) ) */
340 
341 	if (coex_sta->under_ips) {
342 		coex_sta->high_priority_tx = 65535;
343 		coex_sta->high_priority_rx = 65535;
344 		coex_sta->low_priority_tx = 65535;
345 		coex_sta->low_priority_rx = 65535;
346 		return;
347 	}
348 
349 	reg_hp_txrx = 0x770;
350 	reg_lp_txrx = 0x774;
351 
352 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
353 	reg_hp_tx = u32tmp & MASKLWORD;
354 	reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
355 
356 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
357 	reg_lp_tx = u32tmp & MASKLWORD;
358 	reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
359 
360 	coex_sta->high_priority_tx = reg_hp_tx;
361 	coex_sta->high_priority_rx = reg_hp_rx;
362 	coex_sta->low_priority_tx = reg_lp_tx;
363 	coex_sta->low_priority_rx = reg_lp_rx;
364 
365 	if ((coex_sta->low_priority_tx >= 1050)  &&
366 	    (!coex_sta->c2h_bt_inquiry_page))
367 		coex_sta->pop_event_cnt++;
368 
369 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
370 		    "[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
371 		    reg_hp_rx, reg_hp_tx, reg_lp_rx, reg_lp_tx);
372 	BTC_TRACE(trace_buf);
373 
374 	/* reset counter */
375 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
376 
377 	if ((reg_hp_tx == 0) && (reg_hp_rx == 0) && (reg_lp_tx == 0) &&
378 	    (reg_lp_rx == 0)) {
379 		num_of_bt_counter_chk++;
380 		if (num_of_bt_counter_chk >= 3) {
381 			halbtc8192e1ant_query_bt_info(btcoexist);
382 			num_of_bt_counter_chk = 0;
383 		}
384 	}
385 }
386 
387 
halbtc8192e1ant_monitor_wifi_ctr(IN struct btc_coexist * btcoexist)388 void halbtc8192e1ant_monitor_wifi_ctr(IN struct btc_coexist *btcoexist)
389 {
390 	s32	wifi_rssi = 0;
391 	boolean wifi_busy = false, wifi_under_b_mode = false;
392 	static u8 cck_lock_counter = 0;
393 
394 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
395 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
396 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
397 			   &wifi_under_b_mode);
398 
399 	if (coex_sta->under_ips) {
400 		coex_sta->crc_ok_cck = 0;
401 		coex_sta->crc_ok_11g = 0;
402 		coex_sta->crc_ok_11n = 0;
403 		coex_sta->crc_ok_11n_agg = 0;
404 
405 		coex_sta->crc_err_cck = 0;
406 		coex_sta->crc_err_11g = 0;
407 		coex_sta->crc_err_11n = 0;
408 		coex_sta->crc_err_11n_agg = 0;
409 	} else {
410 		coex_sta->crc_ok_cck	= btcoexist->btc_read_4byte(btcoexist,
411 					  0xf88);
412 		coex_sta->crc_ok_11g	= btcoexist->btc_read_2byte(btcoexist,
413 					  0xf94);
414 		coex_sta->crc_ok_11n	= btcoexist->btc_read_2byte(btcoexist,
415 					  0xf90);
416 		coex_sta->crc_ok_11n_agg = btcoexist->btc_read_2byte(btcoexist,
417 					   0xfb8);
418 
419 		coex_sta->crc_err_cck	 = btcoexist->btc_read_4byte(btcoexist,
420 					   0xf84);
421 		coex_sta->crc_err_11g	 = btcoexist->btc_read_2byte(btcoexist,
422 					   0xf96);
423 		coex_sta->crc_err_11n	 = btcoexist->btc_read_2byte(btcoexist,
424 					   0xf92);
425 		coex_sta->crc_err_11n_agg = btcoexist->btc_read_2byte(btcoexist,
426 					    0xfba);
427 	}
428 
429 
430 	/* reset counter */
431 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0xf16, 0x1, 0x1);
432 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0xf16, 0x1, 0x0);
433 
434 	if ((wifi_busy) && (wifi_rssi >= 30) && (!wifi_under_b_mode)) {
435 		if ((coex_dm->bt_status == BT_8192E_1ANT_BT_STATUS_ACL_BUSY) ||
436 		    (coex_dm->bt_status ==
437 		     BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY) ||
438 		    (coex_dm->bt_status ==
439 		     BT_8192E_1ANT_BT_STATUS_SCO_BUSY)) {
440 			if (coex_sta->crc_ok_cck > (coex_sta->crc_ok_11g +
441 						    coex_sta->crc_ok_11n +
442 						    coex_sta->crc_ok_11n_agg)) {
443 				if (cck_lock_counter < 5)
444 					cck_lock_counter++;
445 			} else {
446 				if (cck_lock_counter > 0)
447 					cck_lock_counter--;
448 			}
449 
450 		} else {
451 			if (cck_lock_counter > 0)
452 				cck_lock_counter--;
453 		}
454 	} else {
455 		if (cck_lock_counter > 0)
456 			cck_lock_counter--;
457 	}
458 
459 	if (!coex_sta->pre_ccklock) {
460 
461 		if (cck_lock_counter >= 5)
462 			coex_sta->cck_lock = true;
463 		else
464 			coex_sta->cck_lock = false;
465 	} else {
466 		if (cck_lock_counter == 0)
467 			coex_sta->cck_lock = false;
468 		else
469 			coex_sta->cck_lock = true;
470 	}
471 
472 	coex_sta->pre_ccklock =  coex_sta->cck_lock;
473 
474 
475 }
476 
halbtc8192e1ant_is_wifi_status_changed(IN struct btc_coexist * btcoexist)477 boolean halbtc8192e1ant_is_wifi_status_changed(IN struct btc_coexist *btcoexist)
478 {
479 	static boolean	pre_wifi_busy = false, pre_under_4way = false,
480 			pre_bt_hs_on = false;
481 	boolean wifi_busy = false, under_4way = false, bt_hs_on = false;
482 	boolean wifi_connected = false;
483 
484 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
485 			   &wifi_connected);
486 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
487 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
488 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
489 			   &under_4way);
490 
491 	if (wifi_connected) {
492 		if (wifi_busy != pre_wifi_busy) {
493 			pre_wifi_busy = wifi_busy;
494 			return true;
495 		}
496 		if (under_4way != pre_under_4way) {
497 			pre_under_4way = under_4way;
498 			return true;
499 		}
500 		if (bt_hs_on != pre_bt_hs_on) {
501 			pre_bt_hs_on = bt_hs_on;
502 			return true;
503 		}
504 	}
505 
506 	return false;
507 }
508 
halbtc8192e1ant_update_bt_link_info(IN struct btc_coexist * btcoexist)509 void halbtc8192e1ant_update_bt_link_info(IN struct btc_coexist *btcoexist)
510 {
511 	struct  btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
512 	boolean				bt_hs_on = false;
513 
514 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
515 
516 	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
517 	bt_link_info->sco_exist = coex_sta->sco_exist;
518 	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
519 	bt_link_info->pan_exist = coex_sta->pan_exist;
520 	bt_link_info->hid_exist = coex_sta->hid_exist;
521 
522 	/* work around for HS mode. */
523 	if (bt_hs_on) {
524 		bt_link_info->pan_exist = true;
525 		bt_link_info->bt_link_exist = true;
526 	}
527 
528 	/* check if Sco only */
529 	if (bt_link_info->sco_exist &&
530 	    !bt_link_info->a2dp_exist &&
531 	    !bt_link_info->pan_exist &&
532 	    !bt_link_info->hid_exist)
533 		bt_link_info->sco_only = true;
534 	else
535 		bt_link_info->sco_only = false;
536 
537 	/* check if A2dp only */
538 	if (!bt_link_info->sco_exist &&
539 	    bt_link_info->a2dp_exist &&
540 	    !bt_link_info->pan_exist &&
541 	    !bt_link_info->hid_exist)
542 		bt_link_info->a2dp_only = true;
543 	else
544 		bt_link_info->a2dp_only = false;
545 
546 	/* check if Pan only */
547 	if (!bt_link_info->sco_exist &&
548 	    !bt_link_info->a2dp_exist &&
549 	    bt_link_info->pan_exist &&
550 	    !bt_link_info->hid_exist)
551 		bt_link_info->pan_only = true;
552 	else
553 		bt_link_info->pan_only = false;
554 
555 	/* check if Hid only */
556 	if (!bt_link_info->sco_exist &&
557 	    !bt_link_info->a2dp_exist &&
558 	    !bt_link_info->pan_exist &&
559 	    bt_link_info->hid_exist)
560 		bt_link_info->hid_only = true;
561 	else
562 		bt_link_info->hid_only = false;
563 }
564 
halbtc8192e1ant_action_algorithm(IN struct btc_coexist * btcoexist)565 u8 halbtc8192e1ant_action_algorithm(IN struct btc_coexist *btcoexist)
566 {
567 	struct  btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
568 	boolean				bt_hs_on = false;
569 	u8				algorithm = BT_8192E_1ANT_COEX_ALGO_UNDEFINED;
570 	u8				num_of_diff_profile = 0;
571 
572 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
573 
574 	if (!bt_link_info->bt_link_exist) {
575 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
576 			    "[BTCoex], No BT link exists!!!\n");
577 		BTC_TRACE(trace_buf);
578 		return algorithm;
579 	}
580 
581 	if (bt_link_info->sco_exist)
582 		num_of_diff_profile++;
583 	if (bt_link_info->hid_exist)
584 		num_of_diff_profile++;
585 	if (bt_link_info->pan_exist)
586 		num_of_diff_profile++;
587 	if (bt_link_info->a2dp_exist)
588 		num_of_diff_profile++;
589 
590 	if (num_of_diff_profile == 1) {
591 		if (bt_link_info->sco_exist) {
592 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
593 				    "[BTCoex], BT Profile = SCO only\n");
594 			BTC_TRACE(trace_buf);
595 			algorithm = BT_8192E_1ANT_COEX_ALGO_SCO;
596 		} else {
597 			if (bt_link_info->hid_exist) {
598 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
599 					"[BTCoex], BT Profile = HID only\n");
600 				BTC_TRACE(trace_buf);
601 				algorithm = BT_8192E_1ANT_COEX_ALGO_HID;
602 			} else if (bt_link_info->a2dp_exist) {
603 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
604 					"[BTCoex], BT Profile = A2DP only\n");
605 				BTC_TRACE(trace_buf);
606 				algorithm = BT_8192E_1ANT_COEX_ALGO_A2DP;
607 			} else if (bt_link_info->pan_exist) {
608 				if (bt_hs_on) {
609 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
610 						"[BTCoex], BT Profile = PAN(HS) only\n");
611 					BTC_TRACE(trace_buf);
612 					algorithm =
613 						BT_8192E_1ANT_COEX_ALGO_PANHS;
614 				} else {
615 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
616 						"[BTCoex], BT Profile = PAN(EDR) only\n");
617 					BTC_TRACE(trace_buf);
618 					algorithm =
619 						BT_8192E_1ANT_COEX_ALGO_PANEDR;
620 				}
621 			}
622 		}
623 	} else if (num_of_diff_profile == 2) {
624 		if (bt_link_info->sco_exist) {
625 			if (bt_link_info->hid_exist) {
626 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
627 					"[BTCoex], BT Profile = SCO + HID\n");
628 				BTC_TRACE(trace_buf);
629 				algorithm = BT_8192E_1ANT_COEX_ALGO_HID;
630 			} else if (bt_link_info->a2dp_exist) {
631 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
632 					"[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
633 				BTC_TRACE(trace_buf);
634 				algorithm = BT_8192E_1ANT_COEX_ALGO_SCO;
635 			} else if (bt_link_info->pan_exist) {
636 				if (bt_hs_on) {
637 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
638 						"[BTCoex], BT Profile = SCO + PAN(HS)\n");
639 					BTC_TRACE(trace_buf);
640 					algorithm = BT_8192E_1ANT_COEX_ALGO_SCO;
641 				} else {
642 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
643 						"[BTCoex], BT Profile = SCO + PAN(EDR)\n");
644 					BTC_TRACE(trace_buf);
645 					algorithm =
646 						BT_8192E_1ANT_COEX_ALGO_PANEDR_HID;
647 				}
648 			}
649 		} else {
650 			if (bt_link_info->hid_exist &&
651 			    bt_link_info->a2dp_exist) {
652 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
653 					"[BTCoex], BT Profile = HID + A2DP\n");
654 				BTC_TRACE(trace_buf);
655 				algorithm = BT_8192E_1ANT_COEX_ALGO_HID_A2DP;
656 			} else if (bt_link_info->hid_exist &&
657 				   bt_link_info->pan_exist) {
658 				if (bt_hs_on) {
659 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
660 						"[BTCoex], BT Profile = HID + PAN(HS)\n");
661 					BTC_TRACE(trace_buf);
662 					algorithm =
663 						BT_8192E_1ANT_COEX_ALGO_HID_A2DP;
664 				} else {
665 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
666 						"[BTCoex], BT Profile = HID + PAN(EDR)\n");
667 					BTC_TRACE(trace_buf);
668 					algorithm =
669 						BT_8192E_1ANT_COEX_ALGO_PANEDR_HID;
670 				}
671 			} else if (bt_link_info->pan_exist &&
672 				   bt_link_info->a2dp_exist) {
673 				if (bt_hs_on) {
674 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
675 						"[BTCoex], BT Profile = A2DP + PAN(HS)\n");
676 					BTC_TRACE(trace_buf);
677 					algorithm =
678 						BT_8192E_1ANT_COEX_ALGO_A2DP_PANHS;
679 				} else {
680 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
681 						"[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
682 					BTC_TRACE(trace_buf);
683 					algorithm =
684 						BT_8192E_1ANT_COEX_ALGO_PANEDR_A2DP;
685 				}
686 			}
687 		}
688 	} else if (num_of_diff_profile == 3) {
689 		if (bt_link_info->sco_exist) {
690 			if (bt_link_info->hid_exist &&
691 			    bt_link_info->a2dp_exist) {
692 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
693 					"[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
694 				BTC_TRACE(trace_buf);
695 				algorithm = BT_8192E_1ANT_COEX_ALGO_HID;
696 			} else if (bt_link_info->hid_exist &&
697 				   bt_link_info->pan_exist) {
698 				if (bt_hs_on) {
699 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
700 						"[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
701 					BTC_TRACE(trace_buf);
702 					algorithm =
703 						BT_8192E_1ANT_COEX_ALGO_HID_A2DP;
704 				} else {
705 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
706 						"[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
707 					BTC_TRACE(trace_buf);
708 					algorithm =
709 						BT_8192E_1ANT_COEX_ALGO_PANEDR_HID;
710 				}
711 			} else if (bt_link_info->pan_exist &&
712 				   bt_link_info->a2dp_exist) {
713 				if (bt_hs_on) {
714 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
715 						"[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
716 					BTC_TRACE(trace_buf);
717 					algorithm = BT_8192E_1ANT_COEX_ALGO_SCO;
718 				} else {
719 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
720 						"[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
721 					BTC_TRACE(trace_buf);
722 					algorithm =
723 						BT_8192E_1ANT_COEX_ALGO_PANEDR_HID;
724 				}
725 			}
726 		} else {
727 			if (bt_link_info->hid_exist &&
728 			    bt_link_info->pan_exist &&
729 			    bt_link_info->a2dp_exist) {
730 				if (bt_hs_on) {
731 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
732 						"[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
733 					BTC_TRACE(trace_buf);
734 					algorithm =
735 						BT_8192E_1ANT_COEX_ALGO_HID_A2DP;
736 				} else {
737 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
738 						"[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
739 					BTC_TRACE(trace_buf);
740 					algorithm =
741 						BT_8192E_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
742 				}
743 			}
744 		}
745 	} else if (num_of_diff_profile >= 3) {
746 		if (bt_link_info->sco_exist) {
747 			if (bt_link_info->hid_exist &&
748 			    bt_link_info->pan_exist &&
749 			    bt_link_info->a2dp_exist) {
750 				if (bt_hs_on) {
751 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
752 						"[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
753 					BTC_TRACE(trace_buf);
754 
755 				} else {
756 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
757 						"[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
758 					BTC_TRACE(trace_buf);
759 					algorithm =
760 						BT_8192E_1ANT_COEX_ALGO_PANEDR_HID;
761 				}
762 			}
763 		}
764 	}
765 
766 	return algorithm;
767 }
768 
halbtc8192e1ant_set_bt_auto_report(IN struct btc_coexist * btcoexist,IN boolean enable_auto_report)769 void halbtc8192e1ant_set_bt_auto_report(IN struct btc_coexist *btcoexist,
770 					IN boolean enable_auto_report)
771 {
772 	u8			h2c_parameter[1] = {0};
773 
774 	h2c_parameter[0] = 0;
775 
776 	if (enable_auto_report)
777 		h2c_parameter[0] |= BIT(0);
778 
779 	btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
780 }
781 
halbtc8192e1ant_bt_auto_report(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean enable_auto_report)782 void halbtc8192e1ant_bt_auto_report(IN struct btc_coexist *btcoexist,
783 		    IN boolean force_exec, IN boolean enable_auto_report)
784 {
785 	coex_dm->cur_bt_auto_report = enable_auto_report;
786 
787 	if (!force_exec) {
788 		if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
789 			return;
790 	}
791 	halbtc8192e1ant_set_bt_auto_report(btcoexist,
792 					   coex_dm->cur_bt_auto_report);
793 
794 	coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
795 }
796 
halbtc8192e1ant_set_sw_penalty_tx_rate_adaptive(IN struct btc_coexist * btcoexist,IN boolean low_penalty_ra)797 void halbtc8192e1ant_set_sw_penalty_tx_rate_adaptive(IN struct btc_coexist
798 		*btcoexist, IN boolean low_penalty_ra)
799 {
800 	u8			h2c_parameter[6] = {0};
801 
802 	h2c_parameter[0] = 0x6;	/* op_code, 0x6= Retry_Penalty */
803 
804 	if (low_penalty_ra) {
805 		h2c_parameter[1] |= BIT(0);
806 		h2c_parameter[2] =
807 			0x00;  /* normal rate except MCS7/6/5, OFDM54/48/36 */
808 		h2c_parameter[3] = 0xf7;  /* MCS7 or OFDM54 */
809 		h2c_parameter[4] = 0xf8;  /* MCS6 or OFDM48 */
810 		h2c_parameter[5] = 0xf9;	/* MCS5 or OFDM36	 */
811 	}
812 
813 	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
814 }
815 
halbtc8192e1ant_low_penalty_ra(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean low_penalty_ra)816 void halbtc8192e1ant_low_penalty_ra(IN struct btc_coexist *btcoexist,
817 			    IN boolean force_exec, IN boolean low_penalty_ra)
818 {
819 	coex_dm->cur_low_penalty_ra = low_penalty_ra;
820 
821 	if (!force_exec) {
822 		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
823 			return;
824 	}
825 	halbtc8192e1ant_set_sw_penalty_tx_rate_adaptive(btcoexist,
826 			coex_dm->cur_low_penalty_ra);
827 
828 	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
829 }
830 
halbtc8192e1ant_set_coex_table(IN struct btc_coexist * btcoexist,IN u32 val0x6c0,IN u32 val0x6c4,IN u32 val0x6c8,IN u8 val0x6cc)831 void halbtc8192e1ant_set_coex_table(IN struct btc_coexist *btcoexist,
832 	    IN u32 val0x6c0, IN u32 val0x6c4, IN u32 val0x6c8, IN u8 val0x6cc)
833 {
834 	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
835 
836 	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
837 
838 	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
839 
840 	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
841 }
842 
halbtc8192e1ant_coex_table(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u32 val0x6c0,IN u32 val0x6c4,IN u32 val0x6c8,IN u8 val0x6cc)843 void halbtc8192e1ant_coex_table(IN struct btc_coexist *btcoexist,
844 			IN boolean force_exec, IN u32 val0x6c0, IN u32 val0x6c4,
845 				IN u32 val0x6c8, IN u8 val0x6cc)
846 {
847 	coex_dm->cur_val0x6c0 = val0x6c0;
848 	coex_dm->cur_val0x6c4 = val0x6c4;
849 	coex_dm->cur_val0x6c8 = val0x6c8;
850 	coex_dm->cur_val0x6cc = val0x6cc;
851 
852 	if (!force_exec) {
853 		if ((coex_dm->pre_val0x6c0 == coex_dm->cur_val0x6c0) &&
854 		    (coex_dm->pre_val0x6c4 == coex_dm->cur_val0x6c4) &&
855 		    (coex_dm->pre_val0x6c8 == coex_dm->cur_val0x6c8) &&
856 		    (coex_dm->pre_val0x6cc == coex_dm->cur_val0x6cc))
857 			return;
858 	}
859 	halbtc8192e1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4, val0x6c8,
860 				       val0x6cc);
861 
862 	coex_dm->pre_val0x6c0 = coex_dm->cur_val0x6c0;
863 	coex_dm->pre_val0x6c4 = coex_dm->cur_val0x6c4;
864 	coex_dm->pre_val0x6c8 = coex_dm->cur_val0x6c8;
865 	coex_dm->pre_val0x6cc = coex_dm->cur_val0x6cc;
866 }
867 
halbtc8192e1ant_coex_table_with_type(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 type)868 void halbtc8192e1ant_coex_table_with_type(IN struct btc_coexist *btcoexist,
869 		IN boolean force_exec, IN u8 type)
870 {
871 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
872 		    "[BTCoex], ********** CoexTable(%d) **********\n", type);
873 	BTC_TRACE(trace_buf);
874 
875 	coex_sta->coex_table_type = type;
876 
877 	switch (type) {
878 	case 0:
879 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
880 				   0x55555555, 0x55555555, 0xffffff, 0x3);
881 		break;
882 	case 1:
883 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
884 				   0x55555555, 0x5a5a5a5a, 0xffffff, 0x3);
885 		break;
886 	case 2:
887 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
888 				   0x5a5a5a5a, 0x5a5a5a5a, 0xffffff, 0x3);
889 		break;
890 	case 3:
891 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
892 				   0x55555555, 0x5a5a5a5a, 0xffffff, 0x3);
893 		break;
894 	case 4:
895 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
896 				   0x55555555, 0xaaaa5a5a, 0xffffff, 0x3);
897 		break;
898 	case 5:
899 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
900 				   0x5a5a5a5a, 0xaa5a5a5a, 0xffffff, 0x3);
901 		break;
902 	case 6:
903 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
904 				   0x55555555, 0xaaaaaaaa, 0xffffff, 0x3);
905 		break;
906 	case 7:
907 		halbtc8192e1ant_coex_table(btcoexist, force_exec,
908 				   0xaaaaaaaa, 0xaaaaaaaa, 0xffffff, 0x3);
909 		break;
910 	default:
911 		break;
912 	}
913 }
914 
halbtc8192e1ant_set_fw_ignore_wlan_act(IN struct btc_coexist * btcoexist,IN boolean enable)915 void halbtc8192e1ant_set_fw_ignore_wlan_act(IN struct btc_coexist *btcoexist,
916 		IN boolean enable)
917 {
918 	u8			h2c_parameter[1] = {0};
919 
920 	if (enable) {
921 		h2c_parameter[0] |= BIT(0);		/* function enable */
922 	}
923 
924 	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
925 }
926 
halbtc8192e1ant_ignore_wlan_act(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean enable)927 void halbtc8192e1ant_ignore_wlan_act(IN struct btc_coexist *btcoexist,
928 				     IN boolean force_exec, IN boolean enable)
929 {
930 	coex_dm->cur_ignore_wlan_act = enable;
931 
932 	if (!force_exec) {
933 		if (coex_dm->pre_ignore_wlan_act ==
934 		    coex_dm->cur_ignore_wlan_act)
935 			return;
936 	}
937 	halbtc8192e1ant_set_fw_ignore_wlan_act(btcoexist, enable);
938 
939 	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
940 }
941 
halbtc8192e1ant_set_lps_rpwm(IN struct btc_coexist * btcoexist,IN u8 lps_val,IN u8 rpwm_val)942 void halbtc8192e1ant_set_lps_rpwm(IN struct btc_coexist *btcoexist,
943 				  IN u8 lps_val, IN u8 rpwm_val)
944 {
945 	u8	lps = lps_val;
946 	u8	rpwm = rpwm_val;
947 
948 	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
949 	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
950 }
951 
halbtc8192e1ant_lps_rpwm(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 lps_val,IN u8 rpwm_val)952 void halbtc8192e1ant_lps_rpwm(IN struct btc_coexist *btcoexist,
953 		      IN boolean force_exec, IN u8 lps_val, IN u8 rpwm_val)
954 {
955 	coex_dm->cur_lps = lps_val;
956 	coex_dm->cur_rpwm = rpwm_val;
957 
958 	if (!force_exec) {
959 		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
960 		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm))
961 			return;
962 	}
963 	halbtc8192e1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
964 
965 	coex_dm->pre_lps = coex_dm->cur_lps;
966 	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
967 }
968 
halbtc8192e1ant_sw_mechanism(IN struct btc_coexist * btcoexist,IN boolean low_penalty_ra)969 void halbtc8192e1ant_sw_mechanism(IN struct btc_coexist *btcoexist,
970 				  IN boolean low_penalty_ra)
971 {
972 	halbtc8192e1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
973 }
974 
halbtc8192e1ant_set_ant_path(IN struct btc_coexist * btcoexist,IN u8 ant_pos_type,IN boolean init_hwcfg,IN boolean wifi_off)975 void halbtc8192e1ant_set_ant_path(IN struct btc_coexist *btcoexist,
976 	  IN u8 ant_pos_type, IN boolean init_hwcfg, IN boolean wifi_off)
977 {
978 	u32			u32tmp = 0;
979 
980 	if (init_hwcfg) {
981 		btcoexist->btc_write_1byte(btcoexist, 0x944, 0x24);
982 		btcoexist->btc_write_4byte(btcoexist, 0x930, 0x700700);
983 		if (btcoexist->chip_interface == BTC_INTF_USB)
984 			btcoexist->btc_write_4byte(btcoexist, 0x64, 0x30430004);
985 		else
986 			btcoexist->btc_write_4byte(btcoexist, 0x64, 0x30030004);
987 
988 		/* 0x4c[27][24]='00', Set Antenna to BB */
989 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
990 		u32tmp &= ~BIT(24);
991 		u32tmp &= ~BIT(27);
992 		btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
993 	} else if (wifi_off) {
994 		if (btcoexist->chip_interface == BTC_INTF_USB)
995 			btcoexist->btc_write_4byte(btcoexist, 0x64, 0x30430004);
996 		else
997 			btcoexist->btc_write_4byte(btcoexist, 0x64, 0x30030004);
998 
999 		/* 0x4c[27][24]='11', Set Antenna to BT, 0x64[8:7]=0, 0x64[2]=1 */
1000 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
1001 		u32tmp |= BIT(24);
1002 		u32tmp |= BIT(27);
1003 		btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
1004 	}
1005 
1006 	/* ext switch setting */
1007 	switch (ant_pos_type) {
1008 	case BTC_ANT_PATH_WIFI:
1009 		btcoexist->btc_write_1byte(btcoexist, 0x92c, 0x4);
1010 		break;
1011 	case BTC_ANT_PATH_BT:
1012 		btcoexist->btc_write_1byte(btcoexist, 0x92c, 0x20);
1013 		break;
1014 	default:
1015 	case BTC_ANT_PATH_PTA:
1016 		btcoexist->btc_write_1byte(btcoexist, 0x92c, 0x4);
1017 		break;
1018 	}
1019 }
1020 
halbtc8192e1ant_set_fw_pstdma(IN struct btc_coexist * btcoexist,IN u8 byte1,IN u8 byte2,IN u8 byte3,IN u8 byte4,IN u8 byte5)1021 void halbtc8192e1ant_set_fw_pstdma(IN struct btc_coexist *btcoexist,
1022 	   IN u8 byte1, IN u8 byte2, IN u8 byte3, IN u8 byte4, IN u8 byte5)
1023 {
1024 	u8			h2c_parameter[5] = {0};
1025 	u8			real_byte1 = byte1, real_byte5 = byte5;
1026 	boolean			ap_enable = false;
1027 
1028 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1029 			   &ap_enable);
1030 
1031 	if (ap_enable) {
1032 		if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
1033 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1034 				    "[BTCoex], FW for 1Ant AP mode\n");
1035 			BTC_TRACE(trace_buf);
1036 			real_byte1 &= ~BIT(4);
1037 			real_byte1 |= BIT(5);
1038 
1039 			real_byte5 |= BIT(5);
1040 			real_byte5 &= ~BIT(6);
1041 		}
1042 	}
1043 
1044 	h2c_parameter[0] = real_byte1;
1045 	h2c_parameter[1] = byte2;
1046 	h2c_parameter[2] = byte3;
1047 	h2c_parameter[3] = byte4;
1048 	h2c_parameter[4] = real_byte5;
1049 
1050 	coex_dm->ps_tdma_para[0] = real_byte1;
1051 	coex_dm->ps_tdma_para[1] = byte2;
1052 	coex_dm->ps_tdma_para[2] = byte3;
1053 	coex_dm->ps_tdma_para[3] = byte4;
1054 	coex_dm->ps_tdma_para[4] = real_byte5;
1055 
1056 	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
1057 }
1058 
1059 
halbtc8192e1ant_ps_tdma(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean turn_on,IN u8 type)1060 void halbtc8192e1ant_ps_tdma(IN struct btc_coexist *btcoexist,
1061 		     IN boolean force_exec, IN boolean turn_on, IN u8 type)
1062 {
1063 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1064 	boolean			wifi_busy = false;
1065 	u8			rssi_adjust_val = 0;
1066 	u8			ps_tdma_byte4_val = 0x50, ps_tdma_byte0_val = 0x51,
1067 				ps_tdma_byte3_val =  0x10;
1068 	s8			wifi_duration_adjust = 0x0;
1069 
1070 	coex_dm->cur_ps_tdma_on = turn_on;
1071 	coex_dm->cur_ps_tdma = type;
1072 
1073 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1074 
1075 	if (coex_dm->cur_ps_tdma_on) {
1076 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1077 			    "[BTCoex], ********** TDMA(on, %d) **********\n",
1078 			    coex_dm->cur_ps_tdma);
1079 		BTC_TRACE(trace_buf);
1080 	} else {
1081 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1082 			    "[BTCoex], ********** TDMA(off, %d) **********\n",
1083 			    coex_dm->cur_ps_tdma);
1084 		BTC_TRACE(trace_buf);
1085 	}
1086 
1087 	if (!force_exec) {
1088 		if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1089 		    (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1090 			return;
1091 	}
1092 
1093 	if (coex_sta->scan_ap_num <= 5)
1094 		wifi_duration_adjust = 5;
1095 	else if (coex_sta->scan_ap_num >= 40)
1096 		wifi_duration_adjust = -15;
1097 	else if (coex_sta->scan_ap_num >= 20)
1098 		wifi_duration_adjust = -10;
1099 
1100 	if (!coex_sta->force_lps_on) { /* only for A2DP-only case 1/2/9/11 while wifi noisy threshold > 30 */
1101 		ps_tdma_byte0_val = 0x61;  /* no null-pkt */
1102 		ps_tdma_byte3_val = 0x11; /* no tx-pause at BT-slot */
1103 		ps_tdma_byte4_val = 0x10; /* 0x778 = d/1 toggle */
1104 	}
1105 
1106 	if ((type == 3) || (type == 13) || (type == 14))
1107 		ps_tdma_byte4_val = ps_tdma_byte4_val &
1108 			    0xbf;  /* no dynamic slot for multi-profile */
1109 
1110 	if (bt_link_info->slave_role == true)
1111 		ps_tdma_byte4_val = ps_tdma_byte4_val |
1112 			0x1;  /* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1113 
1114 	if (turn_on) {
1115 		switch (type) {
1116 		default:
1117 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x51,
1118 				      0x1a, 0x1a, 0x0, ps_tdma_byte4_val);
1119 			break;
1120 		case 1:
1121 			halbtc8192e1ant_set_fw_pstdma(btcoexist,
1122 						      ps_tdma_byte0_val, 0x3a +
1123 					      wifi_duration_adjust, 0x03,
1124 				      ps_tdma_byte3_val, ps_tdma_byte4_val);
1125 			break;
1126 		case 2:
1127 			halbtc8192e1ant_set_fw_pstdma(btcoexist,
1128 						      ps_tdma_byte0_val, 0x2d +
1129 					      wifi_duration_adjust, 0x03,
1130 				      ps_tdma_byte3_val, ps_tdma_byte4_val);
1131 			break;
1132 		case 3:
1133 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x51,
1134 				      0x1d, 0x1d, 0x0, ps_tdma_byte4_val);
1135 			break;
1136 		case 4:
1137 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x93,
1138 						      0x15, 0x3, 0x14, 0x0);
1139 			break;
1140 		case 5:
1141 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x61,
1142 						      0x15, 0x3, 0x11, 0x11);
1143 			break;
1144 		case 6:
1145 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x61,
1146 						      0x20, 0x3, 0x11, 0x11);
1147 			break;
1148 		case 7:
1149 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x13,
1150 						      0xc, 0x5, 0x0, 0x0);
1151 			break;
1152 		case 8:
1153 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x93,
1154 						      0x25, 0x3, 0x10, 0x0);
1155 			break;
1156 		case 9:
1157 			halbtc8192e1ant_set_fw_pstdma(btcoexist,
1158 					      ps_tdma_byte0_val, 0x21, 0x3,
1159 				      ps_tdma_byte3_val, ps_tdma_byte4_val);
1160 			break;
1161 		case 10:
1162 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x13,
1163 						      0xa, 0xa, 0x0, 0x40);
1164 			break;
1165 		case 11:
1166 			halbtc8192e1ant_set_fw_pstdma(btcoexist,
1167 					      ps_tdma_byte0_val, 0x21, 0x03,
1168 				      ps_tdma_byte3_val, ps_tdma_byte4_val);
1169 			break;
1170 		case 12:
1171 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x51,
1172 						      0x0a, 0x0a, 0x0, 0x50);
1173 			break;
1174 		case 13:
1175 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x51,
1176 				      0x12, 0x12, 0x0, ps_tdma_byte4_val);
1177 			break;
1178 		case 14:
1179 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x51,
1180 				      0x21, 0x3, 0x10, ps_tdma_byte4_val);
1181 			break;
1182 		case 15:
1183 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x13,
1184 						      0xa, 0x3, 0x8, 0x0);
1185 			break;
1186 		case 16:
1187 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x93,
1188 						      0x15, 0x3, 0x10, 0x0);
1189 			break;
1190 		case 18:
1191 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x93,
1192 						      0x25, 0x3, 0x10, 0x0);
1193 			break;
1194 		case 20:
1195 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x61,
1196 						      0x3f, 0x03, 0x11, 0x10);
1197 			break;
1198 		case 21:
1199 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x61,
1200 						      0x25, 0x03, 0x11, 0x11);
1201 			break;
1202 		case 22:
1203 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x61,
1204 						      0x25, 0x03, 0x11, 0x10);
1205 			break;
1206 		case 23:
1207 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xe3,
1208 						      0x25, 0x3, 0x31, 0x18);
1209 			break;
1210 		case 24:
1211 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xe3,
1212 						      0x15, 0x3, 0x31, 0x18);
1213 			break;
1214 		case 25:
1215 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xe3,
1216 						      0xa, 0x3, 0x31, 0x18);
1217 			break;
1218 		case 26:
1219 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xe3,
1220 						      0xa, 0x3, 0x31, 0x18);
1221 			break;
1222 		case 27:
1223 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xe3,
1224 						      0x25, 0x3, 0x31, 0x98);
1225 			break;
1226 		case 28:
1227 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x69,
1228 						      0x25, 0x3, 0x31, 0x0);
1229 			break;
1230 		case 29:
1231 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xab,
1232 						      0x1a, 0x1a, 0x1, 0x10);
1233 			break;
1234 		case 30:
1235 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x51,
1236 						      0x30, 0x3, 0x10, 0x10);
1237 			break;
1238 		case 31:
1239 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xd3,
1240 						      0x1a, 0x1a, 0, 0x58);
1241 			break;
1242 		case 32:
1243 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x61,
1244 						      0x35, 0x3, 0x11, 0x11);
1245 			break;
1246 		case 33:
1247 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xa3,
1248 						      0x25, 0x3, 0x30, 0x90);
1249 			break;
1250 		case 34:
1251 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x53,
1252 						      0x1a, 0x1a, 0x0, 0x10);
1253 			break;
1254 		case 35:
1255 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x63,
1256 						      0x1a, 0x1a, 0x0, 0x10);
1257 			break;
1258 		case 36:
1259 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0xd3,
1260 						      0x12, 0x3, 0x14, 0x50);
1261 			break;
1262 		case 40: /* SoftAP only with no sta associated,BT disable ,TDMA mode for power saving */
1263 			/* here softap mode screen off will cost 70-80mA for phone */
1264 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x23,
1265 						      0x18, 0x00, 0x10, 0x24);
1266 			break;
1267 		}
1268 	} else {
1269 
1270 		/* disable PS tdma */
1271 		switch (type) {
1272 		case 8: /* PTA Control */
1273 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x8,
1274 						      0x0, 0x0, 0x0, 0x0);
1275 			halbtc8192e1ant_set_ant_path(btcoexist,
1276 					     BTC_ANT_PATH_PTA, false, false);
1277 			break;
1278 		case 0:
1279 		default:  /* Software control, Antenna at BT side */
1280 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x0,
1281 						      0x0, 0x0, 0x0, 0x0);
1282 			halbtc8192e1ant_set_ant_path(btcoexist,
1283 					     BTC_ANT_PATH_BT, false, false);
1284 			break;
1285 		case 9:   /* Software control, Antenna at WiFi side */
1286 			halbtc8192e1ant_set_fw_pstdma(btcoexist, 0x0,
1287 						      0x0, 0x0, 0x0, 0x0);
1288 			halbtc8192e1ant_set_ant_path(btcoexist,
1289 					     BTC_ANT_PATH_WIFI, false, false);
1290 			break;
1291 		}
1292 	}
1293 	rssi_adjust_val = 0;
1294 	btcoexist->btc_set(btcoexist,
1295 		BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssi_adjust_val);
1296 
1297 
1298 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1299 		"############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
1300 		    btcoexist->btc_read_4byte(btcoexist, 0x948),
1301 		    btcoexist->btc_read_1byte(btcoexist, 0x765),
1302 		    btcoexist->btc_read_1byte(btcoexist, 0x67));
1303 	BTC_TRACE(trace_buf);
1304 	/* update pre state */
1305 	coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1306 	coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1307 }
1308 
halbtc8192e1ant_coex_all_off(IN struct btc_coexist * btcoexist)1309 void halbtc8192e1ant_coex_all_off(IN struct btc_coexist *btcoexist)
1310 {
1311 	/* sw all off */
1312 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
1313 
1314 	/* hw all off */
1315 	halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1316 }
1317 
halbtc8192e1ant_is_common_action(IN struct btc_coexist * btcoexist)1318 boolean halbtc8192e1ant_is_common_action(IN struct btc_coexist *btcoexist)
1319 {
1320 	boolean			common = false, wifi_connected = false, wifi_busy = false;
1321 
1322 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1323 			   &wifi_connected);
1324 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1325 
1326 	if (!wifi_connected &&
1327 	    BT_8192E_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1328 	    coex_dm->bt_status) {
1329 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1330 			"[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
1331 		BTC_TRACE(trace_buf);
1332 		/* halbtc8192e1ant_sw_mechanism(btcoexist, false); */
1333 
1334 		common = true;
1335 	} else if (wifi_connected &&
1336 		   (BT_8192E_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1337 		    coex_dm->bt_status)) {
1338 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1339 			"[BTCoex], Wifi connected + BT non connected-idle!!\n");
1340 		BTC_TRACE(trace_buf);
1341 		/* halbtc8192e1ant_sw_mechanism(btcoexist, false); */
1342 
1343 		common = true;
1344 	} else if (!wifi_connected &&
1345 		(BT_8192E_1ANT_BT_STATUS_CONNECTED_IDLE == coex_dm->bt_status)) {
1346 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1347 			"[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
1348 		BTC_TRACE(trace_buf);
1349 		/* halbtc8192e1ant_sw_mechanism(btcoexist, false); */
1350 
1351 		common = true;
1352 	} else if (wifi_connected &&
1353 		(BT_8192E_1ANT_BT_STATUS_CONNECTED_IDLE == coex_dm->bt_status)) {
1354 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1355 			    "[BTCoex], Wifi connected + BT connected-idle!!\n");
1356 		BTC_TRACE(trace_buf);
1357 		/* halbtc8192e1ant_sw_mechanism(btcoexist, false); */
1358 
1359 		common = true;
1360 	} else if (!wifi_connected &&
1361 		(BT_8192E_1ANT_BT_STATUS_CONNECTED_IDLE != coex_dm->bt_status)) {
1362 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1363 			    "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
1364 		BTC_TRACE(trace_buf);
1365 		/* halbtc8192e1ant_sw_mechanism(btcoexist, false); */
1366 
1367 		common = true;
1368 	} else {
1369 		if (wifi_busy) {
1370 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1371 				"[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1372 			BTC_TRACE(trace_buf);
1373 		} else {
1374 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1375 				"[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1376 			BTC_TRACE(trace_buf);
1377 		}
1378 
1379 		common = false;
1380 	}
1381 
1382 	return common;
1383 }
1384 
1385 
halbtc8192e1ant_tdma_duration_adjust_for_acl(IN struct btc_coexist * btcoexist,IN u8 wifi_status)1386 void halbtc8192e1ant_tdma_duration_adjust_for_acl(IN struct btc_coexist
1387 		*btcoexist, IN u8 wifi_status)
1388 {
1389 	static s32		up, dn, m, n, wait_count;
1390 	s32			result;   /* 0: no change, +1: increase WiFi duration, -1: decrease WiFi duration */
1391 	u8			retry_count = 0, bt_info_ext;
1392 	boolean			wifi_busy = false;
1393 	/*static boolean	pre_wifi_busy = false;*/
1394 
1395 	if (BT_8192E_1ANT_WIFI_STATUS_CONNECTED_BUSY == wifi_status)
1396 		wifi_busy = true;
1397 	else
1398 		wifi_busy = false;
1399 
1400 	if ((BT_8192E_1ANT_WIFI_STATUS_NON_CONNECTED_ASSO_AUTH_SCAN ==
1401 	     wifi_status) ||
1402 	    (BT_8192E_1ANT_WIFI_STATUS_CONNECTED_SCAN == wifi_status) ||
1403 	    (BT_8192E_1ANT_WIFI_STATUS_CONNECTED_SPECIFIC_PKT ==
1404 	     wifi_status)) {
1405 		if (coex_dm->cur_ps_tdma != 1 &&
1406 		    coex_dm->cur_ps_tdma != 2 &&
1407 		    coex_dm->cur_ps_tdma != 3 &&
1408 		    coex_dm->cur_ps_tdma != 9) {
1409 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1410 						9);
1411 			coex_dm->ps_tdma_du_adj_type = 9;
1412 
1413 			up = 0;
1414 			dn = 0;
1415 			m = 1;
1416 			n = 3;
1417 			result = 0;
1418 			wait_count = 0;
1419 		}
1420 		return;
1421 	}
1422 
1423 	if (!coex_dm->auto_tdma_adjust) {
1424 		coex_dm->auto_tdma_adjust = true;
1425 
1426 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 2);
1427 		coex_dm->ps_tdma_du_adj_type = 2;
1428 		/* ============ */
1429 		up = 0;
1430 		dn = 0;
1431 		m = 1;
1432 		n = 3;
1433 		result = 0;
1434 		wait_count = 0;
1435 	} else {
1436 		/* accquire the BT TRx retry count from BT_Info byte2 */
1437 		retry_count = coex_sta->bt_retry_cnt;
1438 		bt_info_ext = coex_sta->bt_info_ext;
1439 
1440 		if ((coex_sta->low_priority_tx) > 1050 ||
1441 		    (coex_sta->low_priority_rx) > 1250)
1442 			retry_count++;
1443 
1444 		result = 0;
1445 		wait_count++;
1446 
1447 		if (retry_count ==
1448 		    0) { /* no retry in the last 2-second duration */
1449 			up++;
1450 			dn--;
1451 
1452 			if (dn <= 0)
1453 				dn = 0;
1454 
1455 			if (up >= n) {	/* if retry count during continuous n*2 seconds is 0, enlarge WiFi duration */
1456 				wait_count = 0;
1457 				n = 3;
1458 				up = 0;
1459 				dn = 0;
1460 				result = 1;
1461 			}
1462 		} else if (retry_count <=
1463 			   3) {	/* <=3 retry in the last 2-second duration */
1464 			up--;
1465 			dn++;
1466 
1467 			if (up <= 0)
1468 				up = 0;
1469 
1470 			if (dn == 2) {	/* if continuous 2 retry count(every 2 seconds) >0 and < 3, reduce WiFi duration */
1471 				if (wait_count <= 2)
1472 					m++; /* to avoid loop between the two levels */
1473 				else
1474 					m = 1;
1475 
1476 				if (m >= 20)  /* maximum of m = 20 ' will recheck if need to adjust wifi duration in maximum time interval 120 seconds */
1477 					m = 20;
1478 
1479 				n = 3 * m;
1480 				up = 0;
1481 				dn = 0;
1482 				wait_count = 0;
1483 				result = -1;
1484 			}
1485 		} else { /* retry count > 3, once retry count > 3, to reduce WiFi duration */
1486 			if (wait_count == 1)
1487 				m++; /* to avoid loop between the two levels */
1488 			else
1489 				m = 1;
1490 
1491 			if (m >= 20)  /* maximum of m = 20 ' will recheck if need to adjust wifi duration in maximum time interval 120 seconds */
1492 				m = 20;
1493 
1494 			n = 3 * m;
1495 			up = 0;
1496 			dn = 0;
1497 			wait_count = 0;
1498 			result = -1;
1499 		}
1500 
1501 		if (result == -1) {
1502 			if ((BT_INFO_8192E_1ANT_A2DP_BASIC_RATE(bt_info_ext)) &&
1503 			    ((coex_dm->cur_ps_tdma == 1) ||
1504 			     (coex_dm->cur_ps_tdma == 2))) {
1505 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1506 							true, 9);
1507 				coex_dm->ps_tdma_du_adj_type = 9;
1508 			} else if (coex_dm->cur_ps_tdma == 1) {
1509 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1510 							true, 2);
1511 				coex_dm->ps_tdma_du_adj_type = 2;
1512 			} else if (coex_dm->cur_ps_tdma == 2) {
1513 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1514 							true, 9);
1515 				coex_dm->ps_tdma_du_adj_type = 9;
1516 			} else if (coex_dm->cur_ps_tdma == 9) {
1517 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1518 							true, 11);
1519 				coex_dm->ps_tdma_du_adj_type = 11;
1520 			}
1521 		} else if (result == 1) {
1522 			if ((BT_INFO_8192E_1ANT_A2DP_BASIC_RATE(bt_info_ext)) &&
1523 			    ((coex_dm->cur_ps_tdma == 1) ||
1524 			     (coex_dm->cur_ps_tdma == 2))) {
1525 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1526 							true, 9);
1527 				coex_dm->ps_tdma_du_adj_type = 9;
1528 			} else if (coex_dm->cur_ps_tdma == 11) {
1529 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1530 							true, 9);
1531 				coex_dm->ps_tdma_du_adj_type = 9;
1532 			} else if (coex_dm->cur_ps_tdma == 9) {
1533 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1534 							true, 2);
1535 				coex_dm->ps_tdma_du_adj_type = 2;
1536 			} else if (coex_dm->cur_ps_tdma == 2) {
1537 				halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1538 							true, 1);
1539 				coex_dm->ps_tdma_du_adj_type = 1;
1540 			}
1541 		} else { /* no change */
1542 			/* Bryant Modify
1543 			if(wifi_busy != pre_wifi_busy)
1544 			{
1545 				pre_wifi_busy = wifi_busy;
1546 				halbtc8192e1ant_ps_tdma(btcoexist, FORCE_EXEC, true, coex_dm->cur_ps_tdma);
1547 			}
1548 			*/
1549 		}
1550 
1551 		if (coex_dm->cur_ps_tdma != 1 &&
1552 		    coex_dm->cur_ps_tdma != 2 &&
1553 		    coex_dm->cur_ps_tdma != 9 &&
1554 		    coex_dm->cur_ps_tdma != 11) {
1555 			/* recover to previous adjust type */
1556 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1557 						coex_dm->ps_tdma_du_adj_type);
1558 		}
1559 	}
1560 }
1561 
halbtc8192e1ant_ps_tdma_check_for_power_save_state(IN struct btc_coexist * btcoexist,IN boolean new_ps_state)1562 void halbtc8192e1ant_ps_tdma_check_for_power_save_state(
1563 	IN struct btc_coexist *btcoexist, IN boolean new_ps_state)
1564 {
1565 	u8	lps_mode = 0x0;
1566 
1567 	btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
1568 
1569 	if (lps_mode) {	/* already under LPS state */
1570 		if (new_ps_state) {
1571 			/* keep state under LPS, do nothing. */
1572 		} else {
1573 			/* will leave LPS state, turn off psTdma first */
1574 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
1575 						0);
1576 		}
1577 	} else {					/* NO PS state */
1578 		if (new_ps_state) {
1579 			/* will enter LPS state, turn off psTdma first */
1580 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
1581 						0);
1582 		} else {
1583 			/* keep state under NO PS state, do nothing. */
1584 		}
1585 	}
1586 }
1587 
halbtc8192e1ant_power_save_state(IN struct btc_coexist * btcoexist,IN u8 ps_type,IN u8 lps_val,IN u8 rpwm_val)1588 void halbtc8192e1ant_power_save_state(IN struct btc_coexist *btcoexist,
1589 			      IN u8 ps_type, IN u8 lps_val, IN u8 rpwm_val)
1590 {
1591 	boolean		low_pwr_disable = false;
1592 
1593 	switch (ps_type) {
1594 	case BTC_PS_WIFI_NATIVE:
1595 		/* recover to original 32k low power setting */
1596 		low_pwr_disable = false;
1597 		btcoexist->btc_set(btcoexist,
1598 				   BTC_SET_ACT_DISABLE_LOW_POWER,
1599 				   &low_pwr_disable);
1600 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1601 				   NULL);
1602 		coex_sta->force_lps_on = false;
1603 		break;
1604 	case BTC_PS_LPS_ON:
1605 		halbtc8192e1ant_ps_tdma_check_for_power_save_state(
1606 			btcoexist, true);
1607 		halbtc8192e1ant_lps_rpwm(btcoexist, NORMAL_EXEC,
1608 					 lps_val, rpwm_val);
1609 		/* when coex force to enter LPS, do not enter 32k low power. */
1610 		low_pwr_disable = true;
1611 		btcoexist->btc_set(btcoexist,
1612 				   BTC_SET_ACT_DISABLE_LOW_POWER,
1613 				   &low_pwr_disable);
1614 		/* power save must executed before psTdma.			 */
1615 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS,
1616 				   NULL);
1617 		coex_sta->force_lps_on = true;
1618 		break;
1619 	case BTC_PS_LPS_OFF:
1620 		halbtc8192e1ant_ps_tdma_check_for_power_save_state(
1621 			btcoexist, false);
1622 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1623 				   NULL);
1624 		coex_sta->force_lps_on = false;
1625 		break;
1626 	default:
1627 		break;
1628 	}
1629 }
1630 
halbtc8192e1ant_action_wifi_only(IN struct btc_coexist * btcoexist)1631 void halbtc8192e1ant_action_wifi_only(IN struct btc_coexist *btcoexist)
1632 {
1633 	halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1634 	halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 9);
1635 }
1636 
halbtc8192e1ant_monitor_bt_enable_disable(IN struct btc_coexist * btcoexist)1637 void halbtc8192e1ant_monitor_bt_enable_disable(IN struct btc_coexist *btcoexist)
1638 {
1639 	static u32		bt_disable_cnt = 0;
1640 	boolean			bt_active = true, bt_disabled = false;
1641 
1642 	/* This function check if bt is disabled */
1643 
1644 	if (coex_sta->high_priority_tx == 0 &&
1645 	    coex_sta->high_priority_rx == 0 &&
1646 	    coex_sta->low_priority_tx == 0 &&
1647 	    coex_sta->low_priority_rx == 0)
1648 		bt_active = false;
1649 	if (coex_sta->high_priority_tx == 0xffff &&
1650 	    coex_sta->high_priority_rx == 0xffff &&
1651 	    coex_sta->low_priority_tx == 0xffff &&
1652 	    coex_sta->low_priority_rx == 0xffff)
1653 		bt_active = false;
1654 	if (bt_active) {
1655 		bt_disable_cnt = 0;
1656 		bt_disabled = false;
1657 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1658 				   &bt_disabled);
1659 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1660 			    "[BTCoex], BT is enabled !!\n");
1661 		BTC_TRACE(trace_buf);
1662 	} else {
1663 		bt_disable_cnt++;
1664 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1665 			    "[BTCoex], bt all counters=0, %d times!!\n",
1666 			    bt_disable_cnt);
1667 		BTC_TRACE(trace_buf);
1668 		if (bt_disable_cnt >= 2) {
1669 			bt_disabled = true;
1670 			btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1671 					   &bt_disabled);
1672 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1673 				    "[BTCoex], BT is disabled !!\n");
1674 			BTC_TRACE(trace_buf);
1675 			halbtc8192e1ant_action_wifi_only(btcoexist);
1676 		}
1677 	}
1678 	if (coex_sta->bt_disabled != bt_disabled) {
1679 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1680 			    "[BTCoex], BT is from %s to %s!!\n",
1681 			    (coex_sta->bt_disabled ? "disabled" : "enabled"),
1682 			    (bt_disabled ? "disabled" : "enabled"));
1683 		BTC_TRACE(trace_buf);
1684 		coex_sta->bt_disabled = bt_disabled;
1685 		if (!bt_disabled) {
1686 		} else {
1687 			btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1688 					   NULL);
1689 			btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1690 					   NULL);
1691 		}
1692 	}
1693 }
1694 
1695 /* *********************************************
1696  *
1697  *	Software Coex Mechanism start
1698  *
1699  * ********************************************* */
1700 
1701 /* SCO only or SCO+PAN(HS) */
1702 
1703 /*
1704 void halbtc8192e1ant_action_sco(IN struct btc_coexist* btcoexist)
1705 {
1706 	halbtc8192e1ant_sw_mechanism(btcoexist, true);
1707 }
1708 
1709 
1710 void halbtc8192e1ant_action_hid(IN struct btc_coexist* btcoexist)
1711 {
1712 	halbtc8192e1ant_sw_mechanism(btcoexist, true);
1713 }
1714 
1715 
1716 void halbtc8192e1ant_action_a2dp(IN struct btc_coexist* btcoexist)
1717 {
1718 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
1719 }
1720 
1721 void halbtc8192e1ant_action_a2dp_pan_hs(IN struct btc_coexist* btcoexist)
1722 {
1723 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
1724 }
1725 
1726 void halbtc8192e1ant_action_pan_edr(IN struct btc_coexist* btcoexist)
1727 {
1728 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
1729 }
1730 
1731 
1732 void halbtc8192e1ant_action_pan_hs(IN struct btc_coexist* btcoexist)
1733 {
1734 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
1735 }
1736 
1737 
1738 void halbtc8192e1ant_action_pan_edr_a2dp(IN struct btc_coexist* btcoexist)
1739 {
1740 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
1741 }
1742 
1743 void halbtc8192e1ant_action_pan_edr_hid(IN struct btc_coexist* btcoexist)
1744 {
1745 	halbtc8192e1ant_sw_mechanism(btcoexist, true);
1746 }
1747 
1748 
1749 void halbtc8192e1ant_action_hid_a2dp_pan_edr(IN struct btc_coexist* btcoexist)
1750 {
1751 	halbtc8192e1ant_sw_mechanism(btcoexist, true);
1752 }
1753 
1754 void halbtc8192e1ant_action_hid_a2dp(IN struct btc_coexist* btcoexist)
1755 {
1756 	halbtc8192e1ant_sw_mechanism(btcoexist, true);
1757 }
1758 
1759 */
1760 
1761 /* *********************************************
1762  *
1763  *	Non-Software Coex Mechanism start
1764  *
1765  * ********************************************* */
halbtc8192e1ant_action_wifi_multi_port(IN struct btc_coexist * btcoexist)1766 void halbtc8192e1ant_action_wifi_multi_port(IN struct btc_coexist *btcoexist)
1767 {
1768 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1769 					 0x0);
1770 
1771 	halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1772 	halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1773 }
1774 
halbtc8192e1ant_action_hs(IN struct btc_coexist * btcoexist)1775 void halbtc8192e1ant_action_hs(IN struct btc_coexist *btcoexist)
1776 {
1777 	halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1778 	halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1779 }
1780 
halbtc8192e1ant_action_bt_inquiry(IN struct btc_coexist * btcoexist)1781 void halbtc8192e1ant_action_bt_inquiry(IN struct btc_coexist *btcoexist)
1782 {
1783 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1784 	boolean			wifi_connected = false, ap_enable = false, wifi_busy = false,
1785 				bt_busy = false;
1786 
1787 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1788 			   &ap_enable);
1789 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1790 			   &wifi_connected);
1791 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1792 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
1793 
1794 	if ((!wifi_connected) && (!coex_sta->wifi_is_high_pri_task)) {
1795 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1796 						 0x0, 0x0);
1797 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1798 
1799 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1800 	} else if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1801 		   (bt_link_info->a2dp_exist)) {
1802 		/* SCO/HID/A2DP  busy */
1803 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1804 						 0x0, 0x0);
1805 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1806 
1807 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1808 	} else if ((bt_link_info->pan_exist) || (wifi_busy)) {
1809 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1810 						 0x0, 0x0);
1811 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1812 
1813 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1814 	} else {
1815 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1816 						 0x0, 0x0);
1817 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1818 
1819 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
1820 	}
1821 }
1822 
halbtc8192e1ant_action_bt_sco_hid_only_busy(IN struct btc_coexist * btcoexist,IN u8 wifi_status)1823 void halbtc8192e1ant_action_bt_sco_hid_only_busy(IN struct btc_coexist
1824 		*btcoexist, IN u8 wifi_status)
1825 {
1826 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1827 	boolean	wifi_connected = false;
1828 
1829 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1830 			   &wifi_connected);
1831 
1832 	/* tdma and coex table */
1833 
1834 	if (bt_link_info->sco_exist) {
1835 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1836 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
1837 	} else { /* HID */
1838 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 6);
1839 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
1840 	}
1841 }
1842 
halbtc8192e1ant_action_wifi_connected_bt_acl_busy(IN struct btc_coexist * btcoexist,IN u8 wifi_status)1843 void halbtc8192e1ant_action_wifi_connected_bt_acl_busy(IN struct btc_coexist
1844 		*btcoexist, IN u8 wifi_status)
1845 {
1846 	u8		bt_rssi_state;
1847 
1848 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1849 	bt_rssi_state = halbtc8192e1ant_bt_rssi_state(2, 28, 0);
1850 
1851 	if ((coex_sta->low_priority_rx >= 1000)  &&
1852 	    (coex_sta->low_priority_rx != 65535))
1853 		bt_link_info->slave_role = true;
1854 	else
1855 		bt_link_info->slave_role = false;
1856 
1857 	if (bt_link_info->hid_only) { /* HID */
1858 		halbtc8192e1ant_action_bt_sco_hid_only_busy(btcoexist,
1859 				wifi_status);
1860 		coex_dm->auto_tdma_adjust = false;
1861 		return;
1862 	} else if (bt_link_info->a2dp_only) { /* A2DP		 */
1863 		if (BT_8192E_1ANT_WIFI_STATUS_CONNECTED_IDLE == wifi_status) {
1864 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1865 						32);
1866 			halbtc8192e1ant_coex_table_with_type(btcoexist,
1867 							     NORMAL_EXEC, 4);
1868 			coex_dm->auto_tdma_adjust = false;
1869 		} else {
1870 			halbtc8192e1ant_tdma_duration_adjust_for_acl(btcoexist,
1871 					wifi_status);
1872 #if 0
1873 			if (coex_sta->cck_lock)
1874 				halbtc8192e1ant_coex_table_with_type(btcoexist,
1875 							     NORMAL_EXEC, 3);
1876 			else
1877 #endif
1878 				halbtc8192e1ant_coex_table_with_type(btcoexist,
1879 							     NORMAL_EXEC, 4);
1880 			coex_dm->auto_tdma_adjust = true;
1881 		}
1882 	} else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1883 		   (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1884 		bt_link_info->pan_exist)) { /* A2DP+PAN(OPP,FTP), HID+A2DP+PAN(OPP,FTP) */
1885 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 13);
1886 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1887 		coex_dm->auto_tdma_adjust = false;
1888 	} else if (bt_link_info->hid_exist &&
1889 		   bt_link_info->a2dp_exist) { /* HID+A2DP */
1890 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1891 		coex_dm->auto_tdma_adjust = false;
1892 
1893 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
1894 	} else if ((bt_link_info->pan_only) || (bt_link_info->hid_exist &&
1895 		bt_link_info->pan_exist)) { /* PAN(OPP,FTP), HID+PAN(OPP,FTP)			 */
1896 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1897 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1898 		coex_dm->auto_tdma_adjust = false;
1899 	} else {
1900 		/* BT no-profile busy (0x9) */
1901 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1902 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1903 		coex_dm->auto_tdma_adjust = false;
1904 	}
1905 }
1906 
halbtc8192e1ant_action_wifi_not_connected(IN struct btc_coexist * btcoexist)1907 void halbtc8192e1ant_action_wifi_not_connected(IN struct btc_coexist *btcoexist)
1908 {
1909 	/* power save state */
1910 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1911 					 0x0);
1912 
1913 	/* tdma and coex table */
1914 	halbtc8192e1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
1915 	halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1916 }
1917 
halbtc8192e1ant_action_wifi_not_connected_scan(IN struct btc_coexist * btcoexist)1918 void halbtc8192e1ant_action_wifi_not_connected_scan(IN struct btc_coexist
1919 		*btcoexist)
1920 {
1921 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1922 
1923 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1924 					 0x0);
1925 
1926 	/* tdma and coex table */
1927 	if (BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1928 		if (bt_link_info->a2dp_exist) {
1929 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1930 						32);
1931 			halbtc8192e1ant_coex_table_with_type(btcoexist,
1932 							     NORMAL_EXEC, 4);
1933 		} else if (bt_link_info->a2dp_exist &&
1934 			   bt_link_info->pan_exist) {
1935 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1936 						22);
1937 			halbtc8192e1ant_coex_table_with_type(btcoexist,
1938 							     NORMAL_EXEC, 4);
1939 		} else {
1940 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1941 						20);
1942 			halbtc8192e1ant_coex_table_with_type(btcoexist,
1943 							     NORMAL_EXEC, 4);
1944 		}
1945 	} else if ((BT_8192E_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
1946 		   (BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1947 		    coex_dm->bt_status)) {
1948 		halbtc8192e1ant_action_bt_sco_hid_only_busy(btcoexist,
1949 				BT_8192E_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1950 	} else {
1951 		/* Bryant Add */
1952 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1953 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1954 	}
1955 }
1956 
halbtc8192e1ant_action_wifi_not_connected_asso_auth(IN struct btc_coexist * btcoexist)1957 void halbtc8192e1ant_action_wifi_not_connected_asso_auth(
1958 	IN struct btc_coexist *btcoexist)
1959 {
1960 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1961 
1962 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1963 					 0x0);
1964 
1965 	/* tdma and coex table */
1966 	if ((bt_link_info->sco_exist)  || (bt_link_info->hid_exist) ||
1967 	    (bt_link_info->a2dp_exist)) {
1968 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1969 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1970 	} else if (bt_link_info->pan_exist) {
1971 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1972 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1973 	} else {
1974 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1975 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1976 	}
1977 }
1978 
halbtc8192e1ant_action_wifi_connected_scan(IN struct btc_coexist * btcoexist)1979 void halbtc8192e1ant_action_wifi_connected_scan(IN struct btc_coexist
1980 		*btcoexist)
1981 {
1982 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1983 
1984 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1985 					 0x0);
1986 
1987 	/* tdma and coex table */
1988 	if (BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1989 		if (bt_link_info->a2dp_exist) {
1990 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1991 						32);
1992 			halbtc8192e1ant_coex_table_with_type(btcoexist,
1993 							     NORMAL_EXEC, 4);
1994 		} else if (bt_link_info->a2dp_exist &&
1995 			   bt_link_info->pan_exist) {
1996 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1997 						22);
1998 			halbtc8192e1ant_coex_table_with_type(btcoexist,
1999 							     NORMAL_EXEC, 4);
2000 		} else {
2001 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
2002 						20);
2003 			halbtc8192e1ant_coex_table_with_type(btcoexist,
2004 							     NORMAL_EXEC, 4);
2005 		}
2006 	} else if ((BT_8192E_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2007 		   (BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY ==
2008 		    coex_dm->bt_status)) {
2009 		halbtc8192e1ant_action_bt_sco_hid_only_busy(btcoexist,
2010 				BT_8192E_1ANT_WIFI_STATUS_CONNECTED_SCAN);
2011 	} else {
2012 		/* Bryant Add */
2013 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
2014 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2015 	}
2016 }
2017 
halbtc8192e1ant_action_wifi_connected_specific_packet(IN struct btc_coexist * btcoexist)2018 void halbtc8192e1ant_action_wifi_connected_specific_packet(
2019 	IN struct btc_coexist *btcoexist)
2020 {
2021 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2022 
2023 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
2024 					 0x0);
2025 
2026 	/* tdma and coex table */
2027 	if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
2028 	    (bt_link_info->a2dp_exist)) {
2029 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
2030 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2031 	} else if (bt_link_info->pan_exist) {
2032 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
2033 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2034 	} else {
2035 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
2036 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2037 	}
2038 }
2039 
halbtc8192e1ant_action_wifi_connected(IN struct btc_coexist * btcoexist)2040 void halbtc8192e1ant_action_wifi_connected(IN struct btc_coexist *btcoexist)
2041 {
2042 	boolean	wifi_busy = false;
2043 	boolean		scan = false, link = false, roam = false;
2044 	boolean		under_4way = false, ap_enable = false;
2045 
2046 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2047 		    "[BTCoex], CoexForWifiConnect()===>\n");
2048 	BTC_TRACE(trace_buf);
2049 
2050 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
2051 			   &under_4way);
2052 	if (under_4way) {
2053 		halbtc8192e1ant_action_wifi_connected_specific_packet(btcoexist);
2054 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2055 			"[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
2056 		BTC_TRACE(trace_buf);
2057 		return;
2058 	}
2059 
2060 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2061 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2062 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2063 	if (scan || link || roam) {
2064 		if (scan)
2065 			halbtc8192e1ant_action_wifi_connected_scan(btcoexist);
2066 		else
2067 			halbtc8192e1ant_action_wifi_connected_specific_packet(
2068 				btcoexist);
2069 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2070 			"[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
2071 		BTC_TRACE(trace_buf);
2072 		return;
2073 	}
2074 
2075 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
2076 			   &ap_enable);
2077 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2078 
2079 	/* power save state */
2080 	if (!ap_enable &&
2081 	    BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status &&
2082 	    !btcoexist->bt_link_info.hid_only) {
2083 		if (btcoexist->bt_link_info.a2dp_only) {	/* A2DP */
2084 			if (!wifi_busy)
2085 				halbtc8192e1ant_power_save_state(btcoexist,
2086 						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2087 			else { /* busy */
2088 				if (coex_sta->scan_ap_num >=
2089 				    BT_8192E_1ANT_WIFI_NOISY_THRESH)  /* no force LPS, no PS-TDMA, use pure TDMA */
2090 					halbtc8192e1ant_power_save_state(
2091 						btcoexist, BTC_PS_WIFI_NATIVE,
2092 						0x0, 0x0);
2093 				else
2094 					halbtc8192e1ant_power_save_state(
2095 						btcoexist, BTC_PS_LPS_ON, 0x50,
2096 						0x4);
2097 			}
2098 		} else if ((coex_sta->pan_exist == false) &&
2099 			   (coex_sta->a2dp_exist == false) &&
2100 			   (coex_sta->hid_exist == false))
2101 			halbtc8192e1ant_power_save_state(btcoexist,
2102 						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2103 		else
2104 			halbtc8192e1ant_power_save_state(btcoexist,
2105 						 BTC_PS_LPS_ON, 0x50, 0x4);
2106 	} else
2107 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2108 						 0x0, 0x0);
2109 
2110 	/* tdma and coex table */
2111 	if (!wifi_busy) {
2112 		if (BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
2113 			halbtc8192e1ant_action_wifi_connected_bt_acl_busy(
2114 				btcoexist,
2115 				BT_8192E_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2116 		} else if ((BT_8192E_1ANT_BT_STATUS_SCO_BUSY ==
2117 			    coex_dm->bt_status) ||
2118 			   (BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY ==
2119 			    coex_dm->bt_status)) {
2120 			halbtc8192e1ant_action_bt_sco_hid_only_busy(btcoexist,
2121 				BT_8192E_1ANT_WIFI_STATUS_CONNECTED_IDLE);
2122 		} else {
2123 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
2124 						8);
2125 
2126 			if ((coex_sta->high_priority_tx) +
2127 			    (coex_sta->high_priority_rx) <= 60)
2128 				halbtc8192e1ant_coex_table_with_type(btcoexist,
2129 							     NORMAL_EXEC, 2);
2130 			else
2131 				halbtc8192e1ant_coex_table_with_type(btcoexist,
2132 							     NORMAL_EXEC, 7);
2133 		}
2134 	} else {
2135 		if (BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
2136 			halbtc8192e1ant_action_wifi_connected_bt_acl_busy(
2137 				btcoexist,
2138 				BT_8192E_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2139 		} else if ((BT_8192E_1ANT_BT_STATUS_SCO_BUSY ==
2140 			    coex_dm->bt_status) ||
2141 			   (BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY ==
2142 			    coex_dm->bt_status)) {
2143 			halbtc8192e1ant_action_bt_sco_hid_only_busy(btcoexist,
2144 				BT_8192E_1ANT_WIFI_STATUS_CONNECTED_BUSY);
2145 		} else {
2146 			halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
2147 						8);
2148 
2149 			if ((coex_sta->high_priority_tx) +
2150 			    (coex_sta->high_priority_rx) <= 60)
2151 				halbtc8192e1ant_coex_table_with_type(btcoexist,
2152 							     NORMAL_EXEC, 2);
2153 			else
2154 				halbtc8192e1ant_coex_table_with_type(btcoexist,
2155 							     NORMAL_EXEC, 7);
2156 		}
2157 	}
2158 }
2159 
halbtc8192e1ant_run_sw_coexist_mechanism(IN struct btc_coexist * btcoexist)2160 void halbtc8192e1ant_run_sw_coexist_mechanism(IN struct btc_coexist *btcoexist)
2161 {
2162 	u8				algorithm = 0;
2163 
2164 	algorithm = halbtc8192e1ant_action_algorithm(btcoexist);
2165 	coex_dm->cur_algorithm = algorithm;
2166 
2167 	if (halbtc8192e1ant_is_common_action(btcoexist)) {
2168 
2169 	} else {
2170 		switch (coex_dm->cur_algorithm) {
2171 		case BT_8192E_1ANT_COEX_ALGO_SCO:
2172 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2173 				    "[BTCoex], Action algorithm = SCO.\n");
2174 			BTC_TRACE(trace_buf);
2175 			/* halbtc8192e1ant_action_sco(btcoexist); */
2176 			break;
2177 		case BT_8192E_1ANT_COEX_ALGO_HID:
2178 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2179 				    "[BTCoex], Action algorithm = HID.\n");
2180 			BTC_TRACE(trace_buf);
2181 			/* halbtc8192e1ant_action_hid(btcoexist); */
2182 			break;
2183 		case BT_8192E_1ANT_COEX_ALGO_A2DP:
2184 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2185 				    "[BTCoex], Action algorithm = A2DP.\n");
2186 			BTC_TRACE(trace_buf);
2187 			/* halbtc8192e1ant_action_a2dp(btcoexist); */
2188 			break;
2189 		case BT_8192E_1ANT_COEX_ALGO_A2DP_PANHS:
2190 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2191 				"[BTCoex], Action algorithm = A2DP+PAN(HS).\n");
2192 			BTC_TRACE(trace_buf);
2193 			/* halbtc8192e1ant_action_a2dp_pan_hs(btcoexist); */
2194 			break;
2195 		case BT_8192E_1ANT_COEX_ALGO_PANEDR:
2196 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2197 				    "[BTCoex], Action algorithm = PAN(EDR).\n");
2198 			BTC_TRACE(trace_buf);
2199 			/* halbtc8192e1ant_action_pan_edr(btcoexist); */
2200 			break;
2201 		case BT_8192E_1ANT_COEX_ALGO_PANHS:
2202 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2203 				    "[BTCoex], Action algorithm = HS mode.\n");
2204 			BTC_TRACE(trace_buf);
2205 			/* halbtc8192e1ant_action_pan_hs(btcoexist); */
2206 			break;
2207 		case BT_8192E_1ANT_COEX_ALGO_PANEDR_A2DP:
2208 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2209 				    "[BTCoex], Action algorithm = PAN+A2DP.\n");
2210 			BTC_TRACE(trace_buf);
2211 			/* halbtc8192e1ant_action_pan_edr_a2dp(btcoexist); */
2212 			break;
2213 		case BT_8192E_1ANT_COEX_ALGO_PANEDR_HID:
2214 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2215 				"[BTCoex], Action algorithm = PAN(EDR)+HID.\n");
2216 			BTC_TRACE(trace_buf);
2217 			/* halbtc8192e1ant_action_pan_edr_hid(btcoexist); */
2218 			break;
2219 		case BT_8192E_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2220 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2221 				"[BTCoex], Action algorithm = HID+A2DP+PAN.\n");
2222 			BTC_TRACE(trace_buf);
2223 			/* halbtc8192e1ant_action_hid_a2dp_pan_edr(btcoexist); */
2224 			break;
2225 		case BT_8192E_1ANT_COEX_ALGO_HID_A2DP:
2226 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2227 				    "[BTCoex], Action algorithm = HID+A2DP.\n");
2228 			BTC_TRACE(trace_buf);
2229 			/* halbtc8192e1ant_action_hid_a2dp(btcoexist); */
2230 			break;
2231 		default:
2232 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2233 				"[BTCoex], Action algorithm = coexist All Off!!\n");
2234 			BTC_TRACE(trace_buf);
2235 			/* halbtc8192e1ant_coex_all_off(btcoexist); */
2236 			break;
2237 		}
2238 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
2239 	}
2240 }
2241 
halbtc8192e1ant_run_coexist_mechanism(IN struct btc_coexist * btcoexist)2242 void halbtc8192e1ant_run_coexist_mechanism(IN struct btc_coexist *btcoexist)
2243 {
2244 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2245 	boolean	wifi_connected = false, bt_hs_on = false;
2246 	boolean	increase_scan_dev_num = false;
2247 	boolean	bt_ctrl_agg_buf_size = false;
2248 	boolean	miracast_plus_bt = false;
2249 	u8	agg_buf_size = 5;
2250 	u32	wifi_link_status = 0;
2251 	u32	num_of_wifi_link = 0;
2252 
2253 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2254 		    "[BTCoex], RunCoexistMechanism()===>\n");
2255 	BTC_TRACE(trace_buf);
2256 
2257 	if (btcoexist->manual_control) {
2258 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2259 			"[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2260 		BTC_TRACE(trace_buf);
2261 		return;
2262 	}
2263 
2264 	if (btcoexist->stop_coex_dm) {
2265 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2266 			"[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2267 		BTC_TRACE(trace_buf);
2268 		return;
2269 	}
2270 
2271 	if (coex_sta->under_ips) {
2272 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2273 			    "[BTCoex], wifi is under IPS !!!\n");
2274 		BTC_TRACE(trace_buf);
2275 		return;
2276 	}
2277 
2278 	if ((BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2279 	    (BT_8192E_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2280 	    (BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2281 		increase_scan_dev_num = true;
2282 
2283 	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2284 			   &increase_scan_dev_num);
2285 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2286 			   &wifi_connected);
2287 
2288 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2289 			   &wifi_link_status);
2290 	num_of_wifi_link = wifi_link_status >> 16;
2291 
2292 	if ((num_of_wifi_link >= 2) ||
2293 	    (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
2294 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2295 			"############# [BTCoex],  Multi-Port num_of_wifi_link = %d, wifi_link_status = 0x%x\n",
2296 			    num_of_wifi_link, wifi_link_status);
2297 		BTC_TRACE(trace_buf);
2298 
2299 		if (bt_link_info->bt_link_exist) {
2300 			halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1,
2301 						   0, 1);
2302 			miracast_plus_bt = true;
2303 		} else {
2304 			halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0,
2305 						   0, 0);
2306 			miracast_plus_bt = false;
2307 		}
2308 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2309 				   &miracast_plus_bt);
2310 		halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2311 					   bt_ctrl_agg_buf_size, agg_buf_size);
2312 
2313 		if ((bt_link_info->a2dp_exist) &&
2314 		    (coex_sta->c2h_bt_inquiry_page)) {
2315 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2316 				"############# [BTCoex],  BT Is Inquirying\n");
2317 			BTC_TRACE(trace_buf);
2318 			halbtc8192e1ant_action_bt_inquiry(btcoexist);
2319 		} else
2320 			halbtc8192e1ant_action_wifi_multi_port(btcoexist);
2321 
2322 		return;
2323 	} else {
2324 		miracast_plus_bt = false;
2325 		btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2326 				   &miracast_plus_bt);
2327 	}
2328 
2329 	if ((bt_link_info->bt_link_exist) && (wifi_connected)) {
2330 		halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1, 0, 1);
2331 
2332 		if (bt_link_info->sco_exist)
2333 			halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC,
2334 						   false, true, 0x5);
2335 		else
2336 			halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC,
2337 						   false, true, 0x8);
2338 
2339 		halbtc8192e1ant_sw_mechanism(btcoexist, true);
2340 		halbtc8192e1ant_run_sw_coexist_mechanism(
2341 			btcoexist);  /* just print debug message */
2342 	} else {
2343 		halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2344 
2345 		halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false,
2346 					   0x5);
2347 
2348 		halbtc8192e1ant_sw_mechanism(btcoexist, false);
2349 		halbtc8192e1ant_run_sw_coexist_mechanism(
2350 			btcoexist); /* just print debug message */
2351 	}
2352 
2353 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2354 	if (coex_sta->c2h_bt_inquiry_page) {
2355 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2356 			    "############# [BTCoex],  BT Is Inquirying\n");
2357 		BTC_TRACE(trace_buf);
2358 		halbtc8192e1ant_action_bt_inquiry(btcoexist);
2359 		return;
2360 	} else if (bt_hs_on) {
2361 		halbtc8192e1ant_action_hs(btcoexist);
2362 		return;
2363 	}
2364 
2365 
2366 	if (!wifi_connected) {
2367 		boolean	scan = false, link = false, roam = false;
2368 
2369 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2370 			    "[BTCoex], wifi is non connected-idle !!!\n");
2371 		BTC_TRACE(trace_buf);
2372 
2373 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2374 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2375 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2376 
2377 		if (scan || link || roam) {
2378 			if (scan)
2379 				halbtc8192e1ant_action_wifi_not_connected_scan(
2380 					btcoexist);
2381 			else
2382 				halbtc8192e1ant_action_wifi_not_connected_asso_auth(
2383 					btcoexist);
2384 		} else
2385 			halbtc8192e1ant_action_wifi_not_connected(btcoexist);
2386 	} else	/* wifi LPS/Busy */
2387 		halbtc8192e1ant_action_wifi_connected(btcoexist);
2388 }
2389 
halbtc8192e1ant_init_coex_dm(IN struct btc_coexist * btcoexist)2390 void halbtc8192e1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
2391 {
2392 	/* force to reset coex mechanism */
2393 
2394 	/* sw all off */
2395 	halbtc8192e1ant_sw_mechanism(btcoexist, false);
2396 
2397 	/* halbtc8192e1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8); */
2398 	halbtc8192e1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2399 
2400 	coex_sta->pop_event_cnt = 0;
2401 }
2402 
halbtc8192e1ant_init_hw_config(IN struct btc_coexist * btcoexist,IN boolean wifi_only)2403 void halbtc8192e1ant_init_hw_config(IN struct btc_coexist *btcoexist,
2404 				    IN boolean wifi_only)
2405 {
2406 	u16	u16tmp = 0;
2407 	u8	u8tmp = 0;
2408 
2409 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2410 		    "[BTCoex], 1Ant Init HW Config!!\n");
2411 	BTC_TRACE(trace_buf);
2412 
2413 	/* antenna sw ctrl to bt */
2414 	halbtc8192e1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, true, false);
2415 
2416 	halbtc8192e1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2417 
2418 	/* antenna switch control parameter */
2419 	btcoexist->btc_write_4byte(btcoexist, 0x858, 0x55555555);
2420 
2421 	/* coex parameters */
2422 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x1);
2423 	/* 0x790[5:0]=0x5 */
2424 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2425 	u8tmp &= 0xc0;
2426 	u8tmp |= 0x5;
2427 	btcoexist->btc_write_1byte(btcoexist, 0x790, u8tmp);
2428 
2429 	/* enable counter statistics */
2430 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
2431 
2432 	/* enable PTA */
2433 	btcoexist->btc_write_1byte(btcoexist, 0x40, 0x20);
2434 	/* enable mailbox interface */
2435 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x40);
2436 	u16tmp |= BIT(9);
2437 	btcoexist->btc_write_2byte(btcoexist, 0x40, u16tmp);
2438 
2439 	/* enable PTA I2C mailbox */
2440 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x101);
2441 	u8tmp |= BIT(4);
2442 	btcoexist->btc_write_1byte(btcoexist, 0x101, u8tmp);
2443 
2444 	/* enable bt clock when wifi is disabled. */
2445 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x93);
2446 	u8tmp |= BIT(0);
2447 	btcoexist->btc_write_1byte(btcoexist, 0x93, u8tmp);
2448 	/* enable bt clock when suspend. */
2449 	u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x7);
2450 	u8tmp |= BIT(0);
2451 	btcoexist->btc_write_1byte(btcoexist, 0x7, u8tmp);
2452 }
2453 
2454 
2455 /*
2456 void halbtc8192e1ant_wifi_off_hw_cfg(IN struct btc_coexist* btcoexist)
2457 {
2458 
2459 
2460 }
2461 */
2462 
2463 /* ************************************************************
2464  * work around function start with wa_halbtc8192e1ant_
2465  * ************************************************************
2466  * ************************************************************
2467  * extern function start with ex_halbtc8192e1ant_
2468  * ************************************************************ */
ex_halbtc8192e1ant_power_on_setting(IN struct btc_coexist * btcoexist)2469 void ex_halbtc8192e1ant_power_on_setting(IN struct btc_coexist *btcoexist)
2470 {
2471 #if 0
2472 	struct  btc_board_info	*board_info = &btcoexist->board_info;
2473 	u8 u8tmp = 0x0;
2474 	u16 u16tmp = 0x0;
2475 
2476 	btcoexist->stop_coex_dm = true;
2477 
2478 	btcoexist->btc_write_1byte(btcoexist, 0x67, 0x20);
2479 
2480 	/* enable BB, REG_SYS_FUNC_EN such that we can write 0x948 correctly. */
2481 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
2482 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT(0) | BIT(1));
2483 
2484 	/* set GRAN_BT = 1 */
2485 	btcoexist->btc_write_1byte(btcoexist, 0x765, 0x18);
2486 	/* set WLAN_ACT = 0 */
2487 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
2488 
2489 	/*  */
2490 	/* S0 or S1 setting and Local register setting(By the setting fw can get ant number, S0/S1, ... info) */
2491 	/* Local setting bit define */
2492 	/*	BIT0: "0" for no antenna inverse; "1" for antenna inverse  */
2493 	/*	BIT1: "0" for internal switch; "1" for external switch */
2494 	/*	BIT2: "0" for one antenna; "1" for two antenna */
2495 	/* NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and BIT2=0 */
2496 	if (btcoexist->chip_interface == BTC_INTF_USB) {
2497 		/* fixed at S0 for USB interface */
2498 		btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
2499 
2500 		u8tmp |= 0x1;	/* antenna inverse */
2501 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
2502 
2503 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
2504 	} else {
2505 		/* for PCIE and SDIO interface, we check efuse 0xc3[6] */
2506 		if (board_info->single_ant_path == 0) {
2507 			/* set to S1 */
2508 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x280);
2509 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
2510 		} else if (board_info->single_ant_path == 1) {
2511 			/* set to S0 */
2512 			btcoexist->btc_write_4byte(btcoexist, 0x948, 0x0);
2513 			u8tmp |= 0x1;	/* antenna inverse */
2514 			board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
2515 		}
2516 
2517 		if (btcoexist->chip_interface == BTC_INTF_PCI)
2518 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x384,
2519 							     u8tmp);
2520 		else if (btcoexist->chip_interface == BTC_INTF_SDIO)
2521 			btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60,
2522 							     u8tmp);
2523 	}
2524 #endif
2525 }
2526 
ex_halbtc8192e1ant_pre_load_firmware(IN struct btc_coexist * btcoexist)2527 void ex_halbtc8192e1ant_pre_load_firmware(IN struct btc_coexist *btcoexist)
2528 {
2529 }
2530 
ex_halbtc8192e1ant_init_hw_config(IN struct btc_coexist * btcoexist,IN boolean wifi_only)2531 void ex_halbtc8192e1ant_init_hw_config(IN struct btc_coexist *btcoexist,
2532 				       IN boolean wifi_only)
2533 {
2534 	halbtc8192e1ant_init_hw_config(btcoexist, wifi_only);
2535 	btcoexist->stop_coex_dm = false;
2536 }
2537 
ex_halbtc8192e1ant_init_coex_dm(IN struct btc_coexist * btcoexist)2538 void ex_halbtc8192e1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
2539 {
2540 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2541 		    "[BTCoex], Coex Mechanism Init!!\n");
2542 	BTC_TRACE(trace_buf);
2543 
2544 	btcoexist->stop_coex_dm = false;
2545 
2546 	halbtc8192e1ant_init_coex_dm(btcoexist);
2547 
2548 	halbtc8192e1ant_query_bt_info(btcoexist);
2549 }
2550 
ex_halbtc8192e1ant_display_coex_info(IN struct btc_coexist * btcoexist)2551 void ex_halbtc8192e1ant_display_coex_info(IN struct btc_coexist *btcoexist)
2552 {
2553 	struct  btc_board_info		*board_info = &btcoexist->board_info;
2554 	struct  btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
2555 	u8				*cli_buf = btcoexist->cli_buf;
2556 	u8				u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2557 	u32				u32tmp[4];
2558 	u32				fw_ver = 0, bt_patch_ver = 0;
2559 
2560 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2561 		   "\r\n ============[BT Coexist info]============");
2562 	CL_PRINTF(cli_buf);
2563 
2564 	if (btcoexist->manual_control) {
2565 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2566 			"\r\n ============[Under Manual Control]============");
2567 		CL_PRINTF(cli_buf);
2568 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2569 			   "\r\n ==========================================");
2570 		CL_PRINTF(cli_buf);
2571 	}
2572 	if (btcoexist->stop_coex_dm) {
2573 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2574 			   "\r\n ============[Coex is STOPPED]============");
2575 		CL_PRINTF(cli_buf);
2576 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2577 			   "\r\n ==========================================");
2578 		CL_PRINTF(cli_buf);
2579 	}
2580 
2581 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d ",
2582 		   "Ant PG number/ Ant mechanism:",
2583 		   board_info->pg_ant_num, board_info->btdm_ant_num);
2584 	CL_PRINTF(cli_buf);
2585 
2586 	btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
2587 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2588 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2589 		   "\r\n %-35s = %d_%d/ 0x%x/ 0x%x(%d)",
2590 		   "CoexVer/ FwVer/ PatchVer",
2591 		   glcoex_ver_date_8192e_1ant, glcoex_ver_8192e_1ant, fw_ver,
2592 		   bt_patch_ver, bt_patch_ver);
2593 	CL_PRINTF(cli_buf);
2594 
2595 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x ",
2596 		   "Wifi channel informed to BT",
2597 		   coex_dm->wifi_chnl_info[0], coex_dm->wifi_chnl_info[1],
2598 		   coex_dm->wifi_chnl_info[2]);
2599 	CL_PRINTF(cli_buf);
2600 
2601 	/* wifi status */
2602 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2603 		   "============[Wifi Status]============");
2604 	CL_PRINTF(cli_buf);
2605 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_WIFI_STATUS);
2606 
2607 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2608 		   "============[BT Status]============");
2609 	CL_PRINTF(cli_buf);
2610 
2611 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s/ %d/ %d] ",
2612 		   "BT [status/ rssi/ retryCnt]",
2613 		   ((coex_sta->bt_disabled) ? ("disabled") :	((
2614 		   coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan")
2615 			   : ((BT_8192E_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2616 			       coex_dm->bt_status) ? "non-connected idle" :
2617 		((BT_8192E_1ANT_BT_STATUS_CONNECTED_IDLE == coex_dm->bt_status)
2618 				       ? "connected-idle" : "busy")))),
2619 		   coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2620 	CL_PRINTF(cli_buf);
2621 
2622 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d / %d / %d",
2623 		   "SCO/HID/PAN/A2DP",
2624 		   bt_link_info->sco_exist, bt_link_info->hid_exist,
2625 		   bt_link_info->pan_exist, bt_link_info->a2dp_exist);
2626 	CL_PRINTF(cli_buf);
2627 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
2628 
2629 	bt_info_ext = coex_sta->bt_info_ext;
2630 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s",
2631 		   "BT Info A2DP rate",
2632 		   (bt_info_ext & BIT(0)) ? "Basic rate" : "EDR rate");
2633 	CL_PRINTF(cli_buf);
2634 
2635 	for (i = 0; i < BT_INFO_SRC_8192E_1ANT_MAX; i++) {
2636 		if (coex_sta->bt_info_c2h_cnt[i]) {
2637 			CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2638 				"\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)",
2639 				   glbt_info_src_8192e_1ant[i],
2640 				   coex_sta->bt_info_c2h[i][0],
2641 				   coex_sta->bt_info_c2h[i][1],
2642 				   coex_sta->bt_info_c2h[i][2],
2643 				   coex_sta->bt_info_c2h[i][3],
2644 				   coex_sta->bt_info_c2h[i][4],
2645 				   coex_sta->bt_info_c2h[i][5],
2646 				   coex_sta->bt_info_c2h[i][6],
2647 				   coex_sta->bt_info_c2h_cnt[i]);
2648 			CL_PRINTF(cli_buf);
2649 		}
2650 	}
2651 
2652 	if (!btcoexist->manual_control) {
2653 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2654 			   "============[mechanisms]============");
2655 		CL_PRINTF(cli_buf);
2656 
2657 		ps_tdma_case = coex_dm->cur_ps_tdma;
2658 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2659 			"\r\n %-35s = %02x %02x %02x %02x %02x case-%d (auto:%d)",
2660 			   "PS TDMA",
2661 			   coex_dm->ps_tdma_para[0], coex_dm->ps_tdma_para[1],
2662 			   coex_dm->ps_tdma_para[2], coex_dm->ps_tdma_para[3],
2663 			   coex_dm->ps_tdma_para[4], ps_tdma_case,
2664 			   coex_dm->auto_tdma_adjust);
2665 		CL_PRINTF(cli_buf);
2666 
2667 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x ",
2668 			   "Latest error condition(should be 0)",
2669 			   coex_dm->error_condition);
2670 		CL_PRINTF(cli_buf);
2671 
2672 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ",
2673 			   "IgnWlanAct",
2674 			   coex_dm->cur_ignore_wlan_act);
2675 		CL_PRINTF(cli_buf);
2676 	}
2677 
2678 	/* Hw setting		 */
2679 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2680 		   "============[Hw setting]============");
2681 	CL_PRINTF(cli_buf);
2682 
2683 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc04);
2684 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0xd04);
2685 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x90c);
2686 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2687 		   "0xc04/ 0xd04/ 0x90c",
2688 		   u32tmp[0], u32tmp[1], u32tmp[2]);
2689 	CL_PRINTF(cli_buf);
2690 
2691 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2692 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x778",
2693 		   u8tmp[0]);
2694 	CL_PRINTF(cli_buf);
2695 
2696 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x92c);
2697 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x930);
2698 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
2699 		   "0x92c/ 0x930",
2700 		   (u8tmp[0]), u32tmp[0]);
2701 	CL_PRINTF(cli_buf);
2702 
2703 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2704 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x4f);
2705 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
2706 		   "0x40/ 0x4f",
2707 		   u8tmp[0], u8tmp[1]);
2708 	CL_PRINTF(cli_buf);
2709 
2710 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2711 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2712 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
2713 		   "0x550(bcn ctrl)/0x522",
2714 		   u32tmp[0], u8tmp[0]);
2715 	CL_PRINTF(cli_buf);
2716 
2717 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2718 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0xc50(dig)",
2719 		   u32tmp[0]);
2720 	CL_PRINTF(cli_buf);
2721 
2722 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2723 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2724 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2725 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
2726 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2727 		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2728 		   "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
2729 		   u32tmp[0], u32tmp[1], u32tmp[2], u8tmp[0]);
2730 	CL_PRINTF(cli_buf);
2731 
2732 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
2733 		   "0x770(hp rx[31:16]/tx[15:0])",
2734 		   coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2735 	CL_PRINTF(cli_buf);
2736 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
2737 		   "0x774(lp rx[31:16]/tx[15:0])",
2738 		   coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2739 	CL_PRINTF(cli_buf);
2740 #if (BT_AUTO_REPORT_ONLY_8192E_1ANT == 1)
2741 	halbtc8192e1ant_monitor_bt_ctr(btcoexist);
2742 #endif
2743 
2744 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS);
2745 }
2746 
ex_halbtc8192e1ant_ips_notify(IN struct btc_coexist * btcoexist,IN u8 type)2747 void ex_halbtc8192e1ant_ips_notify(IN struct btc_coexist *btcoexist, IN u8 type)
2748 {
2749 	if (btcoexist->manual_control ||	btcoexist->stop_coex_dm)
2750 		return;
2751 
2752 	if (BTC_IPS_ENTER == type) {
2753 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2754 			    "[BTCoex], IPS ENTER notify\n");
2755 		BTC_TRACE(trace_buf);
2756 		coex_sta->under_ips = true;
2757 
2758 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2759 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2760 		halbtc8192e1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
2761 					     true);
2762 	} else if (BTC_IPS_LEAVE == type) {
2763 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2764 			    "[BTCoex], IPS LEAVE notify\n");
2765 		BTC_TRACE(trace_buf);
2766 		coex_sta->under_ips = false;
2767 
2768 		halbtc8192e1ant_init_hw_config(btcoexist, false);
2769 		halbtc8192e1ant_init_coex_dm(btcoexist);
2770 		halbtc8192e1ant_query_bt_info(btcoexist);
2771 	}
2772 }
2773 
ex_halbtc8192e1ant_lps_notify(IN struct btc_coexist * btcoexist,IN u8 type)2774 void ex_halbtc8192e1ant_lps_notify(IN struct btc_coexist *btcoexist, IN u8 type)
2775 {
2776 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2777 		return;
2778 
2779 	if (BTC_LPS_ENABLE == type) {
2780 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2781 			    "[BTCoex], LPS ENABLE notify\n");
2782 		BTC_TRACE(trace_buf);
2783 		coex_sta->under_lps = true;
2784 	} else if (BTC_LPS_DISABLE == type) {
2785 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2786 			    "[BTCoex], LPS DISABLE notify\n");
2787 		BTC_TRACE(trace_buf);
2788 		coex_sta->under_lps = false;
2789 	}
2790 }
2791 
ex_halbtc8192e1ant_scan_notify(IN struct btc_coexist * btcoexist,IN u8 type)2792 void ex_halbtc8192e1ant_scan_notify(IN struct btc_coexist *btcoexist,
2793 				    IN u8 type)
2794 {
2795 	boolean wifi_connected = false, bt_hs_on = false;
2796 	u32	wifi_link_status = 0;
2797 	u32	num_of_wifi_link = 0;
2798 	boolean	bt_ctrl_agg_buf_size = false;
2799 	u8	agg_buf_size = 5;
2800 
2801 	u8 u8tmpa, u8tmpb;
2802 	u32 u32tmp;
2803 
2804 	if (btcoexist->manual_control ||
2805 	    btcoexist->stop_coex_dm)
2806 		return;
2807 
2808 	if (BTC_SCAN_START == type) {
2809 		coex_sta->wifi_is_high_pri_task = true;
2810 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2811 			    "[BTCoex], SCAN START notify\n");
2812 		BTC_TRACE(trace_buf);
2813 
2814 		halbtc8192e1ant_ps_tdma(btcoexist, FORCE_EXEC, false,
2815 			8);  /* Force antenna setup for no scan result issue */
2816 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
2817 		u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
2818 		u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
2819 
2820 
2821 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2822 			    "[BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x\n",
2823 			    u32tmp,  u8tmpa, u8tmpb);
2824 		BTC_TRACE(trace_buf);
2825 	} else {
2826 		coex_sta->wifi_is_high_pri_task = false;
2827 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2828 			    "[BTCoex], SCAN FINISH notify\n");
2829 		BTC_TRACE(trace_buf);
2830 
2831 		btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
2832 				   &coex_sta->scan_ap_num);
2833 	}
2834 
2835 	if (coex_sta->bt_disabled)
2836 		return;
2837 
2838 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2839 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2840 			   &wifi_connected);
2841 
2842 	halbtc8192e1ant_query_bt_info(btcoexist);
2843 
2844 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2845 			   &wifi_link_status);
2846 	num_of_wifi_link = wifi_link_status >> 16;
2847 	if (num_of_wifi_link >= 2) {
2848 		halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2849 		halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2850 					   bt_ctrl_agg_buf_size, agg_buf_size);
2851 		halbtc8192e1ant_action_wifi_multi_port(btcoexist);
2852 		return;
2853 	}
2854 
2855 	if (coex_sta->c2h_bt_inquiry_page) {
2856 		halbtc8192e1ant_action_bt_inquiry(btcoexist);
2857 		return;
2858 	} else if (bt_hs_on) {
2859 		halbtc8192e1ant_action_hs(btcoexist);
2860 		return;
2861 	}
2862 
2863 	if (BTC_SCAN_START == type) {
2864 		if (!wifi_connected)	/* non-connected scan */
2865 			halbtc8192e1ant_action_wifi_not_connected_scan(
2866 				btcoexist);
2867 		else	/* wifi is connected */
2868 			halbtc8192e1ant_action_wifi_connected_scan(btcoexist);
2869 	} else if (BTC_SCAN_FINISH == type) {
2870 		if (!wifi_connected)	/* non-connected scan */
2871 			halbtc8192e1ant_action_wifi_not_connected(btcoexist);
2872 		else
2873 			halbtc8192e1ant_action_wifi_connected(btcoexist);
2874 	}
2875 }
2876 
ex_halbtc8192e1ant_connect_notify(IN struct btc_coexist * btcoexist,IN u8 type)2877 void ex_halbtc8192e1ant_connect_notify(IN struct btc_coexist *btcoexist,
2878 				       IN u8 type)
2879 {
2880 	boolean	wifi_connected = false, bt_hs_on = false;
2881 	u32	wifi_link_status = 0;
2882 	u32	num_of_wifi_link = 0;
2883 	boolean	bt_ctrl_agg_buf_size = false;
2884 	u8	agg_buf_size = 5;
2885 
2886 	if (btcoexist->manual_control ||
2887 	    btcoexist->stop_coex_dm ||
2888 	    coex_sta->bt_disabled)
2889 		return;
2890 
2891 	if (BTC_ASSOCIATE_START == type) {
2892 		coex_sta->wifi_is_high_pri_task = true;
2893 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2894 			    "[BTCoex], CONNECT START notify\n");
2895 		BTC_TRACE(trace_buf);
2896 		coex_dm->arp_cnt = 0;
2897 	} else {
2898 		coex_sta->wifi_is_high_pri_task = false;
2899 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2900 			    "[BTCoex], CONNECT FINISH notify\n");
2901 		BTC_TRACE(trace_buf);
2902 		/* coex_dm->arp_cnt = 0; */
2903 	}
2904 
2905 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2906 			   &wifi_link_status);
2907 	num_of_wifi_link = wifi_link_status >> 16;
2908 	if (num_of_wifi_link >= 2) {
2909 		halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2910 		halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2911 					   bt_ctrl_agg_buf_size, agg_buf_size);
2912 		halbtc8192e1ant_action_wifi_multi_port(btcoexist);
2913 		return;
2914 	}
2915 
2916 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2917 	if (coex_sta->c2h_bt_inquiry_page) {
2918 		halbtc8192e1ant_action_bt_inquiry(btcoexist);
2919 		return;
2920 	} else if (bt_hs_on) {
2921 		halbtc8192e1ant_action_hs(btcoexist);
2922 		return;
2923 	}
2924 
2925 	if (BTC_ASSOCIATE_START == type)
2926 		halbtc8192e1ant_action_wifi_not_connected_asso_auth(btcoexist);
2927 	else if (BTC_ASSOCIATE_FINISH == type) {
2928 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2929 				   &wifi_connected);
2930 		if (!wifi_connected) /* non-connected scan */
2931 			halbtc8192e1ant_action_wifi_not_connected(btcoexist);
2932 		else
2933 			halbtc8192e1ant_action_wifi_connected(btcoexist);
2934 	}
2935 }
2936 
ex_halbtc8192e1ant_media_status_notify(IN struct btc_coexist * btcoexist,IN u8 type)2937 void ex_halbtc8192e1ant_media_status_notify(IN struct btc_coexist *btcoexist,
2938 		IN u8 type)
2939 {
2940 	u8			h2c_parameter[3] = {0};
2941 	u32			wifi_bw;
2942 	u8			wifi_central_chnl;
2943 	boolean			wifi_under_b_mode = false;
2944 
2945 	if (btcoexist->manual_control ||
2946 	    btcoexist->stop_coex_dm ||
2947 	    coex_sta->bt_disabled)
2948 		return;
2949 
2950 	if (BTC_MEDIA_CONNECT == type) {
2951 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2952 			    "[BTCoex], MEDIA connect notify\n");
2953 		BTC_TRACE(trace_buf);
2954 
2955 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
2956 				   &wifi_under_b_mode);
2957 
2958 		/* Set CCK Tx/Rx high Pri except 11b mode */
2959 		if (wifi_under_b_mode) {
2960 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
2961 						   0x00); /* CCK Tx */
2962 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
2963 						   0x00); /* CCK Rx */
2964 		} else {
2965 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
2966 						   0x10); /* CCK Tx */
2967 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
2968 						   0x10); /* CCK Rx */
2969 		}
2970 
2971 		coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2972 					    0x430);
2973 		coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2974 					    0x434);
2975 		coex_dm->backup_retry_limit = btcoexist->btc_read_2byte(
2976 						      btcoexist, 0x42a);
2977 		coex_dm->backup_ampdu_max_time = btcoexist->btc_read_1byte(
2978 				btcoexist, 0x456);
2979 	} else {
2980 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2981 			    "[BTCoex], MEDIA disconnect notify\n");
2982 		BTC_TRACE(trace_buf);
2983 		coex_dm->arp_cnt = 0;
2984 
2985 		btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x0); /* CCK Tx */
2986 		btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x0); /* CCK Rx */
2987 	}
2988 
2989 	/* only 2.4G we need to inform bt the chnl mask */
2990 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL,
2991 			   &wifi_central_chnl);
2992 	if ((BTC_MEDIA_CONNECT == type) &&
2993 	    (wifi_central_chnl <= 14)) {
2994 		/* h2c_parameter[0] = 0x1; */
2995 		h2c_parameter[0] = 0x0;
2996 		h2c_parameter[1] = wifi_central_chnl;
2997 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2998 		if (BTC_WIFI_BW_HT40 == wifi_bw)
2999 			h2c_parameter[2] = 0x30;
3000 		else
3001 			h2c_parameter[2] = 0x20;
3002 	}
3003 
3004 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
3005 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
3006 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
3007 
3008 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
3009 }
3010 
ex_halbtc8192e1ant_specific_packet_notify(IN struct btc_coexist * btcoexist,IN u8 type)3011 void ex_halbtc8192e1ant_specific_packet_notify(IN struct btc_coexist *btcoexist,
3012 		IN u8 type)
3013 {
3014 	boolean	bt_hs_on = false;
3015 	u32	wifi_link_status = 0;
3016 	u32	num_of_wifi_link = 0;
3017 	boolean	bt_ctrl_agg_buf_size = false;
3018 	u8	agg_buf_size = 5;
3019 
3020 	if (btcoexist->manual_control ||
3021 	    btcoexist->stop_coex_dm ||
3022 	    coex_sta->bt_disabled)
3023 		return;
3024 
3025 	if (BTC_PACKET_DHCP == type ||
3026 	    BTC_PACKET_EAPOL == type ||
3027 	    BTC_PACKET_ARP == type) {
3028 		if (BTC_PACKET_ARP == type) {
3029 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3030 				    "[BTCoex], specific Packet ARP notify\n");
3031 			BTC_TRACE(trace_buf);
3032 
3033 			coex_dm->arp_cnt++;
3034 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3035 				    "[BTCoex], ARP Packet Count = %d\n",
3036 				    coex_dm->arp_cnt);
3037 			BTC_TRACE(trace_buf);
3038 
3039 			if (coex_dm->arp_cnt >=
3040 			    10) /* if APR PKT > 10 after connect, do not go to ActionWifiConnectedSpecificPacket(btcoexist) */
3041 				coex_sta->wifi_is_high_pri_task = false;
3042 			else
3043 				coex_sta->wifi_is_high_pri_task = true;
3044 		} else {
3045 			coex_sta->wifi_is_high_pri_task = true;
3046 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3047 				"[BTCoex], specific Packet DHCP or EAPOL notify\n");
3048 			BTC_TRACE(trace_buf);
3049 		}
3050 	} else {
3051 		coex_sta->wifi_is_high_pri_task = false;
3052 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3053 			"[BTCoex], specific Packet [Type = %d] notify\n", type);
3054 		BTC_TRACE(trace_buf);
3055 	}
3056 
3057 	coex_sta->specific_pkt_period_cnt = 0;
3058 
3059 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3060 			   &wifi_link_status);
3061 	num_of_wifi_link = wifi_link_status >> 16;
3062 	if (num_of_wifi_link >= 2) {
3063 		halbtc8192e1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
3064 		halbtc8192e1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
3065 					   bt_ctrl_agg_buf_size, agg_buf_size);
3066 		halbtc8192e1ant_action_wifi_multi_port(btcoexist);
3067 		return;
3068 	}
3069 
3070 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3071 	if (coex_sta->c2h_bt_inquiry_page) {
3072 		halbtc8192e1ant_action_bt_inquiry(btcoexist);
3073 		return;
3074 	} else if (bt_hs_on) {
3075 		halbtc8192e1ant_action_hs(btcoexist);
3076 		return;
3077 	}
3078 
3079 	if (BTC_PACKET_DHCP == type ||
3080 	    BTC_PACKET_EAPOL == type ||
3081 	    ((BTC_PACKET_ARP == type) && (coex_sta->wifi_is_high_pri_task)))
3082 		halbtc8192e1ant_action_wifi_connected_specific_packet(btcoexist);
3083 }
3084 
ex_halbtc8192e1ant_bt_info_notify(IN struct btc_coexist * btcoexist,IN u8 * tmp_buf,IN u8 length)3085 void ex_halbtc8192e1ant_bt_info_notify(IN struct btc_coexist *btcoexist,
3086 				       IN u8 *tmp_buf, IN u8 length)
3087 {
3088 	u8				bt_info = 0;
3089 	u8				i, rsp_source = 0;
3090 	boolean				wifi_connected = false;
3091 	boolean				bt_busy = false;
3092 
3093 	coex_sta->c2h_bt_info_req_sent = false;
3094 
3095 	rsp_source = tmp_buf[0] & 0xf;
3096 	if (rsp_source >= BT_INFO_SRC_8192E_1ANT_MAX)
3097 		rsp_source = BT_INFO_SRC_8192E_1ANT_WIFI_FW;
3098 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
3099 
3100 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3101 		    "[BTCoex], Bt info[%d], length=%d, hex data=[", rsp_source,
3102 		    length);
3103 	BTC_TRACE(trace_buf);
3104 	for (i = 0; i < length; i++) {
3105 		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
3106 		if (i == 1)
3107 			bt_info = tmp_buf[i];
3108 		if (i == length - 1) {
3109 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x]\n",
3110 				    tmp_buf[i]);
3111 			BTC_TRACE(trace_buf);
3112 		} else {
3113 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x, ",
3114 				    tmp_buf[i]);
3115 			BTC_TRACE(trace_buf);
3116 		}
3117 	}
3118 
3119 	if (BT_INFO_SRC_8192E_1ANT_WIFI_FW != rsp_source) {
3120 		coex_sta->bt_retry_cnt =	/* [3:0] */
3121 			coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
3122 
3123 		if (coex_sta->bt_retry_cnt >= 1)
3124 			coex_sta->pop_event_cnt++;
3125 
3126 		if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
3127 			coex_sta->c2h_bt_page = true;
3128 		else
3129 			coex_sta->c2h_bt_page = false;
3130 
3131 		coex_sta->bt_rssi =
3132 			coex_sta->bt_info_c2h[rsp_source][3] * 2 - 90;
3133 		/* coex_sta->bt_info_c2h[rsp_source][3]*2+10; */
3134 
3135 		coex_sta->bt_info_ext =
3136 			coex_sta->bt_info_c2h[rsp_source][4];
3137 
3138 		coex_sta->bt_tx_rx_mask = (coex_sta->bt_info_c2h[rsp_source][2]
3139 					   & 0x40);
3140 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
3141 				   &coex_sta->bt_tx_rx_mask);
3142 		if (!coex_sta->bt_tx_rx_mask) {
3143 			/* BT into is responded by BT FW and BT RF REG 0x3C != 0x15 => Need to switch BT TRx Mask */
3144 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3145 				"[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
3146 			BTC_TRACE(trace_buf);
3147 			btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3148 						  0x3c, 0x15);
3149 		}
3150 
3151 		/* Here we need to resend some wifi info to BT */
3152 		/* because bt is reset and loss of the info. */
3153 		if (coex_sta->bt_info_ext & BIT(1)) {
3154 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3155 				"[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
3156 			BTC_TRACE(trace_buf);
3157 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3158 					   &wifi_connected);
3159 			if (wifi_connected)
3160 				ex_halbtc8192e1ant_media_status_notify(
3161 					btcoexist, BTC_MEDIA_CONNECT);
3162 			else
3163 				ex_halbtc8192e1ant_media_status_notify(
3164 					btcoexist, BTC_MEDIA_DISCONNECT);
3165 		}
3166 
3167 		if (coex_sta->bt_info_ext & BIT(3)) {
3168 			if (!btcoexist->manual_control &&
3169 			    !btcoexist->stop_coex_dm) {
3170 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3171 					"[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
3172 				BTC_TRACE(trace_buf);
3173 				halbtc8192e1ant_ignore_wlan_act(btcoexist,
3174 							FORCE_EXEC, false);
3175 			}
3176 		} else {
3177 			/* BT already NOT ignore Wlan active, do nothing here. */
3178 		}
3179 #if (BT_AUTO_REPORT_ONLY_8192E_1ANT == 0)
3180 		if ((coex_sta->bt_info_ext & BIT(4))) {
3181 			/* BT auto report already enabled, do nothing */
3182 		} else
3183 			halbtc8192e1ant_bt_auto_report(btcoexist, FORCE_EXEC,
3184 						       true);
3185 #endif
3186 	}
3187 
3188 	/* check BIT2 first ==> check if bt is under inquiry or page scan */
3189 	if (bt_info & BT_INFO_8192E_1ANT_B_INQ_PAGE)
3190 		coex_sta->c2h_bt_inquiry_page = true;
3191 	else
3192 		coex_sta->c2h_bt_inquiry_page = false;
3193 
3194 	/* set link exist status */
3195 	if (!(bt_info & BT_INFO_8192E_1ANT_B_CONNECTION)) {
3196 		coex_sta->bt_link_exist = false;
3197 		coex_sta->pan_exist = false;
3198 		coex_sta->a2dp_exist = false;
3199 		coex_sta->hid_exist = false;
3200 		coex_sta->sco_exist = false;
3201 	} else {	/* connection exists */
3202 		coex_sta->bt_link_exist = true;
3203 		if (bt_info & BT_INFO_8192E_1ANT_B_FTP)
3204 			coex_sta->pan_exist = true;
3205 		else
3206 			coex_sta->pan_exist = false;
3207 		if (bt_info & BT_INFO_8192E_1ANT_B_A2DP)
3208 			coex_sta->a2dp_exist = true;
3209 		else
3210 			coex_sta->a2dp_exist = false;
3211 		if (bt_info & BT_INFO_8192E_1ANT_B_HID)
3212 			coex_sta->hid_exist = true;
3213 		else
3214 			coex_sta->hid_exist = false;
3215 		if (bt_info & BT_INFO_8192E_1ANT_B_SCO_ESCO)
3216 			coex_sta->sco_exist = true;
3217 		else
3218 			coex_sta->sco_exist = false;
3219 	}
3220 
3221 	halbtc8192e1ant_update_bt_link_info(btcoexist);
3222 
3223 	bt_info = bt_info &
3224 		0x1f;  /* mask profile bit for connect-ilde identification ( for CSR case: A2DP idle --> 0x41) */
3225 
3226 	if (!(bt_info & BT_INFO_8192E_1ANT_B_CONNECTION)) {
3227 		coex_dm->bt_status = BT_8192E_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
3228 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3229 			"[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
3230 		BTC_TRACE(trace_buf);
3231 	} else if (bt_info ==
3232 		BT_INFO_8192E_1ANT_B_CONNECTION) {	/* connection exists but no busy */
3233 		coex_dm->bt_status = BT_8192E_1ANT_BT_STATUS_CONNECTED_IDLE;
3234 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3235 			    "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
3236 		BTC_TRACE(trace_buf);
3237 	} else if ((bt_info & BT_INFO_8192E_1ANT_B_SCO_ESCO) ||
3238 		   (bt_info & BT_INFO_8192E_1ANT_B_SCO_BUSY)) {
3239 		coex_dm->bt_status = BT_8192E_1ANT_BT_STATUS_SCO_BUSY;
3240 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3241 			    "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
3242 		BTC_TRACE(trace_buf);
3243 	} else if (bt_info & BT_INFO_8192E_1ANT_B_ACL_BUSY) {
3244 		if (BT_8192E_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
3245 			coex_dm->auto_tdma_adjust = false;
3246 		coex_dm->bt_status = BT_8192E_1ANT_BT_STATUS_ACL_BUSY;
3247 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3248 			    "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
3249 		BTC_TRACE(trace_buf);
3250 	} else {
3251 		coex_dm->bt_status = BT_8192E_1ANT_BT_STATUS_MAX;
3252 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3253 			"[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
3254 		BTC_TRACE(trace_buf);
3255 	}
3256 
3257 	if ((BT_8192E_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
3258 	    (BT_8192E_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
3259 	    (BT_8192E_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
3260 		bt_busy = true;
3261 	else
3262 		bt_busy = false;
3263 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
3264 
3265 	halbtc8192e1ant_run_coexist_mechanism(btcoexist);
3266 }
3267 
ex_halbtc8192e1ant_rf_status_notify(IN struct btc_coexist * btcoexist,IN u8 type)3268 void ex_halbtc8192e1ant_rf_status_notify(IN struct btc_coexist *btcoexist,
3269 		IN u8 type)
3270 {
3271 	u32	u32tmp;
3272 	u8	u8tmpa, u8tmpb, u8tmpc;
3273 
3274 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], RF Status notify\n");
3275 	BTC_TRACE(trace_buf);
3276 
3277 	if (BTC_RF_ON == type) {
3278 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3279 			    "[BTCoex], RF is turned ON!!\n");
3280 		BTC_TRACE(trace_buf);
3281 		btcoexist->stop_coex_dm = false;
3282 	} else if (BTC_RF_OFF == type) {
3283 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3284 			    "[BTCoex], RF is turned OFF!!\n");
3285 		BTC_TRACE(trace_buf);
3286 
3287 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3288 						 0x0, 0x0);
3289 		halbtc8192e1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3290 		halbtc8192e1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
3291 					     true);
3292 
3293 		halbtc8192e1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3294 		btcoexist->stop_coex_dm = true;
3295 
3296 		u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x948);
3297 		u8tmpa = btcoexist->btc_read_1byte(btcoexist, 0x765);
3298 		u8tmpb = btcoexist->btc_read_1byte(btcoexist, 0x67);
3299 		u8tmpc = btcoexist->btc_read_1byte(btcoexist, 0x76e);
3300 
3301 
3302 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3303 			"############# [BTCoex], 0x948=0x%x, 0x765=0x%x, 0x67=0x%x, 0x76e=0x%x\n",
3304 			    u32tmp,  u8tmpa, u8tmpb, u8tmpc);
3305 		BTC_TRACE(trace_buf);
3306 
3307 	}
3308 }
3309 
ex_halbtc8192e1ant_halt_notify(IN struct btc_coexist * btcoexist)3310 void ex_halbtc8192e1ant_halt_notify(IN struct btc_coexist *btcoexist)
3311 {
3312 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Halt notify\n");
3313 	BTC_TRACE(trace_buf);
3314 
3315 	halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
3316 					 0x0);
3317 	halbtc8192e1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3318 	halbtc8192e1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false, true);
3319 
3320 	halbtc8192e1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3321 
3322 	ex_halbtc8192e1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
3323 
3324 	btcoexist->stop_coex_dm = true;
3325 }
3326 
ex_halbtc8192e1ant_pnp_notify(IN struct btc_coexist * btcoexist,IN u8 pnp_state)3327 void ex_halbtc8192e1ant_pnp_notify(IN struct btc_coexist *btcoexist,
3328 				   IN u8 pnp_state)
3329 {
3330 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Pnp notify\n");
3331 	BTC_TRACE(trace_buf);
3332 
3333 	if (BTC_WIFI_PNP_SLEEP == pnp_state) {
3334 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3335 			    "[BTCoex], Pnp notify to SLEEP\n");
3336 		BTC_TRACE(trace_buf);
3337 
3338 		halbtc8192e1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
3339 						 0x0, 0x0);
3340 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
3341 		halbtc8192e1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
3342 		halbtc8192e1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
3343 					     true);
3344 
3345 		/* Sinda 20150819, workaround for driver skip leave IPS/LPS to speed up sleep time. */
3346 		/* Driver do not leave IPS/LPS when driver is going to sleep, so BTCoexistence think wifi is still under IPS/LPS */
3347 		/* BT should clear UnderIPS/UnderLPS state to avoid mismatch state after wakeup. */
3348 		coex_sta->under_ips = false;
3349 		coex_sta->under_lps = false;
3350 		btcoexist->stop_coex_dm = true;
3351 	} else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
3352 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3353 			    "[BTCoex], Pnp notify to WAKE UP\n");
3354 		BTC_TRACE(trace_buf);
3355 		btcoexist->stop_coex_dm = false;
3356 		halbtc8192e1ant_init_hw_config(btcoexist, false);
3357 		halbtc8192e1ant_init_coex_dm(btcoexist);
3358 		halbtc8192e1ant_query_bt_info(btcoexist);
3359 	}
3360 }
3361 
ex_halbtc8192e1ant_coex_dm_reset(IN struct btc_coexist * btcoexist)3362 void ex_halbtc8192e1ant_coex_dm_reset(IN struct btc_coexist *btcoexist)
3363 {
3364 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3365 		"[BTCoex], *****************Coex DM Reset*****************\n");
3366 	BTC_TRACE(trace_buf);
3367 
3368 	halbtc8192e1ant_init_hw_config(btcoexist, false);
3369 	/* btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0xfffff, 0x0); */
3370 	/* btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x2, 0xfffff, 0x0); */
3371 	halbtc8192e1ant_init_coex_dm(btcoexist);
3372 }
3373 
ex_halbtc8192e1ant_periodical(IN struct btc_coexist * btcoexist)3374 void ex_halbtc8192e1ant_periodical(IN struct btc_coexist *btcoexist)
3375 {
3376 #if (BT_AUTO_REPORT_ONLY_8192E_1ANT == 0)
3377 	halbtc8192e1ant_query_bt_info(btcoexist);
3378 	halbtc8192e1ant_monitor_bt_enable_disable(btcoexist);
3379 #else
3380 	halbtc8192e1ant_monitor_bt_ctr(btcoexist);
3381 	halbtc8192e1ant_monitor_wifi_ctr(btcoexist);
3382 
3383 	if (halbtc8192e1ant_is_wifi_status_changed(btcoexist) ||
3384 	    coex_dm->auto_tdma_adjust)
3385 
3386 		halbtc8192e1ant_run_coexist_mechanism(btcoexist);
3387 
3388 	coex_sta->specific_pkt_period_cnt++;
3389 #endif
3390 }
3391 
3392 
ex_halbtc8192e1ant_dbg_control(IN struct btc_coexist * btcoexist,IN u8 op_code,IN u8 op_len,IN u8 * pdata)3393 void ex_halbtc8192e1ant_dbg_control(IN struct btc_coexist *btcoexist,
3394 				    IN u8 op_code, IN u8 op_len, IN u8 *pdata)
3395 {
3396 	switch (op_code) {
3397 	case BTC_DBG_SET_COEX_NORMAL:
3398 		btcoexist->manual_control = false;
3399 		halbtc8192e1ant_init_coex_dm(btcoexist);
3400 		break;
3401 	case BTC_DBG_SET_COEX_WIFI_ONLY:
3402 		btcoexist->manual_control = true;
3403 		halbtc8192e1ant_power_save_state(btcoexist,
3404 						 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
3405 		halbtc8192e1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
3406 					9);
3407 		break;
3408 	case BTC_DBG_SET_COEX_BT_ONLY:
3409 		/* todo */
3410 		break;
3411 	default:
3412 		break;
3413 	}
3414 }
3415 
3416 #endif	/*  #if (RTL8192E_SUPPORT == 1) */
3417 
3418 #endif	/* #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1) */