diff --git a/drivers/input/usbhidd/src/descs.rs b/drivers/input/usbhidd/src/descs.rs index dddd00b39d..23742fcd14 100644 --- a/drivers/input/usbhidd/src/descs.rs +++ b/drivers/input/usbhidd/src/descs.rs @@ -1,4 +1,4 @@ -use anyhow::{Result, anyhow}; +use anyhow::{anyhow, Result}; use plain::Plain; use smallvec::SmallVec; use std::convert::TryInto; @@ -9,7 +9,7 @@ pub enum HidClassType { #[default] HID = 0x21, Report, - Physical + Physical, } #[repr(C, packed)] @@ -48,41 +48,45 @@ impl HidDescriptor { if (kind != HidClassType::HID as u8) { return Err(anyhow!("This is not a hid descriptor")); } - + let num_descriptors = bytes[5]; if (length != Self::HID_DESC_FIXED_SIZE + num_descriptors * Self::HID_CLASS_DESC_SIZE) { - return Err(anyhow!("Len doesn't match the given number of descriptors ({num_descriptors})")); + return Err(anyhow!( + "Len doesn't match the given number of descriptors ({num_descriptors})" + )); } - - let mut descriptors = SmallVec::<[HidClassDescriptor; 1]>::with_capacity(num_descriptors as usize); - + + let mut descriptors = + SmallVec::<[HidClassDescriptor; 1]>::with_capacity(num_descriptors as usize); + for i in 0..num_descriptors { - match HidClassDescriptor::from_bytes(&bytes[(Self::HID_DESC_FIXED_SIZE + i*Self::HID_CLASS_DESC_SIZE) as usize..(Self::HID_DESC_FIXED_SIZE + (i+1)*Self::HID_CLASS_DESC_SIZE) as usize]) { + match HidClassDescriptor::from_bytes( + &bytes[(Self::HID_DESC_FIXED_SIZE + i * Self::HID_CLASS_DESC_SIZE) as usize + ..(Self::HID_DESC_FIXED_SIZE + (i + 1) * Self::HID_CLASS_DESC_SIZE) as usize], + ) { Ok(desc) => descriptors.push(*desc), Err(e) => return Err(anyhow!("{e:?}")), } } - + Ok(Self { length, kind, hid_spec_release: u16::from_ne_bytes(bytes[2..4].try_into()?), country_code: bytes[4], num_descriptors, - descriptors + descriptors, }) } - pub fn get_report_desc(&self) -> Result { for desc in self.descriptors.iter() { match desc.desc_type { HidClassType::Report => return Ok(*desc), - _ => () + _ => (), } } return Err(anyhow!("No Report descriptor found")); - } -} \ No newline at end of file +} diff --git a/drivers/input/usbhidd/src/main.rs b/drivers/input/usbhidd/src/main.rs index b3cb97e98e..d49f1c1111 100644 --- a/drivers/input/usbhidd/src/main.rs +++ b/drivers/input/usbhidd/src/main.rs @@ -15,8 +15,8 @@ use xhcid_interface::{ use crate::descs::HidDescriptor; -mod reqs; mod descs; +mod reqs; fn send_key_event(display: &mut ProducerHandle, usage_page: u16, usage: u16, pressed: bool) { let scancode = match usage_page { diff --git a/drivers/usb/xhcid/src/xhci/scheme.rs b/drivers/usb/xhcid/src/xhci/scheme.rs index fe80d2062d..30ee55a5b2 100644 --- a/drivers/usb/xhcid/src/xhci/scheme.rs +++ b/drivers/usb/xhcid/src/xhci/scheme.rs @@ -477,7 +477,6 @@ impl From for EndpDesc { } } - impl From for SuperSpeedCmp { fn from(d: usb::SuperSpeedCompanionDescriptor) -> Self { Self { @@ -1525,60 +1524,74 @@ impl Xhci { let mut unknown_descs = SmallVec::new(); let mut iface_unknown_descs = SmallVec::<[UnknownDesc; 1]>::new(); let mut iter = descriptors.into_iter().peekable(); - + let mut cur_iface = None; let mut cur_endpoint = None; let mut endpoints = SmallVec::<[EndpDesc; 4]>::new(); while let Some(item) = iter.next() { debug!("Processing descriptor {item:?}"); - - + match item { AnyDescriptor::Interface(idesc) => { if let Some(cur_idesc) = cur_iface { - if let Some(endp) = cur_endpoint{ + if let Some(endp) = cur_endpoint { if !endpoints.contains(&endp) { endpoints.push(endp); } } interface_descs.push( - self.new_if_desc(port_id, slot, cur_idesc, endpoints, iface_unknown_descs, lang_id) - .await?, + self.new_if_desc( + port_id, + slot, + cur_idesc, + endpoints, + iface_unknown_descs, + lang_id, + ) + .await?, ); } cur_iface = Some(idesc); endpoints = SmallVec::new(); iface_unknown_descs = SmallVec::new(); - }, + } AnyDescriptor::Endpoint(endpdesc) => { if let Some(desc) = cur_endpoint { endpoints.push(desc); } cur_endpoint = Some(EndpDesc::from(endpdesc)); - }, + } AnyDescriptor::SuperSpeedCompanion(ssc) => { if let Some(endp) = cur_endpoint.as_mut() { endp.ssc = Some(SuperSpeedCmp::from(ssc.clone())); } else { error!("Found a superspeedcompanion descriptor before encountering any endpoint descriptor."); } - }, + } AnyDescriptor::SuperSpeedPlusCompanion(sscp) => { if let Some(endp) = cur_endpoint.as_mut() { endp.sspc = Some(SuperSpeedPlusIsochCmp::from(sscp.clone())); } else { error!("Found a superspeedpluscompanion descriptor before encountering any endpoint descriptor."); } - }, + } AnyDescriptor::Unknown(bytes) => { // If we're processing an interface, add the desc to it, otherwise add it to the configuration if cur_iface.is_none() { - unknown_descs.push(UnknownDesc {len: bytes[0], kind: bytes[1], all_bytes: bytes}); + unknown_descs.push(UnknownDesc { + len: bytes[0], + kind: bytes[1], + all_bytes: bytes, + }); } else { - iface_unknown_descs.push(UnknownDesc {len: bytes[0], kind: bytes[1], all_bytes: bytes}); + iface_unknown_descs.push(UnknownDesc { + len: bytes[0], + kind: bytes[1], + all_bytes: bytes, + }); } - }, + } _ => { error!("Found unexpected descriptor {item:?} while parsing config descriptors, skipping"); continue; @@ -1587,18 +1600,27 @@ impl Xhci { } // Push the last endpoint - if let Some(endp) = cur_endpoint{ + if let Some(endp) = cur_endpoint { if !endpoints.contains(&endp) { endpoints.push(endp); } } - + // Push the last interface if let Some(idesc) = cur_iface { - let new_if_desc = self.new_if_desc(port_id, slot, idesc, endpoints, iface_unknown_descs, lang_id).await?; - if !interface_descs.contains(&new_if_desc) { - interface_descs.push(new_if_desc); - } + let new_if_desc = self + .new_if_desc( + port_id, + slot, + idesc, + endpoints, + iface_unknown_descs, + lang_id, + ) + .await?; + if !interface_descs.contains(&new_if_desc) { + interface_descs.push(new_if_desc); + } } config_descs.push(ConfDesc { @@ -1615,7 +1637,7 @@ impl Xhci { attributes: desc.attributes, max_power: desc.max_power, interface_descs, - unknown_descs + unknown_descs, }); }