How to set alternate setting for the USB device using libusb
Asked Answered
S

1

21

I am trying to set alternate setting for the USB HUB device which has 5 interfaces. Following are the configurations for each interface.

 1. Ifs= 5 Cfg#= 1 Atr=c0 MxPwr=  2mA A:  FirstIf#= 0 IfCount= 1
            Cls=08(stor.) Sub=06 Prot=50 A:  FirstIf#= 1 IfCount= 1 Cls=03(HID 
            ) Sub=00 Prot=00 A:  FirstIf#= 2 IfCount= 2 Cls=01(audio) Sub=00
            Prot=00 A:  FirstIf#= 4 IfCount= 1 Cls=fe(app. ) Sub=01 Prot=01


 2. I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50
            Driver=usb-storage E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: 
            Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

 3.   I:* If#= 1 Alt= 0 #EPs= 2 Cls=03(HID  ) Sub=00 Prot=00 Driver=usbhid E:     Ad=82(I)   Atr=03(Int.) MxPS=  64 Ivl=128ms E:  Ad=02(O) Atr=03(Int.) MxPS=  64 Ivl=128ms


 4. I:* If#= 2 Alt= 0 #EPs= 0 Cls=01(audio) Sub=01 Prot=00 Driver=snd-usb-audio


**5. I: If#= 3 Alt= 0 #EPs= 0 Cls=01(audio) Sub=02 Prot=00 Driver=(none)
    I:  If#= 3 Alt= 1 #EPs= 1 Cls=01(audio) Sub=02 Prot=00 Driver=(none)
   E:  Ad=83(I) Atr=01(Isoc) MxPS= 320 Ivl=1ms**


 6: I:* If#= 4 Alt= 0 #EPs= 0 Cls=fe(app. ) Sub=01 Prot=01 Driver=(none)

If you see interface 3 supports 2 alternates settings. 0 alternate setting does not have any endpoints. 1 alternate setting have 1 endpoint.

When I am not using the device I will set the alternate setting to 0 so that no end points are used and otherwise I will set the alternate setting to 1.

My Question is. I am using libusb to set the alternate setting. My libusb calls sequence will be.

  to get the required device.
     1. libusb_init
     2. libusb_get_devicelist
     3. search the device using device discriptor
     4. libusb_open



  to set the alternate setting.
  1) Detach the driver if attached libusb_detachdriver.
  2) claim the interface libusb_claiminterface(3)
  3) set alternate setting libusb_setalternatesetting(0/1)
  4) release the claimed interface libusb_releaseinterface(3)
  5) reattach the driver libusb_attachderiver

I execute the 2nd set of call sequence each time when device is being used/not used.

But when I release the claimed interface(libusb_releaseinterface) after setting alternate setting to 1(libusb_alternatesetting(1)), interface is resetting to alternate setting 0 even while device is being used.

I am confused what libusb call sequence i need to use. If i don't release the claimed interface immediately the driver state will be detached until I release the interface and attach the driver.

Sauropod answered 27/7, 2017 at 9:39 Comment(0)
T
1

If you look at the libusb_release_interface function documentation of the API 1.0 documentation, in module Device Handling and Enumeration, it says "A SET_INTERFACE control request will be sent to the device, resetting interface state to the first alternate setting." The first alternate setting is of course the zero one. So if the device is not in use, it always remains at alternate setting zero, freeing bandwidth allocations and allowing the next user to chose the desired alternate setting. So since your code is not the driver, you can't make the decision. Either the driver itself, directly, or indirectly, the user of the driver needs to chose the alternate setting while the driver is attached. For audio interfaces, for example, the user code typically requests a sample rate and channel format and the driver selects the appropriate alternate setting.

Alternatively, you could possibly drive the device directly via libusb, not using the driver at all.

Tiffaneytiffani answered 3/8, 2022 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.