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