Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2012  Realtek Corporation.*/
0003 
0004 /**************************************************************
0005  * Description:
0006  *
0007  * This file is for RTL8821A Co-exist mechanism
0008  *
0009  * History
0010  * 2012/11/15 Cosa first check in.
0011  *
0012  **************************************************************/
0013 
0014 /**************************************************************
0015  * include files
0016  **************************************************************/
0017 #include "halbt_precomp.h"
0018 /**************************************************************
0019  * Global variables, these are static variables
0020  **************************************************************/
0021 static struct coex_dm_8821a_1ant glcoex_dm_8821a_1ant;
0022 static struct coex_dm_8821a_1ant *coex_dm = &glcoex_dm_8821a_1ant;
0023 static struct coex_sta_8821a_1ant glcoex_sta_8821a_1ant;
0024 static struct coex_sta_8821a_1ant *coex_sta = &glcoex_sta_8821a_1ant;
0025 static void btc8821a1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
0026                           u8 wifi_status);
0027 
0028 static const char *const glbt_info_src_8821a_1ant[] = {
0029       "BT Info[wifi fw]",
0030       "BT Info[bt rsp]",
0031       "BT Info[bt auto report]",
0032 };
0033 
0034 static u32 glcoex_ver_date_8821a_1ant = 20130816;
0035 static u32 glcoex_ver_8821a_1ant = 0x41;
0036 
0037 /**************************************************************
0038  * local function proto type if needed
0039  *
0040  * local function start with btc8821a1ant_
0041  **************************************************************/
0042 static u8 btc8821a1ant_bt_rssi_state(struct btc_coexist *btcoexist,
0043                      u8 level_num, u8 rssi_thresh,
0044                      u8 rssi_thresh1)
0045 {
0046     struct rtl_priv *rtlpriv = btcoexist->adapter;
0047     long bt_rssi = 0;
0048     u8 bt_rssi_state = coex_sta->pre_bt_rssi_state;
0049 
0050     bt_rssi = coex_sta->bt_rssi;
0051 
0052     if (level_num == 2) {
0053         if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
0054             (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
0055             if (bt_rssi >= (rssi_thresh +
0056                     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
0057                 bt_rssi_state = BTC_RSSI_STATE_HIGH;
0058                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0059                     "[BTCoex], BT Rssi state switch to High\n");
0060             } else {
0061                 bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
0062                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0063                     "[BTCoex], BT Rssi state stay at Low\n");
0064             }
0065         } else {
0066             if (bt_rssi < rssi_thresh) {
0067                 bt_rssi_state = BTC_RSSI_STATE_LOW;
0068                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0069                     "[BTCoex], BT Rssi state switch to Low\n");
0070             } else {
0071                 bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
0072                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0073                     "[BTCoex], BT Rssi state stay at High\n");
0074             }
0075         }
0076     } else if (level_num == 3) {
0077         if (rssi_thresh > rssi_thresh1) {
0078             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0079                 "[BTCoex], BT Rssi thresh error!!\n");
0080             return coex_sta->pre_bt_rssi_state;
0081         }
0082 
0083         if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
0084             (coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_STAY_LOW)) {
0085             if (bt_rssi >= (rssi_thresh +
0086                     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
0087                 bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
0088                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0089                     "[BTCoex], BT Rssi state switch to Medium\n");
0090             } else {
0091                 bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
0092                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0093                     "[BTCoex], BT Rssi state stay at Low\n");
0094             }
0095         } else if ((coex_sta->pre_bt_rssi_state ==
0096                BTC_RSSI_STATE_MEDIUM) ||
0097                (coex_sta->pre_bt_rssi_state ==
0098                 BTC_RSSI_STATE_STAY_MEDIUM)) {
0099             if (bt_rssi >= (rssi_thresh1 +
0100                     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
0101                 bt_rssi_state = BTC_RSSI_STATE_HIGH;
0102                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0103                     "[BTCoex], BT Rssi state switch to High\n");
0104             } else if (bt_rssi < rssi_thresh) {
0105                 bt_rssi_state = BTC_RSSI_STATE_LOW;
0106                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0107                     "[BTCoex], BT Rssi state switch to Low\n");
0108             } else {
0109                 bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
0110                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0111                     "[BTCoex], BT Rssi state stay at Medium\n");
0112             }
0113         } else {
0114             if (bt_rssi < rssi_thresh1) {
0115                 bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
0116                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0117                     "[BTCoex], BT Rssi state switch to Medium\n");
0118             } else {
0119                 bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
0120                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0121                     "[BTCoex], BT Rssi state stay at High\n");
0122             }
0123         }
0124     }
0125     coex_sta->pre_bt_rssi_state = bt_rssi_state;
0126 
0127     return bt_rssi_state;
0128 }
0129 
0130 static u8 btc8821a1ant_wifi_rssi_state(struct btc_coexist *btcoexist,
0131                        u8 index, u8 level_num, u8 rssi_thresh,
0132                        u8 rssi_thresh1)
0133 {
0134     struct rtl_priv *rtlpriv = btcoexist->adapter;
0135     long    wifi_rssi = 0;
0136     u8  wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
0137 
0138     btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
0139 
0140     if (level_num == 2) {
0141         if ((coex_sta->pre_wifi_rssi_state[index] ==
0142              BTC_RSSI_STATE_LOW) ||
0143             (coex_sta->pre_wifi_rssi_state[index] ==
0144              BTC_RSSI_STATE_STAY_LOW)) {
0145             if (wifi_rssi >= (rssi_thresh +
0146                     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
0147                 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
0148                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0149                     "[BTCoex], wifi RSSI state switch to High\n");
0150             } else {
0151                 wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
0152                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0153                     "[BTCoex], wifi RSSI state stay at Low\n");
0154             }
0155         } else {
0156             if (wifi_rssi < rssi_thresh) {
0157                 wifi_rssi_state = BTC_RSSI_STATE_LOW;
0158                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0159                     "[BTCoex], wifi RSSI state switch to Low\n");
0160             } else {
0161                 wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
0162                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0163                     "[BTCoex], wifi RSSI state stay at High\n");
0164             }
0165         }
0166     } else if (level_num == 3) {
0167         if (rssi_thresh > rssi_thresh1) {
0168             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0169                 "[BTCoex], wifi RSSI thresh error!!\n");
0170             return coex_sta->pre_wifi_rssi_state[index];
0171         }
0172 
0173         if ((coex_sta->pre_wifi_rssi_state[index] ==
0174              BTC_RSSI_STATE_LOW) ||
0175             (coex_sta->pre_wifi_rssi_state[index] ==
0176              BTC_RSSI_STATE_STAY_LOW)) {
0177             if (wifi_rssi >= (rssi_thresh +
0178                     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
0179                 wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
0180                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0181                     "[BTCoex], wifi RSSI state switch to Medium\n");
0182             } else {
0183                 wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
0184                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0185                     "[BTCoex], wifi RSSI state stay at Low\n");
0186             }
0187         } else if ((coex_sta->pre_wifi_rssi_state[index] ==
0188             BTC_RSSI_STATE_MEDIUM) ||
0189             (coex_sta->pre_wifi_rssi_state[index] ==
0190             BTC_RSSI_STATE_STAY_MEDIUM)) {
0191             if (wifi_rssi >= (rssi_thresh1 +
0192                     BTC_RSSI_COEX_THRESH_TOL_8821A_1ANT)) {
0193                 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
0194                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0195                     "[BTCoex], wifi RSSI state switch to High\n");
0196             } else if (wifi_rssi < rssi_thresh) {
0197                 wifi_rssi_state = BTC_RSSI_STATE_LOW;
0198                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0199                     "[BTCoex], wifi RSSI state switch to Low\n");
0200             } else {
0201                 wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
0202                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0203                     "[BTCoex], wifi RSSI state stay at Medium\n");
0204             }
0205         } else {
0206             if (wifi_rssi < rssi_thresh1) {
0207                 wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
0208                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0209                     "[BTCoex], wifi RSSI state switch to Medium\n");
0210             } else {
0211                 wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
0212                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0213                     "[BTCoex], wifi RSSI state stay at High\n");
0214             }
0215         }
0216     }
0217     coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
0218 
0219     return wifi_rssi_state;
0220 }
0221 
0222 static void btc8821a1ant_update_ra_mask(struct btc_coexist *btcoexist,
0223                     bool force_exec, u32 dis_rate_mask)
0224 {
0225     coex_dm->cur_ra_mask = dis_rate_mask;
0226 
0227     if (force_exec ||
0228         (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask)) {
0229         btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_RAMASK,
0230                    &coex_dm->cur_ra_mask);
0231     }
0232     coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
0233 }
0234 
0235 static void btc8821a1ant_auto_rate_fb_retry(struct btc_coexist *btcoexist,
0236                         bool force_exec, u8 type)
0237 {
0238     bool wifi_under_b_mode = false;
0239 
0240     coex_dm->cur_arfr_type = type;
0241 
0242     if (force_exec ||
0243         (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
0244         switch (coex_dm->cur_arfr_type) {
0245         case 0: /* normal mode */
0246             btcoexist->btc_write_4byte(btcoexist, 0x430,
0247                            coex_dm->backup_arfr_cnt1);
0248             btcoexist->btc_write_4byte(btcoexist, 0x434,
0249                            coex_dm->backup_arfr_cnt2);
0250             break;
0251         case 1:
0252             btcoexist->btc_get(btcoexist,
0253                        BTC_GET_BL_WIFI_UNDER_B_MODE,
0254                        &wifi_under_b_mode);
0255             if (wifi_under_b_mode) {
0256                 btcoexist->btc_write_4byte(btcoexist, 0x430,
0257                                0x0);
0258                 btcoexist->btc_write_4byte(btcoexist, 0x434,
0259                                0x01010101);
0260             } else {
0261                 btcoexist->btc_write_4byte(btcoexist, 0x430,
0262                                0x0);
0263                 btcoexist->btc_write_4byte(btcoexist, 0x434,
0264                                0x04030201);
0265             }
0266             break;
0267         default:
0268             break;
0269         }
0270     }
0271 
0272     coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
0273 }
0274 
0275 static void btc8821a1ant_retry_limit(struct btc_coexist *btcoexist,
0276                      bool force_exec, u8 type)
0277 {
0278     coex_dm->cur_retry_limit_type = type;
0279 
0280     if (force_exec ||
0281         (coex_dm->pre_retry_limit_type != coex_dm->cur_retry_limit_type)) {
0282         switch (coex_dm->cur_retry_limit_type) {
0283         case 0: /* normal mode */
0284             btcoexist->btc_write_2byte(btcoexist, 0x42a,
0285                            coex_dm->backup_retry_limit);
0286             break;
0287         case 1: /* retry limit = 8 */
0288             btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
0289             break;
0290         default:
0291             break;
0292         }
0293     }
0294     coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
0295 }
0296 
0297 static void btc8821a1ant_ampdu_max_time(struct btc_coexist *btcoexist,
0298                     bool force_exec, u8 type)
0299 {
0300     coex_dm->cur_ampdu_time_type = type;
0301 
0302     if (force_exec ||
0303         (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
0304         switch (coex_dm->cur_ampdu_time_type) {
0305         case 0: /* normal mode */
0306             btcoexist->btc_write_1byte(btcoexist, 0x456,
0307                            coex_dm->backup_ampdu_max_time);
0308             break;
0309         case 1: /* AMPDU time = 0x38 * 32us */
0310             btcoexist->btc_write_1byte(btcoexist, 0x456, 0x38);
0311             break;
0312         default:
0313             break;
0314         }
0315     }
0316 
0317     coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
0318 }
0319 
0320 static void btc8821a1ant_limited_tx(struct btc_coexist *btcoexist,
0321                     bool force_exec, u8 ra_mask_type,
0322                     u8 arfr_type, u8 retry_limit_type,
0323                     u8 ampdu_time_type)
0324 {
0325     switch (ra_mask_type) {
0326     case 0: /* normal mode */
0327         btc8821a1ant_update_ra_mask(btcoexist, force_exec, 0x0);
0328         break;
0329     case 1: /* disable cck 1/2 */
0330         btc8821a1ant_update_ra_mask(btcoexist, force_exec,
0331                         0x00000003);
0332         break;
0333     case 2: /* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4 */
0334         btc8821a1ant_update_ra_mask(btcoexist, force_exec,
0335                         0x0001f1f7);
0336         break;
0337     default:
0338         break;
0339     }
0340 
0341     btc8821a1ant_auto_rate_fb_retry(btcoexist, force_exec, arfr_type);
0342     btc8821a1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
0343     btc8821a1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
0344 }
0345 
0346 static void btc8821a1ant_limited_rx(struct btc_coexist *btcoexist,
0347                     bool force_exec, bool rej_ap_agg_pkt,
0348                     bool bt_ctrl_agg_buf_size, u8 agg_buf_size)
0349 {
0350     bool reject_rx_agg = rej_ap_agg_pkt;
0351     bool bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
0352     u8 rx_agg_size = agg_buf_size;
0353 
0354     /* Rx Aggregation related setting */
0355     btcoexist->btc_set(btcoexist,
0356          BTC_SET_BL_TO_REJ_AP_AGG_PKT, &reject_rx_agg);
0357     /* decide BT control aggregation buf size or not */
0358     btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
0359                &bt_ctrl_rx_agg_size);
0360     /* aggregation buf size, only work when BT control Rx agg size */
0361     btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
0362     /* real update aggregation setting */
0363     btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
0364 }
0365 
0366 static void btc8821a1ant_monitor_bt_ctr(struct btc_coexist *btcoexist)
0367 {
0368     u32 reg_hp_tx_rx, reg_lp_tx_rx, u4_tmp;
0369     u32 reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
0370 
0371     reg_hp_tx_rx = 0x770;
0372     reg_lp_tx_rx = 0x774;
0373 
0374     u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_tx_rx);
0375     reg_hp_tx = u4_tmp & MASKLWORD;
0376     reg_hp_rx = (u4_tmp & MASKHWORD) >> 16;
0377 
0378     u4_tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_tx_rx);
0379     reg_lp_tx = u4_tmp & MASKLWORD;
0380     reg_lp_rx = (u4_tmp & MASKHWORD) >> 16;
0381 
0382     coex_sta->high_priority_tx = reg_hp_tx;
0383     coex_sta->high_priority_rx = reg_hp_rx;
0384     coex_sta->low_priority_tx = reg_lp_tx;
0385     coex_sta->low_priority_rx = reg_lp_rx;
0386 
0387     /* reset counter */
0388     btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
0389 }
0390 
0391 static void btc8821a1ant_query_bt_info(struct btc_coexist *btcoexist)
0392 {
0393     struct rtl_priv *rtlpriv = btcoexist->adapter;
0394     u8 h2c_parameter[1] = {0};
0395 
0396     coex_sta->c2h_bt_info_req_sent = true;
0397 
0398     h2c_parameter[0] |= BIT0; /* trigger */
0399 
0400     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0401         "[BTCoex], Query Bt Info, FW write 0x61 = 0x%x\n",
0402         h2c_parameter[0]);
0403 
0404     btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
0405 }
0406 
0407 static void btc8821a1ant_update_bt_link_info(struct btc_coexist *btcoexist)
0408 {
0409     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
0410     bool bt_hs_on = false;
0411 
0412     btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
0413 
0414     bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
0415     bt_link_info->sco_exist = coex_sta->sco_exist;
0416     bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
0417     bt_link_info->pan_exist = coex_sta->pan_exist;
0418     bt_link_info->hid_exist = coex_sta->hid_exist;
0419 
0420     /* work around for HS mode */
0421     if (bt_hs_on) {
0422         bt_link_info->pan_exist = true;
0423         bt_link_info->bt_link_exist = true;
0424     }
0425 
0426     /* check if Sco only */
0427     if (bt_link_info->sco_exist &&
0428         !bt_link_info->a2dp_exist &&
0429         !bt_link_info->pan_exist &&
0430         !bt_link_info->hid_exist)
0431         bt_link_info->sco_only = true;
0432     else
0433         bt_link_info->sco_only = false;
0434 
0435     /* check if A2dp only */
0436     if (!bt_link_info->sco_exist &&
0437         bt_link_info->a2dp_exist &&
0438         !bt_link_info->pan_exist &&
0439         !bt_link_info->hid_exist)
0440         bt_link_info->a2dp_only = true;
0441     else
0442         bt_link_info->a2dp_only = false;
0443 
0444     /* check if Pan only */
0445     if (!bt_link_info->sco_exist &&
0446         !bt_link_info->a2dp_exist &&
0447         bt_link_info->pan_exist &&
0448         !bt_link_info->hid_exist)
0449         bt_link_info->pan_only = true;
0450     else
0451         bt_link_info->pan_only = false;
0452 
0453     /* check if Hid only */
0454     if (!bt_link_info->sco_exist &&
0455         !bt_link_info->a2dp_exist &&
0456         !bt_link_info->pan_exist &&
0457         bt_link_info->hid_exist)
0458         bt_link_info->hid_only = true;
0459     else
0460         bt_link_info->hid_only = false;
0461 }
0462 
0463 static u8 btc8821a1ant_action_algorithm(struct btc_coexist *btcoexist)
0464 {
0465     struct rtl_priv *rtlpriv = btcoexist->adapter;
0466     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
0467     bool bt_hs_on = false;
0468     u8 algorithm = BT_8821A_1ANT_COEX_ALGO_UNDEFINED;
0469     u8 num_of_diff_profile = 0;
0470 
0471     btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
0472 
0473     if (!bt_link_info->bt_link_exist) {
0474         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0475             "[BTCoex], No BT link exists!!!\n");
0476         return algorithm;
0477     }
0478 
0479     if (bt_link_info->sco_exist)
0480         num_of_diff_profile++;
0481     if (bt_link_info->hid_exist)
0482         num_of_diff_profile++;
0483     if (bt_link_info->pan_exist)
0484         num_of_diff_profile++;
0485     if (bt_link_info->a2dp_exist)
0486         num_of_diff_profile++;
0487 
0488     if (num_of_diff_profile == 1) {
0489         if (bt_link_info->sco_exist) {
0490             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0491                 "[BTCoex], BT Profile = SCO only\n");
0492             algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
0493         } else {
0494             if (bt_link_info->hid_exist) {
0495                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0496                     "[BTCoex], BT Profile = HID only\n");
0497                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
0498             } else if (bt_link_info->a2dp_exist) {
0499                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0500                     "[BTCoex], BT Profile = A2DP only\n");
0501                 algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP;
0502             } else if (bt_link_info->pan_exist) {
0503                 if (bt_hs_on) {
0504                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0505                         DBG_LOUD,
0506                         "[BTCoex], BT Profile = PAN(HS) only\n");
0507                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANHS;
0508                 } else {
0509                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0510                         DBG_LOUD,
0511                         "[BTCoex], BT Profile = PAN(EDR) only\n");
0512                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR;
0513                 }
0514             }
0515         }
0516     } else if (num_of_diff_profile == 2) {
0517         if (bt_link_info->sco_exist) {
0518             if (bt_link_info->hid_exist) {
0519                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0520                     "[BTCoex], BT Profile = SCO + HID\n");
0521                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
0522             } else if (bt_link_info->a2dp_exist) {
0523                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0524                     "[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
0525                 algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
0526             } else if (bt_link_info->pan_exist) {
0527                 if (bt_hs_on) {
0528                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0529                         DBG_LOUD,
0530                         "[BTCoex], BT Profile = SCO + PAN(HS)\n");
0531                     algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
0532                 } else {
0533                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0534                         DBG_LOUD,
0535                         "[BTCoex], BT Profile = SCO + PAN(EDR)\n");
0536                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
0537                 }
0538             }
0539         } else {
0540             if (bt_link_info->hid_exist &&
0541                 bt_link_info->a2dp_exist) {
0542                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0543                     "[BTCoex], BT Profile = HID + A2DP\n");
0544                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
0545             } else if (bt_link_info->hid_exist &&
0546                    bt_link_info->pan_exist) {
0547                 if (bt_hs_on) {
0548                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0549                         DBG_LOUD,
0550                         "[BTCoex], BT Profile = HID + PAN(HS)\n");
0551                     algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
0552                 } else {
0553                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0554                         DBG_LOUD,
0555                         "[BTCoex], BT Profile = HID + PAN(EDR)\n");
0556                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
0557                 }
0558             } else if (bt_link_info->pan_exist &&
0559                    bt_link_info->a2dp_exist) {
0560                 if (bt_hs_on) {
0561                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0562                         DBG_LOUD,
0563                         "[BTCoex], BT Profile = A2DP + PAN(HS)\n");
0564                     algorithm = BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS;
0565                 } else {
0566                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0567                         DBG_LOUD,
0568                         "[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
0569                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP;
0570                 }
0571             }
0572         }
0573     } else if (num_of_diff_profile == 3) {
0574         if (bt_link_info->sco_exist) {
0575             if (bt_link_info->hid_exist &&
0576                 bt_link_info->a2dp_exist) {
0577                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0578                     "[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
0579                 algorithm = BT_8821A_1ANT_COEX_ALGO_HID;
0580             } else if (bt_link_info->hid_exist &&
0581                    bt_link_info->pan_exist) {
0582                 if (bt_hs_on) {
0583                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0584                         DBG_LOUD,
0585                         "[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
0586                     algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
0587                 } else {
0588                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0589                         DBG_LOUD,
0590                         "[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
0591                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
0592                 }
0593             } else if (bt_link_info->pan_exist &&
0594                    bt_link_info->a2dp_exist) {
0595                 if (bt_hs_on) {
0596                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0597                         DBG_LOUD,
0598                         "[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
0599                     algorithm = BT_8821A_1ANT_COEX_ALGO_SCO;
0600                 } else {
0601                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0602                         DBG_LOUD,
0603                         "[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
0604                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
0605                 }
0606             }
0607         } else {
0608             if (bt_link_info->hid_exist &&
0609                 bt_link_info->pan_exist &&
0610                 bt_link_info->a2dp_exist) {
0611                 if (bt_hs_on) {
0612                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0613                         DBG_LOUD,
0614                         "[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
0615                     algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP;
0616                 } else {
0617                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0618                         DBG_LOUD,
0619                         "[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
0620                     algorithm = BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
0621                 }
0622             }
0623         }
0624     } else if (num_of_diff_profile >= 3) {
0625         if (bt_link_info->sco_exist) {
0626             if (bt_link_info->hid_exist &&
0627                 bt_link_info->pan_exist &&
0628                 bt_link_info->a2dp_exist) {
0629                 if (bt_hs_on) {
0630                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0631                         DBG_LOUD,
0632                         "[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
0633 
0634                 } else {
0635                     rtl_dbg(rtlpriv, COMP_BT_COEXIST,
0636                         DBG_LOUD,
0637                         "[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
0638                     algorithm = BT_8821A_1ANT_COEX_ALGO_PANEDR_HID;
0639                 }
0640             }
0641         }
0642     }
0643     return algorithm;
0644 }
0645 
0646 static void btc8821a1ant_set_sw_penalty_tx_rate(struct btc_coexist *btcoexist,
0647                         bool low_penalty_ra)
0648 {
0649     struct rtl_priv *rtlpriv = btcoexist->adapter;
0650     u8 h2c_parameter[6] = {0};
0651 
0652     h2c_parameter[0] = 0x6; /* opCode, 0x6= Retry_Penalty*/
0653 
0654     if (low_penalty_ra) {
0655         h2c_parameter[1] |= BIT0;
0656         /* normal rate except MCS7/6/5, OFDM54/48/36 */
0657         h2c_parameter[2] = 0x00;
0658         h2c_parameter[3] = 0xf7; /* MCS7 or OFDM54 */
0659         h2c_parameter[4] = 0xf8; /* MCS6 or OFDM48 */
0660         h2c_parameter[5] = 0xf9; /* MCS5 or OFDM36 */
0661     }
0662 
0663     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0664         "[BTCoex], set WiFi Low-Penalty Retry: %s",
0665         (low_penalty_ra ? "ON!!" : "OFF!!"));
0666 
0667     btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
0668 }
0669 
0670 static void btc8821a1ant_low_penalty_ra(struct btc_coexist *btcoexist,
0671                     bool force_exec, bool low_penalty_ra)
0672 {
0673     coex_dm->cur_low_penalty_ra = low_penalty_ra;
0674 
0675     if (!force_exec) {
0676         if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
0677             return;
0678     }
0679     btc8821a1ant_set_sw_penalty_tx_rate(btcoexist,
0680                         coex_dm->cur_low_penalty_ra);
0681 
0682     coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
0683 }
0684 
0685 static void btc8821a1ant_set_coex_table(struct btc_coexist *btcoexist,
0686                     u32 val0x6c0, u32 val0x6c4,
0687                     u32 val0x6c8, u8 val0x6cc)
0688 {
0689     struct rtl_priv *rtlpriv = btcoexist->adapter;
0690 
0691     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0692         "[BTCoex], set coex table, set 0x6c0 = 0x%x\n", val0x6c0);
0693     btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
0694 
0695     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0696         "[BTCoex], set coex table, set 0x6c4 = 0x%x\n", val0x6c4);
0697     btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
0698 
0699     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0700         "[BTCoex], set coex table, set 0x6c8 = 0x%x\n", val0x6c8);
0701     btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
0702 
0703     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0704         "[BTCoex], set coex table, set 0x6cc = 0x%x\n", val0x6cc);
0705     btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
0706 }
0707 
0708 static void btc8821a1ant_coex_table(struct btc_coexist *btcoexist,
0709                     bool force_exec, u32 val0x6c0, u32 val0x6c4,
0710                     u32 val0x6c8, u8 val0x6cc)
0711 {
0712     struct rtl_priv *rtlpriv = btcoexist->adapter;
0713 
0714     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0715         "[BTCoex], %s write Coex Table 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6c8 = 0x%x, 0x6cc = 0x%x\n",
0716         (force_exec ? "force to" : ""), val0x6c0, val0x6c4,
0717         val0x6c8, val0x6cc);
0718     coex_dm->cur_val_0x6c0 = val0x6c0;
0719     coex_dm->cur_val_0x6c4 = val0x6c4;
0720     coex_dm->cur_val_0x6c8 = val0x6c8;
0721     coex_dm->cur_val_0x6cc = val0x6cc;
0722 
0723     if (!force_exec) {
0724         if ((coex_dm->pre_val_0x6c0 == coex_dm->cur_val_0x6c0) &&
0725             (coex_dm->pre_val_0x6c4 == coex_dm->cur_val_0x6c4) &&
0726             (coex_dm->pre_val_0x6c8 == coex_dm->cur_val_0x6c8) &&
0727             (coex_dm->pre_val_0x6cc == coex_dm->cur_val_0x6cc))
0728             return;
0729     }
0730     btc8821a1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4,
0731                     val0x6c8, val0x6cc);
0732 
0733     coex_dm->pre_val_0x6c0 = coex_dm->cur_val_0x6c0;
0734     coex_dm->pre_val_0x6c4 = coex_dm->cur_val_0x6c4;
0735     coex_dm->pre_val_0x6c8 = coex_dm->cur_val_0x6c8;
0736     coex_dm->pre_val_0x6cc = coex_dm->cur_val_0x6cc;
0737 }
0738 
0739 static void btc8821a1ant_coex_table_with_type(struct btc_coexist *btcoexist,
0740                           bool force_exec, u8 type)
0741 {
0742     switch (type) {
0743     case 0:
0744         btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
0745                     0x55555555, 0xffffff, 0x3);
0746         break;
0747     case 1:
0748         btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
0749                     0x5a5a5a5a, 0xffffff, 0x3);
0750         break;
0751     case 2:
0752         btc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
0753                     0x5a5a5a5a, 0xffffff, 0x3);
0754         break;
0755     case 3:
0756         btc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
0757                     0xaaaaaaaa, 0xffffff, 0x3);
0758         break;
0759     case 4:
0760         btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
0761                     0x5a5a5a5a, 0xffffff, 0x3);
0762         break;
0763     case 5:
0764         btc8821a1ant_coex_table(btcoexist, force_exec, 0x5a5a5a5a,
0765                     0xaaaa5a5a, 0xffffff, 0x3);
0766         break;
0767     case 6:
0768         btc8821a1ant_coex_table(btcoexist, force_exec, 0x55555555,
0769                     0xaaaa5a5a, 0xffffff, 0x3);
0770         break;
0771     case 7:
0772         btc8821a1ant_coex_table(btcoexist, force_exec, 0xaaaaaaaa,
0773                     0xaaaaaaaa, 0xffffff, 0x3);
0774         break;
0775     default:
0776         break;
0777     }
0778 }
0779 
0780 static void btc8821a1ant_set_fw_ignore_wlan_act(struct btc_coexist *btcoexist,
0781                         bool enable)
0782 {
0783     struct rtl_priv *rtlpriv = btcoexist->adapter;
0784     u8 h2c_parameter[1] = {0};
0785 
0786     if (enable)
0787         h2c_parameter[0] |= BIT0; /* function enable */
0788 
0789     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0790         "[BTCoex], set FW for BT Ignore Wlan_Act, FW write 0x63 = 0x%x\n",
0791         h2c_parameter[0]);
0792 
0793     btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
0794 }
0795 
0796 static void btc8821a1ant_ignore_wlan_act(struct btc_coexist *btcoexist,
0797                      bool force_exec, bool enable)
0798 {
0799     struct rtl_priv *rtlpriv = btcoexist->adapter;
0800 
0801     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0802         "[BTCoex], %s turn Ignore WlanAct %s\n",
0803         (force_exec ? "force to" : ""), (enable ? "ON" : "OFF"));
0804     coex_dm->cur_ignore_wlan_act = enable;
0805 
0806     if (!force_exec) {
0807         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0808             "[BTCoex], pre_ignore_wlan_act = %d, cur_ignore_wlan_act = %d!!\n",
0809             coex_dm->pre_ignore_wlan_act,
0810              coex_dm->cur_ignore_wlan_act);
0811 
0812         if (coex_dm->pre_ignore_wlan_act ==
0813             coex_dm->cur_ignore_wlan_act)
0814             return;
0815     }
0816     btc8821a1ant_set_fw_ignore_wlan_act(btcoexist, enable);
0817 
0818     coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
0819 }
0820 
0821 static void btc8821a1ant_set_fw_ps_tdma(struct btc_coexist *btcoexist, u8 byte1,
0822                     u8 byte2, u8 byte3, u8 byte4, u8 byte5)
0823 {
0824     struct rtl_priv *rtlpriv = btcoexist->adapter;
0825     u8 h2c_parameter[5] = {0};
0826     u8 real_byte1 = byte1, real_byte5 = byte5;
0827     bool ap_enable = false;
0828 
0829     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
0830                &ap_enable);
0831 
0832     if (ap_enable) {
0833         if (byte1 & BIT4 && !(byte1 & BIT5)) {
0834             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0835                 "[BTCoex], FW for 1Ant AP mode\n");
0836             real_byte1 &= ~BIT4;
0837             real_byte1 |= BIT5;
0838 
0839             real_byte5 |= BIT5;
0840             real_byte5 &= ~BIT6;
0841         }
0842     }
0843 
0844     h2c_parameter[0] = real_byte1;
0845     h2c_parameter[1] = byte2;
0846     h2c_parameter[2] = byte3;
0847     h2c_parameter[3] = byte4;
0848     h2c_parameter[4] = real_byte5;
0849 
0850     coex_dm->ps_tdma_para[0] = real_byte1;
0851     coex_dm->ps_tdma_para[1] = byte2;
0852     coex_dm->ps_tdma_para[2] = byte3;
0853     coex_dm->ps_tdma_para[3] = byte4;
0854     coex_dm->ps_tdma_para[4] = real_byte5;
0855 
0856     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0857         "[BTCoex], PS-TDMA H2C cmd =0x%x%08x\n",
0858         h2c_parameter[0],
0859         h2c_parameter[1] << 24 |
0860         h2c_parameter[2] << 16 |
0861         h2c_parameter[3] << 8 |
0862         h2c_parameter[4]);
0863     btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
0864 }
0865 
0866 static void btc8821a1ant_set_lps_rpwm(struct btc_coexist *btcoexist,
0867                       u8 lps_val, u8 rpwm_val)
0868 {
0869     u8 lps = lps_val;
0870     u8 rpwm = rpwm_val;
0871 
0872     btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
0873     btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
0874 }
0875 
0876 static void btc8821a1ant_lps_rpwm(struct btc_coexist *btcoexist,
0877                   bool force_exec, u8 lps_val, u8 rpwm_val)
0878 {
0879     struct rtl_priv *rtlpriv = btcoexist->adapter;
0880 
0881     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0882         "[BTCoex], %s set lps/rpwm = 0x%x/0x%x\n",
0883         (force_exec ? "force to" : ""), lps_val, rpwm_val);
0884     coex_dm->cur_lps = lps_val;
0885     coex_dm->cur_rpwm = rpwm_val;
0886 
0887     if (!force_exec) {
0888         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0889             "[BTCoex], LPS-RxBeaconMode = 0x%x, LPS-RPWM = 0x%x!!\n",
0890             coex_dm->cur_lps, coex_dm->cur_rpwm);
0891 
0892         if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
0893             (coex_dm->pre_rpwm == coex_dm->cur_rpwm)) {
0894             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0895                 "[BTCoex], LPS-RPWM_Last = 0x%x, LPS-RPWM_Now = 0x%x!!\n",
0896                 coex_dm->pre_rpwm, coex_dm->cur_rpwm);
0897 
0898             return;
0899         }
0900     }
0901     btc8821a1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
0902 
0903     coex_dm->pre_lps = coex_dm->cur_lps;
0904     coex_dm->pre_rpwm = coex_dm->cur_rpwm;
0905 }
0906 
0907 static void btc8821a1ant_sw_mechanism(struct btc_coexist *btcoexist,
0908                       bool low_penalty_ra)
0909 {
0910     struct rtl_priv *rtlpriv = btcoexist->adapter;
0911 
0912     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
0913         "[BTCoex], SM[LpRA] = %d\n", low_penalty_ra);
0914 
0915     btc8821a1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, low_penalty_ra);
0916 }
0917 
0918 static void btc8821a1ant_set_ant_path(struct btc_coexist *btcoexist,
0919                       u8 ant_pos_type, bool init_hw_cfg,
0920                       bool wifi_off)
0921 {
0922     struct btc_board_info *board_info = &btcoexist->board_info;
0923     u32 u4_tmp = 0;
0924     u8 h2c_parameter[2] = {0};
0925 
0926     if (init_hw_cfg) {
0927         /* 0x4c[23] = 0, 0x4c[24] = 1  Antenna control by WL/BT */
0928         u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
0929         u4_tmp &= ~BIT23;
0930         u4_tmp |= BIT24;
0931         btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
0932 
0933         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x975, 0x3, 0x3);
0934         btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
0935 
0936         if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
0937             /* tell firmware "antenna inverse"
0938              * WRONG firmware antenna control code, need fw to fix
0939              */
0940             h2c_parameter[0] = 1;
0941             h2c_parameter[1] = 1;
0942             btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
0943                         h2c_parameter);
0944         } else {
0945             /* tell firmware "no antenna inverse"
0946              * WRONG firmware antenna control code, need fw to fix
0947              */
0948             h2c_parameter[0] = 0;
0949             h2c_parameter[1] = 1;
0950             btcoexist->btc_fill_h2c(btcoexist, 0x65, 2,
0951                         h2c_parameter);
0952         }
0953     } else if (wifi_off) {
0954         /* 0x4c[24:23] = 00, Set Antenna control
0955          * by BT_RFE_CTRL BT Vendor 0xac = 0xf002
0956          */
0957         u4_tmp = btcoexist->btc_read_4byte(btcoexist, 0x4c);
0958         u4_tmp &= ~BIT23;
0959         u4_tmp &= ~BIT24;
0960         btcoexist->btc_write_4byte(btcoexist, 0x4c, u4_tmp);
0961 
0962         /* 0x765 = 0x18 */
0963         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x3);
0964     } else {
0965         /* 0x765 = 0x0 */
0966         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x765, 0x18, 0x0);
0967     }
0968 
0969     /* ext switch setting */
0970     switch (ant_pos_type) {
0971     case BTC_ANT_PATH_WIFI:
0972         btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
0973         if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
0974             btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
0975                                0x30, 0x1);
0976         else
0977             btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
0978                                0x30, 0x2);
0979         break;
0980     case BTC_ANT_PATH_BT:
0981         btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x77);
0982         if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
0983             btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
0984                                0x30, 0x2);
0985         else
0986             btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
0987                                0x30, 0x1);
0988         break;
0989     default:
0990     case BTC_ANT_PATH_PTA:
0991         btcoexist->btc_write_1byte(btcoexist, 0xcb4, 0x66);
0992         if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
0993             btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
0994                                0x30, 0x1);
0995         else
0996             btcoexist->btc_write_1byte_bitmask(btcoexist, 0xcb7,
0997                                0x30, 0x2);
0998         break;
0999     }
1000 }
1001 
1002 static void btc8821a1ant_ps_tdma(struct btc_coexist *btcoexist,
1003                  bool force_exec, bool turn_on, u8 type)
1004 {
1005     struct rtl_priv *rtlpriv = btcoexist->adapter;
1006     u8 rssi_adjust_val = 0;
1007 
1008     coex_dm->cur_ps_tdma_on = turn_on;
1009     coex_dm->cur_ps_tdma = type;
1010 
1011     if (!force_exec) {
1012         if (coex_dm->cur_ps_tdma_on) {
1013             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1014                 "[BTCoex], ********** TDMA(on, %d) **********\n",
1015                 coex_dm->cur_ps_tdma);
1016         } else {
1017             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1018                 "[BTCoex], ********** TDMA(off, %d) **********\n",
1019                 coex_dm->cur_ps_tdma);
1020         }
1021         if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1022             (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1023             return;
1024     }
1025     if (turn_on) {
1026         switch (type) {
1027         default:
1028             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1a,
1029                             0x1a, 0x0, 0x50);
1030             break;
1031         case 1:
1032             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x3a,
1033                             0x03, 0x10, 0x50);
1034             rssi_adjust_val = 11;
1035             break;
1036         case 2:
1037             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x2b,
1038                             0x03, 0x10, 0x50);
1039             rssi_adjust_val = 14;
1040             break;
1041         case 3:
1042             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1d,
1043                             0x1d, 0x0, 0x10);
1044             break;
1045         case 4:
1046             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x15,
1047                             0x3, 0x14, 0x0);
1048             rssi_adjust_val = 17;
1049             break;
1050         case 5:
1051             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x15,
1052                             0x3, 0x11, 0x10);
1053             break;
1054         case 6:
1055             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x20,
1056                             0x3, 0x11, 0x13);
1057             break;
1058         case 7:
1059             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xc,
1060                             0x5, 0x0, 0x0);
1061             break;
1062         case 8:
1063             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x25,
1064                             0x3, 0x10, 0x0);
1065             break;
1066         case 9:
1067             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x21,
1068                             0x3, 0x10, 0x50);
1069             rssi_adjust_val = 18;
1070             break;
1071         case 10:
1072             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xa,
1073                             0xa, 0x0, 0x40);
1074             break;
1075         case 11:
1076             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x15,
1077                             0x03, 0x10, 0x50);
1078             rssi_adjust_val = 20;
1079             break;
1080         case 12:
1081             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x0a,
1082                             0x0a, 0x0, 0x50);
1083             break;
1084         case 13:
1085             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x12,
1086                             0x12, 0x0, 0x50);
1087             break;
1088         case 14:
1089             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x1e,
1090                             0x3, 0x10, 0x14);
1091             break;
1092         case 15:
1093             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x13, 0xa,
1094                             0x3, 0x8, 0x0);
1095             break;
1096         case 16:
1097             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x15,
1098                             0x3, 0x10, 0x0);
1099             rssi_adjust_val = 18;
1100             break;
1101         case 18:
1102             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x93, 0x25,
1103                             0x3, 0x10, 0x0);
1104             rssi_adjust_val = 14;
1105             break;
1106         case 20:
1107             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x35,
1108                             0x03, 0x11, 0x10);
1109             break;
1110         case 21:
1111             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x25,
1112                             0x03, 0x11, 0x11);
1113             break;
1114         case 22:
1115             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x25,
1116                             0x03, 0x11, 0x10);
1117             break;
1118         case 23:
1119             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1120                             0x3, 0x31, 0x18);
1121             rssi_adjust_val = 22;
1122             break;
1123         case 24:
1124             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x15,
1125                             0x3, 0x31, 0x18);
1126             rssi_adjust_val = 22;
1127             break;
1128         case 25:
1129             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1130                             0x3, 0x31, 0x18);
1131             rssi_adjust_val = 22;
1132             break;
1133         case 26:
1134             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0xa,
1135                             0x3, 0x31, 0x18);
1136             rssi_adjust_val = 22;
1137             break;
1138         case 27:
1139             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xe3, 0x25,
1140                             0x3, 0x31, 0x98);
1141             rssi_adjust_val = 22;
1142             break;
1143         case 28:
1144             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x69, 0x25,
1145                             0x3, 0x31, 0x0);
1146             break;
1147         case 29:
1148             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xab, 0x1a,
1149                             0x1a, 0x1, 0x10);
1150             break;
1151         case 30:
1152             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x30,
1153                             0x3, 0x10, 0x10);
1154             break;
1155         case 31:
1156             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x1a,
1157                             0x1a, 0, 0x58);
1158             break;
1159         case 32:
1160             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x61, 0x35,
1161                             0x3, 0x11, 0x11);
1162             break;
1163         case 33:
1164             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xa3, 0x25,
1165                             0x3, 0x30, 0x90);
1166             break;
1167         case 34:
1168             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x53, 0x1a,
1169                             0x1a, 0x0, 0x10);
1170             break;
1171         case 35:
1172             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x63, 0x1a,
1173                             0x1a, 0x0, 0x10);
1174             break;
1175         case 36:
1176             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0xd3, 0x12,
1177                             0x3, 0x14, 0x50);
1178             break;
1179         case 40:
1180             /* SoftAP only with no sta associated, BT disable, TDMA
1181              * mode for power saving
1182              *
1183              * here softap mode screen off will cost 70-80mA for
1184              * phone
1185              */
1186             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x23, 0x18,
1187                             0x00, 0x10, 0x24);
1188             break;
1189         case 41:
1190             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x15,
1191                             0x3, 0x11, 0x11);
1192             break;
1193         case 42:
1194             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x20,
1195                             0x3, 0x11, 0x11);
1196             break;
1197         case 43:
1198             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x51, 0x30,
1199                             0x3, 0x10, 0x11);
1200             break;
1201         }
1202     } else {
1203         /* disable PS tdma */
1204         switch (type) {
1205         case 8:
1206             /* PTA Control */
1207             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x8, 0x0, 0x0,
1208                             0x0, 0x0);
1209             btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
1210                           false, false);
1211             break;
1212         case 0:
1213         default:
1214             /* Software control, Antenna at BT side */
1215             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1216                             0x0, 0x0);
1217             btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1218                           false, false);
1219             break;
1220         case 9:
1221             /* Software control, Antenna at WiFi side */
1222             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1223                             0x0, 0x0);
1224             btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
1225                           false, false);
1226             break;
1227         case 10:
1228             /* under 5G */
1229             btc8821a1ant_set_fw_ps_tdma(btcoexist, 0x0, 0x0, 0x0,
1230                             0x8, 0x0);
1231             btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
1232                           false, false);
1233             break;
1234         }
1235     }
1236     rssi_adjust_val = 0;
1237     btcoexist->btc_set(btcoexist,
1238          BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE, &rssi_adjust_val);
1239 
1240     /* update pre state */
1241     coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1242     coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1243 }
1244 
1245 static bool btc8821a1ant_is_common_action(struct btc_coexist *btcoexist)
1246 {
1247     struct rtl_priv *rtlpriv = btcoexist->adapter;
1248     bool common = false, wifi_connected = false, wifi_busy = false;
1249 
1250     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1251                &wifi_connected);
1252     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1253 
1254     if (!wifi_connected &&
1255         BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1256         coex_dm->bt_status) {
1257         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1258             "[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
1259         btc8821a1ant_sw_mechanism(btcoexist, false);
1260 
1261         common = true;
1262     } else if (wifi_connected &&
1263            (BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
1264             coex_dm->bt_status)) {
1265         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1266             "[BTCoex], Wifi connected + BT non connected-idle!!\n");
1267         btc8821a1ant_sw_mechanism(btcoexist, false);
1268 
1269         common = true;
1270     } else if (!wifi_connected &&
1271            (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1272             coex_dm->bt_status)) {
1273         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1274             "[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
1275         btc8821a1ant_sw_mechanism(btcoexist, false);
1276 
1277         common = true;
1278     } else if (wifi_connected &&
1279            (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
1280             coex_dm->bt_status)) {
1281         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1282             "[BTCoex], Wifi connected + BT connected-idle!!\n");
1283         btc8821a1ant_sw_mechanism(btcoexist, false);
1284 
1285         common = true;
1286     } else if (!wifi_connected &&
1287            (BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE !=
1288             coex_dm->bt_status)) {
1289         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1290             "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
1291         btc8821a1ant_sw_mechanism(btcoexist, false);
1292 
1293         common = true;
1294     } else {
1295         if (wifi_busy) {
1296             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1297                 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
1298         } else {
1299             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1300                 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
1301         }
1302 
1303         common = false;
1304     }
1305 
1306     return common;
1307 }
1308 
1309 static void btc8821a1ant_ps_tdma_check_for_pwr_save(struct btc_coexist *btcoex,
1310                             bool new_ps_state)
1311 {
1312     u8 lps_mode = 0x0;
1313 
1314     btcoex->btc_get(btcoex, BTC_GET_U1_LPS_MODE, &lps_mode);
1315 
1316     if (lps_mode) {
1317         /* already under LPS state */
1318         if (new_ps_state) {
1319             /* keep state under LPS, do nothing */
1320         } else {
1321             /* will leave LPS state, turn off psTdma first */
1322             btc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1323         }
1324     } else {
1325         /* NO PS state*/
1326         if (new_ps_state) {
1327             /* will enter LPS state, turn off psTdma first */
1328             btc8821a1ant_ps_tdma(btcoex, NORMAL_EXEC, false, 0);
1329         } else {
1330             /* keep state under NO PS state, do nothing */
1331         }
1332     }
1333 }
1334 
1335 static void btc8821a1ant_power_save_state(struct btc_coexist *btcoexist,
1336                       u8 ps_type, u8 lps_val, u8 rpwm_val)
1337 {
1338     bool low_pwr_disable = false;
1339 
1340     switch (ps_type) {
1341     case BTC_PS_WIFI_NATIVE:
1342         /* recover to original 32k low power setting */
1343         low_pwr_disable = false;
1344         btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1345                    &low_pwr_disable);
1346         btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS, NULL);
1347         break;
1348     case BTC_PS_LPS_ON:
1349         btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist,
1350                             true);
1351         btc8821a1ant_lps_rpwm(btcoexist, NORMAL_EXEC, lps_val,
1352                       rpwm_val);
1353         /* when coex force to enter LPS, do not enter 32k low power */
1354         low_pwr_disable = true;
1355         btcoexist->btc_set(btcoexist, BTC_SET_ACT_DISABLE_LOW_POWER,
1356                    &low_pwr_disable);
1357         /* power save must executed before psTdma */
1358         btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS, NULL);
1359         break;
1360     case BTC_PS_LPS_OFF:
1361         btc8821a1ant_ps_tdma_check_for_pwr_save(btcoexist, false);
1362         btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS, NULL);
1363         break;
1364     default:
1365         break;
1366     }
1367 }
1368 
1369 static void btc8821a1ant_coex_under_5g(struct btc_coexist *btcoexist)
1370 {
1371     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1372                       0x0, 0x0);
1373     btc8821a1ant_ignore_wlan_act(btcoexist, NORMAL_EXEC, true);
1374 
1375     btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 10);
1376 
1377     btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1378 
1379     btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1380 
1381     btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false, false, 5);
1382 }
1383 
1384 /***********************************************
1385  *
1386  *  Software Coex Mechanism start
1387  *
1388  ***********************************************/
1389 
1390 /* SCO only or SCO+PAN(HS) */
1391 static void btc8821a1ant_action_sco(struct btc_coexist *btcoexist)
1392 {
1393     btc8821a1ant_sw_mechanism(btcoexist, true);
1394 }
1395 
1396 static void btc8821a1ant_action_hid(struct btc_coexist *btcoexist)
1397 {
1398     btc8821a1ant_sw_mechanism(btcoexist, true);
1399 }
1400 
1401 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
1402 static void btc8821a1ant_action_a2dp(struct btc_coexist *btcoexist)
1403 {
1404     btc8821a1ant_sw_mechanism(btcoexist, false);
1405 }
1406 
1407 static void btc8821a1ant_action_a2dp_pan_hs(struct btc_coexist *btcoexist)
1408 {
1409     btc8821a1ant_sw_mechanism(btcoexist, false);
1410 }
1411 
1412 static void btc8821a1ant_action_pan_edr(struct btc_coexist *btcoexist)
1413 {
1414     btc8821a1ant_sw_mechanism(btcoexist, false);
1415 }
1416 
1417 /* PAN(HS) only */
1418 static void btc8821a1ant_action_pan_hs(struct btc_coexist *btcoexist)
1419 {
1420     btc8821a1ant_sw_mechanism(btcoexist, false);
1421 }
1422 
1423 /* PAN(EDR)+A2DP */
1424 static void btc8821a1ant_action_pan_edr_a2dp(struct btc_coexist *btcoexist)
1425 {
1426     btc8821a1ant_sw_mechanism(btcoexist, false);
1427 }
1428 
1429 static void btc8821a1ant_action_pan_edr_hid(struct btc_coexist *btcoexist)
1430 {
1431     btc8821a1ant_sw_mechanism(btcoexist, true);
1432 }
1433 
1434 /* HID+A2DP+PAN(EDR) */
1435 static void btc8821a1ant_action_hid_a2dp_pan_edr(struct btc_coexist *btcoexist)
1436 {
1437     btc8821a1ant_sw_mechanism(btcoexist, true);
1438 }
1439 
1440 static void btc8821a1ant_action_hid_a2dp(struct btc_coexist *btcoexist)
1441 {
1442     btc8821a1ant_sw_mechanism(btcoexist, true);
1443 }
1444 
1445 /***********************************************
1446  *
1447  *  Non-Software Coex Mechanism start
1448  *
1449  ***********************************************/
1450 static
1451 void btc8821a1ant_action_wifi_multi_port(struct btc_coexist *btcoexist)
1452 {
1453     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1454 
1455     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1456     /* tdma and coex table */
1457     if (coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_ACL_BUSY) {
1458         if (bt_link_info->a2dp_exist) {
1459             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1460             btc8821a1ant_coex_table_with_type(btcoexist,
1461                               NORMAL_EXEC, 1);
1462         } else if (bt_link_info->a2dp_exist &&
1463                bt_link_info->pan_exist) {
1464             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1465             btc8821a1ant_coex_table_with_type(btcoexist,
1466                               NORMAL_EXEC, 4);
1467         } else {
1468             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1469             btc8821a1ant_coex_table_with_type(btcoexist,
1470                               NORMAL_EXEC, 4);
1471         }
1472     } else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1473            (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1474             coex_dm->bt_status)) {
1475         btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1476                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1477     } else {
1478         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1479         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1480     }
1481 }
1482 
1483 static
1484 void btc8821a1ant_action_wifi_not_connected_asso_auth(
1485                     struct btc_coexist *btcoexist)
1486 {
1487     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1488 
1489     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0,
1490                       0x0);
1491 
1492     /* tdma and coex table */
1493     if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist)) {
1494         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1495         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1496     } else if ((bt_link_info->a2dp_exist) || (bt_link_info->pan_exist)) {
1497         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1498         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1499     } else {
1500         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1501         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1502     }
1503 }
1504 
1505 
1506 static void btc8821a1ant_action_hs(struct btc_coexist *btcoexist)
1507 {
1508     btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1509     btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 2);
1510 }
1511 
1512 static void btc8821a1ant_action_bt_inquiry(struct btc_coexist *btcoexist)
1513 {
1514     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1515     bool wifi_connected = false;
1516     bool ap_enable = false;
1517     bool wifi_busy = false, bt_busy = false;
1518 
1519     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1520                &wifi_connected);
1521     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1522                &ap_enable);
1523     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1524     btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
1525 
1526     if (!wifi_connected && !coex_sta->wifi_is_high_pri_task) {
1527         btc8821a1ant_power_save_state(btcoexist,
1528                           BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1529         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1530         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1531     } else if ((bt_link_info->sco_exist) || (bt_link_info->a2dp_exist) ||
1532            (bt_link_info->hid_only)) {
1533         /* SCO/HID-only busy */
1534         btc8821a1ant_power_save_state(btcoexist,
1535                           BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1536         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1537         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1538     } else if ((bt_link_info->a2dp_exist) && (bt_link_info->hid_exist)) {
1539         /* A2DP+HID busy */
1540         btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1541                           0x0, 0x0);
1542         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1543 
1544         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1545     } else if ((bt_link_info->pan_exist) || (wifi_busy)) {
1546         btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1547                           0x0, 0x0);
1548         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1549 
1550         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1551     } else {
1552         btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1553                           0x0, 0x0);
1554         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1555         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
1556     }
1557 }
1558 
1559 static void btc8821a1ant_act_bt_sco_hid_only_busy(struct btc_coexist *btcoexist,
1560                           u8 wifi_status)
1561 {
1562     /* tdma and coex table */
1563     btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 5);
1564 
1565     btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1566 }
1567 
1568 static void btc8821a1ant_act_wifi_con_bt_acl_busy(struct btc_coexist *btcoexist,
1569                           u8 wifi_status)
1570 {
1571     u8 bt_rssi_state;
1572 
1573     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1574 
1575     bt_rssi_state = btc8821a1ant_bt_rssi_state(btcoexist, 2, 28, 0);
1576 
1577     if (bt_link_info->hid_only) {
1578         /* HID */
1579         btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1580                               wifi_status);
1581         coex_dm->auto_tdma_adjust = false;
1582         return;
1583     } else if (bt_link_info->a2dp_only) {
1584         /* A2DP */
1585         if (wifi_status == BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE) {
1586             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1587             btc8821a1ant_coex_table_with_type(btcoexist,
1588                               NORMAL_EXEC, 1);
1589             coex_dm->auto_tdma_adjust = false;
1590         } else if ((bt_rssi_state != BTC_RSSI_STATE_HIGH) &&
1591                (bt_rssi_state == BTC_RSSI_STATE_STAY_HIGH)) {
1592             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1593             btc8821a1ant_coex_table_with_type(btcoexist,
1594                               NORMAL_EXEC, 1);
1595         } else {
1596             /* for low BT RSSI */
1597             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1598             btc8821a1ant_coex_table_with_type(btcoexist,
1599                               NORMAL_EXEC, 1);
1600             coex_dm->auto_tdma_adjust = false;
1601         }
1602     } else if (bt_link_info->hid_exist && bt_link_info->a2dp_exist) {
1603         /* HID+A2DP (no need to consider BT RSSI) */
1604         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC,
1605                      true, 14);
1606         coex_dm->auto_tdma_adjust = false;
1607 
1608         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1609     } else if ((bt_link_info->pan_only) ||
1610         (bt_link_info->hid_exist && bt_link_info->pan_exist)) {
1611         /* PAN(OPP, FTP), HID+PAN(OPP, FTP) */
1612         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 3);
1613         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 6);
1614         coex_dm->auto_tdma_adjust = false;
1615     } else if (((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) ||
1616            (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
1617             bt_link_info->pan_exist)) {
1618         /* A2DP+PAN(OPP, FTP), HID+A2DP+PAN(OPP, FTP) */
1619         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 43);
1620         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1621         coex_dm->auto_tdma_adjust = false;
1622     } else {
1623         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 11);
1624         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1625         coex_dm->auto_tdma_adjust = false;
1626     }
1627 }
1628 
1629 static
1630 void btc8821a1ant_action_wifi_not_connected(struct btc_coexist *btcoexist)
1631 {
1632     /* power save state */
1633     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1634 
1635     /* tdma and coex table */
1636     btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1637     btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
1638 }
1639 
1640 static void btc8821a1ant_act_wifi_not_conn_scan(struct btc_coexist *btcoexist)
1641 {
1642     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1643 
1644     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1645 
1646     /* tdma and coex table */
1647     if (coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_ACL_BUSY) {
1648         if (bt_link_info->a2dp_exist) {
1649             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1650             btc8821a1ant_coex_table_with_type(btcoexist,
1651                               NORMAL_EXEC, 1);
1652         } else if (bt_link_info->a2dp_exist &&
1653                bt_link_info->pan_exist) {
1654             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 22);
1655             btc8821a1ant_coex_table_with_type(btcoexist,
1656                               NORMAL_EXEC, 4);
1657         } else {
1658             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1659             btc8821a1ant_coex_table_with_type(btcoexist,
1660                               NORMAL_EXEC, 4);
1661         }
1662     } else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1663            (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1664             coex_dm->bt_status)) {
1665         btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1666                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1667     } else {
1668         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1669         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1670     }
1671 }
1672 
1673 static
1674 void btc8821a1ant_action_wifi_connected_scan(struct btc_coexist *btcoexist)
1675 {
1676     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1677 
1678     /* power save state */
1679     btc8821a1ant_power_save_state(btcoexist,
1680                       BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1681 
1682     /* tdma and coex table */
1683     if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1684         if (bt_link_info->a2dp_exist) {
1685             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1686             btc8821a1ant_coex_table_with_type(btcoexist,
1687                               NORMAL_EXEC, 1);
1688         } else {
1689             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1690             btc8821a1ant_coex_table_with_type(btcoexist,
1691                               NORMAL_EXEC, 4);
1692         }
1693     } else if ((coex_dm->bt_status == BT_8821A_1ANT_BT_STATUS_SCO_BUSY) ||
1694            (coex_dm->bt_status ==
1695             BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY)) {
1696         btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1697             BT_8821A_1ANT_WIFI_STATUS_CONNECTED_SCAN);
1698     } else {
1699         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1700         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1701     }
1702 }
1703 
1704 static void btc8821a1ant_act_wifi_conn_sp_pkt(struct btc_coexist *btcoexist)
1705 {
1706     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1707 
1708     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1709                       0x0, 0x0);
1710 
1711     /* tdma and coex table */
1712     if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist) ||
1713         (bt_link_info->a2dp_exist)) {
1714         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 32);
1715         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1716     }
1717 
1718     if ((bt_link_info->hid_exist) && (bt_link_info->a2dp_exist)) {
1719         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 14);
1720         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
1721     } else if (bt_link_info->pan_exist) {
1722         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, true, 20);
1723         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
1724     } else {
1725         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1726         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
1727     }
1728 }
1729 
1730 static void btc8821a1ant_action_wifi_connected(struct btc_coexist *btcoexist)
1731 {
1732     struct rtl_priv *rtlpriv = btcoexist->adapter;
1733     bool wifi_busy = false;
1734     bool scan = false, link = false, roam = false;
1735     bool under_4way = false;
1736     bool ap_enable = false;
1737 
1738     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1739         "[BTCoex], CoexForWifiConnect()===>\n");
1740 
1741     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
1742                &under_4way);
1743     if (under_4way) {
1744         btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1745         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1746             "[BTCoex], CoexForWifiConnect(), return for wifi is under 4way<===\n");
1747         return;
1748     }
1749 
1750     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1751     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1752     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
1753     if (scan || link || roam) {
1754         if (scan)
1755             btc8821a1ant_action_wifi_connected_scan(btcoexist);
1756         else
1757             btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
1758 
1759         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1760             "[BTCoex], CoexForWifiConnect(), return for wifi is under scan<===\n");
1761         return;
1762     }
1763 
1764     /* power save state*/
1765     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1766                &ap_enable);
1767     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1768     if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY ==
1769         coex_dm->bt_status && !ap_enable &&
1770         !btcoexist->bt_link_info.hid_only) {
1771         if (!wifi_busy && btcoexist->bt_link_info.a2dp_only)
1772             /* A2DP */
1773             btc8821a1ant_power_save_state(btcoexist,
1774                         BTC_PS_WIFI_NATIVE, 0x0, 0x0);
1775         else
1776             btc8821a1ant_power_save_state(btcoexist, BTC_PS_LPS_ON,
1777                               0x50, 0x4);
1778     } else {
1779         btc8821a1ant_power_save_state(btcoexist,
1780                           BTC_PS_WIFI_NATIVE,
1781                           0x0, 0x0);
1782     }
1783 
1784     /* tdma and coex table */
1785     if (!wifi_busy) {
1786         if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1787             btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1788                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1789         } else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1790                 coex_dm->bt_status) ||
1791                (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1792                 coex_dm->bt_status)) {
1793             btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1794                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_IDLE);
1795         } else {
1796             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1797             btc8821a1ant_coex_table_with_type(btcoexist,
1798                               NORMAL_EXEC, 2);
1799         }
1800     } else {
1801         if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
1802             btc8821a1ant_act_wifi_con_bt_acl_busy(btcoexist,
1803                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1804         } else if ((BT_8821A_1ANT_BT_STATUS_SCO_BUSY ==
1805                 coex_dm->bt_status) ||
1806                (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY ==
1807                 coex_dm->bt_status)) {
1808             btc8821a1ant_act_bt_sco_hid_only_busy(btcoexist,
1809                 BT_8821A_1ANT_WIFI_STATUS_CONNECTED_BUSY);
1810         } else {
1811             btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 8);
1812             btc8821a1ant_coex_table_with_type(btcoexist,
1813                               NORMAL_EXEC, 2);
1814         }
1815     }
1816 }
1817 
1818 static void btc8821a1ant_run_sw_coex_mech(struct btc_coexist *btcoexist)
1819 {
1820     struct rtl_priv *rtlpriv = btcoexist->adapter;
1821     u8 algorithm = 0;
1822 
1823     algorithm = btc8821a1ant_action_algorithm(btcoexist);
1824     coex_dm->cur_algorithm = algorithm;
1825 
1826     if (!btc8821a1ant_is_common_action(btcoexist)) {
1827         switch (coex_dm->cur_algorithm) {
1828         case BT_8821A_1ANT_COEX_ALGO_SCO:
1829             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1830                 "[BTCoex], Action algorithm = SCO\n");
1831             btc8821a1ant_action_sco(btcoexist);
1832             break;
1833         case BT_8821A_1ANT_COEX_ALGO_HID:
1834             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1835                 "[BTCoex], Action algorithm = HID\n");
1836             btc8821a1ant_action_hid(btcoexist);
1837             break;
1838         case BT_8821A_1ANT_COEX_ALGO_A2DP:
1839             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1840                 "[BTCoex], Action algorithm = A2DP\n");
1841             btc8821a1ant_action_a2dp(btcoexist);
1842             break;
1843         case BT_8821A_1ANT_COEX_ALGO_A2DP_PANHS:
1844             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1845                 "[BTCoex], Action algorithm = A2DP+PAN(HS)\n");
1846             btc8821a1ant_action_a2dp_pan_hs(btcoexist);
1847             break;
1848         case BT_8821A_1ANT_COEX_ALGO_PANEDR:
1849             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1850                 "[BTCoex], Action algorithm = PAN(EDR)\n");
1851             btc8821a1ant_action_pan_edr(btcoexist);
1852             break;
1853         case BT_8821A_1ANT_COEX_ALGO_PANHS:
1854             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1855                 "[BTCoex], Action algorithm = HS mode\n");
1856             btc8821a1ant_action_pan_hs(btcoexist);
1857             break;
1858         case BT_8821A_1ANT_COEX_ALGO_PANEDR_A2DP:
1859             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1860                 "[BTCoex], Action algorithm = PAN+A2DP\n");
1861             btc8821a1ant_action_pan_edr_a2dp(btcoexist);
1862             break;
1863         case BT_8821A_1ANT_COEX_ALGO_PANEDR_HID:
1864             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1865                 "[BTCoex], Action algorithm = PAN(EDR)+HID\n");
1866             btc8821a1ant_action_pan_edr_hid(btcoexist);
1867             break;
1868         case BT_8821A_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
1869             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1870                 "[BTCoex], Action algorithm = HID+A2DP+PAN\n");
1871             btc8821a1ant_action_hid_a2dp_pan_edr(btcoexist);
1872             break;
1873         case BT_8821A_1ANT_COEX_ALGO_HID_A2DP:
1874             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1875                 "[BTCoex], Action algorithm = HID+A2DP\n");
1876             btc8821a1ant_action_hid_a2dp(btcoexist);
1877             break;
1878         default:
1879             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1880                 "[BTCoex], Action algorithm = coexist All Off!!\n");
1881             /*btc8821a1ant_coex_all_off(btcoexist);*/
1882             break;
1883         }
1884         coex_dm->pre_algorithm = coex_dm->cur_algorithm;
1885     }
1886 }
1887 
1888 static void btc8821a1ant_run_coexist_mechanism(struct btc_coexist *btcoexist)
1889 {
1890     struct rtl_priv *rtlpriv = btcoexist->adapter;
1891     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1892     bool wifi_connected = false, bt_hs_on = false;
1893     bool increase_scan_dev_num = false;
1894     bool bt_ctrl_agg_buf_size = false;
1895     u8 agg_buf_size = 5;
1896     u32 wifi_link_status = 0;
1897     u32 num_of_wifi_link = 0;
1898     bool wifi_under_5g = false;
1899 
1900     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1901         "[BTCoex], RunCoexistMechanism()===>\n");
1902 
1903     if (btcoexist->manual_control) {
1904         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1905             "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
1906         return;
1907     }
1908 
1909     if (btcoexist->stop_coex_dm) {
1910         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1911             "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
1912         return;
1913     }
1914 
1915     if (coex_sta->under_ips) {
1916         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1917             "[BTCoex], wifi is under IPS !!!\n");
1918         return;
1919     }
1920 
1921     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
1922     if (wifi_under_5g) {
1923         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1924             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
1925         btc8821a1ant_coex_under_5g(btcoexist);
1926         return;
1927     }
1928 
1929     if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
1930         (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
1931         (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
1932         increase_scan_dev_num = true;
1933 
1934     btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
1935                &increase_scan_dev_num);
1936 
1937     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1938                &wifi_connected);
1939 
1940     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
1941                &wifi_link_status);
1942     num_of_wifi_link = wifi_link_status >> 16;
1943     if ((num_of_wifi_link >= 2) ||
1944         (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
1945         btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1946         btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
1947                     bt_ctrl_agg_buf_size, agg_buf_size);
1948         btc8821a1ant_action_wifi_multi_port(btcoexist);
1949         return;
1950     }
1951 
1952     if (!bt_link_info->sco_exist && !bt_link_info->hid_exist) {
1953         btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
1954     } else {
1955         if (wifi_connected) {
1956             btc8821a1ant_wifi_rssi_state(btcoexist, 1, 2,
1957                                  30, 0);
1958             btc8821a1ant_limited_tx(btcoexist,
1959                         NORMAL_EXEC, 1, 1,
1960                         0, 1);
1961         } else {
1962             btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC,
1963                         0, 0, 0, 0);
1964         }
1965     }
1966 
1967     if (bt_link_info->sco_exist) {
1968         bt_ctrl_agg_buf_size = true;
1969         agg_buf_size = 0x3;
1970     } else if (bt_link_info->hid_exist) {
1971         bt_ctrl_agg_buf_size = true;
1972         agg_buf_size = 0x5;
1973     } else if (bt_link_info->a2dp_exist || bt_link_info->pan_exist) {
1974         bt_ctrl_agg_buf_size = true;
1975         agg_buf_size = 0x8;
1976     }
1977     btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
1978                 bt_ctrl_agg_buf_size, agg_buf_size);
1979 
1980     btc8821a1ant_run_sw_coex_mech(btcoexist);
1981 
1982     btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
1983     if (coex_sta->c2h_bt_inquiry_page) {
1984         btc8821a1ant_action_bt_inquiry(btcoexist);
1985         return;
1986     } else if (bt_hs_on) {
1987         btc8821a1ant_action_hs(btcoexist);
1988         return;
1989     }
1990 
1991     if (!wifi_connected) {
1992         bool scan = false, link = false, roam = false;
1993 
1994         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1995             "[BTCoex], wifi is non connected-idle !!!\n");
1996 
1997         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
1998         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
1999         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2000 
2001         if (scan || link || roam) {
2002             if (scan)
2003                 btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2004             else
2005                 btc8821a1ant_action_wifi_not_connected_asso_auth(
2006                     btcoexist);
2007         } else {
2008             btc8821a1ant_action_wifi_not_connected(btcoexist);
2009         }
2010     } else {
2011         /* wifi LPS/Busy */
2012         btc8821a1ant_action_wifi_connected(btcoexist);
2013     }
2014 }
2015 
2016 static void btc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2017 {
2018     /* force to reset coex mechanism
2019      * sw all off
2020      */
2021     btc8821a1ant_sw_mechanism(btcoexist, false);
2022 
2023     btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2024 }
2025 
2026 static void btc8821a1ant_init_hw_config(struct btc_coexist *btcoexist,
2027                     bool back_up, bool wifi_only)
2028 {
2029     struct rtl_priv *rtlpriv = btcoexist->adapter;
2030     u8 u1_tmp = 0;
2031     bool wifi_under_5g = false;
2032 
2033     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2034         "[BTCoex], 1Ant Init HW Config!!\n");
2035 
2036     if (wifi_only)
2037         return;
2038 
2039     if (back_up) {
2040         coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
2041                                       0x430);
2042         coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
2043                                       0x434);
2044         coex_dm->backup_retry_limit =
2045             btcoexist->btc_read_2byte(btcoexist, 0x42a);
2046         coex_dm->backup_ampdu_max_time =
2047             btcoexist->btc_read_1byte(btcoexist, 0x456);
2048     }
2049 
2050     /* 0x790[5:0] = 0x5 */
2051     u1_tmp = btcoexist->btc_read_1byte(btcoexist, 0x790);
2052     u1_tmp &= 0xc0;
2053     u1_tmp |= 0x5;
2054     btcoexist->btc_write_1byte(btcoexist, 0x790, u1_tmp);
2055 
2056     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2057 
2058     /* Antenna config */
2059     if (wifi_under_5g)
2060         btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT,
2061                       true, false);
2062     else
2063         btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_PTA,
2064                       true, false);
2065     /* PTA parameter */
2066     btc8821a1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
2067 
2068     /* Enable counter statistics
2069      * 0x76e[3] =1, WLAN_Act control by PTA
2070      */
2071     btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
2072     btcoexist->btc_write_1byte(btcoexist, 0x778, 0x3);
2073     btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
2074 }
2075 
2076 /**************************************************************
2077  * extern function start with ex_btc8821a1ant_
2078  **************************************************************/
2079 void ex_btc8821a1ant_init_hwconfig(struct btc_coexist *btcoexist, bool wifionly)
2080 {
2081     btc8821a1ant_init_hw_config(btcoexist, true, wifionly);
2082     btcoexist->auto_report_1ant = true;
2083 }
2084 
2085 void ex_btc8821a1ant_init_coex_dm(struct btc_coexist *btcoexist)
2086 {
2087     struct rtl_priv *rtlpriv = btcoexist->adapter;
2088 
2089     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2090         "[BTCoex], Coex Mechanism Init!!\n");
2091 
2092     btcoexist->stop_coex_dm = false;
2093 
2094     btc8821a1ant_init_coex_dm(btcoexist);
2095 
2096     btc8821a1ant_query_bt_info(btcoexist);
2097 }
2098 
2099 void ex_btc8821a1ant_display_coex_info(struct btc_coexist *btcoexist,
2100                        struct seq_file *m)
2101 {
2102     struct btc_board_info *board_info = &btcoexist->board_info;
2103     struct btc_stack_info *stack_info = &btcoexist->stack_info;
2104     struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2105     u8 u1_tmp[4], i, bt_info_ext, ps_tdma_case = 0;
2106     u16 u2_tmp[4];
2107     u32 u4_tmp[4];
2108     bool roam = false, scan = false, link = false, wifi_under_5g = false;
2109     bool bt_hs_on = false, wifi_busy = false;
2110     long wifi_rssi = 0, bt_hs_rssi = 0;
2111     u32 wifi_bw, wifi_traffic_dir;
2112     u8 wifi_dot11_chnl, wifi_hs_chnl;
2113     u32 fw_ver = 0, bt_patch_ver = 0;
2114 
2115     seq_puts(m, "\n ============[BT Coexist info]============");
2116 
2117     if (btcoexist->manual_control) {
2118         seq_puts(m, "\n ============[Under Manual Control]============");
2119         seq_puts(m, "\n ==========================================");
2120     }
2121     if (btcoexist->stop_coex_dm) {
2122         seq_puts(m, "\n ============[Coex is STOPPED]============");
2123         seq_puts(m, "\n ==========================================");
2124     }
2125 
2126     seq_printf(m, "\n %-35s = %d/ %d/ %d",
2127            "Ant PG Num/ Ant Mech/ Ant Pos:",
2128            board_info->pg_ant_num,
2129            board_info->btdm_ant_num,
2130            board_info->btdm_ant_pos);
2131 
2132     seq_printf(m, "\n %-35s = %s / %d", "BT stack/ hci ext ver",
2133            ((stack_info->profile_notified) ? "Yes" : "No"),
2134            stack_info->hci_version);
2135 
2136     btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2137                &bt_patch_ver);
2138     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2139     seq_printf(m, "\n %-35s = %d_%x/ 0x%x/ 0x%x(%d)",
2140            "CoexVer/ FwVer/ PatchVer",
2141            glcoex_ver_date_8821a_1ant,
2142            glcoex_ver_8821a_1ant,
2143            fw_ver, bt_patch_ver,
2144            bt_patch_ver);
2145 
2146     btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION,
2147                &bt_hs_on);
2148     btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL,
2149                &wifi_dot11_chnl);
2150     btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL,
2151                &wifi_hs_chnl);
2152     seq_printf(m, "\n %-35s = %d / %d(%d)",
2153            "Dot11 channel / HsChnl(HsMode)",
2154            wifi_dot11_chnl, wifi_hs_chnl, bt_hs_on);
2155 
2156     seq_printf(m, "\n %-35s = %3ph ",
2157            "H2C Wifi inform bt chnl Info",
2158            coex_dm->wifi_chnl_info);
2159 
2160     btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
2161     btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
2162     seq_printf(m, "\n %-35s = %d/ %d", "Wifi rssi/ HS rssi",
2163            (int)wifi_rssi, (int)bt_hs_rssi);
2164 
2165     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2166     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2167     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2168     seq_printf(m, "\n %-35s = %d/ %d/ %d ", "Wifi link/ roam/ scan",
2169            link, roam, scan);
2170 
2171     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G,
2172                &wifi_under_5g);
2173     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW,
2174                &wifi_bw);
2175     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
2176                &wifi_busy);
2177     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2178                &wifi_traffic_dir);
2179     seq_printf(m, "\n %-35s = %s / %s/ %s ", "Wifi status",
2180            (wifi_under_5g ? "5G" : "2.4G"),
2181            ((wifi_bw == BTC_WIFI_BW_LEGACY) ? "Legacy" :
2182            (((wifi_bw == BTC_WIFI_BW_HT40) ? "HT40" : "HT20"))),
2183            ((!wifi_busy) ? "idle" :
2184            ((wifi_traffic_dir == BTC_WIFI_TRAFFIC_TX) ?
2185            "uplink" : "downlink")));
2186     seq_printf(m, "\n %-35s = [%s/ %d/ %d] ",
2187            "BT [status/ rssi/ retryCnt]",
2188            ((coex_sta->bt_disabled) ? ("disabled") :
2189            ((coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page scan") :
2190            ((BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2191              coex_dm->bt_status) ?
2192            "non-connected idle" :
2193            ((BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE ==
2194              coex_dm->bt_status) ?
2195            "connected-idle" : "busy")))),
2196            coex_sta->bt_rssi, coex_sta->bt_retry_cnt);
2197 
2198     seq_printf(m, "\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP",
2199            bt_link_info->sco_exist,
2200            bt_link_info->hid_exist,
2201            bt_link_info->pan_exist,
2202            bt_link_info->a2dp_exist);
2203     btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_BT_LINK_INFO, m);
2204 
2205     bt_info_ext = coex_sta->bt_info_ext;
2206     seq_printf(m, "\n %-35s = %s",
2207            "BT Info A2DP rate",
2208            (bt_info_ext & BIT0) ?
2209            "Basic rate" : "EDR rate");
2210 
2211     for (i = 0; i < BT_INFO_SRC_8821A_1ANT_MAX; i++) {
2212         if (coex_sta->bt_info_c2h_cnt[i]) {
2213             seq_printf(m, "\n %-35s = %7ph(%d)",
2214                    glbt_info_src_8821a_1ant[i],
2215                    coex_sta->bt_info_c2h[i],
2216                    coex_sta->bt_info_c2h_cnt[i]);
2217         }
2218     }
2219     seq_printf(m, "\n %-35s = %s/%s, (0x%x/0x%x)",
2220            "PS state, IPS/LPS, (lps/rpwm)",
2221            ((coex_sta->under_ips ? "IPS ON" : "IPS OFF")),
2222            ((coex_sta->under_lps ? "LPS ON" : "LPS OFF")),
2223            btcoexist->bt_info.lps_val,
2224            btcoexist->bt_info.rpwm_val);
2225     btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_FW_PWR_MODE_CMD, m);
2226 
2227     if (!btcoexist->manual_control) {
2228         /* Sw mechanism*/
2229         seq_printf(m, "\n %-35s",
2230                "============[Sw mechanism]============");
2231 
2232         seq_printf(m, "\n %-35s = %d", "SM[LowPenaltyRA]",
2233                coex_dm->cur_low_penalty_ra);
2234 
2235         seq_printf(m, "\n %-35s = %s/ %s/ %d ",
2236                "DelBA/ BtCtrlAgg/ AggSize",
2237                (btcoexist->bt_info.reject_agg_pkt ? "Yes" : "No"),
2238                (btcoexist->bt_info.bt_ctrl_buf_size ? "Yes" : "No"),
2239                btcoexist->bt_info.agg_buf_size);
2240         seq_printf(m, "\n %-35s = 0x%x ", "Rate Mask",
2241                btcoexist->bt_info.ra_mask);
2242 
2243         /* Fw mechanism */
2244         seq_printf(m, "\n %-35s",
2245                "============[Fw mechanism]============");
2246 
2247         ps_tdma_case = coex_dm->cur_ps_tdma;
2248         seq_printf(m, "\n %-35s = %5ph case-%d (auto:%d)",
2249                "PS TDMA",
2250                coex_dm->ps_tdma_para,
2251                ps_tdma_case,
2252                coex_dm->auto_tdma_adjust);
2253 
2254         seq_printf(m, "\n %-35s = 0x%x ",
2255                "Latest error condition(should be 0)",
2256                coex_dm->error_condition);
2257 
2258         seq_printf(m, "\n %-35s = %d ", "IgnWlanAct",
2259                coex_dm->cur_ignore_wlan_act);
2260     }
2261 
2262     /* Hw setting */
2263     seq_printf(m, "\n %-35s", "============[Hw setting]============");
2264 
2265     seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2266            "backup ARFR1/ARFR2/RL/AMaxTime",
2267            coex_dm->backup_arfr_cnt1,
2268            coex_dm->backup_arfr_cnt2,
2269            coex_dm->backup_retry_limit,
2270            coex_dm->backup_ampdu_max_time);
2271 
2272     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
2273     u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
2274     u2_tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
2275     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x456);
2276     seq_printf(m, "\n %-35s = 0x%x/0x%x/0x%x/0x%x",
2277            "0x430/0x434/0x42a/0x456",
2278            u4_tmp[0], u4_tmp[1], u2_tmp[0], u1_tmp[0]);
2279 
2280     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
2281     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc58);
2282     seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "0x778/ 0xc58[29:25]",
2283            u1_tmp[0], (u4_tmp[0] & 0x3e000000) >> 25);
2284 
2285     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x8db);
2286     seq_printf(m, "\n %-35s = 0x%x", "0x8db[6:5]",
2287            ((u1_tmp[0] & 0x60) >> 5));
2288 
2289     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x975);
2290     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xcb4);
2291     seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2292            "0xcb4[29:28]/0xcb4[7:0]/0x974[9:8]",
2293            (u4_tmp[0] & 0x30000000) >> 28,
2294             u4_tmp[0] & 0xff,
2295             u1_tmp[0] & 0x3);
2296 
2297     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x40);
2298     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x4c);
2299     u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x64);
2300     seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x",
2301            "0x40/0x4c[24:23]/0x64[0]",
2302            u1_tmp[0], ((u4_tmp[0] & 0x01800000) >> 23),
2303            u1_tmp[1] & 0x1);
2304 
2305     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
2306     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
2307     seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "0x550(bcn ctrl)/0x522",
2308            u4_tmp[0], u1_tmp[0]);
2309 
2310     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xc50);
2311     seq_printf(m, "\n %-35s = 0x%x", "0xc50(dig)",
2312            u4_tmp[0] & 0xff);
2313 
2314     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0xf48);
2315     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0xa5d);
2316     u1_tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0xa5c);
2317     seq_printf(m, "\n %-35s = 0x%x/ 0x%x", "OFDM-FA/ CCK-FA",
2318            u4_tmp[0], (u1_tmp[0] << 8) + u1_tmp[1]);
2319 
2320     u4_tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
2321     u4_tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
2322     u4_tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
2323     u1_tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x6cc);
2324     seq_printf(m, "\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
2325            "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)",
2326            u4_tmp[0], u4_tmp[1], u4_tmp[2], u1_tmp[0]);
2327 
2328     seq_printf(m, "\n %-35s = %d/ %d", "0x770(high-pri rx/tx)",
2329            coex_sta->high_priority_rx, coex_sta->high_priority_tx);
2330     seq_printf(m, "\n %-35s = %d/ %d", "0x774(low-pri rx/tx)",
2331            coex_sta->low_priority_rx, coex_sta->low_priority_tx);
2332     if (btcoexist->auto_report_1ant)
2333         btc8821a1ant_monitor_bt_ctr(btcoexist);
2334     btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS, m);
2335 }
2336 
2337 void ex_btc8821a1ant_ips_notify(struct btc_coexist *btcoexist, u8 type)
2338 {
2339     struct rtl_priv *rtlpriv = btcoexist->adapter;
2340     bool wifi_under_5g = false;
2341 
2342     if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2343         return;
2344     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2345     if (wifi_under_5g) {
2346         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2347             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2348         btc8821a1ant_coex_under_5g(btcoexist);
2349         return;
2350     }
2351 
2352     if (BTC_IPS_ENTER == type) {
2353         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2354             "[BTCoex], IPS ENTER notify\n");
2355         coex_sta->under_ips = true;
2356         btc8821a1ant_set_ant_path(btcoexist,
2357                       BTC_ANT_PATH_BT, false, true);
2358         /* set PTA control */
2359         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2360         btc8821a1ant_coex_table_with_type(btcoexist,
2361                           NORMAL_EXEC, 0);
2362     } else if (BTC_IPS_LEAVE == type) {
2363         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2364             "[BTCoex], IPS LEAVE notify\n");
2365         coex_sta->under_ips = false;
2366 
2367         btc8821a1ant_init_hw_config(btcoexist, false, false);
2368         btc8821a1ant_init_coex_dm(btcoexist);
2369         btc8821a1ant_query_bt_info(btcoexist);
2370     }
2371 }
2372 
2373 void ex_btc8821a1ant_lps_notify(struct btc_coexist *btcoexist, u8 type)
2374 {
2375     struct rtl_priv *rtlpriv = btcoexist->adapter;
2376 
2377     if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2378         return;
2379 
2380     if (BTC_LPS_ENABLE == type) {
2381         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2382             "[BTCoex], LPS ENABLE notify\n");
2383         coex_sta->under_lps = true;
2384     } else if (BTC_LPS_DISABLE == type) {
2385         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2386             "[BTCoex], LPS DISABLE notify\n");
2387         coex_sta->under_lps = false;
2388     }
2389 }
2390 
2391 void ex_btc8821a1ant_scan_notify(struct btc_coexist *btcoexist, u8 type)
2392 {
2393     struct rtl_priv *rtlpriv = btcoexist->adapter;
2394     bool wifi_connected = false, bt_hs_on = false;
2395     bool bt_ctrl_agg_buf_size = false;
2396     bool wifi_under_5g = false;
2397     u32 wifi_link_status = 0;
2398     u32 num_of_wifi_link = 0;
2399     u8 agg_buf_size = 5;
2400 
2401     if (btcoexist->manual_control || btcoexist->stop_coex_dm)
2402         return;
2403     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2404     if (wifi_under_5g) {
2405         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2406             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2407         btc8821a1ant_coex_under_5g(btcoexist);
2408         return;
2409     }
2410 
2411     if (type == BTC_SCAN_START) {
2412         coex_sta->wifi_is_high_pri_task = true;
2413         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2414             "[BTCoex], SCAN START notify\n");
2415 
2416         /* Force antenna setup for no scan result issue */
2417         btc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 8);
2418     } else {
2419         coex_sta->wifi_is_high_pri_task = false;
2420         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2421             "[BTCoex], SCAN FINISH notify\n");
2422     }
2423 
2424     if (coex_sta->bt_disabled)
2425         return;
2426 
2427     btcoexist->btc_get(btcoexist,
2428          BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2429     btcoexist->btc_get(btcoexist,
2430          BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2431 
2432     btc8821a1ant_query_bt_info(btcoexist);
2433 
2434     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2435                &wifi_link_status);
2436     num_of_wifi_link = wifi_link_status >> 16;
2437     if (num_of_wifi_link >= 2) {
2438         btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2439         btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2440                     bt_ctrl_agg_buf_size, agg_buf_size);
2441         btc8821a1ant_action_wifi_multi_port(btcoexist);
2442         return;
2443     }
2444 
2445     if (coex_sta->c2h_bt_inquiry_page) {
2446         btc8821a1ant_action_bt_inquiry(btcoexist);
2447         return;
2448     } else if (bt_hs_on) {
2449         btc8821a1ant_action_hs(btcoexist);
2450         return;
2451     }
2452 
2453     if (BTC_SCAN_START == type) {
2454         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2455             "[BTCoex], SCAN START notify\n");
2456         if (!wifi_connected) {
2457             /* non-connected scan */
2458             btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2459         } else {
2460             /* wifi is connected */
2461             btc8821a1ant_action_wifi_connected_scan(btcoexist);
2462         }
2463     } else if (BTC_SCAN_FINISH == type) {
2464         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2465             "[BTCoex], SCAN FINISH notify\n");
2466         if (!wifi_connected) {
2467             /* non-connected scan */
2468             btc8821a1ant_action_wifi_not_connected(btcoexist);
2469         } else {
2470             btc8821a1ant_action_wifi_connected(btcoexist);
2471         }
2472     }
2473 }
2474 
2475 void ex_btc8821a1ant_connect_notify(struct btc_coexist *btcoexist, u8 type)
2476 {
2477     struct rtl_priv *rtlpriv = btcoexist->adapter;
2478     bool wifi_connected = false, bt_hs_on = false;
2479     u32 wifi_link_status = 0;
2480     u32 num_of_wifi_link = 0;
2481     bool bt_ctrl_agg_buf_size = false;
2482     bool wifi_under_5g = false;
2483     u8 agg_buf_size = 5;
2484 
2485     if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2486         coex_sta->bt_disabled)
2487         return;
2488     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2489     if (wifi_under_5g) {
2490         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2491             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2492         btc8821a1ant_coex_under_5g(btcoexist);
2493         return;
2494     }
2495 
2496     if (type == BTC_ASSOCIATE_START) {
2497         coex_sta->wifi_is_high_pri_task = true;
2498         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2499             "[BTCoex], CONNECT START notify\n");
2500         coex_dm->arp_cnt = 0;
2501     } else {
2502         coex_sta->wifi_is_high_pri_task = false;
2503         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2504             "[BTCoex], CONNECT FINISH notify\n");
2505         coex_dm->arp_cnt = 0;
2506     }
2507 
2508     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2509                &wifi_link_status);
2510     num_of_wifi_link = wifi_link_status >> 16;
2511     if (num_of_wifi_link >= 2) {
2512         btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2513         btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2514                     bt_ctrl_agg_buf_size, agg_buf_size);
2515         btc8821a1ant_action_wifi_multi_port(btcoexist);
2516         return;
2517     }
2518 
2519     btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2520     if (coex_sta->c2h_bt_inquiry_page) {
2521         btc8821a1ant_action_bt_inquiry(btcoexist);
2522         return;
2523     } else if (bt_hs_on) {
2524         btc8821a1ant_action_hs(btcoexist);
2525         return;
2526     }
2527 
2528     if (BTC_ASSOCIATE_START == type) {
2529         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2530             "[BTCoex], CONNECT START notify\n");
2531         btc8821a1ant_act_wifi_not_conn_scan(btcoexist);
2532     } else if (BTC_ASSOCIATE_FINISH == type) {
2533         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2534             "[BTCoex], CONNECT FINISH notify\n");
2535 
2536         btcoexist->btc_get(btcoexist,
2537              BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2538         if (!wifi_connected) {
2539             /* non-connected scan */
2540             btc8821a1ant_action_wifi_not_connected(btcoexist);
2541         } else {
2542             btc8821a1ant_action_wifi_connected(btcoexist);
2543         }
2544     }
2545 }
2546 
2547 void ex_btc8821a1ant_media_status_notify(struct btc_coexist *btcoexist,
2548                      u8 type)
2549 {
2550     struct rtl_priv *rtlpriv = btcoexist->adapter;
2551     u8 h2c_parameter[3] = {0};
2552     u32 wifi_bw;
2553     u8 wifi_central_chnl;
2554     bool wifi_under_5g = false;
2555 
2556     if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2557         coex_sta->bt_disabled)
2558         return;
2559     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2560     if (wifi_under_5g) {
2561         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2562             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2563         btc8821a1ant_coex_under_5g(btcoexist);
2564         return;
2565     }
2566 
2567     if (BTC_MEDIA_CONNECT == type) {
2568         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2569             "[BTCoex], MEDIA connect notify\n");
2570     } else {
2571         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2572             "[BTCoex], MEDIA disconnect notify\n");
2573         coex_dm->arp_cnt = 0;
2574     }
2575 
2576     /* only 2.4G we need to inform bt the chnl mask */
2577     btcoexist->btc_get(btcoexist,
2578                BTC_GET_U1_WIFI_CENTRAL_CHNL,
2579                &wifi_central_chnl);
2580     if ((type == BTC_MEDIA_CONNECT) &&
2581         (wifi_central_chnl <= 14)) {
2582         h2c_parameter[0] = 0x0;
2583         h2c_parameter[1] = wifi_central_chnl;
2584         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2585         if (wifi_bw == BTC_WIFI_BW_HT40)
2586             h2c_parameter[2] = 0x30;
2587         else
2588             h2c_parameter[2] = 0x20;
2589     }
2590 
2591     coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
2592     coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
2593     coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
2594 
2595     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2596         "[BTCoex], FW write 0x66 = 0x%x\n",
2597         h2c_parameter[0] << 16 |
2598         h2c_parameter[1] << 8 |
2599         h2c_parameter[2]);
2600 
2601     btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
2602 }
2603 
2604 void ex_btc8821a1ant_special_packet_notify(struct btc_coexist *btcoexist,
2605                        u8 type)
2606 {
2607     struct rtl_priv *rtlpriv = btcoexist->adapter;
2608     bool bt_hs_on = false;
2609     bool bt_ctrl_agg_buf_size = false;
2610     bool wifi_under_5g = false;
2611     u32 wifi_link_status = 0;
2612     u32 num_of_wifi_link = 0;
2613     u8 agg_buf_size = 5;
2614 
2615     if (btcoexist->manual_control || btcoexist->stop_coex_dm ||
2616         coex_sta->bt_disabled)
2617         return;
2618 
2619     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2620     if (wifi_under_5g) {
2621         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2622             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2623         btc8821a1ant_coex_under_5g(btcoexist);
2624         return;
2625     }
2626 
2627     if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
2628         type == BTC_PACKET_ARP) {
2629         coex_sta->wifi_is_high_pri_task = true;
2630 
2631         if (type == BTC_PACKET_ARP) {
2632             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2633                 "[BTCoex], specific Packet ARP notify\n");
2634         } else {
2635             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2636                 "[BTCoex], specific Packet DHCP or EAPOL notify\n");
2637         }
2638     } else {
2639         coex_sta->wifi_is_high_pri_task = false;
2640         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2641             "[BTCoex], specific Packet [Type = %d] notify\n",
2642             type);
2643     }
2644 
2645     coex_sta->special_pkt_period_cnt = 0;
2646 
2647     btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2648                &wifi_link_status);
2649     num_of_wifi_link = wifi_link_status >> 16;
2650     if (num_of_wifi_link >= 2) {
2651         btc8821a1ant_limited_tx(btcoexist, NORMAL_EXEC, 0, 0, 0, 0);
2652         btc8821a1ant_limited_rx(btcoexist, NORMAL_EXEC, false,
2653                     bt_ctrl_agg_buf_size, agg_buf_size);
2654         btc8821a1ant_action_wifi_multi_port(btcoexist);
2655         return;
2656     }
2657 
2658     btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2659     if (coex_sta->c2h_bt_inquiry_page) {
2660         btc8821a1ant_action_bt_inquiry(btcoexist);
2661         return;
2662     } else if (bt_hs_on) {
2663         btc8821a1ant_action_hs(btcoexist);
2664         return;
2665     }
2666 
2667     if (type == BTC_PACKET_DHCP || type == BTC_PACKET_EAPOL ||
2668         type == BTC_PACKET_ARP) {
2669         if (type == BTC_PACKET_ARP) {
2670             coex_dm->arp_cnt++;
2671             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2672                 "[BTCoex], ARP Packet Count = %d\n",
2673                 coex_dm->arp_cnt);
2674             if (coex_dm->arp_cnt >= 10)
2675                 /* if APR PKT > 10 after connect, do not go to
2676                  * btc8821a1ant_act_wifi_conn_sp_pkt
2677                  */
2678                 return;
2679         }
2680 
2681         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2682             "[BTCoex], special Packet(%d) notify\n", type);
2683         btc8821a1ant_act_wifi_conn_sp_pkt(btcoexist);
2684     }
2685 }
2686 
2687 void ex_btc8821a1ant_bt_info_notify(struct btc_coexist *btcoexist,
2688                     u8 *tmp_buf, u8 length)
2689 {
2690     struct rtl_priv *rtlpriv = btcoexist->adapter;
2691     u8 i;
2692     u8 bt_info = 0;
2693     u8 rsp_source = 0;
2694     bool wifi_connected = false;
2695     bool bt_busy = false;
2696     bool wifi_under_5g = false;
2697 
2698     coex_sta->c2h_bt_info_req_sent = false;
2699 
2700     btcoexist->btc_get(btcoexist,
2701          BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2702 
2703     rsp_source = tmp_buf[0] & 0xf;
2704     if (rsp_source >= BT_INFO_SRC_8821A_1ANT_MAX)
2705         rsp_source = BT_INFO_SRC_8821A_1ANT_WIFI_FW;
2706     coex_sta->bt_info_c2h_cnt[rsp_source]++;
2707 
2708     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2709         "[BTCoex], Bt info[%d], length = %d, hex data = [",
2710         rsp_source, length);
2711     for (i = 0; i < length; i++) {
2712         coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
2713         if (i == 1)
2714             bt_info = tmp_buf[i];
2715         if (i == length - 1) {
2716             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2717                 "0x%02x]\n", tmp_buf[i]);
2718         } else {
2719             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2720                 "0x%02x, ", tmp_buf[i]);
2721         }
2722     }
2723 
2724     if (BT_INFO_SRC_8821A_1ANT_WIFI_FW != rsp_source) {
2725         /* [3:0] */
2726         coex_sta->bt_retry_cnt =
2727             coex_sta->bt_info_c2h[rsp_source][2] & 0xf;
2728 
2729         coex_sta->bt_rssi =
2730             coex_sta->bt_info_c2h[rsp_source][3] * 2 + 10;
2731 
2732         coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
2733 
2734         coex_sta->bt_tx_rx_mask =
2735             (coex_sta->bt_info_c2h[rsp_source][2] & 0x40);
2736         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TX_RX_MASK,
2737                    &coex_sta->bt_tx_rx_mask);
2738         if (!coex_sta->bt_tx_rx_mask) {
2739             /* BT into is responded by BT FW and BT RF REG 0x3C !=
2740              * 0x15 => Need to switch BT TRx Mask
2741              */
2742             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2743                 "[BTCoex], Switch BT TRx Mask since BT RF REG 0x3C != 0x15\n");
2744             btcoexist->btc_set_bt_reg(btcoexist, BTC_BT_REG_RF,
2745                           0x3c, 0x15);
2746         }
2747 
2748         /* Here we need to resend some wifi info to BT
2749          * because bt is reset and lost the info
2750          */
2751         if (coex_sta->bt_info_ext & BIT1) {
2752             rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2753                 "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
2754             btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2755                        &wifi_connected);
2756             if (wifi_connected) {
2757                 ex_btc8821a1ant_media_status_notify(btcoexist,
2758                                    BTC_MEDIA_CONNECT);
2759             } else {
2760                 ex_btc8821a1ant_media_status_notify(btcoexist,
2761                                    BTC_MEDIA_DISCONNECT);
2762             }
2763         }
2764 
2765         if ((coex_sta->bt_info_ext & BIT3) && !wifi_under_5g) {
2766             if (!btcoexist->manual_control &&
2767                 !btcoexist->stop_coex_dm) {
2768                 rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2769                     "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
2770                 btc8821a1ant_ignore_wlan_act(btcoexist,
2771                                  FORCE_EXEC,
2772                                  false);
2773             }
2774         }
2775     }
2776 
2777     /* check BIT2 first ==> check if bt is under inquiry or page scan */
2778     if (bt_info & BT_INFO_8821A_1ANT_B_INQ_PAGE)
2779         coex_sta->c2h_bt_inquiry_page = true;
2780     else
2781         coex_sta->c2h_bt_inquiry_page = false;
2782 
2783     /* set link exist status */
2784     if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
2785         coex_sta->bt_link_exist = false;
2786         coex_sta->pan_exist = false;
2787         coex_sta->a2dp_exist = false;
2788         coex_sta->hid_exist = false;
2789         coex_sta->sco_exist = false;
2790     } else {
2791         /* connection exists */
2792         coex_sta->bt_link_exist = true;
2793         if (bt_info & BT_INFO_8821A_1ANT_B_FTP)
2794             coex_sta->pan_exist = true;
2795         else
2796             coex_sta->pan_exist = false;
2797         if (bt_info & BT_INFO_8821A_1ANT_B_A2DP)
2798             coex_sta->a2dp_exist = true;
2799         else
2800             coex_sta->a2dp_exist = false;
2801         if (bt_info & BT_INFO_8821A_1ANT_B_HID)
2802             coex_sta->hid_exist = true;
2803         else
2804             coex_sta->hid_exist = false;
2805         if (bt_info & BT_INFO_8821A_1ANT_B_SCO_ESCO)
2806             coex_sta->sco_exist = true;
2807         else
2808             coex_sta->sco_exist = false;
2809     }
2810 
2811     btc8821a1ant_update_bt_link_info(btcoexist);
2812 
2813     /* mask profile bit for connect-ilde identification
2814      * (for CSR case: A2DP idle --> 0x41)
2815      */
2816     bt_info = bt_info & 0x1f;
2817 
2818     if (!(bt_info & BT_INFO_8821A_1ANT_B_CONNECTION)) {
2819         coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
2820         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2821             "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
2822     } else if (bt_info == BT_INFO_8821A_1ANT_B_CONNECTION) {
2823         /* connection exists but no busy */
2824         coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_CONNECTED_IDLE;
2825         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2826             "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
2827     } else if ((bt_info&BT_INFO_8821A_1ANT_B_SCO_ESCO) ||
2828         (bt_info & BT_INFO_8821A_1ANT_B_SCO_BUSY)) {
2829         coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_SCO_BUSY;
2830         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2831             "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
2832     } else if (bt_info & BT_INFO_8821A_1ANT_B_ACL_BUSY) {
2833         if (BT_8821A_1ANT_BT_STATUS_ACL_BUSY != coex_dm->bt_status)
2834             coex_dm->auto_tdma_adjust = false;
2835         coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_ACL_BUSY;
2836         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2837             "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
2838     } else {
2839         coex_dm->bt_status = BT_8821A_1ANT_BT_STATUS_MAX;
2840         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2841             "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
2842     }
2843 
2844     if ((BT_8821A_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2845         (BT_8821A_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2846         (BT_8821A_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2847         bt_busy = true;
2848     else
2849         bt_busy = false;
2850     btcoexist->btc_set(btcoexist,
2851                BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2852 
2853     btc8821a1ant_run_coexist_mechanism(btcoexist);
2854 }
2855 
2856 void ex_btc8821a1ant_halt_notify(struct btc_coexist *btcoexist)
2857 {
2858     struct rtl_priv *rtlpriv = btcoexist->adapter;
2859     bool wifi_under_5g = false;
2860 
2861     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2862         "[BTCoex], Halt notify\n");
2863     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2864     if (wifi_under_5g) {
2865         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2866             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2867         btc8821a1ant_coex_under_5g(btcoexist);
2868         return;
2869     }
2870 
2871 
2872     btcoexist->stop_coex_dm = true;
2873 
2874     btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false, true);
2875     btc8821a1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, true);
2876 
2877     btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE, 0x0, 0x0);
2878     btc8821a1ant_ps_tdma(btcoexist, FORCE_EXEC, false, 0);
2879 
2880     ex_btc8821a1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
2881 }
2882 
2883 void ex_btc8821a1ant_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
2884 {
2885     struct rtl_priv *rtlpriv = btcoexist->adapter;
2886     bool wifi_under_5g = false;
2887 
2888     btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
2889     if (wifi_under_5g) {
2890         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2891             "[BTCoex], RunCoexistMechanism(), return for 5G <===\n");
2892         btc8821a1ant_coex_under_5g(btcoexist);
2893         return;
2894     }
2895 
2896     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2897         "[BTCoex], Pnp notify\n");
2898 
2899     if (BTC_WIFI_PNP_SLEEP == pnp_state) {
2900         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2901             "[BTCoex], Pnp notify to SLEEP\n");
2902         /* BT should clear UnderIPS/UnderLPS state to avoid mismatch
2903          * state after wakeup.
2904          */
2905         coex_sta->under_ips = false;
2906         coex_sta->under_lps = false;
2907         btcoexist->stop_coex_dm = true;
2908         btc8821a1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
2909                           0x0, 0x0);
2910         btc8821a1ant_ps_tdma(btcoexist, NORMAL_EXEC, false, 0);
2911         btc8821a1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2912         btc8821a1ant_set_ant_path(btcoexist, BTC_ANT_PATH_BT, false,
2913                       true);
2914     } else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
2915         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2916             "[BTCoex], Pnp notify to WAKE UP\n");
2917         btcoexist->stop_coex_dm = false;
2918         btc8821a1ant_init_hw_config(btcoexist, false, false);
2919         btc8821a1ant_init_coex_dm(btcoexist);
2920         btc8821a1ant_query_bt_info(btcoexist);
2921     }
2922 }
2923 
2924 void ex_btc8821a1ant_periodical(struct btc_coexist *btcoexist)
2925 {
2926     struct rtl_priv *rtlpriv = btcoexist->adapter;
2927     static u8 dis_ver_info_cnt;
2928     u32 fw_ver = 0, bt_patch_ver = 0;
2929     struct btc_board_info *board_info = &btcoexist->board_info;
2930     struct btc_stack_info *stack_info = &btcoexist->stack_info;
2931 
2932     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2933         "[BTCoex], ==========================Periodical===========================\n");
2934 
2935     if (dis_ver_info_cnt <= 5) {
2936         dis_ver_info_cnt += 1;
2937         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2938             "[BTCoex], ****************************************************************\n");
2939         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2940             "[BTCoex], Ant PG Num/ Ant Mech/ Ant Pos = %d/ %d/ %d\n",
2941             board_info->pg_ant_num,
2942             board_info->btdm_ant_num,
2943             board_info->btdm_ant_pos);
2944         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2945             "[BTCoex], BT stack/ hci ext ver = %s / %d\n",
2946             stack_info->profile_notified ? "Yes" : "No",
2947             stack_info->hci_version);
2948         btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
2949                    &bt_patch_ver);
2950         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
2951         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2952             "[BTCoex], CoexVer/ FwVer/ PatchVer = %d_%x/ 0x%x/ 0x%x(%d)\n",
2953             glcoex_ver_date_8821a_1ant,
2954             glcoex_ver_8821a_1ant,
2955             fw_ver, bt_patch_ver,
2956             bt_patch_ver);
2957         rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
2958             "[BTCoex], ****************************************************************\n");
2959     }
2960 
2961     if (!btcoexist->auto_report_1ant) {
2962         btc8821a1ant_query_bt_info(btcoexist);
2963         btc8821a1ant_monitor_bt_ctr(btcoexist);
2964     } else {
2965         coex_sta->special_pkt_period_cnt++;
2966     }
2967 }