317 Commits

Author SHA1 Message Date
squidbus
c4d237a580 More ABI fixes 2025-09-07 13:56:12 -07:00
Tormod Volden
c6c45b8e8c docs: Refer to Topics instead of Modules
doxygen generates a "Topics" instead of "Modules" button these days.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2025-05-03 11:31:07 +02:00
Tormod Volden
1496324291 core: Avoid infinite recursion on invalid LIBUSB_DEBUG value
Silently ignore invalid values, since we cannot use logging inside
the get_env_debug_level() function.

Fixes #1619

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2025-03-18 09:54:55 +01:00
Sean McBride
9d595d4e4a Replace atoi() with strtol() which allows error checking
atoi() gives no opportunity for error checking, strtol() does. Improved
error checking.

Closes #1422
2024-07-28 14:12:07 +02:00
Tormod Volden
2a138c6f12 Consistent use of C-style comment markers
Except for the Windows, Emscripten, and Haiku backends we use C-style
comments markers and not double-slash. Get rid of a few inconsistent
// instances.

Note the doxygen code examples have // comments because they are inside
proper /* */ comments.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-05-26 23:10:24 +02:00
Sean McBride
a99a258102 Increase usbi_get_tid() size from int to long
This function has different implementations on every OS, but for some,
like macOS, it was truncating from 64 to 32 bit by casting to int. So
increase its size from int to long.

(The function is currently only used for debug output.)

Closes #1423
2024-04-20 10:18:17 +02:00
Sean McBride
a07ecfe02a Fix most warnings about implicit sign conversion
- Sometimes just change a type to match where it's coming from

- Sometimes add an explicit cast (but only if it's guaranteed correct,
  like a previous check that a signed value is positive before casting to
  unsigned)

- For libusb_bulk_transfer() add an assert to be certain that the signed
  return value is never negative.
  Update API documentation to underline this fact.
  Add similar assert in usbi_handle_transfer_completion().

- darwin: For parsing OS version, change scanf to use %u instead of %i

- xusb: Add additional range checks before casting

- xusb: Reverse some backwards count and size arguments to calloc, not
  that it really matters

Closes #1414
2024-04-04 09:01:26 +02:00
Tormod Volden
6558778833 docs: Add more references in libusb_option section
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-01-27 10:52:30 +01:00
Lars Kanis
a5483bc0f7 core: Allow setting global log callback after first libusb_init()
When setting LIBUSB_OPTION_LOG_CB before the first libusb_init(),
the global callback is set.

However, after the first libusb_init() there is a default context, and
setting LIBUSB_OPTION_LOG_CB would only set this default context, and
not the global callback. There would be no way to set the global
callback through libusb_set_option().

Change this so that the global log callback is set in addition to the
default context (when libusb_set_option is called with NULL context).

Closes #1397
2024-01-27 10:52:12 +01:00
Lars Kanis
d09d341f41 core: Don't overwrite the log callback on every global libusb_set_option()
But set it only on option LIBUSB_OPTION_LOG_CB.

Otherwise the following second call resets the callback to the
default one:

 libusb_set_option(NULL, LIBUSB_OPTION_LOG_CB, cb_func);
 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);

References #1397
2024-01-27 10:51:42 +01:00
Sean McBride
0929a2b1d1 docs: Prefer use of libusb_init_context() over old libusb_init()
- Replace some uses of the soft-deprecated libusb_init() and
  libusb_set_debug() with their updates.

- Updated various comments to favour use of the new functions.

- Notably remove \deprecated doxygen statement, since it causes
  -Wdocumentation compiler warnings.

- Notably update the xusb example to pass debug options to
  libusb_init_context() instead of setting environment variable.

- Also improve comments related to LIBUSB_API_VERSION and
  LIBUSBX_API_VERSION.

Closes #1408

[Tormod: Fix Doxygen formatting and references]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-01-20 13:27:06 +01:00
Sean McBride
5e4b389f74 Replace all http://libusb.info/ with https
The libusb.info website, like most sites these days, auto-redirects to
the https version anyway.

Also replaced a couple of other http links to https, after verifying
that they autoredirect to https.

Note this changes the "describe" field in the version info returned by
libusb_get_version(). This field is only there for ABI compatibility and
contains the website URL.

Closes #1407
2024-01-19 21:53:20 +01:00
Sean McBride
7ab9c93d7d core: Add missing mutex acquisition when manipulating active_contexts_list
The `active_contexts_list` is supposed to be protected by the
`active_contexts_lock` mutex.

