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