Commit Graph

202 Commits

Author SHA1 Message Date
ShengYi Hung 20056f0e5a libusb: implement libusb_get_platform_descriptor
This adds a function introduced in libusb 1.0.27 to parse
platform-specific USB descriptors, enabling access to vendor- or OS-specific information.

Approved by:    lwhsu (mentor)
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51242
2025-08-06 10:05:40 -04:00
ShengYi Hung 315dec5ce6 libusb: Add missing misc class in LIBUSB_CLASS
Reviewed by:    emaste
Approved by:    markj (mentor), lwhsu (mentor)
Sponsored by:   The FreeBSD Foundation

Differential Revision: https://reviews.freebsd.org/D51738
2025-08-06 10:05:40 -04:00
ShengYi Hung 8720656885 libusb: implement libusb_wrap_sys_device
The libusb_wrap_sys_device function can wrap an opened fd from the
system into a libusb handler. However, in FreeBSD's libusb implementation, a
USB device contains two fds: one for control transfers and another for
normal (bulk, interrupt, isochronous) transfers.

This design makes it impossible for FreeBSD to implement this function
without exposing a different structure in libusb.h to provide two fds.
Therefore, we return LIBUSB_ERROR_NOT_SUPPORTED to maintain API
compatibility.

Approved by:    makrj (mentor), lwhsu (mentor)
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51739
2025-08-06 10:05:40 -04:00
ShengYi Hung b0d5c1cfda libusb: implement libusb_pollfds_handle_timeouts
This function tells applications who maintain the pollfds themselves if
they should handle the timeout for each xfer themselves. In FreeBSD, the
timeout for each xfer is handled by kernel and doesn't need a special timer to
do so. Therefore, we return 1 to indicate that it is handled by libusb
internally.

Approved by:    lwhsu (mentor), markj (mentor)
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51742
2025-08-06 10:05:40 -04:00
ShengYi Hung c43a850451 libusb: add libusb_supported_speed enum
Approved by:    lwhsu (mentor), markj (mentor)
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51743
2025-08-06 10:05:40 -04:00
ShengYi Hung 8ddbb5e800 libusb: add libusb_usb_2_0_extension_attributes
Approved by:    lwhsu (mentor), markj (mentor)
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51745
2025-08-06 10:05:40 -04:00
ShengYi Hung 2525f9f84b libusb: add missing function signature to header
Approved by:    markj (mentor), lwhsu (mentor)
Fixes: 3eda349340 ("libusb: implement libusb_fill_bulk_stream_transfer")
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51754
2025-08-06 10:05:40 -04:00
ShengYi Hung 3eda349340 libusb: implement libusb_fill_bulk_stream_transfer
Bulk streams are essentially bulk transfers with a stream ID. All
requirements from both libusb20 and the kernel are already handled; we
just need to set the stream ID.

Approved by:    markj (mentor), lwhsu(mentor)
Differential Revision: https://reviews.freebsd.org/D51226
2025-07-21 02:16:41 -04:00
ShengYi Hung 776e26f568 libusb: implement libusb_dev_mem_{alloc,free}
libusb provides an API to create DMA buffers for USB packets from the
kernel. However, this feature is only available on Linux. On unsupported
platforms, the allocation function returns NULL, and the free function
returns 'not supported'.

Approved by:    markj (mentor), lwhsu(mentor)
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51222
2025-07-21 02:16:07 -04:00
ShengYi Hung 9ca855653f libusb: implement libusb_hotplug_get_user_data
libusb provides a function to get the callback userdata for a given
callback since this structure is opaque to libusb user.

Approved by:    markj (mentor), lwhsu (mentor)
Sponsored by:   The FreeBSD Foundation
MFC after:      2 weeks
Differential Revision: https://reviews.freebsd.org/D51223
2025-07-21 02:15:58 -04:00
ShengYi Hung 1c675fb365 libusb: implement libusb_free_pollfds
libusb provides an API that frees the storage for pollfds regardless of
their contents. It does not check whether the file descriptors inside
are already closed.

Approved by:    markj (mentor), lwhsu(mentor)
MFC after:      2 weeks
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51221
2025-07-21 02:15:15 -04:00
ShengYi Hung 99d5cf25d7 libusb: implement libusb_setlocale to support different locale of error message
Upstream libusb identifies languages using only the first two
characters of the language code.
We adopt the same approach to maintain API compatibility.

Currently, only English and Mandarin are supported,
as other languages are not yet implemented.

Reviewed by:    kevans
Approved by:    markj (mentor)
MFC after:      2 weeks
Sponsored by:   The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50820
2025-07-11 00:15:30 -04:00
ShengYi Hung bca5df4565 libusb: replace LOG_LEVEL with DEBUG_LEVEL
The libusb set debug level by libusb_log_level instead of
libusb_debug_level. We switch to libusb_log_level for better
compatibility with the upstream libusb.