Upon code review, found one place where the mutex was not acquired.

Closes #1413
2024-01-19 19:43:24 +01:00
Sean McBride
5ad1d992f1 core: Fix -Wswitch warnings by including all enum values in switch
No change in behaviour.

References #1426
2024-01-05 00:14:49 +01:00
Martin Ling
dac541dc75 Revert "windows: Add option for WinUSB RAW_IO endpoint policy"
This reverts commit 9e4bb9cbe5.

References: #1297
2023-12-09 09:06:32 +01:00
Tormod Volden
80493dddfc core: Avoid warning with logging disabled
Fixup of commit 9de0fefb

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-12-01 11:00:29 +01:00
SomeAlphabetGuy
9de0fefb31 core: Avoid possible data race in log_v()
The logging function usbi_dbg() called from udev event thread needs to
safely get the log level of the default context or the fallback context.
This is not trivial because udev thread may log events while contexts
are created and destroyed. Locking the default context mutex is not an
option because usbi_dbg() may be called from code that already holds
this mutex, which would lead to a deadlock. The solution implemented in
this commit is to maintain a separate atomic global variable with the
default debug level.

The issue was found with ThreadSanitizer, although no actual bugs have
been observed.

libusb_set_debug() was also changed to call libusb_set_option() instead
of doing its own option setting which had fewer checks.

Closes #1332
2023-11-30 11:36:27 +01:00
Tormod Volden
d291eec441 Revert "core: Remove select case not possibly reached"
This reverts commit cd8078db, which although it made a static analyzer
happy, unfortunately got compilers to complain (with -Wswitch-enum
enabled).

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-29 18:10:43 +01:00
Tormod Volden
ba83b68c2f core: Fix always-true condition in log callback function setting
This could possibly scribble the arg union for other options
although it wouldn't happen with our current set of options.

Fixup of commit 84ac0b0

References #1265

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-06 11:06:20 +02:00
Tormod Volden
76df571c15 core: Avoid crash due to premature va_end
After va_end(ap) the variable ap is undefined and cannot be
accessed.

Fixes #1274
Closes #1284

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-07-06 10:14:20 +02:00
Ludovic Rousseau
3bace666b7 Fix build error with --enable-debug-log
The error is:
core.c:2451:8: error: no member named 'debug' in 'struct libusb_context'
        _ctx->debug = LIBUSB_LOG_LEVEL_NONE;
        ~~~~  ^

Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2023-05-09 22:47:27 +02:00
Nathan Hjelm
84ac0b0f60 Add support for setting the log callback with libusb_set_option/libusb_init_context
This commit effectively deprecates libusb_set_log_cb by adding support for
setting the callback in either libusb_set_option or libusb_init_context. Since
the libusb_set_log_cb is already in use we can not easily deprecate it without
first incrementing the major version. We may do that in the future.

Closes #1265

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-04-12 13:02:35 -06:00
Nathan Hjelm
d2da7f9b1d libusb_set_options: Adjust semantics of libusb_set_option for log level
This commit updates the actual semantics of:

libusb_set_option(_, LIBUSB_OPTION_LOG_LEVEL, _)

to match the documentation. The documentation states that if the
LIBUSB_DEBUG environment variable is set then that level overrides the
value set by libusb_set_option.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-03-12 12:36:43 +01:00
Tormod Volden
5b7d57e61a Fix most -Wpedantic warnings
On Linux and gcc 12.2 at least.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-21 11:30:06 +01:00
Orhan aib Kavrakoglu
9b42fdd787 core: Add libusb_get_max_alt_packet_size()
Closes #1167
2023-01-21 11:26:11 +01:00
Orhan aib Kavrakoglu
94eb23991b core: Add helper find_alt_endpoint() 2023-01-20 17:51:34 +01:00
Orhan aib Kavrakoglu
6c2149662a core: Factor out helper get_endpoint_max_packet_size()
[Tormod: Initialize r to please compilers]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-20 17:50:55 +01:00
Martin Ling
9e4bb9cbe5 windows: Add option for WinUSB RAW_IO endpoint policy
Add support for enabling or disabling the WinUSB RAW_IO pipe policy for
a given endpoint, which is documented here:

https://learn.microsoft.com/en-us/windows-hardware/drivers/usbcon/winusb-functions-for-pipe-policy-modification

This is necessary to increase performance. Without this option the
WinUSB backend will only queue one inbound operation at a time, even if
the libusb async API is used to submit multiple transfers.

