OSDN Git Service

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