Commit Graph

18 Commits

Author SHA1 Message Date
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
Justin R. Cutler 3bfaffbfe0 linux: fix gcc warnings when INVASIVE_GET_USAGE is defined. 2012-11-26 15:29:01 -05:00
Alex Dupre 645a0c357a FreeBSD: Conditionally remove calls to libusb_detach_kernel_driver()
libusb_detach_kernel_driver() is not needed on FreeBSD.
2012-07-16 00:52:43 -04:00
Alex Dupre 49021efd0f FreeBSD: Add $(LIBS) to libhidapi.so target (Makefile.freebsd) 2012-07-16 00:52:17 -04:00
Alan Ott 6d50f8fa0f Add Autotools build system (automake, autconf)
This adds the familiar autotools build system and associated documentation
for Linux, FreeBSD, Mac, MinGW, and Cygwin.  The old Makefiles have been
kept, and where appropriate have been renamed Makefile-manual.

Thanks to Peter Stuge, Ludovic Rousseau, Xiaofan Chen, Alex Dupre, and
Segher Boessenkool for providing testing, review, and suggestions, and to
Ludovic Rousseau for providing patches which contributed to this commit.
2012-07-16 00:43:04 -04:00
Alan Ott 38a7bf75f0 Renamed manual makefiles so they're out of the way of autotools
Files named Makefile which will conflict with autotools have been renamed to
Makefile-manual.
2012-07-16 00:43:04 -04:00
Sean M. Pappalardo 308ab89a26 Libusb: Prevent bailing out of read_thread() on non-critical errors 2012-05-18 13:12:28 -04:00
Alan Ott bdb87db5b8 Libusb: testgui: Explicitly link to libpthread.
Reported by: Xiaofan Chen <xiaofanc@gmail.com>
2012-05-03 22:25:40 -04:00
Alex Dupre 498fbb42f9 Libusb: Cleanup
Remove unnecessary initialization in new_hid_device()
Remove unnecessary NULL-termination in usb_get_string()
2012-05-02 18:55:37 -04:00
Alan Ott 74440e2dbb FreeBSD: Add FreeBSD implementation.
This adds a FreeBSD implementation to HIDAPI using the libusb back-end. The
libusb/ folder now contains the libusb implementation and associated
Makefiles for Linux and FreeBSD. All libusb code has been removed from the
linux/ folder, so that the code in libusb/ can be shared by both (and
future) platforms.

This commit renames linux/hid-libusb.c to libusb/hid.c. Make sure to
use git log --follow to see full history of that file.

This code was written by:
 Alex Dupre <ale@FreeBSD.org>
 Alan Ott <alan@signal11.us>
2012-05-02 18:55:28 -04:00