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.

ConfigDescriptors.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * \brief USB Configuration Descriptor definitions.
  28. * \copydetails Group_ConfigDescriptorParser
  29. *
  30. * \note This file should not be included directly. It is automatically included as needed by the USB driver
  31. * dispatch header located in LUFA/Drivers/USB/USB.h.
  32. */
  33. /** \ingroup Group_StdDescriptors
  34. * \defgroup Group_ConfigDescriptorParser Configuration Descriptor Parser
  35. * \brief USB Configuration Descriptor definitions.
  36. *
  37. * This section of the library gives a friendly API which can be used in host applications to easily
  38. * parse an attached device's configuration descriptor so that endpoint, interface and other descriptor
  39. * data can be extracted and used as needed.
  40. *
  41. * @{
  42. */
  43. #ifndef __CONFIGDESCRIPTORS_H__
  44. #define __CONFIGDESCRIPTORS_H__
  45. /* Includes: */
  46. #include "../../../Common/Common.h"
  47. #include "USBMode.h"
  48. #include "HostStandardReq.h"
  49. #include "StdDescriptors.h"
  50. /* Enable C linkage for C++ Compilers: */
  51. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. /* Preprocessor Checks: */
  55. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  56. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  57. #endif
  58. /* Public Interface - May be used in end-application: */
  59. /* Macros: */
  60. /** Casts a pointer to a descriptor inside the configuration descriptor into a pointer to the given
  61. * descriptor type.
  62. *
  63. * Usage Example:
  64. * \code
  65. * uint8_t* CurrDescriptor = &ConfigDescriptor[0]; // Pointing to the configuration header
  66. * USB_Descriptor_Configuration_Header_t* ConfigHeaderPtr = DESCRIPTOR_PCAST(CurrDescriptor,
  67. * USB_Descriptor_Configuration_Header_t);
  68. *
  69. * // Can now access elements of the configuration header struct using the -> indirection operator
  70. * \endcode
  71. */
  72. #define DESCRIPTOR_PCAST(DescriptorPtr, Type) ((Type*)(DescriptorPtr))
  73. /** Casts a pointer to a descriptor inside the configuration descriptor into the given descriptor
  74. * type (as an actual struct instance rather than a pointer to a struct).
  75. *
  76. * Usage Example:
  77. * \code
  78. * uint8_t* CurrDescriptor = &ConfigDescriptor[0]; // Pointing to the configuration header
  79. * USB_Descriptor_Configuration_Header_t ConfigHeader = DESCRIPTOR_CAST(CurrDescriptor,
  80. * USB_Descriptor_Configuration_Header_t);
  81. *
  82. * // Can now access elements of the configuration header struct using the . operator
  83. * \endcode
  84. */
  85. #define DESCRIPTOR_CAST(DescriptorPtr, Type) (*DESCRIPTOR_PCAST(DescriptorPtr, Type))
  86. /** Returns the descriptor's type, expressed as the 8-bit type value in the header of the descriptor.
  87. * This value's meaning depends on the descriptor's placement in the descriptor, but standard type
  88. * values can be accessed in the \ref USB_DescriptorTypes_t enum.
  89. */
  90. #define DESCRIPTOR_TYPE(DescriptorPtr) DESCRIPTOR_PCAST(DescriptorPtr, USB_Descriptor_Header_t)->Type
  91. /** Returns the descriptor's size, expressed as the 8-bit value indicating the number of bytes. */
  92. #define DESCRIPTOR_SIZE(DescriptorPtr) DESCRIPTOR_PCAST(DescriptorPtr, USB_Descriptor_Header_t)->Size
  93. /* Type Defines: */
  94. /** Type define for a Configuration Descriptor comparator function (function taking a pointer to an array
  95. * of type void, returning a uint8_t value).
  96. *
  97. * \see \ref USB_GetNextDescriptorComp function for more details.
  98. */
  99. typedef uint8_t (* ConfigComparatorPtr_t)(void*);
  100. /* Enums: */
  101. /** Enum for the possible return codes of the \ref USB_Host_GetDeviceConfigDescriptor() function. */
  102. enum USB_Host_GetConfigDescriptor_ErrorCodes_t
  103. {
  104. HOST_GETCONFIG_Successful = 0, /**< No error occurred while retrieving the configuration descriptor. */
  105. HOST_GETCONFIG_DeviceDisconnect = 1, /**< The attached device was disconnected while retrieving the configuration
  106. * descriptor.
  107. */
  108. HOST_GETCONFIG_PipeError = 2, /**< An error occurred in the pipe while sending the request. */
  109. HOST_GETCONFIG_SetupStalled = 3, /**< The attached device stalled the request to retrieve the configuration
  110. * descriptor.
  111. */
  112. HOST_GETCONFIG_SoftwareTimeOut = 4, /**< The request or data transfer timed out. */
  113. HOST_GETCONFIG_BuffOverflow = 5, /**< The device's configuration descriptor is too large to fit into the allocated
  114. * buffer.
  115. */
  116. HOST_GETCONFIG_InvalidData = 6, /**< The device returned invalid configuration descriptor data. */
  117. };
  118. /** Enum for return values of a descriptor comparator function. */
  119. enum DSearch_Return_ErrorCodes_t
  120. {
  121. DESCRIPTOR_SEARCH_Found = 0, /**< Current descriptor matches comparator criteria. */
  122. DESCRIPTOR_SEARCH_Fail = 1, /**< No further descriptor could possibly match criteria, fail the search. */
  123. DESCRIPTOR_SEARCH_NotFound = 2, /**< Current descriptor does not match comparator criteria. */
  124. };
  125. /** Enum for return values of \ref USB_GetNextDescriptorComp(). */
  126. enum DSearch_Comp_Return_ErrorCodes_t
  127. {
  128. DESCRIPTOR_SEARCH_COMP_Found = 0, /**< Configuration descriptor now points to descriptor which matches
  129. * search criteria of the given comparator function. */
  130. DESCRIPTOR_SEARCH_COMP_Fail = 1, /**< Comparator function returned \ref DESCRIPTOR_SEARCH_Fail. */
  131. DESCRIPTOR_SEARCH_COMP_EndOfDescriptor = 2, /**< End of configuration descriptor reached before match found. */
  132. };
  133. /* Function Prototypes: */
  134. /** Retrieves the configuration descriptor data from an attached device via a standard request into a buffer,
  135. * including validity and size checking to prevent a buffer overflow.
  136. *
  137. * \param[in] ConfigNumber Device configuration descriptor number to fetch from the device (usually set to 1 for
  138. * single configuration devices).
  139. * \param[in,out] ConfigSizePtr Pointer to a location for storing the retrieved configuration descriptor size.
  140. * \param[out] BufferPtr Pointer to the buffer for storing the configuration descriptor data.
  141. * \param[out] BufferSize Size of the allocated buffer where the configuration descriptor is to be stored.
  142. *
  143. * \return A value from the \ref USB_Host_GetConfigDescriptor_ErrorCodes_t enum.
  144. */
  145. uint8_t USB_Host_GetDeviceConfigDescriptor(const uint8_t ConfigNumber,
  146. uint16_t* const ConfigSizePtr,
  147. void* const BufferPtr,
  148. const uint16_t BufferSize) ATTR_NON_NULL_PTR_ARG(2) ATTR_NON_NULL_PTR_ARG(3);
  149. /** Skips to the next sub-descriptor inside the configuration descriptor of the specified type value.
  150. * The bytes remaining value is automatically decremented.
  151. *
  152. * \param[in,out] BytesRem Pointer to the number of bytes remaining of the configuration descriptor.
  153. * \param[in,out] CurrConfigLoc Pointer to the current descriptor inside the configuration descriptor.
  154. * \param[in] Type Descriptor type value to search for.
  155. */
  156. void USB_GetNextDescriptorOfType(uint16_t* const BytesRem,
  157. void** const CurrConfigLoc,
  158. const uint8_t Type)
  159. ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  160. /** Skips to the next sub-descriptor inside the configuration descriptor of the specified type value,
  161. * which must come before a descriptor of the second given type value. If the BeforeType type
  162. * descriptor is reached first, the number of bytes remaining to process is set to zero and the
  163. * function exits. The bytes remaining value is automatically decremented.
  164. *
  165. * \param[in,out] BytesRem Pointer to the number of bytes remaining of the configuration descriptor.
  166. * \param[in,out] CurrConfigLoc Pointer to the current descriptor inside the configuration descriptor.
  167. * \param[in] Type Descriptor type value to search for.
  168. * \param[in] BeforeType Descriptor type value which must not be reached before the given Type descriptor.
  169. */
  170. void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem,
  171. void** const CurrConfigLoc,
  172. const uint8_t Type,
  173. const uint8_t BeforeType)
  174. ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  175. /** Skips to the next sub-descriptor inside the configuration descriptor of the specified type value,
  176. * which must come after a descriptor of the second given type value. The bytes remaining value is
  177. * automatically decremented.
  178. *
  179. * \param[in,out] BytesRem Pointer to the number of bytes remaining of the configuration descriptor.
  180. * \param[in,out] CurrConfigLoc Pointer to the current descriptor inside the configuration descriptor.
  181. * \param[in] Type Descriptor type value to search for.
  182. * \param[in] AfterType Descriptor type value which must be reached before the given Type descriptor.
  183. */
  184. void USB_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem,
  185. void** const CurrConfigLoc,
  186. const uint8_t Type,
  187. const uint8_t AfterType)
  188. ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  189. /** Searches for the next descriptor in the given configuration descriptor using a pre-made comparator
  190. * function. The routine updates the position and remaining configuration descriptor bytes values
  191. * automatically. If a comparator routine fails a search, the descriptor pointer is retreated back
  192. * so that the next descriptor search invocation will start from the descriptor which first caused the
  193. * original search to fail. This behavior allows for one comparator to be used immediately after another
  194. * has failed, starting the second search from the descriptor which failed the first.
  195. *
  196. * Comparator functions should be standard functions which accept a pointer to the header of the current
  197. * descriptor inside the configuration descriptor which is being compared, and should return a value from
  198. * the \ref DSearch_Return_ErrorCodes_t enum as a uint8_t value.
  199. *
  200. * \note This function is available in USB Host mode only.
  201. *
  202. * \param[in,out] BytesRem Pointer to an int storing the remaining bytes in the configuration descriptor.
  203. * \param[in,out] CurrConfigLoc Pointer to the current position in the configuration descriptor.
  204. * \param[in] ComparatorRoutine Name of the comparator search function to use on the configuration descriptor.
  205. *
  206. * \return Value of one of the members of the \ref DSearch_Comp_Return_ErrorCodes_t enum.
  207. *
  208. * Usage Example:
  209. * \code
  210. * uint8_t EndpointSearcher(void* CurrentDescriptor); // Comparator Prototype
  211. *
  212. * uint8_t EndpointSearcher(void* CurrentDescriptor)
  213. * {
  214. * if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
  215. * return DESCRIPTOR_SEARCH_Found;
  216. * else
  217. * return DESCRIPTOR_SEARCH_NotFound;
  218. * }
  219. *
  220. * //...
  221. *
  222. * // After retrieving configuration descriptor:
  223. * if (USB_Host_GetNextDescriptorComp(&BytesRemaining, &CurrentConfigLoc, EndpointSearcher) ==
  224. * Descriptor_Search_Comp_Found)
  225. * {
  226. * // Do something with the endpoint descriptor
  227. * }
  228. * \endcode
  229. */
  230. uint8_t USB_GetNextDescriptorComp(uint16_t* const BytesRem,
  231. void** const CurrConfigLoc,
  232. ConfigComparatorPtr_t const ComparatorRoutine) ATTR_NON_NULL_PTR_ARG(1)
  233. ATTR_NON_NULL_PTR_ARG(2) ATTR_NON_NULL_PTR_ARG(3);
  234. /* Inline Functions: */
  235. /** Skips over the current sub-descriptor inside the configuration descriptor, so that the pointer then
  236. points to the next sub-descriptor. The bytes remaining value is automatically decremented.
  237. *
  238. * \param[in,out] BytesRem Pointer to the number of bytes remaining of the configuration descriptor.
  239. * \param[in,out] CurrConfigLoc Pointer to the current descriptor inside the configuration descriptor.
  240. */
  241. static inline void USB_GetNextDescriptor(uint16_t* const BytesRem,
  242. void** CurrConfigLoc) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  243. static inline void USB_GetNextDescriptor(uint16_t* const BytesRem,
  244. void** CurrConfigLoc)
  245. {
  246. uint16_t CurrDescriptorSize = DESCRIPTOR_CAST(*CurrConfigLoc, USB_Descriptor_Header_t).Size;
  247. if (*BytesRem < CurrDescriptorSize)
  248. CurrDescriptorSize = *BytesRem;
  249. *CurrConfigLoc = (void*)((uintptr_t)*CurrConfigLoc + CurrDescriptorSize);
  250. *BytesRem -= CurrDescriptorSize;
  251. }
  252. /* Disable C linkage for C++ Compilers: */
  253. #if defined(__cplusplus)
  254. }
  255. #endif
  256. #endif
  257. /** @} */