usb 2.17.0 → 3.0.0-alpha.2

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.
Files changed (207) hide show
  1. package/LICENSE +18 -4
  2. package/README.md +25 -516
  3. package/dist/index.d.ts +67 -13
  4. package/dist/index.js +333 -64
  5. package/index.d.ts +89 -0
  6. package/index.js +583 -0
  7. package/package.json +53 -33
  8. package/CHANGELOG.md +0 -294
  9. package/binding.gyp +0 -128
  10. package/dist/index.js.map +0 -1
  11. package/dist/usb/bindings.d.ts +0 -266
  12. package/dist/usb/bindings.js +0 -10
  13. package/dist/usb/bindings.js.map +0 -1
  14. package/dist/usb/capability.d.ts +0 -13
  15. package/dist/usb/capability.js +0 -17
  16. package/dist/usb/capability.js.map +0 -1
  17. package/dist/usb/descriptors.d.ts +0 -128
  18. package/dist/usb/descriptors.js +0 -3
  19. package/dist/usb/descriptors.js.map +0 -1
  20. package/dist/usb/device.d.ts +0 -100
  21. package/dist/usb/device.js +0 -297
  22. package/dist/usb/device.js.map +0 -1
  23. package/dist/usb/endpoint.d.ts +0 -94
  24. package/dist/usb/endpoint.js +0 -219
  25. package/dist/usb/endpoint.js.map +0 -1
  26. package/dist/usb/index.d.ts +0 -31
  27. package/dist/usb/index.js +0 -116
  28. package/dist/usb/index.js.map +0 -1
  29. package/dist/usb/interface.d.ts +0 -80
  30. package/dist/usb/interface.js +0 -133
  31. package/dist/usb/interface.js.map +0 -1
  32. package/dist/webusb/index.d.ts +0 -64
  33. package/dist/webusb/index.js +0 -295
  34. package/dist/webusb/index.js.map +0 -1
  35. package/dist/webusb/webusb-device.d.ts +0 -54
  36. package/dist/webusb/webusb-device.js +0 -434
  37. package/dist/webusb/webusb-device.js.map +0 -1
  38. package/libusb/.clang-tidy +0 -34
  39. package/libusb/.codespellrc +0 -3
  40. package/libusb/.private/README.txt +0 -5
  41. package/libusb/.private/appveyor_build.sh +0 -26
  42. package/libusb/.private/bm.sh +0 -54
  43. package/libusb/.private/ci-build.sh +0 -92
  44. package/libusb/.private/ci-container-build.sh +0 -67
  45. package/libusb/.private/post-rewrite.sh +0 -32
  46. package/libusb/.private/pre-commit.sh +0 -52
  47. package/libusb/.private/wbs.txt +0 -43
  48. package/libusb/.travis.yml +0 -58
  49. package/libusb/AUTHORS +0 -231
  50. package/libusb/COPYING +0 -504
  51. package/libusb/ChangeLog +0 -365
  52. package/libusb/HACKING +0 -25
  53. package/libusb/INSTALL_WIN.txt +0 -52
  54. package/libusb/KEYS +0 -123
  55. package/libusb/Makefile.am +0 -50
  56. package/libusb/NEWS +0 -2
  57. package/libusb/PORTING +0 -94
  58. package/libusb/README +0 -29
  59. package/libusb/README.git +0 -41
  60. package/libusb/TODO +0 -2
  61. package/libusb/Xcode/common.xcconfig +0 -92
  62. package/libusb/Xcode/config.h +0 -31
  63. package/libusb/Xcode/debug.xcconfig +0 -32
  64. package/libusb/Xcode/libusb.xcconfig +0 -21
  65. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +0 -1391
  66. package/libusb/Xcode/libusb_debug.xcconfig +0 -21
  67. package/libusb/Xcode/libusb_release.xcconfig +0 -21
  68. package/libusb/Xcode/release.xcconfig +0 -30
  69. package/libusb/android/README +0 -152
  70. package/libusb/android/config.h +0 -55
  71. package/libusb/android/examples/unrooted_android.c +0 -301
  72. package/libusb/android/examples/unrooted_android.h +0 -36
  73. package/libusb/android/jni/Android.mk +0 -23
  74. package/libusb/android/jni/Application.mk +0 -40
  75. package/libusb/android/jni/examples.mk +0 -168
  76. package/libusb/android/jni/libusb.mk +0 -60
  77. package/libusb/android/jni/tests.mk +0 -45
  78. package/libusb/appveyor.yml +0 -108
  79. package/libusb/autogen.sh +0 -10
  80. package/libusb/bootstrap.sh +0 -10
  81. package/libusb/configure.ac +0 -450
  82. package/libusb/doc/Makefile.in +0 -22
  83. package/libusb/doc/doxygen.cfg.in +0 -2571
  84. package/libusb/doc/libusb.png +0 -0
  85. package/libusb/examples/Makefile.am +0 -12
  86. package/libusb/examples/dpfp.c +0 -711
  87. package/libusb/examples/ezusb.c +0 -846
  88. package/libusb/examples/ezusb.h +0 -109
  89. package/libusb/examples/fxload.c +0 -310
  90. package/libusb/examples/hotplugtest.c +0 -147
  91. package/libusb/examples/listdevs.c +0 -73
  92. package/libusb/examples/sam3u_benchmark.c +0 -228
  93. package/libusb/examples/testlibusb.c +0 -312
  94. package/libusb/examples/xusb.c +0 -1254
  95. package/libusb/libusb/Makefile.am +0 -98
  96. package/libusb/libusb/Makefile.am.extra +0 -26
  97. package/libusb/libusb/core.c +0 -2925
  98. package/libusb/libusb/descriptor.c +0 -1558
  99. package/libusb/libusb/hotplug.c +0 -489
  100. package/libusb/libusb/io.c +0 -2865
  101. package/libusb/libusb/libusb-1.0.def +0 -199
  102. package/libusb/libusb/libusb-1.0.rc +0 -53
  103. package/libusb/libusb/libusb.h +0 -2421
  104. package/libusb/libusb/libusbi.h +0 -1535
  105. package/libusb/libusb/os/darwin_usb.c +0 -2977
  106. package/libusb/libusb/os/darwin_usb.h +0 -156
  107. package/libusb/libusb/os/emscripten_webusb.cpp +0 -875
  108. package/libusb/libusb/os/events_posix.c +0 -340
  109. package/libusb/libusb/os/events_posix.h +0 -62
  110. package/libusb/libusb/os/events_windows.c +0 -214
  111. package/libusb/libusb/os/events_windows.h +0 -46
  112. package/libusb/libusb/os/haiku_pollfs.cpp +0 -372
  113. package/libusb/libusb/os/haiku_usb.h +0 -113
  114. package/libusb/libusb/os/haiku_usb_backend.cpp +0 -532
  115. package/libusb/libusb/os/haiku_usb_raw.cpp +0 -231
  116. package/libusb/libusb/os/haiku_usb_raw.h +0 -188
  117. package/libusb/libusb/os/linux_netlink.c +0 -401
  118. package/libusb/libusb/os/linux_udev.c +0 -321
  119. package/libusb/libusb/os/linux_usbfs.c +0 -2829
  120. package/libusb/libusb/os/linux_usbfs.h +0 -221
  121. package/libusb/libusb/os/netbsd_usb.c +0 -617
  122. package/libusb/libusb/os/null_usb.c +0 -111
  123. package/libusb/libusb/os/openbsd_usb.c +0 -700
  124. package/libusb/libusb/os/sunos_usb.c +0 -1619
  125. package/libusb/libusb/os/sunos_usb.h +0 -79
  126. package/libusb/libusb/os/threads_posix.c +0 -126
  127. package/libusb/libusb/os/threads_posix.h +0 -98
  128. package/libusb/libusb/os/threads_windows.c +0 -40
  129. package/libusb/libusb/os/threads_windows.h +0 -113
  130. package/libusb/libusb/os/windows_common.c +0 -923
  131. package/libusb/libusb/os/windows_common.h +0 -424
  132. package/libusb/libusb/os/windows_usbdk.c +0 -724
  133. package/libusb/libusb/os/windows_usbdk.h +0 -106
  134. package/libusb/libusb/os/windows_winusb.c +0 -4766
  135. package/libusb/libusb/os/windows_winusb.h +0 -787
  136. package/libusb/libusb/strerror.c +0 -223
  137. package/libusb/libusb/sync.c +0 -342
  138. package/libusb/libusb/version.h +0 -18
  139. package/libusb/libusb/version_nano.h +0 -1
  140. package/libusb/libusb-1.0.pc.in +0 -11
  141. package/libusb/msvc/Base.props +0 -60
  142. package/libusb/msvc/Configuration.Application.props +0 -7
  143. package/libusb/msvc/Configuration.Base.props +0 -47
  144. package/libusb/msvc/Configuration.DynamicLibrary.props +0 -21
  145. package/libusb/msvc/Configuration.StaticLibrary.props +0 -7
  146. package/libusb/msvc/ProjectConfigurations.Base.props +0 -69
  147. package/libusb/msvc/build_all.ps1 +0 -17
  148. package/libusb/msvc/config.h +0 -58
  149. package/libusb/msvc/dpfp.vcxproj +0 -33
  150. package/libusb/msvc/dpfp_threaded.vcxproj +0 -38
  151. package/libusb/msvc/fxload.vcxproj +0 -46
  152. package/libusb/msvc/getopt/getopt.c +0 -1060
  153. package/libusb/msvc/getopt/getopt.h +0 -180
  154. package/libusb/msvc/getopt/getopt1.c +0 -188
  155. package/libusb/msvc/getopt.vcxproj +0 -33
  156. package/libusb/msvc/hotplugtest.vcxproj +0 -32
  157. package/libusb/msvc/init_context.vcxproj +0 -35
  158. package/libusb/msvc/libusb.sln +0 -542
  159. package/libusb/msvc/libusb_dll.vcxproj +0 -61
  160. package/libusb/msvc/libusb_static.vcxproj +0 -49
  161. package/libusb/msvc/listdevs.vcxproj +0 -32
  162. package/libusb/msvc/sam3u_benchmark.vcxproj +0 -33
  163. package/libusb/msvc/set_option.vcxproj +0 -35
  164. package/libusb/msvc/stress.vcxproj +0 -35
  165. package/libusb/msvc/stress_mt.vcxproj +0 -33
  166. package/libusb/msvc/testlibusb.vcxproj +0 -32
  167. package/libusb/msvc/xusb.vcxproj +0 -38
  168. package/libusb/tests/Makefile.am +0 -40
  169. package/libusb/tests/init_context.c +0 -153
  170. package/libusb/tests/libusb_testlib.h +0 -76
  171. package/libusb/tests/macos.c +0 -130
  172. package/libusb/tests/set_option.c +0 -253
  173. package/libusb/tests/stress.c +0 -172
  174. package/libusb/tests/stress_mt.c +0 -267
  175. package/libusb/tests/testlib.c +0 -184
  176. package/libusb/tests/umockdev.c +0 -1175
  177. package/libusb/tests/webusb-test-shim/index.js +0 -12
  178. package/libusb/tests/webusb-test-shim/package-lock.json +0 -50
  179. package/libusb/tests/webusb-test-shim/package.json +0 -10
  180. package/libusb.gypi +0 -154
  181. package/libusb_config/config.h +0 -1
  182. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  183. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  184. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  185. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  186. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  187. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  188. package/prebuilds/linux-ia32/node.napi.node +0 -0
  189. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  190. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  191. package/prebuilds/win32-arm64/node.napi.node +0 -0
  192. package/prebuilds/win32-ia32/node.napi.node +0 -0
  193. package/prebuilds/win32-x64/node.napi.node +0 -0
  194. package/src/device.cc +0 -439
  195. package/src/helpers.h +0 -64
  196. package/src/hotplug/hotplug.h +0 -22
  197. package/src/hotplug/libusb.cc +0 -90
  198. package/src/hotplug/windows.cc +0 -168
  199. package/src/node_usb.cc +0 -314
  200. package/src/node_usb.h +0 -131
  201. package/src/thread_name.cc +0 -79
  202. package/src/thread_name.h +0 -11
  203. package/src/transfer.cc +0 -143
  204. package/src/uv_async_queue.h +0 -41
  205. package/test/usb.coffee +0 -250
  206. package/test/webusb.coffee +0 -227
  207. package/test/worker.cjs +0 -13
