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 /* Some USBTMC-specific enums, as defined in the USBTMC standard. */
33 #define SUBCLASS_USBTMC 0x03
34 #define USBTMC_USB488 0x01
36 /* Message logging helpers with driver-specific prefix string. */
37 #define DRIVER_LOG_DOMAIN "usb: "
38 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
39 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
40 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
41 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
42 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
43 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
46 * Find USB devices according to a connection string.
48 * @param usb_ctx libusb context to use while scanning.
49 * @param conn Connection string specifying the device(s) to match. This
50 * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
52 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
53 * matching the device that matched the connection string. The GSList and
54 * its contents must be freed by the caller.
56 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
58 struct sr_usb_dev_inst *usb;
59 struct libusb_device **devlist;
60 struct libusb_device_descriptor des;
64 int vid, pid, bus, addr, b, a, ret, i;
67 vid = pid = bus = addr = 0;
68 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
69 if (g_regex_match(reg, conn, 0, &match)) {
70 if ((mstr = g_match_info_fetch(match, 1)))
71 vid = strtoul(mstr, NULL, 16);
74 if ((mstr = g_match_info_fetch(match, 2)))
75 pid = strtoul(mstr, NULL, 16);
77 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
80 g_match_info_unref(match);
82 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
83 if (g_regex_match(reg, conn, 0, &match)) {
84 if ((mstr = g_match_info_fetch(match, 1)))
85 bus = strtoul(mstr, NULL, 16);
88 if ((mstr = g_match_info_fetch(match, 2)))
89 addr = strtoul(mstr, NULL, 16);
91 sr_dbg("Trying to find USB device with bus.address = "
95 g_match_info_unref(match);
98 if (vid + pid + bus + addr == 0) {
99 sr_err("Neither VID:PID nor bus.address was specified.");
104 sr_err("Invalid bus specified: %d.", bus);
109 sr_err("Invalid address specified: %d.", addr);
113 /* Looks like a valid USB device specification, but is it connected? */
115 libusb_get_device_list(usb_ctx, &devlist);
116 for (i = 0; devlist[i]; i++) {
117 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
118 sr_err("Failed to get device descriptor: %s.",
119 libusb_error_name(ret));
123 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
126 b = libusb_get_bus_number(devlist[i]);
127 a = libusb_get_device_address(devlist[i]);
128 if (bus + addr && (b != bus || a != addr))
131 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
132 "%d.%d).", des.idVendor, des.idProduct, b, a);
134 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
135 libusb_get_device_address(devlist[i]), NULL);
136 devices = g_slist_append(devices, usb);
138 libusb_free_device_list(devlist, 1);
140 sr_dbg("Found %d device(s).", g_slist_length(devices));
146 * Find USB devices supporting the USBTMC class
148 * @param usb_ctx libusb context to use while scanning.
150 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
151 * indicating devices with USBTMC support.
153 SR_PRIV GSList *sr_usb_find_usbtmc(libusb_context *usb_ctx)
155 struct sr_usb_dev_inst *usb;
156 struct libusb_device **devlist;
157 struct libusb_device_descriptor des;
158 struct libusb_config_descriptor *confdes;
159 const struct libusb_interface_descriptor *intfdes;
161 int confidx, intfidx, ret, i;
164 libusb_get_device_list(usb_ctx, &devlist);
165 for (i = 0; devlist[i]; i++) {
166 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
167 sr_err("Failed to get device descriptor: %s.",
168 libusb_error_name(ret));
172 for (confidx = 0; confidx < des.bNumConfigurations; confidx++) {
173 if (libusb_get_config_descriptor(devlist[i], confidx, &confdes) != 0) {
174 sr_err("Failed to get configuration descriptor.");
177 for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) {
178 intfdes = confdes->interface[intfidx].altsetting;
179 if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION
180 || intfdes->bInterfaceSubClass != SUBCLASS_USBTMC
181 || intfdes->bInterfaceProtocol != USBTMC_USB488)
183 sr_dbg("Found USBTMC device (VID:PID = %04x:%04x, bus.address = "
184 "%d.%d).", des.idVendor, des.idProduct,
185 libusb_get_bus_number(devlist[i]),
186 libusb_get_device_address(devlist[i]));
188 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
189 libusb_get_device_address(devlist[i]), NULL);
190 devices = g_slist_append(devices, usb);
194 libusb_free_device_list(devlist, 1);
196 sr_dbg("Found %d device(s).", g_slist_length(devices));
201 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
203 struct libusb_device **devlist;
204 struct libusb_device_descriptor des;
205 int ret, r, cnt, i, a, b;
207 sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
209 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
210 sr_err("Failed to retrieve device list: %s.",
211 libusb_error_name(cnt));
216 for (i = 0; i < cnt; i++) {
217 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
218 sr_err("Failed to get device descriptor: %s.",
219 libusb_error_name(r));
223 b = libusb_get_bus_number(devlist[i]);
224 a = libusb_get_device_address(devlist[i]);
225 if (b != usb->bus || a != usb->address)
228 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
229 sr_err("Failed to open device: %s.",
230 libusb_error_name(r));
234 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
235 "%d.%d).", des.idVendor, des.idProduct, b, a);
241 libusb_free_device_list(devlist, 1);