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

问题描述 投票:0回答:2

我正在尝试为具有 5 个接口的 USB HUB 设备设置备用设置。 以下是每个接口的配置。

 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)

如果你看到接口 3 支持 2 交替设置。 0 备用设置没有任何端点。 1 个备用设置有 1 个端点。

当我不使用设备时,我会将备用设置设置为 0,这样就不会使用端点,否则我会将备用设置设置为 1.

我的问题是。 我正在使用 libusb 来设置备用设置。我的 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

我每次在使用/不使用设备时执行第二组调用序列。

但是当我在将备用设置设置为 1(libusb_alternatesetting(1)) 后释放声明的接口(libusb_releaseinterface)时,即使正在使用设备,接口也会重置为备用设置 0。

我很困惑我需要使用什么 libusb 调用序列。如果我不立即释放声明的接口,驱动程序状态将被分离,直到我释放接口并附加驱动程序。

c++ c usb endpoint libusb
2个回答
0
投票

如果你看看 libusb_release_interface 函数文档 API 1.0 文档,在设备处理和枚举模块中, 它说“一个 SET_INTERFACE 控制请求将被发送到设备,将接口状态重置为第一个备用设置。”第一个备用设置当然是零。因此,如果设备不在使用中,它始终保持备用设置为零,从而释放带宽分配并允许下一个用户选择所需的备用设置。因此,由于您的代码不是驱动程序,因此您无法做出决定。驱动程序本身,直接或间接地,驱动程序的用户需要在连接驱动程序时选择备用设置。例如,对于音频接口,用户代码通常会请求采样率和通道格式,而驱动程序会选择适当的备用设置。

或者,您可以直接通过 libusb 驱动设备,根本不使用驱动程序。


0
投票

听起来您面临的问题是 USB 接口正在重置为备用设置 0,即使您将其设置为备用设置 1。发生这种情况的原因可能是设备或驱动程序正在重置接口,或者因为备用设置更改未正确应用。

要解决此问题,我建议您尝试以下操作:

  • 查看libusb_setalternatesetting()函数的返回值 以确保它成功。

  • 验证界面实际上正在更改为备用设置 1 通过使用 libusb_get_interface_altsetting() 函数来获取
    当前备用设置。

  • 验证设备没有将接口重置为备用
    通过检查设备文档或联系
    设置 0 制造商。

  • 将备用设置设置为 1 后尝试分离驱动程序,
    然后在再次声明接口之前重新连接它。

  • 尝试在声明接口之前将备用设置设置为 1, 然后在不设置alternate
    的情况下释放接口 设置回 0.

此外,您可能需要考虑使用 USB 协议分析器或类似 Wireshark 的工具来监控 USB 流量,并查看总线上是否发生任何错误或意外行为。

© www.soinside.com 2019 - 2024. All rights reserved.