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,1032 @@
1
+ /*
2
+ * Windows CE backend for libusb 1.0
3
+ * Copyright © 2011-2013 RealVNC Ltd.
4
+ * Large portions taken from Windows backend, which is
5
+ * Copyright © 2009-2010 Pete Batard <pbatard@gmail.com>
6
+ * With contributions from Michael Plante, Orin Eman et al.
7
+ * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
8
+ * Major code testing contribution by Xiaofan Chen
9
+ *
10
+ * This library is free software; you can redistribute it and/or
11
+ * modify it under the terms of the GNU Lesser General Public
12
+ * License as published by the Free Software Foundation; either
13
+ * version 2.1 of the License, or (at your option) any later version.
14
+ *
15
+ * This library is distributed in the hope that it will be useful,
16
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
+ * Lesser General Public License for more details.
19
+ *
20
+ * You should have received a copy of the GNU Lesser General Public
21
+ * License along with this library; if not, write to the Free Software
22
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
+ */
24
+
25
+ #include <libusbi.h>
26
+
27
+ #include <stdint.h>
28
+ #include <errno.h>
29
+ #include <inttypes.h>
30
+
31
+ #include "wince_usb.h"
32
+
33
+ // Forward declares
34
+ static int wince_clock_gettime(int clk_id, struct timespec *tp);
35
+ unsigned __stdcall wince_clock_gettime_threaded(void* param);
36
+
37
+ // Global variables
38
+ uint64_t hires_frequency, hires_ticks_to_ps;
39
+ int errno;
40
+ const uint64_t epoch_time = UINT64_C(116444736000000000); // 1970.01.01 00:00:000 in MS Filetime
41
+ int windows_version = WINDOWS_CE;
42
+ static int concurrent_usage = -1;
43
+ // Timer thread
44
+ // NB: index 0 is for monotonic and 1 is for the thread exit event
45
+ HANDLE timer_thread = NULL;
46
+ HANDLE timer_mutex = NULL;
47
+ struct timespec timer_tp;
48
+ volatile LONG request_count[2] = {0, 1}; // last one must be > 0
49
+ HANDLE timer_request[2] = { NULL, NULL };
50
+ HANDLE timer_response = NULL;
51
+ HANDLE driver_handle = INVALID_HANDLE_VALUE;
52
+
53
+ /*
54
+ * Converts a windows error to human readable string
55
+ * uses retval as errorcode, or, if 0, use GetLastError()
56
+ */
57
+ #if defined(ENABLE_LOGGING)
58
+ static char* windows_error_str(uint32_t retval)
59
+ {
60
+ static TCHAR wErr_string[ERR_BUFFER_SIZE];
61
+ static char err_string[ERR_BUFFER_SIZE];
62
+
63
+ DWORD size;
64
+ size_t i;
65
+ uint32_t error_code, format_error;
66
+
67
+ error_code = retval?retval:GetLastError();
68
+
69
+ safe_stprintf(wErr_string, ERR_BUFFER_SIZE, _T("[%d] "), error_code);
70
+
71
+ size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
72
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &wErr_string[safe_tcslen(wErr_string)],
73
+ ERR_BUFFER_SIZE - (DWORD)safe_tcslen(wErr_string), NULL);
74
+ if (size == 0) {
75
+ format_error = GetLastError();
76
+ if (format_error)
77
+ safe_stprintf(wErr_string, ERR_BUFFER_SIZE,
78
+ _T("Windows error code %u (FormatMessage error code %u)"), error_code, format_error);
79
+ else
80
+ safe_stprintf(wErr_string, ERR_BUFFER_SIZE, _T("Unknown error code %u"), error_code);
81
+ } else {
82
+ // Remove CR/LF terminators
83
+ for (i=safe_tcslen(wErr_string)-1; ((wErr_string[i]==0x0A) || (wErr_string[i]==0x0D)); i--) {
84
+ wErr_string[i] = 0;
85
+ }
86
+ }
87
+ if (WideCharToMultiByte(CP_ACP, 0, wErr_string, -1, err_string, ERR_BUFFER_SIZE, NULL, NULL) < 0)
88
+ {
89
+ strcpy(err_string, "Unable to convert error string");
90
+ }
91
+ return err_string;
92
+ }
93
+ #endif
94
+
95
+ static struct wince_device_priv *_device_priv(struct libusb_device *dev)
96
+ {
97
+ return (struct wince_device_priv *) dev->os_priv;
98
+ }
99
+
100
+ // ceusbkwrapper to libusb error code mapping
101
+ static int translate_driver_error(int error)
102
+ {
103
+ switch (error) {
104
+ case ERROR_INVALID_PARAMETER:
105
+ return LIBUSB_ERROR_INVALID_PARAM;
106
+ case ERROR_CALL_NOT_IMPLEMENTED:
107
+ case ERROR_NOT_SUPPORTED:
108
+ return LIBUSB_ERROR_NOT_SUPPORTED;
109
+ case ERROR_NOT_ENOUGH_MEMORY:
110
+ return LIBUSB_ERROR_NO_MEM;
111
+ case ERROR_INVALID_HANDLE:
112
+ return LIBUSB_ERROR_NO_DEVICE;
113
+ case ERROR_BUSY:
114
+ return LIBUSB_ERROR_BUSY;
115
+
116
+ // Error codes that are either unexpected, or have
117
+ // no suitable LIBUSB_ERROR equivilant.
118
+ case ERROR_CANCELLED:
119
+ case ERROR_INTERNAL_ERROR:
120
+ default:
121
+ return LIBUSB_ERROR_OTHER;
122
+ }
123
+ }
124
+
125
+ static int init_dllimports()
126
+ {
127
+ DLL_LOAD(ceusbkwrapper.dll, UkwOpenDriver, TRUE);
128
+ DLL_LOAD(ceusbkwrapper.dll, UkwGetDeviceList, TRUE);
129
+ DLL_LOAD(ceusbkwrapper.dll, UkwReleaseDeviceList, TRUE);
130
+ DLL_LOAD(ceusbkwrapper.dll, UkwGetDeviceAddress, TRUE);
131
+ DLL_LOAD(ceusbkwrapper.dll, UkwGetDeviceDescriptor, TRUE);
132
+ DLL_LOAD(ceusbkwrapper.dll, UkwGetConfigDescriptor, TRUE);
133
+ DLL_LOAD(ceusbkwrapper.dll, UkwCloseDriver, TRUE);
134
+ DLL_LOAD(ceusbkwrapper.dll, UkwCancelTransfer, TRUE);
135
+ DLL_LOAD(ceusbkwrapper.dll, UkwIssueControlTransfer, TRUE);
136
+ DLL_LOAD(ceusbkwrapper.dll, UkwClaimInterface, TRUE);
137
+ DLL_LOAD(ceusbkwrapper.dll, UkwReleaseInterface, TRUE);
138
+ DLL_LOAD(ceusbkwrapper.dll, UkwSetInterfaceAlternateSetting, TRUE);
139
+ DLL_LOAD(ceusbkwrapper.dll, UkwClearHaltHost, TRUE);
140
+ DLL_LOAD(ceusbkwrapper.dll, UkwClearHaltDevice, TRUE);
141
+ DLL_LOAD(ceusbkwrapper.dll, UkwGetConfig, TRUE);
142
+ DLL_LOAD(ceusbkwrapper.dll, UkwSetConfig, TRUE);
143
+ DLL_LOAD(ceusbkwrapper.dll, UkwResetDevice, TRUE);
144
+ DLL_LOAD(ceusbkwrapper.dll, UkwKernelDriverActive, TRUE);
145
+ DLL_LOAD(ceusbkwrapper.dll, UkwAttachKernelDriver, TRUE);
146
+ DLL_LOAD(ceusbkwrapper.dll, UkwDetachKernelDriver, TRUE);
147
+ DLL_LOAD(ceusbkwrapper.dll, UkwIssueBulkTransfer, TRUE);
148
+ DLL_LOAD(ceusbkwrapper.dll, UkwIsPipeHalted, TRUE);
149
+ return LIBUSB_SUCCESS;
150
+ }
151
+
152
+ static int init_device(struct libusb_device *dev, UKW_DEVICE drv_dev,
153
+ unsigned char bus_addr, unsigned char dev_addr)
154
+ {
155
+ struct wince_device_priv *priv = _device_priv(dev);
156
+ int r = LIBUSB_SUCCESS;
157
+
158
+ dev->bus_number = bus_addr;
159
+ dev->device_address = dev_addr;
160
+ priv->dev = drv_dev;
161
+
162
+ if (!UkwGetDeviceDescriptor(priv->dev, &(priv->desc))) {
163
+ r = translate_driver_error(GetLastError());
164
+ }
165
+ return r;
166
+ }
167
+
168
+ // Internal API functions
169
+ static int wince_init(struct libusb_context *ctx)
170
+ {
171
+ int i, r = LIBUSB_ERROR_OTHER;
172
+ HANDLE semaphore;
173
+ TCHAR sem_name[11+1+8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
174
+
175
+ _stprintf(sem_name, _T("libusb_init%08X"), (unsigned int)GetCurrentProcessId()&0xFFFFFFFF);
176
+ semaphore = CreateSemaphore(NULL, 1, 1, sem_name);
177
+ if (semaphore == NULL) {
178
+ usbi_err(ctx, "could not create semaphore: %s", windows_error_str(0));
179
+ return LIBUSB_ERROR_NO_MEM;
180
+ }
181
+
182
+ // A successful wait brings our semaphore count to 0 (unsignaled)
183
+ // => any concurent wait stalls until the semaphore's release
184
+ if (WaitForSingleObject(semaphore, INFINITE) != WAIT_OBJECT_0) {
185
+ usbi_err(ctx, "failure to access semaphore: %s", windows_error_str(0));
186
+ CloseHandle(semaphore);
187
+ return LIBUSB_ERROR_NO_MEM;
188
+ }
189
+
190
+ // NB: concurrent usage supposes that init calls are equally balanced with
191
+ // exit calls. If init is called more than exit, we will not exit properly
192
+ if ( ++concurrent_usage == 0 ) { // First init?
193
+ // Initialize pollable file descriptors
194
+ init_polling();
195
+
196
+ // Load DLL imports
197
+ if (init_dllimports() != LIBUSB_SUCCESS) {
198
+ usbi_err(ctx, "could not resolve DLL functions");
199
+ r = LIBUSB_ERROR_NOT_SUPPORTED;
200
+ goto init_exit;
201
+ }
202
+
203
+ // try to open a handle to the driver
204
+ driver_handle = UkwOpenDriver();
205
+ if (driver_handle == INVALID_HANDLE_VALUE) {
206
+ usbi_err(ctx, "could not connect to driver");
207
+ r = LIBUSB_ERROR_NOT_SUPPORTED;
208
+ goto init_exit;
209
+ }
210
+
211
+ // Windows CE doesn't have a way of specifying thread affinity, so this code
212
+ // just has to hope QueryPerformanceCounter doesn't report different values when
213
+ // running on different cores.
214
+ r = LIBUSB_ERROR_NO_MEM;
215
+ for (i = 0; i < 2; i++) {
216
+ timer_request[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
217
+ if (timer_request[i] == NULL) {
218
+ usbi_err(ctx, "could not create timer request event %d - aborting", i);
219
+ goto init_exit;
220
+ }
221
+ }
222
+ timer_response = CreateSemaphore(NULL, 0, MAX_TIMER_SEMAPHORES, NULL);
223
+ if (timer_response == NULL) {
224
+ usbi_err(ctx, "could not create timer response semaphore - aborting");
225
+ goto init_exit;
226
+ }
227
+ timer_mutex = CreateMutex(NULL, FALSE, NULL);
228
+ if (timer_mutex == NULL) {
229
+ usbi_err(ctx, "could not create timer mutex - aborting");
230
+ goto init_exit;
231
+ }
232
+ timer_thread = CreateThread(NULL, 0, wince_clock_gettime_threaded, NULL, 0, NULL);
233
+ if (timer_thread == NULL) {
234
+ usbi_err(ctx, "Unable to create timer thread - aborting");
235
+ goto init_exit;
236
+ }
237
+
238
+ // Wait for timer thread to init before continuing.
239
+ if (WaitForSingleObject(timer_response, INFINITE) != WAIT_OBJECT_0) {
240
+ usbi_err(ctx, "Failed to wait for timer thread to become ready - aborting");
241
+ goto init_exit;
242
+ }
243
+ }
244
+ // At this stage, either we went through full init successfully, or didn't need to
245
+ r = LIBUSB_SUCCESS;
246
+
247
+ init_exit: // Holds semaphore here.
248
+ if (!concurrent_usage && r != LIBUSB_SUCCESS) { // First init failed?
249
+ if (driver_handle != INVALID_HANDLE_VALUE) {
250
+ UkwCloseDriver(driver_handle);
251
+ driver_handle = INVALID_HANDLE_VALUE;
252
+ }
253
+ if (timer_thread) {
254
+ SetEvent(timer_request[1]); // actually the signal to quit the thread.
255
+ if (WAIT_OBJECT_0 != WaitForSingleObject(timer_thread, INFINITE)) {
256
+ usbi_warn(ctx, "could not wait for timer thread to quit");
257
+ TerminateThread(timer_thread, 1); // shouldn't happen, but we're destroying
258
+ // all objects it might have held anyway.
259
+ }
260
+ CloseHandle(timer_thread);
261
+ timer_thread = NULL;
262
+ }
263
+ for (i = 0; i < 2; i++) {
264
+ if (timer_request[i]) {
265
+ CloseHandle(timer_request[i]);
266
+ timer_request[i] = NULL;
267
+ }
268
+ }
269
+ if (timer_response) {
270
+ CloseHandle(timer_response);
271
+ timer_response = NULL;
272
+ }
273
+ if (timer_mutex) {
274
+ CloseHandle(timer_mutex);
275
+ timer_mutex = NULL;
276
+ }
277
+ }
278
+
279
+ if (r != LIBUSB_SUCCESS)
280
+ --concurrent_usage; // Not expected to call libusb_exit if we failed.
281
+
282
+ ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
283
+ CloseHandle(semaphore);
284
+ return r;
285
+ }
286
+
287
+ static void wince_exit(void)
288
+ {
289
+ int i;
290
+ HANDLE semaphore;
291
+ TCHAR sem_name[11+1+8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
292
+
293
+ _stprintf(sem_name, _T("libusb_init%08X"), (unsigned int)GetCurrentProcessId()&0xFFFFFFFF);
294
+ semaphore = CreateSemaphore(NULL, 1, 1, sem_name);
295
+ if (semaphore == NULL) {
296
+ return;
297
+ }
298
+
299
+ // A successful wait brings our semaphore count to 0 (unsignaled)
300
+ // => any concurent wait stalls until the semaphore release
301
+ if (WaitForSingleObject(semaphore, INFINITE) != WAIT_OBJECT_0) {
302
+ CloseHandle(semaphore);
303
+ return;
304
+ }
305
+
306
+ // Only works if exits and inits are balanced exactly
307
+ if (--concurrent_usage < 0) { // Last exit
308
+ exit_polling();
309
+
310
+ if (timer_thread) {
311
+ SetEvent(timer_request[1]); // actually the signal to quit the thread.
312
+ if (WAIT_OBJECT_0 != WaitForSingleObject(timer_thread, INFINITE)) {
313
+ usbi_dbg("could not wait for timer thread to quit");
314
+ TerminateThread(timer_thread, 1);
315
+ }
316
+ CloseHandle(timer_thread);
317
+ timer_thread = NULL;
318
+ }
319
+ for (i = 0; i < 2; i++) {
320
+ if (timer_request[i]) {
321
+ CloseHandle(timer_request[i]);
322
+ timer_request[i] = NULL;
323
+ }
324
+ }
325
+ if (timer_response) {
326
+ CloseHandle(timer_response);
327
+ timer_response = NULL;
328
+ }
329
+ if (timer_mutex) {
330
+ CloseHandle(timer_mutex);
331
+ timer_mutex = NULL;
332
+ }
333
+ if (driver_handle != INVALID_HANDLE_VALUE) {
334
+ UkwCloseDriver(driver_handle);
335
+ driver_handle = INVALID_HANDLE_VALUE;
336
+ }
337
+ }
338
+
339
+ ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
340
+ CloseHandle(semaphore);
341
+ }
342
+
343
+ static int wince_get_device_list(
344
+ struct libusb_context *ctx,
345
+ struct discovered_devs **discdevs)
346
+ {
347
+ UKW_DEVICE devices[MAX_DEVICE_COUNT];
348
+ struct discovered_devs * new_devices = *discdevs;
349
+ DWORD count = 0, i;
350
+ struct libusb_device *dev = NULL;
351
+ unsigned char bus_addr, dev_addr;
352
+ unsigned long session_id;
353
+ BOOL success;
354
+ DWORD release_list_offset = 0;
355
+ int r = LIBUSB_SUCCESS;
356
+
357
+ success = UkwGetDeviceList(driver_handle, devices, MAX_DEVICE_COUNT, &count);
358
+ if (!success) {
359
+ int libusbErr = translate_driver_error(GetLastError());
360
+ usbi_err(ctx, "could not get devices: %s", windows_error_str(0));
361
+ return libusbErr;
362
+ }
363
+ for(i = 0; i < count; ++i) {
364
+ release_list_offset = i;
365
+ success = UkwGetDeviceAddress(devices[i], &bus_addr, &dev_addr, &session_id);
366
+ if (!success) {
367
+ r = translate_driver_error(GetLastError());
368
+ usbi_err(ctx, "could not get device address for %d: %s", i, windows_error_str(0));
369
+ goto err_out;
370
+ }
371
+ dev = usbi_get_device_by_session_id(ctx, session_id);
372
+ if (dev) {
373
+ usbi_dbg("using existing device for %d/%d (session %ld)",
374
+ bus_addr, dev_addr, session_id);
375
+ // Release just this element in the device list (as we already hold a
376
+ // reference to it).
377
+ UkwReleaseDeviceList(driver_handle, &devices[i], 1);
378
+ release_list_offset++;
379
+ } else {
380
+ usbi_dbg("allocating new device for %d/%d (session %ld)",
381
+ bus_addr, dev_addr, session_id);
382
+ dev = usbi_alloc_device(ctx, session_id);
383
+ if (!dev) {
384
+ r = LIBUSB_ERROR_NO_MEM;
385
+ goto err_out;
386
+ }
387
+ r = init_device(dev, devices[i], bus_addr, dev_addr);
388
+ if (r < 0)
389
+ goto err_out;
390
+ r = usbi_sanitize_device(dev);
391
+ if (r < 0)
392
+ goto err_out;
393
+ }
394
+ new_devices = discovered_devs_append(new_devices, dev);
395
+ if (!discdevs) {
396
+ r = LIBUSB_ERROR_NO_MEM;
397
+ goto err_out;
398
+ }
399
+ safe_unref_device(dev);
400
+ }
401
+ *discdevs = new_devices;
402
+ return r;
403
+ err_out:
404
+ *discdevs = new_devices;
405
+ safe_unref_device(dev);
406
+ // Release the remainder of the unprocessed device list.
407
+ // The devices added to new_devices already will still be passed up to libusb,
408
+ // which can dispose of them at its leisure.
409
+ UkwReleaseDeviceList(driver_handle, &devices[release_list_offset], count - release_list_offset);
410
+ return r;
411
+ }
412
+
413
+ static int wince_open(struct libusb_device_handle *handle)
414
+ {
415
+ // Nothing to do to open devices as a handle to it has
416
+ // been retrieved by wince_get_device_list
417
+ return LIBUSB_SUCCESS;
418
+ }
419
+
420
+ static void wince_close(struct libusb_device_handle *handle)
421
+ {
422
+ // Nothing to do as wince_open does nothing.
423
+ }
424
+
425
+ static int wince_get_device_descriptor(
426
+ struct libusb_device *device,
427
+ unsigned char *buffer, int *host_endian)
428
+ {
429
+ struct wince_device_priv *priv = _device_priv(device);
430
+
431
+ *host_endian = 1;
432
+ memcpy(buffer, &priv->desc, DEVICE_DESC_LENGTH);
433
+ return LIBUSB_SUCCESS;
434
+ }
435
+
436
+ static int wince_get_active_config_descriptor(
437
+ struct libusb_device *device,
438
+ unsigned char *buffer, size_t len, int *host_endian)
439
+ {
440
+ struct wince_device_priv *priv = _device_priv(device);
441
+ DWORD actualSize = len;
442
+ *host_endian = 0;
443
+ if (!UkwGetConfigDescriptor(priv->dev, UKW_ACTIVE_CONFIGURATION, buffer, len, &actualSize)) {
444
+ return translate_driver_error(GetLastError());
445
+ }
446
+ return actualSize;
447
+ }
448
+
449
+ static int wince_get_config_descriptor(
450
+ struct libusb_device *device,
451
+ uint8_t config_index,
452
+ unsigned char *buffer, size_t len, int *host_endian)
453
+ {
454
+ struct wince_device_priv *priv = _device_priv(device);
455
+ DWORD actualSize = len;
456
+ *host_endian = 0;
457
+ if (!UkwGetConfigDescriptor(priv->dev, config_index, buffer, len, &actualSize)) {
458
+ return translate_driver_error(GetLastError());
459
+ }
460
+ return actualSize;
461
+ }
462
+
463
+ static int wince_get_configuration(
464
+ struct libusb_device_handle *handle,
465
+ int *config)
466
+ {
467
+ struct wince_device_priv *priv = _device_priv(handle->dev);
468
+ UCHAR cv = 0;
469
+ if (!UkwGetConfig(priv->dev, &cv)) {
470
+ return translate_driver_error(GetLastError());
471
+ }
472
+ (*config) = cv;
473
+ return LIBUSB_SUCCESS;
474
+ }
475
+
476
+ static int wince_set_configuration(
477
+ struct libusb_device_handle *handle,
478
+ int config)
479
+ {
480
+ struct wince_device_priv *priv = _device_priv(handle->dev);
481
+ // Setting configuration 0 places the device in Address state.
482
+ // This should correspond to the "unconfigured state" required by
483
+ // libusb when the specified configuration is -1.
484
+ UCHAR cv = (config < 0) ? 0 : config;
485
+ if (!UkwSetConfig(priv->dev, cv)) {
486
+ return translate_driver_error(GetLastError());
487
+ }
488
+ return LIBUSB_SUCCESS;
489
+ }
490
+
491
+ static int wince_claim_interface(
492
+ struct libusb_device_handle *handle,
493
+ int interface_number)
494
+ {
495
+ struct wince_device_priv *priv = _device_priv(handle->dev);
496
+ if (!UkwClaimInterface(priv->dev, interface_number)) {
497
+ return translate_driver_error(GetLastError());
498
+ }
499
+ return LIBUSB_SUCCESS;
500
+ }
501
+
502
+ static int wince_release_interface(
503
+ struct libusb_device_handle *handle,
504
+ int interface_number)
505
+ {
506
+ struct wince_device_priv *priv = _device_priv(handle->dev);
507
+ if (!UkwSetInterfaceAlternateSetting(priv->dev, interface_number, 0)) {
508
+ return translate_driver_error(GetLastError());
509
+ }
510
+ if (!UkwReleaseInterface(priv->dev, interface_number)) {
511
+ return translate_driver_error(GetLastError());
512
+ }
513
+ return LIBUSB_SUCCESS;
514
+ }
515
+
516
+ static int wince_set_interface_altsetting(
517
+ struct libusb_device_handle *handle,
518
+ int interface_number, int altsetting)
519
+ {
520
+ struct wince_device_priv *priv = _device_priv(handle->dev);
521
+ if (!UkwSetInterfaceAlternateSetting(priv->dev, interface_number, altsetting)) {
522
+ return translate_driver_error(GetLastError());
523
+ }
524
+ return LIBUSB_SUCCESS;
525
+ }
526
+
527
+ static int wince_clear_halt(
528
+ struct libusb_device_handle *handle,
529
+ unsigned char endpoint)
530
+ {
531
+ struct wince_device_priv *priv = _device_priv(handle->dev);
532
+ if (!UkwClearHaltHost(priv->dev, endpoint)) {
533
+ return translate_driver_error(GetLastError());
534
+ }
535
+ if (!UkwClearHaltDevice(priv->dev, endpoint)) {
536
+ return translate_driver_error(GetLastError());
537
+ }
538
+ return LIBUSB_SUCCESS;
539
+ }
540
+
541
+ static int wince_reset_device(
542
+ struct libusb_device_handle *handle)
543
+ {
544
+ struct wince_device_priv *priv = _device_priv(handle->dev);
545
+ if (!UkwResetDevice(priv->dev)) {
546
+ return translate_driver_error(GetLastError());
547
+ }
548
+ return LIBUSB_SUCCESS;
549
+ }
550
+
551
+ static int wince_kernel_driver_active(
552
+ struct libusb_device_handle *handle,
553
+ int interface_number)
554
+ {
555
+ struct wince_device_priv *priv = _device_priv(handle->dev);
556
+ BOOL result = FALSE;
557
+ if (!UkwKernelDriverActive(priv->dev, interface_number, &result)) {
558
+ return translate_driver_error(GetLastError());
559
+ }
560
+ return result ? 1 : 0;
561
+ }
562
+
563
+ static int wince_detach_kernel_driver(
564
+ struct libusb_device_handle *handle,
565
+ int interface_number)
566
+ {
567
+ struct wince_device_priv *priv = _device_priv(handle->dev);
568
+ if (!UkwDetachKernelDriver(priv->dev, interface_number)) {
569
+ return translate_driver_error(GetLastError());
570
+ }
571
+ return LIBUSB_SUCCESS;
572
+ }
573
+
574
+ static int wince_attach_kernel_driver(
575
+ struct libusb_device_handle *handle,
576
+ int interface_number)
577
+ {
578
+ struct wince_device_priv *priv = _device_priv(handle->dev);
579
+ if (!UkwAttachKernelDriver(priv->dev, interface_number)) {
580
+ return translate_driver_error(GetLastError());
581
+ }
582
+ return LIBUSB_SUCCESS;
583
+ }
584
+
585
+ static void wince_destroy_device(
586
+ struct libusb_device *dev)
587
+ {
588
+ struct wince_device_priv *priv = _device_priv(dev);
589
+ UkwReleaseDeviceList(driver_handle, &priv->dev, 1);
590
+ }
591
+
592
+ static void wince_clear_transfer_priv(
593
+ struct usbi_transfer *itransfer)
594
+ {
595
+ struct wince_transfer_priv *transfer_priv = (struct wince_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
596
+ struct winfd wfd = fd_to_winfd(transfer_priv->pollable_fd.fd);
597
+ // No need to cancel transfer as it is either complete or abandoned
598
+ wfd.itransfer = NULL;
599
+ CloseHandle(wfd.handle);
600
+ usbi_free_fd(&transfer_priv->pollable_fd);
601
+ }
602
+
603
+ static int wince_cancel_transfer(
604
+ struct usbi_transfer *itransfer)
605
+ {
606
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
607
+ struct wince_device_priv *priv = _device_priv(transfer->dev_handle->dev);
608
+ struct wince_transfer_priv *transfer_priv = (struct wince_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
609
+
610
+ if (!UkwCancelTransfer(priv->dev, transfer_priv->pollable_fd.overlapped, UKW_TF_NO_WAIT)) {
611
+ return translate_driver_error(GetLastError());
612
+ }
613
+ return LIBUSB_SUCCESS;
614
+ }
615
+
616
+ static int wince_submit_control_or_bulk_transfer(struct usbi_transfer *itransfer)
617
+ {
618
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
619
+ struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
620
+ struct wince_transfer_priv *transfer_priv = (struct wince_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
621
+ struct wince_device_priv *priv = _device_priv(transfer->dev_handle->dev);
622
+ BOOL direction_in, ret;
623
+ struct winfd wfd;
624
+ DWORD flags;
625
+ HANDLE eventHandle;
626
+ PUKW_CONTROL_HEADER setup = NULL;
627
+ const BOOL control_transfer = transfer->type == LIBUSB_TRANSFER_TYPE_CONTROL;
628
+
629
+ transfer_priv->pollable_fd = INVALID_WINFD;
630
+ if (control_transfer) {
631
+ setup = (PUKW_CONTROL_HEADER) transfer->buffer;
632
+ direction_in = setup->bmRequestType & LIBUSB_ENDPOINT_IN;
633
+ } else {
634
+ direction_in = transfer->endpoint & LIBUSB_ENDPOINT_IN;
635
+ }
636
+ flags = direction_in ? UKW_TF_IN_TRANSFER : UKW_TF_OUT_TRANSFER;
637
+ flags |= UKW_TF_SHORT_TRANSFER_OK;
638
+
639
+ eventHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
640
+ if (eventHandle == NULL) {
641
+ usbi_err(ctx, "Failed to create event for async transfer");
642
+ return LIBUSB_ERROR_NO_MEM;
643
+ }
644
+
645
+ wfd = usbi_create_fd(eventHandle, direction_in ? RW_READ : RW_WRITE, itransfer, &wince_cancel_transfer);
646
+ if (wfd.fd < 0) {
647
+ CloseHandle(eventHandle);
648
+ return LIBUSB_ERROR_NO_MEM;
649
+ }
650
+
651
+ transfer_priv->pollable_fd = wfd;
652
+ if (control_transfer) {
653
+ // Split out control setup header and data buffer
654
+ DWORD bufLen = transfer->length - sizeof(UKW_CONTROL_HEADER);
655
+ PVOID buf = (PVOID) &transfer->buffer[sizeof(UKW_CONTROL_HEADER)];
656
+
657
+ ret = UkwIssueControlTransfer(priv->dev, flags, setup, buf, bufLen, &transfer->actual_length, wfd.overlapped);
658
+ } else {
659
+ ret = UkwIssueBulkTransfer(priv->dev, flags, transfer->endpoint, transfer->buffer,
660
+ transfer->length, &transfer->actual_length, wfd.overlapped);
661
+ }
662
+ if (!ret) {
663
+ int libusbErr = translate_driver_error(GetLastError());
664
+ usbi_err(ctx, "UkwIssue%sTransfer failed: error %d",
665
+ control_transfer ? "Control" : "Bulk", GetLastError());
666
+ wince_clear_transfer_priv(itransfer);
667
+ return libusbErr;
668
+ }
669
+ usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, direction_in ? POLLIN : POLLOUT);
670
+ itransfer->flags |= USBI_TRANSFER_UPDATED_FDS;
671
+
672
+ return LIBUSB_SUCCESS;
673
+ }
674
+
675
+ static int wince_submit_iso_transfer(struct usbi_transfer *itransfer)
676
+ {
677
+ return LIBUSB_ERROR_NOT_SUPPORTED;
678
+ }
679
+
680
+ static int wince_submit_transfer(
681
+ struct usbi_transfer *itransfer)
682
+ {
683
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
684
+
685
+ switch (transfer->type) {
686
+ case LIBUSB_TRANSFER_TYPE_CONTROL:
687
+ case LIBUSB_TRANSFER_TYPE_BULK:
688
+ case LIBUSB_TRANSFER_TYPE_INTERRUPT:
689
+ return wince_submit_control_or_bulk_transfer(itransfer);
690
+ case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
691
+ return wince_submit_iso_transfer(itransfer);
692
+ case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
693
+ return LIBUSB_ERROR_NOT_SUPPORTED;
694
+ default:
695
+ usbi_err(TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
696
+ return LIBUSB_ERROR_INVALID_PARAM;
697
+ }
698
+ }
699
+
700
+ static void wince_transfer_callback(struct usbi_transfer *itransfer, uint32_t io_result, uint32_t io_size)
701
+ {
702
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
703
+ struct wince_transfer_priv *transfer_priv = (struct wince_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
704
+ struct wince_device_priv *priv = _device_priv(transfer->dev_handle->dev);
705
+ int status;
706
+
707
+ usbi_dbg("handling I/O completion with errcode %d", io_result);
708
+
709
+ if (io_result == ERROR_NOT_SUPPORTED &&
710
+ transfer->type != LIBUSB_TRANSFER_TYPE_CONTROL) {
711
+ /* For functional stalls, the WinCE USB layer (and therefore the USB Kernel Wrapper
712
+ * Driver) will report USB_ERROR_STALL/ERROR_NOT_SUPPORTED in situations where the
713
+ * endpoint isn't actually stalled.
714
+ *
715
+ * One example of this is that some devices will occasionally fail to reply to an IN
716
+ * token. The WinCE USB layer carries on with the transaction until it is completed
717
+ * (or cancelled) but then completes it with USB_ERROR_STALL.
718
+ *
719
+ * This code therefore needs to confirm that there really is a stall error, by both
720
+ * checking the pipe status and requesting the endpoint status from the device.
721
+ */
722
+ BOOL halted = FALSE;
723
+ usbi_dbg("checking I/O completion with errcode ERROR_NOT_SUPPORTED is really a stall");
724
+ if (UkwIsPipeHalted(priv->dev, transfer->endpoint, &halted)) {
725
+ /* Pipe status retrieved, so now request endpoint status by sending a GET_STATUS
726
+ * control request to the device. This is done synchronously, which is a bit
727
+ * naughty, but this is a special corner case.
728
+ */
729
+ WORD wStatus = 0;
730
+ DWORD written = 0;
731
+ UKW_CONTROL_HEADER ctrlHeader;
732
+ ctrlHeader.bmRequestType = LIBUSB_REQUEST_TYPE_STANDARD |
733
+ LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_ENDPOINT;
734
+ ctrlHeader.bRequest = LIBUSB_REQUEST_GET_STATUS;
735
+ ctrlHeader.wValue = 0;
736
+ ctrlHeader.wIndex = transfer->endpoint;
737
+ ctrlHeader.wLength = sizeof(wStatus);
738
+ if (UkwIssueControlTransfer(priv->dev,
739
+ UKW_TF_IN_TRANSFER | UKW_TF_SEND_TO_ENDPOINT,
740
+ &ctrlHeader, &wStatus, sizeof(wStatus), &written, NULL)) {
741
+ if (written == sizeof(wStatus) &&
742
+ (wStatus & STATUS_HALT_FLAG) == 0) {
743
+ if (!halted || UkwClearHaltHost(priv->dev, transfer->endpoint)) {
744
+ usbi_dbg("Endpoint doesn't appear to be stalled, overriding error with success");
745
+ io_result = ERROR_SUCCESS;
746
+ } else {
747
+ usbi_dbg("Endpoint doesn't appear to be stalled, but the host is halted, changing error");
748
+ io_result = ERROR_IO_DEVICE;
749
+ }
750
+ }
751
+ }
752
+ }
753
+ }
754
+
755
+ switch(io_result) {
756
+ case ERROR_SUCCESS:
757
+ itransfer->transferred += io_size;
758
+ status = LIBUSB_TRANSFER_COMPLETED;
759
+ break;
760
+ case ERROR_CANCELLED:
761
+ usbi_dbg("detected transfer cancel");
762
+ status = LIBUSB_TRANSFER_CANCELLED;
763
+ break;
764
+ case ERROR_NOT_SUPPORTED:
765
+ case ERROR_GEN_FAILURE:
766
+ usbi_dbg("detected endpoint stall");
767
+ status = LIBUSB_TRANSFER_STALL;
768
+ break;
769
+ case ERROR_SEM_TIMEOUT:
770
+ usbi_dbg("detected semaphore timeout");
771
+ status = LIBUSB_TRANSFER_TIMED_OUT;
772
+ break;
773
+ case ERROR_OPERATION_ABORTED:
774
+ if (itransfer->flags & USBI_TRANSFER_TIMED_OUT) {
775
+ usbi_dbg("detected timeout");
776
+ status = LIBUSB_TRANSFER_TIMED_OUT;
777
+ } else {
778
+ usbi_dbg("detected operation aborted");
779
+ status = LIBUSB_TRANSFER_CANCELLED;
780
+ }
781
+ break;
782
+ default:
783
+ usbi_err(ITRANSFER_CTX(itransfer), "detected I/O error: %s", windows_error_str(io_result));
784
+ status = LIBUSB_TRANSFER_ERROR;
785
+ break;
786
+ }
787
+ wince_clear_transfer_priv(itransfer);
788
+ if (status == LIBUSB_TRANSFER_CANCELLED) {
789
+ usbi_handle_transfer_cancellation(itransfer);
790
+ } else {
791
+ usbi_handle_transfer_completion(itransfer, (enum libusb_transfer_status)status);
792
+ }
793
+ }
794
+
795
+ static void wince_handle_callback (struct usbi_transfer *itransfer, uint32_t io_result, uint32_t io_size)
796
+ {
797
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
798
+
799
+ switch (transfer->type) {
800
+ case LIBUSB_TRANSFER_TYPE_CONTROL:
801
+ case LIBUSB_TRANSFER_TYPE_BULK:
802
+ case LIBUSB_TRANSFER_TYPE_INTERRUPT:
803
+ case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
804
+ wince_transfer_callback (itransfer, io_result, io_size);
805
+ break;
806
+ case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
807
+ return LIBUSB_ERROR_NOT_SUPPORTED;
808
+ default:
809
+ usbi_err(ITRANSFER_CTX(itransfer), "unknown endpoint type %d", transfer->type);
810
+ }
811
+ }
812
+
813
+ static int wince_handle_events(
814
+ struct libusb_context *ctx,
815
+ struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
816
+ {
817
+ struct wince_transfer_priv* transfer_priv = NULL;
818
+ POLL_NFDS_TYPE i = 0;
819
+ BOOL found = FALSE;
820
+ struct usbi_transfer *transfer;
821
+ DWORD io_size, io_result;
822
+
823
+ usbi_mutex_lock(&ctx->open_devs_lock);
824
+ for (i = 0; i < nfds && num_ready > 0; i++) {
825
+
826
+ usbi_dbg("checking fd %d with revents = %04x", fds[i].fd, fds[i].revents);
827
+
828
+ if (!fds[i].revents) {
829
+ continue;
830
+ }
831
+
832
+ num_ready--;
833
+
834
+ // Because a Windows OVERLAPPED is used for poll emulation,
835
+ // a pollable fd is created and stored with each transfer
836
+ usbi_mutex_lock(&ctx->flying_transfers_lock);
837
+ list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
838
+ transfer_priv = usbi_transfer_get_os_priv(transfer);
839
+ if (transfer_priv->pollable_fd.fd == fds[i].fd) {
840
+ found = TRUE;
841
+ break;
842
+ }
843
+ }
844
+ usbi_mutex_unlock(&ctx->flying_transfers_lock);
845
+
846
+ if (found && HasOverlappedIoCompleted(transfer_priv->pollable_fd.overlapped)) {
847
+ io_result = (DWORD)transfer_priv->pollable_fd.overlapped->Internal;
848
+ io_size = (DWORD)transfer_priv->pollable_fd.overlapped->InternalHigh;
849
+ usbi_remove_pollfd(ctx, transfer_priv->pollable_fd.fd);
850
+ // let handle_callback free the event using the transfer wfd
851
+ // If you don't use the transfer wfd, you run a risk of trying to free a
852
+ // newly allocated wfd that took the place of the one from the transfer.
853
+ wince_handle_callback(transfer, io_result, io_size);
854
+ } else if (found) {
855
+ usbi_err(ctx, "matching transfer for fd %x has not completed", fds[i]);
856
+ return LIBUSB_ERROR_OTHER;
857
+ } else {
858
+ usbi_err(ctx, "could not find a matching transfer for fd %x", fds[i]);
859
+ return LIBUSB_ERROR_NOT_FOUND;
860
+ }
861
+ }
862
+
863
+ usbi_mutex_unlock(&ctx->open_devs_lock);
864
+ return LIBUSB_SUCCESS;
865
+ }
866
+
867
+ /*
868
+ * Monotonic and real time functions
869
+ */
870
+ unsigned __stdcall wince_clock_gettime_threaded(void* param)
871
+ {
872
+ LARGE_INTEGER hires_counter, li_frequency;
873
+ LONG nb_responses;
874
+ int timer_index;
875
+
876
+ // Init - find out if we have access to a monotonic (hires) timer
877
+ if (!QueryPerformanceFrequency(&li_frequency)) {
878
+ usbi_dbg("no hires timer available on this platform");
879
+ hires_frequency = 0;
880
+ hires_ticks_to_ps = UINT64_C(0);
881
+ } else {
882
+ hires_frequency = li_frequency.QuadPart;
883
+ // The hires frequency can go as high as 4 GHz, so we'll use a conversion
884
+ // to picoseconds to compute the tv_nsecs part in clock_gettime
885
+ hires_ticks_to_ps = UINT64_C(1000000000000) / hires_frequency;
886
+ usbi_dbg("hires timer available (Frequency: %"PRIu64" Hz)", hires_frequency);
887
+ }
888
+
889
+ // Signal wince_init() that we're ready to service requests
890
+ if (ReleaseSemaphore(timer_response, 1, NULL) == 0) {
891
+ usbi_dbg("unable to release timer semaphore: %s", windows_error_str(0));
892
+ }
893
+
894
+ // Main loop - wait for requests
895
+ while (1) {
896
+ timer_index = WaitForMultipleObjects(2, timer_request, FALSE, INFINITE) - WAIT_OBJECT_0;
897
+ if ( (timer_index != 0) && (timer_index != 1) ) {
898
+ usbi_dbg("failure to wait on requests: %s", windows_error_str(0));
899
+ continue;
900
+ }
901
+ if (request_count[timer_index] == 0) {
902
+ // Request already handled
903
+ ResetEvent(timer_request[timer_index]);
904
+ // There's still a possiblity that a thread sends a request between the
905
+ // time we test request_count[] == 0 and we reset the event, in which case
906
+ // the request would be ignored. The simple solution to that is to test
907
+ // request_count again and process requests if non zero.
908
+ if (request_count[timer_index] == 0)
909
+ continue;
910
+ }
911
+ switch (timer_index) {
912
+ case 0:
913
+ WaitForSingleObject(timer_mutex, INFINITE);
914
+ // Requests to this thread are for hires always
915
+ if (QueryPerformanceCounter(&hires_counter) != 0) {
916
+ timer_tp.tv_sec = (long)(hires_counter.QuadPart / hires_frequency);
917
+ timer_tp.tv_nsec = (long)(((hires_counter.QuadPart % hires_frequency)/1000) * hires_ticks_to_ps);
918
+ } else {
919
+ // Fallback to real-time if we can't get monotonic value
920
+ // Note that real-time clock does not wait on the mutex or this thread.
921
+ wince_clock_gettime(USBI_CLOCK_REALTIME, &timer_tp);
922
+ }
923
+ ReleaseMutex(timer_mutex);
924
+
925
+ nb_responses = InterlockedExchange((LONG*)&request_count[0], 0);
926
+ if ( (nb_responses)
927
+ && (ReleaseSemaphore(timer_response, nb_responses, NULL) == 0) ) {
928
+ usbi_dbg("unable to release timer semaphore: %s", windows_error_str(0));
929
+ }
930
+ continue;
931
+ case 1: // time to quit
932
+ usbi_dbg("timer thread quitting");
933
+ return 0;
934
+ }
935
+ }
936
+ usbi_dbg("ERROR: broken timer thread");
937
+ return 1;
938
+ }
939
+
940
+ static int wince_clock_gettime(int clk_id, struct timespec *tp)
941
+ {
942
+ FILETIME filetime;
943
+ ULARGE_INTEGER rtime;
944
+ DWORD r;
945
+ SYSTEMTIME st;
946
+ switch(clk_id) {
947
+ case USBI_CLOCK_MONOTONIC:
948
+ if (hires_frequency != 0) {
949
+ while (1) {
950
+ InterlockedIncrement((LONG*)&request_count[0]);
951
+ SetEvent(timer_request[0]);
952
+ r = WaitForSingleObject(timer_response, TIMER_REQUEST_RETRY_MS);
953
+ switch(r) {
954
+ case WAIT_OBJECT_0:
955
+ WaitForSingleObject(timer_mutex, INFINITE);
956
+ *tp = timer_tp;
957
+ ReleaseMutex(timer_mutex);
958
+ return LIBUSB_SUCCESS;
959
+ case WAIT_TIMEOUT:
960
+ usbi_dbg("could not obtain a timer value within reasonable timeframe - too much load?");
961
+ break; // Retry until successful
962
+ default:
963
+ usbi_dbg("WaitForSingleObject failed: %s", windows_error_str(0));
964
+ return LIBUSB_ERROR_OTHER;
965
+ }
966
+ }
967
+ }
968
+ // Fall through and return real-time if monotonic was not detected @ timer init
969
+ case USBI_CLOCK_REALTIME:
970
+ // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
971
+ // with a predef epoch_time to have an epoch that starts at 1970.01.01 00:00
972
+ // Note however that our resolution is bounded by the Windows system time
973
+ // functions and is at best of the order of 1 ms (or, usually, worse)
974
+ GetSystemTime(&st);
975
+ SystemTimeToFileTime(&st, &filetime);
976
+ rtime.LowPart = filetime.dwLowDateTime;
977
+ rtime.HighPart = filetime.dwHighDateTime;
978
+ rtime.QuadPart -= epoch_time;
979
+ tp->tv_sec = (long)(rtime.QuadPart / 10000000);
980
+ tp->tv_nsec = (long)((rtime.QuadPart % 10000000)*100);
981
+ return LIBUSB_SUCCESS;
982
+ default:
983
+ return LIBUSB_ERROR_INVALID_PARAM;
984
+ }
985
+ }
986
+
987
+ const struct usbi_os_backend wince_backend = {
988
+ "Windows CE",
989
+ 0,
990
+ wince_init,
991
+ wince_exit,
992
+
993
+ wince_get_device_list,
994
+ NULL, /* hotplug_poll */
995
+ wince_open,
996
+ wince_close,
997
+
998
+ wince_get_device_descriptor,
999
+ wince_get_active_config_descriptor,
1000
+ wince_get_config_descriptor,
1001
+ NULL, /* get_config_descriptor_by_value() */
1002
+
1003
+ wince_get_configuration,
1004
+ wince_set_configuration,
1005
+ wince_claim_interface,
1006
+ wince_release_interface,
1007
+
1008
+ wince_set_interface_altsetting,
1009
+ wince_clear_halt,
1010
+ wince_reset_device,
1011
+
1012
+ NULL, /* alloc_streams */
1013
+ NULL, /* free_streams */
1014
+
1015
+ wince_kernel_driver_active,
1016
+ wince_detach_kernel_driver,
1017
+ wince_attach_kernel_driver,
1018
+
1019
+ wince_destroy_device,
1020
+
1021
+ wince_submit_transfer,
1022
+ wince_cancel_transfer,
1023
+ wince_clear_transfer_priv,
1024
+
1025
+ wince_handle_events,
1026
+
1027
+ wince_clock_gettime,
1028
+ sizeof(struct wince_device_priv),
1029
+ sizeof(struct wince_device_handle_priv),
1030
+ sizeof(struct wince_transfer_priv),
1031
+ 0,
1032
+ };