usb alternate settings

2023-10-10 18:50
文章标签 settings usb alternate

本文主要是介绍usb alternate settings,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言


        最近在看linux usb core 驱动源码,对alternate settings的概念有点疑问,记录下网上好的解释。

链接


        http://stackoverflow.com/questions/28274535/what-is-an-alternate-setting-in-a-usb-interface
        http://www.beyondlogic.org/usbnutshell/usb5.shtml

原文


USB Descriptors

  • All USB devices have a hierarchy of descriptors which describe to the host information such as what the device is, who makes it, what version of USB it supports, how many ways it can be configured, the number of endpoints and their types etc

    The more common USB descriptors are

    • Device Descriptors
    • Configuration Descriptors
    • Interface Descriptors
    • Endpoint Descriptors
    • String Descriptors

    USB devices can only have one device descriptor. The device descriptor includes information such as what USB revision the device complies to, the Product and Vendor IDs used to load the appropriate drivers and the number of possible configurations the device can have. The number of configurations indicate how many configuration descriptors branches are to follow.

    The configuration descriptor specifies values such as the amount of power this particular configuration uses, if the device is self or bus powered and the number of interfaces it has. When a device is enumerated, the host reads the device descriptors and can make a decision of which configuration to enable. It can only enable one configuration at a time.

    For example, It is possible to have a high power bus powered configuration and a self powered configuration. If the device is plugged into a host with a mains power supply, the device driver may choose to enable the high power bus powered configuration enabling the device to be powered without a connection to the mains, yet if it is connected to a laptop or personal organiser it could enable the 2nd configuration (self powered) requiring the user to plug your device into the power point.

    The configuration settings are not limited to power differences. Each configuration could be powered in the same way and draw the same current, yet have different interface or endpoint combinations. However it should be noted that changing the configuration requires all activity on each endpoint to stop. While USB offers this flexibility, very few devices have more than 1 configuration.

    The interface descriptor could be seen as a header or grouping of the endpoints into a functional group performing a single feature of the device. For example you could have a multi-function fax/scanner/printer device. Interface descriptor one could describe the endpoints of the fax function, Interface descriptor two the scanner function and Interface descriptor three the printer function. Unlike the configuration descriptor, there is no limitation as to having only one interface enabled at a time. A device could have 1 or many interface descriptors enabled at once.

    Interface descriptors have a bInterfaceNumber field specifying the Interface number and a bAlternateSetting which allows an interface to change settings on the fly. For example we could have a device with two interfaces, interface one and interface two. Interface one has bInterfaceNumber set to zero indicating it is the first interface descriptor and abAlternativeSetting of zero.

    Interface two would have a bInterfaceNumber set to one indicating it is the second interface and a bAlternativeSettingof zero (default). We could then throw in another descriptor, also with a bInterfaceNumber set to one indicating it is the second interface, but this time setting the bAlternativeSetting to one, indicating this interface descriptor can be an alternative setting to that of the other interface descriptor two.

    When this configuration is enabled, the first two interface descriptors with bAlternativeSettings equal to zero is used. However during operation the host can send a SetInterface request directed to that of Interface one with a alternative setting of one to enable the other interface descriptor.

    This gives an advantage over having two configurations, in that we can be transmitting data over interface zero while we change the endpoint settings associated with interface one without effecting interface zero.

    Each endpoint descriptor is used to specify the type of transfer, direction, polling interval and maximum packet size for each endpoint. Endpoint zero, the default control endpoint is always assumed to be a control endpoint and as such never has a descriptor.

Composition of USB Descriptors

  • All descriptors are made up of a common format. The first byte specifies the length of the descriptor, while the second byte indicates the descriptor type. If the length of a descriptor is smaller than what the specification defines, then the host shall ignore it. However if the size is greater than expected the host will ignore the extra bytes and start looking for the next descriptor at the end of actual length returned.

    0bLength1Number

    Size of Descriptor in Bytes

    1bDescriptionType1Constant

    DescriptorType

    2...n 

    Start of parameters for descriptor

