struct libusb_device_handle *usb_init(struct libusb_context* context, int devid) { struct libusb_device **device_list; struct libusb_device_handle *handle = NULL; int deviceCount = libusb_get_device_list(context, &device_list); int i; for (i = 0; i < deviceCount; i++) { struct libusb_device* device = device_list[i]; struct libusb_device_descriptor desc; libusb_get_device_descriptor(device, &desc); if (desc.idVendor == VENDOR_ID && desc.idProduct == devid) { libusb_open(device, &handle); break; } } libusb_free_device_list(device_list, 1); return handle; }
int open_device(unsigned int vendorID, unsigned int productID, int n) { libusb_device **list, *dev; libusb_device_handle *dev_handle; ssize_t cnt, j; struct libusb_device_descriptor desc; int err, i; cnt = libusb_get_device_list(NULL, &list); if (cnt<0) { libusb_free_device_list(list, 1); return -4; /* Indicates no USB devices found at all. */ } i = 0; for (j = 0; j<cnt; j++) { dev = list[j]; libusb_get_device_descriptor(dev, &desc); if ((desc.idVendor==vendorID) && (desc.idProduct==productID)) { if (i==n) { for (i = 0; i<MAX_DEVICES_OPEN; i++) { if (open_devices[i]==NULL) { err = libusb_open(dev, &dev_handle); open_devices[i] = dev_handle; libusb_free_device_list(list, 1); if (err) { return -1; /* Indicates matching USB device found, but could not be opened. */ } else { devices_open++; return i; } } } libusb_free_device_list(list, 1); return -2; /* Indicates matching USB device found, but too many devices already opened. */ } else { i++; } } } libusb_free_device_list(list, 1); return -3; /* Indicates no matching USB device found. */ }
API_EXPORTED usb_dev_handle *usb_open(struct usb_device *dev) { int r; usbi_dbg(""); usb_dev_handle *udev = malloc(sizeof(*udev)); if (!udev) return NULL; r = libusb_open((libusb_device *) dev->dev, &udev->handle); if (r < 0) { usbi_err("could not open device, error %d", r); free(udev); return NULL; } udev->last_claimed_interface = -1; udev->device = dev; return udev; }
void Y_usb_open_device(int argc) { ydev_instance_t *obj = NULL; libusb_device* dev; int bus, port; int i, ret; if (argc != 2) { y_error("expecting exactly 2 arguments"); } bus = ygets_i(1); port = ygets_i(0); load_device_list(); for (i = 0; i < dev_count; ++i) { dev = dev_list[i]; if (libusb_get_bus_number(dev) == bus && libusb_get_port_number(dev) == port) { obj = (ydev_instance_t *)ypush_obj(&ydev_class, sizeof(ydev_instance_t)); obj->device = libusb_ref_device(dev); ret = libusb_open(obj->device, &obj->handle); if (ret < 0) { obj->handle = NULL; failure("failed to open device", ret); } obj->bus = bus; obj->port = port; obj->address = libusb_get_device_address(dev); ret = libusb_get_device_descriptor(dev, &obj->descriptor); if (ret != 0) { free_dev_list(); failure("unable to get device descriptor", ret); } break; } } free_dev_list(); if (obj == NULL) { ypush_nil(); } }
bool LibusbDevice::Attach() { if (m_device_attached) return true; if (!m_handle) { NOTICE_LOG(IOS_USB, "[%04x:%04x] Opening device", m_vid, m_pid); const int ret = libusb_open(m_device, &m_handle); if (ret != 0) { ERROR_LOG(IOS_USB, "[%04x:%04x] Failed to open: %s", m_vid, m_pid, libusb_error_name(ret)); m_handle = nullptr; return false; } } if (ClaimAllInterfaces(DEFAULT_CONFIG_NUM) < 0) return false; m_device_attached = true; return true; }
std::string LedDeviceHyperionUsbasp::getString(libusb_device * device, int stringDescriptorIndex) { libusb_device_handle * handle = nullptr; int error = libusb_open(device, &handle); if (error != LIBUSB_SUCCESS) { throw error; } char buffer[256]; error = libusb_get_string_descriptor_ascii(handle, stringDescriptorIndex, reinterpret_cast<unsigned char *>(buffer), sizeof(buffer)); if (error <= 0) { libusb_close(handle); throw error; } libusb_close(handle); return std::string(buffer, error); }
static int set_port_power(struct state *st, struct libusb_device *hub_dev, int port, int enabled) { int feature = USB_PORT_FEAT_POWER; int request; int index; int ret; struct libusb_device_handle *hub = NULL; if (enabled) { request = LIBUSB_REQUEST_SET_FEATURE; index = port; } else { request = LIBUSB_REQUEST_CLEAR_FEATURE; feature = USB_PORT_FEAT_POWER; index = port; } ret = libusb_open(hub_dev, &hub); if (ret) { fprintf(stderr, "Unable to open USB hub: %d\n", ret); return ret; } ret = libusb_control_transfer(hub, USB_RT_PORT, request, feature, index, NULL, 0, CTRL_TIMEOUT); if (ret) { fprintf(stderr, "Unable to send packet to USB hub: %d\n", ret); libusb_close(hub); return ret; } libusb_close(hub); return ret; }
void openAuxDevice(int index = 0) { libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices ssize_t cnt = libusb_get_device_list (0, &devs); //get the list of devices if (cnt < 0) { ROS_ERROR("No device on USB"); return; } int nr_mot(0); for (int i = 0; i < cnt; ++i) { struct libusb_device_descriptor desc; const int r = libusb_get_device_descriptor (devs[i], &desc); if (r < 0) continue; // Search for the aux if (desc.idVendor == MS_MAGIC_VENDOR && desc.idProduct == MS_MAGIC_MOTOR_PRODUCT) { // If the index given by the user matches our camera index if (nr_mot == index) { if ((libusb_open (devs[i], &dev) != 0) || (dev == 0)) { ROS_ERROR_STREAM("Cannot open aux " << index); return; } // Claim the aux libusb_claim_interface (dev, 0); break; } else nr_mot++; } } libusb_free_device_list (devs, 1); // free the list, unref the devices in it }
//============================================================================== bool USB_Device::open_by_address(uint8_t bus_number, uint8_t device_address) { init_context(); close(); libusb_device_vector devices; USB_Enumerator enumerator(context_, devices); foreach (libusb_device *item, devices) { if (libusb_get_bus_number(item) == bus_number && libusb_get_device_address(item) == device_address) { int result = libusb_open(item, &handle_); if (result < 0) on_error("libusb_open", result); device_ = libusb_get_device(handle_); return true; } } return false; }
vector<TreehopperBoard>* TreehopperManager::ScanForDevices() { ssize_t cnt; cnt = libusb_get_device_list(NULL, &devs); if (cnt < 0) return &BoardList; libusb_device *dev; int i = 0; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { return &BoardList; } if (desc.idProduct == TreehopperBoard::pid && desc.idVendor == TreehopperBoard::vid) { // Get the name and serial number libusb_device_handle* candidate; libusb_open(dev, &candidate); unsigned char buffer[64]; libusb_get_string_descriptor_ascii(candidate, 4, buffer, 128); string name = string((const char*)buffer); libusb_get_string_descriptor_ascii(candidate, 3, buffer, 128); string serialNumber = string((const char*)buffer); libusb_close(candidate); BoardList.push_back(TreehopperBoard(serialNumber)); } } return &BoardList; }
int ezusb_upload_firmware(libusb_device *dev, int configuration, const char *filename) { struct libusb_device_handle *hdl; int err; sr_info("uploading firmware to device on %d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev)); if ((err = libusb_open(dev, &hdl)) < 0) { sr_warn("failed to open device: %d", err); return SR_ERR; } if (libusb_kernel_driver_active(hdl, 0)) { if ((err = libusb_detach_kernel_driver(hdl, 0)) < 0) { sr_warn("failed to detach kernel driver: %d", err); return SR_ERR; } } if ((err = libusb_set_configuration(hdl, configuration)) < 0) { sr_warn("Unable to set configuration: %d", err); return SR_ERR; } if ((ezusb_reset(hdl, 1)) < 0) return SR_ERR; if (ezusb_install_firmware(hdl, filename) < 0) return SR_ERR; if ((ezusb_reset(hdl, 0)) < 0) return SR_ERR; libusb_close(hdl); return SR_OK; }
void Y_usb_summary(int argc) { ssize_t i; libusb_device_handle* handle; load_device_list(); for (i = 0; i < dev_count; ++i) { int code; struct libusb_device_descriptor desc; libusb_device* dev = dev_list[i]; uint8_t bus_number = libusb_get_bus_number(dev); uint8_t port_number = libusb_get_port_number(dev); uint8_t device_address = libusb_get_device_address(dev); fprintf(stdout, "USB Device %ld:\n", (long)i); fprintf(stdout, " Bus Number ---------> %d\n", (int)bus_number); fprintf(stdout, " Port Number --------> %d\n", (int)port_number); fprintf(stdout, " Device Address -----> %d\n", (int)device_address); code = libusb_get_device_descriptor(dev, &desc); if (code != 0) { failure(NULL, code); } fprintf(stdout, " Vendor ID ----------> 0x%04x\n", (unsigned int)desc.idVendor); fprintf(stdout, " Product ID ---------> 0x%04x\n", (unsigned int)desc.idProduct); code = libusb_open(dev, &handle); if (code == 0) { fprintf(stdout, " Manufacturer -------> %s\n", get_string(handle, desc.iManufacturer)); fprintf(stdout, " Product ------------> %s\n", get_string(handle, desc.idProduct)); fprintf(stdout, " Serial Number ------> %s\n", get_string(handle, desc.iSerialNumber)); libusb_close(handle); } } free_dev_list(); ypush_nil(); }
libusb_device_handle* InfinityPortal::connect(int deviceId) { libusb_device** devices; libusb_context* context; struct libusb_device_handle* tryDeviceHandler; libusb_init(&context); int devicesCount = libusb_get_device_list(context, &devices); int error; struct libusb_device_descriptor descriptor; int retVal = libusb_open(devices[deviceId], &tryDeviceHandler); libusb_get_device_descriptor(devices[deviceId], &descriptor); if(descriptor.idVendor == 0x0e6f && descriptor.idProduct == 0x0129) { return tryDeviceHandler; } }
int upload_firmware(libusb_device *dev) { struct libusb_device_handle *hdl; int err; g_message("uploading firmware to device on %d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev)); err = libusb_open(dev, &hdl); if(err != 0) { g_warning("failed to open device: %d", err); return 1; } err = libusb_set_configuration(hdl, USB_CONFIGURATION); if(err != 0) { g_warning("Unable to set configuration: %d", err); return 1; } if((ezusb_reset(hdl, 1)) < 0) return 1; if(ezusb_install_firmware(hdl, FIRMWARE) != 0) return 1; if((ezusb_reset(hdl, 0)) < 0) return 1; libusb_close(hdl); /* remember when the last firmware update was done */ g_get_current_time(&firmware_updated); return 0; }
void irecv_usb_open(libusb_device *usb_device, libusb_device_handle **usb_handle) { libusbip_error_t error = LIBUSBIP_E_SUCCESS; struct libusbip_device *found = NULL; uint32_t session_data; ssize_t len; size_t i; if (libirecovery_usage_context == IRECV_CTX_LOCAL) { libusb_open(usb_device, usb_handle); return; } len = libirecovery_device_list.n_devices; session_data = usb_device->session_data; for (i = 0; i < len; i++) { struct libusbip_device *idev = &libirecovery_device_list.devices[i]; if (idev->session_data == session_data) { found = idev; break; } } if (!found) { debug("device not found\n"); return; } error = libusbip_open(&libirecovery_connection_info, found, &libirecovery_device_handle); if (error < 0) { debug("libusbip_open failed\n"); return; } *usb_handle = (void *)0xffff; // Pseudo address }
int main(int argc,char **argv) { (void)argc; (void)argv; assert(libusb_init(NULL)==0); libusb_set_debug(NULL,3); libusb_device **device_list; ssize_t list_size=libusb_get_device_list(NULL,&device_list); assert(list_size>=0); libusb_device *found = NULL; for(ssize_t i=0;i<list_size;i++) { if(is_debug(device_list[i])) found=device_list[i]; } if(found) { libusb_device_handle *handle; int err = libusb_open(found,&handle); assert(err==0); main_loop(handle); libusb_close(handle); } libusb_free_device_list(device_list,1); libusb_exit(NULL); return 0; }
SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len) { uint8_t port_numbers[8]; int i, n, len; /* * FreeBSD requires that devices prior to calling libusb_get_port_numbers() * have been opened with libusb_open(). * This apparently also applies to some Mac OS X versions. */ #if defined(__FreeBSD__) || defined(__APPLE__) struct libusb_device_handle *devh; if (libusb_open(dev, &devh) != 0) return SR_ERR; #endif n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); #if defined(__FreeBSD__) || defined(__APPLE__) libusb_close(devh); #endif /* Workaround FreeBSD / Mac OS X libusb_get_port_numbers() returning 0. */ #if defined(__FreeBSD__) || defined(__APPLE__) if (n == 0) { port_numbers[0] = libusb_get_device_address(dev); n = 1; } #endif if (n < 1) return SR_ERR; len = snprintf(path, path_len, "usb/%d-%d", libusb_get_bus_number(dev), port_numbers[0]); for (i = 1; i < n; i++) len += snprintf(path+len, path_len-len, ".%d", port_numbers[i]); return SR_OK; }
// Detach USB Device static int detach_device(libusb_device *dev) { struct libusb_device_descriptor d; struct libusb_config_descriptor *conf; libusb_device_handle *handle; unsigned int i; if ( libusb_get_device_descriptor(dev, &d) ) return 1; if ( libusb_get_active_config_descriptor(dev, &conf) ) return 2; if ( libusb_open(dev, &handle) ) return 3; for(i = 0; i < conf->bNumInterfaces; i++) { int ret = libusb_detach_kernel_driver(handle, i); if(ret==LIBUSB_ERROR_NOT_FOUND){ return 10; } if ( ret ) { libusb_close(handle); libusb_free_config_descriptor(conf); return 4; } printf(" - detached interface %u\n", i); } //libusb_reset_device(handle); libusb_close(handle); libusb_free_config_descriptor(conf); return 6; }
int mcp2200_connect(int index){ if (index < device_list_count){ int conID = findEmptyConnectionSlot(); if (conID >= 0){ int r = libusb_open(device_list[index], &connection_list[conID]); //printf ("Connect. r = %d\n",r); if (r < 0) return r; // Detach kernel driver, if any. // The result of this call is ignored libusb_detach_kernel_driver(connection_list[conID], MCP2200_HID_INTERFACE); // Claim HID interface r = libusb_claim_interface(connection_list[conID], MCP2200_HID_INTERFACE); if (r != 0){ closeDevice(conID); return r; } return conID; } } return -1; }
int scanbtnd_open (scanner_t * scanner) { int result = -ENOSYS; if (scanner->is_open) return -EINVAL; switch (scanner->connection) { case CONNECTION_LIBUSB: /* if devices have been added/removed, return -ENODEV to * make scanbuttond update its device list */ if (libusb_get_changed_device_count () != 0) return -ENODEV; result = libusb_open ((libusb_device_t *) scanner->internal_dev_ptr); break; } if (result == 0) scanner->is_open = 1; return result; }
static const char* tigl_get_product(struct libusb_device *dev) { libusb_device_handle *han; int ret; static unsigned char string[64]; struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { ticables_error("failed to get device descriptor"); return ""; } if (desc.iProduct) { if (!libusb_open(dev, &han)) { ret = libusb_get_string_descriptor_ascii(han, desc.iProduct, string, sizeof(string)); libusb_close(han); if (ret > 0) { return (const char *) string; } else { ticables_warning("libusb_get_string_descriptor_ascii (%s).\n", tigl_strerror(ret)); return ""; } } else { return ""; } } return (const char *)string; }
/** * find in the usb tree if the device is present */ bool DomusEngineUSBDevice::isPresent() { if (device != nullptr || demo) { return true; } else { libusb_device **devices{}; handle = nullptr; ssize_t devicesNum = libusb_get_device_list((libusb_context *) usbContext, &devices); for (int index = 0; index < devicesNum; index++) { libusb_device_descriptor deviceDescriptor; libusb_get_device_descriptor(devices[index], &deviceDescriptor); if (deviceDescriptor.bDeviceClass == deviceClass && deviceDescriptor.idVendor == vendorID && deviceDescriptor.idProduct == productID) { device = devices[index]; libusb_ref_device((libusb_device *) device); int result = libusb_open((libusb_device *) device, (libusb_device_handle **) &handle); if (result != 0) { std::cerr << "Unable to open device: " << strUsbError(result) << std::endl; return false; } break; } } libusb_free_device_list(devices, 1); if (handle != nullptr) { BOOST_LOG_TRIVIAL(info) << "device found"; int result = libusb_claim_interface((libusb_device_handle *) handle, 0); if (result != 0) { BOOST_LOG_TRIVIAL(error) << "Unable to claim interface 0: " << strUsbError(result); } } else { BOOST_LOG_TRIVIAL(error) << "device not found"; } } return device != nullptr; }
/** * Check the USB configuration to determine if this is an fx2lafw device. * * @return TRUE if the device's configuration profile match fx2lafw * configuration, FALSE otherwise. */ SR_PRIV gboolean fx2lafw_check_conf_profile(libusb_device *dev) { struct libusb_device_descriptor des; struct libusb_device_handle *hdl; gboolean ret; unsigned char strdesc[64]; hdl = NULL; ret = FALSE; while (!ret) { /* Assume the FW has not been loaded, unless proven wrong. */ if (libusb_get_device_descriptor(dev, &des) != 0) break; if (libusb_open(dev, &hdl) != 0) break; if (libusb_get_string_descriptor_ascii(hdl, des.iManufacturer, strdesc, sizeof(strdesc)) < 0) break; if (strncmp((const char *)strdesc, "sigrok", 6)) break; if (libusb_get_string_descriptor_ascii(hdl, des.iProduct, strdesc, sizeof(strdesc)) < 0) break; if (strncmp((const char *)strdesc, "fx2lafw", 7)) break; /* If we made it here, it must be an fx2lafw. */ ret = TRUE; } if (hdl) libusb_close(hdl); return ret; }
int USBMgr::listDevices( std::vector<PS3EYECam::PS3EYERef>& list ) { libusb_device *dev; libusb_device **devs; libusb_device_handle *devhandle; int i = 0; int cnt; cnt = libusb_get_device_list(instance()->usb_context, &devs); if (cnt < 0) debug("Error Device scan\n"); cnt = 0; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; libusb_get_device_descriptor(dev, &desc); if(desc.idVendor == PS3EYECam::VENDOR_ID && desc.idProduct == PS3EYECam::PRODUCT_ID) { int err = libusb_open(dev, &devhandle); if (err == 0) { libusb_close(devhandle); list.push_back( PS3EYECam::PS3EYERef( new PS3EYECam(dev) ) ); libusb_ref_device(dev); cnt++; } } } libusb_free_device_list(devs, 1); return cnt; }
int atecard_open_inner() { int ret = 0; libusb_device *dev; struct libusb_device_descriptor desc; for (int i = 0; (dev = g_usbdevs[i]) != NULL; i++) { if (libusb_get_device_descriptor(dev, &desc) >= 0) { if (desc.idVendor == 0xdefa && desc.idProduct == 0x9361) { ret = libusb_open(dev, &g_usbdevhandle); if (ret >= 0) { libusb_claim_interface(g_usbdevhandle, 4); return 0; } } } } return -1; }
libusb_device_handle* mcs7715_find_device() { if(libusb_init(0)!=0) { return 0; } // discover devices libusb_device **list; libusb_device *found = NULL; ssize_t cnt = libusb_get_device_list(NULL, &list); ssize_t i = 0; int err = 0; if (cnt < 0) { return 0; } for (i = 0; i < cnt; i++) { libusb_device *device = list[i]; struct libusb_device_descriptor descriptor; if (libusb_get_device_descriptor(device,&descriptor)!=0) { continue; } if (descriptor.idVendor==MCS7715_VENDOR_ID && descriptor.idProduct==MCS7715_PRODUCT_ID) { found = device; break; } } libusb_device_handle *handle = 0; if (found) { err = libusb_open(found, &handle); if (err) { return 0; } mcs7715_startup(handle); } libusb_free_device_list(list, 1); return handle; }
static int renumerate(void) { cyusb_device *dev = NULL; cyusb_handle *handle = NULL; int found = 0; int i; int r; numdev = libusb_get_device_list(NULL, &list); if ( numdev < 0 ) { printf("Library: Error in enumerating devices...\n"); return -4; } nid = 0; for ( i = 0; i < numdev; ++i ) { cyusb_device *tdev = list[i]; if ( device_is_of_interest(tdev) ) { cydev[nid].dev = tdev; r = libusb_open(tdev, &cydev[nid].handle); if ( r ) { printf("Error in opening device\n"); return -5; } else handle = cydev[nid].handle; cydev[nid].vid = cyusb_getvendor(handle); cydev[nid].pid = cyusb_getproduct(handle); cydev[nid].is_open = 1; cydev[nid].busnum = cyusb_get_busnumber(handle); cydev[nid].devaddr = cyusb_get_devaddr(handle); ++nid; } } return nid; }
/** * Check the USB configuration to determine if this is an fx2lafw device. * * @return TRUE if the device's configuration profile matches fx2lafw * configuration, FALSE otherwise. */ SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer, const char *product) { struct libusb_device_descriptor des; struct libusb_device_handle *hdl; gboolean ret; unsigned char strdesc[64]; hdl = NULL; ret = FALSE; while (!ret) { /* Assume the FW has not been loaded, unless proven wrong. */ libusb_get_device_descriptor(dev, &des); if (libusb_open(dev, &hdl) != 0) break; if (libusb_get_string_descriptor_ascii(hdl, des.iManufacturer, strdesc, sizeof(strdesc)) < 0) break; if (strcmp((const char *)strdesc, manufacturer)) break; if (libusb_get_string_descriptor_ascii(hdl, des.iProduct, strdesc, sizeof(strdesc)) < 0) break; if (strcmp((const char *)strdesc, product)) break; ret = TRUE; } if (hdl) libusb_close(hdl); return ret; }
/* * Start this thread */ bool SunliteOutputPort::Start() { libusb_device_handle *usb_handle; if (libusb_open(m_usb_device, &usb_handle)) { OLA_WARN << "Failed to open Sunlite usb device"; return false; } if (libusb_claim_interface(usb_handle, 0)) { OLA_WARN << "Failed to claim Sunlite usb device"; libusb_close(usb_handle); return false; } m_usb_handle = usb_handle; bool ret = ola::thread::Thread::Start(); if (!ret) { OLA_WARN << "pthread create failed"; libusb_release_interface(m_usb_handle, 0); libusb_close(usb_handle); return false; } return true; }
/* Returns libusb error codes */ static int get_devinfo(libusb_device *dev, struct bladerf_devinfo *info) { int status = 0; libusb_device_handle *handle; struct libusb_device_descriptor desc; status = libusb_open(dev, &handle); if( status ) { log_debug("Couldn't populate devinfo - %s\n", libusb_error_name(status)); } else { /* Populate device info */ info->backend = BLADERF_BACKEND_LIBUSB; info->usb_bus = libusb_get_bus_number(dev); info->usb_addr = libusb_get_device_address(dev); status = libusb_get_device_descriptor(dev, &desc); if (status != 0) { memset(info->serial, 0, BLADERF_SERIAL_LENGTH); } else { status = libusb_get_string_descriptor_ascii( handle, desc.iSerialNumber, (unsigned char *)&info->serial, BLADERF_SERIAL_LENGTH); /* Consider this to be non-fatal, otherwise firmware <= 1.1 * wouldn't be able to get far enough to upgrade */ if (status < 0) { log_debug("Failed to retrieve serial number\n"); memset(info->serial, 0, BLADERF_SERIAL_LENGTH); } else { /* Adjust for > 0 return code */ status = 0; } } libusb_close(handle); } return status; }