Added new SCSI_ASENSE_NOT_READY_TO_READY_CHANGE constant to the Mass Storage class...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / StillImage.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_HOST)
34
35 #define __INCLUDE_FROM_SI_CLASS_HOST_C
36 #define __INCLUDE_FROM_SI_DRIVER
37 #include "StillImage.h"
38
39 uint8_t SImage_Host_ConfigurePipes(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
40 uint16_t ConfigDescriptorSize,
41 void* DeviceConfigDescriptor)
42 {
43 uint8_t FoundEndpoints = 0;
44
45 memset(&SIInterfaceInfo->State, 0x00, sizeof(SIInterfaceInfo->State));
46
47 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
48 return SI_ENUMERROR_InvalidConfigDescriptor;
49
50 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
51 DCOMP_SI_Host_NextSIInterface) != DESCRIPTOR_SEARCH_COMP_Found)
52 {
53 return SI_ENUMERROR_NoSIInterfaceFound;
54 }
55
56 while (FoundEndpoints != (SI_FOUND_EVENTS_IN | SI_FOUND_DATAPIPE_IN | SI_FOUND_DATAPIPE_OUT))
57 {
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
59 DCOMP_SI_Host_NextSIInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
60 {
61 return SI_ENUMERROR_EndpointsNotFound;
62 }
63
64 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
65
66 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
67 {
68 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
69 {
70 Pipe_ConfigurePipe(SIInterfaceInfo->Config.EventsPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
71 EndpointData->EndpointAddress, EndpointData->EndpointSize,
72 SIInterfaceInfo->Config.EventsPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
73 SIInterfaceInfo->State.EventsPipeSize = EndpointData->EndpointSize;
74
75 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
76
77 FoundEndpoints |= SI_FOUND_EVENTS_IN;
78 }
79 }
80 else
81 {
82 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
83 {
84 Pipe_ConfigurePipe(SIInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
85 EndpointData->EndpointAddress, EndpointData->EndpointSize,
86 SIInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
87 SIInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
88
89 FoundEndpoints |= SI_FOUND_DATAPIPE_IN;
90 }
91 else
92 {
93 Pipe_ConfigurePipe(SIInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
94 EndpointData->EndpointAddress, EndpointData->EndpointSize,
95 SIInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
96 SIInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
97
98 FoundEndpoints |= SI_FOUND_DATAPIPE_OUT;
99 }
100 }
101 }
102
103 SIInterfaceInfo->State.IsActive = true;
104 return SI_ENUMERROR_NoError;
105 }
106
107 uint8_t DCOMP_SI_Host_NextSIInterface(void* const CurrentDescriptor)
108 {
109 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
110 {
111 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
112 USB_Descriptor_Interface_t);
113
114 if ((CurrentInterface->Class == STILL_IMAGE_CLASS) &&
115 (CurrentInterface->SubClass == STILL_IMAGE_SUBCLASS) &&
116 (CurrentInterface->Protocol == STILL_IMAGE_PROTOCOL))
117 {
118 return DESCRIPTOR_SEARCH_Found;
119 }
120 }
121
122 return DESCRIPTOR_SEARCH_NotFound;
123 }
124
125 uint8_t DCOMP_SI_Host_NextSIInterfaceEndpoint(void* const CurrentDescriptor)
126 {
127 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
128 {
129 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
130 USB_Descriptor_Endpoint_t);
131
132 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
133
134 if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
135 (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress))))
136 {
137 return DESCRIPTOR_SEARCH_Found;
138 }
139 }
140 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
141 {
142 return DESCRIPTOR_SEARCH_Fail;
143 }
144
145 return DESCRIPTOR_SEARCH_NotFound;
146 }
147
148 uint8_t SImage_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
149 SI_PIMA_Container_t* const PIMAHeader)
150 {
151 uint8_t ErrorCode;
152
153 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
154 return PIPE_RWSTREAM_DeviceDisconnected;
155
156 if (SIInterfaceInfo->State.IsSessionOpen)
157 PIMAHeader->TransactionID = SIInterfaceInfo->State.TransactionID++;
158
159 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
160 Pipe_Unfreeze();
161
162 if ((ErrorCode = Pipe_Write_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
163 return ErrorCode;
164
165 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
166
167 if (ParamBytes)
168 {
169 if ((ErrorCode = Pipe_Write_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
170 return ErrorCode;
171 }
172
173 Pipe_ClearOUT();
174 Pipe_Freeze();
175
176 return PIPE_RWSTREAM_NoError;
177 }
178
179 uint8_t SImage_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
180 SI_PIMA_Container_t* const PIMAHeader)
181 {
182 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
183
184 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
185 return PIPE_RWSTREAM_DeviceDisconnected;
186
187 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
188 Pipe_Unfreeze();
189
190 while (!(Pipe_IsReadWriteAllowed()))
191 {
192 if (USB_INT_HasOccurred(USB_INT_HSOFI))
193 {
194 USB_INT_Clear(USB_INT_HSOFI);
195 TimeoutMSRem--;
196
197 if (!(TimeoutMSRem))
198 {
199 return PIPE_RWSTREAM_Timeout;
200 }
201 }
202
203 Pipe_Freeze();
204 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
205 Pipe_Unfreeze();
206
207 if (Pipe_IsStalled())
208 {
209 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataOUTPipeNumber);
210 return PIPE_RWSTREAM_PipeStalled;
211 }
212
213 Pipe_Freeze();
214 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
215 Pipe_Unfreeze();
216
217 if (Pipe_IsStalled())
218 {
219 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataINPipeNumber);
220 return PIPE_RWSTREAM_PipeStalled;
221 }
222
223 if (USB_HostState == HOST_STATE_Unattached)
224 return PIPE_RWSTREAM_DeviceDisconnected;
225 }
226
227 Pipe_Read_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK);
228
229 if (PIMAHeader->Type == CType_ResponseBlock)
230 {
231 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
232
233 if (ParamBytes)
234 Pipe_Read_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK);
235
236 Pipe_ClearIN();
237 }
238
239 Pipe_Freeze();
240
241 return PIPE_RWSTREAM_NoError;
242 }
243
244 uint8_t SImage_Host_SendData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
245 void* Buffer,
246 const uint16_t Bytes)
247 {
248 uint8_t ErrorCode;
249
250 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
251 return PIPE_RWSTREAM_DeviceDisconnected;
252
253 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
254 Pipe_Unfreeze();
255
256 ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
257
258 Pipe_ClearOUT();
259 Pipe_Freeze();
260
261 return ErrorCode;
262 }
263
264 uint8_t SImage_Host_ReadData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
265 void* Buffer,
266 const uint16_t Bytes)
267 {
268 uint8_t ErrorCode;
269
270 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
271 return PIPE_RWSTREAM_DeviceDisconnected;
272
273 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
274 Pipe_Unfreeze();
275
276 ErrorCode = Pipe_Read_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
277
278 Pipe_Freeze();
279
280 return ErrorCode;
281 }
282
283 bool SImage_Host_IsEventReceived(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
284 {
285 bool IsEventReceived = false;
286
287 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
288 return false;
289
290 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
291 Pipe_Unfreeze();
292
293 if (Pipe_BytesInPipe())
294 IsEventReceived = true;
295
296 Pipe_Freeze();
297
298 return IsEventReceived;
299 }
300
301 uint8_t SImage_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
302 SI_PIMA_Container_t* const PIMAHeader)
303 {
304 uint8_t ErrorCode;
305
306 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
307 return PIPE_RWSTREAM_DeviceDisconnected;
308
309 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
310 Pipe_Unfreeze();
311
312 ErrorCode = Pipe_Read_Stream_LE(PIMAHeader, sizeof(SI_PIMA_Container_t), NO_STREAM_CALLBACK);
313
314 Pipe_ClearIN();
315 Pipe_Freeze();
316
317 return ErrorCode;
318 }
319
320 uint8_t SImage_Host_OpenSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
321 {
322 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
323 return HOST_SENDCONTROL_DeviceDisconnected;
324
325 uint8_t ErrorCode;
326
327 SIInterfaceInfo->State.TransactionID = 0;
328 SIInterfaceInfo->State.IsSessionOpen = false;
329
330 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
331 {
332 .DataLength = PIMA_COMMAND_SIZE(1),
333 .Type = CType_CommandBlock,
334 .Code = 0x1002,
335 .Params = {1},
336 };
337
338 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
339 return ErrorCode;
340
341 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
342 return ErrorCode;
343
344 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
345 return SI_ERROR_LOGICAL_CMD_FAILED;
346
347 SIInterfaceInfo->State.IsSessionOpen = true;
348
349 return PIPE_RWSTREAM_NoError;
350 }
351
352 uint8_t SImage_Host_CloseSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
353 {
354 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
355 return HOST_SENDCONTROL_DeviceDisconnected;
356
357 uint8_t ErrorCode;
358
359 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
360 {
361 .DataLength = PIMA_COMMAND_SIZE(1),
362 .Type = CType_CommandBlock,
363 .Code = 0x1003,
364 .Params = {1},
365 };
366
367 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
368 return ErrorCode;
369
370 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
371 return ErrorCode;
372
373 SIInterfaceInfo->State.IsSessionOpen = false;
374
375 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
376 return SI_ERROR_LOGICAL_CMD_FAILED;
377
378 return PIPE_RWSTREAM_NoError;
379 }
380
381 uint8_t SImage_Host_SendCommand(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
382 const uint16_t Operation,
383 const uint8_t TotalParams,
384 uint32_t* const Params)
385 {
386 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
387 return HOST_SENDCONTROL_DeviceDisconnected;
388
389 uint8_t ErrorCode;
390
391 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
392 {
393 .DataLength = PIMA_COMMAND_SIZE(TotalParams),
394 .Type = CType_CommandBlock,
395 .Code = Operation,
396 };
397
398 memcpy(&PIMABlock.Params, Params, sizeof(uint32_t) * TotalParams);
399
400 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
401 return ErrorCode;
402
403 return PIPE_RWSTREAM_NoError;
404 }
405
406 uint8_t SImage_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
407 {
408 uint8_t ErrorCode;
409 SI_PIMA_Container_t PIMABlock;
410
411 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
412 return HOST_SENDCONTROL_DeviceDisconnected;
413
414 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
415 return ErrorCode;
416
417 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
418 return SI_ERROR_LOGICAL_CMD_FAILED;
419
420 return PIPE_RWSTREAM_NoError;
421 }
422
423 #endif