Minor updates to the Benito programmer - remove redundant PORT register manipulations.
[pub/USBasp.git] / Demos / Device / ClassDriver / MassStorage / Lib / SCSI.c
index d56e343..f400e20 100644 (file)
@@ -84,14 +84,14 @@ SCSI_Request_Sense_Response_t SenseData =
  *  to the appropriate SCSI command handling routine if the issued command is supported by the device, else it returns\r
  *  a command failure due to a ILLEGAL REQUEST.\r
  *\r
  *  to the appropriate SCSI command handling routine if the issued command is supported by the device, else it returns\r
  *  a command failure due to a ILLEGAL REQUEST.\r
  *\r
- *  \param MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
+ *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
  */\r
  */\r
-bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_t* MSInterfaceInfo)\r
+bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)\r
 {\r
        bool CommandSuccess = false;\r
 \r
        /* Run the appropriate SCSI command hander function based on the passed command */\r
 {\r
        bool CommandSuccess = false;\r
 \r
        /* Run the appropriate SCSI command hander function based on the passed command */\r
-       switch (MSInterfaceInfo->CommandBlock.SCSICommandData[0])\r
+       switch (MSInterfaceInfo->State.CommandBlock.SCSICommandData[0])\r
        {\r
                case SCSI_CMD_INQUIRY:\r
                        CommandSuccess = SCSI_Command_Inquiry(MSInterfaceInfo);                 \r
        {\r
                case SCSI_CMD_INQUIRY:\r
                        CommandSuccess = SCSI_Command_Inquiry(MSInterfaceInfo);                 \r
@@ -116,7 +116,7 @@ bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_t* MSInterfaceInfo)
                case SCSI_CMD_VERIFY_10:\r
                        /* These commands should just succeed, no handling required */\r
                        CommandSuccess = true;\r
                case SCSI_CMD_VERIFY_10:\r
                        /* These commands should just succeed, no handling required */\r
                        CommandSuccess = true;\r
-                       MSInterfaceInfo->CommandBlock.DataTransferLength = 0;\r
+                       MSInterfaceInfo->State.CommandBlock.DataTransferLength = 0;\r
                        break;\r
                default:\r
                        /* Update the SENSE key to reflect the invalid command */\r
                        break;\r
                default:\r
                        /* Update the SENSE key to reflect the invalid command */\r
@@ -142,20 +142,20 @@ bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_t* MSInterfaceInfo)
 /** Command processing for an issued SCSI INQUIRY command. This command returns information about the device's features\r
  *  and capabilities to the host.\r
  *\r
 /** Command processing for an issued SCSI INQUIRY command. This command returns information about the device's features\r
  *  and capabilities to the host.\r
  *\r
- *  \param MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
+ *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
-static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_t* MSInterfaceInfo)\r
+static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)\r
 {\r
 {\r
-       uint16_t AllocationLength  = (((uint16_t)MSInterfaceInfo->CommandBlock.SCSICommandData[3] << 8) |\r
-                                                MSInterfaceInfo->CommandBlock.SCSICommandData[4]);\r
+       uint16_t AllocationLength  = (((uint16_t)MSInterfaceInfo->State.CommandBlock.SCSICommandData[3] << 8) |\r
+                                                MSInterfaceInfo->State.CommandBlock.SCSICommandData[4]);\r
        uint16_t BytesTransferred  = (AllocationLength < sizeof(InquiryData))? AllocationLength :\r
                                                                               sizeof(InquiryData);\r
 \r
        /* Only the standard INQUIRY data is supported, check if any optional INQUIRY bits set */\r
        uint16_t BytesTransferred  = (AllocationLength < sizeof(InquiryData))? AllocationLength :\r
                                                                               sizeof(InquiryData);\r
 \r
        /* Only the standard INQUIRY data is supported, check if any optional INQUIRY bits set */\r
-       if ((MSInterfaceInfo->CommandBlock.SCSICommandData[1] & ((1 << 0) | (1 << 1))) ||\r
-            MSInterfaceInfo->CommandBlock.SCSICommandData[2])\r
+       if ((MSInterfaceInfo->State.CommandBlock.SCSICommandData[1] & ((1 << 0) | (1 << 1))) ||\r
+            MSInterfaceInfo->State.CommandBlock.SCSICommandData[2])\r
        {\r
                /* Optional but unsupported bits set - update the SENSE key and fail the request */\r
                SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,\r
        {\r
                /* Optional but unsupported bits set - update the SENSE key and fail the request */\r
                SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,\r
@@ -176,7 +176,7 @@ static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_t* MSInterfaceInfo)
        Endpoint_ClearIN();\r
 \r
        /* Succeed the command and update the bytes transferred counter */\r
        Endpoint_ClearIN();\r
 \r
        /* Succeed the command and update the bytes transferred counter */\r
-       MSInterfaceInfo->CommandBlock.DataTransferLength -= BytesTransferred;\r
+       MSInterfaceInfo->State.CommandBlock.DataTransferLength -= BytesTransferred;\r
        \r
        return true;\r
 }\r
        \r
        return true;\r
 }\r
@@ -184,13 +184,13 @@ static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_t* MSInterfaceInfo)
 /** Command processing for an issued SCSI REQUEST SENSE command. This command returns information about the last issued command,\r
  *  including the error code and additional error information so that the host can determine why a command failed to complete.\r
  *\r
 /** Command processing for an issued SCSI REQUEST SENSE command. This command returns information about the last issued command,\r
  *  including the error code and additional error information so that the host can determine why a command failed to complete.\r
  *\r
- *  \param MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
+ *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
-static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_t* MSInterfaceInfo)\r
+static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)\r
 {\r
 {\r
-       uint8_t  AllocationLength = MSInterfaceInfo->CommandBlock.SCSICommandData[4];\r
+       uint8_t  AllocationLength = MSInterfaceInfo->State.CommandBlock.SCSICommandData[4];\r
        uint8_t  BytesTransferred = (AllocationLength < sizeof(SenseData))? AllocationLength : sizeof(SenseData);\r
        \r
        uint8_t PadBytes[AllocationLength - BytesTransferred];\r
        uint8_t  BytesTransferred = (AllocationLength < sizeof(SenseData))? AllocationLength : sizeof(SenseData);\r
        \r
        uint8_t PadBytes[AllocationLength - BytesTransferred];\r
@@ -200,7 +200,7 @@ static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_t* MSInterfaceInfo)
        Endpoint_ClearIN();\r
 \r
        /* Succeed the command and update the bytes transferred counter */\r
        Endpoint_ClearIN();\r
 \r
        /* Succeed the command and update the bytes transferred counter */\r
