Skip to content

Commit b2784c8

Browse files
committed
tidy and remove unused get_hub_info
1 parent 3753045 commit b2784c8

File tree

6 files changed

+85
-87
lines changed

6 files changed

+85
-87
lines changed

src/platform/linux_usbfs/enumeration.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,19 @@ pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
123123
}
124124

125125
pub fn list_root_hubs() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
126-
Ok(fs::read_dir(SYSFS_PREFIX)?
127-
.filter_map(|entry| {
128-
let path = entry.ok()?.path();
129-
let name = path.file_name()?;
130-
131-
// root hubs are named `usbX` where X is the bus number
132-
if !name.to_string_lossy().starts_with("usb") {
133-
return None;
134-
}
135-
136-
probe_device(SysfsPath(path))
137-
.inspect_err(|e| warn!("{e}; ignoring root hub"))
138-
.ok()
139-
})
140-
)
126+
Ok(fs::read_dir(SYSFS_PREFIX)?.filter_map(|entry| {
127+
let path = entry.ok()?.path();
128+
let name = path.file_name()?;
129+
130+
// root hubs are named `usbX` where X is the bus number
131+
if !name.to_string_lossy().starts_with("usb") {
132+
return None;
133+
}
134+
135+
probe_device(SysfsPath(path))
136+
.inspect_err(|e| warn!("{e}; ignoring root hub"))
137+
.ok()
138+
}))
141139
}
142140

143141
pub fn probe_device(path: SysfsPath) -> Result<DeviceInfo, SysfsError> {

src/platform/linux_usbfs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod usbfs;
55

66
mod enumeration;
77
mod events;
8-
pub use enumeration::{list_devices, SysfsPath, list_root_hubs};
8+
pub use enumeration::{list_devices, list_root_hubs, SysfsPath};
99

1010
mod device;
1111
pub(crate) use device::LinuxDevice as Device;

src/platform/macos_iokit/enumeration.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,20 @@ pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
6969
pub fn list_root_hubs() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
7070
// Chain all the HCI types into one iterator
7171
// A bit of a hack, could maybe probe IOPCIDevice and filter on children with IOClass.starts_with("AppleUSB")
72-
Ok(usb_service_iter(kAppleUSBXHCI)?.filter_map(|h| probe_root_hub(h, AppleUSBController::XHCI))
73-
.chain(usb_service_iter(kAppleUSBEHCI)?.filter_map(|h| probe_root_hub(h, AppleUSBController::EHCI))
74-
.chain(usb_service_iter(kAppleUSBOHCI)?.filter_map(|h| probe_root_hub(h, AppleUSBController::OHCI))
75-
.chain(usb_service_iter(kAppleUSBVHCI)?.filter_map(|h| probe_root_hub(h, AppleUSBController::VHCI))
76-
),
77-
),
78-
))
72+
Ok(usb_service_iter(kAppleUSBXHCI)?
73+
.filter_map(|h| probe_root_hub(h, AppleUSBController::XHCI))
74+
.chain(
75+
usb_service_iter(kAppleUSBEHCI)?
76+
.filter_map(|h| probe_root_hub(h, AppleUSBController::EHCI))
77+
.chain(
78+
usb_service_iter(kAppleUSBOHCI)?
79+
.filter_map(|h| probe_root_hub(h, AppleUSBController::OHCI))
80+
.chain(
81+
usb_service_iter(kAppleUSBVHCI)?
82+
.filter_map(|h| probe_root_hub(h, AppleUSBController::VHCI)),
83+
),
84+
),
85+
))
7986
}
8087

8188
pub(crate) fn service_by_registry_id(registry_id: u64) -> Result<IoService, Error> {
@@ -124,7 +131,10 @@ pub(crate) fn probe_device(device: IoService) -> Option<DeviceInfo> {
124131
})
125132
}
126133

