From 84c3c4a3d90e7f890c61d4640c2d65a469b42b79 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Mon, 27 Jul 2009 10:48:55 +0000
Subject: [PATCH] Change Host mode class driver Pipe configuration routines --
 better to let the application fetch the configuration descriptors once, and
 then pass them to each class driver in turn.

---
 Demos/Host/ClassDriver/CDCHost/CDCHost.c     | 28 +++++++++++++++++---
 Demos/Host/ClassDriver/MouseHost/MouseHost.c | 28 +++++++++++++++++---
 LUFA/Drivers/USB/Class/Host/CDC.c            | 17 +++---------
 LUFA/Drivers/USB/Class/Host/CDC.h            | 13 +++++----
 LUFA/Drivers/USB/Class/Host/HID.c            | 19 +++----------
 LUFA/Drivers/USB/Class/Host/HID.h            | 11 ++++----
 6 files changed, 66 insertions(+), 50 deletions(-)

diff --git a/Demos/Host/ClassDriver/CDCHost/CDCHost.c b/Demos/Host/ClassDriver/CDCHost/CDCHost.c
index 4479e4fb8..b74c39418 100644
--- a/Demos/Host/ClassDriver/CDCHost/CDCHost.c
+++ b/Demos/Host/ClassDriver/CDCHost/CDCHost.c
@@ -74,10 +74,31 @@ int main(void)
 			case HOST_STATE_Addressed:
 				LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
 			
-				if (CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface, 512) != CDC_ENUMERROR_NoError)
+				uint16_t ConfigDescriptorSize;
+				uint8_t  ConfigDescriptorData[512];
+
+				if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
 				{
-					printf("Attached device is not a valid CDC device.\r\n");
+					printf("Error Retrieving Device Descriptor.\r\n");
+					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
+					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
+					break;
+				}
 				
+				if (ConfigDescriptorSize > 512)
+				{
+					printf("Device Descriptor Too Large To Process.\r\n");
+					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
+					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
+					break;
+				}
+				  
+				USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
+
+				if (CDC_Host_ConfigurePipes(&VirtualSerial_CDC_Interface,
+				                            ConfigDescriptorSize, ConfigDescriptorData) != CDC_ENUMERROR_NoError)
+				{
+					printf("Attached Device Not a Valid CDC Class Device.\r\n");
 					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 					break;
@@ -86,12 +107,11 @@ int main(void)
 				if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
 				{
 					printf("Error Setting Device Configuration.\r\n");
-
 					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 					break;
 				}
-
+				
 				printf("CDC Device Enumerated.\r\n");
 				USB_HostState = HOST_STATE_Configured;
 				break;
diff --git a/Demos/Host/ClassDriver/MouseHost/MouseHost.c b/Demos/Host/ClassDriver/MouseHost/MouseHost.c
index 88518a845..c2850cfec 100644
--- a/Demos/Host/ClassDriver/MouseHost/MouseHost.c
+++ b/Demos/Host/ClassDriver/MouseHost/MouseHost.c
@@ -76,10 +76,31 @@ int main(void)
 			case HOST_STATE_Addressed:
 				LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
 			
-				if (HID_Host_ConfigurePipes(&Mouse_HID_Interface, 512) != HID_ENUMERROR_NoError)
+				uint16_t ConfigDescriptorSize;
+				uint8_t  ConfigDescriptorData[512];
+
+				if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
+				{
+					printf("Error Retrieving Device Descriptor.\r\n");
+					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
+					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
+					break;
+				}
+				
+				if (ConfigDescriptorSize > 512)
 				{
-					printf("Attached device is not a valid Mouse.\r\n");
-					
+					printf("Device Descriptor Too Large To Process.\r\n");
+					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
+					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
+					break;
+				}
+				  
+				USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
+
+				if (HID_Host_ConfigurePipes(&Mouse_HID_Interface,
+				                            ConfigDescriptorSize, ConfigDescriptorData) != HID_ENUMERROR_NoError)
+				{
+					printf("Attached Device Not a Valid Mouse.\r\n");
 					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 					break;
@@ -88,7 +109,6 @@ int main(void)
 				if (USB_Host_SetDeviceConfiguration(1) != HOST_SENDCONTROL_Successful)
 				{
 					printf("Error Setting Device Configuration.\r\n");
-
 					LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 					USB_HostState = HOST_STATE_WaitForDeviceRemoval;
 					break;
diff --git a/LUFA/Drivers/USB/Class/Host/CDC.c b/LUFA/Drivers/USB/Class/Host/CDC.c
index d648ec23e..4402f3386 100644
--- a/LUFA/Drivers/USB/Class/Host/CDC.c
+++ b/LUFA/Drivers/USB/Class/Host/CDC.c
@@ -34,24 +34,13 @@
 #define  INCLUDE_FROM_CDC_CLASS_HOST_C
 #include "CDC.h"
 
-uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t MaxConfigBufferSize)
+uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t ConfigDescriptorSize,
+                                uint8_t* ConfigDescriptorData)
 {
-	uint8_t* ConfigDescriptorData;
-	uint16_t ConfigDescriptorSize;
 	uint8_t  FoundEndpoints = 0;
 
-	if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
-	  return CDC_ENUMERROR_ControlError;
-	
-	if (ConfigDescriptorSize > MaxConfigBufferSize)
-	  return CDC_ENUMERROR_DescriptorTooLarge;
-	  
-	ConfigDescriptorData = alloca(ConfigDescriptorSize);
-
-	USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
-	
 	if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
-	  return CDC_ENUMERROR_InvalidConfigDataReturned;
+	  return CDC_ENUMERROR_InvalidConfigDescriptor;
 	
 	if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
 	                              DComp_CDC_Host_NextCDCControlInterface) != DESCRIPTOR_SEARCH_COMP_Found)
