usb 1.7.2-prebuild

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 (183) hide show
  1. package/.github/workflows/prebuild.yml +62 -0
  2. package/.gitmodules +3 -0
  3. package/LICENSE +7 -0
  4. package/Readme.md +339 -0
  5. package/binding.gyp +90 -0
  6. package/libusb/.gitattributes +11 -0
  7. package/libusb/.private/README.txt +5 -0
  8. package/libusb/.private/bd.cmd +89 -0
  9. package/libusb/.private/bm.sh +54 -0
  10. package/libusb/.private/bwince.cmd +57 -0
  11. package/libusb/.private/post-rewrite.sh +28 -0
  12. package/libusb/.private/pre-commit.sh +42 -0
  13. package/libusb/.private/wbs.txt +61 -0
  14. package/libusb/.private/wbs_wince.txt +42 -0
  15. package/libusb/AUTHORS +78 -0
  16. package/libusb/COPYING +504 -0
  17. package/libusb/ChangeLog +211 -0
  18. package/libusb/INSTALL +234 -0
  19. package/libusb/INSTALL_WIN.txt +73 -0
  20. package/libusb/Makefile.am +28 -0
  21. package/libusb/NEWS +2 -0
  22. package/libusb/PORTING +94 -0
  23. package/libusb/README +28 -0
  24. package/libusb/README.git +41 -0
  25. package/libusb/TODO +2 -0
  26. package/libusb/Xcode/common.xcconfig +49 -0
  27. package/libusb/Xcode/debug.xcconfig +29 -0
  28. package/libusb/Xcode/libusb.xcconfig +21 -0
  29. package/libusb/Xcode/libusb.xcodeproj/project.pbxproj +1 -0
  30. package/libusb/Xcode/libusb_debug.xcconfig +21 -0
  31. package/libusb/Xcode/libusb_release.xcconfig +21 -0
  32. package/libusb/Xcode/release.xcconfig +30 -0
  33. package/libusb/android/README +114 -0
  34. package/libusb/android/jni/Android.mk +23 -0
  35. package/libusb/android/jni/Application.mk +24 -0
  36. package/libusb/android/jni/examples.mk +134 -0
  37. package/libusb/android/jni/libusb.mk +54 -0
  38. package/libusb/android/jni/tests.mk +56 -0
  39. package/libusb/autogen.sh +8 -0
  40. package/libusb/bootstrap.sh +19 -0
  41. package/libusb/configure.ac +304 -0
  42. package/libusb/doc/Makefile.am +9 -0
  43. package/libusb/doc/doxygen.cfg.in +1288 -0
  44. package/libusb/doc/libusb.png +0 -0
  45. package/libusb/examples/Makefile.am +19 -0
  46. package/libusb/examples/dpfp.c +506 -0
  47. package/libusb/examples/dpfp_threaded.c +544 -0
  48. package/libusb/examples/ezusb.c +831 -0
  49. package/libusb/examples/ezusb.h +120 -0
  50. package/libusb/examples/fxload.c +287 -0
  51. package/libusb/examples/getopt/getopt.c +1060 -0
  52. package/libusb/examples/getopt/getopt.h +180 -0
  53. package/libusb/examples/getopt/getopt1.c +188 -0
  54. package/libusb/examples/hotplugtest.c +104 -0
  55. package/libusb/examples/listdevs.c +71 -0
  56. package/libusb/examples/sam3u_benchmark.c +193 -0
  57. package/libusb/examples/xusb.c +1129 -0
  58. package/libusb/libusb/Makefile.am +75 -0
  59. package/libusb/libusb/core.c +2342 -0
  60. package/libusb/libusb/descriptor.c +1199 -0
  61. package/libusb/libusb/hotplug.c +327 -0
  62. package/libusb/libusb/hotplug.h +82 -0
  63. package/libusb/libusb/io.c +2631 -0
  64. package/libusb/libusb/libusb-1.0.def +166 -0
  65. package/libusb/libusb/libusb-1.0.rc +61 -0
  66. package/libusb/libusb/libusb.h +1998 -0
  67. package/libusb/libusb/libusbi.h +1040 -0
  68. package/libusb/libusb/os/darwin_usb.c +2009 -0
  69. package/libusb/libusb/os/darwin_usb.h +162 -0
  70. package/libusb/libusb/os/linux_netlink.c +369 -0
  71. package/libusb/libusb/os/linux_udev.c +307 -0
  72. package/libusb/libusb/os/linux_usbfs.c +2695 -0
  73. package/libusb/libusb/os/linux_usbfs.h +192 -0
  74. package/libusb/libusb/os/netbsd_usb.c +738 -0
  75. package/libusb/libusb/os/openbsd_usb.c +832 -0
  76. package/libusb/libusb/os/poll_posix.c +51 -0
  77. package/libusb/libusb/os/poll_posix.h +11 -0
  78. package/libusb/libusb/os/poll_windows.c +796 -0
  79. package/libusb/libusb/os/poll_windows.h +131 -0
  80. package/libusb/libusb/os/threads_posix.c +82 -0
  81. package/libusb/libusb/os/threads_posix.h +50 -0
  82. package/libusb/libusb/os/threads_windows.c +212 -0
  83. package/libusb/libusb/os/threads_windows.h +87 -0
  84. package/libusb/libusb/os/wince_usb.c +1032 -0
  85. package/libusb/libusb/os/wince_usb.h +131 -0
  86. package/libusb/libusb/os/windows_common.h +108 -0
  87. package/libusb/libusb/os/windows_usb.c +5347 -0
  88. package/libusb/libusb/os/windows_usb.h +971 -0
  89. package/libusb/libusb/strerror.c +199 -0
  90. package/libusb/libusb/sync.c +307 -0
  91. package/libusb/libusb/version.h +18 -0
  92. package/libusb/libusb/version_nano.h +1 -0
  93. package/libusb/libusb-1.0.pc.in +11 -0
  94. package/libusb/msvc/config.h +50 -0
  95. package/libusb/msvc/ddk_build.cmd +175 -0
  96. package/libusb/msvc/errno.h +102 -0
  97. package/libusb/msvc/fxload_2010.vcxproj +170 -0
  98. package/libusb/msvc/fxload_2010.vcxproj.filters +25 -0
  99. package/libusb/msvc/fxload_2012.vcxproj +174 -0
  100. package/libusb/msvc/fxload_2012.vcxproj.filters +25 -0
  101. package/libusb/msvc/fxload_2013.vcxproj +174 -0
  102. package/libusb/msvc/fxload_sources +23 -0
  103. package/libusb/msvc/getopt_2005.vcproj +288 -0
  104. package/libusb/msvc/getopt_2010.vcxproj +131 -0
  105. package/libusb/msvc/getopt_2010.vcxproj.filters +26 -0
  106. package/libusb/msvc/getopt_2012.vcxproj +136 -0
  107. package/libusb/msvc/getopt_2012.vcxproj.filters +26 -0
  108. package/libusb/msvc/getopt_2013.vcxproj +136 -0
  109. package/libusb/msvc/getopt_sources +20 -0
  110. package/libusb/msvc/hotplugtest_2010.vcxproj +163 -0
  111. package/libusb/msvc/hotplugtest_2010.vcxproj.filters +14 -0
  112. package/libusb/msvc/hotplugtest_2012.vcxproj +167 -0
  113. package/libusb/msvc/hotplugtest_2012.vcxproj.filters +14 -0
  114. package/libusb/msvc/hotplugtest_2013.vcxproj +167 -0
  115. package/libusb/msvc/hotplugtest_sources +20 -0
  116. package/libusb/msvc/inttypes.h +295 -0
  117. package/libusb/msvc/libusb.dsw +71 -0
  118. package/libusb/msvc/libusb_2005.sln +95 -0
  119. package/libusb/msvc/libusb_2010.sln +94 -0
  120. package/libusb/msvc/libusb_2012.sln +94 -0
  121. package/libusb/msvc/libusb_2013.sln +100 -0
  122. package/libusb/msvc/libusb_2015.sln +100 -0
  123. package/libusb/msvc/libusb_dll.dsp +194 -0
  124. package/libusb/msvc/libusb_dll_2005.vcproj +436 -0
  125. package/libusb/msvc/libusb_dll_2010.vcxproj +170 -0
  126. package/libusb/msvc/libusb_dll_2010.vcxproj.filters +81 -0
  127. package/libusb/msvc/libusb_dll_2012.vcxproj +175 -0
  128. package/libusb/msvc/libusb_dll_2012.vcxproj.filters +84 -0
  129. package/libusb/msvc/libusb_dll_2013.vcxproj +175 -0
  130. package/libusb/msvc/libusb_dll_wince.vcproj +1243 -0
  131. package/libusb/msvc/libusb_sources +38 -0
  132. package/libusb/msvc/libusb_static.dsp +174 -0
  133. package/libusb/msvc/libusb_static_2005.vcproj +362 -0
  134. package/libusb/msvc/libusb_static_2010.vcxproj +156 -0
  135. package/libusb/msvc/libusb_static_2010.vcxproj.filters +74 -0
  136. package/libusb/msvc/libusb_static_2012.vcxproj +160 -0
  137. package/libusb/msvc/libusb_static_2012.vcxproj.filters +74 -0
  138. package/libusb/msvc/libusb_static_2013.vcxproj +160 -0
  139. package/libusb/msvc/libusb_static_wince.vcproj +1185 -0
  140. package/libusb/msvc/libusb_wince.sln +246 -0
  141. package/libusb/msvc/listdevs.dsp +103 -0
  142. package/libusb/msvc/listdevs_2005.vcproj +360 -0
  143. package/libusb/msvc/listdevs_2010.vcxproj +165 -0
  144. package/libusb/msvc/listdevs_2010.vcxproj.filters +14 -0
  145. package/libusb/msvc/listdevs_2012.vcxproj +169 -0
  146. package/libusb/msvc/listdevs_2012.vcxproj.filters +14 -0
  147. package/libusb/msvc/listdevs_2013.vcxproj +169 -0
  148. package/libusb/msvc/listdevs_sources +19 -0
  149. package/libusb/msvc/listdevs_wince.vcproj +1120 -0
  150. package/libusb/msvc/missing.c +80 -0
  151. package/libusb/msvc/missing.h +32 -0
  152. package/libusb/msvc/stdint.h +256 -0
  153. package/libusb/msvc/stress_2005.vcproj +390 -0
  154. package/libusb/msvc/stress_2010.vcxproj +167 -0
  155. package/libusb/msvc/stress_2010.vcxproj.filters +25 -0
  156. package/libusb/msvc/stress_2012.vcxproj +171 -0
  157. package/libusb/msvc/stress_2012.vcxproj.filters +25 -0
  158. package/libusb/msvc/stress_2013.vcxproj +171 -0
  159. package/libusb/msvc/stress_wince.vcproj +1128 -0
  160. package/libusb/msvc/xusb.dsp +102 -0
  161. package/libusb/msvc/xusb_2005.vcproj +344 -0
  162. package/libusb/msvc/xusb_2010.vcxproj +163 -0
  163. package/libusb/msvc/xusb_2010.vcxproj.filters +14 -0
  164. package/libusb/msvc/xusb_2012.vcxproj +167 -0
  165. package/libusb/msvc/xusb_2012.vcxproj.filters +14 -0
  166. package/libusb/msvc/xusb_2013.vcxproj +167 -0
  167. package/libusb/msvc/xusb_sources +20 -0
  168. package/libusb/msvc/xusb_wince.vcproj +1120 -0
  169. package/libusb/tests/Makefile.am +6 -0
  170. package/libusb/tests/libusb_testlib.h +107 -0
  171. package/libusb/tests/stress.c +160 -0
  172. package/libusb/tests/testlib.c +281 -0
  173. package/libusb.gypi +136 -0
  174. package/libusb_config/config.h +1 -0
  175. package/package.json +69 -0
  176. package/src/device.cc +412 -0
  177. package/src/helpers.h +64 -0
  178. package/src/node_usb.cc +319 -0
  179. package/src/node_usb.h +120 -0
  180. package/src/transfer.cc +148 -0
  181. package/src/uv_async_queue.h +33 -0
  182. package/test/usb.coffee +191 -0
  183. package/usb.js +524 -0
