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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
  34. * routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
  35. * with compatible devices.
  36. *
  37. * This routine searches for a MIDI interface descriptor pair containing bulk data IN and OUT endpoints.
  38. *
  39. * \return An error code from the \ref MIDIHost_GetConfigDescriptorDataCodes_t enum.
  40. */
  41. uint8_t ProcessConfigurationDescriptor(void)
  42. {
  43. uint8_t ConfigDescriptorData[512];
  44. void* CurrConfigLocation = ConfigDescriptorData;
  45. uint16_t CurrConfigBytesRem;
  46. USB_Descriptor_Interface_t* MIDIInterface = NULL;
  47. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  48. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  49. /* Retrieve the entire configuration descriptor into the allocated buffer */
  50. switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
  51. {
  52. case HOST_GETCONFIG_Successful:
  53. break;
  54. case HOST_GETCONFIG_InvalidData:
  55. return InvalidConfigDataReturned;
  56. case HOST_GETCONFIG_BuffOverflow:
  57. return DescriptorTooLarge;
  58. default:
  59. return ControlError;
  60. }
  61. while (!(DataINEndpoint) || !(DataOUTEndpoint))
  62. {
  63. /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
  64. if (!(MIDIInterface) ||
  65. USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  66. DComp_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  67. {
  68. /* Get the next Mass Storage interface from the configuration descriptor */
  69. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  70. DComp_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  71. {
  72. /* Descriptor not found, error out */
  73. return NoCompatibleInterfaceFound;
  74. }
  75. /* Save the interface in case we need to refer back to it later */
  76. MIDIInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  77. /* Clear any found endpoints */
  78. DataINEndpoint = NULL;
  79. DataOUTEndpoint = NULL;
  80. /* Skip the remainder of the loop as we have not found an endpoint yet */
  81. continue;
  82. }
  83. /* Retrieve the endpoint address from the endpoint descriptor */
  84. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  85. /* If the endpoint is a IN type endpoint */
  86. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  87. DataINEndpoint = EndpointData;
  88. else
  89. DataOUTEndpoint = EndpointData;
  90. }
  91. /* Configure the MIDI data IN pipe */
  92. Pipe_ConfigurePipe(MIDI_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
  93. /* Configure the MIDI data OUT pipe */
  94. Pipe_ConfigurePipe(MIDI_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
  95. /* Valid data found, return success */
  96. return SuccessfulConfigRead;
  97. }
  98. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  99. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  100. * descriptor processing if an incompatible descriptor configuration is found.
  101. *
  102. * This comparator searches for the next Interface descriptor of the correct MIDI Streaming Class, Subclass and Protocol values.
  103. *
  104. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  105. */
  106. uint8_t DComp_NextMIDIStreamingInterface(void* CurrentDescriptor)
  107. {
  108. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  109. if (Header->Type == DTYPE_Interface)
  110. {
  111. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  112. /* Check the MIDI descriptor class, subclass and protocol, break out if correct data interface found */
  113. if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
  114. (Interface->SubClass == AUDIO_CSCP_MIDIStreamingSubclass) &&
  115. (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
  116. {
  117. return DESCRIPTOR_SEARCH_Found;
  118. }
  119. }
  120. return DESCRIPTOR_SEARCH_NotFound;
  121. }
  122. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  123. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  124. * descriptor processing if an incompatible descriptor configuration is found.
  125. *
  126. * This comparator searches for the next bulk IN or OUT endpoint within the current interface, aborting the search if
  127. * another interface descriptor is found before the required endpoint.
  128. *
  129. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  130. */
  131. uint8_t DComp_NextMIDIStreamingDataEndpoint(void* CurrentDescriptor)
  132. {
  133. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  134. if (Header->Type == DTYPE_Endpoint)
  135. {
  136. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  137. /* Check the endpoint type, break out if correct BULK type endpoint found */
  138. if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK)
  139. return DESCRIPTOR_SEARCH_Found;
  140. }
  141. else if (Header->Type == DTYPE_Interface)
  142. {
  143. return DESCRIPTOR_SEARCH_Fail;
  144. }
  145. return DESCRIPTOR_SEARCH_NotFound;
  146. }