usb 1.7.2 → 1.8.0-libusb.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/.gitmodules +1 -1
  2. package/Readme.md +2 -2
  3. package/binding.gyp +2 -2
  4. package/libusb/.private/pre-commit.sh +7 -1
  5. package/libusb/.travis.yml +49 -0
  6. package/libusb/AUTHORS +44 -3
  7. package/libusb/Brewfile +4 -0
  8. package/libusb/ChangeLog +74 -2
  9. package/libusb/README.md +32 -0
  10. package/libusb/TODO +1 -1
  11. package/libusb/Xcode/common.xcconfig +12 -0
  12. package/libusb/Xcode/config.h +25 -0
  13. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +959 -1
  14. package/libusb/android/README +4 -2
  15. package/libusb/android/config.h +75 -0
  16. package/libusb/appveyor.yml +41 -0
  17. package/libusb/appveyor_cygwin.bat +11 -0
  18. package/libusb/appveyor_minGW.bat +19 -0
  19. package/libusb/autogen.sh +1 -1
  20. package/libusb/bootstrap.sh +3 -16
  21. package/libusb/configure.ac +108 -80
  22. package/libusb/doc/doxygen.cfg.in +1785 -739
  23. package/libusb/examples/Makefile.am +1 -1
  24. package/libusb/examples/dpfp.c +3 -1
  25. package/libusb/examples/dpfp_threaded.c +23 -10
  26. package/libusb/examples/ezusb.c +3 -3
  27. package/libusb/examples/ezusb.h +2 -2
  28. package/libusb/examples/fxload.c +31 -9
  29. package/libusb/examples/hotplugtest.c +35 -7
  30. package/libusb/examples/listdevs.c +3 -1
  31. package/libusb/examples/sam3u_benchmark.c +3 -3
  32. package/libusb/examples/testlibusb.c +277 -0
  33. package/libusb/examples/xusb.c +40 -34
  34. package/libusb/libusb/Makefile.am +49 -23
  35. package/libusb/libusb/core.c +855 -457
  36. package/libusb/libusb/descriptor.c +72 -78
  37. package/libusb/libusb/hotplug.c +122 -76
  38. package/libusb/libusb/hotplug.h +42 -25
  39. package/libusb/libusb/io.c +625 -390
  40. package/libusb/libusb/libusb-1.0.def +12 -0
  41. package/libusb/libusb/libusb.h +218 -150
  42. package/libusb/libusb/libusbi.h +346 -176
  43. package/libusb/libusb/os/darwin_usb.c +604 -319
  44. package/libusb/libusb/os/darwin_usb.h +61 -20
  45. package/libusb/libusb/os/haiku_pollfs.cpp +367 -0
  46. package/libusb/libusb/os/haiku_usb.h +113 -0
  47. package/libusb/libusb/os/haiku_usb_backend.cpp +533 -0
  48. package/libusb/libusb/os/haiku_usb_raw.cpp +267 -0
  49. package/libusb/libusb/os/haiku_usb_raw.h +188 -0
  50. package/libusb/libusb/os/linux_netlink.c +186 -146
  51. package/libusb/libusb/os/linux_udev.c +36 -14
  52. package/libusb/libusb/os/linux_usbfs.c +426 -225
  53. package/libusb/libusb/os/linux_usbfs.h +5 -3
  54. package/libusb/libusb/os/netbsd_usb.c +21 -77
  55. package/libusb/libusb/os/openbsd_usb.c +32 -115
  56. package/libusb/libusb/os/poll_posix.c +38 -5
  57. package/libusb/libusb/os/poll_posix.h +3 -0
  58. package/libusb/libusb/os/poll_windows.c +277 -626
  59. package/libusb/libusb/os/poll_windows.h +11 -44
  60. package/libusb/libusb/os/sunos_usb.c +1695 -0
  61. package/libusb/libusb/os/sunos_usb.h +80 -0
  62. package/libusb/libusb/os/threads_posix.c +24 -26
  63. package/libusb/libusb/os/threads_posix.h +73 -21
  64. package/libusb/libusb/os/threads_windows.c +71 -157
  65. package/libusb/libusb/os/threads_windows.h +68 -44
  66. package/libusb/libusb/os/wince_usb.c +276 -420
  67. package/libusb/libusb/os/wince_usb.h +23 -28
  68. package/libusb/libusb/os/windows_common.h +78 -58
  69. package/libusb/libusb/os/windows_nt_common.c +1010 -0
  70. package/libusb/libusb/os/windows_nt_common.h +110 -0
  71. package/libusb/libusb/os/windows_nt_shared_types.h +147 -0
  72. package/libusb/libusb/os/windows_usbdk.c +830 -0
  73. package/libusb/libusb/os/windows_usbdk.h +103 -0
  74. package/libusb/libusb/os/windows_winusb.c +4391 -0
  75. package/libusb/libusb/os/windows_winusb.h +783 -0
  76. package/libusb/libusb/strerror.c +41 -7
  77. package/libusb/libusb/sync.c +41 -13
  78. package/libusb/libusb/version.h +1 -1
  79. package/libusb/libusb/version_nano.h +1 -1
  80. package/libusb/libusb-1.0.pc.in +1 -1
  81. package/libusb/msvc/appveyor.bat +27 -0
  82. package/libusb/msvc/config.h +5 -4
  83. package/libusb/msvc/ddk_build.cmd +87 -43
  84. package/libusb/msvc/fxload_2010.vcxproj +24 -104
  85. package/libusb/msvc/fxload_2012.vcxproj +24 -107
  86. package/libusb/msvc/fxload_2013.vcxproj +24 -107
  87. package/libusb/msvc/fxload_2015.vcxproj +91 -0
  88. package/libusb/msvc/fxload_2017.vcxproj +114 -0
  89. package/libusb/msvc/fxload_sources +1 -1
  90. package/libusb/msvc/getopt_2010.vcxproj +16 -75
  91. package/libusb/msvc/getopt_2012.vcxproj +16 -79
  92. package/libusb/msvc/getopt_2013.vcxproj +16 -79
  93. package/libusb/msvc/getopt_2015.vcxproj +73 -0
  94. package/libusb/msvc/getopt_2017.vcxproj +98 -0
  95. package/libusb/msvc/getopt_sources +6 -2
  96. package/libusb/msvc/hotplugtest_2010.vcxproj +18 -99
  97. package/libusb/msvc/hotplugtest_2012.vcxproj +18 -102
  98. package/libusb/msvc/hotplugtest_2013.vcxproj +18 -102
  99. package/libusb/msvc/hotplugtest_2015.vcxproj +83 -0
  100. package/libusb/msvc/hotplugtest_2017.vcxproj +106 -0
  101. package/libusb/msvc/hotplugtest_sources +1 -1
  102. package/libusb/msvc/libusb_2005.sln +20 -20
  103. package/libusb/msvc/libusb_2010.sln +57 -46
  104. package/libusb/msvc/libusb_2012.sln +57 -46
  105. package/libusb/msvc/libusb_2013.sln +57 -50
  106. package/libusb/msvc/libusb_2015.sln +59 -52
  107. package/libusb/msvc/libusb_2017.sln +186 -0
  108. package/libusb/msvc/libusb_dll.dsp +2 -2
  109. package/libusb/msvc/libusb_dll_2005.vcproj +30 -2
  110. package/libusb/msvc/libusb_dll_2010.vcxproj +26 -90
  111. package/libusb/msvc/libusb_dll_2012.vcxproj +28 -96
  112. package/libusb/msvc/libusb_dll_2013.vcxproj +28 -96
  113. package/libusb/msvc/libusb_dll_2015.vcxproj +107 -0
  114. package/libusb/msvc/libusb_dll_2017.vcxproj +134 -0
  115. package/libusb/msvc/libusb_dll_wince.vcproj +9 -1
  116. package/libusb/msvc/libusb_sources +10 -5
  117. package/libusb/msvc/libusb_static.dsp +2 -2
  118. package/libusb/msvc/libusb_static_2005.vcproj +32 -4
  119. package/libusb/msvc/libusb_static_2010.vcxproj +24 -83
  120. package/libusb/msvc/libusb_static_2012.vcxproj +25 -87
  121. package/libusb/msvc/libusb_static_2013.vcxproj +25 -87
  122. package/libusb/msvc/libusb_static_2015.vcxproj +98 -0
  123. package/libusb/msvc/libusb_static_2017.vcxproj +117 -0
  124. package/libusb/msvc/libusb_static_wince.vcproj +20 -26
  125. package/libusb/msvc/libusb_wince.sln +88 -88
  126. package/libusb/msvc/listdevs_2010.vcxproj +16 -99
  127. package/libusb/msvc/listdevs_2012.vcxproj +16 -102
  128. package/libusb/msvc/listdevs_2013.vcxproj +16 -102
  129. package/libusb/msvc/listdevs_2015.vcxproj +83 -0
  130. package/libusb/msvc/listdevs_2017.vcxproj +106 -0
  131. package/libusb/msvc/listdevs_sources +2 -1
  132. package/libusb/msvc/stress_2010.vcxproj +20 -101
  133. package/libusb/msvc/stress_2012.vcxproj +20 -104
  134. package/libusb/msvc/stress_2013.vcxproj +20 -104
  135. package/libusb/msvc/stress_2015.vcxproj +87 -0
  136. package/libusb/msvc/stress_2017.vcxproj +110 -0
  137. package/libusb/msvc/stress_sources +21 -0
  138. package/libusb/msvc/testlibusb_2010.vcxproj +82 -0
  139. package/libusb/msvc/testlibusb_2012.vcxproj +83 -0
  140. package/libusb/msvc/testlibusb_2013.vcxproj +83 -0
  141. package/libusb/msvc/testlibusb_2015.vcxproj +83 -0
  142. package/libusb/msvc/testlibusb_2017.vcxproj +106 -0
  143. package/libusb/msvc/testlibusb_sources +20 -0
  144. package/libusb/msvc/xusb_2010.vcxproj +17 -98
  145. package/libusb/msvc/xusb_2012.vcxproj +17 -101
  146. package/libusb/msvc/xusb_2013.vcxproj +17 -101
  147. package/libusb/msvc/xusb_2015.vcxproj +83 -0
  148. package/libusb/msvc/xusb_2017.vcxproj +106 -0
  149. package/libusb/msvc/xusb_sources +1 -1
  150. package/libusb/tests/stress.c +2 -2
  151. package/libusb/tests/testlib.c +0 -4
  152. package/libusb/travis-autogen.sh +39 -0
  153. package/libusb.gypi +13 -2
  154. package/package.json +20 -11
  155. package/prebuilds/android-arm/node.napi.armv7.node +0 -0
  156. package/prebuilds/android-arm64/node.napi.armv8.node +0 -0
  157. package/prebuilds/darwin-x64+arm64/node.napi.node +0 -0
  158. package/prebuilds/linux-arm/node.napi.armv6.node +0 -0
  159. package/prebuilds/linux-arm/node.napi.armv7.node +0 -0
  160. package/prebuilds/linux-arm64/node.napi.armv8.node +0 -0
  161. package/prebuilds/linux-ia32/node.napi.node +0 -0
  162. package/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  163. package/prebuilds/linux-x64/node.napi.musl.node +0 -0
  164. package/prebuilds/win32-ia32/node.napi.node +0 -0
  165. package/prebuilds/win32-x64/node.napi.node +0 -0
  166. package/src/device.cc +1 -1
  167. package/usb.js +1 -1
  168. package/.github/workflows/prebuild.yml +0 -49
  169. package/libusb/INSTALL +0 -234
  170. package/libusb/README +0 -28
  171. package/libusb/libusb/os/windows_usb.c +0 -5347
  172. package/libusb/libusb/os/windows_usb.h +0 -971
  173. package/libusb/msvc/fxload_2010.vcxproj.filters +0 -25
  174. package/libusb/msvc/fxload_2012.vcxproj.filters +0 -25
  175. package/libusb/msvc/getopt_2010.vcxproj.filters +0 -26
  176. package/libusb/msvc/getopt_2012.vcxproj.filters +0 -26
  177. package/libusb/msvc/hotplugtest_2010.vcxproj.filters +0 -14
  178. package/libusb/msvc/hotplugtest_2012.vcxproj.filters +0 -14
  179. package/libusb/msvc/libusb_dll_2010.vcxproj.filters +0 -81
  180. package/libusb/msvc/libusb_dll_2012.vcxproj.filters +0 -84
  181. package/libusb/msvc/libusb_static_2010.vcxproj.filters +0 -74
  182. package/libusb/msvc/libusb_static_2012.vcxproj.filters +0 -74
  183. package/libusb/msvc/listdevs_2010.vcxproj.filters +0 -14
  184. package/libusb/msvc/listdevs_2012.vcxproj.filters +0 -14
  185. package/libusb/msvc/stress_2010.vcxproj.filters +0 -25
  186. package/libusb/msvc/stress_2012.vcxproj.filters +0 -25
  187. package/libusb/msvc/xusb_2010.vcxproj.filters +0 -14
  188. package/libusb/msvc/xusb_2012.vcxproj.filters +0 -14
