xref: /wlan-dirver/qca-wifi-host-cmn/target_if/regulatory/src/target_if_reg.c (revision 4865edfd190c086bbe2c69aae12a8226f877b91e)
1 /*
2  * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
3  *
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20  /**
21  * DOC: target_if_reg.c
22  * This file contains regulatory target interface
23  */
24 
25 
26 #include <wmi_unified_api.h>
27 #include <reg_services_public_struct.h>
28 #include <wlan_reg_tgt_api.h>
29 #include <target_if.h>
30 #include <target_if_reg.h>
31 #include <wmi_unified_reg_api.h>
32 
33 static inline uint32_t get_chan_list_cc_event_id(void)
34 {
35 	return wmi_reg_chan_list_cc_event_id;
36 }
37 
38 static bool tgt_if_regulatory_is_11d_offloaded(struct wlan_objmgr_psoc
39 					       *psoc)
40 {
41 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
42 
43 	return wmi_service_enabled(wmi_handle,
44 				   wmi_service_11d_offload);
45 }
46 
47 static bool tgt_if_regulatory_is_regdb_offloaded(struct wlan_objmgr_psoc
48 						 *psoc)
49 {
50 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
51 
52 	return wmi_service_enabled(wmi_handle,
53 				   wmi_service_regulatory_db);
54 }
55 
56 static bool tgt_if_regulatory_is_there_serv_ready_extn(struct wlan_objmgr_psoc
57 						       *psoc)
58 {
59 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
60 
61 	return wmi_service_enabled(wmi_handle,
62 				   wmi_service_ext_msg);
63 }
64 
65 static inline struct wlan_lmac_if_reg_rx_ops *
66 target_if_regulatory_get_rx_ops(struct wlan_objmgr_psoc *psoc)
67 {
68 	return &psoc->soc_cb.rx_ops.reg_rx_ops;
69 }
70 
71 QDF_STATUS target_if_reg_set_offloaded_info(struct wlan_objmgr_psoc *psoc)
72 {
73 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
74 
75 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
76 	if (!reg_rx_ops) {
77 		target_if_err("reg_rx_ops is NULL");
78 		return QDF_STATUS_E_FAILURE;
79 	}
80 
81 	if (reg_rx_ops->reg_set_regdb_offloaded)
82 		reg_rx_ops->reg_set_regdb_offloaded(psoc,
83 				tgt_if_regulatory_is_regdb_offloaded(psoc));
84 
85 	if (reg_rx_ops->reg_set_11d_offloaded)
86 		reg_rx_ops->reg_set_11d_offloaded(psoc,
87 				tgt_if_regulatory_is_11d_offloaded(psoc));
88 
89 	return QDF_STATUS_SUCCESS;
90 }
91 
92 static int tgt_reg_chan_list_update_handler(ol_scn_t handle,
93 					    uint8_t *event_buf,
94 					    uint32_t len)
95 {
96 	struct wlan_objmgr_psoc *psoc;
97 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
98 	struct cur_regulatory_info *reg_info;
99 	QDF_STATUS status;
100 
101 	TARGET_IF_ENTER();
102 
103 	psoc = target_if_get_psoc_from_scn_hdl(handle);
104 	if (!psoc) {
105 		target_if_err("psoc ptr is NULL");
106 		return -EINVAL;
107 	}
108 
109 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
110 	if (!reg_rx_ops->master_list_handler) {
111 		target_if_err("master_list_handler is NULL");
112 		return -EINVAL;
113 	}
114 
115 	reg_info = qdf_mem_malloc(sizeof(*reg_info));
116 	if (!reg_info) {
117 		target_if_err("memory allocation failed");
118 		return -ENOMEM;
119 	}
120 
121 	if (wmi_extract_reg_chan_list_update_event(GET_WMI_HDL_FROM_PSOC(psoc),
122 						   event_buf, reg_info, len)
123 	    != QDF_STATUS_SUCCESS) {
124 
125 		target_if_err("Extraction of channel list event failed");
126 		qdf_mem_free(reg_info->reg_rules_2g_ptr);
127 		qdf_mem_free(reg_info->reg_rules_5g_ptr);
128 		qdf_mem_free(reg_info);
129 		return -EFAULT;
130 	}
131 
132 	reg_info->psoc = psoc;
133 
134 	status = reg_rx_ops->master_list_handler(reg_info);
135 	if (status != QDF_STATUS_SUCCESS) {
136 		target_if_err("Failed to process master channel list handler");
137 		qdf_mem_free(reg_info->reg_rules_2g_ptr);
138 		qdf_mem_free(reg_info->reg_rules_5g_ptr);
139 		qdf_mem_free(reg_info);
140 		return -EFAULT;
141 	}
142 
143 	qdf_mem_free(reg_info->reg_rules_2g_ptr);
144 	qdf_mem_free(reg_info->reg_rules_5g_ptr);
145 	qdf_mem_free(reg_info);
146 
147 	target_if_debug("processed regulatory channel list");
148 
149 	return 0;
150 }
151 
152 static int tgt_reg_11d_new_cc_handler(ol_scn_t handle,
153 		uint8_t *event_buf, uint32_t len)
154 {
155 	struct wlan_objmgr_psoc *psoc;
156 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
157 	struct reg_11d_new_country reg_11d_new_cc;
158 	QDF_STATUS status;
159 
160 	TARGET_IF_ENTER();
161 
162 	psoc = target_if_get_psoc_from_scn_hdl(handle);
163 	if (!psoc) {
164 		target_if_err("psoc ptr is NULL");
165 		return -EINVAL;
166 	}
167 
168 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
169 
170 	if (!reg_rx_ops->reg_11d_new_cc_handler) {
171 		target_if_err("reg_11d_new_cc_handler is NULL");
172 		return -EINVAL;
173 	}
174 
175 	if (wmi_extract_reg_11d_new_cc_event(GET_WMI_HDL_FROM_PSOC(psoc),
176 				event_buf, &reg_11d_new_cc, len) !=
177 			QDF_STATUS_SUCCESS) {
178 
179 		target_if_err("Extraction of new country event failed");
180 		return -EFAULT;
181 	}
182 
183 	status = reg_rx_ops->reg_11d_new_cc_handler(psoc, &reg_11d_new_cc);
184 	if (status != QDF_STATUS_SUCCESS) {
185 		target_if_err("Failed to process new country code event");
186 		return -EFAULT;
187 	}
188 
189 	target_if_debug("processed 11d new country code event");
190 
191 	return 0;
192 }
193 
194 static int tgt_reg_ch_avoid_event_handler(ol_scn_t handle,
195 		uint8_t *event_buf, uint32_t len)
196 {
197 	struct wlan_objmgr_psoc *psoc;
198 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
199 	struct ch_avoid_ind_type ch_avoid_event;
200 	QDF_STATUS status;
201 
202 	TARGET_IF_ENTER();
203 
204 	psoc = target_if_get_psoc_from_scn_hdl(handle);
205 	if (!psoc) {
206 		target_if_err("psoc ptr is NULL");
207 		return -EINVAL;
208 	}
209 
210 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
211 
212 	if (!reg_rx_ops->reg_ch_avoid_event_handler) {
213 		target_if_err("reg_ch_avoid_event_handler is NULL");
214 		return -EINVAL;
215 	}
216 
217 	if (wmi_extract_reg_ch_avoid_event(GET_WMI_HDL_FROM_PSOC(psoc),
218 				event_buf, &ch_avoid_event, len) !=
219 			QDF_STATUS_SUCCESS) {
220 
221 		target_if_err("Extraction of CH avoid event failed");
222 		return -EFAULT;
223 	}
224 
225 	status = reg_rx_ops->reg_ch_avoid_event_handler(psoc, &ch_avoid_event);
226 	if (status != QDF_STATUS_SUCCESS) {
227 		target_if_err("Failed to process CH avoid event");
228 		return -EFAULT;
229 	}
230 
231 	target_if_debug("processed CH avoid event");
232 
233 	return 0;
234 }
235 
236 static QDF_STATUS tgt_if_regulatory_register_master_list_handler(
237 	struct wlan_objmgr_psoc *psoc, void *arg)
238 {
239 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
240 
241 	return wmi_unified_register_event_handler(wmi_handle,
242 					       wmi_reg_chan_list_cc_event_id,
243 					       tgt_reg_chan_list_update_handler,
244 					       WMI_RX_UMAC_CTX);
245 
246 }
247 
248 static QDF_STATUS tgt_if_regulatory_unregister_master_list_handler(
249 	struct wlan_objmgr_psoc *psoc, void *arg)
250 {
251 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
252 
253 	return wmi_unified_unregister_event_handler(wmi_handle,
254 					       wmi_reg_chan_list_cc_event_id);
255 }
256 
257 static QDF_STATUS tgt_if_regulatory_set_country_code(
258 	struct wlan_objmgr_psoc *psoc, void *arg)
259 {
260 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
261 
262 	return wmi_unified_set_country_cmd_send(wmi_handle, arg);
263 
264 }
265 
266 static QDF_STATUS tgt_if_regulatory_set_user_country_code(
267 	struct wlan_objmgr_psoc *psoc, uint8_t pdev_id, struct cc_regdmn_s *rd)
268 {
269 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
270 
271 	if (wmi_unified_set_user_country_code_cmd_send(wmi_handle, pdev_id,
272 				rd) != QDF_STATUS_SUCCESS) {
273 		target_if_err("Set user country code failed");
274 		return QDF_STATUS_E_FAILURE;
275 	}
276 
277 	return QDF_STATUS_SUCCESS;
278 }
279 
280 static QDF_STATUS tgt_if_regulatory_register_11d_new_cc_handler(
281 	struct wlan_objmgr_psoc *psoc, void *arg)
282 {
283 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
284 
285 	return wmi_unified_register_event(wmi_handle,
286 					  wmi_11d_new_country_event_id,
287 					  tgt_reg_11d_new_cc_handler);
288 }
289 
290 static QDF_STATUS tgt_if_regulatory_unregister_11d_new_cc_handler(
291 	struct wlan_objmgr_psoc *psoc, void *arg)
292 {
293 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
294 
295 	return wmi_unified_unregister_event(wmi_handle,
296 					    wmi_11d_new_country_event_id);
297 }
298 
299 static QDF_STATUS tgt_if_regulatory_register_ch_avoid_event_handler(
300 	struct wlan_objmgr_psoc *psoc, void *arg)
301 {
302 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
303 
304 	return wmi_unified_register_event(wmi_handle,
305 					  wmi_wlan_freq_avoid_event_id,
306 					  tgt_reg_ch_avoid_event_handler);
307 }
308 
309 static QDF_STATUS tgt_if_regulatory_unregister_ch_avoid_event_handler(
310 	struct wlan_objmgr_psoc *psoc, void *arg)
311 {
312 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
313 
314 	return wmi_unified_unregister_event(wmi_handle,
315 			wmi_wlan_freq_avoid_event_id);
316 }
317 static QDF_STATUS tgt_if_regulatory_start_11d_scan(
318 		struct wlan_objmgr_psoc *psoc,
319 		struct reg_start_11d_scan_req *reg_start_11d_scan_req)
320 {
321 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
322 
323 	return wmi_unified_send_start_11d_scan_cmd(wmi_handle,
324 						   reg_start_11d_scan_req);
325 }
326 
327 static QDF_STATUS tgt_if_regulatory_stop_11d_scan(
328 		   struct wlan_objmgr_psoc *psoc,
329 		   struct reg_stop_11d_scan_req *reg_stop_11d_scan_req)
330 {
331 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
332 
333 	return wmi_unified_send_stop_11d_scan_cmd(wmi_handle,
334 						  reg_stop_11d_scan_req);
335 }
336 
337 
338 QDF_STATUS target_if_register_regulatory_tx_ops(struct wlan_lmac_if_tx_ops
339 						*tx_ops)
340 {
341 	struct wlan_lmac_if_reg_tx_ops *reg_ops = &tx_ops->reg_ops;
342 
343 	reg_ops->register_master_handler =
344 		tgt_if_regulatory_register_master_list_handler;
345 
346 	reg_ops->unregister_master_handler =
347 		tgt_if_regulatory_unregister_master_list_handler;
348 
349 	reg_ops->set_country_code = tgt_if_regulatory_set_country_code;
350 
351 	reg_ops->fill_umac_legacy_chanlist = NULL;
352 
353 	reg_ops->set_country_failed = NULL;
354 
355 	reg_ops->register_11d_new_cc_handler =
356 		tgt_if_regulatory_register_11d_new_cc_handler;
357 
358 	reg_ops->unregister_11d_new_cc_handler =
359 		tgt_if_regulatory_unregister_11d_new_cc_handler;
360 
361 	reg_ops->start_11d_scan = tgt_if_regulatory_start_11d_scan;
362 
363 	reg_ops->stop_11d_scan = tgt_if_regulatory_stop_11d_scan;
364 
365 	reg_ops->is_there_serv_ready_extn =
366 		tgt_if_regulatory_is_there_serv_ready_extn;
367 
368 	reg_ops->set_user_country_code =
369 		tgt_if_regulatory_set_user_country_code;
370 
371 	reg_ops->register_ch_avoid_event_handler =
372 		tgt_if_regulatory_register_ch_avoid_event_handler;
373 
374 	reg_ops->unregister_ch_avoid_event_handler =
375 		tgt_if_regulatory_unregister_ch_avoid_event_handler;
376 
377 	return QDF_STATUS_SUCCESS;
378 }
379 
380