-       MSInterfaceInfo->CommandBlock.DataTransferLength -= BytesTransferred;\r
+       MSInterfaceInfo->State.CommandBlock.DataTransferLength -= BytesTransferred;\r
 \r
        return true;\r
 }\r
 \r
        return true;\r
 }\r
@@ -208,21 +208,21 @@ static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_t* MSInterfaceInfo)
 /** Command processing for an issued SCSI READ CAPACITY (10) command. This command returns information about the device's capacity\r
  *  on the selected Logical Unit (drive), as a number of OS-sized blocks.\r
  *\r
 /** Command processing for an issued SCSI READ CAPACITY (10) command. This command returns information about the device's capacity\r
  *  on the selected Logical Unit (drive), as a number of OS-sized blocks.\r
  *\r
- *  \param MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
+ *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
-static bool SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_t* MSInterfaceInfo)\r
+static bool SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)\r
 {\r
 {\r
-       uint32_t TotalLUNs      = (LUN_MEDIA_BLOCKS - 1);\r
-       uint32_t MediaBlockSize = VIRTUAL_MEMORY_BLOCK_SIZE;\r
+       uint32_t LastBlockAddressInLUN = (LUN_MEDIA_BLOCKS - 1);\r
+       uint32_t MediaBlockSize        = VIRTUAL_MEMORY_BLOCK_SIZE;\r
 \r
 \r
-       Endpoint_Write_Stream_BE(&TotalLUNs, sizeof(TotalLUNs), NO_STREAM_CALLBACK);\r
+       Endpoint_Write_Stream_BE(&LastBlockAddressInLUN, sizeof(LastBlockAddressInLUN), NO_STREAM_CALLBACK);\r
        Endpoint_Write_Stream_BE(&MediaBlockSize, sizeof(MediaBlockSize), NO_STREAM_CALLBACK);\r
        Endpoint_ClearIN();\r
        \r
        /* Succeed the command and update the bytes transferred counter */\r
        Endpoint_Write_Stream_BE(&MediaBlockSize, sizeof(MediaBlockSize), NO_STREAM_CALLBACK);\r
        Endpoint_ClearIN();\r
        \r
        /* Succeed the command and update the bytes transferred counter */\r
