app_usbd_descriptor.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright (c) 2017 - 2018, 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_DESCRIPTOR_H__
  41. #define APP_USBD_DESCRIPTOR_H__
  42. #include "nrf.h"
  43. #include "nrf_drv_usbd.h"
  44. #include "app_usbd_langid.h"
  45. #include "app_util_platform.h"
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /* Compiler support for anonymous unions */
  50. ANON_UNIONS_ENABLE;
  51. /**
  52. * @defgroup app_usbd_descriptor USB standard descriptors
  53. * @ingroup app_usbd
  54. *
  55. * @brief @tagAPI52840 Module with types definitions used for standard descriptors.
  56. * @{
  57. */
  58. /**
  59. * @brief Helper macro for translating unsigned 24 bit value to 2 byte raw descriptor.
  60. * */
  61. #define APP_USBD_U16_TO_RAW_DSC(val) (uint8_t)(val), \
  62. (uint8_t)(((val) / (256)))
  63. /**
  64. * @brief Helper macro for translating unsigned 24 bit value to 3 byte raw descriptor.
  65. * */
  66. #define APP_USBD_U24_TO_RAW_DSC(val) (uint8_t)(val), \
  67. (uint8_t)(((val) / (256))), \
  68. (uint8_t)(((val) / (256 * 256)))
  69. /**
  70. * @brief Helper macro for translating unsigned 32 bit value to 4 byte raw descriptor.
  71. * */
  72. #define APP_USBD_U32_TO_RAW_DSC(val) (uint8_t)(val), \
  73. (uint8_t)(((val) / (256))), \
  74. (uint8_t)(((val) / (256 * 256))) \
  75. (uint8_t)(((val) / (256 * 256 * 256)))
  76. /**
  77. * @brief Descriptor types.
  78. *
  79. * Descriptor types used in two situations:
  80. * - when processing @ref APP_USBD_SETUP_STDREQ_GET_DESCRIPTOR SETUP request,
  81. * the required descriptor type may be placed in wValue in HighByte.
  82. * - As a descriptor identifier itself inside descriptor stream.
  83. *
  84. * According to chapter 9.6 of USB 2.0 specification, following descriptors may
  85. * be requested directly by GetDescriptor method:
  86. * - @ref APP_USBD_DESCRIPTOR_DEVICE
  87. * - @ref APP_USBD_DESCRIPTOR_DEVICE_QUALIFIER (not used for FullSpeed only device)
  88. * - @ref APP_USBD_DESCRIPTOR_CONFIGURATION
  89. * - @ref APP_USBD_DESCRIPTOR_STRING
  90. */
  91. typedef enum
  92. {
  93. APP_USBD_DESCRIPTOR_DEVICE = 1, /**< Device descriptor. */
  94. APP_USBD_DESCRIPTOR_CONFIGURATION = 2, /**<
  95. * Specific configuration descriptor.
  96. * Configuration descriptor is always followed by all the related interface
  97. * and endpoints descriptors.
  98. */
  99. APP_USBD_DESCRIPTOR_STRING = 3, /**< String descriptor. */
  100. APP_USBD_DESCRIPTOR_INTERFACE = 4, /**<
  101. * Interface descriptor followed by all the related endpoints descriptors.
  102. *
  103. * @note It is returned together with @ref APP_USBD_DESCRIPTOR_CONFIGURATION.
  104. * Cannot be accessed by GetDescriptor or SetDescriptor
  105. */
  106. APP_USBD_DESCRIPTOR_ENDPOINT = 5, /**<
  107. * Endpoint descriptor.
  108. *
  109. * @note It is returned together with @ref APP_USBD_DESCRIPTOR_CONFIGURATION.
  110. * Cannot be accessed by GetDescriptor or SetDescriptor
  111. */
  112. APP_USBD_DESCRIPTOR_DEVICE_QUALIFIER = 6, /**< @note Not supported - used only in HighSpeed capable devices. */
  113. APP_USBD_DESCRIPTOR_OTHER_SPEED_CONFIGURATION = 7, /**< @note Not supported - our USB implementation supports only one speed. */
  114. APP_USBD_DESCRIPTOR_INTERFACE_POWER = 8, /**< @note Not supported */
  115. APP_USBD_DESCRIPTOR_OTG = 9, /**< @note Not supported - Our USB have not OTG functionality */
  116. APP_USBD_DESCRIPTOR_DEBUG = 10, /**< Debug channel descriptor if available, can be only reached by GetDescriptor */
  117. APP_USBD_DESCRIPTOR_INTERFACE_ASSOCIATION = 11, /**<
  118. * Descriptor used to describe that two or more interfaces are associated to the same function.
  119. *
  120. * @note It is returned together with @ref APP_USBD_DESCRIPTOR_CONFIGURATION.
  121. * Cannot be accessed by GetDescriptor or SetDescriptor
  122. */
  123. APP_USBD_DESCRIPTOR_REPORT = 34, /**< HID Report descriptor. */
  124. APP_USBD_DESCRIPTOR_PHYSICAL = 35 /**< HID Physical descriptor. */
  125. } app_usbd_descriptor_t;
  126. /* Make all descriptors packed */
  127. #pragma pack(push, 1)
  128. /**
  129. * @brief Common descriptor header.
  130. *
  131. * The header that we can find on the beginning of all descriptors that contains
  132. * the descriptor length and type.
  133. */
  134. typedef struct
  135. {
  136. uint8_t bLength; //!< Size of the descriptor in bytes.
  137. uint8_t bDescriptorType; //!< Should equal one of @ref app_usbd_descriptor_t.
  138. /** Class specific descriptors values are defined inside classes. */
  139. } app_usbd_descriptor_header_t;
  140. /**
  141. * @brief Device descriptor.
  142. *
  143. * Descriptor used for the whole device.
  144. */
  145. typedef struct
  146. {
  147. uint8_t bLength; //!< Size of the descriptor in bytes.
  148. uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_DEVICE.
  149. uint16_t bcdUSB; //!< USB Specification Release Number in Binary-Coded Decimal
  150. uint8_t bDeviceClass; //!< Device class code.
  151. /**< If 0, each interface specifies its own class information.
  152. * 0xFF for vendor-specific.
  153. */
  154. uint8_t bDeviceSubClass; //!< Subclass code.
  155. /**< If bDevice Class is set to value other than 0xFF,
  156. * all values here are reserved for assignment by USB-IF.
  157. */
  158. uint8_t bDeviceProtocol; //!< Subclass code.
  159. /**< If 0, no specific protocol is defined on device basis.
  160. * Each interface may define its own protocol then.
  161. * If set to 0xFF, vendor-specific protocol is used.
  162. */
  163. uint8_t bMaxPacketSize0; //!< Maximum packet size for endpoint zero.
  164. uint16_t idVendor; //!< Vendor ID (Assigned by the USB-IF).
  165. uint16_t idProduct; //!< Product ID (assigned by manufacturer).
  166. uint16_t bcdDevice; //!< Device release number in binary-coded decimal.
  167. uint8_t iManufacturer; //!< Index of string descriptor in describing manufacturer.
  168. uint8_t iProduct; //!< Index of string descriptor in describing product.
  169. uint8_t iSerialNumber; //!< Index of string descriptor in describing the device's serial number.
  170. uint8_t bNumConfigurations; //!< Number of possible configurations.
  171. } app_usbd_descriptor_device_t;
  172. /**
  173. * @brief Attributes masks.
  174. *
  175. * Masks used for attributes in configuration.
  176. */
  177. typedef enum
  178. {
  179. /** This is reserved descriptor that has always to be set */
  180. APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_ALWAYS_SET_MASK = 1U << 7,
  181. /** Attribute that informs that device is self powered */
  182. APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_SELF_POWERED_MASK = 1U << 6,
  183. /** Attribute that informs that device has Remove Wakeup functionality */
  184. APP_USBD_DESCRIPTOR_CONFIGURATION_ATTRIBUTE_REMOTE_WAKEUP_MASK = 1U << 5
  185. } app_usbd_descriptor_configuration_attributes_t;
  186. /**
  187. * @brief Configuration descriptor.
  188. *
  189. * Descriptor used at the beginning of configuration response.
  190. */
  191. typedef struct
  192. {
  193. uint8_t bLength; //!< Size of the descriptor in bytes.
  194. uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_DEVICE.
  195. uint16_t wTotalLength; //!< Total length of configuration data, including all descriptors returned after configuration itself.
  196. uint8_t bNumInterfaces; //!< Number of interfaces supportedf by this configuration
  197. uint8_t bConfigurationValue; //!< Value to use as an argument to the SetConfiguration request.
  198. uint8_t iConfiguration; //!< Index of string descriptor describing this configuration.
  199. uint8_t bmAttributes; //!< Configuration characteristics.
  200. uint8_t bMaxPower; //!< Maximum power consumption. Expressed in 2&nbsp;mA units.
  201. } app_usbd_descriptor_configuration_t;
  202. /**
  203. * @brief Raw descriptor - String descriptor zero.
  204. *
  205. * String descriptor sent only as a response for GetDescriptor.
  206. */
  207. typedef struct
  208. {
  209. uint8_t bLength; //!< Size of the descriptor in bytes.
  210. uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_STRING.
  211. uint16_t wLANGID[]; //!< The array of LANGID codes supported by the device.
  212. } app_usbd_descriptor_string0_t;
  213. /**
  214. * @brief Raw descriptor - Any normal string.
  215. *
  216. * String descriptor sent only as a response for GetDescriptor.
  217. */
  218. typedef struct
  219. {
  220. uint8_t bLength; //!< Size of the descriptor in bytes.
  221. uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_STRING.
  222. uint16_t bString[]; //!< UNICODE encoded string.
  223. } app_usbd_descriptor_string_t;
  224. /**
  225. * @brief Interface descriptor.
  226. *
  227. * Interface descriptor, returned as a part of configuration descriptor.
  228. */
  229. typedef struct
  230. {
  231. uint8_t bLength; //!< Size of the descriptor in bytes.
  232. uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_INTERFACE.
  233. uint8_t bInterfaceNumber; //!< Number of this interface.
  234. uint8_t bAlternateSetting; //!< Value used to select this alternate setting.
  235. uint8_t bNumEndpoints; //!< Number of endpoints used by this interface.
  236. uint8_t bInterfaceClass; //!< Class code (assigned by the USB-IF). 0xff for vendor specific.
  237. uint8_t bInterfaceSubClass; //!< Subclass code (assigned by the USB-IF).
  238. uint8_t bInterfaceProtocol; //!< Protocol code (assigned by the USB-IF). 0xff for vendor specific.
  239. uint8_t iInterface; //!< Index of string descriptor describing this interface.
  240. } app_usbd_descriptor_iface_t;
  241. /** Offset of endpoint type attribute bits */
  242. #define APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET 0
  243. /** Mask of endpoint type attribute bits */
  244. #define APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_MASK BF_MASK(2, APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET)
  245. /** Offset of endpoint synchronization type attribute bits */
  246. #define APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET 2
  247. /** Mask of endpoint synchronization type attribute bits */
  248. #define APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_MASK BF_MASK(2, APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET)
  249. /** Offset of endpoint usage type attribute bits */
  250. #define APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET 4
  251. /** Mask of endpoint usage type attribute bits */
  252. #define APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_MASK BF_MASK(2, APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET)
  253. /**
  254. * @brief Endpoint attributes mnemonics.
  255. *
  256. * @sa APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_MASK
  257. * @sa APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_MASK
  258. * @sa APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_MASK
  259. */
  260. typedef enum
  261. {
  262. APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_CONTROL = 0 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET,
  263. APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_ISOCHRONOUS = 1 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET,
  264. APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_BULK = 2 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET,
  265. APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_INTERRUPT = 3 << APP_USBD_DESCRIPTOR_EP_ATTR_TYPE_OFFSET,
  266. APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_NONE = 0 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET,
  267. APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_ASYNCHRONOUS = 1 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET,
  268. APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_ADAPTIVE = 2 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET,
  269. APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_SYNCHRONOUS = 3 << APP_USBD_DESCRIPTOR_EP_ATTR_SYNC_OFFSET,
  270. APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_DATA = 0 << APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET,
  271. APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_FEEDBACK = 1 << APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET,
  272. APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_IMPLICIT = 2 << APP_USBD_DESCRIPTOR_EP_ATTR_USAGE_OFFSET
  273. } app_usbd_descriptor_ep_attr_bitmap_t;
  274. /**
  275. * @brief Endpoint descriptor.
  276. *
  277. * Endpoint descriptor, returned as a part of configuration descriptor.
  278. */
  279. typedef struct
  280. {
  281. uint8_t bLength; //!< Size of the descriptor in bytes.
  282. uint8_t bDescriptorType; //!< Should equal to @ref APP_USBD_DESCRIPTOR_ENDPOINT.
  283. uint8_t bEndpointAddress; //!< Endpoint address
  284. uint8_t bmAttributes; //!< Endpoint attributes
  285. uint16_t wMaxPacketSize; //!< Maximum packet size this endpoint is capable of handling.
  286. uint8_t bInterval; //!< Interval for pooling endpoint for data transfers.
  287. } app_usbd_descriptor_ep_t;
  288. /**
  289. * @brief Interface association descriptor.
  290. */
  291. typedef struct
  292. {
  293. uint8_t bLength; //!< size of this descriptor in bytes
  294. uint8_t bDescriptorType; //!< INTERFACE descriptor type
  295. uint8_t bFirstInterface; //!< Number of interface
  296. uint8_t bInterfaceCount; //!< value to select alternate setting
  297. uint8_t bFunctionClass; //!< Class code assigned by the USB
  298. uint8_t bFunctionSubClass;//!< Sub-class code assigned by the USB
  299. uint8_t bFunctionProtocol;//!< Protocol code assigned by the USB
  300. uint8_t iFunction; //!< Index of string descriptor
  301. } app_usbd_descriptor_iad_t;
  302. #pragma pack(pop)
  303. ANON_UNIONS_DISABLE;
  304. #ifdef __cplusplus
  305. }
  306. #endif
  307. /** @} */
  308. #endif /* APP_USBD_DESCRIPTOR_H__ */