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