-       MSInterfaceInfo->CommandBlock.DataTransferLength -= 8;\r
+       MSInterfaceInfo->State.CommandBlock.DataTransferLength -= 8;\r
        \r
        return true;\r
 }\r
        \r
        return true;\r
 }\r
@@ -231,16 +231,16 @@ static bool SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_t* MSInterfaceInfo)
  *  board, and indicates if they are present and functioning correctly. Only the Self-Test portion of the diagnostic command is\r
  *  supported.\r
  *\r
  *  board, and indicates if they are present and functioning correctly. Only the Self-Test portion of the diagnostic command is\r
  *  supported.\r
  *\r
- *  \param MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
+ *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
-static bool SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_t* MSInterfaceInfo)\r
+static bool SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)\r
 {\r
        uint8_t ReturnByte;\r
 \r
        /* Check to see if the SELF TEST bit is not set */\r
 {\r
        uint8_t ReturnByte;\r
 \r
        /* Check to see if the SELF TEST bit is not set */\r
-       if (!(MSInterfaceInfo->CommandBlock.SCSICommandData[1] & (1 << 2)))\r
+       if (!(MSInterfaceInfo->State.CommandBlock.SCSICommandData[1] & (1 << 2)))\r
        {\r
                /* Only self-test supported - update SENSE key and fail the command */\r
                SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,\r
        {\r
                /* Only self-test supported - update SENSE key and fail the command */\r
                SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,\r
@@ -287,7 +287,7 @@ static bool SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_t* MSInterfaceInfo)
        #endif\r
        \r
        /* Succeed the command and update the bytes transferred counter */\r
        #endif\r
        \r
        /* Succeed the command and update the bytes transferred counter */\r
-       MSInterfaceInfo->CommandBlock.DataTransferLength = 0;\r
+       MSInterfaceInfo->State.CommandBlock.DataTransferLength = 0;\r
        \r
        return true;\r
 }\r
        \r
        return true;\r
 }\r
@@ -296,25 +296,25 @@ static bool SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_t* MSInterfaceInfo)
  *  and total number of blocks to process, then calls the appropriate low-level dataflash routine to handle the actual\r
  *  reading and writing of the data.\r
  *\r
  *  and total number of blocks to process, then calls the appropriate low-level dataflash routine to handle the actual\r
  *  reading and writing of the data.\r
  *\r
- *  \param MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
- *  \param IsDataRead  Indicates if the command is a READ (10) command or WRITE (10) command (DATA_READ or DATA_WRITE)\r
+ *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface structure that the command is associated with\r
+ *  \param[in] IsDataRead  Indicates if the command is a READ (10) command or WRITE (10) command (DATA_READ or DATA_WRITE)\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
  *\r
  *  \return Boolean true if the command completed successfully, false otherwise.\r
  */\r
