app_usbd_request.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef APP_USBD_REQUEST_H__
  41. #define APP_USBD_REQUEST_H__
  42. #include "sdk_common.h"
  43. #include "nrf.h"
  44. #include "nrf_drv_usbd.h"
  45. #include "app_usbd_descriptor.h"
  46. #include "app_util_platform.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /* Compiler support for anonymous unions */
  51. ANON_UNIONS_ENABLE;
  52. #pragma pack(push, 1)
  53. /**
  54. * @defgroup app_usbd_request USB standard requests
  55. * @ingroup app_usbd
  56. *
  57. * @brief @tagAPI52840 Module with types definitions used for standard requests processing.
  58. * @{
  59. */
  60. /**
  61. * @brief Recipient bit-field in request type.
  62. *
  63. * Bits 4...0
  64. */
  65. #define APP_USBD_SETUP_REQ_BF_REC BF_CX(5, 0)
  66. /**
  67. * @brief Type bit-field in request type.
  68. *
  69. * Bits 6...5
  70. */
  71. #define APP_USBD_SETUP_REQ_BF_TYP BF_CX(2, 5)
  72. /**
  73. * @brief Direction bit-field in request type.
  74. *
  75. * Bit 7
  76. */
  77. #define APP_USBD_SETUP_REQ_BF_DIR BF_CX(1, 7)
  78. /**
  79. * @brief Recipient enumerator.
  80. *
  81. * @note It is part of @ref app_usbd_setup_reqtype_t variable type.
  82. */
  83. typedef enum {
  84. APP_USBD_SETUP_REQREC_DEVICE = 0x0, /**< The whole device is a request target */
  85. APP_USBD_SETUP_REQREC_INTERFACE = 0x1, /**< Selected interface is a request target */
  86. APP_USBD_SETUP_REQREC_ENDPOINT = 0x2, /**< Selected endpoint is a request target */
  87. APP_USBD_SETUP_REQREC_OTHER = 0x3 /**< Other element is a request target */
  88. } app_usbd_setup_reqrec_t;
  89. /**
  90. * @brief Request type enumerator.
  91. *
  92. * @note It is part of @ref app_usbd_setup_reqtype_t variable type.
  93. */
  94. typedef enum {
  95. APP_USBD_SETUP_REQTYPE_STD = 0x0, /**< Standard request */
  96. APP_USBD_SETUP_REQTYPE_CLASS = 0x1, /**< Class specific request */
  97. APP_USBD_SETUP_REQTYPE_VENDOR = 0x2 /**< Vendor specific request */
  98. } app_usbd_setup_reqtype_t;
  99. /**
  100. * @brief Direction of setup command.
  101. *
  102. * @note It is part of @ref app_usbd_setup_reqtype_t variable type.
  103. */
  104. typedef enum {
  105. APP_USBD_SETUP_REQDIR_OUT = 0x0, /**< Host to device */
  106. APP_USBD_SETUP_REQDIR_IN = 0x1, /**< Device to host */
  107. } app_usbd_setup_reqdir_t;
  108. /**
  109. * @brief Standard requests.
  110. *
  111. * Enumerator for standard requests values.
  112. */
  113. typedef enum {
  114. APP_USBD_SETUP_STDREQ_GET_STATUS = 0x00, /**<
  115. * Targets: Device, Interface, Endpoint
  116. * Expected SETUP frame format:
  117. * - wValue: Zero
  118. * - wIndex: Zero, (lb): Interface or Endpoint
  119. * - wLength: 2
  120. * - Data:2 bytes of data, depending on targets
  121. * - Device:
  122. * - D15..D2: Reserved (Reset to zero)
  123. * - D1: Remove Wakeup
  124. * - D0: Self Powered
  125. * - Interface:
  126. * - D15..D0: Reserved (Reset to zero)
  127. * - Endpoint:
  128. * - D15..D1: Reserved (Reset to zero)
  129. * - D0: Halt
  130. */
  131. APP_USBD_SETUP_STDREQ_CLEAR_FEATURE = 0x01, /**<
  132. * Targets: Device, Interface, Endpoint
  133. * Expected SETUP frame format:
  134. * - wValue: Feature selector (@ref app_usbd_setup_stdfeature_t)
  135. * - wIndex: Zero, Interface or Endpoint
  136. * - wLength: 0
  137. * - Data: None
  138. */
  139. APP_USBD_SETUP_STDREQ_SET_FEATURE = 0x03, /**<
  140. * Targets: Device, Interface, Endpoint
  141. * Expected SETUP frame format:
  142. * - wValue: Feature selector (@ref app_usbd_setup_stdfeature_t)
  143. * - wIndex: Zero, Interface or Endpoint
  144. * - wLength: 0
  145. * - Data: None
  146. */
  147. APP_USBD_SETUP_STDREQ_SET_ADDRESS = 0x05, /**<
  148. * @note This SETUP request is processed in hardware.
  149. * Use it only to mark current USB state.
  150. *
  151. * Targets: Device
  152. * Expected SETUP frame format:
  153. * - wValue: New device address
  154. * - wIndex: 0
  155. * - wLength: 0
  156. * - Data: None
  157. */
  158. APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR = 0x06, /**<
  159. * Targets: Device
  160. * - wValue: (hb): Descriptor Type and (lb): Descriptor Index
  161. * - wIndex: Zero of Language ID
  162. * - wLength: Descriptor Length
  163. * - Data: Descriptor
  164. */
  165. APP_USBD_SETUP_STDREQ_SET_DESCRIPTOR = 0x07, /**<
  166. * Not supported - Stall when called.
  167. */
  168. APP_USBD_SETUP_STDREQ_GET_CONFIGURATION = 0x08, /**<
  169. * Target: Device
  170. * Expected SETUP frame format:
  171. * - wValue: 0
  172. * - wIndex: 0
  173. * - wLength: 1
  174. * - Data: Configuration value
  175. */
  176. APP_USBD_SETUP_STDREQ_SET_CONFIGURATION = 0x09, /**<
  177. * Target: Device
  178. * Expected SETUP frame format:
  179. * - wValue: (lb): Configuration value
  180. * - wIndex: 0
  181. * - wLength: 0
  182. * - Data: None
  183. */
  184. APP_USBD_SETUP_STDREQ_GET_INTERFACE = 0x0A, /**<
  185. * Target: Interface
  186. * Expected SETUP frame format:
  187. * - wValue: 0
  188. * - wIndex: Interface
  189. * - wLength: 1
  190. * - Data: Alternate setting
  191. */
  192. APP_USBD_SETUP_STDREQ_SET_INTERFACE = 0x0B, /**<
  193. * Target: Interface
  194. * Expected SETUP frame format:
  195. * - wValue: Alternate setting
  196. * - wIndex: Interface
  197. * - wLength: 0
  198. * - Data: None
  199. */
  200. APP_USBD_SETUP_STDREQ_SYNCH_FRAME = 0x0C /**<
  201. * Target: Endpoint
  202. * Expected SETUP frame format:
  203. * - wValue: 0
  204. * - wIndex: Endpoint
  205. * - wLength: 2
  206. * - Data: Frame Number
  207. *
  208. * @note
  209. * This request is used only in connection with isochronous endpoints.
  210. * This is rarely used and probably we would not need to support it.
  211. */
  212. } app_usbd_setup_stdrequest_t;
  213. /**
  214. * @brief Standard feature selectors.
  215. *
  216. * Standard features that may be disabled or enabled by
  217. * @ref APP_USBD_SETUP_STDREQ_CLEAR_FEATURE or @ref APP_USBD_SETUP_STDREQ_SET_FEATURE
  218. */
  219. typedef enum {
  220. APP_USBD_SETUP_STDFEATURE_DEVICE_REMOTE_WAKEUP = 1, /**<
  221. * Remote wakeup feature.
  222. * Target: Device only
  223. */
  224. APP_USBD_SETUP_STDFEATURE_ENDPOINT_HALT = 0, /**<
  225. * Stall or clear the endpoint.
  226. * Target: Endpoint different than default (0)
  227. */
  228. APP_USBD_SETUP_STDFEATURE_TEST_MODE = 2 /**<
  229. * Upstream port test mode.
  230. * Power has to be cycled to exit test mode.
  231. * This feature cannot be cleared.
  232. *
  233. * Target: Device only
  234. *
  235. * @note
  236. * It should only be supported by HighSpeed capable devices.
  237. * Not supported in this library.
  238. */
  239. } app_usbd_setup_stdfeature_t;
  240. /**
  241. * @brief Universal way to access 16 bit values and its parts.
  242. */
  243. typedef union {
  244. uint16_t w; //!< 16 bit access
  245. struct
  246. {
  247. uint8_t lb; //!< Low byte access
  248. uint8_t hb; //!< High byte access
  249. };
  250. } app_usbd_setup_w_t;
  251. /**
  252. * @brief Internal redefinition of setup structure.
  253. *
  254. * Redefinition of the structure to simplify changes in the future
  255. * if required - app_usbd API would present setup data using app_usbd_setup_t.
  256. *
  257. * The structure layout is always the same like @ref nrf_drv_usbd_setup_t
  258. */
  259. typedef struct {
  260. uint8_t bmRequestType; //!< Setup type bitfield
  261. uint8_t bRequest; //!< One of @ref app_usbd_setup_stdrequest_t values or class dependent one.
  262. app_usbd_setup_w_t wValue; //!< byte 2, 3
  263. app_usbd_setup_w_t wIndex; //!< byte 4, 5
  264. app_usbd_setup_w_t wLength; //!< byte 6, 7
  265. } app_usbd_setup_t;
  266. #pragma pack(pop)
  267. /**
  268. * @brief Extract recipient from request type.
  269. *
  270. * @param[in] bmRequestType
  271. *
  272. * @return Extracted recipient field from request type value.
  273. */
  274. static inline app_usbd_setup_reqrec_t app_usbd_setup_req_rec(uint8_t bmRequestType)
  275. {
  276. return (app_usbd_setup_reqrec_t)BF_CX_GET(bmRequestType, APP_USBD_SETUP_REQ_BF_REC);
  277. }
  278. /**
  279. * @brief Extract type from request type.
  280. *
  281. * @param[in] bmRequestType
  282. *
  283. * @return Extracted type field from request type value.
  284. */
  285. static inline app_usbd_setup_reqtype_t app_usbd_setup_req_typ(uint8_t bmRequestType)
  286. {
  287. return (app_usbd_setup_reqtype_t)BF_CX_GET(bmRequestType, APP_USBD_SETUP_REQ_BF_TYP);
  288. }
  289. /**
  290. * @brief Extract direction from request type.
  291. *
  292. * @param[in] bmRequestType
  293. *
  294. * @return Extracted direction field from request type value.
  295. */
  296. static inline app_usbd_setup_reqdir_t app_usbd_setup_req_dir(uint8_t bmRequestType)
  297. {
  298. return (app_usbd_setup_reqdir_t)BF_CX_GET(bmRequestType, APP_USBD_SETUP_REQ_BF_DIR);
  299. }
  300. /**
  301. * @brief Make request type value.
  302. *
  303. * @param[in] rec Recipient.
  304. * @param[in] typ Request type.
  305. * @param[in] dir Direction.
  306. *
  307. * @return Assembled request type value.
  308. */
  309. static inline uint8_t app_usbd_setup_req_val(app_usbd_setup_reqrec_t rec,
  310. app_usbd_setup_reqtype_t typ,
  311. app_usbd_setup_reqdir_t dir)
  312. {
  313. uint32_t bmRequestType = (
  314. BF_CX_VAL(rec, APP_USBD_SETUP_REQ_BF_REC) |
  315. BF_CX_VAL(typ, APP_USBD_SETUP_REQ_BF_TYP) |
  316. BF_CX_VAL(dir, APP_USBD_SETUP_REQ_BF_DIR)
  317. );
  318. ASSERT(bmRequestType < 256U);
  319. return (uint8_t)bmRequestType;
  320. }
  321. ANON_UNIONS_DISABLE;
  322. /** @} */
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326. #endif /* APP_USBD_REQUEST_H__ */