usb 2.12.1 → 2.13.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 (222) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +1 -1
  3. package/dist/index.js +6 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/usb/bindings.js +1 -1
  6. package/dist/usb/bindings.js.map +1 -1
  7. package/dist/usb/capability.js.map +1 -1
  8. package/dist/usb/device.js.map +1 -1
  9. package/dist/usb/endpoint.d.ts +1 -0
  10. package/dist/usb/endpoint.js +2 -2
  11. package/dist/usb/endpoint.js.map +1 -1
  12. package/dist/usb/index.js.map +1 -1
  13. package/dist/usb/interface.js +2 -2
  14. package/dist/usb/interface.js.map +1 -1
  15. package/dist/webusb/index.d.ts +2 -2
  16. package/dist/webusb/index.js.map +1 -1
  17. package/dist/webusb/webusb-device.js +10 -10
  18. package/dist/webusb/webusb-device.js.map +1 -1
  19. package/libusb/.codespellrc +3 -0
  20. package/libusb/.private/appveyor_build.sh +5 -1
  21. package/libusb/.private/ci-build.sh +26 -1
  22. package/libusb/.private/ci-container-build.sh +2 -5
  23. package/libusb/.private/wbs.txt +5 -8
  24. package/libusb/AUTHORS +22 -0
  25. package/libusb/ChangeLog +19 -2
  26. package/libusb/HACKING +25 -0
  27. package/libusb/INSTALL_WIN.txt +11 -10
  28. package/libusb/NEWS +2 -2
  29. package/libusb/README +3 -2
  30. package/libusb/README.git +1 -1
  31. package/libusb/Xcode/common.xcconfig +8 -1
  32. package/libusb/Xcode/config.h +0 -6
  33. package/libusb/Xcode/debug.xcconfig +4 -1
  34. package/libusb/Xcode/libusb.xcconfig +1 -1
  35. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +60 -30
  36. package/libusb/Xcode/libusb_debug.xcconfig +1 -1
  37. package/libusb/Xcode/libusb_release.xcconfig +1 -1
  38. package/libusb/Xcode/release.xcconfig +1 -1
  39. package/libusb/appveyor.yml +33 -9
  40. package/libusb/configure.ac +68 -37
  41. package/libusb/examples/dpfp.c +2 -2
  42. package/libusb/examples/fxload.c +2 -2
  43. package/libusb/examples/hotplugtest.c +28 -13
  44. package/libusb/examples/listdevs.c +1 -1
  45. package/libusb/examples/sam3u_benchmark.c +1 -1
  46. package/libusb/examples/testlibusb.c +1 -1
  47. package/libusb/examples/xusb.c +74 -19
  48. package/libusb/libusb/Makefile.am +11 -1
  49. package/libusb/libusb/core.c +364 -186
  50. package/libusb/libusb/descriptor.c +276 -16
  51. package/libusb/libusb/hotplug.c +5 -4
  52. package/libusb/libusb/io.c +72 -61
  53. package/libusb/libusb/libusb-1.0.def +14 -1
  54. package/libusb/libusb/libusb.h +245 -76
  55. package/libusb/libusb/libusbi.h +35 -13
  56. package/libusb/libusb/os/darwin_usb.c +542 -279
  57. package/libusb/libusb/os/darwin_usb.h +44 -115
  58. package/libusb/libusb/os/emscripten_webusb.cpp +870 -0
  59. package/libusb/libusb/os/events_posix.c +40 -0
  60. package/libusb/libusb/os/events_posix.h +3 -0
  61. package/libusb/libusb/os/linux_usbfs.c +27 -16
  62. package/libusb/libusb/os/netbsd_usb.c +36 -36
  63. package/libusb/libusb/os/openbsd_usb.c +34 -34
  64. package/libusb/libusb/os/sunos_usb.c +25 -15
  65. package/libusb/libusb/os/threads_posix.c +1 -5
  66. package/libusb/libusb/os/windows_common.c +13 -5
  67. package/libusb/libusb/os/windows_common.h +8 -0
  68. package/libusb/libusb/os/windows_winusb.c +366 -174
  69. package/libusb/libusb/os/windows_winusb.h +13 -9
  70. package/libusb/libusb/strerror.c +5 -5
  71. package/libusb/libusb/sync.c +24 -19
  72. package/libusb/libusb/version.h +1 -1
  73. package/libusb/libusb/version_nano.h +1 -1
  74. package/libusb/msvc/Base.props +60 -0
  75. package/libusb/msvc/Configuration.Application.props +7 -0
  76. package/libusb/msvc/Configuration.Base.props +47 -0
  77. package/libusb/msvc/Configuration.DynamicLibrary.props +21 -0
  78. package/libusb/msvc/Configuration.StaticLibrary.props +7 -0
  79. package/libusb/msvc/ProjectConfigurations.Base.props +69 -0
  80. package/libusb/msvc/build_all.ps1 +17 -0
  81. package/libusb/msvc/config.h +2 -2
  82. package/libusb/msvc/dpfp.vcxproj +33 -0
  83. package/libusb/msvc/dpfp_threaded.vcxproj +38 -0
  84. package/libusb/msvc/fxload.vcxproj +46 -0
  85. package/libusb/msvc/getopt.vcxproj +33 -0
  86. package/libusb/msvc/hotplugtest.vcxproj +32 -0
  87. package/libusb/msvc/init_context.vcxproj +35 -0
  88. package/libusb/msvc/libusb.sln +542 -0
  89. package/libusb/msvc/libusb_dll.vcxproj +61 -0
  90. package/libusb/msvc/libusb_static.vcxproj +49 -0
  91. package/libusb/msvc/listdevs.vcxproj +32 -0
  92. package/libusb/msvc/sam3u_benchmark.vcxproj +33 -0
  93. package/libusb/msvc/set_option.vcxproj +35 -0
  94. package/libusb/msvc/stress.vcxproj +35 -0
  95. package/libusb/msvc/stress_mt.vcxproj +33 -0
  96. package/libusb/msvc/testlibusb.vcxproj +32 -0
  97. package/libusb/msvc/xusb.vcxproj +38 -0
  98. package/libusb/tests/Makefile.am +25 -3
  99. package/libusb/tests/init_context.c +153 -0
  100. package/libusb/tests/macos.c +130 -0
  101. package/libusb/tests/set_option.c +253 -0
  102. package/libusb/tests/stress.c +17 -14
  103. package/libusb/tests/stress_mt.c +265 -0
  104. package/libusb/tests/testlib.c +1 -1
  105. package/libusb/tests/umockdev.c +9 -9
  106. package/libusb/tests/webusb-test-shim/index.js +12 -0
  107. package/libusb/tests/webusb-test-shim/package-lock.json +50 -0
  108. package/libusb/tests/webusb-test-shim/package.json +10 -0
  109. package/package.json +7 -7
  110. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  111. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  112. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  113. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  114. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  115. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  116. package/prebuilds/linux-ia32/node.napi.node +0 -0
  117. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  118. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  119. package/prebuilds/win32-arm64/node.napi.node +0 -0
  120. package/prebuilds/win32-ia32/node.napi.node +0 -0
  121. package/prebuilds/win32-x64/node.napi.node +0 -0
  122. package/src/device.cc +15 -5
  123. package/libusb/msvc/dpfp_2013.vcxproj +0 -87
  124. package/libusb/msvc/dpfp_2013.vcxproj.filters +0 -26
  125. package/libusb/msvc/dpfp_2015.vcxproj +0 -87
  126. package/libusb/msvc/dpfp_2015.vcxproj.filters +0 -26
  127. package/libusb/msvc/dpfp_2017.vcxproj +0 -106
  128. package/libusb/msvc/dpfp_2017.vcxproj.filters +0 -26
  129. package/libusb/msvc/dpfp_2019.vcxproj +0 -106
  130. package/libusb/msvc/dpfp_2019.vcxproj.filters +0 -26
  131. package/libusb/msvc/dpfp_threaded_2013.vcxproj +0 -87
  132. package/libusb/msvc/dpfp_threaded_2013.vcxproj.filters +0 -26
  133. package/libusb/msvc/dpfp_threaded_2015.vcxproj +0 -87
  134. package/libusb/msvc/dpfp_threaded_2015.vcxproj.filters +0 -26
  135. package/libusb/msvc/dpfp_threaded_2017.vcxproj +0 -106
  136. package/libusb/msvc/dpfp_threaded_2017.vcxproj.filters +0 -26
  137. package/libusb/msvc/dpfp_threaded_2019.vcxproj +0 -106
  138. package/libusb/msvc/dpfp_threaded_2019.vcxproj.filters +0 -26
  139. package/libusb/msvc/fxload_2013.vcxproj +0 -94
  140. package/libusb/msvc/fxload_2013.vcxproj.filters +0 -35
  141. package/libusb/msvc/fxload_2015.vcxproj +0 -94
  142. package/libusb/msvc/fxload_2015.vcxproj.filters +0 -35
  143. package/libusb/msvc/fxload_2017.vcxproj +0 -113
  144. package/libusb/msvc/fxload_2017.vcxproj.filters +0 -35
  145. package/libusb/msvc/fxload_2019.vcxproj +0 -113
  146. package/libusb/msvc/fxload_2019.vcxproj.filters +0 -35
  147. package/libusb/msvc/getopt_2013.vcxproj +0 -72
  148. package/libusb/msvc/getopt_2013.vcxproj.filters +0 -26
  149. package/libusb/msvc/getopt_2015.vcxproj +0 -73
  150. package/libusb/msvc/getopt_2015.vcxproj.filters +0 -26
  151. package/libusb/msvc/getopt_2017.vcxproj +0 -92
  152. package/libusb/msvc/getopt_2017.vcxproj.filters +0 -26
  153. package/libusb/msvc/getopt_2019.vcxproj +0 -92
  154. package/libusb/msvc/getopt_2019.vcxproj.filters +0 -26
  155. package/libusb/msvc/hotplugtest_2013.vcxproj +0 -86
  156. package/libusb/msvc/hotplugtest_2013.vcxproj.filters +0 -23
  157. package/libusb/msvc/hotplugtest_2015.vcxproj +0 -86
  158. package/libusb/msvc/hotplugtest_2015.vcxproj.filters +0 -23
  159. package/libusb/msvc/hotplugtest_2017.vcxproj +0 -105
  160. package/libusb/msvc/hotplugtest_2017.vcxproj.filters +0 -23
  161. package/libusb/msvc/hotplugtest_2019.vcxproj +0 -105
  162. package/libusb/msvc/hotplugtest_2019.vcxproj.filters +0 -23
  163. package/libusb/msvc/libusb_2013.sln +0 -137
  164. package/libusb/msvc/libusb_2015.sln +0 -137
  165. package/libusb/msvc/libusb_2017.sln +0 -240
  166. package/libusb/msvc/libusb_2019.sln +0 -240
  167. package/libusb/msvc/libusb_dll_2013.vcxproj +0 -104
  168. package/libusb/msvc/libusb_dll_2013.vcxproj.filters +0 -94
  169. package/libusb/msvc/libusb_dll_2015.vcxproj +0 -105
  170. package/libusb/msvc/libusb_dll_2015.vcxproj.filters +0 -94
  171. package/libusb/msvc/libusb_dll_2017.vcxproj +0 -124
  172. package/libusb/msvc/libusb_dll_2017.vcxproj.filters +0 -94
  173. package/libusb/msvc/libusb_dll_2019.vcxproj +0 -124
  174. package/libusb/msvc/libusb_dll_2019.vcxproj.filters +0 -94
  175. package/libusb/msvc/libusb_static_2013.vcxproj +0 -94
  176. package/libusb/msvc/libusb_static_2013.vcxproj.filters +0 -80
  177. package/libusb/msvc/libusb_static_2015.vcxproj +0 -95
  178. package/libusb/msvc/libusb_static_2015.vcxproj.filters +0 -80
  179. package/libusb/msvc/libusb_static_2017.vcxproj +0 -114
  180. package/libusb/msvc/libusb_static_2017.vcxproj.filters +0 -80
  181. package/libusb/msvc/libusb_static_2019.vcxproj +0 -114
  182. package/libusb/msvc/libusb_static_2019.vcxproj.filters +0 -80
  183. package/libusb/msvc/listdevs_2013.vcxproj +0 -86
  184. package/libusb/msvc/listdevs_2013.vcxproj.filters +0 -23
  185. package/libusb/msvc/listdevs_2015.vcxproj +0 -86
  186. package/libusb/msvc/listdevs_2015.vcxproj.filters +0 -23
  187. package/libusb/msvc/listdevs_2017.vcxproj +0 -105
  188. package/libusb/msvc/listdevs_2017.vcxproj.filters +0 -23
  189. package/libusb/msvc/listdevs_2019.vcxproj +0 -105
  190. package/libusb/msvc/listdevs_2019.vcxproj.filters +0 -23
  191. package/libusb/msvc/sam3u_benchmark_2013.vcxproj +0 -87
  192. package/libusb/msvc/sam3u_benchmark_2013.vcxproj.filters +0 -26
  193. package/libusb/msvc/sam3u_benchmark_2015.vcxproj +0 -87
  194. package/libusb/msvc/sam3u_benchmark_2015.vcxproj.filters +0 -26
  195. package/libusb/msvc/sam3u_benchmark_2017.vcxproj +0 -106
  196. package/libusb/msvc/sam3u_benchmark_2017.vcxproj.filters +0 -26
  197. package/libusb/msvc/sam3u_benchmark_2019.vcxproj +0 -106
  198. package/libusb/msvc/sam3u_benchmark_2019.vcxproj.filters +0 -26
  199. package/libusb/msvc/stress_2013.vcxproj +0 -89
  200. package/libusb/msvc/stress_2013.vcxproj.filters +0 -32
  201. package/libusb/msvc/stress_2015.vcxproj +0 -89
  202. package/libusb/msvc/stress_2015.vcxproj.filters +0 -32
  203. package/libusb/msvc/stress_2017.vcxproj +0 -108
  204. package/libusb/msvc/stress_2017.vcxproj.filters +0 -32
  205. package/libusb/msvc/stress_2019.vcxproj +0 -108
  206. package/libusb/msvc/stress_2019.vcxproj.filters +0 -32
  207. package/libusb/msvc/testlibusb_2013.vcxproj +0 -86
  208. package/libusb/msvc/testlibusb_2013.vcxproj.filters +0 -23
  209. package/libusb/msvc/testlibusb_2015.vcxproj +0 -86
  210. package/libusb/msvc/testlibusb_2015.vcxproj.filters +0 -23
  211. package/libusb/msvc/testlibusb_2017.vcxproj +0 -105
  212. package/libusb/msvc/testlibusb_2017.vcxproj.filters +0 -23
  213. package/libusb/msvc/testlibusb_2019.vcxproj +0 -105
  214. package/libusb/msvc/testlibusb_2019.vcxproj.filters +0 -23
  215. package/libusb/msvc/xusb_2013.vcxproj +0 -86
  216. package/libusb/msvc/xusb_2013.vcxproj.filters +0 -23
  217. package/libusb/msvc/xusb_2015.vcxproj +0 -86
  218. package/libusb/msvc/xusb_2015.vcxproj.filters +0 -23
  219. package/libusb/msvc/xusb_2017.vcxproj +0 -105
  220. package/libusb/msvc/xusb_2017.vcxproj.filters +0 -23
  221. package/libusb/msvc/xusb_2019.vcxproj +0 -105
  222. package/libusb/msvc/xusb_2019.vcxproj.filters +0 -23
