diff --git a/lib/usb/src/hid/report.rs b/lib/usb/src/hid/report.rs index 27c975a801..f8c7045fcf 100644 --- a/lib/usb/src/hid/report.rs +++ b/lib/usb/src/hid/report.rs @@ -615,154 +615,185 @@ pub enum Usage { KeyRightShift, KeyRightAlt, KeyRightGui, + + // TODO: there are in theory up to 65535 buttons supported. Do we want to encode that here?? + ButtonNone, + Button1, + Button2, + Button3, + Button4, + Button5, + X, + Y, + Z, + Wheel, } pub fn translate_usage(usage_page: u16, usage_id: u32) -> Option { match usage_page { - 0x07 => { - // Keyboard/Keypad page - match usage_id { - 0x04 => Some(Usage::KeyA), - 0x05 => Some(Usage::KeyB), - 0x06 => Some(Usage::KeyC), - 0x07 => Some(Usage::KeyD), - 0x08 => Some(Usage::KeyE), - 0x09 => Some(Usage::KeyF), - 0x0a => Some(Usage::KeyG), - 0x0b => Some(Usage::KeyH), - 0x0c => Some(Usage::KeyI), - 0x0d => Some(Usage::KeyJ), - 0x0e => Some(Usage::KeyK), - 0x0f => Some(Usage::KeyL), - 0x10 => Some(Usage::KeyM), - 0x11 => Some(Usage::KeyN), - 0x12 => Some(Usage::KeyO), - 0x13 => Some(Usage::KeyP), - 0x14 => Some(Usage::KeyQ), - 0x15 => Some(Usage::KeyR), - 0x16 => Some(Usage::KeyS), - 0x17 => Some(Usage::KeyT), - 0x18 => Some(Usage::KeyU), - 0x19 => Some(Usage::KeyV), - 0x1a => Some(Usage::KeyW), - 0x1b => Some(Usage::KeyX), - 0x1c => Some(Usage::KeyY), - 0x1d => Some(Usage::KeyZ), - 0x1e => Some(Usage::Key1), - 0x1f => Some(Usage::Key2), - 0x20 => Some(Usage::Key3), - 0x21 => Some(Usage::Key4), - 0x22 => Some(Usage::Key5), - 0x23 => Some(Usage::Key6), - 0x24 => Some(Usage::Key7), - 0x25 => Some(Usage::Key8), - 0x26 => Some(Usage::Key9), - 0x27 => Some(Usage::Key0), - 0x28 => Some(Usage::KeyReturn), - 0x29 => Some(Usage::KeyEscape), - 0x2a => Some(Usage::KeyDelete), - 0x2b => Some(Usage::KeyTab), - 0x2c => Some(Usage::KeySpace), - 0x2d => Some(Usage::KeyDash), - 0x2e => Some(Usage::KeyEquals), - 0x2f => Some(Usage::KeyLeftBracket), - 0x30 => Some(Usage::KeyRightBracket), - 0x31 => Some(Usage::KeyForwardSlash), - 0x32 => Some(Usage::KeyPound), - 0x33 => Some(Usage::KeySemicolon), - 0x34 => Some(Usage::KeyApostrophe), - 0x35 => Some(Usage::KeyGrave), - 0x36 => Some(Usage::KeyComma), - 0x37 => Some(Usage::KeyDot), - 0x38 => Some(Usage::KeyBackSlash), - 0x39 => Some(Usage::KeyCapslock), - 0x3a => Some(Usage::KeyF1), - 0x3b => Some(Usage::KeyF2), - 0x3c => Some(Usage::KeyF3), - 0x3d => Some(Usage::KeyF4), - 0x3e => Some(Usage::KeyF5), - 0x3f => Some(Usage::KeyF6), - 0x40 => Some(Usage::KeyF7), - 0x41 => Some(Usage::KeyF8), - 0x42 => Some(Usage::KeyF9), - 0x43 => Some(Usage::KeyF10), - 0x44 => Some(Usage::KeyF11), - 0x45 => Some(Usage::KeyF12), - 0x46 => Some(Usage::KeyPrintScreen), - 0x47 => Some(Usage::KeyScrolllock), - 0x48 => Some(Usage::KeyPause), - 0x49 => Some(Usage::KeyInsert), - 0x4a => Some(Usage::KeyHome), - 0x4b => Some(Usage::KeyPageUp), - 0x4c => Some(Usage::KeyDeleteForward), - 0x4d => Some(Usage::KeyEnd), - 0x4e => Some(Usage::KeyPageDown), - 0x4f => Some(Usage::KeyRightArrow), - 0x50 => Some(Usage::KeyLeftArrow), - 0x51 => Some(Usage::KeyDownArrow), - 0x52 => Some(Usage::KeyUpArrow), - 0x53 => Some(Usage::KeyNumlock), - 0x54 => Some(Usage::KeypadSlash), - 0x55 => Some(Usage::KeypadAsterix), - 0x56 => Some(Usage::KeypadDash), - 0x57 => Some(Usage::KeypadPlus), - 0x58 => Some(Usage::KeypadEnter), - 0x59 => Some(Usage::Keypad1), - 0x5a => Some(Usage::Keypad2), - 0x5b => Some(Usage::Keypad3), - 0x5c => Some(Usage::Keypad4), - 0x5d => Some(Usage::Keypad5), - 0x5e => Some(Usage::Keypad6), - 0x5f => Some(Usage::Keypad7), - 0x60 => Some(Usage::Keypad8), - 0x61 => Some(Usage::Keypad9), - 0x62 => Some(Usage::Keypad0), - 0x63 => Some(Usage::KeypadDot), - 0x64 => Some(Usage::KeypadNonUsBackSlash), - 0x65 => Some(Usage::KeyApplication), - 0x66 => Some(Usage::KeyPower), - 0x67 => Some(Usage::KeypadEquals), - 0x68 => Some(Usage::KeyF13), - 0x69 => Some(Usage::KeyF14), - 0x6a => Some(Usage::KeyF15), - 0x6b => Some(Usage::KeyF16), - 0x6c => Some(Usage::KeyF17), - 0x6d => Some(Usage::KeyF18), - 0x6e => Some(Usage::KeyF19), - 0x6f => Some(Usage::KeyF20), - 0x70 => Some(Usage::KeyF21), - 0x71 => Some(Usage::KeyF22), - 0x72 => Some(Usage::KeyF23), - 0x73 => Some(Usage::KeyF24), - 0x74 => Some(Usage::KeyExecute), - 0x75 => Some(Usage::KeyHelp), - 0x76 => Some(Usage::KeyMenu), - 0x77 => Some(Usage::KeySelect), - 0x78 => Some(Usage::KeyStop), - 0x79 => Some(Usage::KeyAgain), - 0x7a => Some(Usage::KeyUndo), - 0x7b => Some(Usage::KeyCut), - 0x7c => Some(Usage::KeyCopy), - 0x7d => Some(Usage::KeyPaste), - 0x7e => Some(Usage::KeyFind), - 0x7f => Some(Usage::KeyMute), - 0x80 => Some(Usage::KeyVolumeUp), - 0x81 => Some(Usage::KeyVolumeDown), - 0x82 => Some(Usage::KeyLockingCapslock), - 0x83 => Some(Usage::KeyLockingNumlock), - 0x84 => Some(Usage::KeyLockingScrolllock), - 0x85 => Some(Usage::KeypadComma), - 0xe0 => Some(Usage::KeyLeftControl), - 0xe1 => Some(Usage::KeyLeftShift), - 0xe2 => Some(Usage::KeyLeftAlt), - 0xe3 => Some(Usage::KeyLeftGui), - 0xe4 => Some(Usage::KeyRightControl), - 0xe5 => Some(Usage::KeyRightShift), - 0xe6 => Some(Usage::KeyRightAlt), - 0xe7 => Some(Usage::KeyRightGui), - _ => None, - } - } + // Generic Desktop Page + 0x01 => match usage_id { + 0x30 => Some(Usage::X), + 0x31 => Some(Usage::Y), + 0x32 => Some(Usage::Z), + 0x38 => Some(Usage::Wheel), + _ => None, + }, + + // Keyboard/Keypad page + 0x07 => match usage_id { + 0x04 => Some(Usage::KeyA), + 0x05 => Some(Usage::KeyB), + 0x06 => Some(Usage::KeyC), + 0x07 => Some(Usage::KeyD), + 0x08 => Some(Usage::KeyE), + 0x09 => Some(Usage::KeyF), + 0x0a => Some(Usage::KeyG), + 0x0b => Some(Usage::KeyH), + 0x0c => Some(Usage::KeyI), + 0x0d => Some(Usage::KeyJ), + 0x0e => Some(Usage::KeyK), + 0x0f => Some(Usage::KeyL), + 0x10 => Some(Usage::KeyM), + 0x11 => Some(Usage::KeyN), + 0x12 => Some(Usage::KeyO), + 0x13 => Some(Usage::KeyP), + 0x14 => Some(Usage::KeyQ), + 0x15 => Some(Usage::KeyR), + 0x16 => Some(Usage::KeyS), + 0x17 => Some(Usage::KeyT), + 0x18 => Some(Usage::KeyU), + 0x19 => Some(Usage::KeyV), + 0x1a => Some(Usage::KeyW), + 0x1b => Some(Usage::KeyX), + 0x1c => Some(Usage::KeyY), + 0x1d => Some(Usage::KeyZ), + 0x1e => Some(Usage::Key1), + 0x1f => Some(Usage::Key2), + 0x20 => Some(Usage::Key3), + 0x21 => Some(Usage::Key4), + 0x22 => Some(Usage::Key5), + 0x23 => Some(Usage::Key6), + 0x24 => Some(Usage::Key7), + 0x25 => Some(Usage::Key8), + 0x26 => Some(Usage::Key9), + 0x27 => Some(Usage::Key0), + 0x28 => Some(Usage::KeyReturn), + 0x29 => Some(Usage::KeyEscape), + 0x2a => Some(Usage::KeyDelete), + 0x2b => Some(Usage::KeyTab), + 0x2c => Some(Usage::KeySpace), + 0x2d => Some(Usage::KeyDash), + 0x2e => Some(Usage::KeyEquals), + 0x2f => Some(Usage::KeyLeftBracket), + 0x30 => Some(Usage::KeyRightBracket), + 0x31 => Some(Usage::KeyForwardSlash), + 0x32 => Some(Usage::KeyPound), + 0x33 => Some(Usage::KeySemicolon), + 0x34 => Some(Usage::KeyApostrophe), + 0x35 => Some(Usage::KeyGrave), + 0x36 => Some(Usage::KeyComma), + 0x37 => Some(Usage::KeyDot), + 0x38 => Some(Usage::KeyBackSlash), + 0x39 => Some(Usage::KeyCapslock), + 0x3a => Some(Usage::KeyF1), + 0x3b => Some(Usage::KeyF2), + 0x3c => Some(Usage::KeyF3), + 0x3d => Some(Usage::KeyF4), + 0x3e => Some(Usage::KeyF5), + 0x3f => Some(Usage::KeyF6), + 0x40 => Some(Usage::KeyF7), + 0x41 => Some(Usage::KeyF8), + 0x42 => Some(Usage::KeyF9), + 0x43 => Some(Usage::KeyF10), + 0x44 => Some(Usage::KeyF11), + 0x45 => Some(Usage::KeyF12), + 0x46 => Some(Usage::KeyPrintScreen), + 0x47 => Some(Usage::KeyScrolllock), + 0x48 => Some(Usage::KeyPause), + 0x49 => Some(Usage::KeyInsert), + 0x4a => Some(Usage::KeyHome), + 0x4b => Some(Usage::KeyPageUp), + 0x4c => Some(Usage::KeyDeleteForward), + 0x4d => Some(Usage::KeyEnd), + 0x4e => Some(Usage::KeyPageDown), + 0x4f => Some(Usage::KeyRightArrow), + 0x50 => Some(Usage::KeyLeftArrow), + 0x51 => Some(Usage::KeyDownArrow), + 0x52 => Some(Usage::KeyUpArrow), + 0x53 => Some(Usage::KeyNumlock), + 0x54 => Some(Usage::KeypadSlash), + 0x55 => Some(Usage::KeypadAsterix), + 0x56 => Some(Usage::KeypadDash), + 0x57 => Some(Usage::KeypadPlus), + 0x58 => Some(Usage::KeypadEnter), + 0x59 => Some(Usage::Keypad1), + 0x5a => Some(Usage::Keypad2), + 0x5b => Some(Usage::Keypad3), + 0x5c => Some(Usage::Keypad4), + 0x5d => Some(Usage::Keypad5), + 0x5e => Some(Usage::Keypad6), + 0x5f => Some(Usage::Keypad7), + 0x60 => Some(Usage::Keypad8), + 0x61 => Some(Usage::Keypad9), + 0x62 => Some(Usage::Keypad0), + 0x63 => Some(Usage::KeypadDot), + 0x64 => Some(Usage::KeypadNonUsBackSlash), + 0x65 => Some(Usage::KeyApplication), + 0x66 => Some(Usage::KeyPower), + 0x67 => Some(Usage::KeypadEquals), + 0x68 => Some(Usage::KeyF13), + 0x69 => Some(Usage::KeyF14), + 0x6a => Some(Usage::KeyF15), + 0x6b => Some(Usage::KeyF16), + 0x6c => Some(Usage::KeyF17), + 0x6d => Some(Usage::KeyF18), + 0x6e => Some(Usage::KeyF19), + 0x6f => Some(Usage::KeyF20), + 0x70 => Some(Usage::KeyF21), + 0x71 => Some(Usage::KeyF22), + 0x72 => Some(Usage::KeyF23), + 0x73 => Some(Usage::KeyF24), + 0x74 => Some(Usage::KeyExecute), + 0x75 => Some(Usage::KeyHelp), + 0x76 => Some(Usage::KeyMenu), + 0x77 => Some(Usage::KeySelect), + 0x78 => Some(Usage::KeyStop), + 0x79 => Some(Usage::KeyAgain), + 0x7a => Some(Usage::KeyUndo), + 0x7b => Some(Usage::KeyCut), + 0x7c => Some(Usage::KeyCopy), + 0x7d => Some(Usage::KeyPaste), + 0x7e => Some(Usage::KeyFind), + 0x7f => Some(Usage::KeyMute), + 0x80 => Some(Usage::KeyVolumeUp), + 0x81 => Some(Usage::KeyVolumeDown), + 0x82 => Some(Usage::KeyLockingCapslock), + 0x83 => Some(Usage::KeyLockingNumlock), + 0x84 => Some(Usage::KeyLockingScrolllock), + 0x85 => Some(Usage::KeypadComma), + 0xe0 => Some(Usage::KeyLeftControl), + 0xe1 => Some(Usage::KeyLeftShift), + 0xe2 => Some(Usage::KeyLeftAlt), + 0xe3 => Some(Usage::KeyLeftGui), + 0xe4 => Some(Usage::KeyRightControl), + 0xe5 => Some(Usage::KeyRightShift), + 0xe6 => Some(Usage::KeyRightAlt), + 0xe7 => Some(Usage::KeyRightGui), + _ => None, + }, + + // Button page + 0x09 => match usage_id { + 0x00 => Some(Usage::ButtonNone), + 0x01 => Some(Usage::Button1), + 0x02 => Some(Usage::Button2), + 0x03 => Some(Usage::Button3), + 0x04 => Some(Usage::Button4), + 0x05 => Some(Usage::Button5), + _ => None, + }, + _ => None, } } diff --git a/user/usb_hid/src/main.rs b/user/usb_hid/src/main.rs index 2bceb0bd9d..6d7f6cecea 100644 --- a/user/usb_hid/src/main.rs +++ b/user/usb_hid/src/main.rs @@ -105,6 +105,7 @@ pub fn main() { struct ConfigInfo { config_value: u8, interface_num: u8, + interface_protocol: u8, interface_setting: u8, endpoint_num: u8, packet_size: u16, @@ -117,6 +118,7 @@ pub fn main() { fn visit_interface(&mut self, descriptor: &InterfaceDescriptor) { self.interface_num = descriptor.interface_num; + self.interface_protocol = descriptor.interface_protocol; self.interface_setting = descriptor.alternate_setting; } @@ -142,10 +144,21 @@ pub fn main() { * TODO: we need to work out what devices actually are don't we... */ let (device_channel, device_channel_other_end) = Channel::::create().unwrap(); - let name = "usb-hid".to_string(); // TODO: proper name + // TODO: proper name + let name = "usb-hid".to_string(); + // TODO: make this a proper enum I think? + let typ = match config_info.interface_protocol { + 0 => "none", + 1 => "keyboard", + 2 => "mouse", + other => { + warn!("Reserved interface protocol in HID device descriptor: {}", other); + "reserved" + } + }; let device_info = { let mut info = BTreeMap::new(); - info.insert("hid.type".to_string(), Property::String("keyboard".to_string())); + info.insert("hid.type".to_string(), Property::String(typ.to_string())); DeviceInfo(info) }; let handoff_info = { @@ -180,6 +193,7 @@ pub fn main() { let report_desc = usb::hid::report::ReportDescriptorParser::parse(&bytes); report_desc }; + info!("Parsed report descriptor: {:#?}", report_desc); control_channel .send(&DeviceControlMessage::UseConfiguration(config_info.config_value)) @@ -225,6 +239,52 @@ pub fn main() { for field in report { match field { + FieldValue::UntranslatedSelector { usage_page, usage } => { + warn!("Received unknown selector in HID report: page={:#x}, usage={:#x}", usage_page, usage); + } + FieldValue::UntranslatedDynamicValue { usage_page, usage } => { + warn!("Received unknown dynamic value in HID report: page={:#x}, usage={:#x}", usage_page, usage); + } + + // TODO: move to mouse section idk + FieldValue::DynamicValue(Usage::X, value) => { + info!("X: {}", value); + } + FieldValue::DynamicValue(Usage::Y, value) => { + info!("Y: {}", value); + } + FieldValue::DynamicValue(Usage::Z, value) => { + info!("Z: {}", value); + } + FieldValue::DynamicValue(Usage::Wheel, value) => { + info!("Wheel: {}", value); + } + FieldValue::DynamicValue(Usage::Button1, value) => { + if value != 0 { + info!("Button 1 pressed"); + } + } + FieldValue::DynamicValue(Usage::Button2, value) => { + if value != 0 { + info!("Button 2 pressed"); + } + } + FieldValue::DynamicValue(Usage::Button3, value) => { + if value != 0 { + info!("Button 3 pressed"); + } + } + FieldValue::DynamicValue(Usage::Button4, value) => { + if value != 0 { + info!("Button 4 pressed"); + } + } + FieldValue::DynamicValue(Usage::Button5, value) => { + if value != 0 { + info!("Button 5 pressed"); + } + } + FieldValue::DynamicValue(Usage::KeyLeftControl, value) => { state.left_ctrl = value != 0; }