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