Skip to content

Commit 94dffc4

Browse files
default to configuration 0 if the bConfigurationValue file is empty (#181)
Co-authored-by: Kevin Mehall <km@kevinmehall.net>
1 parent b8e8938 commit 94dffc4

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/platform/linux_usbfs/device.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ use super::{
2929
};
3030

3131
#[cfg(not(target_os = "android"))]
32-
use super::SysfsPath;
32+
use super::{
33+
enumeration::{SysfsError, SysfsErrorKind},
34+
SysfsPath,
35+
};
3336

3437
use crate::{
3538
bitset::EndpointBitSet,
@@ -140,10 +143,20 @@ impl LinuxDevice {
140143

141144
#[cfg(not(target_os = "android"))]
142145
let active_config: u8 = if let Some(sysfs) = sysfs.as_ref() {
143-
sysfs.read_attr("bConfigurationValue").map_err(|e| {
144-
warn!("failed to read sysfs bConfigurationValue: {e}");
145-
Error::new(ErrorKind::Other, "failed to read sysfs bConfigurationValue")
146-
})?
146+
match sysfs.read_attr("bConfigurationValue") {
147+
Ok(v) => v,
148+
// Linux returns an empty string when the device is unconfigured.
149+
// We'll assume all parse errors are the empty string.
150+
Err(SysfsError(_, SysfsErrorKind::Parse(_))) => 0,
151+
152+
Err(e) => {
153+
warn!("failed to read sysfs bConfigurationValue: {e}");
154+
return Err(Error::new(
155+
ErrorKind::Other,
156+
"failed to read sysfs bConfigurationValue",
157+
));
158+
}
159+
}
147160
} else {
148161
guess_active_configuration(&fd, &descriptors)
149162
};
@@ -325,6 +338,10 @@ impl LinuxDevice {
325338
self.active_config.store(v, Ordering::SeqCst);
326339
return v;
327340
}
341+
Err(SysfsError(_, SysfsErrorKind::Parse(_))) => {
342+
self.active_config.store(0, Ordering::SeqCst);
343+
return 0;
344+
}
328345
Err(e) => {
329346
error!("Failed to read sysfs bConfigurationValue: {e}, using cached value");
330347
}

src/platform/linux_usbfs/enumeration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use crate::{BusInfo, DeviceInfo, Error, Speed, UsbControllerType};
1515
pub struct SysfsPath(pub(crate) PathBuf);
1616

1717
#[derive(Debug)]
18-
pub struct SysfsError(PathBuf, SysfsErrorKind);
18+
pub struct SysfsError(pub(crate) PathBuf, pub(crate) SysfsErrorKind);
1919

2020
#[derive(Debug)]
21-
enum SysfsErrorKind {
21+
pub(crate) enum SysfsErrorKind {
2222
Io(io::Error),
2323
Parse(String),
2424
}

0 commit comments

Comments
 (0)