For real-time sampling devices with high sample rates and small buffers,
the use of RAW_IO combined with queued async transfers is essential to
maintaining the necessary throughput and avoiding lost samples or buffer
overruns.

Examples of devices affected include Cypress FX2 based logic analyzers
accessed using Sigrok, and the HackRF software defined radio.

The new option must be set by calling libusb_set_option with the arguments:

libusb_set_option(ctx, LIBUSB_OPTION_WINUSB_RAW_IO, dev_handle,
                  endpoint_address, enable, max_transfer_size_ptr)

where the types of the variadic arguments are:

libusb_device_handle *dev_handle;
unsigned int endpoint_address;
unsigned int enable;
unsigned int *max_transfer_size_ptr;

The dev_handle and endpoint_address parameters must identify a valid IN
endpoint on an open device. If enable is nonzero, RAW_IO is enabled,
otherwise it is disabled. Unless max_transfer_size_ptr is NULL, then on
a successful call to enable RAW_IO, the pointer destination will be
written with the MAXIMUM_TRANSFER_SIZE value for the endpoint.

Whilst RAW_IO is enabled for an endpoint, all transfers on that endpoint
must meet the following two requirements:

- The buffer length must be a multiple of the maximum endpoint packet size.
- The length must be less than or equal to the MAXIMUM_TRANSFER_SIZE value.

This option should not be changed when any transfer is in progress on
the specified endpoint.

This option only affects the WinUSB backend. On other backends it is
ignored and returns LIBUSB_ERROR_NOT_SUPPORTED, without modifying the
value pointed to by max_transfer_size_ptr.

A great deal of credit is due to Petteri Aimonen and Patrick Stewart for
previous work, and to everyone else who participated in discussions.

Fixes #490
Closes #1208
2023-01-20 11:05:01 +01:00
Nathan Hjelm
6622f386f5 core: Add libusb_init_context() setting options at context creation
The new initialization function addresses some shortcomings of the
libusb_set_option() interface. Namely, it allows setting the
no-enumeration option (and others) on only the contexts where it is
requested. The old initialization function (libusb_init()) is deprecated
and will be removed in a future release. For now it translates to a call
to libusb_init_context() with no options specified.

Closes #1026

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Tormod: Doxygen description of libusb_init_option structure]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-20 10:16:43 +01:00
Francis Hart
565bb18011 Fix setting log level before libusb_init()
The header docs state its possible to set a default log level before
calling libusb_init(), and that this log level will be used for all
contexts created after (to quote):

    "Note that libusb_set_option(NULL, ...) is special, and adds
     an option to a list of default options for new contexts."

This updates the logic inside libusb_init() to ensure this
behaviour is followed.

Fixes #1207

Signed-off-by: Francis Hart <francis@kuvacode.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-01-09 20:47:02 -07:00
Tormod Volden
1c99382a3d core: Do not warn about new context as implicit default
Only leave a debug message.

Fixes #1215

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-12-19 22:13:26 +01:00
Pablo Prietz
18a64374e1 core: Remove leftover usbi_mutex_unlock in libusb_set_interface_alt_setting
Fixup of commit 1a08aa8.

Closes #1200
2022-12-19 21:38:21 +01:00
Rosen Penev
5c27abd59e windows: Mark libusb_set_option as WINAPIV
WINAPI evaluates to __stdcall, which does not work with variadic
functions. MSVC and MinGW implicitly demote this to __cdecl (clang does
too and also warns about it). Instead of having this implicit, make it
explicit.

Closes #1160

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2022-09-04 21:10:40 +02:00
Tormod Volden
432bc0954d core: Avoid mutex lock while printing error and warning
Closes #1187

Reviewed-by: Dmitry Zakablukov <dimaz@passware.com>
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-09-03 11:55:10 +02:00
Tormod Volden
cd8078db05 core: Remove select case not possibly reached
References #1067

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-06-26 17:09:14 +02:00
Ludovic Rousseau
0c09ba6daf Doxygen: Improve libusb_wrap_sys_device() documentation
Create links to:
- libusb_set_option()
- LIBUSB_OPTION_NO_DEVICE_DISCOVERY
2022-06-26 17:09:14 +02:00
Ludovic Rousseau
69a0145d6f Doxygen: Add references to LIBUSB_ERROR codes
Signed-off-by: Ludovic Rousseau <ludovic.rousseau@free.fr>
2022-06-26 17:09:14 +02:00
Craig Hutchinson
a8f8d4ba3d Fix exit order to match reverse of init
This commit resolve the ordering of 'exit' dependencies as was started for 'init' in 0846456f3a