127-
pub(crate) fn probe_root_hub(device: IoService, host_controller: AppleUSBController) -> Option<DeviceInfo> {
134+
pub(crate) fn probe_root_hub(
135+
device: IoService,
136+
host_controller: AppleUSBController,
137+
) -> Option<DeviceInfo> {
128138
let registry_id = get_registry_id(&device)?;
129139
log::debug!("Probing root_hub {registry_id:08x}");
130140

@@ -138,10 +148,9 @@ pub(crate) fn probe_root_hub(device: IoService, host_controller: AppleUSBControl
138148
let vid = u16::from_str_radix(spid, 16).unwrap_or(0x0000);
139149
(vid, pid)
140150
}
141-
_ => (0x0000, 0x0000)
151+
_ => (0x0000, 0x0000),
142152
}
143153
} else {
144-
debug!("Root hub does not have IOPCIPrimaryMatch property");
145154
(0x0000, 0x0000)
146155
};
147156

@@ -170,7 +179,7 @@ pub(crate) fn probe_root_hub(device: IoService, host_controller: AppleUSBControl
170179
manufacturer_string: get_string_property(&device, "IOProviderClass"),
171180
product_string: get_string_property(&device, "IOClass"),
172181
serial_number: get_string_property(&device, "name"), // name is unique system bus name but not always present
173-
interfaces: Vec::new()
182+
interfaces: Vec::new(),
174183
})
175184
}
176185

src/platform/macos_iokit/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ pub(crate) use transfer::TransferData;
66

77
mod enumeration;
88
mod events;
9-
pub use enumeration::list_devices;
10-
pub use enumeration::list_root_hubs;
9+
pub use enumeration::{list_devices, list_root_hubs};
1110

1211
mod device;
1312
pub(crate) use device::MacDevice as Device;

src/platform/windows_winusb/enumeration.rs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use log::debug;
77
use windows_sys::Win32::Devices::{
88
Properties::{
99
DEVPKEY_Device_Address, DEVPKEY_Device_BusReportedDeviceDesc, DEVPKEY_Device_CompatibleIds,
10-
DEVPKEY_Device_HardwareIds, DEVPKEY_Device_InstanceId, DEVPKEY_Device_LocationPaths,
11-
DEVPKEY_Device_Parent, DEVPKEY_Device_Service, DEVPKEY_Device_Manufacturer,
12-
DEVPKEY_Device_DeviceDesc,
10+
DEVPKEY_Device_DeviceDesc, DEVPKEY_Device_HardwareIds, DEVPKEY_Device_InstanceId,
11+
DEVPKEY_Device_LocationPaths, DEVPKEY_Device_Manufacturer, DEVPKEY_Device_Parent,
12+
DEVPKEY_Device_Service,
1313
},
1414
Usb::{GUID_DEVINTERFACE_USB_DEVICE, GUID_DEVINTERFACE_USB_HUB},
1515
};
@@ -154,7 +154,8 @@ pub fn probe_root_hub(devinst: DevInst) -> Option<DeviceInfo> {
154154

155155
let parent_instance_id = devinst.get_property::<OsString>(DEVPKEY_Device_Parent)?;
156156
let port_number = devinst.get_property::<u32>(DEVPKEY_Device_Address)?;
157-
let (vendor_id, product_id, _, _, _) = parse_host_controller_id(parent_instance_id.as_os_str()).unwrap_or_default();
157+
let (vendor_id, product_id, _, _, _) =
158+
parse_host_controller_id(parent_instance_id.as_os_str()).unwrap_or_default();
158159

159160
let product_string = devinst
160161
.get_property::<OsString>(DEVPKEY_Device_DeviceDesc)
@@ -417,26 +418,43 @@ fn test_parse_location_path() {
417418
);
418419
}
419420

