2 Helper functions to parse HID report descriptor and items.
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "UsbMouseAbsolutePointer.h"
12 Get next HID item from report descriptor.
14 This function retrieves next HID item from report descriptor, according to
16 According to USB HID Specification, An item is piece of information
17 about the device. All items have a one-byte prefix that contains
18 the item tag, item type, and item size.
19 There are two basic types of items: short items and long items.
20 If the item is a short item, its optional data size may be 0, 1, 2, or 4 bytes.
21 Only short item is supported here.
23 @param StartPos Start position of the HID item to get.
24 @param EndPos End position of the range to get the next HID item.
25 @param HidItem Buffer for the HID Item to return.
27 @return Pointer to end of the HID item returned.
28 NULL if no HID item retrieved.
40 if (EndPos
<= StartPos
) {
48 // Bit format of prefix byte:
53 HidItem
->Type
= BitFieldRead8 (Temp
, 2, 3);
54 HidItem
->Tag
= BitFieldRead8 (Temp
, 4, 7);
56 if (HidItem
->Tag
== HID_ITEM_TAG_LONG
) {
58 // Long Items are not supported, although we try to parse it.
60 HidItem
->Format
= HID_ITEM_FORMAT_LONG
;
62 if ((EndPos
- StartPos
) >= 2) {
63 HidItem
->Size
= *StartPos
++;
64 HidItem
->Tag
= *StartPos
++;
66 if ((EndPos
- StartPos
) >= HidItem
->Size
) {
67 HidItem
->Data
.LongData
= StartPos
;
68 StartPos
+= HidItem
->Size
;
73 HidItem
->Format
= HID_ITEM_FORMAT_SHORT
;
74 HidItem
->Size
= BitFieldRead8 (Temp
, 0, 1);
76 switch (HidItem
->Size
) {
87 if ((EndPos
- StartPos
) >= 1) {
88 HidItem
->Data
.Uint8
= *StartPos
++;
96 if ((EndPos
- StartPos
) >= 2) {
97 CopyMem (&HidItem
->Data
.Uint16
, StartPos
, sizeof (UINT16
));
104 // 4-byte data, adjust size
107 if ((EndPos
- StartPos
) >= 4) {
108 CopyMem (&HidItem
->Data
.Uint32
, StartPos
, sizeof (UINT32
));
119 Get data from HID item.
121 This function retrieves data from HID item.
122 It only supports short items, which has 4 types of data:
125 @param HidItem Pointer to the HID item.
127 @return The data of HID item.
136 // Get data from HID item.
138 switch (HidItem
->Size
) {
140 return HidItem
->Data
.Uint8
;
142 return HidItem
->Data
.Uint16
;
144 return HidItem
->Data
.Uint32
;
151 Parse HID item from report descriptor.
153 There are three item types: Main, Global, and Local.
154 This function parses these types of HID items according
157 @param UsbMouse The instance of USB_MOUSE_ABSOLUTE_POINTER_DEV
158 @param HidItem The HID item to parse
163 IN USB_MOUSE_ABSOLUTE_POINTER_DEV
*UsbMouse
,
169 switch (HidItem
->Type
) {
170 case HID_ITEM_TYPE_MAIN
:
172 // we don't care any main items, just skip
176 case HID_ITEM_TYPE_GLOBAL
:
178 // For global items, we only care Usage Page tag for Button Page here
180 if (HidItem
->Tag
== HID_GLOBAL_ITEM_TAG_USAGE_PAGE
) {
181 Data
= (UINT8
)GetItemData (HidItem
);
186 UsbMouse
->PrivateData
.ButtonDetected
= TRUE
;
192 case HID_ITEM_TYPE_LOCAL
:
193 if (HidItem
->Size
== 0) {
195 // No expected data for local item
200 Data
= (UINT8
)GetItemData (HidItem
);
202 switch (HidItem
->Tag
) {
203 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM
:
204 if (UsbMouse
->PrivateData
.ButtonDetected
) {
205 UsbMouse
->PrivateData
.ButtonMinIndex
= Data
;
210 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM
:
212 if (UsbMouse
->PrivateData
.ButtonDetected
) {
213 UsbMouse
->PrivateData
.ButtonMaxIndex
= Data
;
226 Parse Mouse Report Descriptor.
228 According to USB HID Specification, report descriptors are
229 composed of pieces of information. Each piece of information
230 is called an Item. This function retrieves each item from
231 the report descriptor and updates USB_MOUSE_ABSOLUTE_POINTER_DEV.
233 @param UsbMouseAbsolutePointer The instance of USB_MOUSE_ABSOLUTE_POINTER_DEV
234 @param ReportDescriptor Report descriptor to parse
235 @param ReportSize Report descriptor size
237 @retval EFI_SUCCESS Report descriptor successfully parsed.
238 @retval EFI_UNSUPPORTED Report descriptor contains long item.
242 ParseMouseReportDescriptor (
243 OUT USB_MOUSE_ABSOLUTE_POINTER_DEV
*UsbMouseAbsolutePointer
,
244 IN UINT8
*ReportDescriptor
,
248 UINT8
*DescriptorEnd
;
252 DescriptorEnd
= ReportDescriptor
+ ReportSize
;
254 Ptr
= GetNextHidItem (ReportDescriptor
, DescriptorEnd
, &HidItem
);
255 while (Ptr
!= NULL
) {
256 if (HidItem
.Format
!= HID_ITEM_FORMAT_SHORT
) {
258 // Long Item is not supported at current HID revision
260 return EFI_UNSUPPORTED
;
263 ParseHidItem (UsbMouseAbsolutePointer
, &HidItem
);
265 Ptr
= GetNextHidItem (Ptr
, DescriptorEnd
, &HidItem
);
268 UsbMouseAbsolutePointer
->NumberOfButtons
= (UINT8
)(UsbMouseAbsolutePointer
->PrivateData
.ButtonMaxIndex
- UsbMouseAbsolutePointer
->PrivateData
.ButtonMinIndex
+ 1);
269 UsbMouseAbsolutePointer
->XLogicMax
= 1023;
270 UsbMouseAbsolutePointer
->YLogicMax
= 1023;
271 UsbMouseAbsolutePointer
->XLogicMin
= -1023;
272 UsbMouseAbsolutePointer
->YLogicMin
= -1023;