X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fusb.c;h=6bd9e8182960cc330164350ccc4f37facd8a7aea;hp=c3c34e009b2583d59750311e3309ceb316de4ecb;hb=fe953391bd7782813256c02350afa1996c773659;hpb=3393f185a5109832db68966a44b46204cc5c53c5 diff --git a/src/usb.c b/src/usb.c index c3c34e00..6bd9e818 100644 --- a/src/usb.c +++ b/src/usb.c @@ -28,7 +28,7 @@ #include "libsigrok-internal.h" /* SR_CONF_CONN takes one of these: */ -#define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$" +#define CONN_USB_VIDPID "^([0-9a-fA-F]{4})\\.([0-9a-fA-F]{4})$" #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$" #define LOG_PREFIX "usb" @@ -38,7 +38,6 @@ typedef int libusb_os_handle; #endif /** Custom GLib event source for libusb I/O. - * @internal */ struct usb_source { GSource base; @@ -139,7 +138,7 @@ static gboolean usb_source_dispatch(GSource *source, sr_err("Callback not set, cannot dispatch event."); return G_SOURCE_REMOVE; } - keep = (*(sr_receive_data_callback)callback)(-1, revents, user_data); + keep = (*SR_RECEIVE_DATA_CALLBACK(callback))(-1, revents, user_data); if (G_LIKELY(keep) && G_LIKELY(!g_source_is_destroyed(source))) { if (usource->timeout_us >= 0) @@ -184,7 +183,7 @@ static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd, return; pollfd = g_slice_new(GPollFD); -#ifdef G_OS_WIN32 +#ifdef _WIN32 events = G_IO_IN; #endif pollfd->fd = (gintptr)fd; @@ -329,8 +328,7 @@ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) if ((mstr = g_match_info_fetch(match, 2))) pid = strtoul(mstr, NULL, 16); g_free(mstr); - sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.", - vid, pid); + /* Trying to find USB device via VID:PID. */ } else { g_match_info_unref(match); g_regex_unref(reg); @@ -343,8 +341,7 @@ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) if ((mstr = g_match_info_fetch(match, 2))) addr = strtoul(mstr, NULL, 10); g_free(mstr); - sr_dbg("Trying to find USB device with bus.address = " - "%d.%d.", bus, addr); + /* Trying to find USB device via bus.address. */ } } g_match_info_unref(match); @@ -386,14 +383,13 @@ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = " "%d.%d).", des.idVendor, des.idProduct, b, a); - usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), - libusb_get_device_address(devlist[i]), NULL); + usb = sr_usb_dev_inst_new(b, a, NULL); devices = g_slist_append(devices, usb); } libusb_free_device_list(devlist, 1); - sr_dbg("Found %d device(s).", g_slist_length(devices)); - + /* No log message for #devices found (caller will log that). */ + return devices; } @@ -459,7 +455,7 @@ SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx, if (!source) return SR_ERR; - g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL); + g_source_set_callback(source, G_SOURCE_FUNC(cb), cb_data, NULL); ret = sr_session_source_add_internal(session, ctx->libusb_ctx, source); g_source_unref(source); @@ -510,3 +506,47 @@ SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len) return SR_OK; } + +/** + * Check the USB configuration to determine if this device has a given + * manufacturer and product string. + * + * @return TRUE if the device's configuration profile strings + * configuration, FALSE otherwise. + */ +SR_PRIV gboolean usb_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; +}