Move out many of the common class driver constants into grouped enums, to make them...
[pub/USBasp.git] / Demos / Host / LowLevel / MassStorageHost / Lib / MassStoreCommands.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 *
33 * Header file for MassStoreCommands.c.
34 */
35
36 #ifndef _MASS_STORE_COMMANDS_H_
37 #define _MASS_STORE_COMMANDS_H_
38
39 /* Includes: */
40 #include <avr/io.h>
41
42 #include "MassStorageHost.h"
43 #include "SCSI_Codes.h"
44
45 #include <LUFA/Drivers/USB/USB.h>
46
47 /* Macros: */
48 /** Class specific request to reset the Mass Storage interface of the attached device. */
49 #define REQ_MassStorageReset 0xFF
50
51 /** Class specific request to retrieve the maximum Logical Unit Number (LUN) index of the attached device. */
52 #define REQ_GetMaxLUN 0xFE
53
54 /** Command Block Wrapper signature byte, for verification of valid CBW blocks. */
55 #define CBW_SIGNATURE 0x43425355UL
56
57 /** Command Static Wrapper signature byte, for verification of valid CSW blocks. */
58 #define CSW_SIGNATURE 0x53425355UL
59
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)
62
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)
65
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
68
69 /** Pipe number of the Mass Storage data IN pipe. */
70 #define MASS_STORE_DATA_IN_PIPE 1
71
72 /** Pipe number of the Mass Storage data OUT pipe. */
73 #define MASS_STORE_DATA_OUT_PIPE 2
74
75 /** Additional error code for Mass Storage functions when a device returns a logical command failure. */
76 #define MASS_STORE_SCSI_COMMAND_FAILED 0xC0
77
78 /* Type Defines: */
79 /** Type define for a Mass Storage class Command Block Wrapper, used to wrap SCSI
80 * commands for transport over the USB bulk endpoints to the device.
81 */
82 typedef struct
83 {
84 uint32_t Signature; /**< Command block signature, always equal to CBW_SIGNATURE */
85 uint32_t Tag; /**< Current CBW tag, to positively associate a CBW with a CSW (filled automatically) */
86 uint32_t DataTransferLength; /**< Length of data to transfer, following the CBW */
87 uint8_t Flags; /**< Block flags, equal to one of the COMMAND_DIRECTION_DATA_* macros */
88 uint8_t LUN; /**< Logical Unit Number the CBW is addressed to in the device */
89 uint8_t SCSICommandLength; /**< Length of the SCSI command in the CBW */
90 uint8_t SCSICommandData[16]; /**< SCSI command to issue to the device */
91 } CommandBlockWrapper_t;
92
93 /** Type define for a Mass Storage class Command Status Wrapper, used to wrap SCSI
94 * responses for transport over the USB bulk endpoints from the device.
95 */
96 typedef struct
97 {
98 uint32_t Signature; /**< Command status signature, always equal to CSW_SIGNATURE */
99 uint32_t Tag; /**< Current CBW tag, to positively associate a CBW with a CSW */
100 uint32_t DataTransferResidue; /**< Length of data not transferred */
101 uint8_t Status; /**< Command status, a value from the MassStorageHost_CommandStatusCodes_t enum */
102 } CommandStatusWrapper_t;
103
104 /** Type define for a SCSI Sense structure. Structures of this type are filled out by the
105 * device via the \ref MassStore_RequestSense() function, indicating the current sense data of the
106 * device (giving explicit error codes for the last issued command). For details of the
107 * structure contents, refer to the SCSI specifications.
108 */
109 typedef struct
110 {
111 uint8_t ResponseCode;
112
113 uint8_t SegmentNumber;
114
115 unsigned char SenseKey : 4;
116 unsigned char Reserved : 1;
117 unsigned char ILI : 1;
118 unsigned char EOM : 1;
119 unsigned char FileMark : 1;
120
121 uint8_t Information[4];
122 uint8_t AdditionalLength;
123 uint8_t CmdSpecificInformation[4];
124 uint8_t AdditionalSenseCode;
125 uint8_t AdditionalSenseQualifier;
126 uint8_t FieldReplaceableUnitCode;
127 uint8_t SenseKeySpecific[3];
128 } SCSI_Request_Sense_Response_t;
129
130 /** Type define for a SCSI Inquiry structure. Structures of this type are filled out by the
131 * device via the \ref MassStore_Inquiry() function, retrieving the attached device's information.
132 * For details of the structure contents, refer to the SCSI specifications.
133 */
134 typedef struct
135 {
136 unsigned char DeviceType : 5;
137 unsigned char PeripheralQualifier : 3;
138
139 unsigned char Reserved : 7;
140 unsigned char Removable : 1;
141
142 uint8_t Version;
143
144 unsigned char ResponseDataFormat : 4;
145 unsigned char Reserved2 : 1;
146 unsigned char NormACA : 1;
147 unsigned char TrmTsk : 1;
148 unsigned char AERC : 1;
149
150 uint8_t AdditionalLength;
151 uint8_t Reserved3[2];
152
153 unsigned char SoftReset : 1;
154 unsigned char CmdQue : 1;
155 unsigned char Reserved4 : 1;
156 unsigned char Linked : 1;
157 unsigned char Sync : 1;
158 unsigned char WideBus16Bit : 1;
159 unsigned char WideBus32Bit : 1;
160 unsigned char RelAddr : 1;
161
162 uint8_t VendorID[8];
163 uint8_t ProductID[16];
164 uint8_t RevisionID[4];
165 } SCSI_Inquiry_Response_t;
166
167 /** SCSI capacity structure, to hold the total capacity of the device in both the number
168 * of blocks in the current LUN, and the size of each block. This structure is filled by
169 * the device when the \ref MassStore_ReadCapacity() function is called.
170 */
171 typedef struct
172 {
173 uint32_t Blocks; /**< Number of blocks in the addressed LUN of the device */
174 uint32_t BlockSize; /**< Number of bytes in each block in the addressed LUN */
175 } SCSI_Capacity_t;
176
177 /* Enums: */
178 /** CSW status return codes, indicating the overall status of the issued CBW. */
179 enum MassStorageHost_CommandStatusCodes_t
180 {
181 Command_Pass = 0, /**< Command completed successfully */
182 Command_Fail = 1, /**< Command failed to complete successfully */
183 Phase_Error = 2 /**< Phase error while processing the issued command */
184 };
185
186 /* Function Prototypes: */
187 #if defined(INCLUDE_FROM_MASSSTORE_COMMANDS_C)
188 static uint8_t MassStore_SendCommand(CommandBlockWrapper_t* const SCSICommandBlock,
189 void* BufferPtr);
190 static uint8_t MassStore_WaitForDataReceived(void);
191 static uint8_t MassStore_SendReceiveData(CommandBlockWrapper_t* const SCSICommandBlock,
192 void* BufferPtr) ATTR_NON_NULL_PTR_ARG(1);
193 static uint8_t MassStore_GetReturnedStatus(CommandStatusWrapper_t* const SCSICommandStatus) ATTR_NON_NULL_PTR_ARG(1);
194 #endif
195
196 uint8_t MassStore_MassStorageReset(void);
197 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex);
198 uint8_t MassStore_RequestSense(const uint8_t LUNIndex,
199 SCSI_Request_Sense_Response_t* const SensePtr) ATTR_NON_NULL_PTR_ARG(2);
200 uint8_t MassStore_Inquiry(const uint8_t LUNIndex,
201 SCSI_Inquiry_Response_t* const InquiryPtr) ATTR_NON_NULL_PTR_ARG(2);
202 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex,
203 const uint32_t BlockAddress,
204 const uint8_t Blocks,
205 const uint16_t BlockSize,
206 void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
207 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex,
208 const uint32_t BlockAddress,
209 const uint8_t Blocks,
210 const uint16_t BlockSize,
211 void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
212 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex,
213 SCSI_Capacity_t* const CapacityPtr) ATTR_NON_NULL_PTR_ARG(2);
214 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex);
215 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex,
216 const bool PreventRemoval);
217
218 #endif