1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
12 >USB Enumeration Data</TITLE
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="eCos USB Slave Support"
23 HREF="io-usb-slave.html"><LINK
26 HREF="usbs-intro.html"><LINK
28 TITLE="Starting up a USB Device"
29 HREF="usbs-start.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="usbs-intro.html"
71 HREF="usbs-start.html"
82 NAME="USBS-ENUM">USB Enumeration Data</H1
90 >Enumeration Data -- The USB enumeration data structures</DIV
92 CLASS="REFSYNOPSISDIV"
104 >#include <cyg/io/usb/usb.h>
105 #include <cyg/io/usb/usbs.h>
107 typedef struct usb_device_descriptor {
109 } usb_device_descriptor __attribute__((packed));
111 typedef struct usb_configuration_descriptor {
113 } usb_configuration_descriptor __attribute__((packed));
115 typedef struct usb_interface_descriptor {
117 } usb_interface_descriptor __attribute__((packed));
119 typedef struct usb_endpoint_descriptor {
121 } usb_endpoint_descriptor;
123 typedef struct usbs_enumeration_data {
124 usb_device_descriptor device;
125 int total_number_interfaces;
126 int total_number_endpoints;
127 int total_number_strings;
128 const usb_configuration_descriptor* configurations;
129 const usb_interface_descriptor* interfaces;
130 const usb_endpoint_descriptor* endpoints;
131 const unsigned char** strings;
132 } usbs_enumeration_data;</PRE
143 >USB Enumeration Data</H2
145 >When a USB host detects that a peripheral has been plugged in or
146 powered up, one of the first steps is to ask the peripheral to
147 describe itself by supplying enumeration data. Some of this data
148 depends on the class of peripheral. Other fields are vendor-specific.
149 There is also a dependency on the hardware, specifically which
150 endpoints are available should be used. In general it is not possible
151 for generic code to provide this information, so it is the
152 responsibility of application code to provide a suitable
155 >usbs_enumeration_data</SPAN
157 install it in the endpoint 0 data structure during initialization.
158 This must happen before the USB device is enabled by a call to
170 CLASS="PROGRAMLISTING"
171 >const usbs_enumeration_data usb_enum_data = {
176 main(int argc, char** argv)
178 usbs_sa11x0_ep0.enumeration_data = &usb_enum_data;
180 usbs_start(&usbs_sa11x0_ep0);
187 >For most applications the enumeration data will be static, although
190 >usbs_enumeration_data</SPAN
192 filled in at run-time if necessary. Full details of the enumeration
193 data can be found in the Universal Serial Bus specification obtainable
195 HREF="http://www.usb.org/"
197 >USB Implementers Forum web
199 >, although the meaning of most fields is fairly obvious.
200 The various data structures and utility macros are defined in the
203 >cyg/io/usb/usb.h</TT
207 >cyg/io/usb/usbs.h</TT
209 that the example code below makes use of the gcc labelled element
219 >usb_device_descriptor</SPAN
222 >The main information about a USB peripheral comes from a single
225 >usb_device_descriptor</SPAN
226 > structure, which is
227 embedded in the <SPAN
229 >usbs_enumeration_data</SPAN
231 structure. A typical example might look like this:</P
239 CLASS="PROGRAMLISTING"
240 >const usbs_enumeration_data usb_enum_data = {
242 length: USB_DEVICE_DESCRIPTOR_LENGTH,
243 type: USB_DEVICE_DESCRIPTOR_TYPE,
244 usb_spec_lo: USB_DEVICE_DESCRIPTOR_USB11_LO,
245 usb_spec_hi: USB_DEVICE_DESCRIPTOR_USB11_HI,
246 device_class: USB_DEVICE_DESCRIPTOR_CLASS_VENDOR,
247 device_subclass: USB_DEVICE_DESCRIPTOR_SUBCLASS_VENDOR,
248 device_protocol: USB_DEVICE_DESCRIPTOR_PROTOCOL_VENDOR,
258 serial_number_str: 0,
259 number_configurations: 1
267 >The length and type fields are specified by the USB standard. The
279 > fields identify the particular
280 revision of the standard that the peripheral implements, for example
283 >The device class, subclass, and protocol fields are used by generic
284 host-side USB software to determine which host-side device driver
285 should be loaded to interact with the peripheral. A number of standard
286 classes are defined, for example mass-storage devices and
287 human-interface devices. If a peripheral implements one of the
288 standard classes then a standard existing host-side device driver may
289 exist, eliminating the need to write a custom driver. The value
297 peripherals that implement a vendor-specific protocol rather than a
298 standard one. Such peripherals will require a custom host-side device
299 driver. The value <TT
306 >) is reserved and indicates that the
307 protocol used by the peripheral is defined at the interface level
308 rather than for the peripheral as a whole.</P
315 > field specifies the
316 maximum length of a control message. There is a lower bound of eight
317 bytes, and typical hardware will not support anything larger because
318 control messages are usually small and not performance-critical.</P
331 > fields specify a vendor id, which
332 must be obtained from the USB Implementor's Forum. The numbers used in
333 the code fragment above are examples only and must not be used in real
334 USB peripherals. The product identifier is determined by the vendor,
335 and different USB peripherals should use different identifiers. The
336 device identifier field should indicate a release number in
337 binary-coded decimal.</P
339 >The above fields are all numerical in nature. A USB peripheral can
340 also provide a number of strings as described <A
341 HREF="usbs-enum.html#AEN16196"
343 >, for example the name of the
344 vendor can be provided. The various <TT
350 fields act as indices into an array of strings, with index 0
351 indicating that no string is available. </P
353 >A typical USB peripheral involves just a single configuration. However
354 more complicated peripherals can support multiple configurations. Only
355 one configuration will be active at any one time, and the host will
356 switch between them as appropriate. If a peripheral does involve
357 multiple configurations then typically it will be the responsibility
358 of application code to <A
359 HREF="usbs-control.html#AEN16582"
362 set-configuration control message.</P
372 >usb_configuration_descriptor</SPAN
375 >A USB peripheral involves at least one and possible several different
376 configurations. The <SPAN
378 >usbs_enumeration_data</SPAN
380 structure requires a pointer to an array, possibly of length 1, of
383 >usb_configuration_descriptor</SPAN
385 Usually a single structure suffices:</P
393 CLASS="PROGRAMLISTING"
394 >const usb_configuration_descriptor usb_configuration = {
395 length: USB_CONFIGURATION_DESCRIPTOR_LENGTH,
396 type: USB_CONFIGURATION_DESCRIPTOR_TYPE,
397 total_length_lo: USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_LO(1, 2),
398 total_length_hi: USB_CONFIGURATION_DESCRIPTOR_TOTAL_LENGTH_HI(1, 2),
399 number_interfaces: 1,
401 configuration_str: 0,
402 attributes: USB_CONFIGURATION_DESCRIPTOR_ATTR_REQUIRED |
403 USB_CONFIGURATION_DESCRIPTOR_ATTR_SELF_POWERED,
407 const usbs_enumeration_data usb_enum_data = {
409 configurations: &usb_configuration,
416 >The values for the <TT
427 > fields are determined by the standard.
433 > field depends on the
434 number of interfaces and endpoints used by this configuration, and
435 convenience macros are provided to calculate this: the first argument
436 to the macros specify the number of interfaces, the second the number
437 of endpoints. The <TT
440 >number_interfaces</I
443 is self-explanatory. If the peripheral involves multiple
444 configurations then each one must have a unique id, and this will be
445 used in the set-configuration control message. The id
449 > is reserved, and a set-configuration control
450 message that uses this id indicates that the peripheral should be
451 inactive. Configurations can have a string description if required.
457 > field must have the
465 > bit informs the host that the
466 peripheral has its own power supply and will not draw any power over
467 the bus, leaving more bus power available to other peripherals; the
471 > bit is used if the peripheral can
472 interrupt the host when the latter is in power-saving mode. For
473 peripherals that are not self-powered, the
479 > field specifies the power
480 requirements in units of 2mA.</P
490 >usb_interface_descriptor</SPAN
493 >A USB configuration involves one or more interfaces, typically
494 corresponding to different streams of data. For example, one interface
495 might involve video data while another interface is for audio.
496 Multiple interfaces in a single configuration will be active at the
505 CLASS="PROGRAMLISTING"
506 >const usb_interface_descriptor usb_interface = {
507 length: USB_INTERFACE_DESCRIPTOR_LENGTH,
508 type: USB_INTERFACE_DESCRIPTOR_TYPE,
510 alternate_setting: 0,
512 interface_class: USB_INTERFACE_DESCRIPTOR_CLASS_VENDOR,
513 interface_subclass: USB_INTERFACE_DESCRIPTOR_SUBCLASS_VENDOR,
514 interface_protocol: USB_INTERFACE_DESCRIPTOR_PROTOCOL_VENDOR,
518 const usbs_enumeration_data usb_enum_data = {
520 total_number_interfaces: 1,
521 interfaces: &usb_interface,
539 > fields are specified by the standard.
540 Each interface within a configuration requires its own id. However, a
541 given interface may have several alternate settings, in other words
542 entries in the interfaces array with the same id but different
546 >alternate_setting</I
548 > fields. For example,
549 there might be one setting which requires a bandwidth of 100K/s and
550 another setting that only needs 50K/s. The host can use the standard
551 set-interface control message to choose the most appropriate setting.
552 The handling of this request is the responsibility of higher-level
553 code, so the application may have to <A
554 HREF="usbs-control.html#AEN16582"
556 > its own handler.</P
558 >The number of endpoints used by an interface is specified in the
564 > field. Exact details of
565 which endpoints are used is held in a separate array of endpoint
566 descriptors. The class, subclass and protocol fields are used by
567 host-side code to determine which host-side device driver should
568 handle this specific interface. Usually this is determined on a
569 per-peripheral basis in the
572 >usb_device_descriptor</SPAN
573 > structure, but that can
574 defer the details to individual interfaces. A per-interface string
575 is allowed as well.</P
577 >For USB peripherals involving multiple configurations, the array of
580 >usb_interface_descriptor</SPAN
582 first contain all the interfaces for the first configuration, then all
583 the interfaces for the second configuration, and so on.</P
593 >usb_endpoint_descriptor</SPAN
596 >The host also needs information about which endpoint should be used
597 for what. This involves an array of endpoint descriptors:</P
605 CLASS="PROGRAMLISTING"
606 >const usb_endpoint_descriptor usb_endpoints[] = {
608 length: USB_ENDPOINT_DESCRIPTOR_LENGTH,
609 type: USB_ENDPOINT_DESCRIPTOR_TYPE,
610 endpoint: USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT | 1,
611 attributes: USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
617 length: USB_ENDPOINT_DESCRIPTOR_LENGTH,
618 type: USB_ENDPOINT_DESCRIPTOR_TYPE,
619 endpoint: USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN | 2,
620 attributes: USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
627 const usbs_enumeration_data usb_enum_data = {
629 total_number_endpoints: 2,
630 endpoints: usb_endpoints,
637 >As usual the values for the <TT
648 > fields are specified by the standard.
654 > field gives both the endpoint
655 number and the direction, so in the above example endpoint 1 is used
656 for OUT (host to peripheral) transfers and endpoint 2 is used for IN
657 (peripheral to host) transfers. The
663 > field indicates the USB protocol
664 that should be used on this endpoint: <TT
684 > field specifies the maximum size
685 of a single USB packet. For bulk transfers this will typically be 64
686 bytes. For isochronous transfers this can be up to 1023 bytes. For
687 interrupt transfers it can be up to 64 bytes, although usually a
688 smaller value will be used. The <TT
694 field is ignored for control and bulk transfers. For isochronous
695 transfers it should be set to 1. For interrupt transfers it can be a
696 value between 1 and 255, and indicates the number of milliseconds
697 between successive polling operations.</P
699 >For USB peripherals involving multiple configurations or interfaces
700 the array of endpoint descriptors should be organized sequentially:
701 first the endpoints corresponding to the first interface of the first
702 configuration, then the second interface in that configuration, and so
703 on; then all the endpoints for all the interfaces in the second
704 configuration; etc.</P
714 >The enumeration data can contain a number of strings with additional
715 information. Unicode encoding is used for the strings, and it is
716 possible for a peripheral to supply a given string in multiple
717 languages using the appropriate characters. The first two bytes of
718 each string give a length and type field. The first string is special;
719 after the two bytes header it consists of an array of 2-byte language
720 id codes, indicating the supported languages. The language code
721 0x0409 corresponds to English (United States). </P
729 CLASS="PROGRAMLISTING"
730 >const unsigned char* usb_strings[] = {
732 "\020\003R\000e\000d\000 \000H\000a\000t\000"
735 const usbs_enumeration_data usb_enum_data = {
737 total_number_strings: 2,
738 strings: usb_strings,
745 >The default handler for standard control messages assumes that the
746 peripheral only uses a single language. If this is not the case then
747 higher-level code will have to handle the standard get-descriptor
748 control messages when a string descriptor is requested.</P
758 >usbs_enumeration_data</SPAN
763 >usbs_enumeration_data</SPAN
765 collects together all the various descriptors that make up the
766 enumeration data. It is the responsibility of application code to
767 supply a suitable data structure and install it in the control
774 the USB device is started.</P
782 SUMMARY="Footer navigation table"
793 HREF="usbs-intro.html"
811 HREF="usbs-start.html"
827 HREF="io-usb-slave.html"
835 >Starting up a USB Device</TD