Commit graph

1942 commits

Author SHA1 Message Date
Radu Vele
de38189e80 Enable linux netlink event monitoring for Android OS platform services
Some checks failed
linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
MSYS2 build / build (push) Has been cancelled
MSYS2 clang32 build / build (push) Has been cancelled
MSYS2 clang64 build / build (push) Has been cancelled
Introducing the ANDROID_OS macro that is supposed to be set by clients
of libusb that are running on Android at the OS level.

If Android_OS is set (and HAVE_LIBUDEV is not) then linux netlink event
monitoring (and consequently hotplug functionality) is enabled. This
works for Android services or simple binaries running at the OS level as
they have the necessary permissions, in contrast to usual Android apps
(see commit 2f3bc98).

Note: the __ANDROID__ macro is set by GCC for all targets running on
Android (both OS-level services and apps).

Closes #1577

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2024-11-13 21:16:58 -07:00
Fabien Sanglard
7bc88c0f08 macos: Fix Zero-Length Packet for multiple packets per frame
Some checks are pending
linux / build (push) Waiting to run
macOS / build (push) Waiting to run
MSYS2 build / build (push) Waiting to run
MSYS2 clang32 build / build (push) Waiting to run
MSYS2 clang64 build / build (push) Waiting to run
Summary of problem:

The LIBUSB_TRANSFER_ADD_ZERO_PACKET flag does not work as expected on
Darwin. On my USB 3 device, with maxPacketSize = 1024, I see libusb
sending ZLP for packets multiple of 5120 instead of 1024.

The ZLPs are not being sent when they should because
darwin_get_pipe_properties() retrieve the "full" wMaxPacketSize via
GetPipePropertiesV3(), which includes bandwidth multipliers.

Summary of solution:

GetEndpointPropertiesV3() does retrieve unaltered wMaxPacketSize,
without multipliers. To call it, we first use GetPipePropertiesV3() to
set the fields required.

Fixes #1581
Closes #1582
2024-11-13 19:33:24 +01:00
Tormod Volden
7adb2913ce docs: Fix broken doxygen references
Some checks are pending
linux / build (push) Waiting to run
macOS / build (push) Waiting to run
MSYS2 build / build (push) Waiting to run
MSYS2 clang32 build / build (push) Waiting to run
MSYS2 clang64 build / build (push) Waiting to run
Fixup of commit d7b796f1

References #1549

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-11-13 11:07:55 +01:00
Tormod Volden
0b4eda697f docs: Hide internal descriptor.c structure from doxygen
Follow-up of commit f00f06e9

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-11-13 10:57:56 +01:00
Fabien Sanglard
28a6afb690 docs: Document internal_ssplus_capability_descriptor
Also include SSP documentation fixups of commit f00f06e9
that had introduced Doxygen errors.

Closes #1545
2024-11-13 10:57:49 +01:00
Matthijs Lavrijsen
467b6a8896 winusb: Fix winusb_get_device_list() failing to find port numbers
Some checks failed
linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
MSYS2 build / build (push) Has been cancelled
MSYS2 clang32 build / build (push) Has been cancelled
MSYS2 clang64 build / build (push) Has been cancelled
Fix recent regression introduced in commit 9d595d4.

get_dev_port_number() was always returning port number 0 for any port,
due to the incorrect assumption that strtoll's 'end' pointer will point
to a null terminator on success. This was causing
winusb_get_device_list() to always fail. Since we actually know the
expected values of *end for both the SPDRP_LOCATION_INFORMATION and the
SPDRP_LOCATION_PATHS case, check for those instead.

Additionally, document why a return value of 0 is treated as a failure
in this particular function, for valid but somewhat coincidental reasons
that may not be immediately obvious.

Closes #1544
2024-07-31 00:05:01 +02:00
Sean McBride
8776b8021a descriptor: Fix clang -Wimplicit-int-conversion warnings
Some checks are pending
linux / build (push) Waiting to run
macOS / build (push) Waiting to run
MSYS2 build / build (push) Waiting to run
MSYS2 clang32 build / build (push) Waiting to run
MSYS2 clang64 build / build (push) Waiting to run
For the 16-bit case especially, the result of the `or` is implicitly
promoted to `int`, then when returned was warning:

