/* * \brief Open a hid device. * * \param vendor the vendor id of the hid device to open. * \param product the product id of the hid device to open. * * \return the identifier of the opened device (to be used in further operations), \ * or -1 in case of failure (e.g. no device found). */ int hidasync_open_ids(unsigned short vendor, unsigned short product) { int ret = -1; GUID guid; HDEVINFO info; DWORD reqd_size; SP_DEVICE_INTERFACE_DATA iface; SP_DEVICE_INTERFACE_DETAIL_DATA *details; int index; HidD_GetHidGuid(&guid); info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if(info != INVALID_HANDLE_VALUE) { for(index = 0; ; ++index) { iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); if(SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface) == FALSE) { break; //no more device } if(SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL) == FALSE) { if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) { continue; } } details = calloc(reqd_size, sizeof(char)); if(details == NULL) { fprintf(stderr, "%s:%d calloc failed\n", __FILE__, __LINE__); continue; } details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); if(SetupDiGetDeviceInterfaceDetail(info, &iface, details, reqd_size, NULL, NULL) == FALSE) { ASYNC_PRINT_ERROR("SetupDiGetDeviceInterfaceDetail") free(details); details = NULL; continue; } int device = open_path(details->DevicePath, 0); free(details); details = NULL; if(device >= 0) { if(devices[device].hidInfo.vendor_id == vendor && devices[device].hidInfo.product_id == product) { ret = device; break; } async_close(device); } } } return ret; }
static HANDLE open_usb_device(int vid, int pid) { GUID guid; HDEVINFO info; DWORD index, required_size; SP_DEVICE_INTERFACE_DATA iface; SP_DEVICE_INTERFACE_DETAIL_DATA *details; HIDD_ATTRIBUTES attrib; HANDLE h; BOOL ret; HidD_GetHidGuid(&guid); info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (info == INVALID_HANDLE_VALUE) return NULL; for (index=0; 1 ;index++) { iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface); if (!ret) { SetupDiDestroyDeviceInfoList(info); break; } SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &required_size, NULL); details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(required_size); if (details == NULL) continue; memset(details, 0, required_size); details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details, required_size, NULL, NULL); if (!ret) { free(details); continue; } h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); free(details); if (h == INVALID_HANDLE_VALUE) continue; attrib.Size = sizeof(HIDD_ATTRIBUTES); ret = HidD_GetAttributes(h, &attrib); if (!ret) { CloseHandle(h); continue; } if (attrib.VendorID != vid || attrib.ProductID != pid) { CloseHandle(h); continue; } SetupDiDestroyDeviceInfoList(info); return h; } return NULL; }
unsigned int USBDeviceFactory_DiscoverDevices( LPSKYETEK_DEVICE** lpDevices ) { GUID hidGuid; HDEVINFO deviceInfo; BOOL isSuccess = FALSE; unsigned long bytes = 0; SP_INTERFACE_DEVICE_DATA deviceData; PSP_INTERFACE_DEVICE_DETAIL_DATA deviceInterfaceData = 0; unsigned int ix, deviceCount = 0; size_t size = 0; SKYETEK_STATUS status; if((lpDevices == NULL) || (*lpDevices != NULL)) return 0; g_usbDevCount = 0; HidD_GetHidGuid (&hidGuid); deviceInfo = SetupDiGetClassDevs ( &hidGuid, 0, 0, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE )); if ( deviceInfo == INVALID_HANDLE_VALUE ) return 0; *lpDevices = (LPSKYETEK_DEVICE*)malloc(20 * sizeof(LPSKYETEK_DEVICE)); memset(*lpDevices,0,(20*sizeof(LPSKYETEK_DEVICE))); size = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); for ( ix = 0; ix < 20; ix++ ) { memset( &deviceData, 0, sizeof(SP_INTERFACE_DEVICE_DATA) ); deviceData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA); isSuccess = SetupDiEnumDeviceInterfaces ( deviceInfo, 0, &hidGuid, ix, &deviceData); if ( !isSuccess ) { if ( ERROR_NO_MORE_ITEMS == GetLastError() ) break; else continue; } SetupDiGetInterfaceDeviceDetail( deviceInfo, &deviceData, 0, 0, &bytes, 0); deviceInterfaceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(bytes * sizeof(BYTE)); if ( !deviceInterfaceData ) { SetupDiDestroyDeviceInfoList( deviceInfo ); return deviceCount; } memset( deviceInterfaceData, 0, bytes ); deviceInterfaceData->cbSize = size; isSuccess = SetupDiGetInterfaceDeviceDetail( deviceInfo, &deviceData, deviceInterfaceData, bytes, &bytes, 0); if ( !isSuccess ) { free(deviceInterfaceData); SetupDiDestroyDeviceInfoList( deviceInfo ); return deviceCount; } if((_tcsstr(deviceInterfaceData->DevicePath, _T("vid_afef")) != NULL) && (_tcsstr(deviceInterfaceData->DevicePath, _T("pid_0f01")) != NULL)) { /*printf("DevicePath = %s\r\n", deviceInterfaceData->DevicePath);*/ status = USBDeviceFactory_CreateDevice(deviceInterfaceData->DevicePath, &((*lpDevices)[deviceCount])); if( status != SKYETEK_SUCCESS ) { free(deviceInterfaceData); continue; } deviceCount++; } free(deviceInterfaceData); } return deviceCount; }
int iKX::init(int device,int ignore_winmm) { int i=0; char string[1024]; int ret=-1; if(device_num!=-1) // double init { strcpy(error_name,"interface already initialized"); debug("iKX init(): already initialized\n"); return -7; } strcpy(error_name,"no error"); // asio init asio_inited=0; last_asio_voice=-1; asio_method=0; asio_iks_property=NULL; asio_secondary_buffer=NULL; asio_prim_buffer=NULL; asio_ds=NULL; pRenderEnumerator=0; pRenderFilter=0; pRenderPin=0; memset(asio_mem_table,0,sizeof(asio_mem_table)); memset(&guid_,0,sizeof(guid_)); device_num=device; HDEVINFO hInfo=SetupDiGetClassDevs((GUID *)&KSCATEGORY_AUDIO,NULL,NULL,DIGCF_DEVICEINTERFACE |DIGCF_PRESENT); if(hInfo==INVALID_HANDLE_VALUE) { debug("iKX init(): setupdigetclassdevs failed [%x]\n",GetLastError()); strcpy(error_name,"error enumerating devices"); return ret; } SP_DEVINFO_DATA devinfo; devinfo.cbSize=sizeof(devinfo); while(SetupDiEnumDeviceInfo(hInfo,i,&devinfo)) { i++; if(SetupDiGetDeviceInstanceId(hInfo,&devinfo,string,sizeof(string),NULL)) { if( (strstr(string,"VEN_1102&DEV_0002")==NULL) && (strstr(string,"VEN_1102&DEV_0004")==NULL) && (strstr(string,"VEN_1102&DEV_0008")==NULL) ) // FIXME: bad test, actually :( { continue; } } else { continue; } SP_DEVICE_INTERFACE_DATA data; data.cbSize=sizeof(data); int j=0; while(SetupDiEnumDeviceInterfaces(hInfo,&devinfo,(GUID *)&KSCATEGORY_AUDIO,j++,&data)==TRUE) { DWORD ReqLen=0; SetupDiGetInterfaceDeviceDetail ( hInfo, &data, NULL, 0, &ReqLen, NULL ); PSP_INTERFACE_DEVICE_DETAIL_DATA m_Detail = PSP_INTERFACE_DEVICE_DETAIL_DATA(new char[ReqLen]); if ( m_Detail ) { m_Detail->cbSize = sizeof SP_INTERFACE_DEVICE_DETAIL_DATA; if(SetupDiGetInterfaceDeviceDetail ( hInfo, &data, m_Detail, ReqLen, &ReqLen, NULL )==TRUE) { if(strstr(m_Detail->DevicePath,"kx_topo1")!=NULL) { HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(mixer_name,tmp_str,sizeof(mixer_name)); } RegCloseKey(key); } strncpy(topo_path,m_Detail->DevicePath,sizeof(topo_path)); } else if(strstr(m_Detail->DevicePath,"kx_wave0")!=NULL) { HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(wave_name,tmp_str,sizeof(wave_name)); strncpy(wave23_name,tmp_str,sizeof(wave_name)); char *p=strstr(wave23_name," 0/1"); if(p) { *p=0; strcpy(wave45_name,wave23_name); strcpy(wave67_name,wave23_name); strcpy(waveHQ_name,wave23_name); strcat(wave23_name," 2/3"); strcat(wave45_name," 4/5"); strcat(wave67_name," 6/7"); strcat(waveHQ_name," HQ"); } else { wave23_name[0]=0; debug("ikX API: not all the 4 wave devices were found... strange\n"); } } RegCloseKey(key); } strncpy(wave_path,m_Detail->DevicePath,sizeof(wave_path)); } else if(strstr(m_Detail->DevicePath,"kx_uart2")!=NULL) { //strncpy(uart2_path,m_Detail->DevicePath,sizeof(uart2_path)); HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(uart2_name,tmp_str,sizeof(uart2_name)); } RegCloseKey(key); } } else if(strstr(m_Detail->DevicePath,"kx_uart1")!=NULL) { //strncpy(uart_path,m_Detail->DevicePath,sizeof(uart_path)); HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(uart_name,tmp_str,sizeof(uart_name)); } RegCloseKey(key); } } else if(strstr(m_Detail->DevicePath,"kx_ctrl")!=NULL) { //strncpy(ctrl_path,m_Detail->DevicePath,sizeof(ctrl_path)); HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(ctrl_name,tmp_str,sizeof(ctrl_name)); } RegCloseKey(key); } } else if(strstr(m_Detail->DevicePath,"kx_synth2")!=NULL) { //strncpy(synth2_path,m_Detail->DevicePath,sizeof(synth2_path)); HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(synth2_name,tmp_str,sizeof(synth2_name)); } RegCloseKey(key); } } else if(strstr(m_Detail->DevicePath,"kx_synth1")!=NULL) { //strncpy(synth_path,m_Detail->DevicePath,sizeof(synth_path)); HKEY key=SetupDiOpenDeviceInterfaceRegKey(hInfo,&data,0,KEY_ALL_ACCESS); if(key!=INVALID_HANDLE_VALUE) { DWORD type; type=REG_SZ; char tmp_str[256]; DWORD len; len=(DWORD)sizeof(tmp_str); if(RegQueryValueEx(key,"FriendlyName",NULL,&type,(unsigned char *)tmp_str,&len)==ERROR_SUCCESS) { strncpy(synth_name,tmp_str,sizeof(synth_name)); } RegCloseKey(key); } } } // if(SetupDiGetInterfaceDeviceDetail) delete m_Detail; } // if detail -- SetupDiGetInterfaceDeviceDetail } // while enum interfaces if(wave_path[0] && topo_path[0]) // have necessary interfaces: { if(device!=0) { device--; // clean-up: mixer_name[0]=0; topo_path[0]=0; wave_name[0]=0; wave23_name[0]=0; wave45_name[0]=0; wave67_name[0]=0; waveHQ_name[0]=0; wave_path[0]=0; synth_name[0]=0; synth2_name[0]=0; uart_name[0]=0; uart2_name[0]=0; ctrl_name[0]=0; continue; } // right device with correct # found: break; } } // enum dev info SetupDiDestroyDeviceInfoList(hInfo); if(wave_path[0]) { hWave = (void *)CreateFile( wave_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); if((HANDLE)hWave == INVALID_HANDLE_VALUE) { debug("iKX init(): error opening wave device [%x]\n",GetLastError()); strcpy(error_name,"error opening wave device"); return GetLastError(); } } if(topo_path[0]) { hTopo = (void *)CreateFile( topo_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); if((HANDLE)hTopo == INVALID_HANDLE_VALUE) { debug("iKX init() error opening topology device [%x]\n",GetLastError()); strcpy(error_name,"error opening topology device"); return GetLastError(); } } if(hTopo && hWave) { char card_name[KX_MAX_STRING]; get_string(KX_STR_CARD_NAME,card_name); if(!mixer_name[0]) sprintf(mixer_name,"kX Mixer %s 0/1",card_name); if(!wave_name[0]) sprintf(wave_name,"kX Wave %s 0/1",card_name); if(!wave23_name[0]) sprintf(wave23_name,"kX Wave %s 2/3",card_name); if(!wave45_name[0]) sprintf(wave45_name,"kX Wave %s 4/5",card_name); if(!wave67_name[0]) sprintf(wave67_name,"kX Wave %s 6/7",card_name); if(!waveHQ_name[0]) sprintf(waveHQ_name,"kX Wave %s HQ",card_name); if(!uart2_name[0]) sprintf(uart2_name,"kX Uart2 %s",card_name); if(!uart_name[0]) sprintf(uart_name,"kX Uart %s",card_name); if(!synth_name[0]) sprintf(synth_name,"kX Synth %s",card_name); if(!synth2_name[0]) sprintf(synth2_name,"kX Synth2 %s",card_name); if(!ctrl_name[0]) sprintf(ctrl_name,"kX Control %s",card_name); ret=init_winmm(); if(!ignore_winmm) { if(ret) { debug("iKX init() error initializing WinMM subsystem [%x]\n",GetLastError()); strcpy(error_name,"error initializing WinMM subsystem"); } } else { debug("iKX init(): ignore_winmm=1; btw, result was: %d [mixer_num=%d %d %d %d]\n",ret,mixer_handler[0],mixer_handler[1],mixer_handler[2],mixer_handler[3]); // if(mixer_handler[0]==-1) // mixer_num[0]=0; } } else { strcpy(error_name,"kX device not found"); } if(ret==0) { // set emulation is_10k2=0; if(get_dword(KX_DWORD_IS_10K2,(dword *)&is_10k2)) { debug("iKX init(): incorrect driver version\n"); strcpy(error_name,"incorrect driver version"); ret=-100; } // set device name if(get_string(KX_STR_CARD_NAME,device_name)) ret=-200; } return ret; }
// // If succeeded, returns the device file of the LANSCSI Bus Enumerator // If failed, return INVALID_HANDLE_VALUE // static HANDLE OpenBusInterface(VOID) { BOOL fSuccess = FALSE; DWORD err = ERROR_SUCCESS; HANDLE hDevice = INVALID_HANDLE_VALUE; HDEVINFO hDevInfoSet = INVALID_HANDLE_VALUE; SP_INTERFACE_DEVICE_DATA devIntfData = {0}; PSP_INTERFACE_DEVICE_DETAIL_DATA devIntfDetailData = NULL; ULONG predictedLength = 0; ULONG requiredLength = 0; hDevInfoSet = SetupDiGetClassDevs ( (LPGUID)&GUID_NDAS_BUS_ENUMERATOR_INTERFACE_CLASS, NULL, // Define no enumerator (global) NULL, // Define no (DIGCF_PRESENT | // Only Devices present DIGCF_INTERFACEDEVICE)); // Function class devices. if (INVALID_HANDLE_VALUE == hDevInfoSet) { DebugPrintErrEx(_T("OpenBusInterface: SetupDiGetClassDevs failed: ")); goto cleanup; } devIntfData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA); fSuccess = SetupDiEnumDeviceInterfaces ( hDevInfoSet, 0, // No care about specific PDOs (LPGUID)&GUID_NDAS_BUS_ENUMERATOR_INTERFACE_CLASS, 0, // &devIntfData); if (!fSuccess) { DebugPrintErrEx(_T("OpenBusInterface: SetupDiEnumDeviceInterfaces failed: ")); if (ERROR_NO_MORE_ITEMS == GetLastError()) { DebugPrint(1, _T("OpenBusInterface: Interface") _T(" GUID_NDAS_BUS_ENUMERATOR_INTERFACE_CLASS is not registered.\n")); } goto cleanup; } fSuccess = SetupDiGetInterfaceDeviceDetail ( hDevInfoSet, &devIntfData, NULL, // probing so no output buffer yet 0, // probing so output buffer length of zero &requiredLength, NULL // not interested in the specific dev-node ); if (!fSuccess && ERROR_INSUFFICIENT_BUFFER != GetLastError()) { DebugPrintErrEx(_T("OpenBusInterface: SetupDiGetInterfaceDeviceDetail failed: ")); goto cleanup; } predictedLength = requiredLength; devIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, predictedLength); devIntfDetailData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA); fSuccess = SetupDiGetInterfaceDeviceDetail( hDevInfoSet, &devIntfData, devIntfDetailData, predictedLength, &requiredLength, NULL); if (!fSuccess) { DebugPrintErrEx(_T("OpenBusInterface: SetupDiGetInterfaceDeviceDetail failed: ")); goto cleanup; } DebugPrint(3, _T("OpenBusInterface: Opening %s\n"), devIntfDetailData->DevicePath); hDevice = CreateFile ( devIntfDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, 0, // FILE_SHARE_READ | FILE_SHARE_WRITE NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, // No special attributes NULL); // No template file if (INVALID_HANDLE_VALUE == hDevice) { DebugPrintErrEx(_T("OpenBusInterface: Device not ready: ")); goto cleanup; } DebugPrint(3, _T("OpenBusInterface: Device file %s opened successfully.\n"), devIntfDetailData->DevicePath); cleanup: err = GetLastError(); if (INVALID_HANDLE_VALUE != hDevInfoSet) { SetupDiDestroyDeviceInfoList(hDevInfoSet); } if (NULL != devIntfDetailData) { HeapFree(GetProcessHeap(), 0, devIntfDetailData); } SetLastError(err); return hDevice; }
static int openUSBDriver(int *file_handle) #endif { #ifdef WIN32 { // Codice per driver windows SP_DEVINFO_DATA devInfoData; SP_DEVICE_INTERFACE_DATA devInterfaceData; PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData; unsigned long requiredLength = 0; int deviceNumber = 0; HDEVINFO hwDeviceInfo = SetupDiGetClassDevs ( (LPGUID) &CAENUSBDRV_GUID, NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); *file_handle = INVALID_HANDLE_VALUE; if (hwDeviceInfo != INVALID_HANDLE_VALUE) { devInterfaceData.cbSize = sizeof(devInterfaceData); if (SetupDiEnumDeviceInterfaces ( hwDeviceInfo, 0, (LPGUID) &CAENUSBDRV_GUID, deviceNumber, &devInterfaceData)) { unsigned long predictedLength = 0; SetupDiGetInterfaceDeviceDetail ( hwDeviceInfo, &devInterfaceData, NULL, 0, &requiredLength, NULL); predictedLength = requiredLength; functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc (predictedLength); functionClassDeviceData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA); devInfoData.cbSize = sizeof(devInfoData); if (SetupDiGetInterfaceDeviceDetail (hwDeviceInfo, &devInterfaceData, functionClassDeviceData, predictedLength, &requiredLength, &devInfoData)) { *file_handle = CreateFile (functionClassDeviceData->DevicePath, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); SetupDiDestroyDeviceInfoList(hwDeviceInfo); } free(functionClassDeviceData); } } } if( file_handle == INVALID_HANDLE_VALUE ) return FALSE; else return TRUE; #else { char devname[80]; sprintf(devname,"/dev/usb/v1718_0"); // HACK : to be generic it should have a parameter board_number *file_handle = open(devname, O_RDWR); if( *file_handle == INVALID_HANDLE_VALUE ) return FALSE; else return TRUE; } #endif }
s_hid_dev * hidasync_enumerate(unsigned short vendor, unsigned short product) { s_hid_dev * hid_devs = NULL; unsigned int nb_hid_devs = 0; GUID guid; HidD_GetHidGuid(&guid); HDEVINFO info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if(info != INVALID_HANDLE_VALUE) { int index; for(index = 0; ; ++index) { SP_DEVICE_INTERFACE_DATA iface; iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); if(SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface) == FALSE) { break; //no more device } DWORD reqd_size; if(SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL) == FALSE) { if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) { continue; } } SP_DEVICE_INTERFACE_DETAIL_DATA * details = calloc(reqd_size, sizeof(char)); if(details == NULL) { fprintf(stderr, "%s:%d calloc failed\n", __FILE__, __LINE__); continue; } details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); if(SetupDiGetDeviceInterfaceDetail(info, &iface, details, reqd_size, NULL, NULL) == FALSE) { ASYNC_PRINT_ERROR("SetupDiGetDeviceInterfaceDetail") free(details); continue; } int device = open_path(details->DevicePath, 0); free(details); if(device >= 0) { if(vendor) { if (devices[device].hidInfo.vendor_id != vendor) { async_close(device); continue; } if(product) { if(devices[device].hidInfo.product_id != product) { async_close(device); continue; } } } char * path = strdup(devices[device].path); if(path == NULL) { PRINT_ERROR_OTHER("strdup failed") async_close(device); continue; } void * ptr = realloc(hid_devs, (nb_hid_devs + 1) * sizeof(*hid_devs)); if(ptr == NULL) { PRINT_ERROR_ALLOC_FAILED("realloc") free(path); async_close(device); continue; } hid_devs = ptr; if(nb_hid_devs > 0) { hid_devs[nb_hid_devs - 1].next = 1; } hid_devs[nb_hid_devs].path = path; hid_devs[nb_hid_devs].vendor_id = devices[device].hidInfo.vendor_id; hid_devs[nb_hid_devs].product_id = devices[device].hidInfo.product_id; hid_devs[nb_hid_devs].next = 0; ++nb_hid_devs; async_close(device); } } } return hid_devs; }
// open - open 1 or more devices // // Inputs: // max = maximum number of devices to open // vid = Vendor ID, or -1 if any // pid = Product ID, or -1 if any // usage_page = top level usage page, or -1 if any // usage = top level usage number, or -1 if any // Output: // actual number of devices opened // int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage) { GUID guid; HDEVINFO info; DWORD index=0, reqd_size; SP_DEVICE_INTERFACE_DATA iface; SP_DEVICE_INTERFACE_DETAIL_DATA *details; HIDD_ATTRIBUTES attrib; PHIDP_PREPARSED_DATA hid_data; HIDP_CAPS capabilities; HANDLE h; BOOL ret; hid_t *hid; int count=0; if (first_hid) free_all_hid(); if (max < 1) return 0; if (!rx_event) { rx_event = CreateEvent(NULL, TRUE, TRUE, NULL); tx_event = CreateEvent(NULL, TRUE, TRUE, NULL); InitializeCriticalSection(&rx_mutex); InitializeCriticalSection(&tx_mutex); } HidD_GetHidGuid(&guid); info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (info == INVALID_HANDLE_VALUE) return 0; for (index=0; 1 ;index++) { iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface); if (!ret) return count; SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL); details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(reqd_size); if (details == NULL) continue; memset(details, 0, reqd_size); details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details, reqd_size, NULL, NULL); if (!ret) { free(details); continue; } h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (h == INVALID_HANDLE_VALUE) { DWORD err = GetLastError(); // I get ERROR_ACCESS_DENIED with most/all my input devices (mice/trackballs/tablet). // Let's not log it :) if (err == ERROR_ACCESS_DENIED) { free(details); continue; } // qDebug wipes the GetLastError() it seems, so do that after print_win32_err(). print_win32_err(err); qDebug() << "Problem opening handle, path: " << QString().fromWCharArray(details->DevicePath); free(details); continue; } free(details); attrib.Size = sizeof(HIDD_ATTRIBUTES); ret = HidD_GetAttributes(h, &attrib); //printf("vid: %4x\n", attrib.VendorID); if (!ret || (vid > 0 && attrib.VendorID != vid) || (pid > 0 && attrib.ProductID != pid) || !HidD_GetPreparsedData(h, &hid_data)) { CloseHandle(h); continue; } if (!HidP_GetCaps(hid_data, &capabilities) || (usage_page > 0 && capabilities.UsagePage != usage_page) || (usage > 0 && capabilities.Usage != usage)) { HidD_FreePreparsedData(hid_data); CloseHandle(h); continue; } HidD_FreePreparsedData(hid_data); hid = (struct hid_struct *)malloc(sizeof(struct hid_struct)); if (!hid) { CloseHandle(h); continue; } // COMMTIMEOUTS CommTimeouts; // CommTimeouts.ReadIntervalTimeout = 100; // 100ms // CommTimeouts.ReadTotalTimeoutConstant = 5; // ms // CommTimeouts.ReadTotalTimeoutMultiplier = 1; // // CommTimeouts.WriteTotalTimeoutConstant = 5; // ms // CommTimeouts.WriteTotalTimeoutMultiplier = 1; // // if (!SetCommTimeouts(h, &CommTimeouts)) // { //// DWORD err = GetLastError(); // // } qDebug("Open: Handle address: %li for num: %i", (long int) h, count); hid->handle = h; add_hid(hid); count++; if (count >= max) return count; } return count; }
// rawhid_open - open 1 or more devices // // Inputs: // max = maximum number of devices to open // vid = Vendor ID, or -1 if any // pid = Product ID, or -1 if any // usage_page = top level usage page, or -1 if any // usage = top level usage number, or -1 if any // Output: // actual number of devices opened // int rawhid_open(int max, int vid, int pid, int usage_page, int usage) { GUID guid; HDEVINFO info; DWORD index=0, reqd_size; SP_DEVICE_INTERFACE_DATA iface; SP_DEVICE_INTERFACE_DETAIL_DATA *details; HIDD_ATTRIBUTES attrib; PHIDP_PREPARSED_DATA hid_data; HIDP_CAPS capabilities; HANDLE h; BOOL ret; hid_t *hid; int count=0; if (first_hid) free_all_hid(); if (max < 1) return 0; if (!rx_event) { rx_event = CreateEvent(NULL, TRUE, TRUE, NULL); tx_event = CreateEvent(NULL, TRUE, TRUE, NULL); InitializeCriticalSection(&rx_mutex); InitializeCriticalSection(&tx_mutex); } HidD_GetHidGuid(&guid); info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (info == INVALID_HANDLE_VALUE) return 0; for (index=0; 1 ;index++) { iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface); if (!ret) return count; SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL); details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(reqd_size); if (details == NULL) continue; memset(details, 0, reqd_size); details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details, reqd_size, NULL, NULL); if (!ret) { free(details); continue; } h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); free(details); if (h == INVALID_HANDLE_VALUE) continue; attrib.Size = sizeof(HIDD_ATTRIBUTES); ret = HidD_GetAttributes(h, &attrib); //printf("vid: %4x\n", attrib.VendorID); if (!ret || (vid > 0 && attrib.VendorID != vid) || (pid > 0 && attrib.ProductID != pid) || !HidD_GetPreparsedData(h, &hid_data)) { CloseHandle(h); continue; } if (!HidP_GetCaps(hid_data, &capabilities) || (usage_page > 0 && capabilities.UsagePage != usage_page) || (usage > 0 && capabilities.Usage != usage)) { HidD_FreePreparsedData(hid_data); CloseHandle(h); continue; } HidD_FreePreparsedData(hid_data); hid = (struct hid_struct *)malloc(sizeof(struct hid_struct)); if (!hid) { CloseHandle(h); continue; } hid->handle = h; hid->open = 1; add_hid(hid); count++; if (count >= max) return count; } return count; }
/** * @brief 打开HID设备 * @param None * @retval None */ HANDLE OpenMyHIDDevice(int overlapped) { HANDLE hidHandle; GUID hidGuid; HidD_GetHidGuid(&hidGuid); HDEVINFO hDevInfo = SetupDiGetClassDevs(&hidGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); if (hDevInfo == INVALID_HANDLE_VALUE) { return INVALID_HANDLE_VALUE; } SP_DEVICE_INTERFACE_DATA devInfoData; devInfoData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA); int deviceNo = 0; SetLastError(NO_ERROR); while (GetLastError() != ERROR_NO_MORE_ITEMS) { if (SetupDiEnumInterfaceDevice (hDevInfo,0,&hidGuid,deviceNo,&devInfoData)) { ULONG requiredLength = 0; SetupDiGetInterfaceDeviceDetail(hDevInfo, &devInfoData, NULL, 0, &requiredLength, NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail = (SP_INTERFACE_DEVICE_DETAIL_DATA*)malloc(requiredLength); devDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if(!SetupDiGetInterfaceDeviceDetail(hDevInfo, &devInfoData, devDetail, requiredLength, NULL, NULL)) { free(devDetail); SetupDiDestroyDeviceInfoList(hDevInfo); return INVALID_HANDLE_VALUE; } if (overlapped) { hidHandle = CreateFile(devDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); } else { hidHandle = CreateFile(devDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if(hidHandle==INVALID_HANDLE_VALUE) hidHandle = CreateFile(devDetail->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); } free(devDetail); if (hidHandle==INVALID_HANDLE_VALUE) { SetupDiDestroyDeviceInfoList(hDevInfo); free(devDetail); return INVALID_HANDLE_VALUE; } _HIDD_ATTRIBUTES hidAttributes; if(!HidD_GetAttributes(hidHandle, &hidAttributes)) { CloseHandle(hidHandle); SetupDiDestroyDeviceInfoList(hDevInfo); return INVALID_HANDLE_VALUE; } if (USB_VID == hidAttributes.VendorID && USB_PID == hidAttributes.ProductID) { printf("找到了我想要的设备,哈哈哈....\n"); break; } else { CloseHandle(hidHandle); ++deviceNo; } } } SetupDiDestroyDeviceInfoList(hDevInfo); return hidHandle; }
static HANDLE OpenBusInterface( VOID ) { HDEVINFO hardwareDeviceInfo; SP_INTERFACE_DEVICE_DATA deviceInterfaceData; HANDLE file; PSP_INTERFACE_DEVICE_DETAIL_DATA deviceInterfaceDetailData = NULL; ULONG predictedLength = 0; ULONG requiredLength = 0; hardwareDeviceInfo = SetupDiGetClassDevs ( (LPGUID)&GUID_LANSCSI_BUS_ENUMERATOR_INTERFACE_CLASS, NULL, // Define no enumerator (global) NULL, // Define no (DIGCF_PRESENT | // Only Devices present DIGCF_INTERFACEDEVICE) // Function class devices. ); if(INVALID_HANDLE_VALUE == hardwareDeviceInfo) { DebugPrint(1, ("[LanscsiLib]OpenBusInterface: SetupDiGetClassDevs failed: 0x%x\n", GetLastError())); return NULL; } deviceInterfaceData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA); if (SetupDiEnumDeviceInterfaces ( hardwareDeviceInfo, 0, // No care about specific PDOs (LPGUID)&GUID_LANSCSI_BUS_ENUMERATOR_INTERFACE_CLASS, 0, // &deviceInterfaceData)) { } else if (ERROR_NO_MORE_ITEMS == GetLastError()) { DebugPrint(1, ("[LanscsiLib]OpenBusInterface: Interface GUID_LANSCSI_BUS_ENUMERATOR_INTERFACE_CLASS is not registered\n")); return NULL; } SetupDiGetInterfaceDeviceDetail ( hardwareDeviceInfo, &deviceInterfaceData, NULL, // probing so no output buffer yet 0, // probing so output buffer length of zero &requiredLength, NULL // not interested in the specific dev-node ); predictedLength = requiredLength; deviceInterfaceDetailData = HeapAlloc(GetProcessHeap(), 0, predictedLength); deviceInterfaceDetailData->cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA); if (! SetupDiGetInterfaceDeviceDetail ( hardwareDeviceInfo, &deviceInterfaceData, deviceInterfaceDetailData, predictedLength, &requiredLength, NULL)) { DebugPrint(1, ("[LanscsiLib]OpenBusInterface: SetupDiGetInterfaceDeviceDetail Error.\n")); HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData); SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); return NULL; } DebugPrint(3, ("[LanscsiLib]OpenBusInterface: Opening %s\n", deviceInterfaceDetailData->DevicePath)); file = CreateFile ( deviceInterfaceDetailData->DevicePath, GENERIC_READ | GENERIC_WRITE, 0, // FILE_SHARE_READ | FILE_SHARE_WRITE NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, // No special attributes NULL); // No template file if (INVALID_HANDLE_VALUE == file) { DebugPrint(1, ("[LanscsiLib]OpenBusInterface: Device not ready: 0x%x", GetLastError())); HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData); SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); return NULL; } DebugPrint(3, ("[LanscsiLib]OpenBusInterface: Bus interface opened!!! %s\n", deviceInterfaceDetailData->DevicePath)); // Clean up. HeapFree(GetProcessHeap(), 0, deviceInterfaceDetailData); SetupDiDestroyDeviceInfoList (hardwareDeviceInfo); return file; }