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