Fixed LowLevel JoystickHostWithParser demo not saving the chosen HID interface's...
[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 MS_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 MS_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 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 MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
129 MS_SCSI_COMMAND_Pass : MS_SCSI_COMMAND_Fail;
130 MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE;
131 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
132 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
133
134 if ((MSInterfaceInfo->State.CommandStatus.Status == MS_SCSI_COMMAND_Fail) &&
135 (MSInterfaceInfo->State.CommandStatus.DataTransferResidue))
136 {
137 Endpoint_StallTransaction();
138 }
139
140 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
141 }
142 }
143
144 if (MSInterfaceInfo->State.IsMassStoreReset)
145 {
146 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);
147 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber);
148
149 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
150 Endpoint_ClearStall();
151 Endpoint_ResetDataToggle();
152 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
153 Endpoint_ClearStall();
154 Endpoint_ResetDataToggle();
155
156 MSInterfaceInfo->State.IsMassStoreReset = false;
157 }
158 }
159
160 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
161 {
162 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
163
164 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
165 if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
166 (sizeof(MS_CommandBlockWrapper_t) - 16),
167 StreamCallback_MS_Device_AbortOnMassStoreReset))
168 {
169 return false;
170 }
171
172 if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) ||
173 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
174 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
175 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
176 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
177 {
178 Endpoint_StallTransaction();
179 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
180 Endpoint_StallTransaction();
181
182 return false;
183 }
184
185 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
186 if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
187 MSInterfaceInfo->State.CommandBlock.SCSICommandLength,
188 StreamCallback_MS_Device_AbortOnMassStoreReset))
189 {
190 return false;
191 }
192
193 Endpoint_ClearOUT();
194
195 return true;
196 }
197
198 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
199 {
200 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
201
202 while (Endpoint_IsStalled())
203 {
204 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
205 USB_USBTask();
206 #endif
207
208 if (MSInterfaceInfo->State.IsMassStoreReset)
209 return;
210 }
211
212 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
213
214 while (Endpoint_IsStalled())
215 {
216 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
217 USB_USBTask();
218 #endif
219
220 if (MSInterfaceInfo->State.IsMassStoreReset)
221 return;
222 }
223
224 CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;
225 if (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),
226 StreamCallback_MS_Device_AbortOnMassStoreReset))
227 {
228 return;
229 }
230
231 Endpoint_ClearIN();
232 }
233
234 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)
235 {
236 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
237 USB_USBTask();
238 #endif
239
240 if (*CallbackIsResetSource)
241 return STREAMCALLBACK_Abort;
242 else
243 return STREAMCALLBACK_Continue;
244 }
245
246 #endif