From d2e5823ad9ad566d651a211fe407d86df594f8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20Steinfinsb=C3=B8?= Date: Tue, 26 May 2026 11:01:21 +0200 Subject: [PATCH] Include android cfg preprocessor to all linux available paths --- examples/control.rs | 2 +- src/device.rs | 4 +-- src/enumeration.rs | 45 ++++++++++-------------------- src/lib.rs | 5 ---- src/platform/linux_usbfs/device.rs | 26 ++--------------- src/platform/linux_usbfs/mod.rs | 4 --- 6 files changed, 20 insertions(+), 66 deletions(-) diff --git a/examples/control.rs b/examples/control.rs index ced08fd0..ef7a26e0 100644 --- a/examples/control.rs +++ b/examples/control.rs @@ -18,7 +18,7 @@ fn main() { let device = di.open().wait().unwrap(); // Linux can make control transfers without claiming an interface - #[cfg(any(target_os = "linux", target_os = "macos"))] + #[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))] { let result = device .control_out( diff --git a/src/device.rs b/src/device.rs index f68907b3..08193919 100644 --- a/src/device.rs +++ b/src/device.rs @@ -102,7 +102,7 @@ impl Device { /// This function can only detach kernel drivers on Linux. Calling on other platforms has /// no effect. pub fn detach_kernel_driver(&self, interface: u8) -> Result<(), Error> { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "android"))] self.backend.detach_kernel_driver(interface)?; let _ = interface; @@ -115,7 +115,7 @@ impl Device { /// This function can only attach kernel drivers on Linux. Calling on other platforms has /// no effect. pub fn attach_kernel_driver(&self, interface: u8) -> Result<(), Error> { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "android"))] self.backend.attach_kernel_driver(interface)?; let _ = interface; diff --git a/src/enumeration.rs b/src/enumeration.rs index fcf1790c..494bf3dc 100644 --- a/src/enumeration.rs +++ b/src/enumeration.rs @@ -1,7 +1,7 @@ #[cfg(target_os = "windows")] use std::ffi::{OsStr, OsString}; -#[cfg(any(target_os = "linux"))] +#[cfg(any(target_os = "linux", target_os = "android"))] use crate::platform::SysfsPath; use crate::{Device, Error, MaybeFuture}; @@ -22,7 +22,7 @@ pub struct DeviceId(pub(crate) crate::platform::DeviceId); /// * macOS: `registry_id`, `location_id` #[derive(Clone)] pub struct DeviceInfo { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) path: SysfsPath, #[cfg(any(target_os = "linux", target_os = "android"))] @@ -52,7 +52,6 @@ pub struct DeviceInfo { #[cfg(target_os = "macos")] pub(crate) location_id: u32, - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub(crate) bus_id: String, #[cfg(any( @@ -63,13 +62,11 @@ pub struct DeviceInfo { ))] pub(crate) device_address: u8, - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub(crate) port_chain: Vec, pub(crate) vendor_id: u16, pub(crate) product_id: u16, - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub(crate) device_version: u16, pub(crate) usb_version: u16, @@ -108,16 +105,14 @@ impl DeviceInfo { } } - /// *(Linux-only)* Sysfs path for the device. - #[cfg(target_os = "linux")] + /// Sysfs path for the device. + #[cfg(any(target_os = "linux", target_os = "android"))] pub fn sysfs_path(&self) -> &std::path::Path { &self.path.0 } - /// *(Linux-only)* Bus number. - /// - /// On Linux, the `bus_id` is an integer and this provides the value as `u8`. - #[cfg(any(target_os = "linux"))] + /// Bus number. + #[cfg(any(target_os = "linux", target_os = "android"))] pub fn busnum(&self) -> u8 { self.busnum } @@ -153,7 +148,6 @@ impl DeviceInfo { /// /// Since USB SuperSpeed is a separate topology from USB 2.0 speeds, a /// physical port may be identified differently depending on speed. - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub fn port_chain(&self) -> &[u8] { &self.port_chain } @@ -177,13 +171,11 @@ impl DeviceInfo { } /// Identifier for the bus / host controller where the device is connected. - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub fn bus_id(&self) -> &str { &self.bus_id } /// Number identifying the device within the bus. - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub fn device_address(&self) -> u8 { self.device_address } @@ -202,7 +194,6 @@ impl DeviceInfo { /// The device version, normally encoded as BCD, from the `bcdDevice` device descriptor field. #[doc(alias = "bcdDevice")] - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub fn device_version(&self) -> u16 { self.device_version } @@ -234,7 +225,6 @@ impl DeviceInfo { } /// Connection speed - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub fn speed(&self) -> Option { self.speed } @@ -294,7 +284,6 @@ impl std::fmt::Debug for DeviceInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut s = f.debug_struct("DeviceInfo"); - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] s.field("bus_id", &self.bus_id) .field("device_address", &self.device_address) .field("port_chain", &format_args!("{:?}", self.port_chain)); @@ -302,7 +291,6 @@ impl std::fmt::Debug for DeviceInfo { s.field("vendor_id", &format_args!("0x{:04X}", self.vendor_id)) .field("product_id", &format_args!("0x{:04X}", self.product_id)); - #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] s.field( "device_version", &format_args!("0x{:04X}", self.device_version), @@ -317,7 +305,7 @@ impl std::fmt::Debug for DeviceInfo { .field("product_string", &self.product_string) .field("serial_number", &self.serial_number); - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "linux", target_os = "android"))] { s.field("sysfs_path", &self.path); } @@ -475,16 +463,15 @@ impl UsbControllerType { /// * Linux: `path`, `busnum`, `root_hub` /// * Windows: `instance_id`, `parent_instance_id`, `location_paths`, `devinst`, `root_hub_description` /// * macOS: `registry_id`, `location_id`, `name`, `provider_class_name`, `class_name` -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub struct BusInfo { - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) path: SysfsPath, /// The phony root hub device - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) root_hub: DeviceInfo, - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] pub(crate) busnum: u8, #[cfg(target_os = "windows")] @@ -526,10 +513,9 @@ pub struct BusInfo { pub(crate) controller_type: Option, } -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] impl BusInfo { /// *(Linux-only)* Sysfs path for the bus. - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] pub fn sysfs_path(&self) -> &std::path::Path { &self.path.0 } @@ -537,13 +523,13 @@ impl BusInfo { /// *(Linux-only)* Bus number. /// /// On Linux, the `bus_id` is an integer and this provides the value as `u8`. - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] pub fn busnum(&self) -> u8 { self.busnum } /// *(Linux-only)* The root hub [`DeviceInfo`] representing the bus. - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] pub fn root_hub(&self) -> &DeviceInfo { &self.root_hub } @@ -627,7 +613,7 @@ impl BusInfo { /// * macOS: The [IONameMatched](https://developer.apple.com/documentation/bundleresources/information_property_list/ionamematch) key of the IOService entry. /// * Windows: Description field of the root hub device. How the bus will appear in Device Manager. pub fn system_name(&self) -> Option<&str> { - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] { self.root_hub.product_string() } @@ -644,12 +630,11 @@ impl BusInfo { } } -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] impl std::fmt::Debug for BusInfo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut s = f.debug_struct("BusInfo"); - #[cfg(any(target_os = "linux"))] + #[cfg(any(target_os = "linux", target_os = "android"))] { s.field("sysfs_path", &self.path); s.field("busnum", &self.busnum); diff --git a/src/lib.rs b/src/lib.rs index 9905508a..54fb2be2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,6 @@ mod platform; pub mod descriptors; mod enumeration; -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub use enumeration::BusInfo; pub use enumeration::{DeviceId, DeviceInfo, InterfaceInfo, Speed, UsbControllerType}; @@ -192,7 +191,6 @@ pub use device::{Device, Endpoint, Interface}; pub mod transfer; -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub mod hotplug; mod maybe_future; @@ -215,7 +213,6 @@ pub use error::{ActiveConfigurationError, Error, ErrorKind, GetDescriptorError}; /// .find(|dev| dev.vendor_id() == 0xAAAA && dev.product_id() == 0xBBBB) /// .expect("device not connected"); /// ``` -#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] pub fn list_devices() -> impl MaybeFuture, Error>> { platform::list_devices() @@ -240,7 +237,6 @@ pub fn list_devices() -> impl MaybeFuture impl MaybeFuture, Error>> { platform::list_buses() } @@ -280,7 +276,6 @@ pub fn list_buses() -> impl MaybeFuture Result { Ok(hotplug::HotplugWatch(platform::HotplugWatch::new()?)) } diff --git a/src/platform/linux_usbfs/device.rs b/src/platform/linux_usbfs/device.rs index ff823557..f2e22679 100644 --- a/src/platform/linux_usbfs/device.rs +++ b/src/platform/linux_usbfs/device.rs @@ -28,7 +28,6 @@ use super::{ TransferData, }; -#[cfg(not(target_os = "android"))] use super::{ enumeration::{SysfsError, SysfsErrorKind}, SysfsPath, @@ -69,7 +68,6 @@ pub(crate) struct LinuxDevice { /// Read from the fd, consists of device descriptor followed by configuration descriptors descriptors: Vec, - #[cfg(not(target_os = "android"))] sysfs: Option, active_config: AtomicU8, @@ -79,7 +77,6 @@ pub(crate) struct LinuxDevice { } impl LinuxDevice { - #[cfg(not(target_os = "android"))] pub(crate) fn from_device_info( d: &DeviceInfo, ) -> impl MaybeFuture, Error>> { @@ -109,29 +106,18 @@ impl LinuxDevice { }) } - #[cfg(target_os = "android")] - pub(crate) fn from_device_info( - _d: &DeviceInfo, - ) -> impl MaybeFuture, Error>> { - Blocking::new(move || unimplemented!()) - } - pub(crate) fn from_fd( fd: OwnedFd, ) -> impl MaybeFuture, Error>> { Blocking::new(move || { debug!("Wrapping fd {} as usbfs device", fd.as_raw_fd()); - Self::create_inner( - fd, - #[cfg(not(target_os = "android"))] - None, - ) + Self::create_inner(fd, None) }) } pub(crate) fn create_inner( fd: OwnedFd, - #[cfg(not(target_os = "android"))] sysfs: Option, + sysfs: Option, ) -> Result, Error> { let descriptors = read_all_from_fd(&fd).map_err(|e| { Error::new_io(ErrorKind::Other, "failed to read descriptors", e).log_error() @@ -141,7 +127,6 @@ impl LinuxDevice { return Err(Error::new(ErrorKind::Other, "invalid device descriptor")); }; - #[cfg(not(target_os = "android"))] let active_config: u8 = if let Some(sysfs) = sysfs.as_ref() { match sysfs.read_attr("bConfigurationValue") { Ok(v) => v, @@ -161,9 +146,6 @@ impl LinuxDevice { guess_active_configuration(&fd, &descriptors) }; - #[cfg(target_os = "android")] - let active_config = guess_active_configuration(&fd, &descriptors); - let timerfd = timerfd_create( rustix::time::TimerfdClockId::Monotonic, TimerfdFlags::CLOEXEC | TimerfdFlags::NONBLOCK, @@ -176,7 +158,6 @@ impl LinuxDevice { fd, events_id, descriptors, - #[cfg(not(target_os = "android"))] sysfs, active_config: AtomicU8::new(active_config), timerfd, @@ -331,7 +312,6 @@ impl LinuxDevice { } pub(crate) fn active_configuration_value(&self) -> u8 { - #[cfg(not(target_os = "android"))] if let Some(sysfs) = self.sysfs.as_ref() { match sysfs.read_attr("bConfigurationValue") { Ok(v) => { @@ -451,7 +431,6 @@ impl LinuxDevice { }) } - #[cfg(target_os = "linux")] pub(crate) fn detach_kernel_driver( self: &Arc, interface_number: u8, @@ -464,7 +443,6 @@ impl LinuxDevice { }) } - #[cfg(target_os = "linux")] pub(crate) fn attach_kernel_driver( self: &Arc, interface_number: u8, diff --git a/src/platform/linux_usbfs/mod.rs b/src/platform/linux_usbfs/mod.rs index 2e83c940..04fda98b 100644 --- a/src/platform/linux_usbfs/mod.rs +++ b/src/platform/linux_usbfs/mod.rs @@ -6,16 +6,12 @@ use rustix::io::Errno; pub(crate) use transfer::TransferData; mod usbfs; -#[cfg(not(target_os = "android"))] mod enumeration; -#[cfg(not(target_os = "android"))] pub use enumeration::{list_buses, list_devices, SysfsPath}; -#[cfg(not(target_os = "android"))] mod hotplug; -#[cfg(not(target_os = "android"))] pub(crate) use hotplug::LinuxHotplugWatch as HotplugWatch; mod events;