Copy existing Host mode demos to new ClassDriver and LowLevel subfolders.
[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 500
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 /** SCSI capacity structure, to hold the total capacity of the device in both the number
132 * of blocks in the current LUN, and the size of each block. This structure is filled by
133 * the device when the MassStore_ReadCapacity() function is called.
134 */
135 typedef struct
136 {
137 uint32_t Blocks; /**< Number of blocks in the addressed LUN of the device */
138 uint32_t BlockSize; /**< Number of bytes in each block in the addressed LUN */
139 } SCSI_Capacity_t;
140
141 /* Enums: */
142 /** CSW status return codes, indicating the overall status of the issued CBW */
143 enum MassStorageHost_CommandStatusCodes_t
144 {
145 Command_Pass = 0, /**< Command completed successfully */
146 Command_Fail = 1, /**< Command failed to complete successfully */
147 Phase_Error = 2 /**< Phase error while processing the issued command */
148 };
149
150 /* External Variables: */
151 extern CommandStatusWrapper_t SCSICommandStatus;
152
153 /* Function Prototypes: */
154 #if defined(INCLUDE_FROM_MASSSTORE_COMMANDS_C)
155 static uint8_t MassStore_SendCommand(void);
156 static uint8_t MassStore_WaitForDataReceived(void);
157 static uint8_t MassStore_SendReceiveData(void* BufferPtr) ATTR_NON_NULL_PTR_ARG(1);
158 static uint8_t MassStore_GetReturnedStatus(void);
159 #endif
160
161 uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum);
162 uint8_t MassStore_MassStorageReset(void);
163 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex);
164 uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_Response_t* const SensePtr)
165 ATTR_NON_NULL_PTR_ARG(2);
166 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
167 const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
168 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
169 const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
170 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const CapacityPtr)
171 ATTR_NON_NULL_PTR_ARG(2);
172 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex);
173 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool PreventRemoval);
174
175 #endif