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)
41 SR_PRIV GSList *sr_usb_connect(libusb_context *usb_ctx, const char *conn)
43 struct libusb_device **devlist;
44 struct libusb_device_descriptor des;
48 int vid, pid, bus, addr, b, a, ret, i;
51 vid = pid = bus = addr = 0;
52 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
53 if (g_regex_match(reg, conn, 0, &match)) {
55 if ((mstr = g_match_info_fetch(match, 1))) {
56 vid = strtoul(mstr, NULL, 16);
57 sr_spew("Extracted VID 0x%04x.", vid);
62 if ((mstr = g_match_info_fetch(match, 2))) {
63 pid = strtoul(mstr, NULL, 16);
64 sr_spew("Extracted PID 0x%04x.", pid);
67 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
70 g_match_info_unref(match);
72 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
73 if (g_regex_match(reg, conn, 0, &match)) {
75 if ((mstr = g_match_info_fetch(match, 1))) {
76 bus = strtoul(mstr, NULL, 16);
77 sr_spew("Extracted bus %d.", bus);
81 /* Extract address. */
82 if ((mstr = g_match_info_fetch(match, 2))) {
83 addr = strtoul(mstr, NULL, 16);
84 sr_spew("Extracted address %d.", addr);
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 found.");
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: %d.", ret);
118 b = libusb_get_bus_number(devlist[i]);
119 a = libusb_get_device_address(devlist[i]);
122 (des.idVendor != vid || des.idProduct != pid)) {
123 sr_spew("VID:PID = %04x:%04x (%d.%d) doesn't match.",
124 des.idVendor, des.idProduct, b, a);
128 if (bus + addr && (b != bus || a != addr)) {
129 sr_spew("bus.address = %d.%d does not match.", b, a);
133 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
134 "%d.%d).", des.idVendor, des.idProduct, b, a);
136 devices = g_slist_append(devices, devlist[i]);
138 libusb_free_device_list(devlist, 1);
140 sr_dbg("Found %d device(s).", g_slist_length(devices));
145 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
147 struct libusb_device **devlist;
148 struct libusb_device_descriptor des;
149 int ret, r, cnt, i, a, b;
151 sr_dbg("Trying to open USB device.");
153 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
154 sr_err("Failed to retrieve device list: %s.",
155 libusb_error_name(cnt));
160 for (i = 0; i < cnt; i++) {
161 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
162 sr_err("Failed to get device descriptor: %s.",
163 libusb_error_name(r));
167 b = libusb_get_bus_number(devlist[i]);
168 a = libusb_get_device_address(devlist[i]);
170 if (b != usb->bus || a != usb->address) {
171 sr_spew("bus.address = %d.%d does not match.", b, a);
175 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
176 sr_err("Failed to open device: %s.",
177 libusb_error_name(r));
181 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
182 "%d.%d).", des.idVendor, des.idProduct, b, a);
188 libusb_free_device_list(devlist, 1);