From 559ca2ba046e47faa54f0845339f0385a5cfbb26 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Wed, 26 Aug 2009 08:20:55 +0000 Subject: [PATCH] Added MS_Host_TestUnitReady() and MS_Host_ReadDeviceCapacity() to the host mode Mass Storage Class driver. --- .../MassStorageHost/MassStorageHost.c | 12 +- LUFA/Drivers/USB/Class/Host/MassStorage.c | 135 ++++++++++++++++-- LUFA/Drivers/USB/Class/Host/MassStorage.h | 15 +- 3 files changed, 145 insertions(+), 17 deletions(-) diff --git a/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c b/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c index d9fe47d3b6..57aba4599c 100644 --- a/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c +++ b/Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c @@ -150,11 +150,16 @@ int main(void) printf("Vendor \"%.8s\", Product \"%.16s\"\r\n", InquiryData.VendorID, InquiryData.ProductID); printf("Waiting until ready...\r\n"); - bool DeviceReady; - do + for (;;) { - if (MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0, &DeviceReady)) + uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0); + + if (!(ErrorCode)) + break; + + /* Check if an error other than a logical command error (device busy) received */ + if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED) { printf("Error waiting for device to be ready.\r\n"); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); @@ -162,7 +167,6 @@ int main(void) break; } } - while (!(DeviceReady)); printf("Retrieving Capacity... "); diff --git a/LUFA/Drivers/USB/Class/Host/MassStorage.c b/LUFA/Drivers/USB/Class/Host/MassStorage.c index 1610e422aa..4c23e81530 100644 --- a/LUFA/Drivers/USB/Class/Host/MassStorage.c +++ b/LUFA/Drivers/USB/Class/Host/MassStorage.c @@ -328,41 +328,154 @@ uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t .SCSICommandData = { SCSI_CMD_INQUIRY, - 0x00, // Reserved - 0x00, // Reserved - 0x00, // Reserved + 0x00, // Reserved + 0x00, // Reserved + 0x00, // Reserved sizeof(SCSI_Inquiry_Response_t), // Allocation Length - 0x00 // Unused (control) + 0x00 // Unused (control) } }; - MassStore_SendCommand(MSInterfaceInfo, &SCSICommandBlock); - - if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError) + if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ErrorCode; } - if ((ErrorCode = MassStore_SendReceiveData(MSInterfaceInfo, (uint8_t*)InquiryPtr)) != PIPE_RWSTREAM_NoError) + if ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, &SCSICommandBlock, InquiryData)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ErrorCode; } - if ((ErrorCode = MassStore_GetReturnedStatus(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError) + MS_CommandStatusWrapper_t SCSICommandStatus; + if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ErrorCode; } + if (SCSICommandStatus.Status != SCSI_Command_Pass) + ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED; + return ErrorCode; } -uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool* DeviceReady); +uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex) +{ + if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active)) + return HOST_SENDCONTROL_DeviceDisconnect; + + uint8_t ErrorCode = PIPE_RWSTREAM_NoError; + + MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t) + { + .Signature = CBW_SIGNATURE, + .Tag = MSInterfaceInfo->State.TransactionTag, + .DataTransferLength = 0, + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 6, + .SCSICommandData = + { + SCSI_CMD_TEST_UNIT_READY, + 0x00, // Reserved + 0x00, // Reserved + 0x00, // Reserved + 0x00, // Reserved + 0x00 // Unused (control) + } + }; + + if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + MS_CommandStatusWrapper_t SCSICommandStatus; + if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + if (SCSICommandStatus.Status != SCSI_Command_Pass) + ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED; + + return ErrorCode; +} uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, - SCSI_Capacity_t* DeviceCapacity); + SCSI_Capacity_t* DeviceCapacity) +{ + if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active)) + return HOST_SENDCONTROL_DeviceDisconnect; + + uint8_t ErrorCode = PIPE_RWSTREAM_NoError; + + MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t) + { + .Signature = CBW_SIGNATURE, + .Tag = MSInterfaceInfo->State.TransactionTag, + .DataTransferLength = sizeof(SCSI_Capacity_t), + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 10, + .SCSICommandData = + { + SCSI_CMD_READ_CAPACITY_10, + 0x00, // Reserved + 0x00, // MSB of Logical block address + 0x00, + 0x00, + 0x00, // LSB of Logical block address + 0x00, // Reserved + 0x00, // Reserved + 0x00, // Partial Medium Indicator + 0x00 // Unused (control) + } + }; + + if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + if ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + DeviceCapacity->Blocks = SwapEndian_32(DeviceCapacity->Blocks); + DeviceCapacity->BlockSize = SwapEndian_32(DeviceCapacity->BlockSize); + + MS_CommandStatusWrapper_t SCSICommandStatus; + if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError) + { + Pipe_Freeze(); + return ErrorCode; + } + + if (SCSICommandStatus.Status != SCSI_Command_Pass) + ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED; + + return ErrorCode; +} uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, SCSI_Request_Sense_Response_t* SenseData); diff --git a/LUFA/Drivers/USB/Class/Host/MassStorage.h b/LUFA/Drivers/USB/Class/Host/MassStorage.h index 5999fcf6d6..1678aefee7 100644 --- a/LUFA/Drivers/USB/Class/Host/MassStorage.h +++ b/LUFA/Drivers/USB/Class/Host/MassStorage.h @@ -55,6 +55,9 @@ #endif /* Public Interface - May be used in end-application: */ + /* Macros: */ + #define MS_ERROR_LOGICAL_CMD_FAILED 0xC0 + /* Type Defines: */ /** Class state structure. An instance of this structure should be made within the user application, * and passed to each of the HID class driver functions as the HIDInterfaceInfo parameter. This @@ -211,11 +214,19 @@ */ uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t* MaxLUNIndex) ATTR_NON_NULL_PTR_ARG(1, 2); + /** Retrieves the Mass Storage device's inquiry data for the specified LUN, indicating the device characteristics and + * properties. + * + * \param[in,out] MSInterfaceInfo Pointer to a structure containing a MS Class host configuration and state + * \param[in] LUNIndex LUN index within the device the command is being issued to + * \param[out] InquiryData Location where the read inquiry data should be stored + * + * \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED + */ uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, SCSI_Inquiry_Response_t* InquiryData) ATTR_NON_NULL_PTR_ARG(1, 3); - uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, - bool* DeviceReady) ATTR_NON_NULL_PTR_ARG(1, 3); + uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex) ATTR_NON_NULL_PTR_ARG(1); uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, SCSI_Capacity_t* DeviceCapacity) ATTR_NON_NULL_PTR_ARG(1, 3);