VSF Documented
vsf_wifi.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
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
18#ifndef __VSF_WIFI_H__
19#define __VSF_WIFI_H__
20
21/*============================ INCLUDES ======================================*/
22
23#include "./vsf_wifi_cfg.h"
24
25#if VSF_USE_WIFI == ENABLED
26
27#include "kernel/vsf_kernel.h"
28#include "./vsf_wifi_radio.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34/*============================ MACROS ========================================*/
35
36/*
37 * Auth / cipher constants (kept stable across chips so the application can
38 * pass a single vsf_wifi_auth_cfg_t regardless of the underlying NIC).
39 */
40enum {
47};
48
49enum {
56};
57
58#define WIFI_LINK_FLAG_CONNECTED (1 << 0)
59#define WIFI_LINK_FLAG_AUTHORIZED (1 << 1)
60
61#define WIFI_SCAN_FLAG_PRIVACY (1 << 0)
62#define WIFI_SCAN_FLAG_WPS (1 << 1)
63#define WIFI_SCAN_FLAG_WPA (1 << 2)
64#define WIFI_SCAN_FLAG_WPA2 (1 << 3)
65
66/*
67 * MLME connection state (OPEN-system auth + association). Driven entirely
68 * by the wifi layer; the chip layer only strips RX descriptors and routes
69 * mgmt frames to vsf_wifi_mlme_rx().
70 */
71enum {
72 WIFI_MLME_IDLE = 0, /* not connecting / disconnected */
73 WIFI_MLME_AUTH = 1, /* auth-req sent, awaiting auth-resp */
74 WIFI_MLME_ASSOC = 2, /* assoc-req sent, awaiting assoc-resp */
75 WIFI_MLME_4WAY = 4, /* associated, WPA2 4-way handshake running */
76 WIFI_MLME_RUN = 3, /* associated (link up) */
77 WIFI_MLME_KEY_INSTALL = 5, /* hardware key install in progress */
78 WIFI_MLME_RAW_RADIO = 6, /* raw radio mode active */
79};
80
81/*
82 * link-down reason codes passed to vsf_wifi_on_link_down(). Low values
83 * mirror common 802.11 reason codes; high values are wifi-layer specific.
84 */
85enum {
87 WIFI_REASON_AUTH_LEAVING = 3, /* deauth: STA is leaving */
88 WIFI_REASON_MIC_FAILURE = 14, /* EAPOL-Key MIC verification failed */
89 WIFI_REASON_DISASSOC_LEAVING= 8, /* disassoc: STA is leaving */
90 WIFI_REASON_LOCAL_TIMEOUT = 200, /* handshake retries exhausted */
91 WIFI_REASON_AUTH_REJECTED = 201, /* auth-resp status != 0 */
92 WIFI_REASON_ASSOC_REJECTED = 202, /* assoc-resp status != 0 */
93 WIFI_REASON_LOCAL_DISCONNECT= 203, /* user-requested disconnect */
94};
95
96/*
97 * Scratch op buffer size — caps how many ep0 register writes a parameterised
98 * chip op (set_channel / connect / disconnect ...) can issue. Static const
99 * scripts (e.g. the chip init table) are not bounded by this.
100 *
101 * Worst case today is MT76 hardware-CCMP key install: 8 WCID key + 2 WCID IV +
102 * 1 WCID attr + 8 shared key + 1 SKEY_MODE = 20 ops. Default 24 leaves headroom.
103 */
104#ifndef VSF_WIFI_CFG_SCRATCH_OPS
105# define VSF_WIFI_CFG_SCRATCH_OPS 24
106#endif
107
108/* Maximum number of channels in a single active scan (2.4 GHz + 5 GHz). */
109#ifndef VSF_WIFI_CFG_SCAN_MAX_CHANNELS
110# define VSF_WIFI_CFG_SCAN_MAX_CHANNELS 40
111#endif
112
113/*============================ TYPES =========================================*/
114
115typedef struct vsf_wifi_t vsf_wifi_t;
118
119/*
120 * Asynchronous-completion callback. Invoked exactly once per async request
121 * (script / blob / single bus op). err == VSF_ERR_NONE on success.
122 */
123typedef void (*vsf_wifi_done_t)(vsf_wifi_t *wifi, vsf_err_t err);
124
125/*
126 * Predicate used by vsf_wifi_reg_read_poll to decide whether the value just
127 * read from a register satisfies the wait condition. Called from the bus
128 * driver's EDA on every poll iteration; must be cheap and side-effect free.
129 */
131
140 uint8_t wmm; /* 1 if the BSS advertises a WMM/WME IE */
141 /* Security parsed from the RSN IE (tag 48). auth_mode == WIFI_AUTH_OPEN
142 * means no RSN IE was present (open network / WEP). Only WPA2-PSK is
143 * recognised; pairwise/group are WIFI_CIPHER_xxx. */
147 uint8_t ht40_width; /* 0 = 20 MHz, 1 = 40 MHz (from HT Operation IE) */
148 uint8_t ht40_plus; /* 1 = secondary channel above, 0 = below/none */
150
159
167
168/*
169 * One unit of register-access work — write `val` (LE32) to register `reg`.
170 *
171 * BBP / RF indirect writes are NOT a separate op type: chip backends pre-
172 * encode them (see RT_OP_BBP / RT_OP_RF in chip/rt28xx/vsf_wifi_rt28xx.c).
173 * This keeps the wifi state machine free of chip-specific knowledge.
174 */
179
180/*
181 * Bulk register-block payload (firmware blob, calibration table, etc.).
182 * Each chunk is shipped via reg_bus->reg_block_write (or, when the bus
183 * lacks that primitive, decomposed into multiple reg_write calls by the
184 * generic layer). `data` MUST stay valid until the completion callback
185 * fires — pointing at .rodata is fine on every supported bus.
186 */
193
194/*============================ REGISTER BUS OPS ==============================*
195 *
196 * Optional register-access abstraction for register-based WiFi chips (e.g.
197 * Ralink RT2X00). This vtable describes the chip's register semantics, NOT
198 * the underlying physical bus. It therefore MUST NOT contain bus-specific
199 * primitives such as USB ep0 vendor requests; those belong in a chip-private
200 * bus-operations structure (placed in wifi->bus_ops) when a chip really needs
201 * them.
202 *
203 * Chips that do NOT speak a register protocol — such as MediaTek mt76, which
204 * uses firmware commands over bulk/interrupt endpoints — can leave
205 * wifi->reg_bus NULL and implement their own transport inside drv->tx /
206 * drv->init / etc.
207 *
208 * Concurrency contract: at most one reg_bus call may be in flight at any
209 * time. Issuing a second call before the previous `done` callback has
210 * fired is a programming error; the bus driver MAY return VSF_ERR_NOT_
211 * AVAILABLE in that case.
212 *==========================================================================*/
213
215 /* Single-register 32-bit write. REQUIRED.
216 *
217 * Returns VSF_ERR_NONE if the write was queued; completion is delivered
218 * later via `done`. `done` may be NULL for fire-and-forget. Synchronous
219 * errors (resource exhaustion, bus busy) are returned directly without
220 * invoking `done`.
221 */
223 vsf_wifi_done_t done);
224
225 /* Single-register 32-bit read. REQUIRED.
226 *
227 * `out` is written before `done` fires; caller must keep it valid until
228 * then. Same async / error semantics as reg_write.
229 */
231 vsf_wifi_done_t done);
232
233 /* Contiguous register-block write (firmware upload / calibration table
234 * / TX descriptor RAM). OPTIONAL — when NULL the generic layer falls
235 * back to a sequence of reg_write calls.
236 *
237 * `base` is the starting register/memory address; the bus implementation
238 * is free to fragment the payload internally (USB ep0 picks 64-byte
239 * vendor-request chunks, SDIO CMD53 picks block size, etc.). `data`
240 * must stay valid until `done` fires.
241 */
243 const uint8_t *data, uint32_t len,
244 vsf_wifi_done_t done);
245
246 /* Bus-layer ready notification. OPTIONAL — called when the wifi init
247 * chain completes successfully (before vsf_wifi_on_ready). The bus
248 * driver uses this to enable the data path (e.g. submit bulk RX URBs).
249 */
250 void (*on_ready)(vsf_wifi_t *wifi);
251
252 /* Bulk data-frame transmit for register-based buses that also have a
253 * bulk data endpoint. OPTIONAL — NULL when the register bus has no data
254 * endpoint (pure ep0) or when the chip drv->tx handles data transmission
255 * itself. `data` points at a fully chip-encoded TX payload; `len` is its
256 * total length. Fire-and-forget: the bus driver owns completion internally
257 * (TX URB pool). Returns VSF_ERR_NONE when the frame was queued. */
259};
260
261/*============================ CHIP DRIVER VTABLE ============================*
262 *
263 * All chip operations are async: the chip driver builds an op-script (static
264 * .rodata or wifi->scratch_ops) and submits it via vsf_wifi_reg_run_script. The
265 * outer `done` callback fires when the entire register script has been ACKed by the
266 * device. Returning VSF_ERR_NONE from these hooks means the script was
267 * successfully queued; actual completion arrives later via `done`.
268 *
269 * The wifi layer never calls reg_bus directly — it always goes through the
270 * chip vtable. Register-based chips use the helpers vsf_wifi_reg_run_script /
271 * run_blob / reg_read / reg_read_poll; command-based chips ignore reg_bus.
272 *==========================================================================*/
273
274#if VSF_WIFI_USE_WPA == ENABLED
275/*
276 * Optional hardware crypto backend. When a chip driver supplies crypto_ops
277 * with a non-NULL install_key, the wifi layer hands the negotiated PTK.TK /
278 * GTK to the hardware engine and assumes the chip performs CCMP in-line; the
279 * software CCMP path (wpa_hw_crypto == false) is then skipped on both TX and
280 * RX. Leaving crypto_ops (or install_key) NULL selects the built-in software
281 * CCMP fallback.
282 *
283 * install_key : program a key. key_idx 0 + pairwise == the unicast TK;
284 * key_idx 1..3 + !pairwise == a GTK. `mac` is the peer for
285 * pairwise keys, NULL for group keys.
286 * `done` is called when the register script finishes; the
287 * caller may be in an EDA or interrupt context, so
288 * install_key MUST NOT block. Returns VSF_ERR_NONE on
289 * successful submission.
290 * encrypt/decrypt : reserved per-frame overrides for chips that still need
291 * wifi-layer framing with a chip-specific tweak; NULL means
292 * "use the built-in software CCMP".
293 */
294typedef struct vsf_wifi_crypto_ops_t {
295 vsf_err_t (*install_key)(vsf_wifi_t *wifi, uint8_t key_idx, bool pairwise,
296 const uint8_t *key, uint8_t key_len,
297 const uint8_t *mac, vsf_wifi_done_t done);
299 uint16_t *len, uint16_t cap);
300 vsf_err_t (*decrypt) (vsf_wifi_t *wifi, uint8_t *dot11, uint16_t *len);
302#endif
303
305 const char *name;
306
307 /* Optional firmware uploader (rt2870.bin etc.). Invoked once before
308 * init() during attach. Pure ops can use vsf_wifi_reg_run_script; bulk
309 * blob uploads call vsf_wifi_reg_run_blob. Leave NULL when the chip needs
310 * no firmware. */
313 void (*fini) (vsf_wifi_t *wifi);
315 vsf_wifi_done_t done);
317 vsf_wifi_done_t done);
318 vsf_err_t (*set_mac_addr) (vsf_wifi_t *wifi, const uint8_t mac[6],
319 vsf_wifi_done_t done);
321 vsf_wifi_done_t done);
323 const vsf_wifi_auth_cfg_t *cfg,
324 vsf_wifi_done_t done);
326 const uint8_t bssid[6], const uint8_t *ssid,
327 uint8_t ssid_len, uint8_t channel,
328 vsf_wifi_done_t done);
331 vsf_wifi_link_info_t *info); /* sync */
332
333 /* Optional RX-frame parser used during scan. When non-NULL and the
334 * driver is currently scanning, the wifi layer routes incoming frames
335 * here instead of vsf_wifi_on_rx; the chip code extracts beacons /
336 * probe responses and emits vsf_wifi_on_scan_result(). */
337 void (*parse_rx) (vsf_wifi_t *wifi, uint8_t *frame, uint16_t len);
338
339 /* Optional TX-descriptor builder. Wraps a raw 802.11 frame into the
340 * chip-specific on-wire TX layout (e.g. RT2800 TXINFO + TXWI + frame +
341 * pad) by writing into `dst` (capacity `dst_cap`). Returns the total
342 * byte count to ship over the data bus, or 0 on failure (dst too small
343 * / unsupported). Used by drv->tx; may also be left NULL if tx builds
344 * its descriptor internally. */
345 uint16_t (*build_tx) (vsf_wifi_t *wifi, uint8_t *dst, uint16_t dst_cap,
346 const uint8_t *frame, uint16_t frame_len);
347
348 /* Frame transmit hook. The wifi layer routes all outbound 802.11 frames
349 * (active-scan probe-request, auth, assoc, deauth and data frames from
350 * the netdrv) through this hook. The chip driver builds the on-wire
351 * payload and ships it over the bus. REQUIRED; NULL returns
352 * VSF_ERR_NOT_SUPPORT from vsf_wifi_tx. */
353 vsf_err_t (*tx) (vsf_wifi_t *wifi, const uint8_t *frame, uint16_t len);
354
355#if VSF_WIFI_USE_WPA == ENABLED
356 /* Optional hardware crypto backend (see vsf_wifi_crypto_ops_t above).
357 * NULL selects the software CCMP fallback. */
359#endif
360
361 /* Optional raw WiFi radio backend. NULL means the chip only supports
362 * the standard 802.11 station-mode interface. */
364};
365
366/*============================ APPLICATION CALLBACKS =========================*
367 *
368 * Weak hooks supplied by the application. All run in the bus driver's EDA
369 * context (not from ISR). Default no-op implementations live in vsf_wifi.c.
370 *==========================================================================*/
371
372extern void vsf_wifi_on_new (vsf_wifi_t *wifi);
373extern void vsf_wifi_on_del (vsf_wifi_t *wifi);
374extern void vsf_wifi_on_rx (vsf_wifi_t *wifi,
375 uint8_t *frame, uint16_t len);
376extern void vsf_wifi_on_ready (vsf_wifi_t *wifi);
377extern void vsf_wifi_on_scan_result(vsf_wifi_t *wifi,
378 const vsf_wifi_scan_result_t *result);
379extern void vsf_wifi_on_scan_done (vsf_wifi_t *wifi);
380extern void vsf_wifi_on_link_up (vsf_wifi_t *wifi,
381 const vsf_wifi_link_info_t *info);
382extern void vsf_wifi_on_link_down (vsf_wifi_t *wifi, uint8_t reason);
383
384/*============================ NETIF BINDING ================================*
385 *
386 * Runtime binding for a network-stack adapter (e.g. the lwIP netdrv driver
387 * in vsf_wifi_netdrv.c). Once a backend is attached via
388 * vsf_wifi_netdrv_attach(), the core delivers decrypted RX data frames to
389 * its on_rx (replacing the weak vsf_wifi_on_rx hook), and additionally
390 * delivers link up/down events to its on_link_up / on_link_down. Link
391 * events still also fire the weak vsf_wifi_on_link_up / vsf_wifi_on_link_down
392 * hooks: those are control-plane notifications (e.g. unblocking a connect
393 * command) and run alongside the backend's data-plane handling. With no
394 * backend attached only the weak hooks are used (back-compat with the
395 * diagnostic commands). All callbacks run in the bus driver's EDA context,
396 * exactly like the weak hooks. `on_rx` receives a naked, decrypted 802.11
397 * data frame (FC .. payload); the backend is responsible for any 802.11 <->
398 * Ethernet framing. */
399typedef struct vsf_wifi_netif_ops_t {
400 void (*on_rx) (void *param, vsf_wifi_t *wifi,
401 uint8_t *frame, uint16_t len);
402 void (*on_link_up) (void *param, vsf_wifi_t *wifi,
403 const vsf_wifi_link_info_t *info);
404 void (*on_link_down)(void *param, vsf_wifi_t *wifi, uint8_t reason);
406
407/* Register / unregister a netif backend. `ops` (and the storage it points
408 * at) must stay valid until detach; `param` is passed back verbatim to each
409 * callback. Passing ops == NULL is equivalent to detach. */
411 const vsf_wifi_netif_ops_t *ops, void *param);
413
414/*============================ USER API ======================================*/
415
416/* Inspection. */
418const char * vsf_wifi_get_chip_name(vsf_wifi_t *wifi);
419bool vsf_wifi_is_ready (vsf_wifi_t *wifi);
420
421/* BSS Max Idle Period advertised by the AP in the assoc-resp (IE 90).
422 * Returned in units of 1000 TU; 0 means the AP did not advertise it. */
424
425/* Per-device MAC address discovered during chip bring-up (eFuse / EEPROM
426 * read). Returns a pointer to a 6-byte array inside the wifi struct;
427 * all-zero until the chip driver finishes its EEPROM stage. The pointer
428 * stays valid for the lifetime of the wifi instance. */
429const uint8_t * vsf_wifi_get_mac (vsf_wifi_t *wifi);
430
431/* Mutation. Calls return VSF_ERR_NOT_READY before vsf_wifi_on_ready,
432 * VSF_ERR_NOT_AVAILABLE while a scan is in progress, and the chip driver's
433 * own error otherwise. All async — completion is signalled by either the
434 * relevant on_xxx callback or by the next observable side effect (RSSI,
435 * link_info, etc.). */
436void vsf_wifi_set_channel (vsf_wifi_t *wifi, uint8_t channel);
437vsf_err_t vsf_wifi_set_mac (vsf_wifi_t *wifi, const uint8_t mac[6]);
440 const vsf_wifi_auth_cfg_t *cfg);
441
442/*
443 * Active scan: hop through the supplied channel list, dwelling `dwell_ms`
444 * milliseconds on each channel. Beacon / probe-response frames are routed
445 * to the chip parser (drv->parse_rx) which feeds vsf_wifi_on_scan_result.
446 * vsf_wifi_on_scan_done() is fired exactly once when every channel has
447 * been visited. dwell_ms == 0 falls back to a sensible default (120 ms).
448 */
450 const uint8_t *channels,
451 uint8_t num_channels, uint16_t dwell_ms);
453
454/* Channel bandwidth hints used by set_channel / connect. Not all chip
455 * drivers support 40/80 MHz; unsupported values fall back to 20 MHz. */
456#define WIFI_BW_20MHZ 0
457#define WIFI_BW_40MHZ_PLUS 1 /* primary lower, extension above */
458#define WIFI_BW_40MHZ_MINUS 2 /* primary upper, extension below */
459#define WIFI_BW_80MHZ 3
460
462
464 const uint8_t bssid[6],
465 const uint8_t *ssid, uint8_t ssid_len,
466 uint8_t channel);
470
471/*
472 * Transmit a raw 802.11 frame. The chip driver (drv->tx) is responsible for
473 * building the on-wire TX descriptor and shipping it over the bus. Returns
474 * VSF_ERR_NOT_READY before vsf_wifi_on_ready, VSF_ERR_NOT_SUPPORTED when the
475 * chip driver has no tx hook, or a bus-level error. Fire-and-forget: there
476 * is no per-frame completion callback (chip drivers use TX_STA_FIFO /
477 * TX_STA_CNT to inspect results). */
479 const uint8_t *frame, uint16_t len);
480
481/*============================ REGISTER-BUS LAYER API =========================*
482 *
483 * These are exported so a bus driver can drive the wifi state machine.
484 * Application code does not call them directly.
485 *==========================================================================*/
486
487/*
488 * One-shot construction. After this call:
489 * - wifi->drv is bound;
490 * - wifi->reg_bus is bound (may be NULL for non-register-based chips);
491 * - all state fields are zero / cleared;
492 * - scan timer is initialised but not armed;
493 * - the wifi is NOT started yet (call vsf_wifi_start when ready).
494 *
495 * `post_eda` is the EDA on which the wifi layer will post follow-up events
496 * (scan-hop callbacks). Typically the bus driver passes its own EDA so
497 * that timer-driven hops are handled in the same context as bus completions.
498 */
499void vsf_wifi_init(vsf_wifi_t *wifi,
500 const vsf_wifi_chip_drv_t *drv,
501 const vsf_wifi_reg_bus_t *reg_bus,
502 vsf_eda_t *post_eda);
503
504/*
505 * Kick off the chip bring-up chain (firmware_load -> init -> set_rx_filter).
506 * On success vsf_wifi_on_ready fires when the chain finishes; on any error
507 * the bus driver's `on_attach_fail` (passed via vsf_wifi_set_attach_fail)
508 * is invoked so it can tear down the device.
509 *
510 * Must be called from the bus driver's EDA context, typically on
511 * VSF_EVT_INIT.
512 */
513void vsf_wifi_start(vsf_wifi_t *wifi);
514
515/*
516 * Bus driver tear-down. Cancels pending scans / scripts, marks the wifi
517 * as disconnecting so no further callbacks fire, and invokes the chip
518 * driver's fini hook. After this call the wifi struct can be freed.
519 */
520void vsf_wifi_fini(vsf_wifi_t *wifi);
521
522/*
523 * Set a callback that the wifi layer invokes when the bring-up chain or a
524 * scan reports an unrecoverable error. The bus driver typically removes
525 * the device interface from inside this hook. Default: NULL (errors are
526 * logged but otherwise ignored).
527 */
528typedef void (*vsf_wifi_attach_fail_t)(vsf_wifi_t *wifi, vsf_err_t err);
530
531/* Bus driver hands an inbound RX frame to the wifi layer; the layer routes
532 * it to drv->parse_rx (during scan) or vsf_wifi_on_rx (otherwise). */
533void vsf_wifi_on_rx_internal(vsf_wifi_t *wifi, uint8_t *frame, uint16_t len);
534
535/* MLME management-frame entry point. The chip parser (drv->parse_rx) calls
536 * this with a de-descriptored, naked 802.11 management frame (starting at
537 * the FC field) when its subtype is auth / assoc-resp / deauth / disassoc.
538 * The wifi-layer MLME state machine advances the OPEN-system connection. */
539void vsf_wifi_mlme_rx(vsf_wifi_t *wifi, const uint8_t *dot11, uint16_t len);
540
541/* Data-frame entry point. The chip parser (drv->parse_rx) calls this with a
542 * de-descriptored, naked 802.11 data frame (starting at the FC field) once
543 * the link is associated (mlme_state RUN / 4WAY). The wifi layer parses the
544 * data header, detects EAPOL (LLC/SNAP + ethertype 0x888E) for the 4-way
545 * handshake, and forwards decrypted business payloads to vsf_wifi_on_rx. */
546void vsf_wifi_data_rx(vsf_wifi_t *wifi, const uint8_t *dot11, uint16_t len);
547
548#if VSF_WIFI_USE_WPA == ENABLED
549/* Helpers exported to the WPA 4-way handshake (vsf_wifi_wpa.c). The wifi
550 * layer owns the MLME state, the retry timer and the chip TX path; the WPA
551 * module owns EAPOL-Key parsing / building and key derivation, and drives
552 * the connection through these calls:
553 * - mlme_tx : transmit a fully formed 802.11 frame (EAPOL).
554 * - mlme_arm_timer : (re)arm the handshake timeout.
555 * - handshake_done : keys installed -> state RUN + vsf_wifi_on_link_up.
556 * - handshake_fail : abort the handshake -> link down with `reason`. */
557vsf_err_t vsf_wifi_mlme_tx(vsf_wifi_t *wifi, const uint8_t *frame, uint16_t len);
561#endif
562
563/* Bus driver invokes this from its EDA when the wifi-posted scan-hop event
564 * lands; the wifi layer advances to the next channel or finishes the scan. */
566
567/* Bus driver invokes this from its EDA when the wifi-posted MLME-retry event
568 * lands; the wifi layer retransmits the current handshake frame (auth-req /
569 * assoc-req) or declares a link-down timeout once retries are exhausted.
570 * Bounced through the bus EDA (like scan-hop) so the TX runs in the same
571 * context as bus completions. */
573
574/* Custom EDA event used by vsf_wifi to trigger a scan hop on the bus EDA.
575 * The bus driver's evt-handler must dispatch this value to
576 * vsf_wifi_on_scan_hop_evt(). */
577#define VSF_WIFI_EVT_SCAN_HOP (VSF_EVT_USER + 0x40)
578
579/* Custom EDA event used by vsf_wifi to trigger an MLME handshake retransmit
580 * on the bus EDA. The bus driver's evt-handler must dispatch this value to
581 * vsf_wifi_on_mlme_retry_evt(). */
582#define VSF_WIFI_EVT_MLME_RETRY (VSF_EVT_USER + 0x41)
583
584/*============================ CHIP <-> WIFI INTERNAL API ====================*/
585
586/*
587 * Run a sequence of register writes asynchronously.
588 *
589 * ops : array of (reg, val) pairs. May point to .rodata or to
590 * wifi->scratch_ops. Caller MUST keep `ops` valid until
591 * `done` fires.
592 * count : number of ops to execute (must be > 0 for a real submission).
593 * done : completion callback (NULL = fire-and-forget).
594 *
595 * Returns VSF_ERR_NONE on accepted submission (completion via `done`),
596 * VSF_ERR_NOT_AVAILABLE when another script / blob is in flight, or a bus-
597 * level synchronous error otherwise.
598 */
600 const vsf_wifi_reg_op_t *ops, uint16_t count,
601 vsf_wifi_done_t done);
602
603/*
604 * Stream a contiguous register-block payload. Internally calls
605 * reg_bus->reg_block_write when available; otherwise decomposes into
606 * ceil(len / 4) reg_write calls.
607 *
608 * Same concurrency rules as vsf_wifi_reg_run_script: at most one script /
609 * blob in flight per wifi.
610 */
612 const vsf_wifi_reg_blob_t *blob,
613 vsf_wifi_done_t done);
614
615/*
616 * Periodically read `reg` until `match(val)` returns true.
617 *
618 * reg : MAC register to poll.
619 * match : predicate; the read value is fed in on every iteration.
620 * max_retry : upper bound on read attempts (>= 1). The first read
621 * counts as attempt #1.
622 * interval_ms : delay between consecutive reads. 0 means "re-issue
623 * immediately on completion" (use only if the bus already
624 * paces reads, e.g. USB ep0 vendor request).
625 * done : completion callback.
626 * VSF_ERR_NONE : predicate matched.
627 * VSF_ERR_TIMEOUT : retries exhausted without match.
628 * other : bus-level error.
629 *
630 * Same single-flight constraint as run_script / run_blob.
631 */
634 uint16_t max_retry, uint16_t interval_ms,
635 vsf_wifi_done_t done);
636
637/*
638 * Single 32-bit register read. The result is stored at *out before
639 * `done` fires; caller must keep `out` valid until then. Same single-
640 * flight constraint as run_script / run_blob / run_read_poll.
641 */
643 vsf_wifi_done_t done);
644
645/* Per-wifi scratch op buffer (shared by parameterised chip ops). */
647
648/*============================ CHIP-SPECIFIC HEADERS =========================*
649 *
650 * Concrete chip families live in chip/xxx/ and expose their own firmware
651 * symbols, logging helpers and (if needed) public constants. The generic
652 * wifi header only pulls in the chips that are enabled at compile time.
653 *===========================================================================*/
654
655#if VSF_WIFI_USE_RT28XX == ENABLED
657#endif
658
659#if VSF_WIFI_USE_MT76 == ENABLED
661#endif
662
663/*============================ LOGGING HELPERS ===============================*
664 *
665 * Compile-time log level gates for the WiFi subsystem and its sub-modules.
666 * Levels are defined in vsf_wifi_cfg.h; sub-module macros fall back to the
667 * global VSF_WIFI_CFG_LOG_LEVEL when not explicitly set.
668 *==========================================================================*/
669
670#if VSF_WIFI_CFG_LOG_LEVEL >= 1
671# define vsf_wifi_trace_error(...) vsf_trace_error(__VA_ARGS__)
672#else
673# define vsf_wifi_trace_error(...) ((void)0)
674#endif
675
676#if VSF_WIFI_CFG_LOG_LEVEL >= 2
677# define vsf_wifi_trace_info(...) vsf_trace_info(__VA_ARGS__)
678#else
679# define vsf_wifi_trace_info(...) ((void)0)
680#endif
681
682#if VSF_WIFI_CFG_LOG_LEVEL >= 4
683# define vsf_wifi_trace_debug(...) vsf_trace_info(__VA_ARGS__)
684#else
685# define vsf_wifi_trace_debug(...) ((void)0)
686#endif
687
688
689#ifdef __cplusplus
690}
691#endif
692
693#endif // VSF_USE_WIFI
694#endif // __VSF_WIFI_H__
vsf_err_t
Definition __type.h:42
bool
Definition type.h:60
Definition vsf_eda.h:769
struct ieee80211_ext_chansw_ie data
Definition ieee80211.h:80
u8 bssid[ETH_ALEN]
Definition ieee80211.h:2
unsigned short uint16_t
Definition stdint.h:7
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
signed char int8_t
Definition stdint.h:4
Definition vsf_wifi.h:160
uint8_t pairwise_cipher
Definition vsf_wifi.h:162
uint8_t psk[32]
Definition vsf_wifi.h:164
uint8_t psk_len
Definition vsf_wifi.h:165
uint8_t group_cipher
Definition vsf_wifi.h:163
uint8_t auth_mode
Definition vsf_wifi.h:161
Definition vsf_wifi.h:304
vsf_err_t(* set_channel)(vsf_wifi_t *wifi, uint8_t channel, vsf_wifi_done_t done)
Definition vsf_wifi.h:314
vsf_err_t(* disconnect)(vsf_wifi_t *wifi, vsf_wifi_done_t done)
Definition vsf_wifi.h:329
void(* parse_rx)(vsf_wifi_t *wifi, uint8_t *frame, uint16_t len)
Definition vsf_wifi.h:337
vsf_err_t(* set_bssid)(vsf_wifi_t *wifi, const uint8_t bssid[6], vsf_wifi_done_t done)
Definition vsf_wifi.h:320
const vsf_wifi_radio_ops_t * radio_ops
Definition vsf_wifi.h:363
vsf_err_t(* get_link_info)(vsf_wifi_t *wifi, vsf_wifi_link_info_t *info)
Definition vsf_wifi.h:330
void(* fini)(vsf_wifi_t *wifi)
Definition vsf_wifi.h:313
vsf_err_t(* tx)(vsf_wifi_t *wifi, const uint8_t *frame, uint16_t len)
Definition vsf_wifi.h:353
vsf_err_t(* set_mac_addr)(vsf_wifi_t *wifi, const uint8_t mac[6], vsf_wifi_done_t done)
Definition vsf_wifi.h:318
const char * name
Definition vsf_wifi.h:305
const vsf_wifi_crypto_ops_t * crypto_ops
Definition vsf_wifi.h:358
vsf_err_t(* connect)(vsf_wifi_t *wifi, const uint8_t bssid[6], const uint8_t *ssid, uint8_t ssid_len, uint8_t channel, vsf_wifi_done_t done)
Definition vsf_wifi.h:325
uint16_t(* build_tx)(vsf_wifi_t *wifi, uint8_t *dst, uint16_t dst_cap, const uint8_t *frame, uint16_t frame_len)
Definition vsf_wifi.h:345
vsf_err_t(* firmware_load)(vsf_wifi_t *wifi, vsf_wifi_done_t done)
Definition vsf_wifi.h:311
vsf_err_t(* init)(vsf_wifi_t *wifi, vsf_wifi_done_t done)
Definition vsf_wifi.h:312
vsf_err_t(* set_auth_mode)(vsf_wifi_t *wifi, const vsf_wifi_auth_cfg_t *cfg, vsf_wifi_done_t done)
Definition vsf_wifi.h:322
vsf_err_t(* set_rx_filter)(vsf_wifi_t *wifi, uint32_t mask, vsf_wifi_done_t done)
Definition vsf_wifi.h:316
Definition vsf_wifi.h:294
vsf_err_t(* install_key)(vsf_wifi_t *wifi, uint8_t key_idx, bool pairwise, const uint8_t *key, uint8_t key_len, const uint8_t *mac, vsf_wifi_done_t done)
Definition vsf_wifi.h:295
vsf_err_t(* encrypt)(vsf_wifi_t *wifi, uint8_t *dot11, uint16_t *len, uint16_t cap)
Definition vsf_wifi.h:298
vsf_err_t(* decrypt)(vsf_wifi_t *wifi, uint8_t *dot11, uint16_t *len)
Definition vsf_wifi.h:300
Definition vsf_wifi.h:399
void(* on_link_down)(void *param, vsf_wifi_t *wifi, uint8_t reason)
Definition vsf_wifi.h:404
void(* on_rx)(void *param, vsf_wifi_t *wifi, uint8_t *frame, uint16_t len)
Definition vsf_wifi.h:400
void(* on_link_up)(void *param, vsf_wifi_t *wifi, const vsf_wifi_link_info_t *info)
Definition vsf_wifi.h:402
Definition vsf_wifi_radio.h:59
Definition vsf_wifi.h:187
uint16_t chunk_size
Definition vsf_wifi.h:191
uint32_t base_reg
Definition vsf_wifi.h:190
uint32_t len
Definition vsf_wifi.h:189
const uint8_t * data
Definition vsf_wifi.h:188
Definition vsf_wifi.h:214
vsf_err_t(* reg_read)(vsf_wifi_t *wifi, uint32_t reg, uint32_t *out, vsf_wifi_done_t done)
Definition vsf_wifi.h:230
vsf_err_t(* reg_block_write)(vsf_wifi_t *wifi, uint32_t base, const uint8_t *data, uint32_t len, vsf_wifi_done_t done)
Definition vsf_wifi.h:242
vsf_err_t(* data_tx)(vsf_wifi_t *wifi, uint8_t *data, uint16_t len)
Definition vsf_wifi.h:258
vsf_err_t(* reg_write)(vsf_wifi_t *wifi, uint32_t reg, uint32_t val, vsf_wifi_done_t done)
Definition vsf_wifi.h:222
void(* on_ready)(vsf_wifi_t *wifi)
Definition vsf_wifi.h:250
Definition vsf_wifi.h:175
uint32_t reg
Definition vsf_wifi.h:176
uint32_t val
Definition vsf_wifi.h:177
Definition vsf_wifi.h:132
int8_t rssi
Definition vsf_wifi.h:137
uint8_t ssid[33]
Definition vsf_wifi.h:134
uint8_t ht40_width
Definition vsf_wifi.h:147
uint8_t ht40_plus
Definition vsf_wifi.h:148
uint16_t capability
Definition vsf_wifi.h:138
uint8_t auth_mode
Definition vsf_wifi.h:144
uint8_t wmm
Definition vsf_wifi.h:140
uint8_t group_cipher
Definition vsf_wifi.h:146
uint8_t channel
Definition vsf_wifi.h:136
uint8_t bssid[6]
Definition vsf_wifi.h:133
uint8_t pairwise_cipher
Definition vsf_wifi.h:145
uint8_t flags
Definition vsf_wifi.h:139
uint8_t ssid_len
Definition vsf_wifi.h:135
Definition vsf_wifi_priv.h:53
class vsf_$ * reg
bool vsf_wifi_is_ready(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1697
void vsf_wifi_on_del(vsf_wifi_t *wifi)
Definition vsf_wifi.c:97
void vsf_wifi_on_ready(vsf_wifi_t *wifi)
Definition vsf_wifi.c:104
void vsf_wifi_data_rx(vsf_wifi_t *wifi, const uint8_t *dot11, uint16_t len)
Definition vsf_wifi.c:1025
vsf_err_t vsf_wifi_reg_run_script(vsf_wifi_t *wifi, const vsf_wifi_reg_op_t *ops, uint16_t count, vsf_wifi_done_t done)
Definition vsf_wifi.c:348
vsf_err_t vsf_wifi_scan(vsf_wifi_t *wifi, const uint8_t *channels, uint8_t num_channels, uint16_t dwell_ms)
Definition vsf_wifi.c:1775
bool(* vsf_wifi_reg_match_fn_t)(uint32_t val)
Definition vsf_wifi.h:130
vsf_err_t vsf_wifi_disconnect(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1856
void vsf_wifi_on_rx_internal(vsf_wifi_t *wifi, uint8_t *frame, uint16_t len)
Definition vsf_wifi.c:757
struct vsf_wifi_link_info_t vsf_wifi_link_info_t
const uint8_t * vsf_wifi_get_mac(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1712
struct vsf_wifi_netif_ops_t vsf_wifi_netif_ops_t
@ WIFI_CIPHER_TKIP
Definition vsf_wifi.h:53
@ WIFI_CIPHER_TKIP_CCMP
Definition vsf_wifi.h:55
@ WIFI_CIPHER_CCMP
Definition vsf_wifi.h:54
@ WIFI_CIPHER_NONE
Definition vsf_wifi.h:50
@ WIFI_CIPHER_WEP40
Definition vsf_wifi.h:51
@ WIFI_CIPHER_WEP104
Definition vsf_wifi.h:52
void vsf_wifi_mlme_handshake_fail(vsf_wifi_t *wifi, uint8_t reason)
Definition vsf_wifi.c:2082
struct vsf_wifi_scan_result_t vsf_wifi_scan_result_t
vsf_err_t vsf_wifi_reg_read_poll(vsf_wifi_t *wifi, uint32_t reg, vsf_wifi_reg_match_fn_t match, uint16_t max_retry, uint16_t interval_ms, vsf_wifi_done_t done)
Definition vsf_wifi.c:550
vsf_err_t vsf_wifi_reg_run_blob(vsf_wifi_t *wifi, const vsf_wifi_reg_blob_t *blob, vsf_wifi_done_t done)
Definition vsf_wifi.c:427
vsf_err_t vsf_wifi_set_bssid(vsf_wifi_t *wifi, const uint8_t bssid[6])
Definition vsf_wifi.c:1750
void vsf_wifi_start(vsf_wifi_t *wifi)
Definition vsf_wifi.c:650
uint8_t vsf_wifi_get_channel(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1702
@ WIFI_REASON_DISASSOC_LEAVING
Definition vsf_wifi.h:89
@ WIFI_REASON_AUTH_REJECTED
Definition vsf_wifi.h:91
@ WIFI_REASON_LOCAL_TIMEOUT
Definition vsf_wifi.h:90
@ WIFI_REASON_ASSOC_REJECTED
Definition vsf_wifi.h:92
@ WIFI_REASON_MIC_FAILURE
Definition vsf_wifi.h:88
@ WIFI_REASON_UNSPECIFIED
Definition vsf_wifi.h:86
@ WIFI_REASON_LOCAL_DISCONNECT
Definition vsf_wifi.h:93
@ WIFI_REASON_AUTH_LEAVING
Definition vsf_wifi.h:87
@ WIFI_MLME_ASSOC
Definition vsf_wifi.h:74
@ WIFI_MLME_IDLE
Definition vsf_wifi.h:72
@ WIFI_MLME_AUTH
Definition vsf_wifi.h:73
@ WIFI_MLME_KEY_INSTALL
Definition vsf_wifi.h:77
@ WIFI_MLME_RUN
Definition vsf_wifi.h:76
@ WIFI_MLME_4WAY
Definition vsf_wifi.h:75
@ WIFI_MLME_RAW_RADIO
Definition vsf_wifi.h:78
const char * vsf_wifi_get_chip_name(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1707
void vsf_wifi_on_mlme_retry_evt(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1537
void vsf_wifi_mlme_rx(vsf_wifi_t *wifi, const uint8_t *dot11, uint16_t len)
Definition vsf_wifi.c:1573
void vsf_wifi_on_link_down(vsf_wifi_t *wifi, uint8_t reason)
Definition vsf_wifi.c:119
void vsf_wifi_on_scan_hop_evt(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1257
vsf_err_t vsf_wifi_mlme_tx(vsf_wifi_t *wifi, const uint8_t *frame, uint16_t len)
Definition vsf_wifi.c:1922
vsf_err_t vsf_wifi_set_auth_mode(vsf_wifi_t *wifi, const vsf_wifi_auth_cfg_t *cfg)
Definition vsf_wifi.c:1757
void vsf_wifi_on_rx(vsf_wifi_t *wifi, uint8_t *frame, uint16_t len)
Definition vsf_wifi.c:100
struct vsf_wifi_auth_cfg_t vsf_wifi_auth_cfg_t
void vsf_wifi_netdrv_attach(vsf_wifi_t *wifi, const vsf_wifi_netif_ops_t *ops, void *param)
Definition vsf_wifi.c:129
void vsf_wifi_set_channel(vsf_wifi_t *wifi, uint8_t channel)
Definition vsf_wifi.c:1722
void vsf_wifi_mlme_arm_timer(vsf_wifi_t *wifi, uint16_t ms)
Definition vsf_wifi.c:1927
vsf_wifi_reg_op_t * vsf_wifi_reg_get_scratch_ops(vsf_wifi_t *wifi)
Definition vsf_wifi.c:470
void vsf_wifi_fini(vsf_wifi_t *wifi)
Definition vsf_wifi.c:728
vsf_err_t vsf_wifi_reg_read(vsf_wifi_t *wifi, uint32_t reg, uint32_t *out, vsf_wifi_done_t done)
Definition vsf_wifi.c:596
void vsf_wifi_on_link_up(vsf_wifi_t *wifi, const vsf_wifi_link_info_t *info)
Definition vsf_wifi.c:115
void vsf_wifi_netdrv_detach(vsf_wifi_t *wifi)
Definition vsf_wifi.c:136
void vsf_wifi_on_scan_result(vsf_wifi_t *wifi, const vsf_wifi_scan_result_t *result)
Definition vsf_wifi.c:107
vsf_err_t vsf_wifi_get_link_info(vsf_wifi_t *wifi, vsf_wifi_link_info_t *info)
Definition vsf_wifi.c:1873
void vsf_wifi_on_new(vsf_wifi_t *wifi)
Definition vsf_wifi.c:94
struct vsf_wifi_reg_blob_t vsf_wifi_reg_blob_t
void vsf_wifi_on_scan_done(vsf_wifi_t *wifi)
Definition vsf_wifi.c:112
void vsf_wifi_set_attach_fail(vsf_wifi_t *wifi, vsf_wifi_attach_fail_t hook)
Definition vsf_wifi.c:645
vsf_err_t vsf_wifi_tx(vsf_wifi_t *wifi, const uint8_t *frame, uint16_t len)
Definition vsf_wifi.c:1892
void(* vsf_wifi_done_t)(vsf_wifi_t *wifi, vsf_err_t err)
Definition vsf_wifi.h:123
vsf_err_t vsf_wifi_connect(vsf_wifi_t *wifi, const uint8_t bssid[6], const uint8_t *ssid, uint8_t ssid_len, uint8_t channel)
Definition vsf_wifi.c:1819
uint16_t vsf_wifi_get_bss_max_idle_period(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1717
struct vsf_wifi_reg_op_t vsf_wifi_reg_op_t
struct vsf_wifi_crypto_ops_t vsf_wifi_crypto_ops_t
void vsf_wifi_set_channel_bw(vsf_wifi_t *wifi, uint8_t bw)
Definition vsf_wifi.c:1735
void vsf_wifi_mlme_handshake_done(vsf_wifi_t *wifi)
Definition vsf_wifi.c:2052
void vsf_wifi_init(vsf_wifi_t *wifi, const vsf_wifi_chip_drv_t *drv, const vsf_wifi_reg_bus_t *reg_bus, vsf_eda_t *post_eda)
Definition vsf_wifi.c:619
vsf_err_t vsf_wifi_scan_stop(vsf_wifi_t *wifi)
Definition vsf_wifi.c:1806
vsf_err_t vsf_wifi_set_mac(vsf_wifi_t *wifi, const uint8_t mac[6])
Definition vsf_wifi.c:1743
void(* vsf_wifi_attach_fail_t)(vsf_wifi_t *wifi, vsf_err_t err)
Definition vsf_wifi.h:528
@ WIFI_AUTH_WPA2_PSK
Definition vsf_wifi.h:44
@ WIFI_AUTH_WPA_WPA2_PSK
Definition vsf_wifi.h:45
@ WIFI_AUTH_WEP
Definition vsf_wifi.h:42
@ WIFI_AUTH_WPA2_ENTERPRISE
Definition vsf_wifi.h:46
@ WIFI_AUTH_WPA_PSK
Definition vsf_wifi.h:43
@ WIFI_AUTH_OPEN
Definition vsf_wifi.h:41
Generated from commit: vsfteam/vsf@ceb53fd