Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`libusb_attach_kernel_driver` not working

I have a problem shutting down cleanly my Linux application that uses libusb (the kernel does not reclaim the interface):

int rc;

rc = libusb_reset_device(handle_);
if (rc < 0) {
    cerr << "Error resetting the device: " << libusb_error_name(rc);
}

for (int ifnum = 0; ifnum < 2; ifnum++) {
    rc = libusb_release_interface(handle_, ifnum);
    if (rc < 0) {
        cerr << "Error releasing interface: " << libusb_error_name(rc);
    }
    if (libusb_kernel_driver_active(handle_, ifnum)) {
        cerr << "Reattaching CDC ACM kernel driver.";
        rc = libusb_attach_kernel_driver(handle_, ifnum);
        if (rc < 0) {
            cerr << "Error reattaching CDC ACM kernel driver: " << libusb_error_name(rc);
        }
    }
}

libusb_close(handle_);
libusb_exit(NULL);

The problem is that the reattaching the kernel driver does not work. Actually libusb_kernel_driver_active does not return 1 but even if I comment it out and always call libusb_attach_kernel_driver, I never get back my /dev/ttyACM0 device. In this case I get LIBUSB_ERROR_NOT_FOUND.

like image 389
major4x Avatar asked Jul 07 '15 22:07

major4x


1 Answers

I have debugged the linux cdc-acm driver attach code and I have figured out the root-cause of the problem. The reason for the reattach not to work is that I was claiming both the control and data interfaces of the CDC ACM device. If I detach/attach the control interface only (ifnum == 0), then everything works as expected. This should be documented somewhere.

like image 156
major4x Avatar answered Oct 03 '22 19:10

major4x