am7xxx: make sure am7xxx_get_device_info() always returns sensible values
[libam7xxx.git] / src / am7xxx.c
index fedf11a..512cf6f 100644 (file)
@@ -34,7 +34,7 @@
  * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
  */
 #ifndef __GNUC__
-#  define  __attribute__(x)  /*NOTHING*/
+#  define  __attribute__(x)  /* NOTHING */
 #endif
 
 /* Control shared library symbols visibility */
@@ -53,7 +53,7 @@
 
 static void log_message(am7xxx_context *ctx,
                        int level,
-                       const char *function,
+                       const char *function_name,
                        int line,
                        const char *fmt,
                        ...) __attribute__ ((format (printf, 5, 6)));
@@ -341,7 +341,7 @@ static inline unsigned int in_80chars(unsigned int i)
 {
        /* The 3 below is the length of "xx " where xx is the hex string
         * representation of a byte */
-       return ((i+1) % (80/3));
+       return ((i + 1) % (80 / 3));
 }
 
 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
@@ -381,12 +381,13 @@ static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
 {
        int ret;
-       int transferred = 0;
+       int transferred;
 
+       transferred = 0;
        ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
        if (ret != 0 || (unsigned int)transferred != len) {
-               error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
-                     ret, transferred, len);
+               error(dev->ctx, "%s. Transferred: %d (expected %u)\n",
+                     libusb_error_name(ret), transferred, len);
                return ret;
        }
 
@@ -398,14 +399,15 @@ static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
 {
        int ret;
-       int transferred = 0;
+       int transferred;
 
        trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
 
+       transferred = 0;
        ret = libusb_bulk_transfer(dev->usb_device, 0x1, buffer, len, &transferred, 0);
        if (ret != 0 || (unsigned int)transferred != len) {
-               error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
-                     ret, transferred, len);
+               error(dev->ctx, "%s. Transferred: %d (expected %u)\n",
+                     libusb_error_name(ret), transferred, len);
                return ret;
        }
 
