Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F170669569
D54218.id182331.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
9 KB
Referenced Files
None
Subscribers
None
D54218.id182331.diff
View Options
diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c
--- a/lib/libusb/libusb10.c
+++ b/lib/libusb/libusb10.c
@@ -783,6 +783,7 @@
struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
int i;
int j;
+ int ok;
ctx = GET_CONTEXT(ctx);
if (ctx == NULL)
@@ -806,8 +807,9 @@
*/
if (pdesc->idVendor == vendor_id &&
pdesc->idProduct == product_id) {
- libusb_open(devs[j], &pdev);
- break;
+ ok = libusb_open(devs[j], &pdev);
+ if (ok == LIBUSB_SUCCESS)
+ break;
}
}
diff --git a/lib/libusb/libusb20.h b/lib/libusb/libusb20.h
--- a/lib/libusb/libusb20.h
+++ b/lib/libusb/libusb20.h
@@ -173,6 +173,14 @@
LIBUSB20_POWER_RESUME,
};
+/** \ingroup misc
+ * libusb20_dev_open_with_flags values
+ */
+enum libusb20_open_flags {
+ LIBUSB20_OPEN_SHARED = 0,
+ LIBUSB20_OPEN_EXCLUSIVE = 1,
+};
+
struct usb_device_info;
struct libusb20_transfer;
struct libusb20_backend;
@@ -255,6 +263,7 @@
int libusb20_dev_get_stats(struct libusb20_device *pdev, struct libusb20_device_stats *pstat);
int libusb20_dev_kernel_driver_active(struct libusb20_device *pdev, uint8_t iface_index);
int libusb20_dev_open(struct libusb20_device *pdev, uint16_t transfer_max);
+int libusb20_dev_open_with_flags(struct libusb20_device *pdev, uint16_t transfer_max, int flags);
int libusb20_dev_process(struct libusb20_device *pdev);
int libusb20_dev_request_sync(struct libusb20_device *pdev, struct LIBUSB20_CONTROL_SETUP_DECODED *setup, void *data, uint16_t *pactlen, uint32_t timeout, uint8_t flags);
int libusb20_dev_req_string_sync(struct libusb20_device *pdev, uint8_t index, uint16_t langid, void *ptr, uint16_t len);
diff --git a/lib/libusb/libusb20.3 b/lib/libusb/libusb20.3
--- a/lib/libusb/libusb20.3
+++ b/lib/libusb/libusb20.3
@@ -24,7 +24,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd December 27, 2019
+.Dd July 20, 2026
.Dt LIBUSB20 3
.Os
.Sh NAME
@@ -140,6 +140,8 @@
.Ft int
.Fn libusb20_dev_open "struct libusb20_device *pdev" "uint16_t transfer_max"
.Ft int
+.Fn libusb20_dev_open_with_flags "struct libusb20_device *pdev" "uint16_t transfer_max" "int flags"
+.Ft int
.Fn libusb20_dev_process "struct libusb20_device *pdev"
.Ft int
.Fn libusb20_dev_request_sync "struct libusb20_device *pdev" "struct LIBUSB20_CONTROL_SETUP_DECODED *setup" "void *data" "uint16_t *pactlen" "uint32_t timeout" "uint8_t flags"
@@ -679,7 +681,9 @@
.Pp
.
.Fn libusb20_dev_open
-opens an USB device so that setting up USB transfers
+and
+.Fn libusb20_dev_open_with_flags
+open an USB device so that setting up USB transfers
becomes possible.
.
The number of USB transfers can be zero which means only control
diff --git a/lib/libusb/libusb20.c b/lib/libusb/libusb20.c
--- a/lib/libusb/libusb20.c
+++ b/lib/libusb/libusb20.c
@@ -675,7 +675,7 @@
}
int
-libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
+libusb20_dev_open_with_flags(struct libusb20_device *pdev, uint16_t nTransferMax, int flags)
{
struct libusb20_transfer *xfer;
uint32_t size;
@@ -708,7 +708,7 @@
/* set "nTransfer" early */
pdev->nTransfer = nTransferMax;
- error = pdev->beMethods->open_device(pdev, nTransferMax);
+ error = pdev->beMethods->open_device(pdev, nTransferMax, flags);
if (error) {
if (pdev->pTransfer != NULL) {
@@ -724,6 +724,12 @@
return (error);
}
+int
+libusb20_dev_open(struct libusb20_device *pdev, uint16_t nTransferMax)
+{
+ return libusb20_dev_open_with_flags(pdev, nTransferMax, LIBUSB20_OPEN_EXCLUSIVE);
+}
+
int
libusb20_dev_reset(struct libusb20_device *pdev)
{
diff --git a/lib/libusb/libusb20_be_device_foreach.3 b/lib/libusb/libusb20_be_device_foreach.3
--- a/lib/libusb/libusb20_be_device_foreach.3
+++ b/lib/libusb/libusb20_be_device_foreach.3
@@ -31,7 +31,9 @@
.Fa pdev
set to the return value of the previous call yields the next device.
To begin interacting with a USB device, pass the pointer in a call to
-.Xr libusb20_dev_open 3 .
+.Xr libusb20_dev_open 3
+or
+.Xr libusb20_dev_open_with_flags 3 .
.Sh RETURN VALUES
.Nm
returns NULL for end of list.
@@ -53,4 +55,5 @@
.Xr libusb20 3 ,
.Xr libusb20_be_alloc_default 3 ,
.Xr libusb20_be_free 3 ,
-.Xr libusb20_dev_open 3
+.Xr libusb20_dev_open 3,
+.Xr libusb20_dev_open_with_flags 3
diff --git a/lib/libusb/libusb20_dev_open.3 b/lib/libusb/libusb20_dev_open.3
--- a/lib/libusb/libusb20_dev_open.3
+++ b/lib/libusb/libusb20_dev_open.3
@@ -3,7 +3,7 @@
.\"
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
-.Dd December 14, 2025
+.Dd July 20, 2026
.Dt LIBUSB20 3
.Os
.Sh NAME
@@ -13,11 +13,15 @@
.Lb libusb
.In libusb20.h
.Ft int
-.Fn libusb20_dev_open "struct libusb20_device *pdev" "uin16_t transfer_max"
+.Fn libusb20_dev_open "struct libusb20_device *pdev" "uint16_t transfer_max"
+.Ft int
+.Fn libusb20_dev_open_with_flags "struct libusb20_device *pdev" "uin16_t transfer_max" "int flags"
.Sh DESCRIPTION
The
.Nm
-function opens a USB device to retrieve descriptors and initiate transfers.
+and
+.Fn libusb20_dev_open_with_flags
+functions open a USB device to retrieve descriptors and initiate transfers.
.Nm
accepts a pointer as
.Fa pdev
@@ -26,12 +30,32 @@
A zero for
.Fa transfer_max
limits the device to only control transfers.
+.Fn libusb20_dev_open_with_flags
+also accepts a
+.Fa flags
+argument.
+Possible
+.Fa flags
+values are:
+.Bl -tag -width "LIBUSB20_OPEN_EXCLUSIVE" -compact
+.It LIBUSB20_OPEN_SHARED ,
+.It LIBUSB20_OPEN_EXCLUSIVE
+.El
+Calling
+.Nm
+is the same as calling
+.Fn libusb20_dev_open_with_flags
+with
+.Fa flags
+set to LIBUSB20_OPEN_EXCLUSIVE.
Call
.Xr libusb20_dev_close 3
to free resources taken by the open device handle.
.Sh RETURN VALUES
.Nm
-returns one of the following to report success or failure:
+and
+.Fn libusb20_dev_open_with_flags
+return one of the following to report success or failure:
.Pp
.Bl -tag -width "LIBUSB20_ERROR_NO_DEVICE," -compact
.It Er LIBUSB20_SUCCESS ,
@@ -51,18 +75,44 @@
and
.In libusb20.h .
.Sh EXAMPLES
+.Nm
+.Bd -literal
+ #include <libusb20.h>
+ int code;
+ struct libusb20_backend *be = libusb20_be_alloc_default();
+ struct libusb20_device *device = NULL;
+ while ( (device = libusb20_be_device_foreach(be, device)) != NULL ) {
+ code = libusb20_dev_open(device, 4);
+ if (code == LIBUSB20_SUCCESS) {
+ /* do something */
+ libusb20_dev_close(device);
+ }
+ else if (code == LIBUSB20_ERROR_BUSY) {
+ printf("Device already in use.\n");
+ }
+ }
+ libusb20_be_free(be);
+
+.Ed
+.Fn libusb20_dev_open_with_flags
.Bd -literal
#include <libusb20.h>
+ int code;
struct libusb20_backend *be = libusb20_be_alloc_default();
struct libusb20_device *device = NULL;
while ( (device = libusb20_be_device_foreach(be, device)) != NULL ) {
- if (libusb20_dev_open(device, 0) == LIBUSB20_SUCCESS) {
+ code = libusb20_dev_open_with_flags(device, 4, LIBUSB20_OPEN_EXCLUSIVE);
+ if (code == LIBUSB20_SUCCESS) {
/* do something */
libusb20_dev_close(device);
}
+ else if (code == LIBUSB20_ERROR_BUSY) {
+ printf("Device already in use.\n");
+ }
}
libusb20_be_free(be);
.Ed
+.Pp
.Sh SEE ALSO
.Xr libusb20 3 ,
.Xr libusb20_be_device_foreach 3 ,
diff --git a/lib/libusb/libusb20_int.h b/lib/libusb/libusb20_int.h
--- a/lib/libusb/libusb20_int.h
+++ b/lib/libusb/libusb20_int.h
@@ -53,7 +53,7 @@
typedef int (libusb20_dev_get_info_t)(struct libusb20_device *pdev, struct usb_device_info *pinfo);
typedef int (libusb20_dev_get_iface_desc_t)(struct libusb20_device *pdev, uint8_t iface_index, char *buf, uint8_t len);
typedef int (libusb20_init_backend_t)(struct libusb20_backend *pbe);
-typedef int (libusb20_open_device_t)(struct libusb20_device *pdev, uint16_t transfer_count_max);
+typedef int (libusb20_open_device_t)(struct libusb20_device *pdev, uint16_t transfer_count_max, int flags);
typedef void (libusb20_exit_backend_t)(struct libusb20_backend *pbe);
typedef int (libusb20_root_set_template_t)(struct libusb20_backend *pbe, int temp);
typedef int (libusb20_root_get_template_t)(struct libusb20_backend *pbe, int *ptemp);
diff --git a/lib/libusb/libusb20_ugen20.c b/lib/libusb/libusb20_ugen20.c
--- a/lib/libusb/libusb20_ugen20.c
+++ b/lib/libusb/libusb20_ugen20.c
@@ -376,13 +376,14 @@
}
static int
-ugen20_open_device(struct libusb20_device *pdev, uint16_t nMaxTransfer)
+ugen20_open_device(struct libusb20_device *pdev, uint16_t nMaxTransfer, int flags)
{
uint32_t plugtime;
char buf[64];
int f;
int g;
int error;
+ int arg;
snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u",
pdev->bus_number, pdev->device_address);
@@ -408,6 +409,13 @@
/* check that the correct device is still plugged */
if (pdev->session_data.plugtime != plugtime) {
error = LIBUSB20_ERROR_NO_DEVICE;
+ goto done;
+ }
+ arg = ((flags & LIBUSB20_OPEN_EXCLUSIVE) != 0) ? LOCK_EX|LOCK_NB : LOCK_SH|LOCK_NB;
+ if (flock(f, arg) != 0) {
+ close(f);
+ close(g);
+ error = LIBUSB20_ERROR_BUSY;
goto done;
}
/* need to set this before "tr_renew()" */
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Sep 6, 10:39 PM (16 h, 2 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38359384
Default Alt Text
D54218.id182331.diff (9 KB)
Attached To
Mode
D54218: Introduce libusb20_dev_open_with_flags for shared vs. exclusive access.
Attached
Detach File
Event Timeline
Log In to Comment