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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 HID interface descriptor containing at least one Interrupt type IN endpoint and HID descriptor.
  38. *
  39. * \return An error code from the \ref MouseHostWithParser_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_HID_Descriptor_HID_t* HIDDescriptor = NULL;
  48. USB_Descriptor_Endpoint_t* DataINEndpoint = 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))
  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_NextMouseInterfaceDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  67. {
  68. /* Get the next HID interface from the configuration descriptor */
  69. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  70. DComp_NextMouseInterface) != 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. HIDInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  77. /* Get the HID descriptor from the configuration descriptor */
  78. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  79. DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
  80. {
  81. /* Descriptor not found, error out */
  82. return NoCompatibleInterfaceFound;
  83. }
  84. /* Save the HID descriptor for later use */
  85. HIDDescriptor = DESCRIPTOR_PCAST(CurrConfigLocation, USB_HID_Descriptor_HID_t);
  86. /* Skip the remainder of the loop as we have not found an endpoint yet */
  87. continue;
  88. }
  89. /* Retrieve the endpoint address from the endpoint descriptor */
  90. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  91. /* If the endpoint is a IN type endpoint */
  92. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  93. DataINEndpoint = EndpointData;
  94. }
  95. /* Configure the HID data IN pipe */
  96. Pipe_ConfigurePipe(MOUSE_DATA_IN_PIPE, EP_TYPE_INTERRUPT, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
  97. Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
  98. /* Get the HID report size from the HID report descriptor */
  99. HIDReportSize = HIDDescriptor->HIDReportLength;
  100. /* Valid data found, return success */
  101. return SuccessfulConfigRead;
  102. }
  103. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  104. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  105. * descriptor processing if an incompatible descriptor configuration is found.
  106. *
  107. * This comparator searches for the next Interface descriptor of the correct Mouse HID Class and Protocol values.
  108. *
  109. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  110. */
  111. uint8_t DComp_NextMouseInterface(void* CurrentDescriptor)
  112. {
  113. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  114. if (Header->Type == DTYPE_Interface)
  115. {
  116. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  117. /* Check the HID descriptor class, break out if correct class interface found */
  118. if (Interface->Class == HID_CSCP_HIDClass)
  119. {
  120. return DESCRIPTOR_SEARCH_Found;
  121. }
  122. }
  123. return DESCRIPTOR_SEARCH_NotFound;
  124. }
  125. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  126. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  127. * descriptor processing if an incompatible descriptor configuration is found.
  128. *
  129. * This comparator searches for the next Endpoint descriptor inside the current interface descriptor, aborting the
  130. * search if another interface descriptor is found before the required endpoint.
  131. *
  132. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  133. */
  134. uint8_t DComp_NextMouseInterfaceDataEndpoint(void* CurrentDescriptor)
  135. {
  136. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  137. if (Header->Type == DTYPE_Endpoint)
  138. return DESCRIPTOR_SEARCH_Found;
  139. else if (Header->Type == DTYPE_Interface)
  140. return DESCRIPTOR_SEARCH_Fail;
  141. else
  142. return DESCRIPTOR_SEARCH_NotFound;
  143. }
  144. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  145. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  146. * descriptor processing if an incompatible descriptor configuration is found.
  147. *
  148. * This comparator searches for the next HID descriptor within the current HID interface descriptor.
  149. *
  150. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  151. */
  152. uint8_t DComp_NextHID(void* CurrentDescriptor)
  153. {
  154. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  155. if (Header->Type == HID_DTYPE_HID)
  156. return DESCRIPTOR_SEARCH_Found;
  157. else
  158. return DESCRIPTOR_SEARCH_NotFound;
  159. }