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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

hid.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Circuits At Home, LTD
  11. Web : http://www.circuitsathome.com
  12. e-mail : [email protected]
  13. */
  14. #if !defined(__HID_H__)
  15. #define __HID_H__
  16. #include "Usb.h"
  17. #include "hidusagestr.h"
  18. #define MAX_REPORT_PARSERS 2
  19. #define HID_MAX_HID_CLASS_DESCRIPTORS 5
  20. #define DATA_SIZE_MASK 0x03
  21. #define TYPE_MASK 0x0C
  22. #define TAG_MASK 0xF0
  23. #define DATA_SIZE_0 0x00
  24. #define DATA_SIZE_1 0x01
  25. #define DATA_SIZE_2 0x02
  26. #define DATA_SIZE_4 0x03
  27. #define TYPE_MAIN 0x00
  28. #define TYPE_GLOBAL 0x04
  29. #define TYPE_LOCAL 0x08
  30. #define TAG_MAIN_INPUT 0x80
  31. #define TAG_MAIN_OUTPUT 0x90
  32. #define TAG_MAIN_COLLECTION 0xA0
  33. #define TAG_MAIN_FEATURE 0xB0
  34. #define TAG_MAIN_ENDCOLLECTION 0xC0
  35. #define TAG_GLOBAL_USAGEPAGE 0x00
  36. #define TAG_GLOBAL_LOGICALMIN 0x10
  37. #define TAG_GLOBAL_LOGICALMAX 0x20
  38. #define TAG_GLOBAL_PHYSMIN 0x30
  39. #define TAG_GLOBAL_PHYSMAX 0x40
  40. #define TAG_GLOBAL_UNITEXP 0x50
  41. #define TAG_GLOBAL_UNIT 0x60
  42. #define TAG_GLOBAL_REPORTSIZE 0x70
  43. #define TAG_GLOBAL_REPORTID 0x80
  44. #define TAG_GLOBAL_REPORTCOUNT 0x90
  45. #define TAG_GLOBAL_PUSH 0xA0
  46. #define TAG_GLOBAL_POP 0xB0
  47. #define TAG_LOCAL_USAGE 0x00
  48. #define TAG_LOCAL_USAGEMIN 0x10
  49. #define TAG_LOCAL_USAGEMAX 0x20
  50. /* HID requests */
  51. #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
  52. #define bmREQ_HID_IN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
  53. #define bmREQ_HID_REPORT USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_INTERFACE
  54. /* HID constants. Not part of chapter 9 */
  55. /* Class-Specific Requests */
  56. #define HID_REQUEST_GET_REPORT 0x01
  57. #define HID_REQUEST_GET_IDLE 0x02
  58. #define HID_REQUEST_GET_PROTOCOL 0x03
  59. #define HID_REQUEST_SET_REPORT 0x09
  60. #define HID_REQUEST_SET_IDLE 0x0A
  61. #define HID_REQUEST_SET_PROTOCOL 0x0B
  62. /* Class Descriptor Types */
  63. #define HID_DESCRIPTOR_HID 0x21
  64. #define HID_DESCRIPTOR_REPORT 0x22
  65. #define HID_DESRIPTOR_PHY 0x23
  66. /* Protocol Selection */
  67. #define HID_BOOT_PROTOCOL 0x00
  68. #define HID_RPT_PROTOCOL 0x01
  69. /* HID Interface Class Code */
  70. #define HID_INTF 0x03
  71. /* HID Interface Class SubClass Codes */
  72. #define HID_BOOT_INTF_SUBCLASS 0x01
  73. /* HID Interface Class Protocol Codes */
  74. #define HID_PROTOCOL_NONE 0x00
  75. #define HID_PROTOCOL_KEYBOARD 0x01
  76. #define HID_PROTOCOL_MOUSE 0x02
  77. #define HID_ITEM_TYPE_MAIN 0
  78. #define HID_ITEM_TYPE_GLOBAL 1
  79. #define HID_ITEM_TYPE_LOCAL 2
  80. #define HID_ITEM_TYPE_RESERVED 3
  81. #define HID_LONG_ITEM_PREFIX 0xfe // Long item prefix value
  82. #define bmHID_MAIN_ITEM_TAG 0xfc // Main item tag mask
  83. #define bmHID_MAIN_ITEM_INPUT 0x80 // Main item Input tag value
  84. #define bmHID_MAIN_ITEM_OUTPUT 0x90 // Main item Output tag value
  85. #define bmHID_MAIN_ITEM_FEATURE 0xb0 // Main item Feature tag value
  86. #define bmHID_MAIN_ITEM_COLLECTION 0xa0 // Main item Collection tag value
  87. #define bmHID_MAIN_ITEM_END_COLLECTION 0xce // Main item End Collection tag value
  88. #define HID_MAIN_ITEM_COLLECTION_PHYSICAL 0
  89. #define HID_MAIN_ITEM_COLLECTION_APPLICATION 1
  90. #define HID_MAIN_ITEM_COLLECTION_LOGICAL 2
  91. #define HID_MAIN_ITEM_COLLECTION_REPORT 3
  92. #define HID_MAIN_ITEM_COLLECTION_NAMED_ARRAY 4
  93. #define HID_MAIN_ITEM_COLLECTION_USAGE_SWITCH 5
  94. #define HID_MAIN_ITEM_COLLECTION_USAGE_MODIFIER 6
  95. struct HidItemPrefix {
  96. uint8_t bSize : 2;
  97. uint8_t bType : 2;
  98. uint8_t bTag : 4;
  99. };
  100. struct MainItemIOFeature {
  101. uint8_t bmIsConstantOrData : 1;
  102. uint8_t bmIsArrayOrVariable : 1;
  103. uint8_t bmIsRelativeOrAbsolute : 1;
  104. uint8_t bmIsWrapOrNoWrap : 1;
  105. uint8_t bmIsNonLonearOrLinear : 1;
  106. uint8_t bmIsNoPreferedOrPrefered : 1;
  107. uint8_t bmIsNullOrNoNull : 1;
  108. uint8_t bmIsVolatileOrNonVolatile : 1;
  109. };
  110. class HID;
  111. class HIDReportParser {
  112. public:
  113. virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) = 0;
  114. };
  115. class HID : public USBDeviceConfig, public UsbConfigXtracter {
  116. protected:
  117. USB *pUsb; // USB class instance pointer
  118. uint8_t bAddress; // address
  119. protected:
  120. static const uint8_t epInterruptInIndex = 1; // InterruptIN endpoint index
  121. static const uint8_t epInterruptOutIndex = 2; // InterruptOUT endpoint index
  122. static const uint8_t maxHidInterfaces = 3;
  123. static const uint8_t maxEpPerInterface = 2;
  124. static const uint8_t totalEndpoints = (maxHidInterfaces * maxEpPerInterface + 1);
  125. void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
  126. void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
  127. virtual HIDReportParser* GetReportParser(uint8_t id) {
  128. return NULL;
  129. };
  130. public:
  131. HID(USB *pusb) : pUsb(pusb) {
  132. };
  133. const USB* GetUsb() {
  134. return pUsb;
  135. };
  136. virtual bool SetReportParser(uint8_t id, HIDReportParser *prs) {
  137. return false;
  138. };
  139. uint8_t SetProtocol(uint8_t iface, uint8_t protocol);
  140. uint8_t GetProtocol(uint8_t iface, uint8_t* dataptr);
  141. uint8_t GetIdle(uint8_t iface, uint8_t reportID, uint8_t* dataptr);
  142. uint8_t SetIdle(uint8_t iface, uint8_t reportID, uint8_t duration);
  143. uint8_t GetReportDescr(uint16_t wIndex, USBReadParser *parser = NULL);
  144. uint8_t GetHidDescr(uint8_t ep, uint16_t nbytes, uint8_t* dataptr);
  145. uint8_t GetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
  146. uint8_t SetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);
  147. };
  148. #endif // __HID_H__