1 /*
2  * Copyright (c) 2022, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USB_CDC_H
7 #define USB_CDC_H
8 
9 /*------------------------------------------------------------------------------
10  *      Definitions  based on usbcdc11.pdf (www.usb.org)
11  *----------------------------------------------------------------------------*/
12 /* Communication device class specification version 1.10 */
13 #define CDC_V1_10 0x0110U
14 // Communication device class specification version 1.2
15 #define CDC_V1_2_0 0x0120U
16 
17 /* Communication interface class code */
18 /* (usbcdc11.pdf, 4.2, Table 15) */
19 #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02U
20 
21 /* Communication interface class subclass codes */
22 /* (usbcdc11.pdf, 4.3, Table 16) */
23 #define CDC_SUBCLASS_NONE       0x00 /* Reserved */
24 #define CDC_SUBCLASS_DLC        0x01 /* Direct Line Control Model */
25 #define CDC_SUBCLASS_ACM        0x02 /* Abstract Control Model */
26 #define CDC_SUBCLASS_TCM        0x03 /* Telephone Control Model */
27 #define CDC_SUBCLASS_MCM        0x04 /* Multi-Channel Control Model */
28 #define CDC_SUBCLASS_CAPI       0x05 /* CAPI Control Model */
29 #define CDC_SUBCLASS_ECM        0x06 /* Ethernet Networking Control Model */
30 #define CDC_SUBCLASS_ATM        0x07 /* ATM Networking Control Model */
31                                      /* 0x08-0x0d Reserved (future use) */
32 #define CDC_SUBCLASS_MBIM       0x0e /* MBIM Control Model */
33                                      /* 0x0f-0x7f Reserved (future use) */
34                                      /* 0x80-0xfe Reserved (vendor specific) */
35 
36 #define CDC_DIRECT_LINE_CONTROL_MODEL         0x01U
37 #define CDC_ABSTRACT_CONTROL_MODEL            0x02U
38 #define CDC_TELEPHONE_CONTROL_MODEL           0x03U
39 #define CDC_MULTI_CHANNEL_CONTROL_MODEL       0x04U
40 #define CDC_CAPI_CONTROL_MODEL                0x05U
41 #define CDC_ETHERNET_NETWORKING_CONTROL_MODEL 0x06U
42 #define CDC_ATM_NETWORKING_CONTROL_MODEL      0x07U
43 #define CDC_WIRELESS_HANDSET_CONTROL_MODEL    0x08U
44 #define CDC_DEVICE_MANAGEMENT                 0x09U
45 #define CDC_MOBILE_DIRECT_LINE_MODEL          0x0AU
46 #define CDC_OBEX                              0x0BU
47 #define CDC_ETHERNET_EMULATION_MODEL          0x0CU
48 #define CDC_NETWORK_CONTROL_MODEL             0x0DU
49 
50 /* Communication interface class control protocol codes */
51 /* (usbcdc11.pdf, 4.4, Table 17) */
52 #define CDC_COMMON_PROTOCOL_NONE                            0x00U
53 #define CDC_COMMON_PROTOCOL_AT_COMMANDS                     0x01U
54 #define CDC_COMMON_PROTOCOL_AT_COMMANDS_PCCA_101            0x02U
55 #define CDC_COMMON_PROTOCOL_AT_COMMANDS_PCCA_101_AND_ANNEXO 0x03U
56 #define CDC_COMMON_PROTOCOL_AT_COMMANDS_GSM_707             0x04U
57 #define CDC_COMMON_PROTOCOL_AT_COMMANDS_3GPP_27007          0x05U
58 #define CDC_COMMON_PROTOCOL_AT_COMMANDS_CDMA                0x06U
59 #define CDC_COMMON_PROTOCOL_ETHERNET_EMULATION_MODEL        0x07U
60 // NCM Communication Interface Protocol Codes
61 // (usbncm10.pdf, 4.2, Table 4-2)
62 #define CDC_NCM_PROTOCOL_NONE 0x00U
63 #define CDC_NCM_PROTOCOL_OEM  0xFEU
64 
65 /* Data interface class code */
66 /* (usbcdc11.pdf, 4.5, Table 18) */
67 #define CDC_DATA_INTERFACE_CLASS 0x0A
68 
69 /* Data Interface Sub-Class Codes ********************************************/
70 #define CDC_DATA_SUBCLASS_NONE  0x00
71 
72 /* Data interface class protocol codes */
73 /* (usbcdc11.pdf, 4.7, Table 19) */
74 #define CDC_DATA_PROTOCOL_ISDN_BRI            0x30
75 #define CDC_DATA_PROTOCOL_HDLC                0x31
76 #define CDC_DATA_PROTOCOL_TRANSPARENT         0x32
77 #define CDC_DATA_PROTOCOL_Q921_MANAGEMENT     0x50
78 #define CDC_DATA_PROTOCOL_Q921_DATA_LINK      0x51
79 #define CDC_DATA_PROTOCOL_Q921_MULTIPLEXOR    0x52
80 #define CDC_DATA_PROTOCOL_V42                 0x90
81 #define CDC_DATA_PROTOCOL_EURO_ISDN           0x91
82 #define CDC_DATA_PROTOCOL_V24_RATE_ADAPTATION 0x92
83 #define CDC_DATA_PROTOCOL_CAPI                0x93
84 #define CDC_DATA_PROTOCOL_HOST_BASED_DRIVER   0xFD
85 #define CDC_DATA_PROTOCOL_DESCRIBED_IN_PUFD   0xFE
86 
87 /* Type values for bDescriptorType field of functional descriptors */
88 /* (usbcdc11.pdf, 5.2.3, Table 24) */
89 #define CDC_CS_INTERFACE 0x24
90 #define CDC_CS_ENDPOINT  0x25
91 
92 /* Type values for bDescriptorSubtype field of functional descriptors */
93 /* (usbcdc11.pdf, 5.2.3, Table 25) */
94 #define CDC_FUNC_DESC_HEADER                          0x00
95 #define CDC_FUNC_DESC_CALL_MANAGEMENT                 0x01
96 #define CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT     0x02
97 #define CDC_FUNC_DESC_DIRECT_LINE_MANAGEMENT          0x03
98 #define CDC_FUNC_DESC_TELEPHONE_RINGER                0x04
99 #define CDC_FUNC_DESC_REPORTING_CAPABILITIES          0x05
100 #define CDC_FUNC_DESC_UNION                           0x06
101 #define CDC_FUNC_DESC_COUNTRY_SELECTION               0x07
102 #define CDC_FUNC_DESC_TELEPHONE_OPERATIONAL_MODES     0x08
103 #define CDC_FUNC_DESC_USB_TERMINAL                    0x09
104 #define CDC_FUNC_DESC_NETWORK_CHANNEL                 0x0A
105 #define CDC_FUNC_DESC_PROTOCOL_UNIT                   0x0B
106 #define CDC_FUNC_DESC_EXTENSION_UNIT                  0x0C
107 #define CDC_FUNC_DESC_MULTI_CHANNEL_MANAGEMENT        0x0D
108 #define CDC_FUNC_DESC_CAPI_CONTROL_MANAGEMENT         0x0E
109 #define CDC_FUNC_DESC_ETHERNET_NETWORKING             0x0F
110 #define CDC_FUNC_DESC_ATM_NETWORKING                  0x10
111 #define CDC_FUNC_DESC_WIRELESS_HANDSET_CONTROL_MODEL  0x11
112 #define CDC_FUNC_DESC_MOBILE_DIRECT_LINE_MODEL        0x12
113 #define CDC_FUNC_DESC_MOBILE_DIRECT_LINE_MODEL_DETAIL 0x13
114 #define CDC_FUNC_DESC_DEVICE_MANAGEMENT_MODEL         0x14
115 #define CDC_FUNC_DESC_OBEX                            0x15
116 #define CDC_FUNC_DESC_COMMAND_SET                     0x16
117 #define CDC_FUNC_DESC_COMMAND_SET_DETAIL              0x17
118 #define CDC_FUNC_DESC_TELEPHONE_CONTROL_MODEL         0x18
119 #define CDC_FUNC_DESC_OBEX_SERVICE_IDENTIFIER         0x19
120 #define CDC_FUNC_DESC_NCM                             0x1A
121 
122 /* CDC class-specific request codes */
123 /* (usbcdc11.pdf, 6.2, Table 46) */
124 /* see Table 45 for info about the specific requests. */
125 #define CDC_REQUEST_SEND_ENCAPSULATED_COMMAND      0x00
126 #define CDC_REQUEST_GET_ENCAPSULATED_RESPONSE      0x01
127 #define CDC_REQUEST_SET_COMM_FEATURE               0x02
128 #define CDC_REQUEST_GET_COMM_FEATURE               0x03
129 #define CDC_REQUEST_CLEAR_COMM_FEATURE             0x04
130 #define CDC_REQUEST_SET_AUX_LINE_STATE             0x10
131 #define CDC_REQUEST_SET_HOOK_STATE                 0x11
132 #define CDC_REQUEST_PULSE_SETUP                    0x12
133 #define CDC_REQUEST_SEND_PULSE                     0x13
134 #define CDC_REQUEST_SET_PULSE_TIME                 0x14
135 #define CDC_REQUEST_RING_AUX_JACK                  0x15
136 #define CDC_REQUEST_SET_LINE_CODING                0x20
137 #define CDC_REQUEST_GET_LINE_CODING                0x21
138 #define CDC_REQUEST_SET_CONTROL_LINE_STATE         0x22
139 #define CDC_REQUEST_SEND_BREAK                     0x23
140 #define CDC_REQUEST_SET_RINGER_PARMS               0x30
141 #define CDC_REQUEST_GET_RINGER_PARMS               0x31
142 #define CDC_REQUEST_SET_OPERATION_PARMS            0x32
143 #define CDC_REQUEST_GET_OPERATION_PARMS            0x33
144 #define CDC_REQUEST_SET_LINE_PARMS                 0x34
145 #define CDC_REQUEST_GET_LINE_PARMS                 0x35
146 #define CDC_REQUEST_DIAL_DIGITS                    0x36
147 #define CDC_REQUEST_SET_UNIT_PARAMETER             0x37
148 #define CDC_REQUEST_GET_UNIT_PARAMETER             0x38
149 #define CDC_REQUEST_CLEAR_UNIT_PARAMETER           0x39
150 #define CDC_REQUEST_GET_PROFILE                    0x3A
151 #define CDC_REQUEST_SET_ETHERNET_MULTICAST_FILTERS 0x40
152 #define CDC_REQUEST_SET_ETHERNET_PMP_FILTER        0x41
153 #define CDC_REQUEST_GET_ETHERNET_PMP_FILTER        0x42
154 #define CDC_REQUEST_SET_ETHERNET_PACKET_FILTER     0x43
155 #define CDC_REQUEST_GET_ETHERNET_STATISTIC         0x44
156 #define CDC_REQUEST_SET_ATM_DATA_FORMAT            0x50
157 #define CDC_REQUEST_GET_ATM_DEVICE_STATISTICS      0x51
158 #define CDC_REQUEST_SET_ATM_DEFAULT_VC             0x52
159 #define CDC_REQUEST_GET_ATM_VC_STATISTICS          0x53
160 #define CDC_REQUEST_GET_NTB_PARAMETERS             0x80
161 #define CDC_REQUEST_GET_NET_ADDRESS                0x81
162 #define CDC_REQUEST_SET_NET_ADDRESS                0x82
163 #define CDC_REQUEST_GET_NTB_FORMAT                 0x83
164 #define CDC_REQUEST_SET_NTB_FORMAT                 0x84
165 #define CDC_REQUEST_GET_NTB_INPUT_SIZE             0x85
166 #define CDC_REQUEST_SET_NTB_INPUT_SIZE             0x86
167 #define CDC_REQUEST_GET_MAX_DATAGRAM_SIZE          0x87
168 #define CDC_REQUEST_SET_MAX_DATAGRAM_SIZE          0x88
169 #define CDC_REQUEST_GET_CRC_MODE                   0x89
170 #define CDC_REQUEST_SET_CRC_MODE                   0x90
171 
172 /* Communication feature selector codes */
173 /* (usbcdc11.pdf, 6.2.2..6.2.4, Table 47) */
174 #define CDC_ABSTRACT_STATE  0x01
175 #define CDC_COUNTRY_SETTING 0x02
176 
177 /** Control Signal Bitmap Values for SetControlLineState */
178 #define SET_CONTROL_LINE_STATE_RTS 0x02
179 #define SET_CONTROL_LINE_STATE_DTR 0x01
180 
181 /* Feature Status returned for ABSTRACT_STATE Selector */
182 /* (usbcdc11.pdf, 6.2.3, Table 48) */
183 #define CDC_IDLE_SETTING          (1 << 0)
184 #define CDC_DATA_MULTPLEXED_STATE (1 << 1)
185 
186 /* Control signal bitmap values for the SetControlLineState request */
187 /* (usbcdc11.pdf, 6.2.14, Table 51) */
188 #define CDC_DTE_PRESENT      (1 << 0)
189 #define CDC_ACTIVATE_CARRIER (1 << 1)
190 
191 /* CDC class-specific notification codes */
192 /* (usbcdc11.pdf, 6.3, Table 68) */
193 /* see Table 67 for Info about class-specific notifications */
194 #define CDC_NOTIFICATION_NETWORK_CONNECTION 0x00
195 #define CDC_RESPONSE_AVAILABLE              0x01
196 #define CDC_AUX_JACK_HOOK_STATE             0x08
197 #define CDC_RING_DETECT                     0x09
198 #define CDC_NOTIFICATION_SERIAL_STATE       0x20
199 #define CDC_CALL_STATE_CHANGE               0x28
200 #define CDC_LINE_STATE_CHANGE               0x29
201 #define CDC_CONNECTION_SPEED_CHANGE         0x2A
202 
203 /* UART state bitmap values (Serial state notification). */
204 /* (usbcdc11.pdf, 6.3.5, Table 69) */
205 #define CDC_SERIAL_STATE_OVERRUN        (1 << 6) /* receive data overrun error has occurred */
206 #define CDC_SERIAL_STATE_OVERRUN_Pos    (6)
207 #define CDC_SERIAL_STATE_OVERRUN_Msk    (1 << CDC_SERIAL_STATE_OVERRUN_Pos)
208 #define CDC_SERIAL_STATE_PARITY         (1 << 5) /* parity error has occurred */
209 #define CDC_SERIAL_STATE_PARITY_Pos     (5)
210 #define CDC_SERIAL_STATE_PARITY_Msk     (1 << CDC_SERIAL_STATE_PARITY_Pos)
211 #define CDC_SERIAL_STATE_FRAMING        (1 << 4) /* framing error has occurred */
212 #define CDC_SERIAL_STATE_FRAMING_Pos    (4)
213 #define CDC_SERIAL_STATE_FRAMING_Msk    (1 << CDC_SERIAL_STATE_FRAMING_Pos)
214 #define CDC_SERIAL_STATE_RING           (1 << 3) /* state of ring signal detection */
215 #define CDC_SERIAL_STATE_RING_Pos       (3)
216 #define CDC_SERIAL_STATE_RING_Msk       (1 << CDC_SERIAL_STATE_RING_Pos)
217 #define CDC_SERIAL_STATE_BREAK          (1 << 2) /* state of break detection */
218 #define CDC_SERIAL_STATE_BREAK_Pos      (2)
219 #define CDC_SERIAL_STATE_BREAK_Msk      (1 << CDC_SERIAL_STATE_BREAK_Pos)
220 #define CDC_SERIAL_STATE_TX_CARRIER     (1 << 1) /* state of transmission carrier */
221 #define CDC_SERIAL_STATE_TX_CARRIER_Pos (1)
222 #define CDC_SERIAL_STATE_TX_CARRIER_Msk (1 << CDC_SERIAL_STATE_TX_CARRIER_Pos)
223 #define CDC_SERIAL_STATE_RX_CARRIER     (1 << 0) /* state of receiver carrier */
224 #define CDC_SERIAL_STATE_RX_CARRIER_Pos (0)
225 #define CDC_SERIAL_STATE_RX_CARRIER_Msk (1 << CDC_SERIAL_STATE_RX_CARRIER_Pos)
226 
227 #define CDC_ECM_XMIT_OK                                     (1 << 0)
228 #define CDC_ECM_RVC_OK                                      (1 << 1)
229 #define CDC_ECM_XMIT_ERROR                                  (1 << 2)
230 #define CDC_ECM_RCV_ERROR                                   (1 << 3)
231 #define CDC_ECM_RCV_NO_BUFFER                               (1 << 4)
232 #define CDC_ECM_DIRECTED_BYTES_XMIT                         (1 << 5)
233 #define CDC_ECM_DIRECTED_FRAMES_XMIT                        (1 << 6)
234 #define CDC_ECM_MULTICAST_BYTES_XMIT                        (1 << 7)
235 #define CDC_ECM_MULTICAST_FRAMES_XMIT                       (1 << 8)
236 #define CDC_ECM_BROADCAST_BYTES_XMIT                        (1 << 9)
237 #define CDC_ECM_BROADCAST_FRAMES_XMIT                       (1 << 10)
238 #define CDC_ECM_DIRECTED_BYTES_RCV                          (1 << 11)
239 #define CDC_ECM_DIRECTED_FRAMES_RCV                         (1 << 12)
240 #define CDC_ECM_MULTICAST_BYTES_RCV                         (1 << 13)
241 #define CDC_ECM_MULTICAST_FRAMES_RCV                        (1 << 14)
242 #define CDC_ECM_BROADCAST_BYTES_RCV                         (1 << 15)
243 #define CDC_ECM_BROADCAST_FRAMES_RCV                        (1 << 16)
244 #define CDC_ECM_RCV_CRC_ERROR                               (1 << 17)
245 #define CDC_ECM_TRANSMIT_QUEUE_LENGTH                       (1 << 18)
246 #define CDC_ECM_RCV_ERROR_ALIGNMENT                         (1 << 19)
247 #define CDC_ECM_XMIT_ONE_COLLISION                          (1 << 20)
248 #define CDC_ECM_XMIT_MORE_COLLISIONS                        (1 << 21)
249 #define CDC_ECM_XMIT_DEFERRED                               (1 << 22)
250 #define CDC_ECM_XMIT_MAX_COLLISIONS                         (1 << 23)
251 #define CDC_ECM_RCV_OVERRUN                                 (1 << 24)
252 #define CDC_ECM_XMIT_UNDERRUN                               (1 << 25)
253 #define CDC_ECM_XMIT_HEARTBEAT_FAILURE                      (1 << 26)
254 #define CDC_ECM_XMIT_TIMES_CRS_LOST                         (1 << 27)
255 #define CDC_ECM_XMIT_LATE_COLLISIONS                        (1 << 28)
256 
257 #define CDC_ECM_MAC_STR_DESC                                (uint8_t *)"010202030000"
258 #define CDC_ECM_MAC_ADDR0                                   0x00U /* 01 */
259 #define CDC_ECM_MAC_ADDR1                                   0x02U /* 02 */
260 #define CDC_ECM_MAC_ADDR2                                   0x02U /* 03 */
261 #define CDC_ECM_MAC_ADDR3                                   0x03U /* 00 */
262 #define CDC_ECM_MAC_ADDR4                                   0x00U /* 00 */
263 #define CDC_ECM_MAC_ADDR5                                   0x00U /* 00 */
264 
265 #define CDC_ECM_NET_DISCONNECTED                            0x00U
266 #define CDC_ECM_NET_CONNECTED                               0x01U
267 
268 #define CDC_ECM_ETH_STATS_RESERVED                          0xE0U
269 #define CDC_ECM_BMREQUEST_TYPE_ECM                          0xA1U
270 
271 #define CDC_ECM_CONNECT_SPEED_UPSTREAM                      0x004C4B40U /* 5Mbps */
272 #define CDC_ECM_CONNECT_SPEED_DOWNSTREAM                    0x004C4B40U /* 5Mbps */
273 
274 #define CDC_ECM_NOTIFY_CODE_NETWORK_CONNECTION              0x00
275 #define CDC_ECM_NOTIFY_CODE_RESPONSE_AVAILABLE              0x01
276 #define CDC_ECM_NOTIFY_CODE_CONNECTION_SPEED_CHANGE         0x2A
277 
278 #define CDC_NCM_NTH16_SIGNATURE             0x484D434E
279 #define CDC_NCM_NDP16_SIGNATURE_NCM0        0x304D434E
280 #define CDC_NCM_NDP16_SIGNATURE_NCM1        0x314D434E
281 
282 /*------------------------------------------------------------------------------
283  *      Structures  based on usbcdc11.pdf (www.usb.org)
284  *----------------------------------------------------------------------------*/
285 
286 /* Header functional descriptor */
287 /* (usbcdc11.pdf, 5.2.3.1) */
288 /* This header must precede any list of class-specific descriptors. */
289 struct cdc_header_descriptor {
290     uint8_t bFunctionLength;    /* size of this descriptor in bytes */
291     uint8_t bDescriptorType;    /* CS_INTERFACE descriptor type */
292     uint8_t bDescriptorSubtype; /* Header functional descriptor subtype */
293     uint16_t bcdCDC;            /* USB CDC specification release version */
294 } __PACKED;
295 
296 /* Call management functional descriptor */
297 /* (usbcdc11.pdf, 5.2.3.2) */
298 /* Describes the processing of calls for the communication class interface. */
299 struct cdc_call_management_descriptor {
300     uint8_t bFunctionLength;    /* size of this descriptor in bytes */
301     uint8_t bDescriptorType;    /* CS_INTERFACE descriptor type */
302     uint8_t bDescriptorSubtype; /* call management functional descriptor subtype */
303     uint8_t bmCapabilities;     /* capabilities that this configuration supports */
304     uint8_t bDataInterface;     /* interface number of the data class interface used for call management (optional) */
305 } __PACKED;
306 
307 /* Abstract control management functional descriptor */
308 /* (usbcdc11.pdf, 5.2.3.3) */
309 /* Describes the command supported by the communication interface class with the Abstract Control Model subclass code. */
310 struct cdc_abstract_control_management_descriptor {
311     uint8_t bFunctionLength;    /* size of this descriptor in bytes */
312     uint8_t bDescriptorType;    /* CS_INTERFACE descriptor type */
313     uint8_t bDescriptorSubtype; /* abstract control management functional descriptor subtype */
314     uint8_t bmCapabilities;     /* capabilities supported by this configuration */
315 } __PACKED;
316 
317 /* Union functional descriptors */
318 /* (usbcdc11.pdf, 5.2.3.8) */
319 /* Describes the relationship between a group of interfaces that can be considered to form a functional unit. */
320 struct cdc_union_descriptor {
321     uint8_t bFunctionLength;    /* size of this descriptor in bytes */
322     uint8_t bDescriptorType;    /* CS_INTERFACE descriptor type */
323     uint8_t bDescriptorSubtype; /* union functional descriptor subtype */
324     uint8_t bMasterInterface;   /* interface number designated as master */
325 } __PACKED;
326 
327 /* Union functional descriptors with one slave interface */
328 /* (usbcdc11.pdf, 5.2.3.8) */
329 struct cdc_union_1slave_descriptor {
330     uint8_t bFunctionLength;
331     uint8_t bDescriptorType;
332     uint8_t bDescriptorSubtype;
333     uint8_t bControlInterface;
334     uint8_t bSubordinateInterface0;
335 } __PACKED;
336 
337 /* Line coding structure for GET_LINE_CODING / SET_LINE_CODING class requests*/
338 /* Format of the data returned when a GetLineCoding request is received */
339 /* (usbcdc11.pdf, 6.2.13) */
340 struct cdc_line_coding {
341     uint32_t dwDTERate;  /* Data terminal rate in bits per second */
342     uint8_t bCharFormat; /* Number of stop bits */
343     uint8_t bParityType; /* Parity bit type */
344     uint8_t bDataBits;   /* Number of data bits */
345 } __PACKED;
346 
347 /** Data structure for the notification about SerialState */
348 struct cdc_acm_notification {
349     uint8_t bmRequestType;
350     uint8_t bNotificationType;
351     uint16_t wValue;
352     uint16_t wIndex;
353     uint16_t wLength;
354     uint16_t data;
355 } __PACKED;
356 
357 /** Ethernet Networking Functional Descriptor */
358 struct cdc_eth_descriptor {
359     uint8_t bFunctionLength;
360     uint8_t bDescriptorType;
361     uint8_t bDescriptorSubtype;
362     uint8_t iMACAddress;
363     uint32_t bmEthernetStatistics;
364     uint16_t wMaxSegmentSize;
365     uint16_t wNumberMCFilters;
366     uint8_t bNumberPowerFilters;
367 } __PACKED;
368 
369 struct cdc_eth_notification {
370     uint8_t bmRequestType;
371     uint8_t bNotificationType;
372     uint16_t wValue;
373     uint16_t wIndex;
374     uint16_t wLength;
375     uint8_t data[8];
376 } __PACKED;
377 
378 struct cdc_ncm_ntb_parameters {
379     uint16_t wLength;
380     uint16_t bmNtbFormatsSupported;
381     uint32_t dwNtbInMaxSize;
382     uint16_t wNdbInDivisor;
383     uint16_t wNdbInPayloadRemainder;
384     uint16_t wNdbInAlignment;
385     uint16_t wReserved;
386     uint32_t dwNtbOutMaxSize;
387     uint16_t wNdbOutDivisor;
388     uint16_t wNdbOutPayloadRemainder;
389     uint16_t wNdbOutAlignment;
390     uint16_t wNtbOutMaxDatagrams;
391 };
392 
393 struct cdc_ncm_nth16 {
394     uint32_t dwSignature;
395     uint16_t wHeaderLength;
396     uint16_t wSequence;
397     uint16_t wBlockLength;
398     uint16_t wNdpIndex;
399 };
400 
401 struct cdc_ncm_ndp16_datagram {
402     uint16_t wDatagramIndex;
403     uint16_t wDatagramLength;
404 };
405 
406 struct cdc_ncm_ndp16 {
407     uint32_t dwSignature;
408     uint16_t wLength;
409     uint16_t wNextNdpIndex;
410     struct cdc_ncm_ndp16_datagram datagram[];
411 };
412 
413 /*Length of template descriptor: 66 bytes*/
414 #define CDC_ACM_DESCRIPTOR_LEN (8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 7 + 7)
415 // clang-format off
416 #define CDC_ACM_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, wMaxPacketSize, str_idx) \
417     /* Interface Associate */                                                                  \
418     0x08,                                                  /* bLength */                       \
419     USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION,             /* bDescriptorType */               \
420     bFirstInterface,                                       /* bFirstInterface */               \
421     0x02,                                                  /* bInterfaceCount */               \
422     USB_DEVICE_CLASS_CDC,                                  /* bFunctionClass */                \
423     CDC_ABSTRACT_CONTROL_MODEL,                            /* bFunctionSubClass */             \
424     CDC_COMMON_PROTOCOL_NONE,                              /* bFunctionProtocol */             \
425     0x00,                                                  /* iFunction */                     \
426     0x09,                                                  /* bLength */                       \
427     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
428     bFirstInterface,                                       /* bInterfaceNumber */              \
429     0x00,                                                  /* bAlternateSetting */             \
430     0x01,                                                  /* bNumEndpoints */                 \
431     USB_DEVICE_CLASS_CDC,                                  /* bInterfaceClass */               \
432     CDC_ABSTRACT_CONTROL_MODEL,                            /* bInterfaceSubClass */            \
433     CDC_COMMON_PROTOCOL_NONE,                              /* bInterfaceProtocol */            \
434     str_idx,                                               /* iInterface */                    \
435     0x05,                                                  /* bLength */                       \
436     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
437     CDC_FUNC_DESC_HEADER,                                  /* bDescriptorSubtype */            \
438     WBVAL(CDC_V1_10),                                      /* bcdCDC */                        \
439     0x05,                                                  /* bLength */                       \
440     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
441     CDC_FUNC_DESC_CALL_MANAGEMENT,                         /* bDescriptorSubtype */            \
442     0x00,                                                  /* bmCapabilities */                \
443     (uint8_t)(bFirstInterface + 1),                        /* bDataInterface */                \
444     0x04,                                                  /* bLength */                       \
445     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
446     CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT,             /* bDescriptorSubtype */            \
447     0x02,                                                  /* bmCapabilities */                \
448     0x05,                                                  /* bLength */                       \
449     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
450     CDC_FUNC_DESC_UNION,                                   /* bDescriptorSubtype */            \
451     bFirstInterface,                                       /* bMasterInterface */              \
452     (uint8_t)(bFirstInterface + 1),                        /* bSlaveInterface0 */              \
453     0x07,                                                  /* bLength */                       \
454     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
455     int_ep,                                                /* bEndpointAddress */              \
456     0x03,                                                  /* bmAttributes */                  \
457     0x08, 0x00,                                            /* wMaxPacketSize */                \
458     0x0a,                                                  /* bInterval */                     \
459     0x09,                                                  /* bLength */                       \
460     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
461     (uint8_t)(bFirstInterface + 1),                        /* bInterfaceNumber */              \
462     0x00,                                                  /* bAlternateSetting */             \
463     0x02,                                                  /* bNumEndpoints */                 \
464     CDC_DATA_INTERFACE_CLASS,                              /* bInterfaceClass */               \
465     0x00,                                                  /* bInterfaceSubClass */            \
466     0x00,                                                  /* bInterfaceProtocol */            \
467     0x00,                                                  /* iInterface */                    \
468     0x07,                                                  /* bLength */                       \
469     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
470     out_ep,                                                /* bEndpointAddress */              \
471     0x02,                                                  /* bmAttributes */                  \
472     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
473     0x00,                                                  /* bInterval */                     \
474     0x07,                                                  /* bLength */                       \
475     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
476     in_ep,                                                 /* bEndpointAddress */              \
477     0x02,                                                  /* bmAttributes */                  \
478     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
479     0x00                                                   /* bInterval */
480 // clang-format on
481 
482 /*Length of template descriptor: 66 bytes*/
483 #define CDC_RNDIS_DESCRIPTOR_LEN (8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 7 + 7)
484 // clang-format off
485 #define CDC_RNDIS_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, wMaxPacketSize, str_idx) \
486     /* Interface Associate */                                                                  \
487     0x08,                                                  /* bLength */                       \
488     USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION,             /* bDescriptorType */               \
489     bFirstInterface,                                       /* bFirstInterface */               \
490     0x02,                                                  /* bInterfaceCount */               \
491     USB_DEVICE_CLASS_WIRELESS,                             /* bFunctionClass */                \
492     0x01,                                                  /* bFunctionSubClass */             \
493     0x03,                                                  /* bFunctionProtocol */             \
494     0x00,                                                  /* iFunction */                     \
495     0x09,                                                  /* bLength */                       \
496     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
497     bFirstInterface,                                       /* bInterfaceNumber */              \
498     0x00,                                                  /* bAlternateSetting */             \
499     0x01,                                                  /* bNumEndpoints */                 \
500     USB_DEVICE_CLASS_WIRELESS,                             /* bInterfaceClass */               \
501     0x01,                                                  /* bInterfaceSubClass */            \
502     0x03,                                                  /* bInterfaceProtocol */            \
503     str_idx,                                               /* iInterface */                    \
504     0x05,                                                  /* bLength */                       \
505     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
506     CDC_FUNC_DESC_HEADER,                                  /* bDescriptorSubtype */            \
507     WBVAL(CDC_V1_10),                                      /* bcdCDC */                        \
508     0x05,                                                  /* bLength */                       \
509     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
510     CDC_FUNC_DESC_CALL_MANAGEMENT,                         /* bDescriptorSubtype */            \
511     0x00,                                                  /* bmCapabilities */                \
512     (uint8_t)(bFirstInterface + 1),                        /* bDataInterface */                \
513     0x04,                                                  /* bLength */                       \
514     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
515     CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT,             /* bDescriptorSubtype */            \
516     0x00,                                                  /* bmCapabilities */                \
517     0x05,                                                  /* bLength */                       \
518     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
519     CDC_FUNC_DESC_UNION,                                   /* bDescriptorSubtype */            \
520     bFirstInterface,                                       /* bMasterInterface */              \
521     (uint8_t)(bFirstInterface + 1),                        /* bSlaveInterface0 */              \
522     0x07,                                                  /* bLength */                       \
523     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
524     int_ep,                                                /* bEndpointAddress */              \
525     0x03,                                                  /* bmAttributes */                  \
526     0x08, 0x00,                                            /* wMaxPacketSize */                \
527     0x05,                                                  /* bInterval */                     \
528     0x09,                                                  /* bLength */                       \
529     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
530     (uint8_t)(bFirstInterface + 1),                        /* bInterfaceNumber */              \
531     0x00,                                                  /* bAlternateSetting */             \
532     0x02,                                                  /* bNumEndpoints */                 \
533     CDC_DATA_INTERFACE_CLASS,                              /* bInterfaceClass */               \
534     0x00,                                                  /* bInterfaceSubClass */            \
535     0x00,                                                  /* bInterfaceProtocol */            \
536     0x00,                                                  /* iInterface */                    \
537     0x07,                                                  /* bLength */                       \
538     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
539     out_ep,                                                /* bEndpointAddress */              \
540     0x02,                                                  /* bmAttributes */                  \
541     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
542     0x00,                                                  /* bInterval */                     \
543     0x07,                                                  /* bLength */                       \
544     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
545     in_ep,                                                 /* bEndpointAddress */              \
546     0x02,                                                  /* bmAttributes */                  \
547     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
548     0x00                                                   /* bInterval */
549 // clang-format on
550 
551 #define DBVAL_BE(x) ((x >> 24) & 0xFF), ((x >> 16) & 0xFF), ((x >> 8) & 0xFF), (x & 0xFF)
552 
553 /*Length of template descriptor: 71 bytes*/
554 #define CDC_ECM_DESCRIPTOR_LEN   (8 + 9 + 5 + 5 + 13 + 7 + 9 + 7 + 7)
555 // clang-format off
556 #define CDC_ECM_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, wMaxPacketSize, \
557 eth_statistics, wMaxSegmentSize, wNumberMCFilters, bNumberPowerFilters, str_idx) \
558     /* Interface Associate */                                                                  \
559     0x08,                                                  /* bLength */                       \
560     USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION,             /* bDescriptorType */               \
561     bFirstInterface,                                       /* bFirstInterface */               \
562     0x02,                                                  /* bInterfaceCount */               \
563     USB_DEVICE_CLASS_CDC,                                  /* bFunctionClass */                \
564     CDC_ETHERNET_NETWORKING_CONTROL_MODEL,                 /* bFunctionSubClass */             \
565     CDC_COMMON_PROTOCOL_NONE,                              /* bFunctionProtocol */             \
566     0x00,                                                  /* iFunction */                     \
567     0x09,                                                  /* bLength */                       \
568     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
569     bFirstInterface,                                       /* bInterfaceNumber */              \
570     0x00,                                                  /* bAlternateSetting */             \
571     0x01,                                                  /* bNumEndpoints */                 \
572     USB_DEVICE_CLASS_CDC,                                  /* bInterfaceClass */               \
573     CDC_ETHERNET_NETWORKING_CONTROL_MODEL,                 /* bInterfaceSubClass */            \
574     CDC_COMMON_PROTOCOL_NONE,                              /* bInterfaceProtocol */            \
575     str_idx,                                               /* iInterface */                    \
576     0x05,                                                  /* bLength */                       \
577     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
578     CDC_FUNC_DESC_HEADER,                                  /* bDescriptorSubtype */            \
579     WBVAL(CDC_V1_10),                                      /* bcdCDC */                        \
580     0x05,                                                  /* bLength */                       \
581     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
582     CDC_FUNC_DESC_UNION,                                   /* bDescriptorSubtype */            \
583     bFirstInterface,                                       /* bMasterInterface */              \
584     (uint8_t)(bFirstInterface + 1),                        /* bSlaveInterface0 */              \
585     /* CDC_ECM Functional Descriptor */ \
586     0x0D,                                                   /* bFunctionLength */\
587     CDC_CS_INTERFACE,                                       /* bDescriptorType: CS_INTERFACE */\
588     CDC_FUNC_DESC_ETHERNET_NETWORKING, /* Ethernet Networking functional descriptor subtype  */\
589     str_idx,                                                    /* Device's MAC string index */\
590     DBVAL_BE(eth_statistics),                                /* Ethernet statistics (bitmap) */\
591     WBVAL(wMaxSegmentSize),/* wMaxSegmentSize: Ethernet Maximum Segment size, typically 1514 bytes */\
592     WBVAL(wNumberMCFilters),            /* wNumberMCFilters: the number of multicast filters */\
593     bNumberPowerFilters,          /* bNumberPowerFilters: the number of wakeup power filters */\
594     0x07,                                                  /* bLength */                       \
595     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
596     int_ep,                                                /* bEndpointAddress */              \
597     0x03,                                                  /* bmAttributes */                  \
598     0x10, 0x00,                                            /* wMaxPacketSize */                \
599     0x05,                                                  /* bInterval */                     \
600     0x09,                                                  /* bLength */                       \
601     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
602     (uint8_t)(bFirstInterface + 1),                        /* bInterfaceNumber */              \
603     0x00,                                                  /* bAlternateSetting */             \
604     0x02,                                                  /* bNumEndpoints */                 \
605     CDC_DATA_INTERFACE_CLASS,                              /* bInterfaceClass */               \
606     0x00,                                                  /* bInterfaceSubClass */            \
607     0x00,                                                  /* bInterfaceProtocol */            \
608     0x00,                                                  /* iInterface */                    \
609     0x07,                                                  /* bLength */                       \
610     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
611     out_ep,                                                /* bEndpointAddress */              \
612     0x02,                                                  /* bmAttributes */                  \
613     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
614     0x00,                                                  /* bInterval */                     \
615     0x07,                                                  /* bLength */                       \
616     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
617     in_ep,                                                 /* bEndpointAddress */              \
618     0x02,                                                  /* bmAttributes */                  \
619     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
620     0x00                                                   /* bInterval */
621 // clang-format on
622 
623 /*Length of template descriptor: 77 bytes*/
624 #define CDC_NCM_DESCRIPTOR_LEN   (8 + 9 + 5 + 5 + 13 + 6 + 7 + 9 + 7 + 7)
625 // clang-format off
626 #define CDC_NCM_DESCRIPTOR_INIT(bFirstInterface, int_ep, out_ep, in_ep, wMaxPacketSize, \
627 eth_statistics, wMaxSegmentSize, wNumberMCFilters, bNumberPowerFilters, str_idx) \
628     /* Interface Associate */                                                                  \
629     0x08,                                                  /* bLength */                       \
630     USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION,             /* bDescriptorType */               \
631     bFirstInterface,                                       /* bFirstInterface */               \
632     0x02,                                                  /* bInterfaceCount */               \
633     USB_DEVICE_CLASS_CDC,                                  /* bFunctionClass */                \
634     CDC_NETWORK_CONTROL_MODEL,                             /* bFunctionSubClass */             \
635     CDC_COMMON_PROTOCOL_NONE,                              /* bFunctionProtocol */             \
636     0x00,                                                  /* iFunction */                     \
637     0x09,                                                  /* bLength */                       \
638     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
639     bFirstInterface,                                       /* bInterfaceNumber */              \
640     0x00,                                                  /* bAlternateSetting */             \
641     0x01,                                                  /* bNumEndpoints */                 \
642     USB_DEVICE_CLASS_CDC,                                  /* bInterfaceClass */               \
643     CDC_NETWORK_CONTROL_MODEL,                             /* bInterfaceSubClass */            \
644     CDC_COMMON_PROTOCOL_NONE,                              /* bInterfaceProtocol */            \
645     str_idx,                                               /* iInterface */                    \
646     0x05,                                                  /* bLength */                       \
647     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
648     CDC_FUNC_DESC_HEADER,                                  /* bDescriptorSubtype */            \
649     WBVAL(CDC_V1_10),                                      /* bcdCDC */                        \
650     0x05,                                                  /* bLength */                       \
651     CDC_CS_INTERFACE,                                      /* bDescriptorType */               \
652     CDC_FUNC_DESC_UNION,                                   /* bDescriptorSubtype */            \
653     bFirstInterface,                                       /* bMasterInterface */              \
654     (uint8_t)(bFirstInterface + 1),                        /* bSlaveInterface0 */              \
655     /* CDC ETH Functional Descriptor */ \
656     0x0D,                                                   /* bFunctionLength */\
657     CDC_CS_INTERFACE,                                       /* bDescriptorType: CS_INTERFACE */\
658     CDC_FUNC_DESC_ETHERNET_NETWORKING, /* Ethernet Networking functional descriptor subtype  */\
659     str_idx,                                                    /* Device's MAC string index */\
660     DBVAL_BE(eth_statistics),                                /* Ethernet statistics (bitmap) */\
661     WBVAL(wMaxPacketSize),/* wMaxSegmentSize: Ethernet Maximum Segment size, typically 1514 bytes */\
662     WBVAL(wNumberMCFilters),            /* wNumberMCFilters: the number of multicast filters */\
663     bNumberPowerFilters,          /* bNumberPowerFilters: the number of wakeup power filters */\
664     0x06,                                                                                      \
665     CDC_CS_INTERFACE,                                                                          \
666     CDC_FUNC_DESC_NCM,                                                                         \
667     0x00, 0x01,                                                                                \
668     0x23,                                                                                      \
669     0x07,                                                  /* bLength */                       \
670     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
671     int_ep,                                                /* bEndpointAddress */              \
672     0x03,                                                  /* bmAttributes */                  \
673     0x10, 0x00,                                            /* wMaxPacketSize */                \
674     0x10,                                                  /* bInterval */                     \
675     0x09,                                                  /* bLength */                       \
676     USB_DESCRIPTOR_TYPE_INTERFACE,                         /* bDescriptorType */               \
677     (uint8_t)(bFirstInterface + 1),                        /* bInterfaceNumber */              \
678     0x00,                                                  /* bAlternateSetting */             \
679     0x02,                                                  /* bNumEndpoints */                 \
680     CDC_DATA_INTERFACE_CLASS,                              /* bInterfaceClass */               \
681     0x00,                                                  /* bInterfaceSubClass */            \
682     0x00,                                                  /* bInterfaceProtocol */            \
683     0x00,                                                  /* iInterface */                    \
684     0x07,                                                  /* bLength */                       \
685     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
686     out_ep,                                                /* bEndpointAddress */              \
687     0x02,                                                  /* bmAttributes */                  \
688     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
689     0x00,                                                  /* bInterval */                     \
690     0x07,                                                  /* bLength */                       \
691     USB_DESCRIPTOR_TYPE_ENDPOINT,                          /* bDescriptorType */               \
692     in_ep,                                                 /* bEndpointAddress */              \
693     0x02,                                                  /* bmAttributes */                  \
694     WBVAL(wMaxPacketSize),                                 /* wMaxPacketSize */                \
695     0x00                                                   /* bInterval */
696 // clang-format on
697 
698 #endif /* USB_CDC_H */
699