int NiashLibUsbOpen(char const * const pszName, EScannerModel* const peModel) { //Open a handle to the device (FILE_FLAG_OVERLAPPED for WinUSB) s_hDevice = CreateFileA(pszName, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); //s_hDevice = CreateFileA("\\\\.\\Usbscan0", GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if(s_hDevice == INVALID_HANDLE_VALUE) { wprintf_s(L"CreateFile: %s\n", _com_error(GetLastError()).ErrorMessage()); return -1; } //Open a WinUSB handle if(!WinUsb_Initialize(s_hDevice,&s_hWinUSB)) { wprintf_s(L"WinUsb_Initialize: %s\n", _com_error(GetLastError()).ErrorMessage()); return -1; } //Get interface descriptor USB_INTERFACE_DESCRIPTOR USBInterfaceDescriptor; if(!WinUsb_QueryInterfaceSettings(s_hWinUSB,0,&USBInterfaceDescriptor)) { wprintf_s(L"WinUsb_QueryInterfaceSettings: %s\n", _com_error(GetLastError()).ErrorMessage()); return -1; } //Find pipes for(UCHAR i = 0;i < USBInterfaceDescriptor.bNumEndpoints;i++) { WINUSB_PIPE_INFORMATION Pipe; WinUsb_QueryPipe(s_hWinUSB,0,i,&Pipe); if(Pipe.PipeType == UsbdPipeTypeInterrupt) { s_Pipes.Interrupt = Pipe.PipeId; } else if(Pipe.PipeType == UsbdPipeTypeBulk) { if(USB_ENDPOINT_DIRECTION_IN(Pipe.PipeId)) { s_Pipes.BulkIn = Pipe.PipeId; } else { s_Pipes.BulkOut = Pipe.PipeId; } } } if(!s_Pipes.Interrupt || !s_Pipes.BulkIn || !s_Pipes.BulkOut) { puts("Not all required pipes found."); return -1; } assert(s_pScannerModel); *peModel = s_pScannerModel->eModel; return 1; }
BOOL ChinavisionAPI::init(HANDLE threadExit) { //========================================== USB_INTERFACE_DESCRIPTOR ifaceDescriptor; WINUSB_PIPE_INFORMATION pipeInfo; //========================================== m_usbHandle = NULL; m_deviceHandle = NULL; m_inPipeId = 0; m_irCode = 0; m_dataReadyEvent = CreateEvent(NULL,TRUE,FALSE,NULL); m_threadExitEvent = threadExit; findDevice(); if(!_tcslen(m_deviceName)) { return FALSE; // failed to find device name } m_deviceHandle = CreateFile(m_deviceName,GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if(m_deviceHandle==NULL) { return FALSE; } if(!WinUsb_Initialize(m_deviceHandle, &m_usbHandle)) { cleanUp(); return FALSE; } if(!WinUsb_QueryInterfaceSettings(m_usbHandle, 0, &ifaceDescriptor)) { cleanUp(); return FALSE; } for (int i=0; i<ifaceDescriptor.bNumEndpoints; i++) { if(!WinUsb_QueryPipe(m_usbHandle, 0, (UCHAR) i, &pipeInfo)) { cleanUp(); return FALSE; } if (USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) { m_inPipeId = pipeInfo.PipeId; } } if (m_inPipeId==0) { cleanUp(); return FALSE; } m_threadHandle = CreateThread(NULL,0,SZThread,(void *)this,0,NULL); if(m_threadHandle) return TRUE; return FALSE; }
bool AdbWinUsbInterfaceObject::GetEndpointInformation( UCHAR endpoint_index, AdbEndpointInformation* info) { if (!IsOpened()) { SetLastError(ERROR_INVALID_HANDLE); return false; } if (NULL == info) { SetLastError(ERROR_INVALID_PARAMETER); return false; } // Get actual endpoint index for predefined read / write endpoints. if (ADB_QUERY_BULK_READ_ENDPOINT_INDEX == endpoint_index) { endpoint_index = def_read_endpoint_; } else if (ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX == endpoint_index) { endpoint_index = def_write_endpoint_; } // Query endpoint information WINUSB_PIPE_INFORMATION pipe_info; if (!WinUsb_QueryPipe(winusb_handle(), interface_number(), endpoint_index, &pipe_info)) { return false; } // Save endpoint information into output. info->max_packet_size = pipe_info.MaximumPacketSize; info->max_transfer_size = 0xFFFFFFFF; info->endpoint_address = pipe_info.PipeId; info->polling_interval = pipe_info.Interval; info->setting_index = interface_number(); switch (pipe_info.PipeType) { case UsbdPipeTypeControl: info->endpoint_type = AdbEndpointTypeControl; break; case UsbdPipeTypeIsochronous: info->endpoint_type = AdbEndpointTypeIsochronous; break; case UsbdPipeTypeBulk: info->endpoint_type = AdbEndpointTypeBulk; break; case UsbdPipeTypeInterrupt: info->endpoint_type = AdbEndpointTypeInterrupt; break; default: info->endpoint_type = AdbEndpointTypeInvalid; break; } return true; }
BOOL QueryDeviceEndpoints (WINUSB_INTERFACE_HANDLE hDeviceHandle, PIPE_ID* pipeid) { if (hDeviceHandle==INVALID_HANDLE_VALUE) { return FALSE; } BOOL bResult = TRUE; USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; ZeroMemory(&InterfaceDescriptor, sizeof(USB_INTERFACE_DESCRIPTOR)); WINUSB_PIPE_INFORMATION Pipe; ZeroMemory(&Pipe, sizeof(WINUSB_PIPE_INFORMATION)); bResult = WinUsb_QueryInterfaceSettings(hDeviceHandle, 0, &InterfaceDescriptor); if (bResult) { for (int index = 0; index < InterfaceDescriptor.bNumEndpoints; index++) { bResult = WinUsb_QueryPipe(hDeviceHandle, 0, index, &Pipe); if (bResult) { if (Pipe.PipeType == UsbdPipeTypeControl) { printf("Endpoint index: %d Pipe type: Control Pipe ID: %d.\n", index, Pipe.PipeType, Pipe.PipeId); } if (Pipe.PipeType == UsbdPipeTypeIsochronous) { printf("Endpoint index: %d Pipe type: Isochronous Pipe ID: %d.\n", index, Pipe.PipeType, Pipe.PipeId); } if (Pipe.PipeType == UsbdPipeTypeBulk) { if (USB_ENDPOINT_DIRECTION_IN(Pipe.PipeId)) { printf("Endpoint index: %d Pipe type: Bulk IN Pipe ID: %d.\n", index, Pipe.PipeType, Pipe.PipeId); pipeid->PipeInId = Pipe.PipeId; } if (USB_ENDPOINT_DIRECTION_OUT(Pipe.PipeId)) { printf("Endpoint index: %d Pipe type: Bulk OUT Pipe ID: %d.\n", index, Pipe.PipeType, Pipe.PipeId); pipeid->PipeOutId = Pipe.PipeId; } } if (Pipe.PipeType == UsbdPipeTypeInterrupt) { printf("Endpoint index: %d Pipe type: Interrupt Pipe ID: %d.\n", index, Pipe.PipeType, Pipe.PipeId); } } else { continue; } } } //done: return bResult; } // End of QueryDeviceEndpoints()
BOOL WiiMouse_DrvUtil_GetInterfaceHandles(WiiMouse_HandleInfo_t *handleInfo) { BOOL retval = FALSE; UCHAR interfaceIndex = 0; UCHAR pipeIndex = 0; USB_INTERFACE_DESCRIPTOR usbInterfaceDesc; WINUSB_PIPE_INFORMATION pipeInfo; while( WinUsb_QueryInterfaceSettings(handleInfo->winUsbHandle, interfaceIndex, &usbInterfaceDesc) ) { if( _IsValidInterface(&usbInterfaceDesc) ) { break; } interfaceIndex++; } if( GetLastError() == ERROR_INVALID_HANDLE ) { WiiMouse_AddMsg(DERR,"Error, WinUsb_QueryInterfaceSettings invalid handle\n"); return FALSE; } if( !WinUsb_GetAssociatedInterface(handleInfo->winUsbHandle, interfaceIndex, &handleInfo->interfaceHandle) ) { DWORD err = GetLastError(); if( err != ERROR_ALREADY_EXISTS ) { WiiMouse_AddMsg(DERR,"Error, WinUsb_GetAssociatedInterface failed: %d\n", err); return FALSE; } else { handleInfo->interfaceHandle = handleInfo->winUsbHandle; handleInfo->interfaceId = 0; } } handleInfo->interfaceId = interfaceIndex; for( pipeIndex = 0; pipeIndex < usbInterfaceDesc.bNumEndpoints; pipeIndex++ ) { if( !WinUsb_QueryPipe(handleInfo->winUsbHandle, interfaceIndex, pipeIndex, &pipeInfo) ) { WiiMouse_AddMsg(DERR,"Error, WinUsb_QueryPipe failed: %d\n", GetLastError()); retval = FALSE; break; } else { _SetEndpoint(handleInfo, &pipeInfo); } } /* if( !_ValidateEndpoints(handleInfo) ) { WiiMouse_AddMsg(DERR,"Error, endpoint validation failed\n"); retval = FALSE; }*/ if( !retval ) { WinUsb_Free(handleInfo->interfaceHandle); } return retval; }
/*! \brief Open a communcation channel to the given USB port name. */ XsResultValue UsbInterface::open(const XsPortInfo &portInfo, uint32_t, uint32_t) { d->m_endTime = 0; #ifdef USE_WINUSB JLDEBUG(gJournal, "Open usb port " << portInfo.portName().toStdString()); #else JLDEBUG(gJournal, "Open usb port " << portInfo.usbBus() << ":" << portInfo.usbAddress()); #endif if (isOpen()) { JLALERT(gJournal, "Port " << portInfo.portName().toStdString() << " already open"); return (d->m_lastResult = XRV_ALREADYOPEN); } #ifdef USE_WINUSB d->m_deviceHandle = CreateFileA(portInfo.portName().c_str(), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); if (d->m_deviceHandle == INVALID_HANDLE_VALUE) { d->m_deviceHandle = NULL; return (d->m_lastResult = XRV_PORTNOTFOUND); } BOOL result = FALSE; UCHAR speed = 0; ULONG length = 0; USB_INTERFACE_DESCRIPTOR interfaceDescriptor = {0,0,0,0,0,0,0,0,0}; WINUSB_PIPE_INFORMATION pipeInfo; result = WinUsb_Initialize(d->m_deviceHandle, &d->m_usbHandle[0]); if (result) { result = WinUsb_GetAssociatedInterface(d->m_usbHandle[0],0,&d->m_usbHandle[1]); } else { #ifdef XSENS_DEBUG DWORD err = GetLastError(); assert(result); #endif return (d->m_lastResult = XRV_ERROR); } for (int k = 0; k<2;k++) { if(result) { assert(d->m_usbHandle[k] != 0); length = sizeof(UCHAR); result = WinUsb_QueryDeviceInformation(d->m_usbHandle[k], DEVICE_SPEED, &length, &speed); } if(result) { d->m_deviceSpeed = speed; result = WinUsb_QueryInterfaceSettings(d->m_usbHandle[k], 0, &interfaceDescriptor); } if(result) { for(int i=0;i<interfaceDescriptor.bNumEndpoints;i++) { result = WinUsb_QueryPipe(d->m_usbHandle[k], 0, (UCHAR) i, &pipeInfo); if(pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) { d->m_bulkInPipe = pipeInfo.PipeId; d->m_bulkInPipePacketSize = pipeInfo.MaximumPacketSize; } else if(pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId)) { d->m_bulkOutPipe = pipeInfo.PipeId; } else if(pipeInfo.PipeType == UsbdPipeTypeInterrupt) { d->m_interruptPipe = pipeInfo.PipeId; } else { result = FALSE; break; } } } } setTimeout(0); //lint !e534 flushData(); //lint !e534 sprintf(d->m_portname, "%s", portInfo.portName().c_str()); // d->m_offset = 0; ::ResetEvent(&d->m_quitEvent); //lint !e534 d->m_threadHandle = xsStartThread(usbReadThreadFunc, d, &d->m_threadId); if (d->m_threadHandle == XSENS_INVALID_THREAD) { #ifdef XSENS_DEBUG assert(0); #endif return (d->m_lastResult = XRV_ERROR); } #else // !USE_WINUSB static UsbInterfacePrivate::UsbContext contextManager; //m_contextManager; libusb_device **deviceList; ssize_t listLength = libusb_get_device_list(contextManager.m_usbContext /*d->m_contextManager.m_usbContext*/, &deviceList); if (listLength < 0) return d->m_lastResult = d->libusbErrorToXrv((int)listLength); // "USBxxx:yyy" uint8_t bus = XsPortInfo_usbBus(&portInfo); uint8_t address = XsPortInfo_usbAddress(&portInfo); XsResultValue xrv = XRV_OK; int result; libusb_device *device = NULL; for (int i = 0; i < listLength && device == NULL; ++i) { libusb_device *dev = deviceList[i]; if (libusb_get_bus_number(dev) != bus || libusb_get_device_address(dev) != address) continue; libusb_device_descriptor desc; result = libusb_get_device_descriptor(dev, &desc); if (result != LIBUSB_SUCCESS) break; libusb_config_descriptor *configDesc; result = libusb_get_active_config_descriptor(dev, &configDesc); if (result != LIBUSB_SUCCESS) break; d->m_interface = -1; d->m_interfaceCount = configDesc->bNumInterfaces; // find the bulk transfer endpoints for (uint8_t ifCount = 0; ifCount < configDesc->bNumInterfaces && d->m_interface == -1; ++ifCount) { for (uint8_t altsettingCount = 0; altsettingCount < configDesc->interface[ifCount].num_altsetting; altsettingCount++) { const libusb_endpoint_descriptor *endpoints = configDesc->interface[ifCount].altsetting[altsettingCount].endpoint; int inEndpoint = -1, outEndpoint = -1; for (uint8_t i = 0; i < configDesc->interface[ifCount].altsetting[altsettingCount].bNumEndpoints; i++) { if ((endpoints[i].bmAttributes&LIBUSB_TRANSFER_TYPE_MASK) != LIBUSB_TRANSFER_TYPE_BULK) continue; switch (endpoints[i].bEndpointAddress&LIBUSB_ENDPOINT_DIR_MASK) { case LIBUSB_ENDPOINT_IN: inEndpoint = endpoints[i].bEndpointAddress&LIBUSB_ENDPOINT_ADDRESS_MASK; break; case LIBUSB_ENDPOINT_OUT: outEndpoint = endpoints[i].bEndpointAddress&LIBUSB_ENDPOINT_ADDRESS_MASK; break; } } if (outEndpoint == -1 || inEndpoint == -1) continue; d->m_interface = ifCount; d->m_dataOutEndPoint = outEndpoint; d->m_dataInEndPoint = inEndpoint; } } if (d->m_interface == -1) { xrv = XRV_INPUTCANNOTBEOPENED; break; } libusb_free_config_descriptor(configDesc); libusb_ref_device(dev); device = dev; result = LIBUSB_SUCCESS; } libusb_free_device_list(deviceList, 1); if (result != LIBUSB_SUCCESS) { libusb_unref_device(device); return d->m_lastResult = d->libusbErrorToXrv(result); } if (xrv != XRV_OK) { libusb_unref_device(device); return d->m_lastResult = xrv; } libusb_device_handle *handle; result = libusb_open(device, &handle); if (result != LIBUSB_SUCCESS) { libusb_unref_device(device); return d->m_lastResult = d->libusbErrorToXrv(result); } // be rude and claim all interfaces for (int i = 0; i < d->m_interfaceCount; i++) { result = libusb_kernel_driver_active(handle, i); if (result > 0) result = libusb_detach_kernel_driver(handle, i); if (result == LIBUSB_SUCCESS) result = libusb_claim_interface(handle, i); if (result != LIBUSB_SUCCESS) { for (int j = 0; j < i; j++) { while (result != LIBUSB_SUCCESS) { result = libusb_release_interface(handle, j); libusb_attach_kernel_driver(handle, j); } } libusb_close(handle); libusb_unref_device(device); return d->m_lastResult = d->libusbErrorToXrv(result); } } d->m_deviceHandle = handle; sprintf(d->m_portname, "%s", portInfo.portName().c_str()); flushData(); #endif // !USE_WINUSB JLDEBUG(gJournal, "USB Port opened"); return (d->m_lastResult = XRV_OK); }
ADBAPIHANDLE AdbWinUsbInterfaceObject::CreateHandle() { // Open USB device for this inteface Note that WinUsb API // requires the handle to be opened for overlapped I/O. usb_device_handle_ = CreateFile(interface_name().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (INVALID_HANDLE_VALUE == usb_device_handle_) return NULL; // Initialize WinUSB API for this interface if (!WinUsb_Initialize(usb_device_handle_, &winusb_handle_)) return NULL; // Cache current interface number that will be used in // WinUsb_Xxx calls performed on this interface. if (!WinUsb_GetCurrentAlternateSetting(winusb_handle(), &interface_number_)) return false; // Cache interface properties unsigned long bytes_written; // Cache USB device descriptor if (!WinUsb_GetDescriptor(winusb_handle(), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, reinterpret_cast<PUCHAR>(&usb_device_descriptor_), sizeof(usb_device_descriptor_), &bytes_written)) { return false; } // Cache USB configuration descriptor if (!WinUsb_GetDescriptor(winusb_handle(), USB_CONFIGURATION_DESCRIPTOR_TYPE, 0, 0, reinterpret_cast<PUCHAR>(&usb_config_descriptor_), sizeof(usb_config_descriptor_), &bytes_written)) { return false; } // Cache USB interface descriptor if (!WinUsb_QueryInterfaceSettings(winusb_handle(), interface_number(), &usb_interface_descriptor_)) { return false; } // Save indexes and IDs for bulk read / write endpoints. We will use them to // convert ADB_QUERY_BULK_WRITE_ENDPOINT_INDEX and // ADB_QUERY_BULK_READ_ENDPOINT_INDEX into actual endpoint indexes and IDs. for (UCHAR endpoint = 0; endpoint < usb_interface_descriptor_.bNumEndpoints; endpoint++) { // Get endpoint information WINUSB_PIPE_INFORMATION pipe_info; if (!WinUsb_QueryPipe(winusb_handle(), interface_number(), endpoint, &pipe_info)) { return false; } if (UsbdPipeTypeBulk == pipe_info.PipeType) { // This is a bulk endpoint. Cache its index and ID. if (0 != (pipe_info.PipeId & USB_ENDPOINT_DIRECTION_MASK)) { // Use this endpoint as default bulk read endpoint ATLASSERT(0xFF == def_read_endpoint_); def_read_endpoint_ = endpoint; read_endpoint_id_ = pipe_info.PipeId; } else { // Use this endpoint as default bulk write endpoint ATLASSERT(0xFF == def_write_endpoint_); def_write_endpoint_ = endpoint; write_endpoint_id_ = pipe_info.PipeId; } } } return AdbInterfaceObject::CreateHandle(); }
bool QUsbDevice::queryDeviceEndpoints(WINUSB_INTERFACE_HANDLE hWinUSBHandle, QUsbDevice::PIPE_ID *pipeId) { UsbPrintFuncName(); if (hWinUSBHandle==INVALID_HANDLE_VALUE) { return false; } bool bResult = true; if (!WinUsb_SetCurrentAlternateSetting(hWinUSBHandle, mConfig.alternate)) { return false; } if (mDebug) qDebug("Alternate setting: %d.", mConfig.alternate); USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; ZeroMemory(&InterfaceDescriptor, sizeof(USB_INTERFACE_DESCRIPTOR)); WINUSB_PIPE_INFORMATION pipe; ZeroMemory(&pipe, sizeof(WINUSB_PIPE_INFORMATION)); bResult = WinUsb_QueryInterfaceSettings(hWinUSBHandle, mConfig.interface, &InterfaceDescriptor); if (bResult) { for (int index = 0; index < InterfaceDescriptor.bNumEndpoints; index++) { bResult = WinUsb_QueryPipe(hWinUSBHandle, mConfig.interface, index, &pipe); if (bResult) { if (pipe.PipeType == UsbdPipeTypeControl) { if (mDebug) qDebug("Endpoint index: %d Pipe type: Control Pipe ID: 0x%02x.", index, pipe.PipeId); } if (pipe.PipeType == UsbdPipeTypeIsochronous) { if (mDebug) qDebug("Endpoint index: %d Pipe type: Isochronous Pipe ID: 0x%02x.", index, pipe.PipeId); } if (pipe.PipeType == UsbdPipeTypeBulk) { if (USB_ENDPOINT_DIRECTION_IN(pipe.PipeId)) { if (mDebug) qDebug("Bulk IN Endpoint index: %d Pipe type: Bulk Pipe ID: 0x%02x.", index, pipe.PipeId); pipeId->pipeInId = pipe.PipeId; } if (USB_ENDPOINT_DIRECTION_OUT(pipe.PipeId)) { if (mDebug) qDebug("Bulk OUT Endpoint index: %d Pipe type: Bulk Pipe ID: 0x%02x.", index, pipe.PipeId); pipeId->pipeOutId = pipe.PipeId; } } if (pipe.PipeType == UsbdPipeTypeInterrupt) { if (mDebug) qDebug("Endpoint index: %d Pipe type: Interrupt Pipe ID: 0x%02x.", index, pipe.PipeId); } } else { continue; } } } else { return false; } return true; }