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