Code Scrub for USB mouse driver.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@7151 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
xli24
2008-12-30 05:55:39 +00:00
parent 70ec469973
commit 29129ce463
6 changed files with 792 additions and 946 deletions

View File

@@ -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.
@param StartPos Start Position.
@param EndPos End Position.
@param HidItem HidItem to return.
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.
@return Position.
@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 *
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
// Bit format of prefix byte:
// Bits 0-1: Size
// Bits 2-3: Type
// Bits 4-7: Tag
//
HidItem->Type = (UINT8) ((Temp >> 2) & 0x03);
//
// bit 4-7
//
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,140 +141,28 @@ 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
**/
VOID
ParseLocalItem (
IN USB_MOUSE_DEV *UsbMouse,
IN HID_ITEM *LocalItem
)
{
UINT32 Data;
if (LocalItem->Size == 0) {
//
// No expected data for local item
//
return ;
}
Data = GetItemData (LocalItem);
switch (LocalItem->Tag) {
case HID_LOCAL_ITEM_TAG_DELIMITER:
//
// we don't support delimiter here
//
return ;
case HID_LOCAL_ITEM_TAG_USAGE:
return ;
case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
if (UsbMouse->PrivateData.ButtonDetected) {
UsbMouse->PrivateData.ButtonMinIndex = (UINT8) Data;
}
return ;
case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
{
if (UsbMouse->PrivateData.ButtonDetected) {
UsbMouse->PrivateData.ButtonMaxIndex = (UINT8) 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:
{
UsagePage = (UINT8) GetItemData (GlobalItem);
//
// We only care Button Page here
//
if (UsagePage == 0x09) {
//
// Button Page
//
UsbMouse->PrivateData.ButtonDetected = TRUE;
return ;
}
break;
}
}
}
/**
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
@param HidItem The HID item to parse
**/
VOID
@@ -273,28 +171,59 @@ ParseHidItem (
IN HID_ITEM *HidItem
)
{
UINT8 Data;
switch (HidItem->Type) {
case HID_ITEM_TYPE_MAIN:
//
// For Main Item, parse main item
// we don't care any main items, just skip
//
ParseMainItem (UsbMouse, HidItem);
break;
return;
case HID_ITEM_TYPE_GLOBAL:
//
// For global Item, parse global item
// For global items, we only care Usage Page tag for Button Page here
//
ParseGlobalItem (UsbMouse, HidItem);
break;
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:
//
// For Local Item, parse local item
//
ParseLocalItem (UsbMouse, HidItem);
break;
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;
}
}
}
@@ -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;
}