default to configuration 0 if the bConfigurationValue file is empty (#181)

Co-authored-by: Kevin Mehall <km@kevinmehall.net>
This commit is contained in:
Abdullah Al-Banna 2026-02-17 20:24:08 +03:00 committed by GitHub
parent b8e8938975
commit 94dffc4311
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 7 deletions

View file

@ -29,7 +29,10 @@ use super::{
}; };
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
use super::SysfsPath; use super::{
enumeration::{SysfsError, SysfsErrorKind},
SysfsPath,
};
use crate::{ use crate::{
bitset::EndpointBitSet, bitset::EndpointBitSet,
@ -140,10 +143,20 @@ impl LinuxDevice {
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
let active_config: u8 = if let Some(sysfs) = sysfs.as_ref() { let active_config: u8 = if let Some(sysfs) = sysfs.as_ref() {
sysfs.read_attr("bConfigurationValue").map_err(|e| { match sysfs.read_attr("bConfigurationValue") {
Ok(v) => v,
// Linux returns an empty string when the device is unconfigured.
// We'll assume all parse errors are the empty string.
Err(SysfsError(_, SysfsErrorKind::Parse(_))) => 0,
Err(e) => {
warn!("failed to read sysfs bConfigurationValue: {e}"); warn!("failed to read sysfs bConfigurationValue: {e}");
Error::new(ErrorKind::Other, "failed to read sysfs bConfigurationValue") return Err(Error::new(
})? ErrorKind::Other,
"failed to read sysfs bConfigurationValue",
));
}
}
} else { } else {
guess_active_configuration(&fd, &descriptors) guess_active_configuration(&fd, &descriptors)
}; };
@ -325,6 +338,10 @@ impl LinuxDevice {
self.active_config.store(v, Ordering::SeqCst); self.active_config.store(v, Ordering::SeqCst);
return v; return v;
} }
Err(SysfsError(_, SysfsErrorKind::Parse(_))) => {
self.active_config.store(0, Ordering::SeqCst);
return 0;
}
Err(e) => { Err(e) => {
error!("Failed to read sysfs bConfigurationValue: {e}, using cached value"); error!("Failed to read sysfs bConfigurationValue: {e}, using cached value");
} }

View file

@ -15,10 +15,10 @@ use crate::{BusInfo, DeviceInfo, Error, Speed, UsbControllerType};
pub struct SysfsPath(pub(crate) PathBuf); pub struct SysfsPath(pub(crate) PathBuf);
#[derive(Debug)] #[derive(Debug)]
pub struct SysfsError(PathBuf, SysfsErrorKind); pub struct SysfsError(pub(crate) PathBuf, pub(crate) SysfsErrorKind);
#[derive(Debug)] #[derive(Debug)]
enum SysfsErrorKind { pub(crate) enum SysfsErrorKind {
Io(io::Error), Io(io::Error),
Parse(String), Parse(String),
} }