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