192 Commits

Author SHA1 Message Date
Tormod Volden
f52a9c0c4d examples/hotplugtest: Close old device only if new one was opened
We want to keep only one device open for simplicity, but instead of
closing the last opened device already before /trying/ to open a new
one, close it only if the new one was opened successfully. This way
we may keep the device open longer, and in the simple case where no
later arriving devices could be opened, until it is detached again.

Closes #1550

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2025-06-09 15:43:48 +02:00
Tormod Volden
6c41069e77 examples/hotplugtest: Allow multiple devices attaching
If no device ID is specified, and for instance a hub is plugged, many
devices will be attached (and later detached) at the same time. The old
code counting only 2 events would then exit prematurely.

By counting attach and detach events separately, we preserve the old
behaviour in the single-device case, but also allow more complex
sequences to be handled in a way that mostly appears intuitive.

This is not fool-proof, and we can still end up leaving after a surprise
detachment of a "pre-existing" device while we have one of the "new"
devices open. In this case print a warning.

References #1455

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2025-06-09 15:43:16 +02:00
Tormod Volden
ed09a92b0b xusb: Fix error reporting on interface claim
There was a mixup of "ret" and "r", so use "r" consistently as
in the rest of the function.

Closes #1636

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2025-05-16 01:17:04 +02:00
Sean McBride
30ec25f738 examples/ezusb: Fix error checking regression in recent commit
Commit 00454ab0 accidently botched error checking when replacing
rewind() with fseek().

Thanks to GitHub user "anotheruserofgithub" for noticing.

Closes #1539
2024-07-29 19:55:01 +02:00
Yiwei Lin
c3873d5c2c xusb: Define proper exit status
Consider different scenario to return from main, we have to choose the
proper return value. In this way, we can easily check the return code
directly like `echo $?` in Linux to know whether the execution of xusb
is failed.

Closes #1507

Signed-off-by: Yiwei Lin <s921975628@gmail.com>
2024-07-28 13:52:17 +02:00
Sean McBride
9cf84577ce Avoid assignments within if statements
Fixes all clang-tidy bugprone-assignment-in-if-condition warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
a18a964aba darwin: Fix multiplication overflow by better matching type sizes
These multiplication could indeed have overflowed, but now they are
performed with a bigger type, matching the type they are ultimately
stored in.

Fixes all clang-tidy bugprone-implicit-widening-of-multiplication-result
warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
3616e751b1 examples/xusb: Match size of loop index to what is iterated
Fixes all clang-tidy bugprone-too-small-loop-variable warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
a7e471dd48 examples/xusb: Make some parameters const where possible
Fixes all clang-tidy readability-non-const-parameter warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
85055a412b examples/xusb: Make all macro replacement lists parenthesized
To protect any lower-precedence operators from the surrounding
expression.

Fixes all clang-tidy bugprone-macro-parentheses warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
9ffdb7fe6e examples/fxload: Eliminate all reserved C identifiers (leading underscores)
The C langugage reserves various identifiers for itself that user code
must not use.

Fixes all clang-tidy bugprone-reserved-identifier warnings

Also, 4 of 5 file extension tests were case insensitive, and 1 was not.
Changed it to be insensitive too.

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
00454ab087 examples/ezusb: Replace rewind with fseek, to check for errors
Replace rewind, which gives no error result, with fseek, which does.

Fixes all clang-tidy bugprone-unsafe-functions warnings

References #1479
2024-07-28 13:27:05 +02:00
Fabien Sanglard
48c6bdea80 Cosmetic cleanup of SuperSpeedPlus comments
- Fix typos.
- Use SuperSpeedPlus naming consistently.
- Remove C++ style comment in favor of C style.

Fixup of commit f00f06e9

References #1499
Closes #1502
2024-05-26 22:25:26 +02:00
Tormod Volden
34d2ca5c63 xusb: Only retrieve BOS descriptor for bcdUSB 0x0201 or higher
The "Binary device Object Store" (BOS) descriptor was introduced with
bcdUSB revision 0x0201.

References #1492

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-05-26 17:19:37 +02:00
Fabien Sanglard
b00332d34e SuperSpeedPlus: Fix typo mantisa -> mantissa in struct field
Fixup of commit f00f06e9

Closes #1499
2024-05-26 10:44:02 +02:00
Harry Mallon
5b17c383f8 Add API support for LIBUSB_SPEED_SUPER_PLUS_X2 20Gbps USB 3.2 gen 2x2
Implement detection on darwin and linux_usbfs, and report it in xusb.

Closes #1477
2024-05-07 19:44:28 +02:00
Fabien Sanglard
f00f06e9eb Add support for SuperSpeed+ Capability Descriptors
See specs in USB 3.1 specs in section:

9.6.2.5 SuperSpeedPlus USB Device Capability

