You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
9.3 KiB

  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : support@circuitsathome.com
  13. */
  14. #if !defined(__CDCACM_H__)
  15. #define __CDCACM_H__
  16. #include "Usb.h"
  17. #define bmREQ_CDCOUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
  18. #define bmREQ_CDCIN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
  19. // CDC Subclass Constants
  20. #define CDC_SUBCLASS_DLCM 0x01 // Direct Line Control Model
  21. #define CDC_SUBCLASS_ACM 0x02 // Abstract Control Model
  22. #define CDC_SUBCLASS_TCM 0x03 // Telephone Control Model
  23. #define CDC_SUBCLASS_MCCM 0x04 // Multi Channel Control Model
  24. #define CDC_SUBCLASS_CAPI 0x05 // CAPI Control Model
  25. #define CDC_SUBCLASS_ETHERNET 0x06 // Ethernet Network Control Model
  26. #define CDC_SUBCLASS_ATM 0x07 // ATM Network Control Model
  27. #define CDC_SUBCLASS_WIRELESS_HANDSET 0x08 // Wireless Handset Control Model
  28. #define CDC_SUBCLASS_DEVICE_MANAGEMENT 0x09 // Device Management
  29. #define CDC_SUBCLASS_MOBILE_DIRECT_LINE 0x0A // Mobile Direct Line Model
  30. #define CDC_SUBCLASS_OBEX 0x0B // OBEX
  31. #define CDC_SUBCLASS_ETHERNET_EMU 0x0C // Ethernet Emulation Model
  32. // Communication Interface Class Control Protocol Codes
  33. #define CDC_PROTOCOL_ITU_T_V_250 0x01 // AT Commands defined by ITU-T V.250
  34. #define CDC_PROTOCOL_PCCA_101 0x02 // AT Commands defined by PCCA-101
  35. #define CDC_PROTOCOL_PCCA_101_O 0x03 // AT Commands defined by PCCA-101 & Annex O
  36. #define CDC_PROTOCOL_GSM_7_07 0x04 // AT Commands defined by GSM 7.07
  37. #define CDC_PROTOCOL_3GPP_27_07 0x05 // AT Commands defined by 3GPP 27.007
  38. #define CDC_PROTOCOL_C_S0017_0 0x06 // AT Commands defined by TIA for CDMA
  39. #define CDC_PROTOCOL_USB_EEM 0x07 // Ethernet Emulation Model
  40. // CDC Commands defined by CDC 1.2
  41. #define CDC_SEND_ENCAPSULATED_COMMAND 0x00
  42. #define CDC_GET_ENCAPSULATED_RESPONSE 0x01
  43. // CDC Commands defined by PSTN 1.2
  44. #define CDC_SET_COMM_FEATURE 0x02
  45. #define CDC_GET_COMM_FEATURE 0x03
  46. #define CDC_CLEAR_COMM_FEATURE 0x04
  47. #define CDC_SET_AUX_LINE_STATE 0x10
  48. #define CDC_SET_HOOK_STATE 0x11
  49. #define CDC_PULSE_SETUP 0x12
  50. #define CDC_SEND_PULSE 0x13
  51. #define CDC_SET_PULSE_TIME 0x14
  52. #define CDC_RING_AUX_JACK 0x15
  53. #define CDC_SET_LINE_CODING 0x20
  54. #define CDC_GET_LINE_CODING 0x21
  55. #define CDC_SET_CONTROL_LINE_STATE 0x22
  56. #define CDC_SEND_BREAK 0x23
  57. #define CDC_SET_RINGER_PARMS 0x30
  58. #define CDC_GET_RINGER_PARMS 0x31
  59. #define CDC_SET_OPERATION_PARMS 0x32
  60. #define CDC_GET_OPERATION_PARMS 0x33
  61. #define CDC_SET_LINE_PARMS 0x34
  62. #define CDC_GET_LINE_PARMS 0x35
  63. #define CDC_DIAL_DIGITS 0x36
  64. //Class-Specific Notification Codes
  65. #define NETWORK_CONNECTION 0x00
  66. #define RESPONSE_AVAILABLE 0x01
  67. #define AUX_JACK_HOOK_STATE 0x08
  68. #define RING_DETECT 0x09
  69. #define SERIAL_STATE 0x20
  70. #define CALL_STATE_CHANGE 0x28
  71. #define LINE_STATE_CHANGE 0x29
  72. #define CONNECTION_SPEED_CHANGE 0x2a
  73. // CDC Functional Descriptor Structures
  74. typedef struct {
  75. uint8_t bFunctionLength;
  76. uint8_t bDescriptorType;
  77. uint8_t bDescriptorSubtype;
  78. uint8_t bmCapabilities;
  79. uint8_t bDataInterface;
  80. } CALL_MGMNT_FUNC_DESCR;
  81. typedef struct {
  82. uint8_t bFunctionLength;
  83. uint8_t bDescriptorType;
  84. uint8_t bDescriptorSubtype;
  85. uint8_t bmCapabilities;
  86. } ACM_FUNC_DESCR, DLM_FUNC_DESCR, TEL_OPER_MODES_FUNC_DESCR,
  87. TEL_CALL_STATE_REP_CPBL_FUNC_DESCR;
  88. typedef struct {
  89. uint8_t bFunctionLength;
  90. uint8_t bDescriptorType;
  91. uint8_t bDescriptorSubtype;
  92. uint8_t bRingerVolSteps;
  93. uint8_t bNumRingerPatterns;
  94. } TEL_RINGER_FUNC_DESCR;
  95. typedef struct {
  96. uint32_t dwDTERate; // Data Terminal Rate in bits per second
  97. uint8_t bCharFormat; // 0 - 1 stop bit, 1 - 1.5 stop bits, 2 - 2 stop bits
  98. uint8_t bParityType; // 0 - None, 1 - Odd, 2 - Even, 3 - Mark, 4 - Space
  99. uint8_t bDataBits; // Data bits (5, 6, 7, 8 or 16)
  100. } LINE_CODING;
  101. typedef struct {
  102. uint8_t bmRequestType; // 0xa1 for class-specific notifications
  103. uint8_t bNotification;
  104. uint16_t wValue;
  105. uint16_t wIndex;
  106. uint16_t wLength;
  107. uint16_t bmState; //UART state bitmap for SERIAL_STATE, other notifications variable length
  108. } CLASS_NOTIFICATION;
  109. class ACM;
  110. class CDCAsyncOper {
  111. public:
  112. virtual uint8_t OnInit(ACM *pacm) {
  113. return 0;
  114. };
  115. //virtual void OnDataRcvd(ACM *pacm, uint8_t nbytes, uint8_t *dataptr) = 0;
  116. //virtual void OnDisconnected(ACM *pacm) = 0;
  117. };
  118. /**
  119. * This structure is used to report the extended capabilities of the connected device.
  120. * It is also used to report the current status.
  121. * Regular CDC-ACM reports all as false.
  122. */
  123. typedef struct {
  124. union {
  125. uint8_t tty;
  126. struct {
  127. bool enhanced : 1; // Do we have the ability to set/clear any features?
  128. // Status and 8th bit in data stream.
  129. // Presence only indicates feature is available, but this isn't used for CDC-ACM.
  130. bool wide : 1;
  131. bool autoflow_RTS : 1; // Has autoflow on RTS/CTS
  132. bool autoflow_DSR : 1; // Has autoflow on DTR/DSR
  133. bool autoflow_XON : 1; // Has autoflow XON/XOFF
  134. bool half_duplex : 1; // Has half-duplex capability.
  135. } __attribute__((packed));
  136. };
  137. } tty_features;
  138. #define ACM_MAX_ENDPOINTS 4
  139. class ACM : public USBDeviceConfig, public UsbConfigXtracter {
  140. protected:
  141. static const uint8_t epDataInIndex; // DataIn endpoint index
  142. static const uint8_t epDataOutIndex; // DataOUT endpoint index
  143. static const uint8_t epInterruptInIndex; // InterruptIN endpoint index
  144. USB *pUsb;
  145. CDCAsyncOper *pAsync;
  146. uint8_t bAddress;
  147. uint8_t bConfNum; // configuration number
  148. uint8_t bControlIface; // Control interface value
  149. uint8_t bDataIface; // Data interface value
  150. uint8_t bNumEP; // total number of EP in the configuration
  151. uint32_t qNextPollTime; // next poll time
  152. volatile bool bPollEnable; // poll enable flag
  153. volatile bool ready; //device ready indicator
  154. tty_features _enhanced_status; // current status
  155. EpInfo epInfo[ACM_MAX_ENDPOINTS];
  156. void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
  157. public:
  158. ACM(USB *pusb, CDCAsyncOper *pasync);
  159. uint8_t SetCommFeature(uint16_t fid, uint8_t nbytes, uint8_t *dataptr);
  160. uint8_t GetCommFeature(uint16_t fid, uint8_t nbytes, uint8_t *dataptr);
  161. uint8_t ClearCommFeature(uint16_t fid);
  162. uint8_t SetLineCoding(const LINE_CODING *dataptr);
  163. uint8_t GetLineCoding(LINE_CODING *dataptr);
  164. uint8_t SetControlLineState(uint8_t state);
  165. uint8_t SendBreak(uint16_t duration);
  166. uint8_t GetNotif(uint16_t *bytes_rcvd, uint8_t *dataptr);
  167. // Methods for receiving and sending data
  168. uint8_t RcvData(uint16_t *nbytesptr, uint8_t *dataptr);
  169. uint8_t SndData(uint16_t nbytes, uint8_t *dataptr);
  170. // USBDeviceConfig implementation
  171. uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
  172. uint8_t Release();
  173. uint8_t Poll();
  174. bool available(void) {
  175. return false;
  176. };
  177. virtual uint8_t GetAddress() {
  178. return bAddress;
  179. };
  180. virtual bool isReady() {
  181. return ready;
  182. };
  183. virtual tty_features enhanced_status(void) {
  184. return _enhanced_status;
  185. };
  186. virtual tty_features enhanced_features(void) {
  187. tty_features rv;
  188. rv.enhanced = false;
  189. rv.autoflow_RTS = false;
  190. rv.autoflow_DSR = false;
  191. rv.autoflow_XON = false;
  192. rv.half_duplex = false;
  193. rv.wide = false;
  194. return rv;
  195. };
  196. virtual void autoflowRTS(bool s) {
  197. };
  198. virtual void autoflowDSR(bool s) {
  199. };
  200. virtual void autoflowXON(bool s) {
  201. };
  202. virtual void half_duplex(bool s) {
  203. };
  204. virtual void wide(bool s) {
  205. };
  206. // UsbConfigXtracter implementation
  207. void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
  208. };
  209. #endif // __CDCACM_H__