49 SP_DEVINFO_DATA DeviceInfoData;
50 SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
51 PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = NULL;
52 ULONG requiredLength = 0;
54 HANDLE hnd = INVALID_HANDLE_VALUE;
55 WINUSB_INTERFACE_HANDLE wusbHnd = INVALID_HANDLE_VALUE;
59 hDeviceInfo = SetupDiGetClassDevs(&devGUID, NULL, NULL,
60 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
62 if (hDeviceInfo == INVALID_HANDLE_VALUE) {
63 DBG_ERR(
"SetupDiGetClassDevs failed, error=%lx\n", GetLastError());
68 DeviceInfoData.cbSize =
sizeof(SP_DEVINFO_DATA);
70 for (index = 0; SetupDiEnumDeviceInfo(hDeviceInfo, index, &DeviceInfoData);
73 deviceInterfaceData.cbSize =
sizeof(SP_INTERFACE_DEVICE_DATA);
77 bResult = SetupDiEnumDeviceInterfaces(hDeviceInfo, &DeviceInfoData,
78 &devGUID, 0, &deviceInterfaceData);
82 if (GetLastError() == ERROR_NO_MORE_ITEMS) {
88 DBG_ERR(
"SetupDiEnumDeviceInterfaces(1) failed, error=%lx\n",
98 bResult = SetupDiGetDeviceInterfaceDetail(hDeviceInfo, &deviceInterfaceData,
99 NULL, 0, &requiredLength, NULL);
101 if (!bResult && (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) {
102 DBG_ERR(
"SetupDiEnumDeviceInterfaces(2) failed, error=%lx\n",
108 pInterfaceDetailData = malloc(requiredLength);
109 pInterfaceDetailData->cbSize =
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
113 SetupDiGetDeviceInterfaceDetail(hDeviceInfo, &deviceInterfaceData,
114 pInterfaceDetailData, requiredLength,
115 NULL, &DeviceInfoData);
118 DBG_ERR(
"SetupDiGetDeviceInterfaceDetail failed, error=%lx\n",
124 CreateFile(pInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE,
125 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
126 FILE_FLAG_OVERLAPPED, NULL);
128 free(pInterfaceDetailData);
129 pInterfaceDetailData = NULL;
131 if (hnd == INVALID_HANDLE_VALUE) {
132 DBG_ERR(
"CreateFile failed, error=%lx", GetLastError());
136 if (!WinUsb_Initialize(hnd, &wusbHnd)) {
137 DBG_ERR(
"WinUsb_Initialize failed, error=%lx", GetLastError());
141 if (backend->
serial != 0) {
142 USB_DEVICE_DESCRIPTOR desc;
144 unsigned char ser_num[128] = {0};
146 if (!WinUsb_GetDescriptor(wusbHnd, USB_DEVICE_DESCRIPTOR_TYPE, 0, 0,
147 (
unsigned char *) &desc,
sizeof(desc),
149 DBG_ERR(
"WinUsb_GetDescriptor failed, error=%lx", GetLastError());
153 if (!WinUsb_GetDescriptor(wusbHnd, USB_STRING_DESCRIPTOR_TYPE,
156 ser_num,
sizeof(ser_num), &written)) {
157 DBG_ERR(
"WinUsb_GetDescriptor failed, error=%lx", GetLastError());
161 DBG_INFO(
"Extracted serial %ls (%lu bytes) from device desc %d",
162 (
wchar_t *) (ser_num + 2), written, desc.iSerialNumber);
164 unsigned long devSerial = wcstoul((
wchar_t *) (ser_num + 2), NULL, 10);
165 if (devSerial != backend->
serial) {
166 DBG_INFO(
"Device has serial %lu, not matching searched %lu", devSerial,
173 if (wusbHnd != INVALID_HANDLE_VALUE) {
174 WinUsb_Free(wusbHnd);
175 wusbHnd = INVALID_HANDLE_VALUE;
177 if (hnd != INVALID_HANDLE_VALUE) {
179 hnd = INVALID_HANDLE_VALUE;
184 if (wusbHnd != INVALID_HANDLE_VALUE) {
187 bResult = WinUsb_SetPipePolicy(wusbHnd,
PIPE_OUT, SHORT_PACKET_TERMINATE, 1,
190 DBG_ERR(
"SetPipePolicy failed");
191 WinUsb_Free(wusbHnd);
192 wusbHnd = INVALID_HANDLE_VALUE;
194 hnd = INVALID_HANDLE_VALUE;
204 unsigned long transferred = 0;
205 unsigned long timeout = 10;
207 bResult = WinUsb_SetPipePolicy(wusbHnd,
PIPE_IN, PIPE_TRANSFER_TIMEOUT,
208 sizeof(timeout), &timeout);
210 DBG_ERR(
"SetPipePolicy failed");
211 WinUsb_Free(wusbHnd);
212 wusbHnd = INVALID_HANDLE_VALUE;
214 hnd = INVALID_HANDLE_VALUE;
218 if (WinUsb_ReadPipe(wusbHnd,
PIPE_IN,
buf,
sizeof(
buf), &transferred,
220 DBG_INFO(
"%lu bytes of stale data read", transferred);
224 bResult = WinUsb_SetPipePolicy(wusbHnd,
PIPE_IN, PIPE_TRANSFER_TIMEOUT,
225 sizeof(timeout), &timeout);
227 DBG_ERR(
"SetPipePolicy failed");
228 WinUsb_Free(wusbHnd);
229 wusbHnd = INVALID_HANDLE_VALUE;
231 hnd = INVALID_HANDLE_VALUE;
242 SetupDiDestroyDeviceInfoList(hDeviceInfo);
245 free(pInterfaceDetailData);
247 if (wusbHnd != INVALID_HANDLE_VALUE) {