1 /*
2  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /**
19  * DOC: Target interface file for ftm time sync component to
20  * Implement api's which shall be used by ftm time sync component
21  * in target_if internally.
22  */
23 
24 #include "target_if.h"
25 #include "target_if_ftm_time_sync.h"
26 #include "wlan_ftm_time_sync_public_struct.h"
27 #include "wlan_ftm_time_sync_tgt_api.h"
28 #include <wmi_unified_api.h>
29 
30 static QDF_STATUS
target_if_ftm_time_sync_send_qtime(struct wlan_objmgr_psoc * psoc,uint32_t vdev_id,uint64_t lpass_ts)31 target_if_ftm_time_sync_send_qtime(struct wlan_objmgr_psoc *psoc,
32 				   uint32_t vdev_id, uint64_t lpass_ts)
33 {
34 	wmi_unified_t wmi_hdl;
35 
36 	wmi_hdl = get_wmi_unified_hdl_from_psoc(psoc);
37 	if (!wmi_hdl)
38 		return QDF_STATUS_E_FAILURE;
39 
40 	return wmi_unified_send_wlan_time_sync_qtime(wmi_hdl, vdev_id,
41 						     lpass_ts);
42 }
43 
44 static QDF_STATUS
target_if_ftm_time_sync_send_trigger(struct wlan_objmgr_psoc * psoc,uint32_t vdev_id,bool mode)45 target_if_ftm_time_sync_send_trigger(struct wlan_objmgr_psoc *psoc,
46 				     uint32_t vdev_id, bool mode)
47 {
48 	wmi_unified_t wmi_hdl;
49 
50 	wmi_hdl = get_wmi_unified_hdl_from_psoc(psoc);
51 	if (!wmi_hdl)
52 		return QDF_STATUS_E_FAILURE;
53 
54 	return wmi_unified_send_wlan_time_sync_ftm_trigger(wmi_hdl, vdev_id,
55 							   mode);
56 }
57 
58 static int
target_if_time_sync_ftm_start_stop_event_handler(ol_scn_t scn_handle,uint8_t * data,uint32_t len)59 target_if_time_sync_ftm_start_stop_event_handler(ol_scn_t scn_handle,
60 						 uint8_t *data, uint32_t len)
61 {
62 	struct ftm_time_sync_start_stop_params param;
63 	struct wlan_objmgr_psoc *psoc;
64 	wmi_unified_t wmi_handle;
65 
66 	if (!data) {
67 		target_if_err("Invalid pointer");
68 		return -EINVAL;
69 	}
70 
71 	psoc = target_if_get_psoc_from_scn_hdl(scn_handle);
72 	if (!psoc) {
73 		target_if_err("psoc ptr is NULL");
74 		return -EINVAL;
75 	}
76 
77 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
78 	if (!wmi_handle) {
79 		target_if_err("Invalid wmi handle");
80 		return -EINVAL;
81 	}
82 
83 	if (wmi_unified_extract_time_sync_ftm_start_stop_params(
84 			wmi_handle, data, &param) != QDF_STATUS_SUCCESS) {
85 		target_if_err("Extraction of time sync ftm start stop failed");
86 		return -EINVAL;
87 	}
88 
89 	tgt_ftm_ts_start_stop_evt(psoc, &param);
90 
91 	return 0;
92 }
93 
94 static QDF_STATUS
target_if_ftm_time_sync_start_stop_event(struct wlan_objmgr_psoc * psoc)95 target_if_ftm_time_sync_start_stop_event(struct wlan_objmgr_psoc *psoc)
96 {
97 	QDF_STATUS status;
98 	wmi_unified_t wmi_handle;
99 
100 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
101 	if (!wmi_handle) {
102 		target_if_err("Invalid wmi handle");
103 		return QDF_STATUS_E_INVAL;
104 	}
105 
106 	status = wmi_unified_register_event_handler(
107 			wmi_handle, wmi_wlan_time_sync_ftm_start_stop_event_id,
108 			target_if_time_sync_ftm_start_stop_event_handler,
109 			WMI_RX_SERIALIZER_CTX);
110 	if (status) {
111 		target_if_err("Ftm time_sync start stop event register failed");
112 		return QDF_STATUS_E_FAILURE;
113 	}
114 
115 	return status;
116 }
117 
118 static int
target_if_time_sync_initiator_target_offset_event_handler(ol_scn_t scn_handle,uint8_t * data,uint32_t len)119 target_if_time_sync_initiator_target_offset_event_handler(ol_scn_t scn_handle,
120 						      uint8_t *data,
121 						      uint32_t len)
122 {
123 	struct ftm_time_sync_offset param;
124 	struct wlan_objmgr_psoc *psoc;
125 	wmi_unified_t wmi_handle;
126 
127 	if (!data) {
128 		target_if_err("Invalid pointer");
129 		return -EINVAL;
130 	}
131 
132 	psoc = target_if_get_psoc_from_scn_hdl(scn_handle);
133 	if (!psoc) {
134 		target_if_err("psoc ptr is NULL");
135 		return -EINVAL;
136 	}
137 
138 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
139 	if (!wmi_handle) {
140 		target_if_err("Invalid wmi handle");
141 		return -EINVAL;
142 	}
143 
144 	if (wmi_unified_extract_time_sync_ftm_offset(
145 			wmi_handle, data, &param) != QDF_STATUS_SUCCESS) {
146 		target_if_err("Extraction of time_sync ftm offset param failed");
147 		return -EINVAL;
148 	}
149 
150 	tgt_ftm_ts_offset_evt(psoc, &param);
151 
152 	return 0;
153 }
154 
155 static QDF_STATUS
target_if_ftm_time_sync_initiator_target_offset(struct wlan_objmgr_psoc * psoc)156 target_if_ftm_time_sync_initiator_target_offset(struct wlan_objmgr_psoc *psoc)
157 {
158 	QDF_STATUS status;
159 	wmi_unified_t wmi_handle;
160 
161 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
162 	if (!wmi_handle) {
163 		target_if_err("Invalid wmi handle");
164 		return QDF_STATUS_E_INVAL;
165 	}
166 
167 	status = wmi_unified_register_event_handler(
168 			wmi_handle,
169 			wmi_wlan_time_sync_q_initiator_target_offset_eventid,
170 			target_if_time_sync_initiator_target_offset_event_handler,
171 			WMI_RX_SERIALIZER_CTX);
172 	if (QDF_IS_STATUS_ERROR(status)) {
173 		target_if_err("Ftm time_sync offset event register failed");
174 		return QDF_STATUS_E_FAILURE;
175 	}
176 
177 	return status;
178 }
179 
180 QDF_STATUS
target_if_ftm_time_sync_unregister_ev_handlers(struct wlan_objmgr_psoc * psoc)181 target_if_ftm_time_sync_unregister_ev_handlers(struct wlan_objmgr_psoc *psoc)
182 {
183 	wmi_unified_t wmi_handle;
184 	QDF_STATUS ret, status = QDF_STATUS_SUCCESS;
185 
186 	wmi_handle = get_wmi_unified_hdl_from_psoc(psoc);
187 	if (!wmi_handle) {
188 		target_if_err("Invalid wmi handle");
189 		return QDF_STATUS_E_INVAL;
190 	}
191 
192 	ret = wmi_unified_unregister_event(
193 				wmi_handle,
194 				wmi_wlan_time_sync_ftm_start_stop_event_id);
195 	if (QDF_IS_STATUS_ERROR(ret)) {
196 		target_if_err("failed to unregister time sync start/stop evt");
197 		status = ret;
198 	}
199 
200 	ret = wmi_unified_unregister_event(
201 			wmi_handle,
202 			wmi_wlan_time_sync_q_initiator_target_offset_eventid);
203 	if (QDF_IS_STATUS_ERROR(ret)) {
204 		target_if_err("failed to unregister time sync offset evt");
205 		status = ret;
206 	}
207 
208 	if (QDF_IS_STATUS_ERROR(status))
209 		return QDF_STATUS_E_FAILURE;
210 	else
211 		return QDF_STATUS_SUCCESS;
212 }
213 
target_if_ftm_time_sync_register_rx_ops(struct wlan_ftm_time_sync_rx_ops * rx_ops)214 void target_if_ftm_time_sync_register_rx_ops(
215 				struct wlan_ftm_time_sync_rx_ops *rx_ops)
216 {
217 	if (!rx_ops) {
218 		target_if_err("FTM time_sync rx_ops is null");
219 		return;
220 	}
221 
222 	rx_ops->ftm_time_sync_register_start_stop =
223 				target_if_ftm_time_sync_start_stop_event;
224 	rx_ops->ftm_time_sync_regiser_initiator_target_offset =
225 				target_if_ftm_time_sync_initiator_target_offset;
226 }
227 
target_if_ftm_time_sync_register_tx_ops(struct wlan_ftm_time_sync_tx_ops * tx_ops)228 void target_if_ftm_time_sync_register_tx_ops(
229 				struct wlan_ftm_time_sync_tx_ops *tx_ops)
230 {
231 	if (!tx_ops) {
232 		target_if_err("FTM time_sync tx_ops is null");
233 		return;
234 	}
235 
236 	tx_ops->ftm_time_sync_send_qtime = target_if_ftm_time_sync_send_qtime;
237 	tx_ops->ftm_time_sync_send_trigger =
238 				target_if_ftm_time_sync_send_trigger;
239 }
240 
241