usb_cdc.h 44 KB

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