windows_winusb.h 24 KB

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