]> sigrok.org Git - libsigrok.git/blobdiff - src/usb.c
USB: Handle the case of a callback removing its event source
[libsigrok.git] / src / usb.c
index ec502c97501704f38bed9b2d535374166ff7601e..fd8bb7a341fd527e6f96c08711b6c37492508a50 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -23,7 +23,7 @@
 #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: */
@@ -177,98 +177,140 @@ SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
        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);
 }
 
+#ifdef G_OS_WIN32
+/*
+ * USB callback wrapper run when the main loop is idle.
+ */
 static int usb_callback(int fd, int revents, void *cb_data)
 {
-       struct sr_context *ctx = cb_data;
+       int64_t start_time, stop_time, due, timeout;
+       struct timeval tv;
+       struct sr_context *ctx;
        int ret;
 
-       g_mutex_lock(&ctx->usb_mutex);
-       ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data);
+       (void)fd;
+       (void)revents;
+       ctx = cb_data;
+
+       start_time = g_get_monotonic_time();
+       due = ctx->usb_due;
+
+       if (due > start_time) {
+               timeout = due - start_time;
+               tv.tv_sec  = timeout / G_USEC_PER_SEC;
+               tv.tv_usec = timeout % G_USEC_PER_SEC;
+
+               sr_spew("libusb_handle_events enter: %g ms timeout",
+                       1e-3 * timeout);
+
+               ret = libusb_handle_events_timeout_completed(ctx->libusb_ctx,
+                               (ctx->usb_timeout < 0) ? NULL : &tv, NULL);
+               if (ret != 0) {
+                       /* Warn but still invoke the callback, to give
+                        * the driver a chance to deal with the problem.
+                        */
+                       sr_warn("Error handling libusb event (%s)",
+                               libusb_error_name(ret));
+               }
+               stop_time = g_get_monotonic_time();
+
+               sr_spew("libusb_handle_events leave: %g ms elapsed",
+                       1e-3 * (stop_time - start_time));
+               /*
+                * The event source may have been removed by the driver's
+                * libusb transfer callback. Skip the callback in that case.
+                */
+               if (!ctx->usb_source_present)
+                       return TRUE;
+       } else {
+               /* Timeout already expired on entry.
+                */
+               stop_time = start_time;
 
-       if (ctx->usb_thread_running) {
-               ResetEvent(ctx->usb_event);
-               g_mutex_unlock(&ctx->usb_mutex);
+               sr_spew("libusb_handle_events skipped");
        }
 
-       return ret;
+       if (ctx->usb_timeout >= 0)
+               ctx->usb_due = stop_time + ctx->usb_timeout;
+       /*
+        * Run the registered callback to execute any follow-up activity
+        * to libusb's event handling.
+        */
+       return ctx->usb_cb(-1, (stop_time < due) ? G_IO_IN : 0,
+                       ctx->usb_cb_data);
 }
 #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)
 {
+       int ret;
+
        if (ctx->usb_source_present) {
                sr_err("A USB event source is already present.");
                return SR_ERR;
        }
 
-#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;
+#ifdef G_OS_WIN32
+       if (timeout >= 0) {
+               ctx->usb_timeout = INT64_C(1000) * timeout;
+               ctx->usb_due = g_get_monotonic_time() + ctx->usb_timeout;
+       } else {
+               ctx->usb_timeout = -1;
+               ctx->usb_due = INT64_MAX;
+       }
        ctx->usb_cb = cb;
        ctx->usb_cb_data = cb_data;
-       sr_session_source_add_pollfd(session, &ctx->usb_pollfd, timeout,
-                       usb_callback, ctx);
+       /*
+        * TODO: Install an idle source which will fire permanently, and block
+        * in a wrapper callback until any libusb events have been processed.
+        * This will have to do for now, until we implement a proper way to
+        * deal with libusb events on Windows.
+        */
+       ret = sr_session_source_add_internal(session, NULL, 0,
+                       0, &usb_callback, ctx, (gintptr)ctx->libusb_ctx);
 #else
        const struct libusb_pollfd **lupfd;
-       unsigned int i;
+       GPollFD *pollfds;
+       int i;
+       int num_fds = 0;
 
        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);
+       if (!lupfd || !lupfd[0]) {
+               free(lupfd);
+               sr_err("Failed to get libusb file descriptors.");
+               return SR_ERR;
+       }
+       while (lupfd[num_fds])
+               ++num_fds;
+       pollfds = g_new(GPollFD, num_fds);
+
+       for (i = 0; i < num_fds; ++i) {
+               pollfds[i].fd = lupfd[i]->fd;
+               pollfds[i].events = lupfd[i]->events;
+               pollfds[i].revents = 0;
+       }
        free(lupfd);
+       ret = sr_session_source_add_internal(session, pollfds, num_fds,
+                       timeout, cb, cb_data, (gintptr)ctx->libusb_ctx);
+       g_free(pollfds);
 #endif
-       ctx->usb_source_present = TRUE;
+       ctx->usb_source_present = (ret == SR_OK);
 
-       return SR_OK;
+       return ret;
 }
 
 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;
-
-       lupfd = libusb_get_pollfds(ctx->libusb_ctx);
-       for (i = 0; lupfd[i]; i++)
-               sr_session_source_remove(session, lupfd[i]->fd);
-       free(lupfd);
-#endif
-       ctx->usb_source_present = FALSE;
-
-       return SR_OK;
+       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)
@@ -276,7 +318,29 @@ SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len)
        uint8_t port_numbers[8];
        int i, n, len;
 
+/*
+ * 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
        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]);