How to correctly do a USB control transfer (libusb)?

1

I'm trying to write code using libusb to mess with my Asus Aura RGB addressable headers. I've currently got the device handle for the device and as far as I have figured I need to do a control transfer to this device with the following data. The data I captured using USBPcap. I used the exact same values it used but it doesn't send correctly and I get the following logs.

USB Device:

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

Wireshark for working RED set:

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…

Logs for non-working code:

[ 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

non-working code:

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. I read a couple of articles about different inputs to the control transfer like the bRequestCode bRequest wValue wIndex etc. I'm not really sure I understand it well. What exactly are these and what is it's purpose?
  2. How do I debug/fix this issue. I tried with elevated privileges and I get the same result.
usb
libusb
asked on Stack Overflow Sep 17, 2020 by user282909 • edited Sep 17, 2020 by user282909

1 Answer

0

The error you have got form libusb is 16 and it is EBUSY (device or resource busy). Is it possible that you already have some driver installed in the system for this USB device? If yes - try to fix this problem by unloading driver modules for this device and then run your program again.

answered on Stack Overflow Sep 17, 2020 by user2699113

User contributions licensed under CC BY-SA 3.0