usb 2.0.0-alpha.2 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitmodules +1 -1
- package/CHANGELOG.md +81 -0
- package/README.md +570 -0
- package/binding.gyp +2 -2
- package/dist/usb/bindings.d.ts +13 -0
- package/dist/usb/bindings.js +2 -1
- package/dist/usb/bindings.js.map +1 -1
- package/dist/usb/index.js +86 -2
- package/dist/usb/index.js.map +1 -1
- package/dist/usb/interface.js.map +1 -1
- package/dist/webusb/index.d.ts +28 -19
- package/dist/webusb/index.js +152 -66
- package/dist/webusb/index.js.map +1 -1
- package/dist/webusb/webusb-device.d.ts +1 -1
- package/dist/webusb/webusb-device.js +9 -4
- package/dist/webusb/webusb-device.js.map +1 -1
- package/libusb/.private/pre-commit.sh +7 -1
- package/libusb/.travis.yml +49 -0
- package/libusb/AUTHORS +44 -3
- package/libusb/Brewfile +4 -0
- package/libusb/ChangeLog +74 -2
- package/libusb/README.md +32 -0
- package/libusb/TODO +1 -1
- package/libusb/Xcode/common.xcconfig +12 -0
- package/libusb/Xcode/config.h +25 -0
- package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +959 -1
- package/libusb/android/README +4 -2
- package/libusb/android/config.h +75 -0
- package/libusb/appveyor.yml +41 -0
- package/libusb/appveyor_cygwin.bat +11 -0
- package/libusb/appveyor_minGW.bat +19 -0
- package/libusb/autogen.sh +1 -1
- package/libusb/bootstrap.sh +3 -16
- package/libusb/configure.ac +108 -80
- package/libusb/doc/doxygen.cfg.in +1785 -739
- package/libusb/examples/Makefile.am +1 -1
- package/libusb/examples/dpfp.c +3 -1
- package/libusb/examples/dpfp_threaded.c +23 -10
- package/libusb/examples/ezusb.c +3 -3
- package/libusb/examples/ezusb.h +2 -2
- package/libusb/examples/fxload.c +31 -9
- package/libusb/examples/hotplugtest.c +35 -7
- package/libusb/examples/listdevs.c +3 -1
- package/libusb/examples/sam3u_benchmark.c +3 -3
- package/libusb/examples/testlibusb.c +277 -0
- package/libusb/examples/xusb.c +40 -34
- package/libusb/libusb/Makefile.am +49 -23
- package/libusb/libusb/core.c +855 -457
- package/libusb/libusb/descriptor.c +72 -78
- package/libusb/libusb/hotplug.c +122 -76
- package/libusb/libusb/hotplug.h +42 -25
- package/libusb/libusb/io.c +625 -390
- package/libusb/libusb/libusb-1.0.def +12 -0
- package/libusb/libusb/libusb.h +218 -150
- package/libusb/libusb/libusbi.h +346 -176
- package/libusb/libusb/os/darwin_usb.c +604 -319
- package/libusb/libusb/os/darwin_usb.h +61 -20
- package/libusb/libusb/os/haiku_pollfs.cpp +367 -0
- package/libusb/libusb/os/haiku_usb.h +113 -0
- package/libusb/libusb/os/haiku_usb_backend.cpp +533 -0
- package/libusb/libusb/os/haiku_usb_raw.cpp +267 -0
- package/libusb/libusb/os/haiku_usb_raw.h +188 -0
- package/libusb/libusb/os/linux_netlink.c +186 -146
- package/libusb/libusb/os/linux_udev.c +36 -14
- package/libusb/libusb/os/linux_usbfs.c +426 -225
- package/libusb/libusb/os/linux_usbfs.h +5 -3
- package/libusb/libusb/os/netbsd_usb.c +21 -77
- package/libusb/libusb/os/openbsd_usb.c +32 -115
- package/libusb/libusb/os/poll_posix.c +38 -5
- package/libusb/libusb/os/poll_posix.h +3 -0
- package/libusb/libusb/os/poll_windows.c +277 -626
- package/libusb/libusb/os/poll_windows.h +11 -44
- package/libusb/libusb/os/sunos_usb.c +1695 -0
- package/libusb/libusb/os/sunos_usb.h +80 -0
- package/libusb/libusb/os/threads_posix.c +24 -26
- package/libusb/libusb/os/threads_posix.h +73 -21
- package/libusb/libusb/os/threads_windows.c +71 -157
- package/libusb/libusb/os/threads_windows.h +68 -44
- package/libusb/libusb/os/wince_usb.c +276 -420
- package/libusb/libusb/os/wince_usb.h +23 -28
- package/libusb/libusb/os/windows_common.h +78 -58
- package/libusb/libusb/os/windows_nt_common.c +1010 -0
- package/libusb/libusb/os/windows_nt_common.h +110 -0
- package/libusb/libusb/os/windows_nt_shared_types.h +147 -0
- package/libusb/libusb/os/windows_usbdk.c +830 -0
- package/libusb/libusb/os/windows_usbdk.h +103 -0
- package/libusb/libusb/os/windows_winusb.c +4391 -0
- package/libusb/libusb/os/windows_winusb.h +783 -0
- package/libusb/libusb/strerror.c +41 -7
- package/libusb/libusb/sync.c +41 -13
- package/libusb/libusb/version.h +1 -1
- package/libusb/libusb/version_nano.h +1 -1
- package/libusb/libusb-1.0.pc.in +1 -1
- package/libusb/msvc/appveyor.bat +27 -0
- package/libusb/msvc/config.h +5 -4
- package/libusb/msvc/ddk_build.cmd +87 -43
- package/libusb/msvc/fxload_2010.vcxproj +24 -104
- package/libusb/msvc/fxload_2012.vcxproj +24 -107
- package/libusb/msvc/fxload_2013.vcxproj +24 -107
- package/libusb/msvc/fxload_2015.vcxproj +91 -0
- package/libusb/msvc/fxload_2017.vcxproj +114 -0
- package/libusb/msvc/fxload_sources +1 -1
- package/libusb/msvc/getopt_2010.vcxproj +16 -75
- package/libusb/msvc/getopt_2012.vcxproj +16 -79
- package/libusb/msvc/getopt_2013.vcxproj +16 -79
- package/libusb/msvc/getopt_2015.vcxproj +73 -0
- package/libusb/msvc/getopt_2017.vcxproj +98 -0
- package/libusb/msvc/getopt_sources +6 -2
- package/libusb/msvc/hotplugtest_2010.vcxproj +18 -99
- package/libusb/msvc/hotplugtest_2012.vcxproj +18 -102
- package/libusb/msvc/hotplugtest_2013.vcxproj +18 -102
- package/libusb/msvc/hotplugtest_2015.vcxproj +83 -0
- package/libusb/msvc/hotplugtest_2017.vcxproj +106 -0
- package/libusb/msvc/hotplugtest_sources +1 -1
- package/libusb/msvc/libusb_2005.sln +20 -20
- package/libusb/msvc/libusb_2010.sln +57 -46
- package/libusb/msvc/libusb_2012.sln +57 -46
- package/libusb/msvc/libusb_2013.sln +57 -50
- package/libusb/msvc/libusb_2015.sln +59 -52
- package/libusb/msvc/libusb_2017.sln +186 -0
- package/libusb/msvc/libusb_dll.dsp +2 -2
- package/libusb/msvc/libusb_dll_2005.vcproj +30 -2
- package/libusb/msvc/libusb_dll_2010.vcxproj +26 -90
- package/libusb/msvc/libusb_dll_2012.vcxproj +28 -96
- package/libusb/msvc/libusb_dll_2013.vcxproj +28 -96
- package/libusb/msvc/libusb_dll_2015.vcxproj +107 -0
- package/libusb/msvc/libusb_dll_2017.vcxproj +134 -0
- package/libusb/msvc/libusb_dll_wince.vcproj +9 -1
- package/libusb/msvc/libusb_sources +10 -5
- package/libusb/msvc/libusb_static.dsp +2 -2
- package/libusb/msvc/libusb_static_2005.vcproj +32 -4
- package/libusb/msvc/libusb_static_2010.vcxproj +24 -83
- package/libusb/msvc/libusb_static_2012.vcxproj +25 -87
- package/libusb/msvc/libusb_static_2013.vcxproj +25 -87
- package/libusb/msvc/libusb_static_2015.vcxproj +98 -0
- package/libusb/msvc/libusb_static_2017.vcxproj +117 -0
- package/libusb/msvc/libusb_static_wince.vcproj +20 -26
- package/libusb/msvc/libusb_wince.sln +88 -88
- package/libusb/msvc/listdevs_2010.vcxproj +16 -99
- package/libusb/msvc/listdevs_2012.vcxproj +16 -102
- package/libusb/msvc/listdevs_2013.vcxproj +16 -102
- package/libusb/msvc/listdevs_2015.vcxproj +83 -0
- package/libusb/msvc/listdevs_2017.vcxproj +106 -0
- package/libusb/msvc/listdevs_sources +2 -1
- package/libusb/msvc/stress_2010.vcxproj +20 -101
- package/libusb/msvc/stress_2012.vcxproj +20 -104
- package/libusb/msvc/stress_2013.vcxproj +20 -104
- package/libusb/msvc/stress_2015.vcxproj +87 -0
- package/libusb/msvc/stress_2017.vcxproj +110 -0
- package/libusb/msvc/stress_sources +21 -0
- package/libusb/msvc/testlibusb_2010.vcxproj +82 -0
- package/libusb/msvc/testlibusb_2012.vcxproj +83 -0
- package/libusb/msvc/testlibusb_2013.vcxproj +83 -0
- package/libusb/msvc/testlibusb_2015.vcxproj +83 -0
- package/libusb/msvc/testlibusb_2017.vcxproj +106 -0
- package/libusb/msvc/testlibusb_sources +20 -0
- package/libusb/msvc/xusb_2010.vcxproj +17 -98
- package/libusb/msvc/xusb_2012.vcxproj +17 -101
- package/libusb/msvc/xusb_2013.vcxproj +17 -101
- package/libusb/msvc/xusb_2015.vcxproj +83 -0
- package/libusb/msvc/xusb_2017.vcxproj +106 -0
- package/libusb/msvc/xusb_sources +1 -1
- package/libusb/tests/stress.c +2 -2
- package/libusb/tests/testlib.c +0 -4
- package/libusb/travis-autogen.sh +39 -0
- package/libusb.gypi +13 -2
- package/package.json +23 -13
- package/prebuilds/android-arm/node.napi.armv7.node +0 -0
- package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
- package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
- package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
- package/prebuilds/linux-ia32/node.napi.node +0 -0
- package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
- package/prebuilds/linux-x64/node.napi.musl.node +0 -0
- package/prebuilds/win32-ia32/node.napi.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/src/device.cc +7 -7
- package/src/node_usb.cc +43 -1
- package/src/uv_async_queue.h +33 -25
- package/test/usb.coffee +6 -0
- package/tsc/index.ts +1 -1
- package/tsc/usb/bindings.ts +19 -1
- package/tsc/usb/index.ts +67 -19
- package/tsc/usb/interface.ts +2 -2
- package/tsc/webusb/index.ts +187 -75
- package/tsc/webusb/webusb-device.ts +19 -15
- package/.github/workflows/prebuild.yml +0 -48
- package/Readme.md +0 -339
- package/dist/webusb/typed-event-target.d.ts +0 -11
- package/dist/webusb/typed-event-target.js +0 -21
- package/dist/webusb/typed-event-target.js.map +0 -1
- package/docs/assets/css/main.css +0 -2660
- package/docs/assets/images/icons.png +0 -0
- package/docs/assets/images/icons@2x.png +0 -0
- package/docs/assets/images/widgets.png +0 -0
- package/docs/assets/images/widgets@2x.png +0 -0
- package/docs/assets/js/main.js +0 -248
- package/docs/assets/js/search.js +0 -1
- package/docs/classes/usb_bindings.device.html +0 -1338
- package/docs/classes/usb_bindings.libusbexception.html +0 -234
- package/docs/classes/usb_bindings.transfer.html +0 -344
- package/docs/classes/usb_capability.capability.html +0 -297
- package/docs/classes/usb_device.extendeddevice.html +0 -766
- package/docs/classes/usb_endpoint.endpoint.html +0 -472
- package/docs/classes/usb_endpoint.inendpoint.html +0 -766
- package/docs/classes/usb_endpoint.outendpoint.html +0 -582
- package/docs/classes/usb_interface.interface.html +0 -648
- package/docs/classes/webusb.webusb-1.html +0 -615
- package/docs/classes/webusb_mutex.mutex.html +0 -270
- package/docs/classes/webusb_typed_event_target.typedeventtarget.html +0 -443
- package/docs/classes/webusb_webusb_device.webusbdevice.html +0 -904
- package/docs/index.html +0 -500
- package/docs/interfaces/usb.deviceevents.html +0 -242
- package/docs/interfaces/usb_descriptors.bosdescriptor.html +0 -293
- package/docs/interfaces/usb_descriptors.capabilitydescriptor.html +0 -274
- package/docs/interfaces/usb_descriptors.configdescriptor.html +0 -388
- package/docs/interfaces/usb_descriptors.devicedescriptor.html +0 -464
- package/docs/interfaces/usb_descriptors.endpointdescriptor.html +0 -370
- package/docs/interfaces/usb_descriptors.interfacedescriptor.html +0 -407
- package/docs/interfaces/webusb.usboptions.html +0 -226
- package/docs/modules/index.html +0 -304
- package/docs/modules/usb.html +0 -173
- package/docs/modules/usb_bindings.html +0 -2248
- package/docs/modules/usb_capability.html +0 -156
- package/docs/modules/usb_descriptors.html +0 -176
- package/docs/modules/usb_device.html +0 -156
- package/docs/modules/usb_endpoint.html +0 -164
- package/docs/modules/usb_interface.html +0 -156
- package/docs/modules/webusb.html +0 -165
- package/docs/modules/webusb_mutex.html +0 -156
- package/docs/modules/webusb_typed_event_target.html +0 -156
- package/docs/modules/webusb_webusb_device.html +0 -156
- package/docs/modules.html +0 -156
- package/libusb/INSTALL +0 -234
- package/libusb/README +0 -28
- package/libusb/libusb/os/windows_usb.c +0 -5347
- package/libusb/libusb/os/windows_usb.h +0 -971
- package/libusb/msvc/fxload_2010.vcxproj.filters +0 -25
- package/libusb/msvc/fxload_2012.vcxproj.filters +0 -25
- package/libusb/msvc/getopt_2010.vcxproj.filters +0 -26
- package/libusb/msvc/getopt_2012.vcxproj.filters +0 -26
- package/libusb/msvc/hotplugtest_2010.vcxproj.filters +0 -14
- package/libusb/msvc/hotplugtest_2012.vcxproj.filters +0 -14
- package/libusb/msvc/libusb_dll_2010.vcxproj.filters +0 -81
- package/libusb/msvc/libusb_dll_2012.vcxproj.filters +0 -84
- package/libusb/msvc/libusb_static_2010.vcxproj.filters +0 -74
- package/libusb/msvc/libusb_static_2012.vcxproj.filters +0 -74
- package/libusb/msvc/listdevs_2010.vcxproj.filters +0 -14
- package/libusb/msvc/listdevs_2012.vcxproj.filters +0 -14
- package/libusb/msvc/stress_2010.vcxproj.filters +0 -25
- package/libusb/msvc/stress_2012.vcxproj.filters +0 -25
- package/libusb/msvc/xusb_2010.vcxproj.filters +0 -14
- package/libusb/msvc/xusb_2012.vcxproj.filters +0 -14
- package/tsc/webusb/typed-event-target.ts +0 -23
package/libusb/libusb/core.c
CHANGED
|
@@ -44,29 +44,16 @@
|
|
|
44
44
|
#include "libusbi.h"
|
|
45
45
|
#include "hotplug.h"
|
|
46
46
|
|
|
47
|
-
#if defined(OS_LINUX)
|
|
48
|
-
const struct usbi_os_backend * const usbi_backend = &linux_usbfs_backend;
|
|
49
|
-
#elif defined(OS_DARWIN)
|
|
50
|
-
const struct usbi_os_backend * const usbi_backend = &darwin_backend;
|
|
51
|
-
#elif defined(OS_OPENBSD)
|
|
52
|
-
const struct usbi_os_backend * const usbi_backend = &openbsd_backend;
|
|
53
|
-
#elif defined(OS_NETBSD)
|
|
54
|
-
const struct usbi_os_backend * const usbi_backend = &netbsd_backend;
|
|
55
|
-
#elif defined(OS_WINDOWS)
|
|
56
|
-
const struct usbi_os_backend * const usbi_backend = &windows_backend;
|
|
57
|
-
#elif defined(OS_WINCE)
|
|
58
|
-
const struct usbi_os_backend * const usbi_backend = &wince_backend;
|
|
59
|
-
#else
|
|
60
|
-
#error "Unsupported OS"
|
|
61
|
-
#endif
|
|
62
|
-
|
|
63
47
|
struct libusb_context *usbi_default_context = NULL;
|
|
64
48
|
static const struct libusb_version libusb_version_internal =
|
|
65
49
|
{ LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
|
|
66
50
|
LIBUSB_RC, "http://libusb.info" };
|
|
67
51
|
static int default_context_refcnt = 0;
|
|
68
52
|
static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
|
|
69
|
-
static struct
|
|
53
|
+
static struct timespec timestamp_origin = { 0, 0 };
|
|
54
|
+
#ifndef USE_SYSTEM_LOGGING_FACILITY
|
|
55
|
+
static libusb_log_cb log_handler = NULL;
|
|
56
|
+
#endif
|
|
70
57
|
|
|
71
58
|
usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER;
|
|
72
59
|
struct list_head active_contexts_list;
|
|
@@ -92,6 +79,10 @@ struct list_head active_contexts_list;
|
|
|
92
79
|
* Specification</a> which is available for free download. You can probably
|
|
93
80
|
* find less verbose introductions by searching the web.
|
|
94
81
|
*
|
|
82
|
+
* \section API Application Programming Interface (API)
|
|
83
|
+
*
|
|
84
|
+
* See the \ref libusb_api page for a complete list of the libusb functions.
|
|
85
|
+
*
|
|
95
86
|
* \section features Library features
|
|
96
87
|
*
|
|
97
88
|
* - All transfer types supported (control/bulk/interrupt/isochronous)
|
|
@@ -102,7 +93,7 @@ struct list_head active_contexts_list;
|
|
|
102
93
|
* usually won't need to thread)
|
|
103
94
|
* - Lightweight with lean API
|
|
104
95
|
* - Compatible with libusb-0.1 through the libusb-compat-0.1 translation layer
|
|
105
|
-
* - Hotplug support (on some platforms). See \ref
|
|
96
|
+
* - Hotplug support (on some platforms). See \ref libusb_hotplug.
|
|
106
97
|
*
|
|
107
98
|
* \section gettingstarted Getting Started
|
|
108
99
|
*
|
|
@@ -110,7 +101,7 @@ struct list_head active_contexts_list;
|
|
|
110
101
|
* links to the different categories of libusb's functionality.
|
|
111
102
|
*
|
|
112
103
|
* One decision you will have to make is whether to use the synchronous
|
|
113
|
-
* or the asynchronous data transfer interface. The \ref
|
|
104
|
+
* or the asynchronous data transfer interface. The \ref libusb_io documentation
|
|
114
105
|
* provides some insight into this topic.
|
|
115
106
|
*
|
|
116
107
|
* Some example programs can be found in the libusb source distribution under
|
|
@@ -121,22 +112,24 @@ struct list_head active_contexts_list;
|
|
|
121
112
|
*
|
|
122
113
|
* libusb functions typically return 0 on success or a negative error code
|
|
123
114
|
* on failure. These negative error codes relate to LIBUSB_ERROR constants
|
|
124
|
-
* which are listed on the \ref
|
|
115
|
+
* which are listed on the \ref libusb_misc "miscellaneous" documentation page.
|
|
125
116
|
*
|
|
126
117
|
* \section msglog Debug message logging
|
|
127
118
|
*
|
|
128
119
|
* libusb uses stderr for all logging. By default, logging is set to NONE,
|
|
129
120
|
* which means that no output will be produced. However, unless the library
|
|
130
121
|
* has been compiled with logging disabled, then any application calls to
|
|
131
|
-
*
|
|
132
|
-
* LIBUSB_DEBUG outside of the application, can result
|
|
133
|
-
* produced. Your application should therefore not close
|
|
134
|
-
* direct it to the null device if its output is
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
122
|
+
* libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level), or the setting of the
|
|
123
|
+
* environmental variable LIBUSB_DEBUG outside of the application, can result
|
|
124
|
+
* in logging being produced. Your application should therefore not close
|
|
125
|
+
* stderr, but instead direct it to the null device if its output is
|
|
126
|
+
* undesirable.
|
|
127
|
+
*
|
|
128
|
+
* The libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) function can be
|
|
129
|
+
* used to enable logging of certain messages. Under standard configuration,
|
|
130
|
+
* libusb doesn't really log much so you are advised to use this function
|
|
131
|
+
* to enable all error/warning/ informational messages. It will help debug
|
|
132
|
+
* problems with your software.
|
|
140
133
|
*
|
|
141
134
|
* The logged messages are unstructured. There is no one-to-one correspondence
|
|
142
135
|
* between messages being logged and success or failure return codes from
|
|
@@ -151,27 +144,43 @@ struct list_head active_contexts_list;
|
|
|
151
144
|
*
|
|
152
145
|
* The LIBUSB_DEBUG environment variable can be used to enable message logging
|
|
153
146
|
* at run-time. This environment variable should be set to a log level number,
|
|
154
|
-
* which is interpreted the same as the
|
|
147
|
+
* which is interpreted the same as the
|
|
148
|
+
* libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) parameter. When this
|
|
155
149
|
* environment variable is set, the message logging verbosity level is fixed
|
|
156
|
-
* and
|
|
150
|
+
* and libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) effectively does
|
|
151
|
+
* nothing.
|
|
157
152
|
*
|
|
158
153
|
* libusb can be compiled without any logging functions, useful for embedded
|
|
159
|
-
* systems. In this case,
|
|
160
|
-
* variable have no effects.
|
|
154
|
+
* systems. In this case, libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level)
|
|
155
|
+
* and the LIBUSB_DEBUG environment variable have no effects.
|
|
161
156
|
*
|
|
162
157
|
* libusb can also be compiled with verbose debugging messages always. When
|
|
163
158
|
* the library is compiled in this way, all messages of all verbosities are
|
|
164
|
-
* always logged.
|
|
165
|
-
* have no effects.
|
|
159
|
+
* always logged. libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) and
|
|
160
|
+
* the LIBUSB_DEBUG environment variable have no effects.
|
|
166
161
|
*
|
|
167
162
|
* \section remarks Other remarks
|
|
168
163
|
*
|
|
169
|
-
* libusb does have imperfections. The \ref
|
|
164
|
+
* libusb does have imperfections. The \ref libusb_caveats "caveats" page attempts
|
|
170
165
|
* to document these.
|
|
171
166
|
*/
|
|
172
167
|
|
|
173
168
|
/**
|
|
174
|
-
* \page
|
|
169
|
+
* \page libusb_caveats Caveats
|
|
170
|
+
*
|
|
171
|
+
* \section fork Fork considerations
|
|
172
|
+
*
|
|
173
|
+
* libusb is <em>not</em> designed to work across fork() calls. Depending on
|
|
174
|
+
* the platform, there may be resources in the parent process that are not
|
|
175
|
+
* available to the child (e.g. the hotplug monitor thread on Linux). In
|
|
176
|
+
* addition, since the parent and child will share libusb's internal file
|
|
177
|
+
* descriptors, using libusb in any way from the child could cause the parent
|
|
178
|
+
* process's \ref libusb_context to get into an inconsistent state.
|
|
179
|
+
*
|
|
180
|
+
* On Linux, libusb's file descriptors will be marked as CLOEXEC, which means
|
|
181
|
+
* that it is safe to fork() and exec() without worrying about the child
|
|
182
|
+
* process needing to clean up state or having access to these file descriptors.
|
|
183
|
+
* Other platforms may not be so forgiving, so consider yourself warned!
|
|
175
184
|
*
|
|
176
185
|
* \section devresets Device resets
|
|
177
186
|
*
|
|
@@ -217,9 +226,6 @@ struct list_head active_contexts_list;
|
|
|
217
226
|
* -# If the device is already in the desired configuration, calling
|
|
218
227
|
* libusb_set_configuration() using the same configuration value will cause
|
|
219
228
|
* a lightweight device reset. This may not be desirable behaviour.
|
|
220
|
-
* -# libusb will be unable to change configuration if the device is in
|
|
221
|
-
* another configuration and other programs or drivers have claimed
|
|
222
|
-
* interfaces under that configuration.
|
|
223
229
|
* -# In the case where the desired configuration is already active, libusb
|
|
224
230
|
* may not even be able to perform a lightweight device reset. For example,
|
|
225
231
|
* take my USB keyboard with fingerprint reader: I'm interested in driving
|
|
@@ -228,12 +234,17 @@ struct list_head active_contexts_list;
|
|
|
228
234
|
* Because the kernel has claimed an interface, it is not even possible to
|
|
229
235
|
* perform the lightweight device reset, so libusb_set_configuration() will
|
|
230
236
|
* fail. (Luckily the device in question only has a single configuration.)
|
|
237
|
+
* -# libusb will be unable to set a configuration if other programs or
|
|
238
|
+
* drivers have claimed interfaces. In particular, this means that kernel
|
|
239
|
+
* drivers must be detached from all the interfaces before
|
|
240
|
+
* libusb_set_configuration() may succeed.
|
|
231
241
|
*
|
|
232
242
|
* One solution to some of the above problems is to consider the currently
|
|
233
243
|
* active configuration. If the configuration we want is already active, then
|
|
234
244
|
* we don't have to select any configuration:
|
|
235
245
|
\code
|
|
236
|
-
cfg =
|
|
246
|
+
cfg = -1;
|
|
247
|
+
libusb_get_configuration(dev, &cfg);
|
|
237
248
|
if (cfg != desired)
|
|
238
249
|
libusb_set_configuration(dev, desired);
|
|
239
250
|
\endcode
|
|
@@ -275,7 +286,6 @@ if (cfg != desired)
|
|
|
275
286
|
* information about the end of the short packet, and the user probably wanted
|
|
276
287
|
* that surplus data to arrive in the next logical transfer.
|
|
277
288
|
*
|
|
278
|
-
*
|
|
279
289
|
* \section zlp Zero length packets
|
|
280
290
|
*
|
|
281
291
|
* - libusb is able to send a packet of zero length to an endpoint simply by
|
|
@@ -285,7 +295,7 @@ if (cfg != desired)
|
|
|
285
295
|
*/
|
|
286
296
|
|
|
287
297
|
/**
|
|
288
|
-
* \page
|
|
298
|
+
* \page libusb_contexts Contexts
|
|
289
299
|
*
|
|
290
300
|
* It is possible that libusb may be used simultaneously from two independent
|
|
291
301
|
* libraries linked into the same executable. For example, if your application
|
|
@@ -294,7 +304,7 @@ if (cfg != desired)
|
|
|
294
304
|
* developed modules may both use libusb.
|
|
295
305
|
*
|
|
296
306
|
* libusb is written to allow for these multiple user scenarios. The two
|
|
297
|
-
* "instances" of libusb will not interfere:
|
|
307
|
+
* "instances" of libusb will not interfere: libusb_set_option() calls
|
|
298
308
|
* from one user will not affect the same settings for other users, other
|
|
299
309
|
* users can continue using libusb after one of them calls libusb_exit(), etc.
|
|
300
310
|
*
|
|
@@ -323,15 +333,169 @@ if (cfg != desired)
|
|
|
323
333
|
* can infer the context from those objects.
|
|
324
334
|
*/
|
|
325
335
|
|
|
336
|
+
/**
|
|
337
|
+
* \page libusb_api Application Programming Interface
|
|
338
|
+
*
|
|
339
|
+
* This is the complete list of libusb functions, structures and
|
|
340
|
+
* enumerations in alphabetical order.
|
|
341
|
+
*
|
|
342
|
+
* \section Functions
|
|
343
|
+
* - libusb_alloc_streams()
|
|
344
|
+
* - libusb_alloc_transfer()
|
|
345
|
+
* - libusb_attach_kernel_driver()
|
|
346
|
+
* - libusb_bulk_transfer()
|
|
347
|
+
* - libusb_cancel_transfer()
|
|
348
|
+
* - libusb_claim_interface()
|
|
349
|
+
* - libusb_clear_halt()
|
|
350
|
+
* - libusb_close()
|
|
351
|
+
* - libusb_control_transfer()
|
|
352
|
+
* - libusb_control_transfer_get_data()
|
|
353
|
+
* - libusb_control_transfer_get_setup()
|
|
354
|
+
* - libusb_cpu_to_le16()
|
|
355
|
+
* - libusb_detach_kernel_driver()
|
|
356
|
+
* - libusb_dev_mem_alloc()
|
|
357
|
+
* - libusb_dev_mem_free()
|
|
358
|
+
* - libusb_error_name()
|
|
359
|
+
* - libusb_event_handler_active()
|
|
360
|
+
* - libusb_event_handling_ok()
|
|
361
|
+
* - libusb_exit()
|
|
362
|
+
* - libusb_fill_bulk_stream_transfer()
|
|
363
|
+
* - libusb_fill_bulk_transfer()
|
|
364
|
+
* - libusb_fill_control_setup()
|
|
365
|
+
* - libusb_fill_control_transfer()
|
|
366
|
+
* - libusb_fill_interrupt_transfer()
|
|
367
|
+
* - libusb_fill_iso_transfer()
|
|
368
|
+
* - libusb_free_bos_descriptor()
|
|
369
|
+
* - libusb_free_config_descriptor()
|
|
370
|
+
* - libusb_free_container_id_descriptor()
|
|
371
|
+
* - libusb_free_device_list()
|
|
372
|
+
* - libusb_free_pollfds()
|
|
373
|
+
* - libusb_free_ss_endpoint_companion_descriptor()
|
|
374
|
+
* - libusb_free_ss_usb_device_capability_descriptor()
|
|
375
|
+
* - libusb_free_streams()
|
|
376
|
+
* - libusb_free_transfer()
|
|
377
|
+
* - libusb_free_usb_2_0_extension_descriptor()
|
|
378
|
+
* - libusb_get_active_config_descriptor()
|
|
379
|
+
* - libusb_get_bos_descriptor()
|
|
380
|
+
* - libusb_get_bus_number()
|
|
381
|
+
* - libusb_get_config_descriptor()
|
|
382
|
+
* - libusb_get_config_descriptor_by_value()
|
|
383
|
+
* - libusb_get_configuration()
|
|
384
|
+
* - libusb_get_container_id_descriptor()
|
|
385
|
+
* - libusb_get_descriptor()
|
|
386
|
+
* - libusb_get_device()
|
|
387
|
+
* - libusb_get_device_address()
|
|
388
|
+
* - libusb_get_device_descriptor()
|
|
389
|
+
* - libusb_get_device_list()
|
|
390
|
+
* - libusb_get_device_speed()
|
|
391
|
+
* - libusb_get_iso_packet_buffer()
|
|
392
|
+
* - libusb_get_iso_packet_buffer_simple()
|
|
393
|
+
* - libusb_get_max_iso_packet_size()
|
|
394
|
+
* - libusb_get_max_packet_size()
|
|
395
|
+
* - libusb_get_next_timeout()
|
|
396
|
+
* - libusb_get_parent()
|
|
397
|
+
* - libusb_get_pollfds()
|
|
398
|
+
* - libusb_get_port_number()
|
|
399
|
+
* - libusb_get_port_numbers()
|
|
400
|
+
* - libusb_get_port_path()
|
|
401
|
+
* - libusb_get_ss_endpoint_companion_descriptor()
|
|
402
|
+
* - libusb_get_ss_usb_device_capability_descriptor()
|
|
403
|
+
* - libusb_get_string_descriptor()
|
|
404
|
+
* - libusb_get_string_descriptor_ascii()
|
|
405
|
+
* - libusb_get_usb_2_0_extension_descriptor()
|
|
406
|
+
* - libusb_get_version()
|
|
407
|
+
* - libusb_handle_events()
|
|
408
|
+
* - libusb_handle_events_completed()
|
|
409
|
+
* - libusb_handle_events_locked()
|
|
410
|
+
* - libusb_handle_events_timeout()
|
|
411
|
+
* - libusb_handle_events_timeout_completed()
|
|
412
|
+
* - libusb_has_capability()
|
|
413
|
+
* - libusb_hotplug_deregister_callback()
|
|
414
|
+
* - libusb_hotplug_register_callback()
|
|
415
|
+
* - libusb_init()
|
|
416
|
+
* - libusb_interrupt_event_handler()
|
|
417
|
+
* - libusb_interrupt_transfer()
|
|
418
|
+
* - libusb_kernel_driver_active()
|
|
419
|
+
* - libusb_lock_events()
|
|
420
|
+
* - libusb_lock_event_waiters()
|
|
421
|
+
* - libusb_open()
|
|
422
|
+
* - libusb_open_device_with_vid_pid()
|
|
423
|
+
* - libusb_pollfds_handle_timeouts()
|
|
424
|
+
* - libusb_ref_device()
|
|
425
|
+
* - libusb_release_interface()
|
|
426
|
+
* - libusb_reset_device()
|
|
427
|
+
* - libusb_set_auto_detach_kernel_driver()
|
|
428
|
+
* - libusb_set_configuration()
|
|
429
|
+
* - libusb_set_debug()
|
|
430
|
+
* - libusb_set_log_cb()
|
|
431
|
+
* - libusb_set_interface_alt_setting()
|
|
432
|
+
* - libusb_set_iso_packet_lengths()
|
|
433
|
+
* - libusb_set_option()
|
|
434
|
+
* - libusb_setlocale()
|
|
435
|
+
* - libusb_set_pollfd_notifiers()
|
|
436
|
+
* - libusb_strerror()
|
|
437
|
+
* - libusb_submit_transfer()
|
|
438
|
+
* - libusb_transfer_get_stream_id()
|
|
439
|
+
* - libusb_transfer_set_stream_id()
|
|
440
|
+
* - libusb_try_lock_events()
|
|
441
|
+
* - libusb_unlock_events()
|
|
442
|
+
* - libusb_unlock_event_waiters()
|
|
443
|
+
* - libusb_unref_device()
|
|
444
|
+
* - libusb_wait_for_event()
|
|
445
|
+
*
|
|
446
|
+
* \section Structures
|
|
447
|
+
* - libusb_bos_descriptor
|
|
448
|
+
* - libusb_bos_dev_capability_descriptor
|
|
449
|
+
* - libusb_config_descriptor
|
|
450
|
+
* - libusb_container_id_descriptor
|
|
451
|
+
* - \ref libusb_context
|
|
452
|
+
* - libusb_control_setup
|
|
453
|
+
* - \ref libusb_device
|
|
454
|
+
* - libusb_device_descriptor
|
|
455
|
+
* - \ref libusb_device_handle
|
|
456
|
+
* - libusb_endpoint_descriptor
|
|
457
|
+
* - libusb_interface
|
|
458
|
+
* - libusb_interface_descriptor
|
|
459
|
+
* - libusb_iso_packet_descriptor
|
|
460
|
+
* - libusb_pollfd
|
|
461
|
+
* - libusb_ss_endpoint_companion_descriptor
|
|
462
|
+
* - libusb_ss_usb_device_capability_descriptor
|
|
463
|
+
* - libusb_transfer
|
|
464
|
+
* - libusb_usb_2_0_extension_descriptor
|
|
465
|
+
* - libusb_version
|
|
466
|
+
*
|
|
467
|
+
* \section Enums
|
|
468
|
+
* - \ref libusb_bos_type
|
|
469
|
+
* - \ref libusb_capability
|
|
470
|
+
* - \ref libusb_class_code
|
|
471
|
+
* - \ref libusb_descriptor_type
|
|
472
|
+
* - \ref libusb_endpoint_direction
|
|
473
|
+
* - \ref libusb_error
|
|
474
|
+
* - \ref libusb_iso_sync_type
|
|
475
|
+
* - \ref libusb_iso_usage_type
|
|
476
|
+
* - \ref libusb_log_level
|
|
477
|
+
* - \ref libusb_option
|
|
478
|
+
* - \ref libusb_request_recipient
|
|
479
|
+
* - \ref libusb_request_type
|
|
480
|
+
* - \ref libusb_speed
|
|
481
|
+
* - \ref libusb_ss_usb_device_capability_attributes
|
|
482
|
+
* - \ref libusb_standard_request
|
|
483
|
+
* - \ref libusb_supported_speed
|
|
484
|
+
* - \ref libusb_transfer_flags
|
|
485
|
+
* - \ref libusb_transfer_status
|
|
486
|
+
* - \ref libusb_transfer_type
|
|
487
|
+
* - \ref libusb_usb_2_0_extension_attributes
|
|
488
|
+
*/
|
|
489
|
+
|
|
326
490
|
/**
|
|
327
|
-
* @defgroup
|
|
491
|
+
* @defgroup libusb_lib Library initialization/deinitialization
|
|
328
492
|
* This page details how to initialize and deinitialize libusb. Initialization
|
|
329
493
|
* must be performed before using any libusb functionality, and similarly you
|
|
330
494
|
* must not call any libusb functions after deinitialization.
|
|
331
495
|
*/
|
|
332
496
|
|
|
333
497
|
/**
|
|
334
|
-
* @defgroup
|
|
498
|
+
* @defgroup libusb_dev Device handling and enumeration
|
|
335
499
|
* The functionality documented below is designed to help with the following
|
|
336
500
|
* operations:
|
|
337
501
|
* - Enumerating the USB devices currently attached to the system
|
|
@@ -441,7 +605,7 @@ libusb_free_device_list(list, 1);
|
|
|
441
605
|
* libusb_close().
|
|
442
606
|
*/
|
|
443
607
|
|
|
444
|
-
/** @defgroup
|
|
608
|
+
/** @defgroup libusb_misc Miscellaneous */
|
|
445
609
|
|
|
446
610
|
/* we traverse usbfs without knowing how many devices we are going to find.
|
|
447
611
|
* so we create this discovered_devs model which is similar to a linked-list
|
|
@@ -462,6 +626,16 @@ static struct discovered_devs *discovered_devs_alloc(void)
|
|
|
462
626
|
return ret;
|
|
463
627
|
}
|
|
464
628
|
|
|
629
|
+
static void discovered_devs_free(struct discovered_devs *discdevs)
|
|
630
|
+
{
|
|
631
|
+
size_t i;
|
|
632
|
+
|
|
633
|
+
for (i = 0; i < discdevs->len; i++)
|
|
634
|
+
libusb_unref_device(discdevs->devices[i]);
|
|
635
|
+
|
|
636
|
+
free(discdevs);
|
|
637
|
+
}
|
|
638
|
+
|
|
465
639
|
/* append a device to the discovered devices collection. may realloc itself,
|
|
466
640
|
* returning new discdevs. returns NULL on realloc failure. */
|
|
467
641
|
struct discovered_devs *discovered_devs_append(
|
|
@@ -469,6 +643,7 @@ struct discovered_devs *discovered_devs_append(
|
|
|
469
643
|
{
|
|
470
644
|
size_t len = discdevs->len;
|
|
471
645
|
size_t capacity;
|
|
646
|
+
struct discovered_devs *new_discdevs;
|
|
472
647
|
|
|
473
648
|
/* if there is space, just append the device */
|
|
474
649
|
if (len < discdevs->capacity) {
|
|
@@ -480,25 +655,21 @@ struct discovered_devs *discovered_devs_append(
|
|
|
480
655
|
/* exceeded capacity, need to grow */
|
|
481
656
|
usbi_dbg("need to increase capacity");
|
|
482
657
|
capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;
|
|
483
|
-
|
|
658
|
+
/* can't use usbi_reallocf here because in failure cases it would
|
|
659
|
+
* free the existing discdevs without unreferencing its devices. */
|
|
660
|
+
new_discdevs = realloc(discdevs,
|
|
484
661
|
sizeof(*discdevs) + (sizeof(void *) * capacity));
|
|
485
|
-
if (
|
|
486
|
-
discdevs
|
|
487
|
-
|
|
488
|
-
discdevs->len++;
|
|
662
|
+
if (!new_discdevs) {
|
|
663
|
+
discovered_devs_free(discdevs);
|
|
664
|
+
return NULL;
|
|
489
665
|
}
|
|
490
666
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
{
|
|
496
|
-
size_t i;
|
|
497
|
-
|
|
498
|
-
for (i = 0; i < discdevs->len; i++)
|
|
499
|
-
libusb_unref_device(discdevs->devices[i]);
|
|
667
|
+
discdevs = new_discdevs;
|
|
668
|
+
discdevs->capacity = capacity;
|
|
669
|
+
discdevs->devices[len] = libusb_ref_device(dev);
|
|
670
|
+
discdevs->len++;
|
|
500
671
|
|
|
501
|
-
|
|
672
|
+
return discdevs;
|
|
502
673
|
}
|
|
503
674
|
|
|
504
675
|
/* Allocate a new device with a specific session ID. The returned device has
|
|
@@ -506,14 +677,14 @@ static void discovered_devs_free(struct discovered_devs *discdevs)
|
|
|
506
677
|
struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
|
|
507
678
|
unsigned long session_id)
|
|
508
679
|
{
|
|
509
|
-
size_t priv_size = usbi_backend
|
|
680
|
+
size_t priv_size = usbi_backend.device_priv_size;
|
|
510
681
|
struct libusb_device *dev = calloc(1, sizeof(*dev) + priv_size);
|
|
511
682
|
int r;
|
|
512
683
|
|
|
513
684
|
if (!dev)
|
|
514
685
|
return NULL;
|
|
515
686
|
|
|
516
|
-
r = usbi_mutex_init(&dev->lock
|
|
687
|
+
r = usbi_mutex_init(&dev->lock);
|
|
517
688
|
if (r) {
|
|
518
689
|
free(dev);
|
|
519
690
|
return NULL;
|
|
@@ -533,12 +704,8 @@ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
|
|
|
533
704
|
|
|
534
705
|
void usbi_connect_device(struct libusb_device *dev)
|
|
535
706
|
{
|
|
536
|
-
|
|
537
|
-
ssize_t ret;
|
|
707
|
+
struct libusb_context *ctx = DEVICE_CTX(dev);
|
|
538
708
|
|
|
539
|
-
memset(&message, 0, sizeof(message));
|
|
540
|
-
message.event = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED;
|
|
541
|
-
message.device = dev;
|
|
542
709
|
dev->attached = 1;
|
|
543
710
|
|
|
544
711
|
usbi_mutex_lock(&dev->ctx->usb_devs_lock);
|
|
@@ -546,25 +713,17 @@ void usbi_connect_device(struct libusb_device *dev)
|
|
|
546
713
|
usbi_mutex_unlock(&dev->ctx->usb_devs_lock);
|
|
547
714
|
|
|
548
715
|
/* Signal that an event has occurred for this device if we support hotplug AND
|
|
549
|
-
* the hotplug
|
|
550
|
-
* initial enumeration. */
|
|
551
|
-
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->
|
|
552
|
-
|
|
553
|
-
if (sizeof (message) != ret) {
|
|
554
|
-
usbi_err(DEVICE_CTX(dev), "error writing hotplug message");
|
|
555
|
-
}
|
|
716
|
+
* the hotplug message list is ready. This prevents an event from getting raised
|
|
717
|
+
* during initial enumeration. */
|
|
718
|
+
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->hotplug_msgs.next) {
|
|
719
|
+
usbi_hotplug_notification(ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED);
|
|
556
720
|
}
|
|
557
721
|
}
|
|
558
722
|
|
|
559
723
|
void usbi_disconnect_device(struct libusb_device *dev)
|
|
560
724
|
{
|
|
561
|
-
|
|
562
|
-
struct libusb_context *ctx = dev->ctx;
|
|
563
|
-
ssize_t ret;
|
|
725
|
+
struct libusb_context *ctx = DEVICE_CTX(dev);
|
|
564
726
|
|
|
565
|
-
memset(&message, 0, sizeof(message));
|
|
566
|
-
message.event = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
|
|
567
|
-
message.device = dev;
|
|
568
727
|
usbi_mutex_lock(&dev->lock);
|
|
569
728
|
dev->attached = 0;
|
|
570
729
|
usbi_mutex_unlock(&dev->lock);
|
|
@@ -574,14 +733,11 @@ void usbi_disconnect_device(struct libusb_device *dev)
|
|
|
574
733
|
usbi_mutex_unlock(&ctx->usb_devs_lock);
|
|
575
734
|
|
|
576
735
|
/* Signal that an event has occurred for this device if we support hotplug AND
|
|
577
|
-
* the hotplug
|
|
578
|
-
* initial enumeration. libusb_handle_events will take care of dereferencing
|
|
579
|
-
* device. */
|
|
580
|
-
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->
|
|
581
|
-
|
|
582
|
-
if (sizeof(message) != ret) {
|
|
583
|
-
usbi_err(DEVICE_CTX(dev), "error writing hotplug message");
|
|
584
|
-
}
|
|
736
|
+
* the hotplug message list is ready. This prevents an event from getting raised
|
|
737
|
+
* during initial enumeration. libusb_handle_events will take care of dereferencing
|
|
738
|
+
* the device. */
|
|
739
|
+
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->hotplug_msgs.next) {
|
|
740
|
+
usbi_hotplug_notification(ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT);
|
|
585
741
|
}
|
|
586
742
|
}
|
|
587
743
|
|
|
@@ -628,7 +784,7 @@ struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
|
|
|
628
784
|
return ret;
|
|
629
785
|
}
|
|
630
786
|
|
|
631
|
-
/** @ingroup
|
|
787
|
+
/** @ingroup libusb_dev
|
|
632
788
|
* Returns a list of USB devices currently attached to the system. This is
|
|
633
789
|
* your entry point into finding a USB device to operate.
|
|
634
790
|
*
|
|
@@ -665,8 +821,8 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
|
|
665
821
|
/* backend provides hotplug support */
|
|
666
822
|
struct libusb_device *dev;
|
|
667
823
|
|
|
668
|
-
if (usbi_backend
|
|
669
|
-
usbi_backend
|
|
824
|
+
if (usbi_backend.hotplug_poll)
|
|
825
|
+
usbi_backend.hotplug_poll();
|
|
670
826
|
|
|
671
827
|
usbi_mutex_lock(&ctx->usb_devs_lock);
|
|
672
828
|
list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device) {
|
|
@@ -680,7 +836,7 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
|
|
680
836
|
usbi_mutex_unlock(&ctx->usb_devs_lock);
|
|
681
837
|
} else {
|
|
682
838
|
/* backend does not provide hotplug support */
|
|
683
|
-
r = usbi_backend
|
|
839
|
+
r = usbi_backend.get_device_list(ctx, &discdevs);
|
|
684
840
|
}
|
|
685
841
|
|
|
686
842
|
if (r < 0) {
|
|
@@ -689,8 +845,8 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
|
|
689
845
|
}
|
|
690
846
|
|
|
691
847
|
/* convert discovered_devs into a list */
|
|
692
|
-
len = discdevs->len;
|
|
693
|
-
ret = calloc(len + 1, sizeof(struct libusb_device *));
|
|
848
|
+
len = (ssize_t)discdevs->len;
|
|
849
|
+
ret = calloc((size_t)len + 1, sizeof(struct libusb_device *));
|
|
694
850
|
if (!ret) {
|
|
695
851
|
len = LIBUSB_ERROR_NO_MEM;
|
|
696
852
|
goto out;
|
|
@@ -704,11 +860,12 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
|
|
704
860
|
*list = ret;
|
|
705
861
|
|
|
706
862
|
out:
|
|
707
|
-
|
|
863
|
+
if (discdevs)
|
|
864
|
+
discovered_devs_free(discdevs);
|
|
708
865
|
return len;
|
|
709
866
|
}
|
|
710
867
|
|
|
711
|
-
/** \ingroup
|
|
868
|
+
/** \ingroup libusb_dev
|
|
712
869
|
* Frees a list of devices previously discovered using
|
|
713
870
|
* libusb_get_device_list(). If the unref_devices parameter is set, the
|
|
714
871
|
* reference count of each device in the list is decremented by 1.
|
|
@@ -731,7 +888,7 @@ void API_EXPORTED libusb_free_device_list(libusb_device **list,
|
|
|
731
888
|
free(list);
|
|
732
889
|
}
|
|
733
890
|
|
|
734
|
-
/** \ingroup
|
|
891
|
+
/** \ingroup libusb_dev
|
|
735
892
|
* Get the number of the bus that a device is connected to.
|
|
736
893
|
* \param dev a device
|
|
737
894
|
* \returns the bus number
|
|
@@ -741,7 +898,7 @@ uint8_t API_EXPORTED libusb_get_bus_number(libusb_device *dev)
|
|
|
741
898
|
return dev->bus_number;
|
|
742
899
|
}
|
|
743
900
|
|
|
744
|
-
/** \ingroup
|
|
901
|
+
/** \ingroup libusb_dev
|
|
745
902
|
* Get the number of the port that a device is connected to.
|
|
746
903
|
* Unless the OS does something funky, or you are hot-plugging USB extension cards,
|
|
747
904
|
* the port number returned by this call is usually guaranteed to be uniquely tied
|
|
@@ -760,7 +917,7 @@ uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
|
|
|
760
917
|
return dev->port_number;
|
|
761
918
|
}
|
|
762
919
|
|
|
763
|
-
/** \ingroup
|
|
920
|
+
/** \ingroup libusb_dev
|
|
764
921
|
* Get the list of all port numbers from root for the specified device
|
|
765
922
|
*
|
|
766
923
|
* Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
|
|
@@ -794,7 +951,7 @@ int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
|
|
|
794
951
|
return port_numbers_len - i;
|
|
795
952
|
}
|
|
796
953
|
|
|
797
|
-
/** \ingroup
|
|
954
|
+
/** \ingroup libusb_dev
|
|
798
955
|
* Deprecated please use libusb_get_port_numbers instead.
|
|
799
956
|
*/
|
|
800
957
|
int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev,
|
|
@@ -805,7 +962,7 @@ int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev,
|
|
|
805
962
|
return libusb_get_port_numbers(dev, port_numbers, port_numbers_len);
|
|
806
963
|
}
|
|
807
964
|
|
|
808
|
-
/** \ingroup
|
|
965
|
+
/** \ingroup libusb_dev
|
|
809
966
|
* Get the the parent from the specified device.
|
|
810
967
|
* \param dev a device
|
|
811
968
|
* \returns the device parent or NULL if not available
|
|
@@ -822,7 +979,7 @@ libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev)
|
|
|
822
979
|
return dev->parent_dev;
|
|
823
980
|
}
|
|
824
981
|
|
|
825
|
-
/** \ingroup
|
|
982
|
+
/** \ingroup libusb_dev
|
|
826
983
|
* Get the address of the device on the bus it is connected to.
|
|
827
984
|
* \param dev a device
|
|
828
985
|
* \returns the device address
|
|
@@ -832,7 +989,7 @@ uint8_t API_EXPORTED libusb_get_device_address(libusb_device *dev)
|
|
|
832
989
|
return dev->device_address;
|
|
833
990
|
}
|
|
834
991
|
|
|
835
|
-
/** \ingroup
|
|
992
|
+
/** \ingroup libusb_dev
|
|
836
993
|
* Get the negotiated connection speed for a device.
|
|
837
994
|
* \param dev a device
|
|
838
995
|
* \returns a \ref libusb_speed code, where LIBUSB_SPEED_UNKNOWN means that
|
|
@@ -868,7 +1025,7 @@ static const struct libusb_endpoint_descriptor *find_endpoint(
|
|
|
868
1025
|
return NULL;
|
|
869
1026
|
}
|
|
870
1027
|
|
|
871
|
-
/** \ingroup
|
|
1028
|
+
/** \ingroup libusb_dev
|
|
872
1029
|
* Convenience function to retrieve the wMaxPacketSize value for a particular
|
|
873
1030
|
* endpoint in the active device configuration.
|
|
874
1031
|
*
|
|
@@ -911,7 +1068,7 @@ out:
|
|
|
911
1068
|
return r;
|
|
912
1069
|
}
|
|
913
1070
|
|
|
914
|
-
/** \ingroup
|
|
1071
|
+
/** \ingroup libusb_dev
|
|
915
1072
|
* Calculate the maximum packet size which a specific endpoint is capable is
|
|
916
1073
|
* sending or receiving in the duration of 1 microframe
|
|
917
1074
|
*
|
|
@@ -922,7 +1079,9 @@ out:
|
|
|
922
1079
|
* If acting on an isochronous or interrupt endpoint, this function will
|
|
923
1080
|
* multiply the value found in bits 0:10 by the number of transactions per
|
|
924
1081
|
* microframe (determined by bits 11:12). Otherwise, this function just
|
|
925
|
-
* returns the numeric value found in bits 0:10.
|
|
1082
|
+
* returns the numeric value found in bits 0:10. For USB 3.0 device, it
|
|
1083
|
+
* will attempts to retrieve the Endpoint Companion Descriptor to return
|
|
1084
|
+
* wBytesPerInterval.
|
|
926
1085
|
*
|
|
927
1086
|
* This function is useful for setting up isochronous transfers, for example
|
|
928
1087
|
* you might pass the return value from this function to
|
|
@@ -942,9 +1101,11 @@ int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
|
|
|
942
1101
|
{
|
|
943
1102
|
struct libusb_config_descriptor *config;
|
|
944
1103
|
const struct libusb_endpoint_descriptor *ep;
|
|
1104
|
+
struct libusb_ss_endpoint_companion_descriptor *ss_ep_cmp;
|
|
945
1105
|
enum libusb_transfer_type ep_type;
|
|
946
1106
|
uint16_t val;
|
|
947
1107
|
int r;
|
|
1108
|
+
int speed;
|
|
948
1109
|
|
|
949
1110
|
r = libusb_get_active_config_descriptor(dev, &config);
|
|
950
1111
|
if (r < 0) {
|
|
@@ -959,20 +1120,32 @@ int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
|
|
|
959
1120
|
goto out;
|
|
960
1121
|
}
|
|
961
1122
|
|
|
962
|
-
|
|
963
|
-
|
|
1123
|
+
speed = libusb_get_device_speed( dev );
|
|
1124
|
+
if (speed == LIBUSB_SPEED_SUPER) {
|
|
1125
|
+
r = libusb_get_ss_endpoint_companion_descriptor(dev->ctx, ep, &ss_ep_cmp);
|
|
1126
|
+
if (r == LIBUSB_SUCCESS) {
|
|
1127
|
+
r = ss_ep_cmp->wBytesPerInterval;
|
|
1128
|
+
libusb_free_ss_endpoint_companion_descriptor(ss_ep_cmp);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
964
1131
|
|
|
965
|
-
|
|
966
|
-
if (
|
|
967
|
-
|
|
968
|
-
|
|
1132
|
+
/* If the device isn't a SuperSpeed device or retrieving the SS endpoint didn't worked. */
|
|
1133
|
+
if (speed != LIBUSB_SPEED_SUPER || r < 0) {
|
|
1134
|
+
val = ep->wMaxPacketSize;
|
|
1135
|
+
ep_type = (enum libusb_transfer_type) (ep->bmAttributes & 0x3);
|
|
1136
|
+
|
|
1137
|
+
r = val & 0x07ff;
|
|
1138
|
+
if (ep_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
|
|
1139
|
+
|| ep_type == LIBUSB_TRANSFER_TYPE_INTERRUPT)
|
|
1140
|
+
r *= (1 + ((val >> 11) & 3));
|
|
1141
|
+
}
|
|
969
1142
|
|
|
970
1143
|
out:
|
|
971
1144
|
libusb_free_config_descriptor(config);
|
|
972
1145
|
return r;
|
|
973
1146
|
}
|
|
974
1147
|
|
|
975
|
-
/** \ingroup
|
|
1148
|
+
/** \ingroup libusb_dev
|
|
976
1149
|
* Increment the reference count of a device.
|
|
977
1150
|
* \param dev the device to reference
|
|
978
1151
|
* \returns the same device
|
|
@@ -986,7 +1159,7 @@ libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev)
|
|
|
986
1159
|
return dev;
|
|
987
1160
|
}
|
|
988
1161
|
|
|
989
|
-
/** \ingroup
|
|
1162
|
+
/** \ingroup libusb_dev
|
|
990
1163
|
* Decrement the reference count of a device. If the decrement operation
|
|
991
1164
|
* causes the reference count to reach zero, the device shall be destroyed.
|
|
992
1165
|
* \param dev the device to unreference
|
|
@@ -1007,8 +1180,8 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev)
|
|
|
1007
1180
|
|
|
1008
1181
|
libusb_unref_device(dev->parent_dev);
|
|
1009
1182
|
|
|
1010
|
-
if (usbi_backend
|
|
1011
|
-
usbi_backend
|
|
1183
|
+
if (usbi_backend.destroy_device)
|
|
1184
|
+
usbi_backend.destroy_device(dev);
|
|
1012
1185
|
|
|
1013
1186
|
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
|
1014
1187
|
/* backend does not support hotplug */
|
|
@@ -1021,50 +1194,116 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev)
|
|
|
1021
1194
|
}
|
|
1022
1195
|
|
|
1023
1196
|
/*
|
|
1024
|
-
*
|
|
1025
|
-
*
|
|
1197
|
+
* Signal the event pipe so that the event handling thread will be
|
|
1198
|
+
* interrupted to process an internal event.
|
|
1026
1199
|
*/
|
|
1027
|
-
|
|
1200
|
+
int usbi_signal_event(struct libusb_context *ctx)
|
|
1028
1201
|
{
|
|
1029
1202
|
unsigned char dummy = 1;
|
|
1030
1203
|
ssize_t r;
|
|
1031
1204
|
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
/* record that we are messing with poll fds */
|
|
1036
|
-
usbi_mutex_lock(&ctx->pollfd_modify_lock);
|
|
1037
|
-
ctx->pollfd_modify++;
|
|
1038
|
-
usbi_mutex_unlock(&ctx->pollfd_modify_lock);
|
|
1039
|
-
|
|
1040
|
-
/* write some data on control pipe to interrupt event handlers */
|
|
1041
|
-
r = usbi_write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
|
|
1042
|
-
if (r <= 0) {
|
|
1205
|
+
/* write some data on event pipe to interrupt event handlers */
|
|
1206
|
+
r = usbi_write(ctx->event_pipe[1], &dummy, sizeof(dummy));
|
|
1207
|
+
if (r != sizeof(dummy)) {
|
|
1043
1208
|
usbi_warn(ctx, "internal signalling write failed");
|
|
1044
|
-
|
|
1045
|
-
ctx->pollfd_modify--;
|
|
1046
|
-
usbi_mutex_unlock(&ctx->pollfd_modify_lock);
|
|
1047
|
-
return;
|
|
1209
|
+
return LIBUSB_ERROR_IO;
|
|
1048
1210
|
}
|
|
1049
1211
|
|
|
1050
|
-
|
|
1051
|
-
|
|
1212
|
+
return 0;
|
|
1213
|
+
}
|
|
1052
1214
|
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1215
|
+
/*
|
|
1216
|
+
* Clear the event pipe so that the event handling will no longer be
|
|
1217
|
+
* interrupted.
|
|
1218
|
+
*/
|
|
1219
|
+
int usbi_clear_event(struct libusb_context *ctx)
|
|
1220
|
+
{
|
|
1221
|
+
unsigned char dummy;
|
|
1222
|
+
ssize_t r;
|
|
1223
|
+
|
|
1224
|
+
/* read some data on event pipe to clear it */
|
|
1225
|
+
r = usbi_read(ctx->event_pipe[0], &dummy, sizeof(dummy));
|
|
1226
|
+
if (r != sizeof(dummy)) {
|
|
1056
1227
|
usbi_warn(ctx, "internal signalling read failed");
|
|
1228
|
+
return LIBUSB_ERROR_IO;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
return 0;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
/** \ingroup libusb_dev
|
|
1235
|
+
* Wrap a platform-specific system device handle and obtain a libusb device
|
|
1236
|
+
* handle for the underlying device. The handle allows you to use libusb to
|
|
1237
|
+
* perform I/O on the device in question.
|
|
1238
|
+
*
|
|
1239
|
+
* On Linux, the system device handle must be a valid file descriptor opened
|
|
1240
|
+
* on the device node.
|
|
1241
|
+
*
|
|
1242
|
+
* The system device handle must remain open until libusb_close() is called.
|
|
1243
|
+
* The system device handle will not be closed by libusb_close().
|
|
1244
|
+
*
|
|
1245
|
+
* Internally, this function creates a temporary device and makes it
|
|
1246
|
+
* available to you through libusb_get_device(). This device is destroyed
|
|
1247
|
+
* during libusb_close(). The device shall not be opened through libusb_open().
|
|
1248
|
+
*
|
|
1249
|
+
* This is a non-blocking function; no requests are sent over the bus.
|
|
1250
|
+
*
|
|
1251
|
+
* \param ctx the context to operate on, or NULL for the default context
|
|
1252
|
+
* \param sys_dev the platform-specific system device handle
|
|
1253
|
+
* \param dev_handle output location for the returned device handle pointer. Only
|
|
1254
|
+
* populated when the return code is 0.
|
|
1255
|
+
* \returns 0 on success
|
|
1256
|
+
* \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
|
|
1257
|
+
* \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions
|
|
1258
|
+
* \returns LIBUSB_ERROR_NOT_SUPPORTED if the operation is not supported on this
|
|
1259
|
+
* platform
|
|
1260
|
+
* \returns another LIBUSB_ERROR code on other failure
|
|
1261
|
+
*/
|
|
1262
|
+
int API_EXPORTED libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev,
|
|
1263
|
+
libusb_device_handle **dev_handle)
|
|
1264
|
+
{
|
|
1265
|
+
struct libusb_device_handle *_dev_handle;
|
|
1266
|
+
size_t priv_size = usbi_backend.device_handle_priv_size;
|
|
1267
|
+
int r;
|
|
1268
|
+
usbi_dbg("wrap_sys_device %p", (void *)sys_dev);
|
|
1269
|
+
|
|
1270
|
+
USBI_GET_CONTEXT(ctx);
|
|
1271
|
+
|
|
1272
|
+
if (!usbi_backend.wrap_sys_device)
|
|
1273
|
+
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1274
|
+
|
|
1275
|
+
_dev_handle = malloc(sizeof(*_dev_handle) + priv_size);
|
|
1276
|
+
if (!_dev_handle)
|
|
1277
|
+
return LIBUSB_ERROR_NO_MEM;
|
|
1278
|
+
|
|
1279
|
+
r = usbi_mutex_init(&_dev_handle->lock);
|
|
1280
|
+
if (r) {
|
|
1281
|
+
free(_dev_handle);
|
|
1282
|
+
return LIBUSB_ERROR_OTHER;
|
|
1283
|
+
}
|
|
1057
1284
|
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1285
|
+
_dev_handle->dev = NULL;
|
|
1286
|
+
_dev_handle->auto_detach_kernel_driver = 0;
|
|
1287
|
+
_dev_handle->claimed_interfaces = 0;
|
|
1288
|
+
memset(&_dev_handle->os_priv, 0, priv_size);
|
|
1062
1289
|
|
|
1063
|
-
|
|
1064
|
-
|
|
1290
|
+
r = usbi_backend.wrap_sys_device(ctx, _dev_handle, sys_dev);
|
|
1291
|
+
if (r < 0) {
|
|
1292
|
+
usbi_dbg("wrap_sys_device %p returns %d", (void *)sys_dev, r);
|
|
1293
|
+
usbi_mutex_destroy(&_dev_handle->lock);
|
|
1294
|
+
free(_dev_handle);
|
|
1295
|
+
return r;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
usbi_mutex_lock(&ctx->open_devs_lock);
|
|
1299
|
+
list_add(&_dev_handle->list, &ctx->open_devs);
|
|
1300
|
+
usbi_mutex_unlock(&ctx->open_devs_lock);
|
|
1301
|
+
*dev_handle = _dev_handle;
|
|
1302
|
+
|
|
1303
|
+
return 0;
|
|
1065
1304
|
}
|
|
1066
1305
|
|
|
1067
|
-
/** \ingroup
|
|
1306
|
+
/** \ingroup libusb_dev
|
|
1068
1307
|
* Open a device and obtain a device handle. A handle allows you to perform
|
|
1069
1308
|
* I/O on the device in question.
|
|
1070
1309
|
*
|
|
@@ -1075,7 +1314,7 @@ void usbi_fd_notification(struct libusb_context *ctx)
|
|
|
1075
1314
|
* This is a non-blocking function; no requests are sent over the bus.
|
|
1076
1315
|
*
|
|
1077
1316
|
* \param dev the device to open
|
|
1078
|
-
* \param
|
|
1317
|
+
* \param dev_handle output location for the returned device handle pointer. Only
|
|
1079
1318
|
* populated when the return code is 0.
|
|
1080
1319
|
* \returns 0 on success
|
|
1081
1320
|
* \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
|
|
@@ -1084,11 +1323,11 @@ void usbi_fd_notification(struct libusb_context *ctx)
|
|
|
1084
1323
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1085
1324
|
*/
|
|
1086
1325
|
int API_EXPORTED libusb_open(libusb_device *dev,
|
|
1087
|
-
libusb_device_handle **
|
|
1326
|
+
libusb_device_handle **dev_handle)
|
|
1088
1327
|
{
|
|
1089
1328
|
struct libusb_context *ctx = DEVICE_CTX(dev);
|
|
1090
|
-
struct libusb_device_handle *
|
|
1091
|
-
size_t priv_size = usbi_backend
|
|
1329
|
+
struct libusb_device_handle *_dev_handle;
|
|
1330
|
+
size_t priv_size = usbi_backend.device_handle_priv_size;
|
|
1092
1331
|
int r;
|
|
1093
1332
|
usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
|
|
1094
1333
|
|
|
@@ -1096,49 +1335,39 @@ int API_EXPORTED libusb_open(libusb_device *dev,
|
|
|
1096
1335
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1097
1336
|
}
|
|
1098
1337
|
|
|
1099
|
-
|
|
1100
|
-
if (!
|
|
1338
|
+
_dev_handle = malloc(sizeof(*_dev_handle) + priv_size);
|
|
1339
|
+
if (!_dev_handle)
|
|
1101
1340
|
return LIBUSB_ERROR_NO_MEM;
|
|
1102
1341
|
|
|
1103
|
-
r = usbi_mutex_init(&
|
|
1342
|
+
r = usbi_mutex_init(&_dev_handle->lock);
|
|
1104
1343
|
if (r) {
|
|
1105
|
-
free(
|
|
1344
|
+
free(_dev_handle);
|
|
1106
1345
|
return LIBUSB_ERROR_OTHER;
|
|
1107
1346
|
}
|
|
1108
1347
|
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
memset(&
|
|
1348
|
+
_dev_handle->dev = libusb_ref_device(dev);
|
|
1349
|
+
_dev_handle->auto_detach_kernel_driver = 0;
|
|
1350
|
+
_dev_handle->claimed_interfaces = 0;
|
|
1351
|
+
memset(&_dev_handle->os_priv, 0, priv_size);
|
|
1113
1352
|
|
|
1114
|
-
r = usbi_backend
|
|
1353
|
+
r = usbi_backend.open(_dev_handle);
|
|
1115
1354
|
if (r < 0) {
|
|
1116
1355
|
usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
|
|
1117
1356
|
libusb_unref_device(dev);
|
|
1118
|
-
usbi_mutex_destroy(&
|
|
1119
|
-
free(
|
|
1357
|
+
usbi_mutex_destroy(&_dev_handle->lock);
|
|
1358
|
+
free(_dev_handle);
|
|
1120
1359
|
return r;
|
|
1121
1360
|
}
|
|
1122
1361
|
|
|
1123
1362
|
usbi_mutex_lock(&ctx->open_devs_lock);
|
|
1124
|
-
list_add(&
|
|
1363
|
+
list_add(&_dev_handle->list, &ctx->open_devs);
|
|
1125
1364
|
usbi_mutex_unlock(&ctx->open_devs_lock);
|
|
1126
|
-
*
|
|
1127
|
-
|
|
1128
|
-
if (usbi_backend->caps & USBI_CAP_HAS_POLLABLE_DEVICE_FD) {
|
|
1129
|
-
/* At this point, we want to interrupt any existing event handlers so
|
|
1130
|
-
* that they realise the addition of the new device's poll fd. One
|
|
1131
|
-
* example when this is desirable is if the user is running a separate
|
|
1132
|
-
* dedicated libusb events handling thread, which is running with a long
|
|
1133
|
-
* or infinite timeout. We want to interrupt that iteration of the loop,
|
|
1134
|
-
* so that it picks up the new fd, and then continues. */
|
|
1135
|
-
usbi_fd_notification(ctx);
|
|
1136
|
-
}
|
|
1365
|
+
*dev_handle = _dev_handle;
|
|
1137
1366
|
|
|
1138
1367
|
return 0;
|
|
1139
1368
|
}
|
|
1140
1369
|
|
|
1141
|
-
/** \ingroup
|
|
1370
|
+
/** \ingroup libusb_dev
|
|
1142
1371
|
* Convenience function for finding a device with a particular
|
|
1143
1372
|
* <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended
|
|
1144
1373
|
* for those scenarios where you are using libusb to knock up a quick test
|
|
@@ -1152,8 +1381,8 @@ int API_EXPORTED libusb_open(libusb_device *dev,
|
|
|
1152
1381
|
* \param ctx the context to operate on, or NULL for the default context
|
|
1153
1382
|
* \param vendor_id the idVendor value to search for
|
|
1154
1383
|
* \param product_id the idProduct value to search for
|
|
1155
|
-
* \returns a handle for the first found device, or NULL on error
|
|
1156
|
-
* device could not be found. */
|
|
1384
|
+
* \returns a device handle for the first found device, or NULL on error
|
|
1385
|
+
* or if the device could not be found. */
|
|
1157
1386
|
DEFAULT_VISIBILITY
|
|
1158
1387
|
libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
|
|
1159
1388
|
libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
|
|
@@ -1161,7 +1390,7 @@ libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
|
|
|
1161
1390
|
struct libusb_device **devs;
|
|
1162
1391
|
struct libusb_device *found = NULL;
|
|
1163
1392
|
struct libusb_device *dev;
|
|
1164
|
-
struct libusb_device_handle *
|
|
1393
|
+
struct libusb_device_handle *dev_handle = NULL;
|
|
1165
1394
|
size_t i = 0;
|
|
1166
1395
|
int r;
|
|
1167
1396
|
|
|
@@ -1180,14 +1409,14 @@ libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
|
|
|
1180
1409
|
}
|
|
1181
1410
|
|
|
1182
1411
|
if (found) {
|
|
1183
|
-
r = libusb_open(found, &
|
|
1412
|
+
r = libusb_open(found, &dev_handle);
|
|
1184
1413
|
if (r < 0)
|
|
1185
|
-
|
|
1414
|
+
dev_handle = NULL;
|
|
1186
1415
|
}
|
|
1187
1416
|
|
|
1188
1417
|
out:
|
|
1189
1418
|
libusb_free_device_list(devs, 1);
|
|
1190
|
-
return
|
|
1419
|
+
return dev_handle;
|
|
1191
1420
|
}
|
|
1192
1421
|
|
|
1193
1422
|
static void do_close(struct libusb_context *ctx,
|
|
@@ -1196,8 +1425,6 @@ static void do_close(struct libusb_context *ctx,
|
|
|
1196
1425
|
struct usbi_transfer *itransfer;
|
|
1197
1426
|
struct usbi_transfer *tmp;
|
|
1198
1427
|
|
|
1199
|
-
libusb_lock_events(ctx);
|
|
1200
|
-
|
|
1201
1428
|
/* remove any transfers in flight that are for this device */
|
|
1202
1429
|
usbi_mutex_lock(&ctx->flying_transfers_lock);
|
|
1203
1430
|
|
|
@@ -1209,23 +1436,23 @@ static void do_close(struct libusb_context *ctx,
|
|
|
1209
1436
|
if (transfer->dev_handle != dev_handle)
|
|
1210
1437
|
continue;
|
|
1211
1438
|
|
|
1212
|
-
|
|
1439
|
+
usbi_mutex_lock(&itransfer->lock);
|
|
1440
|
+
if (!(itransfer->state_flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) {
|
|
1213
1441
|
usbi_err(ctx, "Device handle closed while transfer was still being processed, but the device is still connected as far as we know");
|
|
1214
1442
|
|
|
1215
|
-
if (itransfer->
|
|
1443
|
+
if (itransfer->state_flags & USBI_TRANSFER_CANCELLING)
|
|
1216
1444
|
usbi_warn(ctx, "A cancellation for an in-flight transfer hasn't completed but closing the device handle");
|
|
1217
1445
|
else
|
|
1218
1446
|
usbi_err(ctx, "A cancellation hasn't even been scheduled on the transfer for which the device is closing");
|
|
1219
1447
|
}
|
|
1448
|
+
usbi_mutex_unlock(&itransfer->lock);
|
|
1220
1449
|
|
|
1221
1450
|
/* remove from the list of in-flight transfers and make sure
|
|
1222
1451
|
* we don't accidentally use the device handle in the future
|
|
1223
1452
|
* (or that such accesses will be easily caught and identified as a crash)
|
|
1224
1453
|
*/
|
|
1225
|
-
usbi_mutex_lock(&itransfer->lock);
|
|
1226
1454
|
list_del(&itransfer->list);
|
|
1227
1455
|
transfer->dev_handle = NULL;
|
|
1228
|
-
usbi_mutex_unlock(&itransfer->lock);
|
|
1229
1456
|
|
|
1230
1457
|
/* it is up to the user to free up the actual transfer struct. this is
|
|
1231
1458
|
* just making sure that we don't attempt to process the transfer after
|
|
@@ -1236,19 +1463,17 @@ static void do_close(struct libusb_context *ctx,
|
|
|
1236
1463
|
}
|
|
1237
1464
|
usbi_mutex_unlock(&ctx->flying_transfers_lock);
|
|
1238
1465
|
|
|
1239
|
-
libusb_unlock_events(ctx);
|
|
1240
|
-
|
|
1241
1466
|
usbi_mutex_lock(&ctx->open_devs_lock);
|
|
1242
1467
|
list_del(&dev_handle->list);
|
|
1243
1468
|
usbi_mutex_unlock(&ctx->open_devs_lock);
|
|
1244
1469
|
|
|
1245
|
-
usbi_backend
|
|
1470
|
+
usbi_backend.close(dev_handle);
|
|
1246
1471
|
libusb_unref_device(dev_handle->dev);
|
|
1247
1472
|
usbi_mutex_destroy(&dev_handle->lock);
|
|
1248
1473
|
free(dev_handle);
|
|
1249
1474
|
}
|
|
1250
1475
|
|
|
1251
|
-
/** \ingroup
|
|
1476
|
+
/** \ingroup libusb_dev
|
|
1252
1477
|
* Close a device handle. Should be called on all open handles before your
|
|
1253
1478
|
* application exits.
|
|
1254
1479
|
*
|
|
@@ -1257,64 +1482,63 @@ static void do_close(struct libusb_context *ctx,
|
|
|
1257
1482
|
*
|
|
1258
1483
|
* This is a non-blocking function; no requests are sent over the bus.
|
|
1259
1484
|
*
|
|
1260
|
-
* \param dev_handle the handle to close
|
|
1485
|
+
* \param dev_handle the device handle to close
|
|
1261
1486
|
*/
|
|
1262
1487
|
void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
|
|
1263
1488
|
{
|
|
1264
1489
|
struct libusb_context *ctx;
|
|
1265
|
-
|
|
1266
|
-
|
|
1490
|
+
int handling_events;
|
|
1491
|
+
int pending_events;
|
|
1267
1492
|
|
|
1268
1493
|
if (!dev_handle)
|
|
1269
1494
|
return;
|
|
1270
1495
|
usbi_dbg("");
|
|
1271
1496
|
|
|
1272
1497
|
ctx = HANDLE_CTX(dev_handle);
|
|
1498
|
+
handling_events = usbi_handling_events(ctx);
|
|
1273
1499
|
|
|
1274
1500
|
/* Similarly to libusb_open(), we want to interrupt all event handlers
|
|
1275
1501
|
* at this point. More importantly, we want to perform the actual close of
|
|
1276
1502
|
* the device while holding the event handling lock (preventing any other
|
|
1277
1503
|
* thread from doing event handling) because we will be removing a file
|
|
1278
|
-
* descriptor from the polling loop.
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1504
|
+
* descriptor from the polling loop. If this is being called by the current
|
|
1505
|
+
* event handler, we can bypass the interruption code because we already
|
|
1506
|
+
* hold the event handling lock. */
|
|
1507
|
+
|
|
1508
|
+
if (!handling_events) {
|
|
1509
|
+
/* Record that we are closing a device.
|
|
1510
|
+
* Only signal an event if there are no prior pending events. */
|
|
1511
|
+
usbi_mutex_lock(&ctx->event_data_lock);
|
|
1512
|
+
pending_events = usbi_pending_events(ctx);
|
|
1513
|
+
ctx->device_close++;
|
|
1514
|
+
if (!pending_events)
|
|
1515
|
+
usbi_signal_event(ctx);
|
|
1516
|
+
usbi_mutex_unlock(&ctx->event_data_lock);
|
|
1517
|
+
|
|
1518
|
+
/* take event handling lock */
|
|
1519
|
+
libusb_lock_events(ctx);
|
|
1294
1520
|
}
|
|
1295
1521
|
|
|
1296
|
-
/* take event handling lock */
|
|
1297
|
-
libusb_lock_events(ctx);
|
|
1298
|
-
|
|
1299
|
-
/* read the dummy data */
|
|
1300
|
-
r = usbi_read(ctx->ctrl_pipe[0], &dummy, sizeof(dummy));
|
|
1301
|
-
if (r <= 0)
|
|
1302
|
-
usbi_warn(ctx, "internal signalling read failed, closing anyway");
|
|
1303
|
-
|
|
1304
1522
|
/* Close the device */
|
|
1305
1523
|
do_close(ctx, dev_handle);
|
|
1306
1524
|
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1525
|
+
if (!handling_events) {
|
|
1526
|
+
/* We're done with closing this device.
|
|
1527
|
+
* Clear the event pipe if there are no further pending events. */
|
|
1528
|
+
usbi_mutex_lock(&ctx->event_data_lock);
|
|
1529
|
+
ctx->device_close--;
|
|
1530
|
+
pending_events = usbi_pending_events(ctx);
|
|
1531
|
+
if (!pending_events)
|
|
1532
|
+
usbi_clear_event(ctx);
|
|
1533
|
+
usbi_mutex_unlock(&ctx->event_data_lock);
|
|
1534
|
+
|
|
1535
|
+
/* Release event handling lock and wake up event waiters */
|
|
1536
|
+
libusb_unlock_events(ctx);
|
|
1537
|
+
}
|
|
1314
1538
|
}
|
|
1315
1539
|
|
|
1316
|
-
/** \ingroup
|
|
1317
|
-
* Get the underlying device for a handle. This function does not modify
|
|
1540
|
+
/** \ingroup libusb_dev
|
|
1541
|
+
* Get the underlying device for a device handle. This function does not modify
|
|
1318
1542
|
* the reference count of the returned device, so do not feel compelled to
|
|
1319
1543
|
* unreference it when you are done.
|
|
1320
1544
|
* \param dev_handle a device handle
|
|
@@ -1326,7 +1550,7 @@ libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle)
|
|
|
1326
1550
|
return dev_handle->dev;
|
|
1327
1551
|
}
|
|
1328
1552
|
|
|
1329
|
-
/** \ingroup
|
|
1553
|
+
/** \ingroup libusb_dev
|
|
1330
1554
|
* Determine the bConfigurationValue of the currently active configuration.
|
|
1331
1555
|
*
|
|
1332
1556
|
* You could formulate your own control request to obtain this information,
|
|
@@ -1339,29 +1563,29 @@ libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle)
|
|
|
1339
1563
|
* This function will return a value of 0 in the <tt>config</tt> output
|
|
1340
1564
|
* parameter if the device is in unconfigured state.
|
|
1341
1565
|
*
|
|
1342
|
-
* \param
|
|
1566
|
+
* \param dev_handle a device handle
|
|
1343
1567
|
* \param config output location for the bConfigurationValue of the active
|
|
1344
1568
|
* configuration (only valid for return code 0)
|
|
1345
1569
|
* \returns 0 on success
|
|
1346
1570
|
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
|
1347
1571
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1348
1572
|
*/
|
|
1349
|
-
int API_EXPORTED libusb_get_configuration(libusb_device_handle *
|
|
1573
|
+
int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev_handle,
|
|
1350
1574
|
int *config)
|
|
1351
1575
|
{
|
|
1352
1576
|
int r = LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1353
1577
|
|
|
1354
1578
|
usbi_dbg("");
|
|
1355
|
-
if (usbi_backend
|
|
1356
|
-
r = usbi_backend
|
|
1579
|
+
if (usbi_backend.get_configuration)
|
|
1580
|
+
r = usbi_backend.get_configuration(dev_handle, config);
|
|
1357
1581
|
|
|
1358
1582
|
if (r == LIBUSB_ERROR_NOT_SUPPORTED) {
|
|
1359
1583
|
uint8_t tmp = 0;
|
|
1360
1584
|
usbi_dbg("falling back to control message");
|
|
1361
|
-
r = libusb_control_transfer(
|
|
1585
|
+
r = libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN,
|
|
1362
1586
|
LIBUSB_REQUEST_GET_CONFIGURATION, 0, 0, &tmp, 1, 1000);
|
|
1363
1587
|
if (r == 0) {
|
|
1364
|
-
usbi_err(HANDLE_CTX(
|
|
1588
|
+
usbi_err(HANDLE_CTX(dev_handle), "zero bytes returned in ctrl transfer?");
|
|
1365
1589
|
r = LIBUSB_ERROR_IO;
|
|
1366
1590
|
} else if (r == 1) {
|
|
1367
1591
|
r = 0;
|
|
@@ -1377,7 +1601,7 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev,
|
|
|
1377
1601
|
return r;
|
|
1378
1602
|
}
|
|
1379
1603
|
|
|
1380
|
-
/** \ingroup
|
|
1604
|
+
/** \ingroup libusb_dev
|
|
1381
1605
|
* Set the active configuration for a device.
|
|
1382
1606
|
*
|
|
1383
1607
|
* The operating system may or may not have already set an active
|
|
@@ -1413,9 +1637,10 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev,
|
|
|
1413
1637
|
*
|
|
1414
1638
|
* This is a blocking function.
|
|
1415
1639
|
*
|
|
1416
|
-
* \param
|
|
1640
|
+
* \param dev_handle a device handle
|
|
1417
1641
|
* \param configuration the bConfigurationValue of the configuration you
|
|
1418
|
-
* wish to activate, or -1 if you wish to put the device in unconfigured
|
|
1642
|
+
* wish to activate, or -1 if you wish to put the device in an unconfigured
|
|
1643
|
+
* state
|
|
1419
1644
|
* \returns 0 on success
|
|
1420
1645
|
* \returns LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist
|
|
1421
1646
|
* \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed
|
|
@@ -1423,14 +1648,14 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev,
|
|
|
1423
1648
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1424
1649
|
* \see libusb_set_auto_detach_kernel_driver()
|
|
1425
1650
|
*/
|
|
1426
|
-
int API_EXPORTED libusb_set_configuration(libusb_device_handle *
|
|
1651
|
+
int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev_handle,
|
|
1427
1652
|
int configuration)
|
|
1428
1653
|
{
|
|
1429
1654
|
usbi_dbg("configuration %d", configuration);
|
|
1430
|
-
return usbi_backend
|
|
1655
|
+
return usbi_backend.set_configuration(dev_handle, configuration);
|
|
1431
1656
|
}
|
|
1432
1657
|
|
|
1433
|
-
/** \ingroup
|
|
1658
|
+
/** \ingroup libusb_dev
|
|
1434
1659
|
* Claim an interface on a given device handle. You must claim the interface
|
|
1435
1660
|
* you wish to use before you can perform I/O on any of its endpoints.
|
|
1436
1661
|
*
|
|
@@ -1447,7 +1672,7 @@ int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev,
|
|
|
1447
1672
|
*
|
|
1448
1673
|
* This is a non-blocking function.
|
|
1449
1674
|
*
|
|
1450
|
-
* \param
|
|
1675
|
+
* \param dev_handle a device handle
|
|
1451
1676
|
* \param interface_number the <tt>bInterfaceNumber</tt> of the interface you
|
|
1452
1677
|
* wish to claim
|
|
1453
1678
|
* \returns 0 on success
|
|
@@ -1458,7 +1683,7 @@ int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev,
|
|
|
1458
1683
|
* \returns a LIBUSB_ERROR code on other failure
|
|
1459
1684
|
* \see libusb_set_auto_detach_kernel_driver()
|
|
1460
1685
|
*/
|
|
1461
|
-
int API_EXPORTED libusb_claim_interface(libusb_device_handle *
|
|
1686
|
+
int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev_handle,
|
|
1462
1687
|
int interface_number)
|
|
1463
1688
|
{
|
|
1464
1689
|
int r = 0;
|
|
@@ -1467,23 +1692,23 @@ int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev,
|
|
|
1467
1692
|
if (interface_number >= USB_MAXINTERFACES)
|
|
1468
1693
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
1469
1694
|
|
|
1470
|
-
if (!
|
|
1695
|
+
if (!dev_handle->dev->attached)
|
|
1471
1696
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1472
1697
|
|
|
1473
|
-
usbi_mutex_lock(&
|
|
1474
|
-
if (
|
|
1698
|
+
usbi_mutex_lock(&dev_handle->lock);
|
|
1699
|
+
if (dev_handle->claimed_interfaces & (1U << interface_number))
|
|
1475
1700
|
goto out;
|
|
1476
1701
|
|
|
1477
|
-
r = usbi_backend
|
|
1702
|
+
r = usbi_backend.claim_interface(dev_handle, interface_number);
|
|
1478
1703
|
if (r == 0)
|
|
1479
|
-
|
|
1704
|
+
dev_handle->claimed_interfaces |= 1U << interface_number;
|
|
1480
1705
|
|
|
1481
1706
|
out:
|
|
1482
|
-
usbi_mutex_unlock(&
|
|
1707
|
+
usbi_mutex_unlock(&dev_handle->lock);
|
|
1483
1708
|
return r;
|
|
1484
1709
|
}
|
|
1485
1710
|
|
|
1486
|
-
/** \ingroup
|
|
1711
|
+
/** \ingroup libusb_dev
|
|
1487
1712
|
* Release an interface previously claimed with libusb_claim_interface(). You
|
|
1488
1713
|
* should release all claimed interfaces before closing a device handle.
|
|
1489
1714
|
*
|
|
@@ -1493,7 +1718,7 @@ out:
|
|
|
1493
1718
|
* If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel
|
|
1494
1719
|
* driver will be re-attached after releasing the interface.
|
|
1495
1720
|
*
|
|
1496
|
-
* \param
|
|
1721
|
+
* \param dev_handle a device handle
|
|
1497
1722
|
* \param interface_number the <tt>bInterfaceNumber</tt> of the
|
|
1498
1723
|
* previously-claimed interface
|
|
1499
1724
|
* \returns 0 on success
|
|
@@ -1502,7 +1727,7 @@ out:
|
|
|
1502
1727
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1503
1728
|
* \see libusb_set_auto_detach_kernel_driver()
|
|
1504
1729
|
*/
|
|
1505
|
-
int API_EXPORTED libusb_release_interface(libusb_device_handle *
|
|
1730
|
+
int API_EXPORTED libusb_release_interface(libusb_device_handle *dev_handle,
|
|
1506
1731
|
int interface_number)
|
|
1507
1732
|
{
|
|
1508
1733
|
int r;
|
|
@@ -1511,22 +1736,22 @@ int API_EXPORTED libusb_release_interface(libusb_device_handle *dev,
|
|
|
1511
1736
|
if (interface_number >= USB_MAXINTERFACES)
|
|
1512
1737
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
1513
1738
|
|
|
1514
|
-
usbi_mutex_lock(&
|
|
1515
|
-
if (!(
|
|
1739
|
+
usbi_mutex_lock(&dev_handle->lock);
|
|
1740
|
+
if (!(dev_handle->claimed_interfaces & (1U << interface_number))) {
|
|
1516
1741
|
r = LIBUSB_ERROR_NOT_FOUND;
|
|
1517
1742
|
goto out;
|
|
1518
1743
|
}
|
|
1519
1744
|
|
|
1520
|
-
r = usbi_backend
|
|
1745
|
+
r = usbi_backend.release_interface(dev_handle, interface_number);
|
|
1521
1746
|
if (r == 0)
|
|
1522
|
-
|
|
1747
|
+
dev_handle->claimed_interfaces &= ~(1U << interface_number);
|
|
1523
1748
|
|
|
1524
1749
|
out:
|
|
1525
|
-
usbi_mutex_unlock(&
|
|
1750
|
+
usbi_mutex_unlock(&dev_handle->lock);
|
|
1526
1751
|
return r;
|
|
1527
1752
|
}
|
|
1528
1753
|
|
|
1529
|
-
/** \ingroup
|
|
1754
|
+
/** \ingroup libusb_dev
|
|
1530
1755
|
* Activate an alternate setting for an interface. The interface must have
|
|
1531
1756
|
* been previously claimed with libusb_claim_interface().
|
|
1532
1757
|
*
|
|
@@ -1536,7 +1761,7 @@ out:
|
|
|
1536
1761
|
*
|
|
1537
1762
|
* This is a blocking function.
|
|
1538
1763
|
*
|
|
1539
|
-
* \param
|
|
1764
|
+
* \param dev_handle a device handle
|
|
1540
1765
|
* \param interface_number the <tt>bInterfaceNumber</tt> of the
|
|
1541
1766
|
* previously-claimed interface
|
|
1542
1767
|
* \param alternate_setting the <tt>bAlternateSetting</tt> of the alternate
|
|
@@ -1547,7 +1772,7 @@ out:
|
|
|
1547
1772
|
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
|
1548
1773
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1549
1774
|
*/
|
|
1550
|
-
int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *
|
|
1775
|
+
int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_handle,
|
|
1551
1776
|
int interface_number, int alternate_setting)
|
|
1552
1777
|
{
|
|
1553
1778
|
usbi_dbg("interface %d altsetting %d",
|
|
@@ -1555,23 +1780,23 @@ int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev,
|
|
|
1555
1780
|
if (interface_number >= USB_MAXINTERFACES)
|
|
1556
1781
|
return LIBUSB_ERROR_INVALID_PARAM;
|
|
1557
1782
|
|
|
1558
|
-
usbi_mutex_lock(&
|
|
1559
|
-
if (!
|
|
1560
|
-
usbi_mutex_unlock(&
|
|
1783
|
+
usbi_mutex_lock(&dev_handle->lock);
|
|
1784
|
+
if (!dev_handle->dev->attached) {
|
|
1785
|
+
usbi_mutex_unlock(&dev_handle->lock);
|
|
1561
1786
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1562
1787
|
}
|
|
1563
1788
|
|
|
1564
|
-
if (!(
|
|
1565
|
-
usbi_mutex_unlock(&
|
|
1789
|
+
if (!(dev_handle->claimed_interfaces & (1U << interface_number))) {
|
|
1790
|
+
usbi_mutex_unlock(&dev_handle->lock);
|
|
1566
1791
|
return LIBUSB_ERROR_NOT_FOUND;
|
|
1567
1792
|
}
|
|
1568
|
-
usbi_mutex_unlock(&
|
|
1793
|
+
usbi_mutex_unlock(&dev_handle->lock);
|
|
1569
1794
|
|
|
1570
|
-
return usbi_backend
|
|
1795
|
+
return usbi_backend.set_interface_altsetting(dev_handle, interface_number,
|
|
1571
1796
|
alternate_setting);
|
|
1572
1797
|
}
|
|
1573
1798
|
|
|
1574
|
-
/** \ingroup
|
|
1799
|
+
/** \ingroup libusb_dev
|
|
1575
1800
|
* Clear the halt/stall condition for an endpoint. Endpoints with halt status
|
|
1576
1801
|
* are unable to receive or transmit data until the halt condition is stalled.
|
|
1577
1802
|
*
|
|
@@ -1580,24 +1805,24 @@ int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev,
|
|
|
1580
1805
|
*
|
|
1581
1806
|
* This is a blocking function.
|
|
1582
1807
|
*
|
|
1583
|
-
* \param
|
|
1808
|
+
* \param dev_handle a device handle
|
|
1584
1809
|
* \param endpoint the endpoint to clear halt status
|
|
1585
1810
|
* \returns 0 on success
|
|
1586
1811
|
* \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
|
|
1587
1812
|
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
|
1588
1813
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1589
1814
|
*/
|
|
1590
|
-
int API_EXPORTED libusb_clear_halt(libusb_device_handle *
|
|
1815
|
+
int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev_handle,
|
|
1591
1816
|
unsigned char endpoint)
|
|
1592
1817
|
{
|
|
1593
1818
|
usbi_dbg("endpoint %x", endpoint);
|
|
1594
|
-
if (!
|
|
1819
|
+
if (!dev_handle->dev->attached)
|
|
1595
1820
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1596
1821
|
|
|
1597
|
-
return usbi_backend
|
|
1822
|
+
return usbi_backend.clear_halt(dev_handle, endpoint);
|
|
1598
1823
|
}
|
|
1599
1824
|
|
|
1600
|
-
/** \ingroup
|
|
1825
|
+
/** \ingroup libusb_dev
|
|
1601
1826
|
* Perform a USB port reset to reinitialize a device. The system will attempt
|
|
1602
1827
|
* to restore the previous configuration and alternate settings after the
|
|
1603
1828
|
* reset has completed.
|
|
@@ -1610,22 +1835,22 @@ int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev,
|
|
|
1610
1835
|
*
|
|
1611
1836
|
* This is a blocking function which usually incurs a noticeable delay.
|
|
1612
1837
|
*
|
|
1613
|
-
* \param
|
|
1838
|
+
* \param dev_handle a handle of the device to reset
|
|
1614
1839
|
* \returns 0 on success
|
|
1615
1840
|
* \returns LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the
|
|
1616
1841
|
* device has been disconnected
|
|
1617
1842
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1618
1843
|
*/
|
|
1619
|
-
int API_EXPORTED libusb_reset_device(libusb_device_handle *
|
|
1844
|
+
int API_EXPORTED libusb_reset_device(libusb_device_handle *dev_handle)
|
|
1620
1845
|
{
|
|
1621
1846
|
usbi_dbg("");
|
|
1622
|
-
if (!
|
|
1847
|
+
if (!dev_handle->dev->attached)
|
|
1623
1848
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1624
1849
|
|
|
1625
|
-
return usbi_backend
|
|
1850
|
+
return usbi_backend.reset_device(dev_handle);
|
|
1626
1851
|
}
|
|
1627
1852
|
|
|
1628
|
-
/** \ingroup
|
|
1853
|
+
/** \ingroup libusb_asyncio
|
|
1629
1854
|
* Allocate up to num_streams usb bulk streams on the specified endpoints. This
|
|
1630
1855
|
* function takes an array of endpoints rather then a single endpoint because
|
|
1631
1856
|
* some protocols require that endpoints are setup with similar stream ids.
|
|
@@ -1640,62 +1865,116 @@ int API_EXPORTED libusb_reset_device(libusb_device_handle *dev)
|
|
|
1640
1865
|
*
|
|
1641
1866
|
* Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
|
|
1642
1867
|
*
|
|
1643
|
-
* \param
|
|
1868
|
+
* \param dev_handle a device handle
|
|
1644
1869
|
* \param num_streams number of streams to try to allocate
|
|
1645
1870
|
* \param endpoints array of endpoints to allocate streams on
|
|
1646
1871
|
* \param num_endpoints length of the endpoints array
|
|
1647
1872
|
* \returns number of streams allocated, or a LIBUSB_ERROR code on failure
|
|
1648
1873
|
*/
|
|
1649
|
-
int API_EXPORTED libusb_alloc_streams(libusb_device_handle *
|
|
1874
|
+
int API_EXPORTED libusb_alloc_streams(libusb_device_handle *dev_handle,
|
|
1650
1875
|
uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
|
|
1651
1876
|
{
|
|
1652
1877
|
usbi_dbg("streams %u eps %d", (unsigned) num_streams, num_endpoints);
|
|
1653
1878
|
|
|
1654
|
-
if (!
|
|
1879
|
+
if (!dev_handle->dev->attached)
|
|
1655
1880
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1656
1881
|
|
|
1657
|
-
if (usbi_backend
|
|
1658
|
-
return usbi_backend
|
|
1882
|
+
if (usbi_backend.alloc_streams)
|
|
1883
|
+
return usbi_backend.alloc_streams(dev_handle, num_streams, endpoints,
|
|
1659
1884
|
num_endpoints);
|
|
1660
1885
|
else
|
|
1661
1886
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1662
1887
|
}
|
|
1663
1888
|
|
|
1664
|
-
/** \ingroup
|
|
1889
|
+
/** \ingroup libusb_asyncio
|
|
1665
1890
|
* Free usb bulk streams allocated with libusb_alloc_streams().
|
|
1666
1891
|
*
|
|
1667
1892
|
* Note streams are automatically free-ed when releasing an interface.
|
|
1668
1893
|
*
|
|
1669
1894
|
* Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
|
|
1670
1895
|
*
|
|
1671
|
-
* \param
|
|
1896
|
+
* \param dev_handle a device handle
|
|
1672
1897
|
* \param endpoints array of endpoints to free streams on
|
|
1673
1898
|
* \param num_endpoints length of the endpoints array
|
|
1674
1899
|
* \returns LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
|
|
1675
1900
|
*/
|
|
1676
|
-
int API_EXPORTED libusb_free_streams(libusb_device_handle *
|
|
1901
|
+
int API_EXPORTED libusb_free_streams(libusb_device_handle *dev_handle,
|
|
1677
1902
|
unsigned char *endpoints, int num_endpoints)
|
|
1678
1903
|
{
|
|
1679
1904
|
usbi_dbg("eps %d", num_endpoints);
|
|
1680
1905
|
|
|
1681
|
-
if (!
|
|
1906
|
+
if (!dev_handle->dev->attached)
|
|
1682
1907
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1683
1908
|
|
|
1684
|
-
if (usbi_backend
|
|
1685
|
-
return usbi_backend
|
|
1909
|
+
if (usbi_backend.free_streams)
|
|
1910
|
+
return usbi_backend.free_streams(dev_handle, endpoints,
|
|
1686
1911
|
num_endpoints);
|
|
1687
1912
|
else
|
|
1688
1913
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1689
1914
|
}
|
|
1690
1915
|
|
|
1691
|
-
/** \ingroup
|
|
1916
|
+
/** \ingroup libusb_asyncio
|
|
1917
|
+
* Attempts to allocate a block of persistent DMA memory suitable for transfers
|
|
1918
|
+
* against the given device. If successful, will return a block of memory
|
|
1919
|
+
* that is suitable for use as "buffer" in \ref libusb_transfer against this
|
|
1920
|
+
* device. Using this memory instead of regular memory means that the host
|
|
1921
|
+
* controller can use DMA directly into the buffer to increase performance, and
|
|
1922
|
+
* also that transfers can no longer fail due to kernel memory fragmentation.
|
|
1923
|
+
*
|
|
1924
|
+
* Note that this means you should not modify this memory (or even data on
|
|
1925
|
+
* the same cache lines) when a transfer is in progress, although it is legal
|
|
1926
|
+
* to have several transfers going on within the same memory block.
|
|
1927
|
+
*
|
|
1928
|
+
* Will return NULL on failure. Many systems do not support such zerocopy
|
|
1929
|
+
* and will always return NULL. Memory allocated with this function must be
|
|
1930
|
+
* freed with \ref libusb_dev_mem_free. Specifically, this means that the
|
|
1931
|
+
* flag \ref LIBUSB_TRANSFER_FREE_BUFFER cannot be used to free memory allocated
|
|
1932
|
+
* with this function.
|
|
1933
|
+
*
|
|
1934
|
+
* Since version 1.0.21, \ref LIBUSB_API_VERSION >= 0x01000105
|
|
1935
|
+
*
|
|
1936
|
+
* \param dev_handle a device handle
|
|
1937
|
+
* \param length size of desired data buffer
|
|
1938
|
+
* \returns a pointer to the newly allocated memory, or NULL on failure
|
|
1939
|
+
*/
|
|
1940
|
+
DEFAULT_VISIBILITY
|
|
1941
|
+
unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handle,
|
|
1942
|
+
size_t length)
|
|
1943
|
+
{
|
|
1944
|
+
if (!dev_handle->dev->attached)
|
|
1945
|
+
return NULL;
|
|
1946
|
+
|
|
1947
|
+
if (usbi_backend.dev_mem_alloc)
|
|
1948
|
+
return usbi_backend.dev_mem_alloc(dev_handle, length);
|
|
1949
|
+
else
|
|
1950
|
+
return NULL;
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
/** \ingroup libusb_asyncio
|
|
1954
|
+
* Free device memory allocated with libusb_dev_mem_alloc().
|
|
1955
|
+
*
|
|
1956
|
+
* \param dev_handle a device handle
|
|
1957
|
+
* \param buffer pointer to the previously allocated memory
|
|
1958
|
+
* \param length size of previously allocated memory
|
|
1959
|
+
* \returns LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
|
|
1960
|
+
*/
|
|
1961
|
+
int API_EXPORTED libusb_dev_mem_free(libusb_device_handle *dev_handle,
|
|
1962
|
+
unsigned char *buffer, size_t length)
|
|
1963
|
+
{
|
|
1964
|
+
if (usbi_backend.dev_mem_free)
|
|
1965
|
+
return usbi_backend.dev_mem_free(dev_handle, buffer, length);
|
|
1966
|
+
else
|
|
1967
|
+
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
/** \ingroup libusb_dev
|
|
1692
1971
|
* Determine if a kernel driver is active on an interface. If a kernel driver
|
|
1693
1972
|
* is active, you cannot claim the interface, and libusb will be unable to
|
|
1694
1973
|
* perform I/O.
|
|
1695
1974
|
*
|
|
1696
1975
|
* This functionality is not available on Windows.
|
|
1697
1976
|
*
|
|
1698
|
-
* \param
|
|
1977
|
+
* \param dev_handle a device handle
|
|
1699
1978
|
* \param interface_number the interface to check
|
|
1700
1979
|
* \returns 0 if no kernel driver is active
|
|
1701
1980
|
* \returns 1 if a kernel driver is active
|
|
@@ -1705,21 +1984,21 @@ int API_EXPORTED libusb_free_streams(libusb_device_handle *dev,
|
|
|
1705
1984
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1706
1985
|
* \see libusb_detach_kernel_driver()
|
|
1707
1986
|
*/
|
|
1708
|
-
int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *
|
|
1987
|
+
int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev_handle,
|
|
1709
1988
|
int interface_number)
|
|
1710
1989
|
{
|
|
1711
1990
|
usbi_dbg("interface %d", interface_number);
|
|
1712
1991
|
|
|
1713
|
-
if (!
|
|
1992
|
+
if (!dev_handle->dev->attached)
|
|
1714
1993
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1715
1994
|
|
|
1716
|
-
if (usbi_backend
|
|
1717
|
-
return usbi_backend
|
|
1995
|
+
if (usbi_backend.kernel_driver_active)
|
|
1996
|
+
return usbi_backend.kernel_driver_active(dev_handle, interface_number);
|
|
1718
1997
|
else
|
|
1719
1998
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1720
1999
|
}
|
|
1721
2000
|
|
|
1722
|
-
/** \ingroup
|
|
2001
|
+
/** \ingroup libusb_dev
|
|
1723
2002
|
* Detach a kernel driver from an interface. If successful, you will then be
|
|
1724
2003
|
* able to claim the interface and perform I/O.
|
|
1725
2004
|
*
|
|
@@ -1729,7 +2008,7 @@ int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev,
|
|
|
1729
2008
|
* driver, if this driver is already attached to the device, this call will
|
|
1730
2009
|
* not detach it and return LIBUSB_ERROR_NOT_FOUND.
|
|
1731
2010
|
*
|
|
1732
|
-
* \param
|
|
2011
|
+
* \param dev_handle a device handle
|
|
1733
2012
|
* \param interface_number the interface to detach the driver from
|
|
1734
2013
|
* \returns 0 on success
|
|
1735
2014
|
* \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
|
|
@@ -1740,28 +2019,28 @@ int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev,
|
|
|
1740
2019
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1741
2020
|
* \see libusb_kernel_driver_active()
|
|
1742
2021
|
*/
|
|
1743
|
-
int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *
|
|
2022
|
+
int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev_handle,
|
|
1744
2023
|
int interface_number)
|
|
1745
2024
|
{
|
|
1746
2025
|
usbi_dbg("interface %d", interface_number);
|
|
1747
2026
|
|
|
1748
|
-
if (!
|
|
2027
|
+
if (!dev_handle->dev->attached)
|
|
1749
2028
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1750
2029
|
|
|
1751
|
-
if (usbi_backend
|
|
1752
|
-
return usbi_backend
|
|
2030
|
+
if (usbi_backend.detach_kernel_driver)
|
|
2031
|
+
return usbi_backend.detach_kernel_driver(dev_handle, interface_number);
|
|
1753
2032
|
else
|
|
1754
2033
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1755
2034
|
}
|
|
1756
2035
|
|
|
1757
|
-
/** \ingroup
|
|
2036
|
+
/** \ingroup libusb_dev
|
|
1758
2037
|
* Re-attach an interface's kernel driver, which was previously detached
|
|
1759
2038
|
* using libusb_detach_kernel_driver(). This call is only effective on
|
|
1760
2039
|
* Linux and returns LIBUSB_ERROR_NOT_SUPPORTED on all other platforms.
|
|
1761
2040
|
*
|
|
1762
2041
|
* This functionality is not available on Darwin or Windows.
|
|
1763
2042
|
*
|
|
1764
|
-
* \param
|
|
2043
|
+
* \param dev_handle a device handle
|
|
1765
2044
|
* \param interface_number the interface to attach the driver from
|
|
1766
2045
|
* \returns 0 on success
|
|
1767
2046
|
* \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
|
|
@@ -1774,21 +2053,21 @@ int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev,
|
|
|
1774
2053
|
* \returns another LIBUSB_ERROR code on other failure
|
|
1775
2054
|
* \see libusb_kernel_driver_active()
|
|
1776
2055
|
*/
|
|
1777
|
-
int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *
|
|
2056
|
+
int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev_handle,
|
|
1778
2057
|
int interface_number)
|
|
1779
2058
|
{
|
|
1780
2059
|
usbi_dbg("interface %d", interface_number);
|
|
1781
2060
|
|
|
1782
|
-
if (!
|
|
2061
|
+
if (!dev_handle->dev->attached)
|
|
1783
2062
|
return LIBUSB_ERROR_NO_DEVICE;
|
|
1784
2063
|
|
|
1785
|
-
if (usbi_backend
|
|
1786
|
-
return usbi_backend
|
|
2064
|
+
if (usbi_backend.attach_kernel_driver)
|
|
2065
|
+
return usbi_backend.attach_kernel_driver(dev_handle, interface_number);
|
|
1787
2066
|
else
|
|
1788
2067
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1789
2068
|
}
|
|
1790
2069
|
|
|
1791
|
-
/** \ingroup
|
|
2070
|
+
/** \ingroup libusb_dev
|
|
1792
2071
|
* Enable/disable libusb's automatic kernel driver detachment. When this is
|
|
1793
2072
|
* enabled libusb will automatically detach the kernel driver on an interface
|
|
1794
2073
|
* when claiming the interface, and attach it when releasing the interface.
|
|
@@ -1800,7 +2079,7 @@ int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev,
|
|
|
1800
2079
|
* this function will return LIBUSB_ERROR_NOT_SUPPORTED, and libusb will
|
|
1801
2080
|
* continue as if this function was never called.
|
|
1802
2081
|
*
|
|
1803
|
-
* \param
|
|
2082
|
+
* \param dev_handle a device handle
|
|
1804
2083
|
* \param enable whether to enable or disable auto kernel driver detachment
|
|
1805
2084
|
*
|
|
1806
2085
|
* \returns LIBUSB_SUCCESS on success
|
|
@@ -1811,48 +2090,154 @@ int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev,
|
|
|
1811
2090
|
* \see libusb_set_configuration()
|
|
1812
2091
|
*/
|
|
1813
2092
|
int API_EXPORTED libusb_set_auto_detach_kernel_driver(
|
|
1814
|
-
libusb_device_handle *
|
|
2093
|
+
libusb_device_handle *dev_handle, int enable)
|
|
1815
2094
|
{
|
|
1816
|
-
if (!(usbi_backend
|
|
2095
|
+
if (!(usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER))
|
|
1817
2096
|
return LIBUSB_ERROR_NOT_SUPPORTED;
|
|
1818
2097
|
|
|
1819
|
-
|
|
2098
|
+
dev_handle->auto_detach_kernel_driver = enable;
|
|
1820
2099
|
return LIBUSB_SUCCESS;
|
|
1821
2100
|
}
|
|
1822
2101
|
|
|
1823
|
-
/** \ingroup
|
|
1824
|
-
*
|
|
1825
|
-
*
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
2102
|
+
/** \ingroup libusb_lib
|
|
2103
|
+
* \deprecated Use libusb_set_option() instead using the
|
|
2104
|
+
* \ref LIBUSB_OPTION_LOG_LEVEL option.
|
|
2105
|
+
*/
|
|
2106
|
+
void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
|
|
2107
|
+
{
|
|
2108
|
+
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
|
|
2109
|
+
USBI_GET_CONTEXT(ctx);
|
|
2110
|
+
if (!ctx->debug_fixed) {
|
|
2111
|
+
level = CLAMP(level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
|
|
2112
|
+
ctx->debug = (enum libusb_log_level)level;
|
|
2113
|
+
}
|
|
2114
|
+
#else
|
|
2115
|
+
UNUSED(ctx);
|
|
2116
|
+
UNUSED(level);
|
|
2117
|
+
#endif
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
/** \ingroup libusb_lib
|
|
2121
|
+
* Set log handler.
|
|
2122
|
+
*
|
|
2123
|
+
* libusb will redirect its log messages to the provided callback function.
|
|
2124
|
+
* libusb supports redirection of per context and global log messages.
|
|
2125
|
+
* Log messages sent to the context will be sent to the global log handler too.
|
|
2126
|
+
*
|
|
2127
|
+
* If libusb is compiled without message logging or USE_SYSTEM_LOGGING_FACILITY
|
|
2128
|
+
* is defined then global callback function will never be called.
|
|
2129
|
+
* If ENABLE_DEBUG_LOGGING is defined then per context callback function will
|
|
2130
|
+
* never be called.
|
|
2131
|
+
*
|
|
2132
|
+
* \param ctx context on which to assign log handler, or NULL for the default
|
|
2133
|
+
* context. Parameter ignored if only LIBUSB_LOG_CB_GLOBAL mode is requested.
|
|
2134
|
+
* \param cb pointer to the callback function, or NULL to stop log
|
|
2135
|
+
* messages redirection
|
|
2136
|
+
* \param mode mode of callback function operation. Several modes can be
|
|
2137
|
+
* selected for a single callback function, see \ref libusb_log_cb_mode for
|
|
2138
|
+
* a description.
|
|
2139
|
+
* \see libusb_log_cb, libusb_log_cb_mode
|
|
2140
|
+
*/
|
|
2141
|
+
void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb,
|
|
2142
|
+
int mode)
|
|
2143
|
+
{
|
|
2144
|
+
#if !defined(USE_SYSTEM_LOGGING_FACILITY)
|
|
2145
|
+
if (mode & LIBUSB_LOG_CB_GLOBAL) {
|
|
2146
|
+
log_handler = cb;
|
|
2147
|
+
}
|
|
2148
|
+
#endif
|
|
2149
|
+
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
|
|
2150
|
+
if (mode & LIBUSB_LOG_CB_CONTEXT) {
|
|
2151
|
+
USBI_GET_CONTEXT(ctx);
|
|
2152
|
+
ctx->log_handler = cb;
|
|
2153
|
+
}
|
|
2154
|
+
#else
|
|
2155
|
+
UNUSED(ctx);
|
|
2156
|
+
#if defined(USE_SYSTEM_LOGGING_FACILITY)
|
|
2157
|
+
UNUSED(cb);
|
|
2158
|
+
UNUSED(mode);
|
|
2159
|
+
#endif
|
|
2160
|
+
#endif
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
/** \ingroup libusb_lib
|
|
2164
|
+
* Set an option in the library.
|
|
1829
2165
|
*
|
|
1830
|
-
*
|
|
1831
|
-
* with its message logging and most of the time, will only log messages that
|
|
1832
|
-
* explain error conditions and other oddities. This will help you debug
|
|
1833
|
-
* your software.
|
|
2166
|
+
* Use this function to configure a specific option within the library.
|
|
1834
2167
|
*
|
|
1835
|
-
*
|
|
1836
|
-
*
|
|
1837
|
-
* to the value in the environment variable.
|
|
2168
|
+
* Some options require one or more arguments to be provided. Consult each
|
|
2169
|
+
* option's documentation for specific requirements.
|
|
1838
2170
|
*
|
|
1839
|
-
*
|
|
1840
|
-
* nothing: you'll never get any messages.
|
|
2171
|
+
* Since version 1.0.22, \ref LIBUSB_API_VERSION >= 0x01000106
|
|
1841
2172
|
*
|
|
1842
|
-
*
|
|
1843
|
-
*
|
|
2173
|
+
* \param ctx context on which to operate
|
|
2174
|
+
* \param option which option to set
|
|
2175
|
+
* \param ... any required arguments for the specified option
|
|
1844
2176
|
*
|
|
1845
|
-
* \
|
|
1846
|
-
* \
|
|
2177
|
+
* \returns LIBUSB_SUCCESS on success
|
|
2178
|
+
* \returns LIBUSB_ERROR_INVALID_PARAM if the option or arguments are invalid
|
|
2179
|
+
* \returns LIBUSB_ERROR_NOT_SUPPORTED if the option is valid but not supported
|
|
2180
|
+
* on this platform
|
|
1847
2181
|
*/
|
|
1848
|
-
|
|
2182
|
+
int API_EXPORTED libusb_set_option(libusb_context *ctx,
|
|
2183
|
+
enum libusb_option option, ...)
|
|
1849
2184
|
{
|
|
2185
|
+
int arg, r = LIBUSB_SUCCESS;
|
|
2186
|
+
va_list ap;
|
|
2187
|
+
|
|
1850
2188
|
USBI_GET_CONTEXT(ctx);
|
|
1851
|
-
|
|
1852
|
-
|
|
2189
|
+
|
|
2190
|
+
va_start(ap, option);
|
|
2191
|
+
switch (option) {
|
|
2192
|
+
case LIBUSB_OPTION_LOG_LEVEL:
|
|
2193
|
+
arg = va_arg(ap, int);
|
|
2194
|
+
if (arg < LIBUSB_LOG_LEVEL_NONE || arg > LIBUSB_LOG_LEVEL_DEBUG) {
|
|
2195
|
+
r = LIBUSB_ERROR_INVALID_PARAM;
|
|
2196
|
+
break;
|
|
2197
|
+
}
|
|
2198
|
+
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
|
|
2199
|
+
if (!ctx->debug_fixed)
|
|
2200
|
+
ctx->debug = (enum libusb_log_level)arg;
|
|
2201
|
+
#endif
|
|
2202
|
+
break;
|
|
2203
|
+
|
|
2204
|
+
/* Handle all backend-specific options here */
|
|
2205
|
+
case LIBUSB_OPTION_USE_USBDK:
|
|
2206
|
+
if (usbi_backend.set_option)
|
|
2207
|
+
r = usbi_backend.set_option(ctx, option, ap);
|
|
2208
|
+
else
|
|
2209
|
+
r = LIBUSB_ERROR_NOT_SUPPORTED;
|
|
2210
|
+
break;
|
|
2211
|
+
|
|
2212
|
+
default:
|
|
2213
|
+
r = LIBUSB_ERROR_INVALID_PARAM;
|
|
2214
|
+
}
|
|
2215
|
+
va_end(ap);
|
|
2216
|
+
|
|
2217
|
+
return r;
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
|
|
2221
|
+
/* returns the log level as defined in the LIBUSB_DEBUG environment variable.
|
|
2222
|
+
* if LIBUSB_DEBUG is not present or not a number, returns LIBUSB_LOG_LEVEL_NONE.
|
|
2223
|
+
* value is clamped to ensure it is within the valid range of possibilities.
|
|
2224
|
+
*/
|
|
2225
|
+
static enum libusb_log_level get_env_debug_level(void)
|
|
2226
|
+
{
|
|
2227
|
+
const char *dbg = getenv("LIBUSB_DEBUG");
|
|
2228
|
+
enum libusb_log_level level;
|
|
2229
|
+
if (dbg) {
|
|
2230
|
+
int dbg_level = atoi(dbg);
|
|
2231
|
+
dbg_level = CLAMP(dbg_level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
|
|
2232
|
+
level = (enum libusb_log_level)dbg_level;
|
|
2233
|
+
} else {
|
|
2234
|
+
level = LIBUSB_LOG_LEVEL_NONE;
|
|
2235
|
+
}
|
|
2236
|
+
return level;
|
|
1853
2237
|
}
|
|
2238
|
+
#endif
|
|
1854
2239
|
|
|
1855
|
-
/** \ingroup
|
|
2240
|
+
/** \ingroup libusb_lib
|
|
1856
2241
|
* Initialize libusb. This function must be called before calling any other
|
|
1857
2242
|
* libusb function.
|
|
1858
2243
|
*
|
|
@@ -1863,12 +2248,12 @@ void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
|
|
|
1863
2248
|
* \param context Optional output location for context pointer.
|
|
1864
2249
|
* Only valid on return code 0.
|
|
1865
2250
|
* \returns 0 on success, or a LIBUSB_ERROR code on failure
|
|
1866
|
-
* \see
|
|
2251
|
+
* \see libusb_contexts
|
|
1867
2252
|
*/
|
|
1868
2253
|
int API_EXPORTED libusb_init(libusb_context **context)
|
|
1869
2254
|
{
|
|
1870
2255
|
struct libusb_device *dev, *next;
|
|
1871
|
-
|
|
2256
|
+
size_t priv_size = usbi_backend.context_priv_size;
|
|
1872
2257
|
struct libusb_context *ctx;
|
|
1873
2258
|
static int first_init = 1;
|
|
1874
2259
|
int r = 0;
|
|
@@ -1876,7 +2261,7 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
|
|
1876
2261
|
usbi_mutex_static_lock(&default_context_lock);
|
|
1877
2262
|
|
|
1878
2263
|
if (!timestamp_origin.tv_sec) {
|
|
1879
|
-
|
|
2264
|
+
usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, ×tamp_origin);
|
|
1880
2265
|
}
|
|
1881
2266
|
|
|
1882
2267
|
if (!context && usbi_default_context) {
|
|
@@ -1886,22 +2271,18 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
|
|
1886
2271
|
return 0;
|
|
1887
2272
|
}
|
|
1888
2273
|
|
|
1889
|
-
ctx = calloc(1, sizeof(*ctx));
|
|
2274
|
+
ctx = calloc(1, sizeof(*ctx) + priv_size);
|
|
1890
2275
|
if (!ctx) {
|
|
1891
2276
|
r = LIBUSB_ERROR_NO_MEM;
|
|
1892
2277
|
goto err_unlock;
|
|
1893
2278
|
}
|
|
1894
2279
|
|
|
1895
|
-
#
|
|
1896
|
-
ctx->debug =
|
|
2280
|
+
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
|
|
2281
|
+
ctx->debug = get_env_debug_level();
|
|
2282
|
+
if (ctx->debug != LIBUSB_LOG_LEVEL_NONE)
|
|
2283
|
+
ctx->debug_fixed = 1;
|
|
1897
2284
|
#endif
|
|
1898
2285
|
|
|
1899
|
-
if (dbg) {
|
|
1900
|
-
ctx->debug = atoi(dbg);
|
|
1901
|
-
if (ctx->debug)
|
|
1902
|
-
ctx->debug_fixed = 1;
|
|
1903
|
-
}
|
|
1904
|
-
|
|
1905
2286
|
/* default context should be initialized before calling usbi_dbg */
|
|
1906
2287
|
if (!usbi_default_context) {
|
|
1907
2288
|
usbi_default_context = ctx;
|
|
@@ -1909,15 +2290,16 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
|
|
1909
2290
|
usbi_dbg("created default context");
|
|
1910
2291
|
}
|
|
1911
2292
|
|
|
1912
|
-
usbi_dbg("libusb v%
|
|
1913
|
-
libusb_version_internal.micro, libusb_version_internal.nano);
|
|
2293
|
+
usbi_dbg("libusb v%u.%u.%u.%u%s", libusb_version_internal.major, libusb_version_internal.minor,
|
|
2294
|
+
libusb_version_internal.micro, libusb_version_internal.nano, libusb_version_internal.rc);
|
|
1914
2295
|
|
|
1915
|
-
usbi_mutex_init(&ctx->usb_devs_lock
|
|
1916
|
-
usbi_mutex_init(&ctx->open_devs_lock
|
|
1917
|
-
usbi_mutex_init(&ctx->hotplug_cbs_lock
|
|
2296
|
+
usbi_mutex_init(&ctx->usb_devs_lock);
|
|
2297
|
+
usbi_mutex_init(&ctx->open_devs_lock);
|
|
2298
|
+
usbi_mutex_init(&ctx->hotplug_cbs_lock);
|
|
1918
2299
|
list_init(&ctx->usb_devs);
|
|
1919
2300
|
list_init(&ctx->open_devs);
|
|
1920
2301
|
list_init(&ctx->hotplug_cbs);
|
|
2302
|
+
ctx->next_hotplug_cb_handle = 1;
|
|
1921
2303
|
|
|
1922
2304
|
usbi_mutex_static_lock(&active_contexts_lock);
|
|
1923
2305
|
if (first_init) {
|
|
@@ -1927,8 +2309,8 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
|
|
1927
2309
|
list_add (&ctx->list, &active_contexts_list);
|
|
1928
2310
|
usbi_mutex_static_unlock(&active_contexts_lock);
|
|
1929
2311
|
|
|
1930
|
-
if (usbi_backend
|
|
1931
|
-
r = usbi_backend
|
|
2312
|
+
if (usbi_backend.init) {
|
|
2313
|
+
r = usbi_backend.init(ctx);
|
|
1932
2314
|
if (r)
|
|
1933
2315
|
goto err_free_ctx;
|
|
1934
2316
|
}
|
|
@@ -1945,8 +2327,8 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
|
|
1945
2327
|
return 0;
|
|
1946
2328
|
|
|
1947
2329
|
err_backend_exit:
|
|
1948
|
-
if (usbi_backend
|
|
1949
|
-
usbi_backend
|
|
2330
|
+
if (usbi_backend.exit)
|
|
2331
|
+
usbi_backend.exit(ctx);
|
|
1950
2332
|
err_free_ctx:
|
|
1951
2333
|
if (ctx == usbi_default_context) {
|
|
1952
2334
|
usbi_default_context = NULL;
|
|
@@ -1974,7 +2356,7 @@ err_unlock:
|
|
|
1974
2356
|
return r;
|
|
1975
2357
|
}
|
|
1976
2358
|
|
|
1977
|
-
/** \ingroup
|
|
2359
|
+
/** \ingroup libusb_lib
|
|
1978
2360
|
* Deinitialize libusb. Should be called after closing all open devices and
|
|
1979
2361
|
* before your application terminates.
|
|
1980
2362
|
* \param ctx the context to deinitialize, or NULL for the default context
|
|
@@ -1983,6 +2365,7 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
|
|
1983
2365
|
{
|
|
1984
2366
|
struct libusb_device *dev, *next;
|
|
1985
2367
|
struct timeval tv = { 0, 0 };
|
|
2368
|
+
int destroying_default_context = 0;
|
|
1986
2369
|
|
|
1987
2370
|
usbi_dbg("");
|
|
1988
2371
|
USBI_GET_CONTEXT(ctx);
|
|
@@ -1997,15 +2380,25 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
|
|
1997
2380
|
return;
|
|
1998
2381
|
}
|
|
1999
2382
|
usbi_dbg("destroying default context");
|
|
2000
|
-
|
|
2001
|
-
|
|
2383
|
+
|
|
2384
|
+
/*
|
|
2385
|
+
* Setting this flag without unlocking the default context, as
|
|
2386
|
+
* we are actually destroying the default context.
|
|
2387
|
+
* usbi_default_context is not set to NULL yet, as all activities
|
|
2388
|
+
* would only stop after usbi_backend->exit() returns.
|
|
2389
|
+
*/
|
|
2390
|
+
destroying_default_context = 1;
|
|
2391
|
+
} else {
|
|
2392
|
+
// Unlock default context, as we're not modifying it.
|
|
2393
|
+
usbi_mutex_static_unlock(&default_context_lock);
|
|
2394
|
+
}
|
|
2002
2395
|
|
|
2003
2396
|
usbi_mutex_static_lock(&active_contexts_lock);
|
|
2004
2397
|
list_del (&ctx->list);
|
|
2005
2398
|
usbi_mutex_static_unlock(&active_contexts_lock);
|
|
2006
2399
|
|
|
2007
2400
|
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
|
2008
|
-
|
|
2401
|
+
usbi_hotplug_deregister(ctx, 1);
|
|
2009
2402
|
|
|
2010
2403
|
/*
|
|
2011
2404
|
* Ensure any pending unplug events are read from the hotplug
|
|
@@ -2035,21 +2428,21 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
|
|
2035
2428
|
usbi_warn(ctx, "application left some devices open");
|
|
2036
2429
|
|
|
2037
2430
|
usbi_io_exit(ctx);
|
|
2038
|
-
if (usbi_backend
|
|
2039
|
-
usbi_backend
|
|
2431
|
+
if (usbi_backend.exit)
|
|
2432
|
+
usbi_backend.exit(ctx);
|
|
2040
2433
|
|
|
2041
2434
|
usbi_mutex_destroy(&ctx->open_devs_lock);
|
|
2042
2435
|
usbi_mutex_destroy(&ctx->usb_devs_lock);
|
|
2043
2436
|
usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
|
|
2437
|
+
free(ctx);
|
|
2044
2438
|
|
|
2045
|
-
if (
|
|
2439
|
+
if (destroying_default_context) {
|
|
2046
2440
|
usbi_default_context = NULL;
|
|
2441
|
+
usbi_mutex_static_unlock(&default_context_lock);
|
|
2047
2442
|
}
|
|
2048
|
-
|
|
2049
|
-
free(ctx);
|
|
2050
2443
|
}
|
|
2051
2444
|
|
|
2052
|
-
/** \ingroup
|
|
2445
|
+
/** \ingroup libusb_misc
|
|
2053
2446
|
* Check at runtime if the loaded library has a given capability.
|
|
2054
2447
|
* This call should be performed after \ref libusb_init(), to ensure the
|
|
2055
2448
|
* backend has updated its capability set.
|
|
@@ -2063,111 +2456,109 @@ int API_EXPORTED libusb_has_capability(uint32_t capability)
|
|
|
2063
2456
|
case LIBUSB_CAP_HAS_CAPABILITY:
|
|
2064
2457
|
return 1;
|
|
2065
2458
|
case LIBUSB_CAP_HAS_HOTPLUG:
|
|
2066
|
-
return !(usbi_backend
|
|
2459
|
+
return !(usbi_backend.get_device_list);
|
|
2067
2460
|
case LIBUSB_CAP_HAS_HID_ACCESS:
|
|
2068
|
-
return (usbi_backend
|
|
2461
|
+
return (usbi_backend.caps & USBI_CAP_HAS_HID_ACCESS);
|
|
2069
2462
|
case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
|
|
2070
|
-
return (usbi_backend
|
|
2463
|
+
return (usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
|
|
2071
2464
|
}
|
|
2072
2465
|
return 0;
|
|
2073
2466
|
}
|
|
2074
2467
|
|
|
2075
|
-
|
|
2076
|
-
#ifdef LIBUSB_GETTIMEOFDAY_WIN32
|
|
2077
|
-
/*
|
|
2078
|
-
* gettimeofday
|
|
2079
|
-
* Implementation according to:
|
|
2080
|
-
* The Open Group Base Specifications Issue 6
|
|
2081
|
-
* IEEE Std 1003.1, 2004 Edition
|
|
2082
|
-
*/
|
|
2468
|
+
#ifdef ENABLE_LOGGING
|
|
2083
2469
|
|
|
2470
|
+
/* this is defined in libusbi.h if needed */
|
|
2471
|
+
#ifdef LIBUSB_PRINTF_WIN32
|
|
2084
2472
|
/*
|
|
2085
|
-
*
|
|
2086
|
-
*
|
|
2087
|
-
*
|
|
2088
|
-
*
|
|
2473
|
+
* Prior to VS2015, Microsoft did not provide the snprintf() function and
|
|
2474
|
+
* provided a vsnprintf() that did not guarantee NULL-terminated output.
|
|
2475
|
+
* Microsoft did provide a _snprintf() function, but again it did not
|
|
2476
|
+
* guarantee NULL-terminated output.
|
|
2089
2477
|
*
|
|
2090
|
-
*
|
|
2091
|
-
*
|
|
2092
|
-
* DISCLAIMED. This includes but is not limited to warranties of
|
|
2093
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
2094
|
-
*
|
|
2095
|
-
* Contributed by:
|
|
2096
|
-
* Danny Smith <dannysmith@users.sourceforge.net>
|
|
2478
|
+
* The below implementations guarantee NULL-terminated output and are
|
|
2479
|
+
* C99 compliant.
|
|
2097
2480
|
*/
|
|
2098
2481
|
|
|
2099
|
-
|
|
2100
|
-
|
|
2482
|
+
int usbi_snprintf(char *str, size_t size, const char *format, ...)
|
|
2483
|
+
{
|
|
2484
|
+
va_list ap;
|
|
2485
|
+
int ret;
|
|
2486
|
+
|
|
2487
|
+
va_start(ap, format);
|
|
2488
|
+
ret = usbi_vsnprintf(str, size, format, ap);
|
|
2489
|
+
va_end(ap);
|
|
2490
|
+
|
|
2491
|
+
return ret;
|
|
2492
|
+
}
|
|
2101
2493
|
|
|
2102
|
-
int
|
|
2494
|
+
int usbi_vsnprintf(char *str, size_t size, const char *format, va_list ap)
|
|
2103
2495
|
{
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
GetSystemTime(&st);
|
|
2114
|
-
SystemTimeToFileTime(&st, &_now.ft);
|
|
2115
|
-
#else
|
|
2116
|
-
GetSystemTimeAsFileTime (&_now.ft);
|
|
2117
|
-
#endif
|
|
2118
|
-
tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
|
|
2119
|
-
tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
|
|
2496
|
+
int ret;
|
|
2497
|
+
|
|
2498
|
+
ret = _vsnprintf(str, size, format, ap);
|
|
2499
|
+
if (ret < 0 || ret == (int)size) {
|
|
2500
|
+
/* Output is truncated, ensure buffer is NULL-terminated and
|
|
2501
|
+
* determine how many characters would have been written. */
|
|
2502
|
+
str[size - 1] = '\0';
|
|
2503
|
+
if (ret < 0)
|
|
2504
|
+
ret = _vsnprintf(NULL, 0, format, ap);
|
|
2120
2505
|
}
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
return 0;
|
|
2506
|
+
|
|
2507
|
+
return ret;
|
|
2124
2508
|
}
|
|
2125
|
-
#endif
|
|
2509
|
+
#endif /* LIBUSB_PRINTF_WIN32 */
|
|
2126
2510
|
|
|
2127
|
-
static void usbi_log_str(
|
|
2128
|
-
enum libusb_log_level level, const char * str)
|
|
2511
|
+
static void usbi_log_str(enum libusb_log_level level, const char *str)
|
|
2129
2512
|
{
|
|
2130
2513
|
#if defined(USE_SYSTEM_LOGGING_FACILITY)
|
|
2131
2514
|
#if defined(OS_WINDOWS) || defined(OS_WINCE)
|
|
2132
|
-
|
|
2515
|
+
#if !defined(UNICODE)
|
|
2516
|
+
OutputDebugStringA(str);
|
|
2517
|
+
#else
|
|
2133
2518
|
WCHAR wbuf[USBI_MAX_LOG_LEN];
|
|
2134
|
-
MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf))
|
|
2135
|
-
|
|
2519
|
+
if (MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf)) != 0)
|
|
2520
|
+
OutputDebugStringW(wbuf);
|
|
2521
|
+
#endif
|
|
2136
2522
|
#elif defined(__ANDROID__)
|
|
2137
2523
|
int priority = ANDROID_LOG_UNKNOWN;
|
|
2138
2524
|
switch (level) {
|
|
2139
|
-
case
|
|
2140
|
-
case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
|
|
2525
|
+
case LIBUSB_LOG_LEVEL_NONE: return;
|
|
2141
2526
|
case LIBUSB_LOG_LEVEL_ERROR: priority = ANDROID_LOG_ERROR; break;
|
|
2527
|
+
case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
|
|
2528
|
+
case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
|
|
2142
2529
|
case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
|
|
2143
2530
|
}
|
|
2144
2531
|
__android_log_write(priority, "libusb", str);
|
|
2145
2532
|
#elif defined(HAVE_SYSLOG_FUNC)
|
|
2146
2533
|
int syslog_level = LOG_INFO;
|
|
2147
2534
|
switch (level) {
|
|
2148
|
-
case
|
|
2149
|
-
case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
|
|
2535
|
+
case LIBUSB_LOG_LEVEL_NONE: return;
|
|
2150
2536
|
case LIBUSB_LOG_LEVEL_ERROR: syslog_level = LOG_ERR; break;
|
|
2537
|
+
case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
|
|
2538
|
+
case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break;
|
|
2151
2539
|
case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break;
|
|
2152
2540
|
}
|
|
2153
2541
|
syslog(syslog_level, "%s", str);
|
|
2154
|
-
#else /* All of gcc, Clang,
|
|
2542
|
+
#else /* All of gcc, Clang, Xcode seem to use #warning */
|
|
2155
2543
|
#warning System logging is not supported on this platform. Logging to stderr will be used instead.
|
|
2156
2544
|
fputs(str, stderr);
|
|
2157
2545
|
#endif
|
|
2158
2546
|
#else
|
|
2159
|
-
|
|
2547
|
+
/* Global log handler */
|
|
2548
|
+
if (log_handler != NULL)
|
|
2549
|
+
log_handler(NULL, level, str);
|
|
2550
|
+
else
|
|
2551
|
+
fputs(str, stderr);
|
|
2160
2552
|
#endif /* USE_SYSTEM_LOGGING_FACILITY */
|
|
2161
|
-
UNUSED(ctx);
|
|
2162
2553
|
UNUSED(level);
|
|
2163
2554
|
}
|
|
2164
2555
|
|
|
2165
2556
|
void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
|
|
2166
2557
|
const char *function, const char *format, va_list args)
|
|
2167
2558
|
{
|
|
2168
|
-
const char *prefix
|
|
2559
|
+
const char *prefix;
|
|
2169
2560
|
char buf[USBI_MAX_LOG_LEN];
|
|
2170
|
-
struct
|
|
2561
|
+
struct timespec now;
|
|
2171
2562
|
int global_debug, header_len, text_len;
|
|
2172
2563
|
static int has_debug_header_been_displayed = 0;
|
|
2173
2564
|
|
|
@@ -2175,18 +2566,15 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
|
|
|
2175
2566
|
global_debug = 1;
|
|
2176
2567
|
UNUSED(ctx);
|
|
2177
2568
|
#else
|
|
2178
|
-
|
|
2569
|
+
enum libusb_log_level ctx_level = LIBUSB_LOG_LEVEL_NONE;
|
|
2179
2570
|
|
|
2180
2571
|
USBI_GET_CONTEXT(ctx);
|
|
2181
|
-
if (ctx)
|
|
2572
|
+
if (ctx)
|
|
2182
2573
|
ctx_level = ctx->debug;
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
}
|
|
2188
|
-
global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
|
|
2189
|
-
if (!ctx_level)
|
|
2574
|
+
else
|
|
2575
|
+
ctx_level = get_env_debug_level();
|
|
2576
|
+
|
|
2577
|
+
if (ctx_level == LIBUSB_LOG_LEVEL_NONE)
|
|
2190
2578
|
return;
|
|
2191
2579
|
if (level == LIBUSB_LOG_LEVEL_WARNING && ctx_level < LIBUSB_LOG_LEVEL_WARNING)
|
|
2192
2580
|
return;
|
|
@@ -2194,36 +2582,38 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
|
|
|
2194
2582
|
return;
|
|
2195
2583
|
if (level == LIBUSB_LOG_LEVEL_DEBUG && ctx_level < LIBUSB_LOG_LEVEL_DEBUG)
|
|
2196
2584
|
return;
|
|
2585
|
+
|
|
2586
|
+
global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
|
|
2197
2587
|
#endif
|
|
2198
2588
|
|
|
2199
|
-
|
|
2589
|
+
usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &now);
|
|
2200
2590
|
if ((global_debug) && (!has_debug_header_been_displayed)) {
|
|
2201
2591
|
has_debug_header_been_displayed = 1;
|
|
2202
|
-
usbi_log_str(
|
|
2203
|
-
usbi_log_str(
|
|
2592
|
+
usbi_log_str(LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
|
|
2593
|
+
usbi_log_str(LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
|
|
2204
2594
|
}
|
|
2205
|
-
if (now.
|
|
2595
|
+
if (now.tv_nsec < timestamp_origin.tv_nsec) {
|
|
2206
2596
|
now.tv_sec--;
|
|
2207
|
-
now.
|
|
2597
|
+
now.tv_nsec += 1000000000L;
|
|
2208
2598
|
}
|
|
2209
2599
|
now.tv_sec -= timestamp_origin.tv_sec;
|
|
2210
|
-
now.
|
|
2600
|
+
now.tv_nsec -= timestamp_origin.tv_nsec;
|
|
2211
2601
|
|
|
2212
2602
|
switch (level) {
|
|
2213
|
-
case
|
|
2214
|
-
|
|
2603
|
+
case LIBUSB_LOG_LEVEL_NONE:
|
|
2604
|
+
return;
|
|
2605
|
+
case LIBUSB_LOG_LEVEL_ERROR:
|
|
2606
|
+
prefix = "error";
|
|
2215
2607
|
break;
|
|
2216
2608
|
case LIBUSB_LOG_LEVEL_WARNING:
|
|
2217
2609
|
prefix = "warning";
|
|
2218
2610
|
break;
|
|
2219
|
-
case
|
|
2220
|
-
prefix = "
|
|
2611
|
+
case LIBUSB_LOG_LEVEL_INFO:
|
|
2612
|
+
prefix = "info";
|
|
2221
2613
|
break;
|
|
2222
2614
|
case LIBUSB_LOG_LEVEL_DEBUG:
|
|
2223
2615
|
prefix = "debug";
|
|
2224
2616
|
break;
|
|
2225
|
-
case LIBUSB_LOG_LEVEL_NONE:
|
|
2226
|
-
return;
|
|
2227
2617
|
default:
|
|
2228
2618
|
prefix = "unknown";
|
|
2229
2619
|
break;
|
|
@@ -2231,34 +2621,40 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
|
|
|
2231
2621
|
|
|
2232
2622
|
if (global_debug) {
|
|
2233
2623
|
header_len = snprintf(buf, sizeof(buf),
|
|
2234
|
-
"[%
|
|
2235
|
-
(
|
|
2624
|
+
"[%2ld.%06ld] [%08x] libusb: %s [%s] ",
|
|
2625
|
+
(long)now.tv_sec, (long)(now.tv_nsec / 1000L), usbi_get_tid(), prefix, function);
|
|
2236
2626
|
} else {
|
|
2237
2627
|
header_len = snprintf(buf, sizeof(buf),
|
|
2238
2628
|
"libusb: %s [%s] ", prefix, function);
|
|
2239
2629
|
}
|
|
2240
2630
|
|
|
2241
|
-
if (header_len < 0 || header_len >= sizeof(buf)) {
|
|
2631
|
+
if (header_len < 0 || header_len >= (int)sizeof(buf)) {
|
|
2242
2632
|
/* Somehow snprintf failed to write to the buffer,
|
|
2243
2633
|
* remove the header so something useful is output. */
|
|
2244
2634
|
header_len = 0;
|
|
2245
2635
|
}
|
|
2246
2636
|
/* Make sure buffer is NUL terminated */
|
|
2247
2637
|
buf[header_len] = '\0';
|
|
2248
|
-
text_len = vsnprintf(buf + header_len, sizeof(buf) - header_len,
|
|
2638
|
+
text_len = vsnprintf(buf + header_len, sizeof(buf) - (size_t)header_len,
|
|
2249
2639
|
format, args);
|
|
2250
|
-
if (text_len < 0 || text_len + header_len >= sizeof(buf)) {
|
|
2640
|
+
if (text_len < 0 || text_len + header_len >= (int)sizeof(buf)) {
|
|
2251
2641
|
/* Truncated log output. On some platforms a -1 return value means
|
|
2252
2642
|
* that the output was truncated. */
|
|
2253
|
-
text_len = sizeof(buf) - header_len;
|
|
2643
|
+
text_len = (int)sizeof(buf) - header_len;
|
|
2254
2644
|
}
|
|
2255
|
-
if (header_len + text_len + sizeof(USBI_LOG_LINE_END) >= sizeof(buf)) {
|
|
2645
|
+
if (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END) >= (int)sizeof(buf)) {
|
|
2256
2646
|
/* Need to truncate the text slightly to fit on the terminator. */
|
|
2257
|
-
text_len -= (header_len + text_len + sizeof(USBI_LOG_LINE_END)) - sizeof(buf);
|
|
2647
|
+
text_len -= (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END)) - (int)sizeof(buf);
|
|
2258
2648
|
}
|
|
2259
2649
|
strcpy(buf + header_len + text_len, USBI_LOG_LINE_END);
|
|
2260
2650
|
|
|
2261
|
-
usbi_log_str(
|
|
2651
|
+
usbi_log_str(level, buf);
|
|
2652
|
+
|
|
2653
|
+
/* Per context log handler */
|
|
2654
|
+
#ifndef ENABLE_DEBUG_LOGGING
|
|
2655
|
+
if (ctx && ctx->log_handler)
|
|
2656
|
+
ctx->log_handler(ctx, level, buf);
|
|
2657
|
+
#endif
|
|
2262
2658
|
}
|
|
2263
2659
|
|
|
2264
2660
|
void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
|
|
@@ -2271,7 +2667,9 @@ void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
|
|
|
2271
2667
|
va_end (args);
|
|
2272
2668
|
}
|
|
2273
2669
|
|
|
2274
|
-
|
|
2670
|
+
#endif /* ENABLE_LOGGING */
|
|
2671
|
+
|
|
2672
|
+
/** \ingroup libusb_misc
|
|
2275
2673
|
* Returns a constant NULL-terminated string with the ASCII name of a libusb
|
|
2276
2674
|
* error or transfer status code. The caller must not free() the returned
|
|
2277
2675
|
* string.
|
|
@@ -2331,7 +2729,7 @@ DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code)
|
|
|
2331
2729
|
}
|
|
2332
2730
|
}
|
|
2333
2731
|
|
|
2334
|
-
/** \ingroup
|
|
2732
|
+
/** \ingroup libusb_misc
|
|
2335
2733
|
* Returns a pointer to const struct libusb_version with the version
|
|
2336
2734
|
* (major, minor, micro, nano and rc) of the running library.
|
|
2337
2735
|
*/
|