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_kbd.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_kbd.h -- operator keyboard for the AIENOS C kernel: a polled xHCI |
| 18 | * driver for one USB HID boot keyboard, the operator line input, the console |
| 19 | * shell of the Rust SEED-0A keyboard gate, and the recovery-access hook. |
| 20 | * Port of crates/aienos-kernel/src/usb/xhci (controller.rs, ring.rs, |
| 21 | * context.rs, trb.rs) and crates/aienos-boot/src/usb_keyboard.rs (run), with |
| 22 | * the same console lines where the Rust gate greps them. |
| 23 | * |
| 24 | * Runs only inside the xHCI DMA fence (xhci_fence.c), between the |
| 25 | * SMMU-confined grant and the revoke: every ring, context and buffer lives |
| 26 | * in the fence's DMA region, which is the controller stream's only SMMU |
| 27 | * window. No SMMU confinement means this code never runs (fail closed). |
| 28 | * Polled only: no interrupt, no scheduler, no Store. |
| 29 | * |
| 30 | * Boot flow (devices stage, after NVMe and virtio-net): |
| 31 | * 1. reset and start the controller, find the first connected port with a |
| 32 | * boot keyboard, address it, configure its interrupt IN endpoint |
| 33 | * ("keyboard: ready (port P, slot S, endpoint 0xEE)"); |
| 34 | * 2. recovery access: wait at most CK_KBD_RECOVERY_WAIT_MS for one key. |
| 35 | * 'r' selects the recovery console, any other key or no key continues |
| 36 | * ("recovery_access: choice=..."); |
| 37 | * 3. normal boot: the console shell ("aienos> ", lines echoed, Enter ends a |
| 38 | * line: "keyboard_echo:", "keyboard_line:", "keyboard: done (enter)", |
| 39 | * then the command output) until "exit", CK_KBD_SHELL_IDLE_MS without a |
| 40 | * key, CK_KBD_SHELL_MS in all, or a halted endpoint; |
| 41 | * 4. the fence then halts the controller and revokes its DMA. A recovery |
| 42 | * choice is acted on by devices.c after that revoke: every device DMA |
| 43 | * released, then ck_recovery_console_stub (identity digest, halt). |
| 44 | * QEMU is not hardware: nothing here is physically qualified. */ |
| 45 | #ifndef AIENOS_CK_USB_KBD_H |
| 46 | #define AIENOS_CK_USB_KBD_H |
| 47 | #include <stddef.h> |
| 48 | #include <stdint.h> |
| 49 | |
| 50 | #ifndef CK_KBD_RECOVERY_WAIT_MS |
| 51 | #define CK_KBD_RECOVERY_WAIT_MS 5000u |
| 52 | #endif |
| 53 | #ifndef CK_KBD_SHELL_MS |
| 54 | #define CK_KBD_SHELL_MS 60000u |
| 55 | #endif |
| 56 | #ifndef CK_KBD_SHELL_IDLE_MS |
| 57 | #define CK_KBD_SHELL_IDLE_MS 15000u |
| 58 | #endif |
| 59 | /* Pages of the fence's DMA region the driver uses (4 KiB each). */ |
| 60 | #define CK_KBD_DMA_PAGES 16u |
| 61 | #define CK_KBD_MAX_SCRATCHPADS 6u |
| 62 | |
| 63 | enum { |
| 64 | CK_KBD_OK = 0, |
| 65 | CK_KBD_E_ARG = -651, /* bad region or registers */ |
| 66 | CK_KBD_E_UNSUP = -652, /* controller needs something this driver lacks */ |
| 67 | CK_KBD_E_TIMEOUT = -653, /* a bounded wait ran out */ |
| 68 | CK_KBD_E_FAILED = -654, /* a command or transfer completed with an error */ |
| 69 | CK_KBD_E_NOKBD = -655, /* no boot keyboard on any connected port */ |
| 70 | }; |
| 71 | |
| 72 | /* Steps 1-3 on a controller the fence has granted (bus master on, stream |
| 73 | * confined) and halted. mem/phys: the fence's DMA region (identity mapped, |
| 74 | * Normal Non-cacheable, CK_KBD_DMA_PAGES pages). Never touches PCI config |
| 75 | * or the SMMU; the caller revokes afterwards whatever this returns. */ |
| 76 | int ck_kbd_phase(volatile uint8_t *bar0, uint8_t *mem, uint64_t phys, size_t bytes); |
| 77 | /* After the devices stage's xHCI step: prints the recovery-access line when |
| 78 | * the phase never offered it (no keyboard: absent, denied, failed). */ |
| 79 | void ck_kbd_recovery_report(const char *xhci_state); |
| 80 | /* 1 when the operator chose the recovery console. */ |
| 81 | int ck_kbd_recovery_requested(void); |
| 82 | /* Recovery console, cut-1 stub: prints the image identity digest and halts |
| 83 | * this CPU for good (interrupts masked, WFI). The caller must have released |
| 84 | * every device's DMA first. Never returns. */ |
| 85 | __attribute__((noreturn)) void ck_recovery_console_stub(void); |
| 86 | |
| 87 | #if defined(CK_CONSOLE_SESSION) && CK_CONSOLE_SESSION |
| 88 | /* Console session (C3-1a, TEST-ONLY QEMU image): the operator console that |
| 89 | * keeps taking serial and USB input until "exit". ck_kbd_session_phase runs |
| 90 | * inside the xHCI fence in place of ck_kbd_phase; ck_console_session_serial_only |
| 91 | * runs the serial half alone (no keyboard, or its endpoint halted); |
| 92 | * ck_console_session_ended is 1 once the shell ran "exit". */ |
| 93 | int ck_kbd_session_phase(volatile uint8_t *bar0, uint8_t *mem, uint64_t phys, size_t bytes); |
| 94 | void ck_console_session_serial_only(void); |
| 95 | int ck_console_session_ended(void); |
| 96 | #endif |
| 97 | #endif |