You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在libusb中实现设备先发URB_INTERRUPT IN的交互流程

Linux下复刻USB设备通信程序的URB交互问题

我在Linux上复刻一款与USB设备通信的Windows程序,目前卡在设备先发URB_INTERRUPT IN包、主机再响应的环节。之前仅接触过主机先发请求的相反场景,且知晓实现方法,但对当前目标场景毫无头绪。

期望的交互抓包

Frame 29: 72 bytes on wire (576 bits), 72 bytes captured (576 bits) on interface usbmon0, id 0
USB URB
    [Source: 1.16.2]
    [Destination: host]
    URB id: 0xffff8d92d4ece840
    URB type: URB_COMPLETE ('C')
    URB transfer type: URB_INTERRUPT (0x01)
    Endpoint: 0x82, Direction: IN
    Device: 16
    URB bus id: 1
    Device setup request: not relevant ('-')
    Data: present (0)
    URB sec: 1664781571
    URB usec: 862138
    URB status: Success (0)
    URB length [bytes]: 8
    Data length [bytes]: 8
    [bInterfaceClass: Unknown (0xffff)]
    Unused Setup Header
    Interval: 8
    Start frame: 0
    Copy of Transfer Flags: 0x00000200, Dir IN
    Number of ISO descriptors: 0
Leftover Capture Data: 0508ff0000000000

Frame 30: 64 bytes on wire (512 bits), 64 bytes captured (512 bits) on interface usbmon0, id 0
USB URB
    [Source: host]
    [Destination: 1.16.2]
    URB id: 0xffff8d9321011cc0
    URB type: URB_SUBMIT ('S')
    URB transfer type: URB_INTERRUPT (0x01)
    Endpoint: 0x82, Direction: IN
    Device: 16
    URB bus id: 1
    Device setup request: not relevant ('-')
    Data: not present ('<')
    URB sec: 1664781571
    URB usec: 865989
    URB status: Operation now in progress (-EINPROGRESS) (-115)
    URB length [bytes]: 8
    Data length [bytes]: 0
    [bInterfaceClass: Unknown (0xffff)]
    Unused Setup Header
    Interval: 8
    Start frame: 0
    Copy of Transfer Flags: 0x00000200, Dir IN
    Number of ISO descriptors: 0

该流程中设备先发送URB_COMPLETE类型的包,主机再发送URB_SUBMIT类型的包。

当前实现代码

我用以下函数实现URB中断传输:

#define INTR_EP_IN 0x82
#define INTR_LENGTH 8
typedef unsigned char byte_t;

/* Proper opening, kernel detaching, and interface claiming... */

static int send_interrupt(libusb_device_handle *handle)
{
    byte_t *packet;
    int transferred = 0;
    short sent;

    packet = calloc(INTR_LENGTH, 1);
    sent = libusb_interrupt_transfer(handle, INTR_EP_IN, packet, INTR_LENGTH,
                                     &transferred, 10);
    free(packet);
    /* Note: always TIMEOUT_ERROR, but the packet gets captured anyway */
    /*
    if(sent != 0)
        fprintf(stderr, INTRRPT_ERR_MSG, libusb_strerror(sent));
    */
    return sent;
}

实际抓包结果

尝试调整libusb_interrupt_transfer的参数后,仍未得到期望结果,每次抓包如下:

Frame 30: 64 bytes on wire (512 bits), 64 bytes captured (512 bits) on interface usbmon0, id 0
USB URB
    [Source: host]
    [Destination: 1.6.2]
    URB id: 0xffff922c3bd69540
    URB type: URB_SUBMIT ('S')
    URB transfer type: URB_INTERRUPT (0x01)
    Endpoint: 0x82, Direction: IN
    Device: 6
    URB bus id: 1
    Device setup request: not relevant ('-')
    Data: not present ('<')
    URB sec: 1668092600
    URB usec: 503008
    URB status: Operation now in progress (-EINPROGRESS) (-115)
    URB length [bytes]: 8
    Data length [bytes]: 0
    [Response in: 31]
    [bInterfaceClass: Unknown (0xffff)]
    Unused Setup Header
    Interval: 8
    Start frame: 0
    Copy of Transfer Flags: 0x00000200, Dir IN
    Number of ISO descriptors: 0

Frame 31: 64 bytes on wire (512 bits), 64 bytes captured (512 bits) on interface usbmon0, id 0
USB URB
    [Source: 1.6.2]
    [Destination: host]
    URB id: 0xffff922c3bd69540
    URB type: URB_COMPLETE ('C')
    URB transfer type: URB_INTERRUPT (0x01)
    Endpoint: 0x82, Direction: IN
    Device: 6
    URB bus id: 1
    Device setup request: not relevant ('-')
    Data: present (0)
    URB sec: 1668092600
    URB usec: 506108
    URB status: No such file or directory (-ENOENT) (-2)
    URB length [bytes]: 0
    Data length [bytes]: 0
    [Request in: 30]
    [Time from request: 0.003100000 seconds]
    [bInterfaceClass: Unknown (0xffff)]
    Unused Setup Header
    Interval: 8
    Start frame: 0
    Copy of Transfer Flags: 0x00000200, Dir IN
    Number of ISO descriptors: 0

我并非USB协议专家,可能存在认知盲区;也有可能libusb不支持此类传输,此时我只能借助系统调用,但这是我的最后选择。

内容的提问来源于stack exchange,提问作者Ors1mer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:25:23