1317 Commits

Author SHA1 Message Date
Chris Dickens
6733f242ae Windows: UsbDk: Fix spelling of InterruptTransferType enum
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-06 21:18:13 -08:00
Chris Dickens
798969f5c6 Windows: UsbDk: Fix some strange type-casting
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-06 21:15:39 -08:00
Chris Dickens
4da478cce0 Misc: Add TargetName to getopt VS project files to supress warning
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 23:44:57 -08:00
Chris Dickens
9843b689df Misc: Sort projects alphabetically in VS solution files
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 23:33:09 -08:00
Chris Dickens
8b6639ea6f Misc: Fix references section in fxload VS project files
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 23:32:33 -08:00
Chris Dickens
6dd2b7f9a5 Misc: Fix linker options in getopt VS project files
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 23:32:14 -08:00
Chris Dickens
e25e0ce759 Misc: Add missing files to VS2005 projects for libusb
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 23:21:48 -08:00
Chris Dickens
cbf2cc8847 Misc: Fix minor discrepancies in WinCE MSVC files
Add missing header files, fix preprocessor definitions for static
library, and sort projects and files by name.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 23:19:47 -08:00
Chris Dickens
664c43d9b1 WinCE: Give a variable a more appropriate name, matching other patterns
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 21:55:01 -08:00
Chris Dickens
c2791c3cb3 WinCE: Remove empty function for unsupported ISO transfers
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 21:54:01 -08:00
Chris Dickens
12c3047c63 WinCE: Add back mistakenly removed errno variable
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 21:51:04 -08:00
Chris Dickens
71a779d078 Windows: Rework WinUSB enumeration process to fix issues on Win8+
As they sometimes do, Microsoft made changes to the way in which the
SetupAPI functions list the devices returned by SetupDiGetClassDevs().
In particular, composite devices started returning their interfaces
before the parent device, which caused some issues with the way the
enumeration logic was assigning things. For composite devices, it
appears that the first interface behaves much like the parent device in
some regards, so the library was creating a device specifically for the
first interface and then again when the actual parent device was
encountered. This caused composite devices to appear in the device list
twice, with the first instance being unusable for most operations.

This commit significantly changes the way in which the enumeration
process is done. Previously we would scan for HCDs, hubs, and generic
devices, in that order and in distinct passes (obtaining a new listing
of devices from SetupAPI). Now we will obtain a single snapshot at the
beginning of the enumeration process and iterate through this to scan
for each type of device.

With a single snapshot, we can be assured that the device instance
handle will not change between passes and thus we can use this as the
unique identifier. This completely removes the need to hash the device
instance ID to obtain a unique identifier and simplifies the process.

The previous enumeration process also created "dummy" libusb_device
instances for the HCDs that were never exposed to the user. This has
been removed in favor of identifying which of the encountered hubs are
actually root hubs.

Finally, the query for the port number has been moved to the GENeric
pass at the point where the devices are actually initialized. This query
operation has been relaxed to allow failure, since some virtual USB
devices don't properly implement this query in their drivers.

Closes #215, Closes #251, Closes #257, Closes #288

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 16:37:09 -08:00
Chris Dickens
839235a0b4 Windows: Remove port member from device's private structure
The libusb_device structure already has a port_number member that stores
the same information, so don't duplicate this elsewhere.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 15:52:56 -08:00
Chris Dickens
def374393b Windows: Be a bit smarter when setting composite interfaces
Instead of open-coding the search for the "MI_" substring, use the
strstr() function to find the string and verify that the following two
characters are indeed digits.

Also guard against the possibility of the interface number being larger
than what we can support.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 15:45:07 -08:00
Chris Dickens
bb0a3e8c01 POSIX: Improve threading abstraction as done in f06c4f5b
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 13:47:34 -08:00
Chris Dickens
f06c4f5b37 Windows: Further improve thread abstraction
Adopt typedefs and inline functions to get the benefits of type checking.