@@ -1,7 +1,7 @@
1
1
  /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
2
2
  /*
3
3
  * Core functions for libusb
4
- * Copyright © 2012-2013 Nathan Hjelm <hjelmn@cs.unm.edu>
4
+ * Copyright © 2012-2023 Nathan Hjelm <hjelmn@cs.unm.edu>
5
5
  * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
6
6
  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
7
7
  *
@@ -34,7 +34,7 @@
34
34
 
35
35
  static const struct libusb_version libusb_version_internal =
36
36
  { LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
37
- LIBUSB_RC, "http://libusb.info" };
37
+ LIBUSB_RC, "https://libusb.info" };
38
38
  static struct timespec timestamp_origin;
39
39
  #if defined(ENABLE_LOGGING) && !defined(USE_SYSTEM_LOGGING_FACILITY)
40
40
  static libusb_log_cb log_handler;
@@ -43,6 +43,9 @@ static libusb_log_cb log_handler;
43
43
  struct libusb_context *usbi_default_context;
44
44
  struct libusb_context *usbi_fallback_context;
45
45
  static int default_context_refcnt;
46
+ #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
47
+ static usbi_atomic_t default_debug_level = -1;
48
+ #endif
46
49
  static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
47
50
  static struct usbi_option default_context_options[LIBUSB_OPTION_MAX];
48
51
 
@@ -57,12 +60,12 @@ struct list_head active_contexts_list;
57
60
  *
58
61
  * libusb is an open source library that allows you to communicate with USB
59
62
  * devices from user space. For more info, see the
60
- * <a href="http://libusb.info">libusb homepage</a>.
63
+ * <a href="https://libusb.info">libusb homepage</a>.
61
64
  *
62
65
  * This documentation is aimed at application developers wishing to
63
66
  * communicate with USB peripherals from their own software. After reviewing
64
67
  * this documentation, feedback and questions can be sent to the
65
- * <a href="http://mailing-list.libusb.info">libusb-devel mailing list</a>.
68
+ * <a href="https://mailing-list.libusb.info">libusb-devel mailing list</a>.
66
69
  *
67
70
  * This documentation assumes knowledge of how to operate USB devices from
68
71
  * a software standpoint (descriptors, configurations, interfaces, endpoints,
@@ -111,17 +114,18 @@ struct list_head active_contexts_list;
111
114
  * libusb uses stderr for all logging. By default, logging is set to NONE,
112
115
  * which means that no output will be produced. However, unless the library
113
116
  * has been compiled with logging disabled, then any application calls to
114
- * libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level), or the setting of the
115
- * environmental variable LIBUSB_DEBUG outside of the application, can result
116
- * in logging being produced. Your application should therefore not close
117
- * stderr, but instead direct it to the null device if its output is
118
- * undesirable.
119
- *
120
- * The libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) function can be
121
- * used to enable logging of certain messages. Under standard configuration,
122
- * libusb doesn't really log much so you are advised to use this function
123
- * to enable all error/warning/ informational messages. It will help debug
124
- * problems with your software.
117
+ * libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level),
118
+ * libusb_init_context, or the setting of the environmental variable
119
+ * LIBUSB_DEBUG outside of the application, can result in logging being
120
+ * produced. Your application should therefore not close stderr, but instead
121
+ * direct it to the null device if its output is undesirable.
122
+ *
123
+ * The libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) or
124
+ * libusb_init_context functions can be used to enable logging of certain
125
+ * messages. With the default configuration, libusb will not log much so if
126
+ * you are advised to use one of these functions to enable all
127
+ * error/warning/informational messages. It will help debug problems with your
128
+ * software.
125
129
  *
126
130
  * The logged messages are unstructured. There is no one-to-one correspondence
127
131
  * between messages being logged and success or failure return codes from
@@ -137,19 +141,19 @@ struct list_head active_contexts_list;
137
141
  * The LIBUSB_DEBUG environment variable can be used to enable message logging
138
142
  * at run-time. This environment variable should be set to a log level number,
139
143
  * which is interpreted the same as the
140
- * libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) parameter. When this
141
- * environment variable is set, the message logging verbosity level is fixed
142
- * and libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) effectively does
143
- * nothing.
144
+ * libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level), or
145
+ * libusb_init_context(&ctx, &(struct libusb_init_option){.option = LIBUSB_OPTION_LOG_LEVEL, .value = {.ival = level}}, 0).
146
+ * When the environment variable is set, the message logging verbosity level is
147
+ * fixed and setting the LIBUSB_OPTION_LOG_LEVEL option has no effect.
144
148
  *
145
149
  * libusb can be compiled without any logging functions, useful for embedded
146
- * systems. In this case, libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level)
147
- * and the LIBUSB_DEBUG environment variable have no effects.
150
+ * systems. In this case, neither the LIBUSB_OPTION_LOG_LEVEL option, nor the
151
+ * LIBUSB_DEBUG environment variable will have any effect.
148
152
  *
149
153
  * libusb can also be compiled with verbose debugging messages always. When
150
154
  * the library is compiled in this way, all messages of all verbosities are
151
- * always logged. libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) and
152
- * the LIBUSB_DEBUG environment variable have no effects.
155
+ * always logged. Again, in this case, neither the LIBUSB_OPTION_LOG_LEVEL
156
+ * option, nor the LIBUSB_DEBUG environment variable will have any effect.
153
157
  *
154
158
  * \section remarks Other remarks
155
159
  *
@@ -327,23 +331,23 @@ if (cfg != desired)
327
331
  * developed modules may both use libusb.
328
332
  *
329
333
  * libusb is written to allow for these multiple user scenarios. The two
330
- * "instances" of libusb will not interfere: libusb_set_option() calls
331
- * from one user will not affect the same settings for other users, other
332
- * users can continue using libusb after one of them calls libusb_exit(), etc.
334
+ * "instances" of libusb will not interfere: an option set by one user will have
335
+ * no effect the same option for other users, other users can continue using
336
+ * libusb after one of them calls libusb_exit(), etc.
333
337
  *
334
338
  * This is made possible through libusb's <em>context</em> concept. When you
335
- * call libusb_init(), you are (optionally) given a context. You can then pass
339
+ * call libusb_init_context(), you are (optionally) given a context. You can then pass
336
340
  * this context pointer back into future libusb functions.
337
341
  *
338
342
  * In order to keep things simple for more simplistic applications, it is
339
343
  * legal to pass NULL to all functions requiring a context pointer (as long as
340
344
  * you're sure no other code will attempt to use libusb from the same process).
341
345
  * When you pass NULL, the default context will be used. The default context
342
- * is created the first time a process calls libusb_init() when no other
346
+ * is created the first time a process calls libusb_init_context() when no other
343
347
  * context is alive. Contexts are destroyed during libusb_exit().
344
348
  *
345
349
  * The default context is reference-counted and can be shared. That means that
346
- * if libusb_init(NULL) is called twice within the same process, the two
350
+ * if libusb_init_context(NULL, x, y) is called twice within the same process, the two
347
351
  * users end up sharing the same context. The deinitialization and freeing of
348
352
  * the default context will only happen when the last user calls libusb_exit().
349
353
  * In other words, the default context is created and initialized when its
@@ -413,6 +417,7 @@ if (cfg != desired)
413
417
  * - libusb_get_device_speed()
414
418
  * - libusb_get_iso_packet_buffer()
415
419
  * - libusb_get_iso_packet_buffer_simple()
420
+ * - libusb_get_max_alt_packet_size()
416
421
  * - libusb_get_max_iso_packet_size()
417
422
  * - libusb_get_max_packet_size()
418
423
  * - libusb_get_next_timeout()
@@ -436,6 +441,7 @@ if (cfg != desired)
436
441
  * - libusb_hotplug_deregister_callback()
437
442
  * - libusb_hotplug_register_callback()
438
443
  * - libusb_init()
444
+ * - libusb_init_context()
439
445
  * - libusb_interrupt_event_handler()
440
446
  * - libusb_interrupt_transfer()
441
447
  * - libusb_kernel_driver_active()
@@ -931,13 +937,13 @@ uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
931
937
  /** \ingroup libusb_dev
932
938
  * Get the list of all port numbers from root for the specified device
933
939
  *
934
- * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
940
+ * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
935
941
  * \param dev a device
936
942
  * \param port_numbers the array that should contain the port numbers
937
943
  * \param port_numbers_len the maximum length of the array. As per the USB 3.0
938
944
  * specs, the current maximum limit for the depth is 7.
939
945
  * \returns the number of elements filled
940
- * \returns LIBUSB_ERROR_OVERFLOW if the array is too small
946
+ * \returns \ref LIBUSB_ERROR_OVERFLOW if the array is too small
941
947
  */
