You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
356 lines
10 KiB
356 lines
10 KiB
15 years ago
|
/*
|
||
|
LUFA Library
|
||
14 years ago
|
Copyright (C) Dean Camera, 2011.
|
||
14 years ago
|
|
||
15 years ago
|
dean [at] fourwalledcubicle [dot] com
|
||
14 years ago
|
www.lufa-lib.org
|
||
15 years ago
|
*/
|
||
|
|
||
|
/*
|
||
14 years ago
|
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
|
||
15 years ago
|
|
||
14 years ago
|
Permission to use, copy, modify, distribute, and sell this
|
||
15 years ago
|
software and its documentation for any purpose is hereby granted
|
||
14 years ago
|
without fee, provided that the above copyright notice appear in
|
||
15 years ago
|
all copies and that both that the copyright notice and this
|
||
14 years ago
|
permission notice and warranty disclaimer appear in supporting
|
||
|
documentation, and that the name of the author not be used in
|
||
|
advertising or publicity pertaining to distribution of the
|
||
15 years ago
|
software without specific, written prior permission.
|
||
|
|
||
|
The author disclaim all warranties with regard to this
|
||
|
software, including all implied warranties of merchantability
|
||
|
and fitness. In no event shall the author be liable for any
|
||
|
special, indirect or consequential damages or any damages
|
||
|
whatsoever resulting from loss of use, data or profits, whether
|
||
|
in an action of contract, negligence or other tortious action,
|
||
|
arising out of or in connection with the use or performance of
|
||
|
this software.
|
||
|
*/
|
||
|
|
||
|
#define __INCLUDE_FROM_USB_DRIVER
|
||
14 years ago
|
#define __INCLUDE_FROM_HID_DRIVER
|
||
15 years ago
|
#include "HIDParser.h"
|
||
|
|
||
15 years ago
|
uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
|
||
|
uint16_t ReportSize,
|
||
|
HID_ReportInfo_t* const ParserData)
|
||
15 years ago
|
{
|
||
|
HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
|
||
|
HID_StateTable_t* CurrStateTable = &StateTable[0];
|
||
|
HID_CollectionPath_t* CurrCollectionPath = NULL;
|
||
14 years ago
|
HID_ReportSizeInfo_t* CurrReportIDInfo = &ParserData->ReportIDSizes[0];
|
||
15 years ago
|
uint16_t UsageList[HID_USAGE_STACK_DEPTH];
|
||
|
uint8_t UsageListSize = 0;
|
||
|
HID_MinMax_t UsageMinMax = {0, 0};
|
||
|
|
||
|
memset(ParserData, 0x00, sizeof(HID_ReportInfo_t));
|
||
|
memset(CurrStateTable, 0x00, sizeof(HID_StateTable_t));
|
||
|
memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
|
||
|
|
||
14 years ago
|
ParserData->TotalDeviceReports = 1;
|
||
15 years ago
|
|
||
|
while (ReportSize)
|
||
|
{
|
||
|
uint8_t HIDReportItem = *ReportData;
|
||
|
uint32_t ReportItemData = 0;
|
||
14 years ago
|
|
||
15 years ago
|
ReportData++;
|
||
|
ReportSize--;
|
||
14 years ago
|
|
||
14 years ago
|
switch (HIDReportItem & HID_RI_DATA_SIZE_MASK)
|
||
15 years ago
|
{
|
||
14 years ago
|
case HID_RI_DATA_BITS_32:
|
||
15 years ago
|
ReportItemData = *((uint32_t*)ReportData);
|
||
|
ReportSize -= 4;
|
||
|
ReportData += 4;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_DATA_BITS_16:
|
||
15 years ago
|
ReportItemData = *((uint16_t*)ReportData);
|
||
|
ReportSize -= 2;
|
||
|
ReportData += 2;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_DATA_BITS_8:
|
||
15 years ago
|
ReportItemData = *((uint8_t*)ReportData);
|
||
|
ReportSize -= 1;
|
||
|
ReportData += 1;
|
||
|
break;
|
||
|
}
|
||
|
|
||
14 years ago
|
switch (HIDReportItem & (HID_RI_TYPE_MASK | HID_RI_TAG_MASK))
|
||
15 years ago
|
{
|
||
14 years ago
|
case HID_RI_PUSH(0):
|
||
15 years ago
|
if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])
|
||
|
return HID_PARSE_HIDStackOverflow;
|
||
14 years ago
|
|
||
15 years ago
|
memcpy((CurrStateTable + 1),
|
||
|
CurrStateTable,
|
||
|
sizeof(HID_ReportItem_t));
|
||
|
|
||
|
CurrStateTable++;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_POP(0):
|
||
15 years ago
|
if (CurrStateTable == &StateTable[0])
|
||
|
return HID_PARSE_HIDStackUnderflow;
|
||
14 years ago
|
|
||
15 years ago
|
CurrStateTable--;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_USAGE_PAGE(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Usage.Page = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_LOGICAL_MINIMUM(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_LOGICAL_MAXIMUM(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_PHYSICAL_MINIMUM(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_PHYSICAL_MAXIMUM(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_UNIT_EXPONENT(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_UNIT(0):
|
||
15 years ago
|
CurrStateTable->Attributes.Unit.Type = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_REPORT_SIZE(0):
|
||
15 years ago
|
CurrStateTable->Attributes.BitSize = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_REPORT_COUNT(0):
|
||
15 years ago
|
CurrStateTable->ReportCount = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_REPORT_ID(0):
|
||
15 years ago
|
CurrStateTable->ReportID = ReportItemData;
|
||
|
|
||
|
if (ParserData->UsingReportIDs)
|
||
|
{
|
||
|
CurrReportIDInfo = NULL;
|
||
|
|
||
|
for (uint8_t i = 0; i < ParserData->TotalDeviceReports; i++)
|
||
|
{
|
||
|
if (ParserData->ReportIDSizes[i].ReportID == CurrStateTable->ReportID)
|
||
|
{
|
||
|
CurrReportIDInfo = &ParserData->ReportIDSizes[i];
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
if (CurrReportIDInfo == NULL)
|
||
|
{
|
||
|
if (ParserData->TotalDeviceReports == HID_MAX_REPORT_IDS)
|
||
|
return HID_PARSE_InsufficientReportIDItems;
|
||
14 years ago
|
|
||
15 years ago
|
CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports++];
|
||
|
memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
ParserData->UsingReportIDs = true;
|
||
14 years ago
|
|
||
15 years ago
|
CurrReportIDInfo->ReportID = CurrStateTable->ReportID;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_USAGE(0):
|
||
15 years ago
|
if (UsageListSize == HID_USAGE_STACK_DEPTH)
|
||
|
return HID_PARSE_UsageListOverflow;
|
||
14 years ago
|
|
||
15 years ago
|
UsageList[UsageListSize++] = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_USAGE_MINIMUM(0):
|
||
15 years ago
|
UsageMinMax.Minimum = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_USAGE_MAXIMUM(0):
|
||
15 years ago
|
UsageMinMax.Maximum = ReportItemData;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_COLLECTION(0):
|
||
15 years ago
|
if (CurrCollectionPath == NULL)
|
||
|
{
|
||
|
CurrCollectionPath = &ParserData->CollectionPaths[0];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
|
||
14 years ago
|
|
||
15 years ago
|
CurrCollectionPath = &ParserData->CollectionPaths[1];
|
||
14 years ago
|
|
||
15 years ago
|
while (CurrCollectionPath->Parent != NULL)
|
||
|
{
|
||
|
if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])
|
||
|
return HID_PARSE_InsufficientCollectionPaths;
|
||
14 years ago
|
|
||
15 years ago
|
CurrCollectionPath++;
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
CurrCollectionPath->Parent = ParentCollectionPath;
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
CurrCollectionPath->Type = ReportItemData;
|
||
|
CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
|
||
14 years ago
|
|
||
15 years ago
|
if (UsageListSize)
|
||
|
{
|
||
|
CurrCollectionPath->Usage.Usage = UsageList[0];
|
||
|
|
||
|
for (uint8_t i = 0; i < UsageListSize; i++)
|
||
|
UsageList[i] = UsageList[i + 1];
|
||
14 years ago
|
|
||
15 years ago
|
UsageListSize--;
|
||
|
}
|
||
|
else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)
|
||
|
{
|
||
|
CurrCollectionPath->Usage.Usage = UsageMinMax.Minimum++;
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
break;
|
||
14 years ago
|
case HID_RI_END_COLLECTION(0):
|
||
15 years ago
|
if (CurrCollectionPath == NULL)
|
||
|
return HID_PARSE_UnexpectedEndCollection;
|
||
14 years ago
|
|
||
15 years ago
|
CurrCollectionPath = CurrCollectionPath->Parent;
|
||
|
break;
|
||
14 years ago
|
case HID_RI_INPUT(0):
|
||
|
case HID_RI_OUTPUT(0):
|
||
|
case HID_RI_FEATURE(0):
|
||
15 years ago
|
for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
|
||
|
{
|
||
|
HID_ReportItem_t NewReportItem;
|
||
14 years ago
|
|
||
15 years ago
|
memcpy(&NewReportItem.Attributes,
|
||
|
&CurrStateTable->Attributes,
|
||
|
sizeof(HID_ReportItem_Attributes_t));
|
||
14 years ago
|
|
||
15 years ago
|
NewReportItem.ItemFlags = ReportItemData;
|
||
|
NewReportItem.CollectionPath = CurrCollectionPath;
|
||
|
NewReportItem.ReportID = CurrStateTable->ReportID;
|
||
14 years ago
|
|
||
15 years ago
|
if (UsageListSize)
|
||
|
{
|
||
|
NewReportItem.Attributes.Usage.Usage = UsageList[0];
|
||
14 years ago
|
|
||
15 years ago
|
for (uint8_t i = 0; i < UsageListSize; i++)
|
||
|
UsageList[i] = UsageList[i + 1];
|
||
14 years ago
|
|
||
15 years ago
|
UsageListSize--;
|
||
|
}
|
||
|
else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)
|
||
|
{
|
||
|
NewReportItem.Attributes.Usage.Usage = UsageMinMax.Minimum++;
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
uint8_t ItemTypeTag = (HIDReportItem & (HID_RI_TYPE_MASK | HID_RI_TAG_MASK));
|
||
14 years ago
|
|
||
14 years ago
|
if (ItemTypeTag == HID_RI_INPUT(0))
|
||
14 years ago
|
NewReportItem.ItemType = HID_REPORT_ITEM_In;
|
||
14 years ago
|
else if (ItemTypeTag == HID_RI_OUTPUT(0))
|
||
14 years ago
|
NewReportItem.ItemType = HID_REPORT_ITEM_Out;
|
||
15 years ago
|
else
|
||
14 years ago
|
NewReportItem.ItemType = HID_REPORT_ITEM_Feature;
|
||
14 years ago
|
|
||
15 years ago
|
NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType];
|
||
14 years ago
|
|
||
15 years ago
|
CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType] += CurrStateTable->Attributes.BitSize;
|
||
14 years ago
|
|
||
15 years ago
|
if (ParserData->LargestReportSizeBits < NewReportItem.BitOffset)
|
||
|
ParserData->LargestReportSizeBits = NewReportItem.BitOffset;
|
||
14 years ago
|
|
||
14 years ago
|
if (!(ReportItemData & HID_IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&NewReportItem))
|
||
14 years ago
|
{
|
||
15 years ago
|
if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
|
||
|
return HID_PARSE_InsufficientReportItems;
|
||
14 years ago
|
|
||
15 years ago
|
memcpy(&ParserData->ReportItems[ParserData->TotalReportItems],
|
||
|
&NewReportItem, sizeof(HID_ReportItem_t));
|
||
14 years ago
|
|
||
15 years ago
|
ParserData->TotalReportItems++;
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
break;
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
if ((HIDReportItem & HID_RI_TYPE_MASK) == HID_RI_TYPE_MAIN)
|
||
15 years ago
|
{
|
||
|
UsageMinMax.Minimum = 0;
|
||
|
UsageMinMax.Maximum = 0;
|
||
14 years ago
|
UsageListSize = 0;
|
||
15 years ago
|
}
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
if (!(ParserData->TotalReportItems))
|
||
|
return HID_PARSE_NoUnfilteredReportItems;
|
||
14 years ago
|
|
||
15 years ago
|
return HID_PARSE_Successful;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool USB_GetHIDReportItemInfo(const uint8_t* ReportData,
|
||
|
HID_ReportItem_t* const ReportItem)
|
||
15 years ago
|
{
|
||
|
uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
|
||
|
uint16_t CurrentBit = ReportItem->BitOffset;
|
||
|
uint32_t BitMask = (1 << 0);
|
||
14 years ago
|
|
||
15 years ago
|
if (ReportItem->ReportID)
|
||
|
{
|
||
|
if (ReportItem->ReportID != ReportData[0])
|
||
|
return false;
|
||
14 years ago
|
|
||
15 years ago
|
ReportData++;
|
||
|
}
|
||
14 years ago
|
|
||
14 years ago
|
ReportItem->PreviousValue = ReportItem->Value;
|
||
|
ReportItem->Value = 0;
|
||
14 years ago
|
|
||
15 years ago
|
while (DataBitsRem--)
|
||
|
{
|
||
|
if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
|
||
|
ReportItem->Value |= BitMask;
|
||
14 years ago
|
|
||
15 years ago
|
CurrentBit++;
|
||
|
BitMask <<= 1;
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
return true;
|
||
|
}
|
||
|
|
||
15 years ago
|
void USB_SetHIDReportItemInfo(uint8_t* ReportData,
|
||
|
HID_ReportItem_t* const ReportItem)
|
||
15 years ago
|
{
|
||
|
uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
|
||
|
uint16_t CurrentBit = ReportItem->BitOffset;
|
||
|
uint32_t BitMask = (1 << 0);
|
||
14 years ago
|
|
||
15 years ago
|
if (ReportItem->ReportID)
|
||
|
{
|
||
|
ReportData[0] = ReportItem->ReportID;
|
||
|
ReportData++;
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
ReportItem->PreviousValue = ReportItem->Value;
|
||
14 years ago
|
|
||
15 years ago
|
while (DataBitsRem--)
|
||
|
{
|
||
|
if (ReportItem->Value & (1 << (CurrentBit % 8)))
|
||
|
ReportData[CurrentBit / 8] |= BitMask;
|
||
14 years ago
|
|
||
15 years ago
|
CurrentBit++;
|
||
|
BitMask <<= 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData,
|
||
|
const uint8_t ReportID,
|
||
|
const uint8_t ReportType)
|
||
15 years ago
|
{
|
||
|
for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)
|
||
|
{
|
||
|
uint16_t ReportSizeBits = ParserData->ReportIDSizes[i].ReportSizeBits[ReportType];
|
||
14 years ago
|
|
||
15 years ago
|
if (ParserData->ReportIDSizes[i].ReportID == ReportID)
|
||
|
return ((ReportSizeBits >> 3) + ((ReportSizeBits & 0x07) ? 1 : 0));
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|