420-
421421
/// Parse device version and ID from Root Hub instance ID
422422
fn parse_root_hub_id(s: &OsStr) -> Option<(u16, Option<String>)> {
423423
let s = s.to_str()?;
424424
let s = s.strip_prefix("USB\\ROOT_HUB")?;
425425
let (version, i) = u16::from_str_radix(s.get(0..2).unwrap_or("11"), 10)
426426
.map(|v| ((v / 10) << 8 | v % 10 << 4, 2)) // convert to BCD
427427
.unwrap_or((0x0110, 0)); // default USB 1.1
428-
let id = s.get(i..).map(|v| v.strip_prefix("\\").map(|s| s.to_owned())).flatten();
428+
let id = s
429+
.get(i..)
430+
.map(|v| v.strip_prefix("\\").map(|s| s.to_owned()))
431+
.flatten();
429432
Some((version, id))
430433
}
431434

432435
#[test]
433436
fn test_parse_root_hub_id() {
434437
assert_eq!(parse_root_hub_id(OsStr::new("")), None);
435-
assert_eq!(parse_root_hub_id(OsStr::new("USB\\ROOT_HUB")), Some((0x0110, None)));
436-
assert_eq!(parse_root_hub_id(OsStr::new("USB\\ROOT_HUB\\4&2FB9F669&0")), Some((0x0110, Some("4&2FB9F669&0".to_string()))));
437-
assert_eq!(parse_root_hub_id(OsStr::new("USB\\ROOT_HUB20")), Some((0x0200, None)));
438-
assert_eq!(parse_root_hub_id(OsStr::new("USB\\ROOT_HUB31")), Some((0x0310, None)));
439-
assert_eq!(parse_root_hub_id(OsStr::new("USB\\ROOT_HUB30\\4&2FB9F669&0")), Some((0x0300, Some("4&2FB9F669&0".to_string()))));
438+
assert_eq!(
439+
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB")),
440+
Some((0x0110, None))
441+
);
442+
assert_eq!(
443+
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB\\4&2FB9F669&0")),
444+
Some((0x0110, Some("4&2FB9F669&0".to_string())))
445+
);
446+
assert_eq!(
447+
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB20")),
448+
Some((0x0200, None))
449+
);
450+
assert_eq!(
451+
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB31")),
452+
Some((0x0310, None))
453+
);
454+
assert_eq!(
455+
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB30\\4&2FB9F669&0")),
456+
Some((0x0300, Some("4&2FB9F669&0".to_string())))
457+
);
440458
}
441459

442460
/// Parse VID, PID, revision, subsys and ID from a Host Controller ID: https://learn.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices
@@ -459,7 +477,24 @@ fn parse_host_controller_id(s: &OsStr) -> Option<(u16, u16, u8, u32, Option<Stri
459477
#[test]
460478
fn test_parse_host_controller_id() {
461479
assert_eq!(parse_host_controller_id(OsStr::new("")), None);
462-
assert_eq!(parse_host_controller_id(OsStr::new("PCI\\VEN_8086&DEV_2658&SUBSYS_04001AB8&REV_02\\3&11583659&0&E8")), Some((0x8086, 0x2658, 2, 0x04001AB8, Some("3&11583659&0&E8".to_string()))));
463-
assert_eq!(parse_host_controller_id(OsStr::new("PCI\\VEN_8086&DEV_2658")), None);
464-
assert_eq!(parse_host_controller_id(OsStr::new("PCI\\VEN_8086&DEV_2658&SUBSYS_04001AB8&REV_02")), Some((0x8086, 0x2658, 2, 0x04001AB8, None)));
480+
assert_eq!(
481+
parse_host_controller_id(OsStr::new(
482+
"PCI\\VEN_8086&DEV_2658&SUBSYS_04001AB8&REV_02\\3&11583659&0&E8"
483+
)),
484+
Some((
485+
0x8086,
486+
0x2658,
487+
2,
488+
0x04001AB8,
489+
Some("3&11583659&0&E8".to_string())
490+
))
491+
);
492+
assert_eq!(
493+
parse_host_controller_id(OsStr::new("PCI\\VEN_8086&DEV_2658")),
494+
None
495+
);
496+
assert_eq!(
497+
parse_host_controller_id(OsStr::new("PCI\\VEN_8086&DEV_2658&SUBSYS_04001AB8&REV_02")),
498+
Some((0x8086, 0x2658, 2, 0x04001AB8, None))
499+
);
465500
}

src/platform/windows_winusb/hub.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ use windows_sys::Win32::{
1717
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
1818
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
1919
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, USB_DESCRIPTOR_REQUEST,
20-
IOCTL_USB_GET_HUB_INFORMATION_EX,
2120
USB_DESCRIPTOR_REQUEST_0, USB_DEVICE_DESCRIPTOR, USB_DEVICE_SPEED,
2221
USB_NODE_CONNECTION_INFORMATION_EX, USB_NODE_CONNECTION_INFORMATION_EX_V2,
23-
USB_HUB_TYPE,
24-
USB_HUB_INFORMATION_EX,
2522
},
2623
},
2724
Foundation::{GetLastError, ERROR_GEN_FAILURE, TRUE},
@@ -127,33 +124,6 @@ impl HubHandle {
127124
}
128125
}
129126

