1 /*
2 * Windows backend common header for libusb 1.0
3 *
4 * This file brings together header code common between
5 * the desktop Windows backends.
6 * Copyright © 2012-2013 RealVNC Ltd.
7 * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
8 * Copyright © 2014-2020 Chris Dickens <christopher.a.dickens@gmail.com>
9 * With contributions from Michael Plante, Orin Eman et al.
10 * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
11 * Major code testing contribution by Xiaofan Chen
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 #ifndef LIBUSB_WINDOWS_COMMON_H
29 #define LIBUSB_WINDOWS_COMMON_H
30
31 #include <stdbool.h>
32
33 /*
34 * Workaround for the mess that exists with the DWORD and ULONG types.
35 * Visual Studio unconditionally defines these types as 'unsigned long'
36 * and a long is always 32-bits, even on 64-bit builds. GCC on the other
37 * hand varies the width of a long, matching it to the build. To make
38 * matters worse, the platform headers for these GCC builds define a
39 * DWORD/ULONG to be 'unsigned long' on 32-bit builds and 'unsigned int'
40 * on 64-bit builds. This creates a great deal of warnings for compilers
41 * that support printf format checking since it will never actually be
42 * an unsigned long.
43 */
44 #if defined(_MSC_VER)
45 #define ULONG_CAST(x) (x)
46 #else
47 #define ULONG_CAST(x) ((unsigned long)(x))
48 #endif
49
50 #if defined(__CYGWIN__)
51 #define _stricmp strcasecmp
52 #define _strdup strdup
53 // _beginthreadex is MSVCRT => unavailable for cygwin. Fallback to using CreateThread
54 #define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, (LPTHREAD_START_ROUTINE)c, d, e, (LPDWORD)f)
55 #endif
56
57 #define safe_free(p) do {if (p != NULL) {free((void *)p); p = NULL;}} while (0)
58
59 /*
60 * API macros - leveraged from libusb-win32 1.x
61 */
62 #define DLL_STRINGIFY(s) #s
63
64 /*
65 * Macros for handling DLL themselves
66 */
67 #define DLL_HANDLE_NAME(name) __dll_##name##_handle
68
69 #define DLL_DECLARE_HANDLE(name) \
70 static HMODULE DLL_HANDLE_NAME(name)
71
72 #define DLL_GET_HANDLE(ctx, name) \
73 do { \
74 DLL_HANDLE_NAME(name) = load_system_library(ctx, \
75 DLL_STRINGIFY(name)); \
76 if (!DLL_HANDLE_NAME(name)) \
77 return false; \
78 } while (0)
79
80 #define DLL_FREE_HANDLE(name) \
81 do { \
82 if (DLL_HANDLE_NAME(name)) { \
83 FreeLibrary(DLL_HANDLE_NAME(name)); \
84 DLL_HANDLE_NAME(name) = NULL; \
85 } \
86 } while (0)
87
88 /*
89 * Macros for handling functions within a DLL
90 */
91 #define DLL_FUNC_NAME(name) __dll_##name##_func_t
92
93 #define DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefixname, name, args) \
94 typedef ret (api * DLL_FUNC_NAME(name))args; \
95 static DLL_FUNC_NAME(name) prefixname
96
97 #define DLL_DECLARE_FUNC(api, ret, name, args) \
98 DLL_DECLARE_FUNC_PREFIXNAME(api, ret, name, name, args)
99 #define DLL_DECLARE_FUNC_PREFIXED(api, ret, prefix, name, args) \
100 DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefix##name, name, args)
101
102 #define DLL_LOAD_FUNC_PREFIXNAME(dll, prefixname, name, ret_on_failure) \
103 do { \
104 HMODULE h = DLL_HANDLE_NAME(dll); \
105 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
106 DLL_STRINGIFY(name)); \
107 if (prefixname) \
108 break; \
109 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
110 DLL_STRINGIFY(name) DLL_STRINGIFY(A)); \
111 if (prefixname) \
112 break; \
113 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
114 DLL_STRINGIFY(name) DLL_STRINGIFY(W)); \
115 if (prefixname) \
116 break; \
117 if (ret_on_failure) \
118 return false; \
119 } while (0)
120
121 #define DLL_LOAD_FUNC(dll, name, ret_on_failure) \
122 DLL_LOAD_FUNC_PREFIXNAME(dll, name, name, ret_on_failure)
123 #define DLL_LOAD_FUNC_PREFIXED(dll, prefix, name, ret_on_failure) \
124 DLL_LOAD_FUNC_PREFIXNAME(dll, prefix##name, name, ret_on_failure)
125
126 // https://msdn.microsoft.com/en-us/library/windows/hardware/ff539136(v=vs.85).aspx
127 #if !defined(USBD_SUCCESS)
128 typedef LONG USBD_STATUS;
129
130 #define USBD_SUCCESS(Status) ((USBD_STATUS)(Status) >= 0)
131
132 #define USBD_STATUS_ENDPOINT_HALTED ((USBD_STATUS)0xC0000030L)
133 #define USBD_STATUS_TIMEOUT ((USBD_STATUS)0xC0006000L)
134 #define USBD_STATUS_DEVICE_GONE ((USBD_STATUS)0xC0007000L)
135 #define USBD_STATUS_CANCELED ((USBD_STATUS)0xC0010000L)
136 #endif
137
138 // error code added with Windows SDK 10.0.18362
139 #ifndef ERROR_NO_SUCH_DEVICE
140 #define ERROR_NO_SUCH_DEVICE 433L
141 #endif
142
143 /* Windows versions */
144 enum windows_version {
145 WINDOWS_UNDEFINED,
146 WINDOWS_2000,
147 WINDOWS_XP,
148 WINDOWS_2003, // Also XP x64
149 WINDOWS_VISTA,
150 WINDOWS_7,
151 WINDOWS_8,
152 WINDOWS_8_1,
153 WINDOWS_10,
154 WINDOWS_11_OR_LATER
155 };
156
157 extern enum windows_version windows_version;
158
159 #include <pshpack1.h>
160
161 typedef struct USB_DEVICE_DESCRIPTOR {
162 UCHAR bLength;
163 UCHAR bDescriptorType;
164 USHORT bcdUSB;
165 UCHAR bDeviceClass;
166 UCHAR bDeviceSubClass;
167 UCHAR bDeviceProtocol;
168 UCHAR bMaxPacketSize0;
169 USHORT idVendor;
170 USHORT idProduct;
171 USHORT bcdDevice;
172 UCHAR iManufacturer;
173 UCHAR iProduct;
174 UCHAR iSerialNumber;
175 UCHAR bNumConfigurations;
176 } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
177
178 typedef struct USB_CONFIGURATION_DESCRIPTOR {
179 UCHAR bLength;
180 UCHAR bDescriptorType;
181 USHORT wTotalLength;
182 UCHAR bNumInterfaces;
183 UCHAR bConfigurationValue;
184 UCHAR iConfiguration;
185 UCHAR bmAttributes;
186 UCHAR MaxPower;
187 } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
188
189 #include <poppack.h>
190
191 #define MAX_DEVICE_ID_LEN 200
192
193 typedef struct USB_DK_DEVICE_ID {
194 WCHAR DeviceID[MAX_DEVICE_ID_LEN];
195 WCHAR InstanceID[MAX_DEVICE_ID_LEN];
196 } USB_DK_DEVICE_ID, *PUSB_DK_DEVICE_ID;
197
198 typedef struct USB_DK_DEVICE_INFO {
199 USB_DK_DEVICE_ID ID;
200 ULONG64 FilterID;
201 ULONG64 Port;
202 ULONG64 Speed;
203 USB_DEVICE_DESCRIPTOR DeviceDescriptor;
204 } USB_DK_DEVICE_INFO, *PUSB_DK_DEVICE_INFO;
205
206 typedef struct USB_DK_ISO_TRANSFER_RESULT {
207 ULONG64 ActualLength;
208 ULONG64 TransferResult;
209 } USB_DK_ISO_TRANSFER_RESULT, *PUSB_DK_ISO_TRANSFER_RESULT;
210
211 typedef struct USB_DK_GEN_TRANSFER_RESULT {
212 ULONG64 BytesTransferred;
213 ULONG64 UsbdStatus; // USBD_STATUS code
214 } USB_DK_GEN_TRANSFER_RESULT, *PUSB_DK_GEN_TRANSFER_RESULT;
215
216 typedef struct USB_DK_TRANSFER_RESULT {
217 USB_DK_GEN_TRANSFER_RESULT GenResult;
218 PVOID64 IsochronousResultsArray; // array of USB_DK_ISO_TRANSFER_RESULT
219 } USB_DK_TRANSFER_RESULT, *PUSB_DK_TRANSFER_RESULT;
220
221 typedef struct USB_DK_TRANSFER_REQUEST {
222 ULONG64 EndpointAddress;
223 PVOID64 Buffer;
224 ULONG64 BufferLength;
225 ULONG64 TransferType;
226 ULONG64 IsochronousPacketsArraySize;
227 PVOID64 IsochronousPacketsArray;
228 USB_DK_TRANSFER_RESULT Result;
229 } USB_DK_TRANSFER_REQUEST, *PUSB_DK_TRANSFER_REQUEST;
230
231 struct usbdk_device_priv {
232 USB_DK_DEVICE_ID ID;
233 PUSB_CONFIGURATION_DESCRIPTOR *config_descriptors;
234 HANDLE redirector_handle;
235 HANDLE system_handle;
236 uint8_t active_configuration;
237 };
238
239 struct winusb_device_priv {
240 bool initialized;
241 bool root_hub;
242 uint8_t active_config;
243 uint8_t depth; // distance to HCD
244 const struct windows_usb_api_backend *apib;
245 char *dev_id;
246 char *path; // device interface path
247 int sub_api; // for WinUSB-like APIs
248 struct {
249 char *path; // each interface needs a device interface path,
250 const struct windows_usb_api_backend *apib; // an API backend (multiple drivers support),
251 int sub_api;
252 int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
253 uint8_t *endpoint;
254 int current_altsetting;
255 bool restricted_functionality; // indicates if the interface functionality is restricted
256 // by Windows (eg. HID keyboards or mice cannot do R/W)
257 } usb_interface[USB_MAXINTERFACES];
258 struct hid_device_priv *hid;
259 PUSB_CONFIGURATION_DESCRIPTOR *config_descriptor; // list of pointers to the cached config descriptors
260 };
261
262 struct usbdk_device_handle_priv {
263 // Not currently used
264 char dummy;
265 };
266
267 struct winusb_device_handle_priv {
268 int active_interface;
269 struct {
270 HANDLE dev_handle; // WinUSB needs an extra handle for the file
271 HANDLE api_handle; // used by the API to communicate with the device
272 } interface_handle[USB_MAXINTERFACES];
273 int autoclaim_count[USB_MAXINTERFACES]; // For auto-release
274 };
275
276 struct usbdk_transfer_priv {
277 USB_DK_TRANSFER_REQUEST request;
278 PULONG64 IsochronousPacketsArray;
279 PUSB_DK_ISO_TRANSFER_RESULT IsochronousResultsArray;
280 };
281
282 struct winusb_transfer_priv {
283 uint8_t interface_number;
284
285 uint8_t *hid_buffer; // 1 byte extended data buffer, required for HID
286 uint8_t *hid_dest; // transfer buffer destination, required for HID
287 size_t hid_expected_size;
288
289 // For isochronous transfers with LibUSBk driver:
290 void *iso_context;
291
292 // For isochronous transfers with Microsoft WinUSB driver:
293 void *isoch_buffer_handle; // The isoch_buffer_handle to free at the end of the transfer
294 BOOL iso_break_stream; // Whether the isoch. stream was to be continued in the last call of libusb_submit_transfer.
295 // As we this structure is zeroed out upon initialization, we need to use inverse logic here.
296 libusb_transfer_cb_fn iso_user_callback; // Original transfer callback of the user. Might be used for isochronous transfers.
297 };
298
299 struct windows_backend {
300 int (*init)(struct libusb_context *ctx);
301 void (*exit)(struct libusb_context *ctx);
302 int (*get_device_list)(struct libusb_context *ctx,
303 struct discovered_devs **discdevs);
304 int (*open)(struct libusb_device_handle *dev_handle);
305 void (*close)(struct libusb_device_handle *dev_handle);
306 int (*get_active_config_descriptor)(struct libusb_device *device,
307 void *buffer, size_t len);
308 int (*get_config_descriptor)(struct libusb_device *device,
309 uint8_t config_index, void *buffer, size_t len);
310 int (*get_config_descriptor_by_value)(struct libusb_device *device,
311 uint8_t bConfigurationValue, void **buffer);
312 int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config);
313 int (*set_configuration)(struct libusb_device_handle *dev_handle, uint8_t config);
314 int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
315 int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
316 int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
317 uint8_t interface_number, uint8_t altsetting);
318 int (*clear_halt)(struct libusb_device_handle *dev_handle,
319 unsigned char endpoint);
320 int (*reset_device)(struct libusb_device_handle *dev_handle);
321 void (*destroy_device)(struct libusb_device *dev);
322 int (*submit_transfer)(struct usbi_transfer *itransfer);
323 int (*cancel_transfer)(struct usbi_transfer *itransfer);
324 void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
325 enum libusb_transfer_status (*copy_transfer_data)(struct usbi_transfer *itransfer, DWORD length);
326 };
327
328 struct windows_context_priv {
329 const struct windows_backend *backend;
330 HANDLE completion_port;
331 HANDLE completion_port_thread;
332 };
333
334 union windows_device_priv {
335 struct usbdk_device_priv usbdk_priv;
336 struct winusb_device_priv winusb_priv;
337 };
338
339 union windows_device_handle_priv {
340 struct usbdk_device_handle_priv usbdk_priv;
341 struct winusb_device_handle_priv winusb_priv;
342 };
343
344 struct windows_transfer_priv {
345 OVERLAPPED overlapped;
346 HANDLE handle;
347 union {
348 struct usbdk_transfer_priv usbdk_priv;
349 struct winusb_transfer_priv winusb_priv;
350 };
351 };
352
get_transfer_priv_overlapped(struct usbi_transfer * itransfer)353 static inline OVERLAPPED *get_transfer_priv_overlapped(struct usbi_transfer *itransfer)
354 {
355 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
356 return &transfer_priv->overlapped;
357 }
358
set_transfer_priv_handle(struct usbi_transfer * itransfer,HANDLE handle)359 static inline void set_transfer_priv_handle(struct usbi_transfer *itransfer, HANDLE handle)
360 {
361 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
362 transfer_priv->handle = handle;
363 }
364
get_usbdk_transfer_priv(struct usbi_transfer * itransfer)365 static inline struct usbdk_transfer_priv *get_usbdk_transfer_priv(struct usbi_transfer *itransfer)
366 {
367 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
368 return &transfer_priv->usbdk_priv;
369 }
370
get_winusb_transfer_priv(struct usbi_transfer * itransfer)371 static inline struct winusb_transfer_priv *get_winusb_transfer_priv(struct usbi_transfer *itransfer)
372 {
373 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
374 return &transfer_priv->winusb_priv;
375 }
376
377 extern const struct windows_backend usbdk_backend;
378 extern const struct windows_backend winusb_backend;
379
380 HMODULE load_system_library(struct libusb_context *ctx, const char *name);
381 unsigned long htab_hash(const char *str);
382 enum libusb_transfer_status usbd_status_to_libusb_transfer_status(USBD_STATUS status);
383 void windows_force_sync_completion(struct usbi_transfer *itransfer, ULONG size);
384
385 #if defined(ENABLE_LOGGING)
386 const char *windows_error_str(DWORD error_code);
387 #endif
388
389 #endif
390