| 1 |
/* |
| 2 |
* windows backend for libusb 1.0 |
| 3 |
* Copyright © 2009-2012 Pete Batard <[email protected]> |
| 4 |
* Copyright © 2016-2018 Chris Dickens <[email protected]> |
| 5 |
* With contributions from Michael Plante, Orin Eman et al. |
| 6 |
* Parts of this code adapted from libusb-win32-v1 by Stephan Meyer |
| 7 |
* HID Reports IOCTLs inspired from HIDAPI by Alan Ott, Signal 11 Software |
| 8 |
* Hash table functions adapted from glibc, by Ulrich Drepper et al. |
| 9 |
* Major code testing contribution by Xiaofan Chen |
| 10 |
* |
| 11 |
* This library is free software; you can redistribute it and/or |
| 12 |
* modify it under the terms of the GNU Lesser General Public |
| 13 |
* License as published by the Free Software Foundation; either |
| 14 |
* version 2.1 of the License, or (at your option) any later version. |
| 15 |
* |
| 16 |
* This library is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 |
* Lesser General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU Lesser General Public |
| 22 |
* License along with this library; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <config.h> |
| 27 |
|
| 28 |
#include <windows.h> |
| 29 |
#include <setupapi.h> |
| 30 |
#include <ctype.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
#include "libusbi.h" |
| 35 |
#include "windows_winusb.h" |
| 36 |
|
| 37 |
#define HANDLE_VALID(h) (((h) != NULL) && ((h) != INVALID_HANDLE_VALUE)) |
| 38 |
|
| 39 |
// The below macro is used in conjunction with safe loops. |
| 40 |
#define LOOP_BREAK(err) \ |
| 41 |
{ \ |
| 42 |
r = err; \ |
| 43 |
continue; \ |
| 44 |
} |
| 45 |
|
| 46 |
static int interface_by_endpoint(struct winusb_device_priv *priv, |
| 47 |
struct winusb_device_handle_priv *handle_priv, uint8_t endpoint_address); |
| 48 |
// WinUSB-like API prototypes |
| 49 |
static bool winusbx_init(struct libusb_context *ctx); |
| 50 |
static void winusbx_exit(void); |
| 51 |
static int winusbx_open(int sub_api, struct libusb_device_handle *dev_handle); |
| 52 |
static void winusbx_close(int sub_api, struct libusb_device_handle *dev_handle); |
| 53 |
static int winusbx_configure_endpoints(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 54 |
static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 55 |
static int winusbx_release_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 56 |
static int winusbx_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 57 |
static int winusbx_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting); |
| 58 |
static int winusbx_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 59 |
static int winusbx_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 60 |
static int winusbx_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint); |
| 61 |
static int winusbx_cancel_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 62 |
static int winusbx_reset_device(int sub_api, struct libusb_device_handle *dev_handle); |
| 63 |
static enum libusb_transfer_status winusbx_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, DWORD length); |
| 64 |
static int winusbx_endpoint_supports_raw_io(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint); |
| 65 |
static int winusbx_endpoint_set_raw_io(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint, int enable); |
| 66 |
static int winusbx_get_max_raw_io_transfer_size(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint); |
| 67 |
// HID API prototypes |
| 68 |
static bool hid_init(struct libusb_context *ctx); |
| 69 |
static void hid_exit(void); |
| 70 |
static int hid_open(int sub_api, struct libusb_device_handle *dev_handle); |
| 71 |
static void hid_close(int sub_api, struct libusb_device_handle *dev_handle); |
| 72 |
static int hid_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 73 |
static int hid_release_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 74 |
static int hid_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting); |
| 75 |
static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 76 |
static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 77 |
static int hid_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint); |
| 78 |
static int hid_reset_device(int sub_api, struct libusb_device_handle *dev_handle); |
| 79 |
static enum libusb_transfer_status hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, DWORD length); |
| 80 |
// Composite API prototypes |
| 81 |
static int composite_open(int sub_api, struct libusb_device_handle *dev_handle); |
| 82 |
static void composite_close(int sub_api, struct libusb_device_handle *dev_handle); |
| 83 |
static int composite_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 84 |
static int composite_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting); |
| 85 |
static int composite_release_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface); |
| 86 |
static int composite_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 87 |
static int composite_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 88 |
static int composite_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 89 |
static int composite_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint); |
| 90 |
static int composite_cancel_transfer(int sub_api, struct usbi_transfer *itransfer); |
| 91 |
static int composite_reset_device(int sub_api, struct libusb_device_handle *dev_handle); |
| 92 |
static enum libusb_transfer_status composite_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, DWORD length); |
| 93 |
static int composite_endpoint_supports_raw_io(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint); |
| 94 |
static int composite_endpoint_set_raw_io(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint, int enable); |
| 95 |
static int composite_get_max_raw_io_transfer_size(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint); |
| 96 |
|
| 97 |
static usbi_mutex_t autoclaim_lock; |
| 98 |
|
| 99 |
// API globals |
| 100 |
static struct winusb_interface WinUSBX[SUB_API_MAX]; |
| 101 |
#define CHECK_WINUSBX_AVAILABLE(sub_api) \ |
| 102 |
do { \ |
| 103 |
if (sub_api == SUB_API_NOTSET) \ |
| 104 |
sub_api = priv->sub_api; \ |
| 105 |
if (WinUSBX[sub_api].hDll == NULL) \ |
| 106 |
return LIBUSB_ERROR_ACCESS; \ |
| 107 |
} while (0) |
| 108 |
|
| 109 |
#define CHECK_HID_AVAILABLE \ |
| 110 |
do { \ |
| 111 |
if (DLL_HANDLE_NAME(hid) == NULL) \ |
| 112 |
return LIBUSB_ERROR_ACCESS; \ |
| 113 |
} while (0) |
| 114 |
|
| 115 |
#if defined(ENABLE_LOGGING) |
| 116 |
static const char *guid_to_string(const GUID *guid, char guid_string[MAX_GUID_STRING_LENGTH]) |
| 117 |
{ |
| 118 |
if (guid == NULL) { |
| 119 |
guid_string[0] = '\0'; |
| 120 |
return guid_string; |
| 121 |
} |
| 122 |
|
| 123 |
sprintf(guid_string, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", |
| 124 |
(unsigned int)guid->Data1, guid->Data2, guid->Data3, |
| 125 |
guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3], |
| 126 |
guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]); |
| 127 |
|
| 128 |
return guid_string; |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
static bool string_to_guid(const char guid_string[MAX_GUID_STRING_LENGTH], GUID *guid) |
| 133 |
{ |
| 134 |
unsigned short tmp[4]; |
| 135 |
int num_chars = -1; |
| 136 |
char extra; |
| 137 |
int r; |
| 138 |
|
| 139 |
// Unfortunately MinGW complains that '%hhx' is not a valid format specifier, |
| 140 |
// even though Visual Studio 2013 and later support it. Rather than complicating |
| 141 |
// the logic in this function with '#ifdef's, use a temporary array on the stack |
| 142 |
// to store the conversions. |
| 143 |
r = sscanf(guid_string, "{%8x-%4hx-%4hx-%4hx-%4hx%4hx%4hx}%n%c", |
| 144 |
(unsigned int *)&guid->Data1, &guid->Data2, &guid->Data3, |
| 145 |
&tmp[0], &tmp[1], &tmp[2], &tmp[3], &num_chars, &extra); |
| 146 |
|
| 147 |
if ((r != 7) || (num_chars != 38)) |
| 148 |
return false; |
| 149 |
|
| 150 |
// Extract the bytes from the 2-byte shorts |
| 151 |
guid->Data4[0] = (unsigned char)((tmp[0] >> 8) & 0xFF); |
| 152 |
guid->Data4[1] = (unsigned char)(tmp[0] & 0xFF); |
| 153 |
guid->Data4[2] = (unsigned char)((tmp[1] >> 8) & 0xFF); |
| 154 |
guid->Data4[3] = (unsigned char)(tmp[1] & 0xFF); |
| 155 |
guid->Data4[4] = (unsigned char)((tmp[2] >> 8) & 0xFF); |
| 156 |
guid->Data4[5] = (unsigned char)(tmp[2] & 0xFF); |
| 157 |
guid->Data4[6] = (unsigned char)((tmp[3] >> 8) & 0xFF); |
| 158 |
guid->Data4[7] = (unsigned char)(tmp[3] & 0xFF); |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Normalize Microsoft's paths: return a duplicate of the given path |
| 165 |
* with all characters converted to uppercase |
| 166 |
*/ |
| 167 |
static char *normalize_path(const char *path) |
| 168 |
{ |
| 169 |
char *ret_path = _strdup(path); |
| 170 |
char *p; |
| 171 |
|
| 172 |
if (ret_path == NULL) |
| 173 |
return NULL; |
| 174 |
|
| 175 |
for (p = ret_path; *p != '\0'; p++) |
| 176 |
*p = (char)toupper((unsigned char)*p); |
| 177 |
|
| 178 |
return ret_path; |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Cfgmgr32, AdvAPI32, OLE32 and SetupAPI DLL functions |
| 183 |
*/ |
| 184 |
static bool init_dlls(struct libusb_context *ctx) |
| 185 |
{ |
| 186 |
DLL_GET_HANDLE(ctx, Cfgmgr32); |
| 187 |
DLL_LOAD_FUNC(Cfgmgr32, CM_Get_Parent, true); |
| 188 |
DLL_LOAD_FUNC(Cfgmgr32, CM_Get_Child, true); |
| 189 |
|
| 190 |
// Prefixed to avoid conflict with header files |
| 191 |
DLL_GET_HANDLE(ctx, AdvAPI32); |
| 192 |
DLL_LOAD_FUNC_PREFIXED(AdvAPI32, p, RegQueryValueExA, true); |
| 193 |
DLL_LOAD_FUNC_PREFIXED(AdvAPI32, p, RegCloseKey, true); |
| 194 |
|
| 195 |
DLL_GET_HANDLE(ctx, SetupAPI); |
| 196 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiGetClassDevsA, true); |
| 197 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiEnumDeviceInfo, true); |
| 198 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiEnumDeviceInterfaces, true); |
| 199 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiGetDeviceInstanceIdA, true); |
| 200 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiGetDeviceInterfaceDetailA, true); |
| 201 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiGetDeviceRegistryPropertyA, true); |
| 202 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiDestroyDeviceInfoList, true); |
| 203 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiOpenDevRegKey, true); |
| 204 |
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiOpenDeviceInterfaceRegKey, true); |
| 205 |
|
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
static void exit_dlls(void) |
| 210 |
{ |
| 211 |
DLL_FREE_HANDLE(SetupAPI); |
| 212 |
DLL_FREE_HANDLE(AdvAPI32); |
| 213 |
DLL_FREE_HANDLE(Cfgmgr32); |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* enumerate interfaces for the whole USB class |
| 218 |
* |
| 219 |
* Parameters: |
| 220 |
* dev_info: a pointer to a dev_info list |
| 221 |
* dev_info_data: a pointer to an SP_DEVINFO_DATA to be filled (or NULL if not needed) |
| 222 |
* enumerator: the generic USB class for which to retrieve interface details |
| 223 |
* index: zero based index of the interface in the device info list |
| 224 |
* |
| 225 |
* Note: it is the responsibility of the caller to free the DEVICE_INTERFACE_DETAIL_DATA |
| 226 |
* structure returned and call this function repeatedly using the same guid (with an |
| 227 |
* incremented index starting at zero) until all interfaces have been returned. |
| 228 |
*/ |
| 229 |
static bool get_devinfo_data(struct libusb_context *ctx, |
| 230 |
HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const char *enumerator, unsigned _index) |
| 231 |
{ |
| 232 |
if (_index == 0) { |
| 233 |
*dev_info = pSetupDiGetClassDevsA(NULL, enumerator, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); |
| 234 |
if (*dev_info == INVALID_HANDLE_VALUE) { |
| 235 |
usbi_err(ctx, "could not obtain device info set for PnP enumerator '%s': %s", |
| 236 |
enumerator, windows_error_str(0)); |
| 237 |
return false; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA); |
| 242 |
if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { |
| 243 |
if (GetLastError() != ERROR_NO_MORE_ITEMS) |
| 244 |
usbi_err(ctx, "could not obtain device info data for PnP enumerator '%s' index %u: %s", |
| 245 |
enumerator, _index, windows_error_str(0)); |
| 246 |
|
| 247 |
pSetupDiDestroyDeviceInfoList(*dev_info); |
| 248 |
*dev_info = INVALID_HANDLE_VALUE; |
| 249 |
return false; |
| 250 |
} |
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
* enumerate interfaces for a specific GUID |
| 256 |
* |
| 257 |
* Parameters: |
| 258 |
* dev_info: a pointer to a dev_info list |
| 259 |
* dev_info_data: a pointer to an SP_DEVINFO_DATA to be filled (or NULL if not needed) |
| 260 |
* guid: the GUID for which to retrieve interface details |
| 261 |
* index: zero based index of the interface in the device info list |
| 262 |
* |
| 263 |
* Note: it is the responsibility of the caller to free the DEVICE_INTERFACE_DETAIL_DATA |
| 264 |
* structure returned and call this function repeatedly using the same guid (with an |
| 265 |
* incremented index starting at zero) until all interfaces have been returned. |
| 266 |
*/ |
| 267 |
static int get_interface_details(struct libusb_context *ctx, HDEVINFO dev_info, |
| 268 |
PSP_DEVINFO_DATA dev_info_data, LPCGUID guid, DWORD *_index, char **dev_interface_path) |
| 269 |
{ |
| 270 |
SP_DEVICE_INTERFACE_DATA dev_interface_data; |
| 271 |
PSP_DEVICE_INTERFACE_DETAIL_DATA_A dev_interface_details; |
| 272 |
char guid_string[MAX_GUID_STRING_LENGTH]; |
| 273 |
DWORD size; |
| 274 |
|
| 275 |
#ifndef ENABLE_LOGGING |
| 276 |
UNUSED(*guid_string); |
| 277 |
#endif |
| 278 |
dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA); |
| 279 |
dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); |
| 280 |
for (;;) { |
| 281 |
if (!pSetupDiEnumDeviceInfo(dev_info, *_index, dev_info_data)) { |
| 282 |
if (GetLastError() != ERROR_NO_MORE_ITEMS) { |
| 283 |
usbi_err(ctx, "Could not obtain device info data for %s index %lu: %s", |
| 284 |
guid_to_string(guid, guid_string), ULONG_CAST(*_index), windows_error_str(0)); |
| 285 |
return LIBUSB_ERROR_OTHER; |
| 286 |
} |
| 287 |
|
| 288 |
// No more devices |
| 289 |
return LIBUSB_SUCCESS; |
| 290 |
} |
| 291 |
|
| 292 |
// Always advance the index for the next iteration |
| 293 |
(*_index)++; |
| 294 |
|
| 295 |
if (pSetupDiEnumDeviceInterfaces(dev_info, dev_info_data, guid, 0, &dev_interface_data)) |
| 296 |
break; |
| 297 |
|
| 298 |
if (GetLastError() != ERROR_NO_MORE_ITEMS) { |
| 299 |
usbi_err(ctx, "Could not obtain interface data for %s devInst %lX: %s", |
| 300 |
guid_to_string(guid, guid_string), ULONG_CAST(dev_info_data->DevInst), windows_error_str(0)); |
| 301 |
return LIBUSB_ERROR_OTHER; |
| 302 |
} |
| 303 |
|
| 304 |
// Device does not have an interface matching this GUID, skip |
| 305 |
} |
| 306 |
|
| 307 |
// Read interface data (dummy + actual) to access the device path |
| 308 |
if (!pSetupDiGetDeviceInterfaceDetailA(dev_info, &dev_interface_data, NULL, 0, &size, NULL)) { |
| 309 |
// The dummy call should fail with ERROR_INSUFFICIENT_BUFFER |
| 310 |
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 311 |
usbi_err(ctx, "could not access interface data (dummy) for %s devInst %lX: %s", |
| 312 |
guid_to_string(guid, guid_string), ULONG_CAST(dev_info_data->DevInst), windows_error_str(0)); |
| 313 |
return LIBUSB_ERROR_OTHER; |
| 314 |
} |
| 315 |
} else { |
| 316 |
usbi_err(ctx, "program assertion failed - http://msdn.microsoft.com/en-us/library/ms792901.aspx is wrong"); |
| 317 |
return LIBUSB_ERROR_OTHER; |
| 318 |
} |
| 319 |
|
| 320 |
dev_interface_details = malloc(size); |
| 321 |
if (dev_interface_details == NULL) { |
| 322 |
usbi_err(ctx, "could not allocate interface data for %s devInst %lX", |
| 323 |
guid_to_string(guid, guid_string), ULONG_CAST(dev_info_data->DevInst)); |
| 324 |
return LIBUSB_ERROR_NO_MEM; |
| 325 |
} |
| 326 |
|
| 327 |
dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); |
| 328 |
if (!pSetupDiGetDeviceInterfaceDetailA(dev_info, &dev_interface_data, |
| 329 |
dev_interface_details, size, NULL, NULL)) { |
| 330 |
usbi_err(ctx, "could not access interface data (actual) for %s devInst %lX: %s", |
| 331 |
guid_to_string(guid, guid_string), ULONG_CAST(dev_info_data->DevInst), windows_error_str(0)); |
| 332 |
free(dev_interface_details); |
| 333 |
return LIBUSB_ERROR_OTHER; |
| 334 |
} |
| 335 |
|
| 336 |
*dev_interface_path = normalize_path(dev_interface_details->DevicePath); |
| 337 |
free(dev_interface_details); |
| 338 |
|
| 339 |
if (*dev_interface_path == NULL) { |
| 340 |
usbi_err(ctx, "could not allocate interface path for %s devInst %lX", |
| 341 |
guid_to_string(guid, guid_string), ULONG_CAST(dev_info_data->DevInst)); |
| 342 |
return LIBUSB_ERROR_NO_MEM; |
| 343 |
} |
| 344 |
|
| 345 |
return LIBUSB_SUCCESS; |
| 346 |
} |
| 347 |
|
| 348 |
/* For libusb0 filter */ |
| 349 |
static int get_interface_details_filter(struct libusb_context *ctx, HDEVINFO *dev_info, |
| 350 |
DWORD _index, char *filter_path, char **dev_interface_path) |
| 351 |
{ |
| 352 |
const GUID *libusb0_guid = &GUID_DEVINTERFACE_LIBUSB0_FILTER; |
| 353 |
SP_DEVICE_INTERFACE_DATA dev_interface_data; |
| 354 |
PSP_DEVICE_INTERFACE_DETAIL_DATA_A dev_interface_details; |
| 355 |
HKEY hkey_dev_interface; |
| 356 |
DWORD size; |
| 357 |
int err = LIBUSB_ERROR_OTHER; |
| 358 |
|
| 359 |
if (_index == 0) { |
| 360 |
*dev_info = pSetupDiGetClassDevsA(libusb0_guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); |
| 361 |
if (*dev_info == INVALID_HANDLE_VALUE) { |
| 362 |
usbi_err(ctx, "could not obtain device info set: %s", windows_error_str(0)); |
| 363 |
return LIBUSB_ERROR_OTHER; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); |
| 368 |
if (!pSetupDiEnumDeviceInterfaces(*dev_info, NULL, libusb0_guid, _index, &dev_interface_data)) { |
| 369 |
if (GetLastError() != ERROR_NO_MORE_ITEMS) { |
| 370 |
usbi_err(ctx, "Could not obtain interface data for index %lu: %s", |
| 371 |
ULONG_CAST(_index), windows_error_str(0)); |
| 372 |
goto err_exit; |
| 373 |
} |
| 374 |
|
| 375 |
pSetupDiDestroyDeviceInfoList(*dev_info); |
| 376 |
*dev_info = INVALID_HANDLE_VALUE; |
| 377 |
return LIBUSB_SUCCESS; |
| 378 |
} |
| 379 |
|
| 380 |
// Read interface data (dummy + actual) to access the device path |
| 381 |
if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) { |
| 382 |
// The dummy call should fail with ERROR_INSUFFICIENT_BUFFER |
| 383 |
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 384 |
usbi_err(ctx, "could not access interface data (dummy) for index %lu: %s", |
| 385 |
ULONG_CAST(_index), windows_error_str(0)); |
| 386 |
goto err_exit; |
| 387 |
} |
| 388 |
} else { |
| 389 |
usbi_err(ctx, "program assertion failed - http://msdn.microsoft.com/en-us/library/ms792901.aspx is wrong"); |
| 390 |
goto err_exit; |
| 391 |
} |
| 392 |
|
| 393 |
dev_interface_details = malloc(size); |
| 394 |
if (dev_interface_details == NULL) { |
| 395 |
usbi_err(ctx, "could not allocate interface data for index %lu", ULONG_CAST(_index)); |
| 396 |
err = LIBUSB_ERROR_NO_MEM; |
| 397 |
goto err_exit; |
| 398 |
} |
| 399 |
|
| 400 |
dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); |
| 401 |
if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, dev_interface_details, size, NULL, NULL)) { |
| 402 |
usbi_err(ctx, "could not access interface data (actual) for index %lu: %s", |
| 403 |
ULONG_CAST(_index), windows_error_str(0)); |
| 404 |
free(dev_interface_details); |
| 405 |
goto err_exit; |
| 406 |
} |
| 407 |
|
| 408 |
*dev_interface_path = normalize_path(dev_interface_details->DevicePath); |
| 409 |
free(dev_interface_details); |
| 410 |
|
| 411 |
if (*dev_interface_path == NULL) { |
| 412 |
usbi_err(ctx, "could not allocate interface path for index %lu", ULONG_CAST(_index)); |
| 413 |
err = LIBUSB_ERROR_NO_MEM; |
| 414 |
goto err_exit; |
| 415 |
} |
| 416 |
|
| 417 |
// [trobinso] lookup the libusb0 symbolic index. |
| 418 |
hkey_dev_interface = pSetupDiOpenDeviceInterfaceRegKey(*dev_info, &dev_interface_data, 0, KEY_READ); |
| 419 |
if (hkey_dev_interface != INVALID_HANDLE_VALUE) { |
| 420 |
DWORD libusb0_symboliclink_index = 0; |
| 421 |
DWORD value_length = sizeof(DWORD); |
| 422 |
LONG status; |
| 423 |
|
| 424 |
status = pRegQueryValueExA(hkey_dev_interface, "LUsb0", NULL, NULL, |
| 425 |
(LPBYTE)&libusb0_symboliclink_index, &value_length); |
| 426 |
if (status == ERROR_SUCCESS) { |
| 427 |
if (libusb0_symboliclink_index < 256) { |
| 428 |
// libusb0.sys is connected to this device instance. |
| 429 |
// If the the device interface guid is {F9F3FF14-AE21-48A0-8A25-8011A7A931D9} then it's a filter. |
| 430 |
sprintf(filter_path, "\\\\.\\libusb0-%04u", (unsigned int)libusb0_symboliclink_index); |
| 431 |
usbi_dbg(ctx, "assigned libusb0 symbolic link %s", filter_path); |
| 432 |
} else { |
| 433 |
// libusb0.sys was connected to this device instance at one time; but not anymore. |
| 434 |
} |
| 435 |
} |
| 436 |
pRegCloseKey(hkey_dev_interface); |
| 437 |
} else { |
| 438 |
usbi_warn(ctx, "could not open device interface registry key for index %lu: %s", |
| 439 |
ULONG_CAST(_index), windows_error_str(0)); |
| 440 |
// TODO: should this be an error? |
| 441 |
} |
| 442 |
|
| 443 |
return LIBUSB_SUCCESS; |
| 444 |
|
| 445 |
err_exit: |
| 446 |
pSetupDiDestroyDeviceInfoList(*dev_info); |
| 447 |
*dev_info = INVALID_HANDLE_VALUE; |
| 448 |
return err; |
| 449 |
} |
| 450 |
|
| 451 |
/* |
| 452 |
* Returns the first known ancestor of a device |
| 453 |
*/ |
| 454 |
static struct libusb_device *get_ancestor(struct libusb_context *ctx, |
| 455 |
DEVINST devinst, PDEVINST _parent_devinst) |
| 456 |
{ |
| 457 |
struct libusb_device *dev = NULL; |
| 458 |
DEVINST parent_devinst; |
| 459 |
|
| 460 |
while (dev == NULL) { |
| 461 |
if (CM_Get_Parent(&parent_devinst, devinst, 0) != CR_SUCCESS) |
| 462 |
break; |
| 463 |
devinst = parent_devinst; |
| 464 |
dev = usbi_get_device_by_session_id(ctx, (unsigned long)devinst); |
| 465 |
} |
| 466 |
|
| 467 |
if ((dev != NULL) && (_parent_devinst != NULL)) |
| 468 |
*_parent_devinst = devinst; |
| 469 |
|
| 470 |
return dev; |
| 471 |
} |
| 472 |
|
| 473 |
/* |
| 474 |
* Determine which interface the given endpoint address belongs to |
| 475 |
*/ |
| 476 |
static int get_interface_by_endpoint(struct libusb_config_descriptor *conf_desc, uint8_t ep) |
| 477 |
{ |
| 478 |
const struct libusb_interface *intf; |
| 479 |
const struct libusb_interface_descriptor *intf_desc; |
| 480 |
uint8_t i, k; |
| 481 |
int j; |
| 482 |
|
| 483 |
for (i = 0; i < conf_desc->bNumInterfaces; i++) { |
| 484 |
intf = &conf_desc->interface[i]; |
| 485 |
for (j = 0; j < intf->num_altsetting; j++) { |
| 486 |
intf_desc = &intf->altsetting[j]; |
| 487 |
for (k = 0; k < intf_desc->bNumEndpoints; k++) { |
| 488 |
if (intf_desc->endpoint[k].bEndpointAddress == ep) { |
| 489 |
usbi_dbg(NULL, "found endpoint %02X on interface %d", intf_desc->bInterfaceNumber, i); |
| 490 |
return intf_desc->bInterfaceNumber; |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
usbi_dbg(NULL, "endpoint %02X not found on any interface", ep); |
| 497 |
return LIBUSB_ERROR_NOT_FOUND; |
| 498 |
} |
| 499 |
|
| 500 |
static const struct libusb_interface_descriptor *get_interface_descriptor_by_number(struct libusb_device_handle *dev_handle, struct libusb_config_descriptor *conf_desc, int iface, uint8_t altsetting) |
| 501 |
{ |
| 502 |
int i; |
| 503 |
|
| 504 |
for (i = 0; i < conf_desc->bNumInterfaces; i++) { |
| 505 |
if (altsetting < conf_desc->interface[i].num_altsetting && conf_desc->interface[i].altsetting[altsetting].bInterfaceNumber == iface) { |
| 506 |
return &conf_desc->interface[i].altsetting[altsetting]; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
usbi_err(HANDLE_CTX(dev_handle), "interface %d with altsetting %d not found for device", iface, (int)altsetting); |
| 511 |
return NULL; |
| 512 |
} |
| 513 |
|
| 514 |
/* |
| 515 |
* Open a device and associate the HANDLE with the context's I/O completion port |
| 516 |
*/ |
| 517 |
static HANDLE windows_open(struct libusb_device_handle *dev_handle, const char *path, DWORD access) |
| 518 |
{ |
| 519 |
struct libusb_context *ctx = HANDLE_CTX(dev_handle); |
| 520 |
struct windows_context_priv *priv = usbi_get_context_priv(ctx); |
| 521 |
HANDLE handle; |
| 522 |
|
| 523 |
handle = CreateFileA(path, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); |
| 524 |
if (handle == INVALID_HANDLE_VALUE) |
| 525 |
return handle; |
| 526 |
|
| 527 |
if (CreateIoCompletionPort(handle, priv->completion_port, (ULONG_PTR)dev_handle, 0) == NULL) { |
| 528 |
usbi_err(ctx, "failed to associate handle to I/O completion port: %s", windows_error_str(0)); |
| 529 |
CloseHandle(handle); |
| 530 |
return INVALID_HANDLE_VALUE; |
| 531 |
} |
| 532 |
|
| 533 |
return handle; |
| 534 |
} |
| 535 |
|
| 536 |
/* |
| 537 |
* Populate the endpoints addresses of the device_priv interface helper structs |
| 538 |
*/ |
| 539 |
static int windows_assign_endpoints(struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting) |
| 540 |
{ |
| 541 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 542 |
struct libusb_config_descriptor *conf_desc; |
| 543 |
const struct libusb_interface_descriptor *if_desc; |
| 544 |
int i, r; |
| 545 |
|
| 546 |
r = libusb_get_active_config_descriptor(dev_handle->dev, &conf_desc); |
| 547 |
if (r != LIBUSB_SUCCESS) { |
| 548 |
usbi_warn(HANDLE_CTX(dev_handle), "could not read config descriptor: error %d", r); |
| 549 |
return r; |
| 550 |
} |
| 551 |
|
| 552 |
if_desc = get_interface_descriptor_by_number(dev_handle, conf_desc, iface, altsetting); |
| 553 |
if (if_desc == NULL) { |
| 554 |
r = LIBUSB_ERROR_NOT_FOUND; |
| 555 |
goto end; |
| 556 |
} |
| 557 |
|
| 558 |
safe_free(priv->usb_interface[iface].endpoint); |
| 559 |
|
| 560 |
if (if_desc->bNumEndpoints == 0) { |
| 561 |
usbi_dbg(HANDLE_CTX(dev_handle), "no endpoints found for interface %u", iface); |
| 562 |
} else { |
| 563 |
priv->usb_interface[iface].endpoint = malloc(if_desc->bNumEndpoints); |
| 564 |
if (priv->usb_interface[iface].endpoint == NULL) { |
| 565 |
r = LIBUSB_ERROR_NO_MEM; |
| 566 |
goto end; |
| 567 |
} |
| 568 |
priv->usb_interface[iface].nb_endpoints = if_desc->bNumEndpoints; |
| 569 |
for (i = 0; i < if_desc->bNumEndpoints; i++) { |
| 570 |
priv->usb_interface[iface].endpoint[i] = if_desc->endpoint[i].bEndpointAddress; |
| 571 |
usbi_dbg(HANDLE_CTX(dev_handle), "(re)assigned endpoint %02X to interface %u", priv->usb_interface[iface].endpoint[i], iface); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
// Extra init may be required to configure endpoints |
| 576 |
if (priv->apib->configure_endpoints) |
| 577 |
r = priv->apib->configure_endpoints(SUB_API_NOTSET, dev_handle, iface); |
| 578 |
|
| 579 |
if (r == LIBUSB_SUCCESS) |
| 580 |
priv->usb_interface[iface].current_altsetting = altsetting; |
| 581 |
|
| 582 |
end: |
| 583 |
libusb_free_config_descriptor(conf_desc); |
| 584 |
return r; |
| 585 |
} |
| 586 |
|
| 587 |
// Lookup for a match in the list of API driver names |
| 588 |
// return -1 if not found, driver match number otherwise |
| 589 |
static int get_sub_api(char *driver, int api) |
| 590 |
{ |
| 591 |
const char sep_str[2] = {LIST_SEPARATOR, 0}; |
| 592 |
char *tok, *tmp_str; |
| 593 |
size_t len = strlen(driver); |
| 594 |
int i; |
| 595 |
|
| 596 |
if (len == 0) |
| 597 |
return SUB_API_NOTSET; |
| 598 |
|
| 599 |
tmp_str = _strdup(driver); |
| 600 |
if (tmp_str == NULL) |
| 601 |
return SUB_API_NOTSET; |
| 602 |
|
| 603 |
tok = strtok(tmp_str, sep_str); |
| 604 |
while (tok != NULL) { |
| 605 |
for (i = 0; i < usb_api_backend[api].nb_driver_names; i++) { |
| 606 |
if (_stricmp(tok, usb_api_backend[api].driver_name_list[i]) == 0) { |
| 607 |
free(tmp_str); |
| 608 |
return i; |
| 609 |
} |
| 610 |
} |
| 611 |
tok = strtok(NULL, sep_str); |
| 612 |
} |
| 613 |
|
| 614 |
free(tmp_str); |
| 615 |
return SUB_API_NOTSET; |
| 616 |
} |
| 617 |
|
| 618 |
/* |
| 619 |
* auto-claiming and auto-release helper functions |
| 620 |
*/ |
| 621 |
static int auto_claim(struct libusb_transfer *transfer, int *interface_number, int api_type) |
| 622 |
{ |
| 623 |
struct winusb_device_handle_priv *handle_priv = |
| 624 |
get_winusb_device_handle_priv(transfer->dev_handle); |
| 625 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 626 |
int current_interface = *interface_number; |
| 627 |
int r = LIBUSB_SUCCESS; |
| 628 |
|
| 629 |
switch (api_type) { |
| 630 |
case USB_API_WINUSBX: |
| 631 |
case USB_API_HID: |
| 632 |
break; |
| 633 |
default: |
| 634 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 635 |
} |
| 636 |
|
| 637 |
usbi_mutex_lock(&autoclaim_lock); |
| 638 |
if (current_interface < 0) { // No serviceable interface was found |
| 639 |
for (current_interface = 0; current_interface < USB_MAXINTERFACES; current_interface++) { |
| 640 |
// Must claim an interface of the same API type |
| 641 |
if ((priv->usb_interface[current_interface].apib->id == api_type) |
| 642 |
&& (libusb_claim_interface(transfer->dev_handle, current_interface) == LIBUSB_SUCCESS)) { |
| 643 |
usbi_dbg(TRANSFER_CTX(transfer), "auto-claimed interface %d for control request", current_interface); |
| 644 |
if (handle_priv->autoclaim_count[current_interface] != 0) |
| 645 |
usbi_err(TRANSFER_CTX(transfer), "program assertion failed - autoclaim_count was nonzero"); |
| 646 |
handle_priv->autoclaim_count[current_interface]++; |
| 647 |
break; |
| 648 |
} |
| 649 |
} |
| 650 |
if (current_interface == USB_MAXINTERFACES) { |
| 651 |
usbi_err(TRANSFER_CTX(transfer), "could not auto-claim any interface"); |
| 652 |
r = LIBUSB_ERROR_NOT_FOUND; |
| 653 |
} |
| 654 |
} else { |
| 655 |
// If we have a valid interface that was autoclaimed, we must increment |
| 656 |
// its autoclaim count so that we can prevent an early release. |
| 657 |
if (handle_priv->autoclaim_count[current_interface] != 0) |
| 658 |
handle_priv->autoclaim_count[current_interface]++; |
| 659 |
} |
| 660 |
usbi_mutex_unlock(&autoclaim_lock); |
| 661 |
|
| 662 |
*interface_number = current_interface; |
| 663 |
return r; |
| 664 |
} |
| 665 |
|
| 666 |
static void auto_release(struct usbi_transfer *itransfer) |
| 667 |
{ |
| 668 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 669 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 670 |
libusb_device_handle *dev_handle = transfer->dev_handle; |
| 671 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 672 |
int r; |
| 673 |
|
| 674 |
usbi_mutex_lock(&autoclaim_lock); |
| 675 |
if (handle_priv->autoclaim_count[transfer_priv->interface_number] > 0) { |
| 676 |
handle_priv->autoclaim_count[transfer_priv->interface_number]--; |
| 677 |
if (handle_priv->autoclaim_count[transfer_priv->interface_number] == 0) { |
| 678 |
r = libusb_release_interface(dev_handle, transfer_priv->interface_number); |
| 679 |
if (r == LIBUSB_SUCCESS) |
| 680 |
usbi_dbg(ITRANSFER_CTX(itransfer), "auto-released interface %d", transfer_priv->interface_number); |
| 681 |
else |
| 682 |
usbi_dbg(ITRANSFER_CTX(itransfer), "failed to auto-release interface %d (%s)", |
| 683 |
transfer_priv->interface_number, libusb_error_name((enum libusb_error)r)); |
| 684 |
} |
| 685 |
} |
| 686 |
usbi_mutex_unlock(&autoclaim_lock); |
| 687 |
} |
| 688 |
|
| 689 |
/* |
| 690 |
* init: libusb backend init function |
| 691 |
*/ |
| 692 |
static int winusb_init(struct libusb_context *ctx) |
| 693 |
{ |
| 694 |
int i; |
| 695 |
|
| 696 |
// Load DLL imports |
| 697 |
if (!init_dlls(ctx)) { |
| 698 |
usbi_err(ctx, "could not resolve DLL functions"); |
| 699 |
return LIBUSB_ERROR_OTHER; |
| 700 |
} |
| 701 |
|
| 702 |
// Initialize the low level APIs (we don't care about errors at this stage) |
| 703 |
for (i = 0; i < USB_API_MAX; i++) { |
| 704 |
if (usb_api_backend[i].init && !usb_api_backend[i].init(ctx)) |
| 705 |
usbi_warn(ctx, "error initializing %s backend", |
| 706 |
usb_api_backend[i].designation); |
| 707 |
} |
| 708 |
|
| 709 |
// We need a lock for proper auto-release |
| 710 |
usbi_mutex_init(&autoclaim_lock); |
| 711 |
|
| 712 |
return LIBUSB_SUCCESS; |
| 713 |
} |
| 714 |
|
| 715 |
/* |
| 716 |
* exit: libusb backend deinitialization function |
| 717 |
*/ |
| 718 |
static void winusb_exit(struct libusb_context *ctx) |
| 719 |
{ |
| 720 |
int i; |
| 721 |
|
| 722 |
UNUSED(ctx); |
| 723 |
|
| 724 |
usbi_mutex_destroy(&autoclaim_lock); |
| 725 |
|
| 726 |
for (i = 0; i < USB_API_MAX; i++) { |
| 727 |
if (usb_api_backend[i].exit) |
| 728 |
usb_api_backend[i].exit(); |
| 729 |
} |
| 730 |
|
| 731 |
exit_dlls(); |
| 732 |
} |
| 733 |
|
| 734 |
/* |
| 735 |
* fetch and cache all the config descriptors through I/O |
| 736 |
*/ |
| 737 |
static void cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle) |
| 738 |
{ |
| 739 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 740 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 741 |
DWORD size, ret_size; |
| 742 |
uint8_t i, num_configurations; |
| 743 |
|
| 744 |
USB_CONFIGURATION_DESCRIPTOR_SHORT cd_buf_short; // dummy request |
| 745 |
PUSB_DESCRIPTOR_REQUEST cd_buf_actual = NULL; // actual request |
| 746 |
PUSB_CONFIGURATION_DESCRIPTOR cd_data; |
| 747 |
|
| 748 |
num_configurations = dev->device_descriptor.bNumConfigurations; |
| 749 |
if (num_configurations == 0) |
| 750 |
return; |
| 751 |
|
| 752 |
assert(sizeof(USB_DESCRIPTOR_REQUEST) == USB_DESCRIPTOR_REQUEST_SIZE); |
| 753 |
|
| 754 |
priv->config_descriptor = calloc(num_configurations, sizeof(PUSB_CONFIGURATION_DESCRIPTOR)); |
| 755 |
if (priv->config_descriptor == NULL) { |
| 756 |
usbi_err(ctx, "could not allocate configuration descriptor array for '%s'", priv->dev_id); |
| 757 |
return; |
| 758 |
} |
| 759 |
|
| 760 |
for (i = 0; i <= num_configurations; i++) { |
| 761 |
safe_free(cd_buf_actual); |
| 762 |
|
| 763 |
if (i == num_configurations) |
| 764 |
break; |
| 765 |
|
| 766 |
size = sizeof(cd_buf_short); |
| 767 |
memset(&cd_buf_short.desc, 0, sizeof(cd_buf_short.desc)); |
| 768 |
|
| 769 |
cd_buf_short.req.ConnectionIndex = (ULONG)dev->port_number; |
| 770 |
cd_buf_short.req.SetupPacket.bmRequest = LIBUSB_ENDPOINT_IN; |
| 771 |
cd_buf_short.req.SetupPacket.bRequest = LIBUSB_REQUEST_GET_DESCRIPTOR; |
| 772 |
cd_buf_short.req.SetupPacket.wValue = (LIBUSB_DT_CONFIG << 8) | i; |
| 773 |
cd_buf_short.req.SetupPacket.wIndex = 0; |
| 774 |
cd_buf_short.req.SetupPacket.wLength = (USHORT)sizeof(USB_CONFIGURATION_DESCRIPTOR); |
| 775 |
|
| 776 |
// Dummy call to get the required data size. Initial failures are reported as info rather |
| 777 |
// than error as they can occur for non-penalizing situations, such as with some hubs. |
| 778 |
// coverity[tainted_data_argument] |
| 779 |
if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &cd_buf_short, size, |
| 780 |
&cd_buf_short, size, &ret_size, NULL)) { |
| 781 |
usbi_info(ctx, "could not access configuration descriptor %u (dummy) for '%s': %s", i, priv->dev_id, windows_error_str(0)); |
| 782 |
continue; |
| 783 |
} |
| 784 |
|
| 785 |
if ((ret_size != size) || (cd_buf_short.desc.wTotalLength < sizeof(USB_CONFIGURATION_DESCRIPTOR))) { |
| 786 |
usbi_info(ctx, "unexpected configuration descriptor %u size (dummy) for '%s'", i, priv->dev_id); |
| 787 |
continue; |
| 788 |
} |
| 789 |
|
| 790 |
size = sizeof(USB_DESCRIPTOR_REQUEST) + cd_buf_short.desc.wTotalLength; |
| 791 |
cd_buf_actual = malloc(size); |
| 792 |
if (cd_buf_actual == NULL) { |
| 793 |
usbi_err(ctx, "could not allocate configuration descriptor %u buffer for '%s'", i, priv->dev_id); |
| 794 |
continue; |
| 795 |
} |
| 796 |
|
| 797 |
// Actual call |
| 798 |
cd_buf_actual->ConnectionIndex = (ULONG)dev->port_number; |
| 799 |
cd_buf_actual->SetupPacket.bmRequest = LIBUSB_ENDPOINT_IN; |
| 800 |
cd_buf_actual->SetupPacket.bRequest = LIBUSB_REQUEST_GET_DESCRIPTOR; |
| 801 |
cd_buf_actual->SetupPacket.wValue = (LIBUSB_DT_CONFIG << 8) | i; |
| 802 |
cd_buf_actual->SetupPacket.wIndex = 0; |
| 803 |
cd_buf_actual->SetupPacket.wLength = cd_buf_short.desc.wTotalLength; |
| 804 |
|
| 805 |
if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, cd_buf_actual, size, |
| 806 |
cd_buf_actual, size, &ret_size, NULL)) { |
| 807 |
usbi_err(ctx, "could not access configuration descriptor %u (actual) for '%s': %s", i, priv->dev_id, windows_error_str(0)); |
| 808 |
continue; |
| 809 |
} |
| 810 |
|
| 811 |
cd_data = (PUSB_CONFIGURATION_DESCRIPTOR)((UCHAR *)cd_buf_actual + USB_DESCRIPTOR_REQUEST_SIZE); |
| 812 |
|
| 813 |
if ((size != ret_size) || (cd_data->wTotalLength != cd_buf_short.desc.wTotalLength)) { |
| 814 |
usbi_err(ctx, "unexpected configuration descriptor %u size (actual) for '%s'", i, priv->dev_id); |
| 815 |
continue; |
| 816 |
} |
| 817 |
|
| 818 |
if (cd_data->bDescriptorType != LIBUSB_DT_CONFIG) { |
| 819 |
usbi_err(ctx, "descriptor %u not a configuration descriptor for '%s'", i, priv->dev_id); |
| 820 |
continue; |
| 821 |
} |
| 822 |
|
| 823 |
usbi_dbg(ctx, "cached config descriptor %u (bConfigurationValue=%u, %u bytes)", |
| 824 |
i, cd_data->bConfigurationValue, cd_data->wTotalLength); |
| 825 |
|
| 826 |
// Cache the descriptor |
| 827 |
priv->config_descriptor[i] = cd_data; |
| 828 |
cd_buf_actual = NULL; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
#define ROOT_HUB_FS_CONFIG_DESC_LENGTH 0x19 |
| 833 |
#define ROOT_HUB_HS_CONFIG_DESC_LENGTH 0x19 |
| 834 |
#define ROOT_HUB_SS_CONFIG_DESC_LENGTH 0x1f |
| 835 |
#define CONFIG_DESC_WTOTAL_LENGTH_OFFSET 0x02 |
| 836 |
#define CONFIG_DESC_EP_MAX_PACKET_OFFSET 0x16 |
| 837 |
#define CONFIG_DESC_EP_BINTERVAL_OFFSET 0x18 |
| 838 |
|
| 839 |
static const uint8_t root_hub_config_descriptor_template[] = { |
| 840 |
// Configuration Descriptor |
| 841 |
LIBUSB_DT_CONFIG_SIZE, // bLength |
| 842 |
LIBUSB_DT_CONFIG, // bDescriptorType |
| 843 |
0x00, 0x00, // wTotalLength (filled in) |
| 844 |
0x01, // bNumInterfaces |
| 845 |
0x01, // bConfigurationValue |
| 846 |
0x00, // iConfiguration |
| 847 |
0xc0, // bmAttributes (reserved + self-powered) |
| 848 |
0x00, // bMaxPower |
| 849 |
// Interface Descriptor |
| 850 |
LIBUSB_DT_INTERFACE_SIZE, // bLength |
| 851 |
LIBUSB_DT_INTERFACE, // bDescriptorType |
| 852 |
0x00, // bInterfaceNumber |
| 853 |
0x00, // bAlternateSetting |
| 854 |
0x01, // bNumEndpoints |
| 855 |
LIBUSB_CLASS_HUB, // bInterfaceClass |
| 856 |
0x00, // bInterfaceSubClass |
| 857 |
0x00, // bInterfaceProtocol |
| 858 |
0x00, // iInterface |
| 859 |
// Endpoint Descriptor |
| 860 |
LIBUSB_DT_ENDPOINT_SIZE, // bLength |
| 861 |
LIBUSB_DT_ENDPOINT, // bDescriptorType |
| 862 |
0x81, // bEndpointAddress |
| 863 |
0x03, // bmAttributes (Interrupt) |
| 864 |
0x00, 0x00, // wMaxPacketSize (filled in) |
| 865 |
0x00, // bInterval (filled in) |
| 866 |
// SuperSpeed Endpoint Companion Descriptor |
| 867 |
LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE, // bLength |
| 868 |
LIBUSB_DT_SS_ENDPOINT_COMPANION, // bDescriptorType |
| 869 |
0x00, // bMaxBurst |
| 870 |
0x00, // bmAttributes |
| 871 |
0x02, 0x00 // wBytesPerInterval |
| 872 |
}; |
| 873 |
|
| 874 |
static int alloc_root_hub_config_desc(struct libusb_device *dev, ULONG num_ports, |
| 875 |
uint8_t config_desc_length, uint8_t ep_interval) |
| 876 |
{ |
| 877 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 878 |
uint8_t *ptr; |
| 879 |
|
| 880 |
priv->config_descriptor = malloc(sizeof(*priv->config_descriptor)); |
| 881 |
if (priv->config_descriptor == NULL) |
| 882 |
return LIBUSB_ERROR_NO_MEM; |
| 883 |
|
| 884 |
// Most config descriptors come from cache_config_descriptors() which obtains the |
| 885 |
// descriptors from the hub using an allocated USB_DESCRIPTOR_REQUEST structure. |
| 886 |
// To avoid an extra malloc + memcpy we just hold on to the USB_DESCRIPTOR_REQUEST |
| 887 |
// structure we already have and back up the pointer in windows_device_priv_release() |
| 888 |
// when freeing the descriptors. To keep a single execution path, we need to offset |
| 889 |
// the pointer here by the same amount. |
| 890 |
ptr = malloc(USB_DESCRIPTOR_REQUEST_SIZE + config_desc_length); |
| 891 |
if (ptr == NULL) |
| 892 |
return LIBUSB_ERROR_NO_MEM; |
| 893 |
|
| 894 |
ptr += USB_DESCRIPTOR_REQUEST_SIZE; |
| 895 |
|
| 896 |
memcpy(ptr, root_hub_config_descriptor_template, config_desc_length); |
| 897 |
ptr[CONFIG_DESC_WTOTAL_LENGTH_OFFSET] = config_desc_length; |
| 898 |
ptr[CONFIG_DESC_EP_MAX_PACKET_OFFSET] = (uint8_t)((num_ports + 7) / 8); |
| 899 |
ptr[CONFIG_DESC_EP_BINTERVAL_OFFSET] = ep_interval; |
| 900 |
|
| 901 |
priv->config_descriptor[0] = (PUSB_CONFIGURATION_DESCRIPTOR)ptr; |
| 902 |
priv->active_config = 1; |
| 903 |
|
| 904 |
return 0; |
| 905 |
} |
| 906 |
|
| 907 |
static int init_root_hub(struct libusb_device *dev) |
| 908 |
{ |
| 909 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 910 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 911 |
USB_NODE_CONNECTION_INFORMATION_EX conn_info; |
| 912 |
USB_NODE_CONNECTION_INFORMATION_EX_V2 conn_info_v2; |
| 913 |
USB_NODE_INFORMATION hub_info; |
| 914 |
enum libusb_speed speed = LIBUSB_SPEED_UNKNOWN; |
| 915 |
uint8_t config_desc_length; |
| 916 |
uint8_t ep_interval; |
| 917 |
HANDLE handle; |
| 918 |
ULONG port_number, num_ports; |
| 919 |
DWORD size; |
| 920 |
int r; |
| 921 |
|
| 922 |
// Determining the speed of a root hub is painful. Microsoft does not directly report the speed |
| 923 |
// capabilities of the root hub itself, only its ports and/or connected devices. Therefore we |
| 924 |
// are forced to query each individual port of the root hub to try and infer the root hub's |
| 925 |
// speed. Note that we have to query all ports because the presence of a device on that port |
| 926 |
// changes if/how Windows returns any useful speed information. |
| 927 |
handle = CreateFileA(priv->path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
| 928 |
if (handle == INVALID_HANDLE_VALUE) { |
| 929 |
usbi_err(ctx, "could not open root hub %s: %s", priv->path, windows_error_str(0)); |
| 930 |
return LIBUSB_ERROR_ACCESS; |
| 931 |
} |
| 932 |
|
| 933 |
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_INFORMATION, NULL, 0, &hub_info, sizeof(hub_info), &size, NULL)) { |
| 934 |
usbi_warn(ctx, "could not get root hub info for '%s': %s", priv->dev_id, windows_error_str(0)); |
| 935 |
CloseHandle(handle); |
| 936 |
return LIBUSB_ERROR_ACCESS; |
| 937 |
} |
| 938 |
|
| 939 |
num_ports = hub_info.u.HubInformation.HubDescriptor.bNumberOfPorts; |
| 940 |
usbi_dbg(ctx, "root hub '%s' reports %lu ports", priv->dev_id, ULONG_CAST(num_ports)); |
| 941 |
|
| 942 |
if (windows_version >= WINDOWS_8) { |
| 943 |
// Windows 8 and later is better at reporting the speed capabilities of the root hub, |
| 944 |
// but it is not perfect. If no device is attached to the port being queried, the |
| 945 |
// returned information will only indicate whether that port supports USB 3.0 signalling. |
| 946 |
// That is not enough information to distinguish between SuperSpeed and SuperSpeed Plus. |
| 947 |
for (port_number = 1; port_number <= num_ports; port_number++) { |
| 948 |
conn_info_v2.ConnectionIndex = port_number; |
| 949 |
conn_info_v2.Length = sizeof(conn_info_v2); |
| 950 |
conn_info_v2.SupportedUsbProtocols.Usb300 = 1; |
| 951 |
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, |
| 952 |
&conn_info_v2, sizeof(conn_info_v2), &conn_info_v2, sizeof(conn_info_v2), &size, NULL)) { |
| 953 |
usbi_warn(ctx, "could not get node connection information (V2) for root hub '%s' port %lu: %s", |
| 954 |
priv->dev_id, ULONG_CAST(port_number), windows_error_str(0)); |
| 955 |
break; |
| 956 |
} |
| 957 |
|
| 958 |
if (conn_info_v2.Flags.DeviceIsSuperSpeedPlusCapableOrHigher) |
| 959 |
speed = MAX(speed, LIBUSB_SPEED_SUPER_PLUS); |
| 960 |
else if (conn_info_v2.Flags.DeviceIsSuperSpeedCapableOrHigher || conn_info_v2.SupportedUsbProtocols.Usb300) |
| 961 |
speed = MAX(speed, LIBUSB_SPEED_SUPER); |
| 962 |
else if (conn_info_v2.SupportedUsbProtocols.Usb200) |
| 963 |
speed = MAX(speed, LIBUSB_SPEED_HIGH); |
| 964 |
else |
| 965 |
speed = MAX(speed, LIBUSB_SPEED_FULL); |
| 966 |
} |
| 967 |
|
| 968 |
if (speed != LIBUSB_SPEED_UNKNOWN) |
| 969 |
goto make_descriptors; |
| 970 |
} |
| 971 |
|
| 972 |
// At this point the speed is still not known, most likely because we are executing on |
| 973 |
// Windows 7 or earlier. The following hackery peeks into the root hub's Device ID and |
| 974 |
// tries to extract speed information from it, based on observed naming conventions. |
| 975 |
// If this does not work, we will query individual ports of the root hub. |
| 976 |
if (strstr(priv->dev_id, "ROOT_HUB31") != NULL) |
| 977 |
speed = LIBUSB_SPEED_SUPER_PLUS; |
| 978 |
else if (strstr(priv->dev_id, "ROOT_HUB30") != NULL) |
| 979 |
speed = LIBUSB_SPEED_SUPER; |
| 980 |
else if (strstr(priv->dev_id, "ROOT_HUB20") != NULL) |
| 981 |
speed = LIBUSB_SPEED_HIGH; |
| 982 |
|
| 983 |
if (speed != LIBUSB_SPEED_UNKNOWN) |
| 984 |
goto make_descriptors; |
| 985 |
|
| 986 |
// Windows only reports speed information about a connected device. This means that a root |
| 987 |
// hub with no connected devices or devices that are all operating at a speed less than the |
| 988 |
// highest speed that the root hub supports will not give us the correct speed. |
| 989 |
for (port_number = 1; port_number <= num_ports; port_number++) { |
| 990 |
conn_info.ConnectionIndex = port_number; |
| 991 |
if (!DeviceIoControl(handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, &conn_info, sizeof(conn_info), |
| 992 |
&conn_info, sizeof(conn_info), &size, NULL)) { |
| 993 |
usbi_warn(ctx, "could not get node connection information for root hub '%s' port %lu: %s", |
| 994 |
priv->dev_id, ULONG_CAST(port_number), windows_error_str(0)); |
| 995 |
continue; |
| 996 |
} |
| 997 |
|
| 998 |
if (conn_info.ConnectionStatus != DeviceConnected) |
| 999 |
continue; |
| 1000 |
|
| 1001 |
if (conn_info.Speed == UsbHighSpeed) { |
| 1002 |
speed = LIBUSB_SPEED_HIGH; |
| 1003 |
break; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
make_descriptors: |
| 1008 |
CloseHandle(handle); |
| 1009 |
|
| 1010 |
dev->device_descriptor.bLength = LIBUSB_DT_DEVICE_SIZE; |
| 1011 |
dev->device_descriptor.bDescriptorType = LIBUSB_DT_DEVICE; |
| 1012 |
dev->device_descriptor.bDeviceClass = LIBUSB_CLASS_HUB; |
| 1013 |
if ((dev->device_descriptor.idVendor == 0) && (dev->device_descriptor.idProduct == 0)) { |
| 1014 |
dev->device_descriptor.idVendor = 0x1d6b; // Linux Foundation |
| 1015 |
dev->device_descriptor.idProduct = (uint16_t)speed; |
| 1016 |
} |
| 1017 |
dev->device_descriptor.bcdDevice = 0x0100; |
| 1018 |
dev->device_descriptor.bNumConfigurations = 1; |
| 1019 |
|
| 1020 |
switch (speed) { |
| 1021 |
case LIBUSB_SPEED_SUPER_PLUS: |
| 1022 |
dev->device_descriptor.bcdUSB = 0x0310; |
| 1023 |
config_desc_length = ROOT_HUB_SS_CONFIG_DESC_LENGTH; |
| 1024 |
ep_interval = 0x0c; // 256ms |
| 1025 |
break; |
| 1026 |
case LIBUSB_SPEED_SUPER: |
| 1027 |
dev->device_descriptor.bcdUSB = 0x0300; |
| 1028 |
config_desc_length = ROOT_HUB_SS_CONFIG_DESC_LENGTH; |
| 1029 |
ep_interval = 0x0c; // 256ms |
| 1030 |
break; |
| 1031 |
case LIBUSB_SPEED_HIGH: |
| 1032 |
dev->device_descriptor.bcdUSB = 0x0200; |
| 1033 |
config_desc_length = ROOT_HUB_HS_CONFIG_DESC_LENGTH; |
| 1034 |
ep_interval = 0x0c; // 256ms |
| 1035 |
break; |
| 1036 |
case LIBUSB_SPEED_LOW: // Not used, but keeps compiler happy |
| 1037 |
case LIBUSB_SPEED_UNKNOWN: |
| 1038 |
// This case means absolutely no information about this root hub was determined. |
| 1039 |
// There is not much choice than to be pessimistic and label this as a |
| 1040 |
// full-speed device. |
| 1041 |
speed = LIBUSB_SPEED_FULL; |
| 1042 |
// fallthrough |
| 1043 |
case LIBUSB_SPEED_FULL: |
| 1044 |
dev->device_descriptor.bcdUSB = 0x0110; |
| 1045 |
config_desc_length = ROOT_HUB_FS_CONFIG_DESC_LENGTH; |
| 1046 |
ep_interval = 0xff; // 255ms |
| 1047 |
break; |
| 1048 |
default: // Impossible, buts keeps compiler happy |
| 1049 |
usbi_err(ctx, "program assertion failed - unknown root hub speed"); |
| 1050 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 1051 |
} |
| 1052 |
|
| 1053 |
if (speed >= LIBUSB_SPEED_SUPER) { |
| 1054 |
dev->device_descriptor.bDeviceProtocol = 0x03; // USB 3.0 Hub |
| 1055 |
dev->device_descriptor.bMaxPacketSize0 = 0x09; // 2^9 bytes |
| 1056 |
} else { |
| 1057 |
dev->device_descriptor.bMaxPacketSize0 = 0x40; // 64 bytes |
| 1058 |
} |
| 1059 |
|
| 1060 |
dev->speed = speed; |
| 1061 |
|
| 1062 |
r = alloc_root_hub_config_desc(dev, num_ports, config_desc_length, ep_interval); |
| 1063 |
if (r) |
| 1064 |
usbi_err(ctx, "could not allocate config descriptor for root hub '%s'", priv->dev_id); |
| 1065 |
|
| 1066 |
return r; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* |
| 1070 |
* Populate a libusb device structure |
| 1071 |
*/ |
| 1072 |
static int init_device(struct libusb_device *dev, struct libusb_device *parent_dev, |
| 1073 |
uint8_t port_number, DEVINST devinst) |
| 1074 |
{ |
| 1075 |
struct libusb_context *ctx = NULL; |
| 1076 |
struct libusb_device *tmp_dev; |
| 1077 |
struct winusb_device_priv *priv, *parent_priv, *tmp_priv; |
| 1078 |
USB_NODE_CONNECTION_INFORMATION_EX conn_info; |
| 1079 |
USB_NODE_CONNECTION_INFORMATION_EX_V2 conn_info_v2; |
| 1080 |
HANDLE hub_handle; |
| 1081 |
DWORD size; |
| 1082 |
uint8_t bus_number, depth; |
| 1083 |
int r; |
| 1084 |
|
| 1085 |
priv = usbi_get_device_priv(dev); |
| 1086 |
|
| 1087 |
// If the device is already initialized, we can stop here |
| 1088 |
if (priv->initialized) |
| 1089 |
return LIBUSB_SUCCESS; |
| 1090 |
|
| 1091 |
if (parent_dev != NULL) { // Not a HCD root hub |
| 1092 |
ctx = DEVICE_CTX(dev); |
| 1093 |
parent_priv = usbi_get_device_priv(parent_dev); |
| 1094 |
if (parent_priv->apib->id != USB_API_HUB) { |
| 1095 |
usbi_warn(ctx, "parent for device '%s' is not a hub", priv->dev_id); |
| 1096 |
return LIBUSB_ERROR_NOT_FOUND; |
| 1097 |
} |
| 1098 |
|
| 1099 |
// Calculate depth and fetch bus number |
| 1100 |
bus_number = parent_dev->bus_number; |
| 1101 |
if (bus_number == 0) { |
| 1102 |
tmp_dev = get_ancestor(ctx, devinst, &devinst); |
| 1103 |
if (tmp_dev != parent_dev) { |
| 1104 |
usbi_err(ctx, "program assertion failed - first ancestor is not parent"); |
| 1105 |
return LIBUSB_ERROR_NOT_FOUND; |
| 1106 |
} |
| 1107 |
libusb_unref_device(tmp_dev); |
| 1108 |
|
| 1109 |
for (depth = 1; bus_number == 0; depth++) { |
| 1110 |
tmp_dev = get_ancestor(ctx, devinst, &devinst); |
| 1111 |
if (tmp_dev == NULL) { |
| 1112 |
usbi_warn(ctx, "ancestor for device '%s' not found at depth %u", priv->dev_id, depth); |
| 1113 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1114 |
} |
| 1115 |
if (tmp_dev->bus_number != 0) { |
| 1116 |
bus_number = tmp_dev->bus_number; |
| 1117 |
tmp_priv = usbi_get_device_priv(tmp_dev); |
| 1118 |
depth += tmp_priv->depth; |
| 1119 |
} |
| 1120 |
libusb_unref_device(tmp_dev); |
| 1121 |
} |
| 1122 |
} else { |
| 1123 |
depth = parent_priv->depth + 1; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (bus_number == 0) { |
| 1127 |
usbi_err(ctx, "program assertion failed - bus number not found for '%s'", priv->dev_id); |
| 1128 |
return LIBUSB_ERROR_NOT_FOUND; |
| 1129 |
} |
| 1130 |
|
| 1131 |
dev->bus_number = bus_number; |
| 1132 |
dev->port_number = port_number; |
| 1133 |
dev->parent_dev = parent_dev; |
| 1134 |
priv->depth = depth; |
| 1135 |
|
| 1136 |
hub_handle = CreateFileA(parent_priv->path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
| 1137 |
if (hub_handle == INVALID_HANDLE_VALUE) { |
| 1138 |
usbi_warn(ctx, "could not open hub %s: %s", parent_priv->path, windows_error_str(0)); |
| 1139 |
return LIBUSB_ERROR_ACCESS; |
| 1140 |
} |
| 1141 |
|
| 1142 |
conn_info.ConnectionIndex = (ULONG)port_number; |
| 1143 |
// coverity[tainted_data_argument] |
| 1144 |
|
| 1145 |
if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX, &conn_info, sizeof(conn_info), |
| 1146 |
&conn_info, sizeof(conn_info), &size, NULL)) { |
| 1147 |
usbi_warn(ctx, "could not get node connection information for device '%s': %s", |
| 1148 |
priv->dev_id, windows_error_str(0)); |
| 1149 |
CloseHandle(hub_handle); |
| 1150 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (conn_info.ConnectionStatus == NoDeviceConnected) { |
| 1154 |
usbi_err(ctx, "device '%s' is no longer connected!", priv->dev_id); |
| 1155 |
CloseHandle(hub_handle); |
| 1156 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1157 |
} |
| 1158 |
|
| 1159 |
if ((conn_info.DeviceDescriptor.bLength != LIBUSB_DT_DEVICE_SIZE) |
| 1160 |
|| (conn_info.DeviceDescriptor.bDescriptorType != LIBUSB_DT_DEVICE)) { |
| 1161 |
usbi_warn(ctx, "device '%s' has invalid descriptor!", priv->dev_id); |
| 1162 |
CloseHandle(hub_handle); |
| 1163 |
return LIBUSB_ERROR_OTHER; |
| 1164 |
} |
| 1165 |
|
| 1166 |
static_assert(sizeof(dev->device_descriptor) == sizeof(conn_info.DeviceDescriptor), |
| 1167 |
"mismatch between libusb and OS device descriptor sizes"); |
| 1168 |
memcpy(&dev->device_descriptor, &conn_info.DeviceDescriptor, LIBUSB_DT_DEVICE_SIZE); |
| 1169 |
usbi_localize_device_descriptor(&dev->device_descriptor); |
| 1170 |
|
| 1171 |
if (conn_info.CurrentConfigurationValue == 0) { |
| 1172 |
usbi_dbg(ctx, "found %u configurations for device '%s' but device is not configured (i.e. current config: 0), ignoring it", |
| 1173 |
dev->device_descriptor.bNumConfigurations, |
| 1174 |
priv->dev_id); |
| 1175 |
CloseHandle(hub_handle); |
| 1176 |
return LIBUSB_ERROR_OTHER; |
| 1177 |
} |
| 1178 |
|
| 1179 |
priv->active_config = conn_info.CurrentConfigurationValue; |
| 1180 |
usbi_dbg(ctx, "found %u configurations (current config: %u) for device '%s'", |
| 1181 |
dev->device_descriptor.bNumConfigurations, priv->active_config, priv->dev_id); |
| 1182 |
|
| 1183 |
// Cache as many config descriptors as we can |
| 1184 |
cache_config_descriptors(dev, hub_handle); |
| 1185 |
|
| 1186 |
// In their great wisdom, Microsoft decided to BREAK the USB speed report between Windows 7 and Windows 8 |
| 1187 |
if (windows_version >= WINDOWS_8) { |
| 1188 |
conn_info_v2.ConnectionIndex = (ULONG)port_number; |
| 1189 |
conn_info_v2.Length = sizeof(USB_NODE_CONNECTION_INFORMATION_EX_V2); |
| 1190 |
conn_info_v2.SupportedUsbProtocols.Usb300 = 1; |
| 1191 |
if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, |
| 1192 |
&conn_info_v2, sizeof(conn_info_v2), &conn_info_v2, sizeof(conn_info_v2), &size, NULL)) { |
| 1193 |
usbi_warn(ctx, "could not get node connection information (V2) for device '%s': %s", |
| 1194 |
priv->dev_id, windows_error_str(0)); |
| 1195 |
} else if (conn_info_v2.Flags.DeviceIsOperatingAtSuperSpeedPlusOrHigher) { |
| 1196 |
conn_info.Speed = UsbSuperSpeedPlus; |
| 1197 |
} else if (conn_info_v2.Flags.DeviceIsOperatingAtSuperSpeedOrHigher) { |
| 1198 |
conn_info.Speed = UsbSuperSpeed; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
CloseHandle(hub_handle); |
| 1203 |
|
| 1204 |
if (conn_info.DeviceAddress > UINT8_MAX) |
| 1205 |
usbi_err(ctx, "program assertion failed - device address overflow"); |
| 1206 |
|
| 1207 |
dev->device_address = (uint8_t)conn_info.DeviceAddress; |
| 1208 |
|
| 1209 |
switch (conn_info.Speed) { |
| 1210 |
case UsbLowSpeed: dev->speed = LIBUSB_SPEED_LOW; break; |
| 1211 |
case UsbFullSpeed: dev->speed = LIBUSB_SPEED_FULL; break; |
| 1212 |
case UsbHighSpeed: dev->speed = LIBUSB_SPEED_HIGH; break; |
| 1213 |
case UsbSuperSpeed: dev->speed = LIBUSB_SPEED_SUPER; break; |
| 1214 |
case UsbSuperSpeedPlus: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break; |
| 1215 |
default: |
| 1216 |
usbi_warn(ctx, "unknown device speed %u", conn_info.Speed); |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
} else { |
| 1220 |
r = init_root_hub(dev); |
| 1221 |
if (r) |
| 1222 |
return r; |
| 1223 |
} |
| 1224 |
|
| 1225 |
r = usbi_sanitize_device(dev); |
| 1226 |
if (r) |
| 1227 |
return r; |
| 1228 |
|
| 1229 |
priv->initialized = true; |
| 1230 |
|
| 1231 |
usbi_dbg(ctx, "(bus: %u, addr: %u, depth: %u, port: %u): '%s'", |
| 1232 |
dev->bus_number, dev->device_address, priv->depth, dev->port_number, priv->dev_id); |
| 1233 |
|
| 1234 |
return LIBUSB_SUCCESS; |
| 1235 |
} |
| 1236 |
|
| 1237 |
static bool get_dev_port_number(HDEVINFO dev_info, SP_DEVINFO_DATA *dev_info_data, DWORD *port_nr) |
| 1238 |
{ |
| 1239 |
char buffer[MAX_KEY_LENGTH]; |
| 1240 |
DWORD size; |
| 1241 |
const char *start = NULL; |
| 1242 |
char *end = NULL; |
| 1243 |
long long port; |
| 1244 |
|
| 1245 |
// First try SPDRP_LOCATION_INFORMATION, which returns a REG_SZ. The string *may* have a format |
| 1246 |
// similar to "Port_#0002.Hub_#000D", in which case we can extract the port number. However, we |
| 1247 |
// cannot extract the port if the returned string does not follow this format. |
| 1248 |
if (pSetupDiGetDeviceRegistryPropertyA(dev_info, dev_info_data, SPDRP_LOCATION_INFORMATION, |
| 1249 |
NULL, (PBYTE)buffer, sizeof(buffer), NULL)) { |
| 1250 |
// Check for the required format. |
| 1251 |
if (strncmp(buffer, "Port_#", 6) == 0) { |
| 1252 |
start = buffer + 6; |
| 1253 |
// Note that 0 is both strtoll's sentinel return value to indicate failure, as well |
| 1254 |
// as (obviously) the return value for the literal "0". Fortunately we can always treat |
| 1255 |
// 0 as a failure, since Windows USB port numbers are numbered 1..n. |
| 1256 |
port = strtoll(start, &end, 10); |
| 1257 |
if (port <= 0 || port >= ULONG_MAX || end == start || (*end != '.' && *end != '\0')) { |
| 1258 |
return false; |
| 1259 |
} |
| 1260 |
*port_nr = (DWORD)port; |
| 1261 |
return true; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
// Next try SPDRP_LOCATION_PATHS, which returns a REG_MULTI_SZ (but we only examine the first |
| 1266 |
// string in it). Each path has a format similar to, |
| 1267 |
// "PCIROOT(B2)#PCI(0300)#PCI(0000)#USBROOT(0)#USB(1)#USB(2)#USBMI(3)", and the port number is |
| 1268 |
// the number within the last "USB(x)" token. |
| 1269 |
if (pSetupDiGetDeviceRegistryPropertyA(dev_info, dev_info_data, SPDRP_LOCATION_PATHS, |
| 1270 |
NULL, (PBYTE)buffer, sizeof(buffer), NULL)) { |
| 1271 |
// Find the last "#USB(x)" substring |
| 1272 |
for (char *token = strrchr(buffer, '#'); token != NULL; token = strrchr(buffer, '#')) { |
| 1273 |
if (strncmp(token, "#USB(", 5) == 0) { |
| 1274 |
start = token + 5; |
| 1275 |
port = strtoll(start, &end, 10); |
| 1276 |
if (port <= 0 || port >= ULONG_MAX || end == start || (*end != ')' && *end != '\0')) { |
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
*port_nr = (DWORD)port; |
| 1280 |
return true; |
| 1281 |
} |
| 1282 |
// Shorten the string and try again. |
| 1283 |
*token = '\0'; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
// Lastly, try SPDRP_ADDRESS, which returns a REG_DWORD. The address *may* be the port number, |
| 1288 |
// which is true for the Microsoft driver but may not be true for other drivers. However, we |
| 1289 |
// have no other options here but to accept what it returns. |
| 1290 |
return pSetupDiGetDeviceRegistryPropertyA(dev_info, dev_info_data, SPDRP_ADDRESS, |
| 1291 |
NULL, (PBYTE)port_nr, sizeof(*port_nr), &size) && (size == sizeof(*port_nr)); |
| 1292 |
} |
| 1293 |
|
| 1294 |
static int enumerate_hcd_root_hub(struct libusb_context *ctx, const char *dev_id, |
| 1295 |
DEVINST devinst) |
| 1296 |
{ |
| 1297 |
DEVINST child_devinst; |
| 1298 |
struct libusb_device* dev; |
| 1299 |
|
| 1300 |
if (CM_Get_Child(&child_devinst, devinst, 0) != CR_SUCCESS) { |
| 1301 |
usbi_warn(ctx, "could not get child devinst for '%s'", dev_id); |
| 1302 |
return LIBUSB_SUCCESS; |
| 1303 |
} |
| 1304 |
|
| 1305 |
dev = usbi_get_device_by_session_id(ctx, (unsigned long)child_devinst); |
| 1306 |
if (dev == NULL) { |
| 1307 |
usbi_warn(ctx, "HCD '%s' child not found", dev_id); |
| 1308 |
return LIBUSB_SUCCESS; |
| 1309 |
} |
| 1310 |
|
| 1311 |
if (sscanf(dev_id, "PCI\\VEN_%04hx&DEV_%04hx%*s", &dev->device_descriptor.idVendor, &dev->device_descriptor.idProduct) != 2) |
| 1312 |
usbi_warn(ctx, "could not infer VID/PID of HCD from '%s'", dev_id); |
| 1313 |
libusb_unref_device(dev); |
| 1314 |
return LIBUSB_SUCCESS; |
| 1315 |
} |
| 1316 |
|
| 1317 |
// Returns the api type, or 0 if not found/unsupported |
| 1318 |
static void get_api_type(HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, |
| 1319 |
int *api, int *sub_api) |
| 1320 |
{ |
| 1321 |
// Precedence for filter drivers vs driver is in the order of this array |
| 1322 |
struct driver_lookup lookup[3] = { |
| 1323 |
{"\0\0", SPDRP_SERVICE, "driver"}, |
| 1324 |
{"\0\0", SPDRP_UPPERFILTERS, "upper filter driver"}, |
| 1325 |
{"\0\0", SPDRP_LOWERFILTERS, "lower filter driver"} |
| 1326 |
}; |
| 1327 |
DWORD size, reg_type; |
| 1328 |
unsigned k, l; |
| 1329 |
int i, j; |
| 1330 |
|
| 1331 |
// Check the service & filter names to know the API we should use |
| 1332 |
for (k = 0; k < 3; k++) { |
| 1333 |
if (pSetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop, |
| 1334 |
®_type, (PBYTE)lookup[k].list, MAX_KEY_LENGTH, &size)) { |
| 1335 |
// Turn the REG_SZ SPDRP_SERVICE into REG_MULTI_SZ |
| 1336 |
if (lookup[k].reg_prop == SPDRP_SERVICE) |
| 1337 |
// our buffers are MAX_KEY_LENGTH + 1 so we can overflow if needed |
| 1338 |
lookup[k].list[strlen(lookup[k].list) + 1] = 0; |
| 1339 |
|
| 1340 |
// MULTI_SZ is a pain to work with. Turn it into something much more manageable |
| 1341 |
// NB: none of the driver names we check against contain LIST_SEPARATOR, |
| 1342 |
// (currently ';'), so even if an unsupported one does, it's not an issue |
| 1343 |
for (l = 0; (lookup[k].list[l] != 0) || (lookup[k].list[l + 1] != 0); l++) { |
| 1344 |
if (lookup[k].list[l] == 0) |
| 1345 |
lookup[k].list[l] = LIST_SEPARATOR; |
| 1346 |
} |
| 1347 |
usbi_dbg(NULL, "%s(s): %s", lookup[k].designation, lookup[k].list); |
| 1348 |
} else { |
| 1349 |
if (GetLastError() != ERROR_INVALID_DATA) |
| 1350 |
usbi_dbg(NULL, "could not access %s: %s", lookup[k].designation, windows_error_str(0)); |
| 1351 |
lookup[k].list[0] = 0; |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
for (i = 2; i < USB_API_MAX; i++) { |
| 1356 |
for (k = 0; k < 3; k++) { |
| 1357 |
j = get_sub_api(lookup[k].list, i); |
| 1358 |
if (j >= 0) { |
| 1359 |
usbi_dbg(NULL, "matched %s name against %s", lookup[k].designation, |
| 1360 |
(i != USB_API_WINUSBX) ? usb_api_backend[i].designation : usb_api_backend[i].driver_name_list[j]); |
| 1361 |
*api = i; |
| 1362 |
*sub_api = j; |
| 1363 |
return; |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
static int set_composite_interface(struct libusb_context *ctx, struct libusb_device *dev, |
| 1370 |
char *dev_interface_path, char *device_id, int api, int sub_api) |
| 1371 |
{ |
| 1372 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 1373 |
int interface_number; |
| 1374 |
const char *mi_str; |
| 1375 |
int iadi, iadintfi; |
| 1376 |
char* endptr; |
| 1377 |
struct libusb_interface_association_descriptor_array *iad_array; |
| 1378 |
const struct libusb_interface_association_descriptor *iad; |
| 1379 |
int r = LIBUSB_SUCCESS; |
| 1380 |
|
| 1381 |
// Because MI_## are not necessarily in sequential order (some composite |
| 1382 |
// devices will have only MI_00 & MI_03 for instance), we retrieve the actual |
| 1383 |
// interface number from the path's MI value |
| 1384 |
mi_str = strstr(device_id, "MI_"); |
| 1385 |
|
| 1386 |
endptr = NULL; |
| 1387 |
// This initialization, while redundant, is needed to make MSVC happy |
| 1388 |
interface_number = -1; |
| 1389 |
|
| 1390 |
if (mi_str != NULL) { |
| 1391 |
interface_number = strtoul(&mi_str[3], &endptr, 16); |
| 1392 |
} |
| 1393 |
|
| 1394 |
if (mi_str == NULL || endptr - &mi_str[3] != 2) { |
| 1395 |
usbi_warn(ctx, "failure to read interface number for %s, using default value", device_id); |
| 1396 |
interface_number = 0; |
| 1397 |
} |
| 1398 |
|
| 1399 |
if (interface_number >= USB_MAXINTERFACES) { |
| 1400 |
usbi_warn(ctx, "interface %d too large - ignoring interface path %s", interface_number, dev_interface_path); |
| 1401 |
return LIBUSB_ERROR_ACCESS; |
| 1402 |
} |
| 1403 |
|
| 1404 |
// Serialize concurrent enumeration callers that all reach this for the |
| 1405 |
// same parent device. Without this, the check-then-modify on |
| 1406 |
// priv->usb_interface[].path causes double-free / use-after-free. |
| 1407 |
usbi_mutex_lock(&priv->interface_lock); |
| 1408 |
|
| 1409 |
if (priv->usb_interface[interface_number].path != NULL) { |
| 1410 |
if (api == USB_API_HID) { |
| 1411 |
// HID devices can have multiple collections (COL##) for each MI_## interface |
| 1412 |
usbi_dbg(ctx, "interface[%d] already set - ignoring HID collection: %s", |
| 1413 |
interface_number, device_id); |
| 1414 |
r = LIBUSB_ERROR_ACCESS; |
| 1415 |
goto out; |
| 1416 |
} |
| 1417 |
// In other cases, just use the latest data |
| 1418 |
safe_free(priv->usb_interface[interface_number].path); |
| 1419 |
} |
| 1420 |
|
| 1421 |
usbi_dbg(ctx, "interface[%d] = %s", interface_number, dev_interface_path); |
| 1422 |
priv->usb_interface[interface_number].path = dev_interface_path; |
| 1423 |
priv->usb_interface[interface_number].apib = &usb_api_backend[api]; |
| 1424 |
priv->usb_interface[interface_number].sub_api = sub_api; |
| 1425 |
if ((api == USB_API_HID) && (priv->hid == NULL)) { |
| 1426 |
priv->hid = calloc(1, sizeof(struct hid_device_priv)); |
| 1427 |
if (priv->hid == NULL) { |
| 1428 |
r = LIBUSB_ERROR_NO_MEM; |
| 1429 |
goto out; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
// For WinUSBX, set up associations for interfaces grouped by an IAD |
| 1434 |
if ((api == USB_API_WINUSBX) && !libusb_get_active_interface_association_descriptors(dev, &iad_array)) { |
| 1435 |
for (iadi = 0; iadi < iad_array->length; iadi++) { |
| 1436 |
iad = &iad_array->iad[iadi]; |
| 1437 |
if (iad->bFirstInterface == interface_number) { |
| 1438 |
priv->usb_interface[interface_number].num_associated_interfaces = iad->bInterfaceCount; |
| 1439 |
priv->usb_interface[interface_number].first_associated_interface = iad->bFirstInterface; |
| 1440 |
for (iadintfi = 1; iadintfi < iad->bInterfaceCount; iadintfi++) { |
| 1441 |
usbi_dbg(ctx, "interface[%d] is associated with interface[%d]", |
| 1442 |
interface_number + iadintfi, interface_number); |
| 1443 |
priv->usb_interface[interface_number + iadintfi].apib = &usb_api_backend[api]; |
| 1444 |
priv->usb_interface[interface_number + iadintfi].sub_api = sub_api; |
| 1445 |
priv->usb_interface[interface_number + iadintfi].num_associated_interfaces = iad->bInterfaceCount; |
| 1446 |
priv->usb_interface[interface_number + iadintfi].first_associated_interface = iad->bFirstInterface; |
| 1447 |
} |
| 1448 |
break; |
| 1449 |
} |
| 1450 |
} |
| 1451 |
libusb_free_interface_association_descriptors(iad_array); |
| 1452 |
} |
| 1453 |
|
| 1454 |
out: |
| 1455 |
usbi_mutex_unlock(&priv->interface_lock); |
| 1456 |
return r; |
| 1457 |
} |
| 1458 |
|
| 1459 |
static int set_hid_interface(struct libusb_context *ctx, struct libusb_device *dev, |
| 1460 |
char *dev_interface_path) |
| 1461 |
{ |
| 1462 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 1463 |
uint8_t i; |
| 1464 |
int r = LIBUSB_SUCCESS; |
| 1465 |
|
| 1466 |
// Serialize concurrent enumeration callers that all reach this for the |
| 1467 |
// same parent device. Protects priv->hid->nb_interfaces and |
| 1468 |
// priv->usb_interface[].path / apib. |
| 1469 |
usbi_mutex_lock(&priv->interface_lock); |
| 1470 |
|
| 1471 |
if (priv->hid == NULL) { |
| 1472 |
usbi_err(ctx, "program assertion failed - parent is not HID"); |
| 1473 |
r = LIBUSB_ERROR_NO_DEVICE; |
| 1474 |
goto out; |
| 1475 |
} else if (priv->hid->nb_interfaces == USB_MAXINTERFACES) { |
| 1476 |
usbi_err(ctx, "program assertion failed - max USB interfaces reached for HID device"); |
| 1477 |
r = LIBUSB_ERROR_NO_DEVICE; |
| 1478 |
goto out; |
| 1479 |
} |
| 1480 |
|
| 1481 |
for (i = 0; i < priv->hid->nb_interfaces; i++) { |
| 1482 |
if ((priv->usb_interface[i].path != NULL) && strcmp(priv->usb_interface[i].path, dev_interface_path) == 0) { |
| 1483 |
usbi_dbg(ctx, "interface[%u] already set to %s", i, dev_interface_path); |
| 1484 |
r = LIBUSB_ERROR_ACCESS; |
| 1485 |
goto out; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
priv->usb_interface[priv->hid->nb_interfaces].path = dev_interface_path; |
| 1490 |
priv->usb_interface[priv->hid->nb_interfaces].apib = &usb_api_backend[USB_API_HID]; |
| 1491 |
usbi_dbg(ctx, "interface[%u] = %s", priv->hid->nb_interfaces, dev_interface_path); |
| 1492 |
priv->hid->nb_interfaces++; |
| 1493 |
|
| 1494 |
out: |
| 1495 |
usbi_mutex_unlock(&priv->interface_lock); |
| 1496 |
return r; |
| 1497 |
} |
| 1498 |
|
| 1499 |
// get the n-th device interface GUID indexed by guid_number |
| 1500 |
static int get_guid(struct libusb_context *ctx, char *dev_id, HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, |
| 1501 |
int guid_number, GUID **if_guid) |
| 1502 |
{ |
| 1503 |
DWORD size, reg_type; |
| 1504 |
HKEY key; |
| 1505 |
char *guid_string, *new_guid_string; |
| 1506 |
char *guid, *guid_term; |
| 1507 |
LONG s; |
| 1508 |
int pass, guids_left; |
| 1509 |
int err = LIBUSB_SUCCESS; |
| 1510 |
#if !defined(ENABLE_LOGGING) |
| 1511 |
UNUSED(dev_id); |
| 1512 |
#endif |
| 1513 |
|
| 1514 |
key = pSetupDiOpenDevRegKey(*dev_info, dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); |
| 1515 |
if (key == INVALID_HANDLE_VALUE) { |
| 1516 |
usbi_warn(ctx, "Cannot get the additional GUIDs for '%s'", dev_id); |
| 1517 |
return LIBUSB_ERROR_ACCESS; |
| 1518 |
} |
| 1519 |
// Reserve buffer large enough to hold one GUID with two terminating characters |
| 1520 |
size = MAX_GUID_STRING_LENGTH + 1; |
| 1521 |
// Allocate memory for storing the guid_string with two extra terminating characters |
| 1522 |
// This is necessary for parsing the REG_MULTI_SZ type below |
| 1523 |
guid_string = malloc(size + 2); |
| 1524 |
if (guid_string == NULL) { |
| 1525 |
usbi_err(ctx, "failed to alloc guid_string"); |
| 1526 |
return LIBUSB_ERROR_NO_MEM; |
| 1527 |
} |
| 1528 |
|
| 1529 |
// The 1st pass tries to get the guid. If it fails due to ERROR_MORE_DATA |
| 1530 |
// then reallocate enough memory for the 2nd pass |
| 1531 |
for (pass = 0; pass < 2; pass++) { |
| 1532 |
// Look for both DeviceInterfaceGUIDs *and* DeviceInterfaceGUID, in that order |
| 1533 |
// If multiple GUIDs, find the n-th that is indexed by guid_number |
| 1534 |
s = pRegQueryValueExA(key, "DeviceInterfaceGUIDs", NULL, ®_type, |
| 1535 |
(LPBYTE)guid_string, &size); |
| 1536 |
if (s == ERROR_FILE_NOT_FOUND) |
| 1537 |
s = pRegQueryValueExA(key, "DeviceInterfaceGUID", NULL, ®_type, |
| 1538 |
(LPBYTE)guid_string, &size); |
| 1539 |
if (s == ERROR_SUCCESS) { |
| 1540 |
// The GUID was read successfully |
| 1541 |
break; |
| 1542 |
} else if (s == ERROR_FILE_NOT_FOUND) { |
| 1543 |
usbi_dbg(ctx, "no DeviceInterfaceGUID registered for '%s'", dev_id); |
| 1544 |
err = LIBUSB_ERROR_ACCESS; |
| 1545 |
goto exit; |
| 1546 |
} else if (s == ERROR_MORE_DATA) { |
| 1547 |
if (pass == 1) { |
| 1548 |
// Previous pass should have allocated enough memory, but reading failed |
| 1549 |
usbi_warn(ctx, "unexpected error from pRegQueryValueExA for '%s'", dev_id); |
| 1550 |
err = LIBUSB_ERROR_OTHER; |
| 1551 |
goto exit; |
| 1552 |
} |
| 1553 |
new_guid_string = realloc((void *)guid_string, size + 2); |
| 1554 |
if (new_guid_string == NULL) { |
| 1555 |
usbi_err(ctx, "failed to realloc guid string"); |
| 1556 |
err = LIBUSB_ERROR_NO_MEM; |
| 1557 |
goto exit; |
| 1558 |
} |
| 1559 |
guid_string = new_guid_string; |
| 1560 |
} else { |
| 1561 |
usbi_warn(ctx, "unexpected error from pRegQueryValueExA for '%s'", dev_id); |
| 1562 |
err = LIBUSB_ERROR_ACCESS; |
| 1563 |
goto exit; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
// https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regqueryvalueexa#remarks |
| 1568 |
// - "string may not have been stored with the proper terminating null characters" |
| 1569 |
// - The following GUIDs should be consider as valid: |
| 1570 |
// "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\0", "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", |
| 1571 |
// "{xxx.....xx}\0\0\0", "{xxx.....xx}\0{xxx.....xx}\0{xxx.....xx}\0", |
| 1572 |
// "{xxx.....xx}\0{xxx.....xx}\0{xxx.....xx}", "{xxx.....xx}{xxx.....xx}{xxx.....xx}", |
| 1573 |
// "{xxx.....xx}\0{xxx.....xx}\0{xxx.....xx}\0\0\0\0" |
| 1574 |
if ((reg_type == REG_SZ ) || (reg_type == REG_MULTI_SZ)) { |
| 1575 |
/* Get the n-th GUID indexed by guid_number since the DeviceInterfaceGUIDs may |
| 1576 |
contain more GUIDs */ |
| 1577 |
guid = guid_string; |
| 1578 |
// Add two terminating chars for not overrunning the allocated memory while iterating |
| 1579 |
guid[size] = '\0'; |
| 1580 |
guid[size + 1] = '\0'; |
| 1581 |
// Iterate the GUIDs in the guid string |
| 1582 |
guids_left = guid_number; |
| 1583 |
while (guids_left) { |
| 1584 |
guid = strchr(guid, '}'); |
| 1585 |
if (guid == NULL) { |
| 1586 |
usbi_warn(ctx, "no GUID with index %d registered for '%s'", guid_number, dev_id); |
| 1587 |
err = LIBUSB_ERROR_ACCESS; |
| 1588 |
goto exit; |
| 1589 |
} |
| 1590 |
guid++; |
| 1591 |
// Skip the terminating char if available |
| 1592 |
if (*guid == '\0') { |
| 1593 |
guid++; |
| 1594 |
} |
| 1595 |
guids_left--; |
| 1596 |
} |
| 1597 |
// Add terminating char to the string |
| 1598 |
guid_term = strchr(guid, '}'); |
| 1599 |
if (guid_term == NULL) { |
| 1600 |
usbi_warn(ctx, "no GUID with index %d registered for '%s'", guid_number, dev_id); |
| 1601 |
err = LIBUSB_ERROR_ACCESS; |
| 1602 |
goto exit; |
| 1603 |
} |
| 1604 |
// Terminate the current guid string to handle the variant without separators |
| 1605 |
guid_term++; |
| 1606 |
*guid_term = '\0'; |
| 1607 |
} else { |
| 1608 |
usbi_warn(ctx, "unexpected type of DeviceInterfaceGUID for '%s'", dev_id); |
| 1609 |
err = LIBUSB_ERROR_ACCESS; |
| 1610 |
goto exit; |
| 1611 |
} |
| 1612 |
|
| 1613 |
*if_guid = malloc(sizeof(GUID)); |
| 1614 |
if (*if_guid == NULL) { |
| 1615 |
usbi_err(ctx, "failed to alloc if_guid"); |
| 1616 |
err = LIBUSB_ERROR_NO_MEM; |
| 1617 |
goto exit; |
| 1618 |
} |
| 1619 |
if (!string_to_guid(guid, *if_guid)) { |
| 1620 |
usbi_warn(ctx, "device '%s' has malformed DeviceInterfaceGUID string '%s', skipping", dev_id, guid); |
| 1621 |
free(*if_guid); |
| 1622 |
*if_guid = NULL; |
| 1623 |
goto exit; |
| 1624 |
} |
| 1625 |
|
| 1626 |
exit: |
| 1627 |
pRegCloseKey(key); |
| 1628 |
free(guid_string); |
| 1629 |
return err; |
| 1630 |
} |
| 1631 |
|
| 1632 |
/* |
| 1633 |
* get_device_list: libusb backend device enumeration function |
| 1634 |
*/ |
| 1635 |
static int winusb_get_device_list(struct libusb_context *ctx, struct discovered_devs **_discdevs) |
| 1636 |
{ |
| 1637 |
struct discovered_devs *discdevs; |
| 1638 |
HDEVINFO *dev_info, dev_info_intf, dev_info_enum; |
| 1639 |
SP_DEVINFO_DATA dev_info_data; |
| 1640 |
DWORD _index = 0; |
| 1641 |
GUID hid_guid; |
| 1642 |
int r = LIBUSB_SUCCESS; |
| 1643 |
int api, sub_api; |
| 1644 |
unsigned int pass, pass_type, i, j; |
| 1645 |
char enumerator[16]; |
| 1646 |
char dev_id[MAX_PATH_LENGTH]; |
| 1647 |
struct libusb_device *dev, *parent_dev; |
| 1648 |
struct winusb_device_priv *priv, *parent_priv; |
| 1649 |
char *dev_interface_path = NULL; |
| 1650 |
unsigned long session_id; |
| 1651 |
DWORD size, port_nr, install_state; |
| 1652 |
uint8_t bus_number = 0; |
| 1653 |
#if defined(ENABLE_LOGGING) |
| 1654 |
char guid_string[MAX_GUID_STRING_LENGTH]; |
| 1655 |
#endif |
| 1656 |
GUID *if_guid; |
| 1657 |
#define HUB_PASS 0 |
| 1658 |
#define DEV_PASS 1 |
| 1659 |
#define HCD_PASS 2 |
| 1660 |
#define GEN_PASS 3 |
| 1661 |
#define HID_PASS 4 |
| 1662 |
#define EXT_PASS 5 |
| 1663 |
// Keep a list of guids that will be enumerated |
| 1664 |
#define GUID_SIZE_STEP 8 |
| 1665 |
const GUID **guid_list, **new_guid_list; |
| 1666 |
unsigned int guid_size = GUID_SIZE_STEP; |
| 1667 |
unsigned int nb_guids; |
| 1668 |
// Keep a list of PnP enumerator strings that are found |
| 1669 |
const char *usb_enumerator[8] = { "USB" }; |
| 1670 |
unsigned int nb_usb_enumerators = 1; |
| 1671 |
unsigned int usb_enum_index = 0; |
| 1672 |
// Keep a list of newly allocated devs to unref |
| 1673 |
#define UNREF_SIZE_STEP 16 |
| 1674 |
libusb_device **unref_list, **new_unref_list; |
| 1675 |
unsigned int unref_size = UNREF_SIZE_STEP; |
| 1676 |
unsigned int unref_cur = 0; |
| 1677 |
DWORD hub_port_nr; |
| 1678 |
|
| 1679 |
// PASS 0 : enumerate HUBs |
| 1680 |
// PASS 1 : (re)enumerate master devices that have a DEVice interface |
| 1681 |
// PASS 2 : (re)enumerate HCDs (allow for HCD hotplug) |
| 1682 |
// PASS 3 : (re)enumerate GENeric devices (including driverless) |
| 1683 |
// and list additional device interface GUIDs to explore |
| 1684 |
// PASS 4 : (re)enumerate device interface GUIDs (including HID) |
| 1685 |
// and set the device interfaces |
| 1686 |
// PASS 5+: (re)enumerate additional EXTra GUID devices |
| 1687 |
|
| 1688 |
// Init the GUID table |
| 1689 |
guid_list = malloc(guid_size * sizeof(void *)); |
| 1690 |
if (guid_list == NULL) { |
| 1691 |
usbi_err(ctx, "failed to alloc guid list"); |
| 1692 |
return LIBUSB_ERROR_NO_MEM; |
| 1693 |
} |
| 1694 |
|
| 1695 |
guid_list[HUB_PASS] = &GUID_DEVINTERFACE_USB_HUB; |
| 1696 |
guid_list[DEV_PASS] = &GUID_DEVINTERFACE_USB_DEVICE; |
| 1697 |
guid_list[HCD_PASS] = &GUID_DEVINTERFACE_USB_HOST_CONTROLLER; |
| 1698 |
guid_list[GEN_PASS] = NULL; |
| 1699 |
if (HidD_GetHidGuid != NULL) { |
| 1700 |
HidD_GetHidGuid(&hid_guid); |
| 1701 |
guid_list[HID_PASS] = &hid_guid; |
| 1702 |
} else { |
| 1703 |
guid_list[HID_PASS] = NULL; |
| 1704 |
} |
| 1705 |
nb_guids = EXT_PASS; |
| 1706 |
|
| 1707 |
unref_list = malloc(unref_size * sizeof(void *)); |
| 1708 |
if (unref_list == NULL) { |
| 1709 |
usbi_err(ctx, "failed to alloc unref list"); |
| 1710 |
free((void *)guid_list); |
| 1711 |
return LIBUSB_ERROR_NO_MEM; |
| 1712 |
} |
| 1713 |
|
| 1714 |
dev_info_intf = pSetupDiGetClassDevsA(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); |
| 1715 |
if (dev_info_intf == INVALID_HANDLE_VALUE) { |
| 1716 |
usbi_err(ctx, "failed to obtain device info list: %s", windows_error_str(0)); |
| 1717 |
free(unref_list); |
| 1718 |
free((void *)guid_list); |
| 1719 |
return LIBUSB_ERROR_OTHER; |
| 1720 |
} |
| 1721 |
|
| 1722 |
for (pass = 0; ((pass < nb_guids) && (r == LIBUSB_SUCCESS)); pass++) { |
| 1723 |
pass_type = MIN(pass, EXT_PASS); |
| 1724 |
#if defined(ENABLE_LOGGING) |
| 1725 |
const char * const passname[] = {"HUB", "DEV", "HCD", "GEN", "HID", "EXT"}; |
| 1726 |
usbi_dbg(ctx, "ENUM pass %s %s", passname[pass_type], guid_to_string(guid_list[pass], guid_string)); |
| 1727 |
#endif |
| 1728 |
if ((pass == HID_PASS) && (guid_list[HID_PASS] == NULL)) |
| 1729 |
continue; |
| 1730 |
|
| 1731 |
dev_info = (pass != GEN_PASS) ? &dev_info_intf : &dev_info_enum; |
| 1732 |
|
| 1733 |
for (i = 0; ; i++) { |
| 1734 |
// safe loop: free up any (unprotected) dynamic resource |
| 1735 |
// NB: this is always executed before breaking the loop |
| 1736 |
safe_free(dev_interface_path); |
| 1737 |
priv = parent_priv = NULL; |
| 1738 |
dev = parent_dev = NULL; |
| 1739 |
|
| 1740 |
// Safe loop: end of loop conditions |
| 1741 |
if (r != LIBUSB_SUCCESS) |
| 1742 |
break; |
| 1743 |
|
| 1744 |
if (pass != GEN_PASS) { |
| 1745 |
// Except for GEN, all passes deal with device interfaces |
| 1746 |
r = get_interface_details(ctx, *dev_info, &dev_info_data, guid_list[pass], &_index, &dev_interface_path); |
| 1747 |
if ((r != LIBUSB_SUCCESS) || (dev_interface_path == NULL)) { |
| 1748 |
_index = 0; |
| 1749 |
break; |
| 1750 |
} |
| 1751 |
} else { |
| 1752 |
// Workaround for a Nec/Renesas USB 3.0 driver bug where root hubs are |
| 1753 |
// being listed under the "NUSB3" PnP Symbolic Name rather than "USB". |
| 1754 |
// The Intel USB 3.0 driver behaves similar, but uses "IUSB3" |
| 1755 |
// The Intel Alpine Ridge USB 3.1 driver uses "IARUSB3" |
| 1756 |
for (; usb_enum_index < nb_usb_enumerators; usb_enum_index++) { |
| 1757 |
if (get_devinfo_data(ctx, dev_info, &dev_info_data, usb_enumerator[usb_enum_index], i)) |
| 1758 |
break; |
| 1759 |
i = 0; |
| 1760 |
} |
| 1761 |
if (usb_enum_index == nb_usb_enumerators) |
| 1762 |
break; |
| 1763 |
} |
| 1764 |
|
| 1765 |
// Read the Device ID path |
| 1766 |
if (!pSetupDiGetDeviceInstanceIdA(*dev_info, &dev_info_data, dev_id, sizeof(dev_id), NULL)) { |
| 1767 |
usbi_warn(ctx, "could not read the device instance ID for devInst %lX, skipping", |
| 1768 |
ULONG_CAST(dev_info_data.DevInst)); |
| 1769 |
continue; |
| 1770 |
} |
| 1771 |
|
| 1772 |
usbi_dbg(ctx, "ENUM processing %s", dev_id); |
| 1773 |
|
| 1774 |
// Set API to use or get additional data from generic pass |
| 1775 |
api = USB_API_UNSUPPORTED; |
| 1776 |
sub_api = SUB_API_NOTSET; |
| 1777 |
switch (pass_type) { |
| 1778 |
case HCD_PASS: |
| 1779 |
break; |
| 1780 |
case HUB_PASS: |
| 1781 |
api = USB_API_HUB; |
| 1782 |
// Fetch the PnP enumerator class for this hub |
| 1783 |
// This will allow us to enumerate all classes during the GEN pass |
| 1784 |
if (!pSetupDiGetDeviceRegistryPropertyA(*dev_info, &dev_info_data, SPDRP_ENUMERATOR_NAME, |
| 1785 |
NULL, (PBYTE)enumerator, sizeof(enumerator), NULL)) { |
| 1786 |
usbi_err(ctx, "could not read enumerator string for device '%s': %s", dev_id, windows_error_str(0)); |
| 1787 |
LOOP_BREAK(LIBUSB_ERROR_OTHER); |
| 1788 |
} |
| 1789 |
for (j = 0; j < nb_usb_enumerators; j++) { |
| 1790 |
if (strcmp(usb_enumerator[j], enumerator) == 0) |
| 1791 |
break; |
| 1792 |
} |
| 1793 |
if (j == nb_usb_enumerators) { |
| 1794 |
usbi_dbg(ctx, "found new PnP enumerator string '%s'", enumerator); |
| 1795 |
if (nb_usb_enumerators < ARRAYSIZE(usb_enumerator)) { |
| 1796 |
usb_enumerator[nb_usb_enumerators] = _strdup(enumerator); |
| 1797 |
if (usb_enumerator[nb_usb_enumerators] != NULL) { |
| 1798 |
nb_usb_enumerators++; |
| 1799 |
} else { |
| 1800 |
usbi_err(ctx, "could not allocate enumerator string '%s'", enumerator); |
| 1801 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 1802 |
} |
| 1803 |
} else { |
| 1804 |
usbi_warn(ctx, "too many enumerator strings, some devices may not be accessible"); |
| 1805 |
} |
| 1806 |
} |
| 1807 |
break; |
| 1808 |
case GEN_PASS: |
| 1809 |
// We use the GEN pass to detect driverless devices... |
| 1810 |
if (!pSetupDiGetDeviceRegistryPropertyA(*dev_info, &dev_info_data, SPDRP_DRIVER, |
| 1811 |
NULL, NULL, 0, NULL) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) { |
| 1812 |
usbi_info(ctx, "The following device has no driver: '%s'", dev_id); |
| 1813 |
usbi_info(ctx, "libusb will not be able to access it"); |
| 1814 |
} |
| 1815 |
// ...and to add the additional device interface GUIDs |
| 1816 |
r = get_guid(ctx, dev_id, dev_info, &dev_info_data, 0, &if_guid); |
| 1817 |
if (r == LIBUSB_SUCCESS && if_guid != NULL) { |
| 1818 |
// Check if we've already seen this GUID |
| 1819 |
for (j = EXT_PASS; j < nb_guids; j++) { |
| 1820 |
if (memcmp(guid_list[j], if_guid, sizeof(*if_guid)) == 0) |
| 1821 |
break; |
| 1822 |
} |
| 1823 |
if (j == nb_guids) { |
| 1824 |
usbi_dbg(ctx, "extra GUID: %s", guid_to_string(if_guid, guid_string)); |
| 1825 |
// Extend the guid_list capacity if needed |
| 1826 |
if (nb_guids == guid_size) { |
| 1827 |
new_guid_list = realloc((void *)guid_list, (guid_size + GUID_SIZE_STEP) * sizeof(void *)); |
| 1828 |
if (new_guid_list == NULL) { |
| 1829 |
usbi_err(ctx, "failed to realloc guid list"); |
| 1830 |
free(if_guid); |
| 1831 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 1832 |
} |
| 1833 |
guid_list = new_guid_list; |
| 1834 |
guid_size += GUID_SIZE_STEP; |
| 1835 |
} |
| 1836 |
guid_list[nb_guids++] = if_guid; |
| 1837 |
} else { |
| 1838 |
// Duplicate, ignore |
| 1839 |
free(if_guid); |
| 1840 |
} |
| 1841 |
} else if (r == LIBUSB_ERROR_ACCESS) { |
| 1842 |
r = LIBUSB_SUCCESS; |
| 1843 |
} else if (r == LIBUSB_ERROR_NO_MEM) { |
| 1844 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 1845 |
} else { |
| 1846 |
if (r != LIBUSB_SUCCESS) { |
| 1847 |
usbi_warn(ctx, "unexpected error during getting DeviceInterfaceGUID for '%s'", dev_id); |
| 1848 |
} |
| 1849 |
} |
| 1850 |
break; |
| 1851 |
case HID_PASS: |
| 1852 |
api = USB_API_HID; |
| 1853 |
break; |
| 1854 |
case DEV_PASS: |
| 1855 |
case EXT_PASS: |
| 1856 |
// Get the API type (after checking that the driver installation is OK) |
| 1857 |
if ((!pSetupDiGetDeviceRegistryPropertyA(*dev_info, &dev_info_data, SPDRP_INSTALL_STATE, |
| 1858 |
NULL, (PBYTE)&install_state, sizeof(install_state), &size)) || (size != sizeof(install_state))) { |
| 1859 |
usbi_warn(ctx, "could not detect installation state of driver for '%s': %s", |
| 1860 |
dev_id, windows_error_str(0)); |
| 1861 |
} else if (install_state != 0) { |
| 1862 |
usbi_warn(ctx, "driver for device '%s' is reporting an issue (code: %lu) - skipping", |
| 1863 |
dev_id, ULONG_CAST(install_state)); |
| 1864 |
continue; |
| 1865 |
} |
| 1866 |
get_api_type(dev_info, &dev_info_data, &api, &sub_api); |
| 1867 |
break; |
| 1868 |
default: |
| 1869 |
assert(false); // unreachable since all pass types covered explicitly |
| 1870 |
} |
| 1871 |
|
| 1872 |
// Find parent device (for the passes that need it) |
| 1873 |
if (pass >= GEN_PASS) { |
| 1874 |
parent_dev = get_ancestor(ctx, dev_info_data.DevInst, NULL); |
| 1875 |
if (parent_dev == NULL) { |
| 1876 |
// Root hubs will not have a parent |
| 1877 |
dev = usbi_get_device_by_session_id(ctx, (unsigned long)dev_info_data.DevInst); |
| 1878 |
if (dev != NULL) { |
| 1879 |
priv = usbi_get_device_priv(dev); |
| 1880 |
if (priv->root_hub) |
| 1881 |
goto track_unref; |
| 1882 |
libusb_unref_device(dev); |
| 1883 |
} |
| 1884 |
|
| 1885 |
usbi_dbg(ctx, "unlisted ancestor for '%s' (non USB HID, newly connected, etc.) - ignoring", dev_id); |
| 1886 |
continue; |
| 1887 |
} |
| 1888 |
|
| 1889 |
parent_priv = usbi_get_device_priv(parent_dev); |
| 1890 |
// virtual USB devices are also listed during GEN - don't process these yet |
| 1891 |
if ((pass == GEN_PASS) && (parent_priv->apib->id != USB_API_HUB)) { |
| 1892 |
libusb_unref_device(parent_dev); |
| 1893 |
continue; |
| 1894 |
} |
| 1895 |
} |
| 1896 |
|
| 1897 |
// Create new or match existing device, using the devInst as session id |
| 1898 |
if ((pass <= GEN_PASS) && (pass != HCD_PASS)) { // For subsequent passes, we'll lookup the parent |
| 1899 |
// These are the passes that create "new" devices |
| 1900 |
session_id = (unsigned long)dev_info_data.DevInst; |
| 1901 |
dev = usbi_get_device_by_session_id(ctx, session_id); |
| 1902 |
if (dev == NULL) { |
| 1903 |
alloc_device: |
| 1904 |
usbi_dbg(ctx, "allocating new device for session [%lX]", session_id); |
| 1905 |
dev = usbi_alloc_device(ctx, session_id); |
| 1906 |
if (dev == NULL) |
| 1907 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 1908 |
// Initialize priv (including interface_lock) and the |
| 1909 |
// fields read by concurrent enumeration paths |
| 1910 |
// (dev_id, class_guid) BEFORE making the device visible |
| 1911 |
// via usbi_attach_device or usbi_connect_device. This |
| 1912 |
// closes a use-after-free in the HOTPLUG error path: |
| 1913 |
// if usbi_attach_device runs before _strdup(dev_id) and |
| 1914 |
// the strdup then fails, libusb_unref_device frees the |
| 1915 |
// device while it is still in ctx->usb_devs, leaving a |
| 1916 |
// dangling pointer for the next list traversal. It also |
| 1917 |
// removes the (theoretical, in practice unreachable in |
| 1918 |
// HOTPLUG mode) window where concurrent paths could see |
| 1919 |
// the device with priv->dev_id == NULL. |
| 1920 |
priv = winusb_device_priv_init(dev); |
| 1921 |
priv->dev_id = _strdup(dev_id); |
| 1922 |
priv->class_guid = dev_info_data.ClassGuid; |
| 1923 |
if (priv->dev_id == NULL) { |
| 1924 |
libusb_unref_device(dev); |
| 1925 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 1926 |
} |
| 1927 |
#if defined(LIBUSB_WINDOWS_HOTPLUG) |
| 1928 |
usbi_attach_device(dev); |
| 1929 |
priv->seen_during_scan = true; |
| 1930 |
goto dont_track_unref; |
| 1931 |
#else |
| 1932 |
// Device priv is now initialized; make it |
| 1933 |
// discoverable via usbi_get_device_by_session_id |
| 1934 |
usbi_connect_device(dev); |
| 1935 |
#endif |
| 1936 |
} else { |
| 1937 |
usbi_dbg(ctx, "found existing device for session [%lX]", session_id); |
| 1938 |
|
| 1939 |
priv = usbi_get_device_priv(dev); |
| 1940 |
|
| 1941 |
if (priv->root_hub && dev->bus_number == bus_number + 1) { |
| 1942 |
// The bus number has already been assigned |
| 1943 |
bus_number++; |
| 1944 |
} |
| 1945 |
|
| 1946 |
if (strcmp(priv->dev_id, dev_id) != 0) { |
| 1947 |
usbi_dbg(ctx, "device instance ID for session [%lX] changed", session_id); |
| 1948 |
usbi_detach_device(dev); // usbi_detach_device is equivalent do usbi_disconnect_device but for the firing of LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT |
| 1949 |
libusb_unref_device(dev); |
| 1950 |
goto alloc_device; |
| 1951 |
} |
| 1952 |
if (!IsEqualGUID(&priv->class_guid, &dev_info_data.ClassGuid)) { |
| 1953 |
usbi_dbg(ctx, "device class GUID for session [%lX] changed", session_id); |
| 1954 |
usbi_detach_device(dev); // usbi_detach_device is equivalent do usbi_disconnect_device but for the firing of LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT |
| 1955 |
libusb_unref_device(dev); |
| 1956 |
goto alloc_device; |
| 1957 |
} |
| 1958 |
#if defined(LIBUSB_WINDOWS_HOTPLUG) |
| 1959 |
priv->seen_during_scan = true; |
| 1960 |
#endif |
| 1961 |
} |
| 1962 |
|
| 1963 |
track_unref: |
| 1964 |
// Keep track of devices that need unref |
| 1965 |
if (unref_cur == unref_size) { |
| 1966 |
new_unref_list = realloc(unref_list, (unref_size + UNREF_SIZE_STEP) * sizeof(void *)); |
| 1967 |
if (new_unref_list == NULL) { |
| 1968 |
usbi_err(ctx, "could not realloc list for unref - aborting"); |
| 1969 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 1970 |
} |
| 1971 |
unref_list = new_unref_list; |
| 1972 |
unref_size += UNREF_SIZE_STEP; |
| 1973 |
} |
| 1974 |
unref_list[unref_cur++] = dev; |
| 1975 |
} |
| 1976 |
#if defined(LIBUSB_WINDOWS_HOTPLUG) |
| 1977 |
dont_track_unref: |
| 1978 |
#endif |
| 1979 |
// Setup device |
| 1980 |
switch (pass_type) { |
| 1981 |
case HUB_PASS: |
| 1982 |
case DEV_PASS: |
| 1983 |
// Serialize concurrent enumerations so they don't both see |
| 1984 |
// priv->path == NULL and both run setup, racing on the |
| 1985 |
// writes below. |
| 1986 |
usbi_mutex_lock(&priv->interface_lock); |
| 1987 |
// If the device has already been setup, don't do it again |
| 1988 |
if (priv->path != NULL) { |
| 1989 |
usbi_mutex_unlock(&priv->interface_lock); |
| 1990 |
break; |
| 1991 |
} |
| 1992 |
// Take care of API initialization |
| 1993 |
priv->path = dev_interface_path; |
| 1994 |
dev_interface_path = NULL; |
| 1995 |
priv->apib = &usb_api_backend[api]; |
| 1996 |
priv->sub_api = sub_api; |
| 1997 |
switch (api) { |
| 1998 |
case USB_API_COMPOSITE: |
| 1999 |
break; |
| 2000 |
case USB_API_HUB: |
| 2001 |
parent_dev = get_ancestor(ctx, dev_info_data.DevInst, NULL); |
| 2002 |
if (parent_dev == NULL) { |
| 2003 |
if (!get_dev_port_number(*dev_info, &dev_info_data, &hub_port_nr) || hub_port_nr == 0) { |
| 2004 |
if (bus_number == UINT8_MAX) { |
| 2005 |
usbi_warn(ctx, "program assertion failed - found more than %u buses, skipping the rest", UINT8_MAX); |
| 2006 |
break; |
| 2007 |
} |
| 2008 |
priv->root_hub = true; |
| 2009 |
dev->bus_number = ++bus_number; |
| 2010 |
usbi_dbg(ctx, "assigning Root Hub '%s' bus number %u", dev_id, bus_number); |
| 2011 |
} |
| 2012 |
} else { |
| 2013 |
libusb_unref_device(parent_dev); |
| 2014 |
} |
| 2015 |
break; |
| 2016 |
case USB_API_HID: |
| 2017 |
priv->hid = calloc(1, sizeof(struct hid_device_priv)); |
| 2018 |
if (priv->hid == NULL) { |
| 2019 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2020 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 2021 |
} |
| 2022 |
break; |
| 2023 |
default: |
| 2024 |
// For other devices, the first interface is the same as the device |
| 2025 |
priv->usb_interface[0].path = _strdup(priv->path); |
| 2026 |
if (priv->usb_interface[0].path == NULL) { |
| 2027 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2028 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 2029 |
} |
| 2030 |
// The following is needed if we want API calls to work for both simple |
| 2031 |
// and composite devices. |
| 2032 |
for (j = 0; j < USB_MAXINTERFACES; j++) |
| 2033 |
priv->usb_interface[j].apib = &usb_api_backend[api]; |
| 2034 |
break; |
| 2035 |
} |
| 2036 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2037 |
break; |
| 2038 |
case HCD_PASS: |
| 2039 |
r = enumerate_hcd_root_hub(ctx, dev_id, dev_info_data.DevInst); |
| 2040 |
break; |
| 2041 |
case GEN_PASS: |
| 2042 |
port_nr = 0; |
| 2043 |
// Serialize concurrent enumerations so they don't both see |
| 2044 |
// priv->initialized == false and both run init_device, |
| 2045 |
// which allocates priv->config_descriptor[] and other |
| 2046 |
// per-device state. Without this, the loser of the race |
| 2047 |
// overwrites the winner's allocations, leaking them. |
| 2048 |
usbi_mutex_lock(&priv->interface_lock); |
| 2049 |
if (priv->initialized) { |
| 2050 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2051 |
libusb_unref_device(parent_dev); |
| 2052 |
r = LIBUSB_SUCCESS; |
| 2053 |
} else { |
| 2054 |
if (!get_dev_port_number(*dev_info, &dev_info_data, &port_nr)) |
| 2055 |
usbi_warn(ctx, "could not retrieve port number for device '%s': %s", dev_id, windows_error_str(0)); |
| 2056 |
r = init_device(dev, parent_dev, (uint8_t)port_nr, dev_info_data.DevInst); |
| 2057 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2058 |
} |
| 2059 |
if (r == LIBUSB_SUCCESS) { |
| 2060 |
// Append device to the list of discovered devices |
| 2061 |
if (_discdevs) { |
| 2062 |
discdevs = discovered_devs_append(*_discdevs, dev); |
| 2063 |
if (!discdevs) |
| 2064 |
LOOP_BREAK(LIBUSB_ERROR_NO_MEM); |
| 2065 |
|
| 2066 |
*_discdevs = discdevs; |
| 2067 |
} |
| 2068 |
} else { |
| 2069 |
// Failed to initialize a single device doesn't stop us from enumerating all other devices, |
| 2070 |
// but we skip it (don't add to list of discovered devices) |
| 2071 |
usbi_warn(ctx, "failed to initialize device '%s'", priv->dev_id); |
| 2072 |
r = LIBUSB_SUCCESS; |
| 2073 |
#if defined(LIBUSB_WINDOWS_HOTPLUG) |
| 2074 |
usbi_detach_device(dev); |
| 2075 |
libusb_unref_device(dev); |
| 2076 |
#endif |
| 2077 |
} |
| 2078 |
break; |
| 2079 |
case HID_PASS: |
| 2080 |
case EXT_PASS: |
| 2081 |
if (parent_priv->apib->id == USB_API_HID || parent_priv->apib->id == USB_API_COMPOSITE) { |
| 2082 |
if (parent_priv->apib->id == USB_API_HID) { |
| 2083 |
usbi_dbg(ctx, "setting HID interface for [%lX]:", parent_dev->session_data); |
| 2084 |
r = set_hid_interface(ctx, parent_dev, dev_interface_path); |
| 2085 |
} else { |
| 2086 |
usbi_dbg(ctx, "setting composite interface for [%lX]:", parent_dev->session_data); |
| 2087 |
r = set_composite_interface(ctx, parent_dev, dev_interface_path, dev_id, api, sub_api); |
| 2088 |
} |
| 2089 |
switch (r) { |
| 2090 |
case LIBUSB_SUCCESS: |
| 2091 |
dev_interface_path = NULL; |
| 2092 |
break; |
| 2093 |
case LIBUSB_ERROR_ACCESS: |
| 2094 |
// interface has already been set => make sure dev_interface_path is freed then |
| 2095 |
r = LIBUSB_SUCCESS; |
| 2096 |
break; |
| 2097 |
default: |
| 2098 |
LOOP_BREAK(r); |
| 2099 |
break; |
| 2100 |
} |
| 2101 |
} |
| 2102 |
libusb_unref_device(parent_dev); |
| 2103 |
break; |
| 2104 |
default: |
| 2105 |
assert(false); // unreachable since all pass types covered explicitly |
| 2106 |
} |
| 2107 |
} |
| 2108 |
} |
| 2109 |
|
| 2110 |
pSetupDiDestroyDeviceInfoList(dev_info_intf); |
| 2111 |
|
| 2112 |
// Free any additional GUIDs |
| 2113 |
for (pass = EXT_PASS; pass < nb_guids; pass++) |
| 2114 |
free((void *)guid_list[pass]); |
| 2115 |
free((void *)guid_list); |
| 2116 |
|
| 2117 |
// Free any PnP enumerator strings |
| 2118 |
for (i = 1; i < nb_usb_enumerators; i++) |
| 2119 |
free((void *)usb_enumerator[i]); |
| 2120 |
|
| 2121 |
// Unref newly allocated devs |
| 2122 |
for (i = 0; i < unref_cur; i++) |
| 2123 |
libusb_unref_device(unref_list[i]); |
| 2124 |
free(unref_list); |
| 2125 |
|
| 2126 |
return r; |
| 2127 |
} |
| 2128 |
|
| 2129 |
#ifdef _MSC_VER |
| 2130 |
#pragma pack(push, 1) |
| 2131 |
#endif |
| 2132 |
|
| 2133 |
struct string_descriptor_s { |
| 2134 |
UCHAR bLength; |
| 2135 |
UCHAR bDescriptorType; |
| 2136 |
uint8_t bString[252]; // UTF-16LE, must be even length |
| 2137 |
} LIBUSB_PACKED; |
| 2138 |
|
| 2139 |
#ifdef _MSC_VER |
| 2140 |
#pragma pack(pop) |
| 2141 |
#endif |
| 2142 |
|
| 2143 |
struct string_descriptor_req_s { |
| 2144 |
USB_DESCRIPTOR_REQUEST req; |
| 2145 |
struct string_descriptor_s desc; |
| 2146 |
}; |
| 2147 |
|
| 2148 |
/* |
| 2149 |
* \brief Convert a UTF-16 little endian string to a UTF-8 string. |
| 2150 |
* |
| 2151 |
* \param src The input UTF-16 little endian string. |
| 2152 |
* \param src_length The length of the src buffer in bytes which |
| 2153 |
* may or may not include the null terminator. |
| 2154 |
* \param dst The output UTF-8 string. |
| 2155 |
* \param dst_length The length of dst buffer in bytes, which should |
| 2156 |
* be large enough to fit the string including null terminator. |
| 2157 |
* \return The total number of bytes in the UTF-8 string, including |
| 2158 |
* the null terminator. |
| 2159 |
* If this is greater than dst_length, then dst is truncated. |
| 2160 |
* \note From MiniBitty RTOS & framework, currently closed source. |
| 2161 |
*/ |
| 2162 |
static int usbi_utf16le_to_utf8(uint8_t const *src, int src_length, char *dst, int dst_length) { |
| 2163 |
int count = 0; |
| 2164 |
uint32_t codepoint = 0; |
| 2165 |
uint32_t next_codepoint = 0; |
| 2166 |
bool is_surrogate_pair = false; |
| 2167 |
bool overflow = false; |
| 2168 |
|
| 2169 |
if (NULL == dst) { |
| 2170 |
dst_length = 0; |
| 2171 |
} |
| 2172 |
if (dst_length > 0) { |
| 2173 |
dst[0] = 0; // empty string by default |
| 2174 |
} |
| 2175 |
if (src_length & 1) { |
| 2176 |
--src_length; // odd length not valid, but proceed. |
| 2177 |
} |
| 2178 |
if ((src == NULL) || (src_length <= 0)) { |
| 2179 |
return 1; |
| 2180 |
} |
| 2181 |
|
| 2182 |
for (int k = 0; k < src_length; k += 2) { |
| 2183 |
next_codepoint = *src++; |
| 2184 |
next_codepoint |= ((uint16_t)*src++) << 8; |
| 2185 |
if (is_surrogate_pair) { |
| 2186 |
is_surrogate_pair = false; |
| 2187 |
if ((next_codepoint >= 0xDC00) && (next_codepoint <= 0xDFFF)) { |
| 2188 |
// correct encoding |
| 2189 |
codepoint |= (next_codepoint & 0x3ff); |
| 2190 |
} else { |
| 2191 |
// incorrect encoding, skip previous codepoint |
| 2192 |
codepoint = next_codepoint; |
| 2193 |
} |
| 2194 |
} else if ((next_codepoint >= 0xD800) && (next_codepoint <= 0xDBFF)) { |
| 2195 |
is_surrogate_pair = true; |
| 2196 |
codepoint = 0x010000 + ((next_codepoint & 0x3ff) << 10); |
| 2197 |
continue; |
| 2198 |
} else { |
| 2199 |
codepoint = next_codepoint; |
| 2200 |
} |
| 2201 |
|
| 2202 |
if (codepoint <= 0x7f) { |
| 2203 |
if (count < dst_length) { |
| 2204 |
dst[count] = (char)codepoint; |
| 2205 |
} else { |
| 2206 |
overflow = true; |
| 2207 |
} |
| 2208 |
count += 1; |
| 2209 |
if (0 == codepoint) { |
| 2210 |
--count; // add null-terminator below |
| 2211 |
break; |
| 2212 |
} |
| 2213 |
} else if (codepoint <= 0x7ff) { |
| 2214 |
if ((count + 1) < dst_length) { |
| 2215 |
dst[count] = (char)(0xC0 | (codepoint >> 6)); |
| 2216 |
dst[count + 1] = (char)(0x80 | (codepoint & 0x3F)); |
| 2217 |
} else { |
| 2218 |
overflow = true; |
| 2219 |
} |
| 2220 |
count += 2; |
| 2221 |
} else if (codepoint <= 0xffff) { |
| 2222 |
if ((count + 2) < dst_length) { |
| 2223 |
dst[count] = (char)(0xE0 | (codepoint >> 12)); |
| 2224 |
dst[count + 1] = (char)(0x80 | ((codepoint >> 6) & 0x3F)); |
| 2225 |
dst[count + 2] = (char)(0x80 | (codepoint & 0x3F)); |
| 2226 |
} else { |
| 2227 |
overflow = true; |
| 2228 |
} |
| 2229 |
count += 3; |
| 2230 |
} else if (codepoint <= 0x10ffff) { |
| 2231 |
if ((count + 3) < dst_length) { |
| 2232 |
dst[count] = (char)(0xF0 | ((codepoint >> 18) & 7)); |
| 2233 |
dst[count + 1] = (char)(0x80 | ((codepoint >> 12) & 0x3F)); |
| 2234 |
dst[count + 2] = (char)(0x80 | ((codepoint >> 6) & 0x3F)); |
| 2235 |
dst[count + 3] = (char)(0x80 | (codepoint & 0x3F)); |
| 2236 |
} else { |
| 2237 |
overflow = true; |
| 2238 |
} |
| 2239 |
count += 4; |
| 2240 |
} else { |
| 2241 |
// invalid codepoint, skip |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
if (count < dst_length) { |
| 2246 |
dst[count] = 0; |
| 2247 |
} |
| 2248 |
++count; // include null terminator |
| 2249 |
|
| 2250 |
if (overflow && dst_length) { |
| 2251 |
// truncate respecting UTF-8 character boundaries |
| 2252 |
int idx = dst_length - 1; |
| 2253 |
while (idx && (0x80 == (dst[idx] & 0xC0))) { // utf-8 continuation byte |
| 2254 |
--idx; |
| 2255 |
} |
| 2256 |
dst[idx] = 0; |
| 2257 |
} |
| 2258 |
|
| 2259 |
return count; |
| 2260 |
} |
| 2261 |
|
| 2262 |
/* |
| 2263 |
* Backend implementation for libusb_get_device_string(). |
| 2264 |
* |
| 2265 |
* Windows makes getting the common device strings |
| 2266 |
* very difficult. DEVPKEY_Device_* does not have SerialNumber, |
| 2267 |
* and it reports the driver manufacturer, not the device manufacturer. |
| 2268 |
* |
| 2269 |
* We could parse the serial number from the DEVICE_ID string: |
| 2270 |
* https://learn.microsoft.com/en-us/windows-hardware/drivers/install/device-instance-ids |
| 2271 |
* https://learn.microsoft.com/en-us/windows-hardware/drivers/install/standard-usb-identifiers |
| 2272 |
* |
| 2273 |
* However, using the dev_id for getting the serial number is |
| 2274 |
* definitely not recommended. |
| 2275 |
* |
| 2276 |
* The following implementation uses an IOCTL |
| 2277 |
* to the parent USB hub to perform the USB control request for the |
| 2278 |
* string descriptor without opening the USB device. |
| 2279 |
* While we would rather not invoke USB IO, we currently lack a |
| 2280 |
* better option. |
| 2281 |
*/ |
| 2282 |
static int winusb_get_device_string(libusb_device *dev, |
| 2283 |
enum libusb_device_string_type string_type, char *data, int length) |
| 2284 |
{ |
| 2285 |
struct winusb_device_priv* priv = usbi_get_device_priv(dev); |
| 2286 |
struct libusb_context* ctx = DEVICE_CTX(dev); |
| 2287 |
DWORD size; |
| 2288 |
DWORD ret_size; |
| 2289 |
struct string_descriptor_req_s sd; |
| 2290 |
|
| 2291 |
if ((NULL != data) && (length > 0)) { |
| 2292 |
*data = 0; |
| 2293 |
} |
| 2294 |
if (NULL == dev->parent_dev) { |
| 2295 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2296 |
} |
| 2297 |
|
| 2298 |
uint8_t string_descriptor_idx; |
| 2299 |
switch (string_type) { |
| 2300 |
case LIBUSB_DEVICE_STRING_MANUFACTURER: string_descriptor_idx = dev->device_descriptor.iManufacturer; break; |
| 2301 |
case LIBUSB_DEVICE_STRING_PRODUCT: string_descriptor_idx = dev->device_descriptor.iProduct; break; |
| 2302 |
case LIBUSB_DEVICE_STRING_SERIAL_NUMBER: string_descriptor_idx = dev->device_descriptor.iSerialNumber; break; |
| 2303 |
default: return LIBUSB_ERROR_INVALID_PARAM; |
| 2304 |
} |
| 2305 |
|
| 2306 |
if (0 == string_descriptor_idx) { |
| 2307 |
return LIBUSB_ERROR_NOT_FOUND; |
| 2308 |
} |
| 2309 |
|
| 2310 |
struct winusb_device_priv* hub_priv = usbi_get_device_priv(dev->parent_dev); |
| 2311 |
HANDLE hub_handle = CreateFileA(hub_priv->path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
| 2312 |
if (hub_handle == INVALID_HANDLE_VALUE) { |
| 2313 |
usbi_warn(ctx, "could not open hub %s: %s", hub_priv->path, windows_error_str(0)); |
| 2314 |
return LIBUSB_ERROR_ACCESS; |
| 2315 |
} |
| 2316 |
|
| 2317 |
// Fetch and cache the device's primary USB language ID. |
| 2318 |
// String descriptor 0 with wIndex=0 returns the supported language table |
| 2319 |
// (USB 2.0 spec section 9.6.7). We use the first (primary) LANGID for |
| 2320 |
// all subsequent string descriptor requests. |
| 2321 |
if (0 == priv->langid) { |
| 2322 |
size = sizeof(sd); |
| 2323 |
memset(&sd, 0, size); |
| 2324 |
sd.req.ConnectionIndex = (ULONG)dev->port_number; |
| 2325 |
sd.req.SetupPacket.bmRequest = LIBUSB_ENDPOINT_IN; |
| 2326 |
sd.req.SetupPacket.bRequest = LIBUSB_REQUEST_GET_DESCRIPTOR; |
| 2327 |
sd.req.SetupPacket.wValue = (LIBUSB_DT_STRING << 8) | 0; |
| 2328 |
sd.req.SetupPacket.wIndex = 0; |
| 2329 |
sd.req.SetupPacket.wLength = (USHORT)sizeof(sd.desc); |
| 2330 |
|
| 2331 |
if (!DeviceIoControl(hub_handle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &sd, size, |
| 2332 |
&sd, size, &ret_size, NULL)) { |
| 2333 |
usbi_warn(ctx, "could not retrieve language IDs for '%s': %s", |
| 2334 |
priv->dev_id, windows_error_str(0)); |
| 2335 |
CloseHandle(hub_handle); |
| 2336 |
return LIBUSB_ERROR_IO; |
| 2337 |
} |
| 2338 |
|
| 2339 |
if (sd.desc.bDescriptorType != LIBUSB_DT_STRING || sd.desc.bLength < 4) { |
| 2340 |
usbi_warn(ctx, "invalid language ID descriptor for '%s'", priv->dev_id); |
| 2341 |
CloseHandle(hub_handle); |
| 2342 |
return LIBUSB_ERROR_IO; |
| 2343 |
} |
| 2344 |
|
| 2345 |
priv->langid = (uint16_t)sd.desc.bString[0] | ((uint16_t)sd.desc.bString[1] << 8); |
| 2346 |
usbi_dbg(ctx, "cached language ID 0x%04x for '%s'", priv->langid, priv->dev_id); |
| 2347 |
} |
| 2348 |
|
| 2349 |
size = sizeof(sd); |
| 2350 |
memset(&sd, 0, size); |
| 2351 |
sd.req.ConnectionIndex = (ULONG)dev->port_number; |
| 2352 |
sd.req.SetupPacket.bmRequest = LIBUSB_ENDPOINT_IN; |
| 2353 |
sd.req.SetupPacket.bRequest = LIBUSB_REQUEST_GET_DESCRIPTOR; |
| 2354 |
sd.req.SetupPacket.wValue = (LIBUSB_DT_STRING << 8) | string_descriptor_idx; |
| 2355 |
sd.req.SetupPacket.wIndex = priv->langid; |
| 2356 |
sd.req.SetupPacket.wLength = (USHORT)sizeof(sd.desc); |
| 2357 |
|
| 2358 |
BOOL rv = DeviceIoControl(hub_handle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &sd, size, |
| 2359 |
&sd, size, &ret_size, NULL); |
| 2360 |
CloseHandle(hub_handle); |
| 2361 |
if (!rv) { |
| 2362 |
usbi_err(ctx, "could not access string descriptor %u for '%s': %s", string_descriptor_idx, |
| 2363 |
priv->dev_id, windows_error_str(0)); |
| 2364 |
return LIBUSB_ERROR_IO; |
| 2365 |
} |
| 2366 |
|
| 2367 |
if (sd.desc.bDescriptorType != LIBUSB_DT_STRING) { |
| 2368 |
usbi_err(ctx, "descriptor %u not a string descriptor for '%s'", string_descriptor_idx, priv->dev_id); |
| 2369 |
return LIBUSB_ERROR_IO; |
| 2370 |
} |
| 2371 |
|
| 2372 |
return usbi_utf16le_to_utf8(sd.desc.bString, (int) (sd.desc.bLength - 2), data, length); |
| 2373 |
} |
| 2374 |
|
| 2375 |
static int winusb_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, void *buffer, size_t len) |
| 2376 |
{ |
| 2377 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 2378 |
PUSB_CONFIGURATION_DESCRIPTOR config_header; |
| 2379 |
|
| 2380 |
if ((priv->config_descriptor == NULL) || (priv->config_descriptor[config_index] == NULL)) |
| 2381 |
return LIBUSB_ERROR_NOT_FOUND; |
| 2382 |
|
| 2383 |
config_header = priv->config_descriptor[config_index]; |
| 2384 |
|
| 2385 |
len = MIN(len, config_header->wTotalLength); |
| 2386 |
memcpy(buffer, config_header, len); |
| 2387 |
return (int)len; |
| 2388 |
} |
| 2389 |
|
| 2390 |
static int winusb_get_config_descriptor_by_value(struct libusb_device *dev, uint8_t bConfigurationValue, |
| 2391 |
void **buffer) |
| 2392 |
{ |
| 2393 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 2394 |
PUSB_CONFIGURATION_DESCRIPTOR config_header; |
| 2395 |
uint8_t index; |
| 2396 |
|
| 2397 |
if (priv->config_descriptor == NULL) |
| 2398 |
return LIBUSB_ERROR_NOT_FOUND; |
| 2399 |
|
| 2400 |
for (index = 0; index < dev->device_descriptor.bNumConfigurations; index++) { |
| 2401 |
config_header = priv->config_descriptor[index]; |
| 2402 |
if (config_header == NULL) |
| 2403 |
continue; |
| 2404 |
if (config_header->bConfigurationValue == bConfigurationValue) { |
| 2405 |
*buffer = config_header; |
| 2406 |
return (int)config_header->wTotalLength; |
| 2407 |
} |
| 2408 |
} |
| 2409 |
|
| 2410 |
return LIBUSB_ERROR_NOT_FOUND; |
| 2411 |
} |
| 2412 |
|
| 2413 |
/* |
| 2414 |
* return the cached copy of the active config descriptor |
| 2415 |
*/ |
| 2416 |
static int winusb_get_active_config_descriptor(struct libusb_device *dev, void *buffer, size_t len) |
| 2417 |
{ |
| 2418 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 2419 |
void *config_desc; |
| 2420 |
int r; |
| 2421 |
|
| 2422 |
if (priv->active_config == 0) |
| 2423 |
return LIBUSB_ERROR_NOT_FOUND; |
| 2424 |
|
| 2425 |
r = winusb_get_config_descriptor_by_value(dev, priv->active_config, &config_desc); |
| 2426 |
if (r < 0) |
| 2427 |
return r; |
| 2428 |
|
| 2429 |
len = MIN(len, (size_t)r); |
| 2430 |
memcpy(buffer, config_desc, len); |
| 2431 |
return (int)len; |
| 2432 |
} |
| 2433 |
|
| 2434 |
static int winusb_open(struct libusb_device_handle *dev_handle) |
| 2435 |
{ |
| 2436 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2437 |
|
| 2438 |
CHECK_SUPPORTED_API(priv->apib, open); |
| 2439 |
|
| 2440 |
return priv->apib->open(SUB_API_NOTSET, dev_handle); |
| 2441 |
} |
| 2442 |
|
| 2443 |
static void winusb_close(struct libusb_device_handle *dev_handle) |
| 2444 |
{ |
| 2445 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2446 |
|
| 2447 |
if (priv->apib->close) |
| 2448 |
priv->apib->close(SUB_API_NOTSET, dev_handle); |
| 2449 |
} |
| 2450 |
|
| 2451 |
static int winusb_get_configuration(struct libusb_device_handle *dev_handle, uint8_t *config) |
| 2452 |
{ |
| 2453 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2454 |
|
| 2455 |
*config = priv->active_config; |
| 2456 |
return LIBUSB_SUCCESS; |
| 2457 |
} |
| 2458 |
|
| 2459 |
/* |
| 2460 |
* from http://msdn.microsoft.com/en-us/library/ms793522.aspx: "The port driver |
| 2461 |
* does not currently expose a service that allows higher-level drivers to set |
| 2462 |
* the configuration." |
| 2463 |
*/ |
| 2464 |
static int winusb_set_configuration(struct libusb_device_handle *dev_handle, uint8_t config) |
| 2465 |
{ |
| 2466 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2467 |
int r = LIBUSB_SUCCESS; |
| 2468 |
|
| 2469 |
r = libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_OUT | |
| 2470 |
LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE, |
| 2471 |
LIBUSB_REQUEST_SET_CONFIGURATION, config, |
| 2472 |
0, NULL, 0, 1000); |
| 2473 |
|
| 2474 |
if (r == LIBUSB_SUCCESS) |
| 2475 |
priv->active_config = config; |
| 2476 |
|
| 2477 |
return r; |
| 2478 |
} |
| 2479 |
|
| 2480 |
static int winusb_claim_interface(struct libusb_device_handle *dev_handle, uint8_t iface) |
| 2481 |
{ |
| 2482 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2483 |
int r; |
| 2484 |
|
| 2485 |
CHECK_SUPPORTED_API(priv->apib, claim_interface); |
| 2486 |
|
| 2487 |
usbi_mutex_lock(&priv->interface_lock); |
| 2488 |
|
| 2489 |
safe_free(priv->usb_interface[iface].endpoint); |
| 2490 |
priv->usb_interface[iface].nb_endpoints = 0; |
| 2491 |
|
| 2492 |
r = priv->apib->claim_interface(SUB_API_NOTSET, dev_handle, iface); |
| 2493 |
|
| 2494 |
if (r == LIBUSB_SUCCESS) |
| 2495 |
r = windows_assign_endpoints(dev_handle, iface, 0); |
| 2496 |
|
| 2497 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2498 |
|
| 2499 |
return r; |
| 2500 |
} |
| 2501 |
|
| 2502 |
static int winusb_set_interface_altsetting(struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting) |
| 2503 |
{ |
| 2504 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2505 |
int r; |
| 2506 |
|
| 2507 |
CHECK_SUPPORTED_API(priv->apib, set_interface_altsetting); |
| 2508 |
|
| 2509 |
usbi_mutex_lock(&priv->interface_lock); |
| 2510 |
|
| 2511 |
safe_free(priv->usb_interface[iface].endpoint); |
| 2512 |
priv->usb_interface[iface].nb_endpoints = 0; |
| 2513 |
|
| 2514 |
r = priv->apib->set_interface_altsetting(SUB_API_NOTSET, dev_handle, iface, altsetting); |
| 2515 |
|
| 2516 |
if (r == LIBUSB_SUCCESS) |
| 2517 |
r = windows_assign_endpoints(dev_handle, iface, altsetting); |
| 2518 |
|
| 2519 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2520 |
|
| 2521 |
return r; |
| 2522 |
} |
| 2523 |
|
| 2524 |
static int winusb_release_interface(struct libusb_device_handle *dev_handle, uint8_t iface) |
| 2525 |
{ |
| 2526 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2527 |
int r; |
| 2528 |
|
| 2529 |
CHECK_SUPPORTED_API(priv->apib, release_interface); |
| 2530 |
|
| 2531 |
// The backend release paths (composite_release_interface, |
| 2532 |
// hid_release_interface) read priv->usb_interface[iface].apib / |
| 2533 |
// .path, which are written under interface_lock by claim/altsetting |
| 2534 |
// and by the enumeration setup paths. Take the lock to make those |
| 2535 |
// reads safe against concurrent writers. |
| 2536 |
usbi_mutex_lock(&priv->interface_lock); |
| 2537 |
r = priv->apib->release_interface(SUB_API_NOTSET, dev_handle, iface); |
| 2538 |
usbi_mutex_unlock(&priv->interface_lock); |
| 2539 |
|
| 2540 |
return r; |
| 2541 |
} |
| 2542 |
|
| 2543 |
static int winusb_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint) |
| 2544 |
{ |
| 2545 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2546 |
|
| 2547 |
CHECK_SUPPORTED_API(priv->apib, clear_halt); |
| 2548 |
|
| 2549 |
return priv->apib->clear_halt(SUB_API_NOTSET, dev_handle, endpoint); |
| 2550 |
} |
| 2551 |
|
| 2552 |
static int winusb_reset_device(struct libusb_device_handle *dev_handle) |
| 2553 |
{ |
| 2554 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2555 |
|
| 2556 |
CHECK_SUPPORTED_API(priv->apib, reset_device); |
| 2557 |
|
| 2558 |
return priv->apib->reset_device(SUB_API_NOTSET, dev_handle); |
| 2559 |
} |
| 2560 |
|
| 2561 |
static void winusb_destroy_device(struct libusb_device *dev) |
| 2562 |
{ |
| 2563 |
winusb_device_priv_release(dev); |
| 2564 |
} |
| 2565 |
|
| 2566 |
static void winusb_clear_transfer_priv(struct usbi_transfer *itransfer) |
| 2567 |
{ |
| 2568 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 2569 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 2570 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 2571 |
int sub_api = priv->sub_api; |
| 2572 |
|
| 2573 |
safe_free(transfer_priv->hid_buffer); |
| 2574 |
|
| 2575 |
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS && sub_api == SUB_API_WINUSB) { |
| 2576 |
if (transfer_priv->isoch_buffer_handle != NULL) { |
| 2577 |
if (WinUSBX[sub_api].UnregisterIsochBuffer(transfer_priv->isoch_buffer_handle)) { |
| 2578 |
transfer_priv->isoch_buffer_handle = NULL; |
| 2579 |
} else { |
| 2580 |
usbi_warn(TRANSFER_CTX(transfer), "failed to unregister WinUSB isoch buffer: %s", windows_error_str(0)); |
| 2581 |
} |
| 2582 |
} |
| 2583 |
} |
| 2584 |
|
| 2585 |
safe_free(transfer_priv->iso_context); |
| 2586 |
|
| 2587 |
// When auto claim is in use, attempt to release the auto-claimed interface |
| 2588 |
auto_release(itransfer); |
| 2589 |
} |
| 2590 |
|
| 2591 |
static int winusb_submit_transfer(struct usbi_transfer *itransfer) |
| 2592 |
{ |
| 2593 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 2594 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 2595 |
int (*transfer_fn)(int, struct usbi_transfer *); |
| 2596 |
|
| 2597 |
switch (transfer->type) { |
| 2598 |
case LIBUSB_TRANSFER_TYPE_CONTROL: |
| 2599 |
transfer_fn = priv->apib->submit_control_transfer; |
| 2600 |
break; |
| 2601 |
case LIBUSB_TRANSFER_TYPE_BULK: |
| 2602 |
case LIBUSB_TRANSFER_TYPE_INTERRUPT: |
| 2603 |
transfer_fn = priv->apib->submit_bulk_transfer; |
| 2604 |
break; |
| 2605 |
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: |
| 2606 |
transfer_fn = priv->apib->submit_iso_transfer; |
| 2607 |
break; |
| 2608 |
default: |
| 2609 |
// Should not get here since windows_submit_transfer() validates |
| 2610 |
// the transfer->type field |
| 2611 |
usbi_err(TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type); |
| 2612 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 2613 |
} |
| 2614 |
|
| 2615 |
if (transfer_fn == NULL) { |
| 2616 |
usbi_warn(TRANSFER_CTX(transfer), |
| 2617 |
"unsupported transfer type %d (unrecognized device driver)", |
| 2618 |
transfer->type); |
| 2619 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2620 |
} |
| 2621 |
|
| 2622 |
return transfer_fn(SUB_API_NOTSET, itransfer); |
| 2623 |
} |
| 2624 |
|
| 2625 |
static int winusb_cancel_transfer(struct usbi_transfer *itransfer) |
| 2626 |
{ |
| 2627 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 2628 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 2629 |
|
| 2630 |
CHECK_SUPPORTED_API(priv->apib, cancel_transfer); |
| 2631 |
|
| 2632 |
return priv->apib->cancel_transfer(SUB_API_NOTSET, itransfer); |
| 2633 |
} |
| 2634 |
|
| 2635 |
static enum libusb_transfer_status winusb_copy_transfer_data(struct usbi_transfer *itransfer, DWORD length) |
| 2636 |
{ |
| 2637 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 2638 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 2639 |
|
| 2640 |
if (priv->apib->copy_transfer_data == NULL) { |
| 2641 |
usbi_err(TRANSFER_CTX(transfer), "program assertion failed - no function to copy transfer data"); |
| 2642 |
return LIBUSB_TRANSFER_ERROR; |
| 2643 |
} |
| 2644 |
|
| 2645 |
return priv->apib->copy_transfer_data(SUB_API_NOTSET, itransfer, length); |
| 2646 |
} |
| 2647 |
|
| 2648 |
static int winusb_endpoint_supports_raw_io(struct libusb_device_handle* dev_handle, uint8_t endpoint) |
| 2649 |
{ |
| 2650 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2651 |
|
| 2652 |
if (priv->apib->endpoint_supports_raw_io == NULL) |
| 2653 |
{ |
| 2654 |
usbi_dbg(HANDLE_CTX(dev_handle), "device driver does not support RAW_IO query -> unsupported."); |
| 2655 |
return 0; |
| 2656 |
} |
| 2657 |
|
| 2658 |
return priv->apib->endpoint_supports_raw_io(SUB_API_NOTSET, dev_handle, endpoint); |
| 2659 |
} |
| 2660 |
|
| 2661 |
static int winusb_endpoint_set_raw_io(struct libusb_device_handle* dev_handle, uint8_t endpoint, int enable) |
| 2662 |
{ |
| 2663 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2664 |
|
| 2665 |
if (priv->apib->endpoint_set_raw_io == NULL) |
| 2666 |
{ |
| 2667 |
usbi_err(HANDLE_CTX(dev_handle), "device driver does not support setting RAW_IO."); |
| 2668 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2669 |
} |
| 2670 |
|
| 2671 |
return priv->apib->endpoint_set_raw_io(SUB_API_NOTSET, dev_handle, endpoint, enable); |
| 2672 |
} |
| 2673 |
|
| 2674 |
static int winusb_get_max_raw_io_transfer_size( |
| 2675 |
struct libusb_device_handle *dev_handle, |
| 2676 |
uint8_t endpoint) |
| 2677 |
{ |
| 2678 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 2679 |
|
| 2680 |
if (priv->apib->get_max_raw_io_transfer_size == NULL) |
| 2681 |
{ |
| 2682 |
usbi_err(HANDLE_CTX(dev_handle), "device driver does not support RAW_IO max size query."); |
| 2683 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2684 |
} |
| 2685 |
|
| 2686 |
return priv->apib->get_max_raw_io_transfer_size(SUB_API_NOTSET, dev_handle, endpoint); |
| 2687 |
} |
| 2688 |
|
| 2689 |
// NB: MSVC6 does not support named initializers. |
| 2690 |
const struct windows_backend winusb_backend = { |
| 2691 |
winusb_init, |
| 2692 |
winusb_exit, |
| 2693 |
winusb_get_device_list, |
| 2694 |
winusb_get_device_string, |
| 2695 |
winusb_open, |
| 2696 |
winusb_close, |
| 2697 |
winusb_get_active_config_descriptor, |
| 2698 |
winusb_get_config_descriptor, |
| 2699 |
winusb_get_config_descriptor_by_value, |
| 2700 |
winusb_get_configuration, |
| 2701 |
winusb_set_configuration, |
| 2702 |
winusb_claim_interface, |
| 2703 |
winusb_release_interface, |
| 2704 |
winusb_set_interface_altsetting, |
| 2705 |
winusb_clear_halt, |
| 2706 |
winusb_reset_device, |
| 2707 |
winusb_destroy_device, |
| 2708 |
winusb_submit_transfer, |
| 2709 |
winusb_cancel_transfer, |
| 2710 |
winusb_clear_transfer_priv, |
| 2711 |
winusb_copy_transfer_data, |
| 2712 |
winusb_endpoint_supports_raw_io, |
| 2713 |
winusb_endpoint_set_raw_io, |
| 2714 |
winusb_get_max_raw_io_transfer_size, |
| 2715 |
}; |
| 2716 |
|
| 2717 |
/* |
| 2718 |
* USB API backends |
| 2719 |
*/ |
| 2720 |
|
| 2721 |
static const char * const composite_driver_names[] = { |
| 2722 |
"USBCCGP", // (Windows built-in) USB Composite Device |
| 2723 |
"dg_ssudbus" // SAMSUNG Mobile USB Composite Device |
| 2724 |
}; |
| 2725 |
static const char * const winusbx_driver_names[] = {"libusbK", "libusb0", "WinUSB"}; |
| 2726 |
static const char * const hid_driver_names[] = {"HIDUSB", "MOUHID", "KBDHID"}; |
| 2727 |
const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = { |
| 2728 |
{ |
| 2729 |
USB_API_UNSUPPORTED, |
| 2730 |
"Unsupported API", |
| 2731 |
NULL, /* driver_name_list */ |
| 2732 |
0, /* nb_driver_names */ |
| 2733 |
NULL, /* init */ |
| 2734 |
NULL, /* exit */ |
| 2735 |
NULL, /* open */ |
| 2736 |
NULL, /* close */ |
| 2737 |
NULL, /* configure_endpoints */ |
| 2738 |
NULL, /* claim_interface */ |
| 2739 |
NULL, /* set_interface_altsetting */ |
| 2740 |
NULL, /* release_interface */ |
| 2741 |
NULL, /* clear_halt */ |
| 2742 |
NULL, /* reset_device */ |
| 2743 |
NULL, /* submit_bulk_transfer */ |
| 2744 |
NULL, /* submit_iso_transfer */ |
| 2745 |
NULL, /* submit_control_transfer */ |
| 2746 |
NULL, /* cancel_transfer */ |
| 2747 |
NULL, /* copy_transfer_data */ |
| 2748 |
NULL, /* endpoint_supports_raw_io */ |
| 2749 |
NULL, /* endpoint_set_raw_io */ |
| 2750 |
NULL, /* get_max_raw_io_transfer_size */ |
| 2751 |
}, |
| 2752 |
{ |
| 2753 |
USB_API_HUB, |
| 2754 |
"HUB API", |
| 2755 |
NULL, /* driver_name_list */ |
| 2756 |
0, /* nb_driver_names */ |
| 2757 |
NULL, /* init */ |
| 2758 |
NULL, /* exit */ |
| 2759 |
NULL, /* open */ |
| 2760 |
NULL, /* close */ |
| 2761 |
NULL, /* configure_endpoints */ |
| 2762 |
NULL, /* claim_interface */ |
| 2763 |
NULL, /* set_interface_altsetting */ |
| 2764 |
NULL, /* release_interface */ |
| 2765 |
NULL, /* clear_halt */ |
| 2766 |
NULL, /* reset_device */ |
| 2767 |
NULL, /* submit_bulk_transfer */ |
| 2768 |
NULL, /* submit_iso_transfer */ |
| 2769 |
NULL, /* submit_control_transfer */ |
| 2770 |
NULL, /* cancel_transfer */ |
| 2771 |
NULL, /* copy_transfer_data */ |
| 2772 |
NULL, /* endpoint_supports_raw_io */ |
| 2773 |
NULL, /* endpoint_set_raw_io */ |
| 2774 |
NULL, /* get_max_raw_io_transfer_size */ |
| 2775 |
}, |
| 2776 |
{ |
| 2777 |
USB_API_COMPOSITE, |
| 2778 |
"Composite API", |
| 2779 |
composite_driver_names, |
| 2780 |
ARRAYSIZE(composite_driver_names), |
| 2781 |
NULL, /* init */ |
| 2782 |
NULL, /* exit */ |
| 2783 |
composite_open, |
| 2784 |
composite_close, |
| 2785 |
NULL, /* configure_endpoints */ |
| 2786 |
composite_claim_interface, |
| 2787 |
composite_set_interface_altsetting, |
| 2788 |
composite_release_interface, |
| 2789 |
composite_clear_halt, |
| 2790 |
composite_reset_device, |
| 2791 |
composite_submit_bulk_transfer, |
| 2792 |
composite_submit_iso_transfer, |
| 2793 |
composite_submit_control_transfer, |
| 2794 |
composite_cancel_transfer, |
| 2795 |
composite_copy_transfer_data, |
| 2796 |
composite_endpoint_supports_raw_io, |
| 2797 |
composite_endpoint_set_raw_io, |
| 2798 |
composite_get_max_raw_io_transfer_size, |
| 2799 |
}, |
| 2800 |
{ |
| 2801 |
USB_API_WINUSBX, |
| 2802 |
"WinUSB-like APIs", |
| 2803 |
winusbx_driver_names, |
| 2804 |
ARRAYSIZE(winusbx_driver_names), |
| 2805 |
winusbx_init, |
| 2806 |
winusbx_exit, |
| 2807 |
winusbx_open, |
| 2808 |
winusbx_close, |
| 2809 |
winusbx_configure_endpoints, |
| 2810 |
winusbx_claim_interface, |
| 2811 |
winusbx_set_interface_altsetting, |
| 2812 |
winusbx_release_interface, |
| 2813 |
winusbx_clear_halt, |
| 2814 |
winusbx_reset_device, |
| 2815 |
winusbx_submit_bulk_transfer, |
| 2816 |
winusbx_submit_iso_transfer, |
| 2817 |
winusbx_submit_control_transfer, |
| 2818 |
winusbx_cancel_transfer, |
| 2819 |
winusbx_copy_transfer_data, |
| 2820 |
winusbx_endpoint_supports_raw_io, |
| 2821 |
winusbx_endpoint_set_raw_io, |
| 2822 |
winusbx_get_max_raw_io_transfer_size, |
| 2823 |
}, |
| 2824 |
{ |
| 2825 |
USB_API_HID, |
| 2826 |
"HID API", |
| 2827 |
hid_driver_names, |
| 2828 |
ARRAYSIZE(hid_driver_names), |
| 2829 |
hid_init, |
| 2830 |
hid_exit, |
| 2831 |
hid_open, |
| 2832 |
hid_close, |
| 2833 |
NULL, /* configure_endpoints */ |
| 2834 |
hid_claim_interface, |
| 2835 |
hid_set_interface_altsetting, |
| 2836 |
hid_release_interface, |
| 2837 |
hid_clear_halt, |
| 2838 |
hid_reset_device, |
| 2839 |
hid_submit_bulk_transfer, |
| 2840 |
NULL, /* submit_iso_transfer */ |
| 2841 |
hid_submit_control_transfer, |
| 2842 |
NULL, /* cancel_transfer */ |
| 2843 |
hid_copy_transfer_data, |
| 2844 |
NULL, /* endpoint_supports_raw_io */ |
| 2845 |
NULL, /* endpoint_set_raw_io */ |
| 2846 |
NULL, /* get_max_raw_io_transfer_size */ |
| 2847 |
}, |
| 2848 |
}; |
| 2849 |
|
| 2850 |
|
| 2851 |
/* |
| 2852 |
* WinUSB-like (WinUSB, libusb0/libusbK through libusbk DLL) API functions |
| 2853 |
*/ |
| 2854 |
#define WinUSB_Set(h, fn, required) \ |
| 2855 |
do { \ |
| 2856 |
WinUSBX[SUB_API_WINUSB].fn = (WinUsb_##fn##_t)GetProcAddress(h, "WinUsb_" #fn); \ |
| 2857 |
if (required && (WinUSBX[SUB_API_WINUSB].fn == NULL)) { \ |
| 2858 |
usbi_err(ctx, "GetProcAddress() failed for WinUsb_%s", #fn); \ |
| 2859 |
goto cleanup_winusb; \ |
| 2860 |
} \ |
| 2861 |
} while (0) |
| 2862 |
|
| 2863 |
#define libusbK_Set(sub_api, fn, required) \ |
| 2864 |
do { \ |
| 2865 |
pLibK_GetProcAddress((PVOID *)&WinUSBX[sub_api].fn, sub_api, KUSB_FNID_##fn); \ |
| 2866 |
if (required && (WinUSBX[sub_api].fn == NULL)) { \ |
| 2867 |
usbi_err(ctx, "LibK_GetProcAddress() failed for LibK_%s", #fn); \ |
| 2868 |
goto cleanup_libusbk; \ |
| 2869 |
} \ |
| 2870 |
} while (0) |
| 2871 |
|
| 2872 |
static bool winusbx_init(struct libusb_context *ctx) |
| 2873 |
{ |
| 2874 |
HMODULE hWinUSB, hlibusbK; |
| 2875 |
|
| 2876 |
hWinUSB = load_system_library(ctx, "WinUSB"); |
| 2877 |
if (hWinUSB != NULL) { |
| 2878 |
WinUSB_Set(hWinUSB, AbortPipe, true); |
| 2879 |
WinUSB_Set(hWinUSB, ControlTransfer, true); |
| 2880 |
WinUSB_Set(hWinUSB, FlushPipe, true); |
| 2881 |
WinUSB_Set(hWinUSB, Free, true); |
| 2882 |
WinUSB_Set(hWinUSB, GetAssociatedInterface, true); |
| 2883 |
WinUSB_Set(hWinUSB, Initialize, true); |
| 2884 |
WinUSB_Set(hWinUSB, ReadPipe, true); |
| 2885 |
WinUSB_Set(hWinUSB, ResetPipe, true); |
| 2886 |
WinUSB_Set(hWinUSB, SetCurrentAlternateSetting, true); |
| 2887 |
WinUSB_Set(hWinUSB, SetPipePolicy, true); |
| 2888 |
WinUSB_Set(hWinUSB, GetPipePolicy, true); |
| 2889 |
WinUSB_Set(hWinUSB, WritePipe, true); |
| 2890 |
|
| 2891 |
// Check for isochronous transfers support (available starting with Windows 8.1) |
| 2892 |
WinUSB_Set(hWinUSB, ReadIsochPipeAsap, false); |
| 2893 |
if (WinUSBX[SUB_API_WINUSB].ReadIsochPipeAsap != NULL) { |
| 2894 |
WinUSB_Set(hWinUSB, QueryPipeEx, true); |
| 2895 |
WinUSB_Set(hWinUSB, RegisterIsochBuffer, true); |
| 2896 |
WinUSB_Set(hWinUSB, UnregisterIsochBuffer, true); |
| 2897 |
WinUSB_Set(hWinUSB, WriteIsochPipeAsap, true); |
| 2898 |
} |
| 2899 |
|
| 2900 |
WinUSBX[SUB_API_WINUSB].hDll = hWinUSB; |
| 2901 |
|
| 2902 |
usbi_info(ctx, "WinUSB DLL available (%s isoch support)", |
| 2903 |
(WinUSBX[SUB_API_WINUSB].ReadIsochPipeAsap != NULL) ? "with" : "without"); |
| 2904 |
|
| 2905 |
cleanup_winusb: |
| 2906 |
if (WinUSBX[SUB_API_WINUSB].hDll == NULL) { |
| 2907 |
usbi_err(ctx, "failed to initialize WinUSB"); |
| 2908 |
memset(&WinUSBX[SUB_API_WINUSB], 0, sizeof(WinUSBX[SUB_API_WINUSB])); |
| 2909 |
FreeLibrary(hWinUSB); |
| 2910 |
hWinUSB = NULL; |
| 2911 |
} |
| 2912 |
} else { |
| 2913 |
usbi_info(ctx, "WinUSB DLL is not available"); |
| 2914 |
} |
| 2915 |
|
| 2916 |
hlibusbK = load_system_library(ctx, "libusbK"); |
| 2917 |
if (hlibusbK != NULL) { |
| 2918 |
LibK_GetVersion_t pLibK_GetVersion; |
| 2919 |
LibK_GetProcAddress_t pLibK_GetProcAddress; |
| 2920 |
int sub_api = 0; |
| 2921 |
|
| 2922 |
pLibK_GetVersion = (LibK_GetVersion_t)GetProcAddress(hlibusbK, "LibK_GetVersion"); |
| 2923 |
if (pLibK_GetVersion != NULL) { |
| 2924 |
KLIB_VERSION LibK_Version; |
| 2925 |
|
| 2926 |
pLibK_GetVersion(&LibK_Version); |
| 2927 |
usbi_dbg(ctx, "libusbK DLL found, version: %d.%d.%d.%d", LibK_Version.Major, LibK_Version.Minor, |
| 2928 |
LibK_Version.Micro, LibK_Version.Nano); |
| 2929 |
} else { |
| 2930 |
usbi_dbg(ctx, "libusbK DLL found, version unknown"); |
| 2931 |
} |
| 2932 |
|
| 2933 |
pLibK_GetProcAddress = (LibK_GetProcAddress_t)GetProcAddress(hlibusbK, "LibK_GetProcAddress"); |
| 2934 |
if (pLibK_GetProcAddress == NULL) { |
| 2935 |
usbi_err(ctx, "LibK_GetProcAddress() not found in libusbK DLL"); |
| 2936 |
goto cleanup_libusbk; |
| 2937 |
} |
| 2938 |
|
| 2939 |
// NB: The below for loop works because the sub_api value for WinUSB |
| 2940 |
// is a higher value than that of libusbK and libusb0 |
| 2941 |
for (; sub_api < SUB_API_WINUSB; sub_api++) { |
| 2942 |
libusbK_Set(sub_api, AbortPipe, true); |
| 2943 |
libusbK_Set(sub_api, ControlTransfer, true); |
| 2944 |
libusbK_Set(sub_api, FlushPipe, true); |
| 2945 |
libusbK_Set(sub_api, Free, true); |
| 2946 |
libusbK_Set(sub_api, GetAssociatedInterface, true); |
| 2947 |
libusbK_Set(sub_api, Initialize, true); |
| 2948 |
libusbK_Set(sub_api, ReadPipe, true); |
| 2949 |
libusbK_Set(sub_api, ResetPipe, true); |
| 2950 |
libusbK_Set(sub_api, SetCurrentAlternateSetting, true); |
| 2951 |
libusbK_Set(sub_api, SetPipePolicy, true); |
| 2952 |
libusbK_Set(sub_api, WritePipe, true); |
| 2953 |
|
| 2954 |
// Optional isochronous support |
| 2955 |
libusbK_Set(sub_api, IsoReadPipe, false); |
| 2956 |
if (WinUSBX[sub_api].IsoReadPipe != NULL) |
| 2957 |
libusbK_Set(sub_api, IsoWritePipe, true); |
| 2958 |
|
| 2959 |
// Optional device reset support |
| 2960 |
libusbK_Set(sub_api, ResetDevice, false); |
| 2961 |
|
| 2962 |
WinUSBX[sub_api].hDll = hlibusbK; |
| 2963 |
} |
| 2964 |
|
| 2965 |
cleanup_libusbk: |
| 2966 |
if (sub_api < SUB_API_WINUSB) { |
| 2967 |
usbi_err(ctx, "failed to initialize libusbK"); |
| 2968 |
while (sub_api >= 0) { |
| 2969 |
memset(&WinUSBX[sub_api], 0, sizeof(WinUSBX[sub_api])); |
| 2970 |
sub_api--; |
| 2971 |
} |
| 2972 |
FreeLibrary(hlibusbK); |
| 2973 |
hlibusbK = NULL; |
| 2974 |
} |
| 2975 |
} else { |
| 2976 |
usbi_info(ctx, "libusbK DLL is not available"); |
| 2977 |
} |
| 2978 |
|
| 2979 |
if ((hWinUSB == NULL) && (hlibusbK == NULL)) { |
| 2980 |
usbi_warn(ctx, "neither WinUSB nor libusbK DLLs were found, " |
| 2981 |
"you will not be able to access devices outside of enumeration"); |
| 2982 |
return false; |
| 2983 |
} |
| 2984 |
|
| 2985 |
return true; |
| 2986 |
} |
| 2987 |
|
| 2988 |
static void winusbx_exit(void) |
| 2989 |
{ |
| 2990 |
bool loaded = false; |
| 2991 |
HMODULE hDll; |
| 2992 |
|
| 2993 |
hDll = WinUSBX[SUB_API_LIBUSBK].hDll; |
| 2994 |
if (hDll != NULL) { |
| 2995 |
FreeLibrary(hDll); |
| 2996 |
loaded = true; |
| 2997 |
} |
| 2998 |
|
| 2999 |
hDll = WinUSBX[SUB_API_WINUSB].hDll; |
| 3000 |
if (hDll != NULL) { |
| 3001 |
FreeLibrary(hDll); |
| 3002 |
loaded = true; |
| 3003 |
} |
| 3004 |
|
| 3005 |
// Reset the WinUSBX API structures if something was loaded |
| 3006 |
if (loaded) |
| 3007 |
memset(&WinUSBX, 0, sizeof(WinUSBX)); |
| 3008 |
} |
| 3009 |
|
| 3010 |
// NB: open and close must ensure that they only handle interface of |
| 3011 |
// the right API type, as these functions can be called wholesale from |
| 3012 |
// composite_open(), with interfaces belonging to different APIs |
| 3013 |
static int winusbx_open(int sub_api, struct libusb_device_handle *dev_handle) |
| 3014 |
{ |
| 3015 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3016 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3017 |
HANDLE file_handle; |
| 3018 |
int i; |
| 3019 |
|
| 3020 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3021 |
|
| 3022 |
// WinUSB requires a separate handle for each interface |
| 3023 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 3024 |
if ((priv->usb_interface[i].path != NULL) |
| 3025 |
&& (priv->usb_interface[i].apib->id == USB_API_WINUSBX)) { |
| 3026 |
file_handle = windows_open(dev_handle, priv->usb_interface[i].path, GENERIC_READ | GENERIC_WRITE); |
| 3027 |
if (file_handle == INVALID_HANDLE_VALUE) { |
| 3028 |
usbi_err(HANDLE_CTX(dev_handle), "could not open device %s (interface %d): %s", priv->usb_interface[i].path, i, windows_error_str(0)); |
| 3029 |
switch (GetLastError()) { |
| 3030 |
case ERROR_FILE_NOT_FOUND: // The device was disconnected |
| 3031 |
return LIBUSB_ERROR_NO_DEVICE; |
| 3032 |
case ERROR_ACCESS_DENIED: |
| 3033 |
return LIBUSB_ERROR_ACCESS; |
| 3034 |
default: |
| 3035 |
return LIBUSB_ERROR_IO; |
| 3036 |
} |
| 3037 |
} |
| 3038 |
|
| 3039 |
handle_priv->interface_handle[i].dev_handle = file_handle; |
| 3040 |
} |
| 3041 |
} |
| 3042 |
|
| 3043 |
return LIBUSB_SUCCESS; |
| 3044 |
} |
| 3045 |
|
| 3046 |
static void winusbx_close(int sub_api, struct libusb_device_handle *dev_handle) |
| 3047 |
{ |
| 3048 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3049 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3050 |
HANDLE handle; |
| 3051 |
int i, ai; |
| 3052 |
|
| 3053 |
if (sub_api == SUB_API_NOTSET) |
| 3054 |
sub_api = priv->sub_api; |
| 3055 |
|
| 3056 |
if (WinUSBX[sub_api].hDll == NULL) |
| 3057 |
return; |
| 3058 |
|
| 3059 |
if (priv->apib->id == USB_API_COMPOSITE) { |
| 3060 |
// If this is a composite device, just free and close any WinUSB-like |
| 3061 |
// interfaces that are not part of an associated group |
| 3062 |
// (each is independent and not associated with another). |
| 3063 |
// For associated interface groupings, free interfaces that |
| 3064 |
// are NOT the first within that group (i.e. not bFirstInterface), |
| 3065 |
// then free & close bFirstInterface last. |
| 3066 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 3067 |
if (priv->usb_interface[i].apib->id == USB_API_WINUSBX) { |
| 3068 |
if (priv->usb_interface[i].num_associated_interfaces == 0) { |
| 3069 |
handle = handle_priv->interface_handle[i].api_handle; |
| 3070 |
if (HANDLE_VALID(handle)) |
| 3071 |
WinUSBX[sub_api].Free(handle); |
| 3072 |
|
| 3073 |
handle = handle_priv->interface_handle[i].dev_handle; |
| 3074 |
if (HANDLE_VALID(handle)) |
| 3075 |
CloseHandle(handle); |
| 3076 |
} else { |
| 3077 |
if (i==priv->usb_interface[i].first_associated_interface) { |
| 3078 |
//first free all handles for all *other* associated interfaces |
| 3079 |
for (ai = 1; ai < priv->usb_interface[i].num_associated_interfaces; ai++) { |
| 3080 |
handle = handle_priv->interface_handle[i + ai].api_handle; |
| 3081 |
if (HANDLE_VALID(handle)) |
| 3082 |
WinUSBX[sub_api].Free(handle); |
| 3083 |
} |
| 3084 |
|
| 3085 |
//free & close bFirstInterface |
| 3086 |
handle = handle_priv->interface_handle[i].api_handle; |
| 3087 |
if (HANDLE_VALID(handle)) |
| 3088 |
WinUSBX[sub_api].Free(handle); |
| 3089 |
|
| 3090 |
handle = handle_priv->interface_handle[i].dev_handle; |
| 3091 |
if (HANDLE_VALID(handle)) |
| 3092 |
CloseHandle(handle); |
| 3093 |
} |
| 3094 |
} |
| 3095 |
} |
| 3096 |
} |
| 3097 |
} else { |
| 3098 |
// If this is a WinUSB device, free all interfaces above interface 0, |
| 3099 |
// then free and close interface 0 last |
| 3100 |
for (i = 1; i < USB_MAXINTERFACES; i++) { |
| 3101 |
handle = handle_priv->interface_handle[i].api_handle; |
| 3102 |
if (HANDLE_VALID(handle)) |
| 3103 |
WinUSBX[sub_api].Free(handle); |
| 3104 |
} |
| 3105 |
handle = handle_priv->interface_handle[0].api_handle; |
| 3106 |
if (HANDLE_VALID(handle)) |
| 3107 |
WinUSBX[sub_api].Free(handle); |
| 3108 |
|
| 3109 |
handle = handle_priv->interface_handle[0].dev_handle; |
| 3110 |
if (HANDLE_VALID(handle)) |
| 3111 |
CloseHandle(handle); |
| 3112 |
} |
| 3113 |
} |
| 3114 |
|
| 3115 |
static int winusbx_configure_endpoints(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 3116 |
{ |
| 3117 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3118 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3119 |
HANDLE winusb_handle = handle_priv->interface_handle[iface].api_handle; |
| 3120 |
UCHAR policy; |
| 3121 |
ULONG timeout = 0; |
| 3122 |
uint8_t endpoint_address; |
| 3123 |
int i; |
| 3124 |
|
| 3125 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3126 |
|
| 3127 |
// With handle and endpoints set (in parent), we can setup the default pipe properties |
| 3128 |
// see http://download.microsoft.com/download/D/1/D/D1DD7745-426B-4CC3-A269-ABBBE427C0EF/DVC-T705_DDC08.pptx |
| 3129 |
for (i = -1; i < priv->usb_interface[iface].nb_endpoints; i++) { |
| 3130 |
endpoint_address = (i == -1) ? 0 : priv->usb_interface[iface].endpoint[i]; |
| 3131 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address, |
| 3132 |
PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout)) |
| 3133 |
usbi_dbg(HANDLE_CTX(dev_handle), "failed to set PIPE_TRANSFER_TIMEOUT for control endpoint %02X", endpoint_address); |
| 3134 |
|
| 3135 |
if ((i == -1) || (sub_api == SUB_API_LIBUSB0)) |
| 3136 |
continue; // Other policies don't apply to control endpoint or libusb0 |
| 3137 |
|
| 3138 |
policy = false; |
| 3139 |
handle_priv->interface_handle[iface].zlp[endpoint_address] = WINUSB_ZLP_UNSET; |
| 3140 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address, |
| 3141 |
SHORT_PACKET_TERMINATE, sizeof(UCHAR), &policy)) |
| 3142 |
usbi_dbg(HANDLE_CTX(dev_handle), "failed to disable SHORT_PACKET_TERMINATE for endpoint %02X", endpoint_address); |
| 3143 |
|
| 3144 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address, |
| 3145 |
IGNORE_SHORT_PACKETS, sizeof(UCHAR), &policy)) |
| 3146 |
usbi_dbg(HANDLE_CTX(dev_handle), "failed to disable IGNORE_SHORT_PACKETS for endpoint %02X", endpoint_address); |
| 3147 |
|
| 3148 |
policy = true; |
| 3149 |
/* ALLOW_PARTIAL_READS must be enabled due to likely libusbK bug. See: |
| 3150 |
https://sourceforge.net/mailarchive/message.php?msg_id=29736015 */ |
| 3151 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address, |
| 3152 |
ALLOW_PARTIAL_READS, sizeof(UCHAR), &policy)) |
| 3153 |
usbi_dbg(HANDLE_CTX(dev_handle), "failed to enable ALLOW_PARTIAL_READS for endpoint %02X", endpoint_address); |
| 3154 |
|
| 3155 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address, |
| 3156 |
AUTO_CLEAR_STALL, sizeof(UCHAR), &policy)) |
| 3157 |
usbi_dbg(HANDLE_CTX(dev_handle), "failed to enable AUTO_CLEAR_STALL for endpoint %02X", endpoint_address); |
| 3158 |
|
| 3159 |
if (sub_api == SUB_API_LIBUSBK) { |
| 3160 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address, |
| 3161 |
ISO_ALWAYS_START_ASAP, sizeof(UCHAR), &policy)) |
| 3162 |
usbi_dbg(HANDLE_CTX(dev_handle), "failed to enable ISO_ALWAYS_START_ASAP for endpoint %02X", endpoint_address); |
| 3163 |
} |
| 3164 |
} |
| 3165 |
|
| 3166 |
return LIBUSB_SUCCESS; |
| 3167 |
} |
| 3168 |
|
| 3169 |
static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 3170 |
{ |
| 3171 |
struct libusb_context *ctx = HANDLE_CTX(dev_handle); |
| 3172 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3173 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3174 |
bool is_using_usbccgp = (priv->apib->id == USB_API_COMPOSITE); |
| 3175 |
bool is_associated_interface = (priv->usb_interface[iface].num_associated_interfaces != 0); |
| 3176 |
HDEVINFO dev_info; |
| 3177 |
char *dev_interface_path = NULL; |
| 3178 |
char *dev_interface_path_guid_start; |
| 3179 |
char filter_path[] = "\\\\.\\libusb0-0000"; |
| 3180 |
bool found_filter = false; |
| 3181 |
HANDLE file_handle, winusb_handle; |
| 3182 |
DWORD err, _index; |
| 3183 |
int r; |
| 3184 |
uint8_t initialized_iface; |
| 3185 |
|
| 3186 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3187 |
|
| 3188 |
// If the device is composite, but using the default Windows composite parent driver (usbccgp) |
| 3189 |
// or if it's the first WinUSB-like interface, we get a handle through Initialize(). |
| 3190 |
// If it's an associated interface, and is the first one (iface==bFirstInterface), we also |
| 3191 |
// want to get the handle through Initialize(). If it's an associated interface, and NOT |
| 3192 |
// the first one, we want to direct control to the 'else' where the handle will be obtained |
| 3193 |
// via GetAssociatedInterface(). |
| 3194 |
if (((is_using_usbccgp) || (iface == 0)) && |
| 3195 |
(!is_associated_interface || (iface==priv->usb_interface[iface].first_associated_interface))) { |
| 3196 |
// composite device (independent interfaces) or interface 0 |
| 3197 |
file_handle = handle_priv->interface_handle[iface].dev_handle; |
| 3198 |
if (!HANDLE_VALID(file_handle)) |
| 3199 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3200 |
|
| 3201 |
if (!WinUSBX[sub_api].Initialize(file_handle, &winusb_handle)) { |
| 3202 |
handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE; |
| 3203 |
err = GetLastError(); |
| 3204 |
switch (err) { |
| 3205 |
case ERROR_BAD_COMMAND: |
| 3206 |
// The device was disconnected |
| 3207 |
usbi_err(ctx, "could not access interface %u: %s", iface, windows_error_str(0)); |
| 3208 |
return LIBUSB_ERROR_NO_DEVICE; |
| 3209 |
default: |
| 3210 |
// it may be that we're using the libusb0 filter driver. |
| 3211 |
// TODO: can we move this whole business into the K/0 DLL? |
| 3212 |
r = LIBUSB_SUCCESS; |
| 3213 |
for (_index = 0; ; _index++) { |
| 3214 |
safe_free(dev_interface_path); |
| 3215 |
|
| 3216 |
if (found_filter) |
| 3217 |
break; |
| 3218 |
|
| 3219 |
r = get_interface_details_filter(ctx, &dev_info, _index, filter_path, &dev_interface_path); |
| 3220 |
if ((r != LIBUSB_SUCCESS) || (dev_interface_path == NULL)) |
| 3221 |
break; |
| 3222 |
|
| 3223 |
// ignore GUID part |
| 3224 |
dev_interface_path_guid_start = strchr(dev_interface_path, '{'); |
| 3225 |
if (dev_interface_path_guid_start == NULL) |
| 3226 |
continue; |
| 3227 |
*dev_interface_path_guid_start = '\0'; |
| 3228 |
|
| 3229 |
if (strncmp(dev_interface_path, priv->usb_interface[iface].path, strlen(dev_interface_path)) == 0) { |
| 3230 |
file_handle = windows_open(dev_handle, filter_path, GENERIC_READ | GENERIC_WRITE); |
| 3231 |
if (file_handle != INVALID_HANDLE_VALUE) { |
| 3232 |
if (WinUSBX[sub_api].Initialize(file_handle, &winusb_handle)) { |
| 3233 |
// Replace the existing file handle with the working one |
| 3234 |
CloseHandle(handle_priv->interface_handle[iface].dev_handle); |
| 3235 |
handle_priv->interface_handle[iface].dev_handle = file_handle; |
| 3236 |
found_filter = true; |
| 3237 |
} else { |
| 3238 |
usbi_err(ctx, "could not initialize filter driver for %s", filter_path); |
| 3239 |
CloseHandle(file_handle); |
| 3240 |
} |
| 3241 |
} else { |
| 3242 |
usbi_err(ctx, "could not open device %s: %s", filter_path, windows_error_str(0)); |
| 3243 |
} |
| 3244 |
} |
| 3245 |
} |
| 3246 |
if (r != LIBUSB_SUCCESS) |
| 3247 |
return r; |
| 3248 |
if (!found_filter) { |
| 3249 |
usbi_err(ctx, "could not access interface %u: %s", iface, windows_error_str(err)); |
| 3250 |
return LIBUSB_ERROR_ACCESS; |
| 3251 |
} |
| 3252 |
} |
| 3253 |
} |
| 3254 |
handle_priv->interface_handle[iface].api_handle = winusb_handle; |
| 3255 |
} else { |
| 3256 |
if (is_associated_interface) { |
| 3257 |
initialized_iface = priv->usb_interface[iface].first_associated_interface; |
| 3258 |
if (iface <= initialized_iface) { |
| 3259 |
usbi_err(ctx, "invalid associated index. iface=%u, initialized iface=%u", iface, initialized_iface); |
| 3260 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3261 |
} |
| 3262 |
} else { |
| 3263 |
initialized_iface = 0; |
| 3264 |
} |
| 3265 |
|
| 3266 |
// For all other interfaces, use GetAssociatedInterface() |
| 3267 |
winusb_handle = handle_priv->interface_handle[initialized_iface].api_handle; |
| 3268 |
// It is a requirement for multiple interface devices on Windows that, to you |
| 3269 |
// must first claim the first interface before you claim the others |
| 3270 |
if (!HANDLE_VALID(winusb_handle)) { |
| 3271 |
file_handle = handle_priv->interface_handle[initialized_iface].dev_handle; |
| 3272 |
if (WinUSBX[sub_api].Initialize(file_handle, &winusb_handle)) { |
| 3273 |
handle_priv->interface_handle[initialized_iface].api_handle = winusb_handle; |
| 3274 |
usbi_warn(ctx, "auto-claimed interface %u (required to claim %u with WinUSB)", initialized_iface, iface); |
| 3275 |
} else { |
| 3276 |
usbi_warn(ctx, "failed to auto-claim interface %u (required to claim %u with WinUSB): %s", |
| 3277 |
initialized_iface, iface, windows_error_str(0)); |
| 3278 |
return LIBUSB_ERROR_ACCESS; |
| 3279 |
} |
| 3280 |
} |
| 3281 |
if (!WinUSBX[sub_api].GetAssociatedInterface(winusb_handle, (UCHAR)(iface - 1 - initialized_iface), |
| 3282 |
&handle_priv->interface_handle[iface].api_handle)) { |
| 3283 |
handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE; |
| 3284 |
switch (GetLastError()) { |
| 3285 |
case ERROR_NO_MORE_ITEMS: // invalid iface |
| 3286 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3287 |
case ERROR_BAD_COMMAND: // The device was disconnected |
| 3288 |
return LIBUSB_ERROR_NO_DEVICE; |
| 3289 |
case ERROR_ALREADY_EXISTS: // already claimed |
| 3290 |
return LIBUSB_ERROR_BUSY; |
| 3291 |
default: |
| 3292 |
usbi_err(ctx, "could not claim interface %u: %s", iface, windows_error_str(0)); |
| 3293 |
return LIBUSB_ERROR_ACCESS; |
| 3294 |
} |
| 3295 |
} |
| 3296 |
handle_priv->interface_handle[iface].dev_handle = handle_priv->interface_handle[initialized_iface].dev_handle; |
| 3297 |
} |
| 3298 |
usbi_dbg(ctx, "claimed interface %u", iface); |
| 3299 |
handle_priv->active_interface = iface; |
| 3300 |
|
| 3301 |
return LIBUSB_SUCCESS; |
| 3302 |
} |
| 3303 |
|
| 3304 |
static int winusbx_release_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 3305 |
{ |
| 3306 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3307 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3308 |
HANDLE winusb_handle; |
| 3309 |
|
| 3310 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3311 |
|
| 3312 |
winusb_handle = handle_priv->interface_handle[iface].api_handle; |
| 3313 |
if (!HANDLE_VALID(winusb_handle)) |
| 3314 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3315 |
|
| 3316 |
WinUSBX[sub_api].Free(winusb_handle); |
| 3317 |
handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE; |
| 3318 |
|
| 3319 |
return LIBUSB_SUCCESS; |
| 3320 |
} |
| 3321 |
|
| 3322 |
/* |
| 3323 |
* Return the first valid interface (of the same API type), for control transfers |
| 3324 |
*/ |
| 3325 |
static int get_valid_interface(struct libusb_device_handle *dev_handle, int api_id) |
| 3326 |
{ |
| 3327 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3328 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3329 |
int i; |
| 3330 |
|
| 3331 |
if ((api_id < USB_API_WINUSBX) || (api_id > USB_API_HID)) { |
| 3332 |
usbi_dbg(HANDLE_CTX(dev_handle), "unsupported API ID"); |
| 3333 |
return -1; |
| 3334 |
} |
| 3335 |
|
| 3336 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 3337 |
if (HANDLE_VALID(handle_priv->interface_handle[i].dev_handle) |
| 3338 |
&& HANDLE_VALID(handle_priv->interface_handle[i].api_handle) |
| 3339 |
&& (priv->usb_interface[i].apib->id == api_id)) |
| 3340 |
return i; |
| 3341 |
} |
| 3342 |
|
| 3343 |
return -1; |
| 3344 |
} |
| 3345 |
|
| 3346 |
/* |
| 3347 |
* Check a specific interface is valid (of the same API type), for control transfers |
| 3348 |
*/ |
| 3349 |
static int check_valid_interface(struct libusb_device_handle *dev_handle, unsigned short interface, int api_id) |
| 3350 |
{ |
| 3351 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3352 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3353 |
|
| 3354 |
if (interface >= USB_MAXINTERFACES) |
| 3355 |
return -1; |
| 3356 |
|
| 3357 |
if ((api_id < USB_API_WINUSBX) || (api_id > USB_API_HID)) { |
| 3358 |
usbi_dbg(HANDLE_CTX(dev_handle), "unsupported API ID"); |
| 3359 |
return -1; |
| 3360 |
} |
| 3361 |
|
| 3362 |
// try the requested interface |
| 3363 |
if (HANDLE_VALID(handle_priv->interface_handle[interface].dev_handle) |
| 3364 |
&& HANDLE_VALID(handle_priv->interface_handle[interface].api_handle) |
| 3365 |
&& (priv->usb_interface[interface].apib->id == api_id)) |
| 3366 |
return interface; |
| 3367 |
|
| 3368 |
return -1; |
| 3369 |
} |
| 3370 |
|
| 3371 |
/* |
| 3372 |
* Lookup interface by endpoint address. -1 if not found |
| 3373 |
*/ |
| 3374 |
static int interface_by_endpoint(struct winusb_device_priv *priv, |
| 3375 |
struct winusb_device_handle_priv *handle_priv, uint8_t endpoint_address) |
| 3376 |
{ |
| 3377 |
int i, j; |
| 3378 |
int result = -1; |
| 3379 |
|
| 3380 |
// priv->usb_interface[].endpoint and .nb_endpoints are written |
| 3381 |
// under interface_lock by claim/altsetting and the enumeration |
| 3382 |
// setup paths. Take the lock so we don't read a freed endpoint |
| 3383 |
// pointer or an inconsistent (nb_endpoints, endpoint[]) pair. |
| 3384 |
usbi_mutex_lock(&priv->interface_lock); |
| 3385 |
|
| 3386 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 3387 |
if (!HANDLE_VALID(handle_priv->interface_handle[i].api_handle)) |
| 3388 |
continue; |
| 3389 |
if (priv->usb_interface[i].endpoint == NULL) |
| 3390 |
continue; |
| 3391 |
for (j = 0; j < priv->usb_interface[i].nb_endpoints; j++) { |
| 3392 |
if (priv->usb_interface[i].endpoint[j] == endpoint_address) { |
| 3393 |
result = i; |
| 3394 |
goto out; |
| 3395 |
} |
| 3396 |
} |
| 3397 |
} |
| 3398 |
|
| 3399 |
out: |
| 3400 |
usbi_mutex_unlock(&priv->interface_lock); |
| 3401 |
return result; |
| 3402 |
} |
| 3403 |
|
| 3404 |
static int winusbx_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 3405 |
{ |
| 3406 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 3407 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 3408 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 3409 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 3410 |
PWINUSB_SETUP_PACKET setup = (PWINUSB_SETUP_PACKET)transfer->buffer; |
| 3411 |
ULONG size, transferred; |
| 3412 |
HANDLE winusb_handle; |
| 3413 |
OVERLAPPED *overlapped; |
| 3414 |
int current_interface; |
| 3415 |
|
| 3416 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3417 |
|
| 3418 |
size = transfer->length - LIBUSB_CONTROL_SETUP_SIZE; |
| 3419 |
|
| 3420 |
// Windows places upper limits on the control transfer size |
| 3421 |
// See: https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/usb-bandwidth-allocation#maximum-transfer-size |
| 3422 |
if (size > MAX_CTRL_BUFFER_LENGTH) |
| 3423 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 3424 |
|
| 3425 |
if ((setup->RequestType & 0x1F) == LIBUSB_RECIPIENT_INTERFACE) |
| 3426 |
current_interface = check_valid_interface(transfer->dev_handle, setup->Index & 0xff, USB_API_WINUSBX); |
| 3427 |
else |
| 3428 |
current_interface = get_valid_interface(transfer->dev_handle, USB_API_WINUSBX); |
| 3429 |
if (current_interface < 0) { |
| 3430 |
if (auto_claim(transfer, ¤t_interface, USB_API_WINUSBX) != LIBUSB_SUCCESS) |
| 3431 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3432 |
} |
| 3433 |
|
| 3434 |
usbi_dbg(ITRANSFER_CTX(itransfer), "will use interface %d", current_interface); |
| 3435 |
|
| 3436 |
transfer_priv->interface_number = (uint8_t)current_interface; |
| 3437 |
winusb_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 3438 |
set_transfer_priv_handle(itransfer, handle_priv->interface_handle[current_interface].dev_handle); |
| 3439 |
overlapped = get_transfer_priv_overlapped(itransfer); |
| 3440 |
|
| 3441 |
// Sending of set configuration control requests from WinUSB creates issues, except when using libusb0.sys |
| 3442 |
if (sub_api != SUB_API_LIBUSB0 |
| 3443 |
&& (LIBUSB_REQ_TYPE(setup->RequestType) == LIBUSB_REQUEST_TYPE_STANDARD) |
| 3444 |
&& (setup->Request == LIBUSB_REQUEST_SET_CONFIGURATION)) { |
| 3445 |
if (setup->Value != priv->active_config) { |
| 3446 |
usbi_warn(TRANSFER_CTX(transfer), "cannot set configuration other than the default one"); |
| 3447 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3448 |
} |
| 3449 |
windows_force_sync_completion(itransfer, 0); |
| 3450 |
} else { |
| 3451 |
if (!WinUSBX[sub_api].ControlTransfer(winusb_handle, *setup, transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, size, &transferred, overlapped)) { |
| 3452 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 3453 |
usbi_warn(TRANSFER_CTX(transfer), "ControlTransfer failed: %s", windows_error_str(0)); |
| 3454 |
return LIBUSB_ERROR_IO; |
| 3455 |
} |
| 3456 |
} else { |
| 3457 |
windows_force_sync_completion(itransfer, transferred); |
| 3458 |
} |
| 3459 |
} |
| 3460 |
|
| 3461 |
return LIBUSB_SUCCESS; |
| 3462 |
} |
| 3463 |
|
| 3464 |
static int winusbx_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting) |
| 3465 |
{ |
| 3466 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3467 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3468 |
HANDLE winusb_handle; |
| 3469 |
|
| 3470 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3471 |
|
| 3472 |
winusb_handle = handle_priv->interface_handle[iface].api_handle; |
| 3473 |
if (!HANDLE_VALID(winusb_handle)) { |
| 3474 |
usbi_err(HANDLE_CTX(dev_handle), "interface must be claimed first"); |
| 3475 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3476 |
} |
| 3477 |
|
| 3478 |
if (!WinUSBX[sub_api].SetCurrentAlternateSetting(winusb_handle, altsetting)) { |
| 3479 |
usbi_err(HANDLE_CTX(dev_handle), "SetCurrentAlternateSetting failed: %s", windows_error_str(0)); |
| 3480 |
return LIBUSB_ERROR_IO; |
| 3481 |
} |
| 3482 |
|
| 3483 |
return LIBUSB_SUCCESS; |
| 3484 |
} |
| 3485 |
|
| 3486 |
|
| 3487 |
static void WINAPI winusbx_native_iso_transfer_continue_stream_callback(struct libusb_transfer *transfer) |
| 3488 |
{ |
| 3489 |
// If this callback is invoked, this means that we attempted to set ContinueStream |
| 3490 |
// to TRUE when calling Read/WriteIsochPipeAsap in winusbx_submit_iso_transfer(). |
| 3491 |
// The role of this callback is to fallback to ContinueStream = FALSE if the transfer |
| 3492 |
// did not succeed. |
| 3493 |
|
| 3494 |
struct winusb_transfer_priv *transfer_priv = |
| 3495 |
get_winusb_transfer_priv(LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer)); |
| 3496 |
bool fallback = (transfer->status != LIBUSB_TRANSFER_COMPLETED); |
| 3497 |
int idx; |
| 3498 |
|
| 3499 |
// Restore the user callback |
| 3500 |
transfer->callback = transfer_priv->iso_user_callback; |
| 3501 |
|
| 3502 |
for (idx = 0; idx < transfer->num_iso_packets && !fallback; idx++) { |
| 3503 |
if (transfer->iso_packet_desc[idx].status != LIBUSB_TRANSFER_COMPLETED) |
| 3504 |
fallback = true; |
| 3505 |
} |
| 3506 |
|
| 3507 |
if (!fallback) { |
| 3508 |
// If the transfer was successful, we restore the user callback and call it. |
| 3509 |
if (transfer->callback) |
| 3510 |
transfer->callback(transfer); |
| 3511 |
} else { |
| 3512 |
// If the transfer wasn't successful we reschedule the transfer while forcing it |
| 3513 |
// not to continue the stream. This might results in a 5-ms delay. |
| 3514 |
transfer_priv->iso_break_stream = TRUE; |
| 3515 |
libusb_submit_transfer(transfer); |
| 3516 |
} |
| 3517 |
} |
| 3518 |
static int winusbx_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 3519 |
{ |
| 3520 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 3521 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 3522 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 3523 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 3524 |
HANDLE winusb_handle; |
| 3525 |
OVERLAPPED *overlapped; |
| 3526 |
BOOL ret; |
| 3527 |
int current_interface; |
| 3528 |
|
| 3529 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3530 |
|
| 3531 |
current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint); |
| 3532 |
if (current_interface < 0) { |
| 3533 |
usbi_err(TRANSFER_CTX(transfer), "unable to match endpoint to an open interface - cancelling transfer"); |
| 3534 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3535 |
} |
| 3536 |
|
| 3537 |
usbi_dbg(TRANSFER_CTX(transfer), "matched endpoint %02X with interface %d", transfer->endpoint, current_interface); |
| 3538 |
|
| 3539 |
transfer_priv->interface_number = (uint8_t)current_interface; |
| 3540 |
winusb_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 3541 |
set_transfer_priv_handle(itransfer, handle_priv->interface_handle[current_interface].dev_handle); |
| 3542 |
overlapped = get_transfer_priv_overlapped(itransfer); |
| 3543 |
|
| 3544 |
if ((sub_api == SUB_API_LIBUSBK) || (sub_api == SUB_API_LIBUSB0)) { |
| 3545 |
int i; |
| 3546 |
UINT offset; |
| 3547 |
size_t iso_ctx_size; |
| 3548 |
PKISO_CONTEXT iso_context; |
| 3549 |
|
| 3550 |
if (WinUSBX[sub_api].IsoReadPipe == NULL) { |
| 3551 |
usbi_warn(TRANSFER_CTX(transfer), "libusbK DLL does not support isoch transfers"); |
| 3552 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3553 |
} |
| 3554 |
|
| 3555 |
iso_ctx_size = sizeof(KISO_CONTEXT) + (transfer->num_iso_packets * sizeof(KISO_PACKET)); |
| 3556 |
transfer_priv->iso_context = iso_context = calloc(1, iso_ctx_size); |
| 3557 |
if (transfer_priv->iso_context == NULL) |
| 3558 |
return LIBUSB_ERROR_NO_MEM; |
| 3559 |
|
| 3560 |
// start ASAP |
| 3561 |
iso_context->StartFrame = 0; |
| 3562 |
iso_context->NumberOfPackets = (SHORT)transfer->num_iso_packets; |
| 3563 |
|
| 3564 |
// convert the transfer packet lengths to iso_packet offsets |
| 3565 |
offset = 0; |
| 3566 |
for (i = 0; i < transfer->num_iso_packets; i++) { |
| 3567 |
iso_context->IsoPackets[i].offset = offset; |
| 3568 |
offset += transfer->iso_packet_desc[i].length; |
| 3569 |
} |
| 3570 |
|
| 3571 |
if (IS_XFERIN(transfer)) { |
| 3572 |
usbi_dbg(TRANSFER_CTX(transfer), "reading %d iso packets", transfer->num_iso_packets); |
| 3573 |
ret = WinUSBX[sub_api].IsoReadPipe(winusb_handle, transfer->endpoint, transfer->buffer, transfer->length, overlapped, iso_context); |
| 3574 |
} else { |
| 3575 |
usbi_dbg(TRANSFER_CTX(transfer), "writing %d iso packets", transfer->num_iso_packets); |
| 3576 |
ret = WinUSBX[sub_api].IsoWritePipe(winusb_handle, transfer->endpoint, transfer->buffer, transfer->length, overlapped, iso_context); |
| 3577 |
} |
| 3578 |
|
| 3579 |
if (!ret && GetLastError() != ERROR_IO_PENDING) { |
| 3580 |
usbi_err(TRANSFER_CTX(transfer), "IsoReadPipe/IsoWritePipe failed: %s", windows_error_str(0)); |
| 3581 |
return LIBUSB_ERROR_IO; |
| 3582 |
} |
| 3583 |
|
| 3584 |
return LIBUSB_SUCCESS; |
| 3585 |
} else if (sub_api == SUB_API_WINUSB) { |
| 3586 |
WINUSB_PIPE_INFORMATION_EX pipe_info_ex = { 0 }; |
| 3587 |
WINUSB_ISOCH_BUFFER_HANDLE buffer_handle; |
| 3588 |
ULONG iso_transfer_size_multiple; |
| 3589 |
int out_transfer_length = 0; |
| 3590 |
int idx; |
| 3591 |
|
| 3592 |
// Depending on the version of Microsoft WinUSB, isochronous transfers may not be supported. |
| 3593 |
if (WinUSBX[sub_api].ReadIsochPipeAsap == NULL) { |
| 3594 |
usbi_warn(TRANSFER_CTX(transfer), "WinUSB DLL does not support isoch transfers"); |
| 3595 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3596 |
} |
| 3597 |
|
| 3598 |
if (sizeof(struct libusb_iso_packet_descriptor) != sizeof(USBD_ISO_PACKET_DESCRIPTOR)) { |
| 3599 |
usbi_err(TRANSFER_CTX(transfer), "size of WinUsb and libusb isoch packet descriptors don't match"); |
| 3600 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3601 |
} |
| 3602 |
|
| 3603 |
// Query the pipe extended information to find the pipe index corresponding to the endpoint. |
| 3604 |
// nb_endpoints and current_altsetting are written under interface_lock by claim/altsetting |
| 3605 |
// and enumeration setup; take the lock so the loop bound and the QueryPipeEx altsetting |
| 3606 |
// argument come from a consistent snapshot. |
| 3607 |
{ |
| 3608 |
int nb_endpoints; |
| 3609 |
uint8_t altsetting; |
| 3610 |
|
| 3611 |
usbi_mutex_lock(&priv->interface_lock); |
| 3612 |
nb_endpoints = priv->usb_interface[current_interface].nb_endpoints; |
| 3613 |
altsetting = (uint8_t)priv->usb_interface[current_interface].current_altsetting; |
| 3614 |
usbi_mutex_unlock(&priv->interface_lock); |
| 3615 |
|
| 3616 |
for (idx = 0; idx < nb_endpoints; ++idx) { |
| 3617 |
ret = WinUSBX[sub_api].QueryPipeEx(winusb_handle, altsetting, (UCHAR)idx, &pipe_info_ex); |
| 3618 |
if (!ret) { |
| 3619 |
usbi_err(TRANSFER_CTX(transfer), "couldn't query interface settings for USB pipe with index %d. Error: %s", idx, windows_error_str(0)); |
| 3620 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3621 |
} |
| 3622 |
|
| 3623 |
if (pipe_info_ex.PipeId == transfer->endpoint && pipe_info_ex.PipeType == UsbdPipeTypeIsochronous) |
| 3624 |
break; |
| 3625 |
} |
| 3626 |
|
| 3627 |
// Make sure we found the index. |
| 3628 |
if (idx == nb_endpoints) { |
| 3629 |
usbi_err(TRANSFER_CTX(transfer), "couldn't find isoch endpoint 0x%02x", transfer->endpoint); |
| 3630 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3631 |
} |
| 3632 |
} |
| 3633 |
|
| 3634 |
if (IS_XFERIN(transfer)) { |
| 3635 |
int interval = pipe_info_ex.Interval; |
| 3636 |
|
| 3637 |
// For high-speed and SuperSpeed device, the interval is 2**(bInterval-1). |
| 3638 |
if (transfer->dev_handle->dev->speed >= LIBUSB_SPEED_HIGH) |
| 3639 |
interval = (1 << (pipe_info_ex.Interval - 1)); |
| 3640 |
|
| 3641 |
// WinUSB only supports isoch transfers spanning a full USB frames. Later, we might be smarter about this |
| 3642 |
// and allocate a temporary buffer. However, this is harder than it seems as its destruction would depend on overlapped |
| 3643 |
// IO... |
| 3644 |
if (transfer->dev_handle->dev->speed >= LIBUSB_SPEED_HIGH) // Microframes (125us) |
| 3645 |
iso_transfer_size_multiple = (pipe_info_ex.MaximumBytesPerInterval * 8) / interval; |
| 3646 |
else // Normal Frames (1ms) |
| 3647 |
iso_transfer_size_multiple = pipe_info_ex.MaximumBytesPerInterval / interval; |
| 3648 |
|
| 3649 |
if (transfer->length % iso_transfer_size_multiple != 0) { |
| 3650 |
usbi_err(TRANSFER_CTX(transfer), "length of isoch buffer must be a multiple of the MaximumBytesPerInterval * 8 / Interval"); |
| 3651 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 3652 |
} |
| 3653 |
} else { |
| 3654 |
// If this is an OUT transfer, we make sure the isoch packets are contiguous as this isn't supported otherwise. |
| 3655 |
bool size_should_be_zero = false; |
| 3656 |
|
| 3657 |
for (idx = 0; idx < transfer->num_iso_packets; ++idx) { |
| 3658 |
if ((size_should_be_zero && transfer->iso_packet_desc[idx].length != 0) || |
| 3659 |
(transfer->iso_packet_desc[idx].length != pipe_info_ex.MaximumBytesPerInterval && idx + 1 < transfer->num_iso_packets && transfer->iso_packet_desc[idx + 1].length > 0)) { |
| 3660 |
usbi_err(TRANSFER_CTX(transfer), "isoch packets for OUT transfer with WinUSB must be contiguous in memory"); |
| 3661 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 3662 |
} |
| 3663 |
|
| 3664 |
size_should_be_zero = (transfer->iso_packet_desc[idx].length == 0); |
| 3665 |
out_transfer_length += transfer->iso_packet_desc[idx].length; |
| 3666 |
} |
| 3667 |
} |
| 3668 |
|
| 3669 |
if (transfer_priv->isoch_buffer_handle != NULL) { |
| 3670 |
if (WinUSBX[sub_api].UnregisterIsochBuffer(transfer_priv->isoch_buffer_handle)) { |
| 3671 |
transfer_priv->isoch_buffer_handle = NULL; |
| 3672 |
} else { |
| 3673 |
usbi_err(TRANSFER_CTX(transfer), "failed to unregister WinUSB isoch buffer: %s", windows_error_str(0)); |
| 3674 |
return LIBUSB_ERROR_OTHER; |
| 3675 |
} |
| 3676 |
} |
| 3677 |
|
| 3678 |
// Register the isoch buffer to the operating system. |
| 3679 |
ret = WinUSBX[sub_api].RegisterIsochBuffer(winusb_handle, transfer->endpoint, transfer->buffer, transfer->length, &buffer_handle); |
| 3680 |
if (!ret) { |
| 3681 |
usbi_err(TRANSFER_CTX(transfer), "failed to register WinUSB isoch buffer: %s", windows_error_str(0)); |
| 3682 |
return LIBUSB_ERROR_NO_MEM; |
| 3683 |
} |
| 3684 |
|
| 3685 |
// Important note: the WinUSB_Read/WriteIsochPipeAsap API requires a ContinueStream parameter that tells whether the isochronous |
| 3686 |
// stream must be continued or if the WinUSB driver can schedule the transfer at its convenience. Profiling subsequent transfers |
| 3687 |
// with ContinueStream = FALSE showed that 5 frames, i.e. about 5 milliseconds, were left empty between each transfer. This |
| 3688 |
// is critical as this greatly diminish the achievable isochronous bandwidth. We solved the problem using the following strategy: |
| 3689 |
// - Transfers are first scheduled with ContinueStream = TRUE and with winusbx_iso_transfer_continue_stream_callback as user callback. |
| 3690 |
// - If the transfer succeeds, winusbx_iso_transfer_continue_stream_callback restore the user callback and calls its. |
| 3691 |
// - If the transfer fails, winusbx_iso_transfer_continue_stream_callback reschedule the transfer and force ContinueStream = FALSE. |
| 3692 |
if (!transfer_priv->iso_break_stream) { |
| 3693 |
transfer_priv->iso_user_callback = transfer->callback; |
| 3694 |
transfer->callback = winusbx_native_iso_transfer_continue_stream_callback; |
| 3695 |
} |
| 3696 |
|
| 3697 |
// Initiate the transfers. |
| 3698 |
if (IS_XFERIN(transfer)) |
| 3699 |
ret = WinUSBX[sub_api].ReadIsochPipeAsap(buffer_handle, 0, transfer->length, !transfer_priv->iso_break_stream, transfer->num_iso_packets, (PUSBD_ISO_PACKET_DESCRIPTOR)transfer->iso_packet_desc, overlapped); |
| 3700 |
else |
| 3701 |
ret = WinUSBX[sub_api].WriteIsochPipeAsap(buffer_handle, 0, out_transfer_length, !transfer_priv->iso_break_stream, overlapped); |
| 3702 |
|
| 3703 |
if (!ret && GetLastError() != ERROR_IO_PENDING) { |
| 3704 |
usbi_err(TRANSFER_CTX(transfer), "ReadIsochPipeAsap/WriteIsochPipeAsap failed: %s", windows_error_str(0)); |
| 3705 |
if (!WinUSBX[sub_api].UnregisterIsochBuffer(buffer_handle)) |
| 3706 |
usbi_warn(TRANSFER_CTX(transfer), "failed to unregister WinUSB isoch buffer: %s", windows_error_str(0)); |
| 3707 |
return LIBUSB_ERROR_IO; |
| 3708 |
} |
| 3709 |
|
| 3710 |
// Restore the ContinueStream parameter to TRUE. |
| 3711 |
transfer_priv->iso_break_stream = FALSE; |
| 3712 |
|
| 3713 |
transfer_priv->isoch_buffer_handle = buffer_handle; |
| 3714 |
|
| 3715 |
return LIBUSB_SUCCESS; |
| 3716 |
} else { |
| 3717 |
PRINT_UNSUPPORTED_API(winusbx_submit_iso_transfer); |
| 3718 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3719 |
} |
| 3720 |
} |
| 3721 |
|
| 3722 |
static int winusbx_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 3723 |
{ |
| 3724 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 3725 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 3726 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 3727 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 3728 |
HANDLE winusb_handle; |
| 3729 |
OVERLAPPED *overlapped; |
| 3730 |
BOOL ret; |
| 3731 |
int current_interface; |
| 3732 |
|
| 3733 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3734 |
|
| 3735 |
current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint); |
| 3736 |
if (current_interface < 0) { |
| 3737 |
usbi_err(TRANSFER_CTX(transfer), "unable to match endpoint to an open interface - cancelling transfer"); |
| 3738 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3739 |
} |
| 3740 |
|
| 3741 |
usbi_dbg(TRANSFER_CTX(transfer), "matched endpoint %02X with interface %d", transfer->endpoint, current_interface); |
| 3742 |
|
| 3743 |
transfer_priv->interface_number = (uint8_t)current_interface; |
| 3744 |
winusb_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 3745 |
set_transfer_priv_handle(itransfer, handle_priv->interface_handle[current_interface].dev_handle); |
| 3746 |
overlapped = get_transfer_priv_overlapped(itransfer); |
| 3747 |
|
| 3748 |
if (IS_XFERIN(transfer)) { |
| 3749 |
// Note: We don't need to handle transfers to pipes with RAW_IO enabled differently, |
| 3750 |
// as ReadPipe() already fails if the length argument doesn't satisfy the RAW_IO requirements. |
| 3751 |
usbi_dbg(TRANSFER_CTX(transfer), "reading %d bytes", transfer->length); |
| 3752 |
ret = WinUSBX[sub_api].ReadPipe(winusb_handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, overlapped); |
| 3753 |
} else { |
| 3754 |
// Set SHORT_PACKET_TERMINATE if ZLP requested. |
| 3755 |
// Changing this can be a problem with packets in flight, so only allow on the first transfer. |
| 3756 |
UCHAR policy = (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) != 0; |
| 3757 |
uint8_t* current_zlp = &handle_priv->interface_handle[current_interface].zlp[transfer->endpoint]; |
| 3758 |
if (*current_zlp == WINUSB_ZLP_UNSET) { |
| 3759 |
if (policy && |
| 3760 |
!WinUSBX[sub_api].SetPipePolicy(winusb_handle, transfer->endpoint, |
| 3761 |
SHORT_PACKET_TERMINATE, sizeof(UCHAR), &policy)) { |
| 3762 |
usbi_err(TRANSFER_CTX(transfer), "failed to set SHORT_PACKET_TERMINATE for endpoint %02X", transfer->endpoint); |
| 3763 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3764 |
} |
| 3765 |
*current_zlp = policy ? WINUSB_ZLP_ON : WINUSB_ZLP_OFF; |
| 3766 |
} else if (policy != (*current_zlp == WINUSB_ZLP_ON)) { |
| 3767 |
usbi_err(TRANSFER_CTX(transfer), "cannot change ZERO_PACKET for endpoint %02X on Windows", transfer->endpoint); |
| 3768 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3769 |
} |
| 3770 |
|
| 3771 |
usbi_dbg(TRANSFER_CTX(transfer), "writing %d bytes", transfer->length); |
| 3772 |
ret = WinUSBX[sub_api].WritePipe(winusb_handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, overlapped); |
| 3773 |
} |
| 3774 |
|
| 3775 |
if (!ret && GetLastError() != ERROR_IO_PENDING) { |
| 3776 |
usbi_err(TRANSFER_CTX(transfer), "ReadPipe/WritePipe failed: %s", windows_error_str(0)); |
| 3777 |
return LIBUSB_ERROR_IO; |
| 3778 |
} |
| 3779 |
|
| 3780 |
return LIBUSB_SUCCESS; |
| 3781 |
} |
| 3782 |
|
| 3783 |
static int winusbx_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint) |
| 3784 |
{ |
| 3785 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3786 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3787 |
HANDLE winusb_handle; |
| 3788 |
int current_interface; |
| 3789 |
|
| 3790 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3791 |
|
| 3792 |
current_interface = interface_by_endpoint(priv, handle_priv, endpoint); |
| 3793 |
if (current_interface < 0) { |
| 3794 |
usbi_err(HANDLE_CTX(dev_handle), "unable to match endpoint to an open interface - cannot clear"); |
| 3795 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3796 |
} |
| 3797 |
|
| 3798 |
usbi_dbg(HANDLE_CTX(dev_handle), "matched endpoint %02X with interface %d", endpoint, current_interface); |
| 3799 |
winusb_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 3800 |
|
| 3801 |
if (!WinUSBX[sub_api].ResetPipe(winusb_handle, endpoint)) { |
| 3802 |
usbi_err(HANDLE_CTX(dev_handle), "ResetPipe failed: %s", windows_error_str(0)); |
| 3803 |
return LIBUSB_ERROR_NO_DEVICE; |
| 3804 |
} |
| 3805 |
|
| 3806 |
return LIBUSB_SUCCESS; |
| 3807 |
} |
| 3808 |
|
| 3809 |
static int winusbx_cancel_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 3810 |
{ |
| 3811 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 3812 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 3813 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 3814 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 3815 |
int current_interface = transfer_priv->interface_number; |
| 3816 |
HANDLE handle; |
| 3817 |
|
| 3818 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3819 |
|
| 3820 |
usbi_dbg(TRANSFER_CTX(transfer), "will use interface %d", current_interface); |
| 3821 |
|
| 3822 |
handle = handle_priv->interface_handle[current_interface].api_handle; |
| 3823 |
if (!WinUSBX[sub_api].AbortPipe(handle, transfer->endpoint)) { |
| 3824 |
usbi_err(TRANSFER_CTX(transfer), "AbortPipe failed: %s", windows_error_str(0)); |
| 3825 |
return LIBUSB_ERROR_NO_DEVICE; |
| 3826 |
} |
| 3827 |
|
| 3828 |
return LIBUSB_SUCCESS; |
| 3829 |
} |
| 3830 |
|
| 3831 |
/* |
| 3832 |
* from the "How to Use WinUSB to Communicate with a USB Device" Microsoft white paper |
| 3833 |
* (http://www.microsoft.com/whdc/connect/usb/winusb_howto.mspx): |
| 3834 |
* "WinUSB does not support host-initiated reset port and cycle port operations" and |
| 3835 |
* IOCTL_INTERNAL_USB_CYCLE_PORT is only available in kernel mode and the |
| 3836 |
* IOCTL_USB_HUB_CYCLE_PORT ioctl was removed from Vista => the best we can do is |
| 3837 |
* cycle the pipes (and even then, the control pipe can not be reset using WinUSB) |
| 3838 |
*/ |
| 3839 |
// TODO: (post hotplug): see if we can force eject the device and redetect it (reuse hotplug?) |
| 3840 |
static int winusbx_reset_device(int sub_api, struct libusb_device_handle *dev_handle) |
| 3841 |
{ |
| 3842 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3843 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 3844 |
HANDLE winusb_handle; |
| 3845 |
int i, j; |
| 3846 |
|
| 3847 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3848 |
|
| 3849 |
// Reset any available pipe (except control). |
| 3850 |
// priv->usb_interface[].endpoint and .nb_endpoints are written under |
| 3851 |
// interface_lock by claim/altsetting and the enumeration setup paths; |
| 3852 |
// take the lock so we don't read a freed endpoint pointer or an |
| 3853 |
// inconsistent (nb_endpoints, endpoint[]) pair. |
| 3854 |
usbi_mutex_lock(&priv->interface_lock); |
| 3855 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 3856 |
winusb_handle = handle_priv->interface_handle[i].api_handle; |
| 3857 |
if (HANDLE_VALID(winusb_handle)) { |
| 3858 |
for (j = 0; j < priv->usb_interface[i].nb_endpoints; j++) { |
| 3859 |
usbi_dbg(HANDLE_CTX(dev_handle), "resetting ep %02X", priv->usb_interface[i].endpoint[j]); |
| 3860 |
if (!WinUSBX[sub_api].AbortPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) |
| 3861 |
usbi_err(HANDLE_CTX(dev_handle), "AbortPipe (pipe address %02X) failed: %s", |
| 3862 |
priv->usb_interface[i].endpoint[j], windows_error_str(0)); |
| 3863 |
|
| 3864 |
// FlushPipe seems to fail on OUT pipes |
| 3865 |
if (IS_EPIN(priv->usb_interface[i].endpoint[j]) |
| 3866 |
&& (!WinUSBX[sub_api].FlushPipe(winusb_handle, priv->usb_interface[i].endpoint[j]))) |
| 3867 |
usbi_err(HANDLE_CTX(dev_handle), "FlushPipe (pipe address %02X) failed: %s", |
| 3868 |
priv->usb_interface[i].endpoint[j], windows_error_str(0)); |
| 3869 |
|
| 3870 |
if (!WinUSBX[sub_api].ResetPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) |
| 3871 |
usbi_err(HANDLE_CTX(dev_handle), "ResetPipe (pipe address %02X) failed: %s", |
| 3872 |
priv->usb_interface[i].endpoint[j], windows_error_str(0)); |
| 3873 |
} |
| 3874 |
} |
| 3875 |
} |
| 3876 |
usbi_mutex_unlock(&priv->interface_lock); |
| 3877 |
|
| 3878 |
// libusbK & libusb0 have the ability to issue an actual device reset |
| 3879 |
if ((sub_api != SUB_API_WINUSB) && (WinUSBX[sub_api].ResetDevice != NULL)) { |
| 3880 |
winusb_handle = handle_priv->interface_handle[0].api_handle; |
| 3881 |
if (HANDLE_VALID(winusb_handle)) |
| 3882 |
WinUSBX[sub_api].ResetDevice(winusb_handle); |
| 3883 |
} |
| 3884 |
|
| 3885 |
return LIBUSB_SUCCESS; |
| 3886 |
} |
| 3887 |
|
| 3888 |
static enum libusb_transfer_status winusbx_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, DWORD length) |
| 3889 |
{ |
| 3890 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 3891 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 3892 |
int i; |
| 3893 |
|
| 3894 |
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) { |
| 3895 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 3896 |
|
| 3897 |
if (sub_api == SUB_API_NOTSET) |
| 3898 |
sub_api = priv->sub_api; |
| 3899 |
if (WinUSBX[sub_api].hDll == NULL) |
| 3900 |
return LIBUSB_TRANSFER_ERROR; |
| 3901 |
|
| 3902 |
// for isochronous, need to copy the individual iso packet actual_lengths and statuses |
| 3903 |
if ((sub_api == SUB_API_LIBUSBK) || (sub_api == SUB_API_LIBUSB0)) { |
| 3904 |
// iso only supported on libusbk-based backends for now |
| 3905 |
PKISO_CONTEXT iso_context = transfer_priv->iso_context; |
| 3906 |
for (i = 0; i < transfer->num_iso_packets; i++) { |
| 3907 |
if (IS_XFERIN(transfer)) { |
| 3908 |
transfer->iso_packet_desc[i].actual_length = iso_context->IsoPackets[i].actual_length; |
| 3909 |
} else { |
| 3910 |
// On Windows the usbd Length field is not used for OUT transfers. |
| 3911 |
// Copy the requested value back for consistency with other platforms. |
| 3912 |
transfer->iso_packet_desc[i].actual_length = transfer->iso_packet_desc[i].length; |
| 3913 |
} |
| 3914 |
transfer->iso_packet_desc[i].status = usbd_status_to_libusb_transfer_status(iso_context->IsoPackets[i].status); |
| 3915 |
} |
| 3916 |
} else if (sub_api == SUB_API_WINUSB) { |
| 3917 |
if (IS_XFERIN(transfer)) { |
| 3918 |
/* Convert isochronous packet descriptor between Windows and libusb representation. |
| 3919 |
* Both representation are guaranteed to have the same length in bytes.*/ |
| 3920 |
PUSBD_ISO_PACKET_DESCRIPTOR usbd_iso_packet_desc = (PUSBD_ISO_PACKET_DESCRIPTOR)transfer->iso_packet_desc; |
| 3921 |
for (i = 0; i < transfer->num_iso_packets; i++) { |
| 3922 |
unsigned int packet_length = (i < transfer->num_iso_packets - 1) ? (usbd_iso_packet_desc[i + 1].Offset - usbd_iso_packet_desc[i].Offset) : usbd_iso_packet_desc[i].Length; |
| 3923 |
unsigned int actual_length = usbd_iso_packet_desc[i].Length; |
| 3924 |
USBD_STATUS status = usbd_iso_packet_desc[i].Status; |
| 3925 |
|
| 3926 |
transfer->iso_packet_desc[i].length = packet_length; |
| 3927 |
transfer->iso_packet_desc[i].actual_length = actual_length; |
| 3928 |
transfer->iso_packet_desc[i].status = usbd_status_to_libusb_transfer_status(status); |
| 3929 |
} |
| 3930 |
} else { |
| 3931 |
for (i = 0; i < transfer->num_iso_packets; i++) { |
| 3932 |
transfer->iso_packet_desc[i].status = LIBUSB_TRANSFER_COMPLETED; |
| 3933 |
// On Windows the usbd Length field is not used for OUT transfers. |
| 3934 |
// Copy the requested value back for consistency with other platforms. |
| 3935 |
transfer->iso_packet_desc[i].actual_length = transfer->iso_packet_desc[i].length; |
| 3936 |
} |
| 3937 |
} |
| 3938 |
} else { |
| 3939 |
// This should only occur if backend is not set correctly or other backend isoc is partially implemented |
| 3940 |
PRINT_UNSUPPORTED_API(copy_transfer_data); |
| 3941 |
return LIBUSB_TRANSFER_ERROR; |
| 3942 |
} |
| 3943 |
} |
| 3944 |
|
| 3945 |
itransfer->transferred += (int)length; |
| 3946 |
return LIBUSB_TRANSFER_COMPLETED; |
| 3947 |
} |
| 3948 |
|
| 3949 |
static int winusbx_endpoint_supports_raw_io(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint) |
| 3950 |
{ |
| 3951 |
struct libusb_context *ctx; |
| 3952 |
struct winusb_device_handle_priv *handle_priv; |
| 3953 |
struct winusb_device_priv *priv; |
| 3954 |
int interface; |
| 3955 |
HANDLE winusb_handle; |
| 3956 |
|
| 3957 |
ctx = HANDLE_CTX(dev_handle); |
| 3958 |
|
| 3959 |
if (!ctx) { |
| 3960 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 3961 |
} |
| 3962 |
|
| 3963 |
if (endpoint & ~(LIBUSB_ENDPOINT_DIR_MASK | LIBUSB_ENDPOINT_ADDRESS_MASK)) { |
| 3964 |
usbi_err(ctx, "invalid endpoint 0x%X passed for RAW_IO support query", endpoint); |
| 3965 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 3966 |
} |
| 3967 |
|
| 3968 |
if (!(endpoint & LIBUSB_ENDPOINT_DIR_MASK)) { |
| 3969 |
usbi_err(ctx, "endpoint 0x%02X is OUT not IN for RAW_IO support query", endpoint); |
| 3970 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 3971 |
} |
| 3972 |
|
| 3973 |
handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 3974 |
priv = usbi_get_device_priv(dev_handle->dev); |
| 3975 |
interface = interface_by_endpoint(priv, handle_priv, (uint8_t) endpoint); |
| 3976 |
|
| 3977 |
if (interface < 0) { |
| 3978 |
usbi_err(ctx, "unable to match endpoint 0x%02X to an open interface for RAW_IO support query", endpoint); |
| 3979 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3980 |
} |
| 3981 |
|
| 3982 |
usbi_dbg(ctx, "matched endpoint 0x%02X to interface %d for RAW_IO support query", endpoint, interface); |
| 3983 |
|
| 3984 |
if (priv->usb_interface[interface].apib->id != USB_API_WINUSBX) { |
| 3985 |
usbi_err(ctx, "interface %d is not managed by WinUSB, cannot query RAW_IO support", interface); |
| 3986 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 3987 |
} |
| 3988 |
|
| 3989 |
winusb_handle = handle_priv->interface_handle[interface].api_handle; |
| 3990 |
|
| 3991 |
if (!HANDLE_VALID(winusb_handle)) { |
| 3992 |
usbi_err(HANDLE_CTX(dev_handle), "WinUSB handle not valid for interface %d, cannot query RAW_IO support", interface); |
| 3993 |
return LIBUSB_ERROR_NOT_FOUND; |
| 3994 |
} |
| 3995 |
|
| 3996 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 3997 |
|
| 3998 |
// If we made it this far RAW_IO is supported. |
| 3999 |
return 1; |
| 4000 |
} |
| 4001 |
|
| 4002 |
int winusbx_endpoint_set_raw_io(int sub_api, libusb_device_handle* dev_handle, uint8_t endpoint, int enable) |
| 4003 |
{ |
| 4004 |
struct libusb_context *ctx; |
| 4005 |
struct winusb_device_handle_priv *handle_priv; |
| 4006 |
struct winusb_device_priv *priv; |
| 4007 |
UCHAR policy; |
| 4008 |
int interface; |
| 4009 |
HANDLE winusb_handle; |
| 4010 |
|
| 4011 |
ctx = HANDLE_CTX(dev_handle); |
| 4012 |
|
| 4013 |
if (!ctx) { |
| 4014 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4015 |
} |
| 4016 |
|
| 4017 |
if (endpoint & ~(LIBUSB_ENDPOINT_DIR_MASK | LIBUSB_ENDPOINT_ADDRESS_MASK)) { |
| 4018 |
usbi_err(ctx, "invalid endpoint 0x%X passed, cannot set RAW_IO", endpoint); |
| 4019 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4020 |
} |
| 4021 |
|
| 4022 |
if (!(endpoint & LIBUSB_ENDPOINT_DIR_MASK)) { |
| 4023 |
usbi_err(ctx, "endpoint 0x%02X is OUT not IN, cannot set RAW_IO", endpoint); |
| 4024 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4025 |
} |
| 4026 |
|
| 4027 |
handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4028 |
priv = usbi_get_device_priv(dev_handle->dev); |
| 4029 |
interface = interface_by_endpoint(priv, handle_priv, (uint8_t) endpoint); |
| 4030 |
|
| 4031 |
if (interface < 0) { |
| 4032 |
usbi_err(ctx, "unable to match endpoint 0x%02X to an open interface for RAW_IO", endpoint); |
| 4033 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4034 |
} |
| 4035 |
|
| 4036 |
usbi_dbg(ctx, "matched endpoint 0x%02X to interface %d", endpoint, interface); |
| 4037 |
|
| 4038 |
if (priv->usb_interface[interface].apib->id != USB_API_WINUSBX) { |
| 4039 |
usbi_err(ctx, "interface %d is not managed by WinUSB, cannot set RAW_IO", interface); |
| 4040 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4041 |
} |
| 4042 |
|
| 4043 |
winusb_handle = handle_priv->interface_handle[interface].api_handle; |
| 4044 |
|
| 4045 |
if (!HANDLE_VALID(winusb_handle)) { |
| 4046 |
usbi_err(HANDLE_CTX(dev_handle), "WinUSB handle not valid for interface %d, cannot set RAW_IO", interface); |
| 4047 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4048 |
} |
| 4049 |
|
| 4050 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 4051 |
|
| 4052 |
policy = enable != 0; |
| 4053 |
|
| 4054 |
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, (UCHAR) endpoint, |
| 4055 |
RAW_IO, sizeof(UCHAR), &policy)) { |
| 4056 |
DWORD error = GetLastError(); |
| 4057 |
usbi_err(ctx, "failed to change RAW_IO for endpoint %02X: %s", endpoint, windows_error_str(error)); |
| 4058 |
switch (error) { |
| 4059 |
case ERROR_INVALID_HANDLE: |
| 4060 |
case ERROR_INVALID_PARAMETER: |
| 4061 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4062 |
case ERROR_NOT_ENOUGH_MEMORY: |
| 4063 |
return LIBUSB_ERROR_NO_MEM; |
| 4064 |
default: |
| 4065 |
return LIBUSB_ERROR_OTHER; |
| 4066 |
} |
| 4067 |
} |
| 4068 |
|
| 4069 |
usbi_dbg(ctx, "%s RAW_IO for endpoint %02X", enable ? "enabled" : "disabled", endpoint); |
| 4070 |
|
| 4071 |
return LIBUSB_SUCCESS; |
| 4072 |
} |
| 4073 |
|
| 4074 |
static int winusbx_get_max_raw_io_transfer_size(int sub_api, struct libusb_device_handle* dev_handle, uint8_t endpoint) |
| 4075 |
{ |
| 4076 |
struct libusb_context *ctx; |
| 4077 |
struct winusb_device_handle_priv *handle_priv; |
| 4078 |
struct winusb_device_priv *priv; |
| 4079 |
int interface; |
| 4080 |
HANDLE winusb_handle; |
| 4081 |
ULONG max_transfer_size = 0; |
| 4082 |
|
| 4083 |
ctx = HANDLE_CTX(dev_handle); |
| 4084 |
|
| 4085 |
if (!ctx) { |
| 4086 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4087 |
} |
| 4088 |
|
| 4089 |
if (endpoint & ~(LIBUSB_ENDPOINT_DIR_MASK | LIBUSB_ENDPOINT_ADDRESS_MASK)) { |
| 4090 |
usbi_err(ctx, "invalid endpoint 0x%X passed, cannot get maximum transfer size for RAW_IO", endpoint); |
| 4091 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4092 |
} |
| 4093 |
|
| 4094 |
handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4095 |
priv = usbi_get_device_priv(dev_handle->dev); |
| 4096 |
interface = interface_by_endpoint(priv, handle_priv, (uint8_t) endpoint); |
| 4097 |
|
| 4098 |
if (interface < 0) { |
| 4099 |
usbi_err(ctx, "unable to match endpoint 0x%02X to an open interface, cannot get maximum transfer size for RAW_IO", endpoint); |
| 4100 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4101 |
} |
| 4102 |
|
| 4103 |
usbi_dbg(ctx, "matched endpoint 0x%02X to interface %d", endpoint, interface); |
| 4104 |
|
| 4105 |
if (priv->usb_interface[interface].apib->id != USB_API_WINUSBX) { |
| 4106 |
usbi_err(ctx, "interface %d is not managed by WinUSB, cannot get maximum transfer size for RAW_IO", interface); |
| 4107 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4108 |
} |
| 4109 |
|
| 4110 |
winusb_handle = handle_priv->interface_handle[interface].api_handle; |
| 4111 |
|
| 4112 |
if (!HANDLE_VALID(winusb_handle)) { |
| 4113 |
usbi_err(HANDLE_CTX(dev_handle), "WinUSB handle not valid for interface %d, cannot get maximum transfer size for RAW_IO", interface); |
| 4114 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4115 |
} |
| 4116 |
|
| 4117 |
CHECK_WINUSBX_AVAILABLE(sub_api); |
| 4118 |
|
| 4119 |
ULONG size = sizeof(ULONG); |
| 4120 |
if (!WinUSBX[sub_api].GetPipePolicy(winusb_handle, (UCHAR) endpoint, |
| 4121 |
MAXIMUM_TRANSFER_SIZE, &size, &max_transfer_size)) { |
| 4122 |
DWORD error = GetLastError(); |
| 4123 |
usbi_err(ctx, "failed to get RAW_IO maximum transfer size for endpoint 0x%02X: %s", endpoint, windows_error_str(error)); |
| 4124 |
switch (error) { |
| 4125 |
case ERROR_INVALID_HANDLE: |
| 4126 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4127 |
default: |
| 4128 |
return LIBUSB_ERROR_OTHER; |
| 4129 |
} |
| 4130 |
} |
| 4131 |
|
| 4132 |
usbi_dbg(ctx, "maximum transfer size for endpoint 0x%02X is %lu", endpoint, max_transfer_size); |
| 4133 |
|
| 4134 |
return (int)max_transfer_size; |
| 4135 |
} |
| 4136 |
|
| 4137 |
/* |
| 4138 |
* Internal HID Support functions (from libusb-win32) |
| 4139 |
* Note that functions that complete data transfer synchronously must return |
| 4140 |
* LIBUSB_COMPLETED instead of LIBUSB_SUCCESS |
| 4141 |
*/ |
| 4142 |
static int _hid_get_hid_descriptor(struct hid_device_priv *dev, void *data, size_t *size); |
| 4143 |
static int _hid_get_report_descriptor(struct hid_device_priv *dev, void *data, size_t *size); |
| 4144 |
|
| 4145 |
static int _hid_wcslen(WCHAR *str) |
| 4146 |
{ |
| 4147 |
int i = 0; |
| 4148 |
|
| 4149 |
while (str[i] && (str[i] != 0x409)) |
| 4150 |
i++; |
| 4151 |
|
| 4152 |
return i; |
| 4153 |
} |
| 4154 |
|
| 4155 |
static int _hid_get_device_descriptor(struct libusb_device *dev, struct hid_device_priv *hid_priv, void *data, size_t *size) |
| 4156 |
{ |
| 4157 |
struct libusb_device_descriptor d; |
| 4158 |
|
| 4159 |
/* Copy some values from the cached device descriptor |
| 4160 |
* because we cannot get them through HID */ |
| 4161 |
d.bLength = LIBUSB_DT_DEVICE_SIZE; |
| 4162 |
d.bDescriptorType = LIBUSB_DT_DEVICE; |
| 4163 |
d.bcdUSB = dev->device_descriptor.bcdUSB; |
| 4164 |
d.bDeviceClass = dev->device_descriptor.bDeviceClass; |
| 4165 |
d.bDeviceSubClass = dev->device_descriptor.bDeviceSubClass; |
| 4166 |
d.bDeviceProtocol = dev->device_descriptor.bDeviceProtocol; |
| 4167 |
d.bMaxPacketSize0 = dev->device_descriptor.bMaxPacketSize0; |
| 4168 |
d.idVendor = (uint16_t)hid_priv->vid; |
| 4169 |
d.idProduct = (uint16_t)hid_priv->pid; |
| 4170 |
d.bcdDevice = dev->device_descriptor.bcdDevice; |
| 4171 |
d.iManufacturer = hid_priv->string_index[0]; |
| 4172 |
d.iProduct = hid_priv->string_index[1]; |
| 4173 |
d.iSerialNumber = hid_priv->string_index[2]; |
| 4174 |
d.bNumConfigurations = dev->device_descriptor.bNumConfigurations; |
| 4175 |
|
| 4176 |
if (*size > LIBUSB_DT_DEVICE_SIZE) |
| 4177 |
*size = LIBUSB_DT_DEVICE_SIZE; |
| 4178 |
memcpy(data, &d, *size); |
| 4179 |
|
| 4180 |
return LIBUSB_COMPLETED; |
| 4181 |
} |
| 4182 |
|
| 4183 |
static int _hid_get_config_descriptor(struct hid_device_priv *hid_priv, void *data, size_t *size) |
| 4184 |
{ |
| 4185 |
char num_endpoints = 0; |
| 4186 |
size_t config_total_len = 0; |
| 4187 |
char tmp[HID_MAX_CONFIG_DESC_SIZE]; |
| 4188 |
struct libusb_config_descriptor *cd; |
| 4189 |
struct libusb_interface_descriptor *id; |
| 4190 |
struct libusb_hid_descriptor *hd; |
| 4191 |
struct libusb_endpoint_descriptor *ed; |
| 4192 |
size_t tmp_size; |
| 4193 |
|
| 4194 |
if (hid_priv->input_report_size) |
| 4195 |
num_endpoints++; |
| 4196 |
if (hid_priv->output_report_size) |
| 4197 |
num_endpoints++; |
| 4198 |
|
| 4199 |
config_total_len = LIBUSB_DT_CONFIG_SIZE + LIBUSB_DT_INTERFACE_SIZE |
| 4200 |
+ LIBUSB_DT_HID_SIZE + num_endpoints * LIBUSB_DT_ENDPOINT_SIZE; |
| 4201 |
|
| 4202 |
cd = (struct libusb_config_descriptor *)tmp; |
| 4203 |
id = (struct libusb_interface_descriptor *)(tmp + LIBUSB_DT_CONFIG_SIZE); |
| 4204 |
hd = (struct libusb_hid_descriptor *)(tmp + LIBUSB_DT_CONFIG_SIZE |
| 4205 |
+ LIBUSB_DT_INTERFACE_SIZE); |
| 4206 |
ed = (struct libusb_endpoint_descriptor *)(tmp + LIBUSB_DT_CONFIG_SIZE |
| 4207 |
+ LIBUSB_DT_INTERFACE_SIZE |
| 4208 |
+ LIBUSB_DT_HID_SIZE); |
| 4209 |
|
| 4210 |
cd->bLength = LIBUSB_DT_CONFIG_SIZE; |
| 4211 |
cd->bDescriptorType = LIBUSB_DT_CONFIG; |
| 4212 |
cd->wTotalLength = (uint16_t)config_total_len; |
| 4213 |
cd->bNumInterfaces = 1; |
| 4214 |
cd->bConfigurationValue = 1; |
| 4215 |
cd->iConfiguration = 0; |
| 4216 |
cd->bmAttributes = 1 << 7; /* bus powered */ |
| 4217 |
cd->MaxPower = 50; |
| 4218 |
|
| 4219 |
id->bLength = LIBUSB_DT_INTERFACE_SIZE; |
| 4220 |
id->bDescriptorType = LIBUSB_DT_INTERFACE; |
| 4221 |
id->bInterfaceNumber = 0; |
| 4222 |
id->bAlternateSetting = 0; |
| 4223 |
id->bNumEndpoints = num_endpoints; |
| 4224 |
id->bInterfaceClass = 3; |
| 4225 |
id->bInterfaceSubClass = 0; |
| 4226 |
id->bInterfaceProtocol = 0; |
| 4227 |
id->iInterface = 0; |
| 4228 |
|
| 4229 |
tmp_size = LIBUSB_DT_HID_SIZE; |
| 4230 |
_hid_get_hid_descriptor(hid_priv, hd, &tmp_size); |
| 4231 |
|
| 4232 |
if (hid_priv->input_report_size) { |
| 4233 |
ed->bLength = LIBUSB_DT_ENDPOINT_SIZE; |
| 4234 |
ed->bDescriptorType = LIBUSB_DT_ENDPOINT; |
| 4235 |
ed->bEndpointAddress = HID_IN_EP; |
| 4236 |
ed->bmAttributes = 3; |
| 4237 |
ed->wMaxPacketSize = hid_priv->input_report_size - 1; |
| 4238 |
ed->bInterval = 10; |
| 4239 |
ed = (struct libusb_endpoint_descriptor *)((char *)ed + LIBUSB_DT_ENDPOINT_SIZE); |
| 4240 |
} |
| 4241 |
|
| 4242 |
if (hid_priv->output_report_size) { |
| 4243 |
ed->bLength = LIBUSB_DT_ENDPOINT_SIZE; |
| 4244 |
ed->bDescriptorType = LIBUSB_DT_ENDPOINT; |
| 4245 |
ed->bEndpointAddress = HID_OUT_EP; |
| 4246 |
ed->bmAttributes = 3; |
| 4247 |
ed->wMaxPacketSize = hid_priv->output_report_size - 1; |
| 4248 |
ed->bInterval = 10; |
| 4249 |
} |
| 4250 |
|
| 4251 |
if (*size > config_total_len) |
| 4252 |
*size = config_total_len; |
| 4253 |
memcpy(data, tmp, *size); |
| 4254 |
|
| 4255 |
return LIBUSB_COMPLETED; |
| 4256 |
} |
| 4257 |
|
| 4258 |
static int _hid_get_string_descriptor(struct hid_device_priv *hid_priv, int _index, |
| 4259 |
void *data, size_t *size, HANDLE hid_handle) |
| 4260 |
{ |
| 4261 |
void *tmp = NULL; |
| 4262 |
WCHAR string[MAX_USB_STRING_LENGTH]; |
| 4263 |
size_t tmp_size = 0; |
| 4264 |
int i; |
| 4265 |
|
| 4266 |
/* language ID, EN-US */ |
| 4267 |
char string_langid[] = {0x09, 0x04}; |
| 4268 |
|
| 4269 |
if (_index == 0) { |
| 4270 |
tmp = string_langid; |
| 4271 |
tmp_size = sizeof(string_langid) + 2; |
| 4272 |
} else { |
| 4273 |
for (i = 0; i < 3; i++) { |
| 4274 |
if (_index == (hid_priv->string_index[i])) { |
| 4275 |
tmp = hid_priv->string[i]; |
| 4276 |
tmp_size = (_hid_wcslen(hid_priv->string[i]) + 1) * sizeof(WCHAR); |
| 4277 |
break; |
| 4278 |
} |
| 4279 |
} |
| 4280 |
|
| 4281 |
if (i == 3) { |
| 4282 |
if (!HidD_GetIndexedString(hid_handle, _index, string, sizeof(string))) |
| 4283 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4284 |
tmp = string; |
| 4285 |
tmp_size = (_hid_wcslen(string) + 1) * sizeof(WCHAR); |
| 4286 |
} |
| 4287 |
} |
| 4288 |
|
| 4289 |
if (!tmp_size) |
| 4290 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4291 |
|
| 4292 |
if (tmp_size < *size) |
| 4293 |
*size = tmp_size; |
| 4294 |
|
| 4295 |
// 2 byte header |
| 4296 |
((uint8_t *)data)[0] = (uint8_t)*size; |
| 4297 |
((uint8_t *)data)[1] = LIBUSB_DT_STRING; |
| 4298 |
memcpy((uint8_t *)data + 2, tmp, *size - 2); |
| 4299 |
|
| 4300 |
return LIBUSB_COMPLETED; |
| 4301 |
} |
| 4302 |
|
| 4303 |
static int _hid_get_hid_descriptor(struct hid_device_priv *hid_priv, void *data, size_t *size) |
| 4304 |
{ |
| 4305 |
struct libusb_hid_descriptor d; |
| 4306 |
uint8_t tmp[MAX_HID_DESCRIPTOR_SIZE]; |
| 4307 |
size_t report_len = MAX_HID_DESCRIPTOR_SIZE; |
| 4308 |
|
| 4309 |
_hid_get_report_descriptor(hid_priv, tmp, &report_len); |
| 4310 |
|
| 4311 |
d.bLength = LIBUSB_DT_HID_SIZE; |
| 4312 |
d.bDescriptorType = LIBUSB_DT_HID; |
| 4313 |
d.bcdHID = 0x0110; /* 1.10 */ |
| 4314 |
d.bCountryCode = 0; |
| 4315 |
d.bNumDescriptors = 1; |
| 4316 |
d.bClassDescriptorType = LIBUSB_DT_REPORT; |
| 4317 |
d.wClassDescriptorLength = (uint16_t)report_len; |
| 4318 |
|
| 4319 |
if (*size > LIBUSB_DT_HID_SIZE) |
| 4320 |
*size = LIBUSB_DT_HID_SIZE; |
| 4321 |
memcpy(data, &d, *size); |
| 4322 |
|
| 4323 |
return LIBUSB_COMPLETED; |
| 4324 |
} |
| 4325 |
|
| 4326 |
static int _hid_get_report_descriptor(struct hid_device_priv *hid_priv, void *data, size_t *size) |
| 4327 |
{ |
| 4328 |
uint8_t d[MAX_HID_DESCRIPTOR_SIZE]; |
| 4329 |
size_t i = 0; |
| 4330 |
|
| 4331 |
/* usage page */ |
| 4332 |
d[i++] = 0x06; d[i++] = hid_priv->usagePage & 0xFF; d[i++] = hid_priv->usagePage >> 8; |
| 4333 |
/* usage */ |
| 4334 |
d[i++] = 0x09; d[i++] = (uint8_t)hid_priv->usage; |
| 4335 |
/* start collection (application) */ |
| 4336 |
d[i++] = 0xA1; d[i++] = 0x01; |
| 4337 |
/* input report */ |
| 4338 |
if (hid_priv->input_report_size) { |
| 4339 |
/* usage (vendor defined) */ |
| 4340 |
d[i++] = 0x09; d[i++] = 0x01; |
| 4341 |
/* logical minimum (0) */ |
| 4342 |
d[i++] = 0x15; d[i++] = 0x00; |
| 4343 |
/* logical maximum (255) */ |
| 4344 |
d[i++] = 0x25; d[i++] = 0xFF; |
| 4345 |
/* report size (8 bits) */ |
| 4346 |
d[i++] = 0x75; d[i++] = 0x08; |
| 4347 |
/* report count */ |
| 4348 |
d[i++] = 0x95; d[i++] = (uint8_t)hid_priv->input_report_size - 1; |
| 4349 |
/* input (data, variable, absolute) */ |
| 4350 |
d[i++] = 0x81; d[i++] = 0x00; |
| 4351 |
} |
| 4352 |
/* output report */ |
| 4353 |
if (hid_priv->output_report_size) { |
| 4354 |
/* usage (vendor defined) */ |
| 4355 |
d[i++] = 0x09; d[i++] = 0x02; |
| 4356 |
/* logical minimum (0) */ |
| 4357 |
d[i++] = 0x15; d[i++] = 0x00; |
| 4358 |
/* logical maximum (255) */ |
| 4359 |
d[i++] = 0x25; d[i++] = 0xFF; |
| 4360 |
/* report size (8 bits) */ |
| 4361 |
d[i++] = 0x75; d[i++] = 0x08; |
| 4362 |
/* report count */ |
| 4363 |
d[i++] = 0x95; d[i++] = (uint8_t)hid_priv->output_report_size - 1; |
| 4364 |
/* output (data, variable, absolute) */ |
| 4365 |
d[i++] = 0x91; d[i++] = 0x00; |
| 4366 |
} |
| 4367 |
/* feature report */ |
| 4368 |
if (hid_priv->feature_report_size) { |
| 4369 |
/* usage (vendor defined) */ |
| 4370 |
d[i++] = 0x09; d[i++] = 0x03; |
| 4371 |
/* logical minimum (0) */ |
| 4372 |
d[i++] = 0x15; d[i++] = 0x00; |
| 4373 |
/* logical maximum (255) */ |
| 4374 |
d[i++] = 0x25; d[i++] = 0xFF; |
| 4375 |
/* report size (8 bits) */ |
| 4376 |
d[i++] = 0x75; d[i++] = 0x08; |
| 4377 |
/* report count */ |
| 4378 |
d[i++] = 0x95; d[i++] = (uint8_t)hid_priv->feature_report_size - 1; |
| 4379 |
/* feature (data, variable, absolute) */ |
| 4380 |
d[i++] = 0xb2; d[i++] = 0x02; d[i++] = 0x01; |
| 4381 |
} |
| 4382 |
|
| 4383 |
/* end collection */ |
| 4384 |
d[i++] = 0xC0; |
| 4385 |
|
| 4386 |
if (*size > i) |
| 4387 |
*size = i; |
| 4388 |
memcpy(data, d, *size); |
| 4389 |
|
| 4390 |
return LIBUSB_COMPLETED; |
| 4391 |
} |
| 4392 |
|
| 4393 |
static int _hid_get_descriptor(struct libusb_device *dev, HANDLE hid_handle, int recipient, |
| 4394 |
int type, int _index, void *data, size_t *size) |
| 4395 |
{ |
| 4396 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 4397 |
UNUSED(recipient); |
| 4398 |
|
| 4399 |
switch (type) { |
| 4400 |
case LIBUSB_DT_DEVICE: |
| 4401 |
usbi_dbg(DEVICE_CTX(dev), "LIBUSB_DT_DEVICE"); |
| 4402 |
return _hid_get_device_descriptor(dev, priv->hid, data, size); |
| 4403 |
case LIBUSB_DT_CONFIG: |
| 4404 |
usbi_dbg(DEVICE_CTX(dev), "LIBUSB_DT_CONFIG"); |
| 4405 |
if (!_index) |
| 4406 |
return _hid_get_config_descriptor(priv->hid, data, size); |
| 4407 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4408 |
case LIBUSB_DT_STRING: |
| 4409 |
usbi_dbg(DEVICE_CTX(dev), "LIBUSB_DT_STRING"); |
| 4410 |
return _hid_get_string_descriptor(priv->hid, _index, data, size, hid_handle); |
| 4411 |
case LIBUSB_DT_HID: |
| 4412 |
usbi_dbg(DEVICE_CTX(dev), "LIBUSB_DT_HID"); |
| 4413 |
if (!_index) |
| 4414 |
return _hid_get_hid_descriptor(priv->hid, data, size); |
| 4415 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4416 |
case LIBUSB_DT_REPORT: |
| 4417 |
usbi_dbg(DEVICE_CTX(dev), "LIBUSB_DT_REPORT"); |
| 4418 |
if (!_index) |
| 4419 |
return _hid_get_report_descriptor(priv->hid, data, size); |
| 4420 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4421 |
case LIBUSB_DT_PHYSICAL: |
| 4422 |
usbi_dbg(DEVICE_CTX(dev), "LIBUSB_DT_PHYSICAL"); |
| 4423 |
if (HidD_GetPhysicalDescriptor(hid_handle, data, (ULONG)*size)) |
| 4424 |
return LIBUSB_COMPLETED; |
| 4425 |
return LIBUSB_ERROR_OTHER; |
| 4426 |
} |
| 4427 |
|
| 4428 |
usbi_warn(DEVICE_CTX(dev), "unsupported"); |
| 4429 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4430 |
} |
| 4431 |
|
| 4432 |
static int _hid_get_report(struct libusb_device *dev, HANDLE hid_handle, int id, void *data, |
| 4433 |
struct winusb_transfer_priv *tp, size_t size, OVERLAPPED *overlapped, int report_type) |
| 4434 |
{ |
| 4435 |
DWORD ioctl_code, expected_size = (DWORD)size; |
| 4436 |
uint8_t *buf; |
| 4437 |
|
| 4438 |
if (tp->hid_buffer != NULL) |
| 4439 |
usbi_err(DEVICE_CTX(dev), "program assertion failed - hid_buffer is not NULL"); |
| 4440 |
|
| 4441 |
if ((size == 0) || (size > MAX_HID_REPORT_SIZE)) { |
| 4442 |
usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size); |
| 4443 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4444 |
} |
| 4445 |
|
| 4446 |
switch (report_type) { |
| 4447 |
case HID_REPORT_TYPE_INPUT: |
| 4448 |
ioctl_code = IOCTL_HID_GET_INPUT_REPORT; |
| 4449 |
break; |
| 4450 |
case HID_REPORT_TYPE_FEATURE: |
| 4451 |
ioctl_code = IOCTL_HID_GET_FEATURE; |
| 4452 |
break; |
| 4453 |
default: |
| 4454 |
usbi_warn(DEVICE_CTX(dev), "unknown HID report type %d", report_type); |
| 4455 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4456 |
} |
| 4457 |
|
| 4458 |
// Add a trailing byte to detect overflows |
| 4459 |
buf = calloc(1, expected_size + 1); |
| 4460 |
if (buf == NULL) |
| 4461 |
return LIBUSB_ERROR_NO_MEM; |
| 4462 |
|
| 4463 |
buf[0] = (uint8_t)id; // Must be set always |
| 4464 |
usbi_dbg(DEVICE_CTX(dev), "report ID: 0x%02X", buf[0]); |
| 4465 |
|
| 4466 |
// NB: The size returned by DeviceIoControl doesn't include report IDs when not in use (0) |
| 4467 |
if (!DeviceIoControl(hid_handle, ioctl_code, buf, expected_size + 1, |
| 4468 |
buf, expected_size + 1, NULL, overlapped)) { |
| 4469 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 4470 |
usbi_err(DEVICE_CTX(dev), "failed to read HID Report: %s", windows_error_str(0)); |
| 4471 |
free(buf); |
| 4472 |
return LIBUSB_ERROR_IO; |
| 4473 |
} |
| 4474 |
} |
| 4475 |
|
| 4476 |
// Asynchronous wait |
| 4477 |
tp->hid_buffer = buf; |
| 4478 |
tp->hid_dest = data; // copy dest, as not necessarily the start of the transfer buffer |
| 4479 |
tp->hid_expected_size = expected_size; |
| 4480 |
|
| 4481 |
return LIBUSB_SUCCESS; |
| 4482 |
} |
| 4483 |
|
| 4484 |
static int _hid_set_report(struct libusb_device *dev, HANDLE hid_handle, int id, void *data, |
| 4485 |
struct winusb_transfer_priv *tp, size_t size, OVERLAPPED *overlapped, int report_type) |
| 4486 |
{ |
| 4487 |
DWORD ioctl_code, write_size = (DWORD)size; |
| 4488 |
// If an id is reported, we must allow MAX_HID_REPORT_SIZE + 1 |
| 4489 |
size_t max_report_size = MAX_HID_REPORT_SIZE + (id ? 1 : 0); |
| 4490 |
uint8_t *buf; |
| 4491 |
|
| 4492 |
if (tp->hid_buffer != NULL) |
| 4493 |
usbi_err(DEVICE_CTX(dev), "program assertion failed - hid_buffer is not NULL"); |
| 4494 |
|
| 4495 |
if ((size == 0) || (size > max_report_size)) { |
| 4496 |
usbi_warn(DEVICE_CTX(dev), "invalid size (%"PRIuPTR")", (uintptr_t)size); |
| 4497 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4498 |
} |
| 4499 |
|
| 4500 |
switch (report_type) { |
| 4501 |
case HID_REPORT_TYPE_OUTPUT: |
| 4502 |
ioctl_code = IOCTL_HID_SET_OUTPUT_REPORT; |
| 4503 |
break; |
| 4504 |
case HID_REPORT_TYPE_FEATURE: |
| 4505 |
ioctl_code = IOCTL_HID_SET_FEATURE; |
| 4506 |
break; |
| 4507 |
default: |
| 4508 |
usbi_warn(DEVICE_CTX(dev), "unknown HID report type %d", report_type); |
| 4509 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4510 |
} |
| 4511 |
|
| 4512 |
usbi_dbg(DEVICE_CTX(dev), "report ID: 0x%02X", id); |
| 4513 |
// When report IDs are not used (i.e. when id == 0), we must add |
| 4514 |
// a null report ID. Otherwise, we just use original data buffer |
| 4515 |
if (id == 0) |
| 4516 |
write_size++; |
| 4517 |
|
| 4518 |
buf = malloc(write_size); |
| 4519 |
if (buf == NULL) |
| 4520 |
return LIBUSB_ERROR_NO_MEM; |
| 4521 |
|
| 4522 |
if (id == 0) { |
| 4523 |
buf[0] = 0; |
| 4524 |
memcpy(buf + 1, data, size); |
| 4525 |
} else { |
| 4526 |
// This seems like a waste, but if we don't duplicate the |
| 4527 |
// data, we'll get issues when freeing hid_buffer |
| 4528 |
memcpy(buf, data, size); |
| 4529 |
if (buf[0] != id) |
| 4530 |
usbi_warn(DEVICE_CTX(dev), "mismatched report ID (data is %02X, parameter is %02X)", buf[0], id); |
| 4531 |
} |
| 4532 |
|
| 4533 |
// NB: The size returned by DeviceIoControl doesn't include report IDs when not in use (0) |
| 4534 |
if (!DeviceIoControl(hid_handle, ioctl_code, buf, write_size, |
| 4535 |
buf, write_size, NULL, overlapped)) { |
| 4536 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 4537 |
usbi_err(DEVICE_CTX(dev), "failed to write HID Output Report: %s", windows_error_str(0)); |
| 4538 |
free(buf); |
| 4539 |
return LIBUSB_ERROR_IO; |
| 4540 |
} |
| 4541 |
} |
| 4542 |
|
| 4543 |
tp->hid_buffer = buf; |
| 4544 |
tp->hid_dest = NULL; |
| 4545 |
return LIBUSB_SUCCESS; |
| 4546 |
} |
| 4547 |
|
| 4548 |
static int _hid_class_request(struct libusb_device *dev, HANDLE hid_handle, int request_type, |
| 4549 |
int request, int value, int _index, void *data, struct winusb_transfer_priv *tp, |
| 4550 |
size_t size, OVERLAPPED *overlapped) |
| 4551 |
{ |
| 4552 |
int report_type = (value >> 8) & 0xFF; |
| 4553 |
int report_id = value & 0xFF; |
| 4554 |
|
| 4555 |
UNUSED(_index); |
| 4556 |
|
| 4557 |
if ((LIBUSB_REQ_RECIPIENT(request_type) != LIBUSB_RECIPIENT_INTERFACE) |
| 4558 |
&& (LIBUSB_REQ_RECIPIENT(request_type) != LIBUSB_RECIPIENT_DEVICE)) |
| 4559 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4560 |
|
| 4561 |
if (LIBUSB_REQ_OUT(request_type) && request == HID_REQ_SET_REPORT) |
| 4562 |
return _hid_set_report(dev, hid_handle, report_id, data, tp, size, overlapped, report_type); |
| 4563 |
|
| 4564 |
if (LIBUSB_REQ_IN(request_type) && request == HID_REQ_GET_REPORT) |
| 4565 |
return _hid_get_report(dev, hid_handle, report_id, data, tp, size, overlapped, report_type); |
| 4566 |
|
| 4567 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4568 |
} |
| 4569 |
|
| 4570 |
/* |
| 4571 |
* HID API functions |
| 4572 |
*/ |
| 4573 |
static bool hid_init(struct libusb_context *ctx) |
| 4574 |
{ |
| 4575 |
DLL_GET_HANDLE(ctx, hid); |
| 4576 |
|
| 4577 |
DLL_LOAD_FUNC(hid, HidD_GetAttributes, true); |
| 4578 |
DLL_LOAD_FUNC(hid, HidD_GetHidGuid, true); |
| 4579 |
DLL_LOAD_FUNC(hid, HidD_GetPreparsedData, true); |
| 4580 |
DLL_LOAD_FUNC(hid, HidD_FreePreparsedData, true); |
| 4581 |
DLL_LOAD_FUNC(hid, HidD_GetManufacturerString, true); |
| 4582 |
DLL_LOAD_FUNC(hid, HidD_GetProductString, true); |
| 4583 |
DLL_LOAD_FUNC(hid, HidD_GetSerialNumberString, true); |
| 4584 |
DLL_LOAD_FUNC(hid, HidD_GetIndexedString, true); |
| 4585 |
DLL_LOAD_FUNC(hid, HidP_GetCaps, true); |
| 4586 |
DLL_LOAD_FUNC(hid, HidD_SetNumInputBuffers, true); |
| 4587 |
DLL_LOAD_FUNC(hid, HidD_GetPhysicalDescriptor, true); |
| 4588 |
DLL_LOAD_FUNC(hid, HidD_FlushQueue, true); |
| 4589 |
DLL_LOAD_FUNC(hid, HidP_GetValueCaps, true); |
| 4590 |
|
| 4591 |
return true; |
| 4592 |
} |
| 4593 |
|
| 4594 |
static void hid_exit(void) |
| 4595 |
{ |
| 4596 |
DLL_FREE_HANDLE(hid); |
| 4597 |
} |
| 4598 |
|
| 4599 |
// NB: open and close must ensure that they only handle interface of |
| 4600 |
// the right API type, as these functions can be called wholesale from |
| 4601 |
// composite_open(), with interfaces belonging to different APIs |
| 4602 |
static int hid_open(int sub_api, struct libusb_device_handle *dev_handle) |
| 4603 |
{ |
| 4604 |
struct libusb_device *dev = dev_handle->dev; |
| 4605 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev); |
| 4606 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4607 |
HIDD_ATTRIBUTES hid_attributes; |
| 4608 |
PHIDP_PREPARSED_DATA preparsed_data = NULL; |
| 4609 |
HIDP_CAPS capabilities; |
| 4610 |
HIDP_VALUE_CAPS *value_caps; |
| 4611 |
HANDLE hid_handle = INVALID_HANDLE_VALUE; |
| 4612 |
int i, j; |
| 4613 |
// report IDs handling |
| 4614 |
ULONG size[3]; |
| 4615 |
int nb_ids[2]; // zero and nonzero report IDs |
| 4616 |
#if defined(ENABLE_LOGGING) |
| 4617 |
const char * const type[3] = {"input", "output", "feature"}; |
| 4618 |
#endif |
| 4619 |
|
| 4620 |
UNUSED(sub_api); |
| 4621 |
CHECK_HID_AVAILABLE; |
| 4622 |
|
| 4623 |
if (priv->hid == NULL) { |
| 4624 |
usbi_err(HANDLE_CTX(dev_handle), "program assertion failed - private HID structure is uninitialized"); |
| 4625 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4626 |
} |
| 4627 |
|
| 4628 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 4629 |
if ((priv->usb_interface[i].path != NULL) |
| 4630 |
&& (priv->usb_interface[i].apib->id == USB_API_HID)) { |
| 4631 |
hid_handle = windows_open(dev_handle, priv->usb_interface[i].path, GENERIC_READ | GENERIC_WRITE); |
| 4632 |
/* |
| 4633 |
* http://www.lvr.com/hidfaq.htm: Why do I receive "Access denied" when attempting to access my HID? |
| 4634 |
* "Windows 2000 and later have exclusive read/write access to HIDs that are configured as a system |
| 4635 |
* keyboards or mice. An application can obtain a handle to a system keyboard or mouse by not |
| 4636 |
* requesting READ or WRITE access with CreateFile. Applications can then use HidD_SetFeature and |
| 4637 |
* HidD_GetFeature (if the device supports Feature reports)." |
| 4638 |
*/ |
| 4639 |
if (hid_handle == INVALID_HANDLE_VALUE) { |
| 4640 |
usbi_warn(HANDLE_CTX(dev_handle), "could not open HID device in R/W mode (keyboard or mouse?) - trying without"); |
| 4641 |
hid_handle = windows_open(dev_handle, priv->usb_interface[i].path, 0); |
| 4642 |
if (hid_handle == INVALID_HANDLE_VALUE) { |
| 4643 |
usbi_err(HANDLE_CTX(dev_handle), "could not open device %s (interface %d): %s", priv->path, i, windows_error_str(0)); |
| 4644 |
switch (GetLastError()) { |
| 4645 |
case ERROR_FILE_NOT_FOUND: // The device was disconnected |
| 4646 |
return LIBUSB_ERROR_NO_DEVICE; |
| 4647 |
case ERROR_ACCESS_DENIED: |
| 4648 |
return LIBUSB_ERROR_ACCESS; |
| 4649 |
default: |
| 4650 |
return LIBUSB_ERROR_IO; |
| 4651 |
} |
| 4652 |
} |
| 4653 |
priv->usb_interface[i].restricted_functionality = true; |
| 4654 |
} |
| 4655 |
handle_priv->interface_handle[i].api_handle = hid_handle; |
| 4656 |
} |
| 4657 |
} |
| 4658 |
|
| 4659 |
hid_attributes.Size = sizeof(hid_attributes); |
| 4660 |
do { |
| 4661 |
if (!HidD_GetAttributes(hid_handle, &hid_attributes)) { |
| 4662 |
usbi_err(HANDLE_CTX(dev_handle), "could not gain access to HID top collection (HidD_GetAttributes)"); |
| 4663 |
break; |
| 4664 |
} |
| 4665 |
|
| 4666 |
priv->hid->vid = hid_attributes.VendorID; |
| 4667 |
priv->hid->pid = hid_attributes.ProductID; |
| 4668 |
|
| 4669 |
// Set the maximum available input buffer size |
| 4670 |
for (i = 32; HidD_SetNumInputBuffers(hid_handle, i); i *= 2); |
| 4671 |
usbi_dbg(HANDLE_CTX(dev_handle), "set maximum input buffer size to %d", i / 2); |
| 4672 |
|
| 4673 |
// Get the maximum input and output report size |
| 4674 |
if (!HidD_GetPreparsedData(hid_handle, &preparsed_data) || !preparsed_data) { |
| 4675 |
usbi_err(HANDLE_CTX(dev_handle), "could not read HID preparsed data (HidD_GetPreparsedData)"); |
| 4676 |
break; |
| 4677 |
} |
| 4678 |
if (HidP_GetCaps(preparsed_data, &capabilities) != HIDP_STATUS_SUCCESS) { |
| 4679 |
usbi_err(HANDLE_CTX(dev_handle), "could not parse HID capabilities (HidP_GetCaps)"); |
| 4680 |
break; |
| 4681 |
} |
| 4682 |
|
| 4683 |
// Find out if interrupt will need report IDs |
| 4684 |
size[0] = capabilities.NumberInputValueCaps; |
| 4685 |
size[1] = capabilities.NumberOutputValueCaps; |
| 4686 |
size[2] = capabilities.NumberFeatureValueCaps; |
| 4687 |
for (j = HidP_Input; j <= HidP_Feature; j++) { |
| 4688 |
usbi_dbg(HANDLE_CTX(dev_handle), "%lu HID %s report value(s) found", ULONG_CAST(size[j]), type[j]); |
| 4689 |
priv->hid->uses_report_ids[j] = false; |
| 4690 |
if (size[j] > 0) { |
| 4691 |
value_caps = calloc(size[j], sizeof(HIDP_VALUE_CAPS)); |
| 4692 |
if ((value_caps != NULL) |
| 4693 |
&& (HidP_GetValueCaps((HIDP_REPORT_TYPE)j, value_caps, &size[j], preparsed_data) == HIDP_STATUS_SUCCESS) |
| 4694 |
&& (size[j] >= 1)) { |
| 4695 |
nb_ids[0] = 0; |
| 4696 |
nb_ids[1] = 0; |
| 4697 |
for (i = 0; i < (int)size[j]; i++) { |
| 4698 |
usbi_dbg(HANDLE_CTX(dev_handle), " Report ID: 0x%02X", value_caps[i].ReportID); |
| 4699 |
if (value_caps[i].ReportID != 0) |
| 4700 |
nb_ids[1]++; |
| 4701 |
else |
| 4702 |
nb_ids[0]++; |
| 4703 |
} |
| 4704 |
if (nb_ids[1] != 0) { |
| 4705 |
if (nb_ids[0] != 0) |
| 4706 |
usbi_warn(HANDLE_CTX(dev_handle), "program assertion failed - zero and nonzero report IDs used for %s", |
| 4707 |
type[j]); |
| 4708 |
priv->hid->uses_report_ids[j] = true; |
| 4709 |
} |
| 4710 |
} else { |
| 4711 |
usbi_warn(HANDLE_CTX(dev_handle), " could not process %s report IDs", type[j]); |
| 4712 |
} |
| 4713 |
free(value_caps); |
| 4714 |
} |
| 4715 |
} |
| 4716 |
|
| 4717 |
// Set the report sizes |
| 4718 |
priv->hid->input_report_size = capabilities.InputReportByteLength; |
| 4719 |
priv->hid->output_report_size = capabilities.OutputReportByteLength; |
| 4720 |
priv->hid->feature_report_size = capabilities.FeatureReportByteLength; |
| 4721 |
|
| 4722 |
// Store usage and usagePage values |
| 4723 |
priv->hid->usage = capabilities.Usage; |
| 4724 |
priv->hid->usagePage = capabilities.UsagePage; |
| 4725 |
|
| 4726 |
// Fetch string descriptors |
| 4727 |
priv->hid->string_index[0] = dev->device_descriptor.iManufacturer; |
| 4728 |
if (priv->hid->string_index[0] != 0) |
| 4729 |
HidD_GetManufacturerString(hid_handle, priv->hid->string[0], sizeof(priv->hid->string[0])); |
| 4730 |
else |
| 4731 |
priv->hid->string[0][0] = 0; |
| 4732 |
|
| 4733 |
priv->hid->string_index[1] = dev->device_descriptor.iProduct; |
| 4734 |
if (priv->hid->string_index[1] != 0) |
| 4735 |
// Using HidD_GetIndexedString() instead of HidD_GetProductString(), as the latter would otherwise return the name |
| 4736 |
// of the interface instead of the iProduct string whenever the iInterface member of the USB_INTERFACE_DESCRIPTOR |
| 4737 |
// structure for the interface is nonzero (see Remarks section in the documentation of the HID API routines) |
| 4738 |
HidD_GetIndexedString(hid_handle, priv->hid->string_index[1], priv->hid->string[1], sizeof(priv->hid->string[1])); |
| 4739 |
else |
| 4740 |
priv->hid->string[1][0] = 0; |
| 4741 |
|
| 4742 |
priv->hid->string_index[2] = dev->device_descriptor.iSerialNumber; |
| 4743 |
if (priv->hid->string_index[2] != 0) |
| 4744 |
HidD_GetSerialNumberString(hid_handle, priv->hid->string[2], sizeof(priv->hid->string[2])); |
| 4745 |
else |
| 4746 |
priv->hid->string[2][0] = 0; |
| 4747 |
} while (0); |
| 4748 |
|
| 4749 |
if (preparsed_data) |
| 4750 |
HidD_FreePreparsedData(preparsed_data); |
| 4751 |
|
| 4752 |
return LIBUSB_SUCCESS; |
| 4753 |
} |
| 4754 |
|
| 4755 |
static void hid_close(int sub_api, struct libusb_device_handle *dev_handle) |
| 4756 |
{ |
| 4757 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 4758 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4759 |
HANDLE file_handle; |
| 4760 |
int i; |
| 4761 |
|
| 4762 |
UNUSED(sub_api); |
| 4763 |
|
| 4764 |
if (DLL_HANDLE_NAME(hid) == NULL) |
| 4765 |
return; |
| 4766 |
|
| 4767 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 4768 |
if (priv->usb_interface[i].apib->id == USB_API_HID) { |
| 4769 |
file_handle = handle_priv->interface_handle[i].api_handle; |
| 4770 |
if (HANDLE_VALID(file_handle)) |
| 4771 |
CloseHandle(file_handle); |
| 4772 |
} |
| 4773 |
} |
| 4774 |
} |
| 4775 |
|
| 4776 |
static int hid_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 4777 |
{ |
| 4778 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4779 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 4780 |
|
| 4781 |
UNUSED(sub_api); |
| 4782 |
CHECK_HID_AVAILABLE; |
| 4783 |
|
| 4784 |
// NB: Disconnection detection is not possible in this function |
| 4785 |
if (priv->usb_interface[iface].path == NULL) |
| 4786 |
return LIBUSB_ERROR_NOT_FOUND; // invalid iface |
| 4787 |
|
| 4788 |
// We use dev_handle as a flag for interface claimed |
| 4789 |
if (handle_priv->interface_handle[iface].dev_handle == INTERFACE_CLAIMED) |
| 4790 |
return LIBUSB_ERROR_BUSY; // already claimed |
| 4791 |
|
| 4792 |
handle_priv->interface_handle[iface].dev_handle = INTERFACE_CLAIMED; |
| 4793 |
|
| 4794 |
usbi_dbg(HANDLE_CTX(dev_handle), "claimed interface %u", iface); |
| 4795 |
handle_priv->active_interface = iface; |
| 4796 |
|
| 4797 |
return LIBUSB_SUCCESS; |
| 4798 |
} |
| 4799 |
|
| 4800 |
static int hid_release_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 4801 |
{ |
| 4802 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4803 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 4804 |
|
| 4805 |
UNUSED(sub_api); |
| 4806 |
CHECK_HID_AVAILABLE; |
| 4807 |
|
| 4808 |
if (priv->usb_interface[iface].path == NULL) |
| 4809 |
return LIBUSB_ERROR_NOT_FOUND; // invalid iface |
| 4810 |
|
| 4811 |
if (handle_priv->interface_handle[iface].dev_handle != INTERFACE_CLAIMED) |
| 4812 |
return LIBUSB_ERROR_NOT_FOUND; // invalid iface |
| 4813 |
|
| 4814 |
handle_priv->interface_handle[iface].dev_handle = INVALID_HANDLE_VALUE; |
| 4815 |
|
| 4816 |
return LIBUSB_SUCCESS; |
| 4817 |
} |
| 4818 |
|
| 4819 |
static int hid_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting) |
| 4820 |
{ |
| 4821 |
UNUSED(sub_api); |
| 4822 |
UNUSED(iface); |
| 4823 |
|
| 4824 |
CHECK_HID_AVAILABLE; |
| 4825 |
|
| 4826 |
if (altsetting != 0) { |
| 4827 |
usbi_err(HANDLE_CTX(dev_handle), "set interface altsetting not supported for altsetting >0"); |
| 4828 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4829 |
} |
| 4830 |
|
| 4831 |
return LIBUSB_SUCCESS; |
| 4832 |
} |
| 4833 |
|
| 4834 |
static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 4835 |
{ |
| 4836 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 4837 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 4838 |
struct libusb_device_handle *dev_handle = transfer->dev_handle; |
| 4839 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 4840 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 4841 |
WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *)transfer->buffer; |
| 4842 |
HANDLE hid_handle; |
| 4843 |
OVERLAPPED *overlapped; |
| 4844 |
int current_interface; |
| 4845 |
uint8_t config; |
| 4846 |
size_t size; |
| 4847 |
int r; |
| 4848 |
|
| 4849 |
UNUSED(sub_api); |
| 4850 |
CHECK_HID_AVAILABLE; |
| 4851 |
|
| 4852 |
safe_free(transfer_priv->hid_buffer); |
| 4853 |
transfer_priv->hid_dest = NULL; |
| 4854 |
size = transfer->length - LIBUSB_CONTROL_SETUP_SIZE; |
| 4855 |
|
| 4856 |
if (size > MAX_CTRL_BUFFER_LENGTH) |
| 4857 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 4858 |
|
| 4859 |
current_interface = get_valid_interface(dev_handle, USB_API_HID); |
| 4860 |
if (current_interface < 0) { |
| 4861 |
if (auto_claim(transfer, ¤t_interface, USB_API_HID) != LIBUSB_SUCCESS) |
| 4862 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4863 |
} |
| 4864 |
|
| 4865 |
usbi_dbg(ITRANSFER_CTX(itransfer), "will use interface %d", current_interface); |
| 4866 |
|
| 4867 |
transfer_priv->interface_number = (uint8_t)current_interface; |
| 4868 |
hid_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 4869 |
set_transfer_priv_handle(itransfer, hid_handle); |
| 4870 |
overlapped = get_transfer_priv_overlapped(itransfer); |
| 4871 |
|
| 4872 |
switch (LIBUSB_REQ_TYPE(setup->RequestType)) { |
| 4873 |
case LIBUSB_REQUEST_TYPE_STANDARD: |
| 4874 |
switch (setup->Request) { |
| 4875 |
case LIBUSB_REQUEST_GET_DESCRIPTOR: |
| 4876 |
r = _hid_get_descriptor(dev_handle->dev, hid_handle, LIBUSB_REQ_RECIPIENT(setup->RequestType), |
| 4877 |
(setup->Value >> 8) & 0xFF, setup->Value & 0xFF, transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, &size); |
| 4878 |
break; |
| 4879 |
case LIBUSB_REQUEST_GET_CONFIGURATION: |
| 4880 |
r = winusb_get_configuration(dev_handle, &config); |
| 4881 |
if (r == LIBUSB_SUCCESS) { |
| 4882 |
size = 1; |
| 4883 |
((uint8_t *)transfer->buffer)[LIBUSB_CONTROL_SETUP_SIZE] = config; |
| 4884 |
r = LIBUSB_COMPLETED; |
| 4885 |
} |
| 4886 |
break; |
| 4887 |
case LIBUSB_REQUEST_SET_CONFIGURATION: |
| 4888 |
if (setup->Value == priv->active_config) { |
| 4889 |
r = LIBUSB_COMPLETED; |
| 4890 |
} else { |
| 4891 |
usbi_warn(TRANSFER_CTX(transfer), "cannot set configuration other than the default one"); |
| 4892 |
r = LIBUSB_ERROR_NOT_SUPPORTED; |
| 4893 |
} |
| 4894 |
break; |
| 4895 |
case LIBUSB_REQUEST_GET_INTERFACE: |
| 4896 |
size = 1; |
| 4897 |
((uint8_t *)transfer->buffer)[LIBUSB_CONTROL_SETUP_SIZE] = 0; |
| 4898 |
r = LIBUSB_COMPLETED; |
| 4899 |
break; |
| 4900 |
case LIBUSB_REQUEST_SET_INTERFACE: |
| 4901 |
r = hid_set_interface_altsetting(0, dev_handle, (uint8_t)setup->Index, (uint8_t)setup->Value); |
| 4902 |
if (r == LIBUSB_SUCCESS) |
| 4903 |
r = LIBUSB_COMPLETED; |
| 4904 |
break; |
| 4905 |
default: |
| 4906 |
usbi_warn(TRANSFER_CTX(transfer), "unsupported HID control request"); |
| 4907 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4908 |
} |
| 4909 |
break; |
| 4910 |
case LIBUSB_REQUEST_TYPE_CLASS: |
| 4911 |
r = _hid_class_request(dev_handle->dev, hid_handle, setup->RequestType, setup->Request, setup->Value, |
| 4912 |
setup->Index, transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, transfer_priv, |
| 4913 |
size, overlapped); |
| 4914 |
break; |
| 4915 |
default: |
| 4916 |
usbi_warn(TRANSFER_CTX(transfer), "unsupported HID control request"); |
| 4917 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4918 |
} |
| 4919 |
|
| 4920 |
if (r < 0) |
| 4921 |
return r; |
| 4922 |
|
| 4923 |
if (r == LIBUSB_COMPLETED) { |
| 4924 |
// Force request to be completed synchronously. Transferred size has been set by previous call |
| 4925 |
windows_force_sync_completion(itransfer, (ULONG)size); |
| 4926 |
r = LIBUSB_SUCCESS; |
| 4927 |
} |
| 4928 |
|
| 4929 |
return LIBUSB_SUCCESS; |
| 4930 |
} |
| 4931 |
|
| 4932 |
static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 4933 |
{ |
| 4934 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 4935 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 4936 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 4937 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 4938 |
HANDLE hid_handle; |
| 4939 |
OVERLAPPED *overlapped; |
| 4940 |
bool direction_in; |
| 4941 |
BOOL ret; |
| 4942 |
int current_interface, length; |
| 4943 |
|
| 4944 |
UNUSED(sub_api); |
| 4945 |
CHECK_HID_AVAILABLE; |
| 4946 |
|
| 4947 |
if (IS_XFEROUT(transfer) && (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)) |
| 4948 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 4949 |
|
| 4950 |
transfer_priv->hid_dest = NULL; |
| 4951 |
safe_free(transfer_priv->hid_buffer); |
| 4952 |
|
| 4953 |
current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint); |
| 4954 |
if (current_interface < 0) { |
| 4955 |
usbi_err(TRANSFER_CTX(transfer), "unable to match endpoint to an open interface - cancelling transfer"); |
| 4956 |
return LIBUSB_ERROR_NOT_FOUND; |
| 4957 |
} |
| 4958 |
|
| 4959 |
usbi_dbg(TRANSFER_CTX(transfer), "matched endpoint %02X with interface %d", transfer->endpoint, current_interface); |
| 4960 |
|
| 4961 |
transfer_priv->interface_number = (uint8_t)current_interface; |
| 4962 |
hid_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 4963 |
set_transfer_priv_handle(itransfer, hid_handle); |
| 4964 |
overlapped = get_transfer_priv_overlapped(itransfer); |
| 4965 |
direction_in = IS_XFERIN(transfer); |
| 4966 |
|
| 4967 |
// If report IDs are not in use, an extra prefix byte must be added |
| 4968 |
if (((direction_in) && (!priv->hid->uses_report_ids[0])) |
| 4969 |
|| ((!direction_in) && (!priv->hid->uses_report_ids[1]))) |
| 4970 |
length = transfer->length + 1; |
| 4971 |
else |
| 4972 |
length = transfer->length; |
| 4973 |
|
| 4974 |
// Add a trailing byte to detect overflows on input |
| 4975 |
transfer_priv->hid_buffer = calloc(1, length + 1); |
| 4976 |
if (transfer_priv->hid_buffer == NULL) |
| 4977 |
return LIBUSB_ERROR_NO_MEM; |
| 4978 |
|
| 4979 |
transfer_priv->hid_expected_size = length; |
| 4980 |
|
| 4981 |
if (direction_in) { |
| 4982 |
transfer_priv->hid_dest = transfer->buffer; |
| 4983 |
usbi_dbg(TRANSFER_CTX(transfer), "reading %d bytes (report ID: 0x00)", length); |
| 4984 |
ret = ReadFile(hid_handle, transfer_priv->hid_buffer, length + 1, NULL, overlapped); |
| 4985 |
} else { |
| 4986 |
if (!priv->hid->uses_report_ids[1]) |
| 4987 |
memcpy(transfer_priv->hid_buffer + 1, transfer->buffer, transfer->length); |
| 4988 |
else |
| 4989 |
// We could actually do without the calloc and memcpy in this case |
| 4990 |
memcpy(transfer_priv->hid_buffer, transfer->buffer, transfer->length); |
| 4991 |
|
| 4992 |
usbi_dbg(TRANSFER_CTX(transfer), "writing %d bytes (report ID: 0x%02X)", length, transfer_priv->hid_buffer[0]); |
| 4993 |
ret = WriteFile(hid_handle, transfer_priv->hid_buffer, length, NULL, overlapped); |
| 4994 |
} |
| 4995 |
|
| 4996 |
if (!ret && GetLastError() != ERROR_IO_PENDING) { |
| 4997 |
usbi_err(TRANSFER_CTX(transfer), "HID transfer failed: %s", windows_error_str(0)); |
| 4998 |
safe_free(transfer_priv->hid_buffer); |
| 4999 |
return LIBUSB_ERROR_IO; |
| 5000 |
} |
| 5001 |
|
| 5002 |
return LIBUSB_SUCCESS; |
| 5003 |
} |
| 5004 |
|
| 5005 |
static int hid_reset_device(int sub_api, struct libusb_device_handle *dev_handle) |
| 5006 |
{ |
| 5007 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 5008 |
HANDLE hid_handle; |
| 5009 |
int current_interface; |
| 5010 |
|
| 5011 |
UNUSED(sub_api); |
| 5012 |
CHECK_HID_AVAILABLE; |
| 5013 |
|
| 5014 |
// Flushing the queues on all interfaces is the best we can achieve |
| 5015 |
for (current_interface = 0; current_interface < USB_MAXINTERFACES; current_interface++) { |
| 5016 |
hid_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 5017 |
if (HANDLE_VALID(hid_handle)) |
| 5018 |
HidD_FlushQueue(hid_handle); |
| 5019 |
} |
| 5020 |
|
| 5021 |
return LIBUSB_SUCCESS; |
| 5022 |
} |
| 5023 |
|
| 5024 |
static int hid_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint) |
| 5025 |
{ |
| 5026 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 5027 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5028 |
HANDLE hid_handle; |
| 5029 |
int current_interface; |
| 5030 |
|
| 5031 |
UNUSED(sub_api); |
| 5032 |
CHECK_HID_AVAILABLE; |
| 5033 |
|
| 5034 |
current_interface = interface_by_endpoint(priv, handle_priv, endpoint); |
| 5035 |
if (current_interface < 0) { |
| 5036 |
usbi_err(HANDLE_CTX(dev_handle), "unable to match endpoint to an open interface - cannot clear"); |
| 5037 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5038 |
} |
| 5039 |
|
| 5040 |
usbi_dbg(HANDLE_CTX(dev_handle), "matched endpoint %02X with interface %d", endpoint, current_interface); |
| 5041 |
hid_handle = handle_priv->interface_handle[current_interface].api_handle; |
| 5042 |
|
| 5043 |
// No endpoint selection with Microsoft's implementation, so we try to flush the |
| 5044 |
// whole interface. Should be OK for most case scenarios |
| 5045 |
if (!HidD_FlushQueue(hid_handle)) { |
| 5046 |
usbi_err(HANDLE_CTX(dev_handle), "Flushing of HID queue failed: %s", windows_error_str(0)); |
| 5047 |
// Device was probably disconnected |
| 5048 |
return LIBUSB_ERROR_NO_DEVICE; |
| 5049 |
} |
| 5050 |
|
| 5051 |
return LIBUSB_SUCCESS; |
| 5052 |
} |
| 5053 |
|
| 5054 |
// This extra function is only needed for HID |
| 5055 |
static enum libusb_transfer_status hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, DWORD length) |
| 5056 |
{ |
| 5057 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 5058 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 5059 |
enum libusb_transfer_status r = LIBUSB_TRANSFER_COMPLETED; |
| 5060 |
|
| 5061 |
UNUSED(sub_api); |
| 5062 |
|
| 5063 |
if (transfer_priv->hid_buffer != NULL) { |
| 5064 |
// If we have a valid hid_buffer, it means the transfer was async |
| 5065 |
if (transfer_priv->hid_dest != NULL) { // Data readout |
| 5066 |
if (length > 0) { |
| 5067 |
// First, check for overflow |
| 5068 |
if ((size_t)length > transfer_priv->hid_expected_size) { |
| 5069 |
usbi_err(TRANSFER_CTX(transfer), "OVERFLOW!"); |
| 5070 |
length = (DWORD)transfer_priv->hid_expected_size; |
| 5071 |
r = LIBUSB_TRANSFER_OVERFLOW; |
| 5072 |
} |
| 5073 |
|
| 5074 |
if (transfer_priv->hid_buffer[0] == 0) { |
| 5075 |
memcpy(transfer_priv->hid_dest, transfer_priv->hid_buffer + 1, length); |
| 5076 |
} else { |
| 5077 |
memcpy(transfer_priv->hid_dest, transfer_priv->hid_buffer, length); |
| 5078 |
} |
| 5079 |
} |
| 5080 |
transfer_priv->hid_dest = NULL; |
| 5081 |
} |
| 5082 |
// For write, we just need to free the hid buffer |
| 5083 |
safe_free(transfer_priv->hid_buffer); |
| 5084 |
} |
| 5085 |
|
| 5086 |
itransfer->transferred += (int)length; |
| 5087 |
return r; |
| 5088 |
} |
| 5089 |
|
| 5090 |
|
| 5091 |
/* |
| 5092 |
* Composite API functions |
| 5093 |
*/ |
| 5094 |
static int composite_open(int sub_api, struct libusb_device_handle *dev_handle) |
| 5095 |
{ |
| 5096 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5097 |
int i, r = LIBUSB_ERROR_NOT_FOUND; |
| 5098 |
// SUB_API_MAX + 1 as the SUB_API_MAX pos is used to indicate availability of HID |
| 5099 |
bool available[SUB_API_MAX + 1]; |
| 5100 |
|
| 5101 |
UNUSED(sub_api); |
| 5102 |
|
| 5103 |
for (i = 0; i < SUB_API_MAX + 1; i++) |
| 5104 |
available[i] = false; |
| 5105 |
|
| 5106 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 5107 |
switch (priv->usb_interface[i].apib->id) { |
| 5108 |
case USB_API_WINUSBX: |
| 5109 |
if (priv->usb_interface[i].sub_api != SUB_API_NOTSET) |
| 5110 |
available[priv->usb_interface[i].sub_api] = true; |
| 5111 |
break; |
| 5112 |
case USB_API_HID: |
| 5113 |
available[SUB_API_MAX] = true; |
| 5114 |
break; |
| 5115 |
default: |
| 5116 |
break; |
| 5117 |
} |
| 5118 |
} |
| 5119 |
|
| 5120 |
for (i = 0; i < SUB_API_MAX; i++) { // WinUSB-like drivers |
| 5121 |
if (available[i]) { |
| 5122 |
r = usb_api_backend[USB_API_WINUSBX].open(i, dev_handle); |
| 5123 |
if (r != LIBUSB_SUCCESS) |
| 5124 |
return r; |
| 5125 |
} |
| 5126 |
} |
| 5127 |
|
| 5128 |
if (available[SUB_API_MAX]) { // HID driver |
| 5129 |
r = hid_open(SUB_API_NOTSET, dev_handle); |
| 5130 |
|
| 5131 |
// On Windows 10 version 1903 (OS Build 18362) and later Windows blocks attempts to |
| 5132 |
// open HID devices with a U2F usage unless running as administrator. We ignore this |
| 5133 |
// failure and proceed without the HID device opened. |
| 5134 |
if (r == LIBUSB_ERROR_ACCESS) { |
| 5135 |
usbi_dbg(HANDLE_CTX(dev_handle), "ignoring access denied error while opening HID interface of composite device"); |
| 5136 |
r = LIBUSB_SUCCESS; |
| 5137 |
} |
| 5138 |
} |
| 5139 |
|
| 5140 |
return r; |
| 5141 |
} |
| 5142 |
|
| 5143 |
static void composite_close(int sub_api, struct libusb_device_handle *dev_handle) |
| 5144 |
{ |
| 5145 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5146 |
int i; |
| 5147 |
// SUB_API_MAX + 1 as the SUB_API_MAX pos is used to indicate availability of HID |
| 5148 |
bool available[SUB_API_MAX + 1]; |
| 5149 |
|
| 5150 |
UNUSED(sub_api); |
| 5151 |
|
| 5152 |
for (i = 0; i < SUB_API_MAX + 1; i++) |
| 5153 |
available[i] = false; |
| 5154 |
|
| 5155 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 5156 |
switch (priv->usb_interface[i].apib->id) { |
| 5157 |
case USB_API_WINUSBX: |
| 5158 |
if (priv->usb_interface[i].sub_api != SUB_API_NOTSET) |
| 5159 |
available[priv->usb_interface[i].sub_api] = true; |
| 5160 |
break; |
| 5161 |
case USB_API_HID: |
| 5162 |
available[SUB_API_MAX] = true; |
| 5163 |
break; |
| 5164 |
default: |
| 5165 |
break; |
| 5166 |
} |
| 5167 |
} |
| 5168 |
|
| 5169 |
for (i = 0; i < SUB_API_MAX; i++) { // WinUSB-like drivers |
| 5170 |
if (available[i]) |
| 5171 |
usb_api_backend[USB_API_WINUSBX].close(i, dev_handle); |
| 5172 |
} |
| 5173 |
|
| 5174 |
if (available[SUB_API_MAX]) // HID driver |
| 5175 |
hid_close(SUB_API_NOTSET, dev_handle); |
| 5176 |
} |
| 5177 |
|
| 5178 |
static int composite_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 5179 |
{ |
| 5180 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5181 |
|
| 5182 |
UNUSED(sub_api); |
| 5183 |
CHECK_SUPPORTED_API(priv->usb_interface[iface].apib, claim_interface); |
| 5184 |
|
| 5185 |
return priv->usb_interface[iface].apib-> |
| 5186 |
claim_interface(priv->usb_interface[iface].sub_api, dev_handle, iface); |
| 5187 |
} |
| 5188 |
|
| 5189 |
static int composite_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface, uint8_t altsetting) |
| 5190 |
{ |
| 5191 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5192 |
|
| 5193 |
UNUSED(sub_api); |
| 5194 |
CHECK_SUPPORTED_API(priv->usb_interface[iface].apib, set_interface_altsetting); |
| 5195 |
|
| 5196 |
return priv->usb_interface[iface].apib-> |
| 5197 |
set_interface_altsetting(priv->usb_interface[iface].sub_api, dev_handle, iface, altsetting); |
| 5198 |
} |
| 5199 |
|
| 5200 |
static int composite_release_interface(int sub_api, struct libusb_device_handle *dev_handle, uint8_t iface) |
| 5201 |
{ |
| 5202 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5203 |
|
| 5204 |
UNUSED(sub_api); |
| 5205 |
CHECK_SUPPORTED_API(priv->usb_interface[iface].apib, release_interface); |
| 5206 |
|
| 5207 |
return priv->usb_interface[iface].apib-> |
| 5208 |
release_interface(priv->usb_interface[iface].sub_api, dev_handle, iface); |
| 5209 |
} |
| 5210 |
|
| 5211 |
static int composite_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 5212 |
{ |
| 5213 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 5214 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 5215 |
struct libusb_config_descriptor *conf_desc; |
| 5216 |
WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *)transfer->buffer; |
| 5217 |
int iface, pass, r; |
| 5218 |
|
| 5219 |
UNUSED(sub_api); |
| 5220 |
|
| 5221 |
// Interface shouldn't matter for control, but it does in practice, with Windows' |
| 5222 |
// restrictions with regards to accessing HID keyboards and mice. Try to target |
| 5223 |
// a specific interface first, if possible. |
| 5224 |
switch (LIBUSB_REQ_RECIPIENT(setup->RequestType)) { |
| 5225 |
case LIBUSB_RECIPIENT_INTERFACE: |
| 5226 |
iface = setup->Index & 0xFF; |
| 5227 |
break; |
| 5228 |
case LIBUSB_RECIPIENT_ENDPOINT: |
| 5229 |
r = libusb_get_active_config_descriptor(transfer->dev_handle->dev, &conf_desc); |
| 5230 |
if (r == LIBUSB_SUCCESS) { |
| 5231 |
iface = get_interface_by_endpoint(conf_desc, (setup->Index & 0xFF)); |
| 5232 |
libusb_free_config_descriptor(conf_desc); |
| 5233 |
break; |
| 5234 |
} |
| 5235 |
// No break if not able to determine interface |
| 5236 |
// Fall through |
| 5237 |
default: |
| 5238 |
iface = -1; |
| 5239 |
break; |
| 5240 |
} |
| 5241 |
|
| 5242 |
// Try and target a specific interface if the control setup indicates such |
| 5243 |
if ((iface >= 0) && (iface < USB_MAXINTERFACES)) { |
| 5244 |
usbi_dbg(TRANSFER_CTX(transfer), "attempting control transfer targeted to interface %d", iface); |
| 5245 |
if ((priv->usb_interface[iface].path != NULL) |
| 5246 |
&& (priv->usb_interface[iface].apib->submit_control_transfer != NULL)) { |
| 5247 |
r = priv->usb_interface[iface].apib->submit_control_transfer(priv->usb_interface[iface].sub_api, itransfer); |
| 5248 |
if (r == LIBUSB_SUCCESS) |
| 5249 |
return r; |
| 5250 |
} |
| 5251 |
} |
| 5252 |
|
| 5253 |
// Either not targeted to a specific interface or no luck in doing so. |
| 5254 |
// Try a 2 pass approach with all interfaces. |
| 5255 |
for (pass = 0; pass < 2; pass++) { |
| 5256 |
for (iface = 0; iface < USB_MAXINTERFACES; iface++) { |
| 5257 |
if ((priv->usb_interface[iface].path != NULL) |
| 5258 |
&& (priv->usb_interface[iface].apib->submit_control_transfer != NULL)) { |
| 5259 |
if ((pass == 0) && (priv->usb_interface[iface].restricted_functionality)) { |
| 5260 |
usbi_dbg(TRANSFER_CTX(transfer), "trying to skip restricted interface #%d (HID keyboard or mouse?)", iface); |
| 5261 |
continue; |
| 5262 |
} |
| 5263 |
usbi_dbg(TRANSFER_CTX(transfer), "using interface %d", iface); |
| 5264 |
r = priv->usb_interface[iface].apib->submit_control_transfer(priv->usb_interface[iface].sub_api, itransfer); |
| 5265 |
// If not supported on this API, it may be supported on another, so don't give up yet!! |
| 5266 |
if (r == LIBUSB_ERROR_NOT_SUPPORTED) |
| 5267 |
continue; |
| 5268 |
return r; |
| 5269 |
} |
| 5270 |
} |
| 5271 |
} |
| 5272 |
|
| 5273 |
usbi_err(TRANSFER_CTX(transfer), "no libusb supported interfaces to complete request"); |
| 5274 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5275 |
} |
| 5276 |
|
| 5277 |
static int composite_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 5278 |
{ |
| 5279 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 5280 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 5281 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 5282 |
int current_interface; |
| 5283 |
|
| 5284 |
UNUSED(sub_api); |
| 5285 |
|
| 5286 |
current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint); |
| 5287 |
if (current_interface < 0) { |
| 5288 |
usbi_err(TRANSFER_CTX(transfer), "unable to match endpoint to an open interface - cancelling transfer"); |
| 5289 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5290 |
} |
| 5291 |
|
| 5292 |
CHECK_SUPPORTED_API(priv->usb_interface[current_interface].apib, submit_bulk_transfer); |
| 5293 |
|
| 5294 |
return priv->usb_interface[current_interface].apib-> |
| 5295 |
submit_bulk_transfer(priv->usb_interface[current_interface].sub_api, itransfer); |
| 5296 |
} |
| 5297 |
|
| 5298 |
static int composite_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 5299 |
{ |
| 5300 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 5301 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(transfer->dev_handle); |
| 5302 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 5303 |
int current_interface; |
| 5304 |
|
| 5305 |
UNUSED(sub_api); |
| 5306 |
|
| 5307 |
current_interface = interface_by_endpoint(priv, handle_priv, transfer->endpoint); |
| 5308 |
if (current_interface < 0) { |
| 5309 |
usbi_err(TRANSFER_CTX(transfer), "unable to match endpoint to an open interface - cancelling transfer"); |
| 5310 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5311 |
} |
| 5312 |
|
| 5313 |
CHECK_SUPPORTED_API(priv->usb_interface[current_interface].apib, submit_iso_transfer); |
| 5314 |
|
| 5315 |
return priv->usb_interface[current_interface].apib-> |
| 5316 |
submit_iso_transfer(priv->usb_interface[current_interface].sub_api, itransfer); |
| 5317 |
} |
| 5318 |
|
| 5319 |
static int composite_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint) |
| 5320 |
{ |
| 5321 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 5322 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5323 |
int current_interface; |
| 5324 |
|
| 5325 |
UNUSED(sub_api); |
| 5326 |
|
| 5327 |
current_interface = interface_by_endpoint(priv, handle_priv, endpoint); |
| 5328 |
if (current_interface < 0) { |
| 5329 |
usbi_err(HANDLE_CTX(dev_handle), "unable to match endpoint to an open interface - cannot clear"); |
| 5330 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5331 |
} |
| 5332 |
|
| 5333 |
CHECK_SUPPORTED_API(priv->usb_interface[current_interface].apib, clear_halt); |
| 5334 |
|
| 5335 |
return priv->usb_interface[current_interface].apib-> |
| 5336 |
clear_halt(priv->usb_interface[current_interface].sub_api, dev_handle, endpoint); |
| 5337 |
} |
| 5338 |
|
| 5339 |
static int composite_cancel_transfer(int sub_api, struct usbi_transfer *itransfer) |
| 5340 |
{ |
| 5341 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 5342 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 5343 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 5344 |
int current_interface = transfer_priv->interface_number; |
| 5345 |
|
| 5346 |
UNUSED(sub_api); |
| 5347 |
|
| 5348 |
if ((current_interface < 0) || (current_interface >= USB_MAXINTERFACES)) { |
| 5349 |
usbi_err(TRANSFER_CTX(transfer), "program assertion failed - invalid interface_number"); |
| 5350 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5351 |
} |
| 5352 |
|
| 5353 |
CHECK_SUPPORTED_API(priv->usb_interface[current_interface].apib, cancel_transfer); |
| 5354 |
|
| 5355 |
return priv->usb_interface[current_interface].apib-> |
| 5356 |
cancel_transfer(priv->usb_interface[current_interface].sub_api, itransfer); |
| 5357 |
} |
| 5358 |
|
| 5359 |
static int composite_reset_device(int sub_api, struct libusb_device_handle *dev_handle) |
| 5360 |
{ |
| 5361 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5362 |
int i, r; |
| 5363 |
bool available[SUB_API_MAX]; |
| 5364 |
|
| 5365 |
UNUSED(sub_api); |
| 5366 |
|
| 5367 |
for (i = 0; i < SUB_API_MAX; i++) |
| 5368 |
available[i] = false; |
| 5369 |
|
| 5370 |
for (i = 0; i < USB_MAXINTERFACES; i++) { |
| 5371 |
if ((priv->usb_interface[i].apib->id == USB_API_WINUSBX) |
| 5372 |
&& (priv->usb_interface[i].sub_api != SUB_API_NOTSET)) |
| 5373 |
available[priv->usb_interface[i].sub_api] = true; |
| 5374 |
} |
| 5375 |
|
| 5376 |
for (i = 0; i < SUB_API_MAX; i++) { |
| 5377 |
if (available[i]) { |
| 5378 |
r = usb_api_backend[USB_API_WINUSBX].reset_device(i, dev_handle); |
| 5379 |
if (r != LIBUSB_SUCCESS) |
| 5380 |
return r; |
| 5381 |
} |
| 5382 |
} |
| 5383 |
|
| 5384 |
return LIBUSB_SUCCESS; |
| 5385 |
} |
| 5386 |
|
| 5387 |
static enum libusb_transfer_status composite_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, DWORD length) |
| 5388 |
{ |
| 5389 |
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 5390 |
struct winusb_transfer_priv *transfer_priv = get_winusb_transfer_priv(itransfer); |
| 5391 |
struct winusb_device_priv *priv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 5392 |
int current_interface = transfer_priv->interface_number; |
| 5393 |
|
| 5394 |
UNUSED(sub_api); |
| 5395 |
if (priv->usb_interface[current_interface].apib->copy_transfer_data == NULL) { |
| 5396 |
usbi_err(TRANSFER_CTX(transfer), "program assertion failed - no function to copy transfer data"); |
| 5397 |
return LIBUSB_TRANSFER_ERROR; |
| 5398 |
} |
| 5399 |
|
| 5400 |
return priv->usb_interface[current_interface].apib-> |
| 5401 |
copy_transfer_data(priv->usb_interface[current_interface].sub_api, itransfer, length); |
| 5402 |
} |
| 5403 |
|
| 5404 |
static int composite_endpoint_supports_raw_io(int sub_api, struct libusb_device_handle *dev_handle, |
| 5405 |
uint8_t endpoint) |
| 5406 |
{ |
| 5407 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 5408 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5409 |
int current_interface; |
| 5410 |
|
| 5411 |
UNUSED(sub_api); |
| 5412 |
|
| 5413 |
current_interface = interface_by_endpoint(priv, handle_priv, endpoint); |
| 5414 |
if (current_interface < 0) { |
| 5415 |
usbi_err(HANDLE_CTX(dev_handle), "unable to match endpoint to an open interface, cannot query RAW_IO support"); |
| 5416 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5417 |
} |
| 5418 |
|
| 5419 |
if (priv->usb_interface[current_interface].apib->endpoint_supports_raw_io == NULL) |
| 5420 |
{ |
| 5421 |
usbi_dbg(HANDLE_CTX(dev_handle), "device driver doesn't support RAW_IO support query"); |
| 5422 |
return 0; |
| 5423 |
} |
| 5424 |
|
| 5425 |
return priv->usb_interface[current_interface].apib-> |
| 5426 |
endpoint_supports_raw_io(priv->usb_interface[current_interface].sub_api, dev_handle, endpoint); |
| 5427 |
} |
| 5428 |
|
| 5429 |
static int composite_endpoint_set_raw_io(int sub_api, libusb_device_handle *dev_handle, |
| 5430 |
uint8_t endpoint, int enable) |
| 5431 |
{ |
| 5432 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 5433 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5434 |
int current_interface; |
| 5435 |
|
| 5436 |
UNUSED(sub_api); |
| 5437 |
|
| 5438 |
current_interface = interface_by_endpoint(priv, handle_priv, endpoint); |
| 5439 |
if (current_interface < 0) { |
| 5440 |
usbi_err(HANDLE_CTX(dev_handle), "unable to match endpoint to an open interface, cannot query RAW_IO support"); |
| 5441 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5442 |
} |
| 5443 |
|
| 5444 |
if (priv->usb_interface[current_interface].apib->endpoint_set_raw_io == NULL) |
| 5445 |
{ |
| 5446 |
usbi_dbg(HANDLE_CTX(dev_handle), "device driver doesn't support setting RAW_IO"); |
| 5447 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 5448 |
} |
| 5449 |
|
| 5450 |
return priv->usb_interface[current_interface].apib-> |
| 5451 |
endpoint_set_raw_io(priv->usb_interface[current_interface].sub_api, dev_handle, endpoint, enable); |
| 5452 |
} |
| 5453 |
|
| 5454 |
static int composite_get_max_raw_io_transfer_size(int sub_api, |
| 5455 |
libusb_device_handle *dev_handle, |
| 5456 |
uint8_t endpoint) |
| 5457 |
{ |
| 5458 |
struct winusb_device_handle_priv *handle_priv = get_winusb_device_handle_priv(dev_handle); |
| 5459 |
struct winusb_device_priv *priv = usbi_get_device_priv(dev_handle->dev); |
| 5460 |
int current_interface; |
| 5461 |
|
| 5462 |
UNUSED(sub_api); |
| 5463 |
|
| 5464 |
current_interface = interface_by_endpoint(priv, handle_priv, endpoint); |
| 5465 |
if (current_interface < 0) { |
| 5466 |
usbi_err(HANDLE_CTX(dev_handle), "unable to match endpoint to an open interface - cannot get max RAW_IO transfer size"); |
| 5467 |
return LIBUSB_ERROR_NOT_FOUND; |
| 5468 |
} |
| 5469 |
|
| 5470 |
if (priv->usb_interface[current_interface].apib->get_max_raw_io_transfer_size == NULL) |
| 5471 |
{ |
| 5472 |
usbi_dbg(HANDLE_CTX(dev_handle), "device driver doesn't support querying max RAW_IO transfer size"); |
| 5473 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 5474 |
} |
| 5475 |
|
| 5476 |
return priv->usb_interface[current_interface].apib-> |
| 5477 |
get_max_raw_io_transfer_size(priv->usb_interface[current_interface].sub_api, dev_handle, endpoint); |
| 5478 |
} |