Closes #1428
Fixes #1429
2024-05-07 19:32:50 +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
Sean McBride
288d82f15e xusb: Avoid warning about implicit switch fallthrough
References #1414
2024-04-04 09:01:06 +02:00
Tormod Volden
853a5eac64 examples/hotplugtest: Tone down message about failed open
References #1425

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-01-31 12:32:56 +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
Ingvar Stepanyan
0a2f511914 examples/hotplugtest: Match any device by default
I spent some time trying to figure out why, when I run this example,
it's not showing any events, until I looked into the code and saw it has
some hardcoded VID:PID.

Most people probably don't have the specific device hardcoded here, and
it seems better to default to showing events for any device unless
overridden.

While at it, also updated device detached message to match the attached
one to show VID:PID.

Closes #1350

[Tormod: Print error strings on failures]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-11-27 23:06:32 +01:00
Tormod Volden
2abb2a186e xusb: Improve Max LUN retrieval stall handling
Do not show an uninitialized Max LUN value if the pipe stalled.

As suggested by the code comment (but not the old code), set a default
zero value only on pipe stall. On other errors simply error out.

References #1181
Closes #1239

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:53:06 +01:00
Tormod Volden
0dbe62bde3 xusb: Improve kernel driver probe messages
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-28 11:40:16 +01:00
Tormod Volden
9e077421b8 xusb: Add newlines in mass-storage test output
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-21 14:03:07 +01:00
Nathan Hjelm
3c33e499a0 Update tests and example to use the new libusb_init_context() function
This commit updates all test and example code to use the newer
libusb_init_context() function instead of libusb_init().

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Tormod: Update umockdev.c as well]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-20 11:04:14 +01:00
Dominik Boehi
82d912307f descriptor: Add support for BOS platform descriptor parsing
Also add BOS platform descriptor dump to xusb example.

Closes #1133

[Tormod: Fixed copy-pasto in Doxygen comment]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2023-01-19 15:04:48 +01:00
Ryan Metcalfe
99f1942e81 xusb: Print info from IAD descriptors
Signed-off-by: Ryan Metcalfe <ryan.metcalfe@novanta.com>
2023-01-19 15:02:11 +01:00
Sean McBride
fa9a25b3c4 examples: Replace most uses of sprintf with safer snprintf
Closes #1155
2022-06-26 17:09:14 +02:00
Tormod Volden
afcd360005 examples: Do not assume positive errno macros
Some functions were returning e.g. -ENOMEM and the caller would check
for negative return values. However, on Haiku, contrary to modern
standards, these macros are negative, so this logic would fail. In any
case, change the function to return -1 instead. For good measure also
set errno to the appropriate value, although it is not used in the
current code.

This was discovered on Haiku builds because the value for ENOMEM is
INT_MIN which cannot be negated without overflow.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-01-22 14:47:17 +01:00
Tormod Volden
b88f144185 xusb: Print total length of configuration descriptor
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2021-10-31 18:32:25 +01:00
luz paz
14d3bde10f Fix various typos in docs/comments
Found via `codespell -q 3`

Closes #1015
2021-10-31 17:44:55 +01:00
Tormod Volden
6992d9bd4f xusb: Print configuration descriptor length
Also inform about reading OS string descriptor.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2021-10-31 09:12:48 +01:00
Nathan Hjelm
42f9c3b43f xusb: add check for interface kernel driver
This commit adds a call to libusb_kernel_driver_active and prints out the result. This
provides a way to quickly check the result when testing.

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2021-07-21 12:41:26 -06:00
Chris Dickens
a2f1e2d5fd Windows: Support building dpfp and sam3u_benchmark with MSVC
Closes #151

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-11-27 18:39:53 -08:00
Chris Dickens
f2e551a5ff build: Prepare config.h for inclusion in examples/tests
There are certain games played in the examples and tests source to
account for differences in build environments and target platforms. This
can be simplified by including config.h and using the definitions there.

