HIDClassHost.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 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 Host mode driver for the library USB HID Class driver.
  28. *
  29. * Host mode driver for the library USB HID Class driver.
  30. *
  31. * \note This file should not be included directly. It is automatically included as needed by the USB module driver
  32. * dispatch header located in LUFA/Drivers/USB.h.
  33. */
  34. /** \ingroup Group_USBClassHID
  35. * \defgroup Group_USBClassHIDHost HID Class Host Mode Driver
  36. *
  37. * \section Sec_USBClassHIDHost_Dependencies Module Source Dependencies
  38. * The following files must be built with any user project that uses this module:
  39. * - LUFA/Drivers/USB/Class/Host/HIDClassHost.c <i>(Makefile source module name: LUFA_SRC_USBCLASS)</i>
  40. *
  41. * \section Sec_USBClassHIDHost_ModDescription Module Description
  42. * Host Mode USB Class driver framework interface, for the HID USB Class driver.
  43. *
  44. * @{
  45. */
  46. #ifndef __HID_CLASS_HOST_H__
  47. #define __HID_CLASS_HOST_H__
  48. /* Includes: */
  49. #include "../../USB.h"
  50. #include "../Common/HIDClassCommon.h"
  51. /* Enable C linkage for C++ Compilers: */
  52. #if defined(__cplusplus)
  53. extern "C" {
  54. #endif
  55. /* Preprocessor Checks: */
  56. #if !defined(__INCLUDE_FROM_HID_DRIVER)
  57. #error Do not include this file directly. Include LUFA/Drivers/USB.h instead.
  58. #endif
  59. /* Public Interface - May be used in end-application: */
  60. /* Macros: */
  61. /** Error code for some HID Host functions, indicating a logical (and not hardware) error. */
  62. #define HID_ERROR_LOGICAL 0x80
  63. /* Type Defines: */
  64. /** \brief HID Class Host Mode Configuration and State Structure.
  65. *
  66. * Class state structure. An instance of this structure should be made within the user application,
  67. * and passed to each of the HID class driver functions as the \c HIDInterfaceInfo parameter. This
  68. * stores each HID interface's configuration and state information.
  69. */
  70. typedef struct
  71. {
  72. struct
  73. {
  74. USB_Pipe_Table_t DataINPipe; /**< Data IN Pipe configuration table. */
  75. USB_Pipe_Table_t DataOUTPipe; /**< Data OUT Pipe configuration table. */
  76. uint8_t HIDInterfaceProtocol; /**< HID interface protocol value to match against if a specific
  77. * boot subclass protocol is required, a protocol value from the
  78. * \ref HID_Descriptor_ClassSubclassProtocol_t enum.
  79. */
  80. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  81. HID_ReportInfo_t* HIDParserData; /**< HID parser data to store the parsed HID report data, when boot protocol
  82. * is not used.
  83. *
  84. * \note When the \c HID_HOST_BOOT_PROTOCOL_ONLY compile time token is defined,
  85. * this field is unavailable.
  86. */
  87. #endif
  88. } Config; /**< Config data for the USB class interface within the device. All elements in this section
  89. * <b>must</b> be set or the interface will fail to enumerate and operate correctly.
  90. */
  91. struct
  92. {
  93. bool IsActive; /**< Indicates if the current interface instance is connected to an attached device, valid
  94. * after \ref HID_Host_ConfigurePipes() is called and the Host state machine is in the
  95. * Configured state.
  96. */
  97. uint8_t InterfaceNumber; /**< Interface index of the HID interface within the attached device. */
  98. bool SupportsBootProtocol; /**< Indicates if the current interface instance supports the HID Boot
  99. * Protocol when enabled via \ref HID_Host_SetBootProtocol().
  100. */
  101. bool DeviceUsesOUTPipe; /**< Indicates if the current interface instance uses a separate OUT data pipe for
  102. * OUT reports, or if OUT reports are sent via the control pipe instead.
  103. */
  104. bool UsingBootProtocol; /**< Indicates that the interface is currently initialized in Boot Protocol mode */
  105. uint16_t HIDReportSize; /**< Size in bytes of the HID report descriptor in the device. */
  106. uint8_t LargestReportSize; /**< Largest report the device will send, in bytes. */
  107. } State; /**< State data for the USB class interface within the device. All elements in this section
  108. * <b>may</b> be set to initial values, but may also be ignored to default to sane values when
  109. * the interface is enumerated.
  110. */
  111. } USB_ClassInfo_HID_Host_t;
  112. /* Enums: */
  113. /** Enum for the possible error codes returned by the \ref HID_Host_ConfigurePipes() function. */
  114. enum HID_Host_EnumerationFailure_ErrorCodes_t
  115. {
  116. HID_ENUMERROR_NoError = 0, /**< Configuration Descriptor was processed successfully. */
  117. HID_ENUMERROR_InvalidConfigDescriptor = 1, /**< The device returned an invalid Configuration Descriptor. */
  118. HID_ENUMERROR_NoCompatibleInterfaceFound = 2, /**< A compatible HID interface was not found in the device's Configuration Descriptor. */
  119. HID_ENUMERROR_PipeConfigurationFailed = 3, /**< One or more pipes for the specified interface could not be configured correctly. */
  120. };
  121. /* Function Prototypes: */
  122. /** Host interface configuration routine, to configure a given HID host interface instance using the Configuration
  123. * Descriptor read from an attached USB device. This function automatically updates the given HID Host instance's
  124. * state values and configures the pipes required to communicate with the interface if it is found within the
  125. * device. This should be called once after the stack has enumerated the attached device, while the host state
  126. * machine is in the Addressed state.
  127. *
  128. * \attention Once the device pipes are configured, the HID device's reporting protocol <b>must</b> be set via a call
  129. * to either the \ref HID_Host_SetBootProtocol() or \ref HID_Host_SetReportProtocol() function.
  130. *
  131. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  132. * \param[in] ConfigDescriptorSize Length of the attached device's Configuration Descriptor.
  133. * \param[in] ConfigDescriptorData Pointer to a buffer containing the attached device's Configuration Descriptor.
  134. *
  135. * \return A value from the \ref HID_Host_EnumerationFailure_ErrorCodes_t enum.
  136. */
  137. uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  138. uint16_t ConfigDescriptorSize,
  139. void* ConfigDescriptorData) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
  140. /** Receives a HID IN report from the attached HID device, when a report has been received on the HID IN Data pipe.
  141. *
  142. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  143. * call will fail.
  144. *
  145. * \attention The destination buffer should be large enough to accommodate the largest report that the attached device
  146. * can generate.
  147. *
  148. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  149. * \param[in] Buffer Buffer to store the received report into.
  150. *
  151. * \return An error code from the \ref Pipe_Stream_RW_ErrorCodes_t enum.
  152. */
  153. uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  154. void* Buffer) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(2);
  155. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  156. /** Receives a HID IN report from the attached device, by the report ID.
  157. *
  158. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  159. * call will fail.
  160. *
  161. * \note When the \c HID_HOST_BOOT_PROTOCOL_ONLY compile time token is defined, this method is unavailable.
  162. *
  163. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  164. * \param[in] ReportID Report ID of the received report if ControlRequest is false, set by the to the Report ID to fetch.
  165. * \param[in] Buffer Buffer to store the received report into.
  166. *
  167. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
  168. */
  169. uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  170. const uint8_t ReportID,
  171. void* Buffer) ATTR_NON_NULL_PTR_ARG(1) ATTR_NON_NULL_PTR_ARG(3);
  172. #endif
  173. /** Sends an OUT or FEATURE report to the currently attached HID device, using the device's OUT pipe if available,
  174. * or the device's Control pipe if not.
  175. *
  176. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  177. * call will fail.
  178. *
  179. * \note When the \c HID_HOST_BOOT_PROTOCOL_ONLY compile time token is defined, the ReportID parameter is removed
  180. * from the parameter list of this function.
  181. *
  182. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  183. * \param[in] ReportID Report ID of the report to send to the device, or 0 if the device does not use report IDs.
  184. * \param[in] ReportType Type of report to issue to the device, either \ref HID_REPORT_ITEM_Out or \ref HID_REPORT_ITEM_Feature.
  185. * \param[in] Buffer Buffer containing the report to send to the attached device.
  186. * \param[in] ReportSize Report size in bytes to send to the attached device.
  187. *
  188. * \return An error code from the \ref USB_Host_SendControlErrorCodes_t enum if the DeviceUsesOUTPipe flag is set in
  189. * the interface's state structure, a value from the \ref Pipe_Stream_RW_ErrorCodes_t enum otherwise.
  190. */
  191. uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  192. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  193. const uint8_t ReportID,
  194. #endif
  195. const uint8_t ReportType,
  196. void* Buffer,
  197. const uint16_t ReportSize) ATTR_NON_NULL_PTR_ARG(1)
  198. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  199. ATTR_NON_NULL_PTR_ARG(4);
  200. #else
  201. ATTR_NON_NULL_PTR_ARG(3);
  202. #endif
  203. /** Determines if a HID IN report has been received from the attached device on the data IN pipe.
  204. *
  205. * \pre This function must only be called when the Host state machine is in the \ref HOST_STATE_Configured state or the
  206. * call will fail.
  207. *
  208. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  209. *
  210. * \return Boolean \c true if a report has been received, \c false otherwise.
  211. */
  212. bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  213. /** Switches the attached HID device's reporting protocol over to the Boot Report protocol mode, on supported devices.
  214. *
  215. * \note When the \c HID_HOST_BOOT_PROTOCOL_ONLY compile time token is defined, this method must still be called
  216. * to explicitly place the attached device into boot protocol mode before use.
  217. *
  218. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  219. *
  220. * \return \ref HID_ERROR_LOGICAL if the device does not support Boot Protocol mode, a value from the
  221. * \ref USB_Host_SendControlErrorCodes_t enum otherwise.
  222. */
  223. uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  224. /** Sets the idle period for the attached HID device to the specified interval. The HID idle period determines the rate
  225. * at which the device should send a report, when no state changes have occurred; i.e. on HID keyboards, this sets the
  226. * hardware key repeat interval.
  227. *
  228. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  229. * \param[in] MS Idle period as a multiple of four milliseconds, zero to disable hardware repeats
  230. *
  231. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum.
  232. */
  233. uint8_t HID_Host_SetIdlePeriod(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  234. const uint16_t MS) ATTR_NON_NULL_PTR_ARG(1);
  235. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  236. /** Switches the attached HID device's reporting protocol over to the standard Report protocol mode. This also retrieves
  237. * and parses the device's HID report descriptor, so that the size of each report can be determined in advance.
  238. *
  239. * \attention Whether this function is used or not, the \ref CALLBACK_HIDParser_FilterHIDReportItem() callback from the HID
  240. * Report Parser this function references <b>must</b> be implemented in the user code.
  241. *
  242. * \note When the \c HID_HOST_BOOT_PROTOCOL_ONLY compile time token is defined, this method is unavailable.
  243. *
  244. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  245. *
  246. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum if an error occurs while retrieving the HID
  247. * Report descriptor or the setting of the Report protocol, \ref HID_ERROR_LOGICAL if the HID interface does
  248. * not have a valid \ref HID_ReportInfo_t structure set in its configuration, a mask of \ref HID_ERROR_LOGICAL
  249. * and a value from the \ref HID_Parse_ErrorCodes_t otherwise.
  250. */
  251. uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  252. #endif
  253. /* Inline Functions: */
  254. /** General management task for a given Human Interface Class host class interface, required for the correct operation of
  255. * the interface. This should be called frequently in the main program loop, before the master USB management task
  256. * \ref USB_USBTask().
  257. *
  258. * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state.
  259. */
  260. static inline void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1);
  261. static inline void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
  262. {
  263. (void)HIDInterfaceInfo;
  264. }
  265. /* Private Interface - For use in library only: */
  266. #if !defined(__DOXYGEN__)
  267. /* Function Prototypes: */
  268. #if defined(__INCLUDE_FROM_HID_HOST_C)
  269. static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor)
  270. ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  271. static uint8_t DCOMP_HID_Host_NextHIDDescriptor(void* const CurrentDescriptor)
  272. ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  273. static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor)
  274. ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  275. #endif
  276. #endif
  277. /* Disable C linkage for C++ Compilers: */
  278. #if defined(__cplusplus)
  279. }
  280. #endif
  281. #endif
  282. /** @} */