Commit Graph

64 Commits

Author SHA1 Message Date
Ludovic Rousseau f808275822 Detach from the kernel only if needed
We first check that the HID device is managed by the kernel before
calling libusb_detach_kernel_driver(). This will avoid side effects of
libusb_detach_kernel_driver() like also doing a
libusb_release_interface()

Ludovic's original patch modified by Alan Ott <alan@signal11.us>
2011-02-08 09:38:46 -05:00
Alan Ott 3290860713 Change interface field to interface_number in hid_device_info struct.
Change field named interface to interface_number because Visual Studio uses
interface as a keyword for managed C++.
2011-01-18 23:00:59 -05:00
Alan Ott a93cdfbeac Add the interface number to hid_device_info on Linux/libusb implementation.
The interface number is added to the hid_device_info struct. Since the
Linux/libusb implementation can't put the Usage and Usage Page in the
hid_device_info struct because it requires detaching the kernel driver, the
interface number is added instead so that users with composite HID devices
can differentiate between the interfaces on the device.
2011-01-18 22:52:06 -05:00
Alan Ott a0f8bec509 Remove the Usage and Usage Page code from the libusb/Linux implementation.
This is removed because it requres detaching the kernel driver for the
device.  This is too invasive to be done on a call to hid_enumerate().
2011-01-18 22:46:54 -05:00
Bastaan Veelo ba51d59a4e hid_device_info with bcdDevice release number. 2011-01-18 18:38:35 -05:00
Alan Ott 1cd43e9d30 Fixed a bug I introduced when cleaning up. 2011-01-15 01:26:40 -05:00
Alan Ott 993919fa8d Initial stab at support for composite devices. To do this, 3 tings
had to happen:
1. Create a hid_device_info struct for each interface which
   reports to be HID, rather than one for each device.
2. Parse and report the Usage Page and Usage for each.
2011-01-14 18:51:54 -05:00
Alan Ott c3c0c9062a Took out register_error() to prevent warning, but marked it with TODO. 2011-01-07 10:23:40 -05:00
Ludovic Rousseau 49122c26d5 Append instead of overwrite CFLAGS
The user may have a custom CFLAGS with debug flags like -Wextra. These
flags should be used instead of overwritten
2011-01-07 10:17:39 -05:00
Ludovic Rousseau 02c55187a7 Use explicit file names
*.o does not match ../hidtest/hidtest.o so we must use an explicit list
of .o file names
2011-01-07 10:17:30 -05:00
Ludovic Rousseau 81382f21d8 Make "make clean" idempotent
Do not fail if the files to remove do not exist
2011-01-07 10:17:24 -05:00
Alan Ott 5e594ebc95 Ensure that -1 is returned on Linux after a device has been disconnected (unplugged). 2010-12-28 11:23:08 -05:00
Alan Ott 486fe65d94 skip is defaulted to 1. There is no need to re-set it to 1. 2010-12-22 00:08:39 -05:00
Alan Ott ec07bca7a9 When devices are unplugged, libusb devices will fail (and return failure).
More of these cases are now checked for.
2010-12-22 00:04:35 -05:00
Alan Ott afc41b7b1d Moved the cleanup of libusb transfer objects to hid_close(), and out of the
read_thread().  This way the sequence of disconnecting the device and then
calling hid_close() will not cause a crash.
2010-12-21 13:24:26 -05:00
Ludovic Rousseau 83483325bb Do not use printf() unconditionally
The problem is that the file descriptor 1 used by printf() may not be
stdout but somethink else. For example a daemon may close fds 0, 1 and 2
and reuse them to open files or sockets. So using printf() may send data
on a file or socket opened by the application.

Errors will now be logged to stderr only if DEBUG_PRINTF is defined.
2010-11-29 12:28:28 -05:00
Ludovic Rousseau f9c06d35b2 Check libusb_get_active_config_descriptor return value
Avoids a crash if libusb_get_active_config_descriptor() returns with an
error code.
2010-11-21 16:18:41 -05:00
Ludovic Rousseau 025d81a5a3 use pkg-config to find udev library 2010-11-21 16:18:09 -05:00
Ludovic Rousseau b91198473f Remove 2 useless lines
intf pointer value is reset at each step of the for() loop. So no need
to increment it at the end of the loop.
2010-11-16 23:22:06 -05:00
Alan Ott dbc0ee08de Changed the logic a bit to make it more concise. 2010-11-16 23:18:37 -05:00
Ludovic Rousseau 9fac48e44b Wait for cancellation to complete
read_thread(): if a transfer was in progress then
libusb_cancel_transfer() will return with no error, then we must wait to
the transfer cancellation to complete. Otherwise the transfer will/may
complete after the resources have been freed and we get a crash.
2010-11-16 23:12:09 -05:00
Alan Ott 2b5e896370 Check for NULL before calling memcpy().
Although it works without problem, it is technically illegal to call memcpy()
with a destination of NULL, even if size is zero.

Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
2010-11-16 22:49:04 -05:00
Alan Ott 67e61de7c9 Release the claimed interface when the handle is closed.
This code came from Ludovic Rosseau (as part of another patch).

Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
2010-10-23 13:37:32 -04:00
Alan Ott ebadfd6d5b Re-activated the cleanup code in read_thread(). For some unknown reason in
the past, I thought it would be a good idea to move this to hid_close(), but
I can't now understand why, as freeing these objects in read_thread() makes
sense because they are created in read_thread().

This code never made it into hid_close(), so it's now re-activated in
read_thread().

Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
2010-10-23 13:27:44 -04:00
Alan Ott 3a3ac5e084 This eliminates a race condition on hid_close(). Previous to this patch,
hid_close() would hang on pthread_join() if hid_close() was called while the
reading thread was executing read_callback() _before_ the call to
libusb_submit_transfer(). This caused libusb_cancel_transfer() to fail, and
the read thread would never stop (and hid_close() would never return).

