usb 2.1.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +3 -0
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.js +3 -1
  5. package/dist/index.js.map +1 -1
  6. package/dist/usb/bindings.d.ts +4 -0
  7. package/dist/usb/device.js +5 -0
  8. package/dist/usb/device.js.map +1 -1
  9. package/libusb/.gitattributes +2 -6
  10. package/libusb/.private/appveyor_build.sh +22 -0
  11. package/libusb/.private/bm.sh +1 -1
  12. package/libusb/.private/ci-build.sh +67 -0
  13. package/libusb/.private/post-rewrite.sh +5 -1
  14. package/libusb/.private/pre-commit.sh +5 -1
  15. package/libusb/.private/wbs.txt +4 -19
  16. package/libusb/.travis.yml +32 -23
  17. package/libusb/AUTHORS +76 -3
  18. package/libusb/ChangeLog +41 -3
  19. package/libusb/INSTALL_WIN.txt +22 -44
  20. package/libusb/Makefile.am +32 -10
  21. package/libusb/{README.md → README} +2 -2
  22. package/libusb/README.git +3 -3
  23. package/libusb/Xcode/common.xcconfig +23 -19
  24. package/libusb/Xcode/config.h +25 -13
  25. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +511 -109
  26. package/libusb/android/README +90 -54
  27. package/libusb/android/config.h +23 -43
  28. package/libusb/android/examples/unrooted_android.c +301 -0
  29. package/libusb/android/examples/unrooted_android.h +36 -0
  30. package/libusb/android/jni/Android.mk +1 -1
  31. package/libusb/android/jni/Application.mk +16 -0
  32. package/libusb/android/jni/examples.mk +63 -29
  33. package/libusb/android/jni/libusb.mk +14 -8
  34. package/libusb/android/jni/tests.mk +13 -24
  35. package/libusb/appveyor.yml +69 -30
  36. package/libusb/autogen.sh +5 -3
  37. package/libusb/bootstrap.sh +6 -2
  38. package/libusb/configure.ac +302 -228
  39. package/libusb/doc/Makefile.in +22 -0
  40. package/libusb/doc/doxygen.cfg.in +460 -223
  41. package/libusb/examples/Makefile.am +6 -13
  42. package/libusb/examples/dpfp.c +276 -73
  43. package/libusb/examples/ezusb.c +18 -8
  44. package/libusb/examples/ezusb.h +6 -17
  45. package/libusb/examples/fxload.c +4 -5
  46. package/libusb/examples/hotplugtest.c +1 -1
  47. package/libusb/examples/sam3u_benchmark.c +59 -24
  48. package/libusb/examples/testlibusb.c +138 -104
  49. package/libusb/examples/xusb.c +26 -22
  50. package/libusb/libusb/Makefile.am +57 -70
  51. package/libusb/libusb/Makefile.am.extra +26 -0
  52. package/libusb/libusb/core.c +420 -423
  53. package/libusb/libusb/descriptor.c +365 -419
  54. package/libusb/libusb/hotplug.c +197 -104
  55. package/libusb/libusb/io.c +491 -528
  56. package/libusb/libusb/libusb-1.0.def +7 -3
  57. package/libusb/libusb/libusb-1.0.rc +1 -9
  58. package/libusb/libusb/libusb.h +295 -226
  59. package/libusb/libusb/libusbi.h +587 -314
  60. package/libusb/libusb/os/darwin_usb.c +634 -317
  61. package/libusb/libusb/os/darwin_usb.h +39 -15
  62. package/libusb/libusb/os/events_posix.c +300 -0
  63. package/libusb/libusb/os/events_posix.h +59 -0
  64. package/libusb/libusb/os/events_windows.c +214 -0
  65. package/libusb/{msvc/missing.h → libusb/os/events_windows.h} +25 -11
  66. package/libusb/libusb/os/haiku_pollfs.cpp +14 -9
  67. package/libusb/libusb/os/haiku_usb.h +12 -12
  68. package/libusb/libusb/os/haiku_usb_backend.cpp +36 -37
  69. package/libusb/libusb/os/haiku_usb_raw.cpp +80 -116
  70. package/libusb/libusb/os/linux_netlink.c +55 -63
  71. package/libusb/libusb/os/linux_udev.c +61 -69
  72. package/libusb/libusb/os/linux_usbfs.c +926 -1015
  73. package/libusb/libusb/os/linux_usbfs.h +74 -57
  74. package/libusb/libusb/os/netbsd_usb.c +103 -168
  75. package/libusb/libusb/os/null_usb.c +111 -0
  76. package/libusb/libusb/os/openbsd_usb.c +71 -120
  77. package/libusb/libusb/os/sunos_usb.c +289 -375
  78. package/libusb/libusb/os/sunos_usb.h +0 -1
  79. package/libusb/libusb/os/threads_posix.c +81 -32
  80. package/libusb/libusb/os/threads_posix.h +19 -23
  81. package/libusb/libusb/os/threads_windows.c +9 -95
  82. package/libusb/libusb/os/threads_windows.h +33 -31
  83. package/libusb/libusb/os/windows_common.c +904 -0
  84. package/libusb/libusb/os/windows_common.h +329 -42
  85. package/libusb/libusb/os/windows_usbdk.c +161 -267
  86. package/libusb/libusb/os/windows_usbdk.h +5 -2
  87. package/libusb/libusb/os/windows_winusb.c +1326 -1190
  88. package/libusb/libusb/os/windows_winusb.h +167 -167
  89. package/libusb/libusb/strerror.c +20 -30
  90. package/libusb/libusb/sync.c +20 -21
  91. package/libusb/libusb/version.h +1 -1
  92. package/libusb/libusb/version_nano.h +1 -1
  93. package/libusb/msvc/.gitattributes +3 -0
  94. package/libusb/msvc/config.h +27 -20
  95. package/libusb/msvc/{hotplugtest_2012.vcxproj → dpfp_2013.vcxproj} +14 -10
  96. package/libusb/msvc/dpfp_2013.vcxproj.filters +26 -0
  97. package/libusb/msvc/{listdevs_2010.vcxproj → dpfp_2015.vcxproj} +14 -9
  98. package/libusb/msvc/dpfp_2015.vcxproj.filters +26 -0
  99. package/libusb/msvc/dpfp_2017.vcxproj +106 -0
  100. package/libusb/msvc/dpfp_2017.vcxproj.filters +26 -0
  101. package/libusb/msvc/dpfp_2019.vcxproj +106 -0
  102. package/libusb/msvc/dpfp_2019.vcxproj.filters +26 -0
  103. package/libusb/msvc/dpfp_threaded_2013.vcxproj +87 -0
  104. package/libusb/msvc/dpfp_threaded_2013.vcxproj.filters +26 -0
  105. package/libusb/msvc/dpfp_threaded_2015.vcxproj +87 -0
  106. package/libusb/msvc/dpfp_threaded_2015.vcxproj.filters +26 -0
  107. package/libusb/msvc/dpfp_threaded_2017.vcxproj +106 -0
  108. package/libusb/msvc/dpfp_threaded_2017.vcxproj.filters +26 -0
  109. package/libusb/msvc/{fxload_2012.vcxproj → dpfp_threaded_2019.vcxproj} +32 -17
  110. package/libusb/msvc/dpfp_threaded_2019.vcxproj.filters +26 -0
  111. package/libusb/msvc/fxload_2013.vcxproj +6 -3
  112. package/libusb/msvc/fxload_2013.vcxproj.filters +35 -0
  113. package/libusb/msvc/fxload_2015.vcxproj +6 -3
  114. package/libusb/msvc/fxload_2015.vcxproj.filters +35 -0
  115. package/libusb/msvc/fxload_2017.vcxproj +6 -7
  116. package/libusb/msvc/fxload_2017.vcxproj.filters +35 -0
  117. package/libusb/msvc/{fxload_2010.vcxproj → fxload_2019.vcxproj} +29 -6
  118. package/libusb/msvc/fxload_2019.vcxproj.filters +35 -0
  119. package/libusb/{examples → msvc}/getopt/getopt.c +0 -0
  120. package/libusb/{examples → msvc}/getopt/getopt.h +0 -0
  121. package/libusb/{examples → msvc}/getopt/getopt1.c +0 -0
  122. package/libusb/msvc/getopt_2013.vcxproj +4 -5
  123. package/libusb/msvc/getopt_2013.vcxproj.filters +26 -0
  124. package/libusb/msvc/getopt_2015.vcxproj +4 -4
  125. package/libusb/msvc/getopt_2015.vcxproj.filters +26 -0
  126. package/libusb/msvc/getopt_2017.vcxproj +4 -10
  127. package/libusb/msvc/getopt_2017.vcxproj.filters +26 -0
  128. package/libusb/msvc/{getopt_2012.vcxproj → getopt_2019.vcxproj} +25 -6
  129. package/libusb/msvc/getopt_2019.vcxproj.filters +26 -0
  130. package/libusb/msvc/hotplugtest_2013.vcxproj +6 -3
  131. package/libusb/msvc/hotplugtest_2013.vcxproj.filters +23 -0
  132. package/libusb/msvc/hotplugtest_2015.vcxproj +6 -3
  133. package/libusb/msvc/hotplugtest_2015.vcxproj.filters +23 -0
  134. package/libusb/msvc/hotplugtest_2017.vcxproj +6 -7
  135. package/libusb/msvc/hotplugtest_2017.vcxproj.filters +23 -0
  136. package/libusb/msvc/hotplugtest_2019.vcxproj +105 -0
  137. package/libusb/msvc/hotplugtest_2019.vcxproj.filters +23 -0
  138. package/libusb/msvc/libusb_2013.sln +50 -20
  139. package/libusb/msvc/libusb_2015.sln +51 -21
  140. package/libusb/msvc/libusb_2017.sln +90 -36
  141. package/libusb/msvc/libusb_2019.sln +240 -0
  142. package/libusb/msvc/libusb_dll_2013.vcxproj +6 -9
  143. package/libusb/msvc/libusb_dll_2013.vcxproj.filters +94 -0
  144. package/libusb/msvc/libusb_dll_2015.vcxproj +6 -8
  145. package/libusb/msvc/libusb_dll_2015.vcxproj.filters +94 -0
  146. package/libusb/msvc/libusb_dll_2017.vcxproj +6 -16
  147. package/libusb/msvc/libusb_dll_2017.vcxproj.filters +94 -0
  148. package/libusb/msvc/{libusb_dll_2010.vcxproj → libusb_dll_2019.vcxproj} +27 -9
  149. package/libusb/msvc/libusb_dll_2019.vcxproj.filters +94 -0
  150. package/libusb/msvc/libusb_static_2013.vcxproj +5 -9
  151. package/libusb/msvc/libusb_static_2013.vcxproj.filters +80 -0
  152. package/libusb/msvc/libusb_static_2015.vcxproj +5 -8
  153. package/libusb/msvc/libusb_static_2015.vcxproj.filters +80 -0
  154. package/libusb/msvc/libusb_static_2017.vcxproj +5 -8
  155. package/libusb/msvc/libusb_static_2017.vcxproj.filters +80 -0
  156. package/libusb/msvc/{libusb_static_2010.vcxproj → libusb_static_2019.vcxproj} +26 -9
  157. package/libusb/msvc/libusb_static_2019.vcxproj.filters +80 -0
  158. package/libusb/msvc/listdevs_2013.vcxproj +6 -3
  159. package/libusb/msvc/listdevs_2013.vcxproj.filters +23 -0
  160. package/libusb/msvc/listdevs_2015.vcxproj +6 -3
  161. package/libusb/msvc/listdevs_2015.vcxproj.filters +23 -0
  162. package/libusb/msvc/listdevs_2017.vcxproj +6 -7
  163. package/libusb/msvc/listdevs_2017.vcxproj.filters +23 -0
  164. package/libusb/msvc/listdevs_2019.vcxproj +105 -0
  165. package/libusb/msvc/listdevs_2019.vcxproj.filters +23 -0
  166. package/libusb/msvc/{listdevs_2012.vcxproj → sam3u_benchmark_2013.vcxproj} +13 -9
  167. package/libusb/msvc/sam3u_benchmark_2013.vcxproj.filters +26 -0
  168. package/libusb/msvc/{hotplugtest_2010.vcxproj → sam3u_benchmark_2015.vcxproj} +13 -8
  169. package/libusb/msvc/sam3u_benchmark_2015.vcxproj.filters +26 -0
  170. package/libusb/msvc/sam3u_benchmark_2017.vcxproj +106 -0
  171. package/libusb/msvc/sam3u_benchmark_2017.vcxproj.filters +26 -0
  172. package/libusb/msvc/sam3u_benchmark_2019.vcxproj +106 -0
  173. package/libusb/msvc/sam3u_benchmark_2019.vcxproj.filters +26 -0
  174. package/libusb/msvc/stress_2013.vcxproj +4 -2
  175. package/libusb/msvc/stress_2013.vcxproj.filters +32 -0
  176. package/libusb/msvc/stress_2015.vcxproj +4 -2
  177. package/libusb/msvc/stress_2015.vcxproj.filters +32 -0
  178. package/libusb/msvc/stress_2017.vcxproj +4 -6
  179. package/libusb/msvc/stress_2017.vcxproj.filters +32 -0
  180. package/libusb/msvc/{stress_2010.vcxproj → stress_2019.vcxproj} +26 -4
  181. package/libusb/msvc/stress_2019.vcxproj.filters +32 -0
  182. package/libusb/msvc/testlibusb_2013.vcxproj +6 -3
  183. package/libusb/msvc/testlibusb_2013.vcxproj.filters +23 -0
  184. package/libusb/msvc/testlibusb_2015.vcxproj +6 -3
  185. package/libusb/msvc/testlibusb_2015.vcxproj.filters +23 -0
  186. package/libusb/msvc/testlibusb_2017.vcxproj +6 -7
  187. package/libusb/msvc/testlibusb_2017.vcxproj.filters +23 -0
  188. package/libusb/msvc/{testlibusb_2010.vcxproj → testlibusb_2019.vcxproj} +28 -5
  189. package/libusb/msvc/testlibusb_2019.vcxproj.filters +23 -0
  190. package/libusb/msvc/xusb_2013.vcxproj +6 -3
  191. package/libusb/msvc/xusb_2013.vcxproj.filters +23 -0
  192. package/libusb/msvc/xusb_2015.vcxproj +6 -3
  193. package/libusb/msvc/xusb_2015.vcxproj.filters +23 -0
  194. package/libusb/msvc/xusb_2017.vcxproj +6 -7
  195. package/libusb/msvc/xusb_2017.vcxproj.filters +23 -0
  196. package/libusb/msvc/{xusb_2010.vcxproj → xusb_2019.vcxproj} +28 -5
  197. package/libusb/msvc/xusb_2019.vcxproj.filters +23 -0
  198. package/libusb/tests/Makefile.am +1 -0
  199. package/libusb/tests/libusb_testlib.h +12 -43
  200. package/libusb/tests/stress.c +59 -50
  201. package/libusb/tests/testlib.c +78 -171
  202. package/libusb.gypi +10 -11
  203. package/package.json +1 -1
  204. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  205. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  206. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  207. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  208. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  209. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  210. package/prebuilds/linux-ia32/node.napi.node +0 -0
  211. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  212. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  213. package/prebuilds/win32-ia32/node.napi.node +0 -0
  214. package/prebuilds/win32-x64/node.napi.node +0 -0
  215. package/src/device.cc +12 -7
  216. package/src/node_usb.cc +10 -0
  217. package/test/usb.coffee +6 -0
  218. package/tsc/index.ts +2 -0
  219. package/tsc/usb/bindings.ts +5 -0
  220. package/tsc/usb/device.ts +6 -0
  221. package/libusb/.private/bd.cmd +0 -89
  222. package/libusb/.private/bwince.cmd +0 -57
  223. package/libusb/.private/wbs_wince.txt +0 -42
  224. package/libusb/Brewfile +0 -4
  225. package/libusb/appveyor_cygwin.bat +0 -11
  226. package/libusb/appveyor_minGW.bat +0 -19
  227. package/libusb/doc/Makefile.am +0 -9
  228. package/libusb/examples/dpfp_threaded.c +0 -557
  229. package/libusb/libusb/hotplug.h +0 -99
  230. package/libusb/libusb/os/poll_posix.c +0 -84
  231. package/libusb/libusb/os/poll_posix.h +0 -14
  232. package/libusb/libusb/os/poll_windows.c +0 -447
  233. package/libusb/libusb/os/poll_windows.h +0 -98
  234. package/libusb/libusb/os/wince_usb.c +0 -888
  235. package/libusb/libusb/os/wince_usb.h +0 -126
  236. package/libusb/libusb/os/windows_nt_common.c +0 -1010
  237. package/libusb/libusb/os/windows_nt_common.h +0 -110
  238. package/libusb/libusb/os/windows_nt_shared_types.h +0 -147
  239. package/libusb/msvc/appveyor.bat +0 -27
  240. package/libusb/msvc/ddk_build.cmd +0 -219
  241. package/libusb/msvc/errno.h +0 -102
  242. package/libusb/msvc/fxload_sources +0 -23
  243. package/libusb/msvc/getopt_2005.vcproj +0 -288
  244. package/libusb/msvc/getopt_2010.vcxproj +0 -72
  245. package/libusb/msvc/getopt_sources +0 -24
  246. package/libusb/msvc/hotplugtest_sources +0 -20
  247. package/libusb/msvc/inttypes.h +0 -295
  248. package/libusb/msvc/libusb.dsw +0 -71
  249. package/libusb/msvc/libusb_2005.sln +0 -95
  250. package/libusb/msvc/libusb_2010.sln +0 -105
  251. package/libusb/msvc/libusb_2012.sln +0 -105
  252. package/libusb/msvc/libusb_dll.dsp +0 -194
  253. package/libusb/msvc/libusb_dll_2005.vcproj +0 -464
  254. package/libusb/msvc/libusb_dll_2012.vcxproj +0 -107
  255. package/libusb/msvc/libusb_dll_wince.vcproj +0 -1251
  256. package/libusb/msvc/libusb_sources +0 -43
  257. package/libusb/msvc/libusb_static.dsp +0 -174
  258. package/libusb/msvc/libusb_static_2005.vcproj +0 -390
  259. package/libusb/msvc/libusb_static_2012.vcxproj +0 -98
  260. package/libusb/msvc/libusb_static_wince.vcproj +0 -1179
  261. package/libusb/msvc/libusb_wince.sln +0 -246
  262. package/libusb/msvc/listdevs.dsp +0 -103
  263. package/libusb/msvc/listdevs_2005.vcproj +0 -360
  264. package/libusb/msvc/listdevs_sources +0 -20
  265. package/libusb/msvc/listdevs_wince.vcproj +0 -1120
  266. package/libusb/msvc/missing.c +0 -80
  267. package/libusb/msvc/stdint.h +0 -256
  268. package/libusb/msvc/stress_2005.vcproj +0 -390
  269. package/libusb/msvc/stress_2012.vcxproj +0 -87
  270. package/libusb/msvc/stress_sources +0 -21
  271. package/libusb/msvc/stress_wince.vcproj +0 -1128
  272. package/libusb/msvc/testlibusb_2012.vcxproj +0 -83
  273. package/libusb/msvc/testlibusb_sources +0 -20
  274. package/libusb/msvc/xusb.dsp +0 -102
  275. package/libusb/msvc/xusb_2005.vcproj +0 -344
  276. package/libusb/msvc/xusb_2012.vcxproj +0 -83
  277. package/libusb/msvc/xusb_sources +0 -20
  278. package/libusb/msvc/xusb_wince.vcproj +0 -1120
  279. package/libusb/travis-autogen.sh +0 -39
