aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / usb_hid.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 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 | /* usb_hid.h -- pure logic of the C kernel operator input (no MMIO, host |
| 18 | * tested in svc/tests/stage_test.c): USB HID boot-keyboard report decode to a |
| 19 | * small fixed keymap, the configuration-descriptor walk that finds a boot |
| 20 | * keyboard, the operator line editor (bounded, fails closed on overflow), the |
| 21 | * console shell commands of the Rust SEED-0A keyboard gate, and the |
| 22 | * recovery-access key decision. |
| 23 | * |
| 24 | * Behaviour follows the Rust reference where the Rust gate |
| 25 | * (scripts/qemu_keyboard_test.sh) observes it: crates/aienos-kernel/src/usb/ |
| 26 | * hid.rs (BootKeyboardDecoder: an event per newly pressed usage, rollover |
| 27 | * reports ignored), usb/descriptor.rs (find_boot_keyboard) and shell.rs |
| 28 | * (LINE_CAPACITY 64, TOKEN_CAPACITY 16, commands help mem el report uptime |
| 29 | * exit). One deliberate difference: the Rust line silently drops keys past |
| 30 | * its capacity; this editor refuses the whole line instead (fail closed). |
| 31 | * Freestanding. QEMU is not hardware: nothing here is physically qualified. */ |
| 32 | #ifndef AIENOS_CK_USB_HID_H |
| 33 | #define AIENOS_CK_USB_HID_H |
| 34 | #include <stddef.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | /* ---- HID boot keyboard report (HID 1.11 appendix B.1) ---- */ |
| 38 | #define CK_HID_REPORT_LEN 8u |
| 39 | #define CK_HID_MAX_KEYS 6u |
| 40 | enum ck_key_kind { CK_KEY_NONE = 0, CK_KEY_CHAR, CK_KEY_ENTER, CK_KEY_BACKSPACE, CK_KEY_ESCAPE, |
| 41 | CK_KEY_UP, CK_KEY_DOWN, CK_KEY_LEFT, CK_KEY_RIGHT, CK_KEY_TAB, /* moves: the line editor ignores them */ |
| 42 | CK_KEY_PGUP, CK_KEY_PGDN, CK_KEY_HOME, CK_KEY_END }; /* reading keys (code on the screen): ignored too */ |
| 43 | typedef struct { |
| 44 | uint8_t kind; /* enum ck_key_kind */ |
| 45 | char c; /* CK_KEY_CHAR only */ |
| 46 | } ck_key_event; |
| 47 | typedef struct { |
| 48 | uint8_t previous[CK_HID_MAX_KEYS]; |
| 49 | } ck_hid_decoder; |
| 50 | |
| 51 | /* Fixed keymap (US layout, the only one): every printable ASCII character a |
| 52 | * US keyboard types, with and without shift (a-z A-Z, 0-9 and !@#$%^&*(), |
| 53 | * space, and the symbol keys - = [ ] \ ; ' ` , . / and their shifted |
| 54 | * _ + { } | : " ~ < > ?), plus Enter, Backspace, Escape, the four arrows and |
| 55 | * Tab (usages 0x4f..0x52, 0x2b), which only move a choice, and Home, Page Up, |
| 56 | * End and Page Down (0x4a, 0x4b, 0x4d, 0x4e), which only scroll the code on the |
| 57 | * screen (USB HID Usage Tables 1.5 p91-92). Every other usage (function keys, |
| 58 | * the non-US key) yields nothing. */ |
| 59 | ck_key_event ck_hid_usage(uint8_t usage, int shift); |
| 60 | /* Decode one 8-byte report: one event per usage that was not pressed in the |
| 61 | * previous report (key repeat is not generated). A phantom/rollover report |
| 62 | * (first usage 0x01) and a report of the wrong length change nothing. |
| 63 | * Returns the number of events written to out (at most CK_HID_MAX_KEYS). */ |
| 64 | unsigned ck_hid_decode(ck_hid_decoder *d, const uint8_t *report, size_t len, ck_key_event out[CK_HID_MAX_KEYS]); |
| 65 | |
| 66 | /* ---- configuration descriptor: first boot keyboard ---- */ |
| 67 | typedef struct { |
| 68 | uint8_t configuration; /* bConfigurationValue */ |
| 69 | uint8_t interface; /* bInterfaceNumber of class 3 / subclass 1 / protocol 1 */ |
| 70 | uint8_t endpoint; /* bEndpointAddress of its first interrupt IN endpoint */ |
| 71 | uint16_t max_packet; /* wMaxPacketSize bits 10:0 */ |
| 72 | uint8_t interval; /* bInterval */ |
| 73 | } ck_boot_kbd; |
| 74 | /* 0 and *out filled, or -1 (no boot keyboard interface with an interrupt IN |
| 75 | * endpoint, or a malformed descriptor: every length is bounds checked, the |
| 76 | * walk never reads past min(len, wTotalLength)). */ |
| 77 | int ck_usb_find_boot_kbd(const uint8_t *cfg, size_t len, ck_boot_kbd *out); |
| 78 | |
| 79 | /* ---- operator line editor ---- */ |
| 80 | #define CK_LINE_CAP 64u /* Rust shell LINE_CAPACITY */ |
| 81 | enum { CK_LINE_MORE = 0, CK_LINE_DONE = 1, CK_LINE_OVERFLOW = 2 }; |
| 82 | typedef struct { |
| 83 | char buf[CK_LINE_CAP + 1]; |
| 84 | unsigned len; |
| 85 | unsigned typed; /* printable keys typed into this line, kept or not */ |
| 86 | int overflow; /* a key arrived with the buffer full: line refused */ |
| 87 | } ck_line; |
| 88 | void ck_line_reset(ck_line *l); |
| 89 | /* Feed one key. Printable keys are appended and echoed (echo(c) for the |
| 90 | * character, echo('\b') for a rubbed-out one; echo may be NULL). Enter ends |
| 91 | * the line: CK_LINE_DONE with buf NUL-terminated, or CK_LINE_OVERFLOW when |
| 92 | * any key was lost to the bound (the line is then emptied and must not be |
| 93 | * used). Escape clears the line. */ |
| 94 | int ck_line_feed(ck_line *l, ck_key_event ev, void (*echo)(char c)); |
| 95 | |
| 96 | /* ---- console session: two input sources, one line (C3-1a) ---- |
| 97 | * Serial (PL011) and USB keys feed the same ck_line. A line is owned by the |
| 98 | * first source that types a printable key into it, until Enter (or Escape, which clears the |
| 99 | * line, or an overflow refusal). Keys from the other source meanwhile are |
| 100 | * dropped and counted; they never reach the line editor. */ |
| 101 | enum { CK_SRC_NONE = 0, CK_SRC_SERIAL = 1, CK_SRC_USB = 2 }; |
| 102 | typedef struct { |
| 103 | int owner; /* CK_SRC_* owning the line being typed, or NONE */ |
| 104 | unsigned dropped; /* keys dropped from the non-owner in this line */ |
| 105 | unsigned dropped_all; /* keys dropped since reset, all lines */ |
| 106 | } ck_src_gate; |
| 107 | void ck_src_gate_reset(ck_src_gate *g); |
| 108 | /* 1 when ev from src may enter the line (a CK_KEY_CHAR claims it when unowned; other keys on an |
| 109 | * unowned line pass without claiming), 0 when |
| 110 | * dropped (and counted). A CK_KEY_NONE event is always 0 and never counted. */ |
| 111 | int ck_src_gate_accept(ck_src_gate *g, int src, ck_key_event ev); |
| 112 | /* The line ended (Enter, Escape, overflow): the line is unowned again and the |
| 113 | * per-line drop count restarts. Returns the drop count of the line just ended. */ |
| 114 | unsigned ck_src_gate_release(ck_src_gate *g); |
| 115 | /* One serial byte to a key event: CR or LF = Enter (an LF right after a CR is |
| 116 | * ignored: *prev carries the last byte), 0x08 and 0x7f = Backspace, 0x1b = |
| 117 | * Escape, a-z A-Z 0-9 space - . / = CK_KEY_CHAR, everything else CK_KEY_NONE |
| 118 | * (printable keys only; the same set as the USB keymap). */ |
| 119 | ck_key_event ck_serial_key(uint8_t byte, uint8_t *prev); |
| 120 | |
| 121 | /* ---- console shell (Rust shell.rs commands) ---- */ |
| 122 | enum { CK_SH_INVALID = -1, CK_SH_UNKNOWN = 0, CK_SH_HELP, CK_SH_MEM, CK_SH_EL, CK_SH_REPORT, CK_SH_UPTIME, CK_SH_EXIT, |
| 123 | CK_SH_EMPTY }; |
| 124 | /* Rust parse_line: at most CK_LINE_CAP bytes, first word at most 16 bytes, at |
| 125 | * most one argument of at most 16 bytes; else CK_SH_INVALID. An all-blank |
| 126 | * line is CK_SH_EMPTY (Rust: None, "invalid command line"). */ |
| 127 | int ck_shell_parse(const char *line, size_t len); |
| 128 | typedef struct { |
| 129 | uint64_t conventional_memory_kb; |
| 130 | unsigned exception_level; |
| 131 | uint64_t uptime_ms; |
| 132 | const char *commit; |
| 133 | } ck_shell_ctx; |
| 134 | /* Run one line; output goes through out(). Returns 1 when the line was |
| 135 | * "exit". Output texts are the Rust ones ("commands: help mem el report |
| 136 | * uptime exit", "conventional_memory_kb: N", "EL1", "uptime_ms: N", |
| 137 | * "unknown command: X", "invalid command line"). */ |
| 138 | int ck_shell_run(const char *line, const ck_shell_ctx *ctx, void (*out)(const char *fmt, ...)); |
| 139 | |
| 140 | /* ---- recovery access ---- */ |
| 141 | enum { CK_RECOVERY_NORMAL_TIMEOUT = 0, CK_RECOVERY_NORMAL_KEY = 1, CK_RECOVERY_CONSOLE = 2 }; |
| 142 | /* The first key the operator presses in the recovery window decides: 'r' or |
| 143 | * 'R' selects the recovery console, any other key continues the normal boot. |
| 144 | * have_key == 0 (no key before the window closed) continues normally. */ |
| 145 | int ck_recovery_decide(int have_key, ck_key_event ev); |
| 146 | const char *ck_recovery_name(int choice); |
| 147 | /* Identity digest the recovery console stub prints: SHA-256 over |
| 148 | * "AIENOS-CK-RECOVERY-IDENTITY-V1" || 0x00 || commit (the image build |
| 149 | * identity; not an owner or machine identity, which do not exist yet). */ |
| 150 | void ck_recovery_identity(const char *commit, uint8_t out[32]); |
| 151 | #endif |