1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 #include <inttypes.h>
9 
10 /**
11  * @brief USB CDC Descriptor Subtypes
12  *
13  * @see Table 13, USB CDC specification rev. 1.2
14  */
15 typedef enum {
16     CDC_DESC_SUBTYPE_HEADER = 0x00,             // Header Functional Descriptor
17     CDC_DESC_SUBTYPE_CALL = 0x01,               // Call Management Functional Descriptor
18     CDC_DESC_SUBTYPE_ACM = 0x02,                // Abstract Control Management Functional Descriptor
19     CDC_DESC_SUBTYPE_DLM = 0x03,                // Direct Line Management Functional Descriptor
20     CDC_DESC_SUBTYPE_TEL_RINGER = 0x04,         // Telephone Ringer Functional Descriptor
21     CDC_DESC_SUBTYPE_TEL_CLSR = 0x05,           // Telephone Call and Line State Reporting Capabilities Functional Descriptor
22     CDC_DESC_SUBTYPE_UNION = 0x06,              // Union Functional Descriptor
23     CDC_DESC_SUBTYPE_COUNTRY = 0x07,            // Country Selection Functional Descriptor
24     CDC_DESC_SUBTYPE_TEL_MODE = 0x08,           // Telephone Operational Modes Functional Descriptor
25     CDC_DESC_SUBTYPE_TERMINAL = 0x09,           // USB Terminal
26     CDC_DESC_SUBTYPE_NCHT = 0x0A,               // Network Channel Terminal
27     CDC_DESC_SUBTYPE_PROTOCOL = 0x08,           // Protocol Unit
28     CDC_DESC_SUBTYPE_EXTENSION = 0x0C,          // Extension Unit
29     CDC_DESC_SUBTYPE_MULTI_CHAN = 0x0D,         // Multi-Channel Management Functional Descriptor
30     CDC_DESC_SUBTYPE_CAPI = 0x0E,               // CAPI Control
31     CDC_DESC_SUBTYPE_ETH = 0x0F,                // Ethernet Networking
32     CDC_DESC_SUBTYPE_ATM = 0x10,                // ATM Networking
33     CDC_DESC_SUBTYPE_WHANDSET = 0x11,           // Wireless Handset Control Model Functional Descriptor
34     CDC_DESC_SUBTYPE_MDLM = 0x12,               // Mobile Direct Line Model
35     CDC_DESC_SUBTYPE_MDLM_DETAIL = 0x13,        // MDLM Detail
36     CDC_DESC_SUBTYPE_DMM = 0x14,                // Device Management Model
37     CDC_DESC_SUBTYPE_OBEX = 0x15,               // OBEX Functional
38     CDC_DESC_SUBTYPE_COMMAND_SET = 0x16,        // Command Set
39     CDC_DESC_SUBTYPE_COMMAND_SET_DETAIL = 0x17, // Command Set Detail Functional Descriptor
40     CDC_DESC_SUBTYPE_TEL_CM = 0x18,             // Telephone Control Model Functional Descriptor
41     CDC_DESC_SUBTYPE_OBEX_SERVICE = 0x19,       // OBEX Service Identifier Functional Descriptor
42     CDC_DESC_SUBTYPE_NCM = 0x1A                 // NCM Functional Descriptor
43 } __attribute__((packed)) cdc_desc_subtype_t;
44 
45 /**
46  * @brief USB CDC Subclass codes
47  *
48  * @see Table 4, USB CDC specification rev. 1.2
49  */
50 typedef enum {
51     CDC_SUBCLASS_DLCM = 0x01,    // Direct Line Control Model
52     CDC_SUBCLASS_ACM = 0x02,     // Abstract Control Model
53     CDC_SUBCLASS_TCM = 0x03,     // Telephone Control Model
54     CDC_SUBCLASS_MCHCM = 0x04,   // Multi-Channel Control Model
55     CDC_SUBCLASS_CAPI = 0x05,    // CAPI Control Model
56     CDC_SUBCLASS_ECM = 0x06,     // Ethernet Networking Control Model
57     CDC_SUBCLASS_ATM = 0x07,     // ATM Networking Model
58     CDC_SUBCLASS_HANDSET = 0x08, // Wireless Handset Control Model
59     CDC_SUBCLASS_DEV_MAN = 0x09, // Device Management
60     CDC_SUBCLASS_MOBILE = 0x0A,  // Mobile Direct Line Model
61     CDC_SUBCLASS_OBEX = 0x0B,    // OBEX
62     CDC_SUBCLASS_EEM = 0x0C,     // Ethernet Emulation Model
63     CDC_SUBCLASS_NCM = 0x0D      // Network Control Model
64 } __attribute__((packed)) cdc_subclass_t;
65 
66 /**
67  * @brief USB CDC Communications Protocol Codes
68  *
69  * @see Table 5, USB CDC specification rev. 1.2
70  */
71 typedef enum {
72     CDC_COMM_PROTOCOL_NONE = 0x00,   // No class specific protocol required
73     CDC_COMM_PROTOCOL_V250 = 0x01,   // AT Commands: V.250 etc
74     CDC_COMM_PROTOCOL_PCAA = 0x02,   // AT Commands defined by PCCA-101
75     CDC_COMM_PROTOCOL_PCAA_A = 0x03, // AT Commands defined by PCAA-101 & Annex O
76     CDC_COMM_PROTOCOL_GSM = 0x04,    // AT Commands defined by GSM 07.07
77     CDC_COMM_PROTOCOL_3GPP = 0x05,   // AT Commands defined by 3GPP 27.007
78     CDC_COMM_PROTOCOL_TIA = 0x06,    // AT Commands defined by TIA for CDMA
79     CDC_COMM_PROTOCOL_EEM = 0x07,    // Ethernet Emulation Model
80     CDC_COMM_PROTOCOL_EXT = 0xFE,    // External Protocol: Commands defined by Command Set Functional Descriptor
81     CDC_COMM_PROTOCOL_VENDOR = 0xFF  // Vendor-specific
82 } __attribute__((packed)) cdc_comm_protocol_t;
83 
84 /**
85  * @brief USB CDC Data Protocol Codes
86  *
87  * @see Table 7, USB CDC specification rev. 1.2
88  */
89 typedef enum {
90     CDC_DATA_PROTOCOL_NONE = 0x00,   // No class specific protocol required
91     CDC_DATA_PROTOCOL_NCM = 0x01,    // Network Transfer Block
92     CDC_DATA_PROTOCOL_I430 = 0x30,   // Physical interface protocol for ISDN BRI
93     CDC_DATA_PROTOCOL_HDLC = 0x31,   // HDLC
94     CDC_DATA_PROTOCOL_Q921M = 0x50,  // Management protocol for Q.921 data link protocol
95     CDC_DATA_PROTOCOL_Q921  = 0x51,  // Data link protocol for Q.931
96     CDC_DATA_PROTOCOL_Q921TM = 0x52, // TEI-multiplexor for Q.921 data link protocol
97     CDC_DATA_PROTOCOL_V42BIS = 0x90, // Data compression procedures
98     CDC_DATA_PROTOCOL_Q931 = 0x91,   // Euro-ISDN protocol control
99     CDC_DATA_PROTOCOL_V120 = 0x92,   // V.24 rate adaptation to ISDN
100     CDC_DATA_PROTOCOL_CAPI = 0x93,   // CAPI Commands
101     CDC_DATA_PROTOCOL_VENDOR = 0xFF  // Vendor-specific
102 } __attribute__((packed)) cdc_data_protocol_t;
103 
104 /**
105  * @brief USB CDC Request Codes
106  *
107  * @see Table 19, USB CDC specification rev. 1.2
108  */
109 typedef enum {
110     CDC_REQ_SEND_ENCAPSULATED_COMMAND = 0x00,
111     CDC_REQ_GET_ENCAPSULATED_RESPONSE = 0x01,
112     CDC_REQ_SET_COMM_FEATURE = 0x02,
113     CDC_REQ_GET_COMM_FEATURE = 0x03,
114     CDC_REQ_CLEAR_COMM_FEATURE = 0x04,
115     CDC_REQ_SET_AUX_LINE_STATE = 0x10,
116     CDC_REQ_SET_HOOK_STATE = 0x11,
117     CDC_REQ_PULSE_SETUP = 0x12,
118     CDC_REQ_SEND_PULSE = 0x13,
119     CDC_REQ_SET_PULSE_TIME = 0x14,
120     CDC_REQ_RING_AUX_JACK = 0x15,
121     CDC_REQ_SET_LINE_CODING = 0x20,
122     CDC_REQ_GET_LINE_CODING = 0x21,
123     CDC_REQ_SET_CONTROL_LINE_STATE = 0x22,
124     CDC_REQ_SEND_BREAK = 0x23,
125     CDC_REQ_SET_RINGER_PARMS = 0x30,
126     CDC_REQ_GET_RINGER_PARMS = 0x31,
127     CDC_REQ_SET_OPERATION_PARMS = 0x32,
128     CDC_REQ_GET_OPERATION_PARMS = 0x33,
129     CDC_REQ_SET_LINE_PARMS = 0x34,
130     CDC_REQ_GET_LINE_PARMS = 0x35,
131     CDC_REQ_DIAL_DIGITS = 0x36,
132     CDC_REQ_SET_UNIT_PARAMETER = 0x37,
133     CDC_REQ_GET_UNIT_PARAMETER = 0x38,
134     CDC_REQ_CLEAR_UNIT_PARAMETER = 0x39,
135     CDC_REQ_GET_PROFILE = 0x3A,
136     CDC_REQ_SET_ETHERNET_MULTICAST_FILTERS = 0x40,
137     CDC_REQ_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x41,
138     CDC_REQ_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x42,
139     CDC_REQ_SET_ETHERNET_PACKET_FILTER = 0x43,
140     CDC_REQ_GET_ETHERNET_STATISTIC = 0x44,
141     CDC_REQ_SET_ATM_DATA_FORMAT = 0x50,
142     CDC_REQ_GET_ATM_DEVICE_STATISTICS = 0x51,
143     CDC_REQ_SET_ATM_DEFAULT_VC = 0x52,
144     CDC_REQ_GET_ATM_VC_STATISTICS = 0x53,
145     CDC_REQ_GET_NTB_PARAMETERS = 0x80,
146     CDC_REQ_GET_NET_ADDRESS = 0x81,
147     CDC_REQ_SET_NET_ADDRESS = 0x82,
148     CDC_REQ_GET_NTB_FORMAT = 0x83,
149     CDC_REQ_SET_NTB_FORMAT = 0x84,
150     CDC_REQ_GET_NTB_INPUT_SIZE = 0x85,
151     CDC_REQ_SET_NTB_INPUT_SIZE = 0x86,
152     CDC_REQ_GET_MAX_DATAGRAM_SIZE = 0x87,
153     CDC_REQ_SET_MAX_DATAGRAM_SIZE = 0x88,
154     CDC_REQ_GET_CRC_MODE = 0x89,
155     CDC_REQ_SET_CRC_MODE = 0x8A
156 } __attribute__((packed)) cdc_request_code_t;
157 
158 /**
159  * @brief USB CDC Notification Codes
160  *
161  * @see Table 20, USB CDC specification rev. 1.2
162  */
163 typedef enum {
164     CDC_NOTIF_NETWORK_CONNECTION = 0x00,
165     CDC_NOTIF_RESPONSE_AVAILABLE = 0x01,
166     CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
167     CDC_NOTIF_RING_DETECT = 0x09,
168     CDC_NOTIF_SERIAL_STATE = 0x20,
169     CDC_NOTIF_CALL_STATE_CHANGE = 0x28,
170     CDC_NOTIF_LINE_STATE_CHANGE = 0x29,
171     CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A
172 } __attribute__((packed)) cdc_notification_code_t;
173 
174 typedef struct {
175     uint8_t bmRequestType;
176     cdc_notification_code_t bNotificationCode;
177     uint16_t wValue;
178     uint16_t wIndex;
179     uint16_t wLength;
180     uint8_t Data[];
181 } __attribute__((packed)) cdc_notification_t;
182 
183 /**
184  * @brief USB CDC Header Functional Descriptor
185  *
186  * @see Table 15, USB CDC specification rev. 1.2
187  */
188 typedef struct {
189     uint8_t bFunctionLength;
190     const uint8_t bDescriptorType; // Upper nibble: CDC code 0x02, Lower nibble: intf/ep descriptor type 0x04/0x05
191     const cdc_desc_subtype_t bDescriptorSubtype;
192     uint16_t bcdCDC; // CDC version as binary-coded decimal. This driver is written for version 1.2
193 } __attribute__((packed)) cdc_header_desc_t;
194 
195 /**
196  * @brief USB CDC Union Functional Descriptor
197  *
198  * @see Table 16, USB CDC specification rev. 1.2
199  */
200 typedef struct {
201     uint8_t bFunctionLength;
202     const uint8_t bDescriptorType; // Upper nibble: CDC code 0x02, Lower nibble: intf/ep descriptor type 0x04/0x05
203     const cdc_desc_subtype_t bDescriptorSubtype;
204     const uint8_t bControlInterface; // Master/controlling interface
205     uint8_t bSubordinateInterface[]; // Slave/subordinate interfaces
206 } __attribute__((packed)) cdc_union_desc_t;
207