@@ -3,7 +3,8 @@
3
3
  * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
4
4
  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
5
5
  * Copyright © 2019 Nathan Hjelm <hjelmn@cs.umm.edu>
6
- * Copyright © 2019 Google LLC. All rights reserved.
6
+ * Copyright © 2019-2020 Google LLC. All rights reserved.
7
+ * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
7
8
  *
8
9
  * This library is free software; you can redistribute it and/or
9
10
  * modify it under the terms of the GNU Lesser General Public
@@ -25,34 +26,98 @@
25
26
 
26
27
  #include <config.h>
27
28
 
28
- #include <stdlib.h>
29
29
  #include <assert.h>
30
- #include <stddef.h>
31
- #include <stdint.h>
32
- #include <time.h>
30
+ #include <inttypes.h>
33
31
  #include <stdarg.h>
34
- #ifdef HAVE_POLL_H
35
- #include <poll.h>
36
- #endif
37
- #ifdef HAVE_MISSING_H
38
- #include <missing.h>
32
+ #include <stddef.h>
33
+ #include <stdlib.h>
34
+ #ifdef HAVE_SYS_TIME_H
35
+ #include <sys/time.h>
39
36
  #endif
40
37
 
41
38
  #include "libusb.h"
42
- #include "version.h"
43
39
 
