int USBLink::openDevice() { libusb_device **list = NULL; int i, count = 0; libusb_device *device; libusb_device_descriptor desc; #ifdef __MACOS__ const unsigned int MILLISECONDS_TO_SLEEP = 100; #endif count = libusb_get_device_list(m_context, &list); for (i=0; i<count; i++) { device = list[i]; libusb_get_device_descriptor(device, &desc); const uint8_t device_address = libusb_get_device_address(device); if (desc.idVendor==PIXY_VID && desc.idProduct==PIXY_PID) { if (libusb_open(device, &m_handle)==0) { #ifdef __MACOS__ libusb_reset_device(m_handle); usleep(MILLISECONDS_TO_SLEEP * 1000); #endif set_mutex_.lock(); if ((devices_in_use_.find(device_address) != devices_in_use_.cend()) || (libusb_set_configuration(m_handle, 1) < 0) || (libusb_claim_interface(m_handle, 1) < 0)) { libusb_close(m_handle); m_handle = 0; set_mutex_.unlock(); continue; } #ifdef __LINUX__ libusb_reset_device(m_handle); #endif devices_in_use_.insert(device_address); device_address_ = device_address; set_mutex_.unlock(); open_ = true; break; } } } libusb_free_device_list(list, 1); if (i==count) // no devices found return -1; return 0; }
void all_devs(void) { libusb_device **devs, **pptr, *dev; ok(libusb_get_device_list(USB_CTX, &devs), "device list"); for (pptr=devs; (dev= *pptr)!=0; pptr++) { struct libusb_device_descriptor d; struct libusb_device_handle *h; int bus, nr; bus = libusb_get_bus_number(dev); nr = libusb_get_device_address(dev); ok(libusb_get_device_descriptor(dev, &d), "descriptor %d:%d", bus, nr); printf("bus %03d dev %03d vend %04x prod %04x\n", bus, nr, d.idVendor, d.idProduct); ok(libusb_open(dev, &h), "open %d:%d", bus, nr); #if 0 ok(libusb_reset_device(h), "reset %d:%d", bus, nr); #endif libusb_close(h); } libusb_free_device_list(devs, 1); }
/* Find a PMD1208FS device. Return a device handle, or NULL. */ libusb_device_handle* pmd_find_first(void) { int i, ret; libusb_device_handle* pmd; ret = libusb_init(NULL); if (ret) { err("failed to init libusb: %s\n", usb_get_errmsg(ret)); return NULL; } pmd = libusb_open_device_with_vid_pid(NULL, PMD_VID, PMD_PID); if (!pmd) { err("device not found\n"); libusb_exit(NULL); return NULL; } for(i = 0; i < 4; i++) { libusb_detach_kernel_driver(pmd, i); } libusb_reset_device(pmd); /* fixes odd-number-of-operations timeout */ libusb_set_configuration(pmd, 1); for(i = 0; i < 4; i++) { ret = libusb_claim_interface(pmd, i); if (ret) { err("failed to claim interface: %s\n", usb_get_errmsg(ret)); libusb_close(pmd); libusb_exit(NULL); return NULL; } } return pmd; }
minipro_handle_t *minipro_open(device_t *device) { int ret; minipro_handle_t *handle = malloc(sizeof(minipro_handle_t)); if(handle == NULL) { ERROR("Couldn't malloc"); } ret = libusb_init(&(handle->ctx)); if(ret < 0) { free(handle); ERROR2("Error initializing libusb: %s", libusb_error_name(ret)); } handle->usb_handle = libusb_open_device_with_vid_pid(handle->ctx, 0x04d8, 0xe11c); if(handle->usb_handle == NULL) { free(handle); ERROR("Error opening device"); } // we need to do this as it is possible that the device was not closed properly in a previous session // if we do not do this and the device was not closed properly it will cause an infinite hang // see: https://github.com/OpenKinect/libfreenect/blob/80f74239db4d450ecc0e45aa8b89cfcbc35defc2/src/usb_libusb10.c#L328 libusb_reset_device(handle->usb_handle); handle->device = device; return(handle); }
int USBLink::open() { libusb_init(&m_context); m_handle = libusb_open_device_with_vid_pid(m_context, PIXY_VID, PIXY_DID); if (m_handle==NULL) return -1; #ifdef __MACOS__ libusb_reset_device(m_handle); Sleeper::msleep(100); #endif if (libusb_set_configuration(m_handle, 1)<0) { libusb_close(m_handle); m_handle = 0; return -1; } if (libusb_claim_interface(m_handle, 1)<0) { libusb_close(m_handle); m_handle = 0; return -1; } return 0; }
GlobalTrainer::~GlobalTrainer() { free(read_buf); if (interface_claimed) { const int ecode = libusb_release_interface(usb_handle, 1); if (ecode) { std::cerr << "Error releasing interface: " << libusb_error_name(ecode) << std::endl; } } if (detached_kernel) { const int ecode = libusb_attach_kernel_driver(usb_handle, 1); if (ecode) { std::cerr << "Error reattaching kernel driver: " << libusb_error_name(ecode) << std::endl; } } const int ecode = libusb_reset_device(usb_handle); if (ecode && ecode != LIBUSB_ERROR_NOT_FOUND) { std::cerr << "Error reseting USB device: " << libusb_error_name(ecode) << std::endl; } if (usb_handle) { libusb_close(usb_handle); } if (usb_ctxt) { libusb_exit(usb_ctxt); } }
bool smtpfs_reset_device(LIBMTP_raw_device_t *device) { if (libusb_init(NULL) != 0) return false; libusb_device **dev_list; ssize_t num_devs = libusb_get_device_list(NULL, &dev_list); if (!num_devs) { libusb_exit(NULL); return false; } libusb_device_handle *dev_handle = nullptr; for (auto i = 0; i < num_devs; ++i) { uint8_t bnum = libusb_get_bus_number(dev_list[i]); uint8_t dnum = libusb_get_device_address(dev_list[i]); if (static_cast<uint32_t>(bnum) == device->bus_location && dnum == device->devnum) { libusb_open(dev_list[i], &dev_handle); libusb_reset_device(dev_handle); libusb_close(dev_handle); break; } } libusb_free_device_list(dev_list, 0); libusb_exit(NULL); return true; }
static void usb_deinit(vfs_dev_t *dev) { int r; if (dev->state == STATE_CONFIGURED) { r = libusb_reset_device(dev->devh); if (r != 0) fprintf(stderr, "Failed to reset device\n"); dev->state = STATE_CLAIMED; } if (dev->state == STATE_CLAIMED) { r = libusb_release_interface(dev->devh, 0); if (r != 0) fprintf(stderr, "Failed to release interface (%d)\n", r); dev->state = STATE_OPEN; } if (dev->state == STATE_OPEN) { libusb_close(dev->devh); dev->devh = NULL; dev->state = STATE_INIT; } if (dev->state == STATE_INIT) { libusb_exit(dev->ctx); dev->ctx = NULL; dev->state = STATE_NOTHING; } }
static int dev_open(struct fp_img_dev *dev, unsigned long driver_data) { struct fpi_ssm *init_ssm; struct vfs0050_dev *vdev = NULL; int r; fp_dbg("dev_open called"); //TODO: this is probably frowned upon :/ but right now a reset is the only // way for me to know I'm working with a clean slate and the rest of this will work. // Will be trying to reverse engineer more of the protocol so I can avoid resetting // the device each time it's opened. libusb_reset_device(dev->udev); r = libusb_claim_interface(dev->udev, 0); if (r < 0) { fp_err("could not claim interface 0"); return r; } libusb_control_transfer(dev->udev, 0x00, 0x09, 0x0001, 0, NULL, 0, 100); vdev = g_malloc0(sizeof(struct vfs0050_dev)); vdev->scanbuf = g_malloc0(VFS0050_INITIAL_SCANBUF_SIZE); vdev->scanbuf_sz = VFS0050_INITIAL_SCANBUF_SIZE; vdev->calbuf = g_malloc0(VFS0050_INITIAL_SCANBUF_SIZE); vdev->calbuf_sz = VFS0050_INITIAL_SCANBUF_SIZE; dev->priv = vdev; init_ssm = fpi_ssm_new(dev->dev, state_init, M_INIT_NUMSTATES); init_ssm->priv = dev; fpi_ssm_start(init_ssm, state_init_complete); return 0; }
int main(int argc, char **argv) { const char *filename; int fd; int rc; if (argc != 2) print_help_and_exit(argv); int result, vid, pid; result = sscanf(argv[1], "%x:%x", &vid, &pid); if(!result) print_help_and_exit(argv); libusb_init(NULL); libusb_device_handle *handle = libusb_open_device_with_vid_pid(NULL, vid, pid); if(!handle) { fprintf(stderr, "Couldn't open device\n"); return 1; } result = libusb_reset_device(handle); if(!handle) { fprintf(stderr, "Couldn't reset device\n"); return 1; } return 0; }
static void usb_init(vfs_dev_t *dev) { int i; int r; assert(dev->state == STATE_NOTHING); r = libusb_init(&dev->ctx); if (r != 0) { fprintf(stderr, "Failed to initialise libusb\n"); return; } dev->state = STATE_INIT; for (i = 0; i < sizeof(usb_ids_supported) / sizeof(usb_ids_supported[0]); i++) { dev->devh = libusb_open_device_with_vid_pid( NULL, usb_ids_supported[i][0], usb_ids_supported[i][1] ); if (dev->devh != NULL) break; } if (dev->devh == NULL) { fprintf(stderr, "Can't open any validity device!\n"); return; } dev->state = STATE_OPEN; for (i = 0; i < 1000000; i++){ r = libusb_kernel_driver_active(dev->devh, i); if (r == 1) { r = libusb_detach_kernel_driver(dev->devh, 4); if (r < 0) fprintf(stderr, "Error detaching kernel driver!\n"); } } r = libusb_claim_interface(dev->devh, 0); if (r != 0) { fprintf(stderr, "usb_claim_interface error %d\n", r); return; } dev->state = STATE_CLAIMED; r = libusb_reset_device(dev->devh); if (r != 0) { fprintf(stderr, "Error resetting device"); return; } r = libusb_control_transfer( dev->devh, LIBUSB_REQUEST_TYPE_STANDARD, LIBUSB_REQUEST_SET_FEATURE, 1, 1, NULL, 0, VALIDITY_DEFAULT_WAIT_TIMEOUT ); if (r != 0) { fprintf(stderr, "device configuring error %d\n", r); return; } dev->state = STATE_CONFIGURED; }
void device_reset() { if (device != NULL) { printf("[Device] Reseting Connection.\n"); libusb_reset_device(device); } }
void dfu_notify_upload_finshed(struct libusb_device_handle *handle) { int ret, i; ret = libusb_control_transfer(handle, 0x21, 1, 0, 0, 0, 0, 100); for (i=0; i<3; i++){ get_status(handle); } libusb_reset_device(handle); }
// caller can use UsbSend() afterwards, and should // finally do libusb_release_interface(handle, 0); libusb_close(handle); libusb_device_handle *UsbInit(struct cutter_id *id) { // Now do stuff with the handle. int r = 0; libusb_device_handle* handle = UsbOpen(id); if (!handle) return NULL; if (libusb_kernel_driver_active(handle, 0) == 1) { r = libusb_detach_kernel_driver(handle, 0); if (r != 0) { libusb_close(handle); id->msg = Error("Error detaching kernel USB driver: " + UsbError(r)); return NULL; } } r = libusb_reset_device(handle); if (r != 0) { libusb_close(handle); id->msg = Error("Error resetting device: " + UsbError(r)); return NULL; } cout << "Selecting configuration." << endl; r = libusb_set_configuration(handle, 1); if (r < 0) { libusb_close(handle); id->msg = Error("Error setting USB configuration: " + UsbError(r)); return NULL; } cout << "Claiming main control interface." << endl; r = libusb_claim_interface(handle, 0); if (r < 0) { libusb_close(handle); id->msg = Error("Error claiming USB interface: " + UsbError(r)); return NULL; } cout << "Setting alt interface." << endl; r = libusb_set_interface_alt_setting(handle, 0, 0); // Probably not really necessary. if (r < 0) { libusb_close(handle); id->msg = Error("Error setting alternate USB interface: " + UsbError(r)); return NULL; } cout << "Initialisation successful." << endl; return handle; }
USBTransport *USBTransport::create(libusb_context *ctx, libusb_device *device, QString *error) { Q_UNUSED(ctx); libusb_device_handle *handle; int ret = libusb_open(device, &handle); if(ret != 0) { *error = USBTransportPlugin::translate_libusb(ret); return NULL; } ret = libusb_set_configuration(handle, 1); if(ret != 0) { *error = USBTransportPlugin::translate_libusb(ret); return NULL; } ret = libusb_claim_interface(handle, 0); if(ret != 0) { *error = USBTransportPlugin::translate_libusb(ret); libusb_close(handle); return NULL; } ret = libusb_set_interface_alt_setting(handle, 0, 0); if(ret != 0) { libusb_release_interface(handle, 0); *error = USBTransportPlugin::translate_libusb(ret); libusb_close(handle); return NULL; } ret = libusb_reset_device(handle); if(ret != 0) { libusb_release_interface(handle, 0); *error = USBTransportPlugin::translate_libusb(ret); libusb_close(handle); return NULL; } return new USBTransport(handle); }
int reset_wheel(wheelstruct* w) { libusb_device_handle *handle = libusb_open_device_with_vid_pid(NULL, VID_LOGITECH, w->native_pid ); if ( handle == NULL ) { printf ( "%s not found. Make sure it is set to native mode (use --native).\n", w->name); return -1; } return libusb_reset_device(handle); }
irecv_error_t irecv_reset(irecv_client_t client) { if (client == NULL || client->handle == NULL) { return IRECV_E_NO_DEVICE; } libusb_reset_device(client->handle); return IRECV_E_SUCCESS; }
void FX3Stream::init(unsigned short vid, unsigned short pid) { this->vid = vid; this->pid = pid; error = libusb_init(0); if( error < 0 ) { cerr << "Error initialising libusb: " << libusb_error_name(error) << endl; return; } if( FX3_DEBUG ) { libusb_set_debug(0,LIBUSB_LOG_LEVEL_WARNING); } devhandle = libusb_open_device_with_vid_pid(0, vid, pid); if( !devhandle ) { cerr << "Error opening the device" << endl; error = 1; return; } error = libusb_reset_device( devhandle ); if( error < 0 ) { cerr << "Error resetting the device: " << libusb_error_name(error) << endl; return; } error = libusb_kernel_driver_active( devhandle, 1 ); if( error < 0 ) { cerr << "Error checking for kernel driver: " << libusb_error_name(error) << endl; return; } else if( error == 1 ) { cerr << "Error: Kernel driver active." << endl; return; } error = libusb_set_configuration( devhandle, 1 ); if( error < 0 ) { cerr << "Error setting the configuration: " << libusb_error_name(error) << endl; return; } error = libusb_claim_interface( devhandle, 0 ); if( error < 0 ) { cerr << "Error claiming the interface: " << libusb_error_name(error) << endl; return; } interfaceClaimed = true; inPktSize = libusb_get_max_packet_size( libusb_get_device(devhandle), FX3_IN_EP ); if( inPktSize < 0 ) { cerr << "Error getting IN EP packet size: " << libusb_error_name(error) << endl; return; } outPktSize = libusb_get_max_packet_size( libusb_get_device(devhandle), FX3_OUT_EP ); if( outPktSize < 0 ) { cerr << "Error getting OUT EP packet size: " << libusb_error_name(error) << endl; return; } if( FX3_DEBUG ) { cerr << "inPktSize: " << inPktSize << ", outPktSize: " << outPktSize << endl; } allocTransfers(); }
bool CameraUSB::resetDevice() { if(_devHandle) { if(libusb_reset_device(_devHandle) != LIBUSB_SUCCESS) return false; } return true; }
int fx2::reset() { assert(dev_handle); int rv=libusb_reset_device(dev_handle); if (rv==LIBUSB_ERROR_NO_DEVICE) { printf ( "Device Changed. Closing\n"); libusb_close(dev_handle); interface=0;alt_setting=0; } return rv; }
/***************************************************************************** * * CloseUSB * ****************************************************************************/ status_t CloseUSB(unsigned int reader_index) { /* device not opened */ if (usbDevice[reader_index].dev_handle == NULL) return STATUS_UNSUCCESSFUL; DEBUG_COMM3("Closing USB device: %d/%d", usbDevice[reader_index].bus_number, usbDevice[reader_index].device_address); if (usbDevice[reader_index].ccid.arrayOfSupportedDataRates && (usbDevice[reader_index].ccid.bCurrentSlotIndex == 0)) { free(usbDevice[reader_index].ccid.arrayOfSupportedDataRates); usbDevice[reader_index].ccid.arrayOfSupportedDataRates = NULL; } if (usbDevice[reader_index].ccid.gemalto_firmware_features) { free(usbDevice[reader_index].ccid.gemalto_firmware_features); usbDevice[reader_index].ccid.gemalto_firmware_features = NULL ; } /* one slot closed */ (*usbDevice[reader_index].nb_opened_slots)--; /* release the allocated ressources for the last slot only */ if (0 == *usbDevice[reader_index].nb_opened_slots) { DEBUG_COMM("Last slot closed. Release resources"); if (usbDevice[reader_index].ccid.sIFD_serial_number) free(usbDevice[reader_index].ccid.sIFD_serial_number); if (usbDevice[reader_index].ccid.sIFD_iManufacturer) free(usbDevice[reader_index].ccid.sIFD_iManufacturer); /* reset so that bSeq starts at 0 again */ if (DriverOptions & DRIVER_OPTION_RESET_ON_CLOSE) (void)libusb_reset_device(usbDevice[reader_index].dev_handle); (void)libusb_release_interface(usbDevice[reader_index].dev_handle, usbDevice[reader_index].interface); (void)libusb_close(usbDevice[reader_index].dev_handle); /* does not work for libusb <= 1.0.8 */ /* libusb_exit(ctx); */ } /* mark the resource unused */ usbDevice[reader_index].dev_handle = NULL; usbDevice[reader_index].interface = 0; return STATUS_SUCCESS; } /* CloseUSB */
int gl_close(void) { if (g_devh) { libusb_release_interface(g_devh, 0); libusb_reset_device(g_devh); libusb_close(g_devh); } libusb_exit(NULL); return 0; }
void usb_reset_device(int vid, int pid) { libusb_context *ctx; if (libusb_init(&ctx) != 0) return; libusb_device_handle *dev = libusb_open_device_with_vid_pid(ctx, vid, pid); if (dev == NULL) return; libusb_reset_device(dev); libusb_exit(ctx); }
int FTDIDevice_Reset(FTDIDevice *dev) { int err; err = libusb_reset_device(dev->handle); if (err) return err; return DeviceInit(dev); }
static int gp_libusb1_reset(GPPort *port) { C_PARAMS (port && port->pl->dh); /* earlier libusb 1 versions get crashes otherwise */ _close_async_interrupts(port); C_LIBUSB (libusb_reset_device (port->pl->dh), GP_ERROR_IO); return GP_OK; }
/** * Check if there is a message from the usb device and emit the right sihnal */ void DomusEngineUSBDevice::getUsbMessage() { unsigned char data[64]{}; int transfered{}; if (demo && !requestedAttributes.empty()) { addSyntetichData(); } if (device) { int result = libusb_bulk_transfer(handle, BULK_ENDPOINT_IN, data, sizeof(data), &transfered, 100); if (result == 0) { BOOST_LOG_TRIVIAL(info) << "new data arrived from endpoint " << (int) BULK_ENDPOINT_IN << ", size " << transfered; usbResponseExecuters.execute(data, transfered); } else if (result == LIBUSB_ERROR_TIMEOUT) { // no data } else { BOOST_LOG_TRIVIAL(error) << "from endpoint " << std::hex << (int) BULK_ENDPOINT_IN << " Transfered: " << transfered; BOOST_LOG_TRIVIAL(error) << strUsbError(result); if (libusb_reset_device(handle) != 0) { handle = nullptr; device = nullptr; } } result = libusb_bulk_transfer(handle, LOG_ENDPOINT_IN, data, sizeof(data), &transfered, 100); if (result == 0) { BOOST_LOG_TRIVIAL(trace) << "new data arrived from endpoint " << std::hex << (int) LOG_ENDPOINT_IN << ", size " << transfered; usbResponseExecuters.execute(data, transfered); } else if (result == LIBUSB_ERROR_TIMEOUT) { // no data } else { BOOST_LOG_TRIVIAL(error) << "from endpoint" << (int) BULK_ENDPOINT_IN << " Transfered: " << transfered; BOOST_LOG_TRIVIAL(error) << strUsbError(result); if (libusb_reset_device(handle) != 0) { handle = nullptr; device = nullptr; } } } }
irecv_error_t irecv_reset(irecv_client_t client) { if (check_context(client) != IRECV_E_SUCCESS) return IRECV_E_NO_DEVICE; #ifndef WIN32 libusb_reset_device(client->handle); #else int ret; DWORD count; ret = DeviceIoControl(client->handle, 0x22000C, NULL, 0, NULL, 0, &count, NULL); #endif return IRECV_E_SUCCESS; }
void irecv_usb_reset_device(libusb_device_handle *usb_handle) { libusbip_error_t error = LIBUSBIP_E_SUCCESS; if (libirecovery_usage_context == IRECV_CTX_LOCAL) { libusb_reset_device(usb_handle); return; } error = libusbip_reset_device(&libirecovery_connection_info, &libirecovery_device_handle); if (error < 0) { debug("libusbip_reset_device failed\n"); } }
static void pen_reset (short vendor, short product) { libusb_context *ctx; libusb_device_handle *dev; int rc; printf("swizzle\n"); rc = libusb_init(&ctx); assert(rc == 0); dev = libusb_open_device_with_vid_pid(ctx, vendor, product); libusb_reset_device(dev); libusb_close(dev); libusb_exit(ctx); }
//============================================================================== void USB_Device::close() { if (handle_) { /* the reset is a workaround to fix a bug * some bulk transfer does not end, and when the device is closed * with an open transfer it is blocked. * by reseting the bulk transfer this blockade can be avoided. * Need to find the unclosed bulk transfer and fix this properly */ libusb_reset_device(handle_); libusb_close(handle_); handle_ = NULL; } }