1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: hmac_psd
15 * Author:
16 * Create: 2022-08-15
17 */
18 #include "hmac_psd.h"
19 #include "hmac_config.h"
20 #ifndef _PRE_WLAN_FEATURE_WS73
21 #include "diag.h"
22 #else
23 #include "soc_zdiag.h"
24 #endif
25 #include "wal_common.h"
26 #include "hmac_feature_interface.h"
27 #include "hal_psd.h"
28
29 #ifdef __cplusplus
30 #if __cplusplus
31 extern "C" {
32 #endif
33 #endif
34
35 #undef THIS_FILE_ID
36 #define THIS_FILE_ID OAM_FILE_ID_HMAC_PSD_C
37 static hmac_psd_enable_timer g_psd_enable_timer = {0};
38 /* psd定时器处理函数 */
39 static osal_u32 hmac_psd_enable_callback(osal_void *hmac_vap_arg);
40 static osal_u8 g_psd_switch = 0;
41
42 typedef osal_void (*psd_enable_callback)(osal_u16 cmd_id, osal_void* cmd_param, osal_u16 cmd_param_size,
43 DIAG_OPTION *option);
44 osal_void uapi_zdiag_psd_enable_register(psd_enable_callback cb);
45 osal_void uapi_zdiag_psd_enable_unregister(void);
46 #ifndef _PRE_WLAN_FEATURE_WS73
47 errcode_t uapi_diag_report_packet(uint16_t cmd_id, DIAG_OPTION *option, const uint8_t *packet, uint16_t packet_size,
48 bool sync);
49 #else
50 ext_errno zdiag_report_packet(td_u16 cmd_id, DIAG_OPTION *option, TD_CONST td_u8 *packet, td_u16 packet_size,
51 td_bool sync);
52 #endif
53
hmac_psd_get_switch(void)54 osal_u8 hmac_psd_get_switch(void)
55 {
56 return g_psd_switch;
57 }
58
hmac_psd_enable(hmac_psd_enable_stru * psd_enable_param,hmac_vap_stru * hmac_vap)59 static osal_void hmac_psd_enable(hmac_psd_enable_stru *psd_enable_param, hmac_vap_stru *hmac_vap)
60 {
61 frw_msg msg_info;
62 (osal_void)memset_s(&msg_info, sizeof(msg_info), 0, sizeof(msg_info));
63
64 g_psd_switch = OSAL_TRUE;
65 /* 设置中心信道(默认7, 80M采样率, 覆盖1~13信道) */
66 msg_info.data = (osal_u8 *)&psd_enable_param->channel;
67 msg_info.data_len = sizeof(psd_enable_param->channel);
68 hmac_config_set_freq_etc(hmac_vap, &msg_info);
69 /* 先打开mac跟phy */
70 hal_enable_machw_phy_and_pa(hmac_vap->hal_device);
71 /* psd寄存器使能 */
72 hal_psd_enable_etc();
73 /* 创建定时器, 定期获取PSD数据 */
74 if (g_psd_enable_timer.psd_timer.is_registerd == OSAL_TRUE) {
75 frw_destroy_timer_entry(&g_psd_enable_timer.psd_timer);
76 }
77 g_psd_enable_timer.sampling_count = 0;
78 /* 采样时间单位为min 转成ms, 乘以60 * 1000; 周期单位为ms */
79 g_psd_enable_timer.max_sampling_count = psd_enable_param->duration * 60 * 1000 / psd_enable_param->cycle;
80 frw_create_timer_entry(&g_psd_enable_timer.psd_timer, hmac_psd_enable_callback, psd_enable_param->cycle, hmac_vap,
81 OSAL_TRUE);
82 wifi_printf("psd enable\r\n");
83 }
84
hmac_psd_disable(hmac_psd_enable_stru * psd_enable_param,hmac_vap_stru * hmac_vap)85 static osal_void hmac_psd_disable(hmac_psd_enable_stru *psd_enable_param, hmac_vap_stru *hmac_vap)
86 {
87 unref_param(hmac_vap);
88 unref_param(psd_enable_param);
89
90 /* psd寄存器去使能 */
91 hal_psd_disable_etc();
92 /* 销毁定时器 */
93 frw_destroy_timer_entry(&g_psd_enable_timer.psd_timer);
94 g_psd_enable_timer.sampling_count = 0;
95 g_psd_enable_timer.max_sampling_count = 0;
96
97 /* 最后关闭mac跟phy */
98 hal_disable_machw_phy_and_pa();
99 g_psd_switch = OSAL_FALSE;
100 wifi_printf("psd disable\r\n");
101 }
102 /* psd使能时的定时处理函数 */
hmac_psd_enable_callback(osal_void * hmac_vap_arg)103 static osal_u32 hmac_psd_enable_callback(osal_void *hmac_vap_arg)
104 {
105 hmac_vap_stru *hmac_vap = (hmac_vap_stru *)hmac_vap_arg;
106 frw_msg psd_msg;
107 DIAG_OPTION option = {0};
108 osal_u32 psd_data = OSAL_FALSE;
109 (osal_void)memset_s(&psd_msg, sizeof(frw_msg), 0, sizeof(frw_msg));
110 g_psd_enable_timer.sampling_count++;
111 /* 采样次数已够 关闭psd */
112 if (g_psd_enable_timer.sampling_count > g_psd_enable_timer.max_sampling_count) {
113 wifi_printf("reach max_sampling_count[%d], disable psd\r\n", g_psd_enable_timer.max_sampling_count);
114 hmac_psd_disable(OSAL_NULL, hmac_vap);
115 /* 给hso发消息终止测量 */
116 #ifndef _PRE_WLAN_FEATURE_WS73
117 uapi_diag_report_packet(DIAG_CMD_ID_PSD_ENABLE, &option, (osal_u8 *)&psd_data, sizeof(psd_data), TD_TRUE);
118 #else
119 zdiag_report_packet(DIAG_CMD_ID_PSD_ENABLE, &option, (osal_u8 *)&psd_data, sizeof(psd_data), TD_TRUE);
120 #endif
121 return OAL_SUCC;
122 }
123 /* 发消息给dmac取数据 */
124 frw_send_msg_to_device(hmac_vap->vap_id, WLAN_MSG_H2D_C_CFG_DEVICE_GET_PSD_DATA, &psd_msg, OSAL_TRUE);
125 return OAL_SUCC;
126 }
127
hmac_config_psd_enable(hmac_vap_stru * hmac_vap,frw_msg * msg)128 osal_s32 hmac_config_psd_enable(hmac_vap_stru *hmac_vap, frw_msg *msg)
129 {
130 hmac_psd_enable_stru *psd_enable_param = (hmac_psd_enable_stru *)msg->data;
131 frw_msg psd_enable_msg = {0};
132 osal_s32 ret;
133 (osal_void)memset_s(&psd_enable_msg, sizeof(frw_msg), 0, sizeof(frw_msg));
134
135 oam_warning_log3(0, OAM_SF_ANY, "hmac_config_psd_enable enable[%d] cyc[%d] dur[%d]",
136 psd_enable_param->enable, psd_enable_param->cycle, psd_enable_param->duration);
137
138 psd_enable_msg.data = (osal_u8 *)&psd_enable_param->enable;
139 psd_enable_msg.data_len = sizeof(psd_enable_param->enable);
140 if (psd_enable_param->enable == OSAL_TRUE) {
141 hmac_psd_enable(psd_enable_param, hmac_vap);
142 } else {
143 hmac_psd_disable(psd_enable_param, hmac_vap);
144 }
145 /* 发消息通知dmac */
146 ret = frw_send_msg_to_device(hmac_vap->vap_id, WLAN_MSG_H2D_C_CFG_PSD_ENABLE, &psd_enable_msg, OSAL_TRUE);
147 return ret;
148 }
149
150 /* 处理dmac报上来的psd数据,交给hso解析 */
hmac_config_psd_data_report(hmac_vap_stru * hmac_vap,frw_msg * msg)151 osal_s32 hmac_config_psd_data_report(hmac_vap_stru *hmac_vap, frw_msg *msg)
152 {
153 osal_s8 *psd_data = (osal_s8 *)msg->data;
154 DIAG_OPTION option = {0};
155 osal_s32 ret;
156
157 unref_param(hmac_vap);
158 #ifndef _PRE_WLAN_FEATURE_WS73
159 uapi_diag_report_packet(DIAG_CMD_ID_PSD_REPORT, &option, (osal_u8 *)psd_data, PSD_DATA_LEN, TD_TRUE);
160 #else
161 zdiag_report_packet(DIAG_CMD_ID_PSD_REPORT, &option, (osal_u8 *)psd_data, PSD_DATA_LEN, TD_TRUE);
162 #endif
163
164 ret = frw_asyn_host_post_msg(WLAN_MSG_H2W_RX_PSD, FRW_POST_PRI_LOW, hmac_vap->vap_id, msg);
165 if (ret != OAL_SUCC) {
166 oam_warning_log1(0, OAM_SF_ANY, "hmac_config_psd_data_report err ret[%d]", ret);
167 return ret;
168 }
169
170 return OAL_SUCC;
171 }
172
hmac_psd_diag_enable_callback(osal_u16 cmd_id,osal_void * cmd_param,osal_u16 cmd_param_size,DIAG_OPTION * option)173 OSAL_STATIC osal_void hmac_psd_diag_enable_callback(osal_u16 cmd_id, osal_void* cmd_param, osal_u16 cmd_param_size,
174 DIAG_OPTION *option)
175 {
176 hmac_psd_enable_stru *psd_enable_param = (hmac_psd_enable_stru *)cmd_param;
177 oal_net_device_stru *net_dev;
178 osal_s32 ret;
179
180 unref_param(option);
181 unref_param(cmd_id);
182 unref_param(cmd_param_size);
183 wifi_printf("hmac_psd_diag_enable_callback enable[%d] ch[%d] bw[%d] cyc[%d] dur[%d]\r\n",
184 psd_enable_param->enable, psd_enable_param->channel, psd_enable_param->band, psd_enable_param->cycle,
185 psd_enable_param->duration);
186 #if defined(_PRE_OS_VERSION_LINUX) && defined(_PRE_OS_VERSION) && (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
187 net_dev = oal_dev_get_by_name(PSD_DEV_NAME);
188 #else
189 net_dev = oal_get_netdev_by_name(PSD_DEV_NAME);
190 #endif
191 if (net_dev == OSAL_NULL) {
192 oam_warning_log0(0, 0, "hmac_psd_diag_enable_callback::net_dev null");
193 return;
194 }
195 #if defined(_PRE_OS_VERSION_LINUX) && defined(_PRE_OS_VERSION) && (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
196 /* 调用oal_dev_get_by_name后,必须调用oal_dev_put使net_dev的引用计数减一 */
197 oal_dev_put(net_dev);
198 #endif
199 /* 此处为临时方案,待设置信道卡死问题解决后改为直调 */
200 ret = wal_async_post2hmac_no_rsp(wal_util_get_vap_id(net_dev), WLAN_MSG_W2H_CFG_PSD_ENABLE,
201 (osal_u8 *)psd_enable_param, sizeof(hmac_psd_enable_stru), FRW_POST_PRI_LOW);
202 if (OAL_UNLIKELY(ret != OAL_SUCC)) {
203 oam_warning_log1(0, OAM_SF_ANY, "{wal_psd_enable_callback::return err code %d!}", ret);
204 return;
205 }
206 }
207
hmac_psd_init(osal_void)208 osal_u32 hmac_psd_init(osal_void)
209 {
210 /* 消息注册 */
211 /* W2H */
212 frw_msg_hook_register(WLAN_MSG_W2H_CFG_PSD_ENABLE, hmac_config_psd_enable);
213 /* H2D */
214 /* D2H */
215 frw_msg_hook_register(WLAN_MSG_D2H_PSD_DATA_REPORT, hmac_config_psd_data_report);
216 /* 对外接口注册 */
217 uapi_zdiag_psd_enable_register(hmac_psd_diag_enable_callback);
218 hmac_feature_hook_register(HMAC_FHOOK_PSD_GET_SWITCH, hmac_psd_get_switch);
219 return OAL_SUCC;
220 }
221
hmac_psd_deinit(osal_void)222 osal_void hmac_psd_deinit(osal_void)
223 {
224 /* 消息去注册 */
225 frw_msg_hook_unregister(WLAN_MSG_D2H_PSD_DATA_REPORT);
226 frw_msg_hook_unregister(WLAN_MSG_W2H_CFG_PSD_ENABLE);
227 /* 对外接口去注册 */
228 uapi_zdiag_psd_enable_unregister();
229 hmac_feature_hook_unregister(HMAC_FHOOK_PSD_GET_SWITCH);
230 return;
231 }
232
233 #ifdef __cplusplus
234 #if __cplusplus
235 }
236 #endif
237 #endif