Выбрать главу

Figure 8.5: Data packet

Figure 8.6 shows the format of a handshake packet, which is used for ACK, NAK, STALL, and NYET. ACK is used when a receiver acknowledges that it has received an error-free data packet. NAK is used when the receiving device cannot accept the packet. STALL indicates when the endpoint is halted, and NYET is used when there is no response from the receiver.

Figure 8.6: Handshake packet

8.3.2 Data Flow Types

Data can be transferred on a USB bus in four ways: bulk transfer, interrupt transfer, isochronous transfer, and control transfer.

Bulk transfers are designed to transfer large amounts of data with error-free delivery and no guarantee of bandwidth. If an OUT endpoint is defined as using bulk transfers, then the host will transfer data to it using OUT transactions. Similarly, if an IN endpoint is defined as using bulk transfers, then the host will transfer data from it using IN transactions. In general, bulk transfers are used where a slow rate of transfer is not a problem. The maximum packet size in a bulk transfer is 8 to 64 packets at full speed, and 512 packets at high speed (bulk transfers are not allowed at low speeds).

Interrupt transfers are used to transfer small amounts of data with a high bandwidth where the data must be transferred as quickly as possible with no delay. Note that interrupt transfers have nothing to do with interrupts in computer systems. Interrupt packets can range in size from 1 to 8 bytes at low speed, from 1 to 64 bytes at full speed, and up to 1024 bytes at high speed.

Isochronous transfers have a guaranteed bandwidth, but error-free delivery is not guaranteed. This type of transfer is generally used in applications, such as audio data transfer, where speed is important but the loss or corruption of some data is not. An isochronous packet may contain 1023 bytes at full speed or up to 1024 bytes at high speed (isochronous transfers are not allowed at low speeds).

A control transfer is a bidirectional data transfer, using both IN and OUT endpoints. Control transfers are generally used for initial configuration of a device by the host. The maximum packet size is 8 bytes at low speed, 8 to 64 bytes at full speed, and 64 bytes at high speed. A control transfer is carried out in three stages: SETUP, DATA, and STATUS.

8.3.3 Enumeration

When a device is plugged into a USB bus, it becomes known to the host through a process called enumeration. The steps of enumeration are:

• When a device is plugged in, the host becomes aware of it because one of the data lines (D+ or D–) becomes logic high.

• The host sends a USB reset signal to the device to place the device in a known state. The reset device responds to address 0.

• The host sends a request on address 0 to the device to find out its maximum packet size using a Get Descriptor command.

• The device responds by sending a small portion of the device descriptor.

• The host sends a USB reset again.

• The host assigns a unique address to the device and sends a Set Address request to the device. After the request is completed, the device assumes the new address. At this point the host is free to reset any other newly plugged-in devices on the bus.

• The host sends a Get Device Descriptor request to retrieve the complete device descriptor, gathering information such as manufacturer, type of device, and maximum control packet size.

• The host sends a Get Configuration Descriptors request to receive the device’s configuration data, such as power requirements and the types and number of interfaces supported.

• The host may request any additional descriptors from the device.

The initial communication between the host and the device is carried out using the control transfer type of data flow.

Initially, the device is addressed, but it is in an unconfigured state. After the host gathers enough information about the device, it loads a suitable device driver which configures the device by sending it a Set Configuration request. At this point the device has been configured, and it is ready to respond to device-specific requests (i.e., it can receive data from and send data to the host).

8.4 Descriptors

All USB devices have a hierarchy of descriptors that describe various features of the device: the manufacturer ID, the version of the device, the version of USB it supports, what the device is, its power requirements, the number and type of endpoints, and so forth.

The most common USB descriptors are:

• Device descriptors

• Configuration descriptors

• Interface descriptors

• HID descriptors

• Endpoint descriptors

The descriptors are in a hierarchical structure as shown in Figure 8.7. At the top of the hierarchy we have the device descriptor, then the configuration descriptors, followed by the interface descriptors, and finally the endpoint descriptors. The HID descriptor always follows the interface descriptor when the interface belongs to the HID class.

Figure 8.7: USB descriptor hierarchy

All descriptors have a common format. The first byte (bLength) specifies the length of the descriptor, while the second byte (bDescriptorType) indicates the descriptor type.

8.4.1 Device Descriptors

The device descriptor is the top-level set of information read from a device and the first item the host attempts to retrieve.

A USB device has only one device descriptor, since the device descriptor represents the entire device. It provides general information such as manufacturer, serial number, product number, the class of the device, and the number of configurations. Table 8.5 shows the format for a device descriptor with the meaning of each field.

 bLength is the length of the device descriptor.

 bDescriptorType is the descriptor type.

 bcdUSB reports the highest version of USB the device supports in BCD format. The number is represented as 0xJJMN, where JJ is the major version number, M is the minor version number, and N is the subminor version number. For example, USB 1.1 is reported as 0x0110.

 bDeviceClass, bDeviceSubClass, and bDeviceProtocol are assigned by the USB organization and are used by the system to find a class driver for the device.

 bMaxPacketSize0 is the maximum input and output packet size for endpoint 0.

 idVendor is assigned by the USB organization and is the vendor’s ID.

 idProduct is assigned by the manufacturer and is the product ID.

 bcdDevice is the device release number and has the same format as the bcdUSB.

 iManufacturer, iProduct, and iSerialNumber are details about the manufacturer and the product. These fields have no requirement and can be set to zero.

 bNumConfigurations is the number of configurations the device supports.

Table 8.5: Device descriptor

Offset Field Size Description
0 bLength 1 Descriptor size in bytes
1 bDescriptorType 1 Device descriptor (0x01)
2 bcdUSB 2 Highest version of USB supported
4 bDeviceClass 1 Class code
5 bDeviceSubClass 1 Subclass code
6 bDeviceProtocol 1 Protocol code
7 bMaxPacketSize0 1 Maximum packet size
8 idVendor 2 Vendor ID
10 idProduct 2 Product ID
12 bcdDevice 2 Device release number
14 iManufacturer 1 Manufacturer string descriptor
15 iProduct 1 Index of product string descriptor
16 iSerialNumber 1 Index of serial number descriptor
17 bNumConfigurations 1 Number of possible configurations