struct discovered_devs *discovered_devs_append( struct discovered_devs *discdevs, struct libusb_device *dev) { size_t len = discdevs->len; size_t capacity; if (len < discdevs->capacity) { discdevs->devices[len] = libusb_ref_device(dev); discdevs->len++; return discdevs; } usbi_dbg("need to increase capacity"); capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP; discdevs = realloc(discdevs, sizeof(*discdevs) + (sizeof(void *) * capacity)); if (discdevs) { discdevs->capacity = capacity; discdevs->devices[len] = libusb_ref_device(dev); discdevs->len++; } return discdevs; }
libusb_device *ausb_libusb1_get_usbdev(const rsct_usbdev_t *d) { if (ausb_libusb1_init()) return NULL; else { libusb_device **list; size_t cnt=libusb_get_device_list(ausb_libusb1_context, &list); size_t i; libusb_device *dev=NULL; for (i=0; i<cnt; i++) { dev=list[i]; if (libusb_get_bus_number(dev)==d->busId && libusb_get_device_address(dev)==d->busPos) break; } if (dev) libusb_ref_device(dev); /* free devices and unref them */ libusb_free_device_list(list, 1); return dev; } }
bool USBSubsystem::find_controller_by_path(const std::string& busid_str, const std::string& devid_str, libusb_device** xbox_device) { int busid = boost::lexical_cast<int>(busid_str); int devid = boost::lexical_cast<int>(devid_str); libusb_device** list; ssize_t num_devices = libusb_get_device_list(NULL, &list); for(ssize_t dev_it = 0; dev_it < num_devices; ++dev_it) { libusb_device* dev = list[dev_it]; if (libusb_get_bus_number(dev) == busid && libusb_get_device_address(dev) == devid) { *xbox_device = dev; // incrementing ref count, user must call unref libusb_ref_device(*xbox_device); libusb_free_device_list(list, 1 /* unref_devices */); return true; } } libusb_free_device_list(list, 1 /* unref_devices */); return false; }
static usb_handle * enumerate(ifc_match_func callback) { static libusb_context *ctx = NULL; usb_handle *h; libusb_device **ppdev; ssize_t ndev; ssize_t x; h = malloc(sizeof(*h)); if (h == NULL) return (h); if (ctx == NULL) libusb_init(&ctx); ndev = libusb_get_device_list(ctx, &ppdev); for (x = 0; x < ndev; x++) { memset(h, 0, sizeof(*h)); h->dev = ppdev[x]; if (probe(h, callback) == 0) { libusb_ref_device(h->dev); libusb_free_device_list(ppdev, 1); return (h); } } free(h); libusb_free_device_list(ppdev, 1); return (NULL); }
static std::unique_ptr<usb_handle> enumerate(ifc_match_func callback) { static libusb_context *ctx = NULL; std::unique_ptr<usb_handle> h; libusb_device **ppdev; ssize_t ndev; ssize_t x; if (ctx == NULL) libusb_init(&ctx); ndev = libusb_get_device_list(ctx, &ppdev); for (x = 0; x < ndev; x++) { h.reset(new usb_handle); h->dev = ppdev[x]; if (probe(h, callback) == 0) { libusb_ref_device(h->dev); libusb_free_device_list(ppdev, 1); return (h); } } h.reset(); libusb_free_device_list(ppdev, 1); return (nullptr); }
ssize_t QUsb::qhyccd_get_device_list(qhyccd_device ***list) { ssize_t length = 0; libusb_device **usb_devices; ssize_t usb_devices_num; ssize_t i; *list = (qhyccd_device**)malloc(sizeof(qhyccd_device*)); (*list)[length] = NULL; usb_devices_num = libusb_get_device_list(qhyusb->ctx, &usb_devices); if (usb_devices_num < 0) { return -1; } for (i = 0; i < usb_devices_num; i++) { libusb_device *device = usb_devices[i]; if (is_supported_device(device)) { libusb_ref_device(device); (*list)[length] = device; length++; *list = (qhyccd_device**) realloc(*list, sizeof(qhyccd_device*) * (length + 1)); (*list)[length] = NULL; } } libusb_free_device_list(usb_devices, 1); return length; }
/** Destructor. */ JacoArm::~JacoArm() { if( __devh != NULL ) { // restore ref to the libusb_device, and mark it as unconnected for (std::list<usb_device_t>::iterator it=__connected_arms->begin(); it != __connected_arms->end(); ++it) { if( (*it).connected && (strcmp((*it).client_name, __client_config.name)==0) ) { (*it).dev = libusb_ref_device(libusb_get_device(__devh)); (*it).connected = false; break; } } // need to relase interface and device-handler libusb_release_interface(__devh, 0); libusb_close(__devh); } if( __auto_ctx ) { // libusb context was created implicitly. Check if devices are still connected for (std::list<usb_device_t>::iterator it=__connected_arms->begin(); it != __connected_arms->end(); ++it) { if( (*it).connected ) return; } // libusb context was created implicitly. so delete it now close_usb(); __auto_ctx = false; } }
ConnectionStatus UsbConnection::usb_connect() { libusb_device **list = NULL; ssize_t size_devices = libusb_get_device_list(context, &list); for (int i = 0; i < size_devices; i++) { libusb_device_descriptor desc; libusb_get_device_descriptor(list[i], &desc); if (desc.idVendor == VENDOR_ID && desc.idProduct == PRODUCT_ID) { int error = libusb_open(list[i], &handler); if (error) { qCritical("Can not open device.\n"); } device = list[i]; libusb_ref_device (device); break; } } libusb_free_device_list(list, 1); if (handler) { initInterfaceAndMaxPackageSize(); // print_info(); return ready; } else { return disconnected; } }
LibUSB::DeviceImpl::DeviceImpl( libusb_device* device ) : languageId(0) { // Add a reference to pDevice and save it. m_pDevice.reset(libusb_ref_device(device), DeviceDeleter()); }
void HWStubDevice::Init(struct libusb_device *dev) { libusb_ref_device(dev); m_dev = dev; m_handle = 0; m_hwdev = 0; m_valid = Probe(); }
/** * Returns a list of USB devices currently attached to the system and matching the given vendorID and productID. */ ssize_t SickTimCommonUsb::getSOPASDeviceList(libusb_context *ctx, uint16_t vendorID, uint16_t productID, libusb_device ***list) { libusb_device **resultDevices = NULL; ssize_t numberOfResultDevices = 0; libusb_device **devices; /* * Get a list of all USB devices connected. */ ssize_t numberOfDevices = libusb_get_device_list(ctx, &devices); /* * Iterate through the list of the connected USB devices and search for devices with the given vendorID and productID. */ for (ssize_t i = 0; i < numberOfDevices; i++) { struct libusb_device_descriptor desc; int result = libusb_get_device_descriptor(devices[i], &desc); if (result < 0) { ROS_ERROR("LIBUSB - Failed to get device descriptor"); diagnostics_.broadcast(diagnostic_msgs::DiagnosticStatus::ERROR, "LIBUSB - Failed to get device descriptor."); continue; } if (desc.idVendor == vendorID && desc.idProduct == 0x5001) { /* * Add the matching device to the function result list and increase the device reference count. */ resultDevices = (libusb_device **)realloc(resultDevices, sizeof(libusb_device *) * (numberOfResultDevices + 2)); if (resultDevices == NULL) { ROS_ERROR("LIBUSB - Failed to allocate memory for the device result list."); diagnostics_.broadcast(diagnostic_msgs::DiagnosticStatus::ERROR, "LIBUSB - Failed to allocate memory for the device result list."); } else { resultDevices[numberOfResultDevices] = devices[i]; resultDevices[numberOfResultDevices + 1] = NULL; libusb_ref_device(devices[i]); numberOfResultDevices++; } } } /* * Free the list of the connected USB devices and decrease the device reference count. */ libusb_free_device_list(devices, 1); /* * Prepare the return values of the function. */ *list = resultDevices; return numberOfResultDevices; }
micronucleus* micronucleus_connect() { micronucleus *nucleus = NULL; libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices libusb_context *ctx = NULL; //a libusb session int r; //for return values ssize_t cnt; //holding number of devices in list r = libusb_init(&ctx); //initialize a library session if(r < 0) { fprintf(stderr, "Init Error\n"); //there was an error return NULL; } //libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation cnt = libusb_get_device_list(ctx, &devs); //get the list of devices if(cnt < 0) { fprintf(stderr, "Get Device List Error\n"); //there was an error } //fprintf(stderr, "%ld devices found.\n", cnt); ssize_t i; //for iterating through the list for(i = 0; i < cnt; i++) { libusb_device *dev = devs[i]; struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r) { fprintf(stderr, "Get Device Descriptor Error\n"); return NULL; } //fprintf(stderr, "> checking device %d %d\n", desc.idVendor, desc.idProduct); /* Check if this device is a micronucleus */ if (desc.idVendor == MICRONUCLEUS_VENDOR_ID && desc.idProduct == MICRONUCLEUS_PRODUCT_ID) { nucleus = malloc(sizeof(micronucleus)); nucleus->version.major = (desc.bcdUSB >> 8) & 0xFF; nucleus->version.minor = desc.bcdUSB & 0xFF; if (nucleus->version.major > MICRONUCLEUS_MAX_MAJOR_VERSION) { fprintf(stderr, "Warning: device with unknown new version of Micronucleus detected.\n"); fprintf(stderr, "This tool doesn't know how to upload to this new device. Updates may be available.\n"); fprintf(stderr, "Device reports version as: %d.%d\n", nucleus->version.major, nucleus->version.minor); return NULL; } libusb_ref_device(dev); libusb_open(dev, &nucleus->device); nucleus->ctx = ctx; // get nucleus info unsigned char buffer[4]; int res = libusb_control_transfer(nucleus->device, 0xC0, 0, 0, 0, (unsigned char*)buffer, 4, MICRONUCLEUS_USB_TIMEOUT); assert(res >= 4); nucleus->flash_size = (buffer[0]<<8) + buffer[1]; nucleus->page_size = buffer[2]; nucleus->pages = (nucleus->flash_size / nucleus->page_size); if (nucleus->pages * nucleus->page_size < nucleus->flash_size) nucleus->pages += 1; nucleus->write_sleep = buffer[3]; nucleus->erase_sleep = nucleus->write_sleep * nucleus->pages; } }
ThreadedUsbReceiver::ThreadedUsbReceiver(libusb_device *usb_device, libusb_device_handle *usb_handle, PluginAdaptor *plugin_adaptor, int interface_number) : m_term(false), m_usb_device(usb_device), m_usb_handle(usb_handle), m_interface_number(interface_number), m_plugin_adaptor(plugin_adaptor), m_receive_callback(NULL) { libusb_ref_device(usb_device); }
XN_C_API XnStatus xnUSBOpenDeviceByPath(const XnUSBConnectionString strDevicePath, XN_USB_DEV_HANDLE* pDevHandlePtr) { XnStatus nRetVal = XN_STATUS_OK; // parse connection string XnUInt16 nVendorID = 0; XnUInt16 nProductID = 0; XnUInt8 nBus = 0; XnUInt8 nAddress = 0; sscanf(strDevicePath, "%hx/%hx@%hhu/%hhu", &nVendorID, &nProductID, &nBus, &nAddress); if (nVendorID == 0 || nProductID == 0 || nBus == 0 || nAddress == 0) { XN_LOG_WARNING_RETURN(XN_STATUS_USB_DEVICE_OPEN_FAILED, "Invalid connection string: %s", strDevicePath); } // find device libusb_device** ppDevices; ssize_t nDeviceCount = libusb_get_device_list(g_InitData.pContext, &ppDevices); libusb_device* pRequestedDevice = NULL; for (ssize_t i = 0; i < nDeviceCount; ++i) { libusb_device* pDevice = ppDevices[i]; // get device descriptor libusb_device_descriptor desc; int rc = libusb_get_device_descriptor(pDevice, &desc); if (rc != 0) { libusb_free_device_list(ppDevices, 1); return (XN_STATUS_USB_ENUMERATE_FAILED); } // check if this is the requested device if (desc.idVendor == nVendorID && desc.idProduct == nProductID && libusb_get_bus_number(pDevice) == nBus && libusb_get_device_address(pDevice) == nAddress) { // add a reference to the device (so it won't be destroyed when list is freed) libusb_ref_device(pDevice); pRequestedDevice = pDevice; break; } } libusb_free_device_list(ppDevices, 1); nRetVal = xnUSBOpenDeviceImpl(pRequestedDevice, pDevHandlePtr); XN_IS_STATUS_OK(nRetVal); return (XN_STATUS_OK); }
static int initialize_device(struct usb_device *dev) { libusb_device *newlib_dev = dev->dev; int num_configurations; size_t alloc_size; int r; int i; /* device descriptor is identical in both libs */ r = libusb_get_device_descriptor(newlib_dev, (struct libusb_device_descriptor *) &dev->descriptor); if (r < 0) { usbi_err("error %d getting device descriptor", r); return compat_err(r); } num_configurations = dev->descriptor.bNumConfigurations; alloc_size = sizeof(struct usb_config_descriptor) * num_configurations; dev->config = malloc(alloc_size); if (!dev->config) return -ENOMEM; memset(dev->config, 0, alloc_size); /* even though structures are identical, we can't just use libusb-1.0's * config descriptors because we have to store all configurations in * a single flat memory area (libusb-1.0 provides separate allocations). * we hand-copy libusb-1.0's descriptors into our own structures. */ for (i = 0; i < num_configurations; i++) { struct libusb_config_descriptor *newlib_config; r = libusb_get_config_descriptor(newlib_dev, i, &newlib_config); if (r < 0) { clear_device(dev); free(dev->config); return compat_err(r); } r = copy_config_descriptor(dev->config + i, newlib_config); libusb_free_config_descriptor(newlib_config); if (r < 0) { clear_device(dev); free(dev->config); return r; } } /* libusb doesn't implement this and it doesn't seem that important. If * someone asks for it, we can implement it in v1.1 or later. */ dev->num_children = 0; dev->children = NULL; libusb_ref_device(newlib_dev); return 0; }
UsbDevice * usbFindDevice (UsbDeviceChooser chooser, void *data) { int result; UsbDeviceExtension *devx; if (!usbContext) { if ((result = libusb_init(&usbContext)) != LIBUSB_SUCCESS) { usbSetErrno(result, "libusb_init"); return NULL; } } if (!usbDeviceList) { ssize_t count; if ((count = libusb_get_device_list(usbContext, &usbDeviceList)) < 0) { usbSetErrno(count, "libusb_get_device_list"); return NULL; } usbDeviceCount = count; } if ((devx = malloc(sizeof(*devx)))) { libusb_device **libusbDevice = usbDeviceList; int deviceCount = usbDeviceCount; while (deviceCount) { deviceCount -= 1; devx->device = *libusbDevice++; libusb_ref_device(devx->device); devx->handle = NULL; { UsbDevice *device = usbTestDevice(devx, chooser, data); if (device) return device; } libusb_unref_device(devx->device); } free(devx); } else { logMallocError(); } return NULL; }
int libusb_submit_transfer(struct libusb_transfer *transfer) { usbi_transfer *itransfer = (usbi_transfer *)transfer; libusb_device_handle *dev_handle = transfer->dev_handle; libusb_context *ctx = dev_handle->dev->ctx; usbfs_urb *urb = &itransfer->urb; int rc; if (transfer->type != LIBUSB_TRANSFER_TYPE_BULK || transfer->timeout != 0 || transfer->callback == NULL) { return LIBUSB_ERROR_INVALID_PARAM; } if (itransfer->submitted) { return LIBUSB_ERROR_BUSY; } libusb_ref_device(dev_handle->dev); itransfer->submitted = true; urb->status = INT32_MIN; urb->endpoint = transfer->endpoint; urb->buffer = transfer->buffer; urb->buffer_length = transfer->length; rc = ioctl(dev_handle->pollfd.fd, IOCTL_USBFS_SUBMITURB, urb); if (rc < 0) { if (errno == ENODEV) { rc = LIBUSB_ERROR_NO_DEVICE; } else { rc = LIBUSB_ERROR_IO; } itransfer->submitted = false; libusb_unref_device(dev_handle->dev); usbi_log_error(ctx, "Could not submit %s transfer %p (length: %d): %s (%d)", (LIBUSB_ENDPOINT_IN & transfer->endpoint) != 0 ? "read" : "write", transfer, transfer->length, get_errno_name(errno), errno); return rc; } node_insert_before(&dev_handle->itransfer_sentinel, &itransfer->node); return LIBUSB_SUCCESS; }
/** * \brief Create the device. * * This private constructor is used to build a Device object from * the libusb data structure. * * \param _dev The libusb_device structure to use for the * Device. * \param _dev_handle The libusb_device_handle structure to use if * the device is already open. */ Device::Device(ContextHolderPtr _context, libusb_device *_dev, libusb_device_handle *_dev_handle) : context(_context), dev(_dev), dev_handle(_dev_handle) { debug(LOG_DEBUG, DEBUG_LOG, 0, "create a device from the libusb structure"); // increment the reference counter libusb_ref_device(dev); // find out whether this is a broken device DeviceDescriptorPtr d = descriptor(); if (d->idVendor() == VENDOR_THE_IMAGING_SOURCE) { debug(LOG_DEBUG, DEBUG_LOG, 0, "broken camera: The Imaging Source"); broken = BROKEN_THE_IMAGING_SOURCE; } }
USBDevice::USBDevice(void *device) : m_timeout(1000), m_bufferSize(0), m_mutex(QMutex::Recursive), m_inboundTransfer(NULL), m_readError(false) { m_data = new USBDevicePrivateData; m_data->device = (libusb_device*) device; m_data->handle = NULL; setErrorString(LIBUSB_SUCCESS); // reference decive to prevent collection of device information libusb_ref_device(m_data->device); libusb_get_device_descriptor(m_data->device, &m_data->descriptor); }
/* libusb-callbacks for when devices are added/removed. It is also called * manually when we detect a hub, since we risk devices being added before we * see for example the YKUSH HID device */ static void usb_device_added(struct usb_monitor_ctx *ctx, libusb_device *dev) { //Check if device is connected to a port we control struct usb_port *port; struct libusb_device_descriptor desc; uint8_t path[USB_PATH_MAX]; uint8_t path_len; libusb_get_device_descriptor(dev, &desc); usb_helpers_fill_port_array(dev, path, &path_len); port = usb_monitor_lists_find_port_path(ctx, path, path_len); if (!port) return; //The enabled check is needed here sine we enable/disable async. So we can //process a disabled request before an add event. Of course, device will //most likely be remove in the next iteration of loop, but still ... if (port->msg_mode == RESET || !port->enabled) return; //Need to check port if it already has a device, since we can risk that we //are called two times for one device if (port->dev && port->dev == dev) return; USB_DEBUG_PRINT_SYSLOG(ctx, LOG_INFO, "Device: %.4x:%.4x added\n", desc.idVendor, desc.idProduct); //We need to configure port. So far, this is all generic port->vp.vid = desc.idVendor; port->vp.pid = desc.idProduct; port->status = PORT_DEV_CONNECTED; port->dev = dev; port->msg_mode = PING; libusb_ref_device(dev); usb_monitor_print_ports(ctx); //Whenever we detect a device, we need to add to timeout to send ping. //However, we need to wait longer than the initial five seconds to let //usb_modeswitch potentially works its magic usb_helpers_start_timeout(port, ADDED_TIMEOUT_SEC); }
bool USBSubsystem::find_xbox360_controller(int id, libusb_device** xbox_device, XPadDevice* type) { libusb_device** list; ssize_t num_devices = libusb_get_device_list(NULL, &list); int id_count = 0; for(ssize_t dev_it = 0; dev_it < num_devices; ++dev_it) { libusb_device* dev = list[dev_it]; libusb_device_descriptor desc; int ret = libusb_get_device_descriptor(dev, &desc); if (ret != LIBUSB_SUCCESS) { log_warn("libusb_get_device_descriptor() failed: " << usb_strerror(ret)); } else { for(int i = 0; i < xpad_devices_count; ++i) { if (desc.idVendor == xpad_devices[i].idVendor && desc.idProduct == xpad_devices[i].idProduct) { if (id_count == id) { *xbox_device = dev; *type = xpad_devices[i]; // increment ref count, user must free the device libusb_ref_device(*xbox_device); libusb_free_device_list(list, 1 /* unref_devices */); return true; } else { id_count += 1; } } } } } libusb_free_device_list(list, 1 /* unref_devices */); return false; }
bool OpenPortalHandle(libusb_device_handle **phPortalHandle) { int OK; libusb_device **list; ssize_t i = 0; struct libusb_device_descriptor attributes; *phPortalHandle = NULL; libusb_init (&g_ctx); libusb_set_debug(g_ctx,0); ssize_t cnt = libusb_get_device_list(g_ctx, &list); if (cnt < 0) return false; for (i = 0; i < cnt; i++) { libusb_device *device = list[i]; libusb_get_device_descriptor(device,&attributes); if (((attributes.idVendor == 0x12ba) || (attributes.idVendor == 0x54c)) || (attributes.idVendor == 0x1430)) { if ((attributes.idProduct == 0x150) || (attributes.idProduct == 0x967)) { printf("Found portal usb device\n"); int err; libusb_ref_device(device); err= libusb_open(device, phPortalHandle); printf ("usb open: %s\n",libusb_error(err)); err = libusb_claim_interface(*phPortalHandle, 0); printf ("claim interface: %s\n",libusb_error(err)); break; } } } libusb_free_device_list(list, 1); // HidD_GetHidGuid(&guid); // OK = OpenPortalHandleFromGUID(guid, phPortalHandle); return (*phPortalHandle != NULL); }
int libusb_open(libusb_device *dev, libusb_device_handle **dev_handle_ptr) { libusb_context *ctx = dev->ctx; libusb_device_handle *dev_handle; jmethodID open_device_mid; int fd; dev_handle = calloc(1, sizeof(libusb_device_handle)); if (dev_handle == NULL) { usbi_log_error(ctx, "Could not allocate device handle"); return LIBUSB_ERROR_NO_MEM; } open_device_mid = (*android_env)->GetMethodID(android_env, (*android_env)->GetObjectClass(android_env, android_service), "openDevice", "(Landroid/hardware/usb/UsbDevice;)I"); // FIXME: check result fd = (*android_env)->CallIntMethod(android_env, android_service, open_device_mid, dev->device); // FIXME: check result if (fd < 0) { free(dev_handle); return LIBUSB_ERROR_NO_DEVICE; } dev_handle->dev = libusb_ref_device(dev); dev_handle->pollfd.fd = fd; dev_handle->pollfd.events = POLLOUT; dev_handle->disconnected = false; node_reset(&dev_handle->itransfer_sentinel); node_insert_before(&ctx->dev_handle_sentinel, &dev_handle->node); ++ctx->dev_handle_count; *dev_handle_ptr = dev_handle; usbi_log_debug(ctx, "Opened device %p (context: %p, name: %s, fd: %d)", dev, ctx, dev->name, dev_handle->pollfd.fd); if (ctx->pollfd_added_callback != NULL) { ctx->pollfd_added_callback(dev_handle->pollfd.fd, dev_handle->pollfd.events, ctx->pollfd_user_data); } return LIBUSB_SUCCESS; }
/** * @brief Find the first camera which is connected. * * Asks libusb for all the devices connected to the computer, and returns * the first PTP device it can find. * * @todo Exposes the fact that we actually use libusb. Hide this fact in the future. * @return A pointer to a \c libusb_device which represents the camera found, or NULL if none found. */ libusb_device * CameraBase::find_first_camera() { // discover devices libusb_device **list; libusb_device *found = NULL; ssize_t cnt = libusb_get_device_list(NULL, &list); ssize_t i = 0, j = 0, k = 0; int err = 0; if (cnt < 0) { return NULL; } for (i = 0; i < cnt; i++) { libusb_device *device = list[i]; struct libusb_config_descriptor * desc; int r = libusb_get_active_config_descriptor(device, &desc); if (r < 0) { return NULL; } for(j = 0; j < desc->bNumInterfaces; j++) { struct libusb_interface interface = desc->interface[j]; for(k = 0; k < interface.num_altsetting; k++) { struct libusb_interface_descriptor altsetting = interface.altsetting[k]; if(altsetting.bInterfaceClass == 6) { // If this has the PTP interface found = device; break; } } if(found) break; } libusb_free_config_descriptor(desc); if(found) break; } if(found) { libusb_ref_device(found); // Add a reference to the device so it doesn't get destroyed when we free_device_list } libusb_free_device_list(list, 1); // Free the device list with dereferencing. Shouldn't delete our device, since we ref'd it return found; }
/* * Finds a USB device. * the returned device must be unreferenced when it is no longer needed using libusb_unref_device. */ XnStatus FindDevice(XnUInt16 nVendorID, XnUInt16 nProductID, void* pExtraParam, libusb_device** ppDevice) { *ppDevice = NULL; // get device list libusb_device** ppDevices; ssize_t nDeviceCount = libusb_get_device_list(g_InitData.pContext, &ppDevices); // check for error if (nDeviceCount < 0) { return (XN_STATUS_USB_ENUMERATE_FAILED); } // enumerate over the devices for (ssize_t i = 0; i < nDeviceCount; ++i) { libusb_device* pDevice = ppDevices[i]; // get device descriptor libusb_device_descriptor desc; int rc = libusb_get_device_descriptor(pDevice, &desc); if (rc != 0) { return (XN_STATUS_USB_ENUMERATE_FAILED); } // check if this is the requested device if (desc.idVendor == nVendorID && desc.idProduct == nProductID) { // add a reference to the device (so it won't be destroyed when list is freed) libusb_ref_device(pDevice); *ppDevice = pDevice; break; } } // free the list (also dereference each device) libusb_free_device_list(ppDevices, 1); return (XN_STATUS_OK); }
uvclib_usb_device(const usb_device_info& info) { int status = libusb_init(&_usb_context); if(status < 0) throw linux_backend_exception(to_string() << "libusb_init(...) returned " << libusb_error_name(status)); std::vector<usb_device_info> results; uvclib_usb_device::foreach_usb_device(_usb_context, [&results, info, this](const usb_device_info& i, libusb_device* dev) { if (i.unique_id == info.unique_id) { _usb_device = dev; libusb_ref_device(dev); } }); _mi = info.mi; }
int API_EXPORTED libusb_open(libusb_device *dev, libusb_device_handle **handle) { struct libusb_context *ctx = DEVICE_CTX(dev); struct libusb_device_handle *_handle; size_t priv_size = usbi_backend->device_handle_priv_size; int r; usbi_dbg("open %d.%d", dev->bus_number, dev->device_address); _handle = malloc(sizeof(*_handle) + priv_size); if (!_handle) return LIBUSB_ERROR_NO_MEM; r = usbi_mutex_init(&_handle->lock, NULL); if (r) { free(_handle); return LIBUSB_ERROR_OTHER; } _handle->dev = libusb_ref_device(dev); _handle->claimed_interfaces = 0; memset(&_handle->os_priv, 0, priv_size); r = usbi_backend->open(_handle); if (r < 0) { usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r); libusb_unref_device(dev); usbi_mutex_destroy(&_handle->lock); free(_handle); return r; } usbi_mutex_lock(&ctx->open_devs_lock); list_add(&_handle->list, &ctx->open_devs); usbi_mutex_unlock(&ctx->open_devs_lock); *handle = _handle; usbi_fd_notification(ctx); return 0; }
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(); } }
libusb_device* usb_find_device_by_path(uint8_t busnum, uint8_t devnum) { libusb_device* ret_device = 0; libusb_device** list; ssize_t num_devices = libusb_get_device_list(NULL, &list); for(ssize_t dev_it = 0; dev_it < num_devices; ++dev_it) { libusb_device* dev = list[dev_it]; if (busnum == libusb_get_bus_number(dev) && devnum == libusb_get_device_address(dev)) { ret_device = dev; libusb_ref_device(ret_device); break; } } libusb_free_device_list(list, 1 /* unref_devices */); return ret_device; }
ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx, libusb_device ***list) { struct discovered_devs *discdevs = discovered_devs_alloc(); struct libusb_device **ret; int r = 0; ssize_t i, len; USBI_GET_CONTEXT(ctx); usbi_dbg(""); if (!discdevs) return LIBUSB_ERROR_NO_MEM; r = usbi_backend->get_device_list(ctx, &discdevs); if (r < 0) { len = r; goto out; } len = discdevs->len; ret = malloc(sizeof(void *) * (len + 1)); if (!ret) { len = LIBUSB_ERROR_NO_MEM; goto out; } ret[len] = NULL; for (i = 0; i < len; i++) { struct libusb_device *dev = discdevs->devices[i]; ret[i] = libusb_ref_device(dev); } *list = ret; out: discovered_devs_free(discdevs); return len; }