Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 1 | /* |
Uwe Hermann | 50985c2 | 2013-04-23 22:24:30 +0200 | [diff] [blame] | 2 | * This file is part of the libsigrok project. |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> |
| 5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <glib.h> |
| 24 | #include <libusb.h> |
| 25 | #include "libsigrok.h" |
| 26 | #include "libsigrok-internal.h" |
| 27 | |
Bert Vermeulen | 1953564 | 2013-01-21 23:22:47 +0100 | [diff] [blame] | 28 | /* SR_CONF_CONN takes one of these: */ |
Bert Vermeulen | 1eb0a0d | 2013-04-15 23:47:04 +0200 | [diff] [blame] | 29 | #define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$" |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 30 | #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$" |
| 31 | |
Bert Vermeulen | 9116262 | 2012-12-30 01:44:58 +0100 | [diff] [blame] | 32 | /* Some USBTMC-specific enums, as defined in the USBTMC standard. */ |
| 33 | #define SUBCLASS_USBTMC 0x03 |
| 34 | #define USBTMC_USB488 0x01 |
| 35 | |
Uwe Hermann | 29a2719 | 2013-05-03 21:59:32 +0200 | [diff] [blame] | 36 | /* Message logging helpers with subsystem-specific prefix string. */ |
| 37 | #define LOG_PREFIX "usb: " |
| 38 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) |
| 39 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) |
| 40 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) |
| 41 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) |
| 42 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) |
| 43 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 44 | |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 45 | /** |
| 46 | * Find USB devices according to a connection string. |
| 47 | * |
| 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>". |
| 51 | * |
| 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. |
| 55 | */ |
| 56 | SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 57 | { |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 58 | struct sr_usb_dev_inst *usb; |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 59 | struct libusb_device **devlist; |
| 60 | struct libusb_device_descriptor des; |
| 61 | GSList *devices; |
| 62 | GRegex *reg; |
| 63 | GMatchInfo *match; |
| 64 | int vid, pid, bus, addr, b, a, ret, i; |
| 65 | char *mstr; |
| 66 | |
| 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)) { |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 70 | if ((mstr = g_match_info_fetch(match, 1))) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 71 | vid = strtoul(mstr, NULL, 16); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 72 | g_free(mstr); |
| 73 | |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 74 | if ((mstr = g_match_info_fetch(match, 2))) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 75 | pid = strtoul(mstr, NULL, 16); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 76 | g_free(mstr); |
| 77 | sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.", |
| 78 | vid, pid); |
| 79 | } else { |
| 80 | g_match_info_unref(match); |
| 81 | g_regex_unref(reg); |
| 82 | reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL); |
| 83 | if (g_regex_match(reg, conn, 0, &match)) { |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 84 | if ((mstr = g_match_info_fetch(match, 1))) |
Bert Vermeulen | 1eb0a0d | 2013-04-15 23:47:04 +0200 | [diff] [blame] | 85 | bus = strtoul(mstr, NULL, 10); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 86 | g_free(mstr); |
| 87 | |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 88 | if ((mstr = g_match_info_fetch(match, 2))) |
Bert Vermeulen | 1eb0a0d | 2013-04-15 23:47:04 +0200 | [diff] [blame] | 89 | addr = strtoul(mstr, NULL, 10); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 90 | g_free(mstr); |
| 91 | sr_dbg("Trying to find USB device with bus.address = " |
| 92 | "%d.%d.", bus, addr); |
| 93 | } |
| 94 | } |
| 95 | g_match_info_unref(match); |
| 96 | g_regex_unref(reg); |
| 97 | |
| 98 | if (vid + pid + bus + addr == 0) { |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 99 | sr_err("Neither VID:PID nor bus.address was specified."); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | if (bus > 64) { |
| 104 | sr_err("Invalid bus specified: %d.", bus); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | if (addr > 127) { |
| 109 | sr_err("Invalid address specified: %d.", addr); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /* Looks like a valid USB device specification, but is it connected? */ |
| 114 | devices = NULL; |
| 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))) { |
Peter Stuge | d4928d7 | 2012-12-04 21:11:25 +0100 | [diff] [blame] | 118 | sr_err("Failed to get device descriptor: %s.", |
| 119 | libusb_error_name(ret)); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 123 | if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) |
| 124 | continue; |
| 125 | |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 126 | b = libusb_get_bus_number(devlist[i]); |
| 127 | a = libusb_get_device_address(devlist[i]); |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 128 | if (bus + addr && (b != bus || a != addr)) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 129 | continue; |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 130 | |
| 131 | sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = " |
| 132 | "%d.%d).", des.idVendor, des.idProduct, b, a); |
| 133 | |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 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); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 137 | } |
| 138 | libusb_free_device_list(devlist, 1); |
| 139 | |
| 140 | sr_dbg("Found %d device(s).", g_slist_length(devices)); |
| 141 | |
| 142 | return devices; |
| 143 | } |
| 144 | |
Bert Vermeulen | 9116262 | 2012-12-30 01:44:58 +0100 | [diff] [blame] | 145 | /** |
| 146 | * Find USB devices supporting the USBTMC class |
| 147 | * |
| 148 | * @param usb_ctx libusb context to use while scanning. |
| 149 | * |
| 150 | * @return A GSList of struct sr_usb_dev_inst, with bus and address fields |
| 151 | * indicating devices with USBTMC support. |
| 152 | */ |
| 153 | SR_PRIV GSList *sr_usb_find_usbtmc(libusb_context *usb_ctx) |
| 154 | { |
| 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; |
| 160 | GSList *devices; |
| 161 | int confidx, intfidx, ret, i; |
| 162 | |
| 163 | devices = NULL; |
| 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)); |
| 169 | continue; |
| 170 | } |
| 171 | |
| 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."); |
| 175 | break; |
| 176 | } |
| 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) |
| 182 | continue; |
| 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])); |
| 187 | |
| 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); |
| 191 | } |
Bert Vermeulen | d8e3685 | 2013-01-19 13:20:34 +0100 | [diff] [blame] | 192 | libusb_free_config_descriptor(confdes); |
Bert Vermeulen | 9116262 | 2012-12-30 01:44:58 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | libusb_free_device_list(devlist, 1); |
| 196 | |
| 197 | sr_dbg("Found %d device(s).", g_slist_length(devices)); |
| 198 | |
| 199 | return devices; |
| 200 | } |
| 201 | |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 202 | SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb) |
| 203 | { |
| 204 | struct libusb_device **devlist; |
| 205 | struct libusb_device_descriptor des; |
| 206 | int ret, r, cnt, i, a, b; |
| 207 | |
Bert Vermeulen | c5f1a02 | 2012-12-09 15:19:39 +0100 | [diff] [blame] | 208 | sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 209 | |
| 210 | if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) { |
| 211 | sr_err("Failed to retrieve device list: %s.", |
| 212 | libusb_error_name(cnt)); |
| 213 | return SR_ERR; |
| 214 | } |
| 215 | |
| 216 | ret = SR_ERR; |
| 217 | for (i = 0; i < cnt; i++) { |
| 218 | if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { |
| 219 | sr_err("Failed to get device descriptor: %s.", |
| 220 | libusb_error_name(r)); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | b = libusb_get_bus_number(devlist[i]); |
| 225 | a = libusb_get_device_address(devlist[i]); |
Bert Vermeulen | c5f1a02 | 2012-12-09 15:19:39 +0100 | [diff] [blame] | 226 | if (b != usb->bus || a != usb->address) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 227 | continue; |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 228 | |
| 229 | if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) { |
| 230 | sr_err("Failed to open device: %s.", |
| 231 | libusb_error_name(r)); |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = " |
| 236 | "%d.%d).", des.idVendor, des.idProduct, b, a); |
| 237 | |
| 238 | ret = SR_OK; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | libusb_free_device_list(devlist, 1); |
| 243 | |
| 244 | return ret; |
| 245 | } |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame^] | 246 | |
| 247 | SR_PRIV int usb_source_add(struct sr_context *ctx, int timeout, |
| 248 | sr_receive_data_callback_t cb, void *cb_data) |
| 249 | { |
| 250 | #ifdef _WIN32 |
| 251 | sr_err("Operation not supported on Windows yet."); |
| 252 | return SR_ERR; |
| 253 | #else |
| 254 | const struct libusb_pollfd **lupfd; |
| 255 | unsigned int i; |
| 256 | |
| 257 | lupfd = libusb_get_pollfds(ctx->libusb_ctx); |
| 258 | for (i = 0; lupfd[i]; i++) |
| 259 | sr_source_add(lupfd[i]->fd, lupfd[i]->events, timeout, cb, cb_data); |
| 260 | free(lupfd); |
| 261 | |
| 262 | return SR_OK; |
| 263 | #endif |
| 264 | } |
| 265 | |
| 266 | SR_PRIV int usb_source_remove(struct sr_context *ctx) |
| 267 | { |
| 268 | #ifdef _WIN32 |
| 269 | sr_err("Operation not supported on Windows yet."); |
| 270 | return SR_ERR; |
| 271 | #else |
| 272 | const struct libusb_pollfd **lupfd; |
| 273 | unsigned int i; |
| 274 | |
| 275 | lupfd = libusb_get_pollfds(ctx->libusb_ctx); |
| 276 | for (i = 0; lupfd[i]; i++) |
| 277 | sr_source_remove(lupfd[i]->fd); |
| 278 | free(lupfd); |
| 279 | |
| 280 | return SR_OK; |
| 281 | #endif |
| 282 | } |