Additionally, The upstream libusb has an option allows user to override the log
function with a user-defined callback.
To support the callback function with a single string without va_arg, we
refactor the log facility as a function instead of a macro and parse all
va_args inside.

The legacy debug_level enum has been removed to enforce maintainer migrate from their
deprecated source code.

Reviewed by:    kevans
Approved by:    markj (mentor), lwhsu (mentor)
MFC after:      2 weeks
Sponsored by:   FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50680
2025-07-08 22:19:47 -04:00
ShengYi Hung f84a0da4e0 libusb: rename bNumDeviceCapabilities to bNumDeviceCaps for compatibility with libusb
THe member bNumDeviceCapabilities is referred to as bNumDeviceCaps in
the upstream libusb project.

To improve compatibility, we are renaming the member accordingly.

For backward compatibility, a mocro will be defined to map
bNumDeviceCapabilities to bNumDeviceCaps.

See: https://github.com/libusb/libusb/commit/02ebafc85d3f219842cbabaf78abc8100b6656e5

Reviewed by:    kevans
Approved by:    markj (mentor)
MFC after:      2 weeks
Sponsored by:   FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50740
2025-07-08 11:11:41 -04:00
SHENGYI HUNG 4981b8968d libusb: consider bad fd as a broken event
Application can use libusb_get_pollfds to get pollfds from libusb then
close the fd themselves. This cause the hotplug thread unable to leave
because it will be consider as a invalid event then loop forever instead
of a broken event that should be quit immediately.

Reviewed by: bapt
Sponsored by: FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50959
2025-06-25 14:40:02 +02:00
SHENGYI HUNG 4acd63a626 libusb: Fix netlink sysevent multicast group name
Reviewed by:	hrs
Differential Revision: https://reviews.freebsd.org/D50739
2025-06-13 04:30:29 +09:00
Aymeric Wibo 4c556a4e8d libusb: implement libusb_get_parent
Newer versions of drivers such as libwacom (graphics tablets) or
libfprint (fingerprint scanners) call g_usb_device_get_parent.  This in
turn calls libusb_get_parent on platforms which implement it, and
returns NULL on platforms that don't.  This patch implements this
function on FreeBSD.

Reviewed by:	bapt, kevans
Differential Revision:	https://reviews.freebsd.org/D46992
2025-06-12 11:56:31 -05:00
SHENGYI HUNG f938c0a903 libusb: add SUPER_PLUS in speed enum.
Summary:
Some application(like usbmuxd) start to use LIBUSB_SPEED_SUPER_PLUS now.
Though we don't have the corresponding infra in kernel right now, it is
harmless to have this enum value in userspace to prevent the compile error

Reviewed By: bapt
Sponsored By: FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50359
2025-05-15 20:30:32 +02:00
Baptiste Daroussin ae28929b7c libusb: use eventfd
Simplify libusb code by replacing pipe(2) inter thread event mecanism
with eventfd(2).

MFC After:	3 weeks
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D50360
2025-05-15 16:24:46 +02:00
SHENGYI HUNG d285265918 libusb: Emit event after deregistering hotplug handler
The original implementation did not emit an event when a hotplug handler
was deregistered. This omission causes issues for programs that follow
the best practices recommended by libusb—particularly those that use
`libusb_hotplug_register_callback()` or similar functions in a loop
while managing hotplug handlers dynamically.

Without emitting an event after deregistration, these programs can
become stuck waiting indefinitely for an event that will never come,
as the condition to break out of the wait loop is never satisfied.

See: https://github.com/libusb/libusb/blob/6c0ae1ab456da49e7805115e77ce0428ace4ea41/libusb/hotplug.c#L459

Reviewed by:    bapt
Sponsored By:   FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50170
2025-05-08 14:20:21 -04:00
Baptiste Daroussin a92ce19519 libusb: use the new snl_get_genl_mcast_group 2025-01-16 16:46:04 +01:00
Baptiste Daroussin 9dc96d8bc3 libusb: hotplug, use events instead of a timer when possible
Try to fetch events from nlsysevent or devd to determine when
to scan the usb bus for devices addition or removal.
if none are available fallback on the regular timer based (4s)
scanner

if devd socket or netlink socket is closed or error fallback on the
timer based method.

Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D48300
2025-01-16 15:10:11 +01:00
Baptiste Daroussin ba5834b8e1 libusb: fix hotplug sigbus
When a hotplug callback has been registered, and the program using
libusb is calling libusb_exit then the thread handler is set to
NO_THREAD which result in the variable controlling the loop the be set
to 0, it does a last pass through device available without having done
a scan, which result in a sigbus after it tried to unregister all the
devices.

