]> sigrok.org Git - libsigrok.git/blobdiff - src/usb.c
Don't free trigger when destroying the session.
[libsigrok.git] / src / usb.c
index da192ec1710f2e6c3514ce58f8f85b45b499d76a..1b22de41acdb84bca86725da2f7fbdae4dbbe4b7 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
 
 #define LOG_PREFIX "usb"
 
+#if !HAVE_LIBUSB_OS_HANDLE
+typedef int libusb_os_handle;
+#endif
+
 /**
  * Find USB devices according to a connection string.
  *
@@ -184,137 +188,86 @@ SR_PRIV void sr_usb_close(struct sr_usb_dev_inst *usb)
        sr_dbg("Closed USB device %d.%d.", usb->bus, usb->address);
 }
 
-#ifdef _WIN32
-/* Thread used to run libusb_wait_for_event() and set a pollable event. */
-static gpointer usb_thread(gpointer data)
+static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd,
+               short events, void *user_data)
 {
-       struct sr_context *ctx = data;
-
-       while (ctx->usb_thread_running) {
-               /* Acquire event waiters lock, needed for libusb_wait_for_event(). */
-               libusb_lock_event_waiters(ctx->libusb_ctx);
-               /* Wait for any libusb event. The main thread can interrupt this wait
-                * by calling libusb_unlock_events(). */
-               libusb_wait_for_event(ctx->libusb_ctx, NULL);
-               /* Release event waiters lock. */
-               libusb_unlock_event_waiters(ctx->libusb_ctx);
-               /* Set event that the main loop will be polling on. */
-               SetEvent(ctx->usb_wait_complete_event);
-               /* Wait for the main thread to signal us to run again. */
-               WaitForSingleObject(ctx->usb_wait_request_event, INFINITE);
-               ResetEvent(ctx->usb_wait_request_event);
-       }
+       struct sr_session *session;
+       gintptr tag;
 
-       return NULL;
+       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);
 }
 
-/* Callback wrapper run when main g_poll() gets a USB event or timeout. */
-static int usb_callback(int fd, int revents, void *cb_data)
+static LIBUSB_CALL void usb_pollfd_removed(libusb_os_handle fd, void *user_data)
 {
-       struct sr_context *ctx = cb_data;
-       int ret;
+       struct sr_session *session;
+       gintptr tag;
 
-       /* Run registered callback to handle libusb events. */
-       ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data);
+       session = user_data;
+       tag = (gintptr)session->ctx->libusb_ctx;
 
-       /* Were we triggered by an event from the wait thread, rather than by a
-        * timeout? */
-       int triggered_by_event = (revents & G_IO_IN);
-
-       /* If so, and if the USB event source has not been removed from the
-        * session, reset the event that woke us and tell the wait thread to start
-        * waiting for events again. */
-       if (triggered_by_event && ctx->usb_thread_running) {
-               ResetEvent(ctx->usb_wait_complete_event);
-               SetEvent(ctx->usb_wait_request_event);
-       }
-
-       return ret;
+       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;
        }
-
-#ifdef _WIN32
-       /* Create events used to signal between main and USB wait threads. */
-       ctx->usb_wait_request_event = CreateEvent(NULL, TRUE, FALSE, NULL);
-       ctx->usb_wait_complete_event = CreateEvent(NULL, TRUE, FALSE, NULL);
-       /* Start USB wait thread. */
-       ctx->usb_thread_running = TRUE;
-       ctx->usb_thread = g_thread_new("usb", usb_thread, ctx);
-       /* Add event, set by USB wait thread, to session poll set. */
-       ctx->usb_pollfd.fd = ctx->usb_wait_complete_event;
-       ctx->usb_pollfd.events = G_IO_IN;
-       ctx->usb_pollfd.revents = 0;
-       ctx->usb_cb = cb;
-       ctx->usb_cb_data = cb_data;
-       ret = sr_session_source_add_pollfd(session, &ctx->usb_pollfd,
-                       timeout, usb_callback, ctx);
-#else
-       const struct libusb_pollfd **lupfd;
-       GPollFD *pollfds;
-       int i;
-       int num_fds = 0;
-
-       lupfd = libusb_get_pollfds(ctx->libusb_ctx);
-       if (!lupfd || !lupfd[0]) {
-               free(lupfd);
+       pollfds = libusb_get_pollfds(ctx->libusb_ctx);
+       if (!pollfds) {
                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;
+       tag = (gintptr)ctx->libusb_ctx;
+       ret = sr_session_source_add_internal(session,
+                       timeout, cb, cb_data, tag);
+
+       ctx->usb_source_present = (ret == SR_OK);
+
+       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);
        }
-       free(lupfd);
-       ret = sr_session_source_add_internal(session, pollfds, num_fds,
-                       timeout, cb, cb_data, (gintptr)ctx->libusb_ctx);
-       g_free(pollfds);
+#if (LIBUSB_API_VERSION >= 0x01000104)
+       libusb_free_pollfds(pollfds);
+#else
+       free(pollfds);
 #endif
-       ctx->usb_source_present = (ret == SR_OK);
+       if (ret != SR_OK)
+               return ret;
 
-       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)
 {
-       int ret;
-
-       if (!ctx->usb_source_present)
-               return SR_OK;
-
-#ifdef _WIN32
-       /* Signal the USB wait thread to stop, then unblock it so it does. */
-       ctx->usb_thread_running = FALSE;
-       SetEvent(ctx->usb_wait_request_event);
-       libusb_unlock_events(ctx->libusb_ctx);
-       /* Wait for USB wait thread to terminate. */
-       g_thread_join(ctx->usb_thread);
-       /* Remove USB event from session poll set. */
-       ret = sr_session_source_remove_pollfd(session, &ctx->usb_pollfd);
-       /* Close event handles that were used between threads. */
-       CloseHandle(ctx->usb_wait_request_event);
-       CloseHandle(ctx->usb_wait_complete_event);
-#else
-       ret = sr_session_source_remove_internal(session,
-                       (gintptr)ctx->libusb_ctx);
-#endif
        ctx->usb_source_present = FALSE;
 
-       return ret;
+       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)