debian/changelog: improve the description of one patch
[libam7xxx.git] / examples / am7xxx-modeswitch.c
index 8e528c6..793130e 100644 (file)
 
 static unsigned char switch_command[] =
        "\x55\x53\x42\x43\x08\x70\x52\x89\x00\x00\x00\x00\x00\x00"
 
 static unsigned char switch_command[] =
        "\x55\x53\x42\x43\x08\x70\x52\x89\x00\x00\x00\x00\x00\x00"
-       "\x0c\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
+       "\x10\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
 
 int main(void)
 {
        int ret;
 
 int main(void)
 {
        int ret;
-       int transferred;
-       libusb_device_handle *usb_device = NULL;
-
+       libusb_device_handle *usb_device;
+       int current_configuration;
        unsigned int len;
        unsigned int len;
+       int transferred;
 
        ret = libusb_init(NULL);
        if (ret < 0) {
 
        ret = libusb_init(NULL);
        if (ret < 0) {
@@ -57,27 +57,57 @@ int main(void)
                goto out;
        }
 
                goto out;
        }
 
-       ret = libusb_set_configuration(usb_device, AM7XXX_STORAGE_CONFIGURATION);
+       current_configuration = -1;
+       ret = libusb_get_configuration(usb_device, &current_configuration);
        if (ret < 0) {
        if (ret < 0) {
-               fprintf(stderr, "libusb_set_configuration failed: %s\n",
+               fprintf(stderr, "libusb_get_configuration failed: %s\n",
                        libusb_error_name(ret));
                        libusb_error_name(ret));
-               fprintf(stderr, "Cannot set configuration %hhu\n",
-                       AM7XXX_STORAGE_CONFIGURATION);
                goto out_libusb_close;
        }
 
                goto out_libusb_close;
        }
 
+       if (current_configuration != AM7XXX_STORAGE_CONFIGURATION) {
+               ret = libusb_set_configuration(usb_device,
+                                              AM7XXX_STORAGE_CONFIGURATION);
+               if (ret < 0) {
+                       fprintf(stderr, "libusb_set_configuration failed: %s\n",
+                               libusb_error_name(ret));
+                       fprintf(stderr, "Cannot set configuration %d\n",
+                               AM7XXX_STORAGE_CONFIGURATION);
+                       goto out_libusb_close;
+               }
+       }
+
        libusb_set_auto_detach_kernel_driver(usb_device, 1);
 
        ret = libusb_claim_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
        if (ret < 0) {
                fprintf(stderr, "libusb_claim_interface failed: %s\n",
                        libusb_error_name(ret));
        libusb_set_auto_detach_kernel_driver(usb_device, 1);
 
        ret = libusb_claim_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
        if (ret < 0) {
                fprintf(stderr, "libusb_claim_interface failed: %s\n",
                        libusb_error_name(ret));
-               fprintf(stderr, "Cannot claim interface %hhu\n",
+               fprintf(stderr, "Cannot claim interface %d\n",
                        AM7XXX_STORAGE_INTERFACE);
                goto out_libusb_close;
        }
 
                        AM7XXX_STORAGE_INTERFACE);
                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(usb_device, &current_configuration);
+       if (ret < 0) {
+               fprintf(stderr, "libusb_get_configuration after claim failed: %s\n",
+                       libusb_error_name(ret));
+               goto out_libusb_release_interface;
+       }
+
+       if (current_configuration != AM7XXX_STORAGE_CONFIGURATION) {
+               fprintf(stderr, "libusb configuration changed (expected: %d, current: %d\n",
+                       AM7XXX_STORAGE_CONFIGURATION, current_configuration);
+               ret = -EINVAL;
+               goto out_libusb_release_interface;
+       }
+
        len = sizeof(switch_command);
        len = sizeof(switch_command);
+
        transferred = 0;
        ret = libusb_bulk_transfer(usb_device, AM7XXX_STORAGE_OUT_EP,
                                   switch_command, len, &transferred, 0);
        transferred = 0;
        ret = libusb_bulk_transfer(usb_device, AM7XXX_STORAGE_OUT_EP,
                                   switch_command, len, &transferred, 0);