(Relates to https://github.com/xloem/libusb/pull/5)

Closes #1126

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2022-04-13 08:47:50 -06:00
Benjamin Berg
6b29aeb9ca core: Install first context as implicit default (and warn about its use)
There was a behaviour change in libusb 1.0.25 which triggers issues when
the API is misused. This caused for instance gutenprint to crash, see
https://bugzilla.redhat.com/show_bug.cgi?id=2055504

This seems to affect several applications "out in the wild".

For now, work around this by installing an implicit default. But, change
the code to log an error in case this "feature" is being used.

This will allow some grace time for developers to fix their
applications, before we at a later point revert to the stricter
behaviour.

Fixes #1089
2022-03-20 10:55:17 +01:00
Benjamin Berg
0846456f3a core: Suppress hotplug events during initial enumeration
The initial enumeration should not result in hotplug events to be fired.
This is just a convenience though, API users still need to be prepared
to be notified a second time for a device that was plugged in between
libusb_init and libusb_hotplug_register_callback.

This regressed with commit 6929b82 ("Fix segmentation fault in
libusb_init() if usbi_backend.init() fails"). This commit avoids the
mentioned segmentation fault by avoiding to clean up the hotplug code if
it was not yet initialised.

Fixes #1082
Closes #1090
References #989
2022-03-16 17:53:00 +01:00
Benjamin Berg
c3639bc23e core: Unset device ctx if it has been destroyed
Devices can outlive their context in some cases (in particular with
python garbage collection). Guard against this by clearing the ctx
pointer so that it is not pointing to uninitialized memory.

Closes #1058
2022-03-15 11:43:05 +01:00
Tormod Volden
33d9949921 Keep LIBUSB_OPTION_WEAK_AUTHORITY as a macro with same value
If the value is always the same we retain binary compatibility.

References #935

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@me.com>
2021-11-27 10:04:05 -07:00
Matthias Bolte
d4452bd555 core: Unlock and clear default ctx in all error paths in libusb_init
Commit 32a2206942 reordered and
refactored libusb_init. This resulted in moving code outside the
default ctx lock that was unrelated to it. But this resulted in
broken error path cleanup logic that leaves the default ctx as
locked, half initialized and freed in case a libusb_set_option or
the usbi_io_init call fails.

Undo part of the previous reordering to unlock and clear the default
ctx in all error paths in libusb_init.

Closes #995

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2021-11-23 23:02:43 -07:00
Tormod Volden
683e3cf21e core: Minor code cleanup in libusb_init (NOP)
After commit f7084fe the if clause would always be true, so remove it.

Thanks to "xloem" for reporting.

References #942

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2021-11-09 10:05:38 +01:00
Tormod Volden
1d1411b606 doc: Explain default options in libusb_set_option documentation
References #942

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2021-11-01 23:22:56 +01:00
Bruno Harbulot
f7084fea11 core: Apply default options to all new contexts
The default options configured with libusb_set_option(NULL, ...)
were only applied when the default context was created, and were
ignored when calling libusb_init() with a non-null context pointer.

Make sure the default options will be applied to all new contexts. This
is important when using LIBUSB_OPTION_NO_DEVICE_DISCOVERY which must
be respected during the context initialization in order to work.

Closes #942

[Tormod: amend comments]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2021-11-01 23:20:59 +01:00
Sean McBride
35b23a047e doc: Improve comments related to device discovery and hotplug
Closes #1013
2021-10-30 15:23:15 +02:00
Tormod Volden
762456139a doc: Update note about LIBUSB_TRANSFER_ADD_ZERO_PACKET availability
Closes #1005

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2021-10-30 15:22:31 +02:00
Ankur Verma
6929b82701 Fix segmentation fault in libusb_init() if usbi_backend.init() fails
If the backend init fails, the control goes to err_io_exit which tries
to clean up hotplug related lists that aren't initialized. Moving
hotplug_init before makes sure the lists are valid so if backend init
fails they get cleaned up without errors.

Closes #989
2021-09-23 10:52:11 +02:00
Nathan Hjelm
e568db9f72 core: set default backend options before calling backend init
This commit restores the previous behavior with regards to setting some backend options and
restores the ability to set default context options before libusb_init. None of the backends
use anything set in init in their set_option function so this should be safe with all
backends and options.

References #942

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2021-07-26 23:23:12 -06:00