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