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.

CDCClassHost.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_HOST)
  29. #define __INCLUDE_FROM_CDC_DRIVER
  30. #define __INCLUDE_FROM_CDC_HOST_C
  31. #include "CDCClassHost.h"
  32. uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  33. uint16_t ConfigDescriptorSize,
  34. void* ConfigDescriptorData)
  35. {
  36. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  37. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  38. USB_Descriptor_Endpoint_t* NotificationEndpoint = NULL;
  39. USB_Descriptor_Interface_t* CDCControlInterface = NULL;
  40. memset(&CDCInterfaceInfo->State, 0x00, sizeof(CDCInterfaceInfo->State));
  41. if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
  42. return CDC_ENUMERROR_InvalidConfigDescriptor;
  43. while (!(DataINEndpoint) || !(DataOUTEndpoint) || !(NotificationEndpoint))
  44. {
  45. if (!(CDCControlInterface) ||
  46. USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  47. DCOMP_CDC_Host_NextCDCInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  48. {
  49. if (NotificationEndpoint)
  50. {
  51. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  52. DCOMP_CDC_Host_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  53. {
  54. return CDC_ENUMERROR_NoCompatibleInterfaceFound;
  55. }
  56. DataINEndpoint = NULL;
  57. DataOUTEndpoint = NULL;
  58. }
  59. else
  60. {
  61. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  62. DCOMP_CDC_Host_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  63. {
  64. return CDC_ENUMERROR_NoCompatibleInterfaceFound;
  65. }
  66. CDCControlInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
  67. NotificationEndpoint = NULL;
  68. }
  69. continue;
  70. }
  71. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
  72. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  73. {
  74. if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
  75. NotificationEndpoint = EndpointData;
  76. else
  77. DataINEndpoint = EndpointData;
  78. }
  79. else
  80. {
  81. DataOUTEndpoint = EndpointData;
  82. }
  83. }
  84. CDCInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
  85. CDCInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
  86. CDCInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
  87. CDCInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
  88. CDCInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
  89. CDCInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
  90. CDCInterfaceInfo->Config.NotificationPipe.Size = le16_to_cpu(NotificationEndpoint->EndpointSize);
  91. CDCInterfaceInfo->Config.NotificationPipe.EndpointAddress = NotificationEndpoint->EndpointAddress;
  92. CDCInterfaceInfo->Config.NotificationPipe.Type = EP_TYPE_INTERRUPT;
  93. if (!(Pipe_ConfigurePipeTable(&CDCInterfaceInfo->Config.DataINPipe, 1)))
  94. return CDC_ENUMERROR_PipeConfigurationFailed;
  95. if (!(Pipe_ConfigurePipeTable(&CDCInterfaceInfo->Config.DataOUTPipe, 1)))
  96. return CDC_ENUMERROR_PipeConfigurationFailed;
  97. if (!(Pipe_ConfigurePipeTable(&CDCInterfaceInfo->Config.NotificationPipe, 1)))
  98. return CDC_ENUMERROR_PipeConfigurationFailed;
  99. CDCInterfaceInfo->State.ControlInterfaceNumber = CDCControlInterface->InterfaceNumber;
  100. CDCInterfaceInfo->State.ControlLineStates.HostToDevice = (CDC_CONTROL_LINE_OUT_RTS | CDC_CONTROL_LINE_OUT_DTR);
  101. CDCInterfaceInfo->State.ControlLineStates.DeviceToHost = (CDC_CONTROL_LINE_IN_DCD | CDC_CONTROL_LINE_IN_DSR);
  102. CDCInterfaceInfo->State.IsActive = true;
  103. return CDC_ENUMERROR_NoError;
  104. }
  105. static uint8_t DCOMP_CDC_Host_NextCDCControlInterface(void* const CurrentDescriptor)
  106. {
  107. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  108. if (Header->Type == DTYPE_Interface)
  109. {
  110. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  111. if ((Interface->Class == CDC_CSCP_CDCClass) &&
  112. (Interface->SubClass == CDC_CSCP_ACMSubclass) &&
  113. (Interface->Protocol == CDC_CSCP_ATCommandProtocol))
  114. {
  115. return DESCRIPTOR_SEARCH_Found;
  116. }
  117. }
  118. return DESCRIPTOR_SEARCH_NotFound;
  119. }
  120. static uint8_t DCOMP_CDC_Host_NextCDCDataInterface(void* const CurrentDescriptor)
  121. {
  122. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  123. if (Header->Type == DTYPE_Interface)
  124. {
  125. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  126. if ((Interface->Class == CDC_CSCP_CDCDataClass) &&
  127. (Interface->SubClass == CDC_CSCP_NoDataSubclass) &&
  128. (Interface->Protocol == CDC_CSCP_NoDataProtocol))
  129. {
  130. return DESCRIPTOR_SEARCH_Found;
  131. }
  132. }
  133. return DESCRIPTOR_SEARCH_NotFound;
  134. }
  135. static uint8_t DCOMP_CDC_Host_NextCDCInterfaceEndpoint(void* const CurrentDescriptor)
  136. {
  137. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  138. if (Header->Type == DTYPE_Endpoint)
  139. {
  140. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  141. uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
  142. if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
  143. !(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
  144. {
  145. return DESCRIPTOR_SEARCH_Found;
  146. }
  147. }
  148. else if (Header->Type == DTYPE_Interface)
  149. {
  150. return DESCRIPTOR_SEARCH_Fail;
  151. }
  152. return DESCRIPTOR_SEARCH_NotFound;
  153. }
  154. void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
  155. {
  156. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  157. return;
  158. Pipe_SelectPipe(CDCInterfaceInfo->Config.NotificationPipe.Address);
  159. Pipe_Unfreeze();
  160. if (Pipe_IsINReceived())
  161. {
  162. USB_Request_Header_t Notification;
  163. Pipe_Read_Stream_LE(&Notification, sizeof(USB_Request_Header_t), NULL);
  164. if ((Notification.bRequest == CDC_NOTIF_SerialState) &&
  165. (Notification.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)))
  166. {
  167. Pipe_Read_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost,
  168. sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),
  169. NULL);
  170. Pipe_ClearIN();
  171. EVENT_CDC_Host_ControLineStateChanged(CDCInterfaceInfo);
  172. }
  173. else
  174. {
  175. Pipe_ClearIN();
  176. }
  177. }
  178. Pipe_Freeze();
  179. #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
  180. CDC_Host_Flush(CDCInterfaceInfo);
  181. #endif
  182. }
  183. uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
  184. {
  185. USB_ControlRequest = (USB_Request_Header_t)
  186. {
  187. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  188. .bRequest = CDC_REQ_SetLineEncoding,
  189. .wValue = 0,
  190. .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
  191. .wLength = sizeof(CDCInterfaceInfo->State.LineEncoding),
  192. };
  193. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  194. return USB_Host_SendControlRequest(&CDCInterfaceInfo->State.LineEncoding);
  195. }
  196. uint8_t CDC_Host_SendControlLineStateChange(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
  197. {
  198. USB_ControlRequest = (USB_Request_Header_t)
  199. {
  200. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  201. .bRequest = CDC_REQ_SetControlLineState,
  202. .wValue = CDCInterfaceInfo->State.ControlLineStates.HostToDevice,
  203. .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
  204. .wLength = 0,
  205. };
  206. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  207. return USB_Host_SendControlRequest(NULL);
  208. }
  209. uint8_t CDC_Host_SendBreak(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  210. const uint8_t Duration)
  211. {
  212. USB_ControlRequest = (USB_Request_Header_t)
  213. {
  214. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  215. .bRequest = CDC_REQ_SendBreak,
  216. .wValue = Duration,
  217. .wIndex = CDCInterfaceInfo->State.ControlInterfaceNumber,
  218. .wLength = 0,
  219. };
  220. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  221. return USB_Host_SendControlRequest(NULL);
  222. }
  223. uint8_t CDC_Host_SendData(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  224. const void* const Buffer,
  225. const uint16_t Length)
  226. {
  227. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  228. return PIPE_READYWAIT_DeviceDisconnected;
  229. uint8_t ErrorCode;
  230. Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipe.Address);
  231. Pipe_Unfreeze();
  232. ErrorCode = Pipe_Write_Stream_LE(Buffer, Length, NULL);
  233. Pipe_Freeze();
  234. return ErrorCode;
  235. }
  236. uint8_t CDC_Host_SendString(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  237. const char* const String)
  238. {
  239. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  240. return PIPE_READYWAIT_DeviceDisconnected;
  241. uint8_t ErrorCode;
  242. Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipe.Address);
  243. Pipe_Unfreeze();
  244. ErrorCode = Pipe_Write_Stream_LE(String, strlen(String), NULL);
  245. Pipe_Freeze();
  246. return ErrorCode;
  247. }
  248. uint8_t CDC_Host_SendByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  249. const uint8_t Data)
  250. {
  251. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  252. return PIPE_READYWAIT_DeviceDisconnected;
  253. uint8_t ErrorCode;
  254. Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipe.Address);
  255. Pipe_Unfreeze();
  256. if (!(Pipe_IsReadWriteAllowed()))
  257. {
  258. Pipe_ClearOUT();
  259. if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
  260. return ErrorCode;
  261. }
  262. Pipe_Write_8(Data);
  263. Pipe_Freeze();
  264. return PIPE_READYWAIT_NoError;
  265. }
  266. uint16_t CDC_Host_BytesReceived(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
  267. {
  268. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  269. return 0;
  270. Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipe.Address);
  271. Pipe_Unfreeze();
  272. if (Pipe_IsINReceived())
  273. {
  274. if (!(Pipe_BytesInPipe()))
  275. {
  276. Pipe_ClearIN();
  277. Pipe_Freeze();
  278. return 0;
  279. }
  280. else
  281. {
  282. Pipe_Freeze();
  283. return Pipe_BytesInPipe();
  284. }
  285. }
  286. else
  287. {
  288. Pipe_Freeze();
  289. return 0;
  290. }
  291. }
  292. int16_t CDC_Host_ReceiveByte(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
  293. {
  294. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  295. return -1;
  296. int16_t ReceivedByte = -1;
  297. Pipe_SelectPipe(CDCInterfaceInfo->Config.DataINPipe.Address);
  298. Pipe_Unfreeze();
  299. if (Pipe_IsINReceived())
  300. {
  301. if (Pipe_BytesInPipe())
  302. ReceivedByte = Pipe_Read_8();
  303. if (!(Pipe_BytesInPipe()))
  304. Pipe_ClearIN();
  305. }
  306. Pipe_Freeze();
  307. return ReceivedByte;
  308. }
  309. uint8_t CDC_Host_Flush(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo)
  310. {
  311. if ((USB_HostState != HOST_STATE_Configured) || !(CDCInterfaceInfo->State.IsActive))
  312. return PIPE_READYWAIT_DeviceDisconnected;
  313. uint8_t ErrorCode;
  314. Pipe_SelectPipe(CDCInterfaceInfo->Config.DataOUTPipe.Address);
  315. Pipe_Unfreeze();
  316. if (!(Pipe_BytesInPipe()))
  317. return PIPE_READYWAIT_NoError;
  318. bool BankFull = !(Pipe_IsReadWriteAllowed());
  319. Pipe_ClearOUT();
  320. if (BankFull)
  321. {
  322. if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
  323. return ErrorCode;
  324. Pipe_ClearOUT();
  325. }
  326. Pipe_Freeze();
  327. return PIPE_READYWAIT_NoError;
  328. }
  329. #if defined(FDEV_SETUP_STREAM)
  330. void CDC_Host_CreateStream(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  331. FILE* const Stream)
  332. {
  333. *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar, _FDEV_SETUP_RW);
  334. fdev_set_udata(Stream, CDCInterfaceInfo);
  335. }
  336. void CDC_Host_CreateBlockingStream(USB_ClassInfo_CDC_Host_t* const CDCInterfaceInfo,
  337. FILE* const Stream)
  338. {
  339. *Stream = (FILE)FDEV_SETUP_STREAM(CDC_Host_putchar, CDC_Host_getchar_Blocking, _FDEV_SETUP_RW);
  340. fdev_set_udata(Stream, CDCInterfaceInfo);
  341. }
  342. static int CDC_Host_putchar(char c,
  343. FILE* Stream)
  344. {
  345. return CDC_Host_SendByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream), c) ? _FDEV_ERR : 0;
  346. }
  347. static int CDC_Host_getchar(FILE* Stream)
  348. {
  349. int16_t ReceivedByte = CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
  350. if (ReceivedByte < 0)
  351. return _FDEV_EOF;
  352. return ReceivedByte;
  353. }
  354. static int CDC_Host_getchar_Blocking(FILE* Stream)
  355. {
  356. int16_t ReceivedByte;
  357. while ((ReceivedByte = CDC_Host_ReceiveByte((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream))) < 0)
  358. {
  359. if (USB_HostState == HOST_STATE_Unattached)
  360. return _FDEV_EOF;
  361. CDC_Host_USBTask((USB_ClassInfo_CDC_Host_t*)fdev_get_udata(Stream));
  362. USB_USBTask();
  363. }
  364. return ReceivedByte;
  365. }
  366. #endif
  367. void CDC_Host_Event_Stub(void)
  368. {
  369. }
  370. #endif