warning: implicit conversion loses integer precision: 'int' to 'uint16_t' (aka 'unsigned short') [-Wimplicit-int-conversion]

Add more casts to shut it up.

Closes #1541
2024-07-30 00:57:06 +02:00
Sean McBride
a3199696e2 xcode: Adjust file indentation settings
Change tab vs space and 2 vs 8 intent in Xcode project to match the
emacs metadata at the top of the files.

Closes #1543
2024-07-30 00:54:21 +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
Tormod Volden
4528752cbe windows: Base HID device descriptor on cached values
Some checks failed
linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
MSYS2 build / build (push) Has been cancelled
MSYS2 clang32 build / build (push) Has been cancelled
MSYS2 clang64 build / build (push) Has been cancelled
Instead of filling in the blanks with hard-coded made-up values that are
sometimes correct, use the cached descriptor values retrieved during
enumeration, which should be a better fallback.

References #1360
Closes #1378

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-07-28 14:25:22 +02:00
Tormod Volden
d04fc0e60b openbsd: Use default clause in _errno_to_libusb()
Same as for NetBSD.

Closes #1295

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-07-28 14:22:20 +02:00
Tormod Volden
bc12cda784 netbsd: Debug print all errors
Same as for OpenBSD.

References #1295

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-07-28 14:21:32 +02: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
bd0fcdb4c2 Add KEYS file for release files verification
Include my own public key, used for 1.0.25/1.0.26/1.0.27 already.

The KEYS file is inspired by the Apache project:
https://infra.apache.org/release-signing#keys-policy

References #1469

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-07-28 14:04:37 +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
e8d76b1a12 clang-tidy: Stop suppressing readability-misleading-indentation warnings
All such warnings are already fixed in master.

Closes #1479
2024-07-28 13:27:06 +02:00
Sean McBride
197e3052cd libusb.h: Match parameter names in declaration and definition
Took the name from the .c file.

Fixes all clang-tidy readability-inconsistent-declaration-parameter-name
warnings

References #1479
2024-07-28 13:27:06 +02:00
Sean McBride
55f8c95551 descriptor: Fix addition overflow by correcting casts
The addition could overflow, the upcast needs to be performed before,
not after.

Fixes all clang-tidy bugprone-misplaced-widening-cast warnings

References #1479
2024-07-28 13:27:06 +02:00
Sean McBride
e3ccc46b6e descriptor: Eliminate all duplicate branch bodies, as they are bug-prone
Copy-pasting can often result in mistakes like:

```
if (test_value(x)) {
  y++;
  do_something(x, y);
} else {
  y++;
  do_something(x, y);
}
```

Thus it is preferable that branch bodies be unique.

Fixes all clang-tidy bugprone-branch-clone warnings

References #1479
2024-07-28 13:27:06 +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
6883f84f93 darwin: Explicitly compare string compare function results to -1, 0, or 1
Fixes all clang-tidy bugprone-suspicious-string-compare warnings

References #1479
2024-07-28 13:27:05 +02:00
Sean McBride
418aadc0f9 darwin: Always use uppercase literal suffixes for improved readability
Depending on the font, an l suffix can look like a 1. Consider "231l".
Thus prefer uppercase.

Fixes all clang-tidy readability-uppercase-literal-suffix 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
Jon Beniston
e678b3fad5 Emscripten: Avoid uncaught TypeError on browsers without USB support
Check if navigator.usb exists before trying to call one of its methods,
to avoid unhandled exception, e.g. on current Firefox.

Closes #1500
2024-05-28 22:35:13 +02:00
Sean McBride
916c740076 descriptor: Avoid buffer over-increment in parse_iad_array function
The first iteration of this loop was safe because the beginning of the
function checked that `size` is at least LIBUSB_DT_CONFIG_SIZE (9) bytes
long.

But for subsequent iterations, it could advance the pointer too far
(which is undefined behaviour) depending on the content of the buffer
itself.