-static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_t* MSInterfaceInfo, const bool IsDataRead)\r
+static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, const bool IsDataRead)\r
 {\r
        uint32_t BlockAddress;\r
        uint16_t TotalBlocks;\r
        \r
        /* Load in the 32-bit block address (SCSI uses big-endian, so have to do it byte-by-byte) */\r
 {\r
        uint32_t BlockAddress;\r
        uint16_t TotalBlocks;\r
        \r
        /* Load in the 32-bit block address (SCSI uses big-endian, so have to do it byte-by-byte) */\r
-       ((uint8_t*)&BlockAddress)[3] = MSInterfaceInfo->CommandBlock.SCSICommandData[2];\r
-       ((uint8_t*)&BlockAddress)[2] = MSInterfaceInfo->CommandBlock.SCSICommandData[3];\r
-       ((uint8_t*)&BlockAddress)[1] = MSInterfaceInfo->CommandBlock.SCSICommandData[4];\r
-       ((uint8_t*)&BlockAddress)[0] = MSInterfaceInfo->CommandBlock.SCSICommandData[5];\r
+       ((uint8_t*)&BlockAddress)[3] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[2];\r
+       ((uint8_t*)&BlockAddress)[2] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[3];\r
+       ((uint8_t*)&BlockAddress)[1] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[4];\r
+       ((uint8_t*)&BlockAddress)[0] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[5];\r
 \r
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to do it byte-by-byte) */\r
 \r
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to do it byte-by-byte) */\r
-       ((uint8_t*)&TotalBlocks)[1]  = MSInterfaceInfo->CommandBlock.SCSICommandData[7];\r
-       ((uint8_t*)&TotalBlocks)[0]  = MSInterfaceInfo->CommandBlock.SCSICommandData[8];\r
+       ((uint8_t*)&TotalBlocks)[1]  = MSInterfaceInfo->State.CommandBlock.SCSICommandData[7];\r
+       ((uint8_t*)&TotalBlocks)[0]  = MSInterfaceInfo->State.CommandBlock.SCSICommandData[8];\r
        \r
        /* Check if the block address is outside the maximum allowable value for the LUN */\r
        if (BlockAddress >= LUN_MEDIA_BLOCKS)\r
        \r
        /* Check if the block address is outside the maximum allowable value for the LUN */\r
        if (BlockAddress >= LUN_MEDIA_BLOCKS)\r
@@ -329,7 +329,7 @@ static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_t* MSInterfaceInfo, const
 \r
        #if (TOTAL_LUNS > 1)\r
        /* Adjust the given block address to the real media address based on the selected LUN */\r
 \r
        #if (TOTAL_LUNS > 1)\r
        /* Adjust the given block address to the real media address based on the selected LUN */\r
-       BlockAddress += ((uint32_t)MSInterfaceInfo->CommandBlock.LUN * LUN_MEDIA_BLOCKS);\r
+       BlockAddress += ((uint32_t)MSInterfaceInfo->State.CommandBlock.LUN * LUN_MEDIA_BLOCKS);\r
        #endif\r
        \r
        /* Determine if the packet is a READ (10) or WRITE (10) command, call appropriate function */\r
        #endif\r
        \r
        /* Determine if the packet is a READ (10) or WRITE (10) command, call appropriate function */\r
@@ -339,7 +339,7 @@ static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_t* MSInterfaceInfo, const
          DataflashManager_WriteBlocks(MSInterfaceInfo, BlockAddress, TotalBlocks);\r
 \r
        /* Update the bytes transferred counter and succeed the command */\r
          DataflashManager_WriteBlocks(MSInterfaceInfo, BlockAddress, TotalBlocks);\r
 \r
        /* Update the bytes transferred counter and succeed the command */\r
-       MSInterfaceInfo->CommandBlock.DataTransferLength -= ((uint32_t)TotalBlocks * VIRTUAL_MEMORY_BLOCK_SIZE);\r
+       MSInterfaceInfo->State.CommandBlock.DataTransferLength -= ((uint32_t)TotalBlocks * VIRTUAL_MEMORY_BLOCK_SIZE);\r
        \r
        return true;\r
 }\r
        \r
        return true;\r
 }\r