MassStorageHost demo now retrieves Inquiry data from the device during enumeration...
[pub/USBasp.git] / Demos / Host / LowLevel / MassStorageHost / Lib / MassStoreCommands.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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> // USB Functionality
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 2000
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 /* Type defines: */
76 /** Type define for a Mass Storage class Command Block Wrapper, used to wrap SCSI
77 * commands for transport over the USB bulk endpoints to the device.
78 */
79 typedef struct
80 {
81 struct
82 {
83 uint32_t Signature; /**< Command block signature, always equal to CBW_SIGNATURE */
84 uint32_t Tag; /**< Current CBW tag, to positively associate a CBW with a CSW */
85 uint32_t DataTransferLength; /**< Length of data to transfer, following the CBW */
86 uint8_t Flags; /**< Block flags, equal to one of the COMMAND_DIRECTION_DATA_* macros */
87 uint8_t LUN; /**< Logical Unit Number the CBW is addressed to in the device */
88 uint8_t SCSICommandLength; /**< Length of the SCSI command in the CBW */
89 } Header;
90
91 uint8_t SCSICommandData[16]; /**< SCSI command to issue to the device */
92 } CommandBlockWrapper_t;
93
94 /** Type define for a Mass Storage class Command Status Wrapper, used to wrap SCSI
95 * responses for transport over the USB bulk endpoints from the device.
96 */
97 typedef struct
98 {
99 uint32_t Signature; /**< Command status signature, always equal to CSW_SIGNATURE */
100 uint32_t Tag; /**< Current CBW tag, to positively associate a CBW with a CSW */
101 uint32_t DataTransferResidue; /**< Length of data not transferred */
102 uint8_t Status; /**< Command status, a value from the MassStorageHost_CommandStatusCodes_t enum */
103 } CommandStatusWrapper_t;
104
105 /** Type define for a SCSI Sense structure. Structures of this type are filled out by the
106 * device via the MassStore_RequestSense() function, indicating the current sense data of the
107 * device (giving explicit error codes for the last issued command). For details of the
108 * structure contents, refer to the SCSI specifications.
109 */
110 typedef struct
111 {
112 uint8_t ReponseCode;
113
114 uint8_t SegmentNumber;
115
116 unsigned char SenseKey : 4;
117 unsigned char _RESERVED1 : 1;
118 unsigned char ILI : 1;
119 unsigned char EOM : 1;
120 unsigned char FileMark : 1;
121
122 uint8_t Information[4];
123 uint8_t AdditionalLength;
124 uint8_t CmdSpecificInformation[4];
125 uint8_t AdditionalSenseCode;
126 uint8_t AdditionalSenseQualifier;
127 uint8_t FieldReplaceableUnitCode;
128 uint8_t SenseKeySpecific[3];
129 } SCSI_Request_Sense_Response_t;
130
131 /** Type define for a SCSI Inquiry structure. Structures of this type are filled out by the
132 * device via the MassStore_Inquiry() function, retrieving the attached device's information.
133 * For details of the structure contents, refer to the SCSI specifications.
134 */
135 typedef struct
136 {
137 unsigned char DeviceType : 5;
138 unsigned char PeripheralQualifier : 3;
139
140 unsigned char _RESERVED1 : 7;
141 unsigned char Removable : 1;
142
143 uint8_t Version;
144
145 unsigned char ResponseDataFormat : 4;
146 unsigned char _RESERVED2 : 1;
147 unsigned char NormACA : 1;
148 unsigned char TrmTsk : 1;
149 unsigned char AERC : 1;
150
151 uint8_t AdditionalLength;
152 uint8_t _RESERVED3[2];
153
154 unsigned char SoftReset : 1;
155 unsigned char CmdQue : 1;
156 unsigned char _RESERVED4 : 1;
157 unsigned char Linked : 1;
158 unsigned char Sync : 1;
159 unsigned char WideBus16Bit : 1;
160 unsigned char WideBus32Bit : 1;
161 unsigned char RelAddr : 1;
162
163 uint8_t VendorID[8];
164 uint8_t ProductID[16];
165 uint8_t RevisionID[4];
166 } SCSI_Inquiry_Response_t;
167
168 /** SCSI capacity structure, to hold the total capacity of the device in both the number
169 * of blocks in the current LUN, and the size of each block. This structure is filled by
170 * the device when the MassStore_ReadCapacity() function is called.
171 */
172 typedef struct
173 {
174 uint32_t Blocks; /**< Number of blocks in the addressed LUN of the device */
175 uint32_t BlockSize; /**< Number of bytes in each block in the addressed LUN */
176 } SCSI_Capacity_t;
177
178 /* Enums: */
179 /** CSW status return codes, indicating the overall status of the issued CBW */
180 enum MassStorageHost_CommandStatusCodes_t
181 {
182 Command_Pass = 0, /**< Command completed successfully */
183 Command_Fail = 1, /**< Command failed to complete successfully */
184 Phase_Error = 2 /**< Phase error while processing the issued command */
185 };
186
187 /* External Variables: */
188 extern CommandStatusWrapper_t SCSICommandStatus;
189
190 /* Function Prototypes: */
191 #if defined(INCLUDE_FROM_MASSSTORE_COMMANDS_C)
192 static uint8_t MassStore_SendCommand(void);
193 static uint8_t MassStore_WaitForDataReceived(void);
194 static uint8_t MassStore_SendReceiveData(void* BufferPtr) ATTR_NON_NULL_PTR_ARG(1);
195 static uint8_t MassStore_GetReturnedStatus(void);
196 #endif
197
198 uint8_t MassStore_MassStorageReset(void);
199 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex);
200 uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_Response_t* const SensePtr)
201 ATTR_NON_NULL_PTR_ARG(2);
202 uint8_t MassStore_Inquiry(const uint8_t LUNIndex, const SCSI_Inquiry_Response_t* const InquiryPtr)
203 ATTR_NON_NULL_PTR_ARG(2);
204 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
205 const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
206 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
207 const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
208 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const CapacityPtr)
209 ATTR_NON_NULL_PTR_ARG(2);
210 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex);
211 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool PreventRemoval);
212
213 #endif