Tuesday, February 22, 2022

[SOLVED] How to set interface alternate setting

Issue

I'm trying to set an alternate setting of an interface using USBFS IOCTL calls.

Following is my code snippet.

    int interface = 3;
    struct usbdevfs_ioctl command;
    struct usbdevfs_getdriver getdrv;

    getdrv.interface = interface;
    ret = ioctl(fd, USBDEVFS_GETDRIVER, &getdrv);
    if (ret < 0)
    {
            print((" get driver failed %d %d", ret, errno));
    }

    command.ifno = interface;
    command.ioctl_code = USBDEVFS_DISCONNECT;
    command.data = NULL;
    ret = ioctl(fd, USBDEVFS_IOCTL, &command);
    if (ret < 0)
    {
            print((" detach driver failed %d %d", ret, errno));
    }

   ret = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &interface);
    if (ret < 0)
   {
            print(("claim interface failed %d %d", ret, errno));
   }

    si.interface = 3;
    si.altsetting = setZerobandwidth;
    ret = ioctl(fd, USBDEVFS_SETINTERFACE, &si);
    if (ret < 0)
    {
            print(("set interface ioctl failed %d %d", ret, errno));

    }

    ret = ioctl(fd, USBDEVFS_RELEASEINTERFACE, &interface);
    if (ret < 0)
   {
            print(("release interface ioctl failed %d %d", ret, errno));
   }

    command.ifno = interface;
    command.ioctl_code = USBDEVFS_CONNECT;
    command.data = NULL;

    ret = ioctl(fd, USBDEVFS_IOCTL, &command);

    if (ret < 0)
    {
            print(("attach driver ioctl failed %d %d", ret, errno));
    }

However ret = ioctl(fd, USBDEVFS_SETINTERFACE, &si) is working fine but once I release the interface ret = ioctl(fd, USBDEVFS_RELEASEINTERFACE, &interface); alternate setting is resetting to first altsetting.

As per libusb API Doc, libusb_release_interface will reset the alternate setting to first alternate setting. Please help me with what IOCTL calls I need to follow.


Solution

You cannot change the alt setting permanently from user space. This can be changed permanently from kernal space.

I would suggest you close usage of end point, kernel will set it to zero bandwidth interface once you close.



Answered By - user10243916
Answer Checked By - Candace Johnson (WPSolving Volunteer)