942
948
  int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
943
949
  uint8_t *port_numbers, int port_numbers_len)
@@ -1049,8 +1055,8 @@ static const struct libusb_endpoint_descriptor *find_endpoint(
1049
1055
  * \param dev a device
1050
1056
  * \param endpoint address of the endpoint in question
1051
1057
  * \returns the wMaxPacketSize value
1052
- * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1053
- * \returns LIBUSB_ERROR_OTHER on other failure
1058
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1059
+ * \returns \ref LIBUSB_ERROR_OTHER on other failure
1054
1060
  */
1055
1061
  int API_EXPORTED libusb_get_max_packet_size(libusb_device *dev,
1056
1062
  unsigned char endpoint)
@@ -1079,6 +1085,65 @@ out:
1079
1085
  return r;
1080
1086
  }
1081
1087
 
1088
+ static const struct libusb_endpoint_descriptor *find_alt_endpoint(
1089
+ struct libusb_config_descriptor *config,
1090
+ int iface_idx, int altsetting_idx, unsigned char endpoint)
1091
+ {
1092
+ if (iface_idx >= config->bNumInterfaces) {
1093
+ return NULL;
1094
+ }
1095
+
1096
+ const struct libusb_interface *iface = &config->interface[iface_idx];
1097
+
1098
+ if (altsetting_idx >= iface->num_altsetting) {
1099
+ return NULL;
1100
+ }
1101
+
1102
+ const struct libusb_interface_descriptor *altsetting
1103
+ = &iface->altsetting[altsetting_idx];
1104
+ int ep_idx;
1105
+
1106
+ for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) {
1107
+ const struct libusb_endpoint_descriptor *ep =
1108
+ &altsetting->endpoint[ep_idx];
1109
+ if (ep->bEndpointAddress == endpoint)
1110
+ return ep;
1111
+ }
1112
+ return NULL;
1113
+ }
1114
+
1115
+ static int get_endpoint_max_packet_size(libusb_device *dev,
1116
+ const struct libusb_endpoint_descriptor *ep)
1117
+ {
1118
+ struct libusb_ss_endpoint_companion_descriptor *ss_ep_cmp;
1119
+ enum libusb_endpoint_transfer_type ep_type;
1120
+ uint16_t val;
1121
+ int r = 0;
1122
+ int speed;
1123
+
1124
+ speed = libusb_get_device_speed(dev);
1125
+ if (speed >= LIBUSB_SPEED_SUPER) {
1126
+ r = libusb_get_ss_endpoint_companion_descriptor(dev->ctx, ep, &ss_ep_cmp);
1127
+ if (r == LIBUSB_SUCCESS) {
1128
+ r = ss_ep_cmp->wBytesPerInterval;
1129
+ libusb_free_ss_endpoint_companion_descriptor(ss_ep_cmp);
1130
+ }
1131
+ }
1132
+
1133
+ /* If the device isn't a SuperSpeed device or retrieving the SS endpoint didn't worked. */
1134
+ if (speed < LIBUSB_SPEED_SUPER || r < 0) {
1135
+ val = ep->wMaxPacketSize;
1136
+ ep_type = (enum libusb_endpoint_transfer_type) (ep->bmAttributes & 0x3);
1137
+
1138
+ r = val & 0x07ff;
1139
+ if (ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_ISOCHRONOUS
1140
+ || ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_INTERRUPT)
1141
+ r *= (1 + ((val >> 11) & 3));
1142
+ }
1143
+
1144
+ return r;
1145
+ }
1146
+
1082
1147
  /** \ingroup libusb_dev
1083
1148
  * Calculate the maximum packet size which a specific endpoint is capable is
1084
1149
  * sending or receiving in the duration of 1 microframe
@@ -1099,24 +1164,25 @@ out:
1099
1164
  * libusb_set_iso_packet_lengths() in order to set the length field of every
1100
1165
  * isochronous packet in a transfer.
1101
1166
  *
1167
+ * This function only considers the first alternate setting of the interface.
1168
+ * If the endpoint has different maximum packet sizes for different alternate
1169
+ * settings, you probably want libusb_get_max_alt_packet_size() instead.
1170
+ *
1102
1171
  * Since v1.0.3.
1103
1172
  *
1104
1173
  * \param dev a device
1105
1174
  * \param endpoint address of the endpoint in question
1106
1175
  * \returns the maximum packet size which can be sent/received on this endpoint
1107
- * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1108
- * \returns LIBUSB_ERROR_OTHER on other failure
1176
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1177
+ * \returns \ref LIBUSB_ERROR_OTHER on other failure
1178
+ * \see libusb_get_max_alt_packet_size
1109
1179
  */
