usb_hid.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* This file is the part of the Lightweight USB device Stack for STM32 microcontrollers
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _USB_HID_H_
  16. #define _USB_HID_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**\addtogroup USB_HID USB HID class
  21. * \brief This module contains USB Human Interface Devices class definitions
  22. * \details This module based on
  23. * + [Device Class Definition for Human Interface Devices (HID) Version 1.11](http://www.usb.org/developers/hidpage/HID1_11.pdf)
  24. * + [LUFA - the Lightweight USB Framework for AVRs.](https://github.com/abcminiuser/lufa)
  25. * @{ */
  26. /**\name USB HID class code */
  27. #define USB_CLASS_HID 0x03 /**<\brief Interface belongs to the Human Interface device class.*/
  28. /**\name USB HID subclass codes
  29. * @{ */
  30. #define USB_HID_SUBCLASS_NONBOOT 0x00 /**<\brief Nonboot subclass.*/
  31. #define USB_HID_SUBCLASS_BOOT 0x01 /**<\brief Boot subclass.*/
  32. /** @} */
  33. /**\name USB HID protocol codes
  34. * @{ */
  35. #define USB_HID_PROTO_NONBOOT 0x00 /**<\brief Nonboot protocol.*/
  36. #define USB_HID_PROTO_KEYBOARD 0x01 /**<\brief Keyboard boot protocol.*/
  37. #define USB_HID_PROTO_MOUSE 0x02 /**<\brief Mouse boot protocol.*/
  38. /** @} */
  39. #define USB_HID_REPORT_IN 0x00 /**<\brief Indicates that the item is an IN report type.*/
  40. #define USB_HID_REPORT_OUT 0x01 /**<\brief Indicates that the item is an OUT report type.*/
  41. #define USB_HID_REPORT_FEATURE 0x02 /**<\brief Indicates that the item is a FEATURE report type.*/
  42. /**\name USB HID class-specified requests
  43. * @{ */
  44. #define USB_HID_GETREPORT 0x01 /**<\brief Request to get the current HID report from the device.*/
  45. #define USB_HID_GETIDLE 0x02 /**<\brief Request to get the current device idle count.*/
  46. #define USB_HID_GETPROTOCOL 0x03 /**<\brief Request to get the current HID report protocol mode.*/
  47. #define USB_HID_SETREPORT 0x09 /**< Request to set the current HID report to the device.*/
  48. #define USB_HID_SETIDLE 0x0A /**<\brief Request to set the device's idle count.*/
  49. #define USB_HID_SETPROTOCOL 0x0B /**< Request to set the current HID report protocol mode.*/
  50. /** @} */
  51. /**\name USB HID class-specified descriptor types
  52. * @{ */
  53. #define USB_DTYPE_HID 0x21 /**<\brief HID class HID descriptor type.*/
  54. #define USB_DTYPE_HID_REPORT 0x22 /**<\brief HID class HID report descriptor type.*/
  55. /** @} */
  56. /**\name USB HID country codes
  57. * @{ */
  58. #define USB_HID_COUNTRY_NONE 0 /**<\brief Not supported */
  59. #define USB_HID_COUNTRY_AR 1 /**<\brief Arabic */
  60. #define USB_HID_COUNTRY_BE 2 /**<\brief Belgian */
  61. #define USB_HID_COUNTRY_CA_BI 3 /**<\brief Canadian-Bilingual */
  62. #define USB_HID_COUNTRY_CA_FR 4 /**<\brief Canadian-French */
  63. #define USB_HID_COUNTRY_CZ 5 /**<\brief Czech Republic */
  64. #define USB_HID_COUNTRY_DK 6 /**<\brief Danish */
  65. #define USB_HID_COUNTRY_FI 7 /**<\brief Finnish */
  66. #define USB_HID_COUNTRY_FR 8 /**<\brief French */
  67. #define USB_HID_COUNTRY_DE 9 /**<\brief German */
  68. #define USB_HID_COUNTRY_GR 10 /**<\brief Greek */
  69. #define USB_HID_COUNTRY_HEB 11 /**<\brief Hebrew */
  70. #define USB_HID_COUNTRY_HU 12 /**<\brief Hungary */
  71. #define USB_HID_COUNTRY_ISO 13 /**<\brief International (ISO) */
  72. #define USB_HID_COUNTRY_IT 14 /**<\brief Italian */
  73. #define USB_HID_COUNTRY_JP 15 /**<\brief Japan (Katakana) */
  74. #define USB_HID_COUNTRY_KR 16 /**<\brief Korean */
  75. #define USB_HID_COUNTRY_LAT 17 /**<\brief Latin American */
  76. #define USB_HID_COUNTRY_NL 18 /**<\brief Netherlands/Dutch */
  77. #define USB_HID_COUNTRY_NO 19 /**<\brief Norwegian */
  78. #define USB_HID_COUNTRY_PER 20 /**<\brief Persian (Farsi) */
  79. #define USB_HID_COUNTRY_PL 21 /**<\brief Poland */
  80. #define USB_HID_COUNTRY_PO 22 /**<\brief Portuguese */
  81. #define USB_HID_COUNTRY_RU 23 /**<\brief Russia */
  82. #define USB_HID_COUNTRY_SK 24 /**<\brief Slovakia */
  83. #define USB_HID_COUNTRY_ES 25 /**<\brief Spanish */
  84. #define USB_HID_COUNTRY_SE 26 /**<\brief Swedish */
  85. #define USB_HID_COUNTRY_CH_FR 26 /**<\brief Swiss-French */
  86. #define USB_HID_COUNTRY_CH_DE 27 /**<\brief Swiss-German */
  87. #define USB_HID_COUNTRY_CH 29 /**<\brief Switzerland */
  88. #define USB_HID_COUNTRY_TW 30 /**<\brief Taiwan */
  89. #define USB_HID_COUNTRY_TR_Q 31 /**<\brief Turkish-Q */
  90. #define USB_HID_COUNTRY_UK 32 /**<\brief United Kingdom */
  91. #define USB_HID_COUNTRY_US 33 /**<\brief United States */
  92. #define USB_HID_COUNTRY_YU 34 /**<\brief Yugoslavia */
  93. #define USB_HID_COUNTRY_TR_F 35 /**<\brief Turkish-F */
  94. /** @} */
  95. /**\brief USB HID functional descriptor */
  96. struct usb_hid_descriptor {
  97. uint8_t bLength; /**<\brief Size of the descriptor, in bytes. */
  98. uint8_t bDescriptorType; /**<\brief Type of the descriptor, set to \ref USB_DTYPE_HID */
  99. uint16_t bcdHID; /**<\brief BCD encoded version that the HID descriptor and device complies to. */
  100. uint8_t bCountryCode; /**<\brief Country code of the localized device, or zero if universal. */
  101. uint8_t bNumDescriptors; /**<\brief Total number of HID report descriptors for the interface. */
  102. uint8_t bDescriptorType0; /**<\brief 1'st HID report descriptor type, set to \ref USB_DTYPE_HID_REPORT */
  103. uint16_t wDescriptorLength0; /**<\brief 1'sr HID report descriptor length in bytes. */
  104. } __attribute__((packed));
  105. /**\brief USB HID report descriptor */
  106. struct usb_hid_report_descriptor {
  107. uint8_t bDescriptorType; /**<\brief Type of HID report, set to \ref USB_DTYPE_HID_REPORT */
  108. uint16_t wDescriptorLength; /**<\brief Length of the associated HID report descriptor, in bytes. */
  109. } __attribute__((packed));
  110. /* Private Interface - For use in library only: */
  111. #if !defined(__DOXYGEN__)
  112. /* Macros: */
  113. #define CONCAT(x, y) x ## y
  114. #define CONCAT_EXPANDED(x, y) CONCAT(x, y)
  115. #define HID_RI_DATA_SIZE_MASK 0x03
  116. #define HID_RI_TYPE_MASK 0x0C
  117. #define HID_RI_TAG_MASK 0xF0
  118. #define HID_RI_TYPE_MAIN 0x00
  119. #define HID_RI_TYPE_GLOBAL 0x04
  120. #define HID_RI_TYPE_LOCAL 0x08
  121. #define HID_RI_DATA_BITS_0 0x00
  122. #define HID_RI_DATA_BITS_8 0x01
  123. #define HID_RI_DATA_BITS_16 0x02
  124. #define HID_RI_DATA_BITS_32 0x03
  125. #define HID_RI_DATA_BITS(DataBits) CONCAT_EXPANDED(HID_RI_DATA_BITS_, DataBits)
  126. #define _HID_RI_ENCODE_0(Data)
  127. #define _HID_RI_ENCODE_8(Data) , (Data & 0xFF)
  128. #define _HID_RI_ENCODE_16(Data) _HID_RI_ENCODE_8(Data) _HID_RI_ENCODE_8(Data >> 8)
  129. #define _HID_RI_ENCODE_32(Data) _HID_RI_ENCODE_16(Data) _HID_RI_ENCODE_16(Data >> 16)
  130. #define _HID_RI_ENCODE(DataBits, ...) CONCAT_EXPANDED(_HID_RI_ENCODE_, DataBits(__VA_ARGS__))
  131. #define _HID_RI_ENTRY(Type, Tag, DataBits, ...) (Type | Tag | HID_RI_DATA_BITS(DataBits)) _HID_RI_ENCODE(DataBits, (__VA_ARGS__))
  132. #endif
  133. /* Public Interface - May be used in end-application: */
  134. /* Macros: */
  135. /** \name HID Input, Output and Feature Report Descriptor Item Flags */
  136. //@{
  137. #define HID_IOF_CONSTANT (1 << 0)
  138. #define HID_IOF_DATA (0 << 0)
  139. #define HID_IOF_VARIABLE (1 << 1)
  140. #define HID_IOF_ARRAY (0 << 1)
  141. #define HID_IOF_RELATIVE (1 << 2)
  142. #define HID_IOF_ABSOLUTE (0 << 2)
  143. #define HID_IOF_WRAP (1 << 3)
  144. #define HID_IOF_NO_WRAP (0 << 3)
  145. #define HID_IOF_NON_LINEAR (1 << 4)
  146. #define HID_IOF_LINEAR (0 << 4)
  147. #define HID_IOF_NO_PREFERRED_STATE (1 << 5)
  148. #define HID_IOF_PREFERRED_STATE (0 << 5)
  149. #define HID_IOF_NULLSTATE (1 << 6)
  150. #define HID_IOF_NO_NULL_POSITION (0 << 6)
  151. #define HID_IOF_VOLATILE (1 << 7)
  152. #define HID_IOF_NON_VOLATILE (0 << 7)
  153. #define HID_IOF_BUFFERED_BYTES (1 << 8)
  154. #define HID_IOF_BITFIELD (0 << 8)
  155. //@}
  156. /** \name HID Report Descriptor Item Macros */
  157. //@{
  158. #define HID_RI_INPUT(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_MAIN , 0x80, DataBits, __VA_ARGS__)
  159. #define HID_RI_OUTPUT(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_MAIN , 0x90, DataBits, __VA_ARGS__)
  160. #define HID_RI_COLLECTION(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_MAIN , 0xA0, DataBits, __VA_ARGS__)
  161. #define HID_RI_FEATURE(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_MAIN , 0xB0, DataBits, __VA_ARGS__)
  162. #define HID_RI_END_COLLECTION(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_MAIN , 0xC0, DataBits, __VA_ARGS__)
  163. #define HID_RI_USAGE_PAGE(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x00, DataBits, __VA_ARGS__)
  164. #define HID_RI_LOGICAL_MINIMUM(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x10, DataBits, __VA_ARGS__)
  165. #define HID_RI_LOGICAL_MAXIMUM(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x20, DataBits, __VA_ARGS__)
  166. #define HID_RI_PHYSICAL_MINIMUM(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x30, DataBits, __VA_ARGS__)
  167. #define HID_RI_PHYSICAL_MAXIMUM(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x40, DataBits, __VA_ARGS__)
  168. #define HID_RI_UNIT_EXPONENT(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x50, DataBits, __VA_ARGS__)
  169. #define HID_RI_UNIT(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x60, DataBits, __VA_ARGS__)
  170. #define HID_RI_REPORT_SIZE(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x70, DataBits, __VA_ARGS__)
  171. #define HID_RI_REPORT_ID(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x80, DataBits, __VA_ARGS__)
  172. #define HID_RI_REPORT_COUNT(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0x90, DataBits, __VA_ARGS__)
  173. #define HID_RI_PUSH(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0xA0, DataBits, __VA_ARGS__)
  174. #define HID_RI_POP(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_GLOBAL, 0xB0, DataBits, __VA_ARGS__)
  175. #define HID_RI_USAGE(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_LOCAL , 0x00, DataBits, __VA_ARGS__)
  176. #define HID_RI_USAGE_MINIMUM(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_LOCAL , 0x10, DataBits, __VA_ARGS__)
  177. #define HID_RI_USAGE_MAXIMUM(DataBits, ...) _HID_RI_ENTRY(HID_RI_TYPE_LOCAL , 0x20, DataBits, __VA_ARGS__)
  178. //@}
  179. /** @} */
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif