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 / crates / aienos-kernel / src / usb / descriptor.rs
| 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 | //! USB descriptor parsing for keyboard bring-up (USB 2.0 section 9.6, HID |
| 17 | //! 1.11 appendix B): the device descriptor's control packet size and the |
| 18 | //! boot keyboard interface inside a configuration descriptor. |
| 19 | |
| 20 | const DESCRIPTOR_DEVICE: u8 = 1; |
| 21 | const DESCRIPTOR_CONFIGURATION: u8 = 2; |
| 22 | const DESCRIPTOR_INTERFACE: u8 = 4; |
| 23 | const DESCRIPTOR_ENDPOINT: u8 = 5; |
| 24 | const CLASS_HID: u8 = 3; |
| 25 | const SUBCLASS_BOOT: u8 = 1; |
| 26 | const PROTOCOL_KEYBOARD: u8 = 1; |
| 27 | const TRANSFER_INTERRUPT: u8 = 3; |
| 28 | |
| 29 | /// bMaxPacketSize0 from the first 8 bytes of a device descriptor. |
| 30 | pub fn control_max_packet(device_descriptor: &[u8]) -> Option<u8> { |
| 31 | match device_descriptor { |
| 32 | [len, DESCRIPTOR_DEVICE, _, _, _, _, _, size, ..] if *len >= 8 && *size > 0 => Some(*size), |
| 33 | _ => None, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /// Where a boot-protocol keyboard's reports come from. |
| 38 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 39 | pub struct BootKeyboard { |
| 40 | /// bConfigurationValue to pass to SET_CONFIGURATION. |
| 41 | pub configuration: u8, |
| 42 | pub interface: u8, |
| 43 | /// Interrupt-IN endpoint address, for example 0x81. |
| 44 | pub endpoint_address: u8, |
| 45 | pub max_packet: u16, |
| 46 | /// bInterval as the device states it. |
| 47 | pub interval: u8, |
| 48 | } |
| 49 | |
| 50 | /// First boot keyboard interface (class 3, subclass 1, protocol 1) with an |
| 51 | /// interrupt-IN endpoint in a configuration descriptor and what follows it. |
| 52 | /// Parsing stops at wTotalLength or the end of `bytes`, whichever is first, |
| 53 | /// and at any descriptor whose length is impossible. |
| 54 | pub fn find_boot_keyboard(bytes: &[u8]) -> Option<BootKeyboard> { |
| 55 | if bytes.len() < 9 || bytes[0] < 9 || bytes[1] != DESCRIPTOR_CONFIGURATION { |
| 56 | return None; |
| 57 | } |
| 58 | let total = usize::from(u16::from_le_bytes([bytes[2], bytes[3]])).min(bytes.len()); |
| 59 | let configuration = bytes[5]; |
| 60 | let mut interface = None; |
| 61 | let mut at = 0; |
| 62 | while at + 2 <= total { |
| 63 | let len = usize::from(bytes[at]); |
| 64 | if len < 2 || at + len > total { |
| 65 | return None; |
| 66 | } |
| 67 | let d = &bytes[at..at + len]; |
| 68 | match d[1] { |
| 69 | DESCRIPTOR_INTERFACE if len >= 9 => { |
| 70 | let keyboard = |
| 71 | d[5] == CLASS_HID && d[6] == SUBCLASS_BOOT && d[7] == PROTOCOL_KEYBOARD; |
| 72 | interface = keyboard.then_some(d[2]); |
| 73 | } |
| 74 | DESCRIPTOR_ENDPOINT if len >= 7 => { |
| 75 | let is_in = d[2] & 0x80 != 0; |
| 76 | if let (Some(interface), true, TRANSFER_INTERRUPT) = (interface, is_in, d[3] & 0x3) |
| 77 | { |
| 78 | return Some(BootKeyboard { |
| 79 | configuration, |
| 80 | interface, |
| 81 | endpoint_address: d[2], |
| 82 | max_packet: u16::from_le_bytes([d[4], d[5]]) & 0x7ff, |
| 83 | interval: d[6], |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | _ => {} |
| 88 | } |
| 89 | at += len; |
| 90 | } |
| 91 | None |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod tests { |
| 96 | use super::*; |
| 97 | |
| 98 | /// Configuration descriptor QEMU 8.2.2 usb-kbd returned to the driver in |
| 99 | /// the QEMU keyboard test: config, interface, HID, endpoint 0x81. |
| 100 | const KEYBOARD: [u8; 34] = [ |
| 101 | 9, 2, 34, 0, 1, 1, 8, 0xa0, 50, // |
| 102 | 9, 4, 0, 0, 1, 3, 1, 1, 0, // |
| 103 | 9, 0x21, 0x11, 0x01, 0, 1, 0x22, 63, 0, // |
| 104 | 7, 5, 0x81, 3, 8, 0, 7, |
| 105 | ]; |
| 106 | |
| 107 | #[test] |
| 108 | fn finds_the_keyboard_interrupt_in_endpoint() { |
| 109 | assert_eq!( |
| 110 | find_boot_keyboard(&KEYBOARD), |
| 111 | Some(BootKeyboard { |
| 112 | configuration: 1, |
| 113 | interface: 0, |
| 114 | endpoint_address: 0x81, |
| 115 | max_packet: 8, |
| 116 | interval: 7, |
| 117 | }) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn skips_a_mouse_interface_and_out_endpoints_in_a_composite_device() { |
| 123 | let composite: [u8; 50] = [ |
| 124 | 9, 2, 50, 0, 2, 2, 0, 0xa0, 50, // |
| 125 | 9, 4, 0, 0, 1, 3, 1, 2, 0, // boot mouse |
| 126 | 7, 5, 0x81, 3, 4, 0, 10, // |
| 127 | 9, 4, 1, 0, 2, 3, 1, 1, 0, // boot keyboard |
| 128 | 7, 5, 0x02, 3, 8, 0, 10, // interrupt OUT (LEDs): not this |
| 129 | 9, 0x21, 0x11, 0x01, 0, 1, 0x22, 63, 0, |
| 130 | ]; |
| 131 | let mut with_endpoint = [0u8; 57]; |
| 132 | with_endpoint[..50].copy_from_slice(&composite); |
| 133 | with_endpoint[50..].copy_from_slice(&[7, 5, 0x83, 3, 8, 0, 4]); |
| 134 | with_endpoint[2] = 57; |
| 135 | let found = find_boot_keyboard(&with_endpoint).unwrap(); |
| 136 | assert_eq!((found.configuration, found.interface), (2, 1)); |
| 137 | assert_eq!(found.endpoint_address, 0x83); |
| 138 | assert_eq!(found.interval, 4); |
| 139 | assert_eq!( |
| 140 | find_boot_keyboard(&composite), |
| 141 | None, |
| 142 | "mouse only, no keyboard IN" |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn stops_at_total_length_and_malformed_descriptors() { |
| 148 | let mut short_total = KEYBOARD; |
| 149 | short_total[2] = 27; // total length excludes the endpoint |
| 150 | assert_eq!(find_boot_keyboard(&short_total), None); |
| 151 | let mut zero_len = KEYBOARD; |
| 152 | zero_len[9] = 0; |
| 153 | assert_eq!(find_boot_keyboard(&zero_len), None, "no endless loop"); |
| 154 | let mut overrun = KEYBOARD; |
| 155 | overrun[27] = 200; |
| 156 | assert_eq!(find_boot_keyboard(&overrun), None); |
| 157 | assert_eq!(find_boot_keyboard(&KEYBOARD[..20]), None, "truncated read"); |
| 158 | assert_eq!( |
| 159 | find_boot_keyboard(&[9, 1, 0, 0]), |
| 160 | None, |
| 161 | "not a configuration" |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn reads_the_control_packet_size_from_a_device_descriptor() { |
| 167 | assert_eq!( |
| 168 | control_max_packet(&[18, 1, 0x00, 0x02, 0, 0, 0, 64]), |
| 169 | Some(64) |
| 170 | ); |
| 171 | assert_eq!( |
| 172 | control_max_packet(&[18, 1, 0x10, 0x01, 0, 0, 0, 8, 0x27, 0x06]), |
| 173 | Some(8) |
| 174 | ); |
| 175 | assert_eq!(control_max_packet(&[18, 2, 0, 0, 0, 0, 0, 8]), None); |
| 176 | assert_eq!(control_max_packet(&[18, 1, 0, 0, 0, 0, 0, 0]), None); |
| 177 | assert_eq!(control_max_packet(&[18, 1, 0]), None); |
| 178 | } |
| 179 | } |