OSDN Git Service

Add rtl8821ce driver version 5.5.2
[android-x86/external-kernel-drivers.git] / rtl8821ce / hal / btc / halbtc8821a1ant.c
1 /* ************************************************************
2  * Description:
3  *
4  * This file is for 8821A_1ANT Co-exist mechanism
5  *
6  * History
7  * 2012/11/15 Cosa first check in.
8  *
9  * ************************************************************
10  * SY modify 2015/04/27
11  * ************************************************************
12  * include files
13  * ************************************************************ */
14 #include "mp_precomp.h"
15
16 #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1)
17
18 #if (RTL8821A_SUPPORT == 1)
19 /* ************************************************************
20  * Global variables, these are static variables
21  * ************************************************************ */
22 static u8        *trace_buf = &gl_btc_trace_buf[0];
23 static struct  coex_dm_8821a_1ant               glcoex_dm_8821a_1ant;
24 static struct  coex_dm_8821a_1ant       *coex_dm = &glcoex_dm_8821a_1ant;
25 static struct  coex_sta_8821a_1ant              glcoex_sta_8821a_1ant;
26 static struct  coex_sta_8821a_1ant      *coex_sta = &glcoex_sta_8821a_1ant;
27
28 const char *const glbt_info_src_8821a_1ant[] = {
29         "BT Info[wifi fw]",
30         "BT Info[bt rsp]",
31         "BT Info[bt auto report]",
32 };
33
34 u32     glcoex_ver_date_8821a_1ant = 20161128;
35 u32     glcoex_ver_8821a_1ant = 0x64;
36 u32     glcoex_ver_btdesired_8821a_1ant = 0x62;
37
38 /* ************************************************************
39  * local function proto type if needed
40  * ************************************************************
41  * ************************************************************
42  * local function start with halbtc8821a1ant_
43  * ************************************************************ */
44 u8 halbtc8821a1ant_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_8821A_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_8821A_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_8821A_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
106 u8 halbtc8821a1ant_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_8821A_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_8821A_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_8821A_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
171 void halbtc8821a1ant_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
182 void halbtc8821a1ant_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
221 void halbtc8821a1ant_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
246 void halbtc8821a1ant_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
270 void halbtc8821a1ant_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                 halbtc8821a1ant_update_ra_mask(btcoexist, force_exec,
277                                                0x0);
278                 break;
279         case 1: /* disable cck 1/2 */
280                 halbtc8821a1ant_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                 halbtc8821a1ant_update_ra_mask(btcoexist, force_exec,
285                                                0x0001f1f7);
286                 break;
287         default:
288                 break;
289         }
290
291         halbtc8821a1ant_auto_rate_fallback_retry(btcoexist, force_exec,
292                         arfr_type);
293         halbtc8821a1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
294         halbtc8821a1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
295 }
296
297
298 /* true/xxxx/x:1
299  * false/false/x: 64
300  * false/true/x:x */
301 void halbtc8821a1ant_limited_rx(IN struct btc_coexist *btcoexist,
302                         IN boolean force_exec, IN boolean rej_ap_agg_pkt,
303                         IN boolean bt_ctrl_agg_buf_size, IN u8 agg_buf_size)
304 {
305         boolean reject_rx_agg = rej_ap_agg_pkt;
306         boolean bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
307         u8      rx_agg_size = agg_buf_size;
308
309         /* ============================================ */
310         /*      Rx Aggregation related setting */
311         /* ============================================ */
312         btcoexist->btc_set(btcoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT,
313                            &reject_rx_agg);
314         /* decide BT control aggregation buf size or not */
315         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
316                            &bt_ctrl_rx_agg_size);
317         /* aggregation buf size, only work when BT control Rx aggregation size. */
318         btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
319         /* real update aggregation setting */
320         btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
321
322
323 }
324
325 void halbtc8821a1ant_monitor_bt_ctr(IN struct btc_coexist *btcoexist)
326 {
327         u32                     reg_hp_txrx, reg_lp_txrx, u32tmp;
328         u32                     reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
329 #if 0
330         /* to avoid 0x76e[3] = 1 (WLAN_Act control by PTA) during IPS */
331         if (!(btcoexist->btc_read_1byte(btcoexist, 0x76e) & 0x8)) {
332                 coex_sta->high_priority_tx = 65535;
333                 coex_sta->high_priority_rx = 65535;
334                 coex_sta->low_priority_tx = 65535;
335                 coex_sta->low_priority_rx = 65535;
336                 return;
337         }
338 #endif
339         reg_hp_txrx = 0x770;
340         reg_lp_txrx = 0x774;
341
342         u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
343         reg_hp_tx = u32tmp & MASKLWORD;
344         reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
345
346         u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
347         reg_lp_tx = u32tmp & MASKLWORD;
348         reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
349
350         coex_sta->high_priority_tx = reg_hp_tx;
351         coex_sta->high_priority_rx = reg_hp_rx;
352         coex_sta->low_priority_tx = reg_lp_tx;
353         coex_sta->low_priority_rx = reg_lp_rx;
354
355         /* reset counter */
356         btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
357 }
358
359 void halbtc8821a1ant_monitor_wifi_ctr(IN struct btc_coexist *btcoexist)
360 {
361 #if 1
362
363         coex_sta->crc_ok_cck = btcoexist->btc_phydm_query_PHY_counter(
364                                        btcoexist,
365                                        PHYDM_INFO_CRC32_OK_CCK);
366         coex_sta->crc_ok_11g = btcoexist->btc_phydm_query_PHY_counter(
367                                        btcoexist,
368                                        PHYDM_INFO_CRC32_OK_LEGACY);
369         coex_sta->crc_ok_11n = btcoexist->btc_phydm_query_PHY_counter(
370                                        btcoexist,
371                                        PHYDM_INFO_CRC32_OK_HT);
372         coex_sta->crc_ok_11n_vht =
373                 btcoexist->btc_phydm_query_PHY_counter(
374                         btcoexist,
375                         PHYDM_INFO_CRC32_OK_VHT);
376
377         coex_sta->crc_err_cck = btcoexist->btc_phydm_query_PHY_counter(
378                                         btcoexist,
379                                         PHYDM_INFO_CRC32_ERROR_CCK);
380         coex_sta->crc_err_11g =  btcoexist->btc_phydm_query_PHY_counter(
381                                          btcoexist,
382                                          PHYDM_INFO_CRC32_ERROR_LEGACY);
383         coex_sta->crc_err_11n = btcoexist->btc_phydm_query_PHY_counter(
384                                         btcoexist,
385                                         PHYDM_INFO_CRC32_ERROR_HT);
386         coex_sta->crc_err_11n_vht =
387                 btcoexist->btc_phydm_query_PHY_counter(
388                         btcoexist,
389                         PHYDM_INFO_CRC32_ERROR_VHT);
390
391 #endif
392 }
393
394
395 void halbtc8821a1ant_query_bt_info(IN struct btc_coexist *btcoexist)
396 {
397         u8                      h2c_parameter[1] = {0};
398
399         coex_sta->c2h_bt_info_req_sent = true;
400
401         h2c_parameter[0] |= BIT(0);     /* trigger */
402
403         btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
404 }
405
406 boolean halbtc8821a1ant_is_wifi_status_changed(IN struct btc_coexist *btcoexist)
407 {
408         static boolean  pre_wifi_busy = false, pre_under_4way = false,
409                         pre_bt_hs_on = false;
410         boolean wifi_busy = false, under_4way = false, bt_hs_on = false;
411         boolean wifi_connected = false;
412
413         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
414                            &wifi_connected);
415         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
416         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
417         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
418                            &under_4way);
419
420         if (wifi_connected) {
421                 if (wifi_busy != pre_wifi_busy) {
422                         pre_wifi_busy = wifi_busy;
423                         return true;
424                 }
425                 if (under_4way != pre_under_4way) {
426                         pre_under_4way = under_4way;
427                         return true;
428                 }
429                 if (bt_hs_on != pre_bt_hs_on) {
430                         pre_bt_hs_on = bt_hs_on;
431                         return true;
432                 }
433         }
434
435         return false;
436 }
437
438 void halbtc8821a1ant_update_bt_link_info(IN struct btc_coexist *btcoexist)
439 {
440         struct  btc_bt_link_info        *bt_link_info = &btcoexist->bt_link_info;
441         boolean                         bt_hs_on = false;
442
443         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
444
445         bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
446         bt_link_info->sco_exist = coex_sta->sco_exist;
447         bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
448         bt_link_info->pan_exist = coex_sta->pan_exist;
449         bt_link_info->hid_exist = coex_sta->hid_exist;
450
451         /* work around for HS mode. */
452         if (bt_hs_on) {
453                 bt_link_info->pan_exist = true;
454                 bt_link_info->bt_link_exist = true;
455         }
456
457         /* check if Sco only */
458         if (bt_link_info->sco_exist &&
459             !bt_link_info->a2dp_exist &&
460             !bt_link_info->pan_exist &&
461             !bt_link_info->hid_exist)
462                 bt_link_info->sco_only = true;
463         else
464                 bt_link_info->sco_only = false;
465
466         /* check if A2dp only */
467         if (!bt_link_info->sco_exist &&
468             bt_link_info->a2dp_exist &&
469             !bt_link_info->pan_exist &&
470             !bt_link_info->hid_exist)
471                 bt_link_info->a2dp_only = true;
472         else
473                 bt_link_info->a2dp_only = false;
474
475         /* check if Pan only */
476         if (!bt_link_info->sco_exist &&
477             !bt_link_info->a2dp_exist &&
478             bt_link_info->pan_exist &&
479             !bt_link_info->hid_exist)
480                 bt_link_info->pan_only = true;
481         else
482                 bt_link_info->pan_only = false;
483
484         /* check if Hid only */
485         if (!bt_link_info->sco_exist &&
486             !bt_link_info->a2dp_exist &&
487             !bt_link_info->pan_exist &&
488             bt_link_info->hid_exist)
489                 bt_link_info->hid_only = true;
490         else
491                 bt_link_info->hid_only = false;
492 }
493
494 u8 halbtc8821a1ant_action_algorithm(IN struct btc_coexist *btcoexist)
495 {
496         struct  btc_bt_link_info        *bt_link_info = &btcoexist->bt_link_info;
497         boolean                         bt_hs_on = false;
498         u8                              algorithm = BT_8821A_1ANT_COEX_ALGO_UNDEFINED;
499         u8                              num_of_diff_profile = 0;
500
501         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
502
503         if (!bt_link_info->bt_link_exist) {
504                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
505                             "[BTCoex], No BT link exists!!!\n");
506                 BTC_TRACE(trace_buf);
507                 return algorithm;
508         }
509
510         if (bt_link_info->sco_exist)
511                 num_of_diff_profile++;
512         if (bt_link_info->hid_exist)
513                 num_of_diff_profile++;
514         if (bt_link_info->pan_exist)
515                 num_of_diff_profile++;
516         if (bt_link_info->a2dp_exist)
517                 num_of_diff_profile++;
518
519         if (num_of_diff_profile == 1) {
520                 if (bt_link_info->sco_exist) {
521                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
522                                     "[BTCoex], BT Profile = SCO only\n");
523                         BTC_TRACE(trace_buf);
524                         algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
525                 } else {
526                         if (bt_link_info->hid_exist) {
527                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
528                                         "[BTCoex], BT Profile = HID only\n");
529                                 BTC_TRACE(trace_buf);
530                                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
531                         } else if (bt_link_info->a2dp_exist) {
532                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
533                                         "[BTCoex], BT Profile = A2DP only\n");
534                                 BTC_TRACE(trace_buf);
535                                 algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP;
536                         } else if (bt_link_info->pan_exist) {
537                                 if (bt_hs_on) {
538                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
539                                                 "[BTCoex], BT Profile = PAN(HS) only\n");
540                                         BTC_TRACE(trace_buf);
541                                         algorithm =
542                                                 BT_8821A_1ANT_COEX_ALGO_PANHS;
543                                 } else {
544                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
545                                                 "[BTCoex], BT Profile = PAN(EDR) only\n");
546                                         BTC_TRACE(trace_buf);
547                                         algorithm =
548                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR;
549                                 }
550                         }
551                 }
552         } else if (num_of_diff_profile == 2) {
553                 if (bt_link_info->sco_exist) {
554                         if (bt_link_info->hid_exist) {
555                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
556                                         "[BTCoex], BT Profile = SCO + HID\n");
557                                 BTC_TRACE(trace_buf);
558                                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
559                         } else if (bt_link_info->a2dp_exist) {
560                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
561                                         "[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
562                                 BTC_TRACE(trace_buf);
563                                 algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
564                         } else if (bt_link_info->pan_exist) {
565                                 if (bt_hs_on) {
566                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
567                                                 "[BTCoex], BT Profile = SCO + PAN(HS)\n");
568                                         BTC_TRACE(trace_buf);
569                                         algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
570                                 } else {
571                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
572                                                 "[BTCoex], BT Profile = SCO + PAN(EDR)\n");
573                                         BTC_TRACE(trace_buf);
574                                         algorithm =
575                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
576                                 }
577                         }
578                 } else {
579                         if (bt_link_info->hid_exist &&
580                             bt_link_info->a2dp_exist) {
581                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
582                                         "[BTCoex], BT Profile = HID + A2DP\n");
583                                 BTC_TRACE(trace_buf);
584                                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
585                         } else if (bt_link_info->hid_exist &&
586                                    bt_link_info->pan_exist) {
587                                 if (bt_hs_on) {
588                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
589                                                 "[BTCoex], BT Profile = HID + PAN(HS)\n");
590                                         BTC_TRACE(trace_buf);
591                                         algorithm =
592                                                 BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
593                                 } else {
594                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
595                                                 "[BTCoex], BT Profile = HID + PAN(EDR)\n");
596                                         BTC_TRACE(trace_buf);
597                                         algorithm =
598                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
599                                 }
600                         } else if (bt_link_info->pan_exist &&
601                                    bt_link_info->a2dp_exist) {
602                                 if (bt_hs_on) {
603                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
604                                                 "[BTCoex], BT Profile = A2DP + PAN(HS)\n");
605                                         BTC_TRACE(trace_buf);
606                                         algorithm =
607                                                 BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS;
608                                 } else {
609                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
610                                                 "[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
611                                         BTC_TRACE(trace_buf);
612                                         algorithm =
613                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP;
614                                 }
615                         }
616                 }
617         } else if (num_of_diff_profile == 3) {
618                 if (bt_link_info->sco_exist) {
619                         if (bt_link_info->hid_exist &&
620                             bt_link_info->a2dp_exist) {
621                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
622                                         "[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
623                                 BTC_TRACE(trace_buf);
624                                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
625                         } else if (bt_link_info->hid_exist &&
626                                    bt_link_info->pan_exist) {
627                                 if (bt_hs_on) {
628                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
629                                                 "[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
630                                         BTC_TRACE(trace_buf);
631                                         algorithm =
632                                                 BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
633                                 } else {
634                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
635                                                 "[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
636                                         BTC_TRACE(trace_buf);
637                                         algorithm =
638                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
639                                 }
640                         } else if (bt_link_info->pan_exist &&
641                                    bt_link_info->a2dp_exist) {
642                                 if (bt_hs_on) {
643                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
644                                                 "[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
645                                         BTC_TRACE(trace_buf);
646                                         algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
647                                 } else {
648                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
649                                                 "[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
650                                         BTC_TRACE(trace_buf);
651                                         algorithm =
652                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
653                                 }
654                         }
655                 } else {
656                         if (bt_link_info->hid_exist &&
657                             bt_link_info->pan_exist &&
658                             bt_link_info->a2dp_exist) {
659                                 if (bt_hs_on) {
660                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
661                                                 "[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
662                                         BTC_TRACE(trace_buf);
663                                         algorithm =
664                                                 BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
665                                 } else {
666                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
667                                                 "[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
668                                         BTC_TRACE(trace_buf);
669                                         algorithm =
670                                                 BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
671                                 }
672                         }
673                 }
674         } else if (num_of_diff_profile >= 3) {
675                 if (bt_link_info->sco_exist) {
676                         if (bt_link_info->hid_exist &&
677                             bt_link_info->pan_exist &&
678                             bt_link_info->a2dp_exist) {
679                                 if (bt_hs_on) {
680                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
681                                                 "[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
682                                         BTC_TRACE(trace_buf);
683
684                                 } else {
685                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
686                                                 "[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
687                                         BTC_TRACE(trace_buf);
688                                         algorithm =
689                                                 BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
690                                 }
691                         }
692                 }
693         }
694
695         return algorithm;
696 }
697
698 void halbtc8821a1ant_set_bt_auto_report(IN struct btc_coexist *btcoexist,
699                                         IN boolean enable_auto_report)
700 {
701         u8                      h2c_parameter[1] = {0};
702
703         h2c_parameter[0] = 0;
704
705         if (enable_auto_report)
706                 h2c_parameter[0] |= BIT(0);
707
708         btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
709 }
710
711 void halbtc8821a1ant_bt_auto_report(IN struct btc_coexist *btcoexist,
712                     IN boolean force_exec, IN boolean enable_auto_report)
713 {
714         coex_dm->cur_bt_auto_report = enable_auto_report;
715
716         if (!force_exec) {
717                 if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
718                         return;
719         }
720         halbtc8821a1ant_set_bt_auto_report(btcoexist,
721                                            coex_dm->cur_bt_auto_report);
722
723         coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
724 }
725
726 void halbtc8821a1ant_set_sw_penalty_tx_rate_adaptive(IN struct btc_coexist
727                 *btcoexist, IN boolean low_penalty_ra)
728 {
729         u8                      h2c_parameter[6] = {0};
730
731         h2c_parameter[0] = 0x6; /* op_code, 0x6= Retry_Penalty */
732
733         if (low_penalty_ra) {
734                 h2c_parameter[1] |= BIT(0);
735                 h2c_parameter[2] =
736                         0x00;  /* normal rate except MCS7/6/5, OFDM54/48/36 */
737                 h2c_parameter[3] = 0xf5;  /* MCS7 or OFDM54 */
738                 h2c_parameter[4] = 0xa0;  /* MCS6 or OFDM48 */
739                 h2c_parameter[5] = 0xa0; /* MCS5 or OFDM36       */
740         }
741
742         btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
743 }
744
745 void halbtc8821a1ant_low_penalty_ra(IN struct btc_coexist *btcoexist,
746                             IN boolean force_exec, IN boolean low_penalty_ra)
747 {
748         coex_dm->cur_low_penalty_ra = low_penalty_ra;
749
750         if (!force_exec) {
751                 if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
752                         return;
753         }
754         halbtc8821a1ant_set_sw_penalty_tx_rate_adaptive(btcoexist,
755                         coex_dm->cur_low_penalty_ra);
756
757         coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
758 }
759
760 void halbtc8821a1ant_sw_mechanism(IN struct btc_coexist *btcoexist,
761                                   IN boolean low_penalty_ra)
762 {
763         halbtc8821a1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
764 }
765
766 void halbtc8821a1ant_set_coex_table(IN struct btc_coexist *btcoexist,
767             IN u32 val0x6c0, IN u32 val0x6c4, IN u32 val0x6c8, IN u8 val0x6cc)
768 {
769         btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
770
771         btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
772
773         btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
774
775         btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
776 }
777
778 void halbtc8821a1ant_coex_table(IN struct btc_coexist *btcoexist,
779                         IN boolean force_exec, IN u32 val0x6c0, IN u32 val0x6c4,
780                                 IN u32 val0x6c8, IN u8 val0x6cc)
781 {
782         coex_dm->cur_val0x6c0 = val0x6c0;
783         coex_dm->cur_val0x6c4 = val0x6c4;
784         coex_dm->cur_val0x6c8 = val0x6c8;
785         coex_dm->cur_val0x6cc = val0x6cc;
786
787         if (!force_exec) {
788                 if ((coex_dm->pre_val0x6c0 == coex_dm->cur_val0x6c0) &&
789                     (coex_dm->pre_val0x6c4 == coex_dm->cur_val0x6c4) &&
790                     (coex_dm->pre_val0x6c8 == coex_dm->cur_val0x6c8) &&
791                     (coex_dm->pre_val0x6cc == coex_dm->cur_val0x6cc))
792                         return;
793         }
794         halbtc8821a1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4, val0x6c8,
795                                        val0x6cc);
796
797         coex_dm->pre_val0x6c0 = coex_dm->cur_val0x6c0;
798         coex_dm->pre_val0x6c4 = coex_dm->cur_val0x6c4;
799         coex_dm->pre_val0x6c8 = coex_dm->cur_val0x6c8;
800         coex_dm->pre_val0x6cc = coex_dm->cur_val0x6cc;
801 }
802
803 void halbtc8821a1ant_coex_table_with_type(IN struct btc_coexist *btcoexist,
804                 IN boolean force_exec, IN u8 type)
805 {
806         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
807                     "[BTCoex], ********** CoexTable(%d) **********\n", type);
808         BTC_TRACE(trace_buf);
809
810         switch (type) {
811         case 0:
812                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
813                                    0x55555555, 0x55555555, 0xffffff, 0x3);
814                 break;
815         case 1:
816                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
817                                    0x55555555, 0x5a5a5a5a, 0xffffff, 0x3);
818                 break;
819         case 2:
820                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
821                                    0x5a5a5a5a, 0x5a5a5a5a, 0xffffff, 0x3);
822                 break;
823         case 3:
824                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
825                                    0x5a5a5a5a, 0xaaaaaaaa, 0xffffff, 0x3);
826                 break;
827         case 4:
828                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
829                                    0x55555555, 0x5a5a5a5a, 0xffffff, 0x3);
830                 break;
831         case 5:
832                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
833                                    0x5a5a5a5a, 0xaaaa5a5a, 0xffffff, 0x3);
834                 break;
835         case 6:
836                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
837                                    0x55555555, 0xaaaa5a5a, 0xffffff, 0x3);
838                 break;
839         case 7:
840                 halbtc8821a1ant_coex_table(btcoexist, force_exec,
841                                    0xaaaaaaaa, 0xaaaaaaaa, 0xffffff, 0x3);
842                 break;
843         default:
844                 break;
845         }
846 }
847
848 void halbtc8821a1ant_set_fw_ignore_wlan_act(IN struct btc_coexist *btcoexist,
849                 IN boolean enable)
850 {
851         u8                      h2c_parameter[1] = {0};
852
853         if (enable)
854                 h2c_parameter[0] |= BIT(0);             /* function enable */
855
856         btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
857 }
858
859 void halbtc8821a1ant_ignore_wlan_act(IN struct btc_coexist *btcoexist,
860                                      IN boolean force_exec, IN boolean enable)
861 {
862         coex_dm->cur_ignore_wlan_act = enable;
863
864         if (!force_exec) {
865                 if (coex_dm->pre_ignore_wlan_act ==
866                     coex_dm->cur_ignore_wlan_act)
867                         return;
868         }
869         halbtc8821a1ant_set_fw_ignore_wlan_act(btcoexist, enable);
870
871         coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
872 }
873
874 void halbtc8821a1ant_set_lps_rpwm(IN struct btc_coexist *btcoexist,
875                                   IN u8 lps_val, IN u8 rpwm_val)
876 {
877         u8      lps = lps_val;
878         u8      rpwm = rpwm_val;
879
880         btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
881         btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
882 }
883
884 void halbtc8821a1ant_lps_rpwm(IN struct btc_coexist *btcoexist,
885                       IN boolean force_exec, IN u8 lps_val, IN u8 rpwm_val)
886 {
887         coex_dm->cur_lps = lps_val;
888         coex_dm->cur_rpwm = rpwm_val;
889
890         if (!force_exec) {
891                 if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
892                     (coex_dm->pre_rpwm == coex_dm->cur_rpwm))
893                         return;
894         }
895         halbtc8821a1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
896
897         coex_dm->pre_lps = coex_dm->cur_lps;
898         coex_dm->pre_rpwm = coex_dm->cur_rpwm;
899 }
900
901 void halbtc8821a1ant_set_ant_path(IN struct btc_coexist *btcoexist,
902           IN u8 ant_pos_type, IN boolean init_hwcfg, IN boolean wifi_off)
903 {
904         struct  btc_board_info *board_info = &btcoexist->board_info;
905         u32                     u32tmp = 0;
906         u8                      h2c_parameter[2] = {0};
907
908         if (init_hwcfg) {
909                 /* 0x4c[23]=0, 0x4c[24]=1  Antenna control by WL/BT */
910                 u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
911                 u32tmp &= ~BIT(23);
912                 u32tmp |= BIT(24);
913                 btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
914
915                 /* 0x765 = 0x18 */
916                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x3);
917
918                 if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
919                         /* tell firmware "antenna inverse"  ==> WRONG firmware antenna control code.==>need fw to fix */
920                         h2c_parameter[0] = 1;
921                         h2c_parameter[1] = 1;
922                         btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
923                                                 h2c_parameter);
924
925                         /* btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64, 0x1, 0x1); */ /*Main Ant to  BT for IPS case 0x4c[23]=1 */
926                 } else {
927                         /* tell firmware "no antenna inverse" ==> WRONG firmware antenna control code.==>need fw to fix */
928                         h2c_parameter[0] = 0;
929                         h2c_parameter[1] = 1;
930                         btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
931                                                 h2c_parameter);
932
933                         /* btcoexist->btc_write_1byte_bitmask(btcoexist, 0x64, 0x1, 0x0); */ /*Aux Ant to  BT for IPS case 0x4c[23]=1 */
934                 }
935         } else if (wifi_off) {
936                 /* 0x4c[24:23]=00, Set Antenna control by BT_RFE_CTRL   BT Vendor 0xac=0xf002 */
937                 u32tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
938                 u32tmp &= ~BIT(23);
939                 u32tmp &= ~BIT(24);
940                 btcoexist->btc_write_4byte(btcoexist, 0x4c, u32tmp);
941
942                 /* 0x765 = 0x18 */
943                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x3);
944         } else {
945                 /* 0x765 = 0x0 */
946                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x0);
947         }
948
949         /* ext switch setting */
950         switch (ant_pos_type) {
951         case BTC_ANT_PATH_WIFI:
952                 btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
953                 if (board_info->btdm_ant_pos ==
954                     BTC_ANTENNA_AT_MAIN_PORT)
955                         btcoexist->btc_write_1byte_bitmask(btcoexist,
956                                                            0xcb7, 0x30, 0x1);
957                 else
958                         btcoexist->btc_write_1byte_bitmask(btcoexist,
959                                                            0xcb7, 0x30, 0x2);
960                 break;
961         case BTC_ANT_PATH_BT:
962                 btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
963                 if (board_info->btdm_ant_pos ==
964                     BTC_ANTENNA_AT_MAIN_PORT)
965                         btcoexist->btc_write_1byte_bitmask(btcoexist,
966                                                            0xcb7, 0x30, 0x2);
967                 else
968                         btcoexist->btc_write_1byte_bitmask(btcoexist,
969                                                            0xcb7, 0x30, 0x1);
970                 break;
971         default:
972         case BTC_ANT_PATH_PTA:
973                 btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x66);
974                 if (board_info->btdm_ant_pos ==
975                     BTC_ANTENNA_AT_MAIN_PORT)
976                         btcoexist->btc_write_1byte_bitmask(btcoexist,
977                                                            0xcb7, 0x30, 0x1);
978                 else
979                         btcoexist->btc_write_1byte_bitmask(btcoexist,
980                                                            0xcb7, 0x30, 0x2);
981                 break;
982         }
983 }
984
985
986 void halbtc8821a1ant_ps_tdma_check_for_power_save_state(
987         IN struct btc_coexist *btcoexist, IN boolean new_ps_state)
988 {
989         u8      lps_mode = 0x0;
990         u8      h2c_parameter[5] = {0x8, 0, 0, 0, 0};
991
992         btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
993
994         if (lps_mode) { /* already under LPS state */
995                 if (new_ps_state) {
996                         /* keep state under LPS, do nothing. */
997                 } else {
998                         /* will leave LPS state, turn off psTdma first */
999 /* halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,8); */
1000                         btcoexist->btc_fill_h2c(btcoexist, 0x60, 5,
1001                                 h2c_parameter);
1002                 }
1003         } else {                                        /* NO PS state */
1004                 if (new_ps_state) {
1005                         /* will enter LPS state, turn off psTdma first */
1006 /* halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,8); */
1007                         btcoexist->btc_fill_h2c(btcoexist, 0x60, 5,
1008                                 h2c_parameter);
1009                 } else {
1010                         /* keep state under NO PS state, do nothing. */
1011                 }
1012         }
1013 }
1014
1015 void halbtc8821a1ant_power_save_state(IN struct btc_coexist *btcoexist,
1016                               IN u8 ps_type, IN u8 lps_val, IN u8 rpwm_val)
1017 {
1018         boolean         low_pwr_disable = false;
1019
1020         switch (ps_type) {
1021         case BTC_PS_WIFI_NATIVE:
1022                 /* recover to original 32k low power setting */
1023                 low_pwr_disable = false;
1024                 btcoexist->btc_set(btcoexist,
1025                                    BTC_SET_ACT_DISABLE_LOW_POWER,
1026                                    &low_pwr_disable);
1027                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1028                                    NULL);
1029                 break;
1030         case BTC_PS_LPS_ON:
1031                 halbtc8821a1ant_ps_tdma_check_for_power_save_state(
1032                         btcoexist, true);
1033                 halbtc8821a1ant_lps_rpwm(btcoexist, NORMAL_EXEC,
1034                                          lps_val, rpwm_val);
1035                 /* when coex force to enter LPS, do not enter 32k low power. */
1036                 low_pwr_disable = true;
1037                 btcoexist->btc_set(btcoexist,
1038                                    BTC_SET_ACT_DISABLE_LOW_POWER,
1039                                    &low_pwr_disable);
1040                 /* power save must executed before psTdma. */
1041                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS,
1042                                    NULL);
1043                 break;
1044         case BTC_PS_LPS_OFF:
1045                 halbtc8821a1ant_ps_tdma_check_for_power_save_state(
1046                         btcoexist, false);
1047                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1048                                    NULL);
1049                 break;
1050         default:
1051                 break;
1052         }
1053 }
1054
1055 void halbtc8821a1ant_set_fw_pstdma(IN struct btc_coexist *btcoexist,
1056            IN u8 byte1, IN u8 byte2, IN u8 byte3, IN u8 byte4, IN u8 byte5)
1057 {
1058         u8                      h2c_parameter[5] = {0};
1059         u8                      real_byte1 = byte1, real_byte5 = byte5;
1060         boolean                 ap_enable = false;
1061
1062         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1063                            &ap_enable);
1064
1065         if (ap_enable) {
1066                 if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
1067                         real_byte1 &= ~BIT(4);
1068                         real_byte1 |= BIT(5);
1069
1070                         real_byte5 |= BIT(5);
1071                         real_byte5 &= ~BIT(6);
1072
1073                         halbtc8821a1ant_power_save_state(btcoexist,
1074                                 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1075                 }
1076 } else if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
1077 halbtc8821a1ant_power_save_state(btcoexist, BTC_PS_LPS_ON, 0x50, 0x4);
1078
1079         } else  {
1080                         halbtc8821a1ant_power_save_state(btcoexist,
1081                                 BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1082         }
1083
1084         h2c_parameter[0] = real_byte1;
1085         h2c_parameter[1] = byte2;
1086         h2c_parameter[2] = byte3;
1087         h2c_parameter[3] = byte4;
1088         h2c_parameter[4] = real_byte5;
1089
1090         coex_dm->ps_tdma_para[0] = real_byte1;
1091         coex_dm->ps_tdma_para[1] = byte2;
1092         coex_dm->ps_tdma_para[2] = byte3;
1093         coex_dm->ps_tdma_para[3] = byte4;
1094         coex_dm->ps_tdma_para[4] = real_byte5;
1095
1096         btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
1097 }
1098
1099 void halbtc8821a1ant_ps_tdma(IN struct btc_coexist *btcoexist,
1100                      IN boolean force_exec, IN boolean turn_on, IN u8 type)
1101 {
1102         u8                      rssi_adjust_val = 0;
1103         /* u32                  fw_ver=0; */
1104
1105         coex_dm->cur_ps_tdma_on = turn_on;
1106         coex_dm->cur_ps_tdma = type;
1107
1108         if (coex_dm->cur_ps_tdma_on) {
1109                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1110                             "[BTCoex], ********** TDMA(on, %d) **********\n",
1111                             coex_dm->cur_ps_tdma);
1112                 BTC_TRACE(trace_buf);
1113         } else {
1114                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1115                             "[BTCoex], ********** TDMA(off, %d) **********\n",
1116                             coex_dm->cur_ps_tdma);
1117                 BTC_TRACE(trace_buf);
1118         }
1119
1120         if (!force_exec) {
1121                 if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1122                     (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1123                         return;
1124         }
1125         if (turn_on) {
1126                 switch (type) {
1127                 default:
1128                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1129                                                       0x1a, 0x1a, 0x0, 0x50);
1130                         break;
1131                 case 1:
1132                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1133                                                       0x3a, 0x03, 0x10, 0x50);
1134                         rssi_adjust_val = 11;
1135                         break;
1136                 case 2:
1137                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1138                                                       0x2b, 0x03, 0x10, 0x50);
1139                         rssi_adjust_val = 14;
1140                         break;
1141                 case 3:
1142                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1143                                                       0x1d, 0x1d, 0x0, 0x52);
1144                         break;
1145                 case 4:
1146                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93,
1147                                                       0x15, 0x3, 0x14, 0x0);
1148                         rssi_adjust_val = 17;
1149                         break;
1150                 case 5:
1151                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61,
1152                                                       0x15, 0x3, 0x11, 0x10);
1153                         break;
1154                 case 6:
1155                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61,
1156                                                       0x20, 0x3, 0x11, 0x13);
1157                         break;
1158                 case 7:
1159                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13,
1160                                                       0xc, 0x5, 0x0, 0x0);
1161                         break;
1162                 case 8:
1163                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93,
1164                                                       0x25, 0x3, 0x10, 0x0);
1165                         break;
1166                 case 9:
1167                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1168                                                       0x21, 0x3, 0x10, 0x50);
1169                         rssi_adjust_val = 18;
1170                         break;
1171                 case 10:
1172                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13,
1173                                                       0xa, 0xa, 0x0, 0x40);
1174                         break;
1175                 case 11:
1176                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1177                                                       0x15, 0x03, 0x10, 0x50);
1178                         rssi_adjust_val = 20;
1179                         break;
1180                 case 12:
1181                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1182                                                       0x0a, 0x0a, 0x0, 0x50);
1183                         break;
1184                 case 13:
1185                         if (coex_sta->scan_ap_num <= 5)
1186                                 halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1187                                                       0x40, 0x3, 0x10, 0x50);
1188                         else
1189                                 halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1190                                                       0x12, 0x12, 0x0, 0x50);
1191                         break;
1192                 case 14:
1193                         if (coex_sta->scan_ap_num <= 5)
1194                                 halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1195                                                       0x30, 0x3, 0x10, 0x50);
1196                         else
1197                                 halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1198                                                       0x1e, 0x3, 0x10, 0x14);
1199                         break;
1200                 case 15:
1201                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x13,
1202                                                       0xa, 0x3, 0x8, 0x0);
1203                         break;
1204                 case 16:
1205                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93,
1206                                                       0x15, 0x3, 0x10, 0x0);
1207                         rssi_adjust_val = 18;
1208                         break;
1209                 case 18:
1210                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x93,
1211                                                       0x25, 0x3, 0x10, 0x0);
1212                         rssi_adjust_val = 14;
1213                         break;
1214                 case 20:
1215                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61,
1216                                                       0x35, 0x03, 0x11, 0x10);
1217                         break;
1218                 case 21:
1219                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61,
1220                                                       0x25, 0x03, 0x11, 0x11);
1221                         break;
1222                 case 22:
1223                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61,
1224                                                       0x25, 0x03, 0x11, 0x10);
1225                         break;
1226                 case 23:
1227                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3,
1228                                                       0x25, 0x3, 0x31, 0x18);
1229                         rssi_adjust_val = 22;
1230                         break;
1231                 case 24:
1232                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3,
1233                                                       0x15, 0x3, 0x31, 0x18);
1234                         rssi_adjust_val = 22;
1235                         break;
1236                 case 25:
1237                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3,
1238                                                       0xa, 0x3, 0x31, 0x18);
1239                         rssi_adjust_val = 22;
1240                         break;
1241                 case 26:
1242                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3,
1243                                                       0xa, 0x3, 0x31, 0x18);
1244                         rssi_adjust_val = 22;
1245                         break;
1246                 case 27:
1247                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xe3,
1248                                                       0x25, 0x3, 0x31, 0x98);
1249                         rssi_adjust_val = 22;
1250                         break;
1251                 case 28:
1252                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x69,
1253                                                       0x25, 0x3, 0x31, 0x0);
1254                         break;
1255                 case 29:
1256                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xab,
1257                                                       0x1a, 0x1a, 0x1, 0x10);
1258                         break;
1259                 case 30:
1260                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1261                                                       0x30, 0x3, 0x10, 0x10);
1262                         break;
1263                 case 31:
1264                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xd3,
1265                                                       0x1a, 0x1a, 0, 0x58);
1266                         break;
1267                 case 32:
1268                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x61,
1269                                                       0x35, 0x3, 0x11, 0x11);
1270                         break;
1271                 case 33:
1272                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xa3,
1273                                                       0x25, 0x3, 0x30, 0x90);
1274                         break;
1275                 case 34:
1276                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x53,
1277                                                       0x1a, 0x1a, 0x0, 0x10);
1278                         break;
1279                 case 35:
1280                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x63,
1281                                                       0x1a, 0x1a, 0x0, 0x10);
1282                         break;
1283                 case 36:
1284                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0xd3,
1285                                                       0x12, 0x3, 0x14, 0x50);
1286                         break;
1287                 case 40: /* SoftAP only with no sta associated,BT disable ,TDMA mode for power saving */
1288                         /* here softap mode screen off will cost 70-80mA for phone */
1289                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x23,
1290                                                       0x18, 0x00, 0x10, 0x24);
1291                         break;
1292                 case 41:
1293                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1294                                                       0x15, 0x3, 0x11, 0x11);
1295                         break;
1296                 case 42:
1297                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1298                                                       0x20, 0x3, 0x11, 0x11);
1299                         break;
1300                 case 43:
1301                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x51,
1302                                                       0x30, 0x3, 0x10, 0x11);
1303                         break;
1304                 }
1305         } else {
1306                 /* disable PS tdma */
1307                 switch (type) {
1308                 case 8: /* PTA Control */
1309                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x8,
1310                                                       0x0, 0x0, 0x0, 0x0);
1311                         halbtc8821a1ant_set_ant_path(btcoexist,
1312                                              BTC_ANT_PATH_PTA, false, false);
1313                         break;
1314                 case 0:
1315                 default:  /* Software control, Antenna at BT side */
1316                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0,
1317                                                       0x0, 0x0, 0x0, 0x0);
1318                         halbtc8821a1ant_set_ant_path(btcoexist,
1319                                              BTC_ANT_PATH_BT, false, false);
1320                         break;
1321                 case 9:   /* Software control, Antenna at WiFi side */
1322                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0,
1323                                                       0x0, 0x0, 0x0, 0x0);
1324                         halbtc8821a1ant_set_ant_path(btcoexist,
1325                                              BTC_ANT_PATH_WIFI, false, false);
1326                         break;
1327                 case 10:        /* under 5G */
1328                         halbtc8821a1ant_set_fw_pstdma(btcoexist, 0x0,
1329                                                       0x0, 0x0, 0x8, 0x0);
1330                         halbtc8821a1ant_set_ant_path(btcoexist,
1331                                              BTC_ANT_PATH_BT, false, false);
1332                         break;
1333                 }
1334         }
1335         rssi_adjust_val = 0;
1336         btcoexist->btc_set(btcoexist,
1337                 BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssi_adjust_val);
1338
1339         /* update pre state */
1340         coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1341         coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1342 }
1343
1344 void halbtc8821a1ant_coex_all_off(IN struct btc_coexist *btcoexist)
1345 {
1346         /* sw all off */
1347         halbtc8821a1ant_sw_mechanism(btcoexist, false);
1348
1349         /* hw all off */
1350         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1351 }
1352
1353 boolean halbtc8821a1ant_is_common_action(IN struct btc_coexist *btcoexist)
1354 {
1355         boolean                 common = false, wifi_connected = false, wifi_busy = false;
1356
1357         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1358                            &wifi_connected);
1359         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1360
1361         if (!wifi_connected &&
1362             BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1363             coex_dm->bt_status) {
1364                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1365                         "[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
1366                 BTC_TRACE(trace_buf);
1367                 halbtc8821a1ant_sw_mechanism(btcoexist, false);
1368
1369                 common = true;
1370         } else if (wifi_connected &&
1371                    (BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1372                     coex_dm->bt_status)) {
1373                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1374                         "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1375                 BTC_TRACE(trace_buf);
1376                 halbtc8821a1ant_sw_mechanism(btcoexist, false);
1377
1378                 common = true;
1379         } else if (!wifi_connected &&
1380                    (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1381                     coex_dm->bt_status)) {
1382                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1383                         "[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
1384                 BTC_TRACE(trace_buf);
1385                 halbtc8821a1ant_sw_mechanism(btcoexist, false);
1386
1387                 common = true;
1388         } else if (wifi_connected &&
1389                    (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1390                     coex_dm->bt_status)) {
1391                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1392                             "[BTCoex], Wifi connected + BT connected-idle!!\n");
1393                 BTC_TRACE(trace_buf);
1394                 halbtc8821a1ant_sw_mechanism(btcoexist, false);
1395
1396                 common = true;
1397         } else if (!wifi_connected &&
1398                    (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE !=
1399                     coex_dm->bt_status)) {
1400                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1401                             "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
1402                 BTC_TRACE(trace_buf);
1403                 halbtc8821a1ant_sw_mechanism(btcoexist, false);
1404
1405                 common = true;
1406         } else {
1407                 if (wifi_busy) {
1408                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1409                                 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1410                         BTC_TRACE(trace_buf);
1411                 } else {
1412                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1413                                 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1414                         BTC_TRACE(trace_buf);
1415                 }
1416
1417                 common = false;
1418         }
1419
1420         return common;
1421 }
1422
1423 /* *********************************************
1424  *
1425  *      Software Coex Mechanism start
1426  *
1427  * ********************************************* */
1428
1429 void halbtc8821a1ant_action_bt_whck_test(IN struct btc_coexist *btcoexist)
1430 {
1431
1432         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1433         /* halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA, NORMAL_EXEC, false, false); */
1434         halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA, false, false);
1435         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1436 }
1437 /* SCO only or SCO+PAN(HS) */
1438 void halbtc8821a1ant_action_sco(IN struct btc_coexist *btcoexist)
1439 {
1440         halbtc8821a1ant_sw_mechanism(btcoexist, true);
1441 }
1442
1443 void halbtc8821a1ant_action_hid(IN struct btc_coexist *btcoexist)
1444 {
1445         halbtc8821a1ant_sw_mechanism(btcoexist, true);
1446 }
1447
1448 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
1449 void halbtc8821a1ant_action_a2dp(IN struct btc_coexist *btcoexist)
1450 {
1451         halbtc8821a1ant_sw_mechanism(btcoexist, false);
1452 }
1453
1454 void halbtc8821a1ant_action_a2dp_pan_hs(IN struct btc_coexist *btcoexist)
1455 {
1456         halbtc8821a1ant_sw_mechanism(btcoexist, false);
1457 }
1458
1459 void halbtc8821a1ant_action_pan_edr(IN struct btc_coexist *btcoexist)
1460 {
1461         halbtc8821a1ant_sw_mechanism(btcoexist, false);
1462 }
1463
1464 /* PAN(HS) only */
1465 void halbtc8821a1ant_action_pan_hs(IN struct btc_coexist *btcoexist)
1466 {
1467         halbtc8821a1ant_sw_mechanism(btcoexist, false);
1468 }
1469
1470 /* PAN(EDR)+A2DP */
1471 void halbtc8821a1ant_action_pan_edr_a2dp(IN struct btc_coexist *btcoexist)
1472 {
1473         halbtc8821a1ant_sw_mechanism(btcoexist, false);
1474 }
1475
1476 void halbtc8821a1ant_action_pan_edr_hid(IN struct btc_coexist *btcoexist)
1477 {
1478         halbtc8821a1ant_sw_mechanism(btcoexist, true);
1479 }
1480
1481 /* HID+A2DP+PAN(EDR) */
1482 void halbtc8821a1ant_action_hid_a2dp_pan_edr(IN struct btc_coexist *btcoexist)
1483 {
1484         halbtc8821a1ant_sw_mechanism(btcoexist, true);
1485 }
1486
1487 void halbtc8821a1ant_action_hid_a2dp(IN struct btc_coexist *btcoexist)
1488 {
1489         halbtc8821a1ant_sw_mechanism(btcoexist, true);
1490 }
1491
1492 /* *********************************************
1493  *
1494  *      Non-Software Coex Mechanism start
1495  *
1496  * ********************************************* */
1497 void halbtc8821a1ant_action_hs(IN struct btc_coexist *btcoexist)
1498 {
1499         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1500         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1501 }
1502
1503 void halbtc8821a1ant_action_bt_inquiry(IN struct btc_coexist *btcoexist)
1504 {
1505         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1506         boolean                 wifi_connected = false, ap_enable = false, wifi_busy = false,
1507                                 bt_busy = false;
1508
1509         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1510                            &ap_enable);
1511         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1512                            &wifi_connected);
1513         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1514         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
1515
1516         if ((!wifi_connected) && (!coex_sta->wifi_is_high_pri_task)) {
1517                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1518
1519                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1520         }
1521
1522         /* sy modify     */
1523         else if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1524                  (bt_link_info->a2dp_exist)) {
1525                 /* SCO/HID/A2DP  busy */
1526
1527                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1528
1529                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1530         }
1531
1532         /* sy modify */
1533
1534         else if ((bt_link_info->a2dp_exist) &&
1535                  (bt_link_info->hid_exist)) {
1536                 /* A2DP+HID     busy */
1537
1538                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1539                                         14);
1540
1541                 halbtc8821a1ant_coex_table_with_type(btcoexist,
1542                                                      NORMAL_EXEC, 1);
1543         }
1544
1545
1546         else if ((bt_link_info->pan_exist) || (wifi_busy)) {
1547                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1548
1549                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1550         } else {
1551                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1552
1553                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
1554         }
1555 }
1556
1557 void halbtc8821a1ant_action_bt_sco_hid_only_busy(IN struct btc_coexist
1558                 *btcoexist, IN u8 wifi_status)
1559 {
1560         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1561         boolean wifi_connected = false;
1562
1563         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1564                            &wifi_connected);
1565
1566         /* tdma and coex table */
1567
1568         if (bt_link_info->sco_exist) {
1569                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 41);
1570                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1571         } else { /* HID */
1572                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 42);
1573                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1574         }
1575 }
1576
1577 void halbtc8821a1ant_coex_under_5g(IN struct btc_coexist *btcoexist)
1578 {
1579
1580         halbtc8821a1ant_ignore_wlan_act(btcoexist, NORMAL_EXEC, true);
1581
1582         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 10);
1583
1584         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1585
1586         halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1587
1588         halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 5);
1589 }
1590
1591 void halbtc8821a1ant_action_wifi_only(IN struct btc_coexist *btcoexist)
1592 {
1593         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1594         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 9);
1595 }
1596
1597 void halbtc8821a1ant_monitor_bt_enable_disable(IN struct btc_coexist *btcoexist)
1598 {
1599         static u32              bt_disable_cnt = 0;
1600         boolean                 bt_active = true, bt_disabled = false;
1601
1602         /* This function check if bt is disabled */
1603
1604         if (coex_sta->high_priority_tx == 0 &&
1605             coex_sta->high_priority_rx == 0 &&
1606             coex_sta->low_priority_tx == 0 &&
1607             coex_sta->low_priority_rx == 0)
1608                 bt_active = false;
1609         if (coex_sta->high_priority_tx == 0xffff &&
1610             coex_sta->high_priority_rx == 0xffff &&
1611             coex_sta->low_priority_tx == 0xffff &&
1612             coex_sta->low_priority_rx == 0xffff)
1613                 bt_active = false;
1614         if (bt_active) {
1615                 bt_disable_cnt = 0;
1616                 bt_disabled = false;
1617                 btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1618                                    &bt_disabled);
1619                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1620                             "[BTCoex], BT is enabled !!\n");
1621                 BTC_TRACE(trace_buf);
1622         } else {
1623                 bt_disable_cnt++;
1624                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1625                             "[BTCoex], bt all counters=0, %d times!!\n",
1626                             bt_disable_cnt);
1627                 BTC_TRACE(trace_buf);
1628                 if (bt_disable_cnt >= 10) {
1629                         bt_disabled = true;
1630                         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1631                                            &bt_disabled);
1632                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1633                                     "[BTCoex], BT is disabled !!\n");
1634                         BTC_TRACE(trace_buf);
1635                         halbtc8821a1ant_action_wifi_only(btcoexist);
1636                 }
1637         }
1638         if (coex_sta->bt_disabled != bt_disabled) {
1639                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1640                             "[BTCoex], BT is from %s to %s!!\n",
1641                             (coex_sta->bt_disabled ? "disabled" : "enabled"),
1642                             (bt_disabled ? "disabled" : "enabled"));
1643                 BTC_TRACE(trace_buf);
1644                 coex_sta->bt_disabled = bt_disabled;
1645                 if (!bt_disabled) {
1646                 } else {
1647                         btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1648                                            NULL);
1649                         btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1650                                            NULL);
1651                 }
1652         }
1653 }
1654
1655 void halbtc8821a1ant_action_wifi_multi_port(IN struct btc_coexist *btcoexist)
1656 {
1657
1658         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1659         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1660 }
1661
1662
1663 void halbtc8821a1ant_action_wifi_connected_bt_acl_busy(IN struct btc_coexist
1664                 *btcoexist, IN u8 wifi_status)
1665 {
1666         u8              bt_rssi_state;
1667
1668         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1669
1670         bt_rssi_state = halbtc8821a1ant_bt_rssi_state(2, 28, 0);
1671
1672         if (bt_link_info->hid_only) { /* HID */
1673                 halbtc8821a1ant_action_bt_sco_hid_only_busy(btcoexist,
1674                                 wifi_status);
1675                 coex_dm->auto_tdma_adjust = false;
1676                 return;
1677         } else if (bt_link_info->a2dp_only) { /* A2DP            */
1678                 if (BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE == wifi_status) {
1679                         /* halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8); */
1680                         /* halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2); */
1681                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1682                                                 32);
1683                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1684                                                              NORMAL_EXEC, 1);
1685                         coex_dm->auto_tdma_adjust = false;
1686                 } else if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1687                            (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1688                         /* halbtc8821a1ant_tdma_duration_adjust_for_acl(btcoexist, wifi_status); */
1689                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1690                                                 14);
1691                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1692                                                              NORMAL_EXEC, 1);
1693                 } else { /* for low BT RSSI */
1694                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1695                                                 14);
1696                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1697                                                              NORMAL_EXEC, 1);
1698                         coex_dm->auto_tdma_adjust = false;
1699                 }
1700         } else if (bt_link_info->hid_exist &&
1701                    bt_link_info->a2dp_exist) { /* HID+A2DP */
1702                 if ((bt_rssi_state == BTC_RSSI_STATE_HIGH) ||
1703                     (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1704                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1705                                                 14);
1706                         coex_dm->auto_tdma_adjust = false;
1707                 } else { /* for low BT RSSI */
1708                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1709                                                 14);
1710                         coex_dm->auto_tdma_adjust = false;
1711                 }
1712
1713                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1714         } else if ((bt_link_info->pan_only) || (bt_link_info->hid_exist &&
1715                 bt_link_info->pan_exist)) { /* PAN(OPP,FTP), HID+PAN(OPP,FTP)                    */
1716                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1717                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 6);
1718                 coex_dm->auto_tdma_adjust = false;
1719         } else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1720                    (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1721                 bt_link_info->pan_exist)) { /* A2DP+PAN(OPP,FTP), HID+A2DP+PAN(OPP,FTP) */
1722                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 43);
1723                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1724                 coex_dm->auto_tdma_adjust = false;
1725         } else {
1726                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1727                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1728                 coex_dm->auto_tdma_adjust = false;
1729         }
1730 }
1731
1732 void halbtc8821a1ant_action_wifi_not_connected(IN struct btc_coexist *btcoexist)
1733 {
1734
1735         /* tdma and coex table   */
1736         halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
1737         halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1738 }
1739
1740 void halbtc8821a1ant_action_wifi_not_connected_scan(IN struct btc_coexist
1741                 *btcoexist)
1742 {
1743         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1744
1745
1746         /* tdma and coex table */
1747         if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1748                 if (bt_link_info->a2dp_exist) {
1749                         /* sy modify */
1750                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1751                                                 14);
1752                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1753                                                              NORMAL_EXEC, 1);
1754                 } else if (bt_link_info->a2dp_exist &&
1755                            bt_link_info->pan_exist) {
1756                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1757                                                 22);
1758                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1759                                                              NORMAL_EXEC, 4);
1760                 } else {
1761                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1762                                                 20);
1763                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1764                                                              NORMAL_EXEC, 4);
1765                 }
1766         } else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
1767                    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1768                     coex_dm->bt_status)) {
1769                 halbtc8821a1ant_action_bt_sco_hid_only_busy(btcoexist,
1770                                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1771         } else {
1772                 /* halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20); */
1773                 /* halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1); */
1774
1775                 /* Bryant Add */
1776                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1777                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1778         }
1779 }
1780
1781 void halbtc8821a1ant_action_wifi_not_connected_asso_auth(
1782         IN struct btc_coexist *btcoexist)
1783 {
1784         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1785
1786         /* tdma and coex table */
1787         if ((bt_link_info->sco_exist)  || (bt_link_info->hid_exist)) {
1788                 /* sy modify */
1789                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1790                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1791         } else if ((bt_link_info->a2dp_exist)  || (bt_link_info->pan_exist)) {
1792                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1793                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1794         } else {
1795                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1796                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1797         }
1798 }
1799
1800 void halbtc8821a1ant_action_wifi_connected_scan(IN struct btc_coexist
1801                 *btcoexist)
1802 {
1803         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1804
1805         /* tdma and coex table */
1806         if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1807                 if (bt_link_info->a2dp_exist) {
1808                         /* sy modify */
1809                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1810                                                 14);
1811                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1812                                                              NORMAL_EXEC, 1);
1813                 } else if (bt_link_info->a2dp_exist &&
1814                            bt_link_info->pan_exist) {
1815                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1816                                                 22);
1817                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1818                                                              NORMAL_EXEC, 4);
1819                 } else {
1820                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1821                                                 20);
1822                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1823                                                              NORMAL_EXEC, 4);
1824                 }
1825         } else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
1826                    (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1827                     coex_dm->bt_status)) {
1828                 halbtc8821a1ant_action_bt_sco_hid_only_busy(btcoexist,
1829                                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1830         } else {
1831                 /* halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20); */
1832                 /* halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1); */
1833
1834                 /* Bryant Add */
1835                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1836                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1837         }
1838 }
1839
1840 void halbtc8821a1ant_action_wifi_connected_specific_packet(
1841         IN struct btc_coexist *btcoexist)
1842 {
1843         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1844
1845         /* tdma and coex table */
1846         /* sy modify */
1847         if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1848             (bt_link_info->a2dp_exist)) {
1849                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1850                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1851         }
1852
1853         if ((bt_link_info->hid_exist) && (bt_link_info->a2dp_exist)) {
1854                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true,
1855                                         14);
1856                 halbtc8821a1ant_coex_table_with_type(btcoexist,
1857                                                      NORMAL_EXEC, 1);
1858         }
1859
1860
1861         else if (bt_link_info->pan_exist) {
1862                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1863                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1864         } else {
1865                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1866                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1867         }
1868 }
1869
1870 void halbtc8821a1ant_action_wifi_connected(IN struct btc_coexist *btcoexist)
1871 {
1872         boolean wifi_busy = false;
1873         boolean scan = false, link = false, roam = false;
1874         boolean         under_4way = false, ap_enable = false;
1875
1876         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1877                     "[BTCoex], CoexForWifiConnect()===>\n");
1878         BTC_TRACE(trace_buf);
1879
1880         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
1881                            &under_4way);
1882         if (under_4way) {
1883                 halbtc8821a1ant_action_wifi_connected_specific_packet(
1884                         btcoexist);
1885                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1886                         "[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
1887                 BTC_TRACE(trace_buf);
1888                 return;
1889         }
1890
1891         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1892         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1893         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1894         if (scan || link || roam) {
1895                 if (scan)
1896                         halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
1897                 else
1898                         halbtc8821a1ant_action_wifi_connected_specific_packet(
1899                                 btcoexist);
1900                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1901                         "[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
1902                 BTC_TRACE(trace_buf);
1903                 return;
1904         }
1905
1906         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1907                            &ap_enable);
1908         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1909
1910         /* tdma and coex table */
1911         if (!wifi_busy) {
1912                 if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1913                         halbtc8821a1ant_action_wifi_connected_bt_acl_busy(
1914                                 btcoexist,
1915                                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1916                 } else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1917                             coex_dm->bt_status) ||
1918                            (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1919                             coex_dm->bt_status)) {
1920                         halbtc8821a1ant_action_bt_sco_hid_only_busy(btcoexist,
1921                                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1922                 } else {
1923                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
1924                                                 8);
1925                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1926                                                              NORMAL_EXEC, 2);
1927                 }
1928         } else {
1929                 if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1930                         halbtc8821a1ant_action_wifi_connected_bt_acl_busy(
1931                                 btcoexist,
1932                                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1933                 } else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1934                             coex_dm->bt_status) ||
1935                            (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1936                             coex_dm->bt_status)) {
1937                         halbtc8821a1ant_action_bt_sco_hid_only_busy(btcoexist,
1938                                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1939                 } else {
1940                         halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false,
1941                                                 8);
1942                         halbtc8821a1ant_coex_table_with_type(btcoexist,
1943                                                              NORMAL_EXEC, 2);
1944                 }
1945         }
1946 }
1947
1948 void halbtc8821a1ant_run_sw_coexist_mechanism(IN struct btc_coexist *btcoexist)
1949 {
1950         u8      algorithm = 0;
1951
1952         algorithm = halbtc8821a1ant_action_algorithm(btcoexist);
1953         coex_dm->cur_algorithm = algorithm;
1954
1955         if (halbtc8821a1ant_is_common_action(btcoexist)) {
1956
1957         } else {
1958                 switch (coex_dm->cur_algorithm) {
1959                 case BT_8821A_1ANT_COEX_ALGO_SCO:
1960                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1961                                     "[BTCoex], Action algorithm = SCO.\n");
1962                         BTC_TRACE(trace_buf);
1963                         halbtc8821a1ant_action_sco(btcoexist);
1964                         break;
1965                 case BT_8821A_1ANT_COEX_ALGO_HID:
1966                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1967                                     "[BTCoex], Action algorithm = HID.\n");
1968                         BTC_TRACE(trace_buf);
1969                         halbtc8821a1ant_action_hid(btcoexist);
1970                         break;
1971                 case BT_8821A_1ANT_COEX_ALGO_A2DP:
1972                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1973                                     "[BTCoex], Action algorithm = A2DP.\n");
1974                         BTC_TRACE(trace_buf);
1975                         halbtc8821a1ant_action_a2dp(btcoexist);
1976                         break;
1977                 case BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS:
1978                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1979                                 "[BTCoex], Action algorithm = A2DP+PAN(HS).\n");
1980                         BTC_TRACE(trace_buf);
1981                         halbtc8821a1ant_action_a2dp_pan_hs(btcoexist);
1982                         break;
1983                 case BT_8821A_1ANT_COEX_ALGO_PANEDR:
1984                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1985                                     "[BTCoex], Action algorithm = PAN(EDR).\n");
1986                         BTC_TRACE(trace_buf);
1987                         halbtc8821a1ant_action_pan_edr(btcoexist);
1988                         break;
1989                 case BT_8821A_1ANT_COEX_ALGO_PANHS:
1990                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1991                                     "[BTCoex], Action algorithm = HS mode.\n");
1992                         BTC_TRACE(trace_buf);
1993                         halbtc8821a1ant_action_pan_hs(btcoexist);
1994                         break;
1995                 case BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP:
1996                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1997                                     "[BTCoex], Action algorithm = PAN+A2DP.\n");
1998                         BTC_TRACE(trace_buf);
1999                         halbtc8821a1ant_action_pan_edr_a2dp(btcoexist);
2000                         break;
2001                 case BT_8821A_1ANT_COEX_ALGO_PANEDR_HID:
2002                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2003                                 "[BTCoex], Action algorithm = PAN(EDR)+HID.\n");
2004                         BTC_TRACE(trace_buf);
2005                         halbtc8821a1ant_action_pan_edr_hid(btcoexist);
2006                         break;
2007                 case BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2008                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2009                                 "[BTCoex], Action algorithm = HID+A2DP+PAN.\n");
2010                         BTC_TRACE(trace_buf);
2011                         halbtc8821a1ant_action_hid_a2dp_pan_edr(
2012                                 btcoexist);
2013                         break;
2014                 case BT_8821A_1ANT_COEX_ALGO_HID_A2DP:
2015                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2016                                     "[BTCoex], Action algorithm = HID+A2DP.\n");
2017                         BTC_TRACE(trace_buf);
2018                         halbtc8821a1ant_action_hid_a2dp(btcoexist);
2019                         break;
2020                 default:
2021                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2022                                 "[BTCoex], Action algorithm = coexist All Off!!\n");
2023                         BTC_TRACE(trace_buf);
2024                         /* halbtc8821a1ant_coex_all_off(btcoexist); */
2025                         break;
2026                 }
2027                 coex_dm->pre_algorithm = coex_dm->cur_algorithm;
2028         }
2029 }
2030
2031 void halbtc8821a1ant_run_coexist_mechanism(IN struct btc_coexist *btcoexist)
2032 {
2033         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2034         boolean wifi_connected = false, bt_hs_on = false;
2035         boolean increase_scan_dev_num = false;
2036         boolean bt_ctrl_agg_buf_size = false;
2037         u8      agg_buf_size = 5;
2038         u8      wifi_rssi_state = BTC_RSSI_STATE_HIGH;
2039         u32     wifi_link_status = 0;
2040         u32     num_of_wifi_link = 0;
2041         boolean wifi_under_5g = false;
2042
2043         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2044                     "[BTCoex], RunCoexistMechanism()===>\n");
2045         BTC_TRACE(trace_buf);
2046
2047         if (btcoexist->manual_control) {
2048                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2049                         "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2050                 BTC_TRACE(trace_buf);
2051                 return;
2052         }
2053
2054         if (btcoexist->stop_coex_dm) {
2055                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2056                         "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2057                 BTC_TRACE(trace_buf);
2058                 return;
2059         }
2060
2061         if (coex_sta->under_ips) {
2062                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2063                             "[BTCoex], wifi is under IPS !!!\n");
2064                 BTC_TRACE(trace_buf);
2065                 return;
2066         }
2067
2068         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2069         if (wifi_under_5g) {
2070                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2071                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2072                 BTC_TRACE(trace_buf);
2073                 halbtc8821a1ant_coex_under_5g(btcoexist);
2074                 return;
2075         }
2076         if (coex_sta->bt_whck_test) {
2077                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2078                             "[BTCoex], BT is under WHCK TEST!!!\n");
2079                 BTC_TRACE(trace_buf);
2080                 halbtc8821a1ant_action_bt_whck_test(btcoexist);
2081                 return;
2082         }
2083
2084         if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2085             (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2086             (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2087                 increase_scan_dev_num = true;
2088
2089         btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2090                            &increase_scan_dev_num);
2091         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2092                            &wifi_connected);
2093
2094         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2095                            &wifi_link_status);
2096         num_of_wifi_link = wifi_link_status >> 16;
2097         if ((num_of_wifi_link >= 2) ||
2098             (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
2099                 halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2100                 halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2101                                            bt_ctrl_agg_buf_size, agg_buf_size);
2102                 halbtc8821a1ant_action_wifi_multi_port(btcoexist);
2103                 return;
2104         }
2105
2106         if (!bt_link_info->sco_exist && !bt_link_info->hid_exist)
2107                 halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2108         else {
2109                 if (wifi_connected) {
2110                         wifi_rssi_state = halbtc8821a1ant_wifi_rssi_state(
2111                                                   btcoexist, 1, 2, 30, 0);
2112                         if ((wifi_rssi_state == BTC_RSSI_STATE_HIGH) ||
2113                             (wifi_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
2114                                 /* halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1, 1, 1); */
2115                                 halbtc8821a1ant_limited_tx(btcoexist,
2116                                                    NORMAL_EXEC, 1, 1, 0, 1);
2117                         } else {
2118                                 /* halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 1, 1, 1, 1); */
2119                                 halbtc8821a1ant_limited_tx(btcoexist,
2120                                                    NORMAL_EXEC, 1, 1, 0, 1);
2121                         }
2122                 } else
2123                         halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0,
2124                                                    0, 0);
2125
2126         }
2127
2128         if (bt_link_info->sco_exist) {
2129                 bt_ctrl_agg_buf_size = true;
2130                 agg_buf_size = 0x3;
2131         } else if (bt_link_info->hid_exist) {
2132                 bt_ctrl_agg_buf_size = true;
2133                 agg_buf_size = 0x5;
2134         } else if (bt_link_info->a2dp_exist || bt_link_info->pan_exist) {
2135                 bt_ctrl_agg_buf_size = true;
2136                 agg_buf_size = 0x8;
2137         }
2138         halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2139                                    bt_ctrl_agg_buf_size, agg_buf_size);
2140
2141         halbtc8821a1ant_run_sw_coexist_mechanism(btcoexist);
2142
2143         /* low pelnaty ra in pcr ra */
2144         btcoexist->btc_phydm_modify_RA_PCR_threshold(btcoexist, 0, 25);
2145
2146         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2147         if (coex_sta->c2h_bt_inquiry_page) {
2148                 halbtc8821a1ant_action_bt_inquiry(btcoexist);
2149                 return;
2150         } else if (bt_hs_on) {
2151                 halbtc8821a1ant_action_hs(btcoexist);
2152                 return;
2153         }
2154
2155
2156         if (!wifi_connected) {
2157                 boolean scan = false, link = false, roam = false;
2158
2159                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2160                             "[BTCoex], wifi is non connected-idle !!!\n");
2161                 BTC_TRACE(trace_buf);
2162
2163                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2164                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2165                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2166
2167                 if (scan || link || roam) {
2168                         if (scan)
2169                                 halbtc8821a1ant_action_wifi_not_connected_scan(
2170                                         btcoexist);
2171                         else
2172                                 halbtc8821a1ant_action_wifi_not_connected_asso_auth(
2173                                         btcoexist);
2174                 } else
2175                         halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2176         } else  /* wifi LPS/Busy */
2177                 halbtc8821a1ant_action_wifi_connected(btcoexist);
2178 }
2179
2180 void halbtc8821a1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
2181 {
2182         /* force to reset coex mechanism */
2183         /* sw all off */
2184         halbtc8821a1ant_sw_mechanism(btcoexist, false);
2185
2186         /* halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8); */
2187         halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2188 }
2189
2190 void halbtc8821a1ant_init_hw_config(IN struct btc_coexist *btcoexist,
2191                                     IN boolean back_up, IN boolean wifi_only)
2192 {
2193         u8      u8tmp = 0;
2194         boolean                 wifi_under_5g = false;
2195
2196
2197         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2198                     "[BTCoex], 1Ant Init HW Config!!\n");
2199         BTC_TRACE(trace_buf);
2200
2201         if (wifi_only)
2202                 return;
2203
2204         if (back_up) {
2205                 coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2206                                             0x430);
2207                 coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2208                                             0x434);
2209                 coex_dm->backup_retry_limit = btcoexist->btc_read_2byte(
2210                                                       btcoexist, 0x42a);
2211                 coex_dm->backup_ampdu_max_time = btcoexist->btc_read_1byte(
2212                                 btcoexist, 0x456);
2213         }
2214
2215         /* 0x790[5:0]=0x5 */
2216         u8tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2217         u8tmp &= 0xc0;
2218         u8tmp |= 0x5;
2219         btcoexist->btc_write_1byte(btcoexist, 0x790, u8tmp);
2220
2221         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2222
2223         /* Give bt_coex_supported_version the default value */
2224         coex_sta->bt_coex_supported_version = 0;
2225
2226         /* 0xf0[15:12] --> Chip Cut information */
2227         coex_sta->cut_version = (btcoexist->btc_read_1byte(btcoexist,
2228                                  0xf1) & 0xf0) >> 4;
2229
2230         /* Antenna config */
2231         if (wifi_under_5g)
2232                 halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, true,
2233                                              false);
2234         else
2235                 halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA, true,
2236                                              false);
2237
2238         /* PTA parameter */
2239         halbtc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2240
2241         /* Enable counter statistics */
2242         btcoexist->btc_write_1byte(btcoexist, 0x76e,
2243                            0xc); /* 0x76e[3] =1, WLAN_Act control by PTA */
2244         btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
2245         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2246 }
2247
2248 /* ************************************************************
2249  * work around function start with wa_halbtc8821a1ant_
2250  * ************************************************************
2251  * ************************************************************
2252  * extern function start with ex_halbtc8821a1ant_
2253  * ************************************************************ */
2254 void ex_halbtc8821a1ant_power_on_setting(IN struct btc_coexist *btcoexist)
2255 {
2256 }
2257
2258 void ex_halbtc8821a1ant_init_hw_config(IN struct btc_coexist *btcoexist,
2259                                        IN boolean wifi_only)
2260 {
2261         halbtc8821a1ant_init_hw_config(btcoexist, true, wifi_only);
2262 }
2263
2264 void ex_halbtc8821a1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
2265 {
2266         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2267                     "[BTCoex], Coex Mechanism Init!!\n");
2268         BTC_TRACE(trace_buf);
2269
2270         btcoexist->stop_coex_dm = false;
2271
2272         halbtc8821a1ant_init_coex_dm(btcoexist);
2273
2274         halbtc8821a1ant_query_bt_info(btcoexist);
2275 }
2276
2277 void ex_halbtc8821a1ant_display_coex_info(IN struct btc_coexist *btcoexist)
2278 {
2279         struct  btc_board_info          *board_info = &btcoexist->board_info;
2280         struct  btc_bt_link_info        *bt_link_info = &btcoexist->bt_link_info;
2281         u8                              *cli_buf = btcoexist->cli_buf;
2282         u8                              u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2283         u16                             u16tmp[4];
2284         u32                             u32tmp[4];
2285         u32                             fw_ver = 0, bt_patch_ver = 0;
2286         u32                             bt_coex_ver = 0;
2287         u32                             fa_ofdm, fa_cck, cca_ofdm, cca_cck;
2288         u32                             phyver = 0;
2289
2290         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2291                    "\r\n ============[BT Coexist info]============");
2292         CL_PRINTF(cli_buf);
2293
2294         if (btcoexist->manual_control) {
2295                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2296                         "\r\n ============[Under Manual Control]============");
2297                 CL_PRINTF(cli_buf);
2298                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2299                            "\r\n ==========================================");
2300                 CL_PRINTF(cli_buf);
2301         }
2302         if (btcoexist->stop_coex_dm) {
2303                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2304                            "\r\n ============[Coex is STOPPED]============");
2305                 CL_PRINTF(cli_buf);
2306                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2307                            "\r\n ==========================================");
2308                 CL_PRINTF(cli_buf);
2309         }
2310
2311         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d",
2312                    "Ant PG Num/ Ant Mech/ Ant Pos:",
2313                    board_info->pg_ant_num, board_info->btdm_ant_num,
2314                    board_info->btdm_ant_pos);
2315         CL_PRINTF(cli_buf);
2316
2317         /* btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver); */
2318         bt_patch_ver = btcoexist->bt_info.bt_get_fw_ver;
2319         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2320         phyver = btcoexist->btc_get_bt_phydm_version(btcoexist);
2321         bt_coex_ver = ((coex_sta->bt_coex_supported_version & 0xff00) >> 8);
2322
2323         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2324                    "\r\n %-35s = %d_%02x/ 0x%02x/ 0x%02x (%s)",
2325                    "CoexVer WL/  BT_Desired/ BT_Report",
2326                    glcoex_ver_date_8821a_1ant, glcoex_ver_8821a_1ant,
2327                    glcoex_ver_btdesired_8821a_1ant,
2328                    bt_coex_ver,
2329                    (bt_coex_ver == 0xff ? "Unknown" :
2330                     (bt_coex_ver >= glcoex_ver_btdesired_8821a_1ant ?
2331                      "Match" : "Mis-Match")));
2332         CL_PRINTF(cli_buf);
2333
2334         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2335                    "\r\n %-35s = 0x%x/ 0x%x/ v%d/ %c",
2336                    "W_FW/ B_FW/ Phy/ Kt",
2337                    fw_ver, bt_patch_ver, phyver,
2338                    coex_sta->cut_version + 65);
2339         CL_PRINTF(cli_buf);
2340
2341         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x ",
2342                    "Wifi channel informed to BT",
2343                    coex_dm->wifi_chnl_info[0], coex_dm->wifi_chnl_info[1],
2344                    coex_dm->wifi_chnl_info[2]);
2345         CL_PRINTF(cli_buf);
2346
2347         /* wifi status */
2348         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2349                    "============[Wifi Status]============");
2350         CL_PRINTF(cli_buf);
2351         btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_WIFI_STATUS);
2352
2353         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2354                    "============[BT Status]============");
2355         CL_PRINTF(cli_buf);
2356
2357         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s/ %d/ %d] ",
2358                    "BT [status/ rssi/ retryCnt]",
2359                    ((coex_sta->bt_disabled) ? ("disabled") :    ((
2360                    coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan")
2361                            : ((BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2362                                coex_dm->bt_status) ? "non-connected idle" :
2363                 ((BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE == coex_dm->bt_status)
2364                                        ? "connected-idle" : "busy")))),
2365                    coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2366         CL_PRINTF(cli_buf);
2367
2368         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d / %d / %d",
2369                    "SCO/HID/PAN/A2DP",
2370                    bt_link_info->sco_exist, bt_link_info->hid_exist,
2371                    bt_link_info->pan_exist, bt_link_info->a2dp_exist);
2372         CL_PRINTF(cli_buf);
2373         btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO);
2374
2375         bt_info_ext = coex_sta->bt_info_ext;
2376         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s",
2377                    "BT Info A2DP rate",
2378                    (bt_info_ext & BIT(0)) ? "Basic rate" : "EDR rate");
2379         CL_PRINTF(cli_buf);
2380
2381         for (i = 0; i < BT_INFO_SRC_8821A_1ANT_MAX; i++) {
2382                 if (coex_sta->bt_info_c2h_cnt[i]) {
2383                         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2384                                 "\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)",
2385                                    glbt_info_src_8821a_1ant[i],
2386                                    coex_sta->bt_info_c2h[i][0],
2387                                    coex_sta->bt_info_c2h[i][1],
2388                                    coex_sta->bt_info_c2h[i][2],
2389                                    coex_sta->bt_info_c2h[i][3],
2390                                    coex_sta->bt_info_c2h[i][4],
2391                                    coex_sta->bt_info_c2h[i][5],
2392                                    coex_sta->bt_info_c2h[i][6],
2393                                    coex_sta->bt_info_c2h_cnt[i]);
2394                         CL_PRINTF(cli_buf);
2395                 }
2396         }
2397
2398         if (!btcoexist->manual_control) {
2399                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d",
2400                            "SM[LowPenaltyRA]",
2401                            coex_dm->cur_low_penalty_ra);
2402                 CL_PRINTF(cli_buf);
2403
2404                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2405                            "============[mechanisms]============");
2406                 CL_PRINTF(cli_buf);
2407
2408                 ps_tdma_case = coex_dm->cur_ps_tdma;
2409                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2410                         "\r\n %-35s = %02x %02x %02x %02x %02x case-%d (auto:%d)",
2411                            "PS TDMA",
2412                            coex_dm->ps_tdma_para[0], coex_dm->ps_tdma_para[1],
2413                            coex_dm->ps_tdma_para[2], coex_dm->ps_tdma_para[3],
2414                            coex_dm->ps_tdma_para[4], ps_tdma_case,
2415                            coex_dm->auto_tdma_adjust);
2416                 CL_PRINTF(cli_buf);
2417
2418                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ",
2419                            "IgnWlanAct",
2420                            coex_dm->cur_ignore_wlan_act);
2421                 CL_PRINTF(cli_buf);
2422
2423                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x ",
2424                            "Latest error condition(should be 0)",
2425                            coex_dm->error_condition);
2426                 CL_PRINTF(cli_buf);
2427         }
2428
2429         /* Hw setting            */
2430         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
2431                    "============[Hw setting]============");
2432         CL_PRINTF(cli_buf);
2433
2434         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2435                    "backup ARFR1/ARFR2/RL/AMaxTime",
2436                    coex_dm->backup_arfr_cnt1, coex_dm->backup_arfr_cnt2,
2437                    coex_dm->backup_retry_limit,
2438                    coex_dm->backup_ampdu_max_time);
2439         CL_PRINTF(cli_buf);
2440
2441         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2442         u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2443         u16tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2444         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2445         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2446                    "0x430/0x434/0x42a/0x456",
2447                    u32tmp[0], u32tmp[1], u16tmp[0], u8tmp[0]);
2448         CL_PRINTF(cli_buf);
2449
2450         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2451         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc58);
2452         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
2453                    "0x778/ 0xc58[29:25]",
2454                    u8tmp[0], (u32tmp[0] & 0x3e000000) >> 25);
2455         CL_PRINTF(cli_buf);
2456
2457         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x8db);
2458         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x8db[6:5]",
2459                    ((u8tmp[0] & 0x60) >> 5));
2460         CL_PRINTF(cli_buf);
2461
2462         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x975);
2463         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xcb4);
2464         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2465                    "0xcb4[29:28]/0xcb4[7:0]/0x974[9:8]",
2466                    (u32tmp[0] & 0x30000000) >> 28, u32tmp[0] & 0xff,
2467                    u8tmp[0] & 0x3);
2468         CL_PRINTF(cli_buf);
2469
2470
2471         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2472         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2473         u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2474         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2475                    "0x40/0x4c[24:23]/0x64[0]",
2476                    u8tmp[0], ((u32tmp[0] & 0x01800000) >> 23), u8tmp[1] & 0x1);
2477         CL_PRINTF(cli_buf);
2478
2479         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2480         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2481         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
2482                    "0x550(bcn ctrl)/0x522",
2483                    u32tmp[0], u8tmp[0]);
2484         CL_PRINTF(cli_buf);
2485
2486         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2487         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0xc50(dig)",
2488                    u32tmp[0] & 0xff);
2489         CL_PRINTF(cli_buf);
2490
2491         fa_ofdm = btcoexist->btc_phydm_query_PHY_counter(btcoexist,
2492                         PHYDM_INFO_FA_OFDM);
2493         fa_cck = btcoexist->btc_phydm_query_PHY_counter(btcoexist,
2494                         PHYDM_INFO_FA_CCK);
2495         cca_ofdm = btcoexist->btc_phydm_query_PHY_counter(btcoexist,
2496                         PHYDM_INFO_CCA_OFDM);
2497         cca_cck = btcoexist->btc_phydm_query_PHY_counter(btcoexist,
2498                         PHYDM_INFO_CCA_CCK);
2499
2500         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
2501                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2502                    "CCK-CCA/CCK-FA/OFDM-CCA/OFDM-FA",
2503                    cca_cck, fa_cck, cca_ofdm, fa_ofdm);
2504         CL_PRINTF(cli_buf);
2505
2506         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
2507                    "CRC_OK CCK/11g/11n/11n-agg",
2508                    coex_sta->crc_ok_cck, coex_sta->crc_ok_11g,
2509                    coex_sta->crc_ok_11n, coex_sta->crc_ok_11n_vht);
2510         CL_PRINTF(cli_buf);
2511
2512         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
2513                    "CRC_Err CCK/11g/11n/11n-agg",
2514                    coex_sta->crc_err_cck, coex_sta->crc_err_11g,
2515                    coex_sta->crc_err_11n, coex_sta->crc_err_11n_vht);
2516         CL_PRINTF(cli_buf);
2517
2518
2519         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2520         u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2521         u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2522         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
2523                    "0x6c0/0x6c4/0x6c8(coexTable)",
2524                    u32tmp[0], u32tmp[1], u32tmp[2]);
2525         CL_PRINTF(cli_buf);
2526
2527         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
2528                    "0x770(high-pri rx/tx)",
2529                    coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2530         CL_PRINTF(cli_buf);
2531         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
2532                    "0x774(low-pri rx/tx)",
2533                    coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2534         CL_PRINTF(cli_buf);
2535 #if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 1)
2536         halbtc8821a1ant_monitor_bt_ctr(btcoexist);
2537 #endif
2538         btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS);
2539 }
2540
2541
2542 void ex_halbtc8821a1ant_ips_notify(IN struct btc_coexist *btcoexist, IN u8 type)
2543 {
2544         boolean wifi_under_5g = false;
2545
2546         if (btcoexist->manual_control ||        btcoexist->stop_coex_dm)
2547                 return;
2548         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G,
2549                            &wifi_under_5g);
2550         if (wifi_under_5g) {
2551                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2552                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2553                 BTC_TRACE(trace_buf);
2554                 halbtc8821a1ant_coex_under_5g(btcoexist);
2555                 return;
2556         }
2557
2558         if (BTC_IPS_ENTER == type) {
2559                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2560                             "[BTCoex], IPS ENTER notify\n");
2561                 BTC_TRACE(trace_buf);
2562                 coex_sta->under_ips = true;
2563
2564                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2565                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2566                 halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
2567                                              true);
2568                 /* halbtc8821a1ant_set_ant_path_d_cut(btcoexist, false, false, false, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF); */
2569         } else if (BTC_IPS_LEAVE == type) {
2570                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2571                             "[BTCoex], IPS LEAVE notify\n");
2572                 BTC_TRACE(trace_buf);
2573                 coex_sta->under_ips = false;
2574
2575                 halbtc8821a1ant_init_hw_config(btcoexist, false, false);
2576                 halbtc8821a1ant_init_coex_dm(btcoexist);
2577                 halbtc8821a1ant_query_bt_info(btcoexist);
2578         }
2579 }
2580
2581 void ex_halbtc8821a1ant_lps_notify(IN struct btc_coexist *btcoexist, IN u8 type)
2582 {
2583
2584
2585         if (BTC_LPS_ENABLE == type) {
2586                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2587                             "[BTCoex], LPS ENABLE notify\n");
2588                 BTC_TRACE(trace_buf);
2589                 coex_sta->under_lps = true;
2590         } else if (BTC_LPS_DISABLE == type) {
2591                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2592                             "[BTCoex], LPS DISABLE notify\n");
2593                 BTC_TRACE(trace_buf);
2594                 coex_sta->under_lps = false;
2595         }
2596 }
2597
2598 void ex_halbtc8821a1ant_scan_notify(IN struct btc_coexist *btcoexist,
2599                                     IN u8 type)
2600 {
2601         boolean wifi_connected = false, bt_hs_on = false;
2602         u32     wifi_link_status = 0;
2603         u32     num_of_wifi_link = 0;
2604         boolean bt_ctrl_agg_buf_size = false;
2605         u8      agg_buf_size = 5;
2606         boolean wifi_under_5g = false;
2607
2608         if (btcoexist->manual_control ||
2609             btcoexist->stop_coex_dm)
2610                 return;
2611         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2612         if (wifi_under_5g) {
2613                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2614                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2615                 BTC_TRACE(trace_buf);
2616                 halbtc8821a1ant_coex_under_5g(btcoexist);
2617                 return;
2618         }
2619
2620         if (BTC_SCAN_START == type) {
2621                 coex_sta->wifi_is_high_pri_task = true;
2622                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2623                             "[BTCoex], SCAN START notify\n");
2624                 BTC_TRACE(trace_buf);
2625
2626                 halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false,
2627                         8);  /* Force antenna setup for no scan result issue */
2628         } else {
2629                 coex_sta->wifi_is_high_pri_task = false;
2630                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2631                             "[BTCoex], SCAN FINISH notify\n");
2632                 BTC_TRACE(trace_buf);
2633         }
2634
2635         if (coex_sta->bt_disabled)
2636                 return;
2637
2638         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2639         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2640                            &wifi_connected);
2641
2642         halbtc8821a1ant_query_bt_info(btcoexist);
2643
2644         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2645                            &wifi_link_status);
2646         num_of_wifi_link = wifi_link_status >> 16;
2647         if (num_of_wifi_link >= 2) {
2648                 halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2649                 halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2650                                            bt_ctrl_agg_buf_size, agg_buf_size);
2651                 halbtc8821a1ant_action_wifi_multi_port(btcoexist);
2652                 return;
2653         }
2654
2655         if (coex_sta->c2h_bt_inquiry_page) {
2656                 halbtc8821a1ant_action_bt_inquiry(btcoexist);
2657                 return;
2658         } else if (bt_hs_on) {
2659                 halbtc8821a1ant_action_hs(btcoexist);
2660                 return;
2661         }
2662
2663         if (BTC_SCAN_START == type) {
2664                 if (!wifi_connected)    /* non-connected scan */
2665                         halbtc8821a1ant_action_wifi_not_connected_scan(
2666                                 btcoexist);
2667                 else    /* wifi is connected */
2668                         halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
2669         } else if (BTC_SCAN_FINISH == type) {
2670                 if (!wifi_connected)    /* non-connected scan */
2671                         halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2672                 else
2673                         halbtc8821a1ant_action_wifi_connected(btcoexist);
2674         }
2675 }
2676
2677 /* copy scan notify content to switch band notify */
2678 void ex_halbtc8821a1ant_switchband_notify(IN struct btc_coexist *btcoexist,
2679                 IN u8 type)
2680 {
2681         boolean wifi_connected = false, bt_hs_on = false;
2682         u32     wifi_link_status = 0;
2683         u32     num_of_wifi_link = 0;
2684         boolean bt_ctrl_agg_buf_size = false;
2685         u8      agg_buf_size = 5;
2686         boolean wifi_under_5g = false;
2687
2688         if (btcoexist->manual_control ||
2689             btcoexist->stop_coex_dm)
2690                 return;
2691         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2692         if (wifi_under_5g) {
2693                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2694                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2695                 BTC_TRACE(trace_buf);
2696                 halbtc8821a1ant_coex_under_5g(btcoexist);
2697                 return;
2698         }
2699
2700         if (BTC_SCAN_START == type) {
2701                 coex_sta->wifi_is_high_pri_task = true;
2702                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2703                             "[BTCoex], SCAN START notify\n");
2704                 BTC_TRACE(trace_buf);
2705
2706                 halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false,
2707                         8);  /* Force antenna setup for no scan result issue */
2708         } else {
2709                 coex_sta->wifi_is_high_pri_task = false;
2710                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2711                             "[BTCoex], SCAN FINISH notify\n");
2712                 BTC_TRACE(trace_buf);
2713         }
2714
2715         if (coex_sta->bt_disabled)
2716                 return;
2717
2718         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2719         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2720                            &wifi_connected);
2721
2722         halbtc8821a1ant_query_bt_info(btcoexist);
2723
2724         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2725                            &wifi_link_status);
2726         num_of_wifi_link = wifi_link_status >> 16;
2727         if (num_of_wifi_link >= 2) {
2728                 halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2729                 halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2730                                            bt_ctrl_agg_buf_size, agg_buf_size);
2731                 halbtc8821a1ant_action_wifi_multi_port(btcoexist);
2732                 return;
2733         }
2734
2735         if (coex_sta->c2h_bt_inquiry_page) {
2736                 halbtc8821a1ant_action_bt_inquiry(btcoexist);
2737                 return;
2738         } else if (bt_hs_on) {
2739                 halbtc8821a1ant_action_hs(btcoexist);
2740                 return;
2741         }
2742
2743         if (BTC_SCAN_START == type) {
2744                 if (!wifi_connected)    /* non-connected scan */
2745                         halbtc8821a1ant_action_wifi_not_connected_scan(
2746                                 btcoexist);
2747                 else    /* wifi is connected */
2748                         halbtc8821a1ant_action_wifi_connected_scan(btcoexist);
2749         } else if (BTC_SCAN_FINISH == type) {
2750                 if (!wifi_connected)    /* non-connected scan */
2751                         halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2752                 else
2753                         halbtc8821a1ant_action_wifi_connected(btcoexist);
2754         }
2755 }
2756 void ex_halbtc8821a1ant_connect_notify(IN struct btc_coexist *btcoexist,
2757                                        IN u8 type)
2758 {
2759         boolean wifi_connected = false, bt_hs_on = false;
2760         u32     wifi_link_status = 0;
2761         u32     num_of_wifi_link = 0;
2762         boolean bt_ctrl_agg_buf_size = false;
2763         u8      agg_buf_size = 5;
2764         boolean wifi_under_5g = false;
2765
2766         if (btcoexist->manual_control ||
2767             btcoexist->stop_coex_dm ||
2768             coex_sta->bt_disabled)
2769                 return;
2770         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2771         if (wifi_under_5g) {
2772                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2773                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2774                 BTC_TRACE(trace_buf);
2775                 halbtc8821a1ant_coex_under_5g(btcoexist);
2776                 return;
2777         }
2778
2779         if (BTC_ASSOCIATE_START == type) {
2780                 coex_sta->wifi_is_high_pri_task = true;
2781                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2782                             "[BTCoex], CONNECT START notify\n");
2783                 BTC_TRACE(trace_buf);
2784                 coex_dm->arp_cnt = 0;
2785         } else {
2786                 coex_sta->wifi_is_high_pri_task = false;
2787                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2788                             "[BTCoex], CONNECT FINISH notify\n");
2789                 BTC_TRACE(trace_buf);
2790                 coex_dm->arp_cnt = 0;
2791         }
2792
2793         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2794                            &wifi_link_status);
2795         num_of_wifi_link = wifi_link_status >> 16;
2796         if (num_of_wifi_link >= 2) {
2797                 halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2798                 halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2799                                            bt_ctrl_agg_buf_size, agg_buf_size);
2800                 halbtc8821a1ant_action_wifi_multi_port(btcoexist);
2801                 return;
2802         }
2803
2804         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2805         if (coex_sta->c2h_bt_inquiry_page) {
2806                 halbtc8821a1ant_action_bt_inquiry(btcoexist);
2807                 return;
2808         } else if (bt_hs_on) {
2809                 halbtc8821a1ant_action_hs(btcoexist);
2810                 return;
2811         }
2812
2813         if (BTC_ASSOCIATE_START == type)
2814                 halbtc8821a1ant_action_wifi_not_connected_asso_auth(btcoexist);
2815         else if (BTC_ASSOCIATE_FINISH == type) {
2816                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2817                                    &wifi_connected);
2818                 if (!wifi_connected) /* non-connected scan */
2819                         halbtc8821a1ant_action_wifi_not_connected(btcoexist);
2820                 else
2821                         halbtc8821a1ant_action_wifi_connected(btcoexist);
2822         }
2823 }
2824
2825 void ex_halbtc8821a1ant_media_status_notify(IN struct btc_coexist *btcoexist,
2826                 IN u8 type)
2827 {
2828         u8                      h2c_parameter[3] = {0};
2829         u32                     wifi_bw;
2830         u8                      wifi_central_chnl;
2831         boolean wifi_under_5g = false;
2832
2833         if (btcoexist->manual_control ||
2834             btcoexist->stop_coex_dm ||
2835             coex_sta->bt_disabled)
2836                 return;
2837         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2838         if (wifi_under_5g) {
2839                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2840                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2841                 BTC_TRACE(trace_buf);
2842                 halbtc8821a1ant_coex_under_5g(btcoexist);
2843                 return;
2844         }
2845
2846         if (BTC_MEDIA_CONNECT == type) {
2847                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2848                             "[BTCoex], MEDIA connect notify\n");
2849                 BTC_TRACE(trace_buf);
2850         } else {
2851                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2852                             "[BTCoex], MEDIA disconnect notify\n");
2853                 BTC_TRACE(trace_buf);
2854                 coex_dm->arp_cnt = 0;
2855         }
2856
2857         /* only 2.4G we need to inform bt the chnl mask */
2858         btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL,
2859                            &wifi_central_chnl);
2860         if ((BTC_MEDIA_CONNECT == type) &&
2861             (wifi_central_chnl <= 14)) {
2862                 /* h2c_parameter[0] = 0x1; */
2863                 h2c_parameter[0] = 0x0;
2864                 h2c_parameter[1] = wifi_central_chnl;
2865                 btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2866                 if (BTC_WIFI_BW_HT40 == wifi_bw)
2867                         h2c_parameter[2] = 0x30;
2868                 else
2869                         h2c_parameter[2] = 0x20;
2870         }
2871
2872         coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2873         coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2874         coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2875
2876         btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2877 }
2878
2879 void ex_halbtc8821a1ant_specific_packet_notify(IN struct btc_coexist *btcoexist,
2880                 IN u8 type)
2881 {
2882         boolean bt_hs_on = false;
2883         u32     wifi_link_status = 0;
2884         u32     num_of_wifi_link = 0;
2885         boolean bt_ctrl_agg_buf_size = false;
2886         u8      agg_buf_size = 5;
2887         boolean wifi_under_5g = false;
2888
2889         if (btcoexist->manual_control ||
2890             btcoexist->stop_coex_dm ||
2891             coex_sta->bt_disabled)
2892                 return;
2893         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2894         if (wifi_under_5g) {
2895                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2896                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2897                 BTC_TRACE(trace_buf);
2898                 halbtc8821a1ant_coex_under_5g(btcoexist);
2899                 return;
2900         }
2901
2902         if (BTC_PACKET_DHCP == type ||
2903             BTC_PACKET_EAPOL == type ||
2904             BTC_PACKET_ARP == type) {
2905                 coex_sta->wifi_is_high_pri_task = true;
2906
2907                 if (BTC_PACKET_ARP == type) {
2908                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2909                                     "[BTCoex], specific Packet ARP notify\n");
2910                         BTC_TRACE(trace_buf);
2911                 } else {
2912                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2913                                 "[BTCoex], specific Packet DHCP or EAPOL notify\n");
2914                         BTC_TRACE(trace_buf);
2915                 }
2916         } else {
2917                 coex_sta->wifi_is_high_pri_task = false;
2918                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2919                         "[BTCoex], specific Packet [Type = %d] notify\n", type);
2920                 BTC_TRACE(trace_buf);
2921         }
2922
2923         coex_sta->specific_pkt_period_cnt = 0;
2924
2925         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2926                            &wifi_link_status);
2927         num_of_wifi_link = wifi_link_status >> 16;
2928         if (num_of_wifi_link >= 2) {
2929                 halbtc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2930                 halbtc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2931                                            bt_ctrl_agg_buf_size, agg_buf_size);
2932                 halbtc8821a1ant_action_wifi_multi_port(btcoexist);
2933                 return;
2934         }
2935
2936         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2937         if (coex_sta->c2h_bt_inquiry_page) {
2938                 halbtc8821a1ant_action_bt_inquiry(btcoexist);
2939                 return;
2940         } else if (bt_hs_on) {
2941                 halbtc8821a1ant_action_hs(btcoexist);
2942                 return;
2943         }
2944
2945         if (BTC_PACKET_DHCP == type ||
2946             BTC_PACKET_EAPOL == type ||
2947             BTC_PACKET_ARP == type) {
2948                 if (BTC_PACKET_ARP == type) {
2949                         coex_dm->arp_cnt++;
2950                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2951                                     "[BTCoex], ARP Packet Count = %d\n",
2952                                     coex_dm->arp_cnt);
2953                         BTC_TRACE(trace_buf);
2954                         if (coex_dm->arp_cnt >=
2955                             10) /* if APR PKT > 10 after connect, do not go to ActionWifiConnectedSpecificPacket(btcoexist) */
2956                                 return;
2957                 }
2958
2959                 halbtc8821a1ant_action_wifi_connected_specific_packet(
2960                         btcoexist);
2961         }
2962 }
2963
2964 void ex_halbtc8821a1ant_bt_info_notify(IN struct btc_coexist *btcoexist,
2965                                        IN u8 *tmp_buf, IN u8 length)
2966 {
2967         u8                              bt_info = 0;
2968         u8                              i, rsp_source = 0;
2969         boolean                         wifi_connected = false;
2970         boolean                         bt_busy = false;
2971         boolean                         wifi_under_5g = false;
2972
2973
2974         coex_sta->c2h_bt_info_req_sent = false;
2975
2976         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2977
2978         rsp_source = tmp_buf[0] & 0xf;
2979         if (rsp_source >= BT_INFO_SRC_8821A_1ANT_MAX)
2980                 rsp_source = BT_INFO_SRC_8821A_1ANT_WIFI_FW;
2981         coex_sta->bt_info_c2h_cnt[rsp_source]++;
2982
2983         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2984                     "[BTCoex], Bt info[%d], length=%d, hex data=[", rsp_source,
2985                     length);
2986         BTC_TRACE(trace_buf);
2987         for (i = 0; i < length; i++) {
2988                 coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
2989                 if (i == 1)
2990                         bt_info = tmp_buf[i];
2991                 if (i == length - 1) {
2992                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x]\n",
2993                                     tmp_buf[i]);
2994                         BTC_TRACE(trace_buf);
2995                 } else {
2996                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x, ",
2997                                     tmp_buf[i]);
2998                         BTC_TRACE(trace_buf);
2999                 }
3000         }
3001         /* if 0xff, it means BT is under WHCK test */
3002         if (bt_info == 0xff)
3003                 coex_sta->bt_whck_test = true;
3004         else
3005                 coex_sta->bt_whck_test = false;
3006
3007         if (BT_INFO_SRC_8821A_1ANT_WIFI_FW != rsp_source) {
3008                 coex_sta->bt_retry_cnt =        /* [3:0] */
3009                         coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
3010
3011                 if (coex_sta->bt_info_c2h[rsp_source][2] & 0x20)
3012                         coex_sta->c2h_bt_page = true;
3013                 else
3014                         coex_sta->c2h_bt_page = false;
3015
3016                 coex_sta->bt_rssi =
3017                         coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
3018
3019                 coex_sta->bt_info_ext =
3020                         coex_sta->bt_info_c2h[rsp_source][4];
3021
3022                 coex_sta->bt_tx_rx_mask = (coex_sta->bt_info_c2h[rsp_source][2]
3023                                            & 0x40);
3024                 btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
3025                                    &coex_sta->bt_tx_rx_mask);
3026                 if (!coex_sta->bt_tx_rx_mask) {
3027                         /* BT into is responded by BT FW and BT RF REG 0x3C != 0x15 => Need to switch BT TRx Mask */
3028                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3029                                 "[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
3030                         BTC_TRACE(trace_buf);
3031                         btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
3032                                                   0x3c, 0x15);
3033                 }
3034
3035                 /* Here we need to resend some wifi info to BT */
3036                 /* because bt is reset and loss of the info. */
3037                 if (coex_sta->bt_info_ext & BIT(1)) {
3038                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3039                                 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
3040                         BTC_TRACE(trace_buf);
3041                         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3042                                            &wifi_connected);
3043                         if (wifi_connected)
3044                                 ex_halbtc8821a1ant_media_status_notify(
3045                                         btcoexist, BTC_MEDIA_CONNECT);
3046                         else
3047                                 ex_halbtc8821a1ant_media_status_notify(
3048                                         btcoexist, BTC_MEDIA_DISCONNECT);
3049                 }
3050
3051                 if ((coex_sta->bt_info_ext & BIT(3)) && !wifi_under_5g) {
3052                         if (!btcoexist->manual_control &&
3053                             !btcoexist->stop_coex_dm) {
3054                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3055                                         "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
3056                                 BTC_TRACE(trace_buf);
3057                                 halbtc8821a1ant_ignore_wlan_act(btcoexist,
3058                                                         FORCE_EXEC, false);
3059                         }
3060                 } else {
3061                         /* BT already NOT ignore Wlan active, do nothing here. */
3062                 }
3063 #if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 0)
3064                 if ((coex_sta->bt_info_ext & BIT(4))) {
3065                         /* BT auto report already enabled, do nothing */
3066                 } else
3067                         halbtc8821a1ant_bt_auto_report(btcoexist, FORCE_EXEC,
3068                                                        true);
3069 #endif
3070         }
3071
3072         /* check BIT2 first ==> check if bt is under inquiry or page scan */
3073         if (bt_info & BT_INFO_8821A_1ANT_B_INQ_PAGE)
3074                 coex_sta->c2h_bt_inquiry_page = true;
3075         else
3076                 coex_sta->c2h_bt_inquiry_page = false;
3077
3078         /* set link exist status */
3079         if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
3080                 coex_sta->bt_link_exist = false;
3081                 coex_sta->pan_exist = false;
3082                 coex_sta->a2dp_exist = false;
3083                 coex_sta->hid_exist = false;
3084                 coex_sta->sco_exist = false;
3085         } else {        /* connection exists */
3086                 coex_sta->bt_link_exist = true;
3087                 if (bt_info & BT_INFO_8821A_1ANT_B_FTP)
3088                         coex_sta->pan_exist = true;
3089                 else
3090                         coex_sta->pan_exist = false;
3091                 if (bt_info & BT_INFO_8821A_1ANT_B_A2DP)
3092                         coex_sta->a2dp_exist = true;
3093                 else
3094                         coex_sta->a2dp_exist = false;
3095                 if (bt_info & BT_INFO_8821A_1ANT_B_HID)
3096                         coex_sta->hid_exist = true;
3097                 else
3098                         coex_sta->hid_exist = false;
3099                 if (bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO)
3100                         coex_sta->sco_exist = true;
3101                 else
3102                         coex_sta->sco_exist = false;
3103         }
3104
3105         halbtc8821a1ant_update_bt_link_info(btcoexist);
3106
3107         bt_info = bt_info &
3108                 0x1f;  /* mask profile bit for connect-ilde identification ( for CSR case: A2DP idle --> 0x41) */
3109
3110         if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
3111                 coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
3112                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3113                         "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
3114                 BTC_TRACE(trace_buf);
3115         } else if (bt_info ==
3116                 BT_INFO_8821A_1ANT_B_CONNECTION) {      /* connection exists but no busy */
3117                 coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE;
3118                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3119                             "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
3120                 BTC_TRACE(trace_buf);
3121         } else if ((bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO) ||
3122                    (bt_info & BT_INFO_8821A_1ANT_B_SCO_BUSY)) {
3123                 coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_SCO_BUSY;
3124                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3125                             "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
3126                 BTC_TRACE(trace_buf);
3127         } else if (bt_info & BT_INFO_8821A_1ANT_B_ACL_BUSY) {
3128                 if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
3129                         coex_dm->auto_tdma_adjust = false;
3130                 coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_ACL_BUSY;
3131                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3132                             "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
3133                 BTC_TRACE(trace_buf);
3134         } else {
3135                 coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_MAX;
3136                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3137                         "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
3138                 BTC_TRACE(trace_buf);
3139         }
3140
3141         if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
3142             (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
3143             (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
3144                 bt_busy = true;
3145         else
3146                 bt_busy = false;
3147         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
3148
3149         halbtc8821a1ant_run_coexist_mechanism(btcoexist);
3150 }
3151
3152 void ex_halbtc8821a1ant_halt_notify(IN struct btc_coexist *btcoexist)
3153 {
3154         boolean wifi_under_5g = false;
3155         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
3156         if (wifi_under_5g) {
3157                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3158                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
3159                 BTC_TRACE(trace_buf);
3160                 halbtc8821a1ant_coex_under_5g(btcoexist);
3161                 return;
3162         }
3163
3164         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Halt notify\n");
3165         BTC_TRACE(trace_buf);
3166
3167         halbtc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
3168         halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false, true);
3169         /* halbtc8821a1ant_set_ant_path_d_cut(btcoexist, false, false, false, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF); */
3170
3171         halbtc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
3172
3173         ex_halbtc8821a1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
3174
3175         btcoexist->stop_coex_dm = true;
3176 }
3177
3178 void ex_halbtc8821a1ant_pnp_notify(IN struct btc_coexist *btcoexist,
3179                                    IN u8 pnp_state)
3180 {
3181         boolean wifi_under_5g = false;
3182
3183         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
3184         if (wifi_under_5g) {
3185                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3186                         "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
3187                 BTC_TRACE(trace_buf);
3188                 halbtc8821a1ant_coex_under_5g(btcoexist);
3189                 return;
3190         }
3191         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Pnp notify\n");
3192         BTC_TRACE(trace_buf);
3193
3194         if (BTC_WIFI_PNP_SLEEP == pnp_state) {
3195                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3196                             "[BTCoex], Pnp notify to SLEEP\n");
3197                 BTC_TRACE(trace_buf);
3198
3199                 halbtc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
3200                 halbtc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
3201                 halbtc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
3202                                              true);
3203                 /* halbtc8821a1ant_set_ant_path_d_cut(btcoexist, false, false, false, BTC_ANT_PATH_BT, BTC_WIFI_STAT_NORMAL_OFF); */
3204
3205                 /* Sinda 20150819, workaround for driver skip leave IPS/LPS to speed up sleep time. */
3206                 /* Driver do not leave IPS/LPS when driver is going to sleep, so BTCoexistence think wifi is still under IPS/LPS */
3207                 /* BT should clear UnderIPS/UnderLPS state to avoid mismatch state after wakeup. */
3208                 coex_sta->under_ips = false;
3209                 coex_sta->under_lps = false;
3210                 btcoexist->stop_coex_dm = true;
3211         } else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
3212                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3213                             "[BTCoex], Pnp notify to WAKE UP\n");
3214                 BTC_TRACE(trace_buf);
3215                 btcoexist->stop_coex_dm = false;
3216                 halbtc8821a1ant_init_hw_config(btcoexist, false, false);
3217                 halbtc8821a1ant_init_coex_dm(btcoexist);
3218                 halbtc8821a1ant_query_bt_info(btcoexist);
3219         }
3220 }
3221
3222 void ex_halbtc8821a1ant_periodical(IN struct btc_coexist *btcoexist)
3223 {
3224         u32 bt_patch_ver;
3225
3226         if (((coex_sta->bt_coex_supported_version == 0) ||
3227              (coex_sta->bt_coex_supported_version == 0xffff)) && (!coex_sta->bt_disabled))
3228                 btcoexist->btc_get(btcoexist, BTC_GET_U4_SUPPORTED_VERSION, &coex_sta->bt_coex_supported_version);
3229
3230         btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
3231         btcoexist->bt_info.bt_get_fw_ver = bt_patch_ver;
3232
3233 #if (BT_AUTO_REPORT_ONLY_8821A_1ANT == 0)
3234         halbtc8821a1ant_query_bt_info(btcoexist);
3235         halbtc8821a1ant_monitor_bt_enable_disable(btcoexist);
3236 #else
3237         halbtc8821a1ant_monitor_bt_ctr(btcoexist);
3238         halbtc8821a1ant_monitor_wifi_ctr(btcoexist);
3239         halbtc8821a1ant_monitor_bt_enable_disable(btcoexist);
3240         if (halbtc8821a1ant_is_wifi_status_changed(btcoexist) ||
3241             coex_dm->auto_tdma_adjust) {
3242                 /* if(coex_sta->specific_pkt_period_cnt > 2) */
3243                 /* { */
3244                 halbtc8821a1ant_run_coexist_mechanism(btcoexist);
3245                 /* } */
3246         }
3247
3248         coex_sta->specific_pkt_period_cnt++;
3249 #endif
3250 }
3251
3252 #endif
3253
3254 #endif  /* #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1) */