summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c
diff options
context:
space:
mode:
Diffstat (limited to 'MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c')
-rw-r--r--MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c267
1 files changed, 101 insertions, 166 deletions
diff --git a/MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c b/MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c
index 212fbf89d2..84eb4ac1c5 100644
--- a/MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c
+++ b/MdeModulePkg/Bus/Usb/UsbMouseDxe/MouseHid.c
@@ -1,6 +1,5 @@
/** @file
-
- Parse mouse hid descriptor.
+ Helper functions to parse HID report descriptor and items.
Copyright (c) 2004 - 2008, Intel Corporation
All rights reserved. This program and the accompanying materials
@@ -13,21 +12,31 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
-#include "MouseHid.h"
+#include "UsbMouse.h"
/**
- Get next item from report descriptor.
+ 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
@@ -35,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;
@@ -69,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
@@ -83,7 +89,7 @@ GetNextItem (
case 1:
//
- // One byte data
+ // 1-byte data
//
if ((EndPos - StartPos) >= 1) {
HidItem->Data.U8 = *StartPos++;
@@ -92,7 +98,7 @@ GetNextItem (
case 2:
//
- // Two byte data
+ // 2-byte data
//
if ((EndPos - StartPos) >= 2) {
CopyMem (&HidItem->Data.U16, StartPos, sizeof (UINT16));
@@ -102,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;
@@ -118,11 +124,15 @@ GetNextItem (
/**
- Get item data from report descriptor.
+ Get data from HID item.
- @param HidItem The pointer to 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.
- @return The Data of HidItem.
+ @param HidItem Pointer to the HID item.
+
+ @return The data of HID item.
**/
UINT32
@@ -131,170 +141,89 @@ 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 from report descriptor.
+ 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_DEV
- @param LocalItem The pointer to local hid item
+ @param HidItem The HID item to parse
**/
VOID
-ParseLocalItem (
+ParseHidItem (
IN USB_MOUSE_DEV *UsbMouse,
- IN HID_ITEM *LocalItem
+ 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 ;
- }
+ return;
- Data = GetItemData (LocalItem);
-
- switch (LocalItem->Tag) {
-
- case HID_LOCAL_ITEM_TAG_DELIMITER:
+ case HID_ITEM_TYPE_GLOBAL:
//
- // we don't support delimiter here
+ // For global items, we only care Usage Page tag for Button Page here
//
- return ;
-
- case HID_LOCAL_ITEM_TAG_USAGE:
- return ;
+ 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_USAGE_MINIMUM:
- if (UsbMouse->PrivateData.ButtonDetected) {
- UsbMouse->PrivateData.ButtonMinIndex = (UINT8) Data;
+ case HID_ITEM_TYPE_LOCAL:
+ if (HidItem->Size == 0) {
+ //
+ // No expected data for local item
+ //
+ return ;
}
- return ;
+ Data = (UINT8) GetItemData (HidItem);
- case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
- {
+ switch (HidItem->Tag) {
+ case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
if (UsbMouse->PrivateData.ButtonDetected) {
- UsbMouse->PrivateData.ButtonMaxIndex = (UINT8) Data;
+ UsbMouse->PrivateData.ButtonMinIndex = Data;
}
-
return ;
- }
- }
-}
-
-/**
- Parse global item from report descriptor.
-
- @param UsbMouse The instance of USB_MOUSE_DEV
- @param GlobalItem The pointer to global hid item
-
-**/
-VOID
-ParseGlobalItem (
- IN USB_MOUSE_DEV *UsbMouse,
- IN HID_ITEM *GlobalItem
- )
-{
- UINT8 UsagePage;
-
- switch (GlobalItem->Tag) {
- case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
+ case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
{
- UsagePage = (UINT8) GetItemData (GlobalItem);
-
- //
- // We only care Button Page here
- //
- if (UsagePage == 0x09) {
- //
- // Button Page
- //
- UsbMouse->PrivateData.ButtonDetected = TRUE;
- return ;
+ if (UsbMouse->PrivateData.ButtonDetected) {
+ UsbMouse->PrivateData.ButtonMaxIndex = Data;
}
- break;
+ return ;
}
- }
-}
-
-
-/**
- Parse main item from report descriptor.
-
- @param UsbMouse The instance of USB_MOUSE_DEV
- @param MainItem Main hid item to parse
-
-**/
-VOID
-ParseMainItem (
- IN USB_MOUSE_DEV *UsbMouse,
- IN HID_ITEM *MainItem
- )
-{
- //
- // we don't care any main items, just skip
- //
- return ;
-}
-
-
-/**
- Parse hid item from report descriptor.
-
- @param UsbMouse The instance of USB_MOUSE_DEV
- @param HidItem The hid item to parse
-
-**/
-VOID
-ParseHidItem (
- IN USB_MOUSE_DEV *UsbMouse,
- IN HID_ITEM *HidItem
- )
-{
- switch (HidItem->Type) {
-
- case HID_ITEM_TYPE_MAIN:
- //
- // For Main Item, parse main item
- //
- ParseMainItem (UsbMouse, HidItem);
- break;
-
- case HID_ITEM_TYPE_GLOBAL:
- //
- // For global Item, parse global item
- //
- ParseGlobalItem (UsbMouse, HidItem);
- break;
-
- case HID_ITEM_TYPE_LOCAL:
- //
- // For Local Item, parse local item
- //
- ParseLocalItem (UsbMouse, HidItem);
- break;
+ default:
+ return;
+ }
}
}
@@ -302,17 +231,22 @@ ParseHidItem (
/**
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_DEV.
+
@param UsbMouse The instance of USB_MOUSE_DEV
@param ReportDescriptor Report descriptor to parse
@param ReportSize Report descriptor size
- @retval EFI_DEVICE_ERROR Report descriptor error
- @retval EFI_SUCCESS Parse descriptor success
+ @retval EFI_SUCCESS Report descriptor successfully parsed.
+ @retval EFI_UNSUPPORTED Report descriptor contains long item.
**/
EFI_STATUS
ParseMouseReportDescriptor (
- IN USB_MOUSE_DEV *UsbMouse,
+ OUT USB_MOUSE_DEV *UsbMouse,
IN UINT8 *ReportDescriptor,
IN UINTN ReportSize
)
@@ -323,24 +257,25 @@ ParseMouseReportDescriptor (
DescriptorEnd = ReportDescriptor + ReportSize;
- Ptr = GetNextItem (ReportDescriptor, DescriptorEnd, &HidItem);
-
+ 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 (UsbMouse, &HidItem);
- Ptr = GetNextItem (Ptr, DescriptorEnd, &HidItem);
+ Ptr = GetNextHidItem (Ptr, DescriptorEnd, &HidItem);
}
- UsbMouse->NumberOfButtons = (UINT8) (UsbMouse->PrivateData.ButtonMaxIndex - UsbMouse->PrivateData.ButtonMinIndex + 1);
- UsbMouse->XLogicMax = UsbMouse->YLogicMax = 127;
- UsbMouse->XLogicMin = UsbMouse->YLogicMin = -127;
+ UsbMouse->NumberOfButtons = (UINT8) (UsbMouse->PrivateData.ButtonMaxIndex - UsbMouse->PrivateData.ButtonMinIndex + 1);
+ UsbMouse->XLogicMax = 127;
+ UsbMouse->YLogicMax = 127;
+ UsbMouse->XLogicMin = -127;
+ UsbMouse->YLogicMin = -127;
return EFI_SUCCESS;
}