@@ -2,6 +2,8 @@
2
2
  * Internal header for libusb
3
3
  * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
4
4
  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
5
+ * Copyright © 2019 Nathan Hjelm <hjelmn@cs.umm.edu>
6
+ * Copyright © 2019 Google LLC. All rights reserved.
5
7
  *
6
8
  * This library is free software; you can redistribute it and/or
7
9
  * modify it under the terms of the GNU Lesser General Public
@@ -21,10 +23,10 @@
21
23
  #ifndef LIBUSBI_H
22
24
  #define LIBUSBI_H
23
25
 
24
- #include "config.h"
26
+ #include <config.h>
25
27
 
26
28
  #include <stdlib.h>
27
-
29
+ #include <assert.h>
28
30
  #include <stddef.h>
29
31
  #include <stdint.h>
30
32
  #include <time.h>
@@ -32,13 +34,27 @@
32
34
  #ifdef HAVE_POLL_H
33
35
  #include <poll.h>
34
36
  #endif
35
-
36
37
  #ifdef HAVE_MISSING_H
37
- #include "missing.h"
38
+ #include <missing.h>
38
39
  #endif
40
+
39
41
  #include "libusb.h"
40
42
  #include "version.h"
41
43
 
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))
49
+ #else
50
+ #define PTR_ALIGNED __declspec(align(4))
51
+ #endif
52
+ #elif defined(__GNUC__) && (__GNUC__ >= 3)
53
+ #define PTR_ALIGNED __attribute__((aligned(sizeof(void *))))
54
+ #else
55
+ #define PTR_ALIGNED
56
+ #endif
57
+
42
58
  /* Inside the libusb code, mark all public functions as follows:
43
59
  * return_type API_EXPORTED function_name(params) { ... }
44
60
  * But if the function returns a pointer, mark it as follows:
@@ -48,16 +64,29 @@
48
64
  */
