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>
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>
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
- 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
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
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
- 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>
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
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
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
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>
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>
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>
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>
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>
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#490Closes#1208
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>
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>
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>
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
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#1082Closes#1090
References #989
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
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>
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>
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>
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>
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
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>