MIDIClassHost.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 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_MIDI_DRIVER
  30. #define __INCLUDE_FROM_MIDI_HOST_C
  31. #include "MIDIClassHost.h"
  32. uint8_t MIDI_Host_ConfigurePipes(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
  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_Interface_t* MIDIInterface = NULL;
  39. memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
  40. if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
  41. return MIDI_ENUMERROR_InvalidConfigDescriptor;
  42. while (!(DataINEndpoint) || !(DataOUTEndpoint))
  43. {
  44. if (!(MIDIInterface) ||
  45. USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  46. DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  47. {
  48. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  49. DCOMP_MIDI_Host_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  50. {
  51. return MIDI_ENUMERROR_NoCompatibleInterfaceFound;
  52. }
  53. MIDIInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
  54. DataINEndpoint = NULL;
  55. DataOUTEndpoint = NULL;
  56. continue;
  57. }
  58. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
  59. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  60. DataINEndpoint = EndpointData;
  61. else
  62. DataOUTEndpoint = EndpointData;
  63. }
  64. MIDIInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
  65. MIDIInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
  66. MIDIInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
  67. MIDIInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
  68. MIDIInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
  69. MIDIInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
  70. if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataINPipe, 1)))
  71. return MIDI_ENUMERROR_PipeConfigurationFailed;
  72. if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataOUTPipe, 1)))
  73. return MIDI_ENUMERROR_PipeConfigurationFailed;
  74. MIDIInterfaceInfo->State.InterfaceNumber = MIDIInterface->InterfaceNumber;
  75. MIDIInterfaceInfo->State.IsActive = true;
  76. return MIDI_ENUMERROR_NoError;
  77. }
  78. static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
  79. {
  80. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  81. if (Header->Type == DTYPE_Interface)
  82. {
  83. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  84. if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
  85. (Interface->SubClass == AUDIO_CSCP_MIDIStreamingSubclass) &&
  86. (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
  87. {
  88. return DESCRIPTOR_SEARCH_Found;
  89. }
  90. }
  91. return DESCRIPTOR_SEARCH_NotFound;
  92. }
  93. static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
  94. {
  95. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  96. if (Header->Type == DTYPE_Endpoint)
  97. {
  98. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  99. uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
  100. if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
  101. return DESCRIPTOR_SEARCH_Found;
  102. }
  103. else if (Header->Type == DTYPE_Interface)
  104. {
  105. return DESCRIPTOR_SEARCH_Fail;
  106. }
  107. return DESCRIPTOR_SEARCH_NotFound;
  108. }
  109. void MIDI_Host_USBTask(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
  110. {
  111. if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
  112. return;
  113. #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
  114. MIDI_Host_Flush(MIDIInterfaceInfo);
  115. #endif
  116. }
  117. uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
  118. {
  119. if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
  120. return PIPE_RWSTREAM_DeviceDisconnected;
  121. uint8_t ErrorCode;
  122. Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
  123. Pipe_Unfreeze();
  124. if (Pipe_BytesInPipe())
  125. {
  126. Pipe_ClearOUT();
  127. if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
  128. {
  129. Pipe_Freeze();
  130. return ErrorCode;
  131. }
  132. }
  133. Pipe_Freeze();
  134. return PIPE_READYWAIT_NoError;
  135. }
  136. uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
  137. MIDI_EventPacket_t* const Event)
  138. {
  139. if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
  140. return HOST_SENDCONTROL_DeviceDisconnected;
  141. uint8_t ErrorCode;
  142. Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
  143. Pipe_Unfreeze();
  144. if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != PIPE_RWSTREAM_NoError)
  145. {
  146. Pipe_Freeze();
  147. return ErrorCode;
  148. }
  149. if (!(Pipe_IsReadWriteAllowed()))
  150. Pipe_ClearOUT();
  151. Pipe_Freeze();
  152. return PIPE_RWSTREAM_NoError;
  153. }
  154. bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
  155. MIDI_EventPacket_t* const Event)
  156. {
  157. if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
  158. return HOST_SENDCONTROL_DeviceDisconnected;
  159. bool DataReady = false;
  160. Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipe.Address);
  161. Pipe_Unfreeze();
  162. if (Pipe_IsINReceived())
  163. {
  164. if (Pipe_BytesInPipe())
  165. {
  166. Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
  167. DataReady = true;
  168. }
  169. if (!(Pipe_BytesInPipe()))
  170. Pipe_ClearIN();
  171. }
  172. Pipe_Freeze();
  173. return DataReady;
  174. }
  175. #endif