49
65
  #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
50
66
 
51
- #define DEVICE_DESC_LENGTH 18
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
+ #ifdef __cplusplus
78
+ extern "C" {
79
+ #endif
80
+
81
+ #define DEVICE_DESC_LENGTH 18
52
82
 
53
83
  #define USB_MAXENDPOINTS 32
54
84
  #define USB_MAXINTERFACES 32
55
85
  #define USB_MAXCONFIG 8
56
86
 
57
87
  /* Backend specific capabilities */
58
- #define USBI_CAP_HAS_HID_ACCESS 0x00010000
88
+ #define USBI_CAP_HAS_HID_ACCESS 0x00010000
59
89
  #define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000
60
- #define USBI_CAP_HAS_POLLABLE_DEVICE_FD 0x00040000
61
90
 
62
91
  /* Maximum number of bytes in a log line */
63
92
  #define USBI_MAX_LOG_LEN 1024
@@ -65,10 +94,10 @@
65
94
  #define USBI_LOG_LINE_END "\n"
66
95
 
67
96
  /* The following is used to silence warnings for unused variables */
68
- #define UNUSED(var) do { (void)(var); } while(0)
97
+ #define UNUSED(var) do { (void)(var); } while(0)
69
98
 
70
99
  #if !defined(ARRAYSIZE)
71
- #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
100
+ #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
72
101
  #endif
73
102
 
74
103
  struct list_head {
@@ -83,6 +112,9 @@ struct list_head {
83
112
  #define list_entry(ptr, type, member) \
84
113
  ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
85
114
 
115
+ #define list_first_entry(ptr, type, member) \
116
+ list_entry((ptr)->next, type, member)
117
+
86
118
  /* Get each entry from a list
87
119
  * pos - A structure pointer has a "member" element
88
120
  * head - list head
@@ -90,14 +122,14 @@ struct list_head {
90
122
  * type - the type of the first parameter
91
123
  */
92
124
  #define list_for_each_entry(pos, head, member, type) \
93
- for (pos = list_entry((head)->next, type, member); \
94
- &pos->member != (head); \
125
+ for (pos = list_entry((head)->next, type, member); \
126
+ &pos->member != (head); \
95
127
  pos = list_entry(pos->member.next, type, member))
96
128
 
97
- #define list_for_each_entry_safe(pos, n, head, member, type) \
98
- for (pos = list_entry((head)->next, type, member), \
99
- n = list_entry(pos->member.next, type, member); \
100
- &pos->member != (head); \
129
+ #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); \
132
+ &pos->member != (head); \
101
133
  pos = n, n = list_entry(n->member.next, type, member))
102
134
 
103
135
  #define list_empty(entry) ((entry)->next == (entry))
@@ -133,6 +165,19 @@ static inline void list_del(struct list_head *entry)
133
165
  entry->next = entry->prev = NULL;
134
166
  }
135
167
 
168
+ static inline void list_cut(struct list_head *list, struct list_head *head)
169
+ {
170
+ if (list_empty(head))
171
+ return;
172
+
173
+ list->next = head->next;
174
+ list->next->prev = list;
175
+ list->prev = head->prev;
176
+ list->prev->next = list;
177
+
178
+ list_init(head);
179
+ }
180
+
136
181
  static inline void *usbi_reallocf(void *ptr, size_t size)
137
182
  {
138
183
  void *ret = realloc(ptr, size);
@@ -141,12 +186,19 @@ static inline void *usbi_reallocf(void *ptr, size_t size)
141
186
  return ret;
142
187
  }
143
188
 
144
- #define container_of(ptr, type, member) ({ \
145
- const typeof( ((type *)0)->member ) *mptr = (ptr); \
146
- (type *)( (char *)mptr - offsetof(type,member) );})
189
+ #define container_of(ptr, type, member) ({ \
190
+ const typeof( ((type *)0)->member ) *mptr = (ptr); \
191
+ (type *)( (char *)mptr - offsetof(type,member) );})
147
192
 
193
+ #ifndef CLAMP
194
+ #define CLAMP(val, min, max) ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
195
+ #endif
196
+ #ifndef MIN
148
197
  #define MIN(a, b) ((a) < (b) ? (a) : (b))
