*/
#include <stdlib.h>
+#include <memory.h>
#include <glib.h>
#include <libusb.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
/* SR_CONF_CONN takes one of these: */
#define LOG_PREFIX "usb"
+#if !HAVE_LIBUSB_OS_HANDLE
+typedef int libusb_os_handle;
+#endif
+
/**
* Find USB devices according to a connection string.
*
return ret;
}
-#ifdef _WIN32
-static gpointer usb_thread(gpointer data)
+SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb)
{
- struct sr_context *ctx = data;
-
- while (ctx->usb_thread_running) {
- g_mutex_lock(&ctx->usb_mutex);
- libusb_wait_for_event(ctx->libusb_ctx, NULL);
- SetEvent(ctx->usb_event);
- g_mutex_unlock(&ctx->usb_mutex);
- g_thread_yield();
- }
-
- return NULL;
+ libusb_close(usb->devhdl);
+ usb->devhdl = NULL;
+ sr_dbg("Closed USB device %d.%d.", usb->bus, usb->address);
}
-static int usb_callback(int fd, int revents, void *cb_data)
+static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd,
+ short events, void *user_data)
{
- struct sr_context *ctx = cb_data;
- int ret;
+ struct sr_session *session;
+ gintptr tag;
- g_mutex_lock(&ctx->usb_mutex);
- ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data);
+ session = user_data;
+ tag = (gintptr)session->ctx->libusb_ctx;
+#ifdef G_OS_WIN32
+ events = G_IO_IN;
+#endif
+ sr_session_source_poll_add(session, tag, (gintptr)fd, events);
+}
- if (ctx->usb_thread_running) {
- ResetEvent(ctx->usb_event);
- g_mutex_unlock(&ctx->usb_mutex);
- }
+static LIBUSB_CALL void usb_pollfd_removed(libusb_os_handle fd, void *user_data)
+{
+ struct sr_session *session;
+ gintptr tag;
- return ret;
+ session = user_data;
+ tag = (gintptr)session->ctx->libusb_ctx;
+
+ sr_session_source_poll_remove(session, tag, (gintptr)fd);
}
-#endif
SR_PRIV int usb_source_add(struct sr_session *session, struct sr_context *ctx,
int timeout, sr_receive_data_callback cb, void *cb_data)
{
+ const struct libusb_pollfd **pollfds;
+ gintptr tag;
+ int i;
+ int ret;
+ int events;
+
if (ctx->usb_source_present) {
sr_err("A USB event source is already present.");
return SR_ERR;
}
+ pollfds = libusb_get_pollfds(ctx->libusb_ctx);
+ if (!pollfds) {
+ sr_err("Failed to get libusb file descriptors.");
+ return SR_ERR;
+ }
+ tag = (gintptr)ctx->libusb_ctx;
+ ret = sr_session_source_add_internal(session,
+ timeout, cb, cb_data, tag);
+
+ ctx->usb_source_present = (ret == SR_OK);
-#ifdef _WIN32
- ctx->usb_event = CreateEvent(NULL, TRUE, FALSE, NULL);
- g_mutex_init(&ctx->usb_mutex);
- ctx->usb_thread_running = TRUE;
- ctx->usb_thread = g_thread_new("usb", usb_thread, ctx);
- ctx->usb_pollfd.fd = ctx->usb_event;
- ctx->usb_pollfd.events = G_IO_IN;
- ctx->usb_cb = cb;
- ctx->usb_cb_data = cb_data;
- sr_session_source_add_pollfd(session, &ctx->usb_pollfd, timeout,
- usb_callback, ctx);
+ for (i = 0; ret == SR_OK && pollfds[i]; ++i) {
+#ifdef G_OS_WIN32
+ events = G_IO_IN;
+#else
+ events = pollfds[i]->events;
+#endif
+ ret = sr_session_source_poll_add(session, tag,
+ (gintptr)pollfds[i]->fd, events);
+ }
+#if (LIBUSB_API_VERSION >= 0x01000104)
+ libusb_free_pollfds(pollfds);
#else
- const struct libusb_pollfd **lupfd;
- unsigned int i;
-
- lupfd = libusb_get_pollfds(ctx->libusb_ctx);
- for (i = 0; lupfd[i]; i++)
- sr_session_source_add(session, lupfd[i]->fd, lupfd[i]->events,
- timeout, cb, cb_data);
- free(lupfd);
+ free(pollfds);
#endif
- ctx->usb_source_present = TRUE;
+ if (ret != SR_OK)
+ return ret;
+
+ libusb_set_pollfd_notifiers(ctx->libusb_ctx,
+ &usb_pollfd_added, &usb_pollfd_removed, session);
return SR_OK;
}
SR_PRIV int usb_source_remove(struct sr_session *session, struct sr_context *ctx)
{
- if (!ctx->usb_source_present)
- return SR_OK;
-
-#ifdef _WIN32
- ctx->usb_thread_running = FALSE;
- g_mutex_unlock(&ctx->usb_mutex);
- libusb_unlock_events(ctx->libusb_ctx);
- g_thread_join(ctx->usb_thread);
- g_mutex_clear(&ctx->usb_mutex);
- sr_session_source_remove_pollfd(session, &ctx->usb_pollfd);
- CloseHandle(ctx->usb_event);
-#else
- const struct libusb_pollfd **lupfd;
- unsigned int i;
+ ctx->usb_source_present = FALSE;
+
+ libusb_set_pollfd_notifiers(ctx->libusb_ctx, NULL, NULL, NULL);
+
+ return sr_session_source_remove_internal(session,
+ (gintptr)ctx->libusb_ctx);
+}
+
+SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len)
+{
+ uint8_t port_numbers[8];
+ int i, n, len;
- lupfd = libusb_get_pollfds(ctx->libusb_ctx);
- for (i = 0; lupfd[i]; i++)
- sr_session_source_remove(session, lupfd[i]->fd);
- free(lupfd);
+/*
+ * FreeBSD requires that devices prior to calling libusb_get_port_numbers()
+ * have been opened with libusb_open().
+ */
+#ifdef __FreeBSD__
+ struct libusb_device_handle *devh;
+ if (libusb_open(dev, &devh) != 0)
+ return SR_ERR;
#endif
- ctx->usb_source_present = FALSE;
+ n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers));
+#ifdef __FreeBSD__
+ libusb_close(devh);
+#endif
+
+/* Workaround FreeBSD libusb_get_port_numbers() returning 0. */
+#ifdef __FreeBSD__
+ if (n == 0) {
+ port_numbers[0] = libusb_get_device_address(dev);
+ n = 1;
+ }
+#endif
+ if (n < 1)
+ return SR_ERR;
+
+ len = snprintf(path, path_len, "usb/%d-%d",
+ libusb_get_bus_number(dev), port_numbers[0]);
+
+ for (i = 1; i < n; i++)
+ len += snprintf(path+len, path_len-len, ".%d", port_numbers[i]);
return SR_OK;
}