am7xxx-modeswitch: improve messages relative to libusb failures
[libam7xxx.git] / examples / am7xxx-modeswitch.c
index a66c37d..8e528c6 100644 (file)
@@ -4,7 +4,7 @@
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -17,6 +17,7 @@
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <errno.h>
 #include <libusb.h>
 
 #include <errno.h>
 #include <libusb.h>
 
@@ -39,38 +40,40 @@ int main(void)
        unsigned int len;
 
        ret = libusb_init(NULL);
        unsigned int len;
 
        ret = libusb_init(NULL);
-       if (ret < 0)
+       if (ret < 0) {
+               fprintf(stderr, "libusb_init failed: %s\n",
+                       libusb_error_name(ret));
                goto out;
                goto out;
+       }
 
 
-       libusb_set_debug(NULL, 3);
+       libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
 
        usb_device = libusb_open_device_with_vid_pid(NULL,
                                                     AM7XXX_STORAGE_VID,
                                                     AM7XXX_STORAGE_PID);
        if (usb_device == NULL) {
 
        usb_device = libusb_open_device_with_vid_pid(NULL,
                                                     AM7XXX_STORAGE_VID,
                                                     AM7XXX_STORAGE_PID);
        if (usb_device == NULL) {
-               fprintf(stderr, "cannot open the device: %d.\n", errno);
+               fprintf(stderr, "libusb_open failed: %s\n", strerror(errno));
                ret = -errno;
                goto out;
        }
 
                ret = -errno;
                goto out;
        }
 
-       if (libusb_kernel_driver_active(usb_device, AM7XXX_STORAGE_INTERFACE)) {
-               ret = libusb_detach_kernel_driver(usb_device,
-                                                 AM7XXX_STORAGE_INTERFACE);
-               if (ret < 0)
-                       fprintf(stderr, "Warning: cannot detach kernel driver.\n");
-       } else {
-               fprintf(stderr, "kernel driver not active.\n");
-       }
-
        ret = libusb_set_configuration(usb_device, AM7XXX_STORAGE_CONFIGURATION);
        if (ret < 0) {
        ret = libusb_set_configuration(usb_device, AM7XXX_STORAGE_CONFIGURATION);
        if (ret < 0) {
-               fprintf(stderr, "cannot set configuration.\n");
+               fprintf(stderr, "libusb_set_configuration failed: %s\n",
+                       libusb_error_name(ret));
+               fprintf(stderr, "Cannot set configuration %hhu\n",
+                       AM7XXX_STORAGE_CONFIGURATION);
                goto out_libusb_close;
        }
 
                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) {
        ret = libusb_claim_interface(usb_device, AM7XXX_STORAGE_INTERFACE);
        if (ret < 0) {
-               fprintf(stderr, "cannot claim interface.\n");
+               fprintf(stderr, "libusb_claim_interface failed: %s\n",
+                       libusb_error_name(ret));
+               fprintf(stderr, "Cannot claim interface %hhu\n",
+                       AM7XXX_STORAGE_INTERFACE);
                goto out_libusb_close;
        }
 
                goto out_libusb_close;
        }