usb_cdc.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2017-2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. #ifndef _USB_CDC_H_
  4. #define _USB_CDC_H_
  5. /*- Includes ----------------------------------------------------------------*/
  6. #include "usb_std.h"
  7. /*- Definitions -------------------------------------------------------------*/
  8. #define USB_CDC_BCD_VERSION 0x0110
  9. enum
  10. {
  11. USB_CDC_SEND_ENCAPSULATED_COMMAND = 0x00,
  12. USB_CDC_GET_ENCAPSULATED_RESPONSE = 0x01,
  13. USB_CDC_SET_COMM_FEATURE = 0x02,
  14. USB_CDC_GET_COMM_FEATURE = 0x03,
  15. USB_CDC_CLEAR_COMM_FEATURE = 0x04,
  16. USB_CDC_SET_AUX_LINE_STATE = 0x10,
  17. USB_CDC_SET_HOOK_STATE = 0x11,
  18. USB_CDC_PULSE_SETUP = 0x12,
  19. USB_CDC_SEND_PULSE = 0x13,
  20. USB_CDC_SET_PULSE_TIME = 0x14,
  21. USB_CDC_RING_AUX_JACK = 0x15,
  22. USB_CDC_SET_LINE_CODING = 0x20,
  23. USB_CDC_GET_LINE_CODING = 0x21,
  24. USB_CDC_SET_CONTROL_LINE_STATE = 0x22,
  25. USB_CDC_SEND_BREAK = 0x23,
  26. USB_CDC_SET_RINGER_PARMS = 0x30,
  27. USB_CDC_GET_RINGER_PARMS = 0x31,
  28. USB_CDC_SET_OPERATION_PARMS = 0x32,
  29. USB_CDC_GET_OPERATION_PARMS = 0x33,
  30. USB_CDC_SET_LINE_PARMS = 0x34,
  31. USB_CDC_GET_LINE_PARMS = 0x35,
  32. USB_CDC_DIAL_DIGITS = 0x36,
  33. USB_CDC_SET_UNIT_PARAMETER = 0x37,
  34. USB_CDC_GET_UNIT_PARAMETER = 0x38,
  35. USB_CDC_CLEAR_UNIT_PARAMETER = 0x39,
  36. USB_CDC_GET_PROFILE = 0x3a,
  37. USB_CDC_NOTIFY_RING_DETECT = 0x09,
  38. USB_CDC_NOTIFY_SERIAL_STATE = 0x20,
  39. USB_CDC_NOTIFY_CALL_STATE_CHANGE = 0x28,
  40. USB_CDC_NOTIFY_LINE_STATE_CHANGE = 0x29,
  41. };
  42. enum
  43. {
  44. USB_CDC_1_STOP_BIT = 0,
  45. USB_CDC_1_5_STOP_BITS = 1,
  46. USB_CDC_2_STOP_BITS = 2,
  47. };
  48. enum
  49. {
  50. USB_CDC_NO_PARITY = 0,
  51. USB_CDC_ODD_PARITY = 1,
  52. USB_CDC_EVEN_PARITY = 2,
  53. USB_CDC_MARK_PARITY = 3,
  54. USB_CDC_SPACE_PARITY = 4,
  55. };
  56. enum
  57. {
  58. USB_CDC_5_DATA_BITS = 5,
  59. USB_CDC_6_DATA_BITS = 6,
  60. USB_CDC_7_DATA_BITS = 7,
  61. USB_CDC_8_DATA_BITS = 8,
  62. USB_CDC_16_DATA_BITS = 16,
  63. };
  64. enum
  65. {
  66. USB_CDC_DEVICE_CLASS = 2, // USB Communication Device Class
  67. USB_CDC_COMM_CLASS = 2, // CDC Communication Class Interface
  68. USB_CDC_DATA_CLASS = 10, // CDC Data Class Interface
  69. };
  70. enum
  71. {
  72. USB_CDC_NO_SUBCLASS = 0,
  73. USB_CDC_DLCM_SUBCLASS = 1, // Direct Line Control Model
  74. USB_CDC_ACM_SUBCLASS = 2, // Abstract Control Model
  75. USB_CDC_TCM_SUBCLASS = 3, // Telephone Control Model
  76. USB_CDC_MCCM_SUBCLASS = 4, // Multi-Channel Control Model
  77. USB_CDC_CCM_SUBCLASS = 5, // CAPI Control Model
  78. USB_CDC_ETH_SUBCLASS = 6, // Ethernet Networking Control Model
  79. USB_CDC_ATM_SUBCLASS = 7, // ATM Networking Control Model
  80. };
  81. enum
  82. {
  83. USB_CDC_HEADER_SUBTYPE = 0, // Header Functional Descriptor
  84. USB_CDC_CALL_MGMT_SUBTYPE = 1, // Call Management
  85. USB_CDC_ACM_SUBTYPE = 2, // Abstract Control Management
  86. USB_CDC_UNION_SUBTYPE = 6, // Union Functional Descriptor
  87. };
  88. // USB CDC Call Management Capabilities
  89. enum
  90. {
  91. USB_CDC_CALL_MGMT_SUPPORTED = (1 << 0),
  92. USB_CDC_CALL_MGMT_OVER_DCI = (1 << 1),
  93. };
  94. // USB CDC ACM Capabilities
  95. enum
  96. {
  97. // Device supports the request combination of Set_Comm_Feature,
  98. // Clear_Comm_Feature, and Get_Comm_Feature.
  99. USB_CDC_ACM_SUPPORT_FEATURE_REQUESTS = (1 << 0),
  100. // Device supports the request combination of Set_Line_Coding, Set_Control_Line_State,
  101. // Get_Line_Coding, and the notification Serial_State.
  102. USB_CDC_ACM_SUPPORT_LINE_REQUESTS = (1 << 1),
  103. // Device supports the request Send_Break.
  104. USB_CDC_ACM_SUPPORT_SENDBREAK_REQUESTS = (1 << 2),
  105. // Device supports the notification Network_Connection.
  106. USB_CDC_ACM_SUPPORT_NOTIFY_REQUESTS = (1 << 3),
  107. };
  108. enum
  109. {
  110. USB_CDC_CTRL_SIGNAL_DTE_PRESENT = (1 << 0), // DTR
  111. USB_CDC_CTRL_SIGNAL_ACTIVATE_CARRIER = (1 << 1), // RTS
  112. };
  113. enum
  114. {
  115. USB_CDC_SERIAL_STATE_DCD = (1 << 0),
  116. USB_CDC_SERIAL_STATE_DSR = (1 << 1),
  117. USB_CDC_SERIAL_STATE_BREAK = (1 << 2),
  118. USB_CDC_SERIAL_STATE_RING = (1 << 3),
  119. USB_CDC_SERIAL_STATE_FRAMING = (1 << 4),
  120. USB_CDC_SERIAL_STATE_PARITY = (1 << 5),
  121. USB_CDC_SERIAL_STATE_OVERRUN = (1 << 6),
  122. };
  123. #define USB_CDC_BREAK_DURATION_DISABLE 0
  124. #define USB_CDC_BREAK_DURATION_INFINITE 0xffff
  125. /*- Types -------------------------------------------------------------------*/
  126. typedef struct USB_PACK
  127. {
  128. uint8_t bFunctionalLength;
  129. uint8_t bDescriptorType;
  130. uint8_t bDescriptorSubtype;
  131. uint16_t bcdCDC;
  132. } usb_cdc_header_functional_descriptor_t;
  133. typedef struct USB_PACK
  134. {
  135. uint8_t bFunctionalLength;
  136. uint8_t bDescriptorType;
  137. uint8_t bDescriptorSubtype;
  138. uint8_t bmCapabilities;
  139. } usb_cdc_abstract_control_managment_descriptor_t;
  140. typedef struct USB_PACK
  141. {
  142. uint8_t bFunctionalLength;
  143. uint8_t bDescriptorType;
  144. uint8_t bDescriptorSubtype;
  145. uint8_t bmCapabilities;
  146. uint8_t bDataInterface;
  147. } usb_cdc_call_managment_functional_descriptor_t;
  148. typedef struct USB_PACK
  149. {
  150. uint8_t bFunctionalLength;
  151. uint8_t bDescriptorType;
  152. uint8_t bDescriptorSubtype;
  153. uint8_t bMasterInterface;
  154. uint8_t bSlaveInterface0;
  155. } usb_cdc_union_functional_descriptor_t;
  156. typedef struct USB_PACK
  157. {
  158. uint32_t dwDTERate;
  159. uint8_t bCharFormat;
  160. uint8_t bParityType;
  161. uint8_t bDataBits;
  162. } usb_cdc_line_coding_t;
  163. typedef struct USB_PACK
  164. {
  165. usb_request_t request;
  166. uint16_t value;
  167. } usb_cdc_notify_serial_state_t;
  168. /*- Prototypes --------------------------------------------------------------*/
  169. void usb_cdc_init(void);
  170. bool usb_cdc_handle_request(usb_request_t *request);
  171. void usb_cdc_send(uint8_t *data, int size);
  172. void usb_cdc_recv(uint8_t *data, int size);
  173. void usb_cdc_set_state(int mask);
  174. void usb_cdc_clear_state(int mask);
  175. usb_cdc_line_coding_t *usb_cdc_get_line_coding(void);
  176. void usb_cdc_send_callback(void);
  177. void usb_cdc_recv_callback(int size);
  178. void usb_cdc_line_coding_updated(usb_cdc_line_coding_t *line_coding);
  179. void usb_cdc_control_line_state_update(int line_state);
  180. void usb_cdc_send_break(int duration);
  181. #endif // _USB_CDC_H_