GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / net / wireless / realtek / 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                 /* In some situation, it doesn't scan after leaving IPS, and
246                  * this will cause btcoex in wrong state.
247                  */
248                 exhalbtc_scan_notify(btcoexist, 1);
249                 exhalbtc_scan_notify(btcoexist, 0);
250         }
251 }
252
253 void rtl_btc_lps_notify(struct rtl_priv *rtlpriv, u8 type)
254 {
255         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
256
257         if (!btcoexist)
258                 return;
259
260         exhalbtc_lps_notify(btcoexist, type);
261 }
262
263 void rtl_btc_scan_notify(struct rtl_priv *rtlpriv, u8 scantype)
264 {
265         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
266
267         if (!btcoexist)
268                 return;
269
270         exhalbtc_scan_notify(btcoexist, scantype);
271 }
272
273 void rtl_btc_scan_notify_wifi_only(struct rtl_priv *rtlpriv, u8 scantype)
274 {
275         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
276         struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
277         u8 is_5g = (rtlhal->current_bandtype == BAND_ON_5G);
278
279         if (!wifionly_cfg)
280                 return;
281
282         exhalbtc_scan_notify_wifi_only(wifionly_cfg, is_5g);
283 }
284
285 void rtl_btc_connect_notify(struct rtl_priv *rtlpriv, u8 action)
286 {
287         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
288
289         if (!btcoexist)
290                 return;
291
292         exhalbtc_connect_notify(btcoexist, action);
293 }
294
295 void rtl_btc_mediastatus_notify(struct rtl_priv *rtlpriv,
296                                 enum rt_media_status mstatus)
297 {
298         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
299
300         if (!btcoexist)
301                 return;
302
303         exhalbtc_mediastatus_notify(btcoexist, mstatus);
304 }
305
306 void rtl_btc_periodical(struct rtl_priv *rtlpriv)
307 {
308         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
309
310         if (!btcoexist)
311                 return;
312
313         /*rtl_bt_dm_monitor();*/
314         exhalbtc_periodical(btcoexist);
315 }
316
317 void rtl_btc_halt_notify(struct rtl_priv *rtlpriv)
318 {
319         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
320
321         if (!btcoexist)
322                 return;
323
324         exhalbtc_halt_notify(btcoexist);
325 }
326
327 void rtl_btc_btinfo_notify(struct rtl_priv *rtlpriv, u8 *tmp_buf, u8 length)
328 {
329         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
330
331         if (!btcoexist)
332                 return;
333
334         exhalbtc_bt_info_notify(btcoexist, tmp_buf, length);
335 }
336
337 void rtl_btc_btmpinfo_notify(struct rtl_priv *rtlpriv, u8 *tmp_buf, u8 length)
338 {
339         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
340         u8 extid, seq, len;
341         u16 bt_real_fw_ver;
342         u8 bt_fw_ver;
343         u8 *data;
344
345         if (!btcoexist)
346                 return;
347
348         if ((length < 4) || (!tmp_buf))
349                 return;
350
351         extid = tmp_buf[0];
352         /* not response from BT FW then exit*/
353         if (extid != 1) /* C2H_TRIG_BY_BT_FW = 1 */
354                 return;
355
356         len = tmp_buf[1] >> 4;
357         seq = tmp_buf[2] >> 4;
358         data = &tmp_buf[3];
359
360         /* BT Firmware version response */
361         switch (seq) {
362         case BT_SEQ_GET_BT_VERSION:
363                 bt_real_fw_ver = tmp_buf[3] | (tmp_buf[4] << 8);
364                 bt_fw_ver = tmp_buf[5];
365
366                 btcoexist->bt_info.bt_real_fw_ver = bt_real_fw_ver;
367                 btcoexist->bt_info.bt_fw_ver = bt_fw_ver;
368                 break;
369         case BT_SEQ_GET_AFH_MAP_L:
370                 btcoexist->bt_info.afh_map_l = le32_to_cpu(*(__le32 *)data);
371                 break;
372         case BT_SEQ_GET_AFH_MAP_M:
373                 btcoexist->bt_info.afh_map_m = le32_to_cpu(*(__le32 *)data);
374                 break;
375         case BT_SEQ_GET_AFH_MAP_H:
376                 btcoexist->bt_info.afh_map_h = le16_to_cpu(*(__le16 *)data);
377                 break;
378         case BT_SEQ_GET_BT_COEX_SUPPORTED_FEATURE:
379                 btcoexist->bt_info.bt_supported_feature = tmp_buf[3] |
380                                                           (tmp_buf[4] << 8);
381                 break;
382         case BT_SEQ_GET_BT_COEX_SUPPORTED_VERSION:
383                 btcoexist->bt_info.bt_supported_version = tmp_buf[3] |
384                                                           (tmp_buf[4] << 8);
385                 break;
386         case BT_SEQ_GET_BT_ANT_DET_VAL:
387                 btcoexist->bt_info.bt_ant_det_val = tmp_buf[3];
388                 break;
389         case BT_SEQ_GET_BT_BLE_SCAN_PARA:
390                 btcoexist->bt_info.bt_ble_scan_para = tmp_buf[3] |
391                                                       (tmp_buf[4] << 8) |
392                                                       (tmp_buf[5] << 16) |
393                                                       (tmp_buf[6] << 24);
394                 break;
395         case BT_SEQ_GET_BT_BLE_SCAN_TYPE:
396                 btcoexist->bt_info.bt_ble_scan_type = tmp_buf[3];
397                 break;
398         case BT_SEQ_GET_BT_DEVICE_INFO:
399                 btcoexist->bt_info.bt_device_info =
400                                                 le32_to_cpu(*(__le32 *)data);
401                 break;
402         case BT_OP_GET_BT_FORBIDDEN_SLOT_VAL:
403                 btcoexist->bt_info.bt_forb_slot_val =
404                                                 le32_to_cpu(*(__le32 *)data);
405                 break;
406         }
407
408         RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
409                  "btmpinfo complete req_num=%d\n", seq);
410
411         complete(&btcoexist->bt_mp_comp);
412 }
413
414 bool rtl_btc_is_limited_dig(struct rtl_priv *rtlpriv)
415 {
416         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
417
418         if (!btcoexist)
419                 return false;
420
421         return btcoexist->bt_info.limited_dig;
422 }
423
424 bool rtl_btc_is_disable_edca_turbo(struct rtl_priv *rtlpriv)
425 {
426         bool bt_change_edca = false;
427         u32 cur_edca_val;
428         u32 edca_bt_hs_uplink = 0x5ea42b, edca_bt_hs_downlink = 0x5ea42b;
429         u32 edca_hs;
430         u32 edca_addr = 0x504;
431
432         cur_edca_val = rtl_read_dword(rtlpriv, edca_addr);
433         if (halbtc_is_wifi_uplink(rtlpriv)) {
434                 if (cur_edca_val != edca_bt_hs_uplink) {
435                         edca_hs = edca_bt_hs_uplink;
436                         bt_change_edca = true;
437                 }
438         } else {
439                 if (cur_edca_val != edca_bt_hs_downlink) {
440                         edca_hs = edca_bt_hs_downlink;
441                         bt_change_edca = true;
442                 }
443         }
444
445         if (bt_change_edca)
446                 rtl_write_dword(rtlpriv, edca_addr, edca_hs);
447
448         return true;
449 }
450
451 bool rtl_btc_is_bt_disabled(struct rtl_priv *rtlpriv)
452 {
453         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
454
455         if (!btcoexist)
456                 return true;
457
458         /* It seems 'bt_disabled' is never be initialized or set. */
459         if (btcoexist->bt_info.bt_disabled)
460                 return true;
461         else
462                 return false;
463 }
464
465 void rtl_btc_special_packet_notify(struct rtl_priv *rtlpriv, u8 pkt_type)
466 {
467         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
468
469         if (!btcoexist)
470                 return;
471
472         return exhalbtc_special_packet_notify(btcoexist, pkt_type);
473 }
474
475 void rtl_btc_switch_band_notify(struct rtl_priv *rtlpriv, u8 band_type,
476                                 bool scanning)
477 {
478         struct btc_coexist *btcoexist = rtl_btc_coexist(rtlpriv);
479         u8 type = BTC_NOT_SWITCH;
480
481         if (!btcoexist)
482                 return;
483
484         switch (band_type) {
485         case BAND_ON_2_4G:
486                 if (scanning)
487                         type = BTC_SWITCH_TO_24G;
488                 else
489                         type = BTC_SWITCH_TO_24G_NOFORSCAN;
490                 break;
491
492         case BAND_ON_5G:
493                 type = BTC_SWITCH_TO_5G;
494                 break;
495         }
496
497         if (type != BTC_NOT_SWITCH)
498                 exhalbtc_switch_band_notify(btcoexist, type);
499 }
500
501 void rtl_btc_switch_band_notify_wifionly(struct rtl_priv *rtlpriv, u8 band_type,
502                                          bool scanning)
503 {
504         struct wifi_only_cfg *wifionly_cfg = rtl_btc_wifi_only(rtlpriv);
505         u8 is_5g = (band_type == BAND_ON_5G);
506
507         if (!wifionly_cfg)
508                 return;
509
510         exhalbtc_switch_band_notify_wifi_only(wifionly_cfg, is_5g);
511 }
512
513 struct rtl_btc_ops *rtl_btc_get_ops_pointer(void)
514 {
515         return &rtl_btc_operation;
516 }
517 EXPORT_SYMBOL(rtl_btc_get_ops_pointer);
518
519
520 enum rt_media_status mgnt_link_status_query(struct ieee80211_hw *hw)
521 {
522         struct rtl_priv *rtlpriv = rtl_priv(hw);
523         struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
524         enum rt_media_status    m_status = RT_MEDIA_DISCONNECT;
525
526         u8 bibss = (mac->opmode == NL80211_IFTYPE_ADHOC) ? 1 : 0;
527
528         if (bibss || rtlpriv->mac80211.link_state >= MAC80211_LINKED)
529                 m_status = RT_MEDIA_CONNECT;
530
531         return m_status;
532 }
533
534 u8 rtl_get_hwpg_bt_exist(struct rtl_priv *rtlpriv)
535 {
536         return rtlpriv->btcoexist.btc_info.btcoexist;
537 }
538
539 MODULE_AUTHOR("Page He  <page_he@realsil.com.cn>");
540 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
541 MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
542 MODULE_LICENSE("GPL");
543 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
544
545 static int __init rtl_btcoexist_module_init(void)
546 {
547         return 0;
548 }
549
550 static void __exit rtl_btcoexist_module_exit(void)
551 {
552         return;
553 }
554
555 module_init(rtl_btcoexist_module_init);
556 module_exit(rtl_btcoexist_module_exit);