`libusb_attach_kernel_driver` not working

`libusb_attach_kernel_driver` not working

本文关键字:not driver working attach libusb kernel      更新时间:2023-10-16

我有一个问题,干净地关闭我的Linux应用程序,使用libusb(内核不回收接口):

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);

问题是重新连接内核驱动程序不工作。实际上,libusb_kernel_driver_active不返回1,但即使我注释掉它并始终调用libusb_attach_kernel_driver,我也永远不会返回我的/dev/ttyACM0设备。在本例中我得到LIBUSB_ERROR_NOT_FOUND

我已经调试了linux cdc-acm驱动附加代码,并找到了问题的根本原因。重新连接不工作的原因是我正在声明CDC ACM设备的控制和数据接口。如果我只分离/附加控制接口(ifnum == 0),那么一切都按预期工作。