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.

ConfigDescriptor.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /** \file
  27. *
  28. * USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations
  29. * needed to communication with an attached USB device. Descriptors are special computer-readable structures
  30. * which the host requests upon device enumeration, to determine the device's capabilities and functions.
  31. */
  32. #include "ConfigDescriptor.h"
  33. /** Index of the currently used Audio Streaming Interface within the device. */
  34. uint8_t StreamingInterfaceIndex = 0;
  35. /** Alternative Setting of the currently used Audio Streaming Interface within the device. */
  36. uint8_t StreamingInterfaceAltSetting = 0;
  37. /** Address of the streaming audio endpoint currently in use within the device. */
  38. uint8_t StreamingEndpointAddress = 0;
  39. /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
  40. * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
  41. * with compatible devices.
  42. *
  43. * This routine searches for a Streaming Audio interface descriptor containing a valid Isochronous audio endpoint.
  44. *
  45. * \return An error code from the \ref AudioHost_GetConfigDescriptorDataCodes_t enum.
  46. */
  47. uint8_t ProcessConfigurationDescriptor(void)
  48. {
  49. uint8_t ConfigDescriptorData[512];
  50. void* CurrConfigLocation = ConfigDescriptorData;
  51. uint16_t CurrConfigBytesRem;
  52. USB_Descriptor_Interface_t* AudioControlInterface = NULL;
  53. USB_Descriptor_Interface_t* AudioStreamingInterface = NULL;
  54. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  55. /* Retrieve the entire configuration descriptor into the allocated buffer */
  56. switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
  57. {
  58. case HOST_GETCONFIG_Successful:
  59. break;
  60. case HOST_GETCONFIG_InvalidData:
  61. return InvalidConfigDataReturned;
  62. case HOST_GETCONFIG_BuffOverflow:
  63. return DescriptorTooLarge;
  64. default:
  65. return ControlError;
  66. }
  67. while (!(DataINEndpoint))
  68. {
  69. /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
  70. if (!(AudioControlInterface) ||
  71. USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  72. DComp_NextAudioInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  73. {
  74. /* Check if we haven't found an Audio Control interface yet, or if we have run out of related Audio Streaming interfaces */
  75. if (!(AudioControlInterface) ||
  76. USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  77. DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  78. {
  79. /* Find a new Audio Control interface if the current one doesn't contain a compatible streaming interface */
  80. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  81. DComp_NextAudioControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  82. {
  83. /* Descriptor not found, error out */
  84. return NoCompatibleInterfaceFound;
  85. }
  86. /* Save the interface in case we need to refer back to it later */
  87. AudioControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  88. /* Find the next Audio Streaming interface within that Audio Control interface */
  89. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  90. DComp_NextAudioStreamInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  91. {
  92. /* Descriptor not found, error out */
  93. return NoCompatibleInterfaceFound;
  94. }
  95. }
  96. /* Save the interface in case we need to refer back to it later */
  97. AudioStreamingInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  98. /* Skip the remainder of the loop as we have not found an endpoint yet */
  99. continue;
  100. }
  101. /* Retrieve the endpoint address from the endpoint descriptor */
  102. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  103. /* Save the endpoint if it is an IN type endpoint */
  104. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  105. DataINEndpoint = EndpointData;
  106. }
  107. StreamingInterfaceIndex = AudioStreamingInterface->InterfaceNumber;
  108. StreamingInterfaceAltSetting = AudioStreamingInterface->AlternateSetting;
  109. StreamingEndpointAddress = DataINEndpoint->EndpointAddress;
  110. /* Configure the Audio data IN pipe */
  111. Pipe_ConfigurePipe(AUDIO_DATA_IN_PIPE, EP_TYPE_ISOCHRONOUS, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 2);
  112. /* Valid data found, return success */
  113. return SuccessfulConfigRead;
  114. }
  115. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  116. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  117. * descriptor processing if an incompatible descriptor configuration is found.
  118. *
  119. * This comparator searches for the next Interface descriptor of the correct Audio Control Class, Subclass and Protocol values.
  120. *
  121. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  122. */
  123. uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor)
  124. {
  125. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  126. if (Header->Type == DTYPE_Interface)
  127. {
  128. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  129. if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
  130. (Interface->SubClass == AUDIO_CSCP_ControlSubclass) &&
  131. (Interface->Protocol == AUDIO_CSCP_ControlProtocol))
  132. {
  133. return DESCRIPTOR_SEARCH_Found;
  134. }
  135. }
  136. return DESCRIPTOR_SEARCH_NotFound;
  137. }
  138. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  139. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  140. * descriptor processing if an incompatible descriptor configuration is found.
  141. *
  142. * This comparator searches for the next Interface descriptor of the correct Audio Streaming Class, Subclass and Protocol values.
  143. *
  144. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  145. */
  146. uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor)
  147. {
  148. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  149. if (Header->Type == DTYPE_Interface)
  150. {
  151. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  152. if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
  153. (Interface->SubClass == AUDIO_CSCP_AudioStreamingSubclass) &&
  154. (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
  155. {
  156. return DESCRIPTOR_SEARCH_Found;
  157. }
  158. }
  159. return DESCRIPTOR_SEARCH_NotFound;
  160. }
  161. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  162. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  163. * descriptor processing if an incompatible descriptor configuration is found.
  164. *
  165. * This comparator searches for the next Isochronous Endpoint descriptor within the current interface, aborting the
  166. * search if another interface descriptor is found before the next endpoint.
  167. *
  168. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  169. */
  170. uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor)
  171. {
  172. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  173. if (Header->Type == DTYPE_Endpoint)
  174. {
  175. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  176. if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_ISOCHRONOUS)
  177. return DESCRIPTOR_SEARCH_Found;
  178. }
  179. else if (Header->Type == DTYPE_Interface)
  180. {
  181. return DESCRIPTOR_SEARCH_Fail;
  182. }
  183. return DESCRIPTOR_SEARCH_NotFound;
  184. }