garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

cartopolis-agent · 2026-11-30T16:33:19Z

refactor(hid): read a keyboard's boot report in pinnacle-hid

The USB driver's boot keyboard decoding (usages to Linux key codes,
modifier bits, the too-many-keys report) moves to pinnacle_hid::Keyboard,
so the Bluetooth service reads a keyboard's boot reports with the same
code. A test covers presses, Shift, the roll-over report and releases.
hid/src/keyboard.rs           | 102 ++++++++++++++++++++++++++++++++++++++++++
 hid/src/lib.rs                |   6 ++-
 hid/src/tests.rs              |  18 ++++++++
 kernel/src/drivers/usb/hid.rs |  57 ++++-------------------
 4 files changed, 133 insertions(+), 50 deletions(-)

diff --git a/hid/src/keyboard.rs b/hid/src/keyboard.rs
new file mode 100644
index 0000000..c55131f
--- /dev/null
+++ b/hid/src/keyboard.rs
@@ -0,0 +1,102 @@
+//! A keyboard's boot report: the eight bytes every keyboard can send
+//! (HID 1.11, appendix B.1), whether over USB or Bluetooth (HID over GATT's
+//! Boot Keyboard Input Report). A byte of modifier bits, a reserved one,
+//! then up to six keys held, as usages of the keyboard page; each report
+//! says what is held now, and [`Keyboard`] turns the difference from the
+//! last into presses and releases of Linux's key codes, which the display
+//! server reads.
+
+use alloc::vec::Vec;
+
+/// Linux's key code for each keyboard usage (HID Usage Tables, page 7), as
+/// its `usbkbd` driver has them.
+const KEYS: [u8; 256] = [
+    0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, //
+    50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3, //
+    4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26, //
+    27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64, //
+    65, 66, 67, 68, 87, 88, 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, //
+    105, 108, 103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, //
+    72, 73, 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190, //
+    191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135, 136, 113, //
+    115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94, 95, 0, 0, 0, //
+    122, 123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
+    29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115, 114, 113, //
+    150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140, 0, 0, 0, 0, //
+];
+
+/// The key codes of the modifier bits of a boot report's first byte: left
+/// Ctrl, Shift, Alt, Meta, then the right ones.
+const MODIFIERS: [u16; 8] = [29, 42, 56, 125, 97, 54, 100, 126];
+
+/// Linux's key code for a keyboard usage, 0 for none.
+pub fn key_code(usage: u8) -> u16 {
+    u16::from(KEYS[usize::from(usage)])
+}
+
+/// The usage of a key code, for whoever makes boot reports (a simulated
+/// keyboard): the first usage that maps to it.
+pub fn usage(code: u16) -> Option<u8> {
+    (4..=255u8).find(|&u| key_code(u) == code && code != 0)
+}
+
+/// A key pressed (`down`) or released, as a Linux key code.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct Key {
+    pub code: u16,
+    pub down: bool,
+}
+
+/// A keyboard sending boot reports: what it held at its last.
+#[derive(Clone, Debug, Default, PartialEq, Eq)]
+pub struct Keyboard {
+    previous: [u8; 8],
+}
+
+impl Keyboard {
+    /// A report arrived: the keys that went down or up since the last,
+    /// modifiers first, releases before presses.
+    pub fn report(&mut self, report: &[u8]) -> Vec<Key> {
+        let mut out = Vec::new();
+        let mut now = [0u8; 8];
+        let n = report.len().min(8);
+        now[..n].copy_from_slice(&report[..n]);
+        // A keyboard says it has too many keys down with all of them 1.
+        if now[2..].iter().all(|&k| k == 1) {
+            return out;
+        }
+        let before = self.previous;
+        let changed = before[0] ^ now[0];
+        for (bit, &code) in MODIFIERS.iter().enumerate() {
+            if changed & (1 << bit) != 0 {
+                out.push(Key {
+                    code,
+                    down: now[0] >> bit & 1 != 0,
+                });
+            }
+        }
+        for &k in before[2..].iter().filter(|&&k| k > 3 && !now[2..].contains(&k)) {
+            out.push(Key {
+                code: key_code(k),
+                down: false,
+            });
+        }
+        for &k in now[2..].iter().filter(|&&k| k > 3 && !before[2..].contains(&k)) {
+            out.push(Key {
+                code: key_code(k),
+                down: true,
+            });
+        }
+        self.previous = now;
+        out
+    }
+
+    /// Everything held let go, as when the keyboard goes away.
+    pub fn release_all(&mut self) -> Vec<Key> {
+        self.report(&[0; 8])
+    }
+}
diff --git a/hid/src/lib.rs b/hid/src/lib.rs
index c80c2a0..06fcaa1 100644
--- a/hid/src/lib.rs
+++ b/hid/src/lib.rs
@@ -4,7 +4,9 @@
 //! X, Y, wheel and buttons are ([`parse`]), and each report it sends
 //! becomes pointer events ([`Pointer`]): an absolute device (a tablet, a
 //! touch screen) points where it points, a relative one (a mouse, a