To that end, move the printf function attribute definition from
libusbi.h to config.h and leverage it where it is used in the examples
and tests.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-11-27 15:22:29 -08:00
Chris Dickens
9d23ed25b4 build: Fix Android and Xcode compilation errors/warnings
Commit f69548c3b3 ("examples: Enable all examples to build on all
platforms") modified dpfp and sam3u_benchmark to be buildable on any
platform, however there were some oversights and regressions introduced
for Android and Xcode.

Update the Android and Xcode build files to account for the removal of
dpfp_threaded.c as well as the inclusion of config.h from examples
and/or tests source.

Additionally switch the threaded version of dpfp to use sem_open()
instead of sem_init() as the latter is in fact deprecated on MacOS.

Closes #808

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-11-16 13:03:52 -08:00
Chris Dickens
f69548c3b3 examples: Enable all examples to build on all platforms
Prior to this change, the dpfp and sam3u_benchmark examples were only
built on POSIX platforms due to a dependency on sigaction(). Furthermore
the dpfp_threaded example only worked with POSIX threads.

Lift this limitation by breaking the dependence on sigaction(). Also
provide a minimal threading abstraction so that dpfp_threaded can be
built on Windows as well.

Also merge the sources for dpfp and dpfp_threaded. The only difference
between the two is how libusb's event handling functions are called and
this can be easily handled within a single source file.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-11-10 14:13:12 -08:00
Chris Dickens
15668087a7 Fix various CI build warnings
* [-Wpointer-arith] arithmetic on a pointer to void is a GNU extension
  * [-Wswitch-enum] enumeration values 'E1, ...' not explicitly handled in switch
  * [-Wunused-parameter] unused parameter 'p'

For '-Wswitch-enum', the switch statements in the individual backends'
set_option() function has been removed. It is not expected that backends
will need to handle or be aware of all the options.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-11-09 15:51:44 -08:00
Hans de Goede
1be063f7bf examples/testlibusb: Print speed of devices
Print the speed of devices to exercise libusb_get_device_speed().

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2020-09-14 09:48:14 +02:00
Hans de Goede
dca3e7ea36 examples/testlibusb: Add support for testing libusb_wrap_sys_device()
Sometimes it is useful to be able to test libusb_wrap_sys_device(),
add support for this to the testlibusb example

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2020-09-14 09:48:14 +02:00
Chris Dickens
da5df37c4d build: Merge events and threads into single platform abstraction
The split between events and threads abstractions is unnecessary.
Simplify the library by merging the two into a "platform" abstraction.
The only meaningful change is that Cygwin builds will no longer use the
POSIX threads abstraction but will instead use the native Windows one.

The downside to doing this is that the dpfp_threaded example program
will no longer be available on Cygwin builds. This should be fine, and
future work will make dpfp_threaded available for all forms of Windows
build systems.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-09-12 22:44:28 -07:00
Chris Dickens
0abebc57a5 Fix typos detected by codespell and manual inspection
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-08-18 15:49:22 -07:00
Chris Dickens
6f0330c784 configure.ac: Simplify detection of Windows compilation
There are a few parts within the library where code is conditionally
compiled based on whether or not the target OS is Windows. Prior to
commit 8b09dd490d ("core: Kill the OS_* definitions and use in the
source code"), the OS_WINDOWS definition was used. With that definition
gone, the checks were replaced with _WIN32. Unfortunately the different
cross-platform toolchains do not universally define this, so add this to
AM_CPPFLAGS when building for Windows.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-04-09 11:46:03 -07:00
Chris Dickens
09d0312fbb Xcode: Update project file
Add '-fvisibility=hidden' to the additional compiler flags of the libusb
target so that internal library symbols are hidden.

Add '-pthread' to the additional compiler flags of the targets that
directly use pthread functionality

Add the 'sam3u_benchmark' and 'testlibusb' targets so that all examples
are now built and fix build warnings that occur when building
'sam3u_benchmark'.

Fix target dependencies so that all targets are able to build without
issues.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-30 16:53:43 -07:00
Chris Dickens
95bbfb6023 build: Enable additional build errors and warnings
Help catch more errors by enabling additional build errors and warnings.
Address some of the warnings seen with these new flags, including moving
the libusb_transfer structure back out of the usbi_transfer structure to
address 'warning: invalid use of structure with flexible array member'.
Apparently a structure ending with a flexible array member is not okay
with the compiler as the last member within another structure.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-30 12:38:47 -07:00
Chris Dickens
9a1bc8cafb build: Require C11 to build and clean up autoconfig/automake files
C11 compiler support has been available for many years now. It is not
unreasonable to require this now, and doing so allows some cleanup to
the configure script. It is no longer necessary to check for compiler
support of the '-fvibility' flag because any compiler that supports C11
will support this flag as well.

Fix up the way that compiler and linker flags are passed down to the
various makefiles. The compiler flags should be shared by all, but the
linker flags and libraries should be separated between the library and
the examples/tests. The visibility flag is only relevant for the
library and the thread flags are only relevant for sources using thread
constructs, so provide them as needed.

Rearrange configure.ac to group similar functionality and consolidate
where possible.

Based on these changes, update the Travis configuration file to include
newer versions of test platforms to ensure proper C11 compiler support.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-30 12:28:11 -07:00
Chris Dickens
07d6d3a2a5 msvc: Reorder solution projects and add project filter files
Since Visual Studio picks the first project as the default startup
project, move the library projects back to the top of the list. This use
to be the order but was changed in commit 9843b689df.

Add the project filter files to help organize the source files within
the Solution Explorer window pane.

Additionally move the getopt source underneath the msvc directory since
it is only used for Visual Studio builds.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-23 15:17:50 -07:00
Chris Dickens
f90d07613c Windows: Remove support for WinCE and Visual Studio older than 2013
There appears to be no need for the WinCE backend anymore, and it is
increasingly difficult to keep healthy as the rest of the library
changes.

Require at least Visual Studio 2013 to compile. This simplifies matters
as there is some semblance of C99 support there.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-01-20 18:02:19 -08:00
Chris Dickens
cb95e3ad35 examples: testlibusb: Always print VID and PID in addition to strings
Previously the program would only print the VID when the manufacturer
string is unavailable and the PID when the product string is
unavailable. Change this to print the VID and PID unconditionally and
print the manufacturer and product strings similar to how the serial
number string is being printed. In addition, line up the string values
with the rest of the output.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-01-13 12:50:40 -08:00