2 Helper functions to parse HID report descriptor and items.
4 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Get next HID item from report descriptor.
21 This function retrieves next HID item from report descriptor, according to
23 According to USB HID Specification, An item is piece of information
24 about the device. All items have a one-byte prefix that contains
25 the item tag, item type, and item size.
26 There are two basic types of items: short items and long items.
27 If the item is a short item, its optional data size may be 0, 1, 2, or 4 bytes.
28 Only short item is supported here.
30 @param StartPos Start position of the HID item to get.
31 @param EndPos End position of the range to get the the next HID item.
32 @param HidItem Buffer for the HID Item to return.
34 @return Pointer to end of the HID item returned.
35 NULL if no HID item retrieved.
47 if (EndPos
<= StartPos
) {
55 // Bit format of prefix byte:
60 HidItem
->Type
= BitFieldRead8 (Temp
, 2, 3);
61 HidItem
->Tag
= BitFieldRead8 (Temp
, 4, 7);
63 if (HidItem
->Tag
== HID_ITEM_TAG_LONG
) {
65 // Long Items are not supported, although we try to parse it.
67 HidItem
->Format
= HID_ITEM_FORMAT_LONG
;
69 if ((EndPos
- StartPos
) >= 2) {
70 HidItem
->Size
= *StartPos
++;
71 HidItem
->Tag
= *StartPos
++;
73 if ((EndPos
- StartPos
) >= HidItem
->Size
) {
74 HidItem
->Data
.LongData
= StartPos
;
75 StartPos
+= HidItem
->Size
;
80 HidItem
->Format
= HID_ITEM_FORMAT_SHORT
;
81 HidItem
->Size
= BitFieldRead8 (Temp
, 0, 1);
83 switch (HidItem
->Size
) {
94 if ((EndPos
- StartPos
) >= 1) {
95 HidItem
->Data
.Uint8
= *StartPos
++;
103 if ((EndPos
- StartPos
) >= 2) {
104 CopyMem (&HidItem
->Data
.Uint16
, StartPos
, sizeof (UINT16
));
111 // 4-byte data, adjust size
114 if ((EndPos
- StartPos
) >= 4) {
115 CopyMem (&HidItem
->Data
.Uint32
, StartPos
, sizeof (UINT32
));
127 Get data from HID item.
129 This function retrieves data from HID item.
130 It only supports short items, which has 4 types of data:
133 @param HidItem Pointer to the HID item.
135 @return The data of HID item.
144 // Get data from HID item.
146 switch (HidItem
->Size
) {
148 return HidItem
->Data
.Uint8
;
150 return HidItem
->Data
.Uint16
;
152 return HidItem
->Data
.Uint32
;
158 Parse HID item from report descriptor.
160 There are three item types: Main, Global, and Local.
161 This function parses these types of HID items according
164 @param UsbMouse The instance of USB_MOUSE_DEV
165 @param HidItem The HID item to parse
170 IN USB_MOUSE_DEV
*UsbMouse
,
176 switch (HidItem
->Type
) {
178 case HID_ITEM_TYPE_MAIN
:
180 // we don't care any main items, just skip
184 case HID_ITEM_TYPE_GLOBAL
:
186 // For global items, we only care Usage Page tag for Button Page here
188 if (HidItem
->Tag
== HID_GLOBAL_ITEM_TAG_USAGE_PAGE
) {
189 Data
= (UINT8
) GetItemData (HidItem
);
194 UsbMouse
->PrivateData
.ButtonDetected
= TRUE
;
199 case HID_ITEM_TYPE_LOCAL
:
200 if (HidItem
->Size
== 0) {
202 // No expected data for local item
207 Data
= (UINT8
) GetItemData (HidItem
);
209 switch (HidItem
->Tag
) {
210 case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM
:
211 if (UsbMouse
->PrivateData
.ButtonDetected
) {
212 UsbMouse
->PrivateData
.ButtonMinIndex
= Data
;
216 case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM
:
218 if (UsbMouse
->PrivateData
.ButtonDetected
) {
219 UsbMouse
->PrivateData
.ButtonMaxIndex
= Data
;
232 Parse Mouse Report Descriptor.
234 According to USB HID Specification, report descriptors are
235 composed of pieces of information. Each piece of information
236 is called an Item. This function retrieves each item from
237 the report descriptor and updates USB_MOUSE_DEV.
239 @param UsbMouse The instance of USB_MOUSE_DEV
240 @param ReportDescriptor Report descriptor to parse
241 @param ReportSize Report descriptor size
243 @retval EFI_SUCCESS Report descriptor successfully parsed.
244 @retval EFI_UNSUPPORTED Report descriptor contains long item.
248 ParseMouseReportDescriptor (
249 OUT USB_MOUSE_DEV
*UsbMouse
,
250 IN UINT8
*ReportDescriptor
,
254 UINT8
*DescriptorEnd
;
258 DescriptorEnd
= ReportDescriptor
+ ReportSize
;
260 Ptr
= GetNextHidItem (ReportDescriptor
, DescriptorEnd
, &HidItem
);
261 while (Ptr
!= NULL
) {
262 if (HidItem
.Format
!= HID_ITEM_FORMAT_SHORT
) {
264 // Long Item is not supported at current HID revision
266 return EFI_UNSUPPORTED
;
269 ParseHidItem (UsbMouse
, &HidItem
);
271 Ptr
= GetNextHidItem (Ptr
, DescriptorEnd
, &HidItem
);
274 UsbMouse
->NumberOfButtons
= (UINT8
) (UsbMouse
->PrivateData
.ButtonMaxIndex
- UsbMouse
->PrivateData
.ButtonMinIndex
+ 1);
275 UsbMouse
->XLogicMax
= 127;
276 UsbMouse
->YLogicMax
= 127;
277 UsbMouse
->XLogicMin
= -127;
278 UsbMouse
->YLogicMin
= -127;