a23e211099242f84bec6a5c537342a2d0bf874a6
[pub/USBasp.git] / Projects / Incomplete / StandaloneProgrammer / Lib / SCSI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
36 */
37
38 #define INCLUDE_FROM_SCSI_C
39 #include "SCSI.h"
40
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.
44 */
45 SCSI_Inquiry_Response_t InquiryData =
46 {
47 .DeviceType = DEVICE_TYPE_BLOCK,
48 .PeripheralQualifier = 0,
49
50 .Removable = true,
51
52 .Version = 0,
53
54 .ResponseDataFormat = 2,
55 .NormACA = false,
56 .TrmTsk = false,
57 .AERC = false,
58
59 .AdditionalLength = 0x1F,
60
61 .SoftReset = false,
62 .CmdQue = false,
63 .Linked = false,
64 .Sync = false,
65 .WideBus16Bit = false,
66 .WideBus32Bit = false,
67 .RelAddr = false,
68
69 .VendorID = "LUFA",
70 .ProductID = "Dataflash Disk",
71 .RevisionID = {'0','.','0','0'},
72 };
73
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.
76 */
77 SCSI_Request_Sense_Response_t SenseData =
78 {
79 .ResponseCode = 0x70,
80 .AdditionalLength = 0x0A,
81 };
82
83
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.
87 *
88 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
89 *
90 * \return Boolean true if the command completed successfully, false otherwise
91 */
92 bool SCSI_DecodeSCSICommand(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
93 {
94 bool CommandSuccess = false;
95
96 /* Run the appropriate SCSI command hander function based on the passed command */
97 switch (MSInterfaceInfo->State.CommandBlock.SCSICommandData[0])
98 {
99 case SCSI_CMD_INQUIRY:
100 CommandSuccess = SCSI_Command_Inquiry(MSInterfaceInfo);
101 break;
102 case SCSI_CMD_REQUEST_SENSE:
103 CommandSuccess = SCSI_Command_Request_Sense(MSInterfaceInfo);
104 break;
105 case SCSI_CMD_READ_CAPACITY_10:
106 CommandSuccess = SCSI_Command_Read_Capacity_10(MSInterfaceInfo);
107 break;
108 case SCSI_CMD_SEND_DIAGNOSTIC:
109 CommandSuccess = SCSI_Command_Send_Diagnostic(MSInterfaceInfo);
110 break;
111 case SCSI_CMD_WRITE_10:
112 CommandSuccess = SCSI_Command_ReadWrite_10(MSInterfaceInfo, DATA_WRITE);
113 break;
114 case SCSI_CMD_READ_10:
115 CommandSuccess = SCSI_Command_ReadWrite_10(MSInterfaceInfo, DATA_READ);
116 break;
117 case SCSI_CMD_MODE_SENSE_6:
118 CommandSuccess = SCSI_Command_ModeSense_6(MSInterfaceInfo);
119 break;
120 case SCSI_CMD_TEST_UNIT_READY:
121 case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
122 case SCSI_CMD_VERIFY_10:
123 /* These commands should just succeed, no handling required */
124 CommandSuccess = true;
125 MSInterfaceInfo->State.CommandBlock.DataTransferLength = 0;
126 break;
127 default:
128 /* Update the SENSE key to reflect the invalid command */
129 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
130 SCSI_ASENSE_INVALID_COMMAND,
131 SCSI_ASENSEQ_NO_QUALIFIER);
132 break;
133 }
134
135 /* Check if command was successfully processed */
136 if (CommandSuccess)
137 {
138 SCSI_SET_SENSE(SCSI_SENSE_KEY_GOOD,
139 SCSI_ASENSE_NO_ADDITIONAL_INFORMATION,
140 SCSI_ASENSEQ_NO_QUALIFIER);
141
142 return true;
143 }
144
145 return false;
146 }
147
148 /** Command processing for an issued SCSI INQUIRY command. This command returns information about the device's features
149 * and capabilities to the host.
150 *
151 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
152 *
153 * \return Boolean true if the command completed successfully, false otherwise.
154 */
155 static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
156 {
157 uint16_t AllocationLength = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[3]);
158 uint16_t BytesTransferred = (AllocationLength < sizeof(InquiryData))? AllocationLength :
159 sizeof(InquiryData);
160
161 /* Only the standard INQUIRY data is supported, check if any optional INQUIRY bits set */
162 if ((MSInterfaceInfo->State.CommandBlock.SCSICommandData[1] & ((1 << 0) | (1 << 1))) ||
163 MSInterfaceInfo->State.CommandBlock.SCSICommandData[2])
164 {
165 /* Optional but unsupported bits set - update the SENSE key and fail the request */
166 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
167 SCSI_ASENSE_INVALID_FIELD_IN_CDB,
168 SCSI_ASENSEQ_NO_QUALIFIER);
169
170 return false;
171 }
172
173 Endpoint_Write_Stream_LE(&InquiryData, BytesTransferred, NO_STREAM_CALLBACK);
174
175 uint8_t PadBytes[AllocationLength - BytesTransferred];
176
177 /* Pad out remaining bytes with 0x00 */
178 Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
179
180 /* Finalize the stream transfer to send the last packet */
181 Endpoint_ClearIN();
182
183 /* Succeed the command and update the bytes transferred counter */
184 MSInterfaceInfo->State.CommandBlock.DataTransferLength -= BytesTransferred;
185
186 return true;
187 }
188
189 /** Command processing for an issued SCSI REQUEST SENSE command. This command returns information about the last issued command,
190 * including the error code and additional error information so that the host can determine why a command failed to complete.
191 *
192 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
193 *
194 * \return Boolean true if the command completed successfully, false otherwise.
195 */
196 static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
197 {
198 uint8_t AllocationLength = MSInterfaceInfo->State.CommandBlock.SCSICommandData[4];
199 uint8_t BytesTransferred = (AllocationLength < sizeof(SenseData))? AllocationLength : sizeof(SenseData);
200
201 uint8_t PadBytes[AllocationLength - BytesTransferred];
202
203 Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
204 Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
205 Endpoint_ClearIN();
206
207 /* Succeed the command and update the bytes transferred counter */
208 MSInterfaceInfo->State.CommandBlock.DataTransferLength -= BytesTransferred;
209
210 return true;
211 }
212
213 /** Command processing for an issued SCSI READ CAPACITY (10) command. This command returns information about the device's capacity
214 * on the selected Logical Unit (drive), as a number of OS-sized blocks.
215 *
216 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
217 *
218 * \return Boolean true if the command completed successfully, false otherwise.
219 */
220 static bool SCSI_Command_Read_Capacity_10(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
221 {
222 uint32_t LastBlockAddressInLUN = (VIRTUAL_MEMORY_BLOCKS - 1);
223 uint32_t MediaBlockSize = VIRTUAL_MEMORY_BLOCK_SIZE;
224
225 Endpoint_Write_Stream_BE(&LastBlockAddressInLUN, sizeof(LastBlockAddressInLUN), NO_STREAM_CALLBACK);
226 Endpoint_Write_Stream_BE(&MediaBlockSize, sizeof(MediaBlockSize), NO_STREAM_CALLBACK);
227 Endpoint_ClearIN();
228
229 /* Succeed the command and update the bytes transferred counter */
230 MSInterfaceInfo->State.CommandBlock.DataTransferLength -= 8;
231
232 return true;
233 }
234
235 /** Command processing for an issued SCSI SEND DIAGNOSTIC command. This command performs a quick check of the Dataflash ICs on the
236 * board, and indicates if they are present and functioning correctly. Only the Self-Test portion of the diagnostic command is
237 * supported.
238 *
239 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
240 *
241 * \return Boolean true if the command completed successfully, false otherwise.
242 */
243 static bool SCSI_Command_Send_Diagnostic(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
244 {
245 /* Check to see if the SELF TEST bit is not set */
246 if (!(MSInterfaceInfo->State.CommandBlock.SCSICommandData[1] & (1 << 2)))
247 {
248 /* Only self-test supported - update SENSE key and fail the command */
249 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
250 SCSI_ASENSE_INVALID_FIELD_IN_CDB,
251 SCSI_ASENSEQ_NO_QUALIFIER);
252
253 return false;
254 }
255
256 /* Check to see if all attached Dataflash ICs are functional */
257 if (!(DataflashManager_CheckDataflashOperation()))
258 {
259 /* Update SENSE key with a hardware error condition and return command fail */
260 SCSI_SET_SENSE(SCSI_SENSE_KEY_HARDWARE_ERROR,
261 SCSI_ASENSE_NO_ADDITIONAL_INFORMATION,
262 SCSI_ASENSEQ_NO_QUALIFIER);
263
264 return false;
265 }
266
267 /* Succeed the command and update the bytes transferred counter */
268 MSInterfaceInfo->State.CommandBlock.DataTransferLength = 0;
269
270 return true;
271 }
272
273 /** Command processing for an issued SCSI READ (10) or WRITE (10) command. This command reads in the block start address
274 * and total number of blocks to process, then calls the appropriate low-level Dataflash routine to handle the actual
275 * reading and writing of the data.
276 *
277 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
278 * \param[in] IsDataRead Indicates if the command is a READ (10) command or WRITE (10) command (DATA_READ or DATA_WRITE)
279 *
280 * \return Boolean true if the command completed successfully, false otherwise.
281 */
282 static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
283 const bool IsDataRead)
284 {
285 uint32_t BlockAddress;
286 uint16_t TotalBlocks;
287
288 /* Check if the disk is write protected or not */
289 if ((IsDataRead == DATA_WRITE) && DISK_READ_ONLY)
290 {
291 /* Block address is invalid, update SENSE key and return command fail */
292 SCSI_SET_SENSE(SCSI_SENSE_KEY_DATA_PROTECT,
293 SCSI_ASENSE_WRITE_PROTECTED,
294 SCSI_ASENSEQ_NO_QUALIFIER);
295
296 return false;
297 }
298
299 /* Load in the 32-bit block address (SCSI uses big-endian, so have to reverse the byte order) */
300 BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
301
302 /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
303 TotalBlocks = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
304
305 /* Check if the block address is outside the maximum allowable value for the LUN */
306 if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)
307 {
308 /* Block address is invalid, update SENSE key and return command fail */
309 SCSI_SET_SENSE(SCSI_SENSE_KEY_ILLEGAL_REQUEST,
310 SCSI_ASENSE_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE,
311 SCSI_ASENSEQ_NO_QUALIFIER);
312
313 return false;
314 }
315
316 /* Determine if the packet is a READ (10) or WRITE (10) command, call appropriate function */
317 if (IsDataRead == DATA_READ)
318 DataflashManager_ReadBlocks(MSInterfaceInfo, BlockAddress, TotalBlocks);
319 else
320 DataflashManager_WriteBlocks(MSInterfaceInfo, BlockAddress, TotalBlocks);
321
322 /* Update the bytes transferred counter and succeed the command */
323 MSInterfaceInfo->State.CommandBlock.DataTransferLength -= ((uint32_t)TotalBlocks * VIRTUAL_MEMORY_BLOCK_SIZE);
324
325 return true;
326 }
327
328 /** Command processing for an issued SCSI MODE SENSE (6) command. This command returns various informational pages about
329 * the SCSI device, as well as the device's Write Protect status.
330 *
331 * \param[in] MSInterfaceInfo Pointer to the Mass Storage class interface structure that the command is associated with
332 *
333 * \return Boolean true if the command completed successfully, false otherwise.
334 */
335 static bool SCSI_Command_ModeSense_6(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
336 {
337 /* Send an empty header response with the Write Protect flag status */
338 Endpoint_Write_Byte(0x00);
339 Endpoint_Write_Byte(0x00);
340 Endpoint_Write_Byte(DISK_READ_ONLY ? 0x80 : 0x00);
341 Endpoint_Write_Byte(0x00);
342 Endpoint_ClearIN();
343
344 /* Update the bytes transferred counter and succeed the command */
345 MSInterfaceInfo->State.CommandBlock.DataTransferLength -= 4;
346
347 return true;
348 }
349 #endif