descriptor.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Koen Zandberg <koen@bergzand.net>
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
20 #include <stdint.h>
21 #include <stdbool.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
31 #define USB_SETUP_REQ_GET_STATUS 0x00
32 #define USB_SETUP_REQ_CLEAR_FEATURE 0x01
33 #define USB_SETUP_REQ_SET_FEATURE 0x03
34 #define USB_SETUP_REQ_SET_ADDRESS 0x05
35 #define USB_SETUP_REQ_GET_DESCRIPTOR 0x06
36 #define USB_SETUP_REQ_SET_DESCRIPTOR 0x07
37 #define USB_SETUP_REQ_GET_CONFIGURATION 0x08
38 #define USB_SETUP_REQ_SET_CONFIGURATION 0x09
39 #define USB_SETUP_REQ_GET_INTERFACE 0x0a
40 #define USB_SETUP_REQ_SET_INTERFACE 0x0b
41 #define USB_SETUP_REQ_SYNCH_FRAME 0x0c
48 #define USB_TYPE_DESCRIPTOR_DEVICE 0x01
49 #define USB_TYPE_DESCRIPTOR_CONFIGURATION 0x02
50 #define USB_TYPE_DESCRIPTOR_STRING 0x03
51 #define USB_TYPE_DESCRIPTOR_INTERFACE 0x04
52 #define USB_TYPE_DESCRIPTOR_ENDPOINT 0x05
53 #define USB_TYPE_DESCRIPTOR_DEV_QUALIFIER 0x06
54 #define USB_TYPE_DESCRIPTOR_SPEED_CONFIG 0x07
55 #define USB_TYPE_DESCRIPTOR_IFACE_POWER 0x08
56 #define USB_TYPE_DESCRIPTOR_INTERFACE_ASSOC 0x0b
63 #define USB_FEATURE_ENDPOINT_HALT 0x00
64 #define USB_FEATURE_DEVICE_REMOTE_WAKEUP 0x01
65 #define USB_FEATURE_TEST_MODE 0x02
73 #define USB_CONF_ATTR_RESERVED 0x80
74 #define USB_CONF_ATTR_SELF_POWERED 0x40
75 #define USB_CONF_ATTR_REM_WAKEUP 0x20
82 #define USB_SETUP_REQUEST_DEVICE2HOST 0x80
86 #define USB_SETUP_REQUEST_RECIPIENT_MASK 0x1f
87 #define USB_SETUP_REQUEST_RECIPIENT_DEVICE 0x00
88 #define USB_SETUP_REQUEST_RECIPIENT_INTERFACE 0x01
89 #define USB_SETUP_REQUEST_RECIPIENT_ENDPOINT 0x02
90 #define USB_SETUP_REQUEST_RECIPIENT_OTHER 0x03
92 #define USB_SETUP_REQUEST_TYPE_MASK 0x60
93 #define USB_SETUP_REQUEST_TYPE_STANDARD 0x00
94 #define USB_SETUP_REQUEST_TYPE_CLASS 0x20
95 #define USB_SETUP_REQUEST_TYPE_VENDOR 0x40
101 #define USB_ENDPOINT_DESCRIPTOR_INTERVAL_ISOCHRONOUS 1
102 
107 #define USB_CLASS_AUDIO 0x01
108 #define USB_CLASS_CDC_CONTROL 0x02
109 #define USB_CLASS_HID 0x03
110 #define USB_CLASS_PHYSICAL 0x05
111 #define USB_CLASS_IMAGE 0x06
112 #define USB_CLASS_PRINTER 0x07
113 #define USB_CLASS_MASS_STORAGE 0x08
114 #define USB_CLASS_CDC_DATA 0x0a
115 #define USB_CLASS_VENDOR 0xff
121 typedef struct __attribute__((packed)) {
122  uint8_t length;
123  uint8_t type;
124  uint16_t bcd_usb;
125  uint8_t class;
126  uint8_t subclass;
127  uint8_t protocol;
128  uint8_t max_packet_size;
129  uint16_t vendor_id;
130  uint16_t product_id;
131  uint16_t bcd_device;
133  uint8_t product_idx;
134  uint8_t serial_idx;
137 
141 typedef struct __attribute__((packed)) {
142  uint8_t length;
143  uint8_t type;
144  uint16_t total_length;
145  uint8_t num_interfaces;
146  uint8_t val;
147  uint8_t idx;
148  uint8_t attributes;
149  uint8_t max_power;
151 
155 typedef struct __attribute__((packed)) {
156  uint8_t length;
157  uint8_t type;
158  uint8_t interface_num;
160  uint8_t num_endpoints;
161  uint8_t class;
162  uint8_t subclass;
163  uint8_t protocol;
164  uint8_t idx;
166 
170 typedef struct __attribute__((packed)) {
171  uint8_t length;
172  uint8_t type;
173  uint8_t address;
174  uint8_t attributes;
175  uint16_t max_packet_size;
176  uint8_t interval;
178 
182 typedef struct __attribute__((packed)) {
183  uint8_t length;
184  uint8_t type;
186 
191 typedef struct __attribute__((packed)) {
192  uint8_t length;
193  uint8_t type;
194  uint8_t first_interface;
195  uint8_t interface_count;
196  uint8_t class;
197  uint8_t subclass;
198  uint8_t protocol;
199  uint8_t idx;
201 
205 typedef struct __attribute__((packed)) {
206  uint8_t type;
207  uint8_t request;
208  uint16_t value;
209  uint16_t index;
210  uint16_t length;
211 } usb_setup_t;
212 
221 static inline bool usb_setup_is_read(usb_setup_t *pkt)
222 {
223  return pkt->type & USB_SETUP_REQUEST_DEVICE2HOST;
224 }
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
#define USB_SETUP_REQUEST_DEVICE2HOST
Request direction From device to host if the bit is set
Definition: descriptor.h:82
static bool usb_setup_is_read(usb_setup_t *pkt)
getter for setup packet direction
Definition: descriptor.h:221
USB configuration descriptor (USB 2.0 spec table 9-10)
Definition: descriptor.h:141
uint16_t total_length
Total length of the configuration
Definition: descriptor.h:144
uint8_t idx
Configuration descriptor string index
Definition: descriptor.h:147
uint8_t attributes
Configuration attributes (USB_CONF_ATTR)
Definition: descriptor.h:148
uint8_t max_power
Maximum power consumption in 2mA steps
Definition: descriptor.h:149
uint8_t num_interfaces
Number of interfaces supported by this configuration
Definition: descriptor.h:145
uint8_t length
Size of this descriptor
Definition: descriptor.h:142
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_CONFIGURATION)
Definition: descriptor.h:143
uint8_t val
Value to select this configuration by
Definition: descriptor.h:146
USB device descriptor (USB 2.0 spec table 9-8)
Definition: descriptor.h:121
uint8_t subclass
Device subclass code
Definition: descriptor.h:126
uint8_t num_configurations
Number of possible configurations
Definition: descriptor.h:135
uint8_t length
Size of this descriptor.
Definition: descriptor.h:122
uint8_t max_packet_size
EP0 max packet size (8, 16, 32 or 64 bytes)
Definition: descriptor.h:128
uint16_t vendor_id
Vendor ID (as assigned by the USB-IF)
Definition: descriptor.h:129
uint8_t serial_idx
Device serial number string index number
Definition: descriptor.h:134
uint16_t product_id
Product ID
Definition: descriptor.h:130
uint8_t manufacturer_idx
Manufacturer string index number
Definition: descriptor.h:132
uint8_t product_idx
Product string index number
Definition: descriptor.h:133
uint16_t bcd_usb
Binary-coded decimal USB release specification
Definition: descriptor.h:124
uint16_t bcd_device
Binary-coded decimal device release
Definition: descriptor.h:131
uint8_t protocol
Device protocol code
Definition: descriptor.h:127
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_DEVICE)
Definition: descriptor.h:123
USB endpoint descriptor (USB 2.0 spec table 9-13)
Definition: descriptor.h:170
uint8_t interval
Polling interval for the endpoint
Definition: descriptor.h:176
uint8_t address
Address of the endpoint
Definition: descriptor.h:173
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_ENDPOINT)
Definition: descriptor.h:172
uint16_t max_packet_size
Maximum packet size of the endpoint
Definition: descriptor.h:175
uint8_t length
Size of this descriptor
Definition: descriptor.h:171
uint8_t attributes
Attributes of the endpoint
Definition: descriptor.h:174
USB interface association descriptor (Interface Association Descriptors table 9-Z)
Definition: descriptor.h:191
uint8_t first_interface
Index of the first associated interface.
Definition: descriptor.h:194
uint8_t length
Size of this descriptor.
Definition: descriptor.h:192
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_INTERFACE_ASSOC)
Definition: descriptor.h:193
uint8_t idx
Descriptor string index.
Definition: descriptor.h:199
uint8_t interface_count
Number of associated interfaces.
Definition: descriptor.h:195
USB interface descriptor (USB 2.0 spec table 9-12)
Definition: descriptor.h:155
uint8_t length
Size of this descriptor
Definition: descriptor.h:156
uint8_t interface_num
Interface number
Definition: descriptor.h:158
uint8_t type
Descriptor type (USB_TYPE_DESCRIPTOR_INTERFACE)
Definition: descriptor.h:157
uint8_t num_endpoints
Number of endpoints used by this interface
Definition: descriptor.h:160
uint8_t alternate_setting
Alternate setting index
Definition: descriptor.h:159
uint8_t protocol
Protocol code
Definition: descriptor.h:163
uint8_t idx
Interface descriptor string index
Definition: descriptor.h:164
uint8_t subclass
Subclass code as assigned by the USB-IF
Definition: descriptor.h:162
USB string descriptor (USB 2.0 spec table 9-16)
Definition: descriptor.h:182
uint8_t length
Size of this descriptor.
Definition: descriptor.h:183
uint8_t type
String descriptor type
Definition: descriptor.h:184
USB setup packet (USB 2.0 spec table 9-2)
Definition: descriptor.h:205
uint16_t value
Value word
Definition: descriptor.h:208
uint8_t type
Request type
Definition: descriptor.h:206
uint16_t index
Index/offset word
Definition: descriptor.h:209
uint16_t length
Length of the data stage.
Definition: descriptor.h:210
uint8_t request
Specific request
Definition: descriptor.h:207