summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c')
-rw-r--r--MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c289
1 files changed, 107 insertions, 182 deletions
diff --git a/MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c b/MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c
index 21b8d05aa8..e0112dd427 100644
--- a/MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c
+++ b/MdeModulePkg/Bus/Usb/UsbMouseAbsolutePointerDxe/mousehid.c
@@ -1,6 +1,7 @@
/** @file
+ Helper functions to parse HID report descriptor and items.
-Copyright (c) 2004, Intel Corporation
+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
@@ -9,35 +10,33 @@ 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.
-Module Name:
-
- Mousehid.c
-
-Abstract:
- Parse mouse hid descriptor
-
-
**/
-#include "mousehid.h"
-
+#include "UsbMouseAbsolutePointer.h"
-//
-// Get an item from report descriptor
-//
/**
- Get Next Item
+ 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
- @param EndPos End Position
- @param HidItem HidItem to return
+ @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 Position
+ @return Pointer to end of the HID item returned.
+ NULL if no HID item retrieved.
**/
UINT8 *
-GetNextItem (
+GetNextHidItem (
IN UINT8 *StartPos,
IN UINT8 *EndPos,
OUT HID_ITEM *HidItem
@@ -45,25 +44,25 @@ GetNextItem (
{
UINT8 Temp;
- if ((EndPos - StartPos) <= 0) {
+ if (EndPos <= StartPos) {
return NULL;
}
Temp = *StartPos;
StartPos++;
+
//
- // bit 2,3
- //
- HidItem->Type = (UINT8) ((Temp >> 2) & 0x03);
- //
- // bit 4-7
+ // Bit format of prefix byte:
+ // Bits 0-1: Size
+ // Bits 2-3: Type
+ // Bits 4-7: Tag
//
- HidItem->Tag = (UINT8) ((Temp >> 4) & 0x0F);
+ HidItem->Type = BitFieldRead8 (Temp, 2, 3);
+ HidItem->Tag = BitFieldRead8 (Temp, 4, 7);
if (HidItem->Tag == HID_ITEM_TAG_LONG) {
//
- // Long Items are not supported by HID rev1.0,
- // although we try to parse it.
+ // Long Items are not supported, although we try to parse it.
//
HidItem->Format = HID_ITEM_FORMAT_LONG;
@@ -79,12 +78,9 @@ GetNextItem (
}
} else {
HidItem->Format = HID_ITEM_FORMAT_SHORT;
- //
- // bit 0, 1
- //
- HidItem->Size = (UINT8) (Temp & 0x03);
- switch (HidItem->Size) {
+ HidItem->Size = BitFieldRead8 (Temp, 0, 1);
+ switch (HidItem->Size) {
case 0:
//
// No data
@@ -93,7 +89,7 @@ GetNextItem (
case 1:
//
- // One byte data
+ // 1-byte data
//
if ((EndPos - StartPos) >= 1) {
HidItem->Data.U8 = *StartPos++;
@@ -102,7 +98,7 @@ GetNextItem (
case 2:
//
- // Two byte data
+ // 2-byte data
//
if ((EndPos - StartPos) >= 2) {
CopyMem (&HidItem->Data.U16, StartPos, sizeof (UINT16));
@@ -112,9 +108,9 @@ GetNextItem (
case 3:
//
- // 4 byte data, adjust size
+ // 4-byte data, adjust size
//
- HidItem->Size++;
+ HidItem->Size = 4;
if ((EndPos - StartPos) >= 4) {
CopyMem (&HidItem->Data.U32, StartPos, sizeof (UINT32));
StartPos += 4;
@@ -128,11 +124,15 @@ GetNextItem (
/**
- Get Item Data
+ 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 HID_ITEM
+ @param HidItem Pointer to the HID item.
- @return HidItem Data
+ @return The data of HID item.
**/
UINT32
@@ -141,216 +141,141 @@ GetItemData (
)
{
//
- // Get Data from HID_ITEM structure
+ // 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 Local Item
+ Parse HID item from report descriptor.
- @param UsbMouseAbsolutePointer USB_MOUSE_ABSOLUTE_POINTER_DEV
- @param LocalItem Local Item
+ 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
-ParseLocalItem (
- IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
- IN HID_ITEM *LocalItem
+ParseHidItem (
+ IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouse,
+ IN HID_ITEM *HidItem
)
{
- UINT32 Data;
+ UINT8 Data;
- if (LocalItem->Size == 0) {
+ switch (HidItem->Type) {
+
+ case HID_ITEM_TYPE_MAIN:
//
- // No expected data for local item
+ // we don't care any main items, just skip
//
return ;
- }
-
- Data = GetItemData (LocalItem);
- switch (LocalItem->Tag) {
+ 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_LOCAL_ITEM_TAG_DELIMITER:
+ case HID_ITEM_TYPE_LOCAL:
+ if (HidItem->Size == 0) {
//
- // we don't support delimiter here
+ // No expected data for local item
//
return ;
+ }
- case HID_LOCAL_ITEM_TAG_USAGE:
- return ;
+ Data = (UINT8) GetItemData (HidItem);
- case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
- if (UsbMouseAbsolutePointer->PrivateData.ButtonDetected) {
- UsbMouseAbsolutePointer->PrivateData.ButtonMinIndex = (UINT8) Data;
+ 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:
+ case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
{
- if (UsbMouseAbsolutePointer->PrivateData.ButtonDetected) {
- UsbMouseAbsolutePointer->PrivateData.ButtonMaxIndex = (UINT8) Data;
+ if (UsbMouse->PrivateData.ButtonDetected) {
+ UsbMouse->PrivateData.ButtonMaxIndex = Data;
}
-
return ;
}
- }
-}
-
-VOID
-ParseGlobalItem (
- IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
- IN HID_ITEM *GlobalItem
- )
-{
- UINT8 UsagePage;
-
- switch (GlobalItem->Tag) {
- case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
- {
- UsagePage = (UINT8) GetItemData (GlobalItem);
- //
- // We only care Button Page here
- //
- if (UsagePage == 0x09) {
- //
- // Button Page
- //
- UsbMouseAbsolutePointer->PrivateData.ButtonDetected = TRUE;
+ default:
return ;
}
- break;
- }
-
}
}
-
/**
- Parse Main Item
-
- @param UsbMouseAbsolutePointer USB_MOUSE_ABSOLUTE_POINTER_DEV
- @param MainItem HID_ITEM to parse
-
- @return VOID
-
-**/
-VOID
-ParseMainItem (
- IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
- IN HID_ITEM *MainItem
- )
-{
- //
- // we don't care any main items, just skip
- //
- return ;
-}
-
-
-/**
- Parse Hid Item
-
- @param UsbMouseAbsolutePointer USB_MOUSE_ABSOLUTE_POINTER_DEV
- @param HidItem HidItem to parse
-
- @return VOID
+ Parse Mouse Report Descriptor.
-**/
-VOID
-ParseHidItem (
- IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
- IN HID_ITEM *HidItem
- )
-{
- switch (HidItem->Type) {
+ 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.
- case HID_ITEM_TYPE_MAIN:
- //
- // For Main Item, parse main item
- //
- ParseMainItem (UsbMouseAbsolutePointer, HidItem);
- break;
+ @param UsbMouseAbsolutePointer The instance of USB_MOUSE_ABSOLUTE_POINTER_DEV
+ @param ReportDescriptor Report descriptor to parse
+ @param ReportSize Report descriptor size
- case HID_ITEM_TYPE_GLOBAL:
- //
- // For global Item, parse global item
- //
- ParseGlobalItem (UsbMouseAbsolutePointer, HidItem);
- break;
-
- case HID_ITEM_TYPE_LOCAL:
- //
- // For Local Item, parse local item
- //
- ParseLocalItem (UsbMouseAbsolutePointer, HidItem);
- break;
- }
-}
-//
-// A simple parse just read some field we are interested in
-//
-
-/**
- Parse Mouse Report Descriptor
-
- @param UsbMouse USB_MOUSE_DEV
- @param ReportDescriptor Report descriptor to parse
- @param ReportSize Report descriptor size
-
- @retval EFI_DEVICE_ERROR Report descriptor error
- @retval EFI_SUCCESS Success
+ @retval EFI_SUCCESS Report descriptor successfully parsed.
+ @retval EFI_UNSUPPORTED Report descriptor contains long item.
**/
EFI_STATUS
ParseMouseReportDescriptor (
- IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
- IN UINT8 *ReportDescriptor,
- IN UINTN ReportSize
+ OUT USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointer,
+ IN UINT8 *ReportDescriptor,
+ IN UINTN ReportSize
)
{
UINT8 *DescriptorEnd;
- UINT8 *ptr;
+ UINT8 *Ptr;
HID_ITEM HidItem;
DescriptorEnd = ReportDescriptor + ReportSize;
- ptr = GetNextItem (ReportDescriptor, DescriptorEnd, &HidItem);
-
- while (ptr != NULL) {
+ Ptr = GetNextHidItem (ReportDescriptor, DescriptorEnd, &HidItem);
+ while (Ptr != NULL) {
if (HidItem.Format != HID_ITEM_FORMAT_SHORT) {
//
- // Long Format Item is not supported at current HID revision
+ // Long Item is not supported at current HID revision
//
- return EFI_DEVICE_ERROR;
+ return EFI_UNSUPPORTED;
}
ParseHidItem (UsbMouseAbsolutePointer, &HidItem);
- ptr = GetNextItem (ptr, DescriptorEnd, &HidItem);
+ Ptr = GetNextHidItem (Ptr, DescriptorEnd, &HidItem);
}
- UsbMouseAbsolutePointer->NumberOfButtons = (UINT8) (UsbMouseAbsolutePointer->PrivateData.ButtonMaxIndex - UsbMouseAbsolutePointer->PrivateData.ButtonMinIndex + 1);
- UsbMouseAbsolutePointer->XLogicMax = UsbMouseAbsolutePointer->YLogicMax = 1023;
- UsbMouseAbsolutePointer->XLogicMin = UsbMouseAbsolutePointer->YLogicMin = -1023;
+ 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;
}