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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 RNDIS interface descriptor containing bulk data IN and OUT endpoints, and an interrupt event endpoint.
  38. *
  39. * \return An error code from the \ref RNDISHost_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* RNDISControlInterface = NULL;
  47. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  48. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  49. USB_Descriptor_Endpoint_t* NotificationEndpoint = NULL;
  50. /* Retrieve the entire configuration descriptor into the allocated buffer */
  51. switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
  52. {
  53. case HOST_GETCONFIG_Successful:
  54. break;
  55. case HOST_GETCONFIG_InvalidData:
  56. return InvalidConfigDataReturned;
  57. case HOST_GETCONFIG_BuffOverflow:
  58. return DescriptorTooLarge;
  59. default:
  60. return ControlError;
  61. }
  62. while (!(DataINEndpoint) || !(DataOUTEndpoint) || !(NotificationEndpoint))
  63. {
  64. /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
  65. if (!(RNDISControlInterface) ||
  66. USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  67. DComp_NextCDCDataInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  68. {
  69. /* Check if we have already found the control interface's notification endpoint or not */
  70. if (NotificationEndpoint)
  71. {
  72. /* Get the next RNDIS data interface from the configuration descriptor */
  73. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  74. DComp_NextCDCDataInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  75. {
  76. /* Descriptor not found, error out */
  77. return NoCompatibleInterfaceFound;
  78. }
  79. /* Clear any found endpoints */
  80. DataINEndpoint = NULL;
  81. DataOUTEndpoint = NULL;
  82. }
  83. else
  84. {
  85. /* Get the next RNDIS control interface from the configuration descriptor */
  86. if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
  87. DComp_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  88. {
  89. /* Descriptor not found, error out */
  90. return NoCompatibleInterfaceFound;
  91. }
  92. /* Save the interface in case we need to refer back to it later */
  93. RNDISControlInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
  94. /* Clear any found endpoints */
  95. NotificationEndpoint = NULL;
  96. }
  97. /* Skip the remainder of the loop as we have not found an endpoint yet */
  98. continue;
  99. }
  100. /* Retrieve the endpoint address from the endpoint descriptor */
  101. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
  102. /* If the endpoint is a IN type endpoint */
  103. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  104. {
  105. /* Check if the found endpoint is a interrupt or bulk type descriptor */
  106. if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
  107. NotificationEndpoint = EndpointData;
  108. else
  109. DataINEndpoint = EndpointData;
  110. }
  111. else
  112. {
  113. DataOUTEndpoint = EndpointData;
  114. }
  115. }
  116. /* Configure the RNDIS data IN pipe */
  117. Pipe_ConfigurePipe(RNDIS_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
  118. /* Configure the RNDIS data OUT pipe */
  119. Pipe_ConfigurePipe(RNDIS_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
  120. /* Configure the RNDIS notification pipe */
  121. Pipe_ConfigurePipe(RNDIS_NOTIFICATION_PIPE, EP_TYPE_INTERRUPT, NotificationEndpoint->EndpointAddress, NotificationEndpoint->EndpointSize, 1);
  122. Pipe_SetInterruptPeriod(NotificationEndpoint->PollingIntervalMS);
  123. /* Valid data found, return success */
  124. return SuccessfulConfigRead;
  125. }
  126. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  127. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  128. * descriptor processing if an incompatible descriptor configuration is found.
  129. *
  130. * This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values.
  131. *
  132. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  133. */
  134. uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor)
  135. {
  136. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  137. if (Header->Type == DTYPE_Interface)
  138. {
  139. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  140. /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */
  141. if ((Interface->Class == CDC_CSCP_CDCClass) &&
  142. (Interface->SubClass == CDC_CSCP_ACMSubclass) &&
  143. (Interface->Protocol == CDC_CSCP_VendorSpecificProtocol))
  144. {
  145. return DESCRIPTOR_SEARCH_Found;
  146. }
  147. }
  148. return DESCRIPTOR_SEARCH_NotFound;
  149. }
  150. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  151. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  152. * descriptor processing if an incompatible descriptor configuration is found.
  153. *
  154. * This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values.
  155. *
  156. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  157. */
  158. uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor)
  159. {
  160. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  161. if (Header->Type == DTYPE_Interface)
  162. {
  163. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  164. /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */
  165. if ((Interface->Class == CDC_CSCP_CDCDataClass) &&
  166. (Interface->SubClass == CDC_CSCP_NoDataSubclass) &&
  167. (Interface->Protocol == CDC_CSCP_NoDataProtocol))
  168. {
  169. return DESCRIPTOR_SEARCH_Found;
  170. }
  171. }
  172. return DESCRIPTOR_SEARCH_NotFound;
  173. }
  174. /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
  175. * configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
  176. * descriptor processing if an incompatible descriptor configuration is found.
  177. *
  178. * This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface,
  179. * aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared
  180. * using a different comparator to determine if it is another CDC class interface).
  181. *
  182. * \return A value from the DSEARCH_Return_ErrorCodes_t enum
  183. */
  184. uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor)
  185. {
  186. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  187. if (Header->Type == DTYPE_Endpoint)
  188. {
  189. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  190. /* Check the endpoint type, break out if correct BULK or INTERRUPT type endpoint found */
  191. if (((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK) ||
  192. ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT))
  193. {
  194. return DESCRIPTOR_SEARCH_Found;
  195. }
  196. }
  197. else if (Header->Type == DTYPE_Interface)
  198. {
  199. return DESCRIPTOR_SEARCH_Fail;
  200. }
  201. return DESCRIPTOR_SEARCH_NotFound;
  202. }