Added documentation for the constants and enums of the new StillImage Host Class...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / StillImage.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #define INCLUDE_FROM_SI_CLASS_HOST_C
35 #include "StillImage.h"
36
37 #warning The Still Image Host mode Class driver is currently incomplete and is for preview purposes only.
38
39 uint8_t SI_Host_ConfigurePipes(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, uint16_t ConfigDescriptorLength,
40 uint8_t* DeviceConfigDescriptor)
41 {
42 uint8_t FoundEndpoints = 0;
43
44 memset(&SIInterfaceInfo->State, 0x00, sizeof(SIInterfaceInfo->State));
45
46 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
47 return SI_ENUMERROR_InvalidConfigDescriptor;
48
49 if (USB_GetNextDescriptorComp(&ConfigDescriptorLength, &DeviceConfigDescriptor,
50 DComp_SI_Host_NextSIInterface) != DESCRIPTOR_SEARCH_COMP_Found)
51 {
52 return SI_ENUMERROR_NoSIInterfaceFound;
53 }
54
55 while (FoundEndpoints != (SI_FOUND_EVENTS_IN | SI_FOUND_DATAPIPE_IN | SI_FOUND_DATAPIPE_OUT))
56 {
57 if (USB_GetNextDescriptorComp(&ConfigDescriptorLength, &DeviceConfigDescriptor,
58 DComp_SI_Host_NextSIInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
59 {
60 return SI_ENUMERROR_EndpointsNotFound;
61 }
62
63 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
64
65 if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
66 {
67 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
68 {
69 Pipe_ConfigurePipe(SIInterfaceInfo->Config.EventsPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
70 EndpointData->EndpointAddress, EndpointData->EndpointSize,
71 PIPE_BANK_DOUBLE);
72 SIInterfaceInfo->State.EventsPipeSize = EndpointData->EndpointSize;
73
74 Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
75
76 FoundEndpoints |= SI_FOUND_EVENTS_IN;
77 }
78 }
79 else
80 {
81 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
82 {
83 Pipe_ConfigurePipe(SIInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
84 EndpointData->EndpointAddress, EndpointData->EndpointSize,
85 PIPE_BANK_DOUBLE);
86 SIInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
87
88 FoundEndpoints |= SI_FOUND_DATAPIPE_IN;
89 }
90 else
91 {
92 Pipe_ConfigurePipe(SIInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
93 EndpointData->EndpointAddress, EndpointData->EndpointSize,
94 PIPE_BANK_DOUBLE);
95 SIInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
96
97 FoundEndpoints |= SI_FOUND_DATAPIPE_OUT;
98 }
99 }
100 }
101
102 return SI_ENUMERROR_NoError;
103 }
104
105 uint8_t DComp_SI_Host_NextSIInterface(void* CurrentDescriptor)
106 {
107 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
108 {
109 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
110 USB_Descriptor_Interface_t);
111
112 if ((CurrentInterface->Class == STILL_IMAGE_CLASS) &&
113 (CurrentInterface->SubClass == STILL_IMAGE_SUBCLASS) &&
114 (CurrentInterface->Protocol == STILL_IMAGE_PROTOCOL))
115 {
116 return DESCRIPTOR_SEARCH_Found;
117 }
118 }
119
120 return DESCRIPTOR_SEARCH_NotFound;
121 }
122
123 uint8_t DComp_SI_Host_NextSIInterfaceEndpoint(void* CurrentDescriptor)
124 {
125 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
126 {
127 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
128 USB_Descriptor_Endpoint_t);
129
130 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
131
132 if (((EndpointType == EP_TYPE_BULK) || (EndpointType == EP_TYPE_INTERRUPT)) &&
133 (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress))))
134 {
135 return DESCRIPTOR_SEARCH_Found;
136 }
137 }
138 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
139 {
140 return DESCRIPTOR_SEARCH_Fail;
141 }
142
143 return DESCRIPTOR_SEARCH_NotFound;
144 }
145
146 void SI_Host_USBTask(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
147 {
148
149 }
150
151 void SImage_Host_SendBlockHeader(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, SI_PIMA_Container_t* PIMAHeader)
152 {
153 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
154 Pipe_Unfreeze();
155
156 Pipe_Write_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK);
157
158 if (PIMAHeader->Type == CType_CommandBlock)
159 {
160 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
161
162 if (ParamBytes)
163 Pipe_Write_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK);
164
165 Pipe_ClearOUT();
166 }
167
168 Pipe_Freeze();
169 }
170
171 uint8_t SImage_Host_RecieveBlockHeader(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, SI_PIMA_Container_t* PIMAHeader)
172 {
173 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
174
175 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
176 Pipe_Unfreeze();
177
178 while (!(Pipe_IsReadWriteAllowed()))
179 {
180 if (USB_INT_HasOccurred(USB_INT_HSOFI))
181 {
182 USB_INT_Clear(USB_INT_HSOFI);
183 TimeoutMSRem--;
184
185 if (!(TimeoutMSRem))
186 {
187 return PIPE_RWSTREAM_Timeout;
188 }
189 }
190
191 Pipe_Freeze();
192 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
193 Pipe_Unfreeze();
194
195 if (Pipe_IsStalled())
196 {
197 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataOUTPipeNumber);
198 return PIPE_RWSTREAM_PipeStalled;
199 }
200
201 Pipe_Freeze();
202 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
203 Pipe_Unfreeze();
204
205 if (Pipe_IsStalled())
206 {
207 USB_Host_ClearPipeStall(SIInterfaceInfo->Config.DataINPipeNumber);
208 return PIPE_RWSTREAM_PipeStalled;
209 }
210
211 if (USB_HostState == HOST_STATE_Unattached)
212 return PIPE_RWSTREAM_DeviceDisconnected;
213 }
214
215 Pipe_Read_Stream_LE(PIMAHeader, PIMA_COMMAND_SIZE(0), NO_STREAM_CALLBACK);
216
217 if (PIMAHeader->Type == CType_ResponseBlock)
218 {
219 uint8_t ParamBytes = (PIMAHeader->DataLength - PIMA_COMMAND_SIZE(0));
220
221 if (ParamBytes)
222 Pipe_Read_Stream_LE(&PIMAHeader->Params, ParamBytes, NO_STREAM_CALLBACK);
223
224 Pipe_ClearIN();
225
226 PIMAHeader->Code &= 0x0000000F;
227 }
228
229 Pipe_Freeze();
230
231 return PIPE_RWSTREAM_NoError;
232 }
233
234 uint8_t SImage_Host_SendData(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, void* Buffer, uint16_t Bytes)
235 {
236 uint8_t ErrorCode;
237
238 Pipe_SelectPipe(SIInterfaceInfo->Config.DataOUTPipeNumber);
239 Pipe_Unfreeze();
240
241 ErrorCode = Pipe_Write_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
242
243 Pipe_ClearOUT();
244 Pipe_Freeze();
245
246 return ErrorCode;
247 }
248
249 uint8_t SImage_Host_ReadData(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, void* Buffer, uint16_t Bytes)
250 {
251 uint8_t ErrorCode;
252
253 Pipe_SelectPipe(SIInterfaceInfo->Config.DataINPipeNumber);
254 Pipe_Unfreeze();
255
256 ErrorCode = Pipe_Read_Stream_LE(Buffer, Bytes, NO_STREAM_CALLBACK);
257
258 Pipe_Freeze();
259
260 return ErrorCode;
261 }
262
263 bool SImage_Host_IsEventReceived(USB_ClassInfo_SI_Host_t* SIInterfaceInfo)
264 {
265 bool IsEventReceived = false;
266
267 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
268 Pipe_Unfreeze();
269
270 if (Pipe_BytesInPipe())
271 IsEventReceived = true;
272
273 Pipe_Freeze();
274
275 return IsEventReceived;
276 }
277
278 uint8_t SImage_Host_RecieveEventHeader(USB_ClassInfo_SI_Host_t* SIInterfaceInfo, SI_PIMA_Container_t* PIMAHeader)
279 {
280 uint8_t ErrorCode;
281
282 Pipe_SelectPipe(SIInterfaceInfo->Config.EventsPipeNumber);
283 Pipe_Unfreeze();
284
285 ErrorCode = Pipe_Read_Stream_LE(PIMAHeader, sizeof(SI_PIMA_Container_t), NO_STREAM_CALLBACK);
286
287 Pipe_ClearIN();
288 Pipe_Freeze();
289
290 return ErrorCode;
291 }
292
293 #endif