Device Descriptors

  • The device descriptor of a USB device represents the entire device. As a result a USB device can only have one device descriptor. It specifies some basic, yet important information about the device such as the supported USB version, maximum packet size, vendor and product IDs and the number of possible configurations the device can have. The format of the device descriptor is shown below.

    0bLength1Number

    Size of the Descriptor in Bytes (18 bytes)

    1bDescriptorType1Constant

    Device Descriptor (0x01)

    2bcdUSB2BCD

    USB Specification Number which device complies too.

    4bDeviceClass1Class

    Class Code (Assigned by USB Org)

    If equal to Zero, each interface specifies it’s own class code

    If equal to 0xFF, the class code is vendor specified.

    Otherwise field is valid Class Code.

    5bDeviceSubClass1SubClass

    Subclass Code (Assigned by USB Org)

    6bDeviceProtocol1Protocol

    Protocol Code (Assigned by USB Org)

    7bMaxPacketSize1Number

    Maximum Packet Size for Zero Endpoint. Valid Sizes are 8, 16, 32, 64

    8idVendor2ID

    Vendor ID (Assigned by USB Org)

    10idProduct2ID

    Product ID (Assigned by Manufacturer)

    12bcdDevice2BCD

    Device Release Number

    14iManufacturer1Index

    Index of Manufacturer String Descriptor

    15iProduct1Index

    Index of Product String Descriptor

    16iSerialNumber1Index

    Index of Serial Number String Descriptor

    17bNumConfigurations1Integer

    Number of Possible Configurations

    • The bcdUSB field reports the highest version of USB the device supports. The value is in binary coded decimal with a format of 0xJJMN where JJ is the major version number, M is the minor version number and N is the sub minor version number. e.g. USB 2.0 is reported as 0x0200, USB 1.1 as 0x0110 and USB 1.0 as 0x0100.

    • The bDeviceClass, bDeviceSubClass and bDeviceProtocol are used by the operating system to find a class driver for your device. Typically only the bDeviceClass is set at the device level. Most class specifications choose to identify itself at the interface level and as a result set the bDeviceClass as 0x00. This allows for the one device to support multiple classes.

    • The bMaxPacketSize field reports the maximum packet size for endpoint zero. All devices must support endpoint zero.

    • The idVendor and idProduct are used by the operating system to find a driver for your device. The Vendor ID is assigned by the USB-IF.

    • The bcdDevice has the same format than the bcdUSB and is used to provide a device version number. This value is assigned by the developer.

    • Three string descriptors exist to provide details of the manufacturer, product and serial number. There is no requirement to have string descriptors. If no string descriptor is present, a index of zero should be used.

    • bNumConfigurations defines the number of configurations the device supports at its current speed.

