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 * Header file for MassStoreCommands.c.
36 #ifndef _MASS_STORE_COMMANDS_H_
37 #define _MASS_STORE_COMMANDS_H_
42 #include "MassStorageHost.h"
43 #include "SCSI_Codes.h"
45 #include <LUFA/Drivers/USB/USB.h>
48 /** Class specific request to reset the Mass Storage interface of the attached device. */
49 #define REQ_MassStorageReset 0xFF
51 /** Class specific request to retrieve the maximum Logical Unit Number (LUN) index of the attached device. */
52 #define REQ_GetMaxLUN 0xFE
54 /** Command Block Wrapper signature byte, for verification of valid CBW blocks. */
55 #define CBW_SIGNATURE 0x43425355UL
57 /** Command Static Wrapper signature byte, for verification of valid CSW blocks. */
58 #define CSW_SIGNATURE 0x53425355UL
60 /** Data direction mask for the Flags field of a CBW, indicating Host-to-Device transfer direction. */
61 #define COMMAND_DIRECTION_DATA_OUT (0 << 7)
63 /** Data direction mask for the Flags field of a CBW, indicating Device-to-Host transfer direction. */
64 #define COMMAND_DIRECTION_DATA_IN (1 << 7)
66 /** Timeout period between the issuing of a CBW to a device, and the reception of the first packet. */
67 #define COMMAND_DATA_TIMEOUT_MS 10000
69 /** Additional error code for Mass Storage functions when a device returns a logical command failure. */
70 #define MASS_STORE_SCSI_COMMAND_FAILED 0xC0
73 /** Type define for a Mass Storage class Command Block Wrapper, used to wrap SCSI
74 * commands for transport over the USB bulk endpoints to the device.
78 uint32_t Signature
; /**< Command block signature, always equal to CBW_SIGNATURE */
79 uint32_t Tag
; /**< Current CBW tag, to positively associate a CBW with a CSW (filled automatically) */
80 uint32_t DataTransferLength
; /**< Length of data to transfer, following the CBW */
81 uint8_t Flags
; /**< Block flags, equal to one of the COMMAND_DIRECTION_DATA_* macros */
82 uint8_t LUN
; /**< Logical Unit Number the CBW is addressed to in the device */
83 uint8_t SCSICommandLength
; /**< Length of the SCSI command in the CBW */
84 uint8_t SCSICommandData
[16]; /**< SCSI command to issue to the device */
85 } CommandBlockWrapper_t
;
87 /** Type define for a Mass Storage class Command Status Wrapper, used to wrap SCSI
88 * responses for transport over the USB bulk endpoints from the device.
92 uint32_t Signature
; /**< Command status signature, always equal to CSW_SIGNATURE */
93 uint32_t Tag
; /**< Current CBW tag, to positively associate a CBW with a CSW */
94 uint32_t DataTransferResidue
; /**< Length of data not transferred */
95 uint8_t Status
; /**< Command status, a value from the MassStorageHost_CommandStatusCodes_t enum */
96 } CommandStatusWrapper_t
;
98 /** Type define for a SCSI Sense structure. Structures of this type are filled out by the
99 * device via the \ref MassStore_RequestSense() function, indicating the current sense data of the
100 * device (giving explicit error codes for the last issued command). For details of the
101 * structure contents, refer to the SCSI specifications.
105 uint8_t ResponseCode
;
107 uint8_t SegmentNumber
;
109 unsigned char SenseKey
: 4;
110 unsigned char Reserved
: 1;
111 unsigned char ILI
: 1;
112 unsigned char EOM
: 1;
113 unsigned char FileMark
: 1;
115 uint8_t Information
[4];
116 uint8_t AdditionalLength
;
117 uint8_t CmdSpecificInformation
[4];
118 uint8_t AdditionalSenseCode
;
119 uint8_t AdditionalSenseQualifier
;
120 uint8_t FieldReplaceableUnitCode
;
121 uint8_t SenseKeySpecific
[3];
122 } SCSI_Request_Sense_Response_t
;
124 /** Type define for a SCSI Inquiry structure. Structures of this type are filled out by the
125 * device via the \ref MassStore_Inquiry() function, retrieving the attached device's information.
126 * For details of the structure contents, refer to the SCSI specifications.
130 unsigned char DeviceType
: 5;
131 unsigned char PeripheralQualifier
: 3;
133 unsigned char Reserved
: 7;
134 unsigned char Removable
: 1;
138 unsigned char ResponseDataFormat
: 4;
139 unsigned char Reserved2
: 1;
140 unsigned char NormACA
: 1;
141 unsigned char TrmTsk
: 1;
142 unsigned char AERC
: 1;
144 uint8_t AdditionalLength
;
145 uint8_t Reserved3
[2];
147 unsigned char SoftReset
: 1;
148 unsigned char CmdQue
: 1;
149 unsigned char Reserved4
: 1;
150 unsigned char Linked
: 1;
151 unsigned char Sync
: 1;
152 unsigned char WideBus16Bit
: 1;
153 unsigned char WideBus32Bit
: 1;
154 unsigned char RelAddr
: 1;
157 uint8_t ProductID
[16];
158 uint8_t RevisionID
[4];
159 } SCSI_Inquiry_Response_t
;
161 /** SCSI capacity structure, to hold the total capacity of the device in both the number
162 * of blocks in the current LUN, and the size of each block. This structure is filled by
163 * the device when the \ref MassStore_ReadCapacity() function is called.
167 uint32_t Blocks
; /**< Number of blocks in the addressed LUN of the device */
168 uint32_t BlockSize
; /**< Number of bytes in each block in the addressed LUN */
172 /** CSW status return codes, indicating the overall status of the issued CBW. */
173 enum MassStorageHost_CommandStatusCodes_t
175 Command_Pass
= 0, /**< Command completed successfully */
176 Command_Fail
= 1, /**< Command failed to complete successfully */
177 Phase_Error
= 2 /**< Phase error while processing the issued command */
180 /* Function Prototypes: */
181 #if defined(INCLUDE_FROM_MASSSTORE_COMMANDS_C)
182 static uint8_t MassStore_SendCommand(CommandBlockWrapper_t
* const SCSICommandBlock
,
184 static uint8_t MassStore_WaitForDataReceived(void);
185 static uint8_t MassStore_SendReceiveData(CommandBlockWrapper_t
* const SCSICommandBlock
,
186 void* BufferPtr
) ATTR_NON_NULL_PTR_ARG(1);
187 static uint8_t MassStore_GetReturnedStatus(CommandStatusWrapper_t
* const SCSICommandStatus
) ATTR_NON_NULL_PTR_ARG(1);
190 uint8_t MassStore_MassStorageReset(void);
191 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex
);
192 uint8_t MassStore_RequestSense(const uint8_t LUNIndex
,
193 SCSI_Request_Sense_Response_t
* const SensePtr
) ATTR_NON_NULL_PTR_ARG(2);
194 uint8_t MassStore_Inquiry(const uint8_t LUNIndex
,
195 SCSI_Inquiry_Response_t
* const InquiryPtr
) ATTR_NON_NULL_PTR_ARG(2);
196 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex
,
197 const uint32_t BlockAddress
,
198 const uint8_t Blocks
,
199 const uint16_t BlockSize
,
200 void* BufferPtr
) ATTR_NON_NULL_PTR_ARG(5);
201 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex
,
202 const uint32_t BlockAddress
,
203 const uint8_t Blocks
,
204 const uint16_t BlockSize
,
205 void* BufferPtr
) ATTR_NON_NULL_PTR_ARG(5);
206 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex
,
207 SCSI_Capacity_t
* const CapacityPtr
) ATTR_NON_NULL_PTR_ARG(2);
208 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex
);
209 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex
,
210 const bool PreventRemoval
);