⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbs.sgml

📁 eCos操作系统源码
💻 SGML
📖 第 1 页 / 共 5 页
字号:
<para>If the application developer explicitly adds a class support packagesuch as the USB-ethernet one then this implies that the USB device isactually needed, and the device will be enabled automatically.However, if no suitable class package is available and the USB devicewill instead be accessed by application code, it is necessary toenable the USB device manually. Usually the easiest way to do this isto enable the configuration option<literal>CYGGLO_IO_USB_SLAVE_APPLICATION</literal>, and the USB devicedriver and related packages will adjust accordingly. Alternatively,the device driver may provide some configuration options to providemore fine-grained control.</para></refsect1></refentry><!-- }}} --><!-- {{{ Enumeration Data               --><refentry id="usbs-enum"><refmeta><refentrytitle>USB Enumeration Data</refentrytitle></refmeta><refnamediv><refname>Enumeration Data</refname><refpurpose>The USB enumeration data structures</refpurpose></refnamediv><refsynopsisdiv><synopsis>#include &lt;cyg/io/usb/usb.h&gt;#include &lt;cyg/io/usb/usbs.h&gt;typedef struct usb_device_descriptor {    &hellip;} usb_device_descriptor __attribute__((packed));typedef struct usb_configuration_descriptor {    &hellip;} usb_configuration_descriptor __attribute__((packed));typedef struct usb_interface_descriptor {    &hellip;} usb_interface_descriptor __attribute__((packed));        typedef struct usb_endpoint_descriptor {    &hellip;} usb_endpoint_descriptor;typedef struct usbs_enumeration_data {    usb_device_descriptor               device;    int                                 total_number_interfaces;    int                                 total_number_endpoints;    int                                 total_number_strings;    const usb_configuration_descriptor* configurations;    const usb_interface_descriptor*     interfaces;    const usb_endpoint_descriptor*      endpoints;    const unsigned char**               strings;} usbs_enumeration_data;</synopsis></refsynopsisdiv><refsect1><title>USB Enumeration Data</title><para>When a USB host detects that a peripheral has been plugged in orpowered up, one of the first steps is to ask the peripheral todescribe itself by supplying enumeration data. Some of this datadepends on the class of peripheral. Other fields are vendor-specific.There is also a dependency on the hardware, specifically whichendpoints are available should be used. In general it is not possiblefor generic code to provide this information, so it is theresponsibility of application code to provide a suitable<structname>usbs_enumeration_data</structname> data structure andinstall it in the endpoint 0 data structure during initialization.This must happen before the USB device is enabled by a call to<function>usbs_start</function>, for example:</para><programlisting width=72>const usbs_enumeration_data usb_enum_data = {    &hellip;};intmain(int argc, char** argv){    usbs_sa11x0_ep0.enumeration_data = &amp;usb_enum_data;    &hellip;    usbs_start(&amp;usbs_sa11x0_ep0);    &hellip;}</programlisting><para>For most applications the enumeration data will be static, althoughthe <structname>usbs_enumeration_data</structname> structure can befilled in at run-time if necessary. Full details of the enumerationdata can be found in the Universal Serial Bus specification obtainablefrom the <ulink url="http://www.usb.org/">USB Implementers Forum website</ulink>, although the meaning of most fields is fairly obvious.The various data structures and utility macros are defined in theheader files <filename class="headerfile">cyg/io/usb/usb.h</filename>and <filename class="headerfile">cyg/io/usb/usbs.h</filename>. Notethat the example code below makes use of the gcc labelled elementextension.</para><refsect2><title><structname>usb_device_descriptor</structname></title><para>The main information about a USB peripheral comes from a single<structname>usb_device_descriptor</structname> structure, which isembedded in the <structname>usbs_enumeration_data</structname>structure. A typical example might look like this:</para><programlisting width=72>const usbs_enumeration_data usb_enum_data = {    {        length:                 USB_DEVICE_DESCRIPTOR_LENGTH,        type:                   USB_DEVICE_DESCRIPTOR_TYPE,        usb_spec_lo:            USB_DEVICE_DESCRIPTOR_USB11_LO,        usb_spec_hi:            USB_DEVICE_DESCRIPTOR_USB11_HI,        device_class:           USB_DEVICE_DESCRIPTOR_CLASS_VENDOR,        device_subclass:        USB_DEVICE_DESCRIPTOR_SUBCLASS_VENDOR,        device_protocol:        USB_DEVICE_DESCRIPTOR_PROTOCOL_VENDOR,        max_packet_size:        8,        vendor_lo:              0x42,        vendor_hi:              0x42,        product_lo:             0x42,        product_hi:             0x42,        device_lo:              0x00,        device_hi:              0x01,        manufacturer_str:       1,        product_str:            2,        serial_number_str:      0,        number_configurations:  1    },    &hellip;};</programlisting><para>The length and type fields are specified by the USB standard. The<structfield>usb_spec_lo</structfield> and<structfield>usb_spec_hi</structfield> fields identify the particularrevision of the standard that the peripheral implements, for examplerevision 1.1.</para><para>The device class, subclass, and protocol fields are used by generichost-side USB software to determine which host-side device drivershould be loaded to interact with the peripheral. A number of standardclasses are defined, for example mass-storage devices andhuman-interface devices. If a peripheral implements one of thestandard classes then a standard existing host-side device driver mayexist, eliminating the need to write a custom driver. The value<literal>0xFF</literal> (<literal>VENDOR</literal>) is reserved forperipherals that implement a vendor-specific protocol rather than astandard one. Such peripherals will require a custom host-side devicedriver. The value <literal>0x00</literal>(<literal>INTERFACE</literal>) is reserved and indicates that theprotocol used by the peripheral is defined at the interface levelrather than for the peripheral as a whole.</para><para>The <structfield>max_package_size</structfield> field specifies themaximum length of a control message. There is a lower bound of eightbytes, and typical hardware will not support anything larger becausecontrol messages are usually small and not performance-critical.</para><para>The <structfield>vendor_lo</structfield> and<structfield>vendor_hi</structfield> fields specify a vendor id, whichmust be obtained from the USB Implementor's Forum. The numbers used inthe code fragment above are examples only and must not be used in realUSB peripherals. The product identifier is determined by the vendor,and different USB peripherals should use different identifiers. Thedevice identifier field should indicate a release number inbinary-coded decimal.</para><para>The above fields are all numerical in nature. A USB peripheral canalso provide a number of strings as described <linklinkend="usbs-enum-strings">below</link>, for example the name of thevendor can be provided. The various <structfield>_str</structfield>fields act as indices into an array of strings, with index 0indicating that no string is available. </para><para>A typical USB peripheral involves just a single configuration. Howevermore complicated peripherals can support multiple configurations. Onlyone configuration will be active at any one time, and the host willswitch between them as appropriate. If a peripheral does involvemultiple configurations then typically it will be the responsibilityof application code to <linklinkend="usbs-control-standard">handle</link> the standardset-configuration control message.</para></refsect2><refsect2><title><structname>usb_configuration_descriptor</structname></title><para>A USB peripheral involves at least one and possible several differentconfigurations. The <structname>usbs_enumeration_data</structname>structure requires a pointer to an array, possibly of length 1, of<structname>usb_configuration_descriptor</structname> structures.Usually a single structure suffices:</para><programlisting width=72>const usb_configuration_descriptor usb_configuration = {    length:             USB_CONFIGURATION_DESCRIPTOR_LENGTH,    type:               USB_CONFIGURATION_DESCRIPTOR_TYPE,    total_length_lo:    USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_LO(1, 2),    total_length_hi:    USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_HI(1, 2),    number_interfaces:  1,    configuration_id:   1,    configuration_str:  0,    attributes:         USB_CONFIGURATION_DESCRIPTOR_ATTR_REQUIRED |                        USB_CONFIGURATION_DESCRIPTOR_ATTR_SELF_POWERED,    max_power:          50};const usbs_enumeration_data usb_enum_data = {    &hellip;    configurations:             &amp;usb_configuration,    &hellip;};</programlisting><para>The values for the <structfield>length</structfield> and<structfield>type</structfield> fields are determined by the standard.The <structfield>total_length</structfield> field depends on thenumber of interfaces and endpoints used by this configuration, andconvenience macros are provided to calculate this: the first argumentto the macros specify the number of interfaces, the second the numberof endpoints. The <structfield>number_interfaces</structfield> fieldis self-explanatory. If the peripheral involves multipleconfigurations then each one must have a unique id, and this will beused in the set-configuration control message. The id<literal>0</literal> is reserved, and a set-configuration controlmessage that uses this id indicates that the peripheral should beinactive. Configurations can have a string description if required.The <structfield>attributes</structfield> field must have the<literal>REQUIRED</literal> bit set; the<literal>SELF_POWERED</literal> bit informs the host that theperipheral has its own power supply and will not draw any power overthe bus, leaving more bus power available to other peripherals; the<literal>REMOTE_WAKEUP</literal> bit is used if the peripheral caninterrupt the host when the latter is in power-saving mode. Forperipherals that are not self-powered, the<structfield>max_power</structfield> field specifies the powerrequirements in units of 2mA.</para></refsect2><refsect2><title><structname>usb_interface_descriptor</structname></title><para>A USB configuration involves one or more interfaces, typicallycorresponding to different streams of data. For example, one interfacemight involve video data while another interface is for audio.Multiple interfaces in a single configuration will be active at thesame time.</para><programlisting width=72>const usb_interface_descriptor usb_interface = {    length:             USB_INTERFACE_DESCRIPTOR_LENGTH,    type:               USB_INTERFACE_DESCRIPTOR_TYPE,    interface_id:       0,    alternate_setting:  0,    number_endpoints:   2,    interface_class:    USB_INTERFACE_DESCRIPTOR_CLASS_VENDOR,    interface_subclass: USB_INTERFACE_DESCRIPTOR_SUBCLASS_VENDOR,    interface_protocol: USB_INTERFACE_DESCRIPTOR_PROTOCOL_VENDOR,    interface_str:      0};const usbs_enumeration_data usb_enum_data = {    &hellip;    total_number_interfaces:    1,    interfaces:                 &amp;usb_interface,    &hellip;};</programlisting><para>Again, the <structfield>length</structfield> and<structfield>type</structfield> fields are specified by the standard.Each interface within a configuration requires its own id. However, agiven interface may have several alternate settings, in other wordsentries in the interfaces array with the same id but different<structfield>alternate_setting</structfield> fields. For example,there might be one setting which requires a bandwidth of 100K/s andanother setting that only needs 50K/s. The host can use the standardset-interface control message to choose the most appropriate setting.The handling of this request is the responsibility of higher-levelcode, so the application may have to <linklinkend="usbs-control-standard">install</link> its own handler.</para><para>The number of endpoints used by an interface is specified in the<structfield>number_endpoints</structfield> field. Exact details ofwhich endpoints are used is held in a separate array of endpointdescriptors. The class, subclass and protocol fields are used byhost-side code to determine which host-side device driver shouldhandle this specific interface. Usually this is determined on aper-peripheral basis in the<structname>usb_device_descriptor</structname> structure, but that candefer the details to individual interfaces. A per-interface string

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -