blob: c4d78f90a236acd036c4ad7e60a1ea902f225fa1 [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
Uwe Hermann29a27192013-05-03 21:59:32 +020036/* 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 Hermann0c632d32012-11-02 21:04:21 +010044
Bert Vermeulen7ae6a752012-12-04 23:25:11 +010045/**
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 */
56SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
Uwe Hermann0c632d32012-11-02 21:04:21 +010057{
Bert Vermeulen7ae6a752012-12-04 23:25:11 +010058 struct sr_usb_dev_inst *usb;
Uwe Hermann0c632d32012-11-02 21:04:21 +010059 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 Vermeulene7f378f2012-12-09 14:39:17 +010070 if ((mstr = g_match_info_fetch(match, 1)))
Uwe Hermann0c632d32012-11-02 21:04:21 +010071 vid = strtoul(mstr, NULL, 16);
Uwe Hermann0c632d32012-11-02 21:04:21 +010072 g_free(mstr);
73
Bert Vermeulene7f378f2012-12-09 14:39:17 +010074 if ((mstr = g_match_info_fetch(match, 2)))
Uwe Hermann0c632d32012-11-02 21:04:21 +010075 pid = strtoul(mstr, NULL, 16);
Uwe Hermann0c632d32012-11-02 21:04:21 +010076 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 Vermeulene7f378f2012-12-09 14:39:17 +010084 if ((mstr = g_match_info_fetch(match, 1)))
Bert Vermeulen1eb0a0d2013-04-15 23:47:04 +020085 bus = strtoul(mstr, NULL, 10);
Uwe Hermann0c632d32012-11-02 21:04:21 +010086 g_free(mstr);
87
Bert Vermeulene7f378f2012-12-09 14:39:17 +010088 if ((mstr = g_match_info_fetch(match, 2)))
Bert Vermeulen1eb0a0d2013-04-15 23:47:04 +020089 addr = strtoul(mstr, NULL, 10);
Uwe Hermann0c632d32012-11-02 21:04:21 +010090 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 Vermeulene7f378f2012-12-09 14:39:17 +010099 sr_err("Neither VID:PID nor bus.address was specified.");
Uwe Hermann0c632d32012-11-02 21:04:21 +0100100 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 Stuged4928d72012-12-04 21:11:25 +0100118 sr_err("Failed to get device descriptor: %s.",
119 libusb_error_name(ret));
Uwe Hermann0c632d32012-11-02 21:04:21 +0100120 continue;
121 }
122
Bert Vermeulen7ae6a752012-12-04 23:25:11 +0100123 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
124 continue;
125
Uwe Hermann0c632d32012-11-02 21:04:21 +0100126 b = libusb_get_bus_number(devlist[i]);
127 a = libusb_get_device_address(devlist[i]);
Bert Vermeulen7ae6a752012-12-04 23:25:11 +0100128 if (bus + addr && (b != bus || a != addr))
Uwe Hermann0c632d32012-11-02 21:04:21 +0100129 continue;
Uwe Hermann0c632d32012-11-02 21:04:21 +0100130
131 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
132 "%d.%d).", des.idVendor, des.idProduct, b, a);
133
Bert Vermeulen7ae6a752012-12-04 23:25:11 +0100134 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 Hermann0c632d32012-11-02 21:04:21 +0100137 }
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 Vermeulen91162622012-12-30 01:44:58 +0100145/**
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 */
153SR_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 Vermeulend8e36852013-01-19 13:20:34 +0100192 libusb_free_config_descriptor(confdes);
Bert Vermeulen91162622012-12-30 01:44:58 +0100193 }
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 Hermann0c632d32012-11-02 21:04:21 +0100202SR_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 Vermeulenc5f1a022012-12-09 15:19:39 +0100208 sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
Uwe Hermann0c632d32012-11-02 21:04:21 +0100209
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 Vermeulenc5f1a022012-12-09 15:19:39 +0100226 if (b != usb->bus || a != usb->address)
Uwe Hermann0c632d32012-11-02 21:04:21 +0100227 continue;
Uwe Hermann0c632d32012-11-02 21:04:21 +0100228
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 Ling6c60fac2013-12-21 23:03:24 +0000246
Martin Ling5321ac62013-12-22 07:16:56 +0000247#ifdef _WIN32
248SR_PRIV gpointer usb_thread(gpointer data)
249{
250 struct sr_context *ctx = data;
251
252 while (ctx->usb_thread_running) {
253 g_mutex_lock(&ctx->usb_mutex);
254 libusb_wait_for_event(ctx->libusb_ctx, NULL);
255 SetEvent(ctx->usb_event);
256 g_mutex_unlock(&ctx->usb_mutex);
257 g_thread_yield();
258 }
259
260 return NULL;
261}
262
263SR_PRIV int usb_callback(int fd, int revents, void *cb_data)
264{
265 struct sr_context *ctx = cb_data;
266 int ret;
267
268 g_mutex_lock(&ctx->usb_mutex);
269 ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data);
270 ResetEvent(ctx->usb_event);
271 g_mutex_unlock(&ctx->usb_mutex);
272
273 return ret;
274}
275#endif
276
Martin Ling6c60fac2013-12-21 23:03:24 +0000277SR_PRIV int usb_source_add(struct sr_context *ctx, int timeout,
278 sr_receive_data_callback_t cb, void *cb_data)
279{
Martin Ling66403242013-12-22 17:27:13 +0000280 if (ctx->usb_source_present) {
281 sr_err("A USB event source is already present.");
282 return SR_ERR;
283 }
284
Martin Ling6c60fac2013-12-21 23:03:24 +0000285#ifdef _WIN32
Martin Ling5321ac62013-12-22 07:16:56 +0000286 ctx->usb_event = CreateEvent(NULL, TRUE, FALSE, NULL);
287 g_mutex_init(&ctx->usb_mutex);
288 ctx->usb_thread_running = TRUE;
289 ctx->usb_thread = g_thread_new("usb", usb_thread, ctx);
290 ctx->usb_pollfd.fd = ctx->usb_event;
291 ctx->usb_pollfd.events = G_IO_IN;
292 ctx->usb_cb = cb;
293 ctx->usb_cb_data = cb_data;
294 sr_session_source_add_pollfd(&ctx->usb_pollfd, timeout, usb_callback, ctx);
Martin Ling6c60fac2013-12-21 23:03:24 +0000295#else
296 const struct libusb_pollfd **lupfd;
297 unsigned int i;
298
299 lupfd = libusb_get_pollfds(ctx->libusb_ctx);
300 for (i = 0; lupfd[i]; i++)
301 sr_source_add(lupfd[i]->fd, lupfd[i]->events, timeout, cb, cb_data);
302 free(lupfd);
Martin Ling5321ac62013-12-22 07:16:56 +0000303#endif
Martin Ling66403242013-12-22 17:27:13 +0000304 ctx->usb_source_present = TRUE;
Martin Ling6c60fac2013-12-21 23:03:24 +0000305
306 return SR_OK;
Martin Ling6c60fac2013-12-21 23:03:24 +0000307}
308
309SR_PRIV int usb_source_remove(struct sr_context *ctx)
310{
Martin Ling66403242013-12-22 17:27:13 +0000311 if (!ctx->usb_source_present)
312 return SR_OK;
313
Martin Ling6c60fac2013-12-21 23:03:24 +0000314#ifdef _WIN32
Martin Ling5321ac62013-12-22 07:16:56 +0000315 ctx->usb_thread_running = FALSE;
Martin Lingb5328e12013-12-22 17:10:57 +0000316 g_mutex_unlock(&ctx->usb_mutex);
Martin Ling5321ac62013-12-22 07:16:56 +0000317 libusb_unlock_events(ctx->libusb_ctx);
318 g_thread_join(ctx->usb_thread);
319 g_mutex_clear(&ctx->usb_mutex);
320 sr_session_source_remove_pollfd(&ctx->usb_pollfd);
321 CloseHandle(ctx->usb_event);
Martin Ling6c60fac2013-12-21 23:03:24 +0000322#else
323 const struct libusb_pollfd **lupfd;
324 unsigned int i;
325
326 lupfd = libusb_get_pollfds(ctx->libusb_ctx);
327 for (i = 0; lupfd[i]; i++)
328 sr_source_remove(lupfd[i]->fd);
329 free(lupfd);
Martin Ling5321ac62013-12-22 07:16:56 +0000330#endif
Martin Ling66403242013-12-22 17:27:13 +0000331 ctx->usb_source_present = FALSE;
Martin Ling6c60fac2013-12-21 23:03:24 +0000332
333 return SR_OK;
Martin Ling6c60fac2013-12-21 23:03:24 +0000334}