44
- /* Attribute to ensure that a structure member is aligned to a natural
45
- * pointer alignment. Used for os_priv member. */
46
- #if defined(_MSC_VER)
47
- #if defined(_WIN64)
48
- #define PTR_ALIGNED __declspec(align(8))
40
+ /* Not all C standard library headers define static_assert in assert.h
41
+ * Additionally, Visual Studio treats static_assert as a keyword.
42
+ */
43
+ #if !defined(__cplusplus) && !defined(static_assert) && !defined(_MSC_VER)
44
+ #define static_assert(cond, msg) _Static_assert(cond, msg)
45
+ #endif
46
+
47
+ #ifdef NDEBUG
48
+ #define ASSERT_EQ(expression, value) (void)expression
49
+ #define ASSERT_NE(expression, value) (void)expression
49
50
  #else
50
- #define PTR_ALIGNED __declspec(align(4))
51
+ #define ASSERT_EQ(expression, value) assert(expression == value)
52
+ #define ASSERT_NE(expression, value) assert(expression != value)
51
53
  #endif
52
- #elif defined(__GNUC__) && (__GNUC__ >= 3)
53
- #define PTR_ALIGNED __attribute__((aligned(sizeof(void *))))
54
+
55
+ #define container_of(ptr, type, member) \
56
+ ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
57
+
58
+ #ifndef ARRAYSIZE
59
+ #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
60
+ #endif
61
+
62
+ #ifndef CLAMP
63
+ #define CLAMP(val, min, max) \
64
+ ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
65
+ #endif
66
+
67
+ #ifndef MIN
68
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
69
+ #endif
70
+
71
+ #ifndef MAX
72
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
73
+ #endif
74
+
75
+ /* The following is used to silence warnings for unused variables */
76
+ #if defined(UNREFERENCED_PARAMETER)
77
+ #define UNUSED(var) UNREFERENCED_PARAMETER(var)
78
+ #else
79
+ #define UNUSED(var) do { (void)(var); } while(0)
80
+ #endif
81
+
82
+ /* Macro to align a value up to the next multiple of the size of a pointer */
83
+ #define PTR_ALIGN(v) \
84
+ (((v) + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1))
85
+
86
+ /* Atomic operations
87
+ *
88
+ * Useful for reference counting or when accessing a value without a lock
89
+ *
90
+ * The following atomic operations are defined:
91
+ * usbi_atomic_load() - Atomically read a variable's value
92
+ * usbi_atomic_store() - Atomically write a new value value to a variable
93
+ * usbi_atomic_inc() - Atomically increment a variable's value and return the new value
94
+ * usbi_atomic_dec() - Atomically decrement a variable's value and return the new value
95
+ *
96
+ * All of these operations are ordered with each other, thus the effects of
97
+ * any one operation is guaranteed to be seen by any other operation.
98
+ */
99
+ #ifdef _MSC_VER
100
+ typedef volatile LONG usbi_atomic_t;
101
+ #define usbi_atomic_load(a) (*(a))
102
+ #define usbi_atomic_store(a, v) (*(a)) = (v)
103
+ #define usbi_atomic_inc(a) InterlockedIncrement((a))
104
+ #define usbi_atomic_dec(a) InterlockedDecrement((a))
54
105
  #else
55
- #define PTR_ALIGNED
106
+ #include <stdatomic.h>
107
+ typedef atomic_long usbi_atomic_t;
108
+ #define usbi_atomic_load(a) atomic_load((a))
109
+ #define usbi_atomic_store(a, v) atomic_store((a), (v))
110
+ #define usbi_atomic_inc(a) (atomic_fetch_add((a), 1) + 1)
111
+ #define usbi_atomic_dec(a) (atomic_fetch_add((a), -1) - 1)
112
+ #endif
113
+
114
+ /* Internal abstractions for event handling and thread synchronization */
115
+ #if defined(PLATFORM_POSIX)
116
+ #include "os/events_posix.h"
117
+ #include "os/threads_posix.h"
118
+ #elif defined(PLATFORM_WINDOWS)
119
+ #include "os/events_windows.h"
120
+ #include "os/threads_windows.h"
56
121
  #endif
57
122
 
58
123
  /* Inside the libusb code, mark all public functions as follows:
@@ -64,22 +129,10 @@
64
129
  */
65
130
  #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
66
131
 
67
- /* Macro to decorate printf-like functions, in order to get
68
- * compiler warnings about format string mistakes.
69
- */
70
- #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
71
- #define USBI_PRINTFLIKE(formatarg, firstvararg) \
72
- __attribute__((__format__ (__printf__, formatarg, firstvararg)))
73
- #else
74
- #define USBI_PRINTFLIKE(formatarg, firstvararg)
75
- #endif
76
-
77
132
  #ifdef __cplusplus
