Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/dev/usb_hid.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / dev / usb_hid.c` 307 lines ยท 10972 bytes Raw Blame History 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.c -- operator input pure logic (see usb_hid.h). Freestanding. */ 18 #include "usb_hid.h" 19 #include "sha256.h" 20 21 #define USAGE_ENTER 0x28u 22 #define USAGE_ESCAPE 0x29u 23 #define USAGE_BACKSPACE 0x2au 24 #define USAGE_SPACE 0x2cu 25 #define USAGE_MINUS 0x2du 26 #define USAGE_SLASH 0x38u 27 #define MOD_ANY_SHIFT 0x22u /* left shift bit 1, right shift bit 5 */ 28 29 ck_key_event ck_hid_usage(uint8_t usage, int shift) 30 { 31 /* the US layout's symbol keys, usages 0x2d..0x38 (0x32, the non-US key, has none) */ 32 static const char sym[2][12] = {{'-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/'}, 33 {'_', '+', '{', '}', '|', 0, ':', '"', '~', '<', '>', '?'}}; 34 static const char shifted_digit[10] = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')'}; 35 ck_key_event ev = {CK_KEY_NONE, 0}; 36 if (usage >= 0x04 && usage <= 0x1d) { /* a..z */ 37 ev.kind = CK_KEY_CHAR; 38 ev.c = (char)((shift ? 'A' : 'a') + (usage - 0x04)); 39 } else if (usage >= 0x1e && usage <= 0x27) { /* 1..9, 0 */ 40 ev.kind = CK_KEY_CHAR; 41 ev.c = shift ? shifted_digit[usage - 0x1e] : usage == 0x27 ? '0' : (char)('1' + (usage - 0x1e)); 42 } else if (usage == USAGE_ENTER) { 43 ev.kind = CK_KEY_ENTER; 44 } else if (usage == USAGE_ESCAPE) { 45 ev.kind = CK_KEY_ESCAPE; 46 } else if (usage == USAGE_BACKSPACE) { 47 ev.kind = CK_KEY_BACKSPACE; 48 } else if (usage >= 0x4f && usage <= 0x52) { /* Right, Left, Down, Up arrow */ 49 static const uint8_t arrow[4] = {CK_KEY_RIGHT, CK_KEY_LEFT, CK_KEY_DOWN, CK_KEY_UP}; 50 ev.kind = arrow[usage - 0x4f]; 51 } else if (usage == 0x4a || usage == 0x4b || usage == 0x4d || usage == 0x4e) { /* Home, Page Up, End, Page Down */ 52 ev.kind = usage == 0x4a ? CK_KEY_HOME : usage == 0x4b ? CK_KEY_PGUP : usage == 0x4d ? CK_KEY_END : CK_KEY_PGDN; 53 } else if (usage == 0x2b) { /* Tab */ 54 ev.kind = CK_KEY_TAB; 55 } else if (usage == USAGE_SPACE) { 56 ev.kind = CK_KEY_CHAR; 57 ev.c = ' '; 58 } else if (usage >= USAGE_MINUS && usage <= USAGE_SLASH && sym[shift != 0][usage - USAGE_MINUS]) { 59 ev.kind = CK_KEY_CHAR; 60 ev.c = sym[shift != 0][usage - USAGE_MINUS]; 61 } 62 return ev; 63 } 64 65 unsigned ck_hid_decode(ck_hid_decoder *d, const uint8_t *report, size_t len, ck_key_event out[CK_HID_MAX_KEYS]) 66 { 67 if (!d || !report || len != CK_HID_REPORT_LEN) return 0; 68 const uint8_t *u = report + 2; 69 if (u[0] == 0x01) return 0; /* ErrorRollOver: keep the previous state */ 70 int shift = (report[0] & MOD_ANY_SHIFT) != 0; 71 unsigned n = 0; 72 for (unsigned i = 0; i < CK_HID_MAX_KEYS; i++) { 73 if (u[i] == 0) continue; 74 int held = 0; 75 for (unsigned j = 0; j < CK_HID_MAX_KEYS; j++) held |= d->previous[j] == u[i]; 76 if (held) continue; 77 ck_key_event ev = ck_hid_usage(u[i], shift); 78 if (ev.kind != CK_KEY_NONE) out[n++] = ev; 79 } 80 for (unsigned i = 0; i < CK_HID_MAX_KEYS; i++) d->previous[i] = u[i]; 81 return n; 82 } 83 84 int ck_usb_find_boot_kbd(const uint8_t *b, size_t len, ck_boot_kbd *out) 85 { 86 if (!b || !out || len < 9 || b[0] < 9 || b[1] != 2) return -1; 87 size_t total = (size_t)b[2] | ((size_t)b[3] << 8); 88 if (total > len) total = len; 89 uint8_t configuration = b[5]; 90 int iface = -1; 91 size_t at = 0; 92 while (at + 2 <= total) { 93 size_t l = b[at]; 94 if (l < 2 || at + l > total) return -1; 95 const uint8_t *d = b + at; 96 if (d[1] == 4 && l >= 9) { /* interface */ 97 iface = (d[5] == 3 && d[6] == 1 && d[7] == 1) ? d[2] : -1; 98 } else if (d[1] == 5 && l >= 7) { /* endpoint */ 99 if (iface >= 0 && (d[2] & 0x80u) && (d[3] & 0x3u) == 3u) { 100 out->configuration = configuration; 101 out->interface = (uint8_t)iface; 102 out->endpoint = d[2]; 103 out->max_packet = (uint16_t)(((unsigned)d[4] | ((unsigned)d[5] << 8)) & 0x7ffu); 104 out->interval = d[6]; 105 return out->max_packet ? 0 : -1; 106 } 107 } 108 at += l; 109 } 110 return -1; 111 } 112 113 void ck_line_reset(ck_line *l) 114 { 115 for (unsigned i = 0; i <= CK_LINE_CAP; i++) l->buf[i] = 0; 116 l->len = 0; 117 l->typed = 0; 118 l->overflow = 0; 119 } 120 121 int ck_line_feed(ck_line *l, ck_key_event ev, void (*echo)(char c)) 122 { 123 switch (ev.kind) { 124 case CK_KEY_CHAR: 125 l->typed++; 126 if (l->len >= CK_LINE_CAP) { 127 l->overflow = 1; /* fail closed: this line will be refused at Enter */ 128 return CK_LINE_MORE; 129 } 130 l->buf[l->len++] = ev.c; 131 l->buf[l->len] = 0; 132 if (echo) echo(ev.c); 133 return CK_LINE_MORE; 134 case CK_KEY_BACKSPACE: 135 if (l->len > 0 && !l->overflow) { 136 l->buf[--l->len] = 0; 137 if (echo) echo('\b'); 138 } 139 return CK_LINE_MORE; 140 case CK_KEY_ESCAPE: 141 ck_line_reset(l); 142 return CK_LINE_MORE; 143 case CK_KEY_ENTER: 144 if (l->overflow) { 145 unsigned typed = l->typed; 146 ck_line_reset(l); 147 l->typed = typed; /* reported by the caller, then reset again */ 148 l->overflow = 1; 149 return CK_LINE_OVERFLOW; 150 } 151 l->buf[l->len] = 0; 152 return CK_LINE_DONE; 153 default: 154 return CK_LINE_MORE; 155 } 156 } 157 158 static int is_space(char c) { return c == ' ' || c == '\t'; } 159 static int word_is(const char *w, size_t n, const char *lit) 160 { 161 size_t i = 0; 162 for (; i < n; i++) 163 if (lit[i] == 0 || lit[i] != w[i]) return 0; 164 return lit[i] == 0; 165 } 166 167 /* Splits like Rust split_ascii_whitespace; first and flen give the first word. */ 168 static int parse(const char *s, size_t len, const char **first, size_t *flen) 169 { 170 if (len > CK_LINE_CAP) return CK_SH_INVALID; 171 const char *w[3] = {0, 0, 0}; 172 size_t wl[3] = {0, 0, 0}; 173 unsigned nw = 0; 174 size_t i = 0; 175 while (i < len) { 176 while (i < len && is_space(s[i])) i++; 177 if (i >= len) break; 178 size_t st = i; 179 while (i < len && !is_space(s[i])) i++; 180 if (nw == 2) return CK_SH_INVALID; /* a third word */ 181 w[nw] = s + st; 182 wl[nw] = i - st; 183 nw++; 184 } 185 if (nw == 0) return CK_SH_EMPTY; 186 if (wl[0] > 16 || (nw == 2 && wl[1] > 16)) return CK_SH_INVALID; 187 *first = w[0]; 188 *flen = wl[0]; 189 if (word_is(w[0], wl[0], "help")) return CK_SH_HELP; 190 if (word_is(w[0], wl[0], "mem")) return CK_SH_MEM; 191 if (word_is(w[0], wl[0], "el")) return CK_SH_EL; 192 if (word_is(w[0], wl[0], "report")) return CK_SH_REPORT; 193 if (word_is(w[0], wl[0], "uptime")) return CK_SH_UPTIME; 194 if (word_is(w[0], wl[0], "exit")) return CK_SH_EXIT; 195 return CK_SH_UNKNOWN; 196 } 197 198 int ck_shell_parse(const char *line, size_t len) 199 { 200 const char *f = 0; 201 size_t fl = 0; 202 return parse(line, len, &f, &fl); 203 } 204 205 int ck_shell_run(const char *line, const ck_shell_ctx *ctx, void (*out)(const char *fmt, ...)) 206 { 207 size_t len = 0; 208 while (line[len] && len <= CK_LINE_CAP) len++; 209 const char *f = 0; 210 size_t fl = 0; 211 switch (parse(line, len, &f, &fl)) { 212 case CK_SH_HELP: out("commands: help mem el report uptime exit\n"); return 0; 213 case CK_SH_MEM: out("conventional_memory_kb: %llu\n", (unsigned long long)ctx->conventional_memory_kb); return 0; 214 case CK_SH_EL: out("EL%u\n", ctx->exception_level); return 0; 215 case CK_SH_REPORT: 216 out("report_kind: shell\naienos_commit: %s\nkernel: C (native/kernel)\n", ctx->commit ? ctx->commit : "unknown"); 217 return 0; 218 case CK_SH_UPTIME: out("uptime_ms: %llu\n", (unsigned long long)ctx->uptime_ms); return 0; 219 case CK_SH_EXIT: return 1; 220 case CK_SH_UNKNOWN: { 221 char name[17]; 222 size_t i = 0; 223 for (; i < fl && i < 16; i++) name[i] = f[i]; 224 name[i] = 0; 225 out("unknown command: %s\n", name); 226 return 0; 227 } 228 default: out("invalid command line\n"); return 0; 229 } 230 } 231 232 int ck_recovery_decide(int have_key, ck_key_event ev) 233 { 234 if (!have_key) return CK_RECOVERY_NORMAL_TIMEOUT; 235 if (ev.kind == CK_KEY_CHAR && (ev.c == 'r' || ev.c == 'R')) return CK_RECOVERY_CONSOLE; 236 return CK_RECOVERY_NORMAL_KEY; 237 } 238 239 const char *ck_recovery_name(int choice) 240 { 241 switch (choice) { 242 case CK_RECOVERY_CONSOLE: return "recovery reason=key-r"; 243 case CK_RECOVERY_NORMAL_KEY: return "normal reason=other-key"; 244 default: return "normal reason=timeout"; 245 } 246 } 247 248 void ck_recovery_identity(const char *commit, uint8_t out[32]) 249 { 250 static const char dom[] = "AIENOS-CK-RECOVERY-IDENTITY-V1"; /* hashed with its NUL */ 251 sha256_ctx c; 252 sha256_init(&c); 253 sha256_update(&c, (const uint8_t *)dom, sizeof dom); 254 size_t n = 0; 255 if (commit) 256 while (n < 64 && commit[n]) n++; 257 sha256_update(&c, (const uint8_t *)(commit ? commit : ""), n); 258 sha256_final(&c, out); 259 } 260 261 /* ---- console session: source ownership of the shared line ---- */ 262 void ck_src_gate_reset(ck_src_gate *g) 263 { 264 g->owner = CK_SRC_NONE; 265 g->dropped = 0; 266 g->dropped_all = 0; 267 } 268 269 int ck_src_gate_accept(ck_src_gate *g, int src, ck_key_event ev) 270 { 271 if (ev.kind == CK_KEY_NONE || (src != CK_SRC_SERIAL && src != CK_SRC_USB)) 272 return 0; 273 if (g->owner == CK_SRC_NONE) { 274 /* Only a printable key claims a line. Backspace, Escape and Enter on an 275 * empty line pass through without owning it, so they cannot lock the 276 * other source out. */ 277 if (ev.kind == CK_KEY_CHAR) g->owner = src; 278 return 1; 279 } 280 if (g->owner == src) 281 return 1; 282 g->dropped++; 283 g->dropped_all++; 284 return 0; 285 } 286 287 unsigned ck_src_gate_release(ck_src_gate *g) 288 { 289 unsigned d = g->dropped; 290 g->owner = CK_SRC_NONE; 291 g->dropped = 0; 292 return d; 293 } 294 295 ck_key_event ck_serial_key(uint8_t byte, uint8_t *prev) 296 { 297 ck_key_event none = {CK_KEY_NONE, 0}; 298 uint8_t before = *prev; 299 *prev = byte; 300 if (byte == '\r' || (byte == '\n' && before != '\r')) return (ck_key_event){CK_KEY_ENTER, 0}; 301 if (byte == 0x08 || byte == 0x7f) return (ck_key_event){CK_KEY_BACKSPACE, 0}; 302 if (byte == 0x1b) return (ck_key_event){CK_KEY_ESCAPE, 0}; 303 if ((byte >= 'a' && byte <= 'z') || (byte >= 'A' && byte <= 'Z') || (byte >= '0' && byte <= '9') || byte == ' ' || 304 byte == '-' || byte == '.' || byte == '/') 305 return (ck_key_event){CK_KEY_CHAR, (char)byte}; 306 return none; 307 }