1110
1180
  int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
1111
1181
  unsigned char endpoint)
1112
1182
  {
1113
1183
  struct libusb_config_descriptor *config;
1114
1184
  const struct libusb_endpoint_descriptor *ep;
1115
- struct libusb_ss_endpoint_companion_descriptor *ss_ep_cmp;
1116
- enum libusb_endpoint_transfer_type ep_type;
1117
- uint16_t val;
1118
1185
  int r;
1119
- int speed;
1120
1186
 
1121
1187
  r = libusb_get_active_config_descriptor(dev, &config);
1122
1188
  if (r < 0) {
@@ -1131,26 +1197,68 @@ int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
1131
1197
  goto out;
1132
1198
  }
1133
1199
 
1134
- speed = libusb_get_device_speed(dev);
1135
- if (speed >= LIBUSB_SPEED_SUPER) {
1136
- r = libusb_get_ss_endpoint_companion_descriptor(dev->ctx, ep, &ss_ep_cmp);
1137
- if (r == LIBUSB_SUCCESS) {
1138
- r = ss_ep_cmp->wBytesPerInterval;
1139
- libusb_free_ss_endpoint_companion_descriptor(ss_ep_cmp);
1140
- }
1141
- }
1200
+ r = get_endpoint_max_packet_size(dev, ep);
1142
1201
 
1143
- /* If the device isn't a SuperSpeed device or retrieving the SS endpoint didn't worked. */
1144
- if (speed < LIBUSB_SPEED_SUPER || r < 0) {
1145
- val = ep->wMaxPacketSize;
1146
- ep_type = (enum libusb_endpoint_transfer_type) (ep->bmAttributes & 0x3);
1202
+ out:
1203
+ libusb_free_config_descriptor(config);
1204
+ return r;
1205
+ }
1147
1206
 
1148
- r = val & 0x07ff;
1149
- if (ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_ISOCHRONOUS
1150
- || ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_INTERRUPT)
1151
- r *= (1 + ((val >> 11) & 3));
1207
+ /** \ingroup libusb_dev
1208
+ * Calculate the maximum packet size which a specific endpoint is capable of
1209
+ * sending or receiving in the duration of 1 microframe
1210
+ *
1211
+ * Only the active configuration is examined. The calculation is based on the
1212
+ * wMaxPacketSize field in the endpoint descriptor as described in section
1213
+ * 9.6.6 in the USB 2.0 specifications.
1214
+ *
1215
+ * If acting on an isochronous or interrupt endpoint, this function will
1216
+ * multiply the value found in bits 0:10 by the number of transactions per
1217
+ * microframe (determined by bits 11:12). Otherwise, this function just
1218
+ * returns the numeric value found in bits 0:10. For USB 3.0 device, it
1219
+ * will attempts to retrieve the Endpoint Companion Descriptor to return
1220
+ * wBytesPerInterval.
1221
+ *
1222
+ * This function is useful for setting up isochronous transfers, for example
1223
+ * you might pass the return value from this function to
1224
+ * libusb_set_iso_packet_lengths() in order to set the length field of every
1225
+ * isochronous packet in a transfer.
1226
+ *
1227
+ * Since version 1.0.27, \ref LIBUSB_API_VERSION >= 0x0100010A
1228
+ *
1229
+ * \param dev a device
1230
+ * \param interface_number the <tt>bInterfaceNumber</tt> of the interface
1231
+ * the endpoint belongs to
1232
+ * \param alternate_setting the <tt>bAlternateSetting</tt> of the interface
1233
+ * \param endpoint address of the endpoint in question
1234
+ * \returns the maximum packet size which can be sent/received on this endpoint
1235
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1236
+ * \returns \ref LIBUSB_ERROR_OTHER on other failure
1237
+ * \see libusb_get_max_iso_packet_size
1238
+ */
1239
+ int API_EXPORTED libusb_get_max_alt_packet_size(libusb_device *dev,
1240
+ int interface_number, int alternate_setting, unsigned char endpoint)
1241
+ {
1242
+ struct libusb_config_descriptor *config;
1243
+ const struct libusb_endpoint_descriptor *ep;
1244
+ int r;
1245
+
1246
+ r = libusb_get_active_config_descriptor(dev, &config);
1247
+ if (r < 0) {
1248
+ usbi_err(DEVICE_CTX(dev),
1249
+ "could not retrieve active config descriptor");
1250
+ return LIBUSB_ERROR_OTHER;
1251
+ }
1252
+
1253
+ ep = find_alt_endpoint(config, interface_number,
1254
+ alternate_setting, endpoint);
1255
+ if (!ep) {
1256
+ r = LIBUSB_ERROR_NOT_FOUND;
1257
+ goto out;
1152
1258
  }
1153
1259
 
1260
+ r = get_endpoint_max_packet_size(dev, ep);
1261
+
1154
1262
  out:
1155
1263
  libusb_free_config_descriptor(config);
1156
1264
  return r;
@@ -1209,10 +1317,10 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev)
1209
1317
  * handle for the underlying device. The handle allows you to use libusb to
1210
1318
  * perform I/O on the device in question.
1211
1319
  *
1212
- * Call libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY) before
1213
- * libusb_init() if you want to skip enumeration of USB devices. In particular,
1214
- * this might be needed on Android if you don't have authority to access USB
1215
- * devices in general.
1320
+ * Call libusb_init_context with the LIBUSB_OPTION_NO_DEVICE_DISCOVERY
1321
+ * option if you want to skip enumeration of USB devices. In particular, this
1322
+ * might be needed on Android if you don't have authority to access USB
1323
+ * devices in general. Setting this option with libusb_set_option is deprecated.
1216
1324
  *