78
133
  extern "C" {
79
134
  #endif
80
135
 
81
- #define DEVICE_DESC_LENGTH 18
82
-
83
136
  #define USB_MAXENDPOINTS 32
84
137
  #define USB_MAXINTERFACES 32
85
138
  #define USB_MAXCONFIG 8
@@ -93,13 +146,6 @@ extern "C" {
93
146
  /* Terminator for log lines */
94
147
  #define USBI_LOG_LINE_END "\n"
95
148
 
96
- /* The following is used to silence warnings for unused variables */
97
- #define UNUSED(var) do { (void)(var); } while(0)
98
-
99
- #if !defined(ARRAYSIZE)
100
- #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
101
- #endif
102
-
103
149
  struct list_head {
104
150
  struct list_head *prev, *next;
105
151
  };
@@ -110,11 +156,14 @@ struct list_head {
110
156
  * member - the list_head element in "type"
111
157
  */
112
158
  #define list_entry(ptr, type, member) \
113
- ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
159
+ container_of(ptr, type, member)
114
160
 
115
161
  #define list_first_entry(ptr, type, member) \
116
162
  list_entry((ptr)->next, type, member)
117
163
 
164
+ #define list_next_entry(ptr, type, member) \
165
+ list_entry((ptr)->member.next, type, member)
166
+
118
167
  /* Get each entry from a list
119
168
  * pos - A structure pointer has a "member" element
120
169
  * head - list head
@@ -122,15 +171,24 @@ struct list_head {
122
171
  * type - the type of the first parameter
123
172
  */
124
173
  #define list_for_each_entry(pos, head, member, type) \
125
- for (pos = list_entry((head)->next, type, member); \
174
+ for (pos = list_first_entry(head, type, member); \
126
175
  &pos->member != (head); \
127
- pos = list_entry(pos->member.next, type, member))
176
+ pos = list_next_entry(pos, type, member))
128
177
 
129
178
  #define list_for_each_entry_safe(pos, n, head, member, type) \
130
- for (pos = list_entry((head)->next, type, member), \
131
- n = list_entry(pos->member.next, type, member); \
179
+ for (pos = list_first_entry(head, type, member), \
180
+ n = list_next_entry(pos, type, member); \
132
181
  &pos->member != (head); \
133
- pos = n, n = list_entry(n->member.next, type, member))
182
+ pos = n, n = list_next_entry(n, type, member))
183
+
184
+ /* Helper macros to iterate over a list. The structure pointed
185
+ * to by "pos" must have a list_head member named "list".
186
+ */
187
+ #define for_each_helper(pos, head, type) \
188
+ list_for_each_entry(pos, head, list, type)
189
+
190
+ #define for_each_safe_helper(pos, n, head, type) \
191
+ list_for_each_entry_safe(pos, n, head, list, type)
134
192
 
135
193
  #define list_empty(entry) ((entry)->next == (entry))
136
194
 
@@ -167,8 +225,10 @@ static inline void list_del(struct list_head *entry)
167
225
 
168
226
  static inline void list_cut(struct list_head *list, struct list_head *head)
169
227
  {
170
- if (list_empty(head))
228
+ if (list_empty(head)) {
229
+ list_init(list);
171
230
  return;
231
+ }
172
232
 
173
233
  list->next = head->next;
174
234
  list->next->prev = list;
@@ -178,31 +238,52 @@ static inline void list_cut(struct list_head *list, struct list_head *head)
178
238
  list_init(head);
179
239
  }
180
240
 
241
+ static inline void list_splice_front(struct list_head *list, struct list_head *head)
242
+ {
243
+ list->next->prev = head;
244
+ list->prev->next = head->next;
245
+ head->next->prev = list->prev;
246
+ head->next = list->next;
247
+ }
248
+
181
249
  static inline void *usbi_reallocf(void *ptr, size_t size)
182
250
  {
183
251
  void *ret = realloc(ptr, size);
252
+
184
253
  if (!ret)
185
254
  free(ptr);
186
255
  return ret;
187
256
  }
188
257
 
189
- #define container_of(ptr, type, member) ({ \
190
- const typeof( ((type *)0)->member ) *mptr = (ptr); \
191
- (type *)( (char *)mptr - offsetof(type,member) );})
192
-
193
- #ifndef CLAMP
194
- #define CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
195
- #endif
196
- #ifndef MIN
197
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
258
+ #if !defined(USEC_PER_SEC)
259
+ #define USEC_PER_SEC 1000000L
198
260
  #endif
199
- #ifndef MAX
200
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
261
+
262
+ #if !defined(NSEC_PER_SEC)
263
+ #define NSEC_PER_SEC 1000000000L
201
264
  #endif
202
265
 
203
- #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
266
+ #define TIMEVAL_IS_VALID(tv) \
267
+ ((tv)->tv_sec >= 0 && \
268
+ (tv)->tv_usec >= 0 && (tv)->tv_usec < USEC_PER_SEC)
269
+
270
+ #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec || (ts)->tv_nsec)
271
+ #define TIMESPEC_CLEAR(ts) (ts)->tv_sec = (ts)->tv_nsec = 0
272
+ #define TIMESPEC_CMP(a, b, CMP) \
273
+ (((a)->tv_sec == (b)->tv_sec) \
274
+ ? ((a)->tv_nsec CMP (b)->tv_nsec) \
275
+ : ((a)->tv_sec CMP (b)->tv_sec))
276
+ #define TIMESPEC_SUB(a, b, result) \
277
+ do { \
278
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
279
+ (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
280
+ if ((result)->tv_nsec < 0L) { \
281
+ --(result)->tv_sec; \
282
+ (result)->tv_nsec += NSEC_PER_SEC; \
283
+ } \
284
+ } while (0)
204
285
 
205
- #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
286
+ #if defined(PLATFORM_WINDOWS)
206
287
  #define TIMEVAL_TV_SEC_TYPE long
207
288
  #else
208
289
  #define TIMEVAL_TV_SEC_TYPE time_t
@@ -213,94 +294,51 @@ static inline void *usbi_reallocf(void *ptr, size_t size)
213
294
  #define TIMESPEC_TO_TIMEVAL(tv, ts) \
214
295
  do { \
215
296
  (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec; \
216
- (tv)->tv_usec = (ts)->tv_nsec / 1000; \
297
+ (tv)->tv_usec = (ts)->tv_nsec / 1000L; \
217
298
  } while (0)
218
299
  #endif
219
300
 
220
301
  #ifdef ENABLE_LOGGING
221
302
 
222
303
  #if defined(_MSC_VER) && (_MSC_VER < 1900)
304
+ #include <stdio.h>
223
305
  #define snprintf usbi_snprintf
224
306
  #define vsnprintf usbi_vsnprintf
225
307
  int usbi_snprintf(char *dst, size_t size, const char *format, ...);
226
- int usbi_vsnprintf(char *dst, size_t size, const char *format, va_list ap);
308
+ int usbi_vsnprintf(char *dst, size_t size, const char *format, va_list args);
227
309
  #define LIBUSB_PRINTF_WIN32
228
310
  #endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
229
311
 
230
312
  void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
231
- const char *function, const char *format, ...) USBI_PRINTFLIKE(4, 5);
232
-
233
- void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
234
- const char *function, const char *format, va_list args) USBI_PRINTFLIKE(4, 0);
235
-
236
- #if !defined(_MSC_VER) || (_MSC_VER >= 1400)
237
-
238
- #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
313
+ const char *function, const char *format, ...) PRINTF_FORMAT(4, 5);
239
314
 
240
- #define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
241
- #define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
242
- #define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
243
- #define usbi_dbg(...) _usbi_log(NULL, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
244
-
245
- #else /* !defined(_MSC_VER) || (_MSC_VER >= 1400) */
246
-
247
- #define LOG_BODY(ctxt, level) \
248
- { \
249
- va_list args; \
250
- va_start(args, format); \
251
- usbi_log_v(ctxt, level, "", format, args); \
252
- va_end(args); \
253
- }
315
+ #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
254
316
 
255
- static inline void usbi_err(struct libusb_context *ctx, const char *format, ...)
256
- LOG_BODY(ctx, LIBUSB_LOG_LEVEL_ERROR)
257
- static inline void usbi_warn(struct libusb_context *ctx, const char *format, ...)
258
- LOG_BODY(ctx, LIBUSB_LOG_LEVEL_WARNING)
259
- static inline void usbi_info(struct libusb_context *ctx, const char *format, ...)
260
- LOG_BODY(ctx, LIBUSB_LOG_LEVEL_INFO)
261
- static inline void usbi_dbg(const char *format, ...)
262
- LOG_BODY(NULL, LIBUSB_LOG_LEVEL_DEBUG)
263
-
264
- #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1400) */
317
+ #define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
318
+ #define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
319
+ #define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
320
+ #define usbi_dbg(ctx ,...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
265
321
 
266
322
  #else /* ENABLE_LOGGING */
267
323
 
268
- #define usbi_err(ctx, ...) do { (void)ctx; } while (0)
269
- #define usbi_warn(ctx, ...) do { (void)ctx; } while (0)
270
- #define usbi_info(ctx, ...) do { (void)ctx; } while (0)
271
- #define usbi_dbg(...) do {} while (0)
324
+ #define usbi_err(ctx, ...) UNUSED(ctx)
325
+ #define usbi_warn(ctx, ...) UNUSED(ctx)
326
+ #define usbi_info(ctx, ...) UNUSED(ctx)
327
+ #define usbi_dbg(ctx, ...) do {} while (0)
272
328
 
273
329
  #endif /* ENABLE_LOGGING */
274
330
 
275
- #define USBI_GET_CONTEXT(ctx) \
276
- do { \
277
- if (!(ctx)) \
278
- (ctx) = usbi_default_context; \
279
- } while(0)
280
-
281
331
  #define DEVICE_CTX(dev) ((dev)->ctx)
282
332
  #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
283
333
  #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
284
- #define ITRANSFER_CTX(transfer) \
285
- (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
334
+ #define ITRANSFER_CTX(itransfer) \
335
+ (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer)))
286
336
 
287
337
  #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
288
338
  #define IS_EPOUT(ep) (!IS_EPIN(ep))
289
339
  #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
290
340
  #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
291
341
 
