X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/7c5444b89a49df7cb671b0b041567990d2a3012e..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 5f8ccfbae..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 @@ -28,14 +28,17 @@ this software. */ +#include "../../HighLevel/USBMode.h" +#if defined(USB_CAN_BE_DEVICE) + #include "HID.h" -void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) +void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) { if (!(Endpoint_IsSETUPReceived())) return; - if (USB_ControlRequest.wIndex != HIDInterfaceInfo->InterfaceNumber) + if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber) return; switch (USB_ControlRequest.bRequest) @@ -45,14 +48,17 @@ void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) { Endpoint_ClearSETUP(); - uint8_t ReportINData[HIDInterfaceInfo->ReportINBufferSize]; - uint16_t ReportINSize; - - memset(ReportINData, 0, sizeof(ReportINData)); + uint16_t ReportINSize = 0; + uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF); + uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1; - ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, ReportINData); + memset(HIDInterfaceInfo->Config.PrevReportINBuffer, 0, HIDInterfaceInfo->Config.PrevReportINBufferSize); + + 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(); } @@ -64,11 +70,12 @@ void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) uint16_t ReportOUTSize = USB_ControlRequest.wLength; uint8_t ReportOUTData[ReportOUTSize]; + uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF); Endpoint_Read_Control_Stream_LE(ReportOUTData, ReportOUTSize); Endpoint_ClearIN(); - CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo, ReportOUTData, ReportOUTSize); + CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportOUTData, ReportOUTSize); } break; @@ -77,11 +84,10 @@ void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) { Endpoint_ClearSETUP(); - Endpoint_Write_Byte(HIDInterfaceInfo->UsingReportProtocol); + Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol); Endpoint_ClearIN(); - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; @@ -90,10 +96,9 @@ void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) { Endpoint_ClearSETUP(); - HIDInterfaceInfo->UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000); + HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00); - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -101,11 +106,10 @@ void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); - - HIDInterfaceInfo->IdleCount = ((USB_ControlRequest.wValue >> 8) << 2); - - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + + HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6); + + Endpoint_ClearStatusStage(); } break; @@ -114,80 +118,72 @@ void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo) { Endpoint_ClearSETUP(); - Endpoint_Write_Byte(HIDInterfaceInfo->IdleCount >> 2); + Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2); Endpoint_ClearIN(); - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; } } -bool USB_HID_ConfigureEndpoints(USB_ClassInfo_HID_t* HIDInterfaceInfo) +bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) { - HIDInterfaceInfo->UsingReportProtocol = true; + memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State)); + HIDInterfaceInfo->State.UsingReportProtocol = true; + HIDInterfaceInfo->State.IdleCount = 500; - if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->ReportINEndpointNumber, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, HIDInterfaceInfo->ReportINEndpointSize, ENDPOINT_BANK_SINGLE))) + if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT, + ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize, + HIDInterfaceInfo->Config.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE))) { return false; } - if (HIDInterfaceInfo->ReportOUTEndpointNumber) - { - if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->ReportOUTEndpointNumber, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_OUT, HIDInterfaceInfo->ReportOUTEndpointSize, ENDPOINT_BANK_SINGLE))) - { - return false; - } - } - return true; } -void USB_HID_USBTask(USB_ClassInfo_HID_t* HIDInterfaceInfo) +void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo) { - if (!(USB_IsConnected)) + if (USB_DeviceState != DEVICE_STATE_Configured) return; - Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportINEndpointNumber); + Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber); - if (Endpoint_IsReadWriteAllowed() && - !(HIDInterfaceInfo->IdleCount && HIDInterfaceInfo->IdleMSRemaining)) + if (Endpoint_IsReadWriteAllowed()) { - if (HIDInterfaceInfo->IdleCount && !(HIDInterfaceInfo->IdleMSRemaining)) - HIDInterfaceInfo->IdleMSRemaining = HIDInterfaceInfo->IdleCount; - - uint8_t ReportINData[HIDInterfaceInfo->ReportINBufferSize]; - uint16_t ReportINSize; + uint8_t ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize]; + uint8_t ReportID = 0; + uint16_t ReportINSize = 0; memset(ReportINData, 0, sizeof(ReportINData)); - ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, ReportINData); - - if (ReportINSize) - Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK); + 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)); - Endpoint_ClearIN(); - } - - if (HIDInterfaceInfo->ReportOUTEndpointNumber) - { - Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportOUTEndpointNumber); - - if (Endpoint_IsOUTReceived()) + if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL) { - uint16_t ReportOUTSize = Endpoint_BytesInEndpoint(); - uint8_t ReportOUTData[ReportOUTSize]; - - if (ReportOUTSize) - Endpoint_Read_Stream_LE(ReportOUTData, ReportOUTSize, NO_STREAM_CALLBACK); - - CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo, ReportOUTData, ReportOUTSize); + StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0); + memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, ReportINSize); + } + + if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed)) + { + HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount; + + Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber); + + if (ReportID) + Endpoint_Write_Byte(ReportID); + + Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK); - Endpoint_ClearOUT(); + Endpoint_ClearIN(); } } } + +#endif