3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] 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 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
128 if (Header
->Type
== DTYPE_Interface
)
130 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
132 if ((Interface
->Class
== SI_CSCP_StillImageClass
) &&
133 (Interface
->SubClass
== SI_CSCP_StillImageSubclass
) &&
134 (Interface
->Protocol
== SI_CSCP_BulkOnlyProtocol
))
136 return DESCRIPTOR_SEARCH_Found
;
140 return DESCRIPTOR_SEARCH_NotFound
;
143 uint8_t DCOMP_SI_Host_NextSIInterfaceEndpoint(void* const CurrentDescriptor
)
145 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
147 if (Header
->Type
== DTYPE_Endpoint
)
149 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
151 uint8_t EndpointType
= (Endpoint
->Attributes
& EP_TYPE_MASK
);
153 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
154 (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
))))
156 return DESCRIPTOR_SEARCH_Found
;
159 else if (Header
->Type
== DTYPE_Interface
)
161 return DESCRIPTOR_SEARCH_Fail
;
164 return DESCRIPTOR_SEARCH_NotFound
;
167 uint8_t SI_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
168 PIMA_Container_t
* const PIMAHeader
)
172 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
173 return PIPE_RWSTREAM_DeviceDisconnected
;
175 if (SIInterfaceInfo
->State
.IsSessionOpen
)
176 PIMAHeader
->TransactionID
= SIInterfaceInfo
->State
.TransactionID
++;
178 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
181 if ((ErrorCode
= Pipe_Write_Stream_LE(PIMAHeader
, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
184 uint8_t ParamBytes
= (PIMAHeader
->DataLength
- PIMA_COMMAND_SIZE(0));
188 if ((ErrorCode
= Pipe_Write_Stream_LE(&PIMAHeader
->Params
, ParamBytes
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
195 return PIPE_RWSTREAM_NoError
;
198 uint8_t SI_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
199 PIMA_Container_t
* const PIMAHeader
)
201 uint16_t TimeoutMSRem
= SI_COMMAND_DATA_TIMEOUT_MS
;
202 uint16_t PreviousFrameNumber
= USB_Host_GetFrameNumber();
204 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
205 return PIPE_RWSTREAM_DeviceDisconnected
;
207 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
210 while (!(Pipe_IsINReceived()))
212 uint16_t CurrentFrameNumber
= USB_Host_GetFrameNumber();
214 if (CurrentFrameNumber
!= PreviousFrameNumber
)
216 PreviousFrameNumber
= CurrentFrameNumber
;
218 if (!(TimeoutMSRem
--))
219 return PIPE_RWSTREAM_Timeout
;
223 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
226 if (Pipe_IsStalled())
228 USB_Host_ClearPipeStall(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
229 return PIPE_RWSTREAM_PipeStalled
;
233 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
236 if (Pipe_IsStalled())
238 USB_Host_ClearPipeStall(SIInterfaceInfo
->Config
.DataINPipeNumber
);
239 return PIPE_RWSTREAM_PipeStalled
;
242 if (USB_HostState
== HOST_STATE_Unattached
)
243 return PIPE_RWSTREAM_DeviceDisconnected
;
246 Pipe_Read_Stream_LE(PIMAHeader
, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK
);
248 if (PIMAHeader
->Type
== PIMA_CONTAINER_ResponseBlock
)
250 uint8_t ParamBytes
= (PIMAHeader
->DataLength
- PIMA_COMMAND_SIZE(0));
253 Pipe_Read_Stream_LE(&PIMAHeader
->Params
, ParamBytes
, NO_STREAM_CALLBACK
);
260 return PIPE_RWSTREAM_NoError
;
263 uint8_t SI_Host_SendData(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
265 const uint16_t Bytes
)
269 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
270 return PIPE_RWSTREAM_DeviceDisconnected
;
272 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
275 ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Bytes
, NO_STREAM_CALLBACK
);
283 uint8_t SI_Host_ReadData(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
285 const uint16_t Bytes
)
289 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
290 return PIPE_RWSTREAM_DeviceDisconnected
;
292 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
295 ErrorCode
= Pipe_Read_Stream_LE(Buffer
, Bytes
, NO_STREAM_CALLBACK
);
302 bool SI_Host_IsEventReceived(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
304 bool IsEventReceived
= false;
306 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
309 Pipe_SelectPipe(SIInterfaceInfo
->Config
.EventsPipeNumber
);
312 if (Pipe_BytesInPipe())
313 IsEventReceived
= true;
317 return IsEventReceived
;
320 uint8_t SI_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
321 PIMA_Container_t
* const PIMAHeader
)
325 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
326 return PIPE_RWSTREAM_DeviceDisconnected
;
328 Pipe_SelectPipe(SIInterfaceInfo
->Config
.EventsPipeNumber
);
331 ErrorCode
= Pipe_Read_Stream_LE(PIMAHeader
, sizeof(PIMA_Container_t
), NO_STREAM_CALLBACK
);
339 uint8_t SI_Host_OpenSession(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
341 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
342 return HOST_SENDCONTROL_DeviceDisconnected
;
346 SIInterfaceInfo
->State
.TransactionID
= 0;
347 SIInterfaceInfo
->State
.IsSessionOpen
= false;
349 PIMA_Container_t PIMABlock
= (PIMA_Container_t
)
351 .DataLength
= PIMA_COMMAND_SIZE(1),
352 .Type
= PIMA_CONTAINER_CommandBlock
,
357 if ((ErrorCode
= SI_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
360 if ((ErrorCode
= SI_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
363 if ((PIMABlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
364 return SI_ERROR_LOGICAL_CMD_FAILED
;
366 SIInterfaceInfo
->State
.IsSessionOpen
= true;
368 return PIPE_RWSTREAM_NoError
;
371 uint8_t SI_Host_CloseSession(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
373 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
374 return HOST_SENDCONTROL_DeviceDisconnected
;
378 PIMA_Container_t PIMABlock
= (PIMA_Container_t
)
380 .DataLength
= PIMA_COMMAND_SIZE(1),
381 .Type
= PIMA_CONTAINER_CommandBlock
,
386 if ((ErrorCode
= SI_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
389 if ((ErrorCode
= SI_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
392 SIInterfaceInfo
->State
.IsSessionOpen
= false;
394 if ((PIMABlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
395 return SI_ERROR_LOGICAL_CMD_FAILED
;
397 return PIPE_RWSTREAM_NoError
;
400 uint8_t SI_Host_SendCommand(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
401 const uint16_t Operation
,
402 const uint8_t TotalParams
,
403 uint32_t* const Params
)
405 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
406 return HOST_SENDCONTROL_DeviceDisconnected
;
410 PIMA_Container_t PIMABlock
= (PIMA_Container_t
)
412 .DataLength
= PIMA_COMMAND_SIZE(TotalParams
),
413 .Type
= PIMA_CONTAINER_CommandBlock
,
417 memcpy(&PIMABlock
.Params
, Params
, sizeof(uint32_t) * TotalParams
);
419 if ((ErrorCode
= SI_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
422 return PIPE_RWSTREAM_NoError
;
425 uint8_t SI_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
428 PIMA_Container_t PIMABlock
;
430 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
431 return HOST_SENDCONTROL_DeviceDisconnected
;
433 if ((ErrorCode
= SI_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
436 if ((PIMABlock
.Type
!= PIMA_CONTAINER_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
437 return SI_ERROR_LOGICAL_CMD_FAILED
;
439 return PIPE_RWSTREAM_NoError
;