1 /******************************************************************************
3 * Copyright(c) 2009-2013 Realtek Corporation.
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.
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
14 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
22 * Larry Finger <Larry.Finger@lwfinger.net>
24 *****************************************************************************/
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
30 #include "halbt_precomp.h"
32 static struct rtl_btc_ops rtl_btc_operation = {
33 .btc_init_variables = rtl_btc_init_variables,
34 .btc_init_variables_wifi_only = rtl_btc_init_variables_wifi_only,
35 .btc_deinit_variables = rtl_btc_deinit_variables,
36 .btc_init_hal_vars = rtl_btc_init_hal_vars,
37 .btc_power_on_setting = rtl_btc_power_on_setting,
38 .btc_init_hw_config = rtl_btc_init_hw_config,
39 .btc_init_hw_config_wifi_only = rtl_btc_init_hw_config_wifi_only,
40 .btc_ips_notify = rtl_btc_ips_notify,
41 .btc_lps_notify = rtl_btc_lps_notify,
42 .btc_scan_notify = rtl_btc_scan_notify,
43 .btc_scan_notify_wifi_only = rtl_btc_scan_notify_wifi_only,
44 .btc_connect_notify = rtl_btc_connect_notify,
45 .btc_mediastatus_notify = rtl_btc_mediastatus_notify,
46 .btc_periodical = rtl_btc_periodical,
47 .btc_halt_notify = rtl_btc_halt_notify,
48 .btc_btinfo_notify = rtl_btc_btinfo_notify,
49 .btc_btmpinfo_notify = rtl_btc_btmpinfo_notify,
50 .btc_is_limited_dig = rtl_btc_is_limited_dig,
51 .btc_is_disable_edca_turbo = rtl_btc_is_disable_edca_turbo,
52 .btc_is_bt_disabled = rtl_btc_is_bt_disabled,
53 .btc_special_packet_notify = rtl_btc_special_packet_notify,
54 .btc_switch_band_notify = rtl_btc_switch_band_notify,
55 .btc_switch_band_notify_wifi_only = rtl_btc_switch_band_notify_wifionly,
56 .btc_record_pwr_mode = rtl_btc_record_pwr_mode,
57 .btc_get_lps_val = rtl_btc_get_lps_val,
58 .btc_get_rpwm_val = rtl_btc_get_rpwm_val,
59 .btc_is_bt_ctrl_lps = rtl_btc_is_bt_ctrl_lps,
60 .btc_is_bt_lps_on = rtl_btc_is_bt_lps_on,
61 .btc_get_ampdu_cfg = rtl_btc_get_ampdu_cfg,
62 .btc_display_bt_coex_info = rtl_btc_display_bt_coex_info,
65 void rtl_btc_display_bt_coex_info(struct rtl_priv *rtlpriv, struct seq_file *m)
67 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
70 seq_puts(m, "btc_coexist context is NULL!\n");
74 exhalbtc_display_bt_coex_info(btcoexist, m);
77 void rtl_btc_record_pwr_mode(struct rtl_priv *rtlpriv, u8 *buf, u8 len)
79 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
85 safe_len = sizeof(btcoexist->pwr_mode_val);
90 memcpy(btcoexist->pwr_mode_val, buf, safe_len);
93 u8 rtl_btc_get_lps_val(struct rtl_priv *rtlpriv)
95 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
100 return btcoexist->bt_info.lps_val;
103 u8 rtl_btc_get_rpwm_val(struct rtl_priv *rtlpriv)
105 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
110 return btcoexist->bt_info.rpwm_val;
113 bool rtl_btc_is_bt_ctrl_lps(struct rtl_priv *rtlpriv)
115 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
120 return btcoexist->bt_info.bt_ctrl_lps;
123 bool rtl_btc_is_bt_lps_on(struct rtl_priv *rtlpriv)
125 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
130 return btcoexist->bt_info.bt_lps_on;
133 void rtl_btc_get_ampdu_cfg(struct rtl_priv *rtlpriv, u8 *reject_agg,
134 u8 *ctrl_agg_size, u8 *agg_size)
136 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
140 *ctrl_agg_size = false;
145 *reject_agg = btcoexist->bt_info.reject_agg_pkt;
147 *ctrl_agg_size = btcoexist->bt_info.bt_ctrl_agg_buf_size;
149 *agg_size = btcoexist->bt_info.agg_buf_size;
152 static void rtl_btc_alloc_variable(struct rtl_priv *rtlpriv, bool wifi_only)
155 rtlpriv->btcoexist.wifi_only_context =
156 kzalloc(sizeof(struct wifi_only_cfg), GFP_KERNEL);
158 rtlpriv->btcoexist.btc_context =
159 kzalloc(sizeof(struct btc_coexist), GFP_KERNEL);
162 static void rtl_btc_free_variable(struct rtl_priv *rtlpriv)
164 kfree(rtlpriv->btcoexist.btc_context);
165 rtlpriv->btcoexist.btc_context = NULL;
167 kfree(rtlpriv->btcoexist.wifi_only_context);
168 rtlpriv->btcoexist.wifi_only_context = NULL;
171 void rtl_btc_init_variables(struct rtl_priv *rtlpriv)
173 rtl_btc_alloc_variable(rtlpriv, false);
175 exhalbtc_initlize_variables(rtlpriv);
176 exhalbtc_bind_bt_coex_withadapter(rtlpriv);
179 void rtl_btc_init_variables_wifi_only(struct rtl_priv *rtlpriv)
181 rtl_btc_alloc_variable(rtlpriv, true);
183 exhalbtc_initlize_variables_wifi_only(rtlpriv);
186 void rtl_btc_deinit_variables(struct rtl_priv *rtlpriv)
188 rtl_btc_free_variable(rtlpriv);
191 void rtl_btc_init_hal_vars(struct rtl_priv *rtlpriv)
193 /* move ant_num, bt_type and single_ant_path to
194 * exhalbtc_bind_bt_coex_withadapter()
198 void rtl_btc_power_on_setting(struct rtl_priv *rtlpriv)
200 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
205 exhalbtc_power_on_setting(btcoexist);
208 void rtl_btc_init_hw_config(struct rtl_priv *rtlpriv)
210 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
214 bt_exist = rtl_get_hwpg_bt_exist(rtlpriv);
215 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
216 "%s, bt_exist is %d\n", __func__, bt_exist);
221 exhalbtc_init_hw_config(btcoexist, !bt_exist);
222 exhalbtc_init_coex_dm(btcoexist);
225 void rtl_btc_init_hw_config_wifi_only(struct rtl_priv *rtlpriv)
227 struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
232 exhalbtc_init_hw_config_wifi_only(wifionly_cfg);
235 void rtl_btc_ips_notify(struct rtl_priv *rtlpriv, u8 type)
237 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
242 exhalbtc_ips_notify(btcoexist, type);
246 * In some situation, it doesn't scan after leaving IPS, and
247 * this will cause btcoex in wrong state.
249 exhalbtc_scan_notify(btcoexist, 1);
250 exhalbtc_scan_notify(btcoexist, 0);
254 void rtl_btc_lps_notify(struct rtl_priv *rtlpriv, u8 type)
256 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
261 exhalbtc_lps_notify(btcoexist, type);
264 void rtl_btc_scan_notify(struct rtl_priv *rtlpriv, u8 scantype)
266 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
271 exhalbtc_scan_notify(btcoexist, scantype);
274 void rtl_btc_scan_notify_wifi_only(struct rtl_priv *rtlpriv, u8 scantype)
276 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
277 struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
278 u8 is_5g = (rtlhal->current_bandtype == BAND_ON_5G);
283 exhalbtc_scan_notify_wifi_only(wifionly_cfg, is_5g);
286 void rtl_btc_connect_notify(struct rtl_priv *rtlpriv, u8 action)
288 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
293 exhalbtc_connect_notify(btcoexist, action);
296 void rtl_btc_mediastatus_notify(struct rtl_priv *rtlpriv,
297 enum rt_media_status mstatus)
299 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
304 exhalbtc_mediastatus_notify(btcoexist, mstatus);
307 void rtl_btc_periodical(struct rtl_priv *rtlpriv)
309 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
314 /*rtl_bt_dm_monitor();*/
315 exhalbtc_periodical(btcoexist);
318 void rtl_btc_halt_notify(struct rtl_priv *rtlpriv)
320 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
325 exhalbtc_halt_notify(btcoexist);
328 void rtl_btc_btinfo_notify(struct rtl_priv *rtlpriv, u8 *tmp_buf, u8 length)
330 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
335 exhalbtc_bt_info_notify(btcoexist, tmp_buf, length);
338 void rtl_btc_btmpinfo_notify(struct rtl_priv *rtlpriv, u8 *tmp_buf, u8 length)
340 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
349 if ((length < 4) || (!tmp_buf))
353 /* not response from BT FW then exit*/
354 if (extid != 1) /* C2H_TRIG_BY_BT_FW = 1 */
357 len = tmp_buf[1] >> 4;
358 seq = tmp_buf[2] >> 4;
361 /* BT Firmware version response */
363 case BT_SEQ_GET_BT_VERSION:
364 bt_real_fw_ver = tmp_buf[3] | (tmp_buf[4] << 8);
365 bt_fw_ver = tmp_buf[5];
367 btcoexist->bt_info.bt_real_fw_ver = bt_real_fw_ver;
368 btcoexist->bt_info.bt_fw_ver = bt_fw_ver;
370 case BT_SEQ_GET_AFH_MAP_L:
371 btcoexist->bt_info.afh_map_l = le32_to_cpu(*(__le32 *)data);
373 case BT_SEQ_GET_AFH_MAP_M:
374 btcoexist->bt_info.afh_map_m = le32_to_cpu(*(__le32 *)data);
376 case BT_SEQ_GET_AFH_MAP_H:
377 btcoexist->bt_info.afh_map_h = le16_to_cpu(*(__le16 *)data);
379 case BT_SEQ_GET_BT_COEX_SUPPORTED_FEATURE:
380 btcoexist->bt_info.bt_supported_feature = tmp_buf[3] |
383 case BT_SEQ_GET_BT_COEX_SUPPORTED_VERSION:
384 btcoexist->bt_info.bt_supported_version = tmp_buf[3] |
387 case BT_SEQ_GET_BT_ANT_DET_VAL:
388 btcoexist->bt_info.bt_ant_det_val = tmp_buf[3];
390 case BT_SEQ_GET_BT_BLE_SCAN_PARA:
391 btcoexist->bt_info.bt_ble_scan_para = tmp_buf[3] |
396 case BT_SEQ_GET_BT_BLE_SCAN_TYPE:
397 btcoexist->bt_info.bt_ble_scan_type = tmp_buf[3];
401 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
402 "btmpinfo complete req_num=%d\n", seq);
404 complete(&btcoexist->bt_mp_comp);
407 bool rtl_btc_is_limited_dig(struct rtl_priv *rtlpriv)
409 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
414 return btcoexist->bt_info.limited_dig;
417 bool rtl_btc_is_disable_edca_turbo(struct rtl_priv *rtlpriv)
419 bool bt_change_edca = false;
421 u32 edca_bt_hs_uplink = 0x5ea42b, edca_bt_hs_downlink = 0x5ea42b;
423 u32 edca_addr = 0x504;
425 cur_edca_val = rtl_read_dword(rtlpriv, edca_addr);
426 if (halbtc_is_wifi_uplink(rtlpriv)) {
427 if (cur_edca_val != edca_bt_hs_uplink) {
428 edca_hs = edca_bt_hs_uplink;
429 bt_change_edca = true;
432 if (cur_edca_val != edca_bt_hs_downlink) {
433 edca_hs = edca_bt_hs_downlink;
434 bt_change_edca = true;
439 rtl_write_dword(rtlpriv, edca_addr, edca_hs);
444 bool rtl_btc_is_bt_disabled(struct rtl_priv *rtlpriv)
446 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
451 /* It seems 'bt_disabled' is never be initialized or set. */
452 if (btcoexist->bt_info.bt_disabled)
458 void rtl_btc_special_packet_notify(struct rtl_priv *rtlpriv, u8 pkt_type)
460 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
465 return exhalbtc_special_packet_notify(btcoexist, pkt_type);
468 void rtl_btc_switch_band_notify(struct rtl_priv *rtlpriv, u8 band_type,
471 struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
472 u8 type = BTC_NOT_SWITCH;
480 type = BTC_SWITCH_TO_24G;
482 type = BTC_SWITCH_TO_24G_NOFORSCAN;
486 type = BTC_SWITCH_TO_5G;
490 if (type != BTC_NOT_SWITCH)
491 exhalbtc_switch_band_notify(btcoexist, type);
494 void rtl_btc_switch_band_notify_wifionly(struct rtl_priv *rtlpriv, u8 band_type,
497 struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
498 u8 is_5g = (band_type == BAND_ON_5G);
503 exhalbtc_switch_band_notify_wifi_only(wifionly_cfg, is_5g);
506 struct rtl_btc_ops *rtl_btc_get_ops_pointer(void)
508 return &rtl_btc_operation;
511 enum rt_media_status mgnt_link_status_query(struct ieee80211_hw *hw)
513 struct rtl_priv *rtlpriv = rtl_priv(hw);
514 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
515 enum rt_media_status m_status = RT_MEDIA_DISCONNECT;
517 u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
519 if (bibss || rtlpriv->mac80211.link_state >= MAC80211_LINKED)
520 m_status = RT_MEDIA_CONNECT;
525 u8 rtl_get_hwpg_bt_exist(struct rtl_priv *rtlpriv)
527 return rtlpriv->btcoexist.btc_info.btcoexist;