1 /*
2 * Windows backend for libusb 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
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #ifndef LIBUSB_WINDOWS_WINUSB_H
24 #define LIBUSB_WINDOWS_WINUSB_H
25
26 #include <devioctl.h>
27 #include <guiddef.h>
28
29 #include "windows_common.h"
30
31 #define MAX_CTRL_BUFFER_LENGTH 4096
32 #define MAX_USB_STRING_LENGTH 128
33 #define MAX_HID_REPORT_SIZE 1024
34 #define MAX_HID_DESCRIPTOR_SIZE 256
35 #define MAX_GUID_STRING_LENGTH 40
36 #define MAX_PATH_LENGTH 256
37 #define MAX_KEY_LENGTH 256
38 #define LIST_SEPARATOR ';'
39
40 // Handle code for HID interface that have been claimed ("dibs")
41 #define INTERFACE_CLAIMED ((HANDLE)(intptr_t)0xD1B5)
42 // Additional return code for HID operations that completed synchronously
43 #define LIBUSB_COMPLETED (LIBUSB_SUCCESS + 1)
44
45 // http://msdn.microsoft.com/en-us/library/ff545978.aspx
46 // http://msdn.microsoft.com/en-us/library/ff545972.aspx
47 // http://msdn.microsoft.com/en-us/library/ff545982.aspx
48 static const GUID GUID_DEVINTERFACE_USB_HOST_CONTROLLER = {0x3ABF6F2D, 0x71C4, 0x462A, {0x8A, 0x92, 0x1E, 0x68, 0x61, 0xE6, 0xAF, 0x27}};
49 static const GUID GUID_DEVINTERFACE_USB_HUB = {0xF18A0E88, 0xC30C, 0x11D0, {0x88, 0x15, 0x00, 0xA0, 0xC9, 0x06, 0xBE, 0xD8}};
50 static const GUID GUID_DEVINTERFACE_USB_DEVICE = {0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}};
51 static const GUID GUID_DEVINTERFACE_LIBUSB0_FILTER = {0xF9F3FF14, 0xAE21, 0x48A0, {0x8A, 0x25, 0x80, 0x11, 0xA7, 0xA9, 0x31, 0xD9}};
52
53 // The following define MUST be == sizeof(USB_DESCRIPTOR_REQUEST)
54 #define USB_DESCRIPTOR_REQUEST_SIZE 12U
55
56 /*
57 * Multiple USB API backend support
58 */
59 #define USB_API_UNSUPPORTED 0
60 #define USB_API_HUB 1
61 #define USB_API_COMPOSITE 2
62 #define USB_API_WINUSBX 3
63 #define USB_API_HID 4
64 #define USB_API_MAX 5
65
66 // Sub-APIs for WinUSB-like driver APIs (WinUSB, libusbK, libusb-win32 through the libusbK DLL)
67 // Must have the same values as the KUSB_DRVID enum from libusbk.h
68 #define SUB_API_NOTSET -1
69 #define SUB_API_LIBUSBK 0
70 #define SUB_API_LIBUSB0 1
71 #define SUB_API_WINUSB 2
72 #define SUB_API_MAX 3
73
74 struct windows_usb_api_backend {
75 const uint8_t id;
76 const char * const designation;
77 const char * const * const driver_name_list; // Driver name, without .sys, e.g. "usbccgp"
78 const uint8_t nb_driver_names;
79 bool (*init)(struct libusb_context *ctx);
80 void (*exit)(void);
81 int (*open)(int sub_api, struct libusb_device_handle *dev_handle);
82 void (*close)(int sub_api, struct libusb_device_handle *dev_handle);
83 int (*configure_endpoints)(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface);
84 int (*claim_interface)(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface);
85 int (*set_interface_altsetting)(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting);
86 int (*release_interface)(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface);
87 int (*clear_halt)(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint);
88 int (*reset_device)(int sub_api, struct libusb_device_handle *dev_handle);
89 int (*submit_bulk_transfer)(int sub_api, struct usbi_transfer *itransfer);
90 int (*submit_iso_transfer)(int sub_api, struct usbi_transfer *itransfer);
91 int (*submit_control_transfer)(int sub_api, struct usbi_transfer *itransfer);
92 int (*cancel_transfer)(int sub_api, struct usbi_transfer *itransfer);
93 enum libusb_transfer_status (*copy_transfer_data)(int sub_api, struct usbi_transfer *itransfer, DWORD length);
94 };
95
96 extern const struct windows_usb_api_backend usb_api_backend[USB_API_MAX];
97
98 #define PRINT_UNSUPPORTED_API(fname) \
99 usbi_dbg(NULL, "unsupported API call for '%s' " \
100 "(unrecognized device driver)", #fname)
101
102 #define CHECK_SUPPORTED_API(apip, fname) \
103 do { \
104 if ((apip)->fname == NULL) { \
105 PRINT_UNSUPPORTED_API(fname); \
106 return LIBUSB_ERROR_NOT_SUPPORTED; \
107 } \
108 } while (0)
109
110 /*
111 * private structures definition
112 * with inline pseudo constructors/destructors
113 */
114
115 // TODO (v2+): move hid desc to libusb.h?
116 struct libusb_hid_descriptor {
117 uint8_t bLength;
118 uint8_t bDescriptorType;
119 uint16_t bcdHID;
120 uint8_t bCountryCode;
121 uint8_t bNumDescriptors;
122 uint8_t bClassDescriptorType;
123 uint16_t wClassDescriptorLength;
124 };
125
126 #define LIBUSB_DT_HID_SIZE 9
127 #define HID_MAX_CONFIG_DESC_SIZE (LIBUSB_DT_CONFIG_SIZE + LIBUSB_DT_INTERFACE_SIZE \
128 + LIBUSB_DT_HID_SIZE + 2 * LIBUSB_DT_ENDPOINT_SIZE)
129 #define HID_MAX_REPORT_SIZE 1024
130 #define HID_IN_EP 0x81
131 #define HID_OUT_EP 0x02
132 #define LIBUSB_REQ_RECIPIENT(request_type) ((request_type) & 0x1F)
133 #define LIBUSB_REQ_TYPE(request_type) ((request_type) & (0x03 << 5))
134 #define LIBUSB_REQ_IN(request_type) ((request_type) & LIBUSB_ENDPOINT_IN)
135 #define LIBUSB_REQ_OUT(request_type) (!LIBUSB_REQ_IN(request_type))
136
137 // The following are used for HID reports IOCTLs
138 #define HID_IN_CTL_CODE(id) \
139 CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_IN_DIRECT, FILE_ANY_ACCESS)
140 #define HID_OUT_CTL_CODE(id) \
141 CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
142
143 #define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
144 #define IOCTL_HID_GET_INPUT_REPORT HID_OUT_CTL_CODE(104)
145 #define IOCTL_HID_SET_FEATURE HID_IN_CTL_CODE(100)
146 #define IOCTL_HID_SET_OUTPUT_REPORT HID_IN_CTL_CODE(101)
147
148 enum libusb_hid_request_type {
149 HID_REQ_GET_REPORT = 0x01,
150 HID_REQ_GET_IDLE = 0x02,
151 HID_REQ_GET_PROTOCOL = 0x03,
152 HID_REQ_SET_REPORT = 0x09,
153 HID_REQ_SET_IDLE = 0x0A,
154 HID_REQ_SET_PROTOCOL = 0x0B
155 };
156
157 enum libusb_hid_report_type {
158 HID_REPORT_TYPE_INPUT = 0x01,
159 HID_REPORT_TYPE_OUTPUT = 0x02,
160 HID_REPORT_TYPE_FEATURE = 0x03
161 };
162
163 struct hid_device_priv {
164 uint16_t vid;
165 uint16_t pid;
166 uint8_t config;
167 uint8_t nb_interfaces;
168 bool uses_report_ids[3]; // input, ouptput, feature
169 uint16_t input_report_size;
170 uint16_t output_report_size;
171 uint16_t feature_report_size;
172 uint16_t usage;
173 uint16_t usagePage;
174 WCHAR string[3][MAX_USB_STRING_LENGTH];
175 uint8_t string_index[3]; // man, prod, ser
176 };
177
winusb_device_priv_init(struct libusb_device * dev)178 static inline struct winusb_device_priv *winusb_device_priv_init(struct libusb_device *dev)
179 {
180 struct winusb_device_priv *priv = usbi_get_device_priv(dev);
181 int i;
182
183 priv->apib = &usb_api_backend[USB_API_UNSUPPORTED];
184 priv->sub_api = SUB_API_NOTSET;
185 for (i = 0; i < USB_MAXINTERFACES; i++) {
186 priv->usb_interface[i].apib = &usb_api_backend[USB_API_UNSUPPORTED];
187 priv->usb_interface[i].sub_api = SUB_API_NOTSET;
188 }
189
190 return priv;
191 }
192
winusb_device_priv_release(struct libusb_device * dev)193 static inline void winusb_device_priv_release(struct libusb_device *dev)
194 {
195 struct winusb_device_priv *priv = usbi_get_device_priv(dev);
196 int i;
197
198 free(priv->dev_id);
199 free(priv->path);
200 if ((dev->device_descriptor.bNumConfigurations > 0) && (priv->config_descriptor != NULL)) {
201 for (i = 0; i < dev->device_descriptor.bNumConfigurations; i++) {
202 if (priv->config_descriptor[i] == NULL)
203 continue;
204 free((UCHAR *)priv->config_descriptor[i] - USB_DESCRIPTOR_REQUEST_SIZE);
205 }
206 }
207 free(priv->config_descriptor);
208 free(priv->hid);
209 for (i = 0; i < USB_MAXINTERFACES; i++) {
210 free(priv->usb_interface[i].path);
211 free(priv->usb_interface[i].endpoint);
212 }
213 }
214
215 // used to match a device driver (including filter drivers) against a supported API
216 struct driver_lookup {
217 char list[MAX_KEY_LENGTH + 1]; // REG_MULTI_SZ list of services (driver) names
218 const DWORD reg_prop; // SPDRP registry key to use to retrieve list
219 const char *designation; // internal designation (for debug output)
220 };
221
222 /*
223 * Windows DDK API definitions. Most of it copied from MinGW's includes
224 */
225 typedef DWORD DEVNODE, DEVINST;
226 typedef DEVNODE *PDEVNODE, *PDEVINST;
227 typedef DWORD RETURN_TYPE;
228 typedef RETURN_TYPE CONFIGRET;
229
230 #define CR_SUCCESS 0x00000000
231
232 /* Cfgmgr32 dependencies */
233 DLL_DECLARE_HANDLE(Cfgmgr32);
234 DLL_DECLARE_FUNC(WINAPI, CONFIGRET, CM_Get_Parent, (PDEVINST, DEVINST, ULONG));
235 DLL_DECLARE_FUNC(WINAPI, CONFIGRET, CM_Get_Child, (PDEVINST, DEVINST, ULONG));
236
237 /* AdvAPI32 dependencies */
238 DLL_DECLARE_HANDLE(AdvAPI32);
239 DLL_DECLARE_FUNC_PREFIXED(WINAPI, LONG, p, RegQueryValueExA, (HKEY, LPCSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD));
240 DLL_DECLARE_FUNC_PREFIXED(WINAPI, LONG, p, RegCloseKey, (HKEY));
241
242 /* SetupAPI dependencies */
243 DLL_DECLARE_HANDLE(SetupAPI);
244 DLL_DECLARE_FUNC_PREFIXED(WINAPI, HDEVINFO, p, SetupDiGetClassDevsA, (LPCGUID, PCSTR, HWND, DWORD));
245 DLL_DECLARE_FUNC_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInfo, (HDEVINFO, DWORD, PSP_DEVINFO_DATA));
246 DLL_DECLARE_FUNC_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInterfaces, (HDEVINFO, PSP_DEVINFO_DATA,
247 LPCGUID, DWORD, PSP_DEVICE_INTERFACE_DATA));
248 DLL_DECLARE_FUNC_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceInstanceIdA, (HDEVINFO, PSP_DEVINFO_DATA,
249 PCSTR, DWORD, PDWORD));
250 DLL_DECLARE_FUNC_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceInterfaceDetailA, (HDEVINFO, PSP_DEVICE_INTERFACE_DATA,
251 PSP_DEVICE_INTERFACE_DETAIL_DATA_A, DWORD, PDWORD, PSP_DEVINFO_DATA));
252 DLL_DECLARE_FUNC_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceRegistryPropertyA, (HDEVINFO,
253 PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD));
254 DLL_DECLARE_FUNC_PREFIXED(WINAPI, BOOL, p, SetupDiDestroyDeviceInfoList, (HDEVINFO));
255 DLL_DECLARE_FUNC_PREFIXED(WINAPI, HKEY, p, SetupDiOpenDevRegKey, (HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, REGSAM));
256 DLL_DECLARE_FUNC_PREFIXED(WINAPI, HKEY, p, SetupDiOpenDeviceInterfaceRegKey, (HDEVINFO, PSP_DEVICE_INTERFACE_DATA, DWORD, DWORD));
257
258 #define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN
259
260 #define USB_GET_NODE_INFORMATION 258
261 #define USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 260
262 #define USB_GET_NODE_CONNECTION_INFORMATION_EX 274
263 #define USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 279
264
265 #define USB_CTL_CODE(id) \
266 CTL_CODE(FILE_DEVICE_USB, (id), METHOD_BUFFERED, FILE_ANY_ACCESS)
267
268 #define IOCTL_USB_GET_NODE_INFORMATION \
269 USB_CTL_CODE(USB_GET_NODE_INFORMATION)
270
271 #define IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION \
272 USB_CTL_CODE(USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION)
273
274 #define IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX \
275 USB_CTL_CODE(USB_GET_NODE_CONNECTION_INFORMATION_EX)
276
277 #define IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 \
278 USB_CTL_CODE(USB_GET_NODE_CONNECTION_INFORMATION_EX_V2)
279
280 typedef enum _USB_CONNECTION_STATUS {
281 NoDeviceConnected,
282 DeviceConnected,
283 DeviceFailedEnumeration,
284 DeviceGeneralFailure,
285 DeviceCausedOvercurrent,
286 DeviceNotEnoughPower,
287 DeviceNotEnoughBandwidth,
288 DeviceHubNestedTooDeeply,
289 DeviceInLegacyHub,
290 DeviceEnumerating,
291 DeviceReset
292 } USB_CONNECTION_STATUS;
293
294 typedef enum _USB_DEVICE_SPEED {
295 UsbLowSpeed = 0,
296 UsbFullSpeed,
297 UsbHighSpeed,
298 UsbSuperSpeed,
299 UsbSuperSpeedPlus // Not in Microsoft headers
300 } USB_DEVICE_SPEED;
301
302 typedef enum _USB_HUB_NODE {
303 UsbHub,
304 UsbMIParent
305 } USB_HUB_NODE;
306
307 #if defined(_MSC_VER)
308 // disable /W4 MSVC warnings that are benign
309 #pragma warning(push)
310 #pragma warning(disable:4214) // bit field types other than int
311 #endif
312
313 // Most of the structures below need to be packed
314 #include <pshpack1.h>
315
316 typedef struct _USB_HUB_DESCRIPTOR {
317 UCHAR bDescriptorLength;
318 UCHAR bDescriptorType;
319 UCHAR bNumberOfPorts;
320 USHORT wHubCharacteristics;
321 UCHAR bPowerOnToPowerGood;
322 UCHAR bHubControlCurrent;
323 UCHAR bRemoveAndPowerMask[64];
324 } USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
325
326 typedef struct _USB_HUB_INFORMATION {
327 USB_HUB_DESCRIPTOR HubDescriptor;
328 BOOLEAN HubIsBusPowered;
329 } USB_HUB_INFORMATION, *PUSB_HUB_INFORMATION;
330
331 typedef struct _USB_NODE_INFORMATION {
332 USB_HUB_NODE NodeType;
333 union {
334 USB_HUB_INFORMATION HubInformation;
335 // USB_MI_PARENT_INFORMATION MiParentInformation;
336 } u;
337 } USB_NODE_INFORMATION, *PUSB_NODE_INFORMATION;
338
339 typedef struct _USB_DESCRIPTOR_REQUEST {
340 ULONG ConnectionIndex;
341 struct {
342 UCHAR bmRequest;
343 UCHAR bRequest;
344 USHORT wValue;
345 USHORT wIndex;
346 USHORT wLength;
347 } SetupPacket;
348 // UCHAR Data[0];
349 } USB_DESCRIPTOR_REQUEST, *PUSB_DESCRIPTOR_REQUEST;
350
351 typedef struct _USB_CONFIGURATION_DESCRIPTOR_SHORT {
352 USB_DESCRIPTOR_REQUEST req;
353 USB_CONFIGURATION_DESCRIPTOR desc;
354 } USB_CONFIGURATION_DESCRIPTOR_SHORT;
355
356 typedef struct USB_INTERFACE_DESCRIPTOR {
357 UCHAR bLength;
358 UCHAR bDescriptorType;
359 UCHAR bInterfaceNumber;
360 UCHAR bAlternateSetting;
361 UCHAR bNumEndpoints;
362 UCHAR bInterfaceClass;
363 UCHAR bInterfaceSubClass;
364 UCHAR bInterfaceProtocol;
365 UCHAR iInterface;
366 } USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
367
368 typedef struct _USB_NODE_CONNECTION_INFORMATION_EX {
369 ULONG ConnectionIndex;
370 USB_DEVICE_DESCRIPTOR DeviceDescriptor;
371 UCHAR CurrentConfigurationValue;
372 UCHAR Speed;
373 BOOLEAN DeviceIsHub;
374 USHORT DeviceAddress;
375 ULONG NumberOfOpenPipes;
376 USB_CONNECTION_STATUS ConnectionStatus;
377 // USB_PIPE_INFO PipeList[0];
378 } USB_NODE_CONNECTION_INFORMATION_EX, *PUSB_NODE_CONNECTION_INFORMATION_EX;
379
380 typedef union _USB_PROTOCOLS {
381 ULONG ul;
382 struct {
383 ULONG Usb110:1;
384 ULONG Usb200:1;
385 ULONG Usb300:1;
386 ULONG ReservedMBZ:29;
387 };
388 } USB_PROTOCOLS, *PUSB_PROTOCOLS;
389
390 typedef union _USB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS {
391 ULONG ul;
392 struct {
393 ULONG DeviceIsOperatingAtSuperSpeedOrHigher:1;
394 ULONG DeviceIsSuperSpeedCapableOrHigher:1;
395 ULONG DeviceIsOperatingAtSuperSpeedPlusOrHigher:1;
396 ULONG DeviceIsSuperSpeedPlusCapableOrHigher:1;
397 ULONG ReservedMBZ:28;
398 };
399 } USB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS, *PUSB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS;
400
401 typedef struct _USB_NODE_CONNECTION_INFORMATION_EX_V2 {
402 ULONG ConnectionIndex;
403 ULONG Length;
404 USB_PROTOCOLS SupportedUsbProtocols;
405 USB_NODE_CONNECTION_INFORMATION_EX_V2_FLAGS Flags;
406 } USB_NODE_CONNECTION_INFORMATION_EX_V2, *PUSB_NODE_CONNECTION_INFORMATION_EX_V2;
407
408 #include <poppack.h>
409
410 #if defined(_MSC_VER)
411 // Restore original warnings
412 #pragma warning(pop)
413 #endif
414
415 /* winusb.dll interface */
416
417 /* pipe policies */
418 #define SHORT_PACKET_TERMINATE 0x01
419 #define AUTO_CLEAR_STALL 0x02
420 #define PIPE_TRANSFER_TIMEOUT 0x03
421 #define IGNORE_SHORT_PACKETS 0x04
422 #define ALLOW_PARTIAL_READS 0x05
423 #define AUTO_FLUSH 0x06
424 #define RAW_IO 0x07
425 #define MAXIMUM_TRANSFER_SIZE 0x08
426 /* libusbK */
427 #define ISO_ALWAYS_START_ASAP 0x21
428
429 typedef struct _USBD_ISO_PACKET_DESCRIPTOR {
430 ULONG Offset;
431 ULONG Length;
432 USBD_STATUS Status;
433 } USBD_ISO_PACKET_DESCRIPTOR, *PUSBD_ISO_PACKET_DESCRIPTOR;
434
435 typedef enum _USBD_PIPE_TYPE {
436 UsbdPipeTypeControl,
437 UsbdPipeTypeIsochronous,
438 UsbdPipeTypeBulk,
439 UsbdPipeTypeInterrupt
440 } USBD_PIPE_TYPE;
441
442 typedef struct {
443 USBD_PIPE_TYPE PipeType;
444 UCHAR PipeId;
445 USHORT MaximumPacketSize;
446 UCHAR Interval;
447 ULONG MaximumBytesPerInterval;
448 } WINUSB_PIPE_INFORMATION_EX, *PWINUSB_PIPE_INFORMATION_EX;
449
450 #include <pshpack1.h>
451
452 typedef struct _WINUSB_SETUP_PACKET {
453 UCHAR RequestType;
454 UCHAR Request;
455 USHORT Value;
456 USHORT Index;
457 USHORT Length;
458 } WINUSB_SETUP_PACKET, *PWINUSB_SETUP_PACKET;
459
460 #include <poppack.h>
461
462 typedef PVOID WINUSB_INTERFACE_HANDLE, *PWINUSB_INTERFACE_HANDLE;
463 typedef PVOID WINUSB_ISOCH_BUFFER_HANDLE, *PWINUSB_ISOCH_BUFFER_HANDLE;
464
465 typedef BOOL (WINAPI *WinUsb_AbortPipe_t)(
466 WINUSB_INTERFACE_HANDLE InterfaceHandle,
467 UCHAR PipeID
468 );
469 typedef BOOL (WINAPI *WinUsb_ControlTransfer_t)(
470 WINUSB_INTERFACE_HANDLE InterfaceHandle,
471 WINUSB_SETUP_PACKET SetupPacket,
472 PUCHAR Buffer,
473 ULONG BufferLength,
474 PULONG LengthTransferred,
475 LPOVERLAPPED Overlapped
476 );
477 typedef BOOL (WINAPI *WinUsb_FlushPipe_t)(
478 WINUSB_INTERFACE_HANDLE InterfaceHandle,
479 UCHAR PipeID
480 );
481 typedef BOOL (WINAPI *WinUsb_Free_t)(
482 WINUSB_INTERFACE_HANDLE InterfaceHandle
483 );
484 typedef BOOL (WINAPI *WinUsb_GetAssociatedInterface_t)(
485 WINUSB_INTERFACE_HANDLE InterfaceHandle,
486 UCHAR AssociatedInterfaceIndex,
487 PWINUSB_INTERFACE_HANDLE AssociatedInterfaceHandle
488 );
489 typedef BOOL (WINAPI *WinUsb_Initialize_t)(
490 HANDLE DeviceHandle,
491 PWINUSB_INTERFACE_HANDLE InterfaceHandle
492 );
493 typedef BOOL (WINAPI *WinUsb_QueryPipeEx_t)(
494 WINUSB_INTERFACE_HANDLE InterfaceHandle,
495 UCHAR AlternateInterfaceHandle,
496 UCHAR PipeIndex,
497 PWINUSB_PIPE_INFORMATION_EX PipeInformationEx
498 );
499 typedef BOOL (WINAPI *WinUsb_ReadIsochPipeAsap_t)(
500 PWINUSB_ISOCH_BUFFER_HANDLE BufferHandle,
501 ULONG Offset,
502 ULONG Length,
503 BOOL ContinueStream,
504 ULONG NumberOfPackets,
505 PUSBD_ISO_PACKET_DESCRIPTOR IsoPacketDescriptors,
506 LPOVERLAPPED Overlapped
507 );
508 typedef BOOL (WINAPI *WinUsb_ReadPipe_t)(
509 WINUSB_INTERFACE_HANDLE InterfaceHandle,
510 UCHAR PipeID,
511 PUCHAR Buffer,
512 ULONG BufferLength,
513 PULONG LengthTransferred,
514 LPOVERLAPPED Overlapped
515 );
516 typedef BOOL (WINAPI *WinUsb_RegisterIsochBuffer_t)(
517 WINUSB_INTERFACE_HANDLE InterfaceHandle,
518 UCHAR PipeID,
519 PVOID Buffer,
520 ULONG BufferLength,
521 PWINUSB_ISOCH_BUFFER_HANDLE BufferHandle
522 );
523 typedef BOOL (WINAPI *WinUsb_ResetPipe_t)(
524 WINUSB_INTERFACE_HANDLE InterfaceHandle,
525 UCHAR PipeID
526 );
527 typedef BOOL (WINAPI *WinUsb_SetCurrentAlternateSetting_t)(
528 WINUSB_INTERFACE_HANDLE InterfaceHandle,
529 UCHAR AlternateSetting
530 );
531 typedef BOOL (WINAPI *WinUsb_SetPipePolicy_t)(
532 WINUSB_INTERFACE_HANDLE InterfaceHandle,
533 UCHAR PipeID,
534 ULONG PolicyType,
535 ULONG ValueLength,
536 PVOID Value
537 );
538 typedef BOOL (WINAPI *WinUsb_UnregisterIsochBuffer_t)(
539 WINUSB_ISOCH_BUFFER_HANDLE BufferHandle
540 );
541 typedef BOOL (WINAPI *WinUsb_WriteIsochPipeAsap_t)(
542 WINUSB_ISOCH_BUFFER_HANDLE BufferHandle,
543 ULONG Offset,
544 ULONG Length,
545 BOOL ContinueStream,
546 LPOVERLAPPED Overlapped
547 );
548 typedef BOOL (WINAPI *WinUsb_WritePipe_t)(
549 WINUSB_INTERFACE_HANDLE InterfaceHandle,
550 UCHAR PipeID,
551 PUCHAR Buffer,
552 ULONG BufferLength,
553 PULONG LengthTransferred,
554 LPOVERLAPPED Overlapped
555 );
556
557 /* /!\ These must match the ones from the official libusbk.h */
558 typedef enum _KUSB_FNID {
559 KUSB_FNID_Init,
560 KUSB_FNID_Free,
561 KUSB_FNID_ClaimInterface,
562 KUSB_FNID_ReleaseInterface,
563 KUSB_FNID_SetAltInterface,
564 KUSB_FNID_GetAltInterface,
565 KUSB_FNID_GetDescriptor,
566 KUSB_FNID_ControlTransfer,
567 KUSB_FNID_SetPowerPolicy,
568 KUSB_FNID_GetPowerPolicy,
569 KUSB_FNID_SetConfiguration,
570 KUSB_FNID_GetConfiguration,
571 KUSB_FNID_ResetDevice,
572 KUSB_FNID_Initialize,
573 KUSB_FNID_SelectInterface,
574 KUSB_FNID_GetAssociatedInterface,
575 KUSB_FNID_Clone,
576 KUSB_FNID_QueryInterfaceSettings,
577 KUSB_FNID_QueryDeviceInformation,
578 KUSB_FNID_SetCurrentAlternateSetting,
579 KUSB_FNID_GetCurrentAlternateSetting,
580 KUSB_FNID_QueryPipe,
581 KUSB_FNID_SetPipePolicy,
582 KUSB_FNID_GetPipePolicy,
583 KUSB_FNID_ReadPipe,
584 KUSB_FNID_WritePipe,
585 KUSB_FNID_ResetPipe,
586 KUSB_FNID_AbortPipe,
587 KUSB_FNID_FlushPipe,
588 KUSB_FNID_IsoReadPipe,
589 KUSB_FNID_IsoWritePipe,
590 KUSB_FNID_GetCurrentFrameNumber,
591 KUSB_FNID_GetOverlappedResult,
592 KUSB_FNID_GetProperty,
593 KUSB_FNID_COUNT,
594 } KUSB_FNID;
595
596 typedef struct _KLIB_VERSION {
597 INT Major;
598 INT Minor;
599 INT Micro;
600 INT Nano;
601 } KLIB_VERSION, *PKLIB_VERSION;
602
603 typedef BOOL (WINAPI *LibK_GetProcAddress_t)(
604 PVOID ProcAddress,
605 INT DriverID,
606 INT FunctionID
607 );
608
609 typedef VOID (WINAPI *LibK_GetVersion_t)(
610 PKLIB_VERSION Version
611 );
612
613 typedef BOOL (WINAPI *LibK_ResetDevice_t)(
614 WINUSB_INTERFACE_HANDLE InterfaceHandle
615 );
616
617 //KISO_PACKET is equivalent of libusb_iso_packet_descriptor except uses absolute "offset" field instead of sequential Lengths
618 typedef struct _KISO_PACKET {
619 UINT offset;
620 USHORT actual_length; //changed from libusbk_shared.h "Length" for clarity
621 USHORT status;
622 } KISO_PACKET, *PKISO_PACKET;
623
624 typedef enum _KISO_FLAG {
625 KISO_FLAG_NONE = 0,
626 KISO_FLAG_SET_START_FRAME = 0x00000001,
627 } KISO_FLAG;
628
629 //KISO_CONTEXT is the conceptual equivalent of libusb_transfer except is isochronous-specific and must match libusbk's version
630 typedef struct _KISO_CONTEXT {
631 KISO_FLAG Flags;
632 UINT StartFrame;
633 SHORT ErrorCount;
634 SHORT NumberOfPackets;
635 UINT UrbHdrStatus;
636 KISO_PACKET IsoPackets[0];
637 } KISO_CONTEXT, *PKISO_CONTEXT;
638
639 typedef BOOL(WINAPI *LibK_IsoReadPipe_t)(
640 WINUSB_INTERFACE_HANDLE InterfaceHandle,
641 UCHAR PipeID,
642 PUCHAR Buffer,
643 ULONG BufferLength,
644 LPOVERLAPPED Overlapped,
645 PKISO_CONTEXT IsoContext
646 );
647
648 typedef BOOL(WINAPI *LibK_IsoWritePipe_t)(
649 WINUSB_INTERFACE_HANDLE InterfaceHandle,
650 UCHAR PipeID,
651 PUCHAR Buffer,
652 ULONG BufferLength,
653 LPOVERLAPPED Overlapped,
654 PKISO_CONTEXT IsoContext
655 );
656
657 struct winusb_interface {
658 HMODULE hDll;
659 WinUsb_AbortPipe_t AbortPipe;
660 WinUsb_ControlTransfer_t ControlTransfer;
661 WinUsb_FlushPipe_t FlushPipe;
662 WinUsb_Free_t Free;
663 WinUsb_GetAssociatedInterface_t GetAssociatedInterface;
664 WinUsb_Initialize_t Initialize;
665 WinUsb_ReadPipe_t ReadPipe;
666 WinUsb_ResetPipe_t ResetPipe;
667 WinUsb_SetCurrentAlternateSetting_t SetCurrentAlternateSetting;
668 WinUsb_SetPipePolicy_t SetPipePolicy;
669 WinUsb_WritePipe_t WritePipe;
670 union {
671 struct {
672 // Isochoronous functions for libusbK sub api:
673 LibK_IsoReadPipe_t IsoReadPipe;
674 LibK_IsoWritePipe_t IsoWritePipe;
675 // Reset device function for libusbK sub api:
676 LibK_ResetDevice_t ResetDevice;
677 };
678 struct {
679 // Isochronous functions for WinUSB sub api:
680 WinUsb_QueryPipeEx_t QueryPipeEx;
681 WinUsb_ReadIsochPipeAsap_t ReadIsochPipeAsap;
682 WinUsb_RegisterIsochBuffer_t RegisterIsochBuffer;
683 WinUsb_UnregisterIsochBuffer_t UnregisterIsochBuffer;
684 WinUsb_WriteIsochPipeAsap_t WriteIsochPipeAsap;
685 };
686 };
687 };
688
689 /* hid.dll interface */
690
691 #define HIDP_STATUS_SUCCESS 0x110000
692 typedef void * PHIDP_PREPARSED_DATA;
693
694 #include <pshpack1.h>
695
696 typedef struct _HIDD_ATTIRBUTES {
697 ULONG Size;
698 USHORT VendorID;
699 USHORT ProductID;
700 USHORT VersionNumber;
701 } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
702
703 #include <poppack.h>
704
705 typedef USHORT USAGE;
706 typedef struct _HIDP_CAPS {
707 USAGE Usage;
708 USAGE UsagePage;
709 USHORT InputReportByteLength;
710 USHORT OutputReportByteLength;
711 USHORT FeatureReportByteLength;
712 USHORT Reserved[17];
713 USHORT NumberLinkCollectionNodes;
714 USHORT NumberInputButtonCaps;
715 USHORT NumberInputValueCaps;
716 USHORT NumberInputDataIndices;
717 USHORT NumberOutputButtonCaps;
718 USHORT NumberOutputValueCaps;
719 USHORT NumberOutputDataIndices;
720 USHORT NumberFeatureButtonCaps;
721 USHORT NumberFeatureValueCaps;
722 USHORT NumberFeatureDataIndices;
723 } HIDP_CAPS, *PHIDP_CAPS;
724
725 typedef enum _HIDP_REPORT_TYPE {
726 HidP_Input,
727 HidP_Output,
728 HidP_Feature
729 } HIDP_REPORT_TYPE;
730
731 typedef struct _HIDP_VALUE_CAPS {
732 USAGE UsagePage;
733 UCHAR ReportID;
734 BOOLEAN IsAlias;
735 USHORT BitField;
736 USHORT LinkCollection;
737 USAGE LinkUsage;
738 USAGE LinkUsagePage;
739 BOOLEAN IsRange;
740 BOOLEAN IsStringRange;
741 BOOLEAN IsDesignatorRange;
742 BOOLEAN IsAbsolute;
743 BOOLEAN HasNull;
744 UCHAR Reserved;
745 USHORT BitSize;
746 USHORT ReportCount;
747 USHORT Reserved2[5];
748 ULONG UnitsExp;
749 ULONG Units;
750 LONG LogicalMin, LogicalMax;
751 LONG PhysicalMin, PhysicalMax;
752 union {
753 struct {
754 USAGE UsageMin, UsageMax;
755 USHORT StringMin, StringMax;
756 USHORT DesignatorMin, DesignatorMax;
757 USHORT DataIndexMin, DataIndexMax;
758 } Range;
759 struct {
760 USAGE Usage, Reserved1;
761 USHORT StringIndex, Reserved2;
762 USHORT DesignatorIndex, Reserved3;
763 USHORT DataIndex, Reserved4;
764 } NotRange;
765 } u;
766 } HIDP_VALUE_CAPS, *PHIDP_VALUE_CAPS;
767
768 DLL_DECLARE_HANDLE(hid);
769 DLL_DECLARE_FUNC(WINAPI, VOID, HidD_GetHidGuid, (LPGUID));
770 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetAttributes, (HANDLE, PHIDD_ATTRIBUTES));
771 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetPreparsedData, (HANDLE, PHIDP_PREPARSED_DATA *));
772 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_FreePreparsedData, (PHIDP_PREPARSED_DATA));
773 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetManufacturerString, (HANDLE, PVOID, ULONG));
774 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetProductString, (HANDLE, PVOID, ULONG));
775 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetSerialNumberString, (HANDLE, PVOID, ULONG));
776 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetIndexedString, (HANDLE, ULONG, PVOID, ULONG));
777 DLL_DECLARE_FUNC(WINAPI, LONG, HidP_GetCaps, (PHIDP_PREPARSED_DATA, PHIDP_CAPS));
778 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_SetNumInputBuffers, (HANDLE, ULONG));
779 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_GetPhysicalDescriptor, (HANDLE, PVOID, ULONG));
780 DLL_DECLARE_FUNC(WINAPI, BOOL, HidD_FlushQueue, (HANDLE));
781 DLL_DECLARE_FUNC(WINAPI, BOOL, HidP_GetValueCaps, (HIDP_REPORT_TYPE, PHIDP_VALUE_CAPS, PULONG, PHIDP_PREPARSED_DATA));
782
783 #endif
784