198
+ #endif
199
+ #ifndef MAX
149
200
  #define MAX(a, b) ((a) > (b) ? (a) : (b))
201
+ #endif
150
202
 
151
203
  #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
152
204
 
@@ -158,79 +210,90 @@ static inline void *usbi_reallocf(void *ptr, size_t size)
158
210
 
159
211
  /* Some platforms don't have this define */
160
212
  #ifndef TIMESPEC_TO_TIMEVAL
161
- #define TIMESPEC_TO_TIMEVAL(tv, ts) \
162
- do { \
163
- (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec; \
164
- (tv)->tv_usec = (ts)->tv_nsec / 1000; \
165
- } while (0)
213
+ #define TIMESPEC_TO_TIMEVAL(tv, ts) \
214
+ do { \
215
+ (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec; \
216
+ (tv)->tv_usec = (ts)->tv_nsec / 1000; \
217
+ } while (0)
166
218
  #endif
167
219
 
220
+ #ifdef ENABLE_LOGGING
221
+
222
+ #if defined(_MSC_VER) && (_MSC_VER < 1900)
223
+ #define snprintf usbi_snprintf
224
+ #define vsnprintf usbi_vsnprintf
225
+ 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);
227
+ #define LIBUSB_PRINTF_WIN32
228
+ #endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
229
+
168
230
  void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
169
- const char *function, const char *format, ...);
231
+ const char *function, const char *format, ...) USBI_PRINTFLIKE(4, 5);
170
232
 
171
233
  void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
172
- const char *function, const char *format, va_list args);
234
+ const char *function, const char *format, va_list args) USBI_PRINTFLIKE(4, 0);
173
235
 
174
- #if !defined(_MSC_VER) || _MSC_VER >= 1400
236
+ #if !defined(_MSC_VER) || (_MSC_VER >= 1400)
175
237
 
176
- #ifdef ENABLE_LOGGING
177
238
  #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
178
- #define usbi_dbg(...) _usbi_log(NULL, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
179
- #else
180
- #define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
181
- #define usbi_dbg(...) do {} while(0)
182
- #endif
183
239
 
184
- #define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
185
- #define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
186
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__)
187
244
 
188
- #else /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
245
+ #else /* !defined(_MSC_VER) || (_MSC_VER >= 1400) */
189
246
 
190
- #ifdef ENABLE_LOGGING
191
- #define LOG_BODY(ctxt, level) \
192
- { \
193
- va_list args; \
194
- va_start (args, format); \
195
- usbi_log_v(ctxt, level, "", format, args); \
196
- va_end(args); \
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); \
197
253
  }
198
- #else
199
- #define LOG_BODY(ctxt, level) do { (void)(ctxt); } while(0)
200
- #endif
201
-
202
- static inline void usbi_info(struct libusb_context *ctx, const char *format,
203
- ...)
204
- LOG_BODY(ctx,LIBUSB_LOG_LEVEL_INFO)
205
- static inline void usbi_warn(struct libusb_context *ctx, const char *format,
206
- ...)
207
- LOG_BODY(ctx,LIBUSB_LOG_LEVEL_WARNING)
208
- static inline void usbi_err( struct libusb_context *ctx, const char *format,
209
- ...)
210
- LOG_BODY(ctx,LIBUSB_LOG_LEVEL_ERROR)
211
254
 
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)
212
261
  static inline void usbi_dbg(const char *format, ...)
213
- LOG_BODY(NULL,LIBUSB_LOG_LEVEL_DEBUG)
262
+ LOG_BODY(NULL, LIBUSB_LOG_LEVEL_DEBUG)
263
+
264
+ #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1400) */
214
265
 
215
- #endif /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
266
+ #else /* ENABLE_LOGGING */
216
267
 
217
- #define USBI_GET_CONTEXT(ctx) if (!(ctx)) (ctx) = usbi_default_context
218
- #define DEVICE_CTX(dev) ((dev)->ctx)
219
- #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
220
- #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
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)
272
+
273
+ #endif /* ENABLE_LOGGING */
274
+
275
+ #define USBI_GET_CONTEXT(ctx) \
276
+ do { \
277
+ if (!(ctx)) \
278
+ (ctx) = usbi_default_context; \
279
+ } while(0)
280
+
281
+ #define DEVICE_CTX(dev) ((dev)->ctx)
282
+ #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
283
+ #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
221
284
  #define ITRANSFER_CTX(transfer) \
