summaryrefslogtreecommitdiff
path: root/libusb/os
Commit message (Collapse)AuthorAgeFilesLines
* linux_usbfs: Parse config descriptors during device initializationChris Dickens2020-04-291-119/+144
| | | | | | | Do the work ahead of time and cache the results so that fetching config descriptors becomes a trivial operation. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Narrow the types passed to certain backend functionsChris Dickens2020-04-2815-205/+206
| | | | | | | | | Backend functions dealing with interfaces and alternate settings should use a type whose range represents that of valid values for interfaces and alternate settings. Switch to use uint8_t instead of int so that backends do not have to cast values or do range checks. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* sunos: Fix a number of compiler warningsChris Dickens2020-04-281-45/+43
| | | | | | | | | | * [-Wdiscarded-qualifiers] assignment discards 'const' qualifier from pointer target type * [-Wpointer-sign] pointer targets in passing argument N of 'func' differ in signedness * [-Wsign-compare] comparision between signed and unsigned integer expressions * [-Wunused-function] 'func' declared 'static' but never defined * [-Wunused-parameter] unused parameter 'p' Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Fix some trivial compiler warnings for the Haiku and BSD backendsChris Dickens2020-04-284-19/+28
| | | | | | | | | | | * [-Wformat=] format 'S' expects argument of type 'T1', but argument N has type 'T2' * [-Wmissing-declarations] no previous declaration for 'func' * [-Wreorder] 'Class::Member' will be initialized after * [-Wsign-compare] comparison between signed and unsigned integer expressions * [-Wunused-but-set-variable] variable 'v' set but not used * [-Wunused-parameter] unused parameter 'p' Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Kill backend get_device_descriptor() functionChris Dickens2020-04-2814-170/+77
| | | | | | | | | | | | | | | | | Simplify the library by moving device descriptor initialization to the backend, while the device is being set up. This removes the duplication of essentially the same code in every backend. Add some missing calls to libusb_le16_to_cpu() when reading multi-byte fields from the "raw" device descriptor. It has worked thus far because the platforms not using the calls happen to be the same endianness as the USB bus. While here, throw in some static assertions to ensure there is no mismatch between the libusb device descriptor structure and any device descriptor structure provided by the platform headers. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Fix some minor inconsistencies in API and codingChris Dickens2020-04-275-22/+22
| | | | | | | | | | | | | | | | | | | | | All of the API functions should take the typedef'ed versions of the opaque libusb structures, but some recent additions to the API did not follow this convention. Fix this by making the changes to the declarations and definitions of the functions. Make the placement of the asterisk in pointer variable declarations consistent (always with the variable name, not the type). Remove some unnecessary casts and initializations relating to dynamically allocated memory. While at it, make casts within the core library consistent in style with no space after the closing parenthesis of the cast. Most of the core already used this style. When using the 'sizeof' operator, dereference the pointer instead of using the type. Most of the core was already doing this, so fix up the few places that weren't. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Change pointer type from 'unsigned char' to 'void'Chris Dickens2020-04-1711-78/+70
| | | | | | This removes the need for pointer casts when calling backend functions. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Kill the 'host_endian' argument for most functionsChris Dickens2020-04-178-62/+31
| | | | | | | | | The 'host_endian' argument exists only for a special case in the Linux backend, that being when the device descriptor is read using usbfs rather than sysfs. It does not apply to any other descriptor types nor any other backends, so remove it. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Remove redundant definitions for descriptor sizesChris Dickens2020-04-177-14/+14
| | | | | | | | The public libusb header provides all the definitions for the various descriptor sizes, so use them instead of defining them again with different names. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Remove usbi_parse_descriptor() functionChris Dickens2020-04-171-22/+23
| | | | | | | | | The Linux backend was the only caller of this function, but with the packed structures introduced in commit d06cc52851 its use is no longer necessary. Convert the function to static and remove the return type as no callers paid attention to it. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Add packed definitions for some standard USB descriptor typesChris Dickens2020-04-171-1/+1
| | | | | | | | These are going to be used in future commits to cleanup some code. Note that these are prefixed as 'usbi' rather than 'usb' to avoid conflicts with definitions provided by some system headers. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* netbsd: Recognize device timeouts.nia2020-04-091-0/+3
| | | | | | Closes #710 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Improve the get_interface_details_filter() functionChris Dickens2020-03-311-64/+79
| | | | | | | | | | | | | | | | The sole caller of this function only cares about the device interface path, so change the calling convention to be like that of the get_interface_details() function. This also adds more precise error reporting. Since this function is specific to the libusb0 filter driver, do not require the caller to provide the GUID for the libusb0 filter driver. Remove the use of strtok() on the result of this function. The strtok() function is not reentrant and is less-than-optimal for locating the start of the GUID component in the device interface path. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Replace the sanitize_path() function with normalize_path()Chris Dickens2020-03-311-34/+10
| | | | | | | | | | | | | | | | | | | Once upon a time the sanitize_path() function was needed to generate a consistent path format in order to hash the resulting string for use as session IDs. Since commit 71a779d078 ("Windows: Rework WinUSB enumeration process to fix issues on Win8+"), this hashing method is no longer used for session IDs, thus the sanitize_path() function was no longer explicitly needed. User lo1ol also reports in issue #633 that the function actually causes issues with devices where there is a path component following the device interface GUID. Rather than patching the function to fix this specific behavior, just replace it with a simpler function that returns an uppercased duplicate of the input string. Closes #633, Closes #662 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* build: Enable additional build errors and warningsChris Dickens2020-03-304-11/+47
| | | | | | | | | | | Help catch more errors by enabling additional build errors and warnings. Address some of the warnings seen with these new flags, including moving the libusb_transfer structure back out of the usbi_transfer structure to address 'warning: invalid use of structure with flexible array member'. Apparently a structure ending with a flexible array member is not okay with the compiler as the last member within another structure. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* build: Require C11 to build and clean up autoconfig/automake filesChris Dickens2020-03-301-1/+1
| | | | | | | | | | | | | | | | | | | | | | | C11 compiler support has been available for many years now. It is not unreasonable to require this now, and doing so allows some cleanup to the configure script. It is no longer necessary to check for compiler support of the '-fvibility' flag because any compiler that supports C11 will support this flag as well. Fix up the way that compiler and linker flags are passed down to the various makefiles. The compiler flags should be shared by all, but the linker flags and libraries should be separated between the library and the examples/tests. The visibility flag is only relevant for the library and the thread flags are only relevant for sources using thread constructs, so provide them as needed. Rearrange configure.ac to group similar functionality and consolidate where possible. Based on these changes, update the Travis configuration file to include newer versions of test platforms to ensure proper C11 compiler support. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* threads_posix: Improve usbi_get_tid() for various platformsChris Dickens2020-03-271-11/+41
| | | | | | | | | | | | | Add support for real thread IDs on macOS 10.6 and later using the new pthread_threadid_np() function. Add support for thread IDs on Haiku, NetBSD and Solaris. Provide a fallback value other than -1 when direct support is not available. This should suffice as a unique identifier since pthread_t, while opaque, is still distinct amongst active threads. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* darwin: Explicitly cleanup cached devices during the last libusb_exit()Chris Dickens2020-03-261-8/+14
| | | | | | | | | | | | | | | | | | | | | | | | Deferring the cached devices cleanup until the "destructor" function is called makes it appear as though libusb is leaking memory, especially if heap allocations are analyzed after calling libusb_exit(). It can also lead to devices staying on the list longer than they should, as seen by the following sequence of events: libusb_init() <-- init_count is 0, async thread is started devices_scan_devices() <-- enumerates devices libusb_exit() <-- init_count is 0, async thread is stopped [one or more devices disconnected] Because the async thread is stopped when device(s) are disconnected in the above sequence, the disconnection event(s) will not be processed and thus darwin_devices_detached() will not be called and the list will have one or more stale entries until the "destructor" function is finally called. Address both of these shortcomings by cleaning up the cached devices list after stopping the async thread. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Haiku: Fix some issues in the backendChris Dickens2020-03-164-57/+80
| | | | | | | | | | | | | | | Since commit 8cfcc63f4f ("haiku_usb_raw: Add missing wrap_sys_device field to usbi_os_backend"), compilation of the Haiku backend has been broken. Since the code is C++, named initializers are not supported. Fix this by going back to the original style of initializing usbi_os_backend. Additionally, commit db99ef3451 ("Various fixes for the Haiku port") further broken some things. The ClearHalt() function was defined as a member of the USBDevice class but is declared and needed as a member of the USBDeviceHandle class. The ioctl command code also contained a typo. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Make the 'reset_device' function of the backend optionalChris Dickens2020-03-165-45/+0
| | | | | | | | The majority of backends do not have support for resetting a device, so simplify them all by making the function optional and having the core return the appropriate error when the function is not implemented. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Fix return value of usbi_clock_gettime()Chris Dickens2020-03-162-7/+12
| | | | | | | | | | | In most cases, usbi_clock_gettime() will map to the standard library's clock_gettime() function. The semantics of this function are that it returns -1 upon failure with the error code available in the errno variable. The backends that need to implement this function should follow the same semantics, and the return value of usbi_clock_gettime() should not be directly propagated upwards. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Move parameter validation from backend to coreChris Dickens2020-03-165-44/+17
| | | | | | | | | | | | | | Some functions (e.g. libusb_set_interface_alt_setting()) do not perform sufficient parameter validation, leaving the burden on the backend to catch invalid user input. Much of this validation is common across all backends, yet not every backend implemented it. Fix this by moving parameter validation to the core library functions. This is also a good opportunity to remove the redundant 'num_configurations' field from the libusb_device structure. The value of this field is already contained in the 'device_descriptor' member. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Kill usbi_backend.clock_gettime() functionChris Dickens2020-03-159-112/+15
| | | | | | | | | | | | | | | | | | Out of all the backends supported by libusb, only two need to provide an implementation of the clock_gettime() function. Windows completely lacks such a function and versions of Mac OS prior to 10.12 do not provide it. In all other cases the backend simply ends up calling the C library's clock_gettime() function. Let's optimize for the common case and check for the availability of clock_gettime() during configure. If available, we will just call it directly from any part of the library that needs it. If not available, the backend is required to provide an implementation of usbi_clock_gettime() that matches the current requirements. Closes #685 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Fix some harmless build warningsChris Dickens2020-03-093-4/+3
| | | | Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Switch usbi_transfer to store timeout as timespecChris Dickens2020-03-051-15/+0
| | | | | | | | | | | | | | | | | The transfer timeout is structured around time values provided by the clock_gettime() function. This function uses a timespec structure, but the usbi_transfer structure was storing its calculated timeout in a timeval structure. This mismatch introduces extra work when checking for transfer timeouts as there must be a conversion between these two structures. Eliminate this by storing the calculated timeout as a timespec, thus allowing direct comparison. Note that a conversion to a timeval is still necessary in the libusb_get_next_timeout() function because the public API uses a timeval structure, but this is now the only place where such a conversion is done. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* darwin: Fix race condition that results in segmentation faultChris Dickens2020-03-051-52/+97
| | | | | | | | | | | | | | | | | Commit 763668cc92 ("darwin: fix occasional dead-lock on libusb_exit") resolved the deadlock situation observed in #112, but unfortunately there is a very rare race condition that can occur when the asynchronous event thread exits the run loop before CFRunLoopWakeUp() is called. This can occur when the shutdown source signal is processed as part of other events in the run loop, in which case the thread was already "awake". Prior to this change I was able to consistently trigger a segmentation fault within 10,000 iterations of a libusb_init()/libusb_exit() loop. With this change I reached over 4 million iterations without issue. Closes #701 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Introduce accessor functions for structure private dataChris Dickens2020-02-2613-360/+339
| | | | | | | | | | | | | | | | | | | | | The backend private data for the internal library structures has been accessed through a zero-length os_priv array of type unsigned char. This approach had two particular disadvantages: 1) A special attribute was needed on the 'os_priv' member to ensure that the field was properly aligned to a natural pointer alignment. The support needed for this is not available in every compiler. 2) Each access to the private data areas required an explicit cast from unsigned char to the type required by the backend. This change reworks the way the private data is accessed by the backends. New accessor functions return the private data as a void pointer type, removing the need for an explicit cast (except for Haiku, which is C++). The special alignment attribute trickery is also replaced by simple pointer arithmetic. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: fix MAX_PATH_LENGTHa1exdandy2020-02-221-1/+1
| | | | | | | | | | | | The MAX_PATH_LENGTH in libusb Windows backend is used as size of dev_id buffer. This buffer used for retreiving Device Instance Id by SetupDiGetDeviceInstanceIdA function. Acording to Microsoft, Device Instance Id must be less than MAX_DEVICE_ID_LEN = 200. So, value of 128 maybe not enough. Closes #699 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* linux: provide an event thread nameAlexander Stein2020-02-072-0/+12
| | | | | | | | | | | | Instead of having just the application name as thread name, provide a more descriptive one, which can e.g. read by htop. If setting the name fails, the thread will still work as intended, just raise a warning in this case. Closes #689 Signed-off-by: Alexander Stein <alexander.stein@mailbox.org> Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* threads_posix: Use thread-local storage to cache thread IDChris Dickens2020-02-071-8/+16
| | | | | | | | | | | | | | | | | | | | | Trying to capture debug logs that reproduce a problem can be tricky. Turning up the debug level will automatically make the program a bit slower. This alone cane make timing-sensitive bugs "disappear" when capturing logs. One of the hot paths for debug messages is fetching the thread ID, which is immeasurably helpful in understanding thread interactions within the library. Unfortunately most implementations require a system call to fetch the executing thread's ID, which isn't exactly going to help in the way of execution time. Add a check for thread-local storage support when configuring the library to build. If the toolchain provides this support, only one system call will be required per thread. This check is only done for non-Windows systems because thread-local storage is inefficiently implemented on MinGW. Closes #682 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Refactoring to consolidate and simplify common codeChris Dickens2020-02-077-1105/+828
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Both the UsbDk and WinUSB backends perform common steps when handling transfers in order to interact with the poll abstraction, both during submission and when processing transfer completion. With some rearranging of shared structures, this can be yanked from the individual backends and placed in the common area. This allows for several functions to be removed outright from each backend. The cancellation logic can also be simplified by attempting CancelIoEx() at the highest level and delegating to the backend if there are alternatives to try should CancelIoEx() fail. After some analysis of how Windows processes asychronous (OVERLAPPED) requests that the underlying driver completes synchronously, it is now evident that such requests need not be handled in any special fashion. Each function that called a driver function that was expected to complete asynchronously had logic to handle the case of a synchronous completion, so this has all been killed off. This significantly cleans up these call sites as now they must only check for an error condition. Finally, the initialization code for the WinUSB backend has been reworked to load the WinUSB DLL independent of the libusbK DLL. Previously when the libusbK DLL was present, all requests to devices using WinUSB would first be sent through the libusbK DLL where they would then be forwarded to the WinUSB DLL. This is slightly inefficient but is also limiting when using Windows 8.1 or later because support for isochronous transfers through WinUSB will be lost. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Fix poll implementation for NDEBUG (Release) buildsChris Dickens2020-01-301-1/+3
| | | | | | | | | | The refactoring in commit df61c0c3a3 ("Windows: improve poll abstraction") introduced a bug in builds where NDEBUG is defined because of a statement with side-effects that was put inside an assertion. When this statement is not evaluated, the file descriptor table gets corrupt. Fix this by moving the statement outside of the assertion. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Fix reported length of synchronous control transfersChris Dickens2020-01-281-3/+3
| | | | | | | | | | | | WinUSB control transfers that complete synchronously are incorrectly having the actual transfer length set to the size of the transfer buffer. If the control transfer is a read, the device may return less data than the transfer buffer size. Fix this by reporting the actual bytes transferred. Closes #667 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* linux_usbfs: Drop support for kernel versions earlier than 2.6.32Chris Dickens2020-01-263-235/+88
| | | | | | | | | | | | | | | The Linux backend plays lots of games to try and work with older versions of the kernel that do not have certain features. Lets simplify the backend by requiring at least 2.6.32 to use libusb. The only thing remaining that still requires explicit version checking is the maximum iso frame packet size. Anything running 2.6.32 or later is sure to have a functional monotonic clock, so this change also allows the removal of the get_timerfd_clock() function from the backend as well as the check for a functional monotonic clock during initialization. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Fix GCC printf format warnings due to DWORD/ULONG typesChris Dickens2020-01-264-23/+44
| | | | | | | | | | | | | The Visual Studio compiler considers a long to always be 32-bits, so the official Windows API headers define the DWORD and ULONG types as unsigned long proper. GCC (and possibly other compilers) vary the width of a long to match the build target, so this complicates printf format strings for these two types because the underlying type is inconsistent. Address this mess by introducing a macro that casts as necessary for the compiler. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Convert internal macros to static inline functionsChris Dickens2020-01-256-32/+69
| | | | | | | | | | | | Older versions of the Visual Studio compiler are picky about macros constructed with the 'do { ... } while (0)' construct. Convert these internal ones to static inline functions. The result is functionally equivalent but gets us type checking and a bit more readability. Also address some compiler warnings due to some header files that are being included in a different order than before. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* linux_usbfs: Clean up inconsistencies and optimize memory usageChris Dickens2020-01-242-570/+538
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The formatting and coding style varied across the whole file. Adopt the following consistent style: - Align function arguments to the opening parenthesis - Do not check for NULL before calling free() - Reduce indentation where possible in loops by continuing in the success case - Remove space between function name and opening parenthesis - Remove pointless pointer casts from void * - Replace comparisons with NULL or 0 by a negation operator - When comparing against rvalues, place the rvalue on the right side of the expression - Where possible, have the debug message string on the same line as the usbi_* call. This makes it easier to grep for specific strings. Also update the definitions in linux_usbfs.h to exactly match that of the kernel and remove definitions that are not needed. A number of functions declared stack buffers of size PATH_MAX. This is generally 4K, which is very much overkill for a lot of the strings and is not friendly for embedded environments. Replace many of these buffers with reasonably-sized ones, in many cases using exactly the size needed. When reading the descriptors during device enumeration, we were starting with a 1K buffer and doubling as needed. The vast majority of devices will not have a large set of descriptors, so change the allocation logic to grow the buffer in steps of 256 bytes. Introduce a new parsing function for reading sysfs attributes. Using the fdopen() function to use fscanf() results in excessive memory allocation, one for the FILE object and another for the buffer into which the C library will read the data. The sysfs attributes of interest are generally just a few characters, so use a small stack buffer and some rigorous parsing to read these attributes. This also consolidates error checking (e.g. negative values or larger than expected values). Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* configure.ac: Cleanup and refactoringChris Dickens2020-01-246-21/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Make the formatting consistent across the entire file. In particular: - Always quote strings whose values are derived - Use tabs consistently - Wrap all arguments with square brackets Replace the use of '-a' with '&&' to be more portable. Rearrange some of the feature checks to be conditional upon the platform or backend. For example, there is no need to check for nfds_t on Windows because poll() doesn't exist there. Similarly we now only check for timerfd on Linux and Solaris. This translates into slightly faster configure times. Explicitly define tokens for both the poll and thread implementations. This makes the preprocessor conditionals much nicer since it is not necessary to enumerate all possible OS_* tokens. Also replace POLL_NFDS_TYPE with a proper typedef that is based on the availability of the nfds_t type. Migrate to config definition names that are more consistent with autoconf. The check for timerfd actually verifies the presence of the library function instead of just the header definitions, and the token USBI_TIMERFD_AVAILABLE is now HAVE_TIMERFD. Similarly the check for syslog results in a definition of HAVE_SYSLOG. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Misc: Clean up zero-length strings and recursion in clock_gettime()Chris Dickens2020-01-243-20/+18
| | | | | | | | | | | | | | Commit 0bf84e4d51 ("core: fix build warning on newer versions of gcc") addressed compiler warnings for zero-length printf format strings in the core library files, but there are some additional remaining in some of the backends. Address these remaining ones in the same manner. Also remove the usbi_dbg() call in netbsd_clock_gettime(). This causes infinite recursion since usbi_dbg() calls the backend's clock_gettime() function. This was similarly addressed for the OpenBSD backend in commit 6acbd8b405 ("Remove infinite recursion in OpenBSD backend"). Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Misc: Cleanup across multiple backendsChris Dickens2020-01-248-200/+35
| | | | | | | | | | | | | | | | | | | Remove the clear_transfer_priv() function from all backends besides Linux. This function is only needed if the backend calls usbi_handle_disconnect(), which only Linux does. Remove the {attach,detach}_kernel_driver() functions from the Darwin backend. They return LIBUSB_ERROR_NOT_SUPPORTED, but the same result is achieved by having those functions be NULL. Remove the init() and exit() functions from the SunOS backend. They are optional and as no-ops are pointless. Remove NULL and 0 initializers from usbi_backend structures. Use named initializers in the NetBSD backend. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Misc: Trim and consolidate header file usageChris Dickens2020-01-2410-84/+51
| | | | | | | | | | | | | Refactor libusbi.h to include the set of common header files needed by every main source file in the library and change these source files to include libusbi.h first, followed by any non-common headers. Including libusbi.h first ensures that the config definitions are pulled in and will eliminate redundant includes in the individual sources files. Also clean up some whitespace errors and remove unnecessary definitions in the manually generated config.h files. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* core: Make style of debug messages with errno consistent across libraryChris Dickens2020-01-245-49/+49
| | | | Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Improve poll abstractionChris Dickens2020-01-223-329/+326
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Commit 395e5a8a6f ("windows: remove total fds (256) limitations") and commit c730a8410c ("windows: workaround WaitForMultipleObjects max 64 events limitation.") lifted some hard-coded limits in the number of HANDLEs that can be used within the library. This change improves on these changes to make them more efficient. A bitmap has been added to provide an efficient lookup mechanism for located unused file descriptor indices. This avoids the O(n) lookup time for traversing the entire fd_table. This bitmap is dynamically resized along with the fd_table. The incremental size of the fd_table has been reduced from 256 to 64. The vast majority of applications won't need to use 256 HANDLEs, so we can optimize memory usage a bit. Commit fb864b7cde ("fix windows crash when multi-thread do sync transfer") added usbi_inc_fds_ref() and usbi_dec_fds_ref() functions to work around a reference count issue. Remove these functions and change the implementation of usbi_poll() to take a reference to each file descriptor upon entry and drop the references when returning. If the application experiences any kind of crash, there is a problem elsewhere. Finally, make the thread executing usbi_poll() take part in the waiting. The original implementation had this thread simply waiting on a single event while separate threads waited on the HANDLEs. Now this thread will wait on MAXIMUM_WAIT_OBJECTS - 1 HANDLEs, thereby reducing the number of threads that are created. Additionally there is now only a single event object that is shared amongst all waiting threads. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Add missing '%' character for printf format specifierChris Dickens2020-01-221-2/+2
| | | | Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Kill the dedicated thread for monotonic clock_gettime()Chris Dickens2020-01-221-195/+18
| | | | | | | | | | | | | | According to Microsoft, anything prior to Vista could provide inconsistent results for the value of QueryPerformanceCounter() across different cores. Now that XP is no longer supported, we can drop the significant overhead of using a dedicated thread pinned to a single core just to read a timestamp. The C++11 steady_clock implementation directly wraps QueryPerformanceCounter(), so if it is good enough for that then it is good enough for our purposes. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Fix some MinGW build warnings and printf format specifiersChris Dickens2020-01-213-11/+11
| | | | Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Drop support for Windows XPChris Dickens2020-01-216-140/+40
| | | | | | | | | XP is nearly 20 years old and there are hoops we jump through to keep supporting it. Time to say goodbye and simplify some things. Closes #267 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Misc: Clean up Visual Studio project filesChris Dickens2020-01-211-5/+41
| | | | | | | | | | | | | | | | | | | | | | | Commit a9b34d170a ("Adding support for ARM & ARM64 Windows Platform") introduced a dependency on a particular version of the Windows 10 SDK for *all* platforms. This is particularly annoying for most users who will only be building for Windows. Fix this by specifying the SDK dependency only for the ARM/ARM64 platforms and bump to the latest. Commit 77037c4dd6 ("Adds /utf-8 to compile options") added this compiler option to all versions of the Visual Studio project files. This results in a number of warnings with the older versions that don't recognize this option. Fix this by keeping this option only for 2015 and newer. Explicitly specify library dependencies for non-static targets. With a small change in the UsbDk backend we can completely remove all depenencies other than kernel32.lib. Lastly, remove the 'MinimalRebuild' option for 2015 and newer project files as this option is now deprecated and results in a warning for each project. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* Windows: Remove support for WinCE and Visual Studio older than 2013Chris Dickens2020-01-2013-1336/+265
| | | | | | | | | | | There appears to be no need for the WinCE backend anymore, and it is increasingly difficult to keep healthy as the rest of the library changes. Require at least Visual Studio 2013 to compile. This simplifies matters as there is some semblance of C99 support there. Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
* linux_usbfs: Wait until all URBs have been reaped before freeing themChris Dickens2020-01-161-30/+30
| | | | | | | | | | | | | | | | Prior to this change, the URBs allocated for an individual transfer were freed when the last URB in the transfer was reaped. Normally this causes no issues because URBs are reaped in the order they were submitted. If the device is disconnected while multiple URBs are queued, these URBs may be reaped in an order that does not match that of submission. Change the logic to free the URBs when all the URBs of a transfer have been reaped rather than the last one. While in here, improve some debug messages. Closes #607 Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>