01f5a74028a0ad1c3630719ab5aa8b7a73f17806
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rtl8821ae / 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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20
21 #include "halbt_precomp.h"
22
23 /*#if(BT_30_SUPPORT == 1)*/
24 #if 1
25 /***********************************************
26  *              Global variables
27  ***********************************************/
28 static const char *const bt_profile_string[] = {
29         "NONE",
30         "A2DP",
31         "PAN",
32         "HID",
33         "SCO",
34 };
35
36 static const char *const bt_spec_string[] = {
37         "1.0b",
38         "1.1",
39         "1.2",
40         "2.0+EDR",
41         "2.1+EDR",
42         "3.0+HS",
43         "4.0",
44 };
45
46 static const char *const bt_link_role_string[] = {
47         "Master",
48         "Slave",
49 };
50
51 static const char *const h2c_state_string[] = {
52         "successful",
53         "h2c busy",
54         "rf off",
55         "fw not read",
56 };
57
58 static const char *const io_state_string[] = {
59         "IO_STATUS_SUCCESS",
60         "IO_STATUS_FAIL_CANNOT_IO",
61         "IO_STATUS_FAIL_RF_OFF",
62         "IO_STATUS_FAIL_FW_READ_CLEAR_TIMEOUT",
63         "IO_STATUS_FAIL_WAIT_IO_EVENT_TIMEOUT",
64         "IO_STATUS_INVALID_LEN",
65         "IO_STATUS_IO_IDLE_QUEUE_EMPTY",
66         "IO_STATUS_IO_INSERT_WAIT_QUEUE_FAIL",
67         "IO_STATUS_UNKNOWN_FAIL",
68         "IO_STATUS_WRONG_LEVEL",
69         "IO_STATUS_H2C_STOPPED",
70 };
71
72 struct btc_coexist gl_bt_coexist;
73
74 u32 btc_dbg_type[BTC_MSG_MAX];
75 static u8 btc_dbg_buf[100];
76
77 /***************************************************
78  *              Debug related function
79  ***************************************************/
80 static bool halbtc_is_bt_coexist_available(struct btc_coexist *btcoexist)
81 {
82         if (!btcoexist->binded || NULL == btcoexist->adapter)
83                 return false;
84         
85         return true;
86 }
87
88 static bool halbtc_is_wifi_busy(struct rtl_priv *rtlpriv)
89 {
90
91         if (rtlpriv->link_info.b_busytraffic)
92                 return true;
93         else
94                 return false;
95 }
96
97
98 static void halbtc_dbg_init(void)
99 {
100         u8 i;
101
102         for (i = 0; i < BTC_MSG_MAX; i++)
103                 btc_dbg_type[i] = 0;
104
105         btc_dbg_type[BTC_MSG_INTERFACE]                 =       \
106 //                      INTF_INIT                               |
107 //                      INTF_NOTIFY                             |
108                         0;
109         
110         btc_dbg_type[BTC_MSG_ALGORITHM]                 =       \
111 //                      ALGO_BT_RSSI_STATE                      |
112 //                      ALGO_WIFI_RSSI_STATE                    |
113 //                      ALGO_BT_MONITOR                         |
114 //                      ALGO_TRACE                              |
115 //                      ALGO_TRACE_FW                           |
116 //                      ALGO_TRACE_FW_DETAIL                    |
117 //                      ALGO_TRACE_FW_EXEC                      |
118 //                      ALGO_TRACE_SW                           |
119 //                      ALGO_TRACE_SW_DETAIL                    |
120 //                      ALGO_TRACE_SW_EXEC                      |
121                         0;
122 }
123
124 static bool halbtc_is_bt40(struct rtl_priv *adapter)
125 {
126         struct rtl_priv *rtlpriv = adapter;
127         struct rtl_phy *rtlphy = &(rtlpriv->phy);
128         bool is_ht40 = true;
129         enum ht_channel_width bw = rtlphy->current_chan_bw;
130         
131         
132         if (bw == HT_CHANNEL_WIDTH_20)
133                 is_ht40 = false;
134         else if (bw == HT_CHANNEL_WIDTH_20_40)
135                 is_ht40 = true;
136
137         return is_ht40;
138 }
139
140 static bool halbtc_legacy(struct rtl_priv *adapter)
141 {
142         struct rtl_priv *rtlpriv = adapter;
143         struct rtl_mac *mac = rtl_mac(rtlpriv);
144         
145         bool is_legacy = false;
146
147         if ((mac->mode == WIRELESS_MODE_B) || (mac->mode == WIRELESS_MODE_B))
148                 is_legacy = true;
149
150         return is_legacy;
151 }
152
153 bool halbtc_is_wifi_uplink(struct rtl_priv *adapter)
154 {
155         struct rtl_priv *rtlpriv = adapter;
156         
157         if (rtlpriv->link_info.b_tx_busy_traffic)
158                 return true;
159         else
160                 return false;
161 }
162
163 static u32 halbtc_get_wifi_bw(struct btc_coexist *btcoexist)
164 {
165         struct rtl_priv *rtlpriv = 
166                 (struct rtl_priv *)btcoexist->adapter;
167         u32 wifi_bw = BTC_WIFI_BW_HT20;
168         
169         if (halbtc_is_bt40(rtlpriv)){
170                 wifi_bw = BTC_WIFI_BW_HT40;     
171         } else {
172                 if(halbtc_legacy(rtlpriv))
173                         wifi_bw = BTC_WIFI_BW_LEGACY;
174                 else
175                         wifi_bw = BTC_WIFI_BW_HT20;
176         }
177         return wifi_bw;
178 }
179
180 static u8 halbtc_get_wifi_central_chnl(struct btc_coexist *btcoexist)
181 {
182         struct rtl_priv *rtlpriv = btcoexist->adapter;
183         struct rtl_phy  *rtlphy = &(rtlpriv->phy);
184         u8 chnl = 1;
185         
186
187         if (rtlphy->current_channel != 0)
188                 chnl = rtlphy->current_channel;
189         BTC_PRINT(BTC_MSG_ALGORITHM, ALGO_TRACE, 
190                   "halbtc_get_wifi_central_chnl:%d\n",chnl);
191         return chnl;
192 }
193
194 static void halbtc_leave_lps(struct btc_coexist *btcoexist)
195 {
196         struct rtl_priv *rtlpriv;
197         struct rtl_ps_ctl *ppsc;
198         bool ap_enable = false;
199         
200         rtlpriv = btcoexist->adapter;
201         ppsc = rtl_psc(rtlpriv);
202         
203         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
204                            &ap_enable);
205         
206         if (ap_enable) {
207                 printk("halbtc_leave_lps()<--dont leave lps under AP mode\n");
208                 return;
209         }
210
211         btcoexist->bt_info.bt_ctrl_lps = true;
212         btcoexist->bt_info.bt_lps_on = false;
213 }
214
215 static void halbtc_enter_lps(struct btc_coexist *btcoexist)
216 {
217         struct rtl_priv *rtlpriv;
218         struct rtl_ps_ctl *ppsc;
219         bool ap_enable = false;
220         
221         rtlpriv = btcoexist->adapter;
222         ppsc = rtl_psc(rtlpriv);
223         
224         btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE, 
225                            &ap_enable);
226         
227         if (ap_enable) {
228                 printk("halbtc_enter_lps()<--dont enter lps under AP mode\n");
229                 return;
230         }
231         
232         btcoexist->bt_info.bt_ctrl_lps = true;
233         btcoexist->bt_info.bt_lps_on = false;
234 }
235
236 static void halbtc_normal_lps(struct btc_coexist *btcoexist)
237 {
238         if (btcoexist->bt_info.bt_ctrl_lps) {
239                 btcoexist->bt_info.bt_lps_on = false;
240                 btcoexist->bt_info.bt_ctrl_lps = false;
241         }
242                 
243 }
244
245 static void halbtc_leave_low_power(void)
246 {
247 }
248
249 static void halbtc_nomal_low_power(void)
250 {
251 }
252
253 static void halbtc_disable_low_power(void)
254 {
255 }
256
257 static void halbtc_aggregation_check(void)
258 {
259 }
260
261
262 static u32 halbtc_get_bt_patch_version(struct btc_coexist *btcoexist)
263 {
264         return 0;
265 }
266
267 static s32 halbtc_get_wifi_rssi(struct rtl_priv *adapter)
268 {
269         struct rtl_priv *rtlpriv = adapter;
270         s32     undecorated_smoothed_pwdb = 0;
271         
272         if (rtlpriv->mac80211.link_state >= MAC80211_LINKED)
273                 undecorated_smoothed_pwdb = 
274                         rtlpriv->dm.undecorated_smoothed_pwdb;
275         else /* associated entry pwdb */
276                 undecorated_smoothed_pwdb =
277                         rtlpriv->dm.undecorated_smoothed_pwdb;
278         return undecorated_smoothed_pwdb;
279 }
280
281 static bool halbtc_get(void *void_btcoexist, u8 get_type, void *out_buf)
282 {
283         struct btc_coexist *btcoexist = (struct btc_coexist *)void_btcoexist;
284         struct rtl_priv *rtlpriv = btcoexist->adapter;
285         struct rtl_phy *rtlphy = &(rtlpriv->phy);
286         struct rtl_mac *mac = rtl_mac(rtlpriv);
287         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
288         bool *bool_tmp = (bool*)out_buf;
289         int *s32_tmp = (int*)out_buf;
290         u32 *u32_tmp = (u32*)out_buf;
291         u8 *u8_tmp = (u8*)out_buf;
292         bool tmp = false;
293
294
295         if (!halbtc_is_bt_coexist_available(btcoexist))
296                 return false;
297         
298
299         switch (get_type){
300         case BTC_GET_BL_HS_OPERATION:
301                 *bool_tmp = false;
302                 break;
303         case BTC_GET_BL_HS_CONNECTING:
304                 *bool_tmp = false;
305                 break;
306         case BTC_GET_BL_WIFI_CONNECTED:
307                 if (rtlpriv->mac80211.link_state >= MAC80211_LINKED)
308                         tmp = true;
309                                 
310                 *bool_tmp = tmp;
311                 break;
312         case BTC_GET_BL_WIFI_BUSY:
313                 if(halbtc_is_wifi_busy(rtlpriv))
314                         *bool_tmp = true;
315                 else
316                         *bool_tmp = false;
317                 break;
318         case BTC_GET_BL_WIFI_SCAN:
319                 if (mac->act_scanning == true)
320                         *bool_tmp = true;
321                 else
322                         *bool_tmp = false;
323                 break;
324         case BTC_GET_BL_WIFI_LINK:
325                 if (mac->link_state == MAC80211_LINKING)
326                         *bool_tmp = true;
327                 else
328                         *bool_tmp = false;
329                 break;
330         case BTC_GET_BL_WIFI_ROAM:      /*TODO*/
331                 if (mac->link_state == MAC80211_LINKING)
332                         *bool_tmp = true;
333                 else
334                         *bool_tmp = false;
335                 break;
336         case BTC_GET_BL_WIFI_4_WAY_PROGRESS:    /*TODO*/
337                         *bool_tmp = false;
338
339                 break;
340         case BTC_GET_BL_WIFI_UNDER_5G:
341                 *bool_tmp = false; /*TODO*/
342                 
343         case BTC_GET_BL_WIFI_DHCP:      /*TODO*/
344                 break;
345         case BTC_GET_BL_WIFI_SOFTAP_IDLE:
346                 *bool_tmp = true;
347                 break;
348         case BTC_GET_BL_WIFI_SOFTAP_LINKING:
349                 *bool_tmp = false;
350                 break;
351         case BTC_GET_BL_WIFI_IN_EARLY_SUSPEND:
352                 *bool_tmp = false;
353                 break;
354         case BTC_GET_BL_WIFI_AP_MODE_ENABLE:
355                 *bool_tmp = false;
356                 break;
357         case BTC_GET_BL_WIFI_ENABLE_ENCRYPTION:
358                 if (NO_ENCRYPTION == rtlpriv->sec.pairwise_enc_algorithm)
359                         *bool_tmp = false;
360                 else
361                         *bool_tmp = true;
362                 break;
363         case BTC_GET_BL_WIFI_UNDER_B_MODE:
364                 *bool_tmp = false; /*TODO*/
365                 break;
366         case BTC_GET_BL_EXT_SWITCH:
367                 *bool_tmp = false;
368                 break;
369         case BTC_GET_S4_WIFI_RSSI:
370                 *s32_tmp = halbtc_get_wifi_rssi(rtlpriv);
371                 break;
372         case BTC_GET_S4_HS_RSSI:        /*TODO*/
373                 *s32_tmp = halbtc_get_wifi_rssi(rtlpriv);
374                 break;
375         case BTC_GET_U4_WIFI_BW:
376                 *u32_tmp = halbtc_get_wifi_bw(btcoexist);
377                 break;
378         case BTC_GET_U4_WIFI_TRAFFIC_DIRECTION:
379                 if (halbtc_is_wifi_uplink(rtlpriv))
380                         *u32_tmp = BTC_WIFI_TRAFFIC_TX;
381                 else
382                         *u32_tmp = BTC_WIFI_TRAFFIC_RX;
383                 break;
384         case BTC_GET_U4_WIFI_FW_VER:
385                 *u32_tmp = rtlhal->fw_version;
386                 break;
387         case BTC_GET_U4_BT_PATCH_VER:
388                 *u32_tmp = halbtc_get_bt_patch_version(btcoexist);
389                 break;
390         case BTC_GET_U1_WIFI_DOT11_CHNL:
391                 *u8_tmp = rtlphy->current_channel;
392                 break;
393         case BTC_GET_U1_WIFI_CENTRAL_CHNL:
394                 *u8_tmp = halbtc_get_wifi_central_chnl(btcoexist);
395                 break;
396         case BTC_GET_U1_WIFI_HS_CHNL:
397                 *u8_tmp = 1;/* BT_OperateChnl(rtlpriv); */
398                 break;
399         case BTC_GET_U1_MAC_PHY_MODE:
400                 *u8_tmp = BTC_MP_UNKNOWN;
401                 break;
402         case BTC_GET_U1_AP_NUM:
403                 /* driver don't know AP num in Linux, 
404                  * So, the return value here is not right */
405                 *u8_tmp = 1;/* pDefMgntInfo->NumBssDesc4Query; */
406                 break;
407
408         /************* 1Ant **************/
409         case BTC_GET_U1_LPS_MODE:
410                 *u8_tmp = btcoexist->pwr_mode_val[0];
411                 break;
412                                         
413         default:
414                 break;
415         }
416         
417         return true;
418 }
419
420 static bool halbtc_set(void *void_btcoexist, u8 set_type, void *in_buf)
421 {
422         struct btc_coexist *btcoexist = (struct btc_coexist *)void_btcoexist;
423         bool *bool_tmp = (bool *)in_buf;
424         u8 *u8_tmp = (u8 *)in_buf;
425         u32 *u32_tmp = (u32 *)in_buf;
426         
427         
428         if (!halbtc_is_bt_coexist_available(btcoexist))
429                 return false;
430
431         switch (set_type) {
432         /* set some bool type variables. */
433         case BTC_SET_BL_BT_DISABLE:
434                 btcoexist->bt_info.bt_disabled = *bool_tmp;
435                 break;
436         case BTC_SET_BL_BT_TRAFFIC_BUSY:
437                 btcoexist->bt_info.bt_busy = *bool_tmp;
438                 break;
439         case BTC_SET_BL_BT_LIMITED_DIG:
440                 btcoexist->bt_info.limited_dig = *bool_tmp;
441                 break;
442         case BTC_SET_BL_FORCE_TO_ROAM:
443                 btcoexist->bt_info.force_to_roam = *bool_tmp;
444                 break;
445         case BTC_SET_BL_TO_REJ_AP_AGG_PKT:
446                 btcoexist->bt_info.reject_agg_pkt = *bool_tmp;
447                 break;
448         case BTC_SET_BL_BT_CTRL_AGG_SIZE:
449                 btcoexist->bt_info.b_bt_ctrl_buf_size = *bool_tmp;
450                 break;
451         case BTC_SET_BL_INC_SCAN_DEV_NUM:
452                 btcoexist->bt_info.increase_scan_dev_num = *bool_tmp;
453                 break;
454                 /* set some u1Byte type variables. */
455         case BTC_SET_U1_RSSI_ADJ_VAL_FOR_AGC_TABLE_ON:
456                 btcoexist->bt_info.rssi_adjust_for_agc_table_on = *u8_tmp;
457                 break;
458         case BTC_SET_U1_AGG_BUF_SIZE:
459                 btcoexist->bt_info.agg_buf_size = *u8_tmp;
460                 break;
461                 /* the following are some action which will be triggered */
462         case BTC_SET_ACT_GET_BT_RSSI:
463                 /*BTHCI_SendGetBtRssiEvent(rtlpriv);*/
464                 break;
465         case BTC_SET_ACT_AGGREGATE_CTRL:
466                 halbtc_aggregation_check();
467                 break;
468
469                 /* 1Ant */
470         case BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE:
471                 btcoexist->bt_info.rssi_adjust_for_1ant_coex_type = *u8_tmp;
472                 break;
473         case BTC_SET_UI_SCAN_SIG_COMPENSATION:
474         /*      rtlpriv->mlmepriv.scan_compensation = *u8_tmp;  */
475                 break;
476         case BTC_SET_U1_1ANT_LPS:
477                 btcoexist->bt_info.lps_1ant = *u8_tmp;
478                 break;
479         case BTC_SET_U1_1ANT_RPWM:
480                 btcoexist->bt_info.rpwm_1ant = *u8_tmp;
481                 break;
482         /* the following are some action which will be triggered  */
483         case BTC_SET_ACT_LEAVE_LPS:
484                 halbtc_leave_lps(btcoexist);
485                 break;
486         case BTC_SET_ACT_ENTER_LPS:
487                 halbtc_enter_lps(btcoexist);
488                 break;
489         case BTC_SET_ACT_NORMAL_LPS:
490                 halbtc_normal_lps(btcoexist);
491                 break;
492         case BTC_SET_ACT_DISABLE_LOW_POWER:
493                 halbtc_disable_low_power();
494                 break;
495         case BTC_SET_ACT_UPDATE_ra_mask:
496                 btcoexist->bt_info.ra_mask = *u32_tmp;
497                 break;
498         case BTC_SET_ACT_SEND_MIMO_PS:
499                 break;
500         case BTC_SET_ACT_INC_FORCE_EXEC_PWR_CMD_CNT:
501                 btcoexist->bt_info.force_exec_pwr_cmd_cnt++;
502                 break;
503         case BTC_SET_ACT_CTRL_BT_INFO: /*wait for 8812/8821*/
504                 break;
505         case BTC_SET_ACT_CTRL_BT_COEX:
506                 break;
507         default:
508                 break;
509         }
510         
511         return true;
512 }
513
514 static void halbtc_display_coex_statistics(struct btc_coexist *btcoexist)
515 {
516 }
517
518 static void halbtc_display_bt_link_info(struct btc_coexist *btcoexist)
519 {
520 }
521
522 static void halbtc_display_bt_fw_info(struct btc_coexist *btcoexist)
523 {
524 }
525
526 static void halbtc_display_fw_pwr_mode_cmd(struct btc_coexist *btcoexist)
527 {
528 }
529
530 /************************************************************
531  *              IO related function
532  ************************************************************/
533 static u8 halbtc_read_1byte(void *bt_context, u32 reg_addr)
534 {
535         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
536         struct rtl_priv *rtlpriv = btcoexist->adapter;
537         
538         return  rtl_read_byte(rtlpriv, reg_addr);
539 }
540
541
542 static u16 halbtc_read_2byte(void *bt_context, u32 reg_addr)
543 {
544         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
545         struct rtl_priv *rtlpriv = btcoexist->adapter;
546
547         return  rtl_read_word(rtlpriv, reg_addr);
548 }
549
550
551 static u32 halbtc_read_4byte(void *bt_context, u32 reg_addr)
552 {
553         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
554         struct rtl_priv *rtlpriv = btcoexist->adapter;
555         
556         return  rtl_read_dword(rtlpriv, reg_addr);
557 }
558
559
560 static void halbtc_write_1byte(void *bt_context, u32 reg_addr, u8 data)
561 {
562         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
563         struct rtl_priv *rtlpriv = btcoexist->adapter;
564
565         rtl_write_byte(rtlpriv, reg_addr, data);
566 }
567
568 static void halbtc_bitmask_write_1byte(void *bt_context, u32 reg_addr,
569                                 u8 bit_mask, u8 data)
570 {
571         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
572         struct rtl_priv *rtlpriv = btcoexist->adapter;
573         u8 original_value, bit_shift = 0;
574         u8 i;
575
576         if (bit_mask != MASKDWORD) {/*if not "double word" write*/
577                 original_value = rtl_read_byte(rtlpriv, reg_addr);      
578                 for (i=0; i<=7; i++) {
579                         if((bit_mask>>i)&0x1)
580                                 break;
581                 }
582                 bit_shift = i;
583                 data = (original_value & (~bit_mask)) | 
584                         ((data << bit_shift) & bit_mask);
585         }
586         rtl_write_byte(rtlpriv, reg_addr, data);
587 }
588
589
590 static void halbtc_write_2byte(void *bt_context, u32 reg_addr, u16 data)
591 {
592         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
593         struct rtl_priv *rtlpriv = btcoexist->adapter;
594
595         rtl_write_word(rtlpriv, reg_addr, data);
596 }
597
598
599 static void halbtc_write_4byte(void *bt_context, u32 reg_addr, u32 data)
600 {
601         struct btc_coexist *btcoexist = 
602                 (struct btc_coexist *)bt_context;
603         struct rtl_priv *rtlpriv = btcoexist->adapter;
604
605         rtl_write_dword(rtlpriv, reg_addr, data);
606 }
607
608 static void halbtc_set_bbreg(void *bt_context, u32 reg_addr,
609                                 u32 bit_mask, u32 data)
610 {
611         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
612         struct rtl_priv *rtlpriv = btcoexist->adapter;
613
614         rtl_set_bbreg(rtlpriv->mac80211.hw, reg_addr, bit_mask, data);
615 }
616
617
618 static u32 halbtc_get_bbreg(void *bt_context, u32 reg_addr, u32 bit_mask)
619 {
620         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
621         struct rtl_priv *rtlpriv = btcoexist->adapter;
622
623         return rtl_get_bbreg(rtlpriv->mac80211.hw,reg_addr, bit_mask);
624 }
625
626
627 static void halbtc_set_rfreg(void *bt_context, u8 rf_path, u32 reg_addr,
628                       u32 bit_mask, u32 data)
629 {
630         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
631         struct rtl_priv *rtlpriv = btcoexist->adapter;
632
633         rtl_set_rfreg(rtlpriv->mac80211.hw, rf_path, reg_addr, bit_mask, data);
634 }
635
636
637 static u32 halbtc_get_rfreg(void *bt_context, u8 rf_path,
638                         u32 reg_addr, u32 bit_mask)
639 {
640         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
641         struct rtl_priv *rtlpriv = btcoexist->adapter;
642
643         return rtl_get_rfreg(rtlpriv->mac80211.hw, rf_path, reg_addr, bit_mask);
644 }
645
646
647 static void halbtc_fill_h2c_cmd(void *bt_context, u8 element_id,
648                          u32 cmd_len, u8 *cmd_buf)
649 {
650         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
651         struct rtl_priv *rtlpriv = btcoexist->adapter;
652         
653         rtlpriv->cfg->ops->fill_h2c_cmd(rtlpriv->mac80211.hw, element_id, 
654                                         cmd_len, cmd_buf);
655 }
656
657 static void halbtc_display_dbg_msg(void *bt_context, u8 disp_type)
658 {
659         struct btc_coexist *btcoexist = (struct btc_coexist *)bt_context;
660         switch (disp_type) {
661         case BTC_DBG_DISP_COEX_STATISTICS:
662                 halbtc_display_coex_statistics(btcoexist);
663                 break;
664         case BTC_DBG_DISP_BT_LINK_INFO:
665                 halbtc_display_bt_link_info(btcoexist);
666                 break;
667         case BTC_DBG_DISP_BT_FW_VER:
668                 halbtc_display_bt_fw_info(btcoexist);
669                 break;
670         case BTC_DBG_DISP_FW_PWR_MODE_CMD:
671                 halbtc_display_fw_pwr_mode_cmd(btcoexist);
672                 break;
673         default:
674                 break;
675         }
676 }
677
678 /*****************************************************************
679  *         Extern functions called by other module
680  *****************************************************************/
681 bool exhalbtc_initlize_variables(struct rtl_priv *adapter)
682 {
683         struct btc_coexist *btcoexist = &gl_bt_coexist;
684
685         btcoexist->statistics.cnt_bind++;
686         
687         halbtc_dbg_init();
688
689         if (btcoexist->binded)
690                 return false;
691         else
692                 btcoexist->binded = true;
693
694         btcoexist->chip_interface = BTC_INTF_UNKNOWN;
695
696         if (NULL == btcoexist->adapter)
697                 btcoexist->adapter = adapter;
698
699         btcoexist->stack_info.profile_notified = false;
700
701         btcoexist->btc_read_1byte = halbtc_read_1byte;
702         btcoexist->btc_write_1byte = halbtc_write_1byte;
703         btcoexist->btc_write_1byte_bitmask = halbtc_bitmask_write_1byte;
704         btcoexist->btc_read_2byte = halbtc_read_2byte;
705         btcoexist->btc_write_2byte = halbtc_write_2byte;
706         btcoexist->btc_read_4byte = halbtc_read_4byte;
707         btcoexist->btc_write_4byte = halbtc_write_4byte;
708
709         btcoexist->btc_set_bb_reg = halbtc_set_bbreg;
710         btcoexist->btc_get_bb_reg = halbtc_get_bbreg;
711
712         btcoexist->btc_set_rf_reg = halbtc_set_rfreg;
713         btcoexist->btc_get_rf_reg = halbtc_get_rfreg;
714
715         btcoexist->btc_fill_h2c = halbtc_fill_h2c_cmd;
716         btcoexist->btc_disp_dbg_msg = halbtc_display_dbg_msg;
717         
718         btcoexist->btc_get = halbtc_get;
719         btcoexist->btc_set = halbtc_set;
720
721         btcoexist->cli_buf = &btc_dbg_buf[0];
722
723         btcoexist->bt_info.b_bt_ctrl_buf_size = false;
724         btcoexist->bt_info.agg_buf_size = 5;
725
726         btcoexist->bt_info.increase_scan_dev_num = false;
727         return true;
728 }
729
730 void exhalbtc_init_hw_config(struct btc_coexist *btcoexist)
731 {
732         struct rtl_priv *rtlpriv = btcoexist->adapter;
733         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
734         
735         if (!halbtc_is_bt_coexist_available(btcoexist))
736                 return;
737
738         btcoexist->statistics.cnt_init_hw_config++;
739
740         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
741                 if (btcoexist->board_info.btdm_ant_num == 2)
742                         ex_halbtc8723b2ant_init_hwconfig(btcoexist);
743                 else if(btcoexist->board_info.btdm_ant_num == 1)
744                         ex_halbtc8723b1ant_init_hwconfig(btcoexist);
745         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
746                 ex_halbtc8192e2ant_init_hwconfig(btcoexist);
747         }
748         
749 }
750
751 void exhalbtc_init_coex_dm(struct btc_coexist *btcoexist)
752 {
753         struct rtl_priv *rtlpriv = btcoexist->adapter;
754         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
755         
756         if (!halbtc_is_bt_coexist_available(btcoexist))
757                 return;
758
759         btcoexist->statistics.cnt_init_coex_dm++;
760
761         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
762                 if (btcoexist->board_info.btdm_ant_num == 2)
763                         ex_halbtc8723b2ant_init_coex_dm(btcoexist);             
764                 else if(btcoexist->board_info.btdm_ant_num == 1)
765                         ex_halbtc8723b1ant_init_coex_dm(btcoexist);
766         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
767                 ex_halbtc8192e2ant_init_coex_dm(btcoexist);
768         }
769         
770         btcoexist->initilized = true;
771 }
772
773 void exhalbtc_ips_notify(struct btc_coexist *btcoexist, u8 type)
774 {
775         struct rtl_priv *rtlpriv = btcoexist->adapter;
776         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
777         u8 ips_type;
778
779         if (!halbtc_is_bt_coexist_available(btcoexist))
780                 return;
781         btcoexist->statistics.cnt_ips_notify++;
782         if (btcoexist->manual_control)
783                 return;
784
785         if (ERFOFF == type)
786                 ips_type = BTC_IPS_ENTER;
787         else
788                 ips_type = BTC_IPS_LEAVE;
789
790         halbtc_leave_low_power();
791         
792         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
793                 if (btcoexist->board_info.btdm_ant_num == 2)
794                         ex_halbtc8723b2ant_ips_notify(btcoexist, ips_type);
795                 else if(btcoexist->board_info.btdm_ant_num == 1)
796                         ex_halbtc8723b1ant_ips_notify(btcoexist, ips_type);
797         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
798                 ex_halbtc8192e2ant_ips_notify(btcoexist, ips_type);
799         }
800
801         halbtc_nomal_low_power();
802 }
803
804 void exhalbtc_lps_notify(struct btc_coexist *btcoexist, u8 type)
805 {
806         struct rtl_priv *rtlpriv = btcoexist->adapter;
807         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
808         u8 lps_type;
809
810         if (!halbtc_is_bt_coexist_available(btcoexist))
811                 return;
812         btcoexist->statistics.cnt_lps_notify++;
813         if (btcoexist->manual_control)
814                 return;
815         
816         if (EACTIVE == type)
817                 lps_type = BTC_LPS_DISABLE;
818         else
819                 lps_type = BTC_LPS_ENABLE;
820
821         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
822                 if (btcoexist->board_info.btdm_ant_num == 2)
823                         ex_halbtc8723b2ant_lps_notify(btcoexist, lps_type);
824                 else if(btcoexist->board_info.btdm_ant_num == 1)
825                         ex_halbtc8723b1ant_lps_notify(btcoexist, lps_type);
826         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
827                 ex_halbtc8192e2ant_lps_notify(btcoexist, lps_type);
828         }
829 }
830
831 void exhalbtc_scan_notify(struct btc_coexist *btcoexist, u8 type)
832 {
833         struct rtl_priv *rtlpriv = btcoexist->adapter;
834         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
835         u8 scan_type;
836
837         if (!halbtc_is_bt_coexist_available(btcoexist))
838                 return;
839         btcoexist->statistics.cnt_scan_notify++;
840         if (btcoexist->manual_control)
841                 return;
842
843         if (type)
844                 scan_type = BTC_SCAN_START;
845         else
846                 scan_type = BTC_SCAN_FINISH;
847
848         halbtc_leave_low_power();
849         
850         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
851                 if (btcoexist->board_info.btdm_ant_num == 2)
852                         ex_halbtc8723b2ant_scan_notify(btcoexist, scan_type);
853                 else if(btcoexist->board_info.btdm_ant_num == 1)
854                         ex_halbtc8723b1ant_scan_notify(btcoexist, scan_type);
855         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
856                 ex_halbtc8192e2ant_scan_notify(btcoexist, scan_type);
857         }
858
859         halbtc_nomal_low_power();
860 }
861
862 void exhalbtc_connect_notify(struct btc_coexist *btcoexist, u8 action)
863 {
864         struct rtl_priv *rtlpriv = btcoexist->adapter;
865         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
866         u8 asso_type;
867
868         if (!halbtc_is_bt_coexist_available(btcoexist))
869                 return;
870         btcoexist->statistics.cnt_connect_notify++;
871         if (btcoexist->manual_control)
872                 return;
873
874         if (action)
875                 asso_type = BTC_ASSOCIATE_START;
876         else
877                 asso_type = BTC_ASSOCIATE_FINISH;
878
879         halbtc_leave_low_power();
880         
881         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
882                 if (btcoexist->board_info.btdm_ant_num == 2)
883                         ex_halbtc8723b2ant_connect_notify(btcoexist, asso_type);
884                 else if(btcoexist->board_info.btdm_ant_num == 1)
885                         ex_halbtc8723b1ant_connect_notify(btcoexist, asso_type);
886         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
887                 ex_halbtc8192e2ant_connect_notify(btcoexist, asso_type);
888         }
889
890         halbtc_nomal_low_power();
891 }
892
893 void exhalbtc_mediastatus_notify(struct btc_coexist *btcoexist, 
894                                  enum rt_media_status media_status)
895 {
896         struct rtl_priv *rtlpriv = btcoexist->adapter;
897         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
898         u8 status;
899
900         if (!halbtc_is_bt_coexist_available(btcoexist))
901                 return;
902         btcoexist->statistics.cnt_media_status_notify++;
903         if (btcoexist->manual_control)
904                 return;
905
906         if (RT_MEDIA_CONNECT == media_status)
907                 status = BTC_MEDIA_CONNECT;
908         else
909                 status = BTC_MEDIA_DISCONNECT;
910
911         halbtc_leave_low_power();
912         
913         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
914                 if (btcoexist->board_info.btdm_ant_num == 2)
915                         ex_halbtc8723b2ant_media_status_notify(btcoexist, status);
916                 else if(btcoexist->board_info.btdm_ant_num == 1)
917                         ex_halbtc8723b1ant_media_status_notify(btcoexist, status);
918         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
919                 ex_halbtc8192e2ant_media_status_notify(btcoexist, status);
920         }
921
922         halbtc_nomal_low_power();
923 }
924
925 void exhalbtc_special_packet_notify(struct btc_coexist *btcoexist, u8 pkt_type)
926 {
927         u8 packet_type;
928
929         if (!halbtc_is_bt_coexist_available(btcoexist))
930                 return;
931         btcoexist->statistics.cnt_special_packet_notify++;
932         if (btcoexist->manual_control)
933                 return;
934
935         /*if(PACKET_DHCP == pkt_type)*/
936                 packet_type = BTC_PACKET_DHCP;
937         /*else if(PACKET_EAPOL == pkt_type)
938                 packet_type = BTC_PACKET_EAPOL;
939         else
940                 packet_type = BTC_PACKET_UNKNOWN;*/
941
942         halbtc_leave_low_power();
943
944         if (btcoexist->board_info.btdm_ant_num == 2)
945                 ex_halbtc8723b2ant_special_packet_notify(btcoexist,
946                                                          packet_type);
947         else if (btcoexist->board_info.btdm_ant_num == 1)
948                 ex_halbtc8723b1ant_special_packet_notify(btcoexist,
949                                                          packet_type);
950
951         halbtc_nomal_low_power();
952 }
953
954 void exhalbtc_bt_info_notify(struct btc_coexist *btcoexist, 
955                              u8 *tmp_buf, u8 length)
956 {
957         struct rtl_priv *rtlpriv = btcoexist->adapter;
958         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
959         if (!halbtc_is_bt_coexist_available(btcoexist))
960                 return;
961         btcoexist->statistics.cnt_bt_info_notify++;
962
963         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
964                 if (btcoexist->board_info.btdm_ant_num == 2)
965                         ex_halbtc8723b2ant_bt_info_notify(btcoexist, tmp_buf, length);
966                 else if(btcoexist->board_info.btdm_ant_num == 1)
967                         ex_halbtc8723b1ant_bt_info_notify(btcoexist, tmp_buf, length);
968         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
969         //      ex_halbtc8192e2ant_bt_info_notify(btcoexist, tmp_buf, length);
970         }
971 }
972
973 void exhalbtc_stack_operation_notify(struct btc_coexist *btcoexist, u8 type)
974 {
975         u8 stack_op_type;
976
977         if (!halbtc_is_bt_coexist_available(btcoexist))
978                 return;
979         btcoexist->statistics.cnt_stack_operation_notify++;
980         if (btcoexist->manual_control)
981                 return;
982
983         stack_op_type = BTC_STACK_OP_NONE;
984 }
985
986 void exhalbtc_halt_notify(struct btc_coexist *btcoexist)
987 {
988         struct rtl_priv *rtlpriv = btcoexist->adapter;
989         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
990         if (!halbtc_is_bt_coexist_available(btcoexist))
991                 return;
992
993         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
994                 if (btcoexist->board_info.btdm_ant_num == 2)
995                         ex_halbtc8723b2ant_halt_notify(btcoexist);
996                 else if(btcoexist->board_info.btdm_ant_num == 1)
997                         ex_halbtc8723b1ant_halt_notify(btcoexist);
998         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
999                 ex_halbtc8192e2ant_halt_notify(btcoexist);
1000         }
1001 }
1002
1003 void exhalbtc_pnp_notify(struct btc_coexist *btcoexist, u8 pnp_state)
1004 {
1005         if (!halbtc_is_bt_coexist_available(btcoexist))
1006                 return;
1007 }
1008
1009 void exhalbtc_periodical(struct btc_coexist *btcoexist)
1010 {
1011         struct rtl_priv *rtlpriv = btcoexist->adapter;
1012         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1013         if (!halbtc_is_bt_coexist_available(btcoexist))
1014                 return;
1015         btcoexist->statistics.cnt_periodical++;
1016
1017         halbtc_leave_low_power();
1018
1019         if (rtlhal->hw_type == HARDWARE_TYPE_RTL8723BE) {
1020                 if (btcoexist->board_info.btdm_ant_num == 2)
1021                         ex_halbtc8723b2ant_periodical(btcoexist);
1022                 else if(btcoexist->board_info.btdm_ant_num == 1)
1023                         ex_halbtc8723b1ant_periodical(btcoexist);
1024         } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8192EE) {
1025                 ex_halbtc8192e2ant_periodical(btcoexist);
1026         }
1027
1028         halbtc_nomal_low_power();
1029 }
1030
1031 void exhalbtc_dbg_control(struct btc_coexist *btcoexist, 
1032                           u8 code, u8 len, u8 *data)
1033 {
1034         if (!halbtc_is_bt_coexist_available(btcoexist))
1035                 return;
1036         btcoexist->statistics.cnt_dbg_ctrl++;
1037 }
1038
1039 void exhalbtc_stack_update_profile_info(void)
1040 {
1041 }
1042
1043 void exhalbtc_update_min_bt_rssi(char bt_rssi)
1044 {
1045         struct btc_coexist *btcoexist = &gl_bt_coexist;
1046
1047         if (!halbtc_is_bt_coexist_available(btcoexist))
1048                 return;
1049
1050         btcoexist->stack_info.min_bt_rssi = bt_rssi;
1051 }
1052
1053
1054 void exhalbtc_set_hci_version(u16 hci_version)
1055 {
1056         struct btc_coexist *btcoexist = &gl_bt_coexist;
1057
1058         if (!halbtc_is_bt_coexist_available(btcoexist))
1059                 return;
1060
1061         btcoexist->stack_info.hci_version = hci_version;
1062 }
1063
1064 void exhalbtc_set_bt_patch_version(u16 bt_hci_version, u16 bt_patch_version) 
1065 {
1066         struct btc_coexist *btcoexist = &gl_bt_coexist;
1067
1068         if (!halbtc_is_bt_coexist_available(btcoexist))
1069                 return;
1070
1071         btcoexist->bt_info.bt_real_fw_ver = bt_patch_version;
1072         btcoexist->bt_info.bt_hci_ver = bt_hci_version;
1073 }
1074
1075 void exhalbtc_set_bt_exist(bool bt_exist)
1076 {
1077         gl_bt_coexist.board_info.bt_exist = bt_exist;
1078 }
1079
1080 void exhalbtc_set_chip_type(u8 chip_type)
1081 {
1082         switch (chip_type) {
1083         default:
1084         case BT_2WIRE:
1085         case BT_ISSC_3WIRE:
1086         case BT_ACCEL:
1087         case BT_RTL8756:
1088                 gl_bt_coexist.board_info.bt_chip_type = BTC_CHIP_UNDEF;
1089                 break;
1090         case BT_CSR_BC4:
1091                 gl_bt_coexist.board_info.bt_chip_type = BTC_CHIP_CSR_BC4;
1092                 break;
1093         case BT_CSR_BC8:
1094                 gl_bt_coexist.board_info.bt_chip_type = BTC_CHIP_CSR_BC8;
1095                 break;
1096         case BT_RTL8723A:
1097                 gl_bt_coexist.board_info.bt_chip_type = BTC_CHIP_RTL8723A;
1098                 break;
1099         case BT_RTL8821A:
1100                 gl_bt_coexist.board_info.bt_chip_type = BTC_CHIP_RTL8821;
1101                 break;
1102         case BT_RTL8723B:
1103                 gl_bt_coexist.board_info.bt_chip_type = BTC_CHIP_RTL8723B;
1104                 break;
1105         }
1106 }
1107
1108 void exhalbtc_set_ant_num(u8 type, u8 ant_num)
1109 {
1110         if (BT_COEX_ANT_TYPE_PG == type) {
1111                 gl_bt_coexist.board_info.pg_ant_num = ant_num;
1112                 gl_bt_coexist.board_info.btdm_ant_num = ant_num;
1113         } else if (BT_COEX_ANT_TYPE_ANTDIV == type) {
1114                 gl_bt_coexist.board_info.btdm_ant_num = ant_num;
1115         }
1116 }
1117
1118 void exhalbtc_display_bt_coex_info(struct btc_coexist *btcoexist)
1119 {
1120         if (!halbtc_is_bt_coexist_available(btcoexist))
1121                 return;
1122         
1123         if (btcoexist->board_info.btdm_ant_num == 2)
1124                 ex_halbtc8723b2ant_display_coex_info(btcoexist);
1125         else if (btcoexist->board_info.btdm_ant_num == 1)
1126                 ex_halbtc8723b1ant_display_coex_info(btcoexist);
1127 }
1128
1129 #endif
1130