descriptor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Koen Zandberg <koen@bergzand.net>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for
6  * more details.
7  */
19 #ifndef USB_DESCRIPTOR_H
20 #define USB_DESCRIPTOR_H
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
33 #define USB_SETUP_REQ_GET_STATUS 0x00
34 #define USB_SETUP_REQ_CLEAR_FEATURE 0x01
35 #define USB_SETUP_REQ_SET_FEATURE 0x03
36 #define USB_SETUP_REQ_SET_ADDRESS 0x05
37 #define USB_SETUP_REQ_GET_DESCRIPTOR 0x06
38 #define USB_SETUP_REQ_SET_DESCRIPTOR 0x07
39 #define USB_SETUP_REQ_GET_CONFIGURATION 0x08
40 #define USB_SETUP_REQ_SET_CONFIGURATION 0x09
41 #define USB_SETUP_REQ_GET_INTERFACE 0x0a
42 #define USB_SETUP_REQ_SET_INTERFACE 0x0b
43 #define USB_SETUP_REQ_SYNCH_FRAME 0x0c
50 #define USB_TYPE_DESCRIPTOR_DEVICE 0x01
51 #define USB_TYPE_DESCRIPTOR_CONFIGURATION 0x02
52 #define USB_TYPE_DESCRIPTOR_STRING 0x03
53 #define USB_TYPE_DESCRIPTOR_INTERFACE 0x04
54 #define USB_TYPE_DESCRIPTOR_ENDPOINT 0x05
55 #define USB_TYPE_DESCRIPTOR_DEV_QUALIFIER 0x06
56 #define USB_TYPE_DESCRIPTOR_SPEED_CONFIG 0x07
57 #define USB_TYPE_DESCRIPTOR_IFACE_POWER 0x08
58 #define USB_TYPE_DESCRIPTOR_INTERFACE_ASSOC 0x0b
66 #define USB_CONF_ATTR_RESERVED 0x80
67 #define USB_CONF_ATTR_SELF_POWERED 0x40
68 #define USB_CONF_ATTR_REM_WAKEUP 0x20
75 #define USB_SETUP_REQUEST_DEVICE2HOST 0x80
79 #define USB_SETUP_REQUEST_RECIPIENT_MASK 0x1f
80 #define USB_SETUP_REQUEST_RECIPIENT_DEVICE 0x00
81 #define USB_SETUP_REQUEST_RECIPIENT_INTERFACE 0x01
82 #define USB_SETUP_REQUEST_RECIPIENT_ENDPOINT 0x02
83 #define USB_SETUP_REQUEST_RECIPIENT_OTHER 0x03
85 #define USB_SETUP_REQUEST_TYPE_MASK 0x60
86 #define USB_SETUP_REQUEST_TYPE_STANDARD 0x00
87 #define USB_SETUP_REQUEST_TYPE_CLASS 0x20
88 #define USB_SETUP_REQUEST_TYPE_VENDOR 0x40
94 #define USB_ENDPOINT_DESCRIPTOR_INTERVAL_ISOCHRONOUS 1
95 
100 #define USB_CLASS_AUDIO 0x01
101 #define USB_CLASS_CDC_CONTROL 0x02
102 #define USB_CLASS_HID 0x03
103 #define USB_CLASS_PHYSICAL 0x05
104 #define USB_CLASS_IMAGE 0x06
105 #define USB_CLASS_PRINTER 0x07
106 #define USB_CLASS_MASS_STORAGE 0x08
107 #define USB_CLASS_CDC_DATA 0x0a
108 #define USB_CLASS_VENDOR 0xff
114 typedef struct __attribute__((packed)) {
115  uint8_t length;
116  uint8_t type;
117  uint16_t bcd_usb;
118  uint8_t class;
119  uint8_t subclass;
120  uint8_t protocol;
121  uint8_t max_packet_size;
122  uint16_t vendor_id;
123  uint16_t product_id;
124  uint16_t bcd_device;
126  uint8_t product_idx;
127  uint8_t serial_idx;
130 
134 typedef struct __attribute__((packed)) {
135  uint8_t length;
136  uint8_t type;
137  uint16_t total_length;
138  uint8_t num_interfaces;
139  uint8_t val;
140  uint8_t idx;
141  uint8_t attributes;
142  uint8_t max_power;
144 
148 typedef struct __attribute__((packed)) {
149  uint8_t length;
150  uint8_t type;
151  uint8_t interface_num;
153  uint8_t num_endpoints;
154  uint8_t class;
155  uint8_t subclass;
156  uint8_t protocol;
157  uint8_t idx;
159 
163 typedef struct __attribute__((packed)) {
164  uint8_t length;
165  uint8_t type;
166  uint8_t address;
167  uint8_t attributes;
168  uint16_t max_packet_size;
169  uint8_t interval;
171 
175 typedef struct __attribute__((packed)) {
176  uint8_t length;
177  uint8_t type;
179 
184 typedef struct __attribute__((packed)) {
185  uint8_t length;
186  uint8_t type;
187  uint8_t first_interface;
188  uint8_t interface_count;
189  uint8_t class;
190  uint8_t subclass;
191  uint8_t protocol;
192  uint8_t idx;
194 
198 typedef struct __attribute__((packed)) {
199  uint8_t type;
200  uint8_t request;
201  uint16_t value;
202  uint16_t index;
203  uint16_t length;
204 } usb_setup_t;
205 
214 static inline bool usb_setup_is_read(usb_setup_t *pkt)
215 {
216  return pkt->type & USB_SETUP_REQUEST_DEVICE2HOST;
217 }
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif /* USB_DESCRIPTOR_H */
224 
usb_setup_t::index
uint16_t index
Index/offset word
Definition: descriptor.h:202
usb_descriptor_configuration_t::max_power
uint8_t max_power
Maximum power consumption in 2mA steps
Definition: descriptor.h:142
usb_descriptor_device_t::max_packet_size
uint8_t max_packet_size
EP0 max packet size (8, 16, 32 or 64 bytes)
Definition: descriptor.h:121
usb_setup_t::length
uint16_t length
Length of the data stage.
Definition: descriptor.h:203
usb_setup_t::type
uint8_t type
Request type
Definition: descriptor.h:199
usb_descriptor_interface_association_t::subclass
uint8_t subclass
Subclass code.
Definition: descriptor.h:190
usb_descriptor_configuration_t::val
uint8_t val
Value to select this configuration by
Definition: descriptor.h:139
usb_descriptor_interface_association_t::protocol
uint8_t protocol
Protocol code.
Definition: descriptor.h:191
usb_descriptor_interface_association_t::first_interface
uint8_t first_interface
Index of the first associated interface.
Definition: descriptor.h:187
usb_descriptor_configuration_t::idx
uint8_t idx
Configuration descriptor string index
Definition: descriptor.h:140
usb_descriptor_device_t::bcd_usb
uint16_t bcd_usb
Binary-coded decimal USB release specification
Definition: descriptor.h:117
usb_descriptor_endpoint_t
USB endpoint descriptor (USB 2.0 spec table 9-13)
Definition: descriptor.h:163
usb_descriptor_device_t::manufacturer_idx
uint8_t manufacturer_idx
Manufacturer string index number
Definition: descriptor.h:125
usb_descriptor_configuration_t
USB configuration descriptor (USB 2.0 spec table 9-10)
Definition: descriptor.h:134
usb_descriptor_endpoint_t::type
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_ENDPOINT)
Definition: descriptor.h:165
usb_descriptor_device_t::vendor_id
uint16_t vendor_id
Vendor ID (as assigned by the USB-IF)
Definition: descriptor.h:122
usb_setup_t::request
uint8_t request
Specific request
Definition: descriptor.h:200
usb_setup_t::value
uint16_t value
Value word
Definition: descriptor.h:201
usb_setup_t
USB setup packet (USB 2.0 spec table 9-2)
Definition: descriptor.h:198
usb_setup_is_read
static bool usb_setup_is_read(usb_setup_t *pkt)
getter for setup packet direction
Definition: descriptor.h:214
usb_descriptor_device_t::protocol
uint8_t protocol
Device protocol code
Definition: descriptor.h:120
usb_descriptor_interface_t::protocol
uint8_t protocol
Protocol code
Definition: descriptor.h:156
usb_descriptor_interface_t::type
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_INTERFACE)
Definition: descriptor.h:150
usb_descriptor_endpoint_t::max_packet_size
uint16_t max_packet_size
Maximum packet size of the endpoint
Definition: descriptor.h:168
usb_descriptor_configuration_t::attributes
uint8_t attributes
Configuration attributes (USB_CONF_ATTR)
Definition: descriptor.h:141
usb_descriptor_interface_t::num_endpoints
uint8_t num_endpoints
Number of endpoints used by this interface
Definition: descriptor.h:153
usb_descriptor_configuration_t::length
uint8_t length
Size of this descriptor
Definition: descriptor.h:135
usb_descriptor_device_t
USB device descriptor (USB 2.0 spec table 9-8)
Definition: descriptor.h:114
usb_descriptor_interface_t::interface_num
uint8_t interface_num
Interface number
Definition: descriptor.h:151
usb_descriptor_interface_association_t
USB interface association descriptor (Interface Association Descriptors table 9-Z)
Definition: descriptor.h:184
usb_descriptor_endpoint_t::length
uint8_t length
Size of this descriptor
Definition: descriptor.h:164
usb_descriptor_device_t::type
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_DEVICE)
Definition: descriptor.h:116
usb_descriptor_configuration_t::total_length
uint16_t total_length
Total length of the configuration
Definition: descriptor.h:137
usb_descriptor_interface_t::alternate_setting
uint8_t alternate_setting
Alternate setting index
Definition: descriptor.h:152
usb_descriptor_device_t::product_id
uint16_t product_id
Product ID
Definition: descriptor.h:123
usb_descriptor_interface_association_t::type
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_INTERFACE_ASSOC)
Definition: descriptor.h:186
usb_descriptor_interface_association_t::interface_count
uint8_t interface_count
Number of associated interfaces.
Definition: descriptor.h:188
USB_SETUP_REQUEST_DEVICE2HOST
#define USB_SETUP_REQUEST_DEVICE2HOST
Request direction From device to host if the bit is set
Definition: descriptor.h:75
usb_descriptor_device_t::num_configurations
uint8_t num_configurations
Number of possible configurations
Definition: descriptor.h:128
usb_descriptor_interface_t::length
uint8_t length
Size of this descriptor
Definition: descriptor.h:149
usb_descriptor_device_t::length
uint8_t length
Size of this descriptor.
Definition: descriptor.h:115
usb_descriptor_string_t
USB string descriptor (USB 2.0 spec table 9-16)
Definition: descriptor.h:175
usb_descriptor_device_t::bcd_device
uint16_t bcd_device
Binary-coded decimal device release
Definition: descriptor.h:124
usb_descriptor_string_t::length
uint8_t length
Size of this descriptor.
Definition: descriptor.h:176
usb_descriptor_configuration_t::num_interfaces
uint8_t num_interfaces
Number of interfaces supported by this configuration
Definition: descriptor.h:138
usb_descriptor_device_t::subclass
uint8_t subclass
Device subclass code
Definition: descriptor.h:119
usb_descriptor_interface_t
USB interface descriptor (USB 2.0 spec table 9-12)
Definition: descriptor.h:148
usb_descriptor_interface_t::idx
uint8_t idx
Interface descriptor string index
Definition: descriptor.h:157
usb_descriptor_configuration_t::type
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_CONFIGURATION)
Definition: descriptor.h:136
usb_descriptor_endpoint_t::interval
uint8_t interval
Polling interval for the endpoint
Definition: descriptor.h:169
usb_descriptor_interface_t::subclass
uint8_t subclass
Subclass code as assigned by the USB-IF
Definition: descriptor.h:155
usb_descriptor_interface_association_t::length
uint8_t length
Size of this descriptor.
Definition: descriptor.h:185
usb_descriptor_device_t::serial_idx
uint8_t serial_idx
Device serial number string index number
Definition: descriptor.h:127
usb_descriptor_endpoint_t::attributes
uint8_t attributes
Attributes of the endpoint
Definition: descriptor.h:167
usb_descriptor_device_t::product_idx
uint8_t product_idx
Product string index number
Definition: descriptor.h:126
usb_descriptor_string_t::type
uint8_t type
String descriptor type
Definition: descriptor.h:177
usb_descriptor_endpoint_t::address
uint8_t address
Address of the endpoint
Definition: descriptor.h:166
usb_descriptor_interface_association_t::idx
uint8_t idx
Descriptor string index.
Definition: descriptor.h:192