3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define __INCLUDE_FROM_SI_DRIVER
36 #define __INCLUDE_FROM_STILLIMAGE_HOST_C
37 #include "StillImage.h"
39 uint8_t SI_Host_ConfigurePipes(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* ConfigDescriptorData
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Endpoint_t
* EventsEndpoint
= NULL
;
46 USB_Descriptor_Interface_t
* StillImageInterface
= NULL
;
48 memset(&SIInterfaceInfo
->State
, 0x00, sizeof(SIInterfaceInfo
->State
));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
51 return SI_ENUMERROR_InvalidConfigDescriptor
;
53 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
55 if (!(StillImageInterface
) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
57 DCOMP_SI_Host_NextSIInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
59 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
60 DCOMP_SI_Host_NextSIInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
62 return SI_ENUMERROR_NoCompatibleInterfaceFound
;
65 StillImageInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
67 DataINEndpoint
= NULL
;
68 DataOUTEndpoint
= NULL
;
69 EventsEndpoint
= NULL
;
74 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
76 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
78 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
79 EventsEndpoint
= EndpointData
;
81 DataINEndpoint
= EndpointData
;
85 DataOUTEndpoint
= EndpointData
;
89 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
91 if (PipeNum
== SIInterfaceInfo
->Config
.DataINPipeNumber
)
93 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
94 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
95 SIInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
97 SIInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
99 else if (PipeNum
== SIInterfaceInfo
->Config
.DataOUTPipeNumber
)
101 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
102 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
103 SIInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
105 SIInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
107 else if (PipeNum
== SIInterfaceInfo
->Config
.EventsPipeNumber
)
109 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
110 EventsEndpoint
->EndpointAddress
, EventsEndpoint
->EndpointSize
,
111 SIInterfaceInfo
->Config
.EventsPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
112 Pipe_SetInterruptPeriod(EventsEndpoint
->PollingIntervalMS
);
114 SIInterfaceInfo
->State
.EventsPipeSize
= EventsEndpoint
->EndpointSize
;
118 SIInterfaceInfo
->State
.InterfaceNumber
= StillImageInterface
->InterfaceNumber
;
119 SIInterfaceInfo
->State
.IsActive
= true;
121 return SI_ENUMERROR_NoError
;
124 uint8_t DCOMP_SI_Host_NextSIInterface(void* const CurrentDescriptor
)
126 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
128 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
129 USB_Descriptor_Interface_t
);
131 if ((CurrentInterface
->Class
== SI_CSCP_StillImageClass
) &&
132 (CurrentInterface
->SubClass
== SI_CSCP_StillImageSubclass
) &&
133 (CurrentInterface
->Protocol
== SI_CSCP_BulkOnlyProtocol
))
135 return DESCRIPTOR_SEARCH_Found
;
139 return DESCRIPTOR_SEARCH_NotFound
;
142 uint8_t DCOMP_SI_Host_NextSIInterfaceEndpoint(void* const CurrentDescriptor
)
144 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
146 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
147 USB_Descriptor_Endpoint_t
);
149 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
151 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
152 (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
))))
154 return DESCRIPTOR_SEARCH_Found
;
157 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
159 return DESCRIPTOR_SEARCH_Fail
;
162 return DESCRIPTOR_SEARCH_NotFound
;
165 uint8_t SI_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
166 PIMA_Container_t
* const PIMAHeader
)
170 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
171 return PIPE_RWSTREAM_DeviceDisconnected
;
173 if (SIInterfaceInfo
->State
.IsSessionOpen
)
174 PIMAHeader
->TransactionID
= SIInterfaceInfo
->State
.TransactionID
++;
176 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
179 if ((ErrorCode
= Pipe_Write_Stream_LE(PIMAHeader
, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
182 uint8_t ParamBytes
= (PIMAHeader
->DataLength
- PIMA_COMMAND_SIZE(0));
186 if ((ErrorCode
= Pipe_Write_Stream_LE(&PIMAHeader
->Params
, ParamBytes
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
193 return PIPE_RWSTREAM_NoError
;
196 uint8_t SI_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
197 PIMA_Container_t
* const PIMAHeader
)
199 uint16_t TimeoutMSRem
= SI_COMMAND_DATA_TIMEOUT_MS
;
200 uint16_t PreviousFrameNumber
= USB_Host_GetFrameNumber();
202 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
203 return PIPE_RWSTREAM_DeviceDisconnected
;
205 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
208 while (!(Pipe_IsINReceived()))
210 uint16_t CurrentFrameNumber
= USB_Host_GetFrameNumber();
212 if (CurrentFrameNumber
!= PreviousFrameNumber
)
214 PreviousFrameNumber
= CurrentFrameNumber
;
216 if (!(TimeoutMSRem
--))
217 return PIPE_RWSTREAM_Timeout
;
221 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
224 if (Pipe_IsStalled())
226 USB_Host_ClearPipeStall(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
227 return PIPE_RWSTREAM_PipeStalled
;
231 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
234 if (Pipe_IsStalled())
236 USB_Host_ClearPipeStall(SIInterfaceInfo
->Config
.DataINPipeNumber
);
237 return PIPE_RWSTREAM_PipeStalled
;
240 if (USB_HostState
== HOST_STATE_Unattached
)
241 return PIPE_RWSTREAM_DeviceDisconnected
;
244 Pipe_Read_Stream_LE(PIMAHeader
, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK
);
246 if (PIMAHeader
->Type
== PIMA_CONTAINER_ResponseBlock
)
248 uint8_t ParamBytes
= (PIMAHeader
->DataLength
- PIMA_COMMAND_SIZE(0));
251 Pipe_Read_Stream_LE(&PIMAHeader
->Params
, ParamBytes
, NO_STREAM_CALLBACK
);
258 return PIPE_RWSTREAM_NoError
;
261 uint8_t SI_Host_SendData(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
263 const uint16_t Bytes
)
267 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
268 return PIPE_RWSTREAM_DeviceDisconnected
;
270 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
273 ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Bytes
, NO_STREAM_CALLBACK
);
281 uint8_t SI_Host_ReadData(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
283 const uint16_t Bytes
)
287 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
288 return PIPE_RWSTREAM_DeviceDisconnected
;
290 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
293 ErrorCode
= Pipe_Read_Stream_LE(Buffer
, Bytes
, NO_STREAM_CALLBACK
);
300 bool SI_Host_IsEventReceived(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
302 bool IsEventReceived
= false;
304 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
307 Pipe_SelectPipe(SIInterfaceInfo
->Config
.EventsPipeNumber
);
310 if (Pipe_BytesInPipe())
311 IsEventReceived
= true;
315 return IsEventReceived
;
318 uint8_t SI_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
319 PIMA_Container_t
* const PIMAHeader
)
323 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
324 return PIPE_RWSTREAM_DeviceDisconnected
;
326 Pipe_SelectPipe(SIInterfaceInfo
->Config
.EventsPipeNumber
);
329 ErrorCode
= Pipe_Read_Stream_LE(PIMAHeader
, sizeof(PIMA_Container_t
), NO_STREAM_CALLBACK
);
337 uint8_t SI_Host_OpenSession(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
339 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
340 return HOST_SENDCONTROL_DeviceDisconnected
;
344 SIInterfaceInfo
->State
.TransactionID
= 0;
345 SIInterfaceInfo
->State
.IsSessionOpen
= false;
347 PIMA_Container_t PIMABlock
= (PIMA_Container_t
)
349 .DataLength
= PIMA_COMMAND_SIZE(1),
350 .Type
= PIMA_CONTAINER_CommandBlock
,
355 if ((ErrorCode
= SI_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
358 if ((ErrorCode
= SI_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
361 if ((PIMABlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
362 return SI_ERROR_LOGICAL_CMD_FAILED
;
364 SIInterfaceInfo
->State
.IsSessionOpen
= true;
366 return PIPE_RWSTREAM_NoError
;
369 uint8_t SI_Host_CloseSession(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
371 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
372 return HOST_SENDCONTROL_DeviceDisconnected
;
376 PIMA_Container_t PIMABlock
= (PIMA_Container_t
)
378 .DataLength
= PIMA_COMMAND_SIZE(1),
379 .Type
= PIMA_CONTAINER_CommandBlock
,
384 if ((ErrorCode
= SI_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
387 if ((ErrorCode
= SI_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
390 SIInterfaceInfo
->State
.IsSessionOpen
= false;
392 if ((PIMABlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
393 return SI_ERROR_LOGICAL_CMD_FAILED
;
395 return PIPE_RWSTREAM_NoError
;
398 uint8_t SI_Host_SendCommand(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
399 const uint16_t Operation
,
400 const uint8_t TotalParams
,
401 uint32_t* const Params
)
403 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
404 return HOST_SENDCONTROL_DeviceDisconnected
;
408 PIMA_Container_t PIMABlock
= (PIMA_Container_t
)
410 .DataLength
= PIMA_COMMAND_SIZE(TotalParams
),
411 .Type
= PIMA_CONTAINER_CommandBlock
,
415 memcpy(&PIMABlock
.Params
, Params
, sizeof(uint32_t) * TotalParams
);
417 if ((ErrorCode
= SI_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
420 return PIPE_RWSTREAM_NoError
;
423 uint8_t SI_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
426 PIMA_Container_t PIMABlock
;
428 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
429 return HOST_SENDCONTROL_DeviceDisconnected
;
431 if ((ErrorCode
= SI_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
434 if ((PIMABlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
435 return SI_ERROR_LOGICAL_CMD_FAILED
;
437 return PIPE_RWSTREAM_NoError
;