• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef BWIFI_HAL_H
16 #define BWIFI_HAL_H
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include "bwifi_event.h"
23 
24 #define BWIFI_MAX_COUNTRY_CODE_SIZE     2
25 #define BWIFI_CONFIG_VAL_LEN            100
26 #define WIFI_SCAN_DUMP
27 
28 typedef enum bwifi_security_type {
29     BWIFI_SECURITY_NONE,              /*!< open access point */
30     BWIFI_SECURITY_WEP40,             /*!< phrase conforms to WEP */
31     BWIFI_SECURITY_WEP104,            /*!< phrase conforms to WEP EXTENDED */
32     BWIFI_SECURITY_WPA,               /*!< WPA-PSK */
33     BWIFI_SECURITY_WPA2,              /*!< WPA2-PSK */
34     BWIFI_SECURITY_WPA_WPA2,          /*!< WPA WPA2 mixed mode */
35     BWIFI_SECURITY_WPA3_SAE,          /*!< WPA3-SAE */
36     BWIFI_SECURITY_WPA3_SAE_WPA2,     /*!< WPA3-SAE WPA2 mixed mode*/
37     BWIFI_SECURITY_ERROR,             /*!< error mode */
38 } BWIFI_SEC_TYPE_T;
39 
40 struct wifi_init_param {
41     char     band;
42     char     country[3];
43     uint16_t *tx_power;
44     uint16_t *tx_power_5G;
45     uint8_t  *wifi_address;
46     void     *fac_wifi;
47     uint32_t coex_mode; // bit0: 0 -TDD, 1 - FDD; bit1: HYBRID
48     uint32_t sleep_mode; // 0 - normal mode, 1 - no sleep mode
49 };
50 
51 struct bwifi_mac_addr {
52     uint8_t mac[6];
53 };
54 
55 struct bwifi_bss_info {
56     uint8_t bssid[6];
57     char ssid[32 + 1];
58     uint8_t channel;
59     int8_t rssi;
60     BWIFI_SEC_TYPE_T security_type;
61     uint8_t *ie; //user program couldn't free(ie);
62     uint32_t ie_length;
63 };
64 
65 struct bwifi_ap_info {
66     char ssid[32 + 1];
67     uint8_t channel;
68     uint8_t hidden;
69     BWIFI_SEC_TYPE_T security;
70     int sec_channel_offset;
71     int wpa_mode;
72     char passwd[64];
73 };
74 
75 struct bwifi_ssid {
76     struct bwifi_ssid *next;
77     uint8_t ssid[32 + 1]; //null-terminated string
78 };
79 
80 struct bwifi_scan_config {
81     struct bwifi_ssid *ssids;   /**< List of specified SSIDs */
82     int *channels;              /**< Array of channels (zero-terminated) to scan or NULL for all channels */
83 };
84 
85 struct bwifi_station_config {
86     uint8_t ssid[33];
87     uint8_t passwd[65];//passwd for encrypted ssid, set "all zero" for unencryped ssid
88     int8_t wep_keyid;//for wep,[0-3] default val:0
89     uint8_t hidden;//1:hidden
90     uint8_t bssid[ETH_ALEN];//bssid or "all zero"
91     uint8_t mfp;
92 };
93 
94 typedef struct bwifi_trans_stat_t {
95     uint32_t tx_succ_cnt;
96     uint32_t tx_fail_cnt;
97     uint32_t tx_retry_cnt;
98     uint32_t rx_cnt;
99     uint32_t txbps; //tx throughput bit/s
100     uint32_t rxbps; //rx throughput bit/s
101 } bwifi_trans_stat;
102 
103 typedef struct bwifi_station_linkinfo_t {
104     bwifi_trans_stat stat;
105     int8_t rssi;
106     int8_t channel;
107     /* BIT 31: MCS_FLAG = 1, BTI30:ShortGI ,BIT29-26: BW(0 20M,1 40M), BIT25-0,MCS index
108      * BIT 31: MCS_FALG = 0, BIT0-B25 is b/g tx rate
109      * */
110     int tx_rate;
111 } bwifi_station_linkinfo;
112 
113 enum wifi_config_item {
114     WIFI_CONFIG_ITEM_WAPI_CERT_PRIVATE_KEY = 0, /**< WAPI own private key */
115     WIFI_CONFIG_ITEM_WAPI_CERT_AS_CERT,         /**< WAPI AS certification */
116     WIFI_CONFIG_ITEM_WAPI_CERT_ASUE_CERT,       /**< WAPI ASUE certification */
117     WIFI_CONFIG_ITEM_MAX
118 };
119 
120 struct bwifi_config_item {
121     int net_index;                      /**< network index */
122     int item_index;                     /**< config item index */
123     int val_len;                        /**< config item value length */
124     uint8_t val[BWIFI_CONFIG_VAL_LEN];  /**< config item value */
125 };
126 
127 enum bwifi_epta_state {
128     EPTA_STATE_WIFI_DISCONNECTED,
129     EPTA_STATE_WIFI_CONNECTED,
130     EPTA_STATE_WIFI_TTS_START,
131     EPTA_STATE_WIFI_TTS_END,
132     EPTA_STATE_BT_INACTIVE,
133     EPTA_STATE_BT_ACTIVE,
134     EPTA_STATE_BT_A2DP,
135 	EPTA_STATE_NUM
136 };
137 
138 struct bwifi_hal_ops {
139 	/**
140 	 * add_config - add sta config
141 	 * @config: bwifi_station_config
142 	 *
143 	 * Returns: 0 - success, other - failure
144 	 */
145     int (*add_config)(struct bwifi_station_config *config);
146 
147 	/**
148 	 * modify_config - modify sta config
149 	 * @config: bwifi_station_config
150 	 *
151 	 * Returns: 0 - success, other - failure
152 	 */
153     int (*modify_config)(struct bwifi_config_item *item);
154 
155 	/**
156 	 * count_ssid_config - get the number of sta config items
157 	 * @item: bwifi_config_item
158 	 *
159 	 * Returns: 0 - success, other - failure
160 	 */
161     int (*count_ssid_config)(void);
162 
163 	/**
164 	 * get_current_config - get current sta config to connect
165 	 * @config: bwifi_station_config array
166      * @count: max item number of config array
167 	 *
168 	 * Returns: the number of current config
169 	 */
170     int (*get_current_config)(struct bwifi_station_config *config, int count);
171 
172 	/**
173 	 * save_config - save sta config to nvram
174 	 * @config: bwifi_station_config
175 	 *
176 	 * Returns: 0 - success, other - failure
177 	 */
178     int (*save_config)(struct bwifi_station_config *config);
179 
180 	/**
181 	 * get_saved_config - get saved sta config
182 	 * @config: bwifi_station_config array
183      * @count: max number of bwifi_station_config in config array
184 	 *
185 	 * Returns: the number of saved config
186 	 */
187     int (*get_saved_config)(struct bwifi_station_config *config, int count);
188 
189 	/**
190 	 * get_saved_ssid - get saved config of specific ssid
191      * @ssid: specific ssid
192      * @config: bwifi_station_config
193 	 *
194 	 * Returns: 0 - success, other - failure
195 	 */
196     int (*get_saved_ssid)(const char *ssid, struct bwifi_station_config *config);
197 
198 	/**
199 	 * del_all_config - delete all sta config
200 	 *
201 	 * Returns: 0 - success, other - failure
202 	 */
203     int (*del_all_config)(void);
204 
205 	/**
206 	 * del_config - delete the specific config
207      * @config: specific config
208 	 *
209 	 * Returns: 0 - success, other - failure
210 	 */
211     int (*del_config)(struct bwifi_station_config *config);
212 
213 	/**
214 	 * scan - start to scan
215 	 *
216 	 * Returns: 0 - success, other - failure
217 	 */
218     int (*start_scan)(void);
219 
220 	/**
221 	 * config_scan - config specific scan
222      * @scan_config: scan config
223 	 *
224 	 * Returns: 0 - success, other - failure
225 	 */
226     int (*config_scan)(struct bwifi_scan_config *scan_config);
227 
228 	/**
229 	 * get_scan_result - get scan result
230      * @result: scan result buffer
231      * @count: max number of bwifi_bss_info in scan result buffer
232 	 *
233 	 * Returns: the number of scanned bss
234 	 */
235     int (*get_scan_result)(struct bwifi_bss_info *result, int count);
236 
237 	/**
238 	 * flush_scan_res - flush all scan results
239 	 *
240 	 * Returns: 0 - success, other - failure
241 	 */
242     int (*flush_scan_res)(void);
243 
244 	/**
245 	 * connect - start to connect
246 	 *
247 	 * Returns: 0 - no suitable network found, 1 - connect success, other - connect fail
248 	 */
249     int (*start_connect)(void);
250 
251 	/**
252 	 * network_enable - enable all network config to connect with
253 	 *
254 	 * Returns: 0 - success, other - failure
255 	 */
256     int (*network_enable)(uint32_t enable);
257 
258 	/**
259 	 * network_enable - enable specific network config to connect with
260      * @nid: network id
261 	 *
262 	 * Returns: 0 - success, other - failure
263 	 */
264     int (*network_id_enable)(int nid, uint32_t enable);
265 
266 	/**
267 	 * wps_bss_find - find wps enabled bss
268      * @bss: bwifi_bss_info
269 	 *
270 	 * Returns: 0 - success, other - failure
271 	 */
272     int (*wps_bss_find)(struct bwifi_bss_info * bss);
273 
274 	/**
275 	 * wps_connect - start wps connection
276      * @bssid: specific bssid to connect
277 	 *
278 	 * Returns: 0 - success, other - failure
279 	 */
280     int (*wps_connect)(uint8_t *bssid);
281 
282 	/**
283 	 * disconnect - disconnect current connection
284 	 *
285 	 * Returns: 0 - success, other - failure
286 	 */
287     int (*disconnect)(void);
288 
289 	/**
290 	 * get_current_ssid - get the ssid of current connection
291      * @ssid: ssid buffer, 32 ascii chars with '0' end
292 	 *
293 	 * Returns: 0 - success, other - failure
294 	 */
295     int (*get_current_ssid)(char *ssid);
296 
297 	/**
298 	 * get_current_bssid - get the bssid of current connection
299      * @bssid: bssid buffer
300 	 *
301 	 * Returns: 0 - success, other - failure
302 	 */
303     int (*get_current_bssid)(uint8_t *bssid);
304 
305 	/**
306 	 * get_own_mac - get sta mac address
307      * @addr: mac address buffer
308 	 *
309 	 * Returns: 0 - success, other - failure
310 	 */
311     int (*get_own_mac)(uint8_t *addr);
312 
313 	/**
314 	 * get_current_rate - get current tx rate
315 	 *
316 	 * Returns: rate index
317      * bit 31: MCS rate flag
318      * bit 30:ShortGI flag
319      * bit 29~26: BW (0:20M, 1:40M, other:reserved)
320      * bit 25-0: MCS index
321 	 */
322     int (*get_current_rate)(void);
323 
324 	/**
325 	 * get_station_linkinfo - get link info of current sta connection
326      * @info: bwifi_station_linkinfo buffer
327 	 *
328 	 * Returns: 0 - success, other - failure
329 	 */
330     int (*get_station_linkinfo)(bwifi_station_linkinfo *info);
331 
332 	/**
333 	 * get_current_rssi - get current rssi
334 	 *
335 	 * Returns: rssi (dbm)
336 	 */
337     int8_t (*get_current_rssi)(void);
338 
339 	/**
340 	 * set_ifa_ip - set ip address of specific interface
341      * @dev_no: device interface index
342      * @ip: hex format ip address
343 	 *
344 	 * Returns: 0 - success, other - failure
345 	 */
346     int (*set_ifa_ip)(uint8_t dev_no, uint32_t *ip);
347 
348 	/**
349 	 * work_channel_notify - notify current work channel
350      * @ch: channel number
351 	 *
352 	 * Returns: 0 - success, other - failure
353      * Need to notify work channel to wifi hal to use the most suitable configuration
354      * e.g. dpd parameter, coexistance mode, etc.
355 	 */
356     int (*work_channel_notify)(uint8_t ch);
357 
358 	/**
359 	 * traffic_stat_en - enable wifi traffic statistic
360      * @en: enable/disable statistic
361      * @interval_sec: the interval of statistic (seconds)
362 	 *
363 	 * Returns: 0 - success, other - failure
364 	 */
365     int (*traffic_stat_en)(uint8_t en, uint8_t interval_sec);
366 
367 	/**
368 	 * traffic_stat_get - get the latest traffic statistic
369      * @stat: statistic data buffer
370      * @clear: indicate if need to clear statistic data
371 	 *
372 	 * Returns: 0 - success, other - failure
373 	 */
374     int (*traffic_stat_get)(bwifi_trans_stat *stat, uint8_t clear);
375 
376 	/**
377 	 * init - wifi subsystem initialize
378 	 * @flag: 0 - init during sys reboot, 1 - init during wifi reset
379 	 *
380 	 * Returns: 0 - success, other - failure
381 	 */
382     int (*init)(uint8_t flag);
383 
384 	/**
385 	 * ps_mode_set - set wifi power saving mode
386 	 * @enable: 0 - disable ps mode, 1 - enable ps mode
387 	 *
388 	 * Returns: 0 - success, other - failure
389 	 */
390     int (*ps_mode_set)(int enable);
391 
392 	/**
393 	 * softap_start - start ap interface
394 	 *
395 	 * Returns: 0 - success, other - failure
396 	 */
397     int (*softap_start)(void);
398 
399 	/**
400 	 * softap_stop - stop ap interface
401 	 *
402 	 * Returns: 0 - success, other - failure
403 	 */
404     int (*softap_stop)(void);
405 
406 	/**
407 	 * softap_config_set - set softap config parameters
408 	 * @info: config parameters
409 	 *
410 	 * Returns: 0 - success, other - failure
411 	 */
412     int (*softap_config_set)(struct bwifi_ap_info *info);
413 
414 	/**
415 	 * softap_peer_num_set - set the number of peer STAs that softap allow to connect
416      * @num: max sta number
417 	 *
418 	 * Returns: 0 - success, other - failure
419 	 */
420     int (*softap_peer_num_set)(int num);
421 
422 	/**
423 	 * softap_country_code_set - set the country code of working region
424      * @country_code: courntry code of regulation domain
425 	 *
426 	 * Returns: 0 - success, other - failure
427 	 */
428     int (*softap_country_code_set)(char *country_code);
429 
430 	/**
431 	 * softap_vendor_elements_set - set vendor ie of softap
432      * @ie: information element buffer
433      * @len: the length of information element
434 	 *
435 	 * Returns: 0 - success, other - failure
436 	 */
437     int (*softap_vendor_elements_set)(const uint8_t *ie, size_t ie_len);
438 
439 	/**
440 	 * softap_peer_query - query connected peer address list
441      * @mac_list: peer address buffer
442      * @count: max peer count in mac_list
443 	 *
444 	 * Returns: the number of connected peer sta(s)
445 	 */
446     int (*softap_peer_query)(struct bwifi_mac_addr *mac_list, int count);
447 
448 	/**
449 	 * channel_switch - switch channel in no signal mode
450      * @mode: interface mode, 0 - softap interface, 1 - sta interface
451      * @channel: channel nunber
452      * @snd_offset: secondary channel offset to center freq, 0 - no snd ch, 1 - upper 10M, -1 - lower 10M
453 	 *
454 	 * Returns: 0 - success, other - failure
455 	 */
456     int (*channel_switch)(uint8_t mode, uint8_t channel, int8_t snd_offset);
457 
458 	/**
459 	 * set_country_code - set country code of regulation domain
460      * @country: country code represented by two ascii characters ended with '\0'
461 	 *
462 	 * Returns: 0 - success, other - failure
463 	 */
464     int (*set_country_code)(char *country);
465 
466 	/**
467 	 * get_country_code - get country code of regulation domain
468      * @country: country code represented by two ascii characters ended with '\0'
469 	 *
470 	 * Returns: 0 - success, other - failure
471 	 */
472     int (*get_country_code)(char *country);
473 
474 	/**
475 	 * send_mgmt_frame - send mgmt frame on specific channel
476      * @data: frame buffer
477      * @len: frame length
478      * @noack: indicate if ack needed
479      * @chan: channel number to send
480 	 *
481 	 * Returns: 0 - success, other - failure
482 	 */
483     int (*send_mgmt_frame)(uint8_t dev_no, const uint8_t *data, size_t len, int noack, uint8_t chan);
484 
485 	/**
486 	 * data_rate_set - set the rate to send data frame
487      * @rate_idx: rate index
488      *   0 ~ 3:   DSSS/CCK rates: 1, 2, 5.5, 11;
489      *   4 ~ 5:   reserved;
490      *   6 ~ 13:  legacy OFDM rates: 6, 9, 12, 18, 24, 36, 48, 54;
491      *   14 ~ 21: 1*1 MCS rates: MCS0 ~ MCS7;
492      *   0xff:    auto-adjusting mode (default)
493 	 * Returns: 0 - success, other - failure
494 	 */
495     int (*data_rate_set)(uint32_t rate_idx);
496 
497 	/**
498 	 * epta_param_config - set epta parameter for wifi/bt TDD coexistance
499      * @wifi_dur: wifi duration (ms)
500      * @bt_dur: bt duration (ms)
501      * @mode: epta work mode
502      *   0: periodic arbitrate by software, using wifi_dur and bt_dur to alloc active time for wifi and bt,
503      *       wifi_dur + bt_dur should equal the 100ms period time
504      *   1: arbitrate by hw itself
505 	 * Returns: 0 - success, other - failure
506 	 */
507     int (*epta_param_config)(int wifi_dur, int bt_dur, int mode);
508 
509 	/**
510 	 * set_epta_param - notify wifi state to epta
511      * @state: wifi connection state, 0 - disconnected, 1 - connected
512 	 * Returns: 0 - success, other - failure
513 	 */
514     int (*epta_state_notify)(enum bwifi_epta_state state);
515 
516 	/**
517 	 * reset - wifi subsystem reset
518 	 * Returns: 0 - success, other - failure
519 	 */
520     int (*reset)(void);
521 
522 	/**
523 	 * data_recv_cb_set - set callback to handle ethernet frame
524 	 * @hdl: call back func pointer
525 	 * Returns: 0 - success, other - failure
526 	 */
527     int (*data_recv_cb_set)(eth_input_handler hdl);
528 
529 	/**
530 	 * eth_cb_set - set callback to handle ethernet frame
531 	 * @hdl: call back func pointer
532 	 * Returns: 0 - success, other - failure
533 	 */
534     uint8_t **(*get_send_buf)(void);
535 
536 	/**
537 	 * eth_cb_set - set callback to handle ethernet frame
538 	 * @hdl: call back func pointer
539 	 * Returns: 0 - success, other - failure
540 	 */
541     int (*data_send)(uint8_t devnum, uint8_t **tx_buf, uint16_t tx_len);
542 
543 	/**
544 	 * event_cb_set - set callback to handle event
545 	 * @hdl: call back func pointer
546 	 * Returns: 0 - success, other - failure
547 	 */
548     int (*event_cb_set)(evt_recv_handler hdl);
549 
550 	/**
551 	 * str_cmd - send string command to wifi subsystem
552 	 * @type: command type, 0 - wifi rf test, other - reserved
553 	 * @cmd_buf: command string ended with '\0'
554 	 * @rsp_buf: response string ended with '\0'
555 	 * @rsp_size: size of buffer to hold the rsp_buf's content
556 	 * Returns: 0 - success, other - failure
557 	 */
558     int (*str_cmd)(uint8_t type, uint8_t *cmd_buf, uint8_t *rsp_buf, uint32_t rsp_size);
559 };
560 
561 extern struct bwifi_hal_ops whops;
562 #define BWIFI_HAL_API_CALL(func,...)	((whops.func) ? (int)whops.func(__VA_ARGS__) : -1)
563 
564 #ifdef __cplusplus
565 }
566 #endif
567 
568 #endif
569