xref: /wlan-dirver/qca-wifi-host-cmn/target_if/regulatory/src/target_if_reg.c (revision 8ddef7dd9a290d4a9b1efd5d3efacf51d78a1a0d)
1 /*
2  * Copyright (c) 2017-2019 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 interfaces.
23  */
24 
25 #include <wmi_unified_api.h>
26 #include <reg_services_public_struct.h>
27 #include <wlan_reg_tgt_api.h>
28 #include <target_if.h>
29 #include <target_if_reg.h>
30 #include <wmi_unified_reg_api.h>
31 #include <qdf_platform.h>
32 #include <target_if_reg_11d.h>
33 #include <target_if_reg_lte.h>
34 #include <wlan_reg_ucfg_api.h>
35 
36 /**
37  * get_chan_list_cc_event_id() - Get chan_list_cc event i
38  *
39  * Return: Event id
40  */
41 static inline uint32_t get_chan_list_cc_event_id(void)
42 {
43 	return wmi_reg_chan_list_cc_event_id;
44 }
45 
46 /**
47  * tgt_if_regulatory_is_regdb_offloaded() - Check if regdb is offloaded
48  * @psoc: Pointer to psoc
49  *
50  * Return: true if regdb if offloaded, else false
51  */
52 static bool tgt_if_regulatory_is_regdb_offloaded(struct wlan_objmgr_psoc *psoc)
53 {
54 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
55 
56 	if (!wmi_handle)
57 		return false;
58 
59 	return wmi_service_enabled(wmi_handle, wmi_service_regulatory_db);
60 }
61 
62 /**
63  * tgt_if_regulatory_is_there_serv_ready_extn() - Check for service ready
64  * extension
65  * @psoc: Pointer to psoc object
66  *
67  * Return: true if service ready extension is present, else false.
68  */
69 static bool tgt_if_regulatory_is_there_serv_ready_extn(
70 		struct wlan_objmgr_psoc *psoc)
71 {
72 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
73 
74 	if (!wmi_handle)
75 		return false;
76 
77 	return wmi_service_enabled(wmi_handle, wmi_service_ext_msg);
78 }
79 
80 /**
81  * target_if_regulatory_get_rx_ops() - Get regdb rx ops
82  * @psoc: Pointer to psoc object
83  *
84  * Return: Reg rx_ops
85  */
86 struct wlan_lmac_if_reg_rx_ops *
87 target_if_regulatory_get_rx_ops(struct wlan_objmgr_psoc *psoc)
88 {
89 	return &psoc->soc_cb.rx_ops.reg_rx_ops;
90 }
91 
92 QDF_STATUS target_if_reg_set_offloaded_info(struct wlan_objmgr_psoc *psoc)
93 {
94 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
95 
96 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
97 	if (!reg_rx_ops) {
98 		target_if_err("reg_rx_ops is NULL");
99 		return QDF_STATUS_E_FAILURE;
100 	}
101 
102 	if (reg_rx_ops->reg_set_regdb_offloaded)
103 		reg_rx_ops->reg_set_regdb_offloaded(
104 				psoc,
105 				tgt_if_regulatory_is_regdb_offloaded(psoc));
106 
107 	if (reg_rx_ops->reg_set_11d_offloaded)
108 		reg_rx_ops->reg_set_11d_offloaded(
109 				psoc, tgt_if_regulatory_is_11d_offloaded(psoc));
110 
111 	return QDF_STATUS_SUCCESS;
112 }
113 
114 /**
115  * tgt_reg_chan_list_update_handler() - Channel list update handler
116  * @handle: scn handle
117  * @event_buf: pointer to event buffer
118  * @len: buffer length
119  *
120  * Return: 0 on success
121  */
122 static int tgt_reg_chan_list_update_handler(ol_scn_t handle, uint8_t *event_buf,
123 					    uint32_t len)
124 {
125 	struct wlan_objmgr_psoc *psoc;
126 	struct wlan_lmac_if_reg_rx_ops *reg_rx_ops;
127 	struct cur_regulatory_info *reg_info;
128 	QDF_STATUS status;
129 	struct wmi_unified *wmi_handle;
130 	int ret_val = 0;
131 
132 	TARGET_IF_ENTER();
133 
134 	psoc = target_if_get_psoc_from_scn_hdl(handle);
135 	if (!psoc) {
136 		target_if_err("psoc ptr is NULL");
137 		return -EINVAL;
138 	}
139 
140 	reg_rx_ops = target_if_regulatory_get_rx_ops(psoc);
141 	if (!reg_rx_ops->master_list_handler) {
142 		target_if_err("master_list_handler is NULL");
143 		return -EINVAL;
144 	}
145 
146 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
147 	if (!wmi_handle) {
148 		target_if_err("invalid wmi handle");
149 		return -EINVAL;
150 	}
151 
152 	reg_info = qdf_mem_malloc(sizeof(*reg_info));
153 	if (!reg_info)
154 		return -ENOMEM;
155 
156 	if (wmi_extract_reg_chan_list_update_event(wmi_handle,
157 						   event_buf, reg_info, len)
158 	    != QDF_STATUS_SUCCESS) {
159 		target_if_err("Extraction of channel list event failed");
160 		ret_val = -EFAULT;
161 		goto clean;
162 	}
163 
164 	if (reg_info->phy_id >= PSOC_MAX_PHY_REG_CAP) {
165 		target_if_err_rl("phy_id %d is out of bounds",
166 				 reg_info->phy_id);
167 		ret_val = -EFAULT;
168 		goto clean;
169 	}
170 
171 	reg_info->psoc = psoc;
172 
173 	status = reg_rx_ops->master_list_handler(reg_info);
174 	if (status != QDF_STATUS_SUCCESS) {
175 		target_if_err("Failed to process master channel list handler");
176 		ret_val = -EFAULT;
177 	}
178 
179 clean:
180 	qdf_mem_free(reg_info->reg_rules_2g_ptr);
181 	qdf_mem_free(reg_info->reg_rules_5g_ptr);
182 	qdf_mem_free(reg_info);
183 
184 	target_if_debug("processed reg channel list ret_val %d", ret_val);
185 
186 	return ret_val;
187 }
188 
189 /**
190  * tgt_if_regulatory_register_master_list_handler() - Register master channel
191  * list
192  * @psoc: Pointer to psoc
193  * @arg: Pointer to argument list
194  *
195  * Return: QDF_STATUS
196  */
197 static QDF_STATUS tgt_if_regulatory_register_master_list_handler(
198 	struct wlan_objmgr_psoc *psoc, void *arg)
199 {
200 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
201 
202 	if (!wmi_handle)
203 		return QDF_STATUS_E_FAILURE;
204 
205 	return wmi_unified_register_event_handler(
206 			wmi_handle, wmi_reg_chan_list_cc_event_id,
207 			tgt_reg_chan_list_update_handler, WMI_RX_UMAC_CTX);
208 }
209 
210 /**
211  * tgt_if_regulatory_unregister_master_list_handler() - Unregister master
212  * channel list
213  * @psoc: Pointer to psoc
214  * @arg: Pointer to argument list
215  *
216  * Return: QDF_STATUS
217  */
218 static QDF_STATUS tgt_if_regulatory_unregister_master_list_handler(
219 	struct wlan_objmgr_psoc *psoc, void *arg)
220 {
221 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
222 
223 	if (!wmi_handle)
224 		return QDF_STATUS_E_FAILURE;
225 
226 	return wmi_unified_unregister_event_handler(
227 			wmi_handle, wmi_reg_chan_list_cc_event_id);
228 }
229 
230 /**
231  * tgt_if_regulatory_set_country_code() - Set country code
232  * @psoc: Pointer to psoc
233  * @arg: Pointer to argument list
234  *
235  * Return: QDF_STATUS
236  */
237 static QDF_STATUS tgt_if_regulatory_set_country_code(
238 	struct wlan_objmgr_psoc *psoc, void *arg)
239 {
240 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
241 
242 	if (!wmi_handle)
243 		return QDF_STATUS_E_FAILURE;
244 
245 	return wmi_unified_set_country_cmd_send(wmi_handle, arg);
246 }
247 
248 /**
249  * tgt_if_regulatory_set_user_country_code() - Set user country code
250  * @psoc: Pointer to psoc
251  * @pdev_id: Pdev id
252  * @rd: Pointer to regdomain structure
253  *
254  * Return: QDF_STATUS
255  */
256 static QDF_STATUS tgt_if_regulatory_set_user_country_code(
257 	struct wlan_objmgr_psoc *psoc, uint8_t pdev_id, struct cc_regdmn_s *rd)
258 {
259 	wmi_unified_t wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
260 
261 	if (!wmi_handle)
262 		return QDF_STATUS_E_FAILURE;
263 
264 	if (wmi_unified_set_user_country_code_cmd_send(
265 				wmi_handle, pdev_id, rd) != QDF_STATUS_SUCCESS
266 			) {
267 		target_if_err("Set user country code failed");
268 		return QDF_STATUS_E_FAILURE;
269 	}
270 
271 	return QDF_STATUS_SUCCESS;
272 }
273 
274 QDF_STATUS tgt_if_regulatory_modify_freq_range(struct wlan_objmgr_psoc *psoc)
275 {
276 	struct wlan_psoc_host_hal_reg_capabilities_ext *reg_cap;
277 
278 	reg_cap = ucfg_reg_get_hal_reg_cap(psoc);
279 	if (!reg_cap) {
280 		target_if_err("reg cap is NULL");
281 		return QDF_STATUS_E_FAILURE;
282 	}
283 
284 	if (!(reg_cap->wireless_modes & WMI_HOST_REGDMN_MODE_11A)) {
285 		reg_cap->low_5ghz_chan = 0;
286 		reg_cap->high_5ghz_chan = 0;
287 	}
288 
289 	if (!(reg_cap->wireless_modes &
290 	     (WMI_HOST_REGDMN_MODE_11B | WMI_HOST_REGDMN_MODE_PUREG))) {
291 		reg_cap->low_2ghz_chan = 0;
292 		reg_cap->high_2ghz_chan = 0;
293 	}
294 
295 	target_if_debug("phy_id = %d - low_2ghz_chan = %d high_2ghz_chan = %d low_5ghz_chan = %d high_5ghz_chan = %d",
296 			reg_cap->phy_id,
297 			reg_cap->low_2ghz_chan,
298 			reg_cap->high_2ghz_chan,
299 			reg_cap->low_5ghz_chan,
300 			reg_cap->high_5ghz_chan);
301 
302 	return QDF_STATUS_SUCCESS;
303 }
304 
305 QDF_STATUS target_if_register_regulatory_tx_ops(
306 		struct wlan_lmac_if_tx_ops *tx_ops)
307 {
308 	struct wlan_lmac_if_reg_tx_ops *reg_ops = &tx_ops->reg_ops;
309 
310 	reg_ops->register_master_handler =
311 		tgt_if_regulatory_register_master_list_handler;
312 
313 	reg_ops->unregister_master_handler =
314 		tgt_if_regulatory_unregister_master_list_handler;
315 
316 	reg_ops->set_country_code = tgt_if_regulatory_set_country_code;
317 
318 	reg_ops->fill_umac_legacy_chanlist = NULL;
319 
320 	reg_ops->set_country_failed = NULL;
321 
322 	reg_ops->register_11d_new_cc_handler =
323 		tgt_if_regulatory_register_11d_new_cc_handler;
324 
325 	reg_ops->unregister_11d_new_cc_handler =
326 		tgt_if_regulatory_unregister_11d_new_cc_handler;
327 
328 	reg_ops->start_11d_scan = tgt_if_regulatory_start_11d_scan;
329 
330 	reg_ops->stop_11d_scan = tgt_if_regulatory_stop_11d_scan;
331 
332 	reg_ops->is_there_serv_ready_extn =
333 		tgt_if_regulatory_is_there_serv_ready_extn;
334 
335 	reg_ops->set_user_country_code =
336 		tgt_if_regulatory_set_user_country_code;
337 
338 	reg_ops->register_ch_avoid_event_handler =
339 		tgt_if_regulatory_register_ch_avoid_event_handler;
340 
341 	reg_ops->unregister_ch_avoid_event_handler =
342 		tgt_if_regulatory_unregister_ch_avoid_event_handler;
343 
344 	return QDF_STATUS_SUCCESS;
345 }
346