GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / rtlwifi / btcoexist / rtl_btc.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2013  Realtek Corporation.
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 #include "../wifi.h"
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28
29 #include "rtl_btc.h"
30 #include "halbt_precomp.h"
31
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,
63 };
64
65 void rtl_btc_display_bt_coex_info(struct rtl_priv *rtlpriv, struct seq_file *m)
66 {
67         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
68
69         if (!btcoexist) {
70                 seq_puts(m, "btc_coexist context is NULL!\n");
71                 return;
72         }
73
74         exhalbtc_display_bt_coex_info(btcoexist, m);
75 }
76
77 void rtl_btc_record_pwr_mode(struct rtl_priv *rtlpriv, u8 *buf, u8 len)
78 {
79         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
80         u8 safe_len;
81
82         if (!btcoexist)
83                 return;
84
85         safe_len = sizeof(btcoexist->pwr_mode_val);
86
87         if (safe_len > len)
88                 safe_len = len;
89
90         memcpy(btcoexist->pwr_mode_val, buf, safe_len);
91 }
92
93 u8 rtl_btc_get_lps_val(struct rtl_priv *rtlpriv)
94 {
95         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
96
97         if (!btcoexist)
98                 return 0;
99
100         return btcoexist->bt_info.lps_val;
101 }
102
103 u8 rtl_btc_get_rpwm_val(struct rtl_priv *rtlpriv)
104 {
105         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
106
107         if (!btcoexist)
108                 return 0;
109
110         return btcoexist->bt_info.rpwm_val;
111 }
112
113 bool rtl_btc_is_bt_ctrl_lps(struct rtl_priv *rtlpriv)
114 {
115         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
116
117         if (!btcoexist)
118                 return false;
119
120         return btcoexist->bt_info.bt_ctrl_lps;
121 }
122
123 bool rtl_btc_is_bt_lps_on(struct rtl_priv *rtlpriv)
124 {
125         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
126
127         if (!btcoexist)
128                 return false;
129
130         return btcoexist->bt_info.bt_lps_on;
131 }
132
133 void rtl_btc_get_ampdu_cfg(struct rtl_priv *rtlpriv, u8 *reject_agg,
134                            u8 *ctrl_agg_size, u8 *agg_size)
135 {
136         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
137
138         if (!btcoexist) {
139                 *reject_agg = false;
140                 *ctrl_agg_size = false;
141                 return;
142         }
143
144         if (reject_agg)
145                 *reject_agg = btcoexist->bt_info.reject_agg_pkt;
146         if (ctrl_agg_size)
147                 *ctrl_agg_size = btcoexist->bt_info.bt_ctrl_agg_buf_size;
148         if (agg_size)
149                 *agg_size = btcoexist->bt_info.agg_buf_size;
150 }
151
152 static void rtl_btc_alloc_variable(struct rtl_priv *rtlpriv, bool wifi_only)
153 {
154         if (wifi_only)
155                 rtlpriv->btcoexist.wifi_only_context =
156                         kzalloc(sizeof(struct wifi_only_cfg), GFP_KERNEL);
157         else
158                 rtlpriv->btcoexist.btc_context =
159                         kzalloc(sizeof(struct btc_coexist), GFP_KERNEL);
160 }
161
162 static void rtl_btc_free_variable(struct rtl_priv *rtlpriv)
163 {
164         kfree(rtlpriv->btcoexist.btc_context);
165         rtlpriv->btcoexist.btc_context = NULL;
166
167         kfree(rtlpriv->btcoexist.wifi_only_context);
168         rtlpriv->btcoexist.wifi_only_context = NULL;
169 }
170
171 void rtl_btc_init_variables(struct rtl_priv *rtlpriv)
172 {
173         rtl_btc_alloc_variable(rtlpriv, false);
174
175         exhalbtc_initlize_variables(rtlpriv);
176         exhalbtc_bind_bt_coex_withadapter(rtlpriv);
177 }
178
179 void rtl_btc_init_variables_wifi_only(struct rtl_priv *rtlpriv)
180 {
181         rtl_btc_alloc_variable(rtlpriv, true);
182
183         exhalbtc_initlize_variables_wifi_only(rtlpriv);
184 }
185
186 void rtl_btc_deinit_variables(struct rtl_priv *rtlpriv)
187 {
188         rtl_btc_free_variable(rtlpriv);
189 }
190
191 void rtl_btc_init_hal_vars(struct rtl_priv *rtlpriv)
192 {
193         /* move ant_num, bt_type and single_ant_path to
194          * exhalbtc_bind_bt_coex_withadapter()
195          */
196 }
197
198 void rtl_btc_power_on_setting(struct rtl_priv *rtlpriv)
199 {
200         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
201
202         if (!btcoexist)
203                 return;
204
205         exhalbtc_power_on_setting(btcoexist);
206 }
207
208 void rtl_btc_init_hw_config(struct rtl_priv *rtlpriv)
209 {
210         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
211
212         u8 bt_exist;
213
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);
217
218         if (!btcoexist)
219                 return;
220
221         exhalbtc_init_hw_config(btcoexist, !bt_exist);
222         exhalbtc_init_coex_dm(btcoexist);
223 }
224
225 void rtl_btc_init_hw_config_wifi_only(struct rtl_priv *rtlpriv)
226 {
227         struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
228
229         if (!wifionly_cfg)
230                 return;
231
232         exhalbtc_init_hw_config_wifi_only(wifionly_cfg);
233 }
234
235 void rtl_btc_ips_notify(struct rtl_priv *rtlpriv, u8 type)
236 {
237         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
238
239         if (!btcoexist)
240                 return;
241
242         exhalbtc_ips_notify(btcoexist, type);
243
244         if (type == ERFON) {
245                 /*
246                  * In some situation, it doesn't scan after leaving IPS, and
247                  * this will cause btcoex in wrong state.
248                  */
249                 exhalbtc_scan_notify(btcoexist, 1);
250                 exhalbtc_scan_notify(btcoexist, 0);
251         }
252 }
253
254 void rtl_btc_lps_notify(struct rtl_priv *rtlpriv, u8 type)
255 {
256         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
257
258         if (!btcoexist)
259                 return;
260
261         exhalbtc_lps_notify(btcoexist, type);
262 }
263
264 void rtl_btc_scan_notify(struct rtl_priv *rtlpriv, u8 scantype)
265 {
266         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
267
268         if (!btcoexist)
269                 return;
270
271         exhalbtc_scan_notify(btcoexist, scantype);
272 }
273
274 void rtl_btc_scan_notify_wifi_only(struct rtl_priv *rtlpriv, u8 scantype)
275 {
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);
279
280         if (!wifionly_cfg)
281                 return;
282
283         exhalbtc_scan_notify_wifi_only(wifionly_cfg, is_5g);
284 }
285
286 void rtl_btc_connect_notify(struct rtl_priv *rtlpriv, u8 action)
287 {
288         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
289
290         if (!btcoexist)
291                 return;
292
293         exhalbtc_connect_notify(btcoexist, action);
294 }
295
296 void rtl_btc_mediastatus_notify(struct rtl_priv *rtlpriv,
297                                 enum rt_media_status mstatus)
298 {
299         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
300
301         if (!btcoexist)
302                 return;
303
304         exhalbtc_mediastatus_notify(btcoexist, mstatus);
305 }
306
307 void rtl_btc_periodical(struct rtl_priv *rtlpriv)
308 {
309         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
310
311         if (!btcoexist)
312                 return;
313
314         /*rtl_bt_dm_monitor();*/
315         exhalbtc_periodical(btcoexist);
316 }
317
318 void rtl_btc_halt_notify(struct rtl_priv *rtlpriv)
319 {
320         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
321
322         if (!btcoexist)
323                 return;
324
325         exhalbtc_halt_notify(btcoexist);
326 }
327
328 void rtl_btc_btinfo_notify(struct rtl_priv *rtlpriv, u8 *tmp_buf, u8 length)
329 {
330         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
331
332         if (!btcoexist)
333                 return;
334
335         exhalbtc_bt_info_notify(btcoexist, tmp_buf, length);
336 }
337
338 void rtl_btc_btmpinfo_notify(struct rtl_priv *rtlpriv, u8 *tmp_buf, u8 length)
339 {
340         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
341         u8 extid, seq, len;
342         u16 bt_real_fw_ver;
343         u8 bt_fw_ver;
344         u8 *data;
345
346         if (!btcoexist)
347                 return;
348
349         if ((length < 4) || (!tmp_buf))
350                 return;
351
352         extid = tmp_buf[0];
353         /* not response from BT FW then exit*/
354         if (extid != 1) /* C2H_TRIG_BY_BT_FW = 1 */
355                 return;
356
357         len = tmp_buf[1] >> 4;
358         seq = tmp_buf[2] >> 4;
359         data = &tmp_buf[3];
360
361         /* BT Firmware version response */
362         switch (seq) {
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];
366
367                 btcoexist->bt_info.bt_real_fw_ver = bt_real_fw_ver;
368                 btcoexist->bt_info.bt_fw_ver = bt_fw_ver;
369                 break;
370         case BT_SEQ_GET_AFH_MAP_L:
371                 btcoexist->bt_info.afh_map_l = le32_to_cpu(*(__le32 *)data);
372                 break;
373         case BT_SEQ_GET_AFH_MAP_M:
374                 btcoexist->bt_info.afh_map_m = le32_to_cpu(*(__le32 *)data);
375                 break;
376         case BT_SEQ_GET_AFH_MAP_H:
377                 btcoexist->bt_info.afh_map_h = le16_to_cpu(*(__le16 *)data);
378                 break;
379         case BT_SEQ_GET_BT_COEX_SUPPORTED_FEATURE:
380                 btcoexist->bt_info.bt_supported_feature = tmp_buf[3] |
381                                                           (tmp_buf[4] << 8);
382                 break;
383         case BT_SEQ_GET_BT_COEX_SUPPORTED_VERSION:
384                 btcoexist->bt_info.bt_supported_version = tmp_buf[3] |
385                                                           (tmp_buf[4] << 8);
386                 break;
387         case BT_SEQ_GET_BT_ANT_DET_VAL:
388                 btcoexist->bt_info.bt_ant_det_val = tmp_buf[3];
389                 break;
390         case BT_SEQ_GET_BT_BLE_SCAN_PARA:
391                 btcoexist->bt_info.bt_ble_scan_para = tmp_buf[3] |
392                                                       (tmp_buf[4] << 8) |
393                                                       (tmp_buf[5] << 16) |
394                                                       (tmp_buf[6] << 24);
395                 break;
396         case BT_SEQ_GET_BT_BLE_SCAN_TYPE:
397                 btcoexist->bt_info.bt_ble_scan_type = tmp_buf[3];
398                 break;
399         }
400
401         RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
402                  "btmpinfo complete req_num=%d\n", seq);
403
404         complete(&btcoexist->bt_mp_comp);
405 }
406
407 bool rtl_btc_is_limited_dig(struct rtl_priv *rtlpriv)
408 {
409         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
410
411         if (!btcoexist)
412                 return false;
413
414         return btcoexist->bt_info.limited_dig;
415 }
416
417 bool rtl_btc_is_disable_edca_turbo(struct rtl_priv *rtlpriv)
418 {
419         bool bt_change_edca = false;
420         u32 cur_edca_val;
421         u32 edca_bt_hs_uplink = 0x5ea42b, edca_bt_hs_downlink = 0x5ea42b;
422         u32 edca_hs;
423         u32 edca_addr = 0x504;
424
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;
430                 }
431         } else {
432                 if (cur_edca_val != edca_bt_hs_downlink) {
433                         edca_hs = edca_bt_hs_downlink;
434                         bt_change_edca = true;
435                 }
436         }
437
438         if (bt_change_edca)
439                 rtl_write_dword(rtlpriv, edca_addr, edca_hs);
440
441         return true;
442 }
443
444 bool rtl_btc_is_bt_disabled(struct rtl_priv *rtlpriv)
445 {
446         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
447
448         if (!btcoexist)
449                 return true;
450
451         /* It seems 'bt_disabled' is never be initialized or set. */
452         if (btcoexist->bt_info.bt_disabled)
453                 return true;
454         else
455                 return false;
456 }
457
458 void rtl_btc_special_packet_notify(struct rtl_priv *rtlpriv, u8 pkt_type)
459 {
460         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
461
462         if (!btcoexist)
463                 return;
464
465         return exhalbtc_special_packet_notify(btcoexist, pkt_type);
466 }
467
468 void rtl_btc_switch_band_notify(struct rtl_priv *rtlpriv, u8 band_type,
469                                 bool scanning)
470 {
471         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
472         u8 type = BTC_NOT_SWITCH;
473
474         if (!btcoexist)
475                 return;
476
477         switch (band_type) {
478         case BAND_ON_2_4G:
479                 if (scanning)
480                         type = BTC_SWITCH_TO_24G;
481                 else
482                         type = BTC_SWITCH_TO_24G_NOFORSCAN;
483                 break;
484
485         case BAND_ON_5G:
486                 type = BTC_SWITCH_TO_5G;
487                 break;
488         }
489
490         if (type != BTC_NOT_SWITCH)
491                 exhalbtc_switch_band_notify(btcoexist, type);
492 }
493
494 void rtl_btc_switch_band_notify_wifionly(struct rtl_priv *rtlpriv, u8 band_type,
495                                          bool scanning)
496 {
497         struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
498         u8 is_5g = (band_type == BAND_ON_5G);
499
500         if (!wifionly_cfg)
501                 return;
502
503         exhalbtc_switch_band_notify_wifi_only(wifionly_cfg, is_5g);
504 }
505
506 struct rtl_btc_ops *rtl_btc_get_ops_pointer(void)
507 {
508         return &rtl_btc_operation;
509 }
510
511 enum rt_media_status mgnt_link_status_query(struct ieee80211_hw *hw)
512 {
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;
516
517         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
518
519         if (bibss || rtlpriv->mac80211.link_state >= MAC80211_LINKED)
520                 m_status = RT_MEDIA_CONNECT;
521
522         return m_status;
523 }
524
525 u8 rtl_get_hwpg_bt_exist(struct rtl_priv *rtlpriv)
526 {
527         return rtlpriv->btcoexist.btc_info.btcoexist;
528 }