Altered all endpoint/pipe stream transfers so that the new BytesProcessed parameter...
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / Lib / SCSI.c
index 598c689..0400cc2 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
+     Copyright (C) Dean Camera, 2011.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -111,6 +111,9 @@ bool SCSI_DecodeSCSICommand(void)
                case SCSI_CMD_READ_10:
                        CommandSuccess = SCSI_Command_ReadWrite_10(DATA_READ);
                        break;
                case SCSI_CMD_READ_10:
                        CommandSuccess = SCSI_Command_ReadWrite_10(DATA_READ);
                        break;
+               case SCSI_CMD_MODE_SENSE_6:
+                       CommandSuccess = SCSI_Command_ModeSense_6();
+                       break;
                case SCSI_CMD_TEST_UNIT_READY:
                case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
                case SCSI_CMD_VERIFY_10:
                case SCSI_CMD_TEST_UNIT_READY:
                case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
                case SCSI_CMD_VERIFY_10:
@@ -163,12 +166,10 @@ static bool SCSI_Command_Inquiry(void)
        }
 
        /* Write the INQUIRY data to the endpoint */
        }
 
        /* Write the INQUIRY data to the endpoint */
-       Endpoint_Write_Stream_LE(&InquiryData, BytesTransferred, StreamCallback_AbortOnMassStoreReset);
-
-       uint8_t PadBytes[AllocationLength - BytesTransferred];
+       Endpoint_Write_Stream_LE(&InquiryData, BytesTransferred, NULL);
 
        /* Pad out remaining bytes with 0x00 */
 
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), StreamCallback_AbortOnMassStoreReset);
+       Endpoint_Null_Stream((AllocationLength - BytesTransferred), NULL);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -190,12 +191,10 @@ static bool SCSI_Command_Request_Sense(void)
        uint8_t  BytesTransferred = (AllocationLength < sizeof(SenseData))? AllocationLength : sizeof(SenseData);
 
        /* Send the SENSE data - this indicates to the host the status of the last command */
        uint8_t  BytesTransferred = (AllocationLength < sizeof(SenseData))? AllocationLength : sizeof(SenseData);
 
        /* Send the SENSE data - this indicates to the host the status of the last command */
-       Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, StreamCallback_AbortOnMassStoreReset);
-
-       uint8_t PadBytes[AllocationLength - BytesTransferred];
+       Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NULL);
 
        /* Pad out remaining bytes with 0x00 */
 
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), StreamCallback_AbortOnMassStoreReset);
+       Endpoint_Null_Stream((AllocationLength - BytesTransferred), NULL);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -278,9 +277,23 @@ static bool SCSI_Command_Send_Diagnostic(void)
  */
 static bool SCSI_Command_ReadWrite_10(const bool IsDataRead)
 {
  */
 static bool SCSI_Command_ReadWrite_10(const bool IsDataRead)
 {
-       uint32_t BlockAddress = SwapEndian_32(*(uint32_t*)&CommandBlock.SCSICommandData[2]);
-       uint16_t TotalBlocks  = SwapEndian_16(*(uint16_t*)&CommandBlock.SCSICommandData[7]);
+       uint32_t BlockAddress;
+       uint16_t TotalBlocks;
+
+       /* Check if the disk is write protected or not */
+       if ((IsDataRead == DATA_WRITE) && DISK_READ_ONLY)
+       {
+               /* Block address is invalid, update SENSE key and return command fail */
+               SCSI_SET_SENSE(SCSI_SENSE_KEY_DATA_PROTECT,
+                              SCSI_ASENSE_WRITE_PROTECTED,
+                              SCSI_ASENSEQ_NO_QUALIFIER);
 
 
+               return false;           
+       }
+
+       BlockAddress = SwapEndian_32(*(uint32_t*)&CommandBlock.SCSICommandData[2]);
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&CommandBlock.SCSICommandData[7]);
+       
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
        {
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
        {
@@ -309,3 +322,22 @@ static bool SCSI_Command_ReadWrite_10(const bool IsDataRead)
        return true;
 }
 
        return true;
 }
 
+/** Command processing for an issued SCSI MODE SENSE (6) command. This command returns various informational pages about
+ *  the SCSI device, as well as the device's Write Protect status.
+ *
+ *  \return Boolean true if the command completed successfully, false otherwise.
+ */
+static bool SCSI_Command_ModeSense_6(void)
+{
+       /* Send an empty header response with the Write Protect flag status */
+       Endpoint_Write_Byte(0x00);
+       Endpoint_Write_Byte(0x00);
+       Endpoint_Write_Byte(DISK_READ_ONLY ? 0x80 : 0x00);
+       Endpoint_Write_Byte(0x00);
+       Endpoint_ClearIN();
+
+       /* Update the bytes transferred counter and succeed the command */
+       CommandBlock.DataTransferLength -= 4;
+
+       return true;
+}