X-Git-Url: https://git.ao2.it/libam7xxx.git/blobdiff_plain/e92eacd8960854b239d698bf05b97f6089a478c6..e3943041a1d8869fb9d6779d49946e0a9d7dbff2:/examples/am7xxx-modeswitch.c diff --git a/examples/am7xxx-modeswitch.c b/examples/am7xxx-modeswitch.c index bba3f3c..1230bd4 100644 --- a/examples/am7xxx-modeswitch.c +++ b/examples/am7xxx-modeswitch.c @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -33,47 +34,78 @@ static unsigned char switch_command[] = int main(void) { int ret; - int transferred; - libusb_device_handle *usb_device = NULL; - + libusb_device_handle *usb_device; + int current_configuration; unsigned int len; + int transferred; ret = libusb_init(NULL); - if (ret < 0) + if (ret < 0) { + fprintf(stderr, "libusb_init failed: %s\n", + libusb_error_name(ret)); 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) { - fprintf(stderr, "cannot open the device: %d.\n", errno); + fprintf(stderr, "libusb_open failed: %s\n", strerror(errno)); 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); + current_configuration = -1; + ret = libusb_get_configuration(usb_device, ¤t_configuration); if (ret < 0) { - fprintf(stderr, "cannot set configuration.\n"); + fprintf(stderr, "libusb_get_configuration failed: %s\n", + libusb_error_name(ret)); 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 %hhu\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, "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; } + /* 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, ¤t_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: %hhu, current: %hhu\n", + AM7XXX_STORAGE_CONFIGURATION, current_configuration); + ret = -EINVAL; + goto out_libusb_release_interface; + } + len = sizeof(switch_command); transferred = 0; ret = libusb_bulk_transfer(usb_device, AM7XXX_STORAGE_OUT_EP,