From 23f8fb8bafeeda93ce234cb40eb7219d2e36c19c Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Thu, 6 Mar 2008 23:43:57 +0000 Subject: [PATCH] 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. --- examples/dpfp.c | 32 ++------------------------------ libusb/core.c | 31 +++++++++++++++++++++++++++++++ libusb/libusb.h | 5 ++++- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/examples/dpfp.c b/examples/dpfp.c index 87a7da1..6b5124e 100644 --- a/examples/dpfp.c +++ b/examples/dpfp.c @@ -81,36 +81,8 @@ static struct libusb_bulk_transfer irqtrf = { static int find_dpfp_device(void) { - struct libusb_device *dev; - struct libusb_device *found = NULL; - 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; + devh = libusb_open_device_with_vid_pid(0x05ba, 0x000a); + return devh ? 0 : -EIO; } static int print_f0_data(void) diff --git a/libusb/core.c b/libusb/core.c index cd01515..a366c8c 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -385,6 +385,37 @@ API_EXPORTED struct libusb_dev_handle *libusb_open(struct libusb_device *dev) 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) { usbi_remove_pollfd(devh->fd); diff --git a/libusb/libusb.h b/libusb/libusb.h index cddc232..4e1bc14 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -231,10 +231,13 @@ void libusb_device_unref(libusb_device *dev); libusb_dev_handle *libusb_open(libusb_device *dev); 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_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 */ libusb_urb_handle *libusb_async_control_transfer(libusb_dev_handle *devh, -- 2.7.4