Skip to content

Commit

Permalink
Fix: Avoid judging speed from its USB version and adjust Debug implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
kirisauce committed Jan 8, 2025
1 parent 8c8df37 commit ffd8202
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
34 changes: 7 additions & 27 deletions src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::{
use log::warn;

use crate::{
enumeration::Speed,
transfer::{Direction, EndpointType},
Error,
};
Expand Down Expand Up @@ -201,11 +200,6 @@ impl<'a> DeviceDescriptor<'a> {
assert!(buf[1] == DESCRIPTOR_TYPE_DEVICE);
Self(buf)
}

/// Get speed of this device. The speed is judged by `usb_version` field.
pub fn speed(&self) -> Option<Speed> {
Speed::from_usb_version(self.usb_version())
}
}

descriptor_fields! {
Expand Down Expand Up @@ -257,32 +251,19 @@ descriptor_fields! {
}
}

fn format_usb_version(ver: u16) -> &'static str {
match ver {
0x0100 => "1.0",
0x0110 => "1.1",
0x0200 => "2.0",
0x0210 => "2.0", // This means a USB 3.x device is running in USB 2.0 mode
0x0300 => "3.0",
0x0310 => "3.1",
0x0320 => "3.2",
_ => "unknown",
}
}

impl<'a> Debug for DeviceDescriptor<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DeviceDescriptor")
.field("usb_version", &format_usb_version(self.usb_version()))
.field("class", &format_args!("0x{:02x}", self.class()))
.field("subclass", &format_args!("0x{:02x}", self.subclass()))
.field("protocol", &format_args!("0x{:02x}", self.protocol()))
.field("usb_version", &format_args!("0x{:04X}", self.usb_version()))
.field("class", &format_args!("0x{:02X}", self.class()))
.field("subclass", &format_args!("0x{:02X}", self.subclass()))
.field("protocol", &format_args!("0x{:02X}", self.protocol()))
.field("max_packet_size_0", &self.max_packet_size_0())
.field("vendor_id", &format_args!("0x{:04x}", self.vendor_id()))
.field("product_id", &format_args!("0x{:04x}", self.product_id()))
.field("vendor_id", &format_args!("0x{:04X}", self.vendor_id()))
.field("product_id", &format_args!("0x{:04X}", self.product_id()))
.field(
"device_version",
&format_args!("0x{:04x}", self.device_version()),
&format_args!("0x{:04X}", self.device_version()),
)
.field(
"manufacturer_string_index",
Expand All @@ -294,7 +275,6 @@ impl<'a> Debug for DeviceDescriptor<'a> {
&self.serial_number_string_index(),
)
.field("num_configurations", &self.num_configurations())
.field("speed", &self.speed())
.finish()
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,6 @@ impl Speed {
_ => None,
}
}

#[allow(dead_code)] // not used on all platforms
pub(crate) fn from_usb_version(ver: u16) -> Option<Self> {
match ver {
0x0100 => Some(Speed::Low),
0x0110 => Some(Speed::Full),
0x0200 | 0x0210 => Some(Speed::High),
0x0300 => Some(Speed::Super),
0x0310 | 0x0320 => Some(Speed::SuperPlus),
_ => None,
}
}
}

/// Summary information about a device's interface, available before opening a device.
Expand Down

0 comments on commit ffd8202

Please sign in to comment.