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,796 @@
1
+ /*
2
+ * poll_windows: poll compatibility wrapper for Windows
3
+ * Copyright © 2012-2013 RealVNC Ltd.
4
+ * Copyright © 2009-2010 Pete Batard <pete@akeo.ie>
5
+ * With contributions from Michael Plante, Orin Eman et al.
6
+ * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
7
+ *
8
+ * This library is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * This library is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with this library; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ *
22
+ */
23
+
24
+ /*
25
+ * poll() and pipe() Windows compatibility layer for libusb 1.0
26
+ *
27
+ * The way this layer works is by using OVERLAPPED with async I/O transfers, as
28
+ * OVERLAPPED have an associated event which is flagged for I/O completion.
29
+ *
30
+ * For USB pollable async I/O, you would typically:
31
+ * - obtain a Windows HANDLE to a file or device that has been opened in
32
+ * OVERLAPPED mode
33
+ * - call usbi_create_fd with this handle to obtain a custom fd.
34
+ * Note that if you need simultaneous R/W access, you need to call create_fd
35
+ * twice, once in RW_READ and once in RW_WRITE mode to obtain 2 separate
36
+ * pollable fds
37
+ * - leave the core functions call the poll routine and flag POLLIN/POLLOUT
38
+ *
39
+ * The pipe pollable synchronous I/O works using the overlapped event associated
40
+ * with a fake pipe. The read/write functions are only meant to be used in that
41
+ * context.
42
+ */
43
+ #include <errno.h>
44
+ #include <stdio.h>
45
+ #include <stdlib.h>
46
+
47
+ #include "libusbi.h"
48
+
49
+ // Uncomment to debug the polling layer
50
+ //#define DEBUG_POLL_WINDOWS
51
+ #if defined(DEBUG_POLL_WINDOWS)
52
+ #define poll_dbg usbi_dbg
53
+ #else
54
+ // MSVC++ < 2005 cannot use a variadic argument and non MSVC
55
+ // compilers produce warnings if parenthesis are ommitted.
56
+ #if defined(_MSC_VER) && (_MSC_VER < 1400)
57
+ #define poll_dbg
58
+ #else
59
+ #define poll_dbg(...)
60
+ #endif
61
+ #endif
62
+
63
+ #if defined(_PREFAST_)
64
+ #pragma warning(disable:28719)
65
+ #endif
66
+
67
+ #define CHECK_INIT_POLLING do {if(!is_polling_set) init_polling();} while(0)
68
+
69
+ // public fd data
70
+ const struct winfd INVALID_WINFD = {-1, INVALID_HANDLE_VALUE, NULL, NULL, NULL, RW_NONE};
71
+ struct winfd poll_fd[MAX_FDS];
72
+
73
+ struct pipe_data {
74
+ struct list_head list;
75
+ unsigned char * data;
76
+ size_t count;
77
+ };
78
+
79
+ // internal fd data
80
+ struct {
81
+ CRITICAL_SECTION mutex; // lock for fds
82
+ // Additional variables for XP CancelIoEx partial emulation
83
+ HANDLE original_handle;
84
+ DWORD thread_id;
85
+ struct list_head list; // used for sending data between threads
86
+ } _poll_fd[MAX_FDS];
87
+
88
+ // globals
89
+ BOOLEAN is_polling_set = FALSE;
90
+ LONG pipe_number = 0;
91
+ static volatile LONG compat_spinlock = 0;
92
+
93
+ #if !defined(_WIN32_WCE)
94
+ // CancelIoEx, available on Vista and later only, provides the ability to cancel
95
+ // a single transfer (OVERLAPPED) when used. As it may not be part of any of the
96
+ // platform headers, we hook into the Kernel32 system DLL directly to seek it.
97
+ static BOOL (__stdcall *pCancelIoEx)(HANDLE, LPOVERLAPPED) = NULL;
98
+ #define Use_Duplicate_Handles (pCancelIoEx == NULL)
99
+
100
+ static inline void setup_cancel_io(void)
101
+ {
102
+ HMODULE hKernel32 = GetModuleHandleA("KERNEL32");
103
+ if (hKernel32 != NULL) {
104
+ pCancelIoEx = (BOOL (__stdcall *)(HANDLE,LPOVERLAPPED))
105
+ GetProcAddress(hKernel32, "CancelIoEx");
106
+ }
107
+ usbi_dbg("Will use CancelIo%s for I/O cancellation",
108
+ Use_Duplicate_Handles?"":"Ex");
109
+ }
110
+
111
+ static inline BOOL cancel_io(int _index)
112
+ {
113
+ if ((_index < 0) || (_index >= MAX_FDS)) {
114
+ return FALSE;
115
+ }
116
+
117
+ if ( (poll_fd[_index].fd < 0) || (poll_fd[_index].handle == INVALID_HANDLE_VALUE)
118
+ || (poll_fd[_index].handle == 0) || (poll_fd[_index].overlapped == NULL) ) {
119
+ return TRUE;
120
+ }
121
+ if (poll_fd[_index].itransfer && poll_fd[_index].cancel_fn) {
122
+ // Cancel outstanding transfer via the specific callback
123
+ (*poll_fd[_index].cancel_fn)(poll_fd[_index].itransfer);
124
+ return TRUE;
125
+ }
126
+ if (pCancelIoEx != NULL) {
127
+ return (*pCancelIoEx)(poll_fd[_index].handle, poll_fd[_index].overlapped);
128
+ }
129
+ if (_poll_fd[_index].thread_id == GetCurrentThreadId()) {
130
+ return CancelIo(poll_fd[_index].handle);
131
+ }
132
+ usbi_warn(NULL, "Unable to cancel I/O that was started from another thread");
133
+ return FALSE;
134
+ }
135
+ #else
136
+ #define Use_Duplicate_Handles FALSE
137
+
138
+ static __inline void setup_cancel_io()
139
+ {
140
+ // No setup needed on WinCE
141
+ }
142
+
143
+ static __inline BOOL cancel_io(int _index)
144
+ {
145
+ if ((_index < 0) || (_index >= MAX_FDS)) {
146
+ return FALSE;
147
+ }
148
+ if ( (poll_fd[_index].fd < 0) || (poll_fd[_index].handle == INVALID_HANDLE_VALUE)
149
+ || (poll_fd[_index].handle == 0) || (poll_fd[_index].overlapped == NULL) ) {
150
+ return TRUE;
151
+ }
152
+ if (poll_fd[_index].itransfer && poll_fd[_index].cancel_fn) {
153
+ // Cancel outstanding transfer via the specific callback
154
+ (*poll_fd[_index].cancel_fn)(poll_fd[_index].itransfer);
155
+ }
156
+ return TRUE;
157
+ }
158
+ #endif
159
+
160
+ // Init
161
+ void init_polling(void)
162
+ {
163
+ int i;
164
+
165
+ while (InterlockedExchange((LONG *)&compat_spinlock, 1) == 1) {
166
+ SleepEx(0, TRUE);
167
+ }
168
+ if (!is_polling_set) {
169
+ setup_cancel_io();
170
+ for (i=0; i<MAX_FDS; i++) {
171
+ poll_fd[i] = INVALID_WINFD;
172
+ _poll_fd[i].original_handle = INVALID_HANDLE_VALUE;
173
+ _poll_fd[i].thread_id = 0;
174
+ list_init(&_poll_fd[i].list);
175
+ InitializeCriticalSection(&_poll_fd[i].mutex);
176
+ }
177
+ is_polling_set = TRUE;
178
+ }
179
+ InterlockedExchange((LONG *)&compat_spinlock, 0);
180
+ }
181
+
182
+ // Internal function to retrieve the table index (and lock the fd mutex)
183
+ static int _fd_to_index_and_lock(int fd)
184
+ {
185
+ int i;
186
+
187
+ if (fd < 0)
188
+ return -1;
189
+
190
+ for (i=0; i<MAX_FDS; i++) {
191
+ if (poll_fd[i].fd == fd) {
192
+ EnterCriticalSection(&_poll_fd[i].mutex);
193
+ // fd might have changed before we got to critical
194
+ if (poll_fd[i].fd != fd) {
195
+ LeaveCriticalSection(&_poll_fd[i].mutex);
196
+ continue;
197
+ }
198
+ return i;
199
+ }
200
+ }
201
+ return -1;
202
+ }
203
+
204
+ static OVERLAPPED *create_overlapped(void)
205
+ {
206
+ OVERLAPPED *overlapped = (OVERLAPPED*) calloc(1, sizeof(OVERLAPPED));
207
+ if (overlapped == NULL) {
208
+ return NULL;
209
+ }
210
+ overlapped->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
211
+ if(overlapped->hEvent == NULL) {
212
+ free (overlapped);
213
+ return NULL;
214
+ }
215
+ return overlapped;
216
+ }
217
+
218
+ static void free_overlapped(OVERLAPPED *overlapped)
219
+ {
220
+ if (overlapped == NULL)
221
+ return;
222
+
223
+ if ( (overlapped->hEvent != 0)
224
+ && (overlapped->hEvent != INVALID_HANDLE_VALUE) ) {
225
+ CloseHandle(overlapped->hEvent);
226
+ }
227
+ free(overlapped);
228
+ }
229
+
230
+ void exit_polling(void)
231
+ {
232
+ struct pipe_data* item;
233
+ int i;
234
+
235
+ while (InterlockedExchange((LONG *)&compat_spinlock, 1) == 1) {
236
+ SleepEx(0, TRUE);
237
+ }
238
+ if (is_polling_set) {
239
+ is_polling_set = FALSE;
240
+
241
+ for (i=0; i<MAX_FDS; i++) {
242
+ // Cancel any async I/O (handle can be invalid)
243
+ cancel_io(i);
244
+ // If anything was pending on that I/O, it should be
245
+ // terminating, and we should be able to access the fd
246
+ // mutex lock before too long
247
+ EnterCriticalSection(&_poll_fd[i].mutex);
248
+ free_overlapped(poll_fd[i].overlapped);
249
+ if (Use_Duplicate_Handles) {
250
+ // Close duplicate handle
251
+ if (_poll_fd[i].original_handle != INVALID_HANDLE_VALUE) {
252
+ CloseHandle(poll_fd[i].handle);
253
+ }
254
+ }
255
+ if (!list_empty(&_poll_fd[i].list))
256
+ usbi_warn(NULL, "There are some pending events in the queue");
257
+ list_for_each_entry(item, &_poll_fd[i].list, list, struct pipe_data) {
258
+ if (!item) {
259
+ usbi_err(NULL, "no item to free");
260
+ continue;
261
+ }
262
+ if (item->data)
263
+ free(item->data);
264
+
265
+ free(item);
266
+ }
267
+ poll_fd[i] = INVALID_WINFD;
268
+ LeaveCriticalSection(&_poll_fd[i].mutex);
269
+ DeleteCriticalSection(&_poll_fd[i].mutex);
270
+ }
271
+ }
272
+ InterlockedExchange((LONG *)&compat_spinlock, 0);
273
+ }
274
+
275
+ /*
276
+ * Create a fake pipe.
277
+ * As libusb only uses pipes for signaling, all we need from a pipe is an
278
+ * event. To that extent, we create a single wfd and overlapped as a means
279
+ * to access that event.
280
+ */
281
+ int usbi_pipe(int filedes[2])
282
+ {
283
+ int i;
284
+ OVERLAPPED* overlapped;
285
+
286
+ CHECK_INIT_POLLING;
287
+
288
+ overlapped = create_overlapped();
289
+
290
+ if (overlapped == NULL) {
291
+ return -1;
292
+ }
293
+ // The overlapped must have status pending for signaling to work in poll
294
+ overlapped->Internal = STATUS_PENDING;
295
+ overlapped->InternalHigh = 0;
296
+
297
+ for (i=0; i<MAX_FDS; i++) {
298
+ if (poll_fd[i].fd < 0) {
299
+ EnterCriticalSection(&_poll_fd[i].mutex);
300
+ // fd might have been allocated before we got to critical
301
+ if (poll_fd[i].fd >= 0) {
302
+ LeaveCriticalSection(&_poll_fd[i].mutex);
303
+ continue;
304
+ }
305
+
306
+ // Use index as the unique fd number
307
+ poll_fd[i].fd = i;
308
+ // Read end of the "pipe"
309
+ filedes[0] = poll_fd[i].fd;
310
+ // We can use the same handle for both ends
311
+ filedes[1] = filedes[0];
312
+
313
+ poll_fd[i].handle = DUMMY_HANDLE;
314
+ poll_fd[i].overlapped = overlapped;
315
+ // There's no polling on the write end, so we just use READ for our needs
316
+ poll_fd[i].rw = RW_READ;
317
+ _poll_fd[i].original_handle = INVALID_HANDLE_VALUE;
318
+ LeaveCriticalSection(&_poll_fd[i].mutex);
319
+ return 0;
320
+ }
321
+ }
322
+ free_overlapped(overlapped);
323
+ return -1;
324
+ }
325
+
326
+ /*
327
+ * Create both an fd and an OVERLAPPED from an open Windows handle, so that
328
+ * it can be used with our polling function
329
+ * The handle MUST support overlapped transfers (usually requires CreateFile
330
+ * with FILE_FLAG_OVERLAPPED)
331
+ * Return a pollable file descriptor struct, or INVALID_WINFD on error
332
+ *
333
+ * Note that the fd returned by this function is a per-transfer fd, rather
334
+ * than a per-session fd and cannot be used for anything else but our
335
+ * custom functions (the fd itself points to the NUL: device)
336
+ * if you plan to do R/W on the same handle, you MUST create 2 fds: one for
337
+ * read and one for write. Using a single R/W fd is unsupported and will
338
+ * produce unexpected results
339
+ */
340
+ struct winfd usbi_create_fd(HANDLE handle, int access_mode, struct usbi_transfer *itransfer, cancel_transfer *cancel_fn)
341
+ {
342
+ int i;
343
+ struct winfd wfd = INVALID_WINFD;
344
+ OVERLAPPED* overlapped = NULL;
345
+
346
+ CHECK_INIT_POLLING;
347
+
348
+ if ((handle == 0) || (handle == INVALID_HANDLE_VALUE)) {
349
+ return INVALID_WINFD;
350
+ }
351
+
352
+ wfd.itransfer = itransfer;
353
+ wfd.cancel_fn = cancel_fn;
354
+
355
+ if ((access_mode != RW_READ) && (access_mode != RW_WRITE)) {
356
+ usbi_warn(NULL, "only one of RW_READ or RW_WRITE are supported.\n"
357
+ "If you want to poll for R/W simultaneously, create multiple fds from the same handle.");
358
+ return INVALID_WINFD;
359
+ }
360
+ if (access_mode == RW_READ) {
361
+ wfd.rw = RW_READ;
362
+ } else {
363
+ wfd.rw = RW_WRITE;
364
+ }
365
+
366
+ overlapped = create_overlapped();
367
+ if(overlapped == NULL) {
368
+ return INVALID_WINFD;
369
+ }
370
+
371
+ for (i=0; i<MAX_FDS; i++) {
372
+ if (poll_fd[i].fd < 0) {
373
+ EnterCriticalSection(&_poll_fd[i].mutex);
374
+ // fd might have been removed before we got to critical
375
+ if (poll_fd[i].fd >= 0) {
376
+ LeaveCriticalSection(&_poll_fd[i].mutex);
377
+ continue;
378
+ }
379
+ // Use index as the unique fd number
380
+ wfd.fd = i;
381
+ // Attempt to emulate some of the CancelIoEx behaviour on platforms
382
+ // that don't have it
383
+ if (Use_Duplicate_Handles) {
384
+ _poll_fd[i].thread_id = GetCurrentThreadId();
385
+ if (!DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(),
386
+ &wfd.handle, 0, TRUE, DUPLICATE_SAME_ACCESS)) {
387
+ usbi_dbg("could not duplicate handle for CancelIo - using original one");
388
+ wfd.handle = handle;
389
+ // Make sure we won't close the original handle on fd deletion then
390
+ _poll_fd[i].original_handle = INVALID_HANDLE_VALUE;
391
+ } else {
392
+ _poll_fd[i].original_handle = handle;
393
+ }
394
+ } else {
395
+ wfd.handle = handle;
396
+ }
397
+ wfd.overlapped = overlapped;
398
+ memcpy(&poll_fd[i], &wfd, sizeof(struct winfd));
399
+ LeaveCriticalSection(&_poll_fd[i].mutex);
400
+ return wfd;
401
+ }
402
+ }
403
+ free_overlapped(overlapped);
404
+ return INVALID_WINFD;
405
+ }
406
+
407
+ static void _free_index(int _index)
408
+ {
409
+ struct pipe_data *item;
410
+
411
+ // Cancel any async IO (Don't care about the validity of our handles for this)
412
+ cancel_io(_index);
413
+ // close the duplicate handle (if we have an actual duplicate)
414
+ if (Use_Duplicate_Handles) {
415
+ if (_poll_fd[_index].original_handle != INVALID_HANDLE_VALUE) {
416
+ CloseHandle(poll_fd[_index].handle);
417
+ }
418
+ _poll_fd[_index].original_handle = INVALID_HANDLE_VALUE;
419
+ _poll_fd[_index].thread_id = 0;
420
+ if (!list_empty(&_poll_fd[_index].list))
421
+ usbi_warn(NULL, "There are some pending events in the queue");
422
+ list_for_each_entry(item, &_poll_fd[_index].list, list, struct pipe_data) {
423
+ if (!item) {
424
+ usbi_dbg("no item");
425
+ continue;
426
+ }
427
+ if (item->data)
428
+ free(item->data);
429
+ free(item);
430
+ }
431
+ }
432
+ free_overlapped(poll_fd[_index].overlapped);
433
+ poll_fd[_index] = INVALID_WINFD;
434
+ }
435
+
436
+ /*
437
+ * Release a pollable file descriptor.
438
+ *
439
+ * Note that the associated Windows handle is not closed by this call
440
+ */
441
+ void usbi_free_fd(struct winfd *wfd)
442
+ {
443
+ int _index;
444
+
445
+ CHECK_INIT_POLLING;
446
+
447
+ _index = _fd_to_index_and_lock(wfd->fd);
448
+ if (_index < 0) {
449
+ return;
450
+ }
451
+ _free_index(_index);
452
+ *wfd = INVALID_WINFD;
453
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
454
+ }
455
+
456
+ /*
457
+ * The functions below perform various conversions between fd, handle and OVERLAPPED
458
+ */
459
+ struct winfd fd_to_winfd(int fd)
460
+ {
461
+ int i;
462
+ struct winfd wfd;
463
+
464
+ CHECK_INIT_POLLING;
465
+
466
+ if (fd < 0)
467
+ return INVALID_WINFD;
468
+
469
+ for (i=0; i<MAX_FDS; i++) {
470
+ if (poll_fd[i].fd == fd) {
471
+ EnterCriticalSection(&_poll_fd[i].mutex);
472
+ // fd might have been deleted before we got to critical
473
+ if (poll_fd[i].fd != fd) {
474
+ LeaveCriticalSection(&_poll_fd[i].mutex);
475
+ continue;
476
+ }
477
+ memcpy(&wfd, &poll_fd[i], sizeof(struct winfd));
478
+ LeaveCriticalSection(&_poll_fd[i].mutex);
479
+ return wfd;
480
+ }
481
+ }
482
+ return INVALID_WINFD;
483
+ }
484
+
485
+ struct winfd handle_to_winfd(HANDLE handle)
486
+ {
487
+ int i;
488
+ struct winfd wfd;
489
+
490
+ CHECK_INIT_POLLING;
491
+
492
+ if ((handle == 0) || (handle == INVALID_HANDLE_VALUE))
493
+ return INVALID_WINFD;
494
+
495
+ for (i=0; i<MAX_FDS; i++) {
496
+ if (poll_fd[i].handle == handle) {
497
+ EnterCriticalSection(&_poll_fd[i].mutex);
498
+ // fd might have been deleted before we got to critical
499
+ if (poll_fd[i].handle != handle) {
500
+ LeaveCriticalSection(&_poll_fd[i].mutex);
501
+ continue;
502
+ }
503
+ memcpy(&wfd, &poll_fd[i], sizeof(struct winfd));
504
+ LeaveCriticalSection(&_poll_fd[i].mutex);
505
+ return wfd;
506
+ }
507
+ }
508
+ return INVALID_WINFD;
509
+ }
510
+
511
+ struct winfd overlapped_to_winfd(OVERLAPPED* overlapped)
512
+ {
513
+ int i;
514
+ struct winfd wfd;
515
+
516
+ CHECK_INIT_POLLING;
517
+
518
+ if (overlapped == NULL)
519
+ return INVALID_WINFD;
520
+
521
+ for (i=0; i<MAX_FDS; i++) {
522
+ if (poll_fd[i].overlapped == overlapped) {
523
+ EnterCriticalSection(&_poll_fd[i].mutex);
524
+ // fd might have been deleted before we got to critical
525
+ if (poll_fd[i].overlapped != overlapped) {
526
+ LeaveCriticalSection(&_poll_fd[i].mutex);
527
+ continue;
528
+ }
529
+ memcpy(&wfd, &poll_fd[i], sizeof(struct winfd));
530
+ LeaveCriticalSection(&_poll_fd[i].mutex);
531
+ return wfd;
532
+ }
533
+ }
534
+ return INVALID_WINFD;
535
+ }
536
+
537
+ /*
538
+ * POSIX poll equivalent, using Windows OVERLAPPED
539
+ * Currently, this function only accepts one of POLLIN or POLLOUT per fd
540
+ * (but you can create multiple fds from the same handle for read and write)
541
+ */
542
+ int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout)
543
+ {
544
+ unsigned i;
545
+ int _index, object_index, triggered;
546
+ HANDLE *handles_to_wait_on;
547
+ int *handle_to_index;
548
+ DWORD nb_handles_to_wait_on = 0;
549
+ DWORD ret;
550
+
551
+ CHECK_INIT_POLLING;
552
+
553
+ triggered = 0;
554
+ handles_to_wait_on = (HANDLE*) calloc(nfds+1, sizeof(HANDLE)); // +1 for fd_update
555
+ handle_to_index = (int*) calloc(nfds, sizeof(int));
556
+ if ((handles_to_wait_on == NULL) || (handle_to_index == NULL)) {
557
+ errno = ENOMEM;
558
+ triggered = -1;
559
+ goto poll_exit;
560
+ }
561
+
562
+ for (i = 0; i < nfds; ++i) {
563
+ fds[i].revents = 0;
564
+
565
+ // Only one of POLLIN or POLLOUT can be selected with this version of poll (not both)
566
+ if ((fds[i].events & ~POLLIN) && (!(fds[i].events & POLLOUT))) {
567
+ fds[i].revents |= POLLERR;
568
+ errno = EACCES;
569
+ usbi_warn(NULL, "unsupported set of events");
570
+ triggered = -1;
571
+ goto poll_exit;
572
+ }
573
+
574
+ _index = _fd_to_index_and_lock(fds[i].fd);
575
+ poll_dbg("fd[%d]=%d: (overlapped=%p) got events %04X", i, poll_fd[_index].fd, poll_fd[_index].overlapped, fds[i].events);
576
+
577
+ if ( (_index < 0) || (poll_fd[_index].handle == INVALID_HANDLE_VALUE)
578
+ || (poll_fd[_index].handle == 0) || (poll_fd[_index].overlapped == NULL)) {
579
+ fds[i].revents |= POLLNVAL | POLLERR;
580
+ errno = EBADF;
581
+ if (_index >= 0) {
582
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
583
+ }
584
+ usbi_warn(NULL, "invalid fd");
585
+ triggered = -1;
586
+ goto poll_exit;
587
+ }
588
+
589
+ // IN or OUT must match our fd direction
590
+ if ((fds[i].events & POLLIN) && (poll_fd[_index].rw != RW_READ)) {
591
+ fds[i].revents |= POLLNVAL | POLLERR;
592
+ errno = EBADF;
593
+ usbi_warn(NULL, "attempted POLLIN on fd without READ access");
594
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
595
+ triggered = -1;
596
+ goto poll_exit;
597
+ }
598
+
599
+ if ((fds[i].events & POLLOUT) && (poll_fd[_index].rw != RW_WRITE)) {
600
+ fds[i].revents |= POLLNVAL | POLLERR;
601
+ errno = EBADF;
602
+ usbi_warn(NULL, "attempted POLLOUT on fd without WRITE access");
603
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
604
+ triggered = -1;
605
+ goto poll_exit;
606
+ }
607
+
608
+ // The following macro only works if overlapped I/O was reported pending
609
+ if ( (HasOverlappedIoCompleted(poll_fd[_index].overlapped))
610
+ || (HasOverlappedIoCompletedSync(poll_fd[_index].overlapped)) ) {
611
+ poll_dbg(" completed");
612
+ // checks above should ensure this works:
613
+ fds[i].revents = fds[i].events;
614
+ triggered++;
615
+ } else {
616
+ handles_to_wait_on[nb_handles_to_wait_on] = poll_fd[_index].overlapped->hEvent;
617
+ handle_to_index[nb_handles_to_wait_on] = i;
618
+ nb_handles_to_wait_on++;
619
+ }
620
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
621
+ }
622
+
623
+ // If nothing was triggered, wait on all fds that require it
624
+ if ((timeout != 0) && (triggered == 0) && (nb_handles_to_wait_on != 0)) {
625
+ if (timeout < 0) {
626
+ poll_dbg("starting infinite wait for %d handles...", (int)nb_handles_to_wait_on);
627
+ } else {
628
+ poll_dbg("starting %d ms wait for %d handles...", timeout, (int)nb_handles_to_wait_on);
629
+ }
630
+ ret = WaitForMultipleObjects(nb_handles_to_wait_on, handles_to_wait_on,
631
+ FALSE, (timeout<0)?INFINITE:(DWORD)timeout);
632
+ object_index = ret-WAIT_OBJECT_0;
633
+ if ((object_index >= 0) && ((DWORD)object_index < nb_handles_to_wait_on)) {
634
+ poll_dbg(" completed after wait");
635
+ i = handle_to_index[object_index];
636
+ _index = _fd_to_index_and_lock(fds[i].fd);
637
+ fds[i].revents = fds[i].events;
638
+ triggered++;
639
+ if (_index >= 0) {
640
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
641
+ }
642
+ } else if (ret == WAIT_TIMEOUT) {
643
+ poll_dbg(" timed out");
644
+ triggered = 0; // 0 = timeout
645
+ } else {
646
+ errno = EIO;
647
+ triggered = -1; // error
648
+ }
649
+ }
650
+
651
+ poll_exit:
652
+ if (handles_to_wait_on != NULL) {
653
+ free(handles_to_wait_on);
654
+ }
655
+ if (handle_to_index != NULL) {
656
+ free(handle_to_index);
657
+ }
658
+ return triggered;
659
+ }
660
+
661
+ /*
662
+ * close a fake pipe fd
663
+ */
664
+ int usbi_close(int fd)
665
+ {
666
+ int _index;
667
+ int r = -1;
668
+
669
+ CHECK_INIT_POLLING;
670
+
671
+ _index = _fd_to_index_and_lock(fd);
672
+
673
+ if (_index < 0) {
674
+ errno = EBADF;
675
+ } else {
676
+ free_overlapped(poll_fd[_index].overlapped);
677
+ poll_fd[_index] = INVALID_WINFD;
678
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
679
+ }
680
+ return r;
681
+ }
682
+
683
+ /*
684
+ * synchronous write for fake "pipe" signaling
685
+ */
686
+ ssize_t usbi_write(int fd, const void *buf, size_t count)
687
+ {
688
+ int _index, i;
689
+ struct pipe_data *item;
690
+ const unsigned char * cbuf = (unsigned char*) buf;
691
+
692
+ CHECK_INIT_POLLING;
693
+
694
+ _index = _fd_to_index_and_lock(fd);
695
+
696
+ if ( (_index < 0) || (poll_fd[_index].overlapped == NULL) ) {
697
+ errno = EBADF;
698
+ if (_index >= 0) {
699
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
700
+ }
701
+ return -1;
702
+ }
703
+
704
+ item = calloc(1, sizeof(*item));
705
+ item->data = calloc(sizeof(unsigned char), count+1);
706
+ item->count = count;
707
+ for(i = 0; i < count; i++) {
708
+ item->data[i] = cbuf[i];
709
+ }
710
+ list_add(&item->list, &_poll_fd[_index].list);
711
+
712
+ poll_dbg("set pipe event (fd = %d, thread = %08X)",
713
+ _index,
714
+ GetCurrentThreadId());
715
+ SetEvent(poll_fd[_index].overlapped->hEvent);
716
+ poll_fd[_index].overlapped->Internal = STATUS_WAIT_0;
717
+ // If two threads write on the pipe at the same time, we need to
718
+ // process two separate reads => use the overlapped as a counter
719
+ poll_fd[_index].overlapped->InternalHigh++;
720
+
721
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
722
+ return count;
723
+ }
724
+
725
+ /*
726
+ * synchronous read for fake "pipe" signaling
727
+ */
728
+ ssize_t usbi_read(int fd, void *buf, size_t count)
729
+ {
730
+ struct pipe_data * pdata;
731
+ int _index;
732
+ int res;
733
+ ssize_t r = -1;
734
+
735
+ CHECK_INIT_POLLING;
736
+
737
+ _index = _fd_to_index_and_lock(fd);
738
+
739
+ if (_index < 0) {
740
+ errno = EBADF;
741
+ return -1;
742
+ }
743
+
744
+ res = WaitForSingleObject(poll_fd[_index].overlapped->hEvent, INFINITE);
745
+ if (res != WAIT_OBJECT_0) {
746
+ usbi_warn(NULL, "waiting for event failed: %d", (int)GetLastError());
747
+ errno = EIO;
748
+ goto out;
749
+ }
750
+
751
+ poll_fd[_index].overlapped->InternalHigh--;
752
+ // Don't reset unless we don't have any more events to process
753
+ if (poll_fd[_index].overlapped->InternalHigh <= 0) {
754
+ ResetEvent(poll_fd[_index].overlapped->hEvent);
755
+ poll_fd[_index].overlapped->Internal = STATUS_PENDING;
756
+ }
757
+
758
+ poll_dbg("clr pipe event (fd = %d, thread = %08X)", _index, GetCurrentThreadId());
759
+
760
+
761
+ if (list_empty(&_poll_fd[_index].list)) {
762
+ usbi_warn(NULL, "no data in the poll");
763
+ r = 0;
764
+ } else {
765
+ int i;
766
+ ssize_t t;
767
+ unsigned char* cbuf = (unsigned char*) buf;
768
+ pdata = list_entry(_poll_fd[_index].list.next, struct pipe_data, list);
769
+ if (pdata == NULL) {
770
+ usbi_err(NULL, "no pdata");
771
+ r = 0;
772
+ goto out;
773
+ }
774
+
775
+ if (pdata->data == NULL){
776
+ usbi_err(NULL, "no data");
777
+ r = 0;
778
+ goto out;
779
+ }
780
+
781
+ t = MIN(pdata->count, count);
782
+
783
+ for(i = 0; i < t; i++) {
784
+ cbuf[i] = pdata->data[i];
785
+ }
786
+
787
+ free(pdata->data);
788
+ list_del(&(pdata->list));
789
+ free(pdata);
790
+ r = t;
791
+ }
792
+
793
+ out:
794
+ LeaveCriticalSection(&_poll_fd[_index].mutex);
795
+ return r;
796
+ }