Convert all trivial functions to inline and remove return values where
they aren't checked.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-04 13:42:52 -08:00
Chris Dickens
3f69af8676 Windows: Constify variables that aren't supposed to change
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 23:27:33 -08:00
Chris Dickens
c4438b3c7b Windows: Kill empty functions and switch to runtime checking
Instead of requiring each driver API to provide a complete set of
functions, some of which may be simple containers for returning an
unsupported error code, allow function pointers to be NULL and return
an error when a NULL pointer is encountered.

Also remove the unused sub_api parameter from the API init/exit
functions.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 23:27:33 -08:00
Chris Dickens
886e396768 Windows: Remove unused DLL functions and header definitions
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 23:27:32 -08:00
Chris Dickens
af1f17536f Windows: Fix some minor style issues
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 23:27:11 -08:00
Chris Dickens
4be320236e Windows: Improve locking in threading abstraction
Convert the usbi_mutex_t type to a CRITICAL_SECTION object. There
are numerous advantages including lower resource usage and a better
fast-path (doesn't require entering kernel space).

Simplify the condition variable implementation by not associating a
wait structure with a particular thread ID. This is not needed and
causes an unnecessary search through the linked list of any available
wait structures when the real optimization is just reusing an already
created event object.

Also, while here remove all the checks for NULL pointers because we
don't do such a silly thing inside the library.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 23:24:27 -08:00
Chris Dickens
3001f93477 Misc: Ensure that both WINVER and _WIN32_WINNT are defined for Windows builds
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 19:19:39 -08:00
Chris Dickens
c74dce8583 Windows: Improve enumeration process
Prior to this commit, there were some limitations and inefficiencies
during the enmeration process.

First, the maximum number of device interface GUIDs that could be
enumerated was fixed at 64. This limit has been removed and the list
of GUIDs is dynamically resized as new ones are encountered. Logic has
also been added to detect the presence of duplicate GUIDs in order to
speed up the enumeration process.

Next, when searching for device interface GUIDs, only the
"DeviceInterfaceGUIDs" registry key was being consulted. Now we will
also consider "DeviceInterfaceGUID" in order to support devices that
have the GUID listed under this key (such as some WCID devices).

Finally, there used to be a static list of USB PnP enumerator strings
that were used to detect devices during the GENeric enumeration pass. In
many cases, this is wasteful as these enumerators are only present with
very specific hardware. To improve this, we now keep track of the USB
PnP enumerator string encountered as we enumerate the hubs. This allows
the enumeration process to only search for devices that could possibly
be present on the system given the hardware and drivers that were
encountered.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 13:53:56 -08:00
Chris Dickens
8d248ab899 Windows: Kill perpetually growing list of hub driver names
During enumeration, it is unnecessary to match a hub against a list of
known driver names because hubs are enumerated with a specific device
interface GUID. This will eliminate the need for further expansions to
the hub driver list as manufacturers release new drivers.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-03 13:53:25 -08:00
Chris Dickens
6402a10c14 Misc: Add _CRT_SECURE_NO_WARNINGS to testlibusb to suppress warning C4996
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:52:36 -08:00
Chris Dickens
db00b074b2 hotplug: Cast ssize_t to int in error path to avoid compiler warning
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:51:28 -08:00
Sean McBride
96b89f90d3 WinCE: Fixed incorrect check of return value, found by cppcheck
cppcheck was warning:

nullPointerRedundantCheck,libusb/os/wince_usb.c:280,warning,Either the condition '!discdevs' is redundant or there is possible null pointer dereference: discdevs.

In fact, the issue was just checking the wrong thing.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:30 -08:00
Sean McBride
0b3d4c68d8 Fixed various trivial cppcheck 1.80 warnings
Specifically:

redundantAssignment,examples/dpfp.c:422,style,Variable 'r' is reassigned a value before the old one has been used.
redundantAssignment,libusb/os/threads_posix.c:64,style,Variable 'ret' is reassigned a value before the old one has been used.
unreadVariable,libusb/os/netbsd_usb.c:217,style,Variable 'hpriv' is assigned a value that is never used.
unreadVariable,libusb/os/netbsd_usb.c:235,style,Variable 'hpriv' is assigned a value that is never used.
unreadVariable,libusb/os/openbsd_usb.c:251,style,Variable 'hpriv' is assigned a value that is never used.
unreadVariable,libusb/os/openbsd_usb.c:275,style,Variable 'hpriv' is assigned a value that is never used.
unsignedLessThanZero,libusb/os/windows_winusb.c:259,style,Checking if unsigned variable '_index' is less than zero.
unsignedLessThanZero,libusb/os/windows_winusb.c:298,style,Checking if unsigned variable '_index' is less than zero.
unsignedLessThanZero,libusb/os/windows_winusb.c:367,style,Checking if unsigned variable '_index' is less than zero.
invalidPrintfArgType_sint,examples/xusb.c:534,warning,%d in format string (no. 1) requires 'int' but the argument type is 'unsigned int'.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:30 -08:00
Sean McBride
c1d8c8d654 Removed accidental double semi colons at end of line
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:30 -08:00
Sean McBride
7338c2d347 core: Fixed deprecated attribute to work on clang
clang was warning:

core.c:2011:5: Declaration is marked with '\deprecated' command but
does not have a deprecation attribute

This was because LIBUSB_DEPRECATED_FOR was checking for gcc >= 4.5
whereas clang identifies itself as gcc 4.2. So fallback to
__attribute__((deprecated)) without a message string on older GCCs
(and thus clang).

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:30 -08:00
Sean McBride
f66c63e9f0 examples: xusb: replaced insecure sprintf with snprintf
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:30 -08:00
Sean McBride
830a9cb27d Fix unused parameter warnings
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:30 -08:00
Sean McBride
8e1a282583 examples: ezusb: Renamed header guard variable to not use reserved identifier
Fixes clang -Wreserved-id-macro warning.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 22:40:26 -08:00
Chris Dickens
4d5df70911 linux_usbfs: libusb_init() should succeed if no devices are present
When using sysfs to scan for devices, libusb_init() will fail if there
are no USB devices present. There is no reason for this behavior, so
this commit modifies the logic to only return an error if one or more
devices are present but none could be successfully enumerated.

Closes #301

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-28 00:40:14 -08:00
Chris Dickens
3dca962104 hotplug: Improve internal structure and deregistration mechanism
This commit shrinks the size of the internal hotplug callback structure
by removing unused fields, using the correctly sized types for matching
fields, and adding a new flags field whose bits control how the callback
structure should behave.

The hotplug callback handle ID counter has also been moved to the
context structure instead of being a global variable shared amongst all
contexts. This lets each context independently manage handle IDs and use
the maximum range of possible IDs.

Finally, the hotplug callback deregistration mechanism has been improved
to signal to the event handler that an explicit deregistration needs to
be handled. This removes the need to send a dummy hotplug message, which
was using an invalid libusb_hotplug_event value anyway that was causing
some compilers to complain.

Closes #373

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-27 23:30:16 -08:00
Chris Dickens
539a3230cf Misc: Fix testlibusb build on VS2013 and earlier and suppress warnings
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 23:58:00 -08:00
Chris Dickens
8aad7fd1de linux_usbfs: Improve isochronous transfer submission and error reporting
The Linux kernel has changed the maximum allowed packet length per
isochronous packet numerous times, which can create difficulties in
trying to report appropriate errors back to the user when submitting too
large of a packet on older kernels.

In an attempt to improve this situation, this commit adds logic that
will use different per-packet limits based on the detected kernel
version. Additionally, the logic has been improved to split URBs based
on the number of isochronous packets per URB, which is currently (and
has been forever) limited to 128.

Finally, the error reporting during URB submission has been improved to
catch and report errors relating to the transfer length being too large.

Closes #118

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 19:15:42 -08:00
Chris Dickens
2ae2348c66 Windows: Add VS 2010/2012/2013/2017 project files for testlibusb
Inspired by PR #201 (commit fa19c152), this commit adds the same project
files for the other supported versions of Visual Studio.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 17:25:59 -08:00
Anil Nair
fa19c15237 Windows: testlibusb example for VS2015
Closes #201

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 17:14:32 -08:00
Chris Dickens
0fece89591 Examples: Misc. cleanup to xusb
Make data that is unchanged const, remove an unused return value, and
add a missing newline to an error message.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 16:37:34 -08:00
Chris Dickens
d8c1f7caa9 Examples: Improvements to xusb's support for Microsoft OS descriptors
As noted in issue #269, the current xusb implementation does not handle
Microsoft OS descriptors from devices whose vendor code is greater than
0x7F. This commit addresses this limitation by using
libusb_get_string_descriptor() instead of the ASCII variant and parsing
the descriptor separately. Note that this issue was addressed in PR #276,
but that approach was too cryptic to read.

Closes #269, Closes #276

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 16:37:22 -08:00
Chris Dickens
ba86d27dca Misc: Fix pre-commit hook to not emit errors in non-Bash shells
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 15:46:13 -08:00
Joshua
1bd541e45d Solaris: Add detach/attach kernel driver support
CD: Add missing error checking/reporting and fix some style issues

Closes #236

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-26 15:29:48 -08:00
chunyu.xie
9d4a6eae03 Windows: Report correct usagePage and usage values for HID
Closes #278, Closes #281

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-22 12:52:59 -08:00
chunyu.xie
61dc33a525 Windows: Fix libusb_get_string_descriptor for HID devices
When querying vendor-defined string descriptors, use GetIndexedString
of the HID library.

Closes #279, Closes #280

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-22 12:52:44 -08:00
Victor Toso
21540e494f linux_usbfs: Remove old comment
This comment was introduced in 737ba04ea4 together with
a few locking calls usbi_mutex_lock() which were removed by
a886bb02c8.

Signed-off-by: Victor Toso <victortoso@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-22 11:47:02 -08:00
Victor Toso
e5ec22e6ad linux_udev: Initialize pointer before calling external API
udev_list_entry_foreach() sets the entry variable for each device but
initializing the pointer to NULL to avoid warnings.

Found by Coverity:

libusb-1.0.21/libusb/os/linux_udev.c:273:
error[uninitvar]: Uninitialized variable: entry
>   271|    devices = udev_enumerate_get_list_entry(enumerator);
>   272|
>   273|->  udev_list_entry_foreach(entry, devices) {
>   274|        const char *path = udev_list_entry_get_name(entry);
>   275|        uint8_t busnum = 0, devaddr = 0;

Signed-off-by: Victor Toso <victortoso@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-22 11:46:42 -08:00
Jakub Klama
bb172b4356 linux_usbfs: Set urb->buffer_length for isochronous transfers
Linux 4.13 expects buffer_length to match sum of iso descriptor lengths.

Closes #365

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-22 11:37:52 -08:00
Roman Kalashnikov
4d9fc62643 linux_usbfs: Fixed excessive check
Closes #358

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-12-22 11:34:12 -08:00
Chris Dickens
b4c9b4272d examples: Fix device handle leak in xusb
The CALL_CHECK macro returns from the current function, so in the
test_device() function the device handle was being leaked when one of
the functions failed. This commit adds a new CALL_CHECK_CLOSE macro that
does the same as CALL_CHECK but also closes the device handle before
returning. In addition, the macros are changed to declare their needed
variable rather than relying on the variable to already exist within the
scope of the function.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2017-08-24 15:52:39 -07:00