@@ -0,0 +1,1040 @@
1
+ /*
2
+ * Internal header for libusb
3
+ * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
4
+ * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
5
+ *
6
+ * This library is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * This library is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with this library; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+ #ifndef LIBUSBI_H
22
+ #define LIBUSBI_H
23
+
24
+ #include "config.h"
25
+
26
+ #include <stdlib.h>
27
+
28
+ #include <stddef.h>
29
+ #include <stdint.h>
30
+ #include <time.h>
31
+ #include <stdarg.h>
32
+ #ifdef HAVE_POLL_H
33
+ #include <poll.h>
34
+ #endif
35
+
36
+ #ifdef HAVE_MISSING_H
37
+ #include "missing.h"
38
+ #endif
39
+ #include "libusb.h"
40
+ #include "version.h"
41
+
42
+ /* Inside the libusb code, mark all public functions as follows:
43
+ * return_type API_EXPORTED function_name(params) { ... }
44
+ * But if the function returns a pointer, mark it as follows:
45
+ * DEFAULT_VISIBILITY return_type * LIBUSB_CALL function_name(params) { ... }
46
+ * In the libusb public header, mark all declarations as:
47
+ * return_type LIBUSB_CALL function_name(params);
48
+ */
49
+ #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
50
+
51
+ #define DEVICE_DESC_LENGTH 18
52
+
53
+ #define USB_MAXENDPOINTS 32
54
+ #define USB_MAXINTERFACES 32
55
+ #define USB_MAXCONFIG 8
56
+
57
+ /* Backend specific capabilities */
58
+ #define USBI_CAP_HAS_HID_ACCESS 0x00010000
59
+ #define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000
60
+ #define USBI_CAP_HAS_POLLABLE_DEVICE_FD 0x00040000
61
+
62
+ /* Maximum number of bytes in a log line */
63
+ #define USBI_MAX_LOG_LEN 1024
64
+ /* Terminator for log lines */
65
+ #define USBI_LOG_LINE_END "\n"
66
+
67
+ /* The following is used to silence warnings for unused variables */
68
+ #define UNUSED(var) do { (void)(var); } while(0)
69
+
70
+ #if !defined(ARRAYSIZE)
71
+ #define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
72
+ #endif
73
+
74
+ struct list_head {
75
+ struct list_head *prev, *next;
76
+ };
77
+
78
+ /* Get an entry from the list
79
+ * ptr - the address of this list_head element in "type"
80
+ * type - the data type that contains "member"
81
+ * member - the list_head element in "type"
82
+ */
83
+ #define list_entry(ptr, type, member) \
84
+ ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
85
+
86
+ /* Get each entry from a list
87
+ * pos - A structure pointer has a "member" element
88
+ * head - list head
89
+ * member - the list_head element in "pos"
90
+ * type - the type of the first parameter
91
+ */
92
+ #define list_for_each_entry(pos, head, member, type) \
93
+ for (pos = list_entry((head)->next, type, member); \
94
+ &pos->member != (head); \
95
+ pos = list_entry(pos->member.next, type, member))
96
+
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); \
101
+ pos = n, n = list_entry(n->member.next, type, member))
102
+
103
+ #define list_empty(entry) ((entry)->next == (entry))
104
+
105
+ static inline void list_init(struct list_head *entry)
106
+ {
107
+ entry->prev = entry->next = entry;
108
+ }
109
+
110
+ static inline void list_add(struct list_head *entry, struct list_head *head)
111
+ {
112
+ entry->next = head->next;
113
+ entry->prev = head;
114
+
115
+ head->next->prev = entry;
116
+ head->next = entry;
117
+ }
118
+
119
+ static inline void list_add_tail(struct list_head *entry,
120
+ struct list_head *head)
121
+ {
122
+ entry->next = head;
123
+ entry->prev = head->prev;
124
+
125
+ head->prev->next = entry;
126
+ head->prev = entry;
127
+ }
128
+
129
+ static inline void list_del(struct list_head *entry)
130
+ {
131
+ entry->next->prev = entry->prev;
132
+ entry->prev->next = entry->next;
133
+ entry->next = entry->prev = NULL;
134
+ }
135
+
136
+ static inline void *usbi_reallocf(void *ptr, size_t size)
137
+ {
138
+ void *ret = realloc(ptr, size);
139
+ if (!ret)
140
+ free(ptr);
141
+ return ret;
142
+ }
143
+
144
+ #define container_of(ptr, type, member) ({ \
145
+ const typeof( ((type *)0)->member ) *mptr = (ptr); \
146
+ (type *)( (char *)mptr - offsetof(type,member) );})
147
+
148
+ #define MIN(a, b) ((a) < (b) ? (a) : (b))
149
+ #define MAX(a, b) ((a) > (b) ? (a) : (b))
150
+
151
+ #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
152
+
153
+ #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
154
+ #define TIMEVAL_TV_SEC_TYPE long
155
+ #else
156
+ #define TIMEVAL_TV_SEC_TYPE time_t
157
+ #endif
158
+
159
+ /* Some platforms don't have this define */
160
+ #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)
166
+ #endif
167
+
168
+ void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
169
+ const char *function, const char *format, ...);
170
+
171
+ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
172
+ const char *function, const char *format, va_list args);
173
+
174
+ #if !defined(_MSC_VER) || _MSC_VER >= 1400
175
+
176
+ #ifdef ENABLE_LOGGING
177
+ #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
+
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
+ #define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
187
+
188
+ #else /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
189
+
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); \
197
+ }
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
+
212
+ static inline void usbi_dbg(const char *format, ...)
213
+ LOG_BODY(NULL,LIBUSB_LOG_LEVEL_DEBUG)
214
+
215
+ #endif /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
216
+
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))
221
+ #define ITRANSFER_CTX(transfer) \
222
+ (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
223
+
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))
228
+
229
+ /* Internal abstraction for thread synchronization */
230
+ #if defined(THREADS_POSIX)
231
+ #include "os/threads_posix.h"
232
+ #elif defined(OS_WINDOWS) || defined(OS_WINCE)
233
+ #include <os/threads_windows.h>
234
+ #endif
235
+
236
+ extern struct libusb_context *usbi_default_context;
237
+
238
+ /* Forward declaration for use in context (fully defined inside poll abstraction) */
239
+ struct pollfd;
240
+
241
+ struct libusb_context {
242
+ int debug;
243
+ int debug_fixed;
244
+
245
+ /* internal control pipe, used for interrupting event handling when
246
+ * something needs to modify poll fds. */
247
+ int ctrl_pipe[2];
248
+
249
+ struct list_head usb_devs;
250
+ usbi_mutex_t usb_devs_lock;
251
+
252
+ /* A list of open handles. Backends are free to traverse this if required.
253
+ */
254
+ struct list_head open_devs;
255
+ usbi_mutex_t open_devs_lock;
256
+
257
+ /* A list of registered hotplug callbacks */
258
+ struct list_head hotplug_cbs;
259
+ usbi_mutex_t hotplug_cbs_lock;
260
+ int hotplug_pipe[2];
261
+
262
+ /* this is a list of in-flight transfer handles, sorted by timeout
263
+ * expiration. URBs to timeout the soonest are placed at the beginning of
264
+ * the list, URBs that will time out later are placed after, and urbs with
265
+ * infinite timeout are always placed at the very end. */
266
+ struct list_head flying_transfers;
267
+ usbi_mutex_t flying_transfers_lock;
268
+
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
+ /* user callbacks for pollfd changes */
284
+ libusb_pollfd_added_cb fd_added_cb;
285
+ libusb_pollfd_removed_cb fd_removed_cb;
286
+ void *fd_cb_user_data;
287
+
288
+ /* ensures that only one thread is handling events at any one time */
289
+ usbi_mutex_t events_lock;
290
+
291
+ /* used to see if there is an active thread doing event handling */
292
+ int event_handler_active;
293
+
294
+ /* used to wait for event completion in threads other than the one that is
295
+ * event handling */
296
+ usbi_mutex_t event_waiters_lock;
297
+ usbi_cond_t event_waiters_cond;
298
+
299
+ #ifdef USBI_TIMERFD_AVAILABLE
300
+ /* used for timeout handling, if supported by OS.
301
+ * this timerfd is maintained to trigger on the next pending timeout */
302
+ int timerfd;
303
+ #endif
304
+
305
+ struct list_head list;
306
+ };
307
+
308
+ #ifdef USBI_TIMERFD_AVAILABLE
309
+ #define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
310
+ #else
311
+ #define usbi_using_timerfd(ctx) (0)
312
+ #endif
313
+
314
+ struct libusb_device {
315
+ /* lock protects refcnt, everything else is finalized at initialization
316
+ * time */
317
+ usbi_mutex_t lock;
318
+ int refcnt;
319
+
320
+ struct libusb_context *ctx;
321
+
322
+ uint8_t bus_number;
323
+ uint8_t port_number;
324
+ struct libusb_device* parent_dev;
325
+ uint8_t device_address;
326
+ uint8_t num_configurations;
327
+ enum libusb_speed speed;
328
+
329
+ struct list_head list;
330
+ unsigned long session_data;
331
+
332
+ struct libusb_device_descriptor device_descriptor;
333
+ int attached;
334
+
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
+ ;
342
+ };
343
+
344
+ struct libusb_device_handle {
345
+ /* lock protects claimed_interfaces */
346
+ usbi_mutex_t lock;
347
+ unsigned long claimed_interfaces;
348
+
349
+ struct list_head list;
350
+ struct libusb_device *dev;
351
+ 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
+ ;
359
+ };
360
+
361
+ enum {
362
+ USBI_CLOCK_MONOTONIC,
363
+ USBI_CLOCK_REALTIME
364
+ };
365
+
366
+ /* in-memory transfer layout:
367
+ *
368
+ * 1. struct usbi_transfer
369
+ * 2. struct libusb_transfer (which includes iso packets) [variable size]
370
+ * 3. os private data [variable size]
371
+ *
372
+ * from a libusb_transfer, you can get the usbi_transfer by rewinding the
373
+ * appropriate number of bytes.
374
+ * the usbi_transfer includes the number of allocated packets, so you can
375
+ * determine the size of the transfer and hence the start and length of the
376
+ * OS-private data.
377
+ */
378
+
379
+ struct usbi_transfer {
380
+ int num_iso_packets;
381
+ struct list_head list;
382
+ struct timeval timeout;
383
+ int transferred;
384
+ uint32_t stream_id;
385
+ uint8_t flags;
386
+
387
+ /* this lock is held during libusb_submit_transfer() and
388
+ * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
389
+ * cancellation, submission-during-cancellation, etc). the OS backend
390
+ * should also take this lock in the handle_events path, to prevent the user
391
+ * cancelling the transfer from another thread while you are processing
392
+ * its completion (presumably there would be races within your OS backend
393
+ * if this were possible). */
394
+ usbi_mutex_t lock;
395
+ };
396
+
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,
403
+
404
+ /* Cancellation was requested via libusb_cancel_transfer() */
405
+ USBI_TRANSFER_CANCELLING = 1 << 2,
406
+
407
+ /* Operation on the transfer failed because the device disappeared */
408
+ USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
409
+
410
+ /* Set by backend submit_transfer() if the fds in use have been updated */
411
+ USBI_TRANSFER_UPDATED_FDS = 1 << 4,
412
+ };
413
+
414
+ #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
415
+ ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
416
+ + sizeof(struct usbi_transfer)))
417
+ #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
418
+ ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
419
+ - sizeof(struct usbi_transfer)))
420
+
421
+ static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
422
+ {
423
+ return ((unsigned char *)transfer) + sizeof(struct usbi_transfer)
424
+ + sizeof(struct libusb_transfer)
425
+ + (transfer->num_iso_packets
426
+ * sizeof(struct libusb_iso_packet_descriptor));
427
+ }
428
+
429
+ /* bus structures */
430
+
431
+ /* All standard descriptors have these 2 fields in common */
432
+ struct usb_descriptor_header {
433
+ uint8_t bLength;
434
+ uint8_t bDescriptorType;
435
+ };
436
+
437
+ /* shared data and functions */
438
+
439
+ int usbi_io_init(struct libusb_context *ctx);
440
+ void usbi_io_exit(struct libusb_context *ctx);
441
+
442
+ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
443
+ unsigned long session_id);
444
+ struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
445
+ unsigned long session_id);
446
+ int usbi_sanitize_device(struct libusb_device *dev);
447
+ void usbi_handle_disconnect(struct libusb_device_handle *handle);
448
+
449
+ int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
450
+ enum libusb_transfer_status status);
451
+ int usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
452
+
453
+ int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
454
+ void *dest, int host_endian);
455
+ int usbi_device_cache_descriptor(libusb_device *dev);
456
+ int usbi_get_config_index_by_value(struct libusb_device *dev,
457
+ uint8_t bConfigurationValue, int *idx);
458
+
459
+ void usbi_connect_device (struct libusb_device *dev);
460
+ void usbi_disconnect_device (struct libusb_device *dev);
461
+
462
+ /* Internal abstraction for poll (needs struct usbi_transfer on Windows) */
463
+ #if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD)
464
+ #include <unistd.h>
465
+ #include "os/poll_posix.h"
466
+ #elif defined(OS_WINDOWS) || defined(OS_WINCE)
467
+ #include "os/poll_windows.h"
468
+ #endif
469
+
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
+ struct usbi_pollfd {
484
+ /* must come first */
485
+ struct libusb_pollfd pollfd;
486
+
487
+ struct list_head list;
488
+ };
489
+
490
+ int usbi_add_pollfd(struct libusb_context *ctx, int fd, short events);
491
+ void usbi_remove_pollfd(struct libusb_context *ctx, int fd);
492
+ void usbi_fd_notification(struct libusb_context *ctx);
493
+
494
+ /* device discovery */
495
+
496
+ /* we traverse usbfs without knowing how many devices we are going to find.
497
+ * so we create this discovered_devs model which is similar to a linked-list
498
+ * which grows when required. it can be freed once discovery has completed,
499
+ * eliminating the need for a list node in the libusb_device structure
500
+ * itself. */
501
+ struct discovered_devs {
502
+ size_t len;
503
+ 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
+ ;
511
+ };
512
+
513
+ struct discovered_devs *discovered_devs_append(
514
+ struct discovered_devs *discdevs, struct libusb_device *dev);
515
+
516
+ /* OS abstraction */
517
+
518
+ /* This is the interface that OS backends need to implement.
519
+ * All fields are mandatory, except ones explicitly noted as optional. */
520
+ struct usbi_os_backend {
521
+ /* A human-readable name for your backend, e.g. "Linux usbfs" */
522
+ const char *name;
523
+
524
+ /* Binary mask for backend specific capabilities */
525
+ uint32_t caps;
526
+
527
+ /* Perform initialization of your backend. You might use this function
528
+ * to determine specific capabilities of the system, allocate required
529
+ * data structures for later, etc.
530
+ *
531
+ * This function is called when a libusb user initializes the library
532
+ * prior to use.
533
+ *
534
+ * Return 0 on success, or a LIBUSB_ERROR code on failure.
535
+ */
536
+ int (*init)(struct libusb_context *ctx);
537
+
538
+ /* Deinitialization. Optional. This function should destroy anything
539
+ * that was set up by init.
540
+ *
541
+ * This function is called when the user deinitializes the library.
542
+ */
543
+ void (*exit)(void);
544
+
545
+ /* Enumerate all the USB devices on the system, returning them in a list
546
+ * of discovered devices.
547
+ *
548
+ * Your implementation should enumerate all devices on the system,
549
+ * regardless of whether they have been seen before or not.
550
+ *
551
+ * When you have found a device, compute a session ID for it. The session
552
+ * ID should uniquely represent that particular device for that particular
553
+ * connection session since boot (i.e. if you disconnect and reconnect a
554
+ * device immediately after, it should be assigned a different session ID).
555
+ * If your OS cannot provide a unique session ID as described above,
556
+ * presenting a session ID of (bus_number << 8 | device_address) should
557
+ * be sufficient. Bus numbers and device addresses wrap and get reused,
558
+ * but that is an unlikely case.
559
+ *
560
+ * After computing a session ID for a device, call
561
+ * usbi_get_device_by_session_id(). This function checks if libusb already
562
+ * knows about the device, and if so, it provides you with a reference
563
+ * to a libusb_device structure for it.
564
+ *
565
+ * If usbi_get_device_by_session_id() returns NULL, it is time to allocate
566
+ * a new device structure for the device. Call usbi_alloc_device() to
567
+ * obtain a new libusb_device structure with reference count 1. Populate
568
+ * the bus_number and device_address attributes of the new device, and
569
+ * perform any other internal backend initialization you need to do. At
570
+ * this point, you should be ready to provide device descriptors and so
571
+ * on through the get_*_descriptor functions. Finally, call
572
+ * usbi_sanitize_device() to perform some final sanity checks on the
573
+ * device. Assuming all of the above succeeded, we can now continue.
574
+ * If any of the above failed, remember to unreference the device that
575
+ * was returned by usbi_alloc_device().
576
+ *
577
+ * At this stage we have a populated libusb_device structure (either one
578
+ * that was found earlier, or one that we have just allocated and
579
+ * populated). This can now be added to the discovered devices list
580
+ * using discovered_devs_append(). Note that discovered_devs_append()
581
+ * may reallocate the list, returning a new location for it, and also
582
+ * note that reallocation can fail. Your backend should handle these
583
+ * error conditions appropriately.
584
+ *
585
+ * This function should not generate any bus I/O and should not block.
586
+ * If I/O is required (e.g. reading the active configuration value), it is
587
+ * OK to ignore these suggestions :)
588
+ *
589
+ * This function is executed when the user wishes to retrieve a list
590
+ * of USB devices connected to the system.
591
+ *
592
+ * If the backend has hotplug support, this function is not used!
593
+ *
594
+ * Return 0 on success, or a LIBUSB_ERROR code on failure.
595
+ */
596
+ int (*get_device_list)(struct libusb_context *ctx,
597
+ struct discovered_devs **discdevs);
598
+
599
+ /* Apps which were written before hotplug support, may listen for
600
+ * hotplug events on their own and call libusb_get_device_list on
601
+ * device addition. In this case libusb_get_device_list will likely
602
+ * return a list without the new device in there, as the hotplug
603
+ * event thread will still be busy enumerating the device, which may
604
+ * take a while, or may not even have seen the event yet.
605
+ *
606
+ * To avoid this libusb_get_device_list will call this optional
607
+ * function for backends with hotplug support before copying
608
+ * ctx->usb_devs to the user. In this function the backend should
609
+ * ensure any pending hotplug events are fully processed before
610
+ * returning.
611
+ *
612
+ * Optional, should be implemented by backends with hotplug support.
613
+ */
614
+ void (*hotplug_poll)(void);
615
+
616
+ /* Open a device for I/O and other USB operations. The device handle
617
+ * is preallocated for you, you can retrieve the device in question
618
+ * through handle->dev.
619
+ *
620
+ * Your backend should allocate any internal resources required for I/O
621
+ * and other operations so that those operations can happen (hopefully)
622
+ * without hiccup. This is also a good place to inform libusb that it
623
+ * should monitor certain file descriptors related to this device -
624
+ * see the usbi_add_pollfd() function.
625
+ *
626
+ * This function should not generate any bus I/O and should not block.
627
+ *
628
+ * This function is called when the user attempts to obtain a device
629
+ * handle for a device.
630
+ *
631
+ * Return:
632
+ * - 0 on success
633
+ * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
634
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since
635
+ * discovery
636
+ * - another LIBUSB_ERROR code on other failure
637
+ *
638
+ * Do not worry about freeing the handle on failed open, the upper layers
639
+ * do this for you.
640
+ */
641
+ int (*open)(struct libusb_device_handle *handle);
642
+
643
+ /* Close a device such that the handle cannot be used again. Your backend
644
+ * should destroy any resources that were allocated in the open path.
645
+ * This may also be a good place to call usbi_remove_pollfd() to inform
646
+ * libusb of any file descriptors associated with this device that should
647
+ * no longer be monitored.
648
+ *
649
+ * This function is called when the user closes a device handle.
650
+ */
651
+ void (*close)(struct libusb_device_handle *handle);
652
+
653
+ /* Retrieve the device descriptor from a device.
654
+ *
655
+ * The descriptor should be retrieved from memory, NOT via bus I/O to the
656
+ * device. This means that you may have to cache it in a private structure
657
+ * during get_device_list enumeration. Alternatively, you may be able
658
+ * to retrieve it from a kernel interface (some Linux setups can do this)
659
+ * still without generating bus I/O.
660
+ *
661
+ * This function is expected to write DEVICE_DESC_LENGTH (18) bytes into
662
+ * buffer, which is guaranteed to be big enough.
663
+ *
664
+ * This function is called when sanity-checking a device before adding
665
+ * it to the list of discovered devices, and also when the user requests
666
+ * to read the device descriptor.
667
+ *
668
+ * This function is expected to return the descriptor in bus-endian format
669
+ * (LE). If it returns the multi-byte values in host-endian format,
670
+ * set the host_endian output parameter to "1".
671
+ *
672
+ * Return 0 on success or a LIBUSB_ERROR code on failure.
673
+ */
674
+ int (*get_device_descriptor)(struct libusb_device *device,
675
+ unsigned char *buffer, int *host_endian);
676
+
677
+ /* Get the ACTIVE configuration descriptor for a device.
678
+ *
679
+ * The descriptor should be retrieved from memory, NOT via bus I/O to the
680
+ * device. This means that you may have to cache it in a private structure
681
+ * during get_device_list enumeration. You may also have to keep track
682
+ * of which configuration is active when the user changes it.
683
+ *
684
+ * This function is expected to write len bytes of data into buffer, which
685
+ * is guaranteed to be big enough. If you can only do a partial write,
686
+ * return an error code.
687
+ *
688
+ * This function is expected to return the descriptor in bus-endian format
689
+ * (LE). If it returns the multi-byte values in host-endian format,
690
+ * set the host_endian output parameter to "1".
691
+ *
692
+ * Return:
693
+ * - 0 on success
694
+ * - LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
695
+ * - another LIBUSB_ERROR code on other failure
696
+ */
697
+ int (*get_active_config_descriptor)(struct libusb_device *device,
698
+ unsigned char *buffer, size_t len, int *host_endian);
699
+
700
+ /* Get a specific configuration descriptor for a device.
701
+ *
702
+ * The descriptor should be retrieved from memory, NOT via bus I/O to the
703
+ * device. This means that you may have to cache it in a private structure
704
+ * during get_device_list enumeration.
705
+ *
706
+ * The requested descriptor is expressed as a zero-based index (i.e. 0
707
+ * indicates that we are requesting the first descriptor). The index does
708
+ * not (necessarily) equal the bConfigurationValue of the configuration
709
+ * being requested.
710
+ *
711
+ * This function is expected to write len bytes of data into buffer, which
712
+ * is guaranteed to be big enough. If you can only do a partial write,
713
+ * return an error code.
714
+ *
715
+ * This function is expected to return the descriptor in bus-endian format
716
+ * (LE). If it returns the multi-byte values in host-endian format,
717
+ * set the host_endian output parameter to "1".
718
+ *
719
+ * Return the length read on success or a LIBUSB_ERROR code on failure.
720
+ */
721
+ int (*get_config_descriptor)(struct libusb_device *device,
722
+ uint8_t config_index, unsigned char *buffer, size_t len,
723
+ int *host_endian);
724
+
725
+ /* Like get_config_descriptor but then by bConfigurationValue instead
726
+ * of by index.
727
+ *
728
+ * Optional, if not present the core will call get_config_descriptor
729
+ * for all configs until it finds the desired bConfigurationValue.
730
+ *
731
+ * Returns a pointer to the raw-descriptor in *buffer, this memory
732
+ * is valid as long as device is valid.
733
+ *
734
+ * Returns the length of the returned raw-descriptor on success,
735
+ * or a LIBUSB_ERROR code on failure.
736
+ */
737
+ int (*get_config_descriptor_by_value)(struct libusb_device *device,
738
+ uint8_t bConfigurationValue, unsigned char **buffer,
739
+ int *host_endian);
740
+
741
+ /* Get the bConfigurationValue for the active configuration for a device.
742
+ * Optional. This should only be implemented if you can retrieve it from
743
+ * cache (don't generate I/O).
744
+ *
745
+ * If you cannot retrieve this from cache, either do not implement this
746
+ * function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause
747
+ * libusb to retrieve the information through a standard control transfer.
748
+ *
749
+ * This function must be non-blocking.
750
+ * Return:
751
+ * - 0 on success
752
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
753
+ * was opened
754
+ * - LIBUSB_ERROR_NOT_SUPPORTED if the value cannot be retrieved without
755
+ * blocking
756
+ * - another LIBUSB_ERROR code on other failure.
757
+ */
758
+ int (*get_configuration)(struct libusb_device_handle *handle, int *config);
759
+
760
+ /* Set the active configuration for a device.
761
+ *
762
+ * A configuration value of -1 should put the device in unconfigured state.
763
+ *
764
+ * This function can block.
765
+ *
766
+ * Return:
767
+ * - 0 on success
768
+ * - LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
769
+ * - LIBUSB_ERROR_BUSY if interfaces are currently claimed (and hence
770
+ * configuration cannot be changed)
771
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
772
+ * was opened
773
+ * - another LIBUSB_ERROR code on other failure.
774
+ */
775
+ int (*set_configuration)(struct libusb_device_handle *handle, int config);
776
+
777
+ /* Claim an interface. When claimed, the application can then perform
778
+ * I/O to an interface's endpoints.
779
+ *
780
+ * This function should not generate any bus I/O and should not block.
781
+ * Interface claiming is a logical operation that simply ensures that
782
+ * no other drivers/applications are using the interface, and after
783
+ * claiming, no other drivers/applicatiosn can use the interface because
784
+ * we now "own" it.
785
+ *
786
+ * Return:
787
+ * - 0 on success
788
+ * - LIBUSB_ERROR_NOT_FOUND if the interface does not exist
789
+ * - LIBUSB_ERROR_BUSY if the interface is in use by another driver/app
790
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
791
+ * was opened
792
+ * - another LIBUSB_ERROR code on other failure
793
+ */
794
+ int (*claim_interface)(struct libusb_device_handle *handle, int interface_number);
795
+
796
+ /* Release a previously claimed interface.
797
+ *
798
+ * This function should also generate a SET_INTERFACE control request,
799
+ * resetting the alternate setting of that interface to 0. It's OK for
800
+ * this function to block as a result.
801
+ *
802
+ * You will only ever be asked to release an interface which was
803
+ * successfully claimed earlier.
804
+ *
805
+ * Return:
806
+ * - 0 on success
807
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
808
+ * was opened
809
+ * - another LIBUSB_ERROR code on other failure
810
+ */
811
+ int (*release_interface)(struct libusb_device_handle *handle, int interface_number);
812
+
813
+ /* Set the alternate setting for an interface.
814
+ *
815
+ * You will only ever be asked to set the alternate setting for an
816
+ * interface which was successfully claimed earlier.
817
+ *
818
+ * It's OK for this function to block.
819
+ *
820
+ * Return:
821
+ * - 0 on success
822
+ * - LIBUSB_ERROR_NOT_FOUND if the alternate setting does not exist
823
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
824
+ * was opened
825
+ * - another LIBUSB_ERROR code on other failure
826
+ */
827
+ int (*set_interface_altsetting)(struct libusb_device_handle *handle,
828
+ int interface_number, int altsetting);
829
+
830
+ /* Clear a halt/stall condition on an endpoint.
831
+ *
832
+ * It's OK for this function to block.
833
+ *
834
+ * Return:
835
+ * - 0 on success
836
+ * - LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
837
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
838
+ * was opened
839
+ * - another LIBUSB_ERROR code on other failure
840
+ */
841
+ int (*clear_halt)(struct libusb_device_handle *handle,
842
+ unsigned char endpoint);
843
+
844
+ /* Perform a USB port reset to reinitialize a device.
845
+ *
846
+ * If possible, the handle should still be usable after the reset
847
+ * completes, assuming that the device descriptors did not change during
848
+ * reset and all previous interface state can be restored.
849
+ *
850
+ * If something changes, or you cannot easily locate/verify the resetted
851
+ * device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application
852
+ * to close the old handle and re-enumerate the device.
853
+ *
854
+ * Return:
855
+ * - 0 on success
856
+ * - LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the device
857
+ * has been disconnected since it was opened
858
+ * - another LIBUSB_ERROR code on other failure
859
+ */
860
+ int (*reset_device)(struct libusb_device_handle *handle);
861
+
862
+ /* Alloc num_streams usb3 bulk streams on the passed in endpoints */
863
+ int (*alloc_streams)(struct libusb_device_handle *handle,
864
+ uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
865
+
866
+ /* Free usb3 bulk streams allocated with alloc_streams */
867
+ int (*free_streams)(struct libusb_device_handle *handle,
868
+ unsigned char *endpoints, int num_endpoints);
869
+
870
+ /* Determine if a kernel driver is active on an interface. Optional.
871
+ *
872
+ * The presence of a kernel driver on an interface indicates that any
873
+ * calls to claim_interface would fail with the LIBUSB_ERROR_BUSY code.
874
+ *
875
+ * Return:
876
+ * - 0 if no driver is active
877
+ * - 1 if a driver is active
878
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
879
+ * was opened
880
+ * - another LIBUSB_ERROR code on other failure
881
+ */
882
+ int (*kernel_driver_active)(struct libusb_device_handle *handle,
883
+ int interface_number);
884
+
885
+ /* Detach a kernel driver from an interface. Optional.
886
+ *
887
+ * After detaching a kernel driver, the interface should be available
888
+ * for claim.
889
+ *
890
+ * Return:
891
+ * - 0 on success
892
+ * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
893
+ * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
894
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
895
+ * was opened
896
+ * - another LIBUSB_ERROR code on other failure
897
+ */
898
+ int (*detach_kernel_driver)(struct libusb_device_handle *handle,
899
+ int interface_number);
900
+
901
+ /* Attach a kernel driver to an interface. Optional.
902
+ *
903
+ * Reattach a kernel driver to the device.
904
+ *
905
+ * Return:
906
+ * - 0 on success
907
+ * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
908
+ * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
909
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
910
+ * was opened
911
+ * - LIBUSB_ERROR_BUSY if a program or driver has claimed the interface,
912
+ * preventing reattachment
913
+ * - another LIBUSB_ERROR code on other failure
914
+ */
915
+ int (*attach_kernel_driver)(struct libusb_device_handle *handle,
916
+ int interface_number);
917
+
918
+ /* Destroy a device. Optional.
919
+ *
920
+ * This function is called when the last reference to a device is
921
+ * destroyed. It should free any resources allocated in the get_device_list
922
+ * path.
923
+ */
924
+ void (*destroy_device)(struct libusb_device *dev);
925
+
926
+ /* Submit a transfer. Your implementation should take the transfer,
927
+ * morph it into whatever form your platform requires, and submit it
928
+ * asynchronously.
929
+ *
930
+ * This function must not block.
931
+ *
932
+ * This function gets called with the flying_transfers_lock locked!
933
+ *
934
+ * Return:
935
+ * - 0 on success
936
+ * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
937
+ * - another LIBUSB_ERROR code on other failure
938
+ */
939
+ int (*submit_transfer)(struct usbi_transfer *itransfer);
940
+
941
+ /* Cancel a previously submitted transfer.
942
+ *
943
+ * This function must not block. The transfer cancellation must complete
944
+ * later, resulting in a call to usbi_handle_transfer_cancellation()
945
+ * from the context of handle_events.
946
+ */
947
+ int (*cancel_transfer)(struct usbi_transfer *itransfer);
948
+
949
+ /* Clear a transfer as if it has completed or cancelled, but do not
950
+ * report any completion/cancellation to the library. You should free
951
+ * all private data from the transfer as if you were just about to report
952
+ * completion or cancellation.
953
+ *
954
+ * This function might seem a bit out of place. It is used when libusb
955
+ * detects a disconnected device - it calls this function for all pending
956
+ * transfers before reporting completion (with the disconnect code) to
957
+ * the user. Maybe we can improve upon this internal interface in future.
958
+ */
959
+ void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
960
+
961
+ /* Handle any pending events. This involves monitoring any active
962
+ * transfers and processing their completion or cancellation.
963
+ *
964
+ * The function is passed an array of pollfd structures (size nfds)
965
+ * as a result of the poll() system call. The num_ready parameter
966
+ * indicates the number of file descriptors that have reported events
967
+ * (i.e. the poll() return value). This should be enough information
968
+ * for you to determine which actions need to be taken on the currently
969
+ * active transfers.
970
+ *
971
+ * For any cancelled transfers, call usbi_handle_transfer_cancellation().
972
+ * For completed transfers, call usbi_handle_transfer_completion().
973
+ * For control/bulk/interrupt transfers, populate the "transferred"
974
+ * element of the appropriate usbi_transfer structure before calling the
975
+ * above functions. For isochronous transfers, populate the status and
976
+ * transferred fields of the iso packet descriptors of the transfer.
977
+ *
978
+ * This function should also be able to detect disconnection of the
979
+ * device, reporting that situation with usbi_handle_disconnect().
980
+ *
981
+ * When processing an event related to a transfer, you probably want to
982
+ * take usbi_transfer.lock to prevent races. See the documentation for
983
+ * the usbi_transfer structure.
984
+ *
985
+ * Return 0 on success, or a LIBUSB_ERROR code on failure.
986
+ */
987
+ int (*handle_events)(struct libusb_context *ctx,
988
+ struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
989
+
990
+ /* Get time from specified clock. At least two clocks must be implemented
991
+ by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
992
+
993
+ Description of clocks:
994
+ USBI_CLOCK_REALTIME : clock returns time since system epoch.
995
+ USBI_CLOCK_MONOTONIC: clock returns time since unspecified start
996
+ time (usually boot).
997
+ */
998
+ int (*clock_gettime)(int clkid, struct timespec *tp);
999
+
1000
+ #ifdef USBI_TIMERFD_AVAILABLE
1001
+ /* clock ID of the clock that should be used for timerfd */
1002
+ clockid_t (*get_timerfd_clockid)(void);
1003
+ #endif
1004
+
1005
+ /* Number of bytes to reserve for per-device private backend data.
1006
+ * This private data area is accessible through the "os_priv" field of
1007
+ * struct libusb_device. */
1008
+ size_t device_priv_size;
1009
+
1010
+ /* Number of bytes to reserve for per-handle private backend data.
1011
+ * This private data area is accessible through the "os_priv" field of
1012
+ * struct libusb_device. */
1013
+ size_t device_handle_priv_size;
1014
+
1015
+ /* Number of bytes to reserve for per-transfer private backend data.
1016
+ * This private data area is accessible by calling
1017
+ * usbi_transfer_get_os_priv() on the appropriate usbi_transfer instance.
1018
+ */
1019
+ 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
+ };
1027
+
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;
1036
+
1037
+ extern struct list_head active_contexts_list;
1038
+ extern usbi_mutex_static_t active_contexts_lock;
1039
+
1040
+ #endif