Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[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 #define INCLUDE_FROM_MS_CLASS_C
32 #include "MassStorage.h"
33
34 static USB_ClassInfo_MS_t* CallbackMSInterfaceInfo;
35
36 void USB_MS_ProcessControlPacket(USB_ClassInfo_MS_t* MSInterfaceInfo)
37 {
38 if (!(Endpoint_IsSETUPReceived()))
39 return;
40
41 if (USB_ControlRequest.wIndex != MSInterfaceInfo->InterfaceNumber)
42 return;
43
44 switch (USB_ControlRequest.bRequest)
45 {
46 case REQ_MassStorageReset:
47 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
48 {
49 Endpoint_ClearSETUP();
50
51 MSInterfaceInfo->IsMassStoreReset = true;
52
53 while (!(Endpoint_IsINReady()));
54 Endpoint_ClearIN();
55 }
56
57 break;
58 case REQ_GetMaxLUN:
59 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
60 {
61 Endpoint_ClearSETUP();
62
63 Endpoint_Write_Byte(MSInterfaceInfo->TotalLUNs - 1);
64
65 Endpoint_ClearIN();
66
67 while (!(Endpoint_IsOUTReceived()));
68 Endpoint_ClearOUT();
69 }
70
71 break;
72 }
73 }
74
75 bool USB_MS_ConfigureEndpoints(USB_ClassInfo_MS_t* MSInterfaceInfo)
76 {
77 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->DataINEndpointNumber, EP_TYPE_BULK,
78 ENDPOINT_DIR_IN, MSInterfaceInfo->DataINEndpointSize,
79 ENDPOINT_BANK_SINGLE)))
80 {
81 return false;
82 }
83
84 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_BULK,
85 ENDPOINT_DIR_OUT, MSInterfaceInfo->DataOUTEndpointSize,
86 ENDPOINT_BANK_SINGLE)))
87 {
88 return false;
89 }
90
91 return true;
92 }
93
94 void USB_MS_USBTask(USB_ClassInfo_MS_t* MSInterfaceInfo)
95 {
96 if (!(USB_IsConnected))
97 return;
98
99 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
100
101 if (Endpoint_IsReadWriteAllowed())
102 {
103 if (USB_MS_ReadInCommandBlock(MSInterfaceInfo))
104 {
105 if (MSInterfaceInfo->CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)
106 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
107
108 MSInterfaceInfo->CommandStatus.Status = CALLBACK_USB_MS_SCSICommandReceived(MSInterfaceInfo) ?
109 Command_Pass : Command_Fail;
110 MSInterfaceInfo->CommandStatus.Signature = CSW_SIGNATURE;
111 MSInterfaceInfo->CommandStatus.Tag = MSInterfaceInfo->CommandBlock.Tag;
112 MSInterfaceInfo->CommandStatus.DataTransferResidue = MSInterfaceInfo->CommandBlock.DataTransferLength;
113
114 if ((MSInterfaceInfo->CommandStatus.Status == Command_Fail) && (MSInterfaceInfo->CommandStatus.DataTransferResidue))
115 Endpoint_StallTransaction();
116
117 USB_MS_ReturnCommandStatus(MSInterfaceInfo);
118
119 if (MSInterfaceInfo->IsMassStoreReset)
120 {
121 Endpoint_ResetFIFO(MSInterfaceInfo->DataOUTEndpointNumber);
122 Endpoint_ResetFIFO(MSInterfaceInfo->DataINEndpointNumber);
123
124 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
125 Endpoint_ClearStall();
126 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
127 Endpoint_ClearStall();
128
129 MSInterfaceInfo->IsMassStoreReset = false;
130 }
131 }
132 }
133 }
134
135 static bool USB_MS_ReadInCommandBlock(USB_ClassInfo_MS_t* MSInterfaceInfo)
136 {
137 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
138
139 CallbackMSInterfaceInfo = MSInterfaceInfo;
140 Endpoint_Read_Stream_LE(&MSInterfaceInfo->CommandBlock,
141 (sizeof(CommandBlockWrapper_t) - MAX_SCSI_COMMAND_LENGTH),
142 StreamCallback_AbortOnMassStoreReset);
143
144 if ((MSInterfaceInfo->CommandBlock.Signature != CBW_SIGNATURE) ||
145 (MSInterfaceInfo->CommandBlock.LUN >= MSInterfaceInfo->TotalLUNs) ||
146 (MSInterfaceInfo->CommandBlock.SCSICommandLength > MAX_SCSI_COMMAND_LENGTH))
147 {
148 Endpoint_StallTransaction();
149 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
150 Endpoint_StallTransaction();
151
152 return false;
153 }
154
155 CallbackMSInterfaceInfo = MSInterfaceInfo;
156 Endpoint_Read_Stream_LE(&MSInterfaceInfo->CommandBlock.SCSICommandData,
157 MSInterfaceInfo->CommandBlock.SCSICommandLength,
158 StreamCallback_AbortOnMassStoreReset);
159
160 Endpoint_ClearOUT();
161
162 if (MSInterfaceInfo->IsMassStoreReset)
163 return false;
164
165 return true;
166 }
167
168 static void USB_MS_ReturnCommandStatus(USB_ClassInfo_MS_t* MSInterfaceInfo)
169 {
170 Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber);
171
172 while (Endpoint_IsStalled())
173 {
174 USB_USBTask();
175
176 if (MSInterfaceInfo->IsMassStoreReset)
177 return;
178 }
179
180 Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber);
181
182 while (Endpoint_IsStalled())
183 {
184 USB_USBTask();
185
186 if (MSInterfaceInfo->IsMassStoreReset)
187 return;
188 }
189
190 CallbackMSInterfaceInfo = MSInterfaceInfo;
191 Endpoint_Write_Stream_LE(&MSInterfaceInfo->CommandStatus, sizeof(CommandStatusWrapper_t),
192 StreamCallback_AbortOnMassStoreReset);
193
194 Endpoint_ClearIN();
195
196 if (MSInterfaceInfo->IsMassStoreReset)
197 return;
198 }
199
200 static uint8_t StreamCallback_AbortOnMassStoreReset(void)
201 {
202 USB_MS_USBTask(CallbackMSInterfaceInfo);
203
204 if (CallbackMSInterfaceInfo->IsMassStoreReset)
205 return STREAMCALLBACK_Abort;
206 else
207 return STREAMCALLBACK_Continue;
208 }