#include "StdAfx.h" #include "DppWinUSB.h" CDppWinUSB::CDppWinUSB(void) { WinUsb_Disabled = false; InitDeviceInfo(); NumPipes = -1; CurrentDevice = 1; NumDevices = 0; USB_Default_Timeout = FALSE; // flag that non-default timeout is in use } CDppWinUSB::~CDppWinUSB(void) { } void CDppWinUSB::Disable_WinUsb() { WinUsb_Disabled = true; } void CDppWinUSB::InitDeviceInfo() { devInfo.deviceHandle = INVALID_HANDLE_VALUE; devInfo.winUSBHandle = NULL; devInfo.deviceConnected = FALSE; } void CDppWinUSB::CloseDeviceHandle() { if ((devInfo.deviceHandle != NULL) && (devInfo.deviceHandle != INVALID_HANDLE_VALUE)) { CloseHandle(devInfo.deviceHandle); devInfo.deviceHandle = NULL; } } void CDppWinUSB::CloseWinUsbDevice() { if (WinUsb_Disabled) return; if (devInfo.winUSBHandle) { if (!WinUsb_Free(devInfo.winUSBHandle)) { //DSYSERR("-WinUsb_Free failed"); //MessageBox(NULL,"WinUsb_Free failed","Closing WinUSB",MB_OK); } devInfo.winUSBHandle = NULL; } CloseDeviceHandle(); } BOOL CDppWinUSB::GetDevicePath(LPGUID InterfaceGuid, PCHAR DevicePath, size_t BufLen, DWORD MemberIndex) { BOOL bResult = FALSE; HDEVINFO deviceInfo; SP_DEVICE_INTERFACE_DATA interfaceData; PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL; ULONG length; ULONG requiredLength=0; HRESULT hr; if (WinUsb_Disabled) return FALSE; // get device information handle deviceInfo = SetupDiGetClassDevs(InterfaceGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // ===== Loop here to find multiple devices ==================== // get interface data for device enumeration (first interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); bResult = SetupDiEnumDeviceInterfaces(deviceInfo, NULL, InterfaceGuid, MemberIndex, &interfaceData); //bResult = SetupDiEnumDeviceInterfaces(deviceInfo, NULL, InterfaceGuid, 0, &interfaceData); if(FALSE == bResult) { return FALSE; } // get the details for the current device interface bResult = SetupDiGetDeviceInterfaceDetail(deviceInfo, &interfaceData, NULL, 0, &requiredLength, NULL); detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED, requiredLength); if(NULL == detailData) { SetupDiDestroyDeviceInfoList(deviceInfo); return FALSE; } detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); length = requiredLength; bResult = SetupDiGetDeviceInterfaceDetail(deviceInfo, &interfaceData, detailData, length, &requiredLength, NULL); if(FALSE == bResult) { LocalFree(detailData); return FALSE; } // copy the device path hr = StringCchCopy(DevicePath, BufLen, detailData->DevicePath); if(FAILED(hr)) { SetupDiDestroyDeviceInfoList(deviceInfo); LocalFree(detailData); } LocalFree(detailData); return bResult; } HANDLE CDppWinUSB::OpenDevice(BOOL bSync, DWORD MemberIndex) { HANDLE hDev = NULL; char devicePath[MAX_DEVPATH_LENGTH]; BOOL retVal; if (WinUsb_Disabled) return INVALID_HANDLE_VALUE; retVal = GetDevicePath((LPGUID) &GUID_DP5_INTERFACE_CLASS, devicePath, sizeof(devicePath), MemberIndex); //Error-handling code omitted. if (retVal) { HRESULT hr; hr = StringCchCopy(devInfo.devicePath, sizeof(devicePath), devicePath); } hDev = CreateFile(devicePath, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); //Error-handling code omitted. return hDev; } BOOL CDppWinUSB::Initialize_Device(DWORD MemberIndex) { BOOL bResult; WINUSB_INTERFACE_HANDLE usbHandle; USB_INTERFACE_DESCRIPTOR ifaceDescriptor; WINUSB_PIPE_INFORMATION pipeInfo; UCHAR speed; ULONG length; BYTE bFalse = 0; BYTE bTrue = 1; ULONG pipeTimeout; if (WinUsb_Disabled) return false; pipeTimeout = 500; // 2/4/10 - seems like 500mS should be plenty? //Change timeout for Erase FPGA (4 Sec), and Diagnostic Data (2.5 Sec) commands InitDeviceInfo(); devInfo.deviceHandle = OpenDevice(TRUE, MemberIndex); bResult = WinUsb_Initialize(devInfo.deviceHandle, &usbHandle); //[1] if(bResult) { devInfo.winUSBHandle = usbHandle; length = sizeof(UCHAR); bResult = WinUsb_QueryDeviceInformation(devInfo.winUSBHandle, DEVICE_SPEED, &length, &speed); } //[2] if(bResult) { devInfo.deviceSpeed = speed; bResult = WinUsb_QueryInterfaceSettings(devInfo.winUSBHandle, 0, &ifaceDescriptor); } if(bResult) { NumPipes = ifaceDescriptor.bNumEndpoints; for(int i=0;i