Changed over www.fourwalledcubicle.com links to the new www.lufa-lib.org redirect...
[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 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
127 {
128 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
129 USB_Descriptor_Interface_t);
130
131 if ((CurrentInterface->Class == SI_CSCP_StillImageClass) &&
132 (CurrentInterface->SubClass == SI_CSCP_StillImageSubclass) &&
133 (CurrentInterface->Protocol == SI_CSCP_BulkOnlyProtocol))
134 {
135 return DESCRIPTOR_SEARCH_Found;
136 }
137 }
138
139 return DESCRIPTOR_SEARCH_NotFound;
140 }
141
142 uint8_t DCOMP_SI_Host_NextSIInterfaceEndpoint(void* const CurrentDescriptor)
143 {
144 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
145 {
146 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
147 USB_Descriptor_Endpoint_t);
148
149 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
150
151 if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
152 (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress))))
153 {
154 return DESCRIPTOR_SEARCH_Found;
155 }
156 }
157 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
158 {
159 return DESCRIPTOR_SEARCH_Fail;
160 }
161
162 return DESCRIPTOR_SEARCH_NotFound;
163 }
164
165 uint8_t SI_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
166 PIMA_Container_t* const PIMAHeader)
167 {
168 uint8_t ErrorCode;
169
170 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
171 return PIPE_RWSTREAM_DeviceDisconnected;
172
173 if (SIInterfaceInfo->State.IsSessionOpen)
174 PIMAHeader->TransactionID = SIInterfaceInfo->State.TransactionID++;
175
176 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
177 Pipe_Unfreeze();
178
179 if ((ErrorCode = Pipe_Write_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
180 return ErrorCode;
181
182 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
183
184 if (ParamBytes)
185 {
186 if ((ErrorCode = Pipe_Write_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
187 return ErrorCode;
188 }
189
190 Pipe_ClearOUT();
191 Pipe_Freeze();
192
193 return PIPE_RWSTREAM_NoError;
194 }
195
196 uint8_t SI_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
197 PIMA_Container_t* const PIMAHeader)
198 {
199 uint16_t TimeoutMSRem = SI_COMMAND_DATA_TIMEOUT_MS;
200 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
201
202 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
203 return PIPE_RWSTREAM_DeviceDisconnected;
204
205 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
206 Pipe_Unfreeze();
207
208 while (!(Pipe_IsINReceived()))
209 {
210 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
211
212 if (CurrentFrameNumber != PreviousFrameNumber)
213 {
214 PreviousFrameNumber = CurrentFrameNumber;
215
216 if (!(TimeoutMSRem--))
217 return PIPE_RWSTREAM_Timeout;
218 }
219
220 Pipe_Freeze();
221 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
222 Pipe_Unfreeze();
223
224 if (Pipe_IsStalled())
225 {
226 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataOUTPipeNumber);
227 return PIPE_RWSTREAM_PipeStalled;
228 }
229
230 Pipe_Freeze();
231 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
232 Pipe_Unfreeze();
233
234 if (Pipe_IsStalled())
235 {
236 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataINPipeNumber);
237 return PIPE_RWSTREAM_PipeStalled;
238 }
239
240 if (USB_HostState == HOST_STATE_Unattached)
241 return PIPE_RWSTREAM_DeviceDisconnected;
242 }
243
244 Pipe_Read_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK);
245
246 if (PIMAHeader->Type == PIMA_CONTAINER_ResponseBlock)
247 {
248 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
249
250 if (ParamBytes)
251 Pipe_Read_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK);
252
253 Pipe_ClearIN();
254 }
255
256 Pipe_Freeze();
257
258 return PIPE_RWSTREAM_NoError;
259 }
260
261 uint8_t SI_Host_SendData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
262 void* Buffer,
263 const uint16_t Bytes)
264 {
265 uint8_t ErrorCode;
266
267 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
268 return PIPE_RWSTREAM_DeviceDisconnected;
269
270 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
271 Pipe_Unfreeze();
272
273 ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
274
275 Pipe_ClearOUT();
276 Pipe_Freeze();
277
278 return ErrorCode;
279 }
280
281 uint8_t SI_Host_ReadData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
282 void* Buffer,
283 const uint16_t Bytes)
284 {
285 uint8_t ErrorCode;
286
287 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
288 return PIPE_RWSTREAM_DeviceDisconnected;
289
290 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
291 Pipe_Unfreeze();
292
293 ErrorCode = Pipe_Read_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
294
295 Pipe_Freeze();
296
297 return ErrorCode;
298 }
299
300 bool SI_Host_IsEventReceived(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
301 {
302 bool IsEventReceived = false;
303
304 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
305 return false;
306
307 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
308 Pipe_Unfreeze();
309
310 if (Pipe_BytesInPipe())
311 IsEventReceived = true;
312
313 Pipe_Freeze();
314
315 return IsEventReceived;
316 }
317
318 uint8_t SI_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
319 PIMA_Container_t* const PIMAHeader)
320 {
321 uint8_t ErrorCode;
322
323 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
324 return PIPE_RWSTREAM_DeviceDisconnected;
325
326 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
327 Pipe_Unfreeze();
328
329 ErrorCode = Pipe_Read_Stream_LE(PIMAHeader, sizeof(PIMA_Container_t), NO_STREAM_CALLBACK);
330
331 Pipe_ClearIN();
332 Pipe_Freeze();
333
334 return ErrorCode;
335 }
336
337 uint8_t SI_Host_OpenSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
338 {
339 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
340 return HOST_SENDCONTROL_DeviceDisconnected;
341
342 uint8_t ErrorCode;
343
344 SIInterfaceInfo->State.TransactionID = 0;
345 SIInterfaceInfo->State.IsSessionOpen = false;
346
347 PIMA_Container_t PIMABlock = (PIMA_Container_t)
348 {
349 .DataLength = PIMA_COMMAND_SIZE(1),
350 .Type = PIMA_CONTAINER_CommandBlock,
351 .Code = 0x1002,
352 .Params = {1},
353 };
354
355 if ((ErrorCode = SI_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
356 return ErrorCode;
357
358 if ((ErrorCode = SI_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
359 return ErrorCode;
360
361 if ((PIMABlock.Type != PIMA_CONTAINER_ResponseBlock) || (PIMABlock.Code != 0x2001))
362 return SI_ERROR_LOGICAL_CMD_FAILED;
363
364 SIInterfaceInfo->State.IsSessionOpen = true;
365
366 return PIPE_RWSTREAM_NoError;
367 }
368
369 uint8_t SI_Host_CloseSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
370 {
371 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
372 return HOST_SENDCONTROL_DeviceDisconnected;
373
374 uint8_t ErrorCode;
375
376 PIMA_Container_t PIMABlock = (PIMA_Container_t)
377 {
378 .DataLength = PIMA_COMMAND_SIZE(1),
379 .Type = PIMA_CONTAINER_CommandBlock,
380 .Code = 0x1003,
381 .Params = {1},
382 };
383
384 if ((ErrorCode = SI_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
385 return ErrorCode;
386
387 if ((ErrorCode = SI_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
388 return ErrorCode;
389
390 SIInterfaceInfo->State.IsSessionOpen = false;
391
392 if ((PIMABlock.Type != PIMA_CONTAINER_ResponseBlock) || (PIMABlock.Code != 0x2001))
393 return SI_ERROR_LOGICAL_CMD_FAILED;
394
395 return PIPE_RWSTREAM_NoError;
396 }
397
398 uint8_t SI_Host_SendCommand(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
399 const uint16_t Operation,
400 const uint8_t TotalParams,
401 uint32_t* const Params)
402 {
403 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
404 return HOST_SENDCONTROL_DeviceDisconnected;
405
406 uint8_t ErrorCode;
407
408 PIMA_Container_t PIMABlock = (PIMA_Container_t)
409 {
410 .DataLength = PIMA_COMMAND_SIZE(TotalParams),
411 .Type = PIMA_CONTAINER_CommandBlock,
412 .Code = Operation,
413 };
414
415 memcpy(&PIMABlock.Params, Params, sizeof(uint32_t) * TotalParams);
416
417 if ((ErrorCode = SI_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
418 return ErrorCode;
419
420 return PIPE_RWSTREAM_NoError;
421 }
422
423 uint8_t SI_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
424 {
425 uint8_t ErrorCode;
426 PIMA_Container_t PIMABlock;
427
428 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
429 return HOST_SENDCONTROL_DeviceDisconnected;
430
431 if ((ErrorCode = SI_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
432 return ErrorCode;
433
434 if ((PIMABlock.Type != PIMA_CONTAINER_ResponseBlock) || (PIMABlock.Code != 0x2001))
435 return SI_ERROR_LOGICAL_CMD_FAILED;
436
437 return PIPE_RWSTREAM_NoError;
438 }
439
440 #endif
441