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