This patch simply sets the shutdown_thread flag from read_close(). This way,
even if hid_close() is called while there is no transfer pending, the loop
in read_thread() will exit, causing read_thread() to end. For this to not
leak resources, read_thread() also now has to call libusb_cancel_transfer().
Note that necessarily, one of the calls to libusb_submit_transfer() will
fail (and return an error code), but that's ok, because one of them is
guaranteed to succeed.

Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
2010-10-23 12:53:27 -04:00
Alan Ott 0d7ed387b0 Changed the default back to blocking. This got inadvertently changed.
Reported-by: Ludovic Rousseau <ludovic.rousseau@gmail.com>
2010-10-21 09:22:17 -04:00
Alan Ott ad65e15c1c Added a pthread_barrier to ensure that the read_thread() function (thread)
has been initialized before hid_open_path() returns. There was a race
condition if hid_close() was called immediately after hid_open(), because
hid_close() was trying to destroy objects that read_thread() had sometimes
not created yet.

Reported-by: Ludovic Rousseau <rousseau@debian.org>
2010-10-13 10:20:19 -04:00
Ludovic Rousseau b8ceae83b8 Do not ovewrite CFLAGS
Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 22:07:13 -04:00
Ludovic Rousseau fcec7ca07f Fix warning: ordered comparison of pointer with integer zero
iconv_open() returns a iconv_t so is always a positive value.

In case of error the returned value is (iconv_t)-1 so compare with this
specific value instead of < 0

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 22:05:17 -04:00
Ludovic Rousseau 218d146c37 Fix warning: comparison of unsigned expression < 0 is always false
iconv() returns a size_t so is always a positive value.

In case of error the returned value is (size_t)-1 so compare with this
specific value instead of < 0

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 22:04:32 -04:00
Ludovic Rousseau 8c80bbd8b7 Fix warning: declaration of ‘index’ shadows a global declaration
hid-libusb.c:172: warning: declaration of ‘index’ shadows a global declaration
/usr/include/string.h:487: warning: shadowed declaration is here

Use idx instead of index to avoid collision with index()

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 22:03:40 -04:00
Ludovic Rousseau 547a864690 Fix a buffer overflow
hid-libusb.c:223: warning: array subscript is above array bounds

sizeof(wbuf) gives the size of the wbuf array in char. It is also the
number of element only if wbuf is an array of char.

Here wbuf is an array of wchar_t so the last element of the array is
wbuf[sizeof(wbuf)/sizeof(wbuf[0])-1]

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 22:02:36 -04:00
Ludovic Rousseau 8c63a6d474 Fix warning: declaration of ‘i’ shadows a previous local
Use a "d" variable to index the devices instead of the generic "i"

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 19:57:05 -04:00
Ludovic Rousseau 095159d825 Fix warning: no previous prototype for ‘new_hid_device’
Local functions should be declared as static

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 19:55:23 -04:00
Ludovic Rousseau bfcaf8f2d3 Fix compiler warning function declaration isn’t a prototype
Functions with no arguments should use void

hid-libusb.c:93: warning: function declaration isn’t a prototype
hid-libusb.c:97: warning: function declaration isn’t a prototype
hid-libusb.c:1010: warning: function declaration isn’t a prototype

Signed-off-by: Alan Ott <alan@signal11.us>
2010-10-08 19:53:57 -04:00
Alan Ott a4b5d7bb1d Remove old build file which shouldn't be in here anymore. The Makefile is the right way to build the project. 2010-09-20 09:18:36 -04:00
Alan Ott a039425436 Put extern "C" around the functions. 2010-09-16 21:44:33 -04:00
Alan Ott 381a170d10 Return non-error for the nonblocking function. 2010-09-13 15:00:34 -04:00
Alan Ott 09cb41aada Additional licenses. HIDAPI is now GPLv3, BSD, or the original license,
at the discretion of the user. This should provide maximum flexibility and
freedom.
2010-09-13 11:15:24 -04:00
Alan Ott defb938797 Limit the receive queue to 30 reports on Linux. 2010-08-23 13:14:34 -04:00
Alan Ott 14dd2eeba4 Took out printf()s. 2010-08-21 14:03:52 -04:00
Alan Ott e0c8032b7b Updated README for Linux. 2010-08-21 14:01:04 -04:00
Alan Ott 17c4c9251c Changed hid_error() to return const. 2010-08-21 13:22:03 -04:00
Alan Ott 4214841ae3 Added the get_*_string() functions for the libusb implementation. 2010-08-21 13:09:23 -04:00
Alan Ott 0c07642ab5 Support for getting USB Strings using the applications locale.
If the locale's language is not supported by the device, it defaults to
the first language available on the device.
2010-08-19 22:53:53 -04:00
Alan Ott 2dedc16309 Fixed get_usb_string() to use the first language the device says it supports. 2010-08-18 21:54:45 -04:00
Alan Ott 28ab1e6ac1 Changed to use pkg-config 2010-08-17 22:50:31 -04:00
Alan Ott 32d785dbfe Changed to use pkg-config for libusb and fixed the position of libusb_close() in hid_enumerate(). 2010-08-17 22:13:54 -04:00
Alan Ott 35a8d0aecc Made the input loop only try to read endpoint->wMaxPacketSize. This way, messages don't get queued up, and are reported immediately. 2010-08-16 23:49:43 -04:00
Alan Ott 89d84b1e02 Don't send the report number if it's not needed for feature and output reports. 2010-08-16 23:32:20 -04:00