windows_usb.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Windows backend for libusb 1.0
  3. * Copyright (C) 2009-2010 Pete Batard <pbatard@gmail.com>
  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. #pragma once
  23. #if defined(_MSC_VER)
  24. // disable /W4 MSVC warnings that are benign
  25. #pragma warning(disable:4127) // conditional expression is constant
  26. #pragma warning(disable:4100) // unreferenced formal parameter
  27. #pragma warning(disable:4214) // bit field types other than int
  28. #pragma warning(disable:4201) // nameless struct/union
  29. #endif
  30. // Windows API default is uppercase - ugh!
  31. #if !defined(bool)
  32. #define bool BOOL
  33. #endif
  34. #if !defined(true)
  35. #define true TRUE
  36. #endif
  37. #if !defined(false)
  38. #define false FALSE
  39. #endif
  40. // Missing from MSVC6 setupapi.h
  41. #if !defined(SPDRP_ADDRESS)
  42. #define SPDRP_ADDRESS 28
  43. #endif
  44. #if !defined(SPDRP_INSTALL_STATE)
  45. #define SPDRP_INSTALL_STATE 34
  46. #endif
  47. #if defined(__CYGWIN__ )
  48. // cygwin produces a warning unless these prototypes are defined
  49. extern int _snprintf(char *buffer, size_t count, const char *format, ...);
  50. extern char *_strdup(const char *strSource);
  51. // _beginthreadex is MSVCRT => unavailable for cygwin. Fallback to using CreateThread
  52. #define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, (LPTHREAD_START_ROUTINE)c, d, e, f)
  53. #endif
  54. #define safe_free(p) do {if (p != NULL) {free((void*)p); p = NULL;}} while(0)
  55. #define safe_closehandle(h) do {if (h != INVALID_HANDLE_VALUE) {CloseHandle(h); h = INVALID_HANDLE_VALUE;}} while(0)
  56. #define safe_min(a, b) min((size_t)(a), (size_t)(b))
  57. #define safe_strcp(dst, dst_max, src, count) do {memcpy(dst, src, safe_min(count, dst_max)); \
  58. ((char*)dst)[safe_min(count, dst_max)-1] = 0;} while(0)
  59. #define safe_strcpy(dst, dst_max, src) safe_strcp(dst, dst_max, src, safe_strlen(src)+1)
  60. #define safe_strncat(dst, dst_max, src, count) strncat(dst, src, safe_min(count, dst_max - safe_strlen(dst) - 1))
  61. #define safe_strcat(dst, dst_max, src) safe_strncat(dst, dst_max, src, safe_strlen(src)+1)
  62. #define safe_strcmp(str1, str2) strcmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2))
  63. #define safe_strncmp(str1, str2, count) strncmp(((str1==NULL)?"<NULL>":str1), ((str2==NULL)?"<NULL>":str2), count)
  64. #define safe_strlen(str) ((str==NULL)?0:strlen(str))
  65. #define safe_sprintf _snprintf
  66. #define safe_unref_device(dev) do {if (dev != NULL) {libusb_unref_device(dev); dev = NULL;}} while(0)
  67. #define wchar_to_utf8_ms(wstr, str, strlen) WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, strlen, NULL, NULL)
  68. static inline void upperize(char* str) {
  69. size_t i;
  70. if (str == NULL) return;
  71. for (i=0; i<safe_strlen(str); i++)
  72. str[i] = (char)toupper((int)str[i]);
  73. }
  74. #define MAX_CTRL_BUFFER_LENGTH 4096
  75. #define MAX_USB_DEVICES 256
  76. #define MAX_USB_STRING_LENGTH 128
  77. #define MAX_GUID_STRING_LENGTH 40
  78. #define MAX_PATH_LENGTH 128
  79. #define MAX_KEY_LENGTH 256
  80. #define MAX_TIMER_SEMAPHORES 128
  81. #define TIMER_REQUEST_RETRY_MS 100
  82. #define ERR_BUFFER_SIZE 256
  83. #define LIST_SEPARATOR ';'
  84. #define HTAB_SIZE 1021
  85. // http://msdn.microsoft.com/en-us/library/ff545978.aspx
  86. // http://msdn.microsoft.com/en-us/library/ff545972.aspx
  87. // http://msdn.microsoft.com/en-us/library/ff545982.aspx
  88. #if !defined(GUID_DEVINTERFACE_USB_HOST_CONTROLLER)
  89. const GUID GUID_DEVINTERFACE_USB_HOST_CONTROLLER = { 0x3ABF6F2D, 0x71C4, 0x462A, {0x8A, 0x92, 0x1E, 0x68, 0x61, 0xE6, 0xAF, 0x27} };
  90. #endif
  91. #if !defined(GUID_DEVINTERFACE_USB_DEVICE)
  92. const GUID GUID_DEVINTERFACE_USB_DEVICE = { 0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED} };
  93. #endif
  94. #if !defined(GUID_DEVINTERFACE_USB_HUB)
  95. const GUID GUID_DEVINTERFACE_USB_HUB = { 0xF18A0E88, 0xC30C, 0x11D0, {0x88, 0x15, 0x00, 0xA0, 0xC9, 0x06, 0xBE, 0xD8} };
  96. #endif
  97. const GUID GUID_NULL = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
  98. /*
  99. * Multiple USB API backend support
  100. */
  101. #define USB_API_UNSUPPORTED 0
  102. #define USB_API_HUB 1
  103. #define USB_API_COMPOSITE 2
  104. #define USB_API_WINUSB 3
  105. #define USB_API_MAX 4
  106. #define CLASS_GUID_UNSUPPORTED GUID_NULL
  107. const GUID CLASS_GUID_LIBUSB_WINUSB = { 0x78A1C341, 0x4539, 0x11D3, {0xB8, 0x8D, 0x00, 0xC0, 0x4F, 0xAD, 0x51, 0x71} };
  108. const GUID CLASS_GUID_COMPOSITE = { 0x36FC9E60, 0xC465, 0x11cF, {0x80, 0x56, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00} };
  109. struct windows_usb_api_backend {
  110. const uint8_t id;
  111. const char* designation;
  112. const GUID *class_guid; // The Class GUID (for fallback in case the driver name cannot be read)
  113. const char **driver_name_list; // Driver name, without .sys, e.g. "usbccgp"
  114. const uint8_t nb_driver_names;
  115. int (*init)(struct libusb_context *ctx);
  116. int (*exit)(void);
  117. int (*open)(struct libusb_device_handle *dev_handle);
  118. void (*close)(struct libusb_device_handle *dev_handle);
  119. int (*claim_interface)(struct libusb_device_handle *dev_handle, int iface);
  120. int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle, int iface, int altsetting);
  121. int (*release_interface)(struct libusb_device_handle *dev_handle, int iface);
  122. int (*clear_halt)(struct libusb_device_handle *dev_handle, unsigned char endpoint);
  123. int (*reset_device)(struct libusb_device_handle *dev_handle);
  124. int (*submit_bulk_transfer)(struct usbi_transfer *itransfer);
  125. int (*submit_iso_transfer)(struct usbi_transfer *itransfer);
  126. int (*submit_control_transfer)(struct usbi_transfer *itransfer);
  127. int (*abort_control)(struct usbi_transfer *itransfer);
  128. int (*abort_transfers)(struct usbi_transfer *itransfer);
  129. int (*copy_transfer_data)(struct usbi_transfer *itransfer, uint32_t io_size);
  130. };
  131. extern const struct windows_usb_api_backend usb_api_backend[USB_API_MAX];
  132. #define PRINT_UNSUPPORTED_API(fname) \
  133. usbi_dbg("unsupported API call for '" \
  134. #fname "' (unrecognized device driver)"); \
  135. return LIBUSB_ERROR_NOT_SUPPORTED;
  136. /*
  137. * private structures definition
  138. * with inline pseudo constructors/destructors
  139. */
  140. typedef struct libusb_device_descriptor USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
  141. struct windows_device_priv {
  142. uint8_t depth; // distance to HCD
  143. uint8_t port; // port number on the hub
  144. struct libusb_device *parent_dev; // access to parent is required for usermode ops
  145. char *path; // device interface path
  146. struct windows_usb_api_backend const *apib;
  147. struct {
  148. char *path; // each interface needs a device interface path,
  149. struct windows_usb_api_backend const *apib; // an API backend (multiple drivers support),
  150. int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
  151. uint8_t *endpoint;
  152. } usb_interface[USB_MAXINTERFACES];
  153. uint8_t composite_api_flags; // composite devices require additional data
  154. uint8_t active_config;
  155. USB_DEVICE_DESCRIPTOR dev_descriptor;
  156. unsigned char **config_descriptor; // list of pointers to the cached config descriptors
  157. };
  158. static inline struct windows_device_priv *_device_priv(struct libusb_device *dev) {
  159. return (struct windows_device_priv *)dev->os_priv;
  160. }
  161. static inline void windows_device_priv_init(libusb_device* dev) {
  162. struct windows_device_priv* p = _device_priv(dev);
  163. int i;
  164. p->depth = 0;
  165. p->port = 0;
  166. p->parent_dev = NULL;
  167. p->path = NULL;
  168. p->apib = &usb_api_backend[USB_API_UNSUPPORTED];
  169. p->composite_api_flags = 0;
  170. p->active_config = 0;
  171. p->config_descriptor = NULL;
  172. memset(&(p->dev_descriptor), 0, sizeof(USB_DEVICE_DESCRIPTOR));
  173. for (i=0; i<USB_MAXINTERFACES; i++) {
  174. p->usb_interface[i].path = NULL;
  175. p->usb_interface[i].apib = &usb_api_backend[USB_API_UNSUPPORTED];
  176. p->usb_interface[i].nb_endpoints = 0;
  177. p->usb_interface[i].endpoint = NULL;
  178. }
  179. }
  180. static inline void windows_device_priv_release(libusb_device* dev) {
  181. struct windows_device_priv* p = _device_priv(dev);
  182. int i;
  183. safe_free(p->path);
  184. if ((dev->num_configurations > 0) && (p->config_descriptor != NULL)) {
  185. for (i=0; i < dev->num_configurations; i++)
  186. safe_free(p->config_descriptor[i]);
  187. }
  188. safe_free(p->config_descriptor);
  189. for (i=0; i<USB_MAXINTERFACES; i++) {
  190. safe_free(p->usb_interface[i].path);
  191. safe_free(p->usb_interface[i].endpoint);
  192. }
  193. }
  194. struct interface_handle_t {
  195. HANDLE dev_handle; // WinUSB needs an extra handle for the file
  196. HANDLE api_handle; // used by the API to communicate with the device
  197. };
  198. struct windows_device_handle_priv {
  199. int active_interface;
  200. struct interface_handle_t interface_handle[USB_MAXINTERFACES];
  201. int autoclaim_count[USB_MAXINTERFACES]; // For auto-release
  202. };
  203. static inline struct windows_device_handle_priv *_device_handle_priv(
  204. struct libusb_device_handle *handle)
  205. {
  206. return (struct windows_device_handle_priv *) handle->os_priv;
  207. }
  208. // used for async polling functions
  209. struct windows_transfer_priv {
  210. struct winfd pollable_fd;
  211. uint8_t interface_number;
  212. };
  213. // used to match a device driver (including filter drivers) against a supported API
  214. struct driver_lookup {
  215. char list[MAX_KEY_LENGTH+1];// REG_MULTI_SZ list of services (driver) names
  216. const DWORD reg_prop; // SPDRP registry key to use to retreive list
  217. const char* designation; // internal designation (for debug output)
  218. };
  219. /*
  220. * API macros - from libusb-win32 1.x
  221. */
  222. #define DLL_DECLARE_PREFIXNAME(api, ret, prefixname, name, args) \
  223. typedef ret (api * __dll_##name##_t)args; \
  224. static __dll_##name##_t prefixname = NULL
  225. #define DLL_LOAD_PREFIXNAME(dll, prefixname, name, ret_on_failure) \
  226. do { \
  227. HMODULE h = GetModuleHandleA(#dll); \
  228. if (!h) \
  229. h = LoadLibraryA(#dll); \
  230. if (!h) { \
  231. if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; }\
  232. else { break; } \
  233. } \
  234. prefixname = (__dll_##name##_t)GetProcAddress(h, #name); \
  235. if (prefixname) break; \
  236. prefixname = (__dll_##name##_t)GetProcAddress(h, #name "A"); \
  237. if (prefixname) break; \
  238. prefixname = (__dll_##name##_t)GetProcAddress(h, #name "W"); \
  239. if (prefixname) break; \
  240. if(ret_on_failure) \
  241. return LIBUSB_ERROR_NOT_FOUND; \
  242. } while(0)
  243. #define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXNAME(api, ret, name, name, args)
  244. #define DLL_LOAD(dll, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, name, name, ret_on_failure)
  245. #define DLL_DECLARE_PREFIXED(api, ret, prefix, name, args) DLL_DECLARE_PREFIXNAME(api, ret, prefix##name, name, args)
  246. #define DLL_LOAD_PREFIXED(dll, prefix, name, ret_on_failure) DLL_LOAD_PREFIXNAME(dll, prefix##name, name, ret_on_failure)
  247. /* OLE32 dependency */
  248. DLL_DECLARE_PREFIXED(WINAPI, HRESULT, p, CLSIDFromString, (LPCOLESTR, LPCLSID));
  249. /* SetupAPI dependencies */
  250. DLL_DECLARE_PREFIXED(WINAPI, HDEVINFO, p, SetupDiGetClassDevsA, (const GUID*, PCSTR, HWND, DWORD));
  251. DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInfo, (HDEVINFO, DWORD, PSP_DEVINFO_DATA));
  252. DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInterfaces, (HDEVINFO, PSP_DEVINFO_DATA,
  253. const GUID*, DWORD, PSP_DEVICE_INTERFACE_DATA));
  254. DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceInterfaceDetailA, (HDEVINFO, PSP_DEVICE_INTERFACE_DATA,
  255. PSP_DEVICE_INTERFACE_DETAIL_DATA_A, DWORD, PDWORD, PSP_DEVINFO_DATA));
  256. DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiDestroyDeviceInfoList, (HDEVINFO));
  257. DLL_DECLARE_PREFIXED(WINAPI, HKEY, p, SetupDiOpenDevRegKey, (HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, REGSAM));
  258. DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceRegistryPropertyA, (HDEVINFO,
  259. PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD));
  260. DLL_DECLARE_PREFIXED(WINAPI, LONG, p, RegQueryValueExW, (HKEY, LPCWSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD));
  261. DLL_DECLARE_PREFIXED(WINAPI, LONG, p, RegCloseKey, (HKEY));
  262. /*
  263. * Windows DDK API definitions. Most of it copied from MinGW's includes
  264. */
  265. typedef DWORD DEVNODE, DEVINST;
  266. typedef DEVNODE *PDEVNODE, *PDEVINST;
  267. typedef DWORD RETURN_TYPE;
  268. typedef RETURN_TYPE CONFIGRET;
  269. #define CR_SUCCESS 0x00000000
  270. #define CR_NO_SUCH_DEVNODE 0x0000000D
  271. #define USB_DEVICE_DESCRIPTOR_TYPE LIBUSB_DT_DEVICE
  272. #define USB_CONFIGURATION_DESCRIPTOR_TYPE LIBUSB_DT_CONFIG
  273. #define USB_STRING_DESCRIPTOR_TYPE LIBUSB_DT_STRING
  274. #define USB_INTERFACE_DESCRIPTOR_TYPE LIBUSB_DT_INTERFACE
  275. #define USB_ENDPOINT_DESCRIPTOR_TYPE LIBUSB_DT_ENDPOINT
  276. #define USB_REQUEST_GET_STATUS LIBUSB_REQUEST_GET_STATUS
  277. #define USB_REQUEST_CLEAR_FEATURE LIBUSB_REQUEST_CLEAR_FEATURE
  278. #define USB_REQUEST_SET_FEATURE LIBUSB_REQUEST_SET_FEATURE
  279. #define USB_REQUEST_SET_ADDRESS LIBUSB_REQUEST_SET_ADDRESS
  280. #define USB_REQUEST_GET_DESCRIPTOR LIBUSB_REQUEST_GET_DESCRIPTOR
  281. #define USB_REQUEST_SET_DESCRIPTOR LIBUSB_REQUEST_SET_DESCRIPTOR
  282. #define USB_REQUEST_GET_CONFIGURATION LIBUSB_REQUEST_GET_CONFIGURATION
  283. #define USB_REQUEST_SET_CONFIGURATION LIBUSB_REQUEST_SET_CONFIGURATION
  284. #define USB_REQUEST_GET_INTERFACE LIBUSB_REQUEST_GET_INTERFACE
  285. #define USB_REQUEST_SET_INTERFACE LIBUSB_REQUEST_SET_INTERFACE
  286. #define USB_REQUEST_SYNC_FRAME LIBUSB_REQUEST_SYNCH_FRAME
  287. #define USB_GET_NODE_INFORMATION 258
  288. #define USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 260
  289. #define USB_GET_NODE_CONNECTION_NAME 261
  290. #define USB_GET_HUB_CAPABILITIES 271
  291. #if !defined(USB_GET_NODE_CONNECTION_INFORMATION_EX)
  292. #define USB_GET_NODE_CONNECTION_INFORMATION_EX 274
  293. #endif
  294. #if !defined(USB_GET_HUB_CAPABILITIES_EX)
  295. #define USB_GET_HUB_CAPABILITIES_EX 276
  296. #endif
  297. #ifndef METHOD_BUFFERED
  298. #define METHOD_BUFFERED 0
  299. #endif
  300. #ifndef FILE_ANY_ACCESS
  301. #define FILE_ANY_ACCESS 0x00000000
  302. #endif
  303. #ifndef FILE_DEVICE_UNKNOWN
  304. #define FILE_DEVICE_UNKNOWN 0x00000022
  305. #endif
  306. #ifndef FILE_DEVICE_USB
  307. #define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN
  308. #endif
  309. #ifndef CTL_CODE
  310. #define CTL_CODE(DeviceType, Function, Method, Access)( \
  311. ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
  312. #endif
  313. typedef enum USB_CONNECTION_STATUS {
  314. NoDeviceConnected,
  315. DeviceConnected,
  316. DeviceFailedEnumeration,
  317. DeviceGeneralFailure,
  318. DeviceCausedOvercurrent,
  319. DeviceNotEnoughPower,
  320. DeviceNotEnoughBandwidth,
  321. DeviceHubNestedTooDeeply,
  322. DeviceInLegacyHub
  323. } USB_CONNECTION_STATUS, *PUSB_CONNECTION_STATUS;
  324. typedef enum USB_HUB_NODE {
  325. UsbHub,
  326. UsbMIParent
  327. } USB_HUB_NODE;
  328. /* Cfgmgr32.dll interface */
  329. DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Parent, (PDEVINST, DEVINST, ULONG));
  330. DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Child, (PDEVINST, DEVINST, ULONG));
  331. DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Sibling, (PDEVINST, DEVINST, ULONG));
  332. DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Device_IDA, (DEVINST, PCHAR, ULONG, ULONG));
  333. #define IOCTL_USB_GET_HUB_CAPABILITIES_EX \
  334. CTL_CODE( FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
  335. #define IOCTL_USB_GET_HUB_CAPABILITIES \
  336. CTL_CODE(FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES, METHOD_BUFFERED, FILE_ANY_ACCESS)
  337. #define IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION \
  338. CTL_CODE(FILE_DEVICE_USB, USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, METHOD_BUFFERED, FILE_ANY_ACCESS)
  339. #define IOCTL_USB_GET_ROOT_HUB_NAME \
  340. CTL_CODE(FILE_DEVICE_USB, HCD_GET_ROOT_HUB_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
  341. #define IOCTL_USB_GET_NODE_INFORMATION \
  342. CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_INFORMATION, METHOD_BUFFERED, FILE_ANY_ACCESS)
  343. #define IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX \
  344. CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
  345. #define IOCTL_USB_GET_NODE_CONNECTION_ATTRIBUTES \
  346. CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_ATTRIBUTES, METHOD_BUFFERED, FILE_ANY_ACCESS)
  347. #define IOCTL_USB_GET_NODE_CONNECTION_NAME \
  348. CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
  349. // Most of the structures below need to be packed
  350. #pragma pack(push, 1)
  351. typedef struct USB_INTERFACE_DESCRIPTOR {
  352. UCHAR bLength;
  353. UCHAR bDescriptorType;
  354. UCHAR bInterfaceNumber;
  355. UCHAR bAlternateSetting;
  356. UCHAR bNumEndpoints;
  357. UCHAR bInterfaceClass;
  358. UCHAR bInterfaceSubClass;
  359. UCHAR bInterfaceProtocol;
  360. UCHAR iInterface;
  361. } USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
  362. typedef struct USB_CONFIGURATION_DESCRIPTOR {
  363. UCHAR bLength;
  364. UCHAR bDescriptorType;
  365. USHORT wTotalLength;
  366. UCHAR bNumInterfaces;
  367. UCHAR bConfigurationValue;
  368. UCHAR iConfiguration;
  369. UCHAR bmAttributes;
  370. UCHAR MaxPower;
  371. } USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
  372. typedef struct USB_CONFIGURATION_DESCRIPTOR_SHORT {
  373. struct {
  374. ULONG ConnectionIndex;
  375. struct {
  376. UCHAR bmRequest;
  377. UCHAR bRequest;
  378. USHORT wValue;
  379. USHORT wIndex;
  380. USHORT wLength;
  381. } SetupPacket;
  382. } req;
  383. USB_CONFIGURATION_DESCRIPTOR data;
  384. } USB_CONFIGURATION_DESCRIPTOR_SHORT;
  385. typedef struct USB_ENDPOINT_DESCRIPTOR {
  386. UCHAR bLength;
  387. UCHAR bDescriptorType;
  388. UCHAR bEndpointAddress;
  389. UCHAR bmAttributes;
  390. USHORT wMaxPacketSize;
  391. UCHAR bInterval;
  392. } USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
  393. typedef struct USB_DESCRIPTOR_REQUEST {
  394. ULONG ConnectionIndex;
  395. struct {
  396. UCHAR bmRequest;
  397. UCHAR bRequest;
  398. USHORT wValue;
  399. USHORT wIndex;
  400. USHORT wLength;
  401. } SetupPacket;
  402. // UCHAR Data[0];
  403. } USB_DESCRIPTOR_REQUEST, *PUSB_DESCRIPTOR_REQUEST;
  404. typedef struct USB_HUB_DESCRIPTOR {
  405. UCHAR bDescriptorLength;
  406. UCHAR bDescriptorType;
  407. UCHAR bNumberOfPorts;
  408. USHORT wHubCharacteristics;
  409. UCHAR bPowerOnToPowerGood;
  410. UCHAR bHubControlCurrent;
  411. UCHAR bRemoveAndPowerMask[64];
  412. } USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
  413. typedef struct USB_ROOT_HUB_NAME {
  414. ULONG ActualLength;
  415. WCHAR RootHubName[1];
  416. } USB_ROOT_HUB_NAME, *PUSB_ROOT_HUB_NAME;
  417. typedef struct USB_ROOT_HUB_NAME_FIXED {
  418. ULONG ActualLength;
  419. WCHAR RootHubName[MAX_PATH_LENGTH];
  420. } USB_ROOT_HUB_NAME_FIXED;
  421. typedef struct USB_NODE_CONNECTION_NAME {
  422. ULONG ConnectionIndex;
  423. ULONG ActualLength;
  424. WCHAR NodeName[1];
  425. } USB_NODE_CONNECTION_NAME, *PUSB_NODE_CONNECTION_NAME;
  426. typedef struct USB_NODE_CONNECTION_NAME_FIXED {
  427. ULONG ConnectionIndex;
  428. ULONG ActualLength;
  429. WCHAR NodeName[MAX_PATH_LENGTH];
  430. } USB_NODE_CONNECTION_NAME_FIXED;
  431. typedef struct USB_HUB_NAME_FIXED {
  432. union {
  433. USB_ROOT_HUB_NAME_FIXED root;
  434. USB_NODE_CONNECTION_NAME_FIXED node;
  435. } u;
  436. } USB_HUB_NAME_FIXED;
  437. typedef struct USB_HUB_INFORMATION {
  438. USB_HUB_DESCRIPTOR HubDescriptor;
  439. BOOLEAN HubIsBusPowered;
  440. } USB_HUB_INFORMATION, *PUSB_HUB_INFORMATION;
  441. typedef struct USB_MI_PARENT_INFORMATION {
  442. ULONG NumberOfInterfaces;
  443. } USB_MI_PARENT_INFORMATION, *PUSB_MI_PARENT_INFORMATION;
  444. typedef struct USB_NODE_INFORMATION {
  445. USB_HUB_NODE NodeType;
  446. union {
  447. USB_HUB_INFORMATION HubInformation;
  448. USB_MI_PARENT_INFORMATION MiParentInformation;
  449. } u;
  450. } USB_NODE_INFORMATION, *PUSB_NODE_INFORMATION;
  451. typedef struct USB_PIPE_INFO {
  452. USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
  453. ULONG ScheduleOffset;
  454. } USB_PIPE_INFO, *PUSB_PIPE_INFO;
  455. typedef struct USB_NODE_CONNECTION_INFORMATION_EX {
  456. ULONG ConnectionIndex;
  457. USB_DEVICE_DESCRIPTOR DeviceDescriptor;
  458. UCHAR CurrentConfigurationValue;
  459. UCHAR Speed;
  460. BOOLEAN DeviceIsHub;
  461. USHORT DeviceAddress;
  462. ULONG NumberOfOpenPipes;
  463. USB_CONNECTION_STATUS ConnectionStatus;
  464. // USB_PIPE_INFO PipeList[0];
  465. } USB_NODE_CONNECTION_INFORMATION_EX, *PUSB_NODE_CONNECTION_INFORMATION_EX;
  466. typedef struct USB_HUB_CAP_FLAGS {
  467. ULONG HubIsHighSpeedCapable:1;
  468. ULONG HubIsHighSpeed:1;
  469. ULONG HubIsMultiTtCapable:1;
  470. ULONG HubIsMultiTt:1;
  471. ULONG HubIsRoot:1;
  472. ULONG HubIsArmedWakeOnConnect:1;
  473. ULONG ReservedMBZ:26;
  474. } USB_HUB_CAP_FLAGS, *PUSB_HUB_CAP_FLAGS;
  475. typedef struct USB_HUB_CAPABILITIES {
  476. ULONG HubIs2xCapable : 1;
  477. } USB_HUB_CAPABILITIES, *PUSB_HUB_CAPABILITIES;
  478. typedef struct USB_HUB_CAPABILITIES_EX {
  479. USB_HUB_CAP_FLAGS CapabilityFlags;
  480. } USB_HUB_CAPABILITIES_EX, *PUSB_HUB_CAPABILITIES_EX;
  481. #pragma pack(pop)
  482. /* winusb.dll interface */
  483. #define SHORT_PACKET_TERMINATE 0x01
  484. #define AUTO_CLEAR_STALL 0x02
  485. #define PIPE_TRANSFER_TIMEOUT 0x03
  486. #define IGNORE_SHORT_PACKETS 0x04
  487. #define ALLOW_PARTIAL_READS 0x05
  488. #define AUTO_FLUSH 0x06
  489. #define RAW_IO 0x07
  490. #define MAXIMUM_TRANSFER_SIZE 0x08
  491. #define AUTO_SUSPEND 0x81
  492. #define SUSPEND_DELAY 0x83
  493. #define DEVICE_SPEED 0x01
  494. #define LowSpeed 0x01
  495. #define FullSpeed 0x02
  496. #define HighSpeed 0x03
  497. typedef enum USBD_PIPE_TYPE {
  498. UsbdPipeTypeControl,
  499. UsbdPipeTypeIsochronous,
  500. UsbdPipeTypeBulk,
  501. UsbdPipeTypeInterrupt
  502. } USBD_PIPE_TYPE;
  503. typedef struct {
  504. USBD_PIPE_TYPE PipeType;
  505. UCHAR PipeId;
  506. USHORT MaximumPacketSize;
  507. UCHAR Interval;
  508. } WINUSB_PIPE_INFORMATION, *PWINUSB_PIPE_INFORMATION;
  509. #pragma pack(1)
  510. typedef struct {
  511. UCHAR request_type;
  512. UCHAR request;
  513. USHORT value;
  514. USHORT index;
  515. USHORT length;
  516. } WINUSB_SETUP_PACKET, *PWINUSB_SETUP_PACKET;
  517. #pragma pack()
  518. typedef void *WINUSB_INTERFACE_HANDLE, *PWINUSB_INTERFACE_HANDLE;
  519. DLL_DECLARE(WINAPI, BOOL, WinUsb_Initialize, (HANDLE, PWINUSB_INTERFACE_HANDLE));
  520. DLL_DECLARE(WINAPI, BOOL, WinUsb_Free, (WINUSB_INTERFACE_HANDLE));
  521. DLL_DECLARE(WINAPI, BOOL, WinUsb_GetAssociatedInterface, (WINUSB_INTERFACE_HANDLE, UCHAR, PWINUSB_INTERFACE_HANDLE));
  522. DLL_DECLARE(WINAPI, BOOL, WinUsb_GetDescriptor, (WINUSB_INTERFACE_HANDLE, UCHAR, UCHAR, USHORT, PUCHAR, ULONG, PULONG));
  523. DLL_DECLARE(WINAPI, BOOL, WinUsb_QueryInterfaceSettings, (WINUSB_INTERFACE_HANDLE, UCHAR, PUSB_INTERFACE_DESCRIPTOR));
  524. DLL_DECLARE(WINAPI, BOOL, WinUsb_QueryDeviceInformation, (WINUSB_INTERFACE_HANDLE, ULONG, PULONG, PVOID));
  525. DLL_DECLARE(WINAPI, BOOL, WinUsb_SetCurrentAlternateSetting, (WINUSB_INTERFACE_HANDLE, UCHAR));
  526. DLL_DECLARE(WINAPI, BOOL, WinUsb_GetCurrentAlternateSetting, (WINUSB_INTERFACE_HANDLE, PUCHAR));
  527. DLL_DECLARE(WINAPI, BOOL, WinUsb_QueryPipe, (WINUSB_INTERFACE_HANDLE, UCHAR, UCHAR, PWINUSB_PIPE_INFORMATION));
  528. DLL_DECLARE(WINAPI, BOOL, WinUsb_SetPipePolicy, (WINUSB_INTERFACE_HANDLE, UCHAR, ULONG, ULONG, PVOID));
  529. DLL_DECLARE(WINAPI, BOOL, WinUsb_GetPipePolicy, (WINUSB_INTERFACE_HANDLE, UCHAR, ULONG, PULONG, PVOID));
  530. DLL_DECLARE(WINAPI, BOOL, WinUsb_ReadPipe, (WINUSB_INTERFACE_HANDLE, UCHAR, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
  531. DLL_DECLARE(WINAPI, BOOL, WinUsb_WritePipe, (WINUSB_INTERFACE_HANDLE, UCHAR, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
  532. DLL_DECLARE(WINAPI, BOOL, WinUsb_ControlTransfer, (WINUSB_INTERFACE_HANDLE, WINUSB_SETUP_PACKET, PUCHAR, ULONG, PULONG, LPOVERLAPPED));
  533. DLL_DECLARE(WINAPI, BOOL, WinUsb_ResetPipe, (WINUSB_INTERFACE_HANDLE, UCHAR));
  534. DLL_DECLARE(WINAPI, BOOL, WinUsb_AbortPipe, (WINUSB_INTERFACE_HANDLE, UCHAR));
  535. DLL_DECLARE(WINAPI, BOOL, WinUsb_FlushPipe, (WINUSB_INTERFACE_HANDLE, UCHAR));