Keyboard firmwares for Atmel AVR and Cortex-M
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.

CDCClassDevice.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #define __INCLUDE_FROM_USB_DRIVER
  27. #include "../../Core/USBMode.h"
  28. #if defined(USB_CAN_BE_DEVICE)
  29. #define __INCLUDE_FROM_CDC_DRIVER
  30. #define __INCLUDE_FROM_CDC_DEVICE_C
  31. #include "CDCClassDevice.h"
  32. void CDC_Device_ProcessControlRequest(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  33. {
  34. if (!(Endpoint_IsSETUPReceived()))
  35. return;
  36. if (USB_ControlRequest.wIndex != CDCInterfaceInfo->Config.ControlInterfaceNumber)
  37. return;
  38. switch (USB_ControlRequest.bRequest)
  39. {
  40. case CDC_REQ_GetLineEncoding:
  41. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  42. {
  43. Endpoint_ClearSETUP();
  44. while (!(Endpoint_IsINReady()));
  45. Endpoint_Write_32_LE(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);
  46. Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.CharFormat);
  47. Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.ParityType);
  48. Endpoint_Write_8(CDCInterfaceInfo->State.LineEncoding.DataBits);
  49. Endpoint_ClearIN();
  50. Endpoint_ClearStatusStage();
  51. }
  52. break;
  53. case CDC_REQ_SetLineEncoding:
  54. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  55. {
  56. Endpoint_ClearSETUP();
  57. while (!(Endpoint_IsOUTReceived()))
  58. {
  59. if (USB_DeviceState == DEVICE_STATE_Unattached)
  60. return;
  61. }
  62. CDCInterfaceInfo->State.LineEncoding.BaudRateBPS = Endpoint_Read_32_LE();
  63. CDCInterfaceInfo->State.LineEncoding.CharFormat = Endpoint_Read_8();
  64. CDCInterfaceInfo->State.LineEncoding.ParityType = Endpoint_Read_8();
  65. CDCInterfaceInfo->State.LineEncoding.DataBits = Endpoint_Read_8();
  66. Endpoint_ClearOUT();
  67. Endpoint_ClearStatusStage();
  68. EVENT_CDC_Device_LineEncodingChanged(CDCInterfaceInfo);
  69. }
  70. break;
  71. case CDC_REQ_SetControlLineState:
  72. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  73. {
  74. Endpoint_ClearSETUP();
  75. Endpoint_ClearStatusStage();
  76. CDCInterfaceInfo->State.ControlLineStates.HostToDevice = USB_ControlRequest.wValue;
  77. EVENT_CDC_Device_ControLineStateChanged(CDCInterfaceInfo);
  78. }
  79. break;
  80. case CDC_REQ_SendBreak:
  81. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  82. {
  83. Endpoint_ClearSETUP();
  84. Endpoint_ClearStatusStage();
  85. EVENT_CDC_Device_BreakSent(CDCInterfaceInfo, (uint8_t)USB_ControlRequest.wValue);
  86. }
  87. break;
  88. }
  89. }
  90. bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  91. {
  92. memset(&CDCInterfaceInfo->State, 0x00, sizeof(CDCInterfaceInfo->State));
  93. CDCInterfaceInfo->Config.DataINEndpoint.Type = EP_TYPE_BULK;
  94. CDCInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;
  95. CDCInterfaceInfo->Config.NotificationEndpoint.Type = EP_TYPE_INTERRUPT;
  96. if (!(Endpoint_ConfigureEndpointTable(&CDCInterfaceInfo->Config.DataINEndpoint, 1)))
  97. return false;
  98. if (!(Endpoint_ConfigureEndpointTable(&CDCInterfaceInfo->Config.DataOUTEndpoint, 1)))
  99. return false;
  100. if (!(Endpoint_ConfigureEndpointTable(&CDCInterfaceInfo->Config.NotificationEndpoint, 1)))
  101. return false;
  102. return true;
  103. }
  104. void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  105. {
  106. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  107. return;
  108. #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
  109. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address);
  110. if (Endpoint_IsINReady())
  111. CDC_Device_Flush(CDCInterfaceInfo);
  112. #endif
  113. }
  114. uint8_t CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
  115. const char* const String)
  116. {
  117. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  118. return ENDPOINT_RWSTREAM_DeviceDisconnected;
  119. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address);
  120. return Endpoint_Write_Stream_LE(String, strlen(String), NULL);
  121. }
  122. uint8_t CDC_Device_SendData(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
  123. const void* const Buffer,
  124. const uint16_t Length)
  125. {
  126. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  127. return ENDPOINT_RWSTREAM_DeviceDisconnected;
  128. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address);
  129. return Endpoint_Write_Stream_LE(Buffer, Length, NULL);
  130. }
  131. uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
  132. const uint8_t Data)
  133. {
  134. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  135. return ENDPOINT_RWSTREAM_DeviceDisconnected;
  136. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address);
  137. if (!(Endpoint_IsReadWriteAllowed()))
  138. {
  139. Endpoint_ClearIN();
  140. uint8_t ErrorCode;
  141. if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
  142. return ErrorCode;
  143. }
  144. Endpoint_Write_8(Data);
  145. return ENDPOINT_READYWAIT_NoError;
  146. }
  147. uint8_t CDC_Device_Flush(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  148. {
  149. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  150. return ENDPOINT_RWSTREAM_DeviceDisconnected;
  151. uint8_t ErrorCode;
  152. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpoint.Address);
  153. if (!(Endpoint_BytesInEndpoint()))
  154. return ENDPOINT_READYWAIT_NoError;
  155. bool BankFull = !(Endpoint_IsReadWriteAllowed());
  156. Endpoint_ClearIN();
  157. if (BankFull)
  158. {
  159. if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
  160. return ErrorCode;
  161. Endpoint_ClearIN();
  162. }
  163. return ENDPOINT_READYWAIT_NoError;
  164. }
  165. uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  166. {
  167. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  168. return 0;
  169. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpoint.Address);
  170. if (Endpoint_IsOUTReceived())
  171. {
  172. if (!(Endpoint_BytesInEndpoint()))
  173. {
  174. Endpoint_ClearOUT();
  175. return 0;
  176. }
  177. else
  178. {
  179. return Endpoint_BytesInEndpoint();
  180. }
  181. }
  182. else
  183. {
  184. return 0;
  185. }
  186. }
  187. int16_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  188. {
  189. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  190. return -1;
  191. int16_t ReceivedByte = -1;
  192. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpoint.Address);
  193. if (Endpoint_IsOUTReceived())
  194. {
  195. if (Endpoint_BytesInEndpoint())
  196. ReceivedByte = Endpoint_Read_8();
  197. if (!(Endpoint_BytesInEndpoint()))
  198. Endpoint_ClearOUT();
  199. }
  200. return ReceivedByte;
  201. }
  202. void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
  203. {
  204. if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))
  205. return;
  206. Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpoint.Address);
  207. USB_Request_Header_t Notification = (USB_Request_Header_t)
  208. {
  209. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
  210. .bRequest = CDC_NOTIF_SerialState,
  211. .wValue = CPU_TO_LE16(0),
  212. .wIndex = CPU_TO_LE16(0),
  213. .wLength = CPU_TO_LE16(sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost)),
  214. };
  215. Endpoint_Write_Stream_LE(&Notification, sizeof(USB_Request_Header_t), NULL);
  216. Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost,
  217. sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),
  218. NULL);
  219. Endpoint_ClearIN();
  220. }
  221. #if defined(FDEV_SETUP_STREAM)
  222. void CDC_Device_CreateStream(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
  223. FILE* const Stream)
  224. {
  225. *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Device_putchar, CDC_Device_getchar, _FDEV_SETUP_RW);
  226. fdev_set_udata(Stream, CDCInterfaceInfo);
  227. }
  228. void CDC_Device_CreateBlockingStream(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo,
  229. FILE* const Stream)
  230. {
  231. *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Device_putchar, CDC_Device_getchar_Blocking, _FDEV_SETUP_RW);
  232. fdev_set_udata(Stream, CDCInterfaceInfo);
  233. }
  234. static int CDC_Device_putchar(char c,
  235. FILE* Stream)
  236. {
  237. return CDC_Device_SendByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
  238. }
  239. static int CDC_Device_getchar(FILE* Stream)
  240. {
  241. int16_t ReceivedByte = CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream));
  242. if (ReceivedByte < 0)
  243. return _FDEV_EOF;
  244. return ReceivedByte;
  245. }
  246. static int CDC_Device_getchar_Blocking(FILE* Stream)
  247. {
  248. int16_t ReceivedByte;
  249. while ((ReceivedByte = CDC_Device_ReceiveByte((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream))) < 0)
  250. {
  251. if (USB_DeviceState == DEVICE_STATE_Unattached)
  252. return _FDEV_EOF;
  253. CDC_Device_USBTask((USB_ClassInfo_CDC_Device_t*)fdev_get_udata(Stream));
  254. USB_USBTask();
  255. }
  256. return ReceivedByte;
  257. }
  258. #endif
  259. void CDC_Device_Event_Stub(void)
  260. {
  261. }
  262. #endif