Add convenience function to find and open a device by USB VID+PID
Lots of libusb apps I write are simple test apps not intended to be real apps. Having a function available to quickly locate my device will be handy in such situations.
This commit is contained in:
parent
9cfdb494fc
commit
23f8fb8baf
3 changed files with 37 additions and 31 deletions
|
@ -81,36 +81,8 @@ static struct libusb_bulk_transfer irqtrf = {
|
||||||
|
|
||||||
static int find_dpfp_device(void)
|
static int find_dpfp_device(void)
|
||||||
{
|
{
|
||||||
struct libusb_device *dev;
|
devh = libusb_open_device_with_vid_pid(0x05ba, 0x000a);
|
||||||
struct libusb_device *found = NULL;
|
return devh ? 0 : -EIO;
|
||||||
struct libusb_device **devs;
|
|
||||||
size_t i = 0;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
r = libusb_get_device_list(&devs);
|
|
||||||
if (r < 0)
|
|
||||||
return r;
|
|
||||||
|
|
||||||
while ((dev = devs[i++]) != NULL) {
|
|
||||||
struct libusb_dev_descriptor *desc = libusb_device_get_descriptor(dev);
|
|
||||||
if (desc->idVendor == 0x05ba && desc->idProduct == 0x000a) {
|
|
||||||
found = dev;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
r = -ENODEV;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
devh = libusb_open(dev);
|
|
||||||
if (!devh)
|
|
||||||
r = -EIO;
|
|
||||||
|
|
||||||
out:
|
|
||||||
libusb_free_device_list(devs, 1);
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int print_f0_data(void)
|
static int print_f0_data(void)
|
||||||
|
|
|
@ -385,6 +385,37 @@ API_EXPORTED struct libusb_dev_handle *libusb_open(struct libusb_device *dev)
|
||||||
return devh;
|
return devh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* convenience function for finding a device with a particular vendor/product
|
||||||
|
* combination. has limitations and is hence not intended for use in "real
|
||||||
|
* applications": if multiple devices have the same VID+PID it'll only
|
||||||
|
* give you the first one, etc. */
|
||||||
|
API_EXPORTED struct libusb_dev_handle *libusb_open_device_with_vid_pid(
|
||||||
|
uint16_t vendor_id, uint16_t product_id)
|
||||||
|
{
|
||||||
|
struct libusb_device **devs;
|
||||||
|
struct libusb_device *found = NULL;
|
||||||
|
struct libusb_device *dev;
|
||||||
|
struct libusb_dev_handle *devh;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
if (libusb_get_device_list(&devs) < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
while ((dev = devs[i++]) != NULL) {
|
||||||
|
struct libusb_dev_descriptor *desc = libusb_device_get_descriptor(dev);
|
||||||
|
if (desc->idVendor == vendor_id && desc->idProduct == product_id) {
|
||||||
|
found = dev;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (found)
|
||||||
|
devh = libusb_open(found);
|
||||||
|
|
||||||
|
libusb_free_device_list(devs, 1);
|
||||||
|
return devh;
|
||||||
|
}
|
||||||
|
|
||||||
static void do_close(struct libusb_dev_handle *devh)
|
static void do_close(struct libusb_dev_handle *devh)
|
||||||
{
|
{
|
||||||
usbi_remove_pollfd(devh->fd);
|
usbi_remove_pollfd(devh->fd);
|
||||||
|
|
|
@ -231,10 +231,13 @@ void libusb_device_unref(libusb_device *dev);
|
||||||
|
|
||||||
libusb_dev_handle *libusb_open(libusb_device *dev);
|
libusb_dev_handle *libusb_open(libusb_device *dev);
|
||||||
void libusb_close(libusb_dev_handle *devh);
|
void libusb_close(libusb_dev_handle *devh);
|
||||||
struct libusb_device *libusb_devh_get_device(libusb_dev_handle *devh);
|
libusb_device *libusb_devh_get_device(libusb_dev_handle *devh);
|
||||||
int libusb_claim_interface(libusb_dev_handle *dev, int iface);
|
int libusb_claim_interface(libusb_dev_handle *dev, int iface);
|
||||||
int libusb_release_interface(libusb_dev_handle *dev, int iface);
|
int libusb_release_interface(libusb_dev_handle *dev, int iface);
|
||||||
|
|
||||||
|
libusb_dev_handle *libusb_open_device_with_vid_pid(uint16_t vendor_id,
|
||||||
|
uint16_t product_id);
|
||||||
|
|
||||||
/* async I/O */
|
/* async I/O */
|
||||||
|
|
||||||
libusb_urb_handle *libusb_async_control_transfer(libusb_dev_handle *devh,
|
libusb_urb_handle *libusb_async_control_transfer(libusb_dev_handle *devh,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue