简体   繁体   English

如何使用 libusb 为 USB 设备设置备用设置

[英]How to set alternate setting for the USB device using libusb

I am trying to set alternate setting for the USB HUB device which has 5 interfaces.我正在尝试为具有 5 个接口的 USB HUB 设备设置备用设置。 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.如果您看到接口 3 支持 2 个备用设置。 0 alternate setting does not have any endpoints. 0 备用设置没有任何端点。 1 alternate setting have 1 endpoint. 1 个备用设置有 1 个端点。

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.当我不使用设备时,我会将备用设置设置为 0,以便不使用端点,否则我会将备用设置设置为 1。

My Question is.我的问题是。 I am using libusb to set the alternate setting.我正在使用 libusb 来设置备用设置。 My libusb calls sequence will be.我的 libusb 调用顺序将是。

  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.但是,当我在将备用设置设置为 1(libusb_alternatesetting(1))后释放声明的接口(libusb_releaseinterface)时,即使在使用设备时,接口也会重置为备用设置 0。

I am confused what libusb call sequence i need to use.我很困惑我需要使用什么 libusb 调用序列。 If i don't release the claimed interface immediately the driver state will be detached until I release the interface and attach the driver.如果我不立即释放声明的接口,驱动程序 state 将被分离,直到我释放接口并附加驱动程序。

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. 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当然是零一。 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.或者,您可以直接通过 libusb 驱动设备,根本不使用驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM