Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 10 Dec 2025 04:57:53 +0000
From:      bugzilla-noreply@freebsd.org
To:        usb@FreeBSD.org
Subject:   [Bug 291506] libusb_open advisory lock (libusb compatiblity)
Message-ID:  <bug-291506-19105-BkULBbXL6v@https.bugs.freebsd.org/bugzilla/>
In-Reply-To: <bug-291506-19105@https.bugs.freebsd.org/bugzilla/>
References:  <bug-291506-19105@https.bugs.freebsd.org/bugzilla/>

next in thread | previous in thread | raw e-mail | index | archive | help

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=291506

--- Comment #2 from unitrunker <unitrunker@unitrunker.net> ---
== DRAFT (feedback welcome) ==

Requirements

libusb_open

assume: libusb_device parameter comes from libusb_get_device_list()

succeeds on opening a valid, attached device not locked by any processes
(including self).
fails on opening a device locked by any process (including self).

libusb_open_with_vid_pid

for "N" valid, matching devices attached to host.
succeeds on opening all N devices with matching vid/pid and not locked by any
process (including self).
fails when all valid, matching devices are taken by any process (including
self).

About "including self" - be sure to cover in-process testing where the same
libusb_context is shared and also where a new 
libusb_context is created prior to each device open attempt.

Tools

Two USB devices with same VID/PID (preferrably with different serial numbers).

usbgrab: a small test tool to take ownership of the first device with matching
vid and pid on the host. Uses libusb_open_with_vid_pid.

Synopis: usbgrab <vid> <pid> [-p] [-a]

vid - four digit hexadecimal vendor id
pid - four digit hexadecimal product id.
-p pause, displays "Press enter..." and waits for input. Keeps the device
"owned" for an indefinite amount of time.
-a all - grabs all matching devices and displays how many were found. If -a is
absent, the program grabs the first matching device.

-p and -a may be combined.

Test Plan

A. Single device

1. Attach one device with known VID/PID.
2. run "usbgrab <vid> <pid> -p &" utility to take ownership of the device. 
3. Verify the tool displays "Found: 1 Press enter ..."
4. run "usbgrab <vid> <pid>" in the foreground.
5. Verify an error is displayed.
6. run "fg" to place the earlier usbgrab process into the foreground.
7. press enter to exit the process.

Verify no usbgrab background processes remain.

B. Two devices, sequential

1. Attach two devices with same known VID/PID.
2. run "usbgrab <vid> <pid> -p &" utility to take ownership of the device.
3. Verify the tool displays "Found: 1 Press enter ..."
4. run "usbgrab <vid> <pid>" in the foreground.
5. Verify NO error is displayed.
6. run "fg" to place the earlier usbgrab process into the foreground.
7. press enter to exit the process.

Verify no usbgrab background processes remain.

C. Two devices, all or none

1. Attach two devices with same known VID/PID.
2. run "usbgrab <vid> <pid> -p -a &" utility to take ownership of all matching
devices.
3. Verify the tool displays "Found: 2 Press enter ..."
4. run "usbgrab <vid> <pid>" in the foreground.
5. Verify an error is displayed.
6. run "fg" to place the earlier usbgrab process into the foreground.
7. press enter to exit the process.

Verify no usbgrab background processes remain.

D. Two devices, alternating

1. Attach two devices with same known VID/PID.
2. run "usbgrab <vid> <pid> -p &" utility to take ownership of the device and
pause in the background.
3. Verify the tool displays "Found: 1 Press enter ..."
4. Make note of the background process pid.
5. run "usbgrab <vid> <pid> -p &" in the background to take ownership of the
second device.
6. Verify "Found 1 Press enter ..." is displayed.
7. run "usbgrab <vid> <pid>" in the foreground.
8. Verify an error is displayed.
9. run "fg <pid>" to place the earlier usbgrab process into the foreground.
10. press enter to exit the process.
11. run "usbgrab <vid> <pid>"
12. Verify "Found 1" is displayed
13. run "fg" to place earlier usbgrab process into foreground.
14. press enter to exit the process.

Verify no usbgrab background processes remain.

Notes

I tested this by running buildworld on a git clone of
https://github.com/freebsd/freebsd-src.git

Instead of installing the results, I renamed three files in /usr/lib (libusb.a,
libusb.so, libusb.so.3) and copied the three newly built files into place. 
There's probably a better way but this works. Those scope of this change is so
small - I can quickly deploy and roll-back the change to compare before vs.
after behavior.

Concerns

For a USB device shared between kernel and userland, what impact might an
advisory lock have on the libusb_detach_kernel_driver and
libusb_attach_kernel_driver functions? The devices I use to test do not have
kernel drivers.

-- 
You are receiving this mail because:
You are the assignee for the bug.


Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-291506-19105-BkULBbXL6v>