PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/LUFA-111009/LUFA/Drivers/USB/Class/Host/Audio.c

https://github.com/davidk/lufa-lib
C | 247 lines | 174 code | 45 blank | 28 comment | 43 complexity | 469f2b7460c14c56ca5374f090dc83ad MD5 | raw file
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2011.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2011 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 disclaim 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_AUDIO_DRIVER
  30. #define __INCLUDE_FROM_AUDIO_HOST_C
  31. #include "Audio.h"
  32. uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
  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* AudioControlInterface = NULL;
  39. USB_Descriptor_Interface_t* AudioStreamingInterface = NULL;
  40. memset(&AudioInterfaceInfo->State, 0x00, sizeof(AudioInterfaceInfo->State));
  41. if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
  42. return AUDIO_ENUMERROR_InvalidConfigDescriptor;
  43. while ((AudioInterfaceInfo->Config.DataINPipeNumber && !(DataINEndpoint)) ||
  44. (AudioInterfaceInfo->Config.DataOUTPipeNumber && !(DataOUTEndpoint)))
  45. {
  46. if (!(AudioControlInterface) ||
  47. USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  48. DCOMP_Audio_Host_NextAudioInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  49. {
  50. if (!(AudioControlInterface) ||
  51. USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  52. DCOMP_Audio_Host_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  53. {
  54. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  55. DCOMP_Audio_Host_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  56. {
  57. return AUDIO_ENUMERROR_NoCompatibleInterfaceFound;
  58. }
  59. AudioControlInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
  60. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  61. DCOMP_Audio_Host_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  62. {
  63. return AUDIO_ENUMERROR_NoCompatibleInterfaceFound;
  64. }
  65. }
  66. AudioStreamingInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
  67. DataINEndpoint = NULL;
  68. DataOUTEndpoint = NULL;
  69. continue;
  70. }
  71. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
  72. if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
  73. DataINEndpoint = EndpointData;
  74. else
  75. DataOUTEndpoint = EndpointData;
  76. }
  77. for (uint8_t PipeNum = 1; PipeNum < PIPE_TOTAL_PIPES; PipeNum++)
  78. {
  79. uint16_t Size;
  80. uint8_t Type;
  81. uint8_t Token;
  82. uint8_t EndpointAddress;
  83. bool DoubleBanked;
  84. if (PipeNum == AudioInterfaceInfo->Config.DataINPipeNumber)
  85. {
  86. Size = le16_to_cpu(DataINEndpoint->EndpointSize);
  87. EndpointAddress = DataINEndpoint->EndpointAddress;
  88. Token = PIPE_TOKEN_IN;
  89. Type = EP_TYPE_ISOCHRONOUS;
  90. DoubleBanked = true;
  91. AudioInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
  92. }
  93. else if (PipeNum == AudioInterfaceInfo->Config.DataOUTPipeNumber)
  94. {
  95. Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
  96. EndpointAddress = DataOUTEndpoint->EndpointAddress;
  97. Token = PIPE_TOKEN_OUT;
  98. Type = EP_TYPE_ISOCHRONOUS;
  99. DoubleBanked = true;
  100. AudioInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
  101. }
  102. else
  103. {
  104. continue;
  105. }
  106. if (!(Pipe_ConfigurePipe(PipeNum, Type, Token, EndpointAddress, Size,
  107. DoubleBanked ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE)))
  108. {
  109. return AUDIO_ENUMERROR_PipeConfigurationFailed;
  110. }
  111. }
  112. AudioInterfaceInfo->State.ControlInterfaceNumber = AudioControlInterface->InterfaceNumber;
  113. AudioInterfaceInfo->State.StreamingInterfaceNumber = AudioStreamingInterface->InterfaceNumber;
  114. AudioInterfaceInfo->State.EnabledStreamingAltIndex = AudioStreamingInterface->AlternateSetting;
  115. AudioInterfaceInfo->State.IsActive = true;
  116. return AUDIO_ENUMERROR_NoError;
  117. }
  118. static uint8_t DCOMP_Audio_Host_NextAudioControlInterface(void* CurrentDescriptor)
  119. {
  120. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  121. if (Header->Type == DTYPE_Interface)
  122. {
  123. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  124. if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
  125. (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
  126. (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
  127. {
  128. return DESCRIPTOR_SEARCH_Found;
  129. }
  130. }
  131. return DESCRIPTOR_SEARCH_NotFound;
  132. }
  133. static uint8_t DCOMP_Audio_Host_NextAudioStreamInterface(void* CurrentDescriptor)
  134. {
  135. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  136. if (Header->Type == DTYPE_Interface)
  137. {
  138. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  139. if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
  140. (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
  141. (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
  142. {
  143. return DESCRIPTOR_SEARCH_Found;
  144. }
  145. }
  146. return DESCRIPTOR_SEARCH_NotFound;
  147. }
  148. static uint8_t DCOMP_Audio_Host_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
  149. {
  150. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  151. if (Header->Type == DTYPE_Endpoint)
  152. {
  153. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  154. if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
  155. return DESCRIPTOR_SEARCH_Found;
  156. }
  157. else if (Header->Type == DTYPE_Interface)
  158. {
  159. return DESCRIPTOR_SEARCH_Fail;
  160. }
  161. return DESCRIPTOR_SEARCH_NotFound;
  162. }
  163. uint8_t Audio_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
  164. const bool EnableStreaming)
  165. {
  166. if (!(AudioInterfaceInfo->State.IsActive))
  167. return HOST_SENDCONTROL_DeviceDisconnected;
  168. return USB_Host_SetInterfaceAltSetting(AudioInterfaceInfo->State.StreamingInterfaceNumber,
  169. EnableStreaming ? AudioInterfaceInfo->State.EnabledStreamingAltIndex : 0);
  170. }
  171. uint8_t Audio_Host_GetSetEndpointProperty(USB_ClassInfo_Audio_Host_t* const AudioInterfaceInfo,
  172. const uint8_t DataPipeIndex,
  173. const uint8_t EndpointProperty,
  174. const uint8_t EndpointControl,
  175. const uint16_t DataLength,
  176. void* const Data)
  177. {
  178. if (!(AudioInterfaceInfo->State.IsActive))
  179. return HOST_SENDCONTROL_DeviceDisconnected;
  180. uint8_t RequestType;
  181. uint8_t EndpointAddress;
  182. if (EndpointProperty & 0x80)
  183. RequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT);
  184. else
  185. RequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_ENDPOINT);
  186. Pipe_SelectPipe(DataPipeIndex);
  187. EndpointAddress = Pipe_GetBoundEndpointAddress();
  188. USB_ControlRequest = (USB_Request_Header_t)
  189. {
  190. .bmRequestType = RequestType,
  191. .bRequest = EndpointProperty,
  192. .wValue = ((uint16_t)EndpointControl << 8),
  193. .wIndex = EndpointAddress,
  194. .wLength = DataLength,
  195. };
  196. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  197. return USB_Host_SendControlRequest(Data);
  198. }
  199. #endif