292
- /* Internal abstraction for thread synchronization */
293
- #if defined(THREADS_POSIX)
294
- #include "os/threads_posix.h"
295
- #elif defined(OS_WINDOWS) || defined(OS_WINCE)
296
- #include "os/threads_windows.h"
297
- #endif
298
-
299
- extern struct libusb_context *usbi_default_context;
300
-
301
- /* Forward declaration for use in context (fully defined inside poll abstraction) */
302
- struct pollfd;
303
-
304
342
  struct libusb_context {
305
343
  #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
306
344
  enum libusb_log_level debug;
@@ -308,8 +346,14 @@ struct libusb_context {
308
346
  libusb_log_cb log_handler;
309
347
  #endif
310
348
 
311
- /* internal event pipe, used for signalling occurrence of an internal event. */
312
- int event_pipe[2];
349
+ /* used for signalling occurrence of an internal event. */
350
+ usbi_event_t event;
351
+
352
+ #ifdef HAVE_OS_TIMER
353
+ /* used for timeout handling, if supported by OS.
354
+ * this timer is maintained to trigger on the next pending timeout */
355
+ usbi_timer_t timer;
356
+ #endif
313
357
 
314
358
  struct list_head usb_devs;
315
359
  usbi_mutex_t usb_devs_lock;
@@ -324,6 +368,9 @@ struct libusb_context {
324
368
  libusb_hotplug_callback_handle next_hotplug_cb_handle;
325
369
  usbi_mutex_t hotplug_cbs_lock;
326
370
 
371
+ /* A flag to indicate that the context is ready for hotplug notifications */
372
+ usbi_atomic_t hotplug_ready;
373
+
327
374
  /* this is a list of in-flight transfer handles, sorted by timeout
328
375
  * expiration. URBs to timeout the soonest are placed at the beginning of
329
376
  * the list, URBs that will time out later are placed after, and urbs with
@@ -333,10 +380,12 @@ struct libusb_context {
333
380
  * take this lock first */
334
381
  usbi_mutex_t flying_transfers_lock;
335
382
 
383
+ #if !defined(PLATFORM_WINDOWS)
336
384
  /* user callbacks for pollfd changes */
337
385
  libusb_pollfd_added_cb fd_added_cb;
338
386
  libusb_pollfd_removed_cb fd_removed_cb;
339
387
  void *fd_cb_user_data;
388
+ #endif
340
389
 
341
390
  /* ensures that only one thread is handling events at any one time */
342
391
  usbi_mutex_t events_lock;
@@ -364,14 +413,17 @@ struct libusb_context {
364
413
  * in order to safely close a device. Protected by event_data_lock. */
365
414
  unsigned int device_close;
366
415
 
367
- /* list and count of poll fds and an array of poll fd structures that is
368
- * (re)allocated as necessary prior to polling. Protected by event_data_lock. */
369
- struct list_head ipollfds;
370
- /* list of pollfds that have been removed. keeps track of pollfd changes
371
- * between the poll call and */
372
- struct list_head removed_ipollfds;
373
- struct pollfd *pollfds;
374
- POLL_NFDS_TYPE pollfds_cnt;
416
+ /* A list of currently active event sources. Protected by event_data_lock. */
417
+ struct list_head event_sources;
418
+
419
+ /* A list of event sources that have been removed since the last time
420
+ * event sources were waited on. Protected by event_data_lock. */
421
+ struct list_head removed_event_sources;
422
+
423
+ /* A pointer and count to platform-specific data used for monitoring event
424
+ * sources. Only accessed during event handling. */
425
+ void *event_data;
426
+ unsigned int event_data_cnt;
375
427
 
376
428
  /* A list of pending hotplug messages. Protected by event_data_lock. */
377
429
  struct list_head hotplug_msgs;
@@ -379,71 +431,71 @@ struct libusb_context {
379
431
  /* A list of pending completed transfers. Protected by event_data_lock. */
380
432
  struct list_head completed_transfers;
381
433
 
382
- #ifdef USBI_TIMERFD_AVAILABLE
383
- /* used for timeout handling, if supported by OS.
384
- * this timerfd is maintained to trigger on the next pending timeout */
385
- int timerfd;
386
- #endif
387
-
388
434
  struct list_head list;
389
-
390
- PTR_ALIGNED unsigned char os_priv[ZERO_SIZED_ARRAY];
391
435
  };
392
436
 
437
+ extern struct libusb_context *usbi_default_context;
438
+
439
+ extern struct list_head active_contexts_list;
440
+ extern usbi_mutex_static_t active_contexts_lock;
441
+
442
+ static inline struct libusb_context *usbi_get_context(struct libusb_context *ctx)
443
+ {
444
+ return ctx ? ctx : usbi_default_context;
445
+ }
446
+
393
447
  enum usbi_event_flags {
394
- /* The list of pollfds has been modified */
395
- USBI_EVENT_POLLFDS_MODIFIED = 1U << 0,
448
+ /* The list of event sources has been modified */
449
+ USBI_EVENT_EVENT_SOURCES_MODIFIED = 1U << 0,
396
450
 
397
451
  /* The user has interrupted the event handler */
398
452
  USBI_EVENT_USER_INTERRUPT = 1U << 1,
399
453
 
400
454
  /* A hotplug callback deregistration is pending */
401
455
  USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2,
402
- };
403
456
 
404
- /* Macros for managing event handling state */
405
- #define usbi_handling_events(ctx) \
406
- (usbi_tls_key_get((ctx)->event_handling_key) != NULL)
457
+ /* One or more hotplug messages are pending */
458
+ USBI_EVENT_HOTPLUG_MSG_PENDING = 1U << 3,
407
459
 
408
- #define usbi_start_event_handling(ctx) \
409
- usbi_tls_key_set((ctx)->event_handling_key, ctx)
460
+ /* One or more completed transfers are pending */
461
+ USBI_EVENT_TRANSFER_COMPLETED = 1U << 4,
410
462
 
411
- #define usbi_end_event_handling(ctx) \
412
- usbi_tls_key_set((ctx)->event_handling_key, NULL)
463
+ /* A device is in the process of being closed */
464
+ USBI_EVENT_DEVICE_CLOSE = 1U << 5,
465
+ };
413
466
 
414
- /* Update the following macro if new event sources are added */
415
- #define usbi_pending_events(ctx) \
416
- ((ctx)->event_flags || (ctx)->device_close \
417
- || !list_empty(&(ctx)->hotplug_msgs) || !list_empty(&(ctx)->completed_transfers))
467
+ /* Macros for managing event handling state */
468
+ static inline int usbi_handling_events(struct libusb_context *ctx)
469
+ {
470
+ return usbi_tls_key_get(ctx->event_handling_key) != NULL;
471
+ }
418
472
 
419
- #ifdef USBI_TIMERFD_AVAILABLE
420
- #define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
421
- #else
422
- #define usbi_using_timerfd(ctx) (0)
423
- #endif
473
+ static inline void usbi_start_event_handling(struct libusb_context *ctx)
474
+ {
475
+ usbi_tls_key_set(ctx->event_handling_key, ctx);
476
+ }
477
+
478
+ static inline void usbi_end_event_handling(struct libusb_context *ctx)
479
+ {
480
+ usbi_tls_key_set(ctx->event_handling_key, NULL);
481
+ }
424
482
 
425
483
  struct libusb_device {
426
- /* lock protects refcnt, everything else is finalized at initialization
427
- * time */
428
- usbi_mutex_t lock;
429
- int refcnt;
484
+ usbi_atomic_t refcnt;
430
485
 
431
486
  struct libusb_context *ctx;
487
+ struct libusb_device *parent_dev;
432
488
 
433
489
  uint8_t bus_number;
434
490
  uint8_t port_number;
435
- struct libusb_device* parent_dev;
436
491
  uint8_t device_address;
437
- uint8_t num_configurations;
438
492
  enum libusb_speed speed;
439
493
 
440
494
  struct list_head list;
441
495
  unsigned long session_data;
442
496
 
443
497
  struct libusb_device_descriptor device_descriptor;
444
- int attached;
445
-
446
- PTR_ALIGNED unsigned char os_priv[ZERO_SIZED_ARRAY];
498
+ usbi_atomic_t attached;
447
499
  };
448
500
 
449
501
  struct libusb_device_handle {
@@ -454,37 +506,61 @@ struct libusb_device_handle {
454
506
  struct list_head list;
455
507
  struct libusb_device *dev;
456
508
  int auto_detach_kernel_driver;
457
-
458
- PTR_ALIGNED unsigned char os_priv[ZERO_SIZED_ARRAY];
459
509
  };
460
510
 
461
- enum {
462
- USBI_CLOCK_MONOTONIC,
463
- USBI_CLOCK_REALTIME
464
- };
511
+ /* Function called by backend during device initialization to convert
512
+ * multi-byte fields in the device descriptor to host-endian format.
513
+ */
514
+ static inline void usbi_localize_device_descriptor(struct libusb_device_descriptor *desc)
515
+ {
516
+ desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB);
517
+ desc->idVendor = libusb_le16_to_cpu(desc->idVendor);
518
+ desc->idProduct = libusb_le16_to_cpu(desc->idProduct);
519
+ desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice);
520
+ }
521
+
522
+ #ifdef HAVE_CLOCK_GETTIME
523
+ static inline void usbi_get_monotonic_time(struct timespec *tp)
524
+ {
525
+ ASSERT_EQ(clock_gettime(CLOCK_MONOTONIC, tp), 0);
526
+ }
527
+ static inline void usbi_get_real_time(struct timespec *tp)
528
+ {
529
+ ASSERT_EQ(clock_gettime(CLOCK_REALTIME, tp), 0);
530
+ }
531
+ #else
532
+ /* If the platform doesn't provide the clock_gettime() function, the backend
533
+ * must provide its own clock implementations. Two clock functions are
534
+ * required:
535
+ *
536
+ * usbi_get_monotonic_time(): returns the time since an unspecified starting
537
+ * point (usually boot) that is monotonically
538
+ * increasing.
539
+ * usbi_get_real_time(): returns the time since system epoch.
540
+ */
541
+ void usbi_get_monotonic_time(struct timespec *tp);
542
+ void usbi_get_real_time(struct timespec *tp);
543
+ #endif
465
544
 
466
545
  /* in-memory transfer layout:
467
546
  *
468
- * 1. struct usbi_transfer
469
- * 2. struct libusb_transfer (which includes iso packets) [variable size]
470
- * 3. os private data [variable size]
547
+ * 1. os private data
548
+ * 2. struct usbi_transfer
549
+ * 3. struct libusb_transfer (which includes iso packets) [variable size]
471
550
  *
472
551
  * from a libusb_transfer, you can get the usbi_transfer by rewinding the
473
552
  * appropriate number of bytes.
474
- * the usbi_transfer includes the number of allocated packets, so you can
475
- * determine the size of the transfer and hence the start and length of the
476
- * OS-private data.
477
553
  */
478
554
 
479
555
  struct usbi_transfer {
480
556
  int num_iso_packets;
481
557
  struct list_head list;
482
558
  struct list_head completed_list;
483
- struct timeval timeout;
559
+ struct timespec timeout;
484
560
  int transferred;
485
561
  uint32_t stream_id;
486
- uint8_t state_flags; /* Protected by usbi_transfer->lock */
487
- uint8_t timeout_flags; /* Protected by the flying_stransfers_lock */
562
+ uint32_t state_flags; /* Protected by usbi_transfer->lock */
563
+ uint32_t timeout_flags; /* Protected by the flying_stransfers_lock */
488
564
 
489
565
  /* this lock is held during libusb_submit_transfer() and
490
566
  * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
@@ -496,6 +572,8 @@ struct usbi_transfer {
496
572
  * Note paths taking both this and the flying_transfers_lock must
497
573
  * always take the flying_transfers_lock first */
498
574
  usbi_mutex_t lock;
575
+
576
+ void *priv;
499
577
  };
500
578
 
501
579
  enum usbi_transfer_state_flags {
@@ -520,32 +598,169 @@ enum usbi_transfer_timeout_flags {
520
598
  USBI_TRANSFER_TIMED_OUT = 1U << 2,
521
599
  };
522
600
 
523
- #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
524
- ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
525
- + sizeof(struct usbi_transfer)))
526
- #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
527
- ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
528
- - sizeof(struct usbi_transfer)))
601
+ #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer) \
602
+ ((struct libusb_transfer *) \
603
+ ((unsigned char *)(itransfer) \
604
+ + PTR_ALIGN(sizeof(struct usbi_transfer))))
605
+ #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
606
+ ((struct usbi_transfer *) \
607
+ ((unsigned char *)(transfer) \
608
+ - PTR_ALIGN(sizeof(struct usbi_transfer))))
609
+
610
+ #ifdef _MSC_VER
611
+ #pragma pack(push, 1)
612
+ #endif
529
613
 
