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/Common/HID.h | 47 ++++++++++++--- LUFA/Drivers/USB/Class/Device/HID.h | 4 +- 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 ++++ LUFA/Drivers/USB/LowLevel/Pipe.c | 2 +- LUFA/Drivers/USB/LowLevel/Pipe.h | 8 +++ 8 files changed, 219 insertions(+), 19 deletions(-) (limited to 'LUFA/Drivers') diff --git a/LUFA/Drivers/USB/Class/Common/HID.h b/LUFA/Drivers/USB/Class/Common/HID.h index b860f0852..f6b49dfce 100644 --- a/LUFA/Drivers/USB/Class/Common/HID.h +++ b/LUFA/Drivers/USB/Class/Common/HID.h @@ -48,29 +48,44 @@ /* Macros: */ /** HID Class Specific Request to get the current HID report from the device. */ - #define REQ_GetReport 0x01 + #define REQ_GetReport 0x01 /** HID Class Specific Request to get the current device idle count. */ - #define REQ_GetIdle 0x02 + #define REQ_GetIdle 0x02 /** HID Class Specific Request to set the current HID report to the device. */ - #define REQ_SetReport 0x09 + #define REQ_SetReport 0x09 /** HID Class Specific Request to set the device's idle count. */ - #define REQ_SetIdle 0x0A + #define REQ_SetIdle 0x0A /** HID Class Specific Request to get the current HID report protocol mode. */ - #define REQ_GetProtocol 0x03 + #define REQ_GetProtocol 0x03 /** HID Class Specific Request to set the current HID report protocol mode. */ - #define REQ_SetProtocol 0x0B + #define REQ_SetProtocol 0x0B /** Descriptor header type value, to indicate a HID class HID descriptor. */ - #define DTYPE_HID 0x21 + #define DTYPE_HID 0x21 /** Descriptor header type value, to indicate a HID class HID report descriptor. */ - #define DTYPE_Report 0x22 + #define DTYPE_Report 0x22 + /** Constant for the protocol value of a HID interface descriptor, indicating that the interface does not support + * any HID class boot protocol (see HID Class Specification). + */ + #define HID_NON_BOOT_PROTOCOL 0x00 + + /** Constant for the protocol value of a HID interface descriptor, indicating that the interface supports the + * HID class Mouse boot protocol (see HID Class Specification). + */ + #define HID_BOOT_MOUSE_PROTOCOL 0x02 + + /** Constant for the protocol value of a HID interface descriptor, indicating that the interface supports the + * HID class Keyboard boot protocol (see HID Class Specification). + */ + #define HID_BOOT_KEYBOARD_PROTOCOL 0x01 + /* Type Defines: */ /** Type define for the HID class specific HID descriptor, to describe the HID device's specifications. Refer to the HID * specification for details on the structure elements. @@ -88,6 +103,22 @@ uint16_t HIDReportLength; } USB_HID_Descriptor_t; + /** Type define for a standard Boot Protocol Mouse report */ + typedef struct + { + uint8_t Button; /**< Button mask for currently pressed buttons in the mouse */ + int8_t X; /**< Current delta X movement of the mouse */ + int8_t Y; /**< Current delta Y movement on the mouse */ + } USB_MouseReport_Data_t; + + /** Type define for a standard Boot Protocol Keyboard report */ + typedef struct + { + uint8_t Modifier; /**< Keyboard modifier byte, indicating pressed modifier keys (such as Shift, Control, etc.) */ + uint8_t Reserved; /**< Reserved for OEM use, always set to 0 */ + uint8_t KeyCode; /**< Key code of the currently pressed key */ + } USB_KeyboardReport_Data_t; + /** Type define for the data type used to store HID report descriptor elements. */ typedef uint8_t USB_Descriptor_HIDReport_Datatype_t; diff --git a/LUFA/Drivers/USB/Class/Device/HID.h b/LUFA/Drivers/USB/Class/Device/HID.h index faf9d5f72..a3e3b8584 100644 --- a/LUFA/Drivers/USB/Class/Device/HID.h +++ b/LUFA/Drivers/USB/Class/Device/HID.h @@ -131,7 +131,7 @@ */ void HID_Device_MillisecondElapsed(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo) ATTR_NON_NULL_PTR_ARG(1); - /** HID class driver callback for the user creation of a HID input report. This callback may fire in response to either + /** HID class driver callback for the user creation of a HID IN report. This callback may fire in response to either * HID class control requests from the host, or by the normal HID endpoint polling procedure. Inside this callback the * user is responsible for the creation of the next HID input report to be sent to the host. * @@ -148,7 +148,7 @@ bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, void* ReportData, uint16_t* ReportSize) ATTR_NON_NULL_PTR_ARG(1, 2, 3, 4); - /** HID class driver callback for the user processing of a received HID input report. This callback may fire in response to + /** HID class driver callback for the user processing of a received HID OUT report. This callback may fire in response to * either HID class control requests from the host, or by the normal HID endpoint polling procedure. Inside this callback * the user is responsible for the processing of the received HID output report from the 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 diff --git a/LUFA/Drivers/USB/LowLevel/Pipe.c b/LUFA/Drivers/USB/LowLevel/Pipe.c index 9c6a53e78..58d7343b0 100644 --- a/LUFA/Drivers/USB/LowLevel/Pipe.c +++ b/LUFA/Drivers/USB/LowLevel/Pipe.c @@ -93,7 +93,7 @@ uint8_t Pipe_WaitUntilReady(void) #else uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS; #endif - + for (;;) { if (Pipe_GetPipeToken() == PIPE_TOKEN_IN) diff --git a/LUFA/Drivers/USB/LowLevel/Pipe.h b/LUFA/Drivers/USB/LowLevel/Pipe.h index 294545804..fd01b5c58 100644 --- a/LUFA/Drivers/USB/LowLevel/Pipe.h +++ b/LUFA/Drivers/USB/LowLevel/Pipe.h @@ -292,6 +292,12 @@ /** Freezes the selected pipe, preventing it from communicating with an attached device. */ static inline void Pipe_Freeze(void); + /** Determines if the currently selected pipe is frozen, and not able to accept data. + * + * \return Boolean true if the currently selected pipe is frozen, false otherwise + */ + static inline bool Pipe_IsFrozen(void); + /** Clears the master pipe error flag. */ static inline void Pipe_ClearError(void); @@ -445,6 +451,8 @@ #define Pipe_Unfreeze() MACROS{ UPCONX &= ~(1 << PFREEZE); }MACROE #define Pipe_Freeze() MACROS{ UPCONX |= (1 << PFREEZE); }MACROE + + #define Pipe_IsFrozen() ((UPCONX & (1 << PFREEZE)) ? true : false) #define Pipe_ClearError() MACROS{ UPINTX &= ~(1 << PERRI); }MACROE -- cgit v1.2.3