60 Commits

Author SHA1 Message Date
Ingvar Stepanyan
066a77fc0b webusb: Wasm+WebUSB backend fixes and improvements
- Added long-awaited support for multithreading. Since WebUSB still
 doesn't support accessing same device from multiple threads, this works
 by proxying I/O operations to the main thread as discussed on the
 original issue. For applications that run on the main thread nothing
 changes and they will continue to access WebUSB via Asyncify like
 before, while other threads will use blocking mechanism until an
 asynchronous response is available from the main thread.

 - Rewrote notification mechanism to use atomic waiting via sync Wasm
 instructions or `Atomics.waitAsync` depending on the thread. This
 results in simpler and faster notifications than the previous
 `postMessage`-based approach (which was used because
 `Atomics.waitAsync` wasn't yet available), as well as allows to send
 notifications across threads for multithreading scenario described
 above.

 - Fixed notification access to only wait/notify on the event we're
 interested instead of using a global lock.

 - Rewrote descriptor reading to query device for raw device &
 configuration descriptors instead of re-serializing them from
 JavaScript object representation. This incurs slight extra cost for the
 initial device opening, but fixes number of issues with information
 that is not yet exposed via WebUSB and allows to read supplementary
 descriptors such as string and BOS which were previously not supported.

 - Fixed listing only one alternate instead of all the available ones.

 - Fixed device closing & re-opening which could previously error out
 with "device busy" error.

 - Added mandatory Emscripten-specific linking flags to the generated
 pkgconfig.

 - Added device speed inference. This is not yet exposed via WebUSB, but
 we can at least make a best effort guess based on USB version and
 packet size, like some other backends already do.

 - Simplified & fixed device session ID generation which is now
 guaranteed to be truly unique, whereas previously it could clash with
 other devices of the same type.

 - Prepare code to support building for the Web without mandatory
 multithreading (which is costly on the Web) in the future. For now
 those `#ifdef`s are defunct as libusb is always compiled with
 `-pthread`, but some upcoming changes on the Emscripten side will allow
 to leverage those codepaths for smaller Wasm binaries.

 - Require explicit `--host=wasm32-unknown-emscripten` as we might want
 to add non-Emscripten WebAssembly backends in the future.

 - Various smaller fixes and improvements.

Note that this requires Emscripten 3.1.48 or newer to build for some of
the features used here, namely `co_await` support for JavaScript values
(without it code would be both more complex and slower) and some
proxying APIs. It shouldn't be a big deal in practice as most users
retrieve Emscripten via the official emsdk installer or Docker images.

Closes #1339
2023-12-08 22:17:40 +01:00
Nathan Hjelm
1ca2bc14ce darwin: add testing for IOKit version fallbacks
This commit adds a new unit test that verifies that the interface
interface and device interface versions are as expected. The unit test
relies on new testonly symbols to get details about the implementation.
The commit also adds a sanity check on the versions to ensure that
libusb_init fails if a supported version can not be found.

In addition to the new tests this commit also updates the tests to
use the static version of libusb. This provides them access to any
hidden symbol including testonly symbols (which should never be
exported in the shared library).

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2023-12-07 12:54:03 -07:00
Ingvar Stepanyan
ff8fe9397d Add Emscripten backend for WebAssembly + WebUSB support
Fixes #501
Closes #1057
2022-06-26 20:44:54 +02:00
Tormod Volden
f9162d36bf windows: Link gcc helpers statically on MinGW
Fixes #1049

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2022-03-16 17:53:00 +01:00
Chris Dickens
32a2206942 core: Refactor initialization and how the default context is handled
Highlights for this change:

 - usbi_default_context is only set if libusb_init() is called with NULL.
 - All hotplug related functionality (e.g. initialization, processing) has been
   moved to hotplug.c
 - Backends are simplified by removing initialization mutexes. Mutual exclusion
   between init()/exit() is provided by default_context_lock.
 - Make hotplug types and functions part of libusbi.h with the common usbi_
   prefixes (removes hotplug.h).

Addresses issue highlighted in #855

Closes #856

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Signed-off-by: Nathan Hjelm <hjelmn@google.com>
2021-06-02 22:53:59 -06:00
Chris Dickens
da5df37c4d build: Merge events and threads into single platform abstraction
The split between events and threads abstractions is unnecessary.
Simplify the library by merging the two into a "platform" abstraction.
The only meaningful change is that Cygwin builds will no longer use the
POSIX threads abstraction but will instead use the native Windows one.

The downside to doing this is that the dpfp_threaded example program
will no longer be available on Cygwin builds. This should be fine, and
future work will make dpfp_threaded available for all forms of Windows
build systems.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-09-12 22:44:28 -07:00
Chris Dickens
d67eb5beaa core: Introduce platform events abstraction
The way in which system handles or resources are represented differs
greatly between Unix-like operating systems and Windows. Ever since
Windows support was added to libusb, Windows been emulating principles
of Unix-like operating systems such as file descriptors and poll().

This commit introduces an abstraction layer that completely removes the
need to perform any emulation. Fundamentally there are three things that
each platform provides to libusb:

  1) A signallable event
  2) A timer (not required, but useful)
  3) A means to wait for event sources such as the above to be triggered

