/*
              LUFA Library
-     Copyright (C) Dean Camera, 2011.
+     Copyright (C) Dean Camera, 2012.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2012  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
                        Token           = PIPE_TOKEN_OUT;
                        Type            = EP_TYPE_BULK;
                        DoubleBanked    = MSInterfaceInfo->Config.DataOUTPipeDoubleBank;
-                       
+
                        MSInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
                }
                else
 
        if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t),
                                              NULL)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
+       {
+               return ErrorCode;
+       }
 
        Pipe_ClearOUT();
        Pipe_WaitUntilReady();
 
        Pipe_Freeze();
 
-       if ((BufferPtr != NULL) &&
-           ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, SCSICommandBlock, (void*)BufferPtr)) != PIPE_RWSTREAM_NoError))
+       if (BufferPtr != NULL)
        {
-               Pipe_Freeze();
-               return ErrorCode;
+               ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, SCSICommandBlock, (void*)BufferPtr);
+
+               if ((ErrorCode != PIPE_RWSTREAM_NoError) && (ErrorCode != PIPE_RWSTREAM_PipeStalled))
+               {
+                       Pipe_Freeze();
+                       return ErrorCode;
+               }
        }
 
-       return ErrorCode;
+       MS_CommandStatusWrapper_t SCSIStatusBlock;
+       return MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSIStatusBlock);
 }
 
 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
 
        if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
          return ErrorCode;
-       
+
        Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
-       
+
        if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
          return ErrorCode;
 
 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
                           uint8_t* const MaxLUNIndex)
 {
-       uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
+       uint8_t ErrorCode;
 
        USB_ControlRequest = (USB_Request_Header_t)
                {
        if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
          return HOST_SENDCONTROL_DeviceDisconnected;
 
-       uint8_t ErrorCode;
-
        MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
                {
                        .DataTransferLength = CPU_TO_LE32(sizeof(SCSI_Inquiry_Response_t)),
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
-       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, InquiryData)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       return PIPE_RWSTREAM_NoError;
+       return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, InquiryData);
 }
 
 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
        if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
          return HOST_SENDCONTROL_DeviceDisconnected;
 
-       uint8_t ErrorCode;
-
        MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
                {
                        .DataTransferLength = CPU_TO_LE32(0),
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
-       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       return PIPE_RWSTREAM_NoError;
+       return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL);
 }
 
 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
        if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)
          return ErrorCode;
 
        DeviceCapacity->Blocks    = BE32_TO_CPU(DeviceCapacity->Blocks);
        DeviceCapacity->BlockSize = BE32_TO_CPU(DeviceCapacity->BlockSize);
 
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
        return PIPE_RWSTREAM_NoError;
 }
 
        if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
          return HOST_SENDCONTROL_DeviceDisconnected;
 
-       uint8_t ErrorCode;
-
        MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
                {
                        .DataTransferLength = CPU_TO_LE32(sizeof(SCSI_Request_Sense_Response_t)),
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
-       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       return PIPE_RWSTREAM_NoError;
+       return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData);
 }
 
 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
        if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
          return HOST_SENDCONTROL_DeviceDisconnected;
 
-       uint8_t ErrorCode;
-
        MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
                {
                        .DataTransferLength = CPU_TO_LE32(0),
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
-       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       return PIPE_RWSTREAM_NoError;
+       return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL);
 }
 
 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
        if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
          return HOST_SENDCONTROL_DeviceDisconnected;
 
-       uint8_t ErrorCode;
-
        MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
                {
                        .DataTransferLength = cpu_to_le32((uint32_t)Blocks * BlockSize),
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
-       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       return PIPE_RWSTREAM_NoError;
+       return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer);
 }
 
 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
        if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
          return HOST_SENDCONTROL_DeviceDisconnected;
 
-       uint8_t ErrorCode;
-
        MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
                {
                        .DataTransferLength = cpu_to_le32((uint32_t)Blocks * BlockSize),
                                }
                };
 
-       MS_CommandStatusWrapper_t SCSICommandStatus;
-
-       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
-         return ErrorCode;
-
-       return PIPE_RWSTREAM_NoError;
+       return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer);
 }
 
 #endif