222
285
  (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
223
286
 
224
- #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
225
- #define IS_EPOUT(ep) (!IS_EPIN(ep))
226
- #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
227
- #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
287
+ #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
288
+ #define IS_EPOUT(ep) (!IS_EPIN(ep))
289
+ #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
290
+ #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
228
291
 
229
292
  /* Internal abstraction for thread synchronization */
230
293
  #if defined(THREADS_POSIX)
231
294
  #include "os/threads_posix.h"
232
295
  #elif defined(OS_WINDOWS) || defined(OS_WINCE)
233
- #include <os/threads_windows.h>
296
+ #include "os/threads_windows.h"
234
297
  #endif
235
298
 
236
299
  extern struct libusb_context *usbi_default_context;
@@ -239,12 +302,14 @@ extern struct libusb_context *usbi_default_context;
239
302
  struct pollfd;
240
303
 
241
304
  struct libusb_context {
242
- int debug;
305
+ #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
306
+ enum libusb_log_level debug;
243
307
  int debug_fixed;
308
+ libusb_log_cb log_handler;
309
+ #endif
244
310
 
245
- /* internal control pipe, used for interrupting event handling when
246
- * something needs to modify poll fds. */
247
- int ctrl_pipe[2];
311
+ /* internal event pipe, used for signalling occurrence of an internal event. */
312
+ int event_pipe[2];
248
313
 
249
314
  struct list_head usb_devs;
250
315
  usbi_mutex_t usb_devs_lock;
@@ -256,30 +321,18 @@ struct libusb_context {
256
321
 
257
322
  /* A list of registered hotplug callbacks */
258
323
  struct list_head hotplug_cbs;
324
+ libusb_hotplug_callback_handle next_hotplug_cb_handle;
259
325
  usbi_mutex_t hotplug_cbs_lock;
260
- int hotplug_pipe[2];
261
326
 
262
327
  /* this is a list of in-flight transfer handles, sorted by timeout
263
328
  * expiration. URBs to timeout the soonest are placed at the beginning of
264
329
  * the list, URBs that will time out later are placed after, and urbs with
265
330
  * infinite timeout are always placed at the very end. */
266
331
  struct list_head flying_transfers;
332
+ /* Note paths taking both this and usbi_transfer->lock must always
333
+ * take this lock first */
267
334
  usbi_mutex_t flying_transfers_lock;
268
335
 
269
- /* list and count of poll fds and an array of poll fd structures that is
270
- * (re)allocated as necessary prior to polling, and a flag to indicate
271
- * when the list of poll fds has changed since the last poll. */
272
- struct list_head ipollfds;
273
- struct pollfd *pollfds;
274
- POLL_NFDS_TYPE pollfds_cnt;
275
- unsigned int pollfds_modified;
276
- usbi_mutex_t pollfds_lock;
277
-
278
- /* a counter that is set when we want to interrupt event handling, in order
279
- * to modify the poll fd set. and a lock to protect it. */
280
- unsigned int pollfd_modify;
281
- usbi_mutex_t pollfd_modify_lock;
282
-
283
336
  /* user callbacks for pollfd changes */
284
337
  libusb_pollfd_added_cb fd_added_cb;
285
338
  libusb_pollfd_removed_cb fd_removed_cb;
@@ -291,11 +344,41 @@ struct libusb_context {
291
344
  /* used to see if there is an active thread doing event handling */
292
345
  int event_handler_active;
293
346
 
347
+ /* A thread-local storage key to track which thread is performing event
348
+ * handling */
349
+ usbi_tls_key_t event_handling_key;
350
+
294
351
  /* used to wait for event completion in threads other than the one that is
295
352
  * event handling */
296
353
  usbi_mutex_t event_waiters_lock;
297
354
  usbi_cond_t event_waiters_cond;
298
355
 
356
+ /* A lock to protect internal context event data. */
357
+ usbi_mutex_t event_data_lock;
358
+
359
+ /* A bitmask of flags that are set to indicate specific events that need to
360
+ * be handled. Protected by event_data_lock. */
361
+ unsigned int event_flags;
362
+
363
+ /* A counter that is set when we want to interrupt and prevent event handling,
364
+ * in order to safely close a device. Protected by event_data_lock. */
365
+ unsigned int device_close;
366
+
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;
375
+
376
+ /* A list of pending hotplug messages. Protected by event_data_lock. */
377
+ struct list_head hotplug_msgs;
378
+
379
+ /* A list of pending completed transfers. Protected by event_data_lock. */
380
+ struct list_head completed_transfers;
381
+
299
382
  #ifdef USBI_TIMERFD_AVAILABLE
300
383
  /* used for timeout handling, if supported by OS.
301
384
  * this timerfd is maintained to trigger on the next pending timeout */
@@ -303,8 +386,36 @@ struct libusb_context {
303
386
  #endif
304
387
 
305
388
  struct list_head list;
389
+
390
+ PTR_ALIGNED unsigned char os_priv[ZERO_SIZED_ARRAY];
306
391
  };
307
392
 
393
+ enum usbi_event_flags {
394
+ /* The list of pollfds has been modified */
395
+ USBI_EVENT_POLLFDS_MODIFIED = 1U << 0,
396
+
397
+ /* The user has interrupted the event handler */
398
+ USBI_EVENT_USER_INTERRUPT = 1U << 1,
399
+
400
+ /* A hotplug callback deregistration is pending */
401
+ USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2,
402
+ };
403
+
404
+ /* Macros for managing event handling state */
405
+ #define usbi_handling_events(ctx) \
406
+ (usbi_tls_key_get((ctx)->event_handling_key) != NULL)
407
+
408
+ #define usbi_start_event_handling(ctx) \
409
+ usbi_tls_key_set((ctx)->event_handling_key, ctx)
410
+
411
+ #define usbi_end_event_handling(ctx) \
412
+ usbi_tls_key_set((ctx)->event_handling_key, NULL)
413
+
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))
418
+
308
419
  #ifdef USBI_TIMERFD_AVAILABLE
309
420
  #define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
310
421
  #else
@@ -332,13 +443,7 @@ struct libusb_device {
332
443
  struct libusb_device_descriptor device_descriptor;
333
444
  int attached;
334
445
 
335
- unsigned char os_priv
336
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
337
- [] /* valid C99 code */
338
- #else
339
- [0] /* non-standard, but usually working code */
340
- #endif
341
- ;
446
+ PTR_ALIGNED unsigned char os_priv[ZERO_SIZED_ARRAY];
342
447
  };
343
448
 
344
449
  struct libusb_device_handle {
@@ -349,18 +454,13 @@ struct libusb_device_handle {
349
454
  struct list_head list;
350
455
  struct libusb_device *dev;
351
456
  int auto_detach_kernel_driver;
352
- unsigned char os_priv
353
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
354
- [] /* valid C99 code */
355
- #else
356
- [0] /* non-standard, but usually working code */
357
- #endif
358
- ;
457
+
458
+ PTR_ALIGNED unsigned char os_priv[ZERO_SIZED_ARRAY];
359
459
  };
360
460
 
361
461
  enum {
362
- USBI_CLOCK_MONOTONIC,
363
- USBI_CLOCK_REALTIME
462
+ USBI_CLOCK_MONOTONIC,
463
+ USBI_CLOCK_REALTIME
364
464
  };
365
465
 
366
466
  /* in-memory transfer layout:
@@ -379,10 +479,12 @@ enum {
379
479
  struct usbi_transfer {
380
480
  int num_iso_packets;
381
481
  struct list_head list;
482
+ struct list_head completed_list;
382
483
  struct timeval timeout;
383
484
  int transferred;
384
485
  uint32_t stream_id;
385
- uint8_t flags;
486
+ uint8_t state_flags; /* Protected by usbi_transfer->lock */
487
+ uint8_t timeout_flags; /* Protected by the flying_stransfers_lock */
386
488
 
387
489
  /* this lock is held during libusb_submit_transfer() and
388
490
  * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
@@ -390,39 +492,47 @@ struct usbi_transfer {
390
492
  * should also take this lock in the handle_events path, to prevent the user
391
493
  * cancelling the transfer from another thread while you are processing
392
494
  * its completion (presumably there would be races within your OS backend
393
- * if this were possible). */
495
+ * if this were possible).
496
+ * Note paths taking both this and the flying_transfers_lock must
497
+ * always take the flying_transfers_lock first */
394
498
  usbi_mutex_t lock;
395
499
  };
396
500
 
397
- enum usbi_transfer_flags {
398
- /* The transfer has timed out */
399
- USBI_TRANSFER_TIMED_OUT = 1 << 0,
400
-
401
- /* Set by backend submit_transfer() if the OS handles timeout */
402
- USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 1,
501
+ enum usbi_transfer_state_flags {
502
+ /* Transfer successfully submitted by backend */
503
+ USBI_TRANSFER_IN_FLIGHT = 1U << 0,
403
504
 
404
505
  /* Cancellation was requested via libusb_cancel_transfer() */
405
- USBI_TRANSFER_CANCELLING = 1 << 2,
506
+ USBI_TRANSFER_CANCELLING = 1U << 1,
406
507
 
407
508
  /* Operation on the transfer failed because the device disappeared */
408
- USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
509
+ USBI_TRANSFER_DEVICE_DISAPPEARED = 1U << 2,
510
+ };
511
+
512
+ enum usbi_transfer_timeout_flags {
513
+ /* Set by backend submit_transfer() if the OS handles timeout */
514
+ USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1U << 0,
515
+
516
+ /* The transfer timeout has been handled */
517
+ USBI_TRANSFER_TIMEOUT_HANDLED = 1U << 1,
409
518
 
410
- /* Set by backend submit_transfer() if the fds in use have been updated */
411
- USBI_TRANSFER_UPDATED_FDS = 1 << 4,
519
+ /* The transfer timeout was successfully processed */
520
+ USBI_TRANSFER_TIMED_OUT = 1U << 2,
412
521
  };
413
522
 
414
- #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
415
- ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
523
+ #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
524
+ ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
416
525
  + sizeof(struct usbi_transfer)))
