2 * Windows backend common header for libusb 1.0
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
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.
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.
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
28 #ifndef LIBUSB_WINDOWS_COMMON_H
29 #define LIBUSB_WINDOWS_COMMON_H
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
45 #define ULONG_CAST(x) (x)
47 #define ULONG_CAST(x) ((unsigned long)(x))
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)
59 #define safe_free(p) do {if (p != NULL) {free((void *)p); p = NULL;}} while (0)
62 * API macros - leveraged from libusb-win32 1.x
64 #define DLL_STRINGIFY(s) #s
67 * Macros for handling DLL themselves
69 #define DLL_HANDLE_NAME(name) __dll_##name##_handle
71 #define DLL_DECLARE_HANDLE(name) \
72 static HMODULE DLL_HANDLE_NAME(name)
74 #define DLL_GET_HANDLE(ctx, name) \
76 DLL_HANDLE_NAME(name) = load_system_library(ctx, \
77 DLL_STRINGIFY(name)); \
78 if (!DLL_HANDLE_NAME(name)) \
82 #define DLL_FREE_HANDLE(name) \
84 if (DLL_HANDLE_NAME(name)) { \
85 FreeLibrary(DLL_HANDLE_NAME(name)); \
86 DLL_HANDLE_NAME(name) = NULL; \
91 * Macros for handling functions within a DLL
93 #define DLL_FUNC_NAME(name) __dll_##name##_func_t
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
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)
104 #define DLL_LOAD_FUNC_PREFIXNAME(dll, prefixname, name, ret_on_failure) \
106 HMODULE h = DLL_HANDLE_NAME(dll); \
107 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
108 DLL_STRINGIFY(name)); \
111 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
112 DLL_STRINGIFY(name) DLL_STRINGIFY(A)); \
115 prefixname = (DLL_FUNC_NAME(name))GetProcAddress(h, \
116 DLL_STRINGIFY(name) DLL_STRINGIFY(W)); \
119 if (ret_on_failure) \
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)
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;
132 #define USBD_SUCCESS(Status) ((USBD_STATUS)(Status) >= 0)
134 #define USBD_STATUS_ENDPOINT_HALTED ((USBD_STATUS)0xC0000030L)
135 #define USBD_STATUS_TIMEOUT ((USBD_STATUS)0xC0006000L)
136 #define USBD_STATUS_DEVICE_GONE ((USBD_STATUS)0xC0007000L)
137 #define USBD_STATUS_CANCELED ((USBD_STATUS)0xC0010000L)
140 // error code added with Windows SDK 10.0.18362
141 #ifndef ERROR_NO_SUCH_DEVICE
142 #define ERROR_NO_SUCH_DEVICE 433L
145 /* Windows versions */
146 enum windows_version {
150 WINDOWS_2003, // Also XP x64
159 extern enum windows_version windows_version;
161 #include <pshpack1.h>
163 typedef struct USB_DEVICE_DESCRIPTOR {
165 UCHAR bDescriptorType;
168 UCHAR bDeviceSubClass;
169 UCHAR bDeviceProtocol;
170 UCHAR bMaxPacketSize0;
177 UCHAR bNumConfigurations;
178 } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
180 typedef struct USB_CONFIGURATION_DESCRIPTOR {
182 UCHAR bDescriptorType;
184 UCHAR bNumInterfaces;
185 UCHAR bConfigurationValue;
186 UCHAR iConfiguration;
189 } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
193 #define MAX_DEVICE_ID_LEN 200
195 typedef struct USB_DK_DEVICE_ID {
196 WCHAR DeviceID[MAX_DEVICE_ID_LEN];
197 WCHAR InstanceID[MAX_DEVICE_ID_LEN];
198 } USB_DK_DEVICE_ID, *PUSB_DK_DEVICE_ID;
200 typedef struct USB_DK_DEVICE_INFO {
205 USB_DEVICE_DESCRIPTOR DeviceDescriptor;
206 } USB_DK_DEVICE_INFO, *PUSB_DK_DEVICE_INFO;
208 typedef struct USB_DK_ISO_TRANSFER_RESULT {
209 ULONG64 ActualLength;
210 ULONG64 TransferResult;
211 } USB_DK_ISO_TRANSFER_RESULT, *PUSB_DK_ISO_TRANSFER_RESULT;
213 typedef struct USB_DK_GEN_TRANSFER_RESULT {
214 ULONG64 BytesTransferred;
215 ULONG64 UsbdStatus; // USBD_STATUS code
216 } USB_DK_GEN_TRANSFER_RESULT, *PUSB_DK_GEN_TRANSFER_RESULT;
218 typedef struct USB_DK_TRANSFER_RESULT {
219 USB_DK_GEN_TRANSFER_RESULT GenResult;
220 PVOID64 IsochronousResultsArray; // array of USB_DK_ISO_TRANSFER_RESULT
221 } USB_DK_TRANSFER_RESULT, *PUSB_DK_TRANSFER_RESULT;
223 typedef struct USB_DK_TRANSFER_REQUEST {
224 ULONG64 EndpointAddress;
226 ULONG64 BufferLength;
227 ULONG64 TransferType;
228 ULONG64 IsochronousPacketsArraySize;
229 PVOID64 IsochronousPacketsArray;
230 USB_DK_TRANSFER_RESULT Result;
231 } USB_DK_TRANSFER_REQUEST, *PUSB_DK_TRANSFER_REQUEST;
233 struct usbdk_device_priv {
235 PUSB_CONFIGURATION_DESCRIPTOR *config_descriptors;
236 HANDLE redirector_handle;
237 HANDLE system_handle;
238 uint8_t active_configuration;
241 struct winusb_device_priv {
244 uint8_t active_config;
245 uint8_t depth; // distance to HCD
246 const struct windows_usb_api_backend *apib;
248 char *path; // device interface path
249 int sub_api; // for WinUSB-like APIs
251 char *path; // each interface needs a device interface path,
252 const struct windows_usb_api_backend *apib; // an API backend (multiple drivers support),
254 int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
256 int current_altsetting;
257 bool restricted_functionality; // indicates if the interface functionality is restricted
258 // by Windows (eg. HID keyboards or mice cannot do R/W)
259 } usb_interface[USB_MAXINTERFACES];
260 struct hid_device_priv *hid;
261 PUSB_CONFIGURATION_DESCRIPTOR *config_descriptor; // list of pointers to the cached config descriptors
262 GUID class_guid; // checked for change during re-enumeration
265 struct usbdk_device_handle_priv {
266 // Not currently used
271 WINUSB_ZLP_UNSET = 0,
276 struct winusb_device_handle_priv {
277 int active_interface;
279 HANDLE dev_handle; // WinUSB needs an extra handle for the file
280 HANDLE api_handle; // used by the API to communicate with the device
281 uint8_t zlp[USB_MAXENDPOINTS]; // Current per-endpoint SHORT_PACKET_TERMINATE status (enum WINUSB_ZLP)
282 } interface_handle[USB_MAXINTERFACES];
283 int autoclaim_count[USB_MAXINTERFACES]; // For auto-release
286 struct usbdk_transfer_priv {
287 USB_DK_TRANSFER_REQUEST request;
288 PULONG64 IsochronousPacketsArray;
289 PUSB_DK_ISO_TRANSFER_RESULT IsochronousResultsArray;
292 struct winusb_transfer_priv {
293 uint8_t interface_number;
295 uint8_t *hid_buffer; // 1 byte extended data buffer, required for HID
296 uint8_t *hid_dest; // transfer buffer destination, required for HID
297 size_t hid_expected_size;
299 // For isochronous transfers with LibUSBk driver:
302 // For isochronous transfers with Microsoft WinUSB driver:
303 void *isoch_buffer_handle; // The isoch_buffer_handle to free at the end of the transfer
304 BOOL iso_break_stream; // Whether the isoch. stream was to be continued in the last call of libusb_submit_transfer.
305 // As we this structure is zeroed out upon initialization, we need to use inverse logic here.
306 libusb_transfer_cb_fn iso_user_callback; // Original transfer callback of the user. Might be used for isochronous transfers.
309 struct windows_backend {
310 int (*init)(struct libusb_context *ctx);
311 void (*exit)(struct libusb_context *ctx);
312 int (*get_device_list)(struct libusb_context *ctx,
313 struct discovered_devs **discdevs);
314 int (*open)(struct libusb_device_handle *dev_handle);
315 void (*close)(struct libusb_device_handle *dev_handle);
316 int (*get_active_config_descriptor)(struct libusb_device *device,
317 void *buffer, size_t len);
318 int (*get_config_descriptor)(struct libusb_device *device,
319 uint8_t config_index, void *buffer, size_t len);
320 int (*get_config_descriptor_by_value)(struct libusb_device *device,
321 uint8_t bConfigurationValue, void **buffer);
322 int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config);
323 int (*set_configuration)(struct libusb_device_handle *dev_handle, uint8_t config);
324 int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
325 int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
326 int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
327 uint8_t interface_number, uint8_t altsetting);
328 int (*clear_halt)(struct libusb_device_handle *dev_handle,
329 unsigned char endpoint);
330 int (*reset_device)(struct libusb_device_handle *dev_handle);
331 void (*destroy_device)(struct libusb_device *dev);
332 int (*submit_transfer)(struct usbi_transfer *itransfer);
333 int (*cancel_transfer)(struct usbi_transfer *itransfer);
334 void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
335 enum libusb_transfer_status (*copy_transfer_data)(struct usbi_transfer *itransfer, DWORD length);
338 struct windows_context_priv {
339 const struct windows_backend *backend;
340 HANDLE completion_port;
341 HANDLE completion_port_thread;
344 union windows_device_priv {
345 struct usbdk_device_priv usbdk_priv;
346 struct winusb_device_priv winusb_priv;
349 struct windows_device_handle_priv {
350 struct list_head active_transfers;
352 struct usbdk_device_handle_priv usbdk_priv;
353 struct winusb_device_handle_priv winusb_priv;
357 struct windows_transfer_priv {
358 OVERLAPPED overlapped;
360 struct list_head list;
362 struct usbdk_transfer_priv usbdk_priv;
363 struct winusb_transfer_priv winusb_priv;
367 static inline struct usbdk_device_handle_priv *get_usbdk_device_handle_priv(struct libusb_device_handle *dev_handle)
369 struct windows_device_handle_priv *handle_priv = usbi_get_device_handle_priv(dev_handle);
370 return &handle_priv->usbdk_priv;
373 static inline struct winusb_device_handle_priv *get_winusb_device_handle_priv(struct libusb_device_handle *dev_handle)
375 struct windows_device_handle_priv *handle_priv = usbi_get_device_handle_priv(dev_handle);
376 return &handle_priv->winusb_priv;
379 static inline OVERLAPPED *get_transfer_priv_overlapped(struct usbi_transfer *itransfer)
381 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
382 return &transfer_priv->overlapped;
385 static inline void set_transfer_priv_handle(struct usbi_transfer *itransfer, HANDLE handle)
387 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
388 transfer_priv->handle = handle;
391 static inline struct usbdk_transfer_priv *get_usbdk_transfer_priv(struct usbi_transfer *itransfer)
393 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
394 return &transfer_priv->usbdk_priv;
397 static inline struct winusb_transfer_priv *get_winusb_transfer_priv(struct usbi_transfer *itransfer)
399 struct windows_transfer_priv *transfer_priv = usbi_get_transfer_priv(itransfer);
400 return &transfer_priv->winusb_priv;
403 extern const struct windows_backend usbdk_backend;
404 extern const struct windows_backend winusb_backend;
406 HMODULE load_system_library(struct libusb_context *ctx, const char *name);
407 unsigned long htab_hash(const char *str);
408 enum libusb_transfer_status usbd_status_to_libusb_transfer_status(USBD_STATUS status);
409 void windows_force_sync_completion(struct usbi_transfer *itransfer, ULONG size);
411 #if defined(ENABLE_LOGGING)
412 const char *windows_error_str(DWORD error_code);