The POSIX abstraction for Unix-like operating systems uses file
descriptors as the "handles" to the underlying system resources. The
signallable event is implemented using a pipe, the timer as a timerfd
(where supported) and the poll() system call is used to wait for events.

The Windows abstraction uses native HANDLEs as the "handles" to the
underlying system resources. The signallable event is implemented using
a manual-reset event, the timer as a manual-reset waitable timer, and
the WaitForMultipleObjects() system call is used to wait for events.

Closes #252

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-08-12 16:06:38 -07:00
Chris Dickens
7c0cea7063 configure.ac: Fix compilation of Haiku's C++ convenience library
Commit 9a1bc8cafb ("build: Require C11 to build and clean up
autoconfig/automake files") added the language standard compiler option
to the AM_CFLAGS and AM_CXXFLAGS. Placing it in the latter is incorrect
as compiling C++ source with the C11 language standard does not make
sense.

Fix this by determining which C11 dialect (GNU or C) the compiler
supports and then constructing the compiler option from that.

Also restrict LT_LDFLAGS to the final libusb library (as was done
previously) since libtool complains about versioning options for
convenience libraries.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-04-09 11:30:19 -07:00
Chris Dickens
17143e307b automake: Add extra Makefile to handle unknown automake targets
Automake does not know how to compile Windows Resource files, so we have
manually told automake how to do this. One particular short-coming is
that dependencies are not automatically generated and tracked as they
are with other known automake targets. Address this by creating an extra
makefile that contains the automake rules needed for building source for
Windows targets. This also includes outputting more accurate tags during
compilation instead of using the generic "GEN" tag.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-04-01 20:54:09 -07:00
Chris Dickens
9a1bc8cafb build: Require C11 to build and clean up autoconfig/automake files
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>
2020-03-30 12:28:11 -07:00
Chris Dickens
0c6c072733 libusb/Makefile.am: Remove unnecessary assignments
Remove the unnecessary assignment of EXTRA_DIST. Anything that could end
up in SOURCES is automatically included in the distribution.

Remove the unnecessary assignment of libusb_1_0_la_CFLAGS. AM_CFLAGS is
already the default value, but assigning to the variable makes automake
think that custom CFLAGS are needed and therefore causes a bunch of
pointless additional rules to be generated. As a reference, the
generated Makefile shed 21,560 bytes (from 52,029 bytes to 30,469).

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-27 19:48:15 -07:00
Chris Dickens
c66f39f9ba libusb/Makefile.am: Fix out-of-tree builds on Windows
The prerequisite $< includes the path when not building in-tree, thus
the use of $(srcdir) as a prefix is incorrect.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-25 00:41:32 -07:00
Chris Dickens
4a5540a925 autotools: Fix a number of issues
Change the name of the project to be what it actually is called
everywhere: libusb-1.0. This allows the public libusb.h header file to
be tracked by automake through pkginclude_HEADERS.

Decouple the doc directory from automake. There aren't any targets that
automake understands, so the build uselessly recurses into the directory.
Update the makefile targets with the correct dependencies so that the
docs aren't regenerated unnecessarily. Update the doxygen config file to
include the version, exclude irrelevant source files and create the
output into 'api-1.0' instead of 'html'. Also fix a deprecation tag for
the libusb_get_port_path() function and add Solaris to the list of
supported platforms.

Fix the 'dist' target. Clean up the README file to remote the GitHub
Markdown and remove the .gitattributes file from the msvc directory.
Add doc/libusb.png to EXTRA_DIST.

Enhance the {dist,doc}-upload targets to look at the SF_USER environment
variable to get the SourceForge username. This allows maintainers (like
me!) to have a local username that is different from their SourceForge
username. Switch the docs-upload recipe to use rsync with --delete to
clean up obsolete files.

Fix the Windows shared library (DLL) targets. The dependencies for the
RC file were incorrect, leading to cases of missed recompilation. The
'all' rule should not be overridden, so define an 'all-local' rule when
necessary. Fix the rule for running dlltool on the just generated DLL so
that it only fires when the correct dependencies change and do not
bother to run the rule when not building a DLL.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-03-25 00:09:26 -07:00
Chris Dickens
a96937af99 autobuild: Fix two issues
The test for defining the automake conditional for the poll
implementation was keying off of the threads variable, producing
incorrect results for Cygwin.

A simple typo in the Makefile causes a build failure when
cross-compiling for Windows.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2020-01-26 14:29:48 -08:00
Chris Dickens
d5bb64b3dc configure.ac: Cleanup and refactoring
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>
2020-01-24 11:22:49 -08:00
Chris Dickens
f90d07613c Windows: Remove support for WinCE and Visual Studio older than 2013
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>
2020-01-20 18:02:19 -08:00
Pino Toscano
53572d7e5e Add Null POSIX backend
Add a simple null backend for POSIX platforms that reports no available
devices, and provides no capabilities. Make use of this new backend on
all the OSes without an existing backend, so libusb can be built even on
OSes without USB support.
2019-12-28 17:57:37 +01:00
Chris Dickens
54884e84d0 Windows: Enable dynamic selection between WinUSB and UsbDk backends
This commit unifies the two Windows backends into a single project and
enables the user to switch to the UsbDk backend, if available, using the
libusb_set_option() function. All contexts will use the WinUSB backend
by default for backwards compatibility.

With this change, the UsbDk-specific projects are no longer required.

Closes #309

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2018-01-08 10:17:26 -08:00
Lei Chen
eefd32213e Solaris backend
Closes #177

Signed-off-by: Nathan Hjelm <hjelmn@me.com>
2016-07-24 20:00:00 -06:00
Chris Dickens
48bfef6c1d Misc: Minor makefile cleanup
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2016-01-26 23:38:33 -08:00
Dmitry Fleytman
24c5289342 build: Integrate usbdk backend
Signed-off-by: Dmitry Fleytman <dfleytma@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2016-01-26 23:38:23 -08:00
Dmitry Fleytman
5d83abac3f windows: Rename windows_usb.h/c windows_winusb.h/c
Signed-off-by: Dmitry Fleytman <dfleytma@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2016-01-23 22:58:35 -08:00
Dmitry Fleytman
92a327815b windows: Move common definitions to a separate file
New files windows_nt_common.h/c introduced.

Signed-off-by: Dmitry Fleytman <dfleytma@redhat.com>
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2016-01-23 22:58:10 -08:00
Chris Dickens
3dc781ce3e Misc: Simplify Haiku build and fix broken parallel build
The Haiku build was previously being done as a nested package
due to its C++ requirement, but for some reason setting SUBDIR
in an automake conditional breaks parallel builds. To fix this
and simplify the Haiku build process, this commit adds an
unconditional check for a C++ compiler using AC_PROG_CXX and
builds the Haiku sources as part of the main libusb project.

Note that AC_PROG_CXX itself does not cause the configure script
to fail if a C++ compiler is not found. Therefore on non-Haiku
platforms there is no requirement to have a C++ compiler installed
in order to build libusb.

Closes #121

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2015-12-17 01:02:08 -08:00
Chris Dickens
4fe0f7e7a9 Misc: Remove autom4te.cache directory from Haiku during 'make dist'
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
2015-07-29 10:15:42 -07:00
Akshay Jaggi
a447fa4cd5 Fix broken 'make install'
...that was broken with dc97425bb4 (Haiku inclusion)
2014-09-27 00:17:39 +01:00
Akshay Jaggi
dc97425bb4 haiku: Add Haiku support 2014-09-25 21:42:16 +01:00
xantares
f6c935902a Windows: Fix MinGW parallel build 2014-01-25 22:42:31 +00:00
Hans de Goede
eb4e7bee44 openbsd: Split openbsd backend into separate openbsd and netbsd backends
Note the new netbsd_usb.c is an unmodified copy of openbsd_usb.c with
s/obsd/netbsd done on it. The reason for this split is that the openbsd
developers have been working on various improvements for their userspace
usb support, and adding support for those means breaking netbsd support,
by giving netbsd its own backend we can add support for the openbsd
improvements without breaking netbsd support.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-08-21 16:18:39 +02:00
Nathan Hjelm
872ff1704b Silence automake 1.14 warning
The warning is:

libusb/Makefile.am:57: warning: source file 'os/threads_windows.c' is in a subdirectory,
libusb/Makefile.am:57: but option 'subdir-objects' is disabled
automake: warning: possible forward-incompatibility.
automake: At least a source file is in a subdirectory, but the 'subdir-objects'
automake: automake option hasn't been enabled.  For now, the corresponding output
automake: object file(s) will be placed in the top-level directory.  However,
automake: this behaviour will change in future Automake versions: they will
automake: unconditionally cause object files to be placed in the same subdirectory
automake: of the corresponding sources.
automake: You are advised to start using 'subdir-objects' option throughout your
automake: project, to avoid future incompatibilities.

Fixed by setting the subdir-objects option.

Closes #125.
2013-07-30 09:10:19 -06:00
Hans de Goede
511ed18228 Core: Add a libusb_strerror() function
This patch adds the much requested libusb_strerror() function, taking into
account all issues people raised wrt previous attempts.

Criteria / Decisions underlying this implementation:
- Must support translated messages
- Must not use gettext as that does not work well in combination with Windows
 (when building with Visual C, or for Windows CE)
- API compatible with FreeBSD and various patched libusb-s floating around
- KISS:
 - Do not add any (other) library dependencies
 - Do not try to deal with message encodings (iconv), simply always return UTF-8
   making encoding the problem of the application using libusb_strerror.
 - Defaults to English, so apps which don't want translated messages,
   don't need to do anything special
 - Defaults to English (with pure ASCII messages), so apps which don't
   call libusb_setlocale() don't need to worry about encoding
2013-06-10 00:16:36 +01:00
Toby Gray
51655d7490 POSIX: Move setting of pipes to non-blocking into usbi_pipe
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-05-28 12:29:02 +02:00
Pete Batard
7e858a3408 Add hotplugtest to msvc project files 2013-05-16 10:55:13 +02:00
Nathan Hjelm
6853291ed0 Add hotplug support to the Linux backend.
There are two ways to configure hotplug support for Linux: udev, and netlink. It
is strongly recommened that udev support is used on systems that utilize udev.
We reenforce this recommendation by defaulting to --with-udev=yes at configure
time. To enable netlink support run configure with --with-udev=no. If udev
support is enabled all device enumeration is done with udev.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-05-15 20:43:32 +02:00
Nathan Hjelm
7801ff94fa Add hotplug support.
The internal API is changing as follows:
 - Adding two new functions. usbi_connect_device, and usbi_disconnect_device.
   Backends must call these functions to add them to the context's device list
   at one of two places: initial enumeration (done at init), and on device
   attach and removal. These functions need to be called once per context.
 - Backends that support hotplug should not provide a get_device_list funtion.
   This function is now deprecated and will likely be removed once all backends
   support hotplug.

The external API is changing as follows:
 - Two new functions have been added to register and deregister callbacks for
   hotplug notification: libusb_hotplug_register_callback(),
   libusb_hotplug_deregister_callback(). Hotplug callbacks are called by
   libusb_handle_events(). Details of the new API can be found in libusb.h.
 - A new capability check has been added to check for hotplug support. See
   LIBUSB_CAP_HAS_HOTPLUG.

Aa suggested by Xiaofan add new example has been added to show how to use
the new external hotplug API. See examples/hotplugtest.c.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2013-05-15 17:28:06 +02:00
Toby Gray
cf8506faf4 WinCE: Fix missing sources from dist tarball 2013-04-03 19:22:52 +01:00
Toby Gray
788e433d0a Windows: Add windows_common.h header
* This file contains definition that will be shared between the
  Windows and Windows CE backends
2013-01-23 00:39:49 +00:00
Peter Stuge
94b0ccc5e5 Autotools: Carry over 2012.10.23 libusb changes
* Use LIBS instead of PC_LIBS_PRIVATE
* Move THREAD_CFLAGS & VISIBILITY_CFLAGS into AM_CFLAGS
* Use AC_SEARCH_LIBS(clock_gettime) for pthreads on Linux
2012-11-25 00:27:45 +00:00
Pete Batard
beeb662e7c Autotools: add libusb-1.0.def and libusb_nano.h as dependencies
* Also add version_nano.h to EXTRA_DIST as it is not added to the
  dist archive by default
2012-06-11 14:26:51 +01:00
Pete Batard
6b33cd4589 Windows: Enable MinGW and MSVC DLL interchangeability
* Because we use the WINAPI calling convention, the def file MUST have the @n
  aliases. There is no way around this as MinGW's .o use decoration always
  for __stdcall, which can't be turned off.
* dlltool must therefore be invoked to create a proper import lib from the .def,
  using the --kill-at option.
* To do that, a CREATE_IMPORT_LIB autotools variable is introduced.
* Note: the .def file is currently maintained manually.
2012-06-08 23:31:56 +01:00
Martin Pieuchot
c56828857e OpenBSD backend 2012-01-30 10:43:46 +01:00
Peter Stuge
cdc5df8f1b libusb/Makefile.am: Avoid recursive variables in .rc silent-rule
Not every make supports recursive variable expansion so some automake
versions complain about non-POSIX variable names ever since commit
70bec4a9f8 which added support for
silent-rules in our rule to compile the Windows .rc file.

This commit removes the recursive variables and instead uses the
simple and generic GEN message and associated variable.
2012-01-12 10:34:55 +01:00
Peter Stuge
60bad550dc libusb/Makefile.am: Add libusb-1.0.rc dependency on version.h 2011-10-17 16:25:51 +02:00
Peter Stuge
70bec4a9f8 libusb/Makefile.am: Make the libusb-1.0.rc rule support silent-rules 2011-10-17 16:25:51 +02:00
Vitali Lovich
74282582cc Add recursive mutexes to threading abstraction
This is necessary for the device close path which needs to attain the
events lock, but which might itself be called while handling an event.
The events lock is necessary to properly clean up transfers which might
still be pointing to the device. References #82.

[stuge: Move usbi_mutex_init_recursive() into threads_posix.c]
[stuge: Must also #define _XOPEN_SOURCE 500 to be able to build]
[pbatard: Un-inline usbi_mutex_init_recursive() to make Cygwin happy]
2011-07-24 22:29:09 +02:00
Peter Stuge
18db4813e8 libusb/Makefile.am: Correct threading files in libusb_1_0_la_SOURCES 2011-07-24 22:29:09 +02:00
Peter Stuge
717f47621d configure.ac: Rename AM_LDFLAGS to LTLDFLAGS and actually use them
The new variable name tries to clarify that libtool is being used.
Linker flags must thus always be specified with -Wl.

Factor out the libtool flag -no-undefined from host specific cases.
The flag is required to build a Windows DLL, but is correct also for
the other supported systems.

Also, start actually using LTLDFLAGS in libusb/Makefile.am, so that
libtool will see the options set by configure.
2011-06-13 22:01:43 +02:00
Daniel Drake
d192c5bd32 Update libtool version info
With input from various people on the mailing list, update the libtool
versioning info and start to update this on every release.

The next libusb release will not need a change here. All following ones
will.
2010-10-04 18:45:18 +01:00
Pete Batard
9a4249f8a1 Add Windows support
Via Cygwin/MinGW, libusb now has windows support.
Thanks to contributors: Michael Plante, Orin Eman, Peter Stuge,
Stephan Meyer, Xiaofan Chen.
2010-07-27 20:57:20 -06:00
Pete Batard
fc0af8e3f7 Abstract low-level event handler operations
The Windows backend uses something other than UNIX file descriptors
for event handling. Abstract out the operations to allow for this.
2010-06-05 12:16:20 -05:00