Completed initial Host mode Mass Storage class driver.
authorDean Camera <dean@fourwalledcubicle.com>
Thu, 27 Aug 2009 06:28:18 +0000 (06:28 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Thu, 27 Aug 2009 06:28:18 +0000 (06:28 +0000)
LUFA/Drivers/USB/Class/Host/MassStorage.c
LUFA/Drivers/USB/Class/Host/MassStorage.h

index 4c23e81..6c01ed4 100644 (file)
@@ -34,8 +34,6 @@
 #define INCLUDE_FROM_MS_CLASS_HOST_C\r
 #include "MassStorage.h"\r
 \r
 #define INCLUDE_FROM_MS_CLASS_HOST_C\r
 #include "MassStorage.h"\r
 \r
-#warning The Mass Storage Host mode Class driver is currently incomplete and is for preview purposes only.\r
-\r
 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint16_t ConfigDescriptorLength,\r
                                                           uint8_t* DeviceConfigDescriptor)\r
 {\r
 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint16_t ConfigDescriptorLength,\r
                                                           uint8_t* DeviceConfigDescriptor)\r
 {\r
@@ -478,14 +476,224 @@ uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uin
 }\r
 \r
 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\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
+                             SCSI_Request_Sense_Response_t* SenseData)\r
+{\r
+       if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active))\r
+         return HOST_SENDCONTROL_DeviceDisconnect;\r
 \r
 \r
-uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool PreventRemoval);                                                     \r
+       uint8_t ErrorCode = PIPE_RWSTREAM_NoError;\r
 \r
 \r
-uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddr,\r
-                                 uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer);\r
+       MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)\r
+               {\r
+                       .Signature          = CBW_SIGNATURE,\r
+                       .Tag                = MSInterfaceInfo->State.TransactionTag,\r
+                       .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),\r
+                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 6,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_REQUEST_SENSE,\r
+                                       0x00,                                  // Reserved\r
+                                       0x00,                                  // Reserved\r
+                                       0x00,                                  // Reserved\r
+                                       sizeof(SCSI_Request_Sense_Response_t), // Allocation Length\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, SenseData)) != 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
 \r
-uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddr,\r
-                                  uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer);\r
+       return ErrorCode;\r
+}\r
+\r
+uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool PreventRemoval)\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_OUT,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 6,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,\r
+                                       0x00,                   // Reserved\r
+                                       0x00,                   // Reserved\r
+                                       PreventRemoval,         // Prevent flag\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_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddress,\r
+                                 uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer)\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 = ((uint32_t)Blocks * BlockSize),\r
+                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 10,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_READ_10,\r
+                                       0x00,                   // Unused (control bits, all off)\r
+                                       (BlockAddress >> 24),   // MSB of Block Address\r
+                                       (BlockAddress >> 16),\r
+                                       (BlockAddress >> 8),\r
+                                       (BlockAddress & 0xFF),  // LSB of Block Address\r
+                                       0x00,                   // Unused (reserved)\r
+                                       0x00,                   // MSB of Total Blocks to Read\r
+                                       Blocks,                 // LSB of Total Blocks to Read\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, BlockBuffer)) != 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_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddress,\r
+                                  uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer)\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 = ((uint32_t)Blocks * BlockSize),\r
+                       .Flags              = COMMAND_DIRECTION_DATA_OUT,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 10,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_WRITE_10,\r
+                                       0x00,                   // Unused (control bits, all off)\r
+                                       (BlockAddress >> 24),   // MSB of Block Address\r
+                                       (BlockAddress >> 16),\r
+                                       (BlockAddress >> 8),\r
+                                       (BlockAddress & 0xFF),  // LSB of Block Address\r
+                                       0x00,                   // Unused (reserved)\r
+                                       0x00,                   // MSB of Total Blocks to Write\r
+                                       Blocks,                 // LSB of Total Blocks to Write\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_SendReceiveData(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != 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
 #endif\r
 \r
 #endif\r
index 1678aef..e64e143 100644 (file)
                        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
+                       /** Sends a TEST UNIT READY command to the device, to determine if it is ready to accept other SCSI commands.\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
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED if not ready\r
+                        */\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_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex) ATTR_NON_NULL_PTR_ARG(1);\r
 \r
+                       /** Retrieves the total capacity of the attached USB Mass Storage device, in blocks, and block size.\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] DeviceCapacity  Pointer to the location where the capacity information should be stored\r
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED if not ready\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
                \r
+                       /** Retrieves the device sense data, indicating the current device state and error codes for the previously\r
+                        *  issued command.\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] SenseData  Pointer to the location where the sense information should be stored\r
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED if not ready\r
+                        */\r
                        uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                     SCSI_Request_Sense_Response_t* SenseData) ATTR_NON_NULL_PTR_ARG(1, 3);\r
                \r
                        uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                     SCSI_Request_Sense_Response_t* SenseData) ATTR_NON_NULL_PTR_ARG(1, 3);\r
                \r
+                       /** Issues a PREVENT MEDIUM REMOVAL command, to logically (or, depending on the type of device, physically) lock\r
+                        *  the device from removal so that blocks of data on the medium can be read or altered.\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[in] PreventRemoval  Boolean true if the device should be locked from removal, false otherwise\r
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED if not ready\r
+                        */\r
                        uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                                  bool PreventRemoval) ATTR_NON_NULL_PTR_ARG(1);\r
                        \r
                        uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,\r
                                                                  bool PreventRemoval) ATTR_NON_NULL_PTR_ARG(1);\r
                        \r
-                       uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddr,\r
+                       /** Reads blocks of data from the attached Mass Storage device's medium.\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[in] BlockAddress  Starting block address within the device to read from\r
+                        *  \param[in] Blocks  Total number of blocks to read\r
+                        *  \param[out] BlockBuffer  Pointer to where the read data from the device should be stored\r
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED if not ready\r
+                        */\r
+                       uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddress,\r
                                                         uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer) ATTR_NON_NULL_PTR_ARG(1, 6);\r
                \r
                                                         uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer) ATTR_NON_NULL_PTR_ARG(1, 6);\r
                \r
-                       uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddr,\r
+                       /** Writes blocks of data to the attached Mass Storage device's medium.\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[in] BlockAddress  Starting block address within the device to write to\r
+                        *  \param[in] Blocks  Total number of blocks to read\r
+                        *  \param[in] BlockBuffer  Pointer to where the data to write should be sourced from\r
+                        *\r
+                        *  \return A value from the \ref Pipe_Stream_RW_ErrorCodes_t enum or MS_ERROR_LOGICAL_CMD_FAILED if not ready\r
+                        */\r
+                       uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddress,\r
                                                          uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer) ATTR_NON_NULL_PTR_ARG(1, 6);\r
 \r
        /* Private Interface - For use in library only: */\r
                                                          uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer) ATTR_NON_NULL_PTR_ARG(1, 6);\r
 \r
        /* Private Interface - For use in library only: */\r