GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / rtlwifi / btcoexist / halbtcoutsrc.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2013 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in the
15  * file called LICENSE.
16  *
17  * Contact Information:
18  * wlanfae <wlanfae@realtek.com>
19  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20  * Hsinchu 300, Taiwan.
21  *
22  * Larry Finger <Larry.Finger@lwfinger.net>
23  *
24  ******************************************************************************/
25
26 #include "halbt_precomp.h"
27
28 /***************************************************
29  *              Debug related function
30  ***************************************************/
31
32 static const char *const gl_btc_wifi_bw_string[] = {
33         "11bg",
34         "HT20",
35         "HT40",
36         "HT80",
37         "HT160"
38 };
39
40 static const char *const gl_btc_wifi_freq_string[] = {
41         "2.4G",
42         "5G"
43 };
44
45 static bool halbtc_is_bt_coexist_available(struct btc_coexist *btcoexist)
46 {
47         if (!btcoexist->binded || NULL == btcoexist->adapter)
48                 return false;
49
50         return true;
51 }
52
53 static bool halbtc_is_wifi_busy(struct rtl_priv *rtlpriv)
54 {
55         if (rtlpriv->link_info.busytraffic)
56                 return true;
57         else
58                 return false;
59 }
60
61 static void halbtc_dbg_init(void)
62 {
63 }
64
65 /***************************************************
66  *              helper function
67  ***************************************************/
68 static bool is_any_client_connect_to_ap(struct btc_coexist *btcoexist)
69 {
70         struct rtl_priv *rtlpriv = btcoexist->adapter;
71         struct rtl_mac *mac = rtl_mac(rtlpriv);
72         struct rtl_sta_info *drv_priv;
73         u8 cnt = 0;
74
75         if (mac->opmode == NL80211_IFTYPE_ADHOC ||
76             mac->opmode == NL80211_IFTYPE_MESH_POINT ||
77             mac->opmode == NL80211_IFTYPE_AP) {
78                 if (in_interrupt() > 0) {
79                         list_for_each_entry(drv_priv, &rtlpriv->entry_list,
80                                             list) {
81                                 cnt++;
82                         }
83                 } else {
84                         spin_lock_bh(&rtlpriv->locks.entry_list_lock);
85                         list_for_each_entry(drv_priv, &rtlpriv->entry_list,
86                                             list) {
87                                 cnt++;
88                         }
89                         spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
90                 }
91         }
92         if (cnt > 0)
93                 return true;
94         else
95                 return false;
96 }
97
98 static bool halbtc_legacy(struct rtl_priv *adapter)
99 {
100         struct rtl_priv *rtlpriv = adapter;
101         struct rtl_mac *mac = rtl_mac(rtlpriv);
102
103         bool is_legacy = false;
104
105         if ((mac->mode == WIRELESS_MODE_B) || (mac->mode == WIRELESS_MODE_G))
106                 is_legacy = true;
107
108         return is_legacy;
109 }
110
111 bool halbtc_is_wifi_uplink(struct rtl_priv *adapter)
112 {
113         struct rtl_priv *rtlpriv = adapter;
114
115         if (rtlpriv->link_info.tx_busy_traffic)
116                 return true;
117         else
118                 return false;
119 }
120
121 static u32 halbtc_get_wifi_bw(struct btc_coexist *btcoexist)
122 {
123         struct rtl_priv *rtlpriv =
124                 (struct rtl_priv *)btcoexist->adapter;
125         struct rtl_phy *rtlphy = &rtlpriv->phy;
126         u32 wifi_bw = BTC_WIFI_BW_HT20;
127
128         if (halbtc_legacy(rtlpriv)) {
129                 wifi_bw = BTC_WIFI_BW_LEGACY;
130         } else {
131                 switch (rtlphy->current_chan_bw) {
132                 case HT_CHANNEL_WIDTH_20:
133                         wifi_bw = BTC_WIFI_BW_HT20;
134                         break;
135                 case HT_CHANNEL_WIDTH_20_40:
136                         wifi_bw = BTC_WIFI_BW_HT40;
137                         break;
138                 case HT_CHANNEL_WIDTH_80:
139                         wifi_bw = BTC_WIFI_BW_HT80;
140                         break;
141                 }
142         }
143
144         return wifi_bw;
145 }
146
147 static u8 halbtc_get_wifi_central_chnl(struct btc_coexist *btcoexist)
148 {
149         struct rtl_priv *rtlpriv = btcoexist->adapter;
150         struct rtl_phy  *rtlphy = &rtlpriv->phy;
151         u8 chnl = 1;
152
153         if (rtlphy->current_channel != 0)
154                 chnl = rtlphy->current_channel;
155         RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
156                  "%s:%d\n", __func__, chnl);
157         return chnl;
158 }
159
160 static u8 rtl_get_hwpg_single_ant_path(struct rtl_priv *rtlpriv)
161 {
162         return rtlpriv->btcoexist.btc_info.single_ant_path;
163 }
164
165 static u8 rtl_get_hwpg_bt_type(struct rtl_priv *rtlpriv)
166 {
167         return rtlpriv->btcoexist.btc_info.bt_type;
168 }
169
170 static u8 rtl_get_hwpg_ant_num(struct rtl_priv *rtlpriv)
171 {
172         u8 num;
173
174         if (rtlpriv->btcoexist.btc_info.ant_num == ANT_X2)
175                 num = 2;
176         else
177                 num = 1;
178
179         return num;
180 }
181
182 static u8 rtl_get_hwpg_package_type(struct rtl_priv *rtlpriv)
183 {
184         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
185
186         return rtlhal->package_type;
187 }
188
189 static
190 u8 rtl_get_hwpg_rfe_type(struct rtl_priv *rtlpriv)
191 {
192         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
193
194         return rtlhal->rfe_type;
195 }
196
197 /* ************************************
198  *         Hal helper function
199  * ************************************
200  */
201 static
202 bool halbtc_is_hw_mailbox_exist(struct btc_coexist *btcoexist)
203 {
204         if (IS_HARDWARE_TYPE_8812(btcoexist->adapter))
205                 return false;
206         else
207                 return true;
208 }
209
210 static
211 bool halbtc_send_bt_mp_operation(struct btc_coexist *btcoexist, u8 op_code,
212                                  u8 *cmd, u32 len, unsigned long wait_ms)
213 {
214         struct rtl_priv *rtlpriv;
215         const u8 oper_ver = 0;
216         u8 req_num;
217
218         if (!halbtc_is_hw_mailbox_exist(btcoexist))
219                 return false;
220
221         if (wait_ms)    /* before h2c to avoid race condition */
222                 reinit_completion(&btcoexist->bt_mp_comp);
223
224         rtlpriv = btcoexist->adapter;
225
226         /*
227          * fill req_num by op_code, and rtl_btc_btmpinfo_notify() use it
228          * to know message type
229          */
230         switch (op_code) {
231         case BT_OP_GET_BT_VERSION:
232                 req_num = BT_SEQ_GET_BT_VERSION;
233                 break;
234         case BT_OP_GET_AFH_MAP_L:
235                 req_num = BT_SEQ_GET_AFH_MAP_L;
236                 break;
237         case BT_OP_GET_AFH_MAP_M:
238                 req_num = BT_SEQ_GET_AFH_MAP_M;
239                 break;
240         case BT_OP_GET_AFH_MAP_H:
241                 req_num = BT_SEQ_GET_AFH_MAP_H;
242                 break;
243         case BT_OP_GET_BT_COEX_SUPPORTED_FEATURE:
244                 req_num = BT_SEQ_GET_BT_COEX_SUPPORTED_FEATURE;
245                 break;
246         case BT_OP_GET_BT_COEX_SUPPORTED_VERSION:
247                 req_num = BT_SEQ_GET_BT_COEX_SUPPORTED_VERSION;
248                 break;
249         case BT_OP_GET_BT_ANT_DET_VAL:
250                 req_num = BT_SEQ_GET_BT_ANT_DET_VAL;
251                 break;
252         case BT_OP_GET_BT_BLE_SCAN_PARA:
253                 req_num = BT_SEQ_GET_BT_BLE_SCAN_PARA;
254                 break;
255         case BT_OP_GET_BT_BLE_SCAN_TYPE:
256                 req_num = BT_SEQ_GET_BT_BLE_SCAN_TYPE;
257                 break;
258         case BT_OP_WRITE_REG_ADDR:
259         case BT_OP_WRITE_REG_VALUE:
260         case BT_OP_READ_REG:
261         default:
262                 req_num = BT_SEQ_DONT_CARE;
263                 break;
264         }
265
266         cmd[0] |= (oper_ver & 0x0f);            /* Set OperVer */
267         cmd[0] |= ((req_num << 4) & 0xf0);      /* Set ReqNum */
268         cmd[1] = op_code;
269         rtlpriv->cfg->ops->fill_h2c_cmd(rtlpriv->mac80211.hw, 0x67, len, cmd);
270
271         /* wait? */
272         if (!wait_ms)
273                 return true;
274
275         RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
276                  "btmpinfo wait req_num=%d wait=%ld\n", req_num, wait_ms);
277
278         if (in_interrupt())
279                 return false;
280
281         if (wait_for_completion_timeout(&btcoexist->bt_mp_comp,
282                                         msecs_to_jiffies(wait_ms)) == 0) {
283                 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
284                          "btmpinfo wait (req_num=%d) timeout\n", req_num);
285
286                 return false;   /* timeout */
287         }
288
289         return true;
290 }
291
292 static void halbtc_leave_lps(struct btc_coexist *btcoexist)
293 {
294         struct rtl_priv *rtlpriv;
295         struct rtl_ps_ctl *ppsc;
296         bool ap_enable = false;
297
298         rtlpriv = btcoexist->adapter;
299         ppsc = rtl_psc(rtlpriv);
300
301         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
302                            &ap_enable);
303
304         if (ap_enable) {
305                 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
306                          "%s()<--dont leave lps under AP mode\n", __func__);
307                 return;
308         }
309
310         btcoexist->bt_info.bt_ctrl_lps = true;
311         btcoexist->bt_info.bt_lps_on = false;
312         rtl_lps_leave(rtlpriv->mac80211.hw);
313 }
314
315 static void halbtc_enter_lps(struct btc_coexist *btcoexist)
316 {
317         struct rtl_priv *rtlpriv;
318         struct rtl_ps_ctl *ppsc;
319         bool ap_enable = false;
320
321         rtlpriv = btcoexist->adapter;
322         ppsc = rtl_psc(rtlpriv);
323
324         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
325                            &ap_enable);
326
327         if (ap_enable) {
328                 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
329                          "%s()<--dont enter lps under AP mode\n", __func__);
330                 return;
331         }
332
333         btcoexist->bt_info.bt_ctrl_lps = true;
334         btcoexist->bt_info.bt_lps_on = true;
335         rtl_lps_enter(rtlpriv->mac80211.hw);
336 }
337
338 static void halbtc_normal_lps(struct btc_coexist *btcoexist)
339 {
340         struct rtl_priv *rtlpriv;
341
342         rtlpriv = btcoexist->adapter;
343
344         if (btcoexist->bt_info.bt_ctrl_lps) {
345                 btcoexist->bt_info.bt_lps_on = false;
346                 rtl_lps_leave(rtlpriv->mac80211.hw);
347                 btcoexist->bt_info.bt_ctrl_lps = false;
348         }
349 }
350
351 static void halbtc_leave_low_power(struct btc_coexist *btcoexist)
352 {
353 }
354
355 static void halbtc_normal_low_power(struct btc_coexist *btcoexist)
356 {
357 }
358
359 static void halbtc_disable_low_power(struct btc_coexist *btcoexist,
360                                      bool low_pwr_disable)
361 {
362         /* TODO: original/leave 32k low power */
363         btcoexist->bt_info.bt_disable_low_pwr = low_pwr_disable;
364 }
365
366 static void halbtc_aggregation_check(struct btc_coexist *btcoexist)
367 {
368         bool need_to_act = false;
369         static unsigned long pre_time;
370         unsigned long cur_time = 0;
371         struct rtl_priv *rtlpriv = btcoexist->adapter;
372
373         /* To void continuous deleteBA=>addBA=>deleteBA=>addBA
374          * This function is not allowed to continuous called
375          * It can only be called after 8 seconds
376          */
377
378         cur_time = jiffies;
379         if (jiffies_to_msecs(cur_time - pre_time) <= 8000) {
380                 /* over 8 seconds you can execute this function again. */
381                 return;
382         }
383         pre_time = cur_time;
384
385         if (btcoexist->bt_info.reject_agg_pkt) {
386                 need_to_act = true;
387                 btcoexist->bt_info.pre_reject_agg_pkt =
388                         btcoexist->bt_info.reject_agg_pkt;
389         } else {
390                 if (btcoexist->bt_info.pre_reject_agg_pkt) {
391                         need_to_act = true;
392                         btcoexist->bt_info.pre_reject_agg_pkt =
393                                 btcoexist->bt_info.reject_agg_pkt;
394                 }
395
396                 if (btcoexist->bt_info.pre_bt_ctrl_agg_buf_size !=
397                     btcoexist->bt_info.bt_ctrl_agg_buf_size) {
398                         need_to_act = true;
399                         btcoexist->bt_info.pre_bt_ctrl_agg_buf_size =
400                                 btcoexist->bt_info.bt_ctrl_agg_buf_size;
401                 }
402
403                 if (btcoexist->bt_info.bt_ctrl_agg_buf_size) {
404                         if (btcoexist->bt_info.pre_agg_buf_size !=
405                             btcoexist->bt_info.agg_buf_size) {
406                                 need_to_act = true;
407                         }
408                         btcoexist->bt_info.pre_agg_buf_size =
409                                 btcoexist->bt_info.agg_buf_size;
410                 }
411
412                 if (need_to_act)
413                         rtl_rx_ampdu_apply(rtlpriv);
414         }
415 }
416
417 static u32 halbtc_get_bt_patch_version(struct btc_coexist *btcoexist)
418 {
419         u8 cmd_buffer[4] = {0};
420
421         if (btcoexist->bt_info.bt_real_fw_ver)
422                 goto label_done;
423
424         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
425         halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_BT_VERSION,
426                                     cmd_buffer, 4, 200);
427
428 label_done:
429         return btcoexist->bt_info.bt_real_fw_ver;
430 }
431
432 static u32 halbtc_get_bt_coex_supported_feature(void *btc_context)
433 {
434         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
435         u8 cmd_buffer[4] = {0};
436
437         if (btcoexist->bt_info.bt_supported_feature)
438                 goto label_done;
439
440         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
441         halbtc_send_bt_mp_operation(btcoexist,
442                                     BT_OP_GET_BT_COEX_SUPPORTED_FEATURE,
443                                     cmd_buffer, 4, 200);
444
445 label_done:
446         return btcoexist->bt_info.bt_supported_feature;
447 }
448
449 static u32 halbtc_get_bt_coex_supported_version(void *btc_context)
450 {
451         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
452         u8 cmd_buffer[4] = {0};
453
454         if (btcoexist->bt_info.bt_supported_version)
455                 goto label_done;
456
457         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
458         halbtc_send_bt_mp_operation(btcoexist,
459                                     BT_OP_GET_BT_COEX_SUPPORTED_VERSION,
460                                     cmd_buffer, 4, 200);
461
462 label_done:
463         return btcoexist->bt_info.bt_supported_version;
464 }
465
466 static u32 halbtc_get_wifi_link_status(struct btc_coexist *btcoexist)
467 {
468         /* return value:
469          * [31:16] => connected port number
470          * [15:0]  => port connected bit define
471          */
472         struct rtl_priv *rtlpriv = btcoexist->adapter;
473         struct rtl_mac *mac = rtl_mac(rtlpriv);
474         u32 ret_val = 0;
475         u32 port_connected_status = 0, num_of_connected_port = 0;
476
477         if (mac->opmode == NL80211_IFTYPE_STATION &&
478             mac->link_state >= MAC80211_LINKED) {
479                 port_connected_status |= WIFI_STA_CONNECTED;
480                 num_of_connected_port++;
481         }
482         /* AP & ADHOC & MESH */
483         if (is_any_client_connect_to_ap(btcoexist)) {
484                 port_connected_status |= WIFI_AP_CONNECTED;
485                 num_of_connected_port++;
486         }
487         /* TODO: P2P Connected Status */
488
489         ret_val = (num_of_connected_port << 16) | port_connected_status;
490
491         return ret_val;
492 }
493
494 static s32 halbtc_get_wifi_rssi(struct rtl_priv *rtlpriv)
495 {
496         int undec_sm_pwdb = 0;
497
498         if (rtlpriv->mac80211.link_state >= MAC80211_LINKED)
499                 undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
500         else /* associated entry pwdb */
501                 undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
502         return undec_sm_pwdb;
503 }
504
505 static bool halbtc_get(void *void_btcoexist, u8 get_type, void *out_buf)
506 {
507         struct btc_coexist *btcoexist = (struct btc_coexist *)void_btcoexist;
508         struct rtl_priv *rtlpriv = btcoexist->adapter;
509         struct rtl_phy *rtlphy = &rtlpriv->phy;
510         struct rtl_mac *mac = rtl_mac(rtlpriv);
511         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
512         bool *bool_tmp = (bool *)out_buf;
513         int *s32_tmp = (int *)out_buf;
514         u32 *u32_tmp = (u32 *)out_buf;
515         u8 *u8_tmp = (u8 *)out_buf;
516         bool tmp = false;
517         bool ret = true;
518
519         if (!halbtc_is_bt_coexist_available(btcoexist))
520                 return false;
521
522         switch (get_type) {
523         case BTC_GET_BL_HS_OPERATION:
524                 *bool_tmp = false;
525                 ret = false;
526                 break;
527         case BTC_GET_BL_HS_CONNECTING:
528                 *bool_tmp = false;
529                 ret = false;
530                 break;
531         case BTC_GET_BL_WIFI_CONNECTED:
532                 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_STATION &&
533                     rtlpriv->mac80211.link_state >= MAC80211_LINKED)
534                         tmp = true;
535                 if (is_any_client_connect_to_ap(btcoexist))
536                         tmp = true;
537                 *bool_tmp = tmp;
538                 break;
539         case BTC_GET_BL_WIFI_BUSY:
540                 if (halbtc_is_wifi_busy(rtlpriv))
541                         *bool_tmp = true;
542                 else
543                         *bool_tmp = false;
544                 break;
545         case BTC_GET_BL_WIFI_SCAN:
546                 if (mac->act_scanning)
547                         *bool_tmp = true;
548                 else
549                         *bool_tmp = false;
550                 break;
551         case BTC_GET_BL_WIFI_LINK:
552                 if (mac->link_state == MAC80211_LINKING)
553                         *bool_tmp = true;
554                 else
555                         *bool_tmp = false;
556                 break;
557         case BTC_GET_BL_WIFI_ROAM:
558                 if (mac->link_state == MAC80211_LINKING)
559                         *bool_tmp = true;
560                 else
561                         *bool_tmp = false;
562                 break;
563         case BTC_GET_BL_WIFI_4_WAY_PROGRESS:
564                 *bool_tmp = rtlpriv->btcoexist.btc_info.in_4way;
565                 break;
566         case BTC_GET_BL_WIFI_UNDER_5G:
567                 if (rtlhal->current_bandtype == BAND_ON_5G)
568                         *bool_tmp = true;
569                 else
570                         *bool_tmp = false;
571                 break;
572         case BTC_GET_BL_WIFI_AP_MODE_ENABLE:
573                 if (mac->opmode == NL80211_IFTYPE_AP)
574                         *bool_tmp = true;
575                 else
576                         *bool_tmp = false;
577                 break;
578         case BTC_GET_BL_WIFI_ENABLE_ENCRYPTION:
579                 if (rtlpriv->sec.pairwise_enc_algorithm == NO_ENCRYPTION)
580                         *bool_tmp = false;
581                 else
582                         *bool_tmp = true;
583                 break;
584         case BTC_GET_BL_WIFI_UNDER_B_MODE:
585                 if (rtlpriv->mac80211.mode == WIRELESS_MODE_B)
586                         *bool_tmp = true;
587                 else
588                         *bool_tmp = false;
589                 break;
590         case BTC_GET_BL_EXT_SWITCH:
591                 *bool_tmp = false;
592                 break;
593         case BTC_GET_BL_WIFI_IS_IN_MP_MODE:
594                 *bool_tmp = false;
595                 break;
596         case BTC_GET_BL_IS_ASUS_8723B:
597                 *bool_tmp = false;
598                 break;
599         case BTC_GET_BL_RF4CE_CONNECTED:
600                 *bool_tmp = false;
601                 break;
602         case BTC_GET_S4_WIFI_RSSI:
603                 *s32_tmp = halbtc_get_wifi_rssi(rtlpriv);
604                 break;
605         case BTC_GET_S4_HS_RSSI:
606                 *s32_tmp = 0;
607                 ret = false;
608                 break;
609         case BTC_GET_U4_WIFI_BW:
610                 *u32_tmp = halbtc_get_wifi_bw(btcoexist);
611                 break;
612         case BTC_GET_U4_WIFI_TRAFFIC_DIRECTION:
613                 if (halbtc_is_wifi_uplink(rtlpriv))
614                         *u32_tmp = BTC_WIFI_TRAFFIC_TX;
615                 else
616                         *u32_tmp = BTC_WIFI_TRAFFIC_RX;
617                 break;
618         case BTC_GET_U4_WIFI_FW_VER:
619                 *u32_tmp = (rtlhal->fw_version << 16) | rtlhal->fw_subversion;
620                 break;
621         case BTC_GET_U4_WIFI_LINK_STATUS:
622                 *u32_tmp = halbtc_get_wifi_link_status(btcoexist);
623                 break;
624         case BTC_GET_U4_BT_PATCH_VER:
625                 *u32_tmp = halbtc_get_bt_patch_version(btcoexist);
626                 break;
627         case BTC_GET_U4_VENDOR:
628                 *u32_tmp = BTC_VENDOR_OTHER;
629                 break;
630         case BTC_GET_U4_SUPPORTED_VERSION:
631                 *u32_tmp = halbtc_get_bt_coex_supported_version(btcoexist);
632                 break;
633         case BTC_GET_U4_SUPPORTED_FEATURE:
634                 *u32_tmp = halbtc_get_bt_coex_supported_feature(btcoexist);
635                 break;
636         case BTC_GET_U4_WIFI_IQK_TOTAL:
637                 *u32_tmp = btcoexist->btc_phydm_query_phy_counter(btcoexist,
638                                                                   "IQK_TOTAL");
639                 break;
640         case BTC_GET_U4_WIFI_IQK_OK:
641                 *u32_tmp = btcoexist->btc_phydm_query_phy_counter(btcoexist,
642                                                                   "IQK_OK");
643                 break;
644         case BTC_GET_U4_WIFI_IQK_FAIL:
645                 *u32_tmp = btcoexist->btc_phydm_query_phy_counter(btcoexist,
646                                                                   "IQK_FAIL");
647                 break;
648         case BTC_GET_U1_WIFI_DOT11_CHNL:
649                 *u8_tmp = rtlphy->current_channel;
650                 break;
651         case BTC_GET_U1_WIFI_CENTRAL_CHNL:
652                 *u8_tmp = halbtc_get_wifi_central_chnl(btcoexist);
653                 break;
654         case BTC_GET_U1_WIFI_HS_CHNL:
655                 *u8_tmp = 0;
656                 ret = false;
657                 break;
658         case BTC_GET_U1_AP_NUM:
659                 *u8_tmp = rtlpriv->btcoexist.btc_info.ap_num;
660                 break;
661         case BTC_GET_U1_ANT_TYPE:
662                 *u8_tmp = (u8)BTC_ANT_TYPE_0;
663                 break;
664         case BTC_GET_U1_IOT_PEER:
665                 *u8_tmp = 0;
666                 break;
667
668                 /************* 1Ant **************/
669         case BTC_GET_U1_LPS_MODE:
670                 *u8_tmp = btcoexist->pwr_mode_val[0];
671                 break;
672
673         default:
674                 ret = false;
675                 break;
676         }
677
678         return ret;
679 }
680
681 static bool halbtc_set(void *void_btcoexist, u8 set_type, void *in_buf)
682 {
683         struct btc_coexist *btcoexist = (struct btc_coexist *)void_btcoexist;
684         bool *bool_tmp = (bool *)in_buf;
685         u8 *u8_tmp = (u8 *)in_buf;
686         u32 *u32_tmp = (u32 *)in_buf;
687         bool ret = true;
688
689         if (!halbtc_is_bt_coexist_available(btcoexist))
690                 return false;
691
692         switch (set_type) {
693         /* set some bool type variables. */
694         case BTC_SET_BL_BT_DISABLE:
695                 btcoexist->bt_info.bt_disabled = *bool_tmp;
696                 break;
697         case BTC_SET_BL_BT_TRAFFIC_BUSY:
698                 btcoexist->bt_info.bt_busy = *bool_tmp;
699                 break;
700         case BTC_SET_BL_BT_LIMITED_DIG:
701                 btcoexist->bt_info.limited_dig = *bool_tmp;
702                 break;
703         case BTC_SET_BL_FORCE_TO_ROAM:
704                 btcoexist->bt_info.force_to_roam = *bool_tmp;
705                 break;
706         case BTC_SET_BL_TO_REJ_AP_AGG_PKT:
707                 btcoexist->bt_info.reject_agg_pkt = *bool_tmp;
708                 break;
709         case BTC_SET_BL_BT_CTRL_AGG_SIZE:
710                 btcoexist->bt_info.bt_ctrl_agg_buf_size = *bool_tmp;
711                 break;
712         case BTC_SET_BL_INC_SCAN_DEV_NUM:
713                 btcoexist->bt_info.increase_scan_dev_num = *bool_tmp;
714                 break;
715         case BTC_SET_BL_BT_TX_RX_MASK:
716                 btcoexist->bt_info.bt_tx_rx_mask = *bool_tmp;
717                 break;
718         case BTC_SET_BL_MIRACAST_PLUS_BT:
719                 btcoexist->bt_info.miracast_plus_bt = *bool_tmp;
720                 break;
721                 /* set some u1Byte type variables. */
722         case BTC_SET_U1_RSSI_ADJ_VAL_FOR_AGC_TABLE_ON:
723                 btcoexist->bt_info.rssi_adjust_for_agc_table_on = *u8_tmp;
724                 break;
725         case BTC_SET_U1_AGG_BUF_SIZE:
726                 btcoexist->bt_info.agg_buf_size = *u8_tmp;
727                 break;
728
729         /* the following are some action which will be triggered */
730         case BTC_SET_ACT_GET_BT_RSSI:
731                 ret = false;
732                 break;
733         case BTC_SET_ACT_AGGREGATE_CTRL:
734                 halbtc_aggregation_check(btcoexist);
735                 break;
736
737         /* 1Ant */
738         case BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE:
739                 btcoexist->bt_info.rssi_adjust_for_1ant_coex_type = *u8_tmp;
740                 break;
741         case BTC_SET_UI_SCAN_SIG_COMPENSATION:
742                 break;
743         case BTC_SET_U1_LPS_VAL:
744                 btcoexist->bt_info.lps_val = *u8_tmp;
745                 break;
746         case BTC_SET_U1_RPWM_VAL:
747                 btcoexist->bt_info.rpwm_val = *u8_tmp;
748                 break;
749         /* the following are some action which will be triggered  */
750         case BTC_SET_ACT_LEAVE_LPS:
751                 halbtc_leave_lps(btcoexist);
752                 break;
753         case BTC_SET_ACT_ENTER_LPS:
754                 halbtc_enter_lps(btcoexist);
755                 break;
756         case BTC_SET_ACT_NORMAL_LPS:
757                 halbtc_normal_lps(btcoexist);
758                 break;
759         case BTC_SET_ACT_DISABLE_LOW_POWER:
760                 halbtc_disable_low_power(btcoexist, *bool_tmp);
761                 break;
762         case BTC_SET_ACT_UPDATE_RAMASK:
763                 btcoexist->bt_info.ra_mask = *u32_tmp;
764                 break;
765         case BTC_SET_ACT_SEND_MIMO_PS:
766                 break;
767         case BTC_SET_ACT_CTRL_BT_INFO: /*wait for 8812/8821*/
768                 break;
769         case BTC_SET_ACT_CTRL_BT_COEX:
770                 break;
771         case BTC_SET_ACT_CTRL_8723B_ANT:
772                 break;
773         default:
774                 break;
775         }
776
777         return ret;
778 }
779
780 static void halbtc_display_coex_statistics(struct btc_coexist *btcoexist,
781                                            struct seq_file *m)
782 {
783 }
784
785 static void halbtc_display_bt_link_info(struct btc_coexist *btcoexist,
786                                         struct seq_file *m)
787 {
788 }
789
790 static void halbtc_display_wifi_status(struct btc_coexist *btcoexist,
791                                        struct seq_file *m)
792 {
793         struct rtl_priv *rtlpriv = btcoexist->adapter;
794         s32 wifi_rssi = 0, bt_hs_rssi = 0;
795         bool scan = false, link = false, roam = false, wifi_busy = false;
796         bool wifi_under_b_mode = false, wifi_under_5g = false;
797         u32 wifi_bw = BTC_WIFI_BW_HT20;
798         u32 wifi_traffic_dir = BTC_WIFI_TRAFFIC_TX;
799         u32 wifi_freq = BTC_FREQ_2_4G;
800         u32 wifi_link_status = 0x0;
801         bool bt_hs_on = false, under_ips = false, under_lps = false;
802         bool low_power = false, dc_mode = false;
803         u8 wifi_chnl = 0, wifi_hs_chnl = 0, fw_ps_state;
804         u8 ap_num = 0;
805
806         wifi_link_status = halbtc_get_wifi_link_status(btcoexist);
807         seq_printf(m, "\n %-35s = %d/ %d/ %d/ %d/ %d",
808                    "STA/vWifi/HS/p2pGo/p2pGc",
809                    ((wifi_link_status & WIFI_STA_CONNECTED) ? 1 : 0),
810                    ((wifi_link_status & WIFI_AP_CONNECTED) ? 1 : 0),
811                    ((wifi_link_status & WIFI_HS_CONNECTED) ? 1 : 0),
812                    ((wifi_link_status & WIFI_P2P_GO_CONNECTED) ? 1 : 0),
813                    ((wifi_link_status & WIFI_P2P_GC_CONNECTED) ? 1 : 0));
814
815         btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
816         btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_DOT11_CHNL, &wifi_chnl);
817         btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_HS_CHNL, &wifi_hs_chnl);
818         seq_printf(m, "\n %-35s = %d / %d(%d)",
819                    "Dot11 channel / HsChnl(High Speed)",
820                    wifi_chnl, wifi_hs_chnl, bt_hs_on);
821
822         btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
823         btcoexist->btc_get(btcoexist, BTC_GET_S4_HS_RSSI, &bt_hs_rssi);
824         seq_printf(m, "\n %-35s = %d/ %d",
825                    "Wifi rssi/ HS rssi",
826                    wifi_rssi - 100, bt_hs_rssi - 100);
827
828         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
829         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
830         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
831         seq_printf(m, "\n %-35s = %d/ %d/ %d ",
832                    "Wifi link/ roam/ scan",
833                    link, roam, scan);
834
835         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_5G, &wifi_under_5g);
836         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
837         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
838         btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
839                            &wifi_traffic_dir);
840         btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM, &ap_num);
841         wifi_freq = (wifi_under_5g ? BTC_FREQ_5G : BTC_FREQ_2_4G);
842         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
843                            &wifi_under_b_mode);
844
845         seq_printf(m, "\n %-35s = %s / %s/ %s/ AP=%d ",
846                    "Wifi freq/ bw/ traffic",
847                    gl_btc_wifi_freq_string[wifi_freq],
848                    ((wifi_under_b_mode) ? "11b" :
849                     gl_btc_wifi_bw_string[wifi_bw]),
850                    ((!wifi_busy) ? "idle" : ((BTC_WIFI_TRAFFIC_TX ==
851                                               wifi_traffic_dir) ? "uplink" :
852                                              "downlink")),
853                    ap_num);
854
855         /* power status  */
856         dc_mode = true; /*TODO*/
857         under_ips = rtlpriv->psc.inactive_pwrstate == ERFOFF ? 1 : 0;
858         under_lps = rtlpriv->psc.dot11_psmode == EACTIVE ? 0 : 1;
859         fw_ps_state = 0;
860         low_power = 0; /*TODO*/
861         seq_printf(m, "\n %-35s = %s%s%s%s",
862                    "Power Status",
863                    (dc_mode ? "DC mode" : "AC mode"),
864                    (under_ips ? ", IPS ON" : ""),
865                    (under_lps ? ", LPS ON" : ""),
866                    (low_power ? ", 32k" : ""));
867
868         seq_printf(m,
869                    "\n %-35s = %02x %02x %02x %02x %02x %02x (0x%x/0x%x)",
870                    "Power mode cmd(lps/rpwm)",
871                    btcoexist->pwr_mode_val[0], btcoexist->pwr_mode_val[1],
872                    btcoexist->pwr_mode_val[2], btcoexist->pwr_mode_val[3],
873                    btcoexist->pwr_mode_val[4], btcoexist->pwr_mode_val[5],
874                    btcoexist->bt_info.lps_val,
875                    btcoexist->bt_info.rpwm_val);
876 }
877
878 /************************************************************
879  *              IO related function
880  ************************************************************/
881 static u8 halbtc_read_1byte(void *bt_context, u32 reg_addr)
882 {
883         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
884         struct rtl_priv *rtlpriv = btcoexist->adapter;
885
886         return  rtl_read_byte(rtlpriv, reg_addr);
887 }
888
889 static u16 halbtc_read_2byte(void *bt_context, u32 reg_addr)
890 {
891         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
892         struct rtl_priv *rtlpriv = btcoexist->adapter;
893
894         return  rtl_read_word(rtlpriv, reg_addr);
895 }
896
897 static u32 halbtc_read_4byte(void *bt_context, u32 reg_addr)
898 {
899         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
900         struct rtl_priv *rtlpriv = btcoexist->adapter;
901
902         return  rtl_read_dword(rtlpriv, reg_addr);
903 }
904
905 static void halbtc_write_1byte(void *bt_context, u32 reg_addr, u32 data)
906 {
907         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
908         struct rtl_priv *rtlpriv = btcoexist->adapter;
909
910         rtl_write_byte(rtlpriv, reg_addr, data);
911 }
912
913 static void halbtc_bitmask_write_1byte(void *bt_context, u32 reg_addr,
914                                        u32 bit_mask, u8 data)
915 {
916         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
917         struct rtl_priv *rtlpriv = btcoexist->adapter;
918         u8 original_value, bit_shift = 0;
919         u8 i;
920
921         if (bit_mask != MASKDWORD) {/*if not "double word" write*/
922                 original_value = rtl_read_byte(rtlpriv, reg_addr);
923                 for (i = 0; i <= 7; i++) {
924                         if ((bit_mask >> i) & 0x1)
925                                 break;
926                 }
927                 bit_shift = i;
928                 data = (original_value & (~bit_mask)) |
929                         ((data << bit_shift) & bit_mask);
930         }
931         rtl_write_byte(rtlpriv, reg_addr, data);
932 }
933
934 static void halbtc_write_2byte(void *bt_context, u32 reg_addr, u16 data)
935 {
936         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
937         struct rtl_priv *rtlpriv = btcoexist->adapter;
938
939         rtl_write_word(rtlpriv, reg_addr, data);
940 }
941
942 static void halbtc_write_4byte(void *bt_context, u32 reg_addr, u32 data)
943 {
944         struct btc_coexist *btcoexist =
945                 (struct btc_coexist *)bt_context;
946         struct rtl_priv *rtlpriv = btcoexist->adapter;
947
948         rtl_write_dword(rtlpriv, reg_addr, data);
949 }
950
951 static void halbtc_write_local_reg_1byte(void *btc_context, u32 reg_addr,
952                                          u8 data)
953 {
954         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
955         struct rtl_priv *rtlpriv = btcoexist->adapter;
956
957         if (btcoexist->chip_interface == BTC_INTF_SDIO)
958                 ;
959         else if (btcoexist->chip_interface == BTC_INTF_PCI)
960                 rtl_write_byte(rtlpriv, reg_addr, data);
961         else if (btcoexist->chip_interface == BTC_INTF_USB)
962                 rtl_write_byte(rtlpriv, reg_addr, data);
963 }
964
965 static void halbtc_set_bbreg(void *bt_context, u32 reg_addr, u32 bit_mask,
966                              u32 data)
967 {
968         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
969         struct rtl_priv *rtlpriv = btcoexist->adapter;
970
971         rtl_set_bbreg(rtlpriv->mac80211.hw, reg_addr, bit_mask, data);
972 }
973
974 static u32 halbtc_get_bbreg(void *bt_context, u32 reg_addr, u32 bit_mask)
975 {
976         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
977         struct rtl_priv *rtlpriv = btcoexist->adapter;
978
979         return rtl_get_bbreg(rtlpriv->mac80211.hw, reg_addr, bit_mask);
980 }
981
982 static void halbtc_set_rfreg(void *bt_context, u8 rf_path, u32 reg_addr,
983                              u32 bit_mask, u32 data)
984 {
985         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
986         struct rtl_priv *rtlpriv = btcoexist->adapter;
987
988         rtl_set_rfreg(rtlpriv->mac80211.hw, rf_path, reg_addr, bit_mask, data);
989 }
990
991 static u32 halbtc_get_rfreg(void *bt_context, u8 rf_path, u32 reg_addr,
992                             u32 bit_mask)
993 {
994         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
995         struct rtl_priv *rtlpriv = btcoexist->adapter;
996
997         return rtl_get_rfreg(rtlpriv->mac80211.hw, rf_path, reg_addr, bit_mask);
998 }
999
1000 static void halbtc_fill_h2c_cmd(void *bt_context, u8 element_id,
1001                                 u32 cmd_len, u8 *cmd_buf)
1002 {
1003         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
1004         struct rtl_priv *rtlpriv = btcoexist->adapter;
1005
1006         rtlpriv->cfg->ops->fill_h2c_cmd(rtlpriv->mac80211.hw, element_id,
1007                                         cmd_len, cmd_buf);
1008 }
1009
1010 static void halbtc_send_wifi_port_id_cmd(void *bt_context)
1011 {
1012         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
1013         struct rtl_priv *rtlpriv = btcoexist->adapter;
1014         u8 cmd_buf[1] = {0};    /* port id [2:0] = 0 */
1015
1016         rtlpriv->cfg->ops->fill_h2c_cmd(rtlpriv->mac80211.hw, 0x71, 1,
1017                                         cmd_buf);
1018 }
1019
1020 static void halbtc_set_default_port_id_cmd(void *bt_context)
1021 {
1022         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
1023         struct rtl_priv *rtlpriv = btcoexist->adapter;
1024         struct ieee80211_hw *hw = rtlpriv->mac80211.hw;
1025
1026         if (!rtlpriv->cfg->ops->set_default_port_id_cmd)
1027                 return;
1028
1029         rtlpriv->cfg->ops->set_default_port_id_cmd(hw);
1030 }
1031
1032 static
1033 void halbtc_set_bt_reg(void *btc_context, u8 reg_type, u32 offset, u32 set_val)
1034 {
1035         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1036         u8 cmd_buffer1[4] = {0};
1037         u8 cmd_buffer2[4] = {0};
1038
1039         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1040         *((__le16 *)&cmd_buffer1[2]) = cpu_to_le16((u16)set_val);
1041         if (!halbtc_send_bt_mp_operation(btcoexist, BT_OP_WRITE_REG_VALUE,
1042                                          cmd_buffer1, 4, 200))
1043                 return;
1044
1045         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1046         cmd_buffer2[2] = reg_type;
1047         *((u8 *)&cmd_buffer2[3]) = (u8)offset;
1048         halbtc_send_bt_mp_operation(btcoexist, BT_OP_WRITE_REG_ADDR,
1049                                     cmd_buffer2, 4, 200);
1050 }
1051
1052 static void halbtc_display_dbg_msg(void *bt_context, u8 disp_type,
1053                                    struct seq_file *m)
1054 {
1055         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
1056
1057         switch (disp_type) {
1058         case BTC_DBG_DISP_COEX_STATISTICS:
1059                 halbtc_display_coex_statistics(btcoexist, m);
1060                 break;
1061         case BTC_DBG_DISP_BT_LINK_INFO:
1062                 halbtc_display_bt_link_info(btcoexist, m);
1063                 break;
1064         case BTC_DBG_DISP_WIFI_STATUS:
1065                 halbtc_display_wifi_status(btcoexist, m);
1066                 break;
1067         default:
1068                 break;
1069         }
1070 }
1071
1072 static u32 halbtc_get_bt_reg(void *btc_context, u8 reg_type, u32 offset)
1073 {
1074         return 0;
1075 }
1076
1077 static
1078 u32 halbtc_get_phydm_version(void *btc_context)
1079 {
1080         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1081         struct rtl_priv *rtlpriv = btcoexist->adapter;
1082
1083         if (rtlpriv->phydm.ops)
1084                 return rtlpriv->phydm.ops->phydm_get_version(rtlpriv);
1085
1086         return 0;
1087 }
1088
1089 static
1090 void halbtc_phydm_modify_ra_pcr_threshold(void *btc_context,
1091                                           u8 ra_offset_direction,
1092                                           u8 ra_threshold_offset)
1093 {
1094         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1095         struct rtl_priv *rtlpriv = btcoexist->adapter;
1096         struct rtl_phydm_ops *phydm_ops = rtlpriv->phydm.ops;
1097
1098         if (phydm_ops)
1099                 phydm_ops->phydm_modify_ra_pcr_threshold(rtlpriv,
1100                                                          ra_offset_direction,
1101                                                          ra_threshold_offset);
1102 }
1103
1104 static
1105 u32 halbtc_phydm_query_phy_counter(void *btc_context, const char *info_type)
1106 {
1107         /* info_type may be strings below:
1108          * PHYDM_INFO_FA_OFDM, PHYDM_INFO_FA_CCK, PHYDM_INFO_CCA_OFDM,
1109          * PHYDM_INFO_CCA_CCK
1110          * IQK_TOTAL, IQK_OK, IQK_FAIL
1111          */
1112
1113         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1114         struct rtl_priv *rtlpriv = btcoexist->adapter;
1115         struct rtl_phydm_ops *phydm_ops = rtlpriv->phydm.ops;
1116
1117         if (phydm_ops)
1118                 return phydm_ops->phydm_query_counter(rtlpriv, info_type);
1119
1120         return 0;
1121 }
1122
1123 static u8 halbtc_get_ant_det_val_from_bt(void *btc_context)
1124 {
1125         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1126         u8 cmd_buffer[4] = {0};
1127
1128         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1129         halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_BT_ANT_DET_VAL,
1130                                     cmd_buffer, 4, 200);
1131
1132         /* need wait completion to return correct value */
1133
1134         return btcoexist->bt_info.bt_ant_det_val;
1135 }
1136
1137 static u8 halbtc_get_ble_scan_type_from_bt(void *btc_context)
1138 {
1139         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1140         u8 cmd_buffer[4] = {0};
1141
1142         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1143         halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_BT_BLE_SCAN_TYPE,
1144                                     cmd_buffer, 4, 200);
1145
1146         /* need wait completion to return correct value */
1147
1148         return btcoexist->bt_info.bt_ble_scan_type;
1149 }
1150
1151 static u32 halbtc_get_ble_scan_para_from_bt(void *btc_context, u8 scan_type)
1152 {
1153         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1154         u8 cmd_buffer[4] = {0};
1155
1156         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1157         halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_BT_BLE_SCAN_PARA,
1158                                     cmd_buffer, 4, 200);
1159
1160         /* need wait completion to return correct value */
1161
1162         return btcoexist->bt_info.bt_ble_scan_para;
1163 }
1164
1165 static bool halbtc_get_bt_afh_map_from_bt(void *btc_context, u8 map_type,
1166                                           u8 *afh_map)
1167 {
1168         struct btc_coexist *btcoexist = (struct btc_coexist *)btc_context;
1169         u8 cmd_buffer[2] = {0};
1170         bool ret;
1171         u32 *afh_map_l = (u32 *)afh_map;
1172         u32 *afh_map_m = (u32 *)(afh_map + 4);
1173         u16 *afh_map_h = (u16 *)(afh_map + 8);
1174
1175         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1176         ret = halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_AFH_MAP_L,
1177                                           cmd_buffer, 2, 200);
1178         if (!ret)
1179                 goto exit;
1180
1181         *afh_map_l = btcoexist->bt_info.afh_map_l;
1182
1183         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1184         ret = halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_AFH_MAP_M,
1185                                           cmd_buffer, 2, 200);
1186         if (!ret)
1187                 goto exit;
1188
1189         *afh_map_m = btcoexist->bt_info.afh_map_m;
1190
1191         /* cmd_buffer[0] and [1] is filled by halbtc_send_bt_mp_operation() */
1192         ret = halbtc_send_bt_mp_operation(btcoexist, BT_OP_GET_AFH_MAP_H,
1193                                           cmd_buffer, 2, 200);
1194         if (!ret)
1195                 goto exit;
1196
1197         *afh_map_h = btcoexist->bt_info.afh_map_h;
1198
1199 exit:
1200         return ret;
1201 }
1202
1203 /*****************************************************************
1204  *         Extern functions called by other module
1205  *****************************************************************/
1206 bool exhalbtc_initlize_variables(struct rtl_priv *rtlpriv)
1207 {
1208         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
1209
1210         if (!btcoexist)
1211                 return false;
1212
1213         halbtc_dbg_init();
1214
1215         btcoexist->btc_read_1byte = halbtc_read_1byte;
1216         btcoexist->btc_write_1byte = halbtc_write_1byte;
1217         btcoexist->btc_write_1byte_bitmask = halbtc_bitmask_write_1byte;
1218         btcoexist->btc_read_2byte = halbtc_read_2byte;
1219         btcoexist->btc_write_2byte = halbtc_write_2byte;
1220         btcoexist->btc_read_4byte = halbtc_read_4byte;
1221         btcoexist->btc_write_4byte = halbtc_write_4byte;
1222         btcoexist->btc_write_local_reg_1byte = halbtc_write_local_reg_1byte;
1223
1224         btcoexist->btc_set_bb_reg = halbtc_set_bbreg;
1225         btcoexist->btc_get_bb_reg = halbtc_get_bbreg;
1226
1227         btcoexist->btc_set_rf_reg = halbtc_set_rfreg;
1228         btcoexist->btc_get_rf_reg = halbtc_get_rfreg;
1229
1230         btcoexist->btc_fill_h2c = halbtc_fill_h2c_cmd;
1231         btcoexist->btc_disp_dbg_msg = halbtc_display_dbg_msg;
1232
1233         btcoexist->btc_get = halbtc_get;
1234         btcoexist->btc_set = halbtc_set;
1235         btcoexist->btc_set_bt_reg = halbtc_set_bt_reg;
1236         btcoexist->btc_get_bt_reg = halbtc_get_bt_reg;
1237
1238         btcoexist->bt_info.bt_ctrl_buf_size = false;
1239         btcoexist->bt_info.agg_buf_size = 5;
1240
1241         btcoexist->bt_info.increase_scan_dev_num = false;
1242
1243         btcoexist->btc_get_bt_coex_supported_feature =
1244                                         halbtc_get_bt_coex_supported_feature;
1245         btcoexist->btc_get_bt_coex_supported_version =
1246                                         halbtc_get_bt_coex_supported_version;
1247         btcoexist->btc_get_bt_phydm_version = halbtc_get_phydm_version;
1248         btcoexist->btc_phydm_modify_ra_pcr_threshold =
1249                                         halbtc_phydm_modify_ra_pcr_threshold;
1250         btcoexist->btc_phydm_query_phy_counter = halbtc_phydm_query_phy_counter;
1251         btcoexist->btc_get_ant_det_val_from_bt = halbtc_get_ant_det_val_from_bt;
1252         btcoexist->btc_get_ble_scan_type_from_bt =
1253                                         halbtc_get_ble_scan_type_from_bt;
1254         btcoexist->btc_get_ble_scan_para_from_bt =
1255                                         halbtc_get_ble_scan_para_from_bt;
1256         btcoexist->btc_get_bt_afh_map_from_bt =
1257                                         halbtc_get_bt_afh_map_from_bt;
1258
1259         init_completion(&btcoexist->bt_mp_comp);
1260
1261         return true;
1262 }
1263
1264 bool exhalbtc_initlize_variables_wifi_only(struct rtl_priv *rtlpriv)
1265 {
1266         struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
1267         struct wifi_only_haldata *wifionly_haldata;
1268
1269         if (!wifionly_cfg)
1270                 return false;
1271
1272         wifionly_cfg->adapter = rtlpriv;
1273
1274         switch (rtlpriv->rtlhal.interface) {
1275         case INTF_PCI:
1276                 wifionly_cfg->chip_interface = BTC_INTF_PCI;
1277                 break;
1278         case INTF_USB:
1279                 wifionly_cfg->chip_interface = BTC_INTF_USB;
1280                 break;
1281         default:
1282                 wifionly_cfg->chip_interface = BTC_INTF_UNKNOWN;
1283                 break;
1284         }
1285
1286         wifionly_haldata = &wifionly_cfg->haldata_info;
1287
1288         wifionly_haldata->customer_id = CUSTOMER_NORMAL;
1289         wifionly_haldata->efuse_pg_antnum = rtl_get_hwpg_ant_num(rtlpriv);
1290         wifionly_haldata->efuse_pg_antpath =
1291                                         rtl_get_hwpg_single_ant_path(rtlpriv);
1292         wifionly_haldata->rfe_type = rtl_get_hwpg_rfe_type(rtlpriv);
1293         wifionly_haldata->ant_div_cfg = 0;
1294
1295         return true;
1296 }
1297
1298 bool exhalbtc_bind_bt_coex_withadapter(void *adapter)
1299 {
1300         struct rtl_priv *rtlpriv = adapter;
1301         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
1302         u8 ant_num = 2, chip_type, single_ant_path = 0;
1303
1304         if (!btcoexist)
1305                 return false;
1306
1307         if (btcoexist->binded)
1308                 return false;
1309
1310         switch (rtlpriv->rtlhal.interface) {
1311         case INTF_PCI:
1312                 btcoexist->chip_interface = BTC_INTF_PCI;
1313                 break;
1314         case INTF_USB:
1315                 btcoexist->chip_interface = BTC_INTF_USB;
1316                 break;
1317         default:
1318                 btcoexist->chip_interface = BTC_INTF_UNKNOWN;
1319                 break;
1320         }
1321
1322         btcoexist->binded = true;
1323         btcoexist->statistics.cnt_bind++;
1324
1325         btcoexist->adapter = adapter;
1326
1327         btcoexist->stack_info.profile_notified = false;
1328
1329         btcoexist->bt_info.bt_ctrl_agg_buf_size = false;
1330         btcoexist->bt_info.agg_buf_size = 5;
1331
1332         btcoexist->bt_info.increase_scan_dev_num = false;
1333         btcoexist->bt_info.miracast_plus_bt = false;
1334
1335         chip_type = rtl_get_hwpg_bt_type(rtlpriv);
1336         exhalbtc_set_chip_type(btcoexist, chip_type);
1337         ant_num = rtl_get_hwpg_ant_num(rtlpriv);
1338         exhalbtc_set_ant_num(rtlpriv, BT_COEX_ANT_TYPE_PG, ant_num);
1339
1340         /* set default antenna position to main  port */
1341         btcoexist->board_info.btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
1342
1343         single_ant_path = rtl_get_hwpg_single_ant_path(rtlpriv);
1344         exhalbtc_set_single_ant_path(btcoexist, single_ant_path);
1345
1346         if (rtl_get_hwpg_package_type(rtlpriv) == 0)
1347                 btcoexist->board_info.tfbga_package = false;
1348         else if (rtl_get_hwpg_package_type(rtlpriv) == 1)
1349                 btcoexist->board_info.tfbga_package = false;
1350         else
1351                 btcoexist->board_info.tfbga_package = true;
1352
1353         if (btcoexist->board_info.tfbga_package)
1354                 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1355                          "[BTCoex], Package Type = TFBGA\n");
1356         else
1357                 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
1358                          "[BTCoex], Package Type = Non-TFBGA\n");
1359
1360         btcoexist->board_info.rfe_type = rtl_get_hwpg_rfe_type(rtlpriv);
1361         btcoexist->board_info.ant_div_cfg = 0;
1362
1363         return true;
1364 }
1365
1366 void exhalbtc_power_on_setting(struct btc_coexist *btcoexist)
1367 {
1368         if (!halbtc_is_bt_coexist_available(btcoexist))
1369                 return;
1370
1371         btcoexist->statistics.cnt_power_on++;
1372
1373         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1374                 if (btcoexist->board_info.btdm_ant_num == 1)
1375                         ex_btc8822b1ant_power_on_setting(btcoexist);
1376                 else if (btcoexist->board_info.btdm_ant_num == 2)
1377                         ex_btc8822b2ant_power_on_setting(btcoexist);
1378         }
1379 }
1380
1381 void exhalbtc_pre_load_firmware(struct btc_coexist *btcoexist)
1382 {
1383         if (!halbtc_is_bt_coexist_available(btcoexist))
1384                 return;
1385
1386         btcoexist->statistics.cnt_pre_load_firmware++;
1387
1388         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1389                 if (btcoexist->board_info.btdm_ant_num == 1)
1390                         ex_btc8822b1ant_pre_load_firmware(btcoexist);
1391                 else if (btcoexist->board_info.btdm_ant_num == 2)
1392                         ex_btc8822b2ant_pre_load_firmware(btcoexist);
1393         }
1394 }
1395
1396 void exhalbtc_init_hw_config(struct btc_coexist *btcoexist, bool wifi_only)
1397 {
1398         if (!halbtc_is_bt_coexist_available(btcoexist))
1399                 return;
1400
1401         btcoexist->statistics.cnt_init_hw_config++;
1402
1403         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1404                 if (btcoexist->board_info.btdm_ant_num == 1)
1405                         ex_btc8822b1ant_init_hw_config(btcoexist, wifi_only);
1406                 else if (btcoexist->board_info.btdm_ant_num == 2)
1407                         ex_btc8822b2ant_init_hw_config(btcoexist, wifi_only);
1408
1409                 halbtc_set_default_port_id_cmd(btcoexist);
1410                 halbtc_send_wifi_port_id_cmd(btcoexist);
1411         }
1412 }
1413
1414 void exhalbtc_init_hw_config_wifi_only(struct wifi_only_cfg *wifionly_cfg)
1415 {
1416         if (IS_HARDWARE_TYPE_8822B(wifionly_cfg->adapter))
1417                 ex_hal8822b_wifi_only_hw_config(wifionly_cfg);
1418 }
1419
1420 void exhalbtc_init_coex_dm(struct btc_coexist *btcoexist)
1421 {
1422         if (!halbtc_is_bt_coexist_available(btcoexist))
1423                 return;
1424
1425         btcoexist->statistics.cnt_init_coex_dm++;
1426
1427         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1428                 if (btcoexist->board_info.btdm_ant_num == 1)
1429                         ex_btc8822b1ant_init_coex_dm(btcoexist);
1430                 else if (btcoexist->board_info.btdm_ant_num == 2)
1431                         ex_btc8822b2ant_init_coex_dm(btcoexist);
1432         }
1433
1434         btcoexist->initilized = true;
1435 }
1436
1437 void exhalbtc_ips_notify(struct btc_coexist *btcoexist, u8 type)
1438 {
1439         u8 ips_type;
1440
1441         if (!halbtc_is_bt_coexist_available(btcoexist))
1442                 return;
1443         btcoexist->statistics.cnt_ips_notify++;
1444         if (btcoexist->manual_control)
1445                 return;
1446
1447         if (type == ERFOFF)
1448                 ips_type = BTC_IPS_ENTER;
1449         else
1450                 ips_type = BTC_IPS_LEAVE;
1451
1452         halbtc_leave_low_power(btcoexist);
1453
1454         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1455                 if (btcoexist->board_info.btdm_ant_num == 1)
1456                         ex_btc8822b1ant_ips_notify(btcoexist, ips_type);
1457                 else if (btcoexist->board_info.btdm_ant_num == 2)
1458                         ex_btc8822b2ant_ips_notify(btcoexist, ips_type);
1459         }
1460
1461         halbtc_normal_low_power(btcoexist);
1462 }
1463
1464 void exhalbtc_lps_notify(struct btc_coexist *btcoexist, u8 type)
1465 {
1466         u8 lps_type;
1467
1468         if (!halbtc_is_bt_coexist_available(btcoexist))
1469                 return;
1470         btcoexist->statistics.cnt_lps_notify++;
1471         if (btcoexist->manual_control)
1472                 return;
1473
1474         if (type == EACTIVE)
1475                 lps_type = BTC_LPS_DISABLE;
1476         else
1477                 lps_type = BTC_LPS_ENABLE;
1478
1479         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1480                 if (btcoexist->board_info.btdm_ant_num == 1)
1481                         ex_btc8822b1ant_lps_notify(btcoexist, lps_type);
1482                 else if (btcoexist->board_info.btdm_ant_num == 2)
1483                         ex_btc8822b2ant_lps_notify(btcoexist, lps_type);
1484         }
1485 }
1486
1487 void exhalbtc_scan_notify(struct btc_coexist *btcoexist, u8 type)
1488 {
1489         u8 scan_type;
1490
1491         if (!halbtc_is_bt_coexist_available(btcoexist))
1492                 return;
1493         btcoexist->statistics.cnt_scan_notify++;
1494         if (btcoexist->manual_control)
1495                 return;
1496
1497         if (type)
1498                 scan_type = BTC_SCAN_START;
1499         else
1500                 scan_type = BTC_SCAN_FINISH;
1501
1502         halbtc_leave_low_power(btcoexist);
1503
1504         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1505                 if (btcoexist->board_info.btdm_ant_num == 1)
1506                         ex_btc8822b1ant_scan_notify(btcoexist, scan_type);
1507                 else if (btcoexist->board_info.btdm_ant_num == 2)
1508                         ex_btc8822b2ant_scan_notify(btcoexist, scan_type);
1509         }
1510
1511         halbtc_normal_low_power(btcoexist);
1512 }
1513
1514 void exhalbtc_scan_notify_wifi_only(struct wifi_only_cfg *wifionly_cfg,
1515                                     u8 is_5g)
1516 {
1517         if (IS_HARDWARE_TYPE_8822B(wifionly_cfg->adapter))
1518                 ex_hal8822b_wifi_only_scannotify(wifionly_cfg, is_5g);
1519 }
1520
1521 void exhalbtc_connect_notify(struct btc_coexist *btcoexist, u8 action)
1522 {
1523         u8 asso_type;
1524
1525         if (!halbtc_is_bt_coexist_available(btcoexist))
1526                 return;
1527         btcoexist->statistics.cnt_connect_notify++;
1528         if (btcoexist->manual_control)
1529                 return;
1530
1531         if (action)
1532                 asso_type = BTC_ASSOCIATE_START;
1533         else
1534                 asso_type = BTC_ASSOCIATE_FINISH;
1535
1536         halbtc_leave_low_power(btcoexist);
1537
1538         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1539                 if (btcoexist->board_info.btdm_ant_num == 1)
1540                         ex_btc8822b1ant_connect_notify(btcoexist, asso_type);
1541                 else if (btcoexist->board_info.btdm_ant_num == 2)
1542                         ex_btc8822b2ant_connect_notify(btcoexist, asso_type);
1543         }
1544
1545         halbtc_normal_low_power(btcoexist);
1546 }
1547
1548 void exhalbtc_mediastatus_notify(struct btc_coexist *btcoexist,
1549                                  enum rt_media_status media_status)
1550 {
1551         u8 status;
1552
1553         if (!halbtc_is_bt_coexist_available(btcoexist))
1554                 return;
1555         btcoexist->statistics.cnt_media_status_notify++;
1556         if (btcoexist->manual_control)
1557                 return;
1558
1559         if (media_status == RT_MEDIA_CONNECT)
1560                 status = BTC_MEDIA_CONNECT;
1561         else
1562                 status = BTC_MEDIA_DISCONNECT;
1563
1564         halbtc_leave_low_power(btcoexist);
1565
1566         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1567                 if (btcoexist->board_info.btdm_ant_num == 1)
1568                         ex_btc8822b1ant_media_status_notify(btcoexist, status);
1569                 else if (btcoexist->board_info.btdm_ant_num == 2)
1570                         ex_btc8822b2ant_media_status_notify(btcoexist, status);
1571         }
1572
1573         halbtc_normal_low_power(btcoexist);
1574 }
1575
1576 void exhalbtc_special_packet_notify(struct btc_coexist *btcoexist, u8 pkt_type)
1577 {
1578         u8 packet_type;
1579
1580         if (!halbtc_is_bt_coexist_available(btcoexist))
1581                 return;
1582         btcoexist->statistics.cnt_special_packet_notify++;
1583         if (btcoexist->manual_control)
1584                 return;
1585
1586         if (pkt_type == PACKET_DHCP) {
1587                 packet_type = BTC_PACKET_DHCP;
1588         } else if (pkt_type == PACKET_EAPOL) {
1589                 packet_type = BTC_PACKET_EAPOL;
1590         } else if (pkt_type == PACKET_ARP) {
1591                 packet_type = BTC_PACKET_ARP;
1592         } else {
1593                 packet_type = BTC_PACKET_UNKNOWN;
1594                 return;
1595         }
1596
1597         halbtc_leave_low_power(btcoexist);
1598
1599         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1600                 if (btcoexist->board_info.btdm_ant_num == 1)
1601                         ex_btc8822b1ant_specific_packet_notify(btcoexist,
1602                                                                packet_type);
1603                 else if (btcoexist->board_info.btdm_ant_num == 2)
1604                         ex_btc8822b2ant_specific_packet_notify(btcoexist,
1605                                                                packet_type);
1606         }
1607
1608         halbtc_normal_low_power(btcoexist);
1609 }
1610
1611 void exhalbtc_bt_info_notify(struct btc_coexist *btcoexist,
1612                              u8 *tmp_buf, u8 length)
1613 {
1614         if (!halbtc_is_bt_coexist_available(btcoexist))
1615                 return;
1616         btcoexist->statistics.cnt_bt_info_notify++;
1617
1618         halbtc_leave_low_power(btcoexist);
1619
1620         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1621                 if (btcoexist->board_info.btdm_ant_num == 1)
1622                         ex_btc8822b1ant_bt_info_notify(btcoexist, tmp_buf,
1623                                                        length);
1624                 else if (btcoexist->board_info.btdm_ant_num == 2)
1625                         ex_btc8822b2ant_bt_info_notify(btcoexist, tmp_buf,
1626                                                        length);
1627         }
1628
1629         halbtc_normal_low_power(btcoexist);
1630 }
1631
1632 void exhalbtc_rf_status_notify(struct btc_coexist *btcoexist, u8 type)
1633 {
1634         if (!halbtc_is_bt_coexist_available(btcoexist))
1635                 return;
1636
1637         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1638                 if (btcoexist->board_info.btdm_ant_num == 1)
1639                         ex_btc8822b1ant_rf_status_notify(btcoexist, type);
1640                 else if (btcoexist->board_info.btdm_ant_num == 2)
1641                         ex_btc8822b2ant_rf_status_notify(btcoexist, type);
1642         }
1643 }
1644
1645 void exhalbtc_stack_operation_notify(struct btc_coexist *btcoexist, u8 type)
1646 {
1647         u8 stack_op_type;
1648
1649         if (!halbtc_is_bt_coexist_available(btcoexist))
1650                 return;
1651         btcoexist->statistics.cnt_stack_operation_notify++;
1652         if (btcoexist->manual_control)
1653                 return;
1654
1655         if ((type == HCI_BT_OP_INQUIRY_START) ||
1656             (type == HCI_BT_OP_PAGING_START) ||
1657             (type == HCI_BT_OP_PAIRING_START)) {
1658                 stack_op_type = BTC_STACK_OP_INQ_PAGE_PAIR_START;
1659         } else if ((type == HCI_BT_OP_INQUIRY_FINISH) ||
1660                    (type == HCI_BT_OP_PAGING_SUCCESS) ||
1661                    (type == HCI_BT_OP_PAGING_UNSUCCESS) ||
1662                    (type == HCI_BT_OP_PAIRING_FINISH)) {
1663                 stack_op_type = BTC_STACK_OP_INQ_PAGE_PAIR_FINISH;
1664         } else {
1665                 stack_op_type = BTC_STACK_OP_NONE;
1666         }
1667 }
1668
1669 void exhalbtc_halt_notify(struct btc_coexist *btcoexist)
1670 {
1671         if (!halbtc_is_bt_coexist_available(btcoexist))
1672                 return;
1673
1674         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1675                 if (btcoexist->board_info.btdm_ant_num == 1)
1676                         ex_btc8822b1ant_halt_notify(btcoexist);
1677                 else if (btcoexist->board_info.btdm_ant_num == 2)
1678                         ex_btc8822b2ant_halt_notify(btcoexist);
1679         }
1680
1681         btcoexist->binded = false;
1682 }
1683
1684 void exhalbtc_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
1685 {
1686         if (!halbtc_is_bt_coexist_available(btcoexist))
1687                 return;
1688
1689         /* currently only 1ant we have to do the notification,
1690          * once pnp is notified to sleep state, we have to leave LPS that
1691          * we can sleep normally.
1692          */
1693
1694         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1695                 if (btcoexist->board_info.btdm_ant_num == 1)
1696                         ex_btc8822b1ant_pnp_notify(btcoexist, pnp_state);
1697                 else if (btcoexist->board_info.btdm_ant_num == 2)
1698                         ex_btc8822b2ant_pnp_notify(btcoexist, pnp_state);
1699         }
1700 }
1701
1702 void exhalbtc_coex_dm_switch(struct btc_coexist *btcoexist)
1703 {
1704         if (!halbtc_is_bt_coexist_available(btcoexist))
1705                 return;
1706         btcoexist->statistics.cnt_coex_dm_switch++;
1707
1708         halbtc_leave_low_power(btcoexist);
1709
1710         halbtc_normal_low_power(btcoexist);
1711 }
1712
1713 void exhalbtc_periodical(struct btc_coexist *btcoexist)
1714 {
1715         if (!halbtc_is_bt_coexist_available(btcoexist))
1716                 return;
1717         btcoexist->statistics.cnt_periodical++;
1718
1719         halbtc_leave_low_power(btcoexist);
1720
1721         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1722                 if (btcoexist->board_info.btdm_ant_num == 1)
1723                         ex_btc8822b1ant_periodical(btcoexist);
1724                 else if (btcoexist->board_info.btdm_ant_num == 2)
1725                         ex_btc8822b2ant_periodical(btcoexist);
1726         }
1727
1728         halbtc_normal_low_power(btcoexist);
1729 }
1730
1731 void exhalbtc_dbg_control(struct btc_coexist *btcoexist,
1732                           u8 code, u8 len, u8 *data)
1733 {
1734         if (!halbtc_is_bt_coexist_available(btcoexist))
1735                 return;
1736         btcoexist->statistics.cnt_dbg_ctrl++;
1737
1738         halbtc_leave_low_power(btcoexist);
1739
1740         halbtc_normal_low_power(btcoexist);
1741 }
1742
1743 void exhalbtc_antenna_detection(struct btc_coexist *btcoexist, u32 cent_freq,
1744                                 u32 offset, u32 span, u32 seconds)
1745 {
1746         if (!halbtc_is_bt_coexist_available(btcoexist))
1747                 return;
1748 }
1749
1750 void exhalbtc_stack_update_profile_info(void)
1751 {
1752 }
1753
1754 void exhalbtc_update_min_bt_rssi(struct btc_coexist *btcoexist, s8 bt_rssi)
1755 {
1756         if (!halbtc_is_bt_coexist_available(btcoexist))
1757                 return;
1758
1759         btcoexist->stack_info.min_bt_rssi = bt_rssi;
1760 }
1761
1762 void exhalbtc_set_hci_version(struct btc_coexist *btcoexist, u16 hci_version)
1763 {
1764         if (!halbtc_is_bt_coexist_available(btcoexist))
1765                 return;
1766
1767         btcoexist->stack_info.hci_version = hci_version;
1768 }
1769
1770 void exhalbtc_set_bt_patch_version(struct btc_coexist *btcoexist,
1771                                    u16 bt_hci_version, u16 bt_patch_version)
1772 {
1773         if (!halbtc_is_bt_coexist_available(btcoexist))
1774                 return;
1775
1776         btcoexist->bt_info.bt_real_fw_ver = bt_patch_version;
1777         btcoexist->bt_info.bt_hci_ver = bt_hci_version;
1778 }
1779
1780 void exhalbtc_set_chip_type(struct btc_coexist *btcoexist, u8 chip_type)
1781 {
1782         switch (chip_type) {
1783         default:
1784         case BT_2WIRE:
1785         case BT_ISSC_3WIRE:
1786         case BT_ACCEL:
1787         case BT_RTL8756:
1788                 btcoexist->board_info.bt_chip_type = BTC_CHIP_UNDEF;
1789                 break;
1790         case BT_CSR_BC4:
1791                 btcoexist->board_info.bt_chip_type = BTC_CHIP_CSR_BC4;
1792                 break;
1793         case BT_CSR_BC8:
1794                 btcoexist->board_info.bt_chip_type = BTC_CHIP_CSR_BC8;
1795                 break;
1796         case BT_RTL8723A:
1797                 btcoexist->board_info.bt_chip_type = BTC_CHIP_RTL8723A;
1798                 break;
1799         case BT_RTL8821A:
1800                 btcoexist->board_info.bt_chip_type = BTC_CHIP_RTL8821;
1801                 break;
1802         case BT_RTL8723B:
1803                 btcoexist->board_info.bt_chip_type = BTC_CHIP_RTL8723B;
1804                 break;
1805         }
1806 }
1807
1808 void exhalbtc_set_ant_num(struct rtl_priv *rtlpriv, u8 type, u8 ant_num)
1809 {
1810         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
1811
1812         if (!btcoexist)
1813                 return;
1814
1815         if (type == BT_COEX_ANT_TYPE_PG) {
1816                 btcoexist->board_info.pg_ant_num = ant_num;
1817                 btcoexist->board_info.btdm_ant_num = ant_num;
1818         } else if (type == BT_COEX_ANT_TYPE_ANTDIV) {
1819                 btcoexist->board_info.btdm_ant_num = ant_num;
1820         } else if (type == BT_COEX_ANT_TYPE_DETECTED) {
1821                 btcoexist->board_info.btdm_ant_num = ant_num;
1822                 if (rtlpriv->cfg->mod_params->ant_sel == 1)
1823                         btcoexist->board_info.btdm_ant_pos =
1824                                 BTC_ANTENNA_AT_AUX_PORT;
1825                 else
1826                         btcoexist->board_info.btdm_ant_pos =
1827                                 BTC_ANTENNA_AT_MAIN_PORT;
1828         }
1829 }
1830
1831 /* Currently used by 8723b only, S0 or S1 */
1832 void exhalbtc_set_single_ant_path(struct btc_coexist *btcoexist,
1833                                   u8 single_ant_path)
1834 {
1835         btcoexist->board_info.single_ant_path = single_ant_path;
1836 }
1837
1838 void exhalbtc_display_bt_coex_info(struct btc_coexist *btcoexist,
1839                                    struct seq_file *m)
1840 {
1841         if (!halbtc_is_bt_coexist_available(btcoexist))
1842                 return;
1843
1844         halbtc_leave_low_power(btcoexist);
1845
1846         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1847                 if (btcoexist->board_info.btdm_ant_num == 1)
1848                         ex_btc8822b1ant_display_coex_info(btcoexist, m);
1849                 else if (btcoexist->board_info.btdm_ant_num == 2)
1850                         ex_btc8822b2ant_display_coex_info(btcoexist, m);
1851         }
1852
1853         halbtc_normal_low_power(btcoexist);
1854 }
1855
1856 void exhalbtc_switch_band_notify(struct btc_coexist *btcoexist, u8 type)
1857 {
1858         if (!halbtc_is_bt_coexist_available(btcoexist))
1859                 return;
1860
1861         if (btcoexist->manual_control)
1862                 return;
1863
1864         halbtc_leave_low_power(btcoexist);
1865
1866         if (IS_HARDWARE_TYPE_8822B(btcoexist->adapter)) {
1867                 if (btcoexist->board_info.btdm_ant_num == 1)
1868                         ex_btc8822b1ant_switchband_notify(btcoexist, type);
1869                 else if (btcoexist->board_info.btdm_ant_num == 2)
1870                         ex_btc8822b2ant_switchband_notify(btcoexist, type);
1871         }
1872
1873         halbtc_normal_low_power(btcoexist);
1874 }
1875
1876 void exhalbtc_switch_band_notify_wifi_only(struct wifi_only_cfg *wifionly_cfg,
1877                                            u8 is_5g)
1878 {
1879         if (IS_HARDWARE_TYPE_8822B(wifionly_cfg->adapter))
1880                 ex_hal8822b_wifi_only_switchbandnotify(wifionly_cfg, is_5g);
1881 }