usb 2.12.1 → 2.14.0

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 (228) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +11 -1
  3. package/dist/index.js +6 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/usb/bindings.d.ts +0 -1
  6. package/dist/usb/bindings.js +1 -1
  7. package/dist/usb/bindings.js.map +1 -1
  8. package/dist/usb/capability.d.ts +0 -1
  9. package/dist/usb/capability.js.map +1 -1
  10. package/dist/usb/descriptors.d.ts +0 -1
  11. package/dist/usb/device.d.ts +0 -1
  12. package/dist/usb/device.js.map +1 -1
  13. package/dist/usb/endpoint.d.ts +0 -1
  14. package/dist/usb/endpoint.js +2 -2
  15. package/dist/usb/endpoint.js.map +1 -1
  16. package/dist/usb/index.js.map +1 -1
  17. package/dist/usb/interface.js +2 -2
  18. package/dist/usb/interface.js.map +1 -1
  19. package/dist/webusb/index.d.ts +1 -3
  20. package/dist/webusb/index.js.map +1 -1
  21. package/dist/webusb/webusb-device.d.ts +0 -1
  22. package/dist/webusb/webusb-device.js +10 -10
  23. package/dist/webusb/webusb-device.js.map +1 -1
  24. package/libusb/.codespellrc +3 -0
  25. package/libusb/.private/appveyor_build.sh +5 -1
  26. package/libusb/.private/ci-build.sh +26 -1
  27. package/libusb/.private/ci-container-build.sh +2 -5
  28. package/libusb/.private/wbs.txt +5 -8
  29. package/libusb/AUTHORS +22 -0
  30. package/libusb/ChangeLog +19 -2
  31. package/libusb/HACKING +25 -0
  32. package/libusb/INSTALL_WIN.txt +11 -10
  33. package/libusb/NEWS +2 -2
  34. package/libusb/README +3 -2
  35. package/libusb/README.git +1 -1
  36. package/libusb/Xcode/common.xcconfig +8 -1
  37. package/libusb/Xcode/config.h +0 -6
  38. package/libusb/Xcode/debug.xcconfig +4 -1
  39. package/libusb/Xcode/libusb.xcconfig +1 -1
  40. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +60 -30
  41. package/libusb/Xcode/libusb_debug.xcconfig +1 -1
  42. package/libusb/Xcode/libusb_release.xcconfig +1 -1
  43. package/libusb/Xcode/release.xcconfig +1 -1
  44. package/libusb/appveyor.yml +33 -9
  45. package/libusb/configure.ac +68 -37
  46. package/libusb/examples/dpfp.c +2 -2
  47. package/libusb/examples/fxload.c +2 -2
  48. package/libusb/examples/hotplugtest.c +28 -13
  49. package/libusb/examples/listdevs.c +1 -1
  50. package/libusb/examples/sam3u_benchmark.c +1 -1
  51. package/libusb/examples/testlibusb.c +1 -1
  52. package/libusb/examples/xusb.c +74 -19
  53. package/libusb/libusb/Makefile.am +11 -1
  54. package/libusb/libusb/core.c +364 -186
  55. package/libusb/libusb/descriptor.c +276 -16
  56. package/libusb/libusb/hotplug.c +5 -4
  57. package/libusb/libusb/io.c +72 -61
  58. package/libusb/libusb/libusb-1.0.def +14 -1
  59. package/libusb/libusb/libusb.h +245 -76
  60. package/libusb/libusb/libusbi.h +35 -13
  61. package/libusb/libusb/os/darwin_usb.c +542 -279
  62. package/libusb/libusb/os/darwin_usb.h +44 -115
  63. package/libusb/libusb/os/emscripten_webusb.cpp +870 -0
  64. package/libusb/libusb/os/events_posix.c +40 -0
  65. package/libusb/libusb/os/events_posix.h +3 -0
  66. package/libusb/libusb/os/linux_usbfs.c +27 -16
  67. package/libusb/libusb/os/netbsd_usb.c +36 -36
  68. package/libusb/libusb/os/openbsd_usb.c +34 -34
  69. package/libusb/libusb/os/sunos_usb.c +25 -15
  70. package/libusb/libusb/os/threads_posix.c +1 -5
  71. package/libusb/libusb/os/windows_common.c +13 -5
  72. package/libusb/libusb/os/windows_common.h +8 -0
  73. package/libusb/libusb/os/windows_winusb.c +366 -174
  74. package/libusb/libusb/os/windows_winusb.h +13 -9
  75. package/libusb/libusb/strerror.c +5 -5
  76. package/libusb/libusb/sync.c +24 -19
  77. package/libusb/libusb/version.h +1 -1
  78. package/libusb/libusb/version_nano.h +1 -1
  79. package/libusb/msvc/Base.props +60 -0
  80. package/libusb/msvc/Configuration.Application.props +7 -0
  81. package/libusb/msvc/Configuration.Base.props +47 -0
  82. package/libusb/msvc/Configuration.DynamicLibrary.props +21 -0
  83. package/libusb/msvc/Configuration.StaticLibrary.props +7 -0
  84. package/libusb/msvc/ProjectConfigurations.Base.props +69 -0
  85. package/libusb/msvc/build_all.ps1 +17 -0
  86. package/libusb/msvc/config.h +2 -2
  87. package/libusb/msvc/dpfp.vcxproj +33 -0
  88. package/libusb/msvc/dpfp_threaded.vcxproj +38 -0
  89. package/libusb/msvc/fxload.vcxproj +46 -0
  90. package/libusb/msvc/getopt.vcxproj +33 -0
  91. package/libusb/msvc/hotplugtest.vcxproj +32 -0
  92. package/libusb/msvc/init_context.vcxproj +35 -0
  93. package/libusb/msvc/libusb.sln +542 -0
  94. package/libusb/msvc/libusb_dll.vcxproj +61 -0
  95. package/libusb/msvc/libusb_static.vcxproj +49 -0
  96. package/libusb/msvc/listdevs.vcxproj +32 -0
  97. package/libusb/msvc/sam3u_benchmark.vcxproj +33 -0
  98. package/libusb/msvc/set_option.vcxproj +35 -0
  99. package/libusb/msvc/stress.vcxproj +35 -0
  100. package/libusb/msvc/stress_mt.vcxproj +33 -0
  101. package/libusb/msvc/testlibusb.vcxproj +32 -0
  102. package/libusb/msvc/xusb.vcxproj +38 -0
  103. package/libusb/tests/Makefile.am +25 -3
  104. package/libusb/tests/init_context.c +153 -0
  105. package/libusb/tests/macos.c +130 -0
  106. package/libusb/tests/set_option.c +253 -0
  107. package/libusb/tests/stress.c +17 -14
  108. package/libusb/tests/stress_mt.c +265 -0
  109. package/libusb/tests/testlib.c +1 -1
  110. package/libusb/tests/umockdev.c +9 -9
  111. package/libusb/tests/webusb-test-shim/index.js +12 -0
  112. package/libusb/tests/webusb-test-shim/package-lock.json +50 -0
  113. package/libusb/tests/webusb-test-shim/package.json +10 -0
  114. package/package.json +8 -8
  115. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  116. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  117. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  118. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  119. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  120. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  121. package/prebuilds/linux-ia32/node.napi.node +0 -0
  122. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  123. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  124. package/prebuilds/win32-arm64/node.napi.node +0 -0
  125. package/prebuilds/win32-ia32/node.napi.node +0 -0
  126. package/prebuilds/win32-x64/node.napi.node +0 -0
  127. package/src/device.cc +16 -6
  128. package/src/transfer.cc +1 -1
  129. package/libusb/msvc/dpfp_2013.vcxproj +0 -87
  130. package/libusb/msvc/dpfp_2013.vcxproj.filters +0 -26
  131. package/libusb/msvc/dpfp_2015.vcxproj +0 -87
  132. package/libusb/msvc/dpfp_2015.vcxproj.filters +0 -26
  133. package/libusb/msvc/dpfp_2017.vcxproj +0 -106
  134. package/libusb/msvc/dpfp_2017.vcxproj.filters +0 -26
  135. package/libusb/msvc/dpfp_2019.vcxproj +0 -106
  136. package/libusb/msvc/dpfp_2019.vcxproj.filters +0 -26
  137. package/libusb/msvc/dpfp_threaded_2013.vcxproj +0 -87
  138. package/libusb/msvc/dpfp_threaded_2013.vcxproj.filters +0 -26
  139. package/libusb/msvc/dpfp_threaded_2015.vcxproj +0 -87
  140. package/libusb/msvc/dpfp_threaded_2015.vcxproj.filters +0 -26
  141. package/libusb/msvc/dpfp_threaded_2017.vcxproj +0 -106
  142. package/libusb/msvc/dpfp_threaded_2017.vcxproj.filters +0 -26
  143. package/libusb/msvc/dpfp_threaded_2019.vcxproj +0 -106
  144. package/libusb/msvc/dpfp_threaded_2019.vcxproj.filters +0 -26
  145. package/libusb/msvc/fxload_2013.vcxproj +0 -94
  146. package/libusb/msvc/fxload_2013.vcxproj.filters +0 -35
  147. package/libusb/msvc/fxload_2015.vcxproj +0 -94
  148. package/libusb/msvc/fxload_2015.vcxproj.filters +0 -35
  149. package/libusb/msvc/fxload_2017.vcxproj +0 -113
  150. package/libusb/msvc/fxload_2017.vcxproj.filters +0 -35
  151. package/libusb/msvc/fxload_2019.vcxproj +0 -113
  152. package/libusb/msvc/fxload_2019.vcxproj.filters +0 -35
  153. package/libusb/msvc/getopt_2013.vcxproj +0 -72
  154. package/libusb/msvc/getopt_2013.vcxproj.filters +0 -26
  155. package/libusb/msvc/getopt_2015.vcxproj +0 -73
  156. package/libusb/msvc/getopt_2015.vcxproj.filters +0 -26
  157. package/libusb/msvc/getopt_2017.vcxproj +0 -92
  158. package/libusb/msvc/getopt_2017.vcxproj.filters +0 -26
  159. package/libusb/msvc/getopt_2019.vcxproj +0 -92
  160. package/libusb/msvc/getopt_2019.vcxproj.filters +0 -26
  161. package/libusb/msvc/hotplugtest_2013.vcxproj +0 -86
  162. package/libusb/msvc/hotplugtest_2013.vcxproj.filters +0 -23
  163. package/libusb/msvc/hotplugtest_2015.vcxproj +0 -86
  164. package/libusb/msvc/hotplugtest_2015.vcxproj.filters +0 -23
  165. package/libusb/msvc/hotplugtest_2017.vcxproj +0 -105
  166. package/libusb/msvc/hotplugtest_2017.vcxproj.filters +0 -23
  167. package/libusb/msvc/hotplugtest_2019.vcxproj +0 -105
  168. package/libusb/msvc/hotplugtest_2019.vcxproj.filters +0 -23
  169. package/libusb/msvc/libusb_2013.sln +0 -137
  170. package/libusb/msvc/libusb_2015.sln +0 -137
  171. package/libusb/msvc/libusb_2017.sln +0 -240
  172. package/libusb/msvc/libusb_2019.sln +0 -240
  173. package/libusb/msvc/libusb_dll_2013.vcxproj +0 -104
  174. package/libusb/msvc/libusb_dll_2013.vcxproj.filters +0 -94
  175. package/libusb/msvc/libusb_dll_2015.vcxproj +0 -105
  176. package/libusb/msvc/libusb_dll_2015.vcxproj.filters +0 -94
  177. package/libusb/msvc/libusb_dll_2017.vcxproj +0 -124
  178. package/libusb/msvc/libusb_dll_2017.vcxproj.filters +0 -94
  179. package/libusb/msvc/libusb_dll_2019.vcxproj +0 -124
  180. package/libusb/msvc/libusb_dll_2019.vcxproj.filters +0 -94
  181. package/libusb/msvc/libusb_static_2013.vcxproj +0 -94
  182. package/libusb/msvc/libusb_static_2013.vcxproj.filters +0 -80
  183. package/libusb/msvc/libusb_static_2015.vcxproj +0 -95
  184. package/libusb/msvc/libusb_static_2015.vcxproj.filters +0 -80
  185. package/libusb/msvc/libusb_static_2017.vcxproj +0 -114
  186. package/libusb/msvc/libusb_static_2017.vcxproj.filters +0 -80
  187. package/libusb/msvc/libusb_static_2019.vcxproj +0 -114
  188. package/libusb/msvc/libusb_static_2019.vcxproj.filters +0 -80
  189. package/libusb/msvc/listdevs_2013.vcxproj +0 -86
  190. package/libusb/msvc/listdevs_2013.vcxproj.filters +0 -23
  191. package/libusb/msvc/listdevs_2015.vcxproj +0 -86
  192. package/libusb/msvc/listdevs_2015.vcxproj.filters +0 -23
  193. package/libusb/msvc/listdevs_2017.vcxproj +0 -105
  194. package/libusb/msvc/listdevs_2017.vcxproj.filters +0 -23
  195. package/libusb/msvc/listdevs_2019.vcxproj +0 -105
  196. package/libusb/msvc/listdevs_2019.vcxproj.filters +0 -23
  197. package/libusb/msvc/sam3u_benchmark_2013.vcxproj +0 -87
  198. package/libusb/msvc/sam3u_benchmark_2013.vcxproj.filters +0 -26
  199. package/libusb/msvc/sam3u_benchmark_2015.vcxproj +0 -87
  200. package/libusb/msvc/sam3u_benchmark_2015.vcxproj.filters +0 -26
  201. package/libusb/msvc/sam3u_benchmark_2017.vcxproj +0 -106
  202. package/libusb/msvc/sam3u_benchmark_2017.vcxproj.filters +0 -26
  203. package/libusb/msvc/sam3u_benchmark_2019.vcxproj +0 -106
  204. package/libusb/msvc/sam3u_benchmark_2019.vcxproj.filters +0 -26
  205. package/libusb/msvc/stress_2013.vcxproj +0 -89
  206. package/libusb/msvc/stress_2013.vcxproj.filters +0 -32
  207. package/libusb/msvc/stress_2015.vcxproj +0 -89
  208. package/libusb/msvc/stress_2015.vcxproj.filters +0 -32
  209. package/libusb/msvc/stress_2017.vcxproj +0 -108
  210. package/libusb/msvc/stress_2017.vcxproj.filters +0 -32
  211. package/libusb/msvc/stress_2019.vcxproj +0 -108
  212. package/libusb/msvc/stress_2019.vcxproj.filters +0 -32
  213. package/libusb/msvc/testlibusb_2013.vcxproj +0 -86
  214. package/libusb/msvc/testlibusb_2013.vcxproj.filters +0 -23
  215. package/libusb/msvc/testlibusb_2015.vcxproj +0 -86
  216. package/libusb/msvc/testlibusb_2015.vcxproj.filters +0 -23
  217. package/libusb/msvc/testlibusb_2017.vcxproj +0 -105
  218. package/libusb/msvc/testlibusb_2017.vcxproj.filters +0 -23
  219. package/libusb/msvc/testlibusb_2019.vcxproj +0 -105
  220. package/libusb/msvc/testlibusb_2019.vcxproj.filters +0 -23
  221. package/libusb/msvc/xusb_2013.vcxproj +0 -86
  222. package/libusb/msvc/xusb_2013.vcxproj.filters +0 -23
  223. package/libusb/msvc/xusb_2015.vcxproj +0 -86
  224. package/libusb/msvc/xusb_2015.vcxproj.filters +0 -23
  225. package/libusb/msvc/xusb_2017.vcxproj +0 -105
  226. package/libusb/msvc/xusb_2017.vcxproj.filters +0 -23
  227. package/libusb/msvc/xusb_2019.vcxproj +0 -105
  228. package/libusb/msvc/xusb_2019.vcxproj.filters +0 -23
