Altered all endpoint/pipe stream transfers so that the new BytesProcessed parameter...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MassStorage.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 "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_DEVICE)
34
35 #define __INCLUDE_FROM_MS_DRIVER
36 #define __INCLUDE_FROM_MASSSTORAGE_DEVICE_C
37 #include "MassStorage.h"
38
39 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
40 {
41 if (!(Endpoint_IsSETUPReceived()))
42 return;
43
44 if (USB_ControlRequest.wIndex != MSInterfaceInfo->Config.InterfaceNumber)
45 return;
46
47 switch (USB_ControlRequest.bRequest)
48 {
49 case MS_REQ_MassStorageReset:
50 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
51 {
52 Endpoint_ClearSETUP();
53 Endpoint_ClearStatusStage();
54
55 MSInterfaceInfo->State.IsMassStoreReset = true;
56 }
57
58 break;
59 case MS_REQ_GetMaxLUN:
60 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
61 {
62 Endpoint_ClearSETUP();
63 Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1);
64 Endpoint_ClearIN();
65 Endpoint_ClearStatusStage();
66 }
67
68 break;
69 }
70 }
71
72 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
73 {
74 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
75
76 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
77 {
78 uint16_t Size;
79 uint8_t Type;
80 uint8_t Direction;
81 bool DoubleBanked;
82
83 if (EndpointNum == MSInterfaceInfo->Config.DataINEndpointNumber)
84 {
85 Size = MSInterfaceInfo->Config.DataINEndpointSize;
86 Direction = ENDPOINT_DIR_IN;
87 Type = EP_TYPE_BULK;
88 DoubleBanked = MSInterfaceInfo->Config.DataINEndpointDoubleBank;
89 }
90 else if (EndpointNum == MSInterfaceInfo->Config.DataOUTEndpointNumber)
91 {
92 Size = MSInterfaceInfo->Config.DataOUTEndpointSize;
93 Direction = ENDPOINT_DIR_OUT;
94 Type = EP_TYPE_BULK;
95 DoubleBanked = MSInterfaceInfo->Config.DataOUTEndpointDoubleBank;
96 }
97 else
98 {
99 continue;
100 }
101
102 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
103 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
104 {
105 return false;
106 }
107 }
108
109 return true;
110 }
111
112 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
113 {
114 if (USB_DeviceState != DEVICE_STATE_Configured)
115 return;
116
117 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
118
119 if (Endpoint_IsReadWriteAllowed())
120 {
121 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
122 {
123 if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
124 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
125
126 MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
127 MS_SCSI_COMMAND_Pass : MS_SCSI_COMMAND_Fail;
128 MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE;
129 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
130 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
131
132 if ((MSInterfaceInfo->State.CommandStatus.Status == MS_SCSI_COMMAND_Fail) &&
133 (MSInterfaceInfo->State.CommandStatus.DataTransferResidue))
134 {
135 Endpoint_StallTransaction();
136 }
137
138 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
139 }
140 }
141
142 if (MSInterfaceInfo->State.IsMassStoreReset)
143 {
144 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);
145 Endpoint_ResetFIFO(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 !defined(INTERRUPT_CONTROL_ENDPOINT)
170 USB_USBTask();
171 #endif
172
173 if (MSInterfaceInfo->State.IsMassStoreReset)
174 return false;
175 }
176
177 if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) ||
178 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
179 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
180 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
181 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
182 {
183 Endpoint_StallTransaction();
184 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
185 Endpoint_StallTransaction();
186
187 return false;
188 }
189
190 BytesProcessed = 0;
191 while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
192 MSInterfaceInfo->State.CommandBlock.SCSICommandLength, &BytesProcessed) ==
193 ENDPOINT_RWSTREAM_IncompleteTransfer)
194 {
195 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
196 USB_USBTask();
197 #endif
198
199 if (MSInterfaceInfo->State.IsMassStoreReset)
200 return false;
201 }
202
203 Endpoint_ClearOUT();
204
205 return true;
206 }
207
208 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
209 {
210 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
211
212 while (Endpoint_IsStalled())
213 {
214 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
215 USB_USBTask();
216 #endif
217
218 if (MSInterfaceInfo->State.IsMassStoreReset)
219 return;
220 }
221
222 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
223
224 while (Endpoint_IsStalled())
225 {
226 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
227 USB_USBTask();
228 #endif
229
230 if (MSInterfaceInfo->State.IsMassStoreReset)
231 return;
232 }
233
234 uint16_t BytesProcessed = 0;
235 while (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus,
236 sizeof(MS_CommandStatusWrapper_t), &BytesProcessed) ==
237 ENDPOINT_RWSTREAM_IncompleteTransfer)
238 {
239 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
240 USB_USBTask();
241 #endif
242
243 if (MSInterfaceInfo->State.IsMassStoreReset)
244 return;
245 }
246
247 Endpoint_ClearIN();
248 }
249
250 #endif