Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement detach_and_claim_interface #35

Merged
merged 7 commits into from
Jan 30, 2024
Merged

Conversation

bugadani
Copy link
Contributor

@bugadani bugadani commented Jan 28, 2024

Closes #34

Interestingly, the PR needs the fallback-to-racy code, otherwise it's not working on my ubuntu which is definitely not 12 years old (I'm getting "this ioctl is not valid for this device" error). Not sure what's going on there, maybe it's an undiscovered bug in libusb which I used as the source to port this code from. Or maybe I just can't copy code very well, as the number of force pushes here indicates :)

@bugadani bugadani force-pushed the ioctl branch 5 times, most recently from 9f640ac to 751218a Compare January 28, 2024 08:21
@bugadani bugadani force-pushed the ioctl branch 15 times, most recently from 189a026 to 33c1924 Compare January 28, 2024 09:47
Comment on lines 106 to 108
if driver.driver[0..6] != *b"usbfs\0" {
return Err(io::Errno::NOENT);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems backwards -- you want to detach a kernel driver (e.g. ftdi_sio), but don't want to detach another instance of nusb / libusb using usbfs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Monkey see this, monkey do !=. Monkey no think source is C and that strcmp returns 0 if equal :)

@kevinmehall
Copy link
Owner

Interestingly, the PR needs the fallback-to-racy code, otherwise it's not working on my ubuntu which is definitely not 12 years old (I'm getting "this ioctl is not valid for this device" error).

It's actually the opposite for me (Ubuntu 22.04, Kernel 6.5.0). The USBDEVFS_DISCONNECT_CLAIM is working, but the USBFS_GETDRIVER and USBFS_IOCTL calls (which also affects reattaching) are failing with ENOTTY ("Inappropriate ioctl for device"). Are you sure that's not what you're seeing?

Those weren't working because the ioctl opcodes were wrong -- the rustix ioctl::ReadOpcode, ioctl::WriteOpcode, ioctl::ReadWriteOpcode corresponds to the _IOR, _IOW, _IOWR etc in https://github.com/torvalds/linux/blob/master/tools/include/uapi/linux/usbdevice_fs.h. That opcode type is arbitrary and doesn't necessarily correspond to the ioctl::patterns type used or direction of actual data flow. I pushed some commits including fixing these.

Can you re-test with RUST_LOG=debug and see if you're getting the USBDEVFS_DISCONNECT_CLAIM ioctl failed, falling back to detach-then-claim debug!() I added in the fallback code path?

@bugadani
Copy link
Contributor Author

bugadani commented Jan 29, 2024

the rustix ioctl::ReadOpcode, ioctl::WriteOpcode, ioctl::ReadWriteOpcode corresponds to the _IOR, _IOW, _IOWR etc

Apparently I see something and write something else :) I promise usbdevice_fs.h was my source, not sure how I got those wrong :)

("Inappropriate ioctl for device")

I should stop reporting from memory.

Can you re-test

All I'm seeing is the following. I probably shouldn't be surprised, mine is also Ubuntu 22.04 with kernel 6.5.0.

DEBUG nusb::platform::linux_usbfs::device: Opening usbfs device /dev/bus/usb/001/006    
DEBUG nusb::platform::linux_usbfs::device: Opened device bus=1 addr=6 with id 0    
DEBUG nusb::platform::linux_usbfs::device: Detached and claimed interface 0 on device id 0

Thanks for touching up my silliness :)

@kevinmehall
Copy link
Owner

Dropped the fallback in the interest of not having legacy code that no one is testing, but if there comes a need for it, it'll be in git history.

@kevinmehall kevinmehall merged commit fc693a9 into kevinmehall:main Jan 30, 2024
4 checks passed
@bugadani bugadani deleted the ioctl branch January 30, 2024 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: alternative for libusb's libusb_detach_kernel_driver et. al.
2 participants