7f321350f2afc603771f66691f15430f8f97793a
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Common / MassStorage.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
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 * \brief Common definitions and declarations for the library USB Mass Storage Class driver.
33 *
34 * \note This file should not be included directly. It is automatically included as needed by the class driver
35 * dispatch header located in LUFA/Drivers/USB/Class/MassStorage.h.
36 */
37
38 /** \ingroup Group_USBClassMS
39 * @defgroup Group_USBClassMSCommon Common Class Definitions
40 *
41 * \section Module Description
42 * Constants, Types and Enum definitions that are common to both Device and Host modes for the USB
43 * Mass Storage Class.
44 *
45 * @{
46 */
47
48 #ifndef _MS_CLASS_COMMON_H_
49 #define _MS_CLASS_COMMON_H_
50
51 /* Includes: */
52 #include "../../USB.h"
53
54 #include <string.h>
55
56 /* Enable C linkage for C++ Compilers: */
57 #if defined(__cplusplus)
58 extern "C" {
59 #endif
60
61 /* Preprocessor Checks: */
62 #if !defined(__INCLUDE_FROM_MS_DRIVER)
63 #error Do not include this file directly. Include LUFA/Drivers/Class/MassStorage.h instead.
64 #endif
65
66 /* Macros: */
67 /** Mass Storage Class specific request to reset the Mass Storage interface, ready for the next command. */
68 #define REQ_MassStorageReset 0xFF
69
70 /** Mass Storage Class specific request to retrieve the total number of Logical Units (drives) in the SCSI device. */
71 #define REQ_GetMaxLUN 0xFE
72
73 /** Magic signature for a Command Block Wrapper used in the Mass Storage Bulk-Only transport protocol. */
74 #define MS_CBW_SIGNATURE 0x43425355UL
75
76 /** Magic signature for a Command Status Wrapper used in the Mass Storage Bulk-Only transport protocol. */
77 #define MS_CSW_SIGNATURE 0x53425355UL
78
79 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from host-to-device. */
80 #define MS_COMMAND_DIR_DATA_OUT (0 << 7)
81
82 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from device-to-host. */
83 #define MS_COMMAND_DIR_DATA_IN (1 << 7)
84
85 /** SCSI Command Code for an INQUIRY command. */
86 #define SCSI_CMD_INQUIRY 0x12
87
88 /** SCSI Command Code for a REQUEST SENSE command. */
89 #define SCSI_CMD_REQUEST_SENSE 0x03
90
91 /** SCSI Command Code for a TEST UNIT READY command. */
92 #define SCSI_CMD_TEST_UNIT_READY 0x00
93
94 /** SCSI Command Code for a READ CAPACITY (10) command. */
95 #define SCSI_CMD_READ_CAPACITY_10 0x25
96
97 /** SCSI Command Code for a SEND DIAGNOSTIC command. */
98 #define SCSI_CMD_SEND_DIAGNOSTIC 0x1D
99
100 /** SCSI Command Code for a PREVENT ALLOW MEDIUM REMOVAL command. */
101 #define SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1E
102
103 /** SCSI Command Code for a WRITE (10) command. */
104 #define SCSI_CMD_WRITE_10 0x2A
105
106 /** SCSI Command Code for a READ (10) command. */
107 #define SCSI_CMD_READ_10 0x28
108
109 /** SCSI Command Code for a WRITE (6) command. */
110 #define SCSI_CMD_WRITE_6 0x0A
111
112 /** SCSI Command Code for a READ (6) command. */
113 #define SCSI_CMD_READ_6 0x08
114
115 /** SCSI Command Code for a VERIFY (10) command. */
116 #define SCSI_CMD_VERIFY_10 0x2F
117
118 /** SCSI Command Code for a MODE SENSE (6) command. */
119 #define SCSI_CMD_MODE_SENSE_6 0x1A
120
121 /** SCSI Command Code for a MODE SENSE (10) command. */
122 #define SCSI_CMD_MODE_SENSE_10 0x5A
123
124 /** SCSI Sense Code to indicate no error has occurred. */
125 #define SCSI_SENSE_KEY_GOOD 0x00
126
127 /** SCSI Sense Code to indicate that the device has recovered from an error. */
128 #define SCSI_SENSE_KEY_RECOVERED_ERROR 0x01
129
130 /** SCSI Sense Code to indicate that the device is not ready for a new command. */
131 #define SCSI_SENSE_KEY_NOT_READY 0x02
132
133 /** SCSI Sense Code to indicate an error whilst accessing the medium. */
134 #define SCSI_SENSE_KEY_MEDIUM_ERROR 0x03
135
136 /** SCSI Sense Code to indicate a hardware has occurred. */
137 #define SCSI_SENSE_KEY_HARDWARE_ERROR 0x04
138
139 /** SCSI Sense Code to indicate that an illegal request has been issued. */
140 #define SCSI_SENSE_KEY_ILLEGAL_REQUEST 0x05
141
142 /** SCSI Sense Code to indicate that the unit requires attention from the host to indicate
143 * a reset event, medium removal or other condition.
144 */
145 #define SCSI_SENSE_KEY_UNIT_ATTENTION 0x06
146
147 /** SCSI Sense Code to indicate that a write attempt on a protected block has been made. */
148 #define SCSI_SENSE_KEY_DATA_PROTECT 0x07
149
150 /** SCSI Sense Code to indicate an error while trying to write to a write-once medium. */
151 #define SCSI_SENSE_KEY_BLANK_CHECK 0x08
152
153 /** SCSI Sense Code to indicate a vendor specific error has occurred. */
154 #define SCSI_SENSE_KEY_VENDOR_SPECIFIC 0x09
155
156 /** SCSI Sense Code to indicate that an EXTENDED COPY command has aborted due to an error. */
157 #define SCSI_SENSE_KEY_COPY_ABORTED 0x0A
158
159 /** SCSI Sense Code to indicate that the device has aborted the issued command. */
160 #define SCSI_SENSE_KEY_ABORTED_COMMAND 0x0B
161
162 /** SCSI Sense Code to indicate an attempt to write past the end of a partition has been made. */
163 #define SCSI_SENSE_KEY_VOLUME_OVERFLOW 0x0D
164
165 /** SCSI Sense Code to indicate that the source data did not match the data read from the medium. */
166 #define SCSI_SENSE_KEY_MISCOMPARE 0x0E
167
168 /** SCSI Additional Sense Code to indicate no additional sense information is available. */
169 #define SCSI_ASENSE_NO_ADDITIONAL_INFORMATION 0x00
170
171 /** SCSI Additional Sense Code to indicate that the logical unit (LUN) addressed is not ready. */
172 #define SCSI_ASENSE_LOGICAL_UNIT_NOT_READY 0x04
173
174 /** SCSI Additional Sense Code to indicate an invalid field was encountered while processing the issued command. */
175 #define SCSI_ASENSE_INVALID_FIELD_IN_CDB 0x24
176
177 /** SCSI Additional Sense Code to indicate that an attempt to write to a protected area was made. */
178 #define SCSI_ASENSE_WRITE_PROTECTED 0x27
179
180 /** SCSI Additional Sense Code to indicate an error whilst formatting the device medium. */
181 #define SCSI_ASENSE_FORMAT_ERROR 0x31
182
183 /** SCSI Additional Sense Code to indicate an invalid command was issued. */
184 #define SCSI_ASENSE_INVALID_COMMAND 0x20
185
186 /** SCSI Additional Sense Code to indicate a write to a block out outside of the medium's range was issued. */
187 #define SCSI_ASENSE_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x21
188
189 /** SCSI Additional Sense Code to indicate that no removable medium is inserted into the device. */
190 #define SCSI_ASENSE_MEDIUM_NOT_PRESENT 0x3A
191
192 /** SCSI Additional Sense Qualifier Code to indicate no additional sense qualifier information is available. */
193 #define SCSI_ASENSEQ_NO_QUALIFIER 0x00
194
195 /** SCSI Additional Sense Qualifier Code to indicate that a medium format command failed to complete. */
196 #define SCSI_ASENSEQ_FORMAT_COMMAND_FAILED 0x01
197
198 /** SCSI Additional Sense Qualifier Code to indicate that an initializing command must be issued before the issued
199 * command can be executed.
200 */
201 #define SCSI_ASENSEQ_INITIALIZING_COMMAND_REQUIRED 0x02
202
203 /** SCSI Additional Sense Qualifier Code to indicate that an operation is currently in progress. */
204 #define SCSI_ASENSEQ_OPERATION_IN_PROGRESS 0x07
205
206 /* Type defines: */
207 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
208 typedef struct
209 {
210 uint32_t Signature; /**< Command block signature, must be CBW_SIGNATURE to indicate a valid Command Block */
211 uint32_t Tag; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
212 uint32_t DataTransferLength; /** Length of the optional data portion of the issued command, in bytes */
213 uint8_t Flags; /**< Command block flags, indicating command data direction */
214 uint8_t LUN; /**< Logical Unit number this command is issued to */
215 uint8_t SCSICommandLength; /**< Length of the issued SCSI command within the SCSI command data array */
216 uint8_t SCSICommandData[16]; /**< Issued SCSI command in the Command Block */
217 } MS_CommandBlockWrapper_t;
218
219 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
220 typedef struct
221 {
222 uint32_t Signature; /**< Status block signature, must be CSW_SIGNATURE to indicate a valid Command Status */
223 uint32_t Tag; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
224 uint32_t DataTransferResidue; /**< Number of bytes of data not processed in the SCSI command */
225 uint8_t Status; /**< Status code of the issued command - a value from the MassStorage_CommandStatusCodes_t enum */
226 } MS_CommandStatusWrapper_t;
227
228 /** Type define for a SCSI Sense structure. Structures of this type are filled out by the
229 * device via the MassStore_RequestSense() function, indicating the current sense data of the
230 * device (giving explicit error codes for the last issued command). For details of the
231 * structure contents, refer to the SCSI specifications.
232 */
233 typedef struct
234 {
235 uint8_t ResponseCode;
236
237 uint8_t SegmentNumber;
238
239 unsigned char SenseKey : 4;
240 unsigned char _RESERVED1 : 1;
241 unsigned char ILI : 1;
242 unsigned char EOM : 1;
243 unsigned char FileMark : 1;
244
245 uint8_t Information[4];
246 uint8_t AdditionalLength;
247 uint8_t CmdSpecificInformation[4];
248 uint8_t AdditionalSenseCode;
249 uint8_t AdditionalSenseQualifier;
250 uint8_t FieldReplaceableUnitCode;
251 uint8_t SenseKeySpecific[3];
252 } SCSI_Request_Sense_Response_t;
253
254 /** Type define for a SCSI Inquiry structure. Structures of this type are filled out by the
255 * device via the MassStore_Inquiry() function, retrieving the attached device's information.
256 * For details of the structure contents, refer to the SCSI specifications.
257 */
258 typedef struct
259 {
260 unsigned char DeviceType : 5;
261 unsigned char PeripheralQualifier : 3;
262
263 unsigned char _RESERVED1 : 7;
264 unsigned char Removable : 1;
265
266 uint8_t Version;
267
268 unsigned char ResponseDataFormat : 4;
269 unsigned char _RESERVED2 : 1;
270 unsigned char NormACA : 1;
271 unsigned char TrmTsk : 1;
272 unsigned char AERC : 1;
273
274 uint8_t AdditionalLength;
275 uint8_t _RESERVED3[2];
276
277 unsigned char SoftReset : 1;
278 unsigned char CmdQue : 1;
279 unsigned char _RESERVED4 : 1;
280 unsigned char Linked : 1;
281 unsigned char Sync : 1;
282 unsigned char WideBus16Bit : 1;
283 unsigned char WideBus32Bit : 1;
284 unsigned char RelAddr : 1;
285
286 uint8_t VendorID[8];
287 uint8_t ProductID[16];
288 uint8_t RevisionID[4];
289 } SCSI_Inquiry_Response_t;
290
291 /* Enums: */
292 /** Enum for the possible command status wrapper return status codes. */
293 enum MassStorage_CommandStatusCodes_t
294 {
295 SCSI_Command_Pass = 0, /**< Command completed with no error */
296 SCSI_Command_Fail = 1, /**< Command failed to complete - host may check the exact error via a SCSI REQUEST SENSE command */
297 SCSI_Phase_Error = 2 /**< Command failed due to being invalid in the current phase */
298 };
299
300 /* Disable C linkage for C++ Compilers: */
301 #if defined(__cplusplus)
302 }
303 #endif
304
305 #endif
306
307 /** @} */