Closes #1460
2024-05-28 22:07:45 +02:00
Sean McBride
678c81271b descriptor: Small clarifications with no behaviour change
All the right hand side is `dev_cap`, change one outlier to match.

Also clarify the relationships between some magic numbers.

No change in behaviour here.
2024-05-28 22:07:45 +02:00
Sean McBride
016a0de33a descriptor: Fix potential offsetting of pointer by too much
This was checking that `size` is at least `LIBUSB_DT_CONFIG_SIZE` (9)
bytes long, but then increments the pointer with `buf +=
header.bLength`. That could end up pointing past of the end of the
buffer. There is a subsequent check that would prevent dereferencing it,
but it's still undefined behaviour to even create such a pointer.

Add a check with a similar pattern as elsewhere in this file.
2024-05-28 22:07:45 +02:00
Sean McBride
5144b1c7b3 descriptor: Restore implicitly casted-away const 2024-05-28 22:07:45 +02:00
Sean McBride
d795c0b821 descriptor: Defer potentially truncating cast to last minute 2024-05-28 22:07:45 +02:00
Sean McBride
2c32efa20e descriptor: Replace parse_descriptor() function
This function had a few problems:

 - it takes two buffers as parameters but knows nothing about their
length, making it easy to overrun them.

 - callers make unwarranted assumptions about the alignment of
structures that are passed to it (it assumes there's no padding)

 - it has tricky pointer arithmetic and masking

With this new formulation, it's easier to see what's being read/written,
especially the destination. It's now very clear that the destination is
not being overrun because we are simply assigning to struct fields.

Also convert byte swapping macros to inline functions for more type
safety.

References #1460
2024-05-28 22:07:29 +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
Tormod Volden
de1398db35 windows: Downgrade get_guid DeviceInterfaceGUID message to debug level
Ideally it should be a warning only for composite devices but it is
difficult to discern that case. The message is too invasive and
confusing for the non-composite case, so make it debug instead.

Follow-up of commit f9ae36b

Closes #1394

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-05-26 22:31:09 +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
Tormod Volden
42e8a9ff2c linux: ioctl() request number is unsigned long
The type mismatch was caught by building with -Wconversion.

References #1497

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-05-26 16:40:59 +02:00
Tormod Volden
8b507434fa linux: Fix type of open() flags argument
The second argument to open() is an int carrying flags (including
"access modes"). A third, optional argument has type mode_t, carrying
"file mode bits" for file permissions.

Also rename the variable to access_mode to make it more clear.

The type mismatch was caught by building with -Wconversion.

References #1497

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2024-05-26 16:40:29 +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
Francis Hart
fef78a96e3 windows: Restore behaviour of skipping malformed device GUIDs
Previously when getting the device list, a malformed device GUID would
be explicitly ignored and skipped, allowing the operation to complete. A
recent change to winusb_get_device_list() in commit fdab67b accidentally
changed this behaviour, so this scenario instead caused an early exit
with error code LIBUSB_ERROR_NO_MEM.

Closes #1475
2024-05-07 20:54:00 +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
f8a6c412f5 darwin: Cleanup of usbi_backend structure
No real change.

- rearrange elements to be initialized in the same order as they appear
  in the struct declaration

- explicitly set values to NULL so that global searches for things like
  `wrap_sys_device` easily reveal that the function is NULL on Darwin

Closes #1439
2024-04-20 11:39:06 +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
Adrien Destugues
2f2e072ce5 haiku: Use gcc atomic builtins to fix build
Fixes #1387
Closes #1482
2024-04-20 10:10:55 +02:00
Sean McBride
43107c84e4 darwin: Suppress false positive warning with an assert
The clang static analyzer doesn't see that the pointer will always be
non-NULL if the return value is success. Just assert this fact so that
it can see this, and then it won't warn:

Access to field 'can_enumerate' results in a dereference of a null
pointer (loaded from variable 'cached_device')

References #1414
2024-04-04 09:02:32 +02:00
Sean McBride
1c1bad9d12 darwin: Fix clang static analyzer warning about a variable that's never read
Reformulate the loop in a way that the variable is read.

References #1414
2024-04-04 09:02:16 +02:00