Debian Codegraph @

libusb/os/windows_common.h

raw
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 <[email protected]>
8 * Copyright © 2014-2020 Chris Dickens <[email protected]>
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 #else
56 #include <process.h>
57 #endif
58
59 #define safe_free(p) do {if (p != NULL) {free((void *)p); p = NULL;}} while (0)
60
61 /*
62 * API macros - leveraged from libusb-win32 1.x
63 */
64 #define DLL_STRINGIFY(s) #s
65
66 /*
67 * Macros for handling DLL themselves
68 */
69 #define DLL_HANDLE_NAME(name) __dll_##name##_handle
70
71 #define DLL_DECLARE_HANDLE(name) \
72 static HMODULE DLL_HANDLE_NAME(name)
73
74 #define DLL_GET_HANDLE(ctx, name) \
75 do { \
76 DLL_HANDLE_NAME(name) = load_system_library(ctx, \
77 DLL_STRINGIFY(name)); \
78 if (!DLL_HANDLE_NAME(name)) \
79 return false; \
80 } while (0)
81
82 #define DLL_FREE_HANDLE(name) \
83 do { \
84 if (DLL_HANDLE_NAME(name)) { \
85 FreeLibrary(DLL_HANDLE_NAME(name)); \
86 DLL_HANDLE_NAME(name) = NULL; \
87 } \
88 } while (0)
89
90 /*
91 * Macros for handling functions within a DLL
92 */
93 #define DLL_FUNC_NAME(name) __dll_##name##_func_t
94
95 #define DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefixname, name, args) \
96 typedef ret (api * DLL_FUNC_NAME(name))args; \
97 static DLL_FUNC_NAME(name) prefixname
98
99 #define DLL_DECLARE_FUNC(api, ret, name, args) \
100 DLL_DECLARE_FUNC_PREFIXNAME(api, ret, name, name, args)
101 #define DLL_DECLARE_FUNC_PREFIXED(api, ret, prefix, name, args) \
102 DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefix##name, name, args)
103
104 #define DLL_LOAD_FUNC_PREFIXNAME(dll, prefixname, name, ret_on_failure) \
105 do { \
106 HMODULE h = DLL_HANDLE_NAME(dll); \
107 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
108 DLL_STRINGIFY(name)); \
109 if (prefixname) \
110 break; \
111 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
112 DLL_STRINGIFY(name) DLL_STRINGIFY(A)); \
113 if (prefixname) \
114 break; \
115 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
116 DLL_STRINGIFY(name) DLL_STRINGIFY(W)); \
117 if (prefixname) \
118 break; \
119 if (ret_on_failure) \
120 return false; \
121 } while (0)
122
123 #define DLL_LOAD_FUNC(dll, name, ret_on_failure) \
124 DLL_LOAD_FUNC_PREFIXNAME(dll, name, name, ret_on_failure)
125 #define DLL_LOAD_FUNC_PREFIXED(dll, prefix, name, ret_on_failure) \
126 DLL_LOAD_FUNC_PREFIXNAME(dll, prefix##name, name, ret_on_failure)
127
128 // https://msdn.microsoft.com/en-us/library/windows/hardware/ff539136(v=vs.85).aspx
129 #if !defined(USBD_SUCCESS)
130 typedef LONG USBD_STATUS;
131
132 #define USBD_SUCCESS(Status) ((USBD_STATUS)(Status) >= 0)
133
134 #define USBD_STATUS_STALL_PID ((USBD_STATUS)0xC0000004L)
135 #define USBD_STATUS_ENDPOINT_HALTED ((USBD_STATUS)0xC0000030L)
136 #define USBD_STATUS_TIMEOUT ((USBD_STATUS)0xC0006000L)
137 #define USBD_STATUS_DEVICE_GONE ((USBD_STATUS)0xC0007000L)
138 #define USBD_STATUS_CANCELED ((USBD_STATUS)0xC0010000L)
139 #endif
140
141 // error code added with Windows SDK 10.0.18362
142 #ifndef ERROR_NO_SUCH_DEVICE
143 #define ERROR_NO_SUCH_DEVICE 433L
144 #endif
145
146 /* Windows versions */
147 enum windows_version {
148 WINDOWS_UNDEFINED,
149 WINDOWS_2000,
150 WINDOWS_XP,
151 WINDOWS_2003, // Also XP x64
152 WINDOWS_VISTA,
153 WINDOWS_7,
154 WINDOWS_8,
155 WINDOWS_8_1,
156 WINDOWS_10,
157 WINDOWS_11,
158 WINDOWS_12_OR_LATER
159 };
160
161 extern enum windows_version windows_version;
162
163 #include <pshpack1.h>
164
165 typedef struct USB_DEVICE_DESCRIPTOR {
166 UCHAR bLength;
167 UCHAR bDescriptorType;
168 USHORT bcdUSB;
169 UCHAR bDeviceClass;
170 UCHAR bDeviceSubClass;
171 UCHAR bDeviceProtocol;
172 UCHAR bMaxPacketSize0;
173 USHORT idVendor;
174 USHORT idProduct;
175 USHORT bcdDevice;
176 UCHAR iManufacturer;
177 UCHAR iProduct;
178 UCHAR iSerialNumber;
179 UCHAR bNumConfigurations;
180 } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
181
182 typedef struct USB_CONFIGURATION_DESCRIPTOR {
183 UCHAR bLength;
184 UCHAR bDescriptorType;
185 USHORT wTotalLength;
186 UCHAR bNumInterfaces;
187 UCHAR bConfigurationValue;
188 UCHAR iConfiguration;
189 UCHAR bmAttributes;
190 UCHAR MaxPower;
191 } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
192
193 #include <poppack.h>
194
195 #define MAX_DEVICE_ID_LEN 200
196
197 typedef struct USB_DK_DEVICE_ID {
198 WCHAR DeviceID[MAX_DEVICE_ID_LEN];
199 WCHAR InstanceID[MAX_DEVICE_ID_LEN];
200 } USB_DK_DEVICE_ID, *PUSB_DK_DEVICE_ID;
201
202 typedef struct USB_DK_DEVICE_INFO {
203 USB_DK_DEVICE_ID ID;
204 ULONG64 FilterID;
205 ULONG64 Port;
206 ULONG64 Speed;
207 USB_DEVICE_DESCRIPTOR DeviceDescriptor;
208 } USB_DK_DEVICE_INFO, *PUSB_DK_DEVICE_INFO;
209
210 typedef struct USB_DK_ISO_TRANSFER_RESULT {
211 ULONG64 ActualLength;
212 ULONG64 TransferResult;
213 } USB_DK_ISO_TRANSFER_RESULT, *PUSB_DK_ISO_TRANSFER_RESULT;
214
215 typedef struct USB_DK_GEN_TRANSFER_RESULT {
216 ULONG64 BytesTransferred;
217 ULONG64 UsbdStatus; // USBD_STATUS code
218 } USB_DK_GEN_TRANSFER_RESULT, *PUSB_DK_GEN_TRANSFER_RESULT;
219
220 typedef struct USB_DK_TRANSFER_RESULT {
221 USB_DK_GEN_TRANSFER_RESULT GenResult;
222 PVOID64 IsochronousResultsArray; // array of USB_DK_ISO_TRANSFER_RESULT
223 } USB_DK_TRANSFER_RESULT, *PUSB_DK_TRANSFER_RESULT;
224
225 typedef struct USB_DK_TRANSFER_REQUEST {
226 ULONG64 EndpointAddress;
227 PVOID64 Buffer;
228 ULONG64 BufferLength;
229 ULONG64 TransferType;
230 ULONG64 IsochronousPacketsArraySize;
231 PVOID64 IsochronousPacketsArray;
232 USB_DK_TRANSFER_RESULT Result;
233 } USB_DK_TRANSFER_REQUEST, *PUSB_DK_TRANSFER_REQUEST;
234
235 struct usbdk_device_priv {
236 USB_DK_DEVICE_ID ID;
237 PUSB_CONFIGURATION_DESCRIPTOR *config_descriptors;
238 HANDLE redirector_handle;
239 HANDLE system_handle;
240 uint8_t active_configuration;
241 };
242
243 struct winusb_device_priv {
244 bool initialized;
245 #if defined(LIBUSB_WINDOWS_HOTPLUG)
246 bool seen_during_scan; // set true for each device encountered during windows_get_device_list
247 bool seen_before_scan; // set true for each device encountered before windows_get_device_list
248 #endif
249 bool root_hub;
250 uint8_t active_config;
251 uint16_t langid; // cached USB language ID for string descriptor requests
252 uint8_t depth; // distance to HCD
253 const struct windows_usb_api_backend *apib;
254 char *dev_id;
255 char *path; // device interface path
256 int sub_api; // for WinUSB-like APIs
257 usbi_mutex_t interface_lock; // protects usb_interface[] against concurrent
258 // claim/release/altsetting from different handles,
259 // and concurrent enumeration setup
260 // (set_composite_interface, set_hid_interface,
261 // and HUB_PASS/DEV_PASS in winusb_get_device_list)
262 struct {
263 char *path; // each interface needs a device interface path,
264 const struct windows_usb_api_backend *apib; // an API backend (multiple drivers support),
265 int sub_api;
266 int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
267 uint8_t *endpoint;
268 int current_altsetting;
269 bool restricted_functionality; // indicates if the interface functionality is restricted
270 // by Windows (eg. HID keyboards or mice cannot do R/W)
271 uint8_t num_associated_interfaces; // If non-zero, the interface is part of a grouped
272 // set of associated interfaces (defined by an IAD)
273 // and this is the number of interfaces within the
274 // associated group (bInterfaceCount in IAD).
275 uint8_t first_associated_interface; // For associated interfaces, this is the index of
276 // the first interface (bFirstInterface in IAD) for
277 // the grouped set of associated interfaces.
278 } usb_interface[USB_MAXINTERFACES];
279 struct hid_device_priv *hid;
280 PUSB_CONFIGURATION_DESCRIPTOR *config_descriptor; // list of pointers to the cached config descriptors
281 GUID class_guid; // checked for change during re-enumeration
282 };
283
284 struct usbdk_device_handle_priv {
285 // Not currently used
286 char dummy;
287 };
288
289 enum WINUSB_ZLP {
290 WINUSB_ZLP_UNSET = 0,
291 WINUSB_ZLP_OFF = 1,
292 WINUSB_ZLP_ON = 2
293 };
294
295 struct winusb_device_handle_priv {
296 int active_interface;
297 struct {
298 HANDLE dev_handle; // WinUSB needs an extra handle for the file
299 HANDLE api_handle; // used by the API to communicate with the device
300 uint8_t zlp[USB_MAXENDPOINTS]; // Current per-endpoint SHORT_PACKET_TERMINATE status (enum WINUSB_ZLP)
301 } interface_handle[USB_MAXINTERFACES];
302 int autoclaim_count[USB_MAXINTERFACES]; // For auto-release
303 };
304
305 struct usbdk_transfer_priv {
306 USB_DK_TRANSFER_REQUEST request;
307 PULONG64 IsochronousPacketsArray;
308 PUSB_DK_ISO_TRANSFER_RESULT IsochronousResultsArray;
309 };
310
311 struct winusb_transfer_priv {
312 uint8_t interface_number;
313
314 uint8_t *hid_buffer; // 1 byte extended data buffer, required for HID
315 uint8_t *hid_dest; // transfer buffer destination, required for HID
316 size_t hid_expected_size;
317
318 // For isochronous transfers with LibUSBk driver:
319 void *iso_context;
320
321 // For isochronous transfers with Microsoft WinUSB driver:
322 void *isoch_buffer_handle; // The isoch_buffer_handle to free at the end of the transfer
323 BOOL iso_break_stream; // Whether the isoch. stream was to be continued in the last call of libusb_submit_transfer.
324 // As we this structure is zeroed out upon initialization, we need to use inverse logic here.
325 libusb_transfer_cb_fn iso_user_callback; // Original transfer callback of the user. Might be used for isochronous transfers.
326 };
327
328 struct windows_backend {
329 int (*init)(struct libusb_context *ctx);
330 void (*exit)(struct libusb_context *ctx);
331 int (*get_device_list)(struct libusb_context *ctx,
332 struct discovered_devs **discdevs);
333 int (*get_device_string)(libusb_device *dev,
334 enum libusb_device_string_type string_type, char *data, int length);
335 int (*open)(struct libusb_device_handle *dev_handle);
336 void (*close)(struct libusb_device_handle *dev_handle);
337 int (*get_active_config_descriptor)(struct libusb_device *device,
338 void *buffer, size_t len);
339 int (*get_config_descriptor)(struct libusb_device *device,
340 uint8_t config_index, void *buffer, size_t len);
341 int (*get_config_descriptor_by_value)(struct libusb_device *device,
342 uint8_t bConfigurationValue, void **buffer);
343 int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config);
344 int (*set_configuration)(struct libusb_device_handle *dev_handle, uint8_t config);
345 int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
346 int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
347 int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
348 uint8_t interface_number, uint8_t altsetting);
349 int (*clear_halt)(struct libusb_device_handle *dev_handle,
350 unsigned char endpoint);
351 int (*reset_device)(struct libusb_device_handle *dev_handle);
352 void (*destroy_device)(struct libusb_device *dev);
353 int (*submit_transfer)(struct usbi_transfer *itransfer);
354 int (*cancel_transfer)(struct usbi_transfer *itransfer);
355 void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
356 enum libusb_transfer_status (*copy_transfer_data)(struct usbi_transfer *itransfer, DWORD length);
357 int (*endpoint_supports_raw_io)(struct libusb_device_handle *dev_handle,
358 uint8_t endpoint);
359 int (*endpoint_set_raw_io)(struct libusb_device_handle *dev_handle,
360 uint8_t endpoint, int enable);
361 int (*get_max_raw_io_transfer_size)(
362 struct libusb_device_handle *dev_handle,
363 uint8_t endpoint);
364 };
365
366 struct windows_context_priv {
367 const struct windows_backend *backend;
368 HANDLE completion_port;
369 HANDLE completion_port_thread;
370 };
371
372 union windows_device_priv {
373 struct usbdk_device_priv usbdk_priv;
374 struct winusb_device_priv winusb_priv;
375 };
376
377 struct windows_device_handle_priv {
378 struct list_head active_transfers;
379 union {
380 struct usbdk_device_handle_priv usbdk_priv;
381 struct winusb_device_handle_priv winusb_priv;
382 };
383 };
384
385 struct windows_transfer_priv {
386 OVERLAPPED overlapped;
387 HANDLE handle;
388 struct list_head list;
389 union {
390 struct usbdk_transfer_priv usbdk_priv;
391 struct winusb_transfer_priv winusb_priv;
392 };
393 };
394
395 static inline struct usbdk_device_handle_priv *get_usbdk_device_handle_priv(struct libusb_device_handle *dev_handle)
396 {
397 struct windows_device_handle_priv *handle_priv = usbi_get_device_handle_priv(dev_handle);
398 return &handle_priv->usbdk_priv;
399 }
400
401 static inline struct winusb_device_handle_priv *get_winusb_device_handle_priv(struct libusb_device_handle *dev_handle)
402 {
403 struct windows_device_handle_priv *handle_priv = usbi_get_device_handle_priv(dev_handle);
404 return &handle_priv->winusb_priv;
405 }
406
407 static inline OVERLAPPED *get_transfer_priv_overlapped(struct usbi_transfer *itransfer)
408 {
409 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
410 return &transfer_priv->overlapped;
411 }
412
413 static inline void set_transfer_priv_handle(struct usbi_transfer *itransfer, HANDLE handle)
414 {
415 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
416 transfer_priv->handle = handle;
417 }
418
419 static inline struct usbdk_transfer_priv *get_usbdk_transfer_priv(struct usbi_transfer *itransfer)
420 {
421 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
422 return &transfer_priv->usbdk_priv;
423 }
424
425 static inline struct winusb_transfer_priv *get_winusb_transfer_priv(struct usbi_transfer *itransfer)
426 {
427 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
428 return &transfer_priv->winusb_priv;
429 }
430
431 extern const struct windows_backend usbdk_backend;
432 extern const struct windows_backend winusb_backend;
433
434 HMODULE load_system_library(struct libusb_context *ctx, const char *name);
435 unsigned long htab_hash(const char *str);
436 enum libusb_transfer_status usbd_status_to_libusb_transfer_status(USBD_STATUS status);
437 void windows_force_sync_completion(struct usbi_transfer *itransfer, ULONG size);
438
439 #if defined(ENABLE_LOGGING)
440 const char *windows_error_str(DWORD error_code);
441 #endif
442
443 #endif