530
- static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
531
- {
532
- assert(transfer->num_iso_packets >= 0);
533
- return ((unsigned char *)transfer) + sizeof(struct usbi_transfer)
534
- + sizeof(struct libusb_transfer)
535
- + ((size_t)transfer->num_iso_packets
536
- * sizeof(struct libusb_iso_packet_descriptor));
537
- }
614
+ /* All standard descriptors have these 2 fields in common */
615
+ struct usbi_descriptor_header {
616
+ uint8_t bLength;
617
+ uint8_t bDescriptorType;
618
+ } LIBUSB_PACKED;
619
+
620
+ struct usbi_device_descriptor {
621
+ uint8_t bLength;
622
+ uint8_t bDescriptorType;
623
+ uint16_t bcdUSB;
624
+ uint8_t bDeviceClass;
625
+ uint8_t bDeviceSubClass;
626
+ uint8_t bDeviceProtocol;
627
+ uint8_t bMaxPacketSize0;
628
+ uint16_t idVendor;
629
+ uint16_t idProduct;
630
+ uint16_t bcdDevice;
631
+ uint8_t iManufacturer;
632
+ uint8_t iProduct;
633
+ uint8_t iSerialNumber;
634
+ uint8_t bNumConfigurations;
635
+ } LIBUSB_PACKED;
636
+
637
+ struct usbi_configuration_descriptor {
638
+ uint8_t bLength;
639
+ uint8_t bDescriptorType;
640
+ uint16_t wTotalLength;
641
+ uint8_t bNumInterfaces;
642
+ uint8_t bConfigurationValue;
643
+ uint8_t iConfiguration;
644
+ uint8_t bmAttributes;
645
+ uint8_t bMaxPower;
646
+ } LIBUSB_PACKED;
647
+
648
+ struct usbi_interface_descriptor {
649
+ uint8_t bLength;
650
+ uint8_t bDescriptorType;
651
+ uint8_t bInterfaceNumber;
652
+ uint8_t bAlternateSetting;
653
+ uint8_t bNumEndpoints;
654
+ uint8_t bInterfaceClass;
655
+ uint8_t bInterfaceSubClass;
656
+ uint8_t bInterfaceProtocol;
657
+ uint8_t iInterface;
658
+ } LIBUSB_PACKED;
659
+
660
+ struct usbi_string_descriptor {
661
+ uint8_t bLength;
662
+ uint8_t bDescriptorType;
663
+ uint16_t wData[ZERO_SIZED_ARRAY];
664
+ } LIBUSB_PACKED;
665
+
666
+ struct usbi_bos_descriptor {
667
+ uint8_t bLength;
668
+ uint8_t bDescriptorType;
669
+ uint16_t wTotalLength;
670
+ uint8_t bNumDeviceCaps;
671
+ } LIBUSB_PACKED;
672
+
673
+ #ifdef _MSC_VER
674
+ #pragma pack(pop)
675
+ #endif
538
676
 
539
- /* bus structures */
677
+ union usbi_config_desc_buf {
678
+ struct usbi_configuration_descriptor desc;
679
+ uint8_t buf[LIBUSB_DT_CONFIG_SIZE];
680
+ uint16_t align; /* Force 2-byte alignment */
681
+ };
540
682
 
541
- /* All standard descriptors have these 2 fields in common */
542
- struct usb_descriptor_header {
543
- uint8_t bLength;
544
- uint8_t bDescriptorType;
683
+ union usbi_string_desc_buf {
684
+ struct usbi_string_descriptor desc;
685
+ uint8_t buf[255]; /* Some devices choke on size > 255 */
686
+ uint16_t align; /* Force 2-byte alignment */
687
+ };
688
+
689
+ union usbi_bos_desc_buf {
690
+ struct usbi_bos_descriptor desc;
691
+ uint8_t buf[LIBUSB_DT_BOS_SIZE];
692
+ uint16_t align; /* Force 2-byte alignment */
693
+ };
694
+
695
+ enum usbi_hotplug_flags {
696
+ /* This callback is interested in device arrivals */
697
+ USBI_HOTPLUG_DEVICE_ARRIVED = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
698
+
699
+ /* This callback is interested in device removals */
700
+ USBI_HOTPLUG_DEVICE_LEFT = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
701
+
702
+ /* IMPORTANT: The values for the below entries must start *after*
703
+ * the highest value of the above entries!!!
704
+ */
705
+
706
+ /* The vendor_id field is valid for matching */
707
+ USBI_HOTPLUG_VENDOR_ID_VALID = (1U << 3),
708
+
709
+ /* The product_id field is valid for matching */
710
+ USBI_HOTPLUG_PRODUCT_ID_VALID = (1U << 4),
711
+
712
+ /* The dev_class field is valid for matching */
713
+ USBI_HOTPLUG_DEV_CLASS_VALID = (1U << 5),
714
+
715
+ /* This callback has been unregistered and needs to be freed */
716
+ USBI_HOTPLUG_NEEDS_FREE = (1U << 6),
717
+ };
718
+
719
+ struct usbi_hotplug_callback {
720
+ /* Flags that control how this callback behaves */
721
+ uint8_t flags;
722
+
723
+ /* Vendor ID to match (if flags says this is valid) */
724
+ uint16_t vendor_id;
725
+
726
+ /* Product ID to match (if flags says this is valid) */
727
+ uint16_t product_id;
728
+
729
+ /* Device class to match (if flags says this is valid) */
730
+ uint8_t dev_class;
731
+
732
+ /* Callback function to invoke for matching event/device */
733
+ libusb_hotplug_callback_fn cb;
734
+
735
+ /* Handle for this callback (used to match on deregister) */
736
+ libusb_hotplug_callback_handle handle;
737
+
738
+ /* User data that will be passed to the callback function */
739
+ void *user_data;
740
+
741
+ /* List this callback is registered in (ctx->hotplug_cbs) */
742
+ struct list_head list;
743
+ };
744
+
745
+ struct usbi_hotplug_message {
746
+ /* The hotplug event that occurred */
747
+ libusb_hotplug_event event;
748
+
749
+ /* The device for which this hotplug event occurred */
750
+ struct libusb_device *device;
751
+
752
+ /* List this message is contained in (ctx->hotplug_msgs) */
753
+ struct list_head list;
545
754
  };
546
755
 
547
756
  /* shared data and functions */
548
757
 
758
+ void usbi_hotplug_init(struct libusb_context *ctx);
759
+ void usbi_hotplug_exit(struct libusb_context *ctx);
760
+ void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
761
+ libusb_hotplug_event event);
762
+ void usbi_hotplug_process(struct libusb_context *ctx, struct list_head *hotplug_msgs);
763
+
549
764
  int usbi_io_init(struct libusb_context *ctx);
550
765
  void usbi_io_exit(struct libusb_context *ctx);
551
766
 
@@ -558,39 +773,95 @@ void usbi_handle_disconnect(struct libusb_device_handle *dev_handle);
558
773
 
559
774
  int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
560
775
  enum libusb_transfer_status status);
561
- int usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
562
- void usbi_signal_transfer_completion(struct usbi_transfer *transfer);
563
-
564
- int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
565
- void *dest, int host_endian);
566
- int usbi_device_cache_descriptor(libusb_device *dev);
567
- int usbi_get_config_index_by_value(struct libusb_device *dev,
568
- uint8_t bConfigurationValue, int *idx);
569
-
570
- void usbi_connect_device (struct libusb_device *dev);
571
- void usbi_disconnect_device (struct libusb_device *dev);
572
-
573
- int usbi_signal_event(struct libusb_context *ctx);
574
- int usbi_clear_event(struct libusb_context *ctx);
575
-
576
- /* Internal abstraction for poll (needs struct usbi_transfer on Windows) */
577
- #if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
578
- defined(OS_HAIKU) || defined(OS_SUNOS)
579
- #include <unistd.h>
580
- #include "os/poll_posix.h"
581
- #elif defined(OS_WINDOWS) || defined(OS_WINCE)
582
- #include "os/poll_windows.h"
583
- #endif
776
+ int usbi_handle_transfer_cancellation(struct usbi_transfer *itransfer);
777
+ void usbi_signal_transfer_completion(struct usbi_transfer *itransfer);
584
778
 
585
- struct usbi_pollfd {
586
- /* must come first */
587
- struct libusb_pollfd pollfd;
779
+ void usbi_connect_device(struct libusb_device *dev);
780
+ void usbi_disconnect_device(struct libusb_device *dev);
588
781
 
782
+ struct usbi_event_source {
783
+ struct usbi_event_source_data {
784
+ usbi_os_handle_t os_handle;
785
+ short poll_events;
786
+ } data;
589
787
  struct list_head list;
590
788
  };
591
789
 
592
- int usbi_add_pollfd(struct libusb_context *ctx, int fd, short events);
593
- void usbi_remove_pollfd(struct libusb_context *ctx, int fd);
790
+ int usbi_add_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle,
791
+ short poll_events);
792
+ void usbi_remove_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle);
793
+
794
+ struct usbi_option {
795
+ int is_set;
796
+ union {
797
+ int ival;
798
+ } arg;
799
+ };
800
+
801
+ /* OS event abstraction */
802
+
803
+ int usbi_create_event(usbi_event_t *event);
804
+ void usbi_destroy_event(usbi_event_t *event);
805
+ void usbi_signal_event(usbi_event_t *event);
806
+ void usbi_clear_event(usbi_event_t *event);
807
+
808
+ #ifdef HAVE_OS_TIMER
809
+ int usbi_create_timer(usbi_timer_t *timer);
810
+ void usbi_destroy_timer(usbi_timer_t *timer);
811
+ int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout);
812
+ int usbi_disarm_timer(usbi_timer_t *timer);
813
+ #endif
814
+
815
+ static inline int usbi_using_timer(struct libusb_context *ctx)
816
+ {
817
+ #ifdef HAVE_OS_TIMER
818
+ return usbi_timer_valid(&ctx->timer);
819
+ #else
820
+ UNUSED(ctx);
821
+ return 0;
822
+ #endif
823
+ }
824
+
825
+ struct usbi_reported_events {
826
+ union {
827
+ struct {
828
+ unsigned int event_triggered:1;
829
+ #ifdef HAVE_OS_TIMER
830
+ unsigned int timer_triggered:1;
831
+ #endif
832
+ };
833
+ unsigned int event_bits;
834
+ };
835
+ void *event_data;
836
+ unsigned int event_data_count;
837
+ unsigned int num_ready;
838
+ };
839
+
840
+ int usbi_alloc_event_data(struct libusb_context *ctx);
841
+ int usbi_wait_for_events(struct libusb_context *ctx,
842
+ struct usbi_reported_events *reported_events, int timeout_ms);
843
+
844
+ /* accessor functions for structure private data */
845
+
846
+ static inline void *usbi_get_context_priv(struct libusb_context *ctx)
847
+ {
848
+ return (unsigned char *)ctx + PTR_ALIGN(sizeof(*ctx));
849
+ }
850
+
851
+ static inline void *usbi_get_device_priv(struct libusb_device *dev)
852
+ {
853
+ return (unsigned char *)dev + PTR_ALIGN(sizeof(*dev));
854
+ }
855
+
856
+ static inline void *usbi_get_device_handle_priv(struct libusb_device_handle *dev_handle)
857
+ {
858
+ return (unsigned char *)dev_handle + PTR_ALIGN(sizeof(*dev_handle));
859
+ }
860
+
861
+ static inline void *usbi_get_transfer_priv(struct usbi_transfer *itransfer)
862
+ {
863
+ return itransfer->priv;
864
+ }
594
865
 
