summaryrefslogtreecommitdiff
path: root/usb/descriptor.h
blob: f5f105c885f9bd114bb70f31cdbee467b0211dff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifndef DESCRIPTOR_H
#define DESCRIPTOR_H

#include <stdint.h>

template <typename A, typename B>
struct X {
	A a;
	B b;
} __attribute__((packed));

template <typename A>
constexpr A pack(A a) {
	return a;
}

template <typename A, typename... R>
constexpr auto pack(A a, R... r) -> X<A, decltype(pack(r...))> {
	return {a, pack(r...)};
}

struct Device_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint16_t bcdUSB;
	uint8_t bDeviceClass;
	uint8_t bDeviceSubClass;
	uint8_t bDeviceProtocol;
	uint8_t bMaxPacketSize0;
	uint16_t idVendor;
	uint16_t idProduct;
	uint16_t bcdDevice;
	uint8_t iManufacturer;
	uint8_t iProduct;
	uint8_t iSerialNumber;
	uint8_t bNumConfigurations;
} __attribute__((packed));

constexpr Device_desc device_desc(
		uint16_t bcdUSB,
		uint8_t bDeviceClass,
		uint8_t bDeviceSubClass,
		uint8_t bDeviceProtocol,
		uint8_t bMaxPacketSize0,
		uint16_t idVendor,
		uint16_t idProduct,
		uint16_t bcdDevice,
		uint8_t iManufacturer,
		uint8_t iProduct,
		uint8_t iSerialNumber,
		uint8_t bNumConfigurations
	) {
	
	return {
		sizeof(Device_desc),
		1,
		bcdUSB,
		bDeviceClass,
		bDeviceSubClass,
		bDeviceProtocol,
		bMaxPacketSize0,
		idVendor,
		idProduct,
		bcdDevice,
		iManufacturer,
		iProduct,
		iSerialNumber,
		bNumConfigurations
	};
}

struct Configuration_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint16_t wTotalLength;
	uint8_t bNumInterfaces;
	uint8_t bConfigurationvalue;
	uint8_t iConfiguration;
	uint8_t bmAttributes;
	uint8_t bMaxPower;
} __attribute__((packed));

template <typename... R>
constexpr auto configuration_desc(
		uint8_t bNumInterfaces,
		uint8_t bConfigurationvalue,
		uint8_t iConfiguration,
		uint8_t bmAttributes,
		uint8_t bMaxPower,
		R... r
	) -> decltype(pack(Configuration_desc(), r...)) {
	
	return pack(Configuration_desc {
		sizeof(Configuration_desc),
		2,
		sizeof(pack(Configuration_desc(), r...)),
		bNumInterfaces,
		bConfigurationvalue,
		iConfiguration,
		bmAttributes,
		bMaxPower
	}, r...);
}

struct Interface_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bInterfaceNumber;
	uint8_t bAlternateSetting;
	uint8_t bNumEndpoints;
	uint8_t bInterfaceClass;
	uint8_t bInterfaceSubClass;
	uint8_t bInterfaceProtocol;
	uint8_t iInterface;
} __attribute__((packed));

template <typename... R>
constexpr auto interface_desc(
		uint8_t bInterfaceNumber,
		uint8_t bAlternateSetting,
		uint8_t bNumEndpoints,
		uint8_t bInterfaceClass,
		uint8_t bInterfaceSubClass,
		uint8_t bInterfaceProtocol,
		uint8_t iInterface,
		R... r
	) -> decltype(pack(Interface_desc(), r...)) {
	
	return pack(Interface_desc {
		sizeof(Interface_desc),
		4,
		bInterfaceNumber,
		bAlternateSetting,
		bNumEndpoints,
		bInterfaceClass,
		bInterfaceSubClass,
		bInterfaceProtocol,
		iInterface
	}, r...);
}

struct Endpoint_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bEndpointAddress;
	uint8_t bmAttributes;
	uint16_t wMaxPacketSize;
	uint8_t bInterval;
} __attribute__((packed));

constexpr Endpoint_desc endpoint_desc(
		uint8_t bEndpointAddress,
		uint8_t bmAttributes,
		uint16_t wMaxPacketSize,
		uint8_t bInterval
	) {
	
	return {
		sizeof(Endpoint_desc),
		5,
		bEndpointAddress,
		bmAttributes,
		wMaxPacketSize,
		bInterval
	};
}

struct CDC_Header_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bDescriptorSubType;
	uint16_t bcdCDC;
} __attribute__((packed));

constexpr CDC_Header_desc cdc_header_desc(
		uint16_t bcdCDC
	) {
	
	return {
		sizeof(CDC_Header_desc),
		0x24,
		0x00,
		bcdCDC
	};
}

struct CDC_Call_Management_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bDescriptorSubType;
	uint8_t bmCapabilities;
	uint8_t bDataInterface;
} __attribute__((packed));

constexpr CDC_Call_Management_desc cdc_call_management_desc(
		uint8_t bmCapabilities,
		uint8_t bDataInterface
	) {
	
	return {
		sizeof(CDC_Call_Management_desc),
		0x24,
		0x01,
		bmCapabilities,
		bDataInterface
	};
}

struct CDC_ACM_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bDescriptorSubType;
	uint8_t bmCapabilities;
} __attribute__((packed));

constexpr CDC_ACM_desc cdc_acm_desc(
		uint8_t bmCapabilities
	) {
	
	return {
		sizeof(CDC_ACM_desc),
		0x24,
		0x02,
		bmCapabilities
	};
}

struct CDC_Union_desc {
	uint8_t bLength;
	uint8_t bDescriptorType;
	uint8_t bDescriptorSubType;
	uint8_t bControlInterface;
	uint8_t bSubordinateInterface0;
} __attribute__((packed));

constexpr CDC_Union_desc cdc_union_desc(
		uint8_t bControlInterface,
		uint8_t bSubordinateInterface0
	) {
	
	return {
		sizeof(CDC_Union_desc),
		0x24,
		0x06,
		bControlInterface,
		bSubordinateInterface0
	};
}

#endif