417
- #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
418
- ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
526
+ #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
527
+ ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
419
528
  - sizeof(struct usbi_transfer)))
420
529
 
421
530
  static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
422
531
  {
532
+ assert(transfer->num_iso_packets >= 0);
423
533
  return ((unsigned char *)transfer) + sizeof(struct usbi_transfer)
424
534
  + sizeof(struct libusb_transfer)
425
- + (transfer->num_iso_packets
535
+ + ((size_t)transfer->num_iso_packets
426
536
  * sizeof(struct libusb_iso_packet_descriptor));
427
537
  }
428
538
 
@@ -430,8 +540,8 @@ static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
430
540
 
431
541
  /* All standard descriptors have these 2 fields in common */
432
542
  struct usb_descriptor_header {
433
- uint8_t bLength;
434
- uint8_t bDescriptorType;
543
+ uint8_t bLength;
544
+ uint8_t bDescriptorType;
435
545
  };
436
546
 
437
547
  /* shared data and functions */
@@ -444,11 +554,12 @@ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
444
554
  struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
445
555
  unsigned long session_id);
446
556
  int usbi_sanitize_device(struct libusb_device *dev);
447
- void usbi_handle_disconnect(struct libusb_device_handle *handle);
557
+ void usbi_handle_disconnect(struct libusb_device_handle *dev_handle);
448
558
 
449
559
  int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
450
560
  enum libusb_transfer_status status);
451
561
  int usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
562
+ void usbi_signal_transfer_completion(struct usbi_transfer *transfer);
452
563
 
453
564
  int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
454
565
  void *dest, int host_endian);
