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