@@ -489,8 +491,8 @@ static int send_data_async(am7xxx_device *dev, uint8_t *buffer, unsigned int len
 
        /* Make a copy of the buffer so the caller can safely reuse it just
         * after libusb_submit_transfer() has returned. This technique
-        * requires more allocations than a proper double-buffering approach
-        * but it takes a lot less code. */
+        * requires more dynamic allocations compared to a proper
+        * double-buffering approach but it takes a lot less code. */
        transfer_buffer = malloc(len);
        if (transfer_buffer == NULL) {
                error(dev->ctx, "cannot allocate transfer buffer (%s)\n",
@@ -626,7 +628,7 @@ static int send_command(am7xxx_device *dev, am7xxx_packet_type type)
  * set up */
 static void log_message(am7xxx_context *ctx,
                        int level,
-                       const char *function,
+                       const char *function_name,
                        int line,
                        const char *fmt,
                        ...)
@@ -634,8 +636,8 @@ static void log_message(am7xxx_context *ctx,
        va_list ap;
 
        if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
-               if (function) {
-                       fprintf(stderr, "%s", function);
+               if (function_name) {
+                       fprintf(stderr, "%s", function_name);
                        if (line)
                                fprintf(stderr, "[%d]", line);
                        fprintf(stderr, ": ");
@@ -662,7 +664,7 @@ static am7xxx_device *add_new_device(am7xxx_context *ctx,
 
        new_device = malloc(sizeof(*new_device));
        if (new_device == NULL) {
-               fatal("cannot allocate a new device (%s)\n", strerror(errno));
+               debug(ctx, "cannot allocate a new device (%s)\n", strerror(errno));
                return NULL;
        }
        memset(new_device, 0, sizeof(*new_device));
@@ -704,10 +706,11 @@ static am7xxx_device *find_device(am7xxx_context *ctx,
 
 static int open_device(am7xxx_context *ctx,
                       unsigned int device_index,
-                      libusb_deviceusb_dev,
+                      libusb_device *usb_dev,
                       am7xxx_device **dev)
 {
        int ret;
+       int current_configuration;
 
        *dev = find_device(ctx, device_index);
        if (*dev == NULL) {
@@ -717,14 +720,13 @@ static int open_device(am7xxx_context *ctx,
 
        /* the usb device has already been opened */
        if ((*dev)->usb_device) {
-               debug(ctx, "(*dev)->usb_device already set\n");
                ret = 1;
                goto out;
        }
 
        ret = libusb_open(usb_dev, &((*dev)->usb_device));
        if (ret < 0) {
-               debug(ctx, "libusb_open failed\n");
+               debug(ctx, "libusb_open failed: %s\n", libusb_error_name(ret));
                goto out;
        }
 
@@ -732,26 +734,83 @@ static int open_device(am7xxx_context *ctx,
         * to close it again before bailing out.
         */
 
-       ret = libusb_set_configuration((*dev)->usb_device,
-                                      (*dev)->desc->configuration);
+       current_configuration = -1;
+       ret = libusb_get_configuration((*dev)->usb_device,
+                                      &current_configuration);
        if (ret < 0) {
-               debug(ctx, "libusb_set_configuration failed\n");
-               debug(ctx, "Cannot set configuration %hhu\n",
-                     (*dev)->desc->configuration);
+               debug(ctx, "libusb_get_configuration failed: %s\n",
+                     libusb_error_name(ret));
                goto out_libusb_close;
        }
 
+       if (current_configuration != (*dev)->desc->configuration) {
+               /*
+                * In principle, before setting a new configuration, kernel
+                * drivers should be detached from _all_ interfaces; for
+                * example calling something like the following "invented"
+                * function _before_ setting the new configuration:
+                *
+                *   libusb_detach_all_kernel_drivers((*dev)->usb_device);
+                *
+                * However, in practice, this is not necessary for most
+                * devices as they have only one configuration.
+                *
+                * When a device only has one configuration:
+                *
+                *   - if there was a kernel driver bound to the device, it
+                *     had already set the configuration and the call below
+                *     will be skipped;
+                *
+                *   - if no kernel driver was bound to the device, the call
+                *     below will suceed.
+                */
+               ret = libusb_set_configuration((*dev)->usb_device,
+                                              (*dev)->desc->configuration);
+               if (ret < 0) {
+                       debug(ctx, "libusb_set_configuration failed: %s\n",
+                             libusb_error_name(ret));
+                       debug(ctx, "Cannot set configuration %hhu\n",
+                             (*dev)->desc->configuration);
+                       goto out_libusb_close;
+               }
+       }
+
+       libusb_set_auto_detach_kernel_driver((*dev)->usb_device, 1);
+
        ret = libusb_claim_interface((*dev)->usb_device,
                                     (*dev)->desc->interface_number);
        if (ret < 0) {
-               debug(ctx, "libusb_claim_interface failed\n");
+               debug(ctx, "libusb_claim_interface failed: %s\n",
+                     libusb_error_name(ret));
                debug(ctx, "Cannot claim interface %hhu\n",
                      (*dev)->desc->interface_number);
                goto out_libusb_close;
        }
+
+       /* Checking that the configuration has not changed, as suggested in
+        * http://libusb.sourceforge.net/api-1.0/caveats.html
+        */
+       current_configuration = -1;
+       ret = libusb_get_configuration((*dev)->usb_device,
+                                      &current_configuration);
+       if (ret < 0) {
+               debug(ctx, "libusb_get_configuration after claim failed: %s\n",
+                     libusb_error_name(ret));
+               goto out_libusb_release_interface;
+       }
+
+       if (current_configuration != (*dev)->desc->configuration) {
+               debug(ctx, "libusb configuration changed (expected: %hhu, current: %d)\n",
+                     (*dev)->desc->configuration, current_configuration);
+               ret = -EINVAL;
+               goto out_libusb_release_interface;
+       }
 out:
        return ret;
 
+out_libusb_release_interface:
+       libusb_release_interface((*dev)->usb_device,
+                                (*dev)->desc->interface_number);
 out_libusb_close:
        libusb_close((*dev)->usb_device);
        (*dev)->usb_device = NULL;
@@ -767,13 +826,14 @@ typedef enum {
  * This is where the central logic of multi-device support is.
  *
  * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
- * 'dev' are ignored; the function returns 0 on success and a negative value
+ * 'dev' are ignored; the function returns 0 on success or a negative value
  * on error.
  *
  * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
  * device with index 'open_device_index' and returns the correspondent
- * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
- * 1 if the device was already open and a negative value on error.
+ * am7xxx_device in the 'dev' parameter; the function returns the value from
+ * open_device(), which is 0 on success, 1 if the device was already open or
+ * a negative value on error.
  *
  * NOTES:
  * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
@@ -784,7 +844,7 @@ static int scan_devices(am7xxx_context *ctx, scan_op op,
                        unsigned int open_device_index, am7xxx_device **dev)
 {
        ssize_t num_devices;
-       libusb_device** list;
+       libusb_device **list;
        unsigned int current_index;
        int i;
        int ret;
@@ -1033,8 +1093,7 @@ AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
        *ctx = malloc(sizeof(**ctx));
        if (*ctx == NULL) {
                fatal("cannot allocate the context (%s)\n", strerror(errno));
-               ret = -ENOMEM;
-               goto out;
+               return -ENOMEM;
        }
        memset(*ctx, 0, sizeof(**ctx));
 
@@ -1042,8 +1101,10 @@ AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
        (*ctx)->log_level = AM7XXX_LOG_TRACE;
 
        ret = libusb_init(&((*ctx)->usb_context));
-       if (ret < 0)
+       if (ret < 0) {
+               error(*ctx, "libusb_init failed: %s\n", libusb_error_name(ret));
                goto out_free_context;
+       }
 
        libusb_set_debug((*ctx)->usb_context, LIBUSB_LOG_LEVEL_INFO);
 
@@ -1152,10 +1213,9 @@ AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
        int ret;
        struct am7xxx_header h;
 
-       if (dev->device_info) {
-               memcpy(device_info, dev->device_info, sizeof(*device_info));
-               return 0;
-       }
+       /* if there is a cached copy of the device info, just return that */
+       if (dev->device_info)
+               goto return_value;
 
        ret = send_command(dev, AM7XXX_PACKET_TYPE_DEVINFO);
        if (ret < 0)
@@ -1189,6 +1249,9 @@ AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
        dev->device_info->unknown1 = h.header_data.devinfo.unknown1;
 #endif
 
+return_value:
+       if (device_info)
+               memcpy(device_info, dev->device_info, sizeof(*device_info));
        return 0;
 }
 
@@ -1199,7 +1262,6 @@ AM7XXX_PUBLIC int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
                                        unsigned int *scaled_width,
                                        unsigned int *scaled_height)
 {
-
        am7xxx_device_info device_info;
        float width_ratio;
        float height_ratio;