Configuration Descriptors

  • A USB device can have several different configurations although the majority of devices are simple and only have one. The configuration descriptor specifies how the device is powered, what the maximum power consumption is, the number of interfaces it has. Therefore it is possible to have two configurations, one for when the device is bus powered and another when it is mains powered. As this is a "header" to the Interface descriptors, its also feasible to have one configuration using a different transfer mode to that of another configuration.

    Once all the configurations have been examined by the host, the host will send a SetConfiguration command with a non zero value which matches the bConfigurationValue of one of the configurations. This is used to select the desired configuration.

    0bLength1Number

    Size of Descriptor in Bytes

    1bDescriptorType1Constant

    Configuration Descriptor (0x02)

    2wTotalLength2Number

    Total length in bytes of data returned

    4bNumInterfaces1Number

    Number of Interfaces

    5bConfigurationValue1Number

    Value to use as an argument to select this configuration

    6iConfiguration1Index

    Index of String Descriptor describing this configuration

    7bmAttributes1Bitmap

    D7 Reserved, set to 1. (USB 1.0 Bus Powered)

    D6 Self Powered

    D5 Remote Wakeup

    D4..0 Reserved, set to 0.

    8bMaxPower1mA

    Maximum Power Consumption in 2mA units

    • When the configuration descriptor is read, it returns the entire configuration hierarchy which includes all related interface and endpoint descriptors. The wTotalLength field reflects the number of bytes in the hierarchy.

    • bNumInterfaces specifies the number of interfaces present for this configuration.

    • bConfigurationValue is used by the SetConfiguration request to select this configuration.

    • iConfiguration is a index to a string descriptor describing the configuration in human readable form.

    • bmAttributes specify power parameters for the configuration. If a device is self powered, it sets D6. Bit D7 was used in USB 1.0 to indicate a bus powered device, but this is now done by bMaxPower. If a device uses any power from the bus, whether it be as a bus powered device or as a self powered device, it must report its power consumption in bMaxPower. Devices can also support remote wakeup which allows the device to wake up the host when the host is in suspend.

    • bMaxPower defines the maximum power the device will drain from the bus. This is in 2mA units, thus a maximum of approximately 500mA can be specified. The specification allows a high powered bus powered device to drain no more than 500mA from Vbus. If a device loses external power, then it must not drain more than indicated in bMaxPower. It should fail any operation it cannot perform without external power.

Interface Descriptors

  • The interface descriptor could be seen as a header or grouping of the endpoints into a functional group performing a single feature of the device. The interface descriptor conforms to the following format,

    0bLength1Number

    Size of Descriptor in Bytes (9 Bytes)

    1bDescriptorType1Constant

    Interface Descriptor (0x04)

    2bInterfaceNumber1Number

    Number of Interface

    3bAlternateSetting1Number

    Value used to select alternative setting

    4bNumEndpoints1Number

    Number of Endpoints used for this interface

    5bInterfaceClass1Class

    Class Code (Assigned by USB Org)

    6bInterfaceSubClass1SubClass

    Subclass Code (Assigned by USB Org)

    7bInterfaceProtocol1Protocol

    Protocol Code (Assigned by USB Org)

    8iInterface1Index

    Index of String Descriptor Describing this interface

    • bInterfaceNumber indicates the index of the interface descriptor. This should be zero based, and incremented once for each new interface descriptor.

    • bAlternativeSetting can be used to specify alternative interfaces. These alternative interfaces can be selected with the Set Interface request.

    • bNumEndpoints indicates the number of endpoints used by the interface. This value should exclude endpoint zero and is used to indicate the number of endpoint descriptors to follow.

    • bInterfaceClass, bInterfaceSubClass and bInterfaceProtocol can be used to specify supported classes (e.g. HID, communications, mass storage etc.) This allows many devices to use class drivers preventing the need to write specific drivers for your device.

    • iInterface allows for a string description of the interface.

Endpoint Descriptors

  • Endpoint descriptors are used to describe endpoints other than endpoint zero. Endpoint zero is always assumed to be a control endpoint and is configured before any descriptors are even requested. The host will use the information returned from these descriptors to determine the bandwidth requirements of the bus.

    0bLength1Number

    Size of Descriptor in Bytes (7 bytes)

    1bDescriptorType1Constant

    Endpoint Descriptor (0x05)

    2bEndpointAddress1EndpointEndpoint Address
    Bits 0..3b Endpoint Number.
    Bits 4..6b Reserved. Set to Zero
    Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints)
    3bmAttributes1BitmapBits 0..1 Transfer Type
    • 00 = Control
      01 = Isochronous
      10 = Bulk
      11 = Interrupt
    Bits 2..7 are reserved. If Isochronous endpoint, 
    Bits 3..2 = Synchronisation Type (Iso Mode)
    • 00 = No Synchonisation
      01 = Asynchronous
      10 = Adaptive
      11 = Synchronous
    Bits 5..4 = Usage Type (Iso Mode)
    • 00 = Data Endpoint
      01 = Feedback Endpoint
      10 = Explicit Feedback Data Endpoint
      11 = Reserved
    4wMaxPacketSize2Number

    Maximum Packet Size this endpoint is capable of sending or receiving

    6bInterval1Number

    Interval for polling endpoint data transfers. Value in frame counts. Ignored for Bulk & Control Endpoints. Isochronous must equal 1 and field may range from 1 to 255 for interrupt endpoints.

    • bEndpointAddress indicates what endpoint this descriptor is describing.

    • bmAttributes specifies the transfer type. This can either be Control, Interrupt, Isochronous or Bulk Transfers. If an Isochronous endpoint is specified, additional attributes can be selected such as the Synchronisation and usage types.

    • wMaxPacketSize indicates the maximum payload size for this endpoint.

    • bInterval is used to specify the polling interval of certain transfers. The units are expressed in frames, thus this equates to either 1ms for low/full speed devices and 125us for high speed devices.