1217
1325
  * On Linux, the system device handle must be a valid file descriptor opened
1218
1326
  * on the device node.
@@ -1233,9 +1341,9 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev)
1233
1341
  * \param dev_handle output location for the returned device handle pointer. Only
1234
1342
  * populated when the return code is 0.
1235
1343
  * \returns 0 on success
1236
- * \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
1237
- * \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1238
- * \returns LIBUSB_ERROR_NOT_SUPPORTED if the operation is not supported on this
1344
+ * \returns \ref LIBUSB_ERROR_NO_MEM on memory allocation failure
1345
+ * \returns \ref LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1346
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if the operation is not supported on this
1239
1347
  * platform
1240
1348
  * \returns another LIBUSB_ERROR code on other failure
1241
1349
  */
@@ -1289,9 +1397,9 @@ int API_EXPORTED libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev,
1289
1397
  * \param dev_handle output location for the returned device handle pointer. Only
1290
1398
  * populated when the return code is 0.
1291
1399
  * \returns 0 on success
1292
- * \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
1293
- * \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1294
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1400
+ * \returns \ref LIBUSB_ERROR_NO_MEM on memory allocation failure
1401
+ * \returns \ref LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1402
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1295
1403
  * \returns another LIBUSB_ERROR code on other failure
1296
1404
  */
