Endpoint configuration is now refined to give better output when all configurations...
[pub/USBasp.git] / Demos / Host / MassStorageHost / 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 unsigned char ReponseCode : 7;
113 unsigned char Valid : 1;
114
115 uint8_t SegmentNumber;
116
117 unsigned char SenseKey : 4;
118 unsigned char _RESERVED1 : 1;
119 unsigned char ILI : 1;
120 unsigned char EOM : 1;
121 unsigned char FileMark : 1;
122
123 uint8_t Information[4];
124 uint8_t AdditionalLength;
125 uint8_t CmdSpecificInformation[4];
126 uint8_t AdditionalSenseCode;
127 uint8_t AdditionalSenseQualifier;
128 uint8_t FieldReplaceableUnitCode;
129 uint8_t SenseKeySpecific[3];
130 } SCSI_Request_Sense_Response_t;
131
132 /** SCSI capacity structure, to hold the total capacity of the device in both the number
133 * of blocks in the current LUN, and the size of each block. This structure is filled by
134 * the device when the MassStore_ReadCapacity() function is called.
135 */
136 typedef struct
137 {
138 uint32_t Blocks; /**< Number of blocks in the addressed LUN of the device */
139 uint32_t BlockSize; /**< Number of bytes in each block in the addressed LUN */
140 } SCSI_Capacity_t;
141
142 /* Enums: */
143 /** CSW status return codes, indicating the overall status of the issued CBW */
144 enum MassStorageHost_CommandStatusCodes_t
145 {
146 Command_Pass = 0, /**< Command completed successfully */
147 Command_Fail = 1, /**< Command failed to complete successfully */
148 Phase_Error = 2 /**< Phase error while processing the issued command */
149 };
150
151 /* External Variables: */
152 extern CommandStatusWrapper_t SCSICommandStatus;
153
154 /* Function Prototypes: */
155 #if defined(INCLUDE_FROM_MASSSTORE_COMMANDS_C)
156 static uint8_t MassStore_SendCommand(void);
157 static uint8_t MassStore_WaitForDataReceived(void);
158 static uint8_t MassStore_SendReceiveData(void* BufferPtr) ATTR_NON_NULL_PTR_ARG(1);
159 static uint8_t MassStore_GetReturnedStatus(void);
160 #endif
161
162 uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum);
163 uint8_t MassStore_MassStorageReset(void);
164 uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex);
165 uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_Response_t* const SensePtr)
166 ATTR_NON_NULL_PTR_ARG(2);
167 uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
168 const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
169 uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
170 const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
171 uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const CapacityPtr)
172 ATTR_NON_NULL_PTR_ARG(2);
173 uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex);
174 uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool PreventRemoval);
175
176 #endif