String Descriptors

  • String descriptors provide human readable information and are optional. If they are not used, any string index fields of descriptors must be set to zero indicating there is no string descriptor available.

    The strings are encoded in the Unicode format and products can be made to support multiple languages. String Index 0 should return a list of supported languages. A list of USB Language IDs can be found in Universal Serial Bus Language Identifiers (LANGIDs) version 1.0

    0bLength1Number

    Size of Descriptor in Bytes

    1bDescriptorType1Constant

    String Descriptor (0x03)

    2wLANGID[0]2number

    Supported Language Code Zero
    (e.g. 0x0409 English - United States)

    4wLANGID[1]2number

    Supported Language Code One
    (e.g. 0x0c09 English - Australian)

    nwLANGID[x]2number

    Supported Language Code x
    (e.g. 0x0407 German - Standard)

    The above String Descriptor shows the format of String Descriptor Zero. The host should read this descriptor to determine what languages are available. If a language is supported, it can then be referenced by sending the language ID in the wIndex field of a Get Descriptor(String) request.

    All subsequent strings take on the format below,

    0bLength1Number

    Size of Descriptor in Bytes

    1bDescriptorType1Constant

    String Descriptor (0x03)

    2bStringnUnicode

    Unicode Encoded String




What is an Alternate Setting in a USB interface?

up vote 0 down vote favorite

What is an Alternate Setting? Example of device that needs them?

The USB spec has a lot of talk about them, but never tells what one is and - most importantly - why I would use one. I never saw a USB device that uses alternate settings.

share improve this question
 

1 Answer

active oldest votes
up vote 2 down vote accepted

Alternate setting is used to provide the advantage of having two configuration on the fly even though the device has only one configuration.

The alternate setting of a USB interface is define by the bAlternativeSetting attribute in the device descriptor. A USB interface with an alternate settings can be used in two mode. For example a USBinterface endpoints may act as INTERRUPT pipes in normal settings, but might act as BULK pipe in alternate settings providing you the facility of two different mode on the same interface. You just have to send a SetInterface request to activate a settings. I myself didn't have to use it much though. And I think normally device drivers don't tend to use it as I couldn't find out any example driver.

You can look at this link for more details.

share improve this answer

这篇关于usb alternate settings的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/182378

相关文章

Usb Audio Device Descriptor(10) Hid Device