@@ -28,6 +28,31 @@
28
28
  #ifdef HAVE_TIMERFD
29
29
  #include <sys/timerfd.h>
30
30
  #endif
31
+
32
+ #ifdef __EMSCRIPTEN__
33
+ /* On Emscripten `pipe` does not conform to the spec and does not block
34
+ * until events are available, which makes it unusable for event system
35
+ * and often results in deadlocks when `pipe` is in a loop like it is
36
+ * in libusb.
37
+ *
38
+ * Therefore use a custom event system based on browser event emitters. */
39
+ #include <emscripten.h>
40
+ #include <emscripten/atomic.h>
41
+ #include <emscripten/threading.h>
42
+
43
+ EM_ASYNC_JS(void, em_libusb_wait_async, (const _Atomic int* ptr, int expected_value, int timeout), {
44
+ await Atomics.waitAsync(HEAP32, ptr >> 2, expected_value, timeout).value;
45
+ });
46
+
47
+ static void em_libusb_wait(const _Atomic int *ptr, int expected_value, int timeout)
48
+ {
49
+ if (emscripten_is_main_runtime_thread()) {
50
+ em_libusb_wait_async(ptr, expected_value, timeout);
51
+ } else {
52
+ emscripten_atomic_wait_u32((int*)ptr, expected_value, 1000000LL * timeout);
53
+ }
54
+ }
55
+ #endif
31
56
  #include <unistd.h>