directly break the loop instead and cleanup the list of devices

this fixes the tests with LGPLed libusb's hotplugtest program

MFC After:	3 days
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D48298
2025-01-04 11:20:49 +01:00
Graham Percival b74aaa1a21 manuals: Fix dates
These were reported by `mandoc -T lint ...` as warnings.

Signed-off-by:	Graham Percival <gperciva@tarsnap.com>
Reviewed by:	mhorne
MFC after:	3 days
Sponsored by:	Tarsnap Backup Inc.
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1475
2024-10-28 12:26:03 -03:00
Ed Maste 5654b42142 libusb: claim to be version 1.0.16
We are not 100% compatible with 1.0.16, but implement some
functionality from that version that is required by certain ports.

PR:		277799
PR:		279555 (exp-run)
Event:		Kitchener-Waterloo Hackathon 202406
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D45514
2024-06-28 12:18:34 -04:00
Ting-Hsuan Huang 2a3a8eb9fa libusb(3): Fix link in comment
Event:		Advanced UNIX Programming Course (Fall’23) at NTHU.
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1042
2024-02-13 16:55:54 +08:00
Warner Losh fa9896e082 Remove $FreeBSD$: two-line nroff pattern
Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/
2023-08-16 11:55:10 -06:00
Warner Losh d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh 2a63c3be15 Remove $FreeBSD$: one-line .c comment pattern
Remove /^/[*/]\s*\$FreeBSD\$.*\n/
2023-08-16 11:54:29 -06:00
Warner Losh 4d846d260e spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix
2023-05-12 10:44:03 -06:00
Hans Petter Selasky 4c6bcffd04 libusb(3): Implement libusb_init_context() and the needed structures and definitions.
Differential Revision:	https://reviews.freebsd.org/D38212
MFC after:	1 week
Sponsored by:	NVIDIA Networking
2023-03-30 17:26:55 +02:00
Hans Petter Selasky aa87aa5232 libusb(3): Implement libusb_interrupt_event_handler() by exposing existing function.
MFC after:	1 week
Sponsored by:	NVIDIA Networking
2022-10-02 17:32:59 +02:00
Alex Richardson ac175bd33a Install working pkgconfig .pc files for compat libraries
The default ones are install them to /usr/libdata/pkgconfig, and we can't
use this path for compat libraries, so we use /usr/lib<suffix>/pkgconfigi here.

