OSDN Git Service

Add rtl8821ce driver version 5.5.2
[android-x86/external-kernel-drivers.git] / rtl8821ce / hal / hal_dm_acs.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2014 - 2017 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  *****************************************************************************/
15 #include <drv_types.h>
16 #include <hal_data.h>
17
18
19 #if defined(CONFIG_RTW_ACS) || defined(CONFIG_BACKGROUND_NOISE_MONITOR)
20 static void _rtw_bss_nums_count(_adapter *adapter, u8 *pbss_nums)
21 {
22         struct mlme_priv        *pmlmepriv = &(adapter->mlmepriv);
23         _queue *queue = &(pmlmepriv->scanned_queue);
24         struct wlan_network *pnetwork = NULL;
25         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
26
27         _list   *plist, *phead;
28         _irqL irqL;
29         int chan_idx = -1;
30
31         if (pbss_nums == NULL) {
32                 RTW_ERR("%s pbss_nums is null pointer\n", __func__);
33                 return;
34         }
35         _rtw_memset(pbss_nums, 0, MAX_CHANNEL_NUM);
36
37         _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL);
38         phead = get_list_head(queue);
39         plist = get_next(phead);
40         while (1) {
41                 if (rtw_end_of_queue_search(phead, plist) == _TRUE)
42                         break;
43
44                 pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
45                 if (!pnetwork)
46                         break;
47                 chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), pnetwork->network.Configuration.DSConfig);
48                 if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) {
49                         RTW_ERR("%s can't get chan_idx(CH:%d)\n",
50                                 __func__, pnetwork->network.Configuration.DSConfig);
51                         chan_idx = 0;
52                 }
53                 /*if (pnetwork->network.Reserved[0] != BSS_TYPE_PROB_REQ)*/
54
55                 pbss_nums[chan_idx]++;
56
57                 plist = get_next(plist);
58         }
59         _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL);
60 }
61
62 u8 rtw_get_ch_num_by_idx(_adapter *adapter, u8 idx)
63 {
64         struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter);
65         RT_CHANNEL_INFO *pch_set = rfctl->channel_set;
66         u8 max_chan_nums = rfctl->max_chan_nums;
67
68         if (idx >= max_chan_nums)
69                 return 0;
70         return pch_set[idx].ChannelNum;
71 }
72 #endif /*defined(CONFIG_RTW_ACS) || defined(CONFIG_BACKGROUND_NOISE_MONITOR)*/
73
74
75 #ifdef CONFIG_RTW_ACS
76 void rtw_acs_version_dump(void *sel, _adapter *adapter)
77 {
78         _RTW_PRINT_SEL(sel, "RTK_ACS VER_%d\n", RTK_ACS_VERSION);
79 }
80 u8 rtw_phydm_clm_ratio(_adapter *adapter)
81 {
82         struct dm_struct *phydm = adapter_to_phydm(adapter);
83
84         return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CLM_RATIO);
85 }
86 u8 rtw_phydm_nhm_ratio(_adapter *adapter)
87 {
88         struct dm_struct *phydm = adapter_to_phydm(adapter);
89
90         return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_NHM_RATIO);
91 }
92 void rtw_acs_reset(_adapter *adapter)
93 {
94         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
95         struct auto_chan_sel *pacs = &hal_data->acs;
96
97         _rtw_memset(pacs, 0, sizeof(struct auto_chan_sel));
98         #ifdef CONFIG_RTW_ACS_DBG
99         rtw_acs_adv_reset(adapter);
100         #endif /*CONFIG_RTW_ACS_DBG*/
101 }
102
103 #ifdef CONFIG_RTW_ACS_DBG
104 u8 rtw_is_acs_igi_valid(_adapter *adapter)
105 {
106         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
107         struct auto_chan_sel *pacs = &hal_data->acs;
108
109         if ((pacs->igi) && ((pacs->igi >= 0x1E) || (pacs->igi < 0x60)))
110                 return _TRUE;
111
112         return _FALSE;
113 }
114 void rtw_acs_adv_setting(_adapter *adapter, RT_SCAN_TYPE scan_type, u16 scan_time, u8 igi, u8 bw)
115 {
116         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
117         struct auto_chan_sel *pacs = &hal_data->acs;
118         struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv;
119         struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
120
121         pacs->scan_type = scan_type;
122         pacs->scan_time = scan_time;
123         pacs->igi = igi;
124         pacs->bw = bw;
125         RTW_INFO("[ACS] ADV setting - scan_type:%c, ch_ms:%d(ms), igi:0x%02x, bw:%d\n",
126                 pacs->scan_type ? 'A' : 'P', pacs->scan_time, pacs->igi, pacs->bw);
127 }
128 void rtw_acs_adv_reset(_adapter *adapter)
129 {
130         rtw_acs_adv_setting(adapter, SCAN_ACTIVE, 0, 0, 0);
131 }
132 #endif /*CONFIG_RTW_ACS_DBG*/
133
134 void rtw_acs_trigger(_adapter *adapter, u16 scan_time_ms, u8 scan_chan, enum NHM_PID pid)
135 {
136         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
137         struct dm_struct *phydm = adapter_to_phydm(adapter);
138 #if (RTK_ACS_VERSION == 3)
139         struct clm_para_info clm_para;
140         struct nhm_para_info nhm_para;
141         struct env_trig_rpt trig_rpt;
142
143         scan_time_ms -= 10;
144
145         init_acs_clm(clm_para, scan_time_ms);
146
147         if (pid == NHM_PID_IEEE_11K_HIGH)
148                 init_11K_high_nhm(nhm_para, scan_time_ms);
149         else if (pid == NHM_PID_IEEE_11K_LOW)
150                 init_11K_low_nhm(nhm_para, scan_time_ms);
151         else
152                 init_acs_nhm(nhm_para, scan_time_ms);
153
154         hal_data->acs.trig_rst = phydm_env_mntr_trigger(phydm, &nhm_para, &clm_para, &trig_rpt);
155         if (hal_data->acs.trig_rst == (NHM_SUCCESS | CLM_SUCCESS)) {
156                 hal_data->acs.trig_rpt.clm_rpt_stamp = trig_rpt.clm_rpt_stamp;
157                 hal_data->acs.trig_rpt.nhm_rpt_stamp = trig_rpt.nhm_rpt_stamp;
158                 /*RTW_INFO("[ACS] trigger success (rst = 0x%02x, clm_stamp:%d, nhm_stamp:%d)\n",
159                         hal_data->acs.trig_rst, hal_data->acs.trig_rpt.clm_rpt_stamp, hal_data->acs.trig_rpt.nhm_rpt_stamp);*/
160         } else
161                 RTW_ERR("[ACS] trigger failed (rst = 0x%02x)\n", hal_data->acs.trig_rst);
162 #else
163         phydm_ccx_monitor_trigger(phydm, scan_time_ms);
164 #endif
165
166         hal_data->acs.trigger_ch = scan_chan;
167         hal_data->acs.triggered = _TRUE;
168
169         #ifdef CONFIG_RTW_ACS_DBG
170         RTW_INFO("[ACS] Trigger CH:%d, Times:%d\n", hal_data->acs.trigger_ch, scan_time_ms);
171         #endif
172 }
173 void rtw_acs_get_rst(_adapter *adapter)
174 {
175         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
176         struct dm_struct *phydm = adapter_to_phydm(adapter);
177         int chan_idx = -1;
178         u8 cur_chan = hal_data->acs.trigger_ch;
179
180         if (cur_chan == 0)
181                 return;
182
183         if (!hal_data->acs.triggered)
184                 return;
185
186         chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), cur_chan);
187         if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) {
188                 RTW_ERR("[ACS] %s can't get chan_idx(CH:%d)\n", __func__, cur_chan);
189                 return;
190         }
191 #if (RTK_ACS_VERSION == 3)
192         if (!(hal_data->acs.trig_rst == (NHM_SUCCESS | CLM_SUCCESS))) {
193                 RTW_ERR("[ACS] get_rst return, due to acs trigger failed\n");
194                 return;
195         }
196
197         {
198                 struct env_mntr_rpt rpt = {0};
199                 u8 rst;
200
201                 rst = phydm_env_mntr_result(phydm, &rpt);
202                 if ((rst == (NHM_SUCCESS | CLM_SUCCESS)) &&
203                         (rpt.clm_rpt_stamp == hal_data->acs.trig_rpt.clm_rpt_stamp) &&
204                         (rpt.nhm_rpt_stamp == hal_data->acs.trig_rpt.nhm_rpt_stamp)){
205                         hal_data->acs.clm_ratio[chan_idx] = rpt.clm_ratio;
206                         hal_data->acs.nhm_ratio[chan_idx] = rpt.nhm_ratio;
207                         _rtw_memcpy(&hal_data->acs.nhm[chan_idx][0], rpt.nhm_result, NHM_RPT_NUM);
208
209                         /*RTW_INFO("[ACS] get_rst success (rst = 0x%02x, clm_stamp:%d:%d, nhm_stamp:%d:%d)\n",
210                         rst,
211                         hal_data->acs.trig_rpt.clm_rpt_stamp, rpt.clm_rpt_stamp,
212                         hal_data->acs.trig_rpt.nhm_rpt_stamp, rpt.nhm_rpt_stamp);*/
213                 } else {
214                         RTW_ERR("[ACS] get_rst failed (rst = 0x%02x, clm_stamp:%d:%d, nhm_stamp:%d:%d)\n",
215                         rst,
216                         hal_data->acs.trig_rpt.clm_rpt_stamp, rpt.clm_rpt_stamp,
217                         hal_data->acs.trig_rpt.nhm_rpt_stamp, rpt.nhm_rpt_stamp);
218                 }
219         }
220
221 #else
222         phydm_ccx_monitor_result(phydm);
223
224         hal_data->acs.clm_ratio[chan_idx] = rtw_phydm_clm_ratio(adapter);
225         hal_data->acs.nhm_ratio[chan_idx] = rtw_phydm_nhm_ratio(adapter);
226 #endif
227         hal_data->acs.triggered = _FALSE;
228         #ifdef CONFIG_RTW_ACS_DBG
229         RTW_INFO("[ACS] Result CH:%d, CLM:%d NHM:%d\n",
230                 cur_chan, hal_data->acs.clm_ratio[chan_idx], hal_data->acs.nhm_ratio[chan_idx]);
231         #endif
232 }
233
234 void _rtw_phydm_acs_select_best_chan(_adapter *adapter)
235 {
236         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
237         struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter);
238         u8 ch_idx;
239         u8 ch_idx_24g = 0xFF, ch_idx_5g = 0xFF;
240         u8 min_itf_24g = 0xFF,  min_itf_5g = 0xFF;
241         u8 *pbss_nums = hal_data->acs.bss_nums;
242         u8 *pclm_ratio = hal_data->acs.clm_ratio;
243         u8 *pnhm_ratio = hal_data->acs.nhm_ratio;
244         u8 *pinterference_time = hal_data->acs.interference_time;
245         u8 max_chan_nums = rfctl->max_chan_nums;
246
247         for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) {
248                 if (pbss_nums[ch_idx])
249                         pinterference_time[ch_idx] = (pclm_ratio[ch_idx] / 2) + pnhm_ratio[ch_idx];
250                 else
251                         pinterference_time[ch_idx] = pclm_ratio[ch_idx] + pnhm_ratio[ch_idx];
252
253                 if (rtw_get_ch_num_by_idx(adapter, ch_idx) < 14) {
254                         if (pinterference_time[ch_idx] < min_itf_24g) {
255                                 min_itf_24g = pinterference_time[ch_idx];
256                                 ch_idx_24g = ch_idx;
257                         }
258                 } else {
259                         if (pinterference_time[ch_idx] < min_itf_5g) {
260                                 min_itf_5g = pinterference_time[ch_idx];
261                                 ch_idx_5g = ch_idx;
262                         }
263                 }
264         }
265         if (ch_idx_24g != 0xFF)
266                 hal_data->acs.best_chan_24g = rtw_get_ch_num_by_idx(adapter, ch_idx_24g);
267
268         if (ch_idx_5g != 0xFF)
269                 hal_data->acs.best_chan_5g = rtw_get_ch_num_by_idx(adapter, ch_idx_5g);
270
271         hal_data->acs.trigger_ch = 0;
272 }
273
274 void rtw_acs_info_dump(void *sel, _adapter *adapter)
275 {
276         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
277         struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter);
278         u8 max_chan_nums = rfctl->max_chan_nums;
279         u8 ch_idx, ch_num;
280
281         _RTW_PRINT_SEL(sel, "========== ACS (VER-%d) ==========\n", RTK_ACS_VERSION);
282         _RTW_PRINT_SEL(sel, "Best 24G Channel:%d\n", hal_data->acs.best_chan_24g);
283         _RTW_PRINT_SEL(sel, "Best 5G Channel:%d\n\n", hal_data->acs.best_chan_5g);
284
285         #ifdef CONFIG_RTW_ACS_DBG
286         _RTW_PRINT_SEL(sel, "Advanced setting - scan_type:%c, ch_ms:%d(ms), igi:0x%02x, bw:%d\n",
287                 hal_data->acs.scan_type ? 'A' : 'P', hal_data->acs.scan_time, hal_data->acs.igi, hal_data->acs.bw);
288
289         _RTW_PRINT_SEL(sel, "BW  20MHz\n");
290         _RTW_PRINT_SEL(sel, "%5s  %3s  %3s  %3s(%%)  %3s(%%)  %3s\n",
291                                                 "Index", "CH", "BSS", "CLM", "NHM", "ITF");
292
293         for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) {
294                 ch_num = rtw_get_ch_num_by_idx(adapter, ch_idx);
295                 _RTW_PRINT_SEL(sel, "%5d  %3d  %3d  %6d  %6d  %3d\n",
296                                                 ch_idx, ch_num, hal_data->acs.bss_nums[ch_idx],
297                                                 hal_data->acs.clm_ratio[ch_idx],
298                                                 hal_data->acs.nhm_ratio[ch_idx],
299                                                 hal_data->acs.interference_time[ch_idx]);
300         }
301         #endif
302 }
303 void rtw_acs_select_best_chan(_adapter *adapter)
304 {
305         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
306
307         _rtw_bss_nums_count(adapter, hal_data->acs.bss_nums);
308         _rtw_phydm_acs_select_best_chan(adapter);
309         rtw_acs_info_dump(RTW_DBGDUMP, adapter);
310 }
311
312 void rtw_acs_start(_adapter *adapter)
313 {
314         rtw_acs_reset(adapter);
315         if (GET_ACS_STATE(adapter) != ACS_ENABLE)
316                 SET_ACS_STATE(adapter, ACS_ENABLE);
317 }
318 void rtw_acs_stop(_adapter *adapter)
319 {
320         SET_ACS_STATE(adapter, ACS_DISABLE);
321 }
322
323
324 u8 rtw_acs_get_clm_ratio_by_ch_num(_adapter *adapter, u8 chan)
325 {
326         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
327         int chan_idx = -1;
328
329         chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan);
330         if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) {
331                 RTW_ERR("[ACS] Get CLM fail, can't get chan_idx(CH:%d)\n", chan);
332                 return 0;
333         }
334
335         return hal_data->acs.clm_ratio[chan_idx];
336 }
337 u8 rtw_acs_get_clm_ratio_by_ch_idx(_adapter *adapter, u8 ch_idx)
338 {
339         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
340
341         if (ch_idx >= MAX_CHANNEL_NUM) {
342                 RTW_ERR("%s [ACS] ch_idx(%d) is invalid\n", __func__, ch_idx);
343                 return 0;
344         }
345
346         return hal_data->acs.clm_ratio[ch_idx];
347 }
348 u8 rtw_acs_get_nhm_ratio_by_ch_num(_adapter *adapter, u8 chan)
349 {
350         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
351         int chan_idx = -1;
352
353         chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan);
354         if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) {
355                 RTW_ERR("[ACS] Get NHM fail, can't get chan_idx(CH:%d)\n", chan);
356                 return 0;
357         }
358
359         return hal_data->acs.nhm_ratio[chan_idx];
360 }
361 u8 rtw_acs_get_num_ratio_by_ch_idx(_adapter *adapter, u8 ch_idx)
362 {
363         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
364
365         if (ch_idx >= MAX_CHANNEL_NUM) {
366                 RTW_ERR("%s [ACS] ch_idx(%d) is invalid\n", __func__, ch_idx);
367                 return 0;
368         }
369
370         return hal_data->acs.nhm_ratio[ch_idx];
371 }
372 void rtw_acs_chan_info_dump(void *sel, _adapter *adapter)
373 {
374         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
375         struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter);
376         u8 max_chan_nums = rfctl->max_chan_nums;
377         u8 ch_idx, ch_num;
378         u8 utilization;
379
380         _RTW_PRINT_SEL(sel, "BW  20MHz\n");
381         _RTW_PRINT_SEL(sel, "%5s  %3s  %7s(%%)  %12s(%%)  %11s(%%)  %9s(%%)  %8s(%%)\n",
382                                                 "Index", "CH", "Quality", "Availability", "Utilization",
383                                                 "WIFI Util", "Interference Util");
384
385         for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) {
386                 ch_num = rtw_get_ch_num_by_idx(adapter, ch_idx);
387                 utilization = hal_data->acs.clm_ratio[ch_idx] + hal_data->acs.nhm_ratio[ch_idx];
388                 _RTW_PRINT_SEL(sel, "%5d  %3d  %7d   %12d   %12d   %12d   %12d\n",
389                                                 ch_idx, ch_num,
390                                                 (100-hal_data->acs.interference_time[ch_idx]),
391                                                 (100-utilization),
392                                                 utilization,
393                                                 hal_data->acs.clm_ratio[ch_idx],
394                                                 hal_data->acs.nhm_ratio[ch_idx]);
395         }
396 }
397 void rtw_acs_current_info_dump(void *sel, _adapter *adapter)
398 {
399         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
400         u8 ch, cen_ch, bw, offset;
401
402         _RTW_PRINT_SEL(sel, "========== ACS (VER-%d) ==========\n", RTK_ACS_VERSION);
403
404         ch = rtw_get_oper_ch(adapter);
405         bw = rtw_get_oper_bw(adapter);
406         offset = rtw_get_oper_choffset(adapter);
407
408         _RTW_PRINT_SEL(sel, "Current Channel:%d\n", ch);
409         if ((bw == CHANNEL_WIDTH_80) ||(bw == CHANNEL_WIDTH_40)) {
410                 cen_ch = rtw_get_center_ch(ch, bw, offset);
411                 _RTW_PRINT_SEL(sel, "Center Channel:%d\n", cen_ch);
412         }
413
414         _RTW_PRINT_SEL(sel, "Current BW %s\n", ch_width_str(bw));
415         if (0)
416                 _RTW_PRINT_SEL(sel, "Current IGI 0x%02x\n", rtw_phydm_get_cur_igi(adapter));
417         _RTW_PRINT_SEL(sel, "CLM:%d, NHM:%d\n\n",
418                 hal_data->acs.cur_ch_clm_ratio, hal_data->acs.cur_ch_nhm_ratio);
419 }
420
421 void rtw_acs_update_current_info(_adapter *adapter)
422 {
423         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
424
425         hal_data->acs.cur_ch_clm_ratio = rtw_phydm_clm_ratio(adapter);
426         hal_data->acs.cur_ch_nhm_ratio = rtw_phydm_nhm_ratio(adapter);
427
428         #ifdef CONFIG_RTW_ACS_DBG
429         rtw_acs_current_info_dump(RTW_DBGDUMP, adapter);
430         #endif
431 }
432 #endif /*CONFIG_RTW_ACS*/
433
434 #ifdef CONFIG_BACKGROUND_NOISE_MONITOR
435 void rtw_noise_monitor_version_dump(void *sel, _adapter *adapter)
436 {
437         _RTW_PRINT_SEL(sel, "RTK_NOISE_MONITOR VER_%d\n", RTK_NOISE_MONITOR_VERSION);
438 }
439 void rtw_nm_enable(_adapter *adapter)
440 {
441         SET_NM_STATE(adapter, NM_ENABLE);
442 }
443 void rtw_nm_disable(_adapter *adapter)
444 {
445         SET_NM_STATE(adapter, NM_DISABLE);
446 }
447 void rtw_noise_info_dump(void *sel, _adapter *adapter)
448 {
449         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
450         struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter);
451         u8 max_chan_nums = rfctl->max_chan_nums;
452         u8 ch_idx, ch_num;
453
454         _RTW_PRINT_SEL(sel, "========== NM (VER-%d) ==========\n", RTK_NOISE_MONITOR_VERSION);
455
456         _RTW_PRINT_SEL(sel, "%5s  %3s  %3s  %10s", "Index", "CH", "BSS", "Noise(dBm)\n");
457
458         _rtw_bss_nums_count(adapter, hal_data->nm.bss_nums);
459
460         for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) {
461                 ch_num = rtw_get_ch_num_by_idx(adapter, ch_idx);
462                 _RTW_PRINT_SEL(sel, "%5d  %3d  %3d  %10d\n",
463                                                 ch_idx, ch_num, hal_data->nm.bss_nums[ch_idx],
464                                                 hal_data->nm.noise[ch_idx]);
465         }
466 }
467
468 void rtw_noise_measure(_adapter *adapter, u8 chan, u8 is_pause_dig, u8 igi_value, u32 max_time)
469 {
470         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
471         struct dm_struct *phydm = &hal_data->odmpriv;
472         int chan_idx = -1;
473         s16 noise = 0;
474
475         #ifdef DBG_NOISE_MONITOR
476         RTW_INFO("[NM] chan(%d)-PauseDIG:%s,  IGIValue:0x%02x, max_time:%d (ms)\n",
477                 chan, (is_pause_dig) ? "Y" : "N", igi_value, max_time);
478         #endif
479
480         chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan);
481         if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) {
482                 RTW_ERR("[NM] Get noise fail, can't get chan_idx(CH:%d)\n", chan);
483                 return;
484         }
485         noise = odm_inband_noise_monitor(phydm, is_pause_dig, igi_value, max_time); /*dBm*/
486
487         hal_data->nm.noise[chan_idx] = noise;
488
489         #ifdef DBG_NOISE_MONITOR
490         RTW_INFO("[NM] %s chan_%d, noise = %d (dBm)\n", __func__, chan, hal_data->nm.noise[chan_idx]);
491
492         RTW_INFO("[NM] noise_a = %d, noise_b = %d  noise_all:%d\n",
493                          phydm->noise_level.noise[RF_PATH_A],
494                          phydm->noise_level.noise[RF_PATH_B],
495                          phydm->noise_level.noise_all);
496         #endif /*DBG_NOISE_MONITOR*/
497 }
498
499 s16 rtw_noise_query_by_chan_num(_adapter *adapter, u8 chan)
500 {
501         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
502         s16 noise = 0;
503         int chan_idx = -1;
504
505         chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan);
506         if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) {
507                 RTW_ERR("[NM] Get noise fail, can't get chan_idx(CH:%d)\n", chan);
508                 return noise;
509         }
510         noise = hal_data->nm.noise[chan_idx];
511
512         #ifdef DBG_NOISE_MONITOR
513         RTW_INFO("[NM] %s chan_%d, noise = %d (dBm)\n", __func__, chan, noise);
514         #endif/*DBG_NOISE_MONITOR*/
515         return noise;
516 }
517 s16 rtw_noise_query_by_chan_idx(_adapter *adapter, u8 ch_idx)
518 {
519         HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter);
520         s16 noise = 0;
521
522         if (ch_idx >= MAX_CHANNEL_NUM) {
523                 RTW_ERR("[NM] %s ch_idx(%d) is invalid\n", __func__, ch_idx);
524                 return noise;
525         }
526         noise = hal_data->nm.noise[ch_idx];
527
528         #ifdef DBG_NOISE_MONITOR
529         RTW_INFO("[NM] %s ch_idx %d, noise = %d (dBm)\n", __func__, ch_idx, noise);
530         #endif/*DBG_NOISE_MONITOR*/
531         return noise;
532 }
533
534 s16 rtw_noise_measure_curchan(_adapter *padapter)
535 {
536         s16 noise = 0;
537         u8 igi_value = 0x1E;
538         u32 max_time = 100;/* ms */
539         u8 is_pause_dig = _TRUE;
540         u8 cur_chan = rtw_get_oper_ch(padapter);
541
542         if (rtw_linked_check(padapter) == _FALSE)
543                 return noise;
544
545         rtw_ps_deny(padapter, PS_DENY_IOCTL);
546         LeaveAllPowerSaveModeDirect(padapter);
547         rtw_noise_measure(padapter, cur_chan, is_pause_dig, igi_value, max_time);
548         noise = rtw_noise_query_by_chan_num(padapter, cur_chan);
549         rtw_ps_deny_cancel(padapter, PS_DENY_IOCTL);
550
551         return noise;
552 }
553 #endif /*CONFIG_BACKGROUND_NOISE_MONITOR*/