net: wireless: rockchip_wlan: add rtl8723cs support
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723cs / hal / btc / halbtc8703b1ant.c
1 /* ************************************************************
2  * Description:
3  *
4  * This file is for RTL8703B Co-exist mechanism
5  *
6  * History
7  * 2012/11/15 Cosa first check in.
8  *
9  * ************************************************************ */
10
11 /* ************************************************************
12  * include files
13  * ************************************************************ */
14 #include "mp_precomp.h"
15
16 #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1)
17
18 #if (RTL8703B_SUPPORT == 1)
19 /* ************************************************************
20  * Global variables, these are static variables
21  * ************************************************************ */
22 static u8       *trace_buf = &gl_btc_trace_buf[0];
23 static struct  coex_dm_8703b_1ant               glcoex_dm_8703b_1ant;
24 static struct  coex_dm_8703b_1ant       *coex_dm = &glcoex_dm_8703b_1ant;
25 static struct  coex_sta_8703b_1ant              glcoex_sta_8703b_1ant;
26 static struct  coex_sta_8703b_1ant      *coex_sta = &glcoex_sta_8703b_1ant;
27 static struct  psdscan_sta_8703b_1ant   gl_psd_scan_8703b_1ant;
28 static struct  psdscan_sta_8703b_1ant *psd_scan = &gl_psd_scan_8703b_1ant;
29
30
31 const char *const glbt_info_src_8703b_1ant[] = {
32         "BT Info[wifi fw]",
33         "BT Info[bt rsp]",
34         "BT Info[bt auto report]",
35 };
36 /* ************************************************************
37  * BtCoex Version Format:
38  * 1. date :                    glcoex_ver_date_XXXXX_1ant
39  * 2. WifiCoexVersion : glcoex_ver_XXXX_1ant
40  * 3. BtCoexVersion :   glcoex_ver_btdesired_XXXXX_1ant
41  * 4. others :                  glcoex_ver_XXXXXX_XXXXX_1ant
42  *
43  * Variable should be indicated IC and Antenna numbers !!!
44  * Please strictly follow this order and naming style !!!
45  *
46  * ************************************************************ */
47 u32     glcoex_ver_date_8703b_1ant = 20170214;
48 u32     glcoex_ver_8703b_1ant = 0x15;
49 u32     glcoex_ver_btdesired_8703b_1ant = 0x10;
50
51
52 /* ************************************************************
53  * local function proto type if needed
54  * ************************************************************
55  * ************************************************************
56  * local function start with halbtc8703b1ant_
57  * ************************************************************ */
58 u8 halbtc8703b1ant_bt_rssi_state(u8 level_num, u8 rssi_thresh, u8 rssi_thresh1)
59 {
60         s32                     bt_rssi = 0;
61         u8                      bt_rssi_state = coex_sta->pre_bt_rssi_state;
62
63         bt_rssi = coex_sta->bt_rssi;
64
65         if (level_num == 2) {
66                 if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
67                     (coex_sta->pre_bt_rssi_state ==
68                      BTC_RSSI_STATE_STAY_LOW)) {
69                         if (bt_rssi >= (rssi_thresh +
70                                         BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
71                                 bt_rssi_state = BTC_RSSI_STATE_HIGH;
72                         else
73                                 bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
74                 } else {
75                         if (bt_rssi < rssi_thresh)
76                                 bt_rssi_state = BTC_RSSI_STATE_LOW;
77                         else
78                                 bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
79                 }
80         } else if (level_num == 3) {
81                 if (rssi_thresh > rssi_thresh1) {
82                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
83                                     "[BTCoex], BT Rssi thresh error!!\n");
84                         BTC_TRACE(trace_buf);
85                         return coex_sta->pre_bt_rssi_state;
86                 }
87
88                 if ((coex_sta->pre_bt_rssi_state == BTC_RSSI_STATE_LOW) ||
89                     (coex_sta->pre_bt_rssi_state ==
90                      BTC_RSSI_STATE_STAY_LOW)) {
91                         if (bt_rssi >= (rssi_thresh +
92                                         BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
93                                 bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
94                         else
95                                 bt_rssi_state = BTC_RSSI_STATE_STAY_LOW;
96                 } else if ((coex_sta->pre_bt_rssi_state ==
97                             BTC_RSSI_STATE_MEDIUM) ||
98                            (coex_sta->pre_bt_rssi_state ==
99                             BTC_RSSI_STATE_STAY_MEDIUM)) {
100                         if (bt_rssi >= (rssi_thresh1 +
101                                         BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
102                                 bt_rssi_state = BTC_RSSI_STATE_HIGH;
103                         else if (bt_rssi < rssi_thresh)
104                                 bt_rssi_state = BTC_RSSI_STATE_LOW;
105                         else
106                                 bt_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
107                 } else {
108                         if (bt_rssi < rssi_thresh1)
109                                 bt_rssi_state = BTC_RSSI_STATE_MEDIUM;
110                         else
111                                 bt_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
112                 }
113         }
114
115         coex_sta->pre_bt_rssi_state = bt_rssi_state;
116
117         return bt_rssi_state;
118 }
119
120 u8 halbtc8703b1ant_wifi_rssi_state(IN struct btc_coexist *btcoexist,
121            IN u8 index, IN u8 level_num, IN u8 rssi_thresh, IN u8 rssi_thresh1)
122 {
123         s32                     wifi_rssi = 0;
124         u8                      wifi_rssi_state = coex_sta->pre_wifi_rssi_state[index];
125
126         btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
127
128         if (level_num == 2) {
129                 if ((coex_sta->pre_wifi_rssi_state[index] == BTC_RSSI_STATE_LOW)
130                     ||
131                     (coex_sta->pre_wifi_rssi_state[index] ==
132                      BTC_RSSI_STATE_STAY_LOW)) {
133                         if (wifi_rssi >= (rssi_thresh +
134                                           BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
135                                 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
136                         else
137                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
138                 } else {
139                         if (wifi_rssi < rssi_thresh)
140                                 wifi_rssi_state = BTC_RSSI_STATE_LOW;
141                         else
142                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
143                 }
144         } else if (level_num == 3) {
145                 if (rssi_thresh > rssi_thresh1) {
146                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
147                                     "[BTCoex], wifi RSSI thresh error!!\n");
148                         BTC_TRACE(trace_buf);
149                         return coex_sta->pre_wifi_rssi_state[index];
150                 }
151
152                 if ((coex_sta->pre_wifi_rssi_state[index] == BTC_RSSI_STATE_LOW)
153                     ||
154                     (coex_sta->pre_wifi_rssi_state[index] ==
155                      BTC_RSSI_STATE_STAY_LOW)) {
156                         if (wifi_rssi >= (rssi_thresh +
157                                           BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
158                                 wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
159                         else
160                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_LOW;
161                 } else if ((coex_sta->pre_wifi_rssi_state[index] ==
162                             BTC_RSSI_STATE_MEDIUM) ||
163                            (coex_sta->pre_wifi_rssi_state[index] ==
164                             BTC_RSSI_STATE_STAY_MEDIUM)) {
165                         if (wifi_rssi >= (rssi_thresh1 +
166                                           BTC_RSSI_COEX_THRESH_TOL_8703B_1ANT))
167                                 wifi_rssi_state = BTC_RSSI_STATE_HIGH;
168                         else if (wifi_rssi < rssi_thresh)
169                                 wifi_rssi_state = BTC_RSSI_STATE_LOW;
170                         else
171                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_MEDIUM;
172                 } else {
173                         if (wifi_rssi < rssi_thresh1)
174                                 wifi_rssi_state = BTC_RSSI_STATE_MEDIUM;
175                         else
176                                 wifi_rssi_state = BTC_RSSI_STATE_STAY_HIGH;
177                 }
178         }
179
180         coex_sta->pre_wifi_rssi_state[index] = wifi_rssi_state;
181
182         return wifi_rssi_state;
183 }
184
185 void halbtc8703b1ant_update_ra_mask(IN struct btc_coexist *btcoexist,
186                                     IN boolean force_exec, IN u32 dis_rate_mask)
187 {
188         coex_dm->cur_ra_mask = dis_rate_mask;
189
190         if (force_exec || (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask))
191                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_RAMASK,
192                                    &coex_dm->cur_ra_mask);
193         coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
194 }
195
196 void halbtc8703b1ant_auto_rate_fallback_retry(IN struct btc_coexist *btcoexist,
197                 IN boolean force_exec, IN u8 type)
198 {
199         boolean wifi_under_b_mode = FALSE;
200
201         coex_dm->cur_arfr_type = type;
202
203         if (force_exec || (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
204                 switch (coex_dm->cur_arfr_type) {
205                 case 0: /* normal mode */
206                         btcoexist->btc_write_4byte(btcoexist, 0x430,
207                                                    coex_dm->backup_arfr_cnt1);
208                         btcoexist->btc_write_4byte(btcoexist, 0x434,
209                                                    coex_dm->backup_arfr_cnt2);
210                         break;
211                 case 1:
212                         btcoexist->btc_get(btcoexist,
213                                            BTC_GET_BL_WIFI_UNDER_B_MODE,
214                                            &wifi_under_b_mode);
215                         if (wifi_under_b_mode) {
216                                 btcoexist->btc_write_4byte(btcoexist,
217                                                            0x430, 0x0);
218                                 btcoexist->btc_write_4byte(btcoexist,
219                                                            0x434, 0x01010101);
220                         } else {
221                                 btcoexist->btc_write_4byte(btcoexist,
222                                                            0x430, 0x0);
223                                 btcoexist->btc_write_4byte(btcoexist,
224                                                            0x434, 0x04030201);
225                         }
226                         break;
227                 default:
228                         break;
229                 }
230         }
231
232         coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
233 }
234
235 void halbtc8703b1ant_retry_limit(IN struct btc_coexist *btcoexist,
236                                  IN boolean force_exec, IN u8 type)
237 {
238         coex_dm->cur_retry_limit_type = type;
239
240         if (force_exec ||
241             (coex_dm->pre_retry_limit_type !=
242              coex_dm->cur_retry_limit_type)) {
243                 switch (coex_dm->cur_retry_limit_type) {
244                 case 0: /* normal mode */
245                         btcoexist->btc_write_2byte(btcoexist, 0x42a,
246                                                    coex_dm->backup_retry_limit);
247                         break;
248                 case 1: /* retry limit=8 */
249                         btcoexist->btc_write_2byte(btcoexist, 0x42a,
250                                                    0x0808);
251                         break;
252                 default:
253                         break;
254                 }
255         }
256
257         coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
258 }
259
260 void halbtc8703b1ant_ampdu_max_time(IN struct btc_coexist *btcoexist,
261                                     IN boolean force_exec, IN u8 type)
262 {
263         coex_dm->cur_ampdu_time_type = type;
264
265         if (force_exec ||
266             (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
267                 switch (coex_dm->cur_ampdu_time_type) {
268                 case 0: /* normal mode */
269                         btcoexist->btc_write_1byte(btcoexist, 0x456,
270                                            coex_dm->backup_ampdu_max_time);
271                         break;
272                 case 1: /* AMPDU timw = 0x38 * 32us */
273                         btcoexist->btc_write_1byte(btcoexist, 0x456,
274                                                    0x38);
275                         break;
276                 default:
277                         break;
278                 }
279         }
280
281         coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
282 }
283
284 void halbtc8703b1ant_limited_tx(IN struct btc_coexist *btcoexist,
285                 IN boolean force_exec, IN u8 ra_mask_type, IN u8 arfr_type,
286                                 IN u8 retry_limit_type, IN u8 ampdu_time_type)
287 {
288         switch (ra_mask_type) {
289         case 0: /* normal mode */
290                 halbtc8703b1ant_update_ra_mask(btcoexist, force_exec,
291                                                0x0);
292                 break;
293         case 1: /* disable cck 1/2 */
294                 halbtc8703b1ant_update_ra_mask(btcoexist, force_exec,
295                                                0x00000003);
296                 break;
297         case 2: /* disable cck 1/2/5.5, ofdm 6/9/12/18/24, mcs 0/1/2/3/4 */
298                 halbtc8703b1ant_update_ra_mask(btcoexist, force_exec,
299                                                0x0001f1f7);
300                 break;
301         default:
302                 break;
303         }
304
305         halbtc8703b1ant_auto_rate_fallback_retry(btcoexist, force_exec,
306                         arfr_type);
307         halbtc8703b1ant_retry_limit(btcoexist, force_exec, retry_limit_type);
308         halbtc8703b1ant_ampdu_max_time(btcoexist, force_exec, ampdu_time_type);
309 }
310
311 void halbtc8703b1ant_limited_rx(IN struct btc_coexist *btcoexist,
312                         IN boolean force_exec, IN boolean rej_ap_agg_pkt,
313                         IN boolean bt_ctrl_agg_buf_size, IN u8 agg_buf_size)
314 {
315         boolean reject_rx_agg = rej_ap_agg_pkt;
316         boolean bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
317         u8      rx_agg_size = agg_buf_size;
318
319         /* ============================================ */
320         /*      Rx Aggregation related setting */
321         /* ============================================ */
322         btcoexist->btc_set(btcoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT,
323                            &reject_rx_agg);
324         /* decide BT control aggregation buf size or not */
325         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
326                            &bt_ctrl_rx_agg_size);
327         /* aggregation buf size, only work when BT control Rx aggregation size. */
328         btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
329         /* real update aggregation setting */
330         btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
331
332
333 }
334
335 void halbtc8703b1ant_query_bt_info(IN struct btc_coexist *btcoexist)
336 {
337         u8                      h2c_parameter[1] = {0};
338
339
340         h2c_parameter[0] |= BIT(0);     /* trigger */
341
342         btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
343 }
344
345 void halbtc8703b1ant_monitor_bt_ctr(IN struct btc_coexist *btcoexist)
346 {
347         u32                     reg_hp_txrx, reg_lp_txrx, u32tmp;
348         u32                     reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
349         static u8               num_of_bt_counter_chk = 0, cnt_slave = 0, cnt_overhead = 0;
350         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
351
352         /* to avoid 0x76e[3] = 1 (WLAN_Act control by PTA) during IPS */
353         /* if (! (btcoexist->btc_read_1byte(btcoexist, 0x76e) & 0x8) ) */
354
355         reg_hp_txrx = 0x770;
356         reg_lp_txrx = 0x774;
357
358         u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
359         reg_hp_tx = u32tmp & MASKLWORD;
360         reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
361
362         u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
363         reg_lp_tx = u32tmp & MASKLWORD;
364         reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
365
366         coex_sta->high_priority_tx = reg_hp_tx;
367         coex_sta->high_priority_rx = reg_hp_rx;
368         coex_sta->low_priority_tx = reg_lp_tx;
369         coex_sta->low_priority_rx = reg_lp_rx;
370
371         if (BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
372                         coex_dm->bt_status) {
373
374                         if (coex_sta->high_priority_rx >= 15) {
375                                         if (cnt_overhead < 3)
376                                                 cnt_overhead++;
377
378                                         if (cnt_overhead == 3)
379                                                 coex_sta->is_hiPri_rx_overhead = TRUE;
380                         } else {
381                                         if (cnt_overhead > 0)
382                                                 cnt_overhead--;
383
384                                         if (cnt_overhead == 0)
385                                                 coex_sta->is_hiPri_rx_overhead = FALSE;
386                         }
387         } else
388                 coex_sta->is_hiPri_rx_overhead = FALSE;
389
390         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
391                     "[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
392                     reg_hp_rx, reg_hp_tx, reg_lp_rx, reg_lp_tx);
393
394         BTC_TRACE(trace_buf);
395
396         /* reset counter */
397         btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
398
399         if ((coex_sta->low_priority_tx > 1150)  &&
400             (!coex_sta->c2h_bt_inquiry_page))
401                 coex_sta->pop_event_cnt++;
402
403         if ((coex_sta->low_priority_rx >= 1150) &&
404             (coex_sta->low_priority_rx >= coex_sta->low_priority_tx)
405             && (!coex_sta->under_ips)  && (!coex_sta->c2h_bt_inquiry_page) &&
406             (coex_sta->bt_link_exist))  {
407                 if (cnt_slave >= 3) {
408                         bt_link_info->slave_role = TRUE;
409                         cnt_slave = 3;
410                 } else
411                         cnt_slave++;
412         } else {
413                 if (cnt_slave == 0)     {
414                         bt_link_info->slave_role = FALSE;
415                         cnt_slave = 0;
416                 } else
417                         cnt_slave--;
418
419         }
420
421         if (!coex_sta->bt_disabled) {
422                 if ((coex_sta->high_priority_tx == 0) &&
423                     (coex_sta->high_priority_rx == 0) &&
424                     (coex_sta->low_priority_tx == 0) &&
425                     (coex_sta->low_priority_rx == 0)) {
426                         num_of_bt_counter_chk++;
427                         if (num_of_bt_counter_chk >= 3) {
428                                 halbtc8703b1ant_query_bt_info(btcoexist);
429                                 num_of_bt_counter_chk = 0;
430                         }
431                 }
432         }
433
434 }
435
436
437 void halbtc8703b1ant_monitor_wifi_ctr(IN struct btc_coexist *btcoexist)
438 {
439 #if 1
440                 s32 wifi_rssi = 0;
441                 boolean wifi_busy = FALSE, wifi_under_b_mode = FALSE,
442                                 wifi_scan = FALSE;
443                 boolean bt_idle = FALSE, wl_idle = FALSE;
444                 static u8 cck_lock_counter = 0, wl_noisy_count0 = 0,
445                            wl_noisy_count1 = 3, wl_noisy_count2 = 0;
446                 u32 total_cnt, reg_val1, reg_val2, cck_cnt;
447
448                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
449                 btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
450                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
451                                    &wifi_under_b_mode);
452
453                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
454
455                 coex_sta->crc_ok_cck = btcoexist->btc_phydm_query_PHY_counter(
456                                                                         btcoexist, PHYDM_INFO_CRC32_OK_CCK);
457                 coex_sta->crc_ok_11g = btcoexist->btc_phydm_query_PHY_counter(
458                                                                         btcoexist, PHYDM_INFO_CRC32_OK_LEGACY);
459                 coex_sta->crc_ok_11n = btcoexist->btc_phydm_query_PHY_counter(
460                                                                         btcoexist, PHYDM_INFO_CRC32_OK_HT);
461                 coex_sta->crc_ok_11n_vht = btcoexist->btc_phydm_query_PHY_counter(
462                                                                         btcoexist, PHYDM_INFO_CRC32_OK_VHT);
463
464                 coex_sta->crc_err_cck = btcoexist->btc_phydm_query_PHY_counter(
465                                                                         btcoexist, PHYDM_INFO_CRC32_ERROR_CCK);
466                 coex_sta->crc_err_11g =  btcoexist->btc_phydm_query_PHY_counter(
467                                                                         btcoexist, PHYDM_INFO_CRC32_ERROR_LEGACY);
468                 coex_sta->crc_err_11n = btcoexist->btc_phydm_query_PHY_counter(
469                                                                         btcoexist, PHYDM_INFO_CRC32_ERROR_HT);
470                 coex_sta->crc_err_11n_vht = btcoexist->btc_phydm_query_PHY_counter(
471                                                                         btcoexist, PHYDM_INFO_CRC32_ERROR_VHT);
472
473                 cck_cnt = coex_sta->crc_ok_cck + coex_sta->crc_err_cck;
474
475                 if ((coex_dm->bt_status == BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE) ||
476                         (coex_dm->bt_status == BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE) ||
477                         (coex_sta->bt_disabled))
478                         bt_idle = TRUE;
479
480                 if ((!wifi_busy) && (!wifi_scan) && (!coex_sta->under_lps)
481                         && (!coex_sta->under_ips))
482                         wl_idle = TRUE;
483
484                 if ((wl_idle) && (bt_idle)) {
485                         if (cck_cnt > 250) {
486                                 if (wl_noisy_count2 < 3)
487                                         wl_noisy_count2++;
488
489                                 if (wl_noisy_count2 == 3) {
490                                         wl_noisy_count0 = 0;
491                                         wl_noisy_count1 = 0;
492                                 }
493                         } else if (cck_cnt < 50) {
494                                 if (wl_noisy_count0 < 3)
495                                         wl_noisy_count0++;
496
497                                 if (wl_noisy_count0 == 3) {
498                                         wl_noisy_count1 = 0;
499                                         wl_noisy_count2 = 0;
500                                 }
501                         } else {
502                                 if (wl_noisy_count1 < 3)
503                                         wl_noisy_count1++;
504
505                                 if (wl_noisy_count1 == 3) {
506                                         wl_noisy_count0 = 0;
507                                         wl_noisy_count2 = 0;
508                                 }
509                         }
510
511                         if (wl_noisy_count2 == 3)
512                                 coex_sta->wl_noisy_level = 2;
513                         else if (wl_noisy_count1 == 3)
514                                 coex_sta->wl_noisy_level = 1;
515                         else
516                                 coex_sta->wl_noisy_level = 0;
517                 }
518
519                 if ((wifi_busy) && (wifi_rssi >= 30) && (!wifi_under_b_mode)) {
520                         total_cnt = coex_sta->crc_ok_cck + coex_sta->crc_ok_11g +
521                                         coex_sta->crc_ok_11n + coex_sta->crc_ok_11n_vht;
522
523                         if ((coex_dm->bt_status == BT_8703B_1ANT_BT_STATUS_ACL_BUSY) ||
524                                 (coex_dm->bt_status == BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY) ||
525                                 (coex_dm->bt_status == BT_8703B_1ANT_BT_STATUS_SCO_BUSY)) {
526                                 if (coex_sta->crc_ok_cck > (total_cnt -
527                                                                 coex_sta->crc_ok_cck)) {
528                                         if (cck_lock_counter < 3)
529                                                 cck_lock_counter++;
530                                 } else {
531                                         if (cck_lock_counter > 0)
532                                                 cck_lock_counter--;
533                                 }
534
535                         } else {
536                                 if (cck_lock_counter > 0)
537                                         cck_lock_counter--;
538                         }
539                 } else {
540                         if (cck_lock_counter > 0)
541                                 cck_lock_counter--;
542                 }
543
544                 if (!coex_sta->pre_ccklock) {
545
546                         if (cck_lock_counter >= 3)
547                                 coex_sta->cck_lock = TRUE;
548                         else
549                                 coex_sta->cck_lock = FALSE;
550                 } else {
551                         if (cck_lock_counter == 0)
552                                 coex_sta->cck_lock = FALSE;
553                         else
554                                 coex_sta->cck_lock = TRUE;
555                 }
556
557                 if (coex_sta->cck_lock)
558                         coex_sta->cck_ever_lock = TRUE;
559
560                 coex_sta->pre_ccklock =  coex_sta->cck_lock;
561
562 #endif
563 }
564
565 void halbtc8703b1ant_update_bt_link_info(IN struct btc_coexist *btcoexist)
566 {
567         struct  btc_bt_link_info        *bt_link_info = &btcoexist->bt_link_info;
568         boolean                         bt_hs_on = FALSE;
569         boolean         bt_busy = FALSE;
570
571
572         coex_sta->num_of_profile = 0;
573
574         /* set link exist status */
575         if (!(coex_sta->bt_info & BT_INFO_8703B_1ANT_B_CONNECTION)) {
576                 coex_sta->bt_link_exist = FALSE;
577                 coex_sta->pan_exist = FALSE;
578                 coex_sta->a2dp_exist = FALSE;
579                 coex_sta->hid_exist = FALSE;
580                 coex_sta->sco_exist = FALSE;
581         } else {        /* connection exists */
582                 coex_sta->bt_link_exist = TRUE;
583                 if (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_FTP) {
584                         coex_sta->pan_exist = TRUE;
585                         coex_sta->num_of_profile++;
586                 } else
587                         coex_sta->pan_exist = FALSE;
588
589                 if (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_A2DP) {
590                         coex_sta->a2dp_exist = TRUE;
591                         coex_sta->num_of_profile++;
592                 } else
593                         coex_sta->a2dp_exist = FALSE;
594
595                 if (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_HID) {
596                         coex_sta->hid_exist = TRUE;
597                         coex_sta->num_of_profile++;
598                 } else
599                         coex_sta->hid_exist = FALSE;
600
601                 if (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_SCO_ESCO) {
602                         coex_sta->sco_exist = TRUE;
603                         coex_sta->num_of_profile++;
604                 } else
605                         coex_sta->sco_exist = FALSE;
606
607         }
608
609         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
610
611         bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
612         bt_link_info->sco_exist = coex_sta->sco_exist;
613         bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
614         bt_link_info->pan_exist = coex_sta->pan_exist;
615         bt_link_info->hid_exist = coex_sta->hid_exist;
616         bt_link_info->acl_busy = coex_sta->acl_busy;
617
618         /* work around for HS mode. */
619         if (bt_hs_on) {
620                 bt_link_info->pan_exist = TRUE;
621                 bt_link_info->bt_link_exist = TRUE;
622         }
623
624         /* check if Sco only */
625         if (bt_link_info->sco_exist &&
626             !bt_link_info->a2dp_exist &&
627             !bt_link_info->pan_exist &&
628             !bt_link_info->hid_exist)
629                 bt_link_info->sco_only = TRUE;
630         else
631                 bt_link_info->sco_only = FALSE;
632
633         /* check if A2dp only */
634         if (!bt_link_info->sco_exist &&
635             bt_link_info->a2dp_exist &&
636             !bt_link_info->pan_exist &&
637             !bt_link_info->hid_exist)
638                 bt_link_info->a2dp_only = TRUE;
639         else
640                 bt_link_info->a2dp_only = FALSE;
641
642         /* check if Pan only */
643         if (!bt_link_info->sco_exist &&
644             !bt_link_info->a2dp_exist &&
645             bt_link_info->pan_exist &&
646             !bt_link_info->hid_exist)
647                 bt_link_info->pan_only = TRUE;
648         else
649                 bt_link_info->pan_only = FALSE;
650
651         /* check if Hid only */
652         if (!bt_link_info->sco_exist &&
653             !bt_link_info->a2dp_exist &&
654             !bt_link_info->pan_exist &&
655             bt_link_info->hid_exist)
656                 bt_link_info->hid_only = TRUE;
657         else
658                 bt_link_info->hid_only = FALSE;
659
660         if (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_INQ_PAGE) {
661                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_INQ_PAGE;
662                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
663                         "[BTCoex], BtInfoNotify(), BT Inq/page!!!\n");
664         } else if (!(coex_sta->bt_info & BT_INFO_8703B_1ANT_B_CONNECTION)) {
665                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
666                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
667                         "[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
668         } else if (coex_sta->bt_info == BT_INFO_8703B_1ANT_B_CONNECTION) {
669                 /* connection exists but no busy */
670                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE;
671                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
672                             "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
673         } else if (((coex_sta->bt_info & BT_INFO_8703B_1ANT_B_SCO_ESCO) ||
674                     (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_SCO_BUSY)) &&
675                    (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_ACL_BUSY)) {
676                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY;
677                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
678                             "[BTCoex], BtInfoNotify(), BT ACL SCO busy!!!\n");
679         } else if ((coex_sta->bt_info & BT_INFO_8703B_1ANT_B_SCO_ESCO) ||
680                    (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_SCO_BUSY)) {
681                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_SCO_BUSY;
682                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
683                             "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
684         } else if (coex_sta->bt_info & BT_INFO_8703B_1ANT_B_ACL_BUSY) {
685                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_ACL_BUSY;
686                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
687                             "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
688         } else {
689                 coex_dm->bt_status = BT_8703B_1ANT_BT_STATUS_MAX;
690                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
691                         "[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
692         }
693
694         BTC_TRACE(trace_buf);
695
696         if ((BT_8703B_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
697             (BT_8703B_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
698             (BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
699                 bt_busy = TRUE;
700         else
701                 bt_busy = FALSE;
702
703         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
704 }
705
706
707 void halbtc8703b1ant_update_wifi_channel_info(IN struct btc_coexist *btcoexist,
708                 IN u8 type)
709 {
710         u8                      h2c_parameter[3] = {0};
711         u32                     wifi_bw;
712         u8                      wifi_central_chnl;
713
714         /* only 2.4G we need to inform bt the chnl mask */
715         btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL,
716                            &wifi_central_chnl);
717         if ((BTC_MEDIA_CONNECT == type) &&
718             (wifi_central_chnl <= 14)) {
719                 h2c_parameter[0] =
720                         0x1;  /* enable BT AFH skip WL channel for 8703b because BT Rx LO interference */
721                 /* h2c_parameter[0] = 0x0; */
722                 h2c_parameter[1] = wifi_central_chnl;
723                 btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
724                 if (BTC_WIFI_BW_HT40 == wifi_bw)
725                         h2c_parameter[2] = 0x30;
726                 else
727                         h2c_parameter[2] = 0x20;
728         }
729
730         coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
731         coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
732         coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
733
734         btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
735
736 }
737
738 void halbtc8703b1ant_set_bt_auto_report(IN struct btc_coexist *btcoexist,
739                                         IN boolean enable_auto_report)
740 {
741         u8                      h2c_parameter[1] = {0};
742
743         h2c_parameter[0] = 0;
744
745         if (enable_auto_report)
746                 h2c_parameter[0] |= BIT(0);
747
748         btcoexist->btc_fill_h2c(btcoexist, 0x68, 1, h2c_parameter);
749 }
750
751 void halbtc8703b1ant_bt_auto_report(IN struct btc_coexist *btcoexist,
752                     IN boolean force_exec, IN boolean enable_auto_report)
753 {
754         coex_dm->cur_bt_auto_report = enable_auto_report;
755
756         if (!force_exec) {
757                 if (coex_dm->pre_bt_auto_report == coex_dm->cur_bt_auto_report)
758                         return;
759         }
760         halbtc8703b1ant_set_bt_auto_report(btcoexist,
761                                            coex_dm->cur_bt_auto_report);
762
763         coex_dm->pre_bt_auto_report = coex_dm->cur_bt_auto_report;
764 }
765
766 void halbtc8703b1ant_set_fw_low_penalty_ra(IN struct btc_coexist
767                 *btcoexist, IN boolean low_penalty_ra)
768 {
769         u8                      h2c_parameter[6] = {0};
770
771         h2c_parameter[0] = 0x6; /* op_code, 0x6= Retry_Penalty */
772
773         if (low_penalty_ra) {
774                 h2c_parameter[1] |= BIT(0);
775                 h2c_parameter[2] =
776                         0x00;  /* normal rate except MCS7/6/5, OFDM54/48/36 */
777                 h2c_parameter[3] = 0xf7;  /* MCS7 or OFDM54 */
778                 h2c_parameter[4] = 0xf8;  /* MCS6 or OFDM48 */
779                 h2c_parameter[5] = 0xf9;        /* MCS5 or OFDM36        */
780         }
781
782         btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
783 }
784
785 void halbtc8703b1ant_low_penalty_ra(IN struct btc_coexist *btcoexist,
786                             IN boolean force_exec, IN boolean low_penalty_ra)
787 {
788         coex_dm->cur_low_penalty_ra = low_penalty_ra;
789
790         if (!force_exec) {
791                 if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
792                         return;
793         }
794
795         halbtc8703b1ant_set_fw_low_penalty_ra(btcoexist,
796                                               coex_dm->cur_low_penalty_ra);
797
798         coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
799 }
800
801 void halbtc8703b1ant_write_score_board(
802         IN      struct  btc_coexist             *btcoexist,
803         IN      u16                             bitpos,
804         IN      boolean         state
805 )
806 {
807
808         static u16 originalval = 0x8002;
809
810         if (state)
811                 originalval = originalval | bitpos;
812         else
813                 originalval = originalval & (~bitpos);
814
815
816         btcoexist->btc_write_2byte(btcoexist, 0xaa, originalval);
817 #if 0
818         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
819                     "\n [BTCoex], ********** Write Scoreboard = %x**********\n",
820                     originalval);
821         BTC_TRACE(trace_buf);
822 #endif
823
824 }
825
826 void halbtc8703b1ant_read_score_board(
827         IN      struct  btc_coexist             *btcoexist,
828         IN   u16                                *score_board_val
829 )
830 {
831
832         *score_board_val = (btcoexist->btc_read_2byte(btcoexist,
833                             0xaa)) & 0x7fff;
834 }
835
836 void halbtc8703b1ant_post_state_to_bt(
837         IN      struct  btc_coexist             *btcoexist,
838         IN      u16                                             type,
839         IN  boolean                 state
840 )
841 {
842         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
843                 "[BTCoex], halbtc8703b1ant_post_state_to_bt: type = %d, state =%d\n",
844                 type, state);
845         BTC_TRACE(trace_buf);
846
847         halbtc8703b1ant_write_score_board(btcoexist, (u16) type, state);
848 }
849
850 boolean halbtc8703b1ant_is_wifibt_status_changed(IN struct btc_coexist
851                 *btcoexist)
852 {
853         static boolean  pre_wifi_busy = FALSE, pre_under_4way = FALSE,
854                         pre_bt_hs_on = FALSE, pre_bt_off = FALSE, pre_bt_slave = FALSE;
855         static u8 pre_hid_busy_num = 0, pre_wl_noisy_level = 0;
856         boolean wifi_busy = FALSE, under_4way = FALSE, bt_hs_on = FALSE;
857         boolean wifi_connected = FALSE;
858         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
859
860         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
861                            &wifi_connected);
862         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
863         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
864         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
865                            &under_4way);
866
867         if (coex_sta->bt_disabled != pre_bt_off) {
868                 pre_bt_off = coex_sta->bt_disabled;
869
870                 if (coex_sta->bt_disabled)
871                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
872                                         "[BTCoex], BT is disabled !!\n");
873                 else
874                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
875                                         "[BTCoex], BT is enabled !!\n");
876
877                 BTC_TRACE(trace_buf);
878
879                 coex_sta->bt_coex_supported_feature = 0;
880                 coex_sta->bt_coex_supported_version = 0;
881                 coex_sta->bt_ble_scan_type = 0;
882                 coex_sta->bt_ble_scan_para[0] = 0;
883                 coex_sta->bt_ble_scan_para[1] = 0;
884                 coex_sta->bt_ble_scan_para[2] = 0;
885
886                 return TRUE;
887         }
888
889         if (wifi_connected) {
890                 if (wifi_busy != pre_wifi_busy) {
891                         pre_wifi_busy = wifi_busy;
892
893                         if (wifi_busy)
894                                 halbtc8703b1ant_post_state_to_bt(btcoexist,
895                                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST, TRUE);
896                         else
897                                 halbtc8703b1ant_post_state_to_bt(btcoexist,
898                                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST, FALSE);
899                         return TRUE;
900                 }
901                 if (under_4way != pre_under_4way) {
902                         pre_under_4way = under_4way;
903                         return TRUE;
904                 }
905                 if (bt_hs_on != pre_bt_hs_on) {
906                         pre_bt_hs_on = bt_hs_on;
907                         return TRUE;
908                 }
909                 if (coex_sta->wl_noisy_level != pre_wl_noisy_level) {
910                         pre_wl_noisy_level = coex_sta->wl_noisy_level;
911                         return TRUE;
912                 }
913         }
914
915         if (!coex_sta->bt_disabled) {
916                 if (coex_sta->hid_busy_num != pre_hid_busy_num) {
917                         pre_hid_busy_num = coex_sta->hid_busy_num;
918                         return TRUE;
919                 }
920         }
921
922         if (bt_link_info->slave_role != pre_bt_slave) {
923                 pre_bt_slave = bt_link_info->slave_role;
924                 return TRUE;
925         }
926
927         return FALSE;
928 }
929
930
931 void halbtc8703b1ant_monitor_bt_enable_disable(IN struct btc_coexist *btcoexist)
932 {
933         static u32              bt_disable_cnt = 0;
934         boolean                 bt_active = TRUE, bt_disabled = FALSE;
935         u16                     u16tmp;
936
937         /* This function check if bt is disabled */
938 #if 1
939         if (coex_sta->high_priority_tx == 0 &&
940             coex_sta->high_priority_rx == 0 &&
941             coex_sta->low_priority_tx == 0 &&
942             coex_sta->low_priority_rx == 0)
943                 bt_active = FALSE;
944         if (coex_sta->high_priority_tx == 0xffff &&
945             coex_sta->high_priority_rx == 0xffff &&
946             coex_sta->low_priority_tx == 0xffff &&
947             coex_sta->low_priority_rx == 0xffff)
948                 bt_active = FALSE;
949
950
951 #else   /*  8703b BT can't show correct on/off status in scoreboard[1] 2015/11/26 */
952
953         halbtc8703b1ant_read_score_board(btcoexist,     &u16tmp);
954
955         bt_active = u16tmp & BIT(1);
956
957
958 #endif
959
960         if (bt_active) {
961                 bt_disable_cnt = 0;
962                 bt_disabled = FALSE;
963                 btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
964                                    &bt_disabled);
965         } else {
966
967                 bt_disable_cnt++;
968                 if (bt_disable_cnt >= 10) {
969                         bt_disabled = TRUE;
970                         bt_disable_cnt = 10;
971                 }
972
973                 btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
974                                    &bt_disabled);
975         }
976
977         if (bt_disabled)
978                 halbtc8703b1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, FALSE);
979         else
980                 halbtc8703b1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, TRUE);
981
982         if (coex_sta->bt_disabled != bt_disabled) {
983                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
984                             "[BTCoex], BT is from %s to %s!!\n",
985                             (coex_sta->bt_disabled ? "disabled" : "enabled"),
986                             (bt_disabled ? "disabled" : "enabled"));
987                 BTC_TRACE(trace_buf);
988                 coex_sta->bt_disabled = bt_disabled;
989
990         }
991 }
992
993
994
995 void halbtc8703b1ant_enable_gnt_to_gpio(IN struct btc_coexist *btcoexist,
996                                         IN boolean isenable)
997 {
998
999 #if (BT_8703B_1ANT_ENABLE_GNTBT_TO_GPIO14 == 1)
1000         if (isenable) {
1001                 /* enable GNT_WL/GNT_BT debug signal to GPIO14/15 */
1002                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x73, 0x8, 0x1);
1003
1004                 /* enable GNT_BT debug to GPIO */
1005                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4e, 0x40, 0x0);
1006                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x1, 0x0);
1007         } else {
1008                 /* enable GNT_WL/GNT_BT debug signal to GPIO14/15 */
1009                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x73, 0x8, 0x0);
1010
1011                 /* Disable GNT_BT debug to GPIO, and enable chip_wakeup_host */
1012                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4e, 0x40, 0x1);
1013                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x1, 0x1);
1014         }
1015 #endif
1016 }
1017
1018 u32 halbtc8703b1ant_ltecoex_indirect_read_reg(IN struct btc_coexist *btcoexist,
1019                 IN u16 reg_addr)
1020 {
1021         u32 j = 0, delay_count = 0;
1022
1023         /* wait for ready bit before access 0x7c0/0x7c4 */
1024         while (1) {
1025                 if ((btcoexist->btc_read_1byte(btcoexist, 0x7c3)&BIT(5)) == 0) {
1026                         delay_ms(50);
1027                         delay_count++;
1028                         if (delay_count >= 10) {
1029                                 delay_count = 0;
1030                                 break;
1031                         }
1032                 } else
1033                         break;
1034         }
1035
1036         btcoexist->btc_write_4byte(btcoexist, 0x7c0, 0x800F0000 | reg_addr);
1037
1038         return btcoexist->btc_read_4byte(btcoexist,
1039                                          0x7c8);  /* get read data */
1040
1041 }
1042
1043 void halbtc8703b1ant_ltecoex_indirect_write_reg(IN struct btc_coexist
1044                 *btcoexist,
1045                 IN u16 reg_addr, IN u32 bit_mask, IN u32 reg_value)
1046 {
1047         u32 val, i = 0, j = 0, bitpos = 0, delay_count = 0;
1048
1049         if (bit_mask == 0x0)
1050                 return;
1051         if (bit_mask == 0xffffffff) {
1052                 /* wait for ready bit before access 0x7c0/0x7c4 */
1053                 while (1) {
1054                         if ((btcoexist->btc_read_1byte(btcoexist, 0x7c3)&BIT(5)) == 0) {
1055                                 delay_ms(50);
1056                                 delay_count++;
1057                                 if (delay_count >= 10) {
1058                                         delay_count = 0;
1059                                         break;
1060                                 }
1061                         } else
1062                                 break;
1063                 }
1064
1065                 btcoexist->btc_write_4byte(btcoexist, 0x7c4,
1066                                            reg_value); /* put write data */
1067
1068                 btcoexist->btc_write_4byte(btcoexist, 0x7c0,
1069                                            0xc00F0000 | reg_addr);
1070         } else {
1071                 for (i = 0; i <= 31; i++) {
1072                         if (((bit_mask >> i) & 0x1) == 0x1) {
1073                                 bitpos = i;
1074                                 break;
1075                         }
1076                 }
1077
1078                 /* read back register value before write */
1079                 val = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
1080                                 reg_addr);
1081                 val = (val & (~bit_mask)) | (reg_value << bitpos);
1082
1083                 /* wait for ready bit before access 0x7c0/0x7c4 */
1084                 while (1) {
1085                         if ((btcoexist->btc_read_1byte(btcoexist, 0x7c3)&BIT(5)) == 0) {
1086                                 delay_ms(50);
1087                                 delay_count++;
1088                                 if (delay_count >= 10) {
1089                                         delay_count = 0;
1090                                         break;
1091                                 }
1092                         } else
1093                                 break;
1094                 }
1095
1096                 btcoexist->btc_write_4byte(btcoexist, 0x7c4,
1097                                            val); /* put write data */
1098
1099                 btcoexist->btc_write_4byte(btcoexist, 0x7c0,
1100                                            0xc00F0000 | reg_addr);
1101
1102         }
1103
1104 }
1105
1106 void halbtc8703b1ant_ltecoex_enable(IN struct btc_coexist *btcoexist,
1107                                     IN boolean enable)
1108 {
1109         u8 val;
1110
1111         val = (enable) ? 1 : 0;
1112         halbtc8703b1ant_ltecoex_indirect_write_reg(btcoexist, 0x38, 0x80,
1113                         val);  /* 0x38[7] */
1114
1115 }
1116
1117 void halbtc8703b1ant_ltecoex_pathcontrol_owner(IN struct btc_coexist *btcoexist,
1118                 IN boolean wifi_control)
1119 {
1120         u8 val;
1121
1122         val = (wifi_control) ? 1 : 0;
1123         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x73, 0x4,
1124                                            val); /* 0x70[26] */
1125
1126 }
1127
1128 void halbtc8703b1ant_ltecoex_set_gnt_bt(IN struct btc_coexist *btcoexist,
1129                         IN u8 control_block, IN boolean sw_control, IN u8 state)
1130 {
1131         u32 val = 0, val_orig = 0;
1132
1133         if (!sw_control)
1134                 val = 0x0;
1135         else if (state & 0x1)
1136                 val = 0x3;
1137         else
1138                 val = 0x1;
1139
1140         val_orig = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
1141                                 0x38);
1142
1143         switch (control_block) {
1144         case BT_8703B_1ANT_GNT_BLOCK_RFC_BB:
1145         default:
1146                 val = ((val << 14) | (val << 10)) | (val_orig & 0xffff33ff);
1147                 break;
1148         case BT_8703B_1ANT_GNT_BLOCK_RFC:
1149                 val = (val << 14) | (val_orig & 0xffff3fff);
1150                 break;
1151         case BT_8703B_1ANT_GNT_BLOCK_BB:
1152                 val = (val << 10) | (val_orig & 0xfffff3ff);
1153                 break;
1154         }
1155
1156         halbtc8703b1ant_ltecoex_indirect_write_reg(btcoexist,
1157                         0x38, 0xffffffff, val);
1158 }
1159
1160
1161 void halbtc8703b1ant_ltecoex_set_gnt_wl(IN struct btc_coexist *btcoexist,
1162                         IN u8 control_block, IN boolean sw_control, IN u8 state)
1163 {
1164         u32 val = 0, val_orig = 0;
1165
1166         if (!sw_control)
1167                 val = 0x0;
1168         else if (state & 0x1)
1169                 val = 0x3;
1170         else
1171                 val = 0x1;
1172
1173         val_orig = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
1174                                 0x38);
1175
1176         switch (control_block) {
1177         case BT_8703B_1ANT_GNT_BLOCK_RFC_BB:
1178         default:
1179                 val = ((val << 12) | (val << 8)) | (val_orig & 0xffffccff);
1180                 break;
1181         case BT_8703B_1ANT_GNT_BLOCK_RFC:
1182                 val = (val << 12) | (val_orig & 0xffffcfff);
1183                 break;
1184         case BT_8703B_1ANT_GNT_BLOCK_BB:
1185                 val = (val << 8) | (val_orig & 0xfffffcff);
1186                 break;
1187         }
1188
1189         halbtc8703b1ant_ltecoex_indirect_write_reg(btcoexist,
1190                         0x38, 0xffffffff, val);
1191 }
1192
1193
1194 void halbtc8703b1ant_ltecoex_set_coex_table(IN struct btc_coexist *btcoexist,
1195                 IN u8 table_type, IN u16 table_content)
1196 {
1197         u16 reg_addr = 0x0000;
1198
1199         switch (table_type) {
1200         case BT_8703B_1ANT_CTT_WL_VS_LTE:
1201                 reg_addr = 0xa0;
1202                 break;
1203         case BT_8703B_1ANT_CTT_BT_VS_LTE:
1204                 reg_addr = 0xa4;
1205                 break;
1206         }
1207
1208         if (reg_addr != 0x0000)
1209                 halbtc8703b1ant_ltecoex_indirect_write_reg(btcoexist, reg_addr,
1210                         0xffff, table_content); /* 0xa0[15:0] or 0xa4[15:0] */
1211
1212
1213 }
1214
1215
1216 void halbtc8703b1ant_ltecoex_set_break_table(IN struct btc_coexist *btcoexist,
1217                 IN u8 table_type, IN u8 table_content)
1218 {
1219         u16 reg_addr = 0x0000;
1220
1221         switch (table_type) {
1222         case BT_8703B_1ANT_LBTT_WL_BREAK_LTE:
1223                 reg_addr = 0xa8;
1224                 break;
1225         case BT_8703B_1ANT_LBTT_BT_BREAK_LTE:
1226                 reg_addr = 0xac;
1227                 break;
1228         case BT_8703B_1ANT_LBTT_LTE_BREAK_WL:
1229                 reg_addr = 0xb0;
1230                 break;
1231         case BT_8703B_1ANT_LBTT_LTE_BREAK_BT:
1232                 reg_addr = 0xb4;
1233                 break;
1234         }
1235
1236         if (reg_addr != 0x0000)
1237                 halbtc8703b1ant_ltecoex_indirect_write_reg(btcoexist, reg_addr,
1238                         0xff, table_content); /* 0xa8[15:0] or 0xb4[15:0] */
1239
1240
1241 }
1242
1243 void halbtc8703b1ant_set_wltoggle_coex_table(IN struct btc_coexist *btcoexist,
1244                 IN boolean force_exec,  IN u8 interval,
1245                 IN u8 val0x6c4_b0, IN u8 val0x6c4_b1, IN u8 val0x6c4_b2,
1246                 IN u8 val0x6c4_b3)
1247 {
1248         static u8 pre_h2c_parameter[6] = {0};
1249         u8      cur_h2c_parameter[6] = {0};
1250         u8 i, match_cnt = 0;
1251
1252         cur_h2c_parameter[0] = 0x7;     /* op_code, 0x7= wlan toggle slot*/
1253
1254         cur_h2c_parameter[1] = interval;
1255         cur_h2c_parameter[2] = val0x6c4_b0;
1256         cur_h2c_parameter[3] = val0x6c4_b1;
1257         cur_h2c_parameter[4] = val0x6c4_b2;
1258         cur_h2c_parameter[5] = val0x6c4_b3;
1259
1260         if (!force_exec) {
1261                 for (i = 1; i <= 5; i++) {
1262                         if (cur_h2c_parameter[i] != pre_h2c_parameter[i])
1263                                 break;
1264
1265                         match_cnt++;
1266                 }
1267
1268                 if (match_cnt == 5)
1269                         return;
1270         }
1271
1272         for (i = 1; i <= 5; i++)
1273                 pre_h2c_parameter[i] = cur_h2c_parameter[i];
1274
1275         btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, cur_h2c_parameter);
1276 }
1277
1278
1279 void halbtc8703b1ant_set_coex_table(IN struct btc_coexist *btcoexist,
1280             IN u32 val0x6c0, IN u32 val0x6c4, IN u32 val0x6c8, IN u8 val0x6cc)
1281 {
1282         btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
1283
1284         btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
1285
1286         btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
1287
1288         btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
1289 }
1290
1291 void halbtc8703b1ant_coex_table(IN struct btc_coexist *btcoexist,
1292                         IN boolean force_exec, IN u32 val0x6c0, IN u32 val0x6c4,
1293                                 IN u32 val0x6c8, IN u8 val0x6cc)
1294 {
1295         coex_dm->cur_val0x6c0 = val0x6c0;
1296         coex_dm->cur_val0x6c4 = val0x6c4;
1297         coex_dm->cur_val0x6c8 = val0x6c8;
1298         coex_dm->cur_val0x6cc = val0x6cc;
1299
1300         if (!force_exec) {
1301                 if ((coex_dm->pre_val0x6c0 == coex_dm->cur_val0x6c0) &&
1302                     (coex_dm->pre_val0x6c4 == coex_dm->cur_val0x6c4) &&
1303                     (coex_dm->pre_val0x6c8 == coex_dm->cur_val0x6c8) &&
1304                     (coex_dm->pre_val0x6cc == coex_dm->cur_val0x6cc))
1305                         return;
1306         }
1307         halbtc8703b1ant_set_coex_table(btcoexist, val0x6c0, val0x6c4, val0x6c8,
1308                                        val0x6cc);
1309
1310         coex_dm->pre_val0x6c0 = coex_dm->cur_val0x6c0;
1311         coex_dm->pre_val0x6c4 = coex_dm->cur_val0x6c4;
1312         coex_dm->pre_val0x6c8 = coex_dm->cur_val0x6c8;
1313         coex_dm->pre_val0x6cc = coex_dm->cur_val0x6cc;
1314 }
1315
1316 void halbtc8703b1ant_coex_table_with_type(IN struct btc_coexist *btcoexist,
1317                 IN boolean force_exec, IN u8 type)
1318 {
1319         u32     break_table;
1320         u8      select_table;
1321
1322         coex_sta->coex_table_type = type;
1323
1324         if (coex_sta->concurrent_rx_mode_on == TRUE) {
1325                 break_table = 0xf0ffffff;  /* set WL hi-pri can break BT */
1326                 select_table =
1327                         0xb;            /* set Tx response = Hi-Pri (ex: Transmitting ACK,BA,CTS) */
1328         } else {
1329                 break_table = 0xffffff;
1330                 select_table = 0x3;
1331         }
1332
1333                 switch (type) {
1334                 case 0:
1335                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1336                                                    0x55555555, 0x55555555, break_table,
1337                                                    select_table);
1338                         break;
1339                 case 1:
1340                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1341                                                    0x55555555, 0xaa5a5a5a, break_table,
1342                                                    select_table);
1343                         break;
1344                 case 2:
1345                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1346                                                    0xaa5a5a5a, 0xaa5a5a5a, break_table,
1347                                                    select_table);
1348                         break;
1349                 case 3:
1350                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1351                                                    0x55555555, 0x5a5a5a5a, break_table,
1352                                                    select_table);
1353                         break;
1354                 case 4:
1355                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1356                                                    0xa5555555, 0xaa5a5a5a, break_table,
1357                                                    select_table);
1358                         break;
1359                 case 5:
1360                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1361                                                    0x5a5a5a5a, 0x5a5a5a5a, break_table,
1362                                                    select_table);
1363                         break;
1364                 case 6:
1365                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1366                                                    0x55555555, 0x5a5a5a5a, break_table,
1367                                                    select_table);
1368                         break;
1369                 case 7:
1370                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1371                                                    0xaa555555, 0xaa555555, break_table,
1372                                                    select_table);
1373                         break;
1374                 case 8:
1375                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1376                                                    0xa5555555, 0xaaaaaaaa, break_table,
1377                                                    select_table);
1378                         break;
1379                 case 9:
1380                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1381                                                    0x5a5a5a5a, 0xaaaa5aaa, break_table,
1382                                                    select_table);
1383                         break;
1384                 case 10:
1385                         halbtc8703b1ant_coex_table(btcoexist, force_exec,
1386                                                    0xaaaaaaaa, 0xaaaaaaaa, break_table,
1387                                                    select_table);
1388                         break;
1389                 default:
1390                         break;
1391                 }
1392 }
1393
1394 void halbtc8703b1ant_set_fw_ignore_wlan_act(IN struct btc_coexist *btcoexist,
1395                 IN boolean enable)
1396 {
1397         u8                      h2c_parameter[1] = {0};
1398
1399         if (enable)
1400                 h2c_parameter[0] |= BIT(0);/* function enable */
1401
1402         btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
1403 }
1404
1405 void halbtc8703b1ant_ignore_wlan_act(IN struct btc_coexist *btcoexist,
1406                                      IN boolean force_exec, IN boolean enable)
1407 {
1408         coex_dm->cur_ignore_wlan_act = enable;
1409
1410         if (!force_exec) {
1411                 if (coex_dm->pre_ignore_wlan_act ==
1412                     coex_dm->cur_ignore_wlan_act)
1413                         return;
1414         }
1415         halbtc8703b1ant_set_fw_ignore_wlan_act(btcoexist, enable);
1416
1417         coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
1418 }
1419
1420 void halbtc8703b1ant_set_lps_rpwm(IN struct btc_coexist *btcoexist,
1421                                   IN u8 lps_val, IN u8 rpwm_val)
1422 {
1423         u8      lps = lps_val;
1424         u8      rpwm = rpwm_val;
1425
1426         btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
1427         btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
1428 }
1429
1430 void halbtc8703b1ant_lps_rpwm(IN struct btc_coexist *btcoexist,
1431                       IN boolean force_exec, IN u8 lps_val, IN u8 rpwm_val)
1432 {
1433         coex_dm->cur_lps = lps_val;
1434         coex_dm->cur_rpwm = rpwm_val;
1435
1436         if (!force_exec) {
1437                 if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
1438                     (coex_dm->pre_rpwm == coex_dm->cur_rpwm))
1439                         return;
1440         }
1441         halbtc8703b1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
1442
1443         coex_dm->pre_lps = coex_dm->cur_lps;
1444         coex_dm->pre_rpwm = coex_dm->cur_rpwm;
1445 }
1446
1447 void halbtc8703b1ant_ps_tdma_check_for_power_save_state(
1448         IN struct btc_coexist *btcoexist, IN boolean new_ps_state)
1449 {
1450         u8      lps_mode = 0x0;
1451         u8      h2c_parameter[5] = {0x8, 0, 0, 0, 0};
1452
1453         btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
1454
1455         if (lps_mode) { /* already under LPS state */
1456                 if (new_ps_state) {
1457                         /* keep state under LPS, do nothing. */
1458                 } else {
1459                         /* will leave LPS state, turn off psTdma first */
1460                         /*halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE,
1461                                                 8); */
1462                         btcoexist->btc_fill_h2c(btcoexist, 0x60, 5,
1463                                                 h2c_parameter);
1464                 }
1465         } else {                                        /* NO PS state */
1466                 if (new_ps_state) {
1467                         /* will enter LPS state, turn off psTdma first */
1468                         /*halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE,
1469                                                 8);*/
1470                         btcoexist->btc_fill_h2c(btcoexist, 0x60, 5,
1471                                                 h2c_parameter);
1472                 } else {
1473                         /* keep state under NO PS state, do nothing. */
1474                 }
1475         }
1476 }
1477
1478 void halbtc8703b1ant_power_save_state(IN struct btc_coexist *btcoexist,
1479                               IN u8 ps_type, IN u8 lps_val, IN u8 rpwm_val)
1480 {
1481         boolean         low_pwr_disable = FALSE;
1482
1483         switch (ps_type) {
1484         case BTC_PS_WIFI_NATIVE:
1485                 /* recover to original 32k low power setting */
1486                 coex_sta->force_lps_on = FALSE;
1487                 low_pwr_disable = FALSE;
1488                 btcoexist->btc_set(btcoexist,
1489                                    BTC_SET_ACT_DISABLE_LOW_POWER,
1490                                    &low_pwr_disable);
1491                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_NORMAL_LPS,
1492                                    NULL);
1493
1494                 break;
1495         case BTC_PS_LPS_ON:
1496                 coex_sta->force_lps_on = TRUE;
1497                 halbtc8703b1ant_ps_tdma_check_for_power_save_state(
1498                         btcoexist, TRUE);
1499                 halbtc8703b1ant_lps_rpwm(btcoexist, NORMAL_EXEC,
1500                                          lps_val, rpwm_val);
1501                 /* when coex force to enter LPS, do not enter 32k low power. */
1502                 low_pwr_disable = TRUE;
1503                 btcoexist->btc_set(btcoexist,
1504                                    BTC_SET_ACT_DISABLE_LOW_POWER,
1505                                    &low_pwr_disable);
1506                 /* power save must executed before psTdma.                       */
1507                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS,
1508                                    NULL);
1509
1510                 break;
1511         case BTC_PS_LPS_OFF:
1512                 coex_sta->force_lps_on = FALSE;
1513                 halbtc8703b1ant_ps_tdma_check_for_power_save_state(
1514                         btcoexist, FALSE);
1515                 btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1516                                    NULL);
1517
1518                 break;
1519         default:
1520                 break;
1521         }
1522 }
1523
1524
1525
1526 void halbtc8703b1ant_set_fw_pstdma(IN struct btc_coexist *btcoexist,
1527            IN u8 byte1, IN u8 byte2, IN u8 byte3, IN u8 byte4, IN u8 byte5)
1528 {
1529         u8                      h2c_parameter[5] = {0};
1530         u8                      real_byte1 = byte1, real_byte5 = byte5;
1531         boolean                 ap_enable = FALSE;
1532
1533         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1534                            &ap_enable);
1535
1536         if (ap_enable) {
1537                 if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
1538                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1539                                     "[BTCoex], FW for 1Ant AP mode\n");
1540                         BTC_TRACE(trace_buf);
1541                         real_byte1 &= ~BIT(4);
1542                         real_byte1 |= BIT(5);
1543
1544                         real_byte5 |= BIT(5);
1545                         real_byte5 &= ~BIT(6);
1546
1547                         halbtc8703b1ant_power_save_state(btcoexist,
1548                                                  BTC_PS_WIFI_NATIVE, 0x0,
1549                                                          0x0);
1550                 }
1551         } else if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
1552
1553                 halbtc8703b1ant_power_save_state(
1554                         btcoexist, BTC_PS_LPS_ON, 0x50,
1555                         0x4);
1556         } else {
1557                 halbtc8703b1ant_power_save_state(btcoexist, BTC_PS_WIFI_NATIVE,
1558                                                  0x0,
1559                                                  0x0);
1560         }
1561
1562
1563         h2c_parameter[0] = real_byte1;
1564         h2c_parameter[1] = byte2;
1565         h2c_parameter[2] = byte3;
1566         h2c_parameter[3] = byte4;
1567         h2c_parameter[4] = real_byte5;
1568
1569         coex_dm->ps_tdma_para[0] = real_byte1;
1570         coex_dm->ps_tdma_para[1] = byte2;
1571         coex_dm->ps_tdma_para[2] = byte3;
1572         coex_dm->ps_tdma_para[3] = byte4;
1573         coex_dm->ps_tdma_para[4] = real_byte5;
1574
1575         btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
1576 }
1577
1578
1579 void halbtc8703b1ant_ps_tdma(IN struct btc_coexist *btcoexist,
1580                      IN boolean force_exec, IN boolean turn_on, IN u8 type)
1581 {
1582         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1583         boolean                 wifi_busy = FALSE;
1584         u8                      rssi_adjust_val = 0;
1585         static u8                       psTdmaByte4Modify = 0x0, pre_psTdmaByte4Modify = 0x0;
1586         static boolean   pre_wifi_busy = FALSE;
1587
1588         coex_dm->cur_ps_tdma_on = turn_on;
1589         coex_dm->cur_ps_tdma = type;
1590
1591         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1592
1593         if (wifi_busy != pre_wifi_busy) {
1594                 force_exec = TRUE;
1595                 pre_wifi_busy = wifi_busy;
1596         }
1597
1598         /* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1599         if ((bt_link_info->slave_role) && (bt_link_info->a2dp_exist))
1600                 psTdmaByte4Modify = 0x1;
1601         else
1602                 psTdmaByte4Modify = 0x0;
1603
1604         if (pre_psTdmaByte4Modify != psTdmaByte4Modify) {
1605
1606                 force_exec = TRUE;
1607                 pre_psTdmaByte4Modify = psTdmaByte4Modify;
1608         }
1609
1610         if (!force_exec) {
1611                 if ((coex_dm->pre_ps_tdma_on == coex_dm->cur_ps_tdma_on) &&
1612                     (coex_dm->pre_ps_tdma == coex_dm->cur_ps_tdma))
1613                         return;
1614         }
1615
1616         if (coex_dm->cur_ps_tdma_on) {
1617                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1618                             "[BTCoex], ********** TDMA(on, %d) **********\n",
1619                             coex_dm->cur_ps_tdma);
1620                 BTC_TRACE(trace_buf);
1621         } else {
1622                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1623                             "[BTCoex], ********** TDMA(off, %d) **********\n",
1624                             coex_dm->cur_ps_tdma);
1625                 BTC_TRACE(trace_buf);
1626         }
1627
1628         if (turn_on)    {
1629
1630                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x550, 0x8,
1631                                            0x1);  /* enable TBTT nterrupt */
1632         }
1633
1634
1635         if (turn_on) {
1636                 switch (type) {
1637                 default:
1638                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1639                                                         0x61, 0x35, 0x03, 0x11, 0x11);
1640                         break;
1641
1642                 case 3:
1643                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1644                                                         0x51, 0x3a, 0x03, 0x10, 0x50);
1645                         break;
1646                 case 4:
1647                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1648                                                         0x51, 0x21, 0x03, 0x10, 0x50);
1649                         break;
1650                 case 5:
1651                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1652                                                         0x61, 0x15, 0x03, 0x11, 0x11);
1653                         break;
1654                 case 6:
1655                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1656                                                         0x61, 0x20, 0x03, 0x11, 0x11);
1657                         break;
1658                 case 7:
1659                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1660                                                         0x51, 0x10, 0x03, 0x10,  0x54 |
1661                                                         psTdmaByte4Modify);
1662                         break;
1663                 case 8:
1664                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1665                                                         0x51, 0x10, 0x03, 0x10,  0x54 |
1666                                                         psTdmaByte4Modify);
1667                         break;
1668                 case 9:
1669                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1670                                                         0x51, 0x10, 0x03, 0x10,  0x54 |
1671                                                         psTdmaByte4Modify);
1672                         break;
1673                 case 10:
1674                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1675                                                         0x61, 0x30, 0x03, 0x11, 0x10);
1676                         break;
1677                 case 11:
1678                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1679                                                         0x61, 0x25, 0x03, 0x11,  0x11 |
1680                                                         psTdmaByte4Modify);
1681                         break;
1682                 case 12:
1683                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1684                                                         0x51, 0x30, 0x03, 0x10,  0x50 |
1685                                                         psTdmaByte4Modify);
1686                         break;
1687                 case 13:
1688                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1689                                                         0x51, 0x25, 0x03, 0x10,  0x50 |
1690                                                         psTdmaByte4Modify);
1691                         break;
1692                 case 14:
1693                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1694                                                         0x51, 0x15, 0x03, 0x10,  0x50 |
1695                                                         psTdmaByte4Modify);
1696                         break;
1697                 case 15:
1698                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1699                                                         0x51, 0x20, 0x03, 0x10,  0x50 |
1700                                                         psTdmaByte4Modify);
1701                         break;
1702                 case 16:
1703                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1704                                                         0x61, 0x10, 0x03, 0x11,  0x15 |
1705                                                         psTdmaByte4Modify);
1706                         break;
1707                 case 17:
1708                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1709                                                         0x61, 0x10, 0x03, 0x11, 0x14);
1710                         break;
1711                 case 18:
1712                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1713                                                         0x51, 0x30, 0x03, 0x10,  0x50 |
1714                                                         psTdmaByte4Modify);
1715                         break;
1716                 case 19:
1717                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1718                                                         0x61, 0x15, 0x03, 0x11, 0x10);
1719                         break;
1720                 case 20:
1721                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1722                                                         0x61, 0x30, 0x03, 0x11, 0x10);
1723                         break;
1724                 case 21:
1725                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1726                                                         0x61, 0x30, 0x03, 0x11, 0x10);
1727                         break;
1728                 case 22:
1729                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1730                                                         0x61, 0x25, 0x03, 0x11, 0x10);
1731                         break;
1732                 case 27:
1733                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1734                                                         0x61, 0x10, 0x03, 0x11, 0x15);
1735                         break;
1736                 case 32:
1737                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1738                                                         0x61, 0x35, 0x03, 0x11, 0x11);
1739                         break;
1740                 case 33:
1741                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1742                                                         0x61, 0x35, 0x03, 0x11, 0x10);
1743                         break;
1744                 case 57:
1745                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1746                                                         0x51, 0x10, 0x03, 0x10,  0x50 |
1747                                                         psTdmaByte4Modify);
1748                         break;
1749                 case 58:
1750                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1751                                                         0x51, 0x10, 0x03, 0x10,  0x50 |
1752                                                         psTdmaByte4Modify);
1753                         break;
1754                 case 67:
1755                         halbtc8703b1ant_set_fw_pstdma(btcoexist,
1756                                                         0x61, 0x10, 0x03, 0x11,  0x10 |
1757                                                         psTdmaByte4Modify);
1758                         break;
1759                 }
1760         } else {
1761
1762                 /* disable PS tdma */
1763                 switch (type) {
1764                 case 8: /* PTA Control */
1765                         halbtc8703b1ant_set_fw_pstdma(btcoexist, 0x8,
1766                                                         0x0, 0x0, 0x0, 0x0);
1767                         break;
1768                 case 0:
1769                 default:  /* Software control, Antenna at BT side */
1770                         halbtc8703b1ant_set_fw_pstdma(btcoexist, 0x0,
1771                                                         0x0, 0x0, 0x0, 0x0);
1772                         break;
1773                 case 1: /* 2-Ant, 0x778=3, antenna control by antenna diversity */
1774                         halbtc8703b1ant_set_fw_pstdma(btcoexist, 0x0,
1775                                                         0x0, 0x0, 0x48, 0x0);
1776                         break;
1777                 }
1778         }
1779
1780         /* update pre state */
1781         coex_dm->pre_ps_tdma_on = coex_dm->cur_ps_tdma_on;
1782         coex_dm->pre_ps_tdma = coex_dm->cur_ps_tdma;
1783 }
1784
1785 void halbtc8703b1ant_set_ant_path(IN struct btc_coexist *btcoexist,
1786                                   IN u8 ant_pos_type, IN boolean force_exec,
1787                                   IN u8 phase)
1788 {
1789         u32     cnt_bt_cal_chk = 0;
1790         boolean is_in_mp_mode = FALSE;
1791         u8      u8tmp = 0;
1792         u32     u32tmp1 = 0, u32tmp2 = 0;
1793
1794
1795         u32tmp1 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
1796                                 0x38);
1797
1798         /* To avoid indirect access fail        */
1799         if (((u32tmp1 & 0xf000) >> 12) != ((u32tmp1 & 0x0f00) >> 8)) {
1800                 force_exec = TRUE;
1801                 coex_sta->gnt_error_cnt++;
1802         }
1803
1804 #if 1
1805         u32tmp2 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
1806                         0x54);
1807         u8tmp  = btcoexist->btc_read_1byte(btcoexist, 0x73);
1808
1809         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1810                 "[BTCoex], ********** (Before Ant Setup) 0x73 = 0x%x, 0x38= 0x%x, 0x54= 0x%x**********\n",
1811                     u8tmp, u32tmp1, u32tmp2);
1812         BTC_TRACE(trace_buf);
1813 #endif
1814
1815         coex_dm->cur_ant_pos_type = ant_pos_type;
1816
1817         if (!force_exec) {
1818                 if (coex_dm->cur_ant_pos_type == coex_dm->pre_ant_pos_type) {
1819
1820                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1821                                 "[BTCoex], ********** Skip Antenna Path Setup because no change!!**********\n");
1822                         BTC_TRACE(trace_buf);
1823                         return;
1824                 }
1825         }
1826
1827         coex_dm->pre_ant_pos_type = coex_dm->cur_ant_pos_type;
1828
1829         switch (phase) {
1830         case BT_8703B_1ANT_PHASE_COEX_INIT:
1831                 /* Disable LTE Coex Function in WiFi side (this should be on if LTE coex is required) */
1832                 halbtc8703b1ant_ltecoex_enable(btcoexist, 0x0);
1833
1834                 /* GNT_WL_LTE always = 1 (this should be config if LTE coex is required) */
1835                 halbtc8703b1ant_ltecoex_set_coex_table(
1836                         btcoexist,
1837                         BT_8703B_1ANT_CTT_WL_VS_LTE,
1838                         0xffff);
1839
1840                 /* GNT_BT_LTE always = 1 (this should be config if LTE coex is required) */
1841                 halbtc8703b1ant_ltecoex_set_coex_table(
1842                         btcoexist,
1843                         BT_8703B_1ANT_CTT_BT_VS_LTE,
1844                         0xffff);
1845
1846                 /* Wait If BT IQK running, because Path control owner is at BT during BT IQK (setup by WiFi firmware) */
1847                 while (cnt_bt_cal_chk <= 20) {
1848                         u8tmp = btcoexist->btc_read_1byte(
1849                                         btcoexist,
1850                                         0x49d);
1851                         cnt_bt_cal_chk++;
1852                         if (u8tmp & BIT(0)) {
1853                                 BTC_SPRINTF(trace_buf,
1854                                             BT_TMP_BUF_SIZE,
1855                                         "[BTCoex], ########### BT is calibrating (wait cnt=%d) ###########\n",
1856                                             cnt_bt_cal_chk);
1857                                 BTC_TRACE(trace_buf);
1858                                 delay_ms(50);
1859                         } else {
1860                                 BTC_SPRINTF(trace_buf,
1861                                             BT_TMP_BUF_SIZE,
1862                                         "[BTCoex], ********** BT is NOT calibrating (wait cnt=%d)**********\n",
1863                                             cnt_bt_cal_chk);
1864                                 BTC_TRACE(trace_buf);
1865                                 break;
1866                         }
1867                 }
1868
1869
1870                 /* set Path control owner to WL at initial step */
1871                 halbtc8703b1ant_ltecoex_pathcontrol_owner(
1872                         btcoexist,
1873                         BT_8703B_1ANT_PCO_WLSIDE);
1874
1875                 /* set GNT_BT to SW high */
1876                 halbtc8703b1ant_ltecoex_set_gnt_bt(btcoexist,
1877                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1878                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
1879                                            BT_8703B_1ANT_SIG_STA_SET_TO_HIGH);
1880                 /* Set GNT_WL to SW low */
1881                 halbtc8703b1ant_ltecoex_set_gnt_wl(btcoexist,
1882                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1883                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
1884                                            BT_8703B_1ANT_SIG_STA_SET_TO_LOW);
1885
1886                 if (BTC_ANT_PATH_AUTO == ant_pos_type)
1887                         ant_pos_type = BTC_ANT_PATH_BT;
1888
1889                 coex_sta->run_time_state = FALSE;
1890                 break;
1891         case BT_8703B_1ANT_PHASE_WLANONLY_INIT:
1892                 /* Disable LTE Coex Function in WiFi side (this should be on if LTE coex is required) */
1893                 halbtc8703b1ant_ltecoex_enable(btcoexist, 0x0);
1894
1895                 /* GNT_WL_LTE always = 1 (this should be config if LTE coex is required) */
1896                 halbtc8703b1ant_ltecoex_set_coex_table(
1897                         btcoexist,
1898                         BT_8703B_1ANT_CTT_WL_VS_LTE,
1899                         0xffff);
1900
1901                 /* GNT_BT_LTE always = 1 (this should be config if LTE coex is required) */
1902                 halbtc8703b1ant_ltecoex_set_coex_table(
1903                         btcoexist,
1904                         BT_8703B_1ANT_CTT_BT_VS_LTE,
1905                         0xffff);
1906
1907                 /* set Path control owner to WL at initial step */
1908                 halbtc8703b1ant_ltecoex_pathcontrol_owner(
1909                         btcoexist,
1910                         BT_8703B_1ANT_PCO_WLSIDE);
1911
1912                 /* set GNT_BT to SW low */
1913                 halbtc8703b1ant_ltecoex_set_gnt_bt(btcoexist,
1914                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1915                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
1916                                            BT_8703B_1ANT_SIG_STA_SET_TO_LOW);
1917                 /* Set GNT_WL to SW high */
1918                 halbtc8703b1ant_ltecoex_set_gnt_wl(btcoexist,
1919                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1920                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
1921                                            BT_8703B_1ANT_SIG_STA_SET_TO_HIGH);
1922
1923                 if (BTC_ANT_PATH_AUTO == ant_pos_type)
1924                         ant_pos_type =
1925                                 BTC_ANT_PATH_WIFI;
1926
1927                 coex_sta->run_time_state = FALSE;
1928                 break;
1929         case BT_8703B_1ANT_PHASE_WLAN_OFF:
1930                 /* Disable LTE Coex Function in WiFi side */
1931                 halbtc8703b1ant_ltecoex_enable(btcoexist, 0x0);
1932
1933                 /* set Path control owner to BT */
1934                 halbtc8703b1ant_ltecoex_pathcontrol_owner(
1935                         btcoexist,
1936                         BT_8703B_1ANT_PCO_BTSIDE);
1937
1938                 if (BTC_ANT_PATH_AUTO == ant_pos_type)
1939                         ant_pos_type = BTC_ANT_PATH_BT;
1940
1941                 coex_sta->run_time_state = FALSE;
1942                 break;
1943         case BT_8703B_1ANT_PHASE_2G_RUNTIME:
1944                 halbtc8703b1ant_ltecoex_pathcontrol_owner(
1945                         btcoexist,
1946                         BT_8703B_1ANT_PCO_WLSIDE);
1947
1948                 /* set GNT_BT to PTA */
1949                 halbtc8703b1ant_ltecoex_set_gnt_bt(btcoexist,
1950                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1951                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_PTA,
1952                                            BT_8703B_1ANT_SIG_STA_SET_BY_HW);
1953                 /* Set GNT_WL to PTA */
1954                 halbtc8703b1ant_ltecoex_set_gnt_wl(btcoexist,
1955                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1956                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_PTA,
1957                                            BT_8703B_1ANT_SIG_STA_SET_BY_HW);
1958
1959                 if (BTC_ANT_PATH_AUTO == ant_pos_type)
1960                         ant_pos_type = BTC_ANT_PATH_PTA;
1961
1962                 coex_sta->run_time_state = TRUE;
1963                 break;
1964         case BT_8703B_1ANT_PHASE_BTMPMODE:
1965                 halbtc8703b1ant_ltecoex_pathcontrol_owner(
1966                         btcoexist,
1967                         BT_8703B_1ANT_PCO_WLSIDE);
1968
1969                 /* set GNT_BT to high */
1970                 halbtc8703b1ant_ltecoex_set_gnt_bt(btcoexist,
1971                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1972                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
1973                                            BT_8703B_1ANT_SIG_STA_SET_TO_HIGH);
1974                 /* Set GNT_WL to low */
1975                 halbtc8703b1ant_ltecoex_set_gnt_wl(btcoexist,
1976                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
1977                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
1978                                            BT_8703B_1ANT_SIG_STA_SET_TO_LOW);
1979
1980                 if (BTC_ANT_PATH_AUTO == ant_pos_type)
1981                         ant_pos_type = BTC_ANT_PATH_BT;
1982
1983                 coex_sta->run_time_state = FALSE;
1984                 break;
1985         }
1986
1987
1988 #if 1
1989         u32tmp1 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
1990         u32tmp2 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
1991         u8tmp  = btcoexist->btc_read_1byte(btcoexist, 0x73);
1992
1993
1994         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1995                 "[BTCoex], ********** (After Ant-Setup) 0x73 = 0x%x, 0x38= 0x%x, 0x54= 0x%x**********\n",
1996                     u8tmp, u32tmp1, u32tmp2);
1997         BTC_TRACE(trace_buf);
1998
1999 #endif
2000 }
2001
2002
2003 boolean halbtc8703b1ant_is_common_action(IN struct btc_coexist *btcoexist)
2004 {
2005         boolean                 common = FALSE, wifi_connected = FALSE, wifi_busy = FALSE;
2006
2007         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2008                            &wifi_connected);
2009         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2010
2011         if (!wifi_connected &&
2012             BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2013             coex_dm->bt_status) {
2014                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2015                         "[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
2016                 BTC_TRACE(trace_buf);
2017                 common = TRUE;
2018         } else if (wifi_connected &&
2019                    (BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2020                     coex_dm->bt_status)) {
2021                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2022                         "[BTCoex], Wifi connected + BT non connected-idle!!\n");
2023                 BTC_TRACE(trace_buf);
2024                 common = TRUE;
2025         } else if (!wifi_connected &&
2026                    (BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE ==
2027                     coex_dm->bt_status)) {
2028                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2029                         "[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
2030                 BTC_TRACE(trace_buf);
2031                 common = TRUE;
2032         } else if (wifi_connected &&
2033                    (BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE ==
2034                     coex_dm->bt_status)) {
2035                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2036                             "[BTCoex], Wifi connected + BT connected-idle!!\n");
2037                 BTC_TRACE(trace_buf);
2038                 common = TRUE;
2039         } else if (!wifi_connected &&
2040                    (BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE !=
2041                     coex_dm->bt_status)) {
2042                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2043                             "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
2044                 BTC_TRACE(trace_buf);
2045                 common = TRUE;
2046         } else {
2047                 if (wifi_busy) {
2048                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2049                                 "[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
2050                         BTC_TRACE(trace_buf);
2051                 } else {
2052                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2053                                 "[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
2054                         BTC_TRACE(trace_buf);
2055                 }
2056
2057                 common = FALSE;
2058         }
2059
2060         return common;
2061 }
2062
2063
2064 /* *********************************************
2065  *
2066  *      Non-Software Coex Mechanism start
2067  *
2068  * ********************************************* */
2069 u8 halbtc8703b1ant_action_algorithm(IN struct btc_coexist *btcoexist)
2070 {
2071         struct  btc_bt_link_info        *bt_link_info = &btcoexist->bt_link_info;
2072         boolean bt_hs_on = FALSE;
2073         u8      algorithm = BT_8703B_1ANT_COEX_ALGO_UNDEFINED;
2074         u8      num_of_diff_profile = 0;
2075
2076         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2077
2078         if (!bt_link_info->bt_link_exist) {
2079                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2080                             "[BTCoex], No BT link exists!!!\n");
2081                 BTC_TRACE(trace_buf);
2082                 return algorithm;
2083         }
2084
2085         if (bt_link_info->sco_exist)
2086                 num_of_diff_profile++;
2087         if (bt_link_info->hid_exist)
2088                 num_of_diff_profile++;
2089         if (bt_link_info->pan_exist)
2090                 num_of_diff_profile++;
2091         if (bt_link_info->a2dp_exist)
2092                 num_of_diff_profile++;
2093
2094         if (num_of_diff_profile == 1) {
2095                 if (bt_link_info->sco_exist) {
2096                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2097                                     "[BTCoex], BT Profile = SCO only\n");
2098                         BTC_TRACE(trace_buf);
2099                         algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
2100                 } else {
2101                         if (bt_link_info->hid_exist) {
2102                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2103                                         "[BTCoex], BT Profile = HID only\n");
2104                                 BTC_TRACE(trace_buf);
2105                                 algorithm = BT_8703B_1ANT_COEX_ALGO_HID;
2106                         } else if (bt_link_info->a2dp_exist) {
2107                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2108                                         "[BTCoex], BT Profile = A2DP only\n");
2109                                 BTC_TRACE(trace_buf);
2110                                 algorithm = BT_8703B_1ANT_COEX_ALGO_A2DP;
2111                         } else if (bt_link_info->pan_exist) {
2112                                 if (bt_hs_on) {
2113                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2114                                                 "[BTCoex], BT Profile = PAN(HS) only\n");
2115                                         BTC_TRACE(trace_buf);
2116                                         algorithm =
2117                                                 BT_8703B_1ANT_COEX_ALGO_PANHS;
2118                                 } else {
2119                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2120                                                 "[BTCoex], BT Profile = PAN(EDR) only\n");
2121                                         BTC_TRACE(trace_buf);
2122                                         algorithm =
2123                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR;
2124                                 }
2125                         }
2126                 }
2127         } else if (num_of_diff_profile == 2) {
2128                 if (bt_link_info->sco_exist) {
2129                         if (bt_link_info->hid_exist) {
2130                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2131                                         "[BTCoex], BT Profile = SCO + HID\n");
2132                                 BTC_TRACE(trace_buf);
2133                                 algorithm = BT_8703B_1ANT_COEX_ALGO_HID;
2134                         } else if (bt_link_info->a2dp_exist) {
2135                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2136                                         "[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
2137                                 BTC_TRACE(trace_buf);
2138                                 algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
2139                         } else if (bt_link_info->pan_exist) {
2140                                 if (bt_hs_on) {
2141                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2142                                                 "[BTCoex], BT Profile = SCO + PAN(HS)\n");
2143                                         BTC_TRACE(trace_buf);
2144                                         algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
2145                                 } else {
2146                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2147                                                 "[BTCoex], BT Profile = SCO + PAN(EDR)\n");
2148                                         BTC_TRACE(trace_buf);
2149                                         algorithm =
2150                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
2151                                 }
2152                         }
2153                 } else {
2154                         if (bt_link_info->hid_exist &&
2155                             bt_link_info->a2dp_exist) {
2156                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2157                                         "[BTCoex], BT Profile = HID + A2DP\n");
2158                                 BTC_TRACE(trace_buf);
2159                                 algorithm = BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
2160                         } else if (bt_link_info->hid_exist &&
2161                                    bt_link_info->pan_exist) {
2162                                 if (bt_hs_on) {
2163                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2164                                                 "[BTCoex], BT Profile = HID + PAN(HS)\n");
2165                                         BTC_TRACE(trace_buf);
2166                                         algorithm =
2167                                                 BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
2168                                 } else {
2169                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2170                                                 "[BTCoex], BT Profile = HID + PAN(EDR)\n");
2171                                         BTC_TRACE(trace_buf);
2172                                         algorithm =
2173                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
2174                                 }
2175                         } else if (bt_link_info->pan_exist &&
2176                                    bt_link_info->a2dp_exist) {
2177                                 if (bt_hs_on) {
2178                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2179                                                 "[BTCoex], BT Profile = A2DP + PAN(HS)\n");
2180                                         BTC_TRACE(trace_buf);
2181                                         algorithm =
2182                                                 BT_8703B_1ANT_COEX_ALGO_A2DP_PANHS;
2183                                 } else {
2184                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2185                                                 "[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
2186                                         BTC_TRACE(trace_buf);
2187                                         algorithm =
2188                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR_A2DP;
2189                                 }
2190                         }
2191                 }
2192         } else if (num_of_diff_profile == 3) {
2193                 if (bt_link_info->sco_exist) {
2194                         if (bt_link_info->hid_exist &&
2195                             bt_link_info->a2dp_exist) {
2196                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2197                                         "[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
2198                                 BTC_TRACE(trace_buf);
2199                                 algorithm = BT_8703B_1ANT_COEX_ALGO_HID;
2200                         } else if (bt_link_info->hid_exist &&
2201                                    bt_link_info->pan_exist) {
2202                                 if (bt_hs_on) {
2203                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2204                                                 "[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
2205                                         BTC_TRACE(trace_buf);
2206                                         algorithm =
2207                                                 BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
2208                                 } else {
2209                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2210                                                 "[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
2211                                         BTC_TRACE(trace_buf);
2212                                         algorithm =
2213                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
2214                                 }
2215                         } else if (bt_link_info->pan_exist &&
2216                                    bt_link_info->a2dp_exist) {
2217                                 if (bt_hs_on) {
2218                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2219                                                 "[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
2220                                         BTC_TRACE(trace_buf);
2221                                         algorithm = BT_8703B_1ANT_COEX_ALGO_SCO;
2222                                 } else {
2223                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2224                                                 "[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
2225                                         BTC_TRACE(trace_buf);
2226                                         algorithm =
2227                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
2228                                 }
2229                         }
2230                 } else {
2231                         if (bt_link_info->hid_exist &&
2232                             bt_link_info->pan_exist &&
2233                             bt_link_info->a2dp_exist) {
2234                                 if (bt_hs_on) {
2235                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2236                                                 "[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
2237                                         BTC_TRACE(trace_buf);
2238                                         algorithm =
2239                                                 BT_8703B_1ANT_COEX_ALGO_HID_A2DP;
2240                                 } else {
2241                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2242                                                 "[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
2243                                         BTC_TRACE(trace_buf);
2244                                         algorithm =
2245                                                 BT_8703B_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
2246                                 }
2247                         }
2248                 }
2249         } else if (num_of_diff_profile >= 3) {
2250                 if (bt_link_info->sco_exist) {
2251                         if (bt_link_info->hid_exist &&
2252                             bt_link_info->pan_exist &&
2253                             bt_link_info->a2dp_exist) {
2254                                 if (bt_hs_on) {
2255                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2256                                                 "[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
2257                                         BTC_TRACE(trace_buf);
2258
2259                                 } else {
2260                                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2261                                                 "[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
2262                                         BTC_TRACE(trace_buf);
2263                                         algorithm =
2264                                                 BT_8703B_1ANT_COEX_ALGO_PANEDR_HID;
2265                                 }
2266                         }
2267                 }
2268         }
2269
2270         return algorithm;
2271 }
2272
2273 void halbtc8703b1ant_action_bt_whql_test(IN struct btc_coexist *btcoexist)
2274 {
2275         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2276         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, NORMAL_EXEC,
2277                                      BT_8703B_1ANT_PHASE_2G_RUNTIME);
2278         halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2279 }
2280
2281 void halbtc8703b1ant_action_bt_hs(IN struct btc_coexist *btcoexist)
2282 {
2283         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 5);
2284         halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2285 }
2286
2287 void halbtc8703b1ant_action_bt_relink(IN struct btc_coexist *btcoexist)
2288 {
2289         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 5);
2290         halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
2291 }
2292
2293 void halbtc8703b1ant_action_bt_idle(IN struct btc_coexist *btcoexist)
2294 {
2295         boolean wifi_busy = FALSE;
2296
2297         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2298
2299         if (!wifi_busy) {
2300                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2301                                                         6);
2302                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2303                                                         NORMAL_EXEC, 3);
2304         } else {/* if wl busy */
2305
2306         if (BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2307                         coex_dm->bt_status) {
2308
2309                 if (coex_sta->is_hiPri_rx_overhead)
2310                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2311                                                                 NORMAL_EXEC, 7);
2312                 else
2313                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2314                                                                 NORMAL_EXEC, 8);
2315
2316                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2317                                                         33);
2318         } else {
2319
2320            halbtc8703b1ant_coex_table_with_type(btcoexist,
2321                                                              NORMAL_EXEC, 8);
2322            halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2323                                                         32);
2324           }
2325         }
2326 }
2327
2328 void halbtc8703b1ant_action_bt_inquiry(IN struct btc_coexist *btcoexist)
2329 {
2330         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2331         boolean                 wifi_connected = FALSE, ap_enable = FALSE, wifi_busy = FALSE,
2332                                 bt_busy = FALSE;
2333         boolean wifi_scan = FALSE, wifi_link = FALSE, wifi_roam = FALSE;
2334
2335         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
2336                            &ap_enable);
2337         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2338                            &wifi_connected);
2339         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2340         btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2341         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
2342
2343         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &wifi_link);
2344         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &wifi_roam);
2345
2346         if ((wifi_link) || (wifi_roam)  || (coex_sta->wifi_is_high_pri_task)) {
2347
2348                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 33);
2349                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
2350
2351         } else if (((wifi_scan) || (wifi_busy)) &&
2352                    (coex_sta->bt_create_connection)) {
2353
2354                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 33);
2355                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
2356
2357         } else if ((!wifi_connected) && (!wifi_scan)) {
2358
2359                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2360                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2361         }  else if ((bt_link_info->sco_exist) || (bt_link_info->hid_exist)) {
2362
2363                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 6);
2364                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
2365         } else if ((bt_link_info->a2dp_exist) && (bt_link_info->pan_exist)) {
2366
2367                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 22);
2368                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2369
2370         } else if (bt_link_info->a2dp_exist) {
2371
2372                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 32);
2373
2374                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2375         } else if (wifi_scan) {
2376
2377                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 20);
2378
2379                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2380
2381         } else if (wifi_busy) {
2382                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 21);
2383
2384                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2385         } else {
2386                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 21);
2387
2388                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2389         }
2390 }
2391
2392 void halbtc8703b1ant_action_bt_sco_hid_only_busy(IN struct btc_coexist
2393                 *btcoexist)
2394 {
2395         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2396         boolean wifi_connected = FALSE, wifi_busy = FALSE;
2397         u32  wifi_bw = 1;
2398
2399         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2400                            &wifi_connected);
2401
2402         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW,
2403                            &wifi_bw);
2404
2405         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2406
2407
2408         if (bt_link_info->sco_exist) {
2409                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 5);
2410                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
2411         } else if (coex_sta->hid_busy_num >= 2) {/*for 4/18 hid */
2412                 /* if 11bg mode */
2413                 if (wifi_bw == 0) {
2414
2415                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2416                                                                 NORMAL_EXEC, 6);
2417 #if 0
2418                         halbtc8703b1ant_set_wltoggle_coex_table(btcoexist,
2419                                                                 NORMAL_EXEC,
2420                                                                 0x1, 0xaa,
2421                                                                 0x5a, 0xaa,
2422                                                                 0xaa);
2423 #endif
2424                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 11);
2425                 } else {
2426
2427                 if (wifi_busy) {
2428
2429                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2430                                                                 NORMAL_EXEC, 6);
2431 #if 0
2432                         halbtc8703b1ant_set_wltoggle_coex_table(btcoexist,
2433                                                                 NORMAL_EXEC,
2434                                                                 0x2, 0xaa,
2435                                                                 0x5a, 0xaa,
2436                                                                 0xaa);
2437 #endif
2438                      halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 11);
2439                   } else {
2440
2441                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 6);
2442                         halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
2443                   }
2444                 }
2445         } else {
2446                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2447                                                 TRUE, 6);
2448                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2449                                                              NORMAL_EXEC, 3);
2450                 }
2451 }
2452
2453
2454 void halbtc8703b1ant_action_wifi_only(IN struct btc_coexist *btcoexist)
2455 {
2456         halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 8);
2457         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, FORCE_EXEC,
2458                                      BT_8703B_1ANT_PHASE_2G_RUNTIME);
2459         halbtc8703b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 10);
2460 }
2461
2462 void halbtc8703b1ant_action_wifi_multi_port(IN struct btc_coexist *btcoexist)
2463 {
2464         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2465
2466         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2467         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, NORMAL_EXEC,
2468                                      BT_8703B_1ANT_PHASE_2G_RUNTIME);
2469
2470         if ((BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2471                 coex_dm->bt_status) ||
2472                 (BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE ==
2473                 coex_dm->bt_status))
2474                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 7);
2475         else if (!bt_link_info->pan_exist)
2476                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2477         else
2478                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2479 }
2480
2481 void halbtc8703b1ant_action_wifi_linkscan_process(IN struct btc_coexist
2482                 *btcoexist)
2483 {
2484         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2485
2486         if (bt_link_info->pan_exist) {
2487
2488                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 22);
2489
2490                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2491
2492         } else if (bt_link_info->a2dp_exist) {
2493
2494                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 27);
2495
2496                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2497         } else {
2498
2499                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 21);
2500
2501                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2502         }
2503 }
2504
2505 void halbtc8703b1ant_action_wifi_connected_bt_acl_busy(IN struct btc_coexist
2506                 *btcoexist)
2507 {
2508         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2509         boolean wifi_busy = FALSE, wifi_turbo = FALSE;
2510
2511         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2512         btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
2513                                 &coex_sta->scan_ap_num);
2514         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2515                         "############# [BTCoex],  scan_ap_num = %d, wl_noisy_level = %d\n",
2516                         coex_sta->scan_ap_num, coex_sta->wl_noisy_level);
2517         BTC_TRACE(trace_buf);
2518
2519 #if 1
2520         if ((wifi_busy) && (coex_sta->wl_noisy_level == 0))
2521                 wifi_turbo = TRUE;
2522 #endif
2523
2524         if (bt_link_info->a2dp_only) { /* A2DP           */
2525                 if (!wifi_busy) {
2526                         /*halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2527                                                 32);*/
2528                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2529                                                 27);
2530                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2531                                                                  NORMAL_EXEC, 4);
2532                 } else {
2533
2534                         if (coex_sta->wl_noisy_level == 2)
2535                                 halbtc8703b1ant_ps_tdma(btcoexist,
2536                                                         NORMAL_EXEC, TRUE, 17);
2537                         else
2538                                 halbtc8703b1ant_ps_tdma(btcoexist,
2539                                                         NORMAL_EXEC, TRUE, 7);
2540
2541                         if (wifi_turbo)
2542                                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2543                                                                  NORMAL_EXEC, 8);
2544                         else
2545                                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2546                                                                  NORMAL_EXEC, 4);
2547                 }
2548         } else if (((bt_link_info->a2dp_exist) &&
2549                         (bt_link_info->pan_exist)) ||
2550                    (bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
2551                 bt_link_info->pan_exist)) { /* A2DP+PAN(OPP,FTP), HID+A2DP+PAN(OPP,FTP) */
2552
2553                 if ((bt_link_info->hid_exist) && (coex_sta->hid_busy_num >= 2)) {
2554                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2555                                                                  NORMAL_EXEC, 6);
2556 #if 0
2557                         halbtc8703b1ant_set_wltoggle_coex_table(btcoexist,
2558                                                                 NORMAL_EXEC,
2559                                                                 0x2, 0xaa,
2560                                                                 0x5a, 0xaa,
2561                                                                         0xaa);
2562 #endif
2563                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2564                                                         12);
2565                 } else if (wifi_busy)   {
2566                         if (((coex_sta->a2dp_bit_pool > 40) &&
2567                                  (coex_sta->a2dp_bit_pool < 255)) ||
2568                                 (!coex_sta->is_A2DP_3M))
2569                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2570                                                         TRUE, 15);
2571                         else if (wifi_turbo)
2572                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2573                                                         TRUE, 18);
2574                         else
2575                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC,
2576                                                         TRUE, 13);
2577                 } else
2578                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2579                                                 14);
2580
2581                 if (bt_link_info->hid_exist)
2582                         halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
2583                 else if (wifi_turbo)
2584                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2585                                                                 NORMAL_EXEC, 8);
2586                 else
2587                         halbtc8703b1ant_coex_table_with_type(btcoexist,
2588                                                                 NORMAL_EXEC, 4);
2589         } else if (bt_link_info->hid_exist &&
2590                                         bt_link_info->a2dp_exist) {/* HID+A2DP */
2591
2592                         if ((wifi_busy) && (coex_sta->hid_busy_num >= 2)) { /*for 4/18 hid */
2593                                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2594                                                                 NORMAL_EXEC, 6);
2595 #if 0
2596                                 halbtc8703b1ant_set_wltoggle_coex_table(btcoexist,
2597                                                                 NORMAL_EXEC,
2598                                                                 0x2, 0xaa,
2599                                                                 0x5a, 0xaa,
2600                                                                 0xaa);
2601 #endif
2602                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2603                                                         9);
2604                         } else {
2605                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2606                                                         8);
2607                                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2608                                                         NORMAL_EXEC, 1);
2609                         }
2610
2611         } else if ((bt_link_info->pan_only)
2612                                         || (bt_link_info->hid_exist && bt_link_info->pan_exist)) {
2613                         /* PAN(OPP,FTP), HID+PAN(OPP,FTP) */
2614
2615                 if ((bt_link_info->hid_exist) && (bt_link_info->pan_exist) &&
2616                                 (coex_sta->hid_busy_num >= 2)) {
2617
2618                                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 6);
2619 #if 0
2620                                 halbtc8703b1ant_set_wltoggle_coex_table(btcoexist,
2621                                                                 NORMAL_EXEC,
2622                                                                 0x2, 0xaa,
2623                                                                 0x5a, 0xaa,
2624                                                                 0xaa);
2625 #endif
2626                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2627                                                         12);
2628                 } else {
2629
2630                         if (!wifi_busy)
2631                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2632                                                         4);
2633                         else
2634                                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
2635                                                         3);
2636
2637                         if (bt_link_info->hid_exist)
2638                                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 1);
2639                         else if (wifi_turbo)
2640                                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2641                                                                          NORMAL_EXEC, 8);
2642                         else
2643                                 halbtc8703b1ant_coex_table_with_type(btcoexist,
2644                                                                          NORMAL_EXEC, 4);
2645                 }
2646         } else {
2647                 /* BT no-profile busy (0x9) */
2648                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 33);
2649                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2650         }
2651 }
2652
2653 void halbtc8703b1ant_action_wifi_not_connected(IN struct btc_coexist *btcoexist)
2654 {
2655         /* tdma and coex table */
2656         halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 8);
2657         halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2658 }
2659
2660 void halbtc8703b1ant_action_wifi_connected(IN struct btc_coexist *btcoexist)
2661 {
2662         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2663         boolean wifi_busy = FALSE;
2664
2665         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2666                         "[BTCoex], CoexForWifiConnect()===>\n");
2667         BTC_TRACE(trace_buf);
2668
2669         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
2670                                         NORMAL_EXEC,
2671                                         BT_8703B_1ANT_PHASE_2G_RUNTIME);
2672
2673         if (BT_8703B_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) {
2674
2675                 if (bt_link_info->hid_only)/* HID only */
2676                         halbtc8703b1ant_action_bt_sco_hid_only_busy(btcoexist);
2677                 else
2678                         halbtc8703b1ant_action_wifi_connected_bt_acl_busy(btcoexist);
2679
2680         } else if ((BT_8703B_1ANT_BT_STATUS_SCO_BUSY ==
2681                                 coex_dm->bt_status) ||
2682                            (BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY ==
2683                                 coex_dm->bt_status)) {
2684                         halbtc8703b1ant_action_bt_sco_hid_only_busy(btcoexist);
2685         } else
2686                 halbtc8703b1ant_action_bt_idle(btcoexist);
2687 }
2688
2689
2690 void halbtc8703b1ant_run_sw_coexist_mechanism(IN struct btc_coexist *btcoexist)
2691 {
2692         u8      algorithm = 0;
2693
2694         algorithm = halbtc8703b1ant_action_algorithm(btcoexist);
2695         coex_dm->cur_algorithm = algorithm;
2696
2697         if (halbtc8703b1ant_is_common_action(btcoexist)) {
2698
2699         } else {
2700                 switch (coex_dm->cur_algorithm) {
2701                 case BT_8703B_1ANT_COEX_ALGO_SCO:
2702                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2703                                     "[BTCoex], Action algorithm = SCO.\n");
2704                         BTC_TRACE(trace_buf);
2705                         break;
2706                 case BT_8703B_1ANT_COEX_ALGO_HID:
2707                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2708                                     "[BTCoex], Action algorithm = HID.\n");
2709                         BTC_TRACE(trace_buf);
2710                         break;
2711                 case BT_8703B_1ANT_COEX_ALGO_A2DP:
2712                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2713                                     "[BTCoex], Action algorithm = A2DP.\n");
2714                         BTC_TRACE(trace_buf);
2715                         break;
2716                 case BT_8703B_1ANT_COEX_ALGO_A2DP_PANHS:
2717                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2718                                 "[BTCoex], Action algorithm = A2DP+PAN(HS).\n");
2719                         BTC_TRACE(trace_buf);
2720                         break;
2721                 case BT_8703B_1ANT_COEX_ALGO_PANEDR:
2722                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2723                                     "[BTCoex], Action algorithm = PAN(EDR).\n");
2724                         BTC_TRACE(trace_buf);
2725                         break;
2726                 case BT_8703B_1ANT_COEX_ALGO_PANHS:
2727                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2728                                     "[BTCoex], Action algorithm = HS mode.\n");
2729                         BTC_TRACE(trace_buf);
2730                         break;
2731                 case BT_8703B_1ANT_COEX_ALGO_PANEDR_A2DP:
2732                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2733                                     "[BTCoex], Action algorithm = PAN+A2DP.\n");
2734                         BTC_TRACE(trace_buf);
2735                         break;
2736                 case BT_8703B_1ANT_COEX_ALGO_PANEDR_HID:
2737                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2738                                 "[BTCoex], Action algorithm = PAN(EDR)+HID.\n");
2739                         BTC_TRACE(trace_buf);
2740                         break;
2741                 case BT_8703B_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
2742                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2743                                 "[BTCoex], Action algorithm = HID+A2DP+PAN.\n");
2744                         BTC_TRACE(trace_buf);
2745                         break;
2746                 case BT_8703B_1ANT_COEX_ALGO_HID_A2DP:
2747                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2748                                     "[BTCoex], Action algorithm = HID+A2DP.\n");
2749                         BTC_TRACE(trace_buf);
2750                         break;
2751                 default:
2752                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2753                                 "[BTCoex], Action algorithm = coexist All Off!!\n");
2754                         BTC_TRACE(trace_buf);
2755                         break;
2756                 }
2757                 coex_dm->pre_algorithm = coex_dm->cur_algorithm;
2758         }
2759 }
2760
2761 void halbtc8703b1ant_run_coexist_mechanism(IN struct btc_coexist *btcoexist)
2762 {
2763         struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2764         boolean wifi_connected = FALSE, bt_hs_on = FALSE;
2765         boolean increase_scan_dev_num = FALSE;
2766         boolean bt_ctrl_agg_buf_size = FALSE;
2767         boolean miracast_plus_bt = FALSE, wifi_under_5g = FALSE;
2768         u8      agg_buf_size = 5;
2769         u32 wifi_link_status = 0;
2770         u32 num_of_wifi_link = 0, wifi_bw;
2771         u8      iot_peer = BTC_IOT_PEER_UNKNOWN;
2772         boolean scan = FALSE, link = FALSE, roam = FALSE, under_4way = FALSE;
2773
2774         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2775                         "[BTCoex], RunCoexistMechanism()===>\n");
2776         BTC_TRACE(trace_buf);
2777
2778         if (btcoexist->manual_control) {
2779                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2780                         "[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
2781                 BTC_TRACE(trace_buf);
2782                 return;
2783         }
2784
2785         if (btcoexist->stop_coex_dm) {
2786                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2787                         "[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
2788                 BTC_TRACE(trace_buf);
2789                 return;
2790         }
2791
2792         if (coex_sta->under_ips) {
2793                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2794                                 "[BTCoex], wifi is under IPS !!!\n");
2795                 BTC_TRACE(trace_buf);
2796                 return;
2797         }
2798
2799         if (!coex_sta->run_time_state) {
2800                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2801                         "[BTCoex], return for run_time_state = FALSE !!!\n");
2802                 BTC_TRACE(trace_buf);
2803                 return;
2804         }
2805
2806         if (coex_sta->freeze_coexrun_by_btinfo) {
2807                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2808                         "[BTCoex], BtInfoNotify(), return for freeze_coexrun_by_btinfo\n");
2809                 BTC_TRACE(trace_buf);
2810                 return;
2811         }
2812
2813         if (coex_sta->bt_whck_test) {
2814                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2815                                 "[BTCoex], BT is under WHCK TEST!!!\n");
2816                 BTC_TRACE(trace_buf);
2817                 halbtc8703b1ant_action_bt_whql_test(btcoexist);
2818                 return;
2819         }
2820
2821         if (coex_sta->bt_disabled) {
2822                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2823                                 "[BTCoex], BT is disabled !!!\n");
2824                 halbtc8703b1ant_action_wifi_only(btcoexist);
2825                 return;
2826         }
2827
2828         if (coex_sta->c2h_bt_inquiry_page) {
2829                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2830                                         "[BTCoex], BT is under inquiry/page scan !!\n");
2831                         BTC_TRACE(trace_buf);
2832                         halbtc8703b1ant_action_bt_inquiry(btcoexist);
2833                         return;
2834         }
2835
2836         if (coex_sta->is_setupLink) {
2837                  BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2838                  "[BTCoex], BT is re-link !!!\n");
2839                  halbtc8703b1ant_action_bt_relink(btcoexist);
2840                  return;
2841         }
2842
2843         if ((BT_8703B_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
2844                 (BT_8703B_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
2845                 (BT_8703B_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
2846                 increase_scan_dev_num = TRUE;
2847
2848         btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
2849                                 &increase_scan_dev_num);
2850
2851         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2852                                 &wifi_link_status);
2853
2854         num_of_wifi_link = wifi_link_status >> 16;
2855
2856         if ((num_of_wifi_link >= 2) ||
2857                 (wifi_link_status & WIFI_P2P_GO_CONNECTED)) {
2858                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2859                         "############# [BTCoex],  Multi-Port num_of_wifi_link = %d, wifi_link_status = 0x%x\n",
2860                                 num_of_wifi_link, wifi_link_status);
2861                 BTC_TRACE(trace_buf);
2862
2863                 if (bt_link_info->bt_link_exist)
2864                         miracast_plus_bt = TRUE;
2865                 else
2866                         miracast_plus_bt = FALSE;
2867
2868                 btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2869                                    &miracast_plus_bt);
2870 #if 0
2871                 halbtc8703b1ant_limited_rx(btcoexist, NORMAL_EXEC, FALSE,
2872                                         FALSE, 0x5);
2873 #endif
2874                 halbtc8703b1ant_action_wifi_multi_port(btcoexist);
2875
2876                 return;
2877         } else {
2878
2879         miracast_plus_bt = FALSE;
2880         btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
2881                                 &miracast_plus_bt);
2882         }
2883
2884         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2885         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2886
2887         if ((bt_link_info->bt_link_exist) && (wifi_connected)) {
2888
2889                 btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &iot_peer);
2890
2891                 if (BTC_IOT_PEER_CISCO == iot_peer) {
2892
2893                         if (BTC_WIFI_BW_HT40 == wifi_bw)
2894                                         halbtc8703b1ant_limited_rx(btcoexist,
2895                                                 NORMAL_EXEC, FALSE, TRUE, 0x10);
2896                         else
2897                                         halbtc8703b1ant_limited_rx(btcoexist,
2898                                                 NORMAL_EXEC, FALSE, TRUE, 0x8);
2899                 }
2900         }
2901
2902         halbtc8703b1ant_run_sw_coexist_mechanism(
2903                         btcoexist);  /* just print debug message */
2904
2905         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
2906
2907         if (bt_hs_on) {
2908                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2909                                 "############# [BTCoex],  BT Is hs\n");
2910                 BTC_TRACE(trace_buf);
2911                 halbtc8703b1ant_action_bt_hs(btcoexist);
2912                 return;
2913         }
2914
2915         if ((BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2916                         coex_dm->bt_status) ||
2917                 (BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE ==
2918                         coex_dm->bt_status)) {
2919                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2920                                 "############# [BTCoex],  BT Is idle\n");
2921                 BTC_TRACE(trace_buf);
2922                 halbtc8703b1ant_action_bt_idle(btcoexist);
2923                 return;
2924         }
2925
2926         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
2927         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
2928         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
2929         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
2930                         &under_4way);
2931
2932         if (scan || link || roam || under_4way) {
2933                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2934                         "[BTCoex], scan = %d, link = %d, roam = %d 4way = %d!!!\n",
2935                                         scan, link, roam, under_4way);
2936                 BTC_TRACE(trace_buf);
2937
2938                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2939                         "[BTCoex], wifi is under linkscan process!!\n");
2940                 BTC_TRACE(trace_buf);
2941
2942                 halbtc8703b1ant_action_wifi_linkscan_process(btcoexist);
2943         } else if (wifi_connected) {
2944                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2945                         "[BTCoex], wifi is under connected!!\n");
2946                 BTC_TRACE(trace_buf);
2947
2948                 halbtc8703b1ant_action_wifi_connected(btcoexist);
2949         } else {
2950                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2951                         "[BTCoex], wifi is under not-connected!!\n");
2952                 BTC_TRACE(trace_buf);
2953
2954                 halbtc8703b1ant_action_wifi_not_connected(btcoexist);
2955          }
2956 }
2957
2958
2959 void halbtc8703b1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
2960 {
2961         /* force to reset coex mechanism */
2962
2963         halbtc8703b1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, FALSE);
2964
2965         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2966                     "[BTCoex], Coex Mechanism Init!!\n");
2967         BTC_TRACE(trace_buf);
2968
2969         coex_sta->pop_event_cnt = 0;
2970         coex_sta->cnt_RemoteNameReq = 0;
2971         coex_sta->cnt_ReInit = 0;
2972         coex_sta->cnt_setupLink = 0;
2973         coex_sta->cnt_IgnWlanAct = 0;
2974         coex_sta->cnt_Page = 0;
2975 }
2976
2977 void halbtc8703b1ant_init_hw_config(IN struct btc_coexist *btcoexist,
2978                                     IN boolean back_up, IN boolean wifi_only)
2979 {
2980         u32                             u32tmp0 = 0, u32tmp1 = 0, u32tmp2 = 0;
2981
2982         u32tmp0 = btcoexist->btc_read_4byte(btcoexist, 0x70),
2983         u32tmp1 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
2984         u32tmp2 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
2985
2986         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2987                 "\n [BTCoex], ********** 0x70/ 0x38/ 0x54 (Before Init HW config) = 0x%x/ 0x%x/ 0x%x**********\n",
2988                     u32tmp0,
2989                     u32tmp1, u32tmp2);
2990         BTC_TRACE(trace_buf);
2991
2992         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2993                     "[BTCoex], 1Ant Init HW Config!!\n");
2994         BTC_TRACE(trace_buf);
2995
2996         coex_sta->bt_coex_supported_feature = 0;
2997         coex_sta->bt_coex_supported_version = 0;
2998         coex_sta->bt_ble_scan_type = 0;
2999         coex_sta->bt_ble_scan_para[0] = 0;
3000         coex_sta->bt_ble_scan_para[1] = 0;
3001         coex_sta->bt_ble_scan_para[2] = 0;
3002         coex_sta->gnt_error_cnt = 0;
3003
3004
3005         /* 0xf0[15:12] --> Chip Cut information */
3006         coex_sta->cut_version = (btcoexist->btc_read_1byte(btcoexist,
3007                                  0xf1) & 0xf0) >> 4;
3008
3009         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x550, 0x8,
3010                                            0x1);  /* enable TBTT nterrupt */
3011
3012         /* BT report packet sample rate  */
3013         btcoexist->btc_write_1byte(btcoexist, 0x790, 0x5);
3014
3015         /* Enable BT counter statistics */
3016         btcoexist->btc_write_1byte(btcoexist, 0x778, 0x1);
3017
3018         /* Enable PTA (3-wire function form BT side) */
3019         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
3020         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x41, 0x02, 0x1);
3021
3022         /* Enable PTA (tx/rx signal form WiFi side) */
3023         btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4c6, 0x10, 0x1);
3024
3025         halbtc8703b1ant_enable_gnt_to_gpio(btcoexist, FALSE);
3026
3027 #if 0
3028         if (btcoexist->btc_read_1byte(btcoexist, 0x80) == 0xc6)
3029                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3030                                          BT_8703B_1ANT_SCOREBOARD_ONOFF, TRUE);
3031 #endif
3032
3033         halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 8);
3034
3035         /* Antenna config */
3036         if (coex_sta->is_rf_state_off) {
3037
3038                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3039                                              FORCE_EXEC,
3040                                              BT_8703B_1ANT_PHASE_WLAN_OFF);
3041
3042                 btcoexist->stop_coex_dm = TRUE;
3043
3044                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3045                         "[BTCoex], **********  halbtc8703b1ant_init_hw_config (RF Off)**********\n");
3046                 BTC_TRACE(trace_buf);
3047         } else if (wifi_only) {
3048                 coex_sta->concurrent_rx_mode_on = FALSE;
3049                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
3050                                              FORCE_EXEC,
3051                                              BT_8703B_1ANT_PHASE_WLANONLY_INIT);
3052         } else {
3053                 coex_sta->concurrent_rx_mode_on = TRUE;
3054                 btcoexist->btc_write_1byte_bitmask(btcoexist, 0x953, 0x2, 0x1);
3055                 /* RF 0x1[0] = 0->Set GNT_WL_RF_Rx always = 1 for con-current Rx */
3056                 btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0x1, 0x0);
3057                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3058                                              FORCE_EXEC,
3059                                              BT_8703B_1ANT_PHASE_COEX_INIT);
3060         }
3061
3062         /* PTA parameter */
3063         halbtc8703b1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
3064
3065         u32tmp0 = btcoexist->btc_read_4byte(btcoexist, 0x70),
3066         u32tmp1 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
3067         u32tmp2 = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
3068
3069         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3070                 "[BTCoex], ********** 0x70/ 0x38/ 0x54 (After Init HW config) = 0x%x/ 0x%x/ 0x%x**********\n",
3071                     u32tmp0,
3072                     u32tmp1, u32tmp2);
3073         BTC_TRACE(trace_buf);
3074
3075 }
3076
3077
3078
3079 /* ************************************************************
3080  * work around function start with wa_halbtc8703b1ant_
3081  * ************************************************************
3082  * ************************************************************
3083  * extern function start with ex_halbtc8703b1ant_
3084  * ************************************************************ */
3085 void ex_halbtc8703b1ant_power_on_setting(IN struct btc_coexist *btcoexist)
3086 {
3087         struct  btc_board_info  *board_info = &btcoexist->board_info;
3088         u8 u8tmp = 0x0;
3089         u16 u16tmp = 0x0;
3090
3091         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3092                 "xxxxxxxxxxxxxxxx Execute 8703b 1-Ant PowerOn Setting xxxxxxxxxxxxxxxx!!\n");
3093         BTC_TRACE(trace_buf);
3094
3095         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3096                     "Ant Det Finish = %s, Ant Det Number  = %d\n",
3097                     (board_info->btdm_ant_det_finish ? "Yes" : "No"),
3098                     board_info->btdm_ant_num_by_ant_det);
3099         BTC_TRACE(trace_buf);
3100
3101         btcoexist->stop_coex_dm = TRUE;
3102         coex_sta->is_rf_state_off = FALSE;
3103
3104         /* enable BB, REG_SYS_FUNC_EN such that we can write BB/MAC reg correctly. */
3105         u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
3106         btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT(0) | BIT(1));
3107
3108         /* set Path control owner to WiFi */
3109         halbtc8703b1ant_ltecoex_pathcontrol_owner(btcoexist,
3110                         BT_8703B_1ANT_PCO_WLSIDE);
3111
3112         /* set GNT_BT to high */
3113         halbtc8703b1ant_ltecoex_set_gnt_bt(btcoexist,
3114                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
3115                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
3116                                            BT_8703B_1ANT_SIG_STA_SET_TO_HIGH);
3117         /* Set GNT_WL to low */
3118         halbtc8703b1ant_ltecoex_set_gnt_wl(btcoexist,
3119                                            BT_8703B_1ANT_GNT_BLOCK_RFC_BB,
3120                                            BT_8703B_1ANT_GNT_TYPE_CTRL_BY_SW,
3121                                            BT_8703B_1ANT_SIG_STA_SET_TO_LOW);
3122
3123         /* set WLAN_ACT = 0 */
3124         btcoexist->btc_write_1byte(btcoexist, 0x76e, 0x4);
3125
3126         halbtc8703b1ant_enable_gnt_to_gpio(btcoexist, FALSE);
3127
3128         /* */
3129         /* S0 or S1 setting and Local register setting(By the setting fw can get ant number, S0/S1, ... info) */
3130         /* Local setting bit define */
3131         /*      BIT0: "0" for no antenna inverse; "1" for antenna inverse  */
3132         /*      BIT1: "0" for internal switch; "1" for external switch */
3133         /*      BIT2: "0" for one antenna; "1" for two antenna */
3134         /* NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and BIT2=0 */
3135
3136         u8tmp = 0;
3137         board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
3138
3139         if (btcoexist->chip_interface == BTC_INTF_USB)
3140                 btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
3141         else if (btcoexist->chip_interface == BTC_INTF_SDIO)
3142                 btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60, u8tmp);
3143
3144
3145
3146         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3147                 "[BTCoex], **********  0x70(MAC)/0x38/0x54 (Power-On) =0x%x/  0x%x/ 0x%x**********\n",
3148                     btcoexist->btc_read_4byte(btcoexist, 0x70),
3149                     halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x38),
3150                     halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x54));
3151         BTC_TRACE(trace_buf);
3152
3153
3154 }
3155
3156 void ex_halbtc8703b1ant_pre_load_firmware(IN struct btc_coexist *btcoexist)
3157 {
3158 }
3159
3160 void ex_halbtc8703b1ant_init_hw_config(IN struct btc_coexist *btcoexist,
3161                                        IN boolean wifi_only)
3162 {
3163         halbtc8703b1ant_init_hw_config(btcoexist, TRUE, wifi_only);
3164         btcoexist->stop_coex_dm = FALSE;
3165 }
3166
3167 void ex_halbtc8703b1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
3168 {
3169         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3170                     "[BTCoex], Coex Mechanism Init!!\n");
3171         BTC_TRACE(trace_buf);
3172
3173         halbtc8703b1ant_init_coex_dm(btcoexist);
3174 }
3175
3176 void ex_halbtc8703b1ant_display_coex_info(IN struct btc_coexist *btcoexist)
3177 {
3178         struct  btc_board_info          *board_info = &btcoexist->board_info;
3179         struct  btc_stack_info          *stack_info = &btcoexist->stack_info;
3180         struct  btc_bt_link_info        *bt_link_info = &btcoexist->bt_link_info;
3181         u8                              *cli_buf = btcoexist->cli_buf;
3182         u8                              u8tmp[4], i, bt_info_ext, ps_tdma_case = 0;
3183         u16                             u16tmp[4];
3184         u32                             u32tmp[4];
3185         u32                             fa_ofdm, fa_cck, cca_ofdm, cca_cck;
3186         u32                             fw_ver = 0, bt_patch_ver = 0, bt_coex_ver = 0;
3187         static u8                       pop_report_in_10s = 0;
3188         u32                     phyver = 0;
3189         boolean                 lte_coex_on = FALSE;
3190
3191         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3192                    "\r\n ============[BT Coexist info]============");
3193         CL_PRINTF(cli_buf);
3194
3195         if (btcoexist->manual_control) {
3196                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3197                         "\r\n ============[Under Manual Control]============");
3198                 CL_PRINTF(cli_buf);
3199                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3200                            "\r\n ==========================================");
3201                 CL_PRINTF(cli_buf);
3202         }
3203         if (btcoexist->stop_coex_dm) {
3204                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3205                            "\r\n ============[Coex is STOPPED]============");
3206                 CL_PRINTF(cli_buf);
3207                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3208                            "\r\n ==========================================");
3209                 CL_PRINTF(cli_buf);
3210         }
3211
3212         if (!coex_sta->bt_disabled) {
3213                 if (coex_sta->bt_coex_supported_feature == 0)
3214                         btcoexist->btc_get(btcoexist, BTC_GET_U4_SUPPORTED_FEATURE,
3215                                                 &coex_sta->bt_coex_supported_feature);
3216
3217                 if ((coex_sta->bt_coex_supported_version == 0) ||
3218                          (coex_sta->bt_coex_supported_version == 0xffff))
3219                         btcoexist->btc_get(btcoexist, BTC_GET_U4_SUPPORTED_VERSION,
3220                                                 &coex_sta->bt_coex_supported_version);
3221
3222                 btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver);
3223                 btcoexist->bt_info.bt_get_fw_ver = bt_patch_ver;
3224         }
3225
3226         if (psd_scan->ant_det_try_count == 0) {
3227                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d",
3228                            "Ant PG Num/ Mech/ Pos",
3229                            board_info->pg_ant_num, board_info->btdm_ant_num,
3230                            board_info->btdm_ant_pos);
3231                 CL_PRINTF(cli_buf);
3232         } else {
3233                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3234                            "\r\n %-35s = %d/ %d/ %d  (%d/%d/%d)",
3235                            "Ant PG Num/ Mech(Ant_Det)/ Pos",
3236                            board_info->pg_ant_num,
3237                            board_info->btdm_ant_num_by_ant_det,
3238                            board_info->btdm_ant_pos,
3239                            psd_scan->ant_det_try_count,
3240                            psd_scan->ant_det_fail_count,
3241                            psd_scan->ant_det_result);
3242                 CL_PRINTF(cli_buf);
3243
3244                 if (board_info->btdm_ant_det_finish) {
3245                         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s",
3246                                    "Ant Det PSD Value",
3247                                    psd_scan->ant_det_peak_val);
3248                         CL_PRINTF(cli_buf);
3249                 }
3250         }
3251
3252
3253         /*bt_patch_ver = btcoexist->bt_info.bt_get_fw_ver;*/
3254         bt_patch_ver = btcoexist->bt_info.bt_get_fw_ver;
3255         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3256         phyver = btcoexist->btc_get_bt_phydm_version(btcoexist);
3257
3258         bt_coex_ver = ((coex_sta->bt_coex_supported_version & 0xff00) >> 8);
3259
3260         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3261                    "\r\n %-35s = %d_%02x/ 0x%02x/ 0x%02x (%s)",
3262                    "CoexVer WL/  BT_Desired/ BT_Report",
3263                    glcoex_ver_date_8703b_1ant, glcoex_ver_8703b_1ant,
3264                    glcoex_ver_btdesired_8703b_1ant,
3265                    bt_coex_ver,
3266                    (bt_coex_ver == 0xff ? "Unknown" :
3267                    (bt_coex_ver >= glcoex_ver_btdesired_8703b_1ant ?
3268                       "Match":"Mis-Match")));
3269         CL_PRINTF(cli_buf);
3270
3271         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3272                    "\r\n %-35s = 0x%x/ 0x%x/ v%d/ %c",
3273                    "W_FW/ B_FW/ Phy/ Kt",
3274                    fw_ver, bt_patch_ver, phyver,
3275                    coex_sta->cut_version + 65);
3276         CL_PRINTF(cli_buf);
3277
3278         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x ",
3279                    "Wifi channel informed to BT",
3280                    coex_dm->wifi_chnl_info[0], coex_dm->wifi_chnl_info[1],
3281                    coex_dm->wifi_chnl_info[2]);
3282         CL_PRINTF(cli_buf);
3283         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s",
3284                    "WifibHiPri/ Ccklock/ CckEverLock",
3285                    (coex_sta->wifi_is_high_pri_task ? "Yes" : "No"),
3286                    (coex_sta->cck_lock ? "Yes" : "No"),
3287                    (coex_sta->cck_ever_lock ? "Yes" : "No"));
3288         CL_PRINTF(cli_buf);
3289
3290         /* wifi status */
3291         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3292                    "============[Wifi Status]============");
3293         CL_PRINTF(cli_buf);
3294         btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_WIFI_STATUS);
3295
3296         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3297                    "============[BT Status]============");
3298         CL_PRINTF(cli_buf);
3299
3300         pop_report_in_10s++;
3301         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s/ %d/ %d/ %d] ",
3302                    "BT [status/ rssi/ retryCnt/ popCnt]",
3303                    ((coex_sta->bt_disabled) ? ("disabled") :    ((
3304                            coex_sta->c2h_bt_inquiry_page) ? ("inquiry/page")
3305                            : ((BT_8703B_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
3306                                coex_dm->bt_status) ? "non-connected idle" :
3307                 ((BT_8703B_1ANT_BT_STATUS_CONNECTED_IDLE == coex_dm->bt_status)
3308                                        ? "connected-idle" : "busy")))),
3309                    coex_sta->bt_rssi - 100, coex_sta->bt_retry_cnt,
3310                    coex_sta->pop_event_cnt);
3311         CL_PRINTF(cli_buf);
3312
3313         if (pop_report_in_10s >= 5) {
3314                 coex_sta->pop_event_cnt = 0;
3315                 pop_report_in_10s = 0;
3316         }
3317
3318         if (coex_sta->num_of_profile != 0)
3319                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3320                            "\r\n %-35s = %s%s%s%s%s",
3321                            "Profiles",
3322                            ((bt_link_info->a2dp_exist) ? "A2DP," : ""),
3323                            ((bt_link_info->sco_exist) ?  "SCO," : ""),
3324                            ((bt_link_info->hid_exist) ?
3325                             ((coex_sta->hid_busy_num >= 2) ? "HID(4/18)," :
3326                              "HID(2/18),") : ""),
3327                            ((bt_link_info->pan_exist) ?  "PAN," : ""),
3328                            ((coex_sta->voice_over_HOGP) ? "Voice" : ""));
3329         else
3330                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3331                            "\r\n %-35s = None",
3332                            "Profiles");
3333
3334         CL_PRINTF(cli_buf);
3335
3336         if (bt_link_info->a2dp_exist) {
3337                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %s",
3338                            "A2DP Rate/Bitpool/Auto_Slot",
3339                            ((coex_sta->is_A2DP_3M) ? "3M" : "No_3M"),
3340                            coex_sta->a2dp_bit_pool,
3341                            ((coex_sta->is_autoslot) ? "On" : "Off")
3342                           );
3343                 CL_PRINTF(cli_buf);
3344         }
3345
3346         if (bt_link_info->hid_exist) {
3347                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
3348                            "HID PairNum/Forbid_Slot",
3349                            coex_sta->hid_pair_cnt,
3350                            coex_sta->forbidden_slot
3351                           );
3352                 CL_PRINTF(cli_buf);
3353         }
3354
3355         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s / %s/ 0x%x",
3356                    "Role/IgnWlanAct/Feature",
3357                    ((bt_link_info->slave_role) ? "Slave" : "Master"),
3358                    ((coex_dm->cur_ignore_wlan_act) ? "Yes" : "No"),
3359                    coex_sta->bt_coex_supported_feature);
3360         CL_PRINTF(cli_buf);
3361
3362
3363         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d/ %d",
3364                    "ReInit/ReLink/IgnWlact/Page/NameReq",
3365                    coex_sta->cnt_ReInit,
3366                    coex_sta->cnt_setupLink,
3367                    coex_sta->cnt_IgnWlanAct,
3368                    coex_sta->cnt_Page,
3369                    coex_sta->cnt_RemoteNameReq
3370                   );
3371         CL_PRINTF(cli_buf);
3372
3373         halbtc8703b1ant_read_score_board(btcoexist, &u16tmp[0]);
3374
3375         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %04x",
3376                    "ScoreBoard[14:0] (from BT)", u16tmp[0]);
3377         CL_PRINTF(cli_buf);
3378
3379
3380         for (i = 0; i < BT_INFO_SRC_8703B_1ANT_MAX; i++) {
3381                 if (coex_sta->bt_info_c2h_cnt[i]) {
3382                         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3383                                 "\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x(%d)",
3384                                    glbt_info_src_8703b_1ant[i],
3385                                    coex_sta->bt_info_c2h[i][0],
3386                                    coex_sta->bt_info_c2h[i][1],
3387                                    coex_sta->bt_info_c2h[i][2],
3388                                    coex_sta->bt_info_c2h[i][3],
3389                                    coex_sta->bt_info_c2h[i][4],
3390                                    coex_sta->bt_info_c2h[i][5],
3391                                    coex_sta->bt_info_c2h[i][6],
3392                                    coex_sta->bt_info_c2h_cnt[i]);
3393                         CL_PRINTF(cli_buf);
3394                 }
3395         }
3396
3397         if (btcoexist->manual_control)
3398                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3399                         "============[mechanisms] (before Manual)============");
3400         else
3401                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3402                            "============[mechanisms]============");
3403         CL_PRINTF(cli_buf);
3404
3405         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d",
3406                    "SM[LowPenaltyRA]",
3407                    coex_dm->cur_low_penalty_ra);
3408         CL_PRINTF(cli_buf);
3409
3410         ps_tdma_case = coex_dm->cur_ps_tdma;
3411         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3412                    "\r\n %-35s = %02x %02x %02x %02x %02x case-%d (%s,%s)",
3413                    "PS TDMA",
3414                    coex_dm->ps_tdma_para[0], coex_dm->ps_tdma_para[1],
3415                    coex_dm->ps_tdma_para[2], coex_dm->ps_tdma_para[3],
3416                    coex_dm->ps_tdma_para[4], ps_tdma_case,
3417                    (coex_dm->cur_ps_tdma_on ? "On" : "Off"),
3418                    (coex_dm->auto_tdma_adjust ? "Adj" : "Fix"));
3419
3420         CL_PRINTF(cli_buf);
3421
3422         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d",
3423                    "WL/BT Coex Table Type",
3424                    coex_sta->coex_table_type);
3425         CL_PRINTF(cli_buf);
3426
3427         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
3428         u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
3429         u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
3430         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3431                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3432                    "0x6c0/0x6c4/0x6c8(coexTable)",
3433                    u32tmp[0], u32tmp[1], u32tmp[2]);
3434         CL_PRINTF(cli_buf);
3435
3436         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
3437         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6cc);
3438         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3439                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%x",
3440                    "0x778/0x6cc/IgnWlanAct",
3441                    u8tmp[0], u32tmp[0],  coex_dm->cur_ignore_wlan_act);
3442         CL_PRINTF(cli_buf);
3443
3444         u32tmp[0] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
3445         lte_coex_on = ((u32tmp[0] & BIT(7)) >> 7) ?  TRUE : FALSE;
3446
3447         if (lte_coex_on) {
3448                 u32tmp[0] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
3449                                 0xa0);
3450                 u32tmp[1] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
3451                                 0xa4);
3452
3453                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
3454                            "LTE Coex Table W_L/B_L",
3455                            u32tmp[0] & 0xffff, u32tmp[1] & 0xffff);
3456                 CL_PRINTF(cli_buf);
3457
3458                 u32tmp[0] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
3459                                 0xa8);
3460                 u32tmp[1] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
3461                                 0xac);
3462                 u32tmp[2] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
3463                                 0xb0);
3464                 u32tmp[3] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist,
3465                                 0xb4);
3466
3467                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3468                            "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3469                            "LTE Break Table W_L/B_L/L_W/L_B",
3470                            u32tmp[0] & 0xffff, u32tmp[1] & 0xffff,
3471                            u32tmp[2] & 0xffff, u32tmp[3] & 0xffff);
3472                 CL_PRINTF(cli_buf);
3473         }
3474         /* Hw setting            */
3475         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3476                    "============[Hw setting]============");
3477         CL_PRINTF(cli_buf);
3478
3479         u32tmp[0] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
3480         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x73);
3481
3482         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %s",
3483                    "LTE CoexOn/Path Ctrl Owner",
3484                    (int)((u32tmp[0] & BIT(7)) >> 7),
3485                    ((u8tmp[0] & BIT(2)) ? "WL" : "BT"));
3486         CL_PRINTF(cli_buf);
3487
3488         if (lte_coex_on) {
3489                 CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
3490                            "LTE 3Wire/OPMode/UART/UARTMode",
3491                            (int)((u32tmp[0] & BIT(6)) >> 6),
3492                            (int)((u32tmp[0] & (BIT(5) | BIT(4))) >> 4),
3493                            (int)((u32tmp[0] & BIT(3)) >> 3),
3494                            (int)(u32tmp[0] & (BIT(2) | BIT(1) | BIT(0))));
3495                 CL_PRINTF(cli_buf);
3496         }
3497         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3498                            "\r\n %-35s = %s (BB:%s)/ %s (BB:%s)/ %s %d",
3499                            "GNT_WL_Ctrl/GNT_BT_Ctrl/Dbg",
3500                            ((u32tmp[0] & BIT(12)) ? "SW" : "HW"),
3501                            ((u32tmp[0] & BIT(8)) ?      "SW" : "HW"),
3502                            ((u32tmp[0] & BIT(14)) ? "SW" : "HW"),
3503                            ((u32tmp[0] & BIT(10)) ?  "SW" : "HW"),
3504                            ((u8tmp[0] & BIT(3)) ? "On" : "Off"),
3505                            coex_sta->gnt_error_cnt);
3506         CL_PRINTF(cli_buf);
3507
3508         u32tmp[0] = halbtc8703b1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
3509
3510         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
3511                    "GNT_WL/GNT_BT/LTE_Busy/UART_Busy",
3512                    (int)((u32tmp[0] & BIT(2)) >> 2),
3513                    (int)((u32tmp[0] & BIT(3)) >> 3),
3514                    (int)((u32tmp[0] & BIT(1)) >> 1), (int)(u32tmp[0] & BIT(0)));
3515         CL_PRINTF(cli_buf);
3516
3517
3518         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x4c6);
3519         u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x40);
3520
3521         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
3522                    "0x4c6[4]/0x40[5] (WL/BT PTA)",
3523                    (int)((u8tmp[0] & BIT(4)) >> 4),
3524                    (int)((u8tmp[1] & BIT(5)) >> 5));
3525         CL_PRINTF(cli_buf);
3526
3527         u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
3528         u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
3529         u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x953);
3530         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ %s",
3531                    "0x550(bcn ctrl)/0x522/4-RxAGC",
3532                    u32tmp[0], u8tmp[0], (u8tmp[1] & 0x2) ? "On" : "Off");
3533         CL_PRINTF(cli_buf);
3534
3535         fa_ofdm = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_FA_OFDM);
3536         fa_cck = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_FA_CCK);
3537         cca_ofdm = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_CCA_OFDM);
3538         cca_cck = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_CCA_CCK);
3539
3540         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3541                    "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
3542                    "CCK-CCA/CCK-FA/OFDM-CCA/OFDM-FA",
3543                    cca_cck, fa_cck, cca_ofdm, fa_ofdm);
3544         CL_PRINTF(cli_buf);
3545
3546 #if 1
3547         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
3548                    "CRC_OK CCK/11g/11n/11n-agg",
3549                    coex_sta->crc_ok_cck, coex_sta->crc_ok_11g,
3550                    coex_sta->crc_ok_11n, coex_sta->crc_ok_11n_vht);
3551         CL_PRINTF(cli_buf);
3552
3553         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
3554                    "CRC_Err CCK/11g/11n/11n-agg",
3555                    coex_sta->crc_err_cck, coex_sta->crc_err_11g,
3556                    coex_sta->crc_err_11n, coex_sta->crc_err_11n_vht);
3557         CL_PRINTF(cli_buf);
3558 #endif
3559
3560         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s/ %d",
3561                            "WlHiPri/ Locking/ Locked/ Noisy",
3562                            (coex_sta->wifi_is_high_pri_task ? "Yes" : "No"),
3563                            (coex_sta->cck_lock ? "Yes" : "No"),
3564                            (coex_sta->cck_ever_lock ? "Yes" : "No"),
3565                            coex_sta->wl_noisy_level);
3566         CL_PRINTF(cli_buf);
3567
3568         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d %s",
3569                    "0x770(Hi-pri rx/tx)",
3570                    coex_sta->high_priority_rx, coex_sta->high_priority_tx,
3571                    (coex_sta->is_hiPri_rx_overhead ? "(scan overhead!!)" : ""));
3572         CL_PRINTF(cli_buf);
3573
3574         CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d %s",
3575                    "0x774(Lo-pri rx/tx)",
3576                    coex_sta->low_priority_rx, coex_sta->low_priority_tx,
3577                    (bt_link_info->slave_role ? "(Slave!!)" : ""));
3578         CL_PRINTF(cli_buf);
3579
3580         btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS);
3581 }
3582
3583
3584 void ex_halbtc8703b1ant_ips_notify(IN struct btc_coexist *btcoexist, IN u8 type)
3585 {
3586         if (btcoexist->manual_control ||        btcoexist->stop_coex_dm)
3587                 return;
3588
3589         if (BTC_IPS_ENTER == type) {
3590                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3591                             "[BTCoex], IPS ENTER notify\n");
3592                 BTC_TRACE(trace_buf);
3593                 coex_sta->under_ips = TRUE;
3594                 coex_sta->under_lps = FALSE;
3595
3596                 /* Write WL "Active" in Score-board for LPS off */
3597                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3598                                 BT_8703B_1ANT_SCOREBOARD_ACTIVE |
3599                                 BT_8703B_1ANT_SCOREBOARD_ONOFF |
3600                                 BT_8703B_1ANT_SCOREBOARD_SCAN |
3601                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST,
3602                                 FALSE);
3603
3604                 halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 0);
3605
3606                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3607                                              FORCE_EXEC,
3608                                              BT_8703B_1ANT_PHASE_WLAN_OFF);
3609
3610                 halbtc8703b1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
3611         } else if (BTC_IPS_LEAVE == type) {
3612                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3613                             "[BTCoex], IPS LEAVE notify\n");
3614                 BTC_TRACE(trace_buf);
3615 #if 0
3616                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3617                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, TRUE);
3618 #endif
3619                 halbtc8703b1ant_init_hw_config(btcoexist, FALSE, FALSE);
3620                 halbtc8703b1ant_init_coex_dm(btcoexist);
3621                 halbtc8703b1ant_query_bt_info(btcoexist);
3622
3623                 coex_sta->under_ips = FALSE;
3624         }
3625 }
3626
3627 void ex_halbtc8703b1ant_lps_notify(IN struct btc_coexist *btcoexist, IN u8 type)
3628 {
3629         static boolean  pre_force_lps_on = FALSE;
3630
3631         if (btcoexist->manual_control || btcoexist->stop_coex_dm)
3632                 return;
3633
3634         if (BTC_LPS_ENABLE == type) {
3635                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3636                             "[BTCoex], LPS ENABLE notify\n");
3637                 BTC_TRACE(trace_buf);
3638                 coex_sta->under_lps = TRUE;
3639                 coex_sta->under_ips = FALSE;
3640
3641                 if (coex_sta->force_lps_on == TRUE) { /* LPS No-32K */
3642                         /* Write WL "Active" in Score-board for PS-TDMA */
3643                         pre_force_lps_on = TRUE;
3644                         halbtc8703b1ant_post_state_to_bt(btcoexist,
3645                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, TRUE);
3646
3647                 } else { /* LPS-32K, need check if this h2c 0x71 can work?? (2015/08/28) */
3648                         /* Write WL "Non-Active" in Score-board for Native-PS */
3649                         pre_force_lps_on = FALSE;
3650                         halbtc8703b1ant_post_state_to_bt(btcoexist,
3651                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, FALSE);
3652
3653                         halbtc8703b1ant_coex_table_with_type(btcoexist,
3654                                                              NORMAL_EXEC, 5);
3655
3656                         halbtc8703b1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
3657                         coex_sta->freeze_coexrun_by_btinfo = TRUE;
3658                 }
3659         } else if (BTC_LPS_DISABLE == type) {
3660                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3661                             "[BTCoex], LPS DISABLE notify\n");
3662                 BTC_TRACE(trace_buf);
3663                 coex_sta->under_lps = FALSE;
3664
3665                 /* Write WL "Active" in Score-board for LPS off */
3666                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3667                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, TRUE);
3668
3669                 if (pre_force_lps_on == FALSE) {
3670                         coex_sta->freeze_coexrun_by_btinfo = FALSE;
3671                         halbtc8703b1ant_run_coexist_mechanism(btcoexist);
3672                 }
3673         }
3674 }
3675
3676 void ex_halbtc8703b1ant_scan_notify(IN struct btc_coexist *btcoexist,
3677                                     IN u8 type)
3678 {
3679         boolean wifi_connected = FALSE;
3680
3681         if (btcoexist->manual_control ||
3682             btcoexist->stop_coex_dm)
3683                 return;
3684
3685         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3686                            &wifi_connected);
3687
3688         halbtc8703b1ant_query_bt_info(btcoexist);
3689
3690         if (BTC_SCAN_START == type) {
3691
3692                 coex_sta->wifi_is_high_pri_task = TRUE;
3693
3694                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3695                             "[BTCoex], SCAN START notify\n");
3696                 BTC_TRACE(trace_buf);
3697
3698                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3699                                         BT_8703B_1ANT_SCOREBOARD_ACTIVE |
3700                                         BT_8703B_1ANT_SCOREBOARD_SCAN |
3701                                         BT_8703B_1ANT_SCOREBOARD_ONOFF,
3702                                         TRUE);
3703
3704                 halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE,
3705                                         8);
3706
3707                 /* Force antenna setup for no scan result issue */
3708                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3709                                              FORCE_EXEC,
3710                                              BT_8703B_1ANT_PHASE_2G_RUNTIME);
3711
3712                 halbtc8703b1ant_run_coexist_mechanism(btcoexist);
3713
3714         } else {
3715
3716                 coex_sta->wifi_is_high_pri_task = FALSE;
3717
3718                 btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
3719                                    &coex_sta->scan_ap_num);
3720
3721                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3722                             "[BTCoex], SCAN FINISH notify  (Scan-AP = %d)\n",
3723                             coex_sta->scan_ap_num);
3724                 BTC_TRACE(trace_buf);
3725
3726                 halbtc8703b1ant_run_coexist_mechanism(btcoexist);
3727         }
3728
3729         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3730                     "[BTCoex], SCAN START Notify() end\n");
3731         BTC_TRACE(trace_buf);
3732
3733 }
3734
3735 void ex_halbtc8703b1ant_connect_notify(IN struct btc_coexist *btcoexist,
3736                                        IN u8 type)
3737 {
3738         boolean wifi_connected = FALSE;
3739
3740         if (btcoexist->manual_control ||
3741             btcoexist->stop_coex_dm)
3742                 return;
3743
3744         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3745                            &wifi_connected);
3746
3747         if (BTC_ASSOCIATE_START == type) {
3748                 coex_sta->wifi_is_high_pri_task = TRUE;
3749
3750                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3751                                          BT_8703B_1ANT_SCOREBOARD_ACTIVE |
3752                                          BT_8703B_1ANT_SCOREBOARD_SCAN |
3753                                          BT_8703B_1ANT_SCOREBOARD_ONOFF,
3754                                          TRUE);
3755
3756                 halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE,
3757                                         8);
3758
3759                 /* Force antenna setup for no scan result issue */
3760                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3761                                              FORCE_EXEC,
3762                                              BT_8703B_1ANT_PHASE_2G_RUNTIME);
3763                 /* psd_scan->ant_det_is_ant_det_available = TRUE; */
3764                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3765                             "[BTCoex], CONNECT START notify\n");
3766                 BTC_TRACE(trace_buf);
3767                 coex_dm->arp_cnt = 0;
3768
3769                 halbtc8703b1ant_run_coexist_mechanism(btcoexist);
3770         } else {
3771                 coex_sta->wifi_is_high_pri_task = FALSE;
3772                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3773                             "[BTCoex], CONNECT FINISH notify\n");
3774                 BTC_TRACE(trace_buf);
3775
3776                 halbtc8703b1ant_run_coexist_mechanism(btcoexist);
3777         }
3778
3779 }
3780
3781 void ex_halbtc8703b1ant_media_status_notify(IN struct btc_coexist *btcoexist,
3782                 IN u8 type)
3783 {
3784         boolean                 wifi_under_b_mode = FALSE;
3785
3786         if (btcoexist->manual_control ||
3787             btcoexist->stop_coex_dm)
3788                 return;
3789
3790         if (BTC_MEDIA_CONNECT == type) {
3791
3792                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3793                             "[BTCoex], MEDIA connect notify\n");
3794                 BTC_TRACE(trace_buf);
3795
3796                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3797                                          BT_8703B_1ANT_SCOREBOARD_ACTIVE |
3798                                          BT_8703B_1ANT_SCOREBOARD_ONOFF,
3799                                          TRUE);
3800
3801                 halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE,
3802                                         8);
3803
3804                 /* Force antenna setup for no scan result issue */
3805                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3806                                              FORCE_EXEC,
3807                                              BT_8703B_1ANT_PHASE_2G_RUNTIME);
3808
3809                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
3810                                    &wifi_under_b_mode);
3811
3812                 /* Set CCK Tx/Rx high Pri except 11b mode */
3813                 if (wifi_under_b_mode) {
3814                         btcoexist->btc_write_1byte(btcoexist, 0x6cd,
3815                                                    0x00); /* CCK Tx */
3816                         btcoexist->btc_write_1byte(btcoexist, 0x6cf,
3817                                                    0x00); /* CCK Rx */
3818                 } else {
3819                         /* btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x10); */ /*CCK Tx */
3820                         /* btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x10); */ /*CCK Rx */
3821                         btcoexist->btc_write_1byte(btcoexist, 0x6cd,
3822                                                    0x00); /* CCK Tx */
3823                         btcoexist->btc_write_1byte(btcoexist, 0x6cf,
3824                                                    0x10); /* CCK Rx */
3825                 }
3826
3827                 coex_dm->backup_arfr_cnt1 = btcoexist->btc_read_4byte(btcoexist,
3828                                             0x430);
3829                 coex_dm->backup_arfr_cnt2 = btcoexist->btc_read_4byte(btcoexist,
3830                                             0x434);
3831                 coex_dm->backup_retry_limit = btcoexist->btc_read_2byte(
3832                                                       btcoexist, 0x42a);
3833                 coex_dm->backup_ampdu_max_time = btcoexist->btc_read_1byte(
3834                                 btcoexist, 0x456);
3835         } else {
3836
3837                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3838                             "[BTCoex], MEDIA disconnect notify\n");
3839                 BTC_TRACE(trace_buf);
3840
3841                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3842                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, FALSE);
3843
3844                 btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x0); /* CCK Tx */
3845                 btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x0); /* CCK Rx */
3846
3847                 coex_sta->cck_ever_lock = FALSE;
3848         }
3849
3850         halbtc8703b1ant_update_wifi_channel_info(btcoexist, type);
3851
3852 }
3853
3854 void ex_halbtc8703b1ant_specific_packet_notify(IN struct btc_coexist *btcoexist,
3855                 IN u8 type)
3856 {
3857         boolean under_4way = FALSE;
3858
3859         if (btcoexist->manual_control ||
3860             btcoexist->stop_coex_dm)
3861                 return;
3862
3863
3864         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
3865                            &under_4way);
3866
3867         if (under_4way) {
3868
3869                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3870                             "[BTCoex], specific Packet ---- under_4way!!\n");
3871                 BTC_TRACE(trace_buf);
3872
3873                 coex_sta->wifi_is_high_pri_task = TRUE;
3874                 coex_sta->specific_pkt_period_cnt = 2;
3875         } else if (BTC_PACKET_ARP == type) {
3876
3877                 coex_dm->arp_cnt++;
3878                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3879                             "[BTCoex], specific Packet ARP notify -cnt = %d\n",
3880                             coex_dm->arp_cnt);
3881                 BTC_TRACE(trace_buf);
3882
3883         } else {
3884
3885                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3886                         "[BTCoex], specific Packet DHCP or EAPOL notify [Type = %d]\n",
3887                             type);
3888                 BTC_TRACE(trace_buf);
3889
3890                 coex_sta->wifi_is_high_pri_task = TRUE;
3891                 coex_sta->specific_pkt_period_cnt = 2;
3892         }
3893
3894         if (coex_sta->wifi_is_high_pri_task) {
3895                 halbtc8703b1ant_post_state_to_bt(btcoexist,
3896                                          BT_8703B_1ANT_SCOREBOARD_SCAN, TRUE);
3897                 halbtc8703b1ant_run_coexist_mechanism(btcoexist);
3898         }
3899 }
3900
3901 void ex_halbtc8703b1ant_bt_info_notify(IN struct btc_coexist *btcoexist,
3902                                        IN u8 *tmp_buf, IN u8 length)
3903 {
3904         u8                              i, rsp_source = 0;
3905         boolean                         wifi_connected = FALSE;
3906         boolean wifi_scan = FALSE, wifi_link = FALSE, wifi_roam = FALSE,
3907                     wifi_busy = FALSE;
3908         static boolean is_scoreboard_scan = FALSE;
3909
3910
3911         rsp_source = tmp_buf[0] & 0xf;
3912         if (rsp_source >= BT_INFO_SRC_8703B_1ANT_MAX)
3913                 rsp_source = BT_INFO_SRC_8703B_1ANT_WIFI_FW;
3914         coex_sta->bt_info_c2h_cnt[rsp_source]++;
3915
3916         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3917                     "[BTCoex], Bt_info[%d], len=%d, data=[", rsp_source,
3918                     length);
3919         BTC_TRACE(trace_buf);
3920
3921         for (i = 0; i < length; i++) {
3922                 coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
3923
3924                 if (i == length - 1) {
3925                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x]\n",
3926                                     tmp_buf[i]);
3927                         BTC_TRACE(trace_buf);
3928                 } else {
3929                         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x, ",
3930                                     tmp_buf[i]);
3931                         BTC_TRACE(trace_buf);
3932                 }
3933         }
3934
3935         coex_sta->bt_info = coex_sta->bt_info_c2h[rsp_source][1];
3936         coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
3937         coex_sta->bt_info_ext2 = coex_sta->bt_info_c2h[rsp_source][5];
3938
3939         if (BT_INFO_SRC_8703B_1ANT_WIFI_FW != rsp_source) {
3940
3941                 /* if 0xff, it means BT is under WHCK test */
3942                 coex_sta->bt_whck_test = ((coex_sta->bt_info == 0xff) ? TRUE :
3943                                           FALSE);
3944
3945                 coex_sta->bt_create_connection = ((
3946                         coex_sta->bt_info_c2h[rsp_source][2] & 0x80) ? TRUE :
3947                                                   FALSE);
3948
3949                 /* unit: %, value-100 to translate to unit: dBm */
3950                 coex_sta->bt_rssi = coex_sta->bt_info_c2h[rsp_source][3] * 2 +
3951                                     10;
3952
3953                 coex_sta->c2h_bt_remote_name_req = ((
3954                         coex_sta->bt_info_c2h[rsp_source][2] & 0x20) ? TRUE :
3955                                                     FALSE);
3956
3957                 coex_sta->is_A2DP_3M = ((coex_sta->bt_info_c2h[rsp_source][2] &
3958                                          0x10) ? TRUE : FALSE);
3959
3960                 coex_sta->acl_busy = ((coex_sta->bt_info_c2h[rsp_source][1] &
3961                                        0x9) ? TRUE : FALSE);
3962
3963                 coex_sta->voice_over_HOGP = ((coex_sta->bt_info_ext & 0x10) ?
3964                                              TRUE : FALSE);
3965
3966                 coex_sta->c2h_bt_inquiry_page = ((coex_sta->bt_info &
3967                           BT_INFO_8703B_1ANT_B_INQ_PAGE) ? TRUE : FALSE);
3968
3969                 coex_sta->a2dp_bit_pool = (((
3970                         coex_sta->bt_info_c2h[rsp_source][1] & 0x49) == 0x49) ?
3971                                    coex_sta->bt_info_c2h[rsp_source][6] : 0);
3972
3973                 coex_sta->bt_retry_cnt = coex_sta->bt_info_c2h[rsp_source][2] &
3974                                          0xf;
3975
3976                 coex_sta->is_autoslot = coex_sta->bt_info_ext2 & 0x8;
3977
3978                 coex_sta->forbidden_slot = coex_sta->bt_info_ext2 & 0x7;
3979
3980                 coex_sta->hid_busy_num = (coex_sta->bt_info_ext2 & 0x30) >> 4;
3981
3982                 coex_sta->hid_pair_cnt = (coex_sta->bt_info_ext2 & 0xc0) >> 6;
3983
3984                 if (coex_sta->bt_retry_cnt >= 1)
3985                         coex_sta->pop_event_cnt++;
3986
3987                 if (coex_sta->c2h_bt_remote_name_req)
3988                         coex_sta->cnt_RemoteNameReq++;
3989
3990                 if (coex_sta->bt_info_ext & BIT(1))
3991                         coex_sta->cnt_ReInit++;
3992
3993                 if (coex_sta->bt_info_ext & BIT(2)) {
3994                         coex_sta->cnt_setupLink++;
3995                         coex_sta->is_setupLink = TRUE;
3996                 } else
3997                         coex_sta->is_setupLink = FALSE;
3998
3999                 if (coex_sta->bt_info_ext & BIT(3))
4000                         coex_sta->cnt_IgnWlanAct++;
4001
4002                 if (coex_sta->bt_create_connection) {
4003                         coex_sta->cnt_Page++;
4004
4005                         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
4006                                            &wifi_busy);
4007
4008                         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
4009                         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &wifi_link);
4010                         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &wifi_roam);
4011
4012                         if ((wifi_link) || (wifi_roam) || (wifi_scan) ||
4013                             (coex_sta->wifi_is_high_pri_task) || (wifi_busy)) {
4014
4015                                 is_scoreboard_scan = TRUE;
4016                                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4017                                          BT_8703B_1ANT_SCOREBOARD_SCAN, TRUE);
4018
4019                         } else
4020                                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4021                                          BT_8703B_1ANT_SCOREBOARD_SCAN, FALSE);
4022
4023                 } else {
4024                                 if (is_scoreboard_scan) {
4025                                         halbtc8703b1ant_post_state_to_bt(btcoexist,
4026                                                  BT_8703B_1ANT_SCOREBOARD_SCAN, FALSE);
4027                                         is_scoreboard_scan = FALSE;
4028                                 }
4029                 }
4030
4031                 /* Here we need to resend some wifi info to BT */
4032                 /* because bt is reset and loss of the info. */
4033
4034                 if ((!btcoexist->manual_control) &&
4035                     (!btcoexist->stop_coex_dm)) {
4036
4037                         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4038                                            &wifi_connected);
4039
4040                         /*  Re-Init */
4041                         if ((coex_sta->bt_info_ext & BIT(1))) {
4042                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4043                                         "[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
4044                                 BTC_TRACE(trace_buf);
4045                                 if (wifi_connected)
4046                                         halbtc8703b1ant_update_wifi_channel_info(
4047                                                 btcoexist, BTC_MEDIA_CONNECT);
4048                                 else
4049                                         halbtc8703b1ant_update_wifi_channel_info(
4050                                                 btcoexist,
4051                                                 BTC_MEDIA_DISCONNECT);
4052                         }
4053
4054
4055                         /*  If Ignore_WLanAct && not SetUp_Link */
4056                         if ((coex_sta->bt_info_ext & BIT(3)) &&
4057                             (!(coex_sta->bt_info_ext & BIT(2)))) {
4058
4059                                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4060                                         "[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
4061                                 BTC_TRACE(trace_buf);
4062                                 halbtc8703b1ant_ignore_wlan_act(btcoexist,
4063                                                         FORCE_EXEC, FALSE);
4064                         }
4065                 }
4066
4067         }
4068         if ((coex_sta->bt_info_ext & BIT(5))) {
4069                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4070                                         "[BTCoex], BT ext info bit4 check, query BLE Scan type!!\n");
4071                 BTC_TRACE(trace_buf);
4072                 coex_sta->bt_ble_scan_type = btcoexist->btc_get_ble_scan_type_from_bt(btcoexist);
4073
4074                 if ((coex_sta->bt_ble_scan_type & 0x1) == 0x1)
4075                         coex_sta->bt_ble_scan_para[0]  = btcoexist->btc_get_ble_scan_para_from_bt(btcoexist, 0x1);
4076                 if ((coex_sta->bt_ble_scan_type & 0x2) == 0x2)
4077                         coex_sta->bt_ble_scan_para[1]  = btcoexist->btc_get_ble_scan_para_from_bt(btcoexist, 0x2);
4078                 if ((coex_sta->bt_ble_scan_type & 0x4) == 0x4)
4079                         coex_sta->bt_ble_scan_para[2]  = btcoexist->btc_get_ble_scan_para_from_bt(btcoexist, 0x4);
4080         }
4081
4082         halbtc8703b1ant_update_bt_link_info(btcoexist);
4083
4084         halbtc8703b1ant_run_coexist_mechanism(btcoexist);
4085 }
4086
4087
4088 void ex_halbtc8703b1ant_rf_status_notify(IN struct btc_coexist *btcoexist,
4089                 IN u8 type)
4090 {
4091         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], RF Status notify\n");
4092         BTC_TRACE(trace_buf);
4093
4094         if (BTC_RF_ON == type) {
4095                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4096                             "[BTCoex], RF is turned ON!!\n");
4097                 BTC_TRACE(trace_buf);
4098
4099                 btcoexist->stop_coex_dm = FALSE;
4100                 coex_sta->is_rf_state_off = FALSE;
4101 #if 0
4102                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4103                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, TRUE);
4104                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4105                                          BT_8703B_1ANT_SCOREBOARD_ONOFF, TRUE);
4106 #endif
4107                 /*      halbtc8703b1ant_init_hw_config(btcoexist, FALSE, FALSE); */
4108         } else if (BTC_RF_OFF == type) {
4109                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4110                             "[BTCoex], RF is turned OFF!!\n");
4111                 BTC_TRACE(trace_buf);
4112                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4113                                 BT_8703B_1ANT_SCOREBOARD_ACTIVE |
4114                                 BT_8703B_1ANT_SCOREBOARD_ONOFF |
4115                                 BT_8703B_1ANT_SCOREBOARD_SCAN |
4116                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST,
4117                                 FALSE);
4118
4119                 halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 0);
4120                 halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4121                                              FORCE_EXEC,
4122                                              BT_8703B_1ANT_PHASE_WLAN_OFF);
4123
4124                 btcoexist->stop_coex_dm = TRUE;
4125                 coex_sta->is_rf_state_off = TRUE;
4126         }
4127 }
4128
4129 void ex_halbtc8703b1ant_halt_notify(IN struct btc_coexist *btcoexist)
4130 {
4131         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Halt notify\n");
4132         BTC_TRACE(trace_buf);
4133
4134         halbtc8703b1ant_post_state_to_bt(btcoexist,
4135                                 BT_8703B_1ANT_SCOREBOARD_ACTIVE |
4136                                 BT_8703B_1ANT_SCOREBOARD_ONOFF |
4137                                 BT_8703B_1ANT_SCOREBOARD_SCAN |
4138                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST,
4139                                 FALSE);
4140
4141         halbtc8703b1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 0);
4142         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, FORCE_EXEC,
4143                                      BT_8703B_1ANT_PHASE_WLAN_OFF);
4144
4145         ex_halbtc8703b1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
4146
4147         halbtc8703b1ant_enable_gnt_to_gpio(btcoexist, FALSE);
4148
4149         btcoexist->stop_coex_dm = TRUE;
4150 }
4151
4152 void ex_halbtc8703b1ant_pnp_notify(IN struct btc_coexist *btcoexist,
4153                                    IN u8 pnp_state)
4154 {
4155         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Pnp notify\n");
4156         BTC_TRACE(trace_buf);
4157
4158         if ((BTC_WIFI_PNP_SLEEP == pnp_state) ||
4159             (BTC_WIFI_PNP_SLEEP_KEEP_ANT == pnp_state)) {
4160                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4161                             "[BTCoex], Pnp notify to SLEEP\n");
4162                 BTC_TRACE(trace_buf);
4163
4164                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4165                                 BT_8703B_1ANT_SCOREBOARD_ACTIVE |
4166                                 BT_8703B_1ANT_SCOREBOARD_ONOFF |
4167                                 BT_8703B_1ANT_SCOREBOARD_SCAN |
4168                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST,
4169                                 FALSE);
4170
4171                 if (BTC_WIFI_PNP_SLEEP_KEEP_ANT == pnp_state) {
4172
4173                         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4174                                                      FORCE_EXEC,
4175                                              BT_8703B_1ANT_PHASE_2G_RUNTIME);
4176                 } else {
4177
4178                         halbtc8703b1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4179                                                      FORCE_EXEC,
4180                                              BT_8703B_1ANT_PHASE_WLAN_OFF);
4181                 }
4182
4183                 btcoexist->stop_coex_dm = TRUE;
4184         } else if (BTC_WIFI_PNP_WAKE_UP == pnp_state) {
4185                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4186                             "[BTCoex], Pnp notify to WAKE UP\n");
4187                 BTC_TRACE(trace_buf);
4188 #if 0
4189                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4190                                  BT_8703B_1ANT_SCOREBOARD_ACTIVE, TRUE);
4191                 halbtc8703b1ant_post_state_to_bt(btcoexist,
4192                                                          BT_8703B_1ANT_SCOREBOARD_ONOFF, TRUE);
4193 #endif
4194                 btcoexist->stop_coex_dm = FALSE;
4195         }
4196 }
4197
4198 void ex_halbtc8703b1ant_coex_dm_reset(IN struct btc_coexist *btcoexist)
4199 {
4200         BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4201                 "[BTCoex], *****************Coex DM Reset*****************\n");
4202         BTC_TRACE(trace_buf);
4203
4204         halbtc8703b1ant_init_hw_config(btcoexist, FALSE, FALSE);
4205         halbtc8703b1ant_init_coex_dm(btcoexist);
4206 }
4207
4208 void ex_halbtc8703b1ant_periodical(IN struct btc_coexist *btcoexist)
4209 {
4210         u32     bt_patch_ver;
4211         boolean wifi_busy = FALSE;
4212
4213 #if (BT_AUTO_REPORT_ONLY_8703B_1ANT == 0)
4214         halbtc8703b1ant_query_bt_info(btcoexist);
4215 #endif
4216
4217         halbtc8703b1ant_monitor_bt_ctr(btcoexist);
4218         halbtc8703b1ant_monitor_wifi_ctr(btcoexist);
4219
4220         halbtc8703b1ant_monitor_bt_enable_disable(btcoexist);
4221
4222 #if 0
4223                 btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
4224
4225                 /* halbtc8703b1ant_read_score_board(btcoexist, &bt_scoreboard_val); */
4226
4227                 if (wifi_busy) {
4228                         halbtc8703b1ant_post_state_to_bt(btcoexist,
4229                                         BT_8703B_1ANT_SCOREBOARD_UNDERTEST, TRUE);
4230                         /*
4231                         halbtc8703b1ant_post_state_to_bt(btcoexist,
4232                                                  BT_8703B_1ANT_SCOREBOARD_WLBUSY, TRUE);
4233
4234                         if (bt_scoreboard_val & BIT(6))
4235                                 halbtc8703b1ant_query_bt_info(btcoexist); */
4236                 } else {
4237                         halbtc8703b1ant_post_state_to_bt(btcoexist,
4238                                                 BT_8703B_1ANT_SCOREBOARD_UNDERTEST, FALSE);
4239                         /*
4240                         halbtc8703b1ant_post_state_to_bt(btcoexist,
4241                                                 BT_8703B_1ANT_SCOREBOARD_WLBUSY,
4242                                                 FALSE);  */
4243                 }
4244 #endif
4245
4246
4247         /* for 4-way, DHCP, EAPOL packet */
4248         if (coex_sta->specific_pkt_period_cnt > 0) {
4249
4250                 coex_sta->specific_pkt_period_cnt--;
4251
4252                 if ((coex_sta->specific_pkt_period_cnt == 0) &&
4253                     (coex_sta->wifi_is_high_pri_task))
4254                         coex_sta->wifi_is_high_pri_task = FALSE;
4255
4256                 BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4257                         "[BTCoex], ***************** Hi-Pri Task = %s*****************\n",
4258                             (coex_sta->wifi_is_high_pri_task ? "Yes" :
4259                              "No"));
4260                 BTC_TRACE(trace_buf);
4261
4262         }
4263
4264         if (halbtc8703b1ant_is_wifibt_status_changed(btcoexist))
4265                 halbtc8703b1ant_run_coexist_mechanism(btcoexist);
4266 }
4267
4268 void ex_halbtc8703b1ant_antenna_detection(IN struct btc_coexist *btcoexist,
4269                 IN u32 cent_freq, IN u32 offset, IN u32 span, IN u32 seconds)
4270 {
4271         /* No Antenna Detection required because 8730b is only 1-Ant */
4272 }
4273
4274 void ex_halbtc8703b1ant_antenna_isolation(IN struct btc_coexist *btcoexist,
4275                 IN u32 cent_freq, IN u32 offset, IN u32 span, IN u32 seconds)
4276 {
4277
4278
4279 }
4280
4281 void ex_halbtc8703b1ant_psd_scan(IN struct btc_coexist *btcoexist,
4282                  IN u32 cent_freq, IN u32 offset, IN u32 span, IN u32 seconds)
4283 {
4284
4285
4286 }
4287
4288 void ex_halbtc8703b1ant_display_ant_detection(IN struct btc_coexist *btcoexist)
4289 {
4290
4291 }
4292
4293 #endif
4294
4295 #endif  /* #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1) */
4296