Test Plan:	grep -rn libdir= ./usr/lib32/pkgconfig/*.pc
MFC after:	1 week
Differential Revision: https://reviews.freebsd.org/D34939
2022-08-11 23:18:34 +01:00
Hans Petter Selasky d94d94e2c0 libusb(3): Ignore SIGPIPE when initializing the LibUSB v1.0 API.
The LibUSB v1.0 emulation layer uses pipes internally to signal between
threads. When USB devices are reset, as part of loading firmware, SIGPIPE
may happen, and that is expected and should be ignored.

PR:		261891
MFC after:	1 week
Sponsored by:	NVIDIA Networking
2022-03-02 12:33:40 +01:00
Brooks Davis 8010f4ad21 libusb: remove use of COMPAT_32BIT
This codepath used uint64_t's in place of pointers in structs and
arrays to allow 32-bit code to use 64-bit version of ioctls.  Now
that we support 32-bit compat natively this is no longer needed.

Reviewed by:	hselasky, jrtc27 (prior version)
2021-12-17 21:28:14 +00:00
Hans Petter Selasky 6847ea5019 Improve handling of USB device re-open in the LibUSB v1.x API.
Make sure the "device_is_gone" flag is cleared after every successful open,
so that the "device_is_gone" flag doesn't persist forever.

Found by:	sergii.dmytruk@3mdeb.com
PR:		256296
MFC after:	1 week
Sponsored by:	Mellanox Technologies // NVIDIA Networking
2021-06-11 17:06:44 +02:00
Emmanuel Vadot 500f4659d7 Convert libs with pc files to use PCFILES
Now the .pc ends up in the correct package (-dev)

Reviewed by:	bapt, emaste
Differential Revision:	https://reviews.freebsd.org/D29172
MFC after:      2 weeks
2021-03-16 07:13:07 +01:00
Hans Petter Selasky 1055793115 Be bug compatible with other operating systems by allowing non-sequential
interface numbering for USB descriptors in userspace. Else certain USB
control requests using the interface number, won't be recognized by the
USB firmware.

Refer to section 9.2.3 in the USB 2.0 specification:
Interfaces are numbered from zero to one less than the number of concurrent interfaces
supported by the configuration.

PR:		251784
MFC after:	1 week
Sponsored by:	Mellanox Technologies // NVIDIA Networking
2020-12-14 11:56:16 +00:00
Gordon Bergling 3d265fce43 Fix a few mandoc issues
- skipping paragraph macro: Pp after Sh
- sections out of conventional order: Sh EXAMPLES
- whitespace at end of input line
- normalizing date format
2020-10-09 19:12:44 +00:00
Kyle Evans 04391da364 libusb: improve compatibility
Specifically, add LIBUSB_CLASS_PHYSICAL and the libusb_has_capability API.
Descriptions and functionality for these derived from the
documentation at [0].  The current set of capabilities are all supported by
libusb.

These were detected as missing after updating net/freerdp to 2.1.1, which
attempted to use both.

[0] http://libusb.sourceforge.net/api-1.0/group__libusb__misc.html

Reviewed by:	hselasky
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D25194
2020-06-09 17:17:43 +00:00
Hans Petter Selasky f6428705d8 Fix for building libusb under Linux.
MFC after:	3 days
Sponsored by:	Mellanox Technologies
2020-03-10 15:59:20 +00:00
Hans Petter Selasky cca46c5e69 Add missing mutex unlock in failure case.
Differential Revision:	https://reviews.freebsd.org/D23430
Submitted by:	cem
Reported by:	Coverity
Coverity CID:	1368773
MFC after:	3 days
Sponsored by:	Mellanox Technologies
2020-01-30 17:30:04 +00:00
Hans Petter Selasky 34b0ca243f Implement new libusb v2.0 API function, libusb20_dev_get_stats().
This function is useful when debugging USB device issues.

MFC after:	1 week
Sponsored by:	Mellanox Technologies
2019-12-27 20:35:12 +00:00
Simon J. Gerraty 2c9a9dfc18 Update Makefile.depend files
Update a bunch of Makefile.depend files as
a result of adding Makefile.depend.options files

Reviewed by:	 bdrewery
MFC after:	1 week
Sponsored by:   Juniper Networks
Differential Revision:  https://reviews.freebsd.org/D22494
2019-12-11 17:37:53 +00:00
Kyle Evans 8495fa081b libusb: LIBUSB_DEBUG environment variable override of libusb_set_debug
The debug level generally just controls verbosity of libusb for debugging
libusb devices/usage. We allow the environment to set the debug level
independent of the application, but the application will always override
this if it explicitly sets the debug level.

Changing the environment is easy, but patching the software to change the
debug level isn't necessarily easy or possible. Further, there's this
write-only debug_fixed variable that would seem to imply that the debug
level should be fixed, but it isn't currently used. Change the logic to use
strtol() so we can detect real 0 vs. conversion failure, then honor
debug_fixed in libusb_set_debug.

Reviewed by:	hselasky
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D21877
2019-10-02 15:19:39 +00:00
Emmanuel Vadot 4c1a82cea5 pkgbase: Create a FreeBSD-utilities package and make it the default one
The default package use to be FreeBSD-runtime but it should only contain
binaries and libs enough to boot to single user and repair the system, it
is also very handy to have a package that can be tranform to a small mfsroot.
So create a new package named FreeBSD-utilities and make it the default one.
Also move a few binaries and lib into this package when it make sense.
Reviewed by:	bapt, gjb
Differential Revision:	https://reviews.freebsd.org/D21506
2019-09-05 14:15:47 +00:00
Hans Petter Selasky a5b24a2b65 Only call libusb_hotplug_enumerate() once from libusb_hotplug_register_callback().
Else when registering multiple filters the same USB device may appear twice in
the list.

MFC after:	3 days
Sponsored by:	Mellanox Technologies
2019-06-26 12:04:54 +00:00
Hans Petter Selasky a41b0ec143 Fix support for LIBUSB_HOTPLUG_ENUMERATE in libusb. Currently all
devices are enumerated regardless of of the LIBUSB_HOTPLUG_ENUMERATE
flag. Make sure when the flag is not specified no arrival events are
generated for currently enumerated devices.

MFC after:	3 days
Sponsored by:	Mellanox Technologies
2019-06-26 11:28:08 +00:00
Hans Petter Selasky 08216d1854 Fix for reading the configuration descriptor in libusb. Catch invalid
configuration descriptor reads early on to avoid issues with devices
that don't check for a valid USB configuration read request.

Submitted by:	takahiro.kurosawa@gmail.com
PR:		238412
MFC after:	3 days
2019-06-08 09:34:02 +00:00
Hans Petter Selasky 1efeb40d1d Fix typos in libusb.
Found by:		Denis Ahrens <denis@h3q.com>
MFC after:		1 week
Sponsored by:		Mellanox Technologies
2019-03-05 14:47:15 +00:00