|
|
@ -14,32 +14,222 @@
|
|
|
|
#include "downstream_hid.h"
|
|
|
|
#include "downstream_hid.h"
|
|
|
|
#include "downstream_statemachine.h"
|
|
|
|
#include "downstream_statemachine.h"
|
|
|
|
#include "usbh_hid.h"
|
|
|
|
#include "usbh_hid.h"
|
|
|
|
|
|
|
|
#include "stm32f4xx_hal.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define HID_MAX_REPORT_LEN 8
|
|
|
|
#define HID_MAX_REPORT_LEN 8
|
|
|
|
#define HID_MOUSE_DATA_LEN 3
|
|
|
|
#define HID_MOUSE_DATA_LEN 4
|
|
|
|
#define HID_KEYBOARD_DATA_LEN 0
|
|
|
|
#define HID_KEYBOARD_DATA_LEN 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define HID_MOUSE_MAX_BUTTONS 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DownstreamPacketTypeDef* SavedPacket;
|
|
|
|
|
|
|
|
|
|
|
|
extern USBH_HandleTypeDef hUsbHostFS; //Hard-link ourselves to usb_host.c
|
|
|
|
extern USBH_HandleTypeDef hUsbHostFS; //Hard-link ourselves to usb_host.c
|
|
|
|
extern InterfaceCommandClassTypeDef ConfiguredDeviceClass; //Do a cheap hard-link to downstream_statemachine.c, rather than keep a duplicate here
|
|
|
|
extern InterfaceCommandClassTypeDef ConfiguredDeviceClass; //Do a cheap hard-link to downstream_statemachine.c, rather than keep a duplicate here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Information required to extract the data we need from incoming device reports.
|
|
|
|
|
|
|
|
uint8_t ReportButtonBitOffset;
|
|
|
|
|
|
|
|
uint8_t ReportButtonBitLength;
|
|
|
|
|
|
|
|
uint8_t ReportXBitOffset;
|
|
|
|
|
|
|
|
uint8_t ReportXBitLength;
|
|
|
|
|
|
|
|
uint8_t ReportYBitOffset;
|
|
|
|
|
|
|
|
uint8_t ReportYBitLength;
|
|
|
|
|
|
|
|
uint8_t ReportWheelBitOffset;
|
|
|
|
|
|
|
|
uint8_t ReportWheelBitLength;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Stuff used while parsing HID report
|
|
|
|
|
|
|
|
uint8_t* ReportDataPointer;
|
|
|
|
|
|
|
|
uint8_t ItemHeader;
|
|
|
|
|
|
|
|
uint8_t ItemData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static HAL_StatusTypeDef Downstream_HID_Mouse_ParseReportDescriptor(void);
|
|
|
|
|
|
|
|
static HAL_StatusTypeDef Downstream_HID_GetNextReportItem(void);
|
|
|
|
|
|
|
|
static void Downstream_HID_Mouse_ExtractDataFromReport(uint8_t* reportBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InterfaceCommandClassTypeDef Downstream_HID_ApproveConnectedDevice(void)
|
|
|
|
InterfaceCommandClassTypeDef Downstream_HID_ApproveConnectedDevice(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
HID_HandleTypeDef* HID_Handle = (HID_HandleTypeDef*)hUsbHostFS.pActiveClass->pData;
|
|
|
|
HID_HandleTypeDef* HID_Handle = (HID_HandleTypeDef*)hUsbHostFS.pActiveClass->pData;
|
|
|
|
|
|
|
|
|
|
|
|
if (HID_Handle->Protocol == HID_MOUSE_BOOT_CODE)
|
|
|
|
if (HID_Handle->Protocol != HID_MOUSE_BOOT_CODE)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if ((HID_Handle->length >= HID_MOUSE_DATA_LEN) && (HID_Handle->length <= HID_MAX_REPORT_LEN))
|
|
|
|
return COMMAND_CLASS_INTERFACE; //fail
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Downstream_HID_Mouse_ParseReportDescriptor() != HAL_OK)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return COMMAND_CLASS_INTERFACE; //fail
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return COMMAND_CLASS_HID_MOUSE; //success!
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HAL_StatusTypeDef Downstream_HID_Mouse_ParseReportDescriptor(void)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
uint32_t currentReportBitIndex = 0;
|
|
|
|
|
|
|
|
uint8_t currentUsagePage = 0;
|
|
|
|
|
|
|
|
uint8_t currentUsageIndex = 0;
|
|
|
|
|
|
|
|
uint8_t xUsageIndex = 0xFF;
|
|
|
|
|
|
|
|
uint8_t yUsageIndex = 0xFF;
|
|
|
|
|
|
|
|
uint8_t wheelUsageIndex = 0xFF;
|
|
|
|
|
|
|
|
uint8_t currentReportSize = 0;
|
|
|
|
|
|
|
|
uint8_t currentReportCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReportDataPointer = hUsbHostFS.device.Data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReportButtonBitLength = 0;
|
|
|
|
|
|
|
|
ReportXBitLength = 0;
|
|
|
|
|
|
|
|
ReportYBitLength = 0;
|
|
|
|
|
|
|
|
ReportWheelBitLength = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while ((Downstream_HID_GetNextReportItem() == HAL_OK) && ((ReportButtonBitLength == 0) ||
|
|
|
|
|
|
|
|
(ReportXBitLength == 0) ||
|
|
|
|
|
|
|
|
(ReportYBitLength == 0) ||
|
|
|
|
|
|
|
|
(ReportWheelBitLength == 0)))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (ItemHeader)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return COMMAND_CLASS_HID_MOUSE;
|
|
|
|
case HID_ITEM_USAGE_PAGE:
|
|
|
|
|
|
|
|
currentUsagePage = ItemData;
|
|
|
|
|
|
|
|
currentUsageIndex = 0;
|
|
|
|
|
|
|
|
xUsageIndex = 0xFF;
|
|
|
|
|
|
|
|
yUsageIndex = 0xFF;
|
|
|
|
|
|
|
|
wheelUsageIndex = 0xFF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_USAGE:
|
|
|
|
|
|
|
|
switch (ItemData)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case HID_ITEM_USAGE_X:
|
|
|
|
|
|
|
|
xUsageIndex = currentUsageIndex;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_USAGE_Y:
|
|
|
|
|
|
|
|
yUsageIndex = currentUsageIndex;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_USAGE_WHEEL:
|
|
|
|
|
|
|
|
wheelUsageIndex = currentUsageIndex;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
currentUsageIndex++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_REPORT_SIZE:
|
|
|
|
|
|
|
|
currentReportSize = ItemData;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_REPORT_COUNT:
|
|
|
|
|
|
|
|
currentReportCount = ItemData;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_INPUT:
|
|
|
|
|
|
|
|
switch (currentUsagePage)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
case HID_ITEM_USAGE_PAGE_BUTTON:
|
|
|
|
|
|
|
|
if (ItemData == HID_ITEM_INPUT_ABS)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//Buttons found!
|
|
|
|
|
|
|
|
if (currentReportSize != 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return HAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportButtonBitOffset = currentReportBitIndex;
|
|
|
|
|
|
|
|
ReportButtonBitLength = currentReportCount;
|
|
|
|
|
|
|
|
if (ReportButtonBitLength > HID_MOUSE_MAX_BUTTONS)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ReportButtonBitLength = HID_MOUSE_MAX_BUTTONS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case HID_ITEM_USAGE_PAGE_DESKTOP:
|
|
|
|
|
|
|
|
if (ItemData == HID_ITEM_INPUT_REL)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//Movement data found!
|
|
|
|
|
|
|
|
if ((currentReportSize < 8) || (currentReportSize > 16))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return HAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xUsageIndex != 0xFF)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ReportXBitOffset = currentReportBitIndex + (currentReportSize * xUsageIndex);
|
|
|
|
|
|
|
|
ReportXBitLength = currentReportSize;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (yUsageIndex != 0xFF)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ReportYBitOffset = currentReportBitIndex + (currentReportSize * yUsageIndex);
|
|
|
|
|
|
|
|
ReportYBitLength = currentReportSize;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xUsageIndex != 0xFF)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ReportWheelBitOffset = currentReportBitIndex + (currentReportSize * wheelUsageIndex);
|
|
|
|
|
|
|
|
ReportWheelBitLength = currentReportSize;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
currentReportBitIndex += (currentReportSize * currentReportCount);
|
|
|
|
|
|
|
|
if (currentReportBitIndex < (HID_MAX_REPORT_LEN * 8))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return HAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return COMMAND_CLASS_INTERFACE; //fail
|
|
|
|
//We don't mind if we didn't find a scrollwheel item.
|
|
|
|
|
|
|
|
if ((ReportButtonBitLength == 0) ||
|
|
|
|
|
|
|
|
(ReportXBitLength == 0) ||
|
|
|
|
|
|
|
|
(ReportYBitLength == 0))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return HAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return HAL_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Retrieves the next item in the HID report, and at most one of its associated data bytes.
|
|
|
|
|
|
|
|
//Then it updates ReportDataPointer based on the actual length of the retrieved item.
|
|
|
|
|
|
|
|
HAL_StatusTypeDef Downstream_HID_GetNextReportItem(void)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
HID_HandleTypeDef* HID_Handle = (HID_HandleTypeDef*)hUsbHostFS.pActiveClass->pData;
|
|
|
|
|
|
|
|
uint32_t itemLength;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((ReportDataPointer >= &hUsbHostFS.device.Data[USBH_MAX_DATA_BUFFER]) ||
|
|
|
|
|
|
|
|
((ReportDataPointer - &hUsbHostFS.device.Data[0]) >= HID_Handle->HID_Desc.wItemLength))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return HAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ItemHeader = *ReportDataPointer++;
|
|
|
|
|
|
|
|
itemLength = ItemHeader & HID_ITEM_LENGTH_MASK;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ItemHeader == HID_ITEM_LONG)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
itemLength += *ReportDataPointer; //Long items have another length byte
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (itemLength > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ItemData = *ReportDataPointer; //If it is a short item, grab its first data byte
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ReportDataPointer += itemLength;
|
|
|
|
|
|
|
|
return HAL_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -52,9 +242,10 @@ void Downstream_HID_PacketProcessor(DownstreamPacketTypeDef* receivedPacket)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SavedPacket = receivedPacket;
|
|
|
|
|
|
|
|
|
|
|
|
if (USBH_HID_GetInterruptReport(&hUsbHostFS,
|
|
|
|
if (USBH_HID_GetInterruptReport(&hUsbHostFS,
|
|
|
|
Downstream_HID_InterruptReportCallback,
|
|
|
|
Downstream_HID_InterruptReportCallback) != HAL_OK)
|
|
|
|
receivedPacket) != HAL_OK) //Don't free the packet, USBH_HID will use it then return it to InterruptReportCallback below
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Downstream_PacketProcessor_FreakOut();
|
|
|
|
Downstream_PacketProcessor_FreakOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -62,11 +253,12 @@ void Downstream_HID_PacketProcessor(DownstreamPacketTypeDef* receivedPacket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Downstream_HID_InterruptReportCallback(DownstreamPacketTypeDef* packetToSend)
|
|
|
|
void Downstream_HID_InterruptReportCallback(uint8_t* reportBuffer)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (ConfiguredDeviceClass == COMMAND_CLASS_HID_MOUSE)
|
|
|
|
if (ConfiguredDeviceClass == COMMAND_CLASS_HID_MOUSE)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
packetToSend->Length16 = ((HID_MOUSE_DATA_LEN + 1) / 2) + DOWNSTREAM_PACKET_HEADER_LEN_16;
|
|
|
|
Downstream_HID_Mouse_ExtractDataFromReport(reportBuffer);
|
|
|
|
|
|
|
|
SavedPacket->Length16 = ((HID_MOUSE_DATA_LEN + 1) / 2) + DOWNSTREAM_PACKET_HEADER_LEN_16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//else if...
|
|
|
|
//else if...
|
|
|
|
else
|
|
|
|
else
|
|
|
@ -75,7 +267,35 @@ void Downstream_HID_InterruptReportCallback(DownstreamPacketTypeDef* packetToSen
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Downstream_PacketProcessor_ClassReply(packetToSend);
|
|
|
|
Downstream_PacketProcessor_ClassReply(SavedPacket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Downstream_HID_Mouse_ExtractDataFromReport(uint8_t* reportBuffer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
uint16_t readData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Grab the buttons
|
|
|
|
|
|
|
|
readData = *(uint16_t*)&(reportBuffer[(ReportButtonBitOffset / 8)]);
|
|
|
|
|
|
|
|
readData >>= (ReportButtonBitOffset % 8);
|
|
|
|
|
|
|
|
readData &= ((1 << ReportButtonBitLength) - 1);
|
|
|
|
|
|
|
|
SavedPacket->Data[0] = readData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Grab X
|
|
|
|
|
|
|
|
readData = *(uint16_t*)&(reportBuffer[(ReportXBitOffset / 8)]);
|
|
|
|
|
|
|
|
readData >>= (ReportXBitOffset % 8);
|
|
|
|
|
|
|
|
SavedPacket->Data[1] = (uint8_t)readData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Grab Y
|
|
|
|
|
|
|
|
readData = *(uint16_t*)&(reportBuffer[(ReportYBitOffset / 8)]);
|
|
|
|
|
|
|
|
readData >>= (ReportYBitOffset % 8);
|
|
|
|
|
|
|
|
SavedPacket->Data[2] = (uint8_t)readData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Grab wheel
|
|
|
|
|
|
|
|
readData = *(uint16_t*)&(reportBuffer[(ReportWheelBitOffset / 8)]);
|
|
|
|
|
|
|
|
readData >>= (ReportWheelBitOffset % 8);
|
|
|
|
|
|
|
|
SavedPacket->Data[3] = (uint8_t)readData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|