2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
29 /* SR_CONF_CONN takes one of these: */
30 #define CONN_USB_VIDPID "^([0-9a-z]{4})\\.([0-9a-z]{4})$"
31 #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
33 #define LOG_PREFIX "usb"
35 #if !HAVE_LIBUSB_OS_HANDLE
36 typedef int libusb_os_handle;
39 /** Custom GLib event source for libusb I/O.
48 /* Needed to keep track of installed sources */
49 struct sr_session *session;
51 struct libusb_context *usb_ctx;
55 /** USB event source prepare() method.
57 static gboolean usb_source_prepare(GSource *source, int *timeout)
59 int64_t now_us, usb_due_us;
60 struct usb_source *usource;
61 struct timeval usb_timeout;
65 usource = (struct usb_source *)source;
67 ret = libusb_get_next_timeout(usource->usb_ctx, &usb_timeout);
68 if (G_UNLIKELY(ret < 0)) {
69 sr_err("Failed to get libusb timeout: %s",
70 libusb_error_name(ret));
72 now_us = g_source_get_time(source);
74 if (usource->due_us == 0) {
75 /* First-time initialization of the expiration time */
76 usource->due_us = now_us + usource->timeout_us;
79 usb_due_us = (int64_t)usb_timeout.tv_sec * G_USEC_PER_SEC
80 + usb_timeout.tv_usec + now_us;
81 if (usb_due_us < usource->due_us)
82 usource->due_us = usb_due_us;
84 if (usource->due_us != INT64_MAX)
85 remaining_ms = (MAX(0, usource->due_us - now_us) + 999) / 1000;
89 *timeout = remaining_ms;
91 return (remaining_ms == 0);
94 /** USB event source check() method.
96 static gboolean usb_source_check(GSource *source)
98 struct usb_source *usource;
100 unsigned int revents;
103 usource = (struct usb_source *)source;
106 for (i = 0; i < usource->pollfds->len; i++) {
107 pollfd = g_ptr_array_index(usource->pollfds, i);
108 revents |= pollfd->revents;
110 return (revents != 0 || (usource->due_us != INT64_MAX
111 && usource->due_us <= g_source_get_time(source)));
114 /** USB event source dispatch() method.
116 static gboolean usb_source_dispatch(GSource *source,
117 GSourceFunc callback, void *user_data)
119 struct usb_source *usource;
121 unsigned int revents;
125 usource = (struct usb_source *)source;
128 * This is somewhat arbitrary, but drivers use revents to distinguish
129 * actual I/O from timeouts. When we remove the user timeout from the
130 * driver API, this will no longer be needed.
132 for (i = 0; i < usource->pollfds->len; i++) {
133 pollfd = g_ptr_array_index(usource->pollfds, i);
134 revents |= pollfd->revents;
137 sr_spew("%s: revents 0x%.2X", __func__, revents);
139 sr_spew("%s: timed out", __func__);
142 sr_err("Callback not set, cannot dispatch event.");
143 return G_SOURCE_REMOVE;
145 keep = (*(sr_receive_data_callback)callback)(-1, revents, user_data);
147 if (G_LIKELY(keep) && G_LIKELY(!g_source_is_destroyed(source))) {
148 if (usource->timeout_us >= 0)
149 usource->due_us = g_source_get_time(source)
150 + usource->timeout_us;
152 usource->due_us = INT64_MAX;
157 /** USB event source finalize() method.
159 static void usb_source_finalize(GSource *source)
161 struct usb_source *usource;
163 usource = (struct usb_source *)source;
165 sr_spew("%s", __func__);
167 libusb_set_pollfd_notifiers(usource->usb_ctx, NULL, NULL, NULL);
169 g_ptr_array_unref(usource->pollfds);
170 usource->pollfds = NULL;
172 sr_session_source_destroyed(usource->session,
173 usource->usb_ctx, source);
176 /** Callback invoked when a new libusb FD should be added to the poll set.
178 static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd,
179 short events, void *user_data)
181 struct usb_source *usource;
186 pollfd = g_slice_new(GPollFD);
190 pollfd->fd = (gintptr)fd;
191 pollfd->events = events;
194 g_ptr_array_add(usource->pollfds, pollfd);
195 g_source_add_poll((GSource *)usource, pollfd);
198 /** Callback invoked when a libusb FD should be removed from the poll set.
200 static LIBUSB_CALL void usb_pollfd_removed(libusb_os_handle fd, void *user_data)
202 struct usb_source *usource;
208 /* It's likely that the removed poll FD is at the end.
210 for (i = usource->pollfds->len; G_LIKELY(i > 0); i--) {
211 pollfd = g_ptr_array_index(usource->pollfds, i - 1);
213 if ((libusb_os_handle)pollfd->fd == fd) {
214 g_source_remove_poll((GSource *)usource, pollfd);
215 g_ptr_array_remove_index_fast(usource->pollfds, i - 1);
219 sr_err("FD to be removed (%" G_GINTPTR_FORMAT
220 ") not found in event source poll set.", (gintptr)fd);
223 /** Destroy notify callback for FDs maintained by the USB event source.
225 static void usb_source_free_pollfd(void *data)
227 g_slice_free(GPollFD, data);
230 /** Create an event source for libusb I/O.
232 * TODO: The combination of the USB I/O source with a user timeout is
233 * conceptually broken. The user timeout supplied here is completely
234 * unrelated to I/O -- the actual I/O timeout is set when submitting
236 * The sigrok drivers generally use the timeout to poll device state.
237 * Usually, this polling can be sensibly done only when there is no
238 * active USB transfer -- i.e. it's actually mutually exclusive with
239 * waiting for transfer completion.
240 * Thus, the user timeout should be removed from the USB event source
241 * API at some point. Instead, drivers should install separate timer
242 * event sources for their polling needs.
244 * @param session The session the event source belongs to.
245 * @param usb_ctx The libusb context for which to handle events.
246 * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely.
247 * @return A new event source object, or NULL on failure.
249 static GSource *usb_source_new(struct sr_session *session,
250 struct libusb_context *usb_ctx, int timeout_ms)
252 static GSourceFuncs usb_source_funcs = {
253 .prepare = &usb_source_prepare,
254 .check = &usb_source_check,
255 .dispatch = &usb_source_dispatch,
256 .finalize = &usb_source_finalize
259 struct usb_source *usource;
260 const struct libusb_pollfd **upollfds, **upfd;
262 upollfds = libusb_get_pollfds(usb_ctx);
264 sr_err("Failed to get libusb file descriptors.");
267 source = g_source_new(&usb_source_funcs, sizeof(struct usb_source));
268 usource = (struct usb_source *)source;
270 g_source_set_name(source, "usb");
272 if (timeout_ms >= 0) {
273 usource->timeout_us = 1000 * (int64_t)timeout_ms;
276 usource->timeout_us = -1;
277 usource->due_us = INT64_MAX;
279 usource->session = session;
280 usource->usb_ctx = usb_ctx;
281 usource->pollfds = g_ptr_array_new_full(8, &usb_source_free_pollfd);
283 for (upfd = upollfds; *upfd != NULL; upfd++)
284 usb_pollfd_added((*upfd)->fd, (*upfd)->events, usource);
286 #if (LIBUSB_API_VERSION >= 0x01000104)
287 libusb_free_pollfds(upollfds);
291 libusb_set_pollfd_notifiers(usb_ctx,
292 &usb_pollfd_added, &usb_pollfd_removed, usource);
298 * Find USB devices according to a connection string.
300 * @param usb_ctx libusb context to use while scanning.
301 * @param conn Connection string specifying the device(s) to match. This
302 * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
304 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
305 * matching the device that matched the connection string. The GSList and
306 * its contents must be freed by the caller.
308 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
310 struct sr_usb_dev_inst *usb;
311 struct libusb_device **devlist;
312 struct libusb_device_descriptor des;
316 int vid, pid, bus, addr, b, a, ret, i;
319 vid = pid = bus = addr = 0;
320 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
321 if (g_regex_match(reg, conn, 0, &match)) {
322 if ((mstr = g_match_info_fetch(match, 1)))
323 vid = strtoul(mstr, NULL, 16);
326 if ((mstr = g_match_info_fetch(match, 2)))
327 pid = strtoul(mstr, NULL, 16);
329 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
332 g_match_info_unref(match);
334 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
335 if (g_regex_match(reg, conn, 0, &match)) {
336 if ((mstr = g_match_info_fetch(match, 1)))
337 bus = strtoul(mstr, NULL, 10);
340 if ((mstr = g_match_info_fetch(match, 2)))
341 addr = strtoul(mstr, NULL, 10);
343 sr_dbg("Trying to find USB device with bus.address = "
344 "%d.%d.", bus, addr);
347 g_match_info_unref(match);
350 if (vid + pid + bus + addr == 0) {
351 sr_err("Neither VID:PID nor bus.address was specified.");
356 sr_err("Invalid bus specified: %d.", bus);
361 sr_err("Invalid address specified: %d.", addr);
365 /* Looks like a valid USB device specification, but is it connected? */
367 libusb_get_device_list(usb_ctx, &devlist);
368 for (i = 0; devlist[i]; i++) {
369 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
370 sr_err("Failed to get device descriptor: %s.",
371 libusb_error_name(ret));
375 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
378 b = libusb_get_bus_number(devlist[i]);
379 a = libusb_get_device_address(devlist[i]);
380 if (bus + addr && (b != bus || a != addr))
383 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
384 "%d.%d).", des.idVendor, des.idProduct, b, a);
386 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
387 libusb_get_device_address(devlist[i]), NULL);
388 devices = g_slist_append(devices, usb);
390 libusb_free_device_list(devlist, 1);
392 sr_dbg("Found %d device(s).", g_slist_length(devices));
397 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
399 struct libusb_device **devlist;
400 struct libusb_device_descriptor des;
401 int ret, r, cnt, i, a, b;
403 sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
405 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
406 sr_err("Failed to retrieve device list: %s.",
407 libusb_error_name(cnt));
412 for (i = 0; i < cnt; i++) {
413 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
414 sr_err("Failed to get device descriptor: %s.",
415 libusb_error_name(r));
419 b = libusb_get_bus_number(devlist[i]);
420 a = libusb_get_device_address(devlist[i]);
421 if (b != usb->bus || a != usb->address)
424 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
425 sr_err("Failed to open device: %s.",
426 libusb_error_name(r));
430 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
431 "%d.%d).", des.idVendor, des.idProduct, b, a);
437 libusb_free_device_list(devlist, 1);
442 SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb)
444 libusb_close(usb->devhdl);
446 sr_dbg("Closed USB device %d.%d.", usb->bus, usb->address);
449 SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
450 int timeout, sr_receive_data_callback cb, void *cb_data)
455 source = usb_source_new(session, ctx->libusb_ctx, timeout);
459 g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL);
461 ret = sr_session_source_add_internal(session, ctx->libusb_ctx, source);
462 g_source_unref(source);
467 SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx)
469 return sr_session_source_remove_internal(session, ctx->libusb_ctx);
472 SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len)
474 uint8_t port_numbers[8];
478 * FreeBSD requires that devices prior to calling libusb_get_port_numbers()
479 * have been opened with libusb_open().
482 struct libusb_device_handle *devh;
483 if (libusb_open(dev, &devh) != 0)
486 n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers));
491 /* Workaround FreeBSD libusb_get_port_numbers() returning 0. */
494 port_numbers[0] = libusb_get_device_address(dev);
501 len = snprintf(path, path_len, "usb/%d-%d",
502 libusb_get_bus_number(dev), port_numbers[0]);
504 for (i = 1; i < n; i++)
505 len += snprintf(path+len, path_len-len, ".%d", port_numbers[i]);