3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
21 The author disclaims 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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_SI_DRIVER
37 #define __INCLUDE_FROM_STILLIMAGE_HOST_C
38 #include "StillImageClassHost.h"
40 uint8_t SI_Host_ConfigurePipes(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
41 uint16_t ConfigDescriptorSize,
42 void* ConfigDescriptorData)
44 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
45 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
46 USB_Descriptor_Endpoint_t* EventsEndpoint = NULL;
47 USB_Descriptor_Interface_t* StillImageInterface = NULL;
49 memset(&SIInterfaceInfo->State, 0x00, sizeof(SIInterfaceInfo->State));
51 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
52 return SI_ENUMERROR_InvalidConfigDescriptor;
54 while (!(DataINEndpoint) || !(DataOUTEndpoint) || !(EventsEndpoint))
56 if (!(StillImageInterface) ||
57 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
58 DCOMP_SI_Host_NextSIInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
60 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
61 DCOMP_SI_Host_NextSIInterface) != DESCRIPTOR_SEARCH_COMP_Found)
63 return SI_ENUMERROR_NoCompatibleInterfaceFound;
66 StillImageInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
68 DataINEndpoint = NULL;
69 DataOUTEndpoint = NULL;
70 EventsEndpoint = NULL;
75 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
77 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
79 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
80 EventsEndpoint = EndpointData;
82 DataINEndpoint = EndpointData;
86 DataOUTEndpoint = EndpointData;
90 SIInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
91 SIInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
92 SIInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
94 SIInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
95 SIInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
96 SIInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
98 SIInterfaceInfo->Config.EventsPipe.Size = le16_to_cpu(EventsEndpoint->EndpointSize);
99 SIInterfaceInfo->Config.EventsPipe.EndpointAddress = EventsEndpoint->EndpointAddress;
100 SIInterfaceInfo->Config.EventsPipe.Type = EP_TYPE_INTERRUPT;
102 if (!(Pipe_ConfigurePipeTable(&SIInterfaceInfo->Config.DataINPipe, 1)))
103 return SI_ENUMERROR_PipeConfigurationFailed;
105 if (!(Pipe_ConfigurePipeTable(&SIInterfaceInfo->Config.DataOUTPipe, 1)))
106 return SI_ENUMERROR_PipeConfigurationFailed;
108 if (!(Pipe_ConfigurePipeTable(&SIInterfaceInfo->Config.EventsPipe, 1)))
109 return SI_ENUMERROR_PipeConfigurationFailed;
111 SIInterfaceInfo->State.InterfaceNumber = StillImageInterface->InterfaceNumber;
112 SIInterfaceInfo->State.IsActive = true;
114 return SI_ENUMERROR_NoError;
117 uint8_t DCOMP_SI_Host_NextSIInterface(void* const CurrentDescriptor)
119 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
121 if (Header->Type == DTYPE_Interface)
123 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
125 if ((Interface->Class == SI_CSCP_StillImageClass) &&
126 (Interface->SubClass == SI_CSCP_StillImageSubclass) &&
127 (Interface->Protocol == SI_CSCP_BulkOnlyProtocol))
129 return DESCRIPTOR_SEARCH_Found;
133 return DESCRIPTOR_SEARCH_NotFound;
136 uint8_t DCOMP_SI_Host_NextSIInterfaceEndpoint(void* const CurrentDescriptor)
138 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
140 if (Header->Type == DTYPE_Endpoint)
142 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
144 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
146 if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
147 (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress))))
149 return DESCRIPTOR_SEARCH_Found;
152 else if (Header->Type == DTYPE_Interface)
154 return DESCRIPTOR_SEARCH_Fail;
157 return DESCRIPTOR_SEARCH_NotFound;
160 uint8_t SI_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
161 PIMA_Container_t* const PIMAHeader)
165 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
166 return PIPE_RWSTREAM_DeviceDisconnected;
168 if (SIInterfaceInfo->State.IsSessionOpen)
169 PIMAHeader->TransactionID = cpu_to_le32(SIInterfaceInfo->State.TransactionID++);
171 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipe.Address);
174 if ((ErrorCode = Pipe_Write_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NULL)) != PIPE_RWSTREAM_NoError)
177 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
181 if ((ErrorCode = Pipe_Write_Stream_LE(&PIMAHeader->Params, ParamBytes, NULL)) != PIPE_RWSTREAM_NoError)
188 return PIPE_RWSTREAM_NoError;
191 uint8_t SI_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
192 PIMA_Container_t* const PIMAHeader)
194 uint16_t TimeoutMSRem = SI_COMMAND_DATA_TIMEOUT_MS;
195 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
197 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
198 return PIPE_RWSTREAM_DeviceDisconnected;
200 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipe.Address);
203 while (!(Pipe_IsINReceived()))
205 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
207 if (CurrentFrameNumber != PreviousFrameNumber)
209 PreviousFrameNumber = CurrentFrameNumber;
211 if (!(TimeoutMSRem--))
212 return PIPE_RWSTREAM_Timeout;
216 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipe.Address);
219 if (Pipe_IsStalled())
221 USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
222 return PIPE_RWSTREAM_PipeStalled;
226 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipe.Address);
229 if (Pipe_IsStalled())
231 USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
232 return PIPE_RWSTREAM_PipeStalled;
235 if (USB_HostState == HOST_STATE_Unattached)
236 return PIPE_RWSTREAM_DeviceDisconnected;
239 Pipe_Read_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NULL);
241 if (PIMAHeader->Type == CPU_TO_LE16(PIMA_CONTAINER_ResponseBlock))
243 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
246 Pipe_Read_Stream_LE(&PIMAHeader->Params, ParamBytes, NULL);
253 return PIPE_RWSTREAM_NoError;
256 uint8_t SI_Host_SendData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
258 const uint16_t Bytes)
262 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
263 return PIPE_RWSTREAM_DeviceDisconnected;
265 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipe.Address);
268 ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes, NULL);
276 uint8_t SI_Host_ReadData(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
278 const uint16_t Bytes)
282 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
283 return PIPE_RWSTREAM_DeviceDisconnected;
285 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipe.Address);
288 ErrorCode = Pipe_Read_Stream_LE(Buffer, Bytes, NULL);
295 bool SI_Host_IsEventReceived(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
297 bool IsEventReceived = false;
299 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
302 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipe.Address);
305 if (Pipe_IsINReceived())
306 IsEventReceived = true;
310 return IsEventReceived;
313 uint8_t SI_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
314 PIMA_Container_t* const PIMAHeader)
318 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
319 return PIPE_RWSTREAM_DeviceDisconnected;
321 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipe.Address);
324 ErrorCode = Pipe_Read_Stream_LE(PIMAHeader, sizeof(PIMA_Container_t), NULL);
332 uint8_t SI_Host_OpenSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
334 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
335 return PIPE_RWSTREAM_DeviceDisconnected;
339 SIInterfaceInfo->State.TransactionID = 0;
340 SIInterfaceInfo->State.IsSessionOpen = false;
342 PIMA_Container_t PIMABlock = (PIMA_Container_t)
344 .DataLength = CPU_TO_LE32(PIMA_COMMAND_SIZE(1)),
345 .Type = CPU_TO_LE16(PIMA_CONTAINER_CommandBlock),
346 .Code = CPU_TO_LE16(0x1002),
347 .Params = {CPU_TO_LE32(1)},
350 if ((ErrorCode = SI_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
353 if ((ErrorCode = SI_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
356 if ((PIMABlock.Type != CPU_TO_LE16(PIMA_CONTAINER_ResponseBlock)) || (PIMABlock.Code != CPU_TO_LE16(0x2001)))
357 return SI_ERROR_LOGICAL_CMD_FAILED;
359 SIInterfaceInfo->State.IsSessionOpen = true;
361 return PIPE_RWSTREAM_NoError;
364 uint8_t SI_Host_CloseSession(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
366 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
367 return PIPE_RWSTREAM_DeviceDisconnected;
371 PIMA_Container_t PIMABlock = (PIMA_Container_t)
373 .DataLength = CPU_TO_LE32(PIMA_COMMAND_SIZE(1)),
374 .Type = CPU_TO_LE16(PIMA_CONTAINER_CommandBlock),
375 .Code = CPU_TO_LE16(0x1003),
376 .Params = {CPU_TO_LE32(1)},
379 if ((ErrorCode = SI_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
382 if ((ErrorCode = SI_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
385 SIInterfaceInfo->State.IsSessionOpen = false;
387 if ((PIMABlock.Type != CPU_TO_LE16(PIMA_CONTAINER_ResponseBlock)) || (PIMABlock.Code != CPU_TO_LE16(0x2001)))
388 return SI_ERROR_LOGICAL_CMD_FAILED;
390 return PIPE_RWSTREAM_NoError;
393 uint8_t SI_Host_SendCommand(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo,
394 const uint16_t Operation,
395 const uint8_t TotalParams,
396 uint32_t* const Params)
398 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
399 return PIPE_RWSTREAM_DeviceDisconnected;
403 PIMA_Container_t PIMABlock = (PIMA_Container_t)
405 .DataLength = cpu_to_le32(PIMA_COMMAND_SIZE(TotalParams)),
406 .Type = CPU_TO_LE16(PIMA_CONTAINER_CommandBlock),
407 .Code = cpu_to_le16(Operation),
410 memcpy(&PIMABlock.Params, Params, sizeof(uint32_t) * TotalParams);
412 if ((ErrorCode = SI_Host_SendBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
415 return PIPE_RWSTREAM_NoError;
418 uint8_t SI_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t* const SIInterfaceInfo)
421 PIMA_Container_t PIMABlock;
423 if ((USB_HostState != HOST_STATE_Configured) || !(SIInterfaceInfo->State.IsActive))
424 return PIPE_RWSTREAM_DeviceDisconnected;
426 if ((ErrorCode = SI_Host_ReceiveBlockHeader(SIInterfaceInfo, &PIMABlock)) != PIPE_RWSTREAM_NoError)
429 if ((PIMABlock.Type != CPU_TO_LE16(PIMA_CONTAINER_ResponseBlock)) || (PIMABlock.Code != CPU_TO_LE16(0x2001)))
430 return SI_ERROR_LOGICAL_CMD_FAILED;
432 return PIPE_RWSTREAM_NoError;