usb 2.0.0-alpha.2 → 2.0.1

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