| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * Standard USB descriptors.
|
|---|
| 34 | */
|
|---|
| 35 | #ifndef LIBUSB_DESCRIPTOR_H_
|
|---|
| 36 | #define LIBUSB_DESCRIPTOR_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <sys/types.h>
|
|---|
| 39 |
|
|---|
| 40 | /** Descriptor type. */
|
|---|
| 41 | typedef enum {
|
|---|
| 42 | USB_DESCTYPE_DEVICE = 1,
|
|---|
| 43 | USB_DESCTYPE_CONFIGURATION = 2,
|
|---|
| 44 | USB_DESCTYPE_STRING = 3,
|
|---|
| 45 | USB_DESCTYPE_INTERFACE = 4,
|
|---|
| 46 | USB_DESCTYPE_ENDPOINT = 5,
|
|---|
| 47 | /* New in USB2.0 */
|
|---|
| 48 | USB_DESCTYPE_DEVICE_QUALIFIER = 6,
|
|---|
| 49 | USB_DESCTYPE_OTHER_SPEED_CONFIGURATION = 7,
|
|---|
| 50 | USB_DESCTYPE_INTERFACE_POWER = 8,
|
|---|
| 51 | /* Class specific */
|
|---|
| 52 | USB_DESCTYPE_HID = 0x21,
|
|---|
| 53 | USB_DESCTYPE_HID_REPORT = 0x22,
|
|---|
| 54 | USB_DESCTYPE_HID_PHYSICAL = 0x23,
|
|---|
| 55 | USB_DESCTYPE_HUB = 0x29,
|
|---|
| 56 | /* USB_DESCTYPE_ = */
|
|---|
| 57 | } usb_descriptor_type_t;
|
|---|
| 58 |
|
|---|
| 59 | /** Standard USB device descriptor.
|
|---|
| 60 | */
|
|---|
| 61 | typedef struct {
|
|---|
| 62 | /** Size of this descriptor in bytes. */
|
|---|
| 63 | uint8_t length;
|
|---|
| 64 | /** Descriptor type (USB_DESCTYPE_DEVICE). */
|
|---|
| 65 | uint8_t descriptor_type;
|
|---|
| 66 | /** USB specification release number.
|
|---|
| 67 | * The number shall be coded as binary-coded decimal (BCD).
|
|---|
| 68 | */
|
|---|
| 69 | uint16_t usb_spec_version;
|
|---|
| 70 | /** Device class. */
|
|---|
| 71 | uint8_t device_class;
|
|---|
| 72 | /** Device sub-class. */
|
|---|
| 73 | uint8_t device_subclass;
|
|---|
| 74 | /** Device protocol. */
|
|---|
| 75 | uint8_t device_protocol;
|
|---|
| 76 | /** Maximum packet size for endpoint zero.
|
|---|
| 77 | * Valid values are only 8, 16, 32, 64).
|
|---|
| 78 | */
|
|---|
| 79 | uint8_t max_packet_size;
|
|---|
| 80 | /** Vendor ID. */
|
|---|
| 81 | uint16_t vendor_id;
|
|---|
| 82 | /** Product ID. */
|
|---|
| 83 | uint16_t product_id;
|
|---|
| 84 | /** Device release number (in BCD). */
|
|---|
| 85 | uint16_t device_version;
|
|---|
| 86 | /** Manufacturer descriptor index. */
|
|---|
| 87 | uint8_t str_manufacturer;
|
|---|
| 88 | /** Product descriptor index. */
|
|---|
| 89 | uint8_t str_product;
|
|---|
| 90 | /** Device serial number descriptor index. */
|
|---|
| 91 | uint8_t str_serial_number;
|
|---|
| 92 | /** Number of possible configurations. */
|
|---|
| 93 | uint8_t configuration_count;
|
|---|
| 94 | } __attribute__ ((packed)) usb_standard_device_descriptor_t;
|
|---|
| 95 |
|
|---|
| 96 | /** USB device qualifier decriptor is basically a cut down version of the device
|
|---|
| 97 | * descriptor with values that would be valid if the device operated on the
|
|---|
| 98 | * other speed (HIGH vs. FULL)
|
|---|
| 99 | */
|
|---|
| 100 | typedef struct {
|
|---|
| 101 | /** Size of this descriptor in bytes */
|
|---|
| 102 | uint8_t length;
|
|---|
| 103 | /** Descriptor type (USB_DESCTYPE_DEVICE_QUALIFIER) */
|
|---|
| 104 | uint8_t descriptor_type;
|
|---|
| 105 | /** USB specification release number.
|
|---|
| 106 | * The number shall be coded as binary-coded decimal (BCD).
|
|---|
| 107 | */
|
|---|
| 108 | uint16_t usb_spec_version;
|
|---|
| 109 | /** Device class. */
|
|---|
| 110 | uint8_t device_class;
|
|---|
| 111 | /** Device sub-class. */
|
|---|
| 112 | uint8_t device_subclass;
|
|---|
| 113 | /** Device protocol. */
|
|---|
| 114 | uint8_t device_protocol;
|
|---|
| 115 | /** Maximum packet size for endpoint zero.
|
|---|
| 116 | * Valid values are only 8, 16, 32, 64).
|
|---|
| 117 | */
|
|---|
| 118 | uint8_t max_packet_size;
|
|---|
| 119 | /** Number of possible configurations. */
|
|---|
| 120 | uint8_t configuration_count;
|
|---|
| 121 | uint8_t reserved;
|
|---|
| 122 | } __attribute__ ((packed)) usb_standard_device_qualifier_descriptor_t;
|
|---|
| 123 |
|
|---|
| 124 | /** Standard USB configuration descriptor.
|
|---|
| 125 | */
|
|---|
| 126 | typedef struct {
|
|---|
| 127 | /** Size of this descriptor in bytes. */
|
|---|
| 128 | uint8_t length;
|
|---|
| 129 | /** Descriptor type (USB_DESCTYPE_CONFIGURATION). */
|
|---|
| 130 | uint8_t descriptor_type;
|
|---|
| 131 | /** Total length of all data of this configuration.
|
|---|
| 132 | * This includes the combined length of all descriptors
|
|---|
| 133 | * (configuration, interface, endpoint, class-specific and
|
|---|
| 134 | * vendor-specific) valid for this configuration.
|
|---|
| 135 | */
|
|---|
| 136 | uint16_t total_length;
|
|---|
| 137 | /** Number of possible interfaces under this configuration. */
|
|---|
| 138 | uint8_t interface_count;
|
|---|
| 139 | /** Configuration value used when setting this configuration. */
|
|---|
| 140 | uint8_t configuration_number;
|
|---|
| 141 | /** String descriptor describing this configuration. */
|
|---|
| 142 | uint8_t str_configuration;
|
|---|
| 143 | /** Attribute bitmap. */
|
|---|
| 144 | uint8_t attributes;
|
|---|
| 145 | /** Maximum power consumption from the USB under this configuration.
|
|---|
| 146 | * Expressed in 2mA unit (e.g. 50 ~ 100mA).
|
|---|
| 147 | */
|
|---|
| 148 | uint8_t max_power;
|
|---|
| 149 | } __attribute__ ((packed)) usb_standard_configuration_descriptor_t;
|
|---|
| 150 |
|
|---|
| 151 | /** USB Other Speed Configuration descriptor shows values that would change
|
|---|
| 152 | * in the configuration descriptor if the device operated at its other
|
|---|
| 153 | * possible speed (HIGH vs. FULL)
|
|---|
| 154 | */
|
|---|
| 155 | typedef usb_standard_configuration_descriptor_t
|
|---|
| 156 | usb_other_speed_configuration_descriptor_t;
|
|---|
| 157 |
|
|---|
| 158 | /** Standard USB interface descriptor.
|
|---|
| 159 | */
|
|---|
| 160 | typedef struct {
|
|---|
| 161 | /** Size of this descriptor in bytes. */
|
|---|
| 162 | uint8_t length;
|
|---|
| 163 | /** Descriptor type (USB_DESCTYPE_INTERFACE). */
|
|---|
| 164 | uint8_t descriptor_type;
|
|---|
| 165 | /** Number of interface.
|
|---|
| 166 | * Zero-based index into array of interfaces for current configuration.
|
|---|
| 167 | */
|
|---|
| 168 | uint8_t interface_number;
|
|---|
| 169 | /** Alternate setting for value in interface_number. */
|
|---|
| 170 | uint8_t alternate_setting;
|
|---|
| 171 | /** Number of endpoints used by this interface.
|
|---|
| 172 | * This number must exclude usage of endpoint zero
|
|---|
| 173 | * (default control pipe).
|
|---|
| 174 | */
|
|---|
| 175 | uint8_t endpoint_count;
|
|---|
| 176 | /** Class code. */
|
|---|
| 177 | uint8_t interface_class;
|
|---|
| 178 | /** Subclass code. */
|
|---|
| 179 | uint8_t interface_subclass;
|
|---|
| 180 | /** Protocol code. */
|
|---|
| 181 | uint8_t interface_protocol;
|
|---|
| 182 | /** String descriptor describing this interface. */
|
|---|
| 183 | uint8_t str_interface;
|
|---|
| 184 | } __attribute__ ((packed)) usb_standard_interface_descriptor_t;
|
|---|
| 185 |
|
|---|
| 186 | /** Standard USB endpoint descriptor.
|
|---|
| 187 | */
|
|---|
| 188 | typedef struct {
|
|---|
| 189 | /** Size of this descriptor in bytes. */
|
|---|
| 190 | uint8_t length;
|
|---|
| 191 | /** Descriptor type (USB_DESCTYPE_ENDPOINT). */
|
|---|
| 192 | uint8_t descriptor_type;
|
|---|
| 193 | /** Endpoint address together with data flow direction. */
|
|---|
| 194 | uint8_t endpoint_address;
|
|---|
| 195 | /** Endpoint attributes.
|
|---|
| 196 | * Includes transfer type (usb_transfer_type_t).
|
|---|
| 197 | */
|
|---|
| 198 | uint8_t attributes;
|
|---|
| 199 | /** Maximum packet size.
|
|---|
| 200 | * Lower 10 bits represent the actuall size
|
|---|
| 201 | * Bits 11,12 specify addtional transfer opportunitities for
|
|---|
| 202 | * HS INT and ISO transfers. */
|
|---|
| 203 | uint16_t max_packet_size;
|
|---|
| 204 | #define ED_MPS_PACKET_SIZE_MASK 0x3ff
|
|---|
| 205 | #define ED_MPS_PACKET_SIZE_GET(value) \
|
|---|
| 206 | ((value) & ED_MPS_PACKET_SIZE_MASK)
|
|---|
| 207 | #define ED_MPS_TRANS_OPPORTUNITIES_GET(value) \
|
|---|
| 208 | ((((value) >> 10) & 0x3) + 1)
|
|---|
| 209 | /** Polling interval in milliseconds.
|
|---|
| 210 | * Ignored for bulk and control endpoints.
|
|---|
| 211 | * Isochronous endpoints must use value 1.
|
|---|
| 212 | * Interrupt endpoints any value from 1 to 255.
|
|---|
| 213 | */
|
|---|
| 214 | uint8_t poll_interval;
|
|---|
| 215 | } __attribute__ ((packed)) usb_standard_endpoint_descriptor_t;
|
|---|
| 216 |
|
|---|
| 217 | /** Part of standard USB HID descriptor specifying one class descriptor.
|
|---|
| 218 | *
|
|---|
| 219 | * (See HID Specification, p.22)
|
|---|
| 220 | */
|
|---|
| 221 | typedef struct {
|
|---|
| 222 | /** Type of class-specific descriptor (Report or Physical). */
|
|---|
| 223 | uint8_t type;
|
|---|
| 224 | /** Length of class-specific descriptor in bytes. */
|
|---|
| 225 | uint16_t length;
|
|---|
| 226 | } __attribute__ ((packed)) usb_standard_hid_class_descriptor_info_t;
|
|---|
| 227 |
|
|---|
| 228 | /** Standard USB HID descriptor.
|
|---|
| 229 | *
|
|---|
| 230 | * (See HID Specification, p.22)
|
|---|
| 231 | *
|
|---|
| 232 | * It is actually only the "header" of the descriptor, it does not contain
|
|---|
| 233 | * the last two mandatory fields (type and length of the first class-specific
|
|---|
| 234 | * descriptor).
|
|---|
| 235 | */
|
|---|
| 236 | typedef struct {
|
|---|
| 237 | /** Total size of this descriptor in bytes.
|
|---|
| 238 | *
|
|---|
| 239 | * This includes all class-specific descriptor info - type + length
|
|---|
| 240 | * for each descriptor.
|
|---|
| 241 | */
|
|---|
| 242 | uint8_t length;
|
|---|
| 243 | /** Descriptor type (USB_DESCTYPE_HID). */
|
|---|
| 244 | uint8_t descriptor_type;
|
|---|
| 245 | /** HID Class Specification release. */
|
|---|
| 246 | uint16_t spec_release;
|
|---|
| 247 | /** Country code of localized hardware. */
|
|---|
| 248 | uint8_t country_code;
|
|---|
| 249 | /** Total number of class-specific (i.e. Report and Physical)
|
|---|
| 250 | * descriptors.
|
|---|
| 251 | *
|
|---|
| 252 | * @note There is always only one Report descriptor.
|
|---|
| 253 | */
|
|---|
| 254 | uint8_t class_desc_count;
|
|---|
| 255 | /** First mandatory class descriptor (Report) info. */
|
|---|
| 256 | usb_standard_hid_class_descriptor_info_t report_desc_info;
|
|---|
| 257 | } __attribute__ ((packed)) usb_standard_hid_descriptor_t;
|
|---|
| 258 |
|
|---|
| 259 | #endif
|
|---|
| 260 | /**
|
|---|
| 261 | * @}
|
|---|
| 262 | */
|
|---|