ConfigDescriptor.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 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 HID interface descriptor containing at least one Interrupt type IN endpoint.
  38. *
  39. * \return An error code from the GenericHIDHost_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* HIDInterface = 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 (!(HIDInterface) ||
  65. USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  66. DComp_NextHIDInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  67. {
  68. /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor
  69. * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */
  70. if (DataINEndpoint)
  71. break;
  72. /* Get the next HID interface from the configuration descriptor */
  73. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  74. DComp_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  75. {
  76. /* Descriptor not found, error out */
  77. return NoCompatibleInterfaceFound;
  78. }
  79. /* Save the interface in case we need to refer back to it later */
  80. HIDInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  81. /* Clear any found endpoints */
  82. DataOUTEndpoint = NULL;
  83. /* Skip the remainder of the loop as we have not found an endpoint yet */
  84. continue;
  85. }
  86. /* Retrieve the endpoint address from the endpoint descriptor */
  87. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  88. /* If the endpoint is a IN type endpoint */
  89. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  90. DataINEndpoint = EndpointData;
  91. else
  92. DataOUTEndpoint = EndpointData;
  93. }
  94. /* Configure the HID data IN pipe */
  95. Pipe_ConfigurePipe(HID_DATA_IN_PIPE, EP_TYPE_INTERRUPT, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
  96. Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
  97. /* Check if the HID interface contained an optional OUT data endpoint */
  98. if (DataOUTEndpoint)
  99. {
  100. /* Configure the HID data OUT pipe */
  101. Pipe_ConfigurePipe(HID_DATA_OUT_PIPE, EP_TYPE_INTERRUPT, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
  102. }
  103. /* Valid data found, return success */
  104. return SuccessfulConfigRead;
  105. }
  106. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  107. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  108. * descriptor processing if an incompatible descriptor configuration is found.
  109. *
  110. * This comparator searches for the next Interface descriptor of the correct HID Class value.
  111. *
  112. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  113. */
  114. uint8_t DComp_NextHIDInterface(void* CurrentDescriptor)
  115. {
  116. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  117. /* Determine if the current descriptor is an interface descriptor */
  118. if (Header->Type == DTYPE_Interface)
  119. {
  120. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  121. /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */
  122. if (Interface->Class == HID_CLASS)
  123. {
  124. /* Indicate that the descriptor being searched for has been found */
  125. return DESCRIPTOR_SEARCH_Found;
  126. }
  127. }
  128. /* Current descriptor does not match what this comparator is looking for */
  129. return DESCRIPTOR_SEARCH_NotFound;
  130. }
  131. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  132. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  133. * descriptor processing if an incompatible descriptor configuration is found.
  134. *
  135. * This comparator searches for the next Endpoint descriptor inside the current interface descriptor,
  136. * aborting the search if another interface descriptor is found before the required endpoint.
  137. *
  138. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  139. */
  140. uint8_t DComp_NextHIDInterfaceDataEndpoint(void* CurrentDescriptor)
  141. {
  142. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  143. /* Determine the type of the current descriptor */
  144. if (Header->Type == DTYPE_Endpoint)
  145. {
  146. /* Indicate that the descriptor being searched for has been found */
  147. return DESCRIPTOR_SEARCH_Found;
  148. }
  149. else if (Header->Type == DTYPE_Interface)
  150. {
  151. /* Indicate that the search has failed prematurely and should be aborted */
  152. return DESCRIPTOR_SEARCH_Fail;
  153. }
  154. else
  155. {
  156. /* Current descriptor does not match what this comparator is looking for */
  157. return DESCRIPTOR_SEARCH_NotFound;
  158. }
  159. }