1 /*
2  * Copyright (C) 2016 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 
17 #ifndef __WIFI_HAL_H__
18 #define __WIFI_HAL_H__
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 #include <stdint.h>
25 
26 /* WiFi Common definitions */
27 /* channel operating width */
28 typedef enum {
29     WIFI_CHAN_WIDTH_20    = 0,
30     WIFI_CHAN_WIDTH_40    = 1,
31     WIFI_CHAN_WIDTH_80    = 2,
32     WIFI_CHAN_WIDTH_160   = 3,
33     WIFI_CHAN_WIDTH_80P80 = 4,
34     WIFI_CHAN_WIDTH_5     = 5,
35     WIFI_CHAN_WIDTH_10    = 6,
36     WIFI_CHAN_WIDTH_INVALID = -1
37 } wifi_channel_width;
38 
39 typedef int wifi_radio;
40 typedef int wifi_channel;
41 
42 typedef struct {
43     wifi_channel_width width;
44     int center_frequency0;
45     int center_frequency1;
46     int primary_frequency;
47 } wifi_channel_spec;
48 
49 typedef enum {
50     WIFI_SUCCESS = 0,
51     WIFI_ERROR_NONE = 0,
52     WIFI_ERROR_UNKNOWN = -1,
53     WIFI_ERROR_UNINITIALIZED = -2,
54     WIFI_ERROR_NOT_SUPPORTED = -3,
55     WIFI_ERROR_NOT_AVAILABLE = -4,              // Not available right now, but try later
56     WIFI_ERROR_INVALID_ARGS = -5,
57     WIFI_ERROR_INVALID_REQUEST_ID = -6,
58     WIFI_ERROR_TIMED_OUT = -7,
59     WIFI_ERROR_TOO_MANY_REQUESTS = -8,          // Too many instances of this request
60     WIFI_ERROR_OUT_OF_MEMORY = -9,
61     WIFI_ERROR_BUSY = -10,
62 } wifi_error;
63 
64 typedef unsigned char byte;
65 typedef unsigned char u8;
66 typedef signed char s8;
67 typedef uint16_t u16;
68 typedef uint32_t u32;
69 typedef int32_t s32;
70 typedef uint64_t u64;
71 typedef int64_t s64;
72 typedef int wifi_request_id;
73 typedef int wifi_channel;                       // indicates channel frequency in MHz
74 typedef int wifi_rssi;
75 typedef byte mac_addr[6];
76 typedef byte oui[3];
77 typedef int64_t wifi_timestamp;                 // In microseconds (us)
78 typedef int64_t wifi_timespan;                  // In picoseconds  (ps)
79 
80 struct wifi_info;
81 struct wifi_interface_info;
82 typedef struct wifi_info *wifi_handle;
83 typedef struct wifi_interface_info *wifi_interface_handle;
84 
85 /* Initialize/Cleanup */
86 
87 wifi_error wifi_initialize(wifi_handle *handle);
88 typedef void (*wifi_cleaned_up_handler) (wifi_handle handle);
89 void wifi_cleanup(wifi_handle handle, wifi_cleaned_up_handler handler);
90 void wifi_event_loop(wifi_handle handle);
91 
92 /* Error handling */
93 void wifi_get_error_info(wifi_error err, const char **msg); // return a pointer to a static string
94 
95 /* Feature enums */
96 #define WIFI_FEATURE_INFRA              0x0001      // Basic infrastructure mode
97 #define WIFI_FEATURE_INFRA_5G           0x0002      // Support for 5 GHz Band
98 #define WIFI_FEATURE_HOTSPOT            0x0004      // Support for GAS/ANQP
99 #define WIFI_FEATURE_P2P                0x0008      // Wifi-Direct
100 #define WIFI_FEATURE_SOFT_AP            0x0010      // Soft AP
101 #define WIFI_FEATURE_GSCAN              0x0020      // Google-Scan APIs
102 #define WIFI_FEATURE_NAN                0x0040      // Neighbor Awareness Networking
103 #define WIFI_FEATURE_D2D_RTT            0x0080      // Device-to-device RTT
104 #define WIFI_FEATURE_D2AP_RTT           0x0100      // Device-to-AP RTT
105 #define WIFI_FEATURE_BATCH_SCAN         0x0200      // Batched Scan (legacy)
106 #define WIFI_FEATURE_PNO                0x0400      // Preferred network offload
107 #define WIFI_FEATURE_ADDITIONAL_STA     0x0800      // Support for two STAs
108 #define WIFI_FEATURE_TDLS               0x1000      // Tunnel directed link setup
109 #define WIFI_FEATURE_TDLS_OFFCHANNEL    0x2000      // Support for TDLS off channel
110 #define WIFI_FEATURE_EPR                0x4000      // Enhanced power reporting
111 #define WIFI_FEATURE_AP_STA             0x8000      // Support for AP STA Concurrency
112 #define WIFI_FEATURE_LINK_LAYER_STATS   0x10000     // Link layer stats collection
113 #define WIFI_FEATURE_LOGGER             0x20000     // WiFi Logger
114 #define WIFI_FEATURE_HAL_EPNO           0x40000     // WiFi PNO enhanced
115 #define WIFI_FEATURE_RSSI_MONITOR       0x80000     // RSSI Monitor
116 #define WIFI_FEATURE_MKEEP_ALIVE        0x100000    // WiFi mkeep_alive
117 #define WIFI_FEATURE_CONFIG_NDO         0x200000    // ND offload configure
118 #define WIFI_FEATURE_TX_TRANSMIT_POWER  0x400000    // Capture Tx transmit power levels
119 #define WIFI_FEATURE_CONTROL_ROAMING    0x800000    // Enable/Disable firmware roaming
120 #define WIFI_FEATURE_IE_WHITELIST       0x1000000   // Support Probe IE white listing
121 #define WIFI_FEATURE_SCAN_RAND          0x2000000   // Support MAC & Probe Sequence Number randomization
122 // Add more features here
123 
124 
125 typedef int feature_set;
126 
127 #define IS_MASK_SET(mask, flags)        (((flags) & (mask)) == (mask))
128 
129 #define IS_SUPPORTED_FEATURE(feature, featureSet)       IS_MASK_SET(feature, featureSet)
130 
131 /* Feature set */
132 wifi_error wifi_get_supported_feature_set(wifi_interface_handle handle, feature_set *set);
133 
134 /*
135  * Each row represents a valid feature combination;
136  * all other combinations are invalid!
137  */
138 wifi_error wifi_get_concurrency_matrix(wifi_interface_handle handle, int set_size_max,
139         feature_set set[], int *set_size);
140 
141 /* multiple interface support */
142 
143 wifi_error wifi_get_ifaces(wifi_handle handle, int *num_ifaces, wifi_interface_handle **ifaces);
144 wifi_error wifi_get_iface_name(wifi_interface_handle iface, char *name, size_t size);
145 wifi_interface_handle wifi_get_iface_handle(wifi_handle handle, char *name);
146 
147 /* Configuration events */
148 
149 typedef struct {
150     void (*on_country_code_changed)(char code[2]);      // We can get this from supplicant too
151 
152     // More event handlers
153 } wifi_event_handler;
154 
155 typedef struct {
156         void (*on_rssi_threshold_breached)(wifi_request_id id, u8 *cur_bssid, s8 cur_rssi);
157 } wifi_rssi_event_handler;
158 
159 wifi_error wifi_set_iface_event_handler(wifi_request_id id, wifi_interface_handle iface, wifi_event_handler eh);
160 wifi_error wifi_reset_iface_event_handler(wifi_request_id id, wifi_interface_handle iface);
161 
162 wifi_error wifi_set_nodfs_flag(wifi_interface_handle handle, u32 nodfs);
163 
164 typedef struct rx_data_cnt_details_t {
165     int rx_unicast_cnt;     /*Total rx unicast packet which woke up host */
166     int rx_multicast_cnt;   /*Total rx multicast packet which woke up host */
167     int rx_broadcast_cnt;   /*Total rx broadcast packet which woke up host */
168 } RX_DATA_WAKE_CNT_DETAILS;
169 
170 typedef struct rx_wake_pkt_type_classification_t {
171     int icmp_pkt;   /*wake icmp packet count */
172     int icmp6_pkt;  /*wake icmp6 packet count */
173     int icmp6_ra;   /*wake icmp6 RA packet count */
174     int icmp6_na;   /*wake icmp6 NA packet count */
175     int icmp6_ns;   /*wake icmp6 NS packet count */
176     //ToDo: Any more interesting classification to add?
177 } RX_WAKE_PKT_TYPE_CLASSFICATION;
178 
179 typedef struct rx_multicast_cnt_t{
180     int ipv4_rx_multicast_addr_cnt; /*Rx wake packet was ipv4 multicast */
181     int ipv6_rx_multicast_addr_cnt; /*Rx wake packet was ipv6 multicast */
182     int other_rx_multicast_addr_cnt;/*Rx wake packet was non-ipv4 and non-ipv6*/
183 } RX_MULTICAST_WAKE_DATA_CNT;
184 
185 /*
186  * Structure holding all the driver/firmware wake count reasons.
187  *
188  * Buffers for the array fields (cmd_event_wake_cnt/driver_fw_local_wake_cnt)
189  * are allocated and freed by the framework. The size of each allocated
190  * array is indicated by the corresponding |_cnt| field. HAL needs to fill in
191  * the corresponding |_used| field to indicate the number of elements used in
192  * the array.
193  */
194 typedef struct wlan_driver_wake_reason_cnt_t {
195     int total_cmd_event_wake;    /* Total count of cmd event wakes */
196     int *cmd_event_wake_cnt;     /* Individual wake count array, each index a reason */
197     int cmd_event_wake_cnt_sz;   /* Max number of cmd event wake reasons */
198     int cmd_event_wake_cnt_used; /* Number of cmd event wake reasons specific to the driver */
199 
200     int total_driver_fw_local_wake;    /* Total count of drive/fw wakes, for local reasons */
201     int *driver_fw_local_wake_cnt;     /* Individual wake count array, each index a reason */
202     int driver_fw_local_wake_cnt_sz;   /* Max number of local driver/fw wake reasons */
203     int driver_fw_local_wake_cnt_used; /* Number of local driver/fw wake reasons specific to the driver */
204 
205     int total_rx_data_wake;     /* total data rx packets, that woke up host */
206     RX_DATA_WAKE_CNT_DETAILS rx_wake_details;
207     RX_WAKE_PKT_TYPE_CLASSFICATION rx_wake_pkt_classification_info;
208     RX_MULTICAST_WAKE_DATA_CNT rx_multicast_wake_pkt_info;
209 } WLAN_DRIVER_WAKE_REASON_CNT;
210 
211 
212 
213 /* include various feature headers */
214 
215 #include "gscan.h"
216 #include "link_layer_stats.h"
217 #include "rtt.h"
218 #include "tdls.h"
219 #include "wifi_logger.h"
220 #include "wifi_config.h"
221 #include "wifi_nan.h"
222 #include "wifi_offload.h"
223 #include "roam.h"
224 
225 //wifi HAL function pointer table
226 typedef struct {
227     wifi_error (* wifi_initialize) (wifi_handle *);
228     void (* wifi_cleanup) (wifi_handle, wifi_cleaned_up_handler);
229     void (*wifi_event_loop)(wifi_handle);
230     void (* wifi_get_error_info) (wifi_error , const char **);
231     wifi_error (* wifi_get_supported_feature_set) (wifi_interface_handle, feature_set *);
232     wifi_error (* wifi_get_concurrency_matrix) (wifi_interface_handle, int, feature_set *, int *);
233     wifi_error (* wifi_set_scanning_mac_oui) (wifi_interface_handle, unsigned char *);
234     wifi_error (* wifi_get_supported_channels)(wifi_handle, int *, wifi_channel *);
235     wifi_error (* wifi_is_epr_supported)(wifi_handle);
236     wifi_error (* wifi_get_ifaces) (wifi_handle , int *, wifi_interface_handle **);
237     wifi_error (* wifi_get_iface_name) (wifi_interface_handle, char *name, size_t);
238     wifi_error (* wifi_set_iface_event_handler) (wifi_request_id,wifi_interface_handle ,
239             wifi_event_handler);
240     wifi_error (* wifi_reset_iface_event_handler) (wifi_request_id, wifi_interface_handle);
241     wifi_error (* wifi_start_gscan) (wifi_request_id, wifi_interface_handle, wifi_scan_cmd_params,
242             wifi_scan_result_handler);
243     wifi_error (* wifi_stop_gscan)(wifi_request_id, wifi_interface_handle);
244     wifi_error (* wifi_get_cached_gscan_results)(wifi_interface_handle, byte, int,
245             wifi_cached_scan_results *, int *);
246     wifi_error (* wifi_set_bssid_hotlist)(wifi_request_id, wifi_interface_handle,
247             wifi_bssid_hotlist_params, wifi_hotlist_ap_found_handler);
248     wifi_error (* wifi_reset_bssid_hotlist)(wifi_request_id, wifi_interface_handle);
249     wifi_error (* wifi_set_significant_change_handler)(wifi_request_id, wifi_interface_handle,
250             wifi_significant_change_params, wifi_significant_change_handler);
251     wifi_error (* wifi_reset_significant_change_handler)(wifi_request_id, wifi_interface_handle);
252     wifi_error (* wifi_get_gscan_capabilities)(wifi_interface_handle, wifi_gscan_capabilities *);
253     wifi_error (* wifi_set_link_stats) (wifi_interface_handle, wifi_link_layer_params);
254     wifi_error (* wifi_get_link_stats) (wifi_request_id,wifi_interface_handle,
255             wifi_stats_result_handler);
256     wifi_error (* wifi_clear_link_stats)(wifi_interface_handle,u32, u32 *, u8, u8 *);
257     wifi_error (* wifi_get_valid_channels)(wifi_interface_handle,int, int, wifi_channel *, int *);
258     wifi_error (* wifi_rtt_range_request)(wifi_request_id, wifi_interface_handle, unsigned,
259             wifi_rtt_config[], wifi_rtt_event_handler);
260     wifi_error (* wifi_rtt_range_cancel)(wifi_request_id,  wifi_interface_handle, unsigned,
261             mac_addr[]);
262     wifi_error (* wifi_get_rtt_capabilities)(wifi_interface_handle, wifi_rtt_capabilities *);
263     wifi_error (* wifi_rtt_get_responder_info)(wifi_interface_handle iface,
264             wifi_rtt_responder *responder_info);
265     wifi_error (* wifi_enable_responder)(wifi_request_id id, wifi_interface_handle iface,
266             wifi_channel_info channel_hint, unsigned max_duration_seconds,
267             wifi_rtt_responder *responder_info);
268     wifi_error (* wifi_disable_responder)(wifi_request_id id, wifi_interface_handle iface);
269     wifi_error (* wifi_set_nodfs_flag)(wifi_interface_handle, u32);
270     wifi_error (* wifi_start_logging)(wifi_interface_handle, u32, u32, u32, u32, char *);
271     wifi_error (* wifi_set_epno_list)(wifi_request_id, wifi_interface_handle,
272             const wifi_epno_params *, wifi_epno_handler);
273     wifi_error (* wifi_reset_epno_list)(wifi_request_id, wifi_interface_handle);
274     wifi_error (* wifi_set_country_code)(wifi_interface_handle, const char *);
275     wifi_error (* wifi_get_firmware_memory_dump)( wifi_interface_handle iface,
276             wifi_firmware_memory_dump_handler handler);
277     wifi_error (* wifi_set_log_handler)(wifi_request_id id, wifi_interface_handle iface,
278         wifi_ring_buffer_data_handler handler);
279     wifi_error (* wifi_reset_log_handler)(wifi_request_id id, wifi_interface_handle iface);
280     wifi_error (* wifi_set_alert_handler)(wifi_request_id id, wifi_interface_handle iface,
281         wifi_alert_handler handler);
282     wifi_error (* wifi_reset_alert_handler)(wifi_request_id id, wifi_interface_handle iface);
283     wifi_error (* wifi_get_firmware_version)(wifi_interface_handle iface, char *buffer,
284             int buffer_size);
285     wifi_error (* wifi_get_ring_buffers_status)(wifi_interface_handle iface,
286             u32 *num_rings, wifi_ring_buffer_status *status);
287     wifi_error (* wifi_get_logger_supported_feature_set)(wifi_interface_handle iface,
288             unsigned int *support);
289     wifi_error (* wifi_get_ring_data)(wifi_interface_handle iface, char *ring_name);
290     wifi_error (* wifi_enable_tdls)(wifi_interface_handle, mac_addr, wifi_tdls_params *,
291             wifi_tdls_handler);
292     wifi_error (* wifi_disable_tdls)(wifi_interface_handle, mac_addr);
293     wifi_error (*wifi_get_tdls_status) (wifi_interface_handle, mac_addr, wifi_tdls_status *);
294     wifi_error (*wifi_get_tdls_capabilities)(wifi_interface_handle iface,
295             wifi_tdls_capabilities *capabilities);
296     wifi_error (* wifi_get_driver_version)(wifi_interface_handle iface, char *buffer,
297             int buffer_size);
298     wifi_error (* wifi_set_passpoint_list)(wifi_request_id id, wifi_interface_handle iface,
299             int num, wifi_passpoint_network *networks, wifi_passpoint_event_handler handler);
300     wifi_error (* wifi_reset_passpoint_list)(wifi_request_id id, wifi_interface_handle iface);
301     wifi_error (*wifi_set_lci) (wifi_request_id id, wifi_interface_handle iface,
302 	                             wifi_lci_information *lci);
303     wifi_error (*wifi_set_lcr) (wifi_request_id id, wifi_interface_handle iface,
304 	                             wifi_lcr_information *lcr);
305     wifi_error (*wifi_start_sending_offloaded_packet)(wifi_request_id id,
306                                 wifi_interface_handle iface, u8 *ip_packet, u16 ip_packet_len,
307                                 u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec);
308     wifi_error (*wifi_stop_sending_offloaded_packet)(wifi_request_id id,
309                                 wifi_interface_handle iface);
310     wifi_error (*wifi_start_rssi_monitoring)(wifi_request_id id, wifi_interface_handle
311                         iface, s8 max_rssi, s8 min_rssi, wifi_rssi_event_handler eh);
312     wifi_error (*wifi_stop_rssi_monitoring)(wifi_request_id id, wifi_interface_handle iface);
313     wifi_error (*wifi_get_wake_reason_stats)(wifi_interface_handle iface,
314                                 WLAN_DRIVER_WAKE_REASON_CNT *wifi_wake_reason_cnt);
315     wifi_error (*wifi_configure_nd_offload)(wifi_interface_handle iface, u8 enable);
316     wifi_error (*wifi_get_driver_memory_dump)(wifi_interface_handle iface,
317                                 wifi_driver_memory_dump_callbacks callbacks);
318     wifi_error (*wifi_start_pkt_fate_monitoring)(wifi_interface_handle iface);
319     wifi_error (*wifi_get_tx_pkt_fates)(wifi_interface_handle handle,
320         wifi_tx_report *tx_report_bufs,
321         size_t n_requested_fates,
322         size_t *n_provided_fates);
323     wifi_error (*wifi_get_rx_pkt_fates)(wifi_interface_handle handle,
324         wifi_rx_report *rx_report_bufs,
325         size_t n_requested_fates,
326         size_t *n_provided_fates);
327 
328     /* NAN functions */
329     wifi_error (*wifi_nan_enable_request)(transaction_id id,
330         wifi_interface_handle iface,
331         NanEnableRequest* msg);
332     wifi_error (*wifi_nan_disable_request)(transaction_id id,
333         wifi_interface_handle iface);
334     wifi_error (*wifi_nan_publish_request)(transaction_id id,
335         wifi_interface_handle iface,
336         NanPublishRequest* msg);
337     wifi_error (*wifi_nan_publish_cancel_request)(transaction_id id,
338         wifi_interface_handle iface,
339         NanPublishCancelRequest* msg);
340     wifi_error (*wifi_nan_subscribe_request)(transaction_id id,
341         wifi_interface_handle iface,
342         NanSubscribeRequest* msg);
343     wifi_error (*wifi_nan_subscribe_cancel_request)(transaction_id id,
344         wifi_interface_handle iface,
345         NanSubscribeCancelRequest* msg);
346     wifi_error (*wifi_nan_transmit_followup_request)(transaction_id id,
347         wifi_interface_handle iface,
348         NanTransmitFollowupRequest* msg);
349     wifi_error (*wifi_nan_stats_request)(transaction_id id,
350         wifi_interface_handle iface,
351         NanStatsRequest* msg);
352     wifi_error (*wifi_nan_config_request)(transaction_id id,
353         wifi_interface_handle iface,
354         NanConfigRequest* msg);
355     wifi_error (*wifi_nan_tca_request)(transaction_id id,
356         wifi_interface_handle iface,
357         NanTCARequest* msg);
358     wifi_error (*wifi_nan_beacon_sdf_payload_request)(transaction_id id,
359         wifi_interface_handle iface,
360         NanBeaconSdfPayloadRequest* msg);
361     wifi_error (*wifi_nan_register_handler)(wifi_interface_handle iface,
362         NanCallbackHandler handlers);
363     wifi_error (*wifi_nan_get_version)(wifi_handle handle,
364         NanVersion* version);
365     wifi_error (*wifi_nan_get_capabilities)(transaction_id id,
366         wifi_interface_handle iface);
367     wifi_error (*wifi_nan_data_interface_create)(transaction_id id,
368                                                  wifi_interface_handle iface,
369                                                  char *iface_name);
370     wifi_error (*wifi_nan_data_interface_delete)(transaction_id id,
371                                                  wifi_interface_handle iface,
372                                                  char *iface_name);
373     wifi_error (*wifi_nan_data_request_initiator)(
374         transaction_id id, wifi_interface_handle iface,
375         NanDataPathInitiatorRequest *msg);
376     wifi_error (*wifi_nan_data_indication_response)(
377         transaction_id id, wifi_interface_handle iface,
378         NanDataPathIndicationResponse *msg);
379     wifi_error (*wifi_nan_data_end)(transaction_id id,
380                                     wifi_interface_handle iface,
381                                     NanDataPathEndRequest *msg);
382 
383     /**
384      * Returns the chipset's hardware filtering capabilities:
385      * @param version pointer to version of the packet filter interpreter
386      *                supported, filled in upon return. 0 indicates no support.
387      * @param max_len pointer to maximum size of the filter bytecode, filled in
388      *                upon return.
389      */
390     wifi_error (*wifi_get_packet_filter_capabilities)(wifi_interface_handle handle,
391                                                       u32 *version, u32 *max_len);
392     /**
393      * Programs the packet filter.
394      * @param program pointer to the program byte-code.
395      * @param len length of the program byte-code.
396      */
397     wifi_error (*wifi_set_packet_filter)(wifi_interface_handle handle,
398                                          const u8 *program, u32 len);
399     wifi_error (*wifi_get_roaming_capabilities)(wifi_interface_handle handle,
400                                                 wifi_roaming_capabilities *caps);
401     wifi_error (*wifi_enable_firmware_roaming)(wifi_interface_handle handle,
402                                                fw_roaming_state_t state);
403     wifi_error (*wifi_configure_roaming)(wifi_interface_handle handle,
404                                          wifi_roaming_config *roaming_config);
405 } wifi_hal_fn;
406 wifi_error init_wifi_vendor_hal_func_table(wifi_hal_fn *fn);
407 #ifdef __cplusplus
408 }
409 #endif
410 
411 #endif
412