对于 Standard Interface Descriptor, 当 bInterfaceClass=0x03时,即为HID设备。Standard Interface Descriptor如下 struct usb_standard_interface_descriptor{U8 bLength; /*Size of this descriptor in bytes*/U8 bDescrip

Android rk3399 UAC(USB Audio)开发笔记

一、UAC有1.0和2.0,因Windows对2.0支持不好,我使用的是UAC1.0驱动 内核配置:CONFIG_USB_CONFIGFS_F_UAC1          ---这个宏配置无需物理codec,使用虚拟 alsa codec  驱动路径:"kernel\drivers\usb\gadget\function\f_uac1.c" 内核配置:CONFIG_USB_CONFIGFS_

多款式随身WiFi如何挑选,USB随身WiFi、无线电池随身WiFi、充电宝随身WiFi哪个好?优缺点分析!

市面上的随身WiFi款式多样琳琅满目,最具代表性的就是USB插电款、无线款和充电宝款。今天就来用一篇文章分析一下这三种款式的优缺点。 USB插电款 优点:便宜,无需充电,在有电源的地方可以随时随地插电使用,比如中兴的USB随身WiFi。 缺点:无电源的情况下,无法带出门使用,部分品牌考虑到这个问题,会配备一个充电仓,这个充电仓相对来说就有点累赘了。网速上也不太稳定,波动比较大。

基于FPGA的开源项目:FOC/SHA/USB/JPEG等

文章目录 [1. USB 1.1控制器](https://github.com/WangXuan95/FPGA-USB-Device)[2. FOC控制算法](https://github.com/WangXuan95/FPGA-FOC)[3. BSV高级硬件描述语言入门指南](https://github.com/WangXuan95/BSV_Tutorial_cn)[4. 基于XDMA的

Android Settings搜索Search方案分析

Android开发会遇到一些自写界面需要允许被搜索,或者三方应用挂靠在Settings,用户也希望能被搜索。 在知道怎么添加之前,得先了解下整个框架,才能更好地加入我们自己的代码。   这里稍微整理了下整个search database数据如何索引加载流程。 Settings搜索界面是由SearchFragment展现,当用户在Settings主页中点击搜索图标,会启动到SearchAc

关于小米手机USB传输稍大点的文件老中断的问题解决方法!

关于小米手机USB传输稍大点的文件老中断的问题解决方法! 这是一个很痛苦的事情,当你传输大文件的时候,传输到一半就会莫名其妙的中断,拔插数据线很多次以后,好不容易没准可以成功传输一次。 后来使用了360的手机助手,从调试模式传输文件,虽然不会中断,但是慢的要死。 最后我看到手机插上后手机提示 有3种模式:仅限充电 传输文件(MTP) 传输照片(PTP)。当然我们选择传输文件是没戏了,会中

VirtualBox安装VirtualBox Extension Pack,支持USB No USB devices connected after upgrade

安装步骤及出现问题解决No USB devices connected after upgrade: 一、本要主机ubuntu14.04,安装virtualbox,支持usb设置步骤: 1.安装VirtualBox. 可以从https://www.virtualbox.org官方站点下载或者从软件中心。 2.在VirtualBox里安装Windows; 3.为USB2.0,你需要

QT Android开发之Android端usb调试模式设置与问题解决

一.QT android开发android端usb调试模式设置 QT android开发环境搭建完成后,android设备需要打开usb调试模式才能正常连接,下面以小米ipad为例进行设置(其他手机和ipad设置方法类似) 常见问题: 本文会介绍相关问题的解决方法。 二.小

【电子通识】无法识别USB设备怎么就和USB线序有关系

最近在做一个和USB通信有关的工装,有点类似于HUB但又有点不同。Type-C和USB-A两种输入选择,然后再选择输出8个通道中的一个。         在焊接调试时,首先先验证一路。因为USB-A公头转USB-A公头的线有点难找,所以找到一根USB线缆,中间剪掉一半直接焊在USBA焊盘上方便调试。         根据标准的USB接口线颜色的定义,一般为红(VCC)

将RK3588平台的TMC等USB function驱动挪出内核源码树

背景 前一段时间定位一个上位机通过USB-TMC连接下位机(基于RK3588平台)时界面发生卡顿的问题,发现USB-TMC驱动代码是放在内核源码树里跟内核一起编译的,觉着这样既不便于更换TMC 驱动版本(每次修改代码都要重编内核),也不便于将TMC驱动代码单独放到SVN管理(RK的build系统有16GB之巨,全部提交到SVN服务器吃不消),于是考虑将其挪到源码树外。 想参照自己之前在zynq