Change Host mode class driver Pipe configuration routines -- better to let the applic...
[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_ProcessControlPacket(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 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK,
78 ENDPOINT_DIR_IN, MSInterfaceInfo->Config.DataINEndpointSize,
79 ENDPOINT_BANK_SINGLE)))
80 {
81 return false;
82 }
83
84 if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK,
85 ENDPOINT_DIR_OUT, MSInterfaceInfo->Config.DataOUTEndpointSize,
86 ENDPOINT_BANK_SINGLE)))
87 {
88 return false;
89 }
90
91 return true;
92 }
93
94 void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
95 {
96 if (USB_DeviceState != DEVICE_STATE_Configured)
97 return;
98
99 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
100
101 if (Endpoint_IsReadWriteAllowed())
102 {
103 if (MS_Device_ReadInCommandBlock(MSInterfaceInfo))
104 {
105 if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN)
106 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
107
108 MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ?
109 SCSI_Command_Pass : SCSI_Command_Fail;
110 MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE;
111 MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag;
112 MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength;
113
114 if ((MSInterfaceInfo->State.CommandStatus.Status == SCSI_Command_Fail) &&
115 (MSInterfaceInfo->State.CommandStatus.DataTransferResidue))
116 {
117 Endpoint_StallTransaction();
118 }
119
120 MS_Device_ReturnCommandStatus(MSInterfaceInfo);
121
122 if (MSInterfaceInfo->State.IsMassStoreReset)
123 {
124 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);
125 Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber);
126
127 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
128 Endpoint_ClearStall();
129 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
130 Endpoint_ClearStall();
131 }
132 }
133 }
134
135 MSInterfaceInfo->State.IsMassStoreReset = false;
136 }
137
138 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
139 {
140 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
141
142 CallbackMSInterfaceInfo = MSInterfaceInfo;
143 Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,
144 (sizeof(MS_CommandBlockWrapper_t) - 16),
145 StreamCallback_MS_Device_AbortOnMassStoreReset);
146
147 if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) ||
148 (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) ||
149 (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) ||
150 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) ||
151 (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16))
152 {
153 Endpoint_StallTransaction();
154 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
155 Endpoint_StallTransaction();
156
157 return false;
158 }
159
160 CallbackMSInterfaceInfo = MSInterfaceInfo;
161 Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,
162 MSInterfaceInfo->State.CommandBlock.SCSICommandLength,
163 StreamCallback_MS_Device_AbortOnMassStoreReset);
164
165 Endpoint_ClearOUT();
166
167 if (MSInterfaceInfo->State.IsMassStoreReset)
168 return false;
169
170 return true;
171 }
172
173 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
174 {
175 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);
176
177 while (Endpoint_IsStalled())
178 {
179 USB_USBTask();
180
181 if (MSInterfaceInfo->State.IsMassStoreReset)
182 return;
183 }
184
185 Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);
186
187 while (Endpoint_IsStalled())
188 {
189 USB_USBTask();
190
191 if (MSInterfaceInfo->State.IsMassStoreReset)
192 return;
193 }
194
195 CallbackMSInterfaceInfo = MSInterfaceInfo;
196 Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),
197 StreamCallback_MS_Device_AbortOnMassStoreReset);
198
199 Endpoint_ClearIN();
200
201 if (MSInterfaceInfo->State.IsMassStoreReset)
202 return;
203 }
204
205 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)
206 {
207 MS_Device_USBTask(CallbackMSInterfaceInfo);
208
209 if (CallbackMSInterfaceInfo->State.IsMassStoreReset)
210 return STREAMCALLBACK_Abort;
211 else
212 return STREAMCALLBACK_Continue;
213 }
214
215 #endif