Changed Still Image Host class driver to auto-fill TransactionID element of sent...
[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 #warning The Still Image Host mode Class driver is currently incomplete and is for preview purposes only.
38
39 uint8_t SI_Host_ConfigurePipes(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, uint16_t ConfigDescriptorLength,
40 uint8_t* 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(&ConfigDescriptorLength, &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(&ConfigDescriptorLength, &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 PIPE_BANK_DOUBLE);
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 PIPE_BANK_DOUBLE);
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 PIPE_BANK_DOUBLE);
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* 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* 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 void SI_Host_USBTask(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
148 {
149
150 }
151
152 static uint8_t SImage_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, SI_PIMA_Container_t* PIMAHeader)
153 {
154 uint8_t ErrorCode;
155
156 PIMAHeader->TransactionID = SIInterfaceInfo->State.TransactionID++;
157
158 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
159 Pipe_Unfreeze();
160
161 if ((ErrorCode = Pipe_Write_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
162 return ErrorCode;
163
164 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
165
166 if (ParamBytes)
167 {
168 if ((ErrorCode = Pipe_Write_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
169 return ErrorCode;
170 }
171
172 Pipe_ClearOUT();
173 Pipe_Freeze();
174
175 return PIPE_RWSTREAM_NoError;
176 }
177
178 static uint8_t SImage_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, SI_PIMA_Container_t* PIMAHeader)
179 {
180 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
181
182 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
183 Pipe_Unfreeze();
184
185 while (!(Pipe_IsReadWriteAllowed()))
186 {
187 if (USB_INT_HasOccurred(USB_INT_HSOFI))
188 {
189 USB_INT_Clear(USB_INT_HSOFI);
190 TimeoutMSRem--;
191
192 if (!(TimeoutMSRem))
193 {
194 return PIPE_RWSTREAM_Timeout;
195 }
196 }
197
198 Pipe_Freeze();
199 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
200 Pipe_Unfreeze();
201
202 if (Pipe_IsStalled())
203 {
204 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataOUTPipeNumber);
205 return PIPE_RWSTREAM_PipeStalled;
206 }
207
208 Pipe_Freeze();
209 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
210 Pipe_Unfreeze();
211
212 if (Pipe_IsStalled())
213 {
214 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataINPipeNumber);
215 return PIPE_RWSTREAM_PipeStalled;
216 }
217
218 if (USB_HostState == HOST_STATE_Unattached)
219 return PIPE_RWSTREAM_DeviceDisconnected;
220 }
221
222 Pipe_Read_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK);
223
224 if (PIMAHeader->Type == CType_ResponseBlock)
225 {
226 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
227
228 if (ParamBytes)
229 Pipe_Read_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK);
230
231 Pipe_ClearIN();
232
233 PIMAHeader->Code &= 0x0000000F;
234 }
235
236 Pipe_Freeze();
237
238 return PIPE_RWSTREAM_NoError;
239 }
240
241 static uint8_t SImage_Host_SendData(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, void* Buffer, uint16_t Bytes)
242 {
243 uint8_t ErrorCode;
244
245 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
246 Pipe_Unfreeze();
247
248 ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
249
250 Pipe_ClearOUT();
251 Pipe_Freeze();
252
253 return ErrorCode;
254 }
255
256 static uint8_t SImage_Host_ReadData(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, void* Buffer, uint16_t Bytes)
257 {
258 uint8_t ErrorCode;
259
260 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
261 Pipe_Unfreeze();
262
263 ErrorCode = Pipe_Read_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
264
265 Pipe_Freeze();
266
267 return ErrorCode;
268 }
269
270 static bool SImage_Host_IsEventReceived(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
271 {
272 bool IsEventReceived = false;
273
274 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
275 Pipe_Unfreeze();
276
277 if (Pipe_BytesInPipe())
278 IsEventReceived = true;
279
280 Pipe_Freeze();
281
282 return IsEventReceived;
283 }
284
285 static uint8_t SImage_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, SI_PIMA_Container_t* PIMAHeader)
286 {
287 uint8_t ErrorCode;
288
289 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
290 Pipe_Unfreeze();
291
292 ErrorCode = Pipe_Read_Stream_LE(PIMAHeader, sizeof(SI_PIMA_Container_t), NO_STREAM_CALLBACK);
293
294 Pipe_ClearIN();
295 Pipe_Freeze();
296
297 return ErrorCode;
298 }
299
300 uint8_t SImage_Host_OpenSession(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
301 {
302 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
303 return HOST_SENDCONTROL_DeviceDisconnect;
304
305 uint8_t ErrorCode;
306
307 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
308 {
309 .DataLength = PIMA_COMMAND_SIZE(0),
310 .Type = CType_CommandBlock,
311 .Code = 0x1002,
312 .Params = {},
313 };
314
315 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
316 return ErrorCode;
317
318 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
319 return ErrorCode;
320
321 SIInterfaceInfo->State.TransactionID = 0;
322 SIInterfaceInfo->State.IsSessionOpen = true;
323
324 return PIPE_RWSTREAM_NoError;
325 }
326
327 uint8_t SImage_Host_CloseSession(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
328 {
329 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
330 return HOST_SENDCONTROL_DeviceDisconnect;
331
332 uint8_t ErrorCode;
333
334 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
335 {
336 .DataLength = PIMA_COMMAND_SIZE(0),
337 .Type = CType_CommandBlock,
338 .Code = 0x1003,
339 .Params = {},
340 };
341
342 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
343 return ErrorCode;
344
345 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
346 return ErrorCode;
347
348 SIInterfaceInfo->State.IsSessionOpen = false;
349
350 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
351 return SI_ERROR_LOGICAL_CMD_FAILED;
352
353 return PIPE_RWSTREAM_NoError;
354 }
355
356 uint8_t SImage_Host_SendCommand(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, uint16_t Operation, uint8_t UsedParams,
357 uint32_t Param1, uint32_t Param2, uint32_t Param3, void* DataBuff)
358 {
359 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
360 return HOST_SENDCONTROL_DeviceDisconnect;
361
362 uint8_t ErrorCode;
363
364 SI_PIMA_Container_t PIMABlock = (SI_PIMA_Container_t)
365 {
366 .DataLength = PIMA_COMMAND_SIZE(UsedParams),
367 .Type = CType_CommandBlock,
368 .Code = Operation,
369 .Params = {Param1, Param2, Param3},
370 };
371
372 if ((ErrorCode = SImage_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
373 return ErrorCode;
374
375 if ((ErrorCode = SImage_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
376 return ErrorCode;
377
378 if ((PIMABlock.Type != CType_ResponseBlock) || (PIMABlock.Code != 0x2001))
379 return SI_ERROR_LOGICAL_CMD_FAILED;
380
381 return PIPE_RWSTREAM_NoError;
382 }
383
384 #endif