Add more include protection macros to give the user warnings when they try to manuall...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MassStorage.c
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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_DEVICE)
34
35 #define __INCLUDE_FROM_MS_CLASS_DEVICE_C
36 #define __INCLUDE_FROM_MS_DRIVER
37 #include "MassStorage.h"
38
39 static volatile bool* CallbackIsResetSource;
40
41 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
42 {
43 if (!(Endpoint_IsSETUPReceived()))
44 return;
45
46 if (USB_ControlRequest.wIndex != MSInterfaceInfo->Config.InterfaceNumber)
47 return;
48
49 switch (USB_ControlRequest.bRequest)
50 {
51 case REQ_MassStorageReset:
52 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
53 {
54 Endpoint_ClearSETUP();
55
56 MSInterfaceInfo->State.IsMassStoreReset = true;
57
58 Endpoint_ClearStatusStage();
59 }
60
61 break;
62 case REQ_GetMaxLUN:
63 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
64 {
65 Endpoint_ClearSETUP();
66
67 Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1);
68 Endpoint_ClearIN();
69
70 Endpoint_ClearStatusStage();
71 }
72
73 break;
74 }
75 }
76
77 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
78 {
79 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
80
81 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK,
82 ENDPOINT_DIR_IN, MSInterfaceInfo->Config.DataINEndpointSize,
83 MSInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
84 {
85 return false;
86 }
87
88 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK,
89 ENDPOINT_DIR_OUT, MSInterfaceInfo->Config.DataOUTEndpointSize,
90 MSInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
91 {
92 return false;
93 }
94
95 return true;
96 }
97
98 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
99 {
100 if (USB_DeviceState != DEVICE_STATE_Configured)
101 return;
102
103 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
104
105 if (Endpoint_IsReadWriteAllowed())
106 {
107 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
108 {
109 if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
110 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
111
112 MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
113 SCSI_Command_Pass : SCSI_Command_Fail;
114 MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE;
115 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
116 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
117
118 if ((MSInterfaceInfo->State.CommandStatus.Status == SCSI_Command_Fail) &&
119 (MSInterfaceInfo->State.CommandStatus.DataTransferResidue))
120 {
121 Endpoint_StallTransaction();
122 }
123
124 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
125 }
126 }
127
128 if (MSInterfaceInfo->State.IsMassStoreReset)
129 {
130 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);
131 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber);
132
133 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
134 Endpoint_ClearStall();
135 Endpoint_ResetDataToggle();
136 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
137 Endpoint_ClearStall();
138 Endpoint_ResetDataToggle();
139
140 MSInterfaceInfo->State.IsMassStoreReset = false;
141 }
142 }
143
144 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
145 {
146 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
147
148 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
149 if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
150 (sizeof(MS_CommandBlockWrapper_t) - 16),
151 StreamCallback_MS_Device_AbortOnMassStoreReset))
152 {
153 return false;
154 }
155
156 if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) ||
157 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
158 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
159 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
160 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
161 {
162 Endpoint_StallTransaction();
163 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
164 Endpoint_StallTransaction();
165
166 return false;
167 }
168
169 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
170 if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
171 MSInterfaceInfo->State.CommandBlock.SCSICommandLength,
172 StreamCallback_MS_Device_AbortOnMassStoreReset))
173 {
174 return false;
175 }
176
177 Endpoint_ClearOUT();
178
179 return true;
180 }
181
182 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
183 {
184 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
185
186 while (Endpoint_IsStalled())
187 {
188 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
189 USB_USBTask();
190 #endif
191
192 if (MSInterfaceInfo->State.IsMassStoreReset)
193 return;
194 }
195
196 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
197
198 while (Endpoint_IsStalled())
199 {
200 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
201 USB_USBTask();
202 #endif
203
204 if (MSInterfaceInfo->State.IsMassStoreReset)
205 return;
206 }
207
208 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
209 if (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),
210 StreamCallback_MS_Device_AbortOnMassStoreReset))
211 {
212 return;
213 }
214
215 Endpoint_ClearIN();
216 }
217
218 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)
219 {
220 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
221 USB_USBTask();
222 #endif
223
224 if (*CallbackIsResetSource)
225 return STREAMCALLBACK_Abort;
226 else
227 return STREAMCALLBACK_Continue;
228 }
229
230 #endif