2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 /* SR_HWCAP_CONN takes one of these: */
29 #define CONN_USB_VIDPID "^([0-9a-z]{1,4})\\.([0-9a-z]{1,4})$"
30 #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
32 /* Message logging helpers with driver-specific prefix string. */
33 #define DRIVER_LOG_DOMAIN "usb: "
34 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
36 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
37 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
38 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
39 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
42 * Find USB devices according to a connection string.
44 * @param usb_ctx libusb context to use while scanning.
45 * @param conn Connection string specifying the device(s) to match. This
46 * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
48 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
49 * matching the device that matched the connection string. The GSList and
50 * its contents must be freed by the caller.
52 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
54 struct sr_usb_dev_inst *usb;
55 struct libusb_device **devlist;
56 struct libusb_device_descriptor des;
60 int vid, pid, bus, addr, b, a, ret, i;
63 vid = pid = bus = addr = 0;
64 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
65 if (g_regex_match(reg, conn, 0, &match)) {
66 if ((mstr = g_match_info_fetch(match, 1)))
67 vid = strtoul(mstr, NULL, 16);
70 if ((mstr = g_match_info_fetch(match, 2)))
71 pid = strtoul(mstr, NULL, 16);
73 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
76 g_match_info_unref(match);
78 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
79 if (g_regex_match(reg, conn, 0, &match)) {
80 if ((mstr = g_match_info_fetch(match, 1)))
81 bus = strtoul(mstr, NULL, 16);
84 if ((mstr = g_match_info_fetch(match, 2)))
85 addr = strtoul(mstr, NULL, 16);
87 sr_dbg("Trying to find USB device with bus.address = "
91 g_match_info_unref(match);
94 if (vid + pid + bus + addr == 0) {
95 sr_err("Neither VID:PID nor bus.address was specified.");
100 sr_err("Invalid bus specified: %d.", bus);
105 sr_err("Invalid address specified: %d.", addr);
109 /* Looks like a valid USB device specification, but is it connected? */
111 libusb_get_device_list(usb_ctx, &devlist);
112 for (i = 0; devlist[i]; i++) {
113 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
114 sr_err("Failed to get device descriptor: %s.",
115 libusb_error_name(ret));
119 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
122 b = libusb_get_bus_number(devlist[i]);
123 a = libusb_get_device_address(devlist[i]);
124 if (bus + addr && (b != bus || a != addr))
127 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
128 "%d.%d).", des.idVendor, des.idProduct, b, a);
130 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
131 libusb_get_device_address(devlist[i]), NULL);
132 devices = g_slist_append(devices, usb);
134 libusb_free_device_list(devlist, 1);
136 sr_dbg("Found %d device(s).", g_slist_length(devices));
141 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
143 struct libusb_device **devlist;
144 struct libusb_device_descriptor des;
145 int ret, r, cnt, i, a, b;
147 sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
149 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
150 sr_err("Failed to retrieve device list: %s.",
151 libusb_error_name(cnt));
156 for (i = 0; i < cnt; i++) {
157 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
158 sr_err("Failed to get device descriptor: %s.",
159 libusb_error_name(r));
163 b = libusb_get_bus_number(devlist[i]);
164 a = libusb_get_device_address(devlist[i]);
165 if (b != usb->bus || a != usb->address)
168 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
169 sr_err("Failed to open device: %s.",
170 libusb_error_name(r));
174 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
175 "%d.%d).", des.idVendor, des.idProduct, b, a);
181 libusb_free_device_list(devlist, 1);