【问题标题】:How to correctly do a USB control transfer (libusb)?如何正确进行 USB 控制传输(libusb)?
【发布时间】:2020-09-17 07:20:32
【问题描述】:

我正在尝试使用 libusb 编写代码来处理我的 Asus Aura RGB 可寻址标头。我目前已经获得了设备的设备句柄,据我所知,我需要使用以下数据对该设备进行控制传输。我使用 USBPcap 捕获的数据。我使用了完全相同的值,但它没有正确发送,我得到以下日志。

USB 设备:

T:  Bus=03 Lev=01 Prnt=01 Port=05 Cnt=02 Dev#=  3 Spd=12  MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0b05 ProdID=18f3 Rev=01.00
S:  Manufacturer=AsusTek Computer Inc.
S:  Product=AURA LED Controller
S:  SerialNumber=9876543210
C:  #Ifs= 2 Cfg#= 1 Atr=a0 MxPwr=16mA
I:  If#=0x0 Alt= 0 #EPs= 0 Cls=ff(vend.) Sub=ff Prot=ff Driver=(none)
I:  If#=0x2 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=00 Driver=usbhid

用于工作 RED 集的 Wireshark:

USB URB
    [Source: host]
    [Destination: 3.3.0]
    URB id: 0xffff9f12269dca80
    URB type: URB_SUBMIT ('S')
    URB transfer type: URB_CONTROL (0x02)
    Endpoint: 0x00, Direction: OUT
    Device: 3
    URB bus id: 3
    Device setup request: relevant (0)
    Data: present (0)
    URB sec: 1600327717
    URB usec: 525841
    URB status: Operation now in progress (-EINPROGRESS) (-115)
    URB length [bytes]: 65
    Data length [bytes]: 65
    [Response in: 24]
    Interval: 0
    Start frame: 0
    Copy of Transfer Flags: 0x00000000
    Number of ISO descriptors: 0
    [bInterfaceClass: Unknown (0xffff)]
Setup Data
    bmRequestType: 0x21
        0... .... = Direction: Host-to-device
        .01. .... = Type: Class (0x1)
        ...0 0001 = Recipient: Interface (0x01)
    bRequest: 9
    wValue: 0x02ec
    wIndex: 2 (0x0002)
    wLength: 65
    Data Fragment: ec40840005ff0000ff0000ff0000ff0000ff000000000000…

非工作代码的日志:

[ 0.328415] [000041d2] libusb: debug [libusb_claim_interface] interface 0
2020-09-17 03:15:38: set_rgb::47 > Length = 65
[ 0.328463] [000041d2] libusb: debug [libusb_alloc_transfer] transfer 0x56099b879030
[ 0.328466] [000041d2] libusb: debug [libusb_submit_transfer] transfer 0x56099b879030
[ 0.328478] [000041d2] libusb: error [submit_control_transfer] submiturb failed error -1 errno=16
[ 0.328482] [000041d2] libusb: debug [libusb_free_transfer] transfer 0x56099b879030
2020-09-17 03:15:38: set_rgb::53 > Failure LIBUSB_ERROR_IO

非工作代码:

static int set_rgb(int red, int green, int blue)
{
    // no rgb just static red for now. (This is the data captured by Wireshark Windows).
    // S Co:3:003:0 s 21 09 02ec 0002 0041 65 = ec408400 05ff0000 ff0000ff 0000ff00 00ff0000 00000000 00000000 00000000
    // looking at this 0x21 bRequestType 0x09 bRequest 0x02ec wValue 0x2 wIndex.
    unsigned char data[65] =
        "\xec\x40\x84\x00\x05\xff\x00\x00\xff\x00\x00\xff\x00\x00\xff\x00"
        "\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";

    struct libusb_config_descriptor *cfg_desc = NULL;
    int ret = 0;

    ret = libusb_set_auto_detach_kernel_driver(dev_handle, 1);
    CHECK_COND(ret == LIBUSB_SUCCESS, ret);

    libusb_get_active_config_descriptor(dev, &cfg_desc);

    int interface_number =
        cfg_desc->interface[0].altsetting[0].bInterfaceNumber;

    ret = libusb_claim_interface(dev_handle, interface_number);
    CHECK_COND(ret == LIBUSB_SUCCESS, ret);

    int r = libusb_control_transfer(dev_handle, 0x21, 9, 0x02ec, 0x0002,
                    data, sizeof(data), 0);
    if (r == LIBUSB_SUCCESS) {
        LOG("Sucess\n");
    } else {
        LOG("Failure %s\n", libusb_error_name(r));
    }

    return r;
}
  1. 我阅读了几篇关于控制传输的不同输入的文章,例如 bRequestCode bRequest wValue wIndex 等。我不确定我是否理解得很好。这些究竟是什么,它的目的是什么?
  2. 如何调试/修复此问题。我尝试使用提升的权限,得到了相同的结果。

【问题讨论】:

  • 关于接口编号的日志有些混乱。设备描述符表示有两个接口,编号为 0 和 2,0 是供应商特定的一个,2 是 HID 设备。 Wireshark 日志显示接口号 1 的控制消息。interface_number 的实际值是多少?尝试 0、1 和 2。

标签: usb libusb


【解决方案1】:

你从 libusb 得到的错误是 16,它是 EBUSY(设备或资源忙)。您是否可能已经在系统中为此 USB 设备安装了一些驱动程序?如果是 - 尝试通过卸载此设备的驱动程序模块来解决此问题,然后再次运行您的程序。

【讨论】:

  • 我已在 OP 中添加了与端点关联的驱动程序。我看不到任何附加/使用它的东西。
猜你喜欢
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2013-06-18
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多