More work on the Mass Storage Host mode Class driver.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / 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_HOST)
33
34 #define INCLUDE_FROM_MS_CLASS_HOST_C
35 #include "MassStorage.h"
36
37 #warning The Mass Storage Host mode Class driver is currently incomplete and is for preview purposes only.
38
39 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint16_t ConfigDescriptorLength,
40 uint8_t* DeviceConfigDescriptor)
41 {
42 uint8_t FoundEndpoints = 0;
43
44 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
45
46 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
47 return MS_ENUMERROR_InvalidConfigDescriptor;
48
49 if (USB_GetNextDescriptorComp(&ConfigDescriptorLength, &DeviceConfigDescriptor,
50 DComp_NextMassStorageInterface) != DESCRIPTOR_SEARCH_COMP_Found)
51 {
52 return MS_ENUMERROR_NoMSInterfaceFound;
53 }
54
55 MSInterfaceInfo->State.InterfaceNumber =
56 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
57 DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Interface_t)->InterfaceNumber;
58 #else
59 DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Interface_t)->bInterfaceNumber;
60 #endif
61
62 while (FoundEndpoints != (MS_FOUND_DATAPIPE_IN | MS_FOUND_DATAPIPE_OUT))
63 {
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorLength, &DeviceConfigDescriptor,
65 DComp_NextInterfaceBulkDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
66 {
67 return MS_ENUMERROR_EndpointsNotFound;
68 }
69
70 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
71
72 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
73 {
74 Pipe_ConfigurePipe(MSInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
75 EndpointData->EndpointAddress, EndpointData->EndpointSize,
76 PIPE_BANK_DOUBLE);
77 MSInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
78
79 FoundEndpoints |= MS_FOUND_DATAPIPE_IN;
80 }
81 else
82 {
83 Pipe_ConfigurePipe(MSInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
84 EndpointData->EndpointAddress, EndpointData->EndpointSize,
85 PIPE_BANK_DOUBLE);
86 MSInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
87
88 FoundEndpoints |= MS_FOUND_DATAPIPE_OUT;
89 }
90 }
91
92 MSInterfaceInfo->State.Active = true;
93 return MS_ENUMERROR_NoError;
94 }
95
96 static uint8_t DComp_NextMassStorageInterface(void* CurrentDescriptor)
97 {
98 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
99 {
100 if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == MASS_STORE_CLASS) &&
101 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == MASS_STORE_SUBCLASS) &&
102 (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == MASS_STORE_PROTOCOL))
103 {
104 return DESCRIPTOR_SEARCH_Found;
105 }
106 }
107
108 return DESCRIPTOR_SEARCH_NotFound;
109 }
110
111 static uint8_t DComp_NextInterfaceBulkDataEndpoint(void* CurrentDescriptor)
112 {
113 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
114 {
115 uint8_t EndpointType = (DESCRIPTOR_CAST(CurrentDescriptor,
116 USB_Descriptor_Endpoint_t).Attributes & EP_TYPE_MASK);
117
118 if (EndpointType == EP_TYPE_BULK)
119 return DESCRIPTOR_SEARCH_Found;
120 }
121 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
122 {
123 return DESCRIPTOR_SEARCH_Fail;
124 }
125
126 return DESCRIPTOR_SEARCH_NotFound;
127 }
128
129 void MS_Host_USBTask(USB_ClassInfo_MS_Host_t* MSInterfaceInfo)
130 {
131
132 }
133
134 static uint8_t MassStore_SendCommand(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, MS_CommandBlockWrapper_t* SCSICommandBlock)
135 {
136 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
137
138 if (++MSInterfaceInfo->State.TransactionTag == 0xFFFFFFFF)
139 MSInterfaceInfo->State.TransactionTag = 1;
140
141 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
142 Pipe_Unfreeze();
143
144 if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t))) != PIPE_RWSTREAM_NoError)
145 return ErrorCode;
146
147 Pipe_ClearOUT();
148 while(!(Pipe_IsOUTReady()));
149
150 Pipe_Freeze();
151
152 return PIPE_RWSTREAM_NoError;
153 }
154
155 static uint8_t MassStore_WaitForDataReceived(USB_ClassInfo_MS_Host_t* MSInterfaceInfo)
156 {
157 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
158
159 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
160 Pipe_Unfreeze();
161
162 while (!(Pipe_IsINReceived()))
163 {
164 if (USB_INT_HasOccurred(USB_INT_HSOFI))
165 {
166 USB_INT_Clear(USB_INT_HSOFI);
167 TimeoutMSRem--;
168
169 if (!(TimeoutMSRem))
170 return PIPE_RWSTREAM_Timeout;
171 }
172
173 Pipe_Freeze();
174 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
175 Pipe_Unfreeze();
176
177 if (Pipe_IsStalled())
178 {
179 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataOUTPipeNumber);
180
181 return PIPE_RWSTREAM_PipeStalled;
182 }
183
184 Pipe_Freeze();
185 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
186 Pipe_Unfreeze();
187
188 if (Pipe_IsStalled())
189 {
190 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataINPipeNumber);
191
192 return PIPE_RWSTREAM_PipeStalled;
193 }
194
195 if (USB_HostState == HOST_STATE_Unattached)
196 return PIPE_RWSTREAM_DeviceDisconnected;
197 };
198
199 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
200 Pipe_Freeze();
201
202 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
203 Pipe_Freeze();
204
205 return PIPE_RWSTREAM_NoError;
206 }
207
208 static uint8_t MassStore_SendReceiveData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo,
209 MS_CommandBlockWrapper_t* SCSICommandBlock, void* BufferPtr)
210 {
211 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
212 uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
213
214 if (SCSICommandBlock->Flags & COMMAND_DIRECTION_DATA_IN)
215 {
216 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
217 Pipe_Unfreeze();
218
219 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_NoError)
220 return ErrorCode;
221
222 Pipe_ClearIN();
223 }
224 else
225 {
226 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
227 Pipe_Unfreeze();
228
229 if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_NoError)
230 return ErrorCode;
231
232 Pipe_ClearOUT();
233
234 while (!(Pipe_IsOUTReady()))
235 {
236 if (USB_HostState == HOST_STATE_Unattached)
237 return PIPE_RWSTREAM_DeviceDisconnected;
238 }
239 }
240
241 Pipe_Freeze();
242
243 return PIPE_RWSTREAM_NoError;
244 }
245
246 static uint8_t MassStore_GetReturnedStatus(USB_ClassInfo_MS_Host_t* MSInterfaceInfo,
247 MS_CommandStatusWrapper_t* SCSICommandStatus)
248 {
249 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
250
251 if ((ErrorCode = MassStore_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
252 return ErrorCode;
253
254 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
255 Pipe_Unfreeze();
256
257 if ((ErrorCode = Pipe_Read_Stream_LE(&SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t))) != PIPE_RWSTREAM_NoError)
258 return ErrorCode;
259
260 Pipe_ClearIN();
261 Pipe_Freeze();
262
263 return PIPE_RWSTREAM_NoError;
264 }
265
266 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* MSInterfaceInfo)
267 {
268 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active))
269 return HOST_SENDCONTROL_DeviceDisconnect;
270
271 USB_ControlRequest = (USB_Request_Header_t)
272 {
273 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
274 .bRequest = REQ_MassStorageReset,
275 .wValue = 0,
276 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
277 .wLength = 0,
278 };
279
280 Pipe_SelectPipe(PIPE_CONTROLPIPE);
281
282 return USB_Host_SendControlRequest(NULL);
283 }
284
285 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t* MaxLUNIndex)
286 {
287 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.Active))
288 return HOST_SENDCONTROL_DeviceDisconnect;
289
290 uint8_t ErrorCode;
291
292 USB_ControlRequest = (USB_Request_Header_t)
293 {
294 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
295 .bRequest = REQ_GetMaxLUN,
296 .wValue = 0,
297 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
298 .wLength = 1,
299 };
300
301 Pipe_SelectPipe(PIPE_CONTROLPIPE);
302
303 if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)
304 {
305 Pipe_ClearStall();
306
307 *MaxLUNIndex = 0;
308 }
309
310 return ErrorCode;
311 }
312
313 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, SCSI_Inquiry_Response_t* InquiryData)
314 {
315
316 }
317
318 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool* DeviceReady);
319 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,
320 SCSI_Capacity_t* DeviceCapacity);
321
322 #endif