blob: 8d4224d1e35545579e5ab5c7c6b2c5b94f99defb [file] [log] [blame]
Uwe Hermann0c632d32012-11-02 21:04:21 +01001/*
Uwe Hermann50985c22013-04-23 22:24:30 +02002 * This file is part of the libsigrok project.
Uwe Hermann0c632d32012-11-02 21:04:21 +01003 *
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 Vermeulen19535642013-01-21 23:22:47 +010028/* SR_CONF_CONN takes one of these: */
Bert Vermeulen1eb0a0d2013-04-15 23:47:04 +020029#define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$"
Uwe Hermann0c632d32012-11-02 21:04:21 +010030#define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
31
Bert Vermeulen91162622012-12-30 01:44:58 +010032/* Some USBTMC-specific enums, as defined in the USBTMC standard. */
33#define SUBCLASS_USBTMC 0x03
34#define USBTMC_USB488 0x01
35
Martin Ling3544f842013-12-23 03:38:35 +000036#define LOG_PREFIX "usb"
Uwe Hermann0c632d32012-11-02 21:04:21 +010037
Bert Vermeulen7ae6a752012-12-04 23:25:11 +010038/**
39 * Find USB devices according to a connection string.
40 *
41 * @param usb_ctx libusb context to use while scanning.
42 * @param conn Connection string specifying the device(s) to match. This
43 * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
44 *
45 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
46 * matching the device that matched the connection string. The GSList and
47 * its contents must be freed by the caller.
48 */
49SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
Uwe Hermann0c632d32012-11-02 21:04:21 +010050{
Bert Vermeulen7ae6a752012-12-04 23:25:11 +010051 struct sr_usb_dev_inst *usb;
Uwe Hermann0c632d32012-11-02 21:04:21 +010052 struct libusb_device **devlist;
53 struct libusb_device_descriptor des;
54 GSList *devices;
55 GRegex *reg;
56 GMatchInfo *match;
57 int vid, pid, bus, addr, b, a, ret, i;
58 char *mstr;
59
60 vid = pid = bus = addr = 0;
61 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
62 if (g_regex_match(reg, conn, 0, &match)) {
Bert Vermeulene7f378f2012-12-09 14:39:17 +010063 if ((mstr = g_match_info_fetch(match, 1)))
Uwe Hermann0c632d32012-11-02 21:04:21 +010064 vid = strtoul(mstr, NULL, 16);
Uwe Hermann0c632d32012-11-02 21:04:21 +010065 g_free(mstr);
66
Bert Vermeulene7f378f2012-12-09 14:39:17 +010067 if ((mstr = g_match_info_fetch(match, 2)))
Uwe Hermann0c632d32012-11-02 21:04:21 +010068 pid = strtoul(mstr, NULL, 16);
Uwe Hermann0c632d32012-11-02 21:04:21 +010069 g_free(mstr);
70 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
71 vid, pid);
72 } else {
73 g_match_info_unref(match);
74 g_regex_unref(reg);
75 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
76 if (g_regex_match(reg, conn, 0, &match)) {
Bert Vermeulene7f378f2012-12-09 14:39:17 +010077 if ((mstr = g_match_info_fetch(match, 1)))
Bert Vermeulen1eb0a0d2013-04-15 23:47:04 +020078 bus = strtoul(mstr, NULL, 10);
Uwe Hermann0c632d32012-11-02 21:04:21 +010079 g_free(mstr);
80
Bert Vermeulene7f378f2012-12-09 14:39:17 +010081 if ((mstr = g_match_info_fetch(match, 2)))
Bert Vermeulen1eb0a0d2013-04-15 23:47:04 +020082 addr = strtoul(mstr, NULL, 10);
Uwe Hermann0c632d32012-11-02 21:04:21 +010083 g_free(mstr);
84 sr_dbg("Trying to find USB device with bus.address = "
85 "%d.%d.", bus, addr);
86 }
87 }
88 g_match_info_unref(match);
89 g_regex_unref(reg);
90
91 if (vid + pid + bus + addr == 0) {
Bert Vermeulene7f378f2012-12-09 14:39:17 +010092 sr_err("Neither VID:PID nor bus.address was specified.");
Uwe Hermann0c632d32012-11-02 21:04:21 +010093 return NULL;
94 }
95
96 if (bus > 64) {
97 sr_err("Invalid bus specified: %d.", bus);
98 return NULL;
99 }
100
101 if (addr > 127) {
102 sr_err("Invalid address specified: %d.", addr);
103 return NULL;
104 }
105
106 /* Looks like a valid USB device specification, but is it connected? */
107 devices = NULL;
108 libusb_get_device_list(usb_ctx, &devlist);
109 for (i = 0; devlist[i]; i++) {
110 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
Peter Stuged4928d72012-12-04 21:11:25 +0100111 sr_err("Failed to get device descriptor: %s.",
112 libusb_error_name(ret));
Uwe Hermann0c632d32012-11-02 21:04:21 +0100113 continue;
114 }
115
Bert Vermeulen7ae6a752012-12-04 23:25:11 +0100116 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
117 continue;
118
Uwe Hermann0c632d32012-11-02 21:04:21 +0100119 b = libusb_get_bus_number(devlist[i]);
120 a = libusb_get_device_address(devlist[i]);
Bert Vermeulen7ae6a752012-12-04 23:25:11 +0100121 if (bus + addr && (b != bus || a != addr))
Uwe Hermann0c632d32012-11-02 21:04:21 +0100122 continue;
Uwe Hermann0c632d32012-11-02 21:04:21 +0100123
124 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
125 "%d.%d).", des.idVendor, des.idProduct, b, a);
126
Bert Vermeulen7ae6a752012-12-04 23:25:11 +0100127 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
128 libusb_get_device_address(devlist[i]), NULL);
129 devices = g_slist_append(devices, usb);
Uwe Hermann0c632d32012-11-02 21:04:21 +0100130 }
131 libusb_free_device_list(devlist, 1);
132
133 sr_dbg("Found %d device(s).", g_slist_length(devices));
134
135 return devices;
136}
137
Bert Vermeulen91162622012-12-30 01:44:58 +0100138/**
139 * Find USB devices supporting the USBTMC class
140 *
141 * @param usb_ctx libusb context to use while scanning.
142 *
143 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
144 * indicating devices with USBTMC support.
145 */
146SR_PRIV GSList *sr_usb_find_usbtmc(libusb_context *usb_ctx)
147{
148 struct sr_usb_dev_inst *usb;
149 struct libusb_device **devlist;
150 struct libusb_device_descriptor des;
151 struct libusb_config_descriptor *confdes;
152 const struct libusb_interface_descriptor *intfdes;
153 GSList *devices;
154 int confidx, intfidx, ret, i;
155
156 devices = NULL;
157 libusb_get_device_list(usb_ctx, &devlist);
158 for (i = 0; devlist[i]; i++) {
159 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
160 sr_err("Failed to get device descriptor: %s.",
161 libusb_error_name(ret));
162 continue;
163 }
164
165 for (confidx = 0; confidx < des.bNumConfigurations; confidx++) {
166 if (libusb_get_config_descriptor(devlist[i], confidx, &confdes) != 0) {
167 sr_err("Failed to get configuration descriptor.");
168 break;
169 }
170 for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) {
171 intfdes = confdes->interface[intfidx].altsetting;
172 if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION
173 || intfdes->bInterfaceSubClass != SUBCLASS_USBTMC
174 || intfdes->bInterfaceProtocol != USBTMC_USB488)
175 continue;
176 sr_dbg("Found USBTMC device (VID:PID = %04x:%04x, bus.address = "
177 "%d.%d).", des.idVendor, des.idProduct,
178 libusb_get_bus_number(devlist[i]),
179 libusb_get_device_address(devlist[i]));
180
181 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
182 libusb_get_device_address(devlist[i]), NULL);
183 devices = g_slist_append(devices, usb);
184 }
Bert Vermeulend8e36852013-01-19 13:20:34 +0100185 libusb_free_config_descriptor(confdes);
Bert Vermeulen91162622012-12-30 01:44:58 +0100186 }
187 }
188 libusb_free_device_list(devlist, 1);
189
190 sr_dbg("Found %d device(s).", g_slist_length(devices));
191
192 return devices;
193}
194
Uwe Hermann0c632d32012-11-02 21:04:21 +0100195SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
196{
197 struct libusb_device **devlist;
198 struct libusb_device_descriptor des;
199 int ret, r, cnt, i, a, b;
200
Bert Vermeulenc5f1a022012-12-09 15:19:39 +0100201 sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
Uwe Hermann0c632d32012-11-02 21:04:21 +0100202
203 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
204 sr_err("Failed to retrieve device list: %s.",
205 libusb_error_name(cnt));
206 return SR_ERR;
207 }
208
209 ret = SR_ERR;
210 for (i = 0; i < cnt; i++) {
211 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
212 sr_err("Failed to get device descriptor: %s.",
213 libusb_error_name(r));
214 continue;
215 }
216
217 b = libusb_get_bus_number(devlist[i]);
218 a = libusb_get_device_address(devlist[i]);
Bert Vermeulenc5f1a022012-12-09 15:19:39 +0100219 if (b != usb->bus || a != usb->address)
Uwe Hermann0c632d32012-11-02 21:04:21 +0100220 continue;
Uwe Hermann0c632d32012-11-02 21:04:21 +0100221
222 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
223 sr_err("Failed to open device: %s.",
224 libusb_error_name(r));
225 break;
226 }
227
228 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
229 "%d.%d).", des.idVendor, des.idProduct, b, a);
230
231 ret = SR_OK;
232 break;
233 }
234
235 libusb_free_device_list(devlist, 1);
236
237 return ret;
238}
Martin Ling6c60fac2013-12-21 23:03:24 +0000239
Martin Ling5321ac62013-12-22 07:16:56 +0000240#ifdef _WIN32
Uwe Hermanndcc94342014-01-17 00:26:30 +0100241static gpointer usb_thread(gpointer data)
Martin Ling5321ac62013-12-22 07:16:56 +0000242{
243 struct sr_context *ctx = data;
244
245 while (ctx->usb_thread_running) {
246 g_mutex_lock(&ctx->usb_mutex);
247 libusb_wait_for_event(ctx->libusb_ctx, NULL);
248 SetEvent(ctx->usb_event);
249 g_mutex_unlock(&ctx->usb_mutex);
250 g_thread_yield();
251 }
252
253 return NULL;
254}
255
Uwe Hermanndcc94342014-01-17 00:26:30 +0100256static int usb_callback(int fd, int revents, void *cb_data)
Martin Ling5321ac62013-12-22 07:16:56 +0000257{
258 struct sr_context *ctx = cb_data;
259 int ret;
260
261 g_mutex_lock(&ctx->usb_mutex);
262 ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data);
Martin Ling34ea7f92013-12-22 17:38:24 +0000263
264 if (ctx->usb_thread_running) {
265 ResetEvent(ctx->usb_event);
266 g_mutex_unlock(&ctx->usb_mutex);
267 }
Martin Ling5321ac62013-12-22 07:16:56 +0000268
269 return ret;
270}
271#endif
272
Martin Ling6c60fac2013-12-21 23:03:24 +0000273SR_PRIV int usb_source_add(struct sr_context *ctx, int timeout,
274 sr_receive_data_callback_t cb, void *cb_data)
275{
Martin Ling66403242013-12-22 17:27:13 +0000276 if (ctx->usb_source_present) {
277 sr_err("A USB event source is already present.");
278 return SR_ERR;
279 }
280
Martin Ling6c60fac2013-12-21 23:03:24 +0000281#ifdef _WIN32
Martin Ling5321ac62013-12-22 07:16:56 +0000282 ctx->usb_event = CreateEvent(NULL, TRUE, FALSE, NULL);
283 g_mutex_init(&ctx->usb_mutex);
284 ctx->usb_thread_running = TRUE;
285 ctx->usb_thread = g_thread_new("usb", usb_thread, ctx);
286 ctx->usb_pollfd.fd = ctx->usb_event;
287 ctx->usb_pollfd.events = G_IO_IN;
288 ctx->usb_cb = cb;
289 ctx->usb_cb_data = cb_data;
290 sr_session_source_add_pollfd(&ctx->usb_pollfd, timeout, usb_callback, ctx);
Martin Ling6c60fac2013-12-21 23:03:24 +0000291#else
292 const struct libusb_pollfd **lupfd;
293 unsigned int i;
294
295 lupfd = libusb_get_pollfds(ctx->libusb_ctx);
296 for (i = 0; lupfd[i]; i++)
297 sr_source_add(lupfd[i]->fd, lupfd[i]->events, timeout, cb, cb_data);
298 free(lupfd);
Martin Ling5321ac62013-12-22 07:16:56 +0000299#endif
Martin Ling66403242013-12-22 17:27:13 +0000300 ctx->usb_source_present = TRUE;
Martin Ling6c60fac2013-12-21 23:03:24 +0000301
302 return SR_OK;
Martin Ling6c60fac2013-12-21 23:03:24 +0000303}
304
305SR_PRIV int usb_source_remove(struct sr_context *ctx)
306{
Martin Ling66403242013-12-22 17:27:13 +0000307 if (!ctx->usb_source_present)
308 return SR_OK;
309
Martin Ling6c60fac2013-12-21 23:03:24 +0000310#ifdef _WIN32
Martin Ling5321ac62013-12-22 07:16:56 +0000311 ctx->usb_thread_running = FALSE;
Martin Lingb5328e12013-12-22 17:10:57 +0000312 g_mutex_unlock(&ctx->usb_mutex);
Martin Ling5321ac62013-12-22 07:16:56 +0000313 libusb_unlock_events(ctx->libusb_ctx);
314 g_thread_join(ctx->usb_thread);
315 g_mutex_clear(&ctx->usb_mutex);
316 sr_session_source_remove_pollfd(&ctx->usb_pollfd);
317 CloseHandle(ctx->usb_event);
Martin Ling6c60fac2013-12-21 23:03:24 +0000318#else
319 const struct libusb_pollfd **lupfd;
320 unsigned int i;
321
322 lupfd = libusb_get_pollfds(ctx->libusb_ctx);
323 for (i = 0; lupfd[i]; i++)
324 sr_source_remove(lupfd[i]->fd);
325 free(lupfd);
Martin Ling5321ac62013-12-22 07:16:56 +0000326#endif
Martin Ling66403242013-12-22 17:27:13 +0000327 ctx->usb_source_present = FALSE;
Martin Ling6c60fac2013-12-21 23:03:24 +0000328
329 return SR_OK;
Martin Ling6c60fac2013-12-21 23:03:24 +0000330}