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.

HostStandardReq.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 host standard request management.
  28. *
  29. * This file contains the function prototypes necessary for the issuing of outgoing standard control requests
  30. * when the library is in USB host mode.
  31. *
  32. * \note This file should not be included directly. It is automatically included as needed by the USB driver
  33. * dispatch header located in LUFA/Drivers/USB/USB.h.
  34. */
  35. #ifndef __HOSTSTDREQ_H__
  36. #define __HOSTSTDREQ_H__
  37. /* Includes: */
  38. #include "../../../Common/Common.h"
  39. #include "USBMode.h"
  40. #include "StdRequestType.h"
  41. #include "USBController.h"
  42. /* Enable C linkage for C++ Compilers: */
  43. #if defined(__cplusplus)
  44. extern "C" {
  45. #endif
  46. /* Preprocessor Checks: */
  47. #if !defined(__INCLUDE_FROM_USB_DRIVER)
  48. #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
  49. #endif
  50. /* Public Interface - May be used in end-application: */
  51. /* Macros: */
  52. #if !defined(USB_HOST_TIMEOUT_MS) || defined(__DOXYGEN__)
  53. /** Constant for the maximum software timeout period of sent USB control transactions to an attached
  54. * device. If a device fails to respond to a sent control request within this period, the
  55. * library will return a timeout error code.
  56. *
  57. * This value may be overridden in the user project makefile as the value of the
  58. * \ref USB_HOST_TIMEOUT_MS token, and passed to the compiler using the -D switch.
  59. */
  60. #define USB_HOST_TIMEOUT_MS 1000
  61. #endif
  62. /* Enums: */
  63. /** Enum for the \ref USB_Host_SendControlRequest() return code, indicating the reason for the error
  64. * if the transfer of the request is unsuccessful.
  65. *
  66. * \ingroup Group_PipeControlReq
  67. */
  68. enum USB_Host_SendControlErrorCodes_t
  69. {
  70. HOST_SENDCONTROL_Successful = 0, /**< No error occurred in the request transfer. */
  71. HOST_SENDCONTROL_DeviceDisconnected = 1, /**< The attached device was disconnected during the
  72. * request transfer.
  73. */
  74. HOST_SENDCONTROL_PipeError = 2, /**< An error occurred in the pipe while sending the request. */
  75. HOST_SENDCONTROL_SetupStalled = 3, /**< The attached device stalled the request, usually
  76. * indicating that the request is unsupported on the device.
  77. */
  78. HOST_SENDCONTROL_SoftwareTimeOut = 4, /**< The request or data transfer timed out. */
  79. };
  80. /* Global Variables: */
  81. /** Indicates the currently set configuration number of the attached device. This indicates the currently
  82. * selected configuration value if one has been set successfully, or 0 if no configuration has been selected.
  83. *
  84. * To set a device configuration, call the \ref USB_Host_SetDeviceConfiguration() function.
  85. *
  86. * \attention This variable should be treated as read-only in the user application, and never manually
  87. * changed in value.
  88. *
  89. * \ingroup Group_Host
  90. */
  91. extern uint8_t USB_Host_ConfigurationNumber;
  92. /* Function Prototypes: */
  93. /** Sends the request stored in the \ref USB_ControlRequest global structure to the attached device,
  94. * and transfers the data stored in the buffer to the device, or from the device to the buffer
  95. * as requested. The transfer is made on the currently selected pipe.
  96. *
  97. * \ingroup Group_PipeControlReq
  98. *
  99. * \param[in] BufferPtr Pointer to the start of the data buffer if the request has a data stage, or
  100. * \c NULL if the request transfers no data to or from the device.
  101. *
  102. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  103. */
  104. uint8_t USB_Host_SendControlRequest(void* const BufferPtr);
  105. /** Sends a SET CONFIGURATION standard request to the attached device, with the given configuration index.
  106. *
  107. * This routine will automatically update the \ref USB_HostState and \ref USB_Host_ConfigurationNumber
  108. * state variables according to the given function parameters and the result of the request.
  109. *
  110. * \note After this routine returns, the control pipe will be selected.
  111. *
  112. * \ingroup Group_PipeControlReq
  113. *
  114. * \param[in] ConfigNumber Configuration index to send to the device.
  115. *
  116. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  117. */
  118. uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber);
  119. /** Sends a GET CONFIGURATION standard request to the attached device, to retrieve the currently selected
  120. * device configuration index.
  121. *
  122. * \note After this routine returns, the control pipe will be selected.
  123. *
  124. * \ingroup Group_PipeControlReq
  125. *
  126. * \param[out] ConfigNumber Pointer to a location where the retrieved configuration index should be stored.
  127. *
  128. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  129. */
  130. uint8_t USB_Host_GetDeviceConfiguration(uint8_t* const ConfigNumber) ATTR_NON_NULL_PTR_ARG(1);
  131. /** Sends a GET DESCRIPTOR standard request to the attached device, requesting the descriptor of the
  132. * specified type and index.
  133. *
  134. * \note After this routine returns, the control pipe will be selected.
  135. *
  136. * \ingroup Group_PipeControlReq
  137. *
  138. * \param[in] Type Type of descriptor to retrieve, a value from the \ref USB_DescriptorTypes_t enum.
  139. * \param[in] Index Index of the descriptor to retrieve.
  140. * \param[out] Buffer Pointer to the destination buffer where the retrieved string descriptor is to be stored.
  141. * \param[in] BufferLength Maximum size of the string descriptor which can be stored into the buffer.
  142. *
  143. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  144. */
  145. uint8_t USB_Host_GetDescriptor(const uint8_t Type,
  146. const uint8_t Index,
  147. void* const Buffer,
  148. const uint8_t BufferLength) ATTR_NON_NULL_PTR_ARG(3);
  149. /** Retrieves the current feature status of the attached device, via a GET STATUS standard request. The
  150. * retrieved feature status can then be examined by masking the retrieved value with the various
  151. * \c FEATURE_* masks for bus/self power information and remote wakeup support.
  152. *
  153. * \note After this routine returns, the control pipe will be selected.
  154. *
  155. * \ingroup Group_PipeControlReq
  156. *
  157. * \param[out] FeatureStatus Location where the retrieved feature status should be stored.
  158. *
  159. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  160. */
  161. uint8_t USB_Host_GetDeviceStatus(uint8_t* const FeatureStatus) ATTR_NON_NULL_PTR_ARG(1);
  162. /** Clears a stall condition on the given pipe, via a CLEAR FEATURE standard request to the attached device.
  163. *
  164. * \note After this routine returns, the control pipe will be selected.
  165. *
  166. * \ingroup Group_PipeControlReq
  167. *
  168. * \param[in] EndpointAddress Address of the endpoint to clear, including the endpoint's direction.
  169. *
  170. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  171. */
  172. uint8_t USB_Host_ClearEndpointStall(const uint8_t EndpointAddress);
  173. /** Selects a given alternative setting for the specified interface, via a SET INTERFACE standard request to
  174. * the attached device.
  175. *
  176. * \note After this routine returns, the control pipe will be selected.
  177. *
  178. * \ingroup Group_PipeControlReq
  179. *
  180. * \param[in] InterfaceIndex Index of the interface whose alternative setting is to be altered.
  181. * \param[in] AltSetting Index of the interface's alternative setting which is to be selected.
  182. *
  183. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  184. */
  185. uint8_t USB_Host_SetInterfaceAltSetting(const uint8_t InterfaceIndex,
  186. const uint8_t AltSetting);
  187. /** Retrieves the current alternative setting for the specified interface, via a GET INTERFACE standard request to
  188. * the attached device.
  189. *
  190. * \note After this routine returns, the control pipe will be selected.
  191. *
  192. * \ingroup Group_PipeControlReq
  193. *
  194. * \param[in] InterfaceIndex Index of the interface whose alternative setting is to be altered.
  195. * \param[out] AltSetting Pointer to a location where the retrieved alternative setting value should be stored.
  196. *
  197. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  198. */
  199. uint8_t USB_Host_GetInterfaceAltSetting(const uint8_t InterfaceIndex,
  200. uint8_t* const AltSetting) ATTR_NON_NULL_PTR_ARG(2);
  201. /* Inline Functions: */
  202. /** Sends a GET DESCRIPTOR standard request to the attached device, requesting the device descriptor.
  203. * This can be used to easily retrieve information about the device such as its VID, PID and power
  204. * requirements. This is a convenience wrapper for \ref USB_Host_GetDescriptor().
  205. *
  206. * \note After this routine returns, the control pipe will be selected.
  207. *
  208. * \ingroup Group_PipeControlReq
  209. *
  210. * \param[out] DeviceDescriptorPtr Pointer to the destination device descriptor structure where
  211. * the read data is to be stored.
  212. *
  213. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  214. */
  215. static inline uint8_t USB_Host_GetDeviceDescriptor(USB_Descriptor_Device_t* const DeviceDescriptorPtr) ATTR_NON_NULL_PTR_ARG(1);
  216. static inline uint8_t USB_Host_GetDeviceDescriptor(USB_Descriptor_Device_t* const DeviceDescriptorPtr)
  217. {
  218. return USB_Host_GetDescriptor(DTYPE_Device, 0, DeviceDescriptorPtr, sizeof(USB_Descriptor_Device_t));
  219. }
  220. /** Sends a GET DESCRIPTOR standard request to the attached device, requesting the string descriptor
  221. * of the specified index. This can be used to easily retrieve string descriptors from the device by
  222. * index, after the index is obtained from the Device or Configuration descriptors. This is a convenience
  223. * wrapper for \ref USB_Host_GetDescriptor().
  224. *
  225. * \note After this routine returns, the control pipe will be selected.
  226. *
  227. * \ingroup Group_PipeControlReq
  228. *
  229. * \param[in] Index Index of the string descriptor to retrieve.
  230. * \param[out] Buffer Pointer to the destination buffer where the retrieved string descriptor is
  231. * to be stored.
  232. * \param[in] BufferLength Maximum size of the string descriptor which can be stored into the buffer.
  233. *
  234. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
  235. */
  236. static inline uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
  237. void* const Buffer,
  238. const uint8_t BufferLength) ATTR_NON_NULL_PTR_ARG(2);
  239. static inline uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
  240. void* const Buffer,
  241. const uint8_t BufferLength)
  242. {
  243. return USB_Host_GetDescriptor(DTYPE_String, Index, Buffer, BufferLength);
  244. }
  245. /* Private Interface - For use in library only: */
  246. #if !defined(__DOXYGEN__)
  247. /* Enums: */
  248. enum USB_WaitForTypes_t
  249. {
  250. USB_HOST_WAITFOR_SetupSent,
  251. USB_HOST_WAITFOR_InReceived,
  252. USB_HOST_WAITFOR_OutReady,
  253. };
  254. /* Function Prototypes: */
  255. #if defined(__INCLUDE_FROM_HOSTSTDREQ_C)
  256. static uint8_t USB_Host_SendControlRequest_PRV(void* const BufferPtr);
  257. static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType);
  258. #endif
  259. #endif
  260. /* Disable C linkage for C++ Compilers: */
  261. #if defined(__cplusplus)
  262. }
  263. #endif
  264. #endif