130-
pub fn get_hub_info(
131-
&self,
132-
) -> Result<USB_HUB_INFORMATION_EX, Error> {
133-
unsafe {
134-
let mut info: USB_HUB_INFORMATION_EX = mem::zeroed();
135-
let mut bytes_returned: u32 = 0;
136-
let r = DeviceIoControl(
137-
raw_handle(&self.0),
138-
IOCTL_USB_GET_HUB_INFORMATION_EX,
139-
&info as *const _ as *const c_void,
140-
mem::size_of_val(&info) as u32,
141-
&mut info as *mut _ as *mut c_void,
142-
mem::size_of_val(&info) as u32,
143-
&mut bytes_returned,
144-
null_mut(),
145-
);
146-
147-
if r == TRUE {
148-
Ok(info)
149-
} else {
150-
let err = Error::last_os_error();
151-
debug!("Hub info DeviceIoControl failed: {err:?}");
152-
Err(err)
153-
}
154-
}
155-
}
156-
157127
pub fn get_descriptor(
158128
&self,
159129
port_number: u32,
@@ -234,10 +204,6 @@ pub struct HubDeviceInfo {
234204
pub address: u8,
235205
pub active_config: u8,
236206
}
237-
pub struct HubInfo {
238-
pub hub_type: USB_HUB_TYPE,
239-
pub highest_port_number: u16,
240-
}
241207

242208
impl HubPort {
243209
pub fn by_child_devinst(devinst: DevInst) -> Result<HubPort, Error> {
@@ -259,7 +225,7 @@ impl HubPort {
259225
})
260226
}
261227

262-
pub fn by_parent_devinst(devinst: DevInst) -> Result<HubPort, Error> {
228+
pub fn by_hub_devinst(devinst: DevInst) -> Result<HubPort, Error> {
263229
let hub_handle = HubHandle::by_devinst(devinst)
264230
.ok_or_else(|| Error::new(ErrorKind::Other, "failed to open parent hub"))?;
265231
let Some(port_number) = devinst.get_property::<u32>(DEVPKEY_Device_Address) else {
@@ -307,15 +273,6 @@ impl HubPort {
307273
})
308274
}
309275

310-
pub fn get_hub_info(&self) -> Result<HubInfo, Error> {
311-
let info = self.hub_handle.get_hub_info()?;
312-
313-
Ok(HubInfo {
314-
hub_type: info.HubType,
315-
highest_port_number: info.HighestPortNumber,
316-
})
317-
}
318-
319276
pub fn get_descriptor(
320277
&self,
321278
descriptor_type: u8,

0 commit comments

Comments
 (0)