1
0

usb_cdc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* This file is the part of the LUS32 project
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _USB_CDC_H_
  16. #define _USB_CDC_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**\addtogroup USB_CDC USB CDC class
  21. * \brief Generic USB CDC class definitions
  22. * \details This module based on
  23. * + Universal Serial Bus Class Definitions for Communications Devices Revision 1.2 (Errata 1)
  24. * @{ */
  25. /**\name USB CDC Class codes
  26. * @{ */
  27. #define USB_CLASS_CDC 0x02 /**< Device/Interface belongs to the Communicational device class */
  28. #define USB_CLASS_CDC_DATA 0x0A /**< Interface belongs to the Data Interface class */
  29. /** @} */
  30. /**\name USB CDC subclass codes
  31. * @{ */
  32. #define USB_CDC_SUBCLASS_ACM 0x02 /**< Abstract Control Model */
  33. /** @} */
  34. /**\name Communications Class Protocol Codes
  35. * @{ */
  36. #define USB_CDC_PROTO_V25TER 0x01 /**< AT Commands: V.250 etc */
  37. /** @} */
  38. /** \name Data Interface Class Protocol Codes
  39. * @{ */
  40. #define USB_CDC_PROTO_NTB 0x01 /**< Network Transfer Block */
  41. #define USB_CDC_PROTO_HOST 0xFD /**< Host based driver. \note This protocol code should only be used in messages between
  42. * host and device to identify the host driver portion of a protocol stack.*/
  43. #define USB_CDC_PROTO_CDCSPEC 0xFE /**< The protocol(s) are described using a Protocol Unit Functional Descriptors on Communication Class Interface.*/
  44. /** @} */
  45. /**\name USB CDC class-specified functional descriptors
  46. * @{ */
  47. #define USB_DTYPE_CDC_HEADER 0x00 /**< Header Functional Descriptor. \ref usb_cdc_header_desc */
  48. #define USB_DTYPE_CDC_CALL_MANAGEMENT 0x01 /**< Call Management Functional Descriptor. \ref usb_cdc_call_mgmt_desc */
  49. #define USB_DTYPE_CDC_ACM 0x02 /**< Abstract Control Management Functional Descriptor. \ref usb_cdc_acm_desc */
  50. #define USB_DTYPE_CDC_UNION 0x06 /**< Union Functional descriptor */
  51. #define USB_DTYPE_CDC_COUNTRY 0x07 /**< Country Selection Functional Descriptor */
  52. /** @} */
  53. /** \name USB CDC class-specific requests
  54. * @{ */
  55. #define USB_CDC_SEND_ENCAPSULATED_CMD 0x00 /**< Used to issue a command in the format of the supported control protocol of the Communication Class interface. */
  56. #define USB_CDC_GET_ENCAPSULATED_RESP 0x01 /**< Used to request a response in the format of the supported control protocol of the Communication Class interface. */
  57. #define USB_CDC_SET_COMM_FEATURE 0x02 /**< Controls the settings for a particular communication feature of a particular target */
  58. #define USB_CDC_GET_COMM_FEATURE 0x03 /**< Returns the current settings for the communication feature as selected */
  59. #define USB_CDC_CLEAR_COMM_FEATURE 0x04 /**< Controls the settings for a particular communication feature of a particular
  60. * target, setting the selected feature to its default state. */
  61. #define USB_CDC_SET_LINE_CODING 0x20 /**< Allows the host to specify typical asynchronous line-character formatting properties. */
  62. #define USB_CDC_GET_LINE_CODING 0x21 /**< Allows the host to find out the currently configured line coding. */
  63. #define USB_CDC_SET_CONTROL_LINE_STATE 0x22 /**< Generates RS-232/V.24 style control signals. */
  64. #define USB_CDC_SEND_BREAK 0x23 /**< Sends special carrier modulation that generates an RS-232 style break. */
  65. /** @} */
  66. /**\name Generic CDC specific notifications
  67. * @{ */
  68. #define USB_CDC_NTF_NETWORK_CONNECTION 0x00 /**< Allows the device to notify the host about network connection status. */
  69. #define USB_CDC_NTF_RESPONSE_AVAILABLE 0x01 /**< Allows the device to notify the host that a response is available. */
  70. #define USB_CDC_NTF_SERIAL_STATE 0x20 /**< Sends asynchronous notification of UART status. */
  71. #define USB_CDC_NTF_SPEED_CHANGE 0x2A /**< allows the device to inform the host-networking driver that a change in either the uplink
  72. * or the downlink bit rate of the connection has occurred. */
  73. /** @} */
  74. /**\anchor USB_CDC_ACMGMNTCAP
  75. * \name USB CDC Abstract Control Management capabilities
  76. * @{ */
  77. #define USB_CDC_COMM_FEATURE 0x01 /**< Supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature. */
  78. #define USB_CDC_CAP_LINE 0x02 /**< Supports the request combination of Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State. */
  79. #define USB_CDC_CAP_BRK 0x04 /**< Supports the request Send_Break */
  80. #define USB_CDC_CAP_NOTIFY 0x08 /**< Supports notification Network_Connection. */
  81. /** @} */
  82. /**\anchor USB_CDC_CALLMGMTCAP
  83. * \name USB CDC Call Management capabilities
  84. * @{ */
  85. #define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01 /**< Device handles call management itself. */
  86. #define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02 /**< Device can send/receive call management information over a Data Class interface. */
  87. /** @} */
  88. /**\anchor USB_CDC_LINECODE
  89. * \name Line coding structire bit fields
  90. * @{ */
  91. #define USB_CDC_1_STOP_BITS 0x00
  92. #define USB_CDC_1_5_STOP_BITS 0x01
  93. #define USB_CDC_2_STOP_BITS 0x02
  94. #define USB_CDC_NO_PARITY 0x00
  95. #define USB_CDC_ODD_PARITY 0x01
  96. #define USB_CDC_EVEN_PARITY 0x02
  97. #define USB_CDC_MARK_PARITY 0x03
  98. #define USB_CDC_SPACE_PARITY 0x04
  99. /** @} */
  100. /**\name SERIAL_STATE notification data values
  101. * @{ */
  102. #define USB_CDC_STATE_RX_CARRIER 0x0001 /**< State of receiver carrier detection mechanism of device. This signal corresponds to V.24 signal 109 and RS-232 signal DCD. */
  103. #define USB_CDC_STATE_TX_CARRIER 0x0002 /**< State of transmission carrier. This signal corresponds to V.24 signal 106 and RS-232 signal DSR. */
  104. #define USB_CDC_STATE_BREAK 0x0004 /**< State of break detection mechanism of the device. */
  105. #define USB_CDC_STATE_RING 0x0008 /**< State of ring signal detection of the device. */
  106. #define USB_CDC_STATE_FRAMING 0x0010 /**< A framing error has occurred. */
  107. #define USB_CDC_STATE_PARITY 0x0020 /**< A parity error has occurred. */
  108. #define USB_CDC_STATE_OVERRUN 0x0040 /**< Received data has been discarded due to overrun in the device */
  109. /** @} */
  110. /** \brief Header Functional Descriptor
  111. * \details Header Functional Descriptor marks the beginning of the concatenated set of functional descriptors for the interface. */
  112. struct usb_cdc_header_desc {
  113. uint8_t bFunctionLength; /**< Size of this descriptor in bytes. */
  114. uint8_t bDescriptorType; /**< CS_INTERFACE descriptor type. \see \ref USB_DTYPE_CS_INTERFACE */
  115. uint8_t bDescriptorSubType; /**< Header functional descriptor subtype \ref USB_DTYPE_CDC_HEADER */
  116. uint16_t bcdCDC; /**< USB CDC Specification release number in binary-coded decimal. \see \ref VERSION_BCD macro */
  117. } __attribute__ ((packed));
  118. /** \brief Union Functional Descriptor
  119. * \details The Union functional descriptor describes the relationship between a group of interfaces that can be considered to form
  120. * a functional unit. It can only occur within the class-specific portion of an Interface descriptor. One of the interfaces in
  121. * the group is designated as a master or controlling interface for the group, and certain class-specific messages can be
  122. * sent to this interface to act upon the group as a whole.
  123. */
  124. struct usb_cdc_union_desc {
  125. uint8_t bFunctionLength; /**< Size of this functional descriptor, in bytes. */
  126. uint8_t bDescriptorType; /**< CS_INTERFACE descriptor type. \see \ref USB_DTYPE_CS_INTERFACE */
  127. uint8_t bDescriptorSubType; /**< Union Functional Descriptor. \ref USB_DTYPE_CDC_UNION */
  128. uint8_t bMasterInterface0; /**< The interface number of the CDC interface, designated as the master or controlling interface for the union. */
  129. uint8_t bSlaveInterface0; /**< Interface number of first slave or associated interface in the union. */
  130. /* ... and there could be other slave interfaces */
  131. } __attribute__ ((packed));
  132. /** \brief Country Selection Functional Descriptor
  133. * \details The Country Selection functional descriptor identifies the countries in which the communication device is qualified to
  134. * operate. The parameters of the network connection often vary from one country to another, especially in Europe. Also
  135. * legal requirements impose certain restrictions on devices because of different regulations by the governing body of the
  136. * network to which the device must adhere. This descriptor can only occur within the class-specific portion of an
  137. * Interface descriptor and should only be provided to a master Communication Class interface of a union. The country
  138. * codes used in the Country Selection Functional Descriptor are not the same as the country codes used in dialing
  139. * international telephone calls. Implementers should refer to the ISO 3166 specification for more information.
  140. */
  141. struct usb_cdc_country_desc {
  142. uint8_t bFunctionLength; /**< Size of this functional descriptor, in bytes. */
  143. uint8_t bDescriptorType; /**< CS_INTERFACE descriptor type. \see \ref USB_DTYPE_CS_INTERFACE */
  144. uint8_t bDescriptorSubType; /**< Country Selection Functional Descriptor \ref USB_DTYPE_CDC_COUNTRY */
  145. uint8_t iCountryCodeRelDate; /**< Index of a string giving the release date for the implemented ISO 3166 Country Codes. */
  146. uint8_t wCountyCode0; /**< Country code in hexadecimal format as defined in ISO 3166, release date as specified
  147. * in iCountryCodeRelDate for the first supported country. */
  148. /* ... and there can be a lot of country codes */
  149. } __attribute__ ((packed));
  150. /** \brief Call Management Functional Descriptor.
  151. * \details The Call Management functional descriptor describes the processing of calls for the Communication Class interface. It
  152. * can only occur within the class-specific portion of an Interface descriptor.
  153. */
  154. struct usb_cdc_call_mgmt_desc {
  155. uint8_t bFunctionLength; /**< Size of this functional descriptor, in bytes. */
  156. uint8_t bDescriptorType; /**< CS_INTERFACE descriptor type. \see \ref USB_DTYPE_CS_INTERFACE */
  157. uint8_t bDescriptorSubType; /**< Call Management functional descriptor subtype \ref USB_DTYPE_CDC_CALL_MANAGEMENT */
  158. uint8_t bmCapabilities; /**< The \ref USB_CDC_CALLMGMTCAP "capabilities" that this configuration supports */
  159. uint8_t bDataInterface; /**< Interface number of Data Class interface optionally used for call management. */
  160. } __attribute__ ((packed));
  161. /** \brief Abstract Control Management Functional Descriptor
  162. * \details The Abstract Control Management functional descriptor describes the commands supported by the Communication
  163. * Class interface, as defined in Section 3.6.2, with the SubClass code of Abstract Control Model. It can only occur
  164. * within the class-specific portion of an Interface descriptor.
  165. */
  166. struct usb_cdc_acm_desc {
  167. uint8_t bFunctionLength; /**< Size of this functional descriptor, in bytes. */
  168. uint8_t bDescriptorType; /**< CS_INTERFACE descriptor type. \see \ref USB_DTYPE_CS_INTERFACE */
  169. uint8_t bDescriptorSubType; /**< Abstract Control Management functional descriptor subtype \ref USB_DTYPE_CDC_ACM */
  170. uint8_t bmCapabilities; /**< The \ref USB_CDC_ACMGMNTCAP "capabilities" that this configuration supports */
  171. } __attribute__ ((packed));
  172. ///\brief Notification structure from CDC
  173. struct usb_cdc_notification {
  174. uint8_t bmRequestType;
  175. uint8_t bNotificationType;
  176. uint16_t wValue;
  177. uint16_t wIndex;
  178. uint16_t wLength;
  179. uint8_t Data[];
  180. } __attribute__ ((packed));
  181. /**\brief Line Coding Structure
  182. * \details used in GET_LINE_CODING and SET_LINE_CODING requests
  183. */
  184. struct usb_cdc_line_coding {
  185. uint32_t dwDTERate; /**< Data terminal rate, in bits per second. */
  186. uint8_t bCharFormat; /**< Stop bits */
  187. uint8_t bParityType; /**< Parity */
  188. uint8_t bDataBits; /**< Data bits (5,6,7,8 or 16) */
  189. } __attribute__ ((packed));
  190. /** @} */
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif /* _USB_CDC_H_ */