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