Added MS_Host_TestUnitReady() and MS_Host_ReadDeviceCapacity() to the host mode Mass...
authorDean Camera <dean@fourwalledcubicle.com>
Wed, 26 Aug 2009 08:20:55 +0000 (08:20 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Wed, 26 Aug 2009 08:20:55 +0000 (08:20 +0000)
Demos/Host/ClassDriver/MassStorageHost/MassStorageHost.c
LUFA/Drivers/USB/Class/Host/MassStorage.c
LUFA/Drivers/USB/Class/Host/MassStorage.h

index d9fe47d..57aba45 100644 (file)
@@ -150,11 +150,16 @@ int main(void)
                                printf("Vendor \"%.8s\", Product \"%.16s\"\r\n", InquiryData.VendorID, InquiryData.ProductID);\r
                                \r
                                printf("Waiting until ready...\r\n");\r
                                printf("Vendor \"%.8s\", Product \"%.16s\"\r\n", InquiryData.VendorID, InquiryData.ProductID);\r
                                \r
                                printf("Waiting until ready...\r\n");\r
-                               bool DeviceReady;\r
 \r
 \r
-                               do\r
+                               for (;;)\r
                                {\r
                                {\r
-                                       if (MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0, &DeviceReady))\r
+                                       uint8_t ErrorCode = MS_Host_TestUnitReady(&FlashDisk_MS_Interface, 0);\r
+                                       \r
+                                       if (!(ErrorCode))\r
+                                         break;\r
+\r
+                                       /* Check if an error other than a logical command error (device busy) received */\r
+                                       if (ErrorCode != MS_ERROR_LOGICAL_CMD_FAILED)\r
                                        {\r
                                                printf("Error waiting for device to be ready.\r\n");\r
                                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
                                        {\r
                                                printf("Error waiting for device to be ready.\r\n");\r
                                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
@@ -162,7 +167,6 @@ int main(void)
                                                break;\r
                                        }\r
                                }\r
                                                break;\r
                                        }\r
                                }\r
-                               while (!(DeviceReady));\r
 \r
                                printf("Retrieving Capacity... ");\r
 \r
 \r
                                printf("Retrieving Capacity... ");\r
 \r
index 1610e42..4c23e81 100644 (file)
@@ -328,41 +328,154 @@ uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t
                        .SCSICommandData    =\r
                                {\r
                                        SCSI_CMD_INQUIRY,\r
                        .SCSICommandData    =\r
                                {\r
                                        SCSI_CMD_INQUIRY,\r
-                                       0x00,                   // Reserved\r
-                                       0x00,                   // Reserved\r
-                                       0x00,                   // Reserved\r
+                                       0x00,                            // Reserved\r
+                                       0x00,                            // Reserved\r
+                                       0x00,                            // Reserved\r
                                        sizeof(SCSI_Inquiry_Response_t), // Allocation Length\r
                                        sizeof(SCSI_Inquiry_Response_t), // Allocation Length\r
-                                       0x00                    // Unused (control)\r
+                                       0x00                             // Unused (control)\r
                                }\r
                };\r
        \r
                                }\r
                };\r
        \r
-       MassStore_SendCommand(MSInterfaceInfo, &SCSICommandBlock);\r
-\r
-       if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)\r
+       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;       \r
+       }\r
+       \r
+       if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)\r
        {\r
                Pipe_Freeze();\r
                return ErrorCode;\r
        }\r
 \r
        {\r
                Pipe_Freeze();\r
                return ErrorCode;\r
        }\r
 \r
-       if ((ErrorCode = MassStore_SendReceiveData(MSInterfaceInfo, (uint8_t*)InquiryPtr)) != PIPE_RWSTREAM_NoError)\r
+       if ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, &SCSICommandBlock, InquiryData)) != PIPE_RWSTREAM_NoError)\r
        {\r
                Pipe_Freeze();\r
                return ErrorCode;\r
        }       \r
        \r
        {\r
                Pipe_Freeze();\r
                return ErrorCode;\r
        }       \r
        \r
-       if ((ErrorCode = MassStore_GetReturnedStatus(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)\r
+       MS_CommandStatusWrapper_t SCSICommandStatus;\r
+       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)\r
        {\r
                Pipe_Freeze();\r
                return ErrorCode;\r
        }\r
        \r
        {\r
                Pipe_Freeze();\r
                return ErrorCode;\r
        }\r
        \r
+       if (SCSICommandStatus.Status != SCSI_Command_Pass)\r
+         ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;\r
+\r
        return ErrorCode;\r
 }\r
 \r
        return ErrorCode;\r
 }\r
 \r
-uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool* DeviceReady);\r
+uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex)\r
+{\r
+       if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active))\r
+         return HOST_SENDCONTROL_DeviceDisconnect;\r
+\r
+       uint8_t ErrorCode = PIPE_RWSTREAM_NoError;      \r
+\r
+       MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)\r
+               {\r
+                       .Signature          = CBW_SIGNATURE,\r
+                       .Tag                = MSInterfaceInfo->State.TransactionTag,\r
+                       .DataTransferLength = 0,\r
+                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 6,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_TEST_UNIT_READY,\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       0x00                    // Unused (control)\r
+                               }\r
+               };\r
+       \r
+       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;       \r
+       }\r
+       \r
+       MS_CommandStatusWrapper_t SCSICommandStatus;\r
+       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;\r
+       }\r
+       \r
+       if (SCSICommandStatus.Status != SCSI_Command_Pass)\r
+         ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;\r
+\r
+       return ErrorCode;\r
+}\r
 \r
 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
 \r
 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
-                                   SCSI_Capacity_t* DeviceCapacity);\r
+                                   SCSI_Capacity_t* DeviceCapacity)\r
+{\r
+       if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active))\r
+         return HOST_SENDCONTROL_DeviceDisconnect;\r
+\r
+       uint8_t ErrorCode = PIPE_RWSTREAM_NoError;\r
+\r
+       MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)\r
+               {\r
+                       .Signature          = CBW_SIGNATURE,\r
+                       .Tag                = MSInterfaceInfo->State.TransactionTag,\r
+                       .DataTransferLength = sizeof(SCSI_Capacity_t),\r
+                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 10,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_READ_CAPACITY_10,\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // MSB of Logical block address\r
+                                       0x00,\r
+                                       0x00,\r
+                                       0x00,                   // LSB of Logical block address\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Partial Medium Indicator\r
+                                       0x00                    // Unused (control)\r
+                               }\r
+               };\r
+       \r
+       if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;\r
+       }\r
+\r
+       if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;\r
+       }\r
+\r
+       if ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;\r
+       }\r
+         \r
+       DeviceCapacity->Blocks    = SwapEndian_32(DeviceCapacity->Blocks);\r
+       DeviceCapacity->BlockSize = SwapEndian_32(DeviceCapacity->BlockSize);\r
+       \r
+       MS_CommandStatusWrapper_t SCSICommandStatus;\r
+       if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)\r
+       {\r
+               Pipe_Freeze();\r
+               return ErrorCode;\r
+       }\r
+       \r
+       if (SCSICommandStatus.Status != SCSI_Command_Pass)\r
+         ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;\r
+\r
+       return ErrorCode;\r
+}\r
 \r
 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                              SCSI_Request_Sense_Response_t* SenseData);\r
 \r
 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                              SCSI_Request_Sense_Response_t* SenseData);\r
index 5999fcf..1678aef 100644 (file)
@@ -55,6 +55,9 @@
                #endif\r
 \r
        /* Public Interface - May be used in end-application: */\r
                #endif\r
 \r
        /* Public Interface - May be used in end-application: */\r
+               /* Macros: */\r
+                       #define MS_ERROR_LOGICAL_CMD_FAILED              0xC0\r
+       \r
                /* Type Defines: */\r
                        /** Class state structure. An instance of this structure should be made within the user application,\r
                         *  and passed to each of the HID class driver functions as the HIDInterfaceInfo parameter. This\r
                /* Type Defines: */\r
                        /** Class state structure. An instance of this structure should be made within the user application,\r
                         *  and passed to each of the HID class driver functions as the HIDInterfaceInfo parameter. This\r
                         */\r
                        uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t* MaxLUNIndex) ATTR_NON_NULL_PTR_ARG(1, 2);\r
 \r
                         */\r
                        uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t* MaxLUNIndex) ATTR_NON_NULL_PTR_ARG(1, 2);\r
 \r
+                       /** Retrieves the Mass Storage device's inquiry data for the specified LUN, indicating the device characteristics and\r
+                        *  properties.\r
+                        *\r
+                        *  \param[in,out] MSInterfaceInfo  Pointer to a structure containing a MS Class host configuration and state\r
+                        *  \param[in] LUNIndex  LUN index within the device the command is being issued to\r
+                        *  \param[out] InquiryData  Location where the read inquiry data should be stored\r
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED\r
+                        */\r
                        uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                       SCSI_Inquiry_Response_t* InquiryData) ATTR_NON_NULL_PTR_ARG(1, 3);\r
 \r
                        uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                       SCSI_Inquiry_Response_t* InquiryData) ATTR_NON_NULL_PTR_ARG(1, 3);\r
 \r
-                       uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
-                                                     bool* DeviceReady) ATTR_NON_NULL_PTR_ARG(1, 3);\r
+                       uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex) ATTR_NON_NULL_PTR_ARG(1);\r
 \r
                        uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                           SCSI_Capacity_t* DeviceCapacity) ATTR_NON_NULL_PTR_ARG(1, 3);\r
 \r
                        uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                           SCSI_Capacity_t* DeviceCapacity) ATTR_NON_NULL_PTR_ARG(1, 3);\r