am7xxx: fail if USB configuration or interface are not right
authorAntonio Ospite <ospite@studenti.unina.it>
Sat, 23 Mar 2013 21:40:25 +0000 (22:40 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Sat, 23 Mar 2013 22:44:40 +0000 (23:44 +0100)
Check the return values of libusb_set_configuration() and
libusb_claim_interface(), this way it is easier to spot devices which
require a different setting.

src/am7xxx.c

index 83d9a91..c080660 100644 (file)
@@ -598,8 +598,28 @@ static int scan_devices(am7xxx_context *ctx, scan_op op,
                                                goto out;
                                        }
 
                                                goto out;
                                        }
 
-                                       libusb_set_configuration((*dev)->usb_device, 2);
-                                       libusb_claim_interface((*dev)->usb_device, 0);
+                                       /* XXX, the device is now open, if any
+                                        * of the calls below fail we need to
+                                        * close it again before bailing out.
+                                        */
+
+                                       ret = libusb_set_configuration((*dev)->usb_device, 2);
+                                       if (ret < 0) {
+                                               debug(ctx, "libusb_set_configuration failed\n");
+                                               debug(ctx, "Cannot set configuration 2\n");
+                                               goto out_libusb_close;
+                                       }
+
+                                       ret = libusb_claim_interface((*dev)->usb_device, 0);
+                                       if (ret < 0) {
+                                               debug(ctx, "libusb_claim_interface failed\n");
+                                               debug(ctx, "Cannot claim interface 0\n");
+out_libusb_close:
+                                               libusb_close((*dev)->usb_device);
+                                               (*dev)->usb_device = NULL;
+                                               goto out;
+                                       }
+
                                        goto out;
                                }
                                current_index++;
                                        goto out;
                                }
                                current_index++;