c++ - `libusb_attach_kernel_driver` 不工作

标签 c++ linux usb libusb

我在完全关闭使用 libusb 的 Linux 应用程序时遇到问题(内核不回收接口(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);

问题是重新附加内核驱动程序不起作用。实际上 libusb_kernel_driver_active 不会返回 1,但即使我将其注释掉并始终调用 libusb_attach_kernel_driver,我也永远不会取回我的 /dev/ttyACM0 设备。在这种情况下,我得到 LIBUSB_ERROR_NOT_FOUND

最佳答案

我已经调试了 linux cdc-acm 驱动程序附加代码,并且找出了问题的根本原因。重新连接不起作用的原因是我同时声明了 CDC ACM 设备的控制和数据接口(interface)。如果我只分离/附加控制接口(interface)(ifnum == 0),那么一切都按预期工作。这应该记录在某处。

关于c++ - `libusb_attach_kernel_driver` 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280258/

相关文章:

android - 选中 CCMenuItemToggle cocos2d-x

c++ - Makefile 错误 - 找不到文件 - *.cpp

android - 连接 USB 电缆后自动启动我的 android 应用程序。

python - 一些帮助理解异步 USB 操作与 libusb-1.0 和 ctypes

c# - 在 C# 中检测 USB 设备的插入和移除

c++ - 二叉搜索树实现

c++ - 为什么 const YAML::Node 对象的行为不像具有 yaml-cpp 的类值对象?

c++ - 编译错误 'set_ssl_context_callback' : on Windows

linux - 仅一次将 2 行添加到文件中,但下次在 shell 脚本中不形成

regex - Bash 使用 cut 获取字段