libusbx 1.0.18 *FINAL RELEASE*
[libusbx.git] / libusb / os / windows_usb.h
blob14ee422c3e9fa784234f95c7bbafe37e745f65b5
1 /*
2 * Windows backend for libusbx 1.0
3 * Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
4 * With contributions from Michael Plante, Orin Eman et al.
5 * Parts of this code adapted from libusb-win32-v1 by Stephan Meyer
6 * Major code testing contribution by Xiaofan Chen
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.
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.
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
23 #pragma once
25 #include "windows_common.h"
27 #if defined(_MSC_VER)
28 // disable /W4 MSVC warnings that are benign
29 #pragma warning(disable:4127) // conditional expression is constant
30 #pragma warning(disable:4100) // unreferenced formal parameter
31 #pragma warning(disable:4214) // bit field types other than int
32 #pragma warning(disable:4201) // nameless struct/union
33 #endif
35 // Missing from MSVC6 setupapi.h
36 #if !defined(SPDRP_ADDRESS)
37 #define SPDRP_ADDRESS 28
38 #endif
39 #if !defined(SPDRP_INSTALL_STATE)
40 #define SPDRP_INSTALL_STATE 34
41 #endif
43 // Missing from MinGW
44 #if !defined(FACILITY_SETUPAPI)
45 #define FACILITY_SETUPAPI 15
46 #endif
48 #if defined(__CYGWIN__ )
49 #define _stricmp stricmp
50 // cygwin produces a warning unless these prototypes are defined
51 extern int _snprintf(char *buffer, size_t count, const char *format, ...);
52 extern char *_strdup(const char *strSource);
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, f)
55 #endif
57 #define MAX_CTRL_BUFFER_LENGTH 4096
58 #define MAX_USB_DEVICES 256
59 #define MAX_USB_STRING_LENGTH 128
60 #define MAX_HID_REPORT_SIZE 1024
61 #define MAX_HID_DESCRIPTOR_SIZE 256
62 #define MAX_GUID_STRING_LENGTH 40
63 #define MAX_PATH_LENGTH 128
64 #define MAX_KEY_LENGTH 256
65 #define LIST_SEPARATOR ';'
66 #define HTAB_SIZE 1021
68 // Handle code for HID interface that have been claimed ("dibs")
69 #define INTERFACE_CLAIMED ((HANDLE)(intptr_t)0xD1B5)
70 // Additional return code for HID operations that completed synchronously
71 #define LIBUSB_COMPLETED (LIBUSB_SUCCESS + 1)
73 // http://msdn.microsoft.com/en-us/library/ff545978.aspx
74 // http://msdn.microsoft.com/en-us/library/ff545972.aspx
75 // http://msdn.microsoft.com/en-us/library/ff545982.aspx
76 #if !defined(GUID_DEVINTERFACE_USB_HOST_CONTROLLER)
77 const GUID GUID_DEVINTERFACE_USB_HOST_CONTROLLER = { 0x3ABF6F2D, 0x71C4, 0x462A, {0x8A, 0x92, 0x1E, 0x68, 0x61, 0xE6, 0xAF, 0x27} };
78 #endif
79 #if !defined(GUID_DEVINTERFACE_USB_DEVICE)
80 const GUID GUID_DEVINTERFACE_USB_DEVICE = { 0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED} };
81 #endif
82 #if !defined(GUID_DEVINTERFACE_USB_HUB)
83 const GUID GUID_DEVINTERFACE_USB_HUB = { 0xF18A0E88, 0xC30C, 0x11D0, {0x88, 0x15, 0x00, 0xA0, 0xC9, 0x06, 0xBE, 0xD8} };
84 #endif
85 #if !defined(GUID_DEVINTERFACE_LIBUSB0_FILTER)
86 const GUID GUID_DEVINTERFACE_LIBUSB0_FILTER = { 0xF9F3FF14, 0xAE21, 0x48A0, {0x8A, 0x25, 0x80, 0x11, 0xA7, 0xA9, 0x31, 0xD9} };
87 #endif
91 * Multiple USB API backend support
93 #define USB_API_UNSUPPORTED 0
94 #define USB_API_HUB 1
95 #define USB_API_COMPOSITE 2
96 #define USB_API_WINUSBX 3
97 #define USB_API_HID 4
98 #define USB_API_MAX 5
99 // The following is used to indicate if the HID or composite extra props have already been set.
100 #define USB_API_SET (1<<USB_API_MAX)
102 // Sub-APIs for WinUSB-like driver APIs (WinUSB, libusbK, libusb-win32 through the libusbK DLL)
103 // Must have the same values as the KUSB_DRVID enum from libusbk.h
104 #define SUB_API_NOTSET -1
105 #define SUB_API_LIBUSBK 0
106 #define SUB_API_LIBUSB0 1
107 #define SUB_API_WINUSB 2
108 #define SUB_API_MAX 3
110 #define WINUSBX_DRV_NAMES { "libusbK", "libusb0", "WinUSB"}
112 struct windows_usb_api_backend {
113 const uint8_t id;
114 const char* designation;
115 const char **driver_name_list; // Driver name, without .sys, e.g. "usbccgp"
116 const uint8_t nb_driver_names;
117 int (*init)(int sub_api, struct libusb_context *ctx);
118 int (*exit)(int sub_api);
119 int (*open)(int sub_api, struct libusb_device_handle *dev_handle);
120 void (*close)(int sub_api, struct libusb_device_handle *dev_handle);
121 int (*configure_endpoints)(int sub_api, struct libusb_device_handle *dev_handle, int iface);
122 int (*claim_interface)(int sub_api, struct libusb_device_handle *dev_handle, int iface);
123 int (*set_interface_altsetting)(int sub_api, struct libusb_device_handle *dev_handle, int iface, int altsetting);
124 int (*release_interface)(int sub_api, struct libusb_device_handle *dev_handle, int iface);
125 int (*clear_halt)(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint);
126 int (*reset_device)(int sub_api, struct libusb_device_handle *dev_handle);
127 int (*submit_bulk_transfer)(int sub_api, struct usbi_transfer *itransfer);
128 int (*submit_iso_transfer)(int sub_api, struct usbi_transfer *itransfer);
129 int (*submit_control_transfer)(int sub_api, struct usbi_transfer *itransfer);
130 int (*abort_control)(int sub_api, struct usbi_transfer *itransfer);
131 int (*abort_transfers)(int sub_api, struct usbi_transfer *itransfer);
132 int (*copy_transfer_data)(int sub_api, struct usbi_transfer *itransfer, uint32_t io_size);
135 extern const struct windows_usb_api_backend usb_api_backend[USB_API_MAX];
137 #define PRINT_UNSUPPORTED_API(fname) \
138 usbi_dbg("unsupported API call for '" \
139 #fname "' (unrecognized device driver)"); \
140 return LIBUSB_ERROR_NOT_SUPPORTED;
143 * private structures definition
144 * with inline pseudo constructors/destructors
147 // TODO (v2+): move hid desc to libusb.h?
148 struct libusb_hid_descriptor {
149 uint8_t bLength;
150 uint8_t bDescriptorType;
151 uint16_t bcdHID;
152 uint8_t bCountryCode;
153 uint8_t bNumDescriptors;
154 uint8_t bClassDescriptorType;
155 uint16_t wClassDescriptorLength;
157 #define LIBUSB_DT_HID_SIZE 9
158 #define HID_MAX_CONFIG_DESC_SIZE (LIBUSB_DT_CONFIG_SIZE + LIBUSB_DT_INTERFACE_SIZE \
159 + LIBUSB_DT_HID_SIZE + 2 * LIBUSB_DT_ENDPOINT_SIZE)
160 #define HID_MAX_REPORT_SIZE 1024
161 #define HID_IN_EP 0x81
162 #define HID_OUT_EP 0x02
163 #define LIBUSB_REQ_RECIPIENT(request_type) ((request_type) & 0x1F)
164 #define LIBUSB_REQ_TYPE(request_type) ((request_type) & (0x03 << 5))
165 #define LIBUSB_REQ_IN(request_type) ((request_type) & LIBUSB_ENDPOINT_IN)
166 #define LIBUSB_REQ_OUT(request_type) (!LIBUSB_REQ_IN(request_type))
168 // The following are used for HID reports IOCTLs
169 #define HID_CTL_CODE(id) \
170 CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_NEITHER, FILE_ANY_ACCESS)
171 #define HID_BUFFER_CTL_CODE(id) \
172 CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_BUFFERED, FILE_ANY_ACCESS)
173 #define HID_IN_CTL_CODE(id) \
174 CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_IN_DIRECT, FILE_ANY_ACCESS)
175 #define HID_OUT_CTL_CODE(id) \
176 CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
178 #define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
179 #define IOCTL_HID_GET_INPUT_REPORT HID_OUT_CTL_CODE(104)
180 #define IOCTL_HID_SET_FEATURE HID_IN_CTL_CODE(100)
181 #define IOCTL_HID_SET_OUTPUT_REPORT HID_IN_CTL_CODE(101)
183 enum libusb_hid_request_type {
184 HID_REQ_GET_REPORT = 0x01,
185 HID_REQ_GET_IDLE = 0x02,
186 HID_REQ_GET_PROTOCOL = 0x03,
187 HID_REQ_SET_REPORT = 0x09,
188 HID_REQ_SET_IDLE = 0x0A,
189 HID_REQ_SET_PROTOCOL = 0x0B
192 enum libusb_hid_report_type {
193 HID_REPORT_TYPE_INPUT = 0x01,
194 HID_REPORT_TYPE_OUTPUT = 0x02,
195 HID_REPORT_TYPE_FEATURE = 0x03
198 struct hid_device_priv {
199 uint16_t vid;
200 uint16_t pid;
201 uint8_t config;
202 uint8_t nb_interfaces;
203 bool uses_report_ids[3]; // input, ouptput, feature
204 uint16_t input_report_size;
205 uint16_t output_report_size;
206 uint16_t feature_report_size;
207 WCHAR string[3][MAX_USB_STRING_LENGTH];
208 uint8_t string_index[3]; // man, prod, ser
211 typedef struct libusb_device_descriptor USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
212 struct windows_device_priv {
213 uint8_t depth; // distance to HCD
214 uint8_t port; // port number on the hub
215 uint8_t active_config;
216 struct libusb_device *parent_dev; // access to parent is required for usermode ops
217 struct windows_usb_api_backend const *apib;
218 char *path; // device interface path
219 int sub_api; // for WinUSB-like APIs
220 struct {
221 char *path; // each interface needs a device interface path,
222 struct windows_usb_api_backend const *apib; // an API backend (multiple drivers support),
223 int sub_api;
224 int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
225 uint8_t *endpoint;
226 bool restricted_functionality; // indicates if the interface functionality is restricted
227 // by Windows (eg. HID keyboards or mice cannot do R/W)
228 } usb_interface[USB_MAXINTERFACES];
229 struct hid_device_priv *hid;
230 USB_DEVICE_DESCRIPTOR dev_descriptor;
231 unsigned char **config_descriptor; // list of pointers to the cached config descriptors
234 static inline struct windows_device_priv *_device_priv(struct libusb_device *dev) {
235 return (struct windows_device_priv *)dev->os_priv;
238 static inline void windows_device_priv_init(libusb_device* dev) {
239 struct windows_device_priv* p = _device_priv(dev);
240 int i;
241 p->depth = 0;
242 p->port = 0;
243 p->parent_dev = NULL;
244 p->path = NULL;
245 p->apib = &usb_api_backend[USB_API_UNSUPPORTED];
246 p->sub_api = SUB_API_NOTSET;
247 p->hid = NULL;
248 p->active_config = 0;
249 p->config_descriptor = NULL;
250 memset(&(p->dev_descriptor), 0, sizeof(USB_DEVICE_DESCRIPTOR));
251 for (i=0; i<USB_MAXINTERFACES; i++) {
252 p->usb_interface[i].path = NULL;
253 p->usb_interface[i].apib = &usb_api_backend[USB_API_UNSUPPORTED];
254 p->usb_interface[i].sub_api = SUB_API_NOTSET;
255 p->usb_interface[i].nb_endpoints = 0;
256 p->usb_interface[i].endpoint = NULL;
257 p->usb_interface[i].restricted_functionality = false;
261 static inline void windows_device_priv_release(libusb_device* dev) {
262 struct windows_device_priv* p = _device_priv(dev);
263 int i;
264 safe_free(p->path);
265 if ((dev->num_configurations > 0) && (p->config_descriptor != NULL)) {
266 for (i=0; i < dev->num_configurations; i++)
267 safe_free(p->config_descriptor[i]);
269 safe_free(p->config_descriptor);
270 safe_free(p->hid);
271 for (i=0; i<USB_MAXINTERFACES; i++) {
272 safe_free(p->usb_interface[i].path);
273 safe_free(p->usb_interface[i].endpoint);
277 struct interface_handle_t {
278 HANDLE dev_handle; // WinUSB needs an extra handle for the file
279 HANDLE api_handle; // used by the API to communicate with the device
282 struct windows_device_handle_priv {
283 int active_interface;
284 struct interface_handle_t interface_handle[USB_MAXINTERFACES];
285 int autoclaim_count[USB_MAXINTERFACES]; // For auto-release
288 static inline struct windows_device_handle_priv *_device_handle_priv(
289 struct libusb_device_handle *handle)
291 return (struct windows_device_handle_priv *) handle->os_priv;
294 // used for async polling functions
295 struct windows_transfer_priv {
296 struct winfd pollable_fd;
297 uint8_t interface_number;
298 uint8_t *hid_buffer; // 1 byte extended data buffer, required for HID
299 uint8_t *hid_dest; // transfer buffer destination, required for HID
300 size_t hid_expected_size;
303 // used to match a device driver (including filter drivers) against a supported API
304 struct driver_lookup {
305 char list[MAX_KEY_LENGTH+1];// REG_MULTI_SZ list of services (driver) names
306 const DWORD reg_prop; // SPDRP registry key to use to retreive list
307 const char* designation; // internal designation (for debug output)
310 /* OLE32 dependency */
311 DLL_DECLARE_PREFIXED(WINAPI, HRESULT, p, CLSIDFromString, (LPCOLESTR, LPCLSID));
313 /* SetupAPI dependencies */
314 DLL_DECLARE_PREFIXED(WINAPI, HDEVINFO, p, SetupDiGetClassDevsA, (const GUID*, PCSTR, HWND, DWORD));
315 DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInfo, (HDEVINFO, DWORD, PSP_DEVINFO_DATA));
316 DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInterfaces, (HDEVINFO, PSP_DEVINFO_DATA,
317 const GUID*, DWORD, PSP_DEVICE_INTERFACE_DATA));
318 DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceInterfaceDetailA, (HDEVINFO, PSP_DEVICE_INTERFACE_DATA,
319 PSP_DEVICE_INTERFACE_DETAIL_DATA_A, DWORD, PDWORD, PSP_DEVINFO_DATA));
320 DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiDestroyDeviceInfoList, (HDEVINFO));
321 DLL_DECLARE_PREFIXED(WINAPI, HKEY, p, SetupDiOpenDevRegKey, (HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, REGSAM));
322 DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceRegistryPropertyA, (HDEVINFO,
323 PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD));
324 DLL_DECLARE_PREFIXED(WINAPI, HKEY, p, SetupDiOpenDeviceInterfaceRegKey, (HDEVINFO, PSP_DEVICE_INTERFACE_DATA, DWORD, DWORD));
325 DLL_DECLARE_PREFIXED(WINAPI, LONG, p, RegQueryValueExW, (HKEY, LPCWSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD));
326 DLL_DECLARE_PREFIXED(WINAPI, LONG, p, RegCloseKey, (HKEY));
329 * Windows DDK API definitions. Most of it copied from MinGW's includes
331 typedef DWORD DEVNODE, DEVINST;
332 typedef DEVNODE *PDEVNODE, *PDEVINST;
333 typedef DWORD RETURN_TYPE;
334 typedef RETURN_TYPE CONFIGRET;
336 #define CR_SUCCESS 0x00000000
337 #define CR_NO_SUCH_DEVNODE 0x0000000D
339 #define USB_DEVICE_DESCRIPTOR_TYPE LIBUSB_DT_DEVICE
340 #define USB_CONFIGURATION_DESCRIPTOR_TYPE LIBUSB_DT_CONFIG
341 #define USB_STRING_DESCRIPTOR_TYPE LIBUSB_DT_STRING
342 #define USB_INTERFACE_DESCRIPTOR_TYPE LIBUSB_DT_INTERFACE
343 #define USB_ENDPOINT_DESCRIPTOR_TYPE LIBUSB_DT_ENDPOINT
345 #define USB_REQUEST_GET_STATUS LIBUSB_REQUEST_GET_STATUS
346 #define USB_REQUEST_CLEAR_FEATURE LIBUSB_REQUEST_CLEAR_FEATURE
347 #define USB_REQUEST_SET_FEATURE LIBUSB_REQUEST_SET_FEATURE
348 #define USB_REQUEST_SET_ADDRESS LIBUSB_REQUEST_SET_ADDRESS
349 #define USB_REQUEST_GET_DESCRIPTOR LIBUSB_REQUEST_GET_DESCRIPTOR
350 #define USB_REQUEST_SET_DESCRIPTOR LIBUSB_REQUEST_SET_DESCRIPTOR
351 #define USB_REQUEST_GET_CONFIGURATION LIBUSB_REQUEST_GET_CONFIGURATION
352 #define USB_REQUEST_SET_CONFIGURATION LIBUSB_REQUEST_SET_CONFIGURATION
353 #define USB_REQUEST_GET_INTERFACE LIBUSB_REQUEST_GET_INTERFACE
354 #define USB_REQUEST_SET_INTERFACE LIBUSB_REQUEST_SET_INTERFACE
355 #define USB_REQUEST_SYNC_FRAME LIBUSB_REQUEST_SYNCH_FRAME
357 #define USB_GET_NODE_INFORMATION 258
358 #define USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 260
359 #define USB_GET_NODE_CONNECTION_NAME 261
360 #define USB_GET_HUB_CAPABILITIES 271
361 #if !defined(USB_GET_NODE_CONNECTION_INFORMATION_EX)
362 #define USB_GET_NODE_CONNECTION_INFORMATION_EX 274
363 #endif
364 #if !defined(USB_GET_HUB_CAPABILITIES_EX)
365 #define USB_GET_HUB_CAPABILITIES_EX 276
366 #endif
368 #ifndef METHOD_BUFFERED
369 #define METHOD_BUFFERED 0
370 #endif
371 #ifndef FILE_ANY_ACCESS
372 #define FILE_ANY_ACCESS 0x00000000
373 #endif
374 #ifndef FILE_DEVICE_UNKNOWN
375 #define FILE_DEVICE_UNKNOWN 0x00000022
376 #endif
377 #ifndef FILE_DEVICE_USB
378 #define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN
379 #endif
381 #ifndef CTL_CODE
382 #define CTL_CODE(DeviceType, Function, Method, Access)( \
383 ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
384 #endif
386 typedef enum USB_CONNECTION_STATUS {
387 NoDeviceConnected,
388 DeviceConnected,
389 DeviceFailedEnumeration,
390 DeviceGeneralFailure,
391 DeviceCausedOvercurrent,
392 DeviceNotEnoughPower,
393 DeviceNotEnoughBandwidth,
394 DeviceHubNestedTooDeeply,
395 DeviceInLegacyHub
396 } USB_CONNECTION_STATUS, *PUSB_CONNECTION_STATUS;
398 typedef enum USB_HUB_NODE {
399 UsbHub,
400 UsbMIParent
401 } USB_HUB_NODE;
403 /* Cfgmgr32.dll interface */
404 DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Parent, (PDEVINST, DEVINST, ULONG));
405 DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Child, (PDEVINST, DEVINST, ULONG));
406 DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Sibling, (PDEVINST, DEVINST, ULONG));
407 DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Device_IDA, (DEVINST, PCHAR, ULONG, ULONG));
409 #define IOCTL_USB_GET_HUB_CAPABILITIES_EX \
410 CTL_CODE( FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
412 #define IOCTL_USB_GET_HUB_CAPABILITIES \
413 CTL_CODE(FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES, METHOD_BUFFERED, FILE_ANY_ACCESS)
415 #define IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION \
416 CTL_CODE(FILE_DEVICE_USB, USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, METHOD_BUFFERED, FILE_ANY_ACCESS)
418 #define IOCTL_USB_GET_ROOT_HUB_NAME \
419 CTL_CODE(FILE_DEVICE_USB, HCD_GET_ROOT_HUB_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
421 #define IOCTL_USB_GET_NODE_INFORMATION \
422 CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_INFORMATION, METHOD_BUFFERED, FILE_ANY_ACCESS)
424 #define IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX \
425 CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
427 #define IOCTL_USB_GET_NODE_CONNECTION_ATTRIBUTES \
428 CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_ATTRIBUTES, METHOD_BUFFERED, FILE_ANY_ACCESS)
430 #define IOCTL_USB_GET_NODE_CONNECTION_NAME \
431 CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
433 // Most of the structures below need to be packed
434 #pragma pack(push, 1)
436 typedef struct USB_INTERFACE_DESCRIPTOR {
437 UCHAR bLength;
438 UCHAR bDescriptorType;
439 UCHAR bInterfaceNumber;
440 UCHAR bAlternateSetting;
441 UCHAR bNumEndpoints;
442 UCHAR bInterfaceClass;
443 UCHAR bInterfaceSubClass;
444 UCHAR bInterfaceProtocol;
445 UCHAR iInterface;
446 } USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
448 typedef struct USB_CONFIGURATION_DESCRIPTOR {
449 UCHAR bLength;
450 UCHAR bDescriptorType;
451 USHORT wTotalLength;
452 UCHAR bNumInterfaces;
453 UCHAR bConfigurationValue;
454 UCHAR iConfiguration;
455 UCHAR bmAttributes;
456 UCHAR MaxPower;
457 } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
459 typedef struct USB_CONFIGURATION_DESCRIPTOR_SHORT {
460 struct {
461 ULONG ConnectionIndex;
462 struct {
463 UCHAR bmRequest;
464 UCHAR bRequest;
465 USHORT wValue;
466 USHORT wIndex;
467 USHORT wLength;
468 } SetupPacket;
469 } req;
470 USB_CONFIGURATION_DESCRIPTOR data;
471 } USB_CONFIGURATION_DESCRIPTOR_SHORT;
473 typedef struct USB_ENDPOINT_DESCRIPTOR {
474 UCHAR bLength;
475 UCHAR bDescriptorType;
476 UCHAR bEndpointAddress;
477 UCHAR bmAttributes;
478 USHORT wMaxPacketSize;
479 UCHAR bInterval;
480 } USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
482 typedef struct USB_DESCRIPTOR_REQUEST {
483 ULONG ConnectionIndex;
484 struct {
485 UCHAR bmRequest;
486 UCHAR bRequest;
487 USHORT wValue;
488 USHORT wIndex;
489 USHORT wLength;
490 } SetupPacket;
491 // UCHAR Data[0];
492 } USB_DESCRIPTOR_REQUEST, *PUSB_DESCRIPTOR_REQUEST;
494 typedef struct USB_HUB_DESCRIPTOR {
495 UCHAR bDescriptorLength;
496 UCHAR bDescriptorType;
497 UCHAR bNumberOfPorts;
498 USHORT wHubCharacteristics;
499 UCHAR bPowerOnToPowerGood;
500 UCHAR bHubControlCurrent;
501 UCHAR bRemoveAndPowerMask[64];
502 } USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
504 typedef struct USB_ROOT_HUB_NAME {
505 ULONG ActualLength;
506 WCHAR RootHubName[1];
507 } USB_ROOT_HUB_NAME, *PUSB_ROOT_HUB_NAME;
509 typedef struct USB_ROOT_HUB_NAME_FIXED {
510 ULONG ActualLength;
511 WCHAR RootHubName[MAX_PATH_LENGTH];
512 } USB_ROOT_HUB_NAME_FIXED;
514 typedef struct USB_NODE_CONNECTION_NAME {
515 ULONG ConnectionIndex;
516 ULONG ActualLength;
517 WCHAR NodeName[1];
518 } USB_NODE_CONNECTION_NAME, *PUSB_NODE_CONNECTION_NAME;
520 typedef struct USB_NODE_CONNECTION_NAME_FIXED {
521 ULONG ConnectionIndex;
522 ULONG ActualLength;
523 WCHAR NodeName[MAX_PATH_LENGTH];
524 } USB_NODE_CONNECTION_NAME_FIXED;
526 typedef struct USB_HUB_NAME_FIXED {
527 union {
528 USB_ROOT_HUB_NAME_FIXED root;
529 USB_NODE_CONNECTION_NAME_FIXED node;
530 } u;
531 } USB_HUB_NAME_FIXED;
533 typedef struct USB_HUB_INFORMATION {
534 USB_HUB_DESCRIPTOR HubDescriptor;
535 BOOLEAN HubIsBusPowered;
536 } USB_HUB_INFORMATION, *PUSB_HUB_INFORMATION;
538 typedef struct USB_MI_PARENT_INFORMATION {
539 ULONG NumberOfInterfaces;
540 } USB_MI_PARENT_INFORMATION, *PUSB_MI_PARENT_INFORMATION;
542 typedef struct USB_NODE_INFORMATION {
543 USB_HUB_NODE NodeType;
544 union {
545 USB_HUB_INFORMATION HubInformation;
546 USB_MI_PARENT_INFORMATION MiParentInformation;
547 } u;
548 } USB_NODE_INFORMATION, *PUSB_NODE_INFORMATION;
550 typedef struct USB_PIPE_INFO {
551 USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
552 ULONG ScheduleOffset;
553 } USB_PIPE_INFO, *PUSB_PIPE_INFO;
555 typedef struct USB_NODE_CONNECTION_INFORMATION_EX {
556 ULONG ConnectionIndex;
557 USB_DEVICE_DESCRIPTOR DeviceDescriptor;
558 UCHAR CurrentConfigurationValue;
559 UCHAR Speed;
560 BOOLEAN DeviceIsHub;
561 USHORT DeviceAddress;
562 ULONG NumberOfOpenPipes;
563 USB_CONNECTION_STATUS ConnectionStatus;
564 // USB_PIPE_INFO PipeList[0];
565 } USB_NODE_CONNECTION_INFORMATION_EX, *PUSB_NODE_CONNECTION_INFORMATION_EX;
567 typedef struct USB_HUB_CAP_FLAGS {
568 ULONG HubIsHighSpeedCapable:1;
569 ULONG HubIsHighSpeed:1;
570 ULONG HubIsMultiTtCapable:1;
571 ULONG HubIsMultiTt:1;
572 ULONG HubIsRoot:1;
573 ULONG HubIsArmedWakeOnConnect:1;
574 ULONG ReservedMBZ:26;
575 } USB_HUB_CAP_FLAGS, *PUSB_HUB_CAP_FLAGS;
577 typedef struct USB_HUB_CAPABILITIES {
578 ULONG HubIs2xCapable : 1;
579 } USB_HUB_CAPABILITIES, *PUSB_HUB_CAPABILITIES;
581 typedef struct USB_HUB_CAPABILITIES_EX {
582 USB_HUB_CAP_FLAGS CapabilityFlags;
583 } USB_HUB_CAPABILITIES_EX, *PUSB_HUB_CAPABILITIES_EX;
585 #pragma pack(pop)
587 /* winusb.dll interface */
589 #define SHORT_PACKET_TERMINATE 0x01
590 #define AUTO_CLEAR_STALL 0x02
591 #define PIPE_TRANSFER_TIMEOUT 0x03
592 #define IGNORE_SHORT_PACKETS 0x04
593 #define ALLOW_PARTIAL_READS 0x05
594 #define AUTO_FLUSH 0x06
595 #define RAW_IO 0x07
596 #define MAXIMUM_TRANSFER_SIZE 0x08
597 #define AUTO_SUSPEND 0x81
598 #define SUSPEND_DELAY 0x83
599 #define DEVICE_SPEED 0x01
600 #define LowSpeed 0x01
601 #define FullSpeed 0x02
602 #define HighSpeed 0x03
604 typedef enum USBD_PIPE_TYPE {
605 UsbdPipeTypeControl,
606 UsbdPipeTypeIsochronous,
607 UsbdPipeTypeBulk,
608 UsbdPipeTypeInterrupt
609 } USBD_PIPE_TYPE;
611 typedef struct {
612 USBD_PIPE_TYPE PipeType;
613 UCHAR PipeId;
614 USHORT MaximumPacketSize;
615 UCHAR Interval;
616 } WINUSB_PIPE_INFORMATION, *PWINUSB_PIPE_INFORMATION;
618 #pragma pack(1)
619 typedef struct {
620 UCHAR request_type;
621 UCHAR request;
622 USHORT value;
623 USHORT index;
624 USHORT length;
625 } WINUSB_SETUP_PACKET, *PWINUSB_SETUP_PACKET;
626 #pragma pack()
628 typedef void *WINUSB_INTERFACE_HANDLE, *PWINUSB_INTERFACE_HANDLE;
630 typedef BOOL (WINAPI *WinUsb_AbortPipe_t)(
631 WINUSB_INTERFACE_HANDLE InterfaceHandle,
632 UCHAR PipeID
634 typedef BOOL (WINAPI *WinUsb_ControlTransfer_t)(
635 WINUSB_INTERFACE_HANDLE InterfaceHandle,
636 WINUSB_SETUP_PACKET SetupPacket,
637 PUCHAR Buffer,
638 ULONG BufferLength,
639 PULONG LengthTransferred,
640 LPOVERLAPPED Overlapped
642 typedef BOOL (WINAPI *WinUsb_FlushPipe_t)(
643 WINUSB_INTERFACE_HANDLE InterfaceHandle,
644 UCHAR PipeID
646 typedef BOOL (WINAPI *WinUsb_Free_t)(
647 WINUSB_INTERFACE_HANDLE InterfaceHandle
649 typedef BOOL (WINAPI *WinUsb_GetAssociatedInterface_t)(
650 WINUSB_INTERFACE_HANDLE InterfaceHandle,
651 UCHAR AssociatedInterfaceIndex,
652 PWINUSB_INTERFACE_HANDLE AssociatedInterfaceHandle
654 typedef BOOL (WINAPI *WinUsb_GetCurrentAlternateSetting_t)(
655 WINUSB_INTERFACE_HANDLE InterfaceHandle,
656 PUCHAR AlternateSetting
658 typedef BOOL (WINAPI *WinUsb_GetDescriptor_t)(
659 WINUSB_INTERFACE_HANDLE InterfaceHandle,
660 UCHAR DescriptorType,
661 UCHAR Index,
662 USHORT LanguageID,
663 PUCHAR Buffer,
664 ULONG BufferLength,
665 PULONG LengthTransferred
667 typedef BOOL (WINAPI *WinUsb_GetOverlappedResult_t)(
668 WINUSB_INTERFACE_HANDLE InterfaceHandle,
669 LPOVERLAPPED lpOverlapped,
670 LPDWORD lpNumberOfBytesTransferred,
671 BOOL bWait
673 typedef BOOL (WINAPI *WinUsb_GetPipePolicy_t)(
674 WINUSB_INTERFACE_HANDLE InterfaceHandle,
675 UCHAR PipeID,
676 ULONG PolicyType,
677 PULONG ValueLength,
678 PVOID Value
680 typedef BOOL (WINAPI *WinUsb_GetPowerPolicy_t)(
681 WINUSB_INTERFACE_HANDLE InterfaceHandle,
682 ULONG PolicyType,
683 PULONG ValueLength,
684 PVOID Value
686 typedef BOOL (WINAPI *WinUsb_Initialize_t)(
687 HANDLE DeviceHandle,
688 PWINUSB_INTERFACE_HANDLE InterfaceHandle
690 typedef BOOL (WINAPI *WinUsb_QueryDeviceInformation_t)(
691 WINUSB_INTERFACE_HANDLE InterfaceHandle,
692 ULONG InformationType,
693 PULONG BufferLength,
694 PVOID Buffer
696 typedef BOOL (WINAPI *WinUsb_QueryInterfaceSettings_t)(
697 WINUSB_INTERFACE_HANDLE InterfaceHandle,
698 UCHAR AlternateSettingNumber,
699 PUSB_INTERFACE_DESCRIPTOR UsbAltInterfaceDescriptor
701 typedef BOOL (WINAPI *WinUsb_QueryPipe_t)(
702 WINUSB_INTERFACE_HANDLE InterfaceHandle,
703 UCHAR AlternateInterfaceNumber,
704 UCHAR PipeIndex,
705 PWINUSB_PIPE_INFORMATION PipeInformation
707 typedef BOOL (WINAPI *WinUsb_ReadPipe_t)(
708 WINUSB_INTERFACE_HANDLE InterfaceHandle,
709 UCHAR PipeID,
710 PUCHAR Buffer,
711 ULONG BufferLength,
712 PULONG LengthTransferred,
713 LPOVERLAPPED Overlapped
715 typedef BOOL (WINAPI *WinUsb_ResetPipe_t)(
716 WINUSB_INTERFACE_HANDLE InterfaceHandle,
717 UCHAR PipeID
719 typedef BOOL (WINAPI *WinUsb_SetCurrentAlternateSetting_t)(
720 WINUSB_INTERFACE_HANDLE InterfaceHandle,
721 UCHAR AlternateSetting
723 typedef BOOL (WINAPI *WinUsb_SetPipePolicy_t)(
724 WINUSB_INTERFACE_HANDLE InterfaceHandle,
725 UCHAR PipeID,
726 ULONG PolicyType,
727 ULONG ValueLength,
728 PVOID Value
730 typedef BOOL (WINAPI *WinUsb_SetPowerPolicy_t)(
731 WINUSB_INTERFACE_HANDLE InterfaceHandle,
732 ULONG PolicyType,
733 ULONG ValueLength,
734 PVOID Value
736 typedef BOOL (WINAPI *WinUsb_WritePipe_t)(
737 WINUSB_INTERFACE_HANDLE InterfaceHandle,
738 UCHAR PipeID,
739 PUCHAR Buffer,
740 ULONG BufferLength,
741 PULONG LengthTransferred,
742 LPOVERLAPPED Overlapped
744 typedef BOOL (WINAPI *WinUsb_ResetDevice_t)(
745 WINUSB_INTERFACE_HANDLE InterfaceHandle
748 /* /!\ These must match the ones from the official libusbk.h */
749 typedef enum _KUSB_FNID
751 KUSB_FNID_Init,
752 KUSB_FNID_Free,
753 KUSB_FNID_ClaimInterface,
754 KUSB_FNID_ReleaseInterface,
755 KUSB_FNID_SetAltInterface,
756 KUSB_FNID_GetAltInterface,
757 KUSB_FNID_GetDescriptor,
758 KUSB_FNID_ControlTransfer,
759 KUSB_FNID_SetPowerPolicy,
760 KUSB_FNID_GetPowerPolicy,
761 KUSB_FNID_SetConfiguration,
762 KUSB_FNID_GetConfiguration,
763 KUSB_FNID_ResetDevice,
764 KUSB_FNID_Initialize,
765 KUSB_FNID_SelectInterface,
766 KUSB_FNID_GetAssociatedInterface,
767 KUSB_FNID_Clone,
768 KUSB_FNID_QueryInterfaceSettings,
769 KUSB_FNID_QueryDeviceInformation,
770 KUSB_FNID_SetCurrentAlternateSetting,
771 KUSB_FNID_GetCurrentAlternateSetting,
772 KUSB_FNID_QueryPipe,
773 KUSB_FNID_SetPipePolicy,
774 KUSB_FNID_GetPipePolicy,
775 KUSB_FNID_ReadPipe,
776 KUSB_FNID_WritePipe,
777 KUSB_FNID_ResetPipe,
778 KUSB_FNID_AbortPipe,
779 KUSB_FNID_FlushPipe,
780 KUSB_FNID_IsoReadPipe,
781 KUSB_FNID_IsoWritePipe,
782 KUSB_FNID_GetCurrentFrameNumber,
783 KUSB_FNID_GetOverlappedResult,
784 KUSB_FNID_GetProperty,
785 KUSB_FNID_COUNT,
786 } KUSB_FNID;
788 typedef struct _KLIB_VERSION {
789 INT Major;
790 INT Minor;
791 INT Micro;
792 INT Nano;
793 } KLIB_VERSION;
794 typedef KLIB_VERSION* PKLIB_VERSION;
796 typedef BOOL (WINAPI *LibK_GetProcAddress_t)(
797 PVOID* ProcAddress,
798 ULONG DriverID,
799 ULONG FunctionID
802 typedef VOID (WINAPI *LibK_GetVersion_t)(
803 PKLIB_VERSION Version
806 struct winusb_interface {
807 bool initialized;
808 WinUsb_AbortPipe_t AbortPipe;
809 WinUsb_ControlTransfer_t ControlTransfer;
810 WinUsb_FlushPipe_t FlushPipe;
811 WinUsb_Free_t Free;
812 WinUsb_GetAssociatedInterface_t GetAssociatedInterface;
813 WinUsb_GetCurrentAlternateSetting_t GetCurrentAlternateSetting;
814 WinUsb_GetDescriptor_t GetDescriptor;
815 WinUsb_GetOverlappedResult_t GetOverlappedResult;
816 WinUsb_GetPipePolicy_t GetPipePolicy;
817 WinUsb_GetPowerPolicy_t GetPowerPolicy;
818 WinUsb_Initialize_t Initialize;
819 WinUsb_QueryDeviceInformation_t QueryDeviceInformation;
820 WinUsb_QueryInterfaceSettings_t QueryInterfaceSettings;
821 WinUsb_QueryPipe_t QueryPipe;
822 WinUsb_ReadPipe_t ReadPipe;
823 WinUsb_ResetPipe_t ResetPipe;
824 WinUsb_SetCurrentAlternateSetting_t SetCurrentAlternateSetting;
825 WinUsb_SetPipePolicy_t SetPipePolicy;
826 WinUsb_SetPowerPolicy_t SetPowerPolicy;
827 WinUsb_WritePipe_t WritePipe;
828 WinUsb_ResetDevice_t ResetDevice;
831 /* hid.dll interface */
833 #define HIDP_STATUS_SUCCESS 0x110000
834 typedef void* PHIDP_PREPARSED_DATA;
836 #pragma pack(1)
837 typedef struct {
838 ULONG Size;
839 USHORT VendorID;
840 USHORT ProductID;
841 USHORT VersionNumber;
842 } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
843 #pragma pack()
845 typedef USHORT USAGE;
846 typedef struct {
847 USAGE Usage;
848 USAGE UsagePage;
849 USHORT InputReportByteLength;
850 USHORT OutputReportByteLength;
851 USHORT FeatureReportByteLength;
852 USHORT Reserved[17];
853 USHORT NumberLinkCollectionNodes;
854 USHORT NumberInputButtonCaps;
855 USHORT NumberInputValueCaps;
856 USHORT NumberInputDataIndices;
857 USHORT NumberOutputButtonCaps;
858 USHORT NumberOutputValueCaps;
859 USHORT NumberOutputDataIndices;
860 USHORT NumberFeatureButtonCaps;
861 USHORT NumberFeatureValueCaps;
862 USHORT NumberFeatureDataIndices;
863 } HIDP_CAPS, *PHIDP_CAPS;
865 typedef enum _HIDP_REPORT_TYPE {
866 HidP_Input,
867 HidP_Output,
868 HidP_Feature
869 } HIDP_REPORT_TYPE;
871 typedef struct _HIDP_VALUE_CAPS {
872 USAGE UsagePage;
873 UCHAR ReportID;
874 BOOLEAN IsAlias;
875 USHORT BitField;
876 USHORT LinkCollection;
877 USAGE LinkUsage;
878 USAGE LinkUsagePage;
879 BOOLEAN IsRange;
880 BOOLEAN IsStringRange;
881 BOOLEAN IsDesignatorRange;
882 BOOLEAN IsAbsolute;
883 BOOLEAN HasNull;
884 UCHAR Reserved;
885 USHORT BitSize;
886 USHORT ReportCount;
887 USHORT Reserved2[5];
888 ULONG UnitsExp;
889 ULONG Units;
890 LONG LogicalMin, LogicalMax;
891 LONG PhysicalMin, PhysicalMax;
892 union {
893 struct {
894 USAGE UsageMin, UsageMax;
895 USHORT StringMin, StringMax;
896 USHORT DesignatorMin, DesignatorMax;
897 USHORT DataIndexMin, DataIndexMax;
898 } Range;
899 struct {
900 USAGE Usage, Reserved1;
901 USHORT StringIndex, Reserved2;
902 USHORT DesignatorIndex, Reserved3;
903 USHORT DataIndex, Reserved4;
904 } NotRange;
905 } u;
906 } HIDP_VALUE_CAPS, *PHIDP_VALUE_CAPS;
908 DLL_DECLARE(WINAPI, BOOL, HidD_GetAttributes, (HANDLE, PHIDD_ATTRIBUTES));
909 DLL_DECLARE(WINAPI, VOID, HidD_GetHidGuid, (LPGUID));
910 DLL_DECLARE(WINAPI, BOOL, HidD_GetPreparsedData, (HANDLE, PHIDP_PREPARSED_DATA *));
911 DLL_DECLARE(WINAPI, BOOL, HidD_FreePreparsedData, (PHIDP_PREPARSED_DATA));
912 DLL_DECLARE(WINAPI, BOOL, HidD_GetManufacturerString, (HANDLE, PVOID, ULONG));
913 DLL_DECLARE(WINAPI, BOOL, HidD_GetProductString, (HANDLE, PVOID, ULONG));
914 DLL_DECLARE(WINAPI, BOOL, HidD_GetSerialNumberString, (HANDLE, PVOID, ULONG));
915 DLL_DECLARE(WINAPI, LONG, HidP_GetCaps, (PHIDP_PREPARSED_DATA, PHIDP_CAPS));
916 DLL_DECLARE(WINAPI, BOOL, HidD_SetNumInputBuffers, (HANDLE, ULONG));
917 DLL_DECLARE(WINAPI, BOOL, HidD_SetFeature, (HANDLE, PVOID, ULONG));
918 DLL_DECLARE(WINAPI, BOOL, HidD_GetFeature, (HANDLE, PVOID, ULONG));
919 DLL_DECLARE(WINAPI, BOOL, HidD_GetPhysicalDescriptor, (HANDLE, PVOID, ULONG));
920 DLL_DECLARE(WINAPI, BOOL, HidD_GetInputReport, (HANDLE, PVOID, ULONG));
921 DLL_DECLARE(WINAPI, BOOL, HidD_SetOutputReport, (HANDLE, PVOID, ULONG));
922 DLL_DECLARE(WINAPI, BOOL, HidD_FlushQueue, (HANDLE));
923 DLL_DECLARE(WINAPI, BOOL, HidP_GetValueCaps, (HIDP_REPORT_TYPE, PHIDP_VALUE_CAPS, PULONG, PHIDP_PREPARSED_DATA));