@@ -1,489 +0,0 @@
1
- /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
2
- /*
3
- * Hotplug functions for libusb
4
- * Copyright © 2012-2021 Nathan Hjelm <hjelmn@mac.com>
5
- * Copyright © 2012-2013 Peter Stuge <peter@stuge.se>
6
- *
7
- * This library is free software; you can redistribute it and/or
8
- * modify it under the terms of the GNU Lesser General Public
9
- * License as published by the Free Software Foundation; either
10
- * version 2.1 of the License, or (at your option) any later version.
11
- *
12
- * This library is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
- * Lesser General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU Lesser General Public
18
- * License along with this library; if not, write to the Free Software
19
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
- */
21
-
22
- #include "libusbi.h"
23
-
24
- /**
25
- * @defgroup libusb_hotplug Device hotplug event notification
26
- * This page details how to use the libusb hotplug interface, where available.
27
- *
28
- * Be mindful that not all platforms currently implement hotplug notification and
29
- * that you should first call on \ref libusb_has_capability() with parameter
30
- * \ref LIBUSB_CAP_HAS_HOTPLUG to confirm that hotplug support is available.
31
- *
32
- * \page libusb_hotplug Device hotplug event notification
33
- *
34
- * \section hotplug_intro Introduction
35
- *
36
- * Version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102, has added support
37
- * for hotplug events on <b>some</b> platforms (you should test if your platform
38
- * supports hotplug notification by calling \ref libusb_has_capability() with
39
- * parameter \ref LIBUSB_CAP_HAS_HOTPLUG).
40
- *
41
- * This interface allows you to request notification for the arrival and departure
42
- * of matching USB devices.
43
- *
44
- * To receive hotplug notification you register a callback by calling
45
- * \ref libusb_hotplug_register_callback(). This function will optionally return
46
- * a callback handle that can be passed to \ref libusb_hotplug_deregister_callback().
47
- *
48
- * A callback function must return an int (0 or 1) indicating whether the callback is
49
- * expecting additional events. Returning 0 will rearm the callback and 1 will cause
50
- * the callback to be deregistered. Note that when callbacks are called from
51
- * libusb_hotplug_register_callback() because of the \ref LIBUSB_HOTPLUG_ENUMERATE
52
- * flag, the callback return value is ignored. In other words, you cannot cause a
53
- * callback to be deregistered by returning 1 when it is called from
54
- * libusb_hotplug_register_callback().
55
- *
56
- * Callbacks for a particular context are automatically deregistered by libusb_exit().
57
- *
58
- * As of 1.0.16 there are two supported hotplug events:
59
- * - LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: A device has arrived and is ready to use
60
- * - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: A device has left and is no longer available
61
- *
62
- * A hotplug event can listen for either or both of these events.
63
- *
64
- * Note: If you receive notification that a device has left and you have any
65
- * libusb_device_handles for the device it is up to you to call libusb_close()
66
- * on each device handle to free up any remaining resources associated with the device.
67
- * Once a device has left any libusb_device_handle associated with the device
68
- * are invalid and will remain so even if the device comes back.
69
- *
70
- * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED event it is considered
71
- * safe to call any libusb function that takes a libusb_device. It also safe to
72
- * open a device and submit asynchronous transfers. However, most other functions
73
- * that take a libusb_device_handle are <b>not</b> safe to call. Examples of such
74
- * functions are any of the \ref libusb_syncio "synchronous API" functions or the blocking
75
- * functions that retrieve various \ref libusb_desc "USB descriptors". These functions must
76
- * be used outside of the context of the hotplug callback.
77
- *
78
- * When handling a LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT event the only safe function
79
- * is libusb_get_device_descriptor().
80
- *
81
- * The following code provides an example of the usage of the hotplug interface:
82
- \code
83
- #include <stdio.h>
84
- #include <stdlib.h>
85
- #include <time.h>
86
- #include <libusb.h>
87
-
88
- static int count = 0;
89
-
90
- int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
91
- libusb_hotplug_event event, void *user_data) {
92
- static libusb_device_handle *dev_handle = NULL;
93
- struct libusb_device_descriptor desc;
94
- int rc;
95
-
96
- (void)libusb_get_device_descriptor(dev, &desc);
97
-
98
- if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
99
- rc = libusb_open(dev, &dev_handle);
100
- if (LIBUSB_SUCCESS != rc) {
101
- printf("Could not open USB device\n");
102
- }
103
- } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
104
- if (dev_handle) {
105
- libusb_close(dev_handle);
106
- dev_handle = NULL;
107
- }
108
- } else {
109
- printf("Unhandled event %d\n", event);
110
- }
111
- count++;
112
-
113
- return 0;
114
- }
115
-
116
- int main (void) {
117
- libusb_hotplug_callback_handle callback_handle;
118
- int rc;
119
-
120
- libusb_init_context(NULL, NULL, 0);
121
-
122
- rc = libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
123
- LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, 0x045a, 0x5005,
124
- LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL,
125
- &callback_handle);
126
- if (LIBUSB_SUCCESS != rc) {
127
- printf("Error creating a hotplug callback\n");
128
- libusb_exit(NULL);
129
- return EXIT_FAILURE;
130
- }
131
-
132
- while (count < 2) {
133
- libusb_handle_events_completed(NULL, NULL);
134
- nanosleep(&(struct timespec){0, 10000000UL}, NULL);
135
- }
136
-
137
- libusb_hotplug_deregister_callback(NULL, callback_handle);
138
- libusb_exit(NULL);
139
-
140
- return 0;
141
- }
142
- \endcode
143
- */
144
-
145
- #define VALID_HOTPLUG_EVENTS \
146
- (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | \
147
- LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
148
-
149
- #define VALID_HOTPLUG_FLAGS \
150
- (LIBUSB_HOTPLUG_ENUMERATE)
151
-
152
- void usbi_hotplug_init(struct libusb_context *ctx)
153
- {
154
- /* check for hotplug support */
155
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
156
- return;
157
-
158
- usbi_mutex_init(&ctx->hotplug_cbs_lock);
159
- list_init(&ctx->hotplug_cbs);
160
- ctx->next_hotplug_cb_handle = 1;
161
- usbi_atomic_store(&ctx->hotplug_ready, 1);
162
- }
163
-
164
- static void usbi_recursively_remove_parents(struct libusb_device *dev, struct libusb_device *next_dev)
165
- {
166
- if (dev && dev->parent_dev) {
167
- if (usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
168
- /* The parent was processed before this device in the list and
169
- * therefore has its ref count already decremented for its own ref.
170
- * The only remaining counted ref comes from its remaining single child.
171
- * It will thus be released when its child will be released. So we
172
- * can remove it from the list. This is safe as parent_dev cannot be
173
- * equal to next_dev given that we know at this point that it was
174
- * previously seen in the list. */
175
- assert (dev->parent_dev != next_dev);
176
- if (dev->parent_dev->list.next && dev->parent_dev->list.prev) {
177
- list_del(&dev->parent_dev->list);
178
- }
179
- }
180
-
181
- usbi_recursively_remove_parents(dev->parent_dev, next_dev);
182
- }
183
- }
184
-
185
- void usbi_hotplug_exit(struct libusb_context *ctx)
186
- {
187
- struct usbi_hotplug_callback *hotplug_cb, *next_cb;
188
- struct usbi_hotplug_message *msg;
189
- struct libusb_device *dev, *next_dev;
190
-
191
- /* check for hotplug support */
192
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
193
- return;
194
-
195
- if (!usbi_atomic_load(&ctx->hotplug_ready))
196
- return;
197
-
198
- /* free all registered hotplug callbacks */
199
- for_each_hotplug_cb_safe(ctx, hotplug_cb, next_cb) {
200
- list_del(&hotplug_cb->list);
201
- free(hotplug_cb);
202
- }
203
-
204
- /* free all pending hotplug messages */
205
- while (!list_empty(&ctx->hotplug_msgs)) {
206
- msg = list_first_entry(&ctx->hotplug_msgs, struct usbi_hotplug_message, list);
207
-
208
- /* if the device left, the message holds a reference
209
- * and we must drop it */
210
- if (msg->event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
211
- libusb_unref_device(msg->device);
212
-
213
- list_del(&msg->list);
214
- free(msg);
215
- }
216
-
217
- usbi_mutex_lock(&ctx->usb_devs_lock); /* hotplug thread might still be processing an already triggered event, possibly accessing this list as well */
218
- /* free all discovered devices */
219
- for_each_device_safe(ctx, dev, next_dev) {
220
- /* remove the device from the usb_devs list only if there are no
221
- * references held, otherwise leave it on the list so that a
222
- * warning message will be shown */
223
- if (usbi_atomic_load(&dev->refcnt) == 1) {
224
- list_del(&dev->list);
225
- }
226
-
227
- usbi_recursively_remove_parents(dev, next_dev);
228
-
229
- libusb_unref_device(dev);
230
- }
231
- usbi_mutex_unlock(&ctx->usb_devs_lock);
232
-
233
- usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
234
- }
235
-
236
- static int usbi_hotplug_match_cb(struct libusb_device *dev,
237
- libusb_hotplug_event event, struct usbi_hotplug_callback *hotplug_cb)
238
- {
239
- if (!(hotplug_cb->flags & event)) {
240
- return 0;
241
- }
242
-
243
- if ((hotplug_cb->flags & USBI_HOTPLUG_VENDOR_ID_VALID) &&
244
- hotplug_cb->vendor_id != dev->device_descriptor.idVendor) {
245
- return 0;
246
- }
247
-
248
- if ((hotplug_cb->flags & USBI_HOTPLUG_PRODUCT_ID_VALID) &&
249
- hotplug_cb->product_id != dev->device_descriptor.idProduct) {
250
- return 0;
251
- }
252
-
253
- if ((hotplug_cb->flags & USBI_HOTPLUG_DEV_CLASS_VALID) &&
254
- hotplug_cb->dev_class != dev->device_descriptor.bDeviceClass) {
255
- return 0;
256
- }
257
-
258
- return hotplug_cb->cb(DEVICE_CTX(dev), dev, event, hotplug_cb->user_data);
259
- }
260
-
261
- void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
262
- libusb_hotplug_event event)
263
- {
264
- struct usbi_hotplug_message *msg;
265
- unsigned int event_flags;
266
-
267
- /* Only generate a notification if hotplug is ready. This prevents hotplug
268
- * notifications from being generated during initial enumeration or if the
269
- * backend does not support hotplug. */
270
- if (!usbi_atomic_load(&ctx->hotplug_ready))
271
- return;
272
-
273
- msg = calloc(1, sizeof(*msg));
274
- if (!msg) {
275
- usbi_err(ctx, "error allocating hotplug message");
276
- return;
277
- }
278
-
279
- msg->event = event;
280
- msg->device = dev;
281
-
282
- /* Take the event data lock and add this message to the list.
283
- * Only signal an event if there are no prior pending events. */
284
- usbi_mutex_lock(&ctx->event_data_lock);
285
- event_flags = ctx->event_flags;
286
- ctx->event_flags |= USBI_EVENT_HOTPLUG_MSG_PENDING;
287
- list_add_tail(&msg->list, &ctx->hotplug_msgs);
288
- if (!event_flags)
289
- usbi_signal_event(&ctx->event);
290
- usbi_mutex_unlock(&ctx->event_data_lock);
291
- }
292
-
293
- void usbi_hotplug_process(struct libusb_context *ctx, struct list_head *hotplug_msgs)
294
- {
295
- struct usbi_hotplug_callback *hotplug_cb, *next_cb;
296
- struct usbi_hotplug_message *msg;
297
- int r;
298
-
299
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
300
-
301
- /* dispatch all pending hotplug messages */
302
- while (!list_empty(hotplug_msgs)) {
303
- msg = list_first_entry(hotplug_msgs, struct usbi_hotplug_message, list);
304
-
305
- for_each_hotplug_cb_safe(ctx, hotplug_cb, next_cb) {
306
- /* skip callbacks that have unregistered */
307
- if (hotplug_cb->flags & USBI_HOTPLUG_NEEDS_FREE)
308
- continue;
309
-
310
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
311
- r = usbi_hotplug_match_cb(msg->device, msg->event, hotplug_cb);
312
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
313
-
314
- if (r) {
315
- list_del(&hotplug_cb->list);
316
- free(hotplug_cb);
317
- }
318
- }
319
-
320
- /* if the device left, the message holds a reference
321
- * and we must drop it */
322
- if (msg->event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
323
- libusb_unref_device(msg->device);
324
-
325
- list_del(&msg->list);
326
- free(msg);
327
- }
328
-
329
- /* free any callbacks that have unregistered */
330
- for_each_hotplug_cb_safe(ctx, hotplug_cb, next_cb) {
331
- if (hotplug_cb->flags & USBI_HOTPLUG_NEEDS_FREE) {
332
- usbi_dbg(ctx, "freeing hotplug cb %p with handle %d",
333
- (void *) hotplug_cb, hotplug_cb->handle);
334
- list_del(&hotplug_cb->list);
335
- free(hotplug_cb);
336
- }
337
- }
338
-
339
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
340
- }
341
-
342
- int API_EXPORTED libusb_hotplug_register_callback(libusb_context *ctx,
343
- int events, int flags,
344
- int vendor_id, int product_id, int dev_class,
345
- libusb_hotplug_callback_fn cb_fn, void *user_data,
346
- libusb_hotplug_callback_handle *callback_handle)
347
- {
348
- struct usbi_hotplug_callback *hotplug_cb;
349
-
350
- /* check for sane values */
351
- if (!events || (~VALID_HOTPLUG_EVENTS & events) ||
352
- (~VALID_HOTPLUG_FLAGS & flags) ||
353
- (LIBUSB_HOTPLUG_MATCH_ANY != vendor_id && (~0xffff & vendor_id)) ||
354
- (LIBUSB_HOTPLUG_MATCH_ANY != product_id && (~0xffff & product_id)) ||
355
- (LIBUSB_HOTPLUG_MATCH_ANY != dev_class && (~0xff & dev_class)) ||
356
- !cb_fn) {
357
- return LIBUSB_ERROR_INVALID_PARAM;
358
- }
359
-
360
- /* check for hotplug support */
361
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
362
- return LIBUSB_ERROR_NOT_SUPPORTED;
363
-
364
- ctx = usbi_get_context(ctx);
365
-
366
- hotplug_cb = calloc(1, sizeof(*hotplug_cb));
367
- if (!hotplug_cb)
368
- return LIBUSB_ERROR_NO_MEM;
369
-
370
- hotplug_cb->flags = (uint8_t)events;
371
- if (LIBUSB_HOTPLUG_MATCH_ANY != vendor_id) {
372
- hotplug_cb->flags |= USBI_HOTPLUG_VENDOR_ID_VALID;
373
- hotplug_cb->vendor_id = (uint16_t)vendor_id;
374
- }
375
- if (LIBUSB_HOTPLUG_MATCH_ANY != product_id) {
376
- hotplug_cb->flags |= USBI_HOTPLUG_PRODUCT_ID_VALID;
377
- hotplug_cb->product_id = (uint16_t)product_id;
378
- }
379
- if (LIBUSB_HOTPLUG_MATCH_ANY != dev_class) {
380
- hotplug_cb->flags |= USBI_HOTPLUG_DEV_CLASS_VALID;
381
- hotplug_cb->dev_class = (uint8_t)dev_class;
382
- }
383
- hotplug_cb->cb = cb_fn;
384
- hotplug_cb->user_data = user_data;
385
-
386
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
387
-
388
- /* protect the handle by the context hotplug lock */
389
- hotplug_cb->handle = ctx->next_hotplug_cb_handle++;
390
-
391
- /* handle the unlikely case of overflow */
392
- if (ctx->next_hotplug_cb_handle < 0)
393
- ctx->next_hotplug_cb_handle = 1;
394
-
395
- list_add(&hotplug_cb->list, &ctx->hotplug_cbs);
396
-
397
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
398
-
399
- usbi_dbg(ctx, "new hotplug cb %p with handle %d",
400
- (void *) hotplug_cb, hotplug_cb->handle);
401
-
402
- if ((flags & LIBUSB_HOTPLUG_ENUMERATE) && (events & LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)) {
403
- ssize_t i, len;
404
- struct libusb_device **devs;
405
-
406
- len = libusb_get_device_list(ctx, &devs);
407
- if (len < 0) {
408
- libusb_hotplug_deregister_callback(ctx, hotplug_cb->handle);
409
- return (int)len;
410
- }
411
-
412
- for (i = 0; i < len; i++) {
413
- usbi_hotplug_match_cb(devs[i],
414
- LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
415
- hotplug_cb);
416
- }
417
-
418
- libusb_free_device_list(devs, 1);
419
- }
420
-
421
- if (callback_handle)
422
- *callback_handle = hotplug_cb->handle;
423
-
424
- return LIBUSB_SUCCESS;
425
- }
426
-
427
- void API_EXPORTED libusb_hotplug_deregister_callback(libusb_context *ctx,
428
- libusb_hotplug_callback_handle callback_handle)
429
- {
430
- struct usbi_hotplug_callback *hotplug_cb;
431
- int deregistered = 0;
432
-
433
- /* check for hotplug support */
434
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
435
- return;
436
-
437
- usbi_dbg(ctx, "deregister hotplug cb %d", callback_handle);
438
-
439
- ctx = usbi_get_context(ctx);
440
-
441
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
442
- for_each_hotplug_cb(ctx, hotplug_cb) {
443
- if (callback_handle == hotplug_cb->handle) {
444
- /* mark this callback for deregistration */
445
- hotplug_cb->flags |= USBI_HOTPLUG_NEEDS_FREE;
446
- deregistered = 1;
447
- break;
448
- }
449
- }
450
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
451
-
452
- if (deregistered) {
453
- unsigned int event_flags;
454
-
455
- usbi_mutex_lock(&ctx->event_data_lock);
456
- event_flags = ctx->event_flags;
457
- ctx->event_flags |= USBI_EVENT_HOTPLUG_CB_DEREGISTERED;
458
- if (!event_flags)
459
- usbi_signal_event(&ctx->event);
460
- usbi_mutex_unlock(&ctx->event_data_lock);
461
- }
462
- }
463
-
464
- DEFAULT_VISIBILITY
465
- void * LIBUSB_CALL libusb_hotplug_get_user_data(libusb_context *ctx,
466
- libusb_hotplug_callback_handle callback_handle)
467
- {
468
- struct usbi_hotplug_callback *hotplug_cb;
469
- void *user_data = NULL;
470
-
471
- /* check for hotplug support */
472
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
473
- return NULL;
474
-
475
- usbi_dbg(ctx, "get hotplug cb %d user data", callback_handle);
476
-
477
- ctx = usbi_get_context(ctx);
478
-
479
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
480
- for_each_hotplug_cb(ctx, hotplug_cb) {
481
- if (callback_handle == hotplug_cb->handle) {
482
- user_data = hotplug_cb->user_data;
483
- break;
484
- }
485
- }
486
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
487
-
488
- return user_data;
489
- }