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



我正在尝试为具有 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 调用序列。如果我不立即释放声明的接口,则驱动程序状态将被分离,直到我释放接口并附加驱动程序。

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

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

最新更新