From c49bdcb7c930f2d0e0cf6887b3326f9e8f7f37b3 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Fri, 4 Dec 2009 09:08:48 +0000
Subject: [PATCH] Fixed Mass Storage Host Class driver and Low Level demo not
 clearing the error condition if an attached device returns a STALL to a GET
 MAX LUN request (thanks to Martin Luxen).

---
 .../LowLevel/MassStorageHost/Lib/MassStoreCommands.c  | 11 +++++++++--
 LUFA/Drivers/USB/Class/Host/MassStorage.c             |  7 +++++--
 LUFA/Drivers/USB/Class/Host/MassStorage.h             |  4 ++++
 LUFA/ManPages/ChangeLog.txt                           |  2 ++
 4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c b/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c
index ff01dc0f2..edd142525 100644
--- a/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c
+++ b/Demos/Host/LowLevel/MassStorageHost/Lib/MassStoreCommands.c
@@ -287,13 +287,17 @@ uint8_t MassStore_MassStorageReset(void)
 /** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical
  *  Unit in the attached device.
  *
+ *  \note Some devices do not support this request, and will STALL it when issued. To get around this,
+ *        on unsupported devices the max LUN index will be reported as zero and no error will be returned
+ *        if the device STALLs the request.
+ *
  *  \param[out] MaxLUNIndex  Pointer to the location that the maximum LUN index value should be stored
  *
  *  \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
 {
-	uint8_t ErrorCode;
+	uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
 
 	USB_ControlRequest = (USB_Request_Header_t)
 		{
@@ -313,7 +317,10 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
 		Pipe_ClearStall();
 	
 		/* Some faulty Mass Storage devices don't implement the GET_MAX_LUN request, so assume a single LUN */
-		*MaxLUNIndex = 0;	
+		*MaxLUNIndex = 0;
+		
+		/* Clear the error, and pretend the request executed correctly if the device STALLed it */
+		ErrorCode = HOST_SENDCONTROL_Successful;
 	}
 	
 	return ErrorCode;
diff --git a/LUFA/Drivers/USB/Class/Host/MassStorage.c b/LUFA/Drivers/USB/Class/Host/MassStorage.c
index 75b8ef485..ca071752d 100644
--- a/LUFA/Drivers/USB/Class/Host/MassStorage.c
+++ b/LUFA/Drivers/USB/Class/Host/MassStorage.c
@@ -305,7 +305,7 @@ uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
 
 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, uint8_t* const MaxLUNIndex)
 {
-	uint8_t ErrorCode;
+	uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
 
 	USB_ControlRequest = (USB_Request_Header_t)
 		{
@@ -319,7 +319,10 @@ uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, uint8_
 	Pipe_SelectPipe(PIPE_CONTROLPIPE);
 
 	if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) != HOST_SENDCONTROL_Successful)
-	  *MaxLUNIndex = 0;
+	{
+		*MaxLUNIndex = 0;
+		ErrorCode = HOST_SENDCONTROL_Successful;
+	}
 	
 	return ErrorCode;
 }
diff --git a/LUFA/Drivers/USB/Class/Host/MassStorage.h b/LUFA/Drivers/USB/Class/Host/MassStorage.h
index fc3bc6678..584172fe8 100644
--- a/LUFA/Drivers/USB/Class/Host/MassStorage.h
+++ b/LUFA/Drivers/USB/Class/Host/MassStorage.h
@@ -149,6 +149,10 @@
 			 *  UNit, a logical drive) in the device. This value can then be used in the other functions of the Mass Storage
 			 *  Host mode Class driver to address a specific LUN within the device.
 			 *
+			 *  \note Some devices do not support this request, and will STALL it when issued. To get around this,
+			 *        on unsupported devices the max LUN index will be reported as zero and no error will be returned
+			 *        if the device STALLs the request.
+			 *
 			 *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state
 			 *  \param[out] MaxLUNIndex  Pointer to a location where the highest LUN index value should be stored
 			 *
diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index d3d80bfb4..629467486 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -49,6 +49,8 @@
   *  - Fixed CDCHost failing on devices with bidirectional endpoints
   *  - Fixed USB driver failing to define the PLL prescaler mask for the ATMEGA8U2 and ATMEGA16U2
   *  - Fixed HID Parser not distributing the Usage Min and Usage Max values across an array of report items
+  *  - Fixed Mass Storage Host Class driver and Low Level demo not clearing the error condition if an attached device returns a
+  *    STALL to a GET MAX LUN request (thanks to Martin Luxen)
   *
   *  \section Sec_ChangeLog091122 Version 091122
   *
-- 
GitLab