a3dcb1c68fd9af1fab790fcb9ee730d1e082cc3a
[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 "../../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 "MassStorage.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 Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1);
65 Endpoint_ClearIN();
66 Endpoint_ClearStatusStage();
67 }
68
69 break;
70 }
71 }
72
73 bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
74 {
75 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
76
77 for (uint8_t EndpointNum = 1; EndpointNum < ENDPOINT_TOTAL_ENDPOINTS; EndpointNum++)
78 {
79 uint16_t Size;
80 uint8_t Type;
81 uint8_t Direction;
82 bool DoubleBanked;
83
84 if (EndpointNum == MSInterfaceInfo->Config.DataINEndpointNumber)
85 {
86 Size = MSInterfaceInfo->Config.DataINEndpointSize;
87 Direction = ENDPOINT_DIR_IN;
88 Type = EP_TYPE_BULK;
89 DoubleBanked = MSInterfaceInfo->Config.DataINEndpointDoubleBank;
90 }
91 else if (EndpointNum == MSInterfaceInfo->Config.DataOUTEndpointNumber)
92 {
93 Size = MSInterfaceInfo->Config.DataOUTEndpointSize;
94 Direction = ENDPOINT_DIR_OUT;
95 Type = EP_TYPE_BULK;
96 DoubleBanked = MSInterfaceInfo->Config.DataOUTEndpointDoubleBank;
97 }
98 else
99 {
100 continue;
101 }
102
103 if (!(Endpoint_ConfigureEndpoint(EndpointNum, Type, Direction, Size,
104 DoubleBanked ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
105 {
106 return false;
107 }
108 }
109
110 return true;
111 }
112
113 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
114 {
115 if (USB_DeviceState != DEVICE_STATE_Configured)
116 return;
117
118 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
119
120 if (Endpoint_IsReadWriteAllowed())
121 {
122 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
123 {
124 if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
125 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
126
127 MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
128 MS_SCSI_COMMAND_Pass : MS_SCSI_COMMAND_Fail;
129 MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE;
130 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
131 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
132
133 if ((MSInterfaceInfo->State.CommandStatus.Status == MS_SCSI_COMMAND_Fail) &&
134 (MSInterfaceInfo->State.CommandStatus.DataTransferResidue))
135 {
136 Endpoint_StallTransaction();
137 }
138
139 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
140 }
141 }
142
143 if (MSInterfaceInfo->State.IsMassStoreReset)
144 {
145 Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
146 Endpoint_ResetEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
147
148 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
149 Endpoint_ClearStall();
150 Endpoint_ResetDataToggle();
151 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
152 Endpoint_ClearStall();
153 Endpoint_ResetDataToggle();
154
155 MSInterfaceInfo->State.IsMassStoreReset = false;
156 }
157 }
158
159 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
160 {
161 uint16_t BytesProcessed;
162
163 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
164
165 BytesProcessed = 0;
166 while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
167 (sizeof(MS_CommandBlockWrapper_t) - 16), &BytesProcessed) ==
168 ENDPOINT_RWSTREAM_IncompleteTransfer)
169 {
170 if (MSInterfaceInfo->State.IsMassStoreReset)
171 return false;
172 }
173
174 if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) ||
175 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
176 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
177 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
178 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
179 {
180 Endpoint_StallTransaction();
181 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
182 Endpoint_StallTransaction();
183
184 return false;
185 }
186
187 BytesProcessed = 0;
188 while (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
189 MSInterfaceInfo->State.CommandBlock.SCSICommandLength, &BytesProcessed) ==
190 ENDPOINT_RWSTREAM_IncompleteTransfer)
191 {
192 if (MSInterfaceInfo->State.IsMassStoreReset)
193 return false;
194 }
195
196 Endpoint_ClearOUT();
197
198 return true;
199 }
200
201 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
202 {
203 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
204
205 while (Endpoint_IsStalled())
206 {
207 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
208 USB_USBTask();
209 #endif
210
211 if (MSInterfaceInfo->State.IsMassStoreReset)
212 return;
213 }
214
215 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
216
217 while (Endpoint_IsStalled())
218 {
219 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
220 USB_USBTask();
221 #endif
222
223 if (MSInterfaceInfo->State.IsMassStoreReset)
224 return;
225 }
226
227 uint16_t BytesProcessed = 0;
228 while (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus,
229 sizeof(MS_CommandStatusWrapper_t), &BytesProcessed) ==
230 ENDPOINT_RWSTREAM_IncompleteTransfer)
231 {
232 if (MSInterfaceInfo->State.IsMassStoreReset)
233 return;
234 }
235
236 Endpoint_ClearIN();
237 }
238
239 #endif