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: Public API implementation of ftm time_sync called by north bound iface.
20  */
21 
22 #include "ftm_time_sync_ucfg_api.h"
23 #include "ftm_time_sync_main.h"
24 #include <qdf_str.h>
25 
ucfg_ftm_time_sync_init(void)26 QDF_STATUS ucfg_ftm_time_sync_init(void)
27 {
28 	QDF_STATUS status;
29 
30 	status = wlan_objmgr_register_psoc_create_handler(
31 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
32 				ftm_time_sync_psoc_create_notification,
33 				NULL);
34 	if (QDF_IS_STATUS_ERROR(status)) {
35 		ftm_time_sync_err("Failed to register psoc create handler");
36 		return status;
37 	}
38 
39 	status = wlan_objmgr_register_psoc_destroy_handler(
40 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
41 				ftm_time_sync_psoc_destroy_notification,
42 				NULL);
43 	if (QDF_IS_STATUS_ERROR(status)) {
44 		ftm_time_sync_err("Failed to register psoc delete handler");
45 		goto fail_destroy_psoc;
46 	}
47 
48 	status = wlan_objmgr_register_vdev_create_handler(
49 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
50 				ftm_time_sync_vdev_create_notification, NULL);
51 	if (QDF_IS_STATUS_ERROR(status)) {
52 		ftm_time_sync_err("Failed to register vdev create handler");
53 		goto fail_create_vdev;
54 	}
55 
56 	status = wlan_objmgr_register_vdev_destroy_handler(
57 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
58 				ftm_time_sync_vdev_destroy_notification, NULL);
59 	if (QDF_IS_STATUS_ERROR(status)) {
60 		ftm_time_sync_err("Failed to register vdev destroy handler");
61 		goto fail_destroy_vdev;
62 	}
63 	return status;
64 
65 fail_destroy_vdev:
66 	wlan_objmgr_unregister_vdev_create_handler(
67 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
68 				ftm_time_sync_vdev_create_notification, NULL);
69 
70 fail_create_vdev:
71 	wlan_objmgr_unregister_psoc_destroy_handler(
72 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
73 				ftm_time_sync_psoc_destroy_notification, NULL);
74 
75 fail_destroy_psoc:
76 	wlan_objmgr_unregister_psoc_create_handler(
77 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
78 				ftm_time_sync_psoc_create_notification, NULL);
79 
80 	return status;
81 }
82 
ucfg_ftm_time_sync_deinit(void)83 void ucfg_ftm_time_sync_deinit(void)
84 {
85 	QDF_STATUS status;
86 
87 	status = wlan_objmgr_unregister_vdev_destroy_handler(
88 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
89 				ftm_time_sync_vdev_destroy_notification,
90 				NULL);
91 	if (QDF_IS_STATUS_ERROR(status))
92 		ftm_time_sync_err("Failed to unregister vdev delete handler");
93 
94 	status = wlan_objmgr_unregister_vdev_create_handler(
95 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
96 				ftm_time_sync_vdev_create_notification, NULL);
97 	if (!QDF_IS_STATUS_ERROR(status))
98 		ftm_time_sync_err("Failed to unregister vdev create handler");
99 
100 	status = wlan_objmgr_unregister_psoc_destroy_handler(
101 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
102 				ftm_time_sync_psoc_destroy_notification,
103 				NULL);
104 	if (QDF_IS_STATUS_ERROR(status))
105 		ftm_time_sync_err("Failed to unregister psoc destroy handler");
106 
107 	status = wlan_objmgr_unregister_psoc_create_handler(
108 				WLAN_UMAC_COMP_FTM_TIME_SYNC,
109 				ftm_time_sync_psoc_create_notification,
110 				NULL);
111 	if (QDF_IS_STATUS_ERROR(status))
112 		ftm_time_sync_err("Failed to unregister psoc create handler");
113 }
114 
ucfg_is_ftm_time_sync_enable(struct wlan_objmgr_psoc * psoc)115 bool  ucfg_is_ftm_time_sync_enable(struct wlan_objmgr_psoc *psoc)
116 {
117 	return ftm_time_sync_is_enable(psoc);
118 }
119 
ucfg_ftm_time_sync_set_enable(struct wlan_objmgr_psoc * psoc,bool enable)120 void ucfg_ftm_time_sync_set_enable(struct wlan_objmgr_psoc *psoc, bool enable)
121 {
122 	return ftm_time_sync_set_enable(psoc, enable);
123 }
124 
ucfg_ftm_time_sync_update_sta_connect_state(struct wlan_objmgr_vdev * vdev,enum ftm_time_sync_sta_state sta_state,struct qdf_mac_addr bssid)125 void ucfg_ftm_time_sync_update_sta_connect_state(
126 					struct wlan_objmgr_vdev *vdev,
127 					enum ftm_time_sync_sta_state sta_state,
128 					struct qdf_mac_addr bssid)
129 {
130 	struct wlan_objmgr_psoc *psoc;
131 	enum ftm_time_sync_role role;
132 
133 	psoc = wlan_vdev_get_psoc(vdev);
134 	if (!psoc) {
135 		ftm_time_sync_err("Failed to get psoc");
136 		return;
137 	}
138 
139 	role = ftm_time_sync_get_role(psoc);
140 	if (role == FTM_TIMESYNC_TARGET_ROLE) {
141 		if (sta_state == FTM_TIME_SYNC_STA_CONNECTED)
142 			ftm_time_sync_send_trigger(vdev);
143 		else
144 			ftm_time_sync_stop(vdev);
145 
146 		ftm_time_sync_update_bssid(vdev, bssid);
147 	}
148 }
149 
ucfg_ftm_time_sync_update_bss_state(struct wlan_objmgr_vdev * vdev,enum ftm_time_sync_bss_state ap_state)150 void ucfg_ftm_time_sync_update_bss_state(struct wlan_objmgr_vdev *vdev,
151 					 enum ftm_time_sync_bss_state ap_state)
152 {
153 	struct wlan_objmgr_psoc *psoc;
154 	enum ftm_time_sync_role role;
155 
156 	psoc = wlan_vdev_get_psoc(vdev);
157 	if (!psoc) {
158 		ftm_time_sync_err("Failed to get psoc");
159 		return;
160 	}
161 
162 	if (!ftm_time_sync_is_enable(psoc))
163 		return;
164 
165 	role = ftm_time_sync_get_role(psoc);
166 	if (role == FTM_TIMESYNC_INITIATOR_ROLE) {
167 		if (ap_state == FTM_TIME_SYNC_BSS_STARTED)
168 			ftm_time_sync_send_trigger(vdev);
169 		else
170 			ftm_time_sync_stop(vdev);
171 	}
172 }
173 
ucfg_ftm_time_sync_show(struct wlan_objmgr_vdev * vdev,char * buf)174 ssize_t ucfg_ftm_time_sync_show(struct wlan_objmgr_vdev *vdev, char *buf)
175 {
176 	return ftm_time_sync_show(vdev, buf);
177 }
178