595
866
  /* device discovery */
596
867
 
@@ -624,7 +895,8 @@ struct usbi_os_backend {
624
895
  * data structures for later, etc.
625
896
  *
626
897
  * This function is called when a libusb user initializes the library
627
- * prior to use.
898
+ * prior to use. Mutual exclusion with other init and exit calls is
899
+ * guaranteed when this function is called.
628
900
  *
629
901
  * Return 0 on success, or a LIBUSB_ERROR code on failure.
630
902
  */
@@ -634,6 +906,8 @@ struct usbi_os_backend {
634
906
  * that was set up by init.
635
907
  *
636
908
  * This function is called when the user deinitializes the library.
909
+ * Mutual exclusion with other init and exit calls is guaranteed when
910
+ * this function is called.
637
911
  */
638
912
  void (*exit)(struct libusb_context *ctx);
639
913
 
@@ -725,7 +999,7 @@ struct usbi_os_backend {
725
999
  * and other operations so that those operations can happen (hopefully)
726
1000
  * without hiccup. This is also a good place to inform libusb that it
727
1001
  * should monitor certain file descriptors related to this device -
728
- * see the usbi_add_pollfd() function.
1002
+ * see the usbi_add_event_source() function.
729
1003
  *
730
1004
  * Your backend should also initialize the device structure
731
1005
  * (dev_handle->dev), which is NULL at the beginning of the call.
@@ -754,7 +1028,7 @@ struct usbi_os_backend {
754
1028
  * and other operations so that those operations can happen (hopefully)
755
1029
  * without hiccup. This is also a good place to inform libusb that it
756
1030
  * should monitor certain file descriptors related to this device -
757
- * see the usbi_add_pollfd() function.
1031
+ * see the usbi_add_event_source() function.
758
1032
  *
759
1033
  * This function should not generate any bus I/O and should not block.
760
1034
  *
@@ -775,38 +1049,14 @@ struct usbi_os_backend {
775
1049
 
776
1050
  /* Close a device such that the handle cannot be used again. Your backend
777
1051
  * should destroy any resources that were allocated in the open path.
778
- * This may also be a good place to call usbi_remove_pollfd() to inform
779
- * libusb of any file descriptors associated with this device that should
780
- * no longer be monitored.
1052
+ * This may also be a good place to call usbi_remove_event_source() to
1053
+ * inform libusb of any event sources associated with this device that
1054
+ * should no longer be monitored.
781
1055
  *
782
1056
  * This function is called when the user closes a device handle.
783
1057
  */
784
1058
  void (*close)(struct libusb_device_handle *dev_handle);
785
1059
 
786
- /* Retrieve the device descriptor from a device.
787
- *
788
- * The descriptor should be retrieved from memory, NOT via bus I/O to the
789
- * device. This means that you may have to cache it in a private structure
790
- * during get_device_list enumeration. Alternatively, you may be able
791
- * to retrieve it from a kernel interface (some Linux setups can do this)
792
- * still without generating bus I/O.
793
- *
794
- * This function is expected to write DEVICE_DESC_LENGTH (18) bytes into
795
- * buffer, which is guaranteed to be big enough.
796
- *
797
- * This function is called when sanity-checking a device before adding
798
- * it to the list of discovered devices, and also when the user requests
799
- * to read the device descriptor.
800
- *
801
- * This function is expected to return the descriptor in bus-endian format
802
- * (LE). If it returns the multi-byte values in host-endian format,
803
- * set the host_endian output parameter to "1".
804
- *
805
- * Return 0 on success or a LIBUSB_ERROR code on failure.
806
- */
807
- int (*get_device_descriptor)(struct libusb_device *device,
808
- unsigned char *buffer, int *host_endian);
809
-
810
1060
  /* Get the ACTIVE configuration descriptor for a device.
811
1061
  *
812
1062
  * The descriptor should be retrieved from memory, NOT via bus I/O to the
@@ -819,8 +1069,7 @@ struct usbi_os_backend {
819
1069
  * return an error code.
820
1070
  *
821
1071
  * This function is expected to return the descriptor in bus-endian format
822
- * (LE). If it returns the multi-byte values in host-endian format,
823
- * set the host_endian output parameter to "1".
1072
+ * (LE).
824
1073
  *
825
1074
  * Return:
826
1075
  * - 0 on success
@@ -828,7 +1077,7 @@ struct usbi_os_backend {
828
1077
  * - another LIBUSB_ERROR code on other failure
829
1078
  */
830
1079
  int (*get_active_config_descriptor)(struct libusb_device *device,
831
- unsigned char *buffer, size_t len, int *host_endian);
1080
+ void *buffer, size_t len);
832
1081
 
833
1082
  /* Get a specific configuration descriptor for a device.
834
1083
  *
@@ -846,14 +1095,12 @@ struct usbi_os_backend {
846
1095
  * return an error code.
847
1096
  *
848
1097
  * This function is expected to return the descriptor in bus-endian format
849
- * (LE). If it returns the multi-byte values in host-endian format,
850
- * set the host_endian output parameter to "1".
1098
+ * (LE).
851
1099
  *
852
1100
  * Return the length read on success or a LIBUSB_ERROR code on failure.
853
1101
  */
854
1102
  int (*get_config_descriptor)(struct libusb_device *device,
855
- uint8_t config_index, unsigned char *buffer, size_t len,
856
- int *host_endian);
1103
+ uint8_t config_index, void *buffer, size_t len);
857
1104
 
858
1105
  /* Like get_config_descriptor but then by bConfigurationValue instead
859
1106
  * of by index.
@@ -868,8 +1115,7 @@ struct usbi_os_backend {
868
1115
  * or a LIBUSB_ERROR code on failure.
869
1116
  */
870
1117
  int (*get_config_descriptor_by_value)(struct libusb_device *device,
871
- uint8_t bConfigurationValue, unsigned char **buffer,
872
- int *host_endian);
1118
+ uint8_t bConfigurationValue, void **buffer);
873
1119
 
874
1120
  /* Get the bConfigurationValue for the active configuration for a device.
875
1121
  * Optional. This should only be implemented if you can retrieve it from
@@ -888,7 +1134,7 @@ struct usbi_os_backend {
888
1134
  * blocking
889
1135
  * - another LIBUSB_ERROR code on other failure.
890
1136
  */
891
- int (*get_configuration)(struct libusb_device_handle *dev_handle, int *config);
1137
+ int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config);
892
1138
 
893
1139
  /* Set the active configuration for a device.
894
1140
  *
@@ -924,7 +1170,7 @@ struct usbi_os_backend {
924
1170
  * was opened
925
1171
  * - another LIBUSB_ERROR code on other failure
926
1172
  */
927
- int (*claim_interface)(struct libusb_device_handle *dev_handle, int interface_number);
1173
+ int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
928
1174
 
929
1175
  /* Release a previously claimed interface.
930
1176
  *
@@ -941,7 +1187,7 @@ struct usbi_os_backend {
941
1187
  * was opened
942
1188
  * - another LIBUSB_ERROR code on other failure
943
1189
  */
944
- int (*release_interface)(struct libusb_device_handle *dev_handle, int interface_number);
1190
+ int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
945
1191
 
946
1192
  /* Set the alternate setting for an interface.
947
1193
  *
@@ -958,7 +1204,7 @@ struct usbi_os_backend {
958
1204
  * - another LIBUSB_ERROR code on other failure
959
1205
  */
960
1206
  int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
961
- int interface_number, int altsetting);
1207
+ uint8_t interface_number, uint8_t altsetting);
962
1208
 
963
1209
  /* Clear a halt/stall condition on an endpoint.
964
1210
  *
@@ -974,13 +1220,13 @@ struct usbi_os_backend {
974
1220
  int (*clear_halt)(struct libusb_device_handle *dev_handle,
975
1221
  unsigned char endpoint);
976
1222
 
977
- /* Perform a USB port reset to reinitialize a device.
1223
+ /* Perform a USB port reset to reinitialize a device. Optional.
978
1224
  *
979
1225
  * If possible, the device handle should still be usable after the reset
980
1226
  * completes, assuming that the device descriptors did not change during
981
1227
  * reset and all previous interface state can be restored.
982
1228
  *
983
- * If something changes, or you cannot easily locate/verify the resetted
1229
+ * If something changes, or you cannot easily locate/verify the reset
984
1230
  * device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application
985
1231
  * to close the old handle and re-enumerate the device.
986
1232
  *
@@ -1003,12 +1249,11 @@ struct usbi_os_backend {
1003
1249
  /* Allocate persistent DMA memory for the given device, suitable for
1004
1250
  * zerocopy. May return NULL on failure. Optional to implement.
1005
1251
  */
