]> sigrok.org Git - libsigrok.git/blobdiff - src/usb.c
Introduce standard cleanup helper
[libsigrok.git] / src / usb.c
index d66f188085c94638efea1283b4dd900d4fb9a130..35a3e80ed58cc7d340317b1aeea116ba4d1f2d83 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -19,6 +19,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdlib.h>
 #include <memory.h>
 #include <glib.h>
@@ -27,7 +28,7 @@
 #include "libsigrok-internal.h"
 
 /* SR_CONF_CONN takes one of these: */
-#define CONN_USB_VIDPID  "^([0-9a-z]{4})\\.([0-9a-z]{4})$"
+#define CONN_USB_VIDPID  "^([0-9a-fA-F]{4})\\.([0-9a-fA-F]{4})$"
 #define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
 
 #define LOG_PREFIX "usb"
@@ -133,10 +134,6 @@ static gboolean usb_source_dispatch(GSource *source,
                pollfd = g_ptr_array_index(usource->pollfds, i);
                revents |= pollfd->revents;
        }
-       if (revents != 0)
-               sr_spew("%s: revents 0x%.2X", __func__, revents);
-       else
-               sr_spew("%s: timed out", __func__);
 
        if (!callback) {
                sr_err("Callback not set, cannot dispatch event.");
@@ -183,6 +180,9 @@ static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd,
 
        usource = user_data;
 
+       if (G_UNLIKELY(g_source_is_destroyed(&usource->base)))
+               return;
+
        pollfd = g_slice_new(GPollFD);
 #ifdef G_OS_WIN32
        events = G_IO_IN;
@@ -192,7 +192,7 @@ static LIBUSB_CALL void usb_pollfd_added(libusb_os_handle fd,
        pollfd->revents = 0;
 
        g_ptr_array_add(usource->pollfds, pollfd);
-       g_source_add_poll((GSource *)usource, pollfd);
+       g_source_add_poll(&usource->base, pollfd);
 }
 
 /** Callback invoked when a libusb FD should be removed from the poll set.
@@ -205,13 +205,16 @@ static LIBUSB_CALL void usb_pollfd_removed(libusb_os_handle fd, void *user_data)
 
        usource = user_data;
 
+       if (G_UNLIKELY(g_source_is_destroyed(&usource->base)))
+               return;
+
        /* It's likely that the removed poll FD is at the end.
         */
        for (i = usource->pollfds->len; G_LIKELY(i > 0); i--) {
                pollfd = g_ptr_array_index(usource->pollfds, i - 1);
 
                if ((libusb_os_handle)pollfd->fd == fd) {
-                       g_source_remove_poll((GSource *)usource, pollfd);
+                       g_source_remove_poll(&usource->base, pollfd);
                        g_ptr_array_remove_index_fast(usource->pollfds, i - 1);
                        return;
                }
@@ -352,7 +355,7 @@ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
                return NULL;
        }
 
-       if (bus > 64) {
+       if (bus > 255) {
                sr_err("Invalid bus specified: %d.", bus);
                return NULL;
        }
@@ -477,19 +480,20 @@ SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len)
 /*
  * FreeBSD requires that devices prior to calling libusb_get_port_numbers()
  * have been opened with libusb_open().
+ * This apparently also applies to some Mac OS X versions.
  */
-#ifdef __FreeBSD__
+#if defined(__FreeBSD__) || defined(__APPLE__)
        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__
+#if defined(__FreeBSD__) || defined(__APPLE__)
        libusb_close(devh);
 #endif
 
-/* Workaround FreeBSD libusb_get_port_numbers() returning 0. */
-#ifdef __FreeBSD__
+/* Workaround FreeBSD / Mac OS X libusb_get_port_numbers() returning 0. */
+#if defined(__FreeBSD__) || defined(__APPLE__)
        if (n == 0) {
                port_numbers[0] = libusb_get_device_address(dev);
                n = 1;