Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Comment on lines 102 to +105
self.backend.detach_kernel_driver(interface)?;
let _ = interface;

Expand All @@ -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"))]
Comment on lines 115 to +118
self.backend.attach_kernel_driver(interface)?;
let _ = interface;

Expand Down
45 changes: 15 additions & 30 deletions src/enumeration.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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"))]
Expand Down Expand Up @@ -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(
Expand All @@ -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<u8>,

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,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -234,7 +225,6 @@ impl DeviceInfo {
}

/// Connection speed
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub fn speed(&self) -> Option<Speed> {
self.speed
}
Expand Down Expand Up @@ -294,15 +284,13 @@ 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));

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),
Expand All @@ -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);
}
Expand Down Expand Up @@ -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"))]
Comment on lines 463 to +467
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")]
Expand Down Expand Up @@ -526,24 +513,23 @@ pub struct BusInfo {
pub(crate) controller_type: Option<UsbControllerType>,
}

#[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"))]
Comment on lines 517 to +518
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`.
Comment on lines 523 to 525
#[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"))]
Comment on lines 531 to +532
pub fn root_hub(&self) -> &DeviceInfo {
&self.root_hub
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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;
Expand All @@ -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<Output = Result<impl Iterator<Item = DeviceInfo>, Error>>
{
platform::list_devices()
Expand All @@ -240,7 +237,6 @@ pub fn list_devices() -> impl MaybeFuture<Output = Result<impl Iterator<Item = D
/// })
/// .collect();
/// ```
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub fn list_buses() -> impl MaybeFuture<Output = Result<impl Iterator<Item = BusInfo>, Error>> {
platform::list_buses()
}
Expand Down Expand Up @@ -280,7 +276,6 @@ pub fn list_buses() -> impl MaybeFuture<Output = Result<impl Iterator<Item = Bus
/// when the `Connected` event is emitted. If you are immediately opening the device
/// and claiming an interface when receiving a `Connected` event,
/// you should retry after a short delay if opening or claiming fails.
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
pub fn watch_devices() -> Result<hotplug::HotplugWatch, Error> {
Ok(hotplug::HotplugWatch(platform::HotplugWatch::new()?))
}
26 changes: 2 additions & 24 deletions src/platform/linux_usbfs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use super::{
TransferData,
};

#[cfg(not(target_os = "android"))]
use super::{
enumeration::{SysfsError, SysfsErrorKind},
SysfsPath,
Expand Down Expand Up @@ -69,7 +68,6 @@ pub(crate) struct LinuxDevice {
/// Read from the fd, consists of device descriptor followed by configuration descriptors
descriptors: Vec<u8>,

#[cfg(not(target_os = "android"))]
sysfs: Option<SysfsPath>,

active_config: AtomicU8,
Expand All @@ -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<Output = Result<Arc<LinuxDevice>, Error>> {
Expand Down Expand Up @@ -109,29 +106,18 @@ impl LinuxDevice {
})
}

#[cfg(target_os = "android")]
pub(crate) fn from_device_info(
_d: &DeviceInfo,
) -> impl MaybeFuture<Output = Result<Arc<LinuxDevice>, Error>> {
Blocking::new(move || unimplemented!())
}

pub(crate) fn from_fd(
fd: OwnedFd,
) -> impl MaybeFuture<Output = Result<Arc<LinuxDevice>, 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<SysfsPath>,
sysfs: Option<SysfsPath>,
) -> Result<Arc<LinuxDevice>, Error> {
let descriptors = read_all_from_fd(&fd).map_err(|e| {
Error::new_io(ErrorKind::Other, "failed to read descriptors", e).log_error()
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -176,7 +158,6 @@ impl LinuxDevice {
fd,
events_id,
descriptors,
#[cfg(not(target_os = "android"))]
sysfs,
active_config: AtomicU8::new(active_config),
timerfd,
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -451,7 +431,6 @@ impl LinuxDevice {
})
}

#[cfg(target_os = "linux")]
pub(crate) fn detach_kernel_driver(
self: &Arc<Self>,
interface_number: u8,
Expand All @@ -464,7 +443,6 @@ impl LinuxDevice {
})
}

#[cfg(target_os = "linux")]
pub(crate) fn attach_kernel_driver(
self: &Arc<Self>,
interface_number: u8,
Expand Down
4 changes: 0 additions & 4 deletions src/platform/linux_usbfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines 13 to 15

mod events;
Expand Down