3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
33 * SCSI command processing routines, for SCSI commands issued by the host. Mass Storage
34 * devices use a thin "Bulk-Only Transport" protocol for issuing commands and status information,
35 * which wrap around standard SCSI device commands for controlling the actual storage medium.
38 #define INCLUDE_FROM_SCSI_C
41 #if defined(USB_CAN_BE_DEVICE)
42 /** Structure to hold the SCSI response data to a SCSI INQUIRY command. This gives information about the device's
43 * features and capabilities.
45 SCSI_Inquiry_Response_t InquiryData
=
47 .DeviceType
= DEVICE_TYPE_BLOCK
,
48 .PeripheralQualifier
= 0,
54 .ResponseDataFormat
= 2,
59 .AdditionalLength
= 0x1F,
65 .WideBus16Bit
= false,
66 .WideBus32Bit
= false,
70 .ProductID
= "Dataflash Disk",
71 .RevisionID
= {'0','.','0','0'},
74 /** Structure to hold the sense data for the last issued SCSI command, which is returned to the host after a SCSI REQUEST SENSE
75 * command is issued. This gives information on exactly why the last command failed to complete.
77 SCSI_Request_Sense_Response_t SenseData
=
80 .AdditionalLength
= 0x0A,
84 /** Main routine to process the SCSI command located in the Command Block Wrapper read from the host. This dispatches
85 * to the appropriate SCSI command handling routine if the issued command is supported by the device, else it returns
86 * a command failure due to a ILLEGAL REQUEST.
88 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
90 bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
92 /* Set initial sense data, before the requested command is processed */
93 SCSI_SET_SENSE(SCSI_SENSE_KEY_GOOD
,
94 SCSI_ASENSE_NO_ADDITIONAL_INFORMATION
,
95 SCSI_ASENSEQ_NO_QUALIFIER
);
97 /* Run the appropriate SCSI command hander function based on the passed command */
98 switch (MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[0])
100 case SCSI_CMD_INQUIRY
:
101 SCSI_Command_Inquiry(MSInterfaceInfo
);
103 case SCSI_CMD_REQUEST_SENSE
:
104 SCSI_Command_Request_Sense(MSInterfaceInfo
);
106 case SCSI_CMD_READ_CAPACITY_10
:
107 SCSI_Command_Read_Capacity_10(MSInterfaceInfo
);
109 case SCSI_CMD_SEND_DIAGNOSTIC
:
110 SCSI_Command_Send_Diagnostic(MSInterfaceInfo
);
112 case SCSI_CMD_WRITE_10
:
113 SCSI_Command_ReadWrite_10(MSInterfaceInfo
, DATA_WRITE
);
115 case SCSI_CMD_READ_10
:
116 SCSI_Command_ReadWrite_10(MSInterfaceInfo
, DATA_READ
);
118 case SCSI_CMD_TEST_UNIT_READY
:
119 case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
:
120 case SCSI_CMD_VERIFY_10
:
121 /* These commands should just succeed, no handling required */
122 MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
= 0;
125 /* Update the SENSE key to reflect the invalid command */
126 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
,
127 SCSI_ASENSE_INVALID_COMMAND
,
128 SCSI_ASENSEQ_NO_QUALIFIER
);
132 return (SenseData
.SenseKey
== SCSI_SENSE_KEY_GOOD
);
135 /** Command processing for an issued SCSI INQUIRY command. This command returns information about the device's features
136 * and capabilities to the host.
138 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
140 static void SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
142 uint16_t AllocationLength
= (((uint16_t)MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[3] << 8) |
143 MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[4]);
144 uint16_t BytesTransferred
= (AllocationLength
< sizeof(InquiryData
))? AllocationLength
:
147 /* Only the standard INQUIRY data is supported, check if any optional INQUIRY bits set */
148 if ((MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[1] & ((1 << 0) | (1 << 1))) ||
149 MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[2])
151 /* Optional but unsupported bits set - update the SENSE key and fail the request */
152 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
,
153 SCSI_ASENSE_INVALID_FIELD_IN_CDB
,
154 SCSI_ASENSEQ_NO_QUALIFIER
);
159 Endpoint_Write_Stream_LE(&InquiryData
, BytesTransferred
, NO_STREAM_CALLBACK
);
161 uint8_t PadBytes
[AllocationLength
- BytesTransferred
];
163 /* Pad out remaining bytes with 0x00 */
164 Endpoint_Write_Stream_LE(&PadBytes
, (AllocationLength
- BytesTransferred
), NO_STREAM_CALLBACK
);
166 /* Finalize the stream transfer to send the last packet */
169 /* Succeed the command and update the bytes transferred counter */
170 MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
-= BytesTransferred
;
173 /** Command processing for an issued SCSI REQUEST SENSE command. This command returns information about the last issued command,
174 * including the error code and additional error information so that the host can determine why a command failed to complete.
176 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
178 static void SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
180 uint8_t AllocationLength
= MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[4];
181 uint8_t BytesTransferred
= (AllocationLength
< sizeof(SenseData
))? AllocationLength
: sizeof(SenseData
);
183 uint8_t PadBytes
[AllocationLength
- BytesTransferred
];
185 Endpoint_Write_Stream_LE(&SenseData
, BytesTransferred
, NO_STREAM_CALLBACK
);
186 Endpoint_Write_Stream_LE(&PadBytes
, (AllocationLength
- BytesTransferred
), NO_STREAM_CALLBACK
);
189 /* Succeed the command and update the bytes transferred counter */
190 MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
-= BytesTransferred
;
193 /** Command processing for an issued SCSI READ CAPACITY (10) command. This command returns information about the device's capacity
194 * on the selected Logical Unit (drive), as a number of OS-sized blocks.
196 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
198 static void SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
200 uint32_t LastBlockAddressInLUN
= (VIRTUAL_MEMORY_BLOCKS
- 1);
201 uint32_t MediaBlockSize
= VIRTUAL_MEMORY_BLOCK_SIZE
;
203 Endpoint_Write_Stream_BE(&LastBlockAddressInLUN
, sizeof(LastBlockAddressInLUN
), NO_STREAM_CALLBACK
);
204 Endpoint_Write_Stream_BE(&MediaBlockSize
, sizeof(MediaBlockSize
), NO_STREAM_CALLBACK
);
207 /* Succeed the command and update the bytes transferred counter */
208 MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
-= 8;
211 /** Command processing for an issued SCSI SEND DIAGNOSTIC command. This command performs a quick check of the Dataflash ICs on the
212 * board, and indicates if they are present and functioning correctly. Only the Self-Test portion of the diagnostic command is
215 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
217 static void SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
)
219 /* Check to see if the SELF TEST bit is not set */
220 if (!(MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[1] & (1 << 2)))
222 /* Only self-test supported - update SENSE key and fail the command */
223 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
,
224 SCSI_ASENSE_INVALID_FIELD_IN_CDB
,
225 SCSI_ASENSEQ_NO_QUALIFIER
);
230 /* Check to see if all attached Dataflash ICs are functional */
231 if (!(DataflashManager_CheckDataflashOperation()))
233 /* Update SENSE key with a hardware error condition and return command fail */
234 SCSI_SET_SENSE(SCSI_SENSE_KEY_HARDWARE_ERROR
,
235 SCSI_ASENSE_NO_ADDITIONAL_INFORMATION
,
236 SCSI_ASENSEQ_NO_QUALIFIER
);
241 /* Succeed the command and update the bytes transferred counter */
242 MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
= 0;
245 /** Command processing for an issued SCSI READ (10) or WRITE (10) command. This command reads in the block start address
246 * and total number of blocks to process, then calls the appropriate low-level dataflash routine to handle the actual
247 * reading and writing of the data.
249 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
250 * \param[in] IsDataRead Indicates if the command is a READ (10) command or WRITE (10) command (DATA_READ or DATA_WRITE)
252 static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
, const bool IsDataRead
)
254 uint32_t BlockAddress
;
255 uint16_t TotalBlocks
;
257 /* Load in the 32-bit block address (SCSI uses big-endian, so have to reverse the byte order) */
258 BlockAddress
= SwapEndian_32(*(uint32_t*)&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[2]);
260 /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
261 TotalBlocks
= SwapEndian_16(*(uint32_t*)&MSInterfaceInfo
->State
.CommandBlock
.SCSICommandData
[7]);
263 /* Check if the block address is outside the maximum allowable value for the LUN */
264 if (BlockAddress
>= VIRTUAL_MEMORY_BLOCKS
)
266 /* Block address is invalid, update SENSE key and return command fail */
267 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST
,
268 SCSI_ASENSE_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE
,
269 SCSI_ASENSEQ_NO_QUALIFIER
);
274 /* Determine if the packet is a READ (10) or WRITE (10) command, call appropriate function */
275 if (IsDataRead
== DATA_READ
)
276 DataflashManager_ReadBlocks(MSInterfaceInfo
, BlockAddress
, TotalBlocks
);
278 DataflashManager_WriteBlocks(MSInterfaceInfo
, BlockAddress
, TotalBlocks
);
280 /* Update the bytes transferred counter and succeed the command */
281 MSInterfaceInfo
->State
.CommandBlock
.DataTransferLength
-= ((uint32_t)TotalBlocks
* VIRTUAL_MEMORY_BLOCK_SIZE
);