X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fusb.c;h=60d828ad3d325b012d1607359688b81c376a355f;hb=3a1a6d6b04d7937f025adb557c9051fb21dffa92;hp=9c34b38ad1bdb204d89c5a16f2631c11fb7617f0;hpb=19643b96e27cdeb84123fc91158b25a0d1a6368d;p=libsigrok.git diff --git a/src/usb.c b/src/usb.c index 9c34b38a..60d828ad 100644 --- a/src/usb.c +++ b/src/usb.c @@ -23,7 +23,7 @@ #include #include #include -#include "libsigrok.h" +#include #include "libsigrok-internal.h" /* SR_CONF_CONN takes one of these: */ @@ -177,98 +177,60 @@ 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; -} - -static int usb_callback(int fd, int revents, void *cb_data) -{ - struct sr_context *ctx = cb_data; - int ret; - - g_mutex_lock(&ctx->usb_mutex); - ret = ctx->usb_cb(fd, revents, ctx->usb_cb_data); - - if (ctx->usb_thread_running) { - ResetEvent(ctx->usb_event); - g_mutex_unlock(&ctx->usb_mutex); - } - - return ret; + libusb_close(usb->devhdl); + usb->devhdl = NULL; + sr_dbg("Closed USB device %d.%d.", usb->bus, usb->address); } -#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 **lupfd; + GPollFD *pollfds; + int i; + int num_fds = 0; + 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; - ctx->usb_cb = cb; - ctx->usb_cb_data = cb_data; - sr_session_source_add_pollfd(session, &ctx->usb_pollfd, timeout, - usb_callback, ctx); -#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); + 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) { +#if defined(G_OS_WIN32) && (GLIB_SIZEOF_VOID_P == 4) + /* Avoid a warning on 32-bit Windows. */ + pollfds[i].fd = (gintptr)lupfd[i]->fd; +#else + pollfds[i].fd = lupfd[i]->fd; #endif - ctx->usb_source_present = TRUE; + 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); - return SR_OK; + ctx->usb_source_present = (ret == 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)