32
57
 
33
58
  #ifdef HAVE_EVENTFD
@@ -131,6 +156,10 @@ void usbi_signal_event(usbi_event_t *event)
131
156
  r = write(EVENT_WRITE_FD(event), &dummy, sizeof(dummy));
132
157
  if (r != sizeof(dummy))
133
158
  usbi_warn(NULL, "event write failed");
159
+ #ifdef __EMSCRIPTEN__
160
+ event->has_event = 1;
161
+ emscripten_atomic_notify(&event->has_event, EMSCRIPTEN_NOTIFY_ALL_WAITERS);
162
+ #endif
134
163
  }
135
164
 
136
165
  void usbi_clear_event(usbi_event_t *event)
@@ -141,6 +170,9 @@ void usbi_clear_event(usbi_event_t *event)
141
170
  r = read(EVENT_READ_FD(event), &dummy, sizeof(dummy));
142
171
  if (r != sizeof(dummy))
143
172
  usbi_warn(NULL, "event read failed");
173
+ #ifdef __EMSCRIPTEN__
174
+ event->has_event = 0;
175
+ #endif
144
176
  }
145
177
 
146
178
  #ifdef HAVE_TIMERFD
@@ -223,6 +255,14 @@ int usbi_wait_for_events(struct libusb_context *ctx,
223
255
  int internal_fds, num_ready;
224
256
 
225
257
  usbi_dbg(ctx, "poll() %u fds with timeout in %dms", (unsigned int)nfds, timeout_ms);
258
+ #ifdef __EMSCRIPTEN__
259
+ // Emscripten's poll doesn't actually block, so we need to use an out-of-band
260
+ // waiting signal.
261
+ em_libusb_wait(&ctx->event.has_event, 0, timeout_ms);
262
+ // Emscripten ignores timeout_ms, but set it to 0 for future-proofing in case
263
+ // they ever implement real poll.
264
+ timeout_ms = 0;
265
+ #endif
226
266
  num_ready = poll(fds, nfds, timeout_ms);
227
267
  usbi_dbg(ctx, "poll() returned %d", num_ready);
228
268
  if (num_ready == 0) {
@@ -36,6 +36,9 @@ typedef struct usbi_event {
36
36
  #else
37
37
  typedef struct usbi_event {
38
38
  int pipefd[2];
39
+ #ifdef __EMSCRIPTEN__
40
+ _Atomic int has_event;
41
+ #endif
39
42
  } usbi_event_t;
40
43
  #define USBI_EVENT_OS_HANDLE(e) ((e)->pipefd[0])
41
44
  #define USBI_EVENT_POLL_EVENTS POLLIN
@@ -95,9 +95,6 @@ static int sysfs_available = -1;
95
95
  /* how many times have we initted (and not exited) ? */
96
96
  static int init_count = 0;
97
97
 
98
- /* have no authority to operate usb device directly */
99
- static int no_enumeration = 0;
100
-
101
98
  /* Serialize scan-devices, event-thread, and poll */
102
99
  usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
103
100
 
@@ -119,6 +116,11 @@ struct config_descriptor {
119
116
  size_t actual_len;
120
117
  };
121
118
 
119
+ struct linux_context_priv {
120
+ /* no enumeration or hot-plug detection */
121
+ int no_device_discovery;
122
+ };
123
+
122
124
  struct linux_device_priv {
123
125
  char *sysfs_dir;
124
126
  void *descriptors;
@@ -187,10 +189,10 @@ static int get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
187
189
  int fd;
188
190
 
189
191
  if (usbdev_names)
190
- sprintf(path, USBDEV_PATH "/usbdev%u.%u",
192
+ snprintf(path, sizeof(path), USBDEV_PATH "/usbdev%u.%u",
191
193
  dev->bus_number, dev->device_address);
192
194
  else
193
- sprintf(path, USB_DEVTMPFS_PATH "/%03u/%03u",
195
+ snprintf(path, sizeof(path), USB_DEVTMPFS_PATH "/%03u/%03u",
194
196
  dev->bus_number, dev->device_address);
195
197
 
196
198
  fd = open(path, mode | O_CLOEXEC);
@@ -354,6 +356,7 @@ static int op_init(struct libusb_context *ctx)
354
356
  struct kernel_version kversion;
355
357
  const char *usbfs_path;
356
358
  int r;
359
+ struct linux_context_priv *cpriv = usbi_get_context_priv(ctx);
357
360
 
358
361
  if (get_kernel_version(ctx, &kversion) < 0)
359
362
  return LIBUSB_ERROR_OTHER;
@@ -397,7 +400,7 @@ static int op_init(struct libusb_context *ctx)
397
400
  }
398
401
  }
399
402
 
400
- if (no_enumeration) {
403
+ if (cpriv->no_device_discovery) {
401
404
  return LIBUSB_SUCCESS;
402
405
  }
403
406
 
@@ -421,9 +424,9 @@ static int op_init(struct libusb_context *ctx)
421
424
 
422
425
  static void op_exit(struct libusb_context *ctx)
423
426
  {
424
- UNUSED(ctx);
427
+ struct linux_context_priv *cpriv = usbi_get_context_priv(ctx);
425
428
 
426
- if (no_enumeration) {
429
+ if (cpriv->no_device_discovery) {
427
430
  return;
428
431
  }
429
432
 
@@ -436,12 +439,13 @@ static void op_exit(struct libusb_context *ctx)
436
439
 
437
440
  static int op_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap)
438
441
  {
439
- UNUSED(ctx);
440
442
  UNUSED(ap);
441
443
 
442
444
  if (option == LIBUSB_OPTION_NO_DEVICE_DISCOVERY) {
443
- usbi_dbg(ctx, "no enumeration will be performed");
444
- no_enumeration = 1;
445
+ struct linux_context_priv *cpriv = usbi_get_context_priv(ctx);
446
+
447
+ usbi_dbg(ctx, "no device discovery will be performed");
448
+ cpriv->no_device_discovery = 1;
445
449
  return LIBUSB_SUCCESS;
446
450
  }
447
451
 
@@ -597,7 +601,7 @@ int linux_get_device_address(struct libusb_context *ctx, int detached,
597
601
  char proc_path[32];
598
602
 
599
603
  /* try to retrieve the device node from fd */
600
- sprintf(proc_path, "/proc/self/fd/%d", fd);
604
+ snprintf(proc_path, sizeof(proc_path), "/proc/self/fd/%d", fd);
601
605
  r = readlink(proc_path, fd_path, PATH_MAX - 1);
602
606
  if (r > 0) {
603
607
  fd_path[r] = '\0';
@@ -648,7 +652,7 @@ static int seek_to_next_config(struct libusb_context *ctx,
648
652
 
649
653
  while (len > 0) {
650
654
  if (len < 2) {
651
- usbi_err(ctx, "short descriptor read %zu/2", len);
655
+ usbi_err(ctx, "remaining descriptor length too small %zu/2", len);
652
656
  return LIBUSB_ERROR_IO;
653
657
  }
654
658
 
@@ -656,6 +660,11 @@ static int seek_to_next_config(struct libusb_context *ctx,
656
660
  if (header->bDescriptorType == LIBUSB_DT_CONFIG)
657
661
  return offset;
658
662
 
663
+ if (header->bLength < 2) {
664
+ usbi_err(ctx, "invalid descriptor bLength %hhu", header->bLength);
665
+ return LIBUSB_ERROR_IO;
666
+ }
667
+
659
668
  if (len < header->bLength) {
660
669
  usbi_err(ctx, "bLength overflow by %zu bytes",
661
670
  (size_t)header->bLength - len);
@@ -1079,8 +1088,9 @@ retry:
1079
1088
  goto retry;
1080
1089
  }
1081
1090
 
1082
- usbi_dbg(ctx, "dev %p (%s) has parent %p (%s) port %u", dev, sysfs_dir,
1083
- dev->parent_dev, parent_sysfs_dir, dev->port_number);
1091
+ usbi_dbg(ctx, "dev %p (%s) has parent %p (%s) port %u",
1092
+ (void *) dev, sysfs_dir, (void *) dev->parent_dev,
1093
+ parent_sysfs_dir, dev->port_number);
1084
1094
 
1085
1095
  free(parent_sysfs_dir);
1086
1096
 
@@ -1188,7 +1198,7 @@ static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
1188
1198
  struct dirent *entry;
1189
1199
  int r = LIBUSB_ERROR_IO;
1190
1200
 
1191
- sprintf(dirpath, USB_DEVTMPFS_PATH "/%03u", busnum);
1201
+ snprintf(dirpath, sizeof(dirpath), USB_DEVTMPFS_PATH "/%03u", busnum);
1192
1202
  usbi_dbg(ctx, "%s", dirpath);
1193
1203
  dir = opendir(dirpath);
1194
1204
  if (!dir) {
@@ -2801,6 +2811,7 @@ const struct usbi_os_backend usbi_backend = {
2801
2811
 
2802
2812
  .handle_events = op_handle_events,
2803
2813
 
2814
+ .context_priv_size = sizeof(struct linux_context_priv),
2804
2815
  .device_priv_size = sizeof(struct linux_device_priv),
2805
2816
  .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2806
2817
  .transfer_priv_size = sizeof(struct linux_transfer_priv),
@@ -144,11 +144,11 @@ netbsd_get_device_list(struct libusb_context * ctx,
144
144
  if (dev == NULL) {
145
145
  dev = usbi_alloc_device(ctx, session_id);
146
146
  if (dev == NULL)
147
- return (LIBUSB_ERROR_NO_MEM);
147
+ return LIBUSB_ERROR_NO_MEM;
148
148
 
149
- dev->bus_number = di.udi_bus;
150
- dev->device_address = di.udi_addr;
151
- dev->speed = di.udi_speed;
149
+ dev->bus_number = 1 + di.udi_bus;
150
+ dev->device_address = 1 + di.udi_addr;
151
+ dev->speed = di.udi_speed; /* NetBSD #define's happen to match libusb enum */
152
152
 
153
153
  dpriv = usbi_get_device_priv(dev);
154
154
  strlcpy(dpriv->devnode, devnode, sizeof(devnode));
@@ -175,12 +175,12 @@ netbsd_get_device_list(struct libusb_context * ctx,
175
175
  close(fd);
176
176
 
177
177
  if (discovered_devs_append(*discdevs, dev) == NULL)
178
- return (LIBUSB_ERROR_NO_MEM);
178
+ return LIBUSB_ERROR_NO_MEM;
179
179
 
180
180
  libusb_unref_device(dev);
181
181
  }
182
182
 
183
- return (LIBUSB_SUCCESS);
183
+ return LIBUSB_SUCCESS;
184
184
 
185
185
  error:
186
186
  close(fd);
@@ -207,7 +207,7 @@ netbsd_open(struct libusb_device_handle *handle)
207
207
 
208
208
  usbi_dbg(HANDLE_CTX(handle), "open %s: fd %d", dpriv->devnode, dpriv->fd);
209
209
 
210
- return (LIBUSB_SUCCESS);
210
+ return LIBUSB_SUCCESS;
211
211
  }
212
212
 
213
213
  void
@@ -259,7 +259,7 @@ netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
259
259
  ufd.ufd_size = len;
260
260
  ufd.ufd_data = buf;
261
261
 
262
- if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
262
+ if (ioctl(fd, USB_GET_FULL_DESC, &ufd) < 0) {
263
263
  err = errno;
264
264
  if (dpriv->fd < 0)
265
265
  close(fd);
@@ -286,7 +286,7 @@ netbsd_get_configuration(struct libusb_device_handle *handle, uint8_t *config)
286
286
  usbi_dbg(HANDLE_CTX(handle), "configuration %d", tmp);
287
287
  *config = (uint8_t)tmp;
288
288
 
289
- return (LIBUSB_SUCCESS);
289
+ return LIBUSB_SUCCESS;
290
290
  }
291
291
 
292
292
  int
@@ -313,7 +313,7 @@ netbsd_claim_interface(struct libusb_device_handle *handle, uint8_t iface)
313
313
  for (i = 0; i < USB_MAX_ENDPOINTS; i++)
314
314
  hpriv->endpoints[i] = -1;
315
315
 
316
- return (LIBUSB_SUCCESS);
316
+ return LIBUSB_SUCCESS;
317
317
  }
318
318
 
319
319
  int
@@ -328,7 +328,7 @@ netbsd_release_interface(struct libusb_device_handle *handle, uint8_t iface)
328
328
  if (hpriv->endpoints[i] >= 0)
329
329
  close(hpriv->endpoints[i]);
330
330
 
331
- return (LIBUSB_SUCCESS);
331
+ return LIBUSB_SUCCESS;
332
332
  }
333
333
 
334
334
  int
@@ -348,7 +348,7 @@ netbsd_set_interface_altsetting(struct libusb_device_handle *handle, uint8_t ifa
348
348
  if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
349
349
  return _errno_to_libusb(errno);
350
350
 
351
- return (LIBUSB_SUCCESS);
351
+ return LIBUSB_SUCCESS;
352
352
  }
353
353
 
354
354
  int
@@ -368,7 +368,7 @@ netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
368
368
  if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
369
369
  return _errno_to_libusb(errno);
370
370
 
371
- return (LIBUSB_SUCCESS);
371
+ return LIBUSB_SUCCESS;
372
372
  }
373
373
 
374
374
  void
@@ -418,11 +418,11 @@ netbsd_submit_transfer(struct usbi_transfer *itransfer)
418
418
  }
419
419
 
420
420
  if (err)
421
- return (err);
421
+ return err;
422
422
 
423
423
  usbi_signal_transfer_completion(itransfer);
424
424
 
425
- return (LIBUSB_SUCCESS);
425
+ return LIBUSB_SUCCESS;
426
426
  }
427
427
 
428
428
  int
@@ -432,7 +432,7 @@ netbsd_cancel_transfer(struct usbi_transfer *itransfer)
432
432
 
433
433
  usbi_dbg(ITRANSFER_CTX(itransfer), " ");
434
434
 
435
- return (LIBUSB_ERROR_NOT_SUPPORTED);
435
+ return LIBUSB_ERROR_NOT_SUPPORTED;
436
436
  }
437
437
 
438
438
  int
@@ -446,21 +446,21 @@ _errno_to_libusb(int err)
446
446
  {
447
447
  switch (err) {
448
448
  case EIO:
449
- return (LIBUSB_ERROR_IO);
449
+ return LIBUSB_ERROR_IO;
450
450
  case EACCES:
451
- return (LIBUSB_ERROR_ACCESS);
451
+ return LIBUSB_ERROR_ACCESS;
452
452
  case ENOENT:
453
- return (LIBUSB_ERROR_NO_DEVICE);
453
+ return LIBUSB_ERROR_NO_DEVICE;
454
454
  case ENOMEM:
455
- return (LIBUSB_ERROR_NO_MEM);
455
+ return LIBUSB_ERROR_NO_MEM;
456
456
  case EWOULDBLOCK:
457
457
  case ETIMEDOUT:
458
- return (LIBUSB_ERROR_TIMEOUT);
458
+ return LIBUSB_ERROR_TIMEOUT;
459
459
  }
460
460
 
461
- usbi_dbg(NULL, "error: %s", strerror(err));
461
+ usbi_dbg(NULL, "error: %s (%d)", strerror(err), err);
462
462
 
463
- return (LIBUSB_ERROR_OTHER);
463
+ return LIBUSB_ERROR_OTHER;
464
464
  }
465
465
 
466
466
  int
@@ -476,7 +476,7 @@ _cache_active_config_descriptor(struct libusb_device *dev, int fd)
476
476
 
477
477
  ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
478
478
 
479
- if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
479
+ if (ioctl(fd, USB_GET_CONFIG_DESC, &ucd) < 0)
480
480
  return _errno_to_libusb(errno);
481
481
 
482
482
  usbi_dbg(DEVICE_CTX(dev), "active bLength %d", ucd.ucd_desc.bLength);
@@ -484,7 +484,7 @@ _cache_active_config_descriptor(struct libusb_device *dev, int fd)
484
484
  len = UGETW(ucd.ucd_desc.wTotalLength);
485
485
  buf = malloc((size_t)len);
486
486
  if (buf == NULL)
487
- return (LIBUSB_ERROR_NO_MEM);
487
+ return LIBUSB_ERROR_NO_MEM;
488
488
 
489
489
  ufd.ufd_config_index = ucd.ucd_config_index;
490
490
  ufd.ufd_size = len;
@@ -492,7 +492,7 @@ _cache_active_config_descriptor(struct libusb_device *dev, int fd)
492
492
 
493
493
  usbi_dbg(DEVICE_CTX(dev), "index %d, len %d", ufd.ufd_config_index, len);
494
494
 
495
- if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
495
+ if (ioctl(fd, USB_GET_FULL_DESC, &ufd) < 0) {
496
496
  free(buf);
497
497
  return _errno_to_libusb(errno);
498
498
  }
@@ -501,7 +501,7 @@ _cache_active_config_descriptor(struct libusb_device *dev, int fd)
501
501
  free(dpriv->cdesc);
502
502
  dpriv->cdesc = buf;
503
503
 
504
- return (0);
504
+ return 0;
505
505
  }
506
506
 
507
507
  int
@@ -516,7 +516,7 @@ _sync_control_transfer(struct usbi_transfer *itransfer)
516
516
  dpriv = usbi_get_device_priv(transfer->dev_handle->dev);
517
517
  setup = (struct libusb_control_setup *)transfer->buffer;
518
518
 
519
- usbi_dbg(ITRANSFER_CTX(itransfer), "type %d request %d value %d index %d length %d timeout %d",
519
+ usbi_dbg(ITRANSFER_CTX(itransfer), "type 0x%x request 0x%x value 0x%x index %d length %d timeout %d",
520
520
  setup->bmRequestType, setup->bRequest,
521
521
  libusb_le16_to_cpu(setup->wValue),
522
522
  libusb_le16_to_cpu(setup->wIndex),
@@ -533,17 +533,17 @@ _sync_control_transfer(struct usbi_transfer *itransfer)
533
533
  if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
534
534
  req.ucr_flags = USBD_SHORT_XFER_OK;
535
535
 
536
- if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
536
+ if (ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout) < 0)
537
537
  return _errno_to_libusb(errno);
538
538
 
539
- if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
539
+ if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
540
540
  return _errno_to_libusb(errno);
541
541
 
542
542
  itransfer->transferred = req.ucr_actlen;
543
543
 
544
544
  usbi_dbg(ITRANSFER_CTX(itransfer), "transferred %d", itransfer->transferred);
545
545
 
546
- return (0);
546
+ return 0;
547
547
  }
548
548
 
549
549
  int
@@ -572,12 +572,12 @@ _access_endpoint(struct libusb_transfer *transfer)
572
572
  /* We may need to read/write to the same endpoint later. */
573
573
  if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
574
574
  if ((fd = open(devnode, mode)) < 0)
575
- return (-1);
575
+ return -1;
576
576
 
577
577
  hpriv->endpoints[endpt] = fd;
578
578
  }
579
579
 
580
- return (hpriv->endpoints[endpt]);
580
+ return hpriv->endpoints[endpt];
581
581
  }
582
582
 
583
583
  int
@@ -595,12 +595,12 @@ _sync_gen_transfer(struct usbi_transfer *itransfer)
595
595
  if ((fd = _access_endpoint(transfer)) < 0)
596
596
  return _errno_to_libusb(errno);
597
597
 
598
- if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
598
+ if (ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout) < 0)
599
599
  return _errno_to_libusb(errno);
600
600
 
601
601
  if (IS_XFERIN(transfer)) {
602
602
  if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
603
- if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
603
+ if (ioctl(fd, USB_SET_SHORT_XFER, &nr) < 0)
604
604
  return _errno_to_libusb(errno);
605
605
 
606
606
  nr = read(fd, transfer->buffer, transfer->length);
@@ -613,5 +613,5 @@ _sync_gen_transfer(struct usbi_transfer *itransfer)
613
613
 
614
614
  itransfer->transferred = nr;
615
615
 
616
- return (0);
616
+ return 0;
617
617
  }