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_CLASS_HOST_C
36 #define __INCLUDE_FROM_SI_DRIVER
37 #include "StillImage.h"
39 uint8_t SImage_Host_ConfigurePipes(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* DeviceConfigDescriptor
)
43 uint8_t FoundEndpoints
= 0;
45 memset(&SIInterfaceInfo
->State
, 0x00, sizeof(SIInterfaceInfo
->State
));
47 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor
) != DTYPE_Configuration
)
48 return SI_ENUMERROR_InvalidConfigDescriptor
;
50 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
51 DCOMP_SI_Host_NextSIInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
53 return SI_ENUMERROR_NoSIInterfaceFound
;
56 while (FoundEndpoints
!= (SI_FOUND_EVENTS_IN
| SI_FOUND_DATAPIPE_IN
| SI_FOUND_DATAPIPE_OUT
))
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
59 DCOMP_SI_Host_NextSIInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
61 return SI_ENUMERROR_EndpointsNotFound
;
64 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Endpoint_t
);
66 if ((EndpointData
->Attributes
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)
68 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
70 Pipe_ConfigurePipe(SIInterfaceInfo
->Config
.EventsPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
71 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
72 SIInterfaceInfo
->Config
.EventsPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
73 SIInterfaceInfo
->State
.EventsPipeSize
= EndpointData
->EndpointSize
;
75 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
);
77 FoundEndpoints
|= SI_FOUND_EVENTS_IN
;
82 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
84 Pipe_ConfigurePipe(SIInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
85 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
86 SIInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
87 SIInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
89 FoundEndpoints
|= SI_FOUND_DATAPIPE_IN
;
93 Pipe_ConfigurePipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
94 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
95 SIInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
96 SIInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
98 FoundEndpoints
|= SI_FOUND_DATAPIPE_OUT
;
103 SIInterfaceInfo
->State
.IsActive
= true;
104 return SI_ENUMERROR_NoError
;
107 uint8_t DCOMP_SI_Host_NextSIInterface(void* const CurrentDescriptor
)
109 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
111 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
112 USB_Descriptor_Interface_t
);
114 if ((CurrentInterface
->Class
== STILL_IMAGE_CLASS
) &&
115 (CurrentInterface
->SubClass
== STILL_IMAGE_SUBCLASS
) &&
116 (CurrentInterface
->Protocol
== STILL_IMAGE_PROTOCOL
))
118 return DESCRIPTOR_SEARCH_Found
;
122 return DESCRIPTOR_SEARCH_NotFound
;
125 uint8_t DCOMP_SI_Host_NextSIInterfaceEndpoint(void* const CurrentDescriptor
)
127 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
129 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
130 USB_Descriptor_Endpoint_t
);
132 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
134 if (((EndpointType
== EP_TYPE_BULK
) || (EndpointType
== EP_TYPE_INTERRUPT
)) &&
135 (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
))))
137 return DESCRIPTOR_SEARCH_Found
;
140 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
142 return DESCRIPTOR_SEARCH_Fail
;
145 return DESCRIPTOR_SEARCH_NotFound
;
148 uint8_t SImage_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
149 SI_PIMA_Container_t
* const PIMAHeader
)
153 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
154 return PIPE_RWSTREAM_DeviceDisconnected
;
156 if (SIInterfaceInfo
->State
.IsSessionOpen
)
157 PIMAHeader
->TransactionID
= SIInterfaceInfo
->State
.TransactionID
++;
159 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
162 if ((ErrorCode
= Pipe_Write_Stream_LE(PIMAHeader
, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
165 uint8_t ParamBytes
= (PIMAHeader
->DataLength
- PIMA_COMMAND_SIZE(0));
169 if ((ErrorCode
= Pipe_Write_Stream_LE(&PIMAHeader
->Params
, ParamBytes
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
176 return PIPE_RWSTREAM_NoError
;
179 uint8_t SImage_Host_ReceiveBlockHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
180 SI_PIMA_Container_t
* const PIMAHeader
)
182 uint16_t TimeoutMSRem
= COMMAND_DATA_TIMEOUT_MS
;
184 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
185 return PIPE_RWSTREAM_DeviceDisconnected
;
187 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
190 while (!(Pipe_IsReadWriteAllowed()))
192 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
194 USB_INT_Clear(USB_INT_HSOFI
);
199 return PIPE_RWSTREAM_Timeout
;
204 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
207 if (Pipe_IsStalled())
209 USB_Host_ClearPipeStall(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
210 return PIPE_RWSTREAM_PipeStalled
;
214 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
217 if (Pipe_IsStalled())
219 USB_Host_ClearPipeStall(SIInterfaceInfo
->Config
.DataINPipeNumber
);
220 return PIPE_RWSTREAM_PipeStalled
;
223 if (USB_HostState
== HOST_STATE_Unattached
)
224 return PIPE_RWSTREAM_DeviceDisconnected
;
227 Pipe_Read_Stream_LE(PIMAHeader
, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK
);
229 if (PIMAHeader
->Type
== CType_ResponseBlock
)
231 uint8_t ParamBytes
= (PIMAHeader
->DataLength
- PIMA_COMMAND_SIZE(0));
234 Pipe_Read_Stream_LE(&PIMAHeader
->Params
, ParamBytes
, NO_STREAM_CALLBACK
);
241 return PIPE_RWSTREAM_NoError
;
244 uint8_t SImage_Host_SendData(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
246 const uint16_t Bytes
)
250 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
251 return PIPE_RWSTREAM_DeviceDisconnected
;
253 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataOUTPipeNumber
);
256 ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Bytes
, NO_STREAM_CALLBACK
);
264 uint8_t SImage_Host_ReadData(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
266 const uint16_t Bytes
)
270 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
271 return PIPE_RWSTREAM_DeviceDisconnected
;
273 Pipe_SelectPipe(SIInterfaceInfo
->Config
.DataINPipeNumber
);
276 ErrorCode
= Pipe_Read_Stream_LE(Buffer
, Bytes
, NO_STREAM_CALLBACK
);
283 bool SImage_Host_IsEventReceived(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
285 bool IsEventReceived
= false;
287 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
290 Pipe_SelectPipe(SIInterfaceInfo
->Config
.EventsPipeNumber
);
293 if (Pipe_BytesInPipe())
294 IsEventReceived
= true;
298 return IsEventReceived
;
301 uint8_t SImage_Host_ReceiveEventHeader(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
302 SI_PIMA_Container_t
* const PIMAHeader
)
306 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
307 return PIPE_RWSTREAM_DeviceDisconnected
;
309 Pipe_SelectPipe(SIInterfaceInfo
->Config
.EventsPipeNumber
);
312 ErrorCode
= Pipe_Read_Stream_LE(PIMAHeader
, sizeof(SI_PIMA_Container_t
), NO_STREAM_CALLBACK
);
320 uint8_t SImage_Host_OpenSession(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
322 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
323 return HOST_SENDCONTROL_DeviceDisconnected
;
327 SIInterfaceInfo
->State
.TransactionID
= 0;
328 SIInterfaceInfo
->State
.IsSessionOpen
= false;
330 SI_PIMA_Container_t PIMABlock
= (SI_PIMA_Container_t
)
332 .DataLength
= PIMA_COMMAND_SIZE(1),
333 .Type
= CType_CommandBlock
,
338 if ((ErrorCode
= SImage_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
341 if ((ErrorCode
= SImage_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
344 if ((PIMABlock
.Type
!= CType_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
345 return SI_ERROR_LOGICAL_CMD_FAILED
;
347 SIInterfaceInfo
->State
.IsSessionOpen
= true;
349 return PIPE_RWSTREAM_NoError
;
352 uint8_t SImage_Host_CloseSession(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
354 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
355 return HOST_SENDCONTROL_DeviceDisconnected
;
359 SI_PIMA_Container_t PIMABlock
= (SI_PIMA_Container_t
)
361 .DataLength
= PIMA_COMMAND_SIZE(1),
362 .Type
= CType_CommandBlock
,
367 if ((ErrorCode
= SImage_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
370 if ((ErrorCode
= SImage_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
373 SIInterfaceInfo
->State
.IsSessionOpen
= false;
375 if ((PIMABlock
.Type
!= CType_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
376 return SI_ERROR_LOGICAL_CMD_FAILED
;
378 return PIPE_RWSTREAM_NoError
;
381 uint8_t SImage_Host_SendCommand(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
,
382 const uint16_t Operation
,
383 const uint8_t TotalParams
,
384 uint32_t* const Params
)
386 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
387 return HOST_SENDCONTROL_DeviceDisconnected
;
391 SI_PIMA_Container_t PIMABlock
= (SI_PIMA_Container_t
)
393 .DataLength
= PIMA_COMMAND_SIZE(TotalParams
),
394 .Type
= CType_CommandBlock
,
398 memcpy(&PIMABlock
.Params
, Params
, sizeof(uint32_t) * TotalParams
);
400 if ((ErrorCode
= SImage_Host_SendBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
403 return PIPE_RWSTREAM_NoError
;
406 uint8_t SImage_Host_ReceiveResponse(USB_ClassInfo_SI_Host_t
* const SIInterfaceInfo
)
409 SI_PIMA_Container_t PIMABlock
;
411 if ((USB_HostState
!= HOST_STATE_Configured
) || !(SIInterfaceInfo
->State
.IsActive
))
412 return HOST_SENDCONTROL_DeviceDisconnected
;
414 if ((ErrorCode
= SImage_Host_ReceiveBlockHeader(SIInterfaceInfo
, &PIMABlock
)) != PIPE_RWSTREAM_NoError
)
417 if ((PIMABlock
.Type
!= CType_ResponseBlock
) || (PIMABlock
.Code
!= 0x2001))
418 return SI_ERROR_LOGICAL_CMD_FAILED
;
420 return PIPE_RWSTREAM_NoError
;