Commit Graph

59 Commits

Author SHA1 Message Date
Ihor Dutchak 4e63d6dfc3 Introduce HID_API_MAX_REPORT_DESCRIPTOR_SIZE (#452)
- first step is to use HID_API_MAX_REPORT_DESCRIPTOR_SIZE internally;
- port a few improvements from get-descriptor branch early;

Resolves: #384
2022-09-15 13:05:56 +03:00
Dimitriy Ryazantcev 0f2cf886e5 RFC: Add HID bus type in hid_device_info (#308) 2022-09-10 19:18:09 +03:00
Ihor Dutchak ea38f6538f libusb: fix HID Report descriptor request (#444)
- pass the actual HID Report descriptor size (from HID descriptor);
- interface_num has to be wIndex, and not as part of wValue for LIBUSB_DT_REPORT request;
2022-08-16 16:29:10 +03:00
Ihor Dutchak 779346241b libusb: fix enumerating device path w/o root access (#442) 2022-08-15 12:47:27 +03:00
Ihor Dutchak 597160da3c Reattach kernel driver when failed to claim interface (#441)
Potential execution path may happen if kernel driver successfully detached,
but libusb failed to claim the requested interface.

Otherwise the device remains with detached kernel driver.
2022-08-14 12:31:59 +03:00
Julian Waller 5c9f147a07 Add hid_get_device_info (#432)
- new API function: `struct hid_device_info * hid_get_device_info(hid_device *dev);` to get `hid_device_info` after the device already opened;
- reused existing implementation on Windows and macOS from enumeration routines to have the implementation;
- refactored libusb implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;
- refactored hidraw implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;

Resolves: #431
Closes: #164
Closes: #163
2022-08-13 19:01:57 +03:00
Ihor Dutchak 81dd62dd10 Ensure Iconv is found when provided via CFLAGS/LDFLAGS (#430)
- by default find_file/find_library doesn't respect CFLAGS/LDFLAGS, and FindIconv fails to find Iconv;
- by explicitly trying to link against `-liconv` - we're checking if library is available in such way;
- additionally: if Iconv is detected as BUILT_IN, no need to explicitly depend on `Iconv::Iconv`;
2022-06-22 01:30:45 +03:00
Ihor Dutchak c95795ab5c Fix hidtest build
- fix including platform-specific includes when HIDAPI is a CMake subdirectory;
- check libusb and winapi platform-specific headers with hidtest;
2022-05-05 14:53:17 +03:00
Ihor Dutchak cd95af8d70 libusb: cmake: Proper dependency on Iconv (#405)
- explicitly add Iconv as a dependent library;
- check if iconv requires pointer-to-const as input (introduce ICONV_CONST check);
- NetBSD CI (has external Iconv library implementation that uses all of the above);
2022-05-02 14:18:03 +03:00
SNEHAL 536bad201e libusb: Fix double free of buffer pointer in hid_close() (#395)
Make buffer pointer NULL, once it is freed.

In hid_close() buffer pointer is freed and next to that the libusb_free_transfer function is called which checks for if the LIBUSB_TRANSFER_FREE_BUFFER flag is set and the buffer pointer is not NULL. when this condition evaluates true, it
tries to free memory for a buffer which is already free and a crash occurs.

Even though the described behavior should not happen as per libusb documentation, it has been observed with some version(s) of libusb.
2022-04-23 15:54:26 +03:00
Ihor Dutchak bca404520f Fire v0.12.0 (#398)
- add convenient macros to check HIDAPI version in runtime;
- mark in which version all recent HIAPI API functions where added;
2022-04-23 15:50:41 +03:00
Ihor Dutchak 69dcdc1ff0 Update copyright strings 2022-01-29 18:33:15 +02:00
Ihor Dutchak f0a4a1fa2e Fix potential crash when libusb_detach_kernel_driver fails (#363)
In case if libusb_detach_kernel_driver fails inside hidapi_initialize_device
HIDAPI had tried to `libusb_close(dev->device_handle)` two times:
- right after `libusb_detach_kernel_driver`;
- outside of `hidapi_initialize_device` function;

The fix: `libusb_close` the device only once.
And since `hidapi_initialize_device` is not responsible for opening the device -
it is not responsible for closing it either.
2022-01-03 14:16:37 +02:00
Ihor Dutchak 0ec60c03cb Add <root>/libusb to includes for in-tree build
Otherwise it is impossible to `#include <hidapi_libusb.h>`,
when HIDAPI is added directly as a subdirectory of a CMake project.
2021-09-27 13:08:20 +03:00
Ihor Dutchak aaf5f3d172 add libusb-specific hid_libusb_wrap_sys_device
Rationale: on Android one must use UsbManager, to access any
USB device. As a result, libraries like libusb can only use file descriptors
that are provided by UsbManager.
libusb has an API to use such file descriptors: hid_libusb_wrap_sys_device.
Having hid_libusb_wrap_sys_device currently is the only way to make hidapi
work on Android without root access and without custom Android builds.

Relevant info: https://github.com/libusb/libusb/pull/830/files
2021-09-18 18:56:56 +03:00
bearsh 03998f4418 libusb backend: stabilize device path (#291)
Format the path like the linux kernel does:
<busnum>-<port[.port]>…:<config num>.<interface num>

Closes: #117
2021-07-03 22:06:31 +03:00
Ihor Dutchak f2bb972e7f add CMake build system 2021-07-03 20:38:58 +03:00
Erik OShaughnessy 7620bc7faa Guard against zero length buffers in hid_write (#279) 2021-06-14 04:09:13 +03:00
Ihor Dutchak c7a2339b05 libusb: add support for potential Android JNI backend 2021-05-18 16:39:01 +03:00
Ihor Dutchak f6d0073fcd hidraw/libusb: fix -Wall -Wextra -pedantic -Werror compilation (#214)
- minor code-style fixes;
2020-11-24 13:39:27 +02:00
Ihor Dutchak d2c3a9862e libusb: fix race condition on device close (#189)
Read callback may fire itself on its own even after its been
requested to stop and exactly before the calling code
waits for its completion in indefinite loop.

Explicitly preventing re-fireing the submission loop,
fixes the issue.

Fixes: #142.

Signed-off-by: Ihor Dutchak <ihor.youw@gmail.com>
2020-10-14 18:43:26 +03:00
Ihor Dutchak 8f72236099 add hid_version/hid_version_str API (#192)
- API functions to get runtime version of the library;
- macros to get static/compile-time version of the library;
- VERSION file;
2020-10-14 18:41:08 +03:00
CodeforEvolution f2611e0237 Changes to Build System for Haiku
Co-authored-by: @CodeforEvolution
Co-authored-by: Luca Weiss <luca@z3ntu.xyz>
Co-authored-by: Begasus <begasus@gmail.com>
Co-authored-by: Panagiotis Vasilopoulos <hello@alwayslivid.com>
2020-02-12 17:00:07 +02:00
wet34t 533dd9229a libusb: allow hidapi to be configured without using iconv 2020-02-09 15:22:22 +02:00
Fredrik Fornwall 90ddfc61b7 Fix build when targetting Android 7 or later
Support for pthread_barrier was introduced in Android 7, so trying to
build a custom implementation of that clashes with the system headers.
2020-01-22 14:05:08 +02:00
Luca Weiss 51bdec7844 Fix manual Makefiles and add CI for them (#131) 2020-01-12 04:09:48 +02:00
Daniel Van Noord 083223e779 Added support for Get Input Report (#59)
- Get Input Report supported on linux/libusb, mac and windows;
- linux/hidraw doesn't support it on kernel level;
2019-11-15 13:55:51 +02:00
Mayank Suman fa75ffb743 Reattach kernel driver in hidapi-libusb 2019-10-31 12:56:00 +02:00
Andrew Eikum 93dca80762 Update more repository URLs 2019-07-25 07:57:59 +02:00
Luca Weiss b0ec6f0918 libusb: Return string in hid_error() 2019-07-15 13:15:46 +03:00
luz.paz f01c4ede50 Misc. typo fixes
Found via `codespell -q 3`
2019-07-14 13:54:55 +02:00
Luca Weiss 95adae423d Fix compile warning on FreeBSD
hid.c:458:18: warning: passing 'const char **' to parameter of type 'char **' discards qualifiers in nested pointer types
      [-Wincompatible-pointer-types-discards-qualifiers]
        res = iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
                        ^~~~~~
/usr/local/include/iconv.h:85:43: note: passing argument to parameter 'inbuf' here
extern size_t iconv (iconv_t cd,  char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft);
                                          ^
1 warning generated.
2019-07-13 22:32:10 +03:00
iakov c022f55080 Remove excess incorrect space
Co-Authored-By: Filip Kubicz <Qbicz@users.noreply.github.com>
2019-06-24 22:55:27 +02:00
Iakov Kirilenko 73b9ccb996 Explicit cast from void* to corresponding type
Fix compilation without -fpermissive
2019-06-24 22:55:27 +02:00
Luca Weiss 2f7a7c9948 Rename hid_device* parameters to dev
Previously, the header file used hid_device *device and the
implementations used hid_device *dev. Make this consistent.
2019-06-17 11:32:48 +02:00
Diego Herranz a6a622ffb6 all: Fix a few typos 2016-09-19 23:40:12 -04:00
Tobias Rehbein 0cbc3a409b libusb: Adapt to FreeBSD 10
FreeBSD 10 has libusb_get_string_descriptor() in base libusb, so don't
define it here.
2014-07-23 16:50:14 -04:00
Pavol Rusnak 417b2bf081 libusb: Add Android support
This code originally came from Pekka Nikander <pekka.nikander@senseg.com>
2014-07-23 16:40:51 -04:00
Michael Hanselmann ac6120b589 libusb: Fix null pointer dereference in hid_open()
Make sure hid_device_info.serial_number is not null before passing it to
wcscmp().

When libusb can't open a device for any reason (e.g. file system
permissions), hid_device_info.serial_number is NULL. Make sure
hid_device_info.serial_number is not null before passing it to wcscmp(3).

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
2014-07-22 17:38:16 -04:00
Scott Talbert 6a4743ee59 libusb: Include PTHREAD_CFLAGS when linking libhidapi-libusb
m4/ax_pthread.m4 says that PTHREAD_CFLAGS needs to be provided to the
linker line as well as the compile line.  This enables static library
builds of hidapi.
2014-07-22 16:39:30 -04:00
Scott Talbert 54db8ca208 libusb: Add support for compiling on Debian/kFreeBSD 2014-07-22 16:29:52 -04:00
Mario Kleiner 996dafd2a8 libusb: Fix 'dev' memory leak on hid_init() failure.
Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
2013-10-06 17:36:27 -04:00
Bei Zhang 2e27f98b3e all: Fix license name
"GNU Public License" does not exist. It is called "GNU General Public
License", "GNU GPL" or simply "GPL".
2013-10-06 17:33:44 -04:00
Hans de Goede 02a882c890 libusb: Properly wait for transfer cancellation on read_thread() exit
While triaging libusb bugs, I took an indepth look at:
https://github.com/libusbx/libusbx/issues/25

This has lead me to the conclusion that there are 2 issues with hidapi's
libusb code wrt waiting for the transfer cancellation on read_thread()
exit:

1) There is a race where hid_close() can successfully cancel the transfer
after a read_callback() has submitted it but before read_thread() checks
shutdown_thread.  If this race is hit, then the libusb_cancel_transfer()
in read_thread() will fail, causing read_thread() to not call
libusb_handle_events() to complete the cancelled transfer.  hid_close()
will then free the transfer, and if later on libusb_handle_events() gets
called on the same context, it will try to complete the now freed
transfer.  This is what I believe leads to the segfault described in
https://github.com/libusbx/libusbx/issues/25

2) hidapi uses one read_thread() per hid_device, so if there are multiple
hid_devices then there are multiple threads calling
libusb_handle_events(), in this case there is no guarantee that a single
libusb_handle_events() call will successfully lead to the cancelled
transfer being completed.  If the transfer completion is already handled
by another read_thread() and there are no other events, then the
libusb_handle_events() call will hang, and thus the pthread_join() and
thus hidapi_close() will hang.

As number 2 is a generic problem found in more libusb apps, libusb has
gotten a new API called libusb_handle_events_completed(), which takes an
extra pointer to an int, whose contents must be set to nonzero on
completion by the callback, which allows waiting for the completion of a
specific transfer in a race-free manner.

This patch switches the waiting for the transfer's final completion to
using libusb_handle_events_completed(), thereby fixing both issues.  Note
the while is necessary since libusb_handle_events_completed(), like
libusb_handle_events(), will return as soon as it has handled *any* event.
The difference with libusb_handle_events_completed() is that once it has
all the necessary internal libusb locks, it checks the contents of the
completed parameter, and will bail if that has become nonzero without
waiting for further events.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-09-08 21:50:36 -04:00
Spencer Oliver a991328721 linux/libusb: do not check bDeviceClass on enumeration
Some composite devices do not have bDeviceClass set to
LIBUSB_CLASS_PER_INTERFACE and were ignored when enumerating HID devices.

Ignore checking the bDeviceClass as we will be checking interface
bInterfaceClass later anyway.

Signed-off-by: Spencer Oliver <spen@spen-soft.co.uk>
2013-01-21 12:04:03 -05:00
Atsushi NAGASE ba5612e37f libusb: check for failure of hid_init()
Return NULL from hid_enumerate() and hid_open_path() if hid_init() fails.
2013-01-21 11:57:51 -05:00
Ludovic Rousseau 0d7c7ef066 libusb: remove extra spaces
Remove spaces and tabs at end of lines.
2012-11-28 10:33:43 -05:00
Ludovic Rousseau ec24f93100 all: make hid_enumerate() able to match any vendor or any product
Passing product_id=0 will match any product of a given vendor.  This patch
makes it also possible to use vendor_id=0 to match any vendor for a given
product id.

Windows code added to Ludovic's patch by Alan Ott.
2012-11-27 22:12:35 -05:00
Justin R. Cutler b0e2fb8d35 libusb/mac: gcc -pedantic warning fixes 2012-11-26 20:20:13 -05:00
Justin R. Cutler 69a91a8f6c All: Convert C99 comments (//) into C89 comments (/* */) 2012-11-26 18:49:51 -05:00