Add support for getting device information from opened device on Linux#102
Add support for getting device information from opened device on Linux#102kevinmehall merged 11 commits intokevinmehall:mainfrom
Conversation
kevinmehall
left a comment
There was a problem hiding this comment.
Device descriptor is a good addition. It would be nice to have it on all platforms, but that can be added later if you don't have easy access to macOS and Windows.
I'm not sure the API to get DeviceInfo is a good idea. Pretty much all the useful information is exposed in the device descriptor here.
For Android, using Java APIs to enumerate devices to populate DeviceInfo before opening a device is probably a better idea: #86 (comment)
src/enumeration.rs
Outdated
|
|
||
| #[allow(dead_code)] // not used on all platforms | ||
| pub(crate) fn from_usb_version(ver: u16) -> Option<Self> { | ||
| match ver { | ||
| 0x0100 => Some(Speed::Low), | ||
| 0x0110 => Some(Speed::Full), | ||
| 0x0200 | 0x0210 => Some(Speed::High), | ||
| 0x0300 => Some(Speed::Super), | ||
| 0x0310 | 0x0320 => Some(Speed::SuperPlus), | ||
| _ => None, | ||
| } | ||
| } |
There was a problem hiding this comment.
You can't assume the speed from the USB version. Plenty of Full Speed devices devices specify 0x0200, or 0x0210 if they support BOS. I'd rather return None than take a wild guess like this.
There was a problem hiding this comment.
Thank your for the correction! I will change them later.
3a17c2b to
28cd2b7
Compare
|
@kevinmehall Besides, I'm trying implementing a new feature that could get device speed from an opened device through an ioctl enum usb_device_speed {
USB_SPEED_UNKNOWN = 0, /* enumerating */
USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */
USB_SPEED_HIGH, /* usb 2.0 */
USB_SPEED_WIRELESS, /* wireless (usb 2.5) */
USB_SPEED_SUPER, /* usb 3.0 */
USB_SPEED_SUPER_PLUS, /* usb 3.1 */
}; |
|
That seems like a better approach. It should probably return Option to cover the case of newer speeds introduced that we don't know about yet, or failure of the ioctl, and you can use that to leave |
This is done through an ioctl `USBDEVFS_GET_SPEED`. Judging from the kernel souce code, it just returns a cached value, and should not make any request to the device.
Unlike the Configuration descriptor, it does not contain nested descriptors. It's also made infallible, because the kernel wouldn't enumerate the device without it.
We'll log if the ioctl failed, but there's probably not a use case to determine why it failed to justify Result<Option<>>. It'll be infallible (besides unknown values) on other platforms.
|
Thanks! Made some API tweaks and added Windows and macOS implementations of these APIs. |
Hi! I'm doing some works on Termux. Termux provides a command
termux-usbfor accessing USB device. But it give me only a file descriptor, and I need to get some basic information about the device. So I did some works to get information about the device itself from opened device.New Public APIs
Device::get_device_descriptor()fnDevice::get_device_info()descriptors::DeviceDescriptorI'm sorry for the mistakes I made.
Changelog
20250108-1
device::Device::get_device_infodescriptors::DeviceDescriptor::speeddescriptors::DeviceDescriptor::format_usb_versionplatform::linux_usbfs::device::LinuxDevice::get_busnum_and_devnumenumeration::Speed::from_usb_versionplatform::linux_usbfs::device::LinuxDevice::get_descriptorstodescriptorsDebugimplementation fordescriptors::DeviceDescriptorto keep the same output format20250114-1
platform::linux_usbfs::usbfs::get_speedplatform::linux_usbfs::device::LinuxDevice::get_speeddevice::Device::get_speedNote: The type
descriptors::DeviceDescriptoris defined as USB standard device descriptor.