1297
1405
  int API_EXPORTED libusb_open(libusb_device *dev,
@@ -1397,20 +1505,22 @@ static void do_close(struct libusb_context *ctx,
1397
1505
  for_each_transfer_safe(ctx, itransfer, tmp) {
1398
1506
  struct libusb_transfer *transfer =
1399
1507
  USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1508
+ uint32_t state_flags;
1400
1509
 
1401
1510
  if (transfer->dev_handle != dev_handle)
1402
1511
  continue;
1403
1512
 
1404
1513
  usbi_mutex_lock(&itransfer->lock);
1405
- if (!(itransfer->state_flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) {
1514
+ state_flags = itransfer->state_flags;
1515
+ usbi_mutex_unlock(&itransfer->lock);
1516
+ if (!(state_flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) {
1406
1517
  usbi_err(ctx, "Device handle closed while transfer was still being processed, but the device is still connected as far as we know");
1407
1518
 
1408
- if (itransfer->state_flags & USBI_TRANSFER_CANCELLING)
1519
+ if (state_flags & USBI_TRANSFER_CANCELLING)
1409
1520
  usbi_warn(ctx, "A cancellation for an in-flight transfer hasn't completed but closing the device handle");
1410
1521
  else
1411
1522
  usbi_err(ctx, "A cancellation hasn't even been scheduled on the transfer for which the device is closing");
1412
1523
  }
1413
- usbi_mutex_unlock(&itransfer->lock);
1414
1524
 
1415
1525
  /* remove from the list of in-flight transfers and make sure
1416
1526
  * we don't accidentally use the device handle in the future
@@ -1424,7 +1534,7 @@ static void do_close(struct libusb_context *ctx,
1424
1534
  * the device handle is invalid
1425
1535
  */
1426
1536
  usbi_dbg(ctx, "Removed transfer %p from the in-flight list because device handle %p closed",
1427
- transfer, dev_handle);
1537
+ (void *) transfer, (void *) dev_handle);
1428
1538
  }
1429
1539
  usbi_mutex_unlock(&ctx->flying_transfers_lock);
1430
1540
 
@@ -1533,7 +1643,7 @@ libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle)
1533
1643
  * \param config output location for the bConfigurationValue of the active
1534
1644
  * configuration (only valid for return code 0)
1535
1645
  * \returns 0 on success
1536
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1646
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1537
1647
  * \returns another LIBUSB_ERROR code on other failure
1538
1648
  */
1539
1649
  int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev_handle,
@@ -1584,7 +1694,7 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev_handle,
1584
1694
  * endpoint halts cleared, toggles reset).
1585
1695
  *
1586
1696
  * Not all backends support setting the configuration from user space, which
1587
- * will be indicated by the return code LIBUSB_ERROR_NOT_SUPPORTED. As this
1697
+ * will be indicated by the return code \ref LIBUSB_ERROR_NOT_SUPPORTED. As this
1588
1698
  * suggests that the platform is handling the device configuration itself,
1589
1699
  * this error should generally be safe to ignore.
1590
1700
  *
@@ -1615,11 +1725,11 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev_handle,
1615
1725
  * wish to activate, or -1 if you wish to put the device in an unconfigured
1616
1726
  * state
1617
1727
  * \returns 0 on success
1618
- * \returns LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist
1619
- * \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed
1620
- * \returns LIBUSB_ERROR_NOT_SUPPORTED if setting or changing the configuration
1728
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist
1729
+ * \returns \ref LIBUSB_ERROR_BUSY if interfaces are currently claimed
1730
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if setting or changing the configuration
1621
1731
  * is not supported by the backend
1622
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1732
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1623
1733
  * \returns another LIBUSB_ERROR code on other failure
1624
1734
  * \see libusb_set_auto_detach_kernel_driver()
1625
1735
  */
@@ -1653,10 +1763,10 @@ int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev_handle,
1653
1763
  * \param interface_number the <tt>bInterfaceNumber</tt> of the interface you
1654
1764
  * wish to claim
1655
1765
  * \returns 0 on success
1656
- * \returns LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist
1657
- * \returns LIBUSB_ERROR_BUSY if another program or driver has claimed the
1766
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist
1767
+ * \returns \ref LIBUSB_ERROR_BUSY if another program or driver has claimed the
1658
1768
  * interface
1659
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1769
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1660
1770
  * \returns a LIBUSB_ERROR code on other failure
1661
1771
  * \see libusb_set_auto_detach_kernel_driver()
1662
1772
  */
@@ -1699,8 +1809,8 @@ out:
1699
1809
  * \param interface_number the <tt>bInterfaceNumber</tt> of the
1700
1810
  * previously-claimed interface
1701
1811
  * \returns 0 on success
1702
- * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed
1703
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1812
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the interface was not claimed
1813
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1704
1814
  * \returns another LIBUSB_ERROR code on other failure
1705
1815
  * \see libusb_set_auto_detach_kernel_driver()
1706
1816
  */
@@ -1744,9 +1854,9 @@ out:
1744
1854
  * \param alternate_setting the <tt>bAlternateSetting</tt> of the alternate
1745
1855
  * setting to activate
1746
1856
  * \returns 0 on success
1747
- * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the
1857
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the
1748
1858
  * requested alternate setting does not exist
1749
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1859
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1750
1860
  * \returns another LIBUSB_ERROR code on other failure
1751
1861
  */
1752
1862
  int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_handle,
@@ -1760,7 +1870,6 @@ int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_hand
1760
1870
  return LIBUSB_ERROR_INVALID_PARAM;
1761
1871
 
1762
1872
  if (!usbi_atomic_load(&dev_handle->dev->attached)) {
1763
- usbi_mutex_unlock(&dev_handle->lock);
1764
1873
  return LIBUSB_ERROR_NO_DEVICE;
1765
1874
  }
1766
1875
 
@@ -1787,8 +1896,8 @@ int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_hand
1787
1896
  * \param dev_handle a device handle
1788
1897
  * \param endpoint the endpoint to clear halt status
1789
1898
  * \returns 0 on success
1790
- * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1791
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1899
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1900
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1792
1901
  * \returns another LIBUSB_ERROR code on other failure
1793
1902
  */
1794
1903
  int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev_handle,
@@ -1809,14 +1918,14 @@ int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev_handle,
1809
1918
  * If the reset fails, the descriptors change, or the previous state cannot be
1810
1919
  * restored, the device will appear to be disconnected and reconnected. This
1811
1920
  * means that the device handle is no longer valid (you should close it) and
1812
- * rediscover the device. A return code of LIBUSB_ERROR_NOT_FOUND indicates
1921
+ * rediscover the device. A return code of \ref LIBUSB_ERROR_NOT_FOUND indicates
1813
1922
  * when this is the case.
1814
1923
  *
1815
1924
  * This is a blocking function which usually incurs a noticeable delay.
1816
1925
  *
1817
1926
  * \param dev_handle a handle of the device to reset
1818
1927
  * \returns 0 on success
1819
- * \returns LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the
1928
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the
1820
1929
  * device has been disconnected
1821
1930
  * \returns another LIBUSB_ERROR code on other failure
1822
1931
  */
@@ -1881,7 +1990,7 @@ int API_EXPORTED libusb_alloc_streams(libusb_device_handle *dev_handle,
1881
1990
  * \param dev_handle a device handle
1882
1991
  * \param endpoints array of endpoints to free streams on
1883
1992
  * \param num_endpoints length of the endpoints array
1884
- * \returns LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
1993
+ * \returns \ref LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
1885
1994
  */
1886
1995
  int API_EXPORTED libusb_free_streams(libusb_device_handle *dev_handle,
1887
1996
  unsigned char *endpoints, int num_endpoints)
@@ -1944,7 +2053,7 @@ unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handl
1944
2053
  * \param dev_handle a device handle
1945
2054
  * \param buffer pointer to the previously allocated memory
1946
2055
  * \param length size of previously allocated memory
1947
- * \returns LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
2056
+ * \returns \ref LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
1948
2057
  */
1949
2058
  int API_EXPORTED libusb_dev_mem_free(libusb_device_handle *dev_handle,
1950
2059
  unsigned char *buffer, size_t length)
@@ -1966,8 +2075,8 @@ int API_EXPORTED libusb_dev_mem_free(libusb_device_handle *dev_handle,
1966
2075
  * \param interface_number the interface to check
1967
2076
  * \returns 0 if no kernel driver is active
1968
2077
  * \returns 1 if a kernel driver is active
1969
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1970
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2078
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
2079
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
1971
2080
  * is not available
1972
2081
  * \returns another LIBUSB_ERROR code on other failure
1973
2082
  * \see libusb_detach_kernel_driver()
@@ -1997,15 +2106,15 @@ int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev_handle,
1997
2106
  *
1998
2107
  * Note that libusb itself also talks to the device through a special kernel
1999
2108
  * driver, if this driver is already attached to the device, this call will
2000
- * not detach it and return LIBUSB_ERROR_NOT_FOUND.
2109
+ * not detach it and return \ref LIBUSB_ERROR_NOT_FOUND.
2001
2110
  *
2002
2111
  * \param dev_handle a device handle
2003
2112
  * \param interface_number the interface to detach the driver from
2004
2113
  * \returns 0 on success
2005
- * \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
2006
- * \returns LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
2007
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
2008
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2114
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
2115
+ * \returns \ref LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
2116
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
2117
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2009
2118
  * is not available
2010
2119
  * \returns another LIBUSB_ERROR code on other failure
2011
2120
  * \see libusb_kernel_driver_active()
@@ -2036,12 +2145,12 @@ int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev_handle,
2036
2145
  * \param dev_handle a device handle
2037
2146
  * \param interface_number the interface to attach the driver from
2038
2147
  * \returns 0 on success
2039
- * \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
2040
- * \returns LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
2041
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
2042
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2148
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
2149
+ * \returns \ref LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
2150
+ * \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
2151
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2043
2152
  * is not available
2044
- * \returns LIBUSB_ERROR_BUSY if the driver cannot be attached because the
2153
+ * \returns \ref LIBUSB_ERROR_BUSY if the driver cannot be attached because the
2045
2154
  * interface is claimed by a program or driver
2046
2155
  * \returns another LIBUSB_ERROR code on other failure
2047
2156
  * \see libusb_kernel_driver_active()
@@ -2072,14 +2181,14 @@ int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev_handle,
2072
2181
  * handles by default.
2073
2182
  *
2074
2183
  * On platforms which do not have LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER
2075
- * this function will return LIBUSB_ERROR_NOT_SUPPORTED, and libusb will
2184
+ * this function will return \ref LIBUSB_ERROR_NOT_SUPPORTED, and libusb will
2076
2185
  * continue as if this function was never called.
2077
2186
  *
2078
2187
  * \param dev_handle a device handle
2079
2188
  * \param enable whether to enable or disable auto kernel driver detachment
2080
2189
  *
2081
- * \returns LIBUSB_SUCCESS on success
2082
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2190
+ * \returns \ref LIBUSB_SUCCESS on success
2191
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
2083
2192
  * is not available
2084
2193
  * \see libusb_claim_interface()
2085
2194
  * \see libusb_release_interface()
@@ -2096,20 +2205,34 @@ int API_EXPORTED libusb_set_auto_detach_kernel_driver(
2096
2205
  }
2097
2206
 
2098
2207
  /** \ingroup libusb_lib
2099
- * \deprecated Use libusb_set_option() instead using the
2100
- * \ref LIBUSB_OPTION_LOG_LEVEL option.
2208
+ * Deprecated. Use libusb_set_option() or libusb_init_context() instead,
2209
+ * with the \ref LIBUSB_OPTION_LOG_LEVEL option.
2101
2210
  */
2102
2211
  void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
2103
2212
  {
2104
- #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
2105
- ctx = usbi_get_context(ctx);
2106
- if (!ctx->debug_fixed) {
2107
- level = CLAMP(level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG);
2108
- ctx->debug = (enum libusb_log_level)level;
2213
+ libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level);
2214
+ }
2215
+
2216
+ static void libusb_set_log_cb_internal(libusb_context *ctx, libusb_log_cb cb,
2217
+ int mode)
2218
+ {
2219
+ #if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY))
2220
+ #if !defined(USE_SYSTEM_LOGGING_FACILITY)
2221
+ if (mode & LIBUSB_LOG_CB_GLOBAL)
2222
+ log_handler = cb;
2223
+ #endif
2224
+ #if !defined(ENABLE_DEBUG_LOGGING)
2225
+ if (mode & LIBUSB_LOG_CB_CONTEXT) {
2226
+ ctx = usbi_get_context(ctx);
2227
+ ctx->log_handler = cb;
2109
2228
  }
2110
2229
  #else
2111
2230
  UNUSED(ctx);
2112
- UNUSED(level);
2231
+ #endif
2232
+ #else
2233
+ UNUSED(ctx);
2234
+ UNUSED(cb);
2235
+ UNUSED(mode);
2113
2236
  #endif
2114
2237
  }
2115
2238
 
@@ -2139,24 +2262,7 @@ void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
2139
2262
  void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb,
2140
2263
  int mode)
2141
2264
  {
2142
- #if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY))
2143
- #if !defined(USE_SYSTEM_LOGGING_FACILITY)
2144
- if (mode & LIBUSB_LOG_CB_GLOBAL)
2145
- log_handler = cb;
2146
- #endif
2147
- #if !defined(ENABLE_DEBUG_LOGGING)
2148
- if (mode & LIBUSB_LOG_CB_CONTEXT) {
2149
- ctx = usbi_get_context(ctx);
2150
- ctx->log_handler = cb;
2151
- }
2152
- #else
2153
- UNUSED(ctx);
2154
- #endif
2155
- #else
2156
- UNUSED(ctx);
2157
- UNUSED(cb);
2158
- UNUSED(mode);
2159
- #endif
2265
+ libusb_set_log_cb_internal(ctx, cb, mode);
2160
2266
  }
2161
2267
 
2162
2268
  /** \ingroup libusb_lib
@@ -2176,72 +2282,95 @@ void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb,
2176
2282
  * \param option which option to set
2177
2283
  * \param ... any required arguments for the specified option
2178
2284
  *
2179
- * \returns LIBUSB_SUCCESS on success
2180
- * \returns LIBUSB_ERROR_INVALID_PARAM if the option or arguments are invalid
2181
- * \returns LIBUSB_ERROR_NOT_SUPPORTED if the option is valid but not supported
2285
+ * \returns \ref LIBUSB_SUCCESS on success
2286
+ * \returns \ref LIBUSB_ERROR_INVALID_PARAM if the option or arguments are invalid
2287
+ * \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if the option is valid but not supported
2182
2288
  * on this platform
2183
- * \returns LIBUSB_ERROR_NOT_FOUND if LIBUSB_OPTION_USE_USBDK is valid on this platform but UsbDk is not available
2289
+ * \returns \ref LIBUSB_ERROR_NOT_FOUND if LIBUSB_OPTION_USE_USBDK is valid on this platform but UsbDk is not available
2184
2290
  */
2185
- int API_EXPORTED libusb_set_option(libusb_context *ctx,
2291
+ int API_EXPORTEDV libusb_set_option(libusb_context *ctx,
2186
2292
  enum libusb_option option, ...)
2187
2293
  {
2188
2294
  int arg = 0, r = LIBUSB_SUCCESS;
2295
+ libusb_log_cb log_cb = NULL;
2189
2296
  va_list ap;
2297
+ #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
2298
+ int is_default_context = (NULL == ctx);
2299
+ #endif
2190
2300
 
2191
2301
  va_start(ap, option);
2302
+
2192
2303
  if (LIBUSB_OPTION_LOG_LEVEL == option) {
2193
2304
  arg = va_arg(ap, int);
2194
2305
  if (arg < LIBUSB_LOG_LEVEL_NONE || arg > LIBUSB_LOG_LEVEL_DEBUG) {
2195
2306
  r = LIBUSB_ERROR_INVALID_PARAM;
2196
2307
  }
2197
2308
  }
2198
- va_end(ap);
2199
-
2200
- if (LIBUSB_SUCCESS != r) {
2201
- return r;
2309
+ if (LIBUSB_OPTION_LOG_CB == option) {
2310
+ log_cb = (libusb_log_cb) va_arg(ap, libusb_log_cb);
2202
2311
  }
2203
2312
 
2204
- if (option >= LIBUSB_OPTION_MAX) {
2205
- return LIBUSB_ERROR_INVALID_PARAM;
2206
- }
2313
+ do {
2314
+ if (LIBUSB_SUCCESS != r) {
2315
+ break;
2316
+ }
2207
2317
 
2208
- if (NULL == ctx) {
2209
- usbi_mutex_static_lock(&default_context_lock);
2210
- default_context_options[option].is_set = 1;
2211
- if (LIBUSB_OPTION_LOG_LEVEL == option) {
2212
- default_context_options[option].arg.ival = arg;
2318
+ if (option >= LIBUSB_OPTION_MAX) {
2319
+ r = LIBUSB_ERROR_INVALID_PARAM;
2320
+ break;
2213
2321
  }
2214
- usbi_mutex_static_unlock(&default_context_lock);
2215
- }
2216
2322
 
2217
- ctx = usbi_get_context(ctx);
2218
- if (NULL == ctx) {
2219
- return LIBUSB_SUCCESS;
2220
- }
2323
+ if (NULL == ctx) {
2324
+ usbi_mutex_static_lock(&default_context_lock);
2325
+ default_context_options[option].is_set = 1;
2326
+ if (LIBUSB_OPTION_LOG_LEVEL == option) {
2327
+ default_context_options[option].arg.ival = arg;
2328
+ } else if (LIBUSB_OPTION_LOG_CB == option) {
2329
+ default_context_options[option].arg.log_cbval = log_cb;
2330
+ libusb_set_log_cb_internal(NULL, log_cb, LIBUSB_LOG_CB_GLOBAL);
2331
+ }
2332
+ usbi_mutex_static_unlock(&default_context_lock);
2333
+ }
2221
2334
 
2222
- switch (option) {
2223
- case LIBUSB_OPTION_LOG_LEVEL:
2335
+ ctx = usbi_get_context(ctx);
2336
+ if (NULL == ctx)
2337
+ break;
2338
+
2339
+ switch (option) {
2340
+ case LIBUSB_OPTION_LOG_LEVEL:
2224
2341
  #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
2225
- if (!ctx->debug_fixed)
2226
- ctx->debug = (enum libusb_log_level)arg;
2342
+ if (!ctx->debug_fixed) {
2343
+ ctx->debug = (enum libusb_log_level)arg;
2344
+ if (is_default_context)
2345
+ usbi_atomic_store(&default_debug_level, CLAMP(arg, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG));
2346
+ }
2227
2347
  #endif
2228
- break;
2348
+ break;
2229
2349
 
2230
- /* Handle all backend-specific options here */
2231
- case LIBUSB_OPTION_USE_USBDK:
2232
- case LIBUSB_OPTION_NO_DEVICE_DISCOVERY:
2233
- if (usbi_backend.set_option)
2234
- return usbi_backend.set_option(ctx, option, ap);
2350
+ /* Handle all backend-specific options here */
2351
+ case LIBUSB_OPTION_USE_USBDK:
2352
+ case LIBUSB_OPTION_NO_DEVICE_DISCOVERY:
2353
+ if (usbi_backend.set_option) {
2354
+ r = usbi_backend.set_option(ctx, option, ap);
2355
+ break;
2356
+ }
2235
2357
 
2236
- return LIBUSB_ERROR_NOT_SUPPORTED;
2237
- break;
2358
+ r = LIBUSB_ERROR_NOT_SUPPORTED;
2359
+ break;
2238
2360
 
2239
- case LIBUSB_OPTION_MAX:
2240
- default:
2241
- return LIBUSB_ERROR_INVALID_PARAM;
2242
- }
2361
+ case LIBUSB_OPTION_LOG_CB:
2362
+ libusb_set_log_cb_internal(ctx, log_cb, LIBUSB_LOG_CB_CONTEXT);
2363
+ break;
2243
2364
 
2244
- return LIBUSB_SUCCESS;;
2365
+ case LIBUSB_OPTION_MAX: /* unreachable */
2366
+ default:
2367
+ r = LIBUSB_ERROR_INVALID_PARAM;
2368
+ }
2369
+ } while (0);
2370
+
2371
+ va_end(ap);
2372
+
2373
+ return r;
2245
2374
  }
2246
2375
 
2247
2376
  #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
@@ -2264,20 +2393,35 @@ static enum libusb_log_level get_env_debug_level(void)
2264
2393
  }
2265
2394
  #endif
2266
2395
 
2396
+ /** \ingroup libusb_lib
2397
+ * Deprecated initialization function. Equivalent to calling libusb_init_context with no options.
2398
+ *
2399
+ * \see libusb_init_context
2400
+ */
2401
+ int API_EXPORTED libusb_init(libusb_context **ctx)
2402
+ {
2403
+ return libusb_init_context(ctx, NULL, 0);
2404
+ }
2405
+
2267
2406
  /** \ingroup libusb_lib
2268
2407
  * Initialize libusb. This function must be called before calling any other
2269
2408
  * libusb function.
2270
2409
  *
2271
2410
  * If you do not provide an output location for a context pointer, a default
2272
2411
  * context will be created. If there was already a default context, it will
2273
- * be reused (and nothing will be initialized/reinitialized).
2412
+ * be reused (and nothing will be initialized/reinitialized and options will
2413
+ * be ignored). If num_options is 0 then options is ignored and may be NULL.
2414
+ *
2415
+ * Since version 1.0.27, \ref LIBUSB_API_VERSION >= 0x0100010A
2274
2416
  *
2275
2417
  * \param ctx Optional output location for context pointer.
2276
2418
  * Only valid on return code 0.
2419
+ * \param options Optional array of options to set on the new context.
2420
+ * \param num_options Number of elements in the options array.
2277
2421
  * \returns 0 on success, or a LIBUSB_ERROR code on failure
2278
2422
  * \see libusb_contexts
2279
2423
  */
2280
- int API_EXPORTED libusb_init(libusb_context **ctx)
2424
+ int API_EXPORTED libusb_init_context(libusb_context **ctx, const struct libusb_init_option options[], int num_options)
2281
2425
  {
2282
2426
  size_t priv_size = usbi_backend.context_priv_size;
2283
2427
  struct libusb_context *_ctx;
@@ -2293,10 +2437,12 @@ int API_EXPORTED libusb_init(libusb_context **ctx)
2293
2437
  }
2294
2438
 
2295
2439
  /* check for first init */
2440
+ usbi_mutex_static_lock(&active_contexts_lock);
2296
2441
  if (!active_contexts_list.next) {
2297
2442
  list_init(&active_contexts_list);
2298
2443
  usbi_get_monotonic_time(&timestamp_origin);
2299
2444
  }
2445
+ usbi_mutex_static_unlock(&active_contexts_lock);
2300
2446
 
2301
2447
  _ctx = calloc(1, PTR_ALIGN(sizeof(*_ctx)) + priv_size);
2302
2448
  if (!_ctx) {
@@ -2305,13 +2451,13 @@ int API_EXPORTED libusb_init(libusb_context **ctx)
2305
2451
  }
2306
2452
 
2307
2453
  #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
2308
- if (NULL == ctx && default_context_options[LIBUSB_OPTION_LOG_LEVEL].is_set) {
2309
- _ctx->debug = default_context_options[LIBUSB_OPTION_LOG_LEVEL].arg.ival;
2310
- } else {
2454
+ _ctx->debug = LIBUSB_LOG_LEVEL_NONE;
2455
+ if (getenv("LIBUSB_DEBUG")) {
2311
2456
  _ctx->debug = get_env_debug_level();
2312
- }
2313
- if (_ctx->debug != LIBUSB_LOG_LEVEL_NONE)
2314
2457
  _ctx->debug_fixed = 1;
2458
+ } else if (default_context_options[LIBUSB_OPTION_LOG_LEVEL].is_set) {
2459
+ _ctx->debug = default_context_options[LIBUSB_OPTION_LOG_LEVEL].arg.ival;
2460
+ }
2315
2461
  #endif
2316
2462
 
2317
2463
  usbi_mutex_init(&_ctx->usb_devs_lock);
@@ -2324,7 +2470,29 @@ int API_EXPORTED libusb_init(libusb_context **ctx)
2324
2470
  if (LIBUSB_OPTION_LOG_LEVEL == option || !default_context_options[option].is_set) {
2325
2471
  continue;
2326
2472
  }
2327
- r = libusb_set_option(_ctx, option);
2473
+ if (LIBUSB_OPTION_LOG_CB != option) {
2474
+ r = libusb_set_option(_ctx, option);
2475
+ } else {
2476
+ r = libusb_set_option(_ctx, option, default_context_options[option].arg.log_cbval);
2477
+ }
2478
+ if (LIBUSB_SUCCESS != r)
2479
+ goto err_free_ctx;
2480
+ }
2481
+
2482
+ /* apply any options provided by the user */
2483
+ for (int i = 0 ; i < num_options ; ++i) {
2484
+ switch(options[i].option) {
2485
+ case LIBUSB_OPTION_LOG_CB:
2486
+ r = libusb_set_option(_ctx, options[i].option, options[i].value.log_cbval);
2487
+ break;
2488
+
2489
+ case LIBUSB_OPTION_LOG_LEVEL:
2490
+ case LIBUSB_OPTION_USE_USBDK:
2491
+ case LIBUSB_OPTION_NO_DEVICE_DISCOVERY:
2492
+ case LIBUSB_OPTION_MAX:
2493
+ default:
2494
+ r = libusb_set_option(_ctx, options[i].option, options[i].value.ival);
2495
+ }
2328
2496
  if (LIBUSB_SUCCESS != r)
2329
2497
  goto err_free_ctx;
2330
2498
  }
@@ -2333,6 +2501,9 @@ int API_EXPORTED libusb_init(libusb_context **ctx)
2333
2501
  if (!ctx) {
2334
2502
  usbi_default_context = _ctx;
2335
2503
  default_context_refcnt = 1;
2504
+ #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
2505
+ usbi_atomic_store(&default_debug_level, _ctx->debug);
2506
+ #endif
2336
2507
  usbi_dbg(usbi_default_context, "created default context");
2337
2508
  }
2338
2509
 
@@ -2360,8 +2531,12 @@ int API_EXPORTED libusb_init(libusb_context **ctx)
2360
2531
  *ctx = _ctx;
2361
2532
 
2362
2533
  if (!usbi_fallback_context) {
2534
+ #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
2535
+ if (usbi_atomic_load(&default_debug_level) == -1)
2536
+ usbi_atomic_store(&default_debug_level, _ctx->debug);
2537
+ #endif
2363
2538
  usbi_fallback_context = _ctx;
2364
- usbi_warn(usbi_fallback_context, "installing new context as implicit default");
2539
+ usbi_dbg(usbi_fallback_context, "installing new context as implicit default");
2365
2540
  }
2366
2541
  }
2367
2542
 
@@ -2432,6 +2607,9 @@ void API_EXPORTED libusb_exit(libusb_context *ctx)
2432
2607
  list_del(&_ctx->list);
2433
2608
  usbi_mutex_static_unlock(&active_contexts_lock);
2434
2609
 
2610
+ /* Exit hotplug before backend dependency */
2611
+ usbi_hotplug_exit(_ctx);
2612
+
2435
2613
  if (usbi_backend.exit)
2436
2614
  usbi_backend.exit(_ctx);
2437
2615
 
@@ -2445,7 +2623,6 @@ void API_EXPORTED libusb_exit(libusb_context *ctx)
2445
2623
  /* Don't bother with locking after this point because unless there is
2446
2624
  * an application bug, nobody will be accessing the context. */
2447
2625
 
2448
- usbi_hotplug_exit(_ctx);
2449
2626
  usbi_io_exit(_ctx);
2450
2627
 
2451
2628
  for_each_device(_ctx, dev) {
@@ -2465,7 +2642,7 @@ void API_EXPORTED libusb_exit(libusb_context *ctx)
2465
2642
 
2466
2643
  /** \ingroup libusb_misc
2467
2644
  * Check at runtime if the loaded library has a given capability.
2468
- * This call should be performed after \ref libusb_init(), to ensure the
2645
+ * This call should be performed after \ref libusb_init_context(), to ensure the
2469
2646
  * backend has updated its capability set.
2470
2647
  *
2471
2648
  * \param capability the \ref libusb_capability to check for
@@ -2584,13 +2761,14 @@ static void log_v(struct libusb_context *ctx, enum libusb_log_level level,
2584
2761
  UNUSED(ctx);
2585
2762
  #else
2586
2763
  enum libusb_log_level ctx_level;
2764
+ long default_level_value;
2587
2765
 
2588
- ctx = ctx ? ctx : usbi_default_context;
2589
- ctx = ctx ? ctx : usbi_fallback_context;
2590
- if (ctx)
2766
+ if (ctx) {
2591
2767
  ctx_level = ctx->debug;
2592
- else
2593
- ctx_level = get_env_debug_level();
2768
+ } else {
2769
+ default_level_value = usbi_atomic_load(&default_debug_level);
2770
+ ctx_level = default_level_value < 0 ? get_env_debug_level() : (enum libusb_log_level)default_level_value;
2771
+ }
2594
2772
 
2595
2773
  if (ctx_level < level)
2596
2774
  return;