X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/864196c884ef3602aee44f1a68aceb5819a07f21..03ee87b35abdb8b92e8b55ec040fa943f9a6786c:/LUFA/Drivers/USB/Class/Device/HID.c diff --git a/LUFA/Drivers/USB/Class/Device/HID.c b/LUFA/Drivers/USB/Class/Device/HID.c index 43f11ee7c..a5c03f726 100644 --- a/LUFA/Drivers/USB/Class/Device/HID.c +++ b/LUFA/Drivers/USB/Class/Device/HID.c @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2009. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ /* - Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - 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 + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + 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 software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -33,16 +33,13 @@ #include "HID.h" -void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) +void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) { if (!(Endpoint_IsSETUPReceived())) return; - if ((USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber) && - (USB_ControlRequest.bRequest != REQ_SetIdle)) - { - return; - } + if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber) + return; switch (USB_ControlRequest.bRequest) { @@ -51,15 +48,17 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf { Endpoint_ClearSETUP(); - uint8_t ReportINData[HIDInterfaceInfo->Config.ReportINBufferSize]; - uint16_t ReportINSize; - uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF); + uint16_t ReportINSize = 0; + uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF); + uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1; - memset(ReportINData, 0, sizeof(ReportINData)); + memset(HIDInterfaceInfo->Config.PrevReportINBuffer, 0, HIDInterfaceInfo->Config.PrevReportINBufferSize); - ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData); + CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType, + HIDInterfaceInfo->Config.PrevReportINBuffer, &ReportINSize); - Endpoint_Write_Control_Stream_LE(ReportINData, ReportINSize); + Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP); + Endpoint_Write_Control_Stream_LE(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize); Endpoint_ClearOUT(); } @@ -97,7 +96,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf { Endpoint_ClearSETUP(); - HIDInterfaceInfo->State.UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000); + HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00); Endpoint_ClearStatusStage(); } @@ -106,15 +105,11 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf case REQ_SetIdle: if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { - if ((USB_ControlRequest.wIndex == HIDInterfaceInfo->Config.InterfaceNumber) || - (USB_ControlRequest.wValue & 0xFF) == 0) - { - Endpoint_ClearSETUP(); + Endpoint_ClearSETUP(); - HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6); + HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6); - Endpoint_ClearStatusStage(); - } + Endpoint_ClearStatusStage(); } break; @@ -136,11 +131,12 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) { memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State)); - HIDInterfaceInfo->State.IdleCount = 500; HIDInterfaceInfo->State.UsingReportProtocol = true; + HIDInterfaceInfo->State.IdleCount = 500; if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize, ENDPOINT_BANK_SINGLE))) + ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize, + HIDInterfaceInfo->Config.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE))) { return false; } @@ -155,29 +151,38 @@ void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber); - if (Endpoint_IsReadWriteAllowed() && - !(HIDInterfaceInfo->State.IdleCount && HIDInterfaceInfo->State.IdleMSRemaining)) + if (Endpoint_IsReadWriteAllowed()) { - if (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining)) - HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount; - - uint8_t ReportINData[HIDInterfaceInfo->Config.ReportINBufferSize]; - uint16_t ReportINSize; - uint8_t ReportID = 0; + uint8_t ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize]; + uint8_t ReportID = 0; + uint16_t ReportINSize = 0; memset(ReportINData, 0, sizeof(ReportINData)); - ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData); + bool ForceSend = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, REPORT_ITEM_TYPE_In, + ReportINData, &ReportINSize); + bool StatesChanged = false; + bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining)); + + if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL) + { + StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0); + memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, ReportINSize); + } - if (ReportINSize) + if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed)) { + HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount; + + Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber); + if (ReportID) - Endpoint_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK); + Endpoint_Write_Byte(ReportID); Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK); + + Endpoint_ClearIN(); } - - Endpoint_ClearIN(); } }