diff --git a/LUFA/Drivers/USB/Class/Host/CDC.h b/LUFA/Drivers/USB/Class/Host/CDC.h
index e9518fede..5e0e4690c 100644
--- a/LUFA/Drivers/USB/Class/Host/CDC.h
+++ b/LUFA/Drivers/USB/Class/Host/CDC.h
@@ -108,17 +108,16 @@
 			enum
 			{
 				CDC_ENUMERROR_NoError                    = 0, /**< Configuration Descriptor was processed successfully */
-				CDC_ENUMERROR_ControlError               = 1, /**< A control request to the device failed to complete successfully */
-				CDC_ENUMERROR_DescriptorTooLarge         = 2, /**< The device's Configuration Descriptor is too large to process */
-				CDC_ENUMERROR_InvalidConfigDataReturned  = 3, /**< The device returned an invalid Configuration Descriptor */
-				CDC_ENUMERROR_NoCDCInterfaceFound        = 4, /**< A compatible CDC interface was not found in the device's Configuration Descriptor */
-				CDC_ENUMERROR_EndpointsNotFound          = 5, /**< Compatible CDC endpoints were not found in the device's CDC interface */
+				CDC_ENUMERROR_InvalidConfigDescriptor    = 1, /**< The device returned an invalid Configuration Descriptor */
+				CDC_ENUMERROR_NoCDCInterfaceFound        = 2, /**< A compatible CDC interface was not found in the device's Configuration Descriptor */
+				CDC_ENUMERROR_EndpointsNotFound          = 3, /**< Compatible CDC endpoints were not found in the device's CDC interface */
 			} CDCHost_EnumerationFailure_ErrorCodes_t;
 	
 		/* Function Prototypes: */
 			void CDC_Host_USBTask(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo);
-			uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t MaxConfigBufferSize);
-
+			uint8_t CDC_Host_ConfigurePipes(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo, uint16_t ConfigDescriptorLength,
+			                                uint8_t* DeviceConfigDescriptor);
+			
 			void EVENT_CDC_Host_ControLineStateChanged(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo);
 			
 			uint8_t CDC_Host_SetLineEncoding(USB_ClassInfo_CDC_Host_t* CDCInterfaceInfo);
