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> |
Soeren Apel | db156e5 | 2014-09-22 23:08:17 +0200 | [diff] [blame^] | 23 | #include <memory.h> |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 24 | #include <glib.h> |
| 25 | #include <libusb.h> |
| 26 | #include "libsigrok.h" |
| 27 | #include "libsigrok-internal.h" |
| 28 | |
Bert Vermeulen | 1953564 | 2013-01-21 23:22:47 +0100 | [diff] [blame] | 29 | /* SR_CONF_CONN takes one of these: */ |
Bert Vermeulen | 1eb0a0d | 2013-04-15 23:47:04 +0200 | [diff] [blame] | 30 | #define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$" |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 31 | #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$" |
| 32 | |
Martin Ling | 3544f84 | 2013-12-23 03:38:35 +0000 | [diff] [blame] | 33 | #define LOG_PREFIX "usb" |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 34 | |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 35 | /** |
| 36 | * Find USB devices according to a connection string. |
| 37 | * |
| 38 | * @param usb_ctx libusb context to use while scanning. |
| 39 | * @param conn Connection string specifying the device(s) to match. This |
| 40 | * can be of the form "<bus>.<address>", or "<vendorid>.<productid>". |
| 41 | * |
| 42 | * @return A GSList of struct sr_usb_dev_inst, with bus and address fields |
| 43 | * matching the device that matched the connection string. The GSList and |
| 44 | * its contents must be freed by the caller. |
| 45 | */ |
| 46 | 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] | 47 | { |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 48 | struct sr_usb_dev_inst *usb; |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 49 | struct libusb_device **devlist; |
| 50 | struct libusb_device_descriptor des; |
| 51 | GSList *devices; |
| 52 | GRegex *reg; |
| 53 | GMatchInfo *match; |
| 54 | int vid, pid, bus, addr, b, a, ret, i; |
| 55 | char *mstr; |
| 56 | |
| 57 | vid = pid = bus = addr = 0; |
| 58 | reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL); |
| 59 | if (g_regex_match(reg, conn, 0, &match)) { |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 60 | if ((mstr = g_match_info_fetch(match, 1))) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 61 | vid = strtoul(mstr, NULL, 16); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 62 | g_free(mstr); |
| 63 | |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 64 | if ((mstr = g_match_info_fetch(match, 2))) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 65 | pid = strtoul(mstr, NULL, 16); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 66 | g_free(mstr); |
| 67 | sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.", |
| 68 | vid, pid); |
| 69 | } else { |
| 70 | g_match_info_unref(match); |
| 71 | g_regex_unref(reg); |
| 72 | reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL); |
| 73 | if (g_regex_match(reg, conn, 0, &match)) { |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 74 | if ((mstr = g_match_info_fetch(match, 1))) |
Bert Vermeulen | 1eb0a0d | 2013-04-15 23:47:04 +0200 | [diff] [blame] | 75 | bus = strtoul(mstr, NULL, 10); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 76 | g_free(mstr); |
| 77 | |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 78 | if ((mstr = g_match_info_fetch(match, 2))) |
Bert Vermeulen | 1eb0a0d | 2013-04-15 23:47:04 +0200 | [diff] [blame] | 79 | addr = strtoul(mstr, NULL, 10); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 80 | g_free(mstr); |
| 81 | sr_dbg("Trying to find USB device with bus.address = " |
| 82 | "%d.%d.", bus, addr); |
| 83 | } |
| 84 | } |
| 85 | g_match_info_unref(match); |
| 86 | g_regex_unref(reg); |
| 87 | |
| 88 | if (vid + pid + bus + addr == 0) { |
Bert Vermeulen | e7f378f | 2012-12-09 14:39:17 +0100 | [diff] [blame] | 89 | sr_err("Neither VID:PID nor bus.address was specified."); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | if (bus > 64) { |
| 94 | sr_err("Invalid bus specified: %d.", bus); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (addr > 127) { |
| 99 | sr_err("Invalid address specified: %d.", addr); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | /* Looks like a valid USB device specification, but is it connected? */ |
| 104 | devices = NULL; |
| 105 | libusb_get_device_list(usb_ctx, &devlist); |
| 106 | for (i = 0; devlist[i]; i++) { |
| 107 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { |
Peter Stuge | d4928d7 | 2012-12-04 21:11:25 +0100 | [diff] [blame] | 108 | sr_err("Failed to get device descriptor: %s.", |
| 109 | libusb_error_name(ret)); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 110 | continue; |
| 111 | } |
| 112 | |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 113 | if (vid + pid && (des.idVendor != vid || des.idProduct != pid)) |
| 114 | continue; |
| 115 | |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 116 | b = libusb_get_bus_number(devlist[i]); |
| 117 | a = libusb_get_device_address(devlist[i]); |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 118 | if (bus + addr && (b != bus || a != addr)) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 119 | continue; |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 120 | |
| 121 | sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = " |
| 122 | "%d.%d).", des.idVendor, des.idProduct, b, a); |
| 123 | |
Bert Vermeulen | 7ae6a75 | 2012-12-04 23:25:11 +0100 | [diff] [blame] | 124 | usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), |
| 125 | libusb_get_device_address(devlist[i]), NULL); |
| 126 | devices = g_slist_append(devices, usb); |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 127 | } |
| 128 | libusb_free_device_list(devlist, 1); |
| 129 | |
| 130 | sr_dbg("Found %d device(s).", g_slist_length(devices)); |
| 131 | |
| 132 | return devices; |
| 133 | } |
| 134 | |
| 135 | SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb) |
| 136 | { |
| 137 | struct libusb_device **devlist; |
| 138 | struct libusb_device_descriptor des; |
| 139 | int ret, r, cnt, i, a, b; |
| 140 | |
Bert Vermeulen | c5f1a02 | 2012-12-09 15:19:39 +0100 | [diff] [blame] | 141 | 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] | 142 | |
| 143 | if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) { |
| 144 | sr_err("Failed to retrieve device list: %s.", |
| 145 | libusb_error_name(cnt)); |
| 146 | return SR_ERR; |
| 147 | } |
| 148 | |
| 149 | ret = SR_ERR; |
| 150 | for (i = 0; i < cnt; i++) { |
| 151 | if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { |
| 152 | sr_err("Failed to get device descriptor: %s.", |
| 153 | libusb_error_name(r)); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | b = libusb_get_bus_number(devlist[i]); |
| 158 | a = libusb_get_device_address(devlist[i]); |
Bert Vermeulen | c5f1a02 | 2012-12-09 15:19:39 +0100 | [diff] [blame] | 159 | if (b != usb->bus || a != usb->address) |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 160 | continue; |
Uwe Hermann | 0c632d3 | 2012-11-02 21:04:21 +0100 | [diff] [blame] | 161 | |
| 162 | if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) { |
| 163 | sr_err("Failed to open device: %s.", |
| 164 | libusb_error_name(r)); |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = " |
| 169 | "%d.%d).", des.idVendor, des.idProduct, b, a); |
| 170 | |
| 171 | ret = SR_OK; |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | libusb_free_device_list(devlist, 1); |
| 176 | |
| 177 | return ret; |
| 178 | } |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 179 | |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 180 | #ifdef _WIN32 |
Uwe Hermann | dcc9434 | 2014-01-17 00:26:30 +0100 | [diff] [blame] | 181 | static gpointer usb_thread(gpointer data) |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 182 | { |
| 183 | struct sr_context *ctx = data; |
| 184 | |
| 185 | while (ctx->usb_thread_running) { |
| 186 | g_mutex_lock(&ctx->usb_mutex); |
| 187 | libusb_wait_for_event(ctx->libusb_ctx, NULL); |
| 188 | SetEvent(ctx->usb_event); |
| 189 | g_mutex_unlock(&ctx->usb_mutex); |
| 190 | g_thread_yield(); |
| 191 | } |
| 192 | |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Uwe Hermann | dcc9434 | 2014-01-17 00:26:30 +0100 | [diff] [blame] | 196 | static int usb_callback(int fd, int revents, void *cb_data) |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 197 | { |
| 198 | struct sr_context *ctx = cb_data; |
| 199 | int ret; |
| 200 | |
| 201 | g_mutex_lock(&ctx->usb_mutex); |
| 202 | ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data); |
Martin Ling | 34ea7f9 | 2013-12-22 17:38:24 +0000 | [diff] [blame] | 203 | |
| 204 | if (ctx->usb_thread_running) { |
| 205 | ResetEvent(ctx->usb_event); |
| 206 | g_mutex_unlock(&ctx->usb_mutex); |
| 207 | } |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | #endif |
| 212 | |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 213 | SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx, |
| 214 | int timeout, sr_receive_data_callback cb, void *cb_data) |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 215 | { |
Martin Ling | 6640324 | 2013-12-22 17:27:13 +0000 | [diff] [blame] | 216 | if (ctx->usb_source_present) { |
| 217 | sr_err("A USB event source is already present."); |
| 218 | return SR_ERR; |
| 219 | } |
| 220 | |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 221 | #ifdef _WIN32 |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 222 | ctx->usb_event = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 223 | g_mutex_init(&ctx->usb_mutex); |
| 224 | ctx->usb_thread_running = TRUE; |
| 225 | ctx->usb_thread = g_thread_new("usb", usb_thread, ctx); |
| 226 | ctx->usb_pollfd.fd = ctx->usb_event; |
| 227 | ctx->usb_pollfd.events = G_IO_IN; |
| 228 | ctx->usb_cb = cb; |
| 229 | ctx->usb_cb_data = cb_data; |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 230 | sr_session_source_add_pollfd(session, &ctx->usb_pollfd, timeout, |
| 231 | usb_callback, ctx); |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 232 | #else |
| 233 | const struct libusb_pollfd **lupfd; |
| 234 | unsigned int i; |
| 235 | |
| 236 | lupfd = libusb_get_pollfds(ctx->libusb_ctx); |
| 237 | for (i = 0; lupfd[i]; i++) |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 238 | sr_session_source_add(session, lupfd[i]->fd, lupfd[i]->events, |
| 239 | timeout, cb, cb_data); |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 240 | free(lupfd); |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 241 | #endif |
Martin Ling | 6640324 | 2013-12-22 17:27:13 +0000 | [diff] [blame] | 242 | ctx->usb_source_present = TRUE; |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 243 | |
| 244 | return SR_OK; |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 247 | SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx) |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 248 | { |
Martin Ling | 6640324 | 2013-12-22 17:27:13 +0000 | [diff] [blame] | 249 | if (!ctx->usb_source_present) |
| 250 | return SR_OK; |
| 251 | |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 252 | #ifdef _WIN32 |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 253 | ctx->usb_thread_running = FALSE; |
Martin Ling | b5328e1 | 2013-12-22 17:10:57 +0000 | [diff] [blame] | 254 | g_mutex_unlock(&ctx->usb_mutex); |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 255 | libusb_unlock_events(ctx->libusb_ctx); |
| 256 | g_thread_join(ctx->usb_thread); |
| 257 | g_mutex_clear(&ctx->usb_mutex); |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 258 | sr_session_source_remove_pollfd(session, &ctx->usb_pollfd); |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 259 | CloseHandle(ctx->usb_event); |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 260 | #else |
| 261 | const struct libusb_pollfd **lupfd; |
| 262 | unsigned int i; |
| 263 | |
| 264 | lupfd = libusb_get_pollfds(ctx->libusb_ctx); |
| 265 | for (i = 0; lupfd[i]; i++) |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 266 | sr_session_source_remove(session, lupfd[i]->fd); |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 267 | free(lupfd); |
Martin Ling | 5321ac6 | 2013-12-22 07:16:56 +0000 | [diff] [blame] | 268 | #endif |
Martin Ling | 6640324 | 2013-12-22 17:27:13 +0000 | [diff] [blame] | 269 | ctx->usb_source_present = FALSE; |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 270 | |
| 271 | return SR_OK; |
Martin Ling | 6c60fac | 2013-12-21 23:03:24 +0000 | [diff] [blame] | 272 | } |
Soeren Apel | db156e5 | 2014-09-22 23:08:17 +0200 | [diff] [blame^] | 273 | |
| 274 | SR_PRIV int usb_get_port_path(libusb_device *dev, const char *path, int path_len) |
| 275 | { |
| 276 | gchar s[64]; |
| 277 | gchar tmp_s[8]; |
| 278 | uint8_t port_numbers[32]; |
| 279 | int i; |
| 280 | |
| 281 | memset(port_numbers, 0, sizeof(port_numbers)); |
| 282 | libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); |
| 283 | |
| 284 | g_snprintf((gchar*)&s, sizeof(s), "usb/%d-%d", |
| 285 | libusb_get_bus_number(dev), port_numbers[0]); |
| 286 | |
| 287 | i = 1; |
| 288 | while (port_numbers[i]) { |
| 289 | g_snprintf(tmp_s, sizeof(tmp_s), ".%d", port_numbers[i]); |
| 290 | g_strlcat((gchar*)&s, tmp_s, sizeof(s)); |
| 291 | i++; |
| 292 | } |
| 293 | |
| 294 | g_strlcpy((gchar*)path, s, path_len); |
| 295 | |
| 296 | return SR_OK; |
| 297 | } |