@@ -459,27 +570,18 @@ int usbi_get_config_index_by_value(struct libusb_device *dev,
459
570
  void usbi_connect_device (struct libusb_device *dev);
460
571
  void usbi_disconnect_device (struct libusb_device *dev);
461
572
 
573
+ int usbi_signal_event(struct libusb_context *ctx);
574
+ int usbi_clear_event(struct libusb_context *ctx);
575
+
462
576
  /* Internal abstraction for poll (needs struct usbi_transfer on Windows) */
463
- #if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD)
577
+ #if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD) ||\
578
+ defined(OS_HAIKU) || defined(OS_SUNOS)
464
579
  #include <unistd.h>
465
580
  #include "os/poll_posix.h"
466
581
  #elif defined(OS_WINDOWS) || defined(OS_WINCE)
467
582
  #include "os/poll_windows.h"
468
583
  #endif
469
584
 
470
- #if (defined(OS_WINDOWS) || defined(OS_WINCE)) && !defined(__GNUC__)
471
- #define snprintf _snprintf
472
- #define vsnprintf _vsnprintf
473
- int usbi_gettimeofday(struct timeval *tp, void *tzp);
474
- #define LIBUSB_GETTIMEOFDAY_WIN32
475
- #define HAVE_USBI_GETTIMEOFDAY
476
- #else
477
- #ifdef HAVE_GETTIMEOFDAY
478
- #define usbi_gettimeofday(tv, tz) gettimeofday((tv), (tz))
479
- #define HAVE_USBI_GETTIMEOFDAY
480
- #endif
481
- #endif
482
-
483
585
  struct usbi_pollfd {
484
586
  /* must come first */
485
587
  struct libusb_pollfd pollfd;
@@ -489,7 +591,6 @@ struct usbi_pollfd {
489
591
 
490
592
  int usbi_add_pollfd(struct libusb_context *ctx, int fd, short events);
491
593
  void usbi_remove_pollfd(struct libusb_context *ctx, int fd);
492
- void usbi_fd_notification(struct libusb_context *ctx);
493
594
 
494
595
  /* device discovery */
495
596
 
@@ -501,13 +602,7 @@ void usbi_fd_notification(struct libusb_context *ctx);
501
602
  struct discovered_devs {
502
603
  size_t len;
503
604
  size_t capacity;
504
- struct libusb_device *devices
505
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
506
- [] /* valid C99 code */
507
- #else
508
- [0] /* non-standard, but usually working code */
509
- #endif
510
- ;
605
+ struct libusb_device *devices[ZERO_SIZED_ARRAY];
511
606
  };
512
607
 
513
608
  struct discovered_devs *discovered_devs_append(
@@ -540,7 +635,17 @@ struct usbi_os_backend {
540
635
  *
541
636
  * This function is called when the user deinitializes the library.
542
637
  */
543
- void (*exit)(void);
638
+ void (*exit)(struct libusb_context *ctx);
639
+
640
+ /* Set a backend-specific option. Optional.
641
+ *
642
+ * This function is called when the user calls libusb_set_option() and
643
+ * the option is not handled by the core library.
644
+ *
645
+ * Return 0 on success, or a LIBUSB_ERROR code on failure.
646
+ */
647
+ int (*set_option)(struct libusb_context *ctx, enum libusb_option option,
648
+ va_list args);
544
649
 
545
650
  /* Enumerate all the USB devices on the system, returning them in a list
546
651
  * of discovered devices.
@@ -613,6 +718,34 @@ struct usbi_os_backend {
613
718
  */
614
719
  void (*hotplug_poll)(void);
615
720
 
721
+ /* Wrap a platform-specific device handle for I/O and other USB
722
+ * operations. The device handle is preallocated for you.
723
+ *
724
+ * Your backend should allocate any internal resources required for I/O
725
+ * and other operations so that those operations can happen (hopefully)
726
+ * without hiccup. This is also a good place to inform libusb that it
727
+ * should monitor certain file descriptors related to this device -
728
+ * see the usbi_add_pollfd() function.
729
+ *
730
+ * Your backend should also initialize the device structure
731
+ * (dev_handle->dev), which is NULL at the beginning of the call.
732
+ *
733
+ * This function should not generate any bus I/O and should not block.
734
+ *
735
+ * This function is called when the user attempts to wrap an existing
736
+ * platform-specific device handle for a device.
737
+ *
738
+ * Return:
739
+ * - 0 on success
740
+ * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
741
+ * - another LIBUSB_ERROR code on other failure
742
+ *
743
+ * Do not worry about freeing the handle on failed open, the upper layers
744
+ * do this for you.
745
+ */
746
+ int (*wrap_sys_device)(struct libusb_context *ctx,
747
+ struct libusb_device_handle *dev_handle, intptr_t sys_dev);
748
+
616
749
  /* Open a device for I/O and other USB operations. The device handle
617
750
  * is preallocated for you, you can retrieve the device in question
618
751
  * through handle->dev.
@@ -638,7 +771,7 @@ struct usbi_os_backend {
638
771
  * Do not worry about freeing the handle on failed open, the upper layers
639
772
  * do this for you.
640
773
  */
641
- int (*open)(struct libusb_device_handle *handle);
774
+ int (*open)(struct libusb_device_handle *dev_handle);
642
775
 
643
776
  /* Close a device such that the handle cannot be used again. Your backend
644
777
  * should destroy any resources that were allocated in the open path.
@@ -648,7 +781,7 @@ struct usbi_os_backend {
648
781
  *
649
782
  * This function is called when the user closes a device handle.
650
783
  */
651
- void (*close)(struct libusb_device_handle *handle);
784
+ void (*close)(struct libusb_device_handle *dev_handle);
652
785
 
653
786
  /* Retrieve the device descriptor from a device.
654
787
  *
@@ -755,7 +888,7 @@ struct usbi_os_backend {
755
888
  * blocking
756
889
  * - another LIBUSB_ERROR code on other failure.
757
890
  */
758
- int (*get_configuration)(struct libusb_device_handle *handle, int *config);
891
+ int (*get_configuration)(struct libusb_device_handle *dev_handle, int *config);
759
892
 
760
893
  /* Set the active configuration for a device.
761
894
  *
@@ -772,7 +905,7 @@ struct usbi_os_backend {
772
905
  * was opened
773
906
  * - another LIBUSB_ERROR code on other failure.
774
907
  */
775
- int (*set_configuration)(struct libusb_device_handle *handle, int config);
908
+ int (*set_configuration)(struct libusb_device_handle *dev_handle, int config);
776
909
 
777
910
  /* Claim an interface. When claimed, the application can then perform
778
911
  * I/O to an interface's endpoints.
@@ -780,7 +913,7 @@ struct usbi_os_backend {
780
913
  * This function should not generate any bus I/O and should not block.
781
914
  * Interface claiming is a logical operation that simply ensures that
782
915
  * no other drivers/applications are using the interface, and after
783
- * claiming, no other drivers/applicatiosn can use the interface because
916
+ * claiming, no other drivers/applications can use the interface because
784
917
  * we now "own" it.
785
918
  *
786
919
  * Return:
@@ -791,7 +924,7 @@ struct usbi_os_backend {
791
924
  * was opened
792
925
  * - another LIBUSB_ERROR code on other failure
793
926
  */
794
- int (*claim_interface)(struct libusb_device_handle *handle, int interface_number);
927
+ int (*claim_interface)(struct libusb_device_handle *dev_handle, int interface_number);
795
928
 
796
929
  /* Release a previously claimed interface.
797
930
  *
@@ -808,7 +941,7 @@ struct usbi_os_backend {
808
941
  * was opened
809
942
  * - another LIBUSB_ERROR code on other failure
810
943
  */
811
- int (*release_interface)(struct libusb_device_handle *handle, int interface_number);
944
+ int (*release_interface)(struct libusb_device_handle *dev_handle, int interface_number);
812
945
 
813
946
  /* Set the alternate setting for an interface.
814
947
  *
@@ -824,7 +957,7 @@ struct usbi_os_backend {
824
957
  * was opened
825
958
  * - another LIBUSB_ERROR code on other failure
826
959
  */
827
- int (*set_interface_altsetting)(struct libusb_device_handle *handle,
960
+ int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
828
961
  int interface_number, int altsetting);
829
962
 
830
963
  /* Clear a halt/stall condition on an endpoint.
@@ -838,12 +971,12 @@ struct usbi_os_backend {
838
971
  * was opened
839
972
  * - another LIBUSB_ERROR code on other failure
840
973
  */
841
- int (*clear_halt)(struct libusb_device_handle *handle,
974
+ int (*clear_halt)(struct libusb_device_handle *dev_handle,
842
975
  unsigned char endpoint);
843
976
 
844
977
  /* Perform a USB port reset to reinitialize a device.
845
978
  *
846
- * If possible, the handle should still be usable after the reset
979
+ * If possible, the device handle should still be usable after the reset
847
980
  * completes, assuming that the device descriptors did not change during
848
981
  * reset and all previous interface state can be restored.
849
982
  *
@@ -857,16 +990,26 @@ struct usbi_os_backend {
857
990
  * has been disconnected since it was opened
858
991
  * - another LIBUSB_ERROR code on other failure
859
992
  */
860
- int (*reset_device)(struct libusb_device_handle *handle);
993
+ int (*reset_device)(struct libusb_device_handle *dev_handle);
861
994
 
862
995
  /* Alloc num_streams usb3 bulk streams on the passed in endpoints */
863
- int (*alloc_streams)(struct libusb_device_handle *handle,
996
+ int (*alloc_streams)(struct libusb_device_handle *dev_handle,
864
997
  uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
865
998
 
866
999
  /* Free usb3 bulk streams allocated with alloc_streams */
867
- int (*free_streams)(struct libusb_device_handle *handle,
1000
+ int (*free_streams)(struct libusb_device_handle *dev_handle,
868
1001
  unsigned char *endpoints, int num_endpoints);
869
1002
 
1003
+ /* Allocate persistent DMA memory for the given device, suitable for
1004
+ * zerocopy. May return NULL on failure. Optional to implement.
1005
+ */
1006
+ unsigned char *(*dev_mem_alloc)(struct libusb_device_handle *handle,
1007
+ size_t len);
1008
+
1009
+ /* Free memory allocated by dev_mem_alloc. */
1010
+ int (*dev_mem_free)(struct libusb_device_handle *handle,
1011
+ unsigned char *buffer, size_t len);
1012
+
870
1013
  /* Determine if a kernel driver is active on an interface. Optional.
871
1014
  *
872
1015
  * The presence of a kernel driver on an interface indicates that any
@@ -879,7 +1022,7 @@ struct usbi_os_backend {
879
1022
  * was opened
880
1023
  * - another LIBUSB_ERROR code on other failure
881
1024
  */
882
- int (*kernel_driver_active)(struct libusb_device_handle *handle,
1025
+ int (*kernel_driver_active)(struct libusb_device_handle *dev_handle,
883
1026
  int interface_number);
884
1027
 
885
1028
  /* Detach a kernel driver from an interface. Optional.
@@ -895,7 +1038,7 @@ struct usbi_os_backend {
895
1038
  * was opened
896
1039
  * - another LIBUSB_ERROR code on other failure
897
1040
  */
898
- int (*detach_kernel_driver)(struct libusb_device_handle *handle,
1041
+ int (*detach_kernel_driver)(struct libusb_device_handle *dev_handle,
899
1042
  int interface_number);
900
1043
 
901
1044
  /* Attach a kernel driver to an interface. Optional.
@@ -912,7 +1055,7 @@ struct usbi_os_backend {
912
1055
  * preventing reattachment
913
1056
  * - another LIBUSB_ERROR code on other failure
914
1057
  */
915
- int (*attach_kernel_driver)(struct libusb_device_handle *handle,
1058
+ int (*attach_kernel_driver)(struct libusb_device_handle *dev_handle,
916
1059
  int interface_number);
917
1060
 
918
1061
  /* Destroy a device. Optional.
@@ -958,8 +1101,14 @@ struct usbi_os_backend {
958
1101
  */
959
1102
  void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
960
1103
 
961
- /* Handle any pending events. This involves monitoring any active
962
- * transfers and processing their completion or cancellation.
1104
+ /* Handle any pending events on file descriptors. Optional.
1105
+ *
1106
+ * Provide this function when file descriptors directly indicate device
1107
+ * or transfer activity. If your backend does not have such file descriptors,
1108
+ * implement the handle_transfer_completion function below.
1109
+ *
1110
+ * This involves monitoring any active transfers and processing their
1111
+ * completion or cancellation.
963
1112
  *
964
1113
  * The function is passed an array of pollfd structures (size nfds)
965
1114
  * as a result of the poll() system call. The num_ready parameter
@@ -987,6 +1136,31 @@ struct usbi_os_backend {
987
1136
  int (*handle_events)(struct libusb_context *ctx,
988
1137
  struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
989
1138
 
1139
+ /* Handle transfer completion. Optional.
1140
+ *
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.
1144
+ *
1145
+ * Your backend must tell the library when a transfer has completed by
1146
+ * calling usbi_signal_transfer_completion(). You should store any private
1147
+ * information about the transfer and its completion status in the transfer's
1148
+ * private backend data.
1149
+ *
1150
+ * During event handling, this function will be called on each transfer for
1151
+ * which usbi_signal_transfer_completion() was called.
1152
+ *
1153
+ * For any cancelled transfers, call usbi_handle_transfer_cancellation().
1154
+ * For completed transfers, call usbi_handle_transfer_completion().
1155
+ * For control/bulk/interrupt transfers, populate the "transferred"
1156
+ * element of the appropriate usbi_transfer structure before calling the
1157
+ * above functions. For isochronous transfers, populate the status and
1158
+ * transferred fields of the iso packet descriptors of the transfer.
1159
+ *
1160
+ * Return 0 on success, or a LIBUSB_ERROR code on failure.
1161
+ */
1162
+ int (*handle_transfer_completion)(struct usbi_transfer *itransfer);
1163
+
990
1164
  /* Get time from specified clock. At least two clocks must be implemented
991
1165
  by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
992
1166
 
@@ -1002,6 +1176,11 @@ struct usbi_os_backend {
1002
1176
  clockid_t (*get_timerfd_clockid)(void);
1003
1177
  #endif
1004
1178
 
1179
+ /* 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. */
1182
+ size_t context_priv_size;
1183
+
1005
1184
  /* Number of bytes to reserve for per-device private backend data.
1006
1185
  * This private data area is accessible through the "os_priv" field of
1007
1186
  * struct libusb_device. */
@@ -1017,24 +1196,15 @@ struct usbi_os_backend {
1017
1196
  * usbi_transfer_get_os_priv() on the appropriate usbi_transfer instance.
1018
1197
  */
1019
1198
  size_t transfer_priv_size;
1020
-
1021
- /* Mumber of additional bytes for os_priv for each iso packet.
1022
- * Can your backend use this? */
1023
- /* FIXME: linux can't use this any more. if other OS's cannot either,
1024
- * then remove this */
1025
- size_t add_iso_packet_size;
1026
1199
  };
1027
1200
 
1028
- extern const struct usbi_os_backend * const usbi_backend;
1029
-
1030
- extern const struct usbi_os_backend linux_usbfs_backend;
1031
- extern const struct usbi_os_backend darwin_backend;
1032
- extern const struct usbi_os_backend openbsd_backend;
1033
- extern const struct usbi_os_backend netbsd_backend;
1034
- extern const struct usbi_os_backend windows_backend;
1035
- extern const struct usbi_os_backend wince_backend;
1201
+ extern const struct usbi_os_backend usbi_backend;
1036
1202
 
1037
1203
  extern struct list_head active_contexts_list;
1038
1204
  extern usbi_mutex_static_t active_contexts_lock;
1039
1205
 
1206
+ #ifdef __cplusplus
1207
+ }
1208
+ #endif
1209
+
1040
1210
  #endif