Add more include protection macros to give the user warnings when they try to manuall...
[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 PIMAHeader->TransactionID = SIInterfaceInfo->State.TransactionID++;
155
156 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
157 Pipe_Unfreeze();
158
159 if ((ErrorCode = Pipe_Write_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
160 return ErrorCode;
161
162 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
163
164 if (ParamBytes)
165 {
166 if ((ErrorCode = Pipe_Write_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
167 return ErrorCode;
168 }
169
170 Pipe_ClearOUT();
171 Pipe_Freeze();
172
173 return PIPE_RWSTREAM_NoError;
174 }
175
176 uint8_t SImage_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo, SI_PIMA_Container_t* const PIMAHeader)
177 {
178 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
179
180 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
181 return PIPE_RWSTREAM_DeviceDisconnected;
182
183 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
184 Pipe_Unfreeze();
185
186 while (!(Pipe_IsReadWriteAllowed()))
187 {
188 if (USB_INT_HasOccurred(USB_INT_HSOFI))
189 {
190 USB_INT_Clear(USB_INT_HSOFI);
191 TimeoutMSRem--;
192
193 if (!(TimeoutMSRem))
194 {
195 return PIPE_RWSTREAM_Timeout;
196 }
197 }
198
199 Pipe_Freeze();
200 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
201 Pipe_Unfreeze();
202
203 if (Pipe_IsStalled())
204 {
205 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataOUTPipeNumber);
206 return PIPE_RWSTREAM_PipeStalled;
207 }
208
209 Pipe_Freeze();
210 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
211 Pipe_Unfreeze();
212
213 if (Pipe_IsStalled())
214 {
215 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataINPipeNumber);
216 return PIPE_RWSTREAM_PipeStalled;
217 }
218
219 if (USB_HostState == HOST_STATE_Unattached)
220 return PIPE_RWSTREAM_DeviceDisconnected;
221 }
222
223 Pipe_Read_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK);
224
225 if (PIMAHeader->Type == CType_ResponseBlock)
226 {
227 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
228
229 if (ParamBytes)
230 Pipe_Read_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK);
231
232 Pipe_ClearIN();
233 }
234
235 Pipe_Freeze();
236
237 return PIPE_RWSTREAM_NoError;
238 }
239
240 uint8_t SImage_Host_SendData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo, void* Buffer, const uint16_t Bytes)
241 {
242 uint8_t ErrorCode;
243
244 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
245 return PIPE_RWSTREAM_DeviceDisconnected;
246
247 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
248 Pipe_Unfreeze();
249
250 ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
251
252 Pipe_ClearOUT();
253 Pipe_Freeze();
254
255 return ErrorCode;
256 }
257
258 uint8_t SImage_Host_ReadData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo, void* Buffer, const uint16_t Bytes)
259 {
260 uint8_t ErrorCode;
261
262 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
263 return PIPE_RWSTREAM_DeviceDisconnected;
264
265 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
266 Pipe_Unfreeze();
267
268 ErrorCode = Pipe_Read_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
269
270 Pipe_Freeze();
271
272 return ErrorCode;
273 }
274
275 bool SImage_Host_IsEventReceived(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
276 {
277 bool IsEventReceived = false;
278
279 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
280 return false;
281
282 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
283 Pipe_Unfreeze();
284
285 if (Pipe_BytesInPipe())
286 IsEventReceived = true;
287
288 Pipe_Freeze();
289
290 return IsEventReceived;
291 }
292
293 uint8_t SImage_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo, SI_PIMA_Container_t* const PIMAHeader)
294 {
295 uint8_t ErrorCode;
296
297 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
298 return PIPE_RWSTREAM_DeviceDisconnected;
299
300 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
301 Pipe_Unfreeze();
302
303 ErrorCode = Pipe_Read_Stream_LE(PIMAHeader, sizeof(SI_PIMA_Container_t), NO_STREAM_CALLBACK);
304
305 Pipe_ClearIN();
306 Pipe_Freeze();
307
308 return ErrorCode;
309 }
310
311 uint8_t SImage_Host_OpenSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
312 {
313 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
314 return HOST_SENDCONTROL_DeviceDisconnected;
315
316 uint8_t ErrorCode;
317
318 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
319 {
320 .DataLength = PIMA_COMMAND_SIZE(0),
321 .Type = CType_CommandBlock,
322 .Code = 0x1002,
323 .Params = {},
324 };
325
326 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
327 return ErrorCode;
328
329 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
330 return ErrorCode;
331
332 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
333 return SI_ERROR_LOGICAL_CMD_FAILED;
334
335 SIInterfaceInfo->State.TransactionID = 0;
336 SIInterfaceInfo->State.IsSessionOpen = true;
337
338 return PIPE_RWSTREAM_NoError;
339 }
340
341 uint8_t SImage_Host_CloseSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
342 {
343 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
344 return HOST_SENDCONTROL_DeviceDisconnected;
345
346 uint8_t ErrorCode;
347
348 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
349 {
350 .DataLength = PIMA_COMMAND_SIZE(0),
351 .Type = CType_CommandBlock,
352 .Code = 0x1003,
353 .Params = {},
354 };
355
356 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
357 return ErrorCode;
358
359 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
360 return ErrorCode;
361
362 SIInterfaceInfo->State.IsSessionOpen = false;
363
364 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
365 return SI_ERROR_LOGICAL_CMD_FAILED;
366
367 return PIPE_RWSTREAM_NoError;
368 }
369
370 uint8_t SImage_Host_SendCommand(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo, const uint16_t Operation,
371 const uint8_t TotalParams, uint32_t* Params)
372 {
373 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
374 return HOST_SENDCONTROL_DeviceDisconnected;
375
376 uint8_t ErrorCode;
377
378 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
379 {
380 .DataLength = PIMA_COMMAND_SIZE(TotalParams),
381 .Type = CType_CommandBlock,
382 .Code = Operation,
383 };
384
385 memcpy(&PIMABlock.Params, Params, sizeof(uint32_t) * TotalParams);
386
387 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
388 return ErrorCode;
389
390 return PIPE_RWSTREAM_NoError;
391 }
392
393 uint8_t SImage_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
394 {
395 uint8_t ErrorCode;
396 SI_PIMA_Container_t PIMABlock;
397
398 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
399 return HOST_SENDCONTROL_DeviceDisconnected;
400
401 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
402 return ErrorCode;
403
404 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
405 return SI_ERROR_LOGICAL_CMD_FAILED;
406
407 return PIPE_RWSTREAM_NoError;
408 }
409
410 #endif