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 |
|
---|
18 | #ifndef HID_H_INCLUDED
|
---|
19 | #define HID_H_INCLUDED
|
---|
20 |
|
---|
21 | #include <stdint.h>
|
---|
22 | #include "usb_ch9.h"
|
---|
23 | #include "Usb.h"
|
---|
24 | #include "Arduino.h"
|
---|
25 | #include "confdescparser.h"
|
---|
26 | #include "hidusagestr.h"
|
---|
27 |
|
---|
28 | #define DATA_SIZE_MASK 0x03
|
---|
29 | #define TYPE_MASK 0x0C
|
---|
30 | #define TAG_MASK 0xF0
|
---|
31 |
|
---|
32 | #define DATA_SIZE_0 0x00
|
---|
33 | #define DATA_SIZE_1 0x01
|
---|
34 | #define DATA_SIZE_2 0x02
|
---|
35 | #define DATA_SIZE_4 0x03
|
---|
36 |
|
---|
37 | #define TYPE_MAIN 0x00
|
---|
38 | #define TYPE_GLOBAL 0x04
|
---|
39 | #define TYPE_LOCAL 0x08
|
---|
40 |
|
---|
41 | #define TAG_MAIN_INPUT 0x80
|
---|
42 | #define TAG_MAIN_OUTPUT 0x90
|
---|
43 | #define TAG_MAIN_COLLECTION 0xA0
|
---|
44 | #define TAG_MAIN_FEATURE 0xB0
|
---|
45 | #define TAG_MAIN_ENDCOLLECTION 0xC0
|
---|
46 |
|
---|
47 | #define TAG_GLOBAL_USAGEPAGE 0x00
|
---|
48 | #define TAG_GLOBAL_LOGICALMIN 0x10
|
---|
49 | #define TAG_GLOBAL_LOGICALMAX 0x20
|
---|
50 | #define TAG_GLOBAL_PHYSMIN 0x30
|
---|
51 | #define TAG_GLOBAL_PHYSMAX 0x40
|
---|
52 | #define TAG_GLOBAL_UNITEXP 0x50
|
---|
53 | #define TAG_GLOBAL_UNIT 0x60
|
---|
54 | #define TAG_GLOBAL_REPORTSIZE 0x70
|
---|
55 | #define TAG_GLOBAL_REPORTID 0x80
|
---|
56 | #define TAG_GLOBAL_REPORTCOUNT 0x90
|
---|
57 | #define TAG_GLOBAL_PUSH 0xA0
|
---|
58 | #define TAG_GLOBAL_POP 0xB0
|
---|
59 |
|
---|
60 | #define TAG_LOCAL_USAGE 0x00
|
---|
61 | #define TAG_LOCAL_USAGEMIN 0x10
|
---|
62 | #define TAG_LOCAL_USAGEMAX 0x20
|
---|
63 |
|
---|
64 | /* HID requests */
|
---|
65 | #define bmREQ_HIDOUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
|
---|
66 | #define bmREQ_HIDIN USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
|
---|
67 | #define bmREQ_HIDREPORT USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_STANDARD|USB_SETUP_RECIPIENT_INTERFACE
|
---|
68 |
|
---|
69 | /* HID constants. Not part of chapter 9 */
|
---|
70 | /* Class-Specific Requests */
|
---|
71 | #define HID_REQUEST_GET_REPORT 0x01
|
---|
72 | #define HID_REQUEST_GET_IDLE 0x02
|
---|
73 | #define HID_REQUEST_GET_PROTOCOL 0x03
|
---|
74 | #define HID_REQUEST_SET_REPORT 0x09
|
---|
75 | #define HID_REQUEST_SET_IDLE 0x0A
|
---|
76 | #define HID_REQUEST_SET_PROTOCOL 0x0B
|
---|
77 |
|
---|
78 | /* Class Descriptor Types */
|
---|
79 | #define HID_DESCRIPTOR_HID 0x21
|
---|
80 | #define HID_DESCRIPTOR_REPORT 0x22
|
---|
81 | #define HID_DESRIPTOR_PHY 0x23
|
---|
82 |
|
---|
83 | /* Protocol Selection */
|
---|
84 | #define HID_BOOT_PROTOCOL 0x00
|
---|
85 | #define HID_RPT_PROTOCOL 0x01
|
---|
86 |
|
---|
87 | /* HID Interface Class Code */
|
---|
88 | #define HID_INTF 0x03
|
---|
89 |
|
---|
90 | /* HID Interface Class SubClass Codes */
|
---|
91 | #define HID_BOOT_INTF_SUBCLASS 0x01
|
---|
92 |
|
---|
93 | /* HID Interface Class Protocol Codes */
|
---|
94 | #define HID_PROTOCOL_NONE 0x00
|
---|
95 | #define HID_PROTOCOL_KEYBOARD 0x01
|
---|
96 | #define HID_PROTOCOL_MOUSE 0x02
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * \brief HidItemPrefix definition.
|
---|
100 | */
|
---|
101 | struct HidItemPrefix // Not used
|
---|
102 | {
|
---|
103 | uint8_t bSize : 2;
|
---|
104 | uint8_t bType : 2;
|
---|
105 | uint8_t bTag : 4;
|
---|
106 | };
|
---|
107 |
|
---|
108 | #define HID_ITEM_TYPE_MAIN 0
|
---|
109 | #define HID_ITEM_TYPE_GLOBAL 1
|
---|
110 | #define HID_ITEM_TYPE_LOCAL 2
|
---|
111 | #define HID_ITEM_TYPE_RESERVED 3
|
---|
112 |
|
---|
113 | #define HID_LONG_ITEM_PREFIX 0xfe // Long item prefix value
|
---|
114 |
|
---|
115 | #define bmHID_MAIN_ITEM_TAG 0xfc // Main item tag mask
|
---|
116 |
|
---|
117 | #define bmHID_MAIN_ITEM_INPUT 0x80 // Main item Input tag value
|
---|
118 | #define bmHID_MAIN_ITEM_OUTPUT 0x90 // Main item Output tag value
|
---|
119 | #define bmHID_MAIN_ITEM_FEATURE 0xb0 // Main item Feature tag value
|
---|
120 | #define bmHID_MAIN_ITEM_COLLECTION 0xa0 // Main item Collection tag value
|
---|
121 | #define bmHID_MAIN_ITEM_END_COLLECTION 0xce // Main item End Collection tag value
|
---|
122 |
|
---|
123 | #define HID_MAIN_ITEM_COLLECTION_PHYSICAL 0
|
---|
124 | #define HID_MAIN_ITEM_COLLECTION_APPLICATION 1
|
---|
125 | #define HID_MAIN_ITEM_COLLECTION_LOGICAL 2
|
---|
126 | #define HID_MAIN_ITEM_COLLECTION_REPORT 3
|
---|
127 | #define HID_MAIN_ITEM_COLLECTION_NAMED_ARRAY 4
|
---|
128 | #define HID_MAIN_ITEM_COLLECTION_USAGE_SWITCH 5
|
---|
129 | #define HID_MAIN_ITEM_COLLECTION_USAGE_MODIFIER 6
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * \brief MainItemIOFeature definition.
|
---|
133 | */
|
---|
134 | struct MainItemIOFeature // Not used
|
---|
135 | {
|
---|
136 | uint8_t bmIsConstantOrData : 1;
|
---|
137 | uint8_t bmIsArrayOrVariable : 1;
|
---|
138 | uint8_t bmIsRelativeOrAbsolute : 1;
|
---|
139 | uint8_t bmIsWrapOrNoWrap : 1;
|
---|
140 | uint8_t bmIsNonLonearOrLinear : 1;
|
---|
141 | uint8_t bmIsNoPreferedOrPrefered : 1;
|
---|
142 | uint8_t bmIsNullOrNoNull : 1;
|
---|
143 | uint8_t bmIsVolatileOrNonVolatile : 1;
|
---|
144 | };
|
---|
145 |
|
---|
146 | class HID;
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * \class Abstract HIDReportParser definition.
|
---|
150 | *
|
---|
151 | * \note This class is used to implement HID report parsing.
|
---|
152 | */
|
---|
153 | class HIDReportParser
|
---|
154 | {
|
---|
155 | public:
|
---|
156 | virtual void Parse(HID *hid, bool is_rpt_id, uint32_t len, uint8_t *buf) = 0;
|
---|
157 | };
|
---|
158 |
|
---|
159 | #define MAX_REPORT_PARSERS 2
|
---|
160 | #define HID_MAX_HID_CLASS_DESCRIPTORS 5
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * \class HID definition.
|
---|
164 | */
|
---|
165 | class HID : public USBDeviceConfig, public UsbConfigXtracter
|
---|
166 | {
|
---|
167 | protected:
|
---|
168 | USBHost *pUsb; // USB class instance pointer
|
---|
169 | uint32_t bAddress; // address
|
---|
170 |
|
---|
171 | protected:
|
---|
172 | static const uint32_t epInterruptInIndex = 1; // InterruptIN endpoint index
|
---|
173 | static const uint32_t epInterruptOutIndex = 2; // InterruptOUT endpoint index
|
---|
174 |
|
---|
175 | static const uint32_t maxHidInterfaces = 3;
|
---|
176 | static const uint32_t maxEpPerInterface = 2;
|
---|
177 | static const uint32_t totalEndpoints = (maxHidInterfaces * maxEpPerInterface + 1);
|
---|
178 |
|
---|
179 | void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
|
---|
180 | void PrintHidDescriptor(const USB_HID_DESCRIPTOR *pDesc);
|
---|
181 |
|
---|
182 | virtual HIDReportParser* GetReportParser(uint32_t id) { return 0; };
|
---|
183 |
|
---|
184 | public:
|
---|
185 | HID(USBHost *pusb) : pUsb(pusb) {};
|
---|
186 |
|
---|
187 | const USBHost* GetUsb() { return pUsb; };
|
---|
188 | virtual bool SetReportParser(uint32_t id, HIDReportParser *prs) { return false; };
|
---|
189 |
|
---|
190 | uint32_t SetProtocol(uint32_t iface, uint32_t protocol);
|
---|
191 | uint32_t GetProtocol(uint32_t iface, uint8_t* dataptr);
|
---|
192 | uint32_t GetIdle(uint32_t iface, uint32_t reportID, uint8_t* dataptr);
|
---|
193 | uint32_t SetIdle(uint32_t iface, uint32_t reportID, uint32_t duration);
|
---|
194 |
|
---|
195 | uint32_t GetReportDescr(uint32_t ep, USBReadParser *parser = NULL);
|
---|
196 |
|
---|
197 | uint32_t GetHidDescr(uint32_t ep, uint32_t nbytes, uint8_t* dataptr);
|
---|
198 | uint32_t GetReport(uint32_t ep, uint32_t iface, uint32_t report_type, uint32_t report_id, uint32_t nbytes, uint8_t* dataptr);
|
---|
199 | uint32_t SetReport(uint32_t ep, uint32_t iface, uint32_t report_type, uint32_t report_id, uint32_t nbytes, uint8_t* dataptr);
|
---|
200 | };
|
---|
201 |
|
---|
202 | #endif /* HID_H_INCLUDED */
|
---|