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