From 72932e278000c7086073a28639e3cfae06e39eb3 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Thu, 30 Jul 2009 14:35:42 +0000
Subject: [PATCH] Fix to HID device mode Class driver, so that new reports are
 compared against the old, and updated reports made within the idle period are
 sent immediately to the host.

---
 Demos/Device/ClassDriver/CDC/CDC.c            |  2 +-
 .../ClassDriver/GenericHID/GenericHID.c       |  2 --
 Demos/Device/ClassDriver/Joystick/Joystick.c  |  2 --
 Demos/Device/ClassDriver/Keyboard/Keyboard.c  |  2 --
 .../ClassDriver/KeyboardMouse/KeyboardMouse.c |  4 ---
 Demos/Device/ClassDriver/Mouse/Mouse.c        |  2 --
 LUFA/Drivers/USB/Class/Device/HID.c           | 25 +++++++++++--------
 LUFA/Drivers/USB/Class/Device/HID.h           | 21 ++++++++++------
 Projects/Magstripe/Magstripe.c                |  2 --
 9 files changed, 30 insertions(+), 32 deletions(-)

diff --git a/Demos/Device/ClassDriver/CDC/CDC.c b/Demos/Device/ClassDriver/CDC/CDC.c
index 1bcb4640a..8e3530ca2 100644
--- a/Demos/Device/ClassDriver/CDC/CDC.c
+++ b/Demos/Device/ClassDriver/CDC/CDC.c
@@ -91,7 +91,7 @@ int main(void)
 	for (;;)
 	{
 		CheckJoystickMovement();
-		
+		 
 		/* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
 		while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
 		  CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
diff --git a/Demos/Device/ClassDriver/GenericHID/GenericHID.c b/Demos/Device/ClassDriver/GenericHID/GenericHID.c
index 72063b6fa..b04f7af76 100644
--- a/Demos/Device/ClassDriver/GenericHID/GenericHID.c
+++ b/Demos/Device/ClassDriver/GenericHID/GenericHID.c
@@ -48,8 +48,6 @@ USB_ClassInfo_HID_Device_t Generic_HID_Interface =
 
 				.ReportINEndpointNumber  = GENERIC_IN_EPNUM,
 				.ReportINEndpointSize    = GENERIC_EPSIZE,
-				
-				.ReportINBufferSize      = GENERIC_REPORT_SIZE,
 			},
 	};
 
diff --git a/Demos/Device/ClassDriver/Joystick/Joystick.c b/Demos/Device/ClassDriver/Joystick/Joystick.c
index 9a1080c53..d9ff6ca91 100644
--- a/Demos/Device/ClassDriver/Joystick/Joystick.c
+++ b/Demos/Device/ClassDriver/Joystick/Joystick.c
@@ -48,8 +48,6 @@ USB_ClassInfo_HID_Device_t Joystick_HID_Interface =
 
 				.ReportINEndpointNumber  = JOYSTICK_EPNUM,
 				.ReportINEndpointSize    = JOYSTICK_EPSIZE,
-				
-				.ReportINBufferSize      = sizeof(USB_JoystickReport_Data_t),
 			},
 	};
 
diff --git a/Demos/Device/ClassDriver/Keyboard/Keyboard.c b/Demos/Device/ClassDriver/Keyboard/Keyboard.c
index ba0d2396f..cd8c3e69e 100644
--- a/Demos/Device/ClassDriver/Keyboard/Keyboard.c
+++ b/Demos/Device/ClassDriver/Keyboard/Keyboard.c
@@ -49,8 +49,6 @@ USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
 
 				.ReportINEndpointNumber  = KEYBOARD_EPNUM,
 				.ReportINEndpointSize    = KEYBOARD_EPSIZE,
-
-				.ReportINBufferSize      = sizeof(USB_KeyboardReport_Data_t),
 			},
     };
 
diff --git a/Demos/Device/ClassDriver/KeyboardMouse/KeyboardMouse.c b/Demos/Device/ClassDriver/KeyboardMouse/KeyboardMouse.c
index 241553c2a..45a2c5270 100644
--- a/Demos/Device/ClassDriver/KeyboardMouse/KeyboardMouse.c
+++ b/Demos/Device/ClassDriver/KeyboardMouse/KeyboardMouse.c
@@ -50,8 +50,6 @@ USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
 
 				.ReportINEndpointNumber  = KEYBOARD_IN_EPNUM,
 				.ReportINEndpointSize    = HID_EPSIZE,
-				
-				.ReportINBufferSize      = sizeof(USB_KeyboardReport_Data_t),
 			},
 	};
 	
@@ -68,8 +66,6 @@ USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
 
 				.ReportINEndpointNumber  = MOUSE_IN_EPNUM,
 				.ReportINEndpointSize    = HID_EPSIZE,
-
-				.ReportINBufferSize      = sizeof(USB_MouseReport_Data_t),
 			},
 			
 		.State =
diff --git a/Demos/Device/ClassDriver/Mouse/Mouse.c b/Demos/Device/ClassDriver/Mouse/Mouse.c
index 4bb1fa1dc..44894340b 100644
--- a/Demos/Device/ClassDriver/Mouse/Mouse.c
+++ b/Demos/Device/ClassDriver/Mouse/Mouse.c
@@ -48,8 +48,6 @@ USB_ClassInfo_HID_Device_t Mouse_HID_Interface =
 
 				.ReportINEndpointNumber  = MOUSE_EPNUM,
 				.ReportINEndpointSize    = MOUSE_EPSIZE,
-
-				.ReportINBufferSize      = sizeof(USB_MouseReport_Data_t),
 			},
 	};
 
diff --git a/LUFA/Drivers/USB/Class/Device/HID.c b/LUFA/Drivers/USB/Class/Device/HID.c
index 43f11ee7c..4c13436e0 100644
--- a/LUFA/Drivers/USB/Class/Device/HID.c
+++ b/LUFA/Drivers/USB/Class/Device/HID.c
@@ -51,7 +51,7 @@ void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterf
 			{
 				Endpoint_ClearSETUP();	
 
-				uint8_t  ReportINData[HIDInterfaceInfo->Config.ReportINBufferSize];
+				uint8_t  ReportINData[HID_MAX_REPORT_SIZE];
 				uint16_t ReportINSize;
 				uint8_t  ReportID = (USB_ControlRequest.wValue & 0xFF);
 
@@ -150,27 +150,32 @@ bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfac
 		
 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
 {
+	static uint8_t PreviousReportINData[HID_MAX_REPORT_SIZE];
+
 	if (USB_DeviceState != DEVICE_STATE_Configured)
 	  return;
 
 	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  ReportINData[HID_MAX_REPORT_SIZE];
 		uint8_t  ReportID = 0;
+		uint16_t ReportINSize;
 
 		memset(ReportINData, 0, sizeof(ReportINData));
 
-		ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);
+		ReportINSize  = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);
 
-		if (ReportINSize)
+		bool StatesChanged     = (memcmp(ReportINData, PreviousReportINData, ReportINSize) != 0);
+		bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
+		
+		memcpy(PreviousReportINData, ReportINData, ReportINSize);
+
+		if (ReportINSize && (StatesChanged || IdlePeriodElapsed))
 		{
+			HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
+
 			if (ReportID)
 			  Endpoint_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
 
diff --git a/LUFA/Drivers/USB/Class/Device/HID.h b/LUFA/Drivers/USB/Class/Device/HID.h
index 87f96c680..61f43a141 100644
--- a/LUFA/Drivers/USB/Class/Device/HID.h
+++ b/LUFA/Drivers/USB/Class/Device/HID.h
@@ -56,6 +56,17 @@
 		#endif
 
 	/* Public Interface - May be used in end-application: */
+		/* Macros: */
+			#if !defined(HID_MAX_REPORT_SIZE)
+				/** Maximum size of an IN report which can be generated by the device and sent to the host, in bytes.
+				 *
+				 *  \note If larger reports than the default specified here are to be generated by the device, this
+				 *        value can be overridden by defining this token to the required value in the project makefile
+				 *        and passing it to the compiler via the -D switch.
+				 */
+				#define HID_MAX_REPORT_SIZE     16
+			#endif
+	
 		/* Type Defines: */
 			/** Class state structure. An instance of this structure should be made for each HID interface
 			 *  within the user application, and passed to each of the HID class driver functions as the
@@ -68,19 +79,15 @@
 					uint8_t  InterfaceNumber; /**< Interface number of the HID interface within the device */
 
 					uint8_t  ReportINEndpointNumber; /**< Endpoint number of the HID interface's IN report endpoint */
-					uint16_t ReportINEndpointSize; /**< Size in bytes of the HID interface's IN report endpoint */
-					
-					uint8_t  ReportINBufferSize; /**< Size of the largest possible report to send to the host, for
-												  *   buffer allocation purposes
-												  */
+					uint16_t ReportINEndpointSize; /**< Size in bytes of the HID interface's IN report endpoint */					
 				} Config; /**< Config data for the USB class interface within the device. All elements in this section
 				           *   <b>must</b> be set or the interface will fail to enumerate and operate correctly.
 				           */										 
 				struct
 				{
 					bool     UsingReportProtocol; /**< Indicates if the HID interface is set to Boot or Report protocol mode */
-					uint16_t IdleCount; /**< Report idle period, in ms, set by the host */
-					uint16_t IdleMSRemaining; /**< Total number of ms remaining before the idle period elapsed - this should be
+					uint16_t IdleCount; /**< Report idle period, in mS, set by the host */
+					uint16_t IdleMSRemaining; /**< Total number of mS remaining before the idle period elapsed - this should be
 											   *   decremented by the user application if non-zero each millisecond */			
 				} State; /**< State data for the USB class interface within the device. All elements in this section
 				          *   are reset to their defaults when the interface is enumerated.
diff --git a/Projects/Magstripe/Magstripe.c b/Projects/Magstripe/Magstripe.c
index aaa432d20..898ad3c39 100644
--- a/Projects/Magstripe/Magstripe.c
+++ b/Projects/Magstripe/Magstripe.c
@@ -57,8 +57,6 @@ USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
 
 				.ReportINEndpointNumber  = KEYBOARD_EPNUM,
 				.ReportINEndpointSize    = KEYBOARD_EPSIZE,
-				
-				.ReportINBufferSize      = sizeof(USB_KeyboardReport_Data_t),
 			},
 	};
 
-- 
GitLab