1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Changes from Qualcomm Innovation Center are provided under the following license:
17 *
18 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted (subject to the limitations in the
22 * disclaimer below) provided that the following conditions are met:
23 *
24 * * Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 *
27 * * Redistributions in binary form must reproduce the above
28 * copyright notice, this list of conditions and the following
29 * disclaimer in the documentation and/or other materials provided
30 * with the distribution.
31 *
32 * * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
35 *
36 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
37 * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
38 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
41 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
42 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
46 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
47 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
48 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 */
50
51 #include <hardware_legacy/wifi_hal.h>
52
53 #ifndef __WIFI_HAL_COMMON_H__
54 #define __WIFI_HAL_COMMON_H__
55
56 #ifndef LOG_TAG
57 #define LOG_TAG "WifiHAL"
58 #endif
59
60 #include <stdint.h>
61 #include <fcntl.h>
62 #include <inttypes.h>
63 #include <sys/socket.h>
64 #include <sys/un.h>
65 #include <netlink/genl/genl.h>
66 #include <netlink/genl/family.h>
67 #include <netlink/genl/ctrl.h>
68 #include <linux/rtnetlink.h>
69 #include <netpacket/packet.h>
70 #include <linux/filter.h>
71 #include <linux/errqueue.h>
72
73 #include <linux/pkt_sched.h>
74 #include <netlink/object-api.h>
75 #include <netlink/netlink.h>
76 #include <netlink/socket.h>
77
78 #include "nl80211_copy.h"
79
80 #include <utils/Log.h>
81 #include "rb_wrapper.h"
82 #include "pkt_stats.h"
83 #include "wifihal_internal.h"
84 #include "qca-vendor_copy.h"
85
86 #define SOCKET_BUFFER_SIZE (32768U)
87 #define RECV_BUF_SIZE (4096)
88 #define DEFAULT_EVENT_CB_SIZE (64)
89 #define NUM_RING_BUFS 5
90 #define MAX_NUM_RADAR_HISTORY 64
91
92 #define WIFI_HAL_CTRL_IFACE "/dev/socket/wifihal/wifihal_ctrlsock"
93
94 #define MAC_ADDR_ARRAY(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
95 #define MAC_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
96 #ifndef BIT
97 #define BIT(x) (1 << (x))
98 #endif
99
100 typedef int16_t s16;
101 typedef int32_t s32;
102 typedef int64_t s64;
103
104 typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
105
106 class WifiCommand;
107
108 typedef struct {
109 int nl_cmd;
110 uint32_t vendor_id;
111 int vendor_subcmd;
112 nl_recvmsg_msg_cb_t cb_func;
113 void *cb_arg;
114 } cb_info;
115
116 typedef struct {
117 wifi_request_id id;
118 WifiCommand *cmd;
119 } cmd_info;
120
121 typedef struct {
122 wifi_handle handle; // handle to wifi data
123 char name[IFNAMSIZ+1]; // interface name + trailing null
124 int id; // id to use when talking to driver
125 } interface_info;
126
127 typedef struct {
128 wifi_gscan_capabilities gscan_capa;
129 wifi_roaming_capabilities roaming_capa;
130 } wifi_capa;
131
132 typedef struct {
133 u8 *flags;
134 size_t flags_len;
135 } features_info;
136
137 enum pkt_log_version {
138 PKT_LOG_V0 = 0, // UNSPECIFIED Target
139 PKT_LOG_V1 = 1, // ROME Base Target
140 PKT_LOG_V2 = 2, // HELIUM Base Target
141 PKT_LOG_V3 = 3, // LETHIUM Base target
142 };
143
144 struct gscan_event_handlers_s;
145 struct rssi_monitor_event_handler_s;
146 struct cld80211_ctx;
147
148 struct ctrl_sock {
149 int s;
150 struct sockaddr_un local;
151 };
152
153 typedef struct hal_info_s {
154
155 struct nl_sock *cmd_sock; // command socket object
156 struct nl_sock *event_sock; // event socket object
157 struct nl_sock *user_sock; // user socket object
158 struct ctrl_sock wifihal_ctrl_sock; // ctrl sock object
159 struct list_head monitor_sockets; // list of monitor sockets
160 int nl80211_family_id; // family id for 80211 driver
161
162 bool in_event_loop; // Indicates that event loop is active
163 bool clean_up; // Indication to clean up the socket
164
165 wifi_internal_event_handler event_handler; // default event handler
166 wifi_cleaned_up_handler cleaned_up_handler; // socket cleaned up handler
167
168 cb_info *event_cb; // event callbacks
169 int num_event_cb; // number of event callbacks
170 int alloc_event_cb; // number of allocated callback objects
171 pthread_mutex_t cb_lock; // mutex for the event_cb access
172
173 interface_info **interfaces; // array of interfaces
174 int num_interfaces; // number of interfaces
175
176 feature_set supported_feature_set;
177 /* driver supported features defined by enum qca_wlan_vendor_features that
178 can be queried by vendor command QCA_NL80211_VENDOR_SUBCMD_GET_FEATURES */
179 features_info driver_supported_features;
180 u32 supported_logger_feature_set;
181 // add other details
182 int user_sock_arg;
183 int event_sock_arg;
184 struct rb_info rb_infos[NUM_RING_BUFS];
185 void (*on_ring_buffer_data) (char *ring_name, char *buffer, int buffer_size,
186 wifi_ring_buffer_status *status);
187 void (*on_alert) (wifi_request_id id, char *buffer, int buffer_size, int err_code);
188 struct pkt_stats_s *pkt_stats;
189
190 /* socket pair used to exit from blocking poll*/
191 int exit_sockets[2];
192 u32 rx_buf_size_allocated;
193 u32 rx_buf_size_occupied;
194 wifi_ring_buffer_entry *rx_aggr_pkts;
195 rx_aggr_stats aggr_stats;
196 u32 prev_seq_no;
197 // pointer to structure having various gscan_event_handlers
198 struct gscan_event_handlers_s *gscan_handlers;
199 struct tcp_param_cmd_handler_s *tcp_param_handler;
200 /* mutex for the log_handler access*/
201 pthread_mutex_t lh_lock;
202 /* mutex for the alert_handler access*/
203 pthread_mutex_t ah_lock;
204 u32 firmware_bus_max_size;
205 bool fate_monitoring_enabled;
206 packet_fate_monitor_info *pkt_fate_stats;
207 /* mutex for the packet fate stats shared resource protection */
208 pthread_mutex_t pkt_fate_stats_lock;
209 struct rssi_monitor_event_handler_s *rssi_handlers;
210 struct radio_event_handler_s *radio_handlers;
211 wifi_capa capa;
212 struct cld80211_ctx *cldctx;
213 bool apf_enabled;
214 bool support_nan_ext_cmd;
215 pkt_log_version pkt_log_ver;
216 qca_wlan_vendor_sar_version sar_version;
217 } hal_info;
218
219 typedef struct {
220 bool radar_detected;
221 u32 freq;
222 u64 clock_boottime;
223 } radar_history_result;
224
wifi_put_le16(u8 * a,u16 val)225 static inline void wifi_put_le16(u8 *a, u16 val) {
226 a[1] = val >> 8;
227 a[0] = val & 0xff;
228 }
229
230 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
231 wifi_error wifi_register_vendor_handler(wifi_handle handle,
232 uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
233
234 void wifi_unregister_handler(wifi_handle handle, int cmd);
235 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
236
237 interface_info *getIfaceInfo(wifi_interface_handle);
238 wifi_handle getWifiHandle(wifi_interface_handle handle);
239 hal_info *getHalInfo(wifi_handle handle);
240 hal_info *getHalInfo(wifi_interface_handle handle);
241 wifi_handle getWifiHandle(hal_info *info);
242 wifi_interface_handle getIfaceHandle(interface_info *info);
243 wifi_error initializeGscanHandlers(hal_info *info);
244 wifi_error cleanupGscanHandlers(hal_info *info);
245 wifi_error initializeRSSIMonitorHandler(hal_info *info);
246 wifi_error cleanupRSSIMonitorHandler(hal_info *info);
247 wifi_error initializeRadioHandler(hal_info *info);
248 wifi_error cleanupRadioHandler(hal_info *info);
249
250 lowi_cb_table_t *getLowiCallbackTable(u32 requested_lowi_capabilities);
251
252 wifi_error wifi_start_sending_offloaded_packet(wifi_request_id id,
253 wifi_interface_handle iface, u8 *ip_packet, u16 ip_packet_len,
254 u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec);
255 wifi_error wifi_stop_sending_offloaded_packet(wifi_request_id id,
256 wifi_interface_handle iface);
257 wifi_error wifi_start_rssi_monitoring(wifi_request_id id, wifi_interface_handle
258 iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
259 wifi_error wifi_stop_rssi_monitoring(wifi_request_id id, wifi_interface_handle iface);
260 wifi_error wifi_set_radio_mode_change_handler(wifi_request_id id, wifi_interface_handle
261 iface, wifi_radio_mode_change_handler eh);
262 wifi_error mapKernelErrortoWifiHalError(int kern_err);
263 void wifi_cleanup_dynamic_ifaces(wifi_handle handle);
264 wifi_error wifi_virtual_interface_create(wifi_handle handle, const char* ifname,
265 wifi_interface_type iface_type);
266 wifi_error wifi_virtual_interface_delete(wifi_handle handle, const char* ifname);
267 wifi_error wifi_get_radar_history(wifi_interface_handle handle,
268 radar_history_result *resultBuf, int resultBufSize, int *numResults);
269 wifi_error wifi_disable_next_cac(wifi_interface_handle handle);
270 // some common macros
271
272 #define min(x, y) ((x) < (y) ? (x) : (y))
273 #define max(x, y) ((x) > (y) ? (x) : (y))
274
275 #define REQUEST_ID_MAX 1000
276 #define REQUEST_ID_U8_MAX 255
277 #define get_requestid() ((arc4random()%REQUEST_ID_MAX) + 1)
278 #define get_requestid_u8() ((arc4random()%REQUEST_ID_U8_MAX) + 1)
279 #define WAIT_TIME_FOR_SET_REG_DOMAIN 50000
280
281 #ifndef UNUSED
282 #define UNUSED(x) (void)(x)
283 #endif
284
285 #ifdef __cplusplus
286 extern "C"
287 {
288 #endif /* __cplusplus */
289 void hexdump(void *bytes, u16 len);
290 u8 get_rssi(u8 rssi_wo_noise_floor);
291 #ifdef __cplusplus
292 }
293 #endif /* __cplusplus */
294
295 #endif
296
297