diff --git a/LUFA/Drivers/USB/Class/Host/HID.c b/LUFA/Drivers/USB/Class/Host/HID.c
index 4f013d56e..8639b4836 100644
--- a/LUFA/Drivers/USB/Class/Host/HID.c
+++ b/LUFA/Drivers/USB/Class/Host/HID.c
@@ -34,24 +34,13 @@
 #define INCLUDE_FROM_HID_CLASS_HOST_C
 #include "HID.h"
 
-uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t MaxConfigBufferSize)
+uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorSize,
+                                uint8_t* ConfigDescriptorData)
 {
-	uint8_t* ConfigDescriptorData;
-	uint16_t ConfigDescriptorSize;
 	uint8_t  FoundEndpoints = 0;
 
-	if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
-	  return HID_ENUMERROR_ControlError;
-	
-	if (ConfigDescriptorSize > MaxConfigBufferSize)
-	  return HID_ENUMERROR_DescriptorTooLarge;
-	  
-	ConfigDescriptorData = alloca(ConfigDescriptorSize);
-
-	USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, ConfigDescriptorData);
-	
 	if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
-	  return HID_ENUMERROR_InvalidConfigDataReturned;
+	  return HID_ENUMERROR_InvalidConfigDescriptor;
 	
 	do
 	{
@@ -62,7 +51,7 @@ uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint
 		}
 	} while (HIDInterfaceInfo->Config.MatchInterfaceProtocol &&
 	         DESCRIPTOR_PCAST(ConfigDescriptorData,
-	                          USB_Descriptor_Interface_t)->HIDInterfaceProtocol != HIDInterfaceInfo->Config.Protocol);
+	                          USB_Descriptor_Interface_t)->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol);
 
 	while (FoundEndpoints != ((1 << HID_FOUND_DATAPIPE_IN) | (1 << HID_FOUND_DATAPIPE_OUT)))
 	{
diff --git a/LUFA/Drivers/USB/Class/Host/HID.h b/LUFA/Drivers/USB/Class/Host/HID.h
index 9ae1f1469..1959fd265 100644
--- a/LUFA/Drivers/USB/Class/Host/HID.h
+++ b/LUFA/Drivers/USB/Class/Host/HID.h
@@ -85,16 +85,15 @@
 			enum
 			{
 				HID_ENUMERROR_NoError                    = 0, /**< Configuration Descriptor was processed successfully */
-				HID_ENUMERROR_ControlError               = 1, /**< A control request to the device failed to complete successfully */
-				HID_ENUMERROR_DescriptorTooLarge         = 2, /**< The device's Configuration Descriptor is too large to process */
-				HID_ENUMERROR_InvalidConfigDataReturned  = 3, /**< The device returned an invalid Configuration Descriptor */
-				HID_ENUMERROR_NoHIDInterfaceFound        = 4, /**< A compatible HID interface was not found in the device's Configuration Descriptor */
-				HID_ENUMERROR_EndpointsNotFound          = 5, /**< Compatible HID endpoints were not found in the device's CDC interface */
+				HID_ENUMERROR_InvalidConfigDescriptor    = 1, /**< The device returned an invalid Configuration Descriptor */
+				HID_ENUMERROR_NoHIDInterfaceFound        = 2, /**< A compatible HID interface was not found in the device's Configuration Descriptor */
+				HID_ENUMERROR_EndpointsNotFound          = 3, /**< Compatible HID endpoints were not found in the device's CDC interface */
 			} CDCHost_EnumerationFailure_ErrorCodes_t;
 	
 		/* Function Prototypes: */
 			void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo);
-			uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t MaxConfigBufferSize);
+			uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorLength,
+			                                uint8_t* DeviceConfigDescriptor);
 		
 	/* Private Interface - For use in library only: */
 	#if !defined(__DOXYGEN__)
-- 
GitLab