1006
- unsigned char *(*dev_mem_alloc)(struct libusb_device_handle *handle,
1007
- size_t len);
1252
+ void *(*dev_mem_alloc)(struct libusb_device_handle *handle, size_t len);
1008
1253
 
1009
1254
  /* Free memory allocated by dev_mem_alloc. */
1010
- int (*dev_mem_free)(struct libusb_device_handle *handle,
1011
- unsigned char *buffer, size_t len);
1255
+ int (*dev_mem_free)(struct libusb_device_handle *handle, void *buffer,
1256
+ size_t len);
1012
1257
 
1013
1258
  /* Determine if a kernel driver is active on an interface. Optional.
1014
1259
  *
@@ -1023,7 +1268,7 @@ struct usbi_os_backend {
1023
1268
  * - another LIBUSB_ERROR code on other failure
1024
1269
  */
1025
1270
  int (*kernel_driver_active)(struct libusb_device_handle *dev_handle,
1026
- int interface_number);
1271
+ uint8_t interface_number);
1027
1272
 
1028
1273
  /* Detach a kernel driver from an interface. Optional.
1029
1274
  *
@@ -1039,7 +1284,7 @@ struct usbi_os_backend {
1039
1284
  * - another LIBUSB_ERROR code on other failure
1040
1285
  */
1041
1286
  int (*detach_kernel_driver)(struct libusb_device_handle *dev_handle,
1042
- int interface_number);
1287
+ uint8_t interface_number);
1043
1288
 
1044
1289
  /* Attach a kernel driver to an interface. Optional.
1045
1290
  *
@@ -1056,7 +1301,7 @@ struct usbi_os_backend {
1056
1301
  * - another LIBUSB_ERROR code on other failure
1057
1302
  */
1058
1303
  int (*attach_kernel_driver)(struct libusb_device_handle *dev_handle,
1059
- int interface_number);
1304
+ uint8_t interface_number);
1060
1305
 
1061
1306
  /* Destroy a device. Optional.
1062
1307
  *
@@ -1101,21 +1346,22 @@ struct usbi_os_backend {
1101
1346
  */
1102
1347
  void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
1103
1348
 
1104
- /* Handle any pending events on file descriptors. Optional.
1349
+ /* Handle any pending events on event sources. Optional.
1105
1350
  *
1106
- * Provide this function when file descriptors directly indicate device
1107
- * or transfer activity. If your backend does not have such file descriptors,
1351
+ * Provide this function when event sources directly indicate device
1352
+ * or transfer activity. If your backend does not have such event sources,
1108
1353
  * implement the handle_transfer_completion function below.
1109
1354
  *
1110
1355
  * This involves monitoring any active transfers and processing their
1111
1356
  * completion or cancellation.
1112
1357
  *
1113
- * The function is passed an array of pollfd structures (size nfds)
1114
- * as a result of the poll() system call. The num_ready parameter
1115
- * indicates the number of file descriptors that have reported events
1116
- * (i.e. the poll() return value). This should be enough information
1117
- * for you to determine which actions need to be taken on the currently
1118
- * active transfers.
1358
+ * The function is passed a pointer that represents platform-specific
1359
+ * data for monitoring event sources (size count). This data is to be
1360
+ * (re)allocated as necessary when event sources are modified.
1361
+ * The num_ready parameter indicates the number of event sources that
1362
+ * have reported events. This should be enough information for you to
1363
+ * determine which actions need to be taken on the currently active
1364
+ * transfers.
1119
1365
  *
1120
1366
  * For any cancelled transfers, call usbi_handle_transfer_cancellation().
1121
1367
  * For completed transfers, call usbi_handle_transfer_completion().
@@ -1134,13 +1380,13 @@ struct usbi_os_backend {
1134
1380
  * Return 0 on success, or a LIBUSB_ERROR code on failure.
1135
1381
  */
1136
1382
  int (*handle_events)(struct libusb_context *ctx,
1137
- struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
1383
+ void *event_data, unsigned int count, unsigned int num_ready);
1138
1384
 
1139
1385
  /* Handle transfer completion. Optional.
1140
1386
  *
1141
- * Provide this function when there are no file descriptors available
1142
- * that directly indicate device or transfer activity. If your backend does
1143
- * have such file descriptors, implement the handle_events function above.
1387
+ * Provide this function when there are no event sources available that
1388
+ * directly indicate device or transfer activity. If your backend does
1389
+ * have such event sources, implement the handle_events function above.
1144
1390
  *
1145
1391
  * Your backend must tell the library when a transfer has completed by
1146
1392
  * calling usbi_signal_transfer_completion(). You should store any private
@@ -1161,47 +1407,74 @@ struct usbi_os_backend {
1161
1407
  */
1162
1408
  int (*handle_transfer_completion)(struct usbi_transfer *itransfer);
1163
1409
 
1164
- /* Get time from specified clock. At least two clocks must be implemented
1165
- by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
1166
-
1167
- Description of clocks:
1168
- USBI_CLOCK_REALTIME : clock returns time since system epoch.
1169
- USBI_CLOCK_MONOTONIC: clock returns time since unspecified start
1170
- time (usually boot).
1171
- */
1172
- int (*clock_gettime)(int clkid, struct timespec *tp);
1173
-
1174
- #ifdef USBI_TIMERFD_AVAILABLE
1175
- /* clock ID of the clock that should be used for timerfd */
1176
- clockid_t (*get_timerfd_clockid)(void);
1177
- #endif
1178
-
1179
1410
  /* Number of bytes to reserve for per-context private backend data.
1180
- * This private data area is accessible through the "os_priv" field of
1181
- * struct libusb_context. */
1411
+ * This private data area is accessible by calling
1412
+ * usbi_get_context_priv() on the libusb_context instance.
1413
+ */
1182
1414
  size_t context_priv_size;
1183
1415
 
1184
1416
  /* Number of bytes to reserve for per-device private backend data.
1185
- * This private data area is accessible through the "os_priv" field of
1186
- * struct libusb_device. */
1417
+ * This private data area is accessible by calling
1418
+ * usbi_get_device_priv() on the libusb_device instance.
1419
+ */
1187
1420
  size_t device_priv_size;
1188
1421
 
1189
1422
  /* Number of bytes to reserve for per-handle private backend data.
1190
- * This private data area is accessible through the "os_priv" field of
1191
- * struct libusb_device. */
1423
+ * This private data area is accessible by calling
1424
+ * usbi_get_device_handle_priv() on the libusb_device_handle instance.
1425
+ */
1192
1426
  size_t device_handle_priv_size;
1193
1427
 
1194
1428
  /* Number of bytes to reserve for per-transfer private backend data.
1195
1429
  * This private data area is accessible by calling
1196
- * usbi_transfer_get_os_priv() on the appropriate usbi_transfer instance.
1430
+ * usbi_get_transfer_priv() on the usbi_transfer instance.
1197
1431
  */
1198
1432
  size_t transfer_priv_size;
1199
1433
  };
1200
1434
 
1201
1435
  extern const struct usbi_os_backend usbi_backend;
1202
1436
 
1203
- extern struct list_head active_contexts_list;
1204
- extern usbi_mutex_static_t active_contexts_lock;
1437
+ #define for_each_context(c) \
1438
+ for_each_helper(c, &active_contexts_list, struct libusb_context)
1439
+
1440
+ #define for_each_device(ctx, d) \
1441
+ for_each_helper(d, &(ctx)->usb_devs, struct libusb_device)
1442
+
1443
+ #define for_each_device_safe(ctx, d, n) \
1444
+ for_each_safe_helper(d, n, &(ctx)->usb_devs, struct libusb_device)
1445
+
1446
+ #define for_each_open_device(ctx, h) \
1447
+ for_each_helper(h, &(ctx)->open_devs, struct libusb_device_handle)
1448
+
1449
+ #define __for_each_transfer(list, t) \
1450
+ for_each_helper(t, (list), struct usbi_transfer)
1451
+
1452
+ #define for_each_transfer(ctx, t) \
1453
+ __for_each_transfer(&(ctx)->flying_transfers, t)
1454
+
1455
+ #define __for_each_transfer_safe(list, t, n) \
1456
+ for_each_safe_helper(t, n, (list), struct usbi_transfer)
1457
+
1458
+ #define for_each_transfer_safe(ctx, t, n) \
1459
+ __for_each_transfer_safe(&(ctx)->flying_transfers, t, n)
1460
+
1461
+ #define __for_each_completed_transfer_safe(list, t, n) \
1462
+ list_for_each_entry_safe(t, n, (list), completed_list, struct usbi_transfer)
1463
+
1464
+ #define for_each_event_source(ctx, e) \
1465
+ for_each_helper(e, &(ctx)->event_sources, struct usbi_event_source)
1466
+
1467
+ #define for_each_removed_event_source(ctx, e) \
1468
+ for_each_helper(e, &(ctx)->removed_event_sources, struct usbi_event_source)
1469
+
1470
+ #define for_each_removed_event_source_safe(ctx, e, n) \
1471
+ for_each_safe_helper(e, n, &(ctx)->removed_event_sources, struct usbi_event_source)
1472
+
1473
+ #define for_each_hotplug_cb(ctx, c) \
1474
+ for_each_helper(c, &(ctx)->hotplug_cbs, struct usbi_hotplug_callback)
1475
+
1476
+ #define for_each_hotplug_cb_safe(ctx, c, n) \
1477
+ for_each_safe_helper(c, n, &(ctx)->hotplug_cbs, struct usbi_hotplug_callback)
1205
1478
 
1206
1479
  #ifdef __cplusplus
1207
1480
  }