Minor documentation fixes to the completed Host mode Class drivers.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / MassStorage.c
index ea7ed3d..6c01ed4 100644 (file)
@@ -34,8 +34,6 @@
 #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
@@ -131,7 +129,7 @@ void MS_Host_USBTask(USB_ClassInfo_MS_Host_t* MSInterfaceInfo)
        \r
 }\r
 \r
-static uint8_t MassStore_SendCommand(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, MS_CommandBlockWrapper_t* SCSICommandBlock)\r
+static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, MS_CommandBlockWrapper_t* SCSICommandBlock)\r
 {\r
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;\r
 \r
@@ -152,7 +150,7 @@ static uint8_t MassStore_SendCommand(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, M
        return PIPE_RWSTREAM_NoError;\r
 }\r
 \r
-static uint8_t MassStore_WaitForDataReceived(USB_ClassInfo_MS_Host_t* MSInterfaceInfo)\r
+static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* MSInterfaceInfo)\r
 {\r
        uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;\r
 \r
@@ -205,7 +203,7 @@ static uint8_t MassStore_WaitForDataReceived(USB_ClassInfo_MS_Host_t* MSInterfac
        return PIPE_RWSTREAM_NoError;\r
 }\r
 \r
-static uint8_t MassStore_SendReceiveData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo,\r
+static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo,\r
                                          MS_CommandBlockWrapper_t* SCSICommandBlock, void* BufferPtr)\r
 {\r
        uint8_t  ErrorCode = PIPE_RWSTREAM_NoError;\r
@@ -243,12 +241,12 @@ static uint8_t MassStore_SendReceiveData(USB_ClassInfo_MS_Host_t* MSInterfaceInf
        return PIPE_RWSTREAM_NoError;\r
 }\r
 \r
-static uint8_t MassStore_GetReturnedStatus(USB_ClassInfo_MS_Host_t* MSInterfaceInfo,\r
+static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t* MSInterfaceInfo,\r
                                            MS_CommandStatusWrapper_t* SCSICommandStatus)\r
 {\r
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;\r
 \r
-       if ((ErrorCode = MassStore_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)\r
+       if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)\r
          return ErrorCode;\r
 \r
        Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);\r
@@ -310,13 +308,392 @@ uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t* Max
        return ErrorCode;\r
 }\r
 \r
-uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, SCSI_Inquiry_Response_t* InquiryData)\r
+uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, SCSI_Inquiry_Response_t* InquiryData)\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_Inquiry_Response_t),\r
+                       .Flags              = COMMAND_DIRECTION_DATA_IN,\r
+                       .LUN                = LUNIndex,\r
+                       .SCSICommandLength  = 6,\r
+                       .SCSICommandData    =\r
+                               {\r
+                                       SCSI_CMD_INQUIRY,\r
+                                       0x00,                            // Reserved\r
+                                       0x00,                            // Reserved\r
+                                       0x00,                            // Reserved\r
+                                       sizeof(SCSI_Inquiry_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, InquiryData)) != 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_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_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool* DeviceReady);\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
+       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_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
+       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