-//! touchpad in its mouse mode) moves a pointer kept on the screen.
+//! touchpad in its mouse mode) moves a pointer kept on the screen. A
+//! keyboard's boot report becomes key presses ([`Keyboard`]), from USB or
+//! from Bluetooth alike.
 //!
 //! [`i2c`] is HID over I²C (Microsoft's HID over I2C protocol
 //! specification 1.0): how a touchpad behind a laptop's I²C controller
@@ -17,8 +19,10 @@
 extern crate alloc;
 
 pub mod i2c;
+mod keyboard;
 mod pointer;
 
+pub use keyboard::{Key, Keyboard, key_code, usage};
 pub use pointer::{Pointer, PointerEvent};
 
 use alloc::vec::Vec;
diff --git a/hid/src/tests.rs b/hid/src/tests.rs
index bd60824..478a6f1 100644
--- a/hid/src/tests.rs
+++ b/hid/src/tests.rs
@@ -463,3 +463,21 @@ fn a_descriptor_with_no_pointer_is_refused() {
     bus.descriptor[4..6].copy_from_slice(&(config.len() as u16).to_le_bytes());
     assert_eq!(i2c::bring_up(&mut bus, 0x15, 1, |_| {}), Err(Error::NoPointer));
 }
+
+/// A boot report's keys: "a" down, Shift with it, "a" up, all up; a
+/// report saying too many keys are held changes nothing.
+#[test]
+fn a_boot_keyboard_report_becomes_presses_and_releases() {
+    use crate::{Key, Keyboard};
+    const A: u16 = 30;
+    const SHIFT: u16 = 42;
+    let key = |code, down| Key { code, down };
+    let mut k = Keyboard::default();
+    assert_eq!(k.report(&[0, 0, 4, 0, 0, 0, 0, 0]), [key(A, true)]);
+    assert_eq!(k.report(&[0x02, 0, 4, 0, 0, 0, 0, 0]), [key(SHIFT, true)]);
+    assert!(k.report(&[0x02, 0, 1, 1, 1, 1, 1, 1]).is_empty());
+    assert_eq!(k.report(&[0x02, 0, 0, 0, 0, 0, 0, 0]), [key(A, false)]);
+    assert_eq!(k.release_all(), [key(SHIFT, false)]);
+    assert_eq!(crate::usage(A), Some(4));
+    assert_eq!(crate::key_code(0x28), 28, "Enter");
+}
diff --git a/kernel/src/drivers/usb/hid.rs b/kernel/src/drivers/usb/hid.rs
index d8a8da8..4340680 100644
--- a/kernel/src/drivers/usb/hid.rs
+++ b/kernel/src/drivers/usb/hid.rs
@@ -6,8 +6,8 @@
 //! for where its X, Y, wheel and buttons are: an absolute one (a tablet, a
 //! touch screen) moves to where it points, as the virtio tablet does; a
 //! relative one (a mouse) moves a pointer the kernel keeps on the screen.
-//! The descriptor and the pointer are `pinnacle_hid`'s, shared with the
-//! touchpad on I²C (`drivers::i2c_hid`).
+//! The descriptor, the pointer and the keyboard's report are `pinnacle_hid`'s,
+//! shared with the touchpad on I²C (`drivers::i2c_hid`) and Bluetooth (`bt`).
 
 use super::{Controller, Descriptor, Interface, configure_endpoints, start_reading};
 use crate::mm::pages;
