Added class specific descriptor type defines with standard USB-IF element naming.
[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 Endpoint_ClearStatusStage();
56
57 MSInterfaceInfo->State.IsMassStoreReset = true;
58 }
59
60 break;
61 case REQ_GetMaxLUN:
62 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
63 {
64 Endpoint_ClearSETUP();
65 Endpoint_Write_Byte(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 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK,
79 ENDPOINT_DIR_IN, MSInterfaceInfo->Config.DataINEndpointSize,
80 MSInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
81 {
82 return false;
83 }
84
85 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK,
86 ENDPOINT_DIR_OUT, MSInterfaceInfo->Config.DataOUTEndpointSize,
87 MSInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE)))
88 {
89 return false;
90 }
91
92 return true;
93 }
94
95 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
96 {
97 if (USB_DeviceState != DEVICE_STATE_Configured)
98 return;
99
100 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
101
102 if (Endpoint_IsReadWriteAllowed())
103 {
104 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
105 {
106 if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
107 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
108
109 MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
110 SCSI_Command_Pass : SCSI_Command_Fail;
111 MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE;
112 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
113 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
114
115 if ((MSInterfaceInfo->State.CommandStatus.Status == SCSI_Command_Fail) &&
116 (MSInterfaceInfo->State.CommandStatus.DataTransferResidue))
117 {
118 Endpoint_StallTransaction();
119 }
120
121 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
122 }
123 }
124
125 if (MSInterfaceInfo->State.IsMassStoreReset)
126 {
127 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);
128 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber);
129
130 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
131 Endpoint_ClearStall();
132 Endpoint_ResetDataToggle();
133 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
134 Endpoint_ClearStall();
135 Endpoint_ResetDataToggle();
136
137 MSInterfaceInfo->State.IsMassStoreReset = false;
138 }
139 }
140
141 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
142 {
143 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
144
145 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
146 if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
147 (sizeof(MS_CommandBlockWrapper_t) - 16),
148 StreamCallback_MS_Device_AbortOnMassStoreReset))
149 {
150 return false;
151 }
152
153 if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) ||
154 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
155 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
156 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
157 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
158 {
159 Endpoint_StallTransaction();
160 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
161 Endpoint_StallTransaction();
162
163 return false;
164 }
165
166 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
167 if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
168 MSInterfaceInfo->State.CommandBlock.SCSICommandLength,
169 StreamCallback_MS_Device_AbortOnMassStoreReset))
170 {
171 return false;
172 }
173
174 Endpoint_ClearOUT();
175
176 return true;
177 }
178
179 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
180 {
181 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
182
183 while (Endpoint_IsStalled())
184 {
185 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
186 USB_USBTask();
187 #endif
188
189 if (MSInterfaceInfo->State.IsMassStoreReset)
190 return;
191 }
192
193 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
194
195 while (Endpoint_IsStalled())
196 {
197 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
198 USB_USBTask();
199 #endif
200
201 if (MSInterfaceInfo->State.IsMassStoreReset)
202 return;
203 }
204
205 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
206 if (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),
207 StreamCallback_MS_Device_AbortOnMassStoreReset))
208 {
209 return;
210 }
211
212 Endpoint_ClearIN();
213 }
214
215 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)
216 {
217 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
218 USB_USBTask();
219 #endif
220
221 if (*CallbackIsResetSource)
222 return STREAMCALLBACK_Abort;
223 else
224 return STREAMCALLBACK_Continue;
225 }
226
227 #endif