1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/** @file
Helper functions to parse HID report descriptor and items.
Copyright (c) 2004 - 2008, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "UsbMouseAbsolutePointer.h"
/**
Get next HID item from report descriptor.
This function retrieves next HID item from report descriptor, according to
the start position.
According to USB HID Specification, An item is piece of information
about the device. All items have a one-byte prefix that contains
the item tag, item type, and item size.
There are two basic types of items: short items and long items.
If the item is a short item, its optional data size may be 0, 1, 2, or 4 bytes.
Only short item is supported here.
@param StartPos Start position of the HID item to get.
@param EndPos End position of the range to get the the next HID item.
@param HidItem Buffer for the HID Item to return.
@return Pointer to end of the HID item returned.
NULL if no HID item retrieved.
**/
UINT8 *
GetNextHidItem (
IN UINT8 *StartPos,
IN UINT8 *EndPos,
OUT HID_ITEM *HidItem
)
{
UINT8 Temp;
if (EndPos <= StartPos) {
return NULL;
}
Temp = *StartPos;
StartPos++;
//
// Bit format of prefix byte:
// Bits 0-1: Size
// Bits 2-3: Type
// Bits 4-7: Tag
//
HidItem->Type = BitFieldRead8 (Temp, 2, 3);
HidItem->Tag = BitFieldRead8 (Temp, 4, 7);
if (HidItem->Tag == HID_ITEM_TAG_LONG) {
//
// Long Items are not supported, although we try to parse it.
//
HidItem->Format = HID_ITEM_FORMAT_LONG;
if ((EndPos - StartPos) >= 2) {
HidItem->Size = *StartPos++;
HidItem->Tag = *StartPos++;
if ((EndPos - StartPos) >= HidItem->Size) {
HidItem->Data.LongData = StartPos;
StartPos += HidItem->Size;
return StartPos;
}
}
} else {
HidItem->Format = HID_ITEM_FORMAT_SHORT;
HidItem->Size = BitFieldRead8 (Temp, 0, 1);
switch (HidItem->Size) {
case 0:
//
// No data
//
return StartPos;
case 1:
//
// 1-byte data
//
if ((EndPos - StartPos) >= 1) {
HidItem->Data.U8 = *StartPos++;
return StartPos;
}
case 2:
//
// 2-byte data
//
if ((EndPos - StartPos) >= 2) {
CopyMem (&HidItem->Data.U16, StartPos, sizeof (UINT16));
StartPos += 2;
return StartPos;
}
case 3:
//
// 4-byte data, adjust size
//
HidItem->Size = 4;
if ((EndPos - StartPos) >= 4) {
CopyMem (&HidItem->Data.U32, StartPos, sizeof (UINT32));
StartPos += 4;
return StartPos;
}
}
}
return NULL;
}
/**
Get data from HID item.
This function retrieves data from HID item.
It only supports short items, which has 4 types of data:
0, 1, 2, or 4 bytes.
@param HidItem Pointer to the HID item.
@return The data of HID item.
**/
UINT32
GetItemData (
IN HID_ITEM *HidItem
)
{
//
// Get data from HID item.
//
switch (HidItem->Size) {
case 1:
return HidItem->Data.U8;
case 2:
return HidItem->Data.U16;
case 4:
return HidItem->Data.U32;
}
return 0;
}
/**
Parse HID item from report descriptor.
There are three item types: Main, Global, and Local.
This function parses these types of HID items according
to tag info.
@param UsbMouse The instance of USB_MOUSE_ABSOLUTE_POINTER_DEV
@param HidItem The HID item to parse
**/
VOID
ParseHidItem (
IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouse,
IN HID_ITEM *HidItem
)
{
UINT8 Data;
switch (HidItem->Type) {
case HID_ITEM_TYPE_MAIN:
//
// we don't care any main items, just skip
//
return ;
case HID_ITEM_TYPE_GLOBAL:
//
// For global items, we only care Usage Page tag for Button Page here
//
if (HidItem->Tag == HID_GLOBAL_ITEM_TAG_USAGE_PAGE) {
Data = (UINT8) GetItemData (HidItem);
if (Data == 0x09) {
//
// Button Page
//
UsbMouse->PrivateData.ButtonDetected = TRUE;
}
}
return;
case HID_ITEM_TYPE_LOCAL:
if (HidItem->Size == 0) {
//
// No expected data for local item
//
return ;
}
Data = (UINT8) GetItemData (HidItem);
switch (HidItem->Tag) {
case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
if (UsbMouse->PrivateData.ButtonDetected) {
UsbMouse->PrivateData.ButtonMinIndex = Data;
}
return ;
case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
{
if (UsbMouse->PrivateData.ButtonDetected) {
UsbMouse->PrivateData.ButtonMaxIndex = Data;
}
return ;
}
default:
return ;
}
}
}
/**
Parse Mouse Report Descriptor.
According to USB HID Specification, report descriptors are
composed of pieces of information. Each piece of information
is called an Item. This function retrieves each item from
the report descriptor and updates USB_MOUSE_ABSOLUTE_POINTER_DEV.
@param UsbMouseAbsolutePointer The instance of USB_MOUSE_ABSOLUTE_POINTER_DEV
@param ReportDescriptor Report descriptor to parse
@param ReportSize Report descriptor size
@retval EFI_SUCCESS Report descriptor successfully parsed.
@retval EFI_UNSUPPORTED Report descriptor contains long item.
**/
EFI_STATUS
ParseMouseReportDescriptor (
OUT USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
IN UINT8 *ReportDescriptor,
IN UINTN ReportSize
)
{
UINT8 *DescriptorEnd;
UINT8 *Ptr;
HID_ITEM HidItem;
DescriptorEnd = ReportDescriptor + ReportSize;
Ptr = GetNextHidItem (ReportDescriptor, DescriptorEnd, &HidItem);
while (Ptr != NULL) {
if (HidItem.Format != HID_ITEM_FORMAT_SHORT) {
//
// Long Item is not supported at current HID revision
//
return EFI_UNSUPPORTED;
}
ParseHidItem (UsbMouseAbsolutePointer, &HidItem);
Ptr = GetNextHidItem (Ptr, DescriptorEnd, &HidItem);
}
UsbMouseAbsolutePointer->NumberOfButtons = (UINT8) (UsbMouseAbsolutePointer->PrivateData.ButtonMaxIndex - UsbMouseAbsolutePointer->PrivateData.ButtonMinIndex + 1);
UsbMouseAbsolutePointer->XLogicMax = 1023;
UsbMouseAbsolutePointer->YLogicMax = 1023;
UsbMouseAbsolutePointer->XLogicMin = -1023;
UsbMouseAbsolutePointer->YLogicMin = -1023;
return EFI_SUCCESS;
}
|