@@ -16,36 +16,11 @@ use crate::sync::SpinLock;
 use alloc::string::String;
 use alloc::vec::Vec;
 use pinnacle_abi::InputEvent;
-use pinnacle_hid::{Layout, Pointer, parse};
+use pinnacle_hid::{Keyboard, Layout, Pointer, parse};
 
 /// What each device attached is, for the self-check and Settings.
 pub static ATTACHED: SpinLock<Vec<String>> = SpinLock::new(Vec::new());
 
-/// Linux's key code for each keyboard usage (HID Usage Tables, page 7), as
-/// its `usbkbd` driver has them: what the display server reads.
-const KEYS: [u8; 256] = [
-    0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, //
-    50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3, //
-    4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26, //
-    27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64, //
-    65, 66, 67, 68, 87, 88, 99, 70, 119, 110, 102, 104, 111, 107, 109, 106, //
-    105, 108, 103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71, //
-    72, 73, 82, 83, 86, 127, 116, 117, 183, 184, 185, 186, 187, 188, 189, 190, //
-    191, 192, 193, 194, 134, 138, 130, 132, 128, 129, 131, 137, 133, 135, 136, 113, //
-    115, 114, 0, 0, 0, 121, 0, 89, 93, 124, 92, 94, 95, 0, 0, 0, //
-    122, 123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
-    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
-    29, 42, 56, 125, 97, 54, 100, 126, 164, 166, 165, 163, 161, 115, 114, 113, //
-    150, 158, 159, 128, 136, 177, 178, 176, 142, 152, 173, 140, 0, 0, 0, 0, //
-];
-
-/// The key codes of the modifier bits of a boot report's first byte: left
-/// Ctrl, Shift, Alt, Meta, then the right ones.
-const MODIFIERS: [u16; 8] = [29, 42, 56, 125, 97, 54, 100, 126];
-
 #[derive(Clone)]
 pub(super) enum Kind {
     Keyboard,
@@ -59,7 +34,8 @@ pub struct Reader {
     pub buffer: usize,
     pub length: usize,
     kind: Kind,
-    previous: [u8; 8],
+    /// A keyboard's keys held at its last report.
+    keys: Keyboard,
     /// The pointer a relative device moves, and its buttons held.
     pointer: Pointer,
 }
@@ -98,26 +74,9 @@ impl Reader {
     }
 
     fn keyboard(&mut self, report: &[u8], out: &mut Vec<InputEvent>) {
-        let mut now = [0u8; 8];
-        now[..report.len().min(8)].copy_from_slice(&report[..report.len().min(8)]);
-        // A keyboard says it has too many keys down with all of them 1.
-        if now[2..].iter().all(|&k| k == 1) {
-            return;
-        }
-        let before = self.previous;
-        let changed = before[0] ^ now[0];
-        for (bit, &code) in MODIFIERS.iter().enumerate() {
-            if changed & (1 << bit) != 0 {
-                out.push(event(InputEvent::KEY, code, i32::from(now[0] >> bit & 1), 0, 0));
-            }
-        }
-        for &k in before[2..].iter().filter(|&&k| k > 3 && !now[2..].contains(&k)) {
-            out.push(event(InputEvent::KEY, u16::from(KEYS[usize::from(k)]), 0, 0, 0));
-        }
-        for &k in now[2..].iter().filter(|&&k| k > 3 && !before[2..].contains(&k)) {
-            out.push(event(InputEvent::KEY, u16::from(KEYS[usize::from(k)]), 1, 0, 0));
+        for key in self.keys.report(report) {
+            out.push(event(InputEvent::KEY, key.code, i32::from(key.down), 0, 0));
         }
-        self.previous = now;
     }
 
     fn pointer(&mut self, layout: &Layout, report: &[u8], out: &mut Vec<InputEvent>) {
@@ -176,7 +135,7 @@ impl Reader {
             buffer,
             length,
             kind,
-            previous: [0; 8],
+            keys: Keyboard::default(),
             pointer: Pointer::new(width, height),
         }
     }