From: Bert Vermeulen Date: Tue, 4 Dec 2012 22:25:11 +0000 (+0100) Subject: usb: sr_usb_find() uses standardized connection string to find a USB device X-Git-Tag: dsupstream~478 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=7ae6a75826be6d7ddd885947630ecb26ba9fa1a5;p=libsigrok.git usb: sr_usb_find() uses standardized connection string to find a USB device --- diff --git a/hardware/common/usb.c b/hardware/common/usb.c index 2dabfbee..4c0c5b03 100644 --- a/hardware/common/usb.c +++ b/hardware/common/usb.c @@ -38,8 +38,20 @@ #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args) #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args) -SR_PRIV GSList *sr_usb_connect(libusb_context *usb_ctx, const char *conn) +/** + * Find USB devices according to a connection string. + * + * @param usb_ctx libusb context to use while scanning. + * @param conn Connection string specifying the device(s) to match. This + * can be of the form ".
", or ".". + * + * @return A GSList of struct sr_usb_dev_inst, with bus and address fields + * matching the device that matched the connection string. The GSList and + * its contents must be freed by the caller. + */ +SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) { + struct sr_usb_dev_inst *usb; struct libusb_device **devlist; struct libusb_device_descriptor des; GSList *devices; @@ -115,25 +127,20 @@ SR_PRIV GSList *sr_usb_connect(libusb_context *usb_ctx, const char *conn) continue; } - b = libusb_get_bus_number(devlist[i]); - a = libusb_get_device_address(devlist[i]); - - if (vid + pid && - (des.idVendor != vid || des.idProduct != pid)) { - sr_spew("VID:PID = %04x:%04x (%d.%d) doesn't match.", - des.idVendor, des.idProduct, b, a); + if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) continue; - } - if (bus + addr && (b != bus || a != addr)) { - sr_spew("bus.address = %d.%d does not match.", b, a); + b = libusb_get_bus_number(devlist[i]); + a = libusb_get_device_address(devlist[i]); + if (bus + addr && (b != bus || a != addr)) continue; - } sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = " "%d.%d).", des.idVendor, des.idProduct, b, a); - devices = g_slist_append(devices, devlist[i]); + usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), + libusb_get_device_address(devlist[i]), NULL); + devices = g_slist_append(devices, usb); } libusb_free_device_list(devlist, 1); diff --git a/hardware/uni-t-dmm/api.c b/hardware/uni-t-dmm/api.c index 11755bcd..b4beb504 100644 --- a/hardware/uni-t-dmm/api.c +++ b/hardware/uni-t-dmm/api.c @@ -100,7 +100,7 @@ static GSList *hw_scan(GSList *options) devices = NULL; - if (!(l = sr_usb_connect(drvc->sr_ctx->libusb_ctx, "1a86.e008"))) + if (!(l = sr_usb_find(drvc->sr_ctx->libusb_ctx, "1a86.e008"))) return NULL; for (i = 0; i < (int)g_slist_length(l); i++) { diff --git a/libsigrok-internal.h b/libsigrok-internal.h index ddb4f995..d9f2bcd8 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -165,7 +165,7 @@ SR_PRIV int ezusb_upload_firmware(libusb_device *dev, int configuration, /*--- hardware/common/usb.c -------------------------------------------------*/ #ifdef HAVE_LIBUSB_1_0 -SR_PRIV GSList *sr_usb_connect(libusb_context *usb_ctx, const char *conn); +SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn); SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb); #endif