Added Class, ClassDevice, ClassHost and ClassCommon to the internal class driver...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MassStorageClassDevice.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define __INCLUDE_FROM_MS_DRIVER
37 #define __INCLUDE_FROM_MASSSTORAGE_DEVICE_C
38 #include "MassStorageClassDevice.h"
39
40 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
41 {
42 if (!(Endpoint_IsSETUPReceived()))
43 return;
44
45 if (USB_ControlRequest.wIndex != MSInterfaceInfo->Config.InterfaceNumber)
46 return;
47
48 switch (USB_ControlRequest.bRequest)
49 {
50 case MS_REQ_MassStorageReset:
51 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
52 {
53 Endpoint_ClearSETUP();
54 Endpoint_ClearStatusStage();
55
56 MSInterfaceInfo->State.IsMassStoreReset = true;
57 }
58
59 break;
60 case MS_REQ_GetMaxLUN:
61 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
62 {
63 Endpoint_ClearSETUP();
64 while (!(Endpoint_IsINReady()));
65 Endpoint_Write_8(MSInterfaceInfo->Config.TotalLUNs - 1);
66 Endpoint_ClearIN();
67 Endpoint_ClearStatusStage();
68 }
69
70 break;
71 }
72 }
73
74 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
75 {
76 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
77
78 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
79 {
80 uint16_t Size;
81 uint8_t Type;
82 uint8_t Direction;
83 bool DoubleBanked;
84
85 if (EndpointNum == MSInterfaceInfo->Config.DataINEndpointNumber)
86 {
87 Size = MSInterfaceInfo->Config.DataINEndpointSize;
88 Direction = ENDPOINT_DIR_IN;
89 Type = EP_TYPE_BULK;
90 DoubleBanked = MSInterfaceInfo->Config.DataINEndpointDoubleBank;
91 }
92 else if (EndpointNum == MSInterfaceInfo->Config.DataOUTEndpointNumber)
93 {
94 Size = MSInterfaceInfo->Config.DataOUTEndpointSize;
95 Direction = ENDPOINT_DIR_OUT;
96 Type = EP_TYPE_BULK;
97 DoubleBanked = MSInterfaceInfo->Config.DataOUTEndpointDoubleBank;
98 }
99 else
100 {
101 continue;
102 }
103
104 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
105 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
106 {
107 return false;
108 }
109 }
110
111 return true;
112 }
113
114 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
115 {
116 if (USB_DeviceState != DEVICE_STATE_Configured)
117 return;
118
119 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
120
121 if (Endpoint_IsReadWriteAllowed())
122 {
123 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
124 {
125 if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
126 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
127
128 bool SCSICommandResult = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo);
129
130 MSInterfaceInfo->State.CommandStatus.Status = (SCSICommandResult) ? MS_SCSI_COMMAND_Pass : MS_SCSI_COMMAND_Fail;
131 MSInterfaceInfo->State.CommandStatus.Signature = CPU_TO_LE32(MS_CSW_SIGNATURE);
132 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
133 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
134
135 if (!(SCSICommandResult) && (le32_to_cpu(MSInterfaceInfo->State.CommandStatus.DataTransferResidue)))
136 Endpoint_StallTransaction();
137
138 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
139 }
140 }
141
142 if (MSInterfaceInfo->State.IsMassStoreReset)
143 {
144 Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
145 Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
146
147 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
148 Endpoint_ClearStall();
149 Endpoint_ResetDataToggle();
150 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
151 Endpoint_ClearStall();
152 Endpoint_ResetDataToggle();
153
154 MSInterfaceInfo->State.IsMassStoreReset = false;
155 }
156 }
157
158 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
159 {
160 uint16_t BytesProcessed;
161
162 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
163
164 BytesProcessed = 0;
165 while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
166 (sizeof(MS_CommandBlockWrapper_t) - 16), &BytesProcessed) ==
167 ENDPOINT_RWSTREAM_IncompleteTransfer)
168 {
169 if (MSInterfaceInfo->State.IsMassStoreReset)
170 return false;
171 }
172
173 if ((MSInterfaceInfo->State.CommandBlock.Signature != CPU_TO_LE32(MS_CBW_SIGNATURE)) ||
174 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
175 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
176 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
177 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
178 {
179 Endpoint_StallTransaction();
180 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
181 Endpoint_StallTransaction();
182
183 return false;
184 }
185
186 BytesProcessed = 0;
187 while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
188 MSInterfaceInfo->State.CommandBlock.SCSICommandLength, &BytesProcessed) ==
189 ENDPOINT_RWSTREAM_IncompleteTransfer)
190 {
191 if (MSInterfaceInfo->State.IsMassStoreReset)
192 return false;
193 }
194
195 Endpoint_ClearOUT();
196
197 return true;
198 }
199
200 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
201 {
202 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
203
204 while (Endpoint_IsStalled())
205 {
206 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
207 USB_USBTask();
208 #endif
209
210 if (MSInterfaceInfo->State.IsMassStoreReset)
211 return;
212 }
213
214 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
215
216 while (Endpoint_IsStalled())
217 {
218 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
219 USB_USBTask();
220 #endif
221
222 if (MSInterfaceInfo->State.IsMassStoreReset)
223 return;
224 }
225
226 uint16_t BytesProcessed = 0;
227 while (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus,
228 sizeof(MS_CommandStatusWrapper_t), &BytesProcessed) ==
229 ENDPOINT_RWSTREAM_IncompleteTransfer)
230 {
231 if (MSInterfaceInfo->State.IsMassStoreReset)
232 return;
233 }
234
235 Endpoint_ClearIN();
236 }
237
238 #endif