From 51566d1a811f43dc39f38cb597de44ba9363d974 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Sun, 20 Sep 2009 12:01:25 +0000 Subject: Added new Pipe_IsFrozen() macro to determine if the currently selected pipe is frozen. Added new USB_GetHIDReportSize() function to the HID report parser to retrieve the size of a given report by its ID. More additions to the unfinished HID Host Class Driver. --- LUFA/Drivers/USB/Class/Host/HID.c | 101 +++++++++++++++++++++++++++++++- LUFA/Drivers/USB/Class/Host/HID.h | 43 ++++++++++++-- LUFA/Drivers/USB/Class/Host/HIDParser.c | 21 +++++++ LUFA/Drivers/USB/Class/Host/HIDParser.h | 12 ++++ 4 files changed, 169 insertions(+), 8 deletions(-) (limited to 'LUFA/Drivers/USB/Class/Host') diff --git a/LUFA/Drivers/USB/Class/Host/HID.c b/LUFA/Drivers/USB/Class/Host/HID.c index 9b1ec161d..22b2de1cb 100644 --- a/LUFA/Drivers/USB/Class/Host/HID.c +++ b/LUFA/Drivers/USB/Class/Host/HID.c @@ -61,7 +61,7 @@ uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint (CurrentHIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol)); HIDInterfaceInfo->State.InterfaceNumber = CurrentHIDInterface->InterfaceNumber; - HIDInterfaceInfo->State.SupportsBootSubClass = (CurrentHIDInterface->SubClass != 0); + HIDInterfaceInfo->State.SupportsBootProtocol = (CurrentHIDInterface->SubClass != HID_NON_BOOT_PROTOCOL); if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found) { @@ -97,6 +97,8 @@ uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE); HIDInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize; + HIDInterfaceInfo->State.DeviceUsesOUTPipe = true; + FoundEndpoints |= HID_FOUND_DATAPIPE_OUT; } } @@ -152,6 +154,97 @@ void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) } +uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, bool ControlRequest, uint8_t* ReportID, void* Buffer) +{ + if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive)) + return false; + + if (ControlRequest) + { + USB_ControlRequest = (USB_Request_Header_t) + { + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_SetReport, + .wValue = *ReportID, + .wIndex = HIDInterfaceInfo->State.InterfaceNumber, + .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, *ReportID, REPORT_ITEM_TYPE_In), + }; + + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + return USB_Host_SendControlRequest(Buffer); + } + else + { + uint8_t ErrorCode; + + Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber); + Pipe_Unfreeze(); + + uint16_t ReportSize; + + if (HIDInterfaceInfo->State.UsingBootProtocol) + { + ReportSize = Pipe_BytesInPipe(); + } + else + { + if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs) + *ReportID = Pipe_Read_Byte(); + else + *ReportID = 0; + + ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, *ReportID, REPORT_ITEM_TYPE_In); + } + + if ((ErrorCode = Pipe_Read_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError) + return ErrorCode; + + Pipe_Freeze(); + + return PIPE_RWSTREAM_NoError; + } +} + +uint8_t HID_Host_SendReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint8_t ReportID, void* Buffer, uint16_t ReportSize) +{ + if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive)) + return false; + + if (HIDInterfaceInfo->State.DeviceUsesOUTPipe) + { + USB_ControlRequest = (USB_Request_Header_t) + { + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_SetReport, + .wValue = ReportID, + .wIndex = HIDInterfaceInfo->State.InterfaceNumber, + .wLength = ReportSize, + }; + + Pipe_SelectPipe(PIPE_CONTROLPIPE); + + return USB_Host_SendControlRequest(Buffer); + } + else + { + uint8_t ErrorCode; + + Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber); + Pipe_Unfreeze(); + + if (ReportID) + Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK); + + if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError) + return ErrorCode; + + Pipe_Freeze(); + + return PIPE_RWSTREAM_NoError; + } +} + bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) { if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive)) @@ -165,7 +258,7 @@ bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) ReportReceived = Pipe_IsReadWriteAllowed(); Pipe_Freeze(); - + return ReportReceived; } @@ -174,6 +267,8 @@ uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive)) return false; + uint8_t ErrorCode; + USB_ControlRequest = (USB_Request_Header_t) { .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), @@ -188,7 +283,7 @@ uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) if (!(HIDInterfaceInfo->State.SupportsBootProtocol)) return HID_ERROR_LOGICAL; - if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful) + if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful) return ErrorCode; HIDInterfaceInfo->State.UsingBootProtocol = true; diff --git a/LUFA/Drivers/USB/Class/Host/HID.h b/LUFA/Drivers/USB/Class/Host/HID.h index 95c133419..230930a5c 100644 --- a/LUFA/Drivers/USB/Class/Host/HID.h +++ b/LUFA/Drivers/USB/Class/Host/HID.h @@ -73,8 +73,9 @@ uint8_t DataOUTPipeNumber; /**< Pipe number of the HID interface's OUT data pipe */ uint8_t HIDInterfaceProtocol; /**< HID interface protocol value to match against if a specific - * boot subclass protocol is required (e.g. keyboard, mouse), or - * leave as 0 to match against the first HID interface found + * boot subclass protocol is required, either \ref HID_BOOT_MOUSE_PROTOCOL, + * \ref HID_BOOT_KEYBOARD_PROTOCOL or \ref HID_NON_BOOT_PROTOCOL if any + * HID device should be enumerated by the interface */ HID_ReportInfo_t* HIDParserData; /**< HID parser data to store the parsed HID report data, when boot protocol * is not used */ @@ -95,6 +96,9 @@ bool SupportsBootProtocol; /**< Indicates if the current interface instance supports the HID Boot * Protocol when enabled via \ref USB_HID_Host_SetBootProtocol() */ + bool DeviceUsesOUTPipe; /**< Indicates if the current interface instance uses a seperate OUT data pipe for + * OUT reports, or if OUT reports are sent via the control pipe instead. + */ bool UsingBootProtocol; /**< Indicates that the interface is currently initialised in Boot Protocol mode */ uint16_t HIDReportSize; /**< Size in bytes of the HID report descriptor in the device */ } State; /**< State data for the USB class interface within the device. All elements in this section @@ -141,14 +145,43 @@ uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorLength, uint8_t* DeviceConfigDescriptor) ATTR_NON_NULL_PTR_ARG(1, 3); - /** Determines if a report has been received on the HID interface's IN report pipe, when the device is initialized - * into Report Protocol mode. + + /** Receives a HID IN report from the attached HID device, either the next report from the device's IN data pipe, + * or a given report (by Report ID) if a specific report is desired. + * + * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state + * \param[in] ControlRequest Set to true if the report should be requested by a control request, false otherwise + * \param[in,out] ReportID Report ID of the received report if ControlRequest is false, set by the to the Report ID + * to fetch if ControlRequest is true + * \param[in] Buffer Buffer to store the received report into + * + * \return An error code from the \ref USB_Host_SendControlErrorCodes_t enum if the ControlRequest flag is set, + * a value from the \ref Pipe_Stream_RW_ErrorCodes_t enum otherwise + */ + uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, bool ControlRequest, uint8_t* ReportID, + void* Buffer) ATTR_NON_NULL_PTR_ARG(1, 3); + + /** Sends an OUT report to the currently attached HID device, using the device's OUT pipe if available or the device's + * Control pipe if not. + * + * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state + * \param[in] ReportID Report ID of the report to send to the device, or 0 if the device does not use report IDs + * \param[in] Buffer Buffer containing the report to send to the attached device + * \param[in] ReportSize Report size in bytes to send to the attached device + * + * \return An error code from the \ref USB_Host_SendControlErrorCodes_t enum if the DeviceUsesOUTPipe flag is set in + * the interface's state structure, a value from the \ref Pipe_Stream_RW_ErrorCodes_t enum otherwise + */ + uint8_t HID_Host_SendReport(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint8_t ReportID, + void* Buffer, uint16_t ReportSize) ATTR_NON_NULL_PTR_ARG(1, 3); + + /** Determines if a HID IN report has been received from the attached device on the data IN pipe. * * \param[in,out] HIDInterfaceInfo Pointer to a structure containing a HID Class host configuration and state * * \return Boolean true if a report has been received, false otherwise */ - bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); + bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); /** Switches the attached HID device's reporting protocol over to the Boot Report protocol mode, on supported devices. * diff --git a/LUFA/Drivers/USB/Class/Host/HIDParser.c b/LUFA/Drivers/USB/Class/Host/HIDParser.c index 4293a3d98..008d173f0 100644 --- a/LUFA/Drivers/USB/Class/Host/HIDParser.c +++ b/LUFA/Drivers/USB/Class/Host/HIDParser.c @@ -342,4 +342,25 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* Repor } } +uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, uint8_t ReportID, uint8_t ReportType) +{ + for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++) + { + if (ParserData->ReportIDSizes[i].ReportID == ReportID) + { + switch (ReportType) + { + case REPORT_ITEM_TYPE_In: + return ParserData->ReportIDSizes[i].BitsIn; + case REPORT_ITEM_TYPE_Out: + return ParserData->ReportIDSizes[i].BitsOut; + case REPORT_ITEM_TYPE_Feature: + return ParserData->ReportIDSizes[i].BitsFeature; + } + } + } + + return 0; +} + #endif diff --git a/LUFA/Drivers/USB/Class/Host/HIDParser.h b/LUFA/Drivers/USB/Class/Host/HIDParser.h index d9adae34d..f598789df 100644 --- a/LUFA/Drivers/USB/Class/Host/HIDParser.h +++ b/LUFA/Drivers/USB/Class/Host/HIDParser.h @@ -269,6 +269,18 @@ void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem) ATTR_NON_NULL_PTR_ARG(1, 2); + /** Retrieves the size of a given HID report in bytes from it's Report ID. + * + * \param[in] ParserData Pointer to a \ref HID_ReportInfo_t instance containing the parser output + * \param[in] ReportID Report ID of the report whose size is to be retrieved + * \param[in] ReportType Type of the report whose size is to be determined, a valued from the + * \ref HID_ReportItemTypes_t enum + * + * \return Size of the report in bytes, or 0 if the report does not exist + */ + uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, uint8_t ReportID, + uint8_t ReportType) ATTR_NON_NULL_PTR_ARG(1); + /** Callback routine for the HID Report Parser. This callback must be implemented by the user code when * the parser is used, to determine what report IN, OUT and FEATURE item's information is stored into the user * HID